1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <linux/mempool.h>
23#include <linux/slab.h>
24#include <linux/pci.h>
25#include <linux/interrupt.h>
26
27#include <scsi/scsi_device.h>
28#include <scsi/scsi_transport_fc.h>
29
30#include <scsi/scsi.h>
31
32#include "lpfc_hw4.h"
33#include "lpfc_hw.h"
34#include "lpfc_sli.h"
35#include "lpfc_sli4.h"
36#include "lpfc_nl.h"
37#include "lpfc_disc.h"
38#include "lpfc_scsi.h"
39#include "lpfc.h"
40#include "lpfc_crtn.h"
41
42#define LPFC_MBUF_POOL_SIZE 64
43#define LPFC_MEM_POOL_SIZE 64
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61int
62lpfc_mem_alloc(struct lpfc_hba *phba, int align)
63{
64 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
65 int i;
66
67 if (phba->sli_rev == LPFC_SLI_REV4) {
68
69 if (phba->cfg_sg_dma_buf_size < SLI4_PAGE_SIZE)
70 i = phba->cfg_sg_dma_buf_size;
71 else
72 i = SLI4_PAGE_SIZE;
73
74 phba->lpfc_scsi_dma_buf_pool =
75 pci_pool_create("lpfc_scsi_dma_buf_pool",
76 phba->pcidev,
77 phba->cfg_sg_dma_buf_size,
78 i,
79 0);
80 } else {
81 phba->lpfc_scsi_dma_buf_pool =
82 pci_pool_create("lpfc_scsi_dma_buf_pool",
83 phba->pcidev, phba->cfg_sg_dma_buf_size,
84 align, 0);
85 }
86
87 if (!phba->lpfc_scsi_dma_buf_pool)
88 goto fail;
89
90 phba->lpfc_mbuf_pool = pci_pool_create("lpfc_mbuf_pool", phba->pcidev,
91 LPFC_BPL_SIZE,
92 align, 0);
93 if (!phba->lpfc_mbuf_pool)
94 goto fail_free_dma_buf_pool;
95
96 pool->elements = kmalloc(sizeof(struct lpfc_dmabuf) *
97 LPFC_MBUF_POOL_SIZE, GFP_KERNEL);
98 if (!pool->elements)
99 goto fail_free_lpfc_mbuf_pool;
100
101 pool->max_count = 0;
102 pool->current_count = 0;
103 for ( i = 0; i < LPFC_MBUF_POOL_SIZE; i++) {
104 pool->elements[i].virt = pci_pool_alloc(phba->lpfc_mbuf_pool,
105 GFP_KERNEL, &pool->elements[i].phys);
106 if (!pool->elements[i].virt)
107 goto fail_free_mbuf_pool;
108 pool->max_count++;
109 pool->current_count++;
110 }
111
112 phba->mbox_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
113 sizeof(LPFC_MBOXQ_t));
114 if (!phba->mbox_mem_pool)
115 goto fail_free_mbuf_pool;
116
117 phba->nlp_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
118 sizeof(struct lpfc_nodelist));
119 if (!phba->nlp_mem_pool)
120 goto fail_free_mbox_pool;
121
122 if (phba->sli_rev == LPFC_SLI_REV4) {
123 phba->rrq_pool =
124 mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
125 sizeof(struct lpfc_node_rrq));
126 if (!phba->rrq_pool)
127 goto fail_free_nlp_mem_pool;
128 phba->lpfc_hrb_pool = pci_pool_create("lpfc_hrb_pool",
129 phba->pcidev,
130 LPFC_HDR_BUF_SIZE, align, 0);
131 if (!phba->lpfc_hrb_pool)
132 goto fail_free_rrq_mem_pool;
133
134 phba->lpfc_drb_pool = pci_pool_create("lpfc_drb_pool",
135 phba->pcidev,
136 LPFC_DATA_BUF_SIZE, align, 0);
137 if (!phba->lpfc_drb_pool)
138 goto fail_free_hrb_pool;
139 phba->lpfc_hbq_pool = NULL;
140 } else {
141 phba->lpfc_hbq_pool = pci_pool_create("lpfc_hbq_pool",
142 phba->pcidev, LPFC_BPL_SIZE, align, 0);
143 if (!phba->lpfc_hbq_pool)
144 goto fail_free_nlp_mem_pool;
145 phba->lpfc_hrb_pool = NULL;
146 phba->lpfc_drb_pool = NULL;
147 }
148
149 return 0;
150 fail_free_hrb_pool:
151 pci_pool_destroy(phba->lpfc_hrb_pool);
152 phba->lpfc_hrb_pool = NULL;
153 fail_free_rrq_mem_pool:
154 mempool_destroy(phba->rrq_pool);
155 phba->rrq_pool = NULL;
156 fail_free_nlp_mem_pool:
157 mempool_destroy(phba->nlp_mem_pool);
158 phba->nlp_mem_pool = NULL;
159 fail_free_mbox_pool:
160 mempool_destroy(phba->mbox_mem_pool);
161 phba->mbox_mem_pool = NULL;
162 fail_free_mbuf_pool:
163 while (i--)
164 pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
165 pool->elements[i].phys);
166 kfree(pool->elements);
167 fail_free_lpfc_mbuf_pool:
168 pci_pool_destroy(phba->lpfc_mbuf_pool);
169 phba->lpfc_mbuf_pool = NULL;
170 fail_free_dma_buf_pool:
171 pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool);
172 phba->lpfc_scsi_dma_buf_pool = NULL;
173 fail:
174 return -ENOMEM;
175}
176
177
178
179
180
181
182
183
184
185
186void
187lpfc_mem_free(struct lpfc_hba *phba)
188{
189 int i;
190 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
191
192
193 lpfc_sli_hbqbuf_free_all(phba);
194 if (phba->lpfc_drb_pool)
195 pci_pool_destroy(phba->lpfc_drb_pool);
196 phba->lpfc_drb_pool = NULL;
197 if (phba->lpfc_hrb_pool)
198 pci_pool_destroy(phba->lpfc_hrb_pool);
199 phba->lpfc_hrb_pool = NULL;
200
201 if (phba->lpfc_hbq_pool)
202 pci_pool_destroy(phba->lpfc_hbq_pool);
203 phba->lpfc_hbq_pool = NULL;
204
205 if (phba->rrq_pool)
206 mempool_destroy(phba->rrq_pool);
207 phba->rrq_pool = NULL;
208
209
210 mempool_destroy(phba->nlp_mem_pool);
211 phba->nlp_mem_pool = NULL;
212
213
214 mempool_destroy(phba->mbox_mem_pool);
215 phba->mbox_mem_pool = NULL;
216
217
218 for (i = 0; i < pool->current_count; i++)
219 pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
220 pool->elements[i].phys);
221 kfree(pool->elements);
222
223 pci_pool_destroy(phba->lpfc_mbuf_pool);
224 phba->lpfc_mbuf_pool = NULL;
225
226
227 pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool);
228 phba->lpfc_scsi_dma_buf_pool = NULL;
229
230 return;
231}
232
233
234
235
236
237
238
239
240
241
242
243
244void
245lpfc_mem_free_all(struct lpfc_hba *phba)
246{
247 struct lpfc_sli *psli = &phba->sli;
248 LPFC_MBOXQ_t *mbox, *next_mbox;
249 struct lpfc_dmabuf *mp;
250
251
252 list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq, list) {
253 mp = (struct lpfc_dmabuf *) (mbox->context1);
254 if (mp) {
255 lpfc_mbuf_free(phba, mp->virt, mp->phys);
256 kfree(mp);
257 }
258 list_del(&mbox->list);
259 mempool_free(mbox, phba->mbox_mem_pool);
260 }
261
262 list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq_cmpl, list) {
263 mp = (struct lpfc_dmabuf *) (mbox->context1);
264 if (mp) {
265 lpfc_mbuf_free(phba, mp->virt, mp->phys);
266 kfree(mp);
267 }
268 list_del(&mbox->list);
269 mempool_free(mbox, phba->mbox_mem_pool);
270 }
271
272 spin_lock_irq(&phba->hbalock);
273 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
274 spin_unlock_irq(&phba->hbalock);
275 if (psli->mbox_active) {
276 mbox = psli->mbox_active;
277 mp = (struct lpfc_dmabuf *) (mbox->context1);
278 if (mp) {
279 lpfc_mbuf_free(phba, mp->virt, mp->phys);
280 kfree(mp);
281 }
282 mempool_free(mbox, phba->mbox_mem_pool);
283 psli->mbox_active = NULL;
284 }
285
286
287 lpfc_mem_free(phba);
288
289
290 kfree(psli->iocbq_lookup);
291 psli->iocbq_lookup = NULL;
292
293 return;
294}
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314void *
315lpfc_mbuf_alloc(struct lpfc_hba *phba, int mem_flags, dma_addr_t *handle)
316{
317 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
318 unsigned long iflags;
319 void *ret;
320
321 ret = pci_pool_alloc(phba->lpfc_mbuf_pool, GFP_KERNEL, handle);
322
323 spin_lock_irqsave(&phba->hbalock, iflags);
324 if (!ret && (mem_flags & MEM_PRI) && pool->current_count) {
325 pool->current_count--;
326 ret = pool->elements[pool->current_count].virt;
327 *handle = pool->elements[pool->current_count].phys;
328 }
329 spin_unlock_irqrestore(&phba->hbalock, iflags);
330 return ret;
331}
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347void
348__lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
349{
350 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
351
352 if (pool->current_count < pool->max_count) {
353 pool->elements[pool->current_count].virt = virt;
354 pool->elements[pool->current_count].phys = dma;
355 pool->current_count++;
356 } else {
357 pci_pool_free(phba->lpfc_mbuf_pool, virt, dma);
358 }
359 return;
360}
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375void
376lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
377{
378 unsigned long iflags;
379
380 spin_lock_irqsave(&phba->hbalock, iflags);
381 __lpfc_mbuf_free(phba, virt, dma);
382 spin_unlock_irqrestore(&phba->hbalock, iflags);
383 return;
384}
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399struct hbq_dmabuf *
400lpfc_els_hbq_alloc(struct lpfc_hba *phba)
401{
402 struct hbq_dmabuf *hbqbp;
403
404 hbqbp = kzalloc(sizeof(struct hbq_dmabuf), GFP_KERNEL);
405 if (!hbqbp)
406 return NULL;
407
408 hbqbp->dbuf.virt = pci_pool_alloc(phba->lpfc_hbq_pool, GFP_KERNEL,
409 &hbqbp->dbuf.phys);
410 if (!hbqbp->dbuf.virt) {
411 kfree(hbqbp);
412 return NULL;
413 }
414 hbqbp->size = LPFC_BPL_SIZE;
415 return hbqbp;
416}
417
418
419
420
421
422
423
424
425
426
427
428
429
430void
431lpfc_els_hbq_free(struct lpfc_hba *phba, struct hbq_dmabuf *hbqbp)
432{
433 pci_pool_free(phba->lpfc_hbq_pool, hbqbp->dbuf.virt, hbqbp->dbuf.phys);
434 kfree(hbqbp);
435 return;
436}
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451struct hbq_dmabuf *
452lpfc_sli4_rb_alloc(struct lpfc_hba *phba)
453{
454 struct hbq_dmabuf *dma_buf;
455
456 dma_buf = kzalloc(sizeof(struct hbq_dmabuf), GFP_KERNEL);
457 if (!dma_buf)
458 return NULL;
459
460 dma_buf->hbuf.virt = pci_pool_alloc(phba->lpfc_hrb_pool, GFP_KERNEL,
461 &dma_buf->hbuf.phys);
462 if (!dma_buf->hbuf.virt) {
463 kfree(dma_buf);
464 return NULL;
465 }
466 dma_buf->dbuf.virt = pci_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
467 &dma_buf->dbuf.phys);
468 if (!dma_buf->dbuf.virt) {
469 pci_pool_free(phba->lpfc_hrb_pool, dma_buf->hbuf.virt,
470 dma_buf->hbuf.phys);
471 kfree(dma_buf);
472 return NULL;
473 }
474 dma_buf->size = LPFC_BPL_SIZE;
475 return dma_buf;
476}
477
478
479
480
481
482
483
484
485
486
487
488
489
490void
491lpfc_sli4_rb_free(struct lpfc_hba *phba, struct hbq_dmabuf *dmab)
492{
493 pci_pool_free(phba->lpfc_hrb_pool, dmab->hbuf.virt, dmab->hbuf.phys);
494 pci_pool_free(phba->lpfc_drb_pool, dmab->dbuf.virt, dmab->dbuf.phys);
495 kfree(dmab);
496 return;
497}
498
499
500
501
502
503
504
505
506
507
508
509
510
511void
512lpfc_in_buf_free(struct lpfc_hba *phba, struct lpfc_dmabuf *mp)
513{
514 struct hbq_dmabuf *hbq_entry;
515 unsigned long flags;
516
517 if (!mp)
518 return;
519
520 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
521
522 spin_lock_irqsave(&phba->hbalock, flags);
523 if (!phba->hbq_in_use) {
524 spin_unlock_irqrestore(&phba->hbalock, flags);
525 return;
526 }
527 hbq_entry = container_of(mp, struct hbq_dmabuf, dbuf);
528 list_del(&hbq_entry->dbuf.list);
529 if (hbq_entry->tag == -1) {
530 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
531 (phba, hbq_entry);
532 } else {
533 lpfc_sli_free_hbq(phba, hbq_entry);
534 }
535 spin_unlock_irqrestore(&phba->hbalock, flags);
536 } else {
537 lpfc_mbuf_free(phba, mp->virt, mp->phys);
538 kfree(mp);
539 }
540 return;
541}
542