[SCSI] lpfc 8.3.22: Add new mailbox command and new BSG fix
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / scsi / lpfc / lpfc_bsg.c
blob5a4a2f36ae6d53d8a939ac0f8a80303bd5237a51
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. *
6 * www.emulex.com *
7 * *
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>
27 #include <scsi/scsi.h>
28 #include <scsi/scsi_host.h>
29 #include <scsi/scsi_transport_fc.h>
30 #include <scsi/scsi_bsg_fc.h>
31 #include <scsi/fc/fc_fs.h>
33 #include "lpfc_hw4.h"
34 #include "lpfc_hw.h"
35 #include "lpfc_sli.h"
36 #include "lpfc_sli4.h"
37 #include "lpfc_nl.h"
38 #include "lpfc_bsg.h"
39 #include "lpfc_disc.h"
40 #include "lpfc_scsi.h"
41 #include "lpfc.h"
42 #include "lpfc_logmsg.h"
43 #include "lpfc_crtn.h"
44 #include "lpfc_vport.h"
45 #include "lpfc_version.h"
47 struct lpfc_bsg_event {
48 struct list_head node;
49 struct kref kref;
50 wait_queue_head_t wq;
52 /* Event type and waiter identifiers */
53 uint32_t type_mask;
54 uint32_t req_id;
55 uint32_t reg_id;
57 /* next two flags are here for the auto-delete logic */
58 unsigned long wait_time_stamp;
59 int waiting;
61 /* seen and not seen events */
62 struct list_head events_to_get;
63 struct list_head events_to_see;
65 /* job waiting for this event to finish */
66 struct fc_bsg_job *set_job;
69 struct lpfc_bsg_iocb {
70 struct lpfc_iocbq *cmdiocbq;
71 struct lpfc_iocbq *rspiocbq;
72 struct lpfc_dmabuf *bmp;
73 struct lpfc_nodelist *ndlp;
75 /* job waiting for this iocb to finish */
76 struct fc_bsg_job *set_job;
79 struct lpfc_bsg_mbox {
80 LPFC_MBOXQ_t *pmboxq;
81 MAILBOX_t *mb;
82 struct lpfc_dmabuf *rxbmp; /* for BIU diags */
83 struct lpfc_dmabufext *dmp; /* for BIU diags */
84 uint8_t *ext; /* extended mailbox data */
85 uint32_t mbOffset; /* from app */
86 uint32_t inExtWLen; /* from app */
87 uint32_t outExtWLen; /* from app */
89 /* job waiting for this mbox command to finish */
90 struct fc_bsg_job *set_job;
93 #define MENLO_DID 0x0000FC0E
95 struct lpfc_bsg_menlo {
96 struct lpfc_iocbq *cmdiocbq;
97 struct lpfc_iocbq *rspiocbq;
98 struct lpfc_dmabuf *bmp;
100 /* job waiting for this iocb to finish */
101 struct fc_bsg_job *set_job;
104 #define TYPE_EVT 1
105 #define TYPE_IOCB 2
106 #define TYPE_MBOX 3
107 #define TYPE_MENLO 4
108 struct bsg_job_data {
109 uint32_t type;
110 union {
111 struct lpfc_bsg_event *evt;
112 struct lpfc_bsg_iocb iocb;
113 struct lpfc_bsg_mbox mbox;
114 struct lpfc_bsg_menlo menlo;
115 } context_un;
118 struct event_data {
119 struct list_head node;
120 uint32_t type;
121 uint32_t immed_dat;
122 void *data;
123 uint32_t len;
126 #define BUF_SZ_4K 4096
127 #define SLI_CT_ELX_LOOPBACK 0x10
129 enum ELX_LOOPBACK_CMD {
130 ELX_LOOPBACK_XRI_SETUP,
131 ELX_LOOPBACK_DATA,
134 #define ELX_LOOPBACK_HEADER_SZ \
135 (size_t)(&((struct lpfc_sli_ct_request *)NULL)->un)
137 struct lpfc_dmabufext {
138 struct lpfc_dmabuf dma;
139 uint32_t size;
140 uint32_t flag;
144 * lpfc_bsg_send_mgmt_cmd_cmp - lpfc_bsg_send_mgmt_cmd's completion handler
145 * @phba: Pointer to HBA context object.
146 * @cmdiocbq: Pointer to command iocb.
147 * @rspiocbq: Pointer to response iocb.
149 * This function is the completion handler for iocbs issued using
150 * lpfc_bsg_send_mgmt_cmd function. This function is called by the
151 * ring event handler function without any lock held. This function
152 * can be called from both worker thread context and interrupt
153 * context. This function also can be called from another thread which
154 * cleans up the SLI layer objects.
155 * This function copies the contents of the response iocb to the
156 * response iocb memory object provided by the caller of
157 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
158 * sleeps for the iocb completion.
160 static void
161 lpfc_bsg_send_mgmt_cmd_cmp(struct lpfc_hba *phba,
162 struct lpfc_iocbq *cmdiocbq,
163 struct lpfc_iocbq *rspiocbq)
165 struct bsg_job_data *dd_data;
166 struct fc_bsg_job *job;
167 IOCB_t *rsp;
168 struct lpfc_dmabuf *bmp;
169 struct lpfc_nodelist *ndlp;
170 struct lpfc_bsg_iocb *iocb;
171 unsigned long flags;
172 int rc = 0;
174 spin_lock_irqsave(&phba->ct_ev_lock, flags);
175 dd_data = cmdiocbq->context2;
176 if (!dd_data) {
177 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
178 lpfc_sli_release_iocbq(phba, cmdiocbq);
179 return;
182 iocb = &dd_data->context_un.iocb;
183 job = iocb->set_job;
184 job->dd_data = NULL; /* so timeout handler does not reply */
186 bmp = iocb->bmp;
187 rsp = &rspiocbq->iocb;
188 ndlp = cmdiocbq->context1;
190 pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
191 job->request_payload.sg_cnt, DMA_TO_DEVICE);
192 pci_unmap_sg(phba->pcidev, job->reply_payload.sg_list,
193 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
195 if (rsp->ulpStatus) {
196 if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
197 switch (rsp->un.ulpWord[4] & 0xff) {
198 case IOERR_SEQUENCE_TIMEOUT:
199 rc = -ETIMEDOUT;
200 break;
201 case IOERR_INVALID_RPI:
202 rc = -EFAULT;
203 break;
204 default:
205 rc = -EACCES;
206 break;
208 } else
209 rc = -EACCES;
210 } else
211 job->reply->reply_payload_rcv_len =
212 rsp->un.genreq64.bdl.bdeSize;
214 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
215 lpfc_sli_release_iocbq(phba, cmdiocbq);
216 lpfc_nlp_put(ndlp);
217 kfree(bmp);
218 kfree(dd_data);
219 /* make error code available to userspace */
220 job->reply->result = rc;
221 /* complete the job back to userspace */
222 job->job_done(job);
223 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
224 return;
228 * lpfc_bsg_send_mgmt_cmd - send a CT command from a bsg request
229 * @job: fc_bsg_job to handle
231 static int
232 lpfc_bsg_send_mgmt_cmd(struct fc_bsg_job *job)
234 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
235 struct lpfc_hba *phba = vport->phba;
236 struct lpfc_rport_data *rdata = job->rport->dd_data;
237 struct lpfc_nodelist *ndlp = rdata->pnode;
238 struct ulp_bde64 *bpl = NULL;
239 uint32_t timeout;
240 struct lpfc_iocbq *cmdiocbq = NULL;
241 IOCB_t *cmd;
242 struct lpfc_dmabuf *bmp = NULL;
243 int request_nseg;
244 int reply_nseg;
245 struct scatterlist *sgel = NULL;
246 int numbde;
247 dma_addr_t busaddr;
248 struct bsg_job_data *dd_data;
249 uint32_t creg_val;
250 int rc = 0;
251 int iocb_stat;
253 /* in case no data is transferred */
254 job->reply->reply_payload_rcv_len = 0;
256 /* allocate our bsg tracking structure */
257 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
258 if (!dd_data) {
259 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
260 "2733 Failed allocation of dd_data\n");
261 rc = -ENOMEM;
262 goto no_dd_data;
265 if (!lpfc_nlp_get(ndlp)) {
266 rc = -ENODEV;
267 goto no_ndlp;
270 bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
271 if (!bmp) {
272 rc = -ENOMEM;
273 goto free_ndlp;
276 if (ndlp->nlp_flag & NLP_ELS_SND_MASK) {
277 rc = -ENODEV;
278 goto free_bmp;
281 cmdiocbq = lpfc_sli_get_iocbq(phba);
282 if (!cmdiocbq) {
283 rc = -ENOMEM;
284 goto free_bmp;
287 cmd = &cmdiocbq->iocb;
288 bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
289 if (!bmp->virt) {
290 rc = -ENOMEM;
291 goto free_cmdiocbq;
294 INIT_LIST_HEAD(&bmp->list);
295 bpl = (struct ulp_bde64 *) bmp->virt;
296 request_nseg = pci_map_sg(phba->pcidev, job->request_payload.sg_list,
297 job->request_payload.sg_cnt, DMA_TO_DEVICE);
298 for_each_sg(job->request_payload.sg_list, sgel, request_nseg, numbde) {
299 busaddr = sg_dma_address(sgel);
300 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
301 bpl->tus.f.bdeSize = sg_dma_len(sgel);
302 bpl->tus.w = cpu_to_le32(bpl->tus.w);
303 bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
304 bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
305 bpl++;
308 reply_nseg = pci_map_sg(phba->pcidev, job->reply_payload.sg_list,
309 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
310 for_each_sg(job->reply_payload.sg_list, sgel, reply_nseg, numbde) {
311 busaddr = sg_dma_address(sgel);
312 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
313 bpl->tus.f.bdeSize = sg_dma_len(sgel);
314 bpl->tus.w = cpu_to_le32(bpl->tus.w);
315 bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
316 bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
317 bpl++;
320 cmd->un.genreq64.bdl.ulpIoTag32 = 0;
321 cmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
322 cmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
323 cmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
324 cmd->un.genreq64.bdl.bdeSize =
325 (request_nseg + reply_nseg) * sizeof(struct ulp_bde64);
326 cmd->ulpCommand = CMD_GEN_REQUEST64_CR;
327 cmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
328 cmd->un.genreq64.w5.hcsw.Dfctl = 0;
329 cmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
330 cmd->un.genreq64.w5.hcsw.Type = FC_TYPE_CT;
331 cmd->ulpBdeCount = 1;
332 cmd->ulpLe = 1;
333 cmd->ulpClass = CLASS3;
334 cmd->ulpContext = ndlp->nlp_rpi;
335 cmd->ulpOwner = OWN_CHIP;
336 cmdiocbq->vport = phba->pport;
337 cmdiocbq->context3 = bmp;
338 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
339 timeout = phba->fc_ratov * 2;
340 cmd->ulpTimeout = timeout;
342 cmdiocbq->iocb_cmpl = lpfc_bsg_send_mgmt_cmd_cmp;
343 cmdiocbq->context1 = ndlp;
344 cmdiocbq->context2 = dd_data;
345 dd_data->type = TYPE_IOCB;
346 dd_data->context_un.iocb.cmdiocbq = cmdiocbq;
347 dd_data->context_un.iocb.set_job = job;
348 dd_data->context_un.iocb.bmp = bmp;
350 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
351 creg_val = readl(phba->HCregaddr);
352 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
353 writel(creg_val, phba->HCregaddr);
354 readl(phba->HCregaddr); /* flush */
357 iocb_stat = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq, 0);
358 if (iocb_stat == IOCB_SUCCESS)
359 return 0; /* done for now */
360 else if (iocb_stat == IOCB_BUSY)
361 rc = -EAGAIN;
362 else
363 rc = -EIO;
366 /* iocb failed so cleanup */
367 pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
368 job->request_payload.sg_cnt, DMA_TO_DEVICE);
369 pci_unmap_sg(phba->pcidev, job->reply_payload.sg_list,
370 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
372 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
374 free_cmdiocbq:
375 lpfc_sli_release_iocbq(phba, cmdiocbq);
376 free_bmp:
377 kfree(bmp);
378 free_ndlp:
379 lpfc_nlp_put(ndlp);
380 no_ndlp:
381 kfree(dd_data);
382 no_dd_data:
383 /* make error code available to userspace */
384 job->reply->result = rc;
385 job->dd_data = NULL;
386 return rc;
390 * lpfc_bsg_rport_els_cmp - lpfc_bsg_rport_els's completion handler
391 * @phba: Pointer to HBA context object.
392 * @cmdiocbq: Pointer to command iocb.
393 * @rspiocbq: Pointer to response iocb.
395 * This function is the completion handler for iocbs issued using
396 * lpfc_bsg_rport_els_cmp function. This function is called by the
397 * ring event handler function without any lock held. This function
398 * can be called from both worker thread context and interrupt
399 * context. This function also can be called from other thread which
400 * cleans up the SLI layer objects.
401 * This function copies the contents of the response iocb to the
402 * response iocb memory object provided by the caller of
403 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
404 * sleeps for the iocb completion.
406 static void
407 lpfc_bsg_rport_els_cmp(struct lpfc_hba *phba,
408 struct lpfc_iocbq *cmdiocbq,
409 struct lpfc_iocbq *rspiocbq)
411 struct bsg_job_data *dd_data;
412 struct fc_bsg_job *job;
413 IOCB_t *rsp;
414 struct lpfc_nodelist *ndlp;
415 struct lpfc_dmabuf *pbuflist = NULL;
416 struct fc_bsg_ctels_reply *els_reply;
417 uint8_t *rjt_data;
418 unsigned long flags;
419 int rc = 0;
421 spin_lock_irqsave(&phba->ct_ev_lock, flags);
422 dd_data = cmdiocbq->context1;
423 /* normal completion and timeout crossed paths, already done */
424 if (!dd_data) {
425 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
426 return;
429 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
430 if (cmdiocbq->context2 && rspiocbq)
431 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
432 &rspiocbq->iocb, sizeof(IOCB_t));
434 job = dd_data->context_un.iocb.set_job;
435 cmdiocbq = dd_data->context_un.iocb.cmdiocbq;
436 rspiocbq = dd_data->context_un.iocb.rspiocbq;
437 rsp = &rspiocbq->iocb;
438 ndlp = dd_data->context_un.iocb.ndlp;
440 pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
441 job->request_payload.sg_cnt, DMA_TO_DEVICE);
442 pci_unmap_sg(phba->pcidev, job->reply_payload.sg_list,
443 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
445 if (job->reply->result == -EAGAIN)
446 rc = -EAGAIN;
447 else if (rsp->ulpStatus == IOSTAT_SUCCESS)
448 job->reply->reply_payload_rcv_len =
449 rsp->un.elsreq64.bdl.bdeSize;
450 else if (rsp->ulpStatus == IOSTAT_LS_RJT) {
451 job->reply->reply_payload_rcv_len =
452 sizeof(struct fc_bsg_ctels_reply);
453 /* LS_RJT data returned in word 4 */
454 rjt_data = (uint8_t *)&rsp->un.ulpWord[4];
455 els_reply = &job->reply->reply_data.ctels_reply;
456 els_reply->status = FC_CTELS_STATUS_REJECT;
457 els_reply->rjt_data.action = rjt_data[3];
458 els_reply->rjt_data.reason_code = rjt_data[2];
459 els_reply->rjt_data.reason_explanation = rjt_data[1];
460 els_reply->rjt_data.vendor_unique = rjt_data[0];
461 } else
462 rc = -EIO;
464 pbuflist = (struct lpfc_dmabuf *) cmdiocbq->context3;
465 lpfc_mbuf_free(phba, pbuflist->virt, pbuflist->phys);
466 lpfc_sli_release_iocbq(phba, rspiocbq);
467 lpfc_sli_release_iocbq(phba, cmdiocbq);
468 lpfc_nlp_put(ndlp);
469 kfree(dd_data);
470 /* make error code available to userspace */
471 job->reply->result = rc;
472 job->dd_data = NULL;
473 /* complete the job back to userspace */
474 job->job_done(job);
475 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
476 return;
480 * lpfc_bsg_rport_els - send an ELS command from a bsg request
481 * @job: fc_bsg_job to handle
483 static int
484 lpfc_bsg_rport_els(struct fc_bsg_job *job)
486 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
487 struct lpfc_hba *phba = vport->phba;
488 struct lpfc_rport_data *rdata = job->rport->dd_data;
489 struct lpfc_nodelist *ndlp = rdata->pnode;
490 uint32_t elscmd;
491 uint32_t cmdsize;
492 uint32_t rspsize;
493 struct lpfc_iocbq *rspiocbq;
494 struct lpfc_iocbq *cmdiocbq;
495 IOCB_t *rsp;
496 uint16_t rpi = 0;
497 struct lpfc_dmabuf *pcmd;
498 struct lpfc_dmabuf *prsp;
499 struct lpfc_dmabuf *pbuflist = NULL;
500 struct ulp_bde64 *bpl;
501 int request_nseg;
502 int reply_nseg;
503 struct scatterlist *sgel = NULL;
504 int numbde;
505 dma_addr_t busaddr;
506 struct bsg_job_data *dd_data;
507 uint32_t creg_val;
508 int rc = 0;
510 /* in case no data is transferred */
511 job->reply->reply_payload_rcv_len = 0;
513 /* allocate our bsg tracking structure */
514 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
515 if (!dd_data) {
516 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
517 "2735 Failed allocation of dd_data\n");
518 rc = -ENOMEM;
519 goto no_dd_data;
522 if (!lpfc_nlp_get(ndlp)) {
523 rc = -ENODEV;
524 goto free_dd_data;
527 elscmd = job->request->rqst_data.r_els.els_code;
528 cmdsize = job->request_payload.payload_len;
529 rspsize = job->reply_payload.payload_len;
530 rspiocbq = lpfc_sli_get_iocbq(phba);
531 if (!rspiocbq) {
532 lpfc_nlp_put(ndlp);
533 rc = -ENOMEM;
534 goto free_dd_data;
537 rsp = &rspiocbq->iocb;
538 rpi = ndlp->nlp_rpi;
540 cmdiocbq = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp,
541 ndlp->nlp_DID, elscmd);
542 if (!cmdiocbq) {
543 rc = -EIO;
544 goto free_rspiocbq;
547 /* prep els iocb set context1 to the ndlp, context2 to the command
548 * dmabuf, context3 holds the data dmabuf
550 pcmd = (struct lpfc_dmabuf *) cmdiocbq->context2;
551 prsp = (struct lpfc_dmabuf *) pcmd->list.next;
552 lpfc_mbuf_free(phba, pcmd->virt, pcmd->phys);
553 kfree(pcmd);
554 lpfc_mbuf_free(phba, prsp->virt, prsp->phys);
555 kfree(prsp);
556 cmdiocbq->context2 = NULL;
558 pbuflist = (struct lpfc_dmabuf *) cmdiocbq->context3;
559 bpl = (struct ulp_bde64 *) pbuflist->virt;
561 request_nseg = pci_map_sg(phba->pcidev, job->request_payload.sg_list,
562 job->request_payload.sg_cnt, DMA_TO_DEVICE);
563 for_each_sg(job->request_payload.sg_list, sgel, request_nseg, numbde) {
564 busaddr = sg_dma_address(sgel);
565 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
566 bpl->tus.f.bdeSize = sg_dma_len(sgel);
567 bpl->tus.w = cpu_to_le32(bpl->tus.w);
568 bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
569 bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
570 bpl++;
573 reply_nseg = pci_map_sg(phba->pcidev, job->reply_payload.sg_list,
574 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
575 for_each_sg(job->reply_payload.sg_list, sgel, reply_nseg, numbde) {
576 busaddr = sg_dma_address(sgel);
577 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
578 bpl->tus.f.bdeSize = sg_dma_len(sgel);
579 bpl->tus.w = cpu_to_le32(bpl->tus.w);
580 bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
581 bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
582 bpl++;
584 cmdiocbq->iocb.un.elsreq64.bdl.bdeSize =
585 (request_nseg + reply_nseg) * sizeof(struct ulp_bde64);
586 cmdiocbq->iocb.ulpContext = rpi;
587 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
588 cmdiocbq->context1 = NULL;
589 cmdiocbq->context2 = NULL;
591 cmdiocbq->iocb_cmpl = lpfc_bsg_rport_els_cmp;
592 cmdiocbq->context1 = dd_data;
593 cmdiocbq->context2 = rspiocbq;
594 dd_data->type = TYPE_IOCB;
595 dd_data->context_un.iocb.cmdiocbq = cmdiocbq;
596 dd_data->context_un.iocb.rspiocbq = rspiocbq;
597 dd_data->context_un.iocb.set_job = job;
598 dd_data->context_un.iocb.bmp = NULL;;
599 dd_data->context_un.iocb.ndlp = ndlp;
601 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
602 creg_val = readl(phba->HCregaddr);
603 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
604 writel(creg_val, phba->HCregaddr);
605 readl(phba->HCregaddr); /* flush */
607 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq, 0);
608 lpfc_nlp_put(ndlp);
609 if (rc == IOCB_SUCCESS)
610 return 0; /* done for now */
611 else if (rc == IOCB_BUSY)
612 rc = -EAGAIN;
613 else
614 rc = -EIO;
616 pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
617 job->request_payload.sg_cnt, DMA_TO_DEVICE);
618 pci_unmap_sg(phba->pcidev, job->reply_payload.sg_list,
619 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
621 lpfc_mbuf_free(phba, pbuflist->virt, pbuflist->phys);
623 lpfc_sli_release_iocbq(phba, cmdiocbq);
625 free_rspiocbq:
626 lpfc_sli_release_iocbq(phba, rspiocbq);
628 free_dd_data:
629 kfree(dd_data);
631 no_dd_data:
632 /* make error code available to userspace */
633 job->reply->result = rc;
634 job->dd_data = NULL;
635 return rc;
639 * lpfc_bsg_event_free - frees an allocated event structure
640 * @kref: Pointer to a kref.
642 * Called from kref_put. Back cast the kref into an event structure address.
643 * Free any events to get, delete associated nodes, free any events to see,
644 * free any data then free the event itself.
646 static void
647 lpfc_bsg_event_free(struct kref *kref)
649 struct lpfc_bsg_event *evt = container_of(kref, struct lpfc_bsg_event,
650 kref);
651 struct event_data *ed;
653 list_del(&evt->node);
655 while (!list_empty(&evt->events_to_get)) {
656 ed = list_entry(evt->events_to_get.next, typeof(*ed), node);
657 list_del(&ed->node);
658 kfree(ed->data);
659 kfree(ed);
662 while (!list_empty(&evt->events_to_see)) {
663 ed = list_entry(evt->events_to_see.next, typeof(*ed), node);
664 list_del(&ed->node);
665 kfree(ed->data);
666 kfree(ed);
669 kfree(evt);
673 * lpfc_bsg_event_ref - increments the kref for an event
674 * @evt: Pointer to an event structure.
676 static inline void
677 lpfc_bsg_event_ref(struct lpfc_bsg_event *evt)
679 kref_get(&evt->kref);
683 * lpfc_bsg_event_unref - Uses kref_put to free an event structure
684 * @evt: Pointer to an event structure.
686 static inline void
687 lpfc_bsg_event_unref(struct lpfc_bsg_event *evt)
689 kref_put(&evt->kref, lpfc_bsg_event_free);
693 * lpfc_bsg_event_new - allocate and initialize a event structure
694 * @ev_mask: Mask of events.
695 * @ev_reg_id: Event reg id.
696 * @ev_req_id: Event request id.
698 static struct lpfc_bsg_event *
699 lpfc_bsg_event_new(uint32_t ev_mask, int ev_reg_id, uint32_t ev_req_id)
701 struct lpfc_bsg_event *evt = kzalloc(sizeof(*evt), GFP_KERNEL);
703 if (!evt)
704 return NULL;
706 INIT_LIST_HEAD(&evt->events_to_get);
707 INIT_LIST_HEAD(&evt->events_to_see);
708 evt->type_mask = ev_mask;
709 evt->req_id = ev_req_id;
710 evt->reg_id = ev_reg_id;
711 evt->wait_time_stamp = jiffies;
712 init_waitqueue_head(&evt->wq);
713 kref_init(&evt->kref);
714 return evt;
718 * diag_cmd_data_free - Frees an lpfc dma buffer extension
719 * @phba: Pointer to HBA context object.
720 * @mlist: Pointer to an lpfc dma buffer extension.
722 static int
723 diag_cmd_data_free(struct lpfc_hba *phba, struct lpfc_dmabufext *mlist)
725 struct lpfc_dmabufext *mlast;
726 struct pci_dev *pcidev;
727 struct list_head head, *curr, *next;
729 if ((!mlist) || (!lpfc_is_link_up(phba) &&
730 (phba->link_flag & LS_LOOPBACK_MODE))) {
731 return 0;
734 pcidev = phba->pcidev;
735 list_add_tail(&head, &mlist->dma.list);
737 list_for_each_safe(curr, next, &head) {
738 mlast = list_entry(curr, struct lpfc_dmabufext , dma.list);
739 if (mlast->dma.virt)
740 dma_free_coherent(&pcidev->dev,
741 mlast->size,
742 mlast->dma.virt,
743 mlast->dma.phys);
744 kfree(mlast);
746 return 0;
750 * lpfc_bsg_ct_unsol_event - process an unsolicited CT command
751 * @phba:
752 * @pring:
753 * @piocbq:
755 * This function is called when an unsolicited CT command is received. It
756 * forwards the event to any processes registered to receive CT events.
759 lpfc_bsg_ct_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
760 struct lpfc_iocbq *piocbq)
762 uint32_t evt_req_id = 0;
763 uint32_t cmd;
764 uint32_t len;
765 struct lpfc_dmabuf *dmabuf = NULL;
766 struct lpfc_bsg_event *evt;
767 struct event_data *evt_dat = NULL;
768 struct lpfc_iocbq *iocbq;
769 size_t offset = 0;
770 struct list_head head;
771 struct ulp_bde64 *bde;
772 dma_addr_t dma_addr;
773 int i;
774 struct lpfc_dmabuf *bdeBuf1 = piocbq->context2;
775 struct lpfc_dmabuf *bdeBuf2 = piocbq->context3;
776 struct lpfc_hbq_entry *hbqe;
777 struct lpfc_sli_ct_request *ct_req;
778 struct fc_bsg_job *job = NULL;
779 unsigned long flags;
780 int size = 0;
782 INIT_LIST_HEAD(&head);
783 list_add_tail(&head, &piocbq->list);
785 if (piocbq->iocb.ulpBdeCount == 0 ||
786 piocbq->iocb.un.cont64[0].tus.f.bdeSize == 0)
787 goto error_ct_unsol_exit;
789 if (phba->link_state == LPFC_HBA_ERROR ||
790 (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)))
791 goto error_ct_unsol_exit;
793 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
794 dmabuf = bdeBuf1;
795 else {
796 dma_addr = getPaddr(piocbq->iocb.un.cont64[0].addrHigh,
797 piocbq->iocb.un.cont64[0].addrLow);
798 dmabuf = lpfc_sli_ringpostbuf_get(phba, pring, dma_addr);
800 if (dmabuf == NULL)
801 goto error_ct_unsol_exit;
802 ct_req = (struct lpfc_sli_ct_request *)dmabuf->virt;
803 evt_req_id = ct_req->FsType;
804 cmd = ct_req->CommandResponse.bits.CmdRsp;
805 len = ct_req->CommandResponse.bits.Size;
806 if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
807 lpfc_sli_ringpostbuf_put(phba, pring, dmabuf);
809 spin_lock_irqsave(&phba->ct_ev_lock, flags);
810 list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
811 if (!(evt->type_mask & FC_REG_CT_EVENT) ||
812 evt->req_id != evt_req_id)
813 continue;
815 lpfc_bsg_event_ref(evt);
816 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
817 evt_dat = kzalloc(sizeof(*evt_dat), GFP_KERNEL);
818 if (evt_dat == NULL) {
819 spin_lock_irqsave(&phba->ct_ev_lock, flags);
820 lpfc_bsg_event_unref(evt);
821 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
822 "2614 Memory allocation failed for "
823 "CT event\n");
824 break;
827 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
828 /* take accumulated byte count from the last iocbq */
829 iocbq = list_entry(head.prev, typeof(*iocbq), list);
830 evt_dat->len = iocbq->iocb.unsli3.rcvsli3.acc_len;
831 } else {
832 list_for_each_entry(iocbq, &head, list) {
833 for (i = 0; i < iocbq->iocb.ulpBdeCount; i++)
834 evt_dat->len +=
835 iocbq->iocb.un.cont64[i].tus.f.bdeSize;
839 evt_dat->data = kzalloc(evt_dat->len, GFP_KERNEL);
840 if (evt_dat->data == NULL) {
841 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
842 "2615 Memory allocation failed for "
843 "CT event data, size %d\n",
844 evt_dat->len);
845 kfree(evt_dat);
846 spin_lock_irqsave(&phba->ct_ev_lock, flags);
847 lpfc_bsg_event_unref(evt);
848 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
849 goto error_ct_unsol_exit;
852 list_for_each_entry(iocbq, &head, list) {
853 size = 0;
854 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
855 bdeBuf1 = iocbq->context2;
856 bdeBuf2 = iocbq->context3;
858 for (i = 0; i < iocbq->iocb.ulpBdeCount; i++) {
859 if (phba->sli3_options &
860 LPFC_SLI3_HBQ_ENABLED) {
861 if (i == 0) {
862 hbqe = (struct lpfc_hbq_entry *)
863 &iocbq->iocb.un.ulpWord[0];
864 size = hbqe->bde.tus.f.bdeSize;
865 dmabuf = bdeBuf1;
866 } else if (i == 1) {
867 hbqe = (struct lpfc_hbq_entry *)
868 &iocbq->iocb.unsli3.
869 sli3Words[4];
870 size = hbqe->bde.tus.f.bdeSize;
871 dmabuf = bdeBuf2;
873 if ((offset + size) > evt_dat->len)
874 size = evt_dat->len - offset;
875 } else {
876 size = iocbq->iocb.un.cont64[i].
877 tus.f.bdeSize;
878 bde = &iocbq->iocb.un.cont64[i];
879 dma_addr = getPaddr(bde->addrHigh,
880 bde->addrLow);
881 dmabuf = lpfc_sli_ringpostbuf_get(phba,
882 pring, dma_addr);
884 if (!dmabuf) {
885 lpfc_printf_log(phba, KERN_ERR,
886 LOG_LIBDFC, "2616 No dmabuf "
887 "found for iocbq 0x%p\n",
888 iocbq);
889 kfree(evt_dat->data);
890 kfree(evt_dat);
891 spin_lock_irqsave(&phba->ct_ev_lock,
892 flags);
893 lpfc_bsg_event_unref(evt);
894 spin_unlock_irqrestore(
895 &phba->ct_ev_lock, flags);
896 goto error_ct_unsol_exit;
898 memcpy((char *)(evt_dat->data) + offset,
899 dmabuf->virt, size);
900 offset += size;
901 if (evt_req_id != SLI_CT_ELX_LOOPBACK &&
902 !(phba->sli3_options &
903 LPFC_SLI3_HBQ_ENABLED)) {
904 lpfc_sli_ringpostbuf_put(phba, pring,
905 dmabuf);
906 } else {
907 switch (cmd) {
908 case ELX_LOOPBACK_DATA:
909 diag_cmd_data_free(phba,
910 (struct lpfc_dmabufext *)
911 dmabuf);
912 break;
913 case ELX_LOOPBACK_XRI_SETUP:
914 if ((phba->sli_rev ==
915 LPFC_SLI_REV2) ||
916 (phba->sli3_options &
917 LPFC_SLI3_HBQ_ENABLED
918 )) {
919 lpfc_in_buf_free(phba,
920 dmabuf);
921 } else {
922 lpfc_post_buffer(phba,
923 pring,
926 break;
927 default:
928 if (!(phba->sli3_options &
929 LPFC_SLI3_HBQ_ENABLED))
930 lpfc_post_buffer(phba,
931 pring,
933 break;
939 spin_lock_irqsave(&phba->ct_ev_lock, flags);
940 if (phba->sli_rev == LPFC_SLI_REV4) {
941 evt_dat->immed_dat = phba->ctx_idx;
942 phba->ctx_idx = (phba->ctx_idx + 1) % 64;
943 /* Provide warning for over-run of the ct_ctx array */
944 if (phba->ct_ctx[evt_dat->immed_dat].flags &
945 UNSOL_VALID)
946 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
947 "2717 CT context array entry "
948 "[%d] over-run: oxid:x%x, "
949 "sid:x%x\n", phba->ctx_idx,
950 phba->ct_ctx[
951 evt_dat->immed_dat].oxid,
952 phba->ct_ctx[
953 evt_dat->immed_dat].SID);
954 phba->ct_ctx[evt_dat->immed_dat].oxid =
955 piocbq->iocb.ulpContext;
956 phba->ct_ctx[evt_dat->immed_dat].SID =
957 piocbq->iocb.un.rcvels.remoteID;
958 phba->ct_ctx[evt_dat->immed_dat].flags = UNSOL_VALID;
959 } else
960 evt_dat->immed_dat = piocbq->iocb.ulpContext;
962 evt_dat->type = FC_REG_CT_EVENT;
963 list_add(&evt_dat->node, &evt->events_to_see);
964 if (evt_req_id == SLI_CT_ELX_LOOPBACK) {
965 wake_up_interruptible(&evt->wq);
966 lpfc_bsg_event_unref(evt);
967 break;
970 list_move(evt->events_to_see.prev, &evt->events_to_get);
971 lpfc_bsg_event_unref(evt);
973 job = evt->set_job;
974 evt->set_job = NULL;
975 if (job) {
976 job->reply->reply_payload_rcv_len = size;
977 /* make error code available to userspace */
978 job->reply->result = 0;
979 job->dd_data = NULL;
980 /* complete the job back to userspace */
981 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
982 job->job_done(job);
983 spin_lock_irqsave(&phba->ct_ev_lock, flags);
986 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
988 error_ct_unsol_exit:
989 if (!list_empty(&head))
990 list_del(&head);
991 if (evt_req_id == SLI_CT_ELX_LOOPBACK)
992 return 0;
993 return 1;
997 * lpfc_bsg_hba_set_event - process a SET_EVENT bsg vendor command
998 * @job: SET_EVENT fc_bsg_job
1000 static int
1001 lpfc_bsg_hba_set_event(struct fc_bsg_job *job)
1003 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
1004 struct lpfc_hba *phba = vport->phba;
1005 struct set_ct_event *event_req;
1006 struct lpfc_bsg_event *evt;
1007 int rc = 0;
1008 struct bsg_job_data *dd_data = NULL;
1009 uint32_t ev_mask;
1010 unsigned long flags;
1012 if (job->request_len <
1013 sizeof(struct fc_bsg_request) + sizeof(struct set_ct_event)) {
1014 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1015 "2612 Received SET_CT_EVENT below minimum "
1016 "size\n");
1017 rc = -EINVAL;
1018 goto job_error;
1021 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
1022 if (dd_data == NULL) {
1023 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1024 "2734 Failed allocation of dd_data\n");
1025 rc = -ENOMEM;
1026 goto job_error;
1029 event_req = (struct set_ct_event *)
1030 job->request->rqst_data.h_vendor.vendor_cmd;
1031 ev_mask = ((uint32_t)(unsigned long)event_req->type_mask &
1032 FC_REG_EVENT_MASK);
1033 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1034 list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
1035 if (evt->reg_id == event_req->ev_reg_id) {
1036 lpfc_bsg_event_ref(evt);
1037 evt->wait_time_stamp = jiffies;
1038 break;
1041 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1043 if (&evt->node == &phba->ct_ev_waiters) {
1044 /* no event waiting struct yet - first call */
1045 evt = lpfc_bsg_event_new(ev_mask, event_req->ev_reg_id,
1046 event_req->ev_req_id);
1047 if (!evt) {
1048 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1049 "2617 Failed allocation of event "
1050 "waiter\n");
1051 rc = -ENOMEM;
1052 goto job_error;
1055 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1056 list_add(&evt->node, &phba->ct_ev_waiters);
1057 lpfc_bsg_event_ref(evt);
1058 evt->wait_time_stamp = jiffies;
1059 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1062 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1063 evt->waiting = 1;
1064 dd_data->type = TYPE_EVT;
1065 dd_data->context_un.evt = evt;
1066 evt->set_job = job; /* for unsolicited command */
1067 job->dd_data = dd_data; /* for fc transport timeout callback*/
1068 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1069 return 0; /* call job done later */
1071 job_error:
1072 if (dd_data != NULL)
1073 kfree(dd_data);
1075 job->dd_data = NULL;
1076 return rc;
1080 * lpfc_bsg_hba_get_event - process a GET_EVENT bsg vendor command
1081 * @job: GET_EVENT fc_bsg_job
1083 static int
1084 lpfc_bsg_hba_get_event(struct fc_bsg_job *job)
1086 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
1087 struct lpfc_hba *phba = vport->phba;
1088 struct get_ct_event *event_req;
1089 struct get_ct_event_reply *event_reply;
1090 struct lpfc_bsg_event *evt;
1091 struct event_data *evt_dat = NULL;
1092 unsigned long flags;
1093 uint32_t rc = 0;
1095 if (job->request_len <
1096 sizeof(struct fc_bsg_request) + sizeof(struct get_ct_event)) {
1097 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1098 "2613 Received GET_CT_EVENT request below "
1099 "minimum size\n");
1100 rc = -EINVAL;
1101 goto job_error;
1104 event_req = (struct get_ct_event *)
1105 job->request->rqst_data.h_vendor.vendor_cmd;
1107 event_reply = (struct get_ct_event_reply *)
1108 job->reply->reply_data.vendor_reply.vendor_rsp;
1109 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1110 list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
1111 if (evt->reg_id == event_req->ev_reg_id) {
1112 if (list_empty(&evt->events_to_get))
1113 break;
1114 lpfc_bsg_event_ref(evt);
1115 evt->wait_time_stamp = jiffies;
1116 evt_dat = list_entry(evt->events_to_get.prev,
1117 struct event_data, node);
1118 list_del(&evt_dat->node);
1119 break;
1122 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1124 /* The app may continue to ask for event data until it gets
1125 * an error indicating that there isn't anymore
1127 if (evt_dat == NULL) {
1128 job->reply->reply_payload_rcv_len = 0;
1129 rc = -ENOENT;
1130 goto job_error;
1133 if (evt_dat->len > job->request_payload.payload_len) {
1134 evt_dat->len = job->request_payload.payload_len;
1135 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1136 "2618 Truncated event data at %d "
1137 "bytes\n",
1138 job->request_payload.payload_len);
1141 event_reply->type = evt_dat->type;
1142 event_reply->immed_data = evt_dat->immed_dat;
1143 if (evt_dat->len > 0)
1144 job->reply->reply_payload_rcv_len =
1145 sg_copy_from_buffer(job->request_payload.sg_list,
1146 job->request_payload.sg_cnt,
1147 evt_dat->data, evt_dat->len);
1148 else
1149 job->reply->reply_payload_rcv_len = 0;
1151 if (evt_dat) {
1152 kfree(evt_dat->data);
1153 kfree(evt_dat);
1156 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1157 lpfc_bsg_event_unref(evt);
1158 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1159 job->dd_data = NULL;
1160 job->reply->result = 0;
1161 job->job_done(job);
1162 return 0;
1164 job_error:
1165 job->dd_data = NULL;
1166 job->reply->result = rc;
1167 return rc;
1171 * lpfc_issue_ct_rsp_cmp - lpfc_issue_ct_rsp's completion handler
1172 * @phba: Pointer to HBA context object.
1173 * @cmdiocbq: Pointer to command iocb.
1174 * @rspiocbq: Pointer to response iocb.
1176 * This function is the completion handler for iocbs issued using
1177 * lpfc_issue_ct_rsp_cmp function. This function is called by the
1178 * ring event handler function without any lock held. This function
1179 * can be called from both worker thread context and interrupt
1180 * context. This function also can be called from other thread which
1181 * cleans up the SLI layer objects.
1182 * This function copy the contents of the response iocb to the
1183 * response iocb memory object provided by the caller of
1184 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
1185 * sleeps for the iocb completion.
1187 static void
1188 lpfc_issue_ct_rsp_cmp(struct lpfc_hba *phba,
1189 struct lpfc_iocbq *cmdiocbq,
1190 struct lpfc_iocbq *rspiocbq)
1192 struct bsg_job_data *dd_data;
1193 struct fc_bsg_job *job;
1194 IOCB_t *rsp;
1195 struct lpfc_dmabuf *bmp;
1196 struct lpfc_nodelist *ndlp;
1197 unsigned long flags;
1198 int rc = 0;
1200 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1201 dd_data = cmdiocbq->context2;
1202 /* normal completion and timeout crossed paths, already done */
1203 if (!dd_data) {
1204 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1205 return;
1208 job = dd_data->context_un.iocb.set_job;
1209 bmp = dd_data->context_un.iocb.bmp;
1210 rsp = &rspiocbq->iocb;
1211 ndlp = dd_data->context_un.iocb.ndlp;
1213 pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
1214 job->request_payload.sg_cnt, DMA_TO_DEVICE);
1216 if (rsp->ulpStatus) {
1217 if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
1218 switch (rsp->un.ulpWord[4] & 0xff) {
1219 case IOERR_SEQUENCE_TIMEOUT:
1220 rc = -ETIMEDOUT;
1221 break;
1222 case IOERR_INVALID_RPI:
1223 rc = -EFAULT;
1224 break;
1225 default:
1226 rc = -EACCES;
1227 break;
1229 } else
1230 rc = -EACCES;
1231 } else
1232 job->reply->reply_payload_rcv_len =
1233 rsp->un.genreq64.bdl.bdeSize;
1235 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
1236 lpfc_sli_release_iocbq(phba, cmdiocbq);
1237 lpfc_nlp_put(ndlp);
1238 kfree(bmp);
1239 kfree(dd_data);
1240 /* make error code available to userspace */
1241 job->reply->result = rc;
1242 job->dd_data = NULL;
1243 /* complete the job back to userspace */
1244 job->job_done(job);
1245 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1246 return;
1250 * lpfc_issue_ct_rsp - issue a ct response
1251 * @phba: Pointer to HBA context object.
1252 * @job: Pointer to the job object.
1253 * @tag: tag index value into the ports context exchange array.
1254 * @bmp: Pointer to a dma buffer descriptor.
1255 * @num_entry: Number of enties in the bde.
1257 static int
1258 lpfc_issue_ct_rsp(struct lpfc_hba *phba, struct fc_bsg_job *job, uint32_t tag,
1259 struct lpfc_dmabuf *bmp, int num_entry)
1261 IOCB_t *icmd;
1262 struct lpfc_iocbq *ctiocb = NULL;
1263 int rc = 0;
1264 struct lpfc_nodelist *ndlp = NULL;
1265 struct bsg_job_data *dd_data;
1266 uint32_t creg_val;
1268 /* allocate our bsg tracking structure */
1269 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
1270 if (!dd_data) {
1271 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1272 "2736 Failed allocation of dd_data\n");
1273 rc = -ENOMEM;
1274 goto no_dd_data;
1277 /* Allocate buffer for command iocb */
1278 ctiocb = lpfc_sli_get_iocbq(phba);
1279 if (!ctiocb) {
1280 rc = -ENOMEM;
1281 goto no_ctiocb;
1284 icmd = &ctiocb->iocb;
1285 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
1286 icmd->un.xseq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
1287 icmd->un.xseq64.bdl.addrLow = putPaddrLow(bmp->phys);
1288 icmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
1289 icmd->un.xseq64.bdl.bdeSize = (num_entry * sizeof(struct ulp_bde64));
1290 icmd->un.xseq64.w5.hcsw.Fctl = (LS | LA);
1291 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
1292 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_SOL_CTL;
1293 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
1295 /* Fill in rest of iocb */
1296 icmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;
1297 icmd->ulpBdeCount = 1;
1298 icmd->ulpLe = 1;
1299 icmd->ulpClass = CLASS3;
1300 if (phba->sli_rev == LPFC_SLI_REV4) {
1301 /* Do not issue unsol response if oxid not marked as valid */
1302 if (!(phba->ct_ctx[tag].flags & UNSOL_VALID)) {
1303 rc = IOCB_ERROR;
1304 goto issue_ct_rsp_exit;
1306 icmd->ulpContext = phba->ct_ctx[tag].oxid;
1307 ndlp = lpfc_findnode_did(phba->pport, phba->ct_ctx[tag].SID);
1308 if (!ndlp) {
1309 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
1310 "2721 ndlp null for oxid %x SID %x\n",
1311 icmd->ulpContext,
1312 phba->ct_ctx[tag].SID);
1313 rc = IOCB_ERROR;
1314 goto issue_ct_rsp_exit;
1317 /* Check if the ndlp is active */
1318 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
1319 rc = -IOCB_ERROR;
1320 goto issue_ct_rsp_exit;
1323 /* get a refernece count so the ndlp doesn't go away while
1324 * we respond
1326 if (!lpfc_nlp_get(ndlp)) {
1327 rc = -IOCB_ERROR;
1328 goto issue_ct_rsp_exit;
1331 icmd->un.ulpWord[3] = ndlp->nlp_rpi;
1332 /* The exchange is done, mark the entry as invalid */
1333 phba->ct_ctx[tag].flags &= ~UNSOL_VALID;
1334 } else
1335 icmd->ulpContext = (ushort) tag;
1337 icmd->ulpTimeout = phba->fc_ratov * 2;
1339 /* Xmit CT response on exchange <xid> */
1340 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
1341 "2722 Xmit CT response on exchange x%x Data: x%x x%x\n",
1342 icmd->ulpContext, icmd->ulpIoTag, phba->link_state);
1344 ctiocb->iocb_cmpl = NULL;
1345 ctiocb->iocb_flag |= LPFC_IO_LIBDFC;
1346 ctiocb->vport = phba->pport;
1347 ctiocb->context3 = bmp;
1349 ctiocb->iocb_cmpl = lpfc_issue_ct_rsp_cmp;
1350 ctiocb->context2 = dd_data;
1351 ctiocb->context1 = ndlp;
1352 dd_data->type = TYPE_IOCB;
1353 dd_data->context_un.iocb.cmdiocbq = ctiocb;
1354 dd_data->context_un.iocb.rspiocbq = NULL;
1355 dd_data->context_un.iocb.set_job = job;
1356 dd_data->context_un.iocb.bmp = bmp;
1357 dd_data->context_un.iocb.ndlp = ndlp;
1359 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
1360 creg_val = readl(phba->HCregaddr);
1361 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
1362 writel(creg_val, phba->HCregaddr);
1363 readl(phba->HCregaddr); /* flush */
1366 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
1368 if (rc == IOCB_SUCCESS)
1369 return 0; /* done for now */
1371 issue_ct_rsp_exit:
1372 lpfc_sli_release_iocbq(phba, ctiocb);
1373 no_ctiocb:
1374 kfree(dd_data);
1375 no_dd_data:
1376 return rc;
1380 * lpfc_bsg_send_mgmt_rsp - process a SEND_MGMT_RESP bsg vendor command
1381 * @job: SEND_MGMT_RESP fc_bsg_job
1383 static int
1384 lpfc_bsg_send_mgmt_rsp(struct fc_bsg_job *job)
1386 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
1387 struct lpfc_hba *phba = vport->phba;
1388 struct send_mgmt_resp *mgmt_resp = (struct send_mgmt_resp *)
1389 job->request->rqst_data.h_vendor.vendor_cmd;
1390 struct ulp_bde64 *bpl;
1391 struct lpfc_dmabuf *bmp = NULL;
1392 struct scatterlist *sgel = NULL;
1393 int request_nseg;
1394 int numbde;
1395 dma_addr_t busaddr;
1396 uint32_t tag = mgmt_resp->tag;
1397 unsigned long reqbfrcnt =
1398 (unsigned long)job->request_payload.payload_len;
1399 int rc = 0;
1401 /* in case no data is transferred */
1402 job->reply->reply_payload_rcv_len = 0;
1404 if (!reqbfrcnt || (reqbfrcnt > (80 * BUF_SZ_4K))) {
1405 rc = -ERANGE;
1406 goto send_mgmt_rsp_exit;
1409 bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
1410 if (!bmp) {
1411 rc = -ENOMEM;
1412 goto send_mgmt_rsp_exit;
1415 bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
1416 if (!bmp->virt) {
1417 rc = -ENOMEM;
1418 goto send_mgmt_rsp_free_bmp;
1421 INIT_LIST_HEAD(&bmp->list);
1422 bpl = (struct ulp_bde64 *) bmp->virt;
1423 request_nseg = pci_map_sg(phba->pcidev, job->request_payload.sg_list,
1424 job->request_payload.sg_cnt, DMA_TO_DEVICE);
1425 for_each_sg(job->request_payload.sg_list, sgel, request_nseg, numbde) {
1426 busaddr = sg_dma_address(sgel);
1427 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1428 bpl->tus.f.bdeSize = sg_dma_len(sgel);
1429 bpl->tus.w = cpu_to_le32(bpl->tus.w);
1430 bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
1431 bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
1432 bpl++;
1435 rc = lpfc_issue_ct_rsp(phba, job, tag, bmp, request_nseg);
1437 if (rc == IOCB_SUCCESS)
1438 return 0; /* done for now */
1440 /* TBD need to handle a timeout */
1441 pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
1442 job->request_payload.sg_cnt, DMA_TO_DEVICE);
1443 rc = -EACCES;
1444 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
1446 send_mgmt_rsp_free_bmp:
1447 kfree(bmp);
1448 send_mgmt_rsp_exit:
1449 /* make error code available to userspace */
1450 job->reply->result = rc;
1451 job->dd_data = NULL;
1452 return rc;
1456 * lpfc_bsg_diag_mode - process a LPFC_BSG_VENDOR_DIAG_MODE bsg vendor command
1457 * @job: LPFC_BSG_VENDOR_DIAG_MODE
1459 * This function is responsible for placing a port into diagnostic loopback
1460 * mode in order to perform a diagnostic loopback test.
1461 * All new scsi requests are blocked, a small delay is used to allow the
1462 * scsi requests to complete then the link is brought down. If the link is
1463 * is placed in loopback mode then scsi requests are again allowed
1464 * so the scsi mid-layer doesn't give up on the port.
1465 * All of this is done in-line.
1467 static int
1468 lpfc_bsg_diag_mode(struct fc_bsg_job *job)
1470 struct Scsi_Host *shost = job->shost;
1471 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
1472 struct lpfc_hba *phba = vport->phba;
1473 struct diag_mode_set *loopback_mode;
1474 struct lpfc_sli *psli = &phba->sli;
1475 struct lpfc_sli_ring *pring = &psli->ring[LPFC_FCP_RING];
1476 uint32_t link_flags;
1477 uint32_t timeout;
1478 struct lpfc_vport **vports;
1479 LPFC_MBOXQ_t *pmboxq;
1480 int mbxstatus;
1481 int i = 0;
1482 int rc = 0;
1484 /* no data to return just the return code */
1485 job->reply->reply_payload_rcv_len = 0;
1487 if (job->request_len <
1488 sizeof(struct fc_bsg_request) + sizeof(struct diag_mode_set)) {
1489 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1490 "2738 Received DIAG MODE request below minimum "
1491 "size\n");
1492 rc = -EINVAL;
1493 goto job_error;
1496 loopback_mode = (struct diag_mode_set *)
1497 job->request->rqst_data.h_vendor.vendor_cmd;
1498 link_flags = loopback_mode->type;
1499 timeout = loopback_mode->timeout * 100;
1501 if ((phba->link_state == LPFC_HBA_ERROR) ||
1502 (psli->sli_flag & LPFC_BLOCK_MGMT_IO) ||
1503 (!(psli->sli_flag & LPFC_SLI_ACTIVE))) {
1504 rc = -EACCES;
1505 goto job_error;
1508 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1509 if (!pmboxq) {
1510 rc = -ENOMEM;
1511 goto job_error;
1514 vports = lpfc_create_vport_work_array(phba);
1515 if (vports) {
1516 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
1517 shost = lpfc_shost_from_vport(vports[i]);
1518 scsi_block_requests(shost);
1521 lpfc_destroy_vport_work_array(phba, vports);
1522 } else {
1523 shost = lpfc_shost_from_vport(phba->pport);
1524 scsi_block_requests(shost);
1527 while (pring->txcmplq_cnt) {
1528 if (i++ > 500) /* wait up to 5 seconds */
1529 break;
1531 msleep(10);
1534 memset((void *)pmboxq, 0, sizeof(LPFC_MBOXQ_t));
1535 pmboxq->u.mb.mbxCommand = MBX_DOWN_LINK;
1536 pmboxq->u.mb.mbxOwner = OWN_HOST;
1538 mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
1540 if ((mbxstatus == MBX_SUCCESS) && (pmboxq->u.mb.mbxStatus == 0)) {
1541 /* wait for link down before proceeding */
1542 i = 0;
1543 while (phba->link_state != LPFC_LINK_DOWN) {
1544 if (i++ > timeout) {
1545 rc = -ETIMEDOUT;
1546 goto loopback_mode_exit;
1549 msleep(10);
1552 memset((void *)pmboxq, 0, sizeof(LPFC_MBOXQ_t));
1553 if (link_flags == INTERNAL_LOOP_BACK)
1554 pmboxq->u.mb.un.varInitLnk.link_flags = FLAGS_LOCAL_LB;
1555 else
1556 pmboxq->u.mb.un.varInitLnk.link_flags =
1557 FLAGS_TOPOLOGY_MODE_LOOP;
1559 pmboxq->u.mb.mbxCommand = MBX_INIT_LINK;
1560 pmboxq->u.mb.mbxOwner = OWN_HOST;
1562 mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq,
1563 LPFC_MBOX_TMO);
1565 if ((mbxstatus != MBX_SUCCESS) || (pmboxq->u.mb.mbxStatus))
1566 rc = -ENODEV;
1567 else {
1568 phba->link_flag |= LS_LOOPBACK_MODE;
1569 /* wait for the link attention interrupt */
1570 msleep(100);
1572 i = 0;
1573 while (phba->link_state != LPFC_HBA_READY) {
1574 if (i++ > timeout) {
1575 rc = -ETIMEDOUT;
1576 break;
1579 msleep(10);
1583 } else
1584 rc = -ENODEV;
1586 loopback_mode_exit:
1587 vports = lpfc_create_vport_work_array(phba);
1588 if (vports) {
1589 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
1590 shost = lpfc_shost_from_vport(vports[i]);
1591 scsi_unblock_requests(shost);
1593 lpfc_destroy_vport_work_array(phba, vports);
1594 } else {
1595 shost = lpfc_shost_from_vport(phba->pport);
1596 scsi_unblock_requests(shost);
1600 * Let SLI layer release mboxq if mbox command completed after timeout.
1602 if (mbxstatus != MBX_TIMEOUT)
1603 mempool_free(pmboxq, phba->mbox_mem_pool);
1605 job_error:
1606 /* make error code available to userspace */
1607 job->reply->result = rc;
1608 /* complete the job back to userspace if no error */
1609 if (rc == 0)
1610 job->job_done(job);
1611 return rc;
1615 * lpfcdiag_loop_self_reg - obtains a remote port login id
1616 * @phba: Pointer to HBA context object
1617 * @rpi: Pointer to a remote port login id
1619 * This function obtains a remote port login id so the diag loopback test
1620 * can send and receive its own unsolicited CT command.
1622 static int lpfcdiag_loop_self_reg(struct lpfc_hba *phba, uint16_t *rpi)
1624 LPFC_MBOXQ_t *mbox;
1625 struct lpfc_dmabuf *dmabuff;
1626 int status;
1628 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1629 if (!mbox)
1630 return -ENOMEM;
1632 if (phba->sli_rev == LPFC_SLI_REV4)
1633 *rpi = lpfc_sli4_alloc_rpi(phba);
1634 status = lpfc_reg_rpi(phba, 0, phba->pport->fc_myDID,
1635 (uint8_t *)&phba->pport->fc_sparam, mbox, *rpi);
1636 if (status) {
1637 mempool_free(mbox, phba->mbox_mem_pool);
1638 if (phba->sli_rev == LPFC_SLI_REV4)
1639 lpfc_sli4_free_rpi(phba, *rpi);
1640 return -ENOMEM;
1643 dmabuff = (struct lpfc_dmabuf *) mbox->context1;
1644 mbox->context1 = NULL;
1645 mbox->context2 = NULL;
1646 status = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
1648 if ((status != MBX_SUCCESS) || (mbox->u.mb.mbxStatus)) {
1649 lpfc_mbuf_free(phba, dmabuff->virt, dmabuff->phys);
1650 kfree(dmabuff);
1651 if (status != MBX_TIMEOUT)
1652 mempool_free(mbox, phba->mbox_mem_pool);
1653 if (phba->sli_rev == LPFC_SLI_REV4)
1654 lpfc_sli4_free_rpi(phba, *rpi);
1655 return -ENODEV;
1658 *rpi = mbox->u.mb.un.varWords[0];
1660 lpfc_mbuf_free(phba, dmabuff->virt, dmabuff->phys);
1661 kfree(dmabuff);
1662 mempool_free(mbox, phba->mbox_mem_pool);
1663 return 0;
1667 * lpfcdiag_loop_self_unreg - unregs from the rpi
1668 * @phba: Pointer to HBA context object
1669 * @rpi: Remote port login id
1671 * This function unregisters the rpi obtained in lpfcdiag_loop_self_reg
1673 static int lpfcdiag_loop_self_unreg(struct lpfc_hba *phba, uint16_t rpi)
1675 LPFC_MBOXQ_t *mbox;
1676 int status;
1678 /* Allocate mboxq structure */
1679 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1680 if (mbox == NULL)
1681 return -ENOMEM;
1683 lpfc_unreg_login(phba, 0, rpi, mbox);
1684 status = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
1686 if ((status != MBX_SUCCESS) || (mbox->u.mb.mbxStatus)) {
1687 if (status != MBX_TIMEOUT)
1688 mempool_free(mbox, phba->mbox_mem_pool);
1689 return -EIO;
1691 mempool_free(mbox, phba->mbox_mem_pool);
1692 if (phba->sli_rev == LPFC_SLI_REV4)
1693 lpfc_sli4_free_rpi(phba, rpi);
1694 return 0;
1698 * lpfcdiag_loop_get_xri - obtains the transmit and receive ids
1699 * @phba: Pointer to HBA context object
1700 * @rpi: Remote port login id
1701 * @txxri: Pointer to transmit exchange id
1702 * @rxxri: Pointer to response exchabge id
1704 * This function obtains the transmit and receive ids required to send
1705 * an unsolicited ct command with a payload. A special lpfc FsType and CmdRsp
1706 * flags are used to the unsolicted response handler is able to process
1707 * the ct command sent on the same port.
1709 static int lpfcdiag_loop_get_xri(struct lpfc_hba *phba, uint16_t rpi,
1710 uint16_t *txxri, uint16_t * rxxri)
1712 struct lpfc_bsg_event *evt;
1713 struct lpfc_iocbq *cmdiocbq, *rspiocbq;
1714 IOCB_t *cmd, *rsp;
1715 struct lpfc_dmabuf *dmabuf;
1716 struct ulp_bde64 *bpl = NULL;
1717 struct lpfc_sli_ct_request *ctreq = NULL;
1718 int ret_val = 0;
1719 int time_left;
1720 int iocb_stat = 0;
1721 unsigned long flags;
1723 *txxri = 0;
1724 *rxxri = 0;
1725 evt = lpfc_bsg_event_new(FC_REG_CT_EVENT, current->pid,
1726 SLI_CT_ELX_LOOPBACK);
1727 if (!evt)
1728 return -ENOMEM;
1730 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1731 list_add(&evt->node, &phba->ct_ev_waiters);
1732 lpfc_bsg_event_ref(evt);
1733 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1735 cmdiocbq = lpfc_sli_get_iocbq(phba);
1736 rspiocbq = lpfc_sli_get_iocbq(phba);
1738 dmabuf = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
1739 if (dmabuf) {
1740 dmabuf->virt = lpfc_mbuf_alloc(phba, 0, &dmabuf->phys);
1741 if (dmabuf->virt) {
1742 INIT_LIST_HEAD(&dmabuf->list);
1743 bpl = (struct ulp_bde64 *) dmabuf->virt;
1744 memset(bpl, 0, sizeof(*bpl));
1745 ctreq = (struct lpfc_sli_ct_request *)(bpl + 1);
1746 bpl->addrHigh =
1747 le32_to_cpu(putPaddrHigh(dmabuf->phys +
1748 sizeof(*bpl)));
1749 bpl->addrLow =
1750 le32_to_cpu(putPaddrLow(dmabuf->phys +
1751 sizeof(*bpl)));
1752 bpl->tus.f.bdeFlags = 0;
1753 bpl->tus.f.bdeSize = ELX_LOOPBACK_HEADER_SZ;
1754 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1758 if (cmdiocbq == NULL || rspiocbq == NULL ||
1759 dmabuf == NULL || bpl == NULL || ctreq == NULL ||
1760 dmabuf->virt == NULL) {
1761 ret_val = -ENOMEM;
1762 goto err_get_xri_exit;
1765 cmd = &cmdiocbq->iocb;
1766 rsp = &rspiocbq->iocb;
1768 memset(ctreq, 0, ELX_LOOPBACK_HEADER_SZ);
1770 ctreq->RevisionId.bits.Revision = SLI_CT_REVISION;
1771 ctreq->RevisionId.bits.InId = 0;
1772 ctreq->FsType = SLI_CT_ELX_LOOPBACK;
1773 ctreq->FsSubType = 0;
1774 ctreq->CommandResponse.bits.CmdRsp = ELX_LOOPBACK_XRI_SETUP;
1775 ctreq->CommandResponse.bits.Size = 0;
1778 cmd->un.xseq64.bdl.addrHigh = putPaddrHigh(dmabuf->phys);
1779 cmd->un.xseq64.bdl.addrLow = putPaddrLow(dmabuf->phys);
1780 cmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
1781 cmd->un.xseq64.bdl.bdeSize = sizeof(*bpl);
1783 cmd->un.xseq64.w5.hcsw.Fctl = LA;
1784 cmd->un.xseq64.w5.hcsw.Dfctl = 0;
1785 cmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
1786 cmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
1788 cmd->ulpCommand = CMD_XMIT_SEQUENCE64_CR;
1789 cmd->ulpBdeCount = 1;
1790 cmd->ulpLe = 1;
1791 cmd->ulpClass = CLASS3;
1792 cmd->ulpContext = rpi;
1794 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
1795 cmdiocbq->vport = phba->pport;
1797 iocb_stat = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq,
1798 rspiocbq,
1799 (phba->fc_ratov * 2)
1800 + LPFC_DRVR_TIMEOUT);
1801 if (iocb_stat) {
1802 ret_val = -EIO;
1803 goto err_get_xri_exit;
1805 *txxri = rsp->ulpContext;
1807 evt->waiting = 1;
1808 evt->wait_time_stamp = jiffies;
1809 time_left = wait_event_interruptible_timeout(
1810 evt->wq, !list_empty(&evt->events_to_see),
1811 ((phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT) * HZ);
1812 if (list_empty(&evt->events_to_see))
1813 ret_val = (time_left) ? -EINTR : -ETIMEDOUT;
1814 else {
1815 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1816 list_move(evt->events_to_see.prev, &evt->events_to_get);
1817 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1818 *rxxri = (list_entry(evt->events_to_get.prev,
1819 typeof(struct event_data),
1820 node))->immed_dat;
1822 evt->waiting = 0;
1824 err_get_xri_exit:
1825 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1826 lpfc_bsg_event_unref(evt); /* release ref */
1827 lpfc_bsg_event_unref(evt); /* delete */
1828 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1830 if (dmabuf) {
1831 if (dmabuf->virt)
1832 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
1833 kfree(dmabuf);
1836 if (cmdiocbq && (iocb_stat != IOCB_TIMEDOUT))
1837 lpfc_sli_release_iocbq(phba, cmdiocbq);
1838 if (rspiocbq)
1839 lpfc_sli_release_iocbq(phba, rspiocbq);
1840 return ret_val;
1844 * diag_cmd_data_alloc - fills in a bde struct with dma buffers
1845 * @phba: Pointer to HBA context object
1846 * @bpl: Pointer to 64 bit bde structure
1847 * @size: Number of bytes to process
1848 * @nocopydata: Flag to copy user data into the allocated buffer
1850 * This function allocates page size buffers and populates an lpfc_dmabufext.
1851 * If allowed the user data pointed to with indataptr is copied into the kernel
1852 * memory. The chained list of page size buffers is returned.
1854 static struct lpfc_dmabufext *
1855 diag_cmd_data_alloc(struct lpfc_hba *phba,
1856 struct ulp_bde64 *bpl, uint32_t size,
1857 int nocopydata)
1859 struct lpfc_dmabufext *mlist = NULL;
1860 struct lpfc_dmabufext *dmp;
1861 int cnt, offset = 0, i = 0;
1862 struct pci_dev *pcidev;
1864 pcidev = phba->pcidev;
1866 while (size) {
1867 /* We get chunks of 4K */
1868 if (size > BUF_SZ_4K)
1869 cnt = BUF_SZ_4K;
1870 else
1871 cnt = size;
1873 /* allocate struct lpfc_dmabufext buffer header */
1874 dmp = kmalloc(sizeof(struct lpfc_dmabufext), GFP_KERNEL);
1875 if (!dmp)
1876 goto out;
1878 INIT_LIST_HEAD(&dmp->dma.list);
1880 /* Queue it to a linked list */
1881 if (mlist)
1882 list_add_tail(&dmp->dma.list, &mlist->dma.list);
1883 else
1884 mlist = dmp;
1886 /* allocate buffer */
1887 dmp->dma.virt = dma_alloc_coherent(&pcidev->dev,
1888 cnt,
1889 &(dmp->dma.phys),
1890 GFP_KERNEL);
1892 if (!dmp->dma.virt)
1893 goto out;
1895 dmp->size = cnt;
1897 if (nocopydata) {
1898 bpl->tus.f.bdeFlags = 0;
1899 pci_dma_sync_single_for_device(phba->pcidev,
1900 dmp->dma.phys, LPFC_BPL_SIZE, PCI_DMA_TODEVICE);
1902 } else {
1903 memset((uint8_t *)dmp->dma.virt, 0, cnt);
1904 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1907 /* build buffer ptr list for IOCB */
1908 bpl->addrLow = le32_to_cpu(putPaddrLow(dmp->dma.phys));
1909 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dmp->dma.phys));
1910 bpl->tus.f.bdeSize = (ushort) cnt;
1911 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1912 bpl++;
1914 i++;
1915 offset += cnt;
1916 size -= cnt;
1919 mlist->flag = i;
1920 return mlist;
1921 out:
1922 diag_cmd_data_free(phba, mlist);
1923 return NULL;
1927 * lpfcdiag_loop_post_rxbufs - post the receive buffers for an unsol CT cmd
1928 * @phba: Pointer to HBA context object
1929 * @rxxri: Receive exchange id
1930 * @len: Number of data bytes
1932 * This function allocates and posts a data buffer of sufficient size to recieve
1933 * an unsolicted CT command.
1935 static int lpfcdiag_loop_post_rxbufs(struct lpfc_hba *phba, uint16_t rxxri,
1936 size_t len)
1938 struct lpfc_sli *psli = &phba->sli;
1939 struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
1940 struct lpfc_iocbq *cmdiocbq;
1941 IOCB_t *cmd = NULL;
1942 struct list_head head, *curr, *next;
1943 struct lpfc_dmabuf *rxbmp;
1944 struct lpfc_dmabuf *dmp;
1945 struct lpfc_dmabuf *mp[2] = {NULL, NULL};
1946 struct ulp_bde64 *rxbpl = NULL;
1947 uint32_t num_bde;
1948 struct lpfc_dmabufext *rxbuffer = NULL;
1949 int ret_val = 0;
1950 int iocb_stat;
1951 int i = 0;
1953 cmdiocbq = lpfc_sli_get_iocbq(phba);
1954 rxbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
1955 if (rxbmp != NULL) {
1956 rxbmp->virt = lpfc_mbuf_alloc(phba, 0, &rxbmp->phys);
1957 if (rxbmp->virt) {
1958 INIT_LIST_HEAD(&rxbmp->list);
1959 rxbpl = (struct ulp_bde64 *) rxbmp->virt;
1960 rxbuffer = diag_cmd_data_alloc(phba, rxbpl, len, 0);
1964 if (!cmdiocbq || !rxbmp || !rxbpl || !rxbuffer) {
1965 ret_val = -ENOMEM;
1966 goto err_post_rxbufs_exit;
1969 /* Queue buffers for the receive exchange */
1970 num_bde = (uint32_t)rxbuffer->flag;
1971 dmp = &rxbuffer->dma;
1973 cmd = &cmdiocbq->iocb;
1974 i = 0;
1976 INIT_LIST_HEAD(&head);
1977 list_add_tail(&head, &dmp->list);
1978 list_for_each_safe(curr, next, &head) {
1979 mp[i] = list_entry(curr, struct lpfc_dmabuf, list);
1980 list_del(curr);
1982 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
1983 mp[i]->buffer_tag = lpfc_sli_get_buffer_tag(phba);
1984 cmd->un.quexri64cx.buff.bde.addrHigh =
1985 putPaddrHigh(mp[i]->phys);
1986 cmd->un.quexri64cx.buff.bde.addrLow =
1987 putPaddrLow(mp[i]->phys);
1988 cmd->un.quexri64cx.buff.bde.tus.f.bdeSize =
1989 ((struct lpfc_dmabufext *)mp[i])->size;
1990 cmd->un.quexri64cx.buff.buffer_tag = mp[i]->buffer_tag;
1991 cmd->ulpCommand = CMD_QUE_XRI64_CX;
1992 cmd->ulpPU = 0;
1993 cmd->ulpLe = 1;
1994 cmd->ulpBdeCount = 1;
1995 cmd->unsli3.que_xri64cx_ext_words.ebde_count = 0;
1997 } else {
1998 cmd->un.cont64[i].addrHigh = putPaddrHigh(mp[i]->phys);
1999 cmd->un.cont64[i].addrLow = putPaddrLow(mp[i]->phys);
2000 cmd->un.cont64[i].tus.f.bdeSize =
2001 ((struct lpfc_dmabufext *)mp[i])->size;
2002 cmd->ulpBdeCount = ++i;
2004 if ((--num_bde > 0) && (i < 2))
2005 continue;
2007 cmd->ulpCommand = CMD_QUE_XRI_BUF64_CX;
2008 cmd->ulpLe = 1;
2011 cmd->ulpClass = CLASS3;
2012 cmd->ulpContext = rxxri;
2014 iocb_stat = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq,
2016 if (iocb_stat == IOCB_ERROR) {
2017 diag_cmd_data_free(phba,
2018 (struct lpfc_dmabufext *)mp[0]);
2019 if (mp[1])
2020 diag_cmd_data_free(phba,
2021 (struct lpfc_dmabufext *)mp[1]);
2022 dmp = list_entry(next, struct lpfc_dmabuf, list);
2023 ret_val = -EIO;
2024 goto err_post_rxbufs_exit;
2027 lpfc_sli_ringpostbuf_put(phba, pring, mp[0]);
2028 if (mp[1]) {
2029 lpfc_sli_ringpostbuf_put(phba, pring, mp[1]);
2030 mp[1] = NULL;
2033 /* The iocb was freed by lpfc_sli_issue_iocb */
2034 cmdiocbq = lpfc_sli_get_iocbq(phba);
2035 if (!cmdiocbq) {
2036 dmp = list_entry(next, struct lpfc_dmabuf, list);
2037 ret_val = -EIO;
2038 goto err_post_rxbufs_exit;
2041 cmd = &cmdiocbq->iocb;
2042 i = 0;
2044 list_del(&head);
2046 err_post_rxbufs_exit:
2048 if (rxbmp) {
2049 if (rxbmp->virt)
2050 lpfc_mbuf_free(phba, rxbmp->virt, rxbmp->phys);
2051 kfree(rxbmp);
2054 if (cmdiocbq)
2055 lpfc_sli_release_iocbq(phba, cmdiocbq);
2056 return ret_val;
2060 * lpfc_bsg_diag_test - with a port in loopback issues a Ct cmd to itself
2061 * @job: LPFC_BSG_VENDOR_DIAG_TEST fc_bsg_job
2063 * This function receives a user data buffer to be transmitted and received on
2064 * the same port, the link must be up and in loopback mode prior
2065 * to being called.
2066 * 1. A kernel buffer is allocated to copy the user data into.
2067 * 2. The port registers with "itself".
2068 * 3. The transmit and receive exchange ids are obtained.
2069 * 4. The receive exchange id is posted.
2070 * 5. A new els loopback event is created.
2071 * 6. The command and response iocbs are allocated.
2072 * 7. The cmd iocb FsType is set to elx loopback and the CmdRsp to looppback.
2074 * This function is meant to be called n times while the port is in loopback
2075 * so it is the apps responsibility to issue a reset to take the port out
2076 * of loopback mode.
2078 static int
2079 lpfc_bsg_diag_test(struct fc_bsg_job *job)
2081 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
2082 struct lpfc_hba *phba = vport->phba;
2083 struct diag_mode_test *diag_mode;
2084 struct lpfc_bsg_event *evt;
2085 struct event_data *evdat;
2086 struct lpfc_sli *psli = &phba->sli;
2087 uint32_t size;
2088 uint32_t full_size;
2089 size_t segment_len = 0, segment_offset = 0, current_offset = 0;
2090 uint16_t rpi = 0;
2091 struct lpfc_iocbq *cmdiocbq, *rspiocbq;
2092 IOCB_t *cmd, *rsp;
2093 struct lpfc_sli_ct_request *ctreq;
2094 struct lpfc_dmabuf *txbmp;
2095 struct ulp_bde64 *txbpl = NULL;
2096 struct lpfc_dmabufext *txbuffer = NULL;
2097 struct list_head head;
2098 struct lpfc_dmabuf *curr;
2099 uint16_t txxri, rxxri;
2100 uint32_t num_bde;
2101 uint8_t *ptr = NULL, *rx_databuf = NULL;
2102 int rc = 0;
2103 int time_left;
2104 int iocb_stat;
2105 unsigned long flags;
2106 void *dataout = NULL;
2107 uint32_t total_mem;
2109 /* in case no data is returned return just the return code */
2110 job->reply->reply_payload_rcv_len = 0;
2112 if (job->request_len <
2113 sizeof(struct fc_bsg_request) + sizeof(struct diag_mode_test)) {
2114 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2115 "2739 Received DIAG TEST request below minimum "
2116 "size\n");
2117 rc = -EINVAL;
2118 goto loopback_test_exit;
2121 if (job->request_payload.payload_len !=
2122 job->reply_payload.payload_len) {
2123 rc = -EINVAL;
2124 goto loopback_test_exit;
2127 diag_mode = (struct diag_mode_test *)
2128 job->request->rqst_data.h_vendor.vendor_cmd;
2130 if ((phba->link_state == LPFC_HBA_ERROR) ||
2131 (psli->sli_flag & LPFC_BLOCK_MGMT_IO) ||
2132 (!(psli->sli_flag & LPFC_SLI_ACTIVE))) {
2133 rc = -EACCES;
2134 goto loopback_test_exit;
2137 if (!lpfc_is_link_up(phba) || !(phba->link_flag & LS_LOOPBACK_MODE)) {
2138 rc = -EACCES;
2139 goto loopback_test_exit;
2142 size = job->request_payload.payload_len;
2143 full_size = size + ELX_LOOPBACK_HEADER_SZ; /* plus the header */
2145 if ((size == 0) || (size > 80 * BUF_SZ_4K)) {
2146 rc = -ERANGE;
2147 goto loopback_test_exit;
2150 if (full_size >= BUF_SZ_4K) {
2152 * Allocate memory for ioctl data. If buffer is bigger than 64k,
2153 * then we allocate 64k and re-use that buffer over and over to
2154 * xfer the whole block. This is because Linux kernel has a
2155 * problem allocating more than 120k of kernel space memory. Saw
2156 * problem with GET_FCPTARGETMAPPING...
2158 if (size <= (64 * 1024))
2159 total_mem = full_size;
2160 else
2161 total_mem = 64 * 1024;
2162 } else
2163 /* Allocate memory for ioctl data */
2164 total_mem = BUF_SZ_4K;
2166 dataout = kmalloc(total_mem, GFP_KERNEL);
2167 if (dataout == NULL) {
2168 rc = -ENOMEM;
2169 goto loopback_test_exit;
2172 ptr = dataout;
2173 ptr += ELX_LOOPBACK_HEADER_SZ;
2174 sg_copy_to_buffer(job->request_payload.sg_list,
2175 job->request_payload.sg_cnt,
2176 ptr, size);
2177 rc = lpfcdiag_loop_self_reg(phba, &rpi);
2178 if (rc)
2179 goto loopback_test_exit;
2181 rc = lpfcdiag_loop_get_xri(phba, rpi, &txxri, &rxxri);
2182 if (rc) {
2183 lpfcdiag_loop_self_unreg(phba, rpi);
2184 goto loopback_test_exit;
2187 rc = lpfcdiag_loop_post_rxbufs(phba, rxxri, full_size);
2188 if (rc) {
2189 lpfcdiag_loop_self_unreg(phba, rpi);
2190 goto loopback_test_exit;
2193 evt = lpfc_bsg_event_new(FC_REG_CT_EVENT, current->pid,
2194 SLI_CT_ELX_LOOPBACK);
2195 if (!evt) {
2196 lpfcdiag_loop_self_unreg(phba, rpi);
2197 rc = -ENOMEM;
2198 goto loopback_test_exit;
2201 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2202 list_add(&evt->node, &phba->ct_ev_waiters);
2203 lpfc_bsg_event_ref(evt);
2204 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2206 cmdiocbq = lpfc_sli_get_iocbq(phba);
2207 rspiocbq = lpfc_sli_get_iocbq(phba);
2208 txbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2210 if (txbmp) {
2211 txbmp->virt = lpfc_mbuf_alloc(phba, 0, &txbmp->phys);
2212 if (txbmp->virt) {
2213 INIT_LIST_HEAD(&txbmp->list);
2214 txbpl = (struct ulp_bde64 *) txbmp->virt;
2215 txbuffer = diag_cmd_data_alloc(phba,
2216 txbpl, full_size, 0);
2220 if (!cmdiocbq || !rspiocbq || !txbmp || !txbpl || !txbuffer ||
2221 !txbmp->virt) {
2222 rc = -ENOMEM;
2223 goto err_loopback_test_exit;
2226 cmd = &cmdiocbq->iocb;
2227 rsp = &rspiocbq->iocb;
2229 INIT_LIST_HEAD(&head);
2230 list_add_tail(&head, &txbuffer->dma.list);
2231 list_for_each_entry(curr, &head, list) {
2232 segment_len = ((struct lpfc_dmabufext *)curr)->size;
2233 if (current_offset == 0) {
2234 ctreq = curr->virt;
2235 memset(ctreq, 0, ELX_LOOPBACK_HEADER_SZ);
2236 ctreq->RevisionId.bits.Revision = SLI_CT_REVISION;
2237 ctreq->RevisionId.bits.InId = 0;
2238 ctreq->FsType = SLI_CT_ELX_LOOPBACK;
2239 ctreq->FsSubType = 0;
2240 ctreq->CommandResponse.bits.CmdRsp = ELX_LOOPBACK_DATA;
2241 ctreq->CommandResponse.bits.Size = size;
2242 segment_offset = ELX_LOOPBACK_HEADER_SZ;
2243 } else
2244 segment_offset = 0;
2246 BUG_ON(segment_offset >= segment_len);
2247 memcpy(curr->virt + segment_offset,
2248 ptr + current_offset,
2249 segment_len - segment_offset);
2251 current_offset += segment_len - segment_offset;
2252 BUG_ON(current_offset > size);
2254 list_del(&head);
2256 /* Build the XMIT_SEQUENCE iocb */
2258 num_bde = (uint32_t)txbuffer->flag;
2260 cmd->un.xseq64.bdl.addrHigh = putPaddrHigh(txbmp->phys);
2261 cmd->un.xseq64.bdl.addrLow = putPaddrLow(txbmp->phys);
2262 cmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
2263 cmd->un.xseq64.bdl.bdeSize = (num_bde * sizeof(struct ulp_bde64));
2265 cmd->un.xseq64.w5.hcsw.Fctl = (LS | LA);
2266 cmd->un.xseq64.w5.hcsw.Dfctl = 0;
2267 cmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
2268 cmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
2270 cmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;
2271 cmd->ulpBdeCount = 1;
2272 cmd->ulpLe = 1;
2273 cmd->ulpClass = CLASS3;
2274 cmd->ulpContext = txxri;
2276 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
2277 cmdiocbq->vport = phba->pport;
2279 iocb_stat = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq,
2280 rspiocbq, (phba->fc_ratov * 2) +
2281 LPFC_DRVR_TIMEOUT);
2283 if ((iocb_stat != IOCB_SUCCESS) || (rsp->ulpStatus != IOCB_SUCCESS)) {
2284 rc = -EIO;
2285 goto err_loopback_test_exit;
2288 evt->waiting = 1;
2289 time_left = wait_event_interruptible_timeout(
2290 evt->wq, !list_empty(&evt->events_to_see),
2291 ((phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT) * HZ);
2292 evt->waiting = 0;
2293 if (list_empty(&evt->events_to_see))
2294 rc = (time_left) ? -EINTR : -ETIMEDOUT;
2295 else {
2296 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2297 list_move(evt->events_to_see.prev, &evt->events_to_get);
2298 evdat = list_entry(evt->events_to_get.prev,
2299 typeof(*evdat), node);
2300 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2301 rx_databuf = evdat->data;
2302 if (evdat->len != full_size) {
2303 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
2304 "1603 Loopback test did not receive expected "
2305 "data length. actual length 0x%x expected "
2306 "length 0x%x\n",
2307 evdat->len, full_size);
2308 rc = -EIO;
2309 } else if (rx_databuf == NULL)
2310 rc = -EIO;
2311 else {
2312 rc = IOCB_SUCCESS;
2313 /* skip over elx loopback header */
2314 rx_databuf += ELX_LOOPBACK_HEADER_SZ;
2315 job->reply->reply_payload_rcv_len =
2316 sg_copy_from_buffer(job->reply_payload.sg_list,
2317 job->reply_payload.sg_cnt,
2318 rx_databuf, size);
2319 job->reply->reply_payload_rcv_len = size;
2323 err_loopback_test_exit:
2324 lpfcdiag_loop_self_unreg(phba, rpi);
2326 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2327 lpfc_bsg_event_unref(evt); /* release ref */
2328 lpfc_bsg_event_unref(evt); /* delete */
2329 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2331 if (cmdiocbq != NULL)
2332 lpfc_sli_release_iocbq(phba, cmdiocbq);
2334 if (rspiocbq != NULL)
2335 lpfc_sli_release_iocbq(phba, rspiocbq);
2337 if (txbmp != NULL) {
2338 if (txbpl != NULL) {
2339 if (txbuffer != NULL)
2340 diag_cmd_data_free(phba, txbuffer);
2341 lpfc_mbuf_free(phba, txbmp->virt, txbmp->phys);
2343 kfree(txbmp);
2346 loopback_test_exit:
2347 kfree(dataout);
2348 /* make error code available to userspace */
2349 job->reply->result = rc;
2350 job->dd_data = NULL;
2351 /* complete the job back to userspace if no error */
2352 if (rc == 0)
2353 job->job_done(job);
2354 return rc;
2358 * lpfc_bsg_get_dfc_rev - process a GET_DFC_REV bsg vendor command
2359 * @job: GET_DFC_REV fc_bsg_job
2361 static int
2362 lpfc_bsg_get_dfc_rev(struct fc_bsg_job *job)
2364 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
2365 struct lpfc_hba *phba = vport->phba;
2366 struct get_mgmt_rev *event_req;
2367 struct get_mgmt_rev_reply *event_reply;
2368 int rc = 0;
2370 if (job->request_len <
2371 sizeof(struct fc_bsg_request) + sizeof(struct get_mgmt_rev)) {
2372 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2373 "2740 Received GET_DFC_REV request below "
2374 "minimum size\n");
2375 rc = -EINVAL;
2376 goto job_error;
2379 event_req = (struct get_mgmt_rev *)
2380 job->request->rqst_data.h_vendor.vendor_cmd;
2382 event_reply = (struct get_mgmt_rev_reply *)
2383 job->reply->reply_data.vendor_reply.vendor_rsp;
2385 if (job->reply_len <
2386 sizeof(struct fc_bsg_request) + sizeof(struct get_mgmt_rev_reply)) {
2387 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2388 "2741 Received GET_DFC_REV reply below "
2389 "minimum size\n");
2390 rc = -EINVAL;
2391 goto job_error;
2394 event_reply->info.a_Major = MANAGEMENT_MAJOR_REV;
2395 event_reply->info.a_Minor = MANAGEMENT_MINOR_REV;
2396 job_error:
2397 job->reply->result = rc;
2398 if (rc == 0)
2399 job->job_done(job);
2400 return rc;
2404 * lpfc_bsg_wake_mbox_wait - lpfc_bsg_issue_mbox mbox completion handler
2405 * @phba: Pointer to HBA context object.
2406 * @pmboxq: Pointer to mailbox command.
2408 * This is completion handler function for mailbox commands issued from
2409 * lpfc_bsg_issue_mbox function. This function is called by the
2410 * mailbox event handler function with no lock held. This function
2411 * will wake up thread waiting on the wait queue pointed by context1
2412 * of the mailbox.
2414 void
2415 lpfc_bsg_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
2417 struct bsg_job_data *dd_data;
2418 struct fc_bsg_job *job;
2419 uint32_t size;
2420 unsigned long flags;
2421 uint8_t *to;
2422 uint8_t *from;
2424 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2425 dd_data = pmboxq->context1;
2426 /* job already timed out? */
2427 if (!dd_data) {
2428 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2429 return;
2432 /* build the outgoing buffer to do an sg copy
2433 * the format is the response mailbox followed by any extended
2434 * mailbox data
2436 from = (uint8_t *)&pmboxq->u.mb;
2437 to = (uint8_t *)dd_data->context_un.mbox.mb;
2438 memcpy(to, from, sizeof(MAILBOX_t));
2439 if (pmboxq->u.mb.mbxStatus == MBX_SUCCESS) {
2440 /* copy the extended data if any, count is in words */
2441 if (dd_data->context_un.mbox.outExtWLen) {
2442 from = (uint8_t *)dd_data->context_un.mbox.ext;
2443 to += sizeof(MAILBOX_t);
2444 size = dd_data->context_un.mbox.outExtWLen *
2445 sizeof(uint32_t);
2446 memcpy(to, from, size);
2447 } else if (pmboxq->u.mb.mbxCommand == MBX_RUN_BIU_DIAG64) {
2448 from = (uint8_t *)dd_data->context_un.mbox.
2449 dmp->dma.virt;
2450 to += sizeof(MAILBOX_t);
2451 size = dd_data->context_un.mbox.dmp->size;
2452 memcpy(to, from, size);
2453 } else if ((phba->sli_rev == LPFC_SLI_REV4) &&
2454 (pmboxq->u.mb.mbxCommand == MBX_DUMP_MEMORY)) {
2455 from = (uint8_t *)dd_data->context_un.mbox.dmp->dma.
2456 virt;
2457 to += sizeof(MAILBOX_t);
2458 size = pmboxq->u.mb.un.varWords[5];
2459 memcpy(to, from, size);
2460 } else if ((phba->sli_rev == LPFC_SLI_REV4) &&
2461 (pmboxq->u.mb.mbxCommand == MBX_SLI4_CONFIG)) {
2462 struct lpfc_mbx_nembed_cmd *nembed_sge =
2463 (struct lpfc_mbx_nembed_cmd *)
2464 &pmboxq->u.mb.un.varWords[0];
2466 from = (uint8_t *)dd_data->context_un.mbox.dmp->dma.
2467 virt;
2468 to += sizeof(MAILBOX_t);
2469 size = nembed_sge->sge[0].length;
2470 memcpy(to, from, size);
2471 } else if (pmboxq->u.mb.mbxCommand == MBX_READ_EVENT_LOG) {
2472 from = (uint8_t *)dd_data->context_un.
2473 mbox.dmp->dma.virt;
2474 to += sizeof(MAILBOX_t);
2475 size = dd_data->context_un.mbox.dmp->size;
2476 memcpy(to, from, size);
2480 from = (uint8_t *)dd_data->context_un.mbox.mb;
2481 job = dd_data->context_un.mbox.set_job;
2482 if (job) {
2483 size = job->reply_payload.payload_len;
2484 job->reply->reply_payload_rcv_len =
2485 sg_copy_from_buffer(job->reply_payload.sg_list,
2486 job->reply_payload.sg_cnt,
2487 from, size);
2488 job->reply->result = 0;
2490 job->dd_data = NULL;
2491 job->job_done(job);
2493 dd_data->context_un.mbox.set_job = NULL;
2494 /* need to hold the lock until we call job done to hold off
2495 * the timeout handler returning to the midlayer while
2496 * we are stillprocessing the job
2498 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2500 kfree(dd_data->context_un.mbox.mb);
2501 mempool_free(dd_data->context_un.mbox.pmboxq, phba->mbox_mem_pool);
2502 kfree(dd_data->context_un.mbox.ext);
2503 if (dd_data->context_un.mbox.dmp) {
2504 dma_free_coherent(&phba->pcidev->dev,
2505 dd_data->context_un.mbox.dmp->size,
2506 dd_data->context_un.mbox.dmp->dma.virt,
2507 dd_data->context_un.mbox.dmp->dma.phys);
2508 kfree(dd_data->context_un.mbox.dmp);
2510 if (dd_data->context_un.mbox.rxbmp) {
2511 lpfc_mbuf_free(phba, dd_data->context_un.mbox.rxbmp->virt,
2512 dd_data->context_un.mbox.rxbmp->phys);
2513 kfree(dd_data->context_un.mbox.rxbmp);
2515 kfree(dd_data);
2516 return;
2520 * lpfc_bsg_check_cmd_access - test for a supported mailbox command
2521 * @phba: Pointer to HBA context object.
2522 * @mb: Pointer to a mailbox object.
2523 * @vport: Pointer to a vport object.
2525 * Some commands require the port to be offline, some may not be called from
2526 * the application.
2528 static int lpfc_bsg_check_cmd_access(struct lpfc_hba *phba,
2529 MAILBOX_t *mb, struct lpfc_vport *vport)
2531 /* return negative error values for bsg job */
2532 switch (mb->mbxCommand) {
2533 /* Offline only */
2534 case MBX_INIT_LINK:
2535 case MBX_DOWN_LINK:
2536 case MBX_CONFIG_LINK:
2537 case MBX_CONFIG_RING:
2538 case MBX_RESET_RING:
2539 case MBX_UNREG_LOGIN:
2540 case MBX_CLEAR_LA:
2541 case MBX_DUMP_CONTEXT:
2542 case MBX_RUN_DIAGS:
2543 case MBX_RESTART:
2544 case MBX_SET_MASK:
2545 if (!(vport->fc_flag & FC_OFFLINE_MODE)) {
2546 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2547 "2743 Command 0x%x is illegal in on-line "
2548 "state\n",
2549 mb->mbxCommand);
2550 return -EPERM;
2552 case MBX_WRITE_NV:
2553 case MBX_WRITE_VPARMS:
2554 case MBX_LOAD_SM:
2555 case MBX_READ_NV:
2556 case MBX_READ_CONFIG:
2557 case MBX_READ_RCONFIG:
2558 case MBX_READ_STATUS:
2559 case MBX_READ_XRI:
2560 case MBX_READ_REV:
2561 case MBX_READ_LNK_STAT:
2562 case MBX_DUMP_MEMORY:
2563 case MBX_DOWN_LOAD:
2564 case MBX_UPDATE_CFG:
2565 case MBX_KILL_BOARD:
2566 case MBX_LOAD_AREA:
2567 case MBX_LOAD_EXP_ROM:
2568 case MBX_BEACON:
2569 case MBX_DEL_LD_ENTRY:
2570 case MBX_SET_DEBUG:
2571 case MBX_WRITE_WWN:
2572 case MBX_SLI4_CONFIG:
2573 case MBX_READ_EVENT_LOG:
2574 case MBX_READ_EVENT_LOG_STATUS:
2575 case MBX_WRITE_EVENT_LOG:
2576 case MBX_PORT_CAPABILITIES:
2577 case MBX_PORT_IOV_CONTROL:
2578 case MBX_RUN_BIU_DIAG64:
2579 break;
2580 case MBX_SET_VARIABLE:
2581 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2582 "1226 mbox: set_variable 0x%x, 0x%x\n",
2583 mb->un.varWords[0],
2584 mb->un.varWords[1]);
2585 if ((mb->un.varWords[0] == SETVAR_MLOMNT)
2586 && (mb->un.varWords[1] == 1)) {
2587 phba->wait_4_mlo_maint_flg = 1;
2588 } else if (mb->un.varWords[0] == SETVAR_MLORST) {
2589 phba->link_flag &= ~LS_LOOPBACK_MODE;
2590 phba->fc_topology = LPFC_TOPOLOGY_PT_PT;
2592 break;
2593 case MBX_READ_SPARM64:
2594 case MBX_READ_TOPOLOGY:
2595 case MBX_REG_LOGIN:
2596 case MBX_REG_LOGIN64:
2597 case MBX_CONFIG_PORT:
2598 case MBX_RUN_BIU_DIAG:
2599 default:
2600 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2601 "2742 Unknown Command 0x%x\n",
2602 mb->mbxCommand);
2603 return -EPERM;
2606 return 0; /* ok */
2610 * lpfc_bsg_issue_mbox - issues a mailbox command on behalf of an app
2611 * @phba: Pointer to HBA context object.
2612 * @mb: Pointer to a mailbox object.
2613 * @vport: Pointer to a vport object.
2615 * Allocate a tracking object, mailbox command memory, get a mailbox
2616 * from the mailbox pool, copy the caller mailbox command.
2618 * If offline and the sli is active we need to poll for the command (port is
2619 * being reset) and com-plete the job, otherwise issue the mailbox command and
2620 * let our completion handler finish the command.
2622 static uint32_t
2623 lpfc_bsg_issue_mbox(struct lpfc_hba *phba, struct fc_bsg_job *job,
2624 struct lpfc_vport *vport)
2626 LPFC_MBOXQ_t *pmboxq = NULL; /* internal mailbox queue */
2627 MAILBOX_t *pmb; /* shortcut to the pmboxq mailbox */
2628 /* a 4k buffer to hold the mb and extended data from/to the bsg */
2629 MAILBOX_t *mb = NULL;
2630 struct bsg_job_data *dd_data = NULL; /* bsg data tracking structure */
2631 uint32_t size;
2632 struct lpfc_dmabuf *rxbmp = NULL; /* for biu diag */
2633 struct lpfc_dmabufext *dmp = NULL; /* for biu diag */
2634 struct ulp_bde64 *rxbpl = NULL;
2635 struct dfc_mbox_req *mbox_req = (struct dfc_mbox_req *)
2636 job->request->rqst_data.h_vendor.vendor_cmd;
2637 uint8_t *ext = NULL;
2638 int rc = 0;
2639 uint8_t *from;
2641 /* in case no data is transferred */
2642 job->reply->reply_payload_rcv_len = 0;
2644 /* check if requested extended data lengths are valid */
2645 if ((mbox_req->inExtWLen > MAILBOX_EXT_SIZE) ||
2646 (mbox_req->outExtWLen > MAILBOX_EXT_SIZE)) {
2647 rc = -ERANGE;
2648 goto job_done;
2651 /* allocate our bsg tracking structure */
2652 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
2653 if (!dd_data) {
2654 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2655 "2727 Failed allocation of dd_data\n");
2656 rc = -ENOMEM;
2657 goto job_done;
2660 mb = kzalloc(BSG_MBOX_SIZE, GFP_KERNEL);
2661 if (!mb) {
2662 rc = -ENOMEM;
2663 goto job_done;
2666 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2667 if (!pmboxq) {
2668 rc = -ENOMEM;
2669 goto job_done;
2671 memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
2673 size = job->request_payload.payload_len;
2674 sg_copy_to_buffer(job->request_payload.sg_list,
2675 job->request_payload.sg_cnt,
2676 mb, size);
2678 rc = lpfc_bsg_check_cmd_access(phba, mb, vport);
2679 if (rc != 0)
2680 goto job_done; /* must be negative */
2682 pmb = &pmboxq->u.mb;
2683 memcpy(pmb, mb, sizeof(*pmb));
2684 pmb->mbxOwner = OWN_HOST;
2685 pmboxq->vport = vport;
2687 /* If HBA encountered an error attention, allow only DUMP
2688 * or RESTART mailbox commands until the HBA is restarted.
2690 if (phba->pport->stopped &&
2691 pmb->mbxCommand != MBX_DUMP_MEMORY &&
2692 pmb->mbxCommand != MBX_RESTART &&
2693 pmb->mbxCommand != MBX_WRITE_VPARMS &&
2694 pmb->mbxCommand != MBX_WRITE_WWN)
2695 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
2696 "2797 mbox: Issued mailbox cmd "
2697 "0x%x while in stopped state.\n",
2698 pmb->mbxCommand);
2700 /* Don't allow mailbox commands to be sent when blocked
2701 * or when in the middle of discovery
2703 if (phba->sli.sli_flag & LPFC_BLOCK_MGMT_IO) {
2704 rc = -EAGAIN;
2705 goto job_done;
2708 /* extended mailbox commands will need an extended buffer */
2709 if (mbox_req->inExtWLen || mbox_req->outExtWLen) {
2710 ext = kzalloc(MAILBOX_EXT_SIZE, GFP_KERNEL);
2711 if (!ext) {
2712 rc = -ENOMEM;
2713 goto job_done;
2716 /* any data for the device? */
2717 if (mbox_req->inExtWLen) {
2718 from = (uint8_t *)mb;
2719 from += sizeof(MAILBOX_t);
2720 memcpy((uint8_t *)ext, from,
2721 mbox_req->inExtWLen * sizeof(uint32_t));
2724 pmboxq->context2 = ext;
2725 pmboxq->in_ext_byte_len =
2726 mbox_req->inExtWLen * sizeof(uint32_t);
2727 pmboxq->out_ext_byte_len =
2728 mbox_req->outExtWLen * sizeof(uint32_t);
2729 pmboxq->mbox_offset_word = mbox_req->mbOffset;
2732 /* biu diag will need a kernel buffer to transfer the data
2733 * allocate our own buffer and setup the mailbox command to
2734 * use ours
2736 if (pmb->mbxCommand == MBX_RUN_BIU_DIAG64) {
2737 uint32_t transmit_length = pmb->un.varWords[1];
2738 uint32_t receive_length = pmb->un.varWords[4];
2739 /* transmit length cannot be greater than receive length or
2740 * mailbox extension size
2742 if ((transmit_length > receive_length) ||
2743 (transmit_length > MAILBOX_EXT_SIZE)) {
2744 rc = -ERANGE;
2745 goto job_done;
2748 rxbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2749 if (!rxbmp) {
2750 rc = -ENOMEM;
2751 goto job_done;
2754 rxbmp->virt = lpfc_mbuf_alloc(phba, 0, &rxbmp->phys);
2755 if (!rxbmp->virt) {
2756 rc = -ENOMEM;
2757 goto job_done;
2760 INIT_LIST_HEAD(&rxbmp->list);
2761 rxbpl = (struct ulp_bde64 *) rxbmp->virt;
2762 dmp = diag_cmd_data_alloc(phba, rxbpl, transmit_length, 0);
2763 if (!dmp) {
2764 rc = -ENOMEM;
2765 goto job_done;
2768 INIT_LIST_HEAD(&dmp->dma.list);
2769 pmb->un.varBIUdiag.un.s2.xmit_bde64.addrHigh =
2770 putPaddrHigh(dmp->dma.phys);
2771 pmb->un.varBIUdiag.un.s2.xmit_bde64.addrLow =
2772 putPaddrLow(dmp->dma.phys);
2774 pmb->un.varBIUdiag.un.s2.rcv_bde64.addrHigh =
2775 putPaddrHigh(dmp->dma.phys +
2776 pmb->un.varBIUdiag.un.s2.
2777 xmit_bde64.tus.f.bdeSize);
2778 pmb->un.varBIUdiag.un.s2.rcv_bde64.addrLow =
2779 putPaddrLow(dmp->dma.phys +
2780 pmb->un.varBIUdiag.un.s2.
2781 xmit_bde64.tus.f.bdeSize);
2783 /* copy the transmit data found in the mailbox extension area */
2784 from = (uint8_t *)mb;
2785 from += sizeof(MAILBOX_t);
2786 memcpy((uint8_t *)dmp->dma.virt, from, transmit_length);
2787 } else if (pmb->mbxCommand == MBX_READ_EVENT_LOG) {
2788 struct READ_EVENT_LOG_VAR *rdEventLog =
2789 &pmb->un.varRdEventLog ;
2790 uint32_t receive_length = rdEventLog->rcv_bde64.tus.f.bdeSize;
2791 uint32_t mode = bf_get(lpfc_event_log, rdEventLog);
2793 /* receive length cannot be greater than mailbox
2794 * extension size
2796 if (receive_length > MAILBOX_EXT_SIZE) {
2797 rc = -ERANGE;
2798 goto job_done;
2801 /* mode zero uses a bde like biu diags command */
2802 if (mode == 0) {
2804 /* rebuild the command for sli4 using our own buffers
2805 * like we do for biu diags
2808 rxbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2809 if (!rxbmp) {
2810 rc = -ENOMEM;
2811 goto job_done;
2814 rxbmp->virt = lpfc_mbuf_alloc(phba, 0, &rxbmp->phys);
2815 rxbpl = (struct ulp_bde64 *) rxbmp->virt;
2816 if (rxbpl) {
2817 INIT_LIST_HEAD(&rxbmp->list);
2818 dmp = diag_cmd_data_alloc(phba, rxbpl,
2819 receive_length, 0);
2822 if (!dmp) {
2823 rc = -ENOMEM;
2824 goto job_done;
2827 INIT_LIST_HEAD(&dmp->dma.list);
2828 pmb->un.varWords[3] = putPaddrLow(dmp->dma.phys);
2829 pmb->un.varWords[4] = putPaddrHigh(dmp->dma.phys);
2831 } else if (phba->sli_rev == LPFC_SLI_REV4) {
2832 if (pmb->mbxCommand == MBX_DUMP_MEMORY) {
2833 /* rebuild the command for sli4 using our own buffers
2834 * like we do for biu diags
2836 uint32_t receive_length = pmb->un.varWords[2];
2837 /* receive length cannot be greater than mailbox
2838 * extension size
2840 if ((receive_length == 0) ||
2841 (receive_length > MAILBOX_EXT_SIZE)) {
2842 rc = -ERANGE;
2843 goto job_done;
2846 rxbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2847 if (!rxbmp) {
2848 rc = -ENOMEM;
2849 goto job_done;
2852 rxbmp->virt = lpfc_mbuf_alloc(phba, 0, &rxbmp->phys);
2853 if (!rxbmp->virt) {
2854 rc = -ENOMEM;
2855 goto job_done;
2858 INIT_LIST_HEAD(&rxbmp->list);
2859 rxbpl = (struct ulp_bde64 *) rxbmp->virt;
2860 dmp = diag_cmd_data_alloc(phba, rxbpl, receive_length,
2862 if (!dmp) {
2863 rc = -ENOMEM;
2864 goto job_done;
2867 INIT_LIST_HEAD(&dmp->dma.list);
2868 pmb->un.varWords[3] = putPaddrLow(dmp->dma.phys);
2869 pmb->un.varWords[4] = putPaddrHigh(dmp->dma.phys);
2870 } else if ((pmb->mbxCommand == MBX_UPDATE_CFG) &&
2871 pmb->un.varUpdateCfg.co) {
2872 struct ulp_bde64 *bde =
2873 (struct ulp_bde64 *)&pmb->un.varWords[4];
2875 /* bde size cannot be greater than mailbox ext size */
2876 if (bde->tus.f.bdeSize > MAILBOX_EXT_SIZE) {
2877 rc = -ERANGE;
2878 goto job_done;
2881 rxbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2882 if (!rxbmp) {
2883 rc = -ENOMEM;
2884 goto job_done;
2887 rxbmp->virt = lpfc_mbuf_alloc(phba, 0, &rxbmp->phys);
2888 if (!rxbmp->virt) {
2889 rc = -ENOMEM;
2890 goto job_done;
2893 INIT_LIST_HEAD(&rxbmp->list);
2894 rxbpl = (struct ulp_bde64 *) rxbmp->virt;
2895 dmp = diag_cmd_data_alloc(phba, rxbpl,
2896 bde->tus.f.bdeSize, 0);
2897 if (!dmp) {
2898 rc = -ENOMEM;
2899 goto job_done;
2902 INIT_LIST_HEAD(&dmp->dma.list);
2903 bde->addrHigh = putPaddrHigh(dmp->dma.phys);
2904 bde->addrLow = putPaddrLow(dmp->dma.phys);
2906 /* copy the transmit data found in the mailbox
2907 * extension area
2909 from = (uint8_t *)mb;
2910 from += sizeof(MAILBOX_t);
2911 memcpy((uint8_t *)dmp->dma.virt, from,
2912 bde->tus.f.bdeSize);
2913 } else if (pmb->mbxCommand == MBX_SLI4_CONFIG) {
2914 struct lpfc_mbx_nembed_cmd *nembed_sge;
2915 struct mbox_header *header;
2916 uint32_t receive_length;
2918 /* rebuild the command for sli4 using our own buffers
2919 * like we do for biu diags
2921 header = (struct mbox_header *)&pmb->un.varWords[0];
2922 nembed_sge = (struct lpfc_mbx_nembed_cmd *)
2923 &pmb->un.varWords[0];
2924 receive_length = nembed_sge->sge[0].length;
2926 /* receive length cannot be greater than mailbox
2927 * extension size
2929 if ((receive_length == 0) ||
2930 (receive_length > MAILBOX_EXT_SIZE)) {
2931 rc = -ERANGE;
2932 goto job_done;
2935 rxbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2936 if (!rxbmp) {
2937 rc = -ENOMEM;
2938 goto job_done;
2941 rxbmp->virt = lpfc_mbuf_alloc(phba, 0, &rxbmp->phys);
2942 if (!rxbmp->virt) {
2943 rc = -ENOMEM;
2944 goto job_done;
2947 INIT_LIST_HEAD(&rxbmp->list);
2948 rxbpl = (struct ulp_bde64 *) rxbmp->virt;
2949 dmp = diag_cmd_data_alloc(phba, rxbpl, receive_length,
2951 if (!dmp) {
2952 rc = -ENOMEM;
2953 goto job_done;
2956 INIT_LIST_HEAD(&dmp->dma.list);
2957 nembed_sge->sge[0].pa_hi = putPaddrHigh(dmp->dma.phys);
2958 nembed_sge->sge[0].pa_lo = putPaddrLow(dmp->dma.phys);
2959 /* copy the transmit data found in the mailbox
2960 * extension area
2962 from = (uint8_t *)mb;
2963 from += sizeof(MAILBOX_t);
2964 memcpy((uint8_t *)dmp->dma.virt, from,
2965 header->cfg_mhdr.payload_length);
2969 dd_data->context_un.mbox.rxbmp = rxbmp;
2970 dd_data->context_un.mbox.dmp = dmp;
2972 /* setup wake call as IOCB callback */
2973 pmboxq->mbox_cmpl = lpfc_bsg_wake_mbox_wait;
2975 /* setup context field to pass wait_queue pointer to wake function */
2976 pmboxq->context1 = dd_data;
2977 dd_data->type = TYPE_MBOX;
2978 dd_data->context_un.mbox.pmboxq = pmboxq;
2979 dd_data->context_un.mbox.mb = mb;
2980 dd_data->context_un.mbox.set_job = job;
2981 dd_data->context_un.mbox.ext = ext;
2982 dd_data->context_un.mbox.mbOffset = mbox_req->mbOffset;
2983 dd_data->context_un.mbox.inExtWLen = mbox_req->inExtWLen;
2984 dd_data->context_un.mbox.outExtWLen = mbox_req->outExtWLen;
2985 job->dd_data = dd_data;
2987 if ((vport->fc_flag & FC_OFFLINE_MODE) ||
2988 (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))) {
2989 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
2990 if (rc != MBX_SUCCESS) {
2991 rc = (rc == MBX_TIMEOUT) ? -ETIME : -ENODEV;
2992 goto job_done;
2995 /* job finished, copy the data */
2996 memcpy(mb, pmb, sizeof(*pmb));
2997 job->reply->reply_payload_rcv_len =
2998 sg_copy_from_buffer(job->reply_payload.sg_list,
2999 job->reply_payload.sg_cnt,
3000 mb, size);
3001 /* not waiting mbox already done */
3002 rc = 0;
3003 goto job_done;
3006 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
3007 if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY))
3008 return 1; /* job started */
3010 job_done:
3011 /* common exit for error or job completed inline */
3012 kfree(mb);
3013 if (pmboxq)
3014 mempool_free(pmboxq, phba->mbox_mem_pool);
3015 kfree(ext);
3016 if (dmp) {
3017 dma_free_coherent(&phba->pcidev->dev,
3018 dmp->size, dmp->dma.virt,
3019 dmp->dma.phys);
3020 kfree(dmp);
3022 if (rxbmp) {
3023 lpfc_mbuf_free(phba, rxbmp->virt, rxbmp->phys);
3024 kfree(rxbmp);
3026 kfree(dd_data);
3028 return rc;
3032 * lpfc_bsg_mbox_cmd - process an fc bsg LPFC_BSG_VENDOR_MBOX command
3033 * @job: MBOX fc_bsg_job for LPFC_BSG_VENDOR_MBOX.
3035 static int
3036 lpfc_bsg_mbox_cmd(struct fc_bsg_job *job)
3038 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
3039 struct lpfc_hba *phba = vport->phba;
3040 int rc = 0;
3042 /* in case no data is transferred */
3043 job->reply->reply_payload_rcv_len = 0;
3044 if (job->request_len <
3045 sizeof(struct fc_bsg_request) + sizeof(struct dfc_mbox_req)) {
3046 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3047 "2737 Received MBOX_REQ request below "
3048 "minimum size\n");
3049 rc = -EINVAL;
3050 goto job_error;
3053 if (job->request_payload.payload_len != BSG_MBOX_SIZE) {
3054 rc = -EINVAL;
3055 goto job_error;
3058 if (job->reply_payload.payload_len != BSG_MBOX_SIZE) {
3059 rc = -EINVAL;
3060 goto job_error;
3063 if (phba->sli.sli_flag & LPFC_BLOCK_MGMT_IO) {
3064 rc = -EAGAIN;
3065 goto job_error;
3068 rc = lpfc_bsg_issue_mbox(phba, job, vport);
3070 job_error:
3071 if (rc == 0) {
3072 /* job done */
3073 job->reply->result = 0;
3074 job->dd_data = NULL;
3075 job->job_done(job);
3076 } else if (rc == 1)
3077 /* job submitted, will complete later*/
3078 rc = 0; /* return zero, no error */
3079 else {
3080 /* some error occurred */
3081 job->reply->result = rc;
3082 job->dd_data = NULL;
3085 return rc;
3089 * lpfc_bsg_menlo_cmd_cmp - lpfc_menlo_cmd completion handler
3090 * @phba: Pointer to HBA context object.
3091 * @cmdiocbq: Pointer to command iocb.
3092 * @rspiocbq: Pointer to response iocb.
3094 * This function is the completion handler for iocbs issued using
3095 * lpfc_menlo_cmd function. This function is called by the
3096 * ring event handler function without any lock held. This function
3097 * can be called from both worker thread context and interrupt
3098 * context. This function also can be called from another thread which
3099 * cleans up the SLI layer objects.
3100 * This function copies the contents of the response iocb to the
3101 * response iocb memory object provided by the caller of
3102 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
3103 * sleeps for the iocb completion.
3105 static void
3106 lpfc_bsg_menlo_cmd_cmp(struct lpfc_hba *phba,
3107 struct lpfc_iocbq *cmdiocbq,
3108 struct lpfc_iocbq *rspiocbq)
3110 struct bsg_job_data *dd_data;
3111 struct fc_bsg_job *job;
3112 IOCB_t *rsp;
3113 struct lpfc_dmabuf *bmp;
3114 struct lpfc_bsg_menlo *menlo;
3115 unsigned long flags;
3116 struct menlo_response *menlo_resp;
3117 int rc = 0;
3119 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3120 dd_data = cmdiocbq->context1;
3121 if (!dd_data) {
3122 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3123 return;
3126 menlo = &dd_data->context_un.menlo;
3127 job = menlo->set_job;
3128 job->dd_data = NULL; /* so timeout handler does not reply */
3130 spin_lock(&phba->hbalock);
3131 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
3132 if (cmdiocbq->context2 && rspiocbq)
3133 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
3134 &rspiocbq->iocb, sizeof(IOCB_t));
3135 spin_unlock(&phba->hbalock);
3137 bmp = menlo->bmp;
3138 rspiocbq = menlo->rspiocbq;
3139 rsp = &rspiocbq->iocb;
3141 pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
3142 job->request_payload.sg_cnt, DMA_TO_DEVICE);
3143 pci_unmap_sg(phba->pcidev, job->reply_payload.sg_list,
3144 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
3146 /* always return the xri, this would be used in the case
3147 * of a menlo download to allow the data to be sent as a continuation
3148 * of the exchange.
3150 menlo_resp = (struct menlo_response *)
3151 job->reply->reply_data.vendor_reply.vendor_rsp;
3152 menlo_resp->xri = rsp->ulpContext;
3153 if (rsp->ulpStatus) {
3154 if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
3155 switch (rsp->un.ulpWord[4] & 0xff) {
3156 case IOERR_SEQUENCE_TIMEOUT:
3157 rc = -ETIMEDOUT;
3158 break;
3159 case IOERR_INVALID_RPI:
3160 rc = -EFAULT;
3161 break;
3162 default:
3163 rc = -EACCES;
3164 break;
3166 } else
3167 rc = -EACCES;
3168 } else
3169 job->reply->reply_payload_rcv_len =
3170 rsp->un.genreq64.bdl.bdeSize;
3172 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
3173 lpfc_sli_release_iocbq(phba, rspiocbq);
3174 lpfc_sli_release_iocbq(phba, cmdiocbq);
3175 kfree(bmp);
3176 kfree(dd_data);
3177 /* make error code available to userspace */
3178 job->reply->result = rc;
3179 /* complete the job back to userspace */
3180 job->job_done(job);
3181 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3182 return;
3186 * lpfc_menlo_cmd - send an ioctl for menlo hardware
3187 * @job: fc_bsg_job to handle
3189 * This function issues a gen request 64 CR ioctl for all menlo cmd requests,
3190 * all the command completions will return the xri for the command.
3191 * For menlo data requests a gen request 64 CX is used to continue the exchange
3192 * supplied in the menlo request header xri field.
3194 static int
3195 lpfc_menlo_cmd(struct fc_bsg_job *job)
3197 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
3198 struct lpfc_hba *phba = vport->phba;
3199 struct lpfc_iocbq *cmdiocbq, *rspiocbq;
3200 IOCB_t *cmd, *rsp;
3201 int rc = 0;
3202 struct menlo_command *menlo_cmd;
3203 struct menlo_response *menlo_resp;
3204 struct lpfc_dmabuf *bmp = NULL;
3205 int request_nseg;
3206 int reply_nseg;
3207 struct scatterlist *sgel = NULL;
3208 int numbde;
3209 dma_addr_t busaddr;
3210 struct bsg_job_data *dd_data;
3211 struct ulp_bde64 *bpl = NULL;
3213 /* in case no data is returned return just the return code */
3214 job->reply->reply_payload_rcv_len = 0;
3216 if (job->request_len <
3217 sizeof(struct fc_bsg_request) +
3218 sizeof(struct menlo_command)) {
3219 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3220 "2784 Received MENLO_CMD request below "
3221 "minimum size\n");
3222 rc = -ERANGE;
3223 goto no_dd_data;
3226 if (job->reply_len <
3227 sizeof(struct fc_bsg_request) + sizeof(struct menlo_response)) {
3228 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3229 "2785 Received MENLO_CMD reply below "
3230 "minimum size\n");
3231 rc = -ERANGE;
3232 goto no_dd_data;
3235 if (!(phba->menlo_flag & HBA_MENLO_SUPPORT)) {
3236 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3237 "2786 Adapter does not support menlo "
3238 "commands\n");
3239 rc = -EPERM;
3240 goto no_dd_data;
3243 menlo_cmd = (struct menlo_command *)
3244 job->request->rqst_data.h_vendor.vendor_cmd;
3246 menlo_resp = (struct menlo_response *)
3247 job->reply->reply_data.vendor_reply.vendor_rsp;
3249 /* allocate our bsg tracking structure */
3250 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
3251 if (!dd_data) {
3252 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3253 "2787 Failed allocation of dd_data\n");
3254 rc = -ENOMEM;
3255 goto no_dd_data;
3258 bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3259 if (!bmp) {
3260 rc = -ENOMEM;
3261 goto free_dd;
3264 cmdiocbq = lpfc_sli_get_iocbq(phba);
3265 if (!cmdiocbq) {
3266 rc = -ENOMEM;
3267 goto free_bmp;
3270 rspiocbq = lpfc_sli_get_iocbq(phba);
3271 if (!rspiocbq) {
3272 rc = -ENOMEM;
3273 goto free_cmdiocbq;
3276 rsp = &rspiocbq->iocb;
3278 bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
3279 if (!bmp->virt) {
3280 rc = -ENOMEM;
3281 goto free_rspiocbq;
3284 INIT_LIST_HEAD(&bmp->list);
3285 bpl = (struct ulp_bde64 *) bmp->virt;
3286 request_nseg = pci_map_sg(phba->pcidev, job->request_payload.sg_list,
3287 job->request_payload.sg_cnt, DMA_TO_DEVICE);
3288 for_each_sg(job->request_payload.sg_list, sgel, request_nseg, numbde) {
3289 busaddr = sg_dma_address(sgel);
3290 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
3291 bpl->tus.f.bdeSize = sg_dma_len(sgel);
3292 bpl->tus.w = cpu_to_le32(bpl->tus.w);
3293 bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
3294 bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
3295 bpl++;
3298 reply_nseg = pci_map_sg(phba->pcidev, job->reply_payload.sg_list,
3299 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
3300 for_each_sg(job->reply_payload.sg_list, sgel, reply_nseg, numbde) {
3301 busaddr = sg_dma_address(sgel);
3302 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
3303 bpl->tus.f.bdeSize = sg_dma_len(sgel);
3304 bpl->tus.w = cpu_to_le32(bpl->tus.w);
3305 bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
3306 bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
3307 bpl++;
3310 cmd = &cmdiocbq->iocb;
3311 cmd->un.genreq64.bdl.ulpIoTag32 = 0;
3312 cmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
3313 cmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
3314 cmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
3315 cmd->un.genreq64.bdl.bdeSize =
3316 (request_nseg + reply_nseg) * sizeof(struct ulp_bde64);
3317 cmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
3318 cmd->un.genreq64.w5.hcsw.Dfctl = 0;
3319 cmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CMD;
3320 cmd->un.genreq64.w5.hcsw.Type = MENLO_TRANSPORT_TYPE; /* 0xfe */
3321 cmd->ulpBdeCount = 1;
3322 cmd->ulpClass = CLASS3;
3323 cmd->ulpOwner = OWN_CHIP;
3324 cmd->ulpLe = 1; /* Limited Edition */
3325 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
3326 cmdiocbq->vport = phba->pport;
3327 /* We want the firmware to timeout before we do */
3328 cmd->ulpTimeout = MENLO_TIMEOUT - 5;
3329 cmdiocbq->context3 = bmp;
3330 cmdiocbq->context2 = rspiocbq;
3331 cmdiocbq->iocb_cmpl = lpfc_bsg_menlo_cmd_cmp;
3332 cmdiocbq->context1 = dd_data;
3333 cmdiocbq->context2 = rspiocbq;
3334 if (menlo_cmd->cmd == LPFC_BSG_VENDOR_MENLO_CMD) {
3335 cmd->ulpCommand = CMD_GEN_REQUEST64_CR;
3336 cmd->ulpPU = MENLO_PU; /* 3 */
3337 cmd->un.ulpWord[4] = MENLO_DID; /* 0x0000FC0E */
3338 cmd->ulpContext = MENLO_CONTEXT; /* 0 */
3339 } else {
3340 cmd->ulpCommand = CMD_GEN_REQUEST64_CX;
3341 cmd->ulpPU = 1;
3342 cmd->un.ulpWord[4] = 0;
3343 cmd->ulpContext = menlo_cmd->xri;
3346 dd_data->type = TYPE_MENLO;
3347 dd_data->context_un.menlo.cmdiocbq = cmdiocbq;
3348 dd_data->context_un.menlo.rspiocbq = rspiocbq;
3349 dd_data->context_un.menlo.set_job = job;
3350 dd_data->context_un.menlo.bmp = bmp;
3352 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq,
3353 MENLO_TIMEOUT - 5);
3354 if (rc == IOCB_SUCCESS)
3355 return 0; /* done for now */
3357 /* iocb failed so cleanup */
3358 pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
3359 job->request_payload.sg_cnt, DMA_TO_DEVICE);
3360 pci_unmap_sg(phba->pcidev, job->reply_payload.sg_list,
3361 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
3363 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
3365 free_rspiocbq:
3366 lpfc_sli_release_iocbq(phba, rspiocbq);
3367 free_cmdiocbq:
3368 lpfc_sli_release_iocbq(phba, cmdiocbq);
3369 free_bmp:
3370 kfree(bmp);
3371 free_dd:
3372 kfree(dd_data);
3373 no_dd_data:
3374 /* make error code available to userspace */
3375 job->reply->result = rc;
3376 job->dd_data = NULL;
3377 return rc;
3380 * lpfc_bsg_hst_vendor - process a vendor-specific fc_bsg_job
3381 * @job: fc_bsg_job to handle
3383 static int
3384 lpfc_bsg_hst_vendor(struct fc_bsg_job *job)
3386 int command = job->request->rqst_data.h_vendor.vendor_cmd[0];
3387 int rc;
3389 switch (command) {
3390 case LPFC_BSG_VENDOR_SET_CT_EVENT:
3391 rc = lpfc_bsg_hba_set_event(job);
3392 break;
3393 case LPFC_BSG_VENDOR_GET_CT_EVENT:
3394 rc = lpfc_bsg_hba_get_event(job);
3395 break;
3396 case LPFC_BSG_VENDOR_SEND_MGMT_RESP:
3397 rc = lpfc_bsg_send_mgmt_rsp(job);
3398 break;
3399 case LPFC_BSG_VENDOR_DIAG_MODE:
3400 rc = lpfc_bsg_diag_mode(job);
3401 break;
3402 case LPFC_BSG_VENDOR_DIAG_TEST:
3403 rc = lpfc_bsg_diag_test(job);
3404 break;
3405 case LPFC_BSG_VENDOR_GET_MGMT_REV:
3406 rc = lpfc_bsg_get_dfc_rev(job);
3407 break;
3408 case LPFC_BSG_VENDOR_MBOX:
3409 rc = lpfc_bsg_mbox_cmd(job);
3410 break;
3411 case LPFC_BSG_VENDOR_MENLO_CMD:
3412 case LPFC_BSG_VENDOR_MENLO_DATA:
3413 rc = lpfc_menlo_cmd(job);
3414 break;
3415 default:
3416 rc = -EINVAL;
3417 job->reply->reply_payload_rcv_len = 0;
3418 /* make error code available to userspace */
3419 job->reply->result = rc;
3420 break;
3423 return rc;
3427 * lpfc_bsg_request - handle a bsg request from the FC transport
3428 * @job: fc_bsg_job to handle
3431 lpfc_bsg_request(struct fc_bsg_job *job)
3433 uint32_t msgcode;
3434 int rc;
3436 msgcode = job->request->msgcode;
3437 switch (msgcode) {
3438 case FC_BSG_HST_VENDOR:
3439 rc = lpfc_bsg_hst_vendor(job);
3440 break;
3441 case FC_BSG_RPT_ELS:
3442 rc = lpfc_bsg_rport_els(job);
3443 break;
3444 case FC_BSG_RPT_CT:
3445 rc = lpfc_bsg_send_mgmt_cmd(job);
3446 break;
3447 default:
3448 rc = -EINVAL;
3449 job->reply->reply_payload_rcv_len = 0;
3450 /* make error code available to userspace */
3451 job->reply->result = rc;
3452 break;
3455 return rc;
3459 * lpfc_bsg_timeout - handle timeout of a bsg request from the FC transport
3460 * @job: fc_bsg_job that has timed out
3462 * This function just aborts the job's IOCB. The aborted IOCB will return to
3463 * the waiting function which will handle passing the error back to userspace
3466 lpfc_bsg_timeout(struct fc_bsg_job *job)
3468 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
3469 struct lpfc_hba *phba = vport->phba;
3470 struct lpfc_iocbq *cmdiocb;
3471 struct lpfc_bsg_event *evt;
3472 struct lpfc_bsg_iocb *iocb;
3473 struct lpfc_bsg_mbox *mbox;
3474 struct lpfc_bsg_menlo *menlo;
3475 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
3476 struct bsg_job_data *dd_data;
3477 unsigned long flags;
3479 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3480 dd_data = (struct bsg_job_data *)job->dd_data;
3481 /* timeout and completion crossed paths if no dd_data */
3482 if (!dd_data) {
3483 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3484 return 0;
3487 switch (dd_data->type) {
3488 case TYPE_IOCB:
3489 iocb = &dd_data->context_un.iocb;
3490 cmdiocb = iocb->cmdiocbq;
3491 /* hint to completion handler that the job timed out */
3492 job->reply->result = -EAGAIN;
3493 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3494 /* this will call our completion handler */
3495 spin_lock_irq(&phba->hbalock);
3496 lpfc_sli_issue_abort_iotag(phba, pring, cmdiocb);
3497 spin_unlock_irq(&phba->hbalock);
3498 break;
3499 case TYPE_EVT:
3500 evt = dd_data->context_un.evt;
3501 /* this event has no job anymore */
3502 evt->set_job = NULL;
3503 job->dd_data = NULL;
3504 job->reply->reply_payload_rcv_len = 0;
3505 /* Return -EAGAIN which is our way of signallying the
3506 * app to retry.
3508 job->reply->result = -EAGAIN;
3509 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3510 job->job_done(job);
3511 break;
3512 case TYPE_MBOX:
3513 mbox = &dd_data->context_un.mbox;
3514 /* this mbox has no job anymore */
3515 mbox->set_job = NULL;
3516 job->dd_data = NULL;
3517 job->reply->reply_payload_rcv_len = 0;
3518 job->reply->result = -EAGAIN;
3519 /* the mbox completion handler can now be run */
3520 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3521 job->job_done(job);
3522 break;
3523 case TYPE_MENLO:
3524 menlo = &dd_data->context_un.menlo;
3525 cmdiocb = menlo->cmdiocbq;
3526 /* hint to completion handler that the job timed out */
3527 job->reply->result = -EAGAIN;
3528 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3529 /* this will call our completion handler */
3530 spin_lock_irq(&phba->hbalock);
3531 lpfc_sli_issue_abort_iotag(phba, pring, cmdiocb);
3532 spin_unlock_irq(&phba->hbalock);
3533 break;
3534 default:
3535 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3536 break;
3539 /* scsi transport fc fc_bsg_job_timeout expects a zero return code,
3540 * otherwise an error message will be displayed on the console
3541 * so always return success (zero)
3543 return 0;