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