blob: 3330d7951b427cd61594e8e1fc4a6984a48772aa [file] [log] [blame]
James Smartf1c3b0f2009-07-19 10:01:32 -04001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
3 * Fibre Channel Host Bus Adapters. *
James Smart4fede782010-01-26 23:08:55 -05004 * Copyright (C) 2009-2010 Emulex. All rights reserved. *
James Smartf1c3b0f2009-07-19 10:01:32 -04005 * 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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
James Smart277e76f2010-02-18 11:07:15 -050025#include <linux/delay.h>
James Smartf1c3b0f2009-07-19 10:01:32 -040026
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>
James Smart6a9c52c2009-10-02 15:16:51 -040031#include <scsi/fc/fc_fs.h>
James Smartf1c3b0f2009-07-19 10:01:32 -040032
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"
James Smart4fede782010-01-26 23:08:55 -050038#include "lpfc_bsg.h"
James Smartf1c3b0f2009-07-19 10:01:32 -040039#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
James Smart4cc0e562010-01-26 23:09:48 -050047struct 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
James Smart3b5dd522010-01-26 23:10:15 -050079struct lpfc_bsg_mbox {
80 LPFC_MBOXQ_t *pmboxq;
81 MAILBOX_t *mb;
James Smart7a470272010-03-15 11:25:20 -040082 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 */
James Smartc7495932010-04-06 15:05:28 -040087 uint32_t outExtWLen; /* from app */
James Smart3b5dd522010-01-26 23:10:15 -050088
89 /* job waiting for this mbox command to finish */
90 struct fc_bsg_job *set_job;
91};
92
James Smarte2aed292010-02-26 14:15:00 -050093#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
James Smart4cc0e562010-01-26 23:09:48 -0500104#define TYPE_EVT 1
105#define TYPE_IOCB 2
James Smart3b5dd522010-01-26 23:10:15 -0500106#define TYPE_MBOX 3
James Smarte2aed292010-02-26 14:15:00 -0500107#define TYPE_MENLO 4
James Smart4cc0e562010-01-26 23:09:48 -0500108struct bsg_job_data {
109 uint32_t type;
110 union {
111 struct lpfc_bsg_event *evt;
112 struct lpfc_bsg_iocb iocb;
James Smart3b5dd522010-01-26 23:10:15 -0500113 struct lpfc_bsg_mbox mbox;
James Smarte2aed292010-02-26 14:15:00 -0500114 struct lpfc_bsg_menlo menlo;
James Smart4cc0e562010-01-26 23:09:48 -0500115 } 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
James Smart3b5dd522010-01-26 23:10:15 -0500126#define BUF_SZ_4K 4096
James Smart4cc0e562010-01-26 23:09:48 -0500127#define SLI_CT_ELX_LOOPBACK 0x10
128
129enum ELX_LOOPBACK_CMD {
130 ELX_LOOPBACK_XRI_SETUP,
131 ELX_LOOPBACK_DATA,
132};
133
James Smart3b5dd522010-01-26 23:10:15 -0500134#define ELX_LOOPBACK_HEADER_SZ \
135 (size_t)(&((struct lpfc_sli_ct_request *)NULL)->un)
136
James Smart4cc0e562010-01-26 23:09:48 -0500137struct lpfc_dmabufext {
138 struct lpfc_dmabuf dma;
139 uint32_t size;
140 uint32_t flag;
141};
142
James Smartf1c3b0f2009-07-19 10:01:32 -0400143/**
James Smart4cc0e562010-01-26 23:09:48 -0500144 * 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{
James Smart4cc0e562010-01-26 23:09:48 -0500165 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);
James Smartbe858b62010-12-15 17:57:20 -0500175 dd_data = cmdiocbq->context2;
James Smart4cc0e562010-01-26 23:09:48 -0500176 if (!dd_data) {
177 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smartbe858b62010-12-15 17:57:20 -0500178 lpfc_sli_release_iocbq(phba, cmdiocbq);
James Smart4cc0e562010-01-26 23:09:48 -0500179 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
James Smart4cc0e562010-01-26 23:09:48 -0500186 bmp = iocb->bmp;
James Smart4cc0e562010-01-26 23:09:48 -0500187 rsp = &rspiocbq->iocb;
James Smartbe858b62010-12-15 17:57:20 -0500188 ndlp = cmdiocbq->context1;
James Smart4cc0e562010-01-26 23:09:48 -0500189
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);
James Smart4cc0e562010-01-26 23:09:48 -0500215 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
227/**
228 * lpfc_bsg_send_mgmt_cmd - send a CT command from a bsg request
James Smartf1c3b0f2009-07-19 10:01:32 -0400229 * @job: fc_bsg_job to handle
James Smart3b5dd522010-01-26 23:10:15 -0500230 **/
James Smartf1c3b0f2009-07-19 10:01:32 -0400231static int
James Smart4cc0e562010-01-26 23:09:48 -0500232lpfc_bsg_send_mgmt_cmd(struct fc_bsg_job *job)
James Smartf1c3b0f2009-07-19 10:01:32 -0400233{
James Smartf1c3b0f2009-07-19 10:01:32 -0400234 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;
James Smartf1c3b0f2009-07-19 10:01:32 -0400241 IOCB_t *cmd;
James Smartf1c3b0f2009-07-19 10:01:32 -0400242 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;
James Smart4cc0e562010-01-26 23:09:48 -0500248 struct bsg_job_data *dd_data;
249 uint32_t creg_val;
James Smartf1c3b0f2009-07-19 10:01:32 -0400250 int rc = 0;
James Smartd439d282010-09-29 11:18:45 -0400251 int iocb_stat;
James Smartf1c3b0f2009-07-19 10:01:32 -0400252
253 /* in case no data is transferred */
254 job->reply->reply_payload_rcv_len = 0;
255
James Smart4cc0e562010-01-26 23:09:48 -0500256 /* 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
James Smartf1c3b0f2009-07-19 10:01:32 -0400265 if (!lpfc_nlp_get(ndlp)) {
James Smart4cc0e562010-01-26 23:09:48 -0500266 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;
James Smartf1c3b0f2009-07-19 10:01:32 -0400274 }
275
276 if (ndlp->nlp_flag & NLP_ELS_SND_MASK) {
277 rc = -ENODEV;
James Smart4cc0e562010-01-26 23:09:48 -0500278 goto free_bmp;
James Smartf1c3b0f2009-07-19 10:01:32 -0400279 }
280
James Smartf1c3b0f2009-07-19 10:01:32 -0400281 cmdiocbq = lpfc_sli_get_iocbq(phba);
282 if (!cmdiocbq) {
283 rc = -ENOMEM;
James Smart4cc0e562010-01-26 23:09:48 -0500284 goto free_bmp;
James Smartf1c3b0f2009-07-19 10:01:32 -0400285 }
James Smartf1c3b0f2009-07-19 10:01:32 -0400286
James Smart4cc0e562010-01-26 23:09:48 -0500287 cmd = &cmdiocbq->iocb;
James Smartf1c3b0f2009-07-19 10:01:32 -0400288 bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
289 if (!bmp->virt) {
290 rc = -ENOMEM;
James Smartbe858b62010-12-15 17:57:20 -0500291 goto free_cmdiocbq;
James Smartf1c3b0f2009-07-19 10:01:32 -0400292 }
James Smartf1c3b0f2009-07-19 10:01:32 -0400293
294 INIT_LIST_HEAD(&bmp->list);
295 bpl = (struct ulp_bde64 *) bmp->virt;
James Smartf1c3b0f2009-07-19 10:01:32 -0400296 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;
James Smart6a9c52c2009-10-02 15:16:51 -0400329 cmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
330 cmd->un.genreq64.w5.hcsw.Type = FC_TYPE_CT;
James Smartf1c3b0f2009-07-19 10:01:32 -0400331 cmd->ulpBdeCount = 1;
332 cmd->ulpLe = 1;
333 cmd->ulpClass = CLASS3;
334 cmd->ulpContext = ndlp->nlp_rpi;
335 cmd->ulpOwner = OWN_CHIP;
336 cmdiocbq->vport = phba->pport;
James Smart4cc0e562010-01-26 23:09:48 -0500337 cmdiocbq->context3 = bmp;
James Smartf1c3b0f2009-07-19 10:01:32 -0400338 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
James Smartf1c3b0f2009-07-19 10:01:32 -0400339 timeout = phba->fc_ratov * 2;
James Smart4cc0e562010-01-26 23:09:48 -0500340 cmd->ulpTimeout = timeout;
James Smartf1c3b0f2009-07-19 10:01:32 -0400341
James Smart4cc0e562010-01-26 23:09:48 -0500342 cmdiocbq->iocb_cmpl = lpfc_bsg_send_mgmt_cmd_cmp;
James Smartbe858b62010-12-15 17:57:20 -0500343 cmdiocbq->context1 = ndlp;
344 cmdiocbq->context2 = dd_data;
James Smart4cc0e562010-01-26 23:09:48 -0500345 dd_data->type = TYPE_IOCB;
346 dd_data->context_un.iocb.cmdiocbq = cmdiocbq;
James Smart4cc0e562010-01-26 23:09:48 -0500347 dd_data->context_un.iocb.set_job = job;
348 dd_data->context_un.iocb.bmp = bmp;
James Smartf1c3b0f2009-07-19 10:01:32 -0400349
James Smart4cc0e562010-01-26 23:09:48 -0500350 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
351 creg_val = readl(phba->HCregaddr);
352 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
353 writel(creg_val, phba->HCregaddr);
354 readl(phba->HCregaddr); /* flush */
James Smartf1c3b0f2009-07-19 10:01:32 -0400355 }
356
James Smartd439d282010-09-29 11:18:45 -0400357 iocb_stat = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq, 0);
358 if (iocb_stat == IOCB_SUCCESS)
James Smart4cc0e562010-01-26 23:09:48 -0500359 return 0; /* done for now */
James Smartd439d282010-09-29 11:18:45 -0400360 else if (iocb_stat == IOCB_BUSY)
361 rc = -EAGAIN;
James Smart2a9bf3d2010-06-07 15:24:45 -0400362 else
James Smartd439d282010-09-29 11:18:45 -0400363 rc = -EIO;
James Smart2a9bf3d2010-06-07 15:24:45 -0400364
James Smartf1c3b0f2009-07-19 10:01:32 -0400365
James Smart4cc0e562010-01-26 23:09:48 -0500366 /* iocb failed so cleanup */
367 pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
368 job->request_payload.sg_cnt, DMA_TO_DEVICE);
369 pci_unmap_sg(phba->pcidev, job->reply_payload.sg_list,
370 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
James Smartf1c3b0f2009-07-19 10:01:32 -0400371
James Smartf1c3b0f2009-07-19 10:01:32 -0400372 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
James Smart4cc0e562010-01-26 23:09:48 -0500373
James Smartf1c3b0f2009-07-19 10:01:32 -0400374free_cmdiocbq:
375 lpfc_sli_release_iocbq(phba, cmdiocbq);
James Smart4cc0e562010-01-26 23:09:48 -0500376free_bmp:
377 kfree(bmp);
378free_ndlp:
James Smartf1c3b0f2009-07-19 10:01:32 -0400379 lpfc_nlp_put(ndlp);
James Smart4cc0e562010-01-26 23:09:48 -0500380no_ndlp:
381 kfree(dd_data);
382no_dd_data:
James Smartf1c3b0f2009-07-19 10:01:32 -0400383 /* make error code available to userspace */
384 job->reply->result = rc;
James Smart4cc0e562010-01-26 23:09:48 -0500385 job->dd_data = NULL;
386 return rc;
387}
388
389/**
390 * lpfc_bsg_rport_els_cmp - lpfc_bsg_rport_els's completion handler
391 * @phba: Pointer to HBA context object.
392 * @cmdiocbq: Pointer to command iocb.
393 * @rspiocbq: Pointer to response iocb.
394 *
395 * This function is the completion handler for iocbs issued using
396 * lpfc_bsg_rport_els_cmp function. This function is called by the
397 * ring event handler function without any lock held. This function
398 * can be called from both worker thread context and interrupt
399 * context. This function also can be called from other thread which
400 * cleans up the SLI layer objects.
James Smart3b5dd522010-01-26 23:10:15 -0500401 * This function copies the contents of the response iocb to the
James Smart4cc0e562010-01-26 23:09:48 -0500402 * response iocb memory object provided by the caller of
403 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
404 * sleeps for the iocb completion.
405 **/
406static void
407lpfc_bsg_rport_els_cmp(struct lpfc_hba *phba,
408 struct lpfc_iocbq *cmdiocbq,
409 struct lpfc_iocbq *rspiocbq)
410{
411 struct bsg_job_data *dd_data;
412 struct fc_bsg_job *job;
413 IOCB_t *rsp;
414 struct lpfc_nodelist *ndlp;
415 struct lpfc_dmabuf *pbuflist = NULL;
416 struct fc_bsg_ctels_reply *els_reply;
417 uint8_t *rjt_data;
418 unsigned long flags;
419 int rc = 0;
420
421 spin_lock_irqsave(&phba->ct_ev_lock, flags);
422 dd_data = cmdiocbq->context1;
423 /* normal completion and timeout crossed paths, already done */
424 if (!dd_data) {
Jiri Slaby67221a42010-03-16 16:23:58 +0100425 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smart4cc0e562010-01-26 23:09:48 -0500426 return;
427 }
428
429 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
430 if (cmdiocbq->context2 && rspiocbq)
431 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
432 &rspiocbq->iocb, sizeof(IOCB_t));
433
434 job = dd_data->context_un.iocb.set_job;
435 cmdiocbq = dd_data->context_un.iocb.cmdiocbq;
436 rspiocbq = dd_data->context_un.iocb.rspiocbq;
437 rsp = &rspiocbq->iocb;
438 ndlp = dd_data->context_un.iocb.ndlp;
439
440 pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
441 job->request_payload.sg_cnt, DMA_TO_DEVICE);
442 pci_unmap_sg(phba->pcidev, job->reply_payload.sg_list,
443 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
444
445 if (job->reply->result == -EAGAIN)
446 rc = -EAGAIN;
447 else if (rsp->ulpStatus == IOSTAT_SUCCESS)
448 job->reply->reply_payload_rcv_len =
449 rsp->un.elsreq64.bdl.bdeSize;
450 else if (rsp->ulpStatus == IOSTAT_LS_RJT) {
451 job->reply->reply_payload_rcv_len =
452 sizeof(struct fc_bsg_ctels_reply);
453 /* LS_RJT data returned in word 4 */
454 rjt_data = (uint8_t *)&rsp->un.ulpWord[4];
455 els_reply = &job->reply->reply_data.ctels_reply;
456 els_reply->status = FC_CTELS_STATUS_REJECT;
457 els_reply->rjt_data.action = rjt_data[3];
458 els_reply->rjt_data.reason_code = rjt_data[2];
459 els_reply->rjt_data.reason_explanation = rjt_data[1];
460 els_reply->rjt_data.vendor_unique = rjt_data[0];
461 } else
462 rc = -EIO;
463
464 pbuflist = (struct lpfc_dmabuf *) cmdiocbq->context3;
465 lpfc_mbuf_free(phba, pbuflist->virt, pbuflist->phys);
466 lpfc_sli_release_iocbq(phba, rspiocbq);
467 lpfc_sli_release_iocbq(phba, cmdiocbq);
468 lpfc_nlp_put(ndlp);
469 kfree(dd_data);
470 /* make error code available to userspace */
471 job->reply->result = rc;
472 job->dd_data = NULL;
James Smartf1c3b0f2009-07-19 10:01:32 -0400473 /* complete the job back to userspace */
474 job->job_done(job);
James Smart4cc0e562010-01-26 23:09:48 -0500475 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
476 return;
James Smartf1c3b0f2009-07-19 10:01:32 -0400477}
478
479/**
480 * lpfc_bsg_rport_els - send an ELS command from a bsg request
481 * @job: fc_bsg_job to handle
James Smart3b5dd522010-01-26 23:10:15 -0500482 **/
James Smartf1c3b0f2009-07-19 10:01:32 -0400483static int
484lpfc_bsg_rport_els(struct fc_bsg_job *job)
485{
486 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
487 struct lpfc_hba *phba = vport->phba;
488 struct lpfc_rport_data *rdata = job->rport->dd_data;
489 struct lpfc_nodelist *ndlp = rdata->pnode;
James Smartf1c3b0f2009-07-19 10:01:32 -0400490 uint32_t elscmd;
491 uint32_t cmdsize;
492 uint32_t rspsize;
493 struct lpfc_iocbq *rspiocbq;
494 struct lpfc_iocbq *cmdiocbq;
495 IOCB_t *rsp;
496 uint16_t rpi = 0;
497 struct lpfc_dmabuf *pcmd;
498 struct lpfc_dmabuf *prsp;
499 struct lpfc_dmabuf *pbuflist = NULL;
500 struct ulp_bde64 *bpl;
James Smartf1c3b0f2009-07-19 10:01:32 -0400501 int request_nseg;
502 int reply_nseg;
503 struct scatterlist *sgel = NULL;
504 int numbde;
505 dma_addr_t busaddr;
James Smart4cc0e562010-01-26 23:09:48 -0500506 struct bsg_job_data *dd_data;
507 uint32_t creg_val;
James Smartf1c3b0f2009-07-19 10:01:32 -0400508 int rc = 0;
509
510 /* in case no data is transferred */
511 job->reply->reply_payload_rcv_len = 0;
512
James Smart4cc0e562010-01-26 23:09:48 -0500513 /* allocate our bsg tracking structure */
514 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
515 if (!dd_data) {
516 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
517 "2735 Failed allocation of dd_data\n");
518 rc = -ENOMEM;
519 goto no_dd_data;
520 }
521
James Smartf1c3b0f2009-07-19 10:01:32 -0400522 if (!lpfc_nlp_get(ndlp)) {
523 rc = -ENODEV;
James Smart4cc0e562010-01-26 23:09:48 -0500524 goto free_dd_data;
James Smartf1c3b0f2009-07-19 10:01:32 -0400525 }
526
527 elscmd = job->request->rqst_data.r_els.els_code;
528 cmdsize = job->request_payload.payload_len;
529 rspsize = job->reply_payload.payload_len;
530 rspiocbq = lpfc_sli_get_iocbq(phba);
531 if (!rspiocbq) {
532 lpfc_nlp_put(ndlp);
533 rc = -ENOMEM;
James Smart4cc0e562010-01-26 23:09:48 -0500534 goto free_dd_data;
James Smartf1c3b0f2009-07-19 10:01:32 -0400535 }
536
537 rsp = &rspiocbq->iocb;
538 rpi = ndlp->nlp_rpi;
539
James Smart4cc0e562010-01-26 23:09:48 -0500540 cmdiocbq = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp,
James Smartf1c3b0f2009-07-19 10:01:32 -0400541 ndlp->nlp_DID, elscmd);
James Smartf1c3b0f2009-07-19 10:01:32 -0400542 if (!cmdiocbq) {
James Smart4cc0e562010-01-26 23:09:48 -0500543 rc = -EIO;
544 goto free_rspiocbq;
James Smartf1c3b0f2009-07-19 10:01:32 -0400545 }
546
James Smart4cc0e562010-01-26 23:09:48 -0500547 /* prep els iocb set context1 to the ndlp, context2 to the command
James Smart3b5dd522010-01-26 23:10:15 -0500548 * dmabuf, context3 holds the data dmabuf
549 */
James Smartf1c3b0f2009-07-19 10:01:32 -0400550 pcmd = (struct lpfc_dmabuf *) cmdiocbq->context2;
551 prsp = (struct lpfc_dmabuf *) pcmd->list.next;
James Smartf1c3b0f2009-07-19 10:01:32 -0400552 lpfc_mbuf_free(phba, pcmd->virt, pcmd->phys);
553 kfree(pcmd);
554 lpfc_mbuf_free(phba, prsp->virt, prsp->phys);
555 kfree(prsp);
556 cmdiocbq->context2 = NULL;
557
558 pbuflist = (struct lpfc_dmabuf *) cmdiocbq->context3;
559 bpl = (struct ulp_bde64 *) pbuflist->virt;
560
561 request_nseg = pci_map_sg(phba->pcidev, job->request_payload.sg_list,
562 job->request_payload.sg_cnt, DMA_TO_DEVICE);
James Smartf1c3b0f2009-07-19 10:01:32 -0400563 for_each_sg(job->request_payload.sg_list, sgel, request_nseg, numbde) {
564 busaddr = sg_dma_address(sgel);
565 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
566 bpl->tus.f.bdeSize = sg_dma_len(sgel);
567 bpl->tus.w = cpu_to_le32(bpl->tus.w);
568 bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
569 bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
570 bpl++;
571 }
572
573 reply_nseg = pci_map_sg(phba->pcidev, job->reply_payload.sg_list,
574 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
575 for_each_sg(job->reply_payload.sg_list, sgel, reply_nseg, numbde) {
576 busaddr = sg_dma_address(sgel);
577 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
578 bpl->tus.f.bdeSize = sg_dma_len(sgel);
579 bpl->tus.w = cpu_to_le32(bpl->tus.w);
580 bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
581 bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
582 bpl++;
583 }
James Smartf1c3b0f2009-07-19 10:01:32 -0400584 cmdiocbq->iocb.un.elsreq64.bdl.bdeSize =
585 (request_nseg + reply_nseg) * sizeof(struct ulp_bde64);
586 cmdiocbq->iocb.ulpContext = rpi;
587 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
588 cmdiocbq->context1 = NULL;
589 cmdiocbq->context2 = NULL;
590
James Smart4cc0e562010-01-26 23:09:48 -0500591 cmdiocbq->iocb_cmpl = lpfc_bsg_rport_els_cmp;
592 cmdiocbq->context1 = dd_data;
593 cmdiocbq->context2 = rspiocbq;
594 dd_data->type = TYPE_IOCB;
595 dd_data->context_un.iocb.cmdiocbq = cmdiocbq;
596 dd_data->context_un.iocb.rspiocbq = rspiocbq;
597 dd_data->context_un.iocb.set_job = job;
598 dd_data->context_un.iocb.bmp = NULL;;
599 dd_data->context_un.iocb.ndlp = ndlp;
James Smartf1c3b0f2009-07-19 10:01:32 -0400600
James Smart4cc0e562010-01-26 23:09:48 -0500601 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
602 creg_val = readl(phba->HCregaddr);
603 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
604 writel(creg_val, phba->HCregaddr);
605 readl(phba->HCregaddr); /* flush */
James Smartf1c3b0f2009-07-19 10:01:32 -0400606 }
James Smart4cc0e562010-01-26 23:09:48 -0500607 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq, 0);
608 lpfc_nlp_put(ndlp);
609 if (rc == IOCB_SUCCESS)
610 return 0; /* done for now */
James Smart2a9bf3d2010-06-07 15:24:45 -0400611 else if (rc == IOCB_BUSY)
James Smartd439d282010-09-29 11:18:45 -0400612 rc = -EAGAIN;
James Smart2a9bf3d2010-06-07 15:24:45 -0400613 else
James Smartd439d282010-09-29 11:18:45 -0400614 rc = -EIO;
James Smartf1c3b0f2009-07-19 10:01:32 -0400615
James Smart4cc0e562010-01-26 23:09:48 -0500616 pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
617 job->request_payload.sg_cnt, DMA_TO_DEVICE);
618 pci_unmap_sg(phba->pcidev, job->reply_payload.sg_list,
619 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
James Smartf1c3b0f2009-07-19 10:01:32 -0400620
James Smart4cc0e562010-01-26 23:09:48 -0500621 lpfc_mbuf_free(phba, pbuflist->virt, pbuflist->phys);
James Smartf1c3b0f2009-07-19 10:01:32 -0400622
James Smart4cc0e562010-01-26 23:09:48 -0500623 lpfc_sli_release_iocbq(phba, cmdiocbq);
James Smartf1c3b0f2009-07-19 10:01:32 -0400624
James Smart4cc0e562010-01-26 23:09:48 -0500625free_rspiocbq:
James Smartf1c3b0f2009-07-19 10:01:32 -0400626 lpfc_sli_release_iocbq(phba, rspiocbq);
627
James Smart4cc0e562010-01-26 23:09:48 -0500628free_dd_data:
629 kfree(dd_data);
630
631no_dd_data:
James Smartf1c3b0f2009-07-19 10:01:32 -0400632 /* make error code available to userspace */
633 job->reply->result = rc;
James Smart4cc0e562010-01-26 23:09:48 -0500634 job->dd_data = NULL;
635 return rc;
James Smartf1c3b0f2009-07-19 10:01:32 -0400636}
637
James Smart3b5dd522010-01-26 23:10:15 -0500638/**
639 * lpfc_bsg_event_free - frees an allocated event structure
640 * @kref: Pointer to a kref.
641 *
642 * Called from kref_put. Back cast the kref into an event structure address.
643 * Free any events to get, delete associated nodes, free any events to see,
644 * free any data then free the event itself.
645 **/
James Smartf1c3b0f2009-07-19 10:01:32 -0400646static void
James Smart4cc0e562010-01-26 23:09:48 -0500647lpfc_bsg_event_free(struct kref *kref)
James Smartf1c3b0f2009-07-19 10:01:32 -0400648{
James Smart4cc0e562010-01-26 23:09:48 -0500649 struct lpfc_bsg_event *evt = container_of(kref, struct lpfc_bsg_event,
650 kref);
James Smartf1c3b0f2009-07-19 10:01:32 -0400651 struct event_data *ed;
652
653 list_del(&evt->node);
654
655 while (!list_empty(&evt->events_to_get)) {
656 ed = list_entry(evt->events_to_get.next, typeof(*ed), node);
657 list_del(&ed->node);
658 kfree(ed->data);
659 kfree(ed);
660 }
661
662 while (!list_empty(&evt->events_to_see)) {
663 ed = list_entry(evt->events_to_see.next, typeof(*ed), node);
664 list_del(&ed->node);
665 kfree(ed->data);
666 kfree(ed);
667 }
668
669 kfree(evt);
670}
671
James Smart3b5dd522010-01-26 23:10:15 -0500672/**
673 * lpfc_bsg_event_ref - increments the kref for an event
674 * @evt: Pointer to an event structure.
675 **/
James Smartf1c3b0f2009-07-19 10:01:32 -0400676static inline void
James Smart4cc0e562010-01-26 23:09:48 -0500677lpfc_bsg_event_ref(struct lpfc_bsg_event *evt)
James Smartf1c3b0f2009-07-19 10:01:32 -0400678{
James Smart4cc0e562010-01-26 23:09:48 -0500679 kref_get(&evt->kref);
James Smartf1c3b0f2009-07-19 10:01:32 -0400680}
681
James Smart3b5dd522010-01-26 23:10:15 -0500682/**
683 * lpfc_bsg_event_unref - Uses kref_put to free an event structure
684 * @evt: Pointer to an event structure.
685 **/
James Smartf1c3b0f2009-07-19 10:01:32 -0400686static inline void
James Smart4cc0e562010-01-26 23:09:48 -0500687lpfc_bsg_event_unref(struct lpfc_bsg_event *evt)
James Smartf1c3b0f2009-07-19 10:01:32 -0400688{
James Smart4cc0e562010-01-26 23:09:48 -0500689 kref_put(&evt->kref, lpfc_bsg_event_free);
James Smartf1c3b0f2009-07-19 10:01:32 -0400690}
691
James Smart3b5dd522010-01-26 23:10:15 -0500692/**
693 * lpfc_bsg_event_new - allocate and initialize a event structure
694 * @ev_mask: Mask of events.
695 * @ev_reg_id: Event reg id.
696 * @ev_req_id: Event request id.
697 **/
James Smart4cc0e562010-01-26 23:09:48 -0500698static struct lpfc_bsg_event *
699lpfc_bsg_event_new(uint32_t ev_mask, int ev_reg_id, uint32_t ev_req_id)
700{
701 struct lpfc_bsg_event *evt = kzalloc(sizeof(*evt), GFP_KERNEL);
James Smartf1c3b0f2009-07-19 10:01:32 -0400702
James Smart4cc0e562010-01-26 23:09:48 -0500703 if (!evt)
704 return NULL;
705
706 INIT_LIST_HEAD(&evt->events_to_get);
707 INIT_LIST_HEAD(&evt->events_to_see);
708 evt->type_mask = ev_mask;
709 evt->req_id = ev_req_id;
710 evt->reg_id = ev_reg_id;
711 evt->wait_time_stamp = jiffies;
712 init_waitqueue_head(&evt->wq);
713 kref_init(&evt->kref);
714 return evt;
715}
716
James Smart3b5dd522010-01-26 23:10:15 -0500717/**
718 * diag_cmd_data_free - Frees an lpfc dma buffer extension
719 * @phba: Pointer to HBA context object.
720 * @mlist: Pointer to an lpfc dma buffer extension.
721 **/
James Smart4cc0e562010-01-26 23:09:48 -0500722static int
James Smart3b5dd522010-01-26 23:10:15 -0500723diag_cmd_data_free(struct lpfc_hba *phba, struct lpfc_dmabufext *mlist)
James Smart4cc0e562010-01-26 23:09:48 -0500724{
725 struct lpfc_dmabufext *mlast;
726 struct pci_dev *pcidev;
727 struct list_head head, *curr, *next;
728
729 if ((!mlist) || (!lpfc_is_link_up(phba) &&
730 (phba->link_flag & LS_LOOPBACK_MODE))) {
731 return 0;
732 }
733
734 pcidev = phba->pcidev;
735 list_add_tail(&head, &mlist->dma.list);
736
737 list_for_each_safe(curr, next, &head) {
738 mlast = list_entry(curr, struct lpfc_dmabufext , dma.list);
739 if (mlast->dma.virt)
740 dma_free_coherent(&pcidev->dev,
741 mlast->size,
742 mlast->dma.virt,
743 mlast->dma.phys);
744 kfree(mlast);
745 }
746 return 0;
747}
James Smartf1c3b0f2009-07-19 10:01:32 -0400748
749/**
750 * lpfc_bsg_ct_unsol_event - process an unsolicited CT command
751 * @phba:
752 * @pring:
753 * @piocbq:
754 *
755 * This function is called when an unsolicited CT command is received. It
James Smart4cc0e562010-01-26 23:09:48 -0500756 * forwards the event to any processes registered to receive CT events.
James Smart3b5dd522010-01-26 23:10:15 -0500757 **/
James Smart4fede782010-01-26 23:08:55 -0500758int
James Smartf1c3b0f2009-07-19 10:01:32 -0400759lpfc_bsg_ct_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
760 struct lpfc_iocbq *piocbq)
761{
762 uint32_t evt_req_id = 0;
763 uint32_t cmd;
764 uint32_t len;
765 struct lpfc_dmabuf *dmabuf = NULL;
James Smart4cc0e562010-01-26 23:09:48 -0500766 struct lpfc_bsg_event *evt;
James Smartf1c3b0f2009-07-19 10:01:32 -0400767 struct event_data *evt_dat = NULL;
768 struct lpfc_iocbq *iocbq;
769 size_t offset = 0;
770 struct list_head head;
771 struct ulp_bde64 *bde;
772 dma_addr_t dma_addr;
773 int i;
774 struct lpfc_dmabuf *bdeBuf1 = piocbq->context2;
775 struct lpfc_dmabuf *bdeBuf2 = piocbq->context3;
776 struct lpfc_hbq_entry *hbqe;
777 struct lpfc_sli_ct_request *ct_req;
James Smart4cc0e562010-01-26 23:09:48 -0500778 struct fc_bsg_job *job = NULL;
James Smart4fede782010-01-26 23:08:55 -0500779 unsigned long flags;
James Smart4cc0e562010-01-26 23:09:48 -0500780 int size = 0;
James Smartf1c3b0f2009-07-19 10:01:32 -0400781
782 INIT_LIST_HEAD(&head);
783 list_add_tail(&head, &piocbq->list);
784
785 if (piocbq->iocb.ulpBdeCount == 0 ||
786 piocbq->iocb.un.cont64[0].tus.f.bdeSize == 0)
787 goto error_ct_unsol_exit;
788
James Smart4cc0e562010-01-26 23:09:48 -0500789 if (phba->link_state == LPFC_HBA_ERROR ||
790 (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)))
791 goto error_ct_unsol_exit;
792
James Smartf1c3b0f2009-07-19 10:01:32 -0400793 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
794 dmabuf = bdeBuf1;
795 else {
796 dma_addr = getPaddr(piocbq->iocb.un.cont64[0].addrHigh,
797 piocbq->iocb.un.cont64[0].addrLow);
798 dmabuf = lpfc_sli_ringpostbuf_get(phba, pring, dma_addr);
799 }
James Smart4cc0e562010-01-26 23:09:48 -0500800 if (dmabuf == NULL)
801 goto error_ct_unsol_exit;
James Smartf1c3b0f2009-07-19 10:01:32 -0400802 ct_req = (struct lpfc_sli_ct_request *)dmabuf->virt;
803 evt_req_id = ct_req->FsType;
804 cmd = ct_req->CommandResponse.bits.CmdRsp;
805 len = ct_req->CommandResponse.bits.Size;
806 if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
807 lpfc_sli_ringpostbuf_put(phba, pring, dmabuf);
808
James Smart4fede782010-01-26 23:08:55 -0500809 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -0400810 list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
James Smart4cc0e562010-01-26 23:09:48 -0500811 if (!(evt->type_mask & FC_REG_CT_EVENT) ||
812 evt->req_id != evt_req_id)
James Smartf1c3b0f2009-07-19 10:01:32 -0400813 continue;
814
James Smart4cc0e562010-01-26 23:09:48 -0500815 lpfc_bsg_event_ref(evt);
816 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -0400817 evt_dat = kzalloc(sizeof(*evt_dat), GFP_KERNEL);
James Smart4cc0e562010-01-26 23:09:48 -0500818 if (evt_dat == NULL) {
819 spin_lock_irqsave(&phba->ct_ev_lock, flags);
820 lpfc_bsg_event_unref(evt);
James Smartf1c3b0f2009-07-19 10:01:32 -0400821 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
822 "2614 Memory allocation failed for "
823 "CT event\n");
824 break;
825 }
826
James Smartf1c3b0f2009-07-19 10:01:32 -0400827 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
828 /* take accumulated byte count from the last iocbq */
829 iocbq = list_entry(head.prev, typeof(*iocbq), list);
830 evt_dat->len = iocbq->iocb.unsli3.rcvsli3.acc_len;
831 } else {
832 list_for_each_entry(iocbq, &head, list) {
833 for (i = 0; i < iocbq->iocb.ulpBdeCount; i++)
834 evt_dat->len +=
835 iocbq->iocb.un.cont64[i].tus.f.bdeSize;
836 }
837 }
838
839 evt_dat->data = kzalloc(evt_dat->len, GFP_KERNEL);
James Smart4cc0e562010-01-26 23:09:48 -0500840 if (evt_dat->data == NULL) {
James Smartf1c3b0f2009-07-19 10:01:32 -0400841 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
842 "2615 Memory allocation failed for "
843 "CT event data, size %d\n",
844 evt_dat->len);
845 kfree(evt_dat);
James Smart4fede782010-01-26 23:08:55 -0500846 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smart4cc0e562010-01-26 23:09:48 -0500847 lpfc_bsg_event_unref(evt);
James Smart4fede782010-01-26 23:08:55 -0500848 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -0400849 goto error_ct_unsol_exit;
850 }
851
852 list_for_each_entry(iocbq, &head, list) {
James Smart4cc0e562010-01-26 23:09:48 -0500853 size = 0;
James Smartf1c3b0f2009-07-19 10:01:32 -0400854 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
855 bdeBuf1 = iocbq->context2;
856 bdeBuf2 = iocbq->context3;
857 }
858 for (i = 0; i < iocbq->iocb.ulpBdeCount; i++) {
James Smartf1c3b0f2009-07-19 10:01:32 -0400859 if (phba->sli3_options &
860 LPFC_SLI3_HBQ_ENABLED) {
861 if (i == 0) {
862 hbqe = (struct lpfc_hbq_entry *)
863 &iocbq->iocb.un.ulpWord[0];
864 size = hbqe->bde.tus.f.bdeSize;
865 dmabuf = bdeBuf1;
866 } else if (i == 1) {
867 hbqe = (struct lpfc_hbq_entry *)
868 &iocbq->iocb.unsli3.
869 sli3Words[4];
870 size = hbqe->bde.tus.f.bdeSize;
871 dmabuf = bdeBuf2;
872 }
873 if ((offset + size) > evt_dat->len)
874 size = evt_dat->len - offset;
875 } else {
876 size = iocbq->iocb.un.cont64[i].
877 tus.f.bdeSize;
878 bde = &iocbq->iocb.un.cont64[i];
879 dma_addr = getPaddr(bde->addrHigh,
880 bde->addrLow);
881 dmabuf = lpfc_sli_ringpostbuf_get(phba,
882 pring, dma_addr);
883 }
884 if (!dmabuf) {
885 lpfc_printf_log(phba, KERN_ERR,
886 LOG_LIBDFC, "2616 No dmabuf "
887 "found for iocbq 0x%p\n",
888 iocbq);
889 kfree(evt_dat->data);
890 kfree(evt_dat);
James Smart4fede782010-01-26 23:08:55 -0500891 spin_lock_irqsave(&phba->ct_ev_lock,
892 flags);
James Smart4cc0e562010-01-26 23:09:48 -0500893 lpfc_bsg_event_unref(evt);
James Smart4fede782010-01-26 23:08:55 -0500894 spin_unlock_irqrestore(
895 &phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -0400896 goto error_ct_unsol_exit;
897 }
898 memcpy((char *)(evt_dat->data) + offset,
899 dmabuf->virt, size);
900 offset += size;
901 if (evt_req_id != SLI_CT_ELX_LOOPBACK &&
902 !(phba->sli3_options &
903 LPFC_SLI3_HBQ_ENABLED)) {
904 lpfc_sli_ringpostbuf_put(phba, pring,
905 dmabuf);
906 } else {
907 switch (cmd) {
James Smart4cc0e562010-01-26 23:09:48 -0500908 case ELX_LOOPBACK_DATA:
James Smart3b5dd522010-01-26 23:10:15 -0500909 diag_cmd_data_free(phba,
James Smart4cc0e562010-01-26 23:09:48 -0500910 (struct lpfc_dmabufext *)
911 dmabuf);
912 break;
James Smartf1c3b0f2009-07-19 10:01:32 -0400913 case ELX_LOOPBACK_XRI_SETUP:
James Smart4cc0e562010-01-26 23:09:48 -0500914 if ((phba->sli_rev ==
915 LPFC_SLI_REV2) ||
916 (phba->sli3_options &
917 LPFC_SLI3_HBQ_ENABLED
918 )) {
919 lpfc_in_buf_free(phba,
920 dmabuf);
921 } else {
James Smartf1c3b0f2009-07-19 10:01:32 -0400922 lpfc_post_buffer(phba,
923 pring,
924 1);
James Smart4cc0e562010-01-26 23:09:48 -0500925 }
James Smartf1c3b0f2009-07-19 10:01:32 -0400926 break;
927 default:
928 if (!(phba->sli3_options &
929 LPFC_SLI3_HBQ_ENABLED))
930 lpfc_post_buffer(phba,
931 pring,
932 1);
933 break;
934 }
935 }
936 }
937 }
938
James Smart4fede782010-01-26 23:08:55 -0500939 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -0400940 if (phba->sli_rev == LPFC_SLI_REV4) {
941 evt_dat->immed_dat = phba->ctx_idx;
942 phba->ctx_idx = (phba->ctx_idx + 1) % 64;
James Smart589a52d2010-07-14 15:30:54 -0400943 /* Provide warning for over-run of the ct_ctx array */
944 if (phba->ct_ctx[evt_dat->immed_dat].flags &
945 UNSOL_VALID)
946 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
947 "2717 CT context array entry "
948 "[%d] over-run: oxid:x%x, "
949 "sid:x%x\n", phba->ctx_idx,
950 phba->ct_ctx[
951 evt_dat->immed_dat].oxid,
952 phba->ct_ctx[
953 evt_dat->immed_dat].SID);
James Smartf1c3b0f2009-07-19 10:01:32 -0400954 phba->ct_ctx[evt_dat->immed_dat].oxid =
955 piocbq->iocb.ulpContext;
956 phba->ct_ctx[evt_dat->immed_dat].SID =
957 piocbq->iocb.un.rcvels.remoteID;
James Smart589a52d2010-07-14 15:30:54 -0400958 phba->ct_ctx[evt_dat->immed_dat].flags = UNSOL_VALID;
James Smartf1c3b0f2009-07-19 10:01:32 -0400959 } else
960 evt_dat->immed_dat = piocbq->iocb.ulpContext;
961
962 evt_dat->type = FC_REG_CT_EVENT;
963 list_add(&evt_dat->node, &evt->events_to_see);
James Smart4cc0e562010-01-26 23:09:48 -0500964 if (evt_req_id == SLI_CT_ELX_LOOPBACK) {
965 wake_up_interruptible(&evt->wq);
966 lpfc_bsg_event_unref(evt);
James Smartf1c3b0f2009-07-19 10:01:32 -0400967 break;
James Smart4cc0e562010-01-26 23:09:48 -0500968 }
969
970 list_move(evt->events_to_see.prev, &evt->events_to_get);
971 lpfc_bsg_event_unref(evt);
972
973 job = evt->set_job;
974 evt->set_job = NULL;
975 if (job) {
976 job->reply->reply_payload_rcv_len = size;
977 /* make error code available to userspace */
978 job->reply->result = 0;
979 job->dd_data = NULL;
980 /* complete the job back to userspace */
981 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
982 job->job_done(job);
983 spin_lock_irqsave(&phba->ct_ev_lock, flags);
984 }
James Smartf1c3b0f2009-07-19 10:01:32 -0400985 }
James Smart4fede782010-01-26 23:08:55 -0500986 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -0400987
988error_ct_unsol_exit:
989 if (!list_empty(&head))
990 list_del(&head);
James Smart4cc0e562010-01-26 23:09:48 -0500991 if (evt_req_id == SLI_CT_ELX_LOOPBACK)
992 return 0;
James Smart4fede782010-01-26 23:08:55 -0500993 return 1;
James Smartf1c3b0f2009-07-19 10:01:32 -0400994}
995
996/**
James Smart4cc0e562010-01-26 23:09:48 -0500997 * lpfc_bsg_hba_set_event - process a SET_EVENT bsg vendor command
James Smartf1c3b0f2009-07-19 10:01:32 -0400998 * @job: SET_EVENT fc_bsg_job
James Smart3b5dd522010-01-26 23:10:15 -0500999 **/
James Smartf1c3b0f2009-07-19 10:01:32 -04001000static int
James Smart4cc0e562010-01-26 23:09:48 -05001001lpfc_bsg_hba_set_event(struct fc_bsg_job *job)
James Smartf1c3b0f2009-07-19 10:01:32 -04001002{
1003 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
1004 struct lpfc_hba *phba = vport->phba;
1005 struct set_ct_event *event_req;
James Smart4cc0e562010-01-26 23:09:48 -05001006 struct lpfc_bsg_event *evt;
James Smartf1c3b0f2009-07-19 10:01:32 -04001007 int rc = 0;
James Smart4cc0e562010-01-26 23:09:48 -05001008 struct bsg_job_data *dd_data = NULL;
1009 uint32_t ev_mask;
1010 unsigned long flags;
James Smartf1c3b0f2009-07-19 10:01:32 -04001011
1012 if (job->request_len <
1013 sizeof(struct fc_bsg_request) + sizeof(struct set_ct_event)) {
1014 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1015 "2612 Received SET_CT_EVENT below minimum "
1016 "size\n");
James Smart4cc0e562010-01-26 23:09:48 -05001017 rc = -EINVAL;
1018 goto job_error;
1019 }
1020
1021 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
1022 if (dd_data == NULL) {
1023 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1024 "2734 Failed allocation of dd_data\n");
1025 rc = -ENOMEM;
1026 goto job_error;
James Smartf1c3b0f2009-07-19 10:01:32 -04001027 }
1028
1029 event_req = (struct set_ct_event *)
1030 job->request->rqst_data.h_vendor.vendor_cmd;
James Smart4cc0e562010-01-26 23:09:48 -05001031 ev_mask = ((uint32_t)(unsigned long)event_req->type_mask &
1032 FC_REG_EVENT_MASK);
James Smart4fede782010-01-26 23:08:55 -05001033 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -04001034 list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
1035 if (evt->reg_id == event_req->ev_reg_id) {
James Smart4cc0e562010-01-26 23:09:48 -05001036 lpfc_bsg_event_ref(evt);
James Smartf1c3b0f2009-07-19 10:01:32 -04001037 evt->wait_time_stamp = jiffies;
1038 break;
1039 }
1040 }
James Smart4fede782010-01-26 23:08:55 -05001041 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -04001042
1043 if (&evt->node == &phba->ct_ev_waiters) {
1044 /* no event waiting struct yet - first call */
James Smart4cc0e562010-01-26 23:09:48 -05001045 evt = lpfc_bsg_event_new(ev_mask, event_req->ev_reg_id,
James Smartf1c3b0f2009-07-19 10:01:32 -04001046 event_req->ev_req_id);
1047 if (!evt) {
1048 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1049 "2617 Failed allocation of event "
1050 "waiter\n");
James Smart4cc0e562010-01-26 23:09:48 -05001051 rc = -ENOMEM;
1052 goto job_error;
James Smartf1c3b0f2009-07-19 10:01:32 -04001053 }
1054
James Smart4fede782010-01-26 23:08:55 -05001055 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -04001056 list_add(&evt->node, &phba->ct_ev_waiters);
James Smart4cc0e562010-01-26 23:09:48 -05001057 lpfc_bsg_event_ref(evt);
1058 evt->wait_time_stamp = jiffies;
James Smart4fede782010-01-26 23:08:55 -05001059 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -04001060 }
1061
James Smart4fede782010-01-26 23:08:55 -05001062 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smart4cc0e562010-01-26 23:09:48 -05001063 evt->waiting = 1;
1064 dd_data->type = TYPE_EVT;
1065 dd_data->context_un.evt = evt;
1066 evt->set_job = job; /* for unsolicited command */
1067 job->dd_data = dd_data; /* for fc transport timeout callback*/
James Smart4fede782010-01-26 23:08:55 -05001068 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smart4cc0e562010-01-26 23:09:48 -05001069 return 0; /* call job done later */
James Smartf1c3b0f2009-07-19 10:01:32 -04001070
James Smart4cc0e562010-01-26 23:09:48 -05001071job_error:
1072 if (dd_data != NULL)
1073 kfree(dd_data);
James Smartf1c3b0f2009-07-19 10:01:32 -04001074
James Smart4cc0e562010-01-26 23:09:48 -05001075 job->dd_data = NULL;
1076 return rc;
James Smartf1c3b0f2009-07-19 10:01:32 -04001077}
1078
1079/**
James Smart4cc0e562010-01-26 23:09:48 -05001080 * lpfc_bsg_hba_get_event - process a GET_EVENT bsg vendor command
James Smartf1c3b0f2009-07-19 10:01:32 -04001081 * @job: GET_EVENT fc_bsg_job
James Smart3b5dd522010-01-26 23:10:15 -05001082 **/
James Smartf1c3b0f2009-07-19 10:01:32 -04001083static int
James Smart4cc0e562010-01-26 23:09:48 -05001084lpfc_bsg_hba_get_event(struct fc_bsg_job *job)
James Smartf1c3b0f2009-07-19 10:01:32 -04001085{
1086 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
1087 struct lpfc_hba *phba = vport->phba;
1088 struct get_ct_event *event_req;
1089 struct get_ct_event_reply *event_reply;
James Smart4cc0e562010-01-26 23:09:48 -05001090 struct lpfc_bsg_event *evt;
James Smartf1c3b0f2009-07-19 10:01:32 -04001091 struct event_data *evt_dat = NULL;
James Smart4fede782010-01-26 23:08:55 -05001092 unsigned long flags;
James Smart4cc0e562010-01-26 23:09:48 -05001093 uint32_t rc = 0;
James Smartf1c3b0f2009-07-19 10:01:32 -04001094
1095 if (job->request_len <
1096 sizeof(struct fc_bsg_request) + sizeof(struct get_ct_event)) {
1097 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1098 "2613 Received GET_CT_EVENT request below "
1099 "minimum size\n");
James Smart4cc0e562010-01-26 23:09:48 -05001100 rc = -EINVAL;
1101 goto job_error;
James Smartf1c3b0f2009-07-19 10:01:32 -04001102 }
1103
1104 event_req = (struct get_ct_event *)
1105 job->request->rqst_data.h_vendor.vendor_cmd;
1106
1107 event_reply = (struct get_ct_event_reply *)
1108 job->reply->reply_data.vendor_reply.vendor_rsp;
James Smart4fede782010-01-26 23:08:55 -05001109 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -04001110 list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
1111 if (evt->reg_id == event_req->ev_reg_id) {
1112 if (list_empty(&evt->events_to_get))
1113 break;
James Smart4cc0e562010-01-26 23:09:48 -05001114 lpfc_bsg_event_ref(evt);
James Smartf1c3b0f2009-07-19 10:01:32 -04001115 evt->wait_time_stamp = jiffies;
1116 evt_dat = list_entry(evt->events_to_get.prev,
1117 struct event_data, node);
1118 list_del(&evt_dat->node);
1119 break;
1120 }
1121 }
James Smart4fede782010-01-26 23:08:55 -05001122 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -04001123
James Smart4cc0e562010-01-26 23:09:48 -05001124 /* The app may continue to ask for event data until it gets
1125 * an error indicating that there isn't anymore
1126 */
1127 if (evt_dat == NULL) {
James Smartf1c3b0f2009-07-19 10:01:32 -04001128 job->reply->reply_payload_rcv_len = 0;
1129 rc = -ENOENT;
James Smart4cc0e562010-01-26 23:09:48 -05001130 goto job_error;
James Smartf1c3b0f2009-07-19 10:01:32 -04001131 }
1132
James Smart4cc0e562010-01-26 23:09:48 -05001133 if (evt_dat->len > job->request_payload.payload_len) {
1134 evt_dat->len = job->request_payload.payload_len;
1135 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1136 "2618 Truncated event data at %d "
1137 "bytes\n",
1138 job->request_payload.payload_len);
James Smartf1c3b0f2009-07-19 10:01:32 -04001139 }
1140
James Smart4cc0e562010-01-26 23:09:48 -05001141 event_reply->type = evt_dat->type;
James Smartf1c3b0f2009-07-19 10:01:32 -04001142 event_reply->immed_data = evt_dat->immed_dat;
James Smartf1c3b0f2009-07-19 10:01:32 -04001143 if (evt_dat->len > 0)
1144 job->reply->reply_payload_rcv_len =
James Smart4cc0e562010-01-26 23:09:48 -05001145 sg_copy_from_buffer(job->request_payload.sg_list,
1146 job->request_payload.sg_cnt,
James Smartf1c3b0f2009-07-19 10:01:32 -04001147 evt_dat->data, evt_dat->len);
1148 else
1149 job->reply->reply_payload_rcv_len = 0;
James Smartf1c3b0f2009-07-19 10:01:32 -04001150
James Smart4cc0e562010-01-26 23:09:48 -05001151 if (evt_dat) {
James Smartf1c3b0f2009-07-19 10:01:32 -04001152 kfree(evt_dat->data);
James Smart4cc0e562010-01-26 23:09:48 -05001153 kfree(evt_dat);
1154 }
1155
James Smart4fede782010-01-26 23:08:55 -05001156 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smart4cc0e562010-01-26 23:09:48 -05001157 lpfc_bsg_event_unref(evt);
James Smart4fede782010-01-26 23:08:55 -05001158 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smart4cc0e562010-01-26 23:09:48 -05001159 job->dd_data = NULL;
1160 job->reply->result = 0;
James Smartf1c3b0f2009-07-19 10:01:32 -04001161 job->job_done(job);
James Smart4cc0e562010-01-26 23:09:48 -05001162 return 0;
James Smartf1c3b0f2009-07-19 10:01:32 -04001163
James Smart4cc0e562010-01-26 23:09:48 -05001164job_error:
1165 job->dd_data = NULL;
1166 job->reply->result = rc;
James Smartf1c3b0f2009-07-19 10:01:32 -04001167 return rc;
1168}
1169
1170/**
James Smart3b5dd522010-01-26 23:10:15 -05001171 * lpfc_issue_ct_rsp_cmp - lpfc_issue_ct_rsp's completion handler
1172 * @phba: Pointer to HBA context object.
1173 * @cmdiocbq: Pointer to command iocb.
1174 * @rspiocbq: Pointer to response iocb.
1175 *
1176 * This function is the completion handler for iocbs issued using
1177 * lpfc_issue_ct_rsp_cmp function. This function is called by the
1178 * ring event handler function without any lock held. This function
1179 * can be called from both worker thread context and interrupt
1180 * context. This function also can be called from other thread which
1181 * cleans up the SLI layer objects.
1182 * This function copy the contents of the response iocb to the
1183 * response iocb memory object provided by the caller of
1184 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
1185 * sleeps for the iocb completion.
1186 **/
1187static void
1188lpfc_issue_ct_rsp_cmp(struct lpfc_hba *phba,
1189 struct lpfc_iocbq *cmdiocbq,
1190 struct lpfc_iocbq *rspiocbq)
1191{
1192 struct bsg_job_data *dd_data;
1193 struct fc_bsg_job *job;
1194 IOCB_t *rsp;
1195 struct lpfc_dmabuf *bmp;
1196 struct lpfc_nodelist *ndlp;
1197 unsigned long flags;
1198 int rc = 0;
1199
1200 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smartbe858b62010-12-15 17:57:20 -05001201 dd_data = cmdiocbq->context2;
James Smart3b5dd522010-01-26 23:10:15 -05001202 /* normal completion and timeout crossed paths, already done */
1203 if (!dd_data) {
Jiri Slaby67221a42010-03-16 16:23:58 +01001204 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smart3b5dd522010-01-26 23:10:15 -05001205 return;
1206 }
1207
1208 job = dd_data->context_un.iocb.set_job;
1209 bmp = dd_data->context_un.iocb.bmp;
1210 rsp = &rspiocbq->iocb;
1211 ndlp = dd_data->context_un.iocb.ndlp;
1212
1213 pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
1214 job->request_payload.sg_cnt, DMA_TO_DEVICE);
1215
1216 if (rsp->ulpStatus) {
1217 if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
1218 switch (rsp->un.ulpWord[4] & 0xff) {
1219 case IOERR_SEQUENCE_TIMEOUT:
1220 rc = -ETIMEDOUT;
1221 break;
1222 case IOERR_INVALID_RPI:
1223 rc = -EFAULT;
1224 break;
1225 default:
1226 rc = -EACCES;
1227 break;
1228 }
1229 } else
1230 rc = -EACCES;
1231 } else
1232 job->reply->reply_payload_rcv_len =
1233 rsp->un.genreq64.bdl.bdeSize;
1234
1235 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
1236 lpfc_sli_release_iocbq(phba, cmdiocbq);
1237 lpfc_nlp_put(ndlp);
1238 kfree(bmp);
1239 kfree(dd_data);
1240 /* make error code available to userspace */
1241 job->reply->result = rc;
1242 job->dd_data = NULL;
1243 /* complete the job back to userspace */
1244 job->job_done(job);
1245 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1246 return;
1247}
1248
1249/**
1250 * lpfc_issue_ct_rsp - issue a ct response
1251 * @phba: Pointer to HBA context object.
1252 * @job: Pointer to the job object.
1253 * @tag: tag index value into the ports context exchange array.
1254 * @bmp: Pointer to a dma buffer descriptor.
1255 * @num_entry: Number of enties in the bde.
1256 **/
1257static int
1258lpfc_issue_ct_rsp(struct lpfc_hba *phba, struct fc_bsg_job *job, uint32_t tag,
1259 struct lpfc_dmabuf *bmp, int num_entry)
1260{
1261 IOCB_t *icmd;
1262 struct lpfc_iocbq *ctiocb = NULL;
1263 int rc = 0;
1264 struct lpfc_nodelist *ndlp = NULL;
1265 struct bsg_job_data *dd_data;
1266 uint32_t creg_val;
1267
1268 /* allocate our bsg tracking structure */
1269 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
1270 if (!dd_data) {
1271 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1272 "2736 Failed allocation of dd_data\n");
1273 rc = -ENOMEM;
1274 goto no_dd_data;
1275 }
1276
1277 /* Allocate buffer for command iocb */
1278 ctiocb = lpfc_sli_get_iocbq(phba);
1279 if (!ctiocb) {
James Smartd439d282010-09-29 11:18:45 -04001280 rc = -ENOMEM;
James Smart3b5dd522010-01-26 23:10:15 -05001281 goto no_ctiocb;
1282 }
1283
1284 icmd = &ctiocb->iocb;
1285 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
1286 icmd->un.xseq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
1287 icmd->un.xseq64.bdl.addrLow = putPaddrLow(bmp->phys);
1288 icmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
1289 icmd->un.xseq64.bdl.bdeSize = (num_entry * sizeof(struct ulp_bde64));
1290 icmd->un.xseq64.w5.hcsw.Fctl = (LS | LA);
1291 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
1292 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_SOL_CTL;
1293 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
1294
1295 /* Fill in rest of iocb */
1296 icmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;
1297 icmd->ulpBdeCount = 1;
1298 icmd->ulpLe = 1;
1299 icmd->ulpClass = CLASS3;
1300 if (phba->sli_rev == LPFC_SLI_REV4) {
1301 /* Do not issue unsol response if oxid not marked as valid */
1302 if (!(phba->ct_ctx[tag].flags & UNSOL_VALID)) {
1303 rc = IOCB_ERROR;
1304 goto issue_ct_rsp_exit;
1305 }
1306 icmd->ulpContext = phba->ct_ctx[tag].oxid;
1307 ndlp = lpfc_findnode_did(phba->pport, phba->ct_ctx[tag].SID);
1308 if (!ndlp) {
1309 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
1310 "2721 ndlp null for oxid %x SID %x\n",
1311 icmd->ulpContext,
1312 phba->ct_ctx[tag].SID);
1313 rc = IOCB_ERROR;
1314 goto issue_ct_rsp_exit;
1315 }
James Smart589a52d2010-07-14 15:30:54 -04001316
1317 /* Check if the ndlp is active */
1318 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
1319 rc = -IOCB_ERROR;
1320 goto issue_ct_rsp_exit;
1321 }
1322
1323 /* get a refernece count so the ndlp doesn't go away while
1324 * we respond
1325 */
1326 if (!lpfc_nlp_get(ndlp)) {
1327 rc = -IOCB_ERROR;
1328 goto issue_ct_rsp_exit;
1329 }
1330
James Smart3b5dd522010-01-26 23:10:15 -05001331 icmd->un.ulpWord[3] = ndlp->nlp_rpi;
1332 /* The exchange is done, mark the entry as invalid */
1333 phba->ct_ctx[tag].flags &= ~UNSOL_VALID;
1334 } else
1335 icmd->ulpContext = (ushort) tag;
1336
1337 icmd->ulpTimeout = phba->fc_ratov * 2;
1338
1339 /* Xmit CT response on exchange <xid> */
1340 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
1341 "2722 Xmit CT response on exchange x%x Data: x%x x%x\n",
1342 icmd->ulpContext, icmd->ulpIoTag, phba->link_state);
1343
1344 ctiocb->iocb_cmpl = NULL;
1345 ctiocb->iocb_flag |= LPFC_IO_LIBDFC;
1346 ctiocb->vport = phba->pport;
1347 ctiocb->context3 = bmp;
1348
1349 ctiocb->iocb_cmpl = lpfc_issue_ct_rsp_cmp;
James Smartbe858b62010-12-15 17:57:20 -05001350 ctiocb->context2 = dd_data;
1351 ctiocb->context1 = ndlp;
James Smart3b5dd522010-01-26 23:10:15 -05001352 dd_data->type = TYPE_IOCB;
1353 dd_data->context_un.iocb.cmdiocbq = ctiocb;
1354 dd_data->context_un.iocb.rspiocbq = NULL;
1355 dd_data->context_un.iocb.set_job = job;
1356 dd_data->context_un.iocb.bmp = bmp;
1357 dd_data->context_un.iocb.ndlp = ndlp;
1358
1359 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
1360 creg_val = readl(phba->HCregaddr);
1361 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
1362 writel(creg_val, phba->HCregaddr);
1363 readl(phba->HCregaddr); /* flush */
1364 }
1365
1366 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
1367
1368 if (rc == IOCB_SUCCESS)
1369 return 0; /* done for now */
1370
1371issue_ct_rsp_exit:
1372 lpfc_sli_release_iocbq(phba, ctiocb);
1373no_ctiocb:
1374 kfree(dd_data);
1375no_dd_data:
1376 return rc;
1377}
1378
1379/**
1380 * lpfc_bsg_send_mgmt_rsp - process a SEND_MGMT_RESP bsg vendor command
1381 * @job: SEND_MGMT_RESP fc_bsg_job
1382 **/
1383static int
1384lpfc_bsg_send_mgmt_rsp(struct fc_bsg_job *job)
1385{
1386 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
1387 struct lpfc_hba *phba = vport->phba;
1388 struct send_mgmt_resp *mgmt_resp = (struct send_mgmt_resp *)
1389 job->request->rqst_data.h_vendor.vendor_cmd;
1390 struct ulp_bde64 *bpl;
1391 struct lpfc_dmabuf *bmp = NULL;
1392 struct scatterlist *sgel = NULL;
1393 int request_nseg;
1394 int numbde;
1395 dma_addr_t busaddr;
1396 uint32_t tag = mgmt_resp->tag;
1397 unsigned long reqbfrcnt =
1398 (unsigned long)job->request_payload.payload_len;
1399 int rc = 0;
1400
1401 /* in case no data is transferred */
1402 job->reply->reply_payload_rcv_len = 0;
1403
1404 if (!reqbfrcnt || (reqbfrcnt > (80 * BUF_SZ_4K))) {
1405 rc = -ERANGE;
1406 goto send_mgmt_rsp_exit;
1407 }
1408
1409 bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
1410 if (!bmp) {
1411 rc = -ENOMEM;
1412 goto send_mgmt_rsp_exit;
1413 }
1414
1415 bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
1416 if (!bmp->virt) {
1417 rc = -ENOMEM;
1418 goto send_mgmt_rsp_free_bmp;
1419 }
1420
1421 INIT_LIST_HEAD(&bmp->list);
1422 bpl = (struct ulp_bde64 *) bmp->virt;
1423 request_nseg = pci_map_sg(phba->pcidev, job->request_payload.sg_list,
1424 job->request_payload.sg_cnt, DMA_TO_DEVICE);
1425 for_each_sg(job->request_payload.sg_list, sgel, request_nseg, numbde) {
1426 busaddr = sg_dma_address(sgel);
1427 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1428 bpl->tus.f.bdeSize = sg_dma_len(sgel);
1429 bpl->tus.w = cpu_to_le32(bpl->tus.w);
1430 bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
1431 bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
1432 bpl++;
1433 }
1434
1435 rc = lpfc_issue_ct_rsp(phba, job, tag, bmp, request_nseg);
1436
1437 if (rc == IOCB_SUCCESS)
1438 return 0; /* done for now */
1439
1440 /* TBD need to handle a timeout */
1441 pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
1442 job->request_payload.sg_cnt, DMA_TO_DEVICE);
1443 rc = -EACCES;
1444 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
1445
1446send_mgmt_rsp_free_bmp:
1447 kfree(bmp);
1448send_mgmt_rsp_exit:
1449 /* make error code available to userspace */
1450 job->reply->result = rc;
1451 job->dd_data = NULL;
1452 return rc;
1453}
1454
1455/**
1456 * lpfc_bsg_diag_mode - process a LPFC_BSG_VENDOR_DIAG_MODE bsg vendor command
1457 * @job: LPFC_BSG_VENDOR_DIAG_MODE
1458 *
1459 * This function is responsible for placing a port into diagnostic loopback
1460 * mode in order to perform a diagnostic loopback test.
1461 * All new scsi requests are blocked, a small delay is used to allow the
1462 * scsi requests to complete then the link is brought down. If the link is
1463 * is placed in loopback mode then scsi requests are again allowed
1464 * so the scsi mid-layer doesn't give up on the port.
1465 * All of this is done in-line.
1466 */
1467static int
1468lpfc_bsg_diag_mode(struct fc_bsg_job *job)
1469{
1470 struct Scsi_Host *shost = job->shost;
1471 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
1472 struct lpfc_hba *phba = vport->phba;
1473 struct diag_mode_set *loopback_mode;
1474 struct lpfc_sli *psli = &phba->sli;
1475 struct lpfc_sli_ring *pring = &psli->ring[LPFC_FCP_RING];
1476 uint32_t link_flags;
1477 uint32_t timeout;
1478 struct lpfc_vport **vports;
1479 LPFC_MBOXQ_t *pmboxq;
1480 int mbxstatus;
1481 int i = 0;
1482 int rc = 0;
1483
1484 /* no data to return just the return code */
1485 job->reply->reply_payload_rcv_len = 0;
1486
1487 if (job->request_len <
1488 sizeof(struct fc_bsg_request) + sizeof(struct diag_mode_set)) {
1489 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1490 "2738 Received DIAG MODE request below minimum "
1491 "size\n");
1492 rc = -EINVAL;
1493 goto job_error;
1494 }
1495
1496 loopback_mode = (struct diag_mode_set *)
1497 job->request->rqst_data.h_vendor.vendor_cmd;
1498 link_flags = loopback_mode->type;
James Smart515e0aa2010-09-29 11:19:00 -04001499 timeout = loopback_mode->timeout * 100;
James Smart3b5dd522010-01-26 23:10:15 -05001500
1501 if ((phba->link_state == LPFC_HBA_ERROR) ||
1502 (psli->sli_flag & LPFC_BLOCK_MGMT_IO) ||
1503 (!(psli->sli_flag & LPFC_SLI_ACTIVE))) {
1504 rc = -EACCES;
1505 goto job_error;
1506 }
1507
1508 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1509 if (!pmboxq) {
1510 rc = -ENOMEM;
1511 goto job_error;
1512 }
1513
1514 vports = lpfc_create_vport_work_array(phba);
1515 if (vports) {
1516 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
1517 shost = lpfc_shost_from_vport(vports[i]);
1518 scsi_block_requests(shost);
1519 }
1520
1521 lpfc_destroy_vport_work_array(phba, vports);
1522 } else {
1523 shost = lpfc_shost_from_vport(phba->pport);
1524 scsi_block_requests(shost);
1525 }
1526
1527 while (pring->txcmplq_cnt) {
1528 if (i++ > 500) /* wait up to 5 seconds */
1529 break;
1530
1531 msleep(10);
1532 }
1533
1534 memset((void *)pmboxq, 0, sizeof(LPFC_MBOXQ_t));
1535 pmboxq->u.mb.mbxCommand = MBX_DOWN_LINK;
1536 pmboxq->u.mb.mbxOwner = OWN_HOST;
1537
1538 mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
1539
1540 if ((mbxstatus == MBX_SUCCESS) && (pmboxq->u.mb.mbxStatus == 0)) {
1541 /* wait for link down before proceeding */
1542 i = 0;
1543 while (phba->link_state != LPFC_LINK_DOWN) {
1544 if (i++ > timeout) {
1545 rc = -ETIMEDOUT;
1546 goto loopback_mode_exit;
1547 }
1548
1549 msleep(10);
1550 }
1551
1552 memset((void *)pmboxq, 0, sizeof(LPFC_MBOXQ_t));
1553 if (link_flags == INTERNAL_LOOP_BACK)
1554 pmboxq->u.mb.un.varInitLnk.link_flags = FLAGS_LOCAL_LB;
1555 else
1556 pmboxq->u.mb.un.varInitLnk.link_flags =
1557 FLAGS_TOPOLOGY_MODE_LOOP;
1558
1559 pmboxq->u.mb.mbxCommand = MBX_INIT_LINK;
1560 pmboxq->u.mb.mbxOwner = OWN_HOST;
1561
1562 mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq,
1563 LPFC_MBOX_TMO);
1564
1565 if ((mbxstatus != MBX_SUCCESS) || (pmboxq->u.mb.mbxStatus))
1566 rc = -ENODEV;
1567 else {
1568 phba->link_flag |= LS_LOOPBACK_MODE;
1569 /* wait for the link attention interrupt */
1570 msleep(100);
1571
1572 i = 0;
1573 while (phba->link_state != LPFC_HBA_READY) {
1574 if (i++ > timeout) {
1575 rc = -ETIMEDOUT;
1576 break;
1577 }
1578
1579 msleep(10);
1580 }
1581 }
1582
1583 } else
1584 rc = -ENODEV;
1585
1586loopback_mode_exit:
1587 vports = lpfc_create_vport_work_array(phba);
1588 if (vports) {
1589 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
1590 shost = lpfc_shost_from_vport(vports[i]);
1591 scsi_unblock_requests(shost);
1592 }
1593 lpfc_destroy_vport_work_array(phba, vports);
1594 } else {
1595 shost = lpfc_shost_from_vport(phba->pport);
1596 scsi_unblock_requests(shost);
1597 }
1598
1599 /*
1600 * Let SLI layer release mboxq if mbox command completed after timeout.
1601 */
1602 if (mbxstatus != MBX_TIMEOUT)
1603 mempool_free(pmboxq, phba->mbox_mem_pool);
1604
1605job_error:
1606 /* make error code available to userspace */
1607 job->reply->result = rc;
1608 /* complete the job back to userspace if no error */
1609 if (rc == 0)
1610 job->job_done(job);
1611 return rc;
1612}
1613
1614/**
1615 * lpfcdiag_loop_self_reg - obtains a remote port login id
1616 * @phba: Pointer to HBA context object
1617 * @rpi: Pointer to a remote port login id
1618 *
1619 * This function obtains a remote port login id so the diag loopback test
1620 * can send and receive its own unsolicited CT command.
1621 **/
1622static int lpfcdiag_loop_self_reg(struct lpfc_hba *phba, uint16_t * rpi)
1623{
1624 LPFC_MBOXQ_t *mbox;
1625 struct lpfc_dmabuf *dmabuff;
1626 int status;
1627
1628 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1629 if (!mbox)
James Smartd439d282010-09-29 11:18:45 -04001630 return -ENOMEM;
James Smart3b5dd522010-01-26 23:10:15 -05001631
1632 status = lpfc_reg_rpi(phba, 0, phba->pport->fc_myDID,
1633 (uint8_t *)&phba->pport->fc_sparam, mbox, 0);
1634 if (status) {
1635 mempool_free(mbox, phba->mbox_mem_pool);
James Smartd439d282010-09-29 11:18:45 -04001636 return -ENOMEM;
James Smart3b5dd522010-01-26 23:10:15 -05001637 }
1638
1639 dmabuff = (struct lpfc_dmabuf *) mbox->context1;
1640 mbox->context1 = NULL;
James Smartd439d282010-09-29 11:18:45 -04001641 mbox->context2 = NULL;
James Smart3b5dd522010-01-26 23:10:15 -05001642 status = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
1643
1644 if ((status != MBX_SUCCESS) || (mbox->u.mb.mbxStatus)) {
1645 lpfc_mbuf_free(phba, dmabuff->virt, dmabuff->phys);
1646 kfree(dmabuff);
1647 if (status != MBX_TIMEOUT)
1648 mempool_free(mbox, phba->mbox_mem_pool);
James Smartd439d282010-09-29 11:18:45 -04001649 return -ENODEV;
James Smart3b5dd522010-01-26 23:10:15 -05001650 }
1651
1652 *rpi = mbox->u.mb.un.varWords[0];
1653
1654 lpfc_mbuf_free(phba, dmabuff->virt, dmabuff->phys);
1655 kfree(dmabuff);
1656 mempool_free(mbox, phba->mbox_mem_pool);
1657 return 0;
1658}
1659
1660/**
1661 * lpfcdiag_loop_self_unreg - unregs from the rpi
1662 * @phba: Pointer to HBA context object
1663 * @rpi: Remote port login id
1664 *
1665 * This function unregisters the rpi obtained in lpfcdiag_loop_self_reg
1666 **/
1667static int lpfcdiag_loop_self_unreg(struct lpfc_hba *phba, uint16_t rpi)
1668{
1669 LPFC_MBOXQ_t *mbox;
1670 int status;
1671
1672 /* Allocate mboxq structure */
1673 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1674 if (mbox == NULL)
James Smartd439d282010-09-29 11:18:45 -04001675 return -ENOMEM;
James Smart3b5dd522010-01-26 23:10:15 -05001676
1677 lpfc_unreg_login(phba, 0, rpi, mbox);
1678 status = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
1679
1680 if ((status != MBX_SUCCESS) || (mbox->u.mb.mbxStatus)) {
1681 if (status != MBX_TIMEOUT)
1682 mempool_free(mbox, phba->mbox_mem_pool);
James Smartd439d282010-09-29 11:18:45 -04001683 return -EIO;
James Smart3b5dd522010-01-26 23:10:15 -05001684 }
1685
1686 mempool_free(mbox, phba->mbox_mem_pool);
1687 return 0;
1688}
1689
1690/**
1691 * lpfcdiag_loop_get_xri - obtains the transmit and receive ids
1692 * @phba: Pointer to HBA context object
1693 * @rpi: Remote port login id
1694 * @txxri: Pointer to transmit exchange id
1695 * @rxxri: Pointer to response exchabge id
1696 *
1697 * This function obtains the transmit and receive ids required to send
1698 * an unsolicited ct command with a payload. A special lpfc FsType and CmdRsp
1699 * flags are used to the unsolicted response handler is able to process
1700 * the ct command sent on the same port.
1701 **/
1702static int lpfcdiag_loop_get_xri(struct lpfc_hba *phba, uint16_t rpi,
1703 uint16_t *txxri, uint16_t * rxxri)
1704{
1705 struct lpfc_bsg_event *evt;
1706 struct lpfc_iocbq *cmdiocbq, *rspiocbq;
1707 IOCB_t *cmd, *rsp;
1708 struct lpfc_dmabuf *dmabuf;
1709 struct ulp_bde64 *bpl = NULL;
1710 struct lpfc_sli_ct_request *ctreq = NULL;
1711 int ret_val = 0;
James Smartd439d282010-09-29 11:18:45 -04001712 int time_left;
James Smart515e0aa2010-09-29 11:19:00 -04001713 int iocb_stat = 0;
James Smart3b5dd522010-01-26 23:10:15 -05001714 unsigned long flags;
1715
1716 *txxri = 0;
1717 *rxxri = 0;
1718 evt = lpfc_bsg_event_new(FC_REG_CT_EVENT, current->pid,
1719 SLI_CT_ELX_LOOPBACK);
1720 if (!evt)
James Smartd439d282010-09-29 11:18:45 -04001721 return -ENOMEM;
James Smart3b5dd522010-01-26 23:10:15 -05001722
1723 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1724 list_add(&evt->node, &phba->ct_ev_waiters);
1725 lpfc_bsg_event_ref(evt);
1726 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1727
1728 cmdiocbq = lpfc_sli_get_iocbq(phba);
1729 rspiocbq = lpfc_sli_get_iocbq(phba);
1730
1731 dmabuf = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
1732 if (dmabuf) {
1733 dmabuf->virt = lpfc_mbuf_alloc(phba, 0, &dmabuf->phys);
James Smartc7495932010-04-06 15:05:28 -04001734 if (dmabuf->virt) {
1735 INIT_LIST_HEAD(&dmabuf->list);
1736 bpl = (struct ulp_bde64 *) dmabuf->virt;
1737 memset(bpl, 0, sizeof(*bpl));
1738 ctreq = (struct lpfc_sli_ct_request *)(bpl + 1);
1739 bpl->addrHigh =
1740 le32_to_cpu(putPaddrHigh(dmabuf->phys +
1741 sizeof(*bpl)));
1742 bpl->addrLow =
1743 le32_to_cpu(putPaddrLow(dmabuf->phys +
1744 sizeof(*bpl)));
1745 bpl->tus.f.bdeFlags = 0;
1746 bpl->tus.f.bdeSize = ELX_LOOPBACK_HEADER_SZ;
1747 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1748 }
James Smart3b5dd522010-01-26 23:10:15 -05001749 }
1750
1751 if (cmdiocbq == NULL || rspiocbq == NULL ||
James Smartc7495932010-04-06 15:05:28 -04001752 dmabuf == NULL || bpl == NULL || ctreq == NULL ||
1753 dmabuf->virt == NULL) {
James Smartd439d282010-09-29 11:18:45 -04001754 ret_val = -ENOMEM;
James Smart3b5dd522010-01-26 23:10:15 -05001755 goto err_get_xri_exit;
1756 }
1757
1758 cmd = &cmdiocbq->iocb;
1759 rsp = &rspiocbq->iocb;
1760
1761 memset(ctreq, 0, ELX_LOOPBACK_HEADER_SZ);
1762
1763 ctreq->RevisionId.bits.Revision = SLI_CT_REVISION;
1764 ctreq->RevisionId.bits.InId = 0;
1765 ctreq->FsType = SLI_CT_ELX_LOOPBACK;
1766 ctreq->FsSubType = 0;
1767 ctreq->CommandResponse.bits.CmdRsp = ELX_LOOPBACK_XRI_SETUP;
1768 ctreq->CommandResponse.bits.Size = 0;
1769
1770
1771 cmd->un.xseq64.bdl.addrHigh = putPaddrHigh(dmabuf->phys);
1772 cmd->un.xseq64.bdl.addrLow = putPaddrLow(dmabuf->phys);
1773 cmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
1774 cmd->un.xseq64.bdl.bdeSize = sizeof(*bpl);
1775
1776 cmd->un.xseq64.w5.hcsw.Fctl = LA;
1777 cmd->un.xseq64.w5.hcsw.Dfctl = 0;
1778 cmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
1779 cmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
1780
1781 cmd->ulpCommand = CMD_XMIT_SEQUENCE64_CR;
1782 cmd->ulpBdeCount = 1;
1783 cmd->ulpLe = 1;
1784 cmd->ulpClass = CLASS3;
1785 cmd->ulpContext = rpi;
1786
1787 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
1788 cmdiocbq->vport = phba->pport;
1789
James Smartd439d282010-09-29 11:18:45 -04001790 iocb_stat = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq,
James Smart3b5dd522010-01-26 23:10:15 -05001791 rspiocbq,
1792 (phba->fc_ratov * 2)
1793 + LPFC_DRVR_TIMEOUT);
James Smartd439d282010-09-29 11:18:45 -04001794 if (iocb_stat) {
1795 ret_val = -EIO;
James Smart3b5dd522010-01-26 23:10:15 -05001796 goto err_get_xri_exit;
James Smartd439d282010-09-29 11:18:45 -04001797 }
James Smart3b5dd522010-01-26 23:10:15 -05001798 *txxri = rsp->ulpContext;
1799
1800 evt->waiting = 1;
1801 evt->wait_time_stamp = jiffies;
James Smartd439d282010-09-29 11:18:45 -04001802 time_left = wait_event_interruptible_timeout(
James Smart3b5dd522010-01-26 23:10:15 -05001803 evt->wq, !list_empty(&evt->events_to_see),
1804 ((phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT) * HZ);
1805 if (list_empty(&evt->events_to_see))
James Smartd439d282010-09-29 11:18:45 -04001806 ret_val = (time_left) ? -EINTR : -ETIMEDOUT;
James Smart3b5dd522010-01-26 23:10:15 -05001807 else {
James Smart3b5dd522010-01-26 23:10:15 -05001808 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1809 list_move(evt->events_to_see.prev, &evt->events_to_get);
1810 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1811 *rxxri = (list_entry(evt->events_to_get.prev,
1812 typeof(struct event_data),
1813 node))->immed_dat;
1814 }
1815 evt->waiting = 0;
1816
1817err_get_xri_exit:
1818 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1819 lpfc_bsg_event_unref(evt); /* release ref */
1820 lpfc_bsg_event_unref(evt); /* delete */
1821 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1822
1823 if (dmabuf) {
1824 if (dmabuf->virt)
1825 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
1826 kfree(dmabuf);
1827 }
1828
James Smartd439d282010-09-29 11:18:45 -04001829 if (cmdiocbq && (iocb_stat != IOCB_TIMEDOUT))
James Smart3b5dd522010-01-26 23:10:15 -05001830 lpfc_sli_release_iocbq(phba, cmdiocbq);
1831 if (rspiocbq)
1832 lpfc_sli_release_iocbq(phba, rspiocbq);
1833 return ret_val;
1834}
1835
1836/**
1837 * diag_cmd_data_alloc - fills in a bde struct with dma buffers
1838 * @phba: Pointer to HBA context object
1839 * @bpl: Pointer to 64 bit bde structure
1840 * @size: Number of bytes to process
1841 * @nocopydata: Flag to copy user data into the allocated buffer
1842 *
1843 * This function allocates page size buffers and populates an lpfc_dmabufext.
1844 * If allowed the user data pointed to with indataptr is copied into the kernel
1845 * memory. The chained list of page size buffers is returned.
1846 **/
1847static struct lpfc_dmabufext *
1848diag_cmd_data_alloc(struct lpfc_hba *phba,
1849 struct ulp_bde64 *bpl, uint32_t size,
1850 int nocopydata)
1851{
1852 struct lpfc_dmabufext *mlist = NULL;
1853 struct lpfc_dmabufext *dmp;
1854 int cnt, offset = 0, i = 0;
1855 struct pci_dev *pcidev;
1856
1857 pcidev = phba->pcidev;
1858
1859 while (size) {
1860 /* We get chunks of 4K */
1861 if (size > BUF_SZ_4K)
1862 cnt = BUF_SZ_4K;
1863 else
1864 cnt = size;
1865
1866 /* allocate struct lpfc_dmabufext buffer header */
1867 dmp = kmalloc(sizeof(struct lpfc_dmabufext), GFP_KERNEL);
1868 if (!dmp)
1869 goto out;
1870
1871 INIT_LIST_HEAD(&dmp->dma.list);
1872
1873 /* Queue it to a linked list */
1874 if (mlist)
1875 list_add_tail(&dmp->dma.list, &mlist->dma.list);
1876 else
1877 mlist = dmp;
1878
1879 /* allocate buffer */
1880 dmp->dma.virt = dma_alloc_coherent(&pcidev->dev,
1881 cnt,
1882 &(dmp->dma.phys),
1883 GFP_KERNEL);
1884
1885 if (!dmp->dma.virt)
1886 goto out;
1887
1888 dmp->size = cnt;
1889
1890 if (nocopydata) {
1891 bpl->tus.f.bdeFlags = 0;
1892 pci_dma_sync_single_for_device(phba->pcidev,
1893 dmp->dma.phys, LPFC_BPL_SIZE, PCI_DMA_TODEVICE);
1894
1895 } else {
1896 memset((uint8_t *)dmp->dma.virt, 0, cnt);
1897 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1898 }
1899
1900 /* build buffer ptr list for IOCB */
1901 bpl->addrLow = le32_to_cpu(putPaddrLow(dmp->dma.phys));
1902 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dmp->dma.phys));
1903 bpl->tus.f.bdeSize = (ushort) cnt;
1904 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1905 bpl++;
1906
1907 i++;
1908 offset += cnt;
1909 size -= cnt;
1910 }
1911
1912 mlist->flag = i;
1913 return mlist;
1914out:
1915 diag_cmd_data_free(phba, mlist);
1916 return NULL;
1917}
1918
1919/**
1920 * lpfcdiag_loop_post_rxbufs - post the receive buffers for an unsol CT cmd
1921 * @phba: Pointer to HBA context object
1922 * @rxxri: Receive exchange id
1923 * @len: Number of data bytes
1924 *
1925 * This function allocates and posts a data buffer of sufficient size to recieve
1926 * an unsolicted CT command.
1927 **/
1928static int lpfcdiag_loop_post_rxbufs(struct lpfc_hba *phba, uint16_t rxxri,
1929 size_t len)
1930{
1931 struct lpfc_sli *psli = &phba->sli;
1932 struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
1933 struct lpfc_iocbq *cmdiocbq;
1934 IOCB_t *cmd = NULL;
1935 struct list_head head, *curr, *next;
1936 struct lpfc_dmabuf *rxbmp;
1937 struct lpfc_dmabuf *dmp;
1938 struct lpfc_dmabuf *mp[2] = {NULL, NULL};
1939 struct ulp_bde64 *rxbpl = NULL;
1940 uint32_t num_bde;
1941 struct lpfc_dmabufext *rxbuffer = NULL;
1942 int ret_val = 0;
James Smartd439d282010-09-29 11:18:45 -04001943 int iocb_stat;
James Smart3b5dd522010-01-26 23:10:15 -05001944 int i = 0;
1945
1946 cmdiocbq = lpfc_sli_get_iocbq(phba);
1947 rxbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
1948 if (rxbmp != NULL) {
1949 rxbmp->virt = lpfc_mbuf_alloc(phba, 0, &rxbmp->phys);
James Smartc7495932010-04-06 15:05:28 -04001950 if (rxbmp->virt) {
1951 INIT_LIST_HEAD(&rxbmp->list);
1952 rxbpl = (struct ulp_bde64 *) rxbmp->virt;
1953 rxbuffer = diag_cmd_data_alloc(phba, rxbpl, len, 0);
1954 }
James Smart3b5dd522010-01-26 23:10:15 -05001955 }
1956
1957 if (!cmdiocbq || !rxbmp || !rxbpl || !rxbuffer) {
James Smartd439d282010-09-29 11:18:45 -04001958 ret_val = -ENOMEM;
James Smart3b5dd522010-01-26 23:10:15 -05001959 goto err_post_rxbufs_exit;
1960 }
1961
1962 /* Queue buffers for the receive exchange */
1963 num_bde = (uint32_t)rxbuffer->flag;
1964 dmp = &rxbuffer->dma;
1965
1966 cmd = &cmdiocbq->iocb;
1967 i = 0;
1968
1969 INIT_LIST_HEAD(&head);
1970 list_add_tail(&head, &dmp->list);
1971 list_for_each_safe(curr, next, &head) {
1972 mp[i] = list_entry(curr, struct lpfc_dmabuf, list);
1973 list_del(curr);
1974
1975 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
1976 mp[i]->buffer_tag = lpfc_sli_get_buffer_tag(phba);
1977 cmd->un.quexri64cx.buff.bde.addrHigh =
1978 putPaddrHigh(mp[i]->phys);
1979 cmd->un.quexri64cx.buff.bde.addrLow =
1980 putPaddrLow(mp[i]->phys);
1981 cmd->un.quexri64cx.buff.bde.tus.f.bdeSize =
1982 ((struct lpfc_dmabufext *)mp[i])->size;
1983 cmd->un.quexri64cx.buff.buffer_tag = mp[i]->buffer_tag;
1984 cmd->ulpCommand = CMD_QUE_XRI64_CX;
1985 cmd->ulpPU = 0;
1986 cmd->ulpLe = 1;
1987 cmd->ulpBdeCount = 1;
1988 cmd->unsli3.que_xri64cx_ext_words.ebde_count = 0;
1989
1990 } else {
1991 cmd->un.cont64[i].addrHigh = putPaddrHigh(mp[i]->phys);
1992 cmd->un.cont64[i].addrLow = putPaddrLow(mp[i]->phys);
1993 cmd->un.cont64[i].tus.f.bdeSize =
1994 ((struct lpfc_dmabufext *)mp[i])->size;
1995 cmd->ulpBdeCount = ++i;
1996
1997 if ((--num_bde > 0) && (i < 2))
1998 continue;
1999
2000 cmd->ulpCommand = CMD_QUE_XRI_BUF64_CX;
2001 cmd->ulpLe = 1;
2002 }
2003
2004 cmd->ulpClass = CLASS3;
2005 cmd->ulpContext = rxxri;
2006
James Smartd439d282010-09-29 11:18:45 -04002007 iocb_stat = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq,
2008 0);
2009 if (iocb_stat == IOCB_ERROR) {
James Smart3b5dd522010-01-26 23:10:15 -05002010 diag_cmd_data_free(phba,
2011 (struct lpfc_dmabufext *)mp[0]);
2012 if (mp[1])
2013 diag_cmd_data_free(phba,
2014 (struct lpfc_dmabufext *)mp[1]);
2015 dmp = list_entry(next, struct lpfc_dmabuf, list);
James Smartd439d282010-09-29 11:18:45 -04002016 ret_val = -EIO;
James Smart3b5dd522010-01-26 23:10:15 -05002017 goto err_post_rxbufs_exit;
2018 }
2019
2020 lpfc_sli_ringpostbuf_put(phba, pring, mp[0]);
2021 if (mp[1]) {
2022 lpfc_sli_ringpostbuf_put(phba, pring, mp[1]);
2023 mp[1] = NULL;
2024 }
2025
2026 /* The iocb was freed by lpfc_sli_issue_iocb */
2027 cmdiocbq = lpfc_sli_get_iocbq(phba);
2028 if (!cmdiocbq) {
2029 dmp = list_entry(next, struct lpfc_dmabuf, list);
James Smartd439d282010-09-29 11:18:45 -04002030 ret_val = -EIO;
James Smart3b5dd522010-01-26 23:10:15 -05002031 goto err_post_rxbufs_exit;
2032 }
2033
2034 cmd = &cmdiocbq->iocb;
2035 i = 0;
2036 }
2037 list_del(&head);
2038
2039err_post_rxbufs_exit:
2040
2041 if (rxbmp) {
2042 if (rxbmp->virt)
2043 lpfc_mbuf_free(phba, rxbmp->virt, rxbmp->phys);
2044 kfree(rxbmp);
2045 }
2046
2047 if (cmdiocbq)
2048 lpfc_sli_release_iocbq(phba, cmdiocbq);
2049 return ret_val;
2050}
2051
2052/**
2053 * lpfc_bsg_diag_test - with a port in loopback issues a Ct cmd to itself
2054 * @job: LPFC_BSG_VENDOR_DIAG_TEST fc_bsg_job
2055 *
2056 * This function receives a user data buffer to be transmitted and received on
2057 * the same port, the link must be up and in loopback mode prior
2058 * to being called.
2059 * 1. A kernel buffer is allocated to copy the user data into.
2060 * 2. The port registers with "itself".
2061 * 3. The transmit and receive exchange ids are obtained.
2062 * 4. The receive exchange id is posted.
2063 * 5. A new els loopback event is created.
2064 * 6. The command and response iocbs are allocated.
2065 * 7. The cmd iocb FsType is set to elx loopback and the CmdRsp to looppback.
2066 *
2067 * This function is meant to be called n times while the port is in loopback
2068 * so it is the apps responsibility to issue a reset to take the port out
2069 * of loopback mode.
2070 **/
2071static int
2072lpfc_bsg_diag_test(struct fc_bsg_job *job)
2073{
2074 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
2075 struct lpfc_hba *phba = vport->phba;
2076 struct diag_mode_test *diag_mode;
2077 struct lpfc_bsg_event *evt;
2078 struct event_data *evdat;
2079 struct lpfc_sli *psli = &phba->sli;
2080 uint32_t size;
2081 uint32_t full_size;
2082 size_t segment_len = 0, segment_offset = 0, current_offset = 0;
2083 uint16_t rpi;
2084 struct lpfc_iocbq *cmdiocbq, *rspiocbq;
2085 IOCB_t *cmd, *rsp;
2086 struct lpfc_sli_ct_request *ctreq;
2087 struct lpfc_dmabuf *txbmp;
2088 struct ulp_bde64 *txbpl = NULL;
2089 struct lpfc_dmabufext *txbuffer = NULL;
2090 struct list_head head;
2091 struct lpfc_dmabuf *curr;
2092 uint16_t txxri, rxxri;
2093 uint32_t num_bde;
2094 uint8_t *ptr = NULL, *rx_databuf = NULL;
2095 int rc = 0;
James Smartd439d282010-09-29 11:18:45 -04002096 int time_left;
2097 int iocb_stat;
James Smart3b5dd522010-01-26 23:10:15 -05002098 unsigned long flags;
2099 void *dataout = NULL;
2100 uint32_t total_mem;
2101
2102 /* in case no data is returned return just the return code */
2103 job->reply->reply_payload_rcv_len = 0;
2104
2105 if (job->request_len <
2106 sizeof(struct fc_bsg_request) + sizeof(struct diag_mode_test)) {
2107 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2108 "2739 Received DIAG TEST request below minimum "
2109 "size\n");
2110 rc = -EINVAL;
2111 goto loopback_test_exit;
2112 }
2113
2114 if (job->request_payload.payload_len !=
2115 job->reply_payload.payload_len) {
2116 rc = -EINVAL;
2117 goto loopback_test_exit;
2118 }
2119
2120 diag_mode = (struct diag_mode_test *)
2121 job->request->rqst_data.h_vendor.vendor_cmd;
2122
2123 if ((phba->link_state == LPFC_HBA_ERROR) ||
2124 (psli->sli_flag & LPFC_BLOCK_MGMT_IO) ||
2125 (!(psli->sli_flag & LPFC_SLI_ACTIVE))) {
2126 rc = -EACCES;
2127 goto loopback_test_exit;
2128 }
2129
2130 if (!lpfc_is_link_up(phba) || !(phba->link_flag & LS_LOOPBACK_MODE)) {
2131 rc = -EACCES;
2132 goto loopback_test_exit;
2133 }
2134
2135 size = job->request_payload.payload_len;
2136 full_size = size + ELX_LOOPBACK_HEADER_SZ; /* plus the header */
2137
2138 if ((size == 0) || (size > 80 * BUF_SZ_4K)) {
2139 rc = -ERANGE;
2140 goto loopback_test_exit;
2141 }
2142
James Smart63e801c2010-11-20 23:14:19 -05002143 if (full_size >= BUF_SZ_4K) {
James Smart3b5dd522010-01-26 23:10:15 -05002144 /*
2145 * Allocate memory for ioctl data. If buffer is bigger than 64k,
2146 * then we allocate 64k and re-use that buffer over and over to
2147 * xfer the whole block. This is because Linux kernel has a
2148 * problem allocating more than 120k of kernel space memory. Saw
2149 * problem with GET_FCPTARGETMAPPING...
2150 */
2151 if (size <= (64 * 1024))
James Smart63e801c2010-11-20 23:14:19 -05002152 total_mem = full_size;
James Smart3b5dd522010-01-26 23:10:15 -05002153 else
2154 total_mem = 64 * 1024;
2155 } else
2156 /* Allocate memory for ioctl data */
2157 total_mem = BUF_SZ_4K;
2158
2159 dataout = kmalloc(total_mem, GFP_KERNEL);
2160 if (dataout == NULL) {
2161 rc = -ENOMEM;
2162 goto loopback_test_exit;
2163 }
2164
2165 ptr = dataout;
2166 ptr += ELX_LOOPBACK_HEADER_SZ;
2167 sg_copy_to_buffer(job->request_payload.sg_list,
2168 job->request_payload.sg_cnt,
2169 ptr, size);
2170
2171 rc = lpfcdiag_loop_self_reg(phba, &rpi);
James Smartd439d282010-09-29 11:18:45 -04002172 if (rc)
James Smart3b5dd522010-01-26 23:10:15 -05002173 goto loopback_test_exit;
James Smart3b5dd522010-01-26 23:10:15 -05002174
2175 rc = lpfcdiag_loop_get_xri(phba, rpi, &txxri, &rxxri);
2176 if (rc) {
2177 lpfcdiag_loop_self_unreg(phba, rpi);
James Smart3b5dd522010-01-26 23:10:15 -05002178 goto loopback_test_exit;
2179 }
2180
2181 rc = lpfcdiag_loop_post_rxbufs(phba, rxxri, full_size);
2182 if (rc) {
2183 lpfcdiag_loop_self_unreg(phba, rpi);
James Smart3b5dd522010-01-26 23:10:15 -05002184 goto loopback_test_exit;
2185 }
2186
2187 evt = lpfc_bsg_event_new(FC_REG_CT_EVENT, current->pid,
2188 SLI_CT_ELX_LOOPBACK);
2189 if (!evt) {
2190 lpfcdiag_loop_self_unreg(phba, rpi);
2191 rc = -ENOMEM;
2192 goto loopback_test_exit;
2193 }
2194
2195 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2196 list_add(&evt->node, &phba->ct_ev_waiters);
2197 lpfc_bsg_event_ref(evt);
2198 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2199
2200 cmdiocbq = lpfc_sli_get_iocbq(phba);
2201 rspiocbq = lpfc_sli_get_iocbq(phba);
2202 txbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2203
2204 if (txbmp) {
2205 txbmp->virt = lpfc_mbuf_alloc(phba, 0, &txbmp->phys);
James Smartc7495932010-04-06 15:05:28 -04002206 if (txbmp->virt) {
2207 INIT_LIST_HEAD(&txbmp->list);
2208 txbpl = (struct ulp_bde64 *) txbmp->virt;
James Smart3b5dd522010-01-26 23:10:15 -05002209 txbuffer = diag_cmd_data_alloc(phba,
2210 txbpl, full_size, 0);
James Smartc7495932010-04-06 15:05:28 -04002211 }
James Smart3b5dd522010-01-26 23:10:15 -05002212 }
2213
James Smartc7495932010-04-06 15:05:28 -04002214 if (!cmdiocbq || !rspiocbq || !txbmp || !txbpl || !txbuffer ||
2215 !txbmp->virt) {
James Smart3b5dd522010-01-26 23:10:15 -05002216 rc = -ENOMEM;
2217 goto err_loopback_test_exit;
2218 }
2219
2220 cmd = &cmdiocbq->iocb;
2221 rsp = &rspiocbq->iocb;
2222
2223 INIT_LIST_HEAD(&head);
2224 list_add_tail(&head, &txbuffer->dma.list);
2225 list_for_each_entry(curr, &head, list) {
2226 segment_len = ((struct lpfc_dmabufext *)curr)->size;
2227 if (current_offset == 0) {
2228 ctreq = curr->virt;
2229 memset(ctreq, 0, ELX_LOOPBACK_HEADER_SZ);
2230 ctreq->RevisionId.bits.Revision = SLI_CT_REVISION;
2231 ctreq->RevisionId.bits.InId = 0;
2232 ctreq->FsType = SLI_CT_ELX_LOOPBACK;
2233 ctreq->FsSubType = 0;
2234 ctreq->CommandResponse.bits.CmdRsp = ELX_LOOPBACK_DATA;
2235 ctreq->CommandResponse.bits.Size = size;
2236 segment_offset = ELX_LOOPBACK_HEADER_SZ;
2237 } else
2238 segment_offset = 0;
2239
2240 BUG_ON(segment_offset >= segment_len);
2241 memcpy(curr->virt + segment_offset,
2242 ptr + current_offset,
2243 segment_len - segment_offset);
2244
2245 current_offset += segment_len - segment_offset;
2246 BUG_ON(current_offset > size);
2247 }
2248 list_del(&head);
2249
2250 /* Build the XMIT_SEQUENCE iocb */
2251
2252 num_bde = (uint32_t)txbuffer->flag;
2253
2254 cmd->un.xseq64.bdl.addrHigh = putPaddrHigh(txbmp->phys);
2255 cmd->un.xseq64.bdl.addrLow = putPaddrLow(txbmp->phys);
2256 cmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
2257 cmd->un.xseq64.bdl.bdeSize = (num_bde * sizeof(struct ulp_bde64));
2258
2259 cmd->un.xseq64.w5.hcsw.Fctl = (LS | LA);
2260 cmd->un.xseq64.w5.hcsw.Dfctl = 0;
2261 cmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
2262 cmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
2263
2264 cmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;
2265 cmd->ulpBdeCount = 1;
2266 cmd->ulpLe = 1;
2267 cmd->ulpClass = CLASS3;
2268 cmd->ulpContext = txxri;
2269
2270 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
2271 cmdiocbq->vport = phba->pport;
2272
James Smartd439d282010-09-29 11:18:45 -04002273 iocb_stat = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq,
2274 rspiocbq, (phba->fc_ratov * 2) +
2275 LPFC_DRVR_TIMEOUT);
James Smart3b5dd522010-01-26 23:10:15 -05002276
James Smartd439d282010-09-29 11:18:45 -04002277 if ((iocb_stat != IOCB_SUCCESS) || (rsp->ulpStatus != IOCB_SUCCESS)) {
James Smart3b5dd522010-01-26 23:10:15 -05002278 rc = -EIO;
2279 goto err_loopback_test_exit;
2280 }
2281
2282 evt->waiting = 1;
James Smartd439d282010-09-29 11:18:45 -04002283 time_left = wait_event_interruptible_timeout(
James Smart3b5dd522010-01-26 23:10:15 -05002284 evt->wq, !list_empty(&evt->events_to_see),
2285 ((phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT) * HZ);
2286 evt->waiting = 0;
2287 if (list_empty(&evt->events_to_see))
James Smartd439d282010-09-29 11:18:45 -04002288 rc = (time_left) ? -EINTR : -ETIMEDOUT;
James Smart3b5dd522010-01-26 23:10:15 -05002289 else {
2290 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2291 list_move(evt->events_to_see.prev, &evt->events_to_get);
2292 evdat = list_entry(evt->events_to_get.prev,
2293 typeof(*evdat), node);
2294 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2295 rx_databuf = evdat->data;
2296 if (evdat->len != full_size) {
2297 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
2298 "1603 Loopback test did not receive expected "
2299 "data length. actual length 0x%x expected "
2300 "length 0x%x\n",
2301 evdat->len, full_size);
2302 rc = -EIO;
2303 } else if (rx_databuf == NULL)
2304 rc = -EIO;
2305 else {
2306 rc = IOCB_SUCCESS;
2307 /* skip over elx loopback header */
2308 rx_databuf += ELX_LOOPBACK_HEADER_SZ;
2309 job->reply->reply_payload_rcv_len =
2310 sg_copy_from_buffer(job->reply_payload.sg_list,
2311 job->reply_payload.sg_cnt,
2312 rx_databuf, size);
2313 job->reply->reply_payload_rcv_len = size;
2314 }
2315 }
2316
2317err_loopback_test_exit:
2318 lpfcdiag_loop_self_unreg(phba, rpi);
2319
2320 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2321 lpfc_bsg_event_unref(evt); /* release ref */
2322 lpfc_bsg_event_unref(evt); /* delete */
2323 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2324
2325 if (cmdiocbq != NULL)
2326 lpfc_sli_release_iocbq(phba, cmdiocbq);
2327
2328 if (rspiocbq != NULL)
2329 lpfc_sli_release_iocbq(phba, rspiocbq);
2330
2331 if (txbmp != NULL) {
2332 if (txbpl != NULL) {
2333 if (txbuffer != NULL)
2334 diag_cmd_data_free(phba, txbuffer);
2335 lpfc_mbuf_free(phba, txbmp->virt, txbmp->phys);
2336 }
2337 kfree(txbmp);
2338 }
2339
2340loopback_test_exit:
2341 kfree(dataout);
2342 /* make error code available to userspace */
2343 job->reply->result = rc;
2344 job->dd_data = NULL;
2345 /* complete the job back to userspace if no error */
2346 if (rc == 0)
2347 job->job_done(job);
2348 return rc;
2349}
2350
2351/**
2352 * lpfc_bsg_get_dfc_rev - process a GET_DFC_REV bsg vendor command
2353 * @job: GET_DFC_REV fc_bsg_job
2354 **/
2355static int
2356lpfc_bsg_get_dfc_rev(struct fc_bsg_job *job)
2357{
2358 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
2359 struct lpfc_hba *phba = vport->phba;
2360 struct get_mgmt_rev *event_req;
2361 struct get_mgmt_rev_reply *event_reply;
2362 int rc = 0;
2363
2364 if (job->request_len <
2365 sizeof(struct fc_bsg_request) + sizeof(struct get_mgmt_rev)) {
2366 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2367 "2740 Received GET_DFC_REV request below "
2368 "minimum size\n");
2369 rc = -EINVAL;
2370 goto job_error;
2371 }
2372
2373 event_req = (struct get_mgmt_rev *)
2374 job->request->rqst_data.h_vendor.vendor_cmd;
2375
2376 event_reply = (struct get_mgmt_rev_reply *)
2377 job->reply->reply_data.vendor_reply.vendor_rsp;
2378
2379 if (job->reply_len <
2380 sizeof(struct fc_bsg_request) + sizeof(struct get_mgmt_rev_reply)) {
2381 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2382 "2741 Received GET_DFC_REV reply below "
2383 "minimum size\n");
2384 rc = -EINVAL;
2385 goto job_error;
2386 }
2387
2388 event_reply->info.a_Major = MANAGEMENT_MAJOR_REV;
2389 event_reply->info.a_Minor = MANAGEMENT_MINOR_REV;
2390job_error:
2391 job->reply->result = rc;
2392 if (rc == 0)
2393 job->job_done(job);
2394 return rc;
2395}
2396
2397/**
2398 * lpfc_bsg_wake_mbox_wait - lpfc_bsg_issue_mbox mbox completion handler
2399 * @phba: Pointer to HBA context object.
2400 * @pmboxq: Pointer to mailbox command.
2401 *
2402 * This is completion handler function for mailbox commands issued from
2403 * lpfc_bsg_issue_mbox function. This function is called by the
2404 * mailbox event handler function with no lock held. This function
2405 * will wake up thread waiting on the wait queue pointed by context1
2406 * of the mailbox.
2407 **/
2408void
2409lpfc_bsg_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
2410{
2411 struct bsg_job_data *dd_data;
James Smart3b5dd522010-01-26 23:10:15 -05002412 struct fc_bsg_job *job;
2413 uint32_t size;
2414 unsigned long flags;
James Smart7a470272010-03-15 11:25:20 -04002415 uint8_t *to;
2416 uint8_t *from;
James Smart3b5dd522010-01-26 23:10:15 -05002417
2418 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2419 dd_data = pmboxq->context1;
James Smart7a470272010-03-15 11:25:20 -04002420 /* job already timed out? */
James Smart3b5dd522010-01-26 23:10:15 -05002421 if (!dd_data) {
2422 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2423 return;
2424 }
2425
James Smart7a470272010-03-15 11:25:20 -04002426 /* build the outgoing buffer to do an sg copy
2427 * the format is the response mailbox followed by any extended
2428 * mailbox data
2429 */
2430 from = (uint8_t *)&pmboxq->u.mb;
2431 to = (uint8_t *)dd_data->context_un.mbox.mb;
2432 memcpy(to, from, sizeof(MAILBOX_t));
James Smartc7495932010-04-06 15:05:28 -04002433 if (pmboxq->u.mb.mbxStatus == MBX_SUCCESS) {
2434 /* copy the extended data if any, count is in words */
2435 if (dd_data->context_un.mbox.outExtWLen) {
2436 from = (uint8_t *)dd_data->context_un.mbox.ext;
2437 to += sizeof(MAILBOX_t);
2438 size = dd_data->context_un.mbox.outExtWLen *
2439 sizeof(uint32_t);
2440 memcpy(to, from, size);
2441 } else if (pmboxq->u.mb.mbxCommand == MBX_RUN_BIU_DIAG64) {
2442 from = (uint8_t *)dd_data->context_un.mbox.
2443 dmp->dma.virt;
2444 to += sizeof(MAILBOX_t);
2445 size = dd_data->context_un.mbox.dmp->size;
2446 memcpy(to, from, size);
2447 } else if ((phba->sli_rev == LPFC_SLI_REV4) &&
2448 (pmboxq->u.mb.mbxCommand == MBX_DUMP_MEMORY)) {
2449 from = (uint8_t *)dd_data->context_un.mbox.dmp->dma.
2450 virt;
2451 to += sizeof(MAILBOX_t);
2452 size = pmboxq->u.mb.un.varWords[5];
2453 memcpy(to, from, size);
James Smart515e0aa2010-09-29 11:19:00 -04002454 } else if ((phba->sli_rev == LPFC_SLI_REV4) &&
2455 (pmboxq->u.mb.mbxCommand == MBX_SLI4_CONFIG)) {
2456 struct lpfc_mbx_nembed_cmd *nembed_sge =
2457 (struct lpfc_mbx_nembed_cmd *)
2458 &pmboxq->u.mb.un.varWords[0];
2459
2460 from = (uint8_t *)dd_data->context_un.mbox.dmp->dma.
2461 virt;
2462 to += sizeof(MAILBOX_t);
2463 size = nembed_sge->sge[0].length;
2464 memcpy(to, from, size);
James Smartc7495932010-04-06 15:05:28 -04002465 } else if (pmboxq->u.mb.mbxCommand == MBX_READ_EVENT_LOG) {
2466 from = (uint8_t *)dd_data->context_un.
2467 mbox.dmp->dma.virt;
2468 to += sizeof(MAILBOX_t);
2469 size = dd_data->context_un.mbox.dmp->size;
2470 memcpy(to, from, size);
2471 }
James Smart7a470272010-03-15 11:25:20 -04002472 }
2473
2474 from = (uint8_t *)dd_data->context_un.mbox.mb;
James Smart3b5dd522010-01-26 23:10:15 -05002475 job = dd_data->context_un.mbox.set_job;
James Smart7a470272010-03-15 11:25:20 -04002476 size = job->reply_payload.payload_len;
James Smart3b5dd522010-01-26 23:10:15 -05002477 job->reply->reply_payload_rcv_len =
2478 sg_copy_from_buffer(job->reply_payload.sg_list,
2479 job->reply_payload.sg_cnt,
James Smart7a470272010-03-15 11:25:20 -04002480 from, size);
James Smart3b5dd522010-01-26 23:10:15 -05002481 job->reply->result = 0;
James Smart7a470272010-03-15 11:25:20 -04002482
James Smart3b5dd522010-01-26 23:10:15 -05002483 dd_data->context_un.mbox.set_job = NULL;
2484 job->dd_data = NULL;
2485 job->job_done(job);
James Smart7a470272010-03-15 11:25:20 -04002486 /* need to hold the lock until we call job done to hold off
2487 * the timeout handler returning to the midlayer while
2488 * we are stillprocessing the job
2489 */
James Smart3b5dd522010-01-26 23:10:15 -05002490 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smart7a470272010-03-15 11:25:20 -04002491
2492 kfree(dd_data->context_un.mbox.mb);
James Smart3b5dd522010-01-26 23:10:15 -05002493 mempool_free(dd_data->context_un.mbox.pmboxq, phba->mbox_mem_pool);
James Smart7a470272010-03-15 11:25:20 -04002494 kfree(dd_data->context_un.mbox.ext);
2495 if (dd_data->context_un.mbox.dmp) {
2496 dma_free_coherent(&phba->pcidev->dev,
2497 dd_data->context_un.mbox.dmp->size,
2498 dd_data->context_un.mbox.dmp->dma.virt,
2499 dd_data->context_un.mbox.dmp->dma.phys);
2500 kfree(dd_data->context_un.mbox.dmp);
2501 }
2502 if (dd_data->context_un.mbox.rxbmp) {
2503 lpfc_mbuf_free(phba, dd_data->context_un.mbox.rxbmp->virt,
2504 dd_data->context_un.mbox.rxbmp->phys);
2505 kfree(dd_data->context_un.mbox.rxbmp);
2506 }
James Smart3b5dd522010-01-26 23:10:15 -05002507 kfree(dd_data);
2508 return;
2509}
2510
2511/**
2512 * lpfc_bsg_check_cmd_access - test for a supported mailbox command
2513 * @phba: Pointer to HBA context object.
2514 * @mb: Pointer to a mailbox object.
2515 * @vport: Pointer to a vport object.
2516 *
2517 * Some commands require the port to be offline, some may not be called from
2518 * the application.
2519 **/
2520static int lpfc_bsg_check_cmd_access(struct lpfc_hba *phba,
2521 MAILBOX_t *mb, struct lpfc_vport *vport)
2522{
2523 /* return negative error values for bsg job */
2524 switch (mb->mbxCommand) {
2525 /* Offline only */
2526 case MBX_INIT_LINK:
2527 case MBX_DOWN_LINK:
2528 case MBX_CONFIG_LINK:
2529 case MBX_CONFIG_RING:
2530 case MBX_RESET_RING:
2531 case MBX_UNREG_LOGIN:
2532 case MBX_CLEAR_LA:
2533 case MBX_DUMP_CONTEXT:
2534 case MBX_RUN_DIAGS:
2535 case MBX_RESTART:
2536 case MBX_SET_MASK:
2537 if (!(vport->fc_flag & FC_OFFLINE_MODE)) {
2538 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2539 "2743 Command 0x%x is illegal in on-line "
2540 "state\n",
2541 mb->mbxCommand);
2542 return -EPERM;
2543 }
2544 case MBX_WRITE_NV:
2545 case MBX_WRITE_VPARMS:
2546 case MBX_LOAD_SM:
2547 case MBX_READ_NV:
2548 case MBX_READ_CONFIG:
2549 case MBX_READ_RCONFIG:
2550 case MBX_READ_STATUS:
2551 case MBX_READ_XRI:
2552 case MBX_READ_REV:
2553 case MBX_READ_LNK_STAT:
2554 case MBX_DUMP_MEMORY:
2555 case MBX_DOWN_LOAD:
2556 case MBX_UPDATE_CFG:
2557 case MBX_KILL_BOARD:
2558 case MBX_LOAD_AREA:
2559 case MBX_LOAD_EXP_ROM:
2560 case MBX_BEACON:
2561 case MBX_DEL_LD_ENTRY:
2562 case MBX_SET_DEBUG:
2563 case MBX_WRITE_WWN:
2564 case MBX_SLI4_CONFIG:
James Smartc7495932010-04-06 15:05:28 -04002565 case MBX_READ_EVENT_LOG:
James Smart3b5dd522010-01-26 23:10:15 -05002566 case MBX_READ_EVENT_LOG_STATUS:
2567 case MBX_WRITE_EVENT_LOG:
2568 case MBX_PORT_CAPABILITIES:
2569 case MBX_PORT_IOV_CONTROL:
James Smart7a470272010-03-15 11:25:20 -04002570 case MBX_RUN_BIU_DIAG64:
James Smart3b5dd522010-01-26 23:10:15 -05002571 break;
2572 case MBX_SET_VARIABLE:
James Smarte2aed292010-02-26 14:15:00 -05002573 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2574 "1226 mbox: set_variable 0x%x, 0x%x\n",
2575 mb->un.varWords[0],
2576 mb->un.varWords[1]);
2577 if ((mb->un.varWords[0] == SETVAR_MLOMNT)
2578 && (mb->un.varWords[1] == 1)) {
2579 phba->wait_4_mlo_maint_flg = 1;
2580 } else if (mb->un.varWords[0] == SETVAR_MLORST) {
2581 phba->link_flag &= ~LS_LOOPBACK_MODE;
James Smart76a95d72010-11-20 23:11:48 -05002582 phba->fc_topology = LPFC_TOPOLOGY_PT_PT;
James Smarte2aed292010-02-26 14:15:00 -05002583 }
2584 break;
James Smart3b5dd522010-01-26 23:10:15 -05002585 case MBX_READ_SPARM64:
James Smart76a95d72010-11-20 23:11:48 -05002586 case MBX_READ_TOPOLOGY:
James Smart3b5dd522010-01-26 23:10:15 -05002587 case MBX_REG_LOGIN:
2588 case MBX_REG_LOGIN64:
2589 case MBX_CONFIG_PORT:
2590 case MBX_RUN_BIU_DIAG:
2591 default:
2592 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2593 "2742 Unknown Command 0x%x\n",
2594 mb->mbxCommand);
2595 return -EPERM;
2596 }
2597
2598 return 0; /* ok */
2599}
2600
2601/**
2602 * lpfc_bsg_issue_mbox - issues a mailbox command on behalf of an app
2603 * @phba: Pointer to HBA context object.
2604 * @mb: Pointer to a mailbox object.
2605 * @vport: Pointer to a vport object.
2606 *
2607 * Allocate a tracking object, mailbox command memory, get a mailbox
2608 * from the mailbox pool, copy the caller mailbox command.
2609 *
2610 * If offline and the sli is active we need to poll for the command (port is
2611 * being reset) and com-plete the job, otherwise issue the mailbox command and
2612 * let our completion handler finish the command.
2613 **/
2614static uint32_t
2615lpfc_bsg_issue_mbox(struct lpfc_hba *phba, struct fc_bsg_job *job,
2616 struct lpfc_vport *vport)
2617{
James Smart7a470272010-03-15 11:25:20 -04002618 LPFC_MBOXQ_t *pmboxq = NULL; /* internal mailbox queue */
2619 MAILBOX_t *pmb; /* shortcut to the pmboxq mailbox */
2620 /* a 4k buffer to hold the mb and extended data from/to the bsg */
2621 MAILBOX_t *mb = NULL;
2622 struct bsg_job_data *dd_data = NULL; /* bsg data tracking structure */
James Smart3b5dd522010-01-26 23:10:15 -05002623 uint32_t size;
James Smart7a470272010-03-15 11:25:20 -04002624 struct lpfc_dmabuf *rxbmp = NULL; /* for biu diag */
2625 struct lpfc_dmabufext *dmp = NULL; /* for biu diag */
2626 struct ulp_bde64 *rxbpl = NULL;
2627 struct dfc_mbox_req *mbox_req = (struct dfc_mbox_req *)
2628 job->request->rqst_data.h_vendor.vendor_cmd;
2629 uint8_t *ext = NULL;
James Smart3b5dd522010-01-26 23:10:15 -05002630 int rc = 0;
James Smart7a470272010-03-15 11:25:20 -04002631 uint8_t *from;
2632
2633 /* in case no data is transferred */
2634 job->reply->reply_payload_rcv_len = 0;
2635
2636 /* check if requested extended data lengths are valid */
2637 if ((mbox_req->inExtWLen > MAILBOX_EXT_SIZE) ||
James Smartc7495932010-04-06 15:05:28 -04002638 (mbox_req->outExtWLen > MAILBOX_EXT_SIZE)) {
James Smart7a470272010-03-15 11:25:20 -04002639 rc = -ERANGE;
2640 goto job_done;
2641 }
James Smart3b5dd522010-01-26 23:10:15 -05002642
2643 /* allocate our bsg tracking structure */
2644 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
2645 if (!dd_data) {
2646 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2647 "2727 Failed allocation of dd_data\n");
James Smart7a470272010-03-15 11:25:20 -04002648 rc = -ENOMEM;
2649 goto job_done;
James Smart3b5dd522010-01-26 23:10:15 -05002650 }
2651
James Smart49198b32010-04-06 15:04:33 -04002652 mb = kzalloc(BSG_MBOX_SIZE, GFP_KERNEL);
James Smart3b5dd522010-01-26 23:10:15 -05002653 if (!mb) {
James Smart7a470272010-03-15 11:25:20 -04002654 rc = -ENOMEM;
2655 goto job_done;
James Smart3b5dd522010-01-26 23:10:15 -05002656 }
2657
2658 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2659 if (!pmboxq) {
James Smart7a470272010-03-15 11:25:20 -04002660 rc = -ENOMEM;
2661 goto job_done;
James Smart3b5dd522010-01-26 23:10:15 -05002662 }
James Smart7a470272010-03-15 11:25:20 -04002663 memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
James Smart3b5dd522010-01-26 23:10:15 -05002664
2665 size = job->request_payload.payload_len;
James Smart7a470272010-03-15 11:25:20 -04002666 sg_copy_to_buffer(job->request_payload.sg_list,
2667 job->request_payload.sg_cnt,
2668 mb, size);
James Smart3b5dd522010-01-26 23:10:15 -05002669
2670 rc = lpfc_bsg_check_cmd_access(phba, mb, vport);
James Smart7a470272010-03-15 11:25:20 -04002671 if (rc != 0)
2672 goto job_done; /* must be negative */
James Smart3b5dd522010-01-26 23:10:15 -05002673
James Smart3b5dd522010-01-26 23:10:15 -05002674 pmb = &pmboxq->u.mb;
2675 memcpy(pmb, mb, sizeof(*pmb));
2676 pmb->mbxOwner = OWN_HOST;
James Smart3b5dd522010-01-26 23:10:15 -05002677 pmboxq->vport = vport;
2678
James Smartc7495932010-04-06 15:05:28 -04002679 /* If HBA encountered an error attention, allow only DUMP
2680 * or RESTART mailbox commands until the HBA is restarted.
2681 */
2682 if (phba->pport->stopped &&
2683 pmb->mbxCommand != MBX_DUMP_MEMORY &&
2684 pmb->mbxCommand != MBX_RESTART &&
2685 pmb->mbxCommand != MBX_WRITE_VPARMS &&
2686 pmb->mbxCommand != MBX_WRITE_WWN)
2687 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
2688 "2797 mbox: Issued mailbox cmd "
2689 "0x%x while in stopped state.\n",
2690 pmb->mbxCommand);
2691
2692 /* Don't allow mailbox commands to be sent when blocked
2693 * or when in the middle of discovery
2694 */
2695 if (phba->sli.sli_flag & LPFC_BLOCK_MGMT_IO) {
2696 rc = -EAGAIN;
2697 goto job_done;
2698 }
2699
James Smart7a470272010-03-15 11:25:20 -04002700 /* extended mailbox commands will need an extended buffer */
James Smartc7495932010-04-06 15:05:28 -04002701 if (mbox_req->inExtWLen || mbox_req->outExtWLen) {
James Smart7a470272010-03-15 11:25:20 -04002702 ext = kzalloc(MAILBOX_EXT_SIZE, GFP_KERNEL);
2703 if (!ext) {
2704 rc = -ENOMEM;
2705 goto job_done;
James Smart3b5dd522010-01-26 23:10:15 -05002706 }
2707
James Smart7a470272010-03-15 11:25:20 -04002708 /* any data for the device? */
2709 if (mbox_req->inExtWLen) {
2710 from = (uint8_t *)mb;
2711 from += sizeof(MAILBOX_t);
2712 memcpy((uint8_t *)ext, from,
2713 mbox_req->inExtWLen * sizeof(uint32_t));
2714 }
2715
2716 pmboxq->context2 = ext;
2717 pmboxq->in_ext_byte_len =
James Smart7a470272010-03-15 11:25:20 -04002718 mbox_req->inExtWLen * sizeof(uint32_t);
2719 pmboxq->out_ext_byte_len =
James Smartc7495932010-04-06 15:05:28 -04002720 mbox_req->outExtWLen * sizeof(uint32_t);
James Smart7a470272010-03-15 11:25:20 -04002721 pmboxq->mbox_offset_word = mbox_req->mbOffset;
2722 }
2723
2724 /* biu diag will need a kernel buffer to transfer the data
2725 * allocate our own buffer and setup the mailbox command to
2726 * use ours
2727 */
2728 if (pmb->mbxCommand == MBX_RUN_BIU_DIAG64) {
James Smartc7495932010-04-06 15:05:28 -04002729 uint32_t transmit_length = pmb->un.varWords[1];
2730 uint32_t receive_length = pmb->un.varWords[4];
2731 /* transmit length cannot be greater than receive length or
2732 * mailbox extension size
2733 */
2734 if ((transmit_length > receive_length) ||
2735 (transmit_length > MAILBOX_EXT_SIZE)) {
2736 rc = -ERANGE;
2737 goto job_done;
2738 }
2739
James Smart7a470272010-03-15 11:25:20 -04002740 rxbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2741 if (!rxbmp) {
2742 rc = -ENOMEM;
2743 goto job_done;
2744 }
2745
2746 rxbmp->virt = lpfc_mbuf_alloc(phba, 0, &rxbmp->phys);
James Smartc7495932010-04-06 15:05:28 -04002747 if (!rxbmp->virt) {
2748 rc = -ENOMEM;
2749 goto job_done;
2750 }
2751
James Smart7a470272010-03-15 11:25:20 -04002752 INIT_LIST_HEAD(&rxbmp->list);
2753 rxbpl = (struct ulp_bde64 *) rxbmp->virt;
James Smartc7495932010-04-06 15:05:28 -04002754 dmp = diag_cmd_data_alloc(phba, rxbpl, transmit_length, 0);
James Smart7a470272010-03-15 11:25:20 -04002755 if (!dmp) {
2756 rc = -ENOMEM;
2757 goto job_done;
2758 }
2759
James Smart7a470272010-03-15 11:25:20 -04002760 INIT_LIST_HEAD(&dmp->dma.list);
2761 pmb->un.varBIUdiag.un.s2.xmit_bde64.addrHigh =
2762 putPaddrHigh(dmp->dma.phys);
2763 pmb->un.varBIUdiag.un.s2.xmit_bde64.addrLow =
2764 putPaddrLow(dmp->dma.phys);
2765
2766 pmb->un.varBIUdiag.un.s2.rcv_bde64.addrHigh =
2767 putPaddrHigh(dmp->dma.phys +
2768 pmb->un.varBIUdiag.un.s2.
2769 xmit_bde64.tus.f.bdeSize);
2770 pmb->un.varBIUdiag.un.s2.rcv_bde64.addrLow =
2771 putPaddrLow(dmp->dma.phys +
2772 pmb->un.varBIUdiag.un.s2.
2773 xmit_bde64.tus.f.bdeSize);
James Smartc7495932010-04-06 15:05:28 -04002774
2775 /* copy the transmit data found in the mailbox extension area */
2776 from = (uint8_t *)mb;
2777 from += sizeof(MAILBOX_t);
2778 memcpy((uint8_t *)dmp->dma.virt, from, transmit_length);
2779 } else if (pmb->mbxCommand == MBX_READ_EVENT_LOG) {
2780 struct READ_EVENT_LOG_VAR *rdEventLog =
2781 &pmb->un.varRdEventLog ;
2782 uint32_t receive_length = rdEventLog->rcv_bde64.tus.f.bdeSize;
2783 uint32_t mode = bf_get(lpfc_event_log, rdEventLog);
2784
2785 /* receive length cannot be greater than mailbox
2786 * extension size
2787 */
2788 if (receive_length > MAILBOX_EXT_SIZE) {
2789 rc = -ERANGE;
2790 goto job_done;
2791 }
2792
2793 /* mode zero uses a bde like biu diags command */
2794 if (mode == 0) {
2795
2796 /* rebuild the command for sli4 using our own buffers
2797 * like we do for biu diags
2798 */
2799
2800 rxbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2801 if (!rxbmp) {
2802 rc = -ENOMEM;
2803 goto job_done;
2804 }
2805
2806 rxbmp->virt = lpfc_mbuf_alloc(phba, 0, &rxbmp->phys);
2807 rxbpl = (struct ulp_bde64 *) rxbmp->virt;
2808 if (rxbpl) {
2809 INIT_LIST_HEAD(&rxbmp->list);
2810 dmp = diag_cmd_data_alloc(phba, rxbpl,
2811 receive_length, 0);
2812 }
2813
2814 if (!dmp) {
2815 rc = -ENOMEM;
2816 goto job_done;
2817 }
2818
2819 INIT_LIST_HEAD(&dmp->dma.list);
2820 pmb->un.varWords[3] = putPaddrLow(dmp->dma.phys);
2821 pmb->un.varWords[4] = putPaddrHigh(dmp->dma.phys);
2822 }
2823 } else if (phba->sli_rev == LPFC_SLI_REV4) {
2824 if (pmb->mbxCommand == MBX_DUMP_MEMORY) {
2825 /* rebuild the command for sli4 using our own buffers
2826 * like we do for biu diags
2827 */
2828 uint32_t receive_length = pmb->un.varWords[2];
2829 /* receive length cannot be greater than mailbox
2830 * extension size
2831 */
2832 if ((receive_length == 0) ||
2833 (receive_length > MAILBOX_EXT_SIZE)) {
2834 rc = -ERANGE;
2835 goto job_done;
2836 }
2837
2838 rxbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2839 if (!rxbmp) {
2840 rc = -ENOMEM;
2841 goto job_done;
2842 }
2843
2844 rxbmp->virt = lpfc_mbuf_alloc(phba, 0, &rxbmp->phys);
2845 if (!rxbmp->virt) {
2846 rc = -ENOMEM;
2847 goto job_done;
2848 }
2849
2850 INIT_LIST_HEAD(&rxbmp->list);
2851 rxbpl = (struct ulp_bde64 *) rxbmp->virt;
2852 dmp = diag_cmd_data_alloc(phba, rxbpl, receive_length,
2853 0);
2854 if (!dmp) {
2855 rc = -ENOMEM;
2856 goto job_done;
2857 }
2858
2859 INIT_LIST_HEAD(&dmp->dma.list);
2860 pmb->un.varWords[3] = putPaddrLow(dmp->dma.phys);
2861 pmb->un.varWords[4] = putPaddrHigh(dmp->dma.phys);
2862 } else if ((pmb->mbxCommand == MBX_UPDATE_CFG) &&
2863 pmb->un.varUpdateCfg.co) {
2864 struct ulp_bde64 *bde =
2865 (struct ulp_bde64 *)&pmb->un.varWords[4];
2866
2867 /* bde size cannot be greater than mailbox ext size */
2868 if (bde->tus.f.bdeSize > MAILBOX_EXT_SIZE) {
2869 rc = -ERANGE;
2870 goto job_done;
2871 }
2872
2873 rxbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2874 if (!rxbmp) {
2875 rc = -ENOMEM;
2876 goto job_done;
2877 }
2878
2879 rxbmp->virt = lpfc_mbuf_alloc(phba, 0, &rxbmp->phys);
2880 if (!rxbmp->virt) {
2881 rc = -ENOMEM;
2882 goto job_done;
2883 }
2884
2885 INIT_LIST_HEAD(&rxbmp->list);
2886 rxbpl = (struct ulp_bde64 *) rxbmp->virt;
2887 dmp = diag_cmd_data_alloc(phba, rxbpl,
2888 bde->tus.f.bdeSize, 0);
2889 if (!dmp) {
2890 rc = -ENOMEM;
2891 goto job_done;
2892 }
2893
2894 INIT_LIST_HEAD(&dmp->dma.list);
2895 bde->addrHigh = putPaddrHigh(dmp->dma.phys);
2896 bde->addrLow = putPaddrLow(dmp->dma.phys);
2897
2898 /* copy the transmit data found in the mailbox
2899 * extension area
2900 */
2901 from = (uint8_t *)mb;
2902 from += sizeof(MAILBOX_t);
2903 memcpy((uint8_t *)dmp->dma.virt, from,
2904 bde->tus.f.bdeSize);
James Smart515e0aa2010-09-29 11:19:00 -04002905 } else if (pmb->mbxCommand == MBX_SLI4_CONFIG) {
2906 struct lpfc_mbx_nembed_cmd *nembed_sge;
2907 struct mbox_header *header;
2908 uint32_t receive_length;
2909
2910 /* rebuild the command for sli4 using our own buffers
2911 * like we do for biu diags
2912 */
2913 header = (struct mbox_header *)&pmb->un.varWords[0];
2914 nembed_sge = (struct lpfc_mbx_nembed_cmd *)
2915 &pmb->un.varWords[0];
2916 receive_length = nembed_sge->sge[0].length;
2917
2918 /* receive length cannot be greater than mailbox
2919 * extension size
2920 */
2921 if ((receive_length == 0) ||
2922 (receive_length > MAILBOX_EXT_SIZE)) {
2923 rc = -ERANGE;
2924 goto job_done;
2925 }
2926
2927 rxbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2928 if (!rxbmp) {
2929 rc = -ENOMEM;
2930 goto job_done;
2931 }
2932
2933 rxbmp->virt = lpfc_mbuf_alloc(phba, 0, &rxbmp->phys);
2934 if (!rxbmp->virt) {
2935 rc = -ENOMEM;
2936 goto job_done;
2937 }
2938
2939 INIT_LIST_HEAD(&rxbmp->list);
2940 rxbpl = (struct ulp_bde64 *) rxbmp->virt;
2941 dmp = diag_cmd_data_alloc(phba, rxbpl, receive_length,
2942 0);
2943 if (!dmp) {
2944 rc = -ENOMEM;
2945 goto job_done;
2946 }
2947
2948 INIT_LIST_HEAD(&dmp->dma.list);
2949 nembed_sge->sge[0].pa_hi = putPaddrHigh(dmp->dma.phys);
2950 nembed_sge->sge[0].pa_lo = putPaddrLow(dmp->dma.phys);
2951 /* copy the transmit data found in the mailbox
2952 * extension area
2953 */
2954 from = (uint8_t *)mb;
2955 from += sizeof(MAILBOX_t);
2956 memcpy((uint8_t *)dmp->dma.virt, from,
2957 header->cfg_mhdr.payload_length);
James Smartc7495932010-04-06 15:05:28 -04002958 }
James Smart3b5dd522010-01-26 23:10:15 -05002959 }
2960
James Smartc7495932010-04-06 15:05:28 -04002961 dd_data->context_un.mbox.rxbmp = rxbmp;
2962 dd_data->context_un.mbox.dmp = dmp;
2963
James Smart3b5dd522010-01-26 23:10:15 -05002964 /* setup wake call as IOCB callback */
2965 pmboxq->mbox_cmpl = lpfc_bsg_wake_mbox_wait;
James Smart7a470272010-03-15 11:25:20 -04002966
James Smart3b5dd522010-01-26 23:10:15 -05002967 /* setup context field to pass wait_queue pointer to wake function */
2968 pmboxq->context1 = dd_data;
2969 dd_data->type = TYPE_MBOX;
2970 dd_data->context_un.mbox.pmboxq = pmboxq;
2971 dd_data->context_un.mbox.mb = mb;
2972 dd_data->context_un.mbox.set_job = job;
James Smart7a470272010-03-15 11:25:20 -04002973 dd_data->context_un.mbox.ext = ext;
2974 dd_data->context_un.mbox.mbOffset = mbox_req->mbOffset;
2975 dd_data->context_un.mbox.inExtWLen = mbox_req->inExtWLen;
James Smartc7495932010-04-06 15:05:28 -04002976 dd_data->context_un.mbox.outExtWLen = mbox_req->outExtWLen;
James Smart3b5dd522010-01-26 23:10:15 -05002977 job->dd_data = dd_data;
James Smart7a470272010-03-15 11:25:20 -04002978
2979 if ((vport->fc_flag & FC_OFFLINE_MODE) ||
2980 (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))) {
2981 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
2982 if (rc != MBX_SUCCESS) {
2983 rc = (rc == MBX_TIMEOUT) ? -ETIME : -ENODEV;
2984 goto job_done;
2985 }
2986
2987 /* job finished, copy the data */
2988 memcpy(mb, pmb, sizeof(*pmb));
2989 job->reply->reply_payload_rcv_len =
2990 sg_copy_from_buffer(job->reply_payload.sg_list,
2991 job->reply_payload.sg_cnt,
2992 mb, size);
2993 /* not waiting mbox already done */
2994 rc = 0;
2995 goto job_done;
James Smart3b5dd522010-01-26 23:10:15 -05002996 }
2997
James Smart7a470272010-03-15 11:25:20 -04002998 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
2999 if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY))
3000 return 1; /* job started */
3001
3002job_done:
3003 /* common exit for error or job completed inline */
3004 kfree(mb);
3005 if (pmboxq)
3006 mempool_free(pmboxq, phba->mbox_mem_pool);
3007 kfree(ext);
3008 if (dmp) {
3009 dma_free_coherent(&phba->pcidev->dev,
3010 dmp->size, dmp->dma.virt,
3011 dmp->dma.phys);
3012 kfree(dmp);
3013 }
3014 if (rxbmp) {
3015 lpfc_mbuf_free(phba, rxbmp->virt, rxbmp->phys);
3016 kfree(rxbmp);
3017 }
3018 kfree(dd_data);
3019
3020 return rc;
James Smart3b5dd522010-01-26 23:10:15 -05003021}
3022
3023/**
3024 * lpfc_bsg_mbox_cmd - process an fc bsg LPFC_BSG_VENDOR_MBOX command
3025 * @job: MBOX fc_bsg_job for LPFC_BSG_VENDOR_MBOX.
3026 **/
3027static int
3028lpfc_bsg_mbox_cmd(struct fc_bsg_job *job)
3029{
3030 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
3031 struct lpfc_hba *phba = vport->phba;
3032 int rc = 0;
3033
3034 /* in case no data is transferred */
3035 job->reply->reply_payload_rcv_len = 0;
3036 if (job->request_len <
3037 sizeof(struct fc_bsg_request) + sizeof(struct dfc_mbox_req)) {
3038 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3039 "2737 Received MBOX_REQ request below "
3040 "minimum size\n");
3041 rc = -EINVAL;
3042 goto job_error;
3043 }
3044
James Smart49198b32010-04-06 15:04:33 -04003045 if (job->request_payload.payload_len != BSG_MBOX_SIZE) {
James Smart3b5dd522010-01-26 23:10:15 -05003046 rc = -EINVAL;
3047 goto job_error;
3048 }
3049
James Smart49198b32010-04-06 15:04:33 -04003050 if (job->reply_payload.payload_len != BSG_MBOX_SIZE) {
James Smart7a470272010-03-15 11:25:20 -04003051 rc = -EINVAL;
3052 goto job_error;
3053 }
3054
James Smart3b5dd522010-01-26 23:10:15 -05003055 if (phba->sli.sli_flag & LPFC_BLOCK_MGMT_IO) {
3056 rc = -EAGAIN;
3057 goto job_error;
3058 }
3059
3060 rc = lpfc_bsg_issue_mbox(phba, job, vport);
3061
3062job_error:
3063 if (rc == 0) {
3064 /* job done */
3065 job->reply->result = 0;
3066 job->dd_data = NULL;
3067 job->job_done(job);
3068 } else if (rc == 1)
3069 /* job submitted, will complete later*/
3070 rc = 0; /* return zero, no error */
3071 else {
3072 /* some error occurred */
3073 job->reply->result = rc;
3074 job->dd_data = NULL;
3075 }
3076
3077 return rc;
3078}
3079
3080/**
James Smarte2aed292010-02-26 14:15:00 -05003081 * lpfc_bsg_menlo_cmd_cmp - lpfc_menlo_cmd completion handler
3082 * @phba: Pointer to HBA context object.
3083 * @cmdiocbq: Pointer to command iocb.
3084 * @rspiocbq: Pointer to response iocb.
3085 *
3086 * This function is the completion handler for iocbs issued using
3087 * lpfc_menlo_cmd function. This function is called by the
3088 * ring event handler function without any lock held. This function
3089 * can be called from both worker thread context and interrupt
3090 * context. This function also can be called from another thread which
3091 * cleans up the SLI layer objects.
3092 * This function copies the contents of the response iocb to the
3093 * response iocb memory object provided by the caller of
3094 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
3095 * sleeps for the iocb completion.
3096 **/
3097static void
3098lpfc_bsg_menlo_cmd_cmp(struct lpfc_hba *phba,
3099 struct lpfc_iocbq *cmdiocbq,
3100 struct lpfc_iocbq *rspiocbq)
3101{
3102 struct bsg_job_data *dd_data;
3103 struct fc_bsg_job *job;
3104 IOCB_t *rsp;
3105 struct lpfc_dmabuf *bmp;
3106 struct lpfc_bsg_menlo *menlo;
3107 unsigned long flags;
3108 struct menlo_response *menlo_resp;
3109 int rc = 0;
3110
3111 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3112 dd_data = cmdiocbq->context1;
3113 if (!dd_data) {
3114 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3115 return;
3116 }
3117
3118 menlo = &dd_data->context_un.menlo;
3119 job = menlo->set_job;
3120 job->dd_data = NULL; /* so timeout handler does not reply */
3121
James Smart5989b8d2010-10-22 11:06:56 -04003122 spin_lock(&phba->hbalock);
James Smarte2aed292010-02-26 14:15:00 -05003123 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
3124 if (cmdiocbq->context2 && rspiocbq)
3125 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
3126 &rspiocbq->iocb, sizeof(IOCB_t));
James Smart5989b8d2010-10-22 11:06:56 -04003127 spin_unlock(&phba->hbalock);
James Smarte2aed292010-02-26 14:15:00 -05003128
3129 bmp = menlo->bmp;
3130 rspiocbq = menlo->rspiocbq;
3131 rsp = &rspiocbq->iocb;
3132
3133 pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
3134 job->request_payload.sg_cnt, DMA_TO_DEVICE);
3135 pci_unmap_sg(phba->pcidev, job->reply_payload.sg_list,
3136 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
3137
3138 /* always return the xri, this would be used in the case
3139 * of a menlo download to allow the data to be sent as a continuation
3140 * of the exchange.
3141 */
3142 menlo_resp = (struct menlo_response *)
3143 job->reply->reply_data.vendor_reply.vendor_rsp;
3144 menlo_resp->xri = rsp->ulpContext;
3145 if (rsp->ulpStatus) {
3146 if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
3147 switch (rsp->un.ulpWord[4] & 0xff) {
3148 case IOERR_SEQUENCE_TIMEOUT:
3149 rc = -ETIMEDOUT;
3150 break;
3151 case IOERR_INVALID_RPI:
3152 rc = -EFAULT;
3153 break;
3154 default:
3155 rc = -EACCES;
3156 break;
3157 }
3158 } else
3159 rc = -EACCES;
3160 } else
3161 job->reply->reply_payload_rcv_len =
3162 rsp->un.genreq64.bdl.bdeSize;
3163
3164 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
3165 lpfc_sli_release_iocbq(phba, rspiocbq);
3166 lpfc_sli_release_iocbq(phba, cmdiocbq);
3167 kfree(bmp);
3168 kfree(dd_data);
3169 /* make error code available to userspace */
3170 job->reply->result = rc;
3171 /* complete the job back to userspace */
3172 job->job_done(job);
3173 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3174 return;
3175}
3176
3177/**
3178 * lpfc_menlo_cmd - send an ioctl for menlo hardware
3179 * @job: fc_bsg_job to handle
3180 *
3181 * This function issues a gen request 64 CR ioctl for all menlo cmd requests,
3182 * all the command completions will return the xri for the command.
3183 * For menlo data requests a gen request 64 CX is used to continue the exchange
3184 * supplied in the menlo request header xri field.
3185 **/
3186static int
3187lpfc_menlo_cmd(struct fc_bsg_job *job)
3188{
3189 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
3190 struct lpfc_hba *phba = vport->phba;
3191 struct lpfc_iocbq *cmdiocbq, *rspiocbq;
3192 IOCB_t *cmd, *rsp;
3193 int rc = 0;
3194 struct menlo_command *menlo_cmd;
3195 struct menlo_response *menlo_resp;
3196 struct lpfc_dmabuf *bmp = NULL;
3197 int request_nseg;
3198 int reply_nseg;
3199 struct scatterlist *sgel = NULL;
3200 int numbde;
3201 dma_addr_t busaddr;
3202 struct bsg_job_data *dd_data;
3203 struct ulp_bde64 *bpl = NULL;
3204
3205 /* in case no data is returned return just the return code */
3206 job->reply->reply_payload_rcv_len = 0;
3207
3208 if (job->request_len <
3209 sizeof(struct fc_bsg_request) +
3210 sizeof(struct menlo_command)) {
3211 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3212 "2784 Received MENLO_CMD request below "
3213 "minimum size\n");
3214 rc = -ERANGE;
3215 goto no_dd_data;
3216 }
3217
3218 if (job->reply_len <
3219 sizeof(struct fc_bsg_request) + sizeof(struct menlo_response)) {
3220 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3221 "2785 Received MENLO_CMD reply below "
3222 "minimum size\n");
3223 rc = -ERANGE;
3224 goto no_dd_data;
3225 }
3226
3227 if (!(phba->menlo_flag & HBA_MENLO_SUPPORT)) {
3228 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3229 "2786 Adapter does not support menlo "
3230 "commands\n");
3231 rc = -EPERM;
3232 goto no_dd_data;
3233 }
3234
3235 menlo_cmd = (struct menlo_command *)
3236 job->request->rqst_data.h_vendor.vendor_cmd;
3237
3238 menlo_resp = (struct menlo_response *)
3239 job->reply->reply_data.vendor_reply.vendor_rsp;
3240
3241 /* allocate our bsg tracking structure */
3242 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
3243 if (!dd_data) {
3244 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3245 "2787 Failed allocation of dd_data\n");
3246 rc = -ENOMEM;
3247 goto no_dd_data;
3248 }
3249
3250 bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3251 if (!bmp) {
3252 rc = -ENOMEM;
3253 goto free_dd;
3254 }
3255
3256 cmdiocbq = lpfc_sli_get_iocbq(phba);
3257 if (!cmdiocbq) {
3258 rc = -ENOMEM;
3259 goto free_bmp;
3260 }
3261
3262 rspiocbq = lpfc_sli_get_iocbq(phba);
3263 if (!rspiocbq) {
3264 rc = -ENOMEM;
3265 goto free_cmdiocbq;
3266 }
3267
3268 rsp = &rspiocbq->iocb;
3269
3270 bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
3271 if (!bmp->virt) {
3272 rc = -ENOMEM;
3273 goto free_rspiocbq;
3274 }
3275
3276 INIT_LIST_HEAD(&bmp->list);
3277 bpl = (struct ulp_bde64 *) bmp->virt;
3278 request_nseg = pci_map_sg(phba->pcidev, job->request_payload.sg_list,
3279 job->request_payload.sg_cnt, DMA_TO_DEVICE);
3280 for_each_sg(job->request_payload.sg_list, sgel, request_nseg, numbde) {
3281 busaddr = sg_dma_address(sgel);
3282 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
3283 bpl->tus.f.bdeSize = sg_dma_len(sgel);
3284 bpl->tus.w = cpu_to_le32(bpl->tus.w);
3285 bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
3286 bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
3287 bpl++;
3288 }
3289
3290 reply_nseg = pci_map_sg(phba->pcidev, job->reply_payload.sg_list,
3291 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
3292 for_each_sg(job->reply_payload.sg_list, sgel, reply_nseg, numbde) {
3293 busaddr = sg_dma_address(sgel);
3294 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
3295 bpl->tus.f.bdeSize = sg_dma_len(sgel);
3296 bpl->tus.w = cpu_to_le32(bpl->tus.w);
3297 bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
3298 bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
3299 bpl++;
3300 }
3301
3302 cmd = &cmdiocbq->iocb;
3303 cmd->un.genreq64.bdl.ulpIoTag32 = 0;
3304 cmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
3305 cmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
3306 cmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
3307 cmd->un.genreq64.bdl.bdeSize =
3308 (request_nseg + reply_nseg) * sizeof(struct ulp_bde64);
3309 cmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
3310 cmd->un.genreq64.w5.hcsw.Dfctl = 0;
3311 cmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CMD;
3312 cmd->un.genreq64.w5.hcsw.Type = MENLO_TRANSPORT_TYPE; /* 0xfe */
3313 cmd->ulpBdeCount = 1;
3314 cmd->ulpClass = CLASS3;
3315 cmd->ulpOwner = OWN_CHIP;
3316 cmd->ulpLe = 1; /* Limited Edition */
3317 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
3318 cmdiocbq->vport = phba->pport;
3319 /* We want the firmware to timeout before we do */
3320 cmd->ulpTimeout = MENLO_TIMEOUT - 5;
3321 cmdiocbq->context3 = bmp;
3322 cmdiocbq->context2 = rspiocbq;
3323 cmdiocbq->iocb_cmpl = lpfc_bsg_menlo_cmd_cmp;
3324 cmdiocbq->context1 = dd_data;
3325 cmdiocbq->context2 = rspiocbq;
3326 if (menlo_cmd->cmd == LPFC_BSG_VENDOR_MENLO_CMD) {
3327 cmd->ulpCommand = CMD_GEN_REQUEST64_CR;
3328 cmd->ulpPU = MENLO_PU; /* 3 */
3329 cmd->un.ulpWord[4] = MENLO_DID; /* 0x0000FC0E */
3330 cmd->ulpContext = MENLO_CONTEXT; /* 0 */
3331 } else {
3332 cmd->ulpCommand = CMD_GEN_REQUEST64_CX;
3333 cmd->ulpPU = 1;
3334 cmd->un.ulpWord[4] = 0;
3335 cmd->ulpContext = menlo_cmd->xri;
3336 }
3337
3338 dd_data->type = TYPE_MENLO;
3339 dd_data->context_un.menlo.cmdiocbq = cmdiocbq;
3340 dd_data->context_un.menlo.rspiocbq = rspiocbq;
3341 dd_data->context_un.menlo.set_job = job;
3342 dd_data->context_un.menlo.bmp = bmp;
3343
3344 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq,
3345 MENLO_TIMEOUT - 5);
3346 if (rc == IOCB_SUCCESS)
3347 return 0; /* done for now */
3348
3349 /* iocb failed so cleanup */
3350 pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
3351 job->request_payload.sg_cnt, DMA_TO_DEVICE);
3352 pci_unmap_sg(phba->pcidev, job->reply_payload.sg_list,
3353 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
3354
3355 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
3356
3357free_rspiocbq:
3358 lpfc_sli_release_iocbq(phba, rspiocbq);
3359free_cmdiocbq:
3360 lpfc_sli_release_iocbq(phba, cmdiocbq);
3361free_bmp:
3362 kfree(bmp);
3363free_dd:
3364 kfree(dd_data);
3365no_dd_data:
3366 /* make error code available to userspace */
3367 job->reply->result = rc;
3368 job->dd_data = NULL;
3369 return rc;
3370}
3371/**
James Smartf1c3b0f2009-07-19 10:01:32 -04003372 * lpfc_bsg_hst_vendor - process a vendor-specific fc_bsg_job
3373 * @job: fc_bsg_job to handle
James Smart3b5dd522010-01-26 23:10:15 -05003374 **/
James Smartf1c3b0f2009-07-19 10:01:32 -04003375static int
3376lpfc_bsg_hst_vendor(struct fc_bsg_job *job)
3377{
3378 int command = job->request->rqst_data.h_vendor.vendor_cmd[0];
James Smart4cc0e562010-01-26 23:09:48 -05003379 int rc;
James Smartf1c3b0f2009-07-19 10:01:32 -04003380
3381 switch (command) {
3382 case LPFC_BSG_VENDOR_SET_CT_EVENT:
James Smart4cc0e562010-01-26 23:09:48 -05003383 rc = lpfc_bsg_hba_set_event(job);
James Smartf1c3b0f2009-07-19 10:01:32 -04003384 break;
James Smartf1c3b0f2009-07-19 10:01:32 -04003385 case LPFC_BSG_VENDOR_GET_CT_EVENT:
James Smart4cc0e562010-01-26 23:09:48 -05003386 rc = lpfc_bsg_hba_get_event(job);
James Smartf1c3b0f2009-07-19 10:01:32 -04003387 break;
James Smart3b5dd522010-01-26 23:10:15 -05003388 case LPFC_BSG_VENDOR_SEND_MGMT_RESP:
3389 rc = lpfc_bsg_send_mgmt_rsp(job);
3390 break;
3391 case LPFC_BSG_VENDOR_DIAG_MODE:
3392 rc = lpfc_bsg_diag_mode(job);
3393 break;
3394 case LPFC_BSG_VENDOR_DIAG_TEST:
3395 rc = lpfc_bsg_diag_test(job);
3396 break;
3397 case LPFC_BSG_VENDOR_GET_MGMT_REV:
3398 rc = lpfc_bsg_get_dfc_rev(job);
3399 break;
3400 case LPFC_BSG_VENDOR_MBOX:
3401 rc = lpfc_bsg_mbox_cmd(job);
3402 break;
James Smarte2aed292010-02-26 14:15:00 -05003403 case LPFC_BSG_VENDOR_MENLO_CMD:
3404 case LPFC_BSG_VENDOR_MENLO_DATA:
3405 rc = lpfc_menlo_cmd(job);
3406 break;
James Smartf1c3b0f2009-07-19 10:01:32 -04003407 default:
James Smart4cc0e562010-01-26 23:09:48 -05003408 rc = -EINVAL;
3409 job->reply->reply_payload_rcv_len = 0;
3410 /* make error code available to userspace */
3411 job->reply->result = rc;
3412 break;
James Smartf1c3b0f2009-07-19 10:01:32 -04003413 }
James Smart4cc0e562010-01-26 23:09:48 -05003414
3415 return rc;
James Smartf1c3b0f2009-07-19 10:01:32 -04003416}
3417
3418/**
3419 * lpfc_bsg_request - handle a bsg request from the FC transport
3420 * @job: fc_bsg_job to handle
James Smart3b5dd522010-01-26 23:10:15 -05003421 **/
James Smartf1c3b0f2009-07-19 10:01:32 -04003422int
3423lpfc_bsg_request(struct fc_bsg_job *job)
3424{
3425 uint32_t msgcode;
James Smart4cc0e562010-01-26 23:09:48 -05003426 int rc;
James Smartf1c3b0f2009-07-19 10:01:32 -04003427
3428 msgcode = job->request->msgcode;
James Smartf1c3b0f2009-07-19 10:01:32 -04003429 switch (msgcode) {
3430 case FC_BSG_HST_VENDOR:
3431 rc = lpfc_bsg_hst_vendor(job);
3432 break;
3433 case FC_BSG_RPT_ELS:
3434 rc = lpfc_bsg_rport_els(job);
3435 break;
3436 case FC_BSG_RPT_CT:
James Smart4cc0e562010-01-26 23:09:48 -05003437 rc = lpfc_bsg_send_mgmt_cmd(job);
James Smartf1c3b0f2009-07-19 10:01:32 -04003438 break;
3439 default:
James Smart4cc0e562010-01-26 23:09:48 -05003440 rc = -EINVAL;
3441 job->reply->reply_payload_rcv_len = 0;
3442 /* make error code available to userspace */
3443 job->reply->result = rc;
James Smartf1c3b0f2009-07-19 10:01:32 -04003444 break;
3445 }
3446
3447 return rc;
3448}
3449
3450/**
3451 * lpfc_bsg_timeout - handle timeout of a bsg request from the FC transport
3452 * @job: fc_bsg_job that has timed out
3453 *
3454 * This function just aborts the job's IOCB. The aborted IOCB will return to
3455 * the waiting function which will handle passing the error back to userspace
James Smart3b5dd522010-01-26 23:10:15 -05003456 **/
James Smartf1c3b0f2009-07-19 10:01:32 -04003457int
3458lpfc_bsg_timeout(struct fc_bsg_job *job)
3459{
3460 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
3461 struct lpfc_hba *phba = vport->phba;
James Smart4cc0e562010-01-26 23:09:48 -05003462 struct lpfc_iocbq *cmdiocb;
3463 struct lpfc_bsg_event *evt;
3464 struct lpfc_bsg_iocb *iocb;
James Smart3b5dd522010-01-26 23:10:15 -05003465 struct lpfc_bsg_mbox *mbox;
James Smarte2aed292010-02-26 14:15:00 -05003466 struct lpfc_bsg_menlo *menlo;
James Smartf1c3b0f2009-07-19 10:01:32 -04003467 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart4cc0e562010-01-26 23:09:48 -05003468 struct bsg_job_data *dd_data;
3469 unsigned long flags;
James Smartf1c3b0f2009-07-19 10:01:32 -04003470
James Smart4cc0e562010-01-26 23:09:48 -05003471 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3472 dd_data = (struct bsg_job_data *)job->dd_data;
3473 /* timeout and completion crossed paths if no dd_data */
3474 if (!dd_data) {
3475 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3476 return 0;
3477 }
3478
3479 switch (dd_data->type) {
3480 case TYPE_IOCB:
3481 iocb = &dd_data->context_un.iocb;
3482 cmdiocb = iocb->cmdiocbq;
3483 /* hint to completion handler that the job timed out */
3484 job->reply->result = -EAGAIN;
3485 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3486 /* this will call our completion handler */
3487 spin_lock_irq(&phba->hbalock);
James Smartf1c3b0f2009-07-19 10:01:32 -04003488 lpfc_sli_issue_abort_iotag(phba, pring, cmdiocb);
James Smart4cc0e562010-01-26 23:09:48 -05003489 spin_unlock_irq(&phba->hbalock);
3490 break;
3491 case TYPE_EVT:
3492 evt = dd_data->context_un.evt;
3493 /* this event has no job anymore */
3494 evt->set_job = NULL;
3495 job->dd_data = NULL;
3496 job->reply->reply_payload_rcv_len = 0;
3497 /* Return -EAGAIN which is our way of signallying the
3498 * app to retry.
3499 */
3500 job->reply->result = -EAGAIN;
3501 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3502 job->job_done(job);
3503 break;
James Smart3b5dd522010-01-26 23:10:15 -05003504 case TYPE_MBOX:
3505 mbox = &dd_data->context_un.mbox;
3506 /* this mbox has no job anymore */
3507 mbox->set_job = NULL;
3508 job->dd_data = NULL;
3509 job->reply->reply_payload_rcv_len = 0;
3510 job->reply->result = -EAGAIN;
James Smart7a470272010-03-15 11:25:20 -04003511 /* the mbox completion handler can now be run */
James Smart3b5dd522010-01-26 23:10:15 -05003512 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3513 job->job_done(job);
3514 break;
James Smarte2aed292010-02-26 14:15:00 -05003515 case TYPE_MENLO:
3516 menlo = &dd_data->context_un.menlo;
3517 cmdiocb = menlo->cmdiocbq;
3518 /* hint to completion handler that the job timed out */
3519 job->reply->result = -EAGAIN;
3520 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3521 /* this will call our completion handler */
3522 spin_lock_irq(&phba->hbalock);
3523 lpfc_sli_issue_abort_iotag(phba, pring, cmdiocb);
3524 spin_unlock_irq(&phba->hbalock);
3525 break;
James Smart4cc0e562010-01-26 23:09:48 -05003526 default:
3527 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3528 break;
3529 }
James Smartf1c3b0f2009-07-19 10:01:32 -04003530
James Smart4cc0e562010-01-26 23:09:48 -05003531 /* scsi transport fc fc_bsg_job_timeout expects a zero return code,
3532 * otherwise an error message will be displayed on the console
3533 * so always return success (zero)
3534 */
James Smartf1c3b0f2009-07-19 10:01:32 -04003535 return 0;
3536}