1 /*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
3 * Fibre Channel Host Bus Adapters. *
4 * Copyright (C) 2017-2024 Broadcom. All Rights Reserved. The term *
5 * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. *
6 * Copyright (C) 2009-2015 Emulex. All rights reserved. *
7 * EMULEX and SLI are trademarks of Emulex. *
10 * This program is free software; you can redistribute it and/or *
11 * modify it under the terms of version 2 of the GNU General *
12 * Public License as published by the Free Software Foundation. *
13 * This program is distributed in the hope that it will be useful. *
14 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
15 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
16 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
17 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
18 * TO BE LEGALLY INVALID. See the GNU General Public License for *
19 * more details, a copy of which can be found in the file COPYING *
20 * included with this package. *
21 *******************************************************************/
23 #include <linux/interrupt.h>
24 #include <linux/mempool.h>
25 #include <linux/pci.h>
26 #include <linux/slab.h>
27 #include <linux/delay.h>
28 #include <linux/list.h>
29 #include <linux/bsg-lib.h>
30 #include <linux/vmalloc.h>
32 #include <scsi/scsi.h>
33 #include <scsi/scsi_host.h>
34 #include <scsi/scsi_transport_fc.h>
35 #include <scsi/scsi_bsg_fc.h>
36 #include <scsi/fc/fc_fs.h>
41 #include "lpfc_sli4.h"
44 #include "lpfc_disc.h"
45 #include "lpfc_scsi.h"
47 #include "lpfc_logmsg.h"
48 #include "lpfc_crtn.h"
49 #include "lpfc_debugfs.h"
50 #include "lpfc_vport.h"
51 #include "lpfc_version.h"
53 struct lpfc_bsg_event {
54 struct list_head node;
58 /* Event type and waiter identifiers */
63 /* next two flags are here for the auto-delete logic */
64 unsigned long wait_time_stamp;
67 /* seen and not seen events */
68 struct list_head events_to_get;
69 struct list_head events_to_see;
71 /* driver data associated with the job */
75 struct lpfc_bsg_iocb {
76 struct lpfc_iocbq *cmdiocbq;
77 struct lpfc_dmabuf *rmp;
78 struct lpfc_nodelist *ndlp;
81 struct lpfc_bsg_mbox {
84 struct lpfc_dmabuf *dmabuffers; /* for BIU diags */
85 uint8_t *ext; /* extended mailbox data */
86 uint32_t mbOffset; /* from app */
87 uint32_t inExtWLen; /* from app */
88 uint32_t outExtWLen; /* from app */
96 struct bsg_job *set_job; /* job waiting for this iocb to finish */
98 struct lpfc_bsg_event *evt;
99 struct lpfc_bsg_iocb iocb;
100 struct lpfc_bsg_mbox mbox;
105 struct list_head node;
112 #define BUF_SZ_4K 4096
113 #define SLI_CT_ELX_LOOPBACK 0x10
115 enum ELX_LOOPBACK_CMD {
116 ELX_LOOPBACK_XRI_SETUP,
120 #define ELX_LOOPBACK_HEADER_SZ \
121 (size_t)(&((struct lpfc_sli_ct_request *)NULL)->un)
123 struct lpfc_dmabufext {
124 struct lpfc_dmabuf dma;
130 lpfc_free_bsg_buffers(struct lpfc_hba *phba, struct lpfc_dmabuf *mlist)
132 struct lpfc_dmabuf *mlast, *next_mlast;
135 list_for_each_entry_safe(mlast, next_mlast, &mlist->list,
137 list_del(&mlast->list);
138 lpfc_mbuf_free(phba, mlast->virt, mlast->phys);
141 lpfc_mbuf_free(phba, mlist->virt, mlist->phys);
147 static struct lpfc_dmabuf *
148 lpfc_alloc_bsg_buffers(struct lpfc_hba *phba, unsigned int size,
149 int outbound_buffers, struct ulp_bde64 *bpl,
152 struct lpfc_dmabuf *mlist = NULL;
153 struct lpfc_dmabuf *mp;
154 unsigned int bytes_left = size;
156 /* Verify we can support the size specified */
157 if (!size || (size > (*bpl_entries * LPFC_BPL_SIZE)))
160 /* Determine the number of dma buffers to allocate */
161 *bpl_entries = (size % LPFC_BPL_SIZE ? size/LPFC_BPL_SIZE + 1 :
164 /* Allocate dma buffer and place in BPL passed */
166 /* Allocate dma buffer */
167 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
170 lpfc_free_bsg_buffers(phba, mlist);
174 INIT_LIST_HEAD(&mp->list);
175 mp->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &(mp->phys));
180 lpfc_free_bsg_buffers(phba, mlist);
184 /* Queue it to a linked list */
188 list_add_tail(&mp->list, &mlist->list);
190 /* Add buffer to buffer pointer list */
191 if (outbound_buffers)
192 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
194 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
195 bpl->addrLow = le32_to_cpu(putPaddrLow(mp->phys));
196 bpl->addrHigh = le32_to_cpu(putPaddrHigh(mp->phys));
197 bpl->tus.f.bdeSize = (uint16_t)
198 (bytes_left >= LPFC_BPL_SIZE ? LPFC_BPL_SIZE :
200 bytes_left -= bpl->tus.f.bdeSize;
201 bpl->tus.w = le32_to_cpu(bpl->tus.w);
208 lpfc_bsg_copy_data(struct lpfc_dmabuf *dma_buffers,
209 struct bsg_buffer *bsg_buffers,
210 unsigned int bytes_to_transfer, int to_buffers)
213 struct lpfc_dmabuf *mp;
214 unsigned int transfer_bytes, bytes_copied = 0;
215 unsigned int sg_offset, dma_offset;
216 unsigned char *dma_address, *sg_address;
217 LIST_HEAD(temp_list);
218 struct sg_mapping_iter miter;
220 unsigned int sg_flags = SG_MITER_ATOMIC;
223 list_splice_init(&dma_buffers->list, &temp_list);
224 list_add(&dma_buffers->list, &temp_list);
227 sg_flags |= SG_MITER_FROM_SG;
229 sg_flags |= SG_MITER_TO_SG;
230 sg_miter_start(&miter, bsg_buffers->sg_list, bsg_buffers->sg_cnt,
232 local_irq_save(flags);
233 sg_valid = sg_miter_next(&miter);
234 list_for_each_entry(mp, &temp_list, list) {
236 while (bytes_to_transfer && sg_valid &&
237 (dma_offset < LPFC_BPL_SIZE)) {
238 dma_address = mp->virt + dma_offset;
240 /* Continue previous partial transfer of sg */
241 sg_address = miter.addr + sg_offset;
242 transfer_bytes = miter.length - sg_offset;
244 sg_address = miter.addr;
245 transfer_bytes = miter.length;
247 if (bytes_to_transfer < transfer_bytes)
248 transfer_bytes = bytes_to_transfer;
249 if (transfer_bytes > (LPFC_BPL_SIZE - dma_offset))
250 transfer_bytes = LPFC_BPL_SIZE - dma_offset;
252 memcpy(dma_address, sg_address, transfer_bytes);
254 memcpy(sg_address, dma_address, transfer_bytes);
255 dma_offset += transfer_bytes;
256 sg_offset += transfer_bytes;
257 bytes_to_transfer -= transfer_bytes;
258 bytes_copied += transfer_bytes;
259 if (sg_offset >= miter.length) {
261 sg_valid = sg_miter_next(&miter);
265 sg_miter_stop(&miter);
266 local_irq_restore(flags);
267 list_del_init(&dma_buffers->list);
268 list_splice(&temp_list, &dma_buffers->list);
273 * lpfc_bsg_send_mgmt_cmd_cmp - lpfc_bsg_send_mgmt_cmd's completion handler
274 * @phba: Pointer to HBA context object.
275 * @cmdiocbq: Pointer to command iocb.
276 * @rspiocbq: Pointer to response iocb.
278 * This function is the completion handler for iocbs issued using
279 * lpfc_bsg_send_mgmt_cmd function. This function is called by the
280 * ring event handler function without any lock held. This function
281 * can be called from both worker thread context and interrupt
282 * context. This function also can be called from another thread which
283 * cleans up the SLI layer objects.
284 * This function copies the contents of the response iocb to the
285 * response iocb memory object provided by the caller of
286 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
287 * sleeps for the iocb completion.
290 lpfc_bsg_send_mgmt_cmd_cmp(struct lpfc_hba *phba,
291 struct lpfc_iocbq *cmdiocbq,
292 struct lpfc_iocbq *rspiocbq)
294 struct bsg_job_data *dd_data;
296 struct fc_bsg_reply *bsg_reply;
297 struct lpfc_dmabuf *bmp, *cmp, *rmp;
298 struct lpfc_nodelist *ndlp;
299 struct lpfc_bsg_iocb *iocb;
302 u32 ulp_status, ulp_word4, total_data_placed;
304 dd_data = cmdiocbq->context_un.dd_data;
306 /* Determine if job has been aborted */
307 spin_lock_irqsave(&phba->ct_ev_lock, flags);
308 job = dd_data->set_job;
310 bsg_reply = job->reply;
311 /* Prevent timeout handling from trying to abort job */
314 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
316 /* Close the timeout handler abort window */
317 spin_lock_irqsave(&phba->hbalock, flags);
318 cmdiocbq->cmd_flag &= ~LPFC_IO_CMD_OUTSTANDING;
319 spin_unlock_irqrestore(&phba->hbalock, flags);
321 iocb = &dd_data->context_un.iocb;
322 ndlp = iocb->cmdiocbq->ndlp;
324 cmp = cmdiocbq->cmd_dmabuf;
325 bmp = cmdiocbq->bpl_dmabuf;
326 ulp_status = get_job_ulpstatus(phba, rspiocbq);
327 ulp_word4 = get_job_word4(phba, rspiocbq);
328 total_data_placed = get_job_data_placed(phba, rspiocbq);
330 /* Copy the completed data or set the error status */
334 if (ulp_status == IOSTAT_LOCAL_REJECT) {
335 switch (ulp_word4 & IOERR_PARAM_MASK) {
336 case IOERR_SEQUENCE_TIMEOUT:
339 case IOERR_INVALID_RPI:
350 bsg_reply->reply_payload_rcv_len =
351 lpfc_bsg_copy_data(rmp, &job->reply_payload,
352 total_data_placed, 0);
356 lpfc_free_bsg_buffers(phba, cmp);
357 lpfc_free_bsg_buffers(phba, rmp);
358 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
361 lpfc_sli_release_iocbq(phba, cmdiocbq);
364 /* Complete the job if the job is still active */
367 bsg_reply->result = rc;
368 bsg_job_done(job, bsg_reply->result,
369 bsg_reply->reply_payload_rcv_len);
375 * lpfc_bsg_send_mgmt_cmd - send a CT command from a bsg request
376 * @job: fc_bsg_job to handle
379 lpfc_bsg_send_mgmt_cmd(struct bsg_job *job)
381 struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
382 struct lpfc_rport_data *rdata = fc_bsg_to_rport(job)->dd_data;
383 struct lpfc_hba *phba = vport->phba;
384 struct lpfc_nodelist *ndlp = rdata->pnode;
385 struct fc_bsg_reply *bsg_reply = job->reply;
386 struct ulp_bde64 *bpl = NULL;
387 struct lpfc_iocbq *cmdiocbq = NULL;
388 struct lpfc_dmabuf *bmp = NULL, *cmp = NULL, *rmp = NULL;
389 int request_nseg, reply_nseg;
391 struct bsg_job_data *dd_data;
398 /* in case no data is transferred */
399 bsg_reply->reply_payload_rcv_len = 0;
401 if (ndlp->nlp_flag & NLP_ELS_SND_MASK)
404 /* allocate our bsg tracking structure */
405 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
407 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
408 "2733 Failed allocation of dd_data\n");
413 cmdiocbq = lpfc_sli_get_iocbq(phba);
419 bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
424 bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
430 INIT_LIST_HEAD(&bmp->list);
432 bpl = (struct ulp_bde64 *) bmp->virt;
433 request_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64);
434 cmp = lpfc_alloc_bsg_buffers(phba, job->request_payload.payload_len,
435 1, bpl, &request_nseg);
440 lpfc_bsg_copy_data(cmp, &job->request_payload,
441 job->request_payload.payload_len, 1);
444 reply_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64) - request_nseg;
445 rmp = lpfc_alloc_bsg_buffers(phba, job->reply_payload.payload_len, 0,
452 num_entry = request_nseg + reply_nseg;
454 if (phba->sli_rev == LPFC_SLI_REV4)
455 ulp_context = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
457 ulp_context = ndlp->nlp_rpi;
459 lpfc_sli_prep_gen_req(phba, cmdiocbq, bmp, ulp_context, num_entry,
462 cmdiocbq->num_bdes = num_entry;
463 cmdiocbq->vport = phba->pport;
464 cmdiocbq->cmd_dmabuf = cmp;
465 cmdiocbq->bpl_dmabuf = bmp;
466 cmdiocbq->cmd_flag |= LPFC_IO_LIBDFC;
468 cmdiocbq->cmd_cmpl = lpfc_bsg_send_mgmt_cmd_cmp;
469 cmdiocbq->context_un.dd_data = dd_data;
471 dd_data->type = TYPE_IOCB;
472 dd_data->set_job = job;
473 dd_data->context_un.iocb.cmdiocbq = cmdiocbq;
474 dd_data->context_un.iocb.rmp = rmp;
475 job->dd_data = dd_data;
477 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
478 if (lpfc_readl(phba->HCregaddr, &creg_val)) {
482 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
483 writel(creg_val, phba->HCregaddr);
484 readl(phba->HCregaddr); /* flush */
487 cmdiocbq->ndlp = lpfc_nlp_get(ndlp);
488 if (!cmdiocbq->ndlp) {
493 iocb_stat = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq, 0);
494 if (iocb_stat == IOCB_SUCCESS) {
495 spin_lock_irqsave(&phba->hbalock, flags);
496 /* make sure the I/O had not been completed yet */
497 if (cmdiocbq->cmd_flag & LPFC_IO_LIBDFC) {
498 /* open up abort window to timeout handler */
499 cmdiocbq->cmd_flag |= LPFC_IO_CMD_OUTSTANDING;
501 spin_unlock_irqrestore(&phba->hbalock, flags);
502 return 0; /* done for now */
503 } else if (iocb_stat == IOCB_BUSY) {
509 /* iocb failed so cleanup */
513 lpfc_free_bsg_buffers(phba, rmp);
515 lpfc_free_bsg_buffers(phba, cmp);
518 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
521 lpfc_sli_release_iocbq(phba, cmdiocbq);
525 /* make error code available to userspace */
526 bsg_reply->result = rc;
532 * lpfc_bsg_rport_els_cmp - lpfc_bsg_rport_els's completion handler
533 * @phba: Pointer to HBA context object.
534 * @cmdiocbq: Pointer to command iocb.
535 * @rspiocbq: Pointer to response iocb.
537 * This function is the completion handler for iocbs issued using
538 * lpfc_bsg_rport_els_cmp function. This function is called by the
539 * ring event handler function without any lock held. This function
540 * can be called from both worker thread context and interrupt
541 * context. This function also can be called from other thread which
542 * cleans up the SLI layer objects.
543 * This function copies the contents of the response iocb to the
544 * response iocb memory object provided by the caller of
545 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
546 * sleeps for the iocb completion.
549 lpfc_bsg_rport_els_cmp(struct lpfc_hba *phba,
550 struct lpfc_iocbq *cmdiocbq,
551 struct lpfc_iocbq *rspiocbq)
553 struct bsg_job_data *dd_data;
555 struct fc_bsg_reply *bsg_reply;
556 struct lpfc_nodelist *ndlp;
557 struct lpfc_dmabuf *pcmd = NULL, *prsp = NULL;
558 struct fc_bsg_ctels_reply *els_reply;
561 unsigned int rsp_size;
563 u32 ulp_status, ulp_word4, total_data_placed;
565 dd_data = cmdiocbq->context_un.dd_data;
566 ndlp = dd_data->context_un.iocb.ndlp;
567 cmdiocbq->ndlp = ndlp;
569 /* Determine if job has been aborted */
570 spin_lock_irqsave(&phba->ct_ev_lock, flags);
571 job = dd_data->set_job;
573 bsg_reply = job->reply;
574 /* Prevent timeout handling from trying to abort job */
577 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
579 /* Close the timeout handler abort window */
580 spin_lock_irqsave(&phba->hbalock, flags);
581 cmdiocbq->cmd_flag &= ~LPFC_IO_CMD_OUTSTANDING;
582 spin_unlock_irqrestore(&phba->hbalock, flags);
584 ulp_status = get_job_ulpstatus(phba, rspiocbq);
585 ulp_word4 = get_job_word4(phba, rspiocbq);
586 total_data_placed = get_job_data_placed(phba, rspiocbq);
587 pcmd = cmdiocbq->cmd_dmabuf;
588 prsp = (struct lpfc_dmabuf *)pcmd->list.next;
590 /* Copy the completed job data or determine the job status if job is
595 if (ulp_status == IOSTAT_SUCCESS) {
596 rsp_size = total_data_placed;
597 bsg_reply->reply_payload_rcv_len =
598 sg_copy_from_buffer(job->reply_payload.sg_list,
599 job->reply_payload.sg_cnt,
602 } else if (ulp_status == IOSTAT_LS_RJT) {
603 bsg_reply->reply_payload_rcv_len =
604 sizeof(struct fc_bsg_ctels_reply);
605 /* LS_RJT data returned in word 4 */
606 rjt_data = (uint8_t *)&ulp_word4;
607 els_reply = &bsg_reply->reply_data.ctels_reply;
608 els_reply->status = FC_CTELS_STATUS_REJECT;
609 els_reply->rjt_data.action = rjt_data[3];
610 els_reply->rjt_data.reason_code = rjt_data[2];
611 els_reply->rjt_data.reason_explanation = rjt_data[1];
612 els_reply->rjt_data.vendor_unique = rjt_data[0];
613 } else if (ulp_status == IOSTAT_LOCAL_REJECT &&
614 (ulp_word4 & IOERR_PARAM_MASK) ==
615 IOERR_SEQUENCE_TIMEOUT) {
622 lpfc_els_free_iocb(phba, cmdiocbq);
627 /* Complete the job if the job is still active */
630 bsg_reply->result = rc;
631 bsg_job_done(job, bsg_reply->result,
632 bsg_reply->reply_payload_rcv_len);
638 * lpfc_bsg_rport_els - send an ELS command from a bsg request
639 * @job: fc_bsg_job to handle
642 lpfc_bsg_rport_els(struct bsg_job *job)
644 struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
645 struct lpfc_hba *phba = vport->phba;
646 struct lpfc_rport_data *rdata = fc_bsg_to_rport(job)->dd_data;
647 struct lpfc_nodelist *ndlp = rdata->pnode;
648 struct fc_bsg_request *bsg_request = job->request;
649 struct fc_bsg_reply *bsg_reply = job->reply;
652 struct lpfc_iocbq *cmdiocbq;
654 struct bsg_job_data *dd_data;
659 /* in case no data is transferred */
660 bsg_reply->reply_payload_rcv_len = 0;
662 /* verify the els command is not greater than the
663 * maximum ELS transfer size.
666 if (job->request_payload.payload_len > FCELSSIZE) {
671 /* allocate our bsg tracking structure */
672 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
674 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
675 "2735 Failed allocation of dd_data\n");
680 elscmd = bsg_request->rqst_data.r_els.els_code;
681 cmdsize = job->request_payload.payload_len;
683 if (!lpfc_nlp_get(ndlp)) {
688 /* We will use the allocated dma buffers by prep els iocb for command
689 * and response to ensure if the job times out and the request is freed,
690 * we won't be dma into memory that is no longer allocated to for the
693 cmdiocbq = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp,
694 ndlp->nlp_DID, elscmd);
700 /* Transfer the request payload to allocated command dma buffer */
701 sg_copy_to_buffer(job->request_payload.sg_list,
702 job->request_payload.sg_cnt,
703 cmdiocbq->cmd_dmabuf->virt,
708 if (phba->sli_rev == LPFC_SLI_REV4)
709 bf_set(wqe_ctxt_tag, &cmdiocbq->wqe.generic.wqe_com,
710 phba->sli4_hba.rpi_ids[rpi]);
712 cmdiocbq->iocb.ulpContext = rpi;
713 cmdiocbq->cmd_flag |= LPFC_IO_LIBDFC;
714 cmdiocbq->context_un.dd_data = dd_data;
715 cmdiocbq->ndlp = ndlp;
716 cmdiocbq->cmd_cmpl = lpfc_bsg_rport_els_cmp;
717 dd_data->type = TYPE_IOCB;
718 dd_data->set_job = job;
719 dd_data->context_un.iocb.cmdiocbq = cmdiocbq;
720 dd_data->context_un.iocb.ndlp = ndlp;
721 dd_data->context_un.iocb.rmp = NULL;
722 job->dd_data = dd_data;
724 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
725 if (lpfc_readl(phba->HCregaddr, &creg_val)) {
729 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
730 writel(creg_val, phba->HCregaddr);
731 readl(phba->HCregaddr); /* flush */
734 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq, 0);
735 if (rc == IOCB_SUCCESS) {
736 spin_lock_irqsave(&phba->hbalock, flags);
737 /* make sure the I/O had not been completed/released */
738 if (cmdiocbq->cmd_flag & LPFC_IO_LIBDFC) {
739 /* open up abort window to timeout handler */
740 cmdiocbq->cmd_flag |= LPFC_IO_CMD_OUTSTANDING;
742 spin_unlock_irqrestore(&phba->hbalock, flags);
743 return 0; /* done for now */
744 } else if (rc == IOCB_BUSY) {
750 /* I/O issue failed. Cleanup resources. */
753 lpfc_els_free_iocb(phba, cmdiocbq);
762 /* make error code available to userspace */
763 bsg_reply->result = rc;
769 * lpfc_bsg_event_free - frees an allocated event structure
770 * @kref: Pointer to a kref.
772 * Called from kref_put. Back cast the kref into an event structure address.
773 * Free any events to get, delete associated nodes, free any events to see,
774 * free any data then free the event itself.
777 lpfc_bsg_event_free(struct kref *kref)
779 struct lpfc_bsg_event *evt = container_of(kref, struct lpfc_bsg_event,
781 struct event_data *ed;
783 list_del(&evt->node);
785 while (!list_empty(&evt->events_to_get)) {
786 ed = list_entry(evt->events_to_get.next, typeof(*ed), node);
792 while (!list_empty(&evt->events_to_see)) {
793 ed = list_entry(evt->events_to_see.next, typeof(*ed), node);
804 * lpfc_bsg_event_ref - increments the kref for an event
805 * @evt: Pointer to an event structure.
808 lpfc_bsg_event_ref(struct lpfc_bsg_event *evt)
810 kref_get(&evt->kref);
814 * lpfc_bsg_event_unref - Uses kref_put to free an event structure
815 * @evt: Pointer to an event structure.
818 lpfc_bsg_event_unref(struct lpfc_bsg_event *evt)
820 kref_put(&evt->kref, lpfc_bsg_event_free);
824 * lpfc_bsg_event_new - allocate and initialize a event structure
825 * @ev_mask: Mask of events.
826 * @ev_reg_id: Event reg id.
827 * @ev_req_id: Event request id.
829 static struct lpfc_bsg_event *
830 lpfc_bsg_event_new(uint32_t ev_mask, int ev_reg_id, uint32_t ev_req_id)
832 struct lpfc_bsg_event *evt = kzalloc(sizeof(*evt), GFP_KERNEL);
837 INIT_LIST_HEAD(&evt->events_to_get);
838 INIT_LIST_HEAD(&evt->events_to_see);
839 evt->type_mask = ev_mask;
840 evt->req_id = ev_req_id;
841 evt->reg_id = ev_reg_id;
842 evt->wait_time_stamp = jiffies;
844 init_waitqueue_head(&evt->wq);
845 kref_init(&evt->kref);
850 * diag_cmd_data_free - Frees an lpfc dma buffer extension
851 * @phba: Pointer to HBA context object.
852 * @mlist: Pointer to an lpfc dma buffer extension.
855 diag_cmd_data_free(struct lpfc_hba *phba, struct lpfc_dmabufext *mlist)
857 struct lpfc_dmabufext *mlast;
858 struct pci_dev *pcidev;
859 struct list_head head, *curr, *next;
861 if ((!mlist) || (!lpfc_is_link_up(phba) &&
862 (phba->link_flag & LS_LOOPBACK_MODE))) {
866 pcidev = phba->pcidev;
867 list_add_tail(&head, &mlist->dma.list);
869 list_for_each_safe(curr, next, &head) {
870 mlast = list_entry(curr, struct lpfc_dmabufext , dma.list);
872 dma_free_coherent(&pcidev->dev,
882 * lpfc_bsg_ct_unsol_event - process an unsolicited CT command
884 * This function is called when an unsolicited CT command is received. It
885 * forwards the event to any processes registered to receive CT events.
888 lpfc_bsg_ct_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
889 struct lpfc_iocbq *piocbq)
891 uint32_t evt_req_id = 0;
893 struct lpfc_dmabuf *dmabuf = NULL;
894 struct lpfc_bsg_event *evt;
895 struct event_data *evt_dat = NULL;
896 struct lpfc_iocbq *iocbq;
899 struct list_head head;
900 struct ulp_bde64 *bde;
903 struct lpfc_dmabuf *bdeBuf1 = piocbq->cmd_dmabuf;
904 struct lpfc_dmabuf *bdeBuf2 = piocbq->bpl_dmabuf;
905 struct lpfc_sli_ct_request *ct_req;
906 struct bsg_job *job = NULL;
907 struct fc_bsg_reply *bsg_reply;
908 struct bsg_job_data *dd_data = NULL;
913 INIT_LIST_HEAD(&head);
914 list_add_tail(&head, &piocbq->list);
916 ct_req = (struct lpfc_sli_ct_request *)bdeBuf1->virt;
917 evt_req_id = ct_req->FsType;
918 cmd = be16_to_cpu(ct_req->CommandResponse.bits.CmdRsp);
920 spin_lock_irqsave(&phba->ct_ev_lock, flags);
921 list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
922 if (!(evt->type_mask & FC_REG_CT_EVENT) ||
923 evt->req_id != evt_req_id)
926 lpfc_bsg_event_ref(evt);
927 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
928 evt_dat = kzalloc(sizeof(*evt_dat), GFP_KERNEL);
929 if (evt_dat == NULL) {
930 spin_lock_irqsave(&phba->ct_ev_lock, flags);
931 lpfc_bsg_event_unref(evt);
932 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
933 "2614 Memory allocation failed for "
938 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
939 /* take accumulated byte count from the last iocbq */
940 iocbq = list_entry(head.prev, typeof(*iocbq), list);
941 if (phba->sli_rev == LPFC_SLI_REV4)
942 evt_dat->len = iocbq->wcqe_cmpl.total_data_placed;
944 evt_dat->len = iocbq->iocb.unsli3.rcvsli3.acc_len;
946 list_for_each_entry(iocbq, &head, list) {
948 for (i = 0; i < iocb->ulpBdeCount;
951 iocb->un.cont64[i].tus.f.bdeSize;
955 evt_dat->data = kzalloc(evt_dat->len, GFP_KERNEL);
956 if (evt_dat->data == NULL) {
957 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
958 "2615 Memory allocation failed for "
959 "CT event data, size %d\n",
962 spin_lock_irqsave(&phba->ct_ev_lock, flags);
963 lpfc_bsg_event_unref(evt);
964 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
965 goto error_ct_unsol_exit;
968 list_for_each_entry(iocbq, &head, list) {
970 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
971 bdeBuf1 = iocbq->cmd_dmabuf;
972 bdeBuf2 = iocbq->bpl_dmabuf;
974 if (phba->sli_rev == LPFC_SLI_REV4)
975 bde_count = iocbq->wcqe_cmpl.word3;
977 bde_count = iocbq->iocb.ulpBdeCount;
978 for (i = 0; i < bde_count; i++) {
979 if (phba->sli3_options &
980 LPFC_SLI3_HBQ_ENABLED) {
982 size = iocbq->wqe.gen_req.bde.tus.f.bdeSize;
985 size = iocbq->unsol_rcv_len;
988 if ((offset + size) > evt_dat->len)
989 size = evt_dat->len - offset;
991 size = iocbq->iocb.un.cont64[i].
993 bde = &iocbq->iocb.un.cont64[i];
994 dma_addr = getPaddr(bde->addrHigh,
996 dmabuf = lpfc_sli_ringpostbuf_get(phba,
1000 lpfc_printf_log(phba, KERN_ERR,
1001 LOG_LIBDFC, "2616 No dmabuf "
1002 "found for iocbq x%px\n",
1004 kfree(evt_dat->data);
1006 spin_lock_irqsave(&phba->ct_ev_lock,
1008 lpfc_bsg_event_unref(evt);
1009 spin_unlock_irqrestore(
1010 &phba->ct_ev_lock, flags);
1011 goto error_ct_unsol_exit;
1013 memcpy((char *)(evt_dat->data) + offset,
1014 dmabuf->virt, size);
1016 if (evt_req_id != SLI_CT_ELX_LOOPBACK &&
1017 !(phba->sli3_options &
1018 LPFC_SLI3_HBQ_ENABLED)) {
1019 lpfc_sli_ringpostbuf_put(phba, pring,
1023 case ELX_LOOPBACK_DATA:
1026 diag_cmd_data_free(phba,
1027 (struct lpfc_dmabufext
1030 case ELX_LOOPBACK_XRI_SETUP:
1031 if ((phba->sli_rev ==
1033 (phba->sli3_options &
1034 LPFC_SLI3_HBQ_ENABLED
1036 lpfc_in_buf_free(phba,
1039 lpfc_sli3_post_buffer(phba,
1045 if (!(phba->sli3_options &
1046 LPFC_SLI3_HBQ_ENABLED))
1047 lpfc_sli3_post_buffer(phba,
1056 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1057 if (phba->sli_rev == LPFC_SLI_REV4) {
1058 evt_dat->immed_dat = phba->ctx_idx;
1059 phba->ctx_idx = (phba->ctx_idx + 1) % LPFC_CT_CTX_MAX;
1060 /* Provide warning for over-run of the ct_ctx array */
1061 if (phba->ct_ctx[evt_dat->immed_dat].valid ==
1063 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
1064 "2717 CT context array entry "
1065 "[%d] over-run: oxid:x%x, "
1066 "sid:x%x\n", phba->ctx_idx,
1068 evt_dat->immed_dat].oxid,
1070 evt_dat->immed_dat].SID);
1071 phba->ct_ctx[evt_dat->immed_dat].rxid =
1072 get_job_ulpcontext(phba, piocbq);
1073 phba->ct_ctx[evt_dat->immed_dat].oxid =
1074 get_job_rcvoxid(phba, piocbq);
1075 phba->ct_ctx[evt_dat->immed_dat].SID =
1077 &piocbq->wqe.xmit_els_rsp.wqe_dest);
1078 phba->ct_ctx[evt_dat->immed_dat].valid = UNSOL_VALID;
1080 evt_dat->immed_dat = get_job_ulpcontext(phba, piocbq);
1082 evt_dat->type = FC_REG_CT_EVENT;
1083 list_add(&evt_dat->node, &evt->events_to_see);
1084 if (evt_req_id == SLI_CT_ELX_LOOPBACK) {
1085 wake_up_interruptible(&evt->wq);
1086 lpfc_bsg_event_unref(evt);
1090 list_move(evt->events_to_see.prev, &evt->events_to_get);
1092 dd_data = (struct bsg_job_data *)evt->dd_data;
1093 job = dd_data->set_job;
1094 dd_data->set_job = NULL;
1095 lpfc_bsg_event_unref(evt);
1097 bsg_reply = job->reply;
1098 bsg_reply->reply_payload_rcv_len = size;
1099 /* make error code available to userspace */
1100 bsg_reply->result = 0;
1101 job->dd_data = NULL;
1102 /* complete the job back to userspace */
1103 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1104 bsg_job_done(job, bsg_reply->result,
1105 bsg_reply->reply_payload_rcv_len);
1106 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1109 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1111 error_ct_unsol_exit:
1112 if (!list_empty(&head))
1114 if ((phba->sli_rev < LPFC_SLI_REV4) &&
1115 (evt_req_id == SLI_CT_ELX_LOOPBACK))
1121 * lpfc_bsg_ct_unsol_abort - handler ct abort to management plane
1122 * @phba: Pointer to HBA context object.
1123 * @dmabuf: pointer to a dmabuf that describes the FC sequence
1125 * This function handles abort to the CT command toward management plane
1128 * If the pending context of a CT command to management plane present, clears
1129 * such context and returns 1 for handled; otherwise, it returns 0 indicating
1130 * no context exists.
1133 lpfc_bsg_ct_unsol_abort(struct lpfc_hba *phba, struct hbq_dmabuf *dmabuf)
1135 struct fc_frame_header fc_hdr;
1136 struct fc_frame_header *fc_hdr_ptr = &fc_hdr;
1137 int ctx_idx, handled = 0;
1138 uint16_t oxid, rxid;
1141 memcpy(fc_hdr_ptr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
1142 sid = sli4_sid_from_fc_hdr(fc_hdr_ptr);
1143 oxid = be16_to_cpu(fc_hdr_ptr->fh_ox_id);
1144 rxid = be16_to_cpu(fc_hdr_ptr->fh_rx_id);
1146 for (ctx_idx = 0; ctx_idx < LPFC_CT_CTX_MAX; ctx_idx++) {
1147 if (phba->ct_ctx[ctx_idx].valid != UNSOL_VALID)
1149 if (phba->ct_ctx[ctx_idx].rxid != rxid)
1151 if (phba->ct_ctx[ctx_idx].oxid != oxid)
1153 if (phba->ct_ctx[ctx_idx].SID != sid)
1155 phba->ct_ctx[ctx_idx].valid = UNSOL_INVALID;
1162 * lpfc_bsg_hba_set_event - process a SET_EVENT bsg vendor command
1163 * @job: SET_EVENT fc_bsg_job
1166 lpfc_bsg_hba_set_event(struct bsg_job *job)
1168 struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
1169 struct lpfc_hba *phba = vport->phba;
1170 struct fc_bsg_request *bsg_request = job->request;
1171 struct set_ct_event *event_req;
1172 struct lpfc_bsg_event *evt;
1174 struct bsg_job_data *dd_data = NULL;
1176 unsigned long flags;
1178 if (job->request_len <
1179 sizeof(struct fc_bsg_request) + sizeof(struct set_ct_event)) {
1180 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1181 "2612 Received SET_CT_EVENT below minimum "
1187 event_req = (struct set_ct_event *)
1188 bsg_request->rqst_data.h_vendor.vendor_cmd;
1189 ev_mask = ((uint32_t)(unsigned long)event_req->type_mask &
1191 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1192 list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
1193 if (evt->reg_id == event_req->ev_reg_id) {
1194 lpfc_bsg_event_ref(evt);
1195 evt->wait_time_stamp = jiffies;
1196 dd_data = (struct bsg_job_data *)evt->dd_data;
1200 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1202 if (&evt->node == &phba->ct_ev_waiters) {
1203 /* no event waiting struct yet - first call */
1204 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
1205 if (dd_data == NULL) {
1206 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1207 "2734 Failed allocation of dd_data\n");
1211 evt = lpfc_bsg_event_new(ev_mask, event_req->ev_reg_id,
1212 event_req->ev_req_id);
1214 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1215 "2617 Failed allocation of event "
1220 dd_data->type = TYPE_EVT;
1221 dd_data->set_job = NULL;
1222 dd_data->context_un.evt = evt;
1223 evt->dd_data = (void *)dd_data;
1224 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1225 list_add(&evt->node, &phba->ct_ev_waiters);
1226 lpfc_bsg_event_ref(evt);
1227 evt->wait_time_stamp = jiffies;
1228 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1231 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1233 dd_data->set_job = job; /* for unsolicited command */
1234 job->dd_data = dd_data; /* for fc transport timeout callback*/
1235 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1236 return 0; /* call job done later */
1240 job->dd_data = NULL;
1245 * lpfc_bsg_hba_get_event - process a GET_EVENT bsg vendor command
1246 * @job: GET_EVENT fc_bsg_job
1249 lpfc_bsg_hba_get_event(struct bsg_job *job)
1251 struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
1252 struct lpfc_hba *phba = vport->phba;
1253 struct fc_bsg_request *bsg_request = job->request;
1254 struct fc_bsg_reply *bsg_reply = job->reply;
1255 struct get_ct_event *event_req;
1256 struct get_ct_event_reply *event_reply;
1257 struct lpfc_bsg_event *evt, *evt_next;
1258 struct event_data *evt_dat = NULL;
1259 unsigned long flags;
1262 if (job->request_len <
1263 sizeof(struct fc_bsg_request) + sizeof(struct get_ct_event)) {
1264 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1265 "2613 Received GET_CT_EVENT request below "
1271 event_req = (struct get_ct_event *)
1272 bsg_request->rqst_data.h_vendor.vendor_cmd;
1274 event_reply = (struct get_ct_event_reply *)
1275 bsg_reply->reply_data.vendor_reply.vendor_rsp;
1276 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1277 list_for_each_entry_safe(evt, evt_next, &phba->ct_ev_waiters, node) {
1278 if (evt->reg_id == event_req->ev_reg_id) {
1279 if (list_empty(&evt->events_to_get))
1281 lpfc_bsg_event_ref(evt);
1282 evt->wait_time_stamp = jiffies;
1283 evt_dat = list_entry(evt->events_to_get.prev,
1284 struct event_data, node);
1285 list_del(&evt_dat->node);
1289 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1291 /* The app may continue to ask for event data until it gets
1292 * an error indicating that there isn't anymore
1294 if (evt_dat == NULL) {
1295 bsg_reply->reply_payload_rcv_len = 0;
1300 if (evt_dat->len > job->request_payload.payload_len) {
1301 evt_dat->len = job->request_payload.payload_len;
1302 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1303 "2618 Truncated event data at %d "
1305 job->request_payload.payload_len);
1308 event_reply->type = evt_dat->type;
1309 event_reply->immed_data = evt_dat->immed_dat;
1310 if (evt_dat->len > 0)
1311 bsg_reply->reply_payload_rcv_len =
1312 sg_copy_from_buffer(job->request_payload.sg_list,
1313 job->request_payload.sg_cnt,
1314 evt_dat->data, evt_dat->len);
1316 bsg_reply->reply_payload_rcv_len = 0;
1319 kfree(evt_dat->data);
1323 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1324 lpfc_bsg_event_unref(evt);
1325 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1326 job->dd_data = NULL;
1327 bsg_reply->result = 0;
1328 bsg_job_done(job, bsg_reply->result,
1329 bsg_reply->reply_payload_rcv_len);
1333 job->dd_data = NULL;
1334 bsg_reply->result = rc;
1339 * lpfc_issue_ct_rsp_cmp - lpfc_issue_ct_rsp's completion handler
1340 * @phba: Pointer to HBA context object.
1341 * @cmdiocbq: Pointer to command iocb.
1342 * @rspiocbq: Pointer to response iocb.
1344 * This function is the completion handler for iocbs issued using
1345 * lpfc_issue_ct_rsp_cmp function. This function is called by the
1346 * ring event handler function without any lock held. This function
1347 * can be called from both worker thread context and interrupt
1348 * context. This function also can be called from other thread which
1349 * cleans up the SLI layer objects.
1350 * This function copy the contents of the response iocb to the
1351 * response iocb memory object provided by the caller of
1352 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
1353 * sleeps for the iocb completion.
1356 lpfc_issue_ct_rsp_cmp(struct lpfc_hba *phba,
1357 struct lpfc_iocbq *cmdiocbq,
1358 struct lpfc_iocbq *rspiocbq)
1360 struct bsg_job_data *dd_data;
1361 struct bsg_job *job;
1362 struct fc_bsg_reply *bsg_reply;
1363 struct lpfc_dmabuf *bmp, *cmp;
1364 struct lpfc_nodelist *ndlp;
1365 unsigned long flags;
1367 u32 ulp_status, ulp_word4;
1369 dd_data = cmdiocbq->context_un.dd_data;
1371 /* Determine if job has been aborted */
1372 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1373 job = dd_data->set_job;
1375 /* Prevent timeout handling from trying to abort job */
1376 job->dd_data = NULL;
1378 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1380 /* Close the timeout handler abort window */
1381 spin_lock_irqsave(&phba->hbalock, flags);
1382 cmdiocbq->cmd_flag &= ~LPFC_IO_CMD_OUTSTANDING;
1383 spin_unlock_irqrestore(&phba->hbalock, flags);
1385 ndlp = dd_data->context_un.iocb.ndlp;
1386 cmp = cmdiocbq->cmd_dmabuf;
1387 bmp = cmdiocbq->bpl_dmabuf;
1389 ulp_status = get_job_ulpstatus(phba, rspiocbq);
1390 ulp_word4 = get_job_word4(phba, rspiocbq);
1392 /* Copy the completed job data or set the error status */
1395 bsg_reply = job->reply;
1397 if (ulp_status == IOSTAT_LOCAL_REJECT) {
1398 switch (ulp_word4 & IOERR_PARAM_MASK) {
1399 case IOERR_SEQUENCE_TIMEOUT:
1402 case IOERR_INVALID_RPI:
1413 bsg_reply->reply_payload_rcv_len = 0;
1417 lpfc_free_bsg_buffers(phba, cmp);
1418 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
1420 lpfc_sli_release_iocbq(phba, cmdiocbq);
1424 /* Complete the job if the job is still active */
1427 bsg_reply->result = rc;
1428 bsg_job_done(job, bsg_reply->result,
1429 bsg_reply->reply_payload_rcv_len);
1435 * lpfc_issue_ct_rsp - issue a ct response
1436 * @phba: Pointer to HBA context object.
1437 * @job: Pointer to the job object.
1438 * @tag: tag index value into the ports context exchange array.
1439 * @cmp: Pointer to a cmp dma buffer descriptor.
1440 * @bmp: Pointer to a bmp dma buffer descriptor.
1441 * @num_entry: Number of enties in the bde.
1444 lpfc_issue_ct_rsp(struct lpfc_hba *phba, struct bsg_job *job, uint32_t tag,
1445 struct lpfc_dmabuf *cmp, struct lpfc_dmabuf *bmp,
1448 struct lpfc_iocbq *ctiocb = NULL;
1450 struct lpfc_nodelist *ndlp = NULL;
1451 struct bsg_job_data *dd_data;
1452 unsigned long flags;
1454 u16 ulp_context, iotag;
1456 ndlp = lpfc_findnode_did(phba->pport, phba->ct_ctx[tag].SID);
1458 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
1459 "2721 ndlp null for oxid %x SID %x\n",
1460 phba->ct_ctx[tag].rxid,
1461 phba->ct_ctx[tag].SID);
1465 /* allocate our bsg tracking structure */
1466 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
1468 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1469 "2736 Failed allocation of dd_data\n");
1474 /* Allocate buffer for command iocb */
1475 ctiocb = lpfc_sli_get_iocbq(phba);
1481 if (phba->sli_rev == LPFC_SLI_REV4) {
1482 /* Do not issue unsol response if oxid not marked as valid */
1483 if (phba->ct_ctx[tag].valid != UNSOL_VALID) {
1485 goto issue_ct_rsp_exit;
1488 lpfc_sli_prep_xmit_seq64(phba, ctiocb, bmp,
1489 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
1490 phba->ct_ctx[tag].oxid, num_entry,
1491 FC_RCTL_DD_SOL_CTL, 1,
1492 CMD_XMIT_SEQUENCE64_WQE);
1494 /* The exchange is done, mark the entry as invalid */
1495 phba->ct_ctx[tag].valid = UNSOL_INVALID;
1496 iotag = get_wqe_reqtag(ctiocb);
1498 lpfc_sli_prep_xmit_seq64(phba, ctiocb, bmp, 0, tag, num_entry,
1499 FC_RCTL_DD_SOL_CTL, 1,
1500 CMD_XMIT_SEQUENCE64_CX);
1501 ctiocb->num_bdes = num_entry;
1502 iotag = ctiocb->iocb.ulpIoTag;
1505 ulp_context = get_job_ulpcontext(phba, ctiocb);
1507 /* Xmit CT response on exchange <xid> */
1508 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
1509 "2722 Xmit CT response on exchange x%x Data: x%x x%x x%x\n",
1510 ulp_context, iotag, tag, phba->link_state);
1512 ctiocb->cmd_flag |= LPFC_IO_LIBDFC;
1513 ctiocb->vport = phba->pport;
1514 ctiocb->context_un.dd_data = dd_data;
1515 ctiocb->cmd_dmabuf = cmp;
1516 ctiocb->bpl_dmabuf = bmp;
1517 ctiocb->ndlp = ndlp;
1518 ctiocb->cmd_cmpl = lpfc_issue_ct_rsp_cmp;
1520 dd_data->type = TYPE_IOCB;
1521 dd_data->set_job = job;
1522 dd_data->context_un.iocb.cmdiocbq = ctiocb;
1523 dd_data->context_un.iocb.ndlp = lpfc_nlp_get(ndlp);
1524 if (!dd_data->context_un.iocb.ndlp) {
1526 goto issue_ct_rsp_exit;
1528 dd_data->context_un.iocb.rmp = NULL;
1529 job->dd_data = dd_data;
1531 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
1532 if (lpfc_readl(phba->HCregaddr, &creg_val)) {
1534 goto issue_ct_rsp_exit;
1536 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
1537 writel(creg_val, phba->HCregaddr);
1538 readl(phba->HCregaddr); /* flush */
1541 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
1542 if (rc == IOCB_SUCCESS) {
1543 spin_lock_irqsave(&phba->hbalock, flags);
1544 /* make sure the I/O had not been completed/released */
1545 if (ctiocb->cmd_flag & LPFC_IO_LIBDFC) {
1546 /* open up abort window to timeout handler */
1547 ctiocb->cmd_flag |= LPFC_IO_CMD_OUTSTANDING;
1549 spin_unlock_irqrestore(&phba->hbalock, flags);
1550 return 0; /* done for now */
1553 /* iocb failed so cleanup */
1554 job->dd_data = NULL;
1558 lpfc_sli_release_iocbq(phba, ctiocb);
1566 * lpfc_bsg_send_mgmt_rsp - process a SEND_MGMT_RESP bsg vendor command
1567 * @job: SEND_MGMT_RESP fc_bsg_job
1570 lpfc_bsg_send_mgmt_rsp(struct bsg_job *job)
1572 struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
1573 struct lpfc_hba *phba = vport->phba;
1574 struct fc_bsg_request *bsg_request = job->request;
1575 struct fc_bsg_reply *bsg_reply = job->reply;
1576 struct send_mgmt_resp *mgmt_resp = (struct send_mgmt_resp *)
1577 bsg_request->rqst_data.h_vendor.vendor_cmd;
1578 struct ulp_bde64 *bpl;
1579 struct lpfc_dmabuf *bmp = NULL, *cmp = NULL;
1581 uint32_t tag = mgmt_resp->tag;
1582 unsigned long reqbfrcnt =
1583 (unsigned long)job->request_payload.payload_len;
1586 /* in case no data is transferred */
1587 bsg_reply->reply_payload_rcv_len = 0;
1589 if (!reqbfrcnt || (reqbfrcnt > (80 * BUF_SZ_4K))) {
1591 goto send_mgmt_rsp_exit;
1594 bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
1597 goto send_mgmt_rsp_exit;
1600 bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
1603 goto send_mgmt_rsp_free_bmp;
1606 INIT_LIST_HEAD(&bmp->list);
1607 bpl = (struct ulp_bde64 *) bmp->virt;
1608 bpl_entries = (LPFC_BPL_SIZE/sizeof(struct ulp_bde64));
1609 cmp = lpfc_alloc_bsg_buffers(phba, job->request_payload.payload_len,
1610 1, bpl, &bpl_entries);
1613 goto send_mgmt_rsp_free_bmp;
1615 lpfc_bsg_copy_data(cmp, &job->request_payload,
1616 job->request_payload.payload_len, 1);
1618 rc = lpfc_issue_ct_rsp(phba, job, tag, cmp, bmp, bpl_entries);
1620 if (rc == IOCB_SUCCESS)
1621 return 0; /* done for now */
1625 lpfc_free_bsg_buffers(phba, cmp);
1627 send_mgmt_rsp_free_bmp:
1629 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
1632 /* make error code available to userspace */
1633 bsg_reply->result = rc;
1634 job->dd_data = NULL;
1639 * lpfc_bsg_diag_mode_enter - process preparing into device diag loopback mode
1640 * @phba: Pointer to HBA context object.
1642 * This function is responsible for preparing driver for diag loopback
1646 lpfc_bsg_diag_mode_enter(struct lpfc_hba *phba)
1648 struct lpfc_vport **vports;
1649 struct Scsi_Host *shost;
1650 struct lpfc_sli *psli;
1651 struct lpfc_queue *qp = NULL;
1652 struct lpfc_sli_ring *pring;
1660 if ((phba->link_state == LPFC_HBA_ERROR) ||
1661 (psli->sli_flag & LPFC_BLOCK_MGMT_IO) ||
1662 (!(psli->sli_flag & LPFC_SLI_ACTIVE)))
1665 vports = lpfc_create_vport_work_array(phba);
1667 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
1668 shost = lpfc_shost_from_vport(vports[i]);
1669 scsi_block_requests(shost);
1671 lpfc_destroy_vport_work_array(phba, vports);
1673 shost = lpfc_shost_from_vport(phba->pport);
1674 scsi_block_requests(shost);
1677 if (phba->sli_rev != LPFC_SLI_REV4) {
1678 pring = &psli->sli3_ring[LPFC_FCP_RING];
1679 lpfc_emptyq_wait(phba, &pring->txcmplq, &phba->hbalock);
1682 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
1684 if (!pring || (pring->ringno != LPFC_FCP_RING))
1686 if (!lpfc_emptyq_wait(phba, &pring->txcmplq,
1694 * lpfc_bsg_diag_mode_exit - exit process from device diag loopback mode
1695 * @phba: Pointer to HBA context object.
1697 * This function is responsible for driver exit processing of setting up
1698 * diag loopback mode on device.
1701 lpfc_bsg_diag_mode_exit(struct lpfc_hba *phba)
1703 struct Scsi_Host *shost;
1704 struct lpfc_vport **vports;
1707 vports = lpfc_create_vport_work_array(phba);
1709 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
1710 shost = lpfc_shost_from_vport(vports[i]);
1711 scsi_unblock_requests(shost);
1713 lpfc_destroy_vport_work_array(phba, vports);
1715 shost = lpfc_shost_from_vport(phba->pport);
1716 scsi_unblock_requests(shost);
1722 * lpfc_sli3_bsg_diag_loopback_mode - process an sli3 bsg vendor command
1723 * @phba: Pointer to HBA context object.
1724 * @job: LPFC_BSG_VENDOR_DIAG_MODE
1726 * This function is responsible for placing an sli3 port into diagnostic
1727 * loopback mode in order to perform a diagnostic loopback test.
1728 * All new scsi requests are blocked, a small delay is used to allow the
1729 * scsi requests to complete then the link is brought down. If the link is
1730 * is placed in loopback mode then scsi requests are again allowed
1731 * so the scsi mid-layer doesn't give up on the port.
1732 * All of this is done in-line.
1735 lpfc_sli3_bsg_diag_loopback_mode(struct lpfc_hba *phba, struct bsg_job *job)
1737 struct fc_bsg_request *bsg_request = job->request;
1738 struct fc_bsg_reply *bsg_reply = job->reply;
1739 struct diag_mode_set *loopback_mode;
1740 uint32_t link_flags;
1742 LPFC_MBOXQ_t *pmboxq = NULL;
1743 int mbxstatus = MBX_SUCCESS;
1747 /* no data to return just the return code */
1748 bsg_reply->reply_payload_rcv_len = 0;
1750 if (job->request_len < sizeof(struct fc_bsg_request) +
1751 sizeof(struct diag_mode_set)) {
1752 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1753 "2738 Received DIAG MODE request size:%d "
1754 "below the minimum size:%d\n",
1756 (int)(sizeof(struct fc_bsg_request) +
1757 sizeof(struct diag_mode_set)));
1762 rc = lpfc_bsg_diag_mode_enter(phba);
1766 /* bring the link to diagnostic mode */
1767 loopback_mode = (struct diag_mode_set *)
1768 bsg_request->rqst_data.h_vendor.vendor_cmd;
1769 link_flags = loopback_mode->type;
1770 timeout = loopback_mode->timeout * 100;
1772 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1775 goto loopback_mode_exit;
1777 memset((void *)pmboxq, 0, sizeof(LPFC_MBOXQ_t));
1778 pmboxq->u.mb.mbxCommand = MBX_DOWN_LINK;
1779 pmboxq->u.mb.mbxOwner = OWN_HOST;
1781 mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
1783 if ((mbxstatus == MBX_SUCCESS) && (pmboxq->u.mb.mbxStatus == 0)) {
1784 /* wait for link down before proceeding */
1786 while (phba->link_state != LPFC_LINK_DOWN) {
1787 if (i++ > timeout) {
1789 goto loopback_mode_exit;
1794 memset((void *)pmboxq, 0, sizeof(LPFC_MBOXQ_t));
1795 if (link_flags == INTERNAL_LOOP_BACK)
1796 pmboxq->u.mb.un.varInitLnk.link_flags = FLAGS_LOCAL_LB;
1798 pmboxq->u.mb.un.varInitLnk.link_flags =
1799 FLAGS_TOPOLOGY_MODE_LOOP;
1801 pmboxq->u.mb.mbxCommand = MBX_INIT_LINK;
1802 pmboxq->u.mb.mbxOwner = OWN_HOST;
1804 mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq,
1807 if ((mbxstatus != MBX_SUCCESS) || (pmboxq->u.mb.mbxStatus))
1810 spin_lock_irq(&phba->hbalock);
1811 phba->link_flag |= LS_LOOPBACK_MODE;
1812 spin_unlock_irq(&phba->hbalock);
1813 /* wait for the link attention interrupt */
1817 while (phba->link_state != LPFC_HBA_READY) {
1818 if (i++ > timeout) {
1831 lpfc_bsg_diag_mode_exit(phba);
1834 * Let SLI layer release mboxq if mbox command completed after timeout.
1836 if (pmboxq && mbxstatus != MBX_TIMEOUT)
1837 mempool_free(pmboxq, phba->mbox_mem_pool);
1840 /* make error code available to userspace */
1841 bsg_reply->result = rc;
1842 /* complete the job back to userspace if no error */
1844 bsg_job_done(job, bsg_reply->result,
1845 bsg_reply->reply_payload_rcv_len);
1850 * lpfc_sli4_bsg_set_link_diag_state - set sli4 link diag state
1851 * @phba: Pointer to HBA context object.
1852 * @diag: Flag for set link to diag or nomral operation state.
1854 * This function is responsible for issuing a sli4 mailbox command for setting
1855 * link to either diag state or normal operation state.
1858 lpfc_sli4_bsg_set_link_diag_state(struct lpfc_hba *phba, uint32_t diag)
1860 LPFC_MBOXQ_t *pmboxq;
1861 struct lpfc_mbx_set_link_diag_state *link_diag_state;
1862 uint32_t req_len, alloc_len;
1863 int mbxstatus = MBX_SUCCESS, rc;
1865 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1869 req_len = (sizeof(struct lpfc_mbx_set_link_diag_state) -
1870 sizeof(struct lpfc_sli4_cfg_mhdr));
1871 alloc_len = lpfc_sli4_config(phba, pmboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
1872 LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_STATE,
1873 req_len, LPFC_SLI4_MBX_EMBED);
1874 if (alloc_len != req_len) {
1876 goto link_diag_state_set_out;
1878 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
1879 "3128 Set link to diagnostic state:x%x (x%x/x%x)\n",
1880 diag, phba->sli4_hba.lnk_info.lnk_tp,
1881 phba->sli4_hba.lnk_info.lnk_no);
1883 link_diag_state = &pmboxq->u.mqe.un.link_diag_state;
1884 bf_set(lpfc_mbx_set_diag_state_diag_bit_valid, &link_diag_state->u.req,
1885 LPFC_DIAG_STATE_DIAG_BIT_VALID_CHANGE);
1886 bf_set(lpfc_mbx_set_diag_state_link_num, &link_diag_state->u.req,
1887 phba->sli4_hba.lnk_info.lnk_no);
1888 bf_set(lpfc_mbx_set_diag_state_link_type, &link_diag_state->u.req,
1889 phba->sli4_hba.lnk_info.lnk_tp);
1891 bf_set(lpfc_mbx_set_diag_state_diag,
1892 &link_diag_state->u.req, 1);
1894 bf_set(lpfc_mbx_set_diag_state_diag,
1895 &link_diag_state->u.req, 0);
1897 mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
1899 if ((mbxstatus == MBX_SUCCESS) && (pmboxq->u.mb.mbxStatus == 0))
1904 link_diag_state_set_out:
1905 if (pmboxq && (mbxstatus != MBX_TIMEOUT))
1906 mempool_free(pmboxq, phba->mbox_mem_pool);
1912 * lpfc_sli4_bsg_set_loopback_mode - set sli4 internal loopback diagnostic
1913 * @phba: Pointer to HBA context object.
1914 * @mode: loopback mode to set
1915 * @link_no: link number for loopback mode to set
1917 * This function is responsible for issuing a sli4 mailbox command for setting
1918 * up loopback diagnostic for a link.
1921 lpfc_sli4_bsg_set_loopback_mode(struct lpfc_hba *phba, int mode,
1924 LPFC_MBOXQ_t *pmboxq;
1925 uint32_t req_len, alloc_len;
1926 struct lpfc_mbx_set_link_diag_loopback *link_diag_loopback;
1927 int mbxstatus = MBX_SUCCESS, rc = 0;
1929 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1932 req_len = (sizeof(struct lpfc_mbx_set_link_diag_loopback) -
1933 sizeof(struct lpfc_sli4_cfg_mhdr));
1934 alloc_len = lpfc_sli4_config(phba, pmboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
1935 LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_LOOPBACK,
1936 req_len, LPFC_SLI4_MBX_EMBED);
1937 if (alloc_len != req_len) {
1938 mempool_free(pmboxq, phba->mbox_mem_pool);
1941 link_diag_loopback = &pmboxq->u.mqe.un.link_diag_loopback;
1942 bf_set(lpfc_mbx_set_diag_state_link_num,
1943 &link_diag_loopback->u.req, link_no);
1945 if (phba->sli4_hba.conf_trunk & (1 << link_no)) {
1946 bf_set(lpfc_mbx_set_diag_state_link_type,
1947 &link_diag_loopback->u.req, LPFC_LNK_FC_TRUNKED);
1949 bf_set(lpfc_mbx_set_diag_state_link_type,
1950 &link_diag_loopback->u.req,
1951 phba->sli4_hba.lnk_info.lnk_tp);
1954 bf_set(lpfc_mbx_set_diag_lpbk_type, &link_diag_loopback->u.req,
1957 mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
1958 if ((mbxstatus != MBX_SUCCESS) || (pmboxq->u.mb.mbxStatus)) {
1959 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1960 "3127 Failed setup loopback mode mailbox "
1961 "command, rc:x%x, status:x%x\n", mbxstatus,
1962 pmboxq->u.mb.mbxStatus);
1965 if (pmboxq && (mbxstatus != MBX_TIMEOUT))
1966 mempool_free(pmboxq, phba->mbox_mem_pool);
1971 * lpfc_sli4_diag_fcport_reg_setup - setup port registrations for diagnostic
1972 * @phba: Pointer to HBA context object.
1974 * This function set up SLI4 FC port registrations for diagnostic run, which
1975 * includes all the rpis, vfi, and also vpi.
1978 lpfc_sli4_diag_fcport_reg_setup(struct lpfc_hba *phba)
1980 if (test_bit(FC_VFI_REGISTERED, &phba->pport->fc_flag)) {
1981 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1982 "3136 Port still had vfi registered: "
1983 "mydid:x%x, fcfi:%d, vfi:%d, vpi:%d\n",
1984 phba->pport->fc_myDID, phba->fcf.fcfi,
1985 phba->sli4_hba.vfi_ids[phba->pport->vfi],
1986 phba->vpi_ids[phba->pport->vpi]);
1989 return lpfc_issue_reg_vfi(phba->pport);
1993 * lpfc_sli4_bsg_diag_loopback_mode - process an sli4 bsg vendor command
1994 * @phba: Pointer to HBA context object.
1995 * @job: LPFC_BSG_VENDOR_DIAG_MODE
1997 * This function is responsible for placing an sli4 port into diagnostic
1998 * loopback mode in order to perform a diagnostic loopback test.
2001 lpfc_sli4_bsg_diag_loopback_mode(struct lpfc_hba *phba, struct bsg_job *job)
2003 struct fc_bsg_request *bsg_request = job->request;
2004 struct fc_bsg_reply *bsg_reply = job->reply;
2005 struct diag_mode_set *loopback_mode;
2006 uint32_t link_flags, timeout, link_no;
2009 /* no data to return just the return code */
2010 bsg_reply->reply_payload_rcv_len = 0;
2012 if (job->request_len < sizeof(struct fc_bsg_request) +
2013 sizeof(struct diag_mode_set)) {
2014 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2015 "3011 Received DIAG MODE request size:%d "
2016 "below the minimum size:%d\n",
2018 (int)(sizeof(struct fc_bsg_request) +
2019 sizeof(struct diag_mode_set)));
2024 loopback_mode = (struct diag_mode_set *)
2025 bsg_request->rqst_data.h_vendor.vendor_cmd;
2026 link_flags = loopback_mode->type;
2027 timeout = loopback_mode->timeout * 100;
2029 if (loopback_mode->physical_link == -1)
2030 link_no = phba->sli4_hba.lnk_info.lnk_no;
2032 link_no = loopback_mode->physical_link;
2034 if (link_flags == DISABLE_LOOP_BACK) {
2035 rc = lpfc_sli4_bsg_set_loopback_mode(phba,
2036 LPFC_DIAG_LOOPBACK_TYPE_DISABLE,
2039 /* Unset the need disable bit */
2040 phba->sli4_hba.conf_trunk &= ~((1 << link_no) << 4);
2044 /* Check if we need to disable the loopback state */
2045 if (phba->sli4_hba.conf_trunk & ((1 << link_no) << 4)) {
2051 rc = lpfc_bsg_diag_mode_enter(phba);
2055 /* indicate we are in loobpack diagnostic mode */
2056 spin_lock_irq(&phba->hbalock);
2057 phba->link_flag |= LS_LOOPBACK_MODE;
2058 spin_unlock_irq(&phba->hbalock);
2060 /* reset port to start frome scratch */
2061 rc = lpfc_selective_reset(phba);
2065 /* bring the link to diagnostic mode */
2066 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
2067 "3129 Bring link to diagnostic state.\n");
2069 rc = lpfc_sli4_bsg_set_link_diag_state(phba, 1);
2071 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2072 "3130 Failed to bring link to diagnostic "
2073 "state, rc:x%x\n", rc);
2074 goto loopback_mode_exit;
2077 /* wait for link down before proceeding */
2079 while (phba->link_state != LPFC_LINK_DOWN) {
2080 if (i++ > timeout) {
2082 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
2083 "3131 Timeout waiting for link to "
2084 "diagnostic mode, timeout:%d ms\n",
2086 goto loopback_mode_exit;
2091 /* set up loopback mode */
2092 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
2093 "3132 Set up loopback mode:x%x\n", link_flags);
2095 switch (link_flags) {
2096 case INTERNAL_LOOP_BACK:
2097 if (phba->sli4_hba.conf_trunk & (1 << link_no)) {
2098 rc = lpfc_sli4_bsg_set_loopback_mode(phba,
2099 LPFC_DIAG_LOOPBACK_TYPE_INTERNAL,
2102 /* Trunk is configured, but link is not in this trunk */
2103 if (phba->sli4_hba.conf_trunk) {
2105 goto loopback_mode_exit;
2108 rc = lpfc_sli4_bsg_set_loopback_mode(phba,
2109 LPFC_DIAG_LOOPBACK_TYPE_INTERNAL,
2114 /* Set the need disable bit */
2115 phba->sli4_hba.conf_trunk |= (1 << link_no) << 4;
2119 case EXTERNAL_LOOP_BACK:
2120 if (phba->sli4_hba.conf_trunk & (1 << link_no)) {
2121 rc = lpfc_sli4_bsg_set_loopback_mode(phba,
2122 LPFC_DIAG_LOOPBACK_TYPE_EXTERNAL_TRUNKED,
2125 /* Trunk is configured, but link is not in this trunk */
2126 if (phba->sli4_hba.conf_trunk) {
2128 goto loopback_mode_exit;
2131 rc = lpfc_sli4_bsg_set_loopback_mode(phba,
2132 LPFC_DIAG_LOOPBACK_TYPE_SERDES,
2137 /* Set the need disable bit */
2138 phba->sli4_hba.conf_trunk |= (1 << link_no) << 4;
2144 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
2145 "3141 Loopback mode:x%x not supported\n",
2147 goto loopback_mode_exit;
2151 /* wait for the link attention interrupt */
2154 while (phba->link_state < LPFC_LINK_UP) {
2155 if (i++ > timeout) {
2157 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
2158 "3137 Timeout waiting for link up "
2159 "in loopback mode, timeout:%d ms\n",
2167 /* port resource registration setup for loopback diagnostic */
2169 /* set up a none zero myDID for loopback test */
2170 phba->pport->fc_myDID = 1;
2171 rc = lpfc_sli4_diag_fcport_reg_setup(phba);
2173 goto loopback_mode_exit;
2176 /* wait for the port ready */
2179 while (phba->link_state != LPFC_HBA_READY) {
2180 if (i++ > timeout) {
2182 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
2183 "3133 Timeout waiting for port "
2184 "loopback mode ready, timeout:%d ms\n",
2193 /* clear loopback diagnostic mode */
2195 spin_lock_irq(&phba->hbalock);
2196 phba->link_flag &= ~LS_LOOPBACK_MODE;
2197 spin_unlock_irq(&phba->hbalock);
2199 lpfc_bsg_diag_mode_exit(phba);
2202 /* make error code available to userspace */
2203 bsg_reply->result = rc;
2204 /* complete the job back to userspace if no error */
2206 bsg_job_done(job, bsg_reply->result,
2207 bsg_reply->reply_payload_rcv_len);
2212 * lpfc_bsg_diag_loopback_mode - bsg vendor command for diag loopback mode
2213 * @job: LPFC_BSG_VENDOR_DIAG_MODE
2215 * This function is responsible for responding to check and dispatch bsg diag
2216 * command from the user to proper driver action routines.
2219 lpfc_bsg_diag_loopback_mode(struct bsg_job *job)
2221 struct Scsi_Host *shost;
2222 struct lpfc_vport *vport;
2223 struct lpfc_hba *phba;
2226 shost = fc_bsg_to_shost(job);
2229 vport = shost_priv(shost);
2236 if (phba->sli_rev < LPFC_SLI_REV4)
2237 rc = lpfc_sli3_bsg_diag_loopback_mode(phba, job);
2238 else if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) >=
2239 LPFC_SLI_INTF_IF_TYPE_2)
2240 rc = lpfc_sli4_bsg_diag_loopback_mode(phba, job);
2248 * lpfc_sli4_bsg_diag_mode_end - sli4 bsg vendor command for ending diag mode
2249 * @job: LPFC_BSG_VENDOR_DIAG_MODE_END
2251 * This function is responsible for responding to check and dispatch bsg diag
2252 * command from the user to proper driver action routines.
2255 lpfc_sli4_bsg_diag_mode_end(struct bsg_job *job)
2257 struct fc_bsg_request *bsg_request = job->request;
2258 struct fc_bsg_reply *bsg_reply = job->reply;
2259 struct Scsi_Host *shost;
2260 struct lpfc_vport *vport;
2261 struct lpfc_hba *phba;
2262 struct diag_mode_set *loopback_mode_end_cmd;
2266 shost = fc_bsg_to_shost(job);
2269 vport = shost_priv(shost);
2276 if (phba->sli_rev < LPFC_SLI_REV4)
2278 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
2279 LPFC_SLI_INTF_IF_TYPE_2)
2282 /* clear loopback diagnostic mode */
2283 spin_lock_irq(&phba->hbalock);
2284 phba->link_flag &= ~LS_LOOPBACK_MODE;
2285 spin_unlock_irq(&phba->hbalock);
2286 loopback_mode_end_cmd = (struct diag_mode_set *)
2287 bsg_request->rqst_data.h_vendor.vendor_cmd;
2288 timeout = loopback_mode_end_cmd->timeout * 100;
2290 rc = lpfc_sli4_bsg_set_link_diag_state(phba, 0);
2292 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2293 "3139 Failed to bring link to diagnostic "
2294 "state, rc:x%x\n", rc);
2295 goto loopback_mode_end_exit;
2298 /* wait for link down before proceeding */
2300 while (phba->link_state != LPFC_LINK_DOWN) {
2301 if (i++ > timeout) {
2302 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
2303 "3140 Timeout waiting for link to "
2304 "diagnostic mode_end, timeout:%d ms\n",
2306 /* there is nothing much we can do here */
2312 /* reset port resource registrations */
2313 rc = lpfc_selective_reset(phba);
2314 phba->pport->fc_myDID = 0;
2316 loopback_mode_end_exit:
2317 /* make return code available to userspace */
2318 bsg_reply->result = rc;
2319 /* complete the job back to userspace if no error */
2321 bsg_job_done(job, bsg_reply->result,
2322 bsg_reply->reply_payload_rcv_len);
2327 * lpfc_sli4_bsg_link_diag_test - sli4 bsg vendor command for diag link test
2328 * @job: LPFC_BSG_VENDOR_DIAG_LINK_TEST
2330 * This function is to perform SLI4 diag link test request from the user
2334 lpfc_sli4_bsg_link_diag_test(struct bsg_job *job)
2336 struct fc_bsg_request *bsg_request = job->request;
2337 struct fc_bsg_reply *bsg_reply = job->reply;
2338 struct Scsi_Host *shost;
2339 struct lpfc_vport *vport;
2340 struct lpfc_hba *phba;
2341 LPFC_MBOXQ_t *pmboxq;
2342 struct sli4_link_diag *link_diag_test_cmd;
2343 uint32_t req_len, alloc_len;
2344 struct lpfc_mbx_run_link_diag_test *run_link_diag_test;
2345 union lpfc_sli4_cfg_shdr *shdr;
2346 uint32_t shdr_status, shdr_add_status;
2347 struct diag_status *diag_status_reply;
2348 int mbxstatus, rc = -ENODEV, rc1 = 0;
2350 shost = fc_bsg_to_shost(job);
2354 vport = shost_priv(shost);
2363 if (phba->sli_rev < LPFC_SLI_REV4)
2366 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
2367 LPFC_SLI_INTF_IF_TYPE_2)
2370 if (job->request_len < sizeof(struct fc_bsg_request) +
2371 sizeof(struct sli4_link_diag)) {
2372 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2373 "3013 Received LINK DIAG TEST request "
2374 " size:%d below the minimum size:%d\n",
2376 (int)(sizeof(struct fc_bsg_request) +
2377 sizeof(struct sli4_link_diag)));
2382 rc = lpfc_bsg_diag_mode_enter(phba);
2386 link_diag_test_cmd = (struct sli4_link_diag *)
2387 bsg_request->rqst_data.h_vendor.vendor_cmd;
2389 rc = lpfc_sli4_bsg_set_link_diag_state(phba, 1);
2394 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2397 goto link_diag_test_exit;
2400 req_len = (sizeof(struct lpfc_mbx_set_link_diag_state) -
2401 sizeof(struct lpfc_sli4_cfg_mhdr));
2402 alloc_len = lpfc_sli4_config(phba, pmboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
2403 LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_STATE,
2404 req_len, LPFC_SLI4_MBX_EMBED);
2405 if (alloc_len != req_len) {
2407 goto link_diag_test_exit;
2410 run_link_diag_test = &pmboxq->u.mqe.un.link_diag_test;
2411 bf_set(lpfc_mbx_run_diag_test_link_num, &run_link_diag_test->u.req,
2412 phba->sli4_hba.lnk_info.lnk_no);
2413 bf_set(lpfc_mbx_run_diag_test_link_type, &run_link_diag_test->u.req,
2414 phba->sli4_hba.lnk_info.lnk_tp);
2415 bf_set(lpfc_mbx_run_diag_test_test_id, &run_link_diag_test->u.req,
2416 link_diag_test_cmd->test_id);
2417 bf_set(lpfc_mbx_run_diag_test_loops, &run_link_diag_test->u.req,
2418 link_diag_test_cmd->loops);
2419 bf_set(lpfc_mbx_run_diag_test_test_ver, &run_link_diag_test->u.req,
2420 link_diag_test_cmd->test_version);
2421 bf_set(lpfc_mbx_run_diag_test_err_act, &run_link_diag_test->u.req,
2422 link_diag_test_cmd->error_action);
2424 mbxstatus = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
2426 shdr = (union lpfc_sli4_cfg_shdr *)
2427 &pmboxq->u.mqe.un.sli4_config.header.cfg_shdr;
2428 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
2429 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
2430 if (shdr_status || shdr_add_status || mbxstatus) {
2431 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
2432 "3010 Run link diag test mailbox failed with "
2433 "mbx_status x%x status x%x, add_status x%x\n",
2434 mbxstatus, shdr_status, shdr_add_status);
2437 diag_status_reply = (struct diag_status *)
2438 bsg_reply->reply_data.vendor_reply.vendor_rsp;
2440 if (job->reply_len < sizeof(*bsg_reply) + sizeof(*diag_status_reply)) {
2441 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2442 "3012 Received Run link diag test reply "
2443 "below minimum size (%d): reply_len:%d\n",
2444 (int)(sizeof(*bsg_reply) +
2445 sizeof(*diag_status_reply)),
2451 diag_status_reply->mbox_status = mbxstatus;
2452 diag_status_reply->shdr_status = shdr_status;
2453 diag_status_reply->shdr_add_status = shdr_add_status;
2455 link_diag_test_exit:
2456 rc1 = lpfc_sli4_bsg_set_link_diag_state(phba, 0);
2459 mempool_free(pmboxq, phba->mbox_mem_pool);
2461 lpfc_bsg_diag_mode_exit(phba);
2464 /* make error code available to userspace */
2467 bsg_reply->result = rc;
2468 /* complete the job back to userspace if no error */
2470 bsg_job_done(job, bsg_reply->result,
2471 bsg_reply->reply_payload_rcv_len);
2476 * lpfcdiag_loop_self_reg - obtains a remote port login id
2477 * @phba: Pointer to HBA context object
2478 * @rpi: Pointer to a remote port login id
2480 * This function obtains a remote port login id so the diag loopback test
2481 * can send and receive its own unsolicited CT command.
2483 static int lpfcdiag_loop_self_reg(struct lpfc_hba *phba, uint16_t *rpi)
2486 struct lpfc_dmabuf *dmabuff;
2489 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2493 if (phba->sli_rev < LPFC_SLI_REV4)
2494 status = lpfc_reg_rpi(phba, 0, phba->pport->fc_myDID,
2495 (uint8_t *)&phba->pport->fc_sparam,
2498 *rpi = lpfc_sli4_alloc_rpi(phba);
2499 if (*rpi == LPFC_RPI_ALLOC_ERROR) {
2500 mempool_free(mbox, phba->mbox_mem_pool);
2503 status = lpfc_reg_rpi(phba, phba->pport->vpi,
2504 phba->pport->fc_myDID,
2505 (uint8_t *)&phba->pport->fc_sparam,
2510 mempool_free(mbox, phba->mbox_mem_pool);
2511 if (phba->sli_rev == LPFC_SLI_REV4)
2512 lpfc_sli4_free_rpi(phba, *rpi);
2516 dmabuff = (struct lpfc_dmabuf *)mbox->ctx_buf;
2517 mbox->ctx_buf = NULL;
2518 mbox->ctx_ndlp = NULL;
2519 status = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
2521 if ((status != MBX_SUCCESS) || (mbox->u.mb.mbxStatus)) {
2522 lpfc_mbuf_free(phba, dmabuff->virt, dmabuff->phys);
2524 if (status != MBX_TIMEOUT)
2525 mempool_free(mbox, phba->mbox_mem_pool);
2526 if (phba->sli_rev == LPFC_SLI_REV4)
2527 lpfc_sli4_free_rpi(phba, *rpi);
2531 if (phba->sli_rev < LPFC_SLI_REV4)
2532 *rpi = mbox->u.mb.un.varWords[0];
2534 lpfc_mbuf_free(phba, dmabuff->virt, dmabuff->phys);
2536 mempool_free(mbox, phba->mbox_mem_pool);
2541 * lpfcdiag_loop_self_unreg - unregs from the rpi
2542 * @phba: Pointer to HBA context object
2543 * @rpi: Remote port login id
2545 * This function unregisters the rpi obtained in lpfcdiag_loop_self_reg
2547 static int lpfcdiag_loop_self_unreg(struct lpfc_hba *phba, uint16_t rpi)
2552 /* Allocate mboxq structure */
2553 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2557 if (phba->sli_rev < LPFC_SLI_REV4)
2558 lpfc_unreg_login(phba, 0, rpi, mbox);
2560 lpfc_unreg_login(phba, phba->pport->vpi,
2561 phba->sli4_hba.rpi_ids[rpi], mbox);
2563 status = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
2565 if ((status != MBX_SUCCESS) || (mbox->u.mb.mbxStatus)) {
2566 if (status != MBX_TIMEOUT)
2567 mempool_free(mbox, phba->mbox_mem_pool);
2570 mempool_free(mbox, phba->mbox_mem_pool);
2571 if (phba->sli_rev == LPFC_SLI_REV4)
2572 lpfc_sli4_free_rpi(phba, rpi);
2577 * lpfcdiag_loop_get_xri - obtains the transmit and receive ids
2578 * @phba: Pointer to HBA context object
2579 * @rpi: Remote port login id
2580 * @txxri: Pointer to transmit exchange id
2581 * @rxxri: Pointer to response exchabge id
2583 * This function obtains the transmit and receive ids required to send
2584 * an unsolicited ct command with a payload. A special lpfc FsType and CmdRsp
2585 * flags are used to the unsolicited response handler is able to process
2586 * the ct command sent on the same port.
2588 static int lpfcdiag_loop_get_xri(struct lpfc_hba *phba, uint16_t rpi,
2589 uint16_t *txxri, uint16_t * rxxri)
2591 struct lpfc_bsg_event *evt;
2592 struct lpfc_iocbq *cmdiocbq, *rspiocbq;
2593 struct lpfc_dmabuf *dmabuf;
2594 struct ulp_bde64 *bpl = NULL;
2595 struct lpfc_sli_ct_request *ctreq = NULL;
2598 int iocb_stat = IOCB_SUCCESS;
2599 unsigned long flags;
2604 evt = lpfc_bsg_event_new(FC_REG_CT_EVENT, current->pid,
2605 SLI_CT_ELX_LOOPBACK);
2609 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2610 list_add(&evt->node, &phba->ct_ev_waiters);
2611 lpfc_bsg_event_ref(evt);
2612 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2614 cmdiocbq = lpfc_sli_get_iocbq(phba);
2615 rspiocbq = lpfc_sli_get_iocbq(phba);
2617 dmabuf = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2619 dmabuf->virt = lpfc_mbuf_alloc(phba, 0, &dmabuf->phys);
2621 INIT_LIST_HEAD(&dmabuf->list);
2622 bpl = (struct ulp_bde64 *) dmabuf->virt;
2623 memset(bpl, 0, sizeof(*bpl));
2624 ctreq = (struct lpfc_sli_ct_request *)(bpl + 1);
2626 le32_to_cpu(putPaddrHigh(dmabuf->phys +
2629 le32_to_cpu(putPaddrLow(dmabuf->phys +
2631 bpl->tus.f.bdeFlags = 0;
2632 bpl->tus.f.bdeSize = ELX_LOOPBACK_HEADER_SZ;
2633 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2637 if (cmdiocbq == NULL || rspiocbq == NULL ||
2638 dmabuf == NULL || bpl == NULL || ctreq == NULL ||
2639 dmabuf->virt == NULL) {
2641 goto err_get_xri_exit;
2644 memset(ctreq, 0, ELX_LOOPBACK_HEADER_SZ);
2646 ctreq->RevisionId.bits.Revision = SLI_CT_REVISION;
2647 ctreq->RevisionId.bits.InId = 0;
2648 ctreq->FsType = SLI_CT_ELX_LOOPBACK;
2649 ctreq->FsSubType = 0;
2650 ctreq->CommandResponse.bits.CmdRsp = ELX_LOOPBACK_XRI_SETUP;
2651 ctreq->CommandResponse.bits.Size = 0;
2653 cmdiocbq->bpl_dmabuf = dmabuf;
2654 cmdiocbq->cmd_flag |= LPFC_IO_LIBDFC;
2655 cmdiocbq->vport = phba->pport;
2656 cmdiocbq->cmd_cmpl = NULL;
2658 lpfc_sli_prep_xmit_seq64(phba, cmdiocbq, dmabuf, rpi, 0, 1,
2659 FC_RCTL_DD_SOL_CTL, 0, CMD_XMIT_SEQUENCE64_CR);
2661 iocb_stat = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq,
2662 rspiocbq, (phba->fc_ratov * 2)
2663 + LPFC_DRVR_TIMEOUT);
2665 status = get_job_ulpstatus(phba, rspiocbq);
2666 if (iocb_stat != IOCB_SUCCESS || status != IOCB_SUCCESS) {
2668 goto err_get_xri_exit;
2670 *txxri = get_job_ulpcontext(phba, rspiocbq);
2673 evt->wait_time_stamp = jiffies;
2674 time_left = wait_event_interruptible_timeout(
2675 evt->wq, !list_empty(&evt->events_to_see),
2676 msecs_to_jiffies(1000 *
2677 ((phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT)));
2678 if (list_empty(&evt->events_to_see))
2679 ret_val = (time_left) ? -EINTR : -ETIMEDOUT;
2681 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2682 list_move(evt->events_to_see.prev, &evt->events_to_get);
2683 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2684 *rxxri = (list_entry(evt->events_to_get.prev,
2685 typeof(struct event_data),
2691 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2692 lpfc_bsg_event_unref(evt); /* release ref */
2693 lpfc_bsg_event_unref(evt); /* delete */
2694 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2698 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
2702 if (cmdiocbq && (iocb_stat != IOCB_TIMEDOUT))
2703 lpfc_sli_release_iocbq(phba, cmdiocbq);
2705 lpfc_sli_release_iocbq(phba, rspiocbq);
2710 * lpfc_bsg_dma_page_alloc - allocate a bsg mbox page sized dma buffers
2711 * @phba: Pointer to HBA context object
2713 * This function allocates BSG_MBOX_SIZE (4KB) page size dma buffer and
2714 * returns the pointer to the buffer.
2716 static struct lpfc_dmabuf *
2717 lpfc_bsg_dma_page_alloc(struct lpfc_hba *phba)
2719 struct lpfc_dmabuf *dmabuf;
2720 struct pci_dev *pcidev = phba->pcidev;
2722 /* allocate dma buffer struct */
2723 dmabuf = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2727 INIT_LIST_HEAD(&dmabuf->list);
2729 /* now, allocate dma buffer */
2730 dmabuf->virt = dma_alloc_coherent(&pcidev->dev, BSG_MBOX_SIZE,
2731 &(dmabuf->phys), GFP_KERNEL);
2733 if (!dmabuf->virt) {
2742 * lpfc_bsg_dma_page_free - free a bsg mbox page sized dma buffer
2743 * @phba: Pointer to HBA context object.
2744 * @dmabuf: Pointer to the bsg mbox page sized dma buffer descriptor.
2746 * This routine just simply frees a dma buffer and its associated buffer
2747 * descriptor referred by @dmabuf.
2750 lpfc_bsg_dma_page_free(struct lpfc_hba *phba, struct lpfc_dmabuf *dmabuf)
2752 struct pci_dev *pcidev = phba->pcidev;
2758 dma_free_coherent(&pcidev->dev, BSG_MBOX_SIZE,
2759 dmabuf->virt, dmabuf->phys);
2765 * lpfc_bsg_dma_page_list_free - free a list of bsg mbox page sized dma buffers
2766 * @phba: Pointer to HBA context object.
2767 * @dmabuf_list: Pointer to a list of bsg mbox page sized dma buffer descs.
2769 * This routine just simply frees all dma buffers and their associated buffer
2770 * descriptors referred by @dmabuf_list.
2773 lpfc_bsg_dma_page_list_free(struct lpfc_hba *phba,
2774 struct list_head *dmabuf_list)
2776 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
2778 if (list_empty(dmabuf_list))
2781 list_for_each_entry_safe(dmabuf, next_dmabuf, dmabuf_list, list) {
2782 list_del_init(&dmabuf->list);
2783 lpfc_bsg_dma_page_free(phba, dmabuf);
2789 * diag_cmd_data_alloc - fills in a bde struct with dma buffers
2790 * @phba: Pointer to HBA context object
2791 * @bpl: Pointer to 64 bit bde structure
2792 * @size: Number of bytes to process
2793 * @nocopydata: Flag to copy user data into the allocated buffer
2795 * This function allocates page size buffers and populates an lpfc_dmabufext.
2796 * If allowed the user data pointed to with indataptr is copied into the kernel
2797 * memory. The chained list of page size buffers is returned.
2799 static struct lpfc_dmabufext *
2800 diag_cmd_data_alloc(struct lpfc_hba *phba,
2801 struct ulp_bde64 *bpl, uint32_t size,
2804 struct lpfc_dmabufext *mlist = NULL;
2805 struct lpfc_dmabufext *dmp;
2806 int cnt, offset = 0, i = 0;
2807 struct pci_dev *pcidev;
2809 pcidev = phba->pcidev;
2812 /* We get chunks of 4K */
2813 if (size > BUF_SZ_4K)
2818 /* allocate struct lpfc_dmabufext buffer header */
2819 dmp = kmalloc(sizeof(struct lpfc_dmabufext), GFP_KERNEL);
2823 INIT_LIST_HEAD(&dmp->dma.list);
2825 /* Queue it to a linked list */
2827 list_add_tail(&dmp->dma.list, &mlist->dma.list);
2831 /* allocate buffer */
2832 dmp->dma.virt = dma_alloc_coherent(&pcidev->dev,
2843 bpl->tus.f.bdeFlags = 0;
2845 memset((uint8_t *)dmp->dma.virt, 0, cnt);
2846 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2849 /* build buffer ptr list for IOCB */
2850 bpl->addrLow = le32_to_cpu(putPaddrLow(dmp->dma.phys));
2851 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dmp->dma.phys));
2852 bpl->tus.f.bdeSize = (ushort) cnt;
2853 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2866 diag_cmd_data_free(phba, mlist);
2871 * lpfcdiag_sli3_loop_post_rxbufs - post the receive buffers for an unsol CT cmd
2872 * @phba: Pointer to HBA context object
2873 * @rxxri: Receive exchange id
2874 * @len: Number of data bytes
2876 * This function allocates and posts a data buffer of sufficient size to receive
2877 * an unsolicited CT command.
2879 static int lpfcdiag_sli3_loop_post_rxbufs(struct lpfc_hba *phba, uint16_t rxxri,
2882 struct lpfc_sli_ring *pring;
2883 struct lpfc_iocbq *cmdiocbq;
2885 struct list_head head, *curr, *next;
2886 struct lpfc_dmabuf *rxbmp;
2887 struct lpfc_dmabuf *dmp;
2888 struct lpfc_dmabuf *mp[2] = {NULL, NULL};
2889 struct ulp_bde64 *rxbpl = NULL;
2891 struct lpfc_dmabufext *rxbuffer = NULL;
2896 pring = lpfc_phba_elsring(phba);
2898 cmdiocbq = lpfc_sli_get_iocbq(phba);
2899 rxbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2900 if (rxbmp != NULL) {
2901 rxbmp->virt = lpfc_mbuf_alloc(phba, 0, &rxbmp->phys);
2903 INIT_LIST_HEAD(&rxbmp->list);
2904 rxbpl = (struct ulp_bde64 *) rxbmp->virt;
2905 rxbuffer = diag_cmd_data_alloc(phba, rxbpl, len, 0);
2909 if (!cmdiocbq || !rxbmp || !rxbpl || !rxbuffer || !pring) {
2911 goto err_post_rxbufs_exit;
2914 /* Queue buffers for the receive exchange */
2915 num_bde = (uint32_t)rxbuffer->flag;
2916 dmp = &rxbuffer->dma;
2917 cmd = &cmdiocbq->iocb;
2920 INIT_LIST_HEAD(&head);
2921 list_add_tail(&head, &dmp->list);
2922 list_for_each_safe(curr, next, &head) {
2923 mp[i] = list_entry(curr, struct lpfc_dmabuf, list);
2926 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
2927 mp[i]->buffer_tag = lpfc_sli_get_buffer_tag(phba);
2928 cmd->un.quexri64cx.buff.bde.addrHigh =
2929 putPaddrHigh(mp[i]->phys);
2930 cmd->un.quexri64cx.buff.bde.addrLow =
2931 putPaddrLow(mp[i]->phys);
2932 cmd->un.quexri64cx.buff.bde.tus.f.bdeSize =
2933 ((struct lpfc_dmabufext *)mp[i])->size;
2934 cmd->un.quexri64cx.buff.buffer_tag = mp[i]->buffer_tag;
2935 cmd->ulpCommand = CMD_QUE_XRI64_CX;
2938 cmd->ulpBdeCount = 1;
2939 cmd->unsli3.que_xri64cx_ext_words.ebde_count = 0;
2942 cmd->un.cont64[i].addrHigh = putPaddrHigh(mp[i]->phys);
2943 cmd->un.cont64[i].addrLow = putPaddrLow(mp[i]->phys);
2944 cmd->un.cont64[i].tus.f.bdeSize =
2945 ((struct lpfc_dmabufext *)mp[i])->size;
2946 cmd->ulpBdeCount = ++i;
2948 if ((--num_bde > 0) && (i < 2))
2951 cmd->ulpCommand = CMD_QUE_XRI_BUF64_CX;
2955 cmd->ulpClass = CLASS3;
2956 cmd->ulpContext = rxxri;
2958 iocb_stat = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq,
2960 if (iocb_stat == IOCB_ERROR) {
2961 diag_cmd_data_free(phba,
2962 (struct lpfc_dmabufext *)mp[0]);
2964 diag_cmd_data_free(phba,
2965 (struct lpfc_dmabufext *)mp[1]);
2966 dmp = list_entry(next, struct lpfc_dmabuf, list);
2968 goto err_post_rxbufs_exit;
2971 lpfc_sli_ringpostbuf_put(phba, pring, mp[0]);
2973 lpfc_sli_ringpostbuf_put(phba, pring, mp[1]);
2977 /* The iocb was freed by lpfc_sli_issue_iocb */
2978 cmdiocbq = lpfc_sli_get_iocbq(phba);
2980 dmp = list_entry(next, struct lpfc_dmabuf, list);
2982 goto err_post_rxbufs_exit;
2984 cmd = &cmdiocbq->iocb;
2989 err_post_rxbufs_exit:
2993 lpfc_mbuf_free(phba, rxbmp->virt, rxbmp->phys);
2998 lpfc_sli_release_iocbq(phba, cmdiocbq);
3003 * lpfc_bsg_diag_loopback_run - run loopback on a port by issue ct cmd to itself
3004 * @job: LPFC_BSG_VENDOR_DIAG_TEST fc_bsg_job
3006 * This function receives a user data buffer to be transmitted and received on
3007 * the same port, the link must be up and in loopback mode prior
3009 * 1. A kernel buffer is allocated to copy the user data into.
3010 * 2. The port registers with "itself".
3011 * 3. The transmit and receive exchange ids are obtained.
3012 * 4. The receive exchange id is posted.
3013 * 5. A new els loopback event is created.
3014 * 6. The command and response iocbs are allocated.
3015 * 7. The cmd iocb FsType is set to elx loopback and the CmdRsp to looppback.
3017 * This function is meant to be called n times while the port is in loopback
3018 * so it is the apps responsibility to issue a reset to take the port out
3022 lpfc_bsg_diag_loopback_run(struct bsg_job *job)
3024 struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
3025 struct fc_bsg_reply *bsg_reply = job->reply;
3026 struct lpfc_hba *phba = vport->phba;
3027 struct lpfc_bsg_event *evt;
3028 struct event_data *evdat;
3029 struct lpfc_sli *psli = &phba->sli;
3032 size_t segment_len = 0, segment_offset = 0, current_offset = 0;
3034 struct lpfc_iocbq *cmdiocbq, *rspiocbq = NULL;
3035 union lpfc_wqe128 *cmdwqe, *rspwqe;
3036 struct lpfc_sli_ct_request *ctreq;
3037 struct lpfc_dmabuf *txbmp;
3038 struct ulp_bde64 *txbpl = NULL;
3039 struct lpfc_dmabufext *txbuffer = NULL;
3040 struct list_head head;
3041 struct lpfc_dmabuf *curr;
3042 uint16_t txxri = 0, rxxri;
3044 uint8_t *ptr = NULL, *rx_databuf = NULL;
3047 int iocb_stat = IOCB_SUCCESS;
3048 unsigned long flags;
3049 void *dataout = NULL;
3052 /* in case no data is returned return just the return code */
3053 bsg_reply->reply_payload_rcv_len = 0;
3055 if (job->request_len <
3056 sizeof(struct fc_bsg_request) + sizeof(struct diag_mode_test)) {
3057 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3058 "2739 Received DIAG TEST request below minimum "
3061 goto loopback_test_exit;
3064 if (job->request_payload.payload_len !=
3065 job->reply_payload.payload_len) {
3067 goto loopback_test_exit;
3070 if ((phba->link_state == LPFC_HBA_ERROR) ||
3071 (psli->sli_flag & LPFC_BLOCK_MGMT_IO) ||
3072 (!(psli->sli_flag & LPFC_SLI_ACTIVE))) {
3074 goto loopback_test_exit;
3077 if (!lpfc_is_link_up(phba) || !(phba->link_flag & LS_LOOPBACK_MODE)) {
3079 goto loopback_test_exit;
3082 size = job->request_payload.payload_len;
3083 full_size = size + ELX_LOOPBACK_HEADER_SZ; /* plus the header */
3085 if ((size == 0) || (size > 80 * BUF_SZ_4K)) {
3087 goto loopback_test_exit;
3090 if (full_size >= BUF_SZ_4K) {
3092 * Allocate memory for ioctl data. If buffer is bigger than 64k,
3093 * then we allocate 64k and re-use that buffer over and over to
3094 * xfer the whole block. This is because Linux kernel has a
3095 * problem allocating more than 120k of kernel space memory. Saw
3096 * problem with GET_FCPTARGETMAPPING...
3098 if (size <= (64 * 1024))
3099 total_mem = full_size;
3101 total_mem = 64 * 1024;
3103 /* Allocate memory for ioctl data */
3104 total_mem = BUF_SZ_4K;
3106 dataout = kmalloc(total_mem, GFP_KERNEL);
3107 if (dataout == NULL) {
3109 goto loopback_test_exit;
3113 ptr += ELX_LOOPBACK_HEADER_SZ;
3114 sg_copy_to_buffer(job->request_payload.sg_list,
3115 job->request_payload.sg_cnt,
3117 rc = lpfcdiag_loop_self_reg(phba, &rpi);
3119 goto loopback_test_exit;
3121 if (phba->sli_rev < LPFC_SLI_REV4) {
3122 rc = lpfcdiag_loop_get_xri(phba, rpi, &txxri, &rxxri);
3124 lpfcdiag_loop_self_unreg(phba, rpi);
3125 goto loopback_test_exit;
3128 rc = lpfcdiag_sli3_loop_post_rxbufs(phba, rxxri, full_size);
3130 lpfcdiag_loop_self_unreg(phba, rpi);
3131 goto loopback_test_exit;
3134 evt = lpfc_bsg_event_new(FC_REG_CT_EVENT, current->pid,
3135 SLI_CT_ELX_LOOPBACK);
3137 lpfcdiag_loop_self_unreg(phba, rpi);
3139 goto loopback_test_exit;
3142 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3143 list_add(&evt->node, &phba->ct_ev_waiters);
3144 lpfc_bsg_event_ref(evt);
3145 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3147 cmdiocbq = lpfc_sli_get_iocbq(phba);
3148 if (phba->sli_rev < LPFC_SLI_REV4)
3149 rspiocbq = lpfc_sli_get_iocbq(phba);
3150 txbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3153 txbmp->virt = lpfc_mbuf_alloc(phba, 0, &txbmp->phys);
3155 INIT_LIST_HEAD(&txbmp->list);
3156 txbpl = (struct ulp_bde64 *) txbmp->virt;
3157 txbuffer = diag_cmd_data_alloc(phba,
3158 txbpl, full_size, 0);
3162 if (!cmdiocbq || !txbmp || !txbpl || !txbuffer || !txbmp->virt) {
3164 goto err_loopback_test_exit;
3166 if ((phba->sli_rev < LPFC_SLI_REV4) && !rspiocbq) {
3168 goto err_loopback_test_exit;
3171 cmdwqe = &cmdiocbq->wqe;
3172 memset(cmdwqe, 0, sizeof(union lpfc_wqe));
3173 if (phba->sli_rev < LPFC_SLI_REV4) {
3174 rspwqe = &rspiocbq->wqe;
3175 memset(rspwqe, 0, sizeof(union lpfc_wqe));
3178 INIT_LIST_HEAD(&head);
3179 list_add_tail(&head, &txbuffer->dma.list);
3180 list_for_each_entry(curr, &head, list) {
3181 segment_len = ((struct lpfc_dmabufext *)curr)->size;
3182 if (current_offset == 0) {
3184 memset(ctreq, 0, ELX_LOOPBACK_HEADER_SZ);
3185 ctreq->RevisionId.bits.Revision = SLI_CT_REVISION;
3186 ctreq->RevisionId.bits.InId = 0;
3187 ctreq->FsType = SLI_CT_ELX_LOOPBACK;
3188 ctreq->FsSubType = 0;
3189 ctreq->CommandResponse.bits.CmdRsp = cpu_to_be16(ELX_LOOPBACK_DATA);
3190 ctreq->CommandResponse.bits.Size = cpu_to_be16(size);
3191 segment_offset = ELX_LOOPBACK_HEADER_SZ;
3195 BUG_ON(segment_offset >= segment_len);
3196 memcpy(curr->virt + segment_offset,
3197 ptr + current_offset,
3198 segment_len - segment_offset);
3200 current_offset += segment_len - segment_offset;
3201 BUG_ON(current_offset > size);
3205 /* Build the XMIT_SEQUENCE iocb */
3206 num_bde = (uint32_t)txbuffer->flag;
3208 cmdiocbq->num_bdes = num_bde;
3209 cmdiocbq->cmd_flag |= LPFC_IO_LIBDFC;
3210 cmdiocbq->cmd_flag |= LPFC_IO_LOOPBACK;
3211 cmdiocbq->vport = phba->pport;
3212 cmdiocbq->cmd_cmpl = NULL;
3213 cmdiocbq->bpl_dmabuf = txbmp;
3215 if (phba->sli_rev < LPFC_SLI_REV4) {
3216 lpfc_sli_prep_xmit_seq64(phba, cmdiocbq, txbmp, 0, txxri,
3217 num_bde, FC_RCTL_DD_UNSOL_CTL, 1,
3218 CMD_XMIT_SEQUENCE64_CX);
3221 lpfc_sli_prep_xmit_seq64(phba, cmdiocbq, txbmp,
3222 phba->sli4_hba.rpi_ids[rpi], 0xffff,
3223 full_size, FC_RCTL_DD_UNSOL_CTL, 1,
3224 CMD_XMIT_SEQUENCE64_WQE);
3225 cmdiocbq->sli4_xritag = NO_XRI;
3228 iocb_stat = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq,
3229 rspiocbq, (phba->fc_ratov * 2) +
3231 if (iocb_stat != IOCB_SUCCESS ||
3232 (phba->sli_rev < LPFC_SLI_REV4 &&
3233 (get_job_ulpstatus(phba, rspiocbq) != IOSTAT_SUCCESS))) {
3234 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3235 "3126 Failed loopback test issue iocb: "
3236 "iocb_stat:x%x\n", iocb_stat);
3238 goto err_loopback_test_exit;
3242 time_left = wait_event_interruptible_timeout(
3243 evt->wq, !list_empty(&evt->events_to_see),
3244 msecs_to_jiffies(1000 *
3245 ((phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT)));
3247 if (list_empty(&evt->events_to_see)) {
3248 rc = (time_left) ? -EINTR : -ETIMEDOUT;
3249 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3250 "3125 Not receiving unsolicited event, "
3253 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3254 list_move(evt->events_to_see.prev, &evt->events_to_get);
3255 evdat = list_entry(evt->events_to_get.prev,
3256 typeof(*evdat), node);
3257 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3258 rx_databuf = evdat->data;
3259 if (evdat->len != full_size) {
3260 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3261 "1603 Loopback test did not receive expected "
3262 "data length. actual length 0x%x expected "
3264 evdat->len, full_size);
3266 } else if (rx_databuf == NULL)
3270 /* skip over elx loopback header */
3271 rx_databuf += ELX_LOOPBACK_HEADER_SZ;
3272 bsg_reply->reply_payload_rcv_len =
3273 sg_copy_from_buffer(job->reply_payload.sg_list,
3274 job->reply_payload.sg_cnt,
3276 bsg_reply->reply_payload_rcv_len = size;
3280 err_loopback_test_exit:
3281 lpfcdiag_loop_self_unreg(phba, rpi);
3283 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3284 lpfc_bsg_event_unref(evt); /* release ref */
3285 lpfc_bsg_event_unref(evt); /* delete */
3286 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3288 if ((cmdiocbq != NULL) && (iocb_stat != IOCB_TIMEDOUT))
3289 lpfc_sli_release_iocbq(phba, cmdiocbq);
3291 if (rspiocbq != NULL)
3292 lpfc_sli_release_iocbq(phba, rspiocbq);
3294 if (txbmp != NULL) {
3295 if (txbpl != NULL) {
3296 if (txbuffer != NULL)
3297 diag_cmd_data_free(phba, txbuffer);
3298 lpfc_mbuf_free(phba, txbmp->virt, txbmp->phys);
3305 /* make error code available to userspace */
3306 bsg_reply->result = rc;
3307 job->dd_data = NULL;
3308 /* complete the job back to userspace if no error */
3309 if (rc == IOCB_SUCCESS)
3310 bsg_job_done(job, bsg_reply->result,
3311 bsg_reply->reply_payload_rcv_len);
3316 * lpfc_bsg_get_dfc_rev - process a GET_DFC_REV bsg vendor command
3317 * @job: GET_DFC_REV fc_bsg_job
3320 lpfc_bsg_get_dfc_rev(struct bsg_job *job)
3322 struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
3323 struct fc_bsg_reply *bsg_reply = job->reply;
3324 struct lpfc_hba *phba = vport->phba;
3325 struct get_mgmt_rev_reply *event_reply;
3328 if (job->request_len <
3329 sizeof(struct fc_bsg_request) + sizeof(struct get_mgmt_rev)) {
3330 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3331 "2740 Received GET_DFC_REV request below "
3337 event_reply = (struct get_mgmt_rev_reply *)
3338 bsg_reply->reply_data.vendor_reply.vendor_rsp;
3340 if (job->reply_len < sizeof(*bsg_reply) + sizeof(*event_reply)) {
3341 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3342 "2741 Received GET_DFC_REV reply below "
3348 event_reply->info.a_Major = MANAGEMENT_MAJOR_REV;
3349 event_reply->info.a_Minor = MANAGEMENT_MINOR_REV;
3351 bsg_reply->result = rc;
3353 bsg_job_done(job, bsg_reply->result,
3354 bsg_reply->reply_payload_rcv_len);
3359 * lpfc_bsg_issue_mbox_cmpl - lpfc_bsg_issue_mbox mbox completion handler
3360 * @phba: Pointer to HBA context object.
3361 * @pmboxq: Pointer to mailbox command.
3363 * This is completion handler function for mailbox commands issued from
3364 * lpfc_bsg_issue_mbox function. This function is called by the
3365 * mailbox event handler function with no lock held. This function
3366 * will wake up thread waiting on the wait queue pointed by dd_data
3370 lpfc_bsg_issue_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
3372 struct bsg_job_data *dd_data;
3373 struct fc_bsg_reply *bsg_reply;
3374 struct bsg_job *job;
3376 unsigned long flags;
3377 uint8_t *pmb, *pmb_buf;
3379 dd_data = pmboxq->ctx_ndlp;
3382 * The outgoing buffer is readily referred from the dma buffer,
3383 * just need to get header part from mailboxq structure.
3385 pmb = (uint8_t *)&pmboxq->u.mb;
3386 pmb_buf = (uint8_t *)dd_data->context_un.mbox.mb;
3387 memcpy(pmb_buf, pmb, sizeof(MAILBOX_t));
3389 /* Determine if job has been aborted */
3391 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3392 job = dd_data->set_job;
3394 /* Prevent timeout handling from trying to abort job */
3395 job->dd_data = NULL;
3397 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3399 /* Copy the mailbox data to the job if it is still active */
3402 bsg_reply = job->reply;
3403 size = job->reply_payload.payload_len;
3404 bsg_reply->reply_payload_rcv_len =
3405 sg_copy_from_buffer(job->reply_payload.sg_list,
3406 job->reply_payload.sg_cnt,
3410 dd_data->set_job = NULL;
3411 mempool_free(dd_data->context_un.mbox.pmboxq, phba->mbox_mem_pool);
3412 lpfc_bsg_dma_page_free(phba, dd_data->context_un.mbox.dmabuffers);
3415 /* Complete the job if the job is still active */
3418 bsg_reply->result = 0;
3419 bsg_job_done(job, bsg_reply->result,
3420 bsg_reply->reply_payload_rcv_len);
3426 * lpfc_bsg_check_cmd_access - test for a supported mailbox command
3427 * @phba: Pointer to HBA context object.
3428 * @mb: Pointer to a mailbox object.
3429 * @vport: Pointer to a vport object.
3431 * Some commands require the port to be offline, some may not be called from
3434 static int lpfc_bsg_check_cmd_access(struct lpfc_hba *phba,
3435 MAILBOX_t *mb, struct lpfc_vport *vport)
3437 /* return negative error values for bsg job */
3438 switch (mb->mbxCommand) {
3442 case MBX_CONFIG_LINK:
3443 case MBX_CONFIG_RING:
3444 case MBX_RESET_RING:
3445 case MBX_UNREG_LOGIN:
3447 case MBX_DUMP_CONTEXT:
3451 if (!test_bit(FC_OFFLINE_MODE, &vport->fc_flag)) {
3452 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3453 "2743 Command 0x%x is illegal in on-line "
3460 case MBX_WRITE_VPARMS:
3463 case MBX_READ_CONFIG:
3464 case MBX_READ_RCONFIG:
3465 case MBX_READ_STATUS:
3468 case MBX_READ_LNK_STAT:
3469 case MBX_DUMP_MEMORY:
3471 case MBX_UPDATE_CFG:
3472 case MBX_KILL_BOARD:
3473 case MBX_READ_TOPOLOGY:
3475 case MBX_LOAD_EXP_ROM:
3477 case MBX_DEL_LD_ENTRY:
3480 case MBX_SLI4_CONFIG:
3481 case MBX_READ_EVENT_LOG:
3482 case MBX_READ_EVENT_LOG_STATUS:
3483 case MBX_WRITE_EVENT_LOG:
3484 case MBX_PORT_CAPABILITIES:
3485 case MBX_PORT_IOV_CONTROL:
3486 case MBX_RUN_BIU_DIAG64:
3488 case MBX_SET_VARIABLE:
3489 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3490 "1226 mbox: set_variable 0x%x, 0x%x\n",
3492 mb->un.varWords[1]);
3494 case MBX_READ_SPARM64:
3496 case MBX_REG_LOGIN64:
3497 case MBX_CONFIG_PORT:
3498 case MBX_RUN_BIU_DIAG:
3500 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3501 "2742 Unknown Command 0x%x\n",
3510 * lpfc_bsg_mbox_ext_session_reset - clean up context of multi-buffer mbox session
3511 * @phba: Pointer to HBA context object.
3513 * This is routine clean up and reset BSG handling of multi-buffer mbox
3517 lpfc_bsg_mbox_ext_session_reset(struct lpfc_hba *phba)
3519 if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_IDLE)
3522 /* free all memory, including dma buffers */
3523 lpfc_bsg_dma_page_list_free(phba,
3524 &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
3525 lpfc_bsg_dma_page_free(phba, phba->mbox_ext_buf_ctx.mbx_dmabuf);
3526 /* multi-buffer write mailbox command pass-through complete */
3527 memset((char *)&phba->mbox_ext_buf_ctx, 0,
3528 sizeof(struct lpfc_mbox_ext_buf_ctx));
3529 INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
3535 * lpfc_bsg_issue_mbox_ext_handle_job - job handler for multi-buffer mbox cmpl
3536 * @phba: Pointer to HBA context object.
3537 * @pmboxq: Pointer to mailbox command.
3539 * This is routine handles BSG job for mailbox commands completions with
3540 * multiple external buffers.
3542 static struct bsg_job *
3543 lpfc_bsg_issue_mbox_ext_handle_job(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
3545 struct bsg_job_data *dd_data;
3546 struct bsg_job *job;
3547 struct fc_bsg_reply *bsg_reply;
3548 uint8_t *pmb, *pmb_buf;
3549 unsigned long flags;
3552 struct lpfc_dmabuf *dmabuf;
3553 struct lpfc_sli_config_mbox *sli_cfg_mbx;
3556 dd_data = pmboxq->ctx_buf;
3558 /* Determine if job has been aborted */
3559 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3560 job = dd_data->set_job;
3562 bsg_reply = job->reply;
3563 /* Prevent timeout handling from trying to abort job */
3564 job->dd_data = NULL;
3566 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3569 * The outgoing buffer is readily referred from the dma buffer,
3570 * just need to get header part from mailboxq structure.
3573 pmb = (uint8_t *)&pmboxq->u.mb;
3574 pmb_buf = (uint8_t *)dd_data->context_un.mbox.mb;
3575 /* Copy the byte swapped response mailbox back to the user */
3576 memcpy(pmb_buf, pmb, sizeof(MAILBOX_t));
3577 /* if there is any non-embedded extended data copy that too */
3578 dmabuf = phba->mbox_ext_buf_ctx.mbx_dmabuf;
3579 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
3580 if (!bsg_bf_get(lpfc_mbox_hdr_emb,
3581 &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) {
3582 pmbx = (uint8_t *)dmabuf->virt;
3583 /* byte swap the extended data following the mailbox command */
3584 lpfc_sli_pcimem_bcopy(&pmbx[sizeof(MAILBOX_t)],
3585 &pmbx[sizeof(MAILBOX_t)],
3586 sli_cfg_mbx->un.sli_config_emb0_subsys.mse[0].buf_len);
3589 /* Complete the job if the job is still active */
3592 size = job->reply_payload.payload_len;
3593 bsg_reply->reply_payload_rcv_len =
3594 sg_copy_from_buffer(job->reply_payload.sg_list,
3595 job->reply_payload.sg_cnt,
3598 /* result for successful */
3599 bsg_reply->result = 0;
3601 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3602 "2937 SLI_CONFIG ext-buffer mailbox command "
3603 "(x%x/x%x) complete bsg job done, bsize:%d\n",
3604 phba->mbox_ext_buf_ctx.nembType,
3605 phba->mbox_ext_buf_ctx.mboxType, size);
3606 lpfc_idiag_mbxacc_dump_bsg_mbox(phba,
3607 phba->mbox_ext_buf_ctx.nembType,
3608 phba->mbox_ext_buf_ctx.mboxType,
3609 dma_ebuf, sta_pos_addr,
3610 phba->mbox_ext_buf_ctx.mbx_dmabuf, 0);
3612 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3613 "2938 SLI_CONFIG ext-buffer mailbox "
3614 "command (x%x/x%x) failure, rc:x%x\n",
3615 phba->mbox_ext_buf_ctx.nembType,
3616 phba->mbox_ext_buf_ctx.mboxType, rc);
3621 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_DONE;
3627 * lpfc_bsg_issue_read_mbox_ext_cmpl - compl handler for multi-buffer read mbox
3628 * @phba: Pointer to HBA context object.
3629 * @pmboxq: Pointer to mailbox command.
3631 * This is completion handler function for mailbox read commands with multiple
3635 lpfc_bsg_issue_read_mbox_ext_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
3637 struct bsg_job *job;
3638 struct fc_bsg_reply *bsg_reply;
3640 job = lpfc_bsg_issue_mbox_ext_handle_job(phba, pmboxq);
3642 /* handle the BSG job with mailbox command */
3644 pmboxq->u.mb.mbxStatus = MBXERR_ERROR;
3646 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3647 "2939 SLI_CONFIG ext-buffer rd mailbox command "
3648 "complete, ctxState:x%x, mbxStatus:x%x\n",
3649 phba->mbox_ext_buf_ctx.state, pmboxq->u.mb.mbxStatus);
3651 if (pmboxq->u.mb.mbxStatus || phba->mbox_ext_buf_ctx.numBuf == 1)
3652 lpfc_bsg_mbox_ext_session_reset(phba);
3654 /* free base driver mailbox structure memory */
3655 mempool_free(pmboxq, phba->mbox_mem_pool);
3657 /* if the job is still active, call job done */
3659 bsg_reply = job->reply;
3660 bsg_job_done(job, bsg_reply->result,
3661 bsg_reply->reply_payload_rcv_len);
3667 * lpfc_bsg_issue_write_mbox_ext_cmpl - cmpl handler for multi-buffer write mbox
3668 * @phba: Pointer to HBA context object.
3669 * @pmboxq: Pointer to mailbox command.
3671 * This is completion handler function for mailbox write commands with multiple
3675 lpfc_bsg_issue_write_mbox_ext_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
3677 struct bsg_job *job;
3678 struct fc_bsg_reply *bsg_reply;
3680 job = lpfc_bsg_issue_mbox_ext_handle_job(phba, pmboxq);
3682 /* handle the BSG job with the mailbox command */
3684 pmboxq->u.mb.mbxStatus = MBXERR_ERROR;
3686 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3687 "2940 SLI_CONFIG ext-buffer wr mailbox command "
3688 "complete, ctxState:x%x, mbxStatus:x%x\n",
3689 phba->mbox_ext_buf_ctx.state, pmboxq->u.mb.mbxStatus);
3691 /* free all memory, including dma buffers */
3692 mempool_free(pmboxq, phba->mbox_mem_pool);
3693 lpfc_bsg_mbox_ext_session_reset(phba);
3695 /* if the job is still active, call job done */
3697 bsg_reply = job->reply;
3698 bsg_job_done(job, bsg_reply->result,
3699 bsg_reply->reply_payload_rcv_len);
3706 lpfc_bsg_sli_cfg_dma_desc_setup(struct lpfc_hba *phba, enum nemb_type nemb_tp,
3707 uint32_t index, struct lpfc_dmabuf *mbx_dmabuf,
3708 struct lpfc_dmabuf *ext_dmabuf)
3710 struct lpfc_sli_config_mbox *sli_cfg_mbx;
3712 /* pointer to the start of mailbox command */
3713 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)mbx_dmabuf->virt;
3715 if (nemb_tp == nemb_mse) {
3717 sli_cfg_mbx->un.sli_config_emb0_subsys.
3719 putPaddrHigh(mbx_dmabuf->phys +
3721 sli_cfg_mbx->un.sli_config_emb0_subsys.
3723 putPaddrLow(mbx_dmabuf->phys +
3725 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3726 "2943 SLI_CONFIG(mse)[%d], "
3727 "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3729 sli_cfg_mbx->un.sli_config_emb0_subsys.
3731 sli_cfg_mbx->un.sli_config_emb0_subsys.
3733 sli_cfg_mbx->un.sli_config_emb0_subsys.
3736 sli_cfg_mbx->un.sli_config_emb0_subsys.
3738 putPaddrHigh(ext_dmabuf->phys);
3739 sli_cfg_mbx->un.sli_config_emb0_subsys.
3741 putPaddrLow(ext_dmabuf->phys);
3742 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3743 "2944 SLI_CONFIG(mse)[%d], "
3744 "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3746 sli_cfg_mbx->un.sli_config_emb0_subsys.
3748 sli_cfg_mbx->un.sli_config_emb0_subsys.
3750 sli_cfg_mbx->un.sli_config_emb0_subsys.
3755 sli_cfg_mbx->un.sli_config_emb1_subsys.
3757 putPaddrHigh(mbx_dmabuf->phys +
3759 sli_cfg_mbx->un.sli_config_emb1_subsys.
3761 putPaddrLow(mbx_dmabuf->phys +
3763 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3764 "3007 SLI_CONFIG(hbd)[%d], "
3765 "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3767 bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
3769 sli_config_emb1_subsys.hbd[index]),
3770 sli_cfg_mbx->un.sli_config_emb1_subsys.
3772 sli_cfg_mbx->un.sli_config_emb1_subsys.
3776 sli_cfg_mbx->un.sli_config_emb1_subsys.
3778 putPaddrHigh(ext_dmabuf->phys);
3779 sli_cfg_mbx->un.sli_config_emb1_subsys.
3781 putPaddrLow(ext_dmabuf->phys);
3782 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3783 "3008 SLI_CONFIG(hbd)[%d], "
3784 "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3786 bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
3788 sli_config_emb1_subsys.hbd[index]),
3789 sli_cfg_mbx->un.sli_config_emb1_subsys.
3791 sli_cfg_mbx->un.sli_config_emb1_subsys.
3799 * lpfc_bsg_sli_cfg_read_cmd_ext - sli_config non-embedded mailbox cmd read
3800 * @phba: Pointer to HBA context object.
3801 * @job: Pointer to the job object.
3802 * @nemb_tp: Enumerate of non-embedded mailbox command type.
3803 * @dmabuf: Pointer to a DMA buffer descriptor.
3805 * This routine performs SLI_CONFIG (0x9B) read mailbox command operation with
3806 * non-embedded external buffers.
3809 lpfc_bsg_sli_cfg_read_cmd_ext(struct lpfc_hba *phba, struct bsg_job *job,
3810 enum nemb_type nemb_tp,
3811 struct lpfc_dmabuf *dmabuf)
3813 struct fc_bsg_request *bsg_request = job->request;
3814 struct lpfc_sli_config_mbox *sli_cfg_mbx;
3815 struct dfc_mbox_req *mbox_req;
3816 struct lpfc_dmabuf *curr_dmabuf, *next_dmabuf;
3817 uint32_t ext_buf_cnt, ext_buf_index;
3818 struct lpfc_dmabuf *ext_dmabuf = NULL;
3819 struct bsg_job_data *dd_data = NULL;
3820 LPFC_MBOXQ_t *pmboxq = NULL;
3826 (struct dfc_mbox_req *)bsg_request->rqst_data.h_vendor.vendor_cmd;
3828 /* pointer to the start of mailbox command */
3829 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
3831 if (nemb_tp == nemb_mse) {
3832 ext_buf_cnt = bsg_bf_get(lpfc_mbox_hdr_mse_cnt,
3833 &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr);
3834 if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_MSE) {
3835 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3836 "2945 Handled SLI_CONFIG(mse) rd, "
3837 "ext_buf_cnt(%d) out of range(%d)\n",
3839 LPFC_MBX_SLI_CONFIG_MAX_MSE);
3843 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3844 "2941 Handled SLI_CONFIG(mse) rd, "
3845 "ext_buf_cnt:%d\n", ext_buf_cnt);
3847 /* sanity check on interface type for support */
3848 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
3849 LPFC_SLI_INTF_IF_TYPE_2) {
3853 /* nemb_tp == nemb_hbd */
3854 ext_buf_cnt = sli_cfg_mbx->un.sli_config_emb1_subsys.hbd_count;
3855 if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_HBD) {
3856 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3857 "2946 Handled SLI_CONFIG(hbd) rd, "
3858 "ext_buf_cnt(%d) out of range(%d)\n",
3860 LPFC_MBX_SLI_CONFIG_MAX_HBD);
3864 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3865 "2942 Handled SLI_CONFIG(hbd) rd, "
3866 "ext_buf_cnt:%d\n", ext_buf_cnt);
3869 /* before dma descriptor setup */
3870 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_rd, dma_mbox,
3871 sta_pre_addr, dmabuf, ext_buf_cnt);
3873 /* reject non-embedded mailbox command with none external buffer */
3874 if (ext_buf_cnt == 0) {
3877 } else if (ext_buf_cnt > 1) {
3878 /* additional external read buffers */
3879 for (i = 1; i < ext_buf_cnt; i++) {
3880 ext_dmabuf = lpfc_bsg_dma_page_alloc(phba);
3885 list_add_tail(&ext_dmabuf->list,
3886 &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
3890 /* bsg tracking structure */
3891 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
3897 /* mailbox command structure for base driver */
3898 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3903 memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
3905 /* for the first external buffer */
3906 lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, 0, dmabuf, dmabuf);
3908 /* for the rest of external buffer descriptors if any */
3909 if (ext_buf_cnt > 1) {
3911 list_for_each_entry_safe(curr_dmabuf, next_dmabuf,
3912 &phba->mbox_ext_buf_ctx.ext_dmabuf_list, list) {
3913 lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp,
3914 ext_buf_index, dmabuf,
3920 /* after dma descriptor setup */
3921 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_rd, dma_mbox,
3922 sta_pos_addr, dmabuf, ext_buf_cnt);
3924 /* construct base driver mbox command */
3925 pmb = &pmboxq->u.mb;
3926 pmbx = (uint8_t *)dmabuf->virt;
3927 memcpy(pmb, pmbx, sizeof(*pmb));
3928 pmb->mbxOwner = OWN_HOST;
3929 pmboxq->vport = phba->pport;
3931 /* multi-buffer handling context */
3932 phba->mbox_ext_buf_ctx.nembType = nemb_tp;
3933 phba->mbox_ext_buf_ctx.mboxType = mbox_rd;
3934 phba->mbox_ext_buf_ctx.numBuf = ext_buf_cnt;
3935 phba->mbox_ext_buf_ctx.mbxTag = mbox_req->extMboxTag;
3936 phba->mbox_ext_buf_ctx.seqNum = mbox_req->extSeqNum;
3937 phba->mbox_ext_buf_ctx.mbx_dmabuf = dmabuf;
3939 /* callback for multi-buffer read mailbox command */
3940 pmboxq->mbox_cmpl = lpfc_bsg_issue_read_mbox_ext_cmpl;
3942 /* context fields to callback function */
3943 pmboxq->ctx_buf = dd_data;
3944 dd_data->type = TYPE_MBOX;
3945 dd_data->set_job = job;
3946 dd_data->context_un.mbox.pmboxq = pmboxq;
3947 dd_data->context_un.mbox.mb = (MAILBOX_t *)pmbx;
3948 job->dd_data = dd_data;
3951 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
3954 * Non-embedded mailbox subcommand data gets byte swapped here because
3955 * the lower level driver code only does the first 64 mailbox words.
3957 if ((!bsg_bf_get(lpfc_mbox_hdr_emb,
3958 &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) &&
3959 (nemb_tp == nemb_mse))
3960 lpfc_sli_pcimem_bcopy(&pmbx[sizeof(MAILBOX_t)],
3961 &pmbx[sizeof(MAILBOX_t)],
3962 sli_cfg_mbx->un.sli_config_emb0_subsys.
3965 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
3966 if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
3967 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3968 "2947 Issued SLI_CONFIG ext-buffer "
3969 "mailbox command, rc:x%x\n", rc);
3970 return SLI_CONFIG_HANDLED;
3972 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3973 "2948 Failed to issue SLI_CONFIG ext-buffer "
3974 "mailbox command, rc:x%x\n", rc);
3979 mempool_free(pmboxq, phba->mbox_mem_pool);
3980 lpfc_bsg_dma_page_list_free(phba,
3981 &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
3983 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_IDLE;
3988 * lpfc_bsg_sli_cfg_write_cmd_ext - sli_config non-embedded mailbox cmd write
3989 * @phba: Pointer to HBA context object.
3990 * @job: Pointer to the job object.
3991 * @nemb_tp: Enumerate of non-embedded mailbox command type.
3992 * @dmabuf: Pointer to a DMA buffer descriptor.
3994 * This routine performs SLI_CONFIG (0x9B) write mailbox command operation with
3995 * non-embedded external buffers.
3998 lpfc_bsg_sli_cfg_write_cmd_ext(struct lpfc_hba *phba, struct bsg_job *job,
3999 enum nemb_type nemb_tp,
4000 struct lpfc_dmabuf *dmabuf)
4002 struct fc_bsg_request *bsg_request = job->request;
4003 struct fc_bsg_reply *bsg_reply = job->reply;
4004 struct dfc_mbox_req *mbox_req;
4005 struct lpfc_sli_config_mbox *sli_cfg_mbx;
4006 uint32_t ext_buf_cnt;
4007 struct bsg_job_data *dd_data = NULL;
4008 LPFC_MBOXQ_t *pmboxq = NULL;
4011 int rc = SLI_CONFIG_NOT_HANDLED, i;
4014 (struct dfc_mbox_req *)bsg_request->rqst_data.h_vendor.vendor_cmd;
4016 /* pointer to the start of mailbox command */
4017 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
4019 if (nemb_tp == nemb_mse) {
4020 ext_buf_cnt = bsg_bf_get(lpfc_mbox_hdr_mse_cnt,
4021 &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr);
4022 if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_MSE) {
4023 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4024 "2953 Failed SLI_CONFIG(mse) wr, "
4025 "ext_buf_cnt(%d) out of range(%d)\n",
4027 LPFC_MBX_SLI_CONFIG_MAX_MSE);
4030 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4031 "2949 Handled SLI_CONFIG(mse) wr, "
4032 "ext_buf_cnt:%d\n", ext_buf_cnt);
4034 /* sanity check on interface type for support */
4035 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
4036 LPFC_SLI_INTF_IF_TYPE_2)
4038 /* nemb_tp == nemb_hbd */
4039 ext_buf_cnt = sli_cfg_mbx->un.sli_config_emb1_subsys.hbd_count;
4040 if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_HBD) {
4041 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4042 "2954 Failed SLI_CONFIG(hbd) wr, "
4043 "ext_buf_cnt(%d) out of range(%d)\n",
4045 LPFC_MBX_SLI_CONFIG_MAX_HBD);
4048 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4049 "2950 Handled SLI_CONFIG(hbd) wr, "
4050 "ext_buf_cnt:%d\n", ext_buf_cnt);
4053 /* before dma buffer descriptor setup */
4054 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_wr, dma_mbox,
4055 sta_pre_addr, dmabuf, ext_buf_cnt);
4057 if (ext_buf_cnt == 0)
4060 /* for the first external buffer */
4061 lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, 0, dmabuf, dmabuf);
4063 /* after dma descriptor setup */
4064 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_wr, dma_mbox,
4065 sta_pos_addr, dmabuf, ext_buf_cnt);
4067 /* log for looking forward */
4068 for (i = 1; i < ext_buf_cnt; i++) {
4069 if (nemb_tp == nemb_mse)
4070 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4071 "2951 SLI_CONFIG(mse), buf[%d]-length:%d\n",
4072 i, sli_cfg_mbx->un.sli_config_emb0_subsys.
4075 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4076 "2952 SLI_CONFIG(hbd), buf[%d]-length:%d\n",
4077 i, bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
4078 &sli_cfg_mbx->un.sli_config_emb1_subsys.
4082 /* multi-buffer handling context */
4083 phba->mbox_ext_buf_ctx.nembType = nemb_tp;
4084 phba->mbox_ext_buf_ctx.mboxType = mbox_wr;
4085 phba->mbox_ext_buf_ctx.numBuf = ext_buf_cnt;
4086 phba->mbox_ext_buf_ctx.mbxTag = mbox_req->extMboxTag;
4087 phba->mbox_ext_buf_ctx.seqNum = mbox_req->extSeqNum;
4088 phba->mbox_ext_buf_ctx.mbx_dmabuf = dmabuf;
4090 if (ext_buf_cnt == 1) {
4091 /* bsg tracking structure */
4092 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
4098 /* mailbox command structure for base driver */
4099 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4104 memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
4105 pmb = &pmboxq->u.mb;
4106 mbx = (uint8_t *)dmabuf->virt;
4107 memcpy(pmb, mbx, sizeof(*pmb));
4108 pmb->mbxOwner = OWN_HOST;
4109 pmboxq->vport = phba->pport;
4111 /* callback for multi-buffer read mailbox command */
4112 pmboxq->mbox_cmpl = lpfc_bsg_issue_write_mbox_ext_cmpl;
4114 /* context fields to callback function */
4115 pmboxq->ctx_buf = dd_data;
4116 dd_data->type = TYPE_MBOX;
4117 dd_data->set_job = job;
4118 dd_data->context_un.mbox.pmboxq = pmboxq;
4119 dd_data->context_un.mbox.mb = (MAILBOX_t *)mbx;
4120 job->dd_data = dd_data;
4124 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
4125 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
4126 if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
4127 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4128 "2955 Issued SLI_CONFIG ext-buffer "
4129 "mailbox command, rc:x%x\n", rc);
4130 return SLI_CONFIG_HANDLED;
4132 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4133 "2956 Failed to issue SLI_CONFIG ext-buffer "
4134 "mailbox command, rc:x%x\n", rc);
4139 /* wait for additional external buffers */
4141 bsg_reply->result = 0;
4142 bsg_job_done(job, bsg_reply->result,
4143 bsg_reply->reply_payload_rcv_len);
4144 return SLI_CONFIG_HANDLED;
4148 mempool_free(pmboxq, phba->mbox_mem_pool);
4155 * lpfc_bsg_handle_sli_cfg_mbox - handle sli-cfg mailbox cmd with ext buffer
4156 * @phba: Pointer to HBA context object.
4157 * @job: Pointer to the job object.
4158 * @dmabuf: Pointer to a DMA buffer descriptor.
4160 * This routine handles SLI_CONFIG (0x9B) mailbox command with non-embedded
4161 * external buffers, including both 0x9B with non-embedded MSEs and 0x9B
4162 * with embedded subsystem 0x1 and opcodes with external HBDs.
4165 lpfc_bsg_handle_sli_cfg_mbox(struct lpfc_hba *phba, struct bsg_job *job,
4166 struct lpfc_dmabuf *dmabuf)
4168 struct lpfc_sli_config_mbox *sli_cfg_mbx;
4171 int rc = SLI_CONFIG_NOT_HANDLED;
4173 /* state change on new multi-buffer pass-through mailbox command */
4174 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_HOST;
4176 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
4178 if (!bsg_bf_get(lpfc_mbox_hdr_emb,
4179 &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) {
4180 subsys = bsg_bf_get(lpfc_emb0_subcmnd_subsys,
4181 &sli_cfg_mbx->un.sli_config_emb0_subsys);
4182 opcode = bsg_bf_get(lpfc_emb0_subcmnd_opcode,
4183 &sli_cfg_mbx->un.sli_config_emb0_subsys);
4184 if (subsys == SLI_CONFIG_SUBSYS_FCOE) {
4186 case FCOE_OPCODE_READ_FCF:
4187 case FCOE_OPCODE_GET_DPORT_RESULTS:
4188 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4189 "2957 Handled SLI_CONFIG "
4190 "subsys_fcoe, opcode:x%x\n",
4192 rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
4195 case FCOE_OPCODE_ADD_FCF:
4196 case FCOE_OPCODE_SET_DPORT_MODE:
4197 case LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_STATE:
4198 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4199 "2958 Handled SLI_CONFIG "
4200 "subsys_fcoe, opcode:x%x\n",
4202 rc = lpfc_bsg_sli_cfg_write_cmd_ext(phba, job,
4206 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4207 "2959 Reject SLI_CONFIG "
4208 "subsys_fcoe, opcode:x%x\n",
4213 } else if (subsys == SLI_CONFIG_SUBSYS_COMN) {
4215 case COMN_OPCODE_GET_CNTL_ADDL_ATTRIBUTES:
4216 case COMN_OPCODE_GET_CNTL_ATTRIBUTES:
4217 case COMN_OPCODE_GET_PROFILE_CONFIG:
4218 case COMN_OPCODE_SET_FEATURES:
4219 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4220 "3106 Handled SLI_CONFIG "
4221 "subsys_comn, opcode:x%x\n",
4223 rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
4227 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4228 "3107 Reject SLI_CONFIG "
4229 "subsys_comn, opcode:x%x\n",
4235 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4236 "2977 Reject SLI_CONFIG "
4237 "subsys:x%d, opcode:x%x\n",
4242 subsys = bsg_bf_get(lpfc_emb1_subcmnd_subsys,
4243 &sli_cfg_mbx->un.sli_config_emb1_subsys);
4244 opcode = bsg_bf_get(lpfc_emb1_subcmnd_opcode,
4245 &sli_cfg_mbx->un.sli_config_emb1_subsys);
4246 if (subsys == SLI_CONFIG_SUBSYS_COMN) {
4248 case COMN_OPCODE_READ_OBJECT:
4249 case COMN_OPCODE_READ_OBJECT_LIST:
4250 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4251 "2960 Handled SLI_CONFIG "
4252 "subsys_comn, opcode:x%x\n",
4254 rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
4257 case COMN_OPCODE_WRITE_OBJECT:
4258 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4259 "2961 Handled SLI_CONFIG "
4260 "subsys_comn, opcode:x%x\n",
4262 rc = lpfc_bsg_sli_cfg_write_cmd_ext(phba, job,
4266 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4267 "2962 Not handled SLI_CONFIG "
4268 "subsys_comn, opcode:x%x\n",
4270 rc = SLI_CONFIG_NOT_HANDLED;
4274 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4275 "2978 Not handled SLI_CONFIG "
4276 "subsys:x%d, opcode:x%x\n",
4278 rc = SLI_CONFIG_NOT_HANDLED;
4282 /* state reset on not handled new multi-buffer mailbox command */
4283 if (rc != SLI_CONFIG_HANDLED)
4284 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_IDLE;
4290 * lpfc_bsg_mbox_ext_abort - request to abort mbox command with ext buffers
4291 * @phba: Pointer to HBA context object.
4293 * This routine is for requesting to abort a pass-through mailbox command with
4294 * multiple external buffers due to error condition.
4297 lpfc_bsg_mbox_ext_abort(struct lpfc_hba *phba)
4299 if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_PORT)
4300 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_ABTS;
4302 lpfc_bsg_mbox_ext_session_reset(phba);
4307 * lpfc_bsg_read_ebuf_get - get the next mailbox read external buffer
4308 * @phba: Pointer to HBA context object.
4309 * @job: Pointer to the job object.
4311 * This routine extracts the next mailbox read external buffer back to
4312 * user space through BSG.
4315 lpfc_bsg_read_ebuf_get(struct lpfc_hba *phba, struct bsg_job *job)
4317 struct fc_bsg_reply *bsg_reply = job->reply;
4318 struct lpfc_sli_config_mbox *sli_cfg_mbx;
4319 struct lpfc_dmabuf *dmabuf;
4324 index = phba->mbox_ext_buf_ctx.seqNum;
4325 phba->mbox_ext_buf_ctx.seqNum++;
4327 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)
4328 phba->mbox_ext_buf_ctx.mbx_dmabuf->virt;
4330 if (phba->mbox_ext_buf_ctx.nembType == nemb_mse) {
4331 size = bsg_bf_get(lpfc_mbox_sli_config_mse_len,
4332 &sli_cfg_mbx->un.sli_config_emb0_subsys.mse[index]);
4333 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4334 "2963 SLI_CONFIG (mse) ext-buffer rd get "
4335 "buffer[%d], size:%d\n", index, size);
4337 size = bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
4338 &sli_cfg_mbx->un.sli_config_emb1_subsys.hbd[index]);
4339 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4340 "2964 SLI_CONFIG (hbd) ext-buffer rd get "
4341 "buffer[%d], size:%d\n", index, size);
4343 if (list_empty(&phba->mbox_ext_buf_ctx.ext_dmabuf_list))
4345 dmabuf = list_first_entry(&phba->mbox_ext_buf_ctx.ext_dmabuf_list,
4346 struct lpfc_dmabuf, list);
4347 list_del_init(&dmabuf->list);
4349 /* after dma buffer descriptor setup */
4350 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, phba->mbox_ext_buf_ctx.nembType,
4351 mbox_rd, dma_ebuf, sta_pos_addr,
4354 pbuf = (uint8_t *)dmabuf->virt;
4355 bsg_reply->reply_payload_rcv_len =
4356 sg_copy_from_buffer(job->reply_payload.sg_list,
4357 job->reply_payload.sg_cnt,
4360 lpfc_bsg_dma_page_free(phba, dmabuf);
4362 if (phba->mbox_ext_buf_ctx.seqNum == phba->mbox_ext_buf_ctx.numBuf) {
4363 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4364 "2965 SLI_CONFIG (hbd) ext-buffer rd mbox "
4365 "command session done\n");
4366 lpfc_bsg_mbox_ext_session_reset(phba);
4369 bsg_reply->result = 0;
4370 bsg_job_done(job, bsg_reply->result,
4371 bsg_reply->reply_payload_rcv_len);
4373 return SLI_CONFIG_HANDLED;
4377 * lpfc_bsg_write_ebuf_set - set the next mailbox write external buffer
4378 * @phba: Pointer to HBA context object.
4379 * @job: Pointer to the job object.
4380 * @dmabuf: Pointer to a DMA buffer descriptor.
4382 * This routine sets up the next mailbox read external buffer obtained
4383 * from user space through BSG.
4386 lpfc_bsg_write_ebuf_set(struct lpfc_hba *phba, struct bsg_job *job,
4387 struct lpfc_dmabuf *dmabuf)
4389 struct fc_bsg_reply *bsg_reply = job->reply;
4390 struct bsg_job_data *dd_data = NULL;
4391 LPFC_MBOXQ_t *pmboxq = NULL;
4393 enum nemb_type nemb_tp;
4399 index = phba->mbox_ext_buf_ctx.seqNum;
4400 phba->mbox_ext_buf_ctx.seqNum++;
4401 nemb_tp = phba->mbox_ext_buf_ctx.nembType;
4403 pbuf = (uint8_t *)dmabuf->virt;
4404 size = job->request_payload.payload_len;
4405 sg_copy_to_buffer(job->request_payload.sg_list,
4406 job->request_payload.sg_cnt,
4409 if (phba->mbox_ext_buf_ctx.nembType == nemb_mse) {
4410 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4411 "2966 SLI_CONFIG (mse) ext-buffer wr set "
4412 "buffer[%d], size:%d\n",
4413 phba->mbox_ext_buf_ctx.seqNum, size);
4416 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4417 "2967 SLI_CONFIG (hbd) ext-buffer wr set "
4418 "buffer[%d], size:%d\n",
4419 phba->mbox_ext_buf_ctx.seqNum, size);
4423 /* set up external buffer descriptor and add to external buffer list */
4424 lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, index,
4425 phba->mbox_ext_buf_ctx.mbx_dmabuf,
4427 list_add_tail(&dmabuf->list, &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
4429 /* after write dma buffer */
4430 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, phba->mbox_ext_buf_ctx.nembType,
4431 mbox_wr, dma_ebuf, sta_pos_addr,
4434 if (phba->mbox_ext_buf_ctx.seqNum == phba->mbox_ext_buf_ctx.numBuf) {
4435 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4436 "2968 SLI_CONFIG ext-buffer wr all %d "
4437 "ebuffers received\n",
4438 phba->mbox_ext_buf_ctx.numBuf);
4440 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
4446 /* mailbox command structure for base driver */
4447 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4452 memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
4453 pbuf = (uint8_t *)phba->mbox_ext_buf_ctx.mbx_dmabuf->virt;
4454 pmb = &pmboxq->u.mb;
4455 memcpy(pmb, pbuf, sizeof(*pmb));
4456 pmb->mbxOwner = OWN_HOST;
4457 pmboxq->vport = phba->pport;
4459 /* callback for multi-buffer write mailbox command */
4460 pmboxq->mbox_cmpl = lpfc_bsg_issue_write_mbox_ext_cmpl;
4462 /* context fields to callback function */
4463 pmboxq->ctx_buf = dd_data;
4464 dd_data->type = TYPE_MBOX;
4465 dd_data->set_job = job;
4466 dd_data->context_un.mbox.pmboxq = pmboxq;
4467 dd_data->context_un.mbox.mb = (MAILBOX_t *)pbuf;
4468 job->dd_data = dd_data;
4471 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
4473 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
4474 if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
4475 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4476 "2969 Issued SLI_CONFIG ext-buffer "
4477 "mailbox command, rc:x%x\n", rc);
4478 return SLI_CONFIG_HANDLED;
4480 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4481 "2970 Failed to issue SLI_CONFIG ext-buffer "
4482 "mailbox command, rc:x%x\n", rc);
4487 /* wait for additional external buffers */
4488 bsg_reply->result = 0;
4489 bsg_job_done(job, bsg_reply->result,
4490 bsg_reply->reply_payload_rcv_len);
4491 return SLI_CONFIG_HANDLED;
4495 mempool_free(pmboxq, phba->mbox_mem_pool);
4496 lpfc_bsg_dma_page_free(phba, dmabuf);
4503 * lpfc_bsg_handle_sli_cfg_ebuf - handle ext buffer with sli-cfg mailbox cmd
4504 * @phba: Pointer to HBA context object.
4505 * @job: Pointer to the job object.
4506 * @dmabuf: Pointer to a DMA buffer descriptor.
4508 * This routine handles the external buffer with SLI_CONFIG (0x9B) mailbox
4509 * command with multiple non-embedded external buffers.
4512 lpfc_bsg_handle_sli_cfg_ebuf(struct lpfc_hba *phba, struct bsg_job *job,
4513 struct lpfc_dmabuf *dmabuf)
4517 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4518 "2971 SLI_CONFIG buffer (type:x%x)\n",
4519 phba->mbox_ext_buf_ctx.mboxType);
4521 if (phba->mbox_ext_buf_ctx.mboxType == mbox_rd) {
4522 if (phba->mbox_ext_buf_ctx.state != LPFC_BSG_MBOX_DONE) {
4523 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4524 "2972 SLI_CONFIG rd buffer state "
4526 phba->mbox_ext_buf_ctx.state);
4527 lpfc_bsg_mbox_ext_abort(phba);
4530 rc = lpfc_bsg_read_ebuf_get(phba, job);
4531 if (rc == SLI_CONFIG_HANDLED)
4532 lpfc_bsg_dma_page_free(phba, dmabuf);
4533 } else { /* phba->mbox_ext_buf_ctx.mboxType == mbox_wr */
4534 if (phba->mbox_ext_buf_ctx.state != LPFC_BSG_MBOX_HOST) {
4535 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4536 "2973 SLI_CONFIG wr buffer state "
4538 phba->mbox_ext_buf_ctx.state);
4539 lpfc_bsg_mbox_ext_abort(phba);
4542 rc = lpfc_bsg_write_ebuf_set(phba, job, dmabuf);
4548 * lpfc_bsg_handle_sli_cfg_ext - handle sli-cfg mailbox with external buffer
4549 * @phba: Pointer to HBA context object.
4550 * @job: Pointer to the job object.
4551 * @dmabuf: Pointer to a DMA buffer descriptor.
4553 * This routine checks and handles non-embedded multi-buffer SLI_CONFIG
4554 * (0x9B) mailbox commands and external buffers.
4557 lpfc_bsg_handle_sli_cfg_ext(struct lpfc_hba *phba, struct bsg_job *job,
4558 struct lpfc_dmabuf *dmabuf)
4560 struct fc_bsg_request *bsg_request = job->request;
4561 struct dfc_mbox_req *mbox_req;
4562 int rc = SLI_CONFIG_NOT_HANDLED;
4565 (struct dfc_mbox_req *)bsg_request->rqst_data.h_vendor.vendor_cmd;
4567 /* mbox command with/without single external buffer */
4568 if (mbox_req->extMboxTag == 0 && mbox_req->extSeqNum == 0)
4571 /* mbox command and first external buffer */
4572 if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_IDLE) {
4573 if (mbox_req->extSeqNum == 1) {
4574 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4575 "2974 SLI_CONFIG mailbox: tag:%d, "
4576 "seq:%d\n", mbox_req->extMboxTag,
4577 mbox_req->extSeqNum);
4578 rc = lpfc_bsg_handle_sli_cfg_mbox(phba, job, dmabuf);
4581 goto sli_cfg_ext_error;
4585 * handle additional external buffers
4588 /* check broken pipe conditions */
4589 if (mbox_req->extMboxTag != phba->mbox_ext_buf_ctx.mbxTag)
4590 goto sli_cfg_ext_error;
4591 if (mbox_req->extSeqNum > phba->mbox_ext_buf_ctx.numBuf)
4592 goto sli_cfg_ext_error;
4593 if (mbox_req->extSeqNum != phba->mbox_ext_buf_ctx.seqNum + 1)
4594 goto sli_cfg_ext_error;
4596 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4597 "2975 SLI_CONFIG mailbox external buffer: "
4598 "extSta:x%x, tag:%d, seq:%d\n",
4599 phba->mbox_ext_buf_ctx.state, mbox_req->extMboxTag,
4600 mbox_req->extSeqNum);
4601 rc = lpfc_bsg_handle_sli_cfg_ebuf(phba, job, dmabuf);
4605 /* all other cases, broken pipe */
4606 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4607 "2976 SLI_CONFIG mailbox broken pipe: "
4608 "ctxSta:x%x, ctxNumBuf:%d "
4609 "ctxTag:%d, ctxSeq:%d, tag:%d, seq:%d\n",
4610 phba->mbox_ext_buf_ctx.state,
4611 phba->mbox_ext_buf_ctx.numBuf,
4612 phba->mbox_ext_buf_ctx.mbxTag,
4613 phba->mbox_ext_buf_ctx.seqNum,
4614 mbox_req->extMboxTag, mbox_req->extSeqNum);
4616 lpfc_bsg_mbox_ext_session_reset(phba);
4622 * lpfc_bsg_issue_mbox - issues a mailbox command on behalf of an app
4623 * @phba: Pointer to HBA context object.
4624 * @job: Pointer to the job object.
4625 * @vport: Pointer to a vport object.
4627 * Allocate a tracking object, mailbox command memory, get a mailbox
4628 * from the mailbox pool, copy the caller mailbox command.
4630 * If offline and the sli is active we need to poll for the command (port is
4631 * being reset) and complete the job, otherwise issue the mailbox command and
4632 * let our completion handler finish the command.
4635 lpfc_bsg_issue_mbox(struct lpfc_hba *phba, struct bsg_job *job,
4636 struct lpfc_vport *vport)
4638 struct fc_bsg_request *bsg_request = job->request;
4639 struct fc_bsg_reply *bsg_reply = job->reply;
4640 LPFC_MBOXQ_t *pmboxq = NULL; /* internal mailbox queue */
4641 MAILBOX_t *pmb; /* shortcut to the pmboxq mailbox */
4642 /* a 4k buffer to hold the mb and extended data from/to the bsg */
4643 uint8_t *pmbx = NULL;
4644 struct bsg_job_data *dd_data = NULL; /* bsg data tracking structure */
4645 struct lpfc_dmabuf *dmabuf = NULL;
4646 struct dfc_mbox_req *mbox_req;
4647 struct READ_EVENT_LOG_VAR *rdEventLog;
4648 uint32_t transmit_length, receive_length, mode;
4649 struct lpfc_mbx_sli4_config *sli4_config;
4650 struct lpfc_mbx_nembed_cmd *nembed_sge;
4651 struct ulp_bde64 *bde;
4652 uint8_t *ext = NULL;
4657 /* in case no data is transferred */
4658 bsg_reply->reply_payload_rcv_len = 0;
4660 /* sanity check to protect driver */
4661 if (job->reply_payload.payload_len > BSG_MBOX_SIZE ||
4662 job->request_payload.payload_len > BSG_MBOX_SIZE) {
4668 * Don't allow mailbox commands to be sent when blocked or when in
4669 * the middle of discovery
4671 if (phba->sli.sli_flag & LPFC_BLOCK_MGMT_IO) {
4677 (struct dfc_mbox_req *)bsg_request->rqst_data.h_vendor.vendor_cmd;
4679 /* check if requested extended data lengths are valid */
4680 if ((mbox_req->inExtWLen > BSG_MBOX_SIZE/sizeof(uint32_t)) ||
4681 (mbox_req->outExtWLen > BSG_MBOX_SIZE/sizeof(uint32_t))) {
4686 dmabuf = lpfc_bsg_dma_page_alloc(phba);
4687 if (!dmabuf || !dmabuf->virt) {
4692 /* Get the mailbox command or external buffer from BSG */
4693 pmbx = (uint8_t *)dmabuf->virt;
4694 size = job->request_payload.payload_len;
4695 sg_copy_to_buffer(job->request_payload.sg_list,
4696 job->request_payload.sg_cnt, pmbx, size);
4698 /* Handle possible SLI_CONFIG with non-embedded payloads */
4699 if (phba->sli_rev == LPFC_SLI_REV4) {
4700 rc = lpfc_bsg_handle_sli_cfg_ext(phba, job, dmabuf);
4701 if (rc == SLI_CONFIG_HANDLED)
4705 /* SLI_CONFIG_NOT_HANDLED for other mailbox commands */
4708 rc = lpfc_bsg_check_cmd_access(phba, (MAILBOX_t *)pmbx, vport);
4710 goto job_done; /* must be negative */
4712 /* allocate our bsg tracking structure */
4713 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
4715 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
4716 "2727 Failed allocation of dd_data\n");
4721 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4726 memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
4728 pmb = &pmboxq->u.mb;
4729 memcpy(pmb, pmbx, sizeof(*pmb));
4730 pmb->mbxOwner = OWN_HOST;
4731 pmboxq->vport = vport;
4733 /* If HBA encountered an error attention, allow only DUMP
4734 * or RESTART mailbox commands until the HBA is restarted.
4736 if (phba->pport->stopped &&
4737 pmb->mbxCommand != MBX_DUMP_MEMORY &&
4738 pmb->mbxCommand != MBX_RESTART &&
4739 pmb->mbxCommand != MBX_WRITE_VPARMS &&
4740 pmb->mbxCommand != MBX_WRITE_WWN)
4741 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
4742 "2797 mbox: Issued mailbox cmd "
4743 "0x%x while in stopped state.\n",
4746 /* extended mailbox commands will need an extended buffer */
4747 if (mbox_req->inExtWLen || mbox_req->outExtWLen) {
4749 ext = from + sizeof(MAILBOX_t);
4750 pmboxq->ctx_buf = ext;
4751 pmboxq->in_ext_byte_len =
4752 mbox_req->inExtWLen * sizeof(uint32_t);
4753 pmboxq->out_ext_byte_len =
4754 mbox_req->outExtWLen * sizeof(uint32_t);
4755 pmboxq->mbox_offset_word = mbox_req->mbOffset;
4758 /* biu diag will need a kernel buffer to transfer the data
4759 * allocate our own buffer and setup the mailbox command to
4762 if (pmb->mbxCommand == MBX_RUN_BIU_DIAG64) {
4763 transmit_length = pmb->un.varWords[1];
4764 receive_length = pmb->un.varWords[4];
4765 /* transmit length cannot be greater than receive length or
4766 * mailbox extension size
4768 if ((transmit_length > receive_length) ||
4769 (transmit_length > BSG_MBOX_SIZE - sizeof(MAILBOX_t))) {
4773 pmb->un.varBIUdiag.un.s2.xmit_bde64.addrHigh =
4774 putPaddrHigh(dmabuf->phys + sizeof(MAILBOX_t));
4775 pmb->un.varBIUdiag.un.s2.xmit_bde64.addrLow =
4776 putPaddrLow(dmabuf->phys + sizeof(MAILBOX_t));
4778 pmb->un.varBIUdiag.un.s2.rcv_bde64.addrHigh =
4779 putPaddrHigh(dmabuf->phys + sizeof(MAILBOX_t)
4780 + pmb->un.varBIUdiag.un.s2.xmit_bde64.tus.f.bdeSize);
4781 pmb->un.varBIUdiag.un.s2.rcv_bde64.addrLow =
4782 putPaddrLow(dmabuf->phys + sizeof(MAILBOX_t)
4783 + pmb->un.varBIUdiag.un.s2.xmit_bde64.tus.f.bdeSize);
4784 } else if (pmb->mbxCommand == MBX_READ_EVENT_LOG) {
4785 rdEventLog = &pmb->un.varRdEventLog;
4786 receive_length = rdEventLog->rcv_bde64.tus.f.bdeSize;
4787 mode = bf_get(lpfc_event_log, rdEventLog);
4789 /* receive length cannot be greater than mailbox
4792 if (receive_length > BSG_MBOX_SIZE - sizeof(MAILBOX_t)) {
4797 /* mode zero uses a bde like biu diags command */
4799 pmb->un.varWords[3] = putPaddrLow(dmabuf->phys
4800 + sizeof(MAILBOX_t));
4801 pmb->un.varWords[4] = putPaddrHigh(dmabuf->phys
4802 + sizeof(MAILBOX_t));
4804 } else if (phba->sli_rev == LPFC_SLI_REV4) {
4805 /* Let type 4 (well known data) through because the data is
4806 * returned in varwords[4-8]
4807 * otherwise check the recieve length and fetch the buffer addr
4809 if ((pmb->mbxCommand == MBX_DUMP_MEMORY) &&
4810 (pmb->un.varDmp.type != DMP_WELL_KNOWN)) {
4811 /* rebuild the command for sli4 using our own buffers
4812 * like we do for biu diags
4814 receive_length = pmb->un.varWords[2];
4815 /* receive length cannot be greater than mailbox
4818 if (receive_length == 0) {
4822 pmb->un.varWords[3] = putPaddrLow(dmabuf->phys
4823 + sizeof(MAILBOX_t));
4824 pmb->un.varWords[4] = putPaddrHigh(dmabuf->phys
4825 + sizeof(MAILBOX_t));
4826 } else if ((pmb->mbxCommand == MBX_UPDATE_CFG) &&
4827 pmb->un.varUpdateCfg.co) {
4828 bde = (struct ulp_bde64 *)&pmb->un.varWords[4];
4830 /* bde size cannot be greater than mailbox ext size */
4831 if (bde->tus.f.bdeSize >
4832 BSG_MBOX_SIZE - sizeof(MAILBOX_t)) {
4836 bde->addrHigh = putPaddrHigh(dmabuf->phys
4837 + sizeof(MAILBOX_t));
4838 bde->addrLow = putPaddrLow(dmabuf->phys
4839 + sizeof(MAILBOX_t));
4840 } else if (pmb->mbxCommand == MBX_SLI4_CONFIG) {
4841 /* Handling non-embedded SLI_CONFIG mailbox command */
4842 sli4_config = &pmboxq->u.mqe.un.sli4_config;
4843 if (!bf_get(lpfc_mbox_hdr_emb,
4844 &sli4_config->header.cfg_mhdr)) {
4845 /* rebuild the command for sli4 using our
4846 * own buffers like we do for biu diags
4848 nembed_sge = (struct lpfc_mbx_nembed_cmd *)
4849 &pmb->un.varWords[0];
4850 receive_length = nembed_sge->sge[0].length;
4852 /* receive length cannot be greater than
4853 * mailbox extension size
4855 if ((receive_length == 0) ||
4857 BSG_MBOX_SIZE - sizeof(MAILBOX_t))) {
4862 nembed_sge->sge[0].pa_hi =
4863 putPaddrHigh(dmabuf->phys
4864 + sizeof(MAILBOX_t));
4865 nembed_sge->sge[0].pa_lo =
4866 putPaddrLow(dmabuf->phys
4867 + sizeof(MAILBOX_t));
4872 dd_data->context_un.mbox.dmabuffers = dmabuf;
4874 /* setup wake call as IOCB callback */
4875 pmboxq->mbox_cmpl = lpfc_bsg_issue_mbox_cmpl;
4877 /* setup context field to pass wait_queue pointer to wake function */
4878 pmboxq->ctx_ndlp = dd_data;
4879 dd_data->type = TYPE_MBOX;
4880 dd_data->set_job = job;
4881 dd_data->context_un.mbox.pmboxq = pmboxq;
4882 dd_data->context_un.mbox.mb = (MAILBOX_t *)pmbx;
4883 dd_data->context_un.mbox.ext = ext;
4884 dd_data->context_un.mbox.mbOffset = mbox_req->mbOffset;
4885 dd_data->context_un.mbox.inExtWLen = mbox_req->inExtWLen;
4886 dd_data->context_un.mbox.outExtWLen = mbox_req->outExtWLen;
4887 job->dd_data = dd_data;
4889 if (test_bit(FC_OFFLINE_MODE, &vport->fc_flag) ||
4890 (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))) {
4891 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
4892 if (rc != MBX_SUCCESS) {
4893 rc = (rc == MBX_TIMEOUT) ? -ETIME : -ENODEV;
4897 /* job finished, copy the data */
4898 memcpy(pmbx, pmb, sizeof(*pmb));
4899 bsg_reply->reply_payload_rcv_len =
4900 sg_copy_from_buffer(job->reply_payload.sg_list,
4901 job->reply_payload.sg_cnt,
4903 /* not waiting mbox already done */
4908 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
4909 if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY))
4910 return 1; /* job started */
4913 /* common exit for error or job completed inline */
4915 mempool_free(pmboxq, phba->mbox_mem_pool);
4916 lpfc_bsg_dma_page_free(phba, dmabuf);
4924 * lpfc_bsg_mbox_cmd - process an fc bsg LPFC_BSG_VENDOR_MBOX command
4925 * @job: MBOX fc_bsg_job for LPFC_BSG_VENDOR_MBOX.
4928 lpfc_bsg_mbox_cmd(struct bsg_job *job)
4930 struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
4931 struct fc_bsg_request *bsg_request = job->request;
4932 struct fc_bsg_reply *bsg_reply = job->reply;
4933 struct lpfc_hba *phba = vport->phba;
4934 struct dfc_mbox_req *mbox_req;
4937 /* mix-and-match backward compatibility */
4938 bsg_reply->reply_payload_rcv_len = 0;
4939 if (job->request_len <
4940 sizeof(struct fc_bsg_request) + sizeof(struct dfc_mbox_req)) {
4941 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4942 "2737 Mix-and-match backward compatibility "
4943 "between MBOX_REQ old size:%d and "
4944 "new request size:%d\n",
4945 (int)(job->request_len -
4946 sizeof(struct fc_bsg_request)),
4947 (int)sizeof(struct dfc_mbox_req));
4948 mbox_req = (struct dfc_mbox_req *)
4949 bsg_request->rqst_data.h_vendor.vendor_cmd;
4950 mbox_req->extMboxTag = 0;
4951 mbox_req->extSeqNum = 0;
4954 rc = lpfc_bsg_issue_mbox(phba, job, vport);
4958 bsg_reply->result = 0;
4959 job->dd_data = NULL;
4960 bsg_job_done(job, bsg_reply->result,
4961 bsg_reply->reply_payload_rcv_len);
4963 /* job submitted, will complete later*/
4964 rc = 0; /* return zero, no error */
4966 /* some error occurred */
4967 bsg_reply->result = rc;
4968 job->dd_data = NULL;
4975 lpfc_forced_link_speed(struct bsg_job *job)
4977 struct Scsi_Host *shost = fc_bsg_to_shost(job);
4978 struct lpfc_vport *vport = shost_priv(shost);
4979 struct lpfc_hba *phba = vport->phba;
4980 struct fc_bsg_reply *bsg_reply = job->reply;
4981 struct forced_link_speed_support_reply *forced_reply;
4984 if (job->request_len <
4985 sizeof(struct fc_bsg_request) +
4986 sizeof(struct get_forced_link_speed_support)) {
4987 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
4988 "0048 Received FORCED_LINK_SPEED request "
4989 "below minimum size\n");
4994 forced_reply = (struct forced_link_speed_support_reply *)
4995 bsg_reply->reply_data.vendor_reply.vendor_rsp;
4997 if (job->reply_len < sizeof(*bsg_reply) + sizeof(*forced_reply)) {
4998 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
4999 "0049 Received FORCED_LINK_SPEED reply below "
5005 forced_reply->supported = (phba->hba_flag & HBA_FORCED_LINK_SPEED)
5006 ? LPFC_FORCED_LINK_SPEED_SUPPORTED
5007 : LPFC_FORCED_LINK_SPEED_NOT_SUPPORTED;
5009 bsg_reply->result = rc;
5011 bsg_job_done(job, bsg_reply->result,
5012 bsg_reply->reply_payload_rcv_len);
5017 * lpfc_check_fwlog_support: Check FW log support on the adapter
5018 * @phba: Pointer to HBA context object.
5020 * Check if FW Logging support by the adapter
5023 lpfc_check_fwlog_support(struct lpfc_hba *phba)
5025 struct lpfc_ras_fwlog *ras_fwlog = NULL;
5027 ras_fwlog = &phba->ras_fwlog;
5029 if (!ras_fwlog->ras_hwsupport)
5031 else if (!ras_fwlog->ras_enabled)
5038 * lpfc_bsg_get_ras_config: Get RAS configuration settings
5039 * @job: fc_bsg_job to handle
5041 * Get RAS configuration values set.
5044 lpfc_bsg_get_ras_config(struct bsg_job *job)
5046 struct Scsi_Host *shost = fc_bsg_to_shost(job);
5047 struct lpfc_vport *vport = shost_priv(shost);
5048 struct fc_bsg_reply *bsg_reply = job->reply;
5049 struct lpfc_hba *phba = vport->phba;
5050 struct lpfc_bsg_get_ras_config_reply *ras_reply;
5051 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
5054 if (job->request_len <
5055 sizeof(struct fc_bsg_request) +
5056 sizeof(struct lpfc_bsg_ras_req)) {
5057 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
5058 "6192 FW_LOG request received "
5059 "below minimum size\n");
5064 /* Check FW log status */
5065 rc = lpfc_check_fwlog_support(phba);
5069 ras_reply = (struct lpfc_bsg_get_ras_config_reply *)
5070 bsg_reply->reply_data.vendor_reply.vendor_rsp;
5072 /* Current logging state */
5073 spin_lock_irq(&phba->hbalock);
5074 if (ras_fwlog->state == ACTIVE)
5075 ras_reply->state = LPFC_RASLOG_STATE_RUNNING;
5077 ras_reply->state = LPFC_RASLOG_STATE_STOPPED;
5078 spin_unlock_irq(&phba->hbalock);
5080 ras_reply->log_level = phba->ras_fwlog.fw_loglevel;
5081 ras_reply->log_buff_sz = phba->cfg_ras_fwlog_buffsize;
5084 /* make error code available to userspace */
5085 bsg_reply->result = rc;
5087 /* complete the job back to userspace */
5089 bsg_job_done(job, bsg_reply->result,
5090 bsg_reply->reply_payload_rcv_len);
5095 * lpfc_bsg_set_ras_config: Set FW logging parameters
5096 * @job: fc_bsg_job to handle
5098 * Set log-level parameters for FW-logging in host memory
5101 lpfc_bsg_set_ras_config(struct bsg_job *job)
5103 struct Scsi_Host *shost = fc_bsg_to_shost(job);
5104 struct lpfc_vport *vport = shost_priv(shost);
5105 struct lpfc_hba *phba = vport->phba;
5106 struct lpfc_bsg_set_ras_config_req *ras_req;
5107 struct fc_bsg_request *bsg_request = job->request;
5108 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
5109 struct fc_bsg_reply *bsg_reply = job->reply;
5110 uint8_t action = 0, log_level = 0;
5111 int rc = 0, action_status = 0;
5113 if (job->request_len <
5114 sizeof(struct fc_bsg_request) +
5115 sizeof(struct lpfc_bsg_set_ras_config_req)) {
5116 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
5117 "6182 Received RAS_LOG request "
5118 "below minimum size\n");
5123 /* Check FW log status */
5124 rc = lpfc_check_fwlog_support(phba);
5128 ras_req = (struct lpfc_bsg_set_ras_config_req *)
5129 bsg_request->rqst_data.h_vendor.vendor_cmd;
5130 action = ras_req->action;
5131 log_level = ras_req->log_level;
5133 if (action == LPFC_RASACTION_STOP_LOGGING) {
5134 /* Check if already disabled */
5135 spin_lock_irq(&phba->hbalock);
5136 if (ras_fwlog->state != ACTIVE) {
5137 spin_unlock_irq(&phba->hbalock);
5141 spin_unlock_irq(&phba->hbalock);
5143 /* Disable logging */
5144 lpfc_ras_stop_fwlog(phba);
5146 /*action = LPFC_RASACTION_START_LOGGING*/
5148 /* Even though FW-logging is active re-initialize
5149 * FW-logging with new log-level. Return status
5150 * "Logging already Running" to caller.
5152 spin_lock_irq(&phba->hbalock);
5153 if (ras_fwlog->state != INACTIVE)
5154 action_status = -EINPROGRESS;
5155 spin_unlock_irq(&phba->hbalock);
5157 /* Enable logging */
5158 rc = lpfc_sli4_ras_fwlog_init(phba, log_level,
5159 LPFC_RAS_ENABLE_LOGGING);
5165 /* Check if FW-logging is re-initialized */
5166 if (action_status == -EINPROGRESS)
5170 /* make error code available to userspace */
5171 bsg_reply->result = rc;
5173 /* complete the job back to userspace */
5175 bsg_job_done(job, bsg_reply->result,
5176 bsg_reply->reply_payload_rcv_len);
5182 * lpfc_bsg_get_ras_lwpd: Get log write position data
5183 * @job: fc_bsg_job to handle
5185 * Get Offset/Wrap count of the log message written
5189 lpfc_bsg_get_ras_lwpd(struct bsg_job *job)
5191 struct Scsi_Host *shost = fc_bsg_to_shost(job);
5192 struct lpfc_vport *vport = shost_priv(shost);
5193 struct lpfc_bsg_get_ras_lwpd *ras_reply;
5194 struct lpfc_hba *phba = vport->phba;
5195 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
5196 struct fc_bsg_reply *bsg_reply = job->reply;
5197 u32 *lwpd_ptr = NULL;
5200 rc = lpfc_check_fwlog_support(phba);
5204 if (job->request_len <
5205 sizeof(struct fc_bsg_request) +
5206 sizeof(struct lpfc_bsg_ras_req)) {
5207 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
5208 "6183 Received RAS_LOG request "
5209 "below minimum size\n");
5214 ras_reply = (struct lpfc_bsg_get_ras_lwpd *)
5215 bsg_reply->reply_data.vendor_reply.vendor_rsp;
5217 if (!ras_fwlog->lwpd.virt) {
5218 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
5219 "6193 Restart FW Logging\n");
5224 /* Get lwpd offset */
5225 lwpd_ptr = (uint32_t *)(ras_fwlog->lwpd.virt);
5226 ras_reply->offset = be32_to_cpu(*lwpd_ptr & 0xffffffff);
5228 /* Get wrap count */
5229 ras_reply->wrap_count = be32_to_cpu(*(++lwpd_ptr) & 0xffffffff);
5232 /* make error code available to userspace */
5233 bsg_reply->result = rc;
5235 /* complete the job back to userspace */
5237 bsg_job_done(job, bsg_reply->result,
5238 bsg_reply->reply_payload_rcv_len);
5244 * lpfc_bsg_get_ras_fwlog: Read FW log
5245 * @job: fc_bsg_job to handle
5247 * Copy the FW log into the passed buffer.
5250 lpfc_bsg_get_ras_fwlog(struct bsg_job *job)
5252 struct Scsi_Host *shost = fc_bsg_to_shost(job);
5253 struct lpfc_vport *vport = shost_priv(shost);
5254 struct lpfc_hba *phba = vport->phba;
5255 struct fc_bsg_request *bsg_request = job->request;
5256 struct fc_bsg_reply *bsg_reply = job->reply;
5257 struct lpfc_bsg_get_fwlog_req *ras_req;
5258 u32 rd_offset, rd_index, offset;
5259 void *src, *fwlog_buff;
5260 struct lpfc_ras_fwlog *ras_fwlog = NULL;
5261 struct lpfc_dmabuf *dmabuf, *next;
5264 ras_fwlog = &phba->ras_fwlog;
5266 rc = lpfc_check_fwlog_support(phba);
5270 /* Logging to be stopped before reading */
5271 spin_lock_irq(&phba->hbalock);
5272 if (ras_fwlog->state == ACTIVE) {
5273 spin_unlock_irq(&phba->hbalock);
5277 spin_unlock_irq(&phba->hbalock);
5279 if (job->request_len <
5280 sizeof(struct fc_bsg_request) +
5281 sizeof(struct lpfc_bsg_get_fwlog_req)) {
5282 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
5283 "6184 Received RAS_LOG request "
5284 "below minimum size\n");
5289 ras_req = (struct lpfc_bsg_get_fwlog_req *)
5290 bsg_request->rqst_data.h_vendor.vendor_cmd;
5291 rd_offset = ras_req->read_offset;
5293 /* Allocate memory to read fw log*/
5294 fwlog_buff = vmalloc(ras_req->read_size);
5300 rd_index = (rd_offset / LPFC_RAS_MAX_ENTRY_SIZE);
5301 offset = (rd_offset % LPFC_RAS_MAX_ENTRY_SIZE);
5303 list_for_each_entry_safe(dmabuf, next,
5304 &ras_fwlog->fwlog_buff_list, list) {
5306 if (dmabuf->buffer_tag < rd_index)
5309 src = dmabuf->virt + offset;
5310 memcpy(fwlog_buff, src, ras_req->read_size);
5314 bsg_reply->reply_payload_rcv_len =
5315 sg_copy_from_buffer(job->reply_payload.sg_list,
5316 job->reply_payload.sg_cnt,
5317 fwlog_buff, ras_req->read_size);
5322 bsg_reply->result = rc;
5324 bsg_job_done(job, bsg_reply->result,
5325 bsg_reply->reply_payload_rcv_len);
5331 lpfc_get_trunk_info(struct bsg_job *job)
5333 struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
5334 struct lpfc_hba *phba = vport->phba;
5335 struct fc_bsg_reply *bsg_reply = job->reply;
5336 struct lpfc_trunk_info *event_reply;
5339 if (job->request_len <
5340 sizeof(struct fc_bsg_request) + sizeof(struct get_trunk_info_req)) {
5341 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
5342 "2744 Received GET TRUNK _INFO request below "
5348 event_reply = (struct lpfc_trunk_info *)
5349 bsg_reply->reply_data.vendor_reply.vendor_rsp;
5351 if (job->reply_len < sizeof(*bsg_reply) + sizeof(*event_reply)) {
5352 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5353 "2728 Received GET TRUNK _INFO reply below "
5358 if (event_reply == NULL) {
5363 bsg_bf_set(lpfc_trunk_info_link_status, event_reply,
5364 (phba->link_state >= LPFC_LINK_UP) ? 1 : 0);
5366 bsg_bf_set(lpfc_trunk_info_trunk_active0, event_reply,
5367 (phba->trunk_link.link0.state == LPFC_LINK_UP) ? 1 : 0);
5369 bsg_bf_set(lpfc_trunk_info_trunk_active1, event_reply,
5370 (phba->trunk_link.link1.state == LPFC_LINK_UP) ? 1 : 0);
5372 bsg_bf_set(lpfc_trunk_info_trunk_active2, event_reply,
5373 (phba->trunk_link.link2.state == LPFC_LINK_UP) ? 1 : 0);
5375 bsg_bf_set(lpfc_trunk_info_trunk_active3, event_reply,
5376 (phba->trunk_link.link3.state == LPFC_LINK_UP) ? 1 : 0);
5378 bsg_bf_set(lpfc_trunk_info_trunk_config0, event_reply,
5379 bf_get(lpfc_conf_trunk_port0, &phba->sli4_hba));
5381 bsg_bf_set(lpfc_trunk_info_trunk_config1, event_reply,
5382 bf_get(lpfc_conf_trunk_port1, &phba->sli4_hba));
5384 bsg_bf_set(lpfc_trunk_info_trunk_config2, event_reply,
5385 bf_get(lpfc_conf_trunk_port2, &phba->sli4_hba));
5387 bsg_bf_set(lpfc_trunk_info_trunk_config3, event_reply,
5388 bf_get(lpfc_conf_trunk_port3, &phba->sli4_hba));
5390 event_reply->port_speed = phba->sli4_hba.link_state.speed / 1000;
5391 event_reply->logical_speed =
5392 phba->sli4_hba.link_state.logical_speed / 1000;
5394 bsg_reply->result = rc;
5396 bsg_job_done(job, bsg_reply->result,
5397 bsg_reply->reply_payload_rcv_len);
5403 lpfc_get_cgnbuf_info(struct bsg_job *job)
5405 struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
5406 struct lpfc_hba *phba = vport->phba;
5407 struct fc_bsg_request *bsg_request = job->request;
5408 struct fc_bsg_reply *bsg_reply = job->reply;
5409 struct get_cgnbuf_info_req *cgnbuf_req;
5410 struct lpfc_cgn_info *cp;
5415 if (job->request_len < sizeof(struct fc_bsg_request) +
5416 sizeof(struct get_cgnbuf_info_req)) {
5421 if (!phba->sli4_hba.pc_sli4_params.cmf) {
5426 if (!phba->cgn_i || !phba->cgn_i->virt) {
5431 cp = phba->cgn_i->virt;
5432 if (cp->cgn_info_version < LPFC_CGN_INFO_V3) {
5437 cgnbuf_req = (struct get_cgnbuf_info_req *)
5438 bsg_request->rqst_data.h_vendor.vendor_cmd;
5440 /* For reset or size == 0 */
5441 bsg_reply->reply_payload_rcv_len = 0;
5443 if (cgnbuf_req->reset == LPFC_BSG_CGN_RESET_STAT) {
5444 lpfc_init_congestion_stat(phba);
5448 /* We don't want to include the CRC at the end */
5449 cinfosz = sizeof(struct lpfc_cgn_info) - sizeof(uint32_t);
5451 size = cgnbuf_req->read_size;
5455 if (size < cinfosz) {
5456 /* Just copy back what we can */
5461 /* Allocate memory to read congestion info */
5462 cgn_buff = vmalloc(cinfosz);
5468 memcpy(cgn_buff, cp, cinfosz);
5470 bsg_reply->reply_payload_rcv_len =
5471 sg_copy_from_buffer(job->reply_payload.sg_list,
5472 job->reply_payload.sg_cnt,
5478 bsg_reply->result = rc;
5480 bsg_job_done(job, bsg_reply->result,
5481 bsg_reply->reply_payload_rcv_len);
5483 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
5484 "2724 GET CGNBUF error: %d\n", rc);
5489 * lpfc_bsg_hst_vendor - process a vendor-specific fc_bsg_job
5490 * @job: fc_bsg_job to handle
5493 lpfc_bsg_hst_vendor(struct bsg_job *job)
5495 struct fc_bsg_request *bsg_request = job->request;
5496 struct fc_bsg_reply *bsg_reply = job->reply;
5497 int command = bsg_request->rqst_data.h_vendor.vendor_cmd[0];
5501 case LPFC_BSG_VENDOR_SET_CT_EVENT:
5502 rc = lpfc_bsg_hba_set_event(job);
5504 case LPFC_BSG_VENDOR_GET_CT_EVENT:
5505 rc = lpfc_bsg_hba_get_event(job);
5507 case LPFC_BSG_VENDOR_SEND_MGMT_RESP:
5508 rc = lpfc_bsg_send_mgmt_rsp(job);
5510 case LPFC_BSG_VENDOR_DIAG_MODE:
5511 rc = lpfc_bsg_diag_loopback_mode(job);
5513 case LPFC_BSG_VENDOR_DIAG_MODE_END:
5514 rc = lpfc_sli4_bsg_diag_mode_end(job);
5516 case LPFC_BSG_VENDOR_DIAG_RUN_LOOPBACK:
5517 rc = lpfc_bsg_diag_loopback_run(job);
5519 case LPFC_BSG_VENDOR_LINK_DIAG_TEST:
5520 rc = lpfc_sli4_bsg_link_diag_test(job);
5522 case LPFC_BSG_VENDOR_GET_MGMT_REV:
5523 rc = lpfc_bsg_get_dfc_rev(job);
5525 case LPFC_BSG_VENDOR_MBOX:
5526 rc = lpfc_bsg_mbox_cmd(job);
5528 case LPFC_BSG_VENDOR_FORCED_LINK_SPEED:
5529 rc = lpfc_forced_link_speed(job);
5531 case LPFC_BSG_VENDOR_RAS_GET_LWPD:
5532 rc = lpfc_bsg_get_ras_lwpd(job);
5534 case LPFC_BSG_VENDOR_RAS_GET_FWLOG:
5535 rc = lpfc_bsg_get_ras_fwlog(job);
5537 case LPFC_BSG_VENDOR_RAS_GET_CONFIG:
5538 rc = lpfc_bsg_get_ras_config(job);
5540 case LPFC_BSG_VENDOR_RAS_SET_CONFIG:
5541 rc = lpfc_bsg_set_ras_config(job);
5543 case LPFC_BSG_VENDOR_GET_TRUNK_INFO:
5544 rc = lpfc_get_trunk_info(job);
5546 case LPFC_BSG_VENDOR_GET_CGNBUF_INFO:
5547 rc = lpfc_get_cgnbuf_info(job);
5551 bsg_reply->reply_payload_rcv_len = 0;
5552 /* make error code available to userspace */
5553 bsg_reply->result = rc;
5561 * lpfc_bsg_request - handle a bsg request from the FC transport
5562 * @job: bsg_job to handle
5565 lpfc_bsg_request(struct bsg_job *job)
5567 struct fc_bsg_request *bsg_request = job->request;
5568 struct fc_bsg_reply *bsg_reply = job->reply;
5572 msgcode = bsg_request->msgcode;
5574 case FC_BSG_HST_VENDOR:
5575 rc = lpfc_bsg_hst_vendor(job);
5577 case FC_BSG_RPT_ELS:
5578 rc = lpfc_bsg_rport_els(job);
5581 rc = lpfc_bsg_send_mgmt_cmd(job);
5585 bsg_reply->reply_payload_rcv_len = 0;
5586 /* make error code available to userspace */
5587 bsg_reply->result = rc;
5595 * lpfc_bsg_timeout - handle timeout of a bsg request from the FC transport
5596 * @job: bsg_job that has timed out
5598 * This function just aborts the job's IOCB. The aborted IOCB will return to
5599 * the waiting function which will handle passing the error back to userspace
5602 lpfc_bsg_timeout(struct bsg_job *job)
5604 struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
5605 struct lpfc_hba *phba = vport->phba;
5606 struct lpfc_iocbq *cmdiocb;
5607 struct lpfc_sli_ring *pring;
5608 struct bsg_job_data *dd_data;
5609 unsigned long flags;
5611 LIST_HEAD(completions);
5612 struct lpfc_iocbq *check_iocb, *next_iocb;
5614 pring = lpfc_phba_elsring(phba);
5615 if (unlikely(!pring))
5618 /* if job's driver data is NULL, the command completed or is in the
5619 * the process of completing. In this case, return status to request
5620 * so the timeout is retried. This avoids double completion issues
5621 * and the request will be pulled off the timer queue when the
5622 * command's completion handler executes. Otherwise, prevent the
5623 * command's completion handler from executing the job done callback
5624 * and continue processing to abort the outstanding the command.
5627 spin_lock_irqsave(&phba->ct_ev_lock, flags);
5628 dd_data = (struct bsg_job_data *)job->dd_data;
5630 dd_data->set_job = NULL;
5631 job->dd_data = NULL;
5633 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5637 switch (dd_data->type) {
5639 /* Check to see if IOCB was issued to the port or not. If not,
5640 * remove it from the txq queue and call cancel iocbs.
5641 * Otherwise, call abort iotag
5643 cmdiocb = dd_data->context_un.iocb.cmdiocbq;
5644 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5646 spin_lock_irqsave(&phba->hbalock, flags);
5647 /* make sure the I/O abort window is still open */
5648 if (!(cmdiocb->cmd_flag & LPFC_IO_CMD_OUTSTANDING)) {
5649 spin_unlock_irqrestore(&phba->hbalock, flags);
5652 list_for_each_entry_safe(check_iocb, next_iocb, &pring->txq,
5654 if (check_iocb == cmdiocb) {
5655 list_move_tail(&check_iocb->list, &completions);
5659 if (list_empty(&completions))
5660 lpfc_sli_issue_abort_iotag(phba, pring, cmdiocb, NULL);
5661 spin_unlock_irqrestore(&phba->hbalock, flags);
5662 if (!list_empty(&completions)) {
5663 lpfc_sli_cancel_iocbs(phba, &completions,
5664 IOSTAT_LOCAL_REJECT,
5670 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5674 /* Update the ext buf ctx state if needed */
5676 if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_PORT)
5677 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_ABTS;
5678 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5681 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5685 /* scsi transport fc fc_bsg_job_timeout expects a zero return code,
5686 * otherwise an error message will be displayed on the console
5687 * so always return success (zero)