Skip to content

Commit 349b1e2

Browse files
Justin Teemartinkpetersen
authored andcommitted
scsi: lpfc: Refactor and clean up mailbox command memory free
A lot of repeated clean up code exists when freeing mailbox commands in lpfc_mem_free_all(). Introduce a lpfc_mem_free_sli_mbox() helper routine to refactor the copy-paste code. Additionally, reinitialize the mailbox command structure context pointers to NULL in lpfc_sli4_mbox_cmd_free(). Signed-off-by: Justin Tee <justin.tee@broadcom.com> Link: https://lore.kernel.org/r/20231031191224.150862-7-justintee8345@gmail.com Reviewed-by: Himanshu Madhani <himanshu.madhani@oracle.com> Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
1 parent 57ea41e commit 349b1e2

4 files changed

Lines changed: 40 additions & 23 deletions

File tree

drivers/scsi/lpfc/lpfc_mbox.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1814,7 +1814,9 @@ lpfc_sli4_mbox_cmd_free(struct lpfc_hba *phba, struct lpfcMboxq *mbox)
18141814
dma_free_coherent(&phba->pcidev->dev, SLI4_PAGE_SIZE,
18151815
mbox->sge_array->addr[sgentry], phyaddr);
18161816
}
1817-
/* Free the sge address array memory */
1817+
/* Reinitialize the context pointers to avoid stale usage. */
1818+
mbox->ctx_buf = NULL;
1819+
mbox->context3 = NULL;
18181820
kfree(mbox->sge_array);
18191821
/* Finally, free the mailbox command itself */
18201822
mempool_free(mbox, phba->mbox_mem_pool);

drivers/scsi/lpfc/lpfc_mem.c

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,29 @@
4848
#define LPFC_RRQ_POOL_SIZE 256 /* max elements in non-DMA pool */
4949
#define LPFC_MBX_POOL_SIZE 256 /* max elements in MBX non-DMA pool */
5050

51+
/* lpfc_mbox_free_sli_mbox
52+
*
53+
* @phba: HBA to free memory for
54+
* @mbox: mailbox command to free
55+
*
56+
* This routine detects the mbox type and calls the correct
57+
* free routine to fully release all associated memory.
58+
*/
59+
static void
60+
lpfc_mem_free_sli_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
61+
{
62+
/* Detect if the caller's mbox is an SLI4_CONFIG type. If so, this
63+
* mailbox type requires a different cleanup routine. Otherwise, the
64+
* mailbox is just an mbuf and mem_pool release.
65+
*/
66+
if (phba->sli_rev == LPFC_SLI_REV4 &&
67+
bf_get(lpfc_mqe_command, &mbox->u.mqe) == MBX_SLI4_CONFIG) {
68+
lpfc_sli4_mbox_cmd_free(phba, mbox);
69+
} else {
70+
lpfc_mbox_rsrc_cleanup(phba, mbox, MBOX_THD_UNLOCKED);
71+
}
72+
}
73+
5174
int
5275
lpfc_mem_alloc_active_rrq_pool_s4(struct lpfc_hba *phba) {
5376
size_t bytes;
@@ -288,40 +311,24 @@ lpfc_mem_free_all(struct lpfc_hba *phba)
288311
{
289312
struct lpfc_sli *psli = &phba->sli;
290313
LPFC_MBOXQ_t *mbox, *next_mbox;
291-
struct lpfc_dmabuf *mp;
292314

293315
/* Free memory used in mailbox queue back to mailbox memory pool */
294316
list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq, list) {
295-
mp = (struct lpfc_dmabuf *)(mbox->ctx_buf);
296-
if (mp) {
297-
lpfc_mbuf_free(phba, mp->virt, mp->phys);
298-
kfree(mp);
299-
}
300317
list_del(&mbox->list);
301-
mempool_free(mbox, phba->mbox_mem_pool);
318+
lpfc_mem_free_sli_mbox(phba, mbox);
302319
}
303320
/* Free memory used in mailbox cmpl list back to mailbox memory pool */
304321
list_for_each_entry_safe(mbox, next_mbox, &psli->mboxq_cmpl, list) {
305-
mp = (struct lpfc_dmabuf *)(mbox->ctx_buf);
306-
if (mp) {
307-
lpfc_mbuf_free(phba, mp->virt, mp->phys);
308-
kfree(mp);
309-
}
310322
list_del(&mbox->list);
311-
mempool_free(mbox, phba->mbox_mem_pool);
323+
lpfc_mem_free_sli_mbox(phba, mbox);
312324
}
313325
/* Free the active mailbox command back to the mailbox memory pool */
314326
spin_lock_irq(&phba->hbalock);
315327
psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
316328
spin_unlock_irq(&phba->hbalock);
317329
if (psli->mbox_active) {
318330
mbox = psli->mbox_active;
319-
mp = (struct lpfc_dmabuf *)(mbox->ctx_buf);
320-
if (mp) {
321-
lpfc_mbuf_free(phba, mp->virt, mp->phys);
322-
kfree(mp);
323-
}
324-
mempool_free(mbox, phba->mbox_mem_pool);
331+
lpfc_mem_free_sli_mbox(phba, mbox);
325332
psli->mbox_active = NULL;
326333
}
327334

drivers/scsi/lpfc/lpfc_sli.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22170,6 +22170,12 @@ struct lpfc_io_buf *lpfc_get_io_buf(struct lpfc_hba *phba,
2217022170
* The data will be truncated if datasz is not large enough.
2217122171
* Version 1 is not supported with Embedded mbox cmd, so we must use version 0.
2217222172
* Returns the actual bytes read from the object.
22173+
*
22174+
* This routine is hard coded to use a poll completion. Unlike other
22175+
* sli4_config mailboxes, it uses lpfc_mbuf memory which is not
22176+
* cleaned up in lpfc_sli4_cmd_mbox_free. If this routine is modified
22177+
* to use interrupt-based completions, code is needed to fully cleanup
22178+
* the memory.
2217322179
*/
2217422180
int
2217522181
lpfc_read_object(struct lpfc_hba *phba, char *rdobject, uint32_t *datap,

drivers/scsi/lpfc/lpfc_sli.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,11 @@ typedef struct lpfcMboxq {
182182
struct lpfc_mqe mqe;
183183
} u;
184184
struct lpfc_vport *vport; /* virtual port pointer */
185-
void *ctx_ndlp; /* caller ndlp information */
186-
void *ctx_buf; /* caller buffer information */
187-
void *context3;
185+
void *ctx_ndlp; /* an lpfc_nodelist pointer */
186+
void *ctx_buf; /* an lpfc_dmabuf pointer */
187+
void *context3; /* a generic pointer. Code must
188+
* accommodate the actual datatype.
189+
*/
188190

189191
void (*mbox_cmpl) (struct lpfc_hba *, struct lpfcMboxq *);
190192
uint8_t mbox_flag;

0 commit comments

Comments
 (0)