1 /* 2 * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved. 3 * 4 * This software is available to you under a choice of one of two 5 * licenses. You may choose to be licensed under the terms of the GNU 6 * General Public License (GPL) Version 2, available from the file 7 * COPYING in the main directory of this source tree, or the 8 * OpenIB.org BSD license below: 9 * 10 * Redistribution and use in source and binary forms, with or 11 * without modification, are permitted provided that the following 12 * conditions are met: 13 * 14 * - Redistributions of source code must retain the above 15 * copyright notice, this list of conditions and the following 16 * disclaimer. 17 * 18 * - Redistributions in binary form must reproduce the above 19 * copyright notice, this list of conditions and the following 20 * disclaimer in the documentation and/or other materials 21 * provided with the distribution. 22 * 23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 30 * SOFTWARE. 31 * 32 * $Id$ 33 */ 34 35 #include <linux/mm.h> 36 37 #include "mthca_memfree.h" 38 #include "mthca_dev.h" 39 #include "mthca_cmd.h" 40 41 /* 42 * We allocate in as big chunks as we can, up to a maximum of 256 KB 43 * per chunk. 44 */ 45 enum { 46 MTHCA_ICM_ALLOC_SIZE = 1 << 18, 47 MTHCA_TABLE_CHUNK_SIZE = 1 << 18 48 }; 49 50 void mthca_free_icm(struct mthca_dev *dev, struct mthca_icm *icm) 51 { 52 struct mthca_icm_chunk *chunk, *tmp; 53 int i; 54 55 if (!icm) 56 return; 57 58 list_for_each_entry_safe(chunk, tmp, &icm->chunk_list, list) { 59 if (chunk->nsg > 0) 60 pci_unmap_sg(dev->pdev, chunk->mem, chunk->npages, 61 PCI_DMA_BIDIRECTIONAL); 62 63 for (i = 0; i < chunk->npages; ++i) 64 __free_pages(chunk->mem[i].page, 65 get_order(chunk->mem[i].length)); 66 67 kfree(chunk); 68 } 69 70 kfree(icm); 71 } 72 73 struct mthca_icm *mthca_alloc_icm(struct mthca_dev *dev, int npages, 74 unsigned int gfp_mask) 75 { 76 struct mthca_icm *icm; 77 struct mthca_icm_chunk *chunk = NULL; 78 int cur_order; 79 80 icm = kmalloc(sizeof *icm, gfp_mask & ~(__GFP_HIGHMEM | __GFP_NOWARN)); 81 if (!icm) 82 return icm; 83 84 icm->refcount = 0; 85 INIT_LIST_HEAD(&icm->chunk_list); 86 87 cur_order = get_order(MTHCA_ICM_ALLOC_SIZE); 88 89 while (npages > 0) { 90 if (!chunk) { 91 chunk = kmalloc(sizeof *chunk, 92 gfp_mask & ~(__GFP_HIGHMEM | __GFP_NOWARN)); 93 if (!chunk) 94 goto fail; 95 96 chunk->npages = 0; 97 chunk->nsg = 0; 98 list_add_tail(&chunk->list, &icm->chunk_list); 99 } 100 101 while (1 << cur_order > npages) 102 --cur_order; 103 104 chunk->mem[chunk->npages].page = alloc_pages(gfp_mask, cur_order); 105 if (chunk->mem[chunk->npages].page) { 106 chunk->mem[chunk->npages].length = PAGE_SIZE << cur_order; 107 chunk->mem[chunk->npages].offset = 0; 108 109 if (++chunk->npages == MTHCA_ICM_CHUNK_LEN) { 110 chunk->nsg = pci_map_sg(dev->pdev, chunk->mem, 111 chunk->npages, 112 PCI_DMA_BIDIRECTIONAL); 113 114 if (chunk->nsg <= 0) 115 goto fail; 116 117 chunk = NULL; 118 } 119 120 npages -= 1 << cur_order; 121 } else { 122 --cur_order; 123 if (cur_order < 0) 124 goto fail; 125 } 126 } 127 128 if (chunk) { 129 chunk->nsg = pci_map_sg(dev->pdev, chunk->mem, 130 chunk->npages, 131 PCI_DMA_BIDIRECTIONAL); 132 133 if (chunk->nsg <= 0) 134 goto fail; 135 } 136 137 return icm; 138 139 fail: 140 mthca_free_icm(dev, icm); 141 return NULL; 142 } 143 144 int mthca_table_get(struct mthca_dev *dev, struct mthca_icm_table *table, int obj) 145 { 146 int i = (obj & (table->num_obj - 1)) * table->obj_size / MTHCA_TABLE_CHUNK_SIZE; 147 int ret = 0; 148 u8 status; 149 150 down(&table->mutex); 151 152 if (table->icm[i]) { 153 ++table->icm[i]->refcount; 154 goto out; 155 } 156 157 table->icm[i] = mthca_alloc_icm(dev, MTHCA_TABLE_CHUNK_SIZE >> PAGE_SHIFT, 158 (table->lowmem ? GFP_KERNEL : GFP_HIGHUSER) | 159 __GFP_NOWARN); 160 if (!table->icm[i]) { 161 ret = -ENOMEM; 162 goto out; 163 } 164 165 if (mthca_MAP_ICM(dev, table->icm[i], table->virt + i * MTHCA_TABLE_CHUNK_SIZE, 166 &status) || status) { 167 mthca_free_icm(dev, table->icm[i]); 168 table->icm[i] = NULL; 169 ret = -ENOMEM; 170 goto out; 171 } 172 173 ++table->icm[i]->refcount; 174 175 out: 176 up(&table->mutex); 177 return ret; 178 } 179 180 void mthca_table_put(struct mthca_dev *dev, struct mthca_icm_table *table, int obj) 181 { 182 int i; 183 u8 status; 184 185 if (!mthca_is_memfree(dev)) 186 return; 187 188 i = (obj & (table->num_obj - 1)) * table->obj_size / MTHCA_TABLE_CHUNK_SIZE; 189 190 down(&table->mutex); 191 192 if (--table->icm[i]->refcount == 0) { 193 mthca_UNMAP_ICM(dev, table->virt + i * MTHCA_TABLE_CHUNK_SIZE, 194 MTHCA_TABLE_CHUNK_SIZE >> 12, &status); 195 mthca_free_icm(dev, table->icm[i]); 196 table->icm[i] = NULL; 197 } 198 199 up(&table->mutex); 200 } 201 202 void *mthca_table_find(struct mthca_icm_table *table, int obj) 203 { 204 int idx, offset, i; 205 struct mthca_icm_chunk *chunk; 206 struct mthca_icm *icm; 207 struct page *page = NULL; 208 209 if (!table->lowmem) 210 return NULL; 211 212 down(&table->mutex); 213 214 idx = (obj & (table->num_obj - 1)) * table->obj_size; 215 icm = table->icm[idx / MTHCA_TABLE_CHUNK_SIZE]; 216 offset = idx % MTHCA_TABLE_CHUNK_SIZE; 217 218 if (!icm) 219 goto out; 220 221 list_for_each_entry(chunk, &icm->chunk_list, list) { 222 for (i = 0; i < chunk->npages; ++i) { 223 if (chunk->mem[i].length >= offset) { 224 page = chunk->mem[i].page; 225 break; 226 } 227 offset -= chunk->mem[i].length; 228 } 229 } 230 231 out: 232 up(&table->mutex); 233 return page ? lowmem_page_address(page) + offset : NULL; 234 } 235 236 int mthca_table_get_range(struct mthca_dev *dev, struct mthca_icm_table *table, 237 int start, int end) 238 { 239 int inc = MTHCA_TABLE_CHUNK_SIZE / table->obj_size; 240 int i, err; 241 242 for (i = start; i <= end; i += inc) { 243 err = mthca_table_get(dev, table, i); 244 if (err) 245 goto fail; 246 } 247 248 return 0; 249 250 fail: 251 while (i > start) { 252 i -= inc; 253 mthca_table_put(dev, table, i); 254 } 255 256 return err; 257 } 258 259 void mthca_table_put_range(struct mthca_dev *dev, struct mthca_icm_table *table, 260 int start, int end) 261 { 262 int i; 263 264 if (!mthca_is_memfree(dev)) 265 return; 266 267 for (i = start; i <= end; i += MTHCA_TABLE_CHUNK_SIZE / table->obj_size) 268 mthca_table_put(dev, table, i); 269 } 270 271 struct mthca_icm_table *mthca_alloc_icm_table(struct mthca_dev *dev, 272 u64 virt, int obj_size, 273 int nobj, int reserved, 274 int use_lowmem) 275 { 276 struct mthca_icm_table *table; 277 int num_icm; 278 int i; 279 u8 status; 280 281 num_icm = obj_size * nobj / MTHCA_TABLE_CHUNK_SIZE; 282 283 table = kmalloc(sizeof *table + num_icm * sizeof *table->icm, GFP_KERNEL); 284 if (!table) 285 return NULL; 286 287 table->virt = virt; 288 table->num_icm = num_icm; 289 table->num_obj = nobj; 290 table->obj_size = obj_size; 291 table->lowmem = use_lowmem; 292 init_MUTEX(&table->mutex); 293 294 for (i = 0; i < num_icm; ++i) 295 table->icm[i] = NULL; 296 297 for (i = 0; i * MTHCA_TABLE_CHUNK_SIZE < reserved * obj_size; ++i) { 298 table->icm[i] = mthca_alloc_icm(dev, MTHCA_TABLE_CHUNK_SIZE >> PAGE_SHIFT, 299 (use_lowmem ? GFP_KERNEL : GFP_HIGHUSER) | 300 __GFP_NOWARN); 301 if (!table->icm[i]) 302 goto err; 303 if (mthca_MAP_ICM(dev, table->icm[i], virt + i * MTHCA_TABLE_CHUNK_SIZE, 304 &status) || status) { 305 mthca_free_icm(dev, table->icm[i]); 306 table->icm[i] = NULL; 307 goto err; 308 } 309 310 /* 311 * Add a reference to this ICM chunk so that it never 312 * gets freed (since it contains reserved firmware objects). 313 */ 314 ++table->icm[i]->refcount; 315 } 316 317 return table; 318 319 err: 320 for (i = 0; i < num_icm; ++i) 321 if (table->icm[i]) { 322 mthca_UNMAP_ICM(dev, virt + i * MTHCA_TABLE_CHUNK_SIZE, 323 MTHCA_TABLE_CHUNK_SIZE >> 12, &status); 324 mthca_free_icm(dev, table->icm[i]); 325 } 326 327 kfree(table); 328 329 return NULL; 330 } 331 332 void mthca_free_icm_table(struct mthca_dev *dev, struct mthca_icm_table *table) 333 { 334 int i; 335 u8 status; 336 337 for (i = 0; i < table->num_icm; ++i) 338 if (table->icm[i]) { 339 mthca_UNMAP_ICM(dev, table->virt + i * MTHCA_TABLE_CHUNK_SIZE, 340 MTHCA_TABLE_CHUNK_SIZE >> 12, &status); 341 mthca_free_icm(dev, table->icm[i]); 342 } 343 344 kfree(table); 345 } 346 347 static u64 mthca_uarc_virt(struct mthca_dev *dev, int page) 348 { 349 return dev->uar_table.uarc_base + 350 dev->driver_uar.index * dev->uar_table.uarc_size + 351 page * 4096; 352 } 353 354 int mthca_alloc_db(struct mthca_dev *dev, int type, u32 qn, u32 **db) 355 { 356 int group; 357 int start, end, dir; 358 int i, j; 359 struct mthca_db_page *page; 360 int ret = 0; 361 u8 status; 362 363 down(&dev->db_tab->mutex); 364 365 switch (type) { 366 case MTHCA_DB_TYPE_CQ_ARM: 367 case MTHCA_DB_TYPE_SQ: 368 group = 0; 369 start = 0; 370 end = dev->db_tab->max_group1; 371 dir = 1; 372 break; 373 374 case MTHCA_DB_TYPE_CQ_SET_CI: 375 case MTHCA_DB_TYPE_RQ: 376 case MTHCA_DB_TYPE_SRQ: 377 group = 1; 378 start = dev->db_tab->npages - 1; 379 end = dev->db_tab->min_group2; 380 dir = -1; 381 break; 382 383 default: 384 ret = -EINVAL; 385 goto out; 386 } 387 388 for (i = start; i != end; i += dir) 389 if (dev->db_tab->page[i].db_rec && 390 !bitmap_full(dev->db_tab->page[i].used, 391 MTHCA_DB_REC_PER_PAGE)) { 392 page = dev->db_tab->page + i; 393 goto found; 394 } 395 396 if (dev->db_tab->max_group1 >= dev->db_tab->min_group2 - 1) { 397 ret = -ENOMEM; 398 goto out; 399 } 400 401 page = dev->db_tab->page + end; 402 page->db_rec = dma_alloc_coherent(&dev->pdev->dev, 4096, 403 &page->mapping, GFP_KERNEL); 404 if (!page->db_rec) { 405 ret = -ENOMEM; 406 goto out; 407 } 408 memset(page->db_rec, 0, 4096); 409 410 ret = mthca_MAP_ICM_page(dev, page->mapping, mthca_uarc_virt(dev, i), &status); 411 if (!ret && status) 412 ret = -EINVAL; 413 if (ret) { 414 dma_free_coherent(&dev->pdev->dev, 4096, 415 page->db_rec, page->mapping); 416 goto out; 417 } 418 419 bitmap_zero(page->used, MTHCA_DB_REC_PER_PAGE); 420 if (group == 0) 421 ++dev->db_tab->max_group1; 422 else 423 --dev->db_tab->min_group2; 424 425 found: 426 j = find_first_zero_bit(page->used, MTHCA_DB_REC_PER_PAGE); 427 set_bit(j, page->used); 428 429 if (group == 1) 430 j = MTHCA_DB_REC_PER_PAGE - 1 - j; 431 432 ret = i * MTHCA_DB_REC_PER_PAGE + j; 433 434 page->db_rec[j] = cpu_to_be64((qn << 8) | (type << 5)); 435 436 *db = (u32 *) &page->db_rec[j]; 437 438 out: 439 up(&dev->db_tab->mutex); 440 441 return ret; 442 } 443 444 void mthca_free_db(struct mthca_dev *dev, int type, int db_index) 445 { 446 int i, j; 447 struct mthca_db_page *page; 448 u8 status; 449 450 i = db_index / MTHCA_DB_REC_PER_PAGE; 451 j = db_index % MTHCA_DB_REC_PER_PAGE; 452 453 page = dev->db_tab->page + i; 454 455 down(&dev->db_tab->mutex); 456 457 page->db_rec[j] = 0; 458 if (i >= dev->db_tab->min_group2) 459 j = MTHCA_DB_REC_PER_PAGE - 1 - j; 460 clear_bit(j, page->used); 461 462 if (bitmap_empty(page->used, MTHCA_DB_REC_PER_PAGE) && 463 i >= dev->db_tab->max_group1 - 1) { 464 mthca_UNMAP_ICM(dev, mthca_uarc_virt(dev, i), 1, &status); 465 466 dma_free_coherent(&dev->pdev->dev, 4096, 467 page->db_rec, page->mapping); 468 page->db_rec = NULL; 469 470 if (i == dev->db_tab->max_group1) { 471 --dev->db_tab->max_group1; 472 /* XXX may be able to unmap more pages now */ 473 } 474 if (i == dev->db_tab->min_group2) 475 ++dev->db_tab->min_group2; 476 } 477 478 up(&dev->db_tab->mutex); 479 } 480 481 int mthca_init_db_tab(struct mthca_dev *dev) 482 { 483 int i; 484 485 if (!mthca_is_memfree(dev)) 486 return 0; 487 488 dev->db_tab = kmalloc(sizeof *dev->db_tab, GFP_KERNEL); 489 if (!dev->db_tab) 490 return -ENOMEM; 491 492 init_MUTEX(&dev->db_tab->mutex); 493 494 dev->db_tab->npages = dev->uar_table.uarc_size / 4096; 495 dev->db_tab->max_group1 = 0; 496 dev->db_tab->min_group2 = dev->db_tab->npages - 1; 497 498 dev->db_tab->page = kmalloc(dev->db_tab->npages * 499 sizeof *dev->db_tab->page, 500 GFP_KERNEL); 501 if (!dev->db_tab->page) { 502 kfree(dev->db_tab); 503 return -ENOMEM; 504 } 505 506 for (i = 0; i < dev->db_tab->npages; ++i) 507 dev->db_tab->page[i].db_rec = NULL; 508 509 return 0; 510 } 511 512 void mthca_cleanup_db_tab(struct mthca_dev *dev) 513 { 514 int i; 515 u8 status; 516 517 if (!mthca_is_memfree(dev)) 518 return; 519 520 /* 521 * Because we don't always free our UARC pages when they 522 * become empty to make mthca_free_db() simpler we need to 523 * make a sweep through the doorbell pages and free any 524 * leftover pages now. 525 */ 526 for (i = 0; i < dev->db_tab->npages; ++i) { 527 if (!dev->db_tab->page[i].db_rec) 528 continue; 529 530 if (!bitmap_empty(dev->db_tab->page[i].used, MTHCA_DB_REC_PER_PAGE)) 531 mthca_warn(dev, "Kernel UARC page %d not empty\n", i); 532 533 mthca_UNMAP_ICM(dev, mthca_uarc_virt(dev, i), 1, &status); 534 535 dma_free_coherent(&dev->pdev->dev, 4096, 536 dev->db_tab->page[i].db_rec, 537 dev->db_tab->page[i].mapping); 538 } 539 540 kfree(dev->db_tab->page); 541 kfree(dev->db_tab); 542 } 543