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