1 /*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
3 * Fibre Channel Host Bus Adapters. *
4 * Copyright (C) 2009-2010 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/delay.h>
26 #include <scsi/scsi.h>
27 #include <scsi/scsi_host.h>
28 #include <scsi/scsi_transport_fc.h>
29 #include <scsi/scsi_bsg_fc.h>
30 #include <scsi/fc/fc_fs.h>
35 #include "lpfc_sli4.h"
38 #include "lpfc_disc.h"
39 #include "lpfc_scsi.h"
41 #include "lpfc_logmsg.h"
42 #include "lpfc_crtn.h"
43 #include "lpfc_vport.h"
44 #include "lpfc_version.h"
46 struct lpfc_bsg_event {
47 struct list_head node;
51 /* Event type and waiter identifiers */
56 /* next two flags are here for the auto-delete logic */
57 unsigned long wait_time_stamp;
60 /* seen and not seen events */
61 struct list_head events_to_get;
62 struct list_head events_to_see;
64 /* job waiting for this event to finish */
65 struct fc_bsg_job *set_job;
68 struct lpfc_bsg_iocb {
69 struct lpfc_iocbq *cmdiocbq;
70 struct lpfc_iocbq *rspiocbq;
71 struct lpfc_dmabuf *bmp;
72 struct lpfc_nodelist *ndlp;
74 /* job waiting for this iocb to finish */
75 struct fc_bsg_job *set_job;
78 struct lpfc_bsg_mbox {
82 /* job waiting for this mbox command to finish */
83 struct fc_bsg_job *set_job;
86 #define MENLO_DID 0x0000FC0E
88 struct lpfc_bsg_menlo {
89 struct lpfc_iocbq *cmdiocbq;
90 struct lpfc_iocbq *rspiocbq;
91 struct lpfc_dmabuf *bmp;
93 /* job waiting for this iocb to finish */
94 struct fc_bsg_job *set_job;
101 struct bsg_job_data {
104 struct lpfc_bsg_event *evt;
105 struct lpfc_bsg_iocb iocb;
106 struct lpfc_bsg_mbox mbox;
107 struct lpfc_bsg_menlo menlo;
112 struct list_head node;
119 #define BUF_SZ_4K 4096
120 #define SLI_CT_ELX_LOOPBACK 0x10
122 enum ELX_LOOPBACK_CMD {
123 ELX_LOOPBACK_XRI_SETUP,
127 #define ELX_LOOPBACK_HEADER_SZ \
128 (size_t)(&((struct lpfc_sli_ct_request *)NULL)->un)
130 struct lpfc_dmabufext {
131 struct lpfc_dmabuf dma;
137 * lpfc_bsg_send_mgmt_cmd_cmp - lpfc_bsg_send_mgmt_cmd's completion handler
138 * @phba: Pointer to HBA context object.
139 * @cmdiocbq: Pointer to command iocb.
140 * @rspiocbq: Pointer to response iocb.
142 * This function is the completion handler for iocbs issued using
143 * lpfc_bsg_send_mgmt_cmd function. This function is called by the
144 * ring event handler function without any lock held. This function
145 * can be called from both worker thread context and interrupt
146 * context. This function also can be called from another thread which
147 * cleans up the SLI layer objects.
148 * This function copies the contents of the response iocb to the
149 * response iocb memory object provided by the caller of
150 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
151 * sleeps for the iocb completion.
154 lpfc_bsg_send_mgmt_cmd_cmp(struct lpfc_hba *phba,
155 struct lpfc_iocbq *cmdiocbq,
156 struct lpfc_iocbq *rspiocbq)
158 unsigned long iflags;
159 struct bsg_job_data *dd_data;
160 struct fc_bsg_job *job;
162 struct lpfc_dmabuf *bmp;
163 struct lpfc_nodelist *ndlp;
164 struct lpfc_bsg_iocb *iocb;
168 spin_lock_irqsave(&phba->ct_ev_lock, flags);
169 dd_data = cmdiocbq->context1;
171 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
175 iocb = &dd_data->context_un.iocb;
177 job->dd_data = NULL; /* so timeout handler does not reply */
179 spin_lock_irqsave(&phba->hbalock, iflags);
180 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
181 if (cmdiocbq->context2 && rspiocbq)
182 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
183 &rspiocbq->iocb, sizeof(IOCB_t));
184 spin_unlock_irqrestore(&phba->hbalock, iflags);
187 rspiocbq = iocb->rspiocbq;
188 rsp = &rspiocbq->iocb;
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] & 0xff) {
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, rspiocbq);
217 lpfc_sli_release_iocbq(phba, cmdiocbq);
221 /* make error code available to userspace */
222 job->reply->result = rc;
223 /* complete the job back to userspace */
225 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
230 * lpfc_bsg_send_mgmt_cmd - send a CT command from a bsg request
231 * @job: fc_bsg_job to handle
234 lpfc_bsg_send_mgmt_cmd(struct fc_bsg_job *job)
236 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
237 struct lpfc_hba *phba = vport->phba;
238 struct lpfc_rport_data *rdata = job->rport->dd_data;
239 struct lpfc_nodelist *ndlp = rdata->pnode;
240 struct ulp_bde64 *bpl = NULL;
242 struct lpfc_iocbq *cmdiocbq = NULL;
243 struct lpfc_iocbq *rspiocbq = NULL;
246 struct lpfc_dmabuf *bmp = NULL;
249 struct scatterlist *sgel = NULL;
252 struct bsg_job_data *dd_data;
256 /* in case no data is transferred */
257 job->reply->reply_payload_rcv_len = 0;
259 /* allocate our bsg tracking structure */
260 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
262 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
263 "2733 Failed allocation of dd_data\n");
268 if (!lpfc_nlp_get(ndlp)) {
273 bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
279 if (ndlp->nlp_flag & NLP_ELS_SND_MASK) {
284 cmdiocbq = lpfc_sli_get_iocbq(phba);
290 cmd = &cmdiocbq->iocb;
291 rspiocbq = lpfc_sli_get_iocbq(phba);
297 rsp = &rspiocbq->iocb;
298 bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
304 INIT_LIST_HEAD(&bmp->list);
305 bpl = (struct ulp_bde64 *) bmp->virt;
306 request_nseg = pci_map_sg(phba->pcidev, job->request_payload.sg_list,
307 job->request_payload.sg_cnt, DMA_TO_DEVICE);
308 for_each_sg(job->request_payload.sg_list, sgel, request_nseg, numbde) {
309 busaddr = sg_dma_address(sgel);
310 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
311 bpl->tus.f.bdeSize = sg_dma_len(sgel);
312 bpl->tus.w = cpu_to_le32(bpl->tus.w);
313 bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
314 bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
318 reply_nseg = pci_map_sg(phba->pcidev, job->reply_payload.sg_list,
319 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
320 for_each_sg(job->reply_payload.sg_list, sgel, reply_nseg, numbde) {
321 busaddr = sg_dma_address(sgel);
322 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
323 bpl->tus.f.bdeSize = sg_dma_len(sgel);
324 bpl->tus.w = cpu_to_le32(bpl->tus.w);
325 bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
326 bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
330 cmd->un.genreq64.bdl.ulpIoTag32 = 0;
331 cmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
332 cmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
333 cmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
334 cmd->un.genreq64.bdl.bdeSize =
335 (request_nseg + reply_nseg) * sizeof(struct ulp_bde64);
336 cmd->ulpCommand = CMD_GEN_REQUEST64_CR;
337 cmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
338 cmd->un.genreq64.w5.hcsw.Dfctl = 0;
339 cmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
340 cmd->un.genreq64.w5.hcsw.Type = FC_TYPE_CT;
341 cmd->ulpBdeCount = 1;
343 cmd->ulpClass = CLASS3;
344 cmd->ulpContext = ndlp->nlp_rpi;
345 cmd->ulpOwner = OWN_CHIP;
346 cmdiocbq->vport = phba->pport;
347 cmdiocbq->context3 = bmp;
348 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
349 timeout = phba->fc_ratov * 2;
350 cmd->ulpTimeout = timeout;
352 cmdiocbq->iocb_cmpl = lpfc_bsg_send_mgmt_cmd_cmp;
353 cmdiocbq->context1 = dd_data;
354 cmdiocbq->context2 = rspiocbq;
355 dd_data->type = TYPE_IOCB;
356 dd_data->context_un.iocb.cmdiocbq = cmdiocbq;
357 dd_data->context_un.iocb.rspiocbq = rspiocbq;
358 dd_data->context_un.iocb.set_job = job;
359 dd_data->context_un.iocb.bmp = bmp;
360 dd_data->context_un.iocb.ndlp = ndlp;
362 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
363 creg_val = readl(phba->HCregaddr);
364 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
365 writel(creg_val, phba->HCregaddr);
366 readl(phba->HCregaddr); /* flush */
369 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq, 0);
371 if (rc == IOCB_SUCCESS)
372 return 0; /* done for now */
374 /* iocb failed so cleanup */
375 pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
376 job->request_payload.sg_cnt, DMA_TO_DEVICE);
377 pci_unmap_sg(phba->pcidev, job->reply_payload.sg_list,
378 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
380 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
383 lpfc_sli_release_iocbq(phba, rspiocbq);
385 lpfc_sli_release_iocbq(phba, cmdiocbq);
393 /* make error code available to userspace */
394 job->reply->result = rc;
400 * lpfc_bsg_rport_els_cmp - lpfc_bsg_rport_els's completion handler
401 * @phba: Pointer to HBA context object.
402 * @cmdiocbq: Pointer to command iocb.
403 * @rspiocbq: Pointer to response iocb.
405 * This function is the completion handler for iocbs issued using
406 * lpfc_bsg_rport_els_cmp function. This function is called by the
407 * ring event handler function without any lock held. This function
408 * can be called from both worker thread context and interrupt
409 * context. This function also can be called from other thread which
410 * cleans up the SLI layer objects.
411 * This function copies the contents of the response iocb to the
412 * response iocb memory object provided by the caller of
413 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
414 * sleeps for the iocb completion.
417 lpfc_bsg_rport_els_cmp(struct lpfc_hba *phba,
418 struct lpfc_iocbq *cmdiocbq,
419 struct lpfc_iocbq *rspiocbq)
421 struct bsg_job_data *dd_data;
422 struct fc_bsg_job *job;
424 struct lpfc_nodelist *ndlp;
425 struct lpfc_dmabuf *pbuflist = NULL;
426 struct fc_bsg_ctels_reply *els_reply;
431 spin_lock_irqsave(&phba->ct_ev_lock, flags);
432 dd_data = cmdiocbq->context1;
433 /* normal completion and timeout crossed paths, already done */
435 spin_unlock_irqrestore(&phba->hbalock, flags);
439 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
440 if (cmdiocbq->context2 && rspiocbq)
441 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
442 &rspiocbq->iocb, sizeof(IOCB_t));
444 job = dd_data->context_un.iocb.set_job;
445 cmdiocbq = dd_data->context_un.iocb.cmdiocbq;
446 rspiocbq = dd_data->context_un.iocb.rspiocbq;
447 rsp = &rspiocbq->iocb;
448 ndlp = dd_data->context_un.iocb.ndlp;
450 pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
451 job->request_payload.sg_cnt, DMA_TO_DEVICE);
452 pci_unmap_sg(phba->pcidev, job->reply_payload.sg_list,
453 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
455 if (job->reply->result == -EAGAIN)
457 else if (rsp->ulpStatus == IOSTAT_SUCCESS)
458 job->reply->reply_payload_rcv_len =
459 rsp->un.elsreq64.bdl.bdeSize;
460 else if (rsp->ulpStatus == IOSTAT_LS_RJT) {
461 job->reply->reply_payload_rcv_len =
462 sizeof(struct fc_bsg_ctels_reply);
463 /* LS_RJT data returned in word 4 */
464 rjt_data = (uint8_t *)&rsp->un.ulpWord[4];
465 els_reply = &job->reply->reply_data.ctels_reply;
466 els_reply->status = FC_CTELS_STATUS_REJECT;
467 els_reply->rjt_data.action = rjt_data[3];
468 els_reply->rjt_data.reason_code = rjt_data[2];
469 els_reply->rjt_data.reason_explanation = rjt_data[1];
470 els_reply->rjt_data.vendor_unique = rjt_data[0];
474 pbuflist = (struct lpfc_dmabuf *) cmdiocbq->context3;
475 lpfc_mbuf_free(phba, pbuflist->virt, pbuflist->phys);
476 lpfc_sli_release_iocbq(phba, rspiocbq);
477 lpfc_sli_release_iocbq(phba, cmdiocbq);
480 /* make error code available to userspace */
481 job->reply->result = rc;
483 /* complete the job back to userspace */
485 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
490 * lpfc_bsg_rport_els - send an ELS command from a bsg request
491 * @job: fc_bsg_job to handle
494 lpfc_bsg_rport_els(struct fc_bsg_job *job)
496 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
497 struct lpfc_hba *phba = vport->phba;
498 struct lpfc_rport_data *rdata = job->rport->dd_data;
499 struct lpfc_nodelist *ndlp = rdata->pnode;
503 struct lpfc_iocbq *rspiocbq;
504 struct lpfc_iocbq *cmdiocbq;
507 struct lpfc_dmabuf *pcmd;
508 struct lpfc_dmabuf *prsp;
509 struct lpfc_dmabuf *pbuflist = NULL;
510 struct ulp_bde64 *bpl;
513 struct scatterlist *sgel = NULL;
516 struct bsg_job_data *dd_data;
520 /* in case no data is transferred */
521 job->reply->reply_payload_rcv_len = 0;
523 /* allocate our bsg tracking structure */
524 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
526 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
527 "2735 Failed allocation of dd_data\n");
532 if (!lpfc_nlp_get(ndlp)) {
537 elscmd = job->request->rqst_data.r_els.els_code;
538 cmdsize = job->request_payload.payload_len;
539 rspsize = job->reply_payload.payload_len;
540 rspiocbq = lpfc_sli_get_iocbq(phba);
547 rsp = &rspiocbq->iocb;
550 cmdiocbq = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp,
551 ndlp->nlp_DID, elscmd);
557 /* prep els iocb set context1 to the ndlp, context2 to the command
558 * dmabuf, context3 holds the data dmabuf
560 pcmd = (struct lpfc_dmabuf *) cmdiocbq->context2;
561 prsp = (struct lpfc_dmabuf *) pcmd->list.next;
562 lpfc_mbuf_free(phba, pcmd->virt, pcmd->phys);
564 lpfc_mbuf_free(phba, prsp->virt, prsp->phys);
566 cmdiocbq->context2 = NULL;
568 pbuflist = (struct lpfc_dmabuf *) cmdiocbq->context3;
569 bpl = (struct ulp_bde64 *) pbuflist->virt;
571 request_nseg = pci_map_sg(phba->pcidev, job->request_payload.sg_list,
572 job->request_payload.sg_cnt, DMA_TO_DEVICE);
573 for_each_sg(job->request_payload.sg_list, sgel, request_nseg, numbde) {
574 busaddr = sg_dma_address(sgel);
575 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
576 bpl->tus.f.bdeSize = sg_dma_len(sgel);
577 bpl->tus.w = cpu_to_le32(bpl->tus.w);
578 bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
579 bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
583 reply_nseg = pci_map_sg(phba->pcidev, job->reply_payload.sg_list,
584 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
585 for_each_sg(job->reply_payload.sg_list, sgel, reply_nseg, numbde) {
586 busaddr = sg_dma_address(sgel);
587 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
588 bpl->tus.f.bdeSize = sg_dma_len(sgel);
589 bpl->tus.w = cpu_to_le32(bpl->tus.w);
590 bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
591 bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
594 cmdiocbq->iocb.un.elsreq64.bdl.bdeSize =
595 (request_nseg + reply_nseg) * sizeof(struct ulp_bde64);
596 cmdiocbq->iocb.ulpContext = rpi;
597 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
598 cmdiocbq->context1 = NULL;
599 cmdiocbq->context2 = NULL;
601 cmdiocbq->iocb_cmpl = lpfc_bsg_rport_els_cmp;
602 cmdiocbq->context1 = dd_data;
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 creg_val = readl(phba->HCregaddr);
613 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
614 writel(creg_val, phba->HCregaddr);
615 readl(phba->HCregaddr); /* flush */
617 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq, 0);
619 if (rc == IOCB_SUCCESS)
620 return 0; /* done for now */
622 pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
623 job->request_payload.sg_cnt, DMA_TO_DEVICE);
624 pci_unmap_sg(phba->pcidev, job->reply_payload.sg_list,
625 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
627 lpfc_mbuf_free(phba, pbuflist->virt, pbuflist->phys);
629 lpfc_sli_release_iocbq(phba, cmdiocbq);
632 lpfc_sli_release_iocbq(phba, rspiocbq);
638 /* make error code available to userspace */
639 job->reply->result = rc;
645 * lpfc_bsg_event_free - frees an allocated event structure
646 * @kref: Pointer to a kref.
648 * Called from kref_put. Back cast the kref into an event structure address.
649 * Free any events to get, delete associated nodes, free any events to see,
650 * free any data then free the event itself.
653 lpfc_bsg_event_free(struct kref *kref)
655 struct lpfc_bsg_event *evt = container_of(kref, struct lpfc_bsg_event,
657 struct event_data *ed;
659 list_del(&evt->node);
661 while (!list_empty(&evt->events_to_get)) {
662 ed = list_entry(evt->events_to_get.next, typeof(*ed), node);
668 while (!list_empty(&evt->events_to_see)) {
669 ed = list_entry(evt->events_to_see.next, typeof(*ed), node);
679 * lpfc_bsg_event_ref - increments the kref for an event
680 * @evt: Pointer to an event structure.
683 lpfc_bsg_event_ref(struct lpfc_bsg_event *evt)
685 kref_get(&evt->kref);
689 * lpfc_bsg_event_unref - Uses kref_put to free an event structure
690 * @evt: Pointer to an event structure.
693 lpfc_bsg_event_unref(struct lpfc_bsg_event *evt)
695 kref_put(&evt->kref, lpfc_bsg_event_free);
699 * lpfc_bsg_event_new - allocate and initialize a event structure
700 * @ev_mask: Mask of events.
701 * @ev_reg_id: Event reg id.
702 * @ev_req_id: Event request id.
704 static struct lpfc_bsg_event *
705 lpfc_bsg_event_new(uint32_t ev_mask, int ev_reg_id, uint32_t ev_req_id)
707 struct lpfc_bsg_event *evt = kzalloc(sizeof(*evt), GFP_KERNEL);
712 INIT_LIST_HEAD(&evt->events_to_get);
713 INIT_LIST_HEAD(&evt->events_to_see);
714 evt->type_mask = ev_mask;
715 evt->req_id = ev_req_id;
716 evt->reg_id = ev_reg_id;
717 evt->wait_time_stamp = jiffies;
718 init_waitqueue_head(&evt->wq);
719 kref_init(&evt->kref);
724 * diag_cmd_data_free - Frees an lpfc dma buffer extension
725 * @phba: Pointer to HBA context object.
726 * @mlist: Pointer to an lpfc dma buffer extension.
729 diag_cmd_data_free(struct lpfc_hba *phba, struct lpfc_dmabufext *mlist)
731 struct lpfc_dmabufext *mlast;
732 struct pci_dev *pcidev;
733 struct list_head head, *curr, *next;
735 if ((!mlist) || (!lpfc_is_link_up(phba) &&
736 (phba->link_flag & LS_LOOPBACK_MODE))) {
740 pcidev = phba->pcidev;
741 list_add_tail(&head, &mlist->dma.list);
743 list_for_each_safe(curr, next, &head) {
744 mlast = list_entry(curr, struct lpfc_dmabufext , dma.list);
746 dma_free_coherent(&pcidev->dev,
756 * lpfc_bsg_ct_unsol_event - process an unsolicited CT command
761 * This function is called when an unsolicited CT command is received. It
762 * forwards the event to any processes registered to receive CT events.
765 lpfc_bsg_ct_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
766 struct lpfc_iocbq *piocbq)
768 uint32_t evt_req_id = 0;
771 struct lpfc_dmabuf *dmabuf = NULL;
772 struct lpfc_bsg_event *evt;
773 struct event_data *evt_dat = NULL;
774 struct lpfc_iocbq *iocbq;
776 struct list_head head;
777 struct ulp_bde64 *bde;
780 struct lpfc_dmabuf *bdeBuf1 = piocbq->context2;
781 struct lpfc_dmabuf *bdeBuf2 = piocbq->context3;
782 struct lpfc_hbq_entry *hbqe;
783 struct lpfc_sli_ct_request *ct_req;
784 struct fc_bsg_job *job = NULL;
788 INIT_LIST_HEAD(&head);
789 list_add_tail(&head, &piocbq->list);
791 if (piocbq->iocb.ulpBdeCount == 0 ||
792 piocbq->iocb.un.cont64[0].tus.f.bdeSize == 0)
793 goto error_ct_unsol_exit;
795 if (phba->link_state == LPFC_HBA_ERROR ||
796 (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)))
797 goto error_ct_unsol_exit;
799 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
802 dma_addr = getPaddr(piocbq->iocb.un.cont64[0].addrHigh,
803 piocbq->iocb.un.cont64[0].addrLow);
804 dmabuf = lpfc_sli_ringpostbuf_get(phba, pring, dma_addr);
807 goto error_ct_unsol_exit;
808 ct_req = (struct lpfc_sli_ct_request *)dmabuf->virt;
809 evt_req_id = ct_req->FsType;
810 cmd = ct_req->CommandResponse.bits.CmdRsp;
811 len = ct_req->CommandResponse.bits.Size;
812 if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
813 lpfc_sli_ringpostbuf_put(phba, pring, dmabuf);
815 spin_lock_irqsave(&phba->ct_ev_lock, flags);
816 list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
817 if (!(evt->type_mask & FC_REG_CT_EVENT) ||
818 evt->req_id != evt_req_id)
821 lpfc_bsg_event_ref(evt);
822 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
823 evt_dat = kzalloc(sizeof(*evt_dat), GFP_KERNEL);
824 if (evt_dat == NULL) {
825 spin_lock_irqsave(&phba->ct_ev_lock, flags);
826 lpfc_bsg_event_unref(evt);
827 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
828 "2614 Memory allocation failed for "
833 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
834 /* take accumulated byte count from the last iocbq */
835 iocbq = list_entry(head.prev, typeof(*iocbq), list);
836 evt_dat->len = iocbq->iocb.unsli3.rcvsli3.acc_len;
838 list_for_each_entry(iocbq, &head, list) {
839 for (i = 0; i < iocbq->iocb.ulpBdeCount; i++)
841 iocbq->iocb.un.cont64[i].tus.f.bdeSize;
845 evt_dat->data = kzalloc(evt_dat->len, GFP_KERNEL);
846 if (evt_dat->data == NULL) {
847 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
848 "2615 Memory allocation failed for "
849 "CT event data, size %d\n",
852 spin_lock_irqsave(&phba->ct_ev_lock, flags);
853 lpfc_bsg_event_unref(evt);
854 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
855 goto error_ct_unsol_exit;
858 list_for_each_entry(iocbq, &head, list) {
860 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
861 bdeBuf1 = iocbq->context2;
862 bdeBuf2 = iocbq->context3;
864 for (i = 0; i < iocbq->iocb.ulpBdeCount; i++) {
865 if (phba->sli3_options &
866 LPFC_SLI3_HBQ_ENABLED) {
868 hbqe = (struct lpfc_hbq_entry *)
869 &iocbq->iocb.un.ulpWord[0];
870 size = hbqe->bde.tus.f.bdeSize;
873 hbqe = (struct lpfc_hbq_entry *)
876 size = hbqe->bde.tus.f.bdeSize;
879 if ((offset + size) > evt_dat->len)
880 size = evt_dat->len - offset;
882 size = iocbq->iocb.un.cont64[i].
884 bde = &iocbq->iocb.un.cont64[i];
885 dma_addr = getPaddr(bde->addrHigh,
887 dmabuf = lpfc_sli_ringpostbuf_get(phba,
891 lpfc_printf_log(phba, KERN_ERR,
892 LOG_LIBDFC, "2616 No dmabuf "
893 "found for iocbq 0x%p\n",
895 kfree(evt_dat->data);
897 spin_lock_irqsave(&phba->ct_ev_lock,
899 lpfc_bsg_event_unref(evt);
900 spin_unlock_irqrestore(
901 &phba->ct_ev_lock, flags);
902 goto error_ct_unsol_exit;
904 memcpy((char *)(evt_dat->data) + offset,
907 if (evt_req_id != SLI_CT_ELX_LOOPBACK &&
908 !(phba->sli3_options &
909 LPFC_SLI3_HBQ_ENABLED)) {
910 lpfc_sli_ringpostbuf_put(phba, pring,
914 case ELX_LOOPBACK_DATA:
915 diag_cmd_data_free(phba,
916 (struct lpfc_dmabufext *)
919 case ELX_LOOPBACK_XRI_SETUP:
920 if ((phba->sli_rev ==
922 (phba->sli3_options &
923 LPFC_SLI3_HBQ_ENABLED
925 lpfc_in_buf_free(phba,
928 lpfc_post_buffer(phba,
934 if (!(phba->sli3_options &
935 LPFC_SLI3_HBQ_ENABLED))
936 lpfc_post_buffer(phba,
945 spin_lock_irqsave(&phba->ct_ev_lock, flags);
946 if (phba->sli_rev == LPFC_SLI_REV4) {
947 evt_dat->immed_dat = phba->ctx_idx;
948 phba->ctx_idx = (phba->ctx_idx + 1) % 64;
949 phba->ct_ctx[evt_dat->immed_dat].oxid =
950 piocbq->iocb.ulpContext;
951 phba->ct_ctx[evt_dat->immed_dat].SID =
952 piocbq->iocb.un.rcvels.remoteID;
954 evt_dat->immed_dat = piocbq->iocb.ulpContext;
956 evt_dat->type = FC_REG_CT_EVENT;
957 list_add(&evt_dat->node, &evt->events_to_see);
958 if (evt_req_id == SLI_CT_ELX_LOOPBACK) {
959 wake_up_interruptible(&evt->wq);
960 lpfc_bsg_event_unref(evt);
964 list_move(evt->events_to_see.prev, &evt->events_to_get);
965 lpfc_bsg_event_unref(evt);
970 job->reply->reply_payload_rcv_len = size;
971 /* make error code available to userspace */
972 job->reply->result = 0;
974 /* complete the job back to userspace */
975 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
977 spin_lock_irqsave(&phba->ct_ev_lock, flags);
980 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
983 if (!list_empty(&head))
985 if (evt_req_id == SLI_CT_ELX_LOOPBACK)
991 * lpfc_bsg_hba_set_event - process a SET_EVENT bsg vendor command
992 * @job: SET_EVENT fc_bsg_job
995 lpfc_bsg_hba_set_event(struct fc_bsg_job *job)
997 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
998 struct lpfc_hba *phba = vport->phba;
999 struct set_ct_event *event_req;
1000 struct lpfc_bsg_event *evt;
1002 struct bsg_job_data *dd_data = NULL;
1004 unsigned long flags;
1006 if (job->request_len <
1007 sizeof(struct fc_bsg_request) + sizeof(struct set_ct_event)) {
1008 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1009 "2612 Received SET_CT_EVENT below minimum "
1015 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
1016 if (dd_data == NULL) {
1017 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1018 "2734 Failed allocation of dd_data\n");
1023 event_req = (struct set_ct_event *)
1024 job->request->rqst_data.h_vendor.vendor_cmd;
1025 ev_mask = ((uint32_t)(unsigned long)event_req->type_mask &
1027 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1028 list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
1029 if (evt->reg_id == event_req->ev_reg_id) {
1030 lpfc_bsg_event_ref(evt);
1031 evt->wait_time_stamp = jiffies;
1035 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1037 if (&evt->node == &phba->ct_ev_waiters) {
1038 /* no event waiting struct yet - first call */
1039 evt = lpfc_bsg_event_new(ev_mask, event_req->ev_reg_id,
1040 event_req->ev_req_id);
1042 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1043 "2617 Failed allocation of event "
1049 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1050 list_add(&evt->node, &phba->ct_ev_waiters);
1051 lpfc_bsg_event_ref(evt);
1052 evt->wait_time_stamp = jiffies;
1053 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1056 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1058 dd_data->type = TYPE_EVT;
1059 dd_data->context_un.evt = evt;
1060 evt->set_job = job; /* for unsolicited command */
1061 job->dd_data = dd_data; /* for fc transport timeout callback*/
1062 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1063 return 0; /* call job done later */
1066 if (dd_data != NULL)
1069 job->dd_data = NULL;
1074 * lpfc_bsg_hba_get_event - process a GET_EVENT bsg vendor command
1075 * @job: GET_EVENT fc_bsg_job
1078 lpfc_bsg_hba_get_event(struct fc_bsg_job *job)
1080 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
1081 struct lpfc_hba *phba = vport->phba;
1082 struct get_ct_event *event_req;
1083 struct get_ct_event_reply *event_reply;
1084 struct lpfc_bsg_event *evt;
1085 struct event_data *evt_dat = NULL;
1086 unsigned long flags;
1089 if (job->request_len <
1090 sizeof(struct fc_bsg_request) + sizeof(struct get_ct_event)) {
1091 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1092 "2613 Received GET_CT_EVENT request below "
1098 event_req = (struct get_ct_event *)
1099 job->request->rqst_data.h_vendor.vendor_cmd;
1101 event_reply = (struct get_ct_event_reply *)
1102 job->reply->reply_data.vendor_reply.vendor_rsp;
1103 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1104 list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
1105 if (evt->reg_id == event_req->ev_reg_id) {
1106 if (list_empty(&evt->events_to_get))
1108 lpfc_bsg_event_ref(evt);
1109 evt->wait_time_stamp = jiffies;
1110 evt_dat = list_entry(evt->events_to_get.prev,
1111 struct event_data, node);
1112 list_del(&evt_dat->node);
1116 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1118 /* The app may continue to ask for event data until it gets
1119 * an error indicating that there isn't anymore
1121 if (evt_dat == NULL) {
1122 job->reply->reply_payload_rcv_len = 0;
1127 if (evt_dat->len > job->request_payload.payload_len) {
1128 evt_dat->len = job->request_payload.payload_len;
1129 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1130 "2618 Truncated event data at %d "
1132 job->request_payload.payload_len);
1135 event_reply->type = evt_dat->type;
1136 event_reply->immed_data = evt_dat->immed_dat;
1137 if (evt_dat->len > 0)
1138 job->reply->reply_payload_rcv_len =
1139 sg_copy_from_buffer(job->request_payload.sg_list,
1140 job->request_payload.sg_cnt,
1141 evt_dat->data, evt_dat->len);
1143 job->reply->reply_payload_rcv_len = 0;
1146 kfree(evt_dat->data);
1150 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1151 lpfc_bsg_event_unref(evt);
1152 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1153 job->dd_data = NULL;
1154 job->reply->result = 0;
1159 job->dd_data = NULL;
1160 job->reply->result = rc;
1165 * lpfc_issue_ct_rsp_cmp - lpfc_issue_ct_rsp's completion handler
1166 * @phba: Pointer to HBA context object.
1167 * @cmdiocbq: Pointer to command iocb.
1168 * @rspiocbq: Pointer to response iocb.
1170 * This function is the completion handler for iocbs issued using
1171 * lpfc_issue_ct_rsp_cmp function. This function is called by the
1172 * ring event handler function without any lock held. This function
1173 * can be called from both worker thread context and interrupt
1174 * context. This function also can be called from other thread which
1175 * cleans up the SLI layer objects.
1176 * This function copy the contents of the response iocb to the
1177 * response iocb memory object provided by the caller of
1178 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
1179 * sleeps for the iocb completion.
1182 lpfc_issue_ct_rsp_cmp(struct lpfc_hba *phba,
1183 struct lpfc_iocbq *cmdiocbq,
1184 struct lpfc_iocbq *rspiocbq)
1186 struct bsg_job_data *dd_data;
1187 struct fc_bsg_job *job;
1189 struct lpfc_dmabuf *bmp;
1190 struct lpfc_nodelist *ndlp;
1191 unsigned long flags;
1194 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1195 dd_data = cmdiocbq->context1;
1196 /* normal completion and timeout crossed paths, already done */
1198 spin_unlock_irqrestore(&phba->hbalock, flags);
1202 job = dd_data->context_un.iocb.set_job;
1203 bmp = dd_data->context_un.iocb.bmp;
1204 rsp = &rspiocbq->iocb;
1205 ndlp = dd_data->context_un.iocb.ndlp;
1207 pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
1208 job->request_payload.sg_cnt, DMA_TO_DEVICE);
1210 if (rsp->ulpStatus) {
1211 if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
1212 switch (rsp->un.ulpWord[4] & 0xff) {
1213 case IOERR_SEQUENCE_TIMEOUT:
1216 case IOERR_INVALID_RPI:
1226 job->reply->reply_payload_rcv_len =
1227 rsp->un.genreq64.bdl.bdeSize;
1229 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
1230 lpfc_sli_release_iocbq(phba, cmdiocbq);
1234 /* make error code available to userspace */
1235 job->reply->result = rc;
1236 job->dd_data = NULL;
1237 /* complete the job back to userspace */
1239 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1244 * lpfc_issue_ct_rsp - issue a ct response
1245 * @phba: Pointer to HBA context object.
1246 * @job: Pointer to the job object.
1247 * @tag: tag index value into the ports context exchange array.
1248 * @bmp: Pointer to a dma buffer descriptor.
1249 * @num_entry: Number of enties in the bde.
1252 lpfc_issue_ct_rsp(struct lpfc_hba *phba, struct fc_bsg_job *job, uint32_t tag,
1253 struct lpfc_dmabuf *bmp, int num_entry)
1256 struct lpfc_iocbq *ctiocb = NULL;
1258 struct lpfc_nodelist *ndlp = NULL;
1259 struct bsg_job_data *dd_data;
1262 /* allocate our bsg tracking structure */
1263 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
1265 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1266 "2736 Failed allocation of dd_data\n");
1271 /* Allocate buffer for command iocb */
1272 ctiocb = lpfc_sli_get_iocbq(phba);
1278 icmd = &ctiocb->iocb;
1279 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
1280 icmd->un.xseq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
1281 icmd->un.xseq64.bdl.addrLow = putPaddrLow(bmp->phys);
1282 icmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
1283 icmd->un.xseq64.bdl.bdeSize = (num_entry * sizeof(struct ulp_bde64));
1284 icmd->un.xseq64.w5.hcsw.Fctl = (LS | LA);
1285 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
1286 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_SOL_CTL;
1287 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
1289 /* Fill in rest of iocb */
1290 icmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;
1291 icmd->ulpBdeCount = 1;
1293 icmd->ulpClass = CLASS3;
1294 if (phba->sli_rev == LPFC_SLI_REV4) {
1295 /* Do not issue unsol response if oxid not marked as valid */
1296 if (!(phba->ct_ctx[tag].flags & UNSOL_VALID)) {
1298 goto issue_ct_rsp_exit;
1300 icmd->ulpContext = phba->ct_ctx[tag].oxid;
1301 ndlp = lpfc_findnode_did(phba->pport, phba->ct_ctx[tag].SID);
1303 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
1304 "2721 ndlp null for oxid %x SID %x\n",
1306 phba->ct_ctx[tag].SID);
1308 goto issue_ct_rsp_exit;
1310 icmd->un.ulpWord[3] = ndlp->nlp_rpi;
1311 /* The exchange is done, mark the entry as invalid */
1312 phba->ct_ctx[tag].flags &= ~UNSOL_VALID;
1314 icmd->ulpContext = (ushort) tag;
1316 icmd->ulpTimeout = phba->fc_ratov * 2;
1318 /* Xmit CT response on exchange <xid> */
1319 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
1320 "2722 Xmit CT response on exchange x%x Data: x%x x%x\n",
1321 icmd->ulpContext, icmd->ulpIoTag, phba->link_state);
1323 ctiocb->iocb_cmpl = NULL;
1324 ctiocb->iocb_flag |= LPFC_IO_LIBDFC;
1325 ctiocb->vport = phba->pport;
1326 ctiocb->context3 = bmp;
1328 ctiocb->iocb_cmpl = lpfc_issue_ct_rsp_cmp;
1329 ctiocb->context1 = dd_data;
1330 ctiocb->context2 = NULL;
1331 dd_data->type = TYPE_IOCB;
1332 dd_data->context_un.iocb.cmdiocbq = ctiocb;
1333 dd_data->context_un.iocb.rspiocbq = NULL;
1334 dd_data->context_un.iocb.set_job = job;
1335 dd_data->context_un.iocb.bmp = bmp;
1336 dd_data->context_un.iocb.ndlp = ndlp;
1338 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
1339 creg_val = readl(phba->HCregaddr);
1340 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
1341 writel(creg_val, phba->HCregaddr);
1342 readl(phba->HCregaddr); /* flush */
1345 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
1347 if (rc == IOCB_SUCCESS)
1348 return 0; /* done for now */
1351 lpfc_sli_release_iocbq(phba, ctiocb);
1359 * lpfc_bsg_send_mgmt_rsp - process a SEND_MGMT_RESP bsg vendor command
1360 * @job: SEND_MGMT_RESP fc_bsg_job
1363 lpfc_bsg_send_mgmt_rsp(struct fc_bsg_job *job)
1365 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
1366 struct lpfc_hba *phba = vport->phba;
1367 struct send_mgmt_resp *mgmt_resp = (struct send_mgmt_resp *)
1368 job->request->rqst_data.h_vendor.vendor_cmd;
1369 struct ulp_bde64 *bpl;
1370 struct lpfc_dmabuf *bmp = NULL;
1371 struct scatterlist *sgel = NULL;
1375 uint32_t tag = mgmt_resp->tag;
1376 unsigned long reqbfrcnt =
1377 (unsigned long)job->request_payload.payload_len;
1380 /* in case no data is transferred */
1381 job->reply->reply_payload_rcv_len = 0;
1383 if (!reqbfrcnt || (reqbfrcnt > (80 * BUF_SZ_4K))) {
1385 goto send_mgmt_rsp_exit;
1388 bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
1391 goto send_mgmt_rsp_exit;
1394 bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
1397 goto send_mgmt_rsp_free_bmp;
1400 INIT_LIST_HEAD(&bmp->list);
1401 bpl = (struct ulp_bde64 *) bmp->virt;
1402 request_nseg = pci_map_sg(phba->pcidev, job->request_payload.sg_list,
1403 job->request_payload.sg_cnt, DMA_TO_DEVICE);
1404 for_each_sg(job->request_payload.sg_list, sgel, request_nseg, numbde) {
1405 busaddr = sg_dma_address(sgel);
1406 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1407 bpl->tus.f.bdeSize = sg_dma_len(sgel);
1408 bpl->tus.w = cpu_to_le32(bpl->tus.w);
1409 bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
1410 bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
1414 rc = lpfc_issue_ct_rsp(phba, job, tag, bmp, request_nseg);
1416 if (rc == IOCB_SUCCESS)
1417 return 0; /* done for now */
1419 /* TBD need to handle a timeout */
1420 pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
1421 job->request_payload.sg_cnt, DMA_TO_DEVICE);
1423 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
1425 send_mgmt_rsp_free_bmp:
1428 /* make error code available to userspace */
1429 job->reply->result = rc;
1430 job->dd_data = NULL;
1435 * lpfc_bsg_diag_mode - process a LPFC_BSG_VENDOR_DIAG_MODE bsg vendor command
1436 * @job: LPFC_BSG_VENDOR_DIAG_MODE
1438 * This function is responsible for placing a port into diagnostic loopback
1439 * mode in order to perform a diagnostic loopback test.
1440 * All new scsi requests are blocked, a small delay is used to allow the
1441 * scsi requests to complete then the link is brought down. If the link is
1442 * is placed in loopback mode then scsi requests are again allowed
1443 * so the scsi mid-layer doesn't give up on the port.
1444 * All of this is done in-line.
1447 lpfc_bsg_diag_mode(struct fc_bsg_job *job)
1449 struct Scsi_Host *shost = job->shost;
1450 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
1451 struct lpfc_hba *phba = vport->phba;
1452 struct diag_mode_set *loopback_mode;
1453 struct lpfc_sli *psli = &phba->sli;
1454 struct lpfc_sli_ring *pring = &psli->ring[LPFC_FCP_RING];
1455 uint32_t link_flags;
1457 struct lpfc_vport **vports;
1458 LPFC_MBOXQ_t *pmboxq;
1463 /* no data to return just the return code */
1464 job->reply->reply_payload_rcv_len = 0;
1466 if (job->request_len <
1467 sizeof(struct fc_bsg_request) + sizeof(struct diag_mode_set)) {
1468 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1469 "2738 Received DIAG MODE request below minimum "
1475 loopback_mode = (struct diag_mode_set *)
1476 job->request->rqst_data.h_vendor.vendor_cmd;
1477 link_flags = loopback_mode->type;
1478 timeout = loopback_mode->timeout;
1480 if ((phba->link_state == LPFC_HBA_ERROR) ||
1481 (psli->sli_flag & LPFC_BLOCK_MGMT_IO) ||
1482 (!(psli->sli_flag & LPFC_SLI_ACTIVE))) {
1487 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1493 vports = lpfc_create_vport_work_array(phba);
1495 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
1496 shost = lpfc_shost_from_vport(vports[i]);
1497 scsi_block_requests(shost);
1500 lpfc_destroy_vport_work_array(phba, vports);
1502 shost = lpfc_shost_from_vport(phba->pport);
1503 scsi_block_requests(shost);
1506 while (pring->txcmplq_cnt) {
1507 if (i++ > 500) /* wait up to 5 seconds */
1513 memset((void *)pmboxq, 0, sizeof(LPFC_MBOXQ_t));
1514 pmboxq->u.mb.mbxCommand = MBX_DOWN_LINK;
1515 pmboxq->u.mb.mbxOwner = OWN_HOST;
1517 mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
1519 if ((mbxstatus == MBX_SUCCESS) && (pmboxq->u.mb.mbxStatus == 0)) {
1520 /* wait for link down before proceeding */
1522 while (phba->link_state != LPFC_LINK_DOWN) {
1523 if (i++ > timeout) {
1525 goto loopback_mode_exit;
1531 memset((void *)pmboxq, 0, sizeof(LPFC_MBOXQ_t));
1532 if (link_flags == INTERNAL_LOOP_BACK)
1533 pmboxq->u.mb.un.varInitLnk.link_flags = FLAGS_LOCAL_LB;
1535 pmboxq->u.mb.un.varInitLnk.link_flags =
1536 FLAGS_TOPOLOGY_MODE_LOOP;
1538 pmboxq->u.mb.mbxCommand = MBX_INIT_LINK;
1539 pmboxq->u.mb.mbxOwner = OWN_HOST;
1541 mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq,
1544 if ((mbxstatus != MBX_SUCCESS) || (pmboxq->u.mb.mbxStatus))
1547 phba->link_flag |= LS_LOOPBACK_MODE;
1548 /* wait for the link attention interrupt */
1552 while (phba->link_state != LPFC_HBA_READY) {
1553 if (i++ > timeout) {
1566 vports = lpfc_create_vport_work_array(phba);
1568 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
1569 shost = lpfc_shost_from_vport(vports[i]);
1570 scsi_unblock_requests(shost);
1572 lpfc_destroy_vport_work_array(phba, vports);
1574 shost = lpfc_shost_from_vport(phba->pport);
1575 scsi_unblock_requests(shost);
1579 * Let SLI layer release mboxq if mbox command completed after timeout.
1581 if (mbxstatus != MBX_TIMEOUT)
1582 mempool_free(pmboxq, phba->mbox_mem_pool);
1585 /* make error code available to userspace */
1586 job->reply->result = rc;
1587 /* complete the job back to userspace if no error */
1594 * lpfcdiag_loop_self_reg - obtains a remote port login id
1595 * @phba: Pointer to HBA context object
1596 * @rpi: Pointer to a remote port login id
1598 * This function obtains a remote port login id so the diag loopback test
1599 * can send and receive its own unsolicited CT command.
1601 static int lpfcdiag_loop_self_reg(struct lpfc_hba *phba, uint16_t * rpi)
1604 struct lpfc_dmabuf *dmabuff;
1607 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1611 status = lpfc_reg_rpi(phba, 0, phba->pport->fc_myDID,
1612 (uint8_t *)&phba->pport->fc_sparam, mbox, 0);
1614 mempool_free(mbox, phba->mbox_mem_pool);
1618 dmabuff = (struct lpfc_dmabuf *) mbox->context1;
1619 mbox->context1 = NULL;
1620 status = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
1622 if ((status != MBX_SUCCESS) || (mbox->u.mb.mbxStatus)) {
1623 lpfc_mbuf_free(phba, dmabuff->virt, dmabuff->phys);
1625 if (status != MBX_TIMEOUT)
1626 mempool_free(mbox, phba->mbox_mem_pool);
1630 *rpi = mbox->u.mb.un.varWords[0];
1632 lpfc_mbuf_free(phba, dmabuff->virt, dmabuff->phys);
1634 mempool_free(mbox, phba->mbox_mem_pool);
1639 * lpfcdiag_loop_self_unreg - unregs from the rpi
1640 * @phba: Pointer to HBA context object
1641 * @rpi: Remote port login id
1643 * This function unregisters the rpi obtained in lpfcdiag_loop_self_reg
1645 static int lpfcdiag_loop_self_unreg(struct lpfc_hba *phba, uint16_t rpi)
1650 /* Allocate mboxq structure */
1651 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1655 lpfc_unreg_login(phba, 0, rpi, mbox);
1656 status = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
1658 if ((status != MBX_SUCCESS) || (mbox->u.mb.mbxStatus)) {
1659 if (status != MBX_TIMEOUT)
1660 mempool_free(mbox, phba->mbox_mem_pool);
1664 mempool_free(mbox, phba->mbox_mem_pool);
1669 * lpfcdiag_loop_get_xri - obtains the transmit and receive ids
1670 * @phba: Pointer to HBA context object
1671 * @rpi: Remote port login id
1672 * @txxri: Pointer to transmit exchange id
1673 * @rxxri: Pointer to response exchabge id
1675 * This function obtains the transmit and receive ids required to send
1676 * an unsolicited ct command with a payload. A special lpfc FsType and CmdRsp
1677 * flags are used to the unsolicted response handler is able to process
1678 * the ct command sent on the same port.
1680 static int lpfcdiag_loop_get_xri(struct lpfc_hba *phba, uint16_t rpi,
1681 uint16_t *txxri, uint16_t * rxxri)
1683 struct lpfc_bsg_event *evt;
1684 struct lpfc_iocbq *cmdiocbq, *rspiocbq;
1686 struct lpfc_dmabuf *dmabuf;
1687 struct ulp_bde64 *bpl = NULL;
1688 struct lpfc_sli_ct_request *ctreq = NULL;
1690 unsigned long flags;
1694 evt = lpfc_bsg_event_new(FC_REG_CT_EVENT, current->pid,
1695 SLI_CT_ELX_LOOPBACK);
1699 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1700 list_add(&evt->node, &phba->ct_ev_waiters);
1701 lpfc_bsg_event_ref(evt);
1702 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1704 cmdiocbq = lpfc_sli_get_iocbq(phba);
1705 rspiocbq = lpfc_sli_get_iocbq(phba);
1707 dmabuf = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
1709 dmabuf->virt = lpfc_mbuf_alloc(phba, 0, &dmabuf->phys);
1710 INIT_LIST_HEAD(&dmabuf->list);
1711 bpl = (struct ulp_bde64 *) dmabuf->virt;
1712 memset(bpl, 0, sizeof(*bpl));
1713 ctreq = (struct lpfc_sli_ct_request *)(bpl + 1);
1715 le32_to_cpu(putPaddrHigh(dmabuf->phys + sizeof(*bpl)));
1717 le32_to_cpu(putPaddrLow(dmabuf->phys + sizeof(*bpl)));
1718 bpl->tus.f.bdeFlags = 0;
1719 bpl->tus.f.bdeSize = ELX_LOOPBACK_HEADER_SZ;
1720 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1723 if (cmdiocbq == NULL || rspiocbq == NULL ||
1724 dmabuf == NULL || bpl == NULL || ctreq == NULL) {
1726 goto err_get_xri_exit;
1729 cmd = &cmdiocbq->iocb;
1730 rsp = &rspiocbq->iocb;
1732 memset(ctreq, 0, ELX_LOOPBACK_HEADER_SZ);
1734 ctreq->RevisionId.bits.Revision = SLI_CT_REVISION;
1735 ctreq->RevisionId.bits.InId = 0;
1736 ctreq->FsType = SLI_CT_ELX_LOOPBACK;
1737 ctreq->FsSubType = 0;
1738 ctreq->CommandResponse.bits.CmdRsp = ELX_LOOPBACK_XRI_SETUP;
1739 ctreq->CommandResponse.bits.Size = 0;
1742 cmd->un.xseq64.bdl.addrHigh = putPaddrHigh(dmabuf->phys);
1743 cmd->un.xseq64.bdl.addrLow = putPaddrLow(dmabuf->phys);
1744 cmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
1745 cmd->un.xseq64.bdl.bdeSize = sizeof(*bpl);
1747 cmd->un.xseq64.w5.hcsw.Fctl = LA;
1748 cmd->un.xseq64.w5.hcsw.Dfctl = 0;
1749 cmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
1750 cmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
1752 cmd->ulpCommand = CMD_XMIT_SEQUENCE64_CR;
1753 cmd->ulpBdeCount = 1;
1755 cmd->ulpClass = CLASS3;
1756 cmd->ulpContext = rpi;
1758 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
1759 cmdiocbq->vport = phba->pport;
1761 ret_val = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq,
1763 (phba->fc_ratov * 2)
1764 + LPFC_DRVR_TIMEOUT);
1766 goto err_get_xri_exit;
1768 *txxri = rsp->ulpContext;
1771 evt->wait_time_stamp = jiffies;
1772 ret_val = wait_event_interruptible_timeout(
1773 evt->wq, !list_empty(&evt->events_to_see),
1774 ((phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT) * HZ);
1775 if (list_empty(&evt->events_to_see))
1776 ret_val = (ret_val) ? EINTR : ETIMEDOUT;
1778 ret_val = IOCB_SUCCESS;
1779 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1780 list_move(evt->events_to_see.prev, &evt->events_to_get);
1781 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1782 *rxxri = (list_entry(evt->events_to_get.prev,
1783 typeof(struct event_data),
1789 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1790 lpfc_bsg_event_unref(evt); /* release ref */
1791 lpfc_bsg_event_unref(evt); /* delete */
1792 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1796 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
1800 if (cmdiocbq && (ret_val != IOCB_TIMEDOUT))
1801 lpfc_sli_release_iocbq(phba, cmdiocbq);
1803 lpfc_sli_release_iocbq(phba, rspiocbq);
1808 * diag_cmd_data_alloc - fills in a bde struct with dma buffers
1809 * @phba: Pointer to HBA context object
1810 * @bpl: Pointer to 64 bit bde structure
1811 * @size: Number of bytes to process
1812 * @nocopydata: Flag to copy user data into the allocated buffer
1814 * This function allocates page size buffers and populates an lpfc_dmabufext.
1815 * If allowed the user data pointed to with indataptr is copied into the kernel
1816 * memory. The chained list of page size buffers is returned.
1818 static struct lpfc_dmabufext *
1819 diag_cmd_data_alloc(struct lpfc_hba *phba,
1820 struct ulp_bde64 *bpl, uint32_t size,
1823 struct lpfc_dmabufext *mlist = NULL;
1824 struct lpfc_dmabufext *dmp;
1825 int cnt, offset = 0, i = 0;
1826 struct pci_dev *pcidev;
1828 pcidev = phba->pcidev;
1831 /* We get chunks of 4K */
1832 if (size > BUF_SZ_4K)
1837 /* allocate struct lpfc_dmabufext buffer header */
1838 dmp = kmalloc(sizeof(struct lpfc_dmabufext), GFP_KERNEL);
1842 INIT_LIST_HEAD(&dmp->dma.list);
1844 /* Queue it to a linked list */
1846 list_add_tail(&dmp->dma.list, &mlist->dma.list);
1850 /* allocate buffer */
1851 dmp->dma.virt = dma_alloc_coherent(&pcidev->dev,
1862 bpl->tus.f.bdeFlags = 0;
1863 pci_dma_sync_single_for_device(phba->pcidev,
1864 dmp->dma.phys, LPFC_BPL_SIZE, PCI_DMA_TODEVICE);
1867 memset((uint8_t *)dmp->dma.virt, 0, cnt);
1868 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1871 /* build buffer ptr list for IOCB */
1872 bpl->addrLow = le32_to_cpu(putPaddrLow(dmp->dma.phys));
1873 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dmp->dma.phys));
1874 bpl->tus.f.bdeSize = (ushort) cnt;
1875 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1886 diag_cmd_data_free(phba, mlist);
1891 * lpfcdiag_loop_post_rxbufs - post the receive buffers for an unsol CT cmd
1892 * @phba: Pointer to HBA context object
1893 * @rxxri: Receive exchange id
1894 * @len: Number of data bytes
1896 * This function allocates and posts a data buffer of sufficient size to recieve
1897 * an unsolicted CT command.
1899 static int lpfcdiag_loop_post_rxbufs(struct lpfc_hba *phba, uint16_t rxxri,
1902 struct lpfc_sli *psli = &phba->sli;
1903 struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
1904 struct lpfc_iocbq *cmdiocbq;
1906 struct list_head head, *curr, *next;
1907 struct lpfc_dmabuf *rxbmp;
1908 struct lpfc_dmabuf *dmp;
1909 struct lpfc_dmabuf *mp[2] = {NULL, NULL};
1910 struct ulp_bde64 *rxbpl = NULL;
1912 struct lpfc_dmabufext *rxbuffer = NULL;
1916 cmdiocbq = lpfc_sli_get_iocbq(phba);
1917 rxbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
1918 if (rxbmp != NULL) {
1919 rxbmp->virt = lpfc_mbuf_alloc(phba, 0, &rxbmp->phys);
1920 INIT_LIST_HEAD(&rxbmp->list);
1921 rxbpl = (struct ulp_bde64 *) rxbmp->virt;
1922 rxbuffer = diag_cmd_data_alloc(phba, rxbpl, len, 0);
1925 if (!cmdiocbq || !rxbmp || !rxbpl || !rxbuffer) {
1927 goto err_post_rxbufs_exit;
1930 /* Queue buffers for the receive exchange */
1931 num_bde = (uint32_t)rxbuffer->flag;
1932 dmp = &rxbuffer->dma;
1934 cmd = &cmdiocbq->iocb;
1937 INIT_LIST_HEAD(&head);
1938 list_add_tail(&head, &dmp->list);
1939 list_for_each_safe(curr, next, &head) {
1940 mp[i] = list_entry(curr, struct lpfc_dmabuf, list);
1943 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
1944 mp[i]->buffer_tag = lpfc_sli_get_buffer_tag(phba);
1945 cmd->un.quexri64cx.buff.bde.addrHigh =
1946 putPaddrHigh(mp[i]->phys);
1947 cmd->un.quexri64cx.buff.bde.addrLow =
1948 putPaddrLow(mp[i]->phys);
1949 cmd->un.quexri64cx.buff.bde.tus.f.bdeSize =
1950 ((struct lpfc_dmabufext *)mp[i])->size;
1951 cmd->un.quexri64cx.buff.buffer_tag = mp[i]->buffer_tag;
1952 cmd->ulpCommand = CMD_QUE_XRI64_CX;
1955 cmd->ulpBdeCount = 1;
1956 cmd->unsli3.que_xri64cx_ext_words.ebde_count = 0;
1959 cmd->un.cont64[i].addrHigh = putPaddrHigh(mp[i]->phys);
1960 cmd->un.cont64[i].addrLow = putPaddrLow(mp[i]->phys);
1961 cmd->un.cont64[i].tus.f.bdeSize =
1962 ((struct lpfc_dmabufext *)mp[i])->size;
1963 cmd->ulpBdeCount = ++i;
1965 if ((--num_bde > 0) && (i < 2))
1968 cmd->ulpCommand = CMD_QUE_XRI_BUF64_CX;
1972 cmd->ulpClass = CLASS3;
1973 cmd->ulpContext = rxxri;
1975 ret_val = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq, 0);
1977 if (ret_val == IOCB_ERROR) {
1978 diag_cmd_data_free(phba,
1979 (struct lpfc_dmabufext *)mp[0]);
1981 diag_cmd_data_free(phba,
1982 (struct lpfc_dmabufext *)mp[1]);
1983 dmp = list_entry(next, struct lpfc_dmabuf, list);
1985 goto err_post_rxbufs_exit;
1988 lpfc_sli_ringpostbuf_put(phba, pring, mp[0]);
1990 lpfc_sli_ringpostbuf_put(phba, pring, mp[1]);
1994 /* The iocb was freed by lpfc_sli_issue_iocb */
1995 cmdiocbq = lpfc_sli_get_iocbq(phba);
1997 dmp = list_entry(next, struct lpfc_dmabuf, list);
1999 goto err_post_rxbufs_exit;
2002 cmd = &cmdiocbq->iocb;
2007 err_post_rxbufs_exit:
2011 lpfc_mbuf_free(phba, rxbmp->virt, rxbmp->phys);
2016 lpfc_sli_release_iocbq(phba, cmdiocbq);
2021 * lpfc_bsg_diag_test - with a port in loopback issues a Ct cmd to itself
2022 * @job: LPFC_BSG_VENDOR_DIAG_TEST fc_bsg_job
2024 * This function receives a user data buffer to be transmitted and received on
2025 * the same port, the link must be up and in loopback mode prior
2027 * 1. A kernel buffer is allocated to copy the user data into.
2028 * 2. The port registers with "itself".
2029 * 3. The transmit and receive exchange ids are obtained.
2030 * 4. The receive exchange id is posted.
2031 * 5. A new els loopback event is created.
2032 * 6. The command and response iocbs are allocated.
2033 * 7. The cmd iocb FsType is set to elx loopback and the CmdRsp to looppback.
2035 * This function is meant to be called n times while the port is in loopback
2036 * so it is the apps responsibility to issue a reset to take the port out
2040 lpfc_bsg_diag_test(struct fc_bsg_job *job)
2042 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
2043 struct lpfc_hba *phba = vport->phba;
2044 struct diag_mode_test *diag_mode;
2045 struct lpfc_bsg_event *evt;
2046 struct event_data *evdat;
2047 struct lpfc_sli *psli = &phba->sli;
2050 size_t segment_len = 0, segment_offset = 0, current_offset = 0;
2052 struct lpfc_iocbq *cmdiocbq, *rspiocbq;
2054 struct lpfc_sli_ct_request *ctreq;
2055 struct lpfc_dmabuf *txbmp;
2056 struct ulp_bde64 *txbpl = NULL;
2057 struct lpfc_dmabufext *txbuffer = NULL;
2058 struct list_head head;
2059 struct lpfc_dmabuf *curr;
2060 uint16_t txxri, rxxri;
2062 uint8_t *ptr = NULL, *rx_databuf = NULL;
2064 unsigned long flags;
2065 void *dataout = NULL;
2068 /* in case no data is returned return just the return code */
2069 job->reply->reply_payload_rcv_len = 0;
2071 if (job->request_len <
2072 sizeof(struct fc_bsg_request) + sizeof(struct diag_mode_test)) {
2073 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2074 "2739 Received DIAG TEST request below minimum "
2077 goto loopback_test_exit;
2080 if (job->request_payload.payload_len !=
2081 job->reply_payload.payload_len) {
2083 goto loopback_test_exit;
2086 diag_mode = (struct diag_mode_test *)
2087 job->request->rqst_data.h_vendor.vendor_cmd;
2089 if ((phba->link_state == LPFC_HBA_ERROR) ||
2090 (psli->sli_flag & LPFC_BLOCK_MGMT_IO) ||
2091 (!(psli->sli_flag & LPFC_SLI_ACTIVE))) {
2093 goto loopback_test_exit;
2096 if (!lpfc_is_link_up(phba) || !(phba->link_flag & LS_LOOPBACK_MODE)) {
2098 goto loopback_test_exit;
2101 size = job->request_payload.payload_len;
2102 full_size = size + ELX_LOOPBACK_HEADER_SZ; /* plus the header */
2104 if ((size == 0) || (size > 80 * BUF_SZ_4K)) {
2106 goto loopback_test_exit;
2109 if (size >= BUF_SZ_4K) {
2111 * Allocate memory for ioctl data. If buffer is bigger than 64k,
2112 * then we allocate 64k and re-use that buffer over and over to
2113 * xfer the whole block. This is because Linux kernel has a
2114 * problem allocating more than 120k of kernel space memory. Saw
2115 * problem with GET_FCPTARGETMAPPING...
2117 if (size <= (64 * 1024))
2120 total_mem = 64 * 1024;
2122 /* Allocate memory for ioctl data */
2123 total_mem = BUF_SZ_4K;
2125 dataout = kmalloc(total_mem, GFP_KERNEL);
2126 if (dataout == NULL) {
2128 goto loopback_test_exit;
2132 ptr += ELX_LOOPBACK_HEADER_SZ;
2133 sg_copy_to_buffer(job->request_payload.sg_list,
2134 job->request_payload.sg_cnt,
2137 rc = lpfcdiag_loop_self_reg(phba, &rpi);
2140 goto loopback_test_exit;
2143 rc = lpfcdiag_loop_get_xri(phba, rpi, &txxri, &rxxri);
2145 lpfcdiag_loop_self_unreg(phba, rpi);
2147 goto loopback_test_exit;
2150 rc = lpfcdiag_loop_post_rxbufs(phba, rxxri, full_size);
2152 lpfcdiag_loop_self_unreg(phba, rpi);
2154 goto loopback_test_exit;
2157 evt = lpfc_bsg_event_new(FC_REG_CT_EVENT, current->pid,
2158 SLI_CT_ELX_LOOPBACK);
2160 lpfcdiag_loop_self_unreg(phba, rpi);
2162 goto loopback_test_exit;
2165 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2166 list_add(&evt->node, &phba->ct_ev_waiters);
2167 lpfc_bsg_event_ref(evt);
2168 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2170 cmdiocbq = lpfc_sli_get_iocbq(phba);
2171 rspiocbq = lpfc_sli_get_iocbq(phba);
2172 txbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2175 txbmp->virt = lpfc_mbuf_alloc(phba, 0, &txbmp->phys);
2176 INIT_LIST_HEAD(&txbmp->list);
2177 txbpl = (struct ulp_bde64 *) txbmp->virt;
2179 txbuffer = diag_cmd_data_alloc(phba,
2180 txbpl, full_size, 0);
2183 if (!cmdiocbq || !rspiocbq || !txbmp || !txbpl || !txbuffer) {
2185 goto err_loopback_test_exit;
2188 cmd = &cmdiocbq->iocb;
2189 rsp = &rspiocbq->iocb;
2191 INIT_LIST_HEAD(&head);
2192 list_add_tail(&head, &txbuffer->dma.list);
2193 list_for_each_entry(curr, &head, list) {
2194 segment_len = ((struct lpfc_dmabufext *)curr)->size;
2195 if (current_offset == 0) {
2197 memset(ctreq, 0, ELX_LOOPBACK_HEADER_SZ);
2198 ctreq->RevisionId.bits.Revision = SLI_CT_REVISION;
2199 ctreq->RevisionId.bits.InId = 0;
2200 ctreq->FsType = SLI_CT_ELX_LOOPBACK;
2201 ctreq->FsSubType = 0;
2202 ctreq->CommandResponse.bits.CmdRsp = ELX_LOOPBACK_DATA;
2203 ctreq->CommandResponse.bits.Size = size;
2204 segment_offset = ELX_LOOPBACK_HEADER_SZ;
2208 BUG_ON(segment_offset >= segment_len);
2209 memcpy(curr->virt + segment_offset,
2210 ptr + current_offset,
2211 segment_len - segment_offset);
2213 current_offset += segment_len - segment_offset;
2214 BUG_ON(current_offset > size);
2218 /* Build the XMIT_SEQUENCE iocb */
2220 num_bde = (uint32_t)txbuffer->flag;
2222 cmd->un.xseq64.bdl.addrHigh = putPaddrHigh(txbmp->phys);
2223 cmd->un.xseq64.bdl.addrLow = putPaddrLow(txbmp->phys);
2224 cmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
2225 cmd->un.xseq64.bdl.bdeSize = (num_bde * sizeof(struct ulp_bde64));
2227 cmd->un.xseq64.w5.hcsw.Fctl = (LS | LA);
2228 cmd->un.xseq64.w5.hcsw.Dfctl = 0;
2229 cmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
2230 cmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
2232 cmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;
2233 cmd->ulpBdeCount = 1;
2235 cmd->ulpClass = CLASS3;
2236 cmd->ulpContext = txxri;
2238 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
2239 cmdiocbq->vport = phba->pport;
2241 rc = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq, rspiocbq,
2242 (phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT);
2244 if ((rc != IOCB_SUCCESS) || (rsp->ulpStatus != IOCB_SUCCESS)) {
2246 goto err_loopback_test_exit;
2250 rc = wait_event_interruptible_timeout(
2251 evt->wq, !list_empty(&evt->events_to_see),
2252 ((phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT) * HZ);
2254 if (list_empty(&evt->events_to_see))
2255 rc = (rc) ? -EINTR : -ETIMEDOUT;
2257 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2258 list_move(evt->events_to_see.prev, &evt->events_to_get);
2259 evdat = list_entry(evt->events_to_get.prev,
2260 typeof(*evdat), node);
2261 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2262 rx_databuf = evdat->data;
2263 if (evdat->len != full_size) {
2264 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
2265 "1603 Loopback test did not receive expected "
2266 "data length. actual length 0x%x expected "
2268 evdat->len, full_size);
2270 } else if (rx_databuf == NULL)
2274 /* skip over elx loopback header */
2275 rx_databuf += ELX_LOOPBACK_HEADER_SZ;
2276 job->reply->reply_payload_rcv_len =
2277 sg_copy_from_buffer(job->reply_payload.sg_list,
2278 job->reply_payload.sg_cnt,
2280 job->reply->reply_payload_rcv_len = size;
2284 err_loopback_test_exit:
2285 lpfcdiag_loop_self_unreg(phba, rpi);
2287 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2288 lpfc_bsg_event_unref(evt); /* release ref */
2289 lpfc_bsg_event_unref(evt); /* delete */
2290 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2292 if (cmdiocbq != NULL)
2293 lpfc_sli_release_iocbq(phba, cmdiocbq);
2295 if (rspiocbq != NULL)
2296 lpfc_sli_release_iocbq(phba, rspiocbq);
2298 if (txbmp != NULL) {
2299 if (txbpl != NULL) {
2300 if (txbuffer != NULL)
2301 diag_cmd_data_free(phba, txbuffer);
2302 lpfc_mbuf_free(phba, txbmp->virt, txbmp->phys);
2309 /* make error code available to userspace */
2310 job->reply->result = rc;
2311 job->dd_data = NULL;
2312 /* complete the job back to userspace if no error */
2319 * lpfc_bsg_get_dfc_rev - process a GET_DFC_REV bsg vendor command
2320 * @job: GET_DFC_REV fc_bsg_job
2323 lpfc_bsg_get_dfc_rev(struct fc_bsg_job *job)
2325 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
2326 struct lpfc_hba *phba = vport->phba;
2327 struct get_mgmt_rev *event_req;
2328 struct get_mgmt_rev_reply *event_reply;
2331 if (job->request_len <
2332 sizeof(struct fc_bsg_request) + sizeof(struct get_mgmt_rev)) {
2333 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2334 "2740 Received GET_DFC_REV request below "
2340 event_req = (struct get_mgmt_rev *)
2341 job->request->rqst_data.h_vendor.vendor_cmd;
2343 event_reply = (struct get_mgmt_rev_reply *)
2344 job->reply->reply_data.vendor_reply.vendor_rsp;
2346 if (job->reply_len <
2347 sizeof(struct fc_bsg_request) + sizeof(struct get_mgmt_rev_reply)) {
2348 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2349 "2741 Received GET_DFC_REV reply below "
2355 event_reply->info.a_Major = MANAGEMENT_MAJOR_REV;
2356 event_reply->info.a_Minor = MANAGEMENT_MINOR_REV;
2358 job->reply->result = rc;
2365 * lpfc_bsg_wake_mbox_wait - lpfc_bsg_issue_mbox mbox completion handler
2366 * @phba: Pointer to HBA context object.
2367 * @pmboxq: Pointer to mailbox command.
2369 * This is completion handler function for mailbox commands issued from
2370 * lpfc_bsg_issue_mbox function. This function is called by the
2371 * mailbox event handler function with no lock held. This function
2372 * will wake up thread waiting on the wait queue pointed by context1
2376 lpfc_bsg_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
2378 struct bsg_job_data *dd_data;
2381 struct fc_bsg_job *job;
2383 unsigned long flags;
2385 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2386 dd_data = pmboxq->context1;
2388 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2392 pmb = &dd_data->context_un.mbox.pmboxq->u.mb;
2393 mb = dd_data->context_un.mbox.mb;
2394 job = dd_data->context_un.mbox.set_job;
2395 memcpy(mb, pmb, sizeof(*pmb));
2396 size = job->request_payload.payload_len;
2397 job->reply->reply_payload_rcv_len =
2398 sg_copy_from_buffer(job->reply_payload.sg_list,
2399 job->reply_payload.sg_cnt,
2401 job->reply->result = 0;
2402 dd_data->context_un.mbox.set_job = NULL;
2403 job->dd_data = NULL;
2405 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2406 mempool_free(dd_data->context_un.mbox.pmboxq, phba->mbox_mem_pool);
2413 * lpfc_bsg_check_cmd_access - test for a supported mailbox command
2414 * @phba: Pointer to HBA context object.
2415 * @mb: Pointer to a mailbox object.
2416 * @vport: Pointer to a vport object.
2418 * Some commands require the port to be offline, some may not be called from
2421 static int lpfc_bsg_check_cmd_access(struct lpfc_hba *phba,
2422 MAILBOX_t *mb, struct lpfc_vport *vport)
2424 /* return negative error values for bsg job */
2425 switch (mb->mbxCommand) {
2429 case MBX_CONFIG_LINK:
2430 case MBX_CONFIG_RING:
2431 case MBX_RESET_RING:
2432 case MBX_UNREG_LOGIN:
2434 case MBX_DUMP_CONTEXT:
2438 if (!(vport->fc_flag & FC_OFFLINE_MODE)) {
2439 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2440 "2743 Command 0x%x is illegal in on-line "
2446 case MBX_WRITE_VPARMS:
2449 case MBX_READ_CONFIG:
2450 case MBX_READ_RCONFIG:
2451 case MBX_READ_STATUS:
2454 case MBX_READ_LNK_STAT:
2455 case MBX_DUMP_MEMORY:
2457 case MBX_UPDATE_CFG:
2458 case MBX_KILL_BOARD:
2460 case MBX_LOAD_EXP_ROM:
2462 case MBX_DEL_LD_ENTRY:
2465 case MBX_SLI4_CONFIG:
2466 case MBX_READ_EVENT_LOG_STATUS:
2467 case MBX_WRITE_EVENT_LOG:
2468 case MBX_PORT_CAPABILITIES:
2469 case MBX_PORT_IOV_CONTROL:
2471 case MBX_SET_VARIABLE:
2472 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2473 "1226 mbox: set_variable 0x%x, 0x%x\n",
2475 mb->un.varWords[1]);
2476 if ((mb->un.varWords[0] == SETVAR_MLOMNT)
2477 && (mb->un.varWords[1] == 1)) {
2478 phba->wait_4_mlo_maint_flg = 1;
2479 } else if (mb->un.varWords[0] == SETVAR_MLORST) {
2480 phba->link_flag &= ~LS_LOOPBACK_MODE;
2481 phba->fc_topology = TOPOLOGY_PT_PT;
2484 case MBX_RUN_BIU_DIAG64:
2485 case MBX_READ_EVENT_LOG:
2486 case MBX_READ_SPARM64:
2490 case MBX_REG_LOGIN64:
2491 case MBX_CONFIG_PORT:
2492 case MBX_RUN_BIU_DIAG:
2494 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2495 "2742 Unknown Command 0x%x\n",
2504 * lpfc_bsg_issue_mbox - issues a mailbox command on behalf of an app
2505 * @phba: Pointer to HBA context object.
2506 * @mb: Pointer to a mailbox object.
2507 * @vport: Pointer to a vport object.
2509 * Allocate a tracking object, mailbox command memory, get a mailbox
2510 * from the mailbox pool, copy the caller mailbox command.
2512 * If offline and the sli is active we need to poll for the command (port is
2513 * being reset) and com-plete the job, otherwise issue the mailbox command and
2514 * let our completion handler finish the command.
2517 lpfc_bsg_issue_mbox(struct lpfc_hba *phba, struct fc_bsg_job *job,
2518 struct lpfc_vport *vport)
2520 LPFC_MBOXQ_t *pmboxq;
2523 struct bsg_job_data *dd_data;
2527 /* allocate our bsg tracking structure */
2528 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
2530 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2531 "2727 Failed allocation of dd_data\n");
2535 mb = kzalloc(PAGE_SIZE, GFP_KERNEL);
2541 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2548 size = job->request_payload.payload_len;
2549 job->reply->reply_payload_rcv_len =
2550 sg_copy_to_buffer(job->request_payload.sg_list,
2551 job->request_payload.sg_cnt,
2554 rc = lpfc_bsg_check_cmd_access(phba, mb, vport);
2558 mempool_free(pmboxq, phba->mbox_mem_pool);
2559 return rc; /* must be negative */
2562 memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
2563 pmb = &pmboxq->u.mb;
2564 memcpy(pmb, mb, sizeof(*pmb));
2565 pmb->mbxOwner = OWN_HOST;
2566 pmboxq->context1 = NULL;
2567 pmboxq->vport = vport;
2569 if ((vport->fc_flag & FC_OFFLINE_MODE) ||
2570 (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))) {
2571 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
2572 if (rc != MBX_SUCCESS) {
2573 if (rc != MBX_TIMEOUT) {
2576 mempool_free(pmboxq, phba->mbox_mem_pool);
2578 return (rc == MBX_TIMEOUT) ? -ETIME : -ENODEV;
2581 memcpy(mb, pmb, sizeof(*pmb));
2582 job->reply->reply_payload_rcv_len =
2583 sg_copy_from_buffer(job->reply_payload.sg_list,
2584 job->reply_payload.sg_cnt,
2588 mempool_free(pmboxq, phba->mbox_mem_pool);
2589 /* not waiting mbox already done */
2593 /* setup wake call as IOCB callback */
2594 pmboxq->mbox_cmpl = lpfc_bsg_wake_mbox_wait;
2595 /* setup context field to pass wait_queue pointer to wake function */
2596 pmboxq->context1 = dd_data;
2597 dd_data->type = TYPE_MBOX;
2598 dd_data->context_un.mbox.pmboxq = pmboxq;
2599 dd_data->context_un.mbox.mb = mb;
2600 dd_data->context_un.mbox.set_job = job;
2601 job->dd_data = dd_data;
2602 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
2603 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
2606 mempool_free(pmboxq, phba->mbox_mem_pool);
2614 * lpfc_bsg_mbox_cmd - process an fc bsg LPFC_BSG_VENDOR_MBOX command
2615 * @job: MBOX fc_bsg_job for LPFC_BSG_VENDOR_MBOX.
2618 lpfc_bsg_mbox_cmd(struct fc_bsg_job *job)
2620 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
2621 struct lpfc_hba *phba = vport->phba;
2624 /* in case no data is transferred */
2625 job->reply->reply_payload_rcv_len = 0;
2626 if (job->request_len <
2627 sizeof(struct fc_bsg_request) + sizeof(struct dfc_mbox_req)) {
2628 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2629 "2737 Received MBOX_REQ request below "
2635 if (job->request_payload.payload_len != PAGE_SIZE) {
2640 if (phba->sli.sli_flag & LPFC_BLOCK_MGMT_IO) {
2645 rc = lpfc_bsg_issue_mbox(phba, job, vport);
2650 job->reply->result = 0;
2651 job->dd_data = NULL;
2654 /* job submitted, will complete later*/
2655 rc = 0; /* return zero, no error */
2657 /* some error occurred */
2658 job->reply->result = rc;
2659 job->dd_data = NULL;
2666 * lpfc_bsg_menlo_cmd_cmp - lpfc_menlo_cmd completion handler
2667 * @phba: Pointer to HBA context object.
2668 * @cmdiocbq: Pointer to command iocb.
2669 * @rspiocbq: Pointer to response iocb.
2671 * This function is the completion handler for iocbs issued using
2672 * lpfc_menlo_cmd function. This function is called by the
2673 * ring event handler function without any lock held. This function
2674 * can be called from both worker thread context and interrupt
2675 * context. This function also can be called from another thread which
2676 * cleans up the SLI layer objects.
2677 * This function copies the contents of the response iocb to the
2678 * response iocb memory object provided by the caller of
2679 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
2680 * sleeps for the iocb completion.
2683 lpfc_bsg_menlo_cmd_cmp(struct lpfc_hba *phba,
2684 struct lpfc_iocbq *cmdiocbq,
2685 struct lpfc_iocbq *rspiocbq)
2687 struct bsg_job_data *dd_data;
2688 struct fc_bsg_job *job;
2690 struct lpfc_dmabuf *bmp;
2691 struct lpfc_bsg_menlo *menlo;
2692 unsigned long flags;
2693 struct menlo_response *menlo_resp;
2696 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2697 dd_data = cmdiocbq->context1;
2699 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2703 menlo = &dd_data->context_un.menlo;
2704 job = menlo->set_job;
2705 job->dd_data = NULL; /* so timeout handler does not reply */
2707 spin_lock_irqsave(&phba->hbalock, flags);
2708 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
2709 if (cmdiocbq->context2 && rspiocbq)
2710 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
2711 &rspiocbq->iocb, sizeof(IOCB_t));
2712 spin_unlock_irqrestore(&phba->hbalock, flags);
2715 rspiocbq = menlo->rspiocbq;
2716 rsp = &rspiocbq->iocb;
2718 pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
2719 job->request_payload.sg_cnt, DMA_TO_DEVICE);
2720 pci_unmap_sg(phba->pcidev, job->reply_payload.sg_list,
2721 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
2723 /* always return the xri, this would be used in the case
2724 * of a menlo download to allow the data to be sent as a continuation
2727 menlo_resp = (struct menlo_response *)
2728 job->reply->reply_data.vendor_reply.vendor_rsp;
2729 menlo_resp->xri = rsp->ulpContext;
2730 if (rsp->ulpStatus) {
2731 if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
2732 switch (rsp->un.ulpWord[4] & 0xff) {
2733 case IOERR_SEQUENCE_TIMEOUT:
2736 case IOERR_INVALID_RPI:
2746 job->reply->reply_payload_rcv_len =
2747 rsp->un.genreq64.bdl.bdeSize;
2749 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
2750 lpfc_sli_release_iocbq(phba, rspiocbq);
2751 lpfc_sli_release_iocbq(phba, cmdiocbq);
2754 /* make error code available to userspace */
2755 job->reply->result = rc;
2756 /* complete the job back to userspace */
2758 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2763 * lpfc_menlo_cmd - send an ioctl for menlo hardware
2764 * @job: fc_bsg_job to handle
2766 * This function issues a gen request 64 CR ioctl for all menlo cmd requests,
2767 * all the command completions will return the xri for the command.
2768 * For menlo data requests a gen request 64 CX is used to continue the exchange
2769 * supplied in the menlo request header xri field.
2772 lpfc_menlo_cmd(struct fc_bsg_job *job)
2774 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
2775 struct lpfc_hba *phba = vport->phba;
2776 struct lpfc_iocbq *cmdiocbq, *rspiocbq;
2779 struct menlo_command *menlo_cmd;
2780 struct menlo_response *menlo_resp;
2781 struct lpfc_dmabuf *bmp = NULL;
2784 struct scatterlist *sgel = NULL;
2787 struct bsg_job_data *dd_data;
2788 struct ulp_bde64 *bpl = NULL;
2790 /* in case no data is returned return just the return code */
2791 job->reply->reply_payload_rcv_len = 0;
2793 if (job->request_len <
2794 sizeof(struct fc_bsg_request) +
2795 sizeof(struct menlo_command)) {
2796 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2797 "2784 Received MENLO_CMD request below "
2803 if (job->reply_len <
2804 sizeof(struct fc_bsg_request) + sizeof(struct menlo_response)) {
2805 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2806 "2785 Received MENLO_CMD reply below "
2812 if (!(phba->menlo_flag & HBA_MENLO_SUPPORT)) {
2813 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2814 "2786 Adapter does not support menlo "
2820 menlo_cmd = (struct menlo_command *)
2821 job->request->rqst_data.h_vendor.vendor_cmd;
2823 menlo_resp = (struct menlo_response *)
2824 job->reply->reply_data.vendor_reply.vendor_rsp;
2826 /* allocate our bsg tracking structure */
2827 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
2829 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2830 "2787 Failed allocation of dd_data\n");
2835 bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2841 cmdiocbq = lpfc_sli_get_iocbq(phba);
2847 rspiocbq = lpfc_sli_get_iocbq(phba);
2853 rsp = &rspiocbq->iocb;
2855 bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
2861 INIT_LIST_HEAD(&bmp->list);
2862 bpl = (struct ulp_bde64 *) bmp->virt;
2863 request_nseg = pci_map_sg(phba->pcidev, job->request_payload.sg_list,
2864 job->request_payload.sg_cnt, DMA_TO_DEVICE);
2865 for_each_sg(job->request_payload.sg_list, sgel, request_nseg, numbde) {
2866 busaddr = sg_dma_address(sgel);
2867 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2868 bpl->tus.f.bdeSize = sg_dma_len(sgel);
2869 bpl->tus.w = cpu_to_le32(bpl->tus.w);
2870 bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
2871 bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
2875 reply_nseg = pci_map_sg(phba->pcidev, job->reply_payload.sg_list,
2876 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
2877 for_each_sg(job->reply_payload.sg_list, sgel, reply_nseg, numbde) {
2878 busaddr = sg_dma_address(sgel);
2879 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2880 bpl->tus.f.bdeSize = sg_dma_len(sgel);
2881 bpl->tus.w = cpu_to_le32(bpl->tus.w);
2882 bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
2883 bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
2887 cmd = &cmdiocbq->iocb;
2888 cmd->un.genreq64.bdl.ulpIoTag32 = 0;
2889 cmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
2890 cmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
2891 cmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
2892 cmd->un.genreq64.bdl.bdeSize =
2893 (request_nseg + reply_nseg) * sizeof(struct ulp_bde64);
2894 cmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
2895 cmd->un.genreq64.w5.hcsw.Dfctl = 0;
2896 cmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CMD;
2897 cmd->un.genreq64.w5.hcsw.Type = MENLO_TRANSPORT_TYPE; /* 0xfe */
2898 cmd->ulpBdeCount = 1;
2899 cmd->ulpClass = CLASS3;
2900 cmd->ulpOwner = OWN_CHIP;
2901 cmd->ulpLe = 1; /* Limited Edition */
2902 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
2903 cmdiocbq->vport = phba->pport;
2904 /* We want the firmware to timeout before we do */
2905 cmd->ulpTimeout = MENLO_TIMEOUT - 5;
2906 cmdiocbq->context3 = bmp;
2907 cmdiocbq->context2 = rspiocbq;
2908 cmdiocbq->iocb_cmpl = lpfc_bsg_menlo_cmd_cmp;
2909 cmdiocbq->context1 = dd_data;
2910 cmdiocbq->context2 = rspiocbq;
2911 if (menlo_cmd->cmd == LPFC_BSG_VENDOR_MENLO_CMD) {
2912 cmd->ulpCommand = CMD_GEN_REQUEST64_CR;
2913 cmd->ulpPU = MENLO_PU; /* 3 */
2914 cmd->un.ulpWord[4] = MENLO_DID; /* 0x0000FC0E */
2915 cmd->ulpContext = MENLO_CONTEXT; /* 0 */
2917 cmd->ulpCommand = CMD_GEN_REQUEST64_CX;
2919 cmd->un.ulpWord[4] = 0;
2920 cmd->ulpContext = menlo_cmd->xri;
2923 dd_data->type = TYPE_MENLO;
2924 dd_data->context_un.menlo.cmdiocbq = cmdiocbq;
2925 dd_data->context_un.menlo.rspiocbq = rspiocbq;
2926 dd_data->context_un.menlo.set_job = job;
2927 dd_data->context_un.menlo.bmp = bmp;
2929 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq,
2931 if (rc == IOCB_SUCCESS)
2932 return 0; /* done for now */
2934 /* iocb failed so cleanup */
2935 pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
2936 job->request_payload.sg_cnt, DMA_TO_DEVICE);
2937 pci_unmap_sg(phba->pcidev, job->reply_payload.sg_list,
2938 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
2940 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
2943 lpfc_sli_release_iocbq(phba, rspiocbq);
2945 lpfc_sli_release_iocbq(phba, cmdiocbq);
2951 /* make error code available to userspace */
2952 job->reply->result = rc;
2953 job->dd_data = NULL;
2957 * lpfc_bsg_hst_vendor - process a vendor-specific fc_bsg_job
2958 * @job: fc_bsg_job to handle
2961 lpfc_bsg_hst_vendor(struct fc_bsg_job *job)
2963 int command = job->request->rqst_data.h_vendor.vendor_cmd[0];
2967 case LPFC_BSG_VENDOR_SET_CT_EVENT:
2968 rc = lpfc_bsg_hba_set_event(job);
2970 case LPFC_BSG_VENDOR_GET_CT_EVENT:
2971 rc = lpfc_bsg_hba_get_event(job);
2973 case LPFC_BSG_VENDOR_SEND_MGMT_RESP:
2974 rc = lpfc_bsg_send_mgmt_rsp(job);
2976 case LPFC_BSG_VENDOR_DIAG_MODE:
2977 rc = lpfc_bsg_diag_mode(job);
2979 case LPFC_BSG_VENDOR_DIAG_TEST:
2980 rc = lpfc_bsg_diag_test(job);
2982 case LPFC_BSG_VENDOR_GET_MGMT_REV:
2983 rc = lpfc_bsg_get_dfc_rev(job);
2985 case LPFC_BSG_VENDOR_MBOX:
2986 rc = lpfc_bsg_mbox_cmd(job);
2988 case LPFC_BSG_VENDOR_MENLO_CMD:
2989 case LPFC_BSG_VENDOR_MENLO_DATA:
2990 rc = lpfc_menlo_cmd(job);
2994 job->reply->reply_payload_rcv_len = 0;
2995 /* make error code available to userspace */
2996 job->reply->result = rc;
3004 * lpfc_bsg_request - handle a bsg request from the FC transport
3005 * @job: fc_bsg_job to handle
3008 lpfc_bsg_request(struct fc_bsg_job *job)
3013 msgcode = job->request->msgcode;
3015 case FC_BSG_HST_VENDOR:
3016 rc = lpfc_bsg_hst_vendor(job);
3018 case FC_BSG_RPT_ELS:
3019 rc = lpfc_bsg_rport_els(job);
3022 rc = lpfc_bsg_send_mgmt_cmd(job);
3026 job->reply->reply_payload_rcv_len = 0;
3027 /* make error code available to userspace */
3028 job->reply->result = rc;
3036 * lpfc_bsg_timeout - handle timeout of a bsg request from the FC transport
3037 * @job: fc_bsg_job that has timed out
3039 * This function just aborts the job's IOCB. The aborted IOCB will return to
3040 * the waiting function which will handle passing the error back to userspace
3043 lpfc_bsg_timeout(struct fc_bsg_job *job)
3045 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
3046 struct lpfc_hba *phba = vport->phba;
3047 struct lpfc_iocbq *cmdiocb;
3048 struct lpfc_bsg_event *evt;
3049 struct lpfc_bsg_iocb *iocb;
3050 struct lpfc_bsg_mbox *mbox;
3051 struct lpfc_bsg_menlo *menlo;
3052 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
3053 struct bsg_job_data *dd_data;
3054 unsigned long flags;
3056 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3057 dd_data = (struct bsg_job_data *)job->dd_data;
3058 /* timeout and completion crossed paths if no dd_data */
3060 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3064 switch (dd_data->type) {
3066 iocb = &dd_data->context_un.iocb;
3067 cmdiocb = iocb->cmdiocbq;
3068 /* hint to completion handler that the job timed out */
3069 job->reply->result = -EAGAIN;
3070 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3071 /* this will call our completion handler */
3072 spin_lock_irq(&phba->hbalock);
3073 lpfc_sli_issue_abort_iotag(phba, pring, cmdiocb);
3074 spin_unlock_irq(&phba->hbalock);
3077 evt = dd_data->context_un.evt;
3078 /* this event has no job anymore */
3079 evt->set_job = NULL;
3080 job->dd_data = NULL;
3081 job->reply->reply_payload_rcv_len = 0;
3082 /* Return -EAGAIN which is our way of signallying the
3085 job->reply->result = -EAGAIN;
3086 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3090 mbox = &dd_data->context_un.mbox;
3091 /* this mbox has no job anymore */
3092 mbox->set_job = NULL;
3093 job->dd_data = NULL;
3094 job->reply->reply_payload_rcv_len = 0;
3095 job->reply->result = -EAGAIN;
3096 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3100 menlo = &dd_data->context_un.menlo;
3101 cmdiocb = menlo->cmdiocbq;
3102 /* hint to completion handler that the job timed out */
3103 job->reply->result = -EAGAIN;
3104 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3105 /* this will call our completion handler */
3106 spin_lock_irq(&phba->hbalock);
3107 lpfc_sli_issue_abort_iotag(phba, pring, cmdiocb);
3108 spin_unlock_irq(&phba->hbalock);
3111 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3115 /* scsi transport fc fc_bsg_job_timeout expects a zero return code,
3116 * otherwise an error message will be displayed on the console
3117 * so always return success (zero)