1 /*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
3 * Fibre Channel Host Bus Adapters. *
4 * Copyright (C) 2009-2012 Emulex. All rights reserved. *
5 * EMULEX and SLI are trademarks of Emulex. *
8 * This program is free software; you can redistribute it and/or *
9 * modify it under the terms of version 2 of the GNU General *
10 * Public License as published by the Free Software Foundation. *
11 * This program is distributed in the hope that it will be useful. *
12 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
13 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
14 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
15 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
16 * TO BE LEGALLY INVALID. See the GNU General Public License for *
17 * more details, a copy of which can be found in the file COPYING *
18 * included with this package. *
19 *******************************************************************/
21 #include <linux/interrupt.h>
22 #include <linux/mempool.h>
23 #include <linux/pci.h>
24 #include <linux/slab.h>
25 #include <linux/delay.h>
26 #include <linux/list.h>
28 #include <scsi/scsi.h>
29 #include <scsi/scsi_host.h>
30 #include <scsi/scsi_transport_fc.h>
31 #include <scsi/scsi_bsg_fc.h>
32 #include <scsi/fc/fc_fs.h>
37 #include "lpfc_sli4.h"
40 #include "lpfc_disc.h"
41 #include "lpfc_scsi.h"
43 #include "lpfc_logmsg.h"
44 #include "lpfc_crtn.h"
45 #include "lpfc_debugfs.h"
46 #include "lpfc_vport.h"
47 #include "lpfc_version.h"
49 struct lpfc_bsg_event {
50 struct list_head node;
54 /* Event type and waiter identifiers */
59 /* next two flags are here for the auto-delete logic */
60 unsigned long wait_time_stamp;
63 /* seen and not seen events */
64 struct list_head events_to_get;
65 struct list_head events_to_see;
67 /* driver data associated with the job */
71 struct lpfc_bsg_iocb {
72 struct lpfc_iocbq *cmdiocbq;
73 struct lpfc_dmabuf *rmp;
74 struct lpfc_nodelist *ndlp;
77 struct lpfc_bsg_mbox {
80 struct lpfc_dmabuf *dmabuffers; /* for BIU diags */
81 uint8_t *ext; /* extended mailbox data */
82 uint32_t mbOffset; /* from app */
83 uint32_t inExtWLen; /* from app */
84 uint32_t outExtWLen; /* from app */
87 #define MENLO_DID 0x0000FC0E
89 struct lpfc_bsg_menlo {
90 struct lpfc_iocbq *cmdiocbq;
91 struct lpfc_dmabuf *rmp;
100 struct fc_bsg_job *set_job; /* job waiting for this iocb to finish */
102 struct lpfc_bsg_event *evt;
103 struct lpfc_bsg_iocb iocb;
104 struct lpfc_bsg_mbox mbox;
105 struct lpfc_bsg_menlo menlo;
110 struct list_head node;
117 #define BUF_SZ_4K 4096
118 #define SLI_CT_ELX_LOOPBACK 0x10
120 enum ELX_LOOPBACK_CMD {
121 ELX_LOOPBACK_XRI_SETUP,
125 #define ELX_LOOPBACK_HEADER_SZ \
126 (size_t)(&((struct lpfc_sli_ct_request *)NULL)->un)
128 struct lpfc_dmabufext {
129 struct lpfc_dmabuf dma;
135 lpfc_free_bsg_buffers(struct lpfc_hba *phba, struct lpfc_dmabuf *mlist)
137 struct lpfc_dmabuf *mlast, *next_mlast;
140 list_for_each_entry_safe(mlast, next_mlast, &mlist->list,
142 lpfc_mbuf_free(phba, mlast->virt, mlast->phys);
143 list_del(&mlast->list);
146 lpfc_mbuf_free(phba, mlist->virt, mlist->phys);
152 static struct lpfc_dmabuf *
153 lpfc_alloc_bsg_buffers(struct lpfc_hba *phba, unsigned int size,
154 int outbound_buffers, struct ulp_bde64 *bpl,
157 struct lpfc_dmabuf *mlist = NULL;
158 struct lpfc_dmabuf *mp;
159 unsigned int bytes_left = size;
161 /* Verify we can support the size specified */
162 if (!size || (size > (*bpl_entries * LPFC_BPL_SIZE)))
165 /* Determine the number of dma buffers to allocate */
166 *bpl_entries = (size % LPFC_BPL_SIZE ? size/LPFC_BPL_SIZE + 1 :
169 /* Allocate dma buffer and place in BPL passed */
171 /* Allocate dma buffer */
172 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
175 lpfc_free_bsg_buffers(phba, mlist);
179 INIT_LIST_HEAD(&mp->list);
180 mp->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &(mp->phys));
185 lpfc_free_bsg_buffers(phba, mlist);
189 /* Queue it to a linked list */
193 list_add_tail(&mp->list, &mlist->list);
195 /* Add buffer to buffer pointer list */
196 if (outbound_buffers)
197 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
199 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
200 bpl->addrLow = le32_to_cpu(putPaddrLow(mp->phys));
201 bpl->addrHigh = le32_to_cpu(putPaddrHigh(mp->phys));
202 bpl->tus.f.bdeSize = (uint16_t)
203 (bytes_left >= LPFC_BPL_SIZE ? LPFC_BPL_SIZE :
205 bytes_left -= bpl->tus.f.bdeSize;
206 bpl->tus.w = le32_to_cpu(bpl->tus.w);
213 lpfc_bsg_copy_data(struct lpfc_dmabuf *dma_buffers,
214 struct fc_bsg_buffer *bsg_buffers,
215 unsigned int bytes_to_transfer, int to_buffers)
218 struct lpfc_dmabuf *mp;
219 unsigned int transfer_bytes, bytes_copied = 0;
220 unsigned int sg_offset, dma_offset;
221 unsigned char *dma_address, *sg_address;
222 struct scatterlist *sgel;
223 LIST_HEAD(temp_list);
226 list_splice_init(&dma_buffers->list, &temp_list);
227 list_add(&dma_buffers->list, &temp_list);
229 sgel = bsg_buffers->sg_list;
230 list_for_each_entry(mp, &temp_list, list) {
232 while (bytes_to_transfer && sgel &&
233 (dma_offset < LPFC_BPL_SIZE)) {
234 dma_address = mp->virt + dma_offset;
236 /* Continue previous partial transfer of sg */
237 sg_address = sg_virt(sgel) + sg_offset;
238 transfer_bytes = sgel->length - sg_offset;
240 sg_address = sg_virt(sgel);
241 transfer_bytes = sgel->length;
243 if (bytes_to_transfer < transfer_bytes)
244 transfer_bytes = bytes_to_transfer;
245 if (transfer_bytes > (LPFC_BPL_SIZE - dma_offset))
246 transfer_bytes = LPFC_BPL_SIZE - dma_offset;
248 memcpy(dma_address, sg_address, transfer_bytes);
250 memcpy(sg_address, dma_address, transfer_bytes);
251 dma_offset += transfer_bytes;
252 sg_offset += transfer_bytes;
253 bytes_to_transfer -= transfer_bytes;
254 bytes_copied += transfer_bytes;
255 if (sg_offset >= sgel->length) {
257 sgel = sg_next(sgel);
261 list_del_init(&dma_buffers->list);
262 list_splice(&temp_list, &dma_buffers->list);
267 * lpfc_bsg_send_mgmt_cmd_cmp - lpfc_bsg_send_mgmt_cmd's completion handler
268 * @phba: Pointer to HBA context object.
269 * @cmdiocbq: Pointer to command iocb.
270 * @rspiocbq: Pointer to response iocb.
272 * This function is the completion handler for iocbs issued using
273 * lpfc_bsg_send_mgmt_cmd function. This function is called by the
274 * ring event handler function without any lock held. This function
275 * can be called from both worker thread context and interrupt
276 * context. This function also can be called from another thread which
277 * cleans up the SLI layer objects.
278 * This function copies the contents of the response iocb to the
279 * response iocb memory object provided by the caller of
280 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
281 * sleeps for the iocb completion.
284 lpfc_bsg_send_mgmt_cmd_cmp(struct lpfc_hba *phba,
285 struct lpfc_iocbq *cmdiocbq,
286 struct lpfc_iocbq *rspiocbq)
288 struct bsg_job_data *dd_data;
289 struct fc_bsg_job *job;
291 struct lpfc_dmabuf *bmp, *cmp, *rmp;
292 struct lpfc_nodelist *ndlp;
293 struct lpfc_bsg_iocb *iocb;
295 unsigned int rsp_size;
298 dd_data = cmdiocbq->context1;
300 /* Determine if job has been aborted */
301 spin_lock_irqsave(&phba->ct_ev_lock, flags);
302 job = dd_data->set_job;
304 /* Prevent timeout handling from trying to abort job */
307 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
309 iocb = &dd_data->context_un.iocb;
312 cmp = cmdiocbq->context2;
313 bmp = cmdiocbq->context3;
314 rsp = &rspiocbq->iocb;
316 /* Copy the completed data or set the error status */
319 if (rsp->ulpStatus) {
320 if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
321 switch (rsp->un.ulpWord[4] & IOERR_PARAM_MASK) {
322 case IOERR_SEQUENCE_TIMEOUT:
325 case IOERR_INVALID_RPI:
336 rsp_size = rsp->un.genreq64.bdl.bdeSize;
337 job->reply->reply_payload_rcv_len =
338 lpfc_bsg_copy_data(rmp, &job->reply_payload,
343 lpfc_free_bsg_buffers(phba, cmp);
344 lpfc_free_bsg_buffers(phba, rmp);
345 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
347 lpfc_sli_release_iocbq(phba, cmdiocbq);
351 /* Complete the job if the job is still active */
354 job->reply->result = rc;
361 * lpfc_bsg_send_mgmt_cmd - send a CT command from a bsg request
362 * @job: fc_bsg_job to handle
365 lpfc_bsg_send_mgmt_cmd(struct fc_bsg_job *job)
367 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
368 struct lpfc_hba *phba = vport->phba;
369 struct lpfc_rport_data *rdata = job->rport->dd_data;
370 struct lpfc_nodelist *ndlp = rdata->pnode;
371 struct ulp_bde64 *bpl = NULL;
373 struct lpfc_iocbq *cmdiocbq = NULL;
375 struct lpfc_dmabuf *bmp = NULL, *cmp = NULL, *rmp = NULL;
378 struct bsg_job_data *dd_data;
383 /* in case no data is transferred */
384 job->reply->reply_payload_rcv_len = 0;
386 /* allocate our bsg tracking structure */
387 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
389 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
390 "2733 Failed allocation of dd_data\n");
395 if (!lpfc_nlp_get(ndlp)) {
400 if (ndlp->nlp_flag & NLP_ELS_SND_MASK) {
405 cmdiocbq = lpfc_sli_get_iocbq(phba);
411 cmd = &cmdiocbq->iocb;
413 bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
418 bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
424 INIT_LIST_HEAD(&bmp->list);
426 bpl = (struct ulp_bde64 *) bmp->virt;
427 request_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64);
428 cmp = lpfc_alloc_bsg_buffers(phba, job->request_payload.payload_len,
429 1, bpl, &request_nseg);
434 lpfc_bsg_copy_data(cmp, &job->request_payload,
435 job->request_payload.payload_len, 1);
438 reply_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64) - request_nseg;
439 rmp = lpfc_alloc_bsg_buffers(phba, job->reply_payload.payload_len, 0,
446 cmd->un.genreq64.bdl.ulpIoTag32 = 0;
447 cmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
448 cmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
449 cmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
450 cmd->un.genreq64.bdl.bdeSize =
451 (request_nseg + reply_nseg) * sizeof(struct ulp_bde64);
452 cmd->ulpCommand = CMD_GEN_REQUEST64_CR;
453 cmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
454 cmd->un.genreq64.w5.hcsw.Dfctl = 0;
455 cmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
456 cmd->un.genreq64.w5.hcsw.Type = FC_TYPE_CT;
457 cmd->ulpBdeCount = 1;
459 cmd->ulpClass = CLASS3;
460 cmd->ulpContext = ndlp->nlp_rpi;
461 if (phba->sli_rev == LPFC_SLI_REV4)
462 cmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
463 cmd->ulpOwner = OWN_CHIP;
464 cmdiocbq->vport = phba->pport;
465 cmdiocbq->context3 = bmp;
466 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
467 timeout = phba->fc_ratov * 2;
468 cmd->ulpTimeout = timeout;
470 cmdiocbq->iocb_cmpl = lpfc_bsg_send_mgmt_cmd_cmp;
471 cmdiocbq->context1 = dd_data;
472 cmdiocbq->context2 = cmp;
473 cmdiocbq->context3 = bmp;
474 dd_data->type = TYPE_IOCB;
475 dd_data->set_job = job;
476 dd_data->context_un.iocb.cmdiocbq = cmdiocbq;
477 dd_data->context_un.iocb.ndlp = ndlp;
478 dd_data->context_un.iocb.rmp = rmp;
479 job->dd_data = dd_data;
481 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
482 if (lpfc_readl(phba->HCregaddr, &creg_val)) {
486 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
487 writel(creg_val, phba->HCregaddr);
488 readl(phba->HCregaddr); /* flush */
491 iocb_stat = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq, 0);
492 if (iocb_stat == IOCB_SUCCESS)
493 return 0; /* done for now */
494 else if (iocb_stat == IOCB_BUSY)
499 /* iocb failed so cleanup */
502 lpfc_free_bsg_buffers(phba, rmp);
504 lpfc_free_bsg_buffers(phba, cmp);
507 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
510 lpfc_sli_release_iocbq(phba, cmdiocbq);
516 /* make error code available to userspace */
517 job->reply->result = rc;
523 * lpfc_bsg_rport_els_cmp - lpfc_bsg_rport_els's completion handler
524 * @phba: Pointer to HBA context object.
525 * @cmdiocbq: Pointer to command iocb.
526 * @rspiocbq: Pointer to response iocb.
528 * This function is the completion handler for iocbs issued using
529 * lpfc_bsg_rport_els_cmp function. This function is called by the
530 * ring event handler function without any lock held. This function
531 * can be called from both worker thread context and interrupt
532 * context. This function also can be called from other thread which
533 * cleans up the SLI layer objects.
534 * This function copies the contents of the response iocb to the
535 * response iocb memory object provided by the caller of
536 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
537 * sleeps for the iocb completion.
540 lpfc_bsg_rport_els_cmp(struct lpfc_hba *phba,
541 struct lpfc_iocbq *cmdiocbq,
542 struct lpfc_iocbq *rspiocbq)
544 struct bsg_job_data *dd_data;
545 struct fc_bsg_job *job;
547 struct lpfc_nodelist *ndlp;
548 struct lpfc_dmabuf *pcmd = NULL, *prsp = NULL;
549 struct fc_bsg_ctels_reply *els_reply;
552 unsigned int rsp_size;
555 dd_data = cmdiocbq->context1;
556 ndlp = dd_data->context_un.iocb.ndlp;
557 cmdiocbq->context1 = ndlp;
559 /* Determine if job has been aborted */
560 spin_lock_irqsave(&phba->ct_ev_lock, flags);
561 job = dd_data->set_job;
563 /* Prevent timeout handling from trying to abort job */
566 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
568 rsp = &rspiocbq->iocb;
569 pcmd = (struct lpfc_dmabuf *)cmdiocbq->context2;
570 prsp = (struct lpfc_dmabuf *)pcmd->list.next;
572 /* Copy the completed job data or determine the job status if job is
577 if (rsp->ulpStatus == IOSTAT_SUCCESS) {
578 rsp_size = rsp->un.elsreq64.bdl.bdeSize;
579 job->reply->reply_payload_rcv_len =
580 sg_copy_from_buffer(job->reply_payload.sg_list,
581 job->reply_payload.sg_cnt,
584 } else if (rsp->ulpStatus == IOSTAT_LS_RJT) {
585 job->reply->reply_payload_rcv_len =
586 sizeof(struct fc_bsg_ctels_reply);
587 /* LS_RJT data returned in word 4 */
588 rjt_data = (uint8_t *)&rsp->un.ulpWord[4];
589 els_reply = &job->reply->reply_data.ctels_reply;
590 els_reply->status = FC_CTELS_STATUS_REJECT;
591 els_reply->rjt_data.action = rjt_data[3];
592 els_reply->rjt_data.reason_code = rjt_data[2];
593 els_reply->rjt_data.reason_explanation = rjt_data[1];
594 els_reply->rjt_data.vendor_unique = rjt_data[0];
601 lpfc_els_free_iocb(phba, cmdiocbq);
604 /* Complete the job if the job is still active */
607 job->reply->result = rc;
614 * lpfc_bsg_rport_els - send an ELS command from a bsg request
615 * @job: fc_bsg_job to handle
618 lpfc_bsg_rport_els(struct fc_bsg_job *job)
620 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
621 struct lpfc_hba *phba = vport->phba;
622 struct lpfc_rport_data *rdata = job->rport->dd_data;
623 struct lpfc_nodelist *ndlp = rdata->pnode;
627 struct lpfc_iocbq *cmdiocbq;
629 struct bsg_job_data *dd_data;
633 /* in case no data is transferred */
634 job->reply->reply_payload_rcv_len = 0;
636 /* verify the els command is not greater than the
637 * maximum ELS transfer size.
640 if (job->request_payload.payload_len > FCELSSIZE) {
645 /* allocate our bsg tracking structure */
646 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
648 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
649 "2735 Failed allocation of dd_data\n");
654 elscmd = job->request->rqst_data.r_els.els_code;
655 cmdsize = job->request_payload.payload_len;
656 rspsize = job->reply_payload.payload_len;
658 if (!lpfc_nlp_get(ndlp)) {
663 /* We will use the allocated dma buffers by prep els iocb for command
664 * and response to ensure if the job times out and the request is freed,
665 * we won't be dma into memory that is no longer allocated to for the
669 cmdiocbq = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp,
670 ndlp->nlp_DID, elscmd);
678 /* Transfer the request payload to allocated command dma buffer */
680 sg_copy_to_buffer(job->request_payload.sg_list,
681 job->request_payload.sg_cnt,
682 ((struct lpfc_dmabuf *)cmdiocbq->context2)->virt,
685 if (phba->sli_rev == LPFC_SLI_REV4)
686 cmdiocbq->iocb.ulpContext = phba->sli4_hba.rpi_ids[rpi];
688 cmdiocbq->iocb.ulpContext = rpi;
689 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
690 cmdiocbq->context1 = dd_data;
691 cmdiocbq->context_un.ndlp = ndlp;
692 cmdiocbq->iocb_cmpl = lpfc_bsg_rport_els_cmp;
693 dd_data->type = TYPE_IOCB;
694 dd_data->set_job = job;
695 dd_data->context_un.iocb.cmdiocbq = cmdiocbq;
696 dd_data->context_un.iocb.ndlp = ndlp;
697 dd_data->context_un.iocb.rmp = NULL;
698 job->dd_data = dd_data;
700 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
701 if (lpfc_readl(phba->HCregaddr, &creg_val)) {
705 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
706 writel(creg_val, phba->HCregaddr);
707 readl(phba->HCregaddr); /* flush */
710 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq, 0);
712 if (rc == IOCB_SUCCESS)
713 return 0; /* done for now */
714 else if (rc == IOCB_BUSY)
721 cmdiocbq->context1 = ndlp;
722 lpfc_els_free_iocb(phba, cmdiocbq);
731 /* make error code available to userspace */
732 job->reply->result = rc;
738 * lpfc_bsg_event_free - frees an allocated event structure
739 * @kref: Pointer to a kref.
741 * Called from kref_put. Back cast the kref into an event structure address.
742 * Free any events to get, delete associated nodes, free any events to see,
743 * free any data then free the event itself.
746 lpfc_bsg_event_free(struct kref *kref)
748 struct lpfc_bsg_event *evt = container_of(kref, struct lpfc_bsg_event,
750 struct event_data *ed;
752 list_del(&evt->node);
754 while (!list_empty(&evt->events_to_get)) {
755 ed = list_entry(evt->events_to_get.next, typeof(*ed), node);
761 while (!list_empty(&evt->events_to_see)) {
762 ed = list_entry(evt->events_to_see.next, typeof(*ed), node);
773 * lpfc_bsg_event_ref - increments the kref for an event
774 * @evt: Pointer to an event structure.
777 lpfc_bsg_event_ref(struct lpfc_bsg_event *evt)
779 kref_get(&evt->kref);
783 * lpfc_bsg_event_unref - Uses kref_put to free an event structure
784 * @evt: Pointer to an event structure.
787 lpfc_bsg_event_unref(struct lpfc_bsg_event *evt)
789 kref_put(&evt->kref, lpfc_bsg_event_free);
793 * lpfc_bsg_event_new - allocate and initialize a event structure
794 * @ev_mask: Mask of events.
795 * @ev_reg_id: Event reg id.
796 * @ev_req_id: Event request id.
798 static struct lpfc_bsg_event *
799 lpfc_bsg_event_new(uint32_t ev_mask, int ev_reg_id, uint32_t ev_req_id)
801 struct lpfc_bsg_event *evt = kzalloc(sizeof(*evt), GFP_KERNEL);
806 INIT_LIST_HEAD(&evt->events_to_get);
807 INIT_LIST_HEAD(&evt->events_to_see);
808 evt->type_mask = ev_mask;
809 evt->req_id = ev_req_id;
810 evt->reg_id = ev_reg_id;
811 evt->wait_time_stamp = jiffies;
813 init_waitqueue_head(&evt->wq);
814 kref_init(&evt->kref);
819 * diag_cmd_data_free - Frees an lpfc dma buffer extension
820 * @phba: Pointer to HBA context object.
821 * @mlist: Pointer to an lpfc dma buffer extension.
824 diag_cmd_data_free(struct lpfc_hba *phba, struct lpfc_dmabufext *mlist)
826 struct lpfc_dmabufext *mlast;
827 struct pci_dev *pcidev;
828 struct list_head head, *curr, *next;
830 if ((!mlist) || (!lpfc_is_link_up(phba) &&
831 (phba->link_flag & LS_LOOPBACK_MODE))) {
835 pcidev = phba->pcidev;
836 list_add_tail(&head, &mlist->dma.list);
838 list_for_each_safe(curr, next, &head) {
839 mlast = list_entry(curr, struct lpfc_dmabufext , dma.list);
841 dma_free_coherent(&pcidev->dev,
851 * lpfc_bsg_ct_unsol_event - process an unsolicited CT command
856 * This function is called when an unsolicited CT command is received. It
857 * forwards the event to any processes registered to receive CT events.
860 lpfc_bsg_ct_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
861 struct lpfc_iocbq *piocbq)
863 uint32_t evt_req_id = 0;
866 struct lpfc_dmabuf *dmabuf = NULL;
867 struct lpfc_bsg_event *evt;
868 struct event_data *evt_dat = NULL;
869 struct lpfc_iocbq *iocbq;
871 struct list_head head;
872 struct ulp_bde64 *bde;
875 struct lpfc_dmabuf *bdeBuf1 = piocbq->context2;
876 struct lpfc_dmabuf *bdeBuf2 = piocbq->context3;
877 struct lpfc_hbq_entry *hbqe;
878 struct lpfc_sli_ct_request *ct_req;
879 struct fc_bsg_job *job = NULL;
880 struct bsg_job_data *dd_data = NULL;
884 INIT_LIST_HEAD(&head);
885 list_add_tail(&head, &piocbq->list);
887 if (piocbq->iocb.ulpBdeCount == 0 ||
888 piocbq->iocb.un.cont64[0].tus.f.bdeSize == 0)
889 goto error_ct_unsol_exit;
891 if (phba->link_state == LPFC_HBA_ERROR ||
892 (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)))
893 goto error_ct_unsol_exit;
895 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
898 dma_addr = getPaddr(piocbq->iocb.un.cont64[0].addrHigh,
899 piocbq->iocb.un.cont64[0].addrLow);
900 dmabuf = lpfc_sli_ringpostbuf_get(phba, pring, dma_addr);
903 goto error_ct_unsol_exit;
904 ct_req = (struct lpfc_sli_ct_request *)dmabuf->virt;
905 evt_req_id = ct_req->FsType;
906 cmd = ct_req->CommandResponse.bits.CmdRsp;
907 len = ct_req->CommandResponse.bits.Size;
908 if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
909 lpfc_sli_ringpostbuf_put(phba, pring, dmabuf);
911 spin_lock_irqsave(&phba->ct_ev_lock, flags);
912 list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
913 if (!(evt->type_mask & FC_REG_CT_EVENT) ||
914 evt->req_id != evt_req_id)
917 lpfc_bsg_event_ref(evt);
918 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
919 evt_dat = kzalloc(sizeof(*evt_dat), GFP_KERNEL);
920 if (evt_dat == NULL) {
921 spin_lock_irqsave(&phba->ct_ev_lock, flags);
922 lpfc_bsg_event_unref(evt);
923 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
924 "2614 Memory allocation failed for "
929 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
930 /* take accumulated byte count from the last iocbq */
931 iocbq = list_entry(head.prev, typeof(*iocbq), list);
932 evt_dat->len = iocbq->iocb.unsli3.rcvsli3.acc_len;
934 list_for_each_entry(iocbq, &head, list) {
935 for (i = 0; i < iocbq->iocb.ulpBdeCount; i++)
937 iocbq->iocb.un.cont64[i].tus.f.bdeSize;
941 evt_dat->data = kzalloc(evt_dat->len, GFP_KERNEL);
942 if (evt_dat->data == NULL) {
943 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
944 "2615 Memory allocation failed for "
945 "CT event data, size %d\n",
948 spin_lock_irqsave(&phba->ct_ev_lock, flags);
949 lpfc_bsg_event_unref(evt);
950 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
951 goto error_ct_unsol_exit;
954 list_for_each_entry(iocbq, &head, list) {
956 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
957 bdeBuf1 = iocbq->context2;
958 bdeBuf2 = iocbq->context3;
960 for (i = 0; i < iocbq->iocb.ulpBdeCount; i++) {
961 if (phba->sli3_options &
962 LPFC_SLI3_HBQ_ENABLED) {
964 hbqe = (struct lpfc_hbq_entry *)
965 &iocbq->iocb.un.ulpWord[0];
966 size = hbqe->bde.tus.f.bdeSize;
969 hbqe = (struct lpfc_hbq_entry *)
972 size = hbqe->bde.tus.f.bdeSize;
975 if ((offset + size) > evt_dat->len)
976 size = evt_dat->len - offset;
978 size = iocbq->iocb.un.cont64[i].
980 bde = &iocbq->iocb.un.cont64[i];
981 dma_addr = getPaddr(bde->addrHigh,
983 dmabuf = lpfc_sli_ringpostbuf_get(phba,
987 lpfc_printf_log(phba, KERN_ERR,
988 LOG_LIBDFC, "2616 No dmabuf "
989 "found for iocbq 0x%p\n",
991 kfree(evt_dat->data);
993 spin_lock_irqsave(&phba->ct_ev_lock,
995 lpfc_bsg_event_unref(evt);
996 spin_unlock_irqrestore(
997 &phba->ct_ev_lock, flags);
998 goto error_ct_unsol_exit;
1000 memcpy((char *)(evt_dat->data) + offset,
1001 dmabuf->virt, size);
1003 if (evt_req_id != SLI_CT_ELX_LOOPBACK &&
1004 !(phba->sli3_options &
1005 LPFC_SLI3_HBQ_ENABLED)) {
1006 lpfc_sli_ringpostbuf_put(phba, pring,
1010 case ELX_LOOPBACK_DATA:
1013 diag_cmd_data_free(phba,
1014 (struct lpfc_dmabufext
1017 case ELX_LOOPBACK_XRI_SETUP:
1018 if ((phba->sli_rev ==
1020 (phba->sli3_options &
1021 LPFC_SLI3_HBQ_ENABLED
1023 lpfc_in_buf_free(phba,
1026 lpfc_post_buffer(phba,
1032 if (!(phba->sli3_options &
1033 LPFC_SLI3_HBQ_ENABLED))
1034 lpfc_post_buffer(phba,
1043 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1044 if (phba->sli_rev == LPFC_SLI_REV4) {
1045 evt_dat->immed_dat = phba->ctx_idx;
1046 phba->ctx_idx = (phba->ctx_idx + 1) % LPFC_CT_CTX_MAX;
1047 /* Provide warning for over-run of the ct_ctx array */
1048 if (phba->ct_ctx[evt_dat->immed_dat].valid ==
1050 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
1051 "2717 CT context array entry "
1052 "[%d] over-run: oxid:x%x, "
1053 "sid:x%x\n", phba->ctx_idx,
1055 evt_dat->immed_dat].oxid,
1057 evt_dat->immed_dat].SID);
1058 phba->ct_ctx[evt_dat->immed_dat].rxid =
1059 piocbq->iocb.ulpContext;
1060 phba->ct_ctx[evt_dat->immed_dat].oxid =
1061 piocbq->iocb.unsli3.rcvsli3.ox_id;
1062 phba->ct_ctx[evt_dat->immed_dat].SID =
1063 piocbq->iocb.un.rcvels.remoteID;
1064 phba->ct_ctx[evt_dat->immed_dat].valid = UNSOL_VALID;
1066 evt_dat->immed_dat = piocbq->iocb.ulpContext;
1068 evt_dat->type = FC_REG_CT_EVENT;
1069 list_add(&evt_dat->node, &evt->events_to_see);
1070 if (evt_req_id == SLI_CT_ELX_LOOPBACK) {
1071 wake_up_interruptible(&evt->wq);
1072 lpfc_bsg_event_unref(evt);
1076 list_move(evt->events_to_see.prev, &evt->events_to_get);
1078 dd_data = (struct bsg_job_data *)evt->dd_data;
1079 job = dd_data->set_job;
1080 dd_data->set_job = NULL;
1081 lpfc_bsg_event_unref(evt);
1083 job->reply->reply_payload_rcv_len = size;
1084 /* make error code available to userspace */
1085 job->reply->result = 0;
1086 job->dd_data = NULL;
1087 /* complete the job back to userspace */
1088 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1090 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1093 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1095 error_ct_unsol_exit:
1096 if (!list_empty(&head))
1098 if ((phba->sli_rev < LPFC_SLI_REV4) &&
1099 (evt_req_id == SLI_CT_ELX_LOOPBACK))
1105 * lpfc_bsg_ct_unsol_abort - handler ct abort to management plane
1106 * @phba: Pointer to HBA context object.
1107 * @dmabuf: pointer to a dmabuf that describes the FC sequence
1109 * This function handles abort to the CT command toward management plane
1112 * If the pending context of a CT command to management plane present, clears
1113 * such context and returns 1 for handled; otherwise, it returns 0 indicating
1114 * no context exists.
1117 lpfc_bsg_ct_unsol_abort(struct lpfc_hba *phba, struct hbq_dmabuf *dmabuf)
1119 struct fc_frame_header fc_hdr;
1120 struct fc_frame_header *fc_hdr_ptr = &fc_hdr;
1121 int ctx_idx, handled = 0;
1122 uint16_t oxid, rxid;
1125 memcpy(fc_hdr_ptr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
1126 sid = sli4_sid_from_fc_hdr(fc_hdr_ptr);
1127 oxid = be16_to_cpu(fc_hdr_ptr->fh_ox_id);
1128 rxid = be16_to_cpu(fc_hdr_ptr->fh_rx_id);
1130 for (ctx_idx = 0; ctx_idx < LPFC_CT_CTX_MAX; ctx_idx++) {
1131 if (phba->ct_ctx[ctx_idx].valid != UNSOL_VALID)
1133 if (phba->ct_ctx[ctx_idx].rxid != rxid)
1135 if (phba->ct_ctx[ctx_idx].oxid != oxid)
1137 if (phba->ct_ctx[ctx_idx].SID != sid)
1139 phba->ct_ctx[ctx_idx].valid = UNSOL_INVALID;
1146 * lpfc_bsg_hba_set_event - process a SET_EVENT bsg vendor command
1147 * @job: SET_EVENT fc_bsg_job
1150 lpfc_bsg_hba_set_event(struct fc_bsg_job *job)
1152 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
1153 struct lpfc_hba *phba = vport->phba;
1154 struct set_ct_event *event_req;
1155 struct lpfc_bsg_event *evt;
1157 struct bsg_job_data *dd_data = NULL;
1159 unsigned long flags;
1161 if (job->request_len <
1162 sizeof(struct fc_bsg_request) + sizeof(struct set_ct_event)) {
1163 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1164 "2612 Received SET_CT_EVENT below minimum "
1170 event_req = (struct set_ct_event *)
1171 job->request->rqst_data.h_vendor.vendor_cmd;
1172 ev_mask = ((uint32_t)(unsigned long)event_req->type_mask &
1174 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1175 list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
1176 if (evt->reg_id == event_req->ev_reg_id) {
1177 lpfc_bsg_event_ref(evt);
1178 evt->wait_time_stamp = jiffies;
1179 dd_data = (struct bsg_job_data *)evt->dd_data;
1183 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1185 if (&evt->node == &phba->ct_ev_waiters) {
1186 /* no event waiting struct yet - first call */
1187 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
1188 if (dd_data == NULL) {
1189 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1190 "2734 Failed allocation of dd_data\n");
1194 evt = lpfc_bsg_event_new(ev_mask, event_req->ev_reg_id,
1195 event_req->ev_req_id);
1197 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1198 "2617 Failed allocation of event "
1203 dd_data->type = TYPE_EVT;
1204 dd_data->set_job = NULL;
1205 dd_data->context_un.evt = evt;
1206 evt->dd_data = (void *)dd_data;
1207 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1208 list_add(&evt->node, &phba->ct_ev_waiters);
1209 lpfc_bsg_event_ref(evt);
1210 evt->wait_time_stamp = jiffies;
1211 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1214 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1216 dd_data->set_job = job; /* for unsolicited command */
1217 job->dd_data = dd_data; /* for fc transport timeout callback*/
1218 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1219 return 0; /* call job done later */
1222 if (dd_data != NULL)
1225 job->dd_data = NULL;
1230 * lpfc_bsg_hba_get_event - process a GET_EVENT bsg vendor command
1231 * @job: GET_EVENT fc_bsg_job
1234 lpfc_bsg_hba_get_event(struct fc_bsg_job *job)
1236 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
1237 struct lpfc_hba *phba = vport->phba;
1238 struct get_ct_event *event_req;
1239 struct get_ct_event_reply *event_reply;
1240 struct lpfc_bsg_event *evt;
1241 struct event_data *evt_dat = NULL;
1242 unsigned long flags;
1245 if (job->request_len <
1246 sizeof(struct fc_bsg_request) + sizeof(struct get_ct_event)) {
1247 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1248 "2613 Received GET_CT_EVENT request below "
1254 event_req = (struct get_ct_event *)
1255 job->request->rqst_data.h_vendor.vendor_cmd;
1257 event_reply = (struct get_ct_event_reply *)
1258 job->reply->reply_data.vendor_reply.vendor_rsp;
1259 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1260 list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
1261 if (evt->reg_id == event_req->ev_reg_id) {
1262 if (list_empty(&evt->events_to_get))
1264 lpfc_bsg_event_ref(evt);
1265 evt->wait_time_stamp = jiffies;
1266 evt_dat = list_entry(evt->events_to_get.prev,
1267 struct event_data, node);
1268 list_del(&evt_dat->node);
1272 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1274 /* The app may continue to ask for event data until it gets
1275 * an error indicating that there isn't anymore
1277 if (evt_dat == NULL) {
1278 job->reply->reply_payload_rcv_len = 0;
1283 if (evt_dat->len > job->request_payload.payload_len) {
1284 evt_dat->len = job->request_payload.payload_len;
1285 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1286 "2618 Truncated event data at %d "
1288 job->request_payload.payload_len);
1291 event_reply->type = evt_dat->type;
1292 event_reply->immed_data = evt_dat->immed_dat;
1293 if (evt_dat->len > 0)
1294 job->reply->reply_payload_rcv_len =
1295 sg_copy_from_buffer(job->request_payload.sg_list,
1296 job->request_payload.sg_cnt,
1297 evt_dat->data, evt_dat->len);
1299 job->reply->reply_payload_rcv_len = 0;
1302 kfree(evt_dat->data);
1306 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1307 lpfc_bsg_event_unref(evt);
1308 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1309 job->dd_data = NULL;
1310 job->reply->result = 0;
1315 job->dd_data = NULL;
1316 job->reply->result = rc;
1321 * lpfc_issue_ct_rsp_cmp - lpfc_issue_ct_rsp's completion handler
1322 * @phba: Pointer to HBA context object.
1323 * @cmdiocbq: Pointer to command iocb.
1324 * @rspiocbq: Pointer to response iocb.
1326 * This function is the completion handler for iocbs issued using
1327 * lpfc_issue_ct_rsp_cmp function. This function is called by the
1328 * ring event handler function without any lock held. This function
1329 * can be called from both worker thread context and interrupt
1330 * context. This function also can be called from other thread which
1331 * cleans up the SLI layer objects.
1332 * This function copy the contents of the response iocb to the
1333 * response iocb memory object provided by the caller of
1334 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
1335 * sleeps for the iocb completion.
1338 lpfc_issue_ct_rsp_cmp(struct lpfc_hba *phba,
1339 struct lpfc_iocbq *cmdiocbq,
1340 struct lpfc_iocbq *rspiocbq)
1342 struct bsg_job_data *dd_data;
1343 struct fc_bsg_job *job;
1345 struct lpfc_dmabuf *bmp, *cmp;
1346 struct lpfc_nodelist *ndlp;
1347 unsigned long flags;
1350 dd_data = cmdiocbq->context1;
1352 /* Determine if job has been aborted */
1353 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1354 job = dd_data->set_job;
1356 /* Prevent timeout handling from trying to abort job */
1357 job->dd_data = NULL;
1359 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1361 ndlp = dd_data->context_un.iocb.ndlp;
1362 cmp = cmdiocbq->context2;
1363 bmp = cmdiocbq->context3;
1364 rsp = &rspiocbq->iocb;
1366 /* Copy the completed job data or set the error status */
1369 if (rsp->ulpStatus) {
1370 if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
1371 switch (rsp->un.ulpWord[4] & IOERR_PARAM_MASK) {
1372 case IOERR_SEQUENCE_TIMEOUT:
1375 case IOERR_INVALID_RPI:
1386 job->reply->reply_payload_rcv_len = 0;
1390 lpfc_free_bsg_buffers(phba, cmp);
1391 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
1393 lpfc_sli_release_iocbq(phba, cmdiocbq);
1397 /* Complete the job if the job is still active */
1400 job->reply->result = rc;
1407 * lpfc_issue_ct_rsp - issue a ct response
1408 * @phba: Pointer to HBA context object.
1409 * @job: Pointer to the job object.
1410 * @tag: tag index value into the ports context exchange array.
1411 * @bmp: Pointer to a dma buffer descriptor.
1412 * @num_entry: Number of enties in the bde.
1415 lpfc_issue_ct_rsp(struct lpfc_hba *phba, struct fc_bsg_job *job, uint32_t tag,
1416 struct lpfc_dmabuf *cmp, struct lpfc_dmabuf *bmp,
1420 struct lpfc_iocbq *ctiocb = NULL;
1422 struct lpfc_nodelist *ndlp = NULL;
1423 struct bsg_job_data *dd_data;
1426 /* allocate our bsg tracking structure */
1427 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
1429 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1430 "2736 Failed allocation of dd_data\n");
1435 /* Allocate buffer for command iocb */
1436 ctiocb = lpfc_sli_get_iocbq(phba);
1442 icmd = &ctiocb->iocb;
1443 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
1444 icmd->un.xseq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
1445 icmd->un.xseq64.bdl.addrLow = putPaddrLow(bmp->phys);
1446 icmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
1447 icmd->un.xseq64.bdl.bdeSize = (num_entry * sizeof(struct ulp_bde64));
1448 icmd->un.xseq64.w5.hcsw.Fctl = (LS | LA);
1449 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
1450 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_SOL_CTL;
1451 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
1453 /* Fill in rest of iocb */
1454 icmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;
1455 icmd->ulpBdeCount = 1;
1457 icmd->ulpClass = CLASS3;
1458 if (phba->sli_rev == LPFC_SLI_REV4) {
1459 /* Do not issue unsol response if oxid not marked as valid */
1460 if (phba->ct_ctx[tag].valid != UNSOL_VALID) {
1462 goto issue_ct_rsp_exit;
1464 icmd->ulpContext = phba->ct_ctx[tag].rxid;
1465 icmd->unsli3.rcvsli3.ox_id = phba->ct_ctx[tag].oxid;
1466 ndlp = lpfc_findnode_did(phba->pport, phba->ct_ctx[tag].SID);
1468 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
1469 "2721 ndlp null for oxid %x SID %x\n",
1471 phba->ct_ctx[tag].SID);
1473 goto issue_ct_rsp_exit;
1476 /* Check if the ndlp is active */
1477 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
1479 goto issue_ct_rsp_exit;
1482 /* get a refernece count so the ndlp doesn't go away while
1485 if (!lpfc_nlp_get(ndlp)) {
1487 goto issue_ct_rsp_exit;
1490 icmd->un.ulpWord[3] =
1491 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
1493 /* The exchange is done, mark the entry as invalid */
1494 phba->ct_ctx[tag].valid = UNSOL_INVALID;
1496 icmd->ulpContext = (ushort) tag;
1498 icmd->ulpTimeout = phba->fc_ratov * 2;
1500 /* Xmit CT response on exchange <xid> */
1501 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
1502 "2722 Xmit CT response on exchange x%x Data: x%x x%x x%x\n",
1503 icmd->ulpContext, icmd->ulpIoTag, tag, phba->link_state);
1505 ctiocb->iocb_cmpl = NULL;
1506 ctiocb->iocb_flag |= LPFC_IO_LIBDFC;
1507 ctiocb->vport = phba->pport;
1508 ctiocb->context1 = dd_data;
1509 ctiocb->context2 = cmp;
1510 ctiocb->context3 = bmp;
1511 ctiocb->iocb_cmpl = lpfc_issue_ct_rsp_cmp;
1513 dd_data->type = TYPE_IOCB;
1514 dd_data->set_job = job;
1515 dd_data->context_un.iocb.cmdiocbq = ctiocb;
1516 dd_data->context_un.iocb.ndlp = ndlp;
1517 dd_data->context_un.iocb.rmp = NULL;
1518 job->dd_data = dd_data;
1520 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
1521 if (lpfc_readl(phba->HCregaddr, &creg_val)) {
1523 goto issue_ct_rsp_exit;
1525 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
1526 writel(creg_val, phba->HCregaddr);
1527 readl(phba->HCregaddr); /* flush */
1530 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
1532 if (rc == IOCB_SUCCESS)
1533 return 0; /* done for now */
1536 lpfc_sli_release_iocbq(phba, ctiocb);
1544 * lpfc_bsg_send_mgmt_rsp - process a SEND_MGMT_RESP bsg vendor command
1545 * @job: SEND_MGMT_RESP fc_bsg_job
1548 lpfc_bsg_send_mgmt_rsp(struct fc_bsg_job *job)
1550 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
1551 struct lpfc_hba *phba = vport->phba;
1552 struct send_mgmt_resp *mgmt_resp = (struct send_mgmt_resp *)
1553 job->request->rqst_data.h_vendor.vendor_cmd;
1554 struct ulp_bde64 *bpl;
1555 struct lpfc_dmabuf *bmp = NULL, *cmp = NULL;
1557 uint32_t tag = mgmt_resp->tag;
1558 unsigned long reqbfrcnt =
1559 (unsigned long)job->request_payload.payload_len;
1562 /* in case no data is transferred */
1563 job->reply->reply_payload_rcv_len = 0;
1565 if (!reqbfrcnt || (reqbfrcnt > (80 * BUF_SZ_4K))) {
1567 goto send_mgmt_rsp_exit;
1570 bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
1573 goto send_mgmt_rsp_exit;
1576 bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
1579 goto send_mgmt_rsp_free_bmp;
1582 INIT_LIST_HEAD(&bmp->list);
1583 bpl = (struct ulp_bde64 *) bmp->virt;
1584 bpl_entries = (LPFC_BPL_SIZE/sizeof(struct ulp_bde64));
1585 cmp = lpfc_alloc_bsg_buffers(phba, job->request_payload.payload_len,
1586 1, bpl, &bpl_entries);
1589 goto send_mgmt_rsp_free_bmp;
1591 lpfc_bsg_copy_data(cmp, &job->request_payload,
1592 job->request_payload.payload_len, 1);
1594 rc = lpfc_issue_ct_rsp(phba, job, tag, cmp, bmp, bpl_entries);
1596 if (rc == IOCB_SUCCESS)
1597 return 0; /* done for now */
1601 lpfc_free_bsg_buffers(phba, cmp);
1603 send_mgmt_rsp_free_bmp:
1605 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
1608 /* make error code available to userspace */
1609 job->reply->result = rc;
1610 job->dd_data = NULL;
1615 * lpfc_bsg_diag_mode_enter - process preparing into device diag loopback mode
1616 * @phba: Pointer to HBA context object.
1618 * This function is responsible for preparing driver for diag loopback
1622 lpfc_bsg_diag_mode_enter(struct lpfc_hba *phba)
1624 struct lpfc_vport **vports;
1625 struct Scsi_Host *shost;
1626 struct lpfc_sli *psli;
1627 struct lpfc_sli_ring *pring;
1634 pring = &psli->ring[LPFC_FCP_RING];
1638 if ((phba->link_state == LPFC_HBA_ERROR) ||
1639 (psli->sli_flag & LPFC_BLOCK_MGMT_IO) ||
1640 (!(psli->sli_flag & LPFC_SLI_ACTIVE)))
1643 vports = lpfc_create_vport_work_array(phba);
1645 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
1646 shost = lpfc_shost_from_vport(vports[i]);
1647 scsi_block_requests(shost);
1649 lpfc_destroy_vport_work_array(phba, vports);
1651 shost = lpfc_shost_from_vport(phba->pport);
1652 scsi_block_requests(shost);
1655 while (!list_empty(&pring->txcmplq)) {
1656 if (i++ > 500) /* wait up to 5 seconds */
1664 * lpfc_bsg_diag_mode_exit - exit process from device diag loopback mode
1665 * @phba: Pointer to HBA context object.
1667 * This function is responsible for driver exit processing of setting up
1668 * diag loopback mode on device.
1671 lpfc_bsg_diag_mode_exit(struct lpfc_hba *phba)
1673 struct Scsi_Host *shost;
1674 struct lpfc_vport **vports;
1677 vports = lpfc_create_vport_work_array(phba);
1679 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
1680 shost = lpfc_shost_from_vport(vports[i]);
1681 scsi_unblock_requests(shost);
1683 lpfc_destroy_vport_work_array(phba, vports);
1685 shost = lpfc_shost_from_vport(phba->pport);
1686 scsi_unblock_requests(shost);
1692 * lpfc_sli3_bsg_diag_loopback_mode - process an sli3 bsg vendor command
1693 * @phba: Pointer to HBA context object.
1694 * @job: LPFC_BSG_VENDOR_DIAG_MODE
1696 * This function is responsible for placing an sli3 port into diagnostic
1697 * loopback mode in order to perform a diagnostic loopback test.
1698 * All new scsi requests are blocked, a small delay is used to allow the
1699 * scsi requests to complete then the link is brought down. If the link is
1700 * is placed in loopback mode then scsi requests are again allowed
1701 * so the scsi mid-layer doesn't give up on the port.
1702 * All of this is done in-line.
1705 lpfc_sli3_bsg_diag_loopback_mode(struct lpfc_hba *phba, struct fc_bsg_job *job)
1707 struct diag_mode_set *loopback_mode;
1708 uint32_t link_flags;
1710 LPFC_MBOXQ_t *pmboxq = NULL;
1711 int mbxstatus = MBX_SUCCESS;
1715 /* no data to return just the return code */
1716 job->reply->reply_payload_rcv_len = 0;
1718 if (job->request_len < sizeof(struct fc_bsg_request) +
1719 sizeof(struct diag_mode_set)) {
1720 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1721 "2738 Received DIAG MODE request size:%d "
1722 "below the minimum size:%d\n",
1724 (int)(sizeof(struct fc_bsg_request) +
1725 sizeof(struct diag_mode_set)));
1730 rc = lpfc_bsg_diag_mode_enter(phba);
1734 /* bring the link to diagnostic mode */
1735 loopback_mode = (struct diag_mode_set *)
1736 job->request->rqst_data.h_vendor.vendor_cmd;
1737 link_flags = loopback_mode->type;
1738 timeout = loopback_mode->timeout * 100;
1740 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1743 goto loopback_mode_exit;
1745 memset((void *)pmboxq, 0, sizeof(LPFC_MBOXQ_t));
1746 pmboxq->u.mb.mbxCommand = MBX_DOWN_LINK;
1747 pmboxq->u.mb.mbxOwner = OWN_HOST;
1749 mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
1751 if ((mbxstatus == MBX_SUCCESS) && (pmboxq->u.mb.mbxStatus == 0)) {
1752 /* wait for link down before proceeding */
1754 while (phba->link_state != LPFC_LINK_DOWN) {
1755 if (i++ > timeout) {
1757 goto loopback_mode_exit;
1762 memset((void *)pmboxq, 0, sizeof(LPFC_MBOXQ_t));
1763 if (link_flags == INTERNAL_LOOP_BACK)
1764 pmboxq->u.mb.un.varInitLnk.link_flags = FLAGS_LOCAL_LB;
1766 pmboxq->u.mb.un.varInitLnk.link_flags =
1767 FLAGS_TOPOLOGY_MODE_LOOP;
1769 pmboxq->u.mb.mbxCommand = MBX_INIT_LINK;
1770 pmboxq->u.mb.mbxOwner = OWN_HOST;
1772 mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq,
1775 if ((mbxstatus != MBX_SUCCESS) || (pmboxq->u.mb.mbxStatus))
1778 spin_lock_irq(&phba->hbalock);
1779 phba->link_flag |= LS_LOOPBACK_MODE;
1780 spin_unlock_irq(&phba->hbalock);
1781 /* wait for the link attention interrupt */
1785 while (phba->link_state != LPFC_HBA_READY) {
1786 if (i++ > timeout) {
1799 lpfc_bsg_diag_mode_exit(phba);
1802 * Let SLI layer release mboxq if mbox command completed after timeout.
1804 if (pmboxq && mbxstatus != MBX_TIMEOUT)
1805 mempool_free(pmboxq, phba->mbox_mem_pool);
1808 /* make error code available to userspace */
1809 job->reply->result = rc;
1810 /* complete the job back to userspace if no error */
1817 * lpfc_sli4_bsg_set_link_diag_state - set sli4 link diag state
1818 * @phba: Pointer to HBA context object.
1819 * @diag: Flag for set link to diag or nomral operation state.
1821 * This function is responsible for issuing a sli4 mailbox command for setting
1822 * link to either diag state or normal operation state.
1825 lpfc_sli4_bsg_set_link_diag_state(struct lpfc_hba *phba, uint32_t diag)
1827 LPFC_MBOXQ_t *pmboxq;
1828 struct lpfc_mbx_set_link_diag_state *link_diag_state;
1829 uint32_t req_len, alloc_len;
1830 int mbxstatus = MBX_SUCCESS, rc;
1832 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1836 req_len = (sizeof(struct lpfc_mbx_set_link_diag_state) -
1837 sizeof(struct lpfc_sli4_cfg_mhdr));
1838 alloc_len = lpfc_sli4_config(phba, pmboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
1839 LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_STATE,
1840 req_len, LPFC_SLI4_MBX_EMBED);
1841 if (alloc_len != req_len) {
1843 goto link_diag_state_set_out;
1845 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
1846 "3128 Set link to diagnostic state:x%x (x%x/x%x)\n",
1847 diag, phba->sli4_hba.lnk_info.lnk_tp,
1848 phba->sli4_hba.lnk_info.lnk_no);
1850 link_diag_state = &pmboxq->u.mqe.un.link_diag_state;
1851 bf_set(lpfc_mbx_set_diag_state_diag_bit_valid, &link_diag_state->u.req,
1852 LPFC_DIAG_STATE_DIAG_BIT_VALID_CHANGE);
1853 bf_set(lpfc_mbx_set_diag_state_link_num, &link_diag_state->u.req,
1854 phba->sli4_hba.lnk_info.lnk_no);
1855 bf_set(lpfc_mbx_set_diag_state_link_type, &link_diag_state->u.req,
1856 phba->sli4_hba.lnk_info.lnk_tp);
1858 bf_set(lpfc_mbx_set_diag_state_diag,
1859 &link_diag_state->u.req, 1);
1861 bf_set(lpfc_mbx_set_diag_state_diag,
1862 &link_diag_state->u.req, 0);
1864 mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
1866 if ((mbxstatus == MBX_SUCCESS) && (pmboxq->u.mb.mbxStatus == 0))
1871 link_diag_state_set_out:
1872 if (pmboxq && (mbxstatus != MBX_TIMEOUT))
1873 mempool_free(pmboxq, phba->mbox_mem_pool);
1879 * lpfc_sli4_bsg_set_internal_loopback - set sli4 internal loopback diagnostic
1880 * @phba: Pointer to HBA context object.
1882 * This function is responsible for issuing a sli4 mailbox command for setting
1883 * up internal loopback diagnostic.
1886 lpfc_sli4_bsg_set_internal_loopback(struct lpfc_hba *phba)
1888 LPFC_MBOXQ_t *pmboxq;
1889 uint32_t req_len, alloc_len;
1890 struct lpfc_mbx_set_link_diag_loopback *link_diag_loopback;
1891 int mbxstatus = MBX_SUCCESS, rc = 0;
1893 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1896 req_len = (sizeof(struct lpfc_mbx_set_link_diag_loopback) -
1897 sizeof(struct lpfc_sli4_cfg_mhdr));
1898 alloc_len = lpfc_sli4_config(phba, pmboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
1899 LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_LOOPBACK,
1900 req_len, LPFC_SLI4_MBX_EMBED);
1901 if (alloc_len != req_len) {
1902 mempool_free(pmboxq, phba->mbox_mem_pool);
1905 link_diag_loopback = &pmboxq->u.mqe.un.link_diag_loopback;
1906 bf_set(lpfc_mbx_set_diag_state_link_num,
1907 &link_diag_loopback->u.req, phba->sli4_hba.lnk_info.lnk_no);
1908 bf_set(lpfc_mbx_set_diag_state_link_type,
1909 &link_diag_loopback->u.req, phba->sli4_hba.lnk_info.lnk_tp);
1910 bf_set(lpfc_mbx_set_diag_lpbk_type, &link_diag_loopback->u.req,
1911 LPFC_DIAG_LOOPBACK_TYPE_INTERNAL);
1913 mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
1914 if ((mbxstatus != MBX_SUCCESS) || (pmboxq->u.mb.mbxStatus)) {
1915 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1916 "3127 Failed setup loopback mode mailbox "
1917 "command, rc:x%x, status:x%x\n", mbxstatus,
1918 pmboxq->u.mb.mbxStatus);
1921 if (pmboxq && (mbxstatus != MBX_TIMEOUT))
1922 mempool_free(pmboxq, phba->mbox_mem_pool);
1927 * lpfc_sli4_diag_fcport_reg_setup - setup port registrations for diagnostic
1928 * @phba: Pointer to HBA context object.
1930 * This function set up SLI4 FC port registrations for diagnostic run, which
1931 * includes all the rpis, vfi, and also vpi.
1934 lpfc_sli4_diag_fcport_reg_setup(struct lpfc_hba *phba)
1938 if (phba->pport->fc_flag & FC_VFI_REGISTERED) {
1939 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1940 "3136 Port still had vfi registered: "
1941 "mydid:x%x, fcfi:%d, vfi:%d, vpi:%d\n",
1942 phba->pport->fc_myDID, phba->fcf.fcfi,
1943 phba->sli4_hba.vfi_ids[phba->pport->vfi],
1944 phba->vpi_ids[phba->pport->vpi]);
1947 rc = lpfc_issue_reg_vfi(phba->pport);
1952 * lpfc_sli4_bsg_diag_loopback_mode - process an sli4 bsg vendor command
1953 * @phba: Pointer to HBA context object.
1954 * @job: LPFC_BSG_VENDOR_DIAG_MODE
1956 * This function is responsible for placing an sli4 port into diagnostic
1957 * loopback mode in order to perform a diagnostic loopback test.
1960 lpfc_sli4_bsg_diag_loopback_mode(struct lpfc_hba *phba, struct fc_bsg_job *job)
1962 struct diag_mode_set *loopback_mode;
1963 uint32_t link_flags, timeout;
1966 /* no data to return just the return code */
1967 job->reply->reply_payload_rcv_len = 0;
1969 if (job->request_len < sizeof(struct fc_bsg_request) +
1970 sizeof(struct diag_mode_set)) {
1971 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1972 "3011 Received DIAG MODE request size:%d "
1973 "below the minimum size:%d\n",
1975 (int)(sizeof(struct fc_bsg_request) +
1976 sizeof(struct diag_mode_set)));
1981 rc = lpfc_bsg_diag_mode_enter(phba);
1985 /* indicate we are in loobpack diagnostic mode */
1986 spin_lock_irq(&phba->hbalock);
1987 phba->link_flag |= LS_LOOPBACK_MODE;
1988 spin_unlock_irq(&phba->hbalock);
1990 /* reset port to start frome scratch */
1991 rc = lpfc_selective_reset(phba);
1995 /* bring the link to diagnostic mode */
1996 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
1997 "3129 Bring link to diagnostic state.\n");
1998 loopback_mode = (struct diag_mode_set *)
1999 job->request->rqst_data.h_vendor.vendor_cmd;
2000 link_flags = loopback_mode->type;
2001 timeout = loopback_mode->timeout * 100;
2003 rc = lpfc_sli4_bsg_set_link_diag_state(phba, 1);
2005 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2006 "3130 Failed to bring link to diagnostic "
2007 "state, rc:x%x\n", rc);
2008 goto loopback_mode_exit;
2011 /* wait for link down before proceeding */
2013 while (phba->link_state != LPFC_LINK_DOWN) {
2014 if (i++ > timeout) {
2016 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
2017 "3131 Timeout waiting for link to "
2018 "diagnostic mode, timeout:%d ms\n",
2020 goto loopback_mode_exit;
2025 /* set up loopback mode */
2026 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
2027 "3132 Set up loopback mode:x%x\n", link_flags);
2029 if (link_flags == INTERNAL_LOOP_BACK)
2030 rc = lpfc_sli4_bsg_set_internal_loopback(phba);
2031 else if (link_flags == EXTERNAL_LOOP_BACK)
2032 rc = lpfc_hba_init_link_fc_topology(phba,
2033 FLAGS_TOPOLOGY_MODE_PT_PT,
2037 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
2038 "3141 Loopback mode:x%x not supported\n",
2040 goto loopback_mode_exit;
2044 /* wait for the link attention interrupt */
2047 while (phba->link_state < LPFC_LINK_UP) {
2048 if (i++ > timeout) {
2050 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
2051 "3137 Timeout waiting for link up "
2052 "in loopback mode, timeout:%d ms\n",
2060 /* port resource registration setup for loopback diagnostic */
2062 /* set up a none zero myDID for loopback test */
2063 phba->pport->fc_myDID = 1;
2064 rc = lpfc_sli4_diag_fcport_reg_setup(phba);
2066 goto loopback_mode_exit;
2069 /* wait for the port ready */
2072 while (phba->link_state != LPFC_HBA_READY) {
2073 if (i++ > timeout) {
2075 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
2076 "3133 Timeout waiting for port "
2077 "loopback mode ready, timeout:%d ms\n",
2086 /* clear loopback diagnostic mode */
2088 spin_lock_irq(&phba->hbalock);
2089 phba->link_flag &= ~LS_LOOPBACK_MODE;
2090 spin_unlock_irq(&phba->hbalock);
2092 lpfc_bsg_diag_mode_exit(phba);
2095 /* make error code available to userspace */
2096 job->reply->result = rc;
2097 /* complete the job back to userspace if no error */
2104 * lpfc_bsg_diag_loopback_mode - bsg vendor command for diag loopback mode
2105 * @job: LPFC_BSG_VENDOR_DIAG_MODE
2107 * This function is responsible for responding to check and dispatch bsg diag
2108 * command from the user to proper driver action routines.
2111 lpfc_bsg_diag_loopback_mode(struct fc_bsg_job *job)
2113 struct Scsi_Host *shost;
2114 struct lpfc_vport *vport;
2115 struct lpfc_hba *phba;
2121 vport = (struct lpfc_vport *)job->shost->hostdata;
2128 if (phba->sli_rev < LPFC_SLI_REV4)
2129 rc = lpfc_sli3_bsg_diag_loopback_mode(phba, job);
2130 else if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
2131 LPFC_SLI_INTF_IF_TYPE_2)
2132 rc = lpfc_sli4_bsg_diag_loopback_mode(phba, job);
2140 * lpfc_sli4_bsg_diag_mode_end - sli4 bsg vendor command for ending diag mode
2141 * @job: LPFC_BSG_VENDOR_DIAG_MODE_END
2143 * This function is responsible for responding to check and dispatch bsg diag
2144 * command from the user to proper driver action routines.
2147 lpfc_sli4_bsg_diag_mode_end(struct fc_bsg_job *job)
2149 struct Scsi_Host *shost;
2150 struct lpfc_vport *vport;
2151 struct lpfc_hba *phba;
2152 struct diag_mode_set *loopback_mode_end_cmd;
2159 vport = (struct lpfc_vport *)job->shost->hostdata;
2166 if (phba->sli_rev < LPFC_SLI_REV4)
2168 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
2169 LPFC_SLI_INTF_IF_TYPE_2)
2172 /* clear loopback diagnostic mode */
2173 spin_lock_irq(&phba->hbalock);
2174 phba->link_flag &= ~LS_LOOPBACK_MODE;
2175 spin_unlock_irq(&phba->hbalock);
2176 loopback_mode_end_cmd = (struct diag_mode_set *)
2177 job->request->rqst_data.h_vendor.vendor_cmd;
2178 timeout = loopback_mode_end_cmd->timeout * 100;
2180 rc = lpfc_sli4_bsg_set_link_diag_state(phba, 0);
2182 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2183 "3139 Failed to bring link to diagnostic "
2184 "state, rc:x%x\n", rc);
2185 goto loopback_mode_end_exit;
2188 /* wait for link down before proceeding */
2190 while (phba->link_state != LPFC_LINK_DOWN) {
2191 if (i++ > timeout) {
2193 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
2194 "3140 Timeout waiting for link to "
2195 "diagnostic mode_end, timeout:%d ms\n",
2197 /* there is nothing much we can do here */
2203 /* reset port resource registrations */
2204 rc = lpfc_selective_reset(phba);
2205 phba->pport->fc_myDID = 0;
2207 loopback_mode_end_exit:
2208 /* make return code available to userspace */
2209 job->reply->result = rc;
2210 /* complete the job back to userspace if no error */
2217 * lpfc_sli4_bsg_link_diag_test - sli4 bsg vendor command for diag link test
2218 * @job: LPFC_BSG_VENDOR_DIAG_LINK_TEST
2220 * This function is to perform SLI4 diag link test request from the user
2224 lpfc_sli4_bsg_link_diag_test(struct fc_bsg_job *job)
2226 struct Scsi_Host *shost;
2227 struct lpfc_vport *vport;
2228 struct lpfc_hba *phba;
2229 LPFC_MBOXQ_t *pmboxq;
2230 struct sli4_link_diag *link_diag_test_cmd;
2231 uint32_t req_len, alloc_len;
2233 struct lpfc_mbx_run_link_diag_test *run_link_diag_test;
2234 union lpfc_sli4_cfg_shdr *shdr;
2235 uint32_t shdr_status, shdr_add_status;
2236 struct diag_status *diag_status_reply;
2237 int mbxstatus, rc = 0;
2244 vport = (struct lpfc_vport *)job->shost->hostdata;
2255 if (phba->sli_rev < LPFC_SLI_REV4) {
2259 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
2260 LPFC_SLI_INTF_IF_TYPE_2) {
2265 if (job->request_len < sizeof(struct fc_bsg_request) +
2266 sizeof(struct sli4_link_diag)) {
2267 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2268 "3013 Received LINK DIAG TEST request "
2269 " size:%d below the minimum size:%d\n",
2271 (int)(sizeof(struct fc_bsg_request) +
2272 sizeof(struct sli4_link_diag)));
2277 rc = lpfc_bsg_diag_mode_enter(phba);
2281 link_diag_test_cmd = (struct sli4_link_diag *)
2282 job->request->rqst_data.h_vendor.vendor_cmd;
2283 timeout = link_diag_test_cmd->timeout * 100;
2285 rc = lpfc_sli4_bsg_set_link_diag_state(phba, 1);
2290 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2293 goto link_diag_test_exit;
2296 req_len = (sizeof(struct lpfc_mbx_set_link_diag_state) -
2297 sizeof(struct lpfc_sli4_cfg_mhdr));
2298 alloc_len = lpfc_sli4_config(phba, pmboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
2299 LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_STATE,
2300 req_len, LPFC_SLI4_MBX_EMBED);
2301 if (alloc_len != req_len) {
2303 goto link_diag_test_exit;
2305 run_link_diag_test = &pmboxq->u.mqe.un.link_diag_test;
2306 bf_set(lpfc_mbx_run_diag_test_link_num, &run_link_diag_test->u.req,
2307 phba->sli4_hba.lnk_info.lnk_no);
2308 bf_set(lpfc_mbx_run_diag_test_link_type, &run_link_diag_test->u.req,
2309 phba->sli4_hba.lnk_info.lnk_tp);
2310 bf_set(lpfc_mbx_run_diag_test_test_id, &run_link_diag_test->u.req,
2311 link_diag_test_cmd->test_id);
2312 bf_set(lpfc_mbx_run_diag_test_loops, &run_link_diag_test->u.req,
2313 link_diag_test_cmd->loops);
2314 bf_set(lpfc_mbx_run_diag_test_test_ver, &run_link_diag_test->u.req,
2315 link_diag_test_cmd->test_version);
2316 bf_set(lpfc_mbx_run_diag_test_err_act, &run_link_diag_test->u.req,
2317 link_diag_test_cmd->error_action);
2319 mbxstatus = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
2321 shdr = (union lpfc_sli4_cfg_shdr *)
2322 &pmboxq->u.mqe.un.sli4_config.header.cfg_shdr;
2323 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
2324 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
2325 if (shdr_status || shdr_add_status || mbxstatus) {
2326 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
2327 "3010 Run link diag test mailbox failed with "
2328 "mbx_status x%x status x%x, add_status x%x\n",
2329 mbxstatus, shdr_status, shdr_add_status);
2332 diag_status_reply = (struct diag_status *)
2333 job->reply->reply_data.vendor_reply.vendor_rsp;
2335 if (job->reply_len <
2336 sizeof(struct fc_bsg_request) + sizeof(struct diag_status)) {
2337 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2338 "3012 Received Run link diag test reply "
2339 "below minimum size (%d): reply_len:%d\n",
2340 (int)(sizeof(struct fc_bsg_request) +
2341 sizeof(struct diag_status)),
2347 diag_status_reply->mbox_status = mbxstatus;
2348 diag_status_reply->shdr_status = shdr_status;
2349 diag_status_reply->shdr_add_status = shdr_add_status;
2351 link_diag_test_exit:
2352 rc = lpfc_sli4_bsg_set_link_diag_state(phba, 0);
2355 mempool_free(pmboxq, phba->mbox_mem_pool);
2357 lpfc_bsg_diag_mode_exit(phba);
2360 /* make error code available to userspace */
2361 job->reply->result = rc;
2362 /* complete the job back to userspace if no error */
2369 * lpfcdiag_loop_self_reg - obtains a remote port login id
2370 * @phba: Pointer to HBA context object
2371 * @rpi: Pointer to a remote port login id
2373 * This function obtains a remote port login id so the diag loopback test
2374 * can send and receive its own unsolicited CT command.
2376 static int lpfcdiag_loop_self_reg(struct lpfc_hba *phba, uint16_t *rpi)
2379 struct lpfc_dmabuf *dmabuff;
2382 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2386 if (phba->sli_rev < LPFC_SLI_REV4)
2387 status = lpfc_reg_rpi(phba, 0, phba->pport->fc_myDID,
2388 (uint8_t *)&phba->pport->fc_sparam,
2391 *rpi = lpfc_sli4_alloc_rpi(phba);
2392 status = lpfc_reg_rpi(phba, phba->pport->vpi,
2393 phba->pport->fc_myDID,
2394 (uint8_t *)&phba->pport->fc_sparam,
2399 mempool_free(mbox, phba->mbox_mem_pool);
2400 if (phba->sli_rev == LPFC_SLI_REV4)
2401 lpfc_sli4_free_rpi(phba, *rpi);
2405 dmabuff = (struct lpfc_dmabuf *) mbox->context1;
2406 mbox->context1 = NULL;
2407 mbox->context2 = NULL;
2408 status = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
2410 if ((status != MBX_SUCCESS) || (mbox->u.mb.mbxStatus)) {
2411 lpfc_mbuf_free(phba, dmabuff->virt, dmabuff->phys);
2413 if (status != MBX_TIMEOUT)
2414 mempool_free(mbox, phba->mbox_mem_pool);
2415 if (phba->sli_rev == LPFC_SLI_REV4)
2416 lpfc_sli4_free_rpi(phba, *rpi);
2420 if (phba->sli_rev < LPFC_SLI_REV4)
2421 *rpi = mbox->u.mb.un.varWords[0];
2423 lpfc_mbuf_free(phba, dmabuff->virt, dmabuff->phys);
2425 mempool_free(mbox, phba->mbox_mem_pool);
2430 * lpfcdiag_loop_self_unreg - unregs from the rpi
2431 * @phba: Pointer to HBA context object
2432 * @rpi: Remote port login id
2434 * This function unregisters the rpi obtained in lpfcdiag_loop_self_reg
2436 static int lpfcdiag_loop_self_unreg(struct lpfc_hba *phba, uint16_t rpi)
2441 /* Allocate mboxq structure */
2442 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2446 if (phba->sli_rev < LPFC_SLI_REV4)
2447 lpfc_unreg_login(phba, 0, rpi, mbox);
2449 lpfc_unreg_login(phba, phba->pport->vpi,
2450 phba->sli4_hba.rpi_ids[rpi], mbox);
2452 status = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
2454 if ((status != MBX_SUCCESS) || (mbox->u.mb.mbxStatus)) {
2455 if (status != MBX_TIMEOUT)
2456 mempool_free(mbox, phba->mbox_mem_pool);
2459 mempool_free(mbox, phba->mbox_mem_pool);
2460 if (phba->sli_rev == LPFC_SLI_REV4)
2461 lpfc_sli4_free_rpi(phba, rpi);
2466 * lpfcdiag_loop_get_xri - obtains the transmit and receive ids
2467 * @phba: Pointer to HBA context object
2468 * @rpi: Remote port login id
2469 * @txxri: Pointer to transmit exchange id
2470 * @rxxri: Pointer to response exchabge id
2472 * This function obtains the transmit and receive ids required to send
2473 * an unsolicited ct command with a payload. A special lpfc FsType and CmdRsp
2474 * flags are used to the unsolicted response handler is able to process
2475 * the ct command sent on the same port.
2477 static int lpfcdiag_loop_get_xri(struct lpfc_hba *phba, uint16_t rpi,
2478 uint16_t *txxri, uint16_t * rxxri)
2480 struct lpfc_bsg_event *evt;
2481 struct lpfc_iocbq *cmdiocbq, *rspiocbq;
2483 struct lpfc_dmabuf *dmabuf;
2484 struct ulp_bde64 *bpl = NULL;
2485 struct lpfc_sli_ct_request *ctreq = NULL;
2489 unsigned long flags;
2493 evt = lpfc_bsg_event_new(FC_REG_CT_EVENT, current->pid,
2494 SLI_CT_ELX_LOOPBACK);
2498 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2499 list_add(&evt->node, &phba->ct_ev_waiters);
2500 lpfc_bsg_event_ref(evt);
2501 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2503 cmdiocbq = lpfc_sli_get_iocbq(phba);
2504 rspiocbq = lpfc_sli_get_iocbq(phba);
2506 dmabuf = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2508 dmabuf->virt = lpfc_mbuf_alloc(phba, 0, &dmabuf->phys);
2510 INIT_LIST_HEAD(&dmabuf->list);
2511 bpl = (struct ulp_bde64 *) dmabuf->virt;
2512 memset(bpl, 0, sizeof(*bpl));
2513 ctreq = (struct lpfc_sli_ct_request *)(bpl + 1);
2515 le32_to_cpu(putPaddrHigh(dmabuf->phys +
2518 le32_to_cpu(putPaddrLow(dmabuf->phys +
2520 bpl->tus.f.bdeFlags = 0;
2521 bpl->tus.f.bdeSize = ELX_LOOPBACK_HEADER_SZ;
2522 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2526 if (cmdiocbq == NULL || rspiocbq == NULL ||
2527 dmabuf == NULL || bpl == NULL || ctreq == NULL ||
2528 dmabuf->virt == NULL) {
2530 goto err_get_xri_exit;
2533 cmd = &cmdiocbq->iocb;
2534 rsp = &rspiocbq->iocb;
2536 memset(ctreq, 0, ELX_LOOPBACK_HEADER_SZ);
2538 ctreq->RevisionId.bits.Revision = SLI_CT_REVISION;
2539 ctreq->RevisionId.bits.InId = 0;
2540 ctreq->FsType = SLI_CT_ELX_LOOPBACK;
2541 ctreq->FsSubType = 0;
2542 ctreq->CommandResponse.bits.CmdRsp = ELX_LOOPBACK_XRI_SETUP;
2543 ctreq->CommandResponse.bits.Size = 0;
2546 cmd->un.xseq64.bdl.addrHigh = putPaddrHigh(dmabuf->phys);
2547 cmd->un.xseq64.bdl.addrLow = putPaddrLow(dmabuf->phys);
2548 cmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
2549 cmd->un.xseq64.bdl.bdeSize = sizeof(*bpl);
2551 cmd->un.xseq64.w5.hcsw.Fctl = LA;
2552 cmd->un.xseq64.w5.hcsw.Dfctl = 0;
2553 cmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
2554 cmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
2556 cmd->ulpCommand = CMD_XMIT_SEQUENCE64_CR;
2557 cmd->ulpBdeCount = 1;
2559 cmd->ulpClass = CLASS3;
2560 cmd->ulpContext = rpi;
2562 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
2563 cmdiocbq->vport = phba->pport;
2565 iocb_stat = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq,
2567 (phba->fc_ratov * 2)
2568 + LPFC_DRVR_TIMEOUT);
2571 goto err_get_xri_exit;
2573 *txxri = rsp->ulpContext;
2576 evt->wait_time_stamp = jiffies;
2577 time_left = wait_event_interruptible_timeout(
2578 evt->wq, !list_empty(&evt->events_to_see),
2579 ((phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT) * HZ);
2580 if (list_empty(&evt->events_to_see))
2581 ret_val = (time_left) ? -EINTR : -ETIMEDOUT;
2583 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2584 list_move(evt->events_to_see.prev, &evt->events_to_get);
2585 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2586 *rxxri = (list_entry(evt->events_to_get.prev,
2587 typeof(struct event_data),
2593 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2594 lpfc_bsg_event_unref(evt); /* release ref */
2595 lpfc_bsg_event_unref(evt); /* delete */
2596 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2600 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
2604 if (cmdiocbq && (iocb_stat != IOCB_TIMEDOUT))
2605 lpfc_sli_release_iocbq(phba, cmdiocbq);
2607 lpfc_sli_release_iocbq(phba, rspiocbq);
2612 * lpfc_bsg_dma_page_alloc - allocate a bsg mbox page sized dma buffers
2613 * @phba: Pointer to HBA context object
2615 * This function allocates BSG_MBOX_SIZE (4KB) page size dma buffer and.
2616 * retruns the pointer to the buffer.
2618 static struct lpfc_dmabuf *
2619 lpfc_bsg_dma_page_alloc(struct lpfc_hba *phba)
2621 struct lpfc_dmabuf *dmabuf;
2622 struct pci_dev *pcidev = phba->pcidev;
2624 /* allocate dma buffer struct */
2625 dmabuf = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2629 INIT_LIST_HEAD(&dmabuf->list);
2631 /* now, allocate dma buffer */
2632 dmabuf->virt = dma_alloc_coherent(&pcidev->dev, BSG_MBOX_SIZE,
2633 &(dmabuf->phys), GFP_KERNEL);
2635 if (!dmabuf->virt) {
2639 memset((uint8_t *)dmabuf->virt, 0, BSG_MBOX_SIZE);
2645 * lpfc_bsg_dma_page_free - free a bsg mbox page sized dma buffer
2646 * @phba: Pointer to HBA context object.
2647 * @dmabuf: Pointer to the bsg mbox page sized dma buffer descriptor.
2649 * This routine just simply frees a dma buffer and its associated buffer
2650 * descriptor referred by @dmabuf.
2653 lpfc_bsg_dma_page_free(struct lpfc_hba *phba, struct lpfc_dmabuf *dmabuf)
2655 struct pci_dev *pcidev = phba->pcidev;
2661 dma_free_coherent(&pcidev->dev, BSG_MBOX_SIZE,
2662 dmabuf->virt, dmabuf->phys);
2668 * lpfc_bsg_dma_page_list_free - free a list of bsg mbox page sized dma buffers
2669 * @phba: Pointer to HBA context object.
2670 * @dmabuf_list: Pointer to a list of bsg mbox page sized dma buffer descs.
2672 * This routine just simply frees all dma buffers and their associated buffer
2673 * descriptors referred by @dmabuf_list.
2676 lpfc_bsg_dma_page_list_free(struct lpfc_hba *phba,
2677 struct list_head *dmabuf_list)
2679 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
2681 if (list_empty(dmabuf_list))
2684 list_for_each_entry_safe(dmabuf, next_dmabuf, dmabuf_list, list) {
2685 list_del_init(&dmabuf->list);
2686 lpfc_bsg_dma_page_free(phba, dmabuf);
2692 * diag_cmd_data_alloc - fills in a bde struct with dma buffers
2693 * @phba: Pointer to HBA context object
2694 * @bpl: Pointer to 64 bit bde structure
2695 * @size: Number of bytes to process
2696 * @nocopydata: Flag to copy user data into the allocated buffer
2698 * This function allocates page size buffers and populates an lpfc_dmabufext.
2699 * If allowed the user data pointed to with indataptr is copied into the kernel
2700 * memory. The chained list of page size buffers is returned.
2702 static struct lpfc_dmabufext *
2703 diag_cmd_data_alloc(struct lpfc_hba *phba,
2704 struct ulp_bde64 *bpl, uint32_t size,
2707 struct lpfc_dmabufext *mlist = NULL;
2708 struct lpfc_dmabufext *dmp;
2709 int cnt, offset = 0, i = 0;
2710 struct pci_dev *pcidev;
2712 pcidev = phba->pcidev;
2715 /* We get chunks of 4K */
2716 if (size > BUF_SZ_4K)
2721 /* allocate struct lpfc_dmabufext buffer header */
2722 dmp = kmalloc(sizeof(struct lpfc_dmabufext), GFP_KERNEL);
2726 INIT_LIST_HEAD(&dmp->dma.list);
2728 /* Queue it to a linked list */
2730 list_add_tail(&dmp->dma.list, &mlist->dma.list);
2734 /* allocate buffer */
2735 dmp->dma.virt = dma_alloc_coherent(&pcidev->dev,
2746 bpl->tus.f.bdeFlags = 0;
2747 pci_dma_sync_single_for_device(phba->pcidev,
2748 dmp->dma.phys, LPFC_BPL_SIZE, PCI_DMA_TODEVICE);
2751 memset((uint8_t *)dmp->dma.virt, 0, cnt);
2752 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2755 /* build buffer ptr list for IOCB */
2756 bpl->addrLow = le32_to_cpu(putPaddrLow(dmp->dma.phys));
2757 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dmp->dma.phys));
2758 bpl->tus.f.bdeSize = (ushort) cnt;
2759 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2770 diag_cmd_data_free(phba, mlist);
2775 * lpfcdiag_loop_post_rxbufs - post the receive buffers for an unsol CT cmd
2776 * @phba: Pointer to HBA context object
2777 * @rxxri: Receive exchange id
2778 * @len: Number of data bytes
2780 * This function allocates and posts a data buffer of sufficient size to receive
2781 * an unsolicted CT command.
2783 static int lpfcdiag_loop_post_rxbufs(struct lpfc_hba *phba, uint16_t rxxri,
2786 struct lpfc_sli *psli = &phba->sli;
2787 struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
2788 struct lpfc_iocbq *cmdiocbq;
2790 struct list_head head, *curr, *next;
2791 struct lpfc_dmabuf *rxbmp;
2792 struct lpfc_dmabuf *dmp;
2793 struct lpfc_dmabuf *mp[2] = {NULL, NULL};
2794 struct ulp_bde64 *rxbpl = NULL;
2796 struct lpfc_dmabufext *rxbuffer = NULL;
2801 cmdiocbq = lpfc_sli_get_iocbq(phba);
2802 rxbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2803 if (rxbmp != NULL) {
2804 rxbmp->virt = lpfc_mbuf_alloc(phba, 0, &rxbmp->phys);
2806 INIT_LIST_HEAD(&rxbmp->list);
2807 rxbpl = (struct ulp_bde64 *) rxbmp->virt;
2808 rxbuffer = diag_cmd_data_alloc(phba, rxbpl, len, 0);
2812 if (!cmdiocbq || !rxbmp || !rxbpl || !rxbuffer) {
2814 goto err_post_rxbufs_exit;
2817 /* Queue buffers for the receive exchange */
2818 num_bde = (uint32_t)rxbuffer->flag;
2819 dmp = &rxbuffer->dma;
2821 cmd = &cmdiocbq->iocb;
2824 INIT_LIST_HEAD(&head);
2825 list_add_tail(&head, &dmp->list);
2826 list_for_each_safe(curr, next, &head) {
2827 mp[i] = list_entry(curr, struct lpfc_dmabuf, list);
2830 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
2831 mp[i]->buffer_tag = lpfc_sli_get_buffer_tag(phba);
2832 cmd->un.quexri64cx.buff.bde.addrHigh =
2833 putPaddrHigh(mp[i]->phys);
2834 cmd->un.quexri64cx.buff.bde.addrLow =
2835 putPaddrLow(mp[i]->phys);
2836 cmd->un.quexri64cx.buff.bde.tus.f.bdeSize =
2837 ((struct lpfc_dmabufext *)mp[i])->size;
2838 cmd->un.quexri64cx.buff.buffer_tag = mp[i]->buffer_tag;
2839 cmd->ulpCommand = CMD_QUE_XRI64_CX;
2842 cmd->ulpBdeCount = 1;
2843 cmd->unsli3.que_xri64cx_ext_words.ebde_count = 0;
2846 cmd->un.cont64[i].addrHigh = putPaddrHigh(mp[i]->phys);
2847 cmd->un.cont64[i].addrLow = putPaddrLow(mp[i]->phys);
2848 cmd->un.cont64[i].tus.f.bdeSize =
2849 ((struct lpfc_dmabufext *)mp[i])->size;
2850 cmd->ulpBdeCount = ++i;
2852 if ((--num_bde > 0) && (i < 2))
2855 cmd->ulpCommand = CMD_QUE_XRI_BUF64_CX;
2859 cmd->ulpClass = CLASS3;
2860 cmd->ulpContext = rxxri;
2862 iocb_stat = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq,
2864 if (iocb_stat == IOCB_ERROR) {
2865 diag_cmd_data_free(phba,
2866 (struct lpfc_dmabufext *)mp[0]);
2868 diag_cmd_data_free(phba,
2869 (struct lpfc_dmabufext *)mp[1]);
2870 dmp = list_entry(next, struct lpfc_dmabuf, list);
2872 goto err_post_rxbufs_exit;
2875 lpfc_sli_ringpostbuf_put(phba, pring, mp[0]);
2877 lpfc_sli_ringpostbuf_put(phba, pring, mp[1]);
2881 /* The iocb was freed by lpfc_sli_issue_iocb */
2882 cmdiocbq = lpfc_sli_get_iocbq(phba);
2884 dmp = list_entry(next, struct lpfc_dmabuf, list);
2886 goto err_post_rxbufs_exit;
2889 cmd = &cmdiocbq->iocb;
2894 err_post_rxbufs_exit:
2898 lpfc_mbuf_free(phba, rxbmp->virt, rxbmp->phys);
2903 lpfc_sli_release_iocbq(phba, cmdiocbq);
2908 * lpfc_bsg_diag_loopback_run - run loopback on a port by issue ct cmd to itself
2909 * @job: LPFC_BSG_VENDOR_DIAG_TEST fc_bsg_job
2911 * This function receives a user data buffer to be transmitted and received on
2912 * the same port, the link must be up and in loopback mode prior
2914 * 1. A kernel buffer is allocated to copy the user data into.
2915 * 2. The port registers with "itself".
2916 * 3. The transmit and receive exchange ids are obtained.
2917 * 4. The receive exchange id is posted.
2918 * 5. A new els loopback event is created.
2919 * 6. The command and response iocbs are allocated.
2920 * 7. The cmd iocb FsType is set to elx loopback and the CmdRsp to looppback.
2922 * This function is meant to be called n times while the port is in loopback
2923 * so it is the apps responsibility to issue a reset to take the port out
2927 lpfc_bsg_diag_loopback_run(struct fc_bsg_job *job)
2929 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
2930 struct lpfc_hba *phba = vport->phba;
2931 struct diag_mode_test *diag_mode;
2932 struct lpfc_bsg_event *evt;
2933 struct event_data *evdat;
2934 struct lpfc_sli *psli = &phba->sli;
2937 size_t segment_len = 0, segment_offset = 0, current_offset = 0;
2939 struct lpfc_iocbq *cmdiocbq, *rspiocbq = NULL;
2940 IOCB_t *cmd, *rsp = NULL;
2941 struct lpfc_sli_ct_request *ctreq;
2942 struct lpfc_dmabuf *txbmp;
2943 struct ulp_bde64 *txbpl = NULL;
2944 struct lpfc_dmabufext *txbuffer = NULL;
2945 struct list_head head;
2946 struct lpfc_dmabuf *curr;
2947 uint16_t txxri = 0, rxxri;
2949 uint8_t *ptr = NULL, *rx_databuf = NULL;
2953 unsigned long flags;
2954 void *dataout = NULL;
2957 /* in case no data is returned return just the return code */
2958 job->reply->reply_payload_rcv_len = 0;
2960 if (job->request_len <
2961 sizeof(struct fc_bsg_request) + sizeof(struct diag_mode_test)) {
2962 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2963 "2739 Received DIAG TEST request below minimum "
2966 goto loopback_test_exit;
2969 if (job->request_payload.payload_len !=
2970 job->reply_payload.payload_len) {
2972 goto loopback_test_exit;
2974 diag_mode = (struct diag_mode_test *)
2975 job->request->rqst_data.h_vendor.vendor_cmd;
2977 if ((phba->link_state == LPFC_HBA_ERROR) ||
2978 (psli->sli_flag & LPFC_BLOCK_MGMT_IO) ||
2979 (!(psli->sli_flag & LPFC_SLI_ACTIVE))) {
2981 goto loopback_test_exit;
2984 if (!lpfc_is_link_up(phba) || !(phba->link_flag & LS_LOOPBACK_MODE)) {
2986 goto loopback_test_exit;
2989 size = job->request_payload.payload_len;
2990 full_size = size + ELX_LOOPBACK_HEADER_SZ; /* plus the header */
2992 if ((size == 0) || (size > 80 * BUF_SZ_4K)) {
2994 goto loopback_test_exit;
2997 if (full_size >= BUF_SZ_4K) {
2999 * Allocate memory for ioctl data. If buffer is bigger than 64k,
3000 * then we allocate 64k and re-use that buffer over and over to
3001 * xfer the whole block. This is because Linux kernel has a
3002 * problem allocating more than 120k of kernel space memory. Saw
3003 * problem with GET_FCPTARGETMAPPING...
3005 if (size <= (64 * 1024))
3006 total_mem = full_size;
3008 total_mem = 64 * 1024;
3010 /* Allocate memory for ioctl data */
3011 total_mem = BUF_SZ_4K;
3013 dataout = kmalloc(total_mem, GFP_KERNEL);
3014 if (dataout == NULL) {
3016 goto loopback_test_exit;
3020 ptr += ELX_LOOPBACK_HEADER_SZ;
3021 sg_copy_to_buffer(job->request_payload.sg_list,
3022 job->request_payload.sg_cnt,
3024 rc = lpfcdiag_loop_self_reg(phba, &rpi);
3026 goto loopback_test_exit;
3028 if (phba->sli_rev < LPFC_SLI_REV4) {
3029 rc = lpfcdiag_loop_get_xri(phba, rpi, &txxri, &rxxri);
3031 lpfcdiag_loop_self_unreg(phba, rpi);
3032 goto loopback_test_exit;
3035 rc = lpfcdiag_loop_post_rxbufs(phba, rxxri, full_size);
3037 lpfcdiag_loop_self_unreg(phba, rpi);
3038 goto loopback_test_exit;
3041 evt = lpfc_bsg_event_new(FC_REG_CT_EVENT, current->pid,
3042 SLI_CT_ELX_LOOPBACK);
3044 lpfcdiag_loop_self_unreg(phba, rpi);
3046 goto loopback_test_exit;
3049 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3050 list_add(&evt->node, &phba->ct_ev_waiters);
3051 lpfc_bsg_event_ref(evt);
3052 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3054 cmdiocbq = lpfc_sli_get_iocbq(phba);
3055 if (phba->sli_rev < LPFC_SLI_REV4)
3056 rspiocbq = lpfc_sli_get_iocbq(phba);
3057 txbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3060 txbmp->virt = lpfc_mbuf_alloc(phba, 0, &txbmp->phys);
3062 INIT_LIST_HEAD(&txbmp->list);
3063 txbpl = (struct ulp_bde64 *) txbmp->virt;
3064 txbuffer = diag_cmd_data_alloc(phba,
3065 txbpl, full_size, 0);
3069 if (!cmdiocbq || !txbmp || !txbpl || !txbuffer || !txbmp->virt) {
3071 goto err_loopback_test_exit;
3073 if ((phba->sli_rev < LPFC_SLI_REV4) && !rspiocbq) {
3075 goto err_loopback_test_exit;
3078 cmd = &cmdiocbq->iocb;
3079 if (phba->sli_rev < LPFC_SLI_REV4)
3080 rsp = &rspiocbq->iocb;
3082 INIT_LIST_HEAD(&head);
3083 list_add_tail(&head, &txbuffer->dma.list);
3084 list_for_each_entry(curr, &head, list) {
3085 segment_len = ((struct lpfc_dmabufext *)curr)->size;
3086 if (current_offset == 0) {
3088 memset(ctreq, 0, ELX_LOOPBACK_HEADER_SZ);
3089 ctreq->RevisionId.bits.Revision = SLI_CT_REVISION;
3090 ctreq->RevisionId.bits.InId = 0;
3091 ctreq->FsType = SLI_CT_ELX_LOOPBACK;
3092 ctreq->FsSubType = 0;
3093 ctreq->CommandResponse.bits.CmdRsp = ELX_LOOPBACK_DATA;
3094 ctreq->CommandResponse.bits.Size = size;
3095 segment_offset = ELX_LOOPBACK_HEADER_SZ;
3099 BUG_ON(segment_offset >= segment_len);
3100 memcpy(curr->virt + segment_offset,
3101 ptr + current_offset,
3102 segment_len - segment_offset);
3104 current_offset += segment_len - segment_offset;
3105 BUG_ON(current_offset > size);
3109 /* Build the XMIT_SEQUENCE iocb */
3110 num_bde = (uint32_t)txbuffer->flag;
3112 cmd->un.xseq64.bdl.addrHigh = putPaddrHigh(txbmp->phys);
3113 cmd->un.xseq64.bdl.addrLow = putPaddrLow(txbmp->phys);
3114 cmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
3115 cmd->un.xseq64.bdl.bdeSize = (num_bde * sizeof(struct ulp_bde64));
3117 cmd->un.xseq64.w5.hcsw.Fctl = (LS | LA);
3118 cmd->un.xseq64.w5.hcsw.Dfctl = 0;
3119 cmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
3120 cmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
3122 cmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;
3123 cmd->ulpBdeCount = 1;
3125 cmd->ulpClass = CLASS3;
3127 if (phba->sli_rev < LPFC_SLI_REV4) {
3128 cmd->ulpContext = txxri;
3130 cmd->un.xseq64.bdl.ulpIoTag32 = 0;
3131 cmd->un.ulpWord[3] = phba->sli4_hba.rpi_ids[rpi];
3132 cmdiocbq->context3 = txbmp;
3133 cmdiocbq->sli4_xritag = NO_XRI;
3134 cmd->unsli3.rcvsli3.ox_id = 0xffff;
3136 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
3137 cmdiocbq->vport = phba->pport;
3138 iocb_stat = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq,
3139 rspiocbq, (phba->fc_ratov * 2) +
3142 if ((iocb_stat != IOCB_SUCCESS) || ((phba->sli_rev < LPFC_SLI_REV4) &&
3143 (rsp->ulpStatus != IOCB_SUCCESS))) {
3144 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3145 "3126 Failed loopback test issue iocb: "
3146 "iocb_stat:x%x\n", iocb_stat);
3148 goto err_loopback_test_exit;
3152 time_left = wait_event_interruptible_timeout(
3153 evt->wq, !list_empty(&evt->events_to_see),
3154 ((phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT) * HZ);
3156 if (list_empty(&evt->events_to_see)) {
3157 rc = (time_left) ? -EINTR : -ETIMEDOUT;
3158 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3159 "3125 Not receiving unsolicited event, "
3162 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3163 list_move(evt->events_to_see.prev, &evt->events_to_get);
3164 evdat = list_entry(evt->events_to_get.prev,
3165 typeof(*evdat), node);
3166 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3167 rx_databuf = evdat->data;
3168 if (evdat->len != full_size) {
3169 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3170 "1603 Loopback test did not receive expected "
3171 "data length. actual length 0x%x expected "
3173 evdat->len, full_size);
3175 } else if (rx_databuf == NULL)
3179 /* skip over elx loopback header */
3180 rx_databuf += ELX_LOOPBACK_HEADER_SZ;
3181 job->reply->reply_payload_rcv_len =
3182 sg_copy_from_buffer(job->reply_payload.sg_list,
3183 job->reply_payload.sg_cnt,
3185 job->reply->reply_payload_rcv_len = size;
3189 err_loopback_test_exit:
3190 lpfcdiag_loop_self_unreg(phba, rpi);
3192 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3193 lpfc_bsg_event_unref(evt); /* release ref */
3194 lpfc_bsg_event_unref(evt); /* delete */
3195 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3197 if (cmdiocbq != NULL)
3198 lpfc_sli_release_iocbq(phba, cmdiocbq);
3200 if (rspiocbq != NULL)
3201 lpfc_sli_release_iocbq(phba, rspiocbq);
3203 if (txbmp != NULL) {
3204 if (txbpl != NULL) {
3205 if (txbuffer != NULL)
3206 diag_cmd_data_free(phba, txbuffer);
3207 lpfc_mbuf_free(phba, txbmp->virt, txbmp->phys);
3214 /* make error code available to userspace */
3215 job->reply->result = rc;
3216 job->dd_data = NULL;
3217 /* complete the job back to userspace if no error */
3218 if (rc == IOCB_SUCCESS)
3224 * lpfc_bsg_get_dfc_rev - process a GET_DFC_REV bsg vendor command
3225 * @job: GET_DFC_REV fc_bsg_job
3228 lpfc_bsg_get_dfc_rev(struct fc_bsg_job *job)
3230 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
3231 struct lpfc_hba *phba = vport->phba;
3232 struct get_mgmt_rev *event_req;
3233 struct get_mgmt_rev_reply *event_reply;
3236 if (job->request_len <
3237 sizeof(struct fc_bsg_request) + sizeof(struct get_mgmt_rev)) {
3238 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3239 "2740 Received GET_DFC_REV request below "
3245 event_req = (struct get_mgmt_rev *)
3246 job->request->rqst_data.h_vendor.vendor_cmd;
3248 event_reply = (struct get_mgmt_rev_reply *)
3249 job->reply->reply_data.vendor_reply.vendor_rsp;
3251 if (job->reply_len <
3252 sizeof(struct fc_bsg_request) + sizeof(struct get_mgmt_rev_reply)) {
3253 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3254 "2741 Received GET_DFC_REV reply below "
3260 event_reply->info.a_Major = MANAGEMENT_MAJOR_REV;
3261 event_reply->info.a_Minor = MANAGEMENT_MINOR_REV;
3263 job->reply->result = rc;
3270 * lpfc_bsg_issue_mbox_cmpl - lpfc_bsg_issue_mbox mbox completion handler
3271 * @phba: Pointer to HBA context object.
3272 * @pmboxq: Pointer to mailbox command.
3274 * This is completion handler function for mailbox commands issued from
3275 * lpfc_bsg_issue_mbox function. This function is called by the
3276 * mailbox event handler function with no lock held. This function
3277 * will wake up thread waiting on the wait queue pointed by context1
3281 lpfc_bsg_issue_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
3283 struct bsg_job_data *dd_data;
3284 struct fc_bsg_job *job;
3286 unsigned long flags;
3287 uint8_t *pmb, *pmb_buf;
3289 dd_data = pmboxq->context1;
3292 * The outgoing buffer is readily referred from the dma buffer,
3293 * just need to get header part from mailboxq structure.
3295 pmb = (uint8_t *)&pmboxq->u.mb;
3296 pmb_buf = (uint8_t *)dd_data->context_un.mbox.mb;
3297 memcpy(pmb_buf, pmb, sizeof(MAILBOX_t));
3299 /* Determine if job has been aborted */
3301 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3302 job = dd_data->set_job;
3304 /* Prevent timeout handling from trying to abort job */
3305 job->dd_data = NULL;
3307 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3309 /* Copy the mailbox data to the job if it is still active */
3312 size = job->reply_payload.payload_len;
3313 job->reply->reply_payload_rcv_len =
3314 sg_copy_from_buffer(job->reply_payload.sg_list,
3315 job->reply_payload.sg_cnt,
3319 dd_data->set_job = NULL;
3320 mempool_free(dd_data->context_un.mbox.pmboxq, phba->mbox_mem_pool);
3321 lpfc_bsg_dma_page_free(phba, dd_data->context_un.mbox.dmabuffers);
3324 /* Complete the job if the job is still active */
3327 job->reply->result = 0;
3334 * lpfc_bsg_check_cmd_access - test for a supported mailbox command
3335 * @phba: Pointer to HBA context object.
3336 * @mb: Pointer to a mailbox object.
3337 * @vport: Pointer to a vport object.
3339 * Some commands require the port to be offline, some may not be called from
3342 static int lpfc_bsg_check_cmd_access(struct lpfc_hba *phba,
3343 MAILBOX_t *mb, struct lpfc_vport *vport)
3345 /* return negative error values for bsg job */
3346 switch (mb->mbxCommand) {
3350 case MBX_CONFIG_LINK:
3351 case MBX_CONFIG_RING:
3352 case MBX_RESET_RING:
3353 case MBX_UNREG_LOGIN:
3355 case MBX_DUMP_CONTEXT:
3359 if (!(vport->fc_flag & FC_OFFLINE_MODE)) {
3360 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3361 "2743 Command 0x%x is illegal in on-line "
3367 case MBX_WRITE_VPARMS:
3370 case MBX_READ_CONFIG:
3371 case MBX_READ_RCONFIG:
3372 case MBX_READ_STATUS:
3375 case MBX_READ_LNK_STAT:
3376 case MBX_DUMP_MEMORY:
3378 case MBX_UPDATE_CFG:
3379 case MBX_KILL_BOARD:
3381 case MBX_LOAD_EXP_ROM:
3383 case MBX_DEL_LD_ENTRY:
3386 case MBX_SLI4_CONFIG:
3387 case MBX_READ_EVENT_LOG:
3388 case MBX_READ_EVENT_LOG_STATUS:
3389 case MBX_WRITE_EVENT_LOG:
3390 case MBX_PORT_CAPABILITIES:
3391 case MBX_PORT_IOV_CONTROL:
3392 case MBX_RUN_BIU_DIAG64:
3394 case MBX_SET_VARIABLE:
3395 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3396 "1226 mbox: set_variable 0x%x, 0x%x\n",
3398 mb->un.varWords[1]);
3399 if ((mb->un.varWords[0] == SETVAR_MLOMNT)
3400 && (mb->un.varWords[1] == 1)) {
3401 phba->wait_4_mlo_maint_flg = 1;
3402 } else if (mb->un.varWords[0] == SETVAR_MLORST) {
3403 spin_lock_irq(&phba->hbalock);
3404 phba->link_flag &= ~LS_LOOPBACK_MODE;
3405 spin_unlock_irq(&phba->hbalock);
3406 phba->fc_topology = LPFC_TOPOLOGY_PT_PT;
3409 case MBX_READ_SPARM64:
3410 case MBX_READ_TOPOLOGY:
3412 case MBX_REG_LOGIN64:
3413 case MBX_CONFIG_PORT:
3414 case MBX_RUN_BIU_DIAG:
3416 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3417 "2742 Unknown Command 0x%x\n",
3426 * lpfc_bsg_mbox_ext_cleanup - clean up context of multi-buffer mbox session
3427 * @phba: Pointer to HBA context object.
3429 * This is routine clean up and reset BSG handling of multi-buffer mbox
3433 lpfc_bsg_mbox_ext_session_reset(struct lpfc_hba *phba)
3435 if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_IDLE)
3438 /* free all memory, including dma buffers */
3439 lpfc_bsg_dma_page_list_free(phba,
3440 &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
3441 lpfc_bsg_dma_page_free(phba, phba->mbox_ext_buf_ctx.mbx_dmabuf);
3442 /* multi-buffer write mailbox command pass-through complete */
3443 memset((char *)&phba->mbox_ext_buf_ctx, 0,
3444 sizeof(struct lpfc_mbox_ext_buf_ctx));
3445 INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
3451 * lpfc_bsg_issue_mbox_ext_handle_job - job handler for multi-buffer mbox cmpl
3452 * @phba: Pointer to HBA context object.
3453 * @pmboxq: Pointer to mailbox command.
3455 * This is routine handles BSG job for mailbox commands completions with
3456 * multiple external buffers.
3458 static struct fc_bsg_job *
3459 lpfc_bsg_issue_mbox_ext_handle_job(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
3461 struct bsg_job_data *dd_data;
3462 struct fc_bsg_job *job;
3463 uint8_t *pmb, *pmb_buf;
3464 unsigned long flags;
3467 struct lpfc_dmabuf *dmabuf;
3468 struct lpfc_sli_config_mbox *sli_cfg_mbx;
3471 dd_data = pmboxq->context1;
3473 /* Determine if job has been aborted */
3474 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3475 job = dd_data->set_job;
3477 /* Prevent timeout handling from trying to abort job */
3478 job->dd_data = NULL;
3480 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3483 * The outgoing buffer is readily referred from the dma buffer,
3484 * just need to get header part from mailboxq structure.
3487 pmb = (uint8_t *)&pmboxq->u.mb;
3488 pmb_buf = (uint8_t *)dd_data->context_un.mbox.mb;
3489 /* Copy the byte swapped response mailbox back to the user */
3490 memcpy(pmb_buf, pmb, sizeof(MAILBOX_t));
3491 /* if there is any non-embedded extended data copy that too */
3492 dmabuf = phba->mbox_ext_buf_ctx.mbx_dmabuf;
3493 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
3494 if (!bsg_bf_get(lpfc_mbox_hdr_emb,
3495 &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) {
3496 pmbx = (uint8_t *)dmabuf->virt;
3497 /* byte swap the extended data following the mailbox command */
3498 lpfc_sli_pcimem_bcopy(&pmbx[sizeof(MAILBOX_t)],
3499 &pmbx[sizeof(MAILBOX_t)],
3500 sli_cfg_mbx->un.sli_config_emb0_subsys.mse[0].buf_len);
3503 /* Complete the job if the job is still active */
3506 size = job->reply_payload.payload_len;
3507 job->reply->reply_payload_rcv_len =
3508 sg_copy_from_buffer(job->reply_payload.sg_list,
3509 job->reply_payload.sg_cnt,
3512 /* result for successful */
3513 job->reply->result = 0;
3515 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3516 "2937 SLI_CONFIG ext-buffer maibox command "
3517 "(x%x/x%x) complete bsg job done, bsize:%d\n",
3518 phba->mbox_ext_buf_ctx.nembType,
3519 phba->mbox_ext_buf_ctx.mboxType, size);
3520 lpfc_idiag_mbxacc_dump_bsg_mbox(phba,
3521 phba->mbox_ext_buf_ctx.nembType,
3522 phba->mbox_ext_buf_ctx.mboxType,
3523 dma_ebuf, sta_pos_addr,
3524 phba->mbox_ext_buf_ctx.mbx_dmabuf, 0);
3526 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3527 "2938 SLI_CONFIG ext-buffer maibox "
3528 "command (x%x/x%x) failure, rc:x%x\n",
3529 phba->mbox_ext_buf_ctx.nembType,
3530 phba->mbox_ext_buf_ctx.mboxType, rc);
3535 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_DONE;
3541 * lpfc_bsg_issue_read_mbox_ext_cmpl - compl handler for multi-buffer read mbox
3542 * @phba: Pointer to HBA context object.
3543 * @pmboxq: Pointer to mailbox command.
3545 * This is completion handler function for mailbox read commands with multiple
3549 lpfc_bsg_issue_read_mbox_ext_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
3551 struct fc_bsg_job *job;
3553 job = lpfc_bsg_issue_mbox_ext_handle_job(phba, pmboxq);
3555 /* handle the BSG job with mailbox command */
3557 pmboxq->u.mb.mbxStatus = MBXERR_ERROR;
3559 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3560 "2939 SLI_CONFIG ext-buffer rd maibox command "
3561 "complete, ctxState:x%x, mbxStatus:x%x\n",
3562 phba->mbox_ext_buf_ctx.state, pmboxq->u.mb.mbxStatus);
3564 if (pmboxq->u.mb.mbxStatus || phba->mbox_ext_buf_ctx.numBuf == 1)
3565 lpfc_bsg_mbox_ext_session_reset(phba);
3567 /* free base driver mailbox structure memory */
3568 mempool_free(pmboxq, phba->mbox_mem_pool);
3570 /* if the job is still active, call job done */
3578 * lpfc_bsg_issue_write_mbox_ext_cmpl - cmpl handler for multi-buffer write mbox
3579 * @phba: Pointer to HBA context object.
3580 * @pmboxq: Pointer to mailbox command.
3582 * This is completion handler function for mailbox write commands with multiple
3586 lpfc_bsg_issue_write_mbox_ext_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
3588 struct fc_bsg_job *job;
3590 job = lpfc_bsg_issue_mbox_ext_handle_job(phba, pmboxq);
3592 /* handle the BSG job with the mailbox command */
3594 pmboxq->u.mb.mbxStatus = MBXERR_ERROR;
3596 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3597 "2940 SLI_CONFIG ext-buffer wr maibox command "
3598 "complete, ctxState:x%x, mbxStatus:x%x\n",
3599 phba->mbox_ext_buf_ctx.state, pmboxq->u.mb.mbxStatus);
3601 /* free all memory, including dma buffers */
3602 mempool_free(pmboxq, phba->mbox_mem_pool);
3603 lpfc_bsg_mbox_ext_session_reset(phba);
3605 /* if the job is still active, call job done */
3613 lpfc_bsg_sli_cfg_dma_desc_setup(struct lpfc_hba *phba, enum nemb_type nemb_tp,
3614 uint32_t index, struct lpfc_dmabuf *mbx_dmabuf,
3615 struct lpfc_dmabuf *ext_dmabuf)
3617 struct lpfc_sli_config_mbox *sli_cfg_mbx;
3619 /* pointer to the start of mailbox command */
3620 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)mbx_dmabuf->virt;
3622 if (nemb_tp == nemb_mse) {
3624 sli_cfg_mbx->un.sli_config_emb0_subsys.
3626 putPaddrHigh(mbx_dmabuf->phys +
3628 sli_cfg_mbx->un.sli_config_emb0_subsys.
3630 putPaddrLow(mbx_dmabuf->phys +
3632 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3633 "2943 SLI_CONFIG(mse)[%d], "
3634 "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3636 sli_cfg_mbx->un.sli_config_emb0_subsys.
3638 sli_cfg_mbx->un.sli_config_emb0_subsys.
3640 sli_cfg_mbx->un.sli_config_emb0_subsys.
3643 sli_cfg_mbx->un.sli_config_emb0_subsys.
3645 putPaddrHigh(ext_dmabuf->phys);
3646 sli_cfg_mbx->un.sli_config_emb0_subsys.
3648 putPaddrLow(ext_dmabuf->phys);
3649 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3650 "2944 SLI_CONFIG(mse)[%d], "
3651 "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3653 sli_cfg_mbx->un.sli_config_emb0_subsys.
3655 sli_cfg_mbx->un.sli_config_emb0_subsys.
3657 sli_cfg_mbx->un.sli_config_emb0_subsys.
3662 sli_cfg_mbx->un.sli_config_emb1_subsys.
3664 putPaddrHigh(mbx_dmabuf->phys +
3666 sli_cfg_mbx->un.sli_config_emb1_subsys.
3668 putPaddrLow(mbx_dmabuf->phys +
3670 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3671 "3007 SLI_CONFIG(hbd)[%d], "
3672 "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3674 bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
3676 sli_config_emb1_subsys.hbd[index]),
3677 sli_cfg_mbx->un.sli_config_emb1_subsys.
3679 sli_cfg_mbx->un.sli_config_emb1_subsys.
3683 sli_cfg_mbx->un.sli_config_emb1_subsys.
3685 putPaddrHigh(ext_dmabuf->phys);
3686 sli_cfg_mbx->un.sli_config_emb1_subsys.
3688 putPaddrLow(ext_dmabuf->phys);
3689 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3690 "3008 SLI_CONFIG(hbd)[%d], "
3691 "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3693 bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
3695 sli_config_emb1_subsys.hbd[index]),
3696 sli_cfg_mbx->un.sli_config_emb1_subsys.
3698 sli_cfg_mbx->un.sli_config_emb1_subsys.
3706 * lpfc_bsg_sli_cfg_mse_read_cmd_ext - sli_config non-embedded mailbox cmd read
3707 * @phba: Pointer to HBA context object.
3708 * @mb: Pointer to a BSG mailbox object.
3709 * @nemb_tp: Enumerate of non-embedded mailbox command type.
3710 * @dmabuff: Pointer to a DMA buffer descriptor.
3712 * This routine performs SLI_CONFIG (0x9B) read mailbox command operation with
3713 * non-embedded external bufffers.
3716 lpfc_bsg_sli_cfg_read_cmd_ext(struct lpfc_hba *phba, struct fc_bsg_job *job,
3717 enum nemb_type nemb_tp,
3718 struct lpfc_dmabuf *dmabuf)
3720 struct lpfc_sli_config_mbox *sli_cfg_mbx;
3721 struct dfc_mbox_req *mbox_req;
3722 struct lpfc_dmabuf *curr_dmabuf, *next_dmabuf;
3723 uint32_t ext_buf_cnt, ext_buf_index;
3724 struct lpfc_dmabuf *ext_dmabuf = NULL;
3725 struct bsg_job_data *dd_data = NULL;
3726 LPFC_MBOXQ_t *pmboxq = NULL;
3732 (struct dfc_mbox_req *)job->request->rqst_data.h_vendor.vendor_cmd;
3734 /* pointer to the start of mailbox command */
3735 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
3737 if (nemb_tp == nemb_mse) {
3738 ext_buf_cnt = bsg_bf_get(lpfc_mbox_hdr_mse_cnt,
3739 &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr);
3740 if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_MSE) {
3741 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3742 "2945 Handled SLI_CONFIG(mse) rd, "
3743 "ext_buf_cnt(%d) out of range(%d)\n",
3745 LPFC_MBX_SLI_CONFIG_MAX_MSE);
3749 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3750 "2941 Handled SLI_CONFIG(mse) rd, "
3751 "ext_buf_cnt:%d\n", ext_buf_cnt);
3753 /* sanity check on interface type for support */
3754 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
3755 LPFC_SLI_INTF_IF_TYPE_2) {
3759 /* nemb_tp == nemb_hbd */
3760 ext_buf_cnt = sli_cfg_mbx->un.sli_config_emb1_subsys.hbd_count;
3761 if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_HBD) {
3762 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3763 "2946 Handled SLI_CONFIG(hbd) rd, "
3764 "ext_buf_cnt(%d) out of range(%d)\n",
3766 LPFC_MBX_SLI_CONFIG_MAX_HBD);
3770 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3771 "2942 Handled SLI_CONFIG(hbd) rd, "
3772 "ext_buf_cnt:%d\n", ext_buf_cnt);
3775 /* before dma descriptor setup */
3776 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_rd, dma_mbox,
3777 sta_pre_addr, dmabuf, ext_buf_cnt);
3779 /* reject non-embedded mailbox command with none external buffer */
3780 if (ext_buf_cnt == 0) {
3783 } else if (ext_buf_cnt > 1) {
3784 /* additional external read buffers */
3785 for (i = 1; i < ext_buf_cnt; i++) {
3786 ext_dmabuf = lpfc_bsg_dma_page_alloc(phba);
3791 list_add_tail(&ext_dmabuf->list,
3792 &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
3796 /* bsg tracking structure */
3797 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
3803 /* mailbox command structure for base driver */
3804 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3809 memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
3811 /* for the first external buffer */
3812 lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, 0, dmabuf, dmabuf);
3814 /* for the rest of external buffer descriptors if any */
3815 if (ext_buf_cnt > 1) {
3817 list_for_each_entry_safe(curr_dmabuf, next_dmabuf,
3818 &phba->mbox_ext_buf_ctx.ext_dmabuf_list, list) {
3819 lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp,
3820 ext_buf_index, dmabuf,
3826 /* after dma descriptor setup */
3827 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_rd, dma_mbox,
3828 sta_pos_addr, dmabuf, ext_buf_cnt);
3830 /* construct base driver mbox command */
3831 pmb = &pmboxq->u.mb;
3832 pmbx = (uint8_t *)dmabuf->virt;
3833 memcpy(pmb, pmbx, sizeof(*pmb));
3834 pmb->mbxOwner = OWN_HOST;
3835 pmboxq->vport = phba->pport;
3837 /* multi-buffer handling context */
3838 phba->mbox_ext_buf_ctx.nembType = nemb_tp;
3839 phba->mbox_ext_buf_ctx.mboxType = mbox_rd;
3840 phba->mbox_ext_buf_ctx.numBuf = ext_buf_cnt;
3841 phba->mbox_ext_buf_ctx.mbxTag = mbox_req->extMboxTag;
3842 phba->mbox_ext_buf_ctx.seqNum = mbox_req->extSeqNum;
3843 phba->mbox_ext_buf_ctx.mbx_dmabuf = dmabuf;
3845 /* callback for multi-buffer read mailbox command */
3846 pmboxq->mbox_cmpl = lpfc_bsg_issue_read_mbox_ext_cmpl;
3848 /* context fields to callback function */
3849 pmboxq->context1 = dd_data;
3850 dd_data->type = TYPE_MBOX;
3851 dd_data->set_job = job;
3852 dd_data->context_un.mbox.pmboxq = pmboxq;
3853 dd_data->context_un.mbox.mb = (MAILBOX_t *)pmbx;
3854 job->dd_data = dd_data;
3857 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
3860 * Non-embedded mailbox subcommand data gets byte swapped here because
3861 * the lower level driver code only does the first 64 mailbox words.
3863 if ((!bsg_bf_get(lpfc_mbox_hdr_emb,
3864 &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) &&
3865 (nemb_tp == nemb_mse))
3866 lpfc_sli_pcimem_bcopy(&pmbx[sizeof(MAILBOX_t)],
3867 &pmbx[sizeof(MAILBOX_t)],
3868 sli_cfg_mbx->un.sli_config_emb0_subsys.
3871 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
3872 if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
3873 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3874 "2947 Issued SLI_CONFIG ext-buffer "
3875 "maibox command, rc:x%x\n", rc);
3876 return SLI_CONFIG_HANDLED;
3878 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3879 "2948 Failed to issue SLI_CONFIG ext-buffer "
3880 "maibox command, rc:x%x\n", rc);
3885 mempool_free(pmboxq, phba->mbox_mem_pool);
3886 lpfc_bsg_dma_page_list_free(phba,
3887 &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
3889 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_IDLE;
3894 * lpfc_bsg_sli_cfg_write_cmd_ext - sli_config non-embedded mailbox cmd write
3895 * @phba: Pointer to HBA context object.
3896 * @mb: Pointer to a BSG mailbox object.
3897 * @dmabuff: Pointer to a DMA buffer descriptor.
3899 * This routine performs SLI_CONFIG (0x9B) write mailbox command operation with
3900 * non-embedded external bufffers.
3903 lpfc_bsg_sli_cfg_write_cmd_ext(struct lpfc_hba *phba, struct fc_bsg_job *job,
3904 enum nemb_type nemb_tp,
3905 struct lpfc_dmabuf *dmabuf)
3907 struct dfc_mbox_req *mbox_req;
3908 struct lpfc_sli_config_mbox *sli_cfg_mbx;
3909 uint32_t ext_buf_cnt;
3910 struct bsg_job_data *dd_data = NULL;
3911 LPFC_MBOXQ_t *pmboxq = NULL;
3914 int rc = SLI_CONFIG_NOT_HANDLED, i;
3917 (struct dfc_mbox_req *)job->request->rqst_data.h_vendor.vendor_cmd;
3919 /* pointer to the start of mailbox command */
3920 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
3922 if (nemb_tp == nemb_mse) {
3923 ext_buf_cnt = bsg_bf_get(lpfc_mbox_hdr_mse_cnt,
3924 &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr);
3925 if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_MSE) {
3926 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3927 "2953 Failed SLI_CONFIG(mse) wr, "
3928 "ext_buf_cnt(%d) out of range(%d)\n",
3930 LPFC_MBX_SLI_CONFIG_MAX_MSE);
3933 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3934 "2949 Handled SLI_CONFIG(mse) wr, "
3935 "ext_buf_cnt:%d\n", ext_buf_cnt);
3937 /* sanity check on interface type for support */
3938 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
3939 LPFC_SLI_INTF_IF_TYPE_2)
3941 /* nemb_tp == nemb_hbd */
3942 ext_buf_cnt = sli_cfg_mbx->un.sli_config_emb1_subsys.hbd_count;
3943 if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_HBD) {
3944 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3945 "2954 Failed SLI_CONFIG(hbd) wr, "
3946 "ext_buf_cnt(%d) out of range(%d)\n",
3948 LPFC_MBX_SLI_CONFIG_MAX_HBD);
3951 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3952 "2950 Handled SLI_CONFIG(hbd) wr, "
3953 "ext_buf_cnt:%d\n", ext_buf_cnt);
3956 /* before dma buffer descriptor setup */
3957 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_wr, dma_mbox,
3958 sta_pre_addr, dmabuf, ext_buf_cnt);
3960 if (ext_buf_cnt == 0)
3963 /* for the first external buffer */
3964 lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, 0, dmabuf, dmabuf);
3966 /* after dma descriptor setup */
3967 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_wr, dma_mbox,
3968 sta_pos_addr, dmabuf, ext_buf_cnt);
3970 /* log for looking forward */
3971 for (i = 1; i < ext_buf_cnt; i++) {
3972 if (nemb_tp == nemb_mse)
3973 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3974 "2951 SLI_CONFIG(mse), buf[%d]-length:%d\n",
3975 i, sli_cfg_mbx->un.sli_config_emb0_subsys.
3978 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3979 "2952 SLI_CONFIG(hbd), buf[%d]-length:%d\n",
3980 i, bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
3981 &sli_cfg_mbx->un.sli_config_emb1_subsys.
3985 /* multi-buffer handling context */
3986 phba->mbox_ext_buf_ctx.nembType = nemb_tp;
3987 phba->mbox_ext_buf_ctx.mboxType = mbox_wr;
3988 phba->mbox_ext_buf_ctx.numBuf = ext_buf_cnt;
3989 phba->mbox_ext_buf_ctx.mbxTag = mbox_req->extMboxTag;
3990 phba->mbox_ext_buf_ctx.seqNum = mbox_req->extSeqNum;
3991 phba->mbox_ext_buf_ctx.mbx_dmabuf = dmabuf;
3993 if (ext_buf_cnt == 1) {
3994 /* bsg tracking structure */
3995 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
4001 /* mailbox command structure for base driver */
4002 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4007 memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
4008 pmb = &pmboxq->u.mb;
4009 mbx = (uint8_t *)dmabuf->virt;
4010 memcpy(pmb, mbx, sizeof(*pmb));
4011 pmb->mbxOwner = OWN_HOST;
4012 pmboxq->vport = phba->pport;
4014 /* callback for multi-buffer read mailbox command */
4015 pmboxq->mbox_cmpl = lpfc_bsg_issue_write_mbox_ext_cmpl;
4017 /* context fields to callback function */
4018 pmboxq->context1 = dd_data;
4019 dd_data->type = TYPE_MBOX;
4020 dd_data->set_job = job;
4021 dd_data->context_un.mbox.pmboxq = pmboxq;
4022 dd_data->context_un.mbox.mb = (MAILBOX_t *)mbx;
4023 job->dd_data = dd_data;
4027 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
4028 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
4029 if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
4030 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4031 "2955 Issued SLI_CONFIG ext-buffer "
4032 "maibox command, rc:x%x\n", rc);
4033 return SLI_CONFIG_HANDLED;
4035 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4036 "2956 Failed to issue SLI_CONFIG ext-buffer "
4037 "maibox command, rc:x%x\n", rc);
4042 /* wait for additoinal external buffers */
4044 job->reply->result = 0;
4046 return SLI_CONFIG_HANDLED;
4050 mempool_free(pmboxq, phba->mbox_mem_pool);
4057 * lpfc_bsg_handle_sli_cfg_mbox - handle sli-cfg mailbox cmd with ext buffer
4058 * @phba: Pointer to HBA context object.
4059 * @mb: Pointer to a BSG mailbox object.
4060 * @dmabuff: Pointer to a DMA buffer descriptor.
4062 * This routine handles SLI_CONFIG (0x9B) mailbox command with non-embedded
4063 * external bufffers, including both 0x9B with non-embedded MSEs and 0x9B
4064 * with embedded sussystem 0x1 and opcodes with external HBDs.
4067 lpfc_bsg_handle_sli_cfg_mbox(struct lpfc_hba *phba, struct fc_bsg_job *job,
4068 struct lpfc_dmabuf *dmabuf)
4070 struct lpfc_sli_config_mbox *sli_cfg_mbx;
4073 int rc = SLI_CONFIG_NOT_HANDLED;
4075 /* state change on new multi-buffer pass-through mailbox command */
4076 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_HOST;
4078 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
4080 if (!bsg_bf_get(lpfc_mbox_hdr_emb,
4081 &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) {
4082 subsys = bsg_bf_get(lpfc_emb0_subcmnd_subsys,
4083 &sli_cfg_mbx->un.sli_config_emb0_subsys);
4084 opcode = bsg_bf_get(lpfc_emb0_subcmnd_opcode,
4085 &sli_cfg_mbx->un.sli_config_emb0_subsys);
4086 if (subsys == SLI_CONFIG_SUBSYS_FCOE) {
4088 case FCOE_OPCODE_READ_FCF:
4089 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4090 "2957 Handled SLI_CONFIG "
4091 "subsys_fcoe, opcode:x%x\n",
4093 rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
4096 case FCOE_OPCODE_ADD_FCF:
4097 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4098 "2958 Handled SLI_CONFIG "
4099 "subsys_fcoe, opcode:x%x\n",
4101 rc = lpfc_bsg_sli_cfg_write_cmd_ext(phba, job,
4105 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4106 "2959 Reject SLI_CONFIG "
4107 "subsys_fcoe, opcode:x%x\n",
4112 } else if (subsys == SLI_CONFIG_SUBSYS_COMN) {
4114 case COMN_OPCODE_GET_CNTL_ADDL_ATTRIBUTES:
4115 case COMN_OPCODE_GET_CNTL_ATTRIBUTES:
4116 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4117 "3106 Handled SLI_CONFIG "
4118 "subsys_comn, opcode:x%x\n",
4120 rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
4124 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4125 "3107 Reject SLI_CONFIG "
4126 "subsys_comn, opcode:x%x\n",
4132 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4133 "2977 Reject SLI_CONFIG "
4134 "subsys:x%d, opcode:x%x\n",
4139 subsys = bsg_bf_get(lpfc_emb1_subcmnd_subsys,
4140 &sli_cfg_mbx->un.sli_config_emb1_subsys);
4141 opcode = bsg_bf_get(lpfc_emb1_subcmnd_opcode,
4142 &sli_cfg_mbx->un.sli_config_emb1_subsys);
4143 if (subsys == SLI_CONFIG_SUBSYS_COMN) {
4145 case COMN_OPCODE_READ_OBJECT:
4146 case COMN_OPCODE_READ_OBJECT_LIST:
4147 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4148 "2960 Handled SLI_CONFIG "
4149 "subsys_comn, opcode:x%x\n",
4151 rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
4154 case COMN_OPCODE_WRITE_OBJECT:
4155 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4156 "2961 Handled SLI_CONFIG "
4157 "subsys_comn, opcode:x%x\n",
4159 rc = lpfc_bsg_sli_cfg_write_cmd_ext(phba, job,
4163 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4164 "2962 Not handled SLI_CONFIG "
4165 "subsys_comn, opcode:x%x\n",
4167 rc = SLI_CONFIG_NOT_HANDLED;
4171 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4172 "2978 Not handled SLI_CONFIG "
4173 "subsys:x%d, opcode:x%x\n",
4175 rc = SLI_CONFIG_NOT_HANDLED;
4179 /* state reset on not handled new multi-buffer mailbox command */
4180 if (rc != SLI_CONFIG_HANDLED)
4181 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_IDLE;
4187 * lpfc_bsg_mbox_ext_abort_req - request to abort mbox command with ext buffers
4188 * @phba: Pointer to HBA context object.
4190 * This routine is for requesting to abort a pass-through mailbox command with
4191 * multiple external buffers due to error condition.
4194 lpfc_bsg_mbox_ext_abort(struct lpfc_hba *phba)
4196 if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_PORT)
4197 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_ABTS;
4199 lpfc_bsg_mbox_ext_session_reset(phba);
4204 * lpfc_bsg_read_ebuf_get - get the next mailbox read external buffer
4205 * @phba: Pointer to HBA context object.
4206 * @dmabuf: Pointer to a DMA buffer descriptor.
4208 * This routine extracts the next mailbox read external buffer back to
4209 * user space through BSG.
4212 lpfc_bsg_read_ebuf_get(struct lpfc_hba *phba, struct fc_bsg_job *job)
4214 struct lpfc_sli_config_mbox *sli_cfg_mbx;
4215 struct lpfc_dmabuf *dmabuf;
4220 index = phba->mbox_ext_buf_ctx.seqNum;
4221 phba->mbox_ext_buf_ctx.seqNum++;
4223 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)
4224 phba->mbox_ext_buf_ctx.mbx_dmabuf->virt;
4226 if (phba->mbox_ext_buf_ctx.nembType == nemb_mse) {
4227 size = bsg_bf_get(lpfc_mbox_sli_config_mse_len,
4228 &sli_cfg_mbx->un.sli_config_emb0_subsys.mse[index]);
4229 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4230 "2963 SLI_CONFIG (mse) ext-buffer rd get "
4231 "buffer[%d], size:%d\n", index, size);
4233 size = bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
4234 &sli_cfg_mbx->un.sli_config_emb1_subsys.hbd[index]);
4235 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4236 "2964 SLI_CONFIG (hbd) ext-buffer rd get "
4237 "buffer[%d], size:%d\n", index, size);
4239 if (list_empty(&phba->mbox_ext_buf_ctx.ext_dmabuf_list))
4241 dmabuf = list_first_entry(&phba->mbox_ext_buf_ctx.ext_dmabuf_list,
4242 struct lpfc_dmabuf, list);
4243 list_del_init(&dmabuf->list);
4245 /* after dma buffer descriptor setup */
4246 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, phba->mbox_ext_buf_ctx.nembType,
4247 mbox_rd, dma_ebuf, sta_pos_addr,
4250 pbuf = (uint8_t *)dmabuf->virt;
4251 job->reply->reply_payload_rcv_len =
4252 sg_copy_from_buffer(job->reply_payload.sg_list,
4253 job->reply_payload.sg_cnt,
4256 lpfc_bsg_dma_page_free(phba, dmabuf);
4258 if (phba->mbox_ext_buf_ctx.seqNum == phba->mbox_ext_buf_ctx.numBuf) {
4259 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4260 "2965 SLI_CONFIG (hbd) ext-buffer rd mbox "
4261 "command session done\n");
4262 lpfc_bsg_mbox_ext_session_reset(phba);
4265 job->reply->result = 0;
4268 return SLI_CONFIG_HANDLED;
4272 * lpfc_bsg_write_ebuf_set - set the next mailbox write external buffer
4273 * @phba: Pointer to HBA context object.
4274 * @dmabuf: Pointer to a DMA buffer descriptor.
4276 * This routine sets up the next mailbox read external buffer obtained
4277 * from user space through BSG.
4280 lpfc_bsg_write_ebuf_set(struct lpfc_hba *phba, struct fc_bsg_job *job,
4281 struct lpfc_dmabuf *dmabuf)
4283 struct lpfc_sli_config_mbox *sli_cfg_mbx;
4284 struct bsg_job_data *dd_data = NULL;
4285 LPFC_MBOXQ_t *pmboxq = NULL;
4287 enum nemb_type nemb_tp;
4293 index = phba->mbox_ext_buf_ctx.seqNum;
4294 phba->mbox_ext_buf_ctx.seqNum++;
4295 nemb_tp = phba->mbox_ext_buf_ctx.nembType;
4297 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)
4298 phba->mbox_ext_buf_ctx.mbx_dmabuf->virt;
4300 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
4306 pbuf = (uint8_t *)dmabuf->virt;
4307 size = job->request_payload.payload_len;
4308 sg_copy_to_buffer(job->request_payload.sg_list,
4309 job->request_payload.sg_cnt,
4312 if (phba->mbox_ext_buf_ctx.nembType == nemb_mse) {
4313 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4314 "2966 SLI_CONFIG (mse) ext-buffer wr set "
4315 "buffer[%d], size:%d\n",
4316 phba->mbox_ext_buf_ctx.seqNum, size);
4319 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4320 "2967 SLI_CONFIG (hbd) ext-buffer wr set "
4321 "buffer[%d], size:%d\n",
4322 phba->mbox_ext_buf_ctx.seqNum, size);
4326 /* set up external buffer descriptor and add to external buffer list */
4327 lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, index,
4328 phba->mbox_ext_buf_ctx.mbx_dmabuf,
4330 list_add_tail(&dmabuf->list, &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
4332 /* after write dma buffer */
4333 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, phba->mbox_ext_buf_ctx.nembType,
4334 mbox_wr, dma_ebuf, sta_pos_addr,
4337 if (phba->mbox_ext_buf_ctx.seqNum == phba->mbox_ext_buf_ctx.numBuf) {
4338 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4339 "2968 SLI_CONFIG ext-buffer wr all %d "
4340 "ebuffers received\n",
4341 phba->mbox_ext_buf_ctx.numBuf);
4342 /* mailbox command structure for base driver */
4343 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4348 memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
4349 pbuf = (uint8_t *)phba->mbox_ext_buf_ctx.mbx_dmabuf->virt;
4350 pmb = &pmboxq->u.mb;
4351 memcpy(pmb, pbuf, sizeof(*pmb));
4352 pmb->mbxOwner = OWN_HOST;
4353 pmboxq->vport = phba->pport;
4355 /* callback for multi-buffer write mailbox command */
4356 pmboxq->mbox_cmpl = lpfc_bsg_issue_write_mbox_ext_cmpl;
4358 /* context fields to callback function */
4359 pmboxq->context1 = dd_data;
4360 dd_data->type = TYPE_MBOX;
4361 dd_data->set_job = job;
4362 dd_data->context_un.mbox.pmboxq = pmboxq;
4363 dd_data->context_un.mbox.mb = (MAILBOX_t *)pbuf;
4364 job->dd_data = dd_data;
4367 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
4369 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
4370 if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
4371 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4372 "2969 Issued SLI_CONFIG ext-buffer "
4373 "maibox command, rc:x%x\n", rc);
4374 return SLI_CONFIG_HANDLED;
4376 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4377 "2970 Failed to issue SLI_CONFIG ext-buffer "
4378 "maibox command, rc:x%x\n", rc);
4383 /* wait for additoinal external buffers */
4384 job->reply->result = 0;
4386 return SLI_CONFIG_HANDLED;
4389 lpfc_bsg_dma_page_free(phba, dmabuf);
4396 * lpfc_bsg_handle_sli_cfg_ebuf - handle ext buffer with sli-cfg mailbox cmd
4397 * @phba: Pointer to HBA context object.
4398 * @mb: Pointer to a BSG mailbox object.
4399 * @dmabuff: Pointer to a DMA buffer descriptor.
4401 * This routine handles the external buffer with SLI_CONFIG (0x9B) mailbox
4402 * command with multiple non-embedded external buffers.
4405 lpfc_bsg_handle_sli_cfg_ebuf(struct lpfc_hba *phba, struct fc_bsg_job *job,
4406 struct lpfc_dmabuf *dmabuf)
4410 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4411 "2971 SLI_CONFIG buffer (type:x%x)\n",
4412 phba->mbox_ext_buf_ctx.mboxType);
4414 if (phba->mbox_ext_buf_ctx.mboxType == mbox_rd) {
4415 if (phba->mbox_ext_buf_ctx.state != LPFC_BSG_MBOX_DONE) {
4416 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4417 "2972 SLI_CONFIG rd buffer state "
4419 phba->mbox_ext_buf_ctx.state);
4420 lpfc_bsg_mbox_ext_abort(phba);
4423 rc = lpfc_bsg_read_ebuf_get(phba, job);
4424 if (rc == SLI_CONFIG_HANDLED)
4425 lpfc_bsg_dma_page_free(phba, dmabuf);
4426 } else { /* phba->mbox_ext_buf_ctx.mboxType == mbox_wr */
4427 if (phba->mbox_ext_buf_ctx.state != LPFC_BSG_MBOX_HOST) {
4428 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4429 "2973 SLI_CONFIG wr buffer state "
4431 phba->mbox_ext_buf_ctx.state);
4432 lpfc_bsg_mbox_ext_abort(phba);
4435 rc = lpfc_bsg_write_ebuf_set(phba, job, dmabuf);
4441 * lpfc_bsg_handle_sli_cfg_ext - handle sli-cfg mailbox with external buffer
4442 * @phba: Pointer to HBA context object.
4443 * @mb: Pointer to a BSG mailbox object.
4444 * @dmabuff: Pointer to a DMA buffer descriptor.
4446 * This routine checkes and handles non-embedded multi-buffer SLI_CONFIG
4447 * (0x9B) mailbox commands and external buffers.
4450 lpfc_bsg_handle_sli_cfg_ext(struct lpfc_hba *phba, struct fc_bsg_job *job,
4451 struct lpfc_dmabuf *dmabuf)
4453 struct dfc_mbox_req *mbox_req;
4454 int rc = SLI_CONFIG_NOT_HANDLED;
4457 (struct dfc_mbox_req *)job->request->rqst_data.h_vendor.vendor_cmd;
4459 /* mbox command with/without single external buffer */
4460 if (mbox_req->extMboxTag == 0 && mbox_req->extSeqNum == 0)
4463 /* mbox command and first external buffer */
4464 if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_IDLE) {
4465 if (mbox_req->extSeqNum == 1) {
4466 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4467 "2974 SLI_CONFIG mailbox: tag:%d, "
4468 "seq:%d\n", mbox_req->extMboxTag,
4469 mbox_req->extSeqNum);
4470 rc = lpfc_bsg_handle_sli_cfg_mbox(phba, job, dmabuf);
4473 goto sli_cfg_ext_error;
4477 * handle additional external buffers
4480 /* check broken pipe conditions */
4481 if (mbox_req->extMboxTag != phba->mbox_ext_buf_ctx.mbxTag)
4482 goto sli_cfg_ext_error;
4483 if (mbox_req->extSeqNum > phba->mbox_ext_buf_ctx.numBuf)
4484 goto sli_cfg_ext_error;
4485 if (mbox_req->extSeqNum != phba->mbox_ext_buf_ctx.seqNum + 1)
4486 goto sli_cfg_ext_error;
4488 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4489 "2975 SLI_CONFIG mailbox external buffer: "
4490 "extSta:x%x, tag:%d, seq:%d\n",
4491 phba->mbox_ext_buf_ctx.state, mbox_req->extMboxTag,
4492 mbox_req->extSeqNum);
4493 rc = lpfc_bsg_handle_sli_cfg_ebuf(phba, job, dmabuf);
4497 /* all other cases, broken pipe */
4498 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4499 "2976 SLI_CONFIG mailbox broken pipe: "
4500 "ctxSta:x%x, ctxNumBuf:%d "
4501 "ctxTag:%d, ctxSeq:%d, tag:%d, seq:%d\n",
4502 phba->mbox_ext_buf_ctx.state,
4503 phba->mbox_ext_buf_ctx.numBuf,
4504 phba->mbox_ext_buf_ctx.mbxTag,
4505 phba->mbox_ext_buf_ctx.seqNum,
4506 mbox_req->extMboxTag, mbox_req->extSeqNum);
4508 lpfc_bsg_mbox_ext_session_reset(phba);
4514 * lpfc_bsg_issue_mbox - issues a mailbox command on behalf of an app
4515 * @phba: Pointer to HBA context object.
4516 * @mb: Pointer to a mailbox object.
4517 * @vport: Pointer to a vport object.
4519 * Allocate a tracking object, mailbox command memory, get a mailbox
4520 * from the mailbox pool, copy the caller mailbox command.
4522 * If offline and the sli is active we need to poll for the command (port is
4523 * being reset) and com-plete the job, otherwise issue the mailbox command and
4524 * let our completion handler finish the command.
4527 lpfc_bsg_issue_mbox(struct lpfc_hba *phba, struct fc_bsg_job *job,
4528 struct lpfc_vport *vport)
4530 LPFC_MBOXQ_t *pmboxq = NULL; /* internal mailbox queue */
4531 MAILBOX_t *pmb; /* shortcut to the pmboxq mailbox */
4532 /* a 4k buffer to hold the mb and extended data from/to the bsg */
4533 uint8_t *pmbx = NULL;
4534 struct bsg_job_data *dd_data = NULL; /* bsg data tracking structure */
4535 struct lpfc_dmabuf *dmabuf = NULL;
4536 struct dfc_mbox_req *mbox_req;
4537 struct READ_EVENT_LOG_VAR *rdEventLog;
4538 uint32_t transmit_length, receive_length, mode;
4539 struct lpfc_mbx_sli4_config *sli4_config;
4540 struct lpfc_mbx_nembed_cmd *nembed_sge;
4541 struct mbox_header *header;
4542 struct ulp_bde64 *bde;
4543 uint8_t *ext = NULL;
4548 /* in case no data is transferred */
4549 job->reply->reply_payload_rcv_len = 0;
4551 /* sanity check to protect driver */
4552 if (job->reply_payload.payload_len > BSG_MBOX_SIZE ||
4553 job->request_payload.payload_len > BSG_MBOX_SIZE) {
4559 * Don't allow mailbox commands to be sent when blocked or when in
4560 * the middle of discovery
4562 if (phba->sli.sli_flag & LPFC_BLOCK_MGMT_IO) {
4568 (struct dfc_mbox_req *)job->request->rqst_data.h_vendor.vendor_cmd;
4570 /* check if requested extended data lengths are valid */
4571 if ((mbox_req->inExtWLen > BSG_MBOX_SIZE/sizeof(uint32_t)) ||
4572 (mbox_req->outExtWLen > BSG_MBOX_SIZE/sizeof(uint32_t))) {
4577 dmabuf = lpfc_bsg_dma_page_alloc(phba);
4578 if (!dmabuf || !dmabuf->virt) {
4583 /* Get the mailbox command or external buffer from BSG */
4584 pmbx = (uint8_t *)dmabuf->virt;
4585 size = job->request_payload.payload_len;
4586 sg_copy_to_buffer(job->request_payload.sg_list,
4587 job->request_payload.sg_cnt, pmbx, size);
4589 /* Handle possible SLI_CONFIG with non-embedded payloads */
4590 if (phba->sli_rev == LPFC_SLI_REV4) {
4591 rc = lpfc_bsg_handle_sli_cfg_ext(phba, job, dmabuf);
4592 if (rc == SLI_CONFIG_HANDLED)
4596 /* SLI_CONFIG_NOT_HANDLED for other mailbox commands */
4599 rc = lpfc_bsg_check_cmd_access(phba, (MAILBOX_t *)pmbx, vport);
4601 goto job_done; /* must be negative */
4603 /* allocate our bsg tracking structure */
4604 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
4606 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
4607 "2727 Failed allocation of dd_data\n");
4612 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4617 memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
4619 pmb = &pmboxq->u.mb;
4620 memcpy(pmb, pmbx, sizeof(*pmb));
4621 pmb->mbxOwner = OWN_HOST;
4622 pmboxq->vport = vport;
4624 /* If HBA encountered an error attention, allow only DUMP
4625 * or RESTART mailbox commands until the HBA is restarted.
4627 if (phba->pport->stopped &&
4628 pmb->mbxCommand != MBX_DUMP_MEMORY &&
4629 pmb->mbxCommand != MBX_RESTART &&
4630 pmb->mbxCommand != MBX_WRITE_VPARMS &&
4631 pmb->mbxCommand != MBX_WRITE_WWN)
4632 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
4633 "2797 mbox: Issued mailbox cmd "
4634 "0x%x while in stopped state.\n",
4637 /* extended mailbox commands will need an extended buffer */
4638 if (mbox_req->inExtWLen || mbox_req->outExtWLen) {
4640 ext = from + sizeof(MAILBOX_t);
4641 pmboxq->context2 = ext;
4642 pmboxq->in_ext_byte_len =
4643 mbox_req->inExtWLen * sizeof(uint32_t);
4644 pmboxq->out_ext_byte_len =
4645 mbox_req->outExtWLen * sizeof(uint32_t);
4646 pmboxq->mbox_offset_word = mbox_req->mbOffset;
4649 /* biu diag will need a kernel buffer to transfer the data
4650 * allocate our own buffer and setup the mailbox command to
4653 if (pmb->mbxCommand == MBX_RUN_BIU_DIAG64) {
4654 transmit_length = pmb->un.varWords[1];
4655 receive_length = pmb->un.varWords[4];
4656 /* transmit length cannot be greater than receive length or
4657 * mailbox extension size
4659 if ((transmit_length > receive_length) ||
4660 (transmit_length > BSG_MBOX_SIZE - sizeof(MAILBOX_t))) {
4664 pmb->un.varBIUdiag.un.s2.xmit_bde64.addrHigh =
4665 putPaddrHigh(dmabuf->phys + sizeof(MAILBOX_t));
4666 pmb->un.varBIUdiag.un.s2.xmit_bde64.addrLow =
4667 putPaddrLow(dmabuf->phys + sizeof(MAILBOX_t));
4669 pmb->un.varBIUdiag.un.s2.rcv_bde64.addrHigh =
4670 putPaddrHigh(dmabuf->phys + sizeof(MAILBOX_t)
4671 + pmb->un.varBIUdiag.un.s2.xmit_bde64.tus.f.bdeSize);
4672 pmb->un.varBIUdiag.un.s2.rcv_bde64.addrLow =
4673 putPaddrLow(dmabuf->phys + sizeof(MAILBOX_t)
4674 + pmb->un.varBIUdiag.un.s2.xmit_bde64.tus.f.bdeSize);
4675 } else if (pmb->mbxCommand == MBX_READ_EVENT_LOG) {
4676 rdEventLog = &pmb->un.varRdEventLog;
4677 receive_length = rdEventLog->rcv_bde64.tus.f.bdeSize;
4678 mode = bf_get(lpfc_event_log, rdEventLog);
4680 /* receive length cannot be greater than mailbox
4683 if (receive_length > BSG_MBOX_SIZE - sizeof(MAILBOX_t)) {
4688 /* mode zero uses a bde like biu diags command */
4690 pmb->un.varWords[3] = putPaddrLow(dmabuf->phys
4691 + sizeof(MAILBOX_t));
4692 pmb->un.varWords[4] = putPaddrHigh(dmabuf->phys
4693 + sizeof(MAILBOX_t));
4695 } else if (phba->sli_rev == LPFC_SLI_REV4) {
4696 /* Let type 4 (well known data) through because the data is
4697 * returned in varwords[4-8]
4698 * otherwise check the recieve length and fetch the buffer addr
4700 if ((pmb->mbxCommand == MBX_DUMP_MEMORY) &&
4701 (pmb->un.varDmp.type != DMP_WELL_KNOWN)) {
4702 /* rebuild the command for sli4 using our own buffers
4703 * like we do for biu diags
4705 receive_length = pmb->un.varWords[2];
4706 /* receive length cannot be greater than mailbox
4709 if (receive_length == 0) {
4713 pmb->un.varWords[3] = putPaddrLow(dmabuf->phys
4714 + sizeof(MAILBOX_t));
4715 pmb->un.varWords[4] = putPaddrHigh(dmabuf->phys
4716 + sizeof(MAILBOX_t));
4717 } else if ((pmb->mbxCommand == MBX_UPDATE_CFG) &&
4718 pmb->un.varUpdateCfg.co) {
4719 bde = (struct ulp_bde64 *)&pmb->un.varWords[4];
4721 /* bde size cannot be greater than mailbox ext size */
4722 if (bde->tus.f.bdeSize >
4723 BSG_MBOX_SIZE - sizeof(MAILBOX_t)) {
4727 bde->addrHigh = putPaddrHigh(dmabuf->phys
4728 + sizeof(MAILBOX_t));
4729 bde->addrLow = putPaddrLow(dmabuf->phys
4730 + sizeof(MAILBOX_t));
4731 } else if (pmb->mbxCommand == MBX_SLI4_CONFIG) {
4732 /* Handling non-embedded SLI_CONFIG mailbox command */
4733 sli4_config = &pmboxq->u.mqe.un.sli4_config;
4734 if (!bf_get(lpfc_mbox_hdr_emb,
4735 &sli4_config->header.cfg_mhdr)) {
4736 /* rebuild the command for sli4 using our
4737 * own buffers like we do for biu diags
4739 header = (struct mbox_header *)
4740 &pmb->un.varWords[0];
4741 nembed_sge = (struct lpfc_mbx_nembed_cmd *)
4742 &pmb->un.varWords[0];
4743 receive_length = nembed_sge->sge[0].length;
4745 /* receive length cannot be greater than
4746 * mailbox extension size
4748 if ((receive_length == 0) ||
4750 BSG_MBOX_SIZE - sizeof(MAILBOX_t))) {
4755 nembed_sge->sge[0].pa_hi =
4756 putPaddrHigh(dmabuf->phys
4757 + sizeof(MAILBOX_t));
4758 nembed_sge->sge[0].pa_lo =
4759 putPaddrLow(dmabuf->phys
4760 + sizeof(MAILBOX_t));
4765 dd_data->context_un.mbox.dmabuffers = dmabuf;
4767 /* setup wake call as IOCB callback */
4768 pmboxq->mbox_cmpl = lpfc_bsg_issue_mbox_cmpl;
4770 /* setup context field to pass wait_queue pointer to wake function */
4771 pmboxq->context1 = dd_data;
4772 dd_data->type = TYPE_MBOX;
4773 dd_data->set_job = job;
4774 dd_data->context_un.mbox.pmboxq = pmboxq;
4775 dd_data->context_un.mbox.mb = (MAILBOX_t *)pmbx;
4776 dd_data->context_un.mbox.ext = ext;
4777 dd_data->context_un.mbox.mbOffset = mbox_req->mbOffset;
4778 dd_data->context_un.mbox.inExtWLen = mbox_req->inExtWLen;
4779 dd_data->context_un.mbox.outExtWLen = mbox_req->outExtWLen;
4780 job->dd_data = dd_data;
4782 if ((vport->fc_flag & FC_OFFLINE_MODE) ||
4783 (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))) {
4784 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
4785 if (rc != MBX_SUCCESS) {
4786 rc = (rc == MBX_TIMEOUT) ? -ETIME : -ENODEV;
4790 /* job finished, copy the data */
4791 memcpy(pmbx, pmb, sizeof(*pmb));
4792 job->reply->reply_payload_rcv_len =
4793 sg_copy_from_buffer(job->reply_payload.sg_list,
4794 job->reply_payload.sg_cnt,
4796 /* not waiting mbox already done */
4801 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
4802 if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY))
4803 return 1; /* job started */
4806 /* common exit for error or job completed inline */
4808 mempool_free(pmboxq, phba->mbox_mem_pool);
4809 lpfc_bsg_dma_page_free(phba, dmabuf);
4817 * lpfc_bsg_mbox_cmd - process an fc bsg LPFC_BSG_VENDOR_MBOX command
4818 * @job: MBOX fc_bsg_job for LPFC_BSG_VENDOR_MBOX.
4821 lpfc_bsg_mbox_cmd(struct fc_bsg_job *job)
4823 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
4824 struct lpfc_hba *phba = vport->phba;
4825 struct dfc_mbox_req *mbox_req;
4828 /* mix-and-match backward compatibility */
4829 job->reply->reply_payload_rcv_len = 0;
4830 if (job->request_len <
4831 sizeof(struct fc_bsg_request) + sizeof(struct dfc_mbox_req)) {
4832 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4833 "2737 Mix-and-match backward compatibility "
4834 "between MBOX_REQ old size:%d and "
4835 "new request size:%d\n",
4836 (int)(job->request_len -
4837 sizeof(struct fc_bsg_request)),
4838 (int)sizeof(struct dfc_mbox_req));
4839 mbox_req = (struct dfc_mbox_req *)
4840 job->request->rqst_data.h_vendor.vendor_cmd;
4841 mbox_req->extMboxTag = 0;
4842 mbox_req->extSeqNum = 0;
4845 rc = lpfc_bsg_issue_mbox(phba, job, vport);
4849 job->reply->result = 0;
4850 job->dd_data = NULL;
4853 /* job submitted, will complete later*/
4854 rc = 0; /* return zero, no error */
4856 /* some error occurred */
4857 job->reply->result = rc;
4858 job->dd_data = NULL;
4865 * lpfc_bsg_menlo_cmd_cmp - lpfc_menlo_cmd completion handler
4866 * @phba: Pointer to HBA context object.
4867 * @cmdiocbq: Pointer to command iocb.
4868 * @rspiocbq: Pointer to response iocb.
4870 * This function is the completion handler for iocbs issued using
4871 * lpfc_menlo_cmd function. This function is called by the
4872 * ring event handler function without any lock held. This function
4873 * can be called from both worker thread context and interrupt
4874 * context. This function also can be called from another thread which
4875 * cleans up the SLI layer objects.
4876 * This function copies the contents of the response iocb to the
4877 * response iocb memory object provided by the caller of
4878 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
4879 * sleeps for the iocb completion.
4882 lpfc_bsg_menlo_cmd_cmp(struct lpfc_hba *phba,
4883 struct lpfc_iocbq *cmdiocbq,
4884 struct lpfc_iocbq *rspiocbq)
4886 struct bsg_job_data *dd_data;
4887 struct fc_bsg_job *job;
4889 struct lpfc_dmabuf *bmp, *cmp, *rmp;
4890 struct lpfc_bsg_menlo *menlo;
4891 unsigned long flags;
4892 struct menlo_response *menlo_resp;
4893 unsigned int rsp_size;
4896 dd_data = cmdiocbq->context1;
4897 cmp = cmdiocbq->context2;
4898 bmp = cmdiocbq->context3;
4899 menlo = &dd_data->context_un.menlo;
4901 rsp = &rspiocbq->iocb;
4903 /* Determine if job has been aborted */
4904 spin_lock_irqsave(&phba->ct_ev_lock, flags);
4905 job = dd_data->set_job;
4907 /* Prevent timeout handling from trying to abort job */
4908 job->dd_data = NULL;
4910 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
4912 /* Copy the job data or set the failing status for the job */
4915 /* always return the xri, this would be used in the case
4916 * of a menlo download to allow the data to be sent as a
4917 * continuation of the exchange.
4920 menlo_resp = (struct menlo_response *)
4921 job->reply->reply_data.vendor_reply.vendor_rsp;
4922 menlo_resp->xri = rsp->ulpContext;
4923 if (rsp->ulpStatus) {
4924 if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
4925 switch (rsp->un.ulpWord[4] & IOERR_PARAM_MASK) {
4926 case IOERR_SEQUENCE_TIMEOUT:
4929 case IOERR_INVALID_RPI:
4940 rsp_size = rsp->un.genreq64.bdl.bdeSize;
4941 job->reply->reply_payload_rcv_len =
4942 lpfc_bsg_copy_data(rmp, &job->reply_payload,
4948 lpfc_sli_release_iocbq(phba, cmdiocbq);
4949 lpfc_free_bsg_buffers(phba, cmp);
4950 lpfc_free_bsg_buffers(phba, rmp);
4951 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
4955 /* Complete the job if active */
4958 job->reply->result = rc;
4966 * lpfc_menlo_cmd - send an ioctl for menlo hardware
4967 * @job: fc_bsg_job to handle
4969 * This function issues a gen request 64 CR ioctl for all menlo cmd requests,
4970 * all the command completions will return the xri for the command.
4971 * For menlo data requests a gen request 64 CX is used to continue the exchange
4972 * supplied in the menlo request header xri field.
4975 lpfc_menlo_cmd(struct fc_bsg_job *job)
4977 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
4978 struct lpfc_hba *phba = vport->phba;
4979 struct lpfc_iocbq *cmdiocbq;
4982 struct menlo_command *menlo_cmd;
4983 struct menlo_response *menlo_resp;
4984 struct lpfc_dmabuf *bmp = NULL, *cmp = NULL, *rmp = NULL;
4987 struct bsg_job_data *dd_data;
4988 struct ulp_bde64 *bpl = NULL;
4990 /* in case no data is returned return just the return code */
4991 job->reply->reply_payload_rcv_len = 0;
4993 if (job->request_len <
4994 sizeof(struct fc_bsg_request) +
4995 sizeof(struct menlo_command)) {
4996 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
4997 "2784 Received MENLO_CMD request below "
5003 if (job->reply_len <
5004 sizeof(struct fc_bsg_request) + sizeof(struct menlo_response)) {
5005 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5006 "2785 Received MENLO_CMD reply below "
5012 if (!(phba->menlo_flag & HBA_MENLO_SUPPORT)) {
5013 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5014 "2786 Adapter does not support menlo "
5020 menlo_cmd = (struct menlo_command *)
5021 job->request->rqst_data.h_vendor.vendor_cmd;
5023 menlo_resp = (struct menlo_response *)
5024 job->reply->reply_data.vendor_reply.vendor_rsp;
5026 /* allocate our bsg tracking structure */
5027 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
5029 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5030 "2787 Failed allocation of dd_data\n");
5035 bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5041 bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
5047 INIT_LIST_HEAD(&bmp->list);
5049 bpl = (struct ulp_bde64 *)bmp->virt;
5050 request_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64);
5051 cmp = lpfc_alloc_bsg_buffers(phba, job->request_payload.payload_len,
5052 1, bpl, &request_nseg);
5057 lpfc_bsg_copy_data(cmp, &job->request_payload,
5058 job->request_payload.payload_len, 1);
5060 bpl += request_nseg;
5061 reply_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64) - request_nseg;
5062 rmp = lpfc_alloc_bsg_buffers(phba, job->reply_payload.payload_len, 0,
5069 cmdiocbq = lpfc_sli_get_iocbq(phba);
5075 cmd = &cmdiocbq->iocb;
5076 cmd->un.genreq64.bdl.ulpIoTag32 = 0;
5077 cmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
5078 cmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
5079 cmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
5080 cmd->un.genreq64.bdl.bdeSize =
5081 (request_nseg + reply_nseg) * sizeof(struct ulp_bde64);
5082 cmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
5083 cmd->un.genreq64.w5.hcsw.Dfctl = 0;
5084 cmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CMD;
5085 cmd->un.genreq64.w5.hcsw.Type = MENLO_TRANSPORT_TYPE; /* 0xfe */
5086 cmd->ulpBdeCount = 1;
5087 cmd->ulpClass = CLASS3;
5088 cmd->ulpOwner = OWN_CHIP;
5089 cmd->ulpLe = 1; /* Limited Edition */
5090 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
5091 cmdiocbq->vport = phba->pport;
5092 /* We want the firmware to timeout before we do */
5093 cmd->ulpTimeout = MENLO_TIMEOUT - 5;
5094 cmdiocbq->iocb_cmpl = lpfc_bsg_menlo_cmd_cmp;
5095 cmdiocbq->context1 = dd_data;
5096 cmdiocbq->context2 = cmp;
5097 cmdiocbq->context3 = bmp;
5098 if (menlo_cmd->cmd == LPFC_BSG_VENDOR_MENLO_CMD) {
5099 cmd->ulpCommand = CMD_GEN_REQUEST64_CR;
5100 cmd->ulpPU = MENLO_PU; /* 3 */
5101 cmd->un.ulpWord[4] = MENLO_DID; /* 0x0000FC0E */
5102 cmd->ulpContext = MENLO_CONTEXT; /* 0 */
5104 cmd->ulpCommand = CMD_GEN_REQUEST64_CX;
5106 cmd->un.ulpWord[4] = 0;
5107 cmd->ulpContext = menlo_cmd->xri;
5110 dd_data->type = TYPE_MENLO;
5111 dd_data->set_job = job;
5112 dd_data->context_un.menlo.cmdiocbq = cmdiocbq;
5113 dd_data->context_un.menlo.rmp = rmp;
5114 job->dd_data = dd_data;
5116 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq,
5118 if (rc == IOCB_SUCCESS)
5119 return 0; /* done for now */
5121 lpfc_sli_release_iocbq(phba, cmdiocbq);
5124 lpfc_free_bsg_buffers(phba, rmp);
5126 lpfc_free_bsg_buffers(phba, cmp);
5129 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
5134 /* make error code available to userspace */
5135 job->reply->result = rc;
5136 job->dd_data = NULL;
5141 * lpfc_bsg_hst_vendor - process a vendor-specific fc_bsg_job
5142 * @job: fc_bsg_job to handle
5145 lpfc_bsg_hst_vendor(struct fc_bsg_job *job)
5147 int command = job->request->rqst_data.h_vendor.vendor_cmd[0];
5151 case LPFC_BSG_VENDOR_SET_CT_EVENT:
5152 rc = lpfc_bsg_hba_set_event(job);
5154 case LPFC_BSG_VENDOR_GET_CT_EVENT:
5155 rc = lpfc_bsg_hba_get_event(job);
5157 case LPFC_BSG_VENDOR_SEND_MGMT_RESP:
5158 rc = lpfc_bsg_send_mgmt_rsp(job);
5160 case LPFC_BSG_VENDOR_DIAG_MODE:
5161 rc = lpfc_bsg_diag_loopback_mode(job);
5163 case LPFC_BSG_VENDOR_DIAG_MODE_END:
5164 rc = lpfc_sli4_bsg_diag_mode_end(job);
5166 case LPFC_BSG_VENDOR_DIAG_RUN_LOOPBACK:
5167 rc = lpfc_bsg_diag_loopback_run(job);
5169 case LPFC_BSG_VENDOR_LINK_DIAG_TEST:
5170 rc = lpfc_sli4_bsg_link_diag_test(job);
5172 case LPFC_BSG_VENDOR_GET_MGMT_REV:
5173 rc = lpfc_bsg_get_dfc_rev(job);
5175 case LPFC_BSG_VENDOR_MBOX:
5176 rc = lpfc_bsg_mbox_cmd(job);
5178 case LPFC_BSG_VENDOR_MENLO_CMD:
5179 case LPFC_BSG_VENDOR_MENLO_DATA:
5180 rc = lpfc_menlo_cmd(job);
5184 job->reply->reply_payload_rcv_len = 0;
5185 /* make error code available to userspace */
5186 job->reply->result = rc;
5194 * lpfc_bsg_request - handle a bsg request from the FC transport
5195 * @job: fc_bsg_job to handle
5198 lpfc_bsg_request(struct fc_bsg_job *job)
5203 msgcode = job->request->msgcode;
5205 case FC_BSG_HST_VENDOR:
5206 rc = lpfc_bsg_hst_vendor(job);
5208 case FC_BSG_RPT_ELS:
5209 rc = lpfc_bsg_rport_els(job);
5212 rc = lpfc_bsg_send_mgmt_cmd(job);
5216 job->reply->reply_payload_rcv_len = 0;
5217 /* make error code available to userspace */
5218 job->reply->result = rc;
5226 * lpfc_bsg_timeout - handle timeout of a bsg request from the FC transport
5227 * @job: fc_bsg_job that has timed out
5229 * This function just aborts the job's IOCB. The aborted IOCB will return to
5230 * the waiting function which will handle passing the error back to userspace
5233 lpfc_bsg_timeout(struct fc_bsg_job *job)
5235 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
5236 struct lpfc_hba *phba = vport->phba;
5237 struct lpfc_iocbq *cmdiocb;
5238 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
5239 struct bsg_job_data *dd_data;
5240 unsigned long flags;
5242 LIST_HEAD(completions);
5243 struct lpfc_iocbq *check_iocb, *next_iocb;
5245 /* if job's driver data is NULL, the command completed or is in the
5246 * the process of completing. In this case, return status to request
5247 * so the timeout is retried. This avoids double completion issues
5248 * and the request will be pulled off the timer queue when the
5249 * command's completion handler executes. Otherwise, prevent the
5250 * command's completion handler from executing the job done callback
5251 * and continue processing to abort the outstanding the command.
5254 spin_lock_irqsave(&phba->ct_ev_lock, flags);
5255 dd_data = (struct bsg_job_data *)job->dd_data;
5257 dd_data->set_job = NULL;
5258 job->dd_data = NULL;
5260 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5264 switch (dd_data->type) {
5266 /* Check to see if IOCB was issued to the port or not. If not,
5267 * remove it from the txq queue and call cancel iocbs.
5268 * Otherwise, call abort iotag
5271 cmdiocb = dd_data->context_un.iocb.cmdiocbq;
5272 spin_lock_irq(&phba->hbalock);
5273 list_for_each_entry_safe(check_iocb, next_iocb, &pring->txq,
5275 if (check_iocb == cmdiocb) {
5276 list_move_tail(&check_iocb->list, &completions);
5280 if (list_empty(&completions))
5281 lpfc_sli_issue_abort_iotag(phba, pring, cmdiocb);
5282 spin_unlock_irq(&phba->hbalock);
5283 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5284 if (!list_empty(&completions)) {
5285 lpfc_sli_cancel_iocbs(phba, &completions,
5286 IOSTAT_LOCAL_REJECT,
5292 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5296 /* Update the ext buf ctx state if needed */
5298 if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_PORT)
5299 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_ABTS;
5300 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5303 /* Check to see if IOCB was issued to the port or not. If not,
5304 * remove it from the txq queue and call cancel iocbs.
5305 * Otherwise, call abort iotag.
5308 cmdiocb = dd_data->context_un.menlo.cmdiocbq;
5309 spin_lock_irq(&phba->hbalock);
5310 list_for_each_entry_safe(check_iocb, next_iocb, &pring->txq,
5312 if (check_iocb == cmdiocb) {
5313 list_move_tail(&check_iocb->list, &completions);
5317 if (list_empty(&completions))
5318 lpfc_sli_issue_abort_iotag(phba, pring, cmdiocb);
5319 spin_unlock_irq(&phba->hbalock);
5320 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5321 if (!list_empty(&completions)) {
5322 lpfc_sli_cancel_iocbs(phba, &completions,
5323 IOSTAT_LOCAL_REJECT,
5328 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5332 /* scsi transport fc fc_bsg_job_timeout expects a zero return code,
5333 * otherwise an error message will be displayed on the console
5334 * so always return success (zero)