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