[SCSI] lpfc 8.3.24: Add resource extent support
[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>
f1c3b0fc
JS
26
27#include <scsi/scsi.h>
28#include <scsi/scsi_host.h>
29#include <scsi/scsi_transport_fc.h>
30#include <scsi/scsi_bsg_fc.h>
6a9c52cf 31#include <scsi/fc/fc_fs.h>
f1c3b0fc
JS
32
33#include "lpfc_hw4.h"
34#include "lpfc_hw.h"
35#include "lpfc_sli.h"
36#include "lpfc_sli4.h"
37#include "lpfc_nl.h"
4fede78f 38#include "lpfc_bsg.h"
f1c3b0fc
JS
39#include "lpfc_disc.h"
40#include "lpfc_scsi.h"
41#include "lpfc.h"
42#include "lpfc_logmsg.h"
43#include "lpfc_crtn.h"
44#include "lpfc_vport.h"
45#include "lpfc_version.h"
46
4cc0e56e
JS
47struct lpfc_bsg_event {
48 struct list_head node;
49 struct kref kref;
50 wait_queue_head_t wq;
51
52 /* Event type and waiter identifiers */
53 uint32_t type_mask;
54 uint32_t req_id;
55 uint32_t reg_id;
56
57 /* next two flags are here for the auto-delete logic */
58 unsigned long wait_time_stamp;
59 int waiting;
60
61 /* seen and not seen events */
62 struct list_head events_to_get;
63 struct list_head events_to_see;
64
65 /* job waiting for this event to finish */
66 struct fc_bsg_job *set_job;
67};
68
69struct lpfc_bsg_iocb {
70 struct lpfc_iocbq *cmdiocbq;
71 struct lpfc_iocbq *rspiocbq;
72 struct lpfc_dmabuf *bmp;
73 struct lpfc_nodelist *ndlp;
74
75 /* job waiting for this iocb to finish */
76 struct fc_bsg_job *set_job;
77};
78
3b5dd52a
JS
79struct lpfc_bsg_mbox {
80 LPFC_MBOXQ_t *pmboxq;
81 MAILBOX_t *mb;
7a470277
JS
82 struct lpfc_dmabuf *rxbmp; /* for BIU diags */
83 struct lpfc_dmabufext *dmp; /* for BIU diags */
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/**
3b5dd52a
JS
1472 * lpfc_bsg_diag_mode - process a LPFC_BSG_VENDOR_DIAG_MODE bsg vendor command
1473 * @job: LPFC_BSG_VENDOR_DIAG_MODE
f1c3b0fc 1474 *
3b5dd52a
JS
1475 * This function is responsible for placing a port into diagnostic loopback
1476 * mode in order to perform a diagnostic loopback test.
1477 * All new scsi requests are blocked, a small delay is used to allow the
1478 * scsi requests to complete then the link is brought down. If the link is
1479 * is placed in loopback mode then scsi requests are again allowed
1480 * so the scsi mid-layer doesn't give up on the port.
1481 * All of this is done in-line.
f1c3b0fc 1482 */
3b5dd52a
JS
1483static int
1484lpfc_bsg_diag_mode(struct fc_bsg_job *job)
f1c3b0fc 1485{
3b5dd52a 1486 struct Scsi_Host *shost = job->shost;
f1c3b0fc
JS
1487 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
1488 struct lpfc_hba *phba = vport->phba;
3b5dd52a
JS
1489 struct diag_mode_set *loopback_mode;
1490 struct lpfc_sli *psli = &phba->sli;
1491 struct lpfc_sli_ring *pring = &psli->ring[LPFC_FCP_RING];
1492 uint32_t link_flags;
1493 uint32_t timeout;
1494 struct lpfc_vport **vports;
1495 LPFC_MBOXQ_t *pmboxq;
1496 int mbxstatus;
1497 int i = 0;
1498 int rc = 0;
f1c3b0fc 1499
3b5dd52a
JS
1500 /* no data to return just the return code */
1501 job->reply->reply_payload_rcv_len = 0;
1502
1503 if (job->request_len <
1504 sizeof(struct fc_bsg_request) + sizeof(struct diag_mode_set)) {
1505 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1506 "2738 Received DIAG MODE request below minimum "
1507 "size\n");
1508 rc = -EINVAL;
1509 goto job_error;
1510 }
1511
1512 loopback_mode = (struct diag_mode_set *)
1513 job->request->rqst_data.h_vendor.vendor_cmd;
1514 link_flags = loopback_mode->type;
515e0aa2 1515 timeout = loopback_mode->timeout * 100;
3b5dd52a
JS
1516
1517 if ((phba->link_state == LPFC_HBA_ERROR) ||
1518 (psli->sli_flag & LPFC_BLOCK_MGMT_IO) ||
1519 (!(psli->sli_flag & LPFC_SLI_ACTIVE))) {
1520 rc = -EACCES;
1521 goto job_error;
1522 }
1523
1524 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1525 if (!pmboxq) {
1526 rc = -ENOMEM;
1527 goto job_error;
1528 }
1529
1530 vports = lpfc_create_vport_work_array(phba);
1531 if (vports) {
1532 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
1533 shost = lpfc_shost_from_vport(vports[i]);
1534 scsi_block_requests(shost);
1535 }
1536
1537 lpfc_destroy_vport_work_array(phba, vports);
1538 } else {
1539 shost = lpfc_shost_from_vport(phba->pport);
1540 scsi_block_requests(shost);
1541 }
1542
1543 while (pring->txcmplq_cnt) {
1544 if (i++ > 500) /* wait up to 5 seconds */
1545 break;
1546
1547 msleep(10);
1548 }
1549
1550 memset((void *)pmboxq, 0, sizeof(LPFC_MBOXQ_t));
1551 pmboxq->u.mb.mbxCommand = MBX_DOWN_LINK;
1552 pmboxq->u.mb.mbxOwner = OWN_HOST;
1553
1554 mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
1555
1556 if ((mbxstatus == MBX_SUCCESS) && (pmboxq->u.mb.mbxStatus == 0)) {
1557 /* wait for link down before proceeding */
1558 i = 0;
1559 while (phba->link_state != LPFC_LINK_DOWN) {
1560 if (i++ > timeout) {
1561 rc = -ETIMEDOUT;
1562 goto loopback_mode_exit;
1563 }
1564
1565 msleep(10);
1566 }
1567
1568 memset((void *)pmboxq, 0, sizeof(LPFC_MBOXQ_t));
1569 if (link_flags == INTERNAL_LOOP_BACK)
1570 pmboxq->u.mb.un.varInitLnk.link_flags = FLAGS_LOCAL_LB;
1571 else
1572 pmboxq->u.mb.un.varInitLnk.link_flags =
1573 FLAGS_TOPOLOGY_MODE_LOOP;
1574
1575 pmboxq->u.mb.mbxCommand = MBX_INIT_LINK;
1576 pmboxq->u.mb.mbxOwner = OWN_HOST;
1577
1578 mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq,
1579 LPFC_MBOX_TMO);
1580
1581 if ((mbxstatus != MBX_SUCCESS) || (pmboxq->u.mb.mbxStatus))
1582 rc = -ENODEV;
1583 else {
1584 phba->link_flag |= LS_LOOPBACK_MODE;
1585 /* wait for the link attention interrupt */
1586 msleep(100);
1587
1588 i = 0;
1589 while (phba->link_state != LPFC_HBA_READY) {
1590 if (i++ > timeout) {
1591 rc = -ETIMEDOUT;
1592 break;
1593 }
1594
1595 msleep(10);
1596 }
1597 }
1598
1599 } else
1600 rc = -ENODEV;
1601
1602loopback_mode_exit:
1603 vports = lpfc_create_vport_work_array(phba);
1604 if (vports) {
1605 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
1606 shost = lpfc_shost_from_vport(vports[i]);
1607 scsi_unblock_requests(shost);
1608 }
1609 lpfc_destroy_vport_work_array(phba, vports);
1610 } else {
1611 shost = lpfc_shost_from_vport(phba->pport);
1612 scsi_unblock_requests(shost);
1613 }
1614
1615 /*
1616 * Let SLI layer release mboxq if mbox command completed after timeout.
1617 */
1618 if (mbxstatus != MBX_TIMEOUT)
1619 mempool_free(pmboxq, phba->mbox_mem_pool);
1620
1621job_error:
1622 /* make error code available to userspace */
1623 job->reply->result = rc;
1624 /* complete the job back to userspace if no error */
1625 if (rc == 0)
1626 job->job_done(job);
1627 return rc;
1628}
1629
1630/**
1631 * lpfcdiag_loop_self_reg - obtains a remote port login id
1632 * @phba: Pointer to HBA context object
1633 * @rpi: Pointer to a remote port login id
1634 *
1635 * This function obtains a remote port login id so the diag loopback test
1636 * can send and receive its own unsolicited CT command.
1637 **/
4042629e 1638static int lpfcdiag_loop_self_reg(struct lpfc_hba *phba, uint16_t *rpi)
3b5dd52a
JS
1639{
1640 LPFC_MBOXQ_t *mbox;
1641 struct lpfc_dmabuf *dmabuff;
1642 int status;
1643
1644 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1645 if (!mbox)
d439d286 1646 return -ENOMEM;
3b5dd52a 1647
4042629e
JS
1648 if (phba->sli_rev == LPFC_SLI_REV4)
1649 *rpi = lpfc_sli4_alloc_rpi(phba);
3b5dd52a 1650 status = lpfc_reg_rpi(phba, 0, phba->pport->fc_myDID,
4042629e 1651 (uint8_t *)&phba->pport->fc_sparam, mbox, *rpi);
3b5dd52a
JS
1652 if (status) {
1653 mempool_free(mbox, phba->mbox_mem_pool);
4042629e
JS
1654 if (phba->sli_rev == LPFC_SLI_REV4)
1655 lpfc_sli4_free_rpi(phba, *rpi);
d439d286 1656 return -ENOMEM;
3b5dd52a
JS
1657 }
1658
1659 dmabuff = (struct lpfc_dmabuf *) mbox->context1;
1660 mbox->context1 = NULL;
d439d286 1661 mbox->context2 = NULL;
3b5dd52a
JS
1662 status = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
1663
1664 if ((status != MBX_SUCCESS) || (mbox->u.mb.mbxStatus)) {
1665 lpfc_mbuf_free(phba, dmabuff->virt, dmabuff->phys);
1666 kfree(dmabuff);
1667 if (status != MBX_TIMEOUT)
1668 mempool_free(mbox, phba->mbox_mem_pool);
4042629e
JS
1669 if (phba->sli_rev == LPFC_SLI_REV4)
1670 lpfc_sli4_free_rpi(phba, *rpi);
d439d286 1671 return -ENODEV;
3b5dd52a
JS
1672 }
1673
1674 *rpi = mbox->u.mb.un.varWords[0];
1675
1676 lpfc_mbuf_free(phba, dmabuff->virt, dmabuff->phys);
1677 kfree(dmabuff);
1678 mempool_free(mbox, phba->mbox_mem_pool);
1679 return 0;
1680}
1681
1682/**
1683 * lpfcdiag_loop_self_unreg - unregs from the rpi
1684 * @phba: Pointer to HBA context object
1685 * @rpi: Remote port login id
1686 *
1687 * This function unregisters the rpi obtained in lpfcdiag_loop_self_reg
1688 **/
1689static int lpfcdiag_loop_self_unreg(struct lpfc_hba *phba, uint16_t rpi)
1690{
1691 LPFC_MBOXQ_t *mbox;
1692 int status;
1693
1694 /* Allocate mboxq structure */
1695 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1696 if (mbox == NULL)
d439d286 1697 return -ENOMEM;
3b5dd52a
JS
1698
1699 lpfc_unreg_login(phba, 0, rpi, mbox);
1700 status = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
1701
1702 if ((status != MBX_SUCCESS) || (mbox->u.mb.mbxStatus)) {
1703 if (status != MBX_TIMEOUT)
1704 mempool_free(mbox, phba->mbox_mem_pool);
d439d286 1705 return -EIO;
3b5dd52a 1706 }
3b5dd52a 1707 mempool_free(mbox, phba->mbox_mem_pool);
4042629e
JS
1708 if (phba->sli_rev == LPFC_SLI_REV4)
1709 lpfc_sli4_free_rpi(phba, rpi);
3b5dd52a
JS
1710 return 0;
1711}
1712
1713/**
1714 * lpfcdiag_loop_get_xri - obtains the transmit and receive ids
1715 * @phba: Pointer to HBA context object
1716 * @rpi: Remote port login id
1717 * @txxri: Pointer to transmit exchange id
1718 * @rxxri: Pointer to response exchabge id
1719 *
1720 * This function obtains the transmit and receive ids required to send
1721 * an unsolicited ct command with a payload. A special lpfc FsType and CmdRsp
1722 * flags are used to the unsolicted response handler is able to process
1723 * the ct command sent on the same port.
1724 **/
1725static int lpfcdiag_loop_get_xri(struct lpfc_hba *phba, uint16_t rpi,
1726 uint16_t *txxri, uint16_t * rxxri)
1727{
1728 struct lpfc_bsg_event *evt;
1729 struct lpfc_iocbq *cmdiocbq, *rspiocbq;
1730 IOCB_t *cmd, *rsp;
1731 struct lpfc_dmabuf *dmabuf;
1732 struct ulp_bde64 *bpl = NULL;
1733 struct lpfc_sli_ct_request *ctreq = NULL;
1734 int ret_val = 0;
d439d286 1735 int time_left;
515e0aa2 1736 int iocb_stat = 0;
3b5dd52a
JS
1737 unsigned long flags;
1738
1739 *txxri = 0;
1740 *rxxri = 0;
1741 evt = lpfc_bsg_event_new(FC_REG_CT_EVENT, current->pid,
1742 SLI_CT_ELX_LOOPBACK);
1743 if (!evt)
d439d286 1744 return -ENOMEM;
3b5dd52a
JS
1745
1746 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1747 list_add(&evt->node, &phba->ct_ev_waiters);
1748 lpfc_bsg_event_ref(evt);
1749 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1750
1751 cmdiocbq = lpfc_sli_get_iocbq(phba);
1752 rspiocbq = lpfc_sli_get_iocbq(phba);
1753
1754 dmabuf = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
1755 if (dmabuf) {
1756 dmabuf->virt = lpfc_mbuf_alloc(phba, 0, &dmabuf->phys);
c7495937
JS
1757 if (dmabuf->virt) {
1758 INIT_LIST_HEAD(&dmabuf->list);
1759 bpl = (struct ulp_bde64 *) dmabuf->virt;
1760 memset(bpl, 0, sizeof(*bpl));
1761 ctreq = (struct lpfc_sli_ct_request *)(bpl + 1);
1762 bpl->addrHigh =
1763 le32_to_cpu(putPaddrHigh(dmabuf->phys +
1764 sizeof(*bpl)));
1765 bpl->addrLow =
1766 le32_to_cpu(putPaddrLow(dmabuf->phys +
1767 sizeof(*bpl)));
1768 bpl->tus.f.bdeFlags = 0;
1769 bpl->tus.f.bdeSize = ELX_LOOPBACK_HEADER_SZ;
1770 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1771 }
3b5dd52a
JS
1772 }
1773
1774 if (cmdiocbq == NULL || rspiocbq == NULL ||
c7495937
JS
1775 dmabuf == NULL || bpl == NULL || ctreq == NULL ||
1776 dmabuf->virt == NULL) {
d439d286 1777 ret_val = -ENOMEM;
3b5dd52a
JS
1778 goto err_get_xri_exit;
1779 }
1780
1781 cmd = &cmdiocbq->iocb;
1782 rsp = &rspiocbq->iocb;
1783
1784 memset(ctreq, 0, ELX_LOOPBACK_HEADER_SZ);
1785
1786 ctreq->RevisionId.bits.Revision = SLI_CT_REVISION;
1787 ctreq->RevisionId.bits.InId = 0;
1788 ctreq->FsType = SLI_CT_ELX_LOOPBACK;
1789 ctreq->FsSubType = 0;
1790 ctreq->CommandResponse.bits.CmdRsp = ELX_LOOPBACK_XRI_SETUP;
1791 ctreq->CommandResponse.bits.Size = 0;
1792
1793
1794 cmd->un.xseq64.bdl.addrHigh = putPaddrHigh(dmabuf->phys);
1795 cmd->un.xseq64.bdl.addrLow = putPaddrLow(dmabuf->phys);
1796 cmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
1797 cmd->un.xseq64.bdl.bdeSize = sizeof(*bpl);
1798
1799 cmd->un.xseq64.w5.hcsw.Fctl = LA;
1800 cmd->un.xseq64.w5.hcsw.Dfctl = 0;
1801 cmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
1802 cmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
1803
1804 cmd->ulpCommand = CMD_XMIT_SEQUENCE64_CR;
1805 cmd->ulpBdeCount = 1;
1806 cmd->ulpLe = 1;
1807 cmd->ulpClass = CLASS3;
1808 cmd->ulpContext = rpi;
1809
1810 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
1811 cmdiocbq->vport = phba->pport;
1812
d439d286 1813 iocb_stat = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq,
3b5dd52a
JS
1814 rspiocbq,
1815 (phba->fc_ratov * 2)
1816 + LPFC_DRVR_TIMEOUT);
d439d286
JS
1817 if (iocb_stat) {
1818 ret_val = -EIO;
3b5dd52a 1819 goto err_get_xri_exit;
d439d286 1820 }
3b5dd52a
JS
1821 *txxri = rsp->ulpContext;
1822
1823 evt->waiting = 1;
1824 evt->wait_time_stamp = jiffies;
d439d286 1825 time_left = wait_event_interruptible_timeout(
3b5dd52a
JS
1826 evt->wq, !list_empty(&evt->events_to_see),
1827 ((phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT) * HZ);
1828 if (list_empty(&evt->events_to_see))
d439d286 1829 ret_val = (time_left) ? -EINTR : -ETIMEDOUT;
3b5dd52a 1830 else {
3b5dd52a
JS
1831 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1832 list_move(evt->events_to_see.prev, &evt->events_to_get);
1833 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1834 *rxxri = (list_entry(evt->events_to_get.prev,
1835 typeof(struct event_data),
1836 node))->immed_dat;
1837 }
1838 evt->waiting = 0;
1839
1840err_get_xri_exit:
1841 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1842 lpfc_bsg_event_unref(evt); /* release ref */
1843 lpfc_bsg_event_unref(evt); /* delete */
1844 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1845
1846 if (dmabuf) {
1847 if (dmabuf->virt)
1848 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
1849 kfree(dmabuf);
1850 }
1851
d439d286 1852 if (cmdiocbq && (iocb_stat != IOCB_TIMEDOUT))
3b5dd52a
JS
1853 lpfc_sli_release_iocbq(phba, cmdiocbq);
1854 if (rspiocbq)
1855 lpfc_sli_release_iocbq(phba, rspiocbq);
1856 return ret_val;
1857}
1858
1859/**
1860 * diag_cmd_data_alloc - fills in a bde struct with dma buffers
1861 * @phba: Pointer to HBA context object
1862 * @bpl: Pointer to 64 bit bde structure
1863 * @size: Number of bytes to process
1864 * @nocopydata: Flag to copy user data into the allocated buffer
1865 *
1866 * This function allocates page size buffers and populates an lpfc_dmabufext.
1867 * If allowed the user data pointed to with indataptr is copied into the kernel
1868 * memory. The chained list of page size buffers is returned.
1869 **/
1870static struct lpfc_dmabufext *
1871diag_cmd_data_alloc(struct lpfc_hba *phba,
1872 struct ulp_bde64 *bpl, uint32_t size,
1873 int nocopydata)
1874{
1875 struct lpfc_dmabufext *mlist = NULL;
1876 struct lpfc_dmabufext *dmp;
1877 int cnt, offset = 0, i = 0;
1878 struct pci_dev *pcidev;
1879
1880 pcidev = phba->pcidev;
1881
1882 while (size) {
1883 /* We get chunks of 4K */
1884 if (size > BUF_SZ_4K)
1885 cnt = BUF_SZ_4K;
1886 else
1887 cnt = size;
1888
1889 /* allocate struct lpfc_dmabufext buffer header */
1890 dmp = kmalloc(sizeof(struct lpfc_dmabufext), GFP_KERNEL);
1891 if (!dmp)
1892 goto out;
1893
1894 INIT_LIST_HEAD(&dmp->dma.list);
1895
1896 /* Queue it to a linked list */
1897 if (mlist)
1898 list_add_tail(&dmp->dma.list, &mlist->dma.list);
1899 else
1900 mlist = dmp;
1901
1902 /* allocate buffer */
1903 dmp->dma.virt = dma_alloc_coherent(&pcidev->dev,
1904 cnt,
1905 &(dmp->dma.phys),
1906 GFP_KERNEL);
1907
1908 if (!dmp->dma.virt)
1909 goto out;
1910
1911 dmp->size = cnt;
1912
1913 if (nocopydata) {
1914 bpl->tus.f.bdeFlags = 0;
1915 pci_dma_sync_single_for_device(phba->pcidev,
1916 dmp->dma.phys, LPFC_BPL_SIZE, PCI_DMA_TODEVICE);
1917
1918 } else {
1919 memset((uint8_t *)dmp->dma.virt, 0, cnt);
1920 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1921 }
1922
1923 /* build buffer ptr list for IOCB */
1924 bpl->addrLow = le32_to_cpu(putPaddrLow(dmp->dma.phys));
1925 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dmp->dma.phys));
1926 bpl->tus.f.bdeSize = (ushort) cnt;
1927 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1928 bpl++;
1929
1930 i++;
1931 offset += cnt;
1932 size -= cnt;
1933 }
1934
1935 mlist->flag = i;
1936 return mlist;
1937out:
1938 diag_cmd_data_free(phba, mlist);
1939 return NULL;
1940}
1941
1942/**
1943 * lpfcdiag_loop_post_rxbufs - post the receive buffers for an unsol CT cmd
1944 * @phba: Pointer to HBA context object
1945 * @rxxri: Receive exchange id
1946 * @len: Number of data bytes
1947 *
25985edc 1948 * This function allocates and posts a data buffer of sufficient size to receive
3b5dd52a
JS
1949 * an unsolicted CT command.
1950 **/
1951static int lpfcdiag_loop_post_rxbufs(struct lpfc_hba *phba, uint16_t rxxri,
1952 size_t len)
1953{
1954 struct lpfc_sli *psli = &phba->sli;
1955 struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
1956 struct lpfc_iocbq *cmdiocbq;
1957 IOCB_t *cmd = NULL;
1958 struct list_head head, *curr, *next;
1959 struct lpfc_dmabuf *rxbmp;
1960 struct lpfc_dmabuf *dmp;
1961 struct lpfc_dmabuf *mp[2] = {NULL, NULL};
1962 struct ulp_bde64 *rxbpl = NULL;
1963 uint32_t num_bde;
1964 struct lpfc_dmabufext *rxbuffer = NULL;
1965 int ret_val = 0;
d439d286 1966 int iocb_stat;
3b5dd52a
JS
1967 int i = 0;
1968
1969 cmdiocbq = lpfc_sli_get_iocbq(phba);
1970 rxbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
1971 if (rxbmp != NULL) {
1972 rxbmp->virt = lpfc_mbuf_alloc(phba, 0, &rxbmp->phys);
c7495937
JS
1973 if (rxbmp->virt) {
1974 INIT_LIST_HEAD(&rxbmp->list);
1975 rxbpl = (struct ulp_bde64 *) rxbmp->virt;
1976 rxbuffer = diag_cmd_data_alloc(phba, rxbpl, len, 0);
1977 }
3b5dd52a
JS
1978 }
1979
1980 if (!cmdiocbq || !rxbmp || !rxbpl || !rxbuffer) {
d439d286 1981 ret_val = -ENOMEM;
3b5dd52a
JS
1982 goto err_post_rxbufs_exit;
1983 }
1984
1985 /* Queue buffers for the receive exchange */
1986 num_bde = (uint32_t)rxbuffer->flag;
1987 dmp = &rxbuffer->dma;
1988
1989 cmd = &cmdiocbq->iocb;
1990 i = 0;
1991
1992 INIT_LIST_HEAD(&head);
1993 list_add_tail(&head, &dmp->list);
1994 list_for_each_safe(curr, next, &head) {
1995 mp[i] = list_entry(curr, struct lpfc_dmabuf, list);
1996 list_del(curr);
1997
1998 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
1999 mp[i]->buffer_tag = lpfc_sli_get_buffer_tag(phba);
2000 cmd->un.quexri64cx.buff.bde.addrHigh =
2001 putPaddrHigh(mp[i]->phys);
2002 cmd->un.quexri64cx.buff.bde.addrLow =
2003 putPaddrLow(mp[i]->phys);
2004 cmd->un.quexri64cx.buff.bde.tus.f.bdeSize =
2005 ((struct lpfc_dmabufext *)mp[i])->size;
2006 cmd->un.quexri64cx.buff.buffer_tag = mp[i]->buffer_tag;
2007 cmd->ulpCommand = CMD_QUE_XRI64_CX;
2008 cmd->ulpPU = 0;
2009 cmd->ulpLe = 1;
2010 cmd->ulpBdeCount = 1;
2011 cmd->unsli3.que_xri64cx_ext_words.ebde_count = 0;
2012
2013 } else {
2014 cmd->un.cont64[i].addrHigh = putPaddrHigh(mp[i]->phys);
2015 cmd->un.cont64[i].addrLow = putPaddrLow(mp[i]->phys);
2016 cmd->un.cont64[i].tus.f.bdeSize =
2017 ((struct lpfc_dmabufext *)mp[i])->size;
2018 cmd->ulpBdeCount = ++i;
2019
2020 if ((--num_bde > 0) && (i < 2))
2021 continue;
2022
2023 cmd->ulpCommand = CMD_QUE_XRI_BUF64_CX;
2024 cmd->ulpLe = 1;
2025 }
2026
2027 cmd->ulpClass = CLASS3;
2028 cmd->ulpContext = rxxri;
2029
d439d286
JS
2030 iocb_stat = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq,
2031 0);
2032 if (iocb_stat == IOCB_ERROR) {
3b5dd52a
JS
2033 diag_cmd_data_free(phba,
2034 (struct lpfc_dmabufext *)mp[0]);
2035 if (mp[1])
2036 diag_cmd_data_free(phba,
2037 (struct lpfc_dmabufext *)mp[1]);
2038 dmp = list_entry(next, struct lpfc_dmabuf, list);
d439d286 2039 ret_val = -EIO;
3b5dd52a
JS
2040 goto err_post_rxbufs_exit;
2041 }
2042
2043 lpfc_sli_ringpostbuf_put(phba, pring, mp[0]);
2044 if (mp[1]) {
2045 lpfc_sli_ringpostbuf_put(phba, pring, mp[1]);
2046 mp[1] = NULL;
2047 }
2048
2049 /* The iocb was freed by lpfc_sli_issue_iocb */
2050 cmdiocbq = lpfc_sli_get_iocbq(phba);
2051 if (!cmdiocbq) {
2052 dmp = list_entry(next, struct lpfc_dmabuf, list);
d439d286 2053 ret_val = -EIO;
3b5dd52a
JS
2054 goto err_post_rxbufs_exit;
2055 }
2056
2057 cmd = &cmdiocbq->iocb;
2058 i = 0;
2059 }
2060 list_del(&head);
2061
2062err_post_rxbufs_exit:
2063
2064 if (rxbmp) {
2065 if (rxbmp->virt)
2066 lpfc_mbuf_free(phba, rxbmp->virt, rxbmp->phys);
2067 kfree(rxbmp);
2068 }
2069
2070 if (cmdiocbq)
2071 lpfc_sli_release_iocbq(phba, cmdiocbq);
2072 return ret_val;
2073}
2074
2075/**
2076 * lpfc_bsg_diag_test - with a port in loopback issues a Ct cmd to itself
2077 * @job: LPFC_BSG_VENDOR_DIAG_TEST fc_bsg_job
2078 *
2079 * This function receives a user data buffer to be transmitted and received on
2080 * the same port, the link must be up and in loopback mode prior
2081 * to being called.
2082 * 1. A kernel buffer is allocated to copy the user data into.
2083 * 2. The port registers with "itself".
2084 * 3. The transmit and receive exchange ids are obtained.
2085 * 4. The receive exchange id is posted.
2086 * 5. A new els loopback event is created.
2087 * 6. The command and response iocbs are allocated.
2088 * 7. The cmd iocb FsType is set to elx loopback and the CmdRsp to looppback.
2089 *
2090 * This function is meant to be called n times while the port is in loopback
2091 * so it is the apps responsibility to issue a reset to take the port out
2092 * of loopback mode.
2093 **/
2094static int
2095lpfc_bsg_diag_test(struct fc_bsg_job *job)
2096{
2097 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
2098 struct lpfc_hba *phba = vport->phba;
2099 struct diag_mode_test *diag_mode;
2100 struct lpfc_bsg_event *evt;
2101 struct event_data *evdat;
2102 struct lpfc_sli *psli = &phba->sli;
2103 uint32_t size;
2104 uint32_t full_size;
2105 size_t segment_len = 0, segment_offset = 0, current_offset = 0;
4042629e 2106 uint16_t rpi = 0;
3b5dd52a
JS
2107 struct lpfc_iocbq *cmdiocbq, *rspiocbq;
2108 IOCB_t *cmd, *rsp;
2109 struct lpfc_sli_ct_request *ctreq;
2110 struct lpfc_dmabuf *txbmp;
2111 struct ulp_bde64 *txbpl = NULL;
2112 struct lpfc_dmabufext *txbuffer = NULL;
2113 struct list_head head;
2114 struct lpfc_dmabuf *curr;
2115 uint16_t txxri, rxxri;
2116 uint32_t num_bde;
2117 uint8_t *ptr = NULL, *rx_databuf = NULL;
2118 int rc = 0;
d439d286
JS
2119 int time_left;
2120 int iocb_stat;
3b5dd52a
JS
2121 unsigned long flags;
2122 void *dataout = NULL;
2123 uint32_t total_mem;
2124
2125 /* in case no data is returned return just the return code */
2126 job->reply->reply_payload_rcv_len = 0;
2127
2128 if (job->request_len <
2129 sizeof(struct fc_bsg_request) + sizeof(struct diag_mode_test)) {
2130 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2131 "2739 Received DIAG TEST request below minimum "
2132 "size\n");
2133 rc = -EINVAL;
2134 goto loopback_test_exit;
2135 }
2136
2137 if (job->request_payload.payload_len !=
2138 job->reply_payload.payload_len) {
2139 rc = -EINVAL;
2140 goto loopback_test_exit;
2141 }
2142
2143 diag_mode = (struct diag_mode_test *)
2144 job->request->rqst_data.h_vendor.vendor_cmd;
2145
2146 if ((phba->link_state == LPFC_HBA_ERROR) ||
2147 (psli->sli_flag & LPFC_BLOCK_MGMT_IO) ||
2148 (!(psli->sli_flag & LPFC_SLI_ACTIVE))) {
2149 rc = -EACCES;
2150 goto loopback_test_exit;
2151 }
2152
2153 if (!lpfc_is_link_up(phba) || !(phba->link_flag & LS_LOOPBACK_MODE)) {
2154 rc = -EACCES;
2155 goto loopback_test_exit;
2156 }
2157
2158 size = job->request_payload.payload_len;
2159 full_size = size + ELX_LOOPBACK_HEADER_SZ; /* plus the header */
2160
2161 if ((size == 0) || (size > 80 * BUF_SZ_4K)) {
2162 rc = -ERANGE;
2163 goto loopback_test_exit;
2164 }
2165
63e801ce 2166 if (full_size >= BUF_SZ_4K) {
3b5dd52a
JS
2167 /*
2168 * Allocate memory for ioctl data. If buffer is bigger than 64k,
2169 * then we allocate 64k and re-use that buffer over and over to
2170 * xfer the whole block. This is because Linux kernel has a
2171 * problem allocating more than 120k of kernel space memory. Saw
2172 * problem with GET_FCPTARGETMAPPING...
2173 */
2174 if (size <= (64 * 1024))
63e801ce 2175 total_mem = full_size;
3b5dd52a
JS
2176 else
2177 total_mem = 64 * 1024;
2178 } else
2179 /* Allocate memory for ioctl data */
2180 total_mem = BUF_SZ_4K;
2181
2182 dataout = kmalloc(total_mem, GFP_KERNEL);
2183 if (dataout == NULL) {
2184 rc = -ENOMEM;
2185 goto loopback_test_exit;
2186 }
2187
2188 ptr = dataout;
2189 ptr += ELX_LOOPBACK_HEADER_SZ;
2190 sg_copy_to_buffer(job->request_payload.sg_list,
2191 job->request_payload.sg_cnt,
2192 ptr, size);
3b5dd52a 2193 rc = lpfcdiag_loop_self_reg(phba, &rpi);
d439d286 2194 if (rc)
3b5dd52a 2195 goto loopback_test_exit;
3b5dd52a
JS
2196
2197 rc = lpfcdiag_loop_get_xri(phba, rpi, &txxri, &rxxri);
2198 if (rc) {
2199 lpfcdiag_loop_self_unreg(phba, rpi);
3b5dd52a
JS
2200 goto loopback_test_exit;
2201 }
2202
2203 rc = lpfcdiag_loop_post_rxbufs(phba, rxxri, full_size);
2204 if (rc) {
2205 lpfcdiag_loop_self_unreg(phba, rpi);
3b5dd52a
JS
2206 goto loopback_test_exit;
2207 }
2208
2209 evt = lpfc_bsg_event_new(FC_REG_CT_EVENT, current->pid,
2210 SLI_CT_ELX_LOOPBACK);
2211 if (!evt) {
2212 lpfcdiag_loop_self_unreg(phba, rpi);
2213 rc = -ENOMEM;
2214 goto loopback_test_exit;
2215 }
2216
2217 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2218 list_add(&evt->node, &phba->ct_ev_waiters);
2219 lpfc_bsg_event_ref(evt);
2220 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2221
2222 cmdiocbq = lpfc_sli_get_iocbq(phba);
2223 rspiocbq = lpfc_sli_get_iocbq(phba);
2224 txbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2225
2226 if (txbmp) {
2227 txbmp->virt = lpfc_mbuf_alloc(phba, 0, &txbmp->phys);
c7495937
JS
2228 if (txbmp->virt) {
2229 INIT_LIST_HEAD(&txbmp->list);
2230 txbpl = (struct ulp_bde64 *) txbmp->virt;
3b5dd52a
JS
2231 txbuffer = diag_cmd_data_alloc(phba,
2232 txbpl, full_size, 0);
c7495937 2233 }
3b5dd52a
JS
2234 }
2235
c7495937
JS
2236 if (!cmdiocbq || !rspiocbq || !txbmp || !txbpl || !txbuffer ||
2237 !txbmp->virt) {
3b5dd52a
JS
2238 rc = -ENOMEM;
2239 goto err_loopback_test_exit;
2240 }
2241
2242 cmd = &cmdiocbq->iocb;
2243 rsp = &rspiocbq->iocb;
2244
2245 INIT_LIST_HEAD(&head);
2246 list_add_tail(&head, &txbuffer->dma.list);
2247 list_for_each_entry(curr, &head, list) {
2248 segment_len = ((struct lpfc_dmabufext *)curr)->size;
2249 if (current_offset == 0) {
2250 ctreq = curr->virt;
2251 memset(ctreq, 0, ELX_LOOPBACK_HEADER_SZ);
2252 ctreq->RevisionId.bits.Revision = SLI_CT_REVISION;
2253 ctreq->RevisionId.bits.InId = 0;
2254 ctreq->FsType = SLI_CT_ELX_LOOPBACK;
2255 ctreq->FsSubType = 0;
2256 ctreq->CommandResponse.bits.CmdRsp = ELX_LOOPBACK_DATA;
2257 ctreq->CommandResponse.bits.Size = size;
2258 segment_offset = ELX_LOOPBACK_HEADER_SZ;
2259 } else
2260 segment_offset = 0;
2261
2262 BUG_ON(segment_offset >= segment_len);
2263 memcpy(curr->virt + segment_offset,
2264 ptr + current_offset,
2265 segment_len - segment_offset);
2266
2267 current_offset += segment_len - segment_offset;
2268 BUG_ON(current_offset > size);
2269 }
2270 list_del(&head);
2271
2272 /* Build the XMIT_SEQUENCE iocb */
2273
2274 num_bde = (uint32_t)txbuffer->flag;
2275
2276 cmd->un.xseq64.bdl.addrHigh = putPaddrHigh(txbmp->phys);
2277 cmd->un.xseq64.bdl.addrLow = putPaddrLow(txbmp->phys);
2278 cmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
2279 cmd->un.xseq64.bdl.bdeSize = (num_bde * sizeof(struct ulp_bde64));
2280
2281 cmd->un.xseq64.w5.hcsw.Fctl = (LS | LA);
2282 cmd->un.xseq64.w5.hcsw.Dfctl = 0;
2283 cmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
2284 cmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
2285
2286 cmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;
2287 cmd->ulpBdeCount = 1;
2288 cmd->ulpLe = 1;
2289 cmd->ulpClass = CLASS3;
2290 cmd->ulpContext = txxri;
2291
2292 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
2293 cmdiocbq->vport = phba->pport;
2294
d439d286
JS
2295 iocb_stat = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq,
2296 rspiocbq, (phba->fc_ratov * 2) +
2297 LPFC_DRVR_TIMEOUT);
3b5dd52a 2298
d439d286 2299 if ((iocb_stat != IOCB_SUCCESS) || (rsp->ulpStatus != IOCB_SUCCESS)) {
3b5dd52a
JS
2300 rc = -EIO;
2301 goto err_loopback_test_exit;
2302 }
2303
2304 evt->waiting = 1;
d439d286 2305 time_left = wait_event_interruptible_timeout(
3b5dd52a
JS
2306 evt->wq, !list_empty(&evt->events_to_see),
2307 ((phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT) * HZ);
2308 evt->waiting = 0;
2309 if (list_empty(&evt->events_to_see))
d439d286 2310 rc = (time_left) ? -EINTR : -ETIMEDOUT;
3b5dd52a
JS
2311 else {
2312 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2313 list_move(evt->events_to_see.prev, &evt->events_to_get);
2314 evdat = list_entry(evt->events_to_get.prev,
2315 typeof(*evdat), node);
2316 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2317 rx_databuf = evdat->data;
2318 if (evdat->len != full_size) {
2319 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
2320 "1603 Loopback test did not receive expected "
2321 "data length. actual length 0x%x expected "
2322 "length 0x%x\n",
2323 evdat->len, full_size);
2324 rc = -EIO;
2325 } else if (rx_databuf == NULL)
2326 rc = -EIO;
2327 else {
2328 rc = IOCB_SUCCESS;
2329 /* skip over elx loopback header */
2330 rx_databuf += ELX_LOOPBACK_HEADER_SZ;
2331 job->reply->reply_payload_rcv_len =
2332 sg_copy_from_buffer(job->reply_payload.sg_list,
2333 job->reply_payload.sg_cnt,
2334 rx_databuf, size);
2335 job->reply->reply_payload_rcv_len = size;
2336 }
2337 }
2338
2339err_loopback_test_exit:
2340 lpfcdiag_loop_self_unreg(phba, rpi);
2341
2342 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2343 lpfc_bsg_event_unref(evt); /* release ref */
2344 lpfc_bsg_event_unref(evt); /* delete */
2345 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2346
2347 if (cmdiocbq != NULL)
2348 lpfc_sli_release_iocbq(phba, cmdiocbq);
2349
2350 if (rspiocbq != NULL)
2351 lpfc_sli_release_iocbq(phba, rspiocbq);
2352
2353 if (txbmp != NULL) {
2354 if (txbpl != NULL) {
2355 if (txbuffer != NULL)
2356 diag_cmd_data_free(phba, txbuffer);
2357 lpfc_mbuf_free(phba, txbmp->virt, txbmp->phys);
2358 }
2359 kfree(txbmp);
2360 }
2361
2362loopback_test_exit:
2363 kfree(dataout);
2364 /* make error code available to userspace */
2365 job->reply->result = rc;
2366 job->dd_data = NULL;
2367 /* complete the job back to userspace if no error */
2368 if (rc == 0)
2369 job->job_done(job);
2370 return rc;
2371}
2372
2373/**
2374 * lpfc_bsg_get_dfc_rev - process a GET_DFC_REV bsg vendor command
2375 * @job: GET_DFC_REV fc_bsg_job
2376 **/
2377static int
2378lpfc_bsg_get_dfc_rev(struct fc_bsg_job *job)
2379{
2380 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
2381 struct lpfc_hba *phba = vport->phba;
2382 struct get_mgmt_rev *event_req;
2383 struct get_mgmt_rev_reply *event_reply;
2384 int rc = 0;
2385
2386 if (job->request_len <
2387 sizeof(struct fc_bsg_request) + sizeof(struct get_mgmt_rev)) {
2388 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2389 "2740 Received GET_DFC_REV request below "
2390 "minimum size\n");
2391 rc = -EINVAL;
2392 goto job_error;
2393 }
2394
2395 event_req = (struct get_mgmt_rev *)
2396 job->request->rqst_data.h_vendor.vendor_cmd;
2397
2398 event_reply = (struct get_mgmt_rev_reply *)
2399 job->reply->reply_data.vendor_reply.vendor_rsp;
2400
2401 if (job->reply_len <
2402 sizeof(struct fc_bsg_request) + sizeof(struct get_mgmt_rev_reply)) {
2403 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2404 "2741 Received GET_DFC_REV reply below "
2405 "minimum size\n");
2406 rc = -EINVAL;
2407 goto job_error;
2408 }
2409
2410 event_reply->info.a_Major = MANAGEMENT_MAJOR_REV;
2411 event_reply->info.a_Minor = MANAGEMENT_MINOR_REV;
2412job_error:
2413 job->reply->result = rc;
2414 if (rc == 0)
2415 job->job_done(job);
2416 return rc;
2417}
2418
2419/**
2420 * lpfc_bsg_wake_mbox_wait - lpfc_bsg_issue_mbox mbox completion handler
2421 * @phba: Pointer to HBA context object.
2422 * @pmboxq: Pointer to mailbox command.
2423 *
2424 * This is completion handler function for mailbox commands issued from
2425 * lpfc_bsg_issue_mbox function. This function is called by the
2426 * mailbox event handler function with no lock held. This function
2427 * will wake up thread waiting on the wait queue pointed by context1
2428 * of the mailbox.
2429 **/
2430void
2431lpfc_bsg_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
2432{
2433 struct bsg_job_data *dd_data;
3b5dd52a 2434 struct fc_bsg_job *job;
b6e3b9c6 2435 struct lpfc_mbx_nembed_cmd *nembed_sge;
3b5dd52a
JS
2436 uint32_t size;
2437 unsigned long flags;
7a470277
JS
2438 uint8_t *to;
2439 uint8_t *from;
3b5dd52a
JS
2440
2441 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2442 dd_data = pmboxq->context1;
7a470277 2443 /* job already timed out? */
3b5dd52a
JS
2444 if (!dd_data) {
2445 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2446 return;
2447 }
2448
7a470277
JS
2449 /* build the outgoing buffer to do an sg copy
2450 * the format is the response mailbox followed by any extended
2451 * mailbox data
2452 */
2453 from = (uint8_t *)&pmboxq->u.mb;
2454 to = (uint8_t *)dd_data->context_un.mbox.mb;
2455 memcpy(to, from, sizeof(MAILBOX_t));
c7495937
JS
2456 if (pmboxq->u.mb.mbxStatus == MBX_SUCCESS) {
2457 /* copy the extended data if any, count is in words */
2458 if (dd_data->context_un.mbox.outExtWLen) {
2459 from = (uint8_t *)dd_data->context_un.mbox.ext;
2460 to += sizeof(MAILBOX_t);
2461 size = dd_data->context_un.mbox.outExtWLen *
2462 sizeof(uint32_t);
2463 memcpy(to, from, size);
2464 } else if (pmboxq->u.mb.mbxCommand == MBX_RUN_BIU_DIAG64) {
2465 from = (uint8_t *)dd_data->context_un.mbox.
2466 dmp->dma.virt;
2467 to += sizeof(MAILBOX_t);
2468 size = dd_data->context_un.mbox.dmp->size;
2469 memcpy(to, from, size);
2470 } else if ((phba->sli_rev == LPFC_SLI_REV4) &&
2471 (pmboxq->u.mb.mbxCommand == MBX_DUMP_MEMORY)) {
2472 from = (uint8_t *)dd_data->context_un.mbox.dmp->dma.
2473 virt;
2474 to += sizeof(MAILBOX_t);
2475 size = pmboxq->u.mb.un.varWords[5];
2476 memcpy(to, from, size);
515e0aa2
JS
2477 } else if ((phba->sli_rev == LPFC_SLI_REV4) &&
2478 (pmboxq->u.mb.mbxCommand == MBX_SLI4_CONFIG)) {
b6e3b9c6
JS
2479 nembed_sge = (struct lpfc_mbx_nembed_cmd *)
2480 &pmboxq->u.mb.un.varWords[0];
515e0aa2
JS
2481
2482 from = (uint8_t *)dd_data->context_un.mbox.dmp->dma.
2483 virt;
2484 to += sizeof(MAILBOX_t);
2485 size = nembed_sge->sge[0].length;
2486 memcpy(to, from, size);
c7495937
JS
2487 } else if (pmboxq->u.mb.mbxCommand == MBX_READ_EVENT_LOG) {
2488 from = (uint8_t *)dd_data->context_un.
2489 mbox.dmp->dma.virt;
2490 to += sizeof(MAILBOX_t);
2491 size = dd_data->context_un.mbox.dmp->size;
2492 memcpy(to, from, size);
2493 }
7a470277
JS
2494 }
2495
2496 from = (uint8_t *)dd_data->context_un.mbox.mb;
3b5dd52a 2497 job = dd_data->context_un.mbox.set_job;
5a6f133e
JS
2498 if (job) {
2499 size = job->reply_payload.payload_len;
2500 job->reply->reply_payload_rcv_len =
2501 sg_copy_from_buffer(job->reply_payload.sg_list,
2502 job->reply_payload.sg_cnt,
2503 from, size);
2504 job->reply->result = 0;
b6e3b9c6
JS
2505 /* need to hold the lock until we set job->dd_data to NULL
2506 * to hold off the timeout handler returning to the mid-layer
2507 * while we are still processing the job.
2508 */
5a6f133e 2509 job->dd_data = NULL;
b6e3b9c6
JS
2510 dd_data->context_un.mbox.set_job = NULL;
2511 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5a6f133e 2512 job->job_done(job);
b6e3b9c6
JS
2513 } else {
2514 dd_data->context_un.mbox.set_job = NULL;
2515 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5a6f133e 2516 }
7a470277
JS
2517
2518 kfree(dd_data->context_un.mbox.mb);
3b5dd52a 2519 mempool_free(dd_data->context_un.mbox.pmboxq, phba->mbox_mem_pool);
7a470277
JS
2520 kfree(dd_data->context_un.mbox.ext);
2521 if (dd_data->context_un.mbox.dmp) {
2522 dma_free_coherent(&phba->pcidev->dev,
2523 dd_data->context_un.mbox.dmp->size,
2524 dd_data->context_un.mbox.dmp->dma.virt,
2525 dd_data->context_un.mbox.dmp->dma.phys);
2526 kfree(dd_data->context_un.mbox.dmp);
2527 }
2528 if (dd_data->context_un.mbox.rxbmp) {
2529 lpfc_mbuf_free(phba, dd_data->context_un.mbox.rxbmp->virt,
2530 dd_data->context_un.mbox.rxbmp->phys);
2531 kfree(dd_data->context_un.mbox.rxbmp);
2532 }
3b5dd52a
JS
2533 kfree(dd_data);
2534 return;
2535}
2536
2537/**
2538 * lpfc_bsg_check_cmd_access - test for a supported mailbox command
2539 * @phba: Pointer to HBA context object.
2540 * @mb: Pointer to a mailbox object.
2541 * @vport: Pointer to a vport object.
2542 *
2543 * Some commands require the port to be offline, some may not be called from
2544 * the application.
2545 **/
2546static int lpfc_bsg_check_cmd_access(struct lpfc_hba *phba,
2547 MAILBOX_t *mb, struct lpfc_vport *vport)
2548{
2549 /* return negative error values for bsg job */
2550 switch (mb->mbxCommand) {
2551 /* Offline only */
2552 case MBX_INIT_LINK:
2553 case MBX_DOWN_LINK:
2554 case MBX_CONFIG_LINK:
2555 case MBX_CONFIG_RING:
2556 case MBX_RESET_RING:
2557 case MBX_UNREG_LOGIN:
2558 case MBX_CLEAR_LA:
2559 case MBX_DUMP_CONTEXT:
2560 case MBX_RUN_DIAGS:
2561 case MBX_RESTART:
2562 case MBX_SET_MASK:
2563 if (!(vport->fc_flag & FC_OFFLINE_MODE)) {
2564 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2565 "2743 Command 0x%x is illegal in on-line "
2566 "state\n",
2567 mb->mbxCommand);
2568 return -EPERM;
2569 }
2570 case MBX_WRITE_NV:
2571 case MBX_WRITE_VPARMS:
2572 case MBX_LOAD_SM:
2573 case MBX_READ_NV:
2574 case MBX_READ_CONFIG:
2575 case MBX_READ_RCONFIG:
2576 case MBX_READ_STATUS:
2577 case MBX_READ_XRI:
2578 case MBX_READ_REV:
2579 case MBX_READ_LNK_STAT:
2580 case MBX_DUMP_MEMORY:
2581 case MBX_DOWN_LOAD:
2582 case MBX_UPDATE_CFG:
2583 case MBX_KILL_BOARD:
2584 case MBX_LOAD_AREA:
2585 case MBX_LOAD_EXP_ROM:
2586 case MBX_BEACON:
2587 case MBX_DEL_LD_ENTRY:
2588 case MBX_SET_DEBUG:
2589 case MBX_WRITE_WWN:
2590 case MBX_SLI4_CONFIG:
c7495937 2591 case MBX_READ_EVENT_LOG:
3b5dd52a
JS
2592 case MBX_READ_EVENT_LOG_STATUS:
2593 case MBX_WRITE_EVENT_LOG:
2594 case MBX_PORT_CAPABILITIES:
2595 case MBX_PORT_IOV_CONTROL:
7a470277 2596 case MBX_RUN_BIU_DIAG64:
3b5dd52a
JS
2597 break;
2598 case MBX_SET_VARIABLE:
e2aed29f
JS
2599 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2600 "1226 mbox: set_variable 0x%x, 0x%x\n",
2601 mb->un.varWords[0],
2602 mb->un.varWords[1]);
2603 if ((mb->un.varWords[0] == SETVAR_MLOMNT)
2604 && (mb->un.varWords[1] == 1)) {
2605 phba->wait_4_mlo_maint_flg = 1;
2606 } else if (mb->un.varWords[0] == SETVAR_MLORST) {
2607 phba->link_flag &= ~LS_LOOPBACK_MODE;
76a95d75 2608 phba->fc_topology = LPFC_TOPOLOGY_PT_PT;
e2aed29f
JS
2609 }
2610 break;
3b5dd52a 2611 case MBX_READ_SPARM64:
76a95d75 2612 case MBX_READ_TOPOLOGY:
3b5dd52a
JS
2613 case MBX_REG_LOGIN:
2614 case MBX_REG_LOGIN64:
2615 case MBX_CONFIG_PORT:
2616 case MBX_RUN_BIU_DIAG:
2617 default:
2618 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2619 "2742 Unknown Command 0x%x\n",
2620 mb->mbxCommand);
2621 return -EPERM;
2622 }
2623
2624 return 0; /* ok */
2625}
2626
2627/**
2628 * lpfc_bsg_issue_mbox - issues a mailbox command on behalf of an app
2629 * @phba: Pointer to HBA context object.
2630 * @mb: Pointer to a mailbox object.
2631 * @vport: Pointer to a vport object.
2632 *
2633 * Allocate a tracking object, mailbox command memory, get a mailbox
2634 * from the mailbox pool, copy the caller mailbox command.
2635 *
2636 * If offline and the sli is active we need to poll for the command (port is
2637 * being reset) and com-plete the job, otherwise issue the mailbox command and
2638 * let our completion handler finish the command.
2639 **/
2640static uint32_t
2641lpfc_bsg_issue_mbox(struct lpfc_hba *phba, struct fc_bsg_job *job,
2642 struct lpfc_vport *vport)
2643{
7a470277
JS
2644 LPFC_MBOXQ_t *pmboxq = NULL; /* internal mailbox queue */
2645 MAILBOX_t *pmb; /* shortcut to the pmboxq mailbox */
2646 /* a 4k buffer to hold the mb and extended data from/to the bsg */
2647 MAILBOX_t *mb = NULL;
2648 struct bsg_job_data *dd_data = NULL; /* bsg data tracking structure */
3b5dd52a 2649 uint32_t size;
7a470277
JS
2650 struct lpfc_dmabuf *rxbmp = NULL; /* for biu diag */
2651 struct lpfc_dmabufext *dmp = NULL; /* for biu diag */
2652 struct ulp_bde64 *rxbpl = NULL;
2653 struct dfc_mbox_req *mbox_req = (struct dfc_mbox_req *)
2654 job->request->rqst_data.h_vendor.vendor_cmd;
b6e3b9c6
JS
2655 struct READ_EVENT_LOG_VAR *rdEventLog;
2656 uint32_t transmit_length, receive_length, mode;
2657 struct lpfc_mbx_nembed_cmd *nembed_sge;
2658 struct mbox_header *header;
2659 struct ulp_bde64 *bde;
7a470277 2660 uint8_t *ext = NULL;
3b5dd52a 2661 int rc = 0;
7a470277
JS
2662 uint8_t *from;
2663
2664 /* in case no data is transferred */
2665 job->reply->reply_payload_rcv_len = 0;
2666
b6e3b9c6
JS
2667 /* sanity check to protect driver */
2668 if (job->reply_payload.payload_len > BSG_MBOX_SIZE ||
2669 job->request_payload.payload_len > BSG_MBOX_SIZE) {
2670 rc = -ERANGE;
2671 goto job_done;
2672 }
2673
7a470277 2674 /* check if requested extended data lengths are valid */
b6e3b9c6
JS
2675 if ((mbox_req->inExtWLen > BSG_MBOX_SIZE/sizeof(uint32_t)) ||
2676 (mbox_req->outExtWLen > BSG_MBOX_SIZE/sizeof(uint32_t))) {
7a470277
JS
2677 rc = -ERANGE;
2678 goto job_done;
2679 }
3b5dd52a
JS
2680
2681 /* allocate our bsg tracking structure */
2682 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
2683 if (!dd_data) {
2684 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2685 "2727 Failed allocation of dd_data\n");
7a470277
JS
2686 rc = -ENOMEM;
2687 goto job_done;
3b5dd52a
JS
2688 }
2689
49198b37 2690 mb = kzalloc(BSG_MBOX_SIZE, GFP_KERNEL);
3b5dd52a 2691 if (!mb) {
7a470277
JS
2692 rc = -ENOMEM;
2693 goto job_done;
3b5dd52a
JS
2694 }
2695
2696 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2697 if (!pmboxq) {
7a470277
JS
2698 rc = -ENOMEM;
2699 goto job_done;
3b5dd52a 2700 }
7a470277 2701 memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
3b5dd52a
JS
2702
2703 size = job->request_payload.payload_len;
7a470277
JS
2704 sg_copy_to_buffer(job->request_payload.sg_list,
2705 job->request_payload.sg_cnt,
2706 mb, size);
3b5dd52a
JS
2707
2708 rc = lpfc_bsg_check_cmd_access(phba, mb, vport);
7a470277
JS
2709 if (rc != 0)
2710 goto job_done; /* must be negative */
3b5dd52a 2711
3b5dd52a
JS
2712 pmb = &pmboxq->u.mb;
2713 memcpy(pmb, mb, sizeof(*pmb));
2714 pmb->mbxOwner = OWN_HOST;
3b5dd52a
JS
2715 pmboxq->vport = vport;
2716
c7495937
JS
2717 /* If HBA encountered an error attention, allow only DUMP
2718 * or RESTART mailbox commands until the HBA is restarted.
2719 */
2720 if (phba->pport->stopped &&
2721 pmb->mbxCommand != MBX_DUMP_MEMORY &&
2722 pmb->mbxCommand != MBX_RESTART &&
2723 pmb->mbxCommand != MBX_WRITE_VPARMS &&
2724 pmb->mbxCommand != MBX_WRITE_WWN)
2725 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
2726 "2797 mbox: Issued mailbox cmd "
2727 "0x%x while in stopped state.\n",
2728 pmb->mbxCommand);
2729
2730 /* Don't allow mailbox commands to be sent when blocked
2731 * or when in the middle of discovery
2732 */
2733 if (phba->sli.sli_flag & LPFC_BLOCK_MGMT_IO) {
2734 rc = -EAGAIN;
2735 goto job_done;
2736 }
2737
7a470277 2738 /* extended mailbox commands will need an extended buffer */
c7495937 2739 if (mbox_req->inExtWLen || mbox_req->outExtWLen) {
7a470277
JS
2740 ext = kzalloc(MAILBOX_EXT_SIZE, GFP_KERNEL);
2741 if (!ext) {
2742 rc = -ENOMEM;
2743 goto job_done;
2744 }
2745
2746 /* any data for the device? */
2747 if (mbox_req->inExtWLen) {
2748 from = (uint8_t *)mb;
2749 from += sizeof(MAILBOX_t);
2750 memcpy((uint8_t *)ext, from,
2751 mbox_req->inExtWLen * sizeof(uint32_t));
2752 }
2753
7a470277
JS
2754 pmboxq->context2 = ext;
2755 pmboxq->in_ext_byte_len =
2756 mbox_req->inExtWLen * sizeof(uint32_t);
2757 pmboxq->out_ext_byte_len =
c7495937 2758 mbox_req->outExtWLen * sizeof(uint32_t);
7a470277
JS
2759 pmboxq->mbox_offset_word = mbox_req->mbOffset;
2760 }
2761
2762 /* biu diag will need a kernel buffer to transfer the data
2763 * allocate our own buffer and setup the mailbox command to
2764 * use ours
2765 */
2766 if (pmb->mbxCommand == MBX_RUN_BIU_DIAG64) {
b6e3b9c6
JS
2767 transmit_length = pmb->un.varWords[1];
2768 receive_length = pmb->un.varWords[4];
c7495937
JS
2769 /* transmit length cannot be greater than receive length or
2770 * mailbox extension size
2771 */
2772 if ((transmit_length > receive_length) ||
2773 (transmit_length > MAILBOX_EXT_SIZE)) {
2774 rc = -ERANGE;
2775 goto job_done;
2776 }
2777
7a470277
JS
2778 rxbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2779 if (!rxbmp) {
2780 rc = -ENOMEM;
2781 goto job_done;
2782 }
2783
2784 rxbmp->virt = lpfc_mbuf_alloc(phba, 0, &rxbmp->phys);
c7495937
JS
2785 if (!rxbmp->virt) {
2786 rc = -ENOMEM;
2787 goto job_done;
2788 }
2789
7a470277
JS
2790 INIT_LIST_HEAD(&rxbmp->list);
2791 rxbpl = (struct ulp_bde64 *) rxbmp->virt;
c7495937 2792 dmp = diag_cmd_data_alloc(phba, rxbpl, transmit_length, 0);
7a470277
JS
2793 if (!dmp) {
2794 rc = -ENOMEM;
2795 goto job_done;
2796 }
2797
7a470277
JS
2798 INIT_LIST_HEAD(&dmp->dma.list);
2799 pmb->un.varBIUdiag.un.s2.xmit_bde64.addrHigh =
2800 putPaddrHigh(dmp->dma.phys);
2801 pmb->un.varBIUdiag.un.s2.xmit_bde64.addrLow =
2802 putPaddrLow(dmp->dma.phys);
2803
2804 pmb->un.varBIUdiag.un.s2.rcv_bde64.addrHigh =
2805 putPaddrHigh(dmp->dma.phys +
2806 pmb->un.varBIUdiag.un.s2.
2807 xmit_bde64.tus.f.bdeSize);
2808 pmb->un.varBIUdiag.un.s2.rcv_bde64.addrLow =
2809 putPaddrLow(dmp->dma.phys +
2810 pmb->un.varBIUdiag.un.s2.
2811 xmit_bde64.tus.f.bdeSize);
c7495937
JS
2812
2813 /* copy the transmit data found in the mailbox extension area */
2814 from = (uint8_t *)mb;
2815 from += sizeof(MAILBOX_t);
2816 memcpy((uint8_t *)dmp->dma.virt, from, transmit_length);
2817 } else if (pmb->mbxCommand == MBX_READ_EVENT_LOG) {
b6e3b9c6
JS
2818 rdEventLog = &pmb->un.varRdEventLog;
2819 receive_length = rdEventLog->rcv_bde64.tus.f.bdeSize;
2820 mode = bf_get(lpfc_event_log, rdEventLog);
c7495937
JS
2821
2822 /* receive length cannot be greater than mailbox
2823 * extension size
2824 */
2825 if (receive_length > MAILBOX_EXT_SIZE) {
2826 rc = -ERANGE;
2827 goto job_done;
2828 }
2829
2830 /* mode zero uses a bde like biu diags command */
2831 if (mode == 0) {
2832
2833 /* rebuild the command for sli4 using our own buffers
2834 * like we do for biu diags
2835 */
2836
2837 rxbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2838 if (!rxbmp) {
2839 rc = -ENOMEM;
2840 goto job_done;
2841 }
2842
2843 rxbmp->virt = lpfc_mbuf_alloc(phba, 0, &rxbmp->phys);
2844 rxbpl = (struct ulp_bde64 *) rxbmp->virt;
2845 if (rxbpl) {
2846 INIT_LIST_HEAD(&rxbmp->list);
2847 dmp = diag_cmd_data_alloc(phba, rxbpl,
2848 receive_length, 0);
2849 }
2850
2851 if (!dmp) {
2852 rc = -ENOMEM;
2853 goto job_done;
2854 }
2855
2856 INIT_LIST_HEAD(&dmp->dma.list);
2857 pmb->un.varWords[3] = putPaddrLow(dmp->dma.phys);
2858 pmb->un.varWords[4] = putPaddrHigh(dmp->dma.phys);
2859 }
2860 } else if (phba->sli_rev == LPFC_SLI_REV4) {
2861 if (pmb->mbxCommand == MBX_DUMP_MEMORY) {
2862 /* rebuild the command for sli4 using our own buffers
2863 * like we do for biu diags
2864 */
b6e3b9c6 2865 receive_length = pmb->un.varWords[2];
c7495937
JS
2866 /* receive length cannot be greater than mailbox
2867 * extension size
2868 */
2869 if ((receive_length == 0) ||
2870 (receive_length > MAILBOX_EXT_SIZE)) {
2871 rc = -ERANGE;
2872 goto job_done;
2873 }
2874
2875 rxbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2876 if (!rxbmp) {
2877 rc = -ENOMEM;
2878 goto job_done;
2879 }
2880
2881 rxbmp->virt = lpfc_mbuf_alloc(phba, 0, &rxbmp->phys);
2882 if (!rxbmp->virt) {
2883 rc = -ENOMEM;
2884 goto job_done;
2885 }
2886
2887 INIT_LIST_HEAD(&rxbmp->list);
2888 rxbpl = (struct ulp_bde64 *) rxbmp->virt;
2889 dmp = diag_cmd_data_alloc(phba, rxbpl, receive_length,
2890 0);
2891 if (!dmp) {
2892 rc = -ENOMEM;
2893 goto job_done;
2894 }
2895
2896 INIT_LIST_HEAD(&dmp->dma.list);
2897 pmb->un.varWords[3] = putPaddrLow(dmp->dma.phys);
2898 pmb->un.varWords[4] = putPaddrHigh(dmp->dma.phys);
2899 } else if ((pmb->mbxCommand == MBX_UPDATE_CFG) &&
2900 pmb->un.varUpdateCfg.co) {
b6e3b9c6 2901 bde = (struct ulp_bde64 *)&pmb->un.varWords[4];
c7495937
JS
2902
2903 /* bde size cannot be greater than mailbox ext size */
2904 if (bde->tus.f.bdeSize > MAILBOX_EXT_SIZE) {
2905 rc = -ERANGE;
2906 goto job_done;
2907 }
2908
2909 rxbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2910 if (!rxbmp) {
2911 rc = -ENOMEM;
2912 goto job_done;
2913 }
2914
2915 rxbmp->virt = lpfc_mbuf_alloc(phba, 0, &rxbmp->phys);
2916 if (!rxbmp->virt) {
2917 rc = -ENOMEM;
2918 goto job_done;
2919 }
2920
2921 INIT_LIST_HEAD(&rxbmp->list);
2922 rxbpl = (struct ulp_bde64 *) rxbmp->virt;
2923 dmp = diag_cmd_data_alloc(phba, rxbpl,
2924 bde->tus.f.bdeSize, 0);
2925 if (!dmp) {
2926 rc = -ENOMEM;
2927 goto job_done;
2928 }
2929
2930 INIT_LIST_HEAD(&dmp->dma.list);
2931 bde->addrHigh = putPaddrHigh(dmp->dma.phys);
2932 bde->addrLow = putPaddrLow(dmp->dma.phys);
2933
2934 /* copy the transmit data found in the mailbox
2935 * extension area
2936 */
2937 from = (uint8_t *)mb;
2938 from += sizeof(MAILBOX_t);
2939 memcpy((uint8_t *)dmp->dma.virt, from,
2940 bde->tus.f.bdeSize);
515e0aa2 2941 } else if (pmb->mbxCommand == MBX_SLI4_CONFIG) {
515e0aa2
JS
2942 /* rebuild the command for sli4 using our own buffers
2943 * like we do for biu diags
2944 */
2945 header = (struct mbox_header *)&pmb->un.varWords[0];
2946 nembed_sge = (struct lpfc_mbx_nembed_cmd *)
2947 &pmb->un.varWords[0];
2948 receive_length = nembed_sge->sge[0].length;
2949
2950 /* receive length cannot be greater than mailbox
2951 * extension size
2952 */
2953 if ((receive_length == 0) ||
2954 (receive_length > MAILBOX_EXT_SIZE)) {
2955 rc = -ERANGE;
2956 goto job_done;
2957 }
2958
2959 rxbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2960 if (!rxbmp) {
2961 rc = -ENOMEM;
2962 goto job_done;
2963 }
2964
2965 rxbmp->virt = lpfc_mbuf_alloc(phba, 0, &rxbmp->phys);
2966 if (!rxbmp->virt) {
2967 rc = -ENOMEM;
2968 goto job_done;
2969 }
2970
2971 INIT_LIST_HEAD(&rxbmp->list);
2972 rxbpl = (struct ulp_bde64 *) rxbmp->virt;
2973 dmp = diag_cmd_data_alloc(phba, rxbpl, receive_length,
2974 0);
2975 if (!dmp) {
2976 rc = -ENOMEM;
2977 goto job_done;
2978 }
2979
2980 INIT_LIST_HEAD(&dmp->dma.list);
2981 nembed_sge->sge[0].pa_hi = putPaddrHigh(dmp->dma.phys);
2982 nembed_sge->sge[0].pa_lo = putPaddrLow(dmp->dma.phys);
2983 /* copy the transmit data found in the mailbox
2984 * extension area
2985 */
2986 from = (uint8_t *)mb;
2987 from += sizeof(MAILBOX_t);
2988 memcpy((uint8_t *)dmp->dma.virt, from,
2989 header->cfg_mhdr.payload_length);
c7495937 2990 }
7a470277
JS
2991 }
2992
c7495937
JS
2993 dd_data->context_un.mbox.rxbmp = rxbmp;
2994 dd_data->context_un.mbox.dmp = dmp;
2995
7a470277
JS
2996 /* setup wake call as IOCB callback */
2997 pmboxq->mbox_cmpl = lpfc_bsg_wake_mbox_wait;
2998
2999 /* setup context field to pass wait_queue pointer to wake function */
3000 pmboxq->context1 = dd_data;
3001 dd_data->type = TYPE_MBOX;
3002 dd_data->context_un.mbox.pmboxq = pmboxq;
3003 dd_data->context_un.mbox.mb = mb;
3004 dd_data->context_un.mbox.set_job = job;
3005 dd_data->context_un.mbox.ext = ext;
3006 dd_data->context_un.mbox.mbOffset = mbox_req->mbOffset;
3007 dd_data->context_un.mbox.inExtWLen = mbox_req->inExtWLen;
c7495937 3008 dd_data->context_un.mbox.outExtWLen = mbox_req->outExtWLen;
7a470277
JS
3009 job->dd_data = dd_data;
3010
3b5dd52a
JS
3011 if ((vport->fc_flag & FC_OFFLINE_MODE) ||
3012 (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))) {
3013 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
3014 if (rc != MBX_SUCCESS) {
7a470277
JS
3015 rc = (rc == MBX_TIMEOUT) ? -ETIME : -ENODEV;
3016 goto job_done;
3b5dd52a
JS
3017 }
3018
7a470277 3019 /* job finished, copy the data */
3b5dd52a
JS
3020 memcpy(mb, pmb, sizeof(*pmb));
3021 job->reply->reply_payload_rcv_len =
3022 sg_copy_from_buffer(job->reply_payload.sg_list,
3023 job->reply_payload.sg_cnt,
3024 mb, size);
3b5dd52a 3025 /* not waiting mbox already done */
7a470277
JS
3026 rc = 0;
3027 goto job_done;
3b5dd52a
JS
3028 }
3029
3b5dd52a 3030 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
7a470277
JS
3031 if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY))
3032 return 1; /* job started */
3033
3034job_done:
3035 /* common exit for error or job completed inline */
3036 kfree(mb);
3037 if (pmboxq)
3b5dd52a 3038 mempool_free(pmboxq, phba->mbox_mem_pool);
7a470277
JS
3039 kfree(ext);
3040 if (dmp) {
3041 dma_free_coherent(&phba->pcidev->dev,
3042 dmp->size, dmp->dma.virt,
3043 dmp->dma.phys);
3044 kfree(dmp);
3045 }
3046 if (rxbmp) {
3047 lpfc_mbuf_free(phba, rxbmp->virt, rxbmp->phys);
3048 kfree(rxbmp);
3b5dd52a 3049 }
7a470277 3050 kfree(dd_data);
3b5dd52a 3051
7a470277 3052 return rc;
3b5dd52a
JS
3053}
3054
3055/**
3056 * lpfc_bsg_mbox_cmd - process an fc bsg LPFC_BSG_VENDOR_MBOX command
3057 * @job: MBOX fc_bsg_job for LPFC_BSG_VENDOR_MBOX.
3058 **/
3059static int
3060lpfc_bsg_mbox_cmd(struct fc_bsg_job *job)
3061{
3062 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
3063 struct lpfc_hba *phba = vport->phba;
3064 int rc = 0;
3065
3066 /* in case no data is transferred */
3067 job->reply->reply_payload_rcv_len = 0;
3068 if (job->request_len <
3069 sizeof(struct fc_bsg_request) + sizeof(struct dfc_mbox_req)) {
3070 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3071 "2737 Received MBOX_REQ request below "
3072 "minimum size\n");
3073 rc = -EINVAL;
3074 goto job_error;
3075 }
3076
49198b37 3077 if (job->request_payload.payload_len != BSG_MBOX_SIZE) {
3b5dd52a
JS
3078 rc = -EINVAL;
3079 goto job_error;
3080 }
3081
49198b37 3082 if (job->reply_payload.payload_len != BSG_MBOX_SIZE) {
7a470277
JS
3083 rc = -EINVAL;
3084 goto job_error;
3085 }
3086
3b5dd52a
JS
3087 if (phba->sli.sli_flag & LPFC_BLOCK_MGMT_IO) {
3088 rc = -EAGAIN;
3089 goto job_error;
3090 }
3091
3092 rc = lpfc_bsg_issue_mbox(phba, job, vport);
3093
3094job_error:
3095 if (rc == 0) {
3096 /* job done */
3097 job->reply->result = 0;
3098 job->dd_data = NULL;
3099 job->job_done(job);
3100 } else if (rc == 1)
3101 /* job submitted, will complete later*/
3102 rc = 0; /* return zero, no error */
3103 else {
3104 /* some error occurred */
3105 job->reply->result = rc;
3106 job->dd_data = NULL;
3107 }
3108
3109 return rc;
3110}
3111
e2aed29f
JS
3112/**
3113 * lpfc_bsg_menlo_cmd_cmp - lpfc_menlo_cmd completion handler
3114 * @phba: Pointer to HBA context object.
3115 * @cmdiocbq: Pointer to command iocb.
3116 * @rspiocbq: Pointer to response iocb.
3117 *
3118 * This function is the completion handler for iocbs issued using
3119 * lpfc_menlo_cmd function. This function is called by the
3120 * ring event handler function without any lock held. This function
3121 * can be called from both worker thread context and interrupt
3122 * context. This function also can be called from another thread which
3123 * cleans up the SLI layer objects.
3124 * This function copies the contents of the response iocb to the
3125 * response iocb memory object provided by the caller of
3126 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
3127 * sleeps for the iocb completion.
3128 **/
3129static void
3130lpfc_bsg_menlo_cmd_cmp(struct lpfc_hba *phba,
3131 struct lpfc_iocbq *cmdiocbq,
3132 struct lpfc_iocbq *rspiocbq)
3133{
3134 struct bsg_job_data *dd_data;
3135 struct fc_bsg_job *job;
3136 IOCB_t *rsp;
3137 struct lpfc_dmabuf *bmp;
3138 struct lpfc_bsg_menlo *menlo;
3139 unsigned long flags;
3140 struct menlo_response *menlo_resp;
3141 int rc = 0;
3142
3143 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3144 dd_data = cmdiocbq->context1;
3145 if (!dd_data) {
3146 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3147 return;
3148 }
3149
3150 menlo = &dd_data->context_un.menlo;
3151 job = menlo->set_job;
3152 job->dd_data = NULL; /* so timeout handler does not reply */
3153
5989b8d4 3154 spin_lock(&phba->hbalock);
e2aed29f
JS
3155 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
3156 if (cmdiocbq->context2 && rspiocbq)
3157 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
3158 &rspiocbq->iocb, sizeof(IOCB_t));
5989b8d4 3159 spin_unlock(&phba->hbalock);
e2aed29f
JS
3160
3161 bmp = menlo->bmp;
3162 rspiocbq = menlo->rspiocbq;
3163 rsp = &rspiocbq->iocb;
3164
3165 pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
3166 job->request_payload.sg_cnt, DMA_TO_DEVICE);
3167 pci_unmap_sg(phba->pcidev, job->reply_payload.sg_list,
3168 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
3169
3170 /* always return the xri, this would be used in the case
3171 * of a menlo download to allow the data to be sent as a continuation
3172 * of the exchange.
3173 */
3174 menlo_resp = (struct menlo_response *)
3175 job->reply->reply_data.vendor_reply.vendor_rsp;
3176 menlo_resp->xri = rsp->ulpContext;
3177 if (rsp->ulpStatus) {
3178 if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
3179 switch (rsp->un.ulpWord[4] & 0xff) {
3180 case IOERR_SEQUENCE_TIMEOUT:
3181 rc = -ETIMEDOUT;
3182 break;
3183 case IOERR_INVALID_RPI:
3184 rc = -EFAULT;
3185 break;
3186 default:
3187 rc = -EACCES;
3188 break;
3189 }
3190 } else
3191 rc = -EACCES;
3192 } else
3193 job->reply->reply_payload_rcv_len =
3194 rsp->un.genreq64.bdl.bdeSize;
3195
3196 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
3197 lpfc_sli_release_iocbq(phba, rspiocbq);
3198 lpfc_sli_release_iocbq(phba, cmdiocbq);
3199 kfree(bmp);
3200 kfree(dd_data);
3201 /* make error code available to userspace */
3202 job->reply->result = rc;
3203 /* complete the job back to userspace */
3204 job->job_done(job);
3205 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3206 return;
3207}
3208
3209/**
3210 * lpfc_menlo_cmd - send an ioctl for menlo hardware
3211 * @job: fc_bsg_job to handle
3212 *
3213 * This function issues a gen request 64 CR ioctl for all menlo cmd requests,
3214 * all the command completions will return the xri for the command.
3215 * For menlo data requests a gen request 64 CX is used to continue the exchange
3216 * supplied in the menlo request header xri field.
3217 **/
3218static int
3219lpfc_menlo_cmd(struct fc_bsg_job *job)
3220{
3221 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
3222 struct lpfc_hba *phba = vport->phba;
3223 struct lpfc_iocbq *cmdiocbq, *rspiocbq;
3224 IOCB_t *cmd, *rsp;
3225 int rc = 0;
3226 struct menlo_command *menlo_cmd;
3227 struct menlo_response *menlo_resp;
3228 struct lpfc_dmabuf *bmp = NULL;
3229 int request_nseg;
3230 int reply_nseg;
3231 struct scatterlist *sgel = NULL;
3232 int numbde;
3233 dma_addr_t busaddr;
3234 struct bsg_job_data *dd_data;
3235 struct ulp_bde64 *bpl = NULL;
3236
3237 /* in case no data is returned return just the return code */
3238 job->reply->reply_payload_rcv_len = 0;
3239
3240 if (job->request_len <
3241 sizeof(struct fc_bsg_request) +
3242 sizeof(struct menlo_command)) {
3243 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3244 "2784 Received MENLO_CMD request below "
3245 "minimum size\n");
3246 rc = -ERANGE;
3247 goto no_dd_data;
3248 }
3249
3250 if (job->reply_len <
3251 sizeof(struct fc_bsg_request) + sizeof(struct menlo_response)) {
3252 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3253 "2785 Received MENLO_CMD reply below "
3254 "minimum size\n");
3255 rc = -ERANGE;
3256 goto no_dd_data;
3257 }
3258
3259 if (!(phba->menlo_flag & HBA_MENLO_SUPPORT)) {
3260 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3261 "2786 Adapter does not support menlo "
3262 "commands\n");
3263 rc = -EPERM;
3264 goto no_dd_data;
3265 }
3266
3267 menlo_cmd = (struct menlo_command *)
3268 job->request->rqst_data.h_vendor.vendor_cmd;
3269
3270 menlo_resp = (struct menlo_response *)
3271 job->reply->reply_data.vendor_reply.vendor_rsp;
3272
3273 /* allocate our bsg tracking structure */
3274 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
3275 if (!dd_data) {
3276 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3277 "2787 Failed allocation of dd_data\n");
3278 rc = -ENOMEM;
3279 goto no_dd_data;
3280 }
3281
3282 bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3283 if (!bmp) {
3284 rc = -ENOMEM;
3285 goto free_dd;
3286 }
3287
3288 cmdiocbq = lpfc_sli_get_iocbq(phba);
3289 if (!cmdiocbq) {
3290 rc = -ENOMEM;
3291 goto free_bmp;
3292 }
3293
3294 rspiocbq = lpfc_sli_get_iocbq(phba);
3295 if (!rspiocbq) {
3296 rc = -ENOMEM;
3297 goto free_cmdiocbq;
3298 }
3299
3300 rsp = &rspiocbq->iocb;
3301
3302 bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
3303 if (!bmp->virt) {
3304 rc = -ENOMEM;
3305 goto free_rspiocbq;
3306 }
3307
3308 INIT_LIST_HEAD(&bmp->list);
3309 bpl = (struct ulp_bde64 *) bmp->virt;
3310 request_nseg = pci_map_sg(phba->pcidev, job->request_payload.sg_list,
3311 job->request_payload.sg_cnt, DMA_TO_DEVICE);
3312 for_each_sg(job->request_payload.sg_list, sgel, request_nseg, numbde) {
3313 busaddr = sg_dma_address(sgel);
3314 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
3315 bpl->tus.f.bdeSize = sg_dma_len(sgel);
3316 bpl->tus.w = cpu_to_le32(bpl->tus.w);
3317 bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
3318 bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
3319 bpl++;
3320 }
3321
3322 reply_nseg = pci_map_sg(phba->pcidev, job->reply_payload.sg_list,
3323 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
3324 for_each_sg(job->reply_payload.sg_list, sgel, reply_nseg, numbde) {
3325 busaddr = sg_dma_address(sgel);
3326 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
3327 bpl->tus.f.bdeSize = sg_dma_len(sgel);
3328 bpl->tus.w = cpu_to_le32(bpl->tus.w);
3329 bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
3330 bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
3331 bpl++;
3332 }
3333
3334 cmd = &cmdiocbq->iocb;
3335 cmd->un.genreq64.bdl.ulpIoTag32 = 0;
3336 cmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
3337 cmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
3338 cmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
3339 cmd->un.genreq64.bdl.bdeSize =
3340 (request_nseg + reply_nseg) * sizeof(struct ulp_bde64);
3341 cmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
3342 cmd->un.genreq64.w5.hcsw.Dfctl = 0;
3343 cmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CMD;
3344 cmd->un.genreq64.w5.hcsw.Type = MENLO_TRANSPORT_TYPE; /* 0xfe */
3345 cmd->ulpBdeCount = 1;
3346 cmd->ulpClass = CLASS3;
3347 cmd->ulpOwner = OWN_CHIP;
3348 cmd->ulpLe = 1; /* Limited Edition */
3349 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
3350 cmdiocbq->vport = phba->pport;
3351 /* We want the firmware to timeout before we do */
3352 cmd->ulpTimeout = MENLO_TIMEOUT - 5;
3353 cmdiocbq->context3 = bmp;
3354 cmdiocbq->context2 = rspiocbq;
3355 cmdiocbq->iocb_cmpl = lpfc_bsg_menlo_cmd_cmp;
3356 cmdiocbq->context1 = dd_data;
3357 cmdiocbq->context2 = rspiocbq;
3358 if (menlo_cmd->cmd == LPFC_BSG_VENDOR_MENLO_CMD) {
3359 cmd->ulpCommand = CMD_GEN_REQUEST64_CR;
3360 cmd->ulpPU = MENLO_PU; /* 3 */
3361 cmd->un.ulpWord[4] = MENLO_DID; /* 0x0000FC0E */
3362 cmd->ulpContext = MENLO_CONTEXT; /* 0 */
3363 } else {
3364 cmd->ulpCommand = CMD_GEN_REQUEST64_CX;
3365 cmd->ulpPU = 1;
3366 cmd->un.ulpWord[4] = 0;
3367 cmd->ulpContext = menlo_cmd->xri;
3368 }
3369
3370 dd_data->type = TYPE_MENLO;
3371 dd_data->context_un.menlo.cmdiocbq = cmdiocbq;
3372 dd_data->context_un.menlo.rspiocbq = rspiocbq;
3373 dd_data->context_un.menlo.set_job = job;
3374 dd_data->context_un.menlo.bmp = bmp;
3375
3376 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq,
3377 MENLO_TIMEOUT - 5);
3378 if (rc == IOCB_SUCCESS)
3379 return 0; /* done for now */
3380
3381 /* iocb failed so cleanup */
3382 pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
3383 job->request_payload.sg_cnt, DMA_TO_DEVICE);
3384 pci_unmap_sg(phba->pcidev, job->reply_payload.sg_list,
3385 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
3386
3387 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
3388
3389free_rspiocbq:
3390 lpfc_sli_release_iocbq(phba, rspiocbq);
3391free_cmdiocbq:
3392 lpfc_sli_release_iocbq(phba, cmdiocbq);
3393free_bmp:
3394 kfree(bmp);
3395free_dd:
3396 kfree(dd_data);
3397no_dd_data:
3398 /* make error code available to userspace */
3399 job->reply->result = rc;
3400 job->dd_data = NULL;
3401 return rc;
3402}
b6e3b9c6 3403
3b5dd52a
JS
3404/**
3405 * lpfc_bsg_hst_vendor - process a vendor-specific fc_bsg_job
3406 * @job: fc_bsg_job to handle
3407 **/
3408static int
3409lpfc_bsg_hst_vendor(struct fc_bsg_job *job)
3410{
3411 int command = job->request->rqst_data.h_vendor.vendor_cmd[0];
3412 int rc;
3413
3414 switch (command) {
3415 case LPFC_BSG_VENDOR_SET_CT_EVENT:
3416 rc = lpfc_bsg_hba_set_event(job);
3417 break;
3418 case LPFC_BSG_VENDOR_GET_CT_EVENT:
3419 rc = lpfc_bsg_hba_get_event(job);
3420 break;
3421 case LPFC_BSG_VENDOR_SEND_MGMT_RESP:
3422 rc = lpfc_bsg_send_mgmt_rsp(job);
3423 break;
3424 case LPFC_BSG_VENDOR_DIAG_MODE:
3425 rc = lpfc_bsg_diag_mode(job);
3426 break;
3427 case LPFC_BSG_VENDOR_DIAG_TEST:
3428 rc = lpfc_bsg_diag_test(job);
3429 break;
3430 case LPFC_BSG_VENDOR_GET_MGMT_REV:
3431 rc = lpfc_bsg_get_dfc_rev(job);
3432 break;
3433 case LPFC_BSG_VENDOR_MBOX:
3434 rc = lpfc_bsg_mbox_cmd(job);
3435 break;
e2aed29f
JS
3436 case LPFC_BSG_VENDOR_MENLO_CMD:
3437 case LPFC_BSG_VENDOR_MENLO_DATA:
3438 rc = lpfc_menlo_cmd(job);
3439 break;
3b5dd52a
JS
3440 default:
3441 rc = -EINVAL;
3442 job->reply->reply_payload_rcv_len = 0;
3443 /* make error code available to userspace */
3444 job->reply->result = rc;
3445 break;
3446 }
3447
3448 return rc;
3449}
3450
3451/**
3452 * lpfc_bsg_request - handle a bsg request from the FC transport
3453 * @job: fc_bsg_job to handle
3454 **/
3455int
3456lpfc_bsg_request(struct fc_bsg_job *job)
3457{
3458 uint32_t msgcode;
3459 int rc;
3460
3461 msgcode = job->request->msgcode;
3462 switch (msgcode) {
3463 case FC_BSG_HST_VENDOR:
3464 rc = lpfc_bsg_hst_vendor(job);
3465 break;
3466 case FC_BSG_RPT_ELS:
3467 rc = lpfc_bsg_rport_els(job);
3468 break;
3469 case FC_BSG_RPT_CT:
3470 rc = lpfc_bsg_send_mgmt_cmd(job);
3471 break;
3472 default:
3473 rc = -EINVAL;
3474 job->reply->reply_payload_rcv_len = 0;
3475 /* make error code available to userspace */
3476 job->reply->result = rc;
3477 break;
3478 }
3479
3480 return rc;
3481}
3482
3483/**
3484 * lpfc_bsg_timeout - handle timeout of a bsg request from the FC transport
3485 * @job: fc_bsg_job that has timed out
3486 *
3487 * This function just aborts the job's IOCB. The aborted IOCB will return to
3488 * the waiting function which will handle passing the error back to userspace
3489 **/
3490int
3491lpfc_bsg_timeout(struct fc_bsg_job *job)
3492{
3493 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
3494 struct lpfc_hba *phba = vport->phba;
3495 struct lpfc_iocbq *cmdiocb;
3496 struct lpfc_bsg_event *evt;
3497 struct lpfc_bsg_iocb *iocb;
3498 struct lpfc_bsg_mbox *mbox;
e2aed29f 3499 struct lpfc_bsg_menlo *menlo;
3b5dd52a
JS
3500 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
3501 struct bsg_job_data *dd_data;
3502 unsigned long flags;
3503
3504 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3505 dd_data = (struct bsg_job_data *)job->dd_data;
3506 /* timeout and completion crossed paths if no dd_data */
3507 if (!dd_data) {
3508 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3509 return 0;
4cc0e56e
JS
3510 }
3511
3512 switch (dd_data->type) {
3513 case TYPE_IOCB:
3514 iocb = &dd_data->context_un.iocb;
3515 cmdiocb = iocb->cmdiocbq;
3516 /* hint to completion handler that the job timed out */
3517 job->reply->result = -EAGAIN;
3518 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3519 /* this will call our completion handler */
3520 spin_lock_irq(&phba->hbalock);
f1c3b0fc 3521 lpfc_sli_issue_abort_iotag(phba, pring, cmdiocb);
4cc0e56e
JS
3522 spin_unlock_irq(&phba->hbalock);
3523 break;
3524 case TYPE_EVT:
3525 evt = dd_data->context_un.evt;
3526 /* this event has no job anymore */
3527 evt->set_job = NULL;
3528 job->dd_data = NULL;
3529 job->reply->reply_payload_rcv_len = 0;
3530 /* Return -EAGAIN which is our way of signallying the
3531 * app to retry.
3532 */
3533 job->reply->result = -EAGAIN;
3534 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3535 job->job_done(job);
3536 break;
3b5dd52a
JS
3537 case TYPE_MBOX:
3538 mbox = &dd_data->context_un.mbox;
3539 /* this mbox has no job anymore */
3540 mbox->set_job = NULL;
3541 job->dd_data = NULL;
3542 job->reply->reply_payload_rcv_len = 0;
3543 job->reply->result = -EAGAIN;
7a470277 3544 /* the mbox completion handler can now be run */
3b5dd52a
JS
3545 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3546 job->job_done(job);
3547 break;
e2aed29f
JS
3548 case TYPE_MENLO:
3549 menlo = &dd_data->context_un.menlo;
3550 cmdiocb = menlo->cmdiocbq;
3551 /* hint to completion handler that the job timed out */
3552 job->reply->result = -EAGAIN;
3553 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3554 /* this will call our completion handler */
3555 spin_lock_irq(&phba->hbalock);
3556 lpfc_sli_issue_abort_iotag(phba, pring, cmdiocb);
3557 spin_unlock_irq(&phba->hbalock);
3558 break;
4cc0e56e
JS
3559 default:
3560 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3561 break;
3562 }
f1c3b0fc 3563
4cc0e56e
JS
3564 /* scsi transport fc fc_bsg_job_timeout expects a zero return code,
3565 * otherwise an error message will be displayed on the console
3566 * so always return success (zero)
3567 */
f1c3b0fc
JS
3568 return 0;
3569}
This page took 0.493475 seconds and 5 git commands to generate.