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