Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2004 Topspin Communications. All rights reserved. |
| 3 | * Copyright (c) 2005 Mellanox Technologies. All rights reserved. |
| 4 | * Copyright (c) 2006, 2007 Cisco Systems, Inc. All rights reserved. |
| 5 | * |
| 6 | * This software is available to you under a choice of one of two |
| 7 | * licenses. You may choose to be licensed under the terms of the GNU |
| 8 | * General Public License (GPL) Version 2, available from the file |
| 9 | * COPYING in the main directory of this source tree, or the |
| 10 | * OpenIB.org BSD license below: |
| 11 | * |
| 12 | * Redistribution and use in source and binary forms, with or |
| 13 | * without modification, are permitted provided that the following |
| 14 | * conditions are met: |
| 15 | * |
| 16 | * - Redistributions of source code must retain the above |
| 17 | * copyright notice, this list of conditions and the following |
| 18 | * disclaimer. |
| 19 | * |
| 20 | * - Redistributions in binary form must reproduce the above |
| 21 | * copyright notice, this list of conditions and the following |
| 22 | * disclaimer in the documentation and/or other materials |
| 23 | * provided with the distribution. |
| 24 | * |
| 25 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 26 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 27 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 28 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 29 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 30 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 31 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 32 | * SOFTWARE. |
| 33 | */ |
| 34 | |
| 35 | #include <linux/init.h> |
| 36 | #include <linux/errno.h> |
| 37 | |
| 38 | #include <linux/mlx4/cmd.h> |
| 39 | |
| 40 | #include "mlx4.h" |
| 41 | #include "icm.h" |
| 42 | |
| 43 | /* |
| 44 | * Must be packed because mtt_seg is 64 bits but only aligned to 32 bits. |
| 45 | */ |
| 46 | struct mlx4_mpt_entry { |
| 47 | __be32 flags; |
| 48 | __be32 qpn; |
| 49 | __be32 key; |
| 50 | __be32 pd; |
| 51 | __be64 start; |
| 52 | __be64 length; |
| 53 | __be32 lkey; |
| 54 | __be32 win_cnt; |
| 55 | u8 reserved1[3]; |
| 56 | u8 mtt_rep; |
| 57 | __be64 mtt_seg; |
| 58 | __be32 mtt_sz; |
| 59 | __be32 entity_size; |
| 60 | __be32 first_byte_offset; |
| 61 | } __attribute__((packed)); |
| 62 | |
| 63 | #define MLX4_MPT_FLAG_SW_OWNS (0xfUL << 28) |
| 64 | #define MLX4_MPT_FLAG_MIO (1 << 17) |
| 65 | #define MLX4_MPT_FLAG_BIND_ENABLE (1 << 15) |
| 66 | #define MLX4_MPT_FLAG_PHYSICAL (1 << 9) |
| 67 | #define MLX4_MPT_FLAG_REGION (1 << 8) |
| 68 | |
| 69 | #define MLX4_MTT_FLAG_PRESENT 1 |
| 70 | |
| 71 | static u32 mlx4_buddy_alloc(struct mlx4_buddy *buddy, int order) |
| 72 | { |
| 73 | int o; |
| 74 | int m; |
| 75 | u32 seg; |
| 76 | |
| 77 | spin_lock(&buddy->lock); |
| 78 | |
| 79 | for (o = order; o <= buddy->max_order; ++o) { |
| 80 | m = 1 << (buddy->max_order - o); |
| 81 | seg = find_first_bit(buddy->bits[o], m); |
| 82 | if (seg < m) |
| 83 | goto found; |
| 84 | } |
| 85 | |
| 86 | spin_unlock(&buddy->lock); |
| 87 | return -1; |
| 88 | |
| 89 | found: |
| 90 | clear_bit(seg, buddy->bits[o]); |
| 91 | |
| 92 | while (o > order) { |
| 93 | --o; |
| 94 | seg <<= 1; |
| 95 | set_bit(seg ^ 1, buddy->bits[o]); |
| 96 | } |
| 97 | |
| 98 | spin_unlock(&buddy->lock); |
| 99 | |
| 100 | seg <<= order; |
| 101 | |
| 102 | return seg; |
| 103 | } |
| 104 | |
| 105 | static void mlx4_buddy_free(struct mlx4_buddy *buddy, u32 seg, int order) |
| 106 | { |
| 107 | seg >>= order; |
| 108 | |
| 109 | spin_lock(&buddy->lock); |
| 110 | |
| 111 | while (test_bit(seg ^ 1, buddy->bits[order])) { |
| 112 | clear_bit(seg ^ 1, buddy->bits[order]); |
| 113 | seg >>= 1; |
| 114 | ++order; |
| 115 | } |
| 116 | |
| 117 | set_bit(seg, buddy->bits[order]); |
| 118 | |
| 119 | spin_unlock(&buddy->lock); |
| 120 | } |
| 121 | |
| 122 | static int __devinit mlx4_buddy_init(struct mlx4_buddy *buddy, int max_order) |
| 123 | { |
| 124 | int i, s; |
| 125 | |
| 126 | buddy->max_order = max_order; |
| 127 | spin_lock_init(&buddy->lock); |
| 128 | |
| 129 | buddy->bits = kzalloc((buddy->max_order + 1) * sizeof (long *), |
| 130 | GFP_KERNEL); |
| 131 | if (!buddy->bits) |
| 132 | goto err_out; |
| 133 | |
| 134 | for (i = 0; i <= buddy->max_order; ++i) { |
| 135 | s = BITS_TO_LONGS(1 << (buddy->max_order - i)); |
| 136 | buddy->bits[i] = kmalloc(s * sizeof (long), GFP_KERNEL); |
| 137 | if (!buddy->bits[i]) |
| 138 | goto err_out_free; |
| 139 | bitmap_zero(buddy->bits[i], 1 << (buddy->max_order - i)); |
| 140 | } |
| 141 | |
| 142 | set_bit(0, buddy->bits[buddy->max_order]); |
| 143 | |
| 144 | return 0; |
| 145 | |
| 146 | err_out_free: |
| 147 | for (i = 0; i <= buddy->max_order; ++i) |
| 148 | kfree(buddy->bits[i]); |
| 149 | |
| 150 | kfree(buddy->bits); |
| 151 | |
| 152 | err_out: |
| 153 | return -ENOMEM; |
| 154 | } |
| 155 | |
| 156 | static void mlx4_buddy_cleanup(struct mlx4_buddy *buddy) |
| 157 | { |
| 158 | int i; |
| 159 | |
| 160 | for (i = 0; i <= buddy->max_order; ++i) |
| 161 | kfree(buddy->bits[i]); |
| 162 | |
| 163 | kfree(buddy->bits); |
| 164 | } |
| 165 | |
| 166 | static u32 mlx4_alloc_mtt_range(struct mlx4_dev *dev, int order) |
| 167 | { |
| 168 | struct mlx4_mr_table *mr_table = &mlx4_priv(dev)->mr_table; |
| 169 | u32 seg; |
| 170 | |
| 171 | seg = mlx4_buddy_alloc(&mr_table->mtt_buddy, order); |
| 172 | if (seg == -1) |
| 173 | return -1; |
| 174 | |
| 175 | if (mlx4_table_get_range(dev, &mr_table->mtt_table, seg, |
| 176 | seg + (1 << order) - 1)) { |
| 177 | mlx4_buddy_free(&mr_table->mtt_buddy, seg, order); |
| 178 | return -1; |
| 179 | } |
| 180 | |
| 181 | return seg; |
| 182 | } |
| 183 | |
| 184 | int mlx4_mtt_init(struct mlx4_dev *dev, int npages, int page_shift, |
| 185 | struct mlx4_mtt *mtt) |
| 186 | { |
| 187 | int i; |
| 188 | |
| 189 | if (!npages) { |
| 190 | mtt->order = -1; |
| 191 | mtt->page_shift = MLX4_ICM_PAGE_SHIFT; |
| 192 | return 0; |
| 193 | } else |
| 194 | mtt->page_shift = page_shift; |
| 195 | |
| 196 | for (mtt->order = 0, i = MLX4_MTT_ENTRY_PER_SEG; i < npages; i <<= 1) |
| 197 | ++mtt->order; |
| 198 | |
| 199 | mtt->first_seg = mlx4_alloc_mtt_range(dev, mtt->order); |
| 200 | if (mtt->first_seg == -1) |
| 201 | return -ENOMEM; |
| 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | EXPORT_SYMBOL_GPL(mlx4_mtt_init); |
| 206 | |
| 207 | void mlx4_mtt_cleanup(struct mlx4_dev *dev, struct mlx4_mtt *mtt) |
| 208 | { |
| 209 | struct mlx4_mr_table *mr_table = &mlx4_priv(dev)->mr_table; |
| 210 | |
| 211 | if (mtt->order < 0) |
| 212 | return; |
| 213 | |
| 214 | mlx4_buddy_free(&mr_table->mtt_buddy, mtt->first_seg, mtt->order); |
| 215 | mlx4_table_put_range(dev, &mr_table->mtt_table, mtt->first_seg, |
| 216 | mtt->first_seg + (1 << mtt->order) - 1); |
| 217 | } |
| 218 | EXPORT_SYMBOL_GPL(mlx4_mtt_cleanup); |
| 219 | |
| 220 | u64 mlx4_mtt_addr(struct mlx4_dev *dev, struct mlx4_mtt *mtt) |
| 221 | { |
| 222 | return (u64) mtt->first_seg * dev->caps.mtt_entry_sz; |
| 223 | } |
| 224 | EXPORT_SYMBOL_GPL(mlx4_mtt_addr); |
| 225 | |
| 226 | static u32 hw_index_to_key(u32 ind) |
| 227 | { |
| 228 | return (ind >> 24) | (ind << 8); |
| 229 | } |
| 230 | |
| 231 | static u32 key_to_hw_index(u32 key) |
| 232 | { |
| 233 | return (key << 24) | (key >> 8); |
| 234 | } |
| 235 | |
| 236 | static int mlx4_SW2HW_MPT(struct mlx4_dev *dev, struct mlx4_cmd_mailbox *mailbox, |
| 237 | int mpt_index) |
| 238 | { |
| 239 | return mlx4_cmd(dev, mailbox->dma, mpt_index, 0, MLX4_CMD_SW2HW_MPT, |
| 240 | MLX4_CMD_TIME_CLASS_B); |
| 241 | } |
| 242 | |
| 243 | static int mlx4_HW2SW_MPT(struct mlx4_dev *dev, struct mlx4_cmd_mailbox *mailbox, |
| 244 | int mpt_index) |
| 245 | { |
| 246 | return mlx4_cmd_box(dev, 0, mailbox ? mailbox->dma : 0, mpt_index, |
| 247 | !mailbox, MLX4_CMD_HW2SW_MPT, MLX4_CMD_TIME_CLASS_B); |
| 248 | } |
| 249 | |
| 250 | int mlx4_mr_alloc(struct mlx4_dev *dev, u32 pd, u64 iova, u64 size, u32 access, |
| 251 | int npages, int page_shift, struct mlx4_mr *mr) |
| 252 | { |
| 253 | struct mlx4_priv *priv = mlx4_priv(dev); |
| 254 | u32 index; |
| 255 | int err; |
| 256 | |
| 257 | index = mlx4_bitmap_alloc(&priv->mr_table.mpt_bitmap); |
| 258 | if (index == -1) { |
| 259 | err = -ENOMEM; |
| 260 | goto err; |
| 261 | } |
| 262 | |
| 263 | mr->iova = iova; |
| 264 | mr->size = size; |
| 265 | mr->pd = pd; |
| 266 | mr->access = access; |
| 267 | mr->enabled = 0; |
| 268 | mr->key = hw_index_to_key(index); |
| 269 | |
| 270 | err = mlx4_mtt_init(dev, npages, page_shift, &mr->mtt); |
| 271 | if (err) |
| 272 | goto err_index; |
| 273 | |
| 274 | return 0; |
| 275 | |
| 276 | err_index: |
| 277 | mlx4_bitmap_free(&priv->mr_table.mpt_bitmap, index); |
| 278 | |
| 279 | err: |
| 280 | kfree(mr); |
| 281 | return err; |
| 282 | } |
| 283 | EXPORT_SYMBOL_GPL(mlx4_mr_alloc); |
| 284 | |
| 285 | void mlx4_mr_free(struct mlx4_dev *dev, struct mlx4_mr *mr) |
| 286 | { |
| 287 | struct mlx4_priv *priv = mlx4_priv(dev); |
| 288 | int err; |
| 289 | |
| 290 | if (mr->enabled) { |
| 291 | err = mlx4_HW2SW_MPT(dev, NULL, |
| 292 | key_to_hw_index(mr->key) & |
| 293 | (dev->caps.num_mpts - 1)); |
| 294 | if (err) |
| 295 | mlx4_warn(dev, "HW2SW_MPT failed (%d)\n", err); |
| 296 | } |
| 297 | |
| 298 | mlx4_mtt_cleanup(dev, &mr->mtt); |
| 299 | mlx4_bitmap_free(&priv->mr_table.mpt_bitmap, key_to_hw_index(mr->key)); |
| 300 | } |
| 301 | EXPORT_SYMBOL_GPL(mlx4_mr_free); |
| 302 | |
| 303 | int mlx4_mr_enable(struct mlx4_dev *dev, struct mlx4_mr *mr) |
| 304 | { |
| 305 | struct mlx4_mr_table *mr_table = &mlx4_priv(dev)->mr_table; |
| 306 | struct mlx4_cmd_mailbox *mailbox; |
| 307 | struct mlx4_mpt_entry *mpt_entry; |
| 308 | int err; |
| 309 | |
| 310 | err = mlx4_table_get(dev, &mr_table->dmpt_table, key_to_hw_index(mr->key)); |
| 311 | if (err) |
| 312 | return err; |
| 313 | |
| 314 | mailbox = mlx4_alloc_cmd_mailbox(dev); |
| 315 | if (IS_ERR(mailbox)) { |
| 316 | err = PTR_ERR(mailbox); |
| 317 | goto err_table; |
| 318 | } |
| 319 | mpt_entry = mailbox->buf; |
| 320 | |
| 321 | memset(mpt_entry, 0, sizeof *mpt_entry); |
| 322 | |
| 323 | mpt_entry->flags = cpu_to_be32(MLX4_MPT_FLAG_SW_OWNS | |
| 324 | MLX4_MPT_FLAG_MIO | |
| 325 | MLX4_MPT_FLAG_REGION | |
| 326 | mr->access); |
| 327 | if (mr->mtt.order < 0) |
| 328 | mpt_entry->flags |= cpu_to_be32(MLX4_MPT_FLAG_PHYSICAL); |
| 329 | |
| 330 | mpt_entry->key = cpu_to_be32(key_to_hw_index(mr->key)); |
| 331 | mpt_entry->pd = cpu_to_be32(mr->pd); |
| 332 | mpt_entry->start = cpu_to_be64(mr->iova); |
| 333 | mpt_entry->length = cpu_to_be64(mr->size); |
| 334 | mpt_entry->entity_size = cpu_to_be32(mr->mtt.page_shift); |
| 335 | mpt_entry->mtt_seg = cpu_to_be64(mlx4_mtt_addr(dev, &mr->mtt)); |
| 336 | |
| 337 | err = mlx4_SW2HW_MPT(dev, mailbox, |
| 338 | key_to_hw_index(mr->key) & (dev->caps.num_mpts - 1)); |
| 339 | if (err) { |
| 340 | mlx4_warn(dev, "SW2HW_MPT failed (%d)\n", err); |
| 341 | goto err_cmd; |
| 342 | } |
| 343 | |
| 344 | mr->enabled = 1; |
| 345 | |
| 346 | mlx4_free_cmd_mailbox(dev, mailbox); |
| 347 | |
| 348 | return 0; |
| 349 | |
| 350 | err_cmd: |
| 351 | mlx4_free_cmd_mailbox(dev, mailbox); |
| 352 | |
| 353 | err_table: |
| 354 | mlx4_table_put(dev, &mr_table->dmpt_table, key_to_hw_index(mr->key)); |
| 355 | return err; |
| 356 | } |
| 357 | EXPORT_SYMBOL_GPL(mlx4_mr_enable); |
| 358 | |
| 359 | static int mlx4_WRITE_MTT(struct mlx4_dev *dev, struct mlx4_cmd_mailbox *mailbox, |
| 360 | int num_mtt) |
| 361 | { |
| 362 | return mlx4_cmd(dev, mailbox->dma, num_mtt, 0, MLX4_CMD_WRITE_MTT, |
| 363 | MLX4_CMD_TIME_CLASS_B); |
| 364 | } |
| 365 | |
| 366 | int mlx4_write_mtt(struct mlx4_dev *dev, struct mlx4_mtt *mtt, |
| 367 | int start_index, int npages, u64 *page_list) |
| 368 | { |
| 369 | struct mlx4_cmd_mailbox *mailbox; |
| 370 | __be64 *mtt_entry; |
| 371 | int i; |
| 372 | int err = 0; |
| 373 | |
| 374 | if (mtt->order < 0) |
| 375 | return -EINVAL; |
| 376 | |
| 377 | mailbox = mlx4_alloc_cmd_mailbox(dev); |
| 378 | if (IS_ERR(mailbox)) |
| 379 | return PTR_ERR(mailbox); |
| 380 | |
| 381 | mtt_entry = mailbox->buf; |
| 382 | |
| 383 | while (npages > 0) { |
| 384 | mtt_entry[0] = cpu_to_be64(mlx4_mtt_addr(dev, mtt) + start_index * 8); |
| 385 | mtt_entry[1] = 0; |
| 386 | |
| 387 | for (i = 0; i < npages && i < MLX4_MAILBOX_SIZE / 8 - 2; ++i) |
| 388 | mtt_entry[i + 2] = cpu_to_be64(page_list[i] | |
| 389 | MLX4_MTT_FLAG_PRESENT); |
| 390 | |
| 391 | /* |
| 392 | * If we have an odd number of entries to write, add |
| 393 | * one more dummy entry for firmware efficiency. |
| 394 | */ |
| 395 | if (i & 1) |
| 396 | mtt_entry[i + 2] = 0; |
| 397 | |
| 398 | err = mlx4_WRITE_MTT(dev, mailbox, (i + 1) & ~1); |
| 399 | if (err) |
| 400 | goto out; |
| 401 | |
| 402 | npages -= i; |
| 403 | start_index += i; |
| 404 | page_list += i; |
| 405 | } |
| 406 | |
| 407 | out: |
| 408 | mlx4_free_cmd_mailbox(dev, mailbox); |
| 409 | |
| 410 | return err; |
| 411 | } |
| 412 | EXPORT_SYMBOL_GPL(mlx4_write_mtt); |
| 413 | |
| 414 | int mlx4_buf_write_mtt(struct mlx4_dev *dev, struct mlx4_mtt *mtt, |
| 415 | struct mlx4_buf *buf) |
| 416 | { |
| 417 | u64 *page_list; |
| 418 | int err; |
| 419 | int i; |
| 420 | |
| 421 | page_list = kmalloc(buf->npages * sizeof *page_list, GFP_KERNEL); |
| 422 | if (!page_list) |
| 423 | return -ENOMEM; |
| 424 | |
| 425 | for (i = 0; i < buf->npages; ++i) |
| 426 | if (buf->nbufs == 1) |
| 427 | page_list[i] = buf->u.direct.map + (i << buf->page_shift); |
| 428 | else |
| 429 | page_list[i] = buf->u.page_list[i].map; |
| 430 | |
| 431 | err = mlx4_write_mtt(dev, mtt, 0, buf->npages, page_list); |
| 432 | |
| 433 | kfree(page_list); |
| 434 | return err; |
| 435 | } |
| 436 | EXPORT_SYMBOL_GPL(mlx4_buf_write_mtt); |
| 437 | |
| 438 | int __devinit mlx4_init_mr_table(struct mlx4_dev *dev) |
| 439 | { |
| 440 | struct mlx4_mr_table *mr_table = &mlx4_priv(dev)->mr_table; |
| 441 | int err; |
| 442 | |
| 443 | err = mlx4_bitmap_init(&mr_table->mpt_bitmap, dev->caps.num_mpts, |
| 444 | ~0, dev->caps.reserved_mrws); |
| 445 | if (err) |
| 446 | return err; |
| 447 | |
| 448 | err = mlx4_buddy_init(&mr_table->mtt_buddy, |
| 449 | ilog2(dev->caps.num_mtt_segs)); |
| 450 | if (err) |
| 451 | goto err_buddy; |
| 452 | |
| 453 | if (dev->caps.reserved_mtts) { |
| 454 | if (mlx4_alloc_mtt_range(dev, ilog2(dev->caps.reserved_mtts)) == -1) { |
| 455 | mlx4_warn(dev, "MTT table of order %d is too small.\n", |
| 456 | mr_table->mtt_buddy.max_order); |
| 457 | err = -ENOMEM; |
| 458 | goto err_reserve_mtts; |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | return 0; |
| 463 | |
| 464 | err_reserve_mtts: |
| 465 | mlx4_buddy_cleanup(&mr_table->mtt_buddy); |
| 466 | |
| 467 | err_buddy: |
| 468 | mlx4_bitmap_cleanup(&mr_table->mpt_bitmap); |
| 469 | |
| 470 | return err; |
| 471 | } |
| 472 | |
| 473 | void mlx4_cleanup_mr_table(struct mlx4_dev *dev) |
| 474 | { |
| 475 | struct mlx4_mr_table *mr_table = &mlx4_priv(dev)->mr_table; |
| 476 | |
| 477 | mlx4_buddy_cleanup(&mr_table->mtt_buddy); |
| 478 | mlx4_bitmap_cleanup(&mr_table->mpt_bitmap); |
| 479 | } |