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 /* job waiting for this event to finish */
68 struct fc_bsg_job *set_job;
71 struct lpfc_bsg_iocb {
72 struct lpfc_iocbq *cmdiocbq;
73 struct lpfc_iocbq *rspiocbq;
74 struct lpfc_dmabuf *bmp;
75 struct lpfc_nodelist *ndlp;
77 /* job waiting for this iocb to finish */
78 struct fc_bsg_job *set_job;
81 struct lpfc_bsg_mbox {
84 struct lpfc_dmabuf *dmabuffers; /* for BIU diags */
85 uint8_t *ext; /* extended mailbox data */
86 uint32_t mbOffset; /* from app */
87 uint32_t inExtWLen; /* from app */
88 uint32_t outExtWLen; /* from app */
90 /* job waiting for this mbox command to finish */
91 struct fc_bsg_job *set_job;
94 #define MENLO_DID 0x0000FC0E
96 struct lpfc_bsg_menlo {
97 struct lpfc_iocbq *cmdiocbq;
98 struct lpfc_iocbq *rspiocbq;
99 struct lpfc_dmabuf *bmp;
101 /* job waiting for this iocb to finish */
102 struct fc_bsg_job *set_job;
109 struct bsg_job_data {
112 struct lpfc_bsg_event *evt;
113 struct lpfc_bsg_iocb iocb;
114 struct lpfc_bsg_mbox mbox;
115 struct lpfc_bsg_menlo menlo;
120 struct list_head node;
127 #define BUF_SZ_4K 4096
128 #define SLI_CT_ELX_LOOPBACK 0x10
130 enum ELX_LOOPBACK_CMD {
131 ELX_LOOPBACK_XRI_SETUP,
135 #define ELX_LOOPBACK_HEADER_SZ \
136 (size_t)(&((struct lpfc_sli_ct_request *)NULL)->un)
138 struct lpfc_dmabufext {
139 struct lpfc_dmabuf dma;
145 * lpfc_bsg_send_mgmt_cmd_cmp - lpfc_bsg_send_mgmt_cmd's completion handler
146 * @phba: Pointer to HBA context object.
147 * @cmdiocbq: Pointer to command iocb.
148 * @rspiocbq: Pointer to response iocb.
150 * This function is the completion handler for iocbs issued using
151 * lpfc_bsg_send_mgmt_cmd function. This function is called by the
152 * ring event handler function without any lock held. This function
153 * can be called from both worker thread context and interrupt
154 * context. This function also can be called from another thread which
155 * cleans up the SLI layer objects.
156 * This function copies the contents of the response iocb to the
157 * response iocb memory object provided by the caller of
158 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
159 * sleeps for the iocb completion.
162 lpfc_bsg_send_mgmt_cmd_cmp(struct lpfc_hba *phba,
163 struct lpfc_iocbq *cmdiocbq,
164 struct lpfc_iocbq *rspiocbq)
166 struct bsg_job_data *dd_data;
167 struct fc_bsg_job *job;
169 struct lpfc_dmabuf *bmp;
170 struct lpfc_nodelist *ndlp;
171 struct lpfc_bsg_iocb *iocb;
175 spin_lock_irqsave(&phba->ct_ev_lock, flags);
176 dd_data = cmdiocbq->context2;
178 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
179 lpfc_sli_release_iocbq(phba, cmdiocbq);
183 iocb = &dd_data->context_un.iocb;
185 job->dd_data = NULL; /* so timeout handler does not reply */
188 rsp = &rspiocbq->iocb;
189 ndlp = cmdiocbq->context1;
191 pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
192 job->request_payload.sg_cnt, DMA_TO_DEVICE);
193 pci_unmap_sg(phba->pcidev, job->reply_payload.sg_list,
194 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
196 if (rsp->ulpStatus) {
197 if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
198 switch (rsp->un.ulpWord[4] & IOERR_PARAM_MASK) {
199 case IOERR_SEQUENCE_TIMEOUT:
202 case IOERR_INVALID_RPI:
212 job->reply->reply_payload_rcv_len =
213 rsp->un.genreq64.bdl.bdeSize;
215 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
216 lpfc_sli_release_iocbq(phba, cmdiocbq);
220 /* make error code available to userspace */
221 job->reply->result = rc;
222 /* complete the job back to userspace */
224 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
229 * lpfc_bsg_send_mgmt_cmd - send a CT command from a bsg request
230 * @job: fc_bsg_job to handle
233 lpfc_bsg_send_mgmt_cmd(struct fc_bsg_job *job)
235 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
236 struct lpfc_hba *phba = vport->phba;
237 struct lpfc_rport_data *rdata = job->rport->dd_data;
238 struct lpfc_nodelist *ndlp = rdata->pnode;
239 struct ulp_bde64 *bpl = NULL;
241 struct lpfc_iocbq *cmdiocbq = NULL;
243 struct lpfc_dmabuf *bmp = NULL;
246 struct scatterlist *sgel = NULL;
249 struct bsg_job_data *dd_data;
254 /* in case no data is transferred */
255 job->reply->reply_payload_rcv_len = 0;
257 /* allocate our bsg tracking structure */
258 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
260 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
261 "2733 Failed allocation of dd_data\n");
266 if (!lpfc_nlp_get(ndlp)) {
271 bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
277 if (ndlp->nlp_flag & NLP_ELS_SND_MASK) {
282 cmdiocbq = lpfc_sli_get_iocbq(phba);
288 cmd = &cmdiocbq->iocb;
289 bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
295 INIT_LIST_HEAD(&bmp->list);
296 bpl = (struct ulp_bde64 *) bmp->virt;
297 request_nseg = pci_map_sg(phba->pcidev, job->request_payload.sg_list,
298 job->request_payload.sg_cnt, DMA_TO_DEVICE);
299 for_each_sg(job->request_payload.sg_list, sgel, request_nseg, numbde) {
300 busaddr = sg_dma_address(sgel);
301 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
302 bpl->tus.f.bdeSize = sg_dma_len(sgel);
303 bpl->tus.w = cpu_to_le32(bpl->tus.w);
304 bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
305 bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
309 reply_nseg = pci_map_sg(phba->pcidev, job->reply_payload.sg_list,
310 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
311 for_each_sg(job->reply_payload.sg_list, sgel, reply_nseg, numbde) {
312 busaddr = sg_dma_address(sgel);
313 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
314 bpl->tus.f.bdeSize = sg_dma_len(sgel);
315 bpl->tus.w = cpu_to_le32(bpl->tus.w);
316 bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
317 bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
321 cmd->un.genreq64.bdl.ulpIoTag32 = 0;
322 cmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
323 cmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
324 cmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
325 cmd->un.genreq64.bdl.bdeSize =
326 (request_nseg + reply_nseg) * sizeof(struct ulp_bde64);
327 cmd->ulpCommand = CMD_GEN_REQUEST64_CR;
328 cmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
329 cmd->un.genreq64.w5.hcsw.Dfctl = 0;
330 cmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
331 cmd->un.genreq64.w5.hcsw.Type = FC_TYPE_CT;
332 cmd->ulpBdeCount = 1;
334 cmd->ulpClass = CLASS3;
335 cmd->ulpContext = ndlp->nlp_rpi;
336 if (phba->sli_rev == LPFC_SLI_REV4)
337 cmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
338 cmd->ulpOwner = OWN_CHIP;
339 cmdiocbq->vport = phba->pport;
340 cmdiocbq->context3 = bmp;
341 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
342 timeout = phba->fc_ratov * 2;
343 cmd->ulpTimeout = timeout;
345 cmdiocbq->iocb_cmpl = lpfc_bsg_send_mgmt_cmd_cmp;
346 cmdiocbq->context1 = ndlp;
347 cmdiocbq->context2 = dd_data;
348 dd_data->type = TYPE_IOCB;
349 dd_data->context_un.iocb.cmdiocbq = cmdiocbq;
350 dd_data->context_un.iocb.set_job = job;
351 dd_data->context_un.iocb.bmp = bmp;
353 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
354 if (lpfc_readl(phba->HCregaddr, &creg_val)) {
358 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
359 writel(creg_val, phba->HCregaddr);
360 readl(phba->HCregaddr); /* flush */
363 iocb_stat = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq, 0);
364 if (iocb_stat == IOCB_SUCCESS)
365 return 0; /* done for now */
366 else if (iocb_stat == IOCB_BUSY)
372 /* iocb failed so cleanup */
373 pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
374 job->request_payload.sg_cnt, DMA_TO_DEVICE);
375 pci_unmap_sg(phba->pcidev, job->reply_payload.sg_list,
376 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
378 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
381 lpfc_sli_release_iocbq(phba, cmdiocbq);
389 /* make error code available to userspace */
390 job->reply->result = rc;
396 * lpfc_bsg_rport_els_cmp - lpfc_bsg_rport_els's completion handler
397 * @phba: Pointer to HBA context object.
398 * @cmdiocbq: Pointer to command iocb.
399 * @rspiocbq: Pointer to response iocb.
401 * This function is the completion handler for iocbs issued using
402 * lpfc_bsg_rport_els_cmp function. This function is called by the
403 * ring event handler function without any lock held. This function
404 * can be called from both worker thread context and interrupt
405 * context. This function also can be called from other thread which
406 * cleans up the SLI layer objects.
407 * This function copies the contents of the response iocb to the
408 * response iocb memory object provided by the caller of
409 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
410 * sleeps for the iocb completion.
413 lpfc_bsg_rport_els_cmp(struct lpfc_hba *phba,
414 struct lpfc_iocbq *cmdiocbq,
415 struct lpfc_iocbq *rspiocbq)
417 struct bsg_job_data *dd_data;
418 struct fc_bsg_job *job;
420 struct lpfc_nodelist *ndlp;
421 struct lpfc_dmabuf *pbuflist = NULL;
422 struct fc_bsg_ctels_reply *els_reply;
427 spin_lock_irqsave(&phba->ct_ev_lock, flags);
428 dd_data = cmdiocbq->context1;
429 /* normal completion and timeout crossed paths, already done */
431 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
435 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
436 if (cmdiocbq->context2 && rspiocbq)
437 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
438 &rspiocbq->iocb, sizeof(IOCB_t));
440 job = dd_data->context_un.iocb.set_job;
441 cmdiocbq = dd_data->context_un.iocb.cmdiocbq;
442 rspiocbq = dd_data->context_un.iocb.rspiocbq;
443 rsp = &rspiocbq->iocb;
444 ndlp = dd_data->context_un.iocb.ndlp;
446 pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
447 job->request_payload.sg_cnt, DMA_TO_DEVICE);
448 pci_unmap_sg(phba->pcidev, job->reply_payload.sg_list,
449 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
451 if (job->reply->result == -EAGAIN)
453 else if (rsp->ulpStatus == IOSTAT_SUCCESS)
454 job->reply->reply_payload_rcv_len =
455 rsp->un.elsreq64.bdl.bdeSize;
456 else if (rsp->ulpStatus == IOSTAT_LS_RJT) {
457 job->reply->reply_payload_rcv_len =
458 sizeof(struct fc_bsg_ctels_reply);
459 /* LS_RJT data returned in word 4 */
460 rjt_data = (uint8_t *)&rsp->un.ulpWord[4];
461 els_reply = &job->reply->reply_data.ctels_reply;
462 els_reply->status = FC_CTELS_STATUS_REJECT;
463 els_reply->rjt_data.action = rjt_data[3];
464 els_reply->rjt_data.reason_code = rjt_data[2];
465 els_reply->rjt_data.reason_explanation = rjt_data[1];
466 els_reply->rjt_data.vendor_unique = rjt_data[0];
470 pbuflist = (struct lpfc_dmabuf *) cmdiocbq->context3;
471 lpfc_mbuf_free(phba, pbuflist->virt, pbuflist->phys);
472 lpfc_sli_release_iocbq(phba, rspiocbq);
473 lpfc_sli_release_iocbq(phba, cmdiocbq);
476 /* make error code available to userspace */
477 job->reply->result = rc;
479 /* complete the job back to userspace */
481 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
486 * lpfc_bsg_rport_els - send an ELS command from a bsg request
487 * @job: fc_bsg_job to handle
490 lpfc_bsg_rport_els(struct fc_bsg_job *job)
492 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
493 struct lpfc_hba *phba = vport->phba;
494 struct lpfc_rport_data *rdata = job->rport->dd_data;
495 struct lpfc_nodelist *ndlp = rdata->pnode;
499 struct lpfc_iocbq *rspiocbq;
500 struct lpfc_iocbq *cmdiocbq;
503 struct lpfc_dmabuf *pcmd;
504 struct lpfc_dmabuf *prsp;
505 struct lpfc_dmabuf *pbuflist = NULL;
506 struct ulp_bde64 *bpl;
509 struct scatterlist *sgel = NULL;
512 struct bsg_job_data *dd_data;
516 /* in case no data is transferred */
517 job->reply->reply_payload_rcv_len = 0;
519 /* allocate our bsg tracking structure */
520 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
522 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
523 "2735 Failed allocation of dd_data\n");
528 if (!lpfc_nlp_get(ndlp)) {
533 elscmd = job->request->rqst_data.r_els.els_code;
534 cmdsize = job->request_payload.payload_len;
535 rspsize = job->reply_payload.payload_len;
536 rspiocbq = lpfc_sli_get_iocbq(phba);
543 rsp = &rspiocbq->iocb;
546 cmdiocbq = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp,
547 ndlp->nlp_DID, elscmd);
553 /* prep els iocb set context1 to the ndlp, context2 to the command
554 * dmabuf, context3 holds the data dmabuf
556 pcmd = (struct lpfc_dmabuf *) cmdiocbq->context2;
557 prsp = (struct lpfc_dmabuf *) pcmd->list.next;
558 lpfc_mbuf_free(phba, pcmd->virt, pcmd->phys);
560 lpfc_mbuf_free(phba, prsp->virt, prsp->phys);
562 cmdiocbq->context2 = NULL;
564 pbuflist = (struct lpfc_dmabuf *) cmdiocbq->context3;
565 bpl = (struct ulp_bde64 *) pbuflist->virt;
567 request_nseg = pci_map_sg(phba->pcidev, job->request_payload.sg_list,
568 job->request_payload.sg_cnt, DMA_TO_DEVICE);
569 for_each_sg(job->request_payload.sg_list, sgel, request_nseg, numbde) {
570 busaddr = sg_dma_address(sgel);
571 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
572 bpl->tus.f.bdeSize = sg_dma_len(sgel);
573 bpl->tus.w = cpu_to_le32(bpl->tus.w);
574 bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
575 bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
579 reply_nseg = pci_map_sg(phba->pcidev, job->reply_payload.sg_list,
580 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
581 for_each_sg(job->reply_payload.sg_list, sgel, reply_nseg, numbde) {
582 busaddr = sg_dma_address(sgel);
583 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
584 bpl->tus.f.bdeSize = sg_dma_len(sgel);
585 bpl->tus.w = cpu_to_le32(bpl->tus.w);
586 bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
587 bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
590 cmdiocbq->iocb.un.elsreq64.bdl.bdeSize =
591 (request_nseg + reply_nseg) * sizeof(struct ulp_bde64);
592 if (phba->sli_rev == LPFC_SLI_REV4)
593 cmdiocbq->iocb.ulpContext = phba->sli4_hba.rpi_ids[rpi];
595 cmdiocbq->iocb.ulpContext = rpi;
596 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
597 cmdiocbq->context1 = NULL;
598 cmdiocbq->context2 = NULL;
600 cmdiocbq->iocb_cmpl = lpfc_bsg_rport_els_cmp;
601 cmdiocbq->context1 = dd_data;
602 cmdiocbq->context_un.ndlp = ndlp;
603 cmdiocbq->context2 = rspiocbq;
604 dd_data->type = TYPE_IOCB;
605 dd_data->context_un.iocb.cmdiocbq = cmdiocbq;
606 dd_data->context_un.iocb.rspiocbq = rspiocbq;
607 dd_data->context_un.iocb.set_job = job;
608 dd_data->context_un.iocb.bmp = NULL;
609 dd_data->context_un.iocb.ndlp = ndlp;
611 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
612 if (lpfc_readl(phba->HCregaddr, &creg_val)) {
616 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
617 writel(creg_val, phba->HCregaddr);
618 readl(phba->HCregaddr); /* flush */
620 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq, 0);
622 if (rc == IOCB_SUCCESS)
623 return 0; /* done for now */
624 else if (rc == IOCB_BUSY)
630 pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
631 job->request_payload.sg_cnt, DMA_TO_DEVICE);
632 pci_unmap_sg(phba->pcidev, job->reply_payload.sg_list,
633 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
635 lpfc_mbuf_free(phba, pbuflist->virt, pbuflist->phys);
637 lpfc_sli_release_iocbq(phba, cmdiocbq);
640 lpfc_sli_release_iocbq(phba, rspiocbq);
646 /* make error code available to userspace */
647 job->reply->result = rc;
653 * lpfc_bsg_event_free - frees an allocated event structure
654 * @kref: Pointer to a kref.
656 * Called from kref_put. Back cast the kref into an event structure address.
657 * Free any events to get, delete associated nodes, free any events to see,
658 * free any data then free the event itself.
661 lpfc_bsg_event_free(struct kref *kref)
663 struct lpfc_bsg_event *evt = container_of(kref, struct lpfc_bsg_event,
665 struct event_data *ed;
667 list_del(&evt->node);
669 while (!list_empty(&evt->events_to_get)) {
670 ed = list_entry(evt->events_to_get.next, typeof(*ed), node);
676 while (!list_empty(&evt->events_to_see)) {
677 ed = list_entry(evt->events_to_see.next, typeof(*ed), node);
687 * lpfc_bsg_event_ref - increments the kref for an event
688 * @evt: Pointer to an event structure.
691 lpfc_bsg_event_ref(struct lpfc_bsg_event *evt)
693 kref_get(&evt->kref);
697 * lpfc_bsg_event_unref - Uses kref_put to free an event structure
698 * @evt: Pointer to an event structure.
701 lpfc_bsg_event_unref(struct lpfc_bsg_event *evt)
703 kref_put(&evt->kref, lpfc_bsg_event_free);
707 * lpfc_bsg_event_new - allocate and initialize a event structure
708 * @ev_mask: Mask of events.
709 * @ev_reg_id: Event reg id.
710 * @ev_req_id: Event request id.
712 static struct lpfc_bsg_event *
713 lpfc_bsg_event_new(uint32_t ev_mask, int ev_reg_id, uint32_t ev_req_id)
715 struct lpfc_bsg_event *evt = kzalloc(sizeof(*evt), GFP_KERNEL);
720 INIT_LIST_HEAD(&evt->events_to_get);
721 INIT_LIST_HEAD(&evt->events_to_see);
722 evt->type_mask = ev_mask;
723 evt->req_id = ev_req_id;
724 evt->reg_id = ev_reg_id;
725 evt->wait_time_stamp = jiffies;
726 init_waitqueue_head(&evt->wq);
727 kref_init(&evt->kref);
732 * diag_cmd_data_free - Frees an lpfc dma buffer extension
733 * @phba: Pointer to HBA context object.
734 * @mlist: Pointer to an lpfc dma buffer extension.
737 diag_cmd_data_free(struct lpfc_hba *phba, struct lpfc_dmabufext *mlist)
739 struct lpfc_dmabufext *mlast;
740 struct pci_dev *pcidev;
741 struct list_head head, *curr, *next;
743 if ((!mlist) || (!lpfc_is_link_up(phba) &&
744 (phba->link_flag & LS_LOOPBACK_MODE))) {
748 pcidev = phba->pcidev;
749 list_add_tail(&head, &mlist->dma.list);
751 list_for_each_safe(curr, next, &head) {
752 mlast = list_entry(curr, struct lpfc_dmabufext , dma.list);
754 dma_free_coherent(&pcidev->dev,
764 * lpfc_bsg_ct_unsol_event - process an unsolicited CT command
769 * This function is called when an unsolicited CT command is received. It
770 * forwards the event to any processes registered to receive CT events.
773 lpfc_bsg_ct_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
774 struct lpfc_iocbq *piocbq)
776 uint32_t evt_req_id = 0;
779 struct lpfc_dmabuf *dmabuf = NULL;
780 struct lpfc_bsg_event *evt;
781 struct event_data *evt_dat = NULL;
782 struct lpfc_iocbq *iocbq;
784 struct list_head head;
785 struct ulp_bde64 *bde;
788 struct lpfc_dmabuf *bdeBuf1 = piocbq->context2;
789 struct lpfc_dmabuf *bdeBuf2 = piocbq->context3;
790 struct lpfc_hbq_entry *hbqe;
791 struct lpfc_sli_ct_request *ct_req;
792 struct fc_bsg_job *job = NULL;
796 INIT_LIST_HEAD(&head);
797 list_add_tail(&head, &piocbq->list);
799 if (piocbq->iocb.ulpBdeCount == 0 ||
800 piocbq->iocb.un.cont64[0].tus.f.bdeSize == 0)
801 goto error_ct_unsol_exit;
803 if (phba->link_state == LPFC_HBA_ERROR ||
804 (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)))
805 goto error_ct_unsol_exit;
807 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
810 dma_addr = getPaddr(piocbq->iocb.un.cont64[0].addrHigh,
811 piocbq->iocb.un.cont64[0].addrLow);
812 dmabuf = lpfc_sli_ringpostbuf_get(phba, pring, dma_addr);
815 goto error_ct_unsol_exit;
816 ct_req = (struct lpfc_sli_ct_request *)dmabuf->virt;
817 evt_req_id = ct_req->FsType;
818 cmd = ct_req->CommandResponse.bits.CmdRsp;
819 len = ct_req->CommandResponse.bits.Size;
820 if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
821 lpfc_sli_ringpostbuf_put(phba, pring, dmabuf);
823 spin_lock_irqsave(&phba->ct_ev_lock, flags);
824 list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
825 if (!(evt->type_mask & FC_REG_CT_EVENT) ||
826 evt->req_id != evt_req_id)
829 lpfc_bsg_event_ref(evt);
830 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
831 evt_dat = kzalloc(sizeof(*evt_dat), GFP_KERNEL);
832 if (evt_dat == NULL) {
833 spin_lock_irqsave(&phba->ct_ev_lock, flags);
834 lpfc_bsg_event_unref(evt);
835 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
836 "2614 Memory allocation failed for "
841 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
842 /* take accumulated byte count from the last iocbq */
843 iocbq = list_entry(head.prev, typeof(*iocbq), list);
844 evt_dat->len = iocbq->iocb.unsli3.rcvsli3.acc_len;
846 list_for_each_entry(iocbq, &head, list) {
847 for (i = 0; i < iocbq->iocb.ulpBdeCount; i++)
849 iocbq->iocb.un.cont64[i].tus.f.bdeSize;
853 evt_dat->data = kzalloc(evt_dat->len, GFP_KERNEL);
854 if (evt_dat->data == NULL) {
855 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
856 "2615 Memory allocation failed for "
857 "CT event data, size %d\n",
860 spin_lock_irqsave(&phba->ct_ev_lock, flags);
861 lpfc_bsg_event_unref(evt);
862 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
863 goto error_ct_unsol_exit;
866 list_for_each_entry(iocbq, &head, list) {
868 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
869 bdeBuf1 = iocbq->context2;
870 bdeBuf2 = iocbq->context3;
872 for (i = 0; i < iocbq->iocb.ulpBdeCount; i++) {
873 if (phba->sli3_options &
874 LPFC_SLI3_HBQ_ENABLED) {
876 hbqe = (struct lpfc_hbq_entry *)
877 &iocbq->iocb.un.ulpWord[0];
878 size = hbqe->bde.tus.f.bdeSize;
881 hbqe = (struct lpfc_hbq_entry *)
884 size = hbqe->bde.tus.f.bdeSize;
887 if ((offset + size) > evt_dat->len)
888 size = evt_dat->len - offset;
890 size = iocbq->iocb.un.cont64[i].
892 bde = &iocbq->iocb.un.cont64[i];
893 dma_addr = getPaddr(bde->addrHigh,
895 dmabuf = lpfc_sli_ringpostbuf_get(phba,
899 lpfc_printf_log(phba, KERN_ERR,
900 LOG_LIBDFC, "2616 No dmabuf "
901 "found for iocbq 0x%p\n",
903 kfree(evt_dat->data);
905 spin_lock_irqsave(&phba->ct_ev_lock,
907 lpfc_bsg_event_unref(evt);
908 spin_unlock_irqrestore(
909 &phba->ct_ev_lock, flags);
910 goto error_ct_unsol_exit;
912 memcpy((char *)(evt_dat->data) + offset,
915 if (evt_req_id != SLI_CT_ELX_LOOPBACK &&
916 !(phba->sli3_options &
917 LPFC_SLI3_HBQ_ENABLED)) {
918 lpfc_sli_ringpostbuf_put(phba, pring,
922 case ELX_LOOPBACK_DATA:
925 diag_cmd_data_free(phba,
926 (struct lpfc_dmabufext
929 case ELX_LOOPBACK_XRI_SETUP:
930 if ((phba->sli_rev ==
932 (phba->sli3_options &
933 LPFC_SLI3_HBQ_ENABLED
935 lpfc_in_buf_free(phba,
938 lpfc_post_buffer(phba,
944 if (!(phba->sli3_options &
945 LPFC_SLI3_HBQ_ENABLED))
946 lpfc_post_buffer(phba,
955 spin_lock_irqsave(&phba->ct_ev_lock, flags);
956 if (phba->sli_rev == LPFC_SLI_REV4) {
957 evt_dat->immed_dat = phba->ctx_idx;
958 phba->ctx_idx = (phba->ctx_idx + 1) % 64;
959 /* Provide warning for over-run of the ct_ctx array */
960 if (phba->ct_ctx[evt_dat->immed_dat].flags &
962 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
963 "2717 CT context array entry "
964 "[%d] over-run: oxid:x%x, "
965 "sid:x%x\n", phba->ctx_idx,
967 evt_dat->immed_dat].oxid,
969 evt_dat->immed_dat].SID);
970 phba->ct_ctx[evt_dat->immed_dat].rxid =
971 piocbq->iocb.ulpContext;
972 phba->ct_ctx[evt_dat->immed_dat].oxid =
973 piocbq->iocb.unsli3.rcvsli3.ox_id;
974 phba->ct_ctx[evt_dat->immed_dat].SID =
975 piocbq->iocb.un.rcvels.remoteID;
976 phba->ct_ctx[evt_dat->immed_dat].flags = UNSOL_VALID;
978 evt_dat->immed_dat = piocbq->iocb.ulpContext;
980 evt_dat->type = FC_REG_CT_EVENT;
981 list_add(&evt_dat->node, &evt->events_to_see);
982 if (evt_req_id == SLI_CT_ELX_LOOPBACK) {
983 wake_up_interruptible(&evt->wq);
984 lpfc_bsg_event_unref(evt);
988 list_move(evt->events_to_see.prev, &evt->events_to_get);
989 lpfc_bsg_event_unref(evt);
994 job->reply->reply_payload_rcv_len = size;
995 /* make error code available to userspace */
996 job->reply->result = 0;
998 /* complete the job back to userspace */
999 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1001 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1004 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1006 error_ct_unsol_exit:
1007 if (!list_empty(&head))
1009 if ((phba->sli_rev < LPFC_SLI_REV4) &&
1010 (evt_req_id == SLI_CT_ELX_LOOPBACK))
1016 * lpfc_bsg_hba_set_event - process a SET_EVENT bsg vendor command
1017 * @job: SET_EVENT fc_bsg_job
1020 lpfc_bsg_hba_set_event(struct fc_bsg_job *job)
1022 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
1023 struct lpfc_hba *phba = vport->phba;
1024 struct set_ct_event *event_req;
1025 struct lpfc_bsg_event *evt;
1027 struct bsg_job_data *dd_data = NULL;
1029 unsigned long flags;
1031 if (job->request_len <
1032 sizeof(struct fc_bsg_request) + sizeof(struct set_ct_event)) {
1033 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1034 "2612 Received SET_CT_EVENT below minimum "
1040 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
1041 if (dd_data == NULL) {
1042 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1043 "2734 Failed allocation of dd_data\n");
1048 event_req = (struct set_ct_event *)
1049 job->request->rqst_data.h_vendor.vendor_cmd;
1050 ev_mask = ((uint32_t)(unsigned long)event_req->type_mask &
1052 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1053 list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
1054 if (evt->reg_id == event_req->ev_reg_id) {
1055 lpfc_bsg_event_ref(evt);
1056 evt->wait_time_stamp = jiffies;
1060 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1062 if (&evt->node == &phba->ct_ev_waiters) {
1063 /* no event waiting struct yet - first call */
1064 evt = lpfc_bsg_event_new(ev_mask, event_req->ev_reg_id,
1065 event_req->ev_req_id);
1067 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1068 "2617 Failed allocation of event "
1074 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1075 list_add(&evt->node, &phba->ct_ev_waiters);
1076 lpfc_bsg_event_ref(evt);
1077 evt->wait_time_stamp = jiffies;
1078 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1081 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1083 dd_data->type = TYPE_EVT;
1084 dd_data->context_un.evt = evt;
1085 evt->set_job = job; /* for unsolicited command */
1086 job->dd_data = dd_data; /* for fc transport timeout callback*/
1087 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1088 return 0; /* call job done later */
1091 if (dd_data != NULL)
1094 job->dd_data = NULL;
1099 * lpfc_bsg_hba_get_event - process a GET_EVENT bsg vendor command
1100 * @job: GET_EVENT fc_bsg_job
1103 lpfc_bsg_hba_get_event(struct fc_bsg_job *job)
1105 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
1106 struct lpfc_hba *phba = vport->phba;
1107 struct get_ct_event *event_req;
1108 struct get_ct_event_reply *event_reply;
1109 struct lpfc_bsg_event *evt;
1110 struct event_data *evt_dat = NULL;
1111 unsigned long flags;
1114 if (job->request_len <
1115 sizeof(struct fc_bsg_request) + sizeof(struct get_ct_event)) {
1116 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1117 "2613 Received GET_CT_EVENT request below "
1123 event_req = (struct get_ct_event *)
1124 job->request->rqst_data.h_vendor.vendor_cmd;
1126 event_reply = (struct get_ct_event_reply *)
1127 job->reply->reply_data.vendor_reply.vendor_rsp;
1128 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1129 list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
1130 if (evt->reg_id == event_req->ev_reg_id) {
1131 if (list_empty(&evt->events_to_get))
1133 lpfc_bsg_event_ref(evt);
1134 evt->wait_time_stamp = jiffies;
1135 evt_dat = list_entry(evt->events_to_get.prev,
1136 struct event_data, node);
1137 list_del(&evt_dat->node);
1141 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1143 /* The app may continue to ask for event data until it gets
1144 * an error indicating that there isn't anymore
1146 if (evt_dat == NULL) {
1147 job->reply->reply_payload_rcv_len = 0;
1152 if (evt_dat->len > job->request_payload.payload_len) {
1153 evt_dat->len = job->request_payload.payload_len;
1154 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1155 "2618 Truncated event data at %d "
1157 job->request_payload.payload_len);
1160 event_reply->type = evt_dat->type;
1161 event_reply->immed_data = evt_dat->immed_dat;
1162 if (evt_dat->len > 0)
1163 job->reply->reply_payload_rcv_len =
1164 sg_copy_from_buffer(job->request_payload.sg_list,
1165 job->request_payload.sg_cnt,
1166 evt_dat->data, evt_dat->len);
1168 job->reply->reply_payload_rcv_len = 0;
1171 kfree(evt_dat->data);
1175 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1176 lpfc_bsg_event_unref(evt);
1177 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1178 job->dd_data = NULL;
1179 job->reply->result = 0;
1184 job->dd_data = NULL;
1185 job->reply->result = rc;
1190 * lpfc_issue_ct_rsp_cmp - lpfc_issue_ct_rsp's completion handler
1191 * @phba: Pointer to HBA context object.
1192 * @cmdiocbq: Pointer to command iocb.
1193 * @rspiocbq: Pointer to response iocb.
1195 * This function is the completion handler for iocbs issued using
1196 * lpfc_issue_ct_rsp_cmp function. This function is called by the
1197 * ring event handler function without any lock held. This function
1198 * can be called from both worker thread context and interrupt
1199 * context. This function also can be called from other thread which
1200 * cleans up the SLI layer objects.
1201 * This function copy the contents of the response iocb to the
1202 * response iocb memory object provided by the caller of
1203 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
1204 * sleeps for the iocb completion.
1207 lpfc_issue_ct_rsp_cmp(struct lpfc_hba *phba,
1208 struct lpfc_iocbq *cmdiocbq,
1209 struct lpfc_iocbq *rspiocbq)
1211 struct bsg_job_data *dd_data;
1212 struct fc_bsg_job *job;
1214 struct lpfc_dmabuf *bmp;
1215 struct lpfc_nodelist *ndlp;
1216 unsigned long flags;
1219 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1220 dd_data = cmdiocbq->context2;
1221 /* normal completion and timeout crossed paths, already done */
1223 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1227 job = dd_data->context_un.iocb.set_job;
1228 bmp = dd_data->context_un.iocb.bmp;
1229 rsp = &rspiocbq->iocb;
1230 ndlp = dd_data->context_un.iocb.ndlp;
1232 pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
1233 job->request_payload.sg_cnt, DMA_TO_DEVICE);
1235 if (rsp->ulpStatus) {
1236 if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
1237 switch (rsp->un.ulpWord[4] & IOERR_PARAM_MASK) {
1238 case IOERR_SEQUENCE_TIMEOUT:
1241 case IOERR_INVALID_RPI:
1251 job->reply->reply_payload_rcv_len =
1252 rsp->un.genreq64.bdl.bdeSize;
1254 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
1255 lpfc_sli_release_iocbq(phba, cmdiocbq);
1259 /* make error code available to userspace */
1260 job->reply->result = rc;
1261 job->dd_data = NULL;
1262 /* complete the job back to userspace */
1264 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1269 * lpfc_issue_ct_rsp - issue a ct response
1270 * @phba: Pointer to HBA context object.
1271 * @job: Pointer to the job object.
1272 * @tag: tag index value into the ports context exchange array.
1273 * @bmp: Pointer to a dma buffer descriptor.
1274 * @num_entry: Number of enties in the bde.
1277 lpfc_issue_ct_rsp(struct lpfc_hba *phba, struct fc_bsg_job *job, uint32_t tag,
1278 struct lpfc_dmabuf *bmp, int num_entry)
1281 struct lpfc_iocbq *ctiocb = NULL;
1283 struct lpfc_nodelist *ndlp = NULL;
1284 struct bsg_job_data *dd_data;
1287 /* allocate our bsg tracking structure */
1288 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
1290 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1291 "2736 Failed allocation of dd_data\n");
1296 /* Allocate buffer for command iocb */
1297 ctiocb = lpfc_sli_get_iocbq(phba);
1303 icmd = &ctiocb->iocb;
1304 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
1305 icmd->un.xseq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
1306 icmd->un.xseq64.bdl.addrLow = putPaddrLow(bmp->phys);
1307 icmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
1308 icmd->un.xseq64.bdl.bdeSize = (num_entry * sizeof(struct ulp_bde64));
1309 icmd->un.xseq64.w5.hcsw.Fctl = (LS | LA);
1310 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
1311 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_SOL_CTL;
1312 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
1314 /* Fill in rest of iocb */
1315 icmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;
1316 icmd->ulpBdeCount = 1;
1318 icmd->ulpClass = CLASS3;
1319 if (phba->sli_rev == LPFC_SLI_REV4) {
1320 /* Do not issue unsol response if oxid not marked as valid */
1321 if (!(phba->ct_ctx[tag].flags & UNSOL_VALID)) {
1323 goto issue_ct_rsp_exit;
1325 icmd->ulpContext = phba->ct_ctx[tag].rxid;
1326 icmd->unsli3.rcvsli3.ox_id = phba->ct_ctx[tag].oxid;
1327 ndlp = lpfc_findnode_did(phba->pport, phba->ct_ctx[tag].SID);
1329 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
1330 "2721 ndlp null for oxid %x SID %x\n",
1332 phba->ct_ctx[tag].SID);
1334 goto issue_ct_rsp_exit;
1337 /* Check if the ndlp is active */
1338 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
1340 goto issue_ct_rsp_exit;
1343 /* get a refernece count so the ndlp doesn't go away while
1346 if (!lpfc_nlp_get(ndlp)) {
1348 goto issue_ct_rsp_exit;
1351 icmd->un.ulpWord[3] =
1352 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
1354 /* The exchange is done, mark the entry as invalid */
1355 phba->ct_ctx[tag].flags &= ~UNSOL_VALID;
1357 icmd->ulpContext = (ushort) tag;
1359 icmd->ulpTimeout = phba->fc_ratov * 2;
1361 /* Xmit CT response on exchange <xid> */
1362 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
1363 "2722 Xmit CT response on exchange x%x Data: x%x x%x x%x\n",
1364 icmd->ulpContext, icmd->ulpIoTag, tag, phba->link_state);
1366 ctiocb->iocb_cmpl = NULL;
1367 ctiocb->iocb_flag |= LPFC_IO_LIBDFC;
1368 ctiocb->vport = phba->pport;
1369 ctiocb->context3 = bmp;
1371 ctiocb->iocb_cmpl = lpfc_issue_ct_rsp_cmp;
1372 ctiocb->context2 = dd_data;
1373 ctiocb->context1 = ndlp;
1374 dd_data->type = TYPE_IOCB;
1375 dd_data->context_un.iocb.cmdiocbq = ctiocb;
1376 dd_data->context_un.iocb.rspiocbq = NULL;
1377 dd_data->context_un.iocb.set_job = job;
1378 dd_data->context_un.iocb.bmp = bmp;
1379 dd_data->context_un.iocb.ndlp = ndlp;
1381 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
1382 if (lpfc_readl(phba->HCregaddr, &creg_val)) {
1384 goto issue_ct_rsp_exit;
1386 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
1387 writel(creg_val, phba->HCregaddr);
1388 readl(phba->HCregaddr); /* flush */
1391 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
1393 if (rc == IOCB_SUCCESS)
1394 return 0; /* done for now */
1397 lpfc_sli_release_iocbq(phba, ctiocb);
1405 * lpfc_bsg_send_mgmt_rsp - process a SEND_MGMT_RESP bsg vendor command
1406 * @job: SEND_MGMT_RESP fc_bsg_job
1409 lpfc_bsg_send_mgmt_rsp(struct fc_bsg_job *job)
1411 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
1412 struct lpfc_hba *phba = vport->phba;
1413 struct send_mgmt_resp *mgmt_resp = (struct send_mgmt_resp *)
1414 job->request->rqst_data.h_vendor.vendor_cmd;
1415 struct ulp_bde64 *bpl;
1416 struct lpfc_dmabuf *bmp = NULL;
1417 struct scatterlist *sgel = NULL;
1421 uint32_t tag = mgmt_resp->tag;
1422 unsigned long reqbfrcnt =
1423 (unsigned long)job->request_payload.payload_len;
1426 /* in case no data is transferred */
1427 job->reply->reply_payload_rcv_len = 0;
1429 if (!reqbfrcnt || (reqbfrcnt > (80 * BUF_SZ_4K))) {
1431 goto send_mgmt_rsp_exit;
1434 bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
1437 goto send_mgmt_rsp_exit;
1440 bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
1443 goto send_mgmt_rsp_free_bmp;
1446 INIT_LIST_HEAD(&bmp->list);
1447 bpl = (struct ulp_bde64 *) bmp->virt;
1448 request_nseg = pci_map_sg(phba->pcidev, job->request_payload.sg_list,
1449 job->request_payload.sg_cnt, DMA_TO_DEVICE);
1450 for_each_sg(job->request_payload.sg_list, sgel, request_nseg, numbde) {
1451 busaddr = sg_dma_address(sgel);
1452 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1453 bpl->tus.f.bdeSize = sg_dma_len(sgel);
1454 bpl->tus.w = cpu_to_le32(bpl->tus.w);
1455 bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
1456 bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
1460 rc = lpfc_issue_ct_rsp(phba, job, tag, bmp, request_nseg);
1462 if (rc == IOCB_SUCCESS)
1463 return 0; /* done for now */
1465 /* TBD need to handle a timeout */
1466 pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
1467 job->request_payload.sg_cnt, DMA_TO_DEVICE);
1469 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
1471 send_mgmt_rsp_free_bmp:
1474 /* make error code available to userspace */
1475 job->reply->result = rc;
1476 job->dd_data = NULL;
1481 * lpfc_bsg_diag_mode_enter - process preparing into device diag loopback mode
1482 * @phba: Pointer to HBA context object.
1484 * This function is responsible for preparing driver for diag loopback
1488 lpfc_bsg_diag_mode_enter(struct lpfc_hba *phba)
1490 struct lpfc_vport **vports;
1491 struct Scsi_Host *shost;
1492 struct lpfc_sli *psli;
1493 struct lpfc_sli_ring *pring;
1500 pring = &psli->ring[LPFC_FCP_RING];
1504 if ((phba->link_state == LPFC_HBA_ERROR) ||
1505 (psli->sli_flag & LPFC_BLOCK_MGMT_IO) ||
1506 (!(psli->sli_flag & LPFC_SLI_ACTIVE)))
1509 vports = lpfc_create_vport_work_array(phba);
1511 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
1512 shost = lpfc_shost_from_vport(vports[i]);
1513 scsi_block_requests(shost);
1515 lpfc_destroy_vport_work_array(phba, vports);
1517 shost = lpfc_shost_from_vport(phba->pport);
1518 scsi_block_requests(shost);
1521 while (pring->txcmplq_cnt) {
1522 if (i++ > 500) /* wait up to 5 seconds */
1530 * lpfc_bsg_diag_mode_exit - exit process from device diag loopback mode
1531 * @phba: Pointer to HBA context object.
1533 * This function is responsible for driver exit processing of setting up
1534 * diag loopback mode on device.
1537 lpfc_bsg_diag_mode_exit(struct lpfc_hba *phba)
1539 struct Scsi_Host *shost;
1540 struct lpfc_vport **vports;
1543 vports = lpfc_create_vport_work_array(phba);
1545 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
1546 shost = lpfc_shost_from_vport(vports[i]);
1547 scsi_unblock_requests(shost);
1549 lpfc_destroy_vport_work_array(phba, vports);
1551 shost = lpfc_shost_from_vport(phba->pport);
1552 scsi_unblock_requests(shost);
1558 * lpfc_sli3_bsg_diag_loopback_mode - process an sli3 bsg vendor command
1559 * @phba: Pointer to HBA context object.
1560 * @job: LPFC_BSG_VENDOR_DIAG_MODE
1562 * This function is responsible for placing an sli3 port into diagnostic
1563 * loopback mode in order to perform a diagnostic loopback test.
1564 * All new scsi requests are blocked, a small delay is used to allow the
1565 * scsi requests to complete then the link is brought down. If the link is
1566 * is placed in loopback mode then scsi requests are again allowed
1567 * so the scsi mid-layer doesn't give up on the port.
1568 * All of this is done in-line.
1571 lpfc_sli3_bsg_diag_loopback_mode(struct lpfc_hba *phba, struct fc_bsg_job *job)
1573 struct diag_mode_set *loopback_mode;
1574 uint32_t link_flags;
1576 LPFC_MBOXQ_t *pmboxq = NULL;
1577 int mbxstatus = MBX_SUCCESS;
1581 /* no data to return just the return code */
1582 job->reply->reply_payload_rcv_len = 0;
1584 if (job->request_len < sizeof(struct fc_bsg_request) +
1585 sizeof(struct diag_mode_set)) {
1586 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1587 "2738 Received DIAG MODE request size:%d "
1588 "below the minimum size:%d\n",
1590 (int)(sizeof(struct fc_bsg_request) +
1591 sizeof(struct diag_mode_set)));
1596 rc = lpfc_bsg_diag_mode_enter(phba);
1600 /* bring the link to diagnostic mode */
1601 loopback_mode = (struct diag_mode_set *)
1602 job->request->rqst_data.h_vendor.vendor_cmd;
1603 link_flags = loopback_mode->type;
1604 timeout = loopback_mode->timeout * 100;
1606 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1609 goto loopback_mode_exit;
1611 memset((void *)pmboxq, 0, sizeof(LPFC_MBOXQ_t));
1612 pmboxq->u.mb.mbxCommand = MBX_DOWN_LINK;
1613 pmboxq->u.mb.mbxOwner = OWN_HOST;
1615 mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
1617 if ((mbxstatus == MBX_SUCCESS) && (pmboxq->u.mb.mbxStatus == 0)) {
1618 /* wait for link down before proceeding */
1620 while (phba->link_state != LPFC_LINK_DOWN) {
1621 if (i++ > timeout) {
1623 goto loopback_mode_exit;
1628 memset((void *)pmboxq, 0, sizeof(LPFC_MBOXQ_t));
1629 if (link_flags == INTERNAL_LOOP_BACK)
1630 pmboxq->u.mb.un.varInitLnk.link_flags = FLAGS_LOCAL_LB;
1632 pmboxq->u.mb.un.varInitLnk.link_flags =
1633 FLAGS_TOPOLOGY_MODE_LOOP;
1635 pmboxq->u.mb.mbxCommand = MBX_INIT_LINK;
1636 pmboxq->u.mb.mbxOwner = OWN_HOST;
1638 mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq,
1641 if ((mbxstatus != MBX_SUCCESS) || (pmboxq->u.mb.mbxStatus))
1644 spin_lock_irq(&phba->hbalock);
1645 phba->link_flag |= LS_LOOPBACK_MODE;
1646 spin_unlock_irq(&phba->hbalock);
1647 /* wait for the link attention interrupt */
1651 while (phba->link_state != LPFC_HBA_READY) {
1652 if (i++ > timeout) {
1665 lpfc_bsg_diag_mode_exit(phba);
1668 * Let SLI layer release mboxq if mbox command completed after timeout.
1670 if (pmboxq && mbxstatus != MBX_TIMEOUT)
1671 mempool_free(pmboxq, phba->mbox_mem_pool);
1674 /* make error code available to userspace */
1675 job->reply->result = rc;
1676 /* complete the job back to userspace if no error */
1683 * lpfc_sli4_bsg_set_link_diag_state - set sli4 link diag state
1684 * @phba: Pointer to HBA context object.
1685 * @diag: Flag for set link to diag or nomral operation state.
1687 * This function is responsible for issuing a sli4 mailbox command for setting
1688 * link to either diag state or normal operation state.
1691 lpfc_sli4_bsg_set_link_diag_state(struct lpfc_hba *phba, uint32_t diag)
1693 LPFC_MBOXQ_t *pmboxq;
1694 struct lpfc_mbx_set_link_diag_state *link_diag_state;
1695 uint32_t req_len, alloc_len;
1696 int mbxstatus = MBX_SUCCESS, rc;
1698 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1702 req_len = (sizeof(struct lpfc_mbx_set_link_diag_state) -
1703 sizeof(struct lpfc_sli4_cfg_mhdr));
1704 alloc_len = lpfc_sli4_config(phba, pmboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
1705 LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_STATE,
1706 req_len, LPFC_SLI4_MBX_EMBED);
1707 if (alloc_len != req_len) {
1709 goto link_diag_state_set_out;
1711 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
1712 "3128 Set link to diagnostic state:x%x (x%x/x%x)\n",
1713 diag, phba->sli4_hba.lnk_info.lnk_tp,
1714 phba->sli4_hba.lnk_info.lnk_no);
1716 link_diag_state = &pmboxq->u.mqe.un.link_diag_state;
1717 bf_set(lpfc_mbx_set_diag_state_diag_bit_valid, &link_diag_state->u.req,
1718 LPFC_DIAG_STATE_DIAG_BIT_VALID_CHANGE);
1719 bf_set(lpfc_mbx_set_diag_state_link_num, &link_diag_state->u.req,
1720 phba->sli4_hba.lnk_info.lnk_no);
1721 bf_set(lpfc_mbx_set_diag_state_link_type, &link_diag_state->u.req,
1722 phba->sli4_hba.lnk_info.lnk_tp);
1724 bf_set(lpfc_mbx_set_diag_state_diag,
1725 &link_diag_state->u.req, 1);
1727 bf_set(lpfc_mbx_set_diag_state_diag,
1728 &link_diag_state->u.req, 0);
1730 mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
1732 if ((mbxstatus == MBX_SUCCESS) && (pmboxq->u.mb.mbxStatus == 0))
1737 link_diag_state_set_out:
1738 if (pmboxq && (mbxstatus != MBX_TIMEOUT))
1739 mempool_free(pmboxq, phba->mbox_mem_pool);
1745 * lpfc_sli4_bsg_set_internal_loopback - set sli4 internal loopback diagnostic
1746 * @phba: Pointer to HBA context object.
1748 * This function is responsible for issuing a sli4 mailbox command for setting
1749 * up internal loopback diagnostic.
1752 lpfc_sli4_bsg_set_internal_loopback(struct lpfc_hba *phba)
1754 LPFC_MBOXQ_t *pmboxq;
1755 uint32_t req_len, alloc_len;
1756 struct lpfc_mbx_set_link_diag_loopback *link_diag_loopback;
1757 int mbxstatus = MBX_SUCCESS, rc = 0;
1759 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1762 req_len = (sizeof(struct lpfc_mbx_set_link_diag_loopback) -
1763 sizeof(struct lpfc_sli4_cfg_mhdr));
1764 alloc_len = lpfc_sli4_config(phba, pmboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
1765 LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_LOOPBACK,
1766 req_len, LPFC_SLI4_MBX_EMBED);
1767 if (alloc_len != req_len) {
1768 mempool_free(pmboxq, phba->mbox_mem_pool);
1771 link_diag_loopback = &pmboxq->u.mqe.un.link_diag_loopback;
1772 bf_set(lpfc_mbx_set_diag_state_link_num,
1773 &link_diag_loopback->u.req, phba->sli4_hba.lnk_info.lnk_no);
1774 bf_set(lpfc_mbx_set_diag_state_link_type,
1775 &link_diag_loopback->u.req, phba->sli4_hba.lnk_info.lnk_tp);
1776 bf_set(lpfc_mbx_set_diag_lpbk_type, &link_diag_loopback->u.req,
1777 LPFC_DIAG_LOOPBACK_TYPE_INTERNAL);
1779 mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
1780 if ((mbxstatus != MBX_SUCCESS) || (pmboxq->u.mb.mbxStatus)) {
1781 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1782 "3127 Failed setup loopback mode mailbox "
1783 "command, rc:x%x, status:x%x\n", mbxstatus,
1784 pmboxq->u.mb.mbxStatus);
1787 if (pmboxq && (mbxstatus != MBX_TIMEOUT))
1788 mempool_free(pmboxq, phba->mbox_mem_pool);
1793 * lpfc_sli4_diag_fcport_reg_setup - setup port registrations for diagnostic
1794 * @phba: Pointer to HBA context object.
1796 * This function set up SLI4 FC port registrations for diagnostic run, which
1797 * includes all the rpis, vfi, and also vpi.
1800 lpfc_sli4_diag_fcport_reg_setup(struct lpfc_hba *phba)
1804 if (phba->pport->fc_flag & FC_VFI_REGISTERED) {
1805 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1806 "3136 Port still had vfi registered: "
1807 "mydid:x%x, fcfi:%d, vfi:%d, vpi:%d\n",
1808 phba->pport->fc_myDID, phba->fcf.fcfi,
1809 phba->sli4_hba.vfi_ids[phba->pport->vfi],
1810 phba->vpi_ids[phba->pport->vpi]);
1813 rc = lpfc_issue_reg_vfi(phba->pport);
1818 * lpfc_sli4_bsg_diag_loopback_mode - process an sli4 bsg vendor command
1819 * @phba: Pointer to HBA context object.
1820 * @job: LPFC_BSG_VENDOR_DIAG_MODE
1822 * This function is responsible for placing an sli4 port into diagnostic
1823 * loopback mode in order to perform a diagnostic loopback test.
1826 lpfc_sli4_bsg_diag_loopback_mode(struct lpfc_hba *phba, struct fc_bsg_job *job)
1828 struct diag_mode_set *loopback_mode;
1829 uint32_t link_flags, timeout;
1832 /* no data to return just the return code */
1833 job->reply->reply_payload_rcv_len = 0;
1835 if (job->request_len < sizeof(struct fc_bsg_request) +
1836 sizeof(struct diag_mode_set)) {
1837 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1838 "3011 Received DIAG MODE request size:%d "
1839 "below the minimum size:%d\n",
1841 (int)(sizeof(struct fc_bsg_request) +
1842 sizeof(struct diag_mode_set)));
1847 rc = lpfc_bsg_diag_mode_enter(phba);
1851 /* indicate we are in loobpack diagnostic mode */
1852 spin_lock_irq(&phba->hbalock);
1853 phba->link_flag |= LS_LOOPBACK_MODE;
1854 spin_unlock_irq(&phba->hbalock);
1856 /* reset port to start frome scratch */
1857 rc = lpfc_selective_reset(phba);
1861 /* bring the link to diagnostic mode */
1862 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
1863 "3129 Bring link to diagnostic state.\n");
1864 loopback_mode = (struct diag_mode_set *)
1865 job->request->rqst_data.h_vendor.vendor_cmd;
1866 link_flags = loopback_mode->type;
1867 timeout = loopback_mode->timeout * 100;
1869 rc = lpfc_sli4_bsg_set_link_diag_state(phba, 1);
1871 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1872 "3130 Failed to bring link to diagnostic "
1873 "state, rc:x%x\n", rc);
1874 goto loopback_mode_exit;
1877 /* wait for link down before proceeding */
1879 while (phba->link_state != LPFC_LINK_DOWN) {
1880 if (i++ > timeout) {
1882 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
1883 "3131 Timeout waiting for link to "
1884 "diagnostic mode, timeout:%d ms\n",
1886 goto loopback_mode_exit;
1891 /* set up loopback mode */
1892 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
1893 "3132 Set up loopback mode:x%x\n", link_flags);
1895 if (link_flags == INTERNAL_LOOP_BACK)
1896 rc = lpfc_sli4_bsg_set_internal_loopback(phba);
1897 else if (link_flags == EXTERNAL_LOOP_BACK)
1898 rc = lpfc_hba_init_link_fc_topology(phba,
1899 FLAGS_TOPOLOGY_MODE_PT_PT,
1903 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
1904 "3141 Loopback mode:x%x not supported\n",
1906 goto loopback_mode_exit;
1910 /* wait for the link attention interrupt */
1913 while (phba->link_state < LPFC_LINK_UP) {
1914 if (i++ > timeout) {
1916 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
1917 "3137 Timeout waiting for link up "
1918 "in loopback mode, timeout:%d ms\n",
1926 /* port resource registration setup for loopback diagnostic */
1928 /* set up a none zero myDID for loopback test */
1929 phba->pport->fc_myDID = 1;
1930 rc = lpfc_sli4_diag_fcport_reg_setup(phba);
1932 goto loopback_mode_exit;
1935 /* wait for the port ready */
1938 while (phba->link_state != LPFC_HBA_READY) {
1939 if (i++ > timeout) {
1941 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
1942 "3133 Timeout waiting for port "
1943 "loopback mode ready, timeout:%d ms\n",
1952 /* clear loopback diagnostic mode */
1954 spin_lock_irq(&phba->hbalock);
1955 phba->link_flag &= ~LS_LOOPBACK_MODE;
1956 spin_unlock_irq(&phba->hbalock);
1958 lpfc_bsg_diag_mode_exit(phba);
1961 /* make error code available to userspace */
1962 job->reply->result = rc;
1963 /* complete the job back to userspace if no error */
1970 * lpfc_bsg_diag_loopback_mode - bsg vendor command for diag loopback mode
1971 * @job: LPFC_BSG_VENDOR_DIAG_MODE
1973 * This function is responsible for responding to check and dispatch bsg diag
1974 * command from the user to proper driver action routines.
1977 lpfc_bsg_diag_loopback_mode(struct fc_bsg_job *job)
1979 struct Scsi_Host *shost;
1980 struct lpfc_vport *vport;
1981 struct lpfc_hba *phba;
1987 vport = (struct lpfc_vport *)job->shost->hostdata;
1994 if (phba->sli_rev < LPFC_SLI_REV4)
1995 rc = lpfc_sli3_bsg_diag_loopback_mode(phba, job);
1996 else if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
1997 LPFC_SLI_INTF_IF_TYPE_2)
1998 rc = lpfc_sli4_bsg_diag_loopback_mode(phba, job);
2006 * lpfc_sli4_bsg_diag_mode_end - sli4 bsg vendor command for ending diag mode
2007 * @job: LPFC_BSG_VENDOR_DIAG_MODE_END
2009 * This function is responsible for responding to check and dispatch bsg diag
2010 * command from the user to proper driver action routines.
2013 lpfc_sli4_bsg_diag_mode_end(struct fc_bsg_job *job)
2015 struct Scsi_Host *shost;
2016 struct lpfc_vport *vport;
2017 struct lpfc_hba *phba;
2018 struct diag_mode_set *loopback_mode_end_cmd;
2025 vport = (struct lpfc_vport *)job->shost->hostdata;
2032 if (phba->sli_rev < LPFC_SLI_REV4)
2034 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
2035 LPFC_SLI_INTF_IF_TYPE_2)
2038 /* clear loopback diagnostic mode */
2039 spin_lock_irq(&phba->hbalock);
2040 phba->link_flag &= ~LS_LOOPBACK_MODE;
2041 spin_unlock_irq(&phba->hbalock);
2042 loopback_mode_end_cmd = (struct diag_mode_set *)
2043 job->request->rqst_data.h_vendor.vendor_cmd;
2044 timeout = loopback_mode_end_cmd->timeout * 100;
2046 rc = lpfc_sli4_bsg_set_link_diag_state(phba, 0);
2048 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2049 "3139 Failed to bring link to diagnostic "
2050 "state, rc:x%x\n", rc);
2051 goto loopback_mode_end_exit;
2054 /* wait for link down before proceeding */
2056 while (phba->link_state != LPFC_LINK_DOWN) {
2057 if (i++ > timeout) {
2059 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
2060 "3140 Timeout waiting for link to "
2061 "diagnostic mode_end, timeout:%d ms\n",
2063 /* there is nothing much we can do here */
2069 /* reset port resource registrations */
2070 rc = lpfc_selective_reset(phba);
2071 phba->pport->fc_myDID = 0;
2073 loopback_mode_end_exit:
2074 /* make return code available to userspace */
2075 job->reply->result = rc;
2076 /* complete the job back to userspace if no error */
2083 * lpfc_sli4_bsg_link_diag_test - sli4 bsg vendor command for diag link test
2084 * @job: LPFC_BSG_VENDOR_DIAG_LINK_TEST
2086 * This function is to perform SLI4 diag link test request from the user
2090 lpfc_sli4_bsg_link_diag_test(struct fc_bsg_job *job)
2092 struct Scsi_Host *shost;
2093 struct lpfc_vport *vport;
2094 struct lpfc_hba *phba;
2095 LPFC_MBOXQ_t *pmboxq;
2096 struct sli4_link_diag *link_diag_test_cmd;
2097 uint32_t req_len, alloc_len;
2099 struct lpfc_mbx_run_link_diag_test *run_link_diag_test;
2100 union lpfc_sli4_cfg_shdr *shdr;
2101 uint32_t shdr_status, shdr_add_status;
2102 struct diag_status *diag_status_reply;
2103 int mbxstatus, rc = 0;
2110 vport = (struct lpfc_vport *)job->shost->hostdata;
2121 if (phba->sli_rev < LPFC_SLI_REV4) {
2125 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
2126 LPFC_SLI_INTF_IF_TYPE_2) {
2131 if (job->request_len < sizeof(struct fc_bsg_request) +
2132 sizeof(struct sli4_link_diag)) {
2133 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2134 "3013 Received LINK DIAG TEST request "
2135 " size:%d below the minimum size:%d\n",
2137 (int)(sizeof(struct fc_bsg_request) +
2138 sizeof(struct sli4_link_diag)));
2143 rc = lpfc_bsg_diag_mode_enter(phba);
2147 link_diag_test_cmd = (struct sli4_link_diag *)
2148 job->request->rqst_data.h_vendor.vendor_cmd;
2149 timeout = link_diag_test_cmd->timeout * 100;
2151 rc = lpfc_sli4_bsg_set_link_diag_state(phba, 1);
2156 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2159 goto link_diag_test_exit;
2162 req_len = (sizeof(struct lpfc_mbx_set_link_diag_state) -
2163 sizeof(struct lpfc_sli4_cfg_mhdr));
2164 alloc_len = lpfc_sli4_config(phba, pmboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
2165 LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_STATE,
2166 req_len, LPFC_SLI4_MBX_EMBED);
2167 if (alloc_len != req_len) {
2169 goto link_diag_test_exit;
2171 run_link_diag_test = &pmboxq->u.mqe.un.link_diag_test;
2172 bf_set(lpfc_mbx_run_diag_test_link_num, &run_link_diag_test->u.req,
2173 phba->sli4_hba.lnk_info.lnk_no);
2174 bf_set(lpfc_mbx_run_diag_test_link_type, &run_link_diag_test->u.req,
2175 phba->sli4_hba.lnk_info.lnk_tp);
2176 bf_set(lpfc_mbx_run_diag_test_test_id, &run_link_diag_test->u.req,
2177 link_diag_test_cmd->test_id);
2178 bf_set(lpfc_mbx_run_diag_test_loops, &run_link_diag_test->u.req,
2179 link_diag_test_cmd->loops);
2180 bf_set(lpfc_mbx_run_diag_test_test_ver, &run_link_diag_test->u.req,
2181 link_diag_test_cmd->test_version);
2182 bf_set(lpfc_mbx_run_diag_test_err_act, &run_link_diag_test->u.req,
2183 link_diag_test_cmd->error_action);
2185 mbxstatus = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
2187 shdr = (union lpfc_sli4_cfg_shdr *)
2188 &pmboxq->u.mqe.un.sli4_config.header.cfg_shdr;
2189 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
2190 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
2191 if (shdr_status || shdr_add_status || mbxstatus) {
2192 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
2193 "3010 Run link diag test mailbox failed with "
2194 "mbx_status x%x status x%x, add_status x%x\n",
2195 mbxstatus, shdr_status, shdr_add_status);
2198 diag_status_reply = (struct diag_status *)
2199 job->reply->reply_data.vendor_reply.vendor_rsp;
2201 if (job->reply_len <
2202 sizeof(struct fc_bsg_request) + sizeof(struct diag_status)) {
2203 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2204 "3012 Received Run link diag test reply "
2205 "below minimum size (%d): reply_len:%d\n",
2206 (int)(sizeof(struct fc_bsg_request) +
2207 sizeof(struct diag_status)),
2213 diag_status_reply->mbox_status = mbxstatus;
2214 diag_status_reply->shdr_status = shdr_status;
2215 diag_status_reply->shdr_add_status = shdr_add_status;
2217 link_diag_test_exit:
2218 rc = lpfc_sli4_bsg_set_link_diag_state(phba, 0);
2221 mempool_free(pmboxq, phba->mbox_mem_pool);
2223 lpfc_bsg_diag_mode_exit(phba);
2226 /* make error code available to userspace */
2227 job->reply->result = rc;
2228 /* complete the job back to userspace if no error */
2235 * lpfcdiag_loop_self_reg - obtains a remote port login id
2236 * @phba: Pointer to HBA context object
2237 * @rpi: Pointer to a remote port login id
2239 * This function obtains a remote port login id so the diag loopback test
2240 * can send and receive its own unsolicited CT command.
2242 static int lpfcdiag_loop_self_reg(struct lpfc_hba *phba, uint16_t *rpi)
2245 struct lpfc_dmabuf *dmabuff;
2248 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2252 if (phba->sli_rev < LPFC_SLI_REV4)
2253 status = lpfc_reg_rpi(phba, 0, phba->pport->fc_myDID,
2254 (uint8_t *)&phba->pport->fc_sparam,
2257 *rpi = lpfc_sli4_alloc_rpi(phba);
2258 status = lpfc_reg_rpi(phba, phba->pport->vpi,
2259 phba->pport->fc_myDID,
2260 (uint8_t *)&phba->pport->fc_sparam,
2265 mempool_free(mbox, phba->mbox_mem_pool);
2266 if (phba->sli_rev == LPFC_SLI_REV4)
2267 lpfc_sli4_free_rpi(phba, *rpi);
2271 dmabuff = (struct lpfc_dmabuf *) mbox->context1;
2272 mbox->context1 = NULL;
2273 mbox->context2 = NULL;
2274 status = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
2276 if ((status != MBX_SUCCESS) || (mbox->u.mb.mbxStatus)) {
2277 lpfc_mbuf_free(phba, dmabuff->virt, dmabuff->phys);
2279 if (status != MBX_TIMEOUT)
2280 mempool_free(mbox, phba->mbox_mem_pool);
2281 if (phba->sli_rev == LPFC_SLI_REV4)
2282 lpfc_sli4_free_rpi(phba, *rpi);
2286 if (phba->sli_rev < LPFC_SLI_REV4)
2287 *rpi = mbox->u.mb.un.varWords[0];
2289 lpfc_mbuf_free(phba, dmabuff->virt, dmabuff->phys);
2291 mempool_free(mbox, phba->mbox_mem_pool);
2296 * lpfcdiag_loop_self_unreg - unregs from the rpi
2297 * @phba: Pointer to HBA context object
2298 * @rpi: Remote port login id
2300 * This function unregisters the rpi obtained in lpfcdiag_loop_self_reg
2302 static int lpfcdiag_loop_self_unreg(struct lpfc_hba *phba, uint16_t rpi)
2307 /* Allocate mboxq structure */
2308 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2312 if (phba->sli_rev < LPFC_SLI_REV4)
2313 lpfc_unreg_login(phba, 0, rpi, mbox);
2315 lpfc_unreg_login(phba, phba->pport->vpi,
2316 phba->sli4_hba.rpi_ids[rpi], mbox);
2318 status = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
2320 if ((status != MBX_SUCCESS) || (mbox->u.mb.mbxStatus)) {
2321 if (status != MBX_TIMEOUT)
2322 mempool_free(mbox, phba->mbox_mem_pool);
2325 mempool_free(mbox, phba->mbox_mem_pool);
2326 if (phba->sli_rev == LPFC_SLI_REV4)
2327 lpfc_sli4_free_rpi(phba, rpi);
2332 * lpfcdiag_loop_get_xri - obtains the transmit and receive ids
2333 * @phba: Pointer to HBA context object
2334 * @rpi: Remote port login id
2335 * @txxri: Pointer to transmit exchange id
2336 * @rxxri: Pointer to response exchabge id
2338 * This function obtains the transmit and receive ids required to send
2339 * an unsolicited ct command with a payload. A special lpfc FsType and CmdRsp
2340 * flags are used to the unsolicted response handler is able to process
2341 * the ct command sent on the same port.
2343 static int lpfcdiag_loop_get_xri(struct lpfc_hba *phba, uint16_t rpi,
2344 uint16_t *txxri, uint16_t * rxxri)
2346 struct lpfc_bsg_event *evt;
2347 struct lpfc_iocbq *cmdiocbq, *rspiocbq;
2349 struct lpfc_dmabuf *dmabuf;
2350 struct ulp_bde64 *bpl = NULL;
2351 struct lpfc_sli_ct_request *ctreq = NULL;
2355 unsigned long flags;
2359 evt = lpfc_bsg_event_new(FC_REG_CT_EVENT, current->pid,
2360 SLI_CT_ELX_LOOPBACK);
2364 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2365 list_add(&evt->node, &phba->ct_ev_waiters);
2366 lpfc_bsg_event_ref(evt);
2367 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2369 cmdiocbq = lpfc_sli_get_iocbq(phba);
2370 rspiocbq = lpfc_sli_get_iocbq(phba);
2372 dmabuf = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2374 dmabuf->virt = lpfc_mbuf_alloc(phba, 0, &dmabuf->phys);
2376 INIT_LIST_HEAD(&dmabuf->list);
2377 bpl = (struct ulp_bde64 *) dmabuf->virt;
2378 memset(bpl, 0, sizeof(*bpl));
2379 ctreq = (struct lpfc_sli_ct_request *)(bpl + 1);
2381 le32_to_cpu(putPaddrHigh(dmabuf->phys +
2384 le32_to_cpu(putPaddrLow(dmabuf->phys +
2386 bpl->tus.f.bdeFlags = 0;
2387 bpl->tus.f.bdeSize = ELX_LOOPBACK_HEADER_SZ;
2388 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2392 if (cmdiocbq == NULL || rspiocbq == NULL ||
2393 dmabuf == NULL || bpl == NULL || ctreq == NULL ||
2394 dmabuf->virt == NULL) {
2396 goto err_get_xri_exit;
2399 cmd = &cmdiocbq->iocb;
2400 rsp = &rspiocbq->iocb;
2402 memset(ctreq, 0, ELX_LOOPBACK_HEADER_SZ);
2404 ctreq->RevisionId.bits.Revision = SLI_CT_REVISION;
2405 ctreq->RevisionId.bits.InId = 0;
2406 ctreq->FsType = SLI_CT_ELX_LOOPBACK;
2407 ctreq->FsSubType = 0;
2408 ctreq->CommandResponse.bits.CmdRsp = ELX_LOOPBACK_XRI_SETUP;
2409 ctreq->CommandResponse.bits.Size = 0;
2412 cmd->un.xseq64.bdl.addrHigh = putPaddrHigh(dmabuf->phys);
2413 cmd->un.xseq64.bdl.addrLow = putPaddrLow(dmabuf->phys);
2414 cmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
2415 cmd->un.xseq64.bdl.bdeSize = sizeof(*bpl);
2417 cmd->un.xseq64.w5.hcsw.Fctl = LA;
2418 cmd->un.xseq64.w5.hcsw.Dfctl = 0;
2419 cmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
2420 cmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
2422 cmd->ulpCommand = CMD_XMIT_SEQUENCE64_CR;
2423 cmd->ulpBdeCount = 1;
2425 cmd->ulpClass = CLASS3;
2426 cmd->ulpContext = rpi;
2428 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
2429 cmdiocbq->vport = phba->pport;
2431 iocb_stat = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq,
2433 (phba->fc_ratov * 2)
2434 + LPFC_DRVR_TIMEOUT);
2437 goto err_get_xri_exit;
2439 *txxri = rsp->ulpContext;
2442 evt->wait_time_stamp = jiffies;
2443 time_left = wait_event_interruptible_timeout(
2444 evt->wq, !list_empty(&evt->events_to_see),
2445 ((phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT) * HZ);
2446 if (list_empty(&evt->events_to_see))
2447 ret_val = (time_left) ? -EINTR : -ETIMEDOUT;
2449 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2450 list_move(evt->events_to_see.prev, &evt->events_to_get);
2451 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2452 *rxxri = (list_entry(evt->events_to_get.prev,
2453 typeof(struct event_data),
2459 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2460 lpfc_bsg_event_unref(evt); /* release ref */
2461 lpfc_bsg_event_unref(evt); /* delete */
2462 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2466 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
2470 if (cmdiocbq && (iocb_stat != IOCB_TIMEDOUT))
2471 lpfc_sli_release_iocbq(phba, cmdiocbq);
2473 lpfc_sli_release_iocbq(phba, rspiocbq);
2478 * lpfc_bsg_dma_page_alloc - allocate a bsg mbox page sized dma buffers
2479 * @phba: Pointer to HBA context object
2481 * This function allocates BSG_MBOX_SIZE (4KB) page size dma buffer and.
2482 * retruns the pointer to the buffer.
2484 static struct lpfc_dmabuf *
2485 lpfc_bsg_dma_page_alloc(struct lpfc_hba *phba)
2487 struct lpfc_dmabuf *dmabuf;
2488 struct pci_dev *pcidev = phba->pcidev;
2490 /* allocate dma buffer struct */
2491 dmabuf = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2495 INIT_LIST_HEAD(&dmabuf->list);
2497 /* now, allocate dma buffer */
2498 dmabuf->virt = dma_alloc_coherent(&pcidev->dev, BSG_MBOX_SIZE,
2499 &(dmabuf->phys), GFP_KERNEL);
2501 if (!dmabuf->virt) {
2505 memset((uint8_t *)dmabuf->virt, 0, BSG_MBOX_SIZE);
2511 * lpfc_bsg_dma_page_free - free a bsg mbox page sized dma buffer
2512 * @phba: Pointer to HBA context object.
2513 * @dmabuf: Pointer to the bsg mbox page sized dma buffer descriptor.
2515 * This routine just simply frees a dma buffer and its associated buffer
2516 * descriptor referred by @dmabuf.
2519 lpfc_bsg_dma_page_free(struct lpfc_hba *phba, struct lpfc_dmabuf *dmabuf)
2521 struct pci_dev *pcidev = phba->pcidev;
2527 dma_free_coherent(&pcidev->dev, BSG_MBOX_SIZE,
2528 dmabuf->virt, dmabuf->phys);
2534 * lpfc_bsg_dma_page_list_free - free a list of bsg mbox page sized dma buffers
2535 * @phba: Pointer to HBA context object.
2536 * @dmabuf_list: Pointer to a list of bsg mbox page sized dma buffer descs.
2538 * This routine just simply frees all dma buffers and their associated buffer
2539 * descriptors referred by @dmabuf_list.
2542 lpfc_bsg_dma_page_list_free(struct lpfc_hba *phba,
2543 struct list_head *dmabuf_list)
2545 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
2547 if (list_empty(dmabuf_list))
2550 list_for_each_entry_safe(dmabuf, next_dmabuf, dmabuf_list, list) {
2551 list_del_init(&dmabuf->list);
2552 lpfc_bsg_dma_page_free(phba, dmabuf);
2558 * diag_cmd_data_alloc - fills in a bde struct with dma buffers
2559 * @phba: Pointer to HBA context object
2560 * @bpl: Pointer to 64 bit bde structure
2561 * @size: Number of bytes to process
2562 * @nocopydata: Flag to copy user data into the allocated buffer
2564 * This function allocates page size buffers and populates an lpfc_dmabufext.
2565 * If allowed the user data pointed to with indataptr is copied into the kernel
2566 * memory. The chained list of page size buffers is returned.
2568 static struct lpfc_dmabufext *
2569 diag_cmd_data_alloc(struct lpfc_hba *phba,
2570 struct ulp_bde64 *bpl, uint32_t size,
2573 struct lpfc_dmabufext *mlist = NULL;
2574 struct lpfc_dmabufext *dmp;
2575 int cnt, offset = 0, i = 0;
2576 struct pci_dev *pcidev;
2578 pcidev = phba->pcidev;
2581 /* We get chunks of 4K */
2582 if (size > BUF_SZ_4K)
2587 /* allocate struct lpfc_dmabufext buffer header */
2588 dmp = kmalloc(sizeof(struct lpfc_dmabufext), GFP_KERNEL);
2592 INIT_LIST_HEAD(&dmp->dma.list);
2594 /* Queue it to a linked list */
2596 list_add_tail(&dmp->dma.list, &mlist->dma.list);
2600 /* allocate buffer */
2601 dmp->dma.virt = dma_alloc_coherent(&pcidev->dev,
2612 bpl->tus.f.bdeFlags = 0;
2613 pci_dma_sync_single_for_device(phba->pcidev,
2614 dmp->dma.phys, LPFC_BPL_SIZE, PCI_DMA_TODEVICE);
2617 memset((uint8_t *)dmp->dma.virt, 0, cnt);
2618 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2621 /* build buffer ptr list for IOCB */
2622 bpl->addrLow = le32_to_cpu(putPaddrLow(dmp->dma.phys));
2623 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dmp->dma.phys));
2624 bpl->tus.f.bdeSize = (ushort) cnt;
2625 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2636 diag_cmd_data_free(phba, mlist);
2641 * lpfcdiag_loop_post_rxbufs - post the receive buffers for an unsol CT cmd
2642 * @phba: Pointer to HBA context object
2643 * @rxxri: Receive exchange id
2644 * @len: Number of data bytes
2646 * This function allocates and posts a data buffer of sufficient size to receive
2647 * an unsolicted CT command.
2649 static int lpfcdiag_loop_post_rxbufs(struct lpfc_hba *phba, uint16_t rxxri,
2652 struct lpfc_sli *psli = &phba->sli;
2653 struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
2654 struct lpfc_iocbq *cmdiocbq;
2656 struct list_head head, *curr, *next;
2657 struct lpfc_dmabuf *rxbmp;
2658 struct lpfc_dmabuf *dmp;
2659 struct lpfc_dmabuf *mp[2] = {NULL, NULL};
2660 struct ulp_bde64 *rxbpl = NULL;
2662 struct lpfc_dmabufext *rxbuffer = NULL;
2667 cmdiocbq = lpfc_sli_get_iocbq(phba);
2668 rxbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2669 if (rxbmp != NULL) {
2670 rxbmp->virt = lpfc_mbuf_alloc(phba, 0, &rxbmp->phys);
2672 INIT_LIST_HEAD(&rxbmp->list);
2673 rxbpl = (struct ulp_bde64 *) rxbmp->virt;
2674 rxbuffer = diag_cmd_data_alloc(phba, rxbpl, len, 0);
2678 if (!cmdiocbq || !rxbmp || !rxbpl || !rxbuffer) {
2680 goto err_post_rxbufs_exit;
2683 /* Queue buffers for the receive exchange */
2684 num_bde = (uint32_t)rxbuffer->flag;
2685 dmp = &rxbuffer->dma;
2687 cmd = &cmdiocbq->iocb;
2690 INIT_LIST_HEAD(&head);
2691 list_add_tail(&head, &dmp->list);
2692 list_for_each_safe(curr, next, &head) {
2693 mp[i] = list_entry(curr, struct lpfc_dmabuf, list);
2696 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
2697 mp[i]->buffer_tag = lpfc_sli_get_buffer_tag(phba);
2698 cmd->un.quexri64cx.buff.bde.addrHigh =
2699 putPaddrHigh(mp[i]->phys);
2700 cmd->un.quexri64cx.buff.bde.addrLow =
2701 putPaddrLow(mp[i]->phys);
2702 cmd->un.quexri64cx.buff.bde.tus.f.bdeSize =
2703 ((struct lpfc_dmabufext *)mp[i])->size;
2704 cmd->un.quexri64cx.buff.buffer_tag = mp[i]->buffer_tag;
2705 cmd->ulpCommand = CMD_QUE_XRI64_CX;
2708 cmd->ulpBdeCount = 1;
2709 cmd->unsli3.que_xri64cx_ext_words.ebde_count = 0;
2712 cmd->un.cont64[i].addrHigh = putPaddrHigh(mp[i]->phys);
2713 cmd->un.cont64[i].addrLow = putPaddrLow(mp[i]->phys);
2714 cmd->un.cont64[i].tus.f.bdeSize =
2715 ((struct lpfc_dmabufext *)mp[i])->size;
2716 cmd->ulpBdeCount = ++i;
2718 if ((--num_bde > 0) && (i < 2))
2721 cmd->ulpCommand = CMD_QUE_XRI_BUF64_CX;
2725 cmd->ulpClass = CLASS3;
2726 cmd->ulpContext = rxxri;
2728 iocb_stat = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq,
2730 if (iocb_stat == IOCB_ERROR) {
2731 diag_cmd_data_free(phba,
2732 (struct lpfc_dmabufext *)mp[0]);
2734 diag_cmd_data_free(phba,
2735 (struct lpfc_dmabufext *)mp[1]);
2736 dmp = list_entry(next, struct lpfc_dmabuf, list);
2738 goto err_post_rxbufs_exit;
2741 lpfc_sli_ringpostbuf_put(phba, pring, mp[0]);
2743 lpfc_sli_ringpostbuf_put(phba, pring, mp[1]);
2747 /* The iocb was freed by lpfc_sli_issue_iocb */
2748 cmdiocbq = lpfc_sli_get_iocbq(phba);
2750 dmp = list_entry(next, struct lpfc_dmabuf, list);
2752 goto err_post_rxbufs_exit;
2755 cmd = &cmdiocbq->iocb;
2760 err_post_rxbufs_exit:
2764 lpfc_mbuf_free(phba, rxbmp->virt, rxbmp->phys);
2769 lpfc_sli_release_iocbq(phba, cmdiocbq);
2774 * lpfc_bsg_diag_loopback_run - run loopback on a port by issue ct cmd to itself
2775 * @job: LPFC_BSG_VENDOR_DIAG_TEST fc_bsg_job
2777 * This function receives a user data buffer to be transmitted and received on
2778 * the same port, the link must be up and in loopback mode prior
2780 * 1. A kernel buffer is allocated to copy the user data into.
2781 * 2. The port registers with "itself".
2782 * 3. The transmit and receive exchange ids are obtained.
2783 * 4. The receive exchange id is posted.
2784 * 5. A new els loopback event is created.
2785 * 6. The command and response iocbs are allocated.
2786 * 7. The cmd iocb FsType is set to elx loopback and the CmdRsp to looppback.
2788 * This function is meant to be called n times while the port is in loopback
2789 * so it is the apps responsibility to issue a reset to take the port out
2793 lpfc_bsg_diag_loopback_run(struct fc_bsg_job *job)
2795 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
2796 struct lpfc_hba *phba = vport->phba;
2797 struct diag_mode_test *diag_mode;
2798 struct lpfc_bsg_event *evt;
2799 struct event_data *evdat;
2800 struct lpfc_sli *psli = &phba->sli;
2803 size_t segment_len = 0, segment_offset = 0, current_offset = 0;
2805 struct lpfc_iocbq *cmdiocbq, *rspiocbq = NULL;
2806 IOCB_t *cmd, *rsp = NULL;
2807 struct lpfc_sli_ct_request *ctreq;
2808 struct lpfc_dmabuf *txbmp;
2809 struct ulp_bde64 *txbpl = NULL;
2810 struct lpfc_dmabufext *txbuffer = NULL;
2811 struct list_head head;
2812 struct lpfc_dmabuf *curr;
2813 uint16_t txxri = 0, rxxri;
2815 uint8_t *ptr = NULL, *rx_databuf = NULL;
2819 unsigned long flags;
2820 void *dataout = NULL;
2823 /* in case no data is returned return just the return code */
2824 job->reply->reply_payload_rcv_len = 0;
2826 if (job->request_len <
2827 sizeof(struct fc_bsg_request) + sizeof(struct diag_mode_test)) {
2828 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2829 "2739 Received DIAG TEST request below minimum "
2832 goto loopback_test_exit;
2835 if (job->request_payload.payload_len !=
2836 job->reply_payload.payload_len) {
2838 goto loopback_test_exit;
2840 diag_mode = (struct diag_mode_test *)
2841 job->request->rqst_data.h_vendor.vendor_cmd;
2843 if ((phba->link_state == LPFC_HBA_ERROR) ||
2844 (psli->sli_flag & LPFC_BLOCK_MGMT_IO) ||
2845 (!(psli->sli_flag & LPFC_SLI_ACTIVE))) {
2847 goto loopback_test_exit;
2850 if (!lpfc_is_link_up(phba) || !(phba->link_flag & LS_LOOPBACK_MODE)) {
2852 goto loopback_test_exit;
2855 size = job->request_payload.payload_len;
2856 full_size = size + ELX_LOOPBACK_HEADER_SZ; /* plus the header */
2858 if ((size == 0) || (size > 80 * BUF_SZ_4K)) {
2860 goto loopback_test_exit;
2863 if (full_size >= BUF_SZ_4K) {
2865 * Allocate memory for ioctl data. If buffer is bigger than 64k,
2866 * then we allocate 64k and re-use that buffer over and over to
2867 * xfer the whole block. This is because Linux kernel has a
2868 * problem allocating more than 120k of kernel space memory. Saw
2869 * problem with GET_FCPTARGETMAPPING...
2871 if (size <= (64 * 1024))
2872 total_mem = full_size;
2874 total_mem = 64 * 1024;
2876 /* Allocate memory for ioctl data */
2877 total_mem = BUF_SZ_4K;
2879 dataout = kmalloc(total_mem, GFP_KERNEL);
2880 if (dataout == NULL) {
2882 goto loopback_test_exit;
2886 ptr += ELX_LOOPBACK_HEADER_SZ;
2887 sg_copy_to_buffer(job->request_payload.sg_list,
2888 job->request_payload.sg_cnt,
2890 rc = lpfcdiag_loop_self_reg(phba, &rpi);
2892 goto loopback_test_exit;
2894 if (phba->sli_rev < LPFC_SLI_REV4) {
2895 rc = lpfcdiag_loop_get_xri(phba, rpi, &txxri, &rxxri);
2897 lpfcdiag_loop_self_unreg(phba, rpi);
2898 goto loopback_test_exit;
2901 rc = lpfcdiag_loop_post_rxbufs(phba, rxxri, full_size);
2903 lpfcdiag_loop_self_unreg(phba, rpi);
2904 goto loopback_test_exit;
2907 evt = lpfc_bsg_event_new(FC_REG_CT_EVENT, current->pid,
2908 SLI_CT_ELX_LOOPBACK);
2910 lpfcdiag_loop_self_unreg(phba, rpi);
2912 goto loopback_test_exit;
2915 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2916 list_add(&evt->node, &phba->ct_ev_waiters);
2917 lpfc_bsg_event_ref(evt);
2918 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2920 cmdiocbq = lpfc_sli_get_iocbq(phba);
2921 if (phba->sli_rev < LPFC_SLI_REV4)
2922 rspiocbq = lpfc_sli_get_iocbq(phba);
2923 txbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2926 txbmp->virt = lpfc_mbuf_alloc(phba, 0, &txbmp->phys);
2928 INIT_LIST_HEAD(&txbmp->list);
2929 txbpl = (struct ulp_bde64 *) txbmp->virt;
2930 txbuffer = diag_cmd_data_alloc(phba,
2931 txbpl, full_size, 0);
2935 if (!cmdiocbq || !txbmp || !txbpl || !txbuffer || !txbmp->virt) {
2937 goto err_loopback_test_exit;
2939 if ((phba->sli_rev < LPFC_SLI_REV4) && !rspiocbq) {
2941 goto err_loopback_test_exit;
2944 cmd = &cmdiocbq->iocb;
2945 if (phba->sli_rev < LPFC_SLI_REV4)
2946 rsp = &rspiocbq->iocb;
2948 INIT_LIST_HEAD(&head);
2949 list_add_tail(&head, &txbuffer->dma.list);
2950 list_for_each_entry(curr, &head, list) {
2951 segment_len = ((struct lpfc_dmabufext *)curr)->size;
2952 if (current_offset == 0) {
2954 memset(ctreq, 0, ELX_LOOPBACK_HEADER_SZ);
2955 ctreq->RevisionId.bits.Revision = SLI_CT_REVISION;
2956 ctreq->RevisionId.bits.InId = 0;
2957 ctreq->FsType = SLI_CT_ELX_LOOPBACK;
2958 ctreq->FsSubType = 0;
2959 ctreq->CommandResponse.bits.CmdRsp = ELX_LOOPBACK_DATA;
2960 ctreq->CommandResponse.bits.Size = size;
2961 segment_offset = ELX_LOOPBACK_HEADER_SZ;
2965 BUG_ON(segment_offset >= segment_len);
2966 memcpy(curr->virt + segment_offset,
2967 ptr + current_offset,
2968 segment_len - segment_offset);
2970 current_offset += segment_len - segment_offset;
2971 BUG_ON(current_offset > size);
2975 /* Build the XMIT_SEQUENCE iocb */
2976 num_bde = (uint32_t)txbuffer->flag;
2978 cmd->un.xseq64.bdl.addrHigh = putPaddrHigh(txbmp->phys);
2979 cmd->un.xseq64.bdl.addrLow = putPaddrLow(txbmp->phys);
2980 cmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
2981 cmd->un.xseq64.bdl.bdeSize = (num_bde * sizeof(struct ulp_bde64));
2983 cmd->un.xseq64.w5.hcsw.Fctl = (LS | LA);
2984 cmd->un.xseq64.w5.hcsw.Dfctl = 0;
2985 cmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
2986 cmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
2988 cmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;
2989 cmd->ulpBdeCount = 1;
2991 cmd->ulpClass = CLASS3;
2993 if (phba->sli_rev < LPFC_SLI_REV4) {
2994 cmd->ulpContext = txxri;
2996 cmd->un.xseq64.bdl.ulpIoTag32 = 0;
2997 cmd->un.ulpWord[3] = phba->sli4_hba.rpi_ids[rpi];
2998 cmdiocbq->context3 = txbmp;
2999 cmdiocbq->sli4_xritag = NO_XRI;
3000 cmd->unsli3.rcvsli3.ox_id = 0xffff;
3002 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
3003 cmdiocbq->vport = phba->pport;
3004 iocb_stat = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq,
3005 rspiocbq, (phba->fc_ratov * 2) +
3008 if ((iocb_stat != IOCB_SUCCESS) || ((phba->sli_rev < LPFC_SLI_REV4) &&
3009 (rsp->ulpStatus != IOCB_SUCCESS))) {
3010 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3011 "3126 Failed loopback test issue iocb: "
3012 "iocb_stat:x%x\n", iocb_stat);
3014 goto err_loopback_test_exit;
3018 time_left = wait_event_interruptible_timeout(
3019 evt->wq, !list_empty(&evt->events_to_see),
3020 ((phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT) * HZ);
3022 if (list_empty(&evt->events_to_see)) {
3023 rc = (time_left) ? -EINTR : -ETIMEDOUT;
3024 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3025 "3125 Not receiving unsolicited event, "
3028 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3029 list_move(evt->events_to_see.prev, &evt->events_to_get);
3030 evdat = list_entry(evt->events_to_get.prev,
3031 typeof(*evdat), node);
3032 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3033 rx_databuf = evdat->data;
3034 if (evdat->len != full_size) {
3035 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3036 "1603 Loopback test did not receive expected "
3037 "data length. actual length 0x%x expected "
3039 evdat->len, full_size);
3041 } else if (rx_databuf == NULL)
3045 /* skip over elx loopback header */
3046 rx_databuf += ELX_LOOPBACK_HEADER_SZ;
3047 job->reply->reply_payload_rcv_len =
3048 sg_copy_from_buffer(job->reply_payload.sg_list,
3049 job->reply_payload.sg_cnt,
3051 job->reply->reply_payload_rcv_len = size;
3055 err_loopback_test_exit:
3056 lpfcdiag_loop_self_unreg(phba, rpi);
3058 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3059 lpfc_bsg_event_unref(evt); /* release ref */
3060 lpfc_bsg_event_unref(evt); /* delete */
3061 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3063 if (cmdiocbq != NULL)
3064 lpfc_sli_release_iocbq(phba, cmdiocbq);
3066 if (rspiocbq != NULL)
3067 lpfc_sli_release_iocbq(phba, rspiocbq);
3069 if (txbmp != NULL) {
3070 if (txbpl != NULL) {
3071 if (txbuffer != NULL)
3072 diag_cmd_data_free(phba, txbuffer);
3073 lpfc_mbuf_free(phba, txbmp->virt, txbmp->phys);
3080 /* make error code available to userspace */
3081 job->reply->result = rc;
3082 job->dd_data = NULL;
3083 /* complete the job back to userspace if no error */
3084 if (rc == IOCB_SUCCESS)
3090 * lpfc_bsg_get_dfc_rev - process a GET_DFC_REV bsg vendor command
3091 * @job: GET_DFC_REV fc_bsg_job
3094 lpfc_bsg_get_dfc_rev(struct fc_bsg_job *job)
3096 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
3097 struct lpfc_hba *phba = vport->phba;
3098 struct get_mgmt_rev *event_req;
3099 struct get_mgmt_rev_reply *event_reply;
3102 if (job->request_len <
3103 sizeof(struct fc_bsg_request) + sizeof(struct get_mgmt_rev)) {
3104 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3105 "2740 Received GET_DFC_REV request below "
3111 event_req = (struct get_mgmt_rev *)
3112 job->request->rqst_data.h_vendor.vendor_cmd;
3114 event_reply = (struct get_mgmt_rev_reply *)
3115 job->reply->reply_data.vendor_reply.vendor_rsp;
3117 if (job->reply_len <
3118 sizeof(struct fc_bsg_request) + sizeof(struct get_mgmt_rev_reply)) {
3119 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3120 "2741 Received GET_DFC_REV reply below "
3126 event_reply->info.a_Major = MANAGEMENT_MAJOR_REV;
3127 event_reply->info.a_Minor = MANAGEMENT_MINOR_REV;
3129 job->reply->result = rc;
3136 * lpfc_bsg_issue_mbox_cmpl - lpfc_bsg_issue_mbox mbox completion handler
3137 * @phba: Pointer to HBA context object.
3138 * @pmboxq: Pointer to mailbox command.
3140 * This is completion handler function for mailbox commands issued from
3141 * lpfc_bsg_issue_mbox function. This function is called by the
3142 * mailbox event handler function with no lock held. This function
3143 * will wake up thread waiting on the wait queue pointed by context1
3147 lpfc_bsg_issue_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
3149 struct bsg_job_data *dd_data;
3150 struct fc_bsg_job *job;
3152 unsigned long flags;
3153 uint8_t *pmb, *pmb_buf;
3155 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3156 dd_data = pmboxq->context1;
3157 /* job already timed out? */
3159 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3164 * The outgoing buffer is readily referred from the dma buffer,
3165 * just need to get header part from mailboxq structure.
3167 pmb = (uint8_t *)&pmboxq->u.mb;
3168 pmb_buf = (uint8_t *)dd_data->context_un.mbox.mb;
3169 memcpy(pmb_buf, pmb, sizeof(MAILBOX_t));
3171 job = dd_data->context_un.mbox.set_job;
3173 size = job->reply_payload.payload_len;
3174 job->reply->reply_payload_rcv_len =
3175 sg_copy_from_buffer(job->reply_payload.sg_list,
3176 job->reply_payload.sg_cnt,
3178 /* need to hold the lock until we set job->dd_data to NULL
3179 * to hold off the timeout handler returning to the mid-layer
3180 * while we are still processing the job.
3182 job->dd_data = NULL;
3183 dd_data->context_un.mbox.set_job = NULL;
3184 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3186 dd_data->context_un.mbox.set_job = NULL;
3187 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3190 mempool_free(dd_data->context_un.mbox.pmboxq, phba->mbox_mem_pool);
3191 lpfc_bsg_dma_page_free(phba, dd_data->context_un.mbox.dmabuffers);
3195 job->reply->result = 0;
3202 * lpfc_bsg_check_cmd_access - test for a supported mailbox command
3203 * @phba: Pointer to HBA context object.
3204 * @mb: Pointer to a mailbox object.
3205 * @vport: Pointer to a vport object.
3207 * Some commands require the port to be offline, some may not be called from
3210 static int lpfc_bsg_check_cmd_access(struct lpfc_hba *phba,
3211 MAILBOX_t *mb, struct lpfc_vport *vport)
3213 /* return negative error values for bsg job */
3214 switch (mb->mbxCommand) {
3218 case MBX_CONFIG_LINK:
3219 case MBX_CONFIG_RING:
3220 case MBX_RESET_RING:
3221 case MBX_UNREG_LOGIN:
3223 case MBX_DUMP_CONTEXT:
3227 if (!(vport->fc_flag & FC_OFFLINE_MODE)) {
3228 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3229 "2743 Command 0x%x is illegal in on-line "
3235 case MBX_WRITE_VPARMS:
3238 case MBX_READ_CONFIG:
3239 case MBX_READ_RCONFIG:
3240 case MBX_READ_STATUS:
3243 case MBX_READ_LNK_STAT:
3244 case MBX_DUMP_MEMORY:
3246 case MBX_UPDATE_CFG:
3247 case MBX_KILL_BOARD:
3249 case MBX_LOAD_EXP_ROM:
3251 case MBX_DEL_LD_ENTRY:
3254 case MBX_SLI4_CONFIG:
3255 case MBX_READ_EVENT_LOG:
3256 case MBX_READ_EVENT_LOG_STATUS:
3257 case MBX_WRITE_EVENT_LOG:
3258 case MBX_PORT_CAPABILITIES:
3259 case MBX_PORT_IOV_CONTROL:
3260 case MBX_RUN_BIU_DIAG64:
3262 case MBX_SET_VARIABLE:
3263 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3264 "1226 mbox: set_variable 0x%x, 0x%x\n",
3266 mb->un.varWords[1]);
3267 if ((mb->un.varWords[0] == SETVAR_MLOMNT)
3268 && (mb->un.varWords[1] == 1)) {
3269 phba->wait_4_mlo_maint_flg = 1;
3270 } else if (mb->un.varWords[0] == SETVAR_MLORST) {
3271 spin_lock_irq(&phba->hbalock);
3272 phba->link_flag &= ~LS_LOOPBACK_MODE;
3273 spin_unlock_irq(&phba->hbalock);
3274 phba->fc_topology = LPFC_TOPOLOGY_PT_PT;
3277 case MBX_READ_SPARM64:
3278 case MBX_READ_TOPOLOGY:
3280 case MBX_REG_LOGIN64:
3281 case MBX_CONFIG_PORT:
3282 case MBX_RUN_BIU_DIAG:
3284 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3285 "2742 Unknown Command 0x%x\n",
3294 * lpfc_bsg_mbox_ext_cleanup - clean up context of multi-buffer mbox session
3295 * @phba: Pointer to HBA context object.
3297 * This is routine clean up and reset BSG handling of multi-buffer mbox
3301 lpfc_bsg_mbox_ext_session_reset(struct lpfc_hba *phba)
3303 if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_IDLE)
3306 /* free all memory, including dma buffers */
3307 lpfc_bsg_dma_page_list_free(phba,
3308 &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
3309 lpfc_bsg_dma_page_free(phba, phba->mbox_ext_buf_ctx.mbx_dmabuf);
3310 /* multi-buffer write mailbox command pass-through complete */
3311 memset((char *)&phba->mbox_ext_buf_ctx, 0,
3312 sizeof(struct lpfc_mbox_ext_buf_ctx));
3313 INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
3319 * lpfc_bsg_issue_mbox_ext_handle_job - job handler for multi-buffer mbox cmpl
3320 * @phba: Pointer to HBA context object.
3321 * @pmboxq: Pointer to mailbox command.
3323 * This is routine handles BSG job for mailbox commands completions with
3324 * multiple external buffers.
3326 static struct fc_bsg_job *
3327 lpfc_bsg_issue_mbox_ext_handle_job(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
3329 struct bsg_job_data *dd_data;
3330 struct fc_bsg_job *job;
3331 uint8_t *pmb, *pmb_buf;
3332 unsigned long flags;
3335 struct lpfc_dmabuf *dmabuf;
3336 struct lpfc_sli_config_mbox *sli_cfg_mbx;
3339 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3340 dd_data = pmboxq->context1;
3341 /* has the job already timed out? */
3343 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3349 * The outgoing buffer is readily referred from the dma buffer,
3350 * just need to get header part from mailboxq structure.
3352 pmb = (uint8_t *)&pmboxq->u.mb;
3353 pmb_buf = (uint8_t *)dd_data->context_un.mbox.mb;
3354 /* Copy the byte swapped response mailbox back to the user */
3355 memcpy(pmb_buf, pmb, sizeof(MAILBOX_t));
3356 /* if there is any non-embedded extended data copy that too */
3357 dmabuf = phba->mbox_ext_buf_ctx.mbx_dmabuf;
3358 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
3359 if (!bsg_bf_get(lpfc_mbox_hdr_emb,
3360 &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) {
3361 pmbx = (uint8_t *)dmabuf->virt;
3362 /* byte swap the extended data following the mailbox command */
3363 lpfc_sli_pcimem_bcopy(&pmbx[sizeof(MAILBOX_t)],
3364 &pmbx[sizeof(MAILBOX_t)],
3365 sli_cfg_mbx->un.sli_config_emb0_subsys.mse[0].buf_len);
3368 job = dd_data->context_un.mbox.set_job;
3370 size = job->reply_payload.payload_len;
3371 job->reply->reply_payload_rcv_len =
3372 sg_copy_from_buffer(job->reply_payload.sg_list,
3373 job->reply_payload.sg_cnt,
3375 /* result for successful */
3376 job->reply->result = 0;
3377 job->dd_data = NULL;
3378 /* need to hold the lock util we set job->dd_data to NULL
3379 * to hold off the timeout handler from midlayer to take
3382 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3383 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3384 "2937 SLI_CONFIG ext-buffer maibox command "
3385 "(x%x/x%x) complete bsg job done, bsize:%d\n",
3386 phba->mbox_ext_buf_ctx.nembType,
3387 phba->mbox_ext_buf_ctx.mboxType, size);
3388 lpfc_idiag_mbxacc_dump_bsg_mbox(phba,
3389 phba->mbox_ext_buf_ctx.nembType,
3390 phba->mbox_ext_buf_ctx.mboxType,
3391 dma_ebuf, sta_pos_addr,
3392 phba->mbox_ext_buf_ctx.mbx_dmabuf, 0);
3394 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3398 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3399 "2938 SLI_CONFIG ext-buffer maibox "
3400 "command (x%x/x%x) failure, rc:x%x\n",
3401 phba->mbox_ext_buf_ctx.nembType,
3402 phba->mbox_ext_buf_ctx.mboxType, rc);
3404 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_DONE;
3411 * lpfc_bsg_issue_read_mbox_ext_cmpl - compl handler for multi-buffer read mbox
3412 * @phba: Pointer to HBA context object.
3413 * @pmboxq: Pointer to mailbox command.
3415 * This is completion handler function for mailbox read commands with multiple
3419 lpfc_bsg_issue_read_mbox_ext_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
3421 struct fc_bsg_job *job;
3423 /* handle the BSG job with mailbox command */
3424 if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_ABTS)
3425 pmboxq->u.mb.mbxStatus = MBXERR_ERROR;
3427 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3428 "2939 SLI_CONFIG ext-buffer rd maibox command "
3429 "complete, ctxState:x%x, mbxStatus:x%x\n",
3430 phba->mbox_ext_buf_ctx.state, pmboxq->u.mb.mbxStatus);
3432 job = lpfc_bsg_issue_mbox_ext_handle_job(phba, pmboxq);
3434 if (pmboxq->u.mb.mbxStatus || phba->mbox_ext_buf_ctx.numBuf == 1)
3435 lpfc_bsg_mbox_ext_session_reset(phba);
3437 /* free base driver mailbox structure memory */
3438 mempool_free(pmboxq, phba->mbox_mem_pool);
3440 /* complete the bsg job if we have it */
3448 * lpfc_bsg_issue_write_mbox_ext_cmpl - cmpl handler for multi-buffer write mbox
3449 * @phba: Pointer to HBA context object.
3450 * @pmboxq: Pointer to mailbox command.
3452 * This is completion handler function for mailbox write commands with multiple
3456 lpfc_bsg_issue_write_mbox_ext_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
3458 struct fc_bsg_job *job;
3460 /* handle the BSG job with the mailbox command */
3461 if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_ABTS)
3462 pmboxq->u.mb.mbxStatus = MBXERR_ERROR;
3464 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3465 "2940 SLI_CONFIG ext-buffer wr maibox command "
3466 "complete, ctxState:x%x, mbxStatus:x%x\n",
3467 phba->mbox_ext_buf_ctx.state, pmboxq->u.mb.mbxStatus);
3469 job = lpfc_bsg_issue_mbox_ext_handle_job(phba, pmboxq);
3471 /* free all memory, including dma buffers */
3472 mempool_free(pmboxq, phba->mbox_mem_pool);
3473 lpfc_bsg_mbox_ext_session_reset(phba);
3475 /* complete the bsg job if we have it */
3483 lpfc_bsg_sli_cfg_dma_desc_setup(struct lpfc_hba *phba, enum nemb_type nemb_tp,
3484 uint32_t index, struct lpfc_dmabuf *mbx_dmabuf,
3485 struct lpfc_dmabuf *ext_dmabuf)
3487 struct lpfc_sli_config_mbox *sli_cfg_mbx;
3489 /* pointer to the start of mailbox command */
3490 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)mbx_dmabuf->virt;
3492 if (nemb_tp == nemb_mse) {
3494 sli_cfg_mbx->un.sli_config_emb0_subsys.
3496 putPaddrHigh(mbx_dmabuf->phys +
3498 sli_cfg_mbx->un.sli_config_emb0_subsys.
3500 putPaddrLow(mbx_dmabuf->phys +
3502 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3503 "2943 SLI_CONFIG(mse)[%d], "
3504 "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3506 sli_cfg_mbx->un.sli_config_emb0_subsys.
3508 sli_cfg_mbx->un.sli_config_emb0_subsys.
3510 sli_cfg_mbx->un.sli_config_emb0_subsys.
3513 sli_cfg_mbx->un.sli_config_emb0_subsys.
3515 putPaddrHigh(ext_dmabuf->phys);
3516 sli_cfg_mbx->un.sli_config_emb0_subsys.
3518 putPaddrLow(ext_dmabuf->phys);
3519 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3520 "2944 SLI_CONFIG(mse)[%d], "
3521 "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3523 sli_cfg_mbx->un.sli_config_emb0_subsys.
3525 sli_cfg_mbx->un.sli_config_emb0_subsys.
3527 sli_cfg_mbx->un.sli_config_emb0_subsys.
3532 sli_cfg_mbx->un.sli_config_emb1_subsys.
3534 putPaddrHigh(mbx_dmabuf->phys +
3536 sli_cfg_mbx->un.sli_config_emb1_subsys.
3538 putPaddrLow(mbx_dmabuf->phys +
3540 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3541 "3007 SLI_CONFIG(hbd)[%d], "
3542 "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3544 bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
3546 sli_config_emb1_subsys.hbd[index]),
3547 sli_cfg_mbx->un.sli_config_emb1_subsys.
3549 sli_cfg_mbx->un.sli_config_emb1_subsys.
3553 sli_cfg_mbx->un.sli_config_emb1_subsys.
3555 putPaddrHigh(ext_dmabuf->phys);
3556 sli_cfg_mbx->un.sli_config_emb1_subsys.
3558 putPaddrLow(ext_dmabuf->phys);
3559 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3560 "3008 SLI_CONFIG(hbd)[%d], "
3561 "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3563 bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
3565 sli_config_emb1_subsys.hbd[index]),
3566 sli_cfg_mbx->un.sli_config_emb1_subsys.
3568 sli_cfg_mbx->un.sli_config_emb1_subsys.
3576 * lpfc_bsg_sli_cfg_mse_read_cmd_ext - sli_config non-embedded mailbox cmd read
3577 * @phba: Pointer to HBA context object.
3578 * @mb: Pointer to a BSG mailbox object.
3579 * @nemb_tp: Enumerate of non-embedded mailbox command type.
3580 * @dmabuff: Pointer to a DMA buffer descriptor.
3582 * This routine performs SLI_CONFIG (0x9B) read mailbox command operation with
3583 * non-embedded external bufffers.
3586 lpfc_bsg_sli_cfg_read_cmd_ext(struct lpfc_hba *phba, struct fc_bsg_job *job,
3587 enum nemb_type nemb_tp,
3588 struct lpfc_dmabuf *dmabuf)
3590 struct lpfc_sli_config_mbox *sli_cfg_mbx;
3591 struct dfc_mbox_req *mbox_req;
3592 struct lpfc_dmabuf *curr_dmabuf, *next_dmabuf;
3593 uint32_t ext_buf_cnt, ext_buf_index;
3594 struct lpfc_dmabuf *ext_dmabuf = NULL;
3595 struct bsg_job_data *dd_data = NULL;
3596 LPFC_MBOXQ_t *pmboxq = NULL;
3602 (struct dfc_mbox_req *)job->request->rqst_data.h_vendor.vendor_cmd;
3604 /* pointer to the start of mailbox command */
3605 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
3607 if (nemb_tp == nemb_mse) {
3608 ext_buf_cnt = bsg_bf_get(lpfc_mbox_hdr_mse_cnt,
3609 &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr);
3610 if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_MSE) {
3611 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3612 "2945 Handled SLI_CONFIG(mse) rd, "
3613 "ext_buf_cnt(%d) out of range(%d)\n",
3615 LPFC_MBX_SLI_CONFIG_MAX_MSE);
3619 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3620 "2941 Handled SLI_CONFIG(mse) rd, "
3621 "ext_buf_cnt:%d\n", ext_buf_cnt);
3623 /* sanity check on interface type for support */
3624 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
3625 LPFC_SLI_INTF_IF_TYPE_2) {
3629 /* nemb_tp == nemb_hbd */
3630 ext_buf_cnt = sli_cfg_mbx->un.sli_config_emb1_subsys.hbd_count;
3631 if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_HBD) {
3632 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3633 "2946 Handled SLI_CONFIG(hbd) rd, "
3634 "ext_buf_cnt(%d) out of range(%d)\n",
3636 LPFC_MBX_SLI_CONFIG_MAX_HBD);
3640 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3641 "2942 Handled SLI_CONFIG(hbd) rd, "
3642 "ext_buf_cnt:%d\n", ext_buf_cnt);
3645 /* before dma descriptor setup */
3646 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_rd, dma_mbox,
3647 sta_pre_addr, dmabuf, ext_buf_cnt);
3649 /* reject non-embedded mailbox command with none external buffer */
3650 if (ext_buf_cnt == 0) {
3653 } else if (ext_buf_cnt > 1) {
3654 /* additional external read buffers */
3655 for (i = 1; i < ext_buf_cnt; i++) {
3656 ext_dmabuf = lpfc_bsg_dma_page_alloc(phba);
3661 list_add_tail(&ext_dmabuf->list,
3662 &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
3666 /* bsg tracking structure */
3667 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
3673 /* mailbox command structure for base driver */
3674 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3679 memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
3681 /* for the first external buffer */
3682 lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, 0, dmabuf, dmabuf);
3684 /* for the rest of external buffer descriptors if any */
3685 if (ext_buf_cnt > 1) {
3687 list_for_each_entry_safe(curr_dmabuf, next_dmabuf,
3688 &phba->mbox_ext_buf_ctx.ext_dmabuf_list, list) {
3689 lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp,
3690 ext_buf_index, dmabuf,
3696 /* after dma descriptor setup */
3697 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_rd, dma_mbox,
3698 sta_pos_addr, dmabuf, ext_buf_cnt);
3700 /* construct base driver mbox command */
3701 pmb = &pmboxq->u.mb;
3702 pmbx = (uint8_t *)dmabuf->virt;
3703 memcpy(pmb, pmbx, sizeof(*pmb));
3704 pmb->mbxOwner = OWN_HOST;
3705 pmboxq->vport = phba->pport;
3707 /* multi-buffer handling context */
3708 phba->mbox_ext_buf_ctx.nembType = nemb_tp;
3709 phba->mbox_ext_buf_ctx.mboxType = mbox_rd;
3710 phba->mbox_ext_buf_ctx.numBuf = ext_buf_cnt;
3711 phba->mbox_ext_buf_ctx.mbxTag = mbox_req->extMboxTag;
3712 phba->mbox_ext_buf_ctx.seqNum = mbox_req->extSeqNum;
3713 phba->mbox_ext_buf_ctx.mbx_dmabuf = dmabuf;
3715 /* callback for multi-buffer read mailbox command */
3716 pmboxq->mbox_cmpl = lpfc_bsg_issue_read_mbox_ext_cmpl;
3718 /* context fields to callback function */
3719 pmboxq->context1 = dd_data;
3720 dd_data->type = TYPE_MBOX;
3721 dd_data->context_un.mbox.pmboxq = pmboxq;
3722 dd_data->context_un.mbox.mb = (MAILBOX_t *)pmbx;
3723 dd_data->context_un.mbox.set_job = job;
3724 job->dd_data = dd_data;
3727 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
3730 * Non-embedded mailbox subcommand data gets byte swapped here because
3731 * the lower level driver code only does the first 64 mailbox words.
3733 if ((!bsg_bf_get(lpfc_mbox_hdr_emb,
3734 &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) &&
3735 (nemb_tp == nemb_mse))
3736 lpfc_sli_pcimem_bcopy(&pmbx[sizeof(MAILBOX_t)],
3737 &pmbx[sizeof(MAILBOX_t)],
3738 sli_cfg_mbx->un.sli_config_emb0_subsys.
3741 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
3742 if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
3743 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3744 "2947 Issued SLI_CONFIG ext-buffer "
3745 "maibox command, rc:x%x\n", rc);
3746 return SLI_CONFIG_HANDLED;
3748 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3749 "2948 Failed to issue SLI_CONFIG ext-buffer "
3750 "maibox command, rc:x%x\n", rc);
3755 mempool_free(pmboxq, phba->mbox_mem_pool);
3756 lpfc_bsg_dma_page_list_free(phba,
3757 &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
3759 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_IDLE;
3764 * lpfc_bsg_sli_cfg_write_cmd_ext - sli_config non-embedded mailbox cmd write
3765 * @phba: Pointer to HBA context object.
3766 * @mb: Pointer to a BSG mailbox object.
3767 * @dmabuff: Pointer to a DMA buffer descriptor.
3769 * This routine performs SLI_CONFIG (0x9B) write mailbox command operation with
3770 * non-embedded external bufffers.
3773 lpfc_bsg_sli_cfg_write_cmd_ext(struct lpfc_hba *phba, struct fc_bsg_job *job,
3774 enum nemb_type nemb_tp,
3775 struct lpfc_dmabuf *dmabuf)
3777 struct dfc_mbox_req *mbox_req;
3778 struct lpfc_sli_config_mbox *sli_cfg_mbx;
3779 uint32_t ext_buf_cnt;
3780 struct bsg_job_data *dd_data = NULL;
3781 LPFC_MBOXQ_t *pmboxq = NULL;
3784 int rc = SLI_CONFIG_NOT_HANDLED, i;
3787 (struct dfc_mbox_req *)job->request->rqst_data.h_vendor.vendor_cmd;
3789 /* pointer to the start of mailbox command */
3790 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
3792 if (nemb_tp == nemb_mse) {
3793 ext_buf_cnt = bsg_bf_get(lpfc_mbox_hdr_mse_cnt,
3794 &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr);
3795 if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_MSE) {
3796 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3797 "2953 Failed SLI_CONFIG(mse) wr, "
3798 "ext_buf_cnt(%d) out of range(%d)\n",
3800 LPFC_MBX_SLI_CONFIG_MAX_MSE);
3803 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3804 "2949 Handled SLI_CONFIG(mse) wr, "
3805 "ext_buf_cnt:%d\n", ext_buf_cnt);
3807 /* sanity check on interface type for support */
3808 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
3809 LPFC_SLI_INTF_IF_TYPE_2)
3811 /* nemb_tp == nemb_hbd */
3812 ext_buf_cnt = sli_cfg_mbx->un.sli_config_emb1_subsys.hbd_count;
3813 if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_HBD) {
3814 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3815 "2954 Failed SLI_CONFIG(hbd) wr, "
3816 "ext_buf_cnt(%d) out of range(%d)\n",
3818 LPFC_MBX_SLI_CONFIG_MAX_HBD);
3821 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3822 "2950 Handled SLI_CONFIG(hbd) wr, "
3823 "ext_buf_cnt:%d\n", ext_buf_cnt);
3826 /* before dma buffer descriptor setup */
3827 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_wr, dma_mbox,
3828 sta_pre_addr, dmabuf, ext_buf_cnt);
3830 if (ext_buf_cnt == 0)
3833 /* for the first external buffer */
3834 lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, 0, dmabuf, dmabuf);
3836 /* after dma descriptor setup */
3837 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_wr, dma_mbox,
3838 sta_pos_addr, dmabuf, ext_buf_cnt);
3840 /* log for looking forward */
3841 for (i = 1; i < ext_buf_cnt; i++) {
3842 if (nemb_tp == nemb_mse)
3843 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3844 "2951 SLI_CONFIG(mse), buf[%d]-length:%d\n",
3845 i, sli_cfg_mbx->un.sli_config_emb0_subsys.
3848 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3849 "2952 SLI_CONFIG(hbd), buf[%d]-length:%d\n",
3850 i, bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
3851 &sli_cfg_mbx->un.sli_config_emb1_subsys.
3855 /* multi-buffer handling context */
3856 phba->mbox_ext_buf_ctx.nembType = nemb_tp;
3857 phba->mbox_ext_buf_ctx.mboxType = mbox_wr;
3858 phba->mbox_ext_buf_ctx.numBuf = ext_buf_cnt;
3859 phba->mbox_ext_buf_ctx.mbxTag = mbox_req->extMboxTag;
3860 phba->mbox_ext_buf_ctx.seqNum = mbox_req->extSeqNum;
3861 phba->mbox_ext_buf_ctx.mbx_dmabuf = dmabuf;
3863 if (ext_buf_cnt == 1) {
3864 /* bsg tracking structure */
3865 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
3871 /* mailbox command structure for base driver */
3872 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3877 memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
3878 pmb = &pmboxq->u.mb;
3879 mbx = (uint8_t *)dmabuf->virt;
3880 memcpy(pmb, mbx, sizeof(*pmb));
3881 pmb->mbxOwner = OWN_HOST;
3882 pmboxq->vport = phba->pport;
3884 /* callback for multi-buffer read mailbox command */
3885 pmboxq->mbox_cmpl = lpfc_bsg_issue_write_mbox_ext_cmpl;
3887 /* context fields to callback function */
3888 pmboxq->context1 = dd_data;
3889 dd_data->type = TYPE_MBOX;
3890 dd_data->context_un.mbox.pmboxq = pmboxq;
3891 dd_data->context_un.mbox.mb = (MAILBOX_t *)mbx;
3892 dd_data->context_un.mbox.set_job = job;
3893 job->dd_data = dd_data;
3896 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
3898 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
3899 if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
3900 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3901 "2955 Issued SLI_CONFIG ext-buffer "
3902 "maibox command, rc:x%x\n", rc);
3903 return SLI_CONFIG_HANDLED;
3905 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3906 "2956 Failed to issue SLI_CONFIG ext-buffer "
3907 "maibox command, rc:x%x\n", rc);
3912 /* wait for additoinal external buffers */
3913 job->reply->result = 0;
3915 return SLI_CONFIG_HANDLED;
3919 mempool_free(pmboxq, phba->mbox_mem_pool);
3926 * lpfc_bsg_handle_sli_cfg_mbox - handle sli-cfg mailbox cmd with ext buffer
3927 * @phba: Pointer to HBA context object.
3928 * @mb: Pointer to a BSG mailbox object.
3929 * @dmabuff: Pointer to a DMA buffer descriptor.
3931 * This routine handles SLI_CONFIG (0x9B) mailbox command with non-embedded
3932 * external bufffers, including both 0x9B with non-embedded MSEs and 0x9B
3933 * with embedded sussystem 0x1 and opcodes with external HBDs.
3936 lpfc_bsg_handle_sli_cfg_mbox(struct lpfc_hba *phba, struct fc_bsg_job *job,
3937 struct lpfc_dmabuf *dmabuf)
3939 struct lpfc_sli_config_mbox *sli_cfg_mbx;
3942 int rc = SLI_CONFIG_NOT_HANDLED;
3944 /* state change on new multi-buffer pass-through mailbox command */
3945 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_HOST;
3947 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
3949 if (!bsg_bf_get(lpfc_mbox_hdr_emb,
3950 &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) {
3951 subsys = bsg_bf_get(lpfc_emb0_subcmnd_subsys,
3952 &sli_cfg_mbx->un.sli_config_emb0_subsys);
3953 opcode = bsg_bf_get(lpfc_emb0_subcmnd_opcode,
3954 &sli_cfg_mbx->un.sli_config_emb0_subsys);
3955 if (subsys == SLI_CONFIG_SUBSYS_FCOE) {
3957 case FCOE_OPCODE_READ_FCF:
3958 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3959 "2957 Handled SLI_CONFIG "
3960 "subsys_fcoe, opcode:x%x\n",
3962 rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
3965 case FCOE_OPCODE_ADD_FCF:
3966 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3967 "2958 Handled SLI_CONFIG "
3968 "subsys_fcoe, opcode:x%x\n",
3970 rc = lpfc_bsg_sli_cfg_write_cmd_ext(phba, job,
3974 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3975 "2959 Reject SLI_CONFIG "
3976 "subsys_fcoe, opcode:x%x\n",
3981 } else if (subsys == SLI_CONFIG_SUBSYS_COMN) {
3983 case COMN_OPCODE_GET_CNTL_ADDL_ATTRIBUTES:
3984 case COMN_OPCODE_GET_CNTL_ATTRIBUTES:
3985 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3986 "3106 Handled SLI_CONFIG "
3987 "subsys_comn, opcode:x%x\n",
3989 rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
3993 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3994 "3107 Reject SLI_CONFIG "
3995 "subsys_comn, opcode:x%x\n",
4001 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4002 "2977 Reject SLI_CONFIG "
4003 "subsys:x%d, opcode:x%x\n",
4008 subsys = bsg_bf_get(lpfc_emb1_subcmnd_subsys,
4009 &sli_cfg_mbx->un.sli_config_emb1_subsys);
4010 opcode = bsg_bf_get(lpfc_emb1_subcmnd_opcode,
4011 &sli_cfg_mbx->un.sli_config_emb1_subsys);
4012 if (subsys == SLI_CONFIG_SUBSYS_COMN) {
4014 case COMN_OPCODE_READ_OBJECT:
4015 case COMN_OPCODE_READ_OBJECT_LIST:
4016 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4017 "2960 Handled SLI_CONFIG "
4018 "subsys_comn, opcode:x%x\n",
4020 rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
4023 case COMN_OPCODE_WRITE_OBJECT:
4024 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4025 "2961 Handled SLI_CONFIG "
4026 "subsys_comn, opcode:x%x\n",
4028 rc = lpfc_bsg_sli_cfg_write_cmd_ext(phba, job,
4032 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4033 "2962 Not handled SLI_CONFIG "
4034 "subsys_comn, opcode:x%x\n",
4036 rc = SLI_CONFIG_NOT_HANDLED;
4040 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4041 "2978 Not handled SLI_CONFIG "
4042 "subsys:x%d, opcode:x%x\n",
4044 rc = SLI_CONFIG_NOT_HANDLED;
4048 /* state reset on not handled new multi-buffer mailbox command */
4049 if (rc != SLI_CONFIG_HANDLED)
4050 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_IDLE;
4056 * lpfc_bsg_mbox_ext_abort_req - request to abort mbox command with ext buffers
4057 * @phba: Pointer to HBA context object.
4059 * This routine is for requesting to abort a pass-through mailbox command with
4060 * multiple external buffers due to error condition.
4063 lpfc_bsg_mbox_ext_abort(struct lpfc_hba *phba)
4065 if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_PORT)
4066 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_ABTS;
4068 lpfc_bsg_mbox_ext_session_reset(phba);
4073 * lpfc_bsg_read_ebuf_get - get the next mailbox read external buffer
4074 * @phba: Pointer to HBA context object.
4075 * @dmabuf: Pointer to a DMA buffer descriptor.
4077 * This routine extracts the next mailbox read external buffer back to
4078 * user space through BSG.
4081 lpfc_bsg_read_ebuf_get(struct lpfc_hba *phba, struct fc_bsg_job *job)
4083 struct lpfc_sli_config_mbox *sli_cfg_mbx;
4084 struct lpfc_dmabuf *dmabuf;
4089 index = phba->mbox_ext_buf_ctx.seqNum;
4090 phba->mbox_ext_buf_ctx.seqNum++;
4092 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)
4093 phba->mbox_ext_buf_ctx.mbx_dmabuf->virt;
4095 if (phba->mbox_ext_buf_ctx.nembType == nemb_mse) {
4096 size = bsg_bf_get(lpfc_mbox_sli_config_mse_len,
4097 &sli_cfg_mbx->un.sli_config_emb0_subsys.mse[index]);
4098 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4099 "2963 SLI_CONFIG (mse) ext-buffer rd get "
4100 "buffer[%d], size:%d\n", index, size);
4102 size = bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
4103 &sli_cfg_mbx->un.sli_config_emb1_subsys.hbd[index]);
4104 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4105 "2964 SLI_CONFIG (hbd) ext-buffer rd get "
4106 "buffer[%d], size:%d\n", index, size);
4108 if (list_empty(&phba->mbox_ext_buf_ctx.ext_dmabuf_list))
4110 dmabuf = list_first_entry(&phba->mbox_ext_buf_ctx.ext_dmabuf_list,
4111 struct lpfc_dmabuf, list);
4112 list_del_init(&dmabuf->list);
4114 /* after dma buffer descriptor setup */
4115 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, phba->mbox_ext_buf_ctx.nembType,
4116 mbox_rd, dma_ebuf, sta_pos_addr,
4119 pbuf = (uint8_t *)dmabuf->virt;
4120 job->reply->reply_payload_rcv_len =
4121 sg_copy_from_buffer(job->reply_payload.sg_list,
4122 job->reply_payload.sg_cnt,
4125 lpfc_bsg_dma_page_free(phba, dmabuf);
4127 if (phba->mbox_ext_buf_ctx.seqNum == phba->mbox_ext_buf_ctx.numBuf) {
4128 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4129 "2965 SLI_CONFIG (hbd) ext-buffer rd mbox "
4130 "command session done\n");
4131 lpfc_bsg_mbox_ext_session_reset(phba);
4134 job->reply->result = 0;
4137 return SLI_CONFIG_HANDLED;
4141 * lpfc_bsg_write_ebuf_set - set the next mailbox write external buffer
4142 * @phba: Pointer to HBA context object.
4143 * @dmabuf: Pointer to a DMA buffer descriptor.
4145 * This routine sets up the next mailbox read external buffer obtained
4146 * from user space through BSG.
4149 lpfc_bsg_write_ebuf_set(struct lpfc_hba *phba, struct fc_bsg_job *job,
4150 struct lpfc_dmabuf *dmabuf)
4152 struct lpfc_sli_config_mbox *sli_cfg_mbx;
4153 struct bsg_job_data *dd_data = NULL;
4154 LPFC_MBOXQ_t *pmboxq = NULL;
4156 enum nemb_type nemb_tp;
4162 index = phba->mbox_ext_buf_ctx.seqNum;
4163 phba->mbox_ext_buf_ctx.seqNum++;
4164 nemb_tp = phba->mbox_ext_buf_ctx.nembType;
4166 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)
4167 phba->mbox_ext_buf_ctx.mbx_dmabuf->virt;
4169 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
4175 pbuf = (uint8_t *)dmabuf->virt;
4176 size = job->request_payload.payload_len;
4177 sg_copy_to_buffer(job->request_payload.sg_list,
4178 job->request_payload.sg_cnt,
4181 if (phba->mbox_ext_buf_ctx.nembType == nemb_mse) {
4182 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4183 "2966 SLI_CONFIG (mse) ext-buffer wr set "
4184 "buffer[%d], size:%d\n",
4185 phba->mbox_ext_buf_ctx.seqNum, size);
4188 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4189 "2967 SLI_CONFIG (hbd) ext-buffer wr set "
4190 "buffer[%d], size:%d\n",
4191 phba->mbox_ext_buf_ctx.seqNum, size);
4195 /* set up external buffer descriptor and add to external buffer list */
4196 lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, index,
4197 phba->mbox_ext_buf_ctx.mbx_dmabuf,
4199 list_add_tail(&dmabuf->list, &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
4201 /* after write dma buffer */
4202 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, phba->mbox_ext_buf_ctx.nembType,
4203 mbox_wr, dma_ebuf, sta_pos_addr,
4206 if (phba->mbox_ext_buf_ctx.seqNum == phba->mbox_ext_buf_ctx.numBuf) {
4207 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4208 "2968 SLI_CONFIG ext-buffer wr all %d "
4209 "ebuffers received\n",
4210 phba->mbox_ext_buf_ctx.numBuf);
4211 /* mailbox command structure for base driver */
4212 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4217 memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
4218 pbuf = (uint8_t *)phba->mbox_ext_buf_ctx.mbx_dmabuf->virt;
4219 pmb = &pmboxq->u.mb;
4220 memcpy(pmb, pbuf, sizeof(*pmb));
4221 pmb->mbxOwner = OWN_HOST;
4222 pmboxq->vport = phba->pport;
4224 /* callback for multi-buffer write mailbox command */
4225 pmboxq->mbox_cmpl = lpfc_bsg_issue_write_mbox_ext_cmpl;
4227 /* context fields to callback function */
4228 pmboxq->context1 = dd_data;
4229 dd_data->type = TYPE_MBOX;
4230 dd_data->context_un.mbox.pmboxq = pmboxq;
4231 dd_data->context_un.mbox.mb = (MAILBOX_t *)pbuf;
4232 dd_data->context_un.mbox.set_job = job;
4233 job->dd_data = dd_data;
4236 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
4238 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
4239 if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
4240 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4241 "2969 Issued SLI_CONFIG ext-buffer "
4242 "maibox command, rc:x%x\n", rc);
4243 return SLI_CONFIG_HANDLED;
4245 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4246 "2970 Failed to issue SLI_CONFIG ext-buffer "
4247 "maibox command, rc:x%x\n", rc);
4252 /* wait for additoinal external buffers */
4253 job->reply->result = 0;
4255 return SLI_CONFIG_HANDLED;
4258 lpfc_bsg_dma_page_free(phba, dmabuf);
4265 * lpfc_bsg_handle_sli_cfg_ebuf - handle ext buffer with sli-cfg mailbox cmd
4266 * @phba: Pointer to HBA context object.
4267 * @mb: Pointer to a BSG mailbox object.
4268 * @dmabuff: Pointer to a DMA buffer descriptor.
4270 * This routine handles the external buffer with SLI_CONFIG (0x9B) mailbox
4271 * command with multiple non-embedded external buffers.
4274 lpfc_bsg_handle_sli_cfg_ebuf(struct lpfc_hba *phba, struct fc_bsg_job *job,
4275 struct lpfc_dmabuf *dmabuf)
4279 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4280 "2971 SLI_CONFIG buffer (type:x%x)\n",
4281 phba->mbox_ext_buf_ctx.mboxType);
4283 if (phba->mbox_ext_buf_ctx.mboxType == mbox_rd) {
4284 if (phba->mbox_ext_buf_ctx.state != LPFC_BSG_MBOX_DONE) {
4285 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4286 "2972 SLI_CONFIG rd buffer state "
4288 phba->mbox_ext_buf_ctx.state);
4289 lpfc_bsg_mbox_ext_abort(phba);
4292 rc = lpfc_bsg_read_ebuf_get(phba, job);
4293 if (rc == SLI_CONFIG_HANDLED)
4294 lpfc_bsg_dma_page_free(phba, dmabuf);
4295 } else { /* phba->mbox_ext_buf_ctx.mboxType == mbox_wr */
4296 if (phba->mbox_ext_buf_ctx.state != LPFC_BSG_MBOX_HOST) {
4297 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4298 "2973 SLI_CONFIG wr buffer state "
4300 phba->mbox_ext_buf_ctx.state);
4301 lpfc_bsg_mbox_ext_abort(phba);
4304 rc = lpfc_bsg_write_ebuf_set(phba, job, dmabuf);
4310 * lpfc_bsg_handle_sli_cfg_ext - handle sli-cfg mailbox with external buffer
4311 * @phba: Pointer to HBA context object.
4312 * @mb: Pointer to a BSG mailbox object.
4313 * @dmabuff: Pointer to a DMA buffer descriptor.
4315 * This routine checkes and handles non-embedded multi-buffer SLI_CONFIG
4316 * (0x9B) mailbox commands and external buffers.
4319 lpfc_bsg_handle_sli_cfg_ext(struct lpfc_hba *phba, struct fc_bsg_job *job,
4320 struct lpfc_dmabuf *dmabuf)
4322 struct dfc_mbox_req *mbox_req;
4323 int rc = SLI_CONFIG_NOT_HANDLED;
4326 (struct dfc_mbox_req *)job->request->rqst_data.h_vendor.vendor_cmd;
4328 /* mbox command with/without single external buffer */
4329 if (mbox_req->extMboxTag == 0 && mbox_req->extSeqNum == 0)
4332 /* mbox command and first external buffer */
4333 if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_IDLE) {
4334 if (mbox_req->extSeqNum == 1) {
4335 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4336 "2974 SLI_CONFIG mailbox: tag:%d, "
4337 "seq:%d\n", mbox_req->extMboxTag,
4338 mbox_req->extSeqNum);
4339 rc = lpfc_bsg_handle_sli_cfg_mbox(phba, job, dmabuf);
4342 goto sli_cfg_ext_error;
4346 * handle additional external buffers
4349 /* check broken pipe conditions */
4350 if (mbox_req->extMboxTag != phba->mbox_ext_buf_ctx.mbxTag)
4351 goto sli_cfg_ext_error;
4352 if (mbox_req->extSeqNum > phba->mbox_ext_buf_ctx.numBuf)
4353 goto sli_cfg_ext_error;
4354 if (mbox_req->extSeqNum != phba->mbox_ext_buf_ctx.seqNum + 1)
4355 goto sli_cfg_ext_error;
4357 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4358 "2975 SLI_CONFIG mailbox external buffer: "
4359 "extSta:x%x, tag:%d, seq:%d\n",
4360 phba->mbox_ext_buf_ctx.state, mbox_req->extMboxTag,
4361 mbox_req->extSeqNum);
4362 rc = lpfc_bsg_handle_sli_cfg_ebuf(phba, job, dmabuf);
4366 /* all other cases, broken pipe */
4367 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4368 "2976 SLI_CONFIG mailbox broken pipe: "
4369 "ctxSta:x%x, ctxNumBuf:%d "
4370 "ctxTag:%d, ctxSeq:%d, tag:%d, seq:%d\n",
4371 phba->mbox_ext_buf_ctx.state,
4372 phba->mbox_ext_buf_ctx.numBuf,
4373 phba->mbox_ext_buf_ctx.mbxTag,
4374 phba->mbox_ext_buf_ctx.seqNum,
4375 mbox_req->extMboxTag, mbox_req->extSeqNum);
4377 lpfc_bsg_mbox_ext_session_reset(phba);
4383 * lpfc_bsg_issue_mbox - issues a mailbox command on behalf of an app
4384 * @phba: Pointer to HBA context object.
4385 * @mb: Pointer to a mailbox object.
4386 * @vport: Pointer to a vport object.
4388 * Allocate a tracking object, mailbox command memory, get a mailbox
4389 * from the mailbox pool, copy the caller mailbox command.
4391 * If offline and the sli is active we need to poll for the command (port is
4392 * being reset) and com-plete the job, otherwise issue the mailbox command and
4393 * let our completion handler finish the command.
4396 lpfc_bsg_issue_mbox(struct lpfc_hba *phba, struct fc_bsg_job *job,
4397 struct lpfc_vport *vport)
4399 LPFC_MBOXQ_t *pmboxq = NULL; /* internal mailbox queue */
4400 MAILBOX_t *pmb; /* shortcut to the pmboxq mailbox */
4401 /* a 4k buffer to hold the mb and extended data from/to the bsg */
4402 uint8_t *pmbx = NULL;
4403 struct bsg_job_data *dd_data = NULL; /* bsg data tracking structure */
4404 struct lpfc_dmabuf *dmabuf = NULL;
4405 struct dfc_mbox_req *mbox_req;
4406 struct READ_EVENT_LOG_VAR *rdEventLog;
4407 uint32_t transmit_length, receive_length, mode;
4408 struct lpfc_mbx_sli4_config *sli4_config;
4409 struct lpfc_mbx_nembed_cmd *nembed_sge;
4410 struct mbox_header *header;
4411 struct ulp_bde64 *bde;
4412 uint8_t *ext = NULL;
4418 /* in case no data is transferred */
4419 job->reply->reply_payload_rcv_len = 0;
4421 /* sanity check to protect driver */
4422 if (job->reply_payload.payload_len > BSG_MBOX_SIZE ||
4423 job->request_payload.payload_len > BSG_MBOX_SIZE) {
4429 * Don't allow mailbox commands to be sent when blocked or when in
4430 * the middle of discovery
4432 if (phba->sli.sli_flag & LPFC_BLOCK_MGMT_IO) {
4438 (struct dfc_mbox_req *)job->request->rqst_data.h_vendor.vendor_cmd;
4440 /* check if requested extended data lengths are valid */
4441 if ((mbox_req->inExtWLen > BSG_MBOX_SIZE/sizeof(uint32_t)) ||
4442 (mbox_req->outExtWLen > BSG_MBOX_SIZE/sizeof(uint32_t))) {
4447 dmabuf = lpfc_bsg_dma_page_alloc(phba);
4448 if (!dmabuf || !dmabuf->virt) {
4453 /* Get the mailbox command or external buffer from BSG */
4454 pmbx = (uint8_t *)dmabuf->virt;
4455 size = job->request_payload.payload_len;
4456 sg_copy_to_buffer(job->request_payload.sg_list,
4457 job->request_payload.sg_cnt, pmbx, size);
4459 /* Handle possible SLI_CONFIG with non-embedded payloads */
4460 if (phba->sli_rev == LPFC_SLI_REV4) {
4461 rc = lpfc_bsg_handle_sli_cfg_ext(phba, job, dmabuf);
4462 if (rc == SLI_CONFIG_HANDLED)
4466 /* SLI_CONFIG_NOT_HANDLED for other mailbox commands */
4469 rc = lpfc_bsg_check_cmd_access(phba, (MAILBOX_t *)pmbx, vport);
4471 goto job_done; /* must be negative */
4473 /* allocate our bsg tracking structure */
4474 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
4476 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
4477 "2727 Failed allocation of dd_data\n");
4482 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4487 memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
4489 pmb = &pmboxq->u.mb;
4490 memcpy(pmb, pmbx, sizeof(*pmb));
4491 pmb->mbxOwner = OWN_HOST;
4492 pmboxq->vport = vport;
4494 /* If HBA encountered an error attention, allow only DUMP
4495 * or RESTART mailbox commands until the HBA is restarted.
4497 if (phba->pport->stopped &&
4498 pmb->mbxCommand != MBX_DUMP_MEMORY &&
4499 pmb->mbxCommand != MBX_RESTART &&
4500 pmb->mbxCommand != MBX_WRITE_VPARMS &&
4501 pmb->mbxCommand != MBX_WRITE_WWN)
4502 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
4503 "2797 mbox: Issued mailbox cmd "
4504 "0x%x while in stopped state.\n",
4507 /* extended mailbox commands will need an extended buffer */
4508 if (mbox_req->inExtWLen || mbox_req->outExtWLen) {
4510 ext = from + sizeof(MAILBOX_t);
4511 pmboxq->context2 = ext;
4512 pmboxq->in_ext_byte_len =
4513 mbox_req->inExtWLen * sizeof(uint32_t);
4514 pmboxq->out_ext_byte_len =
4515 mbox_req->outExtWLen * sizeof(uint32_t);
4516 pmboxq->mbox_offset_word = mbox_req->mbOffset;
4519 /* biu diag will need a kernel buffer to transfer the data
4520 * allocate our own buffer and setup the mailbox command to
4523 if (pmb->mbxCommand == MBX_RUN_BIU_DIAG64) {
4524 transmit_length = pmb->un.varWords[1];
4525 receive_length = pmb->un.varWords[4];
4526 /* transmit length cannot be greater than receive length or
4527 * mailbox extension size
4529 if ((transmit_length > receive_length) ||
4530 (transmit_length > BSG_MBOX_SIZE - sizeof(MAILBOX_t))) {
4534 pmb->un.varBIUdiag.un.s2.xmit_bde64.addrHigh =
4535 putPaddrHigh(dmabuf->phys + sizeof(MAILBOX_t));
4536 pmb->un.varBIUdiag.un.s2.xmit_bde64.addrLow =
4537 putPaddrLow(dmabuf->phys + sizeof(MAILBOX_t));
4539 pmb->un.varBIUdiag.un.s2.rcv_bde64.addrHigh =
4540 putPaddrHigh(dmabuf->phys + sizeof(MAILBOX_t)
4541 + pmb->un.varBIUdiag.un.s2.xmit_bde64.tus.f.bdeSize);
4542 pmb->un.varBIUdiag.un.s2.rcv_bde64.addrLow =
4543 putPaddrLow(dmabuf->phys + sizeof(MAILBOX_t)
4544 + pmb->un.varBIUdiag.un.s2.xmit_bde64.tus.f.bdeSize);
4545 } else if (pmb->mbxCommand == MBX_READ_EVENT_LOG) {
4546 rdEventLog = &pmb->un.varRdEventLog;
4547 receive_length = rdEventLog->rcv_bde64.tus.f.bdeSize;
4548 mode = bf_get(lpfc_event_log, rdEventLog);
4550 /* receive length cannot be greater than mailbox
4553 if (receive_length > BSG_MBOX_SIZE - sizeof(MAILBOX_t)) {
4558 /* mode zero uses a bde like biu diags command */
4560 pmb->un.varWords[3] = putPaddrLow(dmabuf->phys
4561 + sizeof(MAILBOX_t));
4562 pmb->un.varWords[4] = putPaddrHigh(dmabuf->phys
4563 + sizeof(MAILBOX_t));
4565 } else if (phba->sli_rev == LPFC_SLI_REV4) {
4566 /* Let type 4 (well known data) through because the data is
4567 * returned in varwords[4-8]
4568 * otherwise check the recieve length and fetch the buffer addr
4570 if ((pmb->mbxCommand == MBX_DUMP_MEMORY) &&
4571 (pmb->un.varDmp.type != DMP_WELL_KNOWN)) {
4572 /* rebuild the command for sli4 using our own buffers
4573 * like we do for biu diags
4575 receive_length = pmb->un.varWords[2];
4576 /* receive length cannot be greater than mailbox
4579 if (receive_length == 0) {
4583 pmb->un.varWords[3] = putPaddrLow(dmabuf->phys
4584 + sizeof(MAILBOX_t));
4585 pmb->un.varWords[4] = putPaddrHigh(dmabuf->phys
4586 + sizeof(MAILBOX_t));
4587 } else if ((pmb->mbxCommand == MBX_UPDATE_CFG) &&
4588 pmb->un.varUpdateCfg.co) {
4589 bde = (struct ulp_bde64 *)&pmb->un.varWords[4];
4591 /* bde size cannot be greater than mailbox ext size */
4592 if (bde->tus.f.bdeSize >
4593 BSG_MBOX_SIZE - sizeof(MAILBOX_t)) {
4597 bde->addrHigh = putPaddrHigh(dmabuf->phys
4598 + sizeof(MAILBOX_t));
4599 bde->addrLow = putPaddrLow(dmabuf->phys
4600 + sizeof(MAILBOX_t));
4601 } else if (pmb->mbxCommand == MBX_SLI4_CONFIG) {
4602 /* Handling non-embedded SLI_CONFIG mailbox command */
4603 sli4_config = &pmboxq->u.mqe.un.sli4_config;
4604 if (!bf_get(lpfc_mbox_hdr_emb,
4605 &sli4_config->header.cfg_mhdr)) {
4606 /* rebuild the command for sli4 using our
4607 * own buffers like we do for biu diags
4609 header = (struct mbox_header *)
4610 &pmb->un.varWords[0];
4611 nembed_sge = (struct lpfc_mbx_nembed_cmd *)
4612 &pmb->un.varWords[0];
4613 receive_length = nembed_sge->sge[0].length;
4615 /* receive length cannot be greater than
4616 * mailbox extension size
4618 if ((receive_length == 0) ||
4620 BSG_MBOX_SIZE - sizeof(MAILBOX_t))) {
4625 nembed_sge->sge[0].pa_hi =
4626 putPaddrHigh(dmabuf->phys
4627 + sizeof(MAILBOX_t));
4628 nembed_sge->sge[0].pa_lo =
4629 putPaddrLow(dmabuf->phys
4630 + sizeof(MAILBOX_t));
4635 dd_data->context_un.mbox.dmabuffers = dmabuf;
4637 /* setup wake call as IOCB callback */
4638 pmboxq->mbox_cmpl = lpfc_bsg_issue_mbox_cmpl;
4640 /* setup context field to pass wait_queue pointer to wake function */
4641 pmboxq->context1 = dd_data;
4642 dd_data->type = TYPE_MBOX;
4643 dd_data->context_un.mbox.pmboxq = pmboxq;
4644 dd_data->context_un.mbox.mb = (MAILBOX_t *)pmbx;
4645 dd_data->context_un.mbox.set_job = job;
4646 dd_data->context_un.mbox.ext = ext;
4647 dd_data->context_un.mbox.mbOffset = mbox_req->mbOffset;
4648 dd_data->context_un.mbox.inExtWLen = mbox_req->inExtWLen;
4649 dd_data->context_un.mbox.outExtWLen = mbox_req->outExtWLen;
4650 job->dd_data = dd_data;
4652 if ((vport->fc_flag & FC_OFFLINE_MODE) ||
4653 (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))) {
4654 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
4655 if (rc != MBX_SUCCESS) {
4656 rc = (rc == MBX_TIMEOUT) ? -ETIME : -ENODEV;
4660 /* job finished, copy the data */
4661 memcpy(pmbx, pmb, sizeof(*pmb));
4662 job->reply->reply_payload_rcv_len =
4663 sg_copy_from_buffer(job->reply_payload.sg_list,
4664 job->reply_payload.sg_cnt,
4666 /* not waiting mbox already done */
4671 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
4672 if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY))
4673 return 1; /* job started */
4676 /* common exit for error or job completed inline */
4678 mempool_free(pmboxq, phba->mbox_mem_pool);
4679 lpfc_bsg_dma_page_free(phba, dmabuf);
4687 * lpfc_bsg_mbox_cmd - process an fc bsg LPFC_BSG_VENDOR_MBOX command
4688 * @job: MBOX fc_bsg_job for LPFC_BSG_VENDOR_MBOX.
4691 lpfc_bsg_mbox_cmd(struct fc_bsg_job *job)
4693 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
4694 struct lpfc_hba *phba = vport->phba;
4695 struct dfc_mbox_req *mbox_req;
4698 /* mix-and-match backward compatibility */
4699 job->reply->reply_payload_rcv_len = 0;
4700 if (job->request_len <
4701 sizeof(struct fc_bsg_request) + sizeof(struct dfc_mbox_req)) {
4702 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4703 "2737 Mix-and-match backward compability "
4704 "between MBOX_REQ old size:%d and "
4705 "new request size:%d\n",
4706 (int)(job->request_len -
4707 sizeof(struct fc_bsg_request)),
4708 (int)sizeof(struct dfc_mbox_req));
4709 mbox_req = (struct dfc_mbox_req *)
4710 job->request->rqst_data.h_vendor.vendor_cmd;
4711 mbox_req->extMboxTag = 0;
4712 mbox_req->extSeqNum = 0;
4715 rc = lpfc_bsg_issue_mbox(phba, job, vport);
4719 job->reply->result = 0;
4720 job->dd_data = NULL;
4723 /* job submitted, will complete later*/
4724 rc = 0; /* return zero, no error */
4726 /* some error occurred */
4727 job->reply->result = rc;
4728 job->dd_data = NULL;
4735 * lpfc_bsg_menlo_cmd_cmp - lpfc_menlo_cmd completion handler
4736 * @phba: Pointer to HBA context object.
4737 * @cmdiocbq: Pointer to command iocb.
4738 * @rspiocbq: Pointer to response iocb.
4740 * This function is the completion handler for iocbs issued using
4741 * lpfc_menlo_cmd function. This function is called by the
4742 * ring event handler function without any lock held. This function
4743 * can be called from both worker thread context and interrupt
4744 * context. This function also can be called from another thread which
4745 * cleans up the SLI layer objects.
4746 * This function copies the contents of the response iocb to the
4747 * response iocb memory object provided by the caller of
4748 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
4749 * sleeps for the iocb completion.
4752 lpfc_bsg_menlo_cmd_cmp(struct lpfc_hba *phba,
4753 struct lpfc_iocbq *cmdiocbq,
4754 struct lpfc_iocbq *rspiocbq)
4756 struct bsg_job_data *dd_data;
4757 struct fc_bsg_job *job;
4759 struct lpfc_dmabuf *bmp;
4760 struct lpfc_bsg_menlo *menlo;
4761 unsigned long flags;
4762 struct menlo_response *menlo_resp;
4765 spin_lock_irqsave(&phba->ct_ev_lock, flags);
4766 dd_data = cmdiocbq->context1;
4768 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
4772 menlo = &dd_data->context_un.menlo;
4773 job = menlo->set_job;
4774 job->dd_data = NULL; /* so timeout handler does not reply */
4776 spin_lock(&phba->hbalock);
4777 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
4778 if (cmdiocbq->context2 && rspiocbq)
4779 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
4780 &rspiocbq->iocb, sizeof(IOCB_t));
4781 spin_unlock(&phba->hbalock);
4784 rspiocbq = menlo->rspiocbq;
4785 rsp = &rspiocbq->iocb;
4787 pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
4788 job->request_payload.sg_cnt, DMA_TO_DEVICE);
4789 pci_unmap_sg(phba->pcidev, job->reply_payload.sg_list,
4790 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
4792 /* always return the xri, this would be used in the case
4793 * of a menlo download to allow the data to be sent as a continuation
4796 menlo_resp = (struct menlo_response *)
4797 job->reply->reply_data.vendor_reply.vendor_rsp;
4798 menlo_resp->xri = rsp->ulpContext;
4799 if (rsp->ulpStatus) {
4800 if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
4801 switch (rsp->un.ulpWord[4] & IOERR_PARAM_MASK) {
4802 case IOERR_SEQUENCE_TIMEOUT:
4805 case IOERR_INVALID_RPI:
4815 job->reply->reply_payload_rcv_len =
4816 rsp->un.genreq64.bdl.bdeSize;
4818 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
4819 lpfc_sli_release_iocbq(phba, rspiocbq);
4820 lpfc_sli_release_iocbq(phba, cmdiocbq);
4823 /* make error code available to userspace */
4824 job->reply->result = rc;
4825 /* complete the job back to userspace */
4827 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
4832 * lpfc_menlo_cmd - send an ioctl for menlo hardware
4833 * @job: fc_bsg_job to handle
4835 * This function issues a gen request 64 CR ioctl for all menlo cmd requests,
4836 * all the command completions will return the xri for the command.
4837 * For menlo data requests a gen request 64 CX is used to continue the exchange
4838 * supplied in the menlo request header xri field.
4841 lpfc_menlo_cmd(struct fc_bsg_job *job)
4843 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
4844 struct lpfc_hba *phba = vport->phba;
4845 struct lpfc_iocbq *cmdiocbq, *rspiocbq;
4848 struct menlo_command *menlo_cmd;
4849 struct menlo_response *menlo_resp;
4850 struct lpfc_dmabuf *bmp = NULL;
4853 struct scatterlist *sgel = NULL;
4856 struct bsg_job_data *dd_data;
4857 struct ulp_bde64 *bpl = NULL;
4859 /* in case no data is returned return just the return code */
4860 job->reply->reply_payload_rcv_len = 0;
4862 if (job->request_len <
4863 sizeof(struct fc_bsg_request) +
4864 sizeof(struct menlo_command)) {
4865 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
4866 "2784 Received MENLO_CMD request below "
4872 if (job->reply_len <
4873 sizeof(struct fc_bsg_request) + sizeof(struct menlo_response)) {
4874 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
4875 "2785 Received MENLO_CMD reply below "
4881 if (!(phba->menlo_flag & HBA_MENLO_SUPPORT)) {
4882 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
4883 "2786 Adapter does not support menlo "
4889 menlo_cmd = (struct menlo_command *)
4890 job->request->rqst_data.h_vendor.vendor_cmd;
4892 menlo_resp = (struct menlo_response *)
4893 job->reply->reply_data.vendor_reply.vendor_rsp;
4895 /* allocate our bsg tracking structure */
4896 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
4898 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
4899 "2787 Failed allocation of dd_data\n");
4904 bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4910 cmdiocbq = lpfc_sli_get_iocbq(phba);
4916 rspiocbq = lpfc_sli_get_iocbq(phba);
4922 rsp = &rspiocbq->iocb;
4924 bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
4930 INIT_LIST_HEAD(&bmp->list);
4931 bpl = (struct ulp_bde64 *) bmp->virt;
4932 request_nseg = pci_map_sg(phba->pcidev, job->request_payload.sg_list,
4933 job->request_payload.sg_cnt, DMA_TO_DEVICE);
4934 for_each_sg(job->request_payload.sg_list, sgel, request_nseg, numbde) {
4935 busaddr = sg_dma_address(sgel);
4936 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
4937 bpl->tus.f.bdeSize = sg_dma_len(sgel);
4938 bpl->tus.w = cpu_to_le32(bpl->tus.w);
4939 bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
4940 bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
4944 reply_nseg = pci_map_sg(phba->pcidev, job->reply_payload.sg_list,
4945 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
4946 for_each_sg(job->reply_payload.sg_list, sgel, reply_nseg, numbde) {
4947 busaddr = sg_dma_address(sgel);
4948 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
4949 bpl->tus.f.bdeSize = sg_dma_len(sgel);
4950 bpl->tus.w = cpu_to_le32(bpl->tus.w);
4951 bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
4952 bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
4956 cmd = &cmdiocbq->iocb;
4957 cmd->un.genreq64.bdl.ulpIoTag32 = 0;
4958 cmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
4959 cmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
4960 cmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
4961 cmd->un.genreq64.bdl.bdeSize =
4962 (request_nseg + reply_nseg) * sizeof(struct ulp_bde64);
4963 cmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
4964 cmd->un.genreq64.w5.hcsw.Dfctl = 0;
4965 cmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CMD;
4966 cmd->un.genreq64.w5.hcsw.Type = MENLO_TRANSPORT_TYPE; /* 0xfe */
4967 cmd->ulpBdeCount = 1;
4968 cmd->ulpClass = CLASS3;
4969 cmd->ulpOwner = OWN_CHIP;
4970 cmd->ulpLe = 1; /* Limited Edition */
4971 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
4972 cmdiocbq->vport = phba->pport;
4973 /* We want the firmware to timeout before we do */
4974 cmd->ulpTimeout = MENLO_TIMEOUT - 5;
4975 cmdiocbq->context3 = bmp;
4976 cmdiocbq->context2 = rspiocbq;
4977 cmdiocbq->iocb_cmpl = lpfc_bsg_menlo_cmd_cmp;
4978 cmdiocbq->context1 = dd_data;
4979 cmdiocbq->context2 = rspiocbq;
4980 if (menlo_cmd->cmd == LPFC_BSG_VENDOR_MENLO_CMD) {
4981 cmd->ulpCommand = CMD_GEN_REQUEST64_CR;
4982 cmd->ulpPU = MENLO_PU; /* 3 */
4983 cmd->un.ulpWord[4] = MENLO_DID; /* 0x0000FC0E */
4984 cmd->ulpContext = MENLO_CONTEXT; /* 0 */
4986 cmd->ulpCommand = CMD_GEN_REQUEST64_CX;
4988 cmd->un.ulpWord[4] = 0;
4989 cmd->ulpContext = menlo_cmd->xri;
4992 dd_data->type = TYPE_MENLO;
4993 dd_data->context_un.menlo.cmdiocbq = cmdiocbq;
4994 dd_data->context_un.menlo.rspiocbq = rspiocbq;
4995 dd_data->context_un.menlo.set_job = job;
4996 dd_data->context_un.menlo.bmp = bmp;
4998 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq,
5000 if (rc == IOCB_SUCCESS)
5001 return 0; /* done for now */
5003 /* iocb failed so cleanup */
5004 pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
5005 job->request_payload.sg_cnt, DMA_TO_DEVICE);
5006 pci_unmap_sg(phba->pcidev, job->reply_payload.sg_list,
5007 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
5009 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
5012 lpfc_sli_release_iocbq(phba, rspiocbq);
5014 lpfc_sli_release_iocbq(phba, cmdiocbq);
5020 /* make error code available to userspace */
5021 job->reply->result = rc;
5022 job->dd_data = NULL;
5027 * lpfc_bsg_hst_vendor - process a vendor-specific fc_bsg_job
5028 * @job: fc_bsg_job to handle
5031 lpfc_bsg_hst_vendor(struct fc_bsg_job *job)
5033 int command = job->request->rqst_data.h_vendor.vendor_cmd[0];
5037 case LPFC_BSG_VENDOR_SET_CT_EVENT:
5038 rc = lpfc_bsg_hba_set_event(job);
5040 case LPFC_BSG_VENDOR_GET_CT_EVENT:
5041 rc = lpfc_bsg_hba_get_event(job);
5043 case LPFC_BSG_VENDOR_SEND_MGMT_RESP:
5044 rc = lpfc_bsg_send_mgmt_rsp(job);
5046 case LPFC_BSG_VENDOR_DIAG_MODE:
5047 rc = lpfc_bsg_diag_loopback_mode(job);
5049 case LPFC_BSG_VENDOR_DIAG_MODE_END:
5050 rc = lpfc_sli4_bsg_diag_mode_end(job);
5052 case LPFC_BSG_VENDOR_DIAG_RUN_LOOPBACK:
5053 rc = lpfc_bsg_diag_loopback_run(job);
5055 case LPFC_BSG_VENDOR_LINK_DIAG_TEST:
5056 rc = lpfc_sli4_bsg_link_diag_test(job);
5058 case LPFC_BSG_VENDOR_GET_MGMT_REV:
5059 rc = lpfc_bsg_get_dfc_rev(job);
5061 case LPFC_BSG_VENDOR_MBOX:
5062 rc = lpfc_bsg_mbox_cmd(job);
5064 case LPFC_BSG_VENDOR_MENLO_CMD:
5065 case LPFC_BSG_VENDOR_MENLO_DATA:
5066 rc = lpfc_menlo_cmd(job);
5070 job->reply->reply_payload_rcv_len = 0;
5071 /* make error code available to userspace */
5072 job->reply->result = rc;
5080 * lpfc_bsg_request - handle a bsg request from the FC transport
5081 * @job: fc_bsg_job to handle
5084 lpfc_bsg_request(struct fc_bsg_job *job)
5089 msgcode = job->request->msgcode;
5091 case FC_BSG_HST_VENDOR:
5092 rc = lpfc_bsg_hst_vendor(job);
5094 case FC_BSG_RPT_ELS:
5095 rc = lpfc_bsg_rport_els(job);
5098 rc = lpfc_bsg_send_mgmt_cmd(job);
5102 job->reply->reply_payload_rcv_len = 0;
5103 /* make error code available to userspace */
5104 job->reply->result = rc;
5112 * lpfc_bsg_timeout - handle timeout of a bsg request from the FC transport
5113 * @job: fc_bsg_job that has timed out
5115 * This function just aborts the job's IOCB. The aborted IOCB will return to
5116 * the waiting function which will handle passing the error back to userspace
5119 lpfc_bsg_timeout(struct fc_bsg_job *job)
5121 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
5122 struct lpfc_hba *phba = vport->phba;
5123 struct lpfc_iocbq *cmdiocb;
5124 struct lpfc_bsg_event *evt;
5125 struct lpfc_bsg_iocb *iocb;
5126 struct lpfc_bsg_mbox *mbox;
5127 struct lpfc_bsg_menlo *menlo;
5128 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
5129 struct bsg_job_data *dd_data;
5130 unsigned long flags;
5132 spin_lock_irqsave(&phba->ct_ev_lock, flags);
5133 dd_data = (struct bsg_job_data *)job->dd_data;
5134 /* timeout and completion crossed paths if no dd_data */
5136 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5140 switch (dd_data->type) {
5142 iocb = &dd_data->context_un.iocb;
5143 cmdiocb = iocb->cmdiocbq;
5144 /* hint to completion handler that the job timed out */
5145 job->reply->result = -EAGAIN;
5146 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5147 /* this will call our completion handler */
5148 spin_lock_irq(&phba->hbalock);
5149 lpfc_sli_issue_abort_iotag(phba, pring, cmdiocb);
5150 spin_unlock_irq(&phba->hbalock);
5153 evt = dd_data->context_un.evt;
5154 /* this event has no job anymore */
5155 evt->set_job = NULL;
5156 job->dd_data = NULL;
5157 job->reply->reply_payload_rcv_len = 0;
5158 /* Return -EAGAIN which is our way of signallying the
5161 job->reply->result = -EAGAIN;
5162 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5166 mbox = &dd_data->context_un.mbox;
5167 /* this mbox has no job anymore */
5168 mbox->set_job = NULL;
5169 job->dd_data = NULL;
5170 job->reply->reply_payload_rcv_len = 0;
5171 job->reply->result = -EAGAIN;
5172 /* the mbox completion handler can now be run */
5173 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5175 if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_PORT)
5176 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_ABTS;
5179 menlo = &dd_data->context_un.menlo;
5180 cmdiocb = menlo->cmdiocbq;
5181 /* hint to completion handler that the job timed out */
5182 job->reply->result = -EAGAIN;
5183 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5184 /* this will call our completion handler */
5185 spin_lock_irq(&phba->hbalock);
5186 lpfc_sli_issue_abort_iotag(phba, pring, cmdiocb);
5187 spin_unlock_irq(&phba->hbalock);
5190 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5194 /* scsi transport fc fc_bsg_job_timeout expects a zero return code,
5195 * otherwise an error message will be displayed on the console
5196 * so always return success (zero)