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 = 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(*cmdwqe));
3173 if (phba->sli_rev < LPFC_SLI_REV4) {
3174 rspwqe = &rspiocbq->wqe;
3175 memset(rspwqe, 0, sizeof(*rspwqe));
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 if (phba->cfg_vmid_app_header)
3212 cmdiocbq->cmd_flag |= LPFC_IO_VMID;
3214 cmdiocbq->vport = phba->pport;
3215 cmdiocbq->cmd_cmpl = NULL;
3216 cmdiocbq->bpl_dmabuf = txbmp;
3218 if (phba->sli_rev < LPFC_SLI_REV4) {
3219 lpfc_sli_prep_xmit_seq64(phba, cmdiocbq, txbmp, 0, txxri,
3220 num_bde, FC_RCTL_DD_UNSOL_CTL, 1,
3221 CMD_XMIT_SEQUENCE64_CX);
3224 lpfc_sli_prep_xmit_seq64(phba, cmdiocbq, txbmp,
3225 phba->sli4_hba.rpi_ids[rpi], 0xffff,
3226 full_size, FC_RCTL_DD_UNSOL_CTL, 1,
3227 CMD_XMIT_SEQUENCE64_WQE);
3228 cmdiocbq->sli4_xritag = NO_XRI;
3231 iocb_stat = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq,
3232 rspiocbq, (phba->fc_ratov * 2) +
3234 if (iocb_stat != IOCB_SUCCESS ||
3235 (phba->sli_rev < LPFC_SLI_REV4 &&
3236 (get_job_ulpstatus(phba, rspiocbq) != IOSTAT_SUCCESS))) {
3237 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3238 "3126 Failed loopback test issue iocb: "
3239 "iocb_stat:x%x\n", iocb_stat);
3241 goto err_loopback_test_exit;
3245 time_left = wait_event_interruptible_timeout(
3246 evt->wq, !list_empty(&evt->events_to_see),
3247 msecs_to_jiffies(1000 *
3248 ((phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT)));
3250 if (list_empty(&evt->events_to_see)) {
3251 rc = (time_left) ? -EINTR : -ETIMEDOUT;
3252 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3253 "3125 Not receiving unsolicited event, "
3256 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3257 list_move(evt->events_to_see.prev, &evt->events_to_get);
3258 evdat = list_entry(evt->events_to_get.prev,
3259 typeof(*evdat), node);
3260 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3261 rx_databuf = evdat->data;
3262 if (evdat->len != full_size) {
3263 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3264 "1603 Loopback test did not receive expected "
3265 "data length. actual length 0x%x expected "
3267 evdat->len, full_size);
3269 } else if (rx_databuf == NULL)
3273 /* skip over elx loopback header */
3274 rx_databuf += ELX_LOOPBACK_HEADER_SZ;
3275 bsg_reply->reply_payload_rcv_len =
3276 sg_copy_from_buffer(job->reply_payload.sg_list,
3277 job->reply_payload.sg_cnt,
3279 bsg_reply->reply_payload_rcv_len = size;
3283 err_loopback_test_exit:
3284 lpfcdiag_loop_self_unreg(phba, rpi);
3286 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3287 lpfc_bsg_event_unref(evt); /* release ref */
3288 lpfc_bsg_event_unref(evt); /* delete */
3289 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3291 if ((cmdiocbq != NULL) && (iocb_stat != IOCB_TIMEDOUT))
3292 lpfc_sli_release_iocbq(phba, cmdiocbq);
3294 if (rspiocbq != NULL)
3295 lpfc_sli_release_iocbq(phba, rspiocbq);
3297 if (txbmp != NULL) {
3298 if (txbpl != NULL) {
3299 if (txbuffer != NULL)
3300 diag_cmd_data_free(phba, txbuffer);
3301 lpfc_mbuf_free(phba, txbmp->virt, txbmp->phys);
3308 /* make error code available to userspace */
3309 bsg_reply->result = rc;
3310 job->dd_data = NULL;
3311 /* complete the job back to userspace if no error */
3312 if (rc == IOCB_SUCCESS)
3313 bsg_job_done(job, bsg_reply->result,
3314 bsg_reply->reply_payload_rcv_len);
3319 * lpfc_bsg_get_dfc_rev - process a GET_DFC_REV bsg vendor command
3320 * @job: GET_DFC_REV fc_bsg_job
3323 lpfc_bsg_get_dfc_rev(struct bsg_job *job)
3325 struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
3326 struct fc_bsg_reply *bsg_reply = job->reply;
3327 struct lpfc_hba *phba = vport->phba;
3328 struct get_mgmt_rev_reply *event_reply;
3331 if (job->request_len <
3332 sizeof(struct fc_bsg_request) + sizeof(struct get_mgmt_rev)) {
3333 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3334 "2740 Received GET_DFC_REV request below "
3340 event_reply = (struct get_mgmt_rev_reply *)
3341 bsg_reply->reply_data.vendor_reply.vendor_rsp;
3343 if (job->reply_len < sizeof(*bsg_reply) + sizeof(*event_reply)) {
3344 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3345 "2741 Received GET_DFC_REV reply below "
3351 event_reply->info.a_Major = MANAGEMENT_MAJOR_REV;
3352 event_reply->info.a_Minor = MANAGEMENT_MINOR_REV;
3354 bsg_reply->result = rc;
3356 bsg_job_done(job, bsg_reply->result,
3357 bsg_reply->reply_payload_rcv_len);
3362 * lpfc_bsg_issue_mbox_cmpl - lpfc_bsg_issue_mbox mbox completion handler
3363 * @phba: Pointer to HBA context object.
3364 * @pmboxq: Pointer to mailbox command.
3366 * This is completion handler function for mailbox commands issued from
3367 * lpfc_bsg_issue_mbox function. This function is called by the
3368 * mailbox event handler function with no lock held. This function
3369 * will wake up thread waiting on the wait queue pointed by dd_data
3373 lpfc_bsg_issue_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
3375 struct bsg_job_data *dd_data;
3376 struct fc_bsg_reply *bsg_reply;
3377 struct bsg_job *job;
3379 unsigned long flags;
3380 uint8_t *pmb, *pmb_buf;
3382 dd_data = pmboxq->ctx_u.dd_data;
3385 * The outgoing buffer is readily referred from the dma buffer,
3386 * just need to get header part from mailboxq structure.
3388 pmb = (uint8_t *)&pmboxq->u.mb;
3389 pmb_buf = (uint8_t *)dd_data->context_un.mbox.mb;
3390 memcpy(pmb_buf, pmb, sizeof(MAILBOX_t));
3392 /* Determine if job has been aborted */
3394 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3395 job = dd_data->set_job;
3397 /* Prevent timeout handling from trying to abort job */
3398 job->dd_data = NULL;
3400 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3402 /* Copy the mailbox data to the job if it is still active */
3405 bsg_reply = job->reply;
3406 size = job->reply_payload.payload_len;
3407 bsg_reply->reply_payload_rcv_len =
3408 sg_copy_from_buffer(job->reply_payload.sg_list,
3409 job->reply_payload.sg_cnt,
3413 dd_data->set_job = NULL;
3414 mempool_free(dd_data->context_un.mbox.pmboxq, phba->mbox_mem_pool);
3415 lpfc_bsg_dma_page_free(phba, dd_data->context_un.mbox.dmabuffers);
3418 /* Complete the job if the job is still active */
3421 bsg_reply->result = 0;
3422 bsg_job_done(job, bsg_reply->result,
3423 bsg_reply->reply_payload_rcv_len);
3429 * lpfc_bsg_check_cmd_access - test for a supported mailbox command
3430 * @phba: Pointer to HBA context object.
3431 * @mb: Pointer to a mailbox object.
3432 * @vport: Pointer to a vport object.
3434 * Some commands require the port to be offline, some may not be called from
3437 static int lpfc_bsg_check_cmd_access(struct lpfc_hba *phba,
3438 MAILBOX_t *mb, struct lpfc_vport *vport)
3440 /* return negative error values for bsg job */
3441 switch (mb->mbxCommand) {
3445 case MBX_CONFIG_LINK:
3446 case MBX_CONFIG_RING:
3447 case MBX_RESET_RING:
3448 case MBX_UNREG_LOGIN:
3450 case MBX_DUMP_CONTEXT:
3454 if (!test_bit(FC_OFFLINE_MODE, &vport->fc_flag)) {
3455 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3456 "2743 Command 0x%x is illegal in on-line "
3463 case MBX_WRITE_VPARMS:
3466 case MBX_READ_CONFIG:
3467 case MBX_READ_RCONFIG:
3468 case MBX_READ_STATUS:
3471 case MBX_READ_LNK_STAT:
3472 case MBX_DUMP_MEMORY:
3474 case MBX_UPDATE_CFG:
3475 case MBX_KILL_BOARD:
3476 case MBX_READ_TOPOLOGY:
3478 case MBX_LOAD_EXP_ROM:
3480 case MBX_DEL_LD_ENTRY:
3483 case MBX_SLI4_CONFIG:
3484 case MBX_READ_EVENT_LOG:
3485 case MBX_READ_EVENT_LOG_STATUS:
3486 case MBX_WRITE_EVENT_LOG:
3487 case MBX_PORT_CAPABILITIES:
3488 case MBX_PORT_IOV_CONTROL:
3489 case MBX_RUN_BIU_DIAG64:
3491 case MBX_SET_VARIABLE:
3492 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3493 "1226 mbox: set_variable 0x%x, 0x%x\n",
3495 mb->un.varWords[1]);
3497 case MBX_READ_SPARM64:
3499 case MBX_REG_LOGIN64:
3500 case MBX_CONFIG_PORT:
3501 case MBX_RUN_BIU_DIAG:
3503 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3504 "2742 Unknown Command 0x%x\n",
3513 * lpfc_bsg_mbox_ext_session_reset - clean up context of multi-buffer mbox session
3514 * @phba: Pointer to HBA context object.
3516 * This is routine clean up and reset BSG handling of multi-buffer mbox
3520 lpfc_bsg_mbox_ext_session_reset(struct lpfc_hba *phba)
3522 if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_IDLE)
3525 /* free all memory, including dma buffers */
3526 lpfc_bsg_dma_page_list_free(phba,
3527 &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
3528 lpfc_bsg_dma_page_free(phba, phba->mbox_ext_buf_ctx.mbx_dmabuf);
3529 /* multi-buffer write mailbox command pass-through complete */
3530 memset((char *)&phba->mbox_ext_buf_ctx, 0,
3531 sizeof(struct lpfc_mbox_ext_buf_ctx));
3532 INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
3538 * lpfc_bsg_issue_mbox_ext_handle_job - job handler for multi-buffer mbox cmpl
3539 * @phba: Pointer to HBA context object.
3540 * @pmboxq: Pointer to mailbox command.
3542 * This is routine handles BSG job for mailbox commands completions with
3543 * multiple external buffers.
3545 static struct bsg_job *
3546 lpfc_bsg_issue_mbox_ext_handle_job(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
3548 struct bsg_job_data *dd_data;
3549 struct bsg_job *job;
3550 struct fc_bsg_reply *bsg_reply;
3551 uint8_t *pmb, *pmb_buf;
3552 unsigned long flags;
3555 struct lpfc_dmabuf *dmabuf;
3556 struct lpfc_sli_config_mbox *sli_cfg_mbx;
3559 dd_data = pmboxq->ctx_u.dd_data;
3561 /* Determine if job has been aborted */
3562 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3563 job = dd_data->set_job;
3565 bsg_reply = job->reply;
3566 /* Prevent timeout handling from trying to abort job */
3567 job->dd_data = NULL;
3569 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3572 * The outgoing buffer is readily referred from the dma buffer,
3573 * just need to get header part from mailboxq structure.
3576 pmb = (uint8_t *)&pmboxq->u.mb;
3577 pmb_buf = (uint8_t *)dd_data->context_un.mbox.mb;
3578 /* Copy the byte swapped response mailbox back to the user */
3579 memcpy(pmb_buf, pmb, sizeof(MAILBOX_t));
3580 /* if there is any non-embedded extended data copy that too */
3581 dmabuf = phba->mbox_ext_buf_ctx.mbx_dmabuf;
3582 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
3583 if (!bsg_bf_get(lpfc_mbox_hdr_emb,
3584 &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) {
3585 pmbx = (uint8_t *)dmabuf->virt;
3586 /* byte swap the extended data following the mailbox command */
3587 lpfc_sli_pcimem_bcopy(&pmbx[sizeof(MAILBOX_t)],
3588 &pmbx[sizeof(MAILBOX_t)],
3589 sli_cfg_mbx->un.sli_config_emb0_subsys.mse[0].buf_len);
3592 /* Complete the job if the job is still active */
3595 size = job->reply_payload.payload_len;
3596 bsg_reply->reply_payload_rcv_len =
3597 sg_copy_from_buffer(job->reply_payload.sg_list,
3598 job->reply_payload.sg_cnt,
3601 /* result for successful */
3602 bsg_reply->result = 0;
3604 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3605 "2937 SLI_CONFIG ext-buffer mailbox command "
3606 "(x%x/x%x) complete bsg job done, bsize:%d\n",
3607 phba->mbox_ext_buf_ctx.nembType,
3608 phba->mbox_ext_buf_ctx.mboxType, size);
3609 lpfc_idiag_mbxacc_dump_bsg_mbox(phba,
3610 phba->mbox_ext_buf_ctx.nembType,
3611 phba->mbox_ext_buf_ctx.mboxType,
3612 dma_ebuf, sta_pos_addr,
3613 phba->mbox_ext_buf_ctx.mbx_dmabuf, 0);
3615 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3616 "2938 SLI_CONFIG ext-buffer mailbox "
3617 "command (x%x/x%x) failure, rc:x%x\n",
3618 phba->mbox_ext_buf_ctx.nembType,
3619 phba->mbox_ext_buf_ctx.mboxType, rc);
3624 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_DONE;
3630 * lpfc_bsg_issue_read_mbox_ext_cmpl - compl handler for multi-buffer read mbox
3631 * @phba: Pointer to HBA context object.
3632 * @pmboxq: Pointer to mailbox command.
3634 * This is completion handler function for mailbox read commands with multiple
3638 lpfc_bsg_issue_read_mbox_ext_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
3640 struct bsg_job *job;
3641 struct fc_bsg_reply *bsg_reply;
3643 job = lpfc_bsg_issue_mbox_ext_handle_job(phba, pmboxq);
3645 /* handle the BSG job with mailbox command */
3647 pmboxq->u.mb.mbxStatus = MBXERR_ERROR;
3649 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3650 "2939 SLI_CONFIG ext-buffer rd mailbox command "
3651 "complete, ctxState:x%x, mbxStatus:x%x\n",
3652 phba->mbox_ext_buf_ctx.state, pmboxq->u.mb.mbxStatus);
3654 if (pmboxq->u.mb.mbxStatus || phba->mbox_ext_buf_ctx.numBuf == 1)
3655 lpfc_bsg_mbox_ext_session_reset(phba);
3657 /* free base driver mailbox structure memory */
3658 mempool_free(pmboxq, phba->mbox_mem_pool);
3660 /* if the job is still active, call job done */
3662 bsg_reply = job->reply;
3663 bsg_job_done(job, bsg_reply->result,
3664 bsg_reply->reply_payload_rcv_len);
3670 * lpfc_bsg_issue_write_mbox_ext_cmpl - cmpl handler for multi-buffer write mbox
3671 * @phba: Pointer to HBA context object.
3672 * @pmboxq: Pointer to mailbox command.
3674 * This is completion handler function for mailbox write commands with multiple
3678 lpfc_bsg_issue_write_mbox_ext_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
3680 struct bsg_job *job;
3681 struct fc_bsg_reply *bsg_reply;
3683 job = lpfc_bsg_issue_mbox_ext_handle_job(phba, pmboxq);
3685 /* handle the BSG job with the mailbox command */
3687 pmboxq->u.mb.mbxStatus = MBXERR_ERROR;
3689 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3690 "2940 SLI_CONFIG ext-buffer wr mailbox command "
3691 "complete, ctxState:x%x, mbxStatus:x%x\n",
3692 phba->mbox_ext_buf_ctx.state, pmboxq->u.mb.mbxStatus);
3694 /* free all memory, including dma buffers */
3695 mempool_free(pmboxq, phba->mbox_mem_pool);
3696 lpfc_bsg_mbox_ext_session_reset(phba);
3698 /* if the job is still active, call job done */
3700 bsg_reply = job->reply;
3701 bsg_job_done(job, bsg_reply->result,
3702 bsg_reply->reply_payload_rcv_len);
3709 lpfc_bsg_sli_cfg_dma_desc_setup(struct lpfc_hba *phba, enum nemb_type nemb_tp,
3710 uint32_t index, struct lpfc_dmabuf *mbx_dmabuf,
3711 struct lpfc_dmabuf *ext_dmabuf)
3713 struct lpfc_sli_config_mbox *sli_cfg_mbx;
3715 /* pointer to the start of mailbox command */
3716 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)mbx_dmabuf->virt;
3718 if (nemb_tp == nemb_mse) {
3720 sli_cfg_mbx->un.sli_config_emb0_subsys.
3722 putPaddrHigh(mbx_dmabuf->phys +
3724 sli_cfg_mbx->un.sli_config_emb0_subsys.
3726 putPaddrLow(mbx_dmabuf->phys +
3728 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3729 "2943 SLI_CONFIG(mse)[%d], "
3730 "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3732 sli_cfg_mbx->un.sli_config_emb0_subsys.
3734 sli_cfg_mbx->un.sli_config_emb0_subsys.
3736 sli_cfg_mbx->un.sli_config_emb0_subsys.
3739 sli_cfg_mbx->un.sli_config_emb0_subsys.
3741 putPaddrHigh(ext_dmabuf->phys);
3742 sli_cfg_mbx->un.sli_config_emb0_subsys.
3744 putPaddrLow(ext_dmabuf->phys);
3745 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3746 "2944 SLI_CONFIG(mse)[%d], "
3747 "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3749 sli_cfg_mbx->un.sli_config_emb0_subsys.
3751 sli_cfg_mbx->un.sli_config_emb0_subsys.
3753 sli_cfg_mbx->un.sli_config_emb0_subsys.
3758 sli_cfg_mbx->un.sli_config_emb1_subsys.
3760 putPaddrHigh(mbx_dmabuf->phys +
3762 sli_cfg_mbx->un.sli_config_emb1_subsys.
3764 putPaddrLow(mbx_dmabuf->phys +
3766 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3767 "3007 SLI_CONFIG(hbd)[%d], "
3768 "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3770 bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
3772 sli_config_emb1_subsys.hbd[index]),
3773 sli_cfg_mbx->un.sli_config_emb1_subsys.
3775 sli_cfg_mbx->un.sli_config_emb1_subsys.
3779 sli_cfg_mbx->un.sli_config_emb1_subsys.
3781 putPaddrHigh(ext_dmabuf->phys);
3782 sli_cfg_mbx->un.sli_config_emb1_subsys.
3784 putPaddrLow(ext_dmabuf->phys);
3785 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3786 "3008 SLI_CONFIG(hbd)[%d], "
3787 "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3789 bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
3791 sli_config_emb1_subsys.hbd[index]),
3792 sli_cfg_mbx->un.sli_config_emb1_subsys.
3794 sli_cfg_mbx->un.sli_config_emb1_subsys.
3802 * lpfc_bsg_sli_cfg_read_cmd_ext - sli_config non-embedded mailbox cmd read
3803 * @phba: Pointer to HBA context object.
3804 * @job: Pointer to the job object.
3805 * @nemb_tp: Enumerate of non-embedded mailbox command type.
3806 * @dmabuf: Pointer to a DMA buffer descriptor.
3808 * This routine performs SLI_CONFIG (0x9B) read mailbox command operation with
3809 * non-embedded external buffers.
3812 lpfc_bsg_sli_cfg_read_cmd_ext(struct lpfc_hba *phba, struct bsg_job *job,
3813 enum nemb_type nemb_tp,
3814 struct lpfc_dmabuf *dmabuf)
3816 struct fc_bsg_request *bsg_request = job->request;
3817 struct lpfc_sli_config_mbox *sli_cfg_mbx;
3818 struct dfc_mbox_req *mbox_req;
3819 struct lpfc_dmabuf *curr_dmabuf, *next_dmabuf;
3820 uint32_t ext_buf_cnt, ext_buf_index;
3821 struct lpfc_dmabuf *ext_dmabuf = NULL;
3822 struct bsg_job_data *dd_data = NULL;
3823 LPFC_MBOXQ_t *pmboxq = NULL;
3829 (struct dfc_mbox_req *)bsg_request->rqst_data.h_vendor.vendor_cmd;
3831 /* pointer to the start of mailbox command */
3832 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
3834 if (nemb_tp == nemb_mse) {
3835 ext_buf_cnt = bsg_bf_get(lpfc_mbox_hdr_mse_cnt,
3836 &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr);
3837 if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_MSE) {
3838 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3839 "2945 Handled SLI_CONFIG(mse) rd, "
3840 "ext_buf_cnt(%d) out of range(%d)\n",
3842 LPFC_MBX_SLI_CONFIG_MAX_MSE);
3846 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3847 "2941 Handled SLI_CONFIG(mse) rd, "
3848 "ext_buf_cnt:%d\n", ext_buf_cnt);
3850 /* sanity check on interface type for support */
3851 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
3852 LPFC_SLI_INTF_IF_TYPE_2) {
3856 /* nemb_tp == nemb_hbd */
3857 ext_buf_cnt = sli_cfg_mbx->un.sli_config_emb1_subsys.hbd_count;
3858 if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_HBD) {
3859 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3860 "2946 Handled SLI_CONFIG(hbd) rd, "
3861 "ext_buf_cnt(%d) out of range(%d)\n",
3863 LPFC_MBX_SLI_CONFIG_MAX_HBD);
3867 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3868 "2942 Handled SLI_CONFIG(hbd) rd, "
3869 "ext_buf_cnt:%d\n", ext_buf_cnt);
3872 /* before dma descriptor setup */
3873 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_rd, dma_mbox,
3874 sta_pre_addr, dmabuf, ext_buf_cnt);
3876 /* reject non-embedded mailbox command with none external buffer */
3877 if (ext_buf_cnt == 0) {
3880 } else if (ext_buf_cnt > 1) {
3881 /* additional external read buffers */
3882 for (i = 1; i < ext_buf_cnt; i++) {
3883 ext_dmabuf = lpfc_bsg_dma_page_alloc(phba);
3888 list_add_tail(&ext_dmabuf->list,
3889 &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
3893 /* bsg tracking structure */
3894 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
3900 /* mailbox command structure for base driver */
3901 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3906 memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
3908 /* for the first external buffer */
3909 lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, 0, dmabuf, dmabuf);
3911 /* for the rest of external buffer descriptors if any */
3912 if (ext_buf_cnt > 1) {
3914 list_for_each_entry_safe(curr_dmabuf, next_dmabuf,
3915 &phba->mbox_ext_buf_ctx.ext_dmabuf_list, list) {
3916 lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp,
3917 ext_buf_index, dmabuf,
3923 /* after dma descriptor setup */
3924 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_rd, dma_mbox,
3925 sta_pos_addr, dmabuf, ext_buf_cnt);
3927 /* construct base driver mbox command */
3928 pmb = &pmboxq->u.mb;
3929 pmbx = (uint8_t *)dmabuf->virt;
3930 memcpy(pmb, pmbx, sizeof(*pmb));
3931 pmb->mbxOwner = OWN_HOST;
3932 pmboxq->vport = phba->pport;
3934 /* multi-buffer handling context */
3935 phba->mbox_ext_buf_ctx.nembType = nemb_tp;
3936 phba->mbox_ext_buf_ctx.mboxType = mbox_rd;
3937 phba->mbox_ext_buf_ctx.numBuf = ext_buf_cnt;
3938 phba->mbox_ext_buf_ctx.mbxTag = mbox_req->extMboxTag;
3939 phba->mbox_ext_buf_ctx.seqNum = mbox_req->extSeqNum;
3940 phba->mbox_ext_buf_ctx.mbx_dmabuf = dmabuf;
3942 /* callback for multi-buffer read mailbox command */
3943 pmboxq->mbox_cmpl = lpfc_bsg_issue_read_mbox_ext_cmpl;
3945 /* context fields to callback function */
3946 pmboxq->ctx_u.dd_data = dd_data;
3947 dd_data->type = TYPE_MBOX;
3948 dd_data->set_job = job;
3949 dd_data->context_un.mbox.pmboxq = pmboxq;
3950 dd_data->context_un.mbox.mb = (MAILBOX_t *)pmbx;
3951 job->dd_data = dd_data;
3954 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
3957 * Non-embedded mailbox subcommand data gets byte swapped here because
3958 * the lower level driver code only does the first 64 mailbox words.
3960 if ((!bsg_bf_get(lpfc_mbox_hdr_emb,
3961 &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) &&
3962 (nemb_tp == nemb_mse))
3963 lpfc_sli_pcimem_bcopy(&pmbx[sizeof(MAILBOX_t)],
3964 &pmbx[sizeof(MAILBOX_t)],
3965 sli_cfg_mbx->un.sli_config_emb0_subsys.
3968 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
3969 if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
3970 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3971 "2947 Issued SLI_CONFIG ext-buffer "
3972 "mailbox command, rc:x%x\n", rc);
3973 return SLI_CONFIG_HANDLED;
3975 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3976 "2948 Failed to issue SLI_CONFIG ext-buffer "
3977 "mailbox command, rc:x%x\n", rc);
3982 mempool_free(pmboxq, phba->mbox_mem_pool);
3983 lpfc_bsg_dma_page_list_free(phba,
3984 &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
3986 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_IDLE;
3991 * lpfc_bsg_sli_cfg_write_cmd_ext - sli_config non-embedded mailbox cmd write
3992 * @phba: Pointer to HBA context object.
3993 * @job: Pointer to the job object.
3994 * @nemb_tp: Enumerate of non-embedded mailbox command type.
3995 * @dmabuf: Pointer to a DMA buffer descriptor.
3997 * This routine performs SLI_CONFIG (0x9B) write mailbox command operation with
3998 * non-embedded external buffers.
4001 lpfc_bsg_sli_cfg_write_cmd_ext(struct lpfc_hba *phba, struct bsg_job *job,
4002 enum nemb_type nemb_tp,
4003 struct lpfc_dmabuf *dmabuf)
4005 struct fc_bsg_request *bsg_request = job->request;
4006 struct fc_bsg_reply *bsg_reply = job->reply;
4007 struct dfc_mbox_req *mbox_req;
4008 struct lpfc_sli_config_mbox *sli_cfg_mbx;
4009 uint32_t ext_buf_cnt;
4010 struct bsg_job_data *dd_data = NULL;
4011 LPFC_MBOXQ_t *pmboxq = NULL;
4014 int rc = SLI_CONFIG_NOT_HANDLED, i;
4017 (struct dfc_mbox_req *)bsg_request->rqst_data.h_vendor.vendor_cmd;
4019 /* pointer to the start of mailbox command */
4020 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
4022 if (nemb_tp == nemb_mse) {
4023 ext_buf_cnt = bsg_bf_get(lpfc_mbox_hdr_mse_cnt,
4024 &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr);
4025 if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_MSE) {
4026 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4027 "2953 Failed SLI_CONFIG(mse) wr, "
4028 "ext_buf_cnt(%d) out of range(%d)\n",
4030 LPFC_MBX_SLI_CONFIG_MAX_MSE);
4033 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4034 "2949 Handled SLI_CONFIG(mse) wr, "
4035 "ext_buf_cnt:%d\n", ext_buf_cnt);
4037 /* sanity check on interface type for support */
4038 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
4039 LPFC_SLI_INTF_IF_TYPE_2)
4041 /* nemb_tp == nemb_hbd */
4042 ext_buf_cnt = sli_cfg_mbx->un.sli_config_emb1_subsys.hbd_count;
4043 if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_HBD) {
4044 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4045 "2954 Failed SLI_CONFIG(hbd) wr, "
4046 "ext_buf_cnt(%d) out of range(%d)\n",
4048 LPFC_MBX_SLI_CONFIG_MAX_HBD);
4051 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4052 "2950 Handled SLI_CONFIG(hbd) wr, "
4053 "ext_buf_cnt:%d\n", ext_buf_cnt);
4056 /* before dma buffer descriptor setup */
4057 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_wr, dma_mbox,
4058 sta_pre_addr, dmabuf, ext_buf_cnt);
4060 if (ext_buf_cnt == 0)
4063 /* for the first external buffer */
4064 lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, 0, dmabuf, dmabuf);
4066 /* after dma descriptor setup */
4067 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_wr, dma_mbox,
4068 sta_pos_addr, dmabuf, ext_buf_cnt);
4070 /* log for looking forward */
4071 for (i = 1; i < ext_buf_cnt; i++) {
4072 if (nemb_tp == nemb_mse)
4073 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4074 "2951 SLI_CONFIG(mse), buf[%d]-length:%d\n",
4075 i, sli_cfg_mbx->un.sli_config_emb0_subsys.
4078 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4079 "2952 SLI_CONFIG(hbd), buf[%d]-length:%d\n",
4080 i, bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
4081 &sli_cfg_mbx->un.sli_config_emb1_subsys.
4085 /* multi-buffer handling context */
4086 phba->mbox_ext_buf_ctx.nembType = nemb_tp;
4087 phba->mbox_ext_buf_ctx.mboxType = mbox_wr;
4088 phba->mbox_ext_buf_ctx.numBuf = ext_buf_cnt;
4089 phba->mbox_ext_buf_ctx.mbxTag = mbox_req->extMboxTag;
4090 phba->mbox_ext_buf_ctx.seqNum = mbox_req->extSeqNum;
4091 phba->mbox_ext_buf_ctx.mbx_dmabuf = dmabuf;
4093 if (ext_buf_cnt == 1) {
4094 /* bsg tracking structure */
4095 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
4101 /* mailbox command structure for base driver */
4102 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4107 memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
4108 pmb = &pmboxq->u.mb;
4109 mbx = (uint8_t *)dmabuf->virt;
4110 memcpy(pmb, mbx, sizeof(*pmb));
4111 pmb->mbxOwner = OWN_HOST;
4112 pmboxq->vport = phba->pport;
4114 /* callback for multi-buffer read mailbox command */
4115 pmboxq->mbox_cmpl = lpfc_bsg_issue_write_mbox_ext_cmpl;
4117 /* context fields to callback function */
4118 pmboxq->ctx_u.dd_data = dd_data;
4119 dd_data->type = TYPE_MBOX;
4120 dd_data->set_job = job;
4121 dd_data->context_un.mbox.pmboxq = pmboxq;
4122 dd_data->context_un.mbox.mb = (MAILBOX_t *)mbx;
4123 job->dd_data = dd_data;
4127 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
4128 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
4129 if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
4130 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4131 "2955 Issued SLI_CONFIG ext-buffer "
4132 "mailbox command, rc:x%x\n", rc);
4133 return SLI_CONFIG_HANDLED;
4135 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4136 "2956 Failed to issue SLI_CONFIG ext-buffer "
4137 "mailbox command, rc:x%x\n", rc);
4142 /* wait for additional external buffers */
4144 bsg_reply->result = 0;
4145 bsg_job_done(job, bsg_reply->result,
4146 bsg_reply->reply_payload_rcv_len);
4147 return SLI_CONFIG_HANDLED;
4151 mempool_free(pmboxq, phba->mbox_mem_pool);
4158 * lpfc_bsg_handle_sli_cfg_mbox - handle sli-cfg mailbox cmd with ext buffer
4159 * @phba: Pointer to HBA context object.
4160 * @job: Pointer to the job object.
4161 * @dmabuf: Pointer to a DMA buffer descriptor.
4163 * This routine handles SLI_CONFIG (0x9B) mailbox command with non-embedded
4164 * external buffers, including both 0x9B with non-embedded MSEs and 0x9B
4165 * with embedded subsystem 0x1 and opcodes with external HBDs.
4168 lpfc_bsg_handle_sli_cfg_mbox(struct lpfc_hba *phba, struct bsg_job *job,
4169 struct lpfc_dmabuf *dmabuf)
4171 struct lpfc_sli_config_mbox *sli_cfg_mbx;
4174 int rc = SLI_CONFIG_NOT_HANDLED;
4176 /* state change on new multi-buffer pass-through mailbox command */
4177 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_HOST;
4179 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
4181 if (!bsg_bf_get(lpfc_mbox_hdr_emb,
4182 &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) {
4183 subsys = bsg_bf_get(lpfc_emb0_subcmnd_subsys,
4184 &sli_cfg_mbx->un.sli_config_emb0_subsys);
4185 opcode = bsg_bf_get(lpfc_emb0_subcmnd_opcode,
4186 &sli_cfg_mbx->un.sli_config_emb0_subsys);
4187 if (subsys == SLI_CONFIG_SUBSYS_FCOE) {
4189 case FCOE_OPCODE_READ_FCF:
4190 case FCOE_OPCODE_GET_DPORT_RESULTS:
4191 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4192 "2957 Handled SLI_CONFIG "
4193 "subsys_fcoe, opcode:x%x\n",
4195 rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
4198 case FCOE_OPCODE_ADD_FCF:
4199 case FCOE_OPCODE_SET_DPORT_MODE:
4200 case LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_STATE:
4201 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4202 "2958 Handled SLI_CONFIG "
4203 "subsys_fcoe, opcode:x%x\n",
4205 rc = lpfc_bsg_sli_cfg_write_cmd_ext(phba, job,
4209 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4210 "2959 Reject SLI_CONFIG "
4211 "subsys_fcoe, opcode:x%x\n",
4216 } else if (subsys == SLI_CONFIG_SUBSYS_COMN) {
4218 case COMN_OPCODE_GET_CNTL_ADDL_ATTRIBUTES:
4219 case COMN_OPCODE_GET_CNTL_ATTRIBUTES:
4220 case COMN_OPCODE_GET_PROFILE_CONFIG:
4221 case COMN_OPCODE_SET_FEATURES:
4222 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4223 "3106 Handled SLI_CONFIG "
4224 "subsys_comn, opcode:x%x\n",
4226 rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
4230 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4231 "3107 Reject SLI_CONFIG "
4232 "subsys_comn, opcode:x%x\n",
4238 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4239 "2977 Reject SLI_CONFIG "
4240 "subsys:x%d, opcode:x%x\n",
4245 subsys = bsg_bf_get(lpfc_emb1_subcmnd_subsys,
4246 &sli_cfg_mbx->un.sli_config_emb1_subsys);
4247 opcode = bsg_bf_get(lpfc_emb1_subcmnd_opcode,
4248 &sli_cfg_mbx->un.sli_config_emb1_subsys);
4249 if (subsys == SLI_CONFIG_SUBSYS_COMN) {
4251 case COMN_OPCODE_READ_OBJECT:
4252 case COMN_OPCODE_READ_OBJECT_LIST:
4253 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4254 "2960 Handled SLI_CONFIG "
4255 "subsys_comn, opcode:x%x\n",
4257 rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
4260 case COMN_OPCODE_WRITE_OBJECT:
4261 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4262 "2961 Handled SLI_CONFIG "
4263 "subsys_comn, opcode:x%x\n",
4265 rc = lpfc_bsg_sli_cfg_write_cmd_ext(phba, job,
4269 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4270 "2962 Not handled SLI_CONFIG "
4271 "subsys_comn, opcode:x%x\n",
4273 rc = SLI_CONFIG_NOT_HANDLED;
4277 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4278 "2978 Not handled SLI_CONFIG "
4279 "subsys:x%d, opcode:x%x\n",
4281 rc = SLI_CONFIG_NOT_HANDLED;
4285 /* state reset on not handled new multi-buffer mailbox command */
4286 if (rc != SLI_CONFIG_HANDLED)
4287 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_IDLE;
4293 * lpfc_bsg_mbox_ext_abort - request to abort mbox command with ext buffers
4294 * @phba: Pointer to HBA context object.
4296 * This routine is for requesting to abort a pass-through mailbox command with
4297 * multiple external buffers due to error condition.
4300 lpfc_bsg_mbox_ext_abort(struct lpfc_hba *phba)
4302 if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_PORT)
4303 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_ABTS;
4305 lpfc_bsg_mbox_ext_session_reset(phba);
4310 * lpfc_bsg_read_ebuf_get - get the next mailbox read external buffer
4311 * @phba: Pointer to HBA context object.
4312 * @job: Pointer to the job object.
4314 * This routine extracts the next mailbox read external buffer back to
4315 * user space through BSG.
4318 lpfc_bsg_read_ebuf_get(struct lpfc_hba *phba, struct bsg_job *job)
4320 struct fc_bsg_reply *bsg_reply = job->reply;
4321 struct lpfc_sli_config_mbox *sli_cfg_mbx;
4322 struct lpfc_dmabuf *dmabuf;
4327 index = phba->mbox_ext_buf_ctx.seqNum;
4328 phba->mbox_ext_buf_ctx.seqNum++;
4330 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)
4331 phba->mbox_ext_buf_ctx.mbx_dmabuf->virt;
4333 if (phba->mbox_ext_buf_ctx.nembType == nemb_mse) {
4334 size = bsg_bf_get(lpfc_mbox_sli_config_mse_len,
4335 &sli_cfg_mbx->un.sli_config_emb0_subsys.mse[index]);
4336 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4337 "2963 SLI_CONFIG (mse) ext-buffer rd get "
4338 "buffer[%d], size:%d\n", index, size);
4340 size = bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
4341 &sli_cfg_mbx->un.sli_config_emb1_subsys.hbd[index]);
4342 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4343 "2964 SLI_CONFIG (hbd) ext-buffer rd get "
4344 "buffer[%d], size:%d\n", index, size);
4346 if (list_empty(&phba->mbox_ext_buf_ctx.ext_dmabuf_list))
4348 dmabuf = list_first_entry(&phba->mbox_ext_buf_ctx.ext_dmabuf_list,
4349 struct lpfc_dmabuf, list);
4350 list_del_init(&dmabuf->list);
4352 /* after dma buffer descriptor setup */
4353 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, phba->mbox_ext_buf_ctx.nembType,
4354 mbox_rd, dma_ebuf, sta_pos_addr,
4357 pbuf = (uint8_t *)dmabuf->virt;
4358 bsg_reply->reply_payload_rcv_len =
4359 sg_copy_from_buffer(job->reply_payload.sg_list,
4360 job->reply_payload.sg_cnt,
4363 lpfc_bsg_dma_page_free(phba, dmabuf);
4365 if (phba->mbox_ext_buf_ctx.seqNum == phba->mbox_ext_buf_ctx.numBuf) {
4366 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4367 "2965 SLI_CONFIG (hbd) ext-buffer rd mbox "
4368 "command session done\n");
4369 lpfc_bsg_mbox_ext_session_reset(phba);
4372 bsg_reply->result = 0;
4373 bsg_job_done(job, bsg_reply->result,
4374 bsg_reply->reply_payload_rcv_len);
4376 return SLI_CONFIG_HANDLED;
4380 * lpfc_bsg_write_ebuf_set - set the next mailbox write external buffer
4381 * @phba: Pointer to HBA context object.
4382 * @job: Pointer to the job object.
4383 * @dmabuf: Pointer to a DMA buffer descriptor.
4385 * This routine sets up the next mailbox read external buffer obtained
4386 * from user space through BSG.
4389 lpfc_bsg_write_ebuf_set(struct lpfc_hba *phba, struct bsg_job *job,
4390 struct lpfc_dmabuf *dmabuf)
4392 struct fc_bsg_reply *bsg_reply = job->reply;
4393 struct bsg_job_data *dd_data = NULL;
4394 LPFC_MBOXQ_t *pmboxq = NULL;
4396 enum nemb_type nemb_tp;
4402 index = phba->mbox_ext_buf_ctx.seqNum;
4403 phba->mbox_ext_buf_ctx.seqNum++;
4404 nemb_tp = phba->mbox_ext_buf_ctx.nembType;
4406 pbuf = (uint8_t *)dmabuf->virt;
4407 size = job->request_payload.payload_len;
4408 sg_copy_to_buffer(job->request_payload.sg_list,
4409 job->request_payload.sg_cnt,
4412 if (phba->mbox_ext_buf_ctx.nembType == nemb_mse) {
4413 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4414 "2966 SLI_CONFIG (mse) ext-buffer wr set "
4415 "buffer[%d], size:%d\n",
4416 phba->mbox_ext_buf_ctx.seqNum, size);
4419 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4420 "2967 SLI_CONFIG (hbd) ext-buffer wr set "
4421 "buffer[%d], size:%d\n",
4422 phba->mbox_ext_buf_ctx.seqNum, size);
4426 /* set up external buffer descriptor and add to external buffer list */
4427 lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, index,
4428 phba->mbox_ext_buf_ctx.mbx_dmabuf,
4430 list_add_tail(&dmabuf->list, &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
4432 /* after write dma buffer */
4433 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, phba->mbox_ext_buf_ctx.nembType,
4434 mbox_wr, dma_ebuf, sta_pos_addr,
4437 if (phba->mbox_ext_buf_ctx.seqNum == phba->mbox_ext_buf_ctx.numBuf) {
4438 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4439 "2968 SLI_CONFIG ext-buffer wr all %d "
4440 "ebuffers received\n",
4441 phba->mbox_ext_buf_ctx.numBuf);
4443 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
4449 /* mailbox command structure for base driver */
4450 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4455 memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
4456 pbuf = (uint8_t *)phba->mbox_ext_buf_ctx.mbx_dmabuf->virt;
4457 pmb = &pmboxq->u.mb;
4458 memcpy(pmb, pbuf, sizeof(*pmb));
4459 pmb->mbxOwner = OWN_HOST;
4460 pmboxq->vport = phba->pport;
4462 /* callback for multi-buffer write mailbox command */
4463 pmboxq->mbox_cmpl = lpfc_bsg_issue_write_mbox_ext_cmpl;
4465 /* context fields to callback function */
4466 pmboxq->ctx_u.dd_data = dd_data;
4467 dd_data->type = TYPE_MBOX;
4468 dd_data->set_job = job;
4469 dd_data->context_un.mbox.pmboxq = pmboxq;
4470 dd_data->context_un.mbox.mb = (MAILBOX_t *)pbuf;
4471 job->dd_data = dd_data;
4474 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
4476 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
4477 if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
4478 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4479 "2969 Issued SLI_CONFIG ext-buffer "
4480 "mailbox command, rc:x%x\n", rc);
4481 return SLI_CONFIG_HANDLED;
4483 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4484 "2970 Failed to issue SLI_CONFIG ext-buffer "
4485 "mailbox command, rc:x%x\n", rc);
4490 /* wait for additional external buffers */
4491 bsg_reply->result = 0;
4492 bsg_job_done(job, bsg_reply->result,
4493 bsg_reply->reply_payload_rcv_len);
4494 return SLI_CONFIG_HANDLED;
4498 mempool_free(pmboxq, phba->mbox_mem_pool);
4499 lpfc_bsg_dma_page_free(phba, dmabuf);
4506 * lpfc_bsg_handle_sli_cfg_ebuf - handle ext buffer with sli-cfg mailbox cmd
4507 * @phba: Pointer to HBA context object.
4508 * @job: Pointer to the job object.
4509 * @dmabuf: Pointer to a DMA buffer descriptor.
4511 * This routine handles the external buffer with SLI_CONFIG (0x9B) mailbox
4512 * command with multiple non-embedded external buffers.
4515 lpfc_bsg_handle_sli_cfg_ebuf(struct lpfc_hba *phba, struct bsg_job *job,
4516 struct lpfc_dmabuf *dmabuf)
4520 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4521 "2971 SLI_CONFIG buffer (type:x%x)\n",
4522 phba->mbox_ext_buf_ctx.mboxType);
4524 if (phba->mbox_ext_buf_ctx.mboxType == mbox_rd) {
4525 if (phba->mbox_ext_buf_ctx.state != LPFC_BSG_MBOX_DONE) {
4526 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4527 "2972 SLI_CONFIG rd buffer state "
4529 phba->mbox_ext_buf_ctx.state);
4530 lpfc_bsg_mbox_ext_abort(phba);
4533 rc = lpfc_bsg_read_ebuf_get(phba, job);
4534 if (rc == SLI_CONFIG_HANDLED)
4535 lpfc_bsg_dma_page_free(phba, dmabuf);
4536 } else { /* phba->mbox_ext_buf_ctx.mboxType == mbox_wr */
4537 if (phba->mbox_ext_buf_ctx.state != LPFC_BSG_MBOX_HOST) {
4538 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4539 "2973 SLI_CONFIG wr buffer state "
4541 phba->mbox_ext_buf_ctx.state);
4542 lpfc_bsg_mbox_ext_abort(phba);
4545 rc = lpfc_bsg_write_ebuf_set(phba, job, dmabuf);
4551 * lpfc_bsg_handle_sli_cfg_ext - handle sli-cfg mailbox with external buffer
4552 * @phba: Pointer to HBA context object.
4553 * @job: Pointer to the job object.
4554 * @dmabuf: Pointer to a DMA buffer descriptor.
4556 * This routine checks and handles non-embedded multi-buffer SLI_CONFIG
4557 * (0x9B) mailbox commands and external buffers.
4560 lpfc_bsg_handle_sli_cfg_ext(struct lpfc_hba *phba, struct bsg_job *job,
4561 struct lpfc_dmabuf *dmabuf)
4563 struct fc_bsg_request *bsg_request = job->request;
4564 struct dfc_mbox_req *mbox_req;
4565 int rc = SLI_CONFIG_NOT_HANDLED;
4568 (struct dfc_mbox_req *)bsg_request->rqst_data.h_vendor.vendor_cmd;
4570 /* mbox command with/without single external buffer */
4571 if (mbox_req->extMboxTag == 0 && mbox_req->extSeqNum == 0)
4574 /* mbox command and first external buffer */
4575 if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_IDLE) {
4576 if (mbox_req->extSeqNum == 1) {
4577 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4578 "2974 SLI_CONFIG mailbox: tag:%d, "
4579 "seq:%d\n", mbox_req->extMboxTag,
4580 mbox_req->extSeqNum);
4581 rc = lpfc_bsg_handle_sli_cfg_mbox(phba, job, dmabuf);
4584 goto sli_cfg_ext_error;
4588 * handle additional external buffers
4591 /* check broken pipe conditions */
4592 if (mbox_req->extMboxTag != phba->mbox_ext_buf_ctx.mbxTag)
4593 goto sli_cfg_ext_error;
4594 if (mbox_req->extSeqNum > phba->mbox_ext_buf_ctx.numBuf)
4595 goto sli_cfg_ext_error;
4596 if (mbox_req->extSeqNum != phba->mbox_ext_buf_ctx.seqNum + 1)
4597 goto sli_cfg_ext_error;
4599 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4600 "2975 SLI_CONFIG mailbox external buffer: "
4601 "extSta:x%x, tag:%d, seq:%d\n",
4602 phba->mbox_ext_buf_ctx.state, mbox_req->extMboxTag,
4603 mbox_req->extSeqNum);
4604 rc = lpfc_bsg_handle_sli_cfg_ebuf(phba, job, dmabuf);
4608 /* all other cases, broken pipe */
4609 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4610 "2976 SLI_CONFIG mailbox broken pipe: "
4611 "ctxSta:x%x, ctxNumBuf:%d "
4612 "ctxTag:%d, ctxSeq:%d, tag:%d, seq:%d\n",
4613 phba->mbox_ext_buf_ctx.state,
4614 phba->mbox_ext_buf_ctx.numBuf,
4615 phba->mbox_ext_buf_ctx.mbxTag,
4616 phba->mbox_ext_buf_ctx.seqNum,
4617 mbox_req->extMboxTag, mbox_req->extSeqNum);
4619 lpfc_bsg_mbox_ext_session_reset(phba);
4625 * lpfc_bsg_issue_mbox - issues a mailbox command on behalf of an app
4626 * @phba: Pointer to HBA context object.
4627 * @job: Pointer to the job object.
4628 * @vport: Pointer to a vport object.
4630 * Allocate a tracking object, mailbox command memory, get a mailbox
4631 * from the mailbox pool, copy the caller mailbox command.
4633 * If offline and the sli is active we need to poll for the command (port is
4634 * being reset) and complete the job, otherwise issue the mailbox command and
4635 * let our completion handler finish the command.
4638 lpfc_bsg_issue_mbox(struct lpfc_hba *phba, struct bsg_job *job,
4639 struct lpfc_vport *vport)
4641 struct fc_bsg_request *bsg_request = job->request;
4642 struct fc_bsg_reply *bsg_reply = job->reply;
4643 LPFC_MBOXQ_t *pmboxq = NULL; /* internal mailbox queue */
4644 MAILBOX_t *pmb; /* shortcut to the pmboxq mailbox */
4645 /* a 4k buffer to hold the mb and extended data from/to the bsg */
4646 uint8_t *pmbx = NULL;
4647 struct bsg_job_data *dd_data = NULL; /* bsg data tracking structure */
4648 struct lpfc_dmabuf *dmabuf = NULL;
4649 struct dfc_mbox_req *mbox_req;
4650 struct READ_EVENT_LOG_VAR *rdEventLog;
4651 uint32_t transmit_length, receive_length, mode;
4652 struct lpfc_mbx_sli4_config *sli4_config;
4653 struct lpfc_mbx_nembed_cmd *nembed_sge;
4654 struct ulp_bde64 *bde;
4655 uint8_t *ext = NULL;
4660 /* in case no data is transferred */
4661 bsg_reply->reply_payload_rcv_len = 0;
4663 /* sanity check to protect driver */
4664 if (job->reply_payload.payload_len > BSG_MBOX_SIZE ||
4665 job->request_payload.payload_len > BSG_MBOX_SIZE) {
4671 * Don't allow mailbox commands to be sent when blocked or when in
4672 * the middle of discovery
4674 if (phba->sli.sli_flag & LPFC_BLOCK_MGMT_IO) {
4680 (struct dfc_mbox_req *)bsg_request->rqst_data.h_vendor.vendor_cmd;
4682 /* check if requested extended data lengths are valid */
4683 if ((mbox_req->inExtWLen > BSG_MBOX_SIZE/sizeof(uint32_t)) ||
4684 (mbox_req->outExtWLen > BSG_MBOX_SIZE/sizeof(uint32_t))) {
4689 dmabuf = lpfc_bsg_dma_page_alloc(phba);
4690 if (!dmabuf || !dmabuf->virt) {
4695 /* Get the mailbox command or external buffer from BSG */
4696 pmbx = (uint8_t *)dmabuf->virt;
4697 size = job->request_payload.payload_len;
4698 sg_copy_to_buffer(job->request_payload.sg_list,
4699 job->request_payload.sg_cnt, pmbx, size);
4701 /* Handle possible SLI_CONFIG with non-embedded payloads */
4702 if (phba->sli_rev == LPFC_SLI_REV4) {
4703 rc = lpfc_bsg_handle_sli_cfg_ext(phba, job, dmabuf);
4704 if (rc == SLI_CONFIG_HANDLED)
4708 /* SLI_CONFIG_NOT_HANDLED for other mailbox commands */
4711 rc = lpfc_bsg_check_cmd_access(phba, (MAILBOX_t *)pmbx, vport);
4713 goto job_done; /* must be negative */
4715 /* allocate our bsg tracking structure */
4716 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
4718 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
4719 "2727 Failed allocation of dd_data\n");
4724 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4729 memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
4731 pmb = &pmboxq->u.mb;
4732 memcpy(pmb, pmbx, sizeof(*pmb));
4733 pmb->mbxOwner = OWN_HOST;
4734 pmboxq->vport = vport;
4736 /* If HBA encountered an error attention, allow only DUMP
4737 * or RESTART mailbox commands until the HBA is restarted.
4739 if (phba->pport->stopped &&
4740 pmb->mbxCommand != MBX_DUMP_MEMORY &&
4741 pmb->mbxCommand != MBX_RESTART &&
4742 pmb->mbxCommand != MBX_WRITE_VPARMS &&
4743 pmb->mbxCommand != MBX_WRITE_WWN)
4744 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
4745 "2797 mbox: Issued mailbox cmd "
4746 "0x%x while in stopped state.\n",
4749 /* extended mailbox commands will need an extended buffer */
4750 if (mbox_req->inExtWLen || mbox_req->outExtWLen) {
4752 ext = from + sizeof(MAILBOX_t);
4753 pmboxq->ext_buf = ext;
4754 pmboxq->in_ext_byte_len =
4755 mbox_req->inExtWLen * sizeof(uint32_t);
4756 pmboxq->out_ext_byte_len =
4757 mbox_req->outExtWLen * sizeof(uint32_t);
4758 pmboxq->mbox_offset_word = mbox_req->mbOffset;
4761 /* biu diag will need a kernel buffer to transfer the data
4762 * allocate our own buffer and setup the mailbox command to
4765 if (pmb->mbxCommand == MBX_RUN_BIU_DIAG64) {
4766 transmit_length = pmb->un.varWords[1];
4767 receive_length = pmb->un.varWords[4];
4768 /* transmit length cannot be greater than receive length or
4769 * mailbox extension size
4771 if ((transmit_length > receive_length) ||
4772 (transmit_length > BSG_MBOX_SIZE - sizeof(MAILBOX_t))) {
4776 pmb->un.varBIUdiag.un.s2.xmit_bde64.addrHigh =
4777 putPaddrHigh(dmabuf->phys + sizeof(MAILBOX_t));
4778 pmb->un.varBIUdiag.un.s2.xmit_bde64.addrLow =
4779 putPaddrLow(dmabuf->phys + sizeof(MAILBOX_t));
4781 pmb->un.varBIUdiag.un.s2.rcv_bde64.addrHigh =
4782 putPaddrHigh(dmabuf->phys + sizeof(MAILBOX_t)
4783 + pmb->un.varBIUdiag.un.s2.xmit_bde64.tus.f.bdeSize);
4784 pmb->un.varBIUdiag.un.s2.rcv_bde64.addrLow =
4785 putPaddrLow(dmabuf->phys + sizeof(MAILBOX_t)
4786 + pmb->un.varBIUdiag.un.s2.xmit_bde64.tus.f.bdeSize);
4787 } else if (pmb->mbxCommand == MBX_READ_EVENT_LOG) {
4788 rdEventLog = &pmb->un.varRdEventLog;
4789 receive_length = rdEventLog->rcv_bde64.tus.f.bdeSize;
4790 mode = bf_get(lpfc_event_log, rdEventLog);
4792 /* receive length cannot be greater than mailbox
4795 if (receive_length > BSG_MBOX_SIZE - sizeof(MAILBOX_t)) {
4800 /* mode zero uses a bde like biu diags command */
4802 pmb->un.varWords[3] = putPaddrLow(dmabuf->phys
4803 + sizeof(MAILBOX_t));
4804 pmb->un.varWords[4] = putPaddrHigh(dmabuf->phys
4805 + sizeof(MAILBOX_t));
4807 } else if (phba->sli_rev == LPFC_SLI_REV4) {
4808 /* Let type 4 (well known data) through because the data is
4809 * returned in varwords[4-8]
4810 * otherwise check the recieve length and fetch the buffer addr
4812 if ((pmb->mbxCommand == MBX_DUMP_MEMORY) &&
4813 (pmb->un.varDmp.type != DMP_WELL_KNOWN)) {
4814 /* rebuild the command for sli4 using our own buffers
4815 * like we do for biu diags
4817 receive_length = pmb->un.varWords[2];
4818 /* receive length cannot be greater than mailbox
4821 if (receive_length == 0) {
4825 pmb->un.varWords[3] = putPaddrLow(dmabuf->phys
4826 + sizeof(MAILBOX_t));
4827 pmb->un.varWords[4] = putPaddrHigh(dmabuf->phys
4828 + sizeof(MAILBOX_t));
4829 } else if ((pmb->mbxCommand == MBX_UPDATE_CFG) &&
4830 pmb->un.varUpdateCfg.co) {
4831 bde = (struct ulp_bde64 *)&pmb->un.varWords[4];
4833 /* bde size cannot be greater than mailbox ext size */
4834 if (bde->tus.f.bdeSize >
4835 BSG_MBOX_SIZE - sizeof(MAILBOX_t)) {
4839 bde->addrHigh = putPaddrHigh(dmabuf->phys
4840 + sizeof(MAILBOX_t));
4841 bde->addrLow = putPaddrLow(dmabuf->phys
4842 + sizeof(MAILBOX_t));
4843 } else if (pmb->mbxCommand == MBX_SLI4_CONFIG) {
4844 /* Handling non-embedded SLI_CONFIG mailbox command */
4845 sli4_config = &pmboxq->u.mqe.un.sli4_config;
4846 if (!bf_get(lpfc_mbox_hdr_emb,
4847 &sli4_config->header.cfg_mhdr)) {
4848 /* rebuild the command for sli4 using our
4849 * own buffers like we do for biu diags
4851 nembed_sge = (struct lpfc_mbx_nembed_cmd *)
4852 &pmb->un.varWords[0];
4853 receive_length = nembed_sge->sge[0].length;
4855 /* receive length cannot be greater than
4856 * mailbox extension size
4858 if ((receive_length == 0) ||
4860 BSG_MBOX_SIZE - sizeof(MAILBOX_t))) {
4865 nembed_sge->sge[0].pa_hi =
4866 putPaddrHigh(dmabuf->phys
4867 + sizeof(MAILBOX_t));
4868 nembed_sge->sge[0].pa_lo =
4869 putPaddrLow(dmabuf->phys
4870 + sizeof(MAILBOX_t));
4875 dd_data->context_un.mbox.dmabuffers = dmabuf;
4877 /* setup wake call as IOCB callback */
4878 pmboxq->mbox_cmpl = lpfc_bsg_issue_mbox_cmpl;
4880 /* setup context field to pass wait_queue pointer to wake function */
4881 pmboxq->ctx_u.dd_data = dd_data;
4882 dd_data->type = TYPE_MBOX;
4883 dd_data->set_job = job;
4884 dd_data->context_un.mbox.pmboxq = pmboxq;
4885 dd_data->context_un.mbox.mb = (MAILBOX_t *)pmbx;
4886 dd_data->context_un.mbox.ext = ext;
4887 dd_data->context_un.mbox.mbOffset = mbox_req->mbOffset;
4888 dd_data->context_un.mbox.inExtWLen = mbox_req->inExtWLen;
4889 dd_data->context_un.mbox.outExtWLen = mbox_req->outExtWLen;
4890 job->dd_data = dd_data;
4892 if (test_bit(FC_OFFLINE_MODE, &vport->fc_flag) ||
4893 (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))) {
4894 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
4895 if (rc != MBX_SUCCESS) {
4896 rc = (rc == MBX_TIMEOUT) ? -ETIME : -ENODEV;
4900 /* job finished, copy the data */
4901 memcpy(pmbx, pmb, sizeof(*pmb));
4902 bsg_reply->reply_payload_rcv_len =
4903 sg_copy_from_buffer(job->reply_payload.sg_list,
4904 job->reply_payload.sg_cnt,
4906 /* not waiting mbox already done */
4911 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
4912 if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY))
4913 return 1; /* job started */
4916 /* common exit for error or job completed inline */
4918 mempool_free(pmboxq, phba->mbox_mem_pool);
4919 lpfc_bsg_dma_page_free(phba, dmabuf);
4927 * lpfc_bsg_mbox_cmd - process an fc bsg LPFC_BSG_VENDOR_MBOX command
4928 * @job: MBOX fc_bsg_job for LPFC_BSG_VENDOR_MBOX.
4931 lpfc_bsg_mbox_cmd(struct bsg_job *job)
4933 struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
4934 struct fc_bsg_request *bsg_request = job->request;
4935 struct fc_bsg_reply *bsg_reply = job->reply;
4936 struct lpfc_hba *phba = vport->phba;
4937 struct dfc_mbox_req *mbox_req;
4940 /* mix-and-match backward compatibility */
4941 bsg_reply->reply_payload_rcv_len = 0;
4942 if (job->request_len <
4943 sizeof(struct fc_bsg_request) + sizeof(struct dfc_mbox_req)) {
4944 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4945 "2737 Mix-and-match backward compatibility "
4946 "between MBOX_REQ old size:%d and "
4947 "new request size:%d\n",
4948 (int)(job->request_len -
4949 sizeof(struct fc_bsg_request)),
4950 (int)sizeof(struct dfc_mbox_req));
4951 mbox_req = (struct dfc_mbox_req *)
4952 bsg_request->rqst_data.h_vendor.vendor_cmd;
4953 mbox_req->extMboxTag = 0;
4954 mbox_req->extSeqNum = 0;
4957 rc = lpfc_bsg_issue_mbox(phba, job, vport);
4961 bsg_reply->result = 0;
4962 job->dd_data = NULL;
4963 bsg_job_done(job, bsg_reply->result,
4964 bsg_reply->reply_payload_rcv_len);
4966 /* job submitted, will complete later*/
4967 rc = 0; /* return zero, no error */
4969 /* some error occurred */
4970 bsg_reply->result = rc;
4971 job->dd_data = NULL;
4978 lpfc_forced_link_speed(struct bsg_job *job)
4980 struct Scsi_Host *shost = fc_bsg_to_shost(job);
4981 struct lpfc_vport *vport = shost_priv(shost);
4982 struct lpfc_hba *phba = vport->phba;
4983 struct fc_bsg_reply *bsg_reply = job->reply;
4984 struct forced_link_speed_support_reply *forced_reply;
4987 if (job->request_len <
4988 sizeof(struct fc_bsg_request) +
4989 sizeof(struct get_forced_link_speed_support)) {
4990 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
4991 "0048 Received FORCED_LINK_SPEED request "
4992 "below minimum size\n");
4997 forced_reply = (struct forced_link_speed_support_reply *)
4998 bsg_reply->reply_data.vendor_reply.vendor_rsp;
5000 if (job->reply_len < sizeof(*bsg_reply) + sizeof(*forced_reply)) {
5001 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5002 "0049 Received FORCED_LINK_SPEED reply below "
5008 forced_reply->supported = test_bit(HBA_FORCED_LINK_SPEED,
5010 ? LPFC_FORCED_LINK_SPEED_SUPPORTED
5011 : LPFC_FORCED_LINK_SPEED_NOT_SUPPORTED;
5013 bsg_reply->result = rc;
5015 bsg_job_done(job, bsg_reply->result,
5016 bsg_reply->reply_payload_rcv_len);
5021 * lpfc_check_fwlog_support: Check FW log support on the adapter
5022 * @phba: Pointer to HBA context object.
5024 * Check if FW Logging support by the adapter
5027 lpfc_check_fwlog_support(struct lpfc_hba *phba)
5029 struct lpfc_ras_fwlog *ras_fwlog = NULL;
5031 ras_fwlog = &phba->ras_fwlog;
5033 if (!ras_fwlog->ras_hwsupport)
5035 else if (!ras_fwlog->ras_enabled)
5042 * lpfc_bsg_get_ras_config: Get RAS configuration settings
5043 * @job: fc_bsg_job to handle
5045 * Get RAS configuration values set.
5048 lpfc_bsg_get_ras_config(struct bsg_job *job)
5050 struct Scsi_Host *shost = fc_bsg_to_shost(job);
5051 struct lpfc_vport *vport = shost_priv(shost);
5052 struct fc_bsg_reply *bsg_reply = job->reply;
5053 struct lpfc_hba *phba = vport->phba;
5054 struct lpfc_bsg_get_ras_config_reply *ras_reply;
5055 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
5058 if (job->request_len <
5059 sizeof(struct fc_bsg_request) +
5060 sizeof(struct lpfc_bsg_ras_req)) {
5061 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
5062 "6192 FW_LOG request received "
5063 "below minimum size\n");
5068 /* Check FW log status */
5069 rc = lpfc_check_fwlog_support(phba);
5073 ras_reply = (struct lpfc_bsg_get_ras_config_reply *)
5074 bsg_reply->reply_data.vendor_reply.vendor_rsp;
5076 /* Current logging state */
5077 spin_lock_irq(&phba->ras_fwlog_lock);
5078 if (ras_fwlog->state == ACTIVE)
5079 ras_reply->state = LPFC_RASLOG_STATE_RUNNING;
5081 ras_reply->state = LPFC_RASLOG_STATE_STOPPED;
5082 spin_unlock_irq(&phba->ras_fwlog_lock);
5084 ras_reply->log_level = phba->ras_fwlog.fw_loglevel;
5085 ras_reply->log_buff_sz = phba->cfg_ras_fwlog_buffsize;
5088 /* make error code available to userspace */
5089 bsg_reply->result = rc;
5091 /* complete the job back to userspace */
5093 bsg_job_done(job, bsg_reply->result,
5094 bsg_reply->reply_payload_rcv_len);
5099 * lpfc_bsg_set_ras_config: Set FW logging parameters
5100 * @job: fc_bsg_job to handle
5102 * Set log-level parameters for FW-logging in host memory
5105 lpfc_bsg_set_ras_config(struct bsg_job *job)
5107 struct Scsi_Host *shost = fc_bsg_to_shost(job);
5108 struct lpfc_vport *vport = shost_priv(shost);
5109 struct lpfc_hba *phba = vport->phba;
5110 struct lpfc_bsg_set_ras_config_req *ras_req;
5111 struct fc_bsg_request *bsg_request = job->request;
5112 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
5113 struct fc_bsg_reply *bsg_reply = job->reply;
5114 uint8_t action = 0, log_level = 0;
5115 int rc = 0, action_status = 0;
5117 if (job->request_len <
5118 sizeof(struct fc_bsg_request) +
5119 sizeof(struct lpfc_bsg_set_ras_config_req)) {
5120 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
5121 "6182 Received RAS_LOG request "
5122 "below minimum size\n");
5127 /* Check FW log status */
5128 rc = lpfc_check_fwlog_support(phba);
5132 ras_req = (struct lpfc_bsg_set_ras_config_req *)
5133 bsg_request->rqst_data.h_vendor.vendor_cmd;
5134 action = ras_req->action;
5135 log_level = ras_req->log_level;
5137 if (action == LPFC_RASACTION_STOP_LOGGING) {
5138 /* Check if already disabled */
5139 spin_lock_irq(&phba->ras_fwlog_lock);
5140 if (ras_fwlog->state != ACTIVE) {
5141 spin_unlock_irq(&phba->ras_fwlog_lock);
5145 spin_unlock_irq(&phba->ras_fwlog_lock);
5147 /* Disable logging */
5148 lpfc_ras_stop_fwlog(phba);
5150 /*action = LPFC_RASACTION_START_LOGGING*/
5152 /* Even though FW-logging is active re-initialize
5153 * FW-logging with new log-level. Return status
5154 * "Logging already Running" to caller.
5156 spin_lock_irq(&phba->ras_fwlog_lock);
5157 if (ras_fwlog->state != INACTIVE)
5158 action_status = -EINPROGRESS;
5159 spin_unlock_irq(&phba->ras_fwlog_lock);
5161 /* Enable logging */
5162 rc = lpfc_sli4_ras_fwlog_init(phba, log_level,
5163 LPFC_RAS_ENABLE_LOGGING);
5169 /* Check if FW-logging is re-initialized */
5170 if (action_status == -EINPROGRESS)
5174 /* make error code available to userspace */
5175 bsg_reply->result = rc;
5177 /* complete the job back to userspace */
5179 bsg_job_done(job, bsg_reply->result,
5180 bsg_reply->reply_payload_rcv_len);
5186 * lpfc_bsg_get_ras_lwpd: Get log write position data
5187 * @job: fc_bsg_job to handle
5189 * Get Offset/Wrap count of the log message written
5193 lpfc_bsg_get_ras_lwpd(struct bsg_job *job)
5195 struct Scsi_Host *shost = fc_bsg_to_shost(job);
5196 struct lpfc_vport *vport = shost_priv(shost);
5197 struct lpfc_bsg_get_ras_lwpd *ras_reply;
5198 struct lpfc_hba *phba = vport->phba;
5199 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
5200 struct fc_bsg_reply *bsg_reply = job->reply;
5201 u32 *lwpd_ptr = NULL;
5204 rc = lpfc_check_fwlog_support(phba);
5208 if (job->request_len <
5209 sizeof(struct fc_bsg_request) +
5210 sizeof(struct lpfc_bsg_ras_req)) {
5211 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
5212 "6183 Received RAS_LOG request "
5213 "below minimum size\n");
5218 ras_reply = (struct lpfc_bsg_get_ras_lwpd *)
5219 bsg_reply->reply_data.vendor_reply.vendor_rsp;
5221 if (!ras_fwlog->lwpd.virt) {
5222 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
5223 "6193 Restart FW Logging\n");
5228 /* Get lwpd offset */
5229 lwpd_ptr = (uint32_t *)(ras_fwlog->lwpd.virt);
5230 ras_reply->offset = be32_to_cpu(*lwpd_ptr & 0xffffffff);
5232 /* Get wrap count */
5233 ras_reply->wrap_count = be32_to_cpu(*(++lwpd_ptr) & 0xffffffff);
5236 /* make error code available to userspace */
5237 bsg_reply->result = rc;
5239 /* complete the job back to userspace */
5241 bsg_job_done(job, bsg_reply->result,
5242 bsg_reply->reply_payload_rcv_len);
5248 * lpfc_bsg_get_ras_fwlog: Read FW log
5249 * @job: fc_bsg_job to handle
5251 * Copy the FW log into the passed buffer.
5254 lpfc_bsg_get_ras_fwlog(struct bsg_job *job)
5256 struct Scsi_Host *shost = fc_bsg_to_shost(job);
5257 struct lpfc_vport *vport = shost_priv(shost);
5258 struct lpfc_hba *phba = vport->phba;
5259 struct fc_bsg_request *bsg_request = job->request;
5260 struct fc_bsg_reply *bsg_reply = job->reply;
5261 struct lpfc_bsg_get_fwlog_req *ras_req;
5262 u32 rd_offset, rd_index, offset;
5263 void *src, *fwlog_buff;
5264 struct lpfc_ras_fwlog *ras_fwlog = NULL;
5265 struct lpfc_dmabuf *dmabuf, *next;
5268 ras_fwlog = &phba->ras_fwlog;
5270 rc = lpfc_check_fwlog_support(phba);
5274 /* Logging to be stopped before reading */
5275 spin_lock_irq(&phba->ras_fwlog_lock);
5276 if (ras_fwlog->state == ACTIVE) {
5277 spin_unlock_irq(&phba->ras_fwlog_lock);
5281 spin_unlock_irq(&phba->ras_fwlog_lock);
5283 if (job->request_len <
5284 sizeof(struct fc_bsg_request) +
5285 sizeof(struct lpfc_bsg_get_fwlog_req)) {
5286 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
5287 "6184 Received RAS_LOG request "
5288 "below minimum size\n");
5293 ras_req = (struct lpfc_bsg_get_fwlog_req *)
5294 bsg_request->rqst_data.h_vendor.vendor_cmd;
5295 rd_offset = ras_req->read_offset;
5297 /* Allocate memory to read fw log*/
5298 fwlog_buff = vmalloc(ras_req->read_size);
5304 rd_index = (rd_offset / LPFC_RAS_MAX_ENTRY_SIZE);
5305 offset = (rd_offset % LPFC_RAS_MAX_ENTRY_SIZE);
5307 list_for_each_entry_safe(dmabuf, next,
5308 &ras_fwlog->fwlog_buff_list, list) {
5310 if (dmabuf->buffer_tag < rd_index)
5313 src = dmabuf->virt + offset;
5314 memcpy(fwlog_buff, src, ras_req->read_size);
5318 bsg_reply->reply_payload_rcv_len =
5319 sg_copy_from_buffer(job->reply_payload.sg_list,
5320 job->reply_payload.sg_cnt,
5321 fwlog_buff, ras_req->read_size);
5326 bsg_reply->result = rc;
5328 bsg_job_done(job, bsg_reply->result,
5329 bsg_reply->reply_payload_rcv_len);
5335 lpfc_get_trunk_info(struct bsg_job *job)
5337 struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
5338 struct lpfc_hba *phba = vport->phba;
5339 struct fc_bsg_reply *bsg_reply = job->reply;
5340 struct lpfc_trunk_info *event_reply;
5343 if (job->request_len <
5344 sizeof(struct fc_bsg_request) + sizeof(struct get_trunk_info_req)) {
5345 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
5346 "2744 Received GET TRUNK _INFO request below "
5352 event_reply = (struct lpfc_trunk_info *)
5353 bsg_reply->reply_data.vendor_reply.vendor_rsp;
5355 if (job->reply_len < sizeof(*bsg_reply) + sizeof(*event_reply)) {
5356 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5357 "2728 Received GET TRUNK _INFO reply below "
5362 if (event_reply == NULL) {
5367 bsg_bf_set(lpfc_trunk_info_link_status, event_reply,
5368 (phba->link_state >= LPFC_LINK_UP) ? 1 : 0);
5370 bsg_bf_set(lpfc_trunk_info_trunk_active0, event_reply,
5371 (phba->trunk_link.link0.state == LPFC_LINK_UP) ? 1 : 0);
5373 bsg_bf_set(lpfc_trunk_info_trunk_active1, event_reply,
5374 (phba->trunk_link.link1.state == LPFC_LINK_UP) ? 1 : 0);
5376 bsg_bf_set(lpfc_trunk_info_trunk_active2, event_reply,
5377 (phba->trunk_link.link2.state == LPFC_LINK_UP) ? 1 : 0);
5379 bsg_bf_set(lpfc_trunk_info_trunk_active3, event_reply,
5380 (phba->trunk_link.link3.state == LPFC_LINK_UP) ? 1 : 0);
5382 bsg_bf_set(lpfc_trunk_info_trunk_config0, event_reply,
5383 bf_get(lpfc_conf_trunk_port0, &phba->sli4_hba));
5385 bsg_bf_set(lpfc_trunk_info_trunk_config1, event_reply,
5386 bf_get(lpfc_conf_trunk_port1, &phba->sli4_hba));
5388 bsg_bf_set(lpfc_trunk_info_trunk_config2, event_reply,
5389 bf_get(lpfc_conf_trunk_port2, &phba->sli4_hba));
5391 bsg_bf_set(lpfc_trunk_info_trunk_config3, event_reply,
5392 bf_get(lpfc_conf_trunk_port3, &phba->sli4_hba));
5394 event_reply->port_speed = phba->sli4_hba.link_state.speed / 1000;
5395 event_reply->logical_speed =
5396 phba->sli4_hba.link_state.logical_speed / 1000;
5398 bsg_reply->result = rc;
5400 bsg_job_done(job, bsg_reply->result,
5401 bsg_reply->reply_payload_rcv_len);
5407 lpfc_get_cgnbuf_info(struct bsg_job *job)
5409 struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
5410 struct lpfc_hba *phba = vport->phba;
5411 struct fc_bsg_request *bsg_request = job->request;
5412 struct fc_bsg_reply *bsg_reply = job->reply;
5413 struct get_cgnbuf_info_req *cgnbuf_req;
5414 struct lpfc_cgn_info *cp;
5416 size_t size, cinfosz;
5419 if (job->request_len < sizeof(struct fc_bsg_request) +
5420 sizeof(struct get_cgnbuf_info_req)) {
5425 if (!phba->sli4_hba.pc_sli4_params.cmf) {
5430 if (!phba->cgn_i || !phba->cgn_i->virt) {
5435 cp = phba->cgn_i->virt;
5436 if (cp->cgn_info_version < LPFC_CGN_INFO_V3) {
5441 cgnbuf_req = (struct get_cgnbuf_info_req *)
5442 bsg_request->rqst_data.h_vendor.vendor_cmd;
5444 /* For reset or size == 0 */
5445 bsg_reply->reply_payload_rcv_len = 0;
5447 if (cgnbuf_req->reset == LPFC_BSG_CGN_RESET_STAT) {
5448 lpfc_init_congestion_stat(phba);
5452 /* We don't want to include the CRC at the end */
5453 cinfosz = sizeof(struct lpfc_cgn_info) - sizeof(uint32_t);
5455 size = cgnbuf_req->read_size;
5459 if (size < cinfosz) {
5460 /* Just copy back what we can */
5465 /* Allocate memory to read congestion info */
5466 cgn_buff = vmalloc(cinfosz);
5472 memcpy(cgn_buff, cp, cinfosz);
5474 bsg_reply->reply_payload_rcv_len =
5475 sg_copy_from_buffer(job->reply_payload.sg_list,
5476 job->reply_payload.sg_cnt,
5482 bsg_reply->result = rc;
5484 bsg_job_done(job, bsg_reply->result,
5485 bsg_reply->reply_payload_rcv_len);
5487 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
5488 "2724 GET CGNBUF error: %d\n", rc);
5493 * lpfc_bsg_hst_vendor - process a vendor-specific fc_bsg_job
5494 * @job: fc_bsg_job to handle
5497 lpfc_bsg_hst_vendor(struct bsg_job *job)
5499 struct fc_bsg_request *bsg_request = job->request;
5500 struct fc_bsg_reply *bsg_reply = job->reply;
5501 int command = bsg_request->rqst_data.h_vendor.vendor_cmd[0];
5505 case LPFC_BSG_VENDOR_SET_CT_EVENT:
5506 rc = lpfc_bsg_hba_set_event(job);
5508 case LPFC_BSG_VENDOR_GET_CT_EVENT:
5509 rc = lpfc_bsg_hba_get_event(job);
5511 case LPFC_BSG_VENDOR_SEND_MGMT_RESP:
5512 rc = lpfc_bsg_send_mgmt_rsp(job);
5514 case LPFC_BSG_VENDOR_DIAG_MODE:
5515 rc = lpfc_bsg_diag_loopback_mode(job);
5517 case LPFC_BSG_VENDOR_DIAG_MODE_END:
5518 rc = lpfc_sli4_bsg_diag_mode_end(job);
5520 case LPFC_BSG_VENDOR_DIAG_RUN_LOOPBACK:
5521 rc = lpfc_bsg_diag_loopback_run(job);
5523 case LPFC_BSG_VENDOR_LINK_DIAG_TEST:
5524 rc = lpfc_sli4_bsg_link_diag_test(job);
5526 case LPFC_BSG_VENDOR_GET_MGMT_REV:
5527 rc = lpfc_bsg_get_dfc_rev(job);
5529 case LPFC_BSG_VENDOR_MBOX:
5530 rc = lpfc_bsg_mbox_cmd(job);
5532 case LPFC_BSG_VENDOR_FORCED_LINK_SPEED:
5533 rc = lpfc_forced_link_speed(job);
5535 case LPFC_BSG_VENDOR_RAS_GET_LWPD:
5536 rc = lpfc_bsg_get_ras_lwpd(job);
5538 case LPFC_BSG_VENDOR_RAS_GET_FWLOG:
5539 rc = lpfc_bsg_get_ras_fwlog(job);
5541 case LPFC_BSG_VENDOR_RAS_GET_CONFIG:
5542 rc = lpfc_bsg_get_ras_config(job);
5544 case LPFC_BSG_VENDOR_RAS_SET_CONFIG:
5545 rc = lpfc_bsg_set_ras_config(job);
5547 case LPFC_BSG_VENDOR_GET_TRUNK_INFO:
5548 rc = lpfc_get_trunk_info(job);
5550 case LPFC_BSG_VENDOR_GET_CGNBUF_INFO:
5551 rc = lpfc_get_cgnbuf_info(job);
5555 bsg_reply->reply_payload_rcv_len = 0;
5556 /* make error code available to userspace */
5557 bsg_reply->result = rc;
5565 * lpfc_bsg_request - handle a bsg request from the FC transport
5566 * @job: bsg_job to handle
5569 lpfc_bsg_request(struct bsg_job *job)
5571 struct fc_bsg_request *bsg_request = job->request;
5572 struct fc_bsg_reply *bsg_reply = job->reply;
5576 msgcode = bsg_request->msgcode;
5578 case FC_BSG_HST_VENDOR:
5579 rc = lpfc_bsg_hst_vendor(job);
5581 case FC_BSG_RPT_ELS:
5582 rc = lpfc_bsg_rport_els(job);
5585 rc = lpfc_bsg_send_mgmt_cmd(job);
5589 bsg_reply->reply_payload_rcv_len = 0;
5590 /* make error code available to userspace */
5591 bsg_reply->result = rc;
5599 * lpfc_bsg_timeout - handle timeout of a bsg request from the FC transport
5600 * @job: bsg_job that has timed out
5602 * This function just aborts the job's IOCB. The aborted IOCB will return to
5603 * the waiting function which will handle passing the error back to userspace
5606 lpfc_bsg_timeout(struct bsg_job *job)
5608 struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
5609 struct lpfc_hba *phba = vport->phba;
5610 struct lpfc_iocbq *cmdiocb;
5611 struct lpfc_sli_ring *pring;
5612 struct bsg_job_data *dd_data;
5613 unsigned long flags;
5615 LIST_HEAD(completions);
5616 struct lpfc_iocbq *check_iocb, *next_iocb;
5618 pring = lpfc_phba_elsring(phba);
5619 if (unlikely(!pring))
5622 /* if job's driver data is NULL, the command completed or is in the
5623 * the process of completing. In this case, return status to request
5624 * so the timeout is retried. This avoids double completion issues
5625 * and the request will be pulled off the timer queue when the
5626 * command's completion handler executes. Otherwise, prevent the
5627 * command's completion handler from executing the job done callback
5628 * and continue processing to abort the outstanding the command.
5631 spin_lock_irqsave(&phba->ct_ev_lock, flags);
5632 dd_data = (struct bsg_job_data *)job->dd_data;
5634 dd_data->set_job = NULL;
5635 job->dd_data = NULL;
5637 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5641 switch (dd_data->type) {
5643 /* Check to see if IOCB was issued to the port or not. If not,
5644 * remove it from the txq queue and call cancel iocbs.
5645 * Otherwise, call abort iotag
5647 cmdiocb = dd_data->context_un.iocb.cmdiocbq;
5648 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5650 spin_lock_irqsave(&phba->hbalock, flags);
5651 /* make sure the I/O abort window is still open */
5652 if (!(cmdiocb->cmd_flag & LPFC_IO_CMD_OUTSTANDING)) {
5653 spin_unlock_irqrestore(&phba->hbalock, flags);
5656 list_for_each_entry_safe(check_iocb, next_iocb, &pring->txq,
5658 if (check_iocb == cmdiocb) {
5659 list_move_tail(&check_iocb->list, &completions);
5663 if (list_empty(&completions))
5664 lpfc_sli_issue_abort_iotag(phba, pring, cmdiocb, NULL);
5665 spin_unlock_irqrestore(&phba->hbalock, flags);
5666 if (!list_empty(&completions)) {
5667 lpfc_sli_cancel_iocbs(phba, &completions,
5668 IOSTAT_LOCAL_REJECT,
5674 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5678 /* Update the ext buf ctx state if needed */
5680 if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_PORT)
5681 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_ABTS;
5682 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5685 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5689 /* scsi transport fc fc_bsg_job_timeout expects a zero return code,
5690 * otherwise an error message will be displayed on the console
5691 * so always return success (zero)