dma-pool: Only allocate from CMA when in same memory zone
[linux-2.6-microblaze.git] / drivers / scsi / lpfc / lpfc_bsg.c
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2017-2019 Broadcom. All Rights Reserved. The term *
5  * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.     *
6  * Copyright (C) 2009-2015 Emulex.  All rights reserved.           *
7  * EMULEX and SLI are trademarks of Emulex.                        *
8  * www.broadcom.com                                                *
9  *                                                                 *
10  * This program is free software; you can redistribute it and/or   *
11  * modify it under the terms of version 2 of the GNU General       *
12  * Public License as published by the Free Software Foundation.    *
13  * This program is distributed in the hope that it will be useful. *
14  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
15  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
16  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
17  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
18  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
19  * more details, a copy of which can be found in the file COPYING  *
20  * included with this package.                                     *
21  *******************************************************************/
22
23 #include <linux/interrupt.h>
24 #include <linux/mempool.h>
25 #include <linux/pci.h>
26 #include <linux/slab.h>
27 #include <linux/delay.h>
28 #include <linux/list.h>
29 #include <linux/bsg-lib.h>
30 #include <linux/vmalloc.h>
31
32 #include <scsi/scsi.h>
33 #include <scsi/scsi_host.h>
34 #include <scsi/scsi_transport_fc.h>
35 #include <scsi/scsi_bsg_fc.h>
36 #include <scsi/fc/fc_fs.h>
37
38 #include "lpfc_hw4.h"
39 #include "lpfc_hw.h"
40 #include "lpfc_sli.h"
41 #include "lpfc_sli4.h"
42 #include "lpfc_nl.h"
43 #include "lpfc_bsg.h"
44 #include "lpfc_disc.h"
45 #include "lpfc_scsi.h"
46 #include "lpfc.h"
47 #include "lpfc_logmsg.h"
48 #include "lpfc_crtn.h"
49 #include "lpfc_debugfs.h"
50 #include "lpfc_vport.h"
51 #include "lpfc_version.h"
52
53 struct lpfc_bsg_event {
54         struct list_head node;
55         struct kref kref;
56         wait_queue_head_t wq;
57
58         /* Event type and waiter identifiers */
59         uint32_t type_mask;
60         uint32_t req_id;
61         uint32_t reg_id;
62
63         /* next two flags are here for the auto-delete logic */
64         unsigned long wait_time_stamp;
65         int waiting;
66
67         /* seen and not seen events */
68         struct list_head events_to_get;
69         struct list_head events_to_see;
70
71         /* driver data associated with the job */
72         void *dd_data;
73 };
74
75 struct lpfc_bsg_iocb {
76         struct lpfc_iocbq *cmdiocbq;
77         struct lpfc_dmabuf *rmp;
78         struct lpfc_nodelist *ndlp;
79 };
80
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 */
89 };
90
91 #define MENLO_DID 0x0000FC0E
92
93 struct lpfc_bsg_menlo {
94         struct lpfc_iocbq *cmdiocbq;
95         struct lpfc_dmabuf *rmp;
96 };
97
98 #define TYPE_EVT        1
99 #define TYPE_IOCB       2
100 #define TYPE_MBOX       3
101 #define TYPE_MENLO      4
102 struct bsg_job_data {
103         uint32_t type;
104         struct bsg_job *set_job; /* job waiting for this iocb to finish */
105         union {
106                 struct lpfc_bsg_event *evt;
107                 struct lpfc_bsg_iocb iocb;
108                 struct lpfc_bsg_mbox mbox;
109                 struct lpfc_bsg_menlo menlo;
110         } context_un;
111 };
112
113 struct event_data {
114         struct list_head node;
115         uint32_t type;
116         uint32_t immed_dat;
117         void *data;
118         uint32_t len;
119 };
120
121 #define BUF_SZ_4K 4096
122 #define SLI_CT_ELX_LOOPBACK 0x10
123
124 enum ELX_LOOPBACK_CMD {
125         ELX_LOOPBACK_XRI_SETUP,
126         ELX_LOOPBACK_DATA,
127 };
128
129 #define ELX_LOOPBACK_HEADER_SZ \
130         (size_t)(&((struct lpfc_sli_ct_request *)NULL)->un)
131
132 struct lpfc_dmabufext {
133         struct lpfc_dmabuf dma;
134         uint32_t size;
135         uint32_t flag;
136 };
137
138 static void
139 lpfc_free_bsg_buffers(struct lpfc_hba *phba, struct lpfc_dmabuf *mlist)
140 {
141         struct lpfc_dmabuf *mlast, *next_mlast;
142
143         if (mlist) {
144                 list_for_each_entry_safe(mlast, next_mlast, &mlist->list,
145                                          list) {
146                         lpfc_mbuf_free(phba, mlast->virt, mlast->phys);
147                         list_del(&mlast->list);
148                         kfree(mlast);
149                 }
150                 lpfc_mbuf_free(phba, mlist->virt, mlist->phys);
151                 kfree(mlist);
152         }
153         return;
154 }
155
156 static struct lpfc_dmabuf *
157 lpfc_alloc_bsg_buffers(struct lpfc_hba *phba, unsigned int size,
158                        int outbound_buffers, struct ulp_bde64 *bpl,
159                        int *bpl_entries)
160 {
161         struct lpfc_dmabuf *mlist = NULL;
162         struct lpfc_dmabuf *mp;
163         unsigned int bytes_left = size;
164
165         /* Verify we can support the size specified */
166         if (!size || (size > (*bpl_entries * LPFC_BPL_SIZE)))
167                 return NULL;
168
169         /* Determine the number of dma buffers to allocate */
170         *bpl_entries = (size % LPFC_BPL_SIZE ? size/LPFC_BPL_SIZE + 1 :
171                         size/LPFC_BPL_SIZE);
172
173         /* Allocate dma buffer and place in BPL passed */
174         while (bytes_left) {
175                 /* Allocate dma buffer  */
176                 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
177                 if (!mp) {
178                         if (mlist)
179                                 lpfc_free_bsg_buffers(phba, mlist);
180                         return NULL;
181                 }
182
183                 INIT_LIST_HEAD(&mp->list);
184                 mp->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &(mp->phys));
185
186                 if (!mp->virt) {
187                         kfree(mp);
188                         if (mlist)
189                                 lpfc_free_bsg_buffers(phba, mlist);
190                         return NULL;
191                 }
192
193                 /* Queue it to a linked list */
194                 if (!mlist)
195                         mlist = mp;
196                 else
197                         list_add_tail(&mp->list, &mlist->list);
198
199                 /* Add buffer to buffer pointer list */
200                 if (outbound_buffers)
201                         bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
202                 else
203                         bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
204                 bpl->addrLow = le32_to_cpu(putPaddrLow(mp->phys));
205                 bpl->addrHigh = le32_to_cpu(putPaddrHigh(mp->phys));
206                 bpl->tus.f.bdeSize = (uint16_t)
207                         (bytes_left >= LPFC_BPL_SIZE ? LPFC_BPL_SIZE :
208                          bytes_left);
209                 bytes_left -= bpl->tus.f.bdeSize;
210                 bpl->tus.w = le32_to_cpu(bpl->tus.w);
211                 bpl++;
212         }
213         return mlist;
214 }
215
216 static unsigned int
217 lpfc_bsg_copy_data(struct lpfc_dmabuf *dma_buffers,
218                    struct bsg_buffer *bsg_buffers,
219                    unsigned int bytes_to_transfer, int to_buffers)
220 {
221
222         struct lpfc_dmabuf *mp;
223         unsigned int transfer_bytes, bytes_copied = 0;
224         unsigned int sg_offset, dma_offset;
225         unsigned char *dma_address, *sg_address;
226         LIST_HEAD(temp_list);
227         struct sg_mapping_iter miter;
228         unsigned long flags;
229         unsigned int sg_flags = SG_MITER_ATOMIC;
230         bool sg_valid;
231
232         list_splice_init(&dma_buffers->list, &temp_list);
233         list_add(&dma_buffers->list, &temp_list);
234         sg_offset = 0;
235         if (to_buffers)
236                 sg_flags |= SG_MITER_FROM_SG;
237         else
238                 sg_flags |= SG_MITER_TO_SG;
239         sg_miter_start(&miter, bsg_buffers->sg_list, bsg_buffers->sg_cnt,
240                        sg_flags);
241         local_irq_save(flags);
242         sg_valid = sg_miter_next(&miter);
243         list_for_each_entry(mp, &temp_list, list) {
244                 dma_offset = 0;
245                 while (bytes_to_transfer && sg_valid &&
246                        (dma_offset < LPFC_BPL_SIZE)) {
247                         dma_address = mp->virt + dma_offset;
248                         if (sg_offset) {
249                                 /* Continue previous partial transfer of sg */
250                                 sg_address = miter.addr + sg_offset;
251                                 transfer_bytes = miter.length - sg_offset;
252                         } else {
253                                 sg_address = miter.addr;
254                                 transfer_bytes = miter.length;
255                         }
256                         if (bytes_to_transfer < transfer_bytes)
257                                 transfer_bytes = bytes_to_transfer;
258                         if (transfer_bytes > (LPFC_BPL_SIZE - dma_offset))
259                                 transfer_bytes = LPFC_BPL_SIZE - dma_offset;
260                         if (to_buffers)
261                                 memcpy(dma_address, sg_address, transfer_bytes);
262                         else
263                                 memcpy(sg_address, dma_address, transfer_bytes);
264                         dma_offset += transfer_bytes;
265                         sg_offset += transfer_bytes;
266                         bytes_to_transfer -= transfer_bytes;
267                         bytes_copied += transfer_bytes;
268                         if (sg_offset >= miter.length) {
269                                 sg_offset = 0;
270                                 sg_valid = sg_miter_next(&miter);
271                         }
272                 }
273         }
274         sg_miter_stop(&miter);
275         local_irq_restore(flags);
276         list_del_init(&dma_buffers->list);
277         list_splice(&temp_list, &dma_buffers->list);
278         return bytes_copied;
279 }
280
281 /**
282  * lpfc_bsg_send_mgmt_cmd_cmp - lpfc_bsg_send_mgmt_cmd's completion handler
283  * @phba: Pointer to HBA context object.
284  * @cmdiocbq: Pointer to command iocb.
285  * @rspiocbq: Pointer to response iocb.
286  *
287  * This function is the completion handler for iocbs issued using
288  * lpfc_bsg_send_mgmt_cmd function. This function is called by the
289  * ring event handler function without any lock held. This function
290  * can be called from both worker thread context and interrupt
291  * context. This function also can be called from another thread which
292  * cleans up the SLI layer objects.
293  * This function copies the contents of the response iocb to the
294  * response iocb memory object provided by the caller of
295  * lpfc_sli_issue_iocb_wait and then wakes up the thread which
296  * sleeps for the iocb completion.
297  **/
298 static void
299 lpfc_bsg_send_mgmt_cmd_cmp(struct lpfc_hba *phba,
300                         struct lpfc_iocbq *cmdiocbq,
301                         struct lpfc_iocbq *rspiocbq)
302 {
303         struct bsg_job_data *dd_data;
304         struct bsg_job *job;
305         struct fc_bsg_reply *bsg_reply;
306         IOCB_t *rsp;
307         struct lpfc_dmabuf *bmp, *cmp, *rmp;
308         struct lpfc_nodelist *ndlp;
309         struct lpfc_bsg_iocb *iocb;
310         unsigned long flags;
311         unsigned int rsp_size;
312         int rc = 0;
313
314         dd_data = cmdiocbq->context1;
315
316         /* Determine if job has been aborted */
317         spin_lock_irqsave(&phba->ct_ev_lock, flags);
318         job = dd_data->set_job;
319         if (job) {
320                 bsg_reply = job->reply;
321                 /* Prevent timeout handling from trying to abort job */
322                 job->dd_data = NULL;
323         }
324         spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
325
326         /* Close the timeout handler abort window */
327         spin_lock_irqsave(&phba->hbalock, flags);
328         cmdiocbq->iocb_flag &= ~LPFC_IO_CMD_OUTSTANDING;
329         spin_unlock_irqrestore(&phba->hbalock, flags);
330
331         iocb = &dd_data->context_un.iocb;
332         ndlp = iocb->ndlp;
333         rmp = iocb->rmp;
334         cmp = cmdiocbq->context2;
335         bmp = cmdiocbq->context3;
336         rsp = &rspiocbq->iocb;
337
338         /* Copy the completed data or set the error status */
339
340         if (job) {
341                 if (rsp->ulpStatus) {
342                         if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
343                                 switch (rsp->un.ulpWord[4] & IOERR_PARAM_MASK) {
344                                 case IOERR_SEQUENCE_TIMEOUT:
345                                         rc = -ETIMEDOUT;
346                                         break;
347                                 case IOERR_INVALID_RPI:
348                                         rc = -EFAULT;
349                                         break;
350                                 default:
351                                         rc = -EACCES;
352                                         break;
353                                 }
354                         } else {
355                                 rc = -EACCES;
356                         }
357                 } else {
358                         rsp_size = rsp->un.genreq64.bdl.bdeSize;
359                         bsg_reply->reply_payload_rcv_len =
360                                 lpfc_bsg_copy_data(rmp, &job->reply_payload,
361                                                    rsp_size, 0);
362                 }
363         }
364
365         lpfc_free_bsg_buffers(phba, cmp);
366         lpfc_free_bsg_buffers(phba, rmp);
367         lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
368         kfree(bmp);
369         lpfc_sli_release_iocbq(phba, cmdiocbq);
370         lpfc_nlp_put(ndlp);
371         kfree(dd_data);
372
373         /* Complete the job if the job is still active */
374
375         if (job) {
376                 bsg_reply->result = rc;
377                 bsg_job_done(job, bsg_reply->result,
378                                bsg_reply->reply_payload_rcv_len);
379         }
380         return;
381 }
382
383 /**
384  * lpfc_bsg_send_mgmt_cmd - send a CT command from a bsg request
385  * @job: fc_bsg_job to handle
386  **/
387 static int
388 lpfc_bsg_send_mgmt_cmd(struct bsg_job *job)
389 {
390         struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
391         struct lpfc_hba *phba = vport->phba;
392         struct lpfc_rport_data *rdata = fc_bsg_to_rport(job)->dd_data;
393         struct lpfc_nodelist *ndlp = rdata->pnode;
394         struct fc_bsg_reply *bsg_reply = job->reply;
395         struct ulp_bde64 *bpl = NULL;
396         uint32_t timeout;
397         struct lpfc_iocbq *cmdiocbq = NULL;
398         IOCB_t *cmd;
399         struct lpfc_dmabuf *bmp = NULL, *cmp = NULL, *rmp = NULL;
400         int request_nseg;
401         int reply_nseg;
402         struct bsg_job_data *dd_data;
403         unsigned long flags;
404         uint32_t creg_val;
405         int rc = 0;
406         int iocb_stat;
407
408         /* in case no data is transferred */
409         bsg_reply->reply_payload_rcv_len = 0;
410
411         /* allocate our bsg tracking structure */
412         dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
413         if (!dd_data) {
414                 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
415                                 "2733 Failed allocation of dd_data\n");
416                 rc = -ENOMEM;
417                 goto no_dd_data;
418         }
419
420         if (!lpfc_nlp_get(ndlp)) {
421                 rc = -ENODEV;
422                 goto no_ndlp;
423         }
424
425         if (ndlp->nlp_flag & NLP_ELS_SND_MASK) {
426                 rc = -ENODEV;
427                 goto free_ndlp;
428         }
429
430         cmdiocbq = lpfc_sli_get_iocbq(phba);
431         if (!cmdiocbq) {
432                 rc = -ENOMEM;
433                 goto free_ndlp;
434         }
435
436         cmd = &cmdiocbq->iocb;
437
438         bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
439         if (!bmp) {
440                 rc = -ENOMEM;
441                 goto free_cmdiocbq;
442         }
443         bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
444         if (!bmp->virt) {
445                 rc = -ENOMEM;
446                 goto free_bmp;
447         }
448
449         INIT_LIST_HEAD(&bmp->list);
450
451         bpl = (struct ulp_bde64 *) bmp->virt;
452         request_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64);
453         cmp = lpfc_alloc_bsg_buffers(phba, job->request_payload.payload_len,
454                                      1, bpl, &request_nseg);
455         if (!cmp) {
456                 rc = -ENOMEM;
457                 goto free_bmp;
458         }
459         lpfc_bsg_copy_data(cmp, &job->request_payload,
460                            job->request_payload.payload_len, 1);
461
462         bpl += request_nseg;
463         reply_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64) - request_nseg;
464         rmp = lpfc_alloc_bsg_buffers(phba, job->reply_payload.payload_len, 0,
465                                      bpl, &reply_nseg);
466         if (!rmp) {
467                 rc = -ENOMEM;
468                 goto free_cmp;
469         }
470
471         cmd->un.genreq64.bdl.ulpIoTag32 = 0;
472         cmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
473         cmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
474         cmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
475         cmd->un.genreq64.bdl.bdeSize =
476                 (request_nseg + reply_nseg) * sizeof(struct ulp_bde64);
477         cmd->ulpCommand = CMD_GEN_REQUEST64_CR;
478         cmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
479         cmd->un.genreq64.w5.hcsw.Dfctl = 0;
480         cmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
481         cmd->un.genreq64.w5.hcsw.Type = FC_TYPE_CT;
482         cmd->ulpBdeCount = 1;
483         cmd->ulpLe = 1;
484         cmd->ulpClass = CLASS3;
485         cmd->ulpContext = ndlp->nlp_rpi;
486         if (phba->sli_rev == LPFC_SLI_REV4)
487                 cmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
488         cmd->ulpOwner = OWN_CHIP;
489         cmdiocbq->vport = phba->pport;
490         cmdiocbq->context3 = bmp;
491         cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
492         timeout = phba->fc_ratov * 2;
493         cmd->ulpTimeout = timeout;
494
495         cmdiocbq->iocb_cmpl = lpfc_bsg_send_mgmt_cmd_cmp;
496         cmdiocbq->context1 = dd_data;
497         cmdiocbq->context2 = cmp;
498         cmdiocbq->context3 = bmp;
499         cmdiocbq->context_un.ndlp = ndlp;
500         dd_data->type = TYPE_IOCB;
501         dd_data->set_job = job;
502         dd_data->context_un.iocb.cmdiocbq = cmdiocbq;
503         dd_data->context_un.iocb.ndlp = ndlp;
504         dd_data->context_un.iocb.rmp = rmp;
505         job->dd_data = dd_data;
506
507         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
508                 if (lpfc_readl(phba->HCregaddr, &creg_val)) {
509                         rc = -EIO ;
510                         goto free_rmp;
511                 }
512                 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
513                 writel(creg_val, phba->HCregaddr);
514                 readl(phba->HCregaddr); /* flush */
515         }
516
517         iocb_stat = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq, 0);
518
519         if (iocb_stat == IOCB_SUCCESS) {
520                 spin_lock_irqsave(&phba->hbalock, flags);
521                 /* make sure the I/O had not been completed yet */
522                 if (cmdiocbq->iocb_flag & LPFC_IO_LIBDFC) {
523                         /* open up abort window to timeout handler */
524                         cmdiocbq->iocb_flag |= LPFC_IO_CMD_OUTSTANDING;
525                 }
526                 spin_unlock_irqrestore(&phba->hbalock, flags);
527                 return 0; /* done for now */
528         } else if (iocb_stat == IOCB_BUSY) {
529                 rc = -EAGAIN;
530         } else {
531                 rc = -EIO;
532         }
533
534         /* iocb failed so cleanup */
535         job->dd_data = NULL;
536
537 free_rmp:
538         lpfc_free_bsg_buffers(phba, rmp);
539 free_cmp:
540         lpfc_free_bsg_buffers(phba, cmp);
541 free_bmp:
542         if (bmp->virt)
543                 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
544         kfree(bmp);
545 free_cmdiocbq:
546         lpfc_sli_release_iocbq(phba, cmdiocbq);
547 free_ndlp:
548         lpfc_nlp_put(ndlp);
549 no_ndlp:
550         kfree(dd_data);
551 no_dd_data:
552         /* make error code available to userspace */
553         bsg_reply->result = rc;
554         job->dd_data = NULL;
555         return rc;
556 }
557
558 /**
559  * lpfc_bsg_rport_els_cmp - lpfc_bsg_rport_els's completion handler
560  * @phba: Pointer to HBA context object.
561  * @cmdiocbq: Pointer to command iocb.
562  * @rspiocbq: Pointer to response iocb.
563  *
564  * This function is the completion handler for iocbs issued using
565  * lpfc_bsg_rport_els_cmp function. This function is called by the
566  * ring event handler function without any lock held. This function
567  * can be called from both worker thread context and interrupt
568  * context. This function also can be called from other thread which
569  * cleans up the SLI layer objects.
570  * This function copies the contents of the response iocb to the
571  * response iocb memory object provided by the caller of
572  * lpfc_sli_issue_iocb_wait and then wakes up the thread which
573  * sleeps for the iocb completion.
574  **/
575 static void
576 lpfc_bsg_rport_els_cmp(struct lpfc_hba *phba,
577                         struct lpfc_iocbq *cmdiocbq,
578                         struct lpfc_iocbq *rspiocbq)
579 {
580         struct bsg_job_data *dd_data;
581         struct bsg_job *job;
582         struct fc_bsg_reply *bsg_reply;
583         IOCB_t *rsp;
584         struct lpfc_nodelist *ndlp;
585         struct lpfc_dmabuf *pcmd = NULL, *prsp = NULL;
586         struct fc_bsg_ctels_reply *els_reply;
587         uint8_t *rjt_data;
588         unsigned long flags;
589         unsigned int rsp_size;
590         int rc = 0;
591
592         dd_data = cmdiocbq->context1;
593         ndlp = dd_data->context_un.iocb.ndlp;
594         cmdiocbq->context1 = ndlp;
595
596         /* Determine if job has been aborted */
597         spin_lock_irqsave(&phba->ct_ev_lock, flags);
598         job = dd_data->set_job;
599         if (job) {
600                 bsg_reply = job->reply;
601                 /* Prevent timeout handling from trying to abort job  */
602                 job->dd_data = NULL;
603         }
604         spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
605
606         /* Close the timeout handler abort window */
607         spin_lock_irqsave(&phba->hbalock, flags);
608         cmdiocbq->iocb_flag &= ~LPFC_IO_CMD_OUTSTANDING;
609         spin_unlock_irqrestore(&phba->hbalock, flags);
610
611         rsp = &rspiocbq->iocb;
612         pcmd = (struct lpfc_dmabuf *)cmdiocbq->context2;
613         prsp = (struct lpfc_dmabuf *)pcmd->list.next;
614
615         /* Copy the completed job data or determine the job status if job is
616          * still active
617          */
618
619         if (job) {
620                 if (rsp->ulpStatus == IOSTAT_SUCCESS) {
621                         rsp_size = rsp->un.elsreq64.bdl.bdeSize;
622                         bsg_reply->reply_payload_rcv_len =
623                                 sg_copy_from_buffer(job->reply_payload.sg_list,
624                                                     job->reply_payload.sg_cnt,
625                                                     prsp->virt,
626                                                     rsp_size);
627                 } else if (rsp->ulpStatus == IOSTAT_LS_RJT) {
628                         bsg_reply->reply_payload_rcv_len =
629                                 sizeof(struct fc_bsg_ctels_reply);
630                         /* LS_RJT data returned in word 4 */
631                         rjt_data = (uint8_t *)&rsp->un.ulpWord[4];
632                         els_reply = &bsg_reply->reply_data.ctels_reply;
633                         els_reply->status = FC_CTELS_STATUS_REJECT;
634                         els_reply->rjt_data.action = rjt_data[3];
635                         els_reply->rjt_data.reason_code = rjt_data[2];
636                         els_reply->rjt_data.reason_explanation = rjt_data[1];
637                         els_reply->rjt_data.vendor_unique = rjt_data[0];
638                 } else {
639                         rc = -EIO;
640                 }
641         }
642
643         lpfc_nlp_put(ndlp);
644         lpfc_els_free_iocb(phba, cmdiocbq);
645         kfree(dd_data);
646
647         /* Complete the job if the job is still active */
648
649         if (job) {
650                 bsg_reply->result = rc;
651                 bsg_job_done(job, bsg_reply->result,
652                                bsg_reply->reply_payload_rcv_len);
653         }
654         return;
655 }
656
657 /**
658  * lpfc_bsg_rport_els - send an ELS command from a bsg request
659  * @job: fc_bsg_job to handle
660  **/
661 static int
662 lpfc_bsg_rport_els(struct bsg_job *job)
663 {
664         struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
665         struct lpfc_hba *phba = vport->phba;
666         struct lpfc_rport_data *rdata = fc_bsg_to_rport(job)->dd_data;
667         struct lpfc_nodelist *ndlp = rdata->pnode;
668         struct fc_bsg_request *bsg_request = job->request;
669         struct fc_bsg_reply *bsg_reply = job->reply;
670         uint32_t elscmd;
671         uint32_t cmdsize;
672         struct lpfc_iocbq *cmdiocbq;
673         uint16_t rpi = 0;
674         struct bsg_job_data *dd_data;
675         unsigned long flags;
676         uint32_t creg_val;
677         int rc = 0;
678
679         /* in case no data is transferred */
680         bsg_reply->reply_payload_rcv_len = 0;
681
682         /* verify the els command is not greater than the
683          * maximum ELS transfer size.
684          */
685
686         if (job->request_payload.payload_len > FCELSSIZE) {
687                 rc = -EINVAL;
688                 goto no_dd_data;
689         }
690
691         /* allocate our bsg tracking structure */
692         dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
693         if (!dd_data) {
694                 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
695                                 "2735 Failed allocation of dd_data\n");
696                 rc = -ENOMEM;
697                 goto no_dd_data;
698         }
699
700         elscmd = bsg_request->rqst_data.r_els.els_code;
701         cmdsize = job->request_payload.payload_len;
702
703         if (!lpfc_nlp_get(ndlp)) {
704                 rc = -ENODEV;
705                 goto free_dd_data;
706         }
707
708         /* We will use the allocated dma buffers by prep els iocb for command
709          * and response to ensure if the job times out and the request is freed,
710          * we won't be dma into memory that is no longer allocated to for the
711          * request.
712          */
713
714         cmdiocbq = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp,
715                                       ndlp->nlp_DID, elscmd);
716         if (!cmdiocbq) {
717                 rc = -EIO;
718                 goto release_ndlp;
719         }
720
721         rpi = ndlp->nlp_rpi;
722
723         /* Transfer the request payload to allocated command dma buffer */
724
725         sg_copy_to_buffer(job->request_payload.sg_list,
726                           job->request_payload.sg_cnt,
727                           ((struct lpfc_dmabuf *)cmdiocbq->context2)->virt,
728                           cmdsize);
729
730         if (phba->sli_rev == LPFC_SLI_REV4)
731                 cmdiocbq->iocb.ulpContext = phba->sli4_hba.rpi_ids[rpi];
732         else
733                 cmdiocbq->iocb.ulpContext = rpi;
734         cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
735         cmdiocbq->context1 = dd_data;
736         cmdiocbq->context_un.ndlp = ndlp;
737         cmdiocbq->iocb_cmpl = lpfc_bsg_rport_els_cmp;
738         dd_data->type = TYPE_IOCB;
739         dd_data->set_job = job;
740         dd_data->context_un.iocb.cmdiocbq = cmdiocbq;
741         dd_data->context_un.iocb.ndlp = ndlp;
742         dd_data->context_un.iocb.rmp = NULL;
743         job->dd_data = dd_data;
744
745         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
746                 if (lpfc_readl(phba->HCregaddr, &creg_val)) {
747                         rc = -EIO;
748                         goto linkdown_err;
749                 }
750                 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
751                 writel(creg_val, phba->HCregaddr);
752                 readl(phba->HCregaddr); /* flush */
753         }
754
755         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq, 0);
756
757         if (rc == IOCB_SUCCESS) {
758                 spin_lock_irqsave(&phba->hbalock, flags);
759                 /* make sure the I/O had not been completed/released */
760                 if (cmdiocbq->iocb_flag & LPFC_IO_LIBDFC) {
761                         /* open up abort window to timeout handler */
762                         cmdiocbq->iocb_flag |= LPFC_IO_CMD_OUTSTANDING;
763                 }
764                 spin_unlock_irqrestore(&phba->hbalock, flags);
765                 return 0; /* done for now */
766         } else if (rc == IOCB_BUSY) {
767                 rc = -EAGAIN;
768         } else {
769                 rc = -EIO;
770         }
771
772         /* iocb failed so cleanup */
773         job->dd_data = NULL;
774
775 linkdown_err:
776         cmdiocbq->context1 = ndlp;
777         lpfc_els_free_iocb(phba, cmdiocbq);
778
779 release_ndlp:
780         lpfc_nlp_put(ndlp);
781
782 free_dd_data:
783         kfree(dd_data);
784
785 no_dd_data:
786         /* make error code available to userspace */
787         bsg_reply->result = rc;
788         job->dd_data = NULL;
789         return rc;
790 }
791
792 /**
793  * lpfc_bsg_event_free - frees an allocated event structure
794  * @kref: Pointer to a kref.
795  *
796  * Called from kref_put. Back cast the kref into an event structure address.
797  * Free any events to get, delete associated nodes, free any events to see,
798  * free any data then free the event itself.
799  **/
800 static void
801 lpfc_bsg_event_free(struct kref *kref)
802 {
803         struct lpfc_bsg_event *evt = container_of(kref, struct lpfc_bsg_event,
804                                                   kref);
805         struct event_data *ed;
806
807         list_del(&evt->node);
808
809         while (!list_empty(&evt->events_to_get)) {
810                 ed = list_entry(evt->events_to_get.next, typeof(*ed), node);
811                 list_del(&ed->node);
812                 kfree(ed->data);
813                 kfree(ed);
814         }
815
816         while (!list_empty(&evt->events_to_see)) {
817                 ed = list_entry(evt->events_to_see.next, typeof(*ed), node);
818                 list_del(&ed->node);
819                 kfree(ed->data);
820                 kfree(ed);
821         }
822
823         kfree(evt->dd_data);
824         kfree(evt);
825 }
826
827 /**
828  * lpfc_bsg_event_ref - increments the kref for an event
829  * @evt: Pointer to an event structure.
830  **/
831 static inline void
832 lpfc_bsg_event_ref(struct lpfc_bsg_event *evt)
833 {
834         kref_get(&evt->kref);
835 }
836
837 /**
838  * lpfc_bsg_event_unref - Uses kref_put to free an event structure
839  * @evt: Pointer to an event structure.
840  **/
841 static inline void
842 lpfc_bsg_event_unref(struct lpfc_bsg_event *evt)
843 {
844         kref_put(&evt->kref, lpfc_bsg_event_free);
845 }
846
847 /**
848  * lpfc_bsg_event_new - allocate and initialize a event structure
849  * @ev_mask: Mask of events.
850  * @ev_reg_id: Event reg id.
851  * @ev_req_id: Event request id.
852  **/
853 static struct lpfc_bsg_event *
854 lpfc_bsg_event_new(uint32_t ev_mask, int ev_reg_id, uint32_t ev_req_id)
855 {
856         struct lpfc_bsg_event *evt = kzalloc(sizeof(*evt), GFP_KERNEL);
857
858         if (!evt)
859                 return NULL;
860
861         INIT_LIST_HEAD(&evt->events_to_get);
862         INIT_LIST_HEAD(&evt->events_to_see);
863         evt->type_mask = ev_mask;
864         evt->req_id = ev_req_id;
865         evt->reg_id = ev_reg_id;
866         evt->wait_time_stamp = jiffies;
867         evt->dd_data = NULL;
868         init_waitqueue_head(&evt->wq);
869         kref_init(&evt->kref);
870         return evt;
871 }
872
873 /**
874  * diag_cmd_data_free - Frees an lpfc dma buffer extension
875  * @phba: Pointer to HBA context object.
876  * @mlist: Pointer to an lpfc dma buffer extension.
877  **/
878 static int
879 diag_cmd_data_free(struct lpfc_hba *phba, struct lpfc_dmabufext *mlist)
880 {
881         struct lpfc_dmabufext *mlast;
882         struct pci_dev *pcidev;
883         struct list_head head, *curr, *next;
884
885         if ((!mlist) || (!lpfc_is_link_up(phba) &&
886                 (phba->link_flag & LS_LOOPBACK_MODE))) {
887                 return 0;
888         }
889
890         pcidev = phba->pcidev;
891         list_add_tail(&head, &mlist->dma.list);
892
893         list_for_each_safe(curr, next, &head) {
894                 mlast = list_entry(curr, struct lpfc_dmabufext , dma.list);
895                 if (mlast->dma.virt)
896                         dma_free_coherent(&pcidev->dev,
897                                           mlast->size,
898                                           mlast->dma.virt,
899                                           mlast->dma.phys);
900                 kfree(mlast);
901         }
902         return 0;
903 }
904
905 /**
906  * lpfc_bsg_ct_unsol_event - process an unsolicited CT command
907  * @phba:
908  * @pring:
909  * @piocbq:
910  *
911  * This function is called when an unsolicited CT command is received.  It
912  * forwards the event to any processes registered to receive CT events.
913  **/
914 int
915 lpfc_bsg_ct_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
916                         struct lpfc_iocbq *piocbq)
917 {
918         uint32_t evt_req_id = 0;
919         uint32_t cmd;
920         struct lpfc_dmabuf *dmabuf = NULL;
921         struct lpfc_bsg_event *evt;
922         struct event_data *evt_dat = NULL;
923         struct lpfc_iocbq *iocbq;
924         size_t offset = 0;
925         struct list_head head;
926         struct ulp_bde64 *bde;
927         dma_addr_t dma_addr;
928         int i;
929         struct lpfc_dmabuf *bdeBuf1 = piocbq->context2;
930         struct lpfc_dmabuf *bdeBuf2 = piocbq->context3;
931         struct lpfc_hbq_entry *hbqe;
932         struct lpfc_sli_ct_request *ct_req;
933         struct bsg_job *job = NULL;
934         struct fc_bsg_reply *bsg_reply;
935         struct bsg_job_data *dd_data = NULL;
936         unsigned long flags;
937         int size = 0;
938
939         INIT_LIST_HEAD(&head);
940         list_add_tail(&head, &piocbq->list);
941
942         if (piocbq->iocb.ulpBdeCount == 0 ||
943             piocbq->iocb.un.cont64[0].tus.f.bdeSize == 0)
944                 goto error_ct_unsol_exit;
945
946         if (phba->link_state == LPFC_HBA_ERROR ||
947                 (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)))
948                 goto error_ct_unsol_exit;
949
950         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
951                 dmabuf = bdeBuf1;
952         else {
953                 dma_addr = getPaddr(piocbq->iocb.un.cont64[0].addrHigh,
954                                     piocbq->iocb.un.cont64[0].addrLow);
955                 dmabuf = lpfc_sli_ringpostbuf_get(phba, pring, dma_addr);
956         }
957         if (dmabuf == NULL)
958                 goto error_ct_unsol_exit;
959         ct_req = (struct lpfc_sli_ct_request *)dmabuf->virt;
960         evt_req_id = ct_req->FsType;
961         cmd = ct_req->CommandResponse.bits.CmdRsp;
962         if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
963                 lpfc_sli_ringpostbuf_put(phba, pring, dmabuf);
964
965         spin_lock_irqsave(&phba->ct_ev_lock, flags);
966         list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
967                 if (!(evt->type_mask & FC_REG_CT_EVENT) ||
968                         evt->req_id != evt_req_id)
969                         continue;
970
971                 lpfc_bsg_event_ref(evt);
972                 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
973                 evt_dat = kzalloc(sizeof(*evt_dat), GFP_KERNEL);
974                 if (evt_dat == NULL) {
975                         spin_lock_irqsave(&phba->ct_ev_lock, flags);
976                         lpfc_bsg_event_unref(evt);
977                         lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
978                                         "2614 Memory allocation failed for "
979                                         "CT event\n");
980                         break;
981                 }
982
983                 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
984                         /* take accumulated byte count from the last iocbq */
985                         iocbq = list_entry(head.prev, typeof(*iocbq), list);
986                         evt_dat->len = iocbq->iocb.unsli3.rcvsli3.acc_len;
987                 } else {
988                         list_for_each_entry(iocbq, &head, list) {
989                                 for (i = 0; i < iocbq->iocb.ulpBdeCount; i++)
990                                         evt_dat->len +=
991                                         iocbq->iocb.un.cont64[i].tus.f.bdeSize;
992                         }
993                 }
994
995                 evt_dat->data = kzalloc(evt_dat->len, GFP_KERNEL);
996                 if (evt_dat->data == NULL) {
997                         lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
998                                         "2615 Memory allocation failed for "
999                                         "CT event data, size %d\n",
1000                                         evt_dat->len);
1001                         kfree(evt_dat);
1002                         spin_lock_irqsave(&phba->ct_ev_lock, flags);
1003                         lpfc_bsg_event_unref(evt);
1004                         spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1005                         goto error_ct_unsol_exit;
1006                 }
1007
1008                 list_for_each_entry(iocbq, &head, list) {
1009                         size = 0;
1010                         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
1011                                 bdeBuf1 = iocbq->context2;
1012                                 bdeBuf2 = iocbq->context3;
1013                         }
1014                         for (i = 0; i < iocbq->iocb.ulpBdeCount; i++) {
1015                                 if (phba->sli3_options &
1016                                     LPFC_SLI3_HBQ_ENABLED) {
1017                                         if (i == 0) {
1018                                                 hbqe = (struct lpfc_hbq_entry *)
1019                                                   &iocbq->iocb.un.ulpWord[0];
1020                                                 size = hbqe->bde.tus.f.bdeSize;
1021                                                 dmabuf = bdeBuf1;
1022                                         } else if (i == 1) {
1023                                                 hbqe = (struct lpfc_hbq_entry *)
1024                                                         &iocbq->iocb.unsli3.
1025                                                         sli3Words[4];
1026                                                 size = hbqe->bde.tus.f.bdeSize;
1027                                                 dmabuf = bdeBuf2;
1028                                         }
1029                                         if ((offset + size) > evt_dat->len)
1030                                                 size = evt_dat->len - offset;
1031                                 } else {
1032                                         size = iocbq->iocb.un.cont64[i].
1033                                                 tus.f.bdeSize;
1034                                         bde = &iocbq->iocb.un.cont64[i];
1035                                         dma_addr = getPaddr(bde->addrHigh,
1036                                                             bde->addrLow);
1037                                         dmabuf = lpfc_sli_ringpostbuf_get(phba,
1038                                                         pring, dma_addr);
1039                                 }
1040                                 if (!dmabuf) {
1041                                         lpfc_printf_log(phba, KERN_ERR,
1042                                                 LOG_LIBDFC, "2616 No dmabuf "
1043                                                 "found for iocbq x%px\n",
1044                                                 iocbq);
1045                                         kfree(evt_dat->data);
1046                                         kfree(evt_dat);
1047                                         spin_lock_irqsave(&phba->ct_ev_lock,
1048                                                 flags);
1049                                         lpfc_bsg_event_unref(evt);
1050                                         spin_unlock_irqrestore(
1051                                                 &phba->ct_ev_lock, flags);
1052                                         goto error_ct_unsol_exit;
1053                                 }
1054                                 memcpy((char *)(evt_dat->data) + offset,
1055                                        dmabuf->virt, size);
1056                                 offset += size;
1057                                 if (evt_req_id != SLI_CT_ELX_LOOPBACK &&
1058                                     !(phba->sli3_options &
1059                                       LPFC_SLI3_HBQ_ENABLED)) {
1060                                         lpfc_sli_ringpostbuf_put(phba, pring,
1061                                                                  dmabuf);
1062                                 } else {
1063                                         switch (cmd) {
1064                                         case ELX_LOOPBACK_DATA:
1065                                                 if (phba->sli_rev <
1066                                                     LPFC_SLI_REV4)
1067                                                         diag_cmd_data_free(phba,
1068                                                         (struct lpfc_dmabufext
1069                                                          *)dmabuf);
1070                                                 break;
1071                                         case ELX_LOOPBACK_XRI_SETUP:
1072                                                 if ((phba->sli_rev ==
1073                                                         LPFC_SLI_REV2) ||
1074                                                         (phba->sli3_options &
1075                                                         LPFC_SLI3_HBQ_ENABLED
1076                                                         )) {
1077                                                         lpfc_in_buf_free(phba,
1078                                                                         dmabuf);
1079                                                 } else {
1080                                                         lpfc_post_buffer(phba,
1081                                                                          pring,
1082                                                                          1);
1083                                                 }
1084                                                 break;
1085                                         default:
1086                                                 if (!(phba->sli3_options &
1087                                                       LPFC_SLI3_HBQ_ENABLED))
1088                                                         lpfc_post_buffer(phba,
1089                                                                          pring,
1090                                                                          1);
1091                                                 break;
1092                                         }
1093                                 }
1094                         }
1095                 }
1096
1097                 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1098                 if (phba->sli_rev == LPFC_SLI_REV4) {
1099                         evt_dat->immed_dat = phba->ctx_idx;
1100                         phba->ctx_idx = (phba->ctx_idx + 1) % LPFC_CT_CTX_MAX;
1101                         /* Provide warning for over-run of the ct_ctx array */
1102                         if (phba->ct_ctx[evt_dat->immed_dat].valid ==
1103                             UNSOL_VALID)
1104                                 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
1105                                                 "2717 CT context array entry "
1106                                                 "[%d] over-run: oxid:x%x, "
1107                                                 "sid:x%x\n", phba->ctx_idx,
1108                                                 phba->ct_ctx[
1109                                                     evt_dat->immed_dat].oxid,
1110                                                 phba->ct_ctx[
1111                                                     evt_dat->immed_dat].SID);
1112                         phba->ct_ctx[evt_dat->immed_dat].rxid =
1113                                 piocbq->iocb.ulpContext;
1114                         phba->ct_ctx[evt_dat->immed_dat].oxid =
1115                                 piocbq->iocb.unsli3.rcvsli3.ox_id;
1116                         phba->ct_ctx[evt_dat->immed_dat].SID =
1117                                 piocbq->iocb.un.rcvels.remoteID;
1118                         phba->ct_ctx[evt_dat->immed_dat].valid = UNSOL_VALID;
1119                 } else
1120                         evt_dat->immed_dat = piocbq->iocb.ulpContext;
1121
1122                 evt_dat->type = FC_REG_CT_EVENT;
1123                 list_add(&evt_dat->node, &evt->events_to_see);
1124                 if (evt_req_id == SLI_CT_ELX_LOOPBACK) {
1125                         wake_up_interruptible(&evt->wq);
1126                         lpfc_bsg_event_unref(evt);
1127                         break;
1128                 }
1129
1130                 list_move(evt->events_to_see.prev, &evt->events_to_get);
1131
1132                 dd_data = (struct bsg_job_data *)evt->dd_data;
1133                 job = dd_data->set_job;
1134                 dd_data->set_job = NULL;
1135                 lpfc_bsg_event_unref(evt);
1136                 if (job) {
1137                         bsg_reply = job->reply;
1138                         bsg_reply->reply_payload_rcv_len = size;
1139                         /* make error code available to userspace */
1140                         bsg_reply->result = 0;
1141                         job->dd_data = NULL;
1142                         /* complete the job back to userspace */
1143                         spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1144                         bsg_job_done(job, bsg_reply->result,
1145                                        bsg_reply->reply_payload_rcv_len);
1146                         spin_lock_irqsave(&phba->ct_ev_lock, flags);
1147                 }
1148         }
1149         spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1150
1151 error_ct_unsol_exit:
1152         if (!list_empty(&head))
1153                 list_del(&head);
1154         if ((phba->sli_rev < LPFC_SLI_REV4) &&
1155             (evt_req_id == SLI_CT_ELX_LOOPBACK))
1156                 return 0;
1157         return 1;
1158 }
1159
1160 /**
1161  * lpfc_bsg_ct_unsol_abort - handler ct abort to management plane
1162  * @phba: Pointer to HBA context object.
1163  * @dmabuf: pointer to a dmabuf that describes the FC sequence
1164  *
1165  * This function handles abort to the CT command toward management plane
1166  * for SLI4 port.
1167  *
1168  * If the pending context of a CT command to management plane present, clears
1169  * such context and returns 1 for handled; otherwise, it returns 0 indicating
1170  * no context exists.
1171  **/
1172 int
1173 lpfc_bsg_ct_unsol_abort(struct lpfc_hba *phba, struct hbq_dmabuf *dmabuf)
1174 {
1175         struct fc_frame_header fc_hdr;
1176         struct fc_frame_header *fc_hdr_ptr = &fc_hdr;
1177         int ctx_idx, handled = 0;
1178         uint16_t oxid, rxid;
1179         uint32_t sid;
1180
1181         memcpy(fc_hdr_ptr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
1182         sid = sli4_sid_from_fc_hdr(fc_hdr_ptr);
1183         oxid = be16_to_cpu(fc_hdr_ptr->fh_ox_id);
1184         rxid = be16_to_cpu(fc_hdr_ptr->fh_rx_id);
1185
1186         for (ctx_idx = 0; ctx_idx < LPFC_CT_CTX_MAX; ctx_idx++) {
1187                 if (phba->ct_ctx[ctx_idx].valid != UNSOL_VALID)
1188                         continue;
1189                 if (phba->ct_ctx[ctx_idx].rxid != rxid)
1190                         continue;
1191                 if (phba->ct_ctx[ctx_idx].oxid != oxid)
1192                         continue;
1193                 if (phba->ct_ctx[ctx_idx].SID != sid)
1194                         continue;
1195                 phba->ct_ctx[ctx_idx].valid = UNSOL_INVALID;
1196                 handled = 1;
1197         }
1198         return handled;
1199 }
1200
1201 /**
1202  * lpfc_bsg_hba_set_event - process a SET_EVENT bsg vendor command
1203  * @job: SET_EVENT fc_bsg_job
1204  **/
1205 static int
1206 lpfc_bsg_hba_set_event(struct bsg_job *job)
1207 {
1208         struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
1209         struct lpfc_hba *phba = vport->phba;
1210         struct fc_bsg_request *bsg_request = job->request;
1211         struct set_ct_event *event_req;
1212         struct lpfc_bsg_event *evt;
1213         int rc = 0;
1214         struct bsg_job_data *dd_data = NULL;
1215         uint32_t ev_mask;
1216         unsigned long flags;
1217
1218         if (job->request_len <
1219             sizeof(struct fc_bsg_request) + sizeof(struct set_ct_event)) {
1220                 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1221                                 "2612 Received SET_CT_EVENT below minimum "
1222                                 "size\n");
1223                 rc = -EINVAL;
1224                 goto job_error;
1225         }
1226
1227         event_req = (struct set_ct_event *)
1228                 bsg_request->rqst_data.h_vendor.vendor_cmd;
1229         ev_mask = ((uint32_t)(unsigned long)event_req->type_mask &
1230                                 FC_REG_EVENT_MASK);
1231         spin_lock_irqsave(&phba->ct_ev_lock, flags);
1232         list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
1233                 if (evt->reg_id == event_req->ev_reg_id) {
1234                         lpfc_bsg_event_ref(evt);
1235                         evt->wait_time_stamp = jiffies;
1236                         dd_data = (struct bsg_job_data *)evt->dd_data;
1237                         break;
1238                 }
1239         }
1240         spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1241
1242         if (&evt->node == &phba->ct_ev_waiters) {
1243                 /* no event waiting struct yet - first call */
1244                 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
1245                 if (dd_data == NULL) {
1246                         lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1247                                         "2734 Failed allocation of dd_data\n");
1248                         rc = -ENOMEM;
1249                         goto job_error;
1250                 }
1251                 evt = lpfc_bsg_event_new(ev_mask, event_req->ev_reg_id,
1252                                         event_req->ev_req_id);
1253                 if (!evt) {
1254                         lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1255                                         "2617 Failed allocation of event "
1256                                         "waiter\n");
1257                         rc = -ENOMEM;
1258                         goto job_error;
1259                 }
1260                 dd_data->type = TYPE_EVT;
1261                 dd_data->set_job = NULL;
1262                 dd_data->context_un.evt = evt;
1263                 evt->dd_data = (void *)dd_data;
1264                 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1265                 list_add(&evt->node, &phba->ct_ev_waiters);
1266                 lpfc_bsg_event_ref(evt);
1267                 evt->wait_time_stamp = jiffies;
1268                 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1269         }
1270
1271         spin_lock_irqsave(&phba->ct_ev_lock, flags);
1272         evt->waiting = 1;
1273         dd_data->set_job = job; /* for unsolicited command */
1274         job->dd_data = dd_data; /* for fc transport timeout callback*/
1275         spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1276         return 0; /* call job done later */
1277
1278 job_error:
1279         kfree(dd_data);
1280         job->dd_data = NULL;
1281         return rc;
1282 }
1283
1284 /**
1285  * lpfc_bsg_hba_get_event - process a GET_EVENT bsg vendor command
1286  * @job: GET_EVENT fc_bsg_job
1287  **/
1288 static int
1289 lpfc_bsg_hba_get_event(struct bsg_job *job)
1290 {
1291         struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
1292         struct lpfc_hba *phba = vport->phba;
1293         struct fc_bsg_request *bsg_request = job->request;
1294         struct fc_bsg_reply *bsg_reply = job->reply;
1295         struct get_ct_event *event_req;
1296         struct get_ct_event_reply *event_reply;
1297         struct lpfc_bsg_event *evt, *evt_next;
1298         struct event_data *evt_dat = NULL;
1299         unsigned long flags;
1300         uint32_t rc = 0;
1301
1302         if (job->request_len <
1303             sizeof(struct fc_bsg_request) + sizeof(struct get_ct_event)) {
1304                 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1305                                 "2613 Received GET_CT_EVENT request below "
1306                                 "minimum size\n");
1307                 rc = -EINVAL;
1308                 goto job_error;
1309         }
1310
1311         event_req = (struct get_ct_event *)
1312                 bsg_request->rqst_data.h_vendor.vendor_cmd;
1313
1314         event_reply = (struct get_ct_event_reply *)
1315                 bsg_reply->reply_data.vendor_reply.vendor_rsp;
1316         spin_lock_irqsave(&phba->ct_ev_lock, flags);
1317         list_for_each_entry_safe(evt, evt_next, &phba->ct_ev_waiters, node) {
1318                 if (evt->reg_id == event_req->ev_reg_id) {
1319                         if (list_empty(&evt->events_to_get))
1320                                 break;
1321                         lpfc_bsg_event_ref(evt);
1322                         evt->wait_time_stamp = jiffies;
1323                         evt_dat = list_entry(evt->events_to_get.prev,
1324                                              struct event_data, node);
1325                         list_del(&evt_dat->node);
1326                         break;
1327                 }
1328         }
1329         spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1330
1331         /* The app may continue to ask for event data until it gets
1332          * an error indicating that there isn't anymore
1333          */
1334         if (evt_dat == NULL) {
1335                 bsg_reply->reply_payload_rcv_len = 0;
1336                 rc = -ENOENT;
1337                 goto job_error;
1338         }
1339
1340         if (evt_dat->len > job->request_payload.payload_len) {
1341                 evt_dat->len = job->request_payload.payload_len;
1342                 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1343                                 "2618 Truncated event data at %d "
1344                                 "bytes\n",
1345                                 job->request_payload.payload_len);
1346         }
1347
1348         event_reply->type = evt_dat->type;
1349         event_reply->immed_data = evt_dat->immed_dat;
1350         if (evt_dat->len > 0)
1351                 bsg_reply->reply_payload_rcv_len =
1352                         sg_copy_from_buffer(job->request_payload.sg_list,
1353                                             job->request_payload.sg_cnt,
1354                                             evt_dat->data, evt_dat->len);
1355         else
1356                 bsg_reply->reply_payload_rcv_len = 0;
1357
1358         if (evt_dat) {
1359                 kfree(evt_dat->data);
1360                 kfree(evt_dat);
1361         }
1362
1363         spin_lock_irqsave(&phba->ct_ev_lock, flags);
1364         lpfc_bsg_event_unref(evt);
1365         spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1366         job->dd_data = NULL;
1367         bsg_reply->result = 0;
1368         bsg_job_done(job, bsg_reply->result,
1369                        bsg_reply->reply_payload_rcv_len);
1370         return 0;
1371
1372 job_error:
1373         job->dd_data = NULL;
1374         bsg_reply->result = rc;
1375         return rc;
1376 }
1377
1378 /**
1379  * lpfc_issue_ct_rsp_cmp - lpfc_issue_ct_rsp's completion handler
1380  * @phba: Pointer to HBA context object.
1381  * @cmdiocbq: Pointer to command iocb.
1382  * @rspiocbq: Pointer to response iocb.
1383  *
1384  * This function is the completion handler for iocbs issued using
1385  * lpfc_issue_ct_rsp_cmp function. This function is called by the
1386  * ring event handler function without any lock held. This function
1387  * can be called from both worker thread context and interrupt
1388  * context. This function also can be called from other thread which
1389  * cleans up the SLI layer objects.
1390  * This function copy the contents of the response iocb to the
1391  * response iocb memory object provided by the caller of
1392  * lpfc_sli_issue_iocb_wait and then wakes up the thread which
1393  * sleeps for the iocb completion.
1394  **/
1395 static void
1396 lpfc_issue_ct_rsp_cmp(struct lpfc_hba *phba,
1397                         struct lpfc_iocbq *cmdiocbq,
1398                         struct lpfc_iocbq *rspiocbq)
1399 {
1400         struct bsg_job_data *dd_data;
1401         struct bsg_job *job;
1402         struct fc_bsg_reply *bsg_reply;
1403         IOCB_t *rsp;
1404         struct lpfc_dmabuf *bmp, *cmp;
1405         struct lpfc_nodelist *ndlp;
1406         unsigned long flags;
1407         int rc = 0;
1408
1409         dd_data = cmdiocbq->context1;
1410
1411         /* Determine if job has been aborted */
1412         spin_lock_irqsave(&phba->ct_ev_lock, flags);
1413         job = dd_data->set_job;
1414         if (job) {
1415                 /* Prevent timeout handling from trying to abort job  */
1416                 job->dd_data = NULL;
1417         }
1418         spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1419
1420         /* Close the timeout handler abort window */
1421         spin_lock_irqsave(&phba->hbalock, flags);
1422         cmdiocbq->iocb_flag &= ~LPFC_IO_CMD_OUTSTANDING;
1423         spin_unlock_irqrestore(&phba->hbalock, flags);
1424
1425         ndlp = dd_data->context_un.iocb.ndlp;
1426         cmp = cmdiocbq->context2;
1427         bmp = cmdiocbq->context3;
1428         rsp = &rspiocbq->iocb;
1429
1430         /* Copy the completed job data or set the error status */
1431
1432         if (job) {
1433                 bsg_reply = job->reply;
1434                 if (rsp->ulpStatus) {
1435                         if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
1436                                 switch (rsp->un.ulpWord[4] & IOERR_PARAM_MASK) {
1437                                 case IOERR_SEQUENCE_TIMEOUT:
1438                                         rc = -ETIMEDOUT;
1439                                         break;
1440                                 case IOERR_INVALID_RPI:
1441                                         rc = -EFAULT;
1442                                         break;
1443                                 default:
1444                                         rc = -EACCES;
1445                                         break;
1446                                 }
1447                         } else {
1448                                 rc = -EACCES;
1449                         }
1450                 } else {
1451                         bsg_reply->reply_payload_rcv_len = 0;
1452                 }
1453         }
1454
1455         lpfc_free_bsg_buffers(phba, cmp);
1456         lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
1457         kfree(bmp);
1458         lpfc_sli_release_iocbq(phba, cmdiocbq);
1459         lpfc_nlp_put(ndlp);
1460         kfree(dd_data);
1461
1462         /* Complete the job if the job is still active */
1463
1464         if (job) {
1465                 bsg_reply->result = rc;
1466                 bsg_job_done(job, bsg_reply->result,
1467                                bsg_reply->reply_payload_rcv_len);
1468         }
1469         return;
1470 }
1471
1472 /**
1473  * lpfc_issue_ct_rsp - issue a ct response
1474  * @phba: Pointer to HBA context object.
1475  * @job: Pointer to the job object.
1476  * @tag: tag index value into the ports context exchange array.
1477  * @bmp: Pointer to a dma buffer descriptor.
1478  * @num_entry: Number of enties in the bde.
1479  **/
1480 static int
1481 lpfc_issue_ct_rsp(struct lpfc_hba *phba, struct bsg_job *job, uint32_t tag,
1482                   struct lpfc_dmabuf *cmp, struct lpfc_dmabuf *bmp,
1483                   int num_entry)
1484 {
1485         IOCB_t *icmd;
1486         struct lpfc_iocbq *ctiocb = NULL;
1487         int rc = 0;
1488         struct lpfc_nodelist *ndlp = NULL;
1489         struct bsg_job_data *dd_data;
1490         unsigned long flags;
1491         uint32_t creg_val;
1492
1493         /* allocate our bsg tracking structure */
1494         dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
1495         if (!dd_data) {
1496                 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1497                                 "2736 Failed allocation of dd_data\n");
1498                 rc = -ENOMEM;
1499                 goto no_dd_data;
1500         }
1501
1502         /* Allocate buffer for  command iocb */
1503         ctiocb = lpfc_sli_get_iocbq(phba);
1504         if (!ctiocb) {
1505                 rc = -ENOMEM;
1506                 goto no_ctiocb;
1507         }
1508
1509         icmd = &ctiocb->iocb;
1510         icmd->un.xseq64.bdl.ulpIoTag32 = 0;
1511         icmd->un.xseq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
1512         icmd->un.xseq64.bdl.addrLow = putPaddrLow(bmp->phys);
1513         icmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
1514         icmd->un.xseq64.bdl.bdeSize = (num_entry * sizeof(struct ulp_bde64));
1515         icmd->un.xseq64.w5.hcsw.Fctl = (LS | LA);
1516         icmd->un.xseq64.w5.hcsw.Dfctl = 0;
1517         icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_SOL_CTL;
1518         icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
1519
1520         /* Fill in rest of iocb */
1521         icmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;
1522         icmd->ulpBdeCount = 1;
1523         icmd->ulpLe = 1;
1524         icmd->ulpClass = CLASS3;
1525         if (phba->sli_rev == LPFC_SLI_REV4) {
1526                 /* Do not issue unsol response if oxid not marked as valid */
1527                 if (phba->ct_ctx[tag].valid != UNSOL_VALID) {
1528                         rc = IOCB_ERROR;
1529                         goto issue_ct_rsp_exit;
1530                 }
1531                 icmd->ulpContext = phba->ct_ctx[tag].rxid;
1532                 icmd->unsli3.rcvsli3.ox_id = phba->ct_ctx[tag].oxid;
1533                 ndlp = lpfc_findnode_did(phba->pport, phba->ct_ctx[tag].SID);
1534                 if (!ndlp) {
1535                         lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
1536                                  "2721 ndlp null for oxid %x SID %x\n",
1537                                         icmd->ulpContext,
1538                                         phba->ct_ctx[tag].SID);
1539                         rc = IOCB_ERROR;
1540                         goto issue_ct_rsp_exit;
1541                 }
1542
1543                 /* Check if the ndlp is active */
1544                 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
1545                         rc = IOCB_ERROR;
1546                         goto issue_ct_rsp_exit;
1547                 }
1548
1549                 /* get a refernece count so the ndlp doesn't go away while
1550                  * we respond
1551                  */
1552                 if (!lpfc_nlp_get(ndlp)) {
1553                         rc = IOCB_ERROR;
1554                         goto issue_ct_rsp_exit;
1555                 }
1556
1557                 icmd->un.ulpWord[3] =
1558                                 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
1559
1560                 /* The exchange is done, mark the entry as invalid */
1561                 phba->ct_ctx[tag].valid = UNSOL_INVALID;
1562         } else
1563                 icmd->ulpContext = (ushort) tag;
1564
1565         icmd->ulpTimeout = phba->fc_ratov * 2;
1566
1567         /* Xmit CT response on exchange <xid> */
1568         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
1569                 "2722 Xmit CT response on exchange x%x Data: x%x x%x x%x\n",
1570                 icmd->ulpContext, icmd->ulpIoTag, tag, phba->link_state);
1571
1572         ctiocb->iocb_flag |= LPFC_IO_LIBDFC;
1573         ctiocb->vport = phba->pport;
1574         ctiocb->context1 = dd_data;
1575         ctiocb->context2 = cmp;
1576         ctiocb->context3 = bmp;
1577         ctiocb->context_un.ndlp = ndlp;
1578         ctiocb->iocb_cmpl = lpfc_issue_ct_rsp_cmp;
1579
1580         dd_data->type = TYPE_IOCB;
1581         dd_data->set_job = job;
1582         dd_data->context_un.iocb.cmdiocbq = ctiocb;
1583         dd_data->context_un.iocb.ndlp = ndlp;
1584         dd_data->context_un.iocb.rmp = NULL;
1585         job->dd_data = dd_data;
1586
1587         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
1588                 if (lpfc_readl(phba->HCregaddr, &creg_val)) {
1589                         rc = -IOCB_ERROR;
1590                         goto issue_ct_rsp_exit;
1591                 }
1592                 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
1593                 writel(creg_val, phba->HCregaddr);
1594                 readl(phba->HCregaddr); /* flush */
1595         }
1596
1597         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
1598
1599         if (rc == IOCB_SUCCESS) {
1600                 spin_lock_irqsave(&phba->hbalock, flags);
1601                 /* make sure the I/O had not been completed/released */
1602                 if (ctiocb->iocb_flag & LPFC_IO_LIBDFC) {
1603                         /* open up abort window to timeout handler */
1604                         ctiocb->iocb_flag |= LPFC_IO_CMD_OUTSTANDING;
1605                 }
1606                 spin_unlock_irqrestore(&phba->hbalock, flags);
1607                 return 0; /* done for now */
1608         }
1609
1610         /* iocb failed so cleanup */
1611         job->dd_data = NULL;
1612
1613 issue_ct_rsp_exit:
1614         lpfc_sli_release_iocbq(phba, ctiocb);
1615 no_ctiocb:
1616         kfree(dd_data);
1617 no_dd_data:
1618         return rc;
1619 }
1620
1621 /**
1622  * lpfc_bsg_send_mgmt_rsp - process a SEND_MGMT_RESP bsg vendor command
1623  * @job: SEND_MGMT_RESP fc_bsg_job
1624  **/
1625 static int
1626 lpfc_bsg_send_mgmt_rsp(struct bsg_job *job)
1627 {
1628         struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
1629         struct lpfc_hba *phba = vport->phba;
1630         struct fc_bsg_request *bsg_request = job->request;
1631         struct fc_bsg_reply *bsg_reply = job->reply;
1632         struct send_mgmt_resp *mgmt_resp = (struct send_mgmt_resp *)
1633                 bsg_request->rqst_data.h_vendor.vendor_cmd;
1634         struct ulp_bde64 *bpl;
1635         struct lpfc_dmabuf *bmp = NULL, *cmp = NULL;
1636         int bpl_entries;
1637         uint32_t tag = mgmt_resp->tag;
1638         unsigned long reqbfrcnt =
1639                         (unsigned long)job->request_payload.payload_len;
1640         int rc = 0;
1641
1642         /* in case no data is transferred */
1643         bsg_reply->reply_payload_rcv_len = 0;
1644
1645         if (!reqbfrcnt || (reqbfrcnt > (80 * BUF_SZ_4K))) {
1646                 rc = -ERANGE;
1647                 goto send_mgmt_rsp_exit;
1648         }
1649
1650         bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
1651         if (!bmp) {
1652                 rc = -ENOMEM;
1653                 goto send_mgmt_rsp_exit;
1654         }
1655
1656         bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
1657         if (!bmp->virt) {
1658                 rc = -ENOMEM;
1659                 goto send_mgmt_rsp_free_bmp;
1660         }
1661
1662         INIT_LIST_HEAD(&bmp->list);
1663         bpl = (struct ulp_bde64 *) bmp->virt;
1664         bpl_entries = (LPFC_BPL_SIZE/sizeof(struct ulp_bde64));
1665         cmp = lpfc_alloc_bsg_buffers(phba, job->request_payload.payload_len,
1666                                      1, bpl, &bpl_entries);
1667         if (!cmp) {
1668                 rc = -ENOMEM;
1669                 goto send_mgmt_rsp_free_bmp;
1670         }
1671         lpfc_bsg_copy_data(cmp, &job->request_payload,
1672                            job->request_payload.payload_len, 1);
1673
1674         rc = lpfc_issue_ct_rsp(phba, job, tag, cmp, bmp, bpl_entries);
1675
1676         if (rc == IOCB_SUCCESS)
1677                 return 0; /* done for now */
1678
1679         rc = -EACCES;
1680
1681         lpfc_free_bsg_buffers(phba, cmp);
1682
1683 send_mgmt_rsp_free_bmp:
1684         if (bmp->virt)
1685                 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
1686         kfree(bmp);
1687 send_mgmt_rsp_exit:
1688         /* make error code available to userspace */
1689         bsg_reply->result = rc;
1690         job->dd_data = NULL;
1691         return rc;
1692 }
1693
1694 /**
1695  * lpfc_bsg_diag_mode_enter - process preparing into device diag loopback mode
1696  * @phba: Pointer to HBA context object.
1697  *
1698  * This function is responsible for preparing driver for diag loopback
1699  * on device.
1700  */
1701 static int
1702 lpfc_bsg_diag_mode_enter(struct lpfc_hba *phba)
1703 {
1704         struct lpfc_vport **vports;
1705         struct Scsi_Host *shost;
1706         struct lpfc_sli *psli;
1707         struct lpfc_queue *qp = NULL;
1708         struct lpfc_sli_ring *pring;
1709         int i = 0;
1710
1711         psli = &phba->sli;
1712         if (!psli)
1713                 return -ENODEV;
1714
1715
1716         if ((phba->link_state == LPFC_HBA_ERROR) ||
1717             (psli->sli_flag & LPFC_BLOCK_MGMT_IO) ||
1718             (!(psli->sli_flag & LPFC_SLI_ACTIVE)))
1719                 return -EACCES;
1720
1721         vports = lpfc_create_vport_work_array(phba);
1722         if (vports) {
1723                 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
1724                         shost = lpfc_shost_from_vport(vports[i]);
1725                         scsi_block_requests(shost);
1726                 }
1727                 lpfc_destroy_vport_work_array(phba, vports);
1728         } else {
1729                 shost = lpfc_shost_from_vport(phba->pport);
1730                 scsi_block_requests(shost);
1731         }
1732
1733         if (phba->sli_rev != LPFC_SLI_REV4) {
1734                 pring = &psli->sli3_ring[LPFC_FCP_RING];
1735                 lpfc_emptyq_wait(phba, &pring->txcmplq, &phba->hbalock);
1736                 return 0;
1737         }
1738         list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
1739                 pring = qp->pring;
1740                 if (!pring || (pring->ringno != LPFC_FCP_RING))
1741                         continue;
1742                 if (!lpfc_emptyq_wait(phba, &pring->txcmplq,
1743                                       &pring->ring_lock))
1744                         break;
1745         }
1746         return 0;
1747 }
1748
1749 /**
1750  * lpfc_bsg_diag_mode_exit - exit process from device diag loopback mode
1751  * @phba: Pointer to HBA context object.
1752  *
1753  * This function is responsible for driver exit processing of setting up
1754  * diag loopback mode on device.
1755  */
1756 static void
1757 lpfc_bsg_diag_mode_exit(struct lpfc_hba *phba)
1758 {
1759         struct Scsi_Host *shost;
1760         struct lpfc_vport **vports;
1761         int i;
1762
1763         vports = lpfc_create_vport_work_array(phba);
1764         if (vports) {
1765                 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
1766                         shost = lpfc_shost_from_vport(vports[i]);
1767                         scsi_unblock_requests(shost);
1768                 }
1769                 lpfc_destroy_vport_work_array(phba, vports);
1770         } else {
1771                 shost = lpfc_shost_from_vport(phba->pport);
1772                 scsi_unblock_requests(shost);
1773         }
1774         return;
1775 }
1776
1777 /**
1778  * lpfc_sli3_bsg_diag_loopback_mode - process an sli3 bsg vendor command
1779  * @phba: Pointer to HBA context object.
1780  * @job: LPFC_BSG_VENDOR_DIAG_MODE
1781  *
1782  * This function is responsible for placing an sli3  port into diagnostic
1783  * loopback mode in order to perform a diagnostic loopback test.
1784  * All new scsi requests are blocked, a small delay is used to allow the
1785  * scsi requests to complete then the link is brought down. If the link is
1786  * is placed in loopback mode then scsi requests are again allowed
1787  * so the scsi mid-layer doesn't give up on the port.
1788  * All of this is done in-line.
1789  */
1790 static int
1791 lpfc_sli3_bsg_diag_loopback_mode(struct lpfc_hba *phba, struct bsg_job *job)
1792 {
1793         struct fc_bsg_request *bsg_request = job->request;
1794         struct fc_bsg_reply *bsg_reply = job->reply;
1795         struct diag_mode_set *loopback_mode;
1796         uint32_t link_flags;
1797         uint32_t timeout;
1798         LPFC_MBOXQ_t *pmboxq  = NULL;
1799         int mbxstatus = MBX_SUCCESS;
1800         int i = 0;
1801         int rc = 0;
1802
1803         /* no data to return just the return code */
1804         bsg_reply->reply_payload_rcv_len = 0;
1805
1806         if (job->request_len < sizeof(struct fc_bsg_request) +
1807             sizeof(struct diag_mode_set)) {
1808                 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1809                                 "2738 Received DIAG MODE request size:%d "
1810                                 "below the minimum size:%d\n",
1811                                 job->request_len,
1812                                 (int)(sizeof(struct fc_bsg_request) +
1813                                 sizeof(struct diag_mode_set)));
1814                 rc = -EINVAL;
1815                 goto job_error;
1816         }
1817
1818         rc = lpfc_bsg_diag_mode_enter(phba);
1819         if (rc)
1820                 goto job_error;
1821
1822         /* bring the link to diagnostic mode */
1823         loopback_mode = (struct diag_mode_set *)
1824                 bsg_request->rqst_data.h_vendor.vendor_cmd;
1825         link_flags = loopback_mode->type;
1826         timeout = loopback_mode->timeout * 100;
1827
1828         pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1829         if (!pmboxq) {
1830                 rc = -ENOMEM;
1831                 goto loopback_mode_exit;
1832         }
1833         memset((void *)pmboxq, 0, sizeof(LPFC_MBOXQ_t));
1834         pmboxq->u.mb.mbxCommand = MBX_DOWN_LINK;
1835         pmboxq->u.mb.mbxOwner = OWN_HOST;
1836
1837         mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
1838
1839         if ((mbxstatus == MBX_SUCCESS) && (pmboxq->u.mb.mbxStatus == 0)) {
1840                 /* wait for link down before proceeding */
1841                 i = 0;
1842                 while (phba->link_state != LPFC_LINK_DOWN) {
1843                         if (i++ > timeout) {
1844                                 rc = -ETIMEDOUT;
1845                                 goto loopback_mode_exit;
1846                         }
1847                         msleep(10);
1848                 }
1849
1850                 memset((void *)pmboxq, 0, sizeof(LPFC_MBOXQ_t));
1851                 if (link_flags == INTERNAL_LOOP_BACK)
1852                         pmboxq->u.mb.un.varInitLnk.link_flags = FLAGS_LOCAL_LB;
1853                 else
1854                         pmboxq->u.mb.un.varInitLnk.link_flags =
1855                                 FLAGS_TOPOLOGY_MODE_LOOP;
1856
1857                 pmboxq->u.mb.mbxCommand = MBX_INIT_LINK;
1858                 pmboxq->u.mb.mbxOwner = OWN_HOST;
1859
1860                 mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq,
1861                                                      LPFC_MBOX_TMO);
1862
1863                 if ((mbxstatus != MBX_SUCCESS) || (pmboxq->u.mb.mbxStatus))
1864                         rc = -ENODEV;
1865                 else {
1866                         spin_lock_irq(&phba->hbalock);
1867                         phba->link_flag |= LS_LOOPBACK_MODE;
1868                         spin_unlock_irq(&phba->hbalock);
1869                         /* wait for the link attention interrupt */
1870                         msleep(100);
1871
1872                         i = 0;
1873                         while (phba->link_state != LPFC_HBA_READY) {
1874                                 if (i++ > timeout) {
1875                                         rc = -ETIMEDOUT;
1876                                         break;
1877                                 }
1878
1879                                 msleep(10);
1880                         }
1881                 }
1882
1883         } else
1884                 rc = -ENODEV;
1885
1886 loopback_mode_exit:
1887         lpfc_bsg_diag_mode_exit(phba);
1888
1889         /*
1890          * Let SLI layer release mboxq if mbox command completed after timeout.
1891          */
1892         if (pmboxq && mbxstatus != MBX_TIMEOUT)
1893                 mempool_free(pmboxq, phba->mbox_mem_pool);
1894
1895 job_error:
1896         /* make error code available to userspace */
1897         bsg_reply->result = rc;
1898         /* complete the job back to userspace if no error */
1899         if (rc == 0)
1900                 bsg_job_done(job, bsg_reply->result,
1901                                bsg_reply->reply_payload_rcv_len);
1902         return rc;
1903 }
1904
1905 /**
1906  * lpfc_sli4_bsg_set_link_diag_state - set sli4 link diag state
1907  * @phba: Pointer to HBA context object.
1908  * @diag: Flag for set link to diag or nomral operation state.
1909  *
1910  * This function is responsible for issuing a sli4 mailbox command for setting
1911  * link to either diag state or normal operation state.
1912  */
1913 static int
1914 lpfc_sli4_bsg_set_link_diag_state(struct lpfc_hba *phba, uint32_t diag)
1915 {
1916         LPFC_MBOXQ_t *pmboxq;
1917         struct lpfc_mbx_set_link_diag_state *link_diag_state;
1918         uint32_t req_len, alloc_len;
1919         int mbxstatus = MBX_SUCCESS, rc;
1920
1921         pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1922         if (!pmboxq)
1923                 return -ENOMEM;
1924
1925         req_len = (sizeof(struct lpfc_mbx_set_link_diag_state) -
1926                    sizeof(struct lpfc_sli4_cfg_mhdr));
1927         alloc_len = lpfc_sli4_config(phba, pmboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
1928                                 LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_STATE,
1929                                 req_len, LPFC_SLI4_MBX_EMBED);
1930         if (alloc_len != req_len) {
1931                 rc = -ENOMEM;
1932                 goto link_diag_state_set_out;
1933         }
1934         lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
1935                         "3128 Set link to diagnostic state:x%x (x%x/x%x)\n",
1936                         diag, phba->sli4_hba.lnk_info.lnk_tp,
1937                         phba->sli4_hba.lnk_info.lnk_no);
1938
1939         link_diag_state = &pmboxq->u.mqe.un.link_diag_state;
1940         bf_set(lpfc_mbx_set_diag_state_diag_bit_valid, &link_diag_state->u.req,
1941                LPFC_DIAG_STATE_DIAG_BIT_VALID_CHANGE);
1942         bf_set(lpfc_mbx_set_diag_state_link_num, &link_diag_state->u.req,
1943                phba->sli4_hba.lnk_info.lnk_no);
1944         bf_set(lpfc_mbx_set_diag_state_link_type, &link_diag_state->u.req,
1945                phba->sli4_hba.lnk_info.lnk_tp);
1946         if (diag)
1947                 bf_set(lpfc_mbx_set_diag_state_diag,
1948                        &link_diag_state->u.req, 1);
1949         else
1950                 bf_set(lpfc_mbx_set_diag_state_diag,
1951                        &link_diag_state->u.req, 0);
1952
1953         mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
1954
1955         if ((mbxstatus == MBX_SUCCESS) && (pmboxq->u.mb.mbxStatus == 0))
1956                 rc = 0;
1957         else
1958                 rc = -ENODEV;
1959
1960 link_diag_state_set_out:
1961         if (pmboxq && (mbxstatus != MBX_TIMEOUT))
1962                 mempool_free(pmboxq, phba->mbox_mem_pool);
1963
1964         return rc;
1965 }
1966
1967 /**
1968  * lpfc_sli4_bsg_set_loopback_mode - set sli4 internal loopback diagnostic
1969  * @phba: Pointer to HBA context object.
1970  * @mode: loopback mode to set
1971  * @link_no: link number for loopback mode to set
1972  *
1973  * This function is responsible for issuing a sli4 mailbox command for setting
1974  * up loopback diagnostic for a link.
1975  */
1976 static int
1977 lpfc_sli4_bsg_set_loopback_mode(struct lpfc_hba *phba, int mode,
1978                                 uint32_t link_no)
1979 {
1980         LPFC_MBOXQ_t *pmboxq;
1981         uint32_t req_len, alloc_len;
1982         struct lpfc_mbx_set_link_diag_loopback *link_diag_loopback;
1983         int mbxstatus = MBX_SUCCESS, rc = 0;
1984
1985         pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1986         if (!pmboxq)
1987                 return -ENOMEM;
1988         req_len = (sizeof(struct lpfc_mbx_set_link_diag_loopback) -
1989                    sizeof(struct lpfc_sli4_cfg_mhdr));
1990         alloc_len = lpfc_sli4_config(phba, pmboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
1991                                 LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_LOOPBACK,
1992                                 req_len, LPFC_SLI4_MBX_EMBED);
1993         if (alloc_len != req_len) {
1994                 mempool_free(pmboxq, phba->mbox_mem_pool);
1995                 return -ENOMEM;
1996         }
1997         link_diag_loopback = &pmboxq->u.mqe.un.link_diag_loopback;
1998         bf_set(lpfc_mbx_set_diag_state_link_num,
1999                &link_diag_loopback->u.req, link_no);
2000
2001         if (phba->sli4_hba.conf_trunk & (1 << link_no)) {
2002                 bf_set(lpfc_mbx_set_diag_state_link_type,
2003                        &link_diag_loopback->u.req, LPFC_LNK_FC_TRUNKED);
2004         } else {
2005                 bf_set(lpfc_mbx_set_diag_state_link_type,
2006                        &link_diag_loopback->u.req,
2007                        phba->sli4_hba.lnk_info.lnk_tp);
2008         }
2009
2010         bf_set(lpfc_mbx_set_diag_lpbk_type, &link_diag_loopback->u.req,
2011                mode);
2012
2013         mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
2014         if ((mbxstatus != MBX_SUCCESS) || (pmboxq->u.mb.mbxStatus)) {
2015                 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2016                                 "3127 Failed setup loopback mode mailbox "
2017                                 "command, rc:x%x, status:x%x\n", mbxstatus,
2018                                 pmboxq->u.mb.mbxStatus);
2019                 rc = -ENODEV;
2020         }
2021         if (pmboxq && (mbxstatus != MBX_TIMEOUT))
2022                 mempool_free(pmboxq, phba->mbox_mem_pool);
2023         return rc;
2024 }
2025
2026 /**
2027  * lpfc_sli4_diag_fcport_reg_setup - setup port registrations for diagnostic
2028  * @phba: Pointer to HBA context object.
2029  *
2030  * This function set up SLI4 FC port registrations for diagnostic run, which
2031  * includes all the rpis, vfi, and also vpi.
2032  */
2033 static int
2034 lpfc_sli4_diag_fcport_reg_setup(struct lpfc_hba *phba)
2035 {
2036         int rc;
2037
2038         if (phba->pport->fc_flag & FC_VFI_REGISTERED) {
2039                 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2040                                 "3136 Port still had vfi registered: "
2041                                 "mydid:x%x, fcfi:%d, vfi:%d, vpi:%d\n",
2042                                 phba->pport->fc_myDID, phba->fcf.fcfi,
2043                                 phba->sli4_hba.vfi_ids[phba->pport->vfi],
2044                                 phba->vpi_ids[phba->pport->vpi]);
2045                 return -EINVAL;
2046         }
2047         rc = lpfc_issue_reg_vfi(phba->pport);
2048         return rc;
2049 }
2050
2051 /**
2052  * lpfc_sli4_bsg_diag_loopback_mode - process an sli4 bsg vendor command
2053  * @phba: Pointer to HBA context object.
2054  * @job: LPFC_BSG_VENDOR_DIAG_MODE
2055  *
2056  * This function is responsible for placing an sli4 port into diagnostic
2057  * loopback mode in order to perform a diagnostic loopback test.
2058  */
2059 static int
2060 lpfc_sli4_bsg_diag_loopback_mode(struct lpfc_hba *phba, struct bsg_job *job)
2061 {
2062         struct fc_bsg_request *bsg_request = job->request;
2063         struct fc_bsg_reply *bsg_reply = job->reply;
2064         struct diag_mode_set *loopback_mode;
2065         uint32_t link_flags, timeout, link_no;
2066         int i, rc = 0;
2067
2068         /* no data to return just the return code */
2069         bsg_reply->reply_payload_rcv_len = 0;
2070
2071         if (job->request_len < sizeof(struct fc_bsg_request) +
2072             sizeof(struct diag_mode_set)) {
2073                 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2074                                 "3011 Received DIAG MODE request size:%d "
2075                                 "below the minimum size:%d\n",
2076                                 job->request_len,
2077                                 (int)(sizeof(struct fc_bsg_request) +
2078                                 sizeof(struct diag_mode_set)));
2079                 rc = -EINVAL;
2080                 goto job_done;
2081         }
2082
2083         loopback_mode = (struct diag_mode_set *)
2084                 bsg_request->rqst_data.h_vendor.vendor_cmd;
2085         link_flags = loopback_mode->type;
2086         timeout = loopback_mode->timeout * 100;
2087
2088         if (loopback_mode->physical_link == -1)
2089                 link_no = phba->sli4_hba.lnk_info.lnk_no;
2090         else
2091                 link_no = loopback_mode->physical_link;
2092
2093         if (link_flags == DISABLE_LOOP_BACK) {
2094                 rc = lpfc_sli4_bsg_set_loopback_mode(phba,
2095                                         LPFC_DIAG_LOOPBACK_TYPE_DISABLE,
2096                                         link_no);
2097                 if (!rc) {
2098                         /* Unset the need disable bit */
2099                         phba->sli4_hba.conf_trunk &= ~((1 << link_no) << 4);
2100                 }
2101                 goto job_done;
2102         } else {
2103                 /* Check if we need to disable the loopback state */
2104                 if (phba->sli4_hba.conf_trunk & ((1 << link_no) << 4)) {
2105                         rc = -EPERM;
2106                         goto job_done;
2107                 }
2108         }
2109
2110         rc = lpfc_bsg_diag_mode_enter(phba);
2111         if (rc)
2112                 goto job_done;
2113
2114         /* indicate we are in loobpack diagnostic mode */
2115         spin_lock_irq(&phba->hbalock);
2116         phba->link_flag |= LS_LOOPBACK_MODE;
2117         spin_unlock_irq(&phba->hbalock);
2118
2119         /* reset port to start frome scratch */
2120         rc = lpfc_selective_reset(phba);
2121         if (rc)
2122                 goto job_done;
2123
2124         /* bring the link to diagnostic mode */
2125         lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
2126                         "3129 Bring link to diagnostic state.\n");
2127
2128         rc = lpfc_sli4_bsg_set_link_diag_state(phba, 1);
2129         if (rc) {
2130                 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2131                                 "3130 Failed to bring link to diagnostic "
2132                                 "state, rc:x%x\n", rc);
2133                 goto loopback_mode_exit;
2134         }
2135
2136         /* wait for link down before proceeding */
2137         i = 0;
2138         while (phba->link_state != LPFC_LINK_DOWN) {
2139                 if (i++ > timeout) {
2140                         rc = -ETIMEDOUT;
2141                         lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
2142                                         "3131 Timeout waiting for link to "
2143                                         "diagnostic mode, timeout:%d ms\n",
2144                                         timeout * 10);
2145                         goto loopback_mode_exit;
2146                 }
2147                 msleep(10);
2148         }
2149
2150         /* set up loopback mode */
2151         lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
2152                         "3132 Set up loopback mode:x%x\n", link_flags);
2153
2154         switch (link_flags) {
2155         case INTERNAL_LOOP_BACK:
2156                 if (phba->sli4_hba.conf_trunk & (1 << link_no)) {
2157                         rc = lpfc_sli4_bsg_set_loopback_mode(phba,
2158                                         LPFC_DIAG_LOOPBACK_TYPE_INTERNAL,
2159                                         link_no);
2160                 } else {
2161                         /* Trunk is configured, but link is not in this trunk */
2162                         if (phba->sli4_hba.conf_trunk) {
2163                                 rc = -ELNRNG;
2164                                 goto loopback_mode_exit;
2165                         }
2166
2167                         rc = lpfc_sli4_bsg_set_loopback_mode(phba,
2168                                         LPFC_DIAG_LOOPBACK_TYPE_INTERNAL,
2169                                         link_no);
2170                 }
2171
2172                 if (!rc) {
2173                         /* Set the need disable bit */
2174                         phba->sli4_hba.conf_trunk |= (1 << link_no) << 4;
2175                 }
2176
2177                 break;
2178         case EXTERNAL_LOOP_BACK:
2179                 if (phba->sli4_hba.conf_trunk & (1 << link_no)) {
2180                         rc = lpfc_sli4_bsg_set_loopback_mode(phba,
2181                                 LPFC_DIAG_LOOPBACK_TYPE_EXTERNAL_TRUNKED,
2182                                 link_no);
2183                 } else {
2184                         /* Trunk is configured, but link is not in this trunk */
2185                         if (phba->sli4_hba.conf_trunk) {
2186                                 rc = -ELNRNG;
2187                                 goto loopback_mode_exit;
2188                         }
2189
2190                         rc = lpfc_sli4_bsg_set_loopback_mode(phba,
2191                                                 LPFC_DIAG_LOOPBACK_TYPE_SERDES,
2192                                                 link_no);
2193                 }
2194
2195                 if (!rc) {
2196                         /* Set the need disable bit */
2197                         phba->sli4_hba.conf_trunk |= (1 << link_no) << 4;
2198                 }
2199
2200                 break;
2201         default:
2202                 rc = -EINVAL;
2203                 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
2204                                 "3141 Loopback mode:x%x not supported\n",
2205                                 link_flags);
2206                 goto loopback_mode_exit;
2207         }
2208
2209         if (!rc) {
2210                 /* wait for the link attention interrupt */
2211                 msleep(100);
2212                 i = 0;
2213                 while (phba->link_state < LPFC_LINK_UP) {
2214                         if (i++ > timeout) {
2215                                 rc = -ETIMEDOUT;
2216                                 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
2217                                         "3137 Timeout waiting for link up "
2218                                         "in loopback mode, timeout:%d ms\n",
2219                                         timeout * 10);
2220                                 break;
2221                         }
2222                         msleep(10);
2223                 }
2224         }
2225
2226         /* port resource registration setup for loopback diagnostic */
2227         if (!rc) {
2228                 /* set up a none zero myDID for loopback test */
2229                 phba->pport->fc_myDID = 1;
2230                 rc = lpfc_sli4_diag_fcport_reg_setup(phba);
2231         } else
2232                 goto loopback_mode_exit;
2233
2234         if (!rc) {
2235                 /* wait for the port ready */
2236                 msleep(100);
2237                 i = 0;
2238                 while (phba->link_state != LPFC_HBA_READY) {
2239                         if (i++ > timeout) {
2240                                 rc = -ETIMEDOUT;
2241                                 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
2242                                         "3133 Timeout waiting for port "
2243                                         "loopback mode ready, timeout:%d ms\n",
2244                                         timeout * 10);
2245                                 break;
2246                         }
2247                         msleep(10);
2248                 }
2249         }
2250
2251 loopback_mode_exit:
2252         /* clear loopback diagnostic mode */
2253         if (rc) {
2254                 spin_lock_irq(&phba->hbalock);
2255                 phba->link_flag &= ~LS_LOOPBACK_MODE;
2256                 spin_unlock_irq(&phba->hbalock);
2257         }
2258         lpfc_bsg_diag_mode_exit(phba);
2259
2260 job_done:
2261         /* make error code available to userspace */
2262         bsg_reply->result = rc;
2263         /* complete the job back to userspace if no error */
2264         if (rc == 0)
2265                 bsg_job_done(job, bsg_reply->result,
2266                                bsg_reply->reply_payload_rcv_len);
2267         return rc;
2268 }
2269
2270 /**
2271  * lpfc_bsg_diag_loopback_mode - bsg vendor command for diag loopback mode
2272  * @job: LPFC_BSG_VENDOR_DIAG_MODE
2273  *
2274  * This function is responsible for responding to check and dispatch bsg diag
2275  * command from the user to proper driver action routines.
2276  */
2277 static int
2278 lpfc_bsg_diag_loopback_mode(struct bsg_job *job)
2279 {
2280         struct Scsi_Host *shost;
2281         struct lpfc_vport *vport;
2282         struct lpfc_hba *phba;
2283         int rc;
2284
2285         shost = fc_bsg_to_shost(job);
2286         if (!shost)
2287                 return -ENODEV;
2288         vport = shost_priv(shost);
2289         if (!vport)
2290                 return -ENODEV;
2291         phba = vport->phba;
2292         if (!phba)
2293                 return -ENODEV;
2294
2295         if (phba->sli_rev < LPFC_SLI_REV4)
2296                 rc = lpfc_sli3_bsg_diag_loopback_mode(phba, job);
2297         else if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) >=
2298                  LPFC_SLI_INTF_IF_TYPE_2)
2299                 rc = lpfc_sli4_bsg_diag_loopback_mode(phba, job);
2300         else
2301                 rc = -ENODEV;
2302
2303         return rc;
2304 }
2305
2306 /**
2307  * lpfc_sli4_bsg_diag_mode_end - sli4 bsg vendor command for ending diag mode
2308  * @job: LPFC_BSG_VENDOR_DIAG_MODE_END
2309  *
2310  * This function is responsible for responding to check and dispatch bsg diag
2311  * command from the user to proper driver action routines.
2312  */
2313 static int
2314 lpfc_sli4_bsg_diag_mode_end(struct bsg_job *job)
2315 {
2316         struct fc_bsg_request *bsg_request = job->request;
2317         struct fc_bsg_reply *bsg_reply = job->reply;
2318         struct Scsi_Host *shost;
2319         struct lpfc_vport *vport;
2320         struct lpfc_hba *phba;
2321         struct diag_mode_set *loopback_mode_end_cmd;
2322         uint32_t timeout;
2323         int rc, i;
2324
2325         shost = fc_bsg_to_shost(job);
2326         if (!shost)
2327                 return -ENODEV;
2328         vport = shost_priv(shost);
2329         if (!vport)
2330                 return -ENODEV;
2331         phba = vport->phba;
2332         if (!phba)
2333                 return -ENODEV;
2334
2335         if (phba->sli_rev < LPFC_SLI_REV4)
2336                 return -ENODEV;
2337         if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
2338             LPFC_SLI_INTF_IF_TYPE_2)
2339                 return -ENODEV;
2340
2341         /* clear loopback diagnostic mode */
2342         spin_lock_irq(&phba->hbalock);
2343         phba->link_flag &= ~LS_LOOPBACK_MODE;
2344         spin_unlock_irq(&phba->hbalock);
2345         loopback_mode_end_cmd = (struct diag_mode_set *)
2346                         bsg_request->rqst_data.h_vendor.vendor_cmd;
2347         timeout = loopback_mode_end_cmd->timeout * 100;
2348
2349         rc = lpfc_sli4_bsg_set_link_diag_state(phba, 0);
2350         if (rc) {
2351                 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2352                                 "3139 Failed to bring link to diagnostic "
2353                                 "state, rc:x%x\n", rc);
2354                 goto loopback_mode_end_exit;
2355         }
2356
2357         /* wait for link down before proceeding */
2358         i = 0;
2359         while (phba->link_state != LPFC_LINK_DOWN) {
2360                 if (i++ > timeout) {
2361                         lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
2362                                         "3140 Timeout waiting for link to "
2363                                         "diagnostic mode_end, timeout:%d ms\n",
2364                                         timeout * 10);
2365                         /* there is nothing much we can do here */
2366                         break;
2367                 }
2368                 msleep(10);
2369         }
2370
2371         /* reset port resource registrations */
2372         rc = lpfc_selective_reset(phba);
2373         phba->pport->fc_myDID = 0;
2374
2375 loopback_mode_end_exit:
2376         /* make return code available to userspace */
2377         bsg_reply->result = rc;
2378         /* complete the job back to userspace if no error */
2379         if (rc == 0)
2380                 bsg_job_done(job, bsg_reply->result,
2381                                bsg_reply->reply_payload_rcv_len);
2382         return rc;
2383 }
2384
2385 /**
2386  * lpfc_sli4_bsg_link_diag_test - sli4 bsg vendor command for diag link test
2387  * @job: LPFC_BSG_VENDOR_DIAG_LINK_TEST
2388  *
2389  * This function is to perform SLI4 diag link test request from the user
2390  * applicaiton.
2391  */
2392 static int
2393 lpfc_sli4_bsg_link_diag_test(struct bsg_job *job)
2394 {
2395         struct fc_bsg_request *bsg_request = job->request;
2396         struct fc_bsg_reply *bsg_reply = job->reply;
2397         struct Scsi_Host *shost;
2398         struct lpfc_vport *vport;
2399         struct lpfc_hba *phba;
2400         LPFC_MBOXQ_t *pmboxq;
2401         struct sli4_link_diag *link_diag_test_cmd;
2402         uint32_t req_len, alloc_len;
2403         struct lpfc_mbx_run_link_diag_test *run_link_diag_test;
2404         union lpfc_sli4_cfg_shdr *shdr;
2405         uint32_t shdr_status, shdr_add_status;
2406         struct diag_status *diag_status_reply;
2407         int mbxstatus, rc = -ENODEV, rc1 = 0;
2408
2409         shost = fc_bsg_to_shost(job);
2410         if (!shost)
2411                 goto job_error;
2412
2413         vport = shost_priv(shost);
2414         if (!vport)
2415                 goto job_error;
2416
2417         phba = vport->phba;
2418         if (!phba)
2419                 goto job_error;
2420
2421
2422         if (phba->sli_rev < LPFC_SLI_REV4)
2423                 goto job_error;
2424
2425         if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
2426             LPFC_SLI_INTF_IF_TYPE_2)
2427                 goto job_error;
2428
2429         if (job->request_len < sizeof(struct fc_bsg_request) +
2430             sizeof(struct sli4_link_diag)) {
2431                 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2432                                 "3013 Received LINK DIAG TEST request "
2433                                 " size:%d below the minimum size:%d\n",
2434                                 job->request_len,
2435                                 (int)(sizeof(struct fc_bsg_request) +
2436                                 sizeof(struct sli4_link_diag)));
2437                 rc = -EINVAL;
2438                 goto job_error;
2439         }
2440
2441         rc = lpfc_bsg_diag_mode_enter(phba);
2442         if (rc)
2443                 goto job_error;
2444
2445         link_diag_test_cmd = (struct sli4_link_diag *)
2446                          bsg_request->rqst_data.h_vendor.vendor_cmd;
2447
2448         rc = lpfc_sli4_bsg_set_link_diag_state(phba, 1);
2449
2450         if (rc)
2451                 goto job_error;
2452
2453         pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2454         if (!pmboxq)
2455                 goto link_diag_test_exit;
2456
2457         req_len = (sizeof(struct lpfc_mbx_set_link_diag_state) -
2458                    sizeof(struct lpfc_sli4_cfg_mhdr));
2459         alloc_len = lpfc_sli4_config(phba, pmboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
2460                                      LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_STATE,
2461                                      req_len, LPFC_SLI4_MBX_EMBED);
2462         if (alloc_len != req_len) {
2463                 rc = -ENOMEM;
2464                 goto link_diag_test_exit;
2465         }
2466
2467         run_link_diag_test = &pmboxq->u.mqe.un.link_diag_test;
2468         bf_set(lpfc_mbx_run_diag_test_link_num, &run_link_diag_test->u.req,
2469                phba->sli4_hba.lnk_info.lnk_no);
2470         bf_set(lpfc_mbx_run_diag_test_link_type, &run_link_diag_test->u.req,
2471                phba->sli4_hba.lnk_info.lnk_tp);
2472         bf_set(lpfc_mbx_run_diag_test_test_id, &run_link_diag_test->u.req,
2473                link_diag_test_cmd->test_id);
2474         bf_set(lpfc_mbx_run_diag_test_loops, &run_link_diag_test->u.req,
2475                link_diag_test_cmd->loops);
2476         bf_set(lpfc_mbx_run_diag_test_test_ver, &run_link_diag_test->u.req,
2477                link_diag_test_cmd->test_version);
2478         bf_set(lpfc_mbx_run_diag_test_err_act, &run_link_diag_test->u.req,
2479                link_diag_test_cmd->error_action);
2480
2481         mbxstatus = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
2482
2483         shdr = (union lpfc_sli4_cfg_shdr *)
2484                 &pmboxq->u.mqe.un.sli4_config.header.cfg_shdr;
2485         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
2486         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
2487         if (shdr_status || shdr_add_status || mbxstatus) {
2488                 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
2489                                 "3010 Run link diag test mailbox failed with "
2490                                 "mbx_status x%x status x%x, add_status x%x\n",
2491                                 mbxstatus, shdr_status, shdr_add_status);
2492         }
2493
2494         diag_status_reply = (struct diag_status *)
2495                             bsg_reply->reply_data.vendor_reply.vendor_rsp;
2496
2497         if (job->reply_len <
2498             sizeof(struct fc_bsg_request) + sizeof(struct diag_status)) {
2499                 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2500                                 "3012 Received Run link diag test reply "
2501                                 "below minimum size (%d): reply_len:%d\n",
2502                                 (int)(sizeof(struct fc_bsg_request) +
2503                                 sizeof(struct diag_status)),
2504                                 job->reply_len);
2505                 rc = -EINVAL;
2506                 goto job_error;
2507         }
2508
2509         diag_status_reply->mbox_status = mbxstatus;
2510         diag_status_reply->shdr_status = shdr_status;
2511         diag_status_reply->shdr_add_status = shdr_add_status;
2512
2513 link_diag_test_exit:
2514         rc1 = lpfc_sli4_bsg_set_link_diag_state(phba, 0);
2515
2516         if (pmboxq)
2517                 mempool_free(pmboxq, phba->mbox_mem_pool);
2518
2519         lpfc_bsg_diag_mode_exit(phba);
2520
2521 job_error:
2522         /* make error code available to userspace */
2523         if (rc1 && !rc)
2524                 rc = rc1;
2525         bsg_reply->result = rc;
2526         /* complete the job back to userspace if no error */
2527         if (rc == 0)
2528                 bsg_job_done(job, bsg_reply->result,
2529                                bsg_reply->reply_payload_rcv_len);
2530         return rc;
2531 }
2532
2533 /**
2534  * lpfcdiag_loop_self_reg - obtains a remote port login id
2535  * @phba: Pointer to HBA context object
2536  * @rpi: Pointer to a remote port login id
2537  *
2538  * This function obtains a remote port login id so the diag loopback test
2539  * can send and receive its own unsolicited CT command.
2540  **/
2541 static int lpfcdiag_loop_self_reg(struct lpfc_hba *phba, uint16_t *rpi)
2542 {
2543         LPFC_MBOXQ_t *mbox;
2544         struct lpfc_dmabuf *dmabuff;
2545         int status;
2546
2547         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2548         if (!mbox)
2549                 return -ENOMEM;
2550
2551         if (phba->sli_rev < LPFC_SLI_REV4)
2552                 status = lpfc_reg_rpi(phba, 0, phba->pport->fc_myDID,
2553                                 (uint8_t *)&phba->pport->fc_sparam,
2554                                 mbox, *rpi);
2555         else {
2556                 *rpi = lpfc_sli4_alloc_rpi(phba);
2557                 if (*rpi == LPFC_RPI_ALLOC_ERROR) {
2558                         mempool_free(mbox, phba->mbox_mem_pool);
2559                         return -EBUSY;
2560                 }
2561                 status = lpfc_reg_rpi(phba, phba->pport->vpi,
2562                                 phba->pport->fc_myDID,
2563                                 (uint8_t *)&phba->pport->fc_sparam,
2564                                 mbox, *rpi);
2565         }
2566
2567         if (status) {
2568                 mempool_free(mbox, phba->mbox_mem_pool);
2569                 if (phba->sli_rev == LPFC_SLI_REV4)
2570                         lpfc_sli4_free_rpi(phba, *rpi);
2571                 return -ENOMEM;
2572         }
2573
2574         dmabuff = (struct lpfc_dmabuf *)mbox->ctx_buf;
2575         mbox->ctx_buf = NULL;
2576         mbox->ctx_ndlp = NULL;
2577         status = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
2578
2579         if ((status != MBX_SUCCESS) || (mbox->u.mb.mbxStatus)) {
2580                 lpfc_mbuf_free(phba, dmabuff->virt, dmabuff->phys);
2581                 kfree(dmabuff);
2582                 if (status != MBX_TIMEOUT)
2583                         mempool_free(mbox, phba->mbox_mem_pool);
2584                 if (phba->sli_rev == LPFC_SLI_REV4)
2585                         lpfc_sli4_free_rpi(phba, *rpi);
2586                 return -ENODEV;
2587         }
2588
2589         if (phba->sli_rev < LPFC_SLI_REV4)
2590                 *rpi = mbox->u.mb.un.varWords[0];
2591
2592         lpfc_mbuf_free(phba, dmabuff->virt, dmabuff->phys);
2593         kfree(dmabuff);
2594         mempool_free(mbox, phba->mbox_mem_pool);
2595         return 0;
2596 }
2597
2598 /**
2599  * lpfcdiag_loop_self_unreg - unregs from the rpi
2600  * @phba: Pointer to HBA context object
2601  * @rpi: Remote port login id
2602  *
2603  * This function unregisters the rpi obtained in lpfcdiag_loop_self_reg
2604  **/
2605 static int lpfcdiag_loop_self_unreg(struct lpfc_hba *phba, uint16_t rpi)
2606 {
2607         LPFC_MBOXQ_t *mbox;
2608         int status;
2609
2610         /* Allocate mboxq structure */
2611         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2612         if (mbox == NULL)
2613                 return -ENOMEM;
2614
2615         if (phba->sli_rev < LPFC_SLI_REV4)
2616                 lpfc_unreg_login(phba, 0, rpi, mbox);
2617         else
2618                 lpfc_unreg_login(phba, phba->pport->vpi,
2619                                  phba->sli4_hba.rpi_ids[rpi], mbox);
2620
2621         status = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
2622
2623         if ((status != MBX_SUCCESS) || (mbox->u.mb.mbxStatus)) {
2624                 if (status != MBX_TIMEOUT)
2625                         mempool_free(mbox, phba->mbox_mem_pool);
2626                 return -EIO;
2627         }
2628         mempool_free(mbox, phba->mbox_mem_pool);
2629         if (phba->sli_rev == LPFC_SLI_REV4)
2630                 lpfc_sli4_free_rpi(phba, rpi);
2631         return 0;
2632 }
2633
2634 /**
2635  * lpfcdiag_loop_get_xri - obtains the transmit and receive ids
2636  * @phba: Pointer to HBA context object
2637  * @rpi: Remote port login id
2638  * @txxri: Pointer to transmit exchange id
2639  * @rxxri: Pointer to response exchabge id
2640  *
2641  * This function obtains the transmit and receive ids required to send
2642  * an unsolicited ct command with a payload. A special lpfc FsType and CmdRsp
2643  * flags are used to the unsolicted response handler is able to process
2644  * the ct command sent on the same port.
2645  **/
2646 static int lpfcdiag_loop_get_xri(struct lpfc_hba *phba, uint16_t rpi,
2647                          uint16_t *txxri, uint16_t * rxxri)
2648 {
2649         struct lpfc_bsg_event *evt;
2650         struct lpfc_iocbq *cmdiocbq, *rspiocbq;
2651         IOCB_t *cmd, *rsp;
2652         struct lpfc_dmabuf *dmabuf;
2653         struct ulp_bde64 *bpl = NULL;
2654         struct lpfc_sli_ct_request *ctreq = NULL;
2655         int ret_val = 0;
2656         int time_left;
2657         int iocb_stat = IOCB_SUCCESS;
2658         unsigned long flags;
2659
2660         *txxri = 0;
2661         *rxxri = 0;
2662         evt = lpfc_bsg_event_new(FC_REG_CT_EVENT, current->pid,
2663                                 SLI_CT_ELX_LOOPBACK);
2664         if (!evt)
2665                 return -ENOMEM;
2666
2667         spin_lock_irqsave(&phba->ct_ev_lock, flags);
2668         list_add(&evt->node, &phba->ct_ev_waiters);
2669         lpfc_bsg_event_ref(evt);
2670         spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2671
2672         cmdiocbq = lpfc_sli_get_iocbq(phba);
2673         rspiocbq = lpfc_sli_get_iocbq(phba);
2674
2675         dmabuf = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2676         if (dmabuf) {
2677                 dmabuf->virt = lpfc_mbuf_alloc(phba, 0, &dmabuf->phys);
2678                 if (dmabuf->virt) {
2679                         INIT_LIST_HEAD(&dmabuf->list);
2680                         bpl = (struct ulp_bde64 *) dmabuf->virt;
2681                         memset(bpl, 0, sizeof(*bpl));
2682                         ctreq = (struct lpfc_sli_ct_request *)(bpl + 1);
2683                         bpl->addrHigh =
2684                                 le32_to_cpu(putPaddrHigh(dmabuf->phys +
2685                                         sizeof(*bpl)));
2686                         bpl->addrLow =
2687                                 le32_to_cpu(putPaddrLow(dmabuf->phys +
2688                                         sizeof(*bpl)));
2689                         bpl->tus.f.bdeFlags = 0;
2690                         bpl->tus.f.bdeSize = ELX_LOOPBACK_HEADER_SZ;
2691                         bpl->tus.w = le32_to_cpu(bpl->tus.w);
2692                 }
2693         }
2694
2695         if (cmdiocbq == NULL || rspiocbq == NULL ||
2696             dmabuf == NULL || bpl == NULL || ctreq == NULL ||
2697                 dmabuf->virt == NULL) {
2698                 ret_val = -ENOMEM;
2699                 goto err_get_xri_exit;
2700         }
2701
2702         cmd = &cmdiocbq->iocb;
2703         rsp = &rspiocbq->iocb;
2704
2705         memset(ctreq, 0, ELX_LOOPBACK_HEADER_SZ);
2706
2707         ctreq->RevisionId.bits.Revision = SLI_CT_REVISION;
2708         ctreq->RevisionId.bits.InId = 0;
2709         ctreq->FsType = SLI_CT_ELX_LOOPBACK;
2710         ctreq->FsSubType = 0;
2711         ctreq->CommandResponse.bits.CmdRsp = ELX_LOOPBACK_XRI_SETUP;
2712         ctreq->CommandResponse.bits.Size = 0;
2713
2714
2715         cmd->un.xseq64.bdl.addrHigh = putPaddrHigh(dmabuf->phys);
2716         cmd->un.xseq64.bdl.addrLow = putPaddrLow(dmabuf->phys);
2717         cmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
2718         cmd->un.xseq64.bdl.bdeSize = sizeof(*bpl);
2719
2720         cmd->un.xseq64.w5.hcsw.Fctl = LA;
2721         cmd->un.xseq64.w5.hcsw.Dfctl = 0;
2722         cmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
2723         cmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
2724
2725         cmd->ulpCommand = CMD_XMIT_SEQUENCE64_CR;
2726         cmd->ulpBdeCount = 1;
2727         cmd->ulpLe = 1;
2728         cmd->ulpClass = CLASS3;
2729         cmd->ulpContext = rpi;
2730
2731         cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
2732         cmdiocbq->vport = phba->pport;
2733         cmdiocbq->iocb_cmpl = NULL;
2734
2735         iocb_stat = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq,
2736                                 rspiocbq,
2737                                 (phba->fc_ratov * 2)
2738                                 + LPFC_DRVR_TIMEOUT);
2739         if ((iocb_stat != IOCB_SUCCESS) || (rsp->ulpStatus != IOSTAT_SUCCESS)) {
2740                 ret_val = -EIO;
2741                 goto err_get_xri_exit;
2742         }
2743         *txxri =  rsp->ulpContext;
2744
2745         evt->waiting = 1;
2746         evt->wait_time_stamp = jiffies;
2747         time_left = wait_event_interruptible_timeout(
2748                 evt->wq, !list_empty(&evt->events_to_see),
2749                 msecs_to_jiffies(1000 *
2750                         ((phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT)));
2751         if (list_empty(&evt->events_to_see))
2752                 ret_val = (time_left) ? -EINTR : -ETIMEDOUT;
2753         else {
2754                 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2755                 list_move(evt->events_to_see.prev, &evt->events_to_get);
2756                 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2757                 *rxxri = (list_entry(evt->events_to_get.prev,
2758                                      typeof(struct event_data),
2759                                      node))->immed_dat;
2760         }
2761         evt->waiting = 0;
2762
2763 err_get_xri_exit:
2764         spin_lock_irqsave(&phba->ct_ev_lock, flags);
2765         lpfc_bsg_event_unref(evt); /* release ref */
2766         lpfc_bsg_event_unref(evt); /* delete */
2767         spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2768
2769         if (dmabuf) {
2770                 if (dmabuf->virt)
2771                         lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
2772                 kfree(dmabuf);
2773         }
2774
2775         if (cmdiocbq && (iocb_stat != IOCB_TIMEDOUT))
2776                 lpfc_sli_release_iocbq(phba, cmdiocbq);
2777         if (rspiocbq)
2778                 lpfc_sli_release_iocbq(phba, rspiocbq);
2779         return ret_val;
2780 }
2781
2782 /**
2783  * lpfc_bsg_dma_page_alloc - allocate a bsg mbox page sized dma buffers
2784  * @phba: Pointer to HBA context object
2785  *
2786  * This function allocates BSG_MBOX_SIZE (4KB) page size dma buffer and
2787  * returns the pointer to the buffer.
2788  **/
2789 static struct lpfc_dmabuf *
2790 lpfc_bsg_dma_page_alloc(struct lpfc_hba *phba)
2791 {
2792         struct lpfc_dmabuf *dmabuf;
2793         struct pci_dev *pcidev = phba->pcidev;
2794
2795         /* allocate dma buffer struct */
2796         dmabuf = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2797         if (!dmabuf)
2798                 return NULL;
2799
2800         INIT_LIST_HEAD(&dmabuf->list);
2801
2802         /* now, allocate dma buffer */
2803         dmabuf->virt = dma_alloc_coherent(&pcidev->dev, BSG_MBOX_SIZE,
2804                                           &(dmabuf->phys), GFP_KERNEL);
2805
2806         if (!dmabuf->virt) {
2807                 kfree(dmabuf);
2808                 return NULL;
2809         }
2810
2811         return dmabuf;
2812 }
2813
2814 /**
2815  * lpfc_bsg_dma_page_free - free a bsg mbox page sized dma buffer
2816  * @phba: Pointer to HBA context object.
2817  * @dmabuf: Pointer to the bsg mbox page sized dma buffer descriptor.
2818  *
2819  * This routine just simply frees a dma buffer and its associated buffer
2820  * descriptor referred by @dmabuf.
2821  **/
2822 static void
2823 lpfc_bsg_dma_page_free(struct lpfc_hba *phba, struct lpfc_dmabuf *dmabuf)
2824 {
2825         struct pci_dev *pcidev = phba->pcidev;
2826
2827         if (!dmabuf)
2828                 return;
2829
2830         if (dmabuf->virt)
2831                 dma_free_coherent(&pcidev->dev, BSG_MBOX_SIZE,
2832                                   dmabuf->virt, dmabuf->phys);
2833         kfree(dmabuf);
2834         return;
2835 }
2836
2837 /**
2838  * lpfc_bsg_dma_page_list_free - free a list of bsg mbox page sized dma buffers
2839  * @phba: Pointer to HBA context object.
2840  * @dmabuf_list: Pointer to a list of bsg mbox page sized dma buffer descs.
2841  *
2842  * This routine just simply frees all dma buffers and their associated buffer
2843  * descriptors referred by @dmabuf_list.
2844  **/
2845 static void
2846 lpfc_bsg_dma_page_list_free(struct lpfc_hba *phba,
2847                             struct list_head *dmabuf_list)
2848 {
2849         struct lpfc_dmabuf *dmabuf, *next_dmabuf;
2850
2851         if (list_empty(dmabuf_list))
2852                 return;
2853
2854         list_for_each_entry_safe(dmabuf, next_dmabuf, dmabuf_list, list) {
2855                 list_del_init(&dmabuf->list);
2856                 lpfc_bsg_dma_page_free(phba, dmabuf);
2857         }
2858         return;
2859 }
2860
2861 /**
2862  * diag_cmd_data_alloc - fills in a bde struct with dma buffers
2863  * @phba: Pointer to HBA context object
2864  * @bpl: Pointer to 64 bit bde structure
2865  * @size: Number of bytes to process
2866  * @nocopydata: Flag to copy user data into the allocated buffer
2867  *
2868  * This function allocates page size buffers and populates an lpfc_dmabufext.
2869  * If allowed the user data pointed to with indataptr is copied into the kernel
2870  * memory. The chained list of page size buffers is returned.
2871  **/
2872 static struct lpfc_dmabufext *
2873 diag_cmd_data_alloc(struct lpfc_hba *phba,
2874                    struct ulp_bde64 *bpl, uint32_t size,
2875                    int nocopydata)
2876 {
2877         struct lpfc_dmabufext *mlist = NULL;
2878         struct lpfc_dmabufext *dmp;
2879         int cnt, offset = 0, i = 0;
2880         struct pci_dev *pcidev;
2881
2882         pcidev = phba->pcidev;
2883
2884         while (size) {
2885                 /* We get chunks of 4K */
2886                 if (size > BUF_SZ_4K)
2887                         cnt = BUF_SZ_4K;
2888                 else
2889                         cnt = size;
2890
2891                 /* allocate struct lpfc_dmabufext buffer header */
2892                 dmp = kmalloc(sizeof(struct lpfc_dmabufext), GFP_KERNEL);
2893                 if (!dmp)
2894                         goto out;
2895
2896                 INIT_LIST_HEAD(&dmp->dma.list);
2897
2898                 /* Queue it to a linked list */
2899                 if (mlist)
2900                         list_add_tail(&dmp->dma.list, &mlist->dma.list);
2901                 else
2902                         mlist = dmp;
2903
2904                 /* allocate buffer */
2905                 dmp->dma.virt = dma_alloc_coherent(&pcidev->dev,
2906                                                    cnt,
2907                                                    &(dmp->dma.phys),
2908                                                    GFP_KERNEL);
2909
2910                 if (!dmp->dma.virt)
2911                         goto out;
2912
2913                 dmp->size = cnt;
2914
2915                 if (nocopydata) {
2916                         bpl->tus.f.bdeFlags = 0;
2917                 } else {
2918                         memset((uint8_t *)dmp->dma.virt, 0, cnt);
2919                         bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2920                 }
2921
2922                 /* build buffer ptr list for IOCB */
2923                 bpl->addrLow = le32_to_cpu(putPaddrLow(dmp->dma.phys));
2924                 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dmp->dma.phys));
2925                 bpl->tus.f.bdeSize = (ushort) cnt;
2926                 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2927                 bpl++;
2928
2929                 i++;
2930                 offset += cnt;
2931                 size -= cnt;
2932         }
2933
2934         if (mlist) {
2935                 mlist->flag = i;
2936                 return mlist;
2937         }
2938 out:
2939         diag_cmd_data_free(phba, mlist);
2940         return NULL;
2941 }
2942
2943 /**
2944  * lpfcdiag_loop_post_rxbufs - post the receive buffers for an unsol CT cmd
2945  * @phba: Pointer to HBA context object
2946  * @rxxri: Receive exchange id
2947  * @len: Number of data bytes
2948  *
2949  * This function allocates and posts a data buffer of sufficient size to receive
2950  * an unsolicted CT command.
2951  **/
2952 static int lpfcdiag_loop_post_rxbufs(struct lpfc_hba *phba, uint16_t rxxri,
2953                              size_t len)
2954 {
2955         struct lpfc_sli_ring *pring;
2956         struct lpfc_iocbq *cmdiocbq;
2957         IOCB_t *cmd = NULL;
2958         struct list_head head, *curr, *next;
2959         struct lpfc_dmabuf *rxbmp;
2960         struct lpfc_dmabuf *dmp;
2961         struct lpfc_dmabuf *mp[2] = {NULL, NULL};
2962         struct ulp_bde64 *rxbpl = NULL;
2963         uint32_t num_bde;
2964         struct lpfc_dmabufext *rxbuffer = NULL;
2965         int ret_val = 0;
2966         int iocb_stat;
2967         int i = 0;
2968
2969         pring = lpfc_phba_elsring(phba);
2970
2971         cmdiocbq = lpfc_sli_get_iocbq(phba);
2972         rxbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2973         if (rxbmp != NULL) {
2974                 rxbmp->virt = lpfc_mbuf_alloc(phba, 0, &rxbmp->phys);
2975                 if (rxbmp->virt) {
2976                         INIT_LIST_HEAD(&rxbmp->list);
2977                         rxbpl = (struct ulp_bde64 *) rxbmp->virt;
2978                         rxbuffer = diag_cmd_data_alloc(phba, rxbpl, len, 0);
2979                 }
2980         }
2981
2982         if (!cmdiocbq || !rxbmp || !rxbpl || !rxbuffer || !pring) {
2983                 ret_val = -ENOMEM;
2984                 goto err_post_rxbufs_exit;
2985         }
2986
2987         /* Queue buffers for the receive exchange */
2988         num_bde = (uint32_t)rxbuffer->flag;
2989         dmp = &rxbuffer->dma;
2990
2991         cmd = &cmdiocbq->iocb;
2992         i = 0;
2993
2994         INIT_LIST_HEAD(&head);
2995         list_add_tail(&head, &dmp->list);
2996         list_for_each_safe(curr, next, &head) {
2997                 mp[i] = list_entry(curr, struct lpfc_dmabuf, list);
2998                 list_del(curr);
2999
3000                 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
3001                         mp[i]->buffer_tag = lpfc_sli_get_buffer_tag(phba);
3002                         cmd->un.quexri64cx.buff.bde.addrHigh =
3003                                 putPaddrHigh(mp[i]->phys);
3004                         cmd->un.quexri64cx.buff.bde.addrLow =
3005                                 putPaddrLow(mp[i]->phys);
3006                         cmd->un.quexri64cx.buff.bde.tus.f.bdeSize =
3007                                 ((struct lpfc_dmabufext *)mp[i])->size;
3008                         cmd->un.quexri64cx.buff.buffer_tag = mp[i]->buffer_tag;
3009                         cmd->ulpCommand = CMD_QUE_XRI64_CX;
3010                         cmd->ulpPU = 0;
3011                         cmd->ulpLe = 1;
3012                         cmd->ulpBdeCount = 1;
3013                         cmd->unsli3.que_xri64cx_ext_words.ebde_count = 0;
3014
3015                 } else {
3016                         cmd->un.cont64[i].addrHigh = putPaddrHigh(mp[i]->phys);
3017                         cmd->un.cont64[i].addrLow = putPaddrLow(mp[i]->phys);
3018                         cmd->un.cont64[i].tus.f.bdeSize =
3019                                 ((struct lpfc_dmabufext *)mp[i])->size;
3020                         cmd->ulpBdeCount = ++i;
3021
3022                         if ((--num_bde > 0) && (i < 2))
3023                                 continue;
3024
3025                         cmd->ulpCommand = CMD_QUE_XRI_BUF64_CX;
3026                         cmd->ulpLe = 1;
3027                 }
3028
3029                 cmd->ulpClass = CLASS3;
3030                 cmd->ulpContext = rxxri;
3031
3032                 iocb_stat = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq,
3033                                                 0);
3034                 if (iocb_stat == IOCB_ERROR) {
3035                         diag_cmd_data_free(phba,
3036                                 (struct lpfc_dmabufext *)mp[0]);
3037                         if (mp[1])
3038                                 diag_cmd_data_free(phba,
3039                                           (struct lpfc_dmabufext *)mp[1]);
3040                         dmp = list_entry(next, struct lpfc_dmabuf, list);
3041                         ret_val = -EIO;
3042                         goto err_post_rxbufs_exit;
3043                 }
3044
3045                 lpfc_sli_ringpostbuf_put(phba, pring, mp[0]);
3046                 if (mp[1]) {
3047                         lpfc_sli_ringpostbuf_put(phba, pring, mp[1]);
3048                         mp[1] = NULL;
3049                 }
3050
3051                 /* The iocb was freed by lpfc_sli_issue_iocb */
3052                 cmdiocbq = lpfc_sli_get_iocbq(phba);
3053                 if (!cmdiocbq) {
3054                         dmp = list_entry(next, struct lpfc_dmabuf, list);
3055                         ret_val = -EIO;
3056                         goto err_post_rxbufs_exit;
3057                 }
3058
3059                 cmd = &cmdiocbq->iocb;
3060                 i = 0;
3061         }
3062         list_del(&head);
3063
3064 err_post_rxbufs_exit:
3065
3066         if (rxbmp) {
3067                 if (rxbmp->virt)
3068                         lpfc_mbuf_free(phba, rxbmp->virt, rxbmp->phys);
3069                 kfree(rxbmp);
3070         }
3071
3072         if (cmdiocbq)
3073                 lpfc_sli_release_iocbq(phba, cmdiocbq);
3074         return ret_val;
3075 }
3076
3077 /**
3078  * lpfc_bsg_diag_loopback_run - run loopback on a port by issue ct cmd to itself
3079  * @job: LPFC_BSG_VENDOR_DIAG_TEST fc_bsg_job
3080  *
3081  * This function receives a user data buffer to be transmitted and received on
3082  * the same port, the link must be up and in loopback mode prior
3083  * to being called.
3084  * 1. A kernel buffer is allocated to copy the user data into.
3085  * 2. The port registers with "itself".
3086  * 3. The transmit and receive exchange ids are obtained.
3087  * 4. The receive exchange id is posted.
3088  * 5. A new els loopback event is created.
3089  * 6. The command and response iocbs are allocated.
3090  * 7. The cmd iocb FsType is set to elx loopback and the CmdRsp to looppback.
3091  *
3092  * This function is meant to be called n times while the port is in loopback
3093  * so it is the apps responsibility to issue a reset to take the port out
3094  * of loopback mode.
3095  **/
3096 static int
3097 lpfc_bsg_diag_loopback_run(struct bsg_job *job)
3098 {
3099         struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
3100         struct fc_bsg_reply *bsg_reply = job->reply;
3101         struct lpfc_hba *phba = vport->phba;
3102         struct lpfc_bsg_event *evt;
3103         struct event_data *evdat;
3104         struct lpfc_sli *psli = &phba->sli;
3105         uint32_t size;
3106         uint32_t full_size;
3107         size_t segment_len = 0, segment_offset = 0, current_offset = 0;
3108         uint16_t rpi = 0;
3109         struct lpfc_iocbq *cmdiocbq, *rspiocbq = NULL;
3110         IOCB_t *cmd, *rsp = NULL;
3111         struct lpfc_sli_ct_request *ctreq;
3112         struct lpfc_dmabuf *txbmp;
3113         struct ulp_bde64 *txbpl = NULL;
3114         struct lpfc_dmabufext *txbuffer = NULL;
3115         struct list_head head;
3116         struct lpfc_dmabuf  *curr;
3117         uint16_t txxri = 0, rxxri;
3118         uint32_t num_bde;
3119         uint8_t *ptr = NULL, *rx_databuf = NULL;
3120         int rc = 0;
3121         int time_left;
3122         int iocb_stat = IOCB_SUCCESS;
3123         unsigned long flags;
3124         void *dataout = NULL;
3125         uint32_t total_mem;
3126
3127         /* in case no data is returned return just the return code */
3128         bsg_reply->reply_payload_rcv_len = 0;
3129
3130         if (job->request_len <
3131             sizeof(struct fc_bsg_request) + sizeof(struct diag_mode_test)) {
3132                 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3133                                 "2739 Received DIAG TEST request below minimum "
3134                                 "size\n");
3135                 rc = -EINVAL;
3136                 goto loopback_test_exit;
3137         }
3138
3139         if (job->request_payload.payload_len !=
3140                 job->reply_payload.payload_len) {
3141                 rc = -EINVAL;
3142                 goto loopback_test_exit;
3143         }
3144
3145         if ((phba->link_state == LPFC_HBA_ERROR) ||
3146             (psli->sli_flag & LPFC_BLOCK_MGMT_IO) ||
3147             (!(psli->sli_flag & LPFC_SLI_ACTIVE))) {
3148                 rc = -EACCES;
3149                 goto loopback_test_exit;
3150         }
3151
3152         if (!lpfc_is_link_up(phba) || !(phba->link_flag & LS_LOOPBACK_MODE)) {
3153                 rc = -EACCES;
3154                 goto loopback_test_exit;
3155         }
3156
3157         size = job->request_payload.payload_len;
3158         full_size = size + ELX_LOOPBACK_HEADER_SZ; /* plus the header */
3159
3160         if ((size == 0) || (size > 80 * BUF_SZ_4K)) {
3161                 rc = -ERANGE;
3162                 goto loopback_test_exit;
3163         }
3164
3165         if (full_size >= BUF_SZ_4K) {
3166                 /*
3167                  * Allocate memory for ioctl data. If buffer is bigger than 64k,
3168                  * then we allocate 64k and re-use that buffer over and over to
3169                  * xfer the whole block. This is because Linux kernel has a
3170                  * problem allocating more than 120k of kernel space memory. Saw
3171                  * problem with GET_FCPTARGETMAPPING...
3172                  */
3173                 if (size <= (64 * 1024))
3174                         total_mem = full_size;
3175                 else
3176                         total_mem = 64 * 1024;
3177         } else
3178                 /* Allocate memory for ioctl data */
3179                 total_mem = BUF_SZ_4K;
3180
3181         dataout = kmalloc(total_mem, GFP_KERNEL);
3182         if (dataout == NULL) {
3183                 rc = -ENOMEM;
3184                 goto loopback_test_exit;
3185         }
3186
3187         ptr = dataout;
3188         ptr += ELX_LOOPBACK_HEADER_SZ;
3189         sg_copy_to_buffer(job->request_payload.sg_list,
3190                                 job->request_payload.sg_cnt,
3191                                 ptr, size);
3192         rc = lpfcdiag_loop_self_reg(phba, &rpi);
3193         if (rc)
3194                 goto loopback_test_exit;
3195
3196         if (phba->sli_rev < LPFC_SLI_REV4) {
3197                 rc = lpfcdiag_loop_get_xri(phba, rpi, &txxri, &rxxri);
3198                 if (rc) {
3199                         lpfcdiag_loop_self_unreg(phba, rpi);
3200                         goto loopback_test_exit;
3201                 }
3202
3203                 rc = lpfcdiag_loop_post_rxbufs(phba, rxxri, full_size);
3204                 if (rc) {
3205                         lpfcdiag_loop_self_unreg(phba, rpi);
3206                         goto loopback_test_exit;
3207                 }
3208         }
3209         evt = lpfc_bsg_event_new(FC_REG_CT_EVENT, current->pid,
3210                                 SLI_CT_ELX_LOOPBACK);
3211         if (!evt) {
3212                 lpfcdiag_loop_self_unreg(phba, rpi);
3213                 rc = -ENOMEM;
3214                 goto loopback_test_exit;
3215         }
3216
3217         spin_lock_irqsave(&phba->ct_ev_lock, flags);
3218         list_add(&evt->node, &phba->ct_ev_waiters);
3219         lpfc_bsg_event_ref(evt);
3220         spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3221
3222         cmdiocbq = lpfc_sli_get_iocbq(phba);
3223         if (phba->sli_rev < LPFC_SLI_REV4)
3224                 rspiocbq = lpfc_sli_get_iocbq(phba);
3225         txbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3226
3227         if (txbmp) {
3228                 txbmp->virt = lpfc_mbuf_alloc(phba, 0, &txbmp->phys);
3229                 if (txbmp->virt) {
3230                         INIT_LIST_HEAD(&txbmp->list);
3231                         txbpl = (struct ulp_bde64 *) txbmp->virt;
3232                         txbuffer = diag_cmd_data_alloc(phba,
3233                                                         txbpl, full_size, 0);
3234                 }
3235         }
3236
3237         if (!cmdiocbq || !txbmp || !txbpl || !txbuffer || !txbmp->virt) {
3238                 rc = -ENOMEM;
3239                 goto err_loopback_test_exit;
3240         }
3241         if ((phba->sli_rev < LPFC_SLI_REV4) && !rspiocbq) {
3242                 rc = -ENOMEM;
3243                 goto err_loopback_test_exit;
3244         }
3245
3246         cmd = &cmdiocbq->iocb;
3247         if (phba->sli_rev < LPFC_SLI_REV4)
3248                 rsp = &rspiocbq->iocb;
3249
3250         INIT_LIST_HEAD(&head);
3251         list_add_tail(&head, &txbuffer->dma.list);
3252         list_for_each_entry(curr, &head, list) {
3253                 segment_len = ((struct lpfc_dmabufext *)curr)->size;
3254                 if (current_offset == 0) {
3255                         ctreq = curr->virt;
3256                         memset(ctreq, 0, ELX_LOOPBACK_HEADER_SZ);
3257                         ctreq->RevisionId.bits.Revision = SLI_CT_REVISION;
3258                         ctreq->RevisionId.bits.InId = 0;
3259                         ctreq->FsType = SLI_CT_ELX_LOOPBACK;
3260                         ctreq->FsSubType = 0;
3261                         ctreq->CommandResponse.bits.CmdRsp = ELX_LOOPBACK_DATA;
3262                         ctreq->CommandResponse.bits.Size   = size;
3263                         segment_offset = ELX_LOOPBACK_HEADER_SZ;
3264                 } else
3265                         segment_offset = 0;
3266
3267                 BUG_ON(segment_offset >= segment_len);
3268                 memcpy(curr->virt + segment_offset,
3269                         ptr + current_offset,
3270                         segment_len - segment_offset);
3271
3272                 current_offset += segment_len - segment_offset;
3273                 BUG_ON(current_offset > size);
3274         }
3275         list_del(&head);
3276
3277         /* Build the XMIT_SEQUENCE iocb */
3278         num_bde = (uint32_t)txbuffer->flag;
3279
3280         cmd->un.xseq64.bdl.addrHigh = putPaddrHigh(txbmp->phys);
3281         cmd->un.xseq64.bdl.addrLow = putPaddrLow(txbmp->phys);
3282         cmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
3283         cmd->un.xseq64.bdl.bdeSize = (num_bde * sizeof(struct ulp_bde64));
3284
3285         cmd->un.xseq64.w5.hcsw.Fctl = (LS | LA);
3286         cmd->un.xseq64.w5.hcsw.Dfctl = 0;
3287         cmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
3288         cmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
3289
3290         cmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;
3291         cmd->ulpBdeCount = 1;
3292         cmd->ulpLe = 1;
3293         cmd->ulpClass = CLASS3;
3294
3295         if (phba->sli_rev < LPFC_SLI_REV4) {
3296                 cmd->ulpContext = txxri;
3297         } else {
3298                 cmd->un.xseq64.bdl.ulpIoTag32 = 0;
3299                 cmd->un.ulpWord[3] = phba->sli4_hba.rpi_ids[rpi];
3300                 cmdiocbq->context3 = txbmp;
3301                 cmdiocbq->sli4_xritag = NO_XRI;
3302                 cmd->unsli3.rcvsli3.ox_id = 0xffff;
3303         }
3304         cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
3305         cmdiocbq->iocb_flag |= LPFC_IO_LOOPBACK;
3306         cmdiocbq->vport = phba->pport;
3307         cmdiocbq->iocb_cmpl = NULL;
3308         iocb_stat = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq,
3309                                              rspiocbq, (phba->fc_ratov * 2) +
3310                                              LPFC_DRVR_TIMEOUT);
3311
3312         if ((iocb_stat != IOCB_SUCCESS) ||
3313             ((phba->sli_rev < LPFC_SLI_REV4) &&
3314              (rsp->ulpStatus != IOSTAT_SUCCESS))) {
3315                 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3316                                 "3126 Failed loopback test issue iocb: "
3317                                 "iocb_stat:x%x\n", iocb_stat);
3318                 rc = -EIO;
3319                 goto err_loopback_test_exit;
3320         }
3321
3322         evt->waiting = 1;
3323         time_left = wait_event_interruptible_timeout(
3324                 evt->wq, !list_empty(&evt->events_to_see),
3325                 msecs_to_jiffies(1000 *
3326                         ((phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT)));
3327         evt->waiting = 0;
3328         if (list_empty(&evt->events_to_see)) {
3329                 rc = (time_left) ? -EINTR : -ETIMEDOUT;
3330                 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3331                                 "3125 Not receiving unsolicited event, "
3332                                 "rc:x%x\n", rc);
3333         } else {
3334                 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3335                 list_move(evt->events_to_see.prev, &evt->events_to_get);
3336                 evdat = list_entry(evt->events_to_get.prev,
3337                                    typeof(*evdat), node);
3338                 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3339                 rx_databuf = evdat->data;
3340                 if (evdat->len != full_size) {
3341                         lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3342                                 "1603 Loopback test did not receive expected "
3343                                 "data length. actual length 0x%x expected "
3344                                 "length 0x%x\n",
3345                                 evdat->len, full_size);
3346                         rc = -EIO;
3347                 } else if (rx_databuf == NULL)
3348                         rc = -EIO;
3349                 else {
3350                         rc = IOCB_SUCCESS;
3351                         /* skip over elx loopback header */
3352                         rx_databuf += ELX_LOOPBACK_HEADER_SZ;
3353                         bsg_reply->reply_payload_rcv_len =
3354                                 sg_copy_from_buffer(job->reply_payload.sg_list,
3355                                                     job->reply_payload.sg_cnt,
3356                                                     rx_databuf, size);
3357                         bsg_reply->reply_payload_rcv_len = size;
3358                 }
3359         }
3360
3361 err_loopback_test_exit:
3362         lpfcdiag_loop_self_unreg(phba, rpi);
3363
3364         spin_lock_irqsave(&phba->ct_ev_lock, flags);
3365         lpfc_bsg_event_unref(evt); /* release ref */
3366         lpfc_bsg_event_unref(evt); /* delete */
3367         spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3368
3369         if ((cmdiocbq != NULL) && (iocb_stat != IOCB_TIMEDOUT))
3370                 lpfc_sli_release_iocbq(phba, cmdiocbq);
3371
3372         if (rspiocbq != NULL)
3373                 lpfc_sli_release_iocbq(phba, rspiocbq);
3374
3375         if (txbmp != NULL) {
3376                 if (txbpl != NULL) {
3377                         if (txbuffer != NULL)
3378                                 diag_cmd_data_free(phba, txbuffer);
3379                         lpfc_mbuf_free(phba, txbmp->virt, txbmp->phys);
3380                 }
3381                 kfree(txbmp);
3382         }
3383
3384 loopback_test_exit:
3385         kfree(dataout);
3386         /* make error code available to userspace */
3387         bsg_reply->result = rc;
3388         job->dd_data = NULL;
3389         /* complete the job back to userspace if no error */
3390         if (rc == IOCB_SUCCESS)
3391                 bsg_job_done(job, bsg_reply->result,
3392                                bsg_reply->reply_payload_rcv_len);
3393         return rc;
3394 }
3395
3396 /**
3397  * lpfc_bsg_get_dfc_rev - process a GET_DFC_REV bsg vendor command
3398  * @job: GET_DFC_REV fc_bsg_job
3399  **/
3400 static int
3401 lpfc_bsg_get_dfc_rev(struct bsg_job *job)
3402 {
3403         struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
3404         struct fc_bsg_reply *bsg_reply = job->reply;
3405         struct lpfc_hba *phba = vport->phba;
3406         struct get_mgmt_rev_reply *event_reply;
3407         int rc = 0;
3408
3409         if (job->request_len <
3410             sizeof(struct fc_bsg_request) + sizeof(struct get_mgmt_rev)) {
3411                 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3412                                 "2740 Received GET_DFC_REV request below "
3413                                 "minimum size\n");
3414                 rc = -EINVAL;
3415                 goto job_error;
3416         }
3417
3418         event_reply = (struct get_mgmt_rev_reply *)
3419                 bsg_reply->reply_data.vendor_reply.vendor_rsp;
3420
3421         if (job->reply_len <
3422             sizeof(struct fc_bsg_request) + sizeof(struct get_mgmt_rev_reply)) {
3423                 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3424                                 "2741 Received GET_DFC_REV reply below "
3425                                 "minimum size\n");
3426                 rc = -EINVAL;
3427                 goto job_error;
3428         }
3429
3430         event_reply->info.a_Major = MANAGEMENT_MAJOR_REV;
3431         event_reply->info.a_Minor = MANAGEMENT_MINOR_REV;
3432 job_error:
3433         bsg_reply->result = rc;
3434         if (rc == 0)
3435                 bsg_job_done(job, bsg_reply->result,
3436                                bsg_reply->reply_payload_rcv_len);
3437         return rc;
3438 }
3439
3440 /**
3441  * lpfc_bsg_issue_mbox_cmpl - lpfc_bsg_issue_mbox mbox completion handler
3442  * @phba: Pointer to HBA context object.
3443  * @pmboxq: Pointer to mailbox command.
3444  *
3445  * This is completion handler function for mailbox commands issued from
3446  * lpfc_bsg_issue_mbox function. This function is called by the
3447  * mailbox event handler function with no lock held. This function
3448  * will wake up thread waiting on the wait queue pointed by context1
3449  * of the mailbox.
3450  **/
3451 static void
3452 lpfc_bsg_issue_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
3453 {
3454         struct bsg_job_data *dd_data;
3455         struct fc_bsg_reply *bsg_reply;
3456         struct bsg_job *job;
3457         uint32_t size;
3458         unsigned long flags;
3459         uint8_t *pmb, *pmb_buf;
3460
3461         dd_data = pmboxq->ctx_ndlp;
3462
3463         /*
3464          * The outgoing buffer is readily referred from the dma buffer,
3465          * just need to get header part from mailboxq structure.
3466          */
3467         pmb = (uint8_t *)&pmboxq->u.mb;
3468         pmb_buf = (uint8_t *)dd_data->context_un.mbox.mb;
3469         memcpy(pmb_buf, pmb, sizeof(MAILBOX_t));
3470
3471         /* Determine if job has been aborted */
3472
3473         spin_lock_irqsave(&phba->ct_ev_lock, flags);
3474         job = dd_data->set_job;
3475         if (job) {
3476                 /* Prevent timeout handling from trying to abort job  */
3477                 job->dd_data = NULL;
3478         }
3479         spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3480
3481         /* Copy the mailbox data to the job if it is still active */
3482
3483         if (job) {
3484                 bsg_reply = job->reply;
3485                 size = job->reply_payload.payload_len;
3486                 bsg_reply->reply_payload_rcv_len =
3487                         sg_copy_from_buffer(job->reply_payload.sg_list,
3488                                             job->reply_payload.sg_cnt,
3489                                             pmb_buf, size);
3490         }
3491
3492         dd_data->set_job = NULL;
3493         mempool_free(dd_data->context_un.mbox.pmboxq, phba->mbox_mem_pool);
3494         lpfc_bsg_dma_page_free(phba, dd_data->context_un.mbox.dmabuffers);
3495         kfree(dd_data);
3496
3497         /* Complete the job if the job is still active */
3498
3499         if (job) {
3500                 bsg_reply->result = 0;
3501                 bsg_job_done(job, bsg_reply->result,
3502                                bsg_reply->reply_payload_rcv_len);
3503         }
3504         return;
3505 }
3506
3507 /**
3508  * lpfc_bsg_check_cmd_access - test for a supported mailbox command
3509  * @phba: Pointer to HBA context object.
3510  * @mb: Pointer to a mailbox object.
3511  * @vport: Pointer to a vport object.
3512  *
3513  * Some commands require the port to be offline, some may not be called from
3514  * the application.
3515  **/
3516 static int lpfc_bsg_check_cmd_access(struct lpfc_hba *phba,
3517         MAILBOX_t *mb, struct lpfc_vport *vport)
3518 {
3519         /* return negative error values for bsg job */
3520         switch (mb->mbxCommand) {
3521         /* Offline only */
3522         case MBX_INIT_LINK:
3523         case MBX_DOWN_LINK:
3524         case MBX_CONFIG_LINK:
3525         case MBX_CONFIG_RING:
3526         case MBX_RESET_RING:
3527         case MBX_UNREG_LOGIN:
3528         case MBX_CLEAR_LA:
3529         case MBX_DUMP_CONTEXT:
3530         case MBX_RUN_DIAGS:
3531         case MBX_RESTART:
3532         case MBX_SET_MASK:
3533                 if (!(vport->fc_flag & FC_OFFLINE_MODE)) {
3534                         lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3535                                 "2743 Command 0x%x is illegal in on-line "
3536                                 "state\n",
3537                                 mb->mbxCommand);
3538                         return -EPERM;
3539                 }
3540         case MBX_WRITE_NV:
3541         case MBX_WRITE_VPARMS:
3542         case MBX_LOAD_SM:
3543         case MBX_READ_NV:
3544         case MBX_READ_CONFIG:
3545         case MBX_READ_RCONFIG:
3546         case MBX_READ_STATUS:
3547         case MBX_READ_XRI:
3548         case MBX_READ_REV:
3549         case MBX_READ_LNK_STAT:
3550         case MBX_DUMP_MEMORY:
3551         case MBX_DOWN_LOAD:
3552         case MBX_UPDATE_CFG:
3553         case MBX_KILL_BOARD:
3554         case MBX_READ_TOPOLOGY:
3555         case MBX_LOAD_AREA:
3556         case MBX_LOAD_EXP_ROM:
3557         case MBX_BEACON:
3558         case MBX_DEL_LD_ENTRY:
3559         case MBX_SET_DEBUG:
3560         case MBX_WRITE_WWN:
3561         case MBX_SLI4_CONFIG:
3562         case MBX_READ_EVENT_LOG:
3563         case MBX_READ_EVENT_LOG_STATUS:
3564         case MBX_WRITE_EVENT_LOG:
3565         case MBX_PORT_CAPABILITIES:
3566         case MBX_PORT_IOV_CONTROL:
3567         case MBX_RUN_BIU_DIAG64:
3568                 break;
3569         case MBX_SET_VARIABLE:
3570                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3571                         "1226 mbox: set_variable 0x%x, 0x%x\n",
3572                         mb->un.varWords[0],
3573                         mb->un.varWords[1]);
3574                 if ((mb->un.varWords[0] == SETVAR_MLOMNT)
3575                         && (mb->un.varWords[1] == 1)) {
3576                         phba->wait_4_mlo_maint_flg = 1;
3577                 } else if (mb->un.varWords[0] == SETVAR_MLORST) {
3578                         spin_lock_irq(&phba->hbalock);
3579                         phba->link_flag &= ~LS_LOOPBACK_MODE;
3580                         spin_unlock_irq(&phba->hbalock);
3581                         phba->fc_topology = LPFC_TOPOLOGY_PT_PT;
3582                 }
3583                 break;
3584         case MBX_READ_SPARM64:
3585         case MBX_REG_LOGIN:
3586         case MBX_REG_LOGIN64:
3587         case MBX_CONFIG_PORT:
3588         case MBX_RUN_BIU_DIAG:
3589         default:
3590                 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3591                         "2742 Unknown Command 0x%x\n",
3592                         mb->mbxCommand);
3593                 return -EPERM;
3594         }
3595
3596         return 0; /* ok */
3597 }
3598
3599 /**
3600  * lpfc_bsg_mbox_ext_cleanup - clean up context of multi-buffer mbox session
3601  * @phba: Pointer to HBA context object.
3602  *
3603  * This is routine clean up and reset BSG handling of multi-buffer mbox
3604  * command session.
3605  **/
3606 static void
3607 lpfc_bsg_mbox_ext_session_reset(struct lpfc_hba *phba)
3608 {
3609         if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_IDLE)
3610                 return;
3611
3612         /* free all memory, including dma buffers */
3613         lpfc_bsg_dma_page_list_free(phba,
3614                                     &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
3615         lpfc_bsg_dma_page_free(phba, phba->mbox_ext_buf_ctx.mbx_dmabuf);
3616         /* multi-buffer write mailbox command pass-through complete */
3617         memset((char *)&phba->mbox_ext_buf_ctx, 0,
3618                sizeof(struct lpfc_mbox_ext_buf_ctx));
3619         INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
3620
3621         return;
3622 }
3623
3624 /**
3625  * lpfc_bsg_issue_mbox_ext_handle_job - job handler for multi-buffer mbox cmpl
3626  * @phba: Pointer to HBA context object.
3627  * @pmboxq: Pointer to mailbox command.
3628  *
3629  * This is routine handles BSG job for mailbox commands completions with
3630  * multiple external buffers.
3631  **/
3632 static struct bsg_job *
3633 lpfc_bsg_issue_mbox_ext_handle_job(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
3634 {
3635         struct bsg_job_data *dd_data;
3636         struct bsg_job *job;
3637         struct fc_bsg_reply *bsg_reply;
3638         uint8_t *pmb, *pmb_buf;
3639         unsigned long flags;
3640         uint32_t size;
3641         int rc = 0;
3642         struct lpfc_dmabuf *dmabuf;
3643         struct lpfc_sli_config_mbox *sli_cfg_mbx;
3644         uint8_t *pmbx;
3645
3646         dd_data = pmboxq->ctx_buf;
3647
3648         /* Determine if job has been aborted */
3649         spin_lock_irqsave(&phba->ct_ev_lock, flags);
3650         job = dd_data->set_job;
3651         if (job) {
3652                 bsg_reply = job->reply;
3653                 /* Prevent timeout handling from trying to abort job  */
3654                 job->dd_data = NULL;
3655         }
3656         spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3657
3658         /*
3659          * The outgoing buffer is readily referred from the dma buffer,
3660          * just need to get header part from mailboxq structure.
3661          */
3662
3663         pmb = (uint8_t *)&pmboxq->u.mb;
3664         pmb_buf = (uint8_t *)dd_data->context_un.mbox.mb;
3665         /* Copy the byte swapped response mailbox back to the user */
3666         memcpy(pmb_buf, pmb, sizeof(MAILBOX_t));
3667         /* if there is any non-embedded extended data copy that too */
3668         dmabuf = phba->mbox_ext_buf_ctx.mbx_dmabuf;
3669         sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
3670         if (!bsg_bf_get(lpfc_mbox_hdr_emb,
3671             &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) {
3672                 pmbx = (uint8_t *)dmabuf->virt;
3673                 /* byte swap the extended data following the mailbox command */
3674                 lpfc_sli_pcimem_bcopy(&pmbx[sizeof(MAILBOX_t)],
3675                         &pmbx[sizeof(MAILBOX_t)],
3676                         sli_cfg_mbx->un.sli_config_emb0_subsys.mse[0].buf_len);
3677         }
3678
3679         /* Complete the job if the job is still active */
3680
3681         if (job) {
3682                 size = job->reply_payload.payload_len;
3683                 bsg_reply->reply_payload_rcv_len =
3684                         sg_copy_from_buffer(job->reply_payload.sg_list,
3685                                             job->reply_payload.sg_cnt,
3686                                             pmb_buf, size);
3687
3688                 /* result for successful */
3689                 bsg_reply->result = 0;
3690
3691                 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3692                                 "2937 SLI_CONFIG ext-buffer mailbox command "
3693                                 "(x%x/x%x) complete bsg job done, bsize:%d\n",
3694                                 phba->mbox_ext_buf_ctx.nembType,
3695                                 phba->mbox_ext_buf_ctx.mboxType, size);
3696                 lpfc_idiag_mbxacc_dump_bsg_mbox(phba,
3697                                         phba->mbox_ext_buf_ctx.nembType,
3698                                         phba->mbox_ext_buf_ctx.mboxType,
3699                                         dma_ebuf, sta_pos_addr,
3700                                         phba->mbox_ext_buf_ctx.mbx_dmabuf, 0);
3701         } else {
3702                 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3703                                 "2938 SLI_CONFIG ext-buffer mailbox "
3704                                 "command (x%x/x%x) failure, rc:x%x\n",
3705                                 phba->mbox_ext_buf_ctx.nembType,
3706                                 phba->mbox_ext_buf_ctx.mboxType, rc);
3707         }
3708
3709
3710         /* state change */
3711         phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_DONE;
3712         kfree(dd_data);
3713         return job;
3714 }
3715
3716 /**
3717  * lpfc_bsg_issue_read_mbox_ext_cmpl - compl handler for multi-buffer read mbox
3718  * @phba: Pointer to HBA context object.
3719  * @pmboxq: Pointer to mailbox command.
3720  *
3721  * This is completion handler function for mailbox read commands with multiple
3722  * external buffers.
3723  **/
3724 static void
3725 lpfc_bsg_issue_read_mbox_ext_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
3726 {
3727         struct bsg_job *job;
3728         struct fc_bsg_reply *bsg_reply;
3729
3730         job = lpfc_bsg_issue_mbox_ext_handle_job(phba, pmboxq);
3731
3732         /* handle the BSG job with mailbox command */
3733         if (!job)
3734                 pmboxq->u.mb.mbxStatus = MBXERR_ERROR;
3735
3736         lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3737                         "2939 SLI_CONFIG ext-buffer rd mailbox command "
3738                         "complete, ctxState:x%x, mbxStatus:x%x\n",
3739                         phba->mbox_ext_buf_ctx.state, pmboxq->u.mb.mbxStatus);
3740
3741         if (pmboxq->u.mb.mbxStatus || phba->mbox_ext_buf_ctx.numBuf == 1)
3742                 lpfc_bsg_mbox_ext_session_reset(phba);
3743
3744         /* free base driver mailbox structure memory */
3745         mempool_free(pmboxq, phba->mbox_mem_pool);
3746
3747         /* if the job is still active, call job done */
3748         if (job) {
3749                 bsg_reply = job->reply;
3750                 bsg_job_done(job, bsg_reply->result,
3751                                bsg_reply->reply_payload_rcv_len);
3752         }
3753         return;
3754 }
3755
3756 /**
3757  * lpfc_bsg_issue_write_mbox_ext_cmpl - cmpl handler for multi-buffer write mbox
3758  * @phba: Pointer to HBA context object.
3759  * @pmboxq: Pointer to mailbox command.
3760  *
3761  * This is completion handler function for mailbox write commands with multiple
3762  * external buffers.
3763  **/
3764 static void
3765 lpfc_bsg_issue_write_mbox_ext_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
3766 {
3767         struct bsg_job *job;
3768         struct fc_bsg_reply *bsg_reply;
3769
3770         job = lpfc_bsg_issue_mbox_ext_handle_job(phba, pmboxq);
3771
3772         /* handle the BSG job with the mailbox command */
3773         if (!job)
3774                 pmboxq->u.mb.mbxStatus = MBXERR_ERROR;
3775
3776         lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3777                         "2940 SLI_CONFIG ext-buffer wr mailbox command "
3778                         "complete, ctxState:x%x, mbxStatus:x%x\n",
3779                         phba->mbox_ext_buf_ctx.state, pmboxq->u.mb.mbxStatus);
3780
3781         /* free all memory, including dma buffers */
3782         mempool_free(pmboxq, phba->mbox_mem_pool);
3783         lpfc_bsg_mbox_ext_session_reset(phba);
3784
3785         /* if the job is still active, call job done */
3786         if (job) {
3787                 bsg_reply = job->reply;
3788                 bsg_job_done(job, bsg_reply->result,
3789                                bsg_reply->reply_payload_rcv_len);
3790         }
3791
3792         return;
3793 }
3794
3795 static void
3796 lpfc_bsg_sli_cfg_dma_desc_setup(struct lpfc_hba *phba, enum nemb_type nemb_tp,
3797                                 uint32_t index, struct lpfc_dmabuf *mbx_dmabuf,
3798                                 struct lpfc_dmabuf *ext_dmabuf)
3799 {
3800         struct lpfc_sli_config_mbox *sli_cfg_mbx;
3801
3802         /* pointer to the start of mailbox command */
3803         sli_cfg_mbx = (struct lpfc_sli_config_mbox *)mbx_dmabuf->virt;
3804
3805         if (nemb_tp == nemb_mse) {
3806                 if (index == 0) {
3807                         sli_cfg_mbx->un.sli_config_emb0_subsys.
3808                                 mse[index].pa_hi =
3809                                 putPaddrHigh(mbx_dmabuf->phys +
3810                                              sizeof(MAILBOX_t));
3811                         sli_cfg_mbx->un.sli_config_emb0_subsys.
3812                                 mse[index].pa_lo =
3813                                 putPaddrLow(mbx_dmabuf->phys +
3814                                             sizeof(MAILBOX_t));
3815                         lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3816                                         "2943 SLI_CONFIG(mse)[%d], "
3817                                         "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3818                                         index,
3819                                         sli_cfg_mbx->un.sli_config_emb0_subsys.
3820                                         mse[index].buf_len,
3821                                         sli_cfg_mbx->un.sli_config_emb0_subsys.
3822                                         mse[index].pa_hi,
3823                                         sli_cfg_mbx->un.sli_config_emb0_subsys.
3824                                         mse[index].pa_lo);
3825                 } else {
3826                         sli_cfg_mbx->un.sli_config_emb0_subsys.
3827                                 mse[index].pa_hi =
3828                                 putPaddrHigh(ext_dmabuf->phys);
3829                         sli_cfg_mbx->un.sli_config_emb0_subsys.
3830                                 mse[index].pa_lo =
3831                                 putPaddrLow(ext_dmabuf->phys);
3832                         lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3833                                         "2944 SLI_CONFIG(mse)[%d], "
3834                                         "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3835                                         index,
3836                                         sli_cfg_mbx->un.sli_config_emb0_subsys.
3837                                         mse[index].buf_len,
3838                                         sli_cfg_mbx->un.sli_config_emb0_subsys.
3839                                         mse[index].pa_hi,
3840                                         sli_cfg_mbx->un.sli_config_emb0_subsys.
3841                                         mse[index].pa_lo);
3842                 }
3843         } else {
3844                 if (index == 0) {
3845                         sli_cfg_mbx->un.sli_config_emb1_subsys.
3846                                 hbd[index].pa_hi =
3847                                 putPaddrHigh(mbx_dmabuf->phys +
3848                                              sizeof(MAILBOX_t));
3849                         sli_cfg_mbx->un.sli_config_emb1_subsys.
3850                                 hbd[index].pa_lo =
3851                                 putPaddrLow(mbx_dmabuf->phys +
3852                                             sizeof(MAILBOX_t));
3853                         lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3854                                         "3007 SLI_CONFIG(hbd)[%d], "
3855                                         "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3856                                 index,
3857                                 bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
3858                                 &sli_cfg_mbx->un.
3859                                 sli_config_emb1_subsys.hbd[index]),
3860                                 sli_cfg_mbx->un.sli_config_emb1_subsys.
3861                                 hbd[index].pa_hi,
3862                                 sli_cfg_mbx->un.sli_config_emb1_subsys.
3863                                 hbd[index].pa_lo);
3864
3865                 } else {
3866                         sli_cfg_mbx->un.sli_config_emb1_subsys.
3867                                 hbd[index].pa_hi =
3868                                 putPaddrHigh(ext_dmabuf->phys);
3869                         sli_cfg_mbx->un.sli_config_emb1_subsys.
3870                                 hbd[index].pa_lo =
3871                                 putPaddrLow(ext_dmabuf->phys);
3872                         lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3873                                         "3008 SLI_CONFIG(hbd)[%d], "
3874                                         "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3875                                 index,
3876                                 bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
3877                                 &sli_cfg_mbx->un.
3878                                 sli_config_emb1_subsys.hbd[index]),
3879                                 sli_cfg_mbx->un.sli_config_emb1_subsys.
3880                                 hbd[index].pa_hi,
3881                                 sli_cfg_mbx->un.sli_config_emb1_subsys.
3882                                 hbd[index].pa_lo);
3883                 }
3884         }
3885         return;
3886 }
3887
3888 /**
3889  * lpfc_bsg_sli_cfg_mse_read_cmd_ext - sli_config non-embedded mailbox cmd read
3890  * @phba: Pointer to HBA context object.
3891  * @mb: Pointer to a BSG mailbox object.
3892  * @nemb_tp: Enumerate of non-embedded mailbox command type.
3893  * @dmabuff: Pointer to a DMA buffer descriptor.
3894  *
3895  * This routine performs SLI_CONFIG (0x9B) read mailbox command operation with
3896  * non-embedded external bufffers.
3897  **/
3898 static int
3899 lpfc_bsg_sli_cfg_read_cmd_ext(struct lpfc_hba *phba, struct bsg_job *job,
3900                               enum nemb_type nemb_tp,
3901                               struct lpfc_dmabuf *dmabuf)
3902 {
3903         struct fc_bsg_request *bsg_request = job->request;
3904         struct lpfc_sli_config_mbox *sli_cfg_mbx;
3905         struct dfc_mbox_req *mbox_req;
3906         struct lpfc_dmabuf *curr_dmabuf, *next_dmabuf;
3907         uint32_t ext_buf_cnt, ext_buf_index;
3908         struct lpfc_dmabuf *ext_dmabuf = NULL;
3909         struct bsg_job_data *dd_data = NULL;
3910         LPFC_MBOXQ_t *pmboxq = NULL;
3911         MAILBOX_t *pmb;
3912         uint8_t *pmbx;
3913         int rc, i;
3914
3915         mbox_req =
3916            (struct dfc_mbox_req *)bsg_request->rqst_data.h_vendor.vendor_cmd;
3917
3918         /* pointer to the start of mailbox command */
3919         sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
3920
3921         if (nemb_tp == nemb_mse) {
3922                 ext_buf_cnt = bsg_bf_get(lpfc_mbox_hdr_mse_cnt,
3923                         &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr);
3924                 if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_MSE) {
3925                         lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3926                                         "2945 Handled SLI_CONFIG(mse) rd, "
3927                                         "ext_buf_cnt(%d) out of range(%d)\n",
3928                                         ext_buf_cnt,
3929                                         LPFC_MBX_SLI_CONFIG_MAX_MSE);
3930                         rc = -ERANGE;
3931                         goto job_error;
3932                 }
3933                 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3934                                 "2941 Handled SLI_CONFIG(mse) rd, "
3935                                 "ext_buf_cnt:%d\n", ext_buf_cnt);
3936         } else {
3937                 /* sanity check on interface type for support */
3938                 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
3939                     LPFC_SLI_INTF_IF_TYPE_2) {
3940                         rc = -ENODEV;
3941                         goto job_error;
3942                 }
3943                 /* nemb_tp == nemb_hbd */
3944                 ext_buf_cnt = sli_cfg_mbx->un.sli_config_emb1_subsys.hbd_count;
3945                 if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_HBD) {
3946                         lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3947                                         "2946 Handled SLI_CONFIG(hbd) rd, "
3948                                         "ext_buf_cnt(%d) out of range(%d)\n",
3949                                         ext_buf_cnt,
3950                                         LPFC_MBX_SLI_CONFIG_MAX_HBD);
3951                         rc = -ERANGE;
3952                         goto job_error;
3953                 }
3954                 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3955                                 "2942 Handled SLI_CONFIG(hbd) rd, "
3956                                 "ext_buf_cnt:%d\n", ext_buf_cnt);
3957         }
3958
3959         /* before dma descriptor setup */
3960         lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_rd, dma_mbox,
3961                                         sta_pre_addr, dmabuf, ext_buf_cnt);
3962
3963         /* reject non-embedded mailbox command with none external buffer */
3964         if (ext_buf_cnt == 0) {
3965                 rc = -EPERM;
3966                 goto job_error;
3967         } else if (ext_buf_cnt > 1) {
3968                 /* additional external read buffers */
3969                 for (i = 1; i < ext_buf_cnt; i++) {
3970                         ext_dmabuf = lpfc_bsg_dma_page_alloc(phba);
3971                         if (!ext_dmabuf) {
3972                                 rc = -ENOMEM;
3973                                 goto job_error;
3974                         }
3975                         list_add_tail(&ext_dmabuf->list,
3976                                       &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
3977                 }
3978         }
3979
3980         /* bsg tracking structure */
3981         dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
3982         if (!dd_data) {
3983                 rc = -ENOMEM;
3984                 goto job_error;
3985         }
3986
3987         /* mailbox command structure for base driver */
3988         pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3989         if (!pmboxq) {
3990                 rc = -ENOMEM;
3991                 goto job_error;
3992         }
3993         memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
3994
3995         /* for the first external buffer */
3996         lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, 0, dmabuf, dmabuf);
3997
3998         /* for the rest of external buffer descriptors if any */
3999         if (ext_buf_cnt > 1) {
4000                 ext_buf_index = 1;
4001                 list_for_each_entry_safe(curr_dmabuf, next_dmabuf,
4002                                 &phba->mbox_ext_buf_ctx.ext_dmabuf_list, list) {
4003                         lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp,
4004                                                 ext_buf_index, dmabuf,
4005                                                 curr_dmabuf);
4006                         ext_buf_index++;
4007                 }
4008         }
4009
4010         /* after dma descriptor setup */
4011         lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_rd, dma_mbox,
4012                                         sta_pos_addr, dmabuf, ext_buf_cnt);
4013
4014         /* construct base driver mbox command */
4015         pmb = &pmboxq->u.mb;
4016         pmbx = (uint8_t *)dmabuf->virt;
4017         memcpy(pmb, pmbx, sizeof(*pmb));
4018         pmb->mbxOwner = OWN_HOST;
4019         pmboxq->vport = phba->pport;
4020
4021         /* multi-buffer handling context */
4022         phba->mbox_ext_buf_ctx.nembType = nemb_tp;
4023         phba->mbox_ext_buf_ctx.mboxType = mbox_rd;
4024         phba->mbox_ext_buf_ctx.numBuf = ext_buf_cnt;
4025         phba->mbox_ext_buf_ctx.mbxTag = mbox_req->extMboxTag;
4026         phba->mbox_ext_buf_ctx.seqNum = mbox_req->extSeqNum;
4027         phba->mbox_ext_buf_ctx.mbx_dmabuf = dmabuf;
4028
4029         /* callback for multi-buffer read mailbox command */
4030         pmboxq->mbox_cmpl = lpfc_bsg_issue_read_mbox_ext_cmpl;
4031
4032         /* context fields to callback function */
4033         pmboxq->ctx_buf = dd_data;
4034         dd_data->type = TYPE_MBOX;
4035         dd_data->set_job = job;
4036         dd_data->context_un.mbox.pmboxq = pmboxq;
4037         dd_data->context_un.mbox.mb = (MAILBOX_t *)pmbx;
4038         job->dd_data = dd_data;
4039
4040         /* state change */
4041         phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
4042
4043         /*
4044          * Non-embedded mailbox subcommand data gets byte swapped here because
4045          * the lower level driver code only does the first 64 mailbox words.
4046          */
4047         if ((!bsg_bf_get(lpfc_mbox_hdr_emb,
4048             &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) &&
4049                 (nemb_tp == nemb_mse))
4050                 lpfc_sli_pcimem_bcopy(&pmbx[sizeof(MAILBOX_t)],
4051                         &pmbx[sizeof(MAILBOX_t)],
4052                                 sli_cfg_mbx->un.sli_config_emb0_subsys.
4053                                         mse[0].buf_len);
4054
4055         rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
4056         if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
4057                 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4058                                 "2947 Issued SLI_CONFIG ext-buffer "
4059                                 "mailbox command, rc:x%x\n", rc);
4060                 return SLI_CONFIG_HANDLED;
4061         }
4062         lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4063                         "2948 Failed to issue SLI_CONFIG ext-buffer "
4064                         "mailbox command, rc:x%x\n", rc);
4065         rc = -EPIPE;
4066
4067 job_error:
4068         if (pmboxq)
4069                 mempool_free(pmboxq, phba->mbox_mem_pool);
4070         lpfc_bsg_dma_page_list_free(phba,
4071                                     &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
4072         kfree(dd_data);
4073         phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_IDLE;
4074         return rc;
4075 }
4076
4077 /**
4078  * lpfc_bsg_sli_cfg_write_cmd_ext - sli_config non-embedded mailbox cmd write
4079  * @phba: Pointer to HBA context object.
4080  * @mb: Pointer to a BSG mailbox object.
4081  * @dmabuff: Pointer to a DMA buffer descriptor.
4082  *
4083  * This routine performs SLI_CONFIG (0x9B) write mailbox command operation with
4084  * non-embedded external bufffers.
4085  **/
4086 static int
4087 lpfc_bsg_sli_cfg_write_cmd_ext(struct lpfc_hba *phba, struct bsg_job *job,
4088                                enum nemb_type nemb_tp,
4089                                struct lpfc_dmabuf *dmabuf)
4090 {
4091         struct fc_bsg_request *bsg_request = job->request;
4092         struct fc_bsg_reply *bsg_reply = job->reply;
4093         struct dfc_mbox_req *mbox_req;
4094         struct lpfc_sli_config_mbox *sli_cfg_mbx;
4095         uint32_t ext_buf_cnt;
4096         struct bsg_job_data *dd_data = NULL;
4097         LPFC_MBOXQ_t *pmboxq = NULL;
4098         MAILBOX_t *pmb;
4099         uint8_t *mbx;
4100         int rc = SLI_CONFIG_NOT_HANDLED, i;
4101
4102         mbox_req =
4103            (struct dfc_mbox_req *)bsg_request->rqst_data.h_vendor.vendor_cmd;
4104
4105         /* pointer to the start of mailbox command */
4106         sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
4107
4108         if (nemb_tp == nemb_mse) {
4109                 ext_buf_cnt = bsg_bf_get(lpfc_mbox_hdr_mse_cnt,
4110                         &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr);
4111                 if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_MSE) {
4112                         lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4113                                         "2953 Failed SLI_CONFIG(mse) wr, "
4114                                         "ext_buf_cnt(%d) out of range(%d)\n",
4115                                         ext_buf_cnt,
4116                                         LPFC_MBX_SLI_CONFIG_MAX_MSE);
4117                         return -ERANGE;
4118                 }
4119                 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4120                                 "2949 Handled SLI_CONFIG(mse) wr, "
4121                                 "ext_buf_cnt:%d\n", ext_buf_cnt);
4122         } else {
4123                 /* sanity check on interface type for support */
4124                 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
4125                     LPFC_SLI_INTF_IF_TYPE_2)
4126                         return -ENODEV;
4127                 /* nemb_tp == nemb_hbd */
4128                 ext_buf_cnt = sli_cfg_mbx->un.sli_config_emb1_subsys.hbd_count;
4129                 if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_HBD) {
4130                         lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4131                                         "2954 Failed SLI_CONFIG(hbd) wr, "
4132                                         "ext_buf_cnt(%d) out of range(%d)\n",
4133                                         ext_buf_cnt,
4134                                         LPFC_MBX_SLI_CONFIG_MAX_HBD);
4135                         return -ERANGE;
4136                 }
4137                 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4138                                 "2950 Handled SLI_CONFIG(hbd) wr, "
4139                                 "ext_buf_cnt:%d\n", ext_buf_cnt);
4140         }
4141
4142         /* before dma buffer descriptor setup */
4143         lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_wr, dma_mbox,
4144                                         sta_pre_addr, dmabuf, ext_buf_cnt);
4145
4146         if (ext_buf_cnt == 0)
4147                 return -EPERM;
4148
4149         /* for the first external buffer */
4150         lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, 0, dmabuf, dmabuf);
4151
4152         /* after dma descriptor setup */
4153         lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_wr, dma_mbox,
4154                                         sta_pos_addr, dmabuf, ext_buf_cnt);
4155
4156         /* log for looking forward */
4157         for (i = 1; i < ext_buf_cnt; i++) {
4158                 if (nemb_tp == nemb_mse)
4159                         lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4160                                 "2951 SLI_CONFIG(mse), buf[%d]-length:%d\n",
4161                                 i, sli_cfg_mbx->un.sli_config_emb0_subsys.
4162                                 mse[i].buf_len);
4163                 else
4164                         lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4165                                 "2952 SLI_CONFIG(hbd), buf[%d]-length:%d\n",
4166                                 i, bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
4167                                 &sli_cfg_mbx->un.sli_config_emb1_subsys.
4168                                 hbd[i]));
4169         }
4170
4171         /* multi-buffer handling context */
4172         phba->mbox_ext_buf_ctx.nembType = nemb_tp;
4173         phba->mbox_ext_buf_ctx.mboxType = mbox_wr;
4174         phba->mbox_ext_buf_ctx.numBuf = ext_buf_cnt;
4175         phba->mbox_ext_buf_ctx.mbxTag = mbox_req->extMboxTag;
4176         phba->mbox_ext_buf_ctx.seqNum = mbox_req->extSeqNum;
4177         phba->mbox_ext_buf_ctx.mbx_dmabuf = dmabuf;
4178
4179         if (ext_buf_cnt == 1) {
4180                 /* bsg tracking structure */
4181                 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
4182                 if (!dd_data) {
4183                         rc = -ENOMEM;
4184                         goto job_error;
4185                 }
4186
4187                 /* mailbox command structure for base driver */
4188                 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4189                 if (!pmboxq) {
4190                         rc = -ENOMEM;
4191                         goto job_error;
4192                 }
4193                 memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
4194                 pmb = &pmboxq->u.mb;
4195                 mbx = (uint8_t *)dmabuf->virt;
4196                 memcpy(pmb, mbx, sizeof(*pmb));
4197                 pmb->mbxOwner = OWN_HOST;
4198                 pmboxq->vport = phba->pport;
4199
4200                 /* callback for multi-buffer read mailbox command */
4201                 pmboxq->mbox_cmpl = lpfc_bsg_issue_write_mbox_ext_cmpl;
4202
4203                 /* context fields to callback function */
4204                 pmboxq->ctx_buf = dd_data;
4205                 dd_data->type = TYPE_MBOX;
4206                 dd_data->set_job = job;
4207                 dd_data->context_un.mbox.pmboxq = pmboxq;
4208                 dd_data->context_un.mbox.mb = (MAILBOX_t *)mbx;
4209                 job->dd_data = dd_data;
4210
4211                 /* state change */
4212
4213                 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
4214                 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
4215                 if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
4216                         lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4217                                         "2955 Issued SLI_CONFIG ext-buffer "
4218                                         "mailbox command, rc:x%x\n", rc);
4219                         return SLI_CONFIG_HANDLED;
4220                 }
4221                 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4222                                 "2956 Failed to issue SLI_CONFIG ext-buffer "
4223                                 "mailbox command, rc:x%x\n", rc);
4224                 rc = -EPIPE;
4225                 goto job_error;
4226         }
4227
4228         /* wait for additoinal external buffers */
4229
4230         bsg_reply->result = 0;
4231         bsg_job_done(job, bsg_reply->result,
4232                        bsg_reply->reply_payload_rcv_len);
4233         return SLI_CONFIG_HANDLED;
4234
4235 job_error:
4236         if (pmboxq)
4237                 mempool_free(pmboxq, phba->mbox_mem_pool);
4238         kfree(dd_data);
4239
4240         return rc;
4241 }
4242
4243 /**
4244  * lpfc_bsg_handle_sli_cfg_mbox - handle sli-cfg mailbox cmd with ext buffer
4245  * @phba: Pointer to HBA context object.
4246  * @mb: Pointer to a BSG mailbox object.
4247  * @dmabuff: Pointer to a DMA buffer descriptor.
4248  *
4249  * This routine handles SLI_CONFIG (0x9B) mailbox command with non-embedded
4250  * external bufffers, including both 0x9B with non-embedded MSEs and 0x9B
4251  * with embedded sussystem 0x1 and opcodes with external HBDs.
4252  **/
4253 static int
4254 lpfc_bsg_handle_sli_cfg_mbox(struct lpfc_hba *phba, struct bsg_job *job,
4255                              struct lpfc_dmabuf *dmabuf)
4256 {
4257         struct lpfc_sli_config_mbox *sli_cfg_mbx;
4258         uint32_t subsys;
4259         uint32_t opcode;
4260         int rc = SLI_CONFIG_NOT_HANDLED;
4261
4262         /* state change on new multi-buffer pass-through mailbox command */
4263         phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_HOST;
4264
4265         sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
4266
4267         if (!bsg_bf_get(lpfc_mbox_hdr_emb,
4268             &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) {
4269                 subsys = bsg_bf_get(lpfc_emb0_subcmnd_subsys,
4270                                     &sli_cfg_mbx->un.sli_config_emb0_subsys);
4271                 opcode = bsg_bf_get(lpfc_emb0_subcmnd_opcode,
4272                                     &sli_cfg_mbx->un.sli_config_emb0_subsys);
4273                 if (subsys == SLI_CONFIG_SUBSYS_FCOE) {
4274                         switch (opcode) {
4275                         case FCOE_OPCODE_READ_FCF:
4276                         case FCOE_OPCODE_GET_DPORT_RESULTS:
4277                                 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4278                                                 "2957 Handled SLI_CONFIG "
4279                                                 "subsys_fcoe, opcode:x%x\n",
4280                                                 opcode);
4281                                 rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
4282                                                         nemb_mse, dmabuf);
4283                                 break;
4284                         case FCOE_OPCODE_ADD_FCF:
4285                         case FCOE_OPCODE_SET_DPORT_MODE:
4286                         case LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_STATE:
4287                                 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4288                                                 "2958 Handled SLI_CONFIG "
4289                                                 "subsys_fcoe, opcode:x%x\n",
4290                                                 opcode);
4291                                 rc = lpfc_bsg_sli_cfg_write_cmd_ext(phba, job,
4292                                                         nemb_mse, dmabuf);
4293                                 break;
4294                         default:
4295                                 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4296                                                 "2959 Reject SLI_CONFIG "
4297                                                 "subsys_fcoe, opcode:x%x\n",
4298                                                 opcode);
4299                                 rc = -EPERM;
4300                                 break;
4301                         }
4302                 } else if (subsys == SLI_CONFIG_SUBSYS_COMN) {
4303                         switch (opcode) {
4304                         case COMN_OPCODE_GET_CNTL_ADDL_ATTRIBUTES:
4305                         case COMN_OPCODE_GET_CNTL_ATTRIBUTES:
4306                         case COMN_OPCODE_GET_PROFILE_CONFIG:
4307                         case COMN_OPCODE_SET_FEATURES:
4308                                 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4309                                                 "3106 Handled SLI_CONFIG "
4310                                                 "subsys_comn, opcode:x%x\n",
4311                                                 opcode);
4312                                 rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
4313                                                         nemb_mse, dmabuf);
4314                                 break;
4315                         default:
4316                                 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4317                                                 "3107 Reject SLI_CONFIG "
4318                                                 "subsys_comn, opcode:x%x\n",
4319                                                 opcode);
4320                                 rc = -EPERM;
4321                                 break;
4322                         }
4323                 } else {
4324                         lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4325                                         "2977 Reject SLI_CONFIG "
4326                                         "subsys:x%d, opcode:x%x\n",
4327                                         subsys, opcode);
4328                         rc = -EPERM;
4329                 }
4330         } else {
4331                 subsys = bsg_bf_get(lpfc_emb1_subcmnd_subsys,
4332                                     &sli_cfg_mbx->un.sli_config_emb1_subsys);
4333                 opcode = bsg_bf_get(lpfc_emb1_subcmnd_opcode,
4334                                     &sli_cfg_mbx->un.sli_config_emb1_subsys);
4335                 if (subsys == SLI_CONFIG_SUBSYS_COMN) {
4336                         switch (opcode) {
4337                         case COMN_OPCODE_READ_OBJECT:
4338                         case COMN_OPCODE_READ_OBJECT_LIST:
4339                                 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4340                                                 "2960 Handled SLI_CONFIG "
4341                                                 "subsys_comn, opcode:x%x\n",
4342                                                 opcode);
4343                                 rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
4344                                                         nemb_hbd, dmabuf);
4345                                 break;
4346                         case COMN_OPCODE_WRITE_OBJECT:
4347                                 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4348                                                 "2961 Handled SLI_CONFIG "
4349                                                 "subsys_comn, opcode:x%x\n",
4350                                                 opcode);
4351                                 rc = lpfc_bsg_sli_cfg_write_cmd_ext(phba, job,
4352                                                         nemb_hbd, dmabuf);
4353                                 break;
4354                         default:
4355                                 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4356                                                 "2962 Not handled SLI_CONFIG "
4357                                                 "subsys_comn, opcode:x%x\n",
4358                                                 opcode);
4359                                 rc = SLI_CONFIG_NOT_HANDLED;
4360                                 break;
4361                         }
4362                 } else {
4363                         lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4364                                         "2978 Not handled SLI_CONFIG "
4365                                         "subsys:x%d, opcode:x%x\n",
4366                                         subsys, opcode);
4367                         rc = SLI_CONFIG_NOT_HANDLED;
4368                 }
4369         }
4370
4371         /* state reset on not handled new multi-buffer mailbox command */
4372         if (rc != SLI_CONFIG_HANDLED)
4373                 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_IDLE;
4374
4375         return rc;
4376 }
4377
4378 /**
4379  * lpfc_bsg_mbox_ext_abort_req - request to abort mbox command with ext buffers
4380  * @phba: Pointer to HBA context object.
4381  *
4382  * This routine is for requesting to abort a pass-through mailbox command with
4383  * multiple external buffers due to error condition.
4384  **/
4385 static void
4386 lpfc_bsg_mbox_ext_abort(struct lpfc_hba *phba)
4387 {
4388         if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_PORT)
4389                 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_ABTS;
4390         else
4391                 lpfc_bsg_mbox_ext_session_reset(phba);
4392         return;
4393 }
4394
4395 /**
4396  * lpfc_bsg_read_ebuf_get - get the next mailbox read external buffer
4397  * @phba: Pointer to HBA context object.
4398  * @dmabuf: Pointer to a DMA buffer descriptor.
4399  *
4400  * This routine extracts the next mailbox read external buffer back to
4401  * user space through BSG.
4402  **/
4403 static int
4404 lpfc_bsg_read_ebuf_get(struct lpfc_hba *phba, struct bsg_job *job)
4405 {
4406         struct fc_bsg_reply *bsg_reply = job->reply;
4407         struct lpfc_sli_config_mbox *sli_cfg_mbx;
4408         struct lpfc_dmabuf *dmabuf;
4409         uint8_t *pbuf;
4410         uint32_t size;
4411         uint32_t index;
4412
4413         index = phba->mbox_ext_buf_ctx.seqNum;
4414         phba->mbox_ext_buf_ctx.seqNum++;
4415
4416         sli_cfg_mbx = (struct lpfc_sli_config_mbox *)
4417                         phba->mbox_ext_buf_ctx.mbx_dmabuf->virt;
4418
4419         if (phba->mbox_ext_buf_ctx.nembType == nemb_mse) {
4420                 size = bsg_bf_get(lpfc_mbox_sli_config_mse_len,
4421                         &sli_cfg_mbx->un.sli_config_emb0_subsys.mse[index]);
4422                 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4423                                 "2963 SLI_CONFIG (mse) ext-buffer rd get "
4424                                 "buffer[%d], size:%d\n", index, size);
4425         } else {
4426                 size = bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
4427                         &sli_cfg_mbx->un.sli_config_emb1_subsys.hbd[index]);
4428                 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4429                                 "2964 SLI_CONFIG (hbd) ext-buffer rd get "
4430                                 "buffer[%d], size:%d\n", index, size);
4431         }
4432         if (list_empty(&phba->mbox_ext_buf_ctx.ext_dmabuf_list))
4433                 return -EPIPE;
4434         dmabuf = list_first_entry(&phba->mbox_ext_buf_ctx.ext_dmabuf_list,
4435                                   struct lpfc_dmabuf, list);
4436         list_del_init(&dmabuf->list);
4437
4438         /* after dma buffer descriptor setup */
4439         lpfc_idiag_mbxacc_dump_bsg_mbox(phba, phba->mbox_ext_buf_ctx.nembType,
4440                                         mbox_rd, dma_ebuf, sta_pos_addr,
4441                                         dmabuf, index);
4442
4443         pbuf = (uint8_t *)dmabuf->virt;
4444         bsg_reply->reply_payload_rcv_len =
4445                 sg_copy_from_buffer(job->reply_payload.sg_list,
4446                                     job->reply_payload.sg_cnt,
4447                                     pbuf, size);
4448
4449         lpfc_bsg_dma_page_free(phba, dmabuf);
4450
4451         if (phba->mbox_ext_buf_ctx.seqNum == phba->mbox_ext_buf_ctx.numBuf) {
4452                 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4453                                 "2965 SLI_CONFIG (hbd) ext-buffer rd mbox "
4454                                 "command session done\n");
4455                 lpfc_bsg_mbox_ext_session_reset(phba);
4456         }
4457
4458         bsg_reply->result = 0;
4459         bsg_job_done(job, bsg_reply->result,
4460                        bsg_reply->reply_payload_rcv_len);
4461
4462         return SLI_CONFIG_HANDLED;
4463 }
4464
4465 /**
4466  * lpfc_bsg_write_ebuf_set - set the next mailbox write external buffer
4467  * @phba: Pointer to HBA context object.
4468  * @dmabuf: Pointer to a DMA buffer descriptor.
4469  *
4470  * This routine sets up the next mailbox read external buffer obtained
4471  * from user space through BSG.
4472  **/
4473 static int
4474 lpfc_bsg_write_ebuf_set(struct lpfc_hba *phba, struct bsg_job *job,
4475                         struct lpfc_dmabuf *dmabuf)
4476 {
4477         struct fc_bsg_reply *bsg_reply = job->reply;
4478         struct bsg_job_data *dd_data = NULL;
4479         LPFC_MBOXQ_t *pmboxq = NULL;
4480         MAILBOX_t *pmb;
4481         enum nemb_type nemb_tp;
4482         uint8_t *pbuf;
4483         uint32_t size;
4484         uint32_t index;
4485         int rc;
4486
4487         index = phba->mbox_ext_buf_ctx.seqNum;
4488         phba->mbox_ext_buf_ctx.seqNum++;
4489         nemb_tp = phba->mbox_ext_buf_ctx.nembType;
4490
4491         pbuf = (uint8_t *)dmabuf->virt;
4492         size = job->request_payload.payload_len;
4493         sg_copy_to_buffer(job->request_payload.sg_list,
4494                           job->request_payload.sg_cnt,
4495                           pbuf, size);
4496
4497         if (phba->mbox_ext_buf_ctx.nembType == nemb_mse) {
4498                 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4499                                 "2966 SLI_CONFIG (mse) ext-buffer wr set "
4500                                 "buffer[%d], size:%d\n",
4501                                 phba->mbox_ext_buf_ctx.seqNum, size);
4502
4503         } else {
4504                 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4505                                 "2967 SLI_CONFIG (hbd) ext-buffer wr set "
4506                                 "buffer[%d], size:%d\n",
4507                                 phba->mbox_ext_buf_ctx.seqNum, size);
4508
4509         }
4510
4511         /* set up external buffer descriptor and add to external buffer list */
4512         lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, index,
4513                                         phba->mbox_ext_buf_ctx.mbx_dmabuf,
4514                                         dmabuf);
4515         list_add_tail(&dmabuf->list, &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
4516
4517         /* after write dma buffer */
4518         lpfc_idiag_mbxacc_dump_bsg_mbox(phba, phba->mbox_ext_buf_ctx.nembType,
4519                                         mbox_wr, dma_ebuf, sta_pos_addr,
4520                                         dmabuf, index);
4521
4522         if (phba->mbox_ext_buf_ctx.seqNum == phba->mbox_ext_buf_ctx.numBuf) {
4523                 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4524                                 "2968 SLI_CONFIG ext-buffer wr all %d "
4525                                 "ebuffers received\n",
4526                                 phba->mbox_ext_buf_ctx.numBuf);
4527
4528                 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
4529                 if (!dd_data) {
4530                         rc = -ENOMEM;
4531                         goto job_error;
4532                 }
4533
4534                 /* mailbox command structure for base driver */
4535                 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4536                 if (!pmboxq) {
4537                         rc = -ENOMEM;
4538                         goto job_error;
4539                 }
4540                 memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
4541                 pbuf = (uint8_t *)phba->mbox_ext_buf_ctx.mbx_dmabuf->virt;
4542                 pmb = &pmboxq->u.mb;
4543                 memcpy(pmb, pbuf, sizeof(*pmb));
4544                 pmb->mbxOwner = OWN_HOST;
4545                 pmboxq->vport = phba->pport;
4546
4547                 /* callback for multi-buffer write mailbox command */
4548                 pmboxq->mbox_cmpl = lpfc_bsg_issue_write_mbox_ext_cmpl;
4549
4550                 /* context fields to callback function */
4551                 pmboxq->ctx_buf = dd_data;
4552                 dd_data->type = TYPE_MBOX;
4553                 dd_data->set_job = job;
4554                 dd_data->context_un.mbox.pmboxq = pmboxq;
4555                 dd_data->context_un.mbox.mb = (MAILBOX_t *)pbuf;
4556                 job->dd_data = dd_data;
4557
4558                 /* state change */
4559                 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
4560
4561                 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
4562                 if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
4563                         lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4564                                         "2969 Issued SLI_CONFIG ext-buffer "
4565                                         "mailbox command, rc:x%x\n", rc);
4566                         return SLI_CONFIG_HANDLED;
4567                 }
4568                 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4569                                 "2970 Failed to issue SLI_CONFIG ext-buffer "
4570                                 "mailbox command, rc:x%x\n", rc);
4571                 rc = -EPIPE;
4572                 goto job_error;
4573         }
4574
4575         /* wait for additoinal external buffers */
4576         bsg_reply->result = 0;
4577         bsg_job_done(job, bsg_reply->result,
4578                        bsg_reply->reply_payload_rcv_len);
4579         return SLI_CONFIG_HANDLED;
4580
4581 job_error:
4582         if (pmboxq)
4583                 mempool_free(pmboxq, phba->mbox_mem_pool);
4584         lpfc_bsg_dma_page_free(phba, dmabuf);
4585         kfree(dd_data);
4586
4587         return rc;
4588 }
4589
4590 /**
4591  * lpfc_bsg_handle_sli_cfg_ebuf - handle ext buffer with sli-cfg mailbox cmd
4592  * @phba: Pointer to HBA context object.
4593  * @mb: Pointer to a BSG mailbox object.
4594  * @dmabuff: Pointer to a DMA buffer descriptor.
4595  *
4596  * This routine handles the external buffer with SLI_CONFIG (0x9B) mailbox
4597  * command with multiple non-embedded external buffers.
4598  **/
4599 static int
4600 lpfc_bsg_handle_sli_cfg_ebuf(struct lpfc_hba *phba, struct bsg_job *job,
4601                              struct lpfc_dmabuf *dmabuf)
4602 {
4603         int rc;
4604
4605         lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4606                         "2971 SLI_CONFIG buffer (type:x%x)\n",
4607                         phba->mbox_ext_buf_ctx.mboxType);
4608
4609         if (phba->mbox_ext_buf_ctx.mboxType == mbox_rd) {
4610                 if (phba->mbox_ext_buf_ctx.state != LPFC_BSG_MBOX_DONE) {
4611                         lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4612                                         "2972 SLI_CONFIG rd buffer state "
4613                                         "mismatch:x%x\n",
4614                                         phba->mbox_ext_buf_ctx.state);
4615                         lpfc_bsg_mbox_ext_abort(phba);
4616                         return -EPIPE;
4617                 }
4618                 rc = lpfc_bsg_read_ebuf_get(phba, job);
4619                 if (rc == SLI_CONFIG_HANDLED)
4620                         lpfc_bsg_dma_page_free(phba, dmabuf);
4621         } else { /* phba->mbox_ext_buf_ctx.mboxType == mbox_wr */
4622                 if (phba->mbox_ext_buf_ctx.state != LPFC_BSG_MBOX_HOST) {
4623                         lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4624                                         "2973 SLI_CONFIG wr buffer state "
4625                                         "mismatch:x%x\n",
4626                                         phba->mbox_ext_buf_ctx.state);
4627                         lpfc_bsg_mbox_ext_abort(phba);
4628                         return -EPIPE;
4629                 }
4630                 rc = lpfc_bsg_write_ebuf_set(phba, job, dmabuf);
4631         }
4632         return rc;
4633 }
4634
4635 /**
4636  * lpfc_bsg_handle_sli_cfg_ext - handle sli-cfg mailbox with external buffer
4637  * @phba: Pointer to HBA context object.
4638  * @mb: Pointer to a BSG mailbox object.
4639  * @dmabuff: Pointer to a DMA buffer descriptor.
4640  *
4641  * This routine checkes and handles non-embedded multi-buffer SLI_CONFIG
4642  * (0x9B) mailbox commands and external buffers.
4643  **/
4644 static int
4645 lpfc_bsg_handle_sli_cfg_ext(struct lpfc_hba *phba, struct bsg_job *job,
4646                             struct lpfc_dmabuf *dmabuf)
4647 {
4648         struct fc_bsg_request *bsg_request = job->request;
4649         struct dfc_mbox_req *mbox_req;
4650         int rc = SLI_CONFIG_NOT_HANDLED;
4651
4652         mbox_req =
4653            (struct dfc_mbox_req *)bsg_request->rqst_data.h_vendor.vendor_cmd;
4654
4655         /* mbox command with/without single external buffer */
4656         if (mbox_req->extMboxTag == 0 && mbox_req->extSeqNum == 0)
4657                 return rc;
4658
4659         /* mbox command and first external buffer */
4660         if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_IDLE) {
4661                 if (mbox_req->extSeqNum == 1) {
4662                         lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4663                                         "2974 SLI_CONFIG mailbox: tag:%d, "
4664                                         "seq:%d\n", mbox_req->extMboxTag,
4665                                         mbox_req->extSeqNum);
4666                         rc = lpfc_bsg_handle_sli_cfg_mbox(phba, job, dmabuf);
4667                         return rc;
4668                 } else
4669                         goto sli_cfg_ext_error;
4670         }
4671
4672         /*
4673          * handle additional external buffers
4674          */
4675
4676         /* check broken pipe conditions */
4677         if (mbox_req->extMboxTag != phba->mbox_ext_buf_ctx.mbxTag)
4678                 goto sli_cfg_ext_error;
4679         if (mbox_req->extSeqNum > phba->mbox_ext_buf_ctx.numBuf)
4680                 goto sli_cfg_ext_error;
4681         if (mbox_req->extSeqNum != phba->mbox_ext_buf_ctx.seqNum + 1)
4682                 goto sli_cfg_ext_error;
4683
4684         lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4685                         "2975 SLI_CONFIG mailbox external buffer: "
4686                         "extSta:x%x, tag:%d, seq:%d\n",
4687                         phba->mbox_ext_buf_ctx.state, mbox_req->extMboxTag,
4688                         mbox_req->extSeqNum);
4689         rc = lpfc_bsg_handle_sli_cfg_ebuf(phba, job, dmabuf);
4690         return rc;
4691
4692 sli_cfg_ext_error:
4693         /* all other cases, broken pipe */
4694         lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4695                         "2976 SLI_CONFIG mailbox broken pipe: "
4696                         "ctxSta:x%x, ctxNumBuf:%d "
4697                         "ctxTag:%d, ctxSeq:%d, tag:%d, seq:%d\n",
4698                         phba->mbox_ext_buf_ctx.state,
4699                         phba->mbox_ext_buf_ctx.numBuf,
4700                         phba->mbox_ext_buf_ctx.mbxTag,
4701                         phba->mbox_ext_buf_ctx.seqNum,
4702                         mbox_req->extMboxTag, mbox_req->extSeqNum);
4703
4704         lpfc_bsg_mbox_ext_session_reset(phba);
4705
4706         return -EPIPE;
4707 }
4708
4709 /**
4710  * lpfc_bsg_issue_mbox - issues a mailbox command on behalf of an app
4711  * @phba: Pointer to HBA context object.
4712  * @mb: Pointer to a mailbox object.
4713  * @vport: Pointer to a vport object.
4714  *
4715  * Allocate a tracking object, mailbox command memory, get a mailbox
4716  * from the mailbox pool, copy the caller mailbox command.
4717  *
4718  * If offline and the sli is active we need to poll for the command (port is
4719  * being reset) and com-plete the job, otherwise issue the mailbox command and
4720  * let our completion handler finish the command.
4721  **/
4722 static int
4723 lpfc_bsg_issue_mbox(struct lpfc_hba *phba, struct bsg_job *job,
4724         struct lpfc_vport *vport)
4725 {
4726         struct fc_bsg_request *bsg_request = job->request;
4727         struct fc_bsg_reply *bsg_reply = job->reply;
4728         LPFC_MBOXQ_t *pmboxq = NULL; /* internal mailbox queue */
4729         MAILBOX_t *pmb; /* shortcut to the pmboxq mailbox */
4730         /* a 4k buffer to hold the mb and extended data from/to the bsg */
4731         uint8_t *pmbx = NULL;
4732         struct bsg_job_data *dd_data = NULL; /* bsg data tracking structure */
4733         struct lpfc_dmabuf *dmabuf = NULL;
4734         struct dfc_mbox_req *mbox_req;
4735         struct READ_EVENT_LOG_VAR *rdEventLog;
4736         uint32_t transmit_length, receive_length, mode;
4737         struct lpfc_mbx_sli4_config *sli4_config;
4738         struct lpfc_mbx_nembed_cmd *nembed_sge;
4739         struct ulp_bde64 *bde;
4740         uint8_t *ext = NULL;
4741         int rc = 0;
4742         uint8_t *from;
4743         uint32_t size;
4744
4745         /* in case no data is transferred */
4746         bsg_reply->reply_payload_rcv_len = 0;
4747
4748         /* sanity check to protect driver */
4749         if (job->reply_payload.payload_len > BSG_MBOX_SIZE ||
4750             job->request_payload.payload_len > BSG_MBOX_SIZE) {
4751                 rc = -ERANGE;
4752                 goto job_done;
4753         }
4754
4755         /*
4756          * Don't allow mailbox commands to be sent when blocked or when in
4757          * the middle of discovery
4758          */
4759         if (phba->sli.sli_flag & LPFC_BLOCK_MGMT_IO) {
4760                 rc = -EAGAIN;
4761                 goto job_done;
4762         }
4763
4764         mbox_req =
4765             (struct dfc_mbox_req *)bsg_request->rqst_data.h_vendor.vendor_cmd;
4766
4767         /* check if requested extended data lengths are valid */
4768         if ((mbox_req->inExtWLen > BSG_MBOX_SIZE/sizeof(uint32_t)) ||
4769             (mbox_req->outExtWLen > BSG_MBOX_SIZE/sizeof(uint32_t))) {
4770                 rc = -ERANGE;
4771                 goto job_done;
4772         }
4773
4774         dmabuf = lpfc_bsg_dma_page_alloc(phba);
4775         if (!dmabuf || !dmabuf->virt) {
4776                 rc = -ENOMEM;
4777                 goto job_done;
4778         }
4779
4780         /* Get the mailbox command or external buffer from BSG */
4781         pmbx = (uint8_t *)dmabuf->virt;
4782         size = job->request_payload.payload_len;
4783         sg_copy_to_buffer(job->request_payload.sg_list,
4784                           job->request_payload.sg_cnt, pmbx, size);
4785
4786         /* Handle possible SLI_CONFIG with non-embedded payloads */
4787         if (phba->sli_rev == LPFC_SLI_REV4) {
4788                 rc = lpfc_bsg_handle_sli_cfg_ext(phba, job, dmabuf);
4789                 if (rc == SLI_CONFIG_HANDLED)
4790                         goto job_cont;
4791                 if (rc)
4792                         goto job_done;
4793                 /* SLI_CONFIG_NOT_HANDLED for other mailbox commands */
4794         }
4795
4796         rc = lpfc_bsg_check_cmd_access(phba, (MAILBOX_t *)pmbx, vport);
4797         if (rc != 0)
4798                 goto job_done; /* must be negative */
4799
4800         /* allocate our bsg tracking structure */
4801         dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
4802         if (!dd_data) {
4803                 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
4804                                 "2727 Failed allocation of dd_data\n");
4805                 rc = -ENOMEM;
4806                 goto job_done;
4807         }
4808
4809         pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4810         if (!pmboxq) {
4811                 rc = -ENOMEM;
4812                 goto job_done;
4813         }
4814         memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
4815
4816         pmb = &pmboxq->u.mb;
4817         memcpy(pmb, pmbx, sizeof(*pmb));
4818         pmb->mbxOwner = OWN_HOST;
4819         pmboxq->vport = vport;
4820
4821         /* If HBA encountered an error attention, allow only DUMP
4822          * or RESTART mailbox commands until the HBA is restarted.
4823          */
4824         if (phba->pport->stopped &&
4825             pmb->mbxCommand != MBX_DUMP_MEMORY &&
4826             pmb->mbxCommand != MBX_RESTART &&
4827             pmb->mbxCommand != MBX_WRITE_VPARMS &&
4828             pmb->mbxCommand != MBX_WRITE_WWN)
4829                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
4830                                 "2797 mbox: Issued mailbox cmd "
4831                                 "0x%x while in stopped state.\n",
4832                                 pmb->mbxCommand);
4833
4834         /* extended mailbox commands will need an extended buffer */
4835         if (mbox_req->inExtWLen || mbox_req->outExtWLen) {
4836                 from = pmbx;
4837                 ext = from + sizeof(MAILBOX_t);
4838                 pmboxq->ctx_buf = ext;
4839                 pmboxq->in_ext_byte_len =
4840                         mbox_req->inExtWLen * sizeof(uint32_t);
4841                 pmboxq->out_ext_byte_len =
4842                         mbox_req->outExtWLen * sizeof(uint32_t);
4843                 pmboxq->mbox_offset_word = mbox_req->mbOffset;
4844         }
4845
4846         /* biu diag will need a kernel buffer to transfer the data
4847          * allocate our own buffer and setup the mailbox command to
4848          * use ours
4849          */
4850         if (pmb->mbxCommand == MBX_RUN_BIU_DIAG64) {
4851                 transmit_length = pmb->un.varWords[1];
4852                 receive_length = pmb->un.varWords[4];
4853                 /* transmit length cannot be greater than receive length or
4854                  * mailbox extension size
4855                  */
4856                 if ((transmit_length > receive_length) ||
4857                         (transmit_length > BSG_MBOX_SIZE - sizeof(MAILBOX_t))) {
4858                         rc = -ERANGE;
4859                         goto job_done;
4860                 }
4861                 pmb->un.varBIUdiag.un.s2.xmit_bde64.addrHigh =
4862                         putPaddrHigh(dmabuf->phys + sizeof(MAILBOX_t));
4863                 pmb->un.varBIUdiag.un.s2.xmit_bde64.addrLow =
4864                         putPaddrLow(dmabuf->phys + sizeof(MAILBOX_t));
4865
4866                 pmb->un.varBIUdiag.un.s2.rcv_bde64.addrHigh =
4867                         putPaddrHigh(dmabuf->phys + sizeof(MAILBOX_t)
4868                           + pmb->un.varBIUdiag.un.s2.xmit_bde64.tus.f.bdeSize);
4869                 pmb->un.varBIUdiag.un.s2.rcv_bde64.addrLow =
4870                         putPaddrLow(dmabuf->phys + sizeof(MAILBOX_t)
4871                           + pmb->un.varBIUdiag.un.s2.xmit_bde64.tus.f.bdeSize);
4872         } else if (pmb->mbxCommand == MBX_READ_EVENT_LOG) {
4873                 rdEventLog = &pmb->un.varRdEventLog;
4874                 receive_length = rdEventLog->rcv_bde64.tus.f.bdeSize;
4875                 mode = bf_get(lpfc_event_log, rdEventLog);
4876
4877                 /* receive length cannot be greater than mailbox
4878                  * extension size
4879                  */
4880                 if (receive_length > BSG_MBOX_SIZE - sizeof(MAILBOX_t)) {
4881                         rc = -ERANGE;
4882                         goto job_done;
4883                 }
4884
4885                 /* mode zero uses a bde like biu diags command */
4886                 if (mode == 0) {
4887                         pmb->un.varWords[3] = putPaddrLow(dmabuf->phys
4888                                                         + sizeof(MAILBOX_t));
4889                         pmb->un.varWords[4] = putPaddrHigh(dmabuf->phys
4890                                                         + sizeof(MAILBOX_t));
4891                 }
4892         } else if (phba->sli_rev == LPFC_SLI_REV4) {
4893                 /* Let type 4 (well known data) through because the data is
4894                  * returned in varwords[4-8]
4895                  * otherwise check the recieve length and fetch the buffer addr
4896                  */
4897                 if ((pmb->mbxCommand == MBX_DUMP_MEMORY) &&
4898                         (pmb->un.varDmp.type != DMP_WELL_KNOWN)) {
4899                         /* rebuild the command for sli4 using our own buffers
4900                         * like we do for biu diags
4901                         */
4902                         receive_length = pmb->un.varWords[2];
4903                         /* receive length cannot be greater than mailbox
4904                          * extension size
4905                          */
4906                         if (receive_length == 0) {
4907                                 rc = -ERANGE;
4908                                 goto job_done;
4909                         }
4910                         pmb->un.varWords[3] = putPaddrLow(dmabuf->phys
4911                                                 + sizeof(MAILBOX_t));
4912                         pmb->un.varWords[4] = putPaddrHigh(dmabuf->phys
4913                                                 + sizeof(MAILBOX_t));
4914                 } else if ((pmb->mbxCommand == MBX_UPDATE_CFG) &&
4915                         pmb->un.varUpdateCfg.co) {
4916                         bde = (struct ulp_bde64 *)&pmb->un.varWords[4];
4917
4918                         /* bde size cannot be greater than mailbox ext size */
4919                         if (bde->tus.f.bdeSize >
4920                             BSG_MBOX_SIZE - sizeof(MAILBOX_t)) {
4921                                 rc = -ERANGE;
4922                                 goto job_done;
4923                         }
4924                         bde->addrHigh = putPaddrHigh(dmabuf->phys
4925                                                 + sizeof(MAILBOX_t));
4926                         bde->addrLow = putPaddrLow(dmabuf->phys
4927                                                 + sizeof(MAILBOX_t));
4928                 } else if (pmb->mbxCommand == MBX_SLI4_CONFIG) {
4929                         /* Handling non-embedded SLI_CONFIG mailbox command */
4930                         sli4_config = &pmboxq->u.mqe.un.sli4_config;
4931                         if (!bf_get(lpfc_mbox_hdr_emb,
4932                             &sli4_config->header.cfg_mhdr)) {
4933                                 /* rebuild the command for sli4 using our
4934                                  * own buffers like we do for biu diags
4935                                  */
4936                                 nembed_sge = (struct lpfc_mbx_nembed_cmd *)
4937                                                 &pmb->un.varWords[0];
4938                                 receive_length = nembed_sge->sge[0].length;
4939
4940                                 /* receive length cannot be greater than
4941                                  * mailbox extension size
4942                                  */
4943                                 if ((receive_length == 0) ||
4944                                     (receive_length >
4945                                      BSG_MBOX_SIZE - sizeof(MAILBOX_t))) {
4946                                         rc = -ERANGE;
4947                                         goto job_done;
4948                                 }
4949
4950                                 nembed_sge->sge[0].pa_hi =
4951                                                 putPaddrHigh(dmabuf->phys
4952                                                    + sizeof(MAILBOX_t));
4953                                 nembed_sge->sge[0].pa_lo =
4954                                                 putPaddrLow(dmabuf->phys
4955                                                    + sizeof(MAILBOX_t));
4956                         }
4957                 }
4958         }
4959
4960         dd_data->context_un.mbox.dmabuffers = dmabuf;
4961
4962         /* setup wake call as IOCB callback */
4963         pmboxq->mbox_cmpl = lpfc_bsg_issue_mbox_cmpl;
4964
4965         /* setup context field to pass wait_queue pointer to wake function */
4966         pmboxq->ctx_ndlp = dd_data;
4967         dd_data->type = TYPE_MBOX;
4968         dd_data->set_job = job;
4969         dd_data->context_un.mbox.pmboxq = pmboxq;
4970         dd_data->context_un.mbox.mb = (MAILBOX_t *)pmbx;
4971         dd_data->context_un.mbox.ext = ext;
4972         dd_data->context_un.mbox.mbOffset = mbox_req->mbOffset;
4973         dd_data->context_un.mbox.inExtWLen = mbox_req->inExtWLen;
4974         dd_data->context_un.mbox.outExtWLen = mbox_req->outExtWLen;
4975         job->dd_data = dd_data;
4976
4977         if ((vport->fc_flag & FC_OFFLINE_MODE) ||
4978             (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))) {
4979                 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
4980                 if (rc != MBX_SUCCESS) {
4981                         rc = (rc == MBX_TIMEOUT) ? -ETIME : -ENODEV;
4982                         goto job_done;
4983                 }
4984
4985                 /* job finished, copy the data */
4986                 memcpy(pmbx, pmb, sizeof(*pmb));
4987                 bsg_reply->reply_payload_rcv_len =
4988                         sg_copy_from_buffer(job->reply_payload.sg_list,
4989                                             job->reply_payload.sg_cnt,
4990                                             pmbx, size);
4991                 /* not waiting mbox already done */
4992                 rc = 0;
4993                 goto job_done;
4994         }
4995
4996         rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
4997         if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY))
4998                 return 1; /* job started */
4999
5000 job_done:
5001         /* common exit for error or job completed inline */
5002         if (pmboxq)
5003                 mempool_free(pmboxq, phba->mbox_mem_pool);
5004         lpfc_bsg_dma_page_free(phba, dmabuf);
5005         kfree(dd_data);
5006
5007 job_cont:
5008         return rc;
5009 }
5010
5011 /**
5012  * lpfc_bsg_mbox_cmd - process an fc bsg LPFC_BSG_VENDOR_MBOX command
5013  * @job: MBOX fc_bsg_job for LPFC_BSG_VENDOR_MBOX.
5014  **/
5015 static int
5016 lpfc_bsg_mbox_cmd(struct bsg_job *job)
5017 {
5018         struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
5019         struct fc_bsg_request *bsg_request = job->request;
5020         struct fc_bsg_reply *bsg_reply = job->reply;
5021         struct lpfc_hba *phba = vport->phba;
5022         struct dfc_mbox_req *mbox_req;
5023         int rc = 0;
5024
5025         /* mix-and-match backward compatibility */
5026         bsg_reply->reply_payload_rcv_len = 0;
5027         if (job->request_len <
5028             sizeof(struct fc_bsg_request) + sizeof(struct dfc_mbox_req)) {
5029                 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
5030                                 "2737 Mix-and-match backward compatibility "
5031                                 "between MBOX_REQ old size:%d and "
5032                                 "new request size:%d\n",
5033                                 (int)(job->request_len -
5034                                       sizeof(struct fc_bsg_request)),
5035                                 (int)sizeof(struct dfc_mbox_req));
5036                 mbox_req = (struct dfc_mbox_req *)
5037                                 bsg_request->rqst_data.h_vendor.vendor_cmd;
5038                 mbox_req->extMboxTag = 0;
5039                 mbox_req->extSeqNum = 0;
5040         }
5041
5042         rc = lpfc_bsg_issue_mbox(phba, job, vport);
5043
5044         if (rc == 0) {
5045                 /* job done */
5046                 bsg_reply->result = 0;
5047                 job->dd_data = NULL;
5048                 bsg_job_done(job, bsg_reply->result,
5049                                bsg_reply->reply_payload_rcv_len);
5050         } else if (rc == 1)
5051                 /* job submitted, will complete later*/
5052                 rc = 0; /* return zero, no error */
5053         else {
5054                 /* some error occurred */
5055                 bsg_reply->result = rc;
5056                 job->dd_data = NULL;
5057         }
5058
5059         return rc;
5060 }
5061
5062 /**
5063  * lpfc_bsg_menlo_cmd_cmp - lpfc_menlo_cmd completion handler
5064  * @phba: Pointer to HBA context object.
5065  * @cmdiocbq: Pointer to command iocb.
5066  * @rspiocbq: Pointer to response iocb.
5067  *
5068  * This function is the completion handler for iocbs issued using
5069  * lpfc_menlo_cmd function. This function is called by the
5070  * ring event handler function without any lock held. This function
5071  * can be called from both worker thread context and interrupt
5072  * context. This function also can be called from another thread which
5073  * cleans up the SLI layer objects.
5074  * This function copies the contents of the response iocb to the
5075  * response iocb memory object provided by the caller of
5076  * lpfc_sli_issue_iocb_wait and then wakes up the thread which
5077  * sleeps for the iocb completion.
5078  **/
5079 static void
5080 lpfc_bsg_menlo_cmd_cmp(struct lpfc_hba *phba,
5081                         struct lpfc_iocbq *cmdiocbq,
5082                         struct lpfc_iocbq *rspiocbq)
5083 {
5084         struct bsg_job_data *dd_data;
5085         struct bsg_job *job;
5086         struct fc_bsg_reply *bsg_reply;
5087         IOCB_t *rsp;
5088         struct lpfc_dmabuf *bmp, *cmp, *rmp;
5089         struct lpfc_bsg_menlo *menlo;
5090         unsigned long flags;
5091         struct menlo_response *menlo_resp;
5092         unsigned int rsp_size;
5093         int rc = 0;
5094
5095         dd_data = cmdiocbq->context1;
5096         cmp = cmdiocbq->context2;
5097         bmp = cmdiocbq->context3;
5098         menlo = &dd_data->context_un.menlo;
5099         rmp = menlo->rmp;
5100         rsp = &rspiocbq->iocb;
5101
5102         /* Determine if job has been aborted */
5103         spin_lock_irqsave(&phba->ct_ev_lock, flags);
5104         job = dd_data->set_job;
5105         if (job) {
5106                 bsg_reply = job->reply;
5107                 /* Prevent timeout handling from trying to abort job  */
5108                 job->dd_data = NULL;
5109         }
5110         spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5111
5112         /* Copy the job data or set the failing status for the job */
5113
5114         if (job) {
5115                 /* always return the xri, this would be used in the case
5116                  * of a menlo download to allow the data to be sent as a
5117                  * continuation of the exchange.
5118                  */
5119
5120                 menlo_resp = (struct menlo_response *)
5121                         bsg_reply->reply_data.vendor_reply.vendor_rsp;
5122                 menlo_resp->xri = rsp->ulpContext;
5123                 if (rsp->ulpStatus) {
5124                         if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
5125                                 switch (rsp->un.ulpWord[4] & IOERR_PARAM_MASK) {
5126                                 case IOERR_SEQUENCE_TIMEOUT:
5127                                         rc = -ETIMEDOUT;
5128                                         break;
5129                                 case IOERR_INVALID_RPI:
5130                                         rc = -EFAULT;
5131                                         break;
5132                                 default:
5133                                         rc = -EACCES;
5134                                         break;
5135                                 }
5136                         } else {
5137                                 rc = -EACCES;
5138                         }
5139                 } else {
5140                         rsp_size = rsp->un.genreq64.bdl.bdeSize;
5141                         bsg_reply->reply_payload_rcv_len =
5142                                 lpfc_bsg_copy_data(rmp, &job->reply_payload,
5143                                                    rsp_size, 0);
5144                 }
5145
5146         }
5147
5148         lpfc_sli_release_iocbq(phba, cmdiocbq);
5149         lpfc_free_bsg_buffers(phba, cmp);
5150         lpfc_free_bsg_buffers(phba, rmp);
5151         lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
5152         kfree(bmp);
5153         kfree(dd_data);
5154
5155         /* Complete the job if active */
5156
5157         if (job) {
5158                 bsg_reply->result = rc;
5159                 bsg_job_done(job, bsg_reply->result,
5160                                bsg_reply->reply_payload_rcv_len);
5161         }
5162
5163         return;
5164 }
5165
5166 /**
5167  * lpfc_menlo_cmd - send an ioctl for menlo hardware
5168  * @job: fc_bsg_job to handle
5169  *
5170  * This function issues a gen request 64 CR ioctl for all menlo cmd requests,
5171  * all the command completions will return the xri for the command.
5172  * For menlo data requests a gen request 64 CX is used to continue the exchange
5173  * supplied in the menlo request header xri field.
5174  **/
5175 static int
5176 lpfc_menlo_cmd(struct bsg_job *job)
5177 {
5178         struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
5179         struct fc_bsg_request *bsg_request = job->request;
5180         struct fc_bsg_reply *bsg_reply = job->reply;
5181         struct lpfc_hba *phba = vport->phba;
5182         struct lpfc_iocbq *cmdiocbq;
5183         IOCB_t *cmd;
5184         int rc = 0;
5185         struct menlo_command *menlo_cmd;
5186         struct lpfc_dmabuf *bmp = NULL, *cmp = NULL, *rmp = NULL;
5187         int request_nseg;
5188         int reply_nseg;
5189         struct bsg_job_data *dd_data;
5190         struct ulp_bde64 *bpl = NULL;
5191
5192         /* in case no data is returned return just the return code */
5193         bsg_reply->reply_payload_rcv_len = 0;
5194
5195         if (job->request_len <
5196             sizeof(struct fc_bsg_request) +
5197                 sizeof(struct menlo_command)) {
5198                 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5199                                 "2784 Received MENLO_CMD request below "
5200                                 "minimum size\n");
5201                 rc = -ERANGE;
5202                 goto no_dd_data;
5203         }
5204
5205         if (job->reply_len <
5206             sizeof(struct fc_bsg_request) + sizeof(struct menlo_response)) {
5207                 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5208                                 "2785 Received MENLO_CMD reply below "
5209                                 "minimum size\n");
5210                 rc = -ERANGE;
5211                 goto no_dd_data;
5212         }
5213
5214         if (!(phba->menlo_flag & HBA_MENLO_SUPPORT)) {
5215                 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5216                                 "2786 Adapter does not support menlo "
5217                                 "commands\n");
5218                 rc = -EPERM;
5219                 goto no_dd_data;
5220         }
5221
5222         menlo_cmd = (struct menlo_command *)
5223                 bsg_request->rqst_data.h_vendor.vendor_cmd;
5224
5225         /* allocate our bsg tracking structure */
5226         dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
5227         if (!dd_data) {
5228                 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5229                                 "2787 Failed allocation of dd_data\n");
5230                 rc = -ENOMEM;
5231                 goto no_dd_data;
5232         }
5233
5234         bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5235         if (!bmp) {
5236                 rc = -ENOMEM;
5237                 goto free_dd;
5238         }
5239
5240         bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
5241         if (!bmp->virt) {
5242                 rc = -ENOMEM;
5243                 goto free_bmp;
5244         }
5245
5246         INIT_LIST_HEAD(&bmp->list);
5247
5248         bpl = (struct ulp_bde64 *)bmp->virt;
5249         request_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64);
5250         cmp = lpfc_alloc_bsg_buffers(phba, job->request_payload.payload_len,
5251                                      1, bpl, &request_nseg);
5252         if (!cmp) {
5253                 rc = -ENOMEM;
5254                 goto free_bmp;
5255         }
5256         lpfc_bsg_copy_data(cmp, &job->request_payload,
5257                            job->request_payload.payload_len, 1);
5258
5259         bpl += request_nseg;
5260         reply_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64) - request_nseg;
5261         rmp = lpfc_alloc_bsg_buffers(phba, job->reply_payload.payload_len, 0,
5262                                      bpl, &reply_nseg);
5263         if (!rmp) {
5264                 rc = -ENOMEM;
5265                 goto free_cmp;
5266         }
5267
5268         cmdiocbq = lpfc_sli_get_iocbq(phba);
5269         if (!cmdiocbq) {
5270                 rc = -ENOMEM;
5271                 goto free_rmp;
5272         }
5273
5274         cmd = &cmdiocbq->iocb;
5275         cmd->un.genreq64.bdl.ulpIoTag32 = 0;
5276         cmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
5277         cmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
5278         cmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
5279         cmd->un.genreq64.bdl.bdeSize =
5280             (request_nseg + reply_nseg) * sizeof(struct ulp_bde64);
5281         cmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
5282         cmd->un.genreq64.w5.hcsw.Dfctl = 0;
5283         cmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CMD;
5284         cmd->un.genreq64.w5.hcsw.Type = MENLO_TRANSPORT_TYPE; /* 0xfe */
5285         cmd->ulpBdeCount = 1;
5286         cmd->ulpClass = CLASS3;
5287         cmd->ulpOwner = OWN_CHIP;
5288         cmd->ulpLe = 1; /* Limited Edition */
5289         cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
5290         cmdiocbq->vport = phba->pport;
5291         /* We want the firmware to timeout before we do */
5292         cmd->ulpTimeout = MENLO_TIMEOUT - 5;
5293         cmdiocbq->iocb_cmpl = lpfc_bsg_menlo_cmd_cmp;
5294         cmdiocbq->context1 = dd_data;
5295         cmdiocbq->context2 = cmp;
5296         cmdiocbq->context3 = bmp;
5297         if (menlo_cmd->cmd == LPFC_BSG_VENDOR_MENLO_CMD) {
5298                 cmd->ulpCommand = CMD_GEN_REQUEST64_CR;
5299                 cmd->ulpPU = MENLO_PU; /* 3 */
5300                 cmd->un.ulpWord[4] = MENLO_DID; /* 0x0000FC0E */
5301                 cmd->ulpContext = MENLO_CONTEXT; /* 0 */
5302         } else {
5303                 cmd->ulpCommand = CMD_GEN_REQUEST64_CX;
5304                 cmd->ulpPU = 1;
5305                 cmd->un.ulpWord[4] = 0;
5306                 cmd->ulpContext = menlo_cmd->xri;
5307         }
5308
5309         dd_data->type = TYPE_MENLO;
5310         dd_data->set_job = job;
5311         dd_data->context_un.menlo.cmdiocbq = cmdiocbq;
5312         dd_data->context_un.menlo.rmp = rmp;
5313         job->dd_data = dd_data;
5314
5315         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq,
5316                 MENLO_TIMEOUT - 5);
5317         if (rc == IOCB_SUCCESS)
5318                 return 0; /* done for now */
5319
5320         lpfc_sli_release_iocbq(phba, cmdiocbq);
5321
5322 free_rmp:
5323         lpfc_free_bsg_buffers(phba, rmp);
5324 free_cmp:
5325         lpfc_free_bsg_buffers(phba, cmp);
5326 free_bmp:
5327         if (bmp->virt)
5328                 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
5329         kfree(bmp);
5330 free_dd:
5331         kfree(dd_data);
5332 no_dd_data:
5333         /* make error code available to userspace */
5334         bsg_reply->result = rc;
5335         job->dd_data = NULL;
5336         return rc;
5337 }
5338
5339 static int
5340 lpfc_forced_link_speed(struct bsg_job *job)
5341 {
5342         struct Scsi_Host *shost = fc_bsg_to_shost(job);
5343         struct lpfc_vport *vport = shost_priv(shost);
5344         struct lpfc_hba *phba = vport->phba;
5345         struct fc_bsg_reply *bsg_reply = job->reply;
5346         struct forced_link_speed_support_reply *forced_reply;
5347         int rc = 0;
5348
5349         if (job->request_len <
5350             sizeof(struct fc_bsg_request) +
5351             sizeof(struct get_forced_link_speed_support)) {
5352                 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5353                                 "0048 Received FORCED_LINK_SPEED request "
5354                                 "below minimum size\n");
5355                 rc = -EINVAL;
5356                 goto job_error;
5357         }
5358
5359         forced_reply = (struct forced_link_speed_support_reply *)
5360                 bsg_reply->reply_data.vendor_reply.vendor_rsp;
5361
5362         if (job->reply_len <
5363             sizeof(struct fc_bsg_request) +
5364             sizeof(struct forced_link_speed_support_reply)) {
5365                 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5366                                 "0049 Received FORCED_LINK_SPEED reply below "
5367                                 "minimum size\n");
5368                 rc = -EINVAL;
5369                 goto job_error;
5370         }
5371
5372         forced_reply->supported = (phba->hba_flag & HBA_FORCED_LINK_SPEED)
5373                                    ? LPFC_FORCED_LINK_SPEED_SUPPORTED
5374                                    : LPFC_FORCED_LINK_SPEED_NOT_SUPPORTED;
5375 job_error:
5376         bsg_reply->result = rc;
5377         if (rc == 0)
5378                 bsg_job_done(job, bsg_reply->result,
5379                                bsg_reply->reply_payload_rcv_len);
5380         return rc;
5381 }
5382
5383 /**
5384  * lpfc_check_fwlog_support: Check FW log support on the adapter
5385  * @phba: Pointer to HBA context object.
5386  *
5387  * Check if FW Logging support by the adapter
5388  **/
5389 int
5390 lpfc_check_fwlog_support(struct lpfc_hba *phba)
5391 {
5392         struct lpfc_ras_fwlog *ras_fwlog = NULL;
5393
5394         ras_fwlog = &phba->ras_fwlog;
5395
5396         if (ras_fwlog->ras_hwsupport == false)
5397                 return -EACCES;
5398         else if (ras_fwlog->ras_enabled == false)
5399                 return -EPERM;
5400         else
5401                 return 0;
5402 }
5403
5404 /**
5405  * lpfc_bsg_get_ras_config: Get RAS configuration settings
5406  * @job: fc_bsg_job to handle
5407  *
5408  * Get RAS configuration values set.
5409  **/
5410 static int
5411 lpfc_bsg_get_ras_config(struct bsg_job *job)
5412 {
5413         struct Scsi_Host *shost = fc_bsg_to_shost(job);
5414         struct lpfc_vport *vport = shost_priv(shost);
5415         struct fc_bsg_reply *bsg_reply = job->reply;
5416         struct lpfc_hba *phba = vport->phba;
5417         struct lpfc_bsg_get_ras_config_reply *ras_reply;
5418         struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
5419         int rc = 0;
5420
5421         if (job->request_len <
5422             sizeof(struct fc_bsg_request) +
5423             sizeof(struct lpfc_bsg_ras_req)) {
5424                 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
5425                                 "6192 FW_LOG request received "
5426                                 "below minimum size\n");
5427                 rc = -EINVAL;
5428                 goto ras_job_error;
5429         }
5430
5431         /* Check FW log status */
5432         rc = lpfc_check_fwlog_support(phba);
5433         if (rc)
5434                 goto ras_job_error;
5435
5436         ras_reply = (struct lpfc_bsg_get_ras_config_reply *)
5437                 bsg_reply->reply_data.vendor_reply.vendor_rsp;
5438
5439         /* Current logging state */
5440         spin_lock_irq(&phba->hbalock);
5441         if (ras_fwlog->state == ACTIVE)
5442                 ras_reply->state = LPFC_RASLOG_STATE_RUNNING;
5443         else
5444                 ras_reply->state = LPFC_RASLOG_STATE_STOPPED;
5445         spin_unlock_irq(&phba->hbalock);
5446
5447         ras_reply->log_level = phba->ras_fwlog.fw_loglevel;
5448         ras_reply->log_buff_sz = phba->cfg_ras_fwlog_buffsize;
5449
5450 ras_job_error:
5451         /* make error code available to userspace */
5452         bsg_reply->result = rc;
5453
5454         /* complete the job back to userspace */
5455         if (!rc)
5456                 bsg_job_done(job, bsg_reply->result,
5457                              bsg_reply->reply_payload_rcv_len);
5458         return rc;
5459 }
5460
5461 /**
5462  * lpfc_bsg_set_ras_config: Set FW logging parameters
5463  * @job: fc_bsg_job to handle
5464  *
5465  * Set log-level parameters for FW-logging in host memory
5466  **/
5467 static int
5468 lpfc_bsg_set_ras_config(struct bsg_job *job)
5469 {
5470         struct Scsi_Host *shost = fc_bsg_to_shost(job);
5471         struct lpfc_vport *vport = shost_priv(shost);
5472         struct lpfc_hba *phba = vport->phba;
5473         struct lpfc_bsg_set_ras_config_req *ras_req;
5474         struct fc_bsg_request *bsg_request = job->request;
5475         struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
5476         struct fc_bsg_reply *bsg_reply = job->reply;
5477         uint8_t action = 0, log_level = 0;
5478         int rc = 0, action_status = 0;
5479
5480         if (job->request_len <
5481             sizeof(struct fc_bsg_request) +
5482             sizeof(struct lpfc_bsg_set_ras_config_req)) {
5483                 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
5484                                 "6182 Received RAS_LOG request "
5485                                 "below minimum size\n");
5486                 rc = -EINVAL;
5487                 goto ras_job_error;
5488         }
5489
5490         /* Check FW log status */
5491         rc = lpfc_check_fwlog_support(phba);
5492         if (rc)
5493                 goto ras_job_error;
5494
5495         ras_req = (struct lpfc_bsg_set_ras_config_req *)
5496                 bsg_request->rqst_data.h_vendor.vendor_cmd;
5497         action = ras_req->action;
5498         log_level = ras_req->log_level;
5499
5500         if (action == LPFC_RASACTION_STOP_LOGGING) {
5501                 /* Check if already disabled */
5502                 spin_lock_irq(&phba->hbalock);
5503                 if (ras_fwlog->state != ACTIVE) {
5504                         spin_unlock_irq(&phba->hbalock);
5505                         rc = -ESRCH;
5506                         goto ras_job_error;
5507                 }
5508                 spin_unlock_irq(&phba->hbalock);
5509
5510                 /* Disable logging */
5511                 lpfc_ras_stop_fwlog(phba);
5512         } else {
5513                 /*action = LPFC_RASACTION_START_LOGGING*/
5514
5515                 /* Even though FW-logging is active re-initialize
5516                  * FW-logging with new log-level. Return status
5517                  * "Logging already Running" to caller.
5518                  **/
5519                 spin_lock_irq(&phba->hbalock);
5520                 if (ras_fwlog->state != INACTIVE)
5521                         action_status = -EINPROGRESS;
5522                 spin_unlock_irq(&phba->hbalock);
5523
5524                 /* Enable logging */
5525                 rc = lpfc_sli4_ras_fwlog_init(phba, log_level,
5526                                               LPFC_RAS_ENABLE_LOGGING);
5527                 if (rc) {
5528                         rc = -EINVAL;
5529                         goto ras_job_error;
5530                 }
5531
5532                 /* Check if FW-logging is re-initialized */
5533                 if (action_status == -EINPROGRESS)
5534                         rc = action_status;
5535         }
5536 ras_job_error:
5537         /* make error code available to userspace */
5538         bsg_reply->result = rc;
5539
5540         /* complete the job back to userspace */
5541         if (!rc)
5542                 bsg_job_done(job, bsg_reply->result,
5543                              bsg_reply->reply_payload_rcv_len);
5544
5545         return rc;
5546 }
5547
5548 /**
5549  * lpfc_bsg_get_ras_lwpd: Get log write position data
5550  * @job: fc_bsg_job to handle
5551  *
5552  * Get Offset/Wrap count of the log message written
5553  * in host memory
5554  **/
5555 static int
5556 lpfc_bsg_get_ras_lwpd(struct bsg_job *job)
5557 {
5558         struct Scsi_Host *shost = fc_bsg_to_shost(job);
5559         struct lpfc_vport *vport = shost_priv(shost);
5560         struct lpfc_bsg_get_ras_lwpd *ras_reply;
5561         struct lpfc_hba *phba = vport->phba;
5562         struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
5563         struct fc_bsg_reply *bsg_reply = job->reply;
5564         u32 *lwpd_ptr = NULL;
5565         int rc = 0;
5566
5567         rc = lpfc_check_fwlog_support(phba);
5568         if (rc)
5569                 goto ras_job_error;
5570
5571         if (job->request_len <
5572             sizeof(struct fc_bsg_request) +
5573             sizeof(struct lpfc_bsg_ras_req)) {
5574                 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
5575                                 "6183 Received RAS_LOG request "
5576                                 "below minimum size\n");
5577                 rc = -EINVAL;
5578                 goto ras_job_error;
5579         }
5580
5581         ras_reply = (struct lpfc_bsg_get_ras_lwpd *)
5582                 bsg_reply->reply_data.vendor_reply.vendor_rsp;
5583
5584         if (!ras_fwlog->lwpd.virt) {
5585                 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
5586                                 "6193 Restart FW Logging\n");
5587                 rc = -EINVAL;
5588                 goto ras_job_error;
5589         }
5590
5591         /* Get lwpd offset */
5592         lwpd_ptr = (uint32_t *)(ras_fwlog->lwpd.virt);
5593         ras_reply->offset = be32_to_cpu(*lwpd_ptr & 0xffffffff);
5594
5595         /* Get wrap count */
5596         ras_reply->wrap_count = be32_to_cpu(*(++lwpd_ptr) & 0xffffffff);
5597
5598 ras_job_error:
5599         /* make error code available to userspace */
5600         bsg_reply->result = rc;
5601
5602         /* complete the job back to userspace */
5603         if (!rc)
5604                 bsg_job_done(job, bsg_reply->result,
5605                              bsg_reply->reply_payload_rcv_len);
5606
5607         return rc;
5608 }
5609
5610 /**
5611  * lpfc_bsg_get_ras_fwlog: Read FW log
5612  * @job: fc_bsg_job to handle
5613  *
5614  * Copy the FW log into the passed buffer.
5615  **/
5616 static int
5617 lpfc_bsg_get_ras_fwlog(struct bsg_job *job)
5618 {
5619         struct Scsi_Host *shost = fc_bsg_to_shost(job);
5620         struct lpfc_vport *vport = shost_priv(shost);
5621         struct lpfc_hba *phba = vport->phba;
5622         struct fc_bsg_request *bsg_request = job->request;
5623         struct fc_bsg_reply *bsg_reply = job->reply;
5624         struct lpfc_bsg_get_fwlog_req *ras_req;
5625         u32 rd_offset, rd_index, offset;
5626         void *src, *fwlog_buff;
5627         struct lpfc_ras_fwlog *ras_fwlog = NULL;
5628         struct lpfc_dmabuf *dmabuf, *next;
5629         int rc = 0;
5630
5631         ras_fwlog = &phba->ras_fwlog;
5632
5633         rc = lpfc_check_fwlog_support(phba);
5634         if (rc)
5635                 goto ras_job_error;
5636
5637         /* Logging to be stopped before reading */
5638         spin_lock_irq(&phba->hbalock);
5639         if (ras_fwlog->state == ACTIVE) {
5640                 spin_unlock_irq(&phba->hbalock);
5641                 rc = -EINPROGRESS;
5642                 goto ras_job_error;
5643         }
5644         spin_unlock_irq(&phba->hbalock);
5645
5646         if (job->request_len <
5647             sizeof(struct fc_bsg_request) +
5648             sizeof(struct lpfc_bsg_get_fwlog_req)) {
5649                 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
5650                                 "6184 Received RAS_LOG request "
5651                                 "below minimum size\n");
5652                 rc = -EINVAL;
5653                 goto ras_job_error;
5654         }
5655
5656         ras_req = (struct lpfc_bsg_get_fwlog_req *)
5657                 bsg_request->rqst_data.h_vendor.vendor_cmd;
5658         rd_offset = ras_req->read_offset;
5659
5660         /* Allocate memory to read fw log*/
5661         fwlog_buff = vmalloc(ras_req->read_size);
5662         if (!fwlog_buff) {
5663                 rc = -ENOMEM;
5664                 goto ras_job_error;
5665         }
5666
5667         rd_index = (rd_offset / LPFC_RAS_MAX_ENTRY_SIZE);
5668         offset = (rd_offset % LPFC_RAS_MAX_ENTRY_SIZE);
5669
5670         list_for_each_entry_safe(dmabuf, next,
5671                               &ras_fwlog->fwlog_buff_list, list) {
5672
5673                 if (dmabuf->buffer_tag < rd_index)
5674                         continue;
5675
5676                 src = dmabuf->virt + offset;
5677                 memcpy(fwlog_buff, src, ras_req->read_size);
5678                 break;
5679         }
5680
5681         bsg_reply->reply_payload_rcv_len =
5682                 sg_copy_from_buffer(job->reply_payload.sg_list,
5683                                     job->reply_payload.sg_cnt,
5684                                     fwlog_buff, ras_req->read_size);
5685
5686         vfree(fwlog_buff);
5687
5688 ras_job_error:
5689         bsg_reply->result = rc;
5690         if (!rc)
5691                 bsg_job_done(job, bsg_reply->result,
5692                              bsg_reply->reply_payload_rcv_len);
5693
5694         return rc;
5695 }
5696
5697 static int
5698 lpfc_get_trunk_info(struct bsg_job *job)
5699 {
5700         struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
5701         struct lpfc_hba *phba = vport->phba;
5702         struct fc_bsg_reply *bsg_reply = job->reply;
5703         struct lpfc_trunk_info *event_reply;
5704         int rc = 0;
5705
5706         if (job->request_len <
5707             sizeof(struct fc_bsg_request) + sizeof(struct get_trunk_info_req)) {
5708                 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
5709                                 "2744 Received GET TRUNK _INFO request below "
5710                                 "minimum size\n");
5711                 rc = -EINVAL;
5712                 goto job_error;
5713         }
5714
5715         event_reply = (struct lpfc_trunk_info *)
5716                 bsg_reply->reply_data.vendor_reply.vendor_rsp;
5717
5718         if (job->reply_len <
5719             sizeof(struct fc_bsg_request) + sizeof(struct lpfc_trunk_info)) {
5720                 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5721                                 "2728 Received GET TRUNK _INFO reply below "
5722                                 "minimum size\n");
5723                 rc = -EINVAL;
5724                 goto job_error;
5725         }
5726         if (event_reply == NULL) {
5727                 rc = -EINVAL;
5728                 goto job_error;
5729         }
5730
5731         bsg_bf_set(lpfc_trunk_info_link_status, event_reply,
5732                    (phba->link_state >= LPFC_LINK_UP) ? 1 : 0);
5733
5734         bsg_bf_set(lpfc_trunk_info_trunk_active0, event_reply,
5735                    (phba->trunk_link.link0.state == LPFC_LINK_UP) ? 1 : 0);
5736
5737         bsg_bf_set(lpfc_trunk_info_trunk_active1, event_reply,
5738                    (phba->trunk_link.link1.state == LPFC_LINK_UP) ? 1 : 0);
5739
5740         bsg_bf_set(lpfc_trunk_info_trunk_active2, event_reply,
5741                    (phba->trunk_link.link2.state == LPFC_LINK_UP) ? 1 : 0);
5742
5743         bsg_bf_set(lpfc_trunk_info_trunk_active3, event_reply,
5744                    (phba->trunk_link.link3.state == LPFC_LINK_UP) ? 1 : 0);
5745
5746         bsg_bf_set(lpfc_trunk_info_trunk_config0, event_reply,
5747                    bf_get(lpfc_conf_trunk_port0, &phba->sli4_hba));
5748
5749         bsg_bf_set(lpfc_trunk_info_trunk_config1, event_reply,
5750                    bf_get(lpfc_conf_trunk_port1, &phba->sli4_hba));
5751
5752         bsg_bf_set(lpfc_trunk_info_trunk_config2, event_reply,
5753                    bf_get(lpfc_conf_trunk_port2, &phba->sli4_hba));
5754
5755         bsg_bf_set(lpfc_trunk_info_trunk_config3, event_reply,
5756                    bf_get(lpfc_conf_trunk_port3, &phba->sli4_hba));
5757
5758         event_reply->port_speed = phba->sli4_hba.link_state.speed / 1000;
5759         event_reply->logical_speed =
5760                                 phba->sli4_hba.link_state.logical_speed / 1000;
5761 job_error:
5762         bsg_reply->result = rc;
5763         if (!rc)
5764                 bsg_job_done(job, bsg_reply->result,
5765                              bsg_reply->reply_payload_rcv_len);
5766         return rc;
5767
5768 }
5769
5770 /**
5771  * lpfc_bsg_hst_vendor - process a vendor-specific fc_bsg_job
5772  * @job: fc_bsg_job to handle
5773  **/
5774 static int
5775 lpfc_bsg_hst_vendor(struct bsg_job *job)
5776 {
5777         struct fc_bsg_request *bsg_request = job->request;
5778         struct fc_bsg_reply *bsg_reply = job->reply;
5779         int command = bsg_request->rqst_data.h_vendor.vendor_cmd[0];
5780         int rc;
5781
5782         switch (command) {
5783         case LPFC_BSG_VENDOR_SET_CT_EVENT:
5784                 rc = lpfc_bsg_hba_set_event(job);
5785                 break;
5786         case LPFC_BSG_VENDOR_GET_CT_EVENT:
5787                 rc = lpfc_bsg_hba_get_event(job);
5788                 break;
5789         case LPFC_BSG_VENDOR_SEND_MGMT_RESP:
5790                 rc = lpfc_bsg_send_mgmt_rsp(job);
5791                 break;
5792         case LPFC_BSG_VENDOR_DIAG_MODE:
5793                 rc = lpfc_bsg_diag_loopback_mode(job);
5794                 break;
5795         case LPFC_BSG_VENDOR_DIAG_MODE_END:
5796                 rc = lpfc_sli4_bsg_diag_mode_end(job);
5797                 break;
5798         case LPFC_BSG_VENDOR_DIAG_RUN_LOOPBACK:
5799                 rc = lpfc_bsg_diag_loopback_run(job);
5800                 break;
5801         case LPFC_BSG_VENDOR_LINK_DIAG_TEST:
5802                 rc = lpfc_sli4_bsg_link_diag_test(job);
5803                 break;
5804         case LPFC_BSG_VENDOR_GET_MGMT_REV:
5805                 rc = lpfc_bsg_get_dfc_rev(job);
5806                 break;
5807         case LPFC_BSG_VENDOR_MBOX:
5808                 rc = lpfc_bsg_mbox_cmd(job);
5809                 break;
5810         case LPFC_BSG_VENDOR_MENLO_CMD:
5811         case LPFC_BSG_VENDOR_MENLO_DATA:
5812                 rc = lpfc_menlo_cmd(job);
5813                 break;
5814         case LPFC_BSG_VENDOR_FORCED_LINK_SPEED:
5815                 rc = lpfc_forced_link_speed(job);
5816                 break;
5817         case LPFC_BSG_VENDOR_RAS_GET_LWPD:
5818                 rc = lpfc_bsg_get_ras_lwpd(job);
5819                 break;
5820         case LPFC_BSG_VENDOR_RAS_GET_FWLOG:
5821                 rc = lpfc_bsg_get_ras_fwlog(job);
5822                 break;
5823         case LPFC_BSG_VENDOR_RAS_GET_CONFIG:
5824                 rc = lpfc_bsg_get_ras_config(job);
5825                 break;
5826         case LPFC_BSG_VENDOR_RAS_SET_CONFIG:
5827                 rc = lpfc_bsg_set_ras_config(job);
5828                 break;
5829         case LPFC_BSG_VENDOR_GET_TRUNK_INFO:
5830                 rc = lpfc_get_trunk_info(job);
5831                 break;
5832         default:
5833                 rc = -EINVAL;
5834                 bsg_reply->reply_payload_rcv_len = 0;
5835                 /* make error code available to userspace */
5836                 bsg_reply->result = rc;
5837                 break;
5838         }
5839
5840         return rc;
5841 }
5842
5843 /**
5844  * lpfc_bsg_request - handle a bsg request from the FC transport
5845  * @job: bsg_job to handle
5846  **/
5847 int
5848 lpfc_bsg_request(struct bsg_job *job)
5849 {
5850         struct fc_bsg_request *bsg_request = job->request;
5851         struct fc_bsg_reply *bsg_reply = job->reply;
5852         uint32_t msgcode;
5853         int rc;
5854
5855         msgcode = bsg_request->msgcode;
5856         switch (msgcode) {
5857         case FC_BSG_HST_VENDOR:
5858                 rc = lpfc_bsg_hst_vendor(job);
5859                 break;
5860         case FC_BSG_RPT_ELS:
5861                 rc = lpfc_bsg_rport_els(job);
5862                 break;
5863         case FC_BSG_RPT_CT:
5864                 rc = lpfc_bsg_send_mgmt_cmd(job);
5865                 break;
5866         default:
5867                 rc = -EINVAL;
5868                 bsg_reply->reply_payload_rcv_len = 0;
5869                 /* make error code available to userspace */
5870                 bsg_reply->result = rc;
5871                 break;
5872         }
5873
5874         return rc;
5875 }
5876
5877 /**
5878  * lpfc_bsg_timeout - handle timeout of a bsg request from the FC transport
5879  * @job: bsg_job that has timed out
5880  *
5881  * This function just aborts the job's IOCB.  The aborted IOCB will return to
5882  * the waiting function which will handle passing the error back to userspace
5883  **/
5884 int
5885 lpfc_bsg_timeout(struct bsg_job *job)
5886 {
5887         struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
5888         struct lpfc_hba *phba = vport->phba;
5889         struct lpfc_iocbq *cmdiocb;
5890         struct lpfc_sli_ring *pring;
5891         struct bsg_job_data *dd_data;
5892         unsigned long flags;
5893         int rc = 0;
5894         LIST_HEAD(completions);
5895         struct lpfc_iocbq *check_iocb, *next_iocb;
5896
5897         pring = lpfc_phba_elsring(phba);
5898         if (unlikely(!pring))
5899                 return -EIO;
5900
5901         /* if job's driver data is NULL, the command completed or is in the
5902          * the process of completing.  In this case, return status to request
5903          * so the timeout is retried.  This avoids double completion issues
5904          * and the request will be pulled off the timer queue when the
5905          * command's completion handler executes.  Otherwise, prevent the
5906          * command's completion handler from executing the job done callback
5907          * and continue processing to abort the outstanding the command.
5908          */
5909
5910         spin_lock_irqsave(&phba->ct_ev_lock, flags);
5911         dd_data = (struct bsg_job_data *)job->dd_data;
5912         if (dd_data) {
5913                 dd_data->set_job = NULL;
5914                 job->dd_data = NULL;
5915         } else {
5916                 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5917                 return -EAGAIN;
5918         }
5919
5920         switch (dd_data->type) {
5921         case TYPE_IOCB:
5922                 /* Check to see if IOCB was issued to the port or not. If not,
5923                  * remove it from the txq queue and call cancel iocbs.
5924                  * Otherwise, call abort iotag
5925                  */
5926                 cmdiocb = dd_data->context_un.iocb.cmdiocbq;
5927                 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5928
5929                 spin_lock_irqsave(&phba->hbalock, flags);
5930                 /* make sure the I/O abort window is still open */
5931                 if (!(cmdiocb->iocb_flag & LPFC_IO_CMD_OUTSTANDING)) {
5932                         spin_unlock_irqrestore(&phba->hbalock, flags);
5933                         return -EAGAIN;
5934                 }
5935                 list_for_each_entry_safe(check_iocb, next_iocb, &pring->txq,
5936                                          list) {
5937                         if (check_iocb == cmdiocb) {
5938                                 list_move_tail(&check_iocb->list, &completions);
5939                                 break;
5940                         }
5941                 }
5942                 if (list_empty(&completions))
5943                         lpfc_sli_issue_abort_iotag(phba, pring, cmdiocb);
5944                 spin_unlock_irqrestore(&phba->hbalock, flags);
5945                 if (!list_empty(&completions)) {
5946                         lpfc_sli_cancel_iocbs(phba, &completions,
5947                                               IOSTAT_LOCAL_REJECT,
5948                                               IOERR_SLI_ABORTED);
5949                 }
5950                 break;
5951
5952         case TYPE_EVT:
5953                 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5954                 break;
5955
5956         case TYPE_MBOX:
5957                 /* Update the ext buf ctx state if needed */
5958
5959                 if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_PORT)
5960                         phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_ABTS;
5961                 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5962                 break;
5963         case TYPE_MENLO:
5964                 /* Check to see if IOCB was issued to the port or not. If not,
5965                  * remove it from the txq queue and call cancel iocbs.
5966                  * Otherwise, call abort iotag.
5967                  */
5968                 cmdiocb = dd_data->context_un.menlo.cmdiocbq;
5969                 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5970
5971                 spin_lock_irqsave(&phba->hbalock, flags);
5972                 list_for_each_entry_safe(check_iocb, next_iocb, &pring->txq,
5973                                          list) {
5974                         if (check_iocb == cmdiocb) {
5975                                 list_move_tail(&check_iocb->list, &completions);
5976                                 break;
5977                         }
5978                 }
5979                 if (list_empty(&completions))
5980                         lpfc_sli_issue_abort_iotag(phba, pring, cmdiocb);
5981                 spin_unlock_irqrestore(&phba->hbalock, flags);
5982                 if (!list_empty(&completions)) {
5983                         lpfc_sli_cancel_iocbs(phba, &completions,
5984                                               IOSTAT_LOCAL_REJECT,
5985                                               IOERR_SLI_ABORTED);
5986                 }
5987                 break;
5988         default:
5989                 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5990                 break;
5991         }
5992
5993         /* scsi transport fc fc_bsg_job_timeout expects a zero return code,
5994          * otherwise an error message will be displayed on the console
5995          * so always return success (zero)
5996          */
5997         return rc;
5998 }