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