blob: 141a1ce9b74254503a15fb288c38134ed91c3888 [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 */
87 uint32_t outWxtWLen; /* 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{
165 unsigned long iflags;
166 struct bsg_job_data *dd_data;
167 struct fc_bsg_job *job;
168 IOCB_t *rsp;
169 struct lpfc_dmabuf *bmp;
170 struct lpfc_nodelist *ndlp;
171 struct lpfc_bsg_iocb *iocb;
172 unsigned long flags;
173 int rc = 0;
174
175 spin_lock_irqsave(&phba->ct_ev_lock, flags);
176 dd_data = cmdiocbq->context1;
177 if (!dd_data) {
178 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
179 return;
180 }
181
182 iocb = &dd_data->context_un.iocb;
183 job = iocb->set_job;
184 job->dd_data = NULL; /* so timeout handler does not reply */
185
186 spin_lock_irqsave(&phba->hbalock, iflags);
187 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
188 if (cmdiocbq->context2 && rspiocbq)
189 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
190 &rspiocbq->iocb, sizeof(IOCB_t));
191 spin_unlock_irqrestore(&phba->hbalock, iflags);
192
193 bmp = iocb->bmp;
194 rspiocbq = iocb->rspiocbq;
195 rsp = &rspiocbq->iocb;
196 ndlp = iocb->ndlp;
197
198 pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
199 job->request_payload.sg_cnt, DMA_TO_DEVICE);
200 pci_unmap_sg(phba->pcidev, job->reply_payload.sg_list,
201 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
202
203 if (rsp->ulpStatus) {
204 if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
205 switch (rsp->un.ulpWord[4] & 0xff) {
206 case IOERR_SEQUENCE_TIMEOUT:
207 rc = -ETIMEDOUT;
208 break;
209 case IOERR_INVALID_RPI:
210 rc = -EFAULT;
211 break;
212 default:
213 rc = -EACCES;
214 break;
215 }
216 } else
217 rc = -EACCES;
218 } else
219 job->reply->reply_payload_rcv_len =
220 rsp->un.genreq64.bdl.bdeSize;
221
222 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
223 lpfc_sli_release_iocbq(phba, rspiocbq);
224 lpfc_sli_release_iocbq(phba, cmdiocbq);
225 lpfc_nlp_put(ndlp);
226 kfree(bmp);
227 kfree(dd_data);
228 /* make error code available to userspace */
229 job->reply->result = rc;
230 /* complete the job back to userspace */
231 job->job_done(job);
232 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
233 return;
234}
235
236/**
237 * lpfc_bsg_send_mgmt_cmd - send a CT command from a bsg request
James Smartf1c3b0f2009-07-19 10:01:32 -0400238 * @job: fc_bsg_job to handle
James Smart3b5dd522010-01-26 23:10:15 -0500239 **/
James Smartf1c3b0f2009-07-19 10:01:32 -0400240static int
James Smart4cc0e562010-01-26 23:09:48 -0500241lpfc_bsg_send_mgmt_cmd(struct fc_bsg_job *job)
James Smartf1c3b0f2009-07-19 10:01:32 -0400242{
James Smartf1c3b0f2009-07-19 10:01:32 -0400243 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
244 struct lpfc_hba *phba = vport->phba;
245 struct lpfc_rport_data *rdata = job->rport->dd_data;
246 struct lpfc_nodelist *ndlp = rdata->pnode;
247 struct ulp_bde64 *bpl = NULL;
248 uint32_t timeout;
249 struct lpfc_iocbq *cmdiocbq = NULL;
250 struct lpfc_iocbq *rspiocbq = NULL;
251 IOCB_t *cmd;
252 IOCB_t *rsp;
253 struct lpfc_dmabuf *bmp = NULL;
254 int request_nseg;
255 int reply_nseg;
256 struct scatterlist *sgel = NULL;
257 int numbde;
258 dma_addr_t busaddr;
James Smart4cc0e562010-01-26 23:09:48 -0500259 struct bsg_job_data *dd_data;
260 uint32_t creg_val;
James Smartf1c3b0f2009-07-19 10:01:32 -0400261 int rc = 0;
262
263 /* in case no data is transferred */
264 job->reply->reply_payload_rcv_len = 0;
265
James Smart4cc0e562010-01-26 23:09:48 -0500266 /* allocate our bsg tracking structure */
267 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
268 if (!dd_data) {
269 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
270 "2733 Failed allocation of dd_data\n");
271 rc = -ENOMEM;
272 goto no_dd_data;
273 }
274
James Smartf1c3b0f2009-07-19 10:01:32 -0400275 if (!lpfc_nlp_get(ndlp)) {
James Smart4cc0e562010-01-26 23:09:48 -0500276 rc = -ENODEV;
277 goto no_ndlp;
278 }
279
280 bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
281 if (!bmp) {
282 rc = -ENOMEM;
283 goto free_ndlp;
James Smartf1c3b0f2009-07-19 10:01:32 -0400284 }
285
286 if (ndlp->nlp_flag & NLP_ELS_SND_MASK) {
287 rc = -ENODEV;
James Smart4cc0e562010-01-26 23:09:48 -0500288 goto free_bmp;
James Smartf1c3b0f2009-07-19 10:01:32 -0400289 }
290
James Smartf1c3b0f2009-07-19 10:01:32 -0400291 cmdiocbq = lpfc_sli_get_iocbq(phba);
292 if (!cmdiocbq) {
293 rc = -ENOMEM;
James Smart4cc0e562010-01-26 23:09:48 -0500294 goto free_bmp;
James Smartf1c3b0f2009-07-19 10:01:32 -0400295 }
James Smartf1c3b0f2009-07-19 10:01:32 -0400296
James Smart4cc0e562010-01-26 23:09:48 -0500297 cmd = &cmdiocbq->iocb;
James Smartf1c3b0f2009-07-19 10:01:32 -0400298 rspiocbq = lpfc_sli_get_iocbq(phba);
299 if (!rspiocbq) {
300 rc = -ENOMEM;
301 goto free_cmdiocbq;
302 }
James Smartf1c3b0f2009-07-19 10:01:32 -0400303
304 rsp = &rspiocbq->iocb;
James Smartf1c3b0f2009-07-19 10:01:32 -0400305 bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
306 if (!bmp->virt) {
307 rc = -ENOMEM;
James Smart4cc0e562010-01-26 23:09:48 -0500308 goto free_rspiocbq;
James Smartf1c3b0f2009-07-19 10:01:32 -0400309 }
James Smartf1c3b0f2009-07-19 10:01:32 -0400310
311 INIT_LIST_HEAD(&bmp->list);
312 bpl = (struct ulp_bde64 *) bmp->virt;
James Smartf1c3b0f2009-07-19 10:01:32 -0400313 request_nseg = pci_map_sg(phba->pcidev, job->request_payload.sg_list,
314 job->request_payload.sg_cnt, DMA_TO_DEVICE);
315 for_each_sg(job->request_payload.sg_list, sgel, request_nseg, numbde) {
316 busaddr = sg_dma_address(sgel);
317 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
318 bpl->tus.f.bdeSize = sg_dma_len(sgel);
319 bpl->tus.w = cpu_to_le32(bpl->tus.w);
320 bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
321 bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
322 bpl++;
323 }
324
325 reply_nseg = pci_map_sg(phba->pcidev, job->reply_payload.sg_list,
326 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
327 for_each_sg(job->reply_payload.sg_list, sgel, reply_nseg, numbde) {
328 busaddr = sg_dma_address(sgel);
329 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
330 bpl->tus.f.bdeSize = sg_dma_len(sgel);
331 bpl->tus.w = cpu_to_le32(bpl->tus.w);
332 bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
333 bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
334 bpl++;
335 }
336
337 cmd->un.genreq64.bdl.ulpIoTag32 = 0;
338 cmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
339 cmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
340 cmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
341 cmd->un.genreq64.bdl.bdeSize =
342 (request_nseg + reply_nseg) * sizeof(struct ulp_bde64);
343 cmd->ulpCommand = CMD_GEN_REQUEST64_CR;
344 cmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
345 cmd->un.genreq64.w5.hcsw.Dfctl = 0;
James Smart6a9c52c2009-10-02 15:16:51 -0400346 cmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
347 cmd->un.genreq64.w5.hcsw.Type = FC_TYPE_CT;
James Smartf1c3b0f2009-07-19 10:01:32 -0400348 cmd->ulpBdeCount = 1;
349 cmd->ulpLe = 1;
350 cmd->ulpClass = CLASS3;
351 cmd->ulpContext = ndlp->nlp_rpi;
352 cmd->ulpOwner = OWN_CHIP;
353 cmdiocbq->vport = phba->pport;
James Smart4cc0e562010-01-26 23:09:48 -0500354 cmdiocbq->context3 = bmp;
James Smartf1c3b0f2009-07-19 10:01:32 -0400355 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
James Smartf1c3b0f2009-07-19 10:01:32 -0400356 timeout = phba->fc_ratov * 2;
James Smart4cc0e562010-01-26 23:09:48 -0500357 cmd->ulpTimeout = timeout;
James Smartf1c3b0f2009-07-19 10:01:32 -0400358
James Smart4cc0e562010-01-26 23:09:48 -0500359 cmdiocbq->iocb_cmpl = lpfc_bsg_send_mgmt_cmd_cmp;
360 cmdiocbq->context1 = dd_data;
361 cmdiocbq->context2 = rspiocbq;
362 dd_data->type = TYPE_IOCB;
363 dd_data->context_un.iocb.cmdiocbq = cmdiocbq;
364 dd_data->context_un.iocb.rspiocbq = rspiocbq;
365 dd_data->context_un.iocb.set_job = job;
366 dd_data->context_un.iocb.bmp = bmp;
367 dd_data->context_un.iocb.ndlp = ndlp;
James Smartf1c3b0f2009-07-19 10:01:32 -0400368
James Smart4cc0e562010-01-26 23:09:48 -0500369 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
370 creg_val = readl(phba->HCregaddr);
371 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
372 writel(creg_val, phba->HCregaddr);
373 readl(phba->HCregaddr); /* flush */
James Smartf1c3b0f2009-07-19 10:01:32 -0400374 }
375
James Smart4cc0e562010-01-26 23:09:48 -0500376 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq, 0);
James Smartf1c3b0f2009-07-19 10:01:32 -0400377
James Smart4cc0e562010-01-26 23:09:48 -0500378 if (rc == IOCB_SUCCESS)
379 return 0; /* done for now */
James Smartf1c3b0f2009-07-19 10:01:32 -0400380
James Smart4cc0e562010-01-26 23:09:48 -0500381 /* iocb failed so cleanup */
382 pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
383 job->request_payload.sg_cnt, DMA_TO_DEVICE);
384 pci_unmap_sg(phba->pcidev, job->reply_payload.sg_list,
385 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
James Smartf1c3b0f2009-07-19 10:01:32 -0400386
James Smartf1c3b0f2009-07-19 10:01:32 -0400387 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
James Smart4cc0e562010-01-26 23:09:48 -0500388
James Smartf1c3b0f2009-07-19 10:01:32 -0400389free_rspiocbq:
390 lpfc_sli_release_iocbq(phba, rspiocbq);
391free_cmdiocbq:
392 lpfc_sli_release_iocbq(phba, cmdiocbq);
James Smart4cc0e562010-01-26 23:09:48 -0500393free_bmp:
394 kfree(bmp);
395free_ndlp:
James Smartf1c3b0f2009-07-19 10:01:32 -0400396 lpfc_nlp_put(ndlp);
James Smart4cc0e562010-01-26 23:09:48 -0500397no_ndlp:
398 kfree(dd_data);
399no_dd_data:
James Smartf1c3b0f2009-07-19 10:01:32 -0400400 /* make error code available to userspace */
401 job->reply->result = rc;
James Smart4cc0e562010-01-26 23:09:48 -0500402 job->dd_data = NULL;
403 return rc;
404}
405
406/**
407 * lpfc_bsg_rport_els_cmp - lpfc_bsg_rport_els's completion handler
408 * @phba: Pointer to HBA context object.
409 * @cmdiocbq: Pointer to command iocb.
410 * @rspiocbq: Pointer to response iocb.
411 *
412 * This function is the completion handler for iocbs issued using
413 * lpfc_bsg_rport_els_cmp function. This function is called by the
414 * ring event handler function without any lock held. This function
415 * can be called from both worker thread context and interrupt
416 * context. This function also can be called from other thread which
417 * cleans up the SLI layer objects.
James Smart3b5dd522010-01-26 23:10:15 -0500418 * This function copies the contents of the response iocb to the
James Smart4cc0e562010-01-26 23:09:48 -0500419 * response iocb memory object provided by the caller of
420 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
421 * sleeps for the iocb completion.
422 **/
423static void
424lpfc_bsg_rport_els_cmp(struct lpfc_hba *phba,
425 struct lpfc_iocbq *cmdiocbq,
426 struct lpfc_iocbq *rspiocbq)
427{
428 struct bsg_job_data *dd_data;
429 struct fc_bsg_job *job;
430 IOCB_t *rsp;
431 struct lpfc_nodelist *ndlp;
432 struct lpfc_dmabuf *pbuflist = NULL;
433 struct fc_bsg_ctels_reply *els_reply;
434 uint8_t *rjt_data;
435 unsigned long flags;
436 int rc = 0;
437
438 spin_lock_irqsave(&phba->ct_ev_lock, flags);
439 dd_data = cmdiocbq->context1;
440 /* normal completion and timeout crossed paths, already done */
441 if (!dd_data) {
Jiri Slaby67221a42010-03-16 16:23:58 +0100442 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smart4cc0e562010-01-26 23:09:48 -0500443 return;
444 }
445
446 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
447 if (cmdiocbq->context2 && rspiocbq)
448 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
449 &rspiocbq->iocb, sizeof(IOCB_t));
450
451 job = dd_data->context_un.iocb.set_job;
452 cmdiocbq = dd_data->context_un.iocb.cmdiocbq;
453 rspiocbq = dd_data->context_un.iocb.rspiocbq;
454 rsp = &rspiocbq->iocb;
455 ndlp = dd_data->context_un.iocb.ndlp;
456
457 pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
458 job->request_payload.sg_cnt, DMA_TO_DEVICE);
459 pci_unmap_sg(phba->pcidev, job->reply_payload.sg_list,
460 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
461
462 if (job->reply->result == -EAGAIN)
463 rc = -EAGAIN;
464 else if (rsp->ulpStatus == IOSTAT_SUCCESS)
465 job->reply->reply_payload_rcv_len =
466 rsp->un.elsreq64.bdl.bdeSize;
467 else if (rsp->ulpStatus == IOSTAT_LS_RJT) {
468 job->reply->reply_payload_rcv_len =
469 sizeof(struct fc_bsg_ctels_reply);
470 /* LS_RJT data returned in word 4 */
471 rjt_data = (uint8_t *)&rsp->un.ulpWord[4];
472 els_reply = &job->reply->reply_data.ctels_reply;
473 els_reply->status = FC_CTELS_STATUS_REJECT;
474 els_reply->rjt_data.action = rjt_data[3];
475 els_reply->rjt_data.reason_code = rjt_data[2];
476 els_reply->rjt_data.reason_explanation = rjt_data[1];
477 els_reply->rjt_data.vendor_unique = rjt_data[0];
478 } else
479 rc = -EIO;
480
481 pbuflist = (struct lpfc_dmabuf *) cmdiocbq->context3;
482 lpfc_mbuf_free(phba, pbuflist->virt, pbuflist->phys);
483 lpfc_sli_release_iocbq(phba, rspiocbq);
484 lpfc_sli_release_iocbq(phba, cmdiocbq);
485 lpfc_nlp_put(ndlp);
486 kfree(dd_data);
487 /* make error code available to userspace */
488 job->reply->result = rc;
489 job->dd_data = NULL;
James Smartf1c3b0f2009-07-19 10:01:32 -0400490 /* complete the job back to userspace */
491 job->job_done(job);
James Smart4cc0e562010-01-26 23:09:48 -0500492 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
493 return;
James Smartf1c3b0f2009-07-19 10:01:32 -0400494}
495
496/**
497 * lpfc_bsg_rport_els - send an ELS command from a bsg request
498 * @job: fc_bsg_job to handle
James Smart3b5dd522010-01-26 23:10:15 -0500499 **/
James Smartf1c3b0f2009-07-19 10:01:32 -0400500static int
501lpfc_bsg_rport_els(struct fc_bsg_job *job)
502{
503 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
504 struct lpfc_hba *phba = vport->phba;
505 struct lpfc_rport_data *rdata = job->rport->dd_data;
506 struct lpfc_nodelist *ndlp = rdata->pnode;
James Smartf1c3b0f2009-07-19 10:01:32 -0400507 uint32_t elscmd;
508 uint32_t cmdsize;
509 uint32_t rspsize;
510 struct lpfc_iocbq *rspiocbq;
511 struct lpfc_iocbq *cmdiocbq;
512 IOCB_t *rsp;
513 uint16_t rpi = 0;
514 struct lpfc_dmabuf *pcmd;
515 struct lpfc_dmabuf *prsp;
516 struct lpfc_dmabuf *pbuflist = NULL;
517 struct ulp_bde64 *bpl;
James Smartf1c3b0f2009-07-19 10:01:32 -0400518 int request_nseg;
519 int reply_nseg;
520 struct scatterlist *sgel = NULL;
521 int numbde;
522 dma_addr_t busaddr;
James Smart4cc0e562010-01-26 23:09:48 -0500523 struct bsg_job_data *dd_data;
524 uint32_t creg_val;
James Smartf1c3b0f2009-07-19 10:01:32 -0400525 int rc = 0;
526
527 /* in case no data is transferred */
528 job->reply->reply_payload_rcv_len = 0;
529
James Smart4cc0e562010-01-26 23:09:48 -0500530 /* allocate our bsg tracking structure */
531 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
532 if (!dd_data) {
533 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
534 "2735 Failed allocation of dd_data\n");
535 rc = -ENOMEM;
536 goto no_dd_data;
537 }
538
James Smartf1c3b0f2009-07-19 10:01:32 -0400539 if (!lpfc_nlp_get(ndlp)) {
540 rc = -ENODEV;
James Smart4cc0e562010-01-26 23:09:48 -0500541 goto free_dd_data;
James Smartf1c3b0f2009-07-19 10:01:32 -0400542 }
543
544 elscmd = job->request->rqst_data.r_els.els_code;
545 cmdsize = job->request_payload.payload_len;
546 rspsize = job->reply_payload.payload_len;
547 rspiocbq = lpfc_sli_get_iocbq(phba);
548 if (!rspiocbq) {
549 lpfc_nlp_put(ndlp);
550 rc = -ENOMEM;
James Smart4cc0e562010-01-26 23:09:48 -0500551 goto free_dd_data;
James Smartf1c3b0f2009-07-19 10:01:32 -0400552 }
553
554 rsp = &rspiocbq->iocb;
555 rpi = ndlp->nlp_rpi;
556
James Smart4cc0e562010-01-26 23:09:48 -0500557 cmdiocbq = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp,
James Smartf1c3b0f2009-07-19 10:01:32 -0400558 ndlp->nlp_DID, elscmd);
James Smartf1c3b0f2009-07-19 10:01:32 -0400559 if (!cmdiocbq) {
James Smart4cc0e562010-01-26 23:09:48 -0500560 rc = -EIO;
561 goto free_rspiocbq;
James Smartf1c3b0f2009-07-19 10:01:32 -0400562 }
563
James Smart4cc0e562010-01-26 23:09:48 -0500564 /* prep els iocb set context1 to the ndlp, context2 to the command
James Smart3b5dd522010-01-26 23:10:15 -0500565 * dmabuf, context3 holds the data dmabuf
566 */
James Smartf1c3b0f2009-07-19 10:01:32 -0400567 pcmd = (struct lpfc_dmabuf *) cmdiocbq->context2;
568 prsp = (struct lpfc_dmabuf *) pcmd->list.next;
James Smartf1c3b0f2009-07-19 10:01:32 -0400569 lpfc_mbuf_free(phba, pcmd->virt, pcmd->phys);
570 kfree(pcmd);
571 lpfc_mbuf_free(phba, prsp->virt, prsp->phys);
572 kfree(prsp);
573 cmdiocbq->context2 = NULL;
574
575 pbuflist = (struct lpfc_dmabuf *) cmdiocbq->context3;
576 bpl = (struct ulp_bde64 *) pbuflist->virt;
577
578 request_nseg = pci_map_sg(phba->pcidev, job->request_payload.sg_list,
579 job->request_payload.sg_cnt, DMA_TO_DEVICE);
James Smartf1c3b0f2009-07-19 10:01:32 -0400580 for_each_sg(job->request_payload.sg_list, sgel, request_nseg, numbde) {
581 busaddr = sg_dma_address(sgel);
582 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
583 bpl->tus.f.bdeSize = sg_dma_len(sgel);
584 bpl->tus.w = cpu_to_le32(bpl->tus.w);
585 bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
586 bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
587 bpl++;
588 }
589
590 reply_nseg = pci_map_sg(phba->pcidev, job->reply_payload.sg_list,
591 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
592 for_each_sg(job->reply_payload.sg_list, sgel, reply_nseg, numbde) {
593 busaddr = sg_dma_address(sgel);
594 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
595 bpl->tus.f.bdeSize = sg_dma_len(sgel);
596 bpl->tus.w = cpu_to_le32(bpl->tus.w);
597 bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
598 bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
599 bpl++;
600 }
James Smartf1c3b0f2009-07-19 10:01:32 -0400601 cmdiocbq->iocb.un.elsreq64.bdl.bdeSize =
602 (request_nseg + reply_nseg) * sizeof(struct ulp_bde64);
603 cmdiocbq->iocb.ulpContext = rpi;
604 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
605 cmdiocbq->context1 = NULL;
606 cmdiocbq->context2 = NULL;
607
James Smart4cc0e562010-01-26 23:09:48 -0500608 cmdiocbq->iocb_cmpl = lpfc_bsg_rport_els_cmp;
609 cmdiocbq->context1 = dd_data;
610 cmdiocbq->context2 = rspiocbq;
611 dd_data->type = TYPE_IOCB;
612 dd_data->context_un.iocb.cmdiocbq = cmdiocbq;
613 dd_data->context_un.iocb.rspiocbq = rspiocbq;
614 dd_data->context_un.iocb.set_job = job;
615 dd_data->context_un.iocb.bmp = NULL;;
616 dd_data->context_un.iocb.ndlp = ndlp;
James Smartf1c3b0f2009-07-19 10:01:32 -0400617
James Smart4cc0e562010-01-26 23:09:48 -0500618 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
619 creg_val = readl(phba->HCregaddr);
620 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
621 writel(creg_val, phba->HCregaddr);
622 readl(phba->HCregaddr); /* flush */
James Smartf1c3b0f2009-07-19 10:01:32 -0400623 }
James Smart4cc0e562010-01-26 23:09:48 -0500624 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq, 0);
625 lpfc_nlp_put(ndlp);
626 if (rc == IOCB_SUCCESS)
627 return 0; /* done for now */
James Smartf1c3b0f2009-07-19 10:01:32 -0400628
James Smart4cc0e562010-01-26 23:09:48 -0500629 pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
630 job->request_payload.sg_cnt, DMA_TO_DEVICE);
631 pci_unmap_sg(phba->pcidev, job->reply_payload.sg_list,
632 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
James Smartf1c3b0f2009-07-19 10:01:32 -0400633
James Smart4cc0e562010-01-26 23:09:48 -0500634 lpfc_mbuf_free(phba, pbuflist->virt, pbuflist->phys);
James Smartf1c3b0f2009-07-19 10:01:32 -0400635
James Smart4cc0e562010-01-26 23:09:48 -0500636 lpfc_sli_release_iocbq(phba, cmdiocbq);
James Smartf1c3b0f2009-07-19 10:01:32 -0400637
James Smart4cc0e562010-01-26 23:09:48 -0500638free_rspiocbq:
James Smartf1c3b0f2009-07-19 10:01:32 -0400639 lpfc_sli_release_iocbq(phba, rspiocbq);
640
James Smart4cc0e562010-01-26 23:09:48 -0500641free_dd_data:
642 kfree(dd_data);
643
644no_dd_data:
James Smartf1c3b0f2009-07-19 10:01:32 -0400645 /* make error code available to userspace */
646 job->reply->result = rc;
James Smart4cc0e562010-01-26 23:09:48 -0500647 job->dd_data = NULL;
648 return rc;
James Smartf1c3b0f2009-07-19 10:01:32 -0400649}
650
James Smart3b5dd522010-01-26 23:10:15 -0500651/**
652 * lpfc_bsg_event_free - frees an allocated event structure
653 * @kref: Pointer to a kref.
654 *
655 * Called from kref_put. Back cast the kref into an event structure address.
656 * Free any events to get, delete associated nodes, free any events to see,
657 * free any data then free the event itself.
658 **/
James Smartf1c3b0f2009-07-19 10:01:32 -0400659static void
James Smart4cc0e562010-01-26 23:09:48 -0500660lpfc_bsg_event_free(struct kref *kref)
James Smartf1c3b0f2009-07-19 10:01:32 -0400661{
James Smart4cc0e562010-01-26 23:09:48 -0500662 struct lpfc_bsg_event *evt = container_of(kref, struct lpfc_bsg_event,
663 kref);
James Smartf1c3b0f2009-07-19 10:01:32 -0400664 struct event_data *ed;
665
666 list_del(&evt->node);
667
668 while (!list_empty(&evt->events_to_get)) {
669 ed = list_entry(evt->events_to_get.next, typeof(*ed), node);
670 list_del(&ed->node);
671 kfree(ed->data);
672 kfree(ed);
673 }
674
675 while (!list_empty(&evt->events_to_see)) {
676 ed = list_entry(evt->events_to_see.next, typeof(*ed), node);
677 list_del(&ed->node);
678 kfree(ed->data);
679 kfree(ed);
680 }
681
682 kfree(evt);
683}
684
James Smart3b5dd522010-01-26 23:10:15 -0500685/**
686 * lpfc_bsg_event_ref - increments the kref for an event
687 * @evt: Pointer to an event structure.
688 **/
James Smartf1c3b0f2009-07-19 10:01:32 -0400689static inline void
James Smart4cc0e562010-01-26 23:09:48 -0500690lpfc_bsg_event_ref(struct lpfc_bsg_event *evt)
James Smartf1c3b0f2009-07-19 10:01:32 -0400691{
James Smart4cc0e562010-01-26 23:09:48 -0500692 kref_get(&evt->kref);
James Smartf1c3b0f2009-07-19 10:01:32 -0400693}
694
James Smart3b5dd522010-01-26 23:10:15 -0500695/**
696 * lpfc_bsg_event_unref - Uses kref_put to free an event structure
697 * @evt: Pointer to an event structure.
698 **/
James Smartf1c3b0f2009-07-19 10:01:32 -0400699static inline void
James Smart4cc0e562010-01-26 23:09:48 -0500700lpfc_bsg_event_unref(struct lpfc_bsg_event *evt)
James Smartf1c3b0f2009-07-19 10:01:32 -0400701{
James Smart4cc0e562010-01-26 23:09:48 -0500702 kref_put(&evt->kref, lpfc_bsg_event_free);
James Smartf1c3b0f2009-07-19 10:01:32 -0400703}
704
James Smart3b5dd522010-01-26 23:10:15 -0500705/**
706 * lpfc_bsg_event_new - allocate and initialize a event structure
707 * @ev_mask: Mask of events.
708 * @ev_reg_id: Event reg id.
709 * @ev_req_id: Event request id.
710 **/
James Smart4cc0e562010-01-26 23:09:48 -0500711static struct lpfc_bsg_event *
712lpfc_bsg_event_new(uint32_t ev_mask, int ev_reg_id, uint32_t ev_req_id)
713{
714 struct lpfc_bsg_event *evt = kzalloc(sizeof(*evt), GFP_KERNEL);
James Smartf1c3b0f2009-07-19 10:01:32 -0400715
James Smart4cc0e562010-01-26 23:09:48 -0500716 if (!evt)
717 return NULL;
718
719 INIT_LIST_HEAD(&evt->events_to_get);
720 INIT_LIST_HEAD(&evt->events_to_see);
721 evt->type_mask = ev_mask;
722 evt->req_id = ev_req_id;
723 evt->reg_id = ev_reg_id;
724 evt->wait_time_stamp = jiffies;
725 init_waitqueue_head(&evt->wq);
726 kref_init(&evt->kref);
727 return evt;
728}
729
James Smart3b5dd522010-01-26 23:10:15 -0500730/**
731 * diag_cmd_data_free - Frees an lpfc dma buffer extension
732 * @phba: Pointer to HBA context object.
733 * @mlist: Pointer to an lpfc dma buffer extension.
734 **/
James Smart4cc0e562010-01-26 23:09:48 -0500735static int
James Smart3b5dd522010-01-26 23:10:15 -0500736diag_cmd_data_free(struct lpfc_hba *phba, struct lpfc_dmabufext *mlist)
James Smart4cc0e562010-01-26 23:09:48 -0500737{
738 struct lpfc_dmabufext *mlast;
739 struct pci_dev *pcidev;
740 struct list_head head, *curr, *next;
741
742 if ((!mlist) || (!lpfc_is_link_up(phba) &&
743 (phba->link_flag & LS_LOOPBACK_MODE))) {
744 return 0;
745 }
746
747 pcidev = phba->pcidev;
748 list_add_tail(&head, &mlist->dma.list);
749
750 list_for_each_safe(curr, next, &head) {
751 mlast = list_entry(curr, struct lpfc_dmabufext , dma.list);
752 if (mlast->dma.virt)
753 dma_free_coherent(&pcidev->dev,
754 mlast->size,
755 mlast->dma.virt,
756 mlast->dma.phys);
757 kfree(mlast);
758 }
759 return 0;
760}
James Smartf1c3b0f2009-07-19 10:01:32 -0400761
762/**
763 * lpfc_bsg_ct_unsol_event - process an unsolicited CT command
764 * @phba:
765 * @pring:
766 * @piocbq:
767 *
768 * This function is called when an unsolicited CT command is received. It
James Smart4cc0e562010-01-26 23:09:48 -0500769 * forwards the event to any processes registered to receive CT events.
James Smart3b5dd522010-01-26 23:10:15 -0500770 **/
James Smart4fede782010-01-26 23:08:55 -0500771int
James Smartf1c3b0f2009-07-19 10:01:32 -0400772lpfc_bsg_ct_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
773 struct lpfc_iocbq *piocbq)
774{
775 uint32_t evt_req_id = 0;
776 uint32_t cmd;
777 uint32_t len;
778 struct lpfc_dmabuf *dmabuf = NULL;
James Smart4cc0e562010-01-26 23:09:48 -0500779 struct lpfc_bsg_event *evt;
James Smartf1c3b0f2009-07-19 10:01:32 -0400780 struct event_data *evt_dat = NULL;
781 struct lpfc_iocbq *iocbq;
782 size_t offset = 0;
783 struct list_head head;
784 struct ulp_bde64 *bde;
785 dma_addr_t dma_addr;
786 int i;
787 struct lpfc_dmabuf *bdeBuf1 = piocbq->context2;
788 struct lpfc_dmabuf *bdeBuf2 = piocbq->context3;
789 struct lpfc_hbq_entry *hbqe;
790 struct lpfc_sli_ct_request *ct_req;
James Smart4cc0e562010-01-26 23:09:48 -0500791 struct fc_bsg_job *job = NULL;
James Smart4fede782010-01-26 23:08:55 -0500792 unsigned long flags;
James Smart4cc0e562010-01-26 23:09:48 -0500793 int size = 0;
James Smartf1c3b0f2009-07-19 10:01:32 -0400794
795 INIT_LIST_HEAD(&head);
796 list_add_tail(&head, &piocbq->list);
797
798 if (piocbq->iocb.ulpBdeCount == 0 ||
799 piocbq->iocb.un.cont64[0].tus.f.bdeSize == 0)
800 goto error_ct_unsol_exit;
801
James Smart4cc0e562010-01-26 23:09:48 -0500802 if (phba->link_state == LPFC_HBA_ERROR ||
803 (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)))
804 goto error_ct_unsol_exit;
805
James Smartf1c3b0f2009-07-19 10:01:32 -0400806 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
807 dmabuf = bdeBuf1;
808 else {
809 dma_addr = getPaddr(piocbq->iocb.un.cont64[0].addrHigh,
810 piocbq->iocb.un.cont64[0].addrLow);
811 dmabuf = lpfc_sli_ringpostbuf_get(phba, pring, dma_addr);
812 }
James Smart4cc0e562010-01-26 23:09:48 -0500813 if (dmabuf == NULL)
814 goto error_ct_unsol_exit;
James Smartf1c3b0f2009-07-19 10:01:32 -0400815 ct_req = (struct lpfc_sli_ct_request *)dmabuf->virt;
816 evt_req_id = ct_req->FsType;
817 cmd = ct_req->CommandResponse.bits.CmdRsp;
818 len = ct_req->CommandResponse.bits.Size;
819 if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
820 lpfc_sli_ringpostbuf_put(phba, pring, dmabuf);
821
James Smart4fede782010-01-26 23:08:55 -0500822 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -0400823 list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
James Smart4cc0e562010-01-26 23:09:48 -0500824 if (!(evt->type_mask & FC_REG_CT_EVENT) ||
825 evt->req_id != evt_req_id)
James Smartf1c3b0f2009-07-19 10:01:32 -0400826 continue;
827
James Smart4cc0e562010-01-26 23:09:48 -0500828 lpfc_bsg_event_ref(evt);
829 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -0400830 evt_dat = kzalloc(sizeof(*evt_dat), GFP_KERNEL);
James Smart4cc0e562010-01-26 23:09:48 -0500831 if (evt_dat == NULL) {
832 spin_lock_irqsave(&phba->ct_ev_lock, flags);
833 lpfc_bsg_event_unref(evt);
James Smartf1c3b0f2009-07-19 10:01:32 -0400834 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
835 "2614 Memory allocation failed for "
836 "CT event\n");
837 break;
838 }
839
James Smartf1c3b0f2009-07-19 10:01:32 -0400840 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
841 /* take accumulated byte count from the last iocbq */
842 iocbq = list_entry(head.prev, typeof(*iocbq), list);
843 evt_dat->len = iocbq->iocb.unsli3.rcvsli3.acc_len;
844 } else {
845 list_for_each_entry(iocbq, &head, list) {
846 for (i = 0; i < iocbq->iocb.ulpBdeCount; i++)
847 evt_dat->len +=
848 iocbq->iocb.un.cont64[i].tus.f.bdeSize;
849 }
850 }
851
852 evt_dat->data = kzalloc(evt_dat->len, GFP_KERNEL);
James Smart4cc0e562010-01-26 23:09:48 -0500853 if (evt_dat->data == NULL) {
James Smartf1c3b0f2009-07-19 10:01:32 -0400854 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
855 "2615 Memory allocation failed for "
856 "CT event data, size %d\n",
857 evt_dat->len);
858 kfree(evt_dat);
James Smart4fede782010-01-26 23:08:55 -0500859 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smart4cc0e562010-01-26 23:09:48 -0500860 lpfc_bsg_event_unref(evt);
James Smart4fede782010-01-26 23:08:55 -0500861 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -0400862 goto error_ct_unsol_exit;
863 }
864
865 list_for_each_entry(iocbq, &head, list) {
James Smart4cc0e562010-01-26 23:09:48 -0500866 size = 0;
James Smartf1c3b0f2009-07-19 10:01:32 -0400867 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
868 bdeBuf1 = iocbq->context2;
869 bdeBuf2 = iocbq->context3;
870 }
871 for (i = 0; i < iocbq->iocb.ulpBdeCount; i++) {
James Smartf1c3b0f2009-07-19 10:01:32 -0400872 if (phba->sli3_options &
873 LPFC_SLI3_HBQ_ENABLED) {
874 if (i == 0) {
875 hbqe = (struct lpfc_hbq_entry *)
876 &iocbq->iocb.un.ulpWord[0];
877 size = hbqe->bde.tus.f.bdeSize;
878 dmabuf = bdeBuf1;
879 } else if (i == 1) {
880 hbqe = (struct lpfc_hbq_entry *)
881 &iocbq->iocb.unsli3.
882 sli3Words[4];
883 size = hbqe->bde.tus.f.bdeSize;
884 dmabuf = bdeBuf2;
885 }
886 if ((offset + size) > evt_dat->len)
887 size = evt_dat->len - offset;
888 } else {
889 size = iocbq->iocb.un.cont64[i].
890 tus.f.bdeSize;
891 bde = &iocbq->iocb.un.cont64[i];
892 dma_addr = getPaddr(bde->addrHigh,
893 bde->addrLow);
894 dmabuf = lpfc_sli_ringpostbuf_get(phba,
895 pring, dma_addr);
896 }
897 if (!dmabuf) {
898 lpfc_printf_log(phba, KERN_ERR,
899 LOG_LIBDFC, "2616 No dmabuf "
900 "found for iocbq 0x%p\n",
901 iocbq);
902 kfree(evt_dat->data);
903 kfree(evt_dat);
James Smart4fede782010-01-26 23:08:55 -0500904 spin_lock_irqsave(&phba->ct_ev_lock,
905 flags);
James Smart4cc0e562010-01-26 23:09:48 -0500906 lpfc_bsg_event_unref(evt);
James Smart4fede782010-01-26 23:08:55 -0500907 spin_unlock_irqrestore(
908 &phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -0400909 goto error_ct_unsol_exit;
910 }
911 memcpy((char *)(evt_dat->data) + offset,
912 dmabuf->virt, size);
913 offset += size;
914 if (evt_req_id != SLI_CT_ELX_LOOPBACK &&
915 !(phba->sli3_options &
916 LPFC_SLI3_HBQ_ENABLED)) {
917 lpfc_sli_ringpostbuf_put(phba, pring,
918 dmabuf);
919 } else {
920 switch (cmd) {
James Smart4cc0e562010-01-26 23:09:48 -0500921 case ELX_LOOPBACK_DATA:
James Smart3b5dd522010-01-26 23:10:15 -0500922 diag_cmd_data_free(phba,
James Smart4cc0e562010-01-26 23:09:48 -0500923 (struct lpfc_dmabufext *)
924 dmabuf);
925 break;
James Smartf1c3b0f2009-07-19 10:01:32 -0400926 case ELX_LOOPBACK_XRI_SETUP:
James Smart4cc0e562010-01-26 23:09:48 -0500927 if ((phba->sli_rev ==
928 LPFC_SLI_REV2) ||
929 (phba->sli3_options &
930 LPFC_SLI3_HBQ_ENABLED
931 )) {
932 lpfc_in_buf_free(phba,
933 dmabuf);
934 } else {
James Smartf1c3b0f2009-07-19 10:01:32 -0400935 lpfc_post_buffer(phba,
936 pring,
937 1);
James Smart4cc0e562010-01-26 23:09:48 -0500938 }
James Smartf1c3b0f2009-07-19 10:01:32 -0400939 break;
940 default:
941 if (!(phba->sli3_options &
942 LPFC_SLI3_HBQ_ENABLED))
943 lpfc_post_buffer(phba,
944 pring,
945 1);
946 break;
947 }
948 }
949 }
950 }
951
James Smart4fede782010-01-26 23:08:55 -0500952 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -0400953 if (phba->sli_rev == LPFC_SLI_REV4) {
954 evt_dat->immed_dat = phba->ctx_idx;
955 phba->ctx_idx = (phba->ctx_idx + 1) % 64;
956 phba->ct_ctx[evt_dat->immed_dat].oxid =
957 piocbq->iocb.ulpContext;
958 phba->ct_ctx[evt_dat->immed_dat].SID =
959 piocbq->iocb.un.rcvels.remoteID;
960 } else
961 evt_dat->immed_dat = piocbq->iocb.ulpContext;
962
963 evt_dat->type = FC_REG_CT_EVENT;
964 list_add(&evt_dat->node, &evt->events_to_see);
James Smart4cc0e562010-01-26 23:09:48 -0500965 if (evt_req_id == SLI_CT_ELX_LOOPBACK) {
966 wake_up_interruptible(&evt->wq);
967 lpfc_bsg_event_unref(evt);
James Smartf1c3b0f2009-07-19 10:01:32 -0400968 break;
James Smart4cc0e562010-01-26 23:09:48 -0500969 }
970
971 list_move(evt->events_to_see.prev, &evt->events_to_get);
972 lpfc_bsg_event_unref(evt);
973
974 job = evt->set_job;
975 evt->set_job = NULL;
976 if (job) {
977 job->reply->reply_payload_rcv_len = size;
978 /* make error code available to userspace */
979 job->reply->result = 0;
980 job->dd_data = NULL;
981 /* complete the job back to userspace */
982 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
983 job->job_done(job);
984 spin_lock_irqsave(&phba->ct_ev_lock, flags);
985 }
James Smartf1c3b0f2009-07-19 10:01:32 -0400986 }
James Smart4fede782010-01-26 23:08:55 -0500987 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -0400988
989error_ct_unsol_exit:
990 if (!list_empty(&head))
991 list_del(&head);
James Smart4cc0e562010-01-26 23:09:48 -0500992 if (evt_req_id == SLI_CT_ELX_LOOPBACK)
993 return 0;
James Smart4fede782010-01-26 23:08:55 -0500994 return 1;
James Smartf1c3b0f2009-07-19 10:01:32 -0400995}
996
997/**
James Smart4cc0e562010-01-26 23:09:48 -0500998 * lpfc_bsg_hba_set_event - process a SET_EVENT bsg vendor command
James Smartf1c3b0f2009-07-19 10:01:32 -0400999 * @job: SET_EVENT fc_bsg_job
James Smart3b5dd522010-01-26 23:10:15 -05001000 **/
James Smartf1c3b0f2009-07-19 10:01:32 -04001001static int
James Smart4cc0e562010-01-26 23:09:48 -05001002lpfc_bsg_hba_set_event(struct fc_bsg_job *job)
James Smartf1c3b0f2009-07-19 10:01:32 -04001003{
1004 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
1005 struct lpfc_hba *phba = vport->phba;
1006 struct set_ct_event *event_req;
James Smart4cc0e562010-01-26 23:09:48 -05001007 struct lpfc_bsg_event *evt;
James Smartf1c3b0f2009-07-19 10:01:32 -04001008 int rc = 0;
James Smart4cc0e562010-01-26 23:09:48 -05001009 struct bsg_job_data *dd_data = NULL;
1010 uint32_t ev_mask;
1011 unsigned long flags;
James Smartf1c3b0f2009-07-19 10:01:32 -04001012
1013 if (job->request_len <
1014 sizeof(struct fc_bsg_request) + sizeof(struct set_ct_event)) {
1015 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1016 "2612 Received SET_CT_EVENT below minimum "
1017 "size\n");
James Smart4cc0e562010-01-26 23:09:48 -05001018 rc = -EINVAL;
1019 goto job_error;
1020 }
1021
1022 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
1023 if (dd_data == NULL) {
1024 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1025 "2734 Failed allocation of dd_data\n");
1026 rc = -ENOMEM;
1027 goto job_error;
James Smartf1c3b0f2009-07-19 10:01:32 -04001028 }
1029
1030 event_req = (struct set_ct_event *)
1031 job->request->rqst_data.h_vendor.vendor_cmd;
James Smart4cc0e562010-01-26 23:09:48 -05001032 ev_mask = ((uint32_t)(unsigned long)event_req->type_mask &
1033 FC_REG_EVENT_MASK);
James Smart4fede782010-01-26 23:08:55 -05001034 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -04001035 list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
1036 if (evt->reg_id == event_req->ev_reg_id) {
James Smart4cc0e562010-01-26 23:09:48 -05001037 lpfc_bsg_event_ref(evt);
James Smartf1c3b0f2009-07-19 10:01:32 -04001038 evt->wait_time_stamp = jiffies;
1039 break;
1040 }
1041 }
James Smart4fede782010-01-26 23:08:55 -05001042 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -04001043
1044 if (&evt->node == &phba->ct_ev_waiters) {
1045 /* no event waiting struct yet - first call */
James Smart4cc0e562010-01-26 23:09:48 -05001046 evt = lpfc_bsg_event_new(ev_mask, event_req->ev_reg_id,
James Smartf1c3b0f2009-07-19 10:01:32 -04001047 event_req->ev_req_id);
1048 if (!evt) {
1049 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1050 "2617 Failed allocation of event "
1051 "waiter\n");
James Smart4cc0e562010-01-26 23:09:48 -05001052 rc = -ENOMEM;
1053 goto job_error;
James Smartf1c3b0f2009-07-19 10:01:32 -04001054 }
1055
James Smart4fede782010-01-26 23:08:55 -05001056 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -04001057 list_add(&evt->node, &phba->ct_ev_waiters);
James Smart4cc0e562010-01-26 23:09:48 -05001058 lpfc_bsg_event_ref(evt);
1059 evt->wait_time_stamp = jiffies;
James Smart4fede782010-01-26 23:08:55 -05001060 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -04001061 }
1062
James Smart4fede782010-01-26 23:08:55 -05001063 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smart4cc0e562010-01-26 23:09:48 -05001064 evt->waiting = 1;
1065 dd_data->type = TYPE_EVT;
1066 dd_data->context_un.evt = evt;
1067 evt->set_job = job; /* for unsolicited command */
1068 job->dd_data = dd_data; /* for fc transport timeout callback*/
James Smart4fede782010-01-26 23:08:55 -05001069 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smart4cc0e562010-01-26 23:09:48 -05001070 return 0; /* call job done later */
James Smartf1c3b0f2009-07-19 10:01:32 -04001071
James Smart4cc0e562010-01-26 23:09:48 -05001072job_error:
1073 if (dd_data != NULL)
1074 kfree(dd_data);
James Smartf1c3b0f2009-07-19 10:01:32 -04001075
James Smart4cc0e562010-01-26 23:09:48 -05001076 job->dd_data = NULL;
1077 return rc;
James Smartf1c3b0f2009-07-19 10:01:32 -04001078}
1079
1080/**
James Smart4cc0e562010-01-26 23:09:48 -05001081 * lpfc_bsg_hba_get_event - process a GET_EVENT bsg vendor command
James Smartf1c3b0f2009-07-19 10:01:32 -04001082 * @job: GET_EVENT fc_bsg_job
James Smart3b5dd522010-01-26 23:10:15 -05001083 **/
James Smartf1c3b0f2009-07-19 10:01:32 -04001084static int
James Smart4cc0e562010-01-26 23:09:48 -05001085lpfc_bsg_hba_get_event(struct fc_bsg_job *job)
James Smartf1c3b0f2009-07-19 10:01:32 -04001086{
1087 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
1088 struct lpfc_hba *phba = vport->phba;
1089 struct get_ct_event *event_req;
1090 struct get_ct_event_reply *event_reply;
James Smart4cc0e562010-01-26 23:09:48 -05001091 struct lpfc_bsg_event *evt;
James Smartf1c3b0f2009-07-19 10:01:32 -04001092 struct event_data *evt_dat = NULL;
James Smart4fede782010-01-26 23:08:55 -05001093 unsigned long flags;
James Smart4cc0e562010-01-26 23:09:48 -05001094 uint32_t rc = 0;
James Smartf1c3b0f2009-07-19 10:01:32 -04001095
1096 if (job->request_len <
1097 sizeof(struct fc_bsg_request) + sizeof(struct get_ct_event)) {
1098 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1099 "2613 Received GET_CT_EVENT request below "
1100 "minimum size\n");
James Smart4cc0e562010-01-26 23:09:48 -05001101 rc = -EINVAL;
1102 goto job_error;
James Smartf1c3b0f2009-07-19 10:01:32 -04001103 }
1104
1105 event_req = (struct get_ct_event *)
1106 job->request->rqst_data.h_vendor.vendor_cmd;
1107
1108 event_reply = (struct get_ct_event_reply *)
1109 job->reply->reply_data.vendor_reply.vendor_rsp;
James Smart4fede782010-01-26 23:08:55 -05001110 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -04001111 list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
1112 if (evt->reg_id == event_req->ev_reg_id) {
1113 if (list_empty(&evt->events_to_get))
1114 break;
James Smart4cc0e562010-01-26 23:09:48 -05001115 lpfc_bsg_event_ref(evt);
James Smartf1c3b0f2009-07-19 10:01:32 -04001116 evt->wait_time_stamp = jiffies;
1117 evt_dat = list_entry(evt->events_to_get.prev,
1118 struct event_data, node);
1119 list_del(&evt_dat->node);
1120 break;
1121 }
1122 }
James Smart4fede782010-01-26 23:08:55 -05001123 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -04001124
James Smart4cc0e562010-01-26 23:09:48 -05001125 /* The app may continue to ask for event data until it gets
1126 * an error indicating that there isn't anymore
1127 */
1128 if (evt_dat == NULL) {
James Smartf1c3b0f2009-07-19 10:01:32 -04001129 job->reply->reply_payload_rcv_len = 0;
1130 rc = -ENOENT;
James Smart4cc0e562010-01-26 23:09:48 -05001131 goto job_error;
James Smartf1c3b0f2009-07-19 10:01:32 -04001132 }
1133
James Smart4cc0e562010-01-26 23:09:48 -05001134 if (evt_dat->len > job->request_payload.payload_len) {
1135 evt_dat->len = job->request_payload.payload_len;
1136 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1137 "2618 Truncated event data at %d "
1138 "bytes\n",
1139 job->request_payload.payload_len);
James Smartf1c3b0f2009-07-19 10:01:32 -04001140 }
1141
James Smart4cc0e562010-01-26 23:09:48 -05001142 event_reply->type = evt_dat->type;
James Smartf1c3b0f2009-07-19 10:01:32 -04001143 event_reply->immed_data = evt_dat->immed_dat;
James Smartf1c3b0f2009-07-19 10:01:32 -04001144 if (evt_dat->len > 0)
1145 job->reply->reply_payload_rcv_len =
James Smart4cc0e562010-01-26 23:09:48 -05001146 sg_copy_from_buffer(job->request_payload.sg_list,
1147 job->request_payload.sg_cnt,
James Smartf1c3b0f2009-07-19 10:01:32 -04001148 evt_dat->data, evt_dat->len);
1149 else
1150 job->reply->reply_payload_rcv_len = 0;
James Smartf1c3b0f2009-07-19 10:01:32 -04001151
James Smart4cc0e562010-01-26 23:09:48 -05001152 if (evt_dat) {
James Smartf1c3b0f2009-07-19 10:01:32 -04001153 kfree(evt_dat->data);
James Smart4cc0e562010-01-26 23:09:48 -05001154 kfree(evt_dat);
1155 }
1156
James Smart4fede782010-01-26 23:08:55 -05001157 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smart4cc0e562010-01-26 23:09:48 -05001158 lpfc_bsg_event_unref(evt);
James Smart4fede782010-01-26 23:08:55 -05001159 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smart4cc0e562010-01-26 23:09:48 -05001160 job->dd_data = NULL;
1161 job->reply->result = 0;
James Smartf1c3b0f2009-07-19 10:01:32 -04001162 job->job_done(job);
James Smart4cc0e562010-01-26 23:09:48 -05001163 return 0;
James Smartf1c3b0f2009-07-19 10:01:32 -04001164
James Smart4cc0e562010-01-26 23:09:48 -05001165job_error:
1166 job->dd_data = NULL;
1167 job->reply->result = rc;
James Smartf1c3b0f2009-07-19 10:01:32 -04001168 return rc;
1169}
1170
1171/**
James Smart3b5dd522010-01-26 23:10:15 -05001172 * lpfc_issue_ct_rsp_cmp - lpfc_issue_ct_rsp's completion handler
1173 * @phba: Pointer to HBA context object.
1174 * @cmdiocbq: Pointer to command iocb.
1175 * @rspiocbq: Pointer to response iocb.
1176 *
1177 * This function is the completion handler for iocbs issued using
1178 * lpfc_issue_ct_rsp_cmp function. This function is called by the
1179 * ring event handler function without any lock held. This function
1180 * can be called from both worker thread context and interrupt
1181 * context. This function also can be called from other thread which
1182 * cleans up the SLI layer objects.
1183 * This function copy the contents of the response iocb to the
1184 * response iocb memory object provided by the caller of
1185 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
1186 * sleeps for the iocb completion.
1187 **/
1188static void
1189lpfc_issue_ct_rsp_cmp(struct lpfc_hba *phba,
1190 struct lpfc_iocbq *cmdiocbq,
1191 struct lpfc_iocbq *rspiocbq)
1192{
1193 struct bsg_job_data *dd_data;
1194 struct fc_bsg_job *job;
1195 IOCB_t *rsp;
1196 struct lpfc_dmabuf *bmp;
1197 struct lpfc_nodelist *ndlp;
1198 unsigned long flags;
1199 int rc = 0;
1200
1201 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1202 dd_data = cmdiocbq->context1;
1203 /* normal completion and timeout crossed paths, already done */
1204 if (!dd_data) {
Jiri Slaby67221a42010-03-16 16:23:58 +01001205 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smart3b5dd522010-01-26 23:10:15 -05001206 return;
1207 }
1208
1209 job = dd_data->context_un.iocb.set_job;
1210 bmp = dd_data->context_un.iocb.bmp;
1211 rsp = &rspiocbq->iocb;
1212 ndlp = dd_data->context_un.iocb.ndlp;
1213
1214 pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
1215 job->request_payload.sg_cnt, DMA_TO_DEVICE);
1216
1217 if (rsp->ulpStatus) {
1218 if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
1219 switch (rsp->un.ulpWord[4] & 0xff) {
1220 case IOERR_SEQUENCE_TIMEOUT:
1221 rc = -ETIMEDOUT;
1222 break;
1223 case IOERR_INVALID_RPI:
1224 rc = -EFAULT;
1225 break;
1226 default:
1227 rc = -EACCES;
1228 break;
1229 }
1230 } else
1231 rc = -EACCES;
1232 } else
1233 job->reply->reply_payload_rcv_len =
1234 rsp->un.genreq64.bdl.bdeSize;
1235
1236 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
1237 lpfc_sli_release_iocbq(phba, cmdiocbq);
1238 lpfc_nlp_put(ndlp);
1239 kfree(bmp);
1240 kfree(dd_data);
1241 /* make error code available to userspace */
1242 job->reply->result = rc;
1243 job->dd_data = NULL;
1244 /* complete the job back to userspace */
1245 job->job_done(job);
1246 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1247 return;
1248}
1249
1250/**
1251 * lpfc_issue_ct_rsp - issue a ct response
1252 * @phba: Pointer to HBA context object.
1253 * @job: Pointer to the job object.
1254 * @tag: tag index value into the ports context exchange array.
1255 * @bmp: Pointer to a dma buffer descriptor.
1256 * @num_entry: Number of enties in the bde.
1257 **/
1258static int
1259lpfc_issue_ct_rsp(struct lpfc_hba *phba, struct fc_bsg_job *job, uint32_t tag,
1260 struct lpfc_dmabuf *bmp, int num_entry)
1261{
1262 IOCB_t *icmd;
1263 struct lpfc_iocbq *ctiocb = NULL;
1264 int rc = 0;
1265 struct lpfc_nodelist *ndlp = NULL;
1266 struct bsg_job_data *dd_data;
1267 uint32_t creg_val;
1268
1269 /* allocate our bsg tracking structure */
1270 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
1271 if (!dd_data) {
1272 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1273 "2736 Failed allocation of dd_data\n");
1274 rc = -ENOMEM;
1275 goto no_dd_data;
1276 }
1277
1278 /* Allocate buffer for command iocb */
1279 ctiocb = lpfc_sli_get_iocbq(phba);
1280 if (!ctiocb) {
1281 rc = ENOMEM;
1282 goto no_ctiocb;
1283 }
1284
1285 icmd = &ctiocb->iocb;
1286 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
1287 icmd->un.xseq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
1288 icmd->un.xseq64.bdl.addrLow = putPaddrLow(bmp->phys);
1289 icmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
1290 icmd->un.xseq64.bdl.bdeSize = (num_entry * sizeof(struct ulp_bde64));
1291 icmd->un.xseq64.w5.hcsw.Fctl = (LS | LA);
1292 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
1293 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_SOL_CTL;
1294 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
1295
1296 /* Fill in rest of iocb */
1297 icmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;
1298 icmd->ulpBdeCount = 1;
1299 icmd->ulpLe = 1;
1300 icmd->ulpClass = CLASS3;
1301 if (phba->sli_rev == LPFC_SLI_REV4) {
1302 /* Do not issue unsol response if oxid not marked as valid */
1303 if (!(phba->ct_ctx[tag].flags & UNSOL_VALID)) {
1304 rc = IOCB_ERROR;
1305 goto issue_ct_rsp_exit;
1306 }
1307 icmd->ulpContext = phba->ct_ctx[tag].oxid;
1308 ndlp = lpfc_findnode_did(phba->pport, phba->ct_ctx[tag].SID);
1309 if (!ndlp) {
1310 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
1311 "2721 ndlp null for oxid %x SID %x\n",
1312 icmd->ulpContext,
1313 phba->ct_ctx[tag].SID);
1314 rc = IOCB_ERROR;
1315 goto issue_ct_rsp_exit;
1316 }
1317 icmd->un.ulpWord[3] = ndlp->nlp_rpi;
1318 /* The exchange is done, mark the entry as invalid */
1319 phba->ct_ctx[tag].flags &= ~UNSOL_VALID;
1320 } else
1321 icmd->ulpContext = (ushort) tag;
1322
1323 icmd->ulpTimeout = phba->fc_ratov * 2;
1324
1325 /* Xmit CT response on exchange <xid> */
1326 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
1327 "2722 Xmit CT response on exchange x%x Data: x%x x%x\n",
1328 icmd->ulpContext, icmd->ulpIoTag, phba->link_state);
1329
1330 ctiocb->iocb_cmpl = NULL;
1331 ctiocb->iocb_flag |= LPFC_IO_LIBDFC;
1332 ctiocb->vport = phba->pport;
1333 ctiocb->context3 = bmp;
1334
1335 ctiocb->iocb_cmpl = lpfc_issue_ct_rsp_cmp;
1336 ctiocb->context1 = dd_data;
1337 ctiocb->context2 = NULL;
1338 dd_data->type = TYPE_IOCB;
1339 dd_data->context_un.iocb.cmdiocbq = ctiocb;
1340 dd_data->context_un.iocb.rspiocbq = NULL;
1341 dd_data->context_un.iocb.set_job = job;
1342 dd_data->context_un.iocb.bmp = bmp;
1343 dd_data->context_un.iocb.ndlp = ndlp;
1344
1345 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
1346 creg_val = readl(phba->HCregaddr);
1347 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
1348 writel(creg_val, phba->HCregaddr);
1349 readl(phba->HCregaddr); /* flush */
1350 }
1351
1352 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
1353
1354 if (rc == IOCB_SUCCESS)
1355 return 0; /* done for now */
1356
1357issue_ct_rsp_exit:
1358 lpfc_sli_release_iocbq(phba, ctiocb);
1359no_ctiocb:
1360 kfree(dd_data);
1361no_dd_data:
1362 return rc;
1363}
1364
1365/**
1366 * lpfc_bsg_send_mgmt_rsp - process a SEND_MGMT_RESP bsg vendor command
1367 * @job: SEND_MGMT_RESP fc_bsg_job
1368 **/
1369static int
1370lpfc_bsg_send_mgmt_rsp(struct fc_bsg_job *job)
1371{
1372 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
1373 struct lpfc_hba *phba = vport->phba;
1374 struct send_mgmt_resp *mgmt_resp = (struct send_mgmt_resp *)
1375 job->request->rqst_data.h_vendor.vendor_cmd;
1376 struct ulp_bde64 *bpl;
1377 struct lpfc_dmabuf *bmp = NULL;
1378 struct scatterlist *sgel = NULL;
1379 int request_nseg;
1380 int numbde;
1381 dma_addr_t busaddr;
1382 uint32_t tag = mgmt_resp->tag;
1383 unsigned long reqbfrcnt =
1384 (unsigned long)job->request_payload.payload_len;
1385 int rc = 0;
1386
1387 /* in case no data is transferred */
1388 job->reply->reply_payload_rcv_len = 0;
1389
1390 if (!reqbfrcnt || (reqbfrcnt > (80 * BUF_SZ_4K))) {
1391 rc = -ERANGE;
1392 goto send_mgmt_rsp_exit;
1393 }
1394
1395 bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
1396 if (!bmp) {
1397 rc = -ENOMEM;
1398 goto send_mgmt_rsp_exit;
1399 }
1400
1401 bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
1402 if (!bmp->virt) {
1403 rc = -ENOMEM;
1404 goto send_mgmt_rsp_free_bmp;
1405 }
1406
1407 INIT_LIST_HEAD(&bmp->list);
1408 bpl = (struct ulp_bde64 *) bmp->virt;
1409 request_nseg = pci_map_sg(phba->pcidev, job->request_payload.sg_list,
1410 job->request_payload.sg_cnt, DMA_TO_DEVICE);
1411 for_each_sg(job->request_payload.sg_list, sgel, request_nseg, numbde) {
1412 busaddr = sg_dma_address(sgel);
1413 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1414 bpl->tus.f.bdeSize = sg_dma_len(sgel);
1415 bpl->tus.w = cpu_to_le32(bpl->tus.w);
1416 bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
1417 bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
1418 bpl++;
1419 }
1420
1421 rc = lpfc_issue_ct_rsp(phba, job, tag, bmp, request_nseg);
1422
1423 if (rc == IOCB_SUCCESS)
1424 return 0; /* done for now */
1425
1426 /* TBD need to handle a timeout */
1427 pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
1428 job->request_payload.sg_cnt, DMA_TO_DEVICE);
1429 rc = -EACCES;
1430 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
1431
1432send_mgmt_rsp_free_bmp:
1433 kfree(bmp);
1434send_mgmt_rsp_exit:
1435 /* make error code available to userspace */
1436 job->reply->result = rc;
1437 job->dd_data = NULL;
1438 return rc;
1439}
1440
1441/**
1442 * lpfc_bsg_diag_mode - process a LPFC_BSG_VENDOR_DIAG_MODE bsg vendor command
1443 * @job: LPFC_BSG_VENDOR_DIAG_MODE
1444 *
1445 * This function is responsible for placing a port into diagnostic loopback
1446 * mode in order to perform a diagnostic loopback test.
1447 * All new scsi requests are blocked, a small delay is used to allow the
1448 * scsi requests to complete then the link is brought down. If the link is
1449 * is placed in loopback mode then scsi requests are again allowed
1450 * so the scsi mid-layer doesn't give up on the port.
1451 * All of this is done in-line.
1452 */
1453static int
1454lpfc_bsg_diag_mode(struct fc_bsg_job *job)
1455{
1456 struct Scsi_Host *shost = job->shost;
1457 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
1458 struct lpfc_hba *phba = vport->phba;
1459 struct diag_mode_set *loopback_mode;
1460 struct lpfc_sli *psli = &phba->sli;
1461 struct lpfc_sli_ring *pring = &psli->ring[LPFC_FCP_RING];
1462 uint32_t link_flags;
1463 uint32_t timeout;
1464 struct lpfc_vport **vports;
1465 LPFC_MBOXQ_t *pmboxq;
1466 int mbxstatus;
1467 int i = 0;
1468 int rc = 0;
1469
1470 /* no data to return just the return code */
1471 job->reply->reply_payload_rcv_len = 0;
1472
1473 if (job->request_len <
1474 sizeof(struct fc_bsg_request) + sizeof(struct diag_mode_set)) {
1475 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1476 "2738 Received DIAG MODE request below minimum "
1477 "size\n");
1478 rc = -EINVAL;
1479 goto job_error;
1480 }
1481
1482 loopback_mode = (struct diag_mode_set *)
1483 job->request->rqst_data.h_vendor.vendor_cmd;
1484 link_flags = loopback_mode->type;
1485 timeout = loopback_mode->timeout;
1486
1487 if ((phba->link_state == LPFC_HBA_ERROR) ||
1488 (psli->sli_flag & LPFC_BLOCK_MGMT_IO) ||
1489 (!(psli->sli_flag & LPFC_SLI_ACTIVE))) {
1490 rc = -EACCES;
1491 goto job_error;
1492 }
1493
1494 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1495 if (!pmboxq) {
1496 rc = -ENOMEM;
1497 goto job_error;
1498 }
1499
1500 vports = lpfc_create_vport_work_array(phba);
1501 if (vports) {
1502 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
1503 shost = lpfc_shost_from_vport(vports[i]);
1504 scsi_block_requests(shost);
1505 }
1506
1507 lpfc_destroy_vport_work_array(phba, vports);
1508 } else {
1509 shost = lpfc_shost_from_vport(phba->pport);
1510 scsi_block_requests(shost);
1511 }
1512
1513 while (pring->txcmplq_cnt) {
1514 if (i++ > 500) /* wait up to 5 seconds */
1515 break;
1516
1517 msleep(10);
1518 }
1519
1520 memset((void *)pmboxq, 0, sizeof(LPFC_MBOXQ_t));
1521 pmboxq->u.mb.mbxCommand = MBX_DOWN_LINK;
1522 pmboxq->u.mb.mbxOwner = OWN_HOST;
1523
1524 mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
1525
1526 if ((mbxstatus == MBX_SUCCESS) && (pmboxq->u.mb.mbxStatus == 0)) {
1527 /* wait for link down before proceeding */
1528 i = 0;
1529 while (phba->link_state != LPFC_LINK_DOWN) {
1530 if (i++ > timeout) {
1531 rc = -ETIMEDOUT;
1532 goto loopback_mode_exit;
1533 }
1534
1535 msleep(10);
1536 }
1537
1538 memset((void *)pmboxq, 0, sizeof(LPFC_MBOXQ_t));
1539 if (link_flags == INTERNAL_LOOP_BACK)
1540 pmboxq->u.mb.un.varInitLnk.link_flags = FLAGS_LOCAL_LB;
1541 else
1542 pmboxq->u.mb.un.varInitLnk.link_flags =
1543 FLAGS_TOPOLOGY_MODE_LOOP;
1544
1545 pmboxq->u.mb.mbxCommand = MBX_INIT_LINK;
1546 pmboxq->u.mb.mbxOwner = OWN_HOST;
1547
1548 mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq,
1549 LPFC_MBOX_TMO);
1550
1551 if ((mbxstatus != MBX_SUCCESS) || (pmboxq->u.mb.mbxStatus))
1552 rc = -ENODEV;
1553 else {
1554 phba->link_flag |= LS_LOOPBACK_MODE;
1555 /* wait for the link attention interrupt */
1556 msleep(100);
1557
1558 i = 0;
1559 while (phba->link_state != LPFC_HBA_READY) {
1560 if (i++ > timeout) {
1561 rc = -ETIMEDOUT;
1562 break;
1563 }
1564
1565 msleep(10);
1566 }
1567 }
1568
1569 } else
1570 rc = -ENODEV;
1571
1572loopback_mode_exit:
1573 vports = lpfc_create_vport_work_array(phba);
1574 if (vports) {
1575 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
1576 shost = lpfc_shost_from_vport(vports[i]);
1577 scsi_unblock_requests(shost);
1578 }
1579 lpfc_destroy_vport_work_array(phba, vports);
1580 } else {
1581 shost = lpfc_shost_from_vport(phba->pport);
1582 scsi_unblock_requests(shost);
1583 }
1584
1585 /*
1586 * Let SLI layer release mboxq if mbox command completed after timeout.
1587 */
1588 if (mbxstatus != MBX_TIMEOUT)
1589 mempool_free(pmboxq, phba->mbox_mem_pool);
1590
1591job_error:
1592 /* make error code available to userspace */
1593 job->reply->result = rc;
1594 /* complete the job back to userspace if no error */
1595 if (rc == 0)
1596 job->job_done(job);
1597 return rc;
1598}
1599
1600/**
1601 * lpfcdiag_loop_self_reg - obtains a remote port login id
1602 * @phba: Pointer to HBA context object
1603 * @rpi: Pointer to a remote port login id
1604 *
1605 * This function obtains a remote port login id so the diag loopback test
1606 * can send and receive its own unsolicited CT command.
1607 **/
1608static int lpfcdiag_loop_self_reg(struct lpfc_hba *phba, uint16_t * rpi)
1609{
1610 LPFC_MBOXQ_t *mbox;
1611 struct lpfc_dmabuf *dmabuff;
1612 int status;
1613
1614 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1615 if (!mbox)
1616 return ENOMEM;
1617
1618 status = lpfc_reg_rpi(phba, 0, phba->pport->fc_myDID,
1619 (uint8_t *)&phba->pport->fc_sparam, mbox, 0);
1620 if (status) {
1621 mempool_free(mbox, phba->mbox_mem_pool);
1622 return ENOMEM;
1623 }
1624
1625 dmabuff = (struct lpfc_dmabuf *) mbox->context1;
1626 mbox->context1 = NULL;
1627 status = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
1628
1629 if ((status != MBX_SUCCESS) || (mbox->u.mb.mbxStatus)) {
1630 lpfc_mbuf_free(phba, dmabuff->virt, dmabuff->phys);
1631 kfree(dmabuff);
1632 if (status != MBX_TIMEOUT)
1633 mempool_free(mbox, phba->mbox_mem_pool);
1634 return ENODEV;
1635 }
1636
1637 *rpi = mbox->u.mb.un.varWords[0];
1638
1639 lpfc_mbuf_free(phba, dmabuff->virt, dmabuff->phys);
1640 kfree(dmabuff);
1641 mempool_free(mbox, phba->mbox_mem_pool);
1642 return 0;
1643}
1644
1645/**
1646 * lpfcdiag_loop_self_unreg - unregs from the rpi
1647 * @phba: Pointer to HBA context object
1648 * @rpi: Remote port login id
1649 *
1650 * This function unregisters the rpi obtained in lpfcdiag_loop_self_reg
1651 **/
1652static int lpfcdiag_loop_self_unreg(struct lpfc_hba *phba, uint16_t rpi)
1653{
1654 LPFC_MBOXQ_t *mbox;
1655 int status;
1656
1657 /* Allocate mboxq structure */
1658 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1659 if (mbox == NULL)
1660 return ENOMEM;
1661
1662 lpfc_unreg_login(phba, 0, rpi, mbox);
1663 status = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
1664
1665 if ((status != MBX_SUCCESS) || (mbox->u.mb.mbxStatus)) {
1666 if (status != MBX_TIMEOUT)
1667 mempool_free(mbox, phba->mbox_mem_pool);
1668 return EIO;
1669 }
1670
1671 mempool_free(mbox, phba->mbox_mem_pool);
1672 return 0;
1673}
1674
1675/**
1676 * lpfcdiag_loop_get_xri - obtains the transmit and receive ids
1677 * @phba: Pointer to HBA context object
1678 * @rpi: Remote port login id
1679 * @txxri: Pointer to transmit exchange id
1680 * @rxxri: Pointer to response exchabge id
1681 *
1682 * This function obtains the transmit and receive ids required to send
1683 * an unsolicited ct command with a payload. A special lpfc FsType and CmdRsp
1684 * flags are used to the unsolicted response handler is able to process
1685 * the ct command sent on the same port.
1686 **/
1687static int lpfcdiag_loop_get_xri(struct lpfc_hba *phba, uint16_t rpi,
1688 uint16_t *txxri, uint16_t * rxxri)
1689{
1690 struct lpfc_bsg_event *evt;
1691 struct lpfc_iocbq *cmdiocbq, *rspiocbq;
1692 IOCB_t *cmd, *rsp;
1693 struct lpfc_dmabuf *dmabuf;
1694 struct ulp_bde64 *bpl = NULL;
1695 struct lpfc_sli_ct_request *ctreq = NULL;
1696 int ret_val = 0;
1697 unsigned long flags;
1698
1699 *txxri = 0;
1700 *rxxri = 0;
1701 evt = lpfc_bsg_event_new(FC_REG_CT_EVENT, current->pid,
1702 SLI_CT_ELX_LOOPBACK);
1703 if (!evt)
1704 return ENOMEM;
1705
1706 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1707 list_add(&evt->node, &phba->ct_ev_waiters);
1708 lpfc_bsg_event_ref(evt);
1709 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1710
1711 cmdiocbq = lpfc_sli_get_iocbq(phba);
1712 rspiocbq = lpfc_sli_get_iocbq(phba);
1713
1714 dmabuf = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
1715 if (dmabuf) {
1716 dmabuf->virt = lpfc_mbuf_alloc(phba, 0, &dmabuf->phys);
1717 INIT_LIST_HEAD(&dmabuf->list);
1718 bpl = (struct ulp_bde64 *) dmabuf->virt;
1719 memset(bpl, 0, sizeof(*bpl));
1720 ctreq = (struct lpfc_sli_ct_request *)(bpl + 1);
1721 bpl->addrHigh =
1722 le32_to_cpu(putPaddrHigh(dmabuf->phys + sizeof(*bpl)));
1723 bpl->addrLow =
1724 le32_to_cpu(putPaddrLow(dmabuf->phys + sizeof(*bpl)));
1725 bpl->tus.f.bdeFlags = 0;
1726 bpl->tus.f.bdeSize = ELX_LOOPBACK_HEADER_SZ;
1727 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1728 }
1729
1730 if (cmdiocbq == NULL || rspiocbq == NULL ||
1731 dmabuf == NULL || bpl == NULL || ctreq == NULL) {
1732 ret_val = ENOMEM;
1733 goto err_get_xri_exit;
1734 }
1735
1736 cmd = &cmdiocbq->iocb;
1737 rsp = &rspiocbq->iocb;
1738
1739 memset(ctreq, 0, ELX_LOOPBACK_HEADER_SZ);
1740
1741 ctreq->RevisionId.bits.Revision = SLI_CT_REVISION;
1742 ctreq->RevisionId.bits.InId = 0;
1743 ctreq->FsType = SLI_CT_ELX_LOOPBACK;
1744 ctreq->FsSubType = 0;
1745 ctreq->CommandResponse.bits.CmdRsp = ELX_LOOPBACK_XRI_SETUP;
1746 ctreq->CommandResponse.bits.Size = 0;
1747
1748
1749 cmd->un.xseq64.bdl.addrHigh = putPaddrHigh(dmabuf->phys);
1750 cmd->un.xseq64.bdl.addrLow = putPaddrLow(dmabuf->phys);
1751 cmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
1752 cmd->un.xseq64.bdl.bdeSize = sizeof(*bpl);
1753
1754 cmd->un.xseq64.w5.hcsw.Fctl = LA;
1755 cmd->un.xseq64.w5.hcsw.Dfctl = 0;
1756 cmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
1757 cmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
1758
1759 cmd->ulpCommand = CMD_XMIT_SEQUENCE64_CR;
1760 cmd->ulpBdeCount = 1;
1761 cmd->ulpLe = 1;
1762 cmd->ulpClass = CLASS3;
1763 cmd->ulpContext = rpi;
1764
1765 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
1766 cmdiocbq->vport = phba->pport;
1767
1768 ret_val = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq,
1769 rspiocbq,
1770 (phba->fc_ratov * 2)
1771 + LPFC_DRVR_TIMEOUT);
1772 if (ret_val)
1773 goto err_get_xri_exit;
1774
1775 *txxri = rsp->ulpContext;
1776
1777 evt->waiting = 1;
1778 evt->wait_time_stamp = jiffies;
1779 ret_val = wait_event_interruptible_timeout(
1780 evt->wq, !list_empty(&evt->events_to_see),
1781 ((phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT) * HZ);
1782 if (list_empty(&evt->events_to_see))
1783 ret_val = (ret_val) ? EINTR : ETIMEDOUT;
1784 else {
1785 ret_val = IOCB_SUCCESS;
1786 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1787 list_move(evt->events_to_see.prev, &evt->events_to_get);
1788 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1789 *rxxri = (list_entry(evt->events_to_get.prev,
1790 typeof(struct event_data),
1791 node))->immed_dat;
1792 }
1793 evt->waiting = 0;
1794
1795err_get_xri_exit:
1796 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1797 lpfc_bsg_event_unref(evt); /* release ref */
1798 lpfc_bsg_event_unref(evt); /* delete */
1799 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1800
1801 if (dmabuf) {
1802 if (dmabuf->virt)
1803 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
1804 kfree(dmabuf);
1805 }
1806
1807 if (cmdiocbq && (ret_val != IOCB_TIMEDOUT))
1808 lpfc_sli_release_iocbq(phba, cmdiocbq);
1809 if (rspiocbq)
1810 lpfc_sli_release_iocbq(phba, rspiocbq);
1811 return ret_val;
1812}
1813
1814/**
1815 * diag_cmd_data_alloc - fills in a bde struct with dma buffers
1816 * @phba: Pointer to HBA context object
1817 * @bpl: Pointer to 64 bit bde structure
1818 * @size: Number of bytes to process
1819 * @nocopydata: Flag to copy user data into the allocated buffer
1820 *
1821 * This function allocates page size buffers and populates an lpfc_dmabufext.
1822 * If allowed the user data pointed to with indataptr is copied into the kernel
1823 * memory. The chained list of page size buffers is returned.
1824 **/
1825static struct lpfc_dmabufext *
1826diag_cmd_data_alloc(struct lpfc_hba *phba,
1827 struct ulp_bde64 *bpl, uint32_t size,
1828 int nocopydata)
1829{
1830 struct lpfc_dmabufext *mlist = NULL;
1831 struct lpfc_dmabufext *dmp;
1832 int cnt, offset = 0, i = 0;
1833 struct pci_dev *pcidev;
1834
1835 pcidev = phba->pcidev;
1836
1837 while (size) {
1838 /* We get chunks of 4K */
1839 if (size > BUF_SZ_4K)
1840 cnt = BUF_SZ_4K;
1841 else
1842 cnt = size;
1843
1844 /* allocate struct lpfc_dmabufext buffer header */
1845 dmp = kmalloc(sizeof(struct lpfc_dmabufext), GFP_KERNEL);
1846 if (!dmp)
1847 goto out;
1848
1849 INIT_LIST_HEAD(&dmp->dma.list);
1850
1851 /* Queue it to a linked list */
1852 if (mlist)
1853 list_add_tail(&dmp->dma.list, &mlist->dma.list);
1854 else
1855 mlist = dmp;
1856
1857 /* allocate buffer */
1858 dmp->dma.virt = dma_alloc_coherent(&pcidev->dev,
1859 cnt,
1860 &(dmp->dma.phys),
1861 GFP_KERNEL);
1862
1863 if (!dmp->dma.virt)
1864 goto out;
1865
1866 dmp->size = cnt;
1867
1868 if (nocopydata) {
1869 bpl->tus.f.bdeFlags = 0;
1870 pci_dma_sync_single_for_device(phba->pcidev,
1871 dmp->dma.phys, LPFC_BPL_SIZE, PCI_DMA_TODEVICE);
1872
1873 } else {
1874 memset((uint8_t *)dmp->dma.virt, 0, cnt);
1875 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1876 }
1877
1878 /* build buffer ptr list for IOCB */
1879 bpl->addrLow = le32_to_cpu(putPaddrLow(dmp->dma.phys));
1880 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dmp->dma.phys));
1881 bpl->tus.f.bdeSize = (ushort) cnt;
1882 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1883 bpl++;
1884
1885 i++;
1886 offset += cnt;
1887 size -= cnt;
1888 }
1889
1890 mlist->flag = i;
1891 return mlist;
1892out:
1893 diag_cmd_data_free(phba, mlist);
1894 return NULL;
1895}
1896
1897/**
1898 * lpfcdiag_loop_post_rxbufs - post the receive buffers for an unsol CT cmd
1899 * @phba: Pointer to HBA context object
1900 * @rxxri: Receive exchange id
1901 * @len: Number of data bytes
1902 *
1903 * This function allocates and posts a data buffer of sufficient size to recieve
1904 * an unsolicted CT command.
1905 **/
1906static int lpfcdiag_loop_post_rxbufs(struct lpfc_hba *phba, uint16_t rxxri,
1907 size_t len)
1908{
1909 struct lpfc_sli *psli = &phba->sli;
1910 struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
1911 struct lpfc_iocbq *cmdiocbq;
1912 IOCB_t *cmd = NULL;
1913 struct list_head head, *curr, *next;
1914 struct lpfc_dmabuf *rxbmp;
1915 struct lpfc_dmabuf *dmp;
1916 struct lpfc_dmabuf *mp[2] = {NULL, NULL};
1917 struct ulp_bde64 *rxbpl = NULL;
1918 uint32_t num_bde;
1919 struct lpfc_dmabufext *rxbuffer = NULL;
1920 int ret_val = 0;
1921 int i = 0;
1922
1923 cmdiocbq = lpfc_sli_get_iocbq(phba);
1924 rxbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
1925 if (rxbmp != NULL) {
1926 rxbmp->virt = lpfc_mbuf_alloc(phba, 0, &rxbmp->phys);
1927 INIT_LIST_HEAD(&rxbmp->list);
1928 rxbpl = (struct ulp_bde64 *) rxbmp->virt;
1929 rxbuffer = diag_cmd_data_alloc(phba, rxbpl, len, 0);
1930 }
1931
1932 if (!cmdiocbq || !rxbmp || !rxbpl || !rxbuffer) {
1933 ret_val = ENOMEM;
1934 goto err_post_rxbufs_exit;
1935 }
1936
1937 /* Queue buffers for the receive exchange */
1938 num_bde = (uint32_t)rxbuffer->flag;
1939 dmp = &rxbuffer->dma;
1940
1941 cmd = &cmdiocbq->iocb;
1942 i = 0;
1943
1944 INIT_LIST_HEAD(&head);
1945 list_add_tail(&head, &dmp->list);
1946 list_for_each_safe(curr, next, &head) {
1947 mp[i] = list_entry(curr, struct lpfc_dmabuf, list);
1948 list_del(curr);
1949
1950 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
1951 mp[i]->buffer_tag = lpfc_sli_get_buffer_tag(phba);
1952 cmd->un.quexri64cx.buff.bde.addrHigh =
1953 putPaddrHigh(mp[i]->phys);
1954 cmd->un.quexri64cx.buff.bde.addrLow =
1955 putPaddrLow(mp[i]->phys);
1956 cmd->un.quexri64cx.buff.bde.tus.f.bdeSize =
1957 ((struct lpfc_dmabufext *)mp[i])->size;
1958 cmd->un.quexri64cx.buff.buffer_tag = mp[i]->buffer_tag;
1959 cmd->ulpCommand = CMD_QUE_XRI64_CX;
1960 cmd->ulpPU = 0;
1961 cmd->ulpLe = 1;
1962 cmd->ulpBdeCount = 1;
1963 cmd->unsli3.que_xri64cx_ext_words.ebde_count = 0;
1964
1965 } else {
1966 cmd->un.cont64[i].addrHigh = putPaddrHigh(mp[i]->phys);
1967 cmd->un.cont64[i].addrLow = putPaddrLow(mp[i]->phys);
1968 cmd->un.cont64[i].tus.f.bdeSize =
1969 ((struct lpfc_dmabufext *)mp[i])->size;
1970 cmd->ulpBdeCount = ++i;
1971
1972 if ((--num_bde > 0) && (i < 2))
1973 continue;
1974
1975 cmd->ulpCommand = CMD_QUE_XRI_BUF64_CX;
1976 cmd->ulpLe = 1;
1977 }
1978
1979 cmd->ulpClass = CLASS3;
1980 cmd->ulpContext = rxxri;
1981
1982 ret_val = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq, 0);
1983
1984 if (ret_val == IOCB_ERROR) {
1985 diag_cmd_data_free(phba,
1986 (struct lpfc_dmabufext *)mp[0]);
1987 if (mp[1])
1988 diag_cmd_data_free(phba,
1989 (struct lpfc_dmabufext *)mp[1]);
1990 dmp = list_entry(next, struct lpfc_dmabuf, list);
1991 ret_val = EIO;
1992 goto err_post_rxbufs_exit;
1993 }
1994
1995 lpfc_sli_ringpostbuf_put(phba, pring, mp[0]);
1996 if (mp[1]) {
1997 lpfc_sli_ringpostbuf_put(phba, pring, mp[1]);
1998 mp[1] = NULL;
1999 }
2000
2001 /* The iocb was freed by lpfc_sli_issue_iocb */
2002 cmdiocbq = lpfc_sli_get_iocbq(phba);
2003 if (!cmdiocbq) {
2004 dmp = list_entry(next, struct lpfc_dmabuf, list);
2005 ret_val = EIO;
2006 goto err_post_rxbufs_exit;
2007 }
2008
2009 cmd = &cmdiocbq->iocb;
2010 i = 0;
2011 }
2012 list_del(&head);
2013
2014err_post_rxbufs_exit:
2015
2016 if (rxbmp) {
2017 if (rxbmp->virt)
2018 lpfc_mbuf_free(phba, rxbmp->virt, rxbmp->phys);
2019 kfree(rxbmp);
2020 }
2021
2022 if (cmdiocbq)
2023 lpfc_sli_release_iocbq(phba, cmdiocbq);
2024 return ret_val;
2025}
2026
2027/**
2028 * lpfc_bsg_diag_test - with a port in loopback issues a Ct cmd to itself
2029 * @job: LPFC_BSG_VENDOR_DIAG_TEST fc_bsg_job
2030 *
2031 * This function receives a user data buffer to be transmitted and received on
2032 * the same port, the link must be up and in loopback mode prior
2033 * to being called.
2034 * 1. A kernel buffer is allocated to copy the user data into.
2035 * 2. The port registers with "itself".
2036 * 3. The transmit and receive exchange ids are obtained.
2037 * 4. The receive exchange id is posted.
2038 * 5. A new els loopback event is created.
2039 * 6. The command and response iocbs are allocated.
2040 * 7. The cmd iocb FsType is set to elx loopback and the CmdRsp to looppback.
2041 *
2042 * This function is meant to be called n times while the port is in loopback
2043 * so it is the apps responsibility to issue a reset to take the port out
2044 * of loopback mode.
2045 **/
2046static int
2047lpfc_bsg_diag_test(struct fc_bsg_job *job)
2048{
2049 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
2050 struct lpfc_hba *phba = vport->phba;
2051 struct diag_mode_test *diag_mode;
2052 struct lpfc_bsg_event *evt;
2053 struct event_data *evdat;
2054 struct lpfc_sli *psli = &phba->sli;
2055 uint32_t size;
2056 uint32_t full_size;
2057 size_t segment_len = 0, segment_offset = 0, current_offset = 0;
2058 uint16_t rpi;
2059 struct lpfc_iocbq *cmdiocbq, *rspiocbq;
2060 IOCB_t *cmd, *rsp;
2061 struct lpfc_sli_ct_request *ctreq;
2062 struct lpfc_dmabuf *txbmp;
2063 struct ulp_bde64 *txbpl = NULL;
2064 struct lpfc_dmabufext *txbuffer = NULL;
2065 struct list_head head;
2066 struct lpfc_dmabuf *curr;
2067 uint16_t txxri, rxxri;
2068 uint32_t num_bde;
2069 uint8_t *ptr = NULL, *rx_databuf = NULL;
2070 int rc = 0;
2071 unsigned long flags;
2072 void *dataout = NULL;
2073 uint32_t total_mem;
2074
2075 /* in case no data is returned return just the return code */
2076 job->reply->reply_payload_rcv_len = 0;
2077
2078 if (job->request_len <
2079 sizeof(struct fc_bsg_request) + sizeof(struct diag_mode_test)) {
2080 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2081 "2739 Received DIAG TEST request below minimum "
2082 "size\n");
2083 rc = -EINVAL;
2084 goto loopback_test_exit;
2085 }
2086
2087 if (job->request_payload.payload_len !=
2088 job->reply_payload.payload_len) {
2089 rc = -EINVAL;
2090 goto loopback_test_exit;
2091 }
2092
2093 diag_mode = (struct diag_mode_test *)
2094 job->request->rqst_data.h_vendor.vendor_cmd;
2095
2096 if ((phba->link_state == LPFC_HBA_ERROR) ||
2097 (psli->sli_flag & LPFC_BLOCK_MGMT_IO) ||
2098 (!(psli->sli_flag & LPFC_SLI_ACTIVE))) {
2099 rc = -EACCES;
2100 goto loopback_test_exit;
2101 }
2102
2103 if (!lpfc_is_link_up(phba) || !(phba->link_flag & LS_LOOPBACK_MODE)) {
2104 rc = -EACCES;
2105 goto loopback_test_exit;
2106 }
2107
2108 size = job->request_payload.payload_len;
2109 full_size = size + ELX_LOOPBACK_HEADER_SZ; /* plus the header */
2110
2111 if ((size == 0) || (size > 80 * BUF_SZ_4K)) {
2112 rc = -ERANGE;
2113 goto loopback_test_exit;
2114 }
2115
2116 if (size >= BUF_SZ_4K) {
2117 /*
2118 * Allocate memory for ioctl data. If buffer is bigger than 64k,
2119 * then we allocate 64k and re-use that buffer over and over to
2120 * xfer the whole block. This is because Linux kernel has a
2121 * problem allocating more than 120k of kernel space memory. Saw
2122 * problem with GET_FCPTARGETMAPPING...
2123 */
2124 if (size <= (64 * 1024))
2125 total_mem = size;
2126 else
2127 total_mem = 64 * 1024;
2128 } else
2129 /* Allocate memory for ioctl data */
2130 total_mem = BUF_SZ_4K;
2131
2132 dataout = kmalloc(total_mem, GFP_KERNEL);
2133 if (dataout == NULL) {
2134 rc = -ENOMEM;
2135 goto loopback_test_exit;
2136 }
2137
2138 ptr = dataout;
2139 ptr += ELX_LOOPBACK_HEADER_SZ;
2140 sg_copy_to_buffer(job->request_payload.sg_list,
2141 job->request_payload.sg_cnt,
2142 ptr, size);
2143
2144 rc = lpfcdiag_loop_self_reg(phba, &rpi);
2145 if (rc) {
2146 rc = -ENOMEM;
2147 goto loopback_test_exit;
2148 }
2149
2150 rc = lpfcdiag_loop_get_xri(phba, rpi, &txxri, &rxxri);
2151 if (rc) {
2152 lpfcdiag_loop_self_unreg(phba, rpi);
2153 rc = -ENOMEM;
2154 goto loopback_test_exit;
2155 }
2156
2157 rc = lpfcdiag_loop_post_rxbufs(phba, rxxri, full_size);
2158 if (rc) {
2159 lpfcdiag_loop_self_unreg(phba, rpi);
2160 rc = -ENOMEM;
2161 goto loopback_test_exit;
2162 }
2163
2164 evt = lpfc_bsg_event_new(FC_REG_CT_EVENT, current->pid,
2165 SLI_CT_ELX_LOOPBACK);
2166 if (!evt) {
2167 lpfcdiag_loop_self_unreg(phba, rpi);
2168 rc = -ENOMEM;
2169 goto loopback_test_exit;
2170 }
2171
2172 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2173 list_add(&evt->node, &phba->ct_ev_waiters);
2174 lpfc_bsg_event_ref(evt);
2175 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2176
2177 cmdiocbq = lpfc_sli_get_iocbq(phba);
2178 rspiocbq = lpfc_sli_get_iocbq(phba);
2179 txbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2180
2181 if (txbmp) {
2182 txbmp->virt = lpfc_mbuf_alloc(phba, 0, &txbmp->phys);
2183 INIT_LIST_HEAD(&txbmp->list);
2184 txbpl = (struct ulp_bde64 *) txbmp->virt;
2185 if (txbpl)
2186 txbuffer = diag_cmd_data_alloc(phba,
2187 txbpl, full_size, 0);
2188 }
2189
2190 if (!cmdiocbq || !rspiocbq || !txbmp || !txbpl || !txbuffer) {
2191 rc = -ENOMEM;
2192 goto err_loopback_test_exit;
2193 }
2194
2195 cmd = &cmdiocbq->iocb;
2196 rsp = &rspiocbq->iocb;
2197
2198 INIT_LIST_HEAD(&head);
2199 list_add_tail(&head, &txbuffer->dma.list);
2200 list_for_each_entry(curr, &head, list) {
2201 segment_len = ((struct lpfc_dmabufext *)curr)->size;
2202 if (current_offset == 0) {
2203 ctreq = curr->virt;
2204 memset(ctreq, 0, ELX_LOOPBACK_HEADER_SZ);
2205 ctreq->RevisionId.bits.Revision = SLI_CT_REVISION;
2206 ctreq->RevisionId.bits.InId = 0;
2207 ctreq->FsType = SLI_CT_ELX_LOOPBACK;
2208 ctreq->FsSubType = 0;
2209 ctreq->CommandResponse.bits.CmdRsp = ELX_LOOPBACK_DATA;
2210 ctreq->CommandResponse.bits.Size = size;
2211 segment_offset = ELX_LOOPBACK_HEADER_SZ;
2212 } else
2213 segment_offset = 0;
2214
2215 BUG_ON(segment_offset >= segment_len);
2216 memcpy(curr->virt + segment_offset,
2217 ptr + current_offset,
2218 segment_len - segment_offset);
2219
2220 current_offset += segment_len - segment_offset;
2221 BUG_ON(current_offset > size);
2222 }
2223 list_del(&head);
2224
2225 /* Build the XMIT_SEQUENCE iocb */
2226
2227 num_bde = (uint32_t)txbuffer->flag;
2228
2229 cmd->un.xseq64.bdl.addrHigh = putPaddrHigh(txbmp->phys);
2230 cmd->un.xseq64.bdl.addrLow = putPaddrLow(txbmp->phys);
2231 cmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
2232 cmd->un.xseq64.bdl.bdeSize = (num_bde * sizeof(struct ulp_bde64));
2233
2234 cmd->un.xseq64.w5.hcsw.Fctl = (LS | LA);
2235 cmd->un.xseq64.w5.hcsw.Dfctl = 0;
2236 cmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
2237 cmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
2238
2239 cmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;
2240 cmd->ulpBdeCount = 1;
2241 cmd->ulpLe = 1;
2242 cmd->ulpClass = CLASS3;
2243 cmd->ulpContext = txxri;
2244
2245 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
2246 cmdiocbq->vport = phba->pport;
2247
2248 rc = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq, rspiocbq,
2249 (phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT);
2250
2251 if ((rc != IOCB_SUCCESS) || (rsp->ulpStatus != IOCB_SUCCESS)) {
2252 rc = -EIO;
2253 goto err_loopback_test_exit;
2254 }
2255
2256 evt->waiting = 1;
2257 rc = wait_event_interruptible_timeout(
2258 evt->wq, !list_empty(&evt->events_to_see),
2259 ((phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT) * HZ);
2260 evt->waiting = 0;
2261 if (list_empty(&evt->events_to_see))
2262 rc = (rc) ? -EINTR : -ETIMEDOUT;
2263 else {
2264 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2265 list_move(evt->events_to_see.prev, &evt->events_to_get);
2266 evdat = list_entry(evt->events_to_get.prev,
2267 typeof(*evdat), node);
2268 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2269 rx_databuf = evdat->data;
2270 if (evdat->len != full_size) {
2271 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
2272 "1603 Loopback test did not receive expected "
2273 "data length. actual length 0x%x expected "
2274 "length 0x%x\n",
2275 evdat->len, full_size);
2276 rc = -EIO;
2277 } else if (rx_databuf == NULL)
2278 rc = -EIO;
2279 else {
2280 rc = IOCB_SUCCESS;
2281 /* skip over elx loopback header */
2282 rx_databuf += ELX_LOOPBACK_HEADER_SZ;
2283 job->reply->reply_payload_rcv_len =
2284 sg_copy_from_buffer(job->reply_payload.sg_list,
2285 job->reply_payload.sg_cnt,
2286 rx_databuf, size);
2287 job->reply->reply_payload_rcv_len = size;
2288 }
2289 }
2290
2291err_loopback_test_exit:
2292 lpfcdiag_loop_self_unreg(phba, rpi);
2293
2294 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2295 lpfc_bsg_event_unref(evt); /* release ref */
2296 lpfc_bsg_event_unref(evt); /* delete */
2297 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2298
2299 if (cmdiocbq != NULL)
2300 lpfc_sli_release_iocbq(phba, cmdiocbq);
2301
2302 if (rspiocbq != NULL)
2303 lpfc_sli_release_iocbq(phba, rspiocbq);
2304
2305 if (txbmp != NULL) {
2306 if (txbpl != NULL) {
2307 if (txbuffer != NULL)
2308 diag_cmd_data_free(phba, txbuffer);
2309 lpfc_mbuf_free(phba, txbmp->virt, txbmp->phys);
2310 }
2311 kfree(txbmp);
2312 }
2313
2314loopback_test_exit:
2315 kfree(dataout);
2316 /* make error code available to userspace */
2317 job->reply->result = rc;
2318 job->dd_data = NULL;
2319 /* complete the job back to userspace if no error */
2320 if (rc == 0)
2321 job->job_done(job);
2322 return rc;
2323}
2324
2325/**
2326 * lpfc_bsg_get_dfc_rev - process a GET_DFC_REV bsg vendor command
2327 * @job: GET_DFC_REV fc_bsg_job
2328 **/
2329static int
2330lpfc_bsg_get_dfc_rev(struct fc_bsg_job *job)
2331{
2332 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
2333 struct lpfc_hba *phba = vport->phba;
2334 struct get_mgmt_rev *event_req;
2335 struct get_mgmt_rev_reply *event_reply;
2336 int rc = 0;
2337
2338 if (job->request_len <
2339 sizeof(struct fc_bsg_request) + sizeof(struct get_mgmt_rev)) {
2340 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2341 "2740 Received GET_DFC_REV request below "
2342 "minimum size\n");
2343 rc = -EINVAL;
2344 goto job_error;
2345 }
2346
2347 event_req = (struct get_mgmt_rev *)
2348 job->request->rqst_data.h_vendor.vendor_cmd;
2349
2350 event_reply = (struct get_mgmt_rev_reply *)
2351 job->reply->reply_data.vendor_reply.vendor_rsp;
2352
2353 if (job->reply_len <
2354 sizeof(struct fc_bsg_request) + sizeof(struct get_mgmt_rev_reply)) {
2355 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2356 "2741 Received GET_DFC_REV reply below "
2357 "minimum size\n");
2358 rc = -EINVAL;
2359 goto job_error;
2360 }
2361
2362 event_reply->info.a_Major = MANAGEMENT_MAJOR_REV;
2363 event_reply->info.a_Minor = MANAGEMENT_MINOR_REV;
2364job_error:
2365 job->reply->result = rc;
2366 if (rc == 0)
2367 job->job_done(job);
2368 return rc;
2369}
2370
2371/**
2372 * lpfc_bsg_wake_mbox_wait - lpfc_bsg_issue_mbox mbox completion handler
2373 * @phba: Pointer to HBA context object.
2374 * @pmboxq: Pointer to mailbox command.
2375 *
2376 * This is completion handler function for mailbox commands issued from
2377 * lpfc_bsg_issue_mbox function. This function is called by the
2378 * mailbox event handler function with no lock held. This function
2379 * will wake up thread waiting on the wait queue pointed by context1
2380 * of the mailbox.
2381 **/
2382void
2383lpfc_bsg_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
2384{
2385 struct bsg_job_data *dd_data;
James Smart3b5dd522010-01-26 23:10:15 -05002386 struct fc_bsg_job *job;
2387 uint32_t size;
2388 unsigned long flags;
James Smart7a470272010-03-15 11:25:20 -04002389 uint8_t *to;
2390 uint8_t *from;
James Smart3b5dd522010-01-26 23:10:15 -05002391
2392 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2393 dd_data = pmboxq->context1;
James Smart7a470272010-03-15 11:25:20 -04002394 /* job already timed out? */
James Smart3b5dd522010-01-26 23:10:15 -05002395 if (!dd_data) {
2396 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2397 return;
2398 }
2399
James Smart7a470272010-03-15 11:25:20 -04002400 /* build the outgoing buffer to do an sg copy
2401 * the format is the response mailbox followed by any extended
2402 * mailbox data
2403 */
2404 from = (uint8_t *)&pmboxq->u.mb;
2405 to = (uint8_t *)dd_data->context_un.mbox.mb;
2406 memcpy(to, from, sizeof(MAILBOX_t));
2407 /* copy the extended data if any, count is in words */
2408 if (dd_data->context_un.mbox.outWxtWLen) {
2409 from = (uint8_t *)dd_data->context_un.mbox.ext;
2410 to += sizeof(MAILBOX_t);
2411 memcpy(to, from,
2412 dd_data->context_un.mbox.outWxtWLen * sizeof(uint32_t));
2413 }
2414
2415 from = (uint8_t *)dd_data->context_un.mbox.mb;
James Smart3b5dd522010-01-26 23:10:15 -05002416 job = dd_data->context_un.mbox.set_job;
James Smart7a470272010-03-15 11:25:20 -04002417 size = job->reply_payload.payload_len;
James Smart3b5dd522010-01-26 23:10:15 -05002418 job->reply->reply_payload_rcv_len =
2419 sg_copy_from_buffer(job->reply_payload.sg_list,
2420 job->reply_payload.sg_cnt,
James Smart7a470272010-03-15 11:25:20 -04002421 from, size);
James Smart3b5dd522010-01-26 23:10:15 -05002422 job->reply->result = 0;
James Smart7a470272010-03-15 11:25:20 -04002423
James Smart3b5dd522010-01-26 23:10:15 -05002424 dd_data->context_un.mbox.set_job = NULL;
2425 job->dd_data = NULL;
2426 job->job_done(job);
James Smart7a470272010-03-15 11:25:20 -04002427 /* need to hold the lock until we call job done to hold off
2428 * the timeout handler returning to the midlayer while
2429 * we are stillprocessing the job
2430 */
James Smart3b5dd522010-01-26 23:10:15 -05002431 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smart7a470272010-03-15 11:25:20 -04002432
2433 kfree(dd_data->context_un.mbox.mb);
James Smart3b5dd522010-01-26 23:10:15 -05002434 mempool_free(dd_data->context_un.mbox.pmboxq, phba->mbox_mem_pool);
James Smart7a470272010-03-15 11:25:20 -04002435 kfree(dd_data->context_un.mbox.ext);
2436 if (dd_data->context_un.mbox.dmp) {
2437 dma_free_coherent(&phba->pcidev->dev,
2438 dd_data->context_un.mbox.dmp->size,
2439 dd_data->context_un.mbox.dmp->dma.virt,
2440 dd_data->context_un.mbox.dmp->dma.phys);
2441 kfree(dd_data->context_un.mbox.dmp);
2442 }
2443 if (dd_data->context_un.mbox.rxbmp) {
2444 lpfc_mbuf_free(phba, dd_data->context_un.mbox.rxbmp->virt,
2445 dd_data->context_un.mbox.rxbmp->phys);
2446 kfree(dd_data->context_un.mbox.rxbmp);
2447 }
James Smart3b5dd522010-01-26 23:10:15 -05002448 kfree(dd_data);
2449 return;
2450}
2451
2452/**
2453 * lpfc_bsg_check_cmd_access - test for a supported mailbox command
2454 * @phba: Pointer to HBA context object.
2455 * @mb: Pointer to a mailbox object.
2456 * @vport: Pointer to a vport object.
2457 *
2458 * Some commands require the port to be offline, some may not be called from
2459 * the application.
2460 **/
2461static int lpfc_bsg_check_cmd_access(struct lpfc_hba *phba,
2462 MAILBOX_t *mb, struct lpfc_vport *vport)
2463{
2464 /* return negative error values for bsg job */
2465 switch (mb->mbxCommand) {
2466 /* Offline only */
2467 case MBX_INIT_LINK:
2468 case MBX_DOWN_LINK:
2469 case MBX_CONFIG_LINK:
2470 case MBX_CONFIG_RING:
2471 case MBX_RESET_RING:
2472 case MBX_UNREG_LOGIN:
2473 case MBX_CLEAR_LA:
2474 case MBX_DUMP_CONTEXT:
2475 case MBX_RUN_DIAGS:
2476 case MBX_RESTART:
2477 case MBX_SET_MASK:
2478 if (!(vport->fc_flag & FC_OFFLINE_MODE)) {
2479 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2480 "2743 Command 0x%x is illegal in on-line "
2481 "state\n",
2482 mb->mbxCommand);
2483 return -EPERM;
2484 }
2485 case MBX_WRITE_NV:
2486 case MBX_WRITE_VPARMS:
2487 case MBX_LOAD_SM:
2488 case MBX_READ_NV:
2489 case MBX_READ_CONFIG:
2490 case MBX_READ_RCONFIG:
2491 case MBX_READ_STATUS:
2492 case MBX_READ_XRI:
2493 case MBX_READ_REV:
2494 case MBX_READ_LNK_STAT:
2495 case MBX_DUMP_MEMORY:
2496 case MBX_DOWN_LOAD:
2497 case MBX_UPDATE_CFG:
2498 case MBX_KILL_BOARD:
2499 case MBX_LOAD_AREA:
2500 case MBX_LOAD_EXP_ROM:
2501 case MBX_BEACON:
2502 case MBX_DEL_LD_ENTRY:
2503 case MBX_SET_DEBUG:
2504 case MBX_WRITE_WWN:
2505 case MBX_SLI4_CONFIG:
2506 case MBX_READ_EVENT_LOG_STATUS:
2507 case MBX_WRITE_EVENT_LOG:
2508 case MBX_PORT_CAPABILITIES:
2509 case MBX_PORT_IOV_CONTROL:
James Smart7a470272010-03-15 11:25:20 -04002510 case MBX_RUN_BIU_DIAG64:
James Smart3b5dd522010-01-26 23:10:15 -05002511 break;
2512 case MBX_SET_VARIABLE:
James Smarte2aed292010-02-26 14:15:00 -05002513 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2514 "1226 mbox: set_variable 0x%x, 0x%x\n",
2515 mb->un.varWords[0],
2516 mb->un.varWords[1]);
2517 if ((mb->un.varWords[0] == SETVAR_MLOMNT)
2518 && (mb->un.varWords[1] == 1)) {
2519 phba->wait_4_mlo_maint_flg = 1;
2520 } else if (mb->un.varWords[0] == SETVAR_MLORST) {
2521 phba->link_flag &= ~LS_LOOPBACK_MODE;
2522 phba->fc_topology = TOPOLOGY_PT_PT;
2523 }
2524 break;
James Smart3b5dd522010-01-26 23:10:15 -05002525 case MBX_READ_EVENT_LOG:
2526 case MBX_READ_SPARM64:
2527 case MBX_READ_LA:
2528 case MBX_READ_LA64:
2529 case MBX_REG_LOGIN:
2530 case MBX_REG_LOGIN64:
2531 case MBX_CONFIG_PORT:
2532 case MBX_RUN_BIU_DIAG:
2533 default:
2534 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2535 "2742 Unknown Command 0x%x\n",
2536 mb->mbxCommand);
2537 return -EPERM;
2538 }
2539
2540 return 0; /* ok */
2541}
2542
2543/**
2544 * lpfc_bsg_issue_mbox - issues a mailbox command on behalf of an app
2545 * @phba: Pointer to HBA context object.
2546 * @mb: Pointer to a mailbox object.
2547 * @vport: Pointer to a vport object.
2548 *
2549 * Allocate a tracking object, mailbox command memory, get a mailbox
2550 * from the mailbox pool, copy the caller mailbox command.
2551 *
2552 * If offline and the sli is active we need to poll for the command (port is
2553 * being reset) and com-plete the job, otherwise issue the mailbox command and
2554 * let our completion handler finish the command.
2555 **/
2556static uint32_t
2557lpfc_bsg_issue_mbox(struct lpfc_hba *phba, struct fc_bsg_job *job,
2558 struct lpfc_vport *vport)
2559{
James Smart7a470272010-03-15 11:25:20 -04002560 LPFC_MBOXQ_t *pmboxq = NULL; /* internal mailbox queue */
2561 MAILBOX_t *pmb; /* shortcut to the pmboxq mailbox */
2562 /* a 4k buffer to hold the mb and extended data from/to the bsg */
2563 MAILBOX_t *mb = NULL;
2564 struct bsg_job_data *dd_data = NULL; /* bsg data tracking structure */
James Smart3b5dd522010-01-26 23:10:15 -05002565 uint32_t size;
James Smart7a470272010-03-15 11:25:20 -04002566 struct lpfc_dmabuf *rxbmp = NULL; /* for biu diag */
2567 struct lpfc_dmabufext *dmp = NULL; /* for biu diag */
2568 struct ulp_bde64 *rxbpl = NULL;
2569 struct dfc_mbox_req *mbox_req = (struct dfc_mbox_req *)
2570 job->request->rqst_data.h_vendor.vendor_cmd;
2571 uint8_t *ext = NULL;
James Smart3b5dd522010-01-26 23:10:15 -05002572 int rc = 0;
James Smart7a470272010-03-15 11:25:20 -04002573 uint8_t *from;
2574
2575 /* in case no data is transferred */
2576 job->reply->reply_payload_rcv_len = 0;
2577
2578 /* check if requested extended data lengths are valid */
2579 if ((mbox_req->inExtWLen > MAILBOX_EXT_SIZE) ||
2580 (mbox_req->outWxtWLen > MAILBOX_EXT_SIZE)) {
2581 rc = -ERANGE;
2582 goto job_done;
2583 }
James Smart3b5dd522010-01-26 23:10:15 -05002584
2585 /* allocate our bsg tracking structure */
2586 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
2587 if (!dd_data) {
2588 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2589 "2727 Failed allocation of dd_data\n");
James Smart7a470272010-03-15 11:25:20 -04002590 rc = -ENOMEM;
2591 goto job_done;
James Smart3b5dd522010-01-26 23:10:15 -05002592 }
2593
James Smart49198b32010-04-06 15:04:33 -04002594 mb = kzalloc(BSG_MBOX_SIZE, GFP_KERNEL);
James Smart3b5dd522010-01-26 23:10:15 -05002595 if (!mb) {
James Smart7a470272010-03-15 11:25:20 -04002596 rc = -ENOMEM;
2597 goto job_done;
James Smart3b5dd522010-01-26 23:10:15 -05002598 }
2599
2600 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2601 if (!pmboxq) {
James Smart7a470272010-03-15 11:25:20 -04002602 rc = -ENOMEM;
2603 goto job_done;
James Smart3b5dd522010-01-26 23:10:15 -05002604 }
James Smart7a470272010-03-15 11:25:20 -04002605 memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
James Smart3b5dd522010-01-26 23:10:15 -05002606
2607 size = job->request_payload.payload_len;
James Smart7a470272010-03-15 11:25:20 -04002608 sg_copy_to_buffer(job->request_payload.sg_list,
2609 job->request_payload.sg_cnt,
2610 mb, size);
James Smart3b5dd522010-01-26 23:10:15 -05002611
2612 rc = lpfc_bsg_check_cmd_access(phba, mb, vport);
James Smart7a470272010-03-15 11:25:20 -04002613 if (rc != 0)
2614 goto job_done; /* must be negative */
James Smart3b5dd522010-01-26 23:10:15 -05002615
James Smart3b5dd522010-01-26 23:10:15 -05002616 pmb = &pmboxq->u.mb;
2617 memcpy(pmb, mb, sizeof(*pmb));
2618 pmb->mbxOwner = OWN_HOST;
James Smart3b5dd522010-01-26 23:10:15 -05002619 pmboxq->vport = vport;
2620
James Smart7a470272010-03-15 11:25:20 -04002621 /* extended mailbox commands will need an extended buffer */
2622 if (mbox_req->inExtWLen || mbox_req->outWxtWLen) {
2623 ext = kzalloc(MAILBOX_EXT_SIZE, GFP_KERNEL);
2624 if (!ext) {
2625 rc = -ENOMEM;
2626 goto job_done;
James Smart3b5dd522010-01-26 23:10:15 -05002627 }
2628
James Smart7a470272010-03-15 11:25:20 -04002629 /* any data for the device? */
2630 if (mbox_req->inExtWLen) {
2631 from = (uint8_t *)mb;
2632 from += sizeof(MAILBOX_t);
2633 memcpy((uint8_t *)ext, from,
2634 mbox_req->inExtWLen * sizeof(uint32_t));
2635 }
2636
2637 pmboxq->context2 = ext;
2638 pmboxq->in_ext_byte_len =
2639 mbox_req->inExtWLen *
2640 sizeof(uint32_t);
2641 pmboxq->out_ext_byte_len =
2642 mbox_req->outWxtWLen *
2643 sizeof(uint32_t);
2644 pmboxq->mbox_offset_word =
2645 mbox_req->mbOffset;
2646 pmboxq->context2 = ext;
2647 pmboxq->in_ext_byte_len =
2648 mbox_req->inExtWLen * sizeof(uint32_t);
2649 pmboxq->out_ext_byte_len =
2650 mbox_req->outWxtWLen * sizeof(uint32_t);
2651 pmboxq->mbox_offset_word = mbox_req->mbOffset;
2652 }
2653
2654 /* biu diag will need a kernel buffer to transfer the data
2655 * allocate our own buffer and setup the mailbox command to
2656 * use ours
2657 */
2658 if (pmb->mbxCommand == MBX_RUN_BIU_DIAG64) {
2659 rxbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2660 if (!rxbmp) {
2661 rc = -ENOMEM;
2662 goto job_done;
2663 }
2664
2665 rxbmp->virt = lpfc_mbuf_alloc(phba, 0, &rxbmp->phys);
2666 INIT_LIST_HEAD(&rxbmp->list);
2667 rxbpl = (struct ulp_bde64 *) rxbmp->virt;
James Smart49198b32010-04-06 15:04:33 -04002668 dmp = diag_cmd_data_alloc(phba, rxbpl, BSG_MBOX_SIZE, 0);
James Smart7a470272010-03-15 11:25:20 -04002669 if (!dmp) {
2670 rc = -ENOMEM;
2671 goto job_done;
2672 }
2673
James Smart7a470272010-03-15 11:25:20 -04002674 INIT_LIST_HEAD(&dmp->dma.list);
2675 pmb->un.varBIUdiag.un.s2.xmit_bde64.addrHigh =
2676 putPaddrHigh(dmp->dma.phys);
2677 pmb->un.varBIUdiag.un.s2.xmit_bde64.addrLow =
2678 putPaddrLow(dmp->dma.phys);
2679
2680 pmb->un.varBIUdiag.un.s2.rcv_bde64.addrHigh =
2681 putPaddrHigh(dmp->dma.phys +
2682 pmb->un.varBIUdiag.un.s2.
2683 xmit_bde64.tus.f.bdeSize);
2684 pmb->un.varBIUdiag.un.s2.rcv_bde64.addrLow =
2685 putPaddrLow(dmp->dma.phys +
2686 pmb->un.varBIUdiag.un.s2.
2687 xmit_bde64.tus.f.bdeSize);
2688 dd_data->context_un.mbox.rxbmp = rxbmp;
2689 dd_data->context_un.mbox.dmp = dmp;
2690 } else {
2691 dd_data->context_un.mbox.rxbmp = NULL;
2692 dd_data->context_un.mbox.dmp = NULL;
James Smart3b5dd522010-01-26 23:10:15 -05002693 }
2694
2695 /* setup wake call as IOCB callback */
2696 pmboxq->mbox_cmpl = lpfc_bsg_wake_mbox_wait;
James Smart7a470272010-03-15 11:25:20 -04002697
James Smart3b5dd522010-01-26 23:10:15 -05002698 /* setup context field to pass wait_queue pointer to wake function */
2699 pmboxq->context1 = dd_data;
2700 dd_data->type = TYPE_MBOX;
2701 dd_data->context_un.mbox.pmboxq = pmboxq;
2702 dd_data->context_un.mbox.mb = mb;
2703 dd_data->context_un.mbox.set_job = job;
James Smart7a470272010-03-15 11:25:20 -04002704 dd_data->context_un.mbox.ext = ext;
2705 dd_data->context_un.mbox.mbOffset = mbox_req->mbOffset;
2706 dd_data->context_un.mbox.inExtWLen = mbox_req->inExtWLen;
2707 dd_data->context_un.mbox.outWxtWLen = mbox_req->outWxtWLen;
James Smart3b5dd522010-01-26 23:10:15 -05002708 job->dd_data = dd_data;
James Smart7a470272010-03-15 11:25:20 -04002709
2710 if ((vport->fc_flag & FC_OFFLINE_MODE) ||
2711 (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))) {
2712 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
2713 if (rc != MBX_SUCCESS) {
2714 rc = (rc == MBX_TIMEOUT) ? -ETIME : -ENODEV;
2715 goto job_done;
2716 }
2717
2718 /* job finished, copy the data */
2719 memcpy(mb, pmb, sizeof(*pmb));
2720 job->reply->reply_payload_rcv_len =
2721 sg_copy_from_buffer(job->reply_payload.sg_list,
2722 job->reply_payload.sg_cnt,
2723 mb, size);
2724 /* not waiting mbox already done */
2725 rc = 0;
2726 goto job_done;
James Smart3b5dd522010-01-26 23:10:15 -05002727 }
2728
James Smart7a470272010-03-15 11:25:20 -04002729 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
2730 if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY))
2731 return 1; /* job started */
2732
2733job_done:
2734 /* common exit for error or job completed inline */
2735 kfree(mb);
2736 if (pmboxq)
2737 mempool_free(pmboxq, phba->mbox_mem_pool);
2738 kfree(ext);
2739 if (dmp) {
2740 dma_free_coherent(&phba->pcidev->dev,
2741 dmp->size, dmp->dma.virt,
2742 dmp->dma.phys);
2743 kfree(dmp);
2744 }
2745 if (rxbmp) {
2746 lpfc_mbuf_free(phba, rxbmp->virt, rxbmp->phys);
2747 kfree(rxbmp);
2748 }
2749 kfree(dd_data);
2750
2751 return rc;
James Smart3b5dd522010-01-26 23:10:15 -05002752}
2753
2754/**
2755 * lpfc_bsg_mbox_cmd - process an fc bsg LPFC_BSG_VENDOR_MBOX command
2756 * @job: MBOX fc_bsg_job for LPFC_BSG_VENDOR_MBOX.
2757 **/
2758static int
2759lpfc_bsg_mbox_cmd(struct fc_bsg_job *job)
2760{
2761 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
2762 struct lpfc_hba *phba = vport->phba;
2763 int rc = 0;
2764
2765 /* in case no data is transferred */
2766 job->reply->reply_payload_rcv_len = 0;
2767 if (job->request_len <
2768 sizeof(struct fc_bsg_request) + sizeof(struct dfc_mbox_req)) {
2769 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2770 "2737 Received MBOX_REQ request below "
2771 "minimum size\n");
2772 rc = -EINVAL;
2773 goto job_error;
2774 }
2775
James Smart49198b32010-04-06 15:04:33 -04002776 if (job->request_payload.payload_len != BSG_MBOX_SIZE) {
James Smart3b5dd522010-01-26 23:10:15 -05002777 rc = -EINVAL;
2778 goto job_error;
2779 }
2780
James Smart49198b32010-04-06 15:04:33 -04002781 if (job->reply_payload.payload_len != BSG_MBOX_SIZE) {
James Smart7a470272010-03-15 11:25:20 -04002782 rc = -EINVAL;
2783 goto job_error;
2784 }
2785
James Smart3b5dd522010-01-26 23:10:15 -05002786 if (phba->sli.sli_flag & LPFC_BLOCK_MGMT_IO) {
2787 rc = -EAGAIN;
2788 goto job_error;
2789 }
2790
2791 rc = lpfc_bsg_issue_mbox(phba, job, vport);
2792
2793job_error:
2794 if (rc == 0) {
2795 /* job done */
2796 job->reply->result = 0;
2797 job->dd_data = NULL;
2798 job->job_done(job);
2799 } else if (rc == 1)
2800 /* job submitted, will complete later*/
2801 rc = 0; /* return zero, no error */
2802 else {
2803 /* some error occurred */
2804 job->reply->result = rc;
2805 job->dd_data = NULL;
2806 }
2807
2808 return rc;
2809}
2810
2811/**
James Smarte2aed292010-02-26 14:15:00 -05002812 * lpfc_bsg_menlo_cmd_cmp - lpfc_menlo_cmd completion handler
2813 * @phba: Pointer to HBA context object.
2814 * @cmdiocbq: Pointer to command iocb.
2815 * @rspiocbq: Pointer to response iocb.
2816 *
2817 * This function is the completion handler for iocbs issued using
2818 * lpfc_menlo_cmd function. This function is called by the
2819 * ring event handler function without any lock held. This function
2820 * can be called from both worker thread context and interrupt
2821 * context. This function also can be called from another thread which
2822 * cleans up the SLI layer objects.
2823 * This function copies the contents of the response iocb to the
2824 * response iocb memory object provided by the caller of
2825 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
2826 * sleeps for the iocb completion.
2827 **/
2828static void
2829lpfc_bsg_menlo_cmd_cmp(struct lpfc_hba *phba,
2830 struct lpfc_iocbq *cmdiocbq,
2831 struct lpfc_iocbq *rspiocbq)
2832{
2833 struct bsg_job_data *dd_data;
2834 struct fc_bsg_job *job;
2835 IOCB_t *rsp;
2836 struct lpfc_dmabuf *bmp;
2837 struct lpfc_bsg_menlo *menlo;
2838 unsigned long flags;
2839 struct menlo_response *menlo_resp;
2840 int rc = 0;
2841
2842 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2843 dd_data = cmdiocbq->context1;
2844 if (!dd_data) {
2845 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2846 return;
2847 }
2848
2849 menlo = &dd_data->context_un.menlo;
2850 job = menlo->set_job;
2851 job->dd_data = NULL; /* so timeout handler does not reply */
2852
2853 spin_lock_irqsave(&phba->hbalock, flags);
2854 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
2855 if (cmdiocbq->context2 && rspiocbq)
2856 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
2857 &rspiocbq->iocb, sizeof(IOCB_t));
2858 spin_unlock_irqrestore(&phba->hbalock, flags);
2859
2860 bmp = menlo->bmp;
2861 rspiocbq = menlo->rspiocbq;
2862 rsp = &rspiocbq->iocb;
2863
2864 pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
2865 job->request_payload.sg_cnt, DMA_TO_DEVICE);
2866 pci_unmap_sg(phba->pcidev, job->reply_payload.sg_list,
2867 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
2868
2869 /* always return the xri, this would be used in the case
2870 * of a menlo download to allow the data to be sent as a continuation
2871 * of the exchange.
2872 */
2873 menlo_resp = (struct menlo_response *)
2874 job->reply->reply_data.vendor_reply.vendor_rsp;
2875 menlo_resp->xri = rsp->ulpContext;
2876 if (rsp->ulpStatus) {
2877 if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
2878 switch (rsp->un.ulpWord[4] & 0xff) {
2879 case IOERR_SEQUENCE_TIMEOUT:
2880 rc = -ETIMEDOUT;
2881 break;
2882 case IOERR_INVALID_RPI:
2883 rc = -EFAULT;
2884 break;
2885 default:
2886 rc = -EACCES;
2887 break;
2888 }
2889 } else
2890 rc = -EACCES;
2891 } else
2892 job->reply->reply_payload_rcv_len =
2893 rsp->un.genreq64.bdl.bdeSize;
2894
2895 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
2896 lpfc_sli_release_iocbq(phba, rspiocbq);
2897 lpfc_sli_release_iocbq(phba, cmdiocbq);
2898 kfree(bmp);
2899 kfree(dd_data);
2900 /* make error code available to userspace */
2901 job->reply->result = rc;
2902 /* complete the job back to userspace */
2903 job->job_done(job);
2904 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2905 return;
2906}
2907
2908/**
2909 * lpfc_menlo_cmd - send an ioctl for menlo hardware
2910 * @job: fc_bsg_job to handle
2911 *
2912 * This function issues a gen request 64 CR ioctl for all menlo cmd requests,
2913 * all the command completions will return the xri for the command.
2914 * For menlo data requests a gen request 64 CX is used to continue the exchange
2915 * supplied in the menlo request header xri field.
2916 **/
2917static int
2918lpfc_menlo_cmd(struct fc_bsg_job *job)
2919{
2920 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
2921 struct lpfc_hba *phba = vport->phba;
2922 struct lpfc_iocbq *cmdiocbq, *rspiocbq;
2923 IOCB_t *cmd, *rsp;
2924 int rc = 0;
2925 struct menlo_command *menlo_cmd;
2926 struct menlo_response *menlo_resp;
2927 struct lpfc_dmabuf *bmp = NULL;
2928 int request_nseg;
2929 int reply_nseg;
2930 struct scatterlist *sgel = NULL;
2931 int numbde;
2932 dma_addr_t busaddr;
2933 struct bsg_job_data *dd_data;
2934 struct ulp_bde64 *bpl = NULL;
2935
2936 /* in case no data is returned return just the return code */
2937 job->reply->reply_payload_rcv_len = 0;
2938
2939 if (job->request_len <
2940 sizeof(struct fc_bsg_request) +
2941 sizeof(struct menlo_command)) {
2942 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2943 "2784 Received MENLO_CMD request below "
2944 "minimum size\n");
2945 rc = -ERANGE;
2946 goto no_dd_data;
2947 }
2948
2949 if (job->reply_len <
2950 sizeof(struct fc_bsg_request) + sizeof(struct menlo_response)) {
2951 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2952 "2785 Received MENLO_CMD reply below "
2953 "minimum size\n");
2954 rc = -ERANGE;
2955 goto no_dd_data;
2956 }
2957
2958 if (!(phba->menlo_flag & HBA_MENLO_SUPPORT)) {
2959 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2960 "2786 Adapter does not support menlo "
2961 "commands\n");
2962 rc = -EPERM;
2963 goto no_dd_data;
2964 }
2965
2966 menlo_cmd = (struct menlo_command *)
2967 job->request->rqst_data.h_vendor.vendor_cmd;
2968
2969 menlo_resp = (struct menlo_response *)
2970 job->reply->reply_data.vendor_reply.vendor_rsp;
2971
2972 /* allocate our bsg tracking structure */
2973 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
2974 if (!dd_data) {
2975 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2976 "2787 Failed allocation of dd_data\n");
2977 rc = -ENOMEM;
2978 goto no_dd_data;
2979 }
2980
2981 bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2982 if (!bmp) {
2983 rc = -ENOMEM;
2984 goto free_dd;
2985 }
2986
2987 cmdiocbq = lpfc_sli_get_iocbq(phba);
2988 if (!cmdiocbq) {
2989 rc = -ENOMEM;
2990 goto free_bmp;
2991 }
2992
2993 rspiocbq = lpfc_sli_get_iocbq(phba);
2994 if (!rspiocbq) {
2995 rc = -ENOMEM;
2996 goto free_cmdiocbq;
2997 }
2998
2999 rsp = &rspiocbq->iocb;
3000
3001 bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
3002 if (!bmp->virt) {
3003 rc = -ENOMEM;
3004 goto free_rspiocbq;
3005 }
3006
3007 INIT_LIST_HEAD(&bmp->list);
3008 bpl = (struct ulp_bde64 *) bmp->virt;
3009 request_nseg = pci_map_sg(phba->pcidev, job->request_payload.sg_list,
3010 job->request_payload.sg_cnt, DMA_TO_DEVICE);
3011 for_each_sg(job->request_payload.sg_list, sgel, request_nseg, numbde) {
3012 busaddr = sg_dma_address(sgel);
3013 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
3014 bpl->tus.f.bdeSize = sg_dma_len(sgel);
3015 bpl->tus.w = cpu_to_le32(bpl->tus.w);
3016 bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
3017 bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
3018 bpl++;
3019 }
3020
3021 reply_nseg = pci_map_sg(phba->pcidev, job->reply_payload.sg_list,
3022 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
3023 for_each_sg(job->reply_payload.sg_list, sgel, reply_nseg, numbde) {
3024 busaddr = sg_dma_address(sgel);
3025 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
3026 bpl->tus.f.bdeSize = sg_dma_len(sgel);
3027 bpl->tus.w = cpu_to_le32(bpl->tus.w);
3028 bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
3029 bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
3030 bpl++;
3031 }
3032
3033 cmd = &cmdiocbq->iocb;
3034 cmd->un.genreq64.bdl.ulpIoTag32 = 0;
3035 cmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
3036 cmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
3037 cmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
3038 cmd->un.genreq64.bdl.bdeSize =
3039 (request_nseg + reply_nseg) * sizeof(struct ulp_bde64);
3040 cmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
3041 cmd->un.genreq64.w5.hcsw.Dfctl = 0;
3042 cmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CMD;
3043 cmd->un.genreq64.w5.hcsw.Type = MENLO_TRANSPORT_TYPE; /* 0xfe */
3044 cmd->ulpBdeCount = 1;
3045 cmd->ulpClass = CLASS3;
3046 cmd->ulpOwner = OWN_CHIP;
3047 cmd->ulpLe = 1; /* Limited Edition */
3048 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
3049 cmdiocbq->vport = phba->pport;
3050 /* We want the firmware to timeout before we do */
3051 cmd->ulpTimeout = MENLO_TIMEOUT - 5;
3052 cmdiocbq->context3 = bmp;
3053 cmdiocbq->context2 = rspiocbq;
3054 cmdiocbq->iocb_cmpl = lpfc_bsg_menlo_cmd_cmp;
3055 cmdiocbq->context1 = dd_data;
3056 cmdiocbq->context2 = rspiocbq;
3057 if (menlo_cmd->cmd == LPFC_BSG_VENDOR_MENLO_CMD) {
3058 cmd->ulpCommand = CMD_GEN_REQUEST64_CR;
3059 cmd->ulpPU = MENLO_PU; /* 3 */
3060 cmd->un.ulpWord[4] = MENLO_DID; /* 0x0000FC0E */
3061 cmd->ulpContext = MENLO_CONTEXT; /* 0 */
3062 } else {
3063 cmd->ulpCommand = CMD_GEN_REQUEST64_CX;
3064 cmd->ulpPU = 1;
3065 cmd->un.ulpWord[4] = 0;
3066 cmd->ulpContext = menlo_cmd->xri;
3067 }
3068
3069 dd_data->type = TYPE_MENLO;
3070 dd_data->context_un.menlo.cmdiocbq = cmdiocbq;
3071 dd_data->context_un.menlo.rspiocbq = rspiocbq;
3072 dd_data->context_un.menlo.set_job = job;
3073 dd_data->context_un.menlo.bmp = bmp;
3074
3075 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq,
3076 MENLO_TIMEOUT - 5);
3077 if (rc == IOCB_SUCCESS)
3078 return 0; /* done for now */
3079
3080 /* iocb failed so cleanup */
3081 pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
3082 job->request_payload.sg_cnt, DMA_TO_DEVICE);
3083 pci_unmap_sg(phba->pcidev, job->reply_payload.sg_list,
3084 job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
3085
3086 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
3087
3088free_rspiocbq:
3089 lpfc_sli_release_iocbq(phba, rspiocbq);
3090free_cmdiocbq:
3091 lpfc_sli_release_iocbq(phba, cmdiocbq);
3092free_bmp:
3093 kfree(bmp);
3094free_dd:
3095 kfree(dd_data);
3096no_dd_data:
3097 /* make error code available to userspace */
3098 job->reply->result = rc;
3099 job->dd_data = NULL;
3100 return rc;
3101}
3102/**
James Smartf1c3b0f2009-07-19 10:01:32 -04003103 * lpfc_bsg_hst_vendor - process a vendor-specific fc_bsg_job
3104 * @job: fc_bsg_job to handle
James Smart3b5dd522010-01-26 23:10:15 -05003105 **/
James Smartf1c3b0f2009-07-19 10:01:32 -04003106static int
3107lpfc_bsg_hst_vendor(struct fc_bsg_job *job)
3108{
3109 int command = job->request->rqst_data.h_vendor.vendor_cmd[0];
James Smart4cc0e562010-01-26 23:09:48 -05003110 int rc;
James Smartf1c3b0f2009-07-19 10:01:32 -04003111
3112 switch (command) {
3113 case LPFC_BSG_VENDOR_SET_CT_EVENT:
James Smart4cc0e562010-01-26 23:09:48 -05003114 rc = lpfc_bsg_hba_set_event(job);
James Smartf1c3b0f2009-07-19 10:01:32 -04003115 break;
James Smartf1c3b0f2009-07-19 10:01:32 -04003116 case LPFC_BSG_VENDOR_GET_CT_EVENT:
James Smart4cc0e562010-01-26 23:09:48 -05003117 rc = lpfc_bsg_hba_get_event(job);
James Smartf1c3b0f2009-07-19 10:01:32 -04003118 break;
James Smart3b5dd522010-01-26 23:10:15 -05003119 case LPFC_BSG_VENDOR_SEND_MGMT_RESP:
3120 rc = lpfc_bsg_send_mgmt_rsp(job);
3121 break;
3122 case LPFC_BSG_VENDOR_DIAG_MODE:
3123 rc = lpfc_bsg_diag_mode(job);
3124 break;
3125 case LPFC_BSG_VENDOR_DIAG_TEST:
3126 rc = lpfc_bsg_diag_test(job);
3127 break;
3128 case LPFC_BSG_VENDOR_GET_MGMT_REV:
3129 rc = lpfc_bsg_get_dfc_rev(job);
3130 break;
3131 case LPFC_BSG_VENDOR_MBOX:
3132 rc = lpfc_bsg_mbox_cmd(job);
3133 break;
James Smarte2aed292010-02-26 14:15:00 -05003134 case LPFC_BSG_VENDOR_MENLO_CMD:
3135 case LPFC_BSG_VENDOR_MENLO_DATA:
3136 rc = lpfc_menlo_cmd(job);
3137 break;
James Smartf1c3b0f2009-07-19 10:01:32 -04003138 default:
James Smart4cc0e562010-01-26 23:09:48 -05003139 rc = -EINVAL;
3140 job->reply->reply_payload_rcv_len = 0;
3141 /* make error code available to userspace */
3142 job->reply->result = rc;
3143 break;
James Smartf1c3b0f2009-07-19 10:01:32 -04003144 }
James Smart4cc0e562010-01-26 23:09:48 -05003145
3146 return rc;
James Smartf1c3b0f2009-07-19 10:01:32 -04003147}
3148
3149/**
3150 * lpfc_bsg_request - handle a bsg request from the FC transport
3151 * @job: fc_bsg_job to handle
James Smart3b5dd522010-01-26 23:10:15 -05003152 **/
James Smartf1c3b0f2009-07-19 10:01:32 -04003153int
3154lpfc_bsg_request(struct fc_bsg_job *job)
3155{
3156 uint32_t msgcode;
James Smart4cc0e562010-01-26 23:09:48 -05003157 int rc;
James Smartf1c3b0f2009-07-19 10:01:32 -04003158
3159 msgcode = job->request->msgcode;
James Smartf1c3b0f2009-07-19 10:01:32 -04003160 switch (msgcode) {
3161 case FC_BSG_HST_VENDOR:
3162 rc = lpfc_bsg_hst_vendor(job);
3163 break;
3164 case FC_BSG_RPT_ELS:
3165 rc = lpfc_bsg_rport_els(job);
3166 break;
3167 case FC_BSG_RPT_CT:
James Smart4cc0e562010-01-26 23:09:48 -05003168 rc = lpfc_bsg_send_mgmt_cmd(job);
James Smartf1c3b0f2009-07-19 10:01:32 -04003169 break;
3170 default:
James Smart4cc0e562010-01-26 23:09:48 -05003171 rc = -EINVAL;
3172 job->reply->reply_payload_rcv_len = 0;
3173 /* make error code available to userspace */
3174 job->reply->result = rc;
James Smartf1c3b0f2009-07-19 10:01:32 -04003175 break;
3176 }
3177
3178 return rc;
3179}
3180
3181/**
3182 * lpfc_bsg_timeout - handle timeout of a bsg request from the FC transport
3183 * @job: fc_bsg_job that has timed out
3184 *
3185 * This function just aborts the job's IOCB. The aborted IOCB will return to
3186 * the waiting function which will handle passing the error back to userspace
James Smart3b5dd522010-01-26 23:10:15 -05003187 **/
James Smartf1c3b0f2009-07-19 10:01:32 -04003188int
3189lpfc_bsg_timeout(struct fc_bsg_job *job)
3190{
3191 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
3192 struct lpfc_hba *phba = vport->phba;
James Smart4cc0e562010-01-26 23:09:48 -05003193 struct lpfc_iocbq *cmdiocb;
3194 struct lpfc_bsg_event *evt;
3195 struct lpfc_bsg_iocb *iocb;
James Smart3b5dd522010-01-26 23:10:15 -05003196 struct lpfc_bsg_mbox *mbox;
James Smarte2aed292010-02-26 14:15:00 -05003197 struct lpfc_bsg_menlo *menlo;
James Smartf1c3b0f2009-07-19 10:01:32 -04003198 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart4cc0e562010-01-26 23:09:48 -05003199 struct bsg_job_data *dd_data;
3200 unsigned long flags;
James Smartf1c3b0f2009-07-19 10:01:32 -04003201
James Smart4cc0e562010-01-26 23:09:48 -05003202 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3203 dd_data = (struct bsg_job_data *)job->dd_data;
3204 /* timeout and completion crossed paths if no dd_data */
3205 if (!dd_data) {
3206 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3207 return 0;
3208 }
3209
3210 switch (dd_data->type) {
3211 case TYPE_IOCB:
3212 iocb = &dd_data->context_un.iocb;
3213 cmdiocb = iocb->cmdiocbq;
3214 /* hint to completion handler that the job timed out */
3215 job->reply->result = -EAGAIN;
3216 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3217 /* this will call our completion handler */
3218 spin_lock_irq(&phba->hbalock);
James Smartf1c3b0f2009-07-19 10:01:32 -04003219 lpfc_sli_issue_abort_iotag(phba, pring, cmdiocb);
James Smart4cc0e562010-01-26 23:09:48 -05003220 spin_unlock_irq(&phba->hbalock);
3221 break;
3222 case TYPE_EVT:
3223 evt = dd_data->context_un.evt;
3224 /* this event has no job anymore */
3225 evt->set_job = NULL;
3226 job->dd_data = NULL;
3227 job->reply->reply_payload_rcv_len = 0;
3228 /* Return -EAGAIN which is our way of signallying the
3229 * app to retry.
3230 */
3231 job->reply->result = -EAGAIN;
3232 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3233 job->job_done(job);
3234 break;
James Smart3b5dd522010-01-26 23:10:15 -05003235 case TYPE_MBOX:
3236 mbox = &dd_data->context_un.mbox;
3237 /* this mbox has no job anymore */
3238 mbox->set_job = NULL;
3239 job->dd_data = NULL;
3240 job->reply->reply_payload_rcv_len = 0;
3241 job->reply->result = -EAGAIN;
James Smart7a470272010-03-15 11:25:20 -04003242 /* the mbox completion handler can now be run */
James Smart3b5dd522010-01-26 23:10:15 -05003243 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3244 job->job_done(job);
3245 break;
James Smarte2aed292010-02-26 14:15:00 -05003246 case TYPE_MENLO:
3247 menlo = &dd_data->context_un.menlo;
3248 cmdiocb = menlo->cmdiocbq;
3249 /* hint to completion handler that the job timed out */
3250 job->reply->result = -EAGAIN;
3251 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3252 /* this will call our completion handler */
3253 spin_lock_irq(&phba->hbalock);
3254 lpfc_sli_issue_abort_iotag(phba, pring, cmdiocb);
3255 spin_unlock_irq(&phba->hbalock);
3256 break;
James Smart4cc0e562010-01-26 23:09:48 -05003257 default:
3258 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3259 break;
3260 }
James Smartf1c3b0f2009-07-19 10:01:32 -04003261
James Smart4cc0e562010-01-26 23:09:48 -05003262 /* scsi transport fc fc_bsg_job_timeout expects a zero return code,
3263 * otherwise an error message will be displayed on the console
3264 * so always return success (zero)
3265 */
James Smartf1c3b0f2009-07-19 10:01:32 -04003266 return 0;
3267}