Add linux-next specific files for 20110831
[linux-2.6/next.git] / drivers / scsi / lpfc / lpfc_bsg.c
blob6760c69f525381f2d4c89be25ef7b4c5d5bc36ed
1 /*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
3 * Fibre Channel Host Bus Adapters. *
4 * Copyright (C) 2009-2011 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>
26 #include <linux/list.h>
28 #include <scsi/scsi.h>
29 #include <scsi/scsi_host.h>
30 #include <scsi/scsi_transport_fc.h>
31 #include <scsi/scsi_bsg_fc.h>
32 #include <scsi/fc/fc_fs.h>
34 #include "lpfc_hw4.h"
35 #include "lpfc_hw.h"
36 #include "lpfc_sli.h"
37 #include "lpfc_sli4.h"
38 #include "lpfc_nl.h"
39 #include "lpfc_bsg.h"
40 #include "lpfc_disc.h"
41 #include "lpfc_scsi.h"
42 #include "lpfc.h"
43 #include "lpfc_logmsg.h"
44 #include "lpfc_crtn.h"
45 #include "lpfc_debugfs.h"
46 #include "lpfc_vport.h"
47 #include "lpfc_version.h"
49 struct lpfc_bsg_event {
50 struct list_head node;
51 struct kref kref;
52 wait_queue_head_t wq;
54 /* Event type and waiter identifiers */
55 uint32_t type_mask;
56 uint32_t req_id;
57 uint32_t reg_id;
59 /* next two flags are here for the auto-delete logic */
60 unsigned long wait_time_stamp;
61 int waiting;
63 /* seen and not seen events */
64 struct list_head events_to_get;
65 struct list_head events_to_see;
67 /* job waiting for this event to finish */
68 struct fc_bsg_job *set_job;
71 struct lpfc_bsg_iocb {
72 struct lpfc_iocbq *cmdiocbq;
73 struct lpfc_iocbq *rspiocbq;
74 struct lpfc_dmabuf *bmp;
75 struct lpfc_nodelist *ndlp;
77 /* job waiting for this iocb to finish */
78 struct fc_bsg_job *set_job;
81 struct lpfc_bsg_mbox {
82 LPFC_MBOXQ_t *pmboxq;
83 MAILBOX_t *mb;
84 struct lpfc_dmabuf *dmabuffers; /* for BIU diags */
85 uint8_t *ext; /* extended mailbox data */
86 uint32_t mbOffset; /* from app */
87 uint32_t inExtWLen; /* from app */
88 uint32_t outExtWLen; /* from app */
90 /* job waiting for this mbox command to finish */
91 struct fc_bsg_job *set_job;
94 #define MENLO_DID 0x0000FC0E
96 struct lpfc_bsg_menlo {
97 struct lpfc_iocbq *cmdiocbq;
98 struct lpfc_iocbq *rspiocbq;
99 struct lpfc_dmabuf *bmp;
101 /* job waiting for this iocb to finish */
102 struct fc_bsg_job *set_job;
105 #define TYPE_EVT 1
106 #define TYPE_IOCB 2
107 #define TYPE_MBOX 3
108 #define TYPE_MENLO 4
109 struct bsg_job_data {
110 uint32_t type;
111 union {
112 struct lpfc_bsg_event *evt;
113 struct lpfc_bsg_iocb iocb;
114 struct lpfc_bsg_mbox mbox;
115 struct lpfc_bsg_menlo menlo;
116 } context_un;
119 struct event_data {
120 struct list_head node;
121 uint32_t type;
122 uint32_t immed_dat;
123 void *data;
124 uint32_t len;
127 #define BUF_SZ_4K 4096
128 #define SLI_CT_ELX_LOOPBACK 0x10
130 enum ELX_LOOPBACK_CMD {
131 ELX_LOOPBACK_XRI_SETUP,
132 ELX_LOOPBACK_DATA,
135 #define ELX_LOOPBACK_HEADER_SZ \
136 (size_t)(&((struct lpfc_sli_ct_request *)NULL)->un)
138 struct lpfc_dmabufext {
139 struct lpfc_dmabuf dma;
140 uint32_t size;
141 uint32_t flag;
145 * lpfc_bsg_send_mgmt_cmd_cmp - lpfc_bsg_send_mgmt_cmd's completion handler
146 * @phba: Pointer to HBA context object.
147 * @cmdiocbq: Pointer to command iocb.
148 * @rspiocbq: Pointer to response iocb.
150 * This function is the completion handler for iocbs issued using
151 * lpfc_bsg_send_mgmt_cmd function. This function is called by the
152 * ring event handler function without any lock held. This function
153 * can be called from both worker thread context and interrupt
154 * context. This function also can be called from another thread which
155 * cleans up the SLI layer objects.
156 * This function copies the contents of the response iocb to the
157 * response iocb memory object provided by the caller of
158 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
159 * sleeps for the iocb completion.
161 static void
162 lpfc_bsg_send_mgmt_cmd_cmp(struct lpfc_hba *phba,
163 struct lpfc_iocbq *cmdiocbq,
164 struct lpfc_iocbq *rspiocbq)
166 struct bsg_job_data *dd_data;
167 struct fc_bsg_job *job;
168 IOCB_t *rsp;
169 struct lpfc_dmabuf *bmp;
170 struct lpfc_nodelist *ndlp;
171 struct lpfc_bsg_iocb *iocb;
172 unsigned long flags;
173 int rc = 0;
175 spin_lock_irqsave(&phba->ct_ev_lock, flags);
176 dd_data = cmdiocbq->context2;
177 if (!dd_data) {
178 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
179 lpfc_sli_release_iocbq(phba, cmdiocbq);
180 return;
183 iocb = &dd_data->context_un.iocb;
184 job = iocb->set_job;
185 job->dd_data = NULL; /* so timeout handler does not reply */
187 bmp = iocb->bmp;
188 rsp = &rspiocbq->iocb;
189 ndlp = cmdiocbq->context1;
191 pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
192 job->request_payload.sg_cnt, DMA_TO_DEVICE);
193 pci_unmap_sg(phba->pcidev, job->reply_payload.sg_list,
194 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
196 if (rsp->ulpStatus) {
197 if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
198 switch (rsp->un.ulpWord[4] & 0xff) {
199 case IOERR_SEQUENCE_TIMEOUT:
200 rc = -ETIMEDOUT;
201 break;
202 case IOERR_INVALID_RPI:
203 rc = -EFAULT;
204 break;
205 default:
206 rc = -EACCES;
207 break;
209 } else
210 rc = -EACCES;
211 } else
212 job->reply->reply_payload_rcv_len =
213 rsp->un.genreq64.bdl.bdeSize;
215 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
216 lpfc_sli_release_iocbq(phba, cmdiocbq);
217 lpfc_nlp_put(ndlp);
218 kfree(bmp);
219 kfree(dd_data);
220 /* make error code available to userspace */
221 job->reply->result = rc;
222 /* complete the job back to userspace */
223 job->job_done(job);
224 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
225 return;
229 * lpfc_bsg_send_mgmt_cmd - send a CT command from a bsg request
230 * @job: fc_bsg_job to handle
232 static int
233 lpfc_bsg_send_mgmt_cmd(struct fc_bsg_job *job)
235 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
236 struct lpfc_hba *phba = vport->phba;
237 struct lpfc_rport_data *rdata = job->rport->dd_data;
238 struct lpfc_nodelist *ndlp = rdata->pnode;
239 struct ulp_bde64 *bpl = NULL;
240 uint32_t timeout;
241 struct lpfc_iocbq *cmdiocbq = NULL;
242 IOCB_t *cmd;
243 struct lpfc_dmabuf *bmp = NULL;
244 int request_nseg;
245 int reply_nseg;
246 struct scatterlist *sgel = NULL;
247 int numbde;
248 dma_addr_t busaddr;
249 struct bsg_job_data *dd_data;
250 uint32_t creg_val;
251 int rc = 0;
252 int iocb_stat;
254 /* in case no data is transferred */
255 job->reply->reply_payload_rcv_len = 0;
257 /* allocate our bsg tracking structure */
258 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
259 if (!dd_data) {
260 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
261 "2733 Failed allocation of dd_data\n");
262 rc = -ENOMEM;
263 goto no_dd_data;
266 if (!lpfc_nlp_get(ndlp)) {
267 rc = -ENODEV;
268 goto no_ndlp;
271 bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
272 if (!bmp) {
273 rc = -ENOMEM;
274 goto free_ndlp;
277 if (ndlp->nlp_flag & NLP_ELS_SND_MASK) {
278 rc = -ENODEV;
279 goto free_bmp;
282 cmdiocbq = lpfc_sli_get_iocbq(phba);
283 if (!cmdiocbq) {
284 rc = -ENOMEM;
285 goto free_bmp;
288 cmd = &cmdiocbq->iocb;
289 bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
290 if (!bmp->virt) {
291 rc = -ENOMEM;
292 goto free_cmdiocbq;
295 INIT_LIST_HEAD(&bmp->list);
296 bpl = (struct ulp_bde64 *) bmp->virt;
297 request_nseg = pci_map_sg(phba->pcidev, job->request_payload.sg_list,
298 job->request_payload.sg_cnt, DMA_TO_DEVICE);
299 for_each_sg(job->request_payload.sg_list, sgel, request_nseg, numbde) {
300 busaddr = sg_dma_address(sgel);
301 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
302 bpl->tus.f.bdeSize = sg_dma_len(sgel);
303 bpl->tus.w = cpu_to_le32(bpl->tus.w);
304 bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
305 bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
306 bpl++;
309 reply_nseg = pci_map_sg(phba->pcidev, job->reply_payload.sg_list,
310 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
311 for_each_sg(job->reply_payload.sg_list, sgel, reply_nseg, numbde) {
312 busaddr = sg_dma_address(sgel);
313 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
314 bpl->tus.f.bdeSize = sg_dma_len(sgel);
315 bpl->tus.w = cpu_to_le32(bpl->tus.w);
316 bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
317 bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
318 bpl++;
321 cmd->un.genreq64.bdl.ulpIoTag32 = 0;
322 cmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
323 cmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
324 cmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
325 cmd->un.genreq64.bdl.bdeSize =
326 (request_nseg + reply_nseg) * sizeof(struct ulp_bde64);
327 cmd->ulpCommand = CMD_GEN_REQUEST64_CR;
328 cmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
329 cmd->un.genreq64.w5.hcsw.Dfctl = 0;
330 cmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
331 cmd->un.genreq64.w5.hcsw.Type = FC_TYPE_CT;
332 cmd->ulpBdeCount = 1;
333 cmd->ulpLe = 1;
334 cmd->ulpClass = CLASS3;
335 cmd->ulpContext = ndlp->nlp_rpi;
336 if (phba->sli_rev == LPFC_SLI_REV4)
337 cmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
338 cmd->ulpOwner = OWN_CHIP;
339 cmdiocbq->vport = phba->pport;
340 cmdiocbq->context3 = bmp;
341 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
342 timeout = phba->fc_ratov * 2;
343 cmd->ulpTimeout = timeout;
345 cmdiocbq->iocb_cmpl = lpfc_bsg_send_mgmt_cmd_cmp;
346 cmdiocbq->context1 = ndlp;
347 cmdiocbq->context2 = dd_data;
348 dd_data->type = TYPE_IOCB;
349 dd_data->context_un.iocb.cmdiocbq = cmdiocbq;
350 dd_data->context_un.iocb.set_job = job;
351 dd_data->context_un.iocb.bmp = bmp;
353 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
354 if (lpfc_readl(phba->HCregaddr, &creg_val)) {
355 rc = -EIO ;
356 goto free_cmdiocbq;
358 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
359 writel(creg_val, phba->HCregaddr);
360 readl(phba->HCregaddr); /* flush */
363 iocb_stat = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq, 0);
364 if (iocb_stat == IOCB_SUCCESS)
365 return 0; /* done for now */
366 else if (iocb_stat == IOCB_BUSY)
367 rc = -EAGAIN;
368 else
369 rc = -EIO;
372 /* iocb failed so cleanup */
373 pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
374 job->request_payload.sg_cnt, DMA_TO_DEVICE);
375 pci_unmap_sg(phba->pcidev, job->reply_payload.sg_list,
376 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
378 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
380 free_cmdiocbq:
381 lpfc_sli_release_iocbq(phba, cmdiocbq);
382 free_bmp:
383 kfree(bmp);
384 free_ndlp:
385 lpfc_nlp_put(ndlp);
386 no_ndlp:
387 kfree(dd_data);
388 no_dd_data:
389 /* make error code available to userspace */
390 job->reply->result = rc;
391 job->dd_data = NULL;
392 return rc;
396 * lpfc_bsg_rport_els_cmp - lpfc_bsg_rport_els's completion handler
397 * @phba: Pointer to HBA context object.
398 * @cmdiocbq: Pointer to command iocb.
399 * @rspiocbq: Pointer to response iocb.
401 * This function is the completion handler for iocbs issued using
402 * lpfc_bsg_rport_els_cmp function. This function is called by the
403 * ring event handler function without any lock held. This function
404 * can be called from both worker thread context and interrupt
405 * context. This function also can be called from other thread which
406 * cleans up the SLI layer objects.
407 * This function copies the contents of the response iocb to the
408 * response iocb memory object provided by the caller of
409 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
410 * sleeps for the iocb completion.
412 static void
413 lpfc_bsg_rport_els_cmp(struct lpfc_hba *phba,
414 struct lpfc_iocbq *cmdiocbq,
415 struct lpfc_iocbq *rspiocbq)
417 struct bsg_job_data *dd_data;
418 struct fc_bsg_job *job;
419 IOCB_t *rsp;
420 struct lpfc_nodelist *ndlp;
421 struct lpfc_dmabuf *pbuflist = NULL;
422 struct fc_bsg_ctels_reply *els_reply;
423 uint8_t *rjt_data;
424 unsigned long flags;
425 int rc = 0;
427 spin_lock_irqsave(&phba->ct_ev_lock, flags);
428 dd_data = cmdiocbq->context1;
429 /* normal completion and timeout crossed paths, already done */
430 if (!dd_data) {
431 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
432 return;
435 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
436 if (cmdiocbq->context2 && rspiocbq)
437 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
438 &rspiocbq->iocb, sizeof(IOCB_t));
440 job = dd_data->context_un.iocb.set_job;
441 cmdiocbq = dd_data->context_un.iocb.cmdiocbq;
442 rspiocbq = dd_data->context_un.iocb.rspiocbq;
443 rsp = &rspiocbq->iocb;
444 ndlp = dd_data->context_un.iocb.ndlp;
446 pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
447 job->request_payload.sg_cnt, DMA_TO_DEVICE);
448 pci_unmap_sg(phba->pcidev, job->reply_payload.sg_list,
449 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
451 if (job->reply->result == -EAGAIN)
452 rc = -EAGAIN;
453 else if (rsp->ulpStatus == IOSTAT_SUCCESS)
454 job->reply->reply_payload_rcv_len =
455 rsp->un.elsreq64.bdl.bdeSize;
456 else if (rsp->ulpStatus == IOSTAT_LS_RJT) {
457 job->reply->reply_payload_rcv_len =
458 sizeof(struct fc_bsg_ctels_reply);
459 /* LS_RJT data returned in word 4 */
460 rjt_data = (uint8_t *)&rsp->un.ulpWord[4];
461 els_reply = &job->reply->reply_data.ctels_reply;
462 els_reply->status = FC_CTELS_STATUS_REJECT;
463 els_reply->rjt_data.action = rjt_data[3];
464 els_reply->rjt_data.reason_code = rjt_data[2];
465 els_reply->rjt_data.reason_explanation = rjt_data[1];
466 els_reply->rjt_data.vendor_unique = rjt_data[0];
467 } else
468 rc = -EIO;
470 pbuflist = (struct lpfc_dmabuf *) cmdiocbq->context3;
471 lpfc_mbuf_free(phba, pbuflist->virt, pbuflist->phys);
472 lpfc_sli_release_iocbq(phba, rspiocbq);
473 lpfc_sli_release_iocbq(phba, cmdiocbq);
474 lpfc_nlp_put(ndlp);
475 kfree(dd_data);
476 /* make error code available to userspace */
477 job->reply->result = rc;
478 job->dd_data = NULL;
479 /* complete the job back to userspace */
480 job->job_done(job);
481 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
482 return;
486 * lpfc_bsg_rport_els - send an ELS command from a bsg request
487 * @job: fc_bsg_job to handle
489 static int
490 lpfc_bsg_rport_els(struct fc_bsg_job *job)
492 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
493 struct lpfc_hba *phba = vport->phba;
494 struct lpfc_rport_data *rdata = job->rport->dd_data;
495 struct lpfc_nodelist *ndlp = rdata->pnode;
496 uint32_t elscmd;
497 uint32_t cmdsize;
498 uint32_t rspsize;
499 struct lpfc_iocbq *rspiocbq;
500 struct lpfc_iocbq *cmdiocbq;
501 IOCB_t *rsp;
502 uint16_t rpi = 0;
503 struct lpfc_dmabuf *pcmd;
504 struct lpfc_dmabuf *prsp;
505 struct lpfc_dmabuf *pbuflist = NULL;
506 struct ulp_bde64 *bpl;
507 int request_nseg;
508 int reply_nseg;
509 struct scatterlist *sgel = NULL;
510 int numbde;
511 dma_addr_t busaddr;
512 struct bsg_job_data *dd_data;
513 uint32_t creg_val;
514 int rc = 0;
516 /* in case no data is transferred */
517 job->reply->reply_payload_rcv_len = 0;
519 /* allocate our bsg tracking structure */
520 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
521 if (!dd_data) {
522 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
523 "2735 Failed allocation of dd_data\n");
524 rc = -ENOMEM;
525 goto no_dd_data;
528 if (!lpfc_nlp_get(ndlp)) {
529 rc = -ENODEV;
530 goto free_dd_data;
533 elscmd = job->request->rqst_data.r_els.els_code;
534 cmdsize = job->request_payload.payload_len;
535 rspsize = job->reply_payload.payload_len;
536 rspiocbq = lpfc_sli_get_iocbq(phba);
537 if (!rspiocbq) {
538 lpfc_nlp_put(ndlp);
539 rc = -ENOMEM;
540 goto free_dd_data;
543 rsp = &rspiocbq->iocb;
544 rpi = ndlp->nlp_rpi;
546 cmdiocbq = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp,
547 ndlp->nlp_DID, elscmd);
548 if (!cmdiocbq) {
549 rc = -EIO;
550 goto free_rspiocbq;
553 /* prep els iocb set context1 to the ndlp, context2 to the command
554 * dmabuf, context3 holds the data dmabuf
556 pcmd = (struct lpfc_dmabuf *) cmdiocbq->context2;
557 prsp = (struct lpfc_dmabuf *) pcmd->list.next;
558 lpfc_mbuf_free(phba, pcmd->virt, pcmd->phys);
559 kfree(pcmd);
560 lpfc_mbuf_free(phba, prsp->virt, prsp->phys);
561 kfree(prsp);
562 cmdiocbq->context2 = NULL;
564 pbuflist = (struct lpfc_dmabuf *) cmdiocbq->context3;
565 bpl = (struct ulp_bde64 *) pbuflist->virt;
567 request_nseg = pci_map_sg(phba->pcidev, job->request_payload.sg_list,
568 job->request_payload.sg_cnt, DMA_TO_DEVICE);
569 for_each_sg(job->request_payload.sg_list, sgel, request_nseg, numbde) {
570 busaddr = sg_dma_address(sgel);
571 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
572 bpl->tus.f.bdeSize = sg_dma_len(sgel);
573 bpl->tus.w = cpu_to_le32(bpl->tus.w);
574 bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
575 bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
576 bpl++;
579 reply_nseg = pci_map_sg(phba->pcidev, job->reply_payload.sg_list,
580 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
581 for_each_sg(job->reply_payload.sg_list, sgel, reply_nseg, numbde) {
582 busaddr = sg_dma_address(sgel);
583 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
584 bpl->tus.f.bdeSize = sg_dma_len(sgel);
585 bpl->tus.w = cpu_to_le32(bpl->tus.w);
586 bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
587 bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
588 bpl++;
590 cmdiocbq->iocb.un.elsreq64.bdl.bdeSize =
591 (request_nseg + reply_nseg) * sizeof(struct ulp_bde64);
592 cmdiocbq->iocb.ulpContext = rpi;
593 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
594 cmdiocbq->context1 = NULL;
595 cmdiocbq->context2 = NULL;
597 cmdiocbq->iocb_cmpl = lpfc_bsg_rport_els_cmp;
598 cmdiocbq->context1 = dd_data;
599 cmdiocbq->context2 = rspiocbq;
600 dd_data->type = TYPE_IOCB;
601 dd_data->context_un.iocb.cmdiocbq = cmdiocbq;
602 dd_data->context_un.iocb.rspiocbq = rspiocbq;
603 dd_data->context_un.iocb.set_job = job;
604 dd_data->context_un.iocb.bmp = NULL;
605 dd_data->context_un.iocb.ndlp = ndlp;
607 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
608 if (lpfc_readl(phba->HCregaddr, &creg_val)) {
609 rc = -EIO;
610 goto linkdown_err;
612 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
613 writel(creg_val, phba->HCregaddr);
614 readl(phba->HCregaddr); /* flush */
616 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq, 0);
617 lpfc_nlp_put(ndlp);
618 if (rc == IOCB_SUCCESS)
619 return 0; /* done for now */
620 else if (rc == IOCB_BUSY)
621 rc = -EAGAIN;
622 else
623 rc = -EIO;
625 linkdown_err:
626 pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
627 job->request_payload.sg_cnt, DMA_TO_DEVICE);
628 pci_unmap_sg(phba->pcidev, job->reply_payload.sg_list,
629 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
631 lpfc_mbuf_free(phba, pbuflist->virt, pbuflist->phys);
633 lpfc_sli_release_iocbq(phba, cmdiocbq);
635 free_rspiocbq:
636 lpfc_sli_release_iocbq(phba, rspiocbq);
638 free_dd_data:
639 kfree(dd_data);
641 no_dd_data:
642 /* make error code available to userspace */
643 job->reply->result = rc;
644 job->dd_data = NULL;
645 return rc;
649 * lpfc_bsg_event_free - frees an allocated event structure
650 * @kref: Pointer to a kref.
652 * Called from kref_put. Back cast the kref into an event structure address.
653 * Free any events to get, delete associated nodes, free any events to see,
654 * free any data then free the event itself.
656 static void
657 lpfc_bsg_event_free(struct kref *kref)
659 struct lpfc_bsg_event *evt = container_of(kref, struct lpfc_bsg_event,
660 kref);
661 struct event_data *ed;
663 list_del(&evt->node);
665 while (!list_empty(&evt->events_to_get)) {
666 ed = list_entry(evt->events_to_get.next, typeof(*ed), node);
667 list_del(&ed->node);
668 kfree(ed->data);
669 kfree(ed);
672 while (!list_empty(&evt->events_to_see)) {
673 ed = list_entry(evt->events_to_see.next, typeof(*ed), node);
674 list_del(&ed->node);
675 kfree(ed->data);
676 kfree(ed);
679 kfree(evt);
683 * lpfc_bsg_event_ref - increments the kref for an event
684 * @evt: Pointer to an event structure.
686 static inline void
687 lpfc_bsg_event_ref(struct lpfc_bsg_event *evt)
689 kref_get(&evt->kref);
693 * lpfc_bsg_event_unref - Uses kref_put to free an event structure
694 * @evt: Pointer to an event structure.
696 static inline void
697 lpfc_bsg_event_unref(struct lpfc_bsg_event *evt)
699 kref_put(&evt->kref, lpfc_bsg_event_free);
703 * lpfc_bsg_event_new - allocate and initialize a event structure
704 * @ev_mask: Mask of events.
705 * @ev_reg_id: Event reg id.
706 * @ev_req_id: Event request id.
708 static struct lpfc_bsg_event *
709 lpfc_bsg_event_new(uint32_t ev_mask, int ev_reg_id, uint32_t ev_req_id)
711 struct lpfc_bsg_event *evt = kzalloc(sizeof(*evt), GFP_KERNEL);
713 if (!evt)
714 return NULL;
716 INIT_LIST_HEAD(&evt->events_to_get);
717 INIT_LIST_HEAD(&evt->events_to_see);
718 evt->type_mask = ev_mask;
719 evt->req_id = ev_req_id;
720 evt->reg_id = ev_reg_id;
721 evt->wait_time_stamp = jiffies;
722 init_waitqueue_head(&evt->wq);
723 kref_init(&evt->kref);
724 return evt;
728 * diag_cmd_data_free - Frees an lpfc dma buffer extension
729 * @phba: Pointer to HBA context object.
730 * @mlist: Pointer to an lpfc dma buffer extension.
732 static int
733 diag_cmd_data_free(struct lpfc_hba *phba, struct lpfc_dmabufext *mlist)
735 struct lpfc_dmabufext *mlast;
736 struct pci_dev *pcidev;
737 struct list_head head, *curr, *next;
739 if ((!mlist) || (!lpfc_is_link_up(phba) &&
740 (phba->link_flag & LS_LOOPBACK_MODE))) {
741 return 0;
744 pcidev = phba->pcidev;
745 list_add_tail(&head, &mlist->dma.list);
747 list_for_each_safe(curr, next, &head) {
748 mlast = list_entry(curr, struct lpfc_dmabufext , dma.list);
749 if (mlast->dma.virt)
750 dma_free_coherent(&pcidev->dev,
751 mlast->size,
752 mlast->dma.virt,
753 mlast->dma.phys);
754 kfree(mlast);
756 return 0;
760 * lpfc_bsg_ct_unsol_event - process an unsolicited CT command
761 * @phba:
762 * @pring:
763 * @piocbq:
765 * This function is called when an unsolicited CT command is received. It
766 * forwards the event to any processes registered to receive CT events.
769 lpfc_bsg_ct_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
770 struct lpfc_iocbq *piocbq)
772 uint32_t evt_req_id = 0;
773 uint32_t cmd;
774 uint32_t len;
775 struct lpfc_dmabuf *dmabuf = NULL;
776 struct lpfc_bsg_event *evt;
777 struct event_data *evt_dat = NULL;
778 struct lpfc_iocbq *iocbq;
779 size_t offset = 0;
780 struct list_head head;
781 struct ulp_bde64 *bde;
782 dma_addr_t dma_addr;
783 int i;
784 struct lpfc_dmabuf *bdeBuf1 = piocbq->context2;
785 struct lpfc_dmabuf *bdeBuf2 = piocbq->context3;
786 struct lpfc_hbq_entry *hbqe;
787 struct lpfc_sli_ct_request *ct_req;
788 struct fc_bsg_job *job = NULL;
789 unsigned long flags;
790 int size = 0;
792 INIT_LIST_HEAD(&head);
793 list_add_tail(&head, &piocbq->list);
795 if (piocbq->iocb.ulpBdeCount == 0 ||
796 piocbq->iocb.un.cont64[0].tus.f.bdeSize == 0)
797 goto error_ct_unsol_exit;
799 if (phba->link_state == LPFC_HBA_ERROR ||
800 (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)))
801 goto error_ct_unsol_exit;
803 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
804 dmabuf = bdeBuf1;
805 else {
806 dma_addr = getPaddr(piocbq->iocb.un.cont64[0].addrHigh,
807 piocbq->iocb.un.cont64[0].addrLow);
808 dmabuf = lpfc_sli_ringpostbuf_get(phba, pring, dma_addr);
810 if (dmabuf == NULL)
811 goto error_ct_unsol_exit;
812 ct_req = (struct lpfc_sli_ct_request *)dmabuf->virt;
813 evt_req_id = ct_req->FsType;
814 cmd = ct_req->CommandResponse.bits.CmdRsp;
815 len = ct_req->CommandResponse.bits.Size;
816 if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
817 lpfc_sli_ringpostbuf_put(phba, pring, dmabuf);
819 spin_lock_irqsave(&phba->ct_ev_lock, flags);
820 list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
821 if (!(evt->type_mask & FC_REG_CT_EVENT) ||
822 evt->req_id != evt_req_id)
823 continue;
825 lpfc_bsg_event_ref(evt);
826 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
827 evt_dat = kzalloc(sizeof(*evt_dat), GFP_KERNEL);
828 if (evt_dat == NULL) {
829 spin_lock_irqsave(&phba->ct_ev_lock, flags);
830 lpfc_bsg_event_unref(evt);
831 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
832 "2614 Memory allocation failed for "
833 "CT event\n");
834 break;
837 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
838 /* take accumulated byte count from the last iocbq */
839 iocbq = list_entry(head.prev, typeof(*iocbq), list);
840 evt_dat->len = iocbq->iocb.unsli3.rcvsli3.acc_len;
841 } else {
842 list_for_each_entry(iocbq, &head, list) {
843 for (i = 0; i < iocbq->iocb.ulpBdeCount; i++)
844 evt_dat->len +=
845 iocbq->iocb.un.cont64[i].tus.f.bdeSize;
849 evt_dat->data = kzalloc(evt_dat->len, GFP_KERNEL);
850 if (evt_dat->data == NULL) {
851 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
852 "2615 Memory allocation failed for "
853 "CT event data, size %d\n",
854 evt_dat->len);
855 kfree(evt_dat);
856 spin_lock_irqsave(&phba->ct_ev_lock, flags);
857 lpfc_bsg_event_unref(evt);
858 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
859 goto error_ct_unsol_exit;
862 list_for_each_entry(iocbq, &head, list) {
863 size = 0;
864 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
865 bdeBuf1 = iocbq->context2;
866 bdeBuf2 = iocbq->context3;
868 for (i = 0; i < iocbq->iocb.ulpBdeCount; i++) {
869 if (phba->sli3_options &
870 LPFC_SLI3_HBQ_ENABLED) {
871 if (i == 0) {
872 hbqe = (struct lpfc_hbq_entry *)
873 &iocbq->iocb.un.ulpWord[0];
874 size = hbqe->bde.tus.f.bdeSize;
875 dmabuf = bdeBuf1;
876 } else if (i == 1) {
877 hbqe = (struct lpfc_hbq_entry *)
878 &iocbq->iocb.unsli3.
879 sli3Words[4];
880 size = hbqe->bde.tus.f.bdeSize;
881 dmabuf = bdeBuf2;
883 if ((offset + size) > evt_dat->len)
884 size = evt_dat->len - offset;
885 } else {
886 size = iocbq->iocb.un.cont64[i].
887 tus.f.bdeSize;
888 bde = &iocbq->iocb.un.cont64[i];
889 dma_addr = getPaddr(bde->addrHigh,
890 bde->addrLow);
891 dmabuf = lpfc_sli_ringpostbuf_get(phba,
892 pring, dma_addr);
894 if (!dmabuf) {
895 lpfc_printf_log(phba, KERN_ERR,
896 LOG_LIBDFC, "2616 No dmabuf "
897 "found for iocbq 0x%p\n",
898 iocbq);
899 kfree(evt_dat->data);
900 kfree(evt_dat);
901 spin_lock_irqsave(&phba->ct_ev_lock,
902 flags);
903 lpfc_bsg_event_unref(evt);
904 spin_unlock_irqrestore(
905 &phba->ct_ev_lock, flags);
906 goto error_ct_unsol_exit;
908 memcpy((char *)(evt_dat->data) + offset,
909 dmabuf->virt, size);
910 offset += size;
911 if (evt_req_id != SLI_CT_ELX_LOOPBACK &&
912 !(phba->sli3_options &
913 LPFC_SLI3_HBQ_ENABLED)) {
914 lpfc_sli_ringpostbuf_put(phba, pring,
915 dmabuf);
916 } else {
917 switch (cmd) {
918 case ELX_LOOPBACK_DATA:
919 diag_cmd_data_free(phba,
920 (struct lpfc_dmabufext *)
921 dmabuf);
922 break;
923 case ELX_LOOPBACK_XRI_SETUP:
924 if ((phba->sli_rev ==
925 LPFC_SLI_REV2) ||
926 (phba->sli3_options &
927 LPFC_SLI3_HBQ_ENABLED
928 )) {
929 lpfc_in_buf_free(phba,
930 dmabuf);
931 } else {
932 lpfc_post_buffer(phba,
933 pring,
936 break;
937 default:
938 if (!(phba->sli3_options &
939 LPFC_SLI3_HBQ_ENABLED))
940 lpfc_post_buffer(phba,
941 pring,
943 break;
949 spin_lock_irqsave(&phba->ct_ev_lock, flags);
950 if (phba->sli_rev == LPFC_SLI_REV4) {
951 evt_dat->immed_dat = phba->ctx_idx;
952 phba->ctx_idx = (phba->ctx_idx + 1) % 64;
953 /* Provide warning for over-run of the ct_ctx array */
954 if (phba->ct_ctx[evt_dat->immed_dat].flags &
955 UNSOL_VALID)
956 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
957 "2717 CT context array entry "
958 "[%d] over-run: oxid:x%x, "
959 "sid:x%x\n", phba->ctx_idx,
960 phba->ct_ctx[
961 evt_dat->immed_dat].oxid,
962 phba->ct_ctx[
963 evt_dat->immed_dat].SID);
964 phba->ct_ctx[evt_dat->immed_dat].rxid =
965 piocbq->iocb.ulpContext;
966 phba->ct_ctx[evt_dat->immed_dat].oxid =
967 piocbq->iocb.unsli3.rcvsli3.ox_id;
968 phba->ct_ctx[evt_dat->immed_dat].SID =
969 piocbq->iocb.un.rcvels.remoteID;
970 phba->ct_ctx[evt_dat->immed_dat].flags = UNSOL_VALID;
971 } else
972 evt_dat->immed_dat = piocbq->iocb.ulpContext;
974 evt_dat->type = FC_REG_CT_EVENT;
975 list_add(&evt_dat->node, &evt->events_to_see);
976 if (evt_req_id == SLI_CT_ELX_LOOPBACK) {
977 wake_up_interruptible(&evt->wq);
978 lpfc_bsg_event_unref(evt);
979 break;
982 list_move(evt->events_to_see.prev, &evt->events_to_get);
983 lpfc_bsg_event_unref(evt);
985 job = evt->set_job;
986 evt->set_job = NULL;
987 if (job) {
988 job->reply->reply_payload_rcv_len = size;
989 /* make error code available to userspace */
990 job->reply->result = 0;
991 job->dd_data = NULL;
992 /* complete the job back to userspace */
993 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
994 job->job_done(job);
995 spin_lock_irqsave(&phba->ct_ev_lock, flags);
998 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1000 error_ct_unsol_exit:
1001 if (!list_empty(&head))
1002 list_del(&head);
1003 if (evt_req_id == SLI_CT_ELX_LOOPBACK)
1004 return 0;
1005 return 1;
1009 * lpfc_bsg_hba_set_event - process a SET_EVENT bsg vendor command
1010 * @job: SET_EVENT fc_bsg_job
1012 static int
1013 lpfc_bsg_hba_set_event(struct fc_bsg_job *job)
1015 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
1016 struct lpfc_hba *phba = vport->phba;
1017 struct set_ct_event *event_req;
1018 struct lpfc_bsg_event *evt;
1019 int rc = 0;
1020 struct bsg_job_data *dd_data = NULL;
1021 uint32_t ev_mask;
1022 unsigned long flags;
1024 if (job->request_len <
1025 sizeof(struct fc_bsg_request) + sizeof(struct set_ct_event)) {
1026 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1027 "2612 Received SET_CT_EVENT below minimum "
1028 "size\n");
1029 rc = -EINVAL;
1030 goto job_error;
1033 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
1034 if (dd_data == NULL) {
1035 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1036 "2734 Failed allocation of dd_data\n");
1037 rc = -ENOMEM;
1038 goto job_error;
1041 event_req = (struct set_ct_event *)
1042 job->request->rqst_data.h_vendor.vendor_cmd;
1043 ev_mask = ((uint32_t)(unsigned long)event_req->type_mask &
1044 FC_REG_EVENT_MASK);
1045 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1046 list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
1047 if (evt->reg_id == event_req->ev_reg_id) {
1048 lpfc_bsg_event_ref(evt);
1049 evt->wait_time_stamp = jiffies;
1050 break;
1053 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1055 if (&evt->node == &phba->ct_ev_waiters) {
1056 /* no event waiting struct yet - first call */
1057 evt = lpfc_bsg_event_new(ev_mask, event_req->ev_reg_id,
1058 event_req->ev_req_id);
1059 if (!evt) {
1060 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1061 "2617 Failed allocation of event "
1062 "waiter\n");
1063 rc = -ENOMEM;
1064 goto job_error;
1067 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1068 list_add(&evt->node, &phba->ct_ev_waiters);
1069 lpfc_bsg_event_ref(evt);
1070 evt->wait_time_stamp = jiffies;
1071 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1074 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1075 evt->waiting = 1;
1076 dd_data->type = TYPE_EVT;
1077 dd_data->context_un.evt = evt;
1078 evt->set_job = job; /* for unsolicited command */
1079 job->dd_data = dd_data; /* for fc transport timeout callback*/
1080 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1081 return 0; /* call job done later */
1083 job_error:
1084 if (dd_data != NULL)
1085 kfree(dd_data);
1087 job->dd_data = NULL;
1088 return rc;
1092 * lpfc_bsg_hba_get_event - process a GET_EVENT bsg vendor command
1093 * @job: GET_EVENT fc_bsg_job
1095 static int
1096 lpfc_bsg_hba_get_event(struct fc_bsg_job *job)
1098 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
1099 struct lpfc_hba *phba = vport->phba;
1100 struct get_ct_event *event_req;
1101 struct get_ct_event_reply *event_reply;
1102 struct lpfc_bsg_event *evt;
1103 struct event_data *evt_dat = NULL;
1104 unsigned long flags;
1105 uint32_t rc = 0;
1107 if (job->request_len <
1108 sizeof(struct fc_bsg_request) + sizeof(struct get_ct_event)) {
1109 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1110 "2613 Received GET_CT_EVENT request below "
1111 "minimum size\n");
1112 rc = -EINVAL;
1113 goto job_error;
1116 event_req = (struct get_ct_event *)
1117 job->request->rqst_data.h_vendor.vendor_cmd;
1119 event_reply = (struct get_ct_event_reply *)
1120 job->reply->reply_data.vendor_reply.vendor_rsp;
1121 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1122 list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
1123 if (evt->reg_id == event_req->ev_reg_id) {
1124 if (list_empty(&evt->events_to_get))
1125 break;
1126 lpfc_bsg_event_ref(evt);
1127 evt->wait_time_stamp = jiffies;
1128 evt_dat = list_entry(evt->events_to_get.prev,
1129 struct event_data, node);
1130 list_del(&evt_dat->node);
1131 break;
1134 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1136 /* The app may continue to ask for event data until it gets
1137 * an error indicating that there isn't anymore
1139 if (evt_dat == NULL) {
1140 job->reply->reply_payload_rcv_len = 0;
1141 rc = -ENOENT;
1142 goto job_error;
1145 if (evt_dat->len > job->request_payload.payload_len) {
1146 evt_dat->len = job->request_payload.payload_len;
1147 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1148 "2618 Truncated event data at %d "
1149 "bytes\n",
1150 job->request_payload.payload_len);
1153 event_reply->type = evt_dat->type;
1154 event_reply->immed_data = evt_dat->immed_dat;
1155 if (evt_dat->len > 0)
1156 job->reply->reply_payload_rcv_len =
1157 sg_copy_from_buffer(job->request_payload.sg_list,
1158 job->request_payload.sg_cnt,
1159 evt_dat->data, evt_dat->len);
1160 else
1161 job->reply->reply_payload_rcv_len = 0;
1163 if (evt_dat) {
1164 kfree(evt_dat->data);
1165 kfree(evt_dat);
1168 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1169 lpfc_bsg_event_unref(evt);
1170 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1171 job->dd_data = NULL;
1172 job->reply->result = 0;
1173 job->job_done(job);
1174 return 0;
1176 job_error:
1177 job->dd_data = NULL;
1178 job->reply->result = rc;
1179 return rc;
1183 * lpfc_issue_ct_rsp_cmp - lpfc_issue_ct_rsp's completion handler
1184 * @phba: Pointer to HBA context object.
1185 * @cmdiocbq: Pointer to command iocb.
1186 * @rspiocbq: Pointer to response iocb.
1188 * This function is the completion handler for iocbs issued using
1189 * lpfc_issue_ct_rsp_cmp function. This function is called by the
1190 * ring event handler function without any lock held. This function
1191 * can be called from both worker thread context and interrupt
1192 * context. This function also can be called from other thread which
1193 * cleans up the SLI layer objects.
1194 * This function copy the contents of the response iocb to the
1195 * response iocb memory object provided by the caller of
1196 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
1197 * sleeps for the iocb completion.
1199 static void
1200 lpfc_issue_ct_rsp_cmp(struct lpfc_hba *phba,
1201 struct lpfc_iocbq *cmdiocbq,
1202 struct lpfc_iocbq *rspiocbq)
1204 struct bsg_job_data *dd_data;
1205 struct fc_bsg_job *job;
1206 IOCB_t *rsp;
1207 struct lpfc_dmabuf *bmp;
1208 struct lpfc_nodelist *ndlp;
1209 unsigned long flags;
1210 int rc = 0;
1212 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1213 dd_data = cmdiocbq->context2;
1214 /* normal completion and timeout crossed paths, already done */
1215 if (!dd_data) {
1216 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1217 return;
1220 job = dd_data->context_un.iocb.set_job;
1221 bmp = dd_data->context_un.iocb.bmp;
1222 rsp = &rspiocbq->iocb;
1223 ndlp = dd_data->context_un.iocb.ndlp;
1225 pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
1226 job->request_payload.sg_cnt, DMA_TO_DEVICE);
1228 if (rsp->ulpStatus) {
1229 if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
1230 switch (rsp->un.ulpWord[4] & 0xff) {
1231 case IOERR_SEQUENCE_TIMEOUT:
1232 rc = -ETIMEDOUT;
1233 break;
1234 case IOERR_INVALID_RPI:
1235 rc = -EFAULT;
1236 break;
1237 default:
1238 rc = -EACCES;
1239 break;
1241 } else
1242 rc = -EACCES;
1243 } else
1244 job->reply->reply_payload_rcv_len =
1245 rsp->un.genreq64.bdl.bdeSize;
1247 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
1248 lpfc_sli_release_iocbq(phba, cmdiocbq);
1249 lpfc_nlp_put(ndlp);
1250 kfree(bmp);
1251 kfree(dd_data);
1252 /* make error code available to userspace */
1253 job->reply->result = rc;
1254 job->dd_data = NULL;
1255 /* complete the job back to userspace */
1256 job->job_done(job);
1257 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1258 return;
1262 * lpfc_issue_ct_rsp - issue a ct response
1263 * @phba: Pointer to HBA context object.
1264 * @job: Pointer to the job object.
1265 * @tag: tag index value into the ports context exchange array.
1266 * @bmp: Pointer to a dma buffer descriptor.
1267 * @num_entry: Number of enties in the bde.
1269 static int
1270 lpfc_issue_ct_rsp(struct lpfc_hba *phba, struct fc_bsg_job *job, uint32_t tag,
1271 struct lpfc_dmabuf *bmp, int num_entry)
1273 IOCB_t *icmd;
1274 struct lpfc_iocbq *ctiocb = NULL;
1275 int rc = 0;
1276 struct lpfc_nodelist *ndlp = NULL;
1277 struct bsg_job_data *dd_data;
1278 uint32_t creg_val;
1280 /* allocate our bsg tracking structure */
1281 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
1282 if (!dd_data) {
1283 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1284 "2736 Failed allocation of dd_data\n");
1285 rc = -ENOMEM;
1286 goto no_dd_data;
1289 /* Allocate buffer for command iocb */
1290 ctiocb = lpfc_sli_get_iocbq(phba);
1291 if (!ctiocb) {
1292 rc = -ENOMEM;
1293 goto no_ctiocb;
1296 icmd = &ctiocb->iocb;
1297 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
1298 icmd->un.xseq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
1299 icmd->un.xseq64.bdl.addrLow = putPaddrLow(bmp->phys);
1300 icmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
1301 icmd->un.xseq64.bdl.bdeSize = (num_entry * sizeof(struct ulp_bde64));
1302 icmd->un.xseq64.w5.hcsw.Fctl = (LS | LA);
1303 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
1304 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_SOL_CTL;
1305 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
1307 /* Fill in rest of iocb */
1308 icmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;
1309 icmd->ulpBdeCount = 1;
1310 icmd->ulpLe = 1;
1311 icmd->ulpClass = CLASS3;
1312 if (phba->sli_rev == LPFC_SLI_REV4) {
1313 /* Do not issue unsol response if oxid not marked as valid */
1314 if (!(phba->ct_ctx[tag].flags & UNSOL_VALID)) {
1315 rc = IOCB_ERROR;
1316 goto issue_ct_rsp_exit;
1318 icmd->ulpContext = phba->ct_ctx[tag].rxid;
1319 icmd->unsli3.rcvsli3.ox_id = phba->ct_ctx[tag].oxid;
1320 ndlp = lpfc_findnode_did(phba->pport, phba->ct_ctx[tag].SID);
1321 if (!ndlp) {
1322 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
1323 "2721 ndlp null for oxid %x SID %x\n",
1324 icmd->ulpContext,
1325 phba->ct_ctx[tag].SID);
1326 rc = IOCB_ERROR;
1327 goto issue_ct_rsp_exit;
1330 /* Check if the ndlp is active */
1331 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
1332 rc = -IOCB_ERROR;
1333 goto issue_ct_rsp_exit;
1336 /* get a refernece count so the ndlp doesn't go away while
1337 * we respond
1339 if (!lpfc_nlp_get(ndlp)) {
1340 rc = -IOCB_ERROR;
1341 goto issue_ct_rsp_exit;
1344 icmd->un.ulpWord[3] =
1345 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
1347 /* The exchange is done, mark the entry as invalid */
1348 phba->ct_ctx[tag].flags &= ~UNSOL_VALID;
1349 } else
1350 icmd->ulpContext = (ushort) tag;
1352 icmd->ulpTimeout = phba->fc_ratov * 2;
1354 /* Xmit CT response on exchange <xid> */
1355 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
1356 "2722 Xmit CT response on exchange x%x Data: x%x x%x x%x\n",
1357 icmd->ulpContext, icmd->ulpIoTag, tag, phba->link_state);
1359 ctiocb->iocb_cmpl = NULL;
1360 ctiocb->iocb_flag |= LPFC_IO_LIBDFC;
1361 ctiocb->vport = phba->pport;
1362 ctiocb->context3 = bmp;
1364 ctiocb->iocb_cmpl = lpfc_issue_ct_rsp_cmp;
1365 ctiocb->context2 = dd_data;
1366 ctiocb->context1 = ndlp;
1367 dd_data->type = TYPE_IOCB;
1368 dd_data->context_un.iocb.cmdiocbq = ctiocb;
1369 dd_data->context_un.iocb.rspiocbq = NULL;
1370 dd_data->context_un.iocb.set_job = job;
1371 dd_data->context_un.iocb.bmp = bmp;
1372 dd_data->context_un.iocb.ndlp = ndlp;
1374 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
1375 if (lpfc_readl(phba->HCregaddr, &creg_val)) {
1376 rc = -IOCB_ERROR;
1377 goto issue_ct_rsp_exit;
1379 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
1380 writel(creg_val, phba->HCregaddr);
1381 readl(phba->HCregaddr); /* flush */
1384 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
1386 if (rc == IOCB_SUCCESS)
1387 return 0; /* done for now */
1389 issue_ct_rsp_exit:
1390 lpfc_sli_release_iocbq(phba, ctiocb);
1391 no_ctiocb:
1392 kfree(dd_data);
1393 no_dd_data:
1394 return rc;
1398 * lpfc_bsg_send_mgmt_rsp - process a SEND_MGMT_RESP bsg vendor command
1399 * @job: SEND_MGMT_RESP fc_bsg_job
1401 static int
1402 lpfc_bsg_send_mgmt_rsp(struct fc_bsg_job *job)
1404 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
1405 struct lpfc_hba *phba = vport->phba;
1406 struct send_mgmt_resp *mgmt_resp = (struct send_mgmt_resp *)
1407 job->request->rqst_data.h_vendor.vendor_cmd;
1408 struct ulp_bde64 *bpl;
1409 struct lpfc_dmabuf *bmp = NULL;
1410 struct scatterlist *sgel = NULL;
1411 int request_nseg;
1412 int numbde;
1413 dma_addr_t busaddr;
1414 uint32_t tag = mgmt_resp->tag;
1415 unsigned long reqbfrcnt =
1416 (unsigned long)job->request_payload.payload_len;
1417 int rc = 0;
1419 /* in case no data is transferred */
1420 job->reply->reply_payload_rcv_len = 0;
1422 if (!reqbfrcnt || (reqbfrcnt > (80 * BUF_SZ_4K))) {
1423 rc = -ERANGE;
1424 goto send_mgmt_rsp_exit;
1427 bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
1428 if (!bmp) {
1429 rc = -ENOMEM;
1430 goto send_mgmt_rsp_exit;
1433 bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
1434 if (!bmp->virt) {
1435 rc = -ENOMEM;
1436 goto send_mgmt_rsp_free_bmp;
1439 INIT_LIST_HEAD(&bmp->list);
1440 bpl = (struct ulp_bde64 *) bmp->virt;
1441 request_nseg = pci_map_sg(phba->pcidev, job->request_payload.sg_list,
1442 job->request_payload.sg_cnt, DMA_TO_DEVICE);
1443 for_each_sg(job->request_payload.sg_list, sgel, request_nseg, numbde) {
1444 busaddr = sg_dma_address(sgel);
1445 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1446 bpl->tus.f.bdeSize = sg_dma_len(sgel);
1447 bpl->tus.w = cpu_to_le32(bpl->tus.w);
1448 bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
1449 bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
1450 bpl++;
1453 rc = lpfc_issue_ct_rsp(phba, job, tag, bmp, request_nseg);
1455 if (rc == IOCB_SUCCESS)
1456 return 0; /* done for now */
1458 /* TBD need to handle a timeout */
1459 pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
1460 job->request_payload.sg_cnt, DMA_TO_DEVICE);
1461 rc = -EACCES;
1462 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
1464 send_mgmt_rsp_free_bmp:
1465 kfree(bmp);
1466 send_mgmt_rsp_exit:
1467 /* make error code available to userspace */
1468 job->reply->result = rc;
1469 job->dd_data = NULL;
1470 return rc;
1474 * lpfc_bsg_diag_mode_enter - process preparing into device diag loopback mode
1475 * @phba: Pointer to HBA context object.
1477 * This function is responsible for preparing driver for diag loopback
1478 * on device.
1480 static int
1481 lpfc_bsg_diag_mode_enter(struct lpfc_hba *phba)
1483 struct lpfc_vport **vports;
1484 struct Scsi_Host *shost;
1485 struct lpfc_sli *psli;
1486 struct lpfc_sli_ring *pring;
1487 int i = 0;
1489 psli = &phba->sli;
1490 if (!psli)
1491 return -ENODEV;
1493 pring = &psli->ring[LPFC_FCP_RING];
1494 if (!pring)
1495 return -ENODEV;
1497 if ((phba->link_state == LPFC_HBA_ERROR) ||
1498 (psli->sli_flag & LPFC_BLOCK_MGMT_IO) ||
1499 (!(psli->sli_flag & LPFC_SLI_ACTIVE)))
1500 return -EACCES;
1502 vports = lpfc_create_vport_work_array(phba);
1503 if (vports) {
1504 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
1505 shost = lpfc_shost_from_vport(vports[i]);
1506 scsi_block_requests(shost);
1508 lpfc_destroy_vport_work_array(phba, vports);
1509 } else {
1510 shost = lpfc_shost_from_vport(phba->pport);
1511 scsi_block_requests(shost);
1514 while (pring->txcmplq_cnt) {
1515 if (i++ > 500) /* wait up to 5 seconds */
1516 break;
1517 msleep(10);
1519 return 0;
1523 * lpfc_bsg_diag_mode_exit - exit process from device diag loopback mode
1524 * @phba: Pointer to HBA context object.
1526 * This function is responsible for driver exit processing of setting up
1527 * diag loopback mode on device.
1529 static void
1530 lpfc_bsg_diag_mode_exit(struct lpfc_hba *phba)
1532 struct Scsi_Host *shost;
1533 struct lpfc_vport **vports;
1534 int i;
1536 vports = lpfc_create_vport_work_array(phba);
1537 if (vports) {
1538 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
1539 shost = lpfc_shost_from_vport(vports[i]);
1540 scsi_unblock_requests(shost);
1542 lpfc_destroy_vport_work_array(phba, vports);
1543 } else {
1544 shost = lpfc_shost_from_vport(phba->pport);
1545 scsi_unblock_requests(shost);
1547 return;
1551 * lpfc_sli3_bsg_diag_loopback_mode - process an sli3 bsg vendor command
1552 * @phba: Pointer to HBA context object.
1553 * @job: LPFC_BSG_VENDOR_DIAG_MODE
1555 * This function is responsible for placing an sli3 port into diagnostic
1556 * loopback mode in order to perform a diagnostic loopback test.
1557 * All new scsi requests are blocked, a small delay is used to allow the
1558 * scsi requests to complete then the link is brought down. If the link is
1559 * is placed in loopback mode then scsi requests are again allowed
1560 * so the scsi mid-layer doesn't give up on the port.
1561 * All of this is done in-line.
1563 static int
1564 lpfc_sli3_bsg_diag_loopback_mode(struct lpfc_hba *phba, struct fc_bsg_job *job)
1566 struct diag_mode_set *loopback_mode;
1567 uint32_t link_flags;
1568 uint32_t timeout;
1569 LPFC_MBOXQ_t *pmboxq;
1570 int mbxstatus = MBX_SUCCESS;
1571 int i = 0;
1572 int rc = 0;
1574 /* no data to return just the return code */
1575 job->reply->reply_payload_rcv_len = 0;
1577 if (job->request_len < sizeof(struct fc_bsg_request) +
1578 sizeof(struct diag_mode_set)) {
1579 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1580 "2738 Received DIAG MODE request size:%d "
1581 "below the minimum size:%d\n",
1582 job->request_len,
1583 (int)(sizeof(struct fc_bsg_request) +
1584 sizeof(struct diag_mode_set)));
1585 rc = -EINVAL;
1586 goto job_error;
1589 rc = lpfc_bsg_diag_mode_enter(phba);
1590 if (rc)
1591 goto job_error;
1593 /* bring the link to diagnostic mode */
1594 loopback_mode = (struct diag_mode_set *)
1595 job->request->rqst_data.h_vendor.vendor_cmd;
1596 link_flags = loopback_mode->type;
1597 timeout = loopback_mode->timeout * 100;
1599 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1600 if (!pmboxq) {
1601 rc = -ENOMEM;
1602 goto loopback_mode_exit;
1604 memset((void *)pmboxq, 0, sizeof(LPFC_MBOXQ_t));
1605 pmboxq->u.mb.mbxCommand = MBX_DOWN_LINK;
1606 pmboxq->u.mb.mbxOwner = OWN_HOST;
1608 mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
1610 if ((mbxstatus == MBX_SUCCESS) && (pmboxq->u.mb.mbxStatus == 0)) {
1611 /* wait for link down before proceeding */
1612 i = 0;
1613 while (phba->link_state != LPFC_LINK_DOWN) {
1614 if (i++ > timeout) {
1615 rc = -ETIMEDOUT;
1616 goto loopback_mode_exit;
1619 msleep(10);
1622 memset((void *)pmboxq, 0, sizeof(LPFC_MBOXQ_t));
1623 if (link_flags == INTERNAL_LOOP_BACK)
1624 pmboxq->u.mb.un.varInitLnk.link_flags = FLAGS_LOCAL_LB;
1625 else
1626 pmboxq->u.mb.un.varInitLnk.link_flags =
1627 FLAGS_TOPOLOGY_MODE_LOOP;
1629 pmboxq->u.mb.mbxCommand = MBX_INIT_LINK;
1630 pmboxq->u.mb.mbxOwner = OWN_HOST;
1632 mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq,
1633 LPFC_MBOX_TMO);
1635 if ((mbxstatus != MBX_SUCCESS) || (pmboxq->u.mb.mbxStatus))
1636 rc = -ENODEV;
1637 else {
1638 phba->link_flag |= LS_LOOPBACK_MODE;
1639 /* wait for the link attention interrupt */
1640 msleep(100);
1642 i = 0;
1643 while (phba->link_state != LPFC_HBA_READY) {
1644 if (i++ > timeout) {
1645 rc = -ETIMEDOUT;
1646 break;
1649 msleep(10);
1653 } else
1654 rc = -ENODEV;
1656 loopback_mode_exit:
1657 lpfc_bsg_diag_mode_exit(phba);
1660 * Let SLI layer release mboxq if mbox command completed after timeout.
1662 if (mbxstatus != MBX_TIMEOUT)
1663 mempool_free(pmboxq, phba->mbox_mem_pool);
1665 job_error:
1666 /* make error code available to userspace */
1667 job->reply->result = rc;
1668 /* complete the job back to userspace if no error */
1669 if (rc == 0)
1670 job->job_done(job);
1671 return rc;
1675 * lpfc_sli4_bsg_set_link_diag_state - set sli4 link diag state
1676 * @phba: Pointer to HBA context object.
1677 * @diag: Flag for set link to diag or nomral operation state.
1679 * This function is responsible for issuing a sli4 mailbox command for setting
1680 * link to either diag state or normal operation state.
1682 static int
1683 lpfc_sli4_bsg_set_link_diag_state(struct lpfc_hba *phba, uint32_t diag)
1685 LPFC_MBOXQ_t *pmboxq;
1686 struct lpfc_mbx_set_link_diag_state *link_diag_state;
1687 uint32_t req_len, alloc_len;
1688 int mbxstatus = MBX_SUCCESS, rc;
1690 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1691 if (!pmboxq)
1692 return -ENOMEM;
1694 req_len = (sizeof(struct lpfc_mbx_set_link_diag_state) -
1695 sizeof(struct lpfc_sli4_cfg_mhdr));
1696 alloc_len = lpfc_sli4_config(phba, pmboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
1697 LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_STATE,
1698 req_len, LPFC_SLI4_MBX_EMBED);
1699 if (alloc_len != req_len) {
1700 rc = -ENOMEM;
1701 goto link_diag_state_set_out;
1703 link_diag_state = &pmboxq->u.mqe.un.link_diag_state;
1704 bf_set(lpfc_mbx_set_diag_state_link_num, &link_diag_state->u.req,
1705 phba->sli4_hba.link_state.number);
1706 bf_set(lpfc_mbx_set_diag_state_link_type, &link_diag_state->u.req,
1707 phba->sli4_hba.link_state.type);
1708 if (diag)
1709 bf_set(lpfc_mbx_set_diag_state_diag,
1710 &link_diag_state->u.req, 1);
1711 else
1712 bf_set(lpfc_mbx_set_diag_state_diag,
1713 &link_diag_state->u.req, 0);
1715 mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
1717 if ((mbxstatus == MBX_SUCCESS) && (pmboxq->u.mb.mbxStatus == 0))
1718 rc = 0;
1719 else
1720 rc = -ENODEV;
1722 link_diag_state_set_out:
1723 if (pmboxq && (mbxstatus != MBX_TIMEOUT))
1724 mempool_free(pmboxq, phba->mbox_mem_pool);
1726 return rc;
1730 * lpfc_sli4_bsg_diag_loopback_mode - process an sli4 bsg vendor command
1731 * @phba: Pointer to HBA context object.
1732 * @job: LPFC_BSG_VENDOR_DIAG_MODE
1734 * This function is responsible for placing an sli4 port into diagnostic
1735 * loopback mode in order to perform a diagnostic loopback test.
1737 static int
1738 lpfc_sli4_bsg_diag_loopback_mode(struct lpfc_hba *phba, struct fc_bsg_job *job)
1740 struct diag_mode_set *loopback_mode;
1741 uint32_t link_flags, timeout, req_len, alloc_len;
1742 struct lpfc_mbx_set_link_diag_loopback *link_diag_loopback;
1743 LPFC_MBOXQ_t *pmboxq = NULL;
1744 int mbxstatus = MBX_SUCCESS, i, rc = 0;
1746 /* no data to return just the return code */
1747 job->reply->reply_payload_rcv_len = 0;
1749 if (job->request_len < sizeof(struct fc_bsg_request) +
1750 sizeof(struct diag_mode_set)) {
1751 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1752 "3011 Received DIAG MODE request size:%d "
1753 "below the minimum size:%d\n",
1754 job->request_len,
1755 (int)(sizeof(struct fc_bsg_request) +
1756 sizeof(struct diag_mode_set)));
1757 rc = -EINVAL;
1758 goto job_error;
1761 rc = lpfc_bsg_diag_mode_enter(phba);
1762 if (rc)
1763 goto job_error;
1765 /* bring the link to diagnostic mode */
1766 loopback_mode = (struct diag_mode_set *)
1767 job->request->rqst_data.h_vendor.vendor_cmd;
1768 link_flags = loopback_mode->type;
1769 timeout = loopback_mode->timeout * 100;
1771 rc = lpfc_sli4_bsg_set_link_diag_state(phba, 1);
1772 if (rc)
1773 goto loopback_mode_exit;
1775 /* wait for link down before proceeding */
1776 i = 0;
1777 while (phba->link_state != LPFC_LINK_DOWN) {
1778 if (i++ > timeout) {
1779 rc = -ETIMEDOUT;
1780 goto loopback_mode_exit;
1782 msleep(10);
1784 /* set up loopback mode */
1785 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1786 if (!pmboxq) {
1787 rc = -ENOMEM;
1788 goto loopback_mode_exit;
1790 req_len = (sizeof(struct lpfc_mbx_set_link_diag_loopback) -
1791 sizeof(struct lpfc_sli4_cfg_mhdr));
1792 alloc_len = lpfc_sli4_config(phba, pmboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
1793 LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_LOOPBACK,
1794 req_len, LPFC_SLI4_MBX_EMBED);
1795 if (alloc_len != req_len) {
1796 rc = -ENOMEM;
1797 goto loopback_mode_exit;
1799 link_diag_loopback = &pmboxq->u.mqe.un.link_diag_loopback;
1800 bf_set(lpfc_mbx_set_diag_state_link_num,
1801 &link_diag_loopback->u.req, phba->sli4_hba.link_state.number);
1802 bf_set(lpfc_mbx_set_diag_state_link_type,
1803 &link_diag_loopback->u.req, phba->sli4_hba.link_state.type);
1804 if (link_flags == INTERNAL_LOOP_BACK)
1805 bf_set(lpfc_mbx_set_diag_lpbk_type,
1806 &link_diag_loopback->u.req,
1807 LPFC_DIAG_LOOPBACK_TYPE_INTERNAL);
1808 else
1809 bf_set(lpfc_mbx_set_diag_lpbk_type,
1810 &link_diag_loopback->u.req,
1811 LPFC_DIAG_LOOPBACK_TYPE_EXTERNAL);
1813 mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
1814 if ((mbxstatus != MBX_SUCCESS) || (pmboxq->u.mb.mbxStatus))
1815 rc = -ENODEV;
1816 else {
1817 phba->link_flag |= LS_LOOPBACK_MODE;
1818 /* wait for the link attention interrupt */
1819 msleep(100);
1820 i = 0;
1821 while (phba->link_state != LPFC_HBA_READY) {
1822 if (i++ > timeout) {
1823 rc = -ETIMEDOUT;
1824 break;
1826 msleep(10);
1830 loopback_mode_exit:
1831 lpfc_bsg_diag_mode_exit(phba);
1834 * Let SLI layer release mboxq if mbox command completed after timeout.
1836 if (pmboxq && (mbxstatus != MBX_TIMEOUT))
1837 mempool_free(pmboxq, phba->mbox_mem_pool);
1839 job_error:
1840 /* make error code available to userspace */
1841 job->reply->result = rc;
1842 /* complete the job back to userspace if no error */
1843 if (rc == 0)
1844 job->job_done(job);
1845 return rc;
1849 * lpfc_bsg_diag_loopback_mode - bsg vendor command for diag loopback mode
1850 * @job: LPFC_BSG_VENDOR_DIAG_MODE
1852 * This function is responsible for responding to check and dispatch bsg diag
1853 * command from the user to proper driver action routines.
1855 static int
1856 lpfc_bsg_diag_loopback_mode(struct fc_bsg_job *job)
1858 struct Scsi_Host *shost;
1859 struct lpfc_vport *vport;
1860 struct lpfc_hba *phba;
1861 int rc;
1863 shost = job->shost;
1864 if (!shost)
1865 return -ENODEV;
1866 vport = (struct lpfc_vport *)job->shost->hostdata;
1867 if (!vport)
1868 return -ENODEV;
1869 phba = vport->phba;
1870 if (!phba)
1871 return -ENODEV;
1873 if (phba->sli_rev < LPFC_SLI_REV4)
1874 rc = lpfc_sli3_bsg_diag_loopback_mode(phba, job);
1875 else if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
1876 LPFC_SLI_INTF_IF_TYPE_2)
1877 rc = lpfc_sli4_bsg_diag_loopback_mode(phba, job);
1878 else
1879 rc = -ENODEV;
1881 return rc;
1886 * lpfc_sli4_bsg_diag_mode_end - sli4 bsg vendor command for ending diag mode
1887 * @job: LPFC_BSG_VENDOR_DIAG_MODE_END
1889 * This function is responsible for responding to check and dispatch bsg diag
1890 * command from the user to proper driver action routines.
1892 static int
1893 lpfc_sli4_bsg_diag_mode_end(struct fc_bsg_job *job)
1895 struct Scsi_Host *shost;
1896 struct lpfc_vport *vport;
1897 struct lpfc_hba *phba;
1898 int rc;
1900 shost = job->shost;
1901 if (!shost)
1902 return -ENODEV;
1903 vport = (struct lpfc_vport *)job->shost->hostdata;
1904 if (!vport)
1905 return -ENODEV;
1906 phba = vport->phba;
1907 if (!phba)
1908 return -ENODEV;
1910 if (phba->sli_rev < LPFC_SLI_REV4)
1911 return -ENODEV;
1912 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
1913 LPFC_SLI_INTF_IF_TYPE_2)
1914 return -ENODEV;
1916 rc = lpfc_sli4_bsg_set_link_diag_state(phba, 0);
1918 if (!rc)
1919 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
1921 return rc;
1925 * lpfc_sli4_bsg_link_diag_test - sli4 bsg vendor command for diag link test
1926 * @job: LPFC_BSG_VENDOR_DIAG_LINK_TEST
1928 * This function is to perform SLI4 diag link test request from the user
1929 * applicaiton.
1931 static int
1932 lpfc_sli4_bsg_link_diag_test(struct fc_bsg_job *job)
1934 struct Scsi_Host *shost;
1935 struct lpfc_vport *vport;
1936 struct lpfc_hba *phba;
1937 LPFC_MBOXQ_t *pmboxq;
1938 struct sli4_link_diag *link_diag_test_cmd;
1939 uint32_t req_len, alloc_len;
1940 uint32_t timeout;
1941 struct lpfc_mbx_run_link_diag_test *run_link_diag_test;
1942 union lpfc_sli4_cfg_shdr *shdr;
1943 uint32_t shdr_status, shdr_add_status;
1944 struct diag_status *diag_status_reply;
1945 int mbxstatus, rc = 0;
1947 shost = job->shost;
1948 if (!shost) {
1949 rc = -ENODEV;
1950 goto job_error;
1952 vport = (struct lpfc_vport *)job->shost->hostdata;
1953 if (!vport) {
1954 rc = -ENODEV;
1955 goto job_error;
1957 phba = vport->phba;
1958 if (!phba) {
1959 rc = -ENODEV;
1960 goto job_error;
1963 if (phba->sli_rev < LPFC_SLI_REV4) {
1964 rc = -ENODEV;
1965 goto job_error;
1967 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
1968 LPFC_SLI_INTF_IF_TYPE_2) {
1969 rc = -ENODEV;
1970 goto job_error;
1973 if (job->request_len < sizeof(struct fc_bsg_request) +
1974 sizeof(struct sli4_link_diag)) {
1975 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1976 "3013 Received LINK DIAG TEST request "
1977 " size:%d below the minimum size:%d\n",
1978 job->request_len,
1979 (int)(sizeof(struct fc_bsg_request) +
1980 sizeof(struct sli4_link_diag)));
1981 rc = -EINVAL;
1982 goto job_error;
1985 rc = lpfc_bsg_diag_mode_enter(phba);
1986 if (rc)
1987 goto job_error;
1989 link_diag_test_cmd = (struct sli4_link_diag *)
1990 job->request->rqst_data.h_vendor.vendor_cmd;
1991 timeout = link_diag_test_cmd->timeout * 100;
1993 rc = lpfc_sli4_bsg_set_link_diag_state(phba, 1);
1995 if (rc)
1996 goto job_error;
1998 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1999 if (!pmboxq) {
2000 rc = -ENOMEM;
2001 goto link_diag_test_exit;
2004 req_len = (sizeof(struct lpfc_mbx_set_link_diag_state) -
2005 sizeof(struct lpfc_sli4_cfg_mhdr));
2006 alloc_len = lpfc_sli4_config(phba, pmboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
2007 LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_STATE,
2008 req_len, LPFC_SLI4_MBX_EMBED);
2009 if (alloc_len != req_len) {
2010 rc = -ENOMEM;
2011 goto link_diag_test_exit;
2013 run_link_diag_test = &pmboxq->u.mqe.un.link_diag_test;
2014 bf_set(lpfc_mbx_run_diag_test_link_num, &run_link_diag_test->u.req,
2015 phba->sli4_hba.link_state.number);
2016 bf_set(lpfc_mbx_run_diag_test_link_type, &run_link_diag_test->u.req,
2017 phba->sli4_hba.link_state.type);
2018 bf_set(lpfc_mbx_run_diag_test_test_id, &run_link_diag_test->u.req,
2019 link_diag_test_cmd->test_id);
2020 bf_set(lpfc_mbx_run_diag_test_loops, &run_link_diag_test->u.req,
2021 link_diag_test_cmd->loops);
2022 bf_set(lpfc_mbx_run_diag_test_test_ver, &run_link_diag_test->u.req,
2023 link_diag_test_cmd->test_version);
2024 bf_set(lpfc_mbx_run_diag_test_err_act, &run_link_diag_test->u.req,
2025 link_diag_test_cmd->error_action);
2027 mbxstatus = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
2029 shdr = (union lpfc_sli4_cfg_shdr *)
2030 &pmboxq->u.mqe.un.sli4_config.header.cfg_shdr;
2031 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
2032 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
2033 if (shdr_status || shdr_add_status || mbxstatus) {
2034 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
2035 "3010 Run link diag test mailbox failed with "
2036 "mbx_status x%x status x%x, add_status x%x\n",
2037 mbxstatus, shdr_status, shdr_add_status);
2040 diag_status_reply = (struct diag_status *)
2041 job->reply->reply_data.vendor_reply.vendor_rsp;
2043 if (job->reply_len <
2044 sizeof(struct fc_bsg_request) + sizeof(struct diag_status)) {
2045 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2046 "3012 Received Run link diag test reply "
2047 "below minimum size (%d): reply_len:%d\n",
2048 (int)(sizeof(struct fc_bsg_request) +
2049 sizeof(struct diag_status)),
2050 job->reply_len);
2051 rc = -EINVAL;
2052 goto job_error;
2055 diag_status_reply->mbox_status = mbxstatus;
2056 diag_status_reply->shdr_status = shdr_status;
2057 diag_status_reply->shdr_add_status = shdr_add_status;
2059 link_diag_test_exit:
2060 rc = lpfc_sli4_bsg_set_link_diag_state(phba, 0);
2062 if (pmboxq)
2063 mempool_free(pmboxq, phba->mbox_mem_pool);
2065 lpfc_bsg_diag_mode_exit(phba);
2067 job_error:
2068 /* make error code available to userspace */
2069 job->reply->result = rc;
2070 /* complete the job back to userspace if no error */
2071 if (rc == 0)
2072 job->job_done(job);
2073 return rc;
2077 * lpfcdiag_loop_self_reg - obtains a remote port login id
2078 * @phba: Pointer to HBA context object
2079 * @rpi: Pointer to a remote port login id
2081 * This function obtains a remote port login id so the diag loopback test
2082 * can send and receive its own unsolicited CT command.
2084 static int lpfcdiag_loop_self_reg(struct lpfc_hba *phba, uint16_t *rpi)
2086 LPFC_MBOXQ_t *mbox;
2087 struct lpfc_dmabuf *dmabuff;
2088 int status;
2090 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2091 if (!mbox)
2092 return -ENOMEM;
2094 if (phba->sli_rev == LPFC_SLI_REV4)
2095 *rpi = lpfc_sli4_alloc_rpi(phba);
2096 status = lpfc_reg_rpi(phba, 0, phba->pport->fc_myDID,
2097 (uint8_t *)&phba->pport->fc_sparam, mbox, *rpi);
2098 if (status) {
2099 mempool_free(mbox, phba->mbox_mem_pool);
2100 if (phba->sli_rev == LPFC_SLI_REV4)
2101 lpfc_sli4_free_rpi(phba, *rpi);
2102 return -ENOMEM;
2105 dmabuff = (struct lpfc_dmabuf *) mbox->context1;
2106 mbox->context1 = NULL;
2107 mbox->context2 = NULL;
2108 status = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
2110 if ((status != MBX_SUCCESS) || (mbox->u.mb.mbxStatus)) {
2111 lpfc_mbuf_free(phba, dmabuff->virt, dmabuff->phys);
2112 kfree(dmabuff);
2113 if (status != MBX_TIMEOUT)
2114 mempool_free(mbox, phba->mbox_mem_pool);
2115 if (phba->sli_rev == LPFC_SLI_REV4)
2116 lpfc_sli4_free_rpi(phba, *rpi);
2117 return -ENODEV;
2120 *rpi = mbox->u.mb.un.varWords[0];
2122 lpfc_mbuf_free(phba, dmabuff->virt, dmabuff->phys);
2123 kfree(dmabuff);
2124 mempool_free(mbox, phba->mbox_mem_pool);
2125 return 0;
2129 * lpfcdiag_loop_self_unreg - unregs from the rpi
2130 * @phba: Pointer to HBA context object
2131 * @rpi: Remote port login id
2133 * This function unregisters the rpi obtained in lpfcdiag_loop_self_reg
2135 static int lpfcdiag_loop_self_unreg(struct lpfc_hba *phba, uint16_t rpi)
2137 LPFC_MBOXQ_t *mbox;
2138 int status;
2140 /* Allocate mboxq structure */
2141 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2142 if (mbox == NULL)
2143 return -ENOMEM;
2145 lpfc_unreg_login(phba, 0, rpi, mbox);
2146 status = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
2148 if ((status != MBX_SUCCESS) || (mbox->u.mb.mbxStatus)) {
2149 if (status != MBX_TIMEOUT)
2150 mempool_free(mbox, phba->mbox_mem_pool);
2151 return -EIO;
2153 mempool_free(mbox, phba->mbox_mem_pool);
2154 if (phba->sli_rev == LPFC_SLI_REV4)
2155 lpfc_sli4_free_rpi(phba, rpi);
2156 return 0;
2160 * lpfcdiag_loop_get_xri - obtains the transmit and receive ids
2161 * @phba: Pointer to HBA context object
2162 * @rpi: Remote port login id
2163 * @txxri: Pointer to transmit exchange id
2164 * @rxxri: Pointer to response exchabge id
2166 * This function obtains the transmit and receive ids required to send
2167 * an unsolicited ct command with a payload. A special lpfc FsType and CmdRsp
2168 * flags are used to the unsolicted response handler is able to process
2169 * the ct command sent on the same port.
2171 static int lpfcdiag_loop_get_xri(struct lpfc_hba *phba, uint16_t rpi,
2172 uint16_t *txxri, uint16_t * rxxri)
2174 struct lpfc_bsg_event *evt;
2175 struct lpfc_iocbq *cmdiocbq, *rspiocbq;
2176 IOCB_t *cmd, *rsp;
2177 struct lpfc_dmabuf *dmabuf;
2178 struct ulp_bde64 *bpl = NULL;
2179 struct lpfc_sli_ct_request *ctreq = NULL;
2180 int ret_val = 0;
2181 int time_left;
2182 int iocb_stat = 0;
2183 unsigned long flags;
2185 *txxri = 0;
2186 *rxxri = 0;
2187 evt = lpfc_bsg_event_new(FC_REG_CT_EVENT, current->pid,
2188 SLI_CT_ELX_LOOPBACK);
2189 if (!evt)
2190 return -ENOMEM;
2192 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2193 list_add(&evt->node, &phba->ct_ev_waiters);
2194 lpfc_bsg_event_ref(evt);
2195 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2197 cmdiocbq = lpfc_sli_get_iocbq(phba);
2198 rspiocbq = lpfc_sli_get_iocbq(phba);
2200 dmabuf = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2201 if (dmabuf) {
2202 dmabuf->virt = lpfc_mbuf_alloc(phba, 0, &dmabuf->phys);
2203 if (dmabuf->virt) {
2204 INIT_LIST_HEAD(&dmabuf->list);
2205 bpl = (struct ulp_bde64 *) dmabuf->virt;
2206 memset(bpl, 0, sizeof(*bpl));
2207 ctreq = (struct lpfc_sli_ct_request *)(bpl + 1);
2208 bpl->addrHigh =
2209 le32_to_cpu(putPaddrHigh(dmabuf->phys +
2210 sizeof(*bpl)));
2211 bpl->addrLow =
2212 le32_to_cpu(putPaddrLow(dmabuf->phys +
2213 sizeof(*bpl)));
2214 bpl->tus.f.bdeFlags = 0;
2215 bpl->tus.f.bdeSize = ELX_LOOPBACK_HEADER_SZ;
2216 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2220 if (cmdiocbq == NULL || rspiocbq == NULL ||
2221 dmabuf == NULL || bpl == NULL || ctreq == NULL ||
2222 dmabuf->virt == NULL) {
2223 ret_val = -ENOMEM;
2224 goto err_get_xri_exit;
2227 cmd = &cmdiocbq->iocb;
2228 rsp = &rspiocbq->iocb;
2230 memset(ctreq, 0, ELX_LOOPBACK_HEADER_SZ);
2232 ctreq->RevisionId.bits.Revision = SLI_CT_REVISION;
2233 ctreq->RevisionId.bits.InId = 0;
2234 ctreq->FsType = SLI_CT_ELX_LOOPBACK;
2235 ctreq->FsSubType = 0;
2236 ctreq->CommandResponse.bits.CmdRsp = ELX_LOOPBACK_XRI_SETUP;
2237 ctreq->CommandResponse.bits.Size = 0;
2240 cmd->un.xseq64.bdl.addrHigh = putPaddrHigh(dmabuf->phys);
2241 cmd->un.xseq64.bdl.addrLow = putPaddrLow(dmabuf->phys);
2242 cmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
2243 cmd->un.xseq64.bdl.bdeSize = sizeof(*bpl);
2245 cmd->un.xseq64.w5.hcsw.Fctl = LA;
2246 cmd->un.xseq64.w5.hcsw.Dfctl = 0;
2247 cmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
2248 cmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
2250 cmd->ulpCommand = CMD_XMIT_SEQUENCE64_CR;
2251 cmd->ulpBdeCount = 1;
2252 cmd->ulpLe = 1;
2253 cmd->ulpClass = CLASS3;
2254 cmd->ulpContext = rpi;
2256 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
2257 cmdiocbq->vport = phba->pport;
2259 iocb_stat = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq,
2260 rspiocbq,
2261 (phba->fc_ratov * 2)
2262 + LPFC_DRVR_TIMEOUT);
2263 if (iocb_stat) {
2264 ret_val = -EIO;
2265 goto err_get_xri_exit;
2267 *txxri = rsp->ulpContext;
2269 evt->waiting = 1;
2270 evt->wait_time_stamp = jiffies;
2271 time_left = wait_event_interruptible_timeout(
2272 evt->wq, !list_empty(&evt->events_to_see),
2273 ((phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT) * HZ);
2274 if (list_empty(&evt->events_to_see))
2275 ret_val = (time_left) ? -EINTR : -ETIMEDOUT;
2276 else {
2277 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2278 list_move(evt->events_to_see.prev, &evt->events_to_get);
2279 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2280 *rxxri = (list_entry(evt->events_to_get.prev,
2281 typeof(struct event_data),
2282 node))->immed_dat;
2284 evt->waiting = 0;
2286 err_get_xri_exit:
2287 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2288 lpfc_bsg_event_unref(evt); /* release ref */
2289 lpfc_bsg_event_unref(evt); /* delete */
2290 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2292 if (dmabuf) {
2293 if (dmabuf->virt)
2294 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
2295 kfree(dmabuf);
2298 if (cmdiocbq && (iocb_stat != IOCB_TIMEDOUT))
2299 lpfc_sli_release_iocbq(phba, cmdiocbq);
2300 if (rspiocbq)
2301 lpfc_sli_release_iocbq(phba, rspiocbq);
2302 return ret_val;
2306 * lpfc_bsg_dma_page_alloc - allocate a bsg mbox page sized dma buffers
2307 * @phba: Pointer to HBA context object
2309 * This function allocates BSG_MBOX_SIZE (4KB) page size dma buffer and.
2310 * retruns the pointer to the buffer.
2312 static struct lpfc_dmabuf *
2313 lpfc_bsg_dma_page_alloc(struct lpfc_hba *phba)
2315 struct lpfc_dmabuf *dmabuf;
2316 struct pci_dev *pcidev = phba->pcidev;
2318 /* allocate dma buffer struct */
2319 dmabuf = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2320 if (!dmabuf)
2321 return NULL;
2323 INIT_LIST_HEAD(&dmabuf->list);
2325 /* now, allocate dma buffer */
2326 dmabuf->virt = dma_alloc_coherent(&pcidev->dev, BSG_MBOX_SIZE,
2327 &(dmabuf->phys), GFP_KERNEL);
2329 if (!dmabuf->virt) {
2330 kfree(dmabuf);
2331 return NULL;
2333 memset((uint8_t *)dmabuf->virt, 0, BSG_MBOX_SIZE);
2335 return dmabuf;
2339 * lpfc_bsg_dma_page_free - free a bsg mbox page sized dma buffer
2340 * @phba: Pointer to HBA context object.
2341 * @dmabuf: Pointer to the bsg mbox page sized dma buffer descriptor.
2343 * This routine just simply frees a dma buffer and its associated buffer
2344 * descriptor referred by @dmabuf.
2346 static void
2347 lpfc_bsg_dma_page_free(struct lpfc_hba *phba, struct lpfc_dmabuf *dmabuf)
2349 struct pci_dev *pcidev = phba->pcidev;
2351 if (!dmabuf)
2352 return;
2354 if (dmabuf->virt)
2355 dma_free_coherent(&pcidev->dev, BSG_MBOX_SIZE,
2356 dmabuf->virt, dmabuf->phys);
2357 kfree(dmabuf);
2358 return;
2362 * lpfc_bsg_dma_page_list_free - free a list of bsg mbox page sized dma buffers
2363 * @phba: Pointer to HBA context object.
2364 * @dmabuf_list: Pointer to a list of bsg mbox page sized dma buffer descs.
2366 * This routine just simply frees all dma buffers and their associated buffer
2367 * descriptors referred by @dmabuf_list.
2369 static void
2370 lpfc_bsg_dma_page_list_free(struct lpfc_hba *phba,
2371 struct list_head *dmabuf_list)
2373 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
2375 if (list_empty(dmabuf_list))
2376 return;
2378 list_for_each_entry_safe(dmabuf, next_dmabuf, dmabuf_list, list) {
2379 list_del_init(&dmabuf->list);
2380 lpfc_bsg_dma_page_free(phba, dmabuf);
2382 return;
2386 * diag_cmd_data_alloc - fills in a bde struct with dma buffers
2387 * @phba: Pointer to HBA context object
2388 * @bpl: Pointer to 64 bit bde structure
2389 * @size: Number of bytes to process
2390 * @nocopydata: Flag to copy user data into the allocated buffer
2392 * This function allocates page size buffers and populates an lpfc_dmabufext.
2393 * If allowed the user data pointed to with indataptr is copied into the kernel
2394 * memory. The chained list of page size buffers is returned.
2396 static struct lpfc_dmabufext *
2397 diag_cmd_data_alloc(struct lpfc_hba *phba,
2398 struct ulp_bde64 *bpl, uint32_t size,
2399 int nocopydata)
2401 struct lpfc_dmabufext *mlist = NULL;
2402 struct lpfc_dmabufext *dmp;
2403 int cnt, offset = 0, i = 0;
2404 struct pci_dev *pcidev;
2406 pcidev = phba->pcidev;
2408 while (size) {
2409 /* We get chunks of 4K */
2410 if (size > BUF_SZ_4K)
2411 cnt = BUF_SZ_4K;
2412 else
2413 cnt = size;
2415 /* allocate struct lpfc_dmabufext buffer header */
2416 dmp = kmalloc(sizeof(struct lpfc_dmabufext), GFP_KERNEL);
2417 if (!dmp)
2418 goto out;
2420 INIT_LIST_HEAD(&dmp->dma.list);
2422 /* Queue it to a linked list */
2423 if (mlist)
2424 list_add_tail(&dmp->dma.list, &mlist->dma.list);
2425 else
2426 mlist = dmp;
2428 /* allocate buffer */
2429 dmp->dma.virt = dma_alloc_coherent(&pcidev->dev,
2430 cnt,
2431 &(dmp->dma.phys),
2432 GFP_KERNEL);
2434 if (!dmp->dma.virt)
2435 goto out;
2437 dmp->size = cnt;
2439 if (nocopydata) {
2440 bpl->tus.f.bdeFlags = 0;
2441 pci_dma_sync_single_for_device(phba->pcidev,
2442 dmp->dma.phys, LPFC_BPL_SIZE, PCI_DMA_TODEVICE);
2444 } else {
2445 memset((uint8_t *)dmp->dma.virt, 0, cnt);
2446 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2449 /* build buffer ptr list for IOCB */
2450 bpl->addrLow = le32_to_cpu(putPaddrLow(dmp->dma.phys));
2451 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dmp->dma.phys));
2452 bpl->tus.f.bdeSize = (ushort) cnt;
2453 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2454 bpl++;
2456 i++;
2457 offset += cnt;
2458 size -= cnt;
2461 mlist->flag = i;
2462 return mlist;
2463 out:
2464 diag_cmd_data_free(phba, mlist);
2465 return NULL;
2469 * lpfcdiag_loop_post_rxbufs - post the receive buffers for an unsol CT cmd
2470 * @phba: Pointer to HBA context object
2471 * @rxxri: Receive exchange id
2472 * @len: Number of data bytes
2474 * This function allocates and posts a data buffer of sufficient size to receive
2475 * an unsolicted CT command.
2477 static int lpfcdiag_loop_post_rxbufs(struct lpfc_hba *phba, uint16_t rxxri,
2478 size_t len)
2480 struct lpfc_sli *psli = &phba->sli;
2481 struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
2482 struct lpfc_iocbq *cmdiocbq;
2483 IOCB_t *cmd = NULL;
2484 struct list_head head, *curr, *next;
2485 struct lpfc_dmabuf *rxbmp;
2486 struct lpfc_dmabuf *dmp;
2487 struct lpfc_dmabuf *mp[2] = {NULL, NULL};
2488 struct ulp_bde64 *rxbpl = NULL;
2489 uint32_t num_bde;
2490 struct lpfc_dmabufext *rxbuffer = NULL;
2491 int ret_val = 0;
2492 int iocb_stat;
2493 int i = 0;
2495 cmdiocbq = lpfc_sli_get_iocbq(phba);
2496 rxbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2497 if (rxbmp != NULL) {
2498 rxbmp->virt = lpfc_mbuf_alloc(phba, 0, &rxbmp->phys);
2499 if (rxbmp->virt) {
2500 INIT_LIST_HEAD(&rxbmp->list);
2501 rxbpl = (struct ulp_bde64 *) rxbmp->virt;
2502 rxbuffer = diag_cmd_data_alloc(phba, rxbpl, len, 0);
2506 if (!cmdiocbq || !rxbmp || !rxbpl || !rxbuffer) {
2507 ret_val = -ENOMEM;
2508 goto err_post_rxbufs_exit;
2511 /* Queue buffers for the receive exchange */
2512 num_bde = (uint32_t)rxbuffer->flag;
2513 dmp = &rxbuffer->dma;
2515 cmd = &cmdiocbq->iocb;
2516 i = 0;
2518 INIT_LIST_HEAD(&head);
2519 list_add_tail(&head, &dmp->list);
2520 list_for_each_safe(curr, next, &head) {
2521 mp[i] = list_entry(curr, struct lpfc_dmabuf, list);
2522 list_del(curr);
2524 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
2525 mp[i]->buffer_tag = lpfc_sli_get_buffer_tag(phba);
2526 cmd->un.quexri64cx.buff.bde.addrHigh =
2527 putPaddrHigh(mp[i]->phys);
2528 cmd->un.quexri64cx.buff.bde.addrLow =
2529 putPaddrLow(mp[i]->phys);
2530 cmd->un.quexri64cx.buff.bde.tus.f.bdeSize =
2531 ((struct lpfc_dmabufext *)mp[i])->size;
2532 cmd->un.quexri64cx.buff.buffer_tag = mp[i]->buffer_tag;
2533 cmd->ulpCommand = CMD_QUE_XRI64_CX;
2534 cmd->ulpPU = 0;
2535 cmd->ulpLe = 1;
2536 cmd->ulpBdeCount = 1;
2537 cmd->unsli3.que_xri64cx_ext_words.ebde_count = 0;
2539 } else {
2540 cmd->un.cont64[i].addrHigh = putPaddrHigh(mp[i]->phys);
2541 cmd->un.cont64[i].addrLow = putPaddrLow(mp[i]->phys);
2542 cmd->un.cont64[i].tus.f.bdeSize =
2543 ((struct lpfc_dmabufext *)mp[i])->size;
2544 cmd->ulpBdeCount = ++i;
2546 if ((--num_bde > 0) && (i < 2))
2547 continue;
2549 cmd->ulpCommand = CMD_QUE_XRI_BUF64_CX;
2550 cmd->ulpLe = 1;
2553 cmd->ulpClass = CLASS3;
2554 cmd->ulpContext = rxxri;
2556 iocb_stat = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq,
2558 if (iocb_stat == IOCB_ERROR) {
2559 diag_cmd_data_free(phba,
2560 (struct lpfc_dmabufext *)mp[0]);
2561 if (mp[1])
2562 diag_cmd_data_free(phba,
2563 (struct lpfc_dmabufext *)mp[1]);
2564 dmp = list_entry(next, struct lpfc_dmabuf, list);
2565 ret_val = -EIO;
2566 goto err_post_rxbufs_exit;
2569 lpfc_sli_ringpostbuf_put(phba, pring, mp[0]);
2570 if (mp[1]) {
2571 lpfc_sli_ringpostbuf_put(phba, pring, mp[1]);
2572 mp[1] = NULL;
2575 /* The iocb was freed by lpfc_sli_issue_iocb */
2576 cmdiocbq = lpfc_sli_get_iocbq(phba);
2577 if (!cmdiocbq) {
2578 dmp = list_entry(next, struct lpfc_dmabuf, list);
2579 ret_val = -EIO;
2580 goto err_post_rxbufs_exit;
2583 cmd = &cmdiocbq->iocb;
2584 i = 0;
2586 list_del(&head);
2588 err_post_rxbufs_exit:
2590 if (rxbmp) {
2591 if (rxbmp->virt)
2592 lpfc_mbuf_free(phba, rxbmp->virt, rxbmp->phys);
2593 kfree(rxbmp);
2596 if (cmdiocbq)
2597 lpfc_sli_release_iocbq(phba, cmdiocbq);
2598 return ret_val;
2602 * lpfc_bsg_diag_loopback_run - run loopback on a port by issue ct cmd to itself
2603 * @job: LPFC_BSG_VENDOR_DIAG_TEST fc_bsg_job
2605 * This function receives a user data buffer to be transmitted and received on
2606 * the same port, the link must be up and in loopback mode prior
2607 * to being called.
2608 * 1. A kernel buffer is allocated to copy the user data into.
2609 * 2. The port registers with "itself".
2610 * 3. The transmit and receive exchange ids are obtained.
2611 * 4. The receive exchange id is posted.
2612 * 5. A new els loopback event is created.
2613 * 6. The command and response iocbs are allocated.
2614 * 7. The cmd iocb FsType is set to elx loopback and the CmdRsp to looppback.
2616 * This function is meant to be called n times while the port is in loopback
2617 * so it is the apps responsibility to issue a reset to take the port out
2618 * of loopback mode.
2620 static int
2621 lpfc_bsg_diag_loopback_run(struct fc_bsg_job *job)
2623 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
2624 struct lpfc_hba *phba = vport->phba;
2625 struct diag_mode_test *diag_mode;
2626 struct lpfc_bsg_event *evt;
2627 struct event_data *evdat;
2628 struct lpfc_sli *psli = &phba->sli;
2629 uint32_t size;
2630 uint32_t full_size;
2631 size_t segment_len = 0, segment_offset = 0, current_offset = 0;
2632 uint16_t rpi = 0;
2633 struct lpfc_iocbq *cmdiocbq, *rspiocbq;
2634 IOCB_t *cmd, *rsp;
2635 struct lpfc_sli_ct_request *ctreq;
2636 struct lpfc_dmabuf *txbmp;
2637 struct ulp_bde64 *txbpl = NULL;
2638 struct lpfc_dmabufext *txbuffer = NULL;
2639 struct list_head head;
2640 struct lpfc_dmabuf *curr;
2641 uint16_t txxri, rxxri;
2642 uint32_t num_bde;
2643 uint8_t *ptr = NULL, *rx_databuf = NULL;
2644 int rc = 0;
2645 int time_left;
2646 int iocb_stat;
2647 unsigned long flags;
2648 void *dataout = NULL;
2649 uint32_t total_mem;
2651 /* in case no data is returned return just the return code */
2652 job->reply->reply_payload_rcv_len = 0;
2654 if (job->request_len <
2655 sizeof(struct fc_bsg_request) + sizeof(struct diag_mode_test)) {
2656 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2657 "2739 Received DIAG TEST request below minimum "
2658 "size\n");
2659 rc = -EINVAL;
2660 goto loopback_test_exit;
2663 if (job->request_payload.payload_len !=
2664 job->reply_payload.payload_len) {
2665 rc = -EINVAL;
2666 goto loopback_test_exit;
2669 diag_mode = (struct diag_mode_test *)
2670 job->request->rqst_data.h_vendor.vendor_cmd;
2672 if ((phba->link_state == LPFC_HBA_ERROR) ||
2673 (psli->sli_flag & LPFC_BLOCK_MGMT_IO) ||
2674 (!(psli->sli_flag & LPFC_SLI_ACTIVE))) {
2675 rc = -EACCES;
2676 goto loopback_test_exit;
2679 if (!lpfc_is_link_up(phba) || !(phba->link_flag & LS_LOOPBACK_MODE)) {
2680 rc = -EACCES;
2681 goto loopback_test_exit;
2684 size = job->request_payload.payload_len;
2685 full_size = size + ELX_LOOPBACK_HEADER_SZ; /* plus the header */
2687 if ((size == 0) || (size > 80 * BUF_SZ_4K)) {
2688 rc = -ERANGE;
2689 goto loopback_test_exit;
2692 if (full_size >= BUF_SZ_4K) {
2694 * Allocate memory for ioctl data. If buffer is bigger than 64k,
2695 * then we allocate 64k and re-use that buffer over and over to
2696 * xfer the whole block. This is because Linux kernel has a
2697 * problem allocating more than 120k of kernel space memory. Saw
2698 * problem with GET_FCPTARGETMAPPING...
2700 if (size <= (64 * 1024))
2701 total_mem = full_size;
2702 else
2703 total_mem = 64 * 1024;
2704 } else
2705 /* Allocate memory for ioctl data */
2706 total_mem = BUF_SZ_4K;
2708 dataout = kmalloc(total_mem, GFP_KERNEL);
2709 if (dataout == NULL) {
2710 rc = -ENOMEM;
2711 goto loopback_test_exit;
2714 ptr = dataout;
2715 ptr += ELX_LOOPBACK_HEADER_SZ;
2716 sg_copy_to_buffer(job->request_payload.sg_list,
2717 job->request_payload.sg_cnt,
2718 ptr, size);
2719 rc = lpfcdiag_loop_self_reg(phba, &rpi);
2720 if (rc)
2721 goto loopback_test_exit;
2723 rc = lpfcdiag_loop_get_xri(phba, rpi, &txxri, &rxxri);
2724 if (rc) {
2725 lpfcdiag_loop_self_unreg(phba, rpi);
2726 goto loopback_test_exit;
2729 rc = lpfcdiag_loop_post_rxbufs(phba, rxxri, full_size);
2730 if (rc) {
2731 lpfcdiag_loop_self_unreg(phba, rpi);
2732 goto loopback_test_exit;
2735 evt = lpfc_bsg_event_new(FC_REG_CT_EVENT, current->pid,
2736 SLI_CT_ELX_LOOPBACK);
2737 if (!evt) {
2738 lpfcdiag_loop_self_unreg(phba, rpi);
2739 rc = -ENOMEM;
2740 goto loopback_test_exit;
2743 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2744 list_add(&evt->node, &phba->ct_ev_waiters);
2745 lpfc_bsg_event_ref(evt);
2746 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2748 cmdiocbq = lpfc_sli_get_iocbq(phba);
2749 rspiocbq = lpfc_sli_get_iocbq(phba);
2750 txbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2752 if (txbmp) {
2753 txbmp->virt = lpfc_mbuf_alloc(phba, 0, &txbmp->phys);
2754 if (txbmp->virt) {
2755 INIT_LIST_HEAD(&txbmp->list);
2756 txbpl = (struct ulp_bde64 *) txbmp->virt;
2757 txbuffer = diag_cmd_data_alloc(phba,
2758 txbpl, full_size, 0);
2762 if (!cmdiocbq || !rspiocbq || !txbmp || !txbpl || !txbuffer ||
2763 !txbmp->virt) {
2764 rc = -ENOMEM;
2765 goto err_loopback_test_exit;
2768 cmd = &cmdiocbq->iocb;
2769 rsp = &rspiocbq->iocb;
2771 INIT_LIST_HEAD(&head);
2772 list_add_tail(&head, &txbuffer->dma.list);
2773 list_for_each_entry(curr, &head, list) {
2774 segment_len = ((struct lpfc_dmabufext *)curr)->size;
2775 if (current_offset == 0) {
2776 ctreq = curr->virt;
2777 memset(ctreq, 0, ELX_LOOPBACK_HEADER_SZ);
2778 ctreq->RevisionId.bits.Revision = SLI_CT_REVISION;
2779 ctreq->RevisionId.bits.InId = 0;
2780 ctreq->FsType = SLI_CT_ELX_LOOPBACK;
2781 ctreq->FsSubType = 0;
2782 ctreq->CommandResponse.bits.CmdRsp = ELX_LOOPBACK_DATA;
2783 ctreq->CommandResponse.bits.Size = size;
2784 segment_offset = ELX_LOOPBACK_HEADER_SZ;
2785 } else
2786 segment_offset = 0;
2788 BUG_ON(segment_offset >= segment_len);
2789 memcpy(curr->virt + segment_offset,
2790 ptr + current_offset,
2791 segment_len - segment_offset);
2793 current_offset += segment_len - segment_offset;
2794 BUG_ON(current_offset > size);
2796 list_del(&head);
2798 /* Build the XMIT_SEQUENCE iocb */
2800 num_bde = (uint32_t)txbuffer->flag;
2802 cmd->un.xseq64.bdl.addrHigh = putPaddrHigh(txbmp->phys);
2803 cmd->un.xseq64.bdl.addrLow = putPaddrLow(txbmp->phys);
2804 cmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
2805 cmd->un.xseq64.bdl.bdeSize = (num_bde * sizeof(struct ulp_bde64));
2807 cmd->un.xseq64.w5.hcsw.Fctl = (LS | LA);
2808 cmd->un.xseq64.w5.hcsw.Dfctl = 0;
2809 cmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
2810 cmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
2812 cmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;
2813 cmd->ulpBdeCount = 1;
2814 cmd->ulpLe = 1;
2815 cmd->ulpClass = CLASS3;
2816 cmd->ulpContext = txxri;
2818 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
2819 cmdiocbq->vport = phba->pport;
2821 iocb_stat = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq,
2822 rspiocbq, (phba->fc_ratov * 2) +
2823 LPFC_DRVR_TIMEOUT);
2825 if ((iocb_stat != IOCB_SUCCESS) || (rsp->ulpStatus != IOCB_SUCCESS)) {
2826 rc = -EIO;
2827 goto err_loopback_test_exit;
2830 evt->waiting = 1;
2831 time_left = wait_event_interruptible_timeout(
2832 evt->wq, !list_empty(&evt->events_to_see),
2833 ((phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT) * HZ);
2834 evt->waiting = 0;
2835 if (list_empty(&evt->events_to_see))
2836 rc = (time_left) ? -EINTR : -ETIMEDOUT;
2837 else {
2838 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2839 list_move(evt->events_to_see.prev, &evt->events_to_get);
2840 evdat = list_entry(evt->events_to_get.prev,
2841 typeof(*evdat), node);
2842 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2843 rx_databuf = evdat->data;
2844 if (evdat->len != full_size) {
2845 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
2846 "1603 Loopback test did not receive expected "
2847 "data length. actual length 0x%x expected "
2848 "length 0x%x\n",
2849 evdat->len, full_size);
2850 rc = -EIO;
2851 } else if (rx_databuf == NULL)
2852 rc = -EIO;
2853 else {
2854 rc = IOCB_SUCCESS;
2855 /* skip over elx loopback header */
2856 rx_databuf += ELX_LOOPBACK_HEADER_SZ;
2857 job->reply->reply_payload_rcv_len =
2858 sg_copy_from_buffer(job->reply_payload.sg_list,
2859 job->reply_payload.sg_cnt,
2860 rx_databuf, size);
2861 job->reply->reply_payload_rcv_len = size;
2865 err_loopback_test_exit:
2866 lpfcdiag_loop_self_unreg(phba, rpi);
2868 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2869 lpfc_bsg_event_unref(evt); /* release ref */
2870 lpfc_bsg_event_unref(evt); /* delete */
2871 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2873 if (cmdiocbq != NULL)
2874 lpfc_sli_release_iocbq(phba, cmdiocbq);
2876 if (rspiocbq != NULL)
2877 lpfc_sli_release_iocbq(phba, rspiocbq);
2879 if (txbmp != NULL) {
2880 if (txbpl != NULL) {
2881 if (txbuffer != NULL)
2882 diag_cmd_data_free(phba, txbuffer);
2883 lpfc_mbuf_free(phba, txbmp->virt, txbmp->phys);
2885 kfree(txbmp);
2888 loopback_test_exit:
2889 kfree(dataout);
2890 /* make error code available to userspace */
2891 job->reply->result = rc;
2892 job->dd_data = NULL;
2893 /* complete the job back to userspace if no error */
2894 if (rc == 0)
2895 job->job_done(job);
2896 return rc;
2900 * lpfc_bsg_get_dfc_rev - process a GET_DFC_REV bsg vendor command
2901 * @job: GET_DFC_REV fc_bsg_job
2903 static int
2904 lpfc_bsg_get_dfc_rev(struct fc_bsg_job *job)
2906 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
2907 struct lpfc_hba *phba = vport->phba;
2908 struct get_mgmt_rev *event_req;
2909 struct get_mgmt_rev_reply *event_reply;
2910 int rc = 0;
2912 if (job->request_len <
2913 sizeof(struct fc_bsg_request) + sizeof(struct get_mgmt_rev)) {
2914 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2915 "2740 Received GET_DFC_REV request below "
2916 "minimum size\n");
2917 rc = -EINVAL;
2918 goto job_error;
2921 event_req = (struct get_mgmt_rev *)
2922 job->request->rqst_data.h_vendor.vendor_cmd;
2924 event_reply = (struct get_mgmt_rev_reply *)
2925 job->reply->reply_data.vendor_reply.vendor_rsp;
2927 if (job->reply_len <
2928 sizeof(struct fc_bsg_request) + sizeof(struct get_mgmt_rev_reply)) {
2929 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2930 "2741 Received GET_DFC_REV reply below "
2931 "minimum size\n");
2932 rc = -EINVAL;
2933 goto job_error;
2936 event_reply->info.a_Major = MANAGEMENT_MAJOR_REV;
2937 event_reply->info.a_Minor = MANAGEMENT_MINOR_REV;
2938 job_error:
2939 job->reply->result = rc;
2940 if (rc == 0)
2941 job->job_done(job);
2942 return rc;
2946 * lpfc_bsg_issue_mbox_cmpl - lpfc_bsg_issue_mbox mbox completion handler
2947 * @phba: Pointer to HBA context object.
2948 * @pmboxq: Pointer to mailbox command.
2950 * This is completion handler function for mailbox commands issued from
2951 * lpfc_bsg_issue_mbox function. This function is called by the
2952 * mailbox event handler function with no lock held. This function
2953 * will wake up thread waiting on the wait queue pointed by context1
2954 * of the mailbox.
2956 void
2957 lpfc_bsg_issue_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
2959 struct bsg_job_data *dd_data;
2960 struct fc_bsg_job *job;
2961 uint32_t size;
2962 unsigned long flags;
2963 uint8_t *pmb, *pmb_buf;
2965 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2966 dd_data = pmboxq->context1;
2967 /* job already timed out? */
2968 if (!dd_data) {
2969 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2970 return;
2974 * The outgoing buffer is readily referred from the dma buffer,
2975 * just need to get header part from mailboxq structure.
2977 pmb = (uint8_t *)&pmboxq->u.mb;
2978 pmb_buf = (uint8_t *)dd_data->context_un.mbox.mb;
2979 memcpy(pmb_buf, pmb, sizeof(MAILBOX_t));
2981 job = dd_data->context_un.mbox.set_job;
2982 if (job) {
2983 size = job->reply_payload.payload_len;
2984 job->reply->reply_payload_rcv_len =
2985 sg_copy_from_buffer(job->reply_payload.sg_list,
2986 job->reply_payload.sg_cnt,
2987 pmb_buf, size);
2988 /* need to hold the lock until we set job->dd_data to NULL
2989 * to hold off the timeout handler returning to the mid-layer
2990 * while we are still processing the job.
2992 job->dd_data = NULL;
2993 dd_data->context_un.mbox.set_job = NULL;
2994 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2995 } else {
2996 dd_data->context_un.mbox.set_job = NULL;
2997 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3000 mempool_free(dd_data->context_un.mbox.pmboxq, phba->mbox_mem_pool);
3001 lpfc_bsg_dma_page_free(phba, dd_data->context_un.mbox.dmabuffers);
3002 kfree(dd_data);
3004 if (job) {
3005 job->reply->result = 0;
3006 job->job_done(job);
3008 return;
3012 * lpfc_bsg_check_cmd_access - test for a supported mailbox command
3013 * @phba: Pointer to HBA context object.
3014 * @mb: Pointer to a mailbox object.
3015 * @vport: Pointer to a vport object.
3017 * Some commands require the port to be offline, some may not be called from
3018 * the application.
3020 static int lpfc_bsg_check_cmd_access(struct lpfc_hba *phba,
3021 MAILBOX_t *mb, struct lpfc_vport *vport)
3023 /* return negative error values for bsg job */
3024 switch (mb->mbxCommand) {
3025 /* Offline only */
3026 case MBX_INIT_LINK:
3027 case MBX_DOWN_LINK:
3028 case MBX_CONFIG_LINK:
3029 case MBX_CONFIG_RING:
3030 case MBX_RESET_RING:
3031 case MBX_UNREG_LOGIN:
3032 case MBX_CLEAR_LA:
3033 case MBX_DUMP_CONTEXT:
3034 case MBX_RUN_DIAGS:
3035 case MBX_RESTART:
3036 case MBX_SET_MASK:
3037 if (!(vport->fc_flag & FC_OFFLINE_MODE)) {
3038 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3039 "2743 Command 0x%x is illegal in on-line "
3040 "state\n",
3041 mb->mbxCommand);
3042 return -EPERM;
3044 case MBX_WRITE_NV:
3045 case MBX_WRITE_VPARMS:
3046 case MBX_LOAD_SM:
3047 case MBX_READ_NV:
3048 case MBX_READ_CONFIG:
3049 case MBX_READ_RCONFIG:
3050 case MBX_READ_STATUS:
3051 case MBX_READ_XRI:
3052 case MBX_READ_REV:
3053 case MBX_READ_LNK_STAT:
3054 case MBX_DUMP_MEMORY:
3055 case MBX_DOWN_LOAD:
3056 case MBX_UPDATE_CFG:
3057 case MBX_KILL_BOARD:
3058 case MBX_LOAD_AREA:
3059 case MBX_LOAD_EXP_ROM:
3060 case MBX_BEACON:
3061 case MBX_DEL_LD_ENTRY:
3062 case MBX_SET_DEBUG:
3063 case MBX_WRITE_WWN:
3064 case MBX_SLI4_CONFIG:
3065 case MBX_READ_EVENT_LOG:
3066 case MBX_READ_EVENT_LOG_STATUS:
3067 case MBX_WRITE_EVENT_LOG:
3068 case MBX_PORT_CAPABILITIES:
3069 case MBX_PORT_IOV_CONTROL:
3070 case MBX_RUN_BIU_DIAG64:
3071 break;
3072 case MBX_SET_VARIABLE:
3073 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3074 "1226 mbox: set_variable 0x%x, 0x%x\n",
3075 mb->un.varWords[0],
3076 mb->un.varWords[1]);
3077 if ((mb->un.varWords[0] == SETVAR_MLOMNT)
3078 && (mb->un.varWords[1] == 1)) {
3079 phba->wait_4_mlo_maint_flg = 1;
3080 } else if (mb->un.varWords[0] == SETVAR_MLORST) {
3081 phba->link_flag &= ~LS_LOOPBACK_MODE;
3082 phba->fc_topology = LPFC_TOPOLOGY_PT_PT;
3084 break;
3085 case MBX_READ_SPARM64:
3086 case MBX_READ_TOPOLOGY:
3087 case MBX_REG_LOGIN:
3088 case MBX_REG_LOGIN64:
3089 case MBX_CONFIG_PORT:
3090 case MBX_RUN_BIU_DIAG:
3091 default:
3092 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3093 "2742 Unknown Command 0x%x\n",
3094 mb->mbxCommand);
3095 return -EPERM;
3098 return 0; /* ok */
3102 * lpfc_bsg_mbox_ext_cleanup - clean up context of multi-buffer mbox session
3103 * @phba: Pointer to HBA context object.
3105 * This is routine clean up and reset BSG handling of multi-buffer mbox
3106 * command session.
3108 static void
3109 lpfc_bsg_mbox_ext_session_reset(struct lpfc_hba *phba)
3111 if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_IDLE)
3112 return;
3114 /* free all memory, including dma buffers */
3115 lpfc_bsg_dma_page_list_free(phba,
3116 &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
3117 lpfc_bsg_dma_page_free(phba, phba->mbox_ext_buf_ctx.mbx_dmabuf);
3118 /* multi-buffer write mailbox command pass-through complete */
3119 memset((char *)&phba->mbox_ext_buf_ctx, 0,
3120 sizeof(struct lpfc_mbox_ext_buf_ctx));
3121 INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
3123 return;
3127 * lpfc_bsg_issue_mbox_ext_handle_job - job handler for multi-buffer mbox cmpl
3128 * @phba: Pointer to HBA context object.
3129 * @pmboxq: Pointer to mailbox command.
3131 * This is routine handles BSG job for mailbox commands completions with
3132 * multiple external buffers.
3134 static struct fc_bsg_job *
3135 lpfc_bsg_issue_mbox_ext_handle_job(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
3137 struct bsg_job_data *dd_data;
3138 struct fc_bsg_job *job;
3139 uint8_t *pmb, *pmb_buf;
3140 unsigned long flags;
3141 uint32_t size;
3142 int rc = 0;
3144 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3145 dd_data = pmboxq->context1;
3146 /* has the job already timed out? */
3147 if (!dd_data) {
3148 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3149 job = NULL;
3150 goto job_done_out;
3154 * The outgoing buffer is readily referred from the dma buffer,
3155 * just need to get header part from mailboxq structure.
3157 pmb = (uint8_t *)&pmboxq->u.mb;
3158 pmb_buf = (uint8_t *)dd_data->context_un.mbox.mb;
3159 memcpy(pmb_buf, pmb, sizeof(MAILBOX_t));
3161 job = dd_data->context_un.mbox.set_job;
3162 if (job) {
3163 size = job->reply_payload.payload_len;
3164 job->reply->reply_payload_rcv_len =
3165 sg_copy_from_buffer(job->reply_payload.sg_list,
3166 job->reply_payload.sg_cnt,
3167 pmb_buf, size);
3168 /* result for successful */
3169 job->reply->result = 0;
3170 job->dd_data = NULL;
3171 /* need to hold the lock util we set job->dd_data to NULL
3172 * to hold off the timeout handler from midlayer to take
3173 * any action.
3175 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3176 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3177 "2937 SLI_CONFIG ext-buffer maibox command "
3178 "(x%x/x%x) complete bsg job done, bsize:%d\n",
3179 phba->mbox_ext_buf_ctx.nembType,
3180 phba->mbox_ext_buf_ctx.mboxType, size);
3181 lpfc_idiag_mbxacc_dump_bsg_mbox(phba,
3182 phba->mbox_ext_buf_ctx.nembType,
3183 phba->mbox_ext_buf_ctx.mboxType,
3184 dma_ebuf, sta_pos_addr,
3185 phba->mbox_ext_buf_ctx.mbx_dmabuf, 0);
3186 } else
3187 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3189 job_done_out:
3190 if (!job)
3191 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3192 "2938 SLI_CONFIG ext-buffer maibox "
3193 "command (x%x/x%x) failure, rc:x%x\n",
3194 phba->mbox_ext_buf_ctx.nembType,
3195 phba->mbox_ext_buf_ctx.mboxType, rc);
3196 /* state change */
3197 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_DONE;
3198 kfree(dd_data);
3200 return job;
3204 * lpfc_bsg_issue_read_mbox_ext_cmpl - compl handler for multi-buffer read mbox
3205 * @phba: Pointer to HBA context object.
3206 * @pmboxq: Pointer to mailbox command.
3208 * This is completion handler function for mailbox read commands with multiple
3209 * external buffers.
3211 static void
3212 lpfc_bsg_issue_read_mbox_ext_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
3214 struct fc_bsg_job *job;
3216 /* handle the BSG job with mailbox command */
3217 if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_ABTS)
3218 pmboxq->u.mb.mbxStatus = MBXERR_ERROR;
3220 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3221 "2939 SLI_CONFIG ext-buffer rd maibox command "
3222 "complete, ctxState:x%x, mbxStatus:x%x\n",
3223 phba->mbox_ext_buf_ctx.state, pmboxq->u.mb.mbxStatus);
3225 job = lpfc_bsg_issue_mbox_ext_handle_job(phba, pmboxq);
3227 if (pmboxq->u.mb.mbxStatus || phba->mbox_ext_buf_ctx.numBuf == 1)
3228 lpfc_bsg_mbox_ext_session_reset(phba);
3230 /* free base driver mailbox structure memory */
3231 mempool_free(pmboxq, phba->mbox_mem_pool);
3233 /* complete the bsg job if we have it */
3234 if (job)
3235 job->job_done(job);
3237 return;
3241 * lpfc_bsg_issue_write_mbox_ext_cmpl - cmpl handler for multi-buffer write mbox
3242 * @phba: Pointer to HBA context object.
3243 * @pmboxq: Pointer to mailbox command.
3245 * This is completion handler function for mailbox write commands with multiple
3246 * external buffers.
3248 static void
3249 lpfc_bsg_issue_write_mbox_ext_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
3251 struct fc_bsg_job *job;
3253 /* handle the BSG job with the mailbox command */
3254 if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_ABTS)
3255 pmboxq->u.mb.mbxStatus = MBXERR_ERROR;
3257 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3258 "2940 SLI_CONFIG ext-buffer wr maibox command "
3259 "complete, ctxState:x%x, mbxStatus:x%x\n",
3260 phba->mbox_ext_buf_ctx.state, pmboxq->u.mb.mbxStatus);
3262 job = lpfc_bsg_issue_mbox_ext_handle_job(phba, pmboxq);
3264 /* free all memory, including dma buffers */
3265 mempool_free(pmboxq, phba->mbox_mem_pool);
3266 lpfc_bsg_mbox_ext_session_reset(phba);
3268 /* complete the bsg job if we have it */
3269 if (job)
3270 job->job_done(job);
3272 return;
3275 static void
3276 lpfc_bsg_sli_cfg_dma_desc_setup(struct lpfc_hba *phba, enum nemb_type nemb_tp,
3277 uint32_t index, struct lpfc_dmabuf *mbx_dmabuf,
3278 struct lpfc_dmabuf *ext_dmabuf)
3280 struct lpfc_sli_config_mbox *sli_cfg_mbx;
3282 /* pointer to the start of mailbox command */
3283 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)mbx_dmabuf->virt;
3285 if (nemb_tp == nemb_mse) {
3286 if (index == 0) {
3287 sli_cfg_mbx->un.sli_config_emb0_subsys.
3288 mse[index].pa_hi =
3289 putPaddrHigh(mbx_dmabuf->phys +
3290 sizeof(MAILBOX_t));
3291 sli_cfg_mbx->un.sli_config_emb0_subsys.
3292 mse[index].pa_lo =
3293 putPaddrLow(mbx_dmabuf->phys +
3294 sizeof(MAILBOX_t));
3295 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3296 "2943 SLI_CONFIG(mse)[%d], "
3297 "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3298 index,
3299 sli_cfg_mbx->un.sli_config_emb0_subsys.
3300 mse[index].buf_len,
3301 sli_cfg_mbx->un.sli_config_emb0_subsys.
3302 mse[index].pa_hi,
3303 sli_cfg_mbx->un.sli_config_emb0_subsys.
3304 mse[index].pa_lo);
3305 } else {
3306 sli_cfg_mbx->un.sli_config_emb0_subsys.
3307 mse[index].pa_hi =
3308 putPaddrHigh(ext_dmabuf->phys);
3309 sli_cfg_mbx->un.sli_config_emb0_subsys.
3310 mse[index].pa_lo =
3311 putPaddrLow(ext_dmabuf->phys);
3312 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3313 "2944 SLI_CONFIG(mse)[%d], "
3314 "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3315 index,
3316 sli_cfg_mbx->un.sli_config_emb0_subsys.
3317 mse[index].buf_len,
3318 sli_cfg_mbx->un.sli_config_emb0_subsys.
3319 mse[index].pa_hi,
3320 sli_cfg_mbx->un.sli_config_emb0_subsys.
3321 mse[index].pa_lo);
3323 } else {
3324 if (index == 0) {
3325 sli_cfg_mbx->un.sli_config_emb1_subsys.
3326 hbd[index].pa_hi =
3327 putPaddrHigh(mbx_dmabuf->phys +
3328 sizeof(MAILBOX_t));
3329 sli_cfg_mbx->un.sli_config_emb1_subsys.
3330 hbd[index].pa_lo =
3331 putPaddrLow(mbx_dmabuf->phys +
3332 sizeof(MAILBOX_t));
3333 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3334 "3007 SLI_CONFIG(hbd)[%d], "
3335 "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3336 index,
3337 bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
3338 &sli_cfg_mbx->un.
3339 sli_config_emb1_subsys.hbd[index]),
3340 sli_cfg_mbx->un.sli_config_emb1_subsys.
3341 hbd[index].pa_hi,
3342 sli_cfg_mbx->un.sli_config_emb1_subsys.
3343 hbd[index].pa_lo);
3345 } else {
3346 sli_cfg_mbx->un.sli_config_emb1_subsys.
3347 hbd[index].pa_hi =
3348 putPaddrHigh(ext_dmabuf->phys);
3349 sli_cfg_mbx->un.sli_config_emb1_subsys.
3350 hbd[index].pa_lo =
3351 putPaddrLow(ext_dmabuf->phys);
3352 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3353 "3008 SLI_CONFIG(hbd)[%d], "
3354 "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3355 index,
3356 bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
3357 &sli_cfg_mbx->un.
3358 sli_config_emb1_subsys.hbd[index]),
3359 sli_cfg_mbx->un.sli_config_emb1_subsys.
3360 hbd[index].pa_hi,
3361 sli_cfg_mbx->un.sli_config_emb1_subsys.
3362 hbd[index].pa_lo);
3365 return;
3369 * lpfc_bsg_sli_cfg_mse_read_cmd_ext - sli_config non-embedded mailbox cmd read
3370 * @phba: Pointer to HBA context object.
3371 * @mb: Pointer to a BSG mailbox object.
3372 * @nemb_tp: Enumerate of non-embedded mailbox command type.
3373 * @dmabuff: Pointer to a DMA buffer descriptor.
3375 * This routine performs SLI_CONFIG (0x9B) read mailbox command operation with
3376 * non-embedded external bufffers.
3378 static int
3379 lpfc_bsg_sli_cfg_read_cmd_ext(struct lpfc_hba *phba, struct fc_bsg_job *job,
3380 enum nemb_type nemb_tp,
3381 struct lpfc_dmabuf *dmabuf)
3383 struct lpfc_sli_config_mbox *sli_cfg_mbx;
3384 struct dfc_mbox_req *mbox_req;
3385 struct lpfc_dmabuf *curr_dmabuf, *next_dmabuf;
3386 uint32_t ext_buf_cnt, ext_buf_index;
3387 struct lpfc_dmabuf *ext_dmabuf = NULL;
3388 struct bsg_job_data *dd_data = NULL;
3389 LPFC_MBOXQ_t *pmboxq = NULL;
3390 MAILBOX_t *pmb;
3391 uint8_t *pmbx;
3392 int rc, i;
3394 mbox_req =
3395 (struct dfc_mbox_req *)job->request->rqst_data.h_vendor.vendor_cmd;
3397 /* pointer to the start of mailbox command */
3398 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
3400 if (nemb_tp == nemb_mse) {
3401 ext_buf_cnt = bsg_bf_get(lpfc_mbox_hdr_mse_cnt,
3402 &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr);
3403 if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_MSE) {
3404 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3405 "2945 Handled SLI_CONFIG(mse) rd, "
3406 "ext_buf_cnt(%d) out of range(%d)\n",
3407 ext_buf_cnt,
3408 LPFC_MBX_SLI_CONFIG_MAX_MSE);
3409 rc = -ERANGE;
3410 goto job_error;
3412 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3413 "2941 Handled SLI_CONFIG(mse) rd, "
3414 "ext_buf_cnt:%d\n", ext_buf_cnt);
3415 } else {
3416 /* sanity check on interface type for support */
3417 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
3418 LPFC_SLI_INTF_IF_TYPE_2) {
3419 rc = -ENODEV;
3420 goto job_error;
3422 /* nemb_tp == nemb_hbd */
3423 ext_buf_cnt = sli_cfg_mbx->un.sli_config_emb1_subsys.hbd_count;
3424 if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_HBD) {
3425 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3426 "2946 Handled SLI_CONFIG(hbd) rd, "
3427 "ext_buf_cnt(%d) out of range(%d)\n",
3428 ext_buf_cnt,
3429 LPFC_MBX_SLI_CONFIG_MAX_HBD);
3430 rc = -ERANGE;
3431 goto job_error;
3433 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3434 "2942 Handled SLI_CONFIG(hbd) rd, "
3435 "ext_buf_cnt:%d\n", ext_buf_cnt);
3438 /* before dma descriptor setup */
3439 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_rd, dma_mbox,
3440 sta_pre_addr, dmabuf, ext_buf_cnt);
3442 /* reject non-embedded mailbox command with none external buffer */
3443 if (ext_buf_cnt == 0) {
3444 rc = -EPERM;
3445 goto job_error;
3446 } else if (ext_buf_cnt > 1) {
3447 /* additional external read buffers */
3448 for (i = 1; i < ext_buf_cnt; i++) {
3449 ext_dmabuf = lpfc_bsg_dma_page_alloc(phba);
3450 if (!ext_dmabuf) {
3451 rc = -ENOMEM;
3452 goto job_error;
3454 list_add_tail(&ext_dmabuf->list,
3455 &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
3459 /* bsg tracking structure */
3460 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
3461 if (!dd_data) {
3462 rc = -ENOMEM;
3463 goto job_error;
3466 /* mailbox command structure for base driver */
3467 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3468 if (!pmboxq) {
3469 rc = -ENOMEM;
3470 goto job_error;
3472 memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
3474 /* for the first external buffer */
3475 lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, 0, dmabuf, dmabuf);
3477 /* for the rest of external buffer descriptors if any */
3478 if (ext_buf_cnt > 1) {
3479 ext_buf_index = 1;
3480 list_for_each_entry_safe(curr_dmabuf, next_dmabuf,
3481 &phba->mbox_ext_buf_ctx.ext_dmabuf_list, list) {
3482 lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp,
3483 ext_buf_index, dmabuf,
3484 curr_dmabuf);
3485 ext_buf_index++;
3489 /* after dma descriptor setup */
3490 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_rd, dma_mbox,
3491 sta_pos_addr, dmabuf, ext_buf_cnt);
3493 /* construct base driver mbox command */
3494 pmb = &pmboxq->u.mb;
3495 pmbx = (uint8_t *)dmabuf->virt;
3496 memcpy(pmb, pmbx, sizeof(*pmb));
3497 pmb->mbxOwner = OWN_HOST;
3498 pmboxq->vport = phba->pport;
3500 /* multi-buffer handling context */
3501 phba->mbox_ext_buf_ctx.nembType = nemb_tp;
3502 phba->mbox_ext_buf_ctx.mboxType = mbox_rd;
3503 phba->mbox_ext_buf_ctx.numBuf = ext_buf_cnt;
3504 phba->mbox_ext_buf_ctx.mbxTag = mbox_req->extMboxTag;
3505 phba->mbox_ext_buf_ctx.seqNum = mbox_req->extSeqNum;
3506 phba->mbox_ext_buf_ctx.mbx_dmabuf = dmabuf;
3508 /* callback for multi-buffer read mailbox command */
3509 pmboxq->mbox_cmpl = lpfc_bsg_issue_read_mbox_ext_cmpl;
3511 /* context fields to callback function */
3512 pmboxq->context1 = dd_data;
3513 dd_data->type = TYPE_MBOX;
3514 dd_data->context_un.mbox.pmboxq = pmboxq;
3515 dd_data->context_un.mbox.mb = (MAILBOX_t *)pmbx;
3516 dd_data->context_un.mbox.set_job = job;
3517 job->dd_data = dd_data;
3519 /* state change */
3520 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
3522 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
3523 if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
3524 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3525 "2947 Issued SLI_CONFIG ext-buffer "
3526 "maibox command, rc:x%x\n", rc);
3527 return SLI_CONFIG_HANDLED;
3529 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3530 "2948 Failed to issue SLI_CONFIG ext-buffer "
3531 "maibox command, rc:x%x\n", rc);
3532 rc = -EPIPE;
3534 job_error:
3535 if (pmboxq)
3536 mempool_free(pmboxq, phba->mbox_mem_pool);
3537 lpfc_bsg_dma_page_list_free(phba,
3538 &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
3539 kfree(dd_data);
3540 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_IDLE;
3541 return rc;
3545 * lpfc_bsg_sli_cfg_write_cmd_ext - sli_config non-embedded mailbox cmd write
3546 * @phba: Pointer to HBA context object.
3547 * @mb: Pointer to a BSG mailbox object.
3548 * @dmabuff: Pointer to a DMA buffer descriptor.
3550 * This routine performs SLI_CONFIG (0x9B) write mailbox command operation with
3551 * non-embedded external bufffers.
3553 static int
3554 lpfc_bsg_sli_cfg_write_cmd_ext(struct lpfc_hba *phba, struct fc_bsg_job *job,
3555 enum nemb_type nemb_tp,
3556 struct lpfc_dmabuf *dmabuf)
3558 struct dfc_mbox_req *mbox_req;
3559 struct lpfc_sli_config_mbox *sli_cfg_mbx;
3560 uint32_t ext_buf_cnt;
3561 struct bsg_job_data *dd_data = NULL;
3562 LPFC_MBOXQ_t *pmboxq = NULL;
3563 MAILBOX_t *pmb;
3564 uint8_t *mbx;
3565 int rc = SLI_CONFIG_NOT_HANDLED, i;
3567 mbox_req =
3568 (struct dfc_mbox_req *)job->request->rqst_data.h_vendor.vendor_cmd;
3570 /* pointer to the start of mailbox command */
3571 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
3573 if (nemb_tp == nemb_mse) {
3574 ext_buf_cnt = bsg_bf_get(lpfc_mbox_hdr_mse_cnt,
3575 &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr);
3576 if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_MSE) {
3577 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3578 "2953 Handled SLI_CONFIG(mse) wr, "
3579 "ext_buf_cnt(%d) out of range(%d)\n",
3580 ext_buf_cnt,
3581 LPFC_MBX_SLI_CONFIG_MAX_MSE);
3582 return -ERANGE;
3584 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3585 "2949 Handled SLI_CONFIG(mse) wr, "
3586 "ext_buf_cnt:%d\n", ext_buf_cnt);
3587 } else {
3588 /* sanity check on interface type for support */
3589 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
3590 LPFC_SLI_INTF_IF_TYPE_2)
3591 return -ENODEV;
3592 /* nemb_tp == nemb_hbd */
3593 ext_buf_cnt = sli_cfg_mbx->un.sli_config_emb1_subsys.hbd_count;
3594 if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_HBD) {
3595 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3596 "2954 Handled SLI_CONFIG(hbd) wr, "
3597 "ext_buf_cnt(%d) out of range(%d)\n",
3598 ext_buf_cnt,
3599 LPFC_MBX_SLI_CONFIG_MAX_HBD);
3600 return -ERANGE;
3602 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3603 "2950 Handled SLI_CONFIG(hbd) wr, "
3604 "ext_buf_cnt:%d\n", ext_buf_cnt);
3607 /* before dma buffer descriptor setup */
3608 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_wr, dma_mbox,
3609 sta_pre_addr, dmabuf, ext_buf_cnt);
3611 if (ext_buf_cnt == 0)
3612 return -EPERM;
3614 /* for the first external buffer */
3615 lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, 0, dmabuf, dmabuf);
3617 /* after dma descriptor setup */
3618 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_wr, dma_mbox,
3619 sta_pos_addr, dmabuf, ext_buf_cnt);
3621 /* log for looking forward */
3622 for (i = 1; i < ext_buf_cnt; i++) {
3623 if (nemb_tp == nemb_mse)
3624 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3625 "2951 SLI_CONFIG(mse), buf[%d]-length:%d\n",
3626 i, sli_cfg_mbx->un.sli_config_emb0_subsys.
3627 mse[i].buf_len);
3628 else
3629 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3630 "2952 SLI_CONFIG(hbd), buf[%d]-length:%d\n",
3631 i, bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
3632 &sli_cfg_mbx->un.sli_config_emb1_subsys.
3633 hbd[i]));
3636 /* multi-buffer handling context */
3637 phba->mbox_ext_buf_ctx.nembType = nemb_tp;
3638 phba->mbox_ext_buf_ctx.mboxType = mbox_wr;
3639 phba->mbox_ext_buf_ctx.numBuf = ext_buf_cnt;
3640 phba->mbox_ext_buf_ctx.mbxTag = mbox_req->extMboxTag;
3641 phba->mbox_ext_buf_ctx.seqNum = mbox_req->extSeqNum;
3642 phba->mbox_ext_buf_ctx.mbx_dmabuf = dmabuf;
3644 if (ext_buf_cnt == 1) {
3645 /* bsg tracking structure */
3646 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
3647 if (!dd_data) {
3648 rc = -ENOMEM;
3649 goto job_error;
3652 /* mailbox command structure for base driver */
3653 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3654 if (!pmboxq) {
3655 rc = -ENOMEM;
3656 goto job_error;
3658 memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
3659 pmb = &pmboxq->u.mb;
3660 mbx = (uint8_t *)dmabuf->virt;
3661 memcpy(pmb, mbx, sizeof(*pmb));
3662 pmb->mbxOwner = OWN_HOST;
3663 pmboxq->vport = phba->pport;
3665 /* callback for multi-buffer read mailbox command */
3666 pmboxq->mbox_cmpl = lpfc_bsg_issue_write_mbox_ext_cmpl;
3668 /* context fields to callback function */
3669 pmboxq->context1 = dd_data;
3670 dd_data->type = TYPE_MBOX;
3671 dd_data->context_un.mbox.pmboxq = pmboxq;
3672 dd_data->context_un.mbox.mb = (MAILBOX_t *)mbx;
3673 dd_data->context_un.mbox.set_job = job;
3674 job->dd_data = dd_data;
3676 /* state change */
3677 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
3679 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
3680 if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
3681 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3682 "2955 Issued SLI_CONFIG ext-buffer "
3683 "maibox command, rc:x%x\n", rc);
3684 return SLI_CONFIG_HANDLED;
3686 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3687 "2956 Failed to issue SLI_CONFIG ext-buffer "
3688 "maibox command, rc:x%x\n", rc);
3689 rc = -EPIPE;
3692 /* wait for additoinal external buffers */
3693 job->reply->result = 0;
3694 job->job_done(job);
3695 return SLI_CONFIG_HANDLED;
3697 job_error:
3698 if (pmboxq)
3699 mempool_free(pmboxq, phba->mbox_mem_pool);
3700 kfree(dd_data);
3702 return rc;
3706 * lpfc_bsg_handle_sli_cfg_mbox - handle sli-cfg mailbox cmd with ext buffer
3707 * @phba: Pointer to HBA context object.
3708 * @mb: Pointer to a BSG mailbox object.
3709 * @dmabuff: Pointer to a DMA buffer descriptor.
3711 * This routine handles SLI_CONFIG (0x9B) mailbox command with non-embedded
3712 * external bufffers, including both 0x9B with non-embedded MSEs and 0x9B
3713 * with embedded sussystem 0x1 and opcodes with external HBDs.
3715 static int
3716 lpfc_bsg_handle_sli_cfg_mbox(struct lpfc_hba *phba, struct fc_bsg_job *job,
3717 struct lpfc_dmabuf *dmabuf)
3719 struct lpfc_sli_config_mbox *sli_cfg_mbx;
3720 uint32_t subsys;
3721 uint32_t opcode;
3722 int rc = SLI_CONFIG_NOT_HANDLED;
3724 /* state change */
3725 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_HOST;
3727 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
3729 if (!bsg_bf_get(lpfc_mbox_hdr_emb,
3730 &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) {
3731 subsys = bsg_bf_get(lpfc_emb0_subcmnd_subsys,
3732 &sli_cfg_mbx->un.sli_config_emb0_subsys);
3733 opcode = bsg_bf_get(lpfc_emb0_subcmnd_opcode,
3734 &sli_cfg_mbx->un.sli_config_emb0_subsys);
3735 if (subsys == SLI_CONFIG_SUBSYS_FCOE) {
3736 switch (opcode) {
3737 case FCOE_OPCODE_READ_FCF:
3738 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3739 "2957 Handled SLI_CONFIG "
3740 "subsys_fcoe, opcode:x%x\n",
3741 opcode);
3742 rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
3743 nemb_mse, dmabuf);
3744 break;
3745 case FCOE_OPCODE_ADD_FCF:
3746 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3747 "2958 Handled SLI_CONFIG "
3748 "subsys_fcoe, opcode:x%x\n",
3749 opcode);
3750 rc = lpfc_bsg_sli_cfg_write_cmd_ext(phba, job,
3751 nemb_mse, dmabuf);
3752 break;
3753 default:
3754 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3755 "2959 Not handled SLI_CONFIG "
3756 "subsys_fcoe, opcode:x%x\n",
3757 opcode);
3758 rc = SLI_CONFIG_NOT_HANDLED;
3759 break;
3761 } else {
3762 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3763 "2977 Handled SLI_CONFIG "
3764 "subsys:x%d, opcode:x%x\n",
3765 subsys, opcode);
3766 rc = SLI_CONFIG_NOT_HANDLED;
3768 } else {
3769 subsys = bsg_bf_get(lpfc_emb1_subcmnd_subsys,
3770 &sli_cfg_mbx->un.sli_config_emb1_subsys);
3771 opcode = bsg_bf_get(lpfc_emb1_subcmnd_opcode,
3772 &sli_cfg_mbx->un.sli_config_emb1_subsys);
3773 if (subsys == SLI_CONFIG_SUBSYS_COMN) {
3774 switch (opcode) {
3775 case COMN_OPCODE_READ_OBJECT:
3776 case COMN_OPCODE_READ_OBJECT_LIST:
3777 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3778 "2960 Handled SLI_CONFIG "
3779 "subsys_comn, opcode:x%x\n",
3780 opcode);
3781 rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
3782 nemb_hbd, dmabuf);
3783 break;
3784 case COMN_OPCODE_WRITE_OBJECT:
3785 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3786 "2961 Handled SLI_CONFIG "
3787 "subsys_comn, opcode:x%x\n",
3788 opcode);
3789 rc = lpfc_bsg_sli_cfg_write_cmd_ext(phba, job,
3790 nemb_hbd, dmabuf);
3791 break;
3792 default:
3793 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3794 "2962 Not handled SLI_CONFIG "
3795 "subsys_comn, opcode:x%x\n",
3796 opcode);
3797 rc = SLI_CONFIG_NOT_HANDLED;
3798 break;
3800 } else {
3801 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3802 "2978 Handled SLI_CONFIG "
3803 "subsys:x%d, opcode:x%x\n",
3804 subsys, opcode);
3805 rc = SLI_CONFIG_NOT_HANDLED;
3808 return rc;
3812 * lpfc_bsg_mbox_ext_abort_req - request to abort mbox command with ext buffers
3813 * @phba: Pointer to HBA context object.
3815 * This routine is for requesting to abort a pass-through mailbox command with
3816 * multiple external buffers due to error condition.
3818 static void
3819 lpfc_bsg_mbox_ext_abort(struct lpfc_hba *phba)
3821 if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_PORT)
3822 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_ABTS;
3823 else
3824 lpfc_bsg_mbox_ext_session_reset(phba);
3825 return;
3829 * lpfc_bsg_read_ebuf_get - get the next mailbox read external buffer
3830 * @phba: Pointer to HBA context object.
3831 * @dmabuf: Pointer to a DMA buffer descriptor.
3833 * This routine extracts the next mailbox read external buffer back to
3834 * user space through BSG.
3836 static int
3837 lpfc_bsg_read_ebuf_get(struct lpfc_hba *phba, struct fc_bsg_job *job)
3839 struct lpfc_sli_config_mbox *sli_cfg_mbx;
3840 struct lpfc_dmabuf *dmabuf;
3841 uint8_t *pbuf;
3842 uint32_t size;
3843 uint32_t index;
3845 index = phba->mbox_ext_buf_ctx.seqNum;
3846 phba->mbox_ext_buf_ctx.seqNum++;
3848 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)
3849 phba->mbox_ext_buf_ctx.mbx_dmabuf->virt;
3851 if (phba->mbox_ext_buf_ctx.nembType == nemb_mse) {
3852 size = bsg_bf_get(lpfc_mbox_sli_config_mse_len,
3853 &sli_cfg_mbx->un.sli_config_emb0_subsys.mse[index]);
3854 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3855 "2963 SLI_CONFIG (mse) ext-buffer rd get "
3856 "buffer[%d], size:%d\n", index, size);
3857 } else {
3858 size = bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
3859 &sli_cfg_mbx->un.sli_config_emb1_subsys.hbd[index]);
3860 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3861 "2964 SLI_CONFIG (hbd) ext-buffer rd get "
3862 "buffer[%d], size:%d\n", index, size);
3864 if (list_empty(&phba->mbox_ext_buf_ctx.ext_dmabuf_list))
3865 return -EPIPE;
3866 dmabuf = list_first_entry(&phba->mbox_ext_buf_ctx.ext_dmabuf_list,
3867 struct lpfc_dmabuf, list);
3868 list_del_init(&dmabuf->list);
3870 /* after dma buffer descriptor setup */
3871 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, phba->mbox_ext_buf_ctx.nembType,
3872 mbox_rd, dma_ebuf, sta_pos_addr,
3873 dmabuf, index);
3875 pbuf = (uint8_t *)dmabuf->virt;
3876 job->reply->reply_payload_rcv_len =
3877 sg_copy_from_buffer(job->reply_payload.sg_list,
3878 job->reply_payload.sg_cnt,
3879 pbuf, size);
3881 lpfc_bsg_dma_page_free(phba, dmabuf);
3883 if (phba->mbox_ext_buf_ctx.seqNum == phba->mbox_ext_buf_ctx.numBuf) {
3884 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3885 "2965 SLI_CONFIG (hbd) ext-buffer rd mbox "
3886 "command session done\n");
3887 lpfc_bsg_mbox_ext_session_reset(phba);
3890 job->reply->result = 0;
3891 job->job_done(job);
3893 return SLI_CONFIG_HANDLED;
3897 * lpfc_bsg_write_ebuf_set - set the next mailbox write external buffer
3898 * @phba: Pointer to HBA context object.
3899 * @dmabuf: Pointer to a DMA buffer descriptor.
3901 * This routine sets up the next mailbox read external buffer obtained
3902 * from user space through BSG.
3904 static int
3905 lpfc_bsg_write_ebuf_set(struct lpfc_hba *phba, struct fc_bsg_job *job,
3906 struct lpfc_dmabuf *dmabuf)
3908 struct lpfc_sli_config_mbox *sli_cfg_mbx;
3909 struct bsg_job_data *dd_data = NULL;
3910 LPFC_MBOXQ_t *pmboxq = NULL;
3911 MAILBOX_t *pmb;
3912 enum nemb_type nemb_tp;
3913 uint8_t *pbuf;
3914 uint32_t size;
3915 uint32_t index;
3916 int rc;
3918 index = phba->mbox_ext_buf_ctx.seqNum;
3919 phba->mbox_ext_buf_ctx.seqNum++;
3920 nemb_tp = phba->mbox_ext_buf_ctx.nembType;
3922 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)
3923 phba->mbox_ext_buf_ctx.mbx_dmabuf->virt;
3925 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
3926 if (!dd_data) {
3927 rc = -ENOMEM;
3928 goto job_error;
3931 pbuf = (uint8_t *)dmabuf->virt;
3932 size = job->request_payload.payload_len;
3933 sg_copy_to_buffer(job->request_payload.sg_list,
3934 job->request_payload.sg_cnt,
3935 pbuf, size);
3937 if (phba->mbox_ext_buf_ctx.nembType == nemb_mse) {
3938 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3939 "2966 SLI_CONFIG (mse) ext-buffer wr set "
3940 "buffer[%d], size:%d\n",
3941 phba->mbox_ext_buf_ctx.seqNum, size);
3943 } else {
3944 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3945 "2967 SLI_CONFIG (hbd) ext-buffer wr set "
3946 "buffer[%d], size:%d\n",
3947 phba->mbox_ext_buf_ctx.seqNum, size);
3951 /* set up external buffer descriptor and add to external buffer list */
3952 lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, index,
3953 phba->mbox_ext_buf_ctx.mbx_dmabuf,
3954 dmabuf);
3955 list_add_tail(&dmabuf->list, &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
3957 /* after write dma buffer */
3958 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, phba->mbox_ext_buf_ctx.nembType,
3959 mbox_wr, dma_ebuf, sta_pos_addr,
3960 dmabuf, index);
3962 if (phba->mbox_ext_buf_ctx.seqNum == phba->mbox_ext_buf_ctx.numBuf) {
3963 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3964 "2968 SLI_CONFIG ext-buffer wr all %d "
3965 "ebuffers received\n",
3966 phba->mbox_ext_buf_ctx.numBuf);
3967 /* mailbox command structure for base driver */
3968 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3969 if (!pmboxq) {
3970 rc = -ENOMEM;
3971 goto job_error;
3973 memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
3974 pbuf = (uint8_t *)phba->mbox_ext_buf_ctx.mbx_dmabuf->virt;
3975 pmb = &pmboxq->u.mb;
3976 memcpy(pmb, pbuf, sizeof(*pmb));
3977 pmb->mbxOwner = OWN_HOST;
3978 pmboxq->vport = phba->pport;
3980 /* callback for multi-buffer write mailbox command */
3981 pmboxq->mbox_cmpl = lpfc_bsg_issue_write_mbox_ext_cmpl;
3983 /* context fields to callback function */
3984 pmboxq->context1 = dd_data;
3985 dd_data->type = TYPE_MBOX;
3986 dd_data->context_un.mbox.pmboxq = pmboxq;
3987 dd_data->context_un.mbox.mb = (MAILBOX_t *)pbuf;
3988 dd_data->context_un.mbox.set_job = job;
3989 job->dd_data = dd_data;
3991 /* state change */
3992 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
3994 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
3995 if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
3996 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3997 "2969 Issued SLI_CONFIG ext-buffer "
3998 "maibox command, rc:x%x\n", rc);
3999 return SLI_CONFIG_HANDLED;
4001 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4002 "2970 Failed to issue SLI_CONFIG ext-buffer "
4003 "maibox command, rc:x%x\n", rc);
4004 rc = -EPIPE;
4005 goto job_error;
4008 /* wait for additoinal external buffers */
4009 job->reply->result = 0;
4010 job->job_done(job);
4011 return SLI_CONFIG_HANDLED;
4013 job_error:
4014 lpfc_bsg_dma_page_free(phba, dmabuf);
4015 kfree(dd_data);
4017 return rc;
4021 * lpfc_bsg_handle_sli_cfg_ebuf - handle ext buffer with sli-cfg mailbox cmd
4022 * @phba: Pointer to HBA context object.
4023 * @mb: Pointer to a BSG mailbox object.
4024 * @dmabuff: Pointer to a DMA buffer descriptor.
4026 * This routine handles the external buffer with SLI_CONFIG (0x9B) mailbox
4027 * command with multiple non-embedded external buffers.
4029 static int
4030 lpfc_bsg_handle_sli_cfg_ebuf(struct lpfc_hba *phba, struct fc_bsg_job *job,
4031 struct lpfc_dmabuf *dmabuf)
4033 int rc;
4035 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4036 "2971 SLI_CONFIG buffer (type:x%x)\n",
4037 phba->mbox_ext_buf_ctx.mboxType);
4039 if (phba->mbox_ext_buf_ctx.mboxType == mbox_rd) {
4040 if (phba->mbox_ext_buf_ctx.state != LPFC_BSG_MBOX_DONE) {
4041 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4042 "2972 SLI_CONFIG rd buffer state "
4043 "mismatch:x%x\n",
4044 phba->mbox_ext_buf_ctx.state);
4045 lpfc_bsg_mbox_ext_abort(phba);
4046 return -EPIPE;
4048 rc = lpfc_bsg_read_ebuf_get(phba, job);
4049 if (rc == SLI_CONFIG_HANDLED)
4050 lpfc_bsg_dma_page_free(phba, dmabuf);
4051 } else { /* phba->mbox_ext_buf_ctx.mboxType == mbox_wr */
4052 if (phba->mbox_ext_buf_ctx.state != LPFC_BSG_MBOX_HOST) {
4053 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4054 "2973 SLI_CONFIG wr buffer state "
4055 "mismatch:x%x\n",
4056 phba->mbox_ext_buf_ctx.state);
4057 lpfc_bsg_mbox_ext_abort(phba);
4058 return -EPIPE;
4060 rc = lpfc_bsg_write_ebuf_set(phba, job, dmabuf);
4062 return rc;
4066 * lpfc_bsg_handle_sli_cfg_ext - handle sli-cfg mailbox with external buffer
4067 * @phba: Pointer to HBA context object.
4068 * @mb: Pointer to a BSG mailbox object.
4069 * @dmabuff: Pointer to a DMA buffer descriptor.
4071 * This routine checkes and handles non-embedded multi-buffer SLI_CONFIG
4072 * (0x9B) mailbox commands and external buffers.
4074 static int
4075 lpfc_bsg_handle_sli_cfg_ext(struct lpfc_hba *phba, struct fc_bsg_job *job,
4076 struct lpfc_dmabuf *dmabuf)
4078 struct dfc_mbox_req *mbox_req;
4079 int rc = SLI_CONFIG_NOT_HANDLED;
4081 mbox_req =
4082 (struct dfc_mbox_req *)job->request->rqst_data.h_vendor.vendor_cmd;
4084 /* mbox command with/without single external buffer */
4085 if (mbox_req->extMboxTag == 0 && mbox_req->extSeqNum == 0)
4086 return rc;
4088 /* mbox command and first external buffer */
4089 if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_IDLE) {
4090 if (mbox_req->extSeqNum == 1) {
4091 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4092 "2974 SLI_CONFIG mailbox: tag:%d, "
4093 "seq:%d\n", mbox_req->extMboxTag,
4094 mbox_req->extSeqNum);
4095 rc = lpfc_bsg_handle_sli_cfg_mbox(phba, job, dmabuf);
4096 return rc;
4097 } else
4098 goto sli_cfg_ext_error;
4102 * handle additional external buffers
4105 /* check broken pipe conditions */
4106 if (mbox_req->extMboxTag != phba->mbox_ext_buf_ctx.mbxTag)
4107 goto sli_cfg_ext_error;
4108 if (mbox_req->extSeqNum > phba->mbox_ext_buf_ctx.numBuf)
4109 goto sli_cfg_ext_error;
4110 if (mbox_req->extSeqNum != phba->mbox_ext_buf_ctx.seqNum + 1)
4111 goto sli_cfg_ext_error;
4113 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4114 "2975 SLI_CONFIG mailbox external buffer: "
4115 "extSta:x%x, tag:%d, seq:%d\n",
4116 phba->mbox_ext_buf_ctx.state, mbox_req->extMboxTag,
4117 mbox_req->extSeqNum);
4118 rc = lpfc_bsg_handle_sli_cfg_ebuf(phba, job, dmabuf);
4119 return rc;
4121 sli_cfg_ext_error:
4122 /* all other cases, broken pipe */
4123 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4124 "2976 SLI_CONFIG mailbox broken pipe: "
4125 "ctxSta:x%x, ctxNumBuf:%d "
4126 "ctxTag:%d, ctxSeq:%d, tag:%d, seq:%d\n",
4127 phba->mbox_ext_buf_ctx.state,
4128 phba->mbox_ext_buf_ctx.numBuf,
4129 phba->mbox_ext_buf_ctx.mbxTag,
4130 phba->mbox_ext_buf_ctx.seqNum,
4131 mbox_req->extMboxTag, mbox_req->extSeqNum);
4133 lpfc_bsg_mbox_ext_session_reset(phba);
4135 return -EPIPE;
4139 * lpfc_bsg_issue_mbox - issues a mailbox command on behalf of an app
4140 * @phba: Pointer to HBA context object.
4141 * @mb: Pointer to a mailbox object.
4142 * @vport: Pointer to a vport object.
4144 * Allocate a tracking object, mailbox command memory, get a mailbox
4145 * from the mailbox pool, copy the caller mailbox command.
4147 * If offline and the sli is active we need to poll for the command (port is
4148 * being reset) and com-plete the job, otherwise issue the mailbox command and
4149 * let our completion handler finish the command.
4151 static uint32_t
4152 lpfc_bsg_issue_mbox(struct lpfc_hba *phba, struct fc_bsg_job *job,
4153 struct lpfc_vport *vport)
4155 LPFC_MBOXQ_t *pmboxq = NULL; /* internal mailbox queue */
4156 MAILBOX_t *pmb; /* shortcut to the pmboxq mailbox */
4157 /* a 4k buffer to hold the mb and extended data from/to the bsg */
4158 uint8_t *pmbx = NULL;
4159 struct bsg_job_data *dd_data = NULL; /* bsg data tracking structure */
4160 struct lpfc_dmabuf *dmabuf = NULL;
4161 struct dfc_mbox_req *mbox_req;
4162 struct READ_EVENT_LOG_VAR *rdEventLog;
4163 uint32_t transmit_length, receive_length, mode;
4164 struct lpfc_mbx_sli4_config *sli4_config;
4165 struct lpfc_mbx_nembed_cmd *nembed_sge;
4166 struct mbox_header *header;
4167 struct ulp_bde64 *bde;
4168 uint8_t *ext = NULL;
4169 int rc = 0;
4170 uint8_t *from;
4171 uint32_t size;
4174 /* in case no data is transferred */
4175 job->reply->reply_payload_rcv_len = 0;
4177 /* sanity check to protect driver */
4178 if (job->reply_payload.payload_len > BSG_MBOX_SIZE ||
4179 job->request_payload.payload_len > BSG_MBOX_SIZE) {
4180 rc = -ERANGE;
4181 goto job_done;
4185 * Don't allow mailbox commands to be sent when blocked or when in
4186 * the middle of discovery
4188 if (phba->sli.sli_flag & LPFC_BLOCK_MGMT_IO) {
4189 rc = -EAGAIN;
4190 goto job_done;
4193 mbox_req =
4194 (struct dfc_mbox_req *)job->request->rqst_data.h_vendor.vendor_cmd;
4196 /* check if requested extended data lengths are valid */
4197 if ((mbox_req->inExtWLen > BSG_MBOX_SIZE/sizeof(uint32_t)) ||
4198 (mbox_req->outExtWLen > BSG_MBOX_SIZE/sizeof(uint32_t))) {
4199 rc = -ERANGE;
4200 goto job_done;
4203 dmabuf = lpfc_bsg_dma_page_alloc(phba);
4204 if (!dmabuf || !dmabuf->virt) {
4205 rc = -ENOMEM;
4206 goto job_done;
4209 /* Get the mailbox command or external buffer from BSG */
4210 pmbx = (uint8_t *)dmabuf->virt;
4211 size = job->request_payload.payload_len;
4212 sg_copy_to_buffer(job->request_payload.sg_list,
4213 job->request_payload.sg_cnt, pmbx, size);
4215 /* Handle possible SLI_CONFIG with non-embedded payloads */
4216 if (phba->sli_rev == LPFC_SLI_REV4) {
4217 rc = lpfc_bsg_handle_sli_cfg_ext(phba, job, dmabuf);
4218 if (rc == SLI_CONFIG_HANDLED)
4219 goto job_cont;
4220 if (rc)
4221 goto job_done;
4222 /* SLI_CONFIG_NOT_HANDLED for other mailbox commands */
4225 rc = lpfc_bsg_check_cmd_access(phba, (MAILBOX_t *)pmbx, vport);
4226 if (rc != 0)
4227 goto job_done; /* must be negative */
4229 /* allocate our bsg tracking structure */
4230 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
4231 if (!dd_data) {
4232 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
4233 "2727 Failed allocation of dd_data\n");
4234 rc = -ENOMEM;
4235 goto job_done;
4238 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4239 if (!pmboxq) {
4240 rc = -ENOMEM;
4241 goto job_done;
4243 memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
4245 pmb = &pmboxq->u.mb;
4246 memcpy(pmb, pmbx, sizeof(*pmb));
4247 pmb->mbxOwner = OWN_HOST;
4248 pmboxq->vport = vport;
4250 /* If HBA encountered an error attention, allow only DUMP
4251 * or RESTART mailbox commands until the HBA is restarted.
4253 if (phba->pport->stopped &&
4254 pmb->mbxCommand != MBX_DUMP_MEMORY &&
4255 pmb->mbxCommand != MBX_RESTART &&
4256 pmb->mbxCommand != MBX_WRITE_VPARMS &&
4257 pmb->mbxCommand != MBX_WRITE_WWN)
4258 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
4259 "2797 mbox: Issued mailbox cmd "
4260 "0x%x while in stopped state.\n",
4261 pmb->mbxCommand);
4263 /* extended mailbox commands will need an extended buffer */
4264 if (mbox_req->inExtWLen || mbox_req->outExtWLen) {
4265 /* any data for the device? */
4266 if (mbox_req->inExtWLen) {
4267 from = pmbx;
4268 ext = from + sizeof(MAILBOX_t);
4270 pmboxq->context2 = ext;
4271 pmboxq->in_ext_byte_len =
4272 mbox_req->inExtWLen * sizeof(uint32_t);
4273 pmboxq->out_ext_byte_len =
4274 mbox_req->outExtWLen * sizeof(uint32_t);
4275 pmboxq->mbox_offset_word = mbox_req->mbOffset;
4278 /* biu diag will need a kernel buffer to transfer the data
4279 * allocate our own buffer and setup the mailbox command to
4280 * use ours
4282 if (pmb->mbxCommand == MBX_RUN_BIU_DIAG64) {
4283 transmit_length = pmb->un.varWords[1];
4284 receive_length = pmb->un.varWords[4];
4285 /* transmit length cannot be greater than receive length or
4286 * mailbox extension size
4288 if ((transmit_length > receive_length) ||
4289 (transmit_length > BSG_MBOX_SIZE - sizeof(MAILBOX_t))) {
4290 rc = -ERANGE;
4291 goto job_done;
4293 pmb->un.varBIUdiag.un.s2.xmit_bde64.addrHigh =
4294 putPaddrHigh(dmabuf->phys + sizeof(MAILBOX_t));
4295 pmb->un.varBIUdiag.un.s2.xmit_bde64.addrLow =
4296 putPaddrLow(dmabuf->phys + sizeof(MAILBOX_t));
4298 pmb->un.varBIUdiag.un.s2.rcv_bde64.addrHigh =
4299 putPaddrHigh(dmabuf->phys + sizeof(MAILBOX_t)
4300 + pmb->un.varBIUdiag.un.s2.xmit_bde64.tus.f.bdeSize);
4301 pmb->un.varBIUdiag.un.s2.rcv_bde64.addrLow =
4302 putPaddrLow(dmabuf->phys + sizeof(MAILBOX_t)
4303 + pmb->un.varBIUdiag.un.s2.xmit_bde64.tus.f.bdeSize);
4304 } else if (pmb->mbxCommand == MBX_READ_EVENT_LOG) {
4305 rdEventLog = &pmb->un.varRdEventLog;
4306 receive_length = rdEventLog->rcv_bde64.tus.f.bdeSize;
4307 mode = bf_get(lpfc_event_log, rdEventLog);
4309 /* receive length cannot be greater than mailbox
4310 * extension size
4312 if (receive_length > BSG_MBOX_SIZE - sizeof(MAILBOX_t)) {
4313 rc = -ERANGE;
4314 goto job_done;
4317 /* mode zero uses a bde like biu diags command */
4318 if (mode == 0) {
4319 pmb->un.varWords[3] = putPaddrLow(dmabuf->phys
4320 + sizeof(MAILBOX_t));
4321 pmb->un.varWords[4] = putPaddrHigh(dmabuf->phys
4322 + sizeof(MAILBOX_t));
4324 } else if (phba->sli_rev == LPFC_SLI_REV4) {
4325 if (pmb->mbxCommand == MBX_DUMP_MEMORY) {
4326 /* rebuild the command for sli4 using our own buffers
4327 * like we do for biu diags
4329 receive_length = pmb->un.varWords[2];
4330 /* receive length cannot be greater than mailbox
4331 * extension size
4333 if (receive_length == 0) {
4334 rc = -ERANGE;
4335 goto job_done;
4337 pmb->un.varWords[3] = putPaddrLow(dmabuf->phys
4338 + sizeof(MAILBOX_t));
4339 pmb->un.varWords[4] = putPaddrHigh(dmabuf->phys
4340 + sizeof(MAILBOX_t));
4341 } else if ((pmb->mbxCommand == MBX_UPDATE_CFG) &&
4342 pmb->un.varUpdateCfg.co) {
4343 bde = (struct ulp_bde64 *)&pmb->un.varWords[4];
4345 /* bde size cannot be greater than mailbox ext size */
4346 if (bde->tus.f.bdeSize >
4347 BSG_MBOX_SIZE - sizeof(MAILBOX_t)) {
4348 rc = -ERANGE;
4349 goto job_done;
4351 bde->addrHigh = putPaddrHigh(dmabuf->phys
4352 + sizeof(MAILBOX_t));
4353 bde->addrLow = putPaddrLow(dmabuf->phys
4354 + sizeof(MAILBOX_t));
4355 } else if (pmb->mbxCommand == MBX_SLI4_CONFIG) {
4356 /* Handling non-embedded SLI_CONFIG mailbox command */
4357 sli4_config = &pmboxq->u.mqe.un.sli4_config;
4358 if (!bf_get(lpfc_mbox_hdr_emb,
4359 &sli4_config->header.cfg_mhdr)) {
4360 /* rebuild the command for sli4 using our
4361 * own buffers like we do for biu diags
4363 header = (struct mbox_header *)
4364 &pmb->un.varWords[0];
4365 nembed_sge = (struct lpfc_mbx_nembed_cmd *)
4366 &pmb->un.varWords[0];
4367 receive_length = nembed_sge->sge[0].length;
4369 /* receive length cannot be greater than
4370 * mailbox extension size
4372 if ((receive_length == 0) ||
4373 (receive_length >
4374 BSG_MBOX_SIZE - sizeof(MAILBOX_t))) {
4375 rc = -ERANGE;
4376 goto job_done;
4379 nembed_sge->sge[0].pa_hi =
4380 putPaddrHigh(dmabuf->phys
4381 + sizeof(MAILBOX_t));
4382 nembed_sge->sge[0].pa_lo =
4383 putPaddrLow(dmabuf->phys
4384 + sizeof(MAILBOX_t));
4389 dd_data->context_un.mbox.dmabuffers = dmabuf;
4391 /* setup wake call as IOCB callback */
4392 pmboxq->mbox_cmpl = lpfc_bsg_issue_mbox_cmpl;
4394 /* setup context field to pass wait_queue pointer to wake function */
4395 pmboxq->context1 = dd_data;
4396 dd_data->type = TYPE_MBOX;
4397 dd_data->context_un.mbox.pmboxq = pmboxq;
4398 dd_data->context_un.mbox.mb = (MAILBOX_t *)pmbx;
4399 dd_data->context_un.mbox.set_job = job;
4400 dd_data->context_un.mbox.ext = ext;
4401 dd_data->context_un.mbox.mbOffset = mbox_req->mbOffset;
4402 dd_data->context_un.mbox.inExtWLen = mbox_req->inExtWLen;
4403 dd_data->context_un.mbox.outExtWLen = mbox_req->outExtWLen;
4404 job->dd_data = dd_data;
4406 if ((vport->fc_flag & FC_OFFLINE_MODE) ||
4407 (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))) {
4408 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
4409 if (rc != MBX_SUCCESS) {
4410 rc = (rc == MBX_TIMEOUT) ? -ETIME : -ENODEV;
4411 goto job_done;
4414 /* job finished, copy the data */
4415 memcpy(pmbx, pmb, sizeof(*pmb));
4416 job->reply->reply_payload_rcv_len =
4417 sg_copy_from_buffer(job->reply_payload.sg_list,
4418 job->reply_payload.sg_cnt,
4419 pmbx, size);
4420 /* not waiting mbox already done */
4421 rc = 0;
4422 goto job_done;
4425 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
4426 if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY))
4427 return 1; /* job started */
4429 job_done:
4430 /* common exit for error or job completed inline */
4431 if (pmboxq)
4432 mempool_free(pmboxq, phba->mbox_mem_pool);
4433 lpfc_bsg_dma_page_free(phba, dmabuf);
4434 kfree(dd_data);
4436 job_cont:
4437 return rc;
4441 * lpfc_bsg_mbox_cmd - process an fc bsg LPFC_BSG_VENDOR_MBOX command
4442 * @job: MBOX fc_bsg_job for LPFC_BSG_VENDOR_MBOX.
4444 static int
4445 lpfc_bsg_mbox_cmd(struct fc_bsg_job *job)
4447 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
4448 struct lpfc_hba *phba = vport->phba;
4449 struct dfc_mbox_req *mbox_req;
4450 int rc = 0;
4452 /* mix-and-match backward compatibility */
4453 job->reply->reply_payload_rcv_len = 0;
4454 if (job->request_len <
4455 sizeof(struct fc_bsg_request) + sizeof(struct dfc_mbox_req)) {
4456 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4457 "2737 Mix-and-match backward compability "
4458 "between MBOX_REQ old size:%d and "
4459 "new request size:%d\n",
4460 (int)(job->request_len -
4461 sizeof(struct fc_bsg_request)),
4462 (int)sizeof(struct dfc_mbox_req));
4463 mbox_req = (struct dfc_mbox_req *)
4464 job->request->rqst_data.h_vendor.vendor_cmd;
4465 mbox_req->extMboxTag = 0;
4466 mbox_req->extSeqNum = 0;
4469 rc = lpfc_bsg_issue_mbox(phba, job, vport);
4471 if (rc == 0) {
4472 /* job done */
4473 job->reply->result = 0;
4474 job->dd_data = NULL;
4475 job->job_done(job);
4476 } else if (rc == 1)
4477 /* job submitted, will complete later*/
4478 rc = 0; /* return zero, no error */
4479 else {
4480 /* some error occurred */
4481 job->reply->result = rc;
4482 job->dd_data = NULL;
4485 return rc;
4489 * lpfc_bsg_menlo_cmd_cmp - lpfc_menlo_cmd completion handler
4490 * @phba: Pointer to HBA context object.
4491 * @cmdiocbq: Pointer to command iocb.
4492 * @rspiocbq: Pointer to response iocb.
4494 * This function is the completion handler for iocbs issued using
4495 * lpfc_menlo_cmd function. This function is called by the
4496 * ring event handler function without any lock held. This function
4497 * can be called from both worker thread context and interrupt
4498 * context. This function also can be called from another thread which
4499 * cleans up the SLI layer objects.
4500 * This function copies the contents of the response iocb to the
4501 * response iocb memory object provided by the caller of
4502 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
4503 * sleeps for the iocb completion.
4505 static void
4506 lpfc_bsg_menlo_cmd_cmp(struct lpfc_hba *phba,
4507 struct lpfc_iocbq *cmdiocbq,
4508 struct lpfc_iocbq *rspiocbq)
4510 struct bsg_job_data *dd_data;
4511 struct fc_bsg_job *job;
4512 IOCB_t *rsp;
4513 struct lpfc_dmabuf *bmp;
4514 struct lpfc_bsg_menlo *menlo;
4515 unsigned long flags;
4516 struct menlo_response *menlo_resp;
4517 int rc = 0;
4519 spin_lock_irqsave(&phba->ct_ev_lock, flags);
4520 dd_data = cmdiocbq->context1;
4521 if (!dd_data) {
4522 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
4523 return;
4526 menlo = &dd_data->context_un.menlo;
4527 job = menlo->set_job;
4528 job->dd_data = NULL; /* so timeout handler does not reply */
4530 spin_lock(&phba->hbalock);
4531 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
4532 if (cmdiocbq->context2 && rspiocbq)
4533 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
4534 &rspiocbq->iocb, sizeof(IOCB_t));
4535 spin_unlock(&phba->hbalock);
4537 bmp = menlo->bmp;
4538 rspiocbq = menlo->rspiocbq;
4539 rsp = &rspiocbq->iocb;
4541 pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
4542 job->request_payload.sg_cnt, DMA_TO_DEVICE);
4543 pci_unmap_sg(phba->pcidev, job->reply_payload.sg_list,
4544 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
4546 /* always return the xri, this would be used in the case
4547 * of a menlo download to allow the data to be sent as a continuation
4548 * of the exchange.
4550 menlo_resp = (struct menlo_response *)
4551 job->reply->reply_data.vendor_reply.vendor_rsp;
4552 menlo_resp->xri = rsp->ulpContext;
4553 if (rsp->ulpStatus) {
4554 if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
4555 switch (rsp->un.ulpWord[4] & 0xff) {
4556 case IOERR_SEQUENCE_TIMEOUT:
4557 rc = -ETIMEDOUT;
4558 break;
4559 case IOERR_INVALID_RPI:
4560 rc = -EFAULT;
4561 break;
4562 default:
4563 rc = -EACCES;
4564 break;
4566 } else
4567 rc = -EACCES;
4568 } else
4569 job->reply->reply_payload_rcv_len =
4570 rsp->un.genreq64.bdl.bdeSize;
4572 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
4573 lpfc_sli_release_iocbq(phba, rspiocbq);
4574 lpfc_sli_release_iocbq(phba, cmdiocbq);
4575 kfree(bmp);
4576 kfree(dd_data);
4577 /* make error code available to userspace */
4578 job->reply->result = rc;
4579 /* complete the job back to userspace */
4580 job->job_done(job);
4581 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
4582 return;
4586 * lpfc_menlo_cmd - send an ioctl for menlo hardware
4587 * @job: fc_bsg_job to handle
4589 * This function issues a gen request 64 CR ioctl for all menlo cmd requests,
4590 * all the command completions will return the xri for the command.
4591 * For menlo data requests a gen request 64 CX is used to continue the exchange
4592 * supplied in the menlo request header xri field.
4594 static int
4595 lpfc_menlo_cmd(struct fc_bsg_job *job)
4597 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
4598 struct lpfc_hba *phba = vport->phba;
4599 struct lpfc_iocbq *cmdiocbq, *rspiocbq;
4600 IOCB_t *cmd, *rsp;
4601 int rc = 0;
4602 struct menlo_command *menlo_cmd;
4603 struct menlo_response *menlo_resp;
4604 struct lpfc_dmabuf *bmp = NULL;
4605 int request_nseg;
4606 int reply_nseg;
4607 struct scatterlist *sgel = NULL;
4608 int numbde;
4609 dma_addr_t busaddr;
4610 struct bsg_job_data *dd_data;
4611 struct ulp_bde64 *bpl = NULL;
4613 /* in case no data is returned return just the return code */
4614 job->reply->reply_payload_rcv_len = 0;
4616 if (job->request_len <
4617 sizeof(struct fc_bsg_request) +
4618 sizeof(struct menlo_command)) {
4619 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
4620 "2784 Received MENLO_CMD request below "
4621 "minimum size\n");
4622 rc = -ERANGE;
4623 goto no_dd_data;
4626 if (job->reply_len <
4627 sizeof(struct fc_bsg_request) + sizeof(struct menlo_response)) {
4628 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
4629 "2785 Received MENLO_CMD reply below "
4630 "minimum size\n");
4631 rc = -ERANGE;
4632 goto no_dd_data;
4635 if (!(phba->menlo_flag & HBA_MENLO_SUPPORT)) {
4636 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
4637 "2786 Adapter does not support menlo "
4638 "commands\n");
4639 rc = -EPERM;
4640 goto no_dd_data;
4643 menlo_cmd = (struct menlo_command *)
4644 job->request->rqst_data.h_vendor.vendor_cmd;
4646 menlo_resp = (struct menlo_response *)
4647 job->reply->reply_data.vendor_reply.vendor_rsp;
4649 /* allocate our bsg tracking structure */
4650 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
4651 if (!dd_data) {
4652 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
4653 "2787 Failed allocation of dd_data\n");
4654 rc = -ENOMEM;
4655 goto no_dd_data;
4658 bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4659 if (!bmp) {
4660 rc = -ENOMEM;
4661 goto free_dd;
4664 cmdiocbq = lpfc_sli_get_iocbq(phba);
4665 if (!cmdiocbq) {
4666 rc = -ENOMEM;
4667 goto free_bmp;
4670 rspiocbq = lpfc_sli_get_iocbq(phba);
4671 if (!rspiocbq) {
4672 rc = -ENOMEM;
4673 goto free_cmdiocbq;
4676 rsp = &rspiocbq->iocb;
4678 bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
4679 if (!bmp->virt) {
4680 rc = -ENOMEM;
4681 goto free_rspiocbq;
4684 INIT_LIST_HEAD(&bmp->list);
4685 bpl = (struct ulp_bde64 *) bmp->virt;
4686 request_nseg = pci_map_sg(phba->pcidev, job->request_payload.sg_list,
4687 job->request_payload.sg_cnt, DMA_TO_DEVICE);
4688 for_each_sg(job->request_payload.sg_list, sgel, request_nseg, numbde) {
4689 busaddr = sg_dma_address(sgel);
4690 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
4691 bpl->tus.f.bdeSize = sg_dma_len(sgel);
4692 bpl->tus.w = cpu_to_le32(bpl->tus.w);
4693 bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
4694 bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
4695 bpl++;
4698 reply_nseg = pci_map_sg(phba->pcidev, job->reply_payload.sg_list,
4699 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
4700 for_each_sg(job->reply_payload.sg_list, sgel, reply_nseg, numbde) {
4701 busaddr = sg_dma_address(sgel);
4702 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
4703 bpl->tus.f.bdeSize = sg_dma_len(sgel);
4704 bpl->tus.w = cpu_to_le32(bpl->tus.w);
4705 bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
4706 bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
4707 bpl++;
4710 cmd = &cmdiocbq->iocb;
4711 cmd->un.genreq64.bdl.ulpIoTag32 = 0;
4712 cmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
4713 cmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
4714 cmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
4715 cmd->un.genreq64.bdl.bdeSize =
4716 (request_nseg + reply_nseg) * sizeof(struct ulp_bde64);
4717 cmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
4718 cmd->un.genreq64.w5.hcsw.Dfctl = 0;
4719 cmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CMD;
4720 cmd->un.genreq64.w5.hcsw.Type = MENLO_TRANSPORT_TYPE; /* 0xfe */
4721 cmd->ulpBdeCount = 1;
4722 cmd->ulpClass = CLASS3;
4723 cmd->ulpOwner = OWN_CHIP;
4724 cmd->ulpLe = 1; /* Limited Edition */
4725 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
4726 cmdiocbq->vport = phba->pport;
4727 /* We want the firmware to timeout before we do */
4728 cmd->ulpTimeout = MENLO_TIMEOUT - 5;
4729 cmdiocbq->context3 = bmp;
4730 cmdiocbq->context2 = rspiocbq;
4731 cmdiocbq->iocb_cmpl = lpfc_bsg_menlo_cmd_cmp;
4732 cmdiocbq->context1 = dd_data;
4733 cmdiocbq->context2 = rspiocbq;
4734 if (menlo_cmd->cmd == LPFC_BSG_VENDOR_MENLO_CMD) {
4735 cmd->ulpCommand = CMD_GEN_REQUEST64_CR;
4736 cmd->ulpPU = MENLO_PU; /* 3 */
4737 cmd->un.ulpWord[4] = MENLO_DID; /* 0x0000FC0E */
4738 cmd->ulpContext = MENLO_CONTEXT; /* 0 */
4739 } else {
4740 cmd->ulpCommand = CMD_GEN_REQUEST64_CX;
4741 cmd->ulpPU = 1;
4742 cmd->un.ulpWord[4] = 0;
4743 cmd->ulpContext = menlo_cmd->xri;
4746 dd_data->type = TYPE_MENLO;
4747 dd_data->context_un.menlo.cmdiocbq = cmdiocbq;
4748 dd_data->context_un.menlo.rspiocbq = rspiocbq;
4749 dd_data->context_un.menlo.set_job = job;
4750 dd_data->context_un.menlo.bmp = bmp;
4752 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq,
4753 MENLO_TIMEOUT - 5);
4754 if (rc == IOCB_SUCCESS)
4755 return 0; /* done for now */
4757 /* iocb failed so cleanup */
4758 pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
4759 job->request_payload.sg_cnt, DMA_TO_DEVICE);
4760 pci_unmap_sg(phba->pcidev, job->reply_payload.sg_list,
4761 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
4763 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
4765 free_rspiocbq:
4766 lpfc_sli_release_iocbq(phba, rspiocbq);
4767 free_cmdiocbq:
4768 lpfc_sli_release_iocbq(phba, cmdiocbq);
4769 free_bmp:
4770 kfree(bmp);
4771 free_dd:
4772 kfree(dd_data);
4773 no_dd_data:
4774 /* make error code available to userspace */
4775 job->reply->result = rc;
4776 job->dd_data = NULL;
4777 return rc;
4781 * lpfc_bsg_hst_vendor - process a vendor-specific fc_bsg_job
4782 * @job: fc_bsg_job to handle
4784 static int
4785 lpfc_bsg_hst_vendor(struct fc_bsg_job *job)
4787 int command = job->request->rqst_data.h_vendor.vendor_cmd[0];
4788 int rc;
4790 switch (command) {
4791 case LPFC_BSG_VENDOR_SET_CT_EVENT:
4792 rc = lpfc_bsg_hba_set_event(job);
4793 break;
4794 case LPFC_BSG_VENDOR_GET_CT_EVENT:
4795 rc = lpfc_bsg_hba_get_event(job);
4796 break;
4797 case LPFC_BSG_VENDOR_SEND_MGMT_RESP:
4798 rc = lpfc_bsg_send_mgmt_rsp(job);
4799 break;
4800 case LPFC_BSG_VENDOR_DIAG_MODE:
4801 rc = lpfc_bsg_diag_loopback_mode(job);
4802 break;
4803 case LPFC_BSG_VENDOR_DIAG_MODE_END:
4804 rc = lpfc_sli4_bsg_diag_mode_end(job);
4805 break;
4806 case LPFC_BSG_VENDOR_DIAG_RUN_LOOPBACK:
4807 rc = lpfc_bsg_diag_loopback_run(job);
4808 break;
4809 case LPFC_BSG_VENDOR_LINK_DIAG_TEST:
4810 rc = lpfc_sli4_bsg_link_diag_test(job);
4811 break;
4812 case LPFC_BSG_VENDOR_GET_MGMT_REV:
4813 rc = lpfc_bsg_get_dfc_rev(job);
4814 break;
4815 case LPFC_BSG_VENDOR_MBOX:
4816 rc = lpfc_bsg_mbox_cmd(job);
4817 break;
4818 case LPFC_BSG_VENDOR_MENLO_CMD:
4819 case LPFC_BSG_VENDOR_MENLO_DATA:
4820 rc = lpfc_menlo_cmd(job);
4821 break;
4822 default:
4823 rc = -EINVAL;
4824 job->reply->reply_payload_rcv_len = 0;
4825 /* make error code available to userspace */
4826 job->reply->result = rc;
4827 break;
4830 return rc;
4834 * lpfc_bsg_request - handle a bsg request from the FC transport
4835 * @job: fc_bsg_job to handle
4838 lpfc_bsg_request(struct fc_bsg_job *job)
4840 uint32_t msgcode;
4841 int rc;
4843 msgcode = job->request->msgcode;
4844 switch (msgcode) {
4845 case FC_BSG_HST_VENDOR:
4846 rc = lpfc_bsg_hst_vendor(job);
4847 break;
4848 case FC_BSG_RPT_ELS:
4849 rc = lpfc_bsg_rport_els(job);
4850 break;
4851 case FC_BSG_RPT_CT:
4852 rc = lpfc_bsg_send_mgmt_cmd(job);
4853 break;
4854 default:
4855 rc = -EINVAL;
4856 job->reply->reply_payload_rcv_len = 0;
4857 /* make error code available to userspace */
4858 job->reply->result = rc;
4859 break;
4862 return rc;
4866 * lpfc_bsg_timeout - handle timeout of a bsg request from the FC transport
4867 * @job: fc_bsg_job that has timed out
4869 * This function just aborts the job's IOCB. The aborted IOCB will return to
4870 * the waiting function which will handle passing the error back to userspace
4873 lpfc_bsg_timeout(struct fc_bsg_job *job)
4875 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
4876 struct lpfc_hba *phba = vport->phba;
4877 struct lpfc_iocbq *cmdiocb;
4878 struct lpfc_bsg_event *evt;
4879 struct lpfc_bsg_iocb *iocb;
4880 struct lpfc_bsg_mbox *mbox;
4881 struct lpfc_bsg_menlo *menlo;
4882 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
4883 struct bsg_job_data *dd_data;
4884 unsigned long flags;
4886 spin_lock_irqsave(&phba->ct_ev_lock, flags);
4887 dd_data = (struct bsg_job_data *)job->dd_data;
4888 /* timeout and completion crossed paths if no dd_data */
4889 if (!dd_data) {
4890 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
4891 return 0;
4894 switch (dd_data->type) {
4895 case TYPE_IOCB:
4896 iocb = &dd_data->context_un.iocb;
4897 cmdiocb = iocb->cmdiocbq;
4898 /* hint to completion handler that the job timed out */
4899 job->reply->result = -EAGAIN;
4900 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
4901 /* this will call our completion handler */
4902 spin_lock_irq(&phba->hbalock);
4903 lpfc_sli_issue_abort_iotag(phba, pring, cmdiocb);
4904 spin_unlock_irq(&phba->hbalock);
4905 break;
4906 case TYPE_EVT:
4907 evt = dd_data->context_un.evt;
4908 /* this event has no job anymore */
4909 evt->set_job = NULL;
4910 job->dd_data = NULL;
4911 job->reply->reply_payload_rcv_len = 0;
4912 /* Return -EAGAIN which is our way of signallying the
4913 * app to retry.
4915 job->reply->result = -EAGAIN;
4916 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
4917 job->job_done(job);
4918 break;
4919 case TYPE_MBOX:
4920 mbox = &dd_data->context_un.mbox;
4921 /* this mbox has no job anymore */
4922 mbox->set_job = NULL;
4923 job->dd_data = NULL;
4924 job->reply->reply_payload_rcv_len = 0;
4925 job->reply->result = -EAGAIN;
4926 /* the mbox completion handler can now be run */
4927 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
4928 job->job_done(job);
4929 if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_PORT)
4930 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_ABTS;
4931 break;
4932 case TYPE_MENLO:
4933 menlo = &dd_data->context_un.menlo;
4934 cmdiocb = menlo->cmdiocbq;
4935 /* hint to completion handler that the job timed out */
4936 job->reply->result = -EAGAIN;
4937 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
4938 /* this will call our completion handler */
4939 spin_lock_irq(&phba->hbalock);
4940 lpfc_sli_issue_abort_iotag(phba, pring, cmdiocb);
4941 spin_unlock_irq(&phba->hbalock);
4942 break;
4943 default:
4944 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
4945 break;
4948 /* scsi transport fc fc_bsg_job_timeout expects a zero return code,
4949 * otherwise an error message will be displayed on the console
4950 * so always return success (zero)
4952 return 0;