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