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