1 /*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
3 * Fibre Channel Host Bus Adapters. *
4 * Copyright (C) 2004-2009 Emulex. All rights reserved. *
5 * EMULEX and SLI are trademarks of Emulex. *
6 * www.emulex.com *
7 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
8 * *
9 * This program is free software; you can redistribute it and/or *
10 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
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 /* max elements in MBUF safety pool */
43 #define LPFC_MEM_POOL_SIZE 64 /* max elem in non-DMA safety pool */
44
45
46 /**
47 * lpfc_mem_alloc - create and allocate all PCI and memory pools
48 * @phba: HBA to allocate pools for
49 *
50 * Description: Creates and allocates PCI pools lpfc_scsi_dma_buf_pool,
51 * lpfc_mbuf_pool, lpfc_hrb_pool. Creates and allocates kmalloc-backed mempools
52 * for LPFC_MBOXQ_t and lpfc_nodelist. Also allocates the VPI bitmask.
53 *
54 * Notes: Not interrupt-safe. Must be called with no locks held. If any
55 * allocation fails, frees all successfully allocated memory before returning.
56 *
57 * Returns:
58 * 0 on success
59 * -ENOMEM on failure (if any memory allocations fail)
60 **/
61 int
lpfc_mem_alloc(struct lpfc_hba * phba,int align)62 lpfc_mem_alloc(struct lpfc_hba *phba, int align)
63 {
64 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
65 int longs;
66 int i;
67
68 if (phba->sli_rev == LPFC_SLI_REV4)
69 phba->lpfc_scsi_dma_buf_pool =
70 pci_pool_create("lpfc_scsi_dma_buf_pool",
71 phba->pcidev,
72 phba->cfg_sg_dma_buf_size,
73 phba->cfg_sg_dma_buf_size,
74 0);
75 else
76 phba->lpfc_scsi_dma_buf_pool =
77 pci_pool_create("lpfc_scsi_dma_buf_pool",
78 phba->pcidev, phba->cfg_sg_dma_buf_size,
79 align, 0);
80 if (!phba->lpfc_scsi_dma_buf_pool)
81 goto fail;
82
83 phba->lpfc_mbuf_pool = pci_pool_create("lpfc_mbuf_pool", phba->pcidev,
84 LPFC_BPL_SIZE,
85 align, 0);
86 if (!phba->lpfc_mbuf_pool)
87 goto fail_free_dma_buf_pool;
88
89 pool->elements = kmalloc(sizeof(struct lpfc_dmabuf) *
90 LPFC_MBUF_POOL_SIZE, GFP_KERNEL);
91 if (!pool->elements)
92 goto fail_free_lpfc_mbuf_pool;
93
94 pool->max_count = 0;
95 pool->current_count = 0;
96 for ( i = 0; i < LPFC_MBUF_POOL_SIZE; i++) {
97 pool->elements[i].virt = pci_pool_alloc(phba->lpfc_mbuf_pool,
98 GFP_KERNEL, &pool->elements[i].phys);
99 if (!pool->elements[i].virt)
100 goto fail_free_mbuf_pool;
101 pool->max_count++;
102 pool->current_count++;
103 }
104
105 phba->mbox_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
106 sizeof(LPFC_MBOXQ_t));
107 if (!phba->mbox_mem_pool)
108 goto fail_free_mbuf_pool;
109
110 phba->nlp_mem_pool = mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
111 sizeof(struct lpfc_nodelist));
112 if (!phba->nlp_mem_pool)
113 goto fail_free_mbox_pool;
114
115 if (phba->sli_rev == LPFC_SLI_REV4) {
116 phba->rrq_pool =
117 mempool_create_kmalloc_pool(LPFC_MEM_POOL_SIZE,
118 sizeof(struct lpfc_node_rrq));
119 if (!phba->rrq_pool)
120 goto fail_free_nlp_mem_pool;
121 phba->lpfc_hrb_pool = pci_pool_create("lpfc_hrb_pool",
122 phba->pcidev,
123 LPFC_HDR_BUF_SIZE, align, 0);
124 if (!phba->lpfc_hrb_pool)
125 goto fail_free_rrq_mem_pool;
126
127 phba->lpfc_drb_pool = pci_pool_create("lpfc_drb_pool",
128 phba->pcidev,
129 LPFC_DATA_BUF_SIZE, align, 0);
130 if (!phba->lpfc_drb_pool)
131 goto fail_free_hrb_pool;
132 phba->lpfc_hbq_pool = NULL;
133 } else {
134 phba->lpfc_hbq_pool = pci_pool_create("lpfc_hbq_pool",
135 phba->pcidev, LPFC_BPL_SIZE, align, 0);
136 if (!phba->lpfc_hbq_pool)
137 goto fail_free_nlp_mem_pool;
138 phba->lpfc_hrb_pool = NULL;
139 phba->lpfc_drb_pool = NULL;
140 }
141 /* vpi zero is reserved for the physical port so add 1 to max */
142 longs = ((phba->max_vpi + 1) + BITS_PER_LONG - 1) / BITS_PER_LONG;
143 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long), GFP_KERNEL);
144 if (!phba->vpi_bmask)
145 goto fail_free_dbq_pool;
146
147 return 0;
148
149 fail_free_dbq_pool:
150 pci_pool_destroy(phba->lpfc_drb_pool);
151 phba->lpfc_drb_pool = NULL;
152 fail_free_hrb_pool:
153 pci_pool_destroy(phba->lpfc_hrb_pool);
154 phba->lpfc_hrb_pool = NULL;
155 fail_free_rrq_mem_pool:
156 mempool_destroy(phba->rrq_pool);
157 phba->rrq_pool = NULL;
158 fail_free_nlp_mem_pool:
159 mempool_destroy(phba->nlp_mem_pool);
160 phba->nlp_mem_pool = NULL;
161 fail_free_mbox_pool:
162 mempool_destroy(phba->mbox_mem_pool);
163 phba->mbox_mem_pool = NULL;
164 fail_free_mbuf_pool:
165 while (i--)
166 pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
167 pool->elements[i].phys);
168 kfree(pool->elements);
169 fail_free_lpfc_mbuf_pool:
170 pci_pool_destroy(phba->lpfc_mbuf_pool);
171 phba->lpfc_mbuf_pool = NULL;
172 fail_free_dma_buf_pool:
173 pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool);
174 phba->lpfc_scsi_dma_buf_pool = NULL;
175 fail:
176 return -ENOMEM;
177 }
178
179 /**
180 * lpfc_mem_free - Frees memory allocated by lpfc_mem_alloc
181 * @phba: HBA to free memory for
182 *
183 * Description: Free the memory allocated by lpfc_mem_alloc routine. This
184 * routine is a the counterpart of lpfc_mem_alloc.
185 *
186 * Returns: None
187 **/
188 void
lpfc_mem_free(struct lpfc_hba * phba)189 lpfc_mem_free(struct lpfc_hba *phba)
190 {
191 int i;
192 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
193
194 /* Free VPI bitmask memory */
195 kfree(phba->vpi_bmask);
196
197 /* Free HBQ pools */
198 lpfc_sli_hbqbuf_free_all(phba);
199 if (phba->lpfc_drb_pool)
200 pci_pool_destroy(phba->lpfc_drb_pool);
201 phba->lpfc_drb_pool = NULL;
202 if (phba->lpfc_hrb_pool)
203 pci_pool_destroy(phba->lpfc_hrb_pool);
204 phba->lpfc_hrb_pool = NULL;
205
206 if (phba->lpfc_hbq_pool)
207 pci_pool_destroy(phba->lpfc_hbq_pool);
208 phba->lpfc_hbq_pool = NULL;
209
210 /* Free NLP memory pool */
211 mempool_destroy(phba->nlp_mem_pool);
212 phba->nlp_mem_pool = NULL;
213
214 /* Free mbox memory pool */
215 mempool_destroy(phba->mbox_mem_pool);
216 phba->mbox_mem_pool = NULL;
217
218 /* Free MBUF memory pool */
219 for (i = 0; i < pool->current_count; i++)
220 pci_pool_free(phba->lpfc_mbuf_pool, pool->elements[i].virt,
221 pool->elements[i].phys);
222 kfree(pool->elements);
223
224 pci_pool_destroy(phba->lpfc_mbuf_pool);
225 phba->lpfc_mbuf_pool = NULL;
226
227 /* Free DMA buffer memory pool */
228 pci_pool_destroy(phba->lpfc_scsi_dma_buf_pool);
229 phba->lpfc_scsi_dma_buf_pool = NULL;
230
231 return;
232 }
233
234 /**
235 * lpfc_mem_free_all - Frees all PCI and driver memory
236 * @phba: HBA to free memory for
237 *
238 * Description: Free memory from PCI and driver memory pools and also those
239 * used : lpfc_scsi_dma_buf_pool, lpfc_mbuf_pool, lpfc_hrb_pool. Frees
240 * kmalloc-backed mempools for LPFC_MBOXQ_t and lpfc_nodelist. Also frees
241 * the VPI bitmask.
242 *
243 * Returns: None
244 **/
245 void
lpfc_mem_free_all(struct lpfc_hba * phba)246 lpfc_mem_free_all(struct lpfc_hba *phba)
247 {
248 struct lpfc_sli *psli = &phba->sli;
249 LPFC_MBOXQ_t *mbox, *next_mbox;
250 struct lpfc_dmabuf *mp;
251
252 /* Free memory used in mailbox queue back to mailbox memory pool */
253 list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq, list) {
254 mp = (struct lpfc_dmabuf *) (mbox->context1);
255 if (mp) {
256 lpfc_mbuf_free(phba, mp->virt, mp->phys);
257 kfree(mp);
258 }
259 list_del(&mbox->list);
260 mempool_free(mbox, phba->mbox_mem_pool);
261 }
262 /* Free memory used in mailbox cmpl list back to mailbox memory pool */
263 list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq_cmpl, list) {
264 mp = (struct lpfc_dmabuf *) (mbox->context1);
265 if (mp) {
266 lpfc_mbuf_free(phba, mp->virt, mp->phys);
267 kfree(mp);
268 }
269 list_del(&mbox->list);
270 mempool_free(mbox, phba->mbox_mem_pool);
271 }
272 /* Free the active mailbox command back to the mailbox memory pool */
273 spin_lock_irq(&phba->hbalock);
274 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
275 spin_unlock_irq(&phba->hbalock);
276 if (psli->mbox_active) {
277 mbox = psli->mbox_active;
278 mp = (struct lpfc_dmabuf *) (mbox->context1);
279 if (mp) {
280 lpfc_mbuf_free(phba, mp->virt, mp->phys);
281 kfree(mp);
282 }
283 mempool_free(mbox, phba->mbox_mem_pool);
284 psli->mbox_active = NULL;
285 }
286
287 /* Free and destroy all the allocated memory pools */
288 lpfc_mem_free(phba);
289
290 /* Free the iocb lookup array */
291 kfree(psli->iocbq_lookup);
292 psli->iocbq_lookup = NULL;
293
294 return;
295 }
296
297 /**
298 * lpfc_mbuf_alloc - Allocate an mbuf from the lpfc_mbuf_pool PCI pool
299 * @phba: HBA which owns the pool to allocate from
300 * @mem_flags: indicates if this is a priority (MEM_PRI) allocation
301 * @handle: used to return the DMA-mapped address of the mbuf
302 *
303 * Description: Allocates a DMA-mapped buffer from the lpfc_mbuf_pool PCI pool.
304 * Allocates from generic pci_pool_alloc function first and if that fails and
305 * mem_flags has MEM_PRI set (the only defined flag), returns an mbuf from the
306 * HBA's pool.
307 *
308 * Notes: Not interrupt-safe. Must be called with no locks held. Takes
309 * phba->hbalock.
310 *
311 * Returns:
312 * pointer to the allocated mbuf on success
313 * NULL on failure
314 **/
315 void *
lpfc_mbuf_alloc(struct lpfc_hba * phba,int mem_flags,dma_addr_t * handle)316 lpfc_mbuf_alloc(struct lpfc_hba *phba, int mem_flags, dma_addr_t *handle)
317 {
318 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
319 unsigned long iflags;
320 void *ret;
321
322 ret = pci_pool_alloc(phba->lpfc_mbuf_pool, GFP_KERNEL, handle);
323
324 spin_lock_irqsave(&phba->hbalock, iflags);
325 if (!ret && (mem_flags & MEM_PRI) && pool->current_count) {
326 pool->current_count--;
327 ret = pool->elements[pool->current_count].virt;
328 *handle = pool->elements[pool->current_count].phys;
329 }
330 spin_unlock_irqrestore(&phba->hbalock, iflags);
331 return ret;
332 }
333
334 /**
335 * __lpfc_mbuf_free - Free an mbuf from the lpfc_mbuf_pool PCI pool (locked)
336 * @phba: HBA which owns the pool to return to
337 * @virt: mbuf to free
338 * @dma: the DMA-mapped address of the lpfc_mbuf_pool to be freed
339 *
340 * Description: Returns an mbuf lpfc_mbuf_pool to the lpfc_mbuf_safety_pool if
341 * it is below its max_count, frees the mbuf otherwise.
342 *
343 * Notes: Must be called with phba->hbalock held to synchronize access to
344 * lpfc_mbuf_safety_pool.
345 *
346 * Returns: None
347 **/
348 void
__lpfc_mbuf_free(struct lpfc_hba * phba,void * virt,dma_addr_t dma)349 __lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
350 {
351 struct lpfc_dma_pool *pool = &phba->lpfc_mbuf_safety_pool;
352
353 if (pool->current_count < pool->max_count) {
354 pool->elements[pool->current_count].virt = virt;
355 pool->elements[pool->current_count].phys = dma;
356 pool->current_count++;
357 } else {
358 pci_pool_free(phba->lpfc_mbuf_pool, virt, dma);
359 }
360 return;
361 }
362
363 /**
364 * lpfc_mbuf_free - Free an mbuf from the lpfc_mbuf_pool PCI pool (unlocked)
365 * @phba: HBA which owns the pool to return to
366 * @virt: mbuf to free
367 * @dma: the DMA-mapped address of the lpfc_mbuf_pool to be freed
368 *
369 * Description: Returns an mbuf lpfc_mbuf_pool to the lpfc_mbuf_safety_pool if
370 * it is below its max_count, frees the mbuf otherwise.
371 *
372 * Notes: Takes phba->hbalock. Can be called with or without other locks held.
373 *
374 * Returns: None
375 **/
376 void
lpfc_mbuf_free(struct lpfc_hba * phba,void * virt,dma_addr_t dma)377 lpfc_mbuf_free(struct lpfc_hba * phba, void *virt, dma_addr_t dma)
378 {
379 unsigned long iflags;
380
381 spin_lock_irqsave(&phba->hbalock, iflags);
382 __lpfc_mbuf_free(phba, virt, dma);
383 spin_unlock_irqrestore(&phba->hbalock, iflags);
384 return;
385 }
386
387 /**
388 * lpfc_els_hbq_alloc - Allocate an HBQ buffer
389 * @phba: HBA to allocate HBQ buffer for
390 *
391 * Description: Allocates a DMA-mapped HBQ buffer from the lpfc_hrb_pool PCI
392 * pool along a non-DMA-mapped container for it.
393 *
394 * Notes: Not interrupt-safe. Must be called with no locks held.
395 *
396 * Returns:
397 * pointer to HBQ on success
398 * NULL on failure
399 **/
400 struct hbq_dmabuf *
lpfc_els_hbq_alloc(struct lpfc_hba * phba)401 lpfc_els_hbq_alloc(struct lpfc_hba *phba)
402 {
403 struct hbq_dmabuf *hbqbp;
404
405 hbqbp = kmalloc(sizeof(struct hbq_dmabuf), GFP_KERNEL);
406 if (!hbqbp)
407 return NULL;
408
409 hbqbp->dbuf.virt = pci_pool_alloc(phba->lpfc_hbq_pool, GFP_KERNEL,
410 &hbqbp->dbuf.phys);
411 if (!hbqbp->dbuf.virt) {
412 kfree(hbqbp);
413 return NULL;
414 }
415 hbqbp->size = LPFC_BPL_SIZE;
416 return hbqbp;
417 }
418
419 /**
420 * lpfc_els_hbq_free - Frees an HBQ buffer allocated with lpfc_els_hbq_alloc
421 * @phba: HBA buffer was allocated for
422 * @hbqbp: HBQ container returned by lpfc_els_hbq_alloc
423 *
424 * Description: Frees both the container and the DMA-mapped buffer returned by
425 * lpfc_els_hbq_alloc.
426 *
427 * Notes: Can be called with or without locks held.
428 *
429 * Returns: None
430 **/
431 void
lpfc_els_hbq_free(struct lpfc_hba * phba,struct hbq_dmabuf * hbqbp)432 lpfc_els_hbq_free(struct lpfc_hba *phba, struct hbq_dmabuf *hbqbp)
433 {
434 pci_pool_free(phba->lpfc_hbq_pool, hbqbp->dbuf.virt, hbqbp->dbuf.phys);
435 kfree(hbqbp);
436 return;
437 }
438
439 /**
440 * lpfc_sli4_rb_alloc - Allocate an SLI4 Receive buffer
441 * @phba: HBA to allocate a receive buffer for
442 *
443 * Description: Allocates a DMA-mapped receive buffer from the lpfc_hrb_pool PCI
444 * pool along a non-DMA-mapped container for it.
445 *
446 * Notes: Not interrupt-safe. Must be called with no locks held.
447 *
448 * Returns:
449 * pointer to HBQ on success
450 * NULL on failure
451 **/
452 struct hbq_dmabuf *
lpfc_sli4_rb_alloc(struct lpfc_hba * phba)453 lpfc_sli4_rb_alloc(struct lpfc_hba *phba)
454 {
455 struct hbq_dmabuf *dma_buf;
456
457 dma_buf = kmalloc(sizeof(struct hbq_dmabuf), GFP_KERNEL);
458 if (!dma_buf)
459 return NULL;
460
461 dma_buf->hbuf.virt = pci_pool_alloc(phba->lpfc_hrb_pool, GFP_KERNEL,
462 &dma_buf->hbuf.phys);
463 if (!dma_buf->hbuf.virt) {
464 kfree(dma_buf);
465 return NULL;
466 }
467 dma_buf->dbuf.virt = pci_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
468 &dma_buf->dbuf.phys);
469 if (!dma_buf->dbuf.virt) {
470 pci_pool_free(phba->lpfc_hrb_pool, dma_buf->hbuf.virt,
471 dma_buf->hbuf.phys);
472 kfree(dma_buf);
473 return NULL;
474 }
475 dma_buf->size = LPFC_BPL_SIZE;
476 return dma_buf;
477 }
478
479 /**
480 * lpfc_sli4_rb_free - Frees a receive buffer
481 * @phba: HBA buffer was allocated for
482 * @dmab: DMA Buffer container returned by lpfc_sli4_hbq_alloc
483 *
484 * Description: Frees both the container and the DMA-mapped buffers returned by
485 * lpfc_sli4_rb_alloc.
486 *
487 * Notes: Can be called with or without locks held.
488 *
489 * Returns: None
490 **/
491 void
lpfc_sli4_rb_free(struct lpfc_hba * phba,struct hbq_dmabuf * dmab)492 lpfc_sli4_rb_free(struct lpfc_hba *phba, struct hbq_dmabuf *dmab)
493 {
494 pci_pool_free(phba->lpfc_hrb_pool, dmab->hbuf.virt, dmab->hbuf.phys);
495 pci_pool_free(phba->lpfc_drb_pool, dmab->dbuf.virt, dmab->dbuf.phys);
496 kfree(dmab);
497 return;
498 }
499
500 /**
501 * lpfc_in_buf_free - Free a DMA buffer
502 * @phba: HBA buffer is associated with
503 * @mp: Buffer to free
504 *
505 * Description: Frees the given DMA buffer in the appropriate way given if the
506 * HBA is running in SLI3 mode with HBQs enabled.
507 *
508 * Notes: Takes phba->hbalock. Can be called with or without other locks held.
509 *
510 * Returns: None
511 **/
512 void
lpfc_in_buf_free(struct lpfc_hba * phba,struct lpfc_dmabuf * mp)513 lpfc_in_buf_free(struct lpfc_hba *phba, struct lpfc_dmabuf *mp)
514 {
515 struct hbq_dmabuf *hbq_entry;
516 unsigned long flags;
517
518 if (!mp)
519 return;
520
521 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
522 /* Check whether HBQ is still in use */
523 spin_lock_irqsave(&phba->hbalock, flags);
524 if (!phba->hbq_in_use) {
525 spin_unlock_irqrestore(&phba->hbalock, flags);
526 return;
527 }
528 hbq_entry = container_of(mp, struct hbq_dmabuf, dbuf);
529 list_del(&hbq_entry->dbuf.list);
530 if (hbq_entry->tag == -1) {
531 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
532 (phba, hbq_entry);
533 } else {
534 lpfc_sli_free_hbq(phba, hbq_entry);
535 }
536 spin_unlock_irqrestore(&phba->hbalock, flags);
537 } else {
538 lpfc_mbuf_free(phba, mp->virt, mp->phys);
539 kfree(mp);
540 }
541 return;
542 }
543