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