Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved. |
Roland Dreier | 56483ec | 2005-07-07 17:57:16 -0700 | [diff] [blame] | 3 | * Copyright (c) 2005 Cisco Systems. All rights reserved. |
Roland Dreier | 2a1d9b7 | 2005-08-10 23:03:10 -0700 | [diff] [blame] | 4 | * Copyright (c) 2005 Mellanox Technologies. All rights reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * $Id$ |
| 35 | */ |
| 36 | |
Roland Dreier | dbcf31b | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 37 | #include <linux/mm.h> |
Michael S. Tsirkin | 391e4de | 2007-02-10 23:15:08 +0200 | [diff] [blame] | 38 | #include <linux/scatterlist.h> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 39 | #include <linux/sched.h> |
Michael S. Tsirkin | 391e4de | 2007-02-10 23:15:08 +0200 | [diff] [blame] | 40 | |
| 41 | #include <asm/page.h> |
Roland Dreier | dbcf31b | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 42 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include "mthca_memfree.h" |
| 44 | #include "mthca_dev.h" |
| 45 | #include "mthca_cmd.h" |
| 46 | |
| 47 | /* |
| 48 | * We allocate in as big chunks as we can, up to a maximum of 256 KB |
| 49 | * per chunk. |
| 50 | */ |
| 51 | enum { |
| 52 | MTHCA_ICM_ALLOC_SIZE = 1 << 18, |
| 53 | MTHCA_TABLE_CHUNK_SIZE = 1 << 18 |
| 54 | }; |
| 55 | |
Roland Dreier | 56483ec | 2005-07-07 17:57:16 -0700 | [diff] [blame] | 56 | struct mthca_user_db_table { |
Roland Dreier | fd9cfdd | 2006-01-30 16:45:11 -0800 | [diff] [blame] | 57 | struct mutex mutex; |
Roland Dreier | 56483ec | 2005-07-07 17:57:16 -0700 | [diff] [blame] | 58 | struct { |
| 59 | u64 uvirt; |
| 60 | struct scatterlist mem; |
| 61 | int refcount; |
| 62 | } page[0]; |
| 63 | }; |
| 64 | |
Michael S. Tsirkin | 391e4de | 2007-02-10 23:15:08 +0200 | [diff] [blame] | 65 | static void mthca_free_icm_pages(struct mthca_dev *dev, struct mthca_icm_chunk *chunk) |
| 66 | { |
| 67 | int i; |
| 68 | |
| 69 | if (chunk->nsg > 0) |
| 70 | pci_unmap_sg(dev->pdev, chunk->mem, chunk->npages, |
| 71 | PCI_DMA_BIDIRECTIONAL); |
| 72 | |
| 73 | for (i = 0; i < chunk->npages; ++i) |
Jens Axboe | 45711f1 | 2007-10-22 21:19:53 +0200 | [diff] [blame] | 74 | __free_pages(sg_page(&chunk->mem[i]), |
Michael S. Tsirkin | 391e4de | 2007-02-10 23:15:08 +0200 | [diff] [blame] | 75 | get_order(chunk->mem[i].length)); |
| 76 | } |
| 77 | |
| 78 | static void mthca_free_icm_coherent(struct mthca_dev *dev, struct mthca_icm_chunk *chunk) |
| 79 | { |
| 80 | int i; |
| 81 | |
| 82 | for (i = 0; i < chunk->npages; ++i) { |
| 83 | dma_free_coherent(&dev->pdev->dev, chunk->mem[i].length, |
Jens Axboe | 45711f1 | 2007-10-22 21:19:53 +0200 | [diff] [blame] | 84 | lowmem_page_address(sg_page(&chunk->mem[i])), |
Michael S. Tsirkin | 391e4de | 2007-02-10 23:15:08 +0200 | [diff] [blame] | 85 | sg_dma_address(&chunk->mem[i])); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | void mthca_free_icm(struct mthca_dev *dev, struct mthca_icm *icm, int coherent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | { |
| 91 | struct mthca_icm_chunk *chunk, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | |
| 93 | if (!icm) |
| 94 | return; |
| 95 | |
| 96 | list_for_each_entry_safe(chunk, tmp, &icm->chunk_list, list) { |
Michael S. Tsirkin | 391e4de | 2007-02-10 23:15:08 +0200 | [diff] [blame] | 97 | if (coherent) |
| 98 | mthca_free_icm_coherent(dev, chunk); |
| 99 | else |
| 100 | mthca_free_icm_pages(dev, chunk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
| 102 | kfree(chunk); |
| 103 | } |
| 104 | |
| 105 | kfree(icm); |
| 106 | } |
| 107 | |
Michael S. Tsirkin | 391e4de | 2007-02-10 23:15:08 +0200 | [diff] [blame] | 108 | static int mthca_alloc_icm_pages(struct scatterlist *mem, int order, gfp_t gfp_mask) |
| 109 | { |
Jens Axboe | 45711f1 | 2007-10-22 21:19:53 +0200 | [diff] [blame] | 110 | struct page *page; |
| 111 | |
Eli Cohen | 87afd44 | 2008-06-23 09:29:58 -0700 | [diff] [blame^] | 112 | /* |
| 113 | * Use __GFP_ZERO because buggy firmware assumes ICM pages are |
| 114 | * cleared, and subtle failures are seen if they aren't. |
| 115 | */ |
| 116 | page = alloc_pages(gfp_mask | __GFP_ZERO, order); |
Jens Axboe | 45711f1 | 2007-10-22 21:19:53 +0200 | [diff] [blame] | 117 | if (!page) |
Michael S. Tsirkin | 391e4de | 2007-02-10 23:15:08 +0200 | [diff] [blame] | 118 | return -ENOMEM; |
| 119 | |
Jens Axboe | 642f14903 | 2007-10-24 11:20:47 +0200 | [diff] [blame] | 120 | sg_set_page(mem, page, PAGE_SIZE << order, 0); |
Michael S. Tsirkin | 391e4de | 2007-02-10 23:15:08 +0200 | [diff] [blame] | 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | static int mthca_alloc_icm_coherent(struct device *dev, struct scatterlist *mem, |
| 125 | int order, gfp_t gfp_mask) |
| 126 | { |
| 127 | void *buf = dma_alloc_coherent(dev, PAGE_SIZE << order, &sg_dma_address(mem), |
| 128 | gfp_mask); |
| 129 | if (!buf) |
| 130 | return -ENOMEM; |
| 131 | |
| 132 | sg_set_buf(mem, buf, PAGE_SIZE << order); |
| 133 | BUG_ON(mem->offset); |
| 134 | sg_dma_len(mem) = PAGE_SIZE << order; |
| 135 | return 0; |
| 136 | } |
| 137 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | struct mthca_icm *mthca_alloc_icm(struct mthca_dev *dev, int npages, |
Michael S. Tsirkin | 391e4de | 2007-02-10 23:15:08 +0200 | [diff] [blame] | 139 | gfp_t gfp_mask, int coherent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | { |
| 141 | struct mthca_icm *icm; |
| 142 | struct mthca_icm_chunk *chunk = NULL; |
| 143 | int cur_order; |
Michael S. Tsirkin | 391e4de | 2007-02-10 23:15:08 +0200 | [diff] [blame] | 144 | int ret; |
| 145 | |
| 146 | /* We use sg_set_buf for coherent allocs, which assumes low memory */ |
| 147 | BUG_ON(coherent && (gfp_mask & __GFP_HIGHMEM)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | |
| 149 | icm = kmalloc(sizeof *icm, gfp_mask & ~(__GFP_HIGHMEM | __GFP_NOWARN)); |
| 150 | if (!icm) |
| 151 | return icm; |
| 152 | |
| 153 | icm->refcount = 0; |
| 154 | INIT_LIST_HEAD(&icm->chunk_list); |
| 155 | |
| 156 | cur_order = get_order(MTHCA_ICM_ALLOC_SIZE); |
| 157 | |
| 158 | while (npages > 0) { |
| 159 | if (!chunk) { |
| 160 | chunk = kmalloc(sizeof *chunk, |
| 161 | gfp_mask & ~(__GFP_HIGHMEM | __GFP_NOWARN)); |
| 162 | if (!chunk) |
| 163 | goto fail; |
| 164 | |
Jens Axboe | 45711f1 | 2007-10-22 21:19:53 +0200 | [diff] [blame] | 165 | sg_init_table(chunk->mem, MTHCA_ICM_CHUNK_LEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | chunk->npages = 0; |
| 167 | chunk->nsg = 0; |
| 168 | list_add_tail(&chunk->list, &icm->chunk_list); |
| 169 | } |
| 170 | |
| 171 | while (1 << cur_order > npages) |
| 172 | --cur_order; |
| 173 | |
Michael S. Tsirkin | 391e4de | 2007-02-10 23:15:08 +0200 | [diff] [blame] | 174 | if (coherent) |
| 175 | ret = mthca_alloc_icm_coherent(&dev->pdev->dev, |
| 176 | &chunk->mem[chunk->npages], |
| 177 | cur_order, gfp_mask); |
| 178 | else |
| 179 | ret = mthca_alloc_icm_pages(&chunk->mem[chunk->npages], |
| 180 | cur_order, gfp_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | |
Michael S. Tsirkin | 391e4de | 2007-02-10 23:15:08 +0200 | [diff] [blame] | 182 | if (!ret) { |
| 183 | ++chunk->npages; |
| 184 | |
Roland Dreier | 11282b3 | 2007-02-16 13:57:33 -0800 | [diff] [blame] | 185 | if (coherent) |
| 186 | ++chunk->nsg; |
| 187 | else if (chunk->npages == MTHCA_ICM_CHUNK_LEN) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | chunk->nsg = pci_map_sg(dev->pdev, chunk->mem, |
| 189 | chunk->npages, |
| 190 | PCI_DMA_BIDIRECTIONAL); |
| 191 | |
| 192 | if (chunk->nsg <= 0) |
| 193 | goto fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Michael S. Tsirkin | 391e4de | 2007-02-10 23:15:08 +0200 | [diff] [blame] | 196 | if (chunk->npages == MTHCA_ICM_CHUNK_LEN) |
| 197 | chunk = NULL; |
| 198 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | npages -= 1 << cur_order; |
| 200 | } else { |
| 201 | --cur_order; |
| 202 | if (cur_order < 0) |
| 203 | goto fail; |
| 204 | } |
| 205 | } |
| 206 | |
Michael S. Tsirkin | 391e4de | 2007-02-10 23:15:08 +0200 | [diff] [blame] | 207 | if (!coherent && chunk) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | chunk->nsg = pci_map_sg(dev->pdev, chunk->mem, |
| 209 | chunk->npages, |
| 210 | PCI_DMA_BIDIRECTIONAL); |
| 211 | |
| 212 | if (chunk->nsg <= 0) |
| 213 | goto fail; |
| 214 | } |
| 215 | |
| 216 | return icm; |
| 217 | |
| 218 | fail: |
Michael S. Tsirkin | 391e4de | 2007-02-10 23:15:08 +0200 | [diff] [blame] | 219 | mthca_free_icm(dev, icm, coherent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | return NULL; |
| 221 | } |
| 222 | |
| 223 | int mthca_table_get(struct mthca_dev *dev, struct mthca_icm_table *table, int obj) |
| 224 | { |
| 225 | int i = (obj & (table->num_obj - 1)) * table->obj_size / MTHCA_TABLE_CHUNK_SIZE; |
| 226 | int ret = 0; |
| 227 | u8 status; |
| 228 | |
Roland Dreier | fd9cfdd | 2006-01-30 16:45:11 -0800 | [diff] [blame] | 229 | mutex_lock(&table->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | |
| 231 | if (table->icm[i]) { |
| 232 | ++table->icm[i]->refcount; |
| 233 | goto out; |
| 234 | } |
| 235 | |
| 236 | table->icm[i] = mthca_alloc_icm(dev, MTHCA_TABLE_CHUNK_SIZE >> PAGE_SHIFT, |
| 237 | (table->lowmem ? GFP_KERNEL : GFP_HIGHUSER) | |
Michael S. Tsirkin | 391e4de | 2007-02-10 23:15:08 +0200 | [diff] [blame] | 238 | __GFP_NOWARN, table->coherent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | if (!table->icm[i]) { |
| 240 | ret = -ENOMEM; |
| 241 | goto out; |
| 242 | } |
| 243 | |
| 244 | if (mthca_MAP_ICM(dev, table->icm[i], table->virt + i * MTHCA_TABLE_CHUNK_SIZE, |
| 245 | &status) || status) { |
Michael S. Tsirkin | 391e4de | 2007-02-10 23:15:08 +0200 | [diff] [blame] | 246 | mthca_free_icm(dev, table->icm[i], table->coherent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | table->icm[i] = NULL; |
| 248 | ret = -ENOMEM; |
| 249 | goto out; |
| 250 | } |
| 251 | |
| 252 | ++table->icm[i]->refcount; |
| 253 | |
| 254 | out: |
Roland Dreier | fd9cfdd | 2006-01-30 16:45:11 -0800 | [diff] [blame] | 255 | mutex_unlock(&table->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | return ret; |
| 257 | } |
| 258 | |
| 259 | void mthca_table_put(struct mthca_dev *dev, struct mthca_icm_table *table, int obj) |
| 260 | { |
Roland Dreier | a03a5a6 | 2005-06-27 14:36:43 -0700 | [diff] [blame] | 261 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | u8 status; |
| 263 | |
Roland Dreier | a03a5a6 | 2005-06-27 14:36:43 -0700 | [diff] [blame] | 264 | if (!mthca_is_memfree(dev)) |
| 265 | return; |
| 266 | |
| 267 | i = (obj & (table->num_obj - 1)) * table->obj_size / MTHCA_TABLE_CHUNK_SIZE; |
| 268 | |
Roland Dreier | fd9cfdd | 2006-01-30 16:45:11 -0800 | [diff] [blame] | 269 | mutex_lock(&table->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | |
| 271 | if (--table->icm[i]->refcount == 0) { |
| 272 | mthca_UNMAP_ICM(dev, table->virt + i * MTHCA_TABLE_CHUNK_SIZE, |
Ishai Rabinovitz | 8d3ef29 | 2006-03-01 22:33:11 -0800 | [diff] [blame] | 273 | MTHCA_TABLE_CHUNK_SIZE / MTHCA_ICM_PAGE_SIZE, |
| 274 | &status); |
Michael S. Tsirkin | 391e4de | 2007-02-10 23:15:08 +0200 | [diff] [blame] | 275 | mthca_free_icm(dev, table->icm[i], table->coherent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | table->icm[i] = NULL; |
| 277 | } |
| 278 | |
Roland Dreier | fd9cfdd | 2006-01-30 16:45:11 -0800 | [diff] [blame] | 279 | mutex_unlock(&table->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Michael S. Tsirkin | 391e4de | 2007-02-10 23:15:08 +0200 | [diff] [blame] | 282 | void *mthca_table_find(struct mthca_icm_table *table, int obj, dma_addr_t *dma_handle) |
Michael S. Tsirkin | 0fabd9f | 2005-04-16 15:26:29 -0700 | [diff] [blame] | 283 | { |
Michael S. Tsirkin | 391e4de | 2007-02-10 23:15:08 +0200 | [diff] [blame] | 284 | int idx, offset, dma_offset, i; |
Michael S. Tsirkin | 0fabd9f | 2005-04-16 15:26:29 -0700 | [diff] [blame] | 285 | struct mthca_icm_chunk *chunk; |
| 286 | struct mthca_icm *icm; |
| 287 | struct page *page = NULL; |
| 288 | |
| 289 | if (!table->lowmem) |
| 290 | return NULL; |
| 291 | |
Roland Dreier | fd9cfdd | 2006-01-30 16:45:11 -0800 | [diff] [blame] | 292 | mutex_lock(&table->mutex); |
Michael S. Tsirkin | 0fabd9f | 2005-04-16 15:26:29 -0700 | [diff] [blame] | 293 | |
| 294 | idx = (obj & (table->num_obj - 1)) * table->obj_size; |
| 295 | icm = table->icm[idx / MTHCA_TABLE_CHUNK_SIZE]; |
Michael S. Tsirkin | 391e4de | 2007-02-10 23:15:08 +0200 | [diff] [blame] | 296 | dma_offset = offset = idx % MTHCA_TABLE_CHUNK_SIZE; |
Michael S. Tsirkin | 0fabd9f | 2005-04-16 15:26:29 -0700 | [diff] [blame] | 297 | |
| 298 | if (!icm) |
| 299 | goto out; |
| 300 | |
| 301 | list_for_each_entry(chunk, &icm->chunk_list, list) { |
| 302 | for (i = 0; i < chunk->npages; ++i) { |
Michael S. Tsirkin | 391e4de | 2007-02-10 23:15:08 +0200 | [diff] [blame] | 303 | if (dma_handle && dma_offset >= 0) { |
| 304 | if (sg_dma_len(&chunk->mem[i]) > dma_offset) |
| 305 | *dma_handle = sg_dma_address(&chunk->mem[i]) + |
| 306 | dma_offset; |
| 307 | dma_offset -= sg_dma_len(&chunk->mem[i]); |
| 308 | } |
| 309 | /* DMA mapping can merge pages but not split them, |
| 310 | * so if we found the page, dma_handle has already |
| 311 | * been assigned to. */ |
Michael S. Tsirkin | 46707e9 | 2007-01-03 14:46:30 +0200 | [diff] [blame] | 312 | if (chunk->mem[i].length > offset) { |
Jens Axboe | 45711f1 | 2007-10-22 21:19:53 +0200 | [diff] [blame] | 313 | page = sg_page(&chunk->mem[i]); |
Michael S. Tsirkin | 6c7d2a7 | 2005-12-15 13:55:50 -0800 | [diff] [blame] | 314 | goto out; |
Michael S. Tsirkin | 0fabd9f | 2005-04-16 15:26:29 -0700 | [diff] [blame] | 315 | } |
| 316 | offset -= chunk->mem[i].length; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | out: |
Roland Dreier | fd9cfdd | 2006-01-30 16:45:11 -0800 | [diff] [blame] | 321 | mutex_unlock(&table->mutex); |
Michael S. Tsirkin | 0fabd9f | 2005-04-16 15:26:29 -0700 | [diff] [blame] | 322 | return page ? lowmem_page_address(page) + offset : NULL; |
| 323 | } |
| 324 | |
Roland Dreier | 86562a1 | 2005-04-16 15:26:13 -0700 | [diff] [blame] | 325 | int mthca_table_get_range(struct mthca_dev *dev, struct mthca_icm_table *table, |
| 326 | int start, int end) |
| 327 | { |
| 328 | int inc = MTHCA_TABLE_CHUNK_SIZE / table->obj_size; |
| 329 | int i, err; |
| 330 | |
| 331 | for (i = start; i <= end; i += inc) { |
| 332 | err = mthca_table_get(dev, table, i); |
| 333 | if (err) |
| 334 | goto fail; |
| 335 | } |
| 336 | |
| 337 | return 0; |
| 338 | |
| 339 | fail: |
| 340 | while (i > start) { |
| 341 | i -= inc; |
| 342 | mthca_table_put(dev, table, i); |
| 343 | } |
| 344 | |
| 345 | return err; |
| 346 | } |
| 347 | |
| 348 | void mthca_table_put_range(struct mthca_dev *dev, struct mthca_icm_table *table, |
| 349 | int start, int end) |
| 350 | { |
| 351 | int i; |
| 352 | |
Roland Dreier | a03a5a6 | 2005-06-27 14:36:43 -0700 | [diff] [blame] | 353 | if (!mthca_is_memfree(dev)) |
| 354 | return; |
| 355 | |
Roland Dreier | 86562a1 | 2005-04-16 15:26:13 -0700 | [diff] [blame] | 356 | for (i = start; i <= end; i += MTHCA_TABLE_CHUNK_SIZE / table->obj_size) |
| 357 | mthca_table_put(dev, table, i); |
| 358 | } |
| 359 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | struct mthca_icm_table *mthca_alloc_icm_table(struct mthca_dev *dev, |
| 361 | u64 virt, int obj_size, |
| 362 | int nobj, int reserved, |
Michael S. Tsirkin | 391e4de | 2007-02-10 23:15:08 +0200 | [diff] [blame] | 363 | int use_lowmem, int use_coherent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | { |
| 365 | struct mthca_icm_table *table; |
Roland Dreier | c263ff6 | 2008-04-16 21:01:13 -0700 | [diff] [blame] | 366 | int obj_per_chunk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | int num_icm; |
Roland Dreier | d20a401 | 2005-08-19 10:36:11 -0700 | [diff] [blame] | 368 | unsigned chunk_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | int i; |
| 370 | u8 status; |
| 371 | |
Roland Dreier | c263ff6 | 2008-04-16 21:01:13 -0700 | [diff] [blame] | 372 | obj_per_chunk = MTHCA_TABLE_CHUNK_SIZE / obj_size; |
| 373 | num_icm = DIV_ROUND_UP(nobj, obj_per_chunk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | |
| 375 | table = kmalloc(sizeof *table + num_icm * sizeof *table->icm, GFP_KERNEL); |
| 376 | if (!table) |
| 377 | return NULL; |
| 378 | |
| 379 | table->virt = virt; |
| 380 | table->num_icm = num_icm; |
| 381 | table->num_obj = nobj; |
| 382 | table->obj_size = obj_size; |
| 383 | table->lowmem = use_lowmem; |
Michael S. Tsirkin | 391e4de | 2007-02-10 23:15:08 +0200 | [diff] [blame] | 384 | table->coherent = use_coherent; |
Roland Dreier | fd9cfdd | 2006-01-30 16:45:11 -0800 | [diff] [blame] | 385 | mutex_init(&table->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | |
| 387 | for (i = 0; i < num_icm; ++i) |
| 388 | table->icm[i] = NULL; |
| 389 | |
| 390 | for (i = 0; i * MTHCA_TABLE_CHUNK_SIZE < reserved * obj_size; ++i) { |
Roland Dreier | d20a401 | 2005-08-19 10:36:11 -0700 | [diff] [blame] | 391 | chunk_size = MTHCA_TABLE_CHUNK_SIZE; |
| 392 | if ((i + 1) * MTHCA_TABLE_CHUNK_SIZE > nobj * obj_size) |
| 393 | chunk_size = nobj * obj_size - i * MTHCA_TABLE_CHUNK_SIZE; |
| 394 | |
| 395 | table->icm[i] = mthca_alloc_icm(dev, chunk_size >> PAGE_SHIFT, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | (use_lowmem ? GFP_KERNEL : GFP_HIGHUSER) | |
Michael S. Tsirkin | 391e4de | 2007-02-10 23:15:08 +0200 | [diff] [blame] | 397 | __GFP_NOWARN, use_coherent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | if (!table->icm[i]) |
| 399 | goto err; |
| 400 | if (mthca_MAP_ICM(dev, table->icm[i], virt + i * MTHCA_TABLE_CHUNK_SIZE, |
| 401 | &status) || status) { |
Michael S. Tsirkin | 391e4de | 2007-02-10 23:15:08 +0200 | [diff] [blame] | 402 | mthca_free_icm(dev, table->icm[i], table->coherent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | table->icm[i] = NULL; |
| 404 | goto err; |
| 405 | } |
| 406 | |
| 407 | /* |
| 408 | * Add a reference to this ICM chunk so that it never |
| 409 | * gets freed (since it contains reserved firmware objects). |
| 410 | */ |
| 411 | ++table->icm[i]->refcount; |
| 412 | } |
| 413 | |
| 414 | return table; |
| 415 | |
| 416 | err: |
| 417 | for (i = 0; i < num_icm; ++i) |
| 418 | if (table->icm[i]) { |
| 419 | mthca_UNMAP_ICM(dev, virt + i * MTHCA_TABLE_CHUNK_SIZE, |
Ishai Rabinovitz | 8d3ef29 | 2006-03-01 22:33:11 -0800 | [diff] [blame] | 420 | MTHCA_TABLE_CHUNK_SIZE / MTHCA_ICM_PAGE_SIZE, |
Roland Dreier | b399939 | 2008-04-16 21:01:03 -0700 | [diff] [blame] | 421 | &status); |
Michael S. Tsirkin | 391e4de | 2007-02-10 23:15:08 +0200 | [diff] [blame] | 422 | mthca_free_icm(dev, table->icm[i], table->coherent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | kfree(table); |
| 426 | |
| 427 | return NULL; |
| 428 | } |
| 429 | |
| 430 | void mthca_free_icm_table(struct mthca_dev *dev, struct mthca_icm_table *table) |
| 431 | { |
| 432 | int i; |
| 433 | u8 status; |
| 434 | |
| 435 | for (i = 0; i < table->num_icm; ++i) |
| 436 | if (table->icm[i]) { |
| 437 | mthca_UNMAP_ICM(dev, table->virt + i * MTHCA_TABLE_CHUNK_SIZE, |
Ishai Rabinovitz | 8d3ef29 | 2006-03-01 22:33:11 -0800 | [diff] [blame] | 438 | MTHCA_TABLE_CHUNK_SIZE / MTHCA_ICM_PAGE_SIZE, |
| 439 | &status); |
Michael S. Tsirkin | 391e4de | 2007-02-10 23:15:08 +0200 | [diff] [blame] | 440 | mthca_free_icm(dev, table->icm[i], table->coherent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | kfree(table); |
| 444 | } |
| 445 | |
Roland Dreier | 56483ec | 2005-07-07 17:57:16 -0700 | [diff] [blame] | 446 | static u64 mthca_uarc_virt(struct mthca_dev *dev, struct mthca_uar *uar, int page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | { |
| 448 | return dev->uar_table.uarc_base + |
Roland Dreier | 56483ec | 2005-07-07 17:57:16 -0700 | [diff] [blame] | 449 | uar->index * dev->uar_table.uarc_size + |
Ishai Rabinovitz | 8d3ef29 | 2006-03-01 22:33:11 -0800 | [diff] [blame] | 450 | page * MTHCA_ICM_PAGE_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | } |
| 452 | |
Roland Dreier | 56483ec | 2005-07-07 17:57:16 -0700 | [diff] [blame] | 453 | int mthca_map_user_db(struct mthca_dev *dev, struct mthca_uar *uar, |
| 454 | struct mthca_user_db_table *db_tab, int index, u64 uaddr) |
| 455 | { |
Jens Axboe | 45711f1 | 2007-10-22 21:19:53 +0200 | [diff] [blame] | 456 | struct page *pages[1]; |
Roland Dreier | 56483ec | 2005-07-07 17:57:16 -0700 | [diff] [blame] | 457 | int ret = 0; |
| 458 | u8 status; |
| 459 | int i; |
| 460 | |
| 461 | if (!mthca_is_memfree(dev)) |
| 462 | return 0; |
| 463 | |
| 464 | if (index < 0 || index > dev->uar_table.uarc_size / 8) |
| 465 | return -EINVAL; |
| 466 | |
Roland Dreier | fd9cfdd | 2006-01-30 16:45:11 -0800 | [diff] [blame] | 467 | mutex_lock(&db_tab->mutex); |
Roland Dreier | 56483ec | 2005-07-07 17:57:16 -0700 | [diff] [blame] | 468 | |
| 469 | i = index / MTHCA_DB_REC_PER_PAGE; |
| 470 | |
| 471 | if ((db_tab->page[i].refcount >= MTHCA_DB_REC_PER_PAGE) || |
| 472 | (db_tab->page[i].uvirt && db_tab->page[i].uvirt != uaddr) || |
| 473 | (uaddr & 4095)) { |
| 474 | ret = -EINVAL; |
| 475 | goto out; |
| 476 | } |
| 477 | |
| 478 | if (db_tab->page[i].refcount) { |
| 479 | ++db_tab->page[i].refcount; |
| 480 | goto out; |
| 481 | } |
| 482 | |
| 483 | ret = get_user_pages(current, current->mm, uaddr & PAGE_MASK, 1, 1, 0, |
Jens Axboe | 45711f1 | 2007-10-22 21:19:53 +0200 | [diff] [blame] | 484 | pages, NULL); |
Roland Dreier | 56483ec | 2005-07-07 17:57:16 -0700 | [diff] [blame] | 485 | if (ret < 0) |
| 486 | goto out; |
| 487 | |
Jens Axboe | 642f14903 | 2007-10-24 11:20:47 +0200 | [diff] [blame] | 488 | sg_set_page(&db_tab->page[i].mem, pages[0], MTHCA_ICM_PAGE_SIZE, |
| 489 | uaddr & ~PAGE_MASK); |
Roland Dreier | 56483ec | 2005-07-07 17:57:16 -0700 | [diff] [blame] | 490 | |
| 491 | ret = pci_map_sg(dev->pdev, &db_tab->page[i].mem, 1, PCI_DMA_TODEVICE); |
| 492 | if (ret < 0) { |
Jens Axboe | 45711f1 | 2007-10-22 21:19:53 +0200 | [diff] [blame] | 493 | put_page(pages[0]); |
Roland Dreier | 56483ec | 2005-07-07 17:57:16 -0700 | [diff] [blame] | 494 | goto out; |
| 495 | } |
| 496 | |
| 497 | ret = mthca_MAP_ICM_page(dev, sg_dma_address(&db_tab->page[i].mem), |
| 498 | mthca_uarc_virt(dev, uar, i), &status); |
| 499 | if (!ret && status) |
| 500 | ret = -EINVAL; |
| 501 | if (ret) { |
| 502 | pci_unmap_sg(dev->pdev, &db_tab->page[i].mem, 1, PCI_DMA_TODEVICE); |
Jens Axboe | 45711f1 | 2007-10-22 21:19:53 +0200 | [diff] [blame] | 503 | put_page(sg_page(&db_tab->page[i].mem)); |
Roland Dreier | 56483ec | 2005-07-07 17:57:16 -0700 | [diff] [blame] | 504 | goto out; |
| 505 | } |
| 506 | |
| 507 | db_tab->page[i].uvirt = uaddr; |
| 508 | db_tab->page[i].refcount = 1; |
| 509 | |
| 510 | out: |
Roland Dreier | fd9cfdd | 2006-01-30 16:45:11 -0800 | [diff] [blame] | 511 | mutex_unlock(&db_tab->mutex); |
Roland Dreier | 56483ec | 2005-07-07 17:57:16 -0700 | [diff] [blame] | 512 | return ret; |
| 513 | } |
| 514 | |
| 515 | void mthca_unmap_user_db(struct mthca_dev *dev, struct mthca_uar *uar, |
| 516 | struct mthca_user_db_table *db_tab, int index) |
| 517 | { |
| 518 | if (!mthca_is_memfree(dev)) |
| 519 | return; |
| 520 | |
| 521 | /* |
| 522 | * To make our bookkeeping simpler, we don't unmap DB |
| 523 | * pages until we clean up the whole db table. |
| 524 | */ |
| 525 | |
Roland Dreier | fd9cfdd | 2006-01-30 16:45:11 -0800 | [diff] [blame] | 526 | mutex_lock(&db_tab->mutex); |
Roland Dreier | 56483ec | 2005-07-07 17:57:16 -0700 | [diff] [blame] | 527 | |
| 528 | --db_tab->page[index / MTHCA_DB_REC_PER_PAGE].refcount; |
| 529 | |
Roland Dreier | fd9cfdd | 2006-01-30 16:45:11 -0800 | [diff] [blame] | 530 | mutex_unlock(&db_tab->mutex); |
Roland Dreier | 56483ec | 2005-07-07 17:57:16 -0700 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | struct mthca_user_db_table *mthca_init_user_db_tab(struct mthca_dev *dev) |
| 534 | { |
| 535 | struct mthca_user_db_table *db_tab; |
| 536 | int npages; |
| 537 | int i; |
| 538 | |
| 539 | if (!mthca_is_memfree(dev)) |
| 540 | return NULL; |
| 541 | |
Ishai Rabinovitz | 8d3ef29 | 2006-03-01 22:33:11 -0800 | [diff] [blame] | 542 | npages = dev->uar_table.uarc_size / MTHCA_ICM_PAGE_SIZE; |
Roland Dreier | 56483ec | 2005-07-07 17:57:16 -0700 | [diff] [blame] | 543 | db_tab = kmalloc(sizeof *db_tab + npages * sizeof *db_tab->page, GFP_KERNEL); |
| 544 | if (!db_tab) |
| 545 | return ERR_PTR(-ENOMEM); |
| 546 | |
Roland Dreier | fd9cfdd | 2006-01-30 16:45:11 -0800 | [diff] [blame] | 547 | mutex_init(&db_tab->mutex); |
Roland Dreier | 56483ec | 2005-07-07 17:57:16 -0700 | [diff] [blame] | 548 | for (i = 0; i < npages; ++i) { |
| 549 | db_tab->page[i].refcount = 0; |
| 550 | db_tab->page[i].uvirt = 0; |
Roland Dreier | fe17435 | 2008-02-12 14:38:22 -0800 | [diff] [blame] | 551 | sg_init_table(&db_tab->page[i].mem, 1); |
Roland Dreier | 56483ec | 2005-07-07 17:57:16 -0700 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | return db_tab; |
| 555 | } |
| 556 | |
| 557 | void mthca_cleanup_user_db_tab(struct mthca_dev *dev, struct mthca_uar *uar, |
| 558 | struct mthca_user_db_table *db_tab) |
| 559 | { |
| 560 | int i; |
| 561 | u8 status; |
| 562 | |
| 563 | if (!mthca_is_memfree(dev)) |
| 564 | return; |
| 565 | |
Ishai Rabinovitz | 8d3ef29 | 2006-03-01 22:33:11 -0800 | [diff] [blame] | 566 | for (i = 0; i < dev->uar_table.uarc_size / MTHCA_ICM_PAGE_SIZE; ++i) { |
Roland Dreier | 56483ec | 2005-07-07 17:57:16 -0700 | [diff] [blame] | 567 | if (db_tab->page[i].uvirt) { |
| 568 | mthca_UNMAP_ICM(dev, mthca_uarc_virt(dev, uar, i), 1, &status); |
| 569 | pci_unmap_sg(dev->pdev, &db_tab->page[i].mem, 1, PCI_DMA_TODEVICE); |
Jens Axboe | 45711f1 | 2007-10-22 21:19:53 +0200 | [diff] [blame] | 570 | put_page(sg_page(&db_tab->page[i].mem)); |
Roland Dreier | 56483ec | 2005-07-07 17:57:16 -0700 | [diff] [blame] | 571 | } |
| 572 | } |
Jack Morgenstein | 52d0df1 | 2005-12-09 13:48:50 -0800 | [diff] [blame] | 573 | |
| 574 | kfree(db_tab); |
Roland Dreier | 56483ec | 2005-07-07 17:57:16 -0700 | [diff] [blame] | 575 | } |
| 576 | |
Roland Dreier | c6f5cb7 | 2005-10-18 13:22:16 -0700 | [diff] [blame] | 577 | int mthca_alloc_db(struct mthca_dev *dev, enum mthca_db_type type, |
| 578 | u32 qn, __be32 **db) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | { |
| 580 | int group; |
| 581 | int start, end, dir; |
| 582 | int i, j; |
| 583 | struct mthca_db_page *page; |
| 584 | int ret = 0; |
| 585 | u8 status; |
| 586 | |
Roland Dreier | fd9cfdd | 2006-01-30 16:45:11 -0800 | [diff] [blame] | 587 | mutex_lock(&dev->db_tab->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | |
| 589 | switch (type) { |
| 590 | case MTHCA_DB_TYPE_CQ_ARM: |
| 591 | case MTHCA_DB_TYPE_SQ: |
| 592 | group = 0; |
| 593 | start = 0; |
| 594 | end = dev->db_tab->max_group1; |
| 595 | dir = 1; |
| 596 | break; |
| 597 | |
| 598 | case MTHCA_DB_TYPE_CQ_SET_CI: |
| 599 | case MTHCA_DB_TYPE_RQ: |
| 600 | case MTHCA_DB_TYPE_SRQ: |
| 601 | group = 1; |
| 602 | start = dev->db_tab->npages - 1; |
| 603 | end = dev->db_tab->min_group2; |
| 604 | dir = -1; |
| 605 | break; |
| 606 | |
| 607 | default: |
Roland Dreier | 2714eb5 | 2005-04-16 15:26:20 -0700 | [diff] [blame] | 608 | ret = -EINVAL; |
| 609 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | for (i = start; i != end; i += dir) |
| 613 | if (dev->db_tab->page[i].db_rec && |
| 614 | !bitmap_full(dev->db_tab->page[i].used, |
| 615 | MTHCA_DB_REC_PER_PAGE)) { |
| 616 | page = dev->db_tab->page + i; |
| 617 | goto found; |
| 618 | } |
| 619 | |
Roland Dreier | 018771f | 2005-09-21 21:40:12 -0700 | [diff] [blame] | 620 | for (i = start; i != end; i += dir) |
| 621 | if (!dev->db_tab->page[i].db_rec) { |
| 622 | page = dev->db_tab->page + i; |
| 623 | goto alloc; |
| 624 | } |
| 625 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | if (dev->db_tab->max_group1 >= dev->db_tab->min_group2 - 1) { |
| 627 | ret = -ENOMEM; |
| 628 | goto out; |
| 629 | } |
| 630 | |
Roland Dreier | 018771f | 2005-09-21 21:40:12 -0700 | [diff] [blame] | 631 | if (group == 0) |
| 632 | ++dev->db_tab->max_group1; |
| 633 | else |
| 634 | --dev->db_tab->min_group2; |
| 635 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | page = dev->db_tab->page + end; |
Roland Dreier | 018771f | 2005-09-21 21:40:12 -0700 | [diff] [blame] | 637 | |
| 638 | alloc: |
Ishai Rabinovitz | 8d3ef29 | 2006-03-01 22:33:11 -0800 | [diff] [blame] | 639 | page->db_rec = dma_alloc_coherent(&dev->pdev->dev, MTHCA_ICM_PAGE_SIZE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | &page->mapping, GFP_KERNEL); |
| 641 | if (!page->db_rec) { |
| 642 | ret = -ENOMEM; |
| 643 | goto out; |
| 644 | } |
Ishai Rabinovitz | 8d3ef29 | 2006-03-01 22:33:11 -0800 | [diff] [blame] | 645 | memset(page->db_rec, 0, MTHCA_ICM_PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | |
Roland Dreier | 56483ec | 2005-07-07 17:57:16 -0700 | [diff] [blame] | 647 | ret = mthca_MAP_ICM_page(dev, page->mapping, |
| 648 | mthca_uarc_virt(dev, &dev->driver_uar, i), &status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | if (!ret && status) |
| 650 | ret = -EINVAL; |
| 651 | if (ret) { |
Ishai Rabinovitz | 8d3ef29 | 2006-03-01 22:33:11 -0800 | [diff] [blame] | 652 | dma_free_coherent(&dev->pdev->dev, MTHCA_ICM_PAGE_SIZE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | page->db_rec, page->mapping); |
| 654 | goto out; |
| 655 | } |
| 656 | |
| 657 | bitmap_zero(page->used, MTHCA_DB_REC_PER_PAGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | |
| 659 | found: |
| 660 | j = find_first_zero_bit(page->used, MTHCA_DB_REC_PER_PAGE); |
| 661 | set_bit(j, page->used); |
| 662 | |
| 663 | if (group == 1) |
| 664 | j = MTHCA_DB_REC_PER_PAGE - 1 - j; |
| 665 | |
| 666 | ret = i * MTHCA_DB_REC_PER_PAGE + j; |
| 667 | |
| 668 | page->db_rec[j] = cpu_to_be64((qn << 8) | (type << 5)); |
| 669 | |
Sean Hefty | 97f52eb | 2005-08-13 21:05:57 -0700 | [diff] [blame] | 670 | *db = (__be32 *) &page->db_rec[j]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | |
| 672 | out: |
Roland Dreier | fd9cfdd | 2006-01-30 16:45:11 -0800 | [diff] [blame] | 673 | mutex_unlock(&dev->db_tab->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | |
| 675 | return ret; |
| 676 | } |
| 677 | |
| 678 | void mthca_free_db(struct mthca_dev *dev, int type, int db_index) |
| 679 | { |
| 680 | int i, j; |
| 681 | struct mthca_db_page *page; |
| 682 | u8 status; |
| 683 | |
| 684 | i = db_index / MTHCA_DB_REC_PER_PAGE; |
| 685 | j = db_index % MTHCA_DB_REC_PER_PAGE; |
| 686 | |
| 687 | page = dev->db_tab->page + i; |
| 688 | |
Roland Dreier | fd9cfdd | 2006-01-30 16:45:11 -0800 | [diff] [blame] | 689 | mutex_lock(&dev->db_tab->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | |
| 691 | page->db_rec[j] = 0; |
| 692 | if (i >= dev->db_tab->min_group2) |
| 693 | j = MTHCA_DB_REC_PER_PAGE - 1 - j; |
| 694 | clear_bit(j, page->used); |
| 695 | |
| 696 | if (bitmap_empty(page->used, MTHCA_DB_REC_PER_PAGE) && |
| 697 | i >= dev->db_tab->max_group1 - 1) { |
Roland Dreier | 56483ec | 2005-07-07 17:57:16 -0700 | [diff] [blame] | 698 | mthca_UNMAP_ICM(dev, mthca_uarc_virt(dev, &dev->driver_uar, i), 1, &status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | |
Ishai Rabinovitz | 8d3ef29 | 2006-03-01 22:33:11 -0800 | [diff] [blame] | 700 | dma_free_coherent(&dev->pdev->dev, MTHCA_ICM_PAGE_SIZE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | page->db_rec, page->mapping); |
| 702 | page->db_rec = NULL; |
| 703 | |
| 704 | if (i == dev->db_tab->max_group1) { |
| 705 | --dev->db_tab->max_group1; |
| 706 | /* XXX may be able to unmap more pages now */ |
| 707 | } |
| 708 | if (i == dev->db_tab->min_group2) |
| 709 | ++dev->db_tab->min_group2; |
| 710 | } |
| 711 | |
Roland Dreier | fd9cfdd | 2006-01-30 16:45:11 -0800 | [diff] [blame] | 712 | mutex_unlock(&dev->db_tab->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | int mthca_init_db_tab(struct mthca_dev *dev) |
| 716 | { |
| 717 | int i; |
| 718 | |
Roland Dreier | d10ddbf | 2005-04-16 15:26:32 -0700 | [diff] [blame] | 719 | if (!mthca_is_memfree(dev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | return 0; |
| 721 | |
| 722 | dev->db_tab = kmalloc(sizeof *dev->db_tab, GFP_KERNEL); |
| 723 | if (!dev->db_tab) |
| 724 | return -ENOMEM; |
| 725 | |
Roland Dreier | fd9cfdd | 2006-01-30 16:45:11 -0800 | [diff] [blame] | 726 | mutex_init(&dev->db_tab->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | |
Ishai Rabinovitz | 8d3ef29 | 2006-03-01 22:33:11 -0800 | [diff] [blame] | 728 | dev->db_tab->npages = dev->uar_table.uarc_size / MTHCA_ICM_PAGE_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | dev->db_tab->max_group1 = 0; |
| 730 | dev->db_tab->min_group2 = dev->db_tab->npages - 1; |
| 731 | |
| 732 | dev->db_tab->page = kmalloc(dev->db_tab->npages * |
| 733 | sizeof *dev->db_tab->page, |
| 734 | GFP_KERNEL); |
| 735 | if (!dev->db_tab->page) { |
| 736 | kfree(dev->db_tab); |
| 737 | return -ENOMEM; |
| 738 | } |
| 739 | |
| 740 | for (i = 0; i < dev->db_tab->npages; ++i) |
| 741 | dev->db_tab->page[i].db_rec = NULL; |
| 742 | |
| 743 | return 0; |
| 744 | } |
| 745 | |
| 746 | void mthca_cleanup_db_tab(struct mthca_dev *dev) |
| 747 | { |
| 748 | int i; |
| 749 | u8 status; |
| 750 | |
Roland Dreier | d10ddbf | 2005-04-16 15:26:32 -0700 | [diff] [blame] | 751 | if (!mthca_is_memfree(dev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | return; |
| 753 | |
| 754 | /* |
| 755 | * Because we don't always free our UARC pages when they |
| 756 | * become empty to make mthca_free_db() simpler we need to |
| 757 | * make a sweep through the doorbell pages and free any |
| 758 | * leftover pages now. |
| 759 | */ |
| 760 | for (i = 0; i < dev->db_tab->npages; ++i) { |
| 761 | if (!dev->db_tab->page[i].db_rec) |
| 762 | continue; |
| 763 | |
| 764 | if (!bitmap_empty(dev->db_tab->page[i].used, MTHCA_DB_REC_PER_PAGE)) |
| 765 | mthca_warn(dev, "Kernel UARC page %d not empty\n", i); |
| 766 | |
Roland Dreier | 56483ec | 2005-07-07 17:57:16 -0700 | [diff] [blame] | 767 | mthca_UNMAP_ICM(dev, mthca_uarc_virt(dev, &dev->driver_uar, i), 1, &status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | |
Ishai Rabinovitz | 8d3ef29 | 2006-03-01 22:33:11 -0800 | [diff] [blame] | 769 | dma_free_coherent(&dev->pdev->dev, MTHCA_ICM_PAGE_SIZE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | dev->db_tab->page[i].db_rec, |
| 771 | dev->db_tab->page[i].mapping); |
| 772 | } |
| 773 | |
| 774 | kfree(dev->db_tab->page); |
| 775 | kfree(dev->db_tab); |
| 776 | } |