blob: a58bffb3f68f9b2af8cfbb3425e0cb414b2bffdb [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 Smartb857ff32014-04-04 13:52:50 -04004 * Copyright (C) 2009-2014 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 Smart7ad20aa2011-05-24 11:44:28 -040026#include <linux/list.h>
James Smartf1c3b0f2009-07-19 10:01:32 -040027
28#include <scsi/scsi.h>
29#include <scsi/scsi_host.h>
30#include <scsi/scsi_transport_fc.h>
31#include <scsi/scsi_bsg_fc.h>
James Smart6a9c52c2009-10-02 15:16:51 -040032#include <scsi/fc/fc_fs.h>
James Smartf1c3b0f2009-07-19 10:01:32 -040033
34#include "lpfc_hw4.h"
35#include "lpfc_hw.h"
36#include "lpfc_sli.h"
37#include "lpfc_sli4.h"
38#include "lpfc_nl.h"
James Smart4fede782010-01-26 23:08:55 -050039#include "lpfc_bsg.h"
James Smartf1c3b0f2009-07-19 10:01:32 -040040#include "lpfc_disc.h"
41#include "lpfc_scsi.h"
42#include "lpfc.h"
43#include "lpfc_logmsg.h"
44#include "lpfc_crtn.h"
James Smartb76f2dc2011-07-22 18:37:42 -040045#include "lpfc_debugfs.h"
James Smartf1c3b0f2009-07-19 10:01:32 -040046#include "lpfc_vport.h"
47#include "lpfc_version.h"
48
James Smart4cc0e562010-01-26 23:09:48 -050049struct lpfc_bsg_event {
50 struct list_head node;
51 struct kref kref;
52 wait_queue_head_t wq;
53
54 /* Event type and waiter identifiers */
55 uint32_t type_mask;
56 uint32_t req_id;
57 uint32_t reg_id;
58
59 /* next two flags are here for the auto-delete logic */
60 unsigned long wait_time_stamp;
61 int waiting;
62
63 /* seen and not seen events */
64 struct list_head events_to_get;
65 struct list_head events_to_see;
66
James Smarta33c4f72013-03-01 16:36:00 -050067 /* driver data associated with the job */
68 void *dd_data;
James Smart4cc0e562010-01-26 23:09:48 -050069};
70
71struct lpfc_bsg_iocb {
72 struct lpfc_iocbq *cmdiocbq;
James Smarta33c4f72013-03-01 16:36:00 -050073 struct lpfc_dmabuf *rmp;
James Smart4cc0e562010-01-26 23:09:48 -050074 struct lpfc_nodelist *ndlp;
James Smart4cc0e562010-01-26 23:09:48 -050075};
76
James Smart3b5dd522010-01-26 23:10:15 -050077struct lpfc_bsg_mbox {
78 LPFC_MBOXQ_t *pmboxq;
79 MAILBOX_t *mb;
James Smart7ad20aa2011-05-24 11:44:28 -040080 struct lpfc_dmabuf *dmabuffers; /* for BIU diags */
James Smart7a470272010-03-15 11:25:20 -040081 uint8_t *ext; /* extended mailbox data */
82 uint32_t mbOffset; /* from app */
83 uint32_t inExtWLen; /* from app */
James Smartc7495932010-04-06 15:05:28 -040084 uint32_t outExtWLen; /* from app */
James Smart3b5dd522010-01-26 23:10:15 -050085};
86
James Smarte2aed292010-02-26 14:15:00 -050087#define MENLO_DID 0x0000FC0E
88
89struct lpfc_bsg_menlo {
90 struct lpfc_iocbq *cmdiocbq;
James Smarta33c4f72013-03-01 16:36:00 -050091 struct lpfc_dmabuf *rmp;
James Smarte2aed292010-02-26 14:15:00 -050092};
93
James Smart4cc0e562010-01-26 23:09:48 -050094#define TYPE_EVT 1
95#define TYPE_IOCB 2
James Smart3b5dd522010-01-26 23:10:15 -050096#define TYPE_MBOX 3
James Smarte2aed292010-02-26 14:15:00 -050097#define TYPE_MENLO 4
James Smart4cc0e562010-01-26 23:09:48 -050098struct bsg_job_data {
99 uint32_t type;
James Smarta33c4f72013-03-01 16:36:00 -0500100 struct fc_bsg_job *set_job; /* job waiting for this iocb to finish */
James Smart4cc0e562010-01-26 23:09:48 -0500101 union {
102 struct lpfc_bsg_event *evt;
103 struct lpfc_bsg_iocb iocb;
James Smart3b5dd522010-01-26 23:10:15 -0500104 struct lpfc_bsg_mbox mbox;
James Smarte2aed292010-02-26 14:15:00 -0500105 struct lpfc_bsg_menlo menlo;
James Smart4cc0e562010-01-26 23:09:48 -0500106 } context_un;
107};
108
109struct event_data {
110 struct list_head node;
111 uint32_t type;
112 uint32_t immed_dat;
113 void *data;
114 uint32_t len;
115};
116
James Smart3b5dd522010-01-26 23:10:15 -0500117#define BUF_SZ_4K 4096
James Smart4cc0e562010-01-26 23:09:48 -0500118#define SLI_CT_ELX_LOOPBACK 0x10
119
120enum ELX_LOOPBACK_CMD {
121 ELX_LOOPBACK_XRI_SETUP,
122 ELX_LOOPBACK_DATA,
123};
124
James Smart3b5dd522010-01-26 23:10:15 -0500125#define ELX_LOOPBACK_HEADER_SZ \
126 (size_t)(&((struct lpfc_sli_ct_request *)NULL)->un)
127
James Smart4cc0e562010-01-26 23:09:48 -0500128struct lpfc_dmabufext {
129 struct lpfc_dmabuf dma;
130 uint32_t size;
131 uint32_t flag;
132};
133
James Smarta33c4f72013-03-01 16:36:00 -0500134static void
135lpfc_free_bsg_buffers(struct lpfc_hba *phba, struct lpfc_dmabuf *mlist)
136{
137 struct lpfc_dmabuf *mlast, *next_mlast;
138
139 if (mlist) {
140 list_for_each_entry_safe(mlast, next_mlast, &mlist->list,
141 list) {
142 lpfc_mbuf_free(phba, mlast->virt, mlast->phys);
143 list_del(&mlast->list);
144 kfree(mlast);
145 }
146 lpfc_mbuf_free(phba, mlist->virt, mlist->phys);
147 kfree(mlist);
148 }
149 return;
150}
151
152static struct lpfc_dmabuf *
153lpfc_alloc_bsg_buffers(struct lpfc_hba *phba, unsigned int size,
154 int outbound_buffers, struct ulp_bde64 *bpl,
155 int *bpl_entries)
156{
157 struct lpfc_dmabuf *mlist = NULL;
158 struct lpfc_dmabuf *mp;
159 unsigned int bytes_left = size;
160
161 /* Verify we can support the size specified */
162 if (!size || (size > (*bpl_entries * LPFC_BPL_SIZE)))
163 return NULL;
164
165 /* Determine the number of dma buffers to allocate */
166 *bpl_entries = (size % LPFC_BPL_SIZE ? size/LPFC_BPL_SIZE + 1 :
167 size/LPFC_BPL_SIZE);
168
169 /* Allocate dma buffer and place in BPL passed */
170 while (bytes_left) {
171 /* Allocate dma buffer */
172 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
173 if (!mp) {
174 if (mlist)
175 lpfc_free_bsg_buffers(phba, mlist);
176 return NULL;
177 }
178
179 INIT_LIST_HEAD(&mp->list);
180 mp->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &(mp->phys));
181
182 if (!mp->virt) {
183 kfree(mp);
184 if (mlist)
185 lpfc_free_bsg_buffers(phba, mlist);
186 return NULL;
187 }
188
189 /* Queue it to a linked list */
190 if (!mlist)
191 mlist = mp;
192 else
193 list_add_tail(&mp->list, &mlist->list);
194
195 /* Add buffer to buffer pointer list */
196 if (outbound_buffers)
197 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
198 else
199 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
200 bpl->addrLow = le32_to_cpu(putPaddrLow(mp->phys));
201 bpl->addrHigh = le32_to_cpu(putPaddrHigh(mp->phys));
202 bpl->tus.f.bdeSize = (uint16_t)
203 (bytes_left >= LPFC_BPL_SIZE ? LPFC_BPL_SIZE :
204 bytes_left);
205 bytes_left -= bpl->tus.f.bdeSize;
206 bpl->tus.w = le32_to_cpu(bpl->tus.w);
207 bpl++;
208 }
209 return mlist;
210}
211
212static unsigned int
213lpfc_bsg_copy_data(struct lpfc_dmabuf *dma_buffers,
214 struct fc_bsg_buffer *bsg_buffers,
215 unsigned int bytes_to_transfer, int to_buffers)
216{
217
218 struct lpfc_dmabuf *mp;
219 unsigned int transfer_bytes, bytes_copied = 0;
220 unsigned int sg_offset, dma_offset;
221 unsigned char *dma_address, *sg_address;
James Smarta33c4f72013-03-01 16:36:00 -0500222 LIST_HEAD(temp_list);
James Smartd5ce53b2013-04-17 20:17:26 -0400223 struct sg_mapping_iter miter;
224 unsigned long flags;
225 unsigned int sg_flags = SG_MITER_ATOMIC;
226 bool sg_valid;
James Smarta33c4f72013-03-01 16:36:00 -0500227
228 list_splice_init(&dma_buffers->list, &temp_list);
229 list_add(&dma_buffers->list, &temp_list);
230 sg_offset = 0;
James Smartd5ce53b2013-04-17 20:17:26 -0400231 if (to_buffers)
232 sg_flags |= SG_MITER_FROM_SG;
233 else
234 sg_flags |= SG_MITER_TO_SG;
235 sg_miter_start(&miter, bsg_buffers->sg_list, bsg_buffers->sg_cnt,
236 sg_flags);
237 local_irq_save(flags);
238 sg_valid = sg_miter_next(&miter);
James Smarta33c4f72013-03-01 16:36:00 -0500239 list_for_each_entry(mp, &temp_list, list) {
240 dma_offset = 0;
James Smartd5ce53b2013-04-17 20:17:26 -0400241 while (bytes_to_transfer && sg_valid &&
James Smarta33c4f72013-03-01 16:36:00 -0500242 (dma_offset < LPFC_BPL_SIZE)) {
243 dma_address = mp->virt + dma_offset;
244 if (sg_offset) {
245 /* Continue previous partial transfer of sg */
James Smartd5ce53b2013-04-17 20:17:26 -0400246 sg_address = miter.addr + sg_offset;
247 transfer_bytes = miter.length - sg_offset;
James Smarta33c4f72013-03-01 16:36:00 -0500248 } else {
James Smartd5ce53b2013-04-17 20:17:26 -0400249 sg_address = miter.addr;
250 transfer_bytes = miter.length;
James Smarta33c4f72013-03-01 16:36:00 -0500251 }
252 if (bytes_to_transfer < transfer_bytes)
253 transfer_bytes = bytes_to_transfer;
254 if (transfer_bytes > (LPFC_BPL_SIZE - dma_offset))
255 transfer_bytes = LPFC_BPL_SIZE - dma_offset;
256 if (to_buffers)
257 memcpy(dma_address, sg_address, transfer_bytes);
258 else
259 memcpy(sg_address, dma_address, transfer_bytes);
260 dma_offset += transfer_bytes;
261 sg_offset += transfer_bytes;
262 bytes_to_transfer -= transfer_bytes;
263 bytes_copied += transfer_bytes;
James Smartd5ce53b2013-04-17 20:17:26 -0400264 if (sg_offset >= miter.length) {
James Smarta33c4f72013-03-01 16:36:00 -0500265 sg_offset = 0;
James Smartd5ce53b2013-04-17 20:17:26 -0400266 sg_valid = sg_miter_next(&miter);
James Smarta33c4f72013-03-01 16:36:00 -0500267 }
268 }
269 }
James Smartd5ce53b2013-04-17 20:17:26 -0400270 sg_miter_stop(&miter);
271 local_irq_restore(flags);
James Smarta33c4f72013-03-01 16:36:00 -0500272 list_del_init(&dma_buffers->list);
273 list_splice(&temp_list, &dma_buffers->list);
274 return bytes_copied;
275}
276
James Smartf1c3b0f2009-07-19 10:01:32 -0400277/**
James Smart4cc0e562010-01-26 23:09:48 -0500278 * lpfc_bsg_send_mgmt_cmd_cmp - lpfc_bsg_send_mgmt_cmd's completion handler
279 * @phba: Pointer to HBA context object.
280 * @cmdiocbq: Pointer to command iocb.
281 * @rspiocbq: Pointer to response iocb.
282 *
283 * This function is the completion handler for iocbs issued using
284 * lpfc_bsg_send_mgmt_cmd function. This function is called by the
285 * ring event handler function without any lock held. This function
286 * can be called from both worker thread context and interrupt
287 * context. This function also can be called from another thread which
288 * cleans up the SLI layer objects.
289 * This function copies the contents of the response iocb to the
290 * response iocb memory object provided by the caller of
291 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
292 * sleeps for the iocb completion.
293 **/
294static void
295lpfc_bsg_send_mgmt_cmd_cmp(struct lpfc_hba *phba,
296 struct lpfc_iocbq *cmdiocbq,
297 struct lpfc_iocbq *rspiocbq)
298{
James Smart4cc0e562010-01-26 23:09:48 -0500299 struct bsg_job_data *dd_data;
300 struct fc_bsg_job *job;
301 IOCB_t *rsp;
James Smarta33c4f72013-03-01 16:36:00 -0500302 struct lpfc_dmabuf *bmp, *cmp, *rmp;
James Smart4cc0e562010-01-26 23:09:48 -0500303 struct lpfc_nodelist *ndlp;
304 struct lpfc_bsg_iocb *iocb;
305 unsigned long flags;
James Smarta33c4f72013-03-01 16:36:00 -0500306 unsigned int rsp_size;
James Smart4cc0e562010-01-26 23:09:48 -0500307 int rc = 0;
308
James Smarta33c4f72013-03-01 16:36:00 -0500309 dd_data = cmdiocbq->context1;
310
311 /* Determine if job has been aborted */
James Smart4cc0e562010-01-26 23:09:48 -0500312 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smarta33c4f72013-03-01 16:36:00 -0500313 job = dd_data->set_job;
314 if (job) {
315 /* Prevent timeout handling from trying to abort job */
316 job->dd_data = NULL;
James Smart4cc0e562010-01-26 23:09:48 -0500317 }
James Smarta33c4f72013-03-01 16:36:00 -0500318 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smart4cc0e562010-01-26 23:09:48 -0500319
James Smartb5a9b2d2013-09-06 12:19:45 -0400320 /* Close the timeout handler abort window */
321 spin_lock_irqsave(&phba->hbalock, flags);
James Smart1b8d11a2013-09-06 12:20:51 -0400322 cmdiocbq->iocb_flag &= ~LPFC_IO_CMD_OUTSTANDING;
James Smartb5a9b2d2013-09-06 12:19:45 -0400323 spin_unlock_irqrestore(&phba->hbalock, flags);
324
James Smart4cc0e562010-01-26 23:09:48 -0500325 iocb = &dd_data->context_un.iocb;
James Smarta33c4f72013-03-01 16:36:00 -0500326 ndlp = iocb->ndlp;
327 rmp = iocb->rmp;
328 cmp = cmdiocbq->context2;
329 bmp = cmdiocbq->context3;
James Smart4cc0e562010-01-26 23:09:48 -0500330 rsp = &rspiocbq->iocb;
James Smart4cc0e562010-01-26 23:09:48 -0500331
James Smarta33c4f72013-03-01 16:36:00 -0500332 /* Copy the completed data or set the error status */
James Smart4cc0e562010-01-26 23:09:48 -0500333
James Smarta33c4f72013-03-01 16:36:00 -0500334 if (job) {
335 if (rsp->ulpStatus) {
336 if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
337 switch (rsp->un.ulpWord[4] & IOERR_PARAM_MASK) {
338 case IOERR_SEQUENCE_TIMEOUT:
339 rc = -ETIMEDOUT;
340 break;
341 case IOERR_INVALID_RPI:
342 rc = -EFAULT;
343 break;
344 default:
345 rc = -EACCES;
346 break;
347 }
348 } else {
James Smart4cc0e562010-01-26 23:09:48 -0500349 rc = -EACCES;
James Smart4cc0e562010-01-26 23:09:48 -0500350 }
James Smarta33c4f72013-03-01 16:36:00 -0500351 } else {
352 rsp_size = rsp->un.genreq64.bdl.bdeSize;
353 job->reply->reply_payload_rcv_len =
354 lpfc_bsg_copy_data(rmp, &job->reply_payload,
355 rsp_size, 0);
356 }
357 }
James Smart4cc0e562010-01-26 23:09:48 -0500358
James Smarta33c4f72013-03-01 16:36:00 -0500359 lpfc_free_bsg_buffers(phba, cmp);
360 lpfc_free_bsg_buffers(phba, rmp);
James Smart4cc0e562010-01-26 23:09:48 -0500361 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
James Smarta33c4f72013-03-01 16:36:00 -0500362 kfree(bmp);
James Smart4cc0e562010-01-26 23:09:48 -0500363 lpfc_sli_release_iocbq(phba, cmdiocbq);
364 lpfc_nlp_put(ndlp);
James Smart4cc0e562010-01-26 23:09:48 -0500365 kfree(dd_data);
James Smarta33c4f72013-03-01 16:36:00 -0500366
367 /* Complete the job if the job is still active */
368
369 if (job) {
370 job->reply->result = rc;
371 job->job_done(job);
372 }
James Smart4cc0e562010-01-26 23:09:48 -0500373 return;
374}
375
376/**
377 * lpfc_bsg_send_mgmt_cmd - send a CT command from a bsg request
James Smartf1c3b0f2009-07-19 10:01:32 -0400378 * @job: fc_bsg_job to handle
James Smart3b5dd522010-01-26 23:10:15 -0500379 **/
James Smartf1c3b0f2009-07-19 10:01:32 -0400380static int
James Smart4cc0e562010-01-26 23:09:48 -0500381lpfc_bsg_send_mgmt_cmd(struct fc_bsg_job *job)
James Smartf1c3b0f2009-07-19 10:01:32 -0400382{
James Smartf1c3b0f2009-07-19 10:01:32 -0400383 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
384 struct lpfc_hba *phba = vport->phba;
385 struct lpfc_rport_data *rdata = job->rport->dd_data;
386 struct lpfc_nodelist *ndlp = rdata->pnode;
387 struct ulp_bde64 *bpl = NULL;
388 uint32_t timeout;
389 struct lpfc_iocbq *cmdiocbq = NULL;
James Smartf1c3b0f2009-07-19 10:01:32 -0400390 IOCB_t *cmd;
James Smarta33c4f72013-03-01 16:36:00 -0500391 struct lpfc_dmabuf *bmp = NULL, *cmp = NULL, *rmp = NULL;
James Smartf1c3b0f2009-07-19 10:01:32 -0400392 int request_nseg;
393 int reply_nseg;
James Smart4cc0e562010-01-26 23:09:48 -0500394 struct bsg_job_data *dd_data;
James Smartb5a9b2d2013-09-06 12:19:45 -0400395 unsigned long flags;
James Smart4cc0e562010-01-26 23:09:48 -0500396 uint32_t creg_val;
James Smartf1c3b0f2009-07-19 10:01:32 -0400397 int rc = 0;
James Smartd439d282010-09-29 11:18:45 -0400398 int iocb_stat;
James Smartf1c3b0f2009-07-19 10:01:32 -0400399
400 /* in case no data is transferred */
401 job->reply->reply_payload_rcv_len = 0;
402
James Smart4cc0e562010-01-26 23:09:48 -0500403 /* allocate our bsg tracking structure */
404 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
405 if (!dd_data) {
406 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
407 "2733 Failed allocation of dd_data\n");
408 rc = -ENOMEM;
409 goto no_dd_data;
410 }
411
James Smartf1c3b0f2009-07-19 10:01:32 -0400412 if (!lpfc_nlp_get(ndlp)) {
James Smart4cc0e562010-01-26 23:09:48 -0500413 rc = -ENODEV;
414 goto no_ndlp;
415 }
416
James Smartf1c3b0f2009-07-19 10:01:32 -0400417 if (ndlp->nlp_flag & NLP_ELS_SND_MASK) {
418 rc = -ENODEV;
James Smarta33c4f72013-03-01 16:36:00 -0500419 goto free_ndlp;
James Smartf1c3b0f2009-07-19 10:01:32 -0400420 }
421
James Smartf1c3b0f2009-07-19 10:01:32 -0400422 cmdiocbq = lpfc_sli_get_iocbq(phba);
423 if (!cmdiocbq) {
424 rc = -ENOMEM;
James Smarta33c4f72013-03-01 16:36:00 -0500425 goto free_ndlp;
James Smartf1c3b0f2009-07-19 10:01:32 -0400426 }
James Smartf1c3b0f2009-07-19 10:01:32 -0400427
James Smart4cc0e562010-01-26 23:09:48 -0500428 cmd = &cmdiocbq->iocb;
James Smarta33c4f72013-03-01 16:36:00 -0500429
430 bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
431 if (!bmp) {
James Smartf1c3b0f2009-07-19 10:01:32 -0400432 rc = -ENOMEM;
James Smartbe858b62010-12-15 17:57:20 -0500433 goto free_cmdiocbq;
James Smartf1c3b0f2009-07-19 10:01:32 -0400434 }
James Smarta33c4f72013-03-01 16:36:00 -0500435 bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
436 if (!bmp->virt) {
437 rc = -ENOMEM;
438 goto free_bmp;
James Smartf1c3b0f2009-07-19 10:01:32 -0400439 }
440
James Smarta33c4f72013-03-01 16:36:00 -0500441 INIT_LIST_HEAD(&bmp->list);
442
443 bpl = (struct ulp_bde64 *) bmp->virt;
444 request_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64);
445 cmp = lpfc_alloc_bsg_buffers(phba, job->request_payload.payload_len,
446 1, bpl, &request_nseg);
447 if (!cmp) {
448 rc = -ENOMEM;
449 goto free_bmp;
450 }
451 lpfc_bsg_copy_data(cmp, &job->request_payload,
452 job->request_payload.payload_len, 1);
453
454 bpl += request_nseg;
455 reply_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64) - request_nseg;
456 rmp = lpfc_alloc_bsg_buffers(phba, job->reply_payload.payload_len, 0,
457 bpl, &reply_nseg);
458 if (!rmp) {
459 rc = -ENOMEM;
460 goto free_cmp;
James Smartf1c3b0f2009-07-19 10:01:32 -0400461 }
462
463 cmd->un.genreq64.bdl.ulpIoTag32 = 0;
464 cmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
465 cmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
466 cmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
467 cmd->un.genreq64.bdl.bdeSize =
468 (request_nseg + reply_nseg) * sizeof(struct ulp_bde64);
469 cmd->ulpCommand = CMD_GEN_REQUEST64_CR;
470 cmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
471 cmd->un.genreq64.w5.hcsw.Dfctl = 0;
James Smart6a9c52c2009-10-02 15:16:51 -0400472 cmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
473 cmd->un.genreq64.w5.hcsw.Type = FC_TYPE_CT;
James Smartf1c3b0f2009-07-19 10:01:32 -0400474 cmd->ulpBdeCount = 1;
475 cmd->ulpLe = 1;
476 cmd->ulpClass = CLASS3;
477 cmd->ulpContext = ndlp->nlp_rpi;
James Smart6d368e52011-05-24 11:44:12 -0400478 if (phba->sli_rev == LPFC_SLI_REV4)
479 cmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smartf1c3b0f2009-07-19 10:01:32 -0400480 cmd->ulpOwner = OWN_CHIP;
481 cmdiocbq->vport = phba->pport;
James Smart4cc0e562010-01-26 23:09:48 -0500482 cmdiocbq->context3 = bmp;
James Smartf1c3b0f2009-07-19 10:01:32 -0400483 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
James Smartf1c3b0f2009-07-19 10:01:32 -0400484 timeout = phba->fc_ratov * 2;
James Smart4cc0e562010-01-26 23:09:48 -0500485 cmd->ulpTimeout = timeout;
James Smartf1c3b0f2009-07-19 10:01:32 -0400486
James Smart4cc0e562010-01-26 23:09:48 -0500487 cmdiocbq->iocb_cmpl = lpfc_bsg_send_mgmt_cmd_cmp;
James Smarta33c4f72013-03-01 16:36:00 -0500488 cmdiocbq->context1 = dd_data;
489 cmdiocbq->context2 = cmp;
490 cmdiocbq->context3 = bmp;
James Smartd5ce53b2013-04-17 20:17:26 -0400491 cmdiocbq->context_un.ndlp = ndlp;
James Smart4cc0e562010-01-26 23:09:48 -0500492 dd_data->type = TYPE_IOCB;
James Smarta33c4f72013-03-01 16:36:00 -0500493 dd_data->set_job = job;
James Smart4cc0e562010-01-26 23:09:48 -0500494 dd_data->context_un.iocb.cmdiocbq = cmdiocbq;
James Smarta33c4f72013-03-01 16:36:00 -0500495 dd_data->context_un.iocb.ndlp = ndlp;
496 dd_data->context_un.iocb.rmp = rmp;
497 job->dd_data = dd_data;
James Smartf1c3b0f2009-07-19 10:01:32 -0400498
James Smart4cc0e562010-01-26 23:09:48 -0500499 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -0500500 if (lpfc_readl(phba->HCregaddr, &creg_val)) {
501 rc = -EIO ;
James Smarta33c4f72013-03-01 16:36:00 -0500502 goto free_rmp;
James Smart9940b972011-03-11 16:06:12 -0500503 }
James Smart4cc0e562010-01-26 23:09:48 -0500504 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
505 writel(creg_val, phba->HCregaddr);
506 readl(phba->HCregaddr); /* flush */
James Smartf1c3b0f2009-07-19 10:01:32 -0400507 }
508
James Smartd439d282010-09-29 11:18:45 -0400509 iocb_stat = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq, 0);
James Smartb5a9b2d2013-09-06 12:19:45 -0400510
511 if (iocb_stat == IOCB_SUCCESS) {
512 spin_lock_irqsave(&phba->hbalock, flags);
513 /* make sure the I/O had not been completed yet */
514 if (cmdiocbq->iocb_flag & LPFC_IO_LIBDFC) {
515 /* open up abort window to timeout handler */
James Smart1b8d11a2013-09-06 12:20:51 -0400516 cmdiocbq->iocb_flag |= LPFC_IO_CMD_OUTSTANDING;
James Smartb5a9b2d2013-09-06 12:19:45 -0400517 }
518 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4cc0e562010-01-26 23:09:48 -0500519 return 0; /* done for now */
James Smartb5a9b2d2013-09-06 12:19:45 -0400520 } else if (iocb_stat == IOCB_BUSY) {
James Smartd439d282010-09-29 11:18:45 -0400521 rc = -EAGAIN;
James Smartb5a9b2d2013-09-06 12:19:45 -0400522 } else {
James Smartd439d282010-09-29 11:18:45 -0400523 rc = -EIO;
James Smartb5a9b2d2013-09-06 12:19:45 -0400524 }
James Smart2a9bf3d2010-06-07 15:24:45 -0400525
James Smart4cc0e562010-01-26 23:09:48 -0500526 /* iocb failed so cleanup */
James Smartb5a9b2d2013-09-06 12:19:45 -0400527 job->dd_data = NULL;
James Smartf1c3b0f2009-07-19 10:01:32 -0400528
James Smarta33c4f72013-03-01 16:36:00 -0500529free_rmp:
530 lpfc_free_bsg_buffers(phba, rmp);
531free_cmp:
532 lpfc_free_bsg_buffers(phba, cmp);
533free_bmp:
534 if (bmp->virt)
535 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
536 kfree(bmp);
James Smartf1c3b0f2009-07-19 10:01:32 -0400537free_cmdiocbq:
538 lpfc_sli_release_iocbq(phba, cmdiocbq);
James Smart4cc0e562010-01-26 23:09:48 -0500539free_ndlp:
James Smartf1c3b0f2009-07-19 10:01:32 -0400540 lpfc_nlp_put(ndlp);
James Smart4cc0e562010-01-26 23:09:48 -0500541no_ndlp:
542 kfree(dd_data);
543no_dd_data:
James Smartf1c3b0f2009-07-19 10:01:32 -0400544 /* make error code available to userspace */
545 job->reply->result = rc;
James Smart4cc0e562010-01-26 23:09:48 -0500546 job->dd_data = NULL;
547 return rc;
548}
549
550/**
551 * lpfc_bsg_rport_els_cmp - lpfc_bsg_rport_els's completion handler
552 * @phba: Pointer to HBA context object.
553 * @cmdiocbq: Pointer to command iocb.
554 * @rspiocbq: Pointer to response iocb.
555 *
556 * This function is the completion handler for iocbs issued using
557 * lpfc_bsg_rport_els_cmp function. This function is called by the
558 * ring event handler function without any lock held. This function
559 * can be called from both worker thread context and interrupt
560 * context. This function also can be called from other thread which
561 * cleans up the SLI layer objects.
James Smart3b5dd522010-01-26 23:10:15 -0500562 * This function copies the contents of the response iocb to the
James Smart4cc0e562010-01-26 23:09:48 -0500563 * response iocb memory object provided by the caller of
564 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
565 * sleeps for the iocb completion.
566 **/
567static void
568lpfc_bsg_rport_els_cmp(struct lpfc_hba *phba,
569 struct lpfc_iocbq *cmdiocbq,
570 struct lpfc_iocbq *rspiocbq)
571{
572 struct bsg_job_data *dd_data;
573 struct fc_bsg_job *job;
574 IOCB_t *rsp;
575 struct lpfc_nodelist *ndlp;
James Smarta33c4f72013-03-01 16:36:00 -0500576 struct lpfc_dmabuf *pcmd = NULL, *prsp = NULL;
James Smart4cc0e562010-01-26 23:09:48 -0500577 struct fc_bsg_ctels_reply *els_reply;
578 uint8_t *rjt_data;
579 unsigned long flags;
James Smarta33c4f72013-03-01 16:36:00 -0500580 unsigned int rsp_size;
James Smart4cc0e562010-01-26 23:09:48 -0500581 int rc = 0;
582
James Smart4cc0e562010-01-26 23:09:48 -0500583 dd_data = cmdiocbq->context1;
James Smarta33c4f72013-03-01 16:36:00 -0500584 ndlp = dd_data->context_un.iocb.ndlp;
585 cmdiocbq->context1 = ndlp;
586
587 /* Determine if job has been aborted */
588 spin_lock_irqsave(&phba->ct_ev_lock, flags);
589 job = dd_data->set_job;
590 if (job) {
591 /* Prevent timeout handling from trying to abort job */
592 job->dd_data = NULL;
593 }
594 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
595
James Smartb5a9b2d2013-09-06 12:19:45 -0400596 /* Close the timeout handler abort window */
597 spin_lock_irqsave(&phba->hbalock, flags);
James Smart1b8d11a2013-09-06 12:20:51 -0400598 cmdiocbq->iocb_flag &= ~LPFC_IO_CMD_OUTSTANDING;
James Smartb5a9b2d2013-09-06 12:19:45 -0400599 spin_unlock_irqrestore(&phba->hbalock, flags);
600
James Smarta33c4f72013-03-01 16:36:00 -0500601 rsp = &rspiocbq->iocb;
602 pcmd = (struct lpfc_dmabuf *)cmdiocbq->context2;
603 prsp = (struct lpfc_dmabuf *)pcmd->list.next;
604
605 /* Copy the completed job data or determine the job status if job is
606 * still active
607 */
608
609 if (job) {
610 if (rsp->ulpStatus == IOSTAT_SUCCESS) {
611 rsp_size = rsp->un.elsreq64.bdl.bdeSize;
612 job->reply->reply_payload_rcv_len =
613 sg_copy_from_buffer(job->reply_payload.sg_list,
614 job->reply_payload.sg_cnt,
615 prsp->virt,
616 rsp_size);
617 } else if (rsp->ulpStatus == IOSTAT_LS_RJT) {
618 job->reply->reply_payload_rcv_len =
619 sizeof(struct fc_bsg_ctels_reply);
620 /* LS_RJT data returned in word 4 */
621 rjt_data = (uint8_t *)&rsp->un.ulpWord[4];
622 els_reply = &job->reply->reply_data.ctels_reply;
623 els_reply->status = FC_CTELS_STATUS_REJECT;
624 els_reply->rjt_data.action = rjt_data[3];
625 els_reply->rjt_data.reason_code = rjt_data[2];
626 els_reply->rjt_data.reason_explanation = rjt_data[1];
627 els_reply->rjt_data.vendor_unique = rjt_data[0];
628 } else {
629 rc = -EIO;
630 }
James Smart4cc0e562010-01-26 23:09:48 -0500631 }
632
James Smart4cc0e562010-01-26 23:09:48 -0500633 lpfc_nlp_put(ndlp);
James Smarta33c4f72013-03-01 16:36:00 -0500634 lpfc_els_free_iocb(phba, cmdiocbq);
James Smart4cc0e562010-01-26 23:09:48 -0500635 kfree(dd_data);
James Smarta33c4f72013-03-01 16:36:00 -0500636
637 /* Complete the job if the job is still active */
638
639 if (job) {
640 job->reply->result = rc;
641 job->job_done(job);
642 }
James Smart4cc0e562010-01-26 23:09:48 -0500643 return;
James Smartf1c3b0f2009-07-19 10:01:32 -0400644}
645
646/**
647 * lpfc_bsg_rport_els - send an ELS command from a bsg request
648 * @job: fc_bsg_job to handle
James Smart3b5dd522010-01-26 23:10:15 -0500649 **/
James Smartf1c3b0f2009-07-19 10:01:32 -0400650static int
651lpfc_bsg_rport_els(struct fc_bsg_job *job)
652{
653 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
654 struct lpfc_hba *phba = vport->phba;
655 struct lpfc_rport_data *rdata = job->rport->dd_data;
656 struct lpfc_nodelist *ndlp = rdata->pnode;
James Smartf1c3b0f2009-07-19 10:01:32 -0400657 uint32_t elscmd;
658 uint32_t cmdsize;
659 uint32_t rspsize;
James Smartf1c3b0f2009-07-19 10:01:32 -0400660 struct lpfc_iocbq *cmdiocbq;
James Smartf1c3b0f2009-07-19 10:01:32 -0400661 uint16_t rpi = 0;
James Smart4cc0e562010-01-26 23:09:48 -0500662 struct bsg_job_data *dd_data;
James Smartb5a9b2d2013-09-06 12:19:45 -0400663 unsigned long flags;
James Smart4cc0e562010-01-26 23:09:48 -0500664 uint32_t creg_val;
James Smartf1c3b0f2009-07-19 10:01:32 -0400665 int rc = 0;
666
667 /* in case no data is transferred */
668 job->reply->reply_payload_rcv_len = 0;
669
James Smarta33c4f72013-03-01 16:36:00 -0500670 /* verify the els command is not greater than the
671 * maximum ELS transfer size.
672 */
673
674 if (job->request_payload.payload_len > FCELSSIZE) {
675 rc = -EINVAL;
676 goto no_dd_data;
677 }
678
James Smart4cc0e562010-01-26 23:09:48 -0500679 /* allocate our bsg tracking structure */
680 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
681 if (!dd_data) {
682 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
683 "2735 Failed allocation of dd_data\n");
684 rc = -ENOMEM;
685 goto no_dd_data;
686 }
687
James Smarta33c4f72013-03-01 16:36:00 -0500688 elscmd = job->request->rqst_data.r_els.els_code;
689 cmdsize = job->request_payload.payload_len;
690 rspsize = job->reply_payload.payload_len;
691
James Smartf1c3b0f2009-07-19 10:01:32 -0400692 if (!lpfc_nlp_get(ndlp)) {
693 rc = -ENODEV;
James Smart4cc0e562010-01-26 23:09:48 -0500694 goto free_dd_data;
James Smartf1c3b0f2009-07-19 10:01:32 -0400695 }
696
James Smarta33c4f72013-03-01 16:36:00 -0500697 /* We will use the allocated dma buffers by prep els iocb for command
698 * and response to ensure if the job times out and the request is freed,
699 * we won't be dma into memory that is no longer allocated to for the
700 * request.
701 */
James Smartf1c3b0f2009-07-19 10:01:32 -0400702
James Smart4cc0e562010-01-26 23:09:48 -0500703 cmdiocbq = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp,
James Smartf1c3b0f2009-07-19 10:01:32 -0400704 ndlp->nlp_DID, elscmd);
James Smartf1c3b0f2009-07-19 10:01:32 -0400705 if (!cmdiocbq) {
James Smart4cc0e562010-01-26 23:09:48 -0500706 rc = -EIO;
James Smarta33c4f72013-03-01 16:36:00 -0500707 goto release_ndlp;
James Smartf1c3b0f2009-07-19 10:01:32 -0400708 }
709
James Smarta33c4f72013-03-01 16:36:00 -0500710 rpi = ndlp->nlp_rpi;
James Smartf1c3b0f2009-07-19 10:01:32 -0400711
James Smarta33c4f72013-03-01 16:36:00 -0500712 /* Transfer the request payload to allocated command dma buffer */
James Smartf1c3b0f2009-07-19 10:01:32 -0400713
James Smarta33c4f72013-03-01 16:36:00 -0500714 sg_copy_to_buffer(job->request_payload.sg_list,
715 job->request_payload.sg_cnt,
716 ((struct lpfc_dmabuf *)cmdiocbq->context2)->virt,
717 cmdsize);
James Smartf1c3b0f2009-07-19 10:01:32 -0400718
James Smart3ef6d242012-01-18 16:23:48 -0500719 if (phba->sli_rev == LPFC_SLI_REV4)
720 cmdiocbq->iocb.ulpContext = phba->sli4_hba.rpi_ids[rpi];
721 else
722 cmdiocbq->iocb.ulpContext = rpi;
James Smartf1c3b0f2009-07-19 10:01:32 -0400723 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
James Smart4cc0e562010-01-26 23:09:48 -0500724 cmdiocbq->context1 = dd_data;
James Smart93d1379e2012-05-09 21:19:34 -0400725 cmdiocbq->context_un.ndlp = ndlp;
James Smarta33c4f72013-03-01 16:36:00 -0500726 cmdiocbq->iocb_cmpl = lpfc_bsg_rport_els_cmp;
James Smart4cc0e562010-01-26 23:09:48 -0500727 dd_data->type = TYPE_IOCB;
James Smarta33c4f72013-03-01 16:36:00 -0500728 dd_data->set_job = job;
James Smart4cc0e562010-01-26 23:09:48 -0500729 dd_data->context_un.iocb.cmdiocbq = cmdiocbq;
James Smart4cc0e562010-01-26 23:09:48 -0500730 dd_data->context_un.iocb.ndlp = ndlp;
James Smarta33c4f72013-03-01 16:36:00 -0500731 dd_data->context_un.iocb.rmp = NULL;
732 job->dd_data = dd_data;
James Smartf1c3b0f2009-07-19 10:01:32 -0400733
James Smart4cc0e562010-01-26 23:09:48 -0500734 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -0500735 if (lpfc_readl(phba->HCregaddr, &creg_val)) {
736 rc = -EIO;
737 goto linkdown_err;
738 }
James Smart4cc0e562010-01-26 23:09:48 -0500739 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
740 writel(creg_val, phba->HCregaddr);
741 readl(phba->HCregaddr); /* flush */
James Smartf1c3b0f2009-07-19 10:01:32 -0400742 }
James Smarta33c4f72013-03-01 16:36:00 -0500743
James Smart4cc0e562010-01-26 23:09:48 -0500744 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq, 0);
James Smarta33c4f72013-03-01 16:36:00 -0500745
James Smartb5a9b2d2013-09-06 12:19:45 -0400746 if (rc == IOCB_SUCCESS) {
747 spin_lock_irqsave(&phba->hbalock, flags);
748 /* make sure the I/O had not been completed/released */
749 if (cmdiocbq->iocb_flag & LPFC_IO_LIBDFC) {
750 /* open up abort window to timeout handler */
James Smart1b8d11a2013-09-06 12:20:51 -0400751 cmdiocbq->iocb_flag |= LPFC_IO_CMD_OUTSTANDING;
James Smartb5a9b2d2013-09-06 12:19:45 -0400752 }
753 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4cc0e562010-01-26 23:09:48 -0500754 return 0; /* done for now */
James Smartb5a9b2d2013-09-06 12:19:45 -0400755 } else if (rc == IOCB_BUSY) {
James Smartd439d282010-09-29 11:18:45 -0400756 rc = -EAGAIN;
James Smartb5a9b2d2013-09-06 12:19:45 -0400757 } else {
James Smartd439d282010-09-29 11:18:45 -0400758 rc = -EIO;
James Smartb5a9b2d2013-09-06 12:19:45 -0400759 }
760
761 /* iocb failed so cleanup */
762 job->dd_data = NULL;
James Smartf1c3b0f2009-07-19 10:01:32 -0400763
James Smart9940b972011-03-11 16:06:12 -0500764linkdown_err:
James Smarta33c4f72013-03-01 16:36:00 -0500765 cmdiocbq->context1 = ndlp;
766 lpfc_els_free_iocb(phba, cmdiocbq);
James Smartf1c3b0f2009-07-19 10:01:32 -0400767
James Smarta33c4f72013-03-01 16:36:00 -0500768release_ndlp:
769 lpfc_nlp_put(ndlp);
James Smartf1c3b0f2009-07-19 10:01:32 -0400770
James Smart4cc0e562010-01-26 23:09:48 -0500771free_dd_data:
772 kfree(dd_data);
773
774no_dd_data:
James Smartf1c3b0f2009-07-19 10:01:32 -0400775 /* make error code available to userspace */
776 job->reply->result = rc;
James Smart4cc0e562010-01-26 23:09:48 -0500777 job->dd_data = NULL;
778 return rc;
James Smartf1c3b0f2009-07-19 10:01:32 -0400779}
780
James Smart3b5dd522010-01-26 23:10:15 -0500781/**
782 * lpfc_bsg_event_free - frees an allocated event structure
783 * @kref: Pointer to a kref.
784 *
785 * Called from kref_put. Back cast the kref into an event structure address.
786 * Free any events to get, delete associated nodes, free any events to see,
787 * free any data then free the event itself.
788 **/
James Smartf1c3b0f2009-07-19 10:01:32 -0400789static void
James Smart4cc0e562010-01-26 23:09:48 -0500790lpfc_bsg_event_free(struct kref *kref)
James Smartf1c3b0f2009-07-19 10:01:32 -0400791{
James Smart4cc0e562010-01-26 23:09:48 -0500792 struct lpfc_bsg_event *evt = container_of(kref, struct lpfc_bsg_event,
793 kref);
James Smartf1c3b0f2009-07-19 10:01:32 -0400794 struct event_data *ed;
795
796 list_del(&evt->node);
797
798 while (!list_empty(&evt->events_to_get)) {
799 ed = list_entry(evt->events_to_get.next, typeof(*ed), node);
800 list_del(&ed->node);
801 kfree(ed->data);
802 kfree(ed);
803 }
804
805 while (!list_empty(&evt->events_to_see)) {
806 ed = list_entry(evt->events_to_see.next, typeof(*ed), node);
807 list_del(&ed->node);
808 kfree(ed->data);
809 kfree(ed);
810 }
811
James Smarta33c4f72013-03-01 16:36:00 -0500812 kfree(evt->dd_data);
James Smartf1c3b0f2009-07-19 10:01:32 -0400813 kfree(evt);
814}
815
James Smart3b5dd522010-01-26 23:10:15 -0500816/**
817 * lpfc_bsg_event_ref - increments the kref for an event
818 * @evt: Pointer to an event structure.
819 **/
James Smartf1c3b0f2009-07-19 10:01:32 -0400820static inline void
James Smart4cc0e562010-01-26 23:09:48 -0500821lpfc_bsg_event_ref(struct lpfc_bsg_event *evt)
James Smartf1c3b0f2009-07-19 10:01:32 -0400822{
James Smart4cc0e562010-01-26 23:09:48 -0500823 kref_get(&evt->kref);
James Smartf1c3b0f2009-07-19 10:01:32 -0400824}
825
James Smart3b5dd522010-01-26 23:10:15 -0500826/**
827 * lpfc_bsg_event_unref - Uses kref_put to free an event structure
828 * @evt: Pointer to an event structure.
829 **/
James Smartf1c3b0f2009-07-19 10:01:32 -0400830static inline void
James Smart4cc0e562010-01-26 23:09:48 -0500831lpfc_bsg_event_unref(struct lpfc_bsg_event *evt)
James Smartf1c3b0f2009-07-19 10:01:32 -0400832{
James Smart4cc0e562010-01-26 23:09:48 -0500833 kref_put(&evt->kref, lpfc_bsg_event_free);
James Smartf1c3b0f2009-07-19 10:01:32 -0400834}
835
James Smart3b5dd522010-01-26 23:10:15 -0500836/**
837 * lpfc_bsg_event_new - allocate and initialize a event structure
838 * @ev_mask: Mask of events.
839 * @ev_reg_id: Event reg id.
840 * @ev_req_id: Event request id.
841 **/
James Smart4cc0e562010-01-26 23:09:48 -0500842static struct lpfc_bsg_event *
843lpfc_bsg_event_new(uint32_t ev_mask, int ev_reg_id, uint32_t ev_req_id)
844{
845 struct lpfc_bsg_event *evt = kzalloc(sizeof(*evt), GFP_KERNEL);
James Smartf1c3b0f2009-07-19 10:01:32 -0400846
James Smart4cc0e562010-01-26 23:09:48 -0500847 if (!evt)
848 return NULL;
849
850 INIT_LIST_HEAD(&evt->events_to_get);
851 INIT_LIST_HEAD(&evt->events_to_see);
852 evt->type_mask = ev_mask;
853 evt->req_id = ev_req_id;
854 evt->reg_id = ev_reg_id;
855 evt->wait_time_stamp = jiffies;
James Smarta33c4f72013-03-01 16:36:00 -0500856 evt->dd_data = NULL;
James Smart4cc0e562010-01-26 23:09:48 -0500857 init_waitqueue_head(&evt->wq);
858 kref_init(&evt->kref);
859 return evt;
860}
861
James Smart3b5dd522010-01-26 23:10:15 -0500862/**
863 * diag_cmd_data_free - Frees an lpfc dma buffer extension
864 * @phba: Pointer to HBA context object.
865 * @mlist: Pointer to an lpfc dma buffer extension.
866 **/
James Smart4cc0e562010-01-26 23:09:48 -0500867static int
James Smart3b5dd522010-01-26 23:10:15 -0500868diag_cmd_data_free(struct lpfc_hba *phba, struct lpfc_dmabufext *mlist)
James Smart4cc0e562010-01-26 23:09:48 -0500869{
870 struct lpfc_dmabufext *mlast;
871 struct pci_dev *pcidev;
872 struct list_head head, *curr, *next;
873
874 if ((!mlist) || (!lpfc_is_link_up(phba) &&
875 (phba->link_flag & LS_LOOPBACK_MODE))) {
876 return 0;
877 }
878
879 pcidev = phba->pcidev;
880 list_add_tail(&head, &mlist->dma.list);
881
882 list_for_each_safe(curr, next, &head) {
883 mlast = list_entry(curr, struct lpfc_dmabufext , dma.list);
884 if (mlast->dma.virt)
885 dma_free_coherent(&pcidev->dev,
886 mlast->size,
887 mlast->dma.virt,
888 mlast->dma.phys);
889 kfree(mlast);
890 }
891 return 0;
892}
James Smartf1c3b0f2009-07-19 10:01:32 -0400893
894/**
895 * lpfc_bsg_ct_unsol_event - process an unsolicited CT command
896 * @phba:
897 * @pring:
898 * @piocbq:
899 *
900 * This function is called when an unsolicited CT command is received. It
James Smart4cc0e562010-01-26 23:09:48 -0500901 * forwards the event to any processes registered to receive CT events.
James Smart3b5dd522010-01-26 23:10:15 -0500902 **/
James Smart4fede782010-01-26 23:08:55 -0500903int
James Smartf1c3b0f2009-07-19 10:01:32 -0400904lpfc_bsg_ct_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
905 struct lpfc_iocbq *piocbq)
906{
907 uint32_t evt_req_id = 0;
908 uint32_t cmd;
909 uint32_t len;
910 struct lpfc_dmabuf *dmabuf = NULL;
James Smart4cc0e562010-01-26 23:09:48 -0500911 struct lpfc_bsg_event *evt;
James Smartf1c3b0f2009-07-19 10:01:32 -0400912 struct event_data *evt_dat = NULL;
913 struct lpfc_iocbq *iocbq;
914 size_t offset = 0;
915 struct list_head head;
916 struct ulp_bde64 *bde;
917 dma_addr_t dma_addr;
918 int i;
919 struct lpfc_dmabuf *bdeBuf1 = piocbq->context2;
920 struct lpfc_dmabuf *bdeBuf2 = piocbq->context3;
921 struct lpfc_hbq_entry *hbqe;
922 struct lpfc_sli_ct_request *ct_req;
James Smart4cc0e562010-01-26 23:09:48 -0500923 struct fc_bsg_job *job = NULL;
James Smarta33c4f72013-03-01 16:36:00 -0500924 struct bsg_job_data *dd_data = NULL;
James Smart4fede782010-01-26 23:08:55 -0500925 unsigned long flags;
James Smart4cc0e562010-01-26 23:09:48 -0500926 int size = 0;
James Smartf1c3b0f2009-07-19 10:01:32 -0400927
928 INIT_LIST_HEAD(&head);
929 list_add_tail(&head, &piocbq->list);
930
931 if (piocbq->iocb.ulpBdeCount == 0 ||
932 piocbq->iocb.un.cont64[0].tus.f.bdeSize == 0)
933 goto error_ct_unsol_exit;
934
James Smart4cc0e562010-01-26 23:09:48 -0500935 if (phba->link_state == LPFC_HBA_ERROR ||
936 (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)))
937 goto error_ct_unsol_exit;
938
James Smartf1c3b0f2009-07-19 10:01:32 -0400939 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
940 dmabuf = bdeBuf1;
941 else {
942 dma_addr = getPaddr(piocbq->iocb.un.cont64[0].addrHigh,
943 piocbq->iocb.un.cont64[0].addrLow);
944 dmabuf = lpfc_sli_ringpostbuf_get(phba, pring, dma_addr);
945 }
James Smart4cc0e562010-01-26 23:09:48 -0500946 if (dmabuf == NULL)
947 goto error_ct_unsol_exit;
James Smartf1c3b0f2009-07-19 10:01:32 -0400948 ct_req = (struct lpfc_sli_ct_request *)dmabuf->virt;
949 evt_req_id = ct_req->FsType;
950 cmd = ct_req->CommandResponse.bits.CmdRsp;
951 len = ct_req->CommandResponse.bits.Size;
952 if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
953 lpfc_sli_ringpostbuf_put(phba, pring, dmabuf);
954
James Smart4fede782010-01-26 23:08:55 -0500955 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -0400956 list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
James Smart4cc0e562010-01-26 23:09:48 -0500957 if (!(evt->type_mask & FC_REG_CT_EVENT) ||
958 evt->req_id != evt_req_id)
James Smartf1c3b0f2009-07-19 10:01:32 -0400959 continue;
960
James Smart4cc0e562010-01-26 23:09:48 -0500961 lpfc_bsg_event_ref(evt);
962 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -0400963 evt_dat = kzalloc(sizeof(*evt_dat), GFP_KERNEL);
James Smart4cc0e562010-01-26 23:09:48 -0500964 if (evt_dat == NULL) {
965 spin_lock_irqsave(&phba->ct_ev_lock, flags);
966 lpfc_bsg_event_unref(evt);
James Smartf1c3b0f2009-07-19 10:01:32 -0400967 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
968 "2614 Memory allocation failed for "
969 "CT event\n");
970 break;
971 }
972
James Smartf1c3b0f2009-07-19 10:01:32 -0400973 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
974 /* take accumulated byte count from the last iocbq */
975 iocbq = list_entry(head.prev, typeof(*iocbq), list);
976 evt_dat->len = iocbq->iocb.unsli3.rcvsli3.acc_len;
977 } else {
978 list_for_each_entry(iocbq, &head, list) {
979 for (i = 0; i < iocbq->iocb.ulpBdeCount; i++)
980 evt_dat->len +=
981 iocbq->iocb.un.cont64[i].tus.f.bdeSize;
982 }
983 }
984
985 evt_dat->data = kzalloc(evt_dat->len, GFP_KERNEL);
James Smart4cc0e562010-01-26 23:09:48 -0500986 if (evt_dat->data == NULL) {
James Smartf1c3b0f2009-07-19 10:01:32 -0400987 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
988 "2615 Memory allocation failed for "
989 "CT event data, size %d\n",
990 evt_dat->len);
991 kfree(evt_dat);
James Smart4fede782010-01-26 23:08:55 -0500992 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smart4cc0e562010-01-26 23:09:48 -0500993 lpfc_bsg_event_unref(evt);
James Smart4fede782010-01-26 23:08:55 -0500994 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -0400995 goto error_ct_unsol_exit;
996 }
997
998 list_for_each_entry(iocbq, &head, list) {
James Smart4cc0e562010-01-26 23:09:48 -0500999 size = 0;
James Smartf1c3b0f2009-07-19 10:01:32 -04001000 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
1001 bdeBuf1 = iocbq->context2;
1002 bdeBuf2 = iocbq->context3;
1003 }
1004 for (i = 0; i < iocbq->iocb.ulpBdeCount; i++) {
James Smartf1c3b0f2009-07-19 10:01:32 -04001005 if (phba->sli3_options &
1006 LPFC_SLI3_HBQ_ENABLED) {
1007 if (i == 0) {
1008 hbqe = (struct lpfc_hbq_entry *)
1009 &iocbq->iocb.un.ulpWord[0];
1010 size = hbqe->bde.tus.f.bdeSize;
1011 dmabuf = bdeBuf1;
1012 } else if (i == 1) {
1013 hbqe = (struct lpfc_hbq_entry *)
1014 &iocbq->iocb.unsli3.
1015 sli3Words[4];
1016 size = hbqe->bde.tus.f.bdeSize;
1017 dmabuf = bdeBuf2;
1018 }
1019 if ((offset + size) > evt_dat->len)
1020 size = evt_dat->len - offset;
1021 } else {
1022 size = iocbq->iocb.un.cont64[i].
1023 tus.f.bdeSize;
1024 bde = &iocbq->iocb.un.cont64[i];
1025 dma_addr = getPaddr(bde->addrHigh,
1026 bde->addrLow);
1027 dmabuf = lpfc_sli_ringpostbuf_get(phba,
1028 pring, dma_addr);
1029 }
1030 if (!dmabuf) {
1031 lpfc_printf_log(phba, KERN_ERR,
1032 LOG_LIBDFC, "2616 No dmabuf "
1033 "found for iocbq 0x%p\n",
1034 iocbq);
1035 kfree(evt_dat->data);
1036 kfree(evt_dat);
James Smart4fede782010-01-26 23:08:55 -05001037 spin_lock_irqsave(&phba->ct_ev_lock,
1038 flags);
James Smart4cc0e562010-01-26 23:09:48 -05001039 lpfc_bsg_event_unref(evt);
James Smart4fede782010-01-26 23:08:55 -05001040 spin_unlock_irqrestore(
1041 &phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -04001042 goto error_ct_unsol_exit;
1043 }
1044 memcpy((char *)(evt_dat->data) + offset,
1045 dmabuf->virt, size);
1046 offset += size;
1047 if (evt_req_id != SLI_CT_ELX_LOOPBACK &&
1048 !(phba->sli3_options &
1049 LPFC_SLI3_HBQ_ENABLED)) {
1050 lpfc_sli_ringpostbuf_put(phba, pring,
1051 dmabuf);
1052 } else {
1053 switch (cmd) {
James Smart4cc0e562010-01-26 23:09:48 -05001054 case ELX_LOOPBACK_DATA:
James Smart1b511972011-12-13 13:23:09 -05001055 if (phba->sli_rev <
1056 LPFC_SLI_REV4)
1057 diag_cmd_data_free(phba,
1058 (struct lpfc_dmabufext
1059 *)dmabuf);
James Smart4cc0e562010-01-26 23:09:48 -05001060 break;
James Smartf1c3b0f2009-07-19 10:01:32 -04001061 case ELX_LOOPBACK_XRI_SETUP:
James Smart4cc0e562010-01-26 23:09:48 -05001062 if ((phba->sli_rev ==
1063 LPFC_SLI_REV2) ||
1064 (phba->sli3_options &
1065 LPFC_SLI3_HBQ_ENABLED
1066 )) {
1067 lpfc_in_buf_free(phba,
1068 dmabuf);
1069 } else {
James Smartf1c3b0f2009-07-19 10:01:32 -04001070 lpfc_post_buffer(phba,
1071 pring,
1072 1);
James Smart4cc0e562010-01-26 23:09:48 -05001073 }
James Smartf1c3b0f2009-07-19 10:01:32 -04001074 break;
1075 default:
1076 if (!(phba->sli3_options &
1077 LPFC_SLI3_HBQ_ENABLED))
1078 lpfc_post_buffer(phba,
1079 pring,
1080 1);
1081 break;
1082 }
1083 }
1084 }
1085 }
1086
James Smart4fede782010-01-26 23:08:55 -05001087 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -04001088 if (phba->sli_rev == LPFC_SLI_REV4) {
1089 evt_dat->immed_dat = phba->ctx_idx;
James Smart6dd9e312013-01-03 15:43:37 -05001090 phba->ctx_idx = (phba->ctx_idx + 1) % LPFC_CT_CTX_MAX;
James Smart589a52d2010-07-14 15:30:54 -04001091 /* Provide warning for over-run of the ct_ctx array */
James Smart6dd9e312013-01-03 15:43:37 -05001092 if (phba->ct_ctx[evt_dat->immed_dat].valid ==
James Smart589a52d2010-07-14 15:30:54 -04001093 UNSOL_VALID)
1094 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
1095 "2717 CT context array entry "
1096 "[%d] over-run: oxid:x%x, "
1097 "sid:x%x\n", phba->ctx_idx,
1098 phba->ct_ctx[
1099 evt_dat->immed_dat].oxid,
1100 phba->ct_ctx[
1101 evt_dat->immed_dat].SID);
James Smart7851fe22011-07-22 18:36:52 -04001102 phba->ct_ctx[evt_dat->immed_dat].rxid =
1103 piocbq->iocb.ulpContext;
James Smartf1c3b0f2009-07-19 10:01:32 -04001104 phba->ct_ctx[evt_dat->immed_dat].oxid =
James Smart7851fe22011-07-22 18:36:52 -04001105 piocbq->iocb.unsli3.rcvsli3.ox_id;
James Smartf1c3b0f2009-07-19 10:01:32 -04001106 phba->ct_ctx[evt_dat->immed_dat].SID =
1107 piocbq->iocb.un.rcvels.remoteID;
James Smart6dd9e312013-01-03 15:43:37 -05001108 phba->ct_ctx[evt_dat->immed_dat].valid = UNSOL_VALID;
James Smartf1c3b0f2009-07-19 10:01:32 -04001109 } else
1110 evt_dat->immed_dat = piocbq->iocb.ulpContext;
1111
1112 evt_dat->type = FC_REG_CT_EVENT;
1113 list_add(&evt_dat->node, &evt->events_to_see);
James Smart4cc0e562010-01-26 23:09:48 -05001114 if (evt_req_id == SLI_CT_ELX_LOOPBACK) {
1115 wake_up_interruptible(&evt->wq);
1116 lpfc_bsg_event_unref(evt);
James Smartf1c3b0f2009-07-19 10:01:32 -04001117 break;
James Smart4cc0e562010-01-26 23:09:48 -05001118 }
1119
1120 list_move(evt->events_to_see.prev, &evt->events_to_get);
James Smart4cc0e562010-01-26 23:09:48 -05001121
James Smarta33c4f72013-03-01 16:36:00 -05001122 dd_data = (struct bsg_job_data *)evt->dd_data;
1123 job = dd_data->set_job;
1124 dd_data->set_job = NULL;
1125 lpfc_bsg_event_unref(evt);
James Smart4cc0e562010-01-26 23:09:48 -05001126 if (job) {
1127 job->reply->reply_payload_rcv_len = size;
1128 /* make error code available to userspace */
1129 job->reply->result = 0;
1130 job->dd_data = NULL;
1131 /* complete the job back to userspace */
1132 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1133 job->job_done(job);
1134 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1135 }
James Smartf1c3b0f2009-07-19 10:01:32 -04001136 }
James Smart4fede782010-01-26 23:08:55 -05001137 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -04001138
1139error_ct_unsol_exit:
1140 if (!list_empty(&head))
1141 list_del(&head);
James Smart1b511972011-12-13 13:23:09 -05001142 if ((phba->sli_rev < LPFC_SLI_REV4) &&
1143 (evt_req_id == SLI_CT_ELX_LOOPBACK))
James Smart4cc0e562010-01-26 23:09:48 -05001144 return 0;
James Smart4fede782010-01-26 23:08:55 -05001145 return 1;
James Smartf1c3b0f2009-07-19 10:01:32 -04001146}
1147
1148/**
James Smart6dd9e312013-01-03 15:43:37 -05001149 * lpfc_bsg_ct_unsol_abort - handler ct abort to management plane
1150 * @phba: Pointer to HBA context object.
1151 * @dmabuf: pointer to a dmabuf that describes the FC sequence
1152 *
1153 * This function handles abort to the CT command toward management plane
1154 * for SLI4 port.
1155 *
1156 * If the pending context of a CT command to management plane present, clears
1157 * such context and returns 1 for handled; otherwise, it returns 0 indicating
1158 * no context exists.
1159 **/
1160int
1161lpfc_bsg_ct_unsol_abort(struct lpfc_hba *phba, struct hbq_dmabuf *dmabuf)
1162{
1163 struct fc_frame_header fc_hdr;
1164 struct fc_frame_header *fc_hdr_ptr = &fc_hdr;
1165 int ctx_idx, handled = 0;
1166 uint16_t oxid, rxid;
1167 uint32_t sid;
1168
1169 memcpy(fc_hdr_ptr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
1170 sid = sli4_sid_from_fc_hdr(fc_hdr_ptr);
1171 oxid = be16_to_cpu(fc_hdr_ptr->fh_ox_id);
1172 rxid = be16_to_cpu(fc_hdr_ptr->fh_rx_id);
1173
1174 for (ctx_idx = 0; ctx_idx < LPFC_CT_CTX_MAX; ctx_idx++) {
1175 if (phba->ct_ctx[ctx_idx].valid != UNSOL_VALID)
1176 continue;
1177 if (phba->ct_ctx[ctx_idx].rxid != rxid)
1178 continue;
1179 if (phba->ct_ctx[ctx_idx].oxid != oxid)
1180 continue;
1181 if (phba->ct_ctx[ctx_idx].SID != sid)
1182 continue;
1183 phba->ct_ctx[ctx_idx].valid = UNSOL_INVALID;
1184 handled = 1;
1185 }
1186 return handled;
1187}
1188
1189/**
James Smart4cc0e562010-01-26 23:09:48 -05001190 * lpfc_bsg_hba_set_event - process a SET_EVENT bsg vendor command
James Smartf1c3b0f2009-07-19 10:01:32 -04001191 * @job: SET_EVENT fc_bsg_job
James Smart3b5dd522010-01-26 23:10:15 -05001192 **/
James Smartf1c3b0f2009-07-19 10:01:32 -04001193static int
James Smart4cc0e562010-01-26 23:09:48 -05001194lpfc_bsg_hba_set_event(struct fc_bsg_job *job)
James Smartf1c3b0f2009-07-19 10:01:32 -04001195{
1196 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
1197 struct lpfc_hba *phba = vport->phba;
1198 struct set_ct_event *event_req;
James Smart4cc0e562010-01-26 23:09:48 -05001199 struct lpfc_bsg_event *evt;
James Smartf1c3b0f2009-07-19 10:01:32 -04001200 int rc = 0;
James Smart4cc0e562010-01-26 23:09:48 -05001201 struct bsg_job_data *dd_data = NULL;
1202 uint32_t ev_mask;
1203 unsigned long flags;
James Smartf1c3b0f2009-07-19 10:01:32 -04001204
1205 if (job->request_len <
1206 sizeof(struct fc_bsg_request) + sizeof(struct set_ct_event)) {
1207 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1208 "2612 Received SET_CT_EVENT below minimum "
1209 "size\n");
James Smart4cc0e562010-01-26 23:09:48 -05001210 rc = -EINVAL;
1211 goto job_error;
1212 }
1213
James Smartf1c3b0f2009-07-19 10:01:32 -04001214 event_req = (struct set_ct_event *)
1215 job->request->rqst_data.h_vendor.vendor_cmd;
James Smart4cc0e562010-01-26 23:09:48 -05001216 ev_mask = ((uint32_t)(unsigned long)event_req->type_mask &
1217 FC_REG_EVENT_MASK);
James Smart4fede782010-01-26 23:08:55 -05001218 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -04001219 list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
1220 if (evt->reg_id == event_req->ev_reg_id) {
James Smart4cc0e562010-01-26 23:09:48 -05001221 lpfc_bsg_event_ref(evt);
James Smartf1c3b0f2009-07-19 10:01:32 -04001222 evt->wait_time_stamp = jiffies;
James Smarta33c4f72013-03-01 16:36:00 -05001223 dd_data = (struct bsg_job_data *)evt->dd_data;
James Smartf1c3b0f2009-07-19 10:01:32 -04001224 break;
1225 }
1226 }
James Smart4fede782010-01-26 23:08:55 -05001227 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -04001228
1229 if (&evt->node == &phba->ct_ev_waiters) {
1230 /* no event waiting struct yet - first call */
James Smarta33c4f72013-03-01 16:36:00 -05001231 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
1232 if (dd_data == NULL) {
1233 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1234 "2734 Failed allocation of dd_data\n");
1235 rc = -ENOMEM;
1236 goto job_error;
1237 }
James Smart4cc0e562010-01-26 23:09:48 -05001238 evt = lpfc_bsg_event_new(ev_mask, event_req->ev_reg_id,
James Smartf1c3b0f2009-07-19 10:01:32 -04001239 event_req->ev_req_id);
1240 if (!evt) {
1241 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1242 "2617 Failed allocation of event "
1243 "waiter\n");
James Smart4cc0e562010-01-26 23:09:48 -05001244 rc = -ENOMEM;
1245 goto job_error;
James Smartf1c3b0f2009-07-19 10:01:32 -04001246 }
James Smarta33c4f72013-03-01 16:36:00 -05001247 dd_data->type = TYPE_EVT;
1248 dd_data->set_job = NULL;
1249 dd_data->context_un.evt = evt;
1250 evt->dd_data = (void *)dd_data;
James Smart4fede782010-01-26 23:08:55 -05001251 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -04001252 list_add(&evt->node, &phba->ct_ev_waiters);
James Smart4cc0e562010-01-26 23:09:48 -05001253 lpfc_bsg_event_ref(evt);
1254 evt->wait_time_stamp = jiffies;
James Smart4fede782010-01-26 23:08:55 -05001255 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -04001256 }
1257
James Smart4fede782010-01-26 23:08:55 -05001258 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smart4cc0e562010-01-26 23:09:48 -05001259 evt->waiting = 1;
James Smarta33c4f72013-03-01 16:36:00 -05001260 dd_data->set_job = job; /* for unsolicited command */
James Smart4cc0e562010-01-26 23:09:48 -05001261 job->dd_data = dd_data; /* for fc transport timeout callback*/
James Smart4fede782010-01-26 23:08:55 -05001262 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smart4cc0e562010-01-26 23:09:48 -05001263 return 0; /* call job done later */
James Smartf1c3b0f2009-07-19 10:01:32 -04001264
James Smart4cc0e562010-01-26 23:09:48 -05001265job_error:
1266 if (dd_data != NULL)
1267 kfree(dd_data);
James Smartf1c3b0f2009-07-19 10:01:32 -04001268
James Smart4cc0e562010-01-26 23:09:48 -05001269 job->dd_data = NULL;
1270 return rc;
James Smartf1c3b0f2009-07-19 10:01:32 -04001271}
1272
1273/**
James Smart4cc0e562010-01-26 23:09:48 -05001274 * lpfc_bsg_hba_get_event - process a GET_EVENT bsg vendor command
James Smartf1c3b0f2009-07-19 10:01:32 -04001275 * @job: GET_EVENT fc_bsg_job
James Smart3b5dd522010-01-26 23:10:15 -05001276 **/
James Smartf1c3b0f2009-07-19 10:01:32 -04001277static int
James Smart4cc0e562010-01-26 23:09:48 -05001278lpfc_bsg_hba_get_event(struct fc_bsg_job *job)
James Smartf1c3b0f2009-07-19 10:01:32 -04001279{
1280 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
1281 struct lpfc_hba *phba = vport->phba;
1282 struct get_ct_event *event_req;
1283 struct get_ct_event_reply *event_reply;
James Smart9a803a72013-09-06 12:17:56 -04001284 struct lpfc_bsg_event *evt, *evt_next;
James Smartf1c3b0f2009-07-19 10:01:32 -04001285 struct event_data *evt_dat = NULL;
James Smart4fede782010-01-26 23:08:55 -05001286 unsigned long flags;
James Smart4cc0e562010-01-26 23:09:48 -05001287 uint32_t rc = 0;
James Smartf1c3b0f2009-07-19 10:01:32 -04001288
1289 if (job->request_len <
1290 sizeof(struct fc_bsg_request) + sizeof(struct get_ct_event)) {
1291 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1292 "2613 Received GET_CT_EVENT request below "
1293 "minimum size\n");
James Smart4cc0e562010-01-26 23:09:48 -05001294 rc = -EINVAL;
1295 goto job_error;
James Smartf1c3b0f2009-07-19 10:01:32 -04001296 }
1297
1298 event_req = (struct get_ct_event *)
1299 job->request->rqst_data.h_vendor.vendor_cmd;
1300
1301 event_reply = (struct get_ct_event_reply *)
1302 job->reply->reply_data.vendor_reply.vendor_rsp;
James Smart4fede782010-01-26 23:08:55 -05001303 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smart9a803a72013-09-06 12:17:56 -04001304 list_for_each_entry_safe(evt, evt_next, &phba->ct_ev_waiters, node) {
James Smartf1c3b0f2009-07-19 10:01:32 -04001305 if (evt->reg_id == event_req->ev_reg_id) {
1306 if (list_empty(&evt->events_to_get))
1307 break;
James Smart4cc0e562010-01-26 23:09:48 -05001308 lpfc_bsg_event_ref(evt);
James Smartf1c3b0f2009-07-19 10:01:32 -04001309 evt->wait_time_stamp = jiffies;
1310 evt_dat = list_entry(evt->events_to_get.prev,
1311 struct event_data, node);
1312 list_del(&evt_dat->node);
1313 break;
1314 }
1315 }
James Smart4fede782010-01-26 23:08:55 -05001316 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -04001317
James Smart4cc0e562010-01-26 23:09:48 -05001318 /* The app may continue to ask for event data until it gets
1319 * an error indicating that there isn't anymore
1320 */
1321 if (evt_dat == NULL) {
James Smartf1c3b0f2009-07-19 10:01:32 -04001322 job->reply->reply_payload_rcv_len = 0;
1323 rc = -ENOENT;
James Smart4cc0e562010-01-26 23:09:48 -05001324 goto job_error;
James Smartf1c3b0f2009-07-19 10:01:32 -04001325 }
1326
James Smart4cc0e562010-01-26 23:09:48 -05001327 if (evt_dat->len > job->request_payload.payload_len) {
1328 evt_dat->len = job->request_payload.payload_len;
1329 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1330 "2618 Truncated event data at %d "
1331 "bytes\n",
1332 job->request_payload.payload_len);
James Smartf1c3b0f2009-07-19 10:01:32 -04001333 }
1334
James Smart4cc0e562010-01-26 23:09:48 -05001335 event_reply->type = evt_dat->type;
James Smartf1c3b0f2009-07-19 10:01:32 -04001336 event_reply->immed_data = evt_dat->immed_dat;
James Smartf1c3b0f2009-07-19 10:01:32 -04001337 if (evt_dat->len > 0)
1338 job->reply->reply_payload_rcv_len =
James Smart4cc0e562010-01-26 23:09:48 -05001339 sg_copy_from_buffer(job->request_payload.sg_list,
1340 job->request_payload.sg_cnt,
James Smartf1c3b0f2009-07-19 10:01:32 -04001341 evt_dat->data, evt_dat->len);
1342 else
1343 job->reply->reply_payload_rcv_len = 0;
James Smartf1c3b0f2009-07-19 10:01:32 -04001344
James Smart4cc0e562010-01-26 23:09:48 -05001345 if (evt_dat) {
James Smartf1c3b0f2009-07-19 10:01:32 -04001346 kfree(evt_dat->data);
James Smart4cc0e562010-01-26 23:09:48 -05001347 kfree(evt_dat);
1348 }
1349
James Smart4fede782010-01-26 23:08:55 -05001350 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smart4cc0e562010-01-26 23:09:48 -05001351 lpfc_bsg_event_unref(evt);
James Smart4fede782010-01-26 23:08:55 -05001352 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smart4cc0e562010-01-26 23:09:48 -05001353 job->dd_data = NULL;
1354 job->reply->result = 0;
James Smartf1c3b0f2009-07-19 10:01:32 -04001355 job->job_done(job);
James Smart4cc0e562010-01-26 23:09:48 -05001356 return 0;
James Smartf1c3b0f2009-07-19 10:01:32 -04001357
James Smart4cc0e562010-01-26 23:09:48 -05001358job_error:
1359 job->dd_data = NULL;
1360 job->reply->result = rc;
James Smartf1c3b0f2009-07-19 10:01:32 -04001361 return rc;
1362}
1363
1364/**
James Smart3b5dd522010-01-26 23:10:15 -05001365 * lpfc_issue_ct_rsp_cmp - lpfc_issue_ct_rsp's completion handler
1366 * @phba: Pointer to HBA context object.
1367 * @cmdiocbq: Pointer to command iocb.
1368 * @rspiocbq: Pointer to response iocb.
1369 *
1370 * This function is the completion handler for iocbs issued using
1371 * lpfc_issue_ct_rsp_cmp function. This function is called by the
1372 * ring event handler function without any lock held. This function
1373 * can be called from both worker thread context and interrupt
1374 * context. This function also can be called from other thread which
1375 * cleans up the SLI layer objects.
1376 * This function copy the contents of the response iocb to the
1377 * response iocb memory object provided by the caller of
1378 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
1379 * sleeps for the iocb completion.
1380 **/
1381static void
1382lpfc_issue_ct_rsp_cmp(struct lpfc_hba *phba,
1383 struct lpfc_iocbq *cmdiocbq,
1384 struct lpfc_iocbq *rspiocbq)
1385{
1386 struct bsg_job_data *dd_data;
1387 struct fc_bsg_job *job;
1388 IOCB_t *rsp;
James Smarta33c4f72013-03-01 16:36:00 -05001389 struct lpfc_dmabuf *bmp, *cmp;
James Smart3b5dd522010-01-26 23:10:15 -05001390 struct lpfc_nodelist *ndlp;
1391 unsigned long flags;
1392 int rc = 0;
1393
James Smarta33c4f72013-03-01 16:36:00 -05001394 dd_data = cmdiocbq->context1;
1395
1396 /* Determine if job has been aborted */
James Smart3b5dd522010-01-26 23:10:15 -05001397 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smarta33c4f72013-03-01 16:36:00 -05001398 job = dd_data->set_job;
1399 if (job) {
1400 /* Prevent timeout handling from trying to abort job */
1401 job->dd_data = NULL;
1402 }
1403 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1404
James Smartb5a9b2d2013-09-06 12:19:45 -04001405 /* Close the timeout handler abort window */
1406 spin_lock_irqsave(&phba->hbalock, flags);
James Smart1b8d11a2013-09-06 12:20:51 -04001407 cmdiocbq->iocb_flag &= ~LPFC_IO_CMD_OUTSTANDING;
James Smartb5a9b2d2013-09-06 12:19:45 -04001408 spin_unlock_irqrestore(&phba->hbalock, flags);
1409
James Smarta33c4f72013-03-01 16:36:00 -05001410 ndlp = dd_data->context_un.iocb.ndlp;
1411 cmp = cmdiocbq->context2;
1412 bmp = cmdiocbq->context3;
1413 rsp = &rspiocbq->iocb;
1414
1415 /* Copy the completed job data or set the error status */
1416
1417 if (job) {
1418 if (rsp->ulpStatus) {
1419 if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
1420 switch (rsp->un.ulpWord[4] & IOERR_PARAM_MASK) {
1421 case IOERR_SEQUENCE_TIMEOUT:
1422 rc = -ETIMEDOUT;
1423 break;
1424 case IOERR_INVALID_RPI:
1425 rc = -EFAULT;
1426 break;
1427 default:
1428 rc = -EACCES;
1429 break;
1430 }
1431 } else {
1432 rc = -EACCES;
1433 }
1434 } else {
1435 job->reply->reply_payload_rcv_len = 0;
1436 }
James Smart3b5dd522010-01-26 23:10:15 -05001437 }
1438
James Smarta33c4f72013-03-01 16:36:00 -05001439 lpfc_free_bsg_buffers(phba, cmp);
James Smart3b5dd522010-01-26 23:10:15 -05001440 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
James Smarta33c4f72013-03-01 16:36:00 -05001441 kfree(bmp);
James Smart3b5dd522010-01-26 23:10:15 -05001442 lpfc_sli_release_iocbq(phba, cmdiocbq);
1443 lpfc_nlp_put(ndlp);
James Smart3b5dd522010-01-26 23:10:15 -05001444 kfree(dd_data);
James Smarta33c4f72013-03-01 16:36:00 -05001445
1446 /* Complete the job if the job is still active */
1447
1448 if (job) {
1449 job->reply->result = rc;
1450 job->job_done(job);
1451 }
James Smart3b5dd522010-01-26 23:10:15 -05001452 return;
1453}
1454
1455/**
1456 * lpfc_issue_ct_rsp - issue a ct response
1457 * @phba: Pointer to HBA context object.
1458 * @job: Pointer to the job object.
1459 * @tag: tag index value into the ports context exchange array.
1460 * @bmp: Pointer to a dma buffer descriptor.
1461 * @num_entry: Number of enties in the bde.
1462 **/
1463static int
1464lpfc_issue_ct_rsp(struct lpfc_hba *phba, struct fc_bsg_job *job, uint32_t tag,
James Smarta33c4f72013-03-01 16:36:00 -05001465 struct lpfc_dmabuf *cmp, struct lpfc_dmabuf *bmp,
1466 int num_entry)
James Smart3b5dd522010-01-26 23:10:15 -05001467{
1468 IOCB_t *icmd;
1469 struct lpfc_iocbq *ctiocb = NULL;
1470 int rc = 0;
1471 struct lpfc_nodelist *ndlp = NULL;
1472 struct bsg_job_data *dd_data;
James Smartb5a9b2d2013-09-06 12:19:45 -04001473 unsigned long flags;
James Smart3b5dd522010-01-26 23:10:15 -05001474 uint32_t creg_val;
1475
1476 /* allocate our bsg tracking structure */
1477 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
1478 if (!dd_data) {
1479 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1480 "2736 Failed allocation of dd_data\n");
1481 rc = -ENOMEM;
1482 goto no_dd_data;
1483 }
1484
1485 /* Allocate buffer for command iocb */
1486 ctiocb = lpfc_sli_get_iocbq(phba);
1487 if (!ctiocb) {
James Smartd439d282010-09-29 11:18:45 -04001488 rc = -ENOMEM;
James Smart3b5dd522010-01-26 23:10:15 -05001489 goto no_ctiocb;
1490 }
1491
1492 icmd = &ctiocb->iocb;
1493 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
1494 icmd->un.xseq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
1495 icmd->un.xseq64.bdl.addrLow = putPaddrLow(bmp->phys);
1496 icmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
1497 icmd->un.xseq64.bdl.bdeSize = (num_entry * sizeof(struct ulp_bde64));
1498 icmd->un.xseq64.w5.hcsw.Fctl = (LS | LA);
1499 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
1500 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_SOL_CTL;
1501 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
1502
1503 /* Fill in rest of iocb */
1504 icmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;
1505 icmd->ulpBdeCount = 1;
1506 icmd->ulpLe = 1;
1507 icmd->ulpClass = CLASS3;
1508 if (phba->sli_rev == LPFC_SLI_REV4) {
1509 /* Do not issue unsol response if oxid not marked as valid */
James Smart6dd9e312013-01-03 15:43:37 -05001510 if (phba->ct_ctx[tag].valid != UNSOL_VALID) {
James Smart3b5dd522010-01-26 23:10:15 -05001511 rc = IOCB_ERROR;
1512 goto issue_ct_rsp_exit;
1513 }
James Smart7851fe22011-07-22 18:36:52 -04001514 icmd->ulpContext = phba->ct_ctx[tag].rxid;
1515 icmd->unsli3.rcvsli3.ox_id = phba->ct_ctx[tag].oxid;
James Smart3b5dd522010-01-26 23:10:15 -05001516 ndlp = lpfc_findnode_did(phba->pport, phba->ct_ctx[tag].SID);
1517 if (!ndlp) {
1518 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
1519 "2721 ndlp null for oxid %x SID %x\n",
1520 icmd->ulpContext,
1521 phba->ct_ctx[tag].SID);
1522 rc = IOCB_ERROR;
1523 goto issue_ct_rsp_exit;
1524 }
James Smart589a52d2010-07-14 15:30:54 -04001525
1526 /* Check if the ndlp is active */
1527 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smarta33c4f72013-03-01 16:36:00 -05001528 rc = IOCB_ERROR;
James Smart589a52d2010-07-14 15:30:54 -04001529 goto issue_ct_rsp_exit;
1530 }
1531
1532 /* get a refernece count so the ndlp doesn't go away while
1533 * we respond
1534 */
1535 if (!lpfc_nlp_get(ndlp)) {
James Smarta33c4f72013-03-01 16:36:00 -05001536 rc = IOCB_ERROR;
James Smart589a52d2010-07-14 15:30:54 -04001537 goto issue_ct_rsp_exit;
1538 }
1539
James Smart7851fe22011-07-22 18:36:52 -04001540 icmd->un.ulpWord[3] =
James Smart6d368e52011-05-24 11:44:12 -04001541 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
1542
James Smart3b5dd522010-01-26 23:10:15 -05001543 /* The exchange is done, mark the entry as invalid */
James Smart6dd9e312013-01-03 15:43:37 -05001544 phba->ct_ctx[tag].valid = UNSOL_INVALID;
James Smart3b5dd522010-01-26 23:10:15 -05001545 } else
1546 icmd->ulpContext = (ushort) tag;
1547
1548 icmd->ulpTimeout = phba->fc_ratov * 2;
1549
1550 /* Xmit CT response on exchange <xid> */
1551 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart7851fe22011-07-22 18:36:52 -04001552 "2722 Xmit CT response on exchange x%x Data: x%x x%x x%x\n",
1553 icmd->ulpContext, icmd->ulpIoTag, tag, phba->link_state);
James Smart3b5dd522010-01-26 23:10:15 -05001554
1555 ctiocb->iocb_cmpl = NULL;
1556 ctiocb->iocb_flag |= LPFC_IO_LIBDFC;
1557 ctiocb->vport = phba->pport;
James Smarta33c4f72013-03-01 16:36:00 -05001558 ctiocb->context1 = dd_data;
1559 ctiocb->context2 = cmp;
James Smart3b5dd522010-01-26 23:10:15 -05001560 ctiocb->context3 = bmp;
James Smartd5ce53b2013-04-17 20:17:26 -04001561 ctiocb->context_un.ndlp = ndlp;
James Smart3b5dd522010-01-26 23:10:15 -05001562 ctiocb->iocb_cmpl = lpfc_issue_ct_rsp_cmp;
James Smarta33c4f72013-03-01 16:36:00 -05001563
James Smart3b5dd522010-01-26 23:10:15 -05001564 dd_data->type = TYPE_IOCB;
James Smarta33c4f72013-03-01 16:36:00 -05001565 dd_data->set_job = job;
James Smart3b5dd522010-01-26 23:10:15 -05001566 dd_data->context_un.iocb.cmdiocbq = ctiocb;
James Smart3b5dd522010-01-26 23:10:15 -05001567 dd_data->context_un.iocb.ndlp = ndlp;
James Smarta33c4f72013-03-01 16:36:00 -05001568 dd_data->context_un.iocb.rmp = NULL;
1569 job->dd_data = dd_data;
James Smart3b5dd522010-01-26 23:10:15 -05001570
1571 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -05001572 if (lpfc_readl(phba->HCregaddr, &creg_val)) {
1573 rc = -IOCB_ERROR;
1574 goto issue_ct_rsp_exit;
1575 }
James Smart3b5dd522010-01-26 23:10:15 -05001576 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
1577 writel(creg_val, phba->HCregaddr);
1578 readl(phba->HCregaddr); /* flush */
1579 }
1580
1581 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
1582
James Smartb5a9b2d2013-09-06 12:19:45 -04001583 if (rc == IOCB_SUCCESS) {
1584 spin_lock_irqsave(&phba->hbalock, flags);
1585 /* make sure the I/O had not been completed/released */
1586 if (ctiocb->iocb_flag & LPFC_IO_LIBDFC) {
1587 /* open up abort window to timeout handler */
James Smart1b8d11a2013-09-06 12:20:51 -04001588 ctiocb->iocb_flag |= LPFC_IO_CMD_OUTSTANDING;
James Smartb5a9b2d2013-09-06 12:19:45 -04001589 }
1590 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart3b5dd522010-01-26 23:10:15 -05001591 return 0; /* done for now */
James Smartb5a9b2d2013-09-06 12:19:45 -04001592 }
1593
1594 /* iocb failed so cleanup */
1595 job->dd_data = NULL;
James Smart3b5dd522010-01-26 23:10:15 -05001596
1597issue_ct_rsp_exit:
1598 lpfc_sli_release_iocbq(phba, ctiocb);
1599no_ctiocb:
1600 kfree(dd_data);
1601no_dd_data:
1602 return rc;
1603}
1604
1605/**
1606 * lpfc_bsg_send_mgmt_rsp - process a SEND_MGMT_RESP bsg vendor command
1607 * @job: SEND_MGMT_RESP fc_bsg_job
1608 **/
1609static int
1610lpfc_bsg_send_mgmt_rsp(struct fc_bsg_job *job)
1611{
1612 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
1613 struct lpfc_hba *phba = vport->phba;
1614 struct send_mgmt_resp *mgmt_resp = (struct send_mgmt_resp *)
1615 job->request->rqst_data.h_vendor.vendor_cmd;
1616 struct ulp_bde64 *bpl;
James Smarta33c4f72013-03-01 16:36:00 -05001617 struct lpfc_dmabuf *bmp = NULL, *cmp = NULL;
1618 int bpl_entries;
James Smart3b5dd522010-01-26 23:10:15 -05001619 uint32_t tag = mgmt_resp->tag;
1620 unsigned long reqbfrcnt =
1621 (unsigned long)job->request_payload.payload_len;
1622 int rc = 0;
1623
1624 /* in case no data is transferred */
1625 job->reply->reply_payload_rcv_len = 0;
1626
1627 if (!reqbfrcnt || (reqbfrcnt > (80 * BUF_SZ_4K))) {
1628 rc = -ERANGE;
1629 goto send_mgmt_rsp_exit;
1630 }
1631
1632 bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
1633 if (!bmp) {
1634 rc = -ENOMEM;
1635 goto send_mgmt_rsp_exit;
1636 }
1637
1638 bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
1639 if (!bmp->virt) {
1640 rc = -ENOMEM;
1641 goto send_mgmt_rsp_free_bmp;
1642 }
1643
1644 INIT_LIST_HEAD(&bmp->list);
1645 bpl = (struct ulp_bde64 *) bmp->virt;
James Smarta33c4f72013-03-01 16:36:00 -05001646 bpl_entries = (LPFC_BPL_SIZE/sizeof(struct ulp_bde64));
1647 cmp = lpfc_alloc_bsg_buffers(phba, job->request_payload.payload_len,
1648 1, bpl, &bpl_entries);
1649 if (!cmp) {
1650 rc = -ENOMEM;
1651 goto send_mgmt_rsp_free_bmp;
James Smart3b5dd522010-01-26 23:10:15 -05001652 }
James Smarta33c4f72013-03-01 16:36:00 -05001653 lpfc_bsg_copy_data(cmp, &job->request_payload,
1654 job->request_payload.payload_len, 1);
James Smart3b5dd522010-01-26 23:10:15 -05001655
James Smarta33c4f72013-03-01 16:36:00 -05001656 rc = lpfc_issue_ct_rsp(phba, job, tag, cmp, bmp, bpl_entries);
James Smart3b5dd522010-01-26 23:10:15 -05001657
1658 if (rc == IOCB_SUCCESS)
1659 return 0; /* done for now */
1660
James Smart3b5dd522010-01-26 23:10:15 -05001661 rc = -EACCES;
James Smarta33c4f72013-03-01 16:36:00 -05001662
1663 lpfc_free_bsg_buffers(phba, cmp);
James Smart3b5dd522010-01-26 23:10:15 -05001664
1665send_mgmt_rsp_free_bmp:
James Smarta33c4f72013-03-01 16:36:00 -05001666 if (bmp->virt)
1667 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
James Smart3b5dd522010-01-26 23:10:15 -05001668 kfree(bmp);
1669send_mgmt_rsp_exit:
1670 /* make error code available to userspace */
1671 job->reply->result = rc;
1672 job->dd_data = NULL;
1673 return rc;
1674}
1675
1676/**
James Smart7ad20aa2011-05-24 11:44:28 -04001677 * lpfc_bsg_diag_mode_enter - process preparing into device diag loopback mode
1678 * @phba: Pointer to HBA context object.
James Smart3b5dd522010-01-26 23:10:15 -05001679 *
James Smart7ad20aa2011-05-24 11:44:28 -04001680 * This function is responsible for preparing driver for diag loopback
1681 * on device.
1682 */
1683static int
James Smart88a2cfb2011-07-22 18:36:33 -04001684lpfc_bsg_diag_mode_enter(struct lpfc_hba *phba)
James Smart7ad20aa2011-05-24 11:44:28 -04001685{
1686 struct lpfc_vport **vports;
1687 struct Scsi_Host *shost;
1688 struct lpfc_sli *psli;
1689 struct lpfc_sli_ring *pring;
1690 int i = 0;
1691
1692 psli = &phba->sli;
1693 if (!psli)
1694 return -ENODEV;
1695
1696 pring = &psli->ring[LPFC_FCP_RING];
1697 if (!pring)
1698 return -ENODEV;
1699
1700 if ((phba->link_state == LPFC_HBA_ERROR) ||
1701 (psli->sli_flag & LPFC_BLOCK_MGMT_IO) ||
1702 (!(psli->sli_flag & LPFC_SLI_ACTIVE)))
1703 return -EACCES;
1704
1705 vports = lpfc_create_vport_work_array(phba);
1706 if (vports) {
1707 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
1708 shost = lpfc_shost_from_vport(vports[i]);
1709 scsi_block_requests(shost);
1710 }
1711 lpfc_destroy_vport_work_array(phba, vports);
1712 } else {
1713 shost = lpfc_shost_from_vport(phba->pport);
1714 scsi_block_requests(shost);
1715 }
1716
James Smart0e9bb8d2013-03-01 16:35:12 -05001717 while (!list_empty(&pring->txcmplq)) {
James Smart7ad20aa2011-05-24 11:44:28 -04001718 if (i++ > 500) /* wait up to 5 seconds */
1719 break;
1720 msleep(10);
1721 }
1722 return 0;
1723}
1724
1725/**
1726 * lpfc_bsg_diag_mode_exit - exit process from device diag loopback mode
1727 * @phba: Pointer to HBA context object.
James Smart7ad20aa2011-05-24 11:44:28 -04001728 *
1729 * This function is responsible for driver exit processing of setting up
1730 * diag loopback mode on device.
1731 */
1732static void
1733lpfc_bsg_diag_mode_exit(struct lpfc_hba *phba)
1734{
1735 struct Scsi_Host *shost;
1736 struct lpfc_vport **vports;
1737 int i;
1738
1739 vports = lpfc_create_vport_work_array(phba);
1740 if (vports) {
1741 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
1742 shost = lpfc_shost_from_vport(vports[i]);
1743 scsi_unblock_requests(shost);
1744 }
1745 lpfc_destroy_vport_work_array(phba, vports);
1746 } else {
1747 shost = lpfc_shost_from_vport(phba->pport);
1748 scsi_unblock_requests(shost);
1749 }
1750 return;
1751}
1752
1753/**
1754 * lpfc_sli3_bsg_diag_loopback_mode - process an sli3 bsg vendor command
1755 * @phba: Pointer to HBA context object.
1756 * @job: LPFC_BSG_VENDOR_DIAG_MODE
1757 *
1758 * This function is responsible for placing an sli3 port into diagnostic
1759 * loopback mode in order to perform a diagnostic loopback test.
James Smart3b5dd522010-01-26 23:10:15 -05001760 * All new scsi requests are blocked, a small delay is used to allow the
1761 * scsi requests to complete then the link is brought down. If the link is
1762 * is placed in loopback mode then scsi requests are again allowed
1763 * so the scsi mid-layer doesn't give up on the port.
1764 * All of this is done in-line.
1765 */
1766static int
James Smart7ad20aa2011-05-24 11:44:28 -04001767lpfc_sli3_bsg_diag_loopback_mode(struct lpfc_hba *phba, struct fc_bsg_job *job)
James Smart3b5dd522010-01-26 23:10:15 -05001768{
James Smart3b5dd522010-01-26 23:10:15 -05001769 struct diag_mode_set *loopback_mode;
James Smart3b5dd522010-01-26 23:10:15 -05001770 uint32_t link_flags;
1771 uint32_t timeout;
James Smart1b511972011-12-13 13:23:09 -05001772 LPFC_MBOXQ_t *pmboxq = NULL;
James Smartb76f2dc2011-07-22 18:37:42 -04001773 int mbxstatus = MBX_SUCCESS;
James Smart3b5dd522010-01-26 23:10:15 -05001774 int i = 0;
1775 int rc = 0;
1776
1777 /* no data to return just the return code */
1778 job->reply->reply_payload_rcv_len = 0;
1779
James Smart7ad20aa2011-05-24 11:44:28 -04001780 if (job->request_len < sizeof(struct fc_bsg_request) +
1781 sizeof(struct diag_mode_set)) {
James Smart3b5dd522010-01-26 23:10:15 -05001782 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
James Smart7ad20aa2011-05-24 11:44:28 -04001783 "2738 Received DIAG MODE request size:%d "
1784 "below the minimum size:%d\n",
1785 job->request_len,
1786 (int)(sizeof(struct fc_bsg_request) +
1787 sizeof(struct diag_mode_set)));
James Smart3b5dd522010-01-26 23:10:15 -05001788 rc = -EINVAL;
1789 goto job_error;
1790 }
1791
James Smart88a2cfb2011-07-22 18:36:33 -04001792 rc = lpfc_bsg_diag_mode_enter(phba);
James Smart7ad20aa2011-05-24 11:44:28 -04001793 if (rc)
1794 goto job_error;
1795
1796 /* bring the link to diagnostic mode */
James Smart3b5dd522010-01-26 23:10:15 -05001797 loopback_mode = (struct diag_mode_set *)
1798 job->request->rqst_data.h_vendor.vendor_cmd;
1799 link_flags = loopback_mode->type;
James Smart515e0aa2010-09-29 11:19:00 -04001800 timeout = loopback_mode->timeout * 100;
James Smart3b5dd522010-01-26 23:10:15 -05001801
James Smart3b5dd522010-01-26 23:10:15 -05001802 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1803 if (!pmboxq) {
1804 rc = -ENOMEM;
James Smart7ad20aa2011-05-24 11:44:28 -04001805 goto loopback_mode_exit;
James Smart3b5dd522010-01-26 23:10:15 -05001806 }
James Smart3b5dd522010-01-26 23:10:15 -05001807 memset((void *)pmboxq, 0, sizeof(LPFC_MBOXQ_t));
1808 pmboxq->u.mb.mbxCommand = MBX_DOWN_LINK;
1809 pmboxq->u.mb.mbxOwner = OWN_HOST;
1810
1811 mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
1812
1813 if ((mbxstatus == MBX_SUCCESS) && (pmboxq->u.mb.mbxStatus == 0)) {
1814 /* wait for link down before proceeding */
1815 i = 0;
1816 while (phba->link_state != LPFC_LINK_DOWN) {
1817 if (i++ > timeout) {
1818 rc = -ETIMEDOUT;
1819 goto loopback_mode_exit;
1820 }
James Smart3b5dd522010-01-26 23:10:15 -05001821 msleep(10);
1822 }
1823
1824 memset((void *)pmboxq, 0, sizeof(LPFC_MBOXQ_t));
1825 if (link_flags == INTERNAL_LOOP_BACK)
1826 pmboxq->u.mb.un.varInitLnk.link_flags = FLAGS_LOCAL_LB;
1827 else
1828 pmboxq->u.mb.un.varInitLnk.link_flags =
1829 FLAGS_TOPOLOGY_MODE_LOOP;
1830
1831 pmboxq->u.mb.mbxCommand = MBX_INIT_LINK;
1832 pmboxq->u.mb.mbxOwner = OWN_HOST;
1833
1834 mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq,
1835 LPFC_MBOX_TMO);
1836
1837 if ((mbxstatus != MBX_SUCCESS) || (pmboxq->u.mb.mbxStatus))
1838 rc = -ENODEV;
1839 else {
James Smart1b511972011-12-13 13:23:09 -05001840 spin_lock_irq(&phba->hbalock);
James Smart3b5dd522010-01-26 23:10:15 -05001841 phba->link_flag |= LS_LOOPBACK_MODE;
James Smart1b511972011-12-13 13:23:09 -05001842 spin_unlock_irq(&phba->hbalock);
James Smart3b5dd522010-01-26 23:10:15 -05001843 /* wait for the link attention interrupt */
1844 msleep(100);
1845
1846 i = 0;
1847 while (phba->link_state != LPFC_HBA_READY) {
1848 if (i++ > timeout) {
1849 rc = -ETIMEDOUT;
1850 break;
1851 }
1852
1853 msleep(10);
1854 }
1855 }
1856
1857 } else
1858 rc = -ENODEV;
1859
1860loopback_mode_exit:
James Smart7ad20aa2011-05-24 11:44:28 -04001861 lpfc_bsg_diag_mode_exit(phba);
James Smart3b5dd522010-01-26 23:10:15 -05001862
1863 /*
1864 * Let SLI layer release mboxq if mbox command completed after timeout.
1865 */
James Smart1b511972011-12-13 13:23:09 -05001866 if (pmboxq && mbxstatus != MBX_TIMEOUT)
James Smart3b5dd522010-01-26 23:10:15 -05001867 mempool_free(pmboxq, phba->mbox_mem_pool);
1868
1869job_error:
1870 /* make error code available to userspace */
1871 job->reply->result = rc;
1872 /* complete the job back to userspace if no error */
1873 if (rc == 0)
1874 job->job_done(job);
1875 return rc;
1876}
1877
1878/**
James Smart7ad20aa2011-05-24 11:44:28 -04001879 * lpfc_sli4_bsg_set_link_diag_state - set sli4 link diag state
1880 * @phba: Pointer to HBA context object.
1881 * @diag: Flag for set link to diag or nomral operation state.
1882 *
1883 * This function is responsible for issuing a sli4 mailbox command for setting
1884 * link to either diag state or normal operation state.
1885 */
1886static int
1887lpfc_sli4_bsg_set_link_diag_state(struct lpfc_hba *phba, uint32_t diag)
1888{
1889 LPFC_MBOXQ_t *pmboxq;
1890 struct lpfc_mbx_set_link_diag_state *link_diag_state;
1891 uint32_t req_len, alloc_len;
1892 int mbxstatus = MBX_SUCCESS, rc;
1893
1894 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1895 if (!pmboxq)
1896 return -ENOMEM;
1897
1898 req_len = (sizeof(struct lpfc_mbx_set_link_diag_state) -
1899 sizeof(struct lpfc_sli4_cfg_mhdr));
1900 alloc_len = lpfc_sli4_config(phba, pmboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
1901 LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_STATE,
1902 req_len, LPFC_SLI4_MBX_EMBED);
1903 if (alloc_len != req_len) {
1904 rc = -ENOMEM;
1905 goto link_diag_state_set_out;
1906 }
James Smart1b511972011-12-13 13:23:09 -05001907 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
1908 "3128 Set link to diagnostic state:x%x (x%x/x%x)\n",
1909 diag, phba->sli4_hba.lnk_info.lnk_tp,
1910 phba->sli4_hba.lnk_info.lnk_no);
1911
James Smart7ad20aa2011-05-24 11:44:28 -04001912 link_diag_state = &pmboxq->u.mqe.un.link_diag_state;
James Smart97315922012-08-03 12:32:52 -04001913 bf_set(lpfc_mbx_set_diag_state_diag_bit_valid, &link_diag_state->u.req,
1914 LPFC_DIAG_STATE_DIAG_BIT_VALID_CHANGE);
James Smart7ad20aa2011-05-24 11:44:28 -04001915 bf_set(lpfc_mbx_set_diag_state_link_num, &link_diag_state->u.req,
James Smart1b511972011-12-13 13:23:09 -05001916 phba->sli4_hba.lnk_info.lnk_no);
James Smart7ad20aa2011-05-24 11:44:28 -04001917 bf_set(lpfc_mbx_set_diag_state_link_type, &link_diag_state->u.req,
James Smart1b511972011-12-13 13:23:09 -05001918 phba->sli4_hba.lnk_info.lnk_tp);
James Smart7ad20aa2011-05-24 11:44:28 -04001919 if (diag)
1920 bf_set(lpfc_mbx_set_diag_state_diag,
1921 &link_diag_state->u.req, 1);
1922 else
1923 bf_set(lpfc_mbx_set_diag_state_diag,
1924 &link_diag_state->u.req, 0);
1925
1926 mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
1927
1928 if ((mbxstatus == MBX_SUCCESS) && (pmboxq->u.mb.mbxStatus == 0))
1929 rc = 0;
1930 else
1931 rc = -ENODEV;
1932
1933link_diag_state_set_out:
1934 if (pmboxq && (mbxstatus != MBX_TIMEOUT))
1935 mempool_free(pmboxq, phba->mbox_mem_pool);
1936
1937 return rc;
1938}
1939
1940/**
James Smart1b511972011-12-13 13:23:09 -05001941 * lpfc_sli4_bsg_set_internal_loopback - set sli4 internal loopback diagnostic
1942 * @phba: Pointer to HBA context object.
1943 *
1944 * This function is responsible for issuing a sli4 mailbox command for setting
1945 * up internal loopback diagnostic.
1946 */
1947static int
1948lpfc_sli4_bsg_set_internal_loopback(struct lpfc_hba *phba)
1949{
1950 LPFC_MBOXQ_t *pmboxq;
1951 uint32_t req_len, alloc_len;
1952 struct lpfc_mbx_set_link_diag_loopback *link_diag_loopback;
1953 int mbxstatus = MBX_SUCCESS, rc = 0;
1954
1955 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1956 if (!pmboxq)
1957 return -ENOMEM;
1958 req_len = (sizeof(struct lpfc_mbx_set_link_diag_loopback) -
1959 sizeof(struct lpfc_sli4_cfg_mhdr));
1960 alloc_len = lpfc_sli4_config(phba, pmboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
1961 LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_LOOPBACK,
1962 req_len, LPFC_SLI4_MBX_EMBED);
1963 if (alloc_len != req_len) {
1964 mempool_free(pmboxq, phba->mbox_mem_pool);
1965 return -ENOMEM;
1966 }
1967 link_diag_loopback = &pmboxq->u.mqe.un.link_diag_loopback;
1968 bf_set(lpfc_mbx_set_diag_state_link_num,
1969 &link_diag_loopback->u.req, phba->sli4_hba.lnk_info.lnk_no);
1970 bf_set(lpfc_mbx_set_diag_state_link_type,
1971 &link_diag_loopback->u.req, phba->sli4_hba.lnk_info.lnk_tp);
1972 bf_set(lpfc_mbx_set_diag_lpbk_type, &link_diag_loopback->u.req,
James Smart3ef6d242012-01-18 16:23:48 -05001973 LPFC_DIAG_LOOPBACK_TYPE_INTERNAL);
James Smart1b511972011-12-13 13:23:09 -05001974
1975 mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
1976 if ((mbxstatus != MBX_SUCCESS) || (pmboxq->u.mb.mbxStatus)) {
1977 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1978 "3127 Failed setup loopback mode mailbox "
1979 "command, rc:x%x, status:x%x\n", mbxstatus,
1980 pmboxq->u.mb.mbxStatus);
1981 rc = -ENODEV;
1982 }
1983 if (pmboxq && (mbxstatus != MBX_TIMEOUT))
1984 mempool_free(pmboxq, phba->mbox_mem_pool);
1985 return rc;
1986}
1987
1988/**
1989 * lpfc_sli4_diag_fcport_reg_setup - setup port registrations for diagnostic
1990 * @phba: Pointer to HBA context object.
1991 *
1992 * This function set up SLI4 FC port registrations for diagnostic run, which
1993 * includes all the rpis, vfi, and also vpi.
1994 */
1995static int
1996lpfc_sli4_diag_fcport_reg_setup(struct lpfc_hba *phba)
1997{
1998 int rc;
1999
2000 if (phba->pport->fc_flag & FC_VFI_REGISTERED) {
2001 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2002 "3136 Port still had vfi registered: "
2003 "mydid:x%x, fcfi:%d, vfi:%d, vpi:%d\n",
2004 phba->pport->fc_myDID, phba->fcf.fcfi,
2005 phba->sli4_hba.vfi_ids[phba->pport->vfi],
2006 phba->vpi_ids[phba->pport->vpi]);
2007 return -EINVAL;
2008 }
2009 rc = lpfc_issue_reg_vfi(phba->pport);
2010 return rc;
2011}
2012
2013/**
James Smart7ad20aa2011-05-24 11:44:28 -04002014 * lpfc_sli4_bsg_diag_loopback_mode - process an sli4 bsg vendor command
2015 * @phba: Pointer to HBA context object.
2016 * @job: LPFC_BSG_VENDOR_DIAG_MODE
2017 *
2018 * This function is responsible for placing an sli4 port into diagnostic
2019 * loopback mode in order to perform a diagnostic loopback test.
2020 */
2021static int
2022lpfc_sli4_bsg_diag_loopback_mode(struct lpfc_hba *phba, struct fc_bsg_job *job)
2023{
2024 struct diag_mode_set *loopback_mode;
James Smart1b511972011-12-13 13:23:09 -05002025 uint32_t link_flags, timeout;
2026 int i, rc = 0;
James Smart7ad20aa2011-05-24 11:44:28 -04002027
2028 /* no data to return just the return code */
2029 job->reply->reply_payload_rcv_len = 0;
2030
2031 if (job->request_len < sizeof(struct fc_bsg_request) +
2032 sizeof(struct diag_mode_set)) {
2033 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2034 "3011 Received DIAG MODE request size:%d "
2035 "below the minimum size:%d\n",
2036 job->request_len,
2037 (int)(sizeof(struct fc_bsg_request) +
2038 sizeof(struct diag_mode_set)));
2039 rc = -EINVAL;
2040 goto job_error;
2041 }
2042
James Smart88a2cfb2011-07-22 18:36:33 -04002043 rc = lpfc_bsg_diag_mode_enter(phba);
James Smart7ad20aa2011-05-24 11:44:28 -04002044 if (rc)
2045 goto job_error;
2046
James Smart1b511972011-12-13 13:23:09 -05002047 /* indicate we are in loobpack diagnostic mode */
2048 spin_lock_irq(&phba->hbalock);
2049 phba->link_flag |= LS_LOOPBACK_MODE;
2050 spin_unlock_irq(&phba->hbalock);
2051
2052 /* reset port to start frome scratch */
2053 rc = lpfc_selective_reset(phba);
2054 if (rc)
2055 goto job_error;
2056
James Smart7ad20aa2011-05-24 11:44:28 -04002057 /* bring the link to diagnostic mode */
James Smart1b511972011-12-13 13:23:09 -05002058 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
2059 "3129 Bring link to diagnostic state.\n");
James Smart7ad20aa2011-05-24 11:44:28 -04002060 loopback_mode = (struct diag_mode_set *)
2061 job->request->rqst_data.h_vendor.vendor_cmd;
2062 link_flags = loopback_mode->type;
2063 timeout = loopback_mode->timeout * 100;
2064
2065 rc = lpfc_sli4_bsg_set_link_diag_state(phba, 1);
James Smart1b511972011-12-13 13:23:09 -05002066 if (rc) {
2067 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2068 "3130 Failed to bring link to diagnostic "
2069 "state, rc:x%x\n", rc);
James Smart7ad20aa2011-05-24 11:44:28 -04002070 goto loopback_mode_exit;
James Smart1b511972011-12-13 13:23:09 -05002071 }
James Smart7ad20aa2011-05-24 11:44:28 -04002072
2073 /* wait for link down before proceeding */
2074 i = 0;
2075 while (phba->link_state != LPFC_LINK_DOWN) {
2076 if (i++ > timeout) {
2077 rc = -ETIMEDOUT;
James Smart1b511972011-12-13 13:23:09 -05002078 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
2079 "3131 Timeout waiting for link to "
2080 "diagnostic mode, timeout:%d ms\n",
2081 timeout * 10);
James Smart7ad20aa2011-05-24 11:44:28 -04002082 goto loopback_mode_exit;
2083 }
2084 msleep(10);
2085 }
James Smart7ad20aa2011-05-24 11:44:28 -04002086
James Smart1b511972011-12-13 13:23:09 -05002087 /* set up loopback mode */
2088 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
2089 "3132 Set up loopback mode:x%x\n", link_flags);
2090
2091 if (link_flags == INTERNAL_LOOP_BACK)
2092 rc = lpfc_sli4_bsg_set_internal_loopback(phba);
2093 else if (link_flags == EXTERNAL_LOOP_BACK)
2094 rc = lpfc_hba_init_link_fc_topology(phba,
2095 FLAGS_TOPOLOGY_MODE_PT_PT,
2096 MBX_NOWAIT);
James Smart7ad20aa2011-05-24 11:44:28 -04002097 else {
James Smart1b511972011-12-13 13:23:09 -05002098 rc = -EINVAL;
2099 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
2100 "3141 Loopback mode:x%x not supported\n",
2101 link_flags);
2102 goto loopback_mode_exit;
2103 }
2104
2105 if (!rc) {
James Smart7ad20aa2011-05-24 11:44:28 -04002106 /* wait for the link attention interrupt */
2107 msleep(100);
2108 i = 0;
James Smart1b511972011-12-13 13:23:09 -05002109 while (phba->link_state < LPFC_LINK_UP) {
2110 if (i++ > timeout) {
2111 rc = -ETIMEDOUT;
2112 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
2113 "3137 Timeout waiting for link up "
2114 "in loopback mode, timeout:%d ms\n",
2115 timeout * 10);
2116 break;
2117 }
2118 msleep(10);
2119 }
2120 }
2121
2122 /* port resource registration setup for loopback diagnostic */
2123 if (!rc) {
2124 /* set up a none zero myDID for loopback test */
2125 phba->pport->fc_myDID = 1;
2126 rc = lpfc_sli4_diag_fcport_reg_setup(phba);
2127 } else
2128 goto loopback_mode_exit;
2129
2130 if (!rc) {
2131 /* wait for the port ready */
2132 msleep(100);
2133 i = 0;
James Smart7ad20aa2011-05-24 11:44:28 -04002134 while (phba->link_state != LPFC_HBA_READY) {
2135 if (i++ > timeout) {
2136 rc = -ETIMEDOUT;
James Smart1b511972011-12-13 13:23:09 -05002137 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
2138 "3133 Timeout waiting for port "
2139 "loopback mode ready, timeout:%d ms\n",
2140 timeout * 10);
James Smart7ad20aa2011-05-24 11:44:28 -04002141 break;
2142 }
2143 msleep(10);
2144 }
2145 }
2146
2147loopback_mode_exit:
James Smart1b511972011-12-13 13:23:09 -05002148 /* clear loopback diagnostic mode */
2149 if (rc) {
2150 spin_lock_irq(&phba->hbalock);
2151 phba->link_flag &= ~LS_LOOPBACK_MODE;
2152 spin_unlock_irq(&phba->hbalock);
2153 }
James Smart7ad20aa2011-05-24 11:44:28 -04002154 lpfc_bsg_diag_mode_exit(phba);
2155
James Smart7ad20aa2011-05-24 11:44:28 -04002156job_error:
2157 /* make error code available to userspace */
2158 job->reply->result = rc;
2159 /* complete the job back to userspace if no error */
2160 if (rc == 0)
2161 job->job_done(job);
2162 return rc;
2163}
2164
2165/**
2166 * lpfc_bsg_diag_loopback_mode - bsg vendor command for diag loopback mode
2167 * @job: LPFC_BSG_VENDOR_DIAG_MODE
2168 *
2169 * This function is responsible for responding to check and dispatch bsg diag
2170 * command from the user to proper driver action routines.
2171 */
2172static int
2173lpfc_bsg_diag_loopback_mode(struct fc_bsg_job *job)
2174{
2175 struct Scsi_Host *shost;
2176 struct lpfc_vport *vport;
2177 struct lpfc_hba *phba;
2178 int rc;
2179
2180 shost = job->shost;
2181 if (!shost)
2182 return -ENODEV;
2183 vport = (struct lpfc_vport *)job->shost->hostdata;
2184 if (!vport)
2185 return -ENODEV;
2186 phba = vport->phba;
2187 if (!phba)
2188 return -ENODEV;
2189
2190 if (phba->sli_rev < LPFC_SLI_REV4)
2191 rc = lpfc_sli3_bsg_diag_loopback_mode(phba, job);
2192 else if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
2193 LPFC_SLI_INTF_IF_TYPE_2)
2194 rc = lpfc_sli4_bsg_diag_loopback_mode(phba, job);
2195 else
2196 rc = -ENODEV;
2197
2198 return rc;
James Smart7ad20aa2011-05-24 11:44:28 -04002199}
2200
2201/**
2202 * lpfc_sli4_bsg_diag_mode_end - sli4 bsg vendor command for ending diag mode
2203 * @job: LPFC_BSG_VENDOR_DIAG_MODE_END
2204 *
2205 * This function is responsible for responding to check and dispatch bsg diag
2206 * command from the user to proper driver action routines.
2207 */
2208static int
2209lpfc_sli4_bsg_diag_mode_end(struct fc_bsg_job *job)
2210{
2211 struct Scsi_Host *shost;
2212 struct lpfc_vport *vport;
2213 struct lpfc_hba *phba;
James Smart1b511972011-12-13 13:23:09 -05002214 struct diag_mode_set *loopback_mode_end_cmd;
2215 uint32_t timeout;
2216 int rc, i;
James Smart7ad20aa2011-05-24 11:44:28 -04002217
2218 shost = job->shost;
2219 if (!shost)
2220 return -ENODEV;
2221 vport = (struct lpfc_vport *)job->shost->hostdata;
2222 if (!vport)
2223 return -ENODEV;
2224 phba = vport->phba;
2225 if (!phba)
2226 return -ENODEV;
2227
2228 if (phba->sli_rev < LPFC_SLI_REV4)
2229 return -ENODEV;
2230 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
2231 LPFC_SLI_INTF_IF_TYPE_2)
2232 return -ENODEV;
2233
James Smart1b511972011-12-13 13:23:09 -05002234 /* clear loopback diagnostic mode */
2235 spin_lock_irq(&phba->hbalock);
2236 phba->link_flag &= ~LS_LOOPBACK_MODE;
2237 spin_unlock_irq(&phba->hbalock);
2238 loopback_mode_end_cmd = (struct diag_mode_set *)
2239 job->request->rqst_data.h_vendor.vendor_cmd;
2240 timeout = loopback_mode_end_cmd->timeout * 100;
2241
James Smart7ad20aa2011-05-24 11:44:28 -04002242 rc = lpfc_sli4_bsg_set_link_diag_state(phba, 0);
James Smart1b511972011-12-13 13:23:09 -05002243 if (rc) {
2244 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2245 "3139 Failed to bring link to diagnostic "
2246 "state, rc:x%x\n", rc);
2247 goto loopback_mode_end_exit;
2248 }
James Smart7ad20aa2011-05-24 11:44:28 -04002249
James Smart1b511972011-12-13 13:23:09 -05002250 /* wait for link down before proceeding */
2251 i = 0;
2252 while (phba->link_state != LPFC_LINK_DOWN) {
2253 if (i++ > timeout) {
2254 rc = -ETIMEDOUT;
2255 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
2256 "3140 Timeout waiting for link to "
2257 "diagnostic mode_end, timeout:%d ms\n",
2258 timeout * 10);
2259 /* there is nothing much we can do here */
2260 break;
2261 }
2262 msleep(10);
2263 }
James Smart7ad20aa2011-05-24 11:44:28 -04002264
James Smart1b511972011-12-13 13:23:09 -05002265 /* reset port resource registrations */
2266 rc = lpfc_selective_reset(phba);
2267 phba->pport->fc_myDID = 0;
2268
2269loopback_mode_end_exit:
2270 /* make return code available to userspace */
2271 job->reply->result = rc;
2272 /* complete the job back to userspace if no error */
2273 if (rc == 0)
2274 job->job_done(job);
James Smart7ad20aa2011-05-24 11:44:28 -04002275 return rc;
2276}
2277
2278/**
2279 * lpfc_sli4_bsg_link_diag_test - sli4 bsg vendor command for diag link test
2280 * @job: LPFC_BSG_VENDOR_DIAG_LINK_TEST
2281 *
2282 * This function is to perform SLI4 diag link test request from the user
2283 * applicaiton.
2284 */
2285static int
2286lpfc_sli4_bsg_link_diag_test(struct fc_bsg_job *job)
2287{
2288 struct Scsi_Host *shost;
2289 struct lpfc_vport *vport;
2290 struct lpfc_hba *phba;
2291 LPFC_MBOXQ_t *pmboxq;
2292 struct sli4_link_diag *link_diag_test_cmd;
2293 uint32_t req_len, alloc_len;
2294 uint32_t timeout;
2295 struct lpfc_mbx_run_link_diag_test *run_link_diag_test;
2296 union lpfc_sli4_cfg_shdr *shdr;
2297 uint32_t shdr_status, shdr_add_status;
2298 struct diag_status *diag_status_reply;
2299 int mbxstatus, rc = 0;
2300
2301 shost = job->shost;
2302 if (!shost) {
2303 rc = -ENODEV;
2304 goto job_error;
2305 }
2306 vport = (struct lpfc_vport *)job->shost->hostdata;
2307 if (!vport) {
2308 rc = -ENODEV;
2309 goto job_error;
2310 }
2311 phba = vport->phba;
2312 if (!phba) {
2313 rc = -ENODEV;
2314 goto job_error;
2315 }
2316
2317 if (phba->sli_rev < LPFC_SLI_REV4) {
2318 rc = -ENODEV;
2319 goto job_error;
2320 }
2321 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
2322 LPFC_SLI_INTF_IF_TYPE_2) {
2323 rc = -ENODEV;
2324 goto job_error;
2325 }
2326
2327 if (job->request_len < sizeof(struct fc_bsg_request) +
2328 sizeof(struct sli4_link_diag)) {
2329 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2330 "3013 Received LINK DIAG TEST request "
2331 " size:%d below the minimum size:%d\n",
2332 job->request_len,
2333 (int)(sizeof(struct fc_bsg_request) +
2334 sizeof(struct sli4_link_diag)));
2335 rc = -EINVAL;
2336 goto job_error;
2337 }
2338
James Smart88a2cfb2011-07-22 18:36:33 -04002339 rc = lpfc_bsg_diag_mode_enter(phba);
James Smart7ad20aa2011-05-24 11:44:28 -04002340 if (rc)
2341 goto job_error;
2342
2343 link_diag_test_cmd = (struct sli4_link_diag *)
2344 job->request->rqst_data.h_vendor.vendor_cmd;
2345 timeout = link_diag_test_cmd->timeout * 100;
2346
2347 rc = lpfc_sli4_bsg_set_link_diag_state(phba, 1);
2348
2349 if (rc)
2350 goto job_error;
2351
2352 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2353 if (!pmboxq) {
2354 rc = -ENOMEM;
2355 goto link_diag_test_exit;
2356 }
2357
2358 req_len = (sizeof(struct lpfc_mbx_set_link_diag_state) -
2359 sizeof(struct lpfc_sli4_cfg_mhdr));
2360 alloc_len = lpfc_sli4_config(phba, pmboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
2361 LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_STATE,
2362 req_len, LPFC_SLI4_MBX_EMBED);
2363 if (alloc_len != req_len) {
2364 rc = -ENOMEM;
2365 goto link_diag_test_exit;
2366 }
2367 run_link_diag_test = &pmboxq->u.mqe.un.link_diag_test;
2368 bf_set(lpfc_mbx_run_diag_test_link_num, &run_link_diag_test->u.req,
James Smart1b511972011-12-13 13:23:09 -05002369 phba->sli4_hba.lnk_info.lnk_no);
James Smart7ad20aa2011-05-24 11:44:28 -04002370 bf_set(lpfc_mbx_run_diag_test_link_type, &run_link_diag_test->u.req,
James Smart1b511972011-12-13 13:23:09 -05002371 phba->sli4_hba.lnk_info.lnk_tp);
James Smart7ad20aa2011-05-24 11:44:28 -04002372 bf_set(lpfc_mbx_run_diag_test_test_id, &run_link_diag_test->u.req,
2373 link_diag_test_cmd->test_id);
2374 bf_set(lpfc_mbx_run_diag_test_loops, &run_link_diag_test->u.req,
2375 link_diag_test_cmd->loops);
2376 bf_set(lpfc_mbx_run_diag_test_test_ver, &run_link_diag_test->u.req,
2377 link_diag_test_cmd->test_version);
2378 bf_set(lpfc_mbx_run_diag_test_err_act, &run_link_diag_test->u.req,
2379 link_diag_test_cmd->error_action);
2380
2381 mbxstatus = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
2382
2383 shdr = (union lpfc_sli4_cfg_shdr *)
2384 &pmboxq->u.mqe.un.sli4_config.header.cfg_shdr;
2385 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
2386 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
2387 if (shdr_status || shdr_add_status || mbxstatus) {
2388 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
2389 "3010 Run link diag test mailbox failed with "
2390 "mbx_status x%x status x%x, add_status x%x\n",
2391 mbxstatus, shdr_status, shdr_add_status);
2392 }
2393
2394 diag_status_reply = (struct diag_status *)
2395 job->reply->reply_data.vendor_reply.vendor_rsp;
2396
2397 if (job->reply_len <
2398 sizeof(struct fc_bsg_request) + sizeof(struct diag_status)) {
2399 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2400 "3012 Received Run link diag test reply "
2401 "below minimum size (%d): reply_len:%d\n",
2402 (int)(sizeof(struct fc_bsg_request) +
2403 sizeof(struct diag_status)),
2404 job->reply_len);
2405 rc = -EINVAL;
2406 goto job_error;
2407 }
2408
2409 diag_status_reply->mbox_status = mbxstatus;
2410 diag_status_reply->shdr_status = shdr_status;
2411 diag_status_reply->shdr_add_status = shdr_add_status;
2412
2413link_diag_test_exit:
2414 rc = lpfc_sli4_bsg_set_link_diag_state(phba, 0);
2415
2416 if (pmboxq)
2417 mempool_free(pmboxq, phba->mbox_mem_pool);
2418
2419 lpfc_bsg_diag_mode_exit(phba);
2420
2421job_error:
2422 /* make error code available to userspace */
2423 job->reply->result = rc;
2424 /* complete the job back to userspace if no error */
2425 if (rc == 0)
2426 job->job_done(job);
2427 return rc;
2428}
2429
2430/**
James Smart3b5dd522010-01-26 23:10:15 -05002431 * lpfcdiag_loop_self_reg - obtains a remote port login id
2432 * @phba: Pointer to HBA context object
2433 * @rpi: Pointer to a remote port login id
2434 *
2435 * This function obtains a remote port login id so the diag loopback test
2436 * can send and receive its own unsolicited CT command.
2437 **/
James Smart40426292010-12-15 17:58:10 -05002438static int lpfcdiag_loop_self_reg(struct lpfc_hba *phba, uint16_t *rpi)
James Smart3b5dd522010-01-26 23:10:15 -05002439{
2440 LPFC_MBOXQ_t *mbox;
2441 struct lpfc_dmabuf *dmabuff;
2442 int status;
2443
2444 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2445 if (!mbox)
James Smartd439d282010-09-29 11:18:45 -04002446 return -ENOMEM;
James Smart3b5dd522010-01-26 23:10:15 -05002447
James Smart1b511972011-12-13 13:23:09 -05002448 if (phba->sli_rev < LPFC_SLI_REV4)
2449 status = lpfc_reg_rpi(phba, 0, phba->pport->fc_myDID,
2450 (uint8_t *)&phba->pport->fc_sparam,
2451 mbox, *rpi);
2452 else {
James Smart40426292010-12-15 17:58:10 -05002453 *rpi = lpfc_sli4_alloc_rpi(phba);
James Smart1b511972011-12-13 13:23:09 -05002454 status = lpfc_reg_rpi(phba, phba->pport->vpi,
2455 phba->pport->fc_myDID,
2456 (uint8_t *)&phba->pport->fc_sparam,
2457 mbox, *rpi);
2458 }
2459
James Smart3b5dd522010-01-26 23:10:15 -05002460 if (status) {
2461 mempool_free(mbox, phba->mbox_mem_pool);
James Smart40426292010-12-15 17:58:10 -05002462 if (phba->sli_rev == LPFC_SLI_REV4)
2463 lpfc_sli4_free_rpi(phba, *rpi);
James Smartd439d282010-09-29 11:18:45 -04002464 return -ENOMEM;
James Smart3b5dd522010-01-26 23:10:15 -05002465 }
2466
2467 dmabuff = (struct lpfc_dmabuf *) mbox->context1;
2468 mbox->context1 = NULL;
James Smartd439d282010-09-29 11:18:45 -04002469 mbox->context2 = NULL;
James Smart3b5dd522010-01-26 23:10:15 -05002470 status = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
2471
2472 if ((status != MBX_SUCCESS) || (mbox->u.mb.mbxStatus)) {
2473 lpfc_mbuf_free(phba, dmabuff->virt, dmabuff->phys);
2474 kfree(dmabuff);
2475 if (status != MBX_TIMEOUT)
2476 mempool_free(mbox, phba->mbox_mem_pool);
James Smart40426292010-12-15 17:58:10 -05002477 if (phba->sli_rev == LPFC_SLI_REV4)
2478 lpfc_sli4_free_rpi(phba, *rpi);
James Smartd439d282010-09-29 11:18:45 -04002479 return -ENODEV;
James Smart3b5dd522010-01-26 23:10:15 -05002480 }
2481
James Smart1b511972011-12-13 13:23:09 -05002482 if (phba->sli_rev < LPFC_SLI_REV4)
2483 *rpi = mbox->u.mb.un.varWords[0];
James Smart3b5dd522010-01-26 23:10:15 -05002484
2485 lpfc_mbuf_free(phba, dmabuff->virt, dmabuff->phys);
2486 kfree(dmabuff);
2487 mempool_free(mbox, phba->mbox_mem_pool);
2488 return 0;
2489}
2490
2491/**
2492 * lpfcdiag_loop_self_unreg - unregs from the rpi
2493 * @phba: Pointer to HBA context object
2494 * @rpi: Remote port login id
2495 *
2496 * This function unregisters the rpi obtained in lpfcdiag_loop_self_reg
2497 **/
2498static int lpfcdiag_loop_self_unreg(struct lpfc_hba *phba, uint16_t rpi)
2499{
2500 LPFC_MBOXQ_t *mbox;
2501 int status;
2502
2503 /* Allocate mboxq structure */
2504 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2505 if (mbox == NULL)
James Smartd439d282010-09-29 11:18:45 -04002506 return -ENOMEM;
James Smart3b5dd522010-01-26 23:10:15 -05002507
James Smart1b511972011-12-13 13:23:09 -05002508 if (phba->sli_rev < LPFC_SLI_REV4)
2509 lpfc_unreg_login(phba, 0, rpi, mbox);
2510 else
2511 lpfc_unreg_login(phba, phba->pport->vpi,
2512 phba->sli4_hba.rpi_ids[rpi], mbox);
2513
James Smart3b5dd522010-01-26 23:10:15 -05002514 status = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
2515
2516 if ((status != MBX_SUCCESS) || (mbox->u.mb.mbxStatus)) {
2517 if (status != MBX_TIMEOUT)
2518 mempool_free(mbox, phba->mbox_mem_pool);
James Smartd439d282010-09-29 11:18:45 -04002519 return -EIO;
James Smart3b5dd522010-01-26 23:10:15 -05002520 }
James Smart3b5dd522010-01-26 23:10:15 -05002521 mempool_free(mbox, phba->mbox_mem_pool);
James Smart40426292010-12-15 17:58:10 -05002522 if (phba->sli_rev == LPFC_SLI_REV4)
2523 lpfc_sli4_free_rpi(phba, rpi);
James Smart3b5dd522010-01-26 23:10:15 -05002524 return 0;
2525}
2526
2527/**
2528 * lpfcdiag_loop_get_xri - obtains the transmit and receive ids
2529 * @phba: Pointer to HBA context object
2530 * @rpi: Remote port login id
2531 * @txxri: Pointer to transmit exchange id
2532 * @rxxri: Pointer to response exchabge id
2533 *
2534 * This function obtains the transmit and receive ids required to send
2535 * an unsolicited ct command with a payload. A special lpfc FsType and CmdRsp
2536 * flags are used to the unsolicted response handler is able to process
2537 * the ct command sent on the same port.
2538 **/
2539static int lpfcdiag_loop_get_xri(struct lpfc_hba *phba, uint16_t rpi,
2540 uint16_t *txxri, uint16_t * rxxri)
2541{
2542 struct lpfc_bsg_event *evt;
2543 struct lpfc_iocbq *cmdiocbq, *rspiocbq;
2544 IOCB_t *cmd, *rsp;
2545 struct lpfc_dmabuf *dmabuf;
2546 struct ulp_bde64 *bpl = NULL;
2547 struct lpfc_sli_ct_request *ctreq = NULL;
2548 int ret_val = 0;
James Smartd439d282010-09-29 11:18:45 -04002549 int time_left;
James Smart5a0916b2013-07-15 18:31:42 -04002550 int iocb_stat = IOCB_SUCCESS;
James Smart3b5dd522010-01-26 23:10:15 -05002551 unsigned long flags;
2552
2553 *txxri = 0;
2554 *rxxri = 0;
2555 evt = lpfc_bsg_event_new(FC_REG_CT_EVENT, current->pid,
2556 SLI_CT_ELX_LOOPBACK);
2557 if (!evt)
James Smartd439d282010-09-29 11:18:45 -04002558 return -ENOMEM;
James Smart3b5dd522010-01-26 23:10:15 -05002559
2560 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2561 list_add(&evt->node, &phba->ct_ev_waiters);
2562 lpfc_bsg_event_ref(evt);
2563 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2564
2565 cmdiocbq = lpfc_sli_get_iocbq(phba);
2566 rspiocbq = lpfc_sli_get_iocbq(phba);
2567
2568 dmabuf = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2569 if (dmabuf) {
2570 dmabuf->virt = lpfc_mbuf_alloc(phba, 0, &dmabuf->phys);
James Smartc7495932010-04-06 15:05:28 -04002571 if (dmabuf->virt) {
2572 INIT_LIST_HEAD(&dmabuf->list);
2573 bpl = (struct ulp_bde64 *) dmabuf->virt;
2574 memset(bpl, 0, sizeof(*bpl));
2575 ctreq = (struct lpfc_sli_ct_request *)(bpl + 1);
2576 bpl->addrHigh =
2577 le32_to_cpu(putPaddrHigh(dmabuf->phys +
2578 sizeof(*bpl)));
2579 bpl->addrLow =
2580 le32_to_cpu(putPaddrLow(dmabuf->phys +
2581 sizeof(*bpl)));
2582 bpl->tus.f.bdeFlags = 0;
2583 bpl->tus.f.bdeSize = ELX_LOOPBACK_HEADER_SZ;
2584 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2585 }
James Smart3b5dd522010-01-26 23:10:15 -05002586 }
2587
2588 if (cmdiocbq == NULL || rspiocbq == NULL ||
James Smartc7495932010-04-06 15:05:28 -04002589 dmabuf == NULL || bpl == NULL || ctreq == NULL ||
2590 dmabuf->virt == NULL) {
James Smartd439d282010-09-29 11:18:45 -04002591 ret_val = -ENOMEM;
James Smart3b5dd522010-01-26 23:10:15 -05002592 goto err_get_xri_exit;
2593 }
2594
2595 cmd = &cmdiocbq->iocb;
2596 rsp = &rspiocbq->iocb;
2597
2598 memset(ctreq, 0, ELX_LOOPBACK_HEADER_SZ);
2599
2600 ctreq->RevisionId.bits.Revision = SLI_CT_REVISION;
2601 ctreq->RevisionId.bits.InId = 0;
2602 ctreq->FsType = SLI_CT_ELX_LOOPBACK;
2603 ctreq->FsSubType = 0;
2604 ctreq->CommandResponse.bits.CmdRsp = ELX_LOOPBACK_XRI_SETUP;
2605 ctreq->CommandResponse.bits.Size = 0;
2606
2607
2608 cmd->un.xseq64.bdl.addrHigh = putPaddrHigh(dmabuf->phys);
2609 cmd->un.xseq64.bdl.addrLow = putPaddrLow(dmabuf->phys);
2610 cmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
2611 cmd->un.xseq64.bdl.bdeSize = sizeof(*bpl);
2612
2613 cmd->un.xseq64.w5.hcsw.Fctl = LA;
2614 cmd->un.xseq64.w5.hcsw.Dfctl = 0;
2615 cmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
2616 cmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
2617
2618 cmd->ulpCommand = CMD_XMIT_SEQUENCE64_CR;
2619 cmd->ulpBdeCount = 1;
2620 cmd->ulpLe = 1;
2621 cmd->ulpClass = CLASS3;
2622 cmd->ulpContext = rpi;
2623
2624 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
2625 cmdiocbq->vport = phba->pport;
James Smart5a0916b2013-07-15 18:31:42 -04002626 cmdiocbq->iocb_cmpl = NULL;
James Smart3b5dd522010-01-26 23:10:15 -05002627
James Smartd439d282010-09-29 11:18:45 -04002628 iocb_stat = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq,
James Smart3b5dd522010-01-26 23:10:15 -05002629 rspiocbq,
2630 (phba->fc_ratov * 2)
2631 + LPFC_DRVR_TIMEOUT);
James Smart53151bb2013-10-10 12:24:07 -04002632 if ((iocb_stat != IOCB_SUCCESS) || (rsp->ulpStatus != IOSTAT_SUCCESS)) {
James Smartd439d282010-09-29 11:18:45 -04002633 ret_val = -EIO;
James Smart3b5dd522010-01-26 23:10:15 -05002634 goto err_get_xri_exit;
James Smartd439d282010-09-29 11:18:45 -04002635 }
James Smart3b5dd522010-01-26 23:10:15 -05002636 *txxri = rsp->ulpContext;
2637
2638 evt->waiting = 1;
2639 evt->wait_time_stamp = jiffies;
James Smartd439d282010-09-29 11:18:45 -04002640 time_left = wait_event_interruptible_timeout(
James Smart3b5dd522010-01-26 23:10:15 -05002641 evt->wq, !list_empty(&evt->events_to_see),
James Smart256ec0d2013-04-17 20:14:58 -04002642 msecs_to_jiffies(1000 *
2643 ((phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT)));
James Smart3b5dd522010-01-26 23:10:15 -05002644 if (list_empty(&evt->events_to_see))
James Smartd439d282010-09-29 11:18:45 -04002645 ret_val = (time_left) ? -EINTR : -ETIMEDOUT;
James Smart3b5dd522010-01-26 23:10:15 -05002646 else {
James Smart3b5dd522010-01-26 23:10:15 -05002647 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2648 list_move(evt->events_to_see.prev, &evt->events_to_get);
2649 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2650 *rxxri = (list_entry(evt->events_to_get.prev,
2651 typeof(struct event_data),
2652 node))->immed_dat;
2653 }
2654 evt->waiting = 0;
2655
2656err_get_xri_exit:
2657 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2658 lpfc_bsg_event_unref(evt); /* release ref */
2659 lpfc_bsg_event_unref(evt); /* delete */
2660 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2661
2662 if (dmabuf) {
2663 if (dmabuf->virt)
2664 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
2665 kfree(dmabuf);
2666 }
2667
James Smartd439d282010-09-29 11:18:45 -04002668 if (cmdiocbq && (iocb_stat != IOCB_TIMEDOUT))
James Smart3b5dd522010-01-26 23:10:15 -05002669 lpfc_sli_release_iocbq(phba, cmdiocbq);
2670 if (rspiocbq)
2671 lpfc_sli_release_iocbq(phba, rspiocbq);
2672 return ret_val;
2673}
2674
2675/**
James Smart7ad20aa2011-05-24 11:44:28 -04002676 * lpfc_bsg_dma_page_alloc - allocate a bsg mbox page sized dma buffers
2677 * @phba: Pointer to HBA context object
2678 *
2679 * This function allocates BSG_MBOX_SIZE (4KB) page size dma buffer and.
Joe Perches9e03aa22013-09-03 13:45:58 -07002680 * returns the pointer to the buffer.
James Smart7ad20aa2011-05-24 11:44:28 -04002681 **/
2682static struct lpfc_dmabuf *
2683lpfc_bsg_dma_page_alloc(struct lpfc_hba *phba)
2684{
2685 struct lpfc_dmabuf *dmabuf;
2686 struct pci_dev *pcidev = phba->pcidev;
2687
2688 /* allocate dma buffer struct */
2689 dmabuf = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2690 if (!dmabuf)
2691 return NULL;
2692
2693 INIT_LIST_HEAD(&dmabuf->list);
2694
2695 /* now, allocate dma buffer */
Joe Perches1aee3832014-09-03 12:56:12 -04002696 dmabuf->virt = dma_zalloc_coherent(&pcidev->dev, BSG_MBOX_SIZE,
2697 &(dmabuf->phys), GFP_KERNEL);
James Smart7ad20aa2011-05-24 11:44:28 -04002698
2699 if (!dmabuf->virt) {
2700 kfree(dmabuf);
2701 return NULL;
2702 }
James Smart7ad20aa2011-05-24 11:44:28 -04002703
2704 return dmabuf;
2705}
2706
2707/**
2708 * lpfc_bsg_dma_page_free - free a bsg mbox page sized dma buffer
2709 * @phba: Pointer to HBA context object.
2710 * @dmabuf: Pointer to the bsg mbox page sized dma buffer descriptor.
2711 *
2712 * This routine just simply frees a dma buffer and its associated buffer
2713 * descriptor referred by @dmabuf.
2714 **/
2715static void
2716lpfc_bsg_dma_page_free(struct lpfc_hba *phba, struct lpfc_dmabuf *dmabuf)
2717{
2718 struct pci_dev *pcidev = phba->pcidev;
2719
2720 if (!dmabuf)
2721 return;
2722
2723 if (dmabuf->virt)
2724 dma_free_coherent(&pcidev->dev, BSG_MBOX_SIZE,
2725 dmabuf->virt, dmabuf->phys);
2726 kfree(dmabuf);
2727 return;
2728}
2729
2730/**
2731 * lpfc_bsg_dma_page_list_free - free a list of bsg mbox page sized dma buffers
2732 * @phba: Pointer to HBA context object.
2733 * @dmabuf_list: Pointer to a list of bsg mbox page sized dma buffer descs.
2734 *
2735 * This routine just simply frees all dma buffers and their associated buffer
2736 * descriptors referred by @dmabuf_list.
2737 **/
2738static void
2739lpfc_bsg_dma_page_list_free(struct lpfc_hba *phba,
2740 struct list_head *dmabuf_list)
2741{
2742 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
2743
2744 if (list_empty(dmabuf_list))
2745 return;
2746
2747 list_for_each_entry_safe(dmabuf, next_dmabuf, dmabuf_list, list) {
2748 list_del_init(&dmabuf->list);
2749 lpfc_bsg_dma_page_free(phba, dmabuf);
2750 }
2751 return;
2752}
2753
2754/**
James Smart3b5dd522010-01-26 23:10:15 -05002755 * diag_cmd_data_alloc - fills in a bde struct with dma buffers
2756 * @phba: Pointer to HBA context object
2757 * @bpl: Pointer to 64 bit bde structure
2758 * @size: Number of bytes to process
2759 * @nocopydata: Flag to copy user data into the allocated buffer
2760 *
2761 * This function allocates page size buffers and populates an lpfc_dmabufext.
2762 * If allowed the user data pointed to with indataptr is copied into the kernel
2763 * memory. The chained list of page size buffers is returned.
2764 **/
2765static struct lpfc_dmabufext *
2766diag_cmd_data_alloc(struct lpfc_hba *phba,
2767 struct ulp_bde64 *bpl, uint32_t size,
2768 int nocopydata)
2769{
2770 struct lpfc_dmabufext *mlist = NULL;
2771 struct lpfc_dmabufext *dmp;
2772 int cnt, offset = 0, i = 0;
2773 struct pci_dev *pcidev;
2774
2775 pcidev = phba->pcidev;
2776
2777 while (size) {
2778 /* We get chunks of 4K */
2779 if (size > BUF_SZ_4K)
2780 cnt = BUF_SZ_4K;
2781 else
2782 cnt = size;
2783
2784 /* allocate struct lpfc_dmabufext buffer header */
2785 dmp = kmalloc(sizeof(struct lpfc_dmabufext), GFP_KERNEL);
2786 if (!dmp)
2787 goto out;
2788
2789 INIT_LIST_HEAD(&dmp->dma.list);
2790
2791 /* Queue it to a linked list */
2792 if (mlist)
2793 list_add_tail(&dmp->dma.list, &mlist->dma.list);
2794 else
2795 mlist = dmp;
2796
2797 /* allocate buffer */
2798 dmp->dma.virt = dma_alloc_coherent(&pcidev->dev,
2799 cnt,
2800 &(dmp->dma.phys),
2801 GFP_KERNEL);
2802
2803 if (!dmp->dma.virt)
2804 goto out;
2805
2806 dmp->size = cnt;
2807
2808 if (nocopydata) {
2809 bpl->tus.f.bdeFlags = 0;
2810 pci_dma_sync_single_for_device(phba->pcidev,
2811 dmp->dma.phys, LPFC_BPL_SIZE, PCI_DMA_TODEVICE);
2812
2813 } else {
2814 memset((uint8_t *)dmp->dma.virt, 0, cnt);
2815 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2816 }
2817
2818 /* build buffer ptr list for IOCB */
2819 bpl->addrLow = le32_to_cpu(putPaddrLow(dmp->dma.phys));
2820 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dmp->dma.phys));
2821 bpl->tus.f.bdeSize = (ushort) cnt;
2822 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2823 bpl++;
2824
2825 i++;
2826 offset += cnt;
2827 size -= cnt;
2828 }
2829
James Smarta2fc4aef2014-09-03 12:57:55 -04002830 if (mlist) {
2831 mlist->flag = i;
2832 return mlist;
2833 }
James Smart3b5dd522010-01-26 23:10:15 -05002834out:
2835 diag_cmd_data_free(phba, mlist);
2836 return NULL;
2837}
2838
2839/**
2840 * lpfcdiag_loop_post_rxbufs - post the receive buffers for an unsol CT cmd
2841 * @phba: Pointer to HBA context object
2842 * @rxxri: Receive exchange id
2843 * @len: Number of data bytes
2844 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002845 * This function allocates and posts a data buffer of sufficient size to receive
James Smart3b5dd522010-01-26 23:10:15 -05002846 * an unsolicted CT command.
2847 **/
2848static int lpfcdiag_loop_post_rxbufs(struct lpfc_hba *phba, uint16_t rxxri,
2849 size_t len)
2850{
2851 struct lpfc_sli *psli = &phba->sli;
2852 struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
2853 struct lpfc_iocbq *cmdiocbq;
2854 IOCB_t *cmd = NULL;
2855 struct list_head head, *curr, *next;
2856 struct lpfc_dmabuf *rxbmp;
2857 struct lpfc_dmabuf *dmp;
2858 struct lpfc_dmabuf *mp[2] = {NULL, NULL};
2859 struct ulp_bde64 *rxbpl = NULL;
2860 uint32_t num_bde;
2861 struct lpfc_dmabufext *rxbuffer = NULL;
2862 int ret_val = 0;
James Smartd439d282010-09-29 11:18:45 -04002863 int iocb_stat;
James Smart3b5dd522010-01-26 23:10:15 -05002864 int i = 0;
2865
2866 cmdiocbq = lpfc_sli_get_iocbq(phba);
2867 rxbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2868 if (rxbmp != NULL) {
2869 rxbmp->virt = lpfc_mbuf_alloc(phba, 0, &rxbmp->phys);
James Smartc7495932010-04-06 15:05:28 -04002870 if (rxbmp->virt) {
2871 INIT_LIST_HEAD(&rxbmp->list);
2872 rxbpl = (struct ulp_bde64 *) rxbmp->virt;
2873 rxbuffer = diag_cmd_data_alloc(phba, rxbpl, len, 0);
2874 }
James Smart3b5dd522010-01-26 23:10:15 -05002875 }
2876
2877 if (!cmdiocbq || !rxbmp || !rxbpl || !rxbuffer) {
James Smartd439d282010-09-29 11:18:45 -04002878 ret_val = -ENOMEM;
James Smart3b5dd522010-01-26 23:10:15 -05002879 goto err_post_rxbufs_exit;
2880 }
2881
2882 /* Queue buffers for the receive exchange */
2883 num_bde = (uint32_t)rxbuffer->flag;
2884 dmp = &rxbuffer->dma;
2885
2886 cmd = &cmdiocbq->iocb;
2887 i = 0;
2888
2889 INIT_LIST_HEAD(&head);
2890 list_add_tail(&head, &dmp->list);
2891 list_for_each_safe(curr, next, &head) {
2892 mp[i] = list_entry(curr, struct lpfc_dmabuf, list);
2893 list_del(curr);
2894
2895 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
2896 mp[i]->buffer_tag = lpfc_sli_get_buffer_tag(phba);
2897 cmd->un.quexri64cx.buff.bde.addrHigh =
2898 putPaddrHigh(mp[i]->phys);
2899 cmd->un.quexri64cx.buff.bde.addrLow =
2900 putPaddrLow(mp[i]->phys);
2901 cmd->un.quexri64cx.buff.bde.tus.f.bdeSize =
2902 ((struct lpfc_dmabufext *)mp[i])->size;
2903 cmd->un.quexri64cx.buff.buffer_tag = mp[i]->buffer_tag;
2904 cmd->ulpCommand = CMD_QUE_XRI64_CX;
2905 cmd->ulpPU = 0;
2906 cmd->ulpLe = 1;
2907 cmd->ulpBdeCount = 1;
2908 cmd->unsli3.que_xri64cx_ext_words.ebde_count = 0;
2909
2910 } else {
2911 cmd->un.cont64[i].addrHigh = putPaddrHigh(mp[i]->phys);
2912 cmd->un.cont64[i].addrLow = putPaddrLow(mp[i]->phys);
2913 cmd->un.cont64[i].tus.f.bdeSize =
2914 ((struct lpfc_dmabufext *)mp[i])->size;
2915 cmd->ulpBdeCount = ++i;
2916
2917 if ((--num_bde > 0) && (i < 2))
2918 continue;
2919
2920 cmd->ulpCommand = CMD_QUE_XRI_BUF64_CX;
2921 cmd->ulpLe = 1;
2922 }
2923
2924 cmd->ulpClass = CLASS3;
2925 cmd->ulpContext = rxxri;
2926
James Smartd439d282010-09-29 11:18:45 -04002927 iocb_stat = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq,
2928 0);
2929 if (iocb_stat == IOCB_ERROR) {
James Smart3b5dd522010-01-26 23:10:15 -05002930 diag_cmd_data_free(phba,
2931 (struct lpfc_dmabufext *)mp[0]);
2932 if (mp[1])
2933 diag_cmd_data_free(phba,
2934 (struct lpfc_dmabufext *)mp[1]);
2935 dmp = list_entry(next, struct lpfc_dmabuf, list);
James Smartd439d282010-09-29 11:18:45 -04002936 ret_val = -EIO;
James Smart3b5dd522010-01-26 23:10:15 -05002937 goto err_post_rxbufs_exit;
2938 }
2939
2940 lpfc_sli_ringpostbuf_put(phba, pring, mp[0]);
2941 if (mp[1]) {
2942 lpfc_sli_ringpostbuf_put(phba, pring, mp[1]);
2943 mp[1] = NULL;
2944 }
2945
2946 /* The iocb was freed by lpfc_sli_issue_iocb */
2947 cmdiocbq = lpfc_sli_get_iocbq(phba);
2948 if (!cmdiocbq) {
2949 dmp = list_entry(next, struct lpfc_dmabuf, list);
James Smartd439d282010-09-29 11:18:45 -04002950 ret_val = -EIO;
James Smart3b5dd522010-01-26 23:10:15 -05002951 goto err_post_rxbufs_exit;
2952 }
2953
2954 cmd = &cmdiocbq->iocb;
2955 i = 0;
2956 }
2957 list_del(&head);
2958
2959err_post_rxbufs_exit:
2960
2961 if (rxbmp) {
2962 if (rxbmp->virt)
2963 lpfc_mbuf_free(phba, rxbmp->virt, rxbmp->phys);
2964 kfree(rxbmp);
2965 }
2966
2967 if (cmdiocbq)
2968 lpfc_sli_release_iocbq(phba, cmdiocbq);
2969 return ret_val;
2970}
2971
2972/**
James Smart7ad20aa2011-05-24 11:44:28 -04002973 * lpfc_bsg_diag_loopback_run - run loopback on a port by issue ct cmd to itself
James Smart3b5dd522010-01-26 23:10:15 -05002974 * @job: LPFC_BSG_VENDOR_DIAG_TEST fc_bsg_job
2975 *
2976 * This function receives a user data buffer to be transmitted and received on
2977 * the same port, the link must be up and in loopback mode prior
2978 * to being called.
2979 * 1. A kernel buffer is allocated to copy the user data into.
2980 * 2. The port registers with "itself".
2981 * 3. The transmit and receive exchange ids are obtained.
2982 * 4. The receive exchange id is posted.
2983 * 5. A new els loopback event is created.
2984 * 6. The command and response iocbs are allocated.
2985 * 7. The cmd iocb FsType is set to elx loopback and the CmdRsp to looppback.
2986 *
2987 * This function is meant to be called n times while the port is in loopback
2988 * so it is the apps responsibility to issue a reset to take the port out
2989 * of loopback mode.
2990 **/
2991static int
James Smart7ad20aa2011-05-24 11:44:28 -04002992lpfc_bsg_diag_loopback_run(struct fc_bsg_job *job)
James Smart3b5dd522010-01-26 23:10:15 -05002993{
2994 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
2995 struct lpfc_hba *phba = vport->phba;
2996 struct diag_mode_test *diag_mode;
2997 struct lpfc_bsg_event *evt;
2998 struct event_data *evdat;
2999 struct lpfc_sli *psli = &phba->sli;
3000 uint32_t size;
3001 uint32_t full_size;
3002 size_t segment_len = 0, segment_offset = 0, current_offset = 0;
James Smart40426292010-12-15 17:58:10 -05003003 uint16_t rpi = 0;
James Smart1b511972011-12-13 13:23:09 -05003004 struct lpfc_iocbq *cmdiocbq, *rspiocbq = NULL;
3005 IOCB_t *cmd, *rsp = NULL;
James Smart3b5dd522010-01-26 23:10:15 -05003006 struct lpfc_sli_ct_request *ctreq;
3007 struct lpfc_dmabuf *txbmp;
3008 struct ulp_bde64 *txbpl = NULL;
3009 struct lpfc_dmabufext *txbuffer = NULL;
3010 struct list_head head;
3011 struct lpfc_dmabuf *curr;
James Smart1b511972011-12-13 13:23:09 -05003012 uint16_t txxri = 0, rxxri;
James Smart3b5dd522010-01-26 23:10:15 -05003013 uint32_t num_bde;
3014 uint8_t *ptr = NULL, *rx_databuf = NULL;
3015 int rc = 0;
James Smartd439d282010-09-29 11:18:45 -04003016 int time_left;
James Smart5a0916b2013-07-15 18:31:42 -04003017 int iocb_stat = IOCB_SUCCESS;
James Smart3b5dd522010-01-26 23:10:15 -05003018 unsigned long flags;
3019 void *dataout = NULL;
3020 uint32_t total_mem;
3021
3022 /* in case no data is returned return just the return code */
3023 job->reply->reply_payload_rcv_len = 0;
3024
3025 if (job->request_len <
3026 sizeof(struct fc_bsg_request) + sizeof(struct diag_mode_test)) {
3027 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3028 "2739 Received DIAG TEST request below minimum "
3029 "size\n");
3030 rc = -EINVAL;
3031 goto loopback_test_exit;
3032 }
3033
3034 if (job->request_payload.payload_len !=
3035 job->reply_payload.payload_len) {
3036 rc = -EINVAL;
3037 goto loopback_test_exit;
3038 }
James Smart3b5dd522010-01-26 23:10:15 -05003039 diag_mode = (struct diag_mode_test *)
3040 job->request->rqst_data.h_vendor.vendor_cmd;
3041
3042 if ((phba->link_state == LPFC_HBA_ERROR) ||
3043 (psli->sli_flag & LPFC_BLOCK_MGMT_IO) ||
3044 (!(psli->sli_flag & LPFC_SLI_ACTIVE))) {
3045 rc = -EACCES;
3046 goto loopback_test_exit;
3047 }
3048
3049 if (!lpfc_is_link_up(phba) || !(phba->link_flag & LS_LOOPBACK_MODE)) {
3050 rc = -EACCES;
3051 goto loopback_test_exit;
3052 }
3053
3054 size = job->request_payload.payload_len;
3055 full_size = size + ELX_LOOPBACK_HEADER_SZ; /* plus the header */
3056
3057 if ((size == 0) || (size > 80 * BUF_SZ_4K)) {
3058 rc = -ERANGE;
3059 goto loopback_test_exit;
3060 }
3061
James Smart63e801c2010-11-20 23:14:19 -05003062 if (full_size >= BUF_SZ_4K) {
James Smart3b5dd522010-01-26 23:10:15 -05003063 /*
3064 * Allocate memory for ioctl data. If buffer is bigger than 64k,
3065 * then we allocate 64k and re-use that buffer over and over to
3066 * xfer the whole block. This is because Linux kernel has a
3067 * problem allocating more than 120k of kernel space memory. Saw
3068 * problem with GET_FCPTARGETMAPPING...
3069 */
3070 if (size <= (64 * 1024))
James Smart63e801c2010-11-20 23:14:19 -05003071 total_mem = full_size;
James Smart3b5dd522010-01-26 23:10:15 -05003072 else
3073 total_mem = 64 * 1024;
3074 } else
3075 /* Allocate memory for ioctl data */
3076 total_mem = BUF_SZ_4K;
3077
3078 dataout = kmalloc(total_mem, GFP_KERNEL);
3079 if (dataout == NULL) {
3080 rc = -ENOMEM;
3081 goto loopback_test_exit;
3082 }
3083
3084 ptr = dataout;
3085 ptr += ELX_LOOPBACK_HEADER_SZ;
3086 sg_copy_to_buffer(job->request_payload.sg_list,
3087 job->request_payload.sg_cnt,
3088 ptr, size);
James Smart3b5dd522010-01-26 23:10:15 -05003089 rc = lpfcdiag_loop_self_reg(phba, &rpi);
James Smartd439d282010-09-29 11:18:45 -04003090 if (rc)
James Smart3b5dd522010-01-26 23:10:15 -05003091 goto loopback_test_exit;
James Smart3b5dd522010-01-26 23:10:15 -05003092
James Smart1b511972011-12-13 13:23:09 -05003093 if (phba->sli_rev < LPFC_SLI_REV4) {
3094 rc = lpfcdiag_loop_get_xri(phba, rpi, &txxri, &rxxri);
3095 if (rc) {
3096 lpfcdiag_loop_self_unreg(phba, rpi);
3097 goto loopback_test_exit;
3098 }
James Smart3b5dd522010-01-26 23:10:15 -05003099
James Smart1b511972011-12-13 13:23:09 -05003100 rc = lpfcdiag_loop_post_rxbufs(phba, rxxri, full_size);
3101 if (rc) {
3102 lpfcdiag_loop_self_unreg(phba, rpi);
3103 goto loopback_test_exit;
3104 }
James Smart3b5dd522010-01-26 23:10:15 -05003105 }
James Smart3b5dd522010-01-26 23:10:15 -05003106 evt = lpfc_bsg_event_new(FC_REG_CT_EVENT, current->pid,
3107 SLI_CT_ELX_LOOPBACK);
3108 if (!evt) {
3109 lpfcdiag_loop_self_unreg(phba, rpi);
3110 rc = -ENOMEM;
3111 goto loopback_test_exit;
3112 }
3113
3114 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3115 list_add(&evt->node, &phba->ct_ev_waiters);
3116 lpfc_bsg_event_ref(evt);
3117 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3118
3119 cmdiocbq = lpfc_sli_get_iocbq(phba);
James Smart1b511972011-12-13 13:23:09 -05003120 if (phba->sli_rev < LPFC_SLI_REV4)
3121 rspiocbq = lpfc_sli_get_iocbq(phba);
James Smart3b5dd522010-01-26 23:10:15 -05003122 txbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3123
3124 if (txbmp) {
3125 txbmp->virt = lpfc_mbuf_alloc(phba, 0, &txbmp->phys);
James Smartc7495932010-04-06 15:05:28 -04003126 if (txbmp->virt) {
3127 INIT_LIST_HEAD(&txbmp->list);
3128 txbpl = (struct ulp_bde64 *) txbmp->virt;
James Smart3b5dd522010-01-26 23:10:15 -05003129 txbuffer = diag_cmd_data_alloc(phba,
3130 txbpl, full_size, 0);
James Smartc7495932010-04-06 15:05:28 -04003131 }
James Smart3b5dd522010-01-26 23:10:15 -05003132 }
3133
James Smart1b511972011-12-13 13:23:09 -05003134 if (!cmdiocbq || !txbmp || !txbpl || !txbuffer || !txbmp->virt) {
3135 rc = -ENOMEM;
3136 goto err_loopback_test_exit;
3137 }
3138 if ((phba->sli_rev < LPFC_SLI_REV4) && !rspiocbq) {
James Smart3b5dd522010-01-26 23:10:15 -05003139 rc = -ENOMEM;
3140 goto err_loopback_test_exit;
3141 }
3142
3143 cmd = &cmdiocbq->iocb;
James Smart1b511972011-12-13 13:23:09 -05003144 if (phba->sli_rev < LPFC_SLI_REV4)
3145 rsp = &rspiocbq->iocb;
James Smart3b5dd522010-01-26 23:10:15 -05003146
3147 INIT_LIST_HEAD(&head);
3148 list_add_tail(&head, &txbuffer->dma.list);
3149 list_for_each_entry(curr, &head, list) {
3150 segment_len = ((struct lpfc_dmabufext *)curr)->size;
3151 if (current_offset == 0) {
3152 ctreq = curr->virt;
3153 memset(ctreq, 0, ELX_LOOPBACK_HEADER_SZ);
3154 ctreq->RevisionId.bits.Revision = SLI_CT_REVISION;
3155 ctreq->RevisionId.bits.InId = 0;
3156 ctreq->FsType = SLI_CT_ELX_LOOPBACK;
3157 ctreq->FsSubType = 0;
3158 ctreq->CommandResponse.bits.CmdRsp = ELX_LOOPBACK_DATA;
3159 ctreq->CommandResponse.bits.Size = size;
3160 segment_offset = ELX_LOOPBACK_HEADER_SZ;
3161 } else
3162 segment_offset = 0;
3163
3164 BUG_ON(segment_offset >= segment_len);
3165 memcpy(curr->virt + segment_offset,
3166 ptr + current_offset,
3167 segment_len - segment_offset);
3168
3169 current_offset += segment_len - segment_offset;
3170 BUG_ON(current_offset > size);
3171 }
3172 list_del(&head);
3173
3174 /* Build the XMIT_SEQUENCE iocb */
James Smart3b5dd522010-01-26 23:10:15 -05003175 num_bde = (uint32_t)txbuffer->flag;
3176
3177 cmd->un.xseq64.bdl.addrHigh = putPaddrHigh(txbmp->phys);
3178 cmd->un.xseq64.bdl.addrLow = putPaddrLow(txbmp->phys);
3179 cmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
3180 cmd->un.xseq64.bdl.bdeSize = (num_bde * sizeof(struct ulp_bde64));
3181
3182 cmd->un.xseq64.w5.hcsw.Fctl = (LS | LA);
3183 cmd->un.xseq64.w5.hcsw.Dfctl = 0;
3184 cmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
3185 cmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
3186
3187 cmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;
3188 cmd->ulpBdeCount = 1;
3189 cmd->ulpLe = 1;
3190 cmd->ulpClass = CLASS3;
James Smart3b5dd522010-01-26 23:10:15 -05003191
James Smart1b511972011-12-13 13:23:09 -05003192 if (phba->sli_rev < LPFC_SLI_REV4) {
3193 cmd->ulpContext = txxri;
3194 } else {
3195 cmd->un.xseq64.bdl.ulpIoTag32 = 0;
3196 cmd->un.ulpWord[3] = phba->sli4_hba.rpi_ids[rpi];
3197 cmdiocbq->context3 = txbmp;
3198 cmdiocbq->sli4_xritag = NO_XRI;
3199 cmd->unsli3.rcvsli3.ox_id = 0xffff;
3200 }
James Smart3b5dd522010-01-26 23:10:15 -05003201 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
3202 cmdiocbq->vport = phba->pport;
James Smart5a0916b2013-07-15 18:31:42 -04003203 cmdiocbq->iocb_cmpl = NULL;
James Smartd439d282010-09-29 11:18:45 -04003204 iocb_stat = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq,
3205 rspiocbq, (phba->fc_ratov * 2) +
3206 LPFC_DRVR_TIMEOUT);
James Smart3b5dd522010-01-26 23:10:15 -05003207
James Smart53151bb2013-10-10 12:24:07 -04003208 if ((iocb_stat != IOCB_SUCCESS) ||
3209 ((phba->sli_rev < LPFC_SLI_REV4) &&
3210 (rsp->ulpStatus != IOSTAT_SUCCESS))) {
James Smart1b511972011-12-13 13:23:09 -05003211 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3212 "3126 Failed loopback test issue iocb: "
3213 "iocb_stat:x%x\n", iocb_stat);
James Smart3b5dd522010-01-26 23:10:15 -05003214 rc = -EIO;
3215 goto err_loopback_test_exit;
3216 }
3217
3218 evt->waiting = 1;
James Smartd439d282010-09-29 11:18:45 -04003219 time_left = wait_event_interruptible_timeout(
James Smart3b5dd522010-01-26 23:10:15 -05003220 evt->wq, !list_empty(&evt->events_to_see),
James Smart256ec0d2013-04-17 20:14:58 -04003221 msecs_to_jiffies(1000 *
3222 ((phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT)));
James Smart3b5dd522010-01-26 23:10:15 -05003223 evt->waiting = 0;
James Smart1b511972011-12-13 13:23:09 -05003224 if (list_empty(&evt->events_to_see)) {
James Smartd439d282010-09-29 11:18:45 -04003225 rc = (time_left) ? -EINTR : -ETIMEDOUT;
James Smart1b511972011-12-13 13:23:09 -05003226 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3227 "3125 Not receiving unsolicited event, "
3228 "rc:x%x\n", rc);
3229 } else {
James Smart3b5dd522010-01-26 23:10:15 -05003230 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3231 list_move(evt->events_to_see.prev, &evt->events_to_get);
3232 evdat = list_entry(evt->events_to_get.prev,
3233 typeof(*evdat), node);
3234 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3235 rx_databuf = evdat->data;
3236 if (evdat->len != full_size) {
3237 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3238 "1603 Loopback test did not receive expected "
3239 "data length. actual length 0x%x expected "
3240 "length 0x%x\n",
3241 evdat->len, full_size);
3242 rc = -EIO;
3243 } else if (rx_databuf == NULL)
3244 rc = -EIO;
3245 else {
3246 rc = IOCB_SUCCESS;
3247 /* skip over elx loopback header */
3248 rx_databuf += ELX_LOOPBACK_HEADER_SZ;
3249 job->reply->reply_payload_rcv_len =
3250 sg_copy_from_buffer(job->reply_payload.sg_list,
3251 job->reply_payload.sg_cnt,
3252 rx_databuf, size);
3253 job->reply->reply_payload_rcv_len = size;
3254 }
3255 }
3256
3257err_loopback_test_exit:
3258 lpfcdiag_loop_self_unreg(phba, rpi);
3259
3260 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3261 lpfc_bsg_event_unref(evt); /* release ref */
3262 lpfc_bsg_event_unref(evt); /* delete */
3263 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3264
James Smart5a0916b2013-07-15 18:31:42 -04003265 if ((cmdiocbq != NULL) && (iocb_stat != IOCB_TIMEDOUT))
James Smart3b5dd522010-01-26 23:10:15 -05003266 lpfc_sli_release_iocbq(phba, cmdiocbq);
3267
3268 if (rspiocbq != NULL)
3269 lpfc_sli_release_iocbq(phba, rspiocbq);
3270
3271 if (txbmp != NULL) {
3272 if (txbpl != NULL) {
3273 if (txbuffer != NULL)
3274 diag_cmd_data_free(phba, txbuffer);
3275 lpfc_mbuf_free(phba, txbmp->virt, txbmp->phys);
3276 }
3277 kfree(txbmp);
3278 }
3279
3280loopback_test_exit:
3281 kfree(dataout);
3282 /* make error code available to userspace */
3283 job->reply->result = rc;
3284 job->dd_data = NULL;
3285 /* complete the job back to userspace if no error */
James Smart1b511972011-12-13 13:23:09 -05003286 if (rc == IOCB_SUCCESS)
James Smart3b5dd522010-01-26 23:10:15 -05003287 job->job_done(job);
3288 return rc;
3289}
3290
3291/**
3292 * lpfc_bsg_get_dfc_rev - process a GET_DFC_REV bsg vendor command
3293 * @job: GET_DFC_REV fc_bsg_job
3294 **/
3295static int
3296lpfc_bsg_get_dfc_rev(struct fc_bsg_job *job)
3297{
3298 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
3299 struct lpfc_hba *phba = vport->phba;
3300 struct get_mgmt_rev *event_req;
3301 struct get_mgmt_rev_reply *event_reply;
3302 int rc = 0;
3303
3304 if (job->request_len <
3305 sizeof(struct fc_bsg_request) + sizeof(struct get_mgmt_rev)) {
3306 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3307 "2740 Received GET_DFC_REV request below "
3308 "minimum size\n");
3309 rc = -EINVAL;
3310 goto job_error;
3311 }
3312
3313 event_req = (struct get_mgmt_rev *)
3314 job->request->rqst_data.h_vendor.vendor_cmd;
3315
3316 event_reply = (struct get_mgmt_rev_reply *)
3317 job->reply->reply_data.vendor_reply.vendor_rsp;
3318
3319 if (job->reply_len <
3320 sizeof(struct fc_bsg_request) + sizeof(struct get_mgmt_rev_reply)) {
3321 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3322 "2741 Received GET_DFC_REV reply below "
3323 "minimum size\n");
3324 rc = -EINVAL;
3325 goto job_error;
3326 }
3327
3328 event_reply->info.a_Major = MANAGEMENT_MAJOR_REV;
3329 event_reply->info.a_Minor = MANAGEMENT_MINOR_REV;
3330job_error:
3331 job->reply->result = rc;
3332 if (rc == 0)
3333 job->job_done(job);
3334 return rc;
3335}
3336
3337/**
James Smart7ad20aa2011-05-24 11:44:28 -04003338 * lpfc_bsg_issue_mbox_cmpl - lpfc_bsg_issue_mbox mbox completion handler
James Smart3b5dd522010-01-26 23:10:15 -05003339 * @phba: Pointer to HBA context object.
3340 * @pmboxq: Pointer to mailbox command.
3341 *
3342 * This is completion handler function for mailbox commands issued from
3343 * lpfc_bsg_issue_mbox function. This function is called by the
3344 * mailbox event handler function with no lock held. This function
3345 * will wake up thread waiting on the wait queue pointed by context1
3346 * of the mailbox.
3347 **/
Rashika Kheria9ab9b132014-09-03 12:55:46 -04003348static void
James Smart7ad20aa2011-05-24 11:44:28 -04003349lpfc_bsg_issue_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
James Smart3b5dd522010-01-26 23:10:15 -05003350{
3351 struct bsg_job_data *dd_data;
James Smart3b5dd522010-01-26 23:10:15 -05003352 struct fc_bsg_job *job;
3353 uint32_t size;
3354 unsigned long flags;
James Smart7ad20aa2011-05-24 11:44:28 -04003355 uint8_t *pmb, *pmb_buf;
James Smart3b5dd522010-01-26 23:10:15 -05003356
James Smart3b5dd522010-01-26 23:10:15 -05003357 dd_data = pmboxq->context1;
James Smart3b5dd522010-01-26 23:10:15 -05003358
James Smart7ad20aa2011-05-24 11:44:28 -04003359 /*
3360 * The outgoing buffer is readily referred from the dma buffer,
3361 * just need to get header part from mailboxq structure.
James Smart7a470272010-03-15 11:25:20 -04003362 */
James Smart7ad20aa2011-05-24 11:44:28 -04003363 pmb = (uint8_t *)&pmboxq->u.mb;
3364 pmb_buf = (uint8_t *)dd_data->context_un.mbox.mb;
3365 memcpy(pmb_buf, pmb, sizeof(MAILBOX_t));
James Smart515e0aa2010-09-29 11:19:00 -04003366
James Smarta33c4f72013-03-01 16:36:00 -05003367 /* Determine if job has been aborted */
3368
3369 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3370 job = dd_data->set_job;
3371 if (job) {
3372 /* Prevent timeout handling from trying to abort job */
3373 job->dd_data = NULL;
3374 }
3375 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3376
3377 /* Copy the mailbox data to the job if it is still active */
3378
James Smart5a6f1332011-03-11 16:05:35 -05003379 if (job) {
3380 size = job->reply_payload.payload_len;
3381 job->reply->reply_payload_rcv_len =
3382 sg_copy_from_buffer(job->reply_payload.sg_list,
James Smart7ad20aa2011-05-24 11:44:28 -04003383 job->reply_payload.sg_cnt,
3384 pmb_buf, size);
James Smart5a6f1332011-03-11 16:05:35 -05003385 }
James Smart7a470272010-03-15 11:25:20 -04003386
James Smarta33c4f72013-03-01 16:36:00 -05003387 dd_data->set_job = NULL;
James Smart3b5dd522010-01-26 23:10:15 -05003388 mempool_free(dd_data->context_un.mbox.pmboxq, phba->mbox_mem_pool);
James Smart7ad20aa2011-05-24 11:44:28 -04003389 lpfc_bsg_dma_page_free(phba, dd_data->context_un.mbox.dmabuffers);
James Smart3b5dd522010-01-26 23:10:15 -05003390 kfree(dd_data);
James Smart7ad20aa2011-05-24 11:44:28 -04003391
James Smarta33c4f72013-03-01 16:36:00 -05003392 /* Complete the job if the job is still active */
3393
James Smart7ad20aa2011-05-24 11:44:28 -04003394 if (job) {
3395 job->reply->result = 0;
3396 job->job_done(job);
3397 }
James Smart3b5dd522010-01-26 23:10:15 -05003398 return;
3399}
3400
3401/**
3402 * lpfc_bsg_check_cmd_access - test for a supported mailbox command
3403 * @phba: Pointer to HBA context object.
3404 * @mb: Pointer to a mailbox object.
3405 * @vport: Pointer to a vport object.
3406 *
3407 * Some commands require the port to be offline, some may not be called from
3408 * the application.
3409 **/
3410static int lpfc_bsg_check_cmd_access(struct lpfc_hba *phba,
3411 MAILBOX_t *mb, struct lpfc_vport *vport)
3412{
3413 /* return negative error values for bsg job */
3414 switch (mb->mbxCommand) {
3415 /* Offline only */
3416 case MBX_INIT_LINK:
3417 case MBX_DOWN_LINK:
3418 case MBX_CONFIG_LINK:
3419 case MBX_CONFIG_RING:
3420 case MBX_RESET_RING:
3421 case MBX_UNREG_LOGIN:
3422 case MBX_CLEAR_LA:
3423 case MBX_DUMP_CONTEXT:
3424 case MBX_RUN_DIAGS:
3425 case MBX_RESTART:
3426 case MBX_SET_MASK:
3427 if (!(vport->fc_flag & FC_OFFLINE_MODE)) {
3428 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3429 "2743 Command 0x%x is illegal in on-line "
3430 "state\n",
3431 mb->mbxCommand);
3432 return -EPERM;
3433 }
3434 case MBX_WRITE_NV:
3435 case MBX_WRITE_VPARMS:
3436 case MBX_LOAD_SM:
3437 case MBX_READ_NV:
3438 case MBX_READ_CONFIG:
3439 case MBX_READ_RCONFIG:
3440 case MBX_READ_STATUS:
3441 case MBX_READ_XRI:
3442 case MBX_READ_REV:
3443 case MBX_READ_LNK_STAT:
3444 case MBX_DUMP_MEMORY:
3445 case MBX_DOWN_LOAD:
3446 case MBX_UPDATE_CFG:
3447 case MBX_KILL_BOARD:
James Smart06f35552013-05-31 17:04:50 -04003448 case MBX_READ_TOPOLOGY:
James Smart3b5dd522010-01-26 23:10:15 -05003449 case MBX_LOAD_AREA:
3450 case MBX_LOAD_EXP_ROM:
3451 case MBX_BEACON:
3452 case MBX_DEL_LD_ENTRY:
3453 case MBX_SET_DEBUG:
3454 case MBX_WRITE_WWN:
3455 case MBX_SLI4_CONFIG:
James Smartc7495932010-04-06 15:05:28 -04003456 case MBX_READ_EVENT_LOG:
James Smart3b5dd522010-01-26 23:10:15 -05003457 case MBX_READ_EVENT_LOG_STATUS:
3458 case MBX_WRITE_EVENT_LOG:
3459 case MBX_PORT_CAPABILITIES:
3460 case MBX_PORT_IOV_CONTROL:
James Smart7a470272010-03-15 11:25:20 -04003461 case MBX_RUN_BIU_DIAG64:
James Smart3b5dd522010-01-26 23:10:15 -05003462 break;
3463 case MBX_SET_VARIABLE:
James Smarte2aed292010-02-26 14:15:00 -05003464 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3465 "1226 mbox: set_variable 0x%x, 0x%x\n",
3466 mb->un.varWords[0],
3467 mb->un.varWords[1]);
3468 if ((mb->un.varWords[0] == SETVAR_MLOMNT)
3469 && (mb->un.varWords[1] == 1)) {
3470 phba->wait_4_mlo_maint_flg = 1;
3471 } else if (mb->un.varWords[0] == SETVAR_MLORST) {
James Smart1b511972011-12-13 13:23:09 -05003472 spin_lock_irq(&phba->hbalock);
James Smarte2aed292010-02-26 14:15:00 -05003473 phba->link_flag &= ~LS_LOOPBACK_MODE;
James Smart1b511972011-12-13 13:23:09 -05003474 spin_unlock_irq(&phba->hbalock);
James Smart76a95d72010-11-20 23:11:48 -05003475 phba->fc_topology = LPFC_TOPOLOGY_PT_PT;
James Smarte2aed292010-02-26 14:15:00 -05003476 }
3477 break;
James Smart3b5dd522010-01-26 23:10:15 -05003478 case MBX_READ_SPARM64:
James Smart3b5dd522010-01-26 23:10:15 -05003479 case MBX_REG_LOGIN:
3480 case MBX_REG_LOGIN64:
3481 case MBX_CONFIG_PORT:
3482 case MBX_RUN_BIU_DIAG:
3483 default:
3484 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3485 "2742 Unknown Command 0x%x\n",
3486 mb->mbxCommand);
3487 return -EPERM;
3488 }
3489
3490 return 0; /* ok */
3491}
3492
3493/**
James Smart7ad20aa2011-05-24 11:44:28 -04003494 * lpfc_bsg_mbox_ext_cleanup - clean up context of multi-buffer mbox session
3495 * @phba: Pointer to HBA context object.
3496 *
3497 * This is routine clean up and reset BSG handling of multi-buffer mbox
3498 * command session.
3499 **/
3500static void
3501lpfc_bsg_mbox_ext_session_reset(struct lpfc_hba *phba)
3502{
3503 if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_IDLE)
3504 return;
3505
3506 /* free all memory, including dma buffers */
3507 lpfc_bsg_dma_page_list_free(phba,
3508 &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
3509 lpfc_bsg_dma_page_free(phba, phba->mbox_ext_buf_ctx.mbx_dmabuf);
3510 /* multi-buffer write mailbox command pass-through complete */
3511 memset((char *)&phba->mbox_ext_buf_ctx, 0,
3512 sizeof(struct lpfc_mbox_ext_buf_ctx));
3513 INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
3514
3515 return;
3516}
3517
3518/**
3519 * lpfc_bsg_issue_mbox_ext_handle_job - job handler for multi-buffer mbox cmpl
3520 * @phba: Pointer to HBA context object.
3521 * @pmboxq: Pointer to mailbox command.
3522 *
3523 * This is routine handles BSG job for mailbox commands completions with
3524 * multiple external buffers.
3525 **/
3526static struct fc_bsg_job *
3527lpfc_bsg_issue_mbox_ext_handle_job(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
3528{
3529 struct bsg_job_data *dd_data;
3530 struct fc_bsg_job *job;
3531 uint8_t *pmb, *pmb_buf;
3532 unsigned long flags;
3533 uint32_t size;
3534 int rc = 0;
James Smart026abb82011-12-13 13:20:45 -05003535 struct lpfc_dmabuf *dmabuf;
3536 struct lpfc_sli_config_mbox *sli_cfg_mbx;
3537 uint8_t *pmbx;
James Smart7ad20aa2011-05-24 11:44:28 -04003538
James Smart7ad20aa2011-05-24 11:44:28 -04003539 dd_data = pmboxq->context1;
James Smarta33c4f72013-03-01 16:36:00 -05003540
3541 /* Determine if job has been aborted */
3542 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3543 job = dd_data->set_job;
3544 if (job) {
3545 /* Prevent timeout handling from trying to abort job */
3546 job->dd_data = NULL;
James Smart7ad20aa2011-05-24 11:44:28 -04003547 }
James Smarta33c4f72013-03-01 16:36:00 -05003548 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smart7ad20aa2011-05-24 11:44:28 -04003549
3550 /*
3551 * The outgoing buffer is readily referred from the dma buffer,
3552 * just need to get header part from mailboxq structure.
3553 */
James Smarta33c4f72013-03-01 16:36:00 -05003554
James Smart7ad20aa2011-05-24 11:44:28 -04003555 pmb = (uint8_t *)&pmboxq->u.mb;
3556 pmb_buf = (uint8_t *)dd_data->context_un.mbox.mb;
James Smart026abb82011-12-13 13:20:45 -05003557 /* Copy the byte swapped response mailbox back to the user */
James Smart7ad20aa2011-05-24 11:44:28 -04003558 memcpy(pmb_buf, pmb, sizeof(MAILBOX_t));
James Smart026abb82011-12-13 13:20:45 -05003559 /* if there is any non-embedded extended data copy that too */
3560 dmabuf = phba->mbox_ext_buf_ctx.mbx_dmabuf;
3561 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
3562 if (!bsg_bf_get(lpfc_mbox_hdr_emb,
3563 &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) {
3564 pmbx = (uint8_t *)dmabuf->virt;
3565 /* byte swap the extended data following the mailbox command */
3566 lpfc_sli_pcimem_bcopy(&pmbx[sizeof(MAILBOX_t)],
3567 &pmbx[sizeof(MAILBOX_t)],
3568 sli_cfg_mbx->un.sli_config_emb0_subsys.mse[0].buf_len);
3569 }
James Smart7ad20aa2011-05-24 11:44:28 -04003570
James Smarta33c4f72013-03-01 16:36:00 -05003571 /* Complete the job if the job is still active */
3572
James Smart7ad20aa2011-05-24 11:44:28 -04003573 if (job) {
3574 size = job->reply_payload.payload_len;
3575 job->reply->reply_payload_rcv_len =
3576 sg_copy_from_buffer(job->reply_payload.sg_list,
3577 job->reply_payload.sg_cnt,
3578 pmb_buf, size);
James Smarta33c4f72013-03-01 16:36:00 -05003579
James Smart7ad20aa2011-05-24 11:44:28 -04003580 /* result for successful */
3581 job->reply->result = 0;
James Smarta33c4f72013-03-01 16:36:00 -05003582
James Smart7ad20aa2011-05-24 11:44:28 -04003583 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3584 "2937 SLI_CONFIG ext-buffer maibox command "
3585 "(x%x/x%x) complete bsg job done, bsize:%d\n",
3586 phba->mbox_ext_buf_ctx.nembType,
3587 phba->mbox_ext_buf_ctx.mboxType, size);
James Smartb76f2dc2011-07-22 18:37:42 -04003588 lpfc_idiag_mbxacc_dump_bsg_mbox(phba,
3589 phba->mbox_ext_buf_ctx.nembType,
3590 phba->mbox_ext_buf_ctx.mboxType,
3591 dma_ebuf, sta_pos_addr,
3592 phba->mbox_ext_buf_ctx.mbx_dmabuf, 0);
James Smarta33c4f72013-03-01 16:36:00 -05003593 } else {
James Smart7ad20aa2011-05-24 11:44:28 -04003594 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3595 "2938 SLI_CONFIG ext-buffer maibox "
3596 "command (x%x/x%x) failure, rc:x%x\n",
3597 phba->mbox_ext_buf_ctx.nembType,
3598 phba->mbox_ext_buf_ctx.mboxType, rc);
James Smarta33c4f72013-03-01 16:36:00 -05003599 }
3600
3601
James Smart7ad20aa2011-05-24 11:44:28 -04003602 /* state change */
3603 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_DONE;
3604 kfree(dd_data);
James Smart7ad20aa2011-05-24 11:44:28 -04003605 return job;
3606}
3607
3608/**
3609 * lpfc_bsg_issue_read_mbox_ext_cmpl - compl handler for multi-buffer read mbox
3610 * @phba: Pointer to HBA context object.
3611 * @pmboxq: Pointer to mailbox command.
3612 *
3613 * This is completion handler function for mailbox read commands with multiple
3614 * external buffers.
3615 **/
3616static void
3617lpfc_bsg_issue_read_mbox_ext_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
3618{
3619 struct fc_bsg_job *job;
3620
James Smarta33c4f72013-03-01 16:36:00 -05003621 job = lpfc_bsg_issue_mbox_ext_handle_job(phba, pmboxq);
3622
James Smart7ad20aa2011-05-24 11:44:28 -04003623 /* handle the BSG job with mailbox command */
James Smarta33c4f72013-03-01 16:36:00 -05003624 if (!job)
James Smart7ad20aa2011-05-24 11:44:28 -04003625 pmboxq->u.mb.mbxStatus = MBXERR_ERROR;
3626
3627 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3628 "2939 SLI_CONFIG ext-buffer rd maibox command "
3629 "complete, ctxState:x%x, mbxStatus:x%x\n",
3630 phba->mbox_ext_buf_ctx.state, pmboxq->u.mb.mbxStatus);
3631
James Smart7ad20aa2011-05-24 11:44:28 -04003632 if (pmboxq->u.mb.mbxStatus || phba->mbox_ext_buf_ctx.numBuf == 1)
3633 lpfc_bsg_mbox_ext_session_reset(phba);
3634
3635 /* free base driver mailbox structure memory */
3636 mempool_free(pmboxq, phba->mbox_mem_pool);
3637
James Smarta33c4f72013-03-01 16:36:00 -05003638 /* if the job is still active, call job done */
James Smart7ad20aa2011-05-24 11:44:28 -04003639 if (job)
3640 job->job_done(job);
3641
3642 return;
3643}
3644
3645/**
3646 * lpfc_bsg_issue_write_mbox_ext_cmpl - cmpl handler for multi-buffer write mbox
3647 * @phba: Pointer to HBA context object.
3648 * @pmboxq: Pointer to mailbox command.
3649 *
3650 * This is completion handler function for mailbox write commands with multiple
3651 * external buffers.
3652 **/
3653static void
3654lpfc_bsg_issue_write_mbox_ext_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
3655{
3656 struct fc_bsg_job *job;
3657
James Smarta33c4f72013-03-01 16:36:00 -05003658 job = lpfc_bsg_issue_mbox_ext_handle_job(phba, pmboxq);
3659
James Smart7ad20aa2011-05-24 11:44:28 -04003660 /* handle the BSG job with the mailbox command */
James Smarta33c4f72013-03-01 16:36:00 -05003661 if (!job)
James Smart7ad20aa2011-05-24 11:44:28 -04003662 pmboxq->u.mb.mbxStatus = MBXERR_ERROR;
3663
3664 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3665 "2940 SLI_CONFIG ext-buffer wr maibox command "
3666 "complete, ctxState:x%x, mbxStatus:x%x\n",
3667 phba->mbox_ext_buf_ctx.state, pmboxq->u.mb.mbxStatus);
3668
James Smart7ad20aa2011-05-24 11:44:28 -04003669 /* free all memory, including dma buffers */
3670 mempool_free(pmboxq, phba->mbox_mem_pool);
3671 lpfc_bsg_mbox_ext_session_reset(phba);
3672
James Smarta33c4f72013-03-01 16:36:00 -05003673 /* if the job is still active, call job done */
James Smart7ad20aa2011-05-24 11:44:28 -04003674 if (job)
3675 job->job_done(job);
3676
3677 return;
3678}
3679
3680static void
3681lpfc_bsg_sli_cfg_dma_desc_setup(struct lpfc_hba *phba, enum nemb_type nemb_tp,
3682 uint32_t index, struct lpfc_dmabuf *mbx_dmabuf,
3683 struct lpfc_dmabuf *ext_dmabuf)
3684{
3685 struct lpfc_sli_config_mbox *sli_cfg_mbx;
3686
3687 /* pointer to the start of mailbox command */
3688 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)mbx_dmabuf->virt;
3689
3690 if (nemb_tp == nemb_mse) {
3691 if (index == 0) {
3692 sli_cfg_mbx->un.sli_config_emb0_subsys.
3693 mse[index].pa_hi =
3694 putPaddrHigh(mbx_dmabuf->phys +
3695 sizeof(MAILBOX_t));
3696 sli_cfg_mbx->un.sli_config_emb0_subsys.
3697 mse[index].pa_lo =
3698 putPaddrLow(mbx_dmabuf->phys +
3699 sizeof(MAILBOX_t));
3700 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3701 "2943 SLI_CONFIG(mse)[%d], "
3702 "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3703 index,
3704 sli_cfg_mbx->un.sli_config_emb0_subsys.
3705 mse[index].buf_len,
3706 sli_cfg_mbx->un.sli_config_emb0_subsys.
3707 mse[index].pa_hi,
3708 sli_cfg_mbx->un.sli_config_emb0_subsys.
3709 mse[index].pa_lo);
3710 } else {
3711 sli_cfg_mbx->un.sli_config_emb0_subsys.
3712 mse[index].pa_hi =
3713 putPaddrHigh(ext_dmabuf->phys);
3714 sli_cfg_mbx->un.sli_config_emb0_subsys.
3715 mse[index].pa_lo =
3716 putPaddrLow(ext_dmabuf->phys);
3717 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3718 "2944 SLI_CONFIG(mse)[%d], "
3719 "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3720 index,
3721 sli_cfg_mbx->un.sli_config_emb0_subsys.
3722 mse[index].buf_len,
3723 sli_cfg_mbx->un.sli_config_emb0_subsys.
3724 mse[index].pa_hi,
3725 sli_cfg_mbx->un.sli_config_emb0_subsys.
3726 mse[index].pa_lo);
3727 }
3728 } else {
3729 if (index == 0) {
3730 sli_cfg_mbx->un.sli_config_emb1_subsys.
3731 hbd[index].pa_hi =
3732 putPaddrHigh(mbx_dmabuf->phys +
3733 sizeof(MAILBOX_t));
3734 sli_cfg_mbx->un.sli_config_emb1_subsys.
3735 hbd[index].pa_lo =
3736 putPaddrLow(mbx_dmabuf->phys +
3737 sizeof(MAILBOX_t));
3738 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3739 "3007 SLI_CONFIG(hbd)[%d], "
3740 "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3741 index,
3742 bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
3743 &sli_cfg_mbx->un.
3744 sli_config_emb1_subsys.hbd[index]),
3745 sli_cfg_mbx->un.sli_config_emb1_subsys.
3746 hbd[index].pa_hi,
3747 sli_cfg_mbx->un.sli_config_emb1_subsys.
3748 hbd[index].pa_lo);
3749
3750 } else {
3751 sli_cfg_mbx->un.sli_config_emb1_subsys.
3752 hbd[index].pa_hi =
3753 putPaddrHigh(ext_dmabuf->phys);
3754 sli_cfg_mbx->un.sli_config_emb1_subsys.
3755 hbd[index].pa_lo =
3756 putPaddrLow(ext_dmabuf->phys);
3757 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3758 "3008 SLI_CONFIG(hbd)[%d], "
3759 "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3760 index,
3761 bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
3762 &sli_cfg_mbx->un.
3763 sli_config_emb1_subsys.hbd[index]),
3764 sli_cfg_mbx->un.sli_config_emb1_subsys.
3765 hbd[index].pa_hi,
3766 sli_cfg_mbx->un.sli_config_emb1_subsys.
3767 hbd[index].pa_lo);
3768 }
3769 }
3770 return;
3771}
3772
3773/**
3774 * lpfc_bsg_sli_cfg_mse_read_cmd_ext - sli_config non-embedded mailbox cmd read
3775 * @phba: Pointer to HBA context object.
3776 * @mb: Pointer to a BSG mailbox object.
3777 * @nemb_tp: Enumerate of non-embedded mailbox command type.
3778 * @dmabuff: Pointer to a DMA buffer descriptor.
3779 *
3780 * This routine performs SLI_CONFIG (0x9B) read mailbox command operation with
3781 * non-embedded external bufffers.
3782 **/
3783static int
3784lpfc_bsg_sli_cfg_read_cmd_ext(struct lpfc_hba *phba, struct fc_bsg_job *job,
3785 enum nemb_type nemb_tp,
3786 struct lpfc_dmabuf *dmabuf)
3787{
3788 struct lpfc_sli_config_mbox *sli_cfg_mbx;
3789 struct dfc_mbox_req *mbox_req;
3790 struct lpfc_dmabuf *curr_dmabuf, *next_dmabuf;
3791 uint32_t ext_buf_cnt, ext_buf_index;
3792 struct lpfc_dmabuf *ext_dmabuf = NULL;
3793 struct bsg_job_data *dd_data = NULL;
3794 LPFC_MBOXQ_t *pmboxq = NULL;
3795 MAILBOX_t *pmb;
3796 uint8_t *pmbx;
3797 int rc, i;
3798
3799 mbox_req =
3800 (struct dfc_mbox_req *)job->request->rqst_data.h_vendor.vendor_cmd;
3801
3802 /* pointer to the start of mailbox command */
3803 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
3804
3805 if (nemb_tp == nemb_mse) {
3806 ext_buf_cnt = bsg_bf_get(lpfc_mbox_hdr_mse_cnt,
3807 &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr);
3808 if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_MSE) {
3809 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3810 "2945 Handled SLI_CONFIG(mse) rd, "
3811 "ext_buf_cnt(%d) out of range(%d)\n",
3812 ext_buf_cnt,
3813 LPFC_MBX_SLI_CONFIG_MAX_MSE);
3814 rc = -ERANGE;
3815 goto job_error;
3816 }
3817 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3818 "2941 Handled SLI_CONFIG(mse) rd, "
3819 "ext_buf_cnt:%d\n", ext_buf_cnt);
3820 } else {
3821 /* sanity check on interface type for support */
3822 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
3823 LPFC_SLI_INTF_IF_TYPE_2) {
3824 rc = -ENODEV;
3825 goto job_error;
3826 }
3827 /* nemb_tp == nemb_hbd */
3828 ext_buf_cnt = sli_cfg_mbx->un.sli_config_emb1_subsys.hbd_count;
3829 if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_HBD) {
3830 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3831 "2946 Handled SLI_CONFIG(hbd) rd, "
3832 "ext_buf_cnt(%d) out of range(%d)\n",
3833 ext_buf_cnt,
3834 LPFC_MBX_SLI_CONFIG_MAX_HBD);
3835 rc = -ERANGE;
3836 goto job_error;
3837 }
3838 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3839 "2942 Handled SLI_CONFIG(hbd) rd, "
3840 "ext_buf_cnt:%d\n", ext_buf_cnt);
3841 }
3842
James Smartb76f2dc2011-07-22 18:37:42 -04003843 /* before dma descriptor setup */
3844 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_rd, dma_mbox,
3845 sta_pre_addr, dmabuf, ext_buf_cnt);
3846
James Smart7ad20aa2011-05-24 11:44:28 -04003847 /* reject non-embedded mailbox command with none external buffer */
3848 if (ext_buf_cnt == 0) {
3849 rc = -EPERM;
3850 goto job_error;
3851 } else if (ext_buf_cnt > 1) {
3852 /* additional external read buffers */
3853 for (i = 1; i < ext_buf_cnt; i++) {
3854 ext_dmabuf = lpfc_bsg_dma_page_alloc(phba);
3855 if (!ext_dmabuf) {
3856 rc = -ENOMEM;
3857 goto job_error;
3858 }
3859 list_add_tail(&ext_dmabuf->list,
3860 &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
3861 }
3862 }
3863
3864 /* bsg tracking structure */
3865 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
3866 if (!dd_data) {
3867 rc = -ENOMEM;
3868 goto job_error;
3869 }
3870
3871 /* mailbox command structure for base driver */
3872 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3873 if (!pmboxq) {
3874 rc = -ENOMEM;
3875 goto job_error;
3876 }
3877 memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
3878
3879 /* for the first external buffer */
3880 lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, 0, dmabuf, dmabuf);
3881
3882 /* for the rest of external buffer descriptors if any */
3883 if (ext_buf_cnt > 1) {
3884 ext_buf_index = 1;
3885 list_for_each_entry_safe(curr_dmabuf, next_dmabuf,
3886 &phba->mbox_ext_buf_ctx.ext_dmabuf_list, list) {
3887 lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp,
3888 ext_buf_index, dmabuf,
3889 curr_dmabuf);
3890 ext_buf_index++;
3891 }
3892 }
3893
James Smartb76f2dc2011-07-22 18:37:42 -04003894 /* after dma descriptor setup */
3895 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_rd, dma_mbox,
3896 sta_pos_addr, dmabuf, ext_buf_cnt);
3897
James Smart7ad20aa2011-05-24 11:44:28 -04003898 /* construct base driver mbox command */
3899 pmb = &pmboxq->u.mb;
3900 pmbx = (uint8_t *)dmabuf->virt;
3901 memcpy(pmb, pmbx, sizeof(*pmb));
3902 pmb->mbxOwner = OWN_HOST;
3903 pmboxq->vport = phba->pport;
3904
3905 /* multi-buffer handling context */
3906 phba->mbox_ext_buf_ctx.nembType = nemb_tp;
3907 phba->mbox_ext_buf_ctx.mboxType = mbox_rd;
3908 phba->mbox_ext_buf_ctx.numBuf = ext_buf_cnt;
3909 phba->mbox_ext_buf_ctx.mbxTag = mbox_req->extMboxTag;
3910 phba->mbox_ext_buf_ctx.seqNum = mbox_req->extSeqNum;
3911 phba->mbox_ext_buf_ctx.mbx_dmabuf = dmabuf;
3912
3913 /* callback for multi-buffer read mailbox command */
3914 pmboxq->mbox_cmpl = lpfc_bsg_issue_read_mbox_ext_cmpl;
3915
3916 /* context fields to callback function */
3917 pmboxq->context1 = dd_data;
3918 dd_data->type = TYPE_MBOX;
James Smarta33c4f72013-03-01 16:36:00 -05003919 dd_data->set_job = job;
James Smart7ad20aa2011-05-24 11:44:28 -04003920 dd_data->context_un.mbox.pmboxq = pmboxq;
3921 dd_data->context_un.mbox.mb = (MAILBOX_t *)pmbx;
James Smart7ad20aa2011-05-24 11:44:28 -04003922 job->dd_data = dd_data;
3923
3924 /* state change */
3925 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
3926
James Smart026abb82011-12-13 13:20:45 -05003927 /*
3928 * Non-embedded mailbox subcommand data gets byte swapped here because
3929 * the lower level driver code only does the first 64 mailbox words.
3930 */
3931 if ((!bsg_bf_get(lpfc_mbox_hdr_emb,
3932 &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) &&
3933 (nemb_tp == nemb_mse))
3934 lpfc_sli_pcimem_bcopy(&pmbx[sizeof(MAILBOX_t)],
3935 &pmbx[sizeof(MAILBOX_t)],
3936 sli_cfg_mbx->un.sli_config_emb0_subsys.
3937 mse[0].buf_len);
3938
James Smart7ad20aa2011-05-24 11:44:28 -04003939 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
3940 if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
3941 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3942 "2947 Issued SLI_CONFIG ext-buffer "
3943 "maibox command, rc:x%x\n", rc);
James Smart88a2cfb2011-07-22 18:36:33 -04003944 return SLI_CONFIG_HANDLED;
James Smart7ad20aa2011-05-24 11:44:28 -04003945 }
3946 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3947 "2948 Failed to issue SLI_CONFIG ext-buffer "
3948 "maibox command, rc:x%x\n", rc);
3949 rc = -EPIPE;
3950
3951job_error:
3952 if (pmboxq)
3953 mempool_free(pmboxq, phba->mbox_mem_pool);
3954 lpfc_bsg_dma_page_list_free(phba,
3955 &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
3956 kfree(dd_data);
3957 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_IDLE;
3958 return rc;
3959}
3960
3961/**
3962 * lpfc_bsg_sli_cfg_write_cmd_ext - sli_config non-embedded mailbox cmd write
3963 * @phba: Pointer to HBA context object.
3964 * @mb: Pointer to a BSG mailbox object.
3965 * @dmabuff: Pointer to a DMA buffer descriptor.
3966 *
3967 * This routine performs SLI_CONFIG (0x9B) write mailbox command operation with
3968 * non-embedded external bufffers.
3969 **/
3970static int
3971lpfc_bsg_sli_cfg_write_cmd_ext(struct lpfc_hba *phba, struct fc_bsg_job *job,
3972 enum nemb_type nemb_tp,
3973 struct lpfc_dmabuf *dmabuf)
3974{
3975 struct dfc_mbox_req *mbox_req;
3976 struct lpfc_sli_config_mbox *sli_cfg_mbx;
3977 uint32_t ext_buf_cnt;
3978 struct bsg_job_data *dd_data = NULL;
3979 LPFC_MBOXQ_t *pmboxq = NULL;
3980 MAILBOX_t *pmb;
3981 uint8_t *mbx;
James Smart88a2cfb2011-07-22 18:36:33 -04003982 int rc = SLI_CONFIG_NOT_HANDLED, i;
James Smart7ad20aa2011-05-24 11:44:28 -04003983
3984 mbox_req =
3985 (struct dfc_mbox_req *)job->request->rqst_data.h_vendor.vendor_cmd;
3986
3987 /* pointer to the start of mailbox command */
3988 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
3989
3990 if (nemb_tp == nemb_mse) {
3991 ext_buf_cnt = bsg_bf_get(lpfc_mbox_hdr_mse_cnt,
3992 &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr);
3993 if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_MSE) {
3994 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
James Smart026abb82011-12-13 13:20:45 -05003995 "2953 Failed SLI_CONFIG(mse) wr, "
James Smart7ad20aa2011-05-24 11:44:28 -04003996 "ext_buf_cnt(%d) out of range(%d)\n",
3997 ext_buf_cnt,
3998 LPFC_MBX_SLI_CONFIG_MAX_MSE);
3999 return -ERANGE;
4000 }
4001 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4002 "2949 Handled SLI_CONFIG(mse) wr, "
4003 "ext_buf_cnt:%d\n", ext_buf_cnt);
4004 } else {
4005 /* sanity check on interface type for support */
4006 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
4007 LPFC_SLI_INTF_IF_TYPE_2)
4008 return -ENODEV;
4009 /* nemb_tp == nemb_hbd */
4010 ext_buf_cnt = sli_cfg_mbx->un.sli_config_emb1_subsys.hbd_count;
4011 if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_HBD) {
4012 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
James Smart026abb82011-12-13 13:20:45 -05004013 "2954 Failed SLI_CONFIG(hbd) wr, "
James Smart7ad20aa2011-05-24 11:44:28 -04004014 "ext_buf_cnt(%d) out of range(%d)\n",
4015 ext_buf_cnt,
4016 LPFC_MBX_SLI_CONFIG_MAX_HBD);
4017 return -ERANGE;
4018 }
4019 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4020 "2950 Handled SLI_CONFIG(hbd) wr, "
4021 "ext_buf_cnt:%d\n", ext_buf_cnt);
4022 }
4023
James Smartb76f2dc2011-07-22 18:37:42 -04004024 /* before dma buffer descriptor setup */
4025 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_wr, dma_mbox,
4026 sta_pre_addr, dmabuf, ext_buf_cnt);
4027
James Smart7ad20aa2011-05-24 11:44:28 -04004028 if (ext_buf_cnt == 0)
4029 return -EPERM;
4030
4031 /* for the first external buffer */
4032 lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, 0, dmabuf, dmabuf);
4033
James Smartb76f2dc2011-07-22 18:37:42 -04004034 /* after dma descriptor setup */
4035 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_wr, dma_mbox,
4036 sta_pos_addr, dmabuf, ext_buf_cnt);
4037
James Smart7ad20aa2011-05-24 11:44:28 -04004038 /* log for looking forward */
4039 for (i = 1; i < ext_buf_cnt; i++) {
4040 if (nemb_tp == nemb_mse)
4041 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4042 "2951 SLI_CONFIG(mse), buf[%d]-length:%d\n",
4043 i, sli_cfg_mbx->un.sli_config_emb0_subsys.
4044 mse[i].buf_len);
4045 else
4046 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4047 "2952 SLI_CONFIG(hbd), buf[%d]-length:%d\n",
4048 i, bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
4049 &sli_cfg_mbx->un.sli_config_emb1_subsys.
4050 hbd[i]));
4051 }
4052
4053 /* multi-buffer handling context */
4054 phba->mbox_ext_buf_ctx.nembType = nemb_tp;
4055 phba->mbox_ext_buf_ctx.mboxType = mbox_wr;
4056 phba->mbox_ext_buf_ctx.numBuf = ext_buf_cnt;
4057 phba->mbox_ext_buf_ctx.mbxTag = mbox_req->extMboxTag;
4058 phba->mbox_ext_buf_ctx.seqNum = mbox_req->extSeqNum;
4059 phba->mbox_ext_buf_ctx.mbx_dmabuf = dmabuf;
4060
4061 if (ext_buf_cnt == 1) {
4062 /* bsg tracking structure */
4063 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
4064 if (!dd_data) {
4065 rc = -ENOMEM;
4066 goto job_error;
4067 }
4068
4069 /* mailbox command structure for base driver */
4070 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4071 if (!pmboxq) {
4072 rc = -ENOMEM;
4073 goto job_error;
4074 }
4075 memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
4076 pmb = &pmboxq->u.mb;
4077 mbx = (uint8_t *)dmabuf->virt;
4078 memcpy(pmb, mbx, sizeof(*pmb));
4079 pmb->mbxOwner = OWN_HOST;
4080 pmboxq->vport = phba->pport;
4081
4082 /* callback for multi-buffer read mailbox command */
4083 pmboxq->mbox_cmpl = lpfc_bsg_issue_write_mbox_ext_cmpl;
4084
4085 /* context fields to callback function */
4086 pmboxq->context1 = dd_data;
4087 dd_data->type = TYPE_MBOX;
James Smarta33c4f72013-03-01 16:36:00 -05004088 dd_data->set_job = job;
James Smart7ad20aa2011-05-24 11:44:28 -04004089 dd_data->context_un.mbox.pmboxq = pmboxq;
4090 dd_data->context_un.mbox.mb = (MAILBOX_t *)mbx;
James Smart7ad20aa2011-05-24 11:44:28 -04004091 job->dd_data = dd_data;
4092
4093 /* state change */
James Smart7ad20aa2011-05-24 11:44:28 -04004094
James Smarta33c4f72013-03-01 16:36:00 -05004095 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
James Smart7ad20aa2011-05-24 11:44:28 -04004096 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
4097 if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
4098 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4099 "2955 Issued SLI_CONFIG ext-buffer "
4100 "maibox command, rc:x%x\n", rc);
James Smart88a2cfb2011-07-22 18:36:33 -04004101 return SLI_CONFIG_HANDLED;
James Smart7ad20aa2011-05-24 11:44:28 -04004102 }
4103 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4104 "2956 Failed to issue SLI_CONFIG ext-buffer "
4105 "maibox command, rc:x%x\n", rc);
4106 rc = -EPIPE;
James Smart026abb82011-12-13 13:20:45 -05004107 goto job_error;
James Smart7ad20aa2011-05-24 11:44:28 -04004108 }
4109
James Smart88a2cfb2011-07-22 18:36:33 -04004110 /* wait for additoinal external buffers */
James Smarta33c4f72013-03-01 16:36:00 -05004111
James Smart88a2cfb2011-07-22 18:36:33 -04004112 job->reply->result = 0;
4113 job->job_done(job);
4114 return SLI_CONFIG_HANDLED;
4115
James Smart7ad20aa2011-05-24 11:44:28 -04004116job_error:
4117 if (pmboxq)
4118 mempool_free(pmboxq, phba->mbox_mem_pool);
4119 kfree(dd_data);
4120
4121 return rc;
4122}
4123
4124/**
4125 * lpfc_bsg_handle_sli_cfg_mbox - handle sli-cfg mailbox cmd with ext buffer
4126 * @phba: Pointer to HBA context object.
4127 * @mb: Pointer to a BSG mailbox object.
4128 * @dmabuff: Pointer to a DMA buffer descriptor.
4129 *
4130 * This routine handles SLI_CONFIG (0x9B) mailbox command with non-embedded
4131 * external bufffers, including both 0x9B with non-embedded MSEs and 0x9B
4132 * with embedded sussystem 0x1 and opcodes with external HBDs.
4133 **/
4134static int
4135lpfc_bsg_handle_sli_cfg_mbox(struct lpfc_hba *phba, struct fc_bsg_job *job,
4136 struct lpfc_dmabuf *dmabuf)
4137{
4138 struct lpfc_sli_config_mbox *sli_cfg_mbx;
4139 uint32_t subsys;
4140 uint32_t opcode;
4141 int rc = SLI_CONFIG_NOT_HANDLED;
4142
James Smart026abb82011-12-13 13:20:45 -05004143 /* state change on new multi-buffer pass-through mailbox command */
James Smart7ad20aa2011-05-24 11:44:28 -04004144 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_HOST;
4145
4146 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
4147
4148 if (!bsg_bf_get(lpfc_mbox_hdr_emb,
4149 &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) {
4150 subsys = bsg_bf_get(lpfc_emb0_subcmnd_subsys,
4151 &sli_cfg_mbx->un.sli_config_emb0_subsys);
4152 opcode = bsg_bf_get(lpfc_emb0_subcmnd_opcode,
4153 &sli_cfg_mbx->un.sli_config_emb0_subsys);
4154 if (subsys == SLI_CONFIG_SUBSYS_FCOE) {
4155 switch (opcode) {
4156 case FCOE_OPCODE_READ_FCF:
James Smart2a2719d2014-02-20 09:56:28 -05004157 case FCOE_OPCODE_GET_DPORT_RESULTS:
James Smart7ad20aa2011-05-24 11:44:28 -04004158 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4159 "2957 Handled SLI_CONFIG "
4160 "subsys_fcoe, opcode:x%x\n",
4161 opcode);
4162 rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
4163 nemb_mse, dmabuf);
4164 break;
4165 case FCOE_OPCODE_ADD_FCF:
James Smart2a2719d2014-02-20 09:56:28 -05004166 case FCOE_OPCODE_SET_DPORT_MODE:
4167 case LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_STATE:
James Smart7ad20aa2011-05-24 11:44:28 -04004168 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4169 "2958 Handled SLI_CONFIG "
4170 "subsys_fcoe, opcode:x%x\n",
4171 opcode);
4172 rc = lpfc_bsg_sli_cfg_write_cmd_ext(phba, job,
4173 nemb_mse, dmabuf);
4174 break;
4175 default:
4176 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
James Smart026abb82011-12-13 13:20:45 -05004177 "2959 Reject SLI_CONFIG "
James Smart7ad20aa2011-05-24 11:44:28 -04004178 "subsys_fcoe, opcode:x%x\n",
4179 opcode);
James Smart026abb82011-12-13 13:20:45 -05004180 rc = -EPERM;
4181 break;
4182 }
4183 } else if (subsys == SLI_CONFIG_SUBSYS_COMN) {
4184 switch (opcode) {
4185 case COMN_OPCODE_GET_CNTL_ADDL_ATTRIBUTES:
James Smartb99570d2012-05-09 21:16:24 -04004186 case COMN_OPCODE_GET_CNTL_ATTRIBUTES:
James Smart026abb82011-12-13 13:20:45 -05004187 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4188 "3106 Handled SLI_CONFIG "
James Smart6b5151f2012-01-18 16:24:06 -05004189 "subsys_comn, opcode:x%x\n",
James Smart026abb82011-12-13 13:20:45 -05004190 opcode);
4191 rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
4192 nemb_mse, dmabuf);
4193 break;
4194 default:
4195 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4196 "3107 Reject SLI_CONFIG "
James Smart6b5151f2012-01-18 16:24:06 -05004197 "subsys_comn, opcode:x%x\n",
James Smart026abb82011-12-13 13:20:45 -05004198 opcode);
4199 rc = -EPERM;
James Smart7ad20aa2011-05-24 11:44:28 -04004200 break;
4201 }
4202 } else {
4203 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
James Smart026abb82011-12-13 13:20:45 -05004204 "2977 Reject SLI_CONFIG "
James Smart7ad20aa2011-05-24 11:44:28 -04004205 "subsys:x%d, opcode:x%x\n",
4206 subsys, opcode);
James Smart026abb82011-12-13 13:20:45 -05004207 rc = -EPERM;
James Smart7ad20aa2011-05-24 11:44:28 -04004208 }
4209 } else {
4210 subsys = bsg_bf_get(lpfc_emb1_subcmnd_subsys,
4211 &sli_cfg_mbx->un.sli_config_emb1_subsys);
4212 opcode = bsg_bf_get(lpfc_emb1_subcmnd_opcode,
4213 &sli_cfg_mbx->un.sli_config_emb1_subsys);
4214 if (subsys == SLI_CONFIG_SUBSYS_COMN) {
4215 switch (opcode) {
4216 case COMN_OPCODE_READ_OBJECT:
4217 case COMN_OPCODE_READ_OBJECT_LIST:
4218 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4219 "2960 Handled SLI_CONFIG "
4220 "subsys_comn, opcode:x%x\n",
4221 opcode);
4222 rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
4223 nemb_hbd, dmabuf);
4224 break;
4225 case COMN_OPCODE_WRITE_OBJECT:
4226 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4227 "2961 Handled SLI_CONFIG "
4228 "subsys_comn, opcode:x%x\n",
4229 opcode);
4230 rc = lpfc_bsg_sli_cfg_write_cmd_ext(phba, job,
4231 nemb_hbd, dmabuf);
4232 break;
4233 default:
4234 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4235 "2962 Not handled SLI_CONFIG "
4236 "subsys_comn, opcode:x%x\n",
4237 opcode);
4238 rc = SLI_CONFIG_NOT_HANDLED;
4239 break;
4240 }
4241 } else {
4242 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
James Smart026abb82011-12-13 13:20:45 -05004243 "2978 Not handled SLI_CONFIG "
James Smart7ad20aa2011-05-24 11:44:28 -04004244 "subsys:x%d, opcode:x%x\n",
4245 subsys, opcode);
4246 rc = SLI_CONFIG_NOT_HANDLED;
4247 }
4248 }
James Smart026abb82011-12-13 13:20:45 -05004249
4250 /* state reset on not handled new multi-buffer mailbox command */
4251 if (rc != SLI_CONFIG_HANDLED)
4252 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_IDLE;
4253
James Smart7ad20aa2011-05-24 11:44:28 -04004254 return rc;
4255}
4256
4257/**
4258 * lpfc_bsg_mbox_ext_abort_req - request to abort mbox command with ext buffers
4259 * @phba: Pointer to HBA context object.
4260 *
4261 * This routine is for requesting to abort a pass-through mailbox command with
4262 * multiple external buffers due to error condition.
4263 **/
4264static void
4265lpfc_bsg_mbox_ext_abort(struct lpfc_hba *phba)
4266{
4267 if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_PORT)
4268 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_ABTS;
4269 else
4270 lpfc_bsg_mbox_ext_session_reset(phba);
4271 return;
4272}
4273
4274/**
4275 * lpfc_bsg_read_ebuf_get - get the next mailbox read external buffer
4276 * @phba: Pointer to HBA context object.
4277 * @dmabuf: Pointer to a DMA buffer descriptor.
4278 *
4279 * This routine extracts the next mailbox read external buffer back to
4280 * user space through BSG.
4281 **/
4282static int
4283lpfc_bsg_read_ebuf_get(struct lpfc_hba *phba, struct fc_bsg_job *job)
4284{
4285 struct lpfc_sli_config_mbox *sli_cfg_mbx;
4286 struct lpfc_dmabuf *dmabuf;
4287 uint8_t *pbuf;
4288 uint32_t size;
4289 uint32_t index;
4290
4291 index = phba->mbox_ext_buf_ctx.seqNum;
4292 phba->mbox_ext_buf_ctx.seqNum++;
4293
4294 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)
4295 phba->mbox_ext_buf_ctx.mbx_dmabuf->virt;
4296
4297 if (phba->mbox_ext_buf_ctx.nembType == nemb_mse) {
4298 size = bsg_bf_get(lpfc_mbox_sli_config_mse_len,
4299 &sli_cfg_mbx->un.sli_config_emb0_subsys.mse[index]);
4300 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4301 "2963 SLI_CONFIG (mse) ext-buffer rd get "
4302 "buffer[%d], size:%d\n", index, size);
4303 } else {
4304 size = bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
4305 &sli_cfg_mbx->un.sli_config_emb1_subsys.hbd[index]);
4306 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4307 "2964 SLI_CONFIG (hbd) ext-buffer rd get "
4308 "buffer[%d], size:%d\n", index, size);
4309 }
4310 if (list_empty(&phba->mbox_ext_buf_ctx.ext_dmabuf_list))
4311 return -EPIPE;
4312 dmabuf = list_first_entry(&phba->mbox_ext_buf_ctx.ext_dmabuf_list,
4313 struct lpfc_dmabuf, list);
4314 list_del_init(&dmabuf->list);
James Smartb76f2dc2011-07-22 18:37:42 -04004315
4316 /* after dma buffer descriptor setup */
4317 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, phba->mbox_ext_buf_ctx.nembType,
4318 mbox_rd, dma_ebuf, sta_pos_addr,
4319 dmabuf, index);
4320
James Smart7ad20aa2011-05-24 11:44:28 -04004321 pbuf = (uint8_t *)dmabuf->virt;
4322 job->reply->reply_payload_rcv_len =
4323 sg_copy_from_buffer(job->reply_payload.sg_list,
4324 job->reply_payload.sg_cnt,
4325 pbuf, size);
4326
4327 lpfc_bsg_dma_page_free(phba, dmabuf);
4328
4329 if (phba->mbox_ext_buf_ctx.seqNum == phba->mbox_ext_buf_ctx.numBuf) {
4330 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4331 "2965 SLI_CONFIG (hbd) ext-buffer rd mbox "
4332 "command session done\n");
4333 lpfc_bsg_mbox_ext_session_reset(phba);
4334 }
4335
4336 job->reply->result = 0;
4337 job->job_done(job);
4338
4339 return SLI_CONFIG_HANDLED;
4340}
4341
4342/**
4343 * lpfc_bsg_write_ebuf_set - set the next mailbox write external buffer
4344 * @phba: Pointer to HBA context object.
4345 * @dmabuf: Pointer to a DMA buffer descriptor.
4346 *
4347 * This routine sets up the next mailbox read external buffer obtained
4348 * from user space through BSG.
4349 **/
4350static int
4351lpfc_bsg_write_ebuf_set(struct lpfc_hba *phba, struct fc_bsg_job *job,
4352 struct lpfc_dmabuf *dmabuf)
4353{
4354 struct lpfc_sli_config_mbox *sli_cfg_mbx;
4355 struct bsg_job_data *dd_data = NULL;
4356 LPFC_MBOXQ_t *pmboxq = NULL;
4357 MAILBOX_t *pmb;
4358 enum nemb_type nemb_tp;
4359 uint8_t *pbuf;
4360 uint32_t size;
4361 uint32_t index;
4362 int rc;
4363
4364 index = phba->mbox_ext_buf_ctx.seqNum;
4365 phba->mbox_ext_buf_ctx.seqNum++;
4366 nemb_tp = phba->mbox_ext_buf_ctx.nembType;
4367
4368 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)
4369 phba->mbox_ext_buf_ctx.mbx_dmabuf->virt;
4370
4371 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
4372 if (!dd_data) {
4373 rc = -ENOMEM;
4374 goto job_error;
4375 }
4376
4377 pbuf = (uint8_t *)dmabuf->virt;
4378 size = job->request_payload.payload_len;
4379 sg_copy_to_buffer(job->request_payload.sg_list,
4380 job->request_payload.sg_cnt,
4381 pbuf, size);
4382
4383 if (phba->mbox_ext_buf_ctx.nembType == nemb_mse) {
4384 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4385 "2966 SLI_CONFIG (mse) ext-buffer wr set "
4386 "buffer[%d], size:%d\n",
4387 phba->mbox_ext_buf_ctx.seqNum, size);
4388
4389 } else {
4390 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4391 "2967 SLI_CONFIG (hbd) ext-buffer wr set "
4392 "buffer[%d], size:%d\n",
4393 phba->mbox_ext_buf_ctx.seqNum, size);
4394
4395 }
4396
4397 /* set up external buffer descriptor and add to external buffer list */
4398 lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, index,
4399 phba->mbox_ext_buf_ctx.mbx_dmabuf,
4400 dmabuf);
4401 list_add_tail(&dmabuf->list, &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
4402
James Smartb76f2dc2011-07-22 18:37:42 -04004403 /* after write dma buffer */
4404 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, phba->mbox_ext_buf_ctx.nembType,
4405 mbox_wr, dma_ebuf, sta_pos_addr,
4406 dmabuf, index);
4407
James Smart7ad20aa2011-05-24 11:44:28 -04004408 if (phba->mbox_ext_buf_ctx.seqNum == phba->mbox_ext_buf_ctx.numBuf) {
4409 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4410 "2968 SLI_CONFIG ext-buffer wr all %d "
4411 "ebuffers received\n",
4412 phba->mbox_ext_buf_ctx.numBuf);
4413 /* mailbox command structure for base driver */
4414 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4415 if (!pmboxq) {
4416 rc = -ENOMEM;
4417 goto job_error;
4418 }
4419 memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
4420 pbuf = (uint8_t *)phba->mbox_ext_buf_ctx.mbx_dmabuf->virt;
4421 pmb = &pmboxq->u.mb;
4422 memcpy(pmb, pbuf, sizeof(*pmb));
4423 pmb->mbxOwner = OWN_HOST;
4424 pmboxq->vport = phba->pport;
4425
4426 /* callback for multi-buffer write mailbox command */
4427 pmboxq->mbox_cmpl = lpfc_bsg_issue_write_mbox_ext_cmpl;
4428
4429 /* context fields to callback function */
4430 pmboxq->context1 = dd_data;
4431 dd_data->type = TYPE_MBOX;
James Smarta33c4f72013-03-01 16:36:00 -05004432 dd_data->set_job = job;
James Smart7ad20aa2011-05-24 11:44:28 -04004433 dd_data->context_un.mbox.pmboxq = pmboxq;
4434 dd_data->context_un.mbox.mb = (MAILBOX_t *)pbuf;
James Smart7ad20aa2011-05-24 11:44:28 -04004435 job->dd_data = dd_data;
4436
4437 /* state change */
4438 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
4439
4440 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
4441 if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
4442 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4443 "2969 Issued SLI_CONFIG ext-buffer "
4444 "maibox command, rc:x%x\n", rc);
James Smart88a2cfb2011-07-22 18:36:33 -04004445 return SLI_CONFIG_HANDLED;
James Smart7ad20aa2011-05-24 11:44:28 -04004446 }
4447 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4448 "2970 Failed to issue SLI_CONFIG ext-buffer "
4449 "maibox command, rc:x%x\n", rc);
4450 rc = -EPIPE;
4451 goto job_error;
4452 }
4453
4454 /* wait for additoinal external buffers */
4455 job->reply->result = 0;
4456 job->job_done(job);
4457 return SLI_CONFIG_HANDLED;
4458
4459job_error:
4460 lpfc_bsg_dma_page_free(phba, dmabuf);
4461 kfree(dd_data);
4462
4463 return rc;
4464}
4465
4466/**
4467 * lpfc_bsg_handle_sli_cfg_ebuf - handle ext buffer with sli-cfg mailbox cmd
4468 * @phba: Pointer to HBA context object.
4469 * @mb: Pointer to a BSG mailbox object.
4470 * @dmabuff: Pointer to a DMA buffer descriptor.
4471 *
4472 * This routine handles the external buffer with SLI_CONFIG (0x9B) mailbox
4473 * command with multiple non-embedded external buffers.
4474 **/
4475static int
4476lpfc_bsg_handle_sli_cfg_ebuf(struct lpfc_hba *phba, struct fc_bsg_job *job,
4477 struct lpfc_dmabuf *dmabuf)
4478{
4479 int rc;
4480
4481 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4482 "2971 SLI_CONFIG buffer (type:x%x)\n",
4483 phba->mbox_ext_buf_ctx.mboxType);
4484
4485 if (phba->mbox_ext_buf_ctx.mboxType == mbox_rd) {
4486 if (phba->mbox_ext_buf_ctx.state != LPFC_BSG_MBOX_DONE) {
4487 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4488 "2972 SLI_CONFIG rd buffer state "
4489 "mismatch:x%x\n",
4490 phba->mbox_ext_buf_ctx.state);
4491 lpfc_bsg_mbox_ext_abort(phba);
4492 return -EPIPE;
4493 }
4494 rc = lpfc_bsg_read_ebuf_get(phba, job);
4495 if (rc == SLI_CONFIG_HANDLED)
4496 lpfc_bsg_dma_page_free(phba, dmabuf);
4497 } else { /* phba->mbox_ext_buf_ctx.mboxType == mbox_wr */
4498 if (phba->mbox_ext_buf_ctx.state != LPFC_BSG_MBOX_HOST) {
4499 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4500 "2973 SLI_CONFIG wr buffer state "
4501 "mismatch:x%x\n",
4502 phba->mbox_ext_buf_ctx.state);
4503 lpfc_bsg_mbox_ext_abort(phba);
4504 return -EPIPE;
4505 }
4506 rc = lpfc_bsg_write_ebuf_set(phba, job, dmabuf);
4507 }
4508 return rc;
4509}
4510
4511/**
4512 * lpfc_bsg_handle_sli_cfg_ext - handle sli-cfg mailbox with external buffer
4513 * @phba: Pointer to HBA context object.
4514 * @mb: Pointer to a BSG mailbox object.
4515 * @dmabuff: Pointer to a DMA buffer descriptor.
4516 *
4517 * This routine checkes and handles non-embedded multi-buffer SLI_CONFIG
4518 * (0x9B) mailbox commands and external buffers.
4519 **/
4520static int
4521lpfc_bsg_handle_sli_cfg_ext(struct lpfc_hba *phba, struct fc_bsg_job *job,
4522 struct lpfc_dmabuf *dmabuf)
4523{
4524 struct dfc_mbox_req *mbox_req;
James Smart88a2cfb2011-07-22 18:36:33 -04004525 int rc = SLI_CONFIG_NOT_HANDLED;
James Smart7ad20aa2011-05-24 11:44:28 -04004526
4527 mbox_req =
4528 (struct dfc_mbox_req *)job->request->rqst_data.h_vendor.vendor_cmd;
4529
4530 /* mbox command with/without single external buffer */
4531 if (mbox_req->extMboxTag == 0 && mbox_req->extSeqNum == 0)
James Smart88a2cfb2011-07-22 18:36:33 -04004532 return rc;
James Smart7ad20aa2011-05-24 11:44:28 -04004533
4534 /* mbox command and first external buffer */
4535 if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_IDLE) {
4536 if (mbox_req->extSeqNum == 1) {
4537 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4538 "2974 SLI_CONFIG mailbox: tag:%d, "
4539 "seq:%d\n", mbox_req->extMboxTag,
4540 mbox_req->extSeqNum);
4541 rc = lpfc_bsg_handle_sli_cfg_mbox(phba, job, dmabuf);
4542 return rc;
4543 } else
4544 goto sli_cfg_ext_error;
4545 }
4546
4547 /*
4548 * handle additional external buffers
4549 */
4550
4551 /* check broken pipe conditions */
4552 if (mbox_req->extMboxTag != phba->mbox_ext_buf_ctx.mbxTag)
4553 goto sli_cfg_ext_error;
4554 if (mbox_req->extSeqNum > phba->mbox_ext_buf_ctx.numBuf)
4555 goto sli_cfg_ext_error;
4556 if (mbox_req->extSeqNum != phba->mbox_ext_buf_ctx.seqNum + 1)
4557 goto sli_cfg_ext_error;
4558
4559 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4560 "2975 SLI_CONFIG mailbox external buffer: "
4561 "extSta:x%x, tag:%d, seq:%d\n",
4562 phba->mbox_ext_buf_ctx.state, mbox_req->extMboxTag,
4563 mbox_req->extSeqNum);
4564 rc = lpfc_bsg_handle_sli_cfg_ebuf(phba, job, dmabuf);
4565 return rc;
4566
4567sli_cfg_ext_error:
4568 /* all other cases, broken pipe */
4569 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4570 "2976 SLI_CONFIG mailbox broken pipe: "
4571 "ctxSta:x%x, ctxNumBuf:%d "
4572 "ctxTag:%d, ctxSeq:%d, tag:%d, seq:%d\n",
4573 phba->mbox_ext_buf_ctx.state,
4574 phba->mbox_ext_buf_ctx.numBuf,
4575 phba->mbox_ext_buf_ctx.mbxTag,
4576 phba->mbox_ext_buf_ctx.seqNum,
4577 mbox_req->extMboxTag, mbox_req->extSeqNum);
4578
4579 lpfc_bsg_mbox_ext_session_reset(phba);
4580
4581 return -EPIPE;
4582}
4583
4584/**
James Smart3b5dd522010-01-26 23:10:15 -05004585 * lpfc_bsg_issue_mbox - issues a mailbox command on behalf of an app
4586 * @phba: Pointer to HBA context object.
4587 * @mb: Pointer to a mailbox object.
4588 * @vport: Pointer to a vport object.
4589 *
4590 * Allocate a tracking object, mailbox command memory, get a mailbox
4591 * from the mailbox pool, copy the caller mailbox command.
4592 *
4593 * If offline and the sli is active we need to poll for the command (port is
4594 * being reset) and com-plete the job, otherwise issue the mailbox command and
4595 * let our completion handler finish the command.
4596 **/
James Smarta2fc4aef2014-09-03 12:57:55 -04004597static int
James Smart3b5dd522010-01-26 23:10:15 -05004598lpfc_bsg_issue_mbox(struct lpfc_hba *phba, struct fc_bsg_job *job,
4599 struct lpfc_vport *vport)
4600{
James Smart7a470272010-03-15 11:25:20 -04004601 LPFC_MBOXQ_t *pmboxq = NULL; /* internal mailbox queue */
4602 MAILBOX_t *pmb; /* shortcut to the pmboxq mailbox */
4603 /* a 4k buffer to hold the mb and extended data from/to the bsg */
James Smart7ad20aa2011-05-24 11:44:28 -04004604 uint8_t *pmbx = NULL;
James Smart7a470272010-03-15 11:25:20 -04004605 struct bsg_job_data *dd_data = NULL; /* bsg data tracking structure */
James Smart7ad20aa2011-05-24 11:44:28 -04004606 struct lpfc_dmabuf *dmabuf = NULL;
4607 struct dfc_mbox_req *mbox_req;
James Smartb6e3b9c2011-04-16 11:03:43 -04004608 struct READ_EVENT_LOG_VAR *rdEventLog;
4609 uint32_t transmit_length, receive_length, mode;
James Smart7ad20aa2011-05-24 11:44:28 -04004610 struct lpfc_mbx_sli4_config *sli4_config;
James Smartb6e3b9c2011-04-16 11:03:43 -04004611 struct lpfc_mbx_nembed_cmd *nembed_sge;
4612 struct mbox_header *header;
4613 struct ulp_bde64 *bde;
James Smart7a470272010-03-15 11:25:20 -04004614 uint8_t *ext = NULL;
James Smart3b5dd522010-01-26 23:10:15 -05004615 int rc = 0;
James Smart7a470272010-03-15 11:25:20 -04004616 uint8_t *from;
James Smart7ad20aa2011-05-24 11:44:28 -04004617 uint32_t size;
4618
James Smart7a470272010-03-15 11:25:20 -04004619 /* in case no data is transferred */
4620 job->reply->reply_payload_rcv_len = 0;
4621
James Smartb6e3b9c2011-04-16 11:03:43 -04004622 /* sanity check to protect driver */
4623 if (job->reply_payload.payload_len > BSG_MBOX_SIZE ||
4624 job->request_payload.payload_len > BSG_MBOX_SIZE) {
4625 rc = -ERANGE;
4626 goto job_done;
4627 }
4628
James Smart7ad20aa2011-05-24 11:44:28 -04004629 /*
4630 * Don't allow mailbox commands to be sent when blocked or when in
4631 * the middle of discovery
4632 */
4633 if (phba->sli.sli_flag & LPFC_BLOCK_MGMT_IO) {
4634 rc = -EAGAIN;
4635 goto job_done;
4636 }
4637
4638 mbox_req =
4639 (struct dfc_mbox_req *)job->request->rqst_data.h_vendor.vendor_cmd;
4640
James Smart7a470272010-03-15 11:25:20 -04004641 /* check if requested extended data lengths are valid */
James Smartb6e3b9c2011-04-16 11:03:43 -04004642 if ((mbox_req->inExtWLen > BSG_MBOX_SIZE/sizeof(uint32_t)) ||
4643 (mbox_req->outExtWLen > BSG_MBOX_SIZE/sizeof(uint32_t))) {
James Smart7a470272010-03-15 11:25:20 -04004644 rc = -ERANGE;
4645 goto job_done;
4646 }
James Smart3b5dd522010-01-26 23:10:15 -05004647
James Smart7ad20aa2011-05-24 11:44:28 -04004648 dmabuf = lpfc_bsg_dma_page_alloc(phba);
4649 if (!dmabuf || !dmabuf->virt) {
4650 rc = -ENOMEM;
4651 goto job_done;
4652 }
4653
4654 /* Get the mailbox command or external buffer from BSG */
4655 pmbx = (uint8_t *)dmabuf->virt;
4656 size = job->request_payload.payload_len;
4657 sg_copy_to_buffer(job->request_payload.sg_list,
4658 job->request_payload.sg_cnt, pmbx, size);
4659
4660 /* Handle possible SLI_CONFIG with non-embedded payloads */
4661 if (phba->sli_rev == LPFC_SLI_REV4) {
4662 rc = lpfc_bsg_handle_sli_cfg_ext(phba, job, dmabuf);
4663 if (rc == SLI_CONFIG_HANDLED)
4664 goto job_cont;
4665 if (rc)
4666 goto job_done;
4667 /* SLI_CONFIG_NOT_HANDLED for other mailbox commands */
4668 }
4669
4670 rc = lpfc_bsg_check_cmd_access(phba, (MAILBOX_t *)pmbx, vport);
4671 if (rc != 0)
4672 goto job_done; /* must be negative */
4673
James Smart3b5dd522010-01-26 23:10:15 -05004674 /* allocate our bsg tracking structure */
4675 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
4676 if (!dd_data) {
4677 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
4678 "2727 Failed allocation of dd_data\n");
James Smart7a470272010-03-15 11:25:20 -04004679 rc = -ENOMEM;
4680 goto job_done;
James Smart3b5dd522010-01-26 23:10:15 -05004681 }
4682
James Smart3b5dd522010-01-26 23:10:15 -05004683 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4684 if (!pmboxq) {
James Smart7a470272010-03-15 11:25:20 -04004685 rc = -ENOMEM;
4686 goto job_done;
James Smart3b5dd522010-01-26 23:10:15 -05004687 }
James Smart7a470272010-03-15 11:25:20 -04004688 memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
James Smart3b5dd522010-01-26 23:10:15 -05004689
James Smart3b5dd522010-01-26 23:10:15 -05004690 pmb = &pmboxq->u.mb;
James Smart7ad20aa2011-05-24 11:44:28 -04004691 memcpy(pmb, pmbx, sizeof(*pmb));
James Smart3b5dd522010-01-26 23:10:15 -05004692 pmb->mbxOwner = OWN_HOST;
James Smart3b5dd522010-01-26 23:10:15 -05004693 pmboxq->vport = vport;
4694
James Smartc7495932010-04-06 15:05:28 -04004695 /* If HBA encountered an error attention, allow only DUMP
4696 * or RESTART mailbox commands until the HBA is restarted.
4697 */
4698 if (phba->pport->stopped &&
4699 pmb->mbxCommand != MBX_DUMP_MEMORY &&
4700 pmb->mbxCommand != MBX_RESTART &&
4701 pmb->mbxCommand != MBX_WRITE_VPARMS &&
4702 pmb->mbxCommand != MBX_WRITE_WWN)
4703 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
4704 "2797 mbox: Issued mailbox cmd "
4705 "0x%x while in stopped state.\n",
4706 pmb->mbxCommand);
4707
James Smart7a470272010-03-15 11:25:20 -04004708 /* extended mailbox commands will need an extended buffer */
James Smartc7495932010-04-06 15:05:28 -04004709 if (mbox_req->inExtWLen || mbox_req->outExtWLen) {
James Smart026abb82011-12-13 13:20:45 -05004710 from = pmbx;
4711 ext = from + sizeof(MAILBOX_t);
James Smart7a470272010-03-15 11:25:20 -04004712 pmboxq->context2 = ext;
4713 pmboxq->in_ext_byte_len =
James Smart7a470272010-03-15 11:25:20 -04004714 mbox_req->inExtWLen * sizeof(uint32_t);
4715 pmboxq->out_ext_byte_len =
James Smartc7495932010-04-06 15:05:28 -04004716 mbox_req->outExtWLen * sizeof(uint32_t);
James Smart7a470272010-03-15 11:25:20 -04004717 pmboxq->mbox_offset_word = mbox_req->mbOffset;
4718 }
4719
4720 /* biu diag will need a kernel buffer to transfer the data
4721 * allocate our own buffer and setup the mailbox command to
4722 * use ours
4723 */
4724 if (pmb->mbxCommand == MBX_RUN_BIU_DIAG64) {
James Smartb6e3b9c2011-04-16 11:03:43 -04004725 transmit_length = pmb->un.varWords[1];
4726 receive_length = pmb->un.varWords[4];
James Smartc7495932010-04-06 15:05:28 -04004727 /* transmit length cannot be greater than receive length or
4728 * mailbox extension size
4729 */
4730 if ((transmit_length > receive_length) ||
James Smart88a2cfb2011-07-22 18:36:33 -04004731 (transmit_length > BSG_MBOX_SIZE - sizeof(MAILBOX_t))) {
James Smartc7495932010-04-06 15:05:28 -04004732 rc = -ERANGE;
4733 goto job_done;
4734 }
James Smart7a470272010-03-15 11:25:20 -04004735 pmb->un.varBIUdiag.un.s2.xmit_bde64.addrHigh =
James Smart7ad20aa2011-05-24 11:44:28 -04004736 putPaddrHigh(dmabuf->phys + sizeof(MAILBOX_t));
James Smart7a470272010-03-15 11:25:20 -04004737 pmb->un.varBIUdiag.un.s2.xmit_bde64.addrLow =
James Smart7ad20aa2011-05-24 11:44:28 -04004738 putPaddrLow(dmabuf->phys + sizeof(MAILBOX_t));
James Smart7a470272010-03-15 11:25:20 -04004739
4740 pmb->un.varBIUdiag.un.s2.rcv_bde64.addrHigh =
James Smart7ad20aa2011-05-24 11:44:28 -04004741 putPaddrHigh(dmabuf->phys + sizeof(MAILBOX_t)
4742 + pmb->un.varBIUdiag.un.s2.xmit_bde64.tus.f.bdeSize);
James Smart7a470272010-03-15 11:25:20 -04004743 pmb->un.varBIUdiag.un.s2.rcv_bde64.addrLow =
James Smart7ad20aa2011-05-24 11:44:28 -04004744 putPaddrLow(dmabuf->phys + sizeof(MAILBOX_t)
4745 + pmb->un.varBIUdiag.un.s2.xmit_bde64.tus.f.bdeSize);
James Smartc7495932010-04-06 15:05:28 -04004746 } else if (pmb->mbxCommand == MBX_READ_EVENT_LOG) {
James Smartb6e3b9c2011-04-16 11:03:43 -04004747 rdEventLog = &pmb->un.varRdEventLog;
4748 receive_length = rdEventLog->rcv_bde64.tus.f.bdeSize;
4749 mode = bf_get(lpfc_event_log, rdEventLog);
James Smartc7495932010-04-06 15:05:28 -04004750
4751 /* receive length cannot be greater than mailbox
4752 * extension size
4753 */
James Smart88a2cfb2011-07-22 18:36:33 -04004754 if (receive_length > BSG_MBOX_SIZE - sizeof(MAILBOX_t)) {
James Smartc7495932010-04-06 15:05:28 -04004755 rc = -ERANGE;
4756 goto job_done;
4757 }
4758
4759 /* mode zero uses a bde like biu diags command */
4760 if (mode == 0) {
James Smart7ad20aa2011-05-24 11:44:28 -04004761 pmb->un.varWords[3] = putPaddrLow(dmabuf->phys
4762 + sizeof(MAILBOX_t));
4763 pmb->un.varWords[4] = putPaddrHigh(dmabuf->phys
4764 + sizeof(MAILBOX_t));
James Smartc7495932010-04-06 15:05:28 -04004765 }
4766 } else if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart3ef6d242012-01-18 16:23:48 -05004767 /* Let type 4 (well known data) through because the data is
4768 * returned in varwords[4-8]
4769 * otherwise check the recieve length and fetch the buffer addr
4770 */
4771 if ((pmb->mbxCommand == MBX_DUMP_MEMORY) &&
4772 (pmb->un.varDmp.type != DMP_WELL_KNOWN)) {
James Smartc7495932010-04-06 15:05:28 -04004773 /* rebuild the command for sli4 using our own buffers
4774 * like we do for biu diags
4775 */
James Smartb6e3b9c2011-04-16 11:03:43 -04004776 receive_length = pmb->un.varWords[2];
James Smartc7495932010-04-06 15:05:28 -04004777 /* receive length cannot be greater than mailbox
4778 * extension size
4779 */
James Smart7ad20aa2011-05-24 11:44:28 -04004780 if (receive_length == 0) {
James Smartc7495932010-04-06 15:05:28 -04004781 rc = -ERANGE;
4782 goto job_done;
4783 }
James Smart7ad20aa2011-05-24 11:44:28 -04004784 pmb->un.varWords[3] = putPaddrLow(dmabuf->phys
4785 + sizeof(MAILBOX_t));
4786 pmb->un.varWords[4] = putPaddrHigh(dmabuf->phys
4787 + sizeof(MAILBOX_t));
James Smartc7495932010-04-06 15:05:28 -04004788 } else if ((pmb->mbxCommand == MBX_UPDATE_CFG) &&
4789 pmb->un.varUpdateCfg.co) {
James Smartb6e3b9c2011-04-16 11:03:43 -04004790 bde = (struct ulp_bde64 *)&pmb->un.varWords[4];
James Smartc7495932010-04-06 15:05:28 -04004791
4792 /* bde size cannot be greater than mailbox ext size */
James Smart88a2cfb2011-07-22 18:36:33 -04004793 if (bde->tus.f.bdeSize >
4794 BSG_MBOX_SIZE - sizeof(MAILBOX_t)) {
James Smartc7495932010-04-06 15:05:28 -04004795 rc = -ERANGE;
4796 goto job_done;
4797 }
James Smart7ad20aa2011-05-24 11:44:28 -04004798 bde->addrHigh = putPaddrHigh(dmabuf->phys
4799 + sizeof(MAILBOX_t));
4800 bde->addrLow = putPaddrLow(dmabuf->phys
4801 + sizeof(MAILBOX_t));
James Smart515e0aa2010-09-29 11:19:00 -04004802 } else if (pmb->mbxCommand == MBX_SLI4_CONFIG) {
James Smart7ad20aa2011-05-24 11:44:28 -04004803 /* Handling non-embedded SLI_CONFIG mailbox command */
4804 sli4_config = &pmboxq->u.mqe.un.sli4_config;
4805 if (!bf_get(lpfc_mbox_hdr_emb,
4806 &sli4_config->header.cfg_mhdr)) {
4807 /* rebuild the command for sli4 using our
4808 * own buffers like we do for biu diags
4809 */
4810 header = (struct mbox_header *)
4811 &pmb->un.varWords[0];
4812 nembed_sge = (struct lpfc_mbx_nembed_cmd *)
4813 &pmb->un.varWords[0];
4814 receive_length = nembed_sge->sge[0].length;
James Smart515e0aa2010-09-29 11:19:00 -04004815
James Smart7ad20aa2011-05-24 11:44:28 -04004816 /* receive length cannot be greater than
4817 * mailbox extension size
4818 */
4819 if ((receive_length == 0) ||
James Smart88a2cfb2011-07-22 18:36:33 -04004820 (receive_length >
4821 BSG_MBOX_SIZE - sizeof(MAILBOX_t))) {
James Smart7ad20aa2011-05-24 11:44:28 -04004822 rc = -ERANGE;
4823 goto job_done;
4824 }
4825
4826 nembed_sge->sge[0].pa_hi =
4827 putPaddrHigh(dmabuf->phys
4828 + sizeof(MAILBOX_t));
4829 nembed_sge->sge[0].pa_lo =
4830 putPaddrLow(dmabuf->phys
4831 + sizeof(MAILBOX_t));
James Smart515e0aa2010-09-29 11:19:00 -04004832 }
James Smartc7495932010-04-06 15:05:28 -04004833 }
James Smart3b5dd522010-01-26 23:10:15 -05004834 }
4835
James Smart7ad20aa2011-05-24 11:44:28 -04004836 dd_data->context_un.mbox.dmabuffers = dmabuf;
James Smartc7495932010-04-06 15:05:28 -04004837
James Smart3b5dd522010-01-26 23:10:15 -05004838 /* setup wake call as IOCB callback */
James Smart7ad20aa2011-05-24 11:44:28 -04004839 pmboxq->mbox_cmpl = lpfc_bsg_issue_mbox_cmpl;
James Smart7a470272010-03-15 11:25:20 -04004840
James Smart3b5dd522010-01-26 23:10:15 -05004841 /* setup context field to pass wait_queue pointer to wake function */
4842 pmboxq->context1 = dd_data;
4843 dd_data->type = TYPE_MBOX;
James Smarta33c4f72013-03-01 16:36:00 -05004844 dd_data->set_job = job;
James Smart3b5dd522010-01-26 23:10:15 -05004845 dd_data->context_un.mbox.pmboxq = pmboxq;
James Smart7ad20aa2011-05-24 11:44:28 -04004846 dd_data->context_un.mbox.mb = (MAILBOX_t *)pmbx;
James Smart7a470272010-03-15 11:25:20 -04004847 dd_data->context_un.mbox.ext = ext;
4848 dd_data->context_un.mbox.mbOffset = mbox_req->mbOffset;
4849 dd_data->context_un.mbox.inExtWLen = mbox_req->inExtWLen;
James Smartc7495932010-04-06 15:05:28 -04004850 dd_data->context_un.mbox.outExtWLen = mbox_req->outExtWLen;
James Smart3b5dd522010-01-26 23:10:15 -05004851 job->dd_data = dd_data;
James Smart7a470272010-03-15 11:25:20 -04004852
4853 if ((vport->fc_flag & FC_OFFLINE_MODE) ||
4854 (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))) {
4855 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
4856 if (rc != MBX_SUCCESS) {
4857 rc = (rc == MBX_TIMEOUT) ? -ETIME : -ENODEV;
4858 goto job_done;
4859 }
4860
4861 /* job finished, copy the data */
James Smart7ad20aa2011-05-24 11:44:28 -04004862 memcpy(pmbx, pmb, sizeof(*pmb));
James Smart7a470272010-03-15 11:25:20 -04004863 job->reply->reply_payload_rcv_len =
4864 sg_copy_from_buffer(job->reply_payload.sg_list,
James Smart7ad20aa2011-05-24 11:44:28 -04004865 job->reply_payload.sg_cnt,
4866 pmbx, size);
James Smart7a470272010-03-15 11:25:20 -04004867 /* not waiting mbox already done */
4868 rc = 0;
4869 goto job_done;
James Smart3b5dd522010-01-26 23:10:15 -05004870 }
4871
James Smart7a470272010-03-15 11:25:20 -04004872 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
4873 if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY))
4874 return 1; /* job started */
4875
4876job_done:
4877 /* common exit for error or job completed inline */
James Smart7a470272010-03-15 11:25:20 -04004878 if (pmboxq)
4879 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart7ad20aa2011-05-24 11:44:28 -04004880 lpfc_bsg_dma_page_free(phba, dmabuf);
James Smart7a470272010-03-15 11:25:20 -04004881 kfree(dd_data);
4882
James Smart7ad20aa2011-05-24 11:44:28 -04004883job_cont:
James Smart7a470272010-03-15 11:25:20 -04004884 return rc;
James Smart3b5dd522010-01-26 23:10:15 -05004885}
4886
4887/**
4888 * lpfc_bsg_mbox_cmd - process an fc bsg LPFC_BSG_VENDOR_MBOX command
4889 * @job: MBOX fc_bsg_job for LPFC_BSG_VENDOR_MBOX.
4890 **/
4891static int
4892lpfc_bsg_mbox_cmd(struct fc_bsg_job *job)
4893{
4894 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
4895 struct lpfc_hba *phba = vport->phba;
James Smart7ad20aa2011-05-24 11:44:28 -04004896 struct dfc_mbox_req *mbox_req;
James Smart3b5dd522010-01-26 23:10:15 -05004897 int rc = 0;
4898
James Smart7ad20aa2011-05-24 11:44:28 -04004899 /* mix-and-match backward compatibility */
James Smart3b5dd522010-01-26 23:10:15 -05004900 job->reply->reply_payload_rcv_len = 0;
4901 if (job->request_len <
4902 sizeof(struct fc_bsg_request) + sizeof(struct dfc_mbox_req)) {
James Smart7ad20aa2011-05-24 11:44:28 -04004903 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
Masanari Iida1051e9b2013-03-31 01:23:50 +09004904 "2737 Mix-and-match backward compatibility "
James Smart7ad20aa2011-05-24 11:44:28 -04004905 "between MBOX_REQ old size:%d and "
4906 "new request size:%d\n",
4907 (int)(job->request_len -
4908 sizeof(struct fc_bsg_request)),
4909 (int)sizeof(struct dfc_mbox_req));
4910 mbox_req = (struct dfc_mbox_req *)
4911 job->request->rqst_data.h_vendor.vendor_cmd;
4912 mbox_req->extMboxTag = 0;
4913 mbox_req->extSeqNum = 0;
James Smart3b5dd522010-01-26 23:10:15 -05004914 }
4915
4916 rc = lpfc_bsg_issue_mbox(phba, job, vport);
4917
James Smart3b5dd522010-01-26 23:10:15 -05004918 if (rc == 0) {
4919 /* job done */
4920 job->reply->result = 0;
4921 job->dd_data = NULL;
4922 job->job_done(job);
4923 } else if (rc == 1)
4924 /* job submitted, will complete later*/
4925 rc = 0; /* return zero, no error */
4926 else {
4927 /* some error occurred */
4928 job->reply->result = rc;
4929 job->dd_data = NULL;
4930 }
4931
4932 return rc;
4933}
4934
4935/**
James Smarte2aed292010-02-26 14:15:00 -05004936 * lpfc_bsg_menlo_cmd_cmp - lpfc_menlo_cmd completion handler
4937 * @phba: Pointer to HBA context object.
4938 * @cmdiocbq: Pointer to command iocb.
4939 * @rspiocbq: Pointer to response iocb.
4940 *
4941 * This function is the completion handler for iocbs issued using
4942 * lpfc_menlo_cmd function. This function is called by the
4943 * ring event handler function without any lock held. This function
4944 * can be called from both worker thread context and interrupt
4945 * context. This function also can be called from another thread which
4946 * cleans up the SLI layer objects.
4947 * This function copies the contents of the response iocb to the
4948 * response iocb memory object provided by the caller of
4949 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
4950 * sleeps for the iocb completion.
4951 **/
4952static void
4953lpfc_bsg_menlo_cmd_cmp(struct lpfc_hba *phba,
4954 struct lpfc_iocbq *cmdiocbq,
4955 struct lpfc_iocbq *rspiocbq)
4956{
4957 struct bsg_job_data *dd_data;
4958 struct fc_bsg_job *job;
4959 IOCB_t *rsp;
James Smarta33c4f72013-03-01 16:36:00 -05004960 struct lpfc_dmabuf *bmp, *cmp, *rmp;
James Smarte2aed292010-02-26 14:15:00 -05004961 struct lpfc_bsg_menlo *menlo;
4962 unsigned long flags;
4963 struct menlo_response *menlo_resp;
James Smarta33c4f72013-03-01 16:36:00 -05004964 unsigned int rsp_size;
James Smarte2aed292010-02-26 14:15:00 -05004965 int rc = 0;
4966
James Smarte2aed292010-02-26 14:15:00 -05004967 dd_data = cmdiocbq->context1;
James Smarta33c4f72013-03-01 16:36:00 -05004968 cmp = cmdiocbq->context2;
4969 bmp = cmdiocbq->context3;
James Smarte2aed292010-02-26 14:15:00 -05004970 menlo = &dd_data->context_un.menlo;
James Smarta33c4f72013-03-01 16:36:00 -05004971 rmp = menlo->rmp;
James Smarte2aed292010-02-26 14:15:00 -05004972 rsp = &rspiocbq->iocb;
4973
James Smarta33c4f72013-03-01 16:36:00 -05004974 /* Determine if job has been aborted */
4975 spin_lock_irqsave(&phba->ct_ev_lock, flags);
4976 job = dd_data->set_job;
4977 if (job) {
4978 /* Prevent timeout handling from trying to abort job */
4979 job->dd_data = NULL;
4980 }
4981 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smarte2aed292010-02-26 14:15:00 -05004982
James Smarta33c4f72013-03-01 16:36:00 -05004983 /* Copy the job data or set the failing status for the job */
4984
4985 if (job) {
4986 /* always return the xri, this would be used in the case
4987 * of a menlo download to allow the data to be sent as a
4988 * continuation of the exchange.
4989 */
4990
4991 menlo_resp = (struct menlo_response *)
4992 job->reply->reply_data.vendor_reply.vendor_rsp;
4993 menlo_resp->xri = rsp->ulpContext;
4994 if (rsp->ulpStatus) {
4995 if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
4996 switch (rsp->un.ulpWord[4] & IOERR_PARAM_MASK) {
4997 case IOERR_SEQUENCE_TIMEOUT:
4998 rc = -ETIMEDOUT;
4999 break;
5000 case IOERR_INVALID_RPI:
5001 rc = -EFAULT;
5002 break;
5003 default:
5004 rc = -EACCES;
5005 break;
5006 }
5007 } else {
James Smarte2aed292010-02-26 14:15:00 -05005008 rc = -EACCES;
James Smarte2aed292010-02-26 14:15:00 -05005009 }
James Smarta33c4f72013-03-01 16:36:00 -05005010 } else {
5011 rsp_size = rsp->un.genreq64.bdl.bdeSize;
5012 job->reply->reply_payload_rcv_len =
5013 lpfc_bsg_copy_data(rmp, &job->reply_payload,
5014 rsp_size, 0);
5015 }
James Smarte2aed292010-02-26 14:15:00 -05005016
James Smarta33c4f72013-03-01 16:36:00 -05005017 }
5018
James Smarte2aed292010-02-26 14:15:00 -05005019 lpfc_sli_release_iocbq(phba, cmdiocbq);
James Smarta33c4f72013-03-01 16:36:00 -05005020 lpfc_free_bsg_buffers(phba, cmp);
5021 lpfc_free_bsg_buffers(phba, rmp);
5022 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
James Smarte2aed292010-02-26 14:15:00 -05005023 kfree(bmp);
5024 kfree(dd_data);
James Smarta33c4f72013-03-01 16:36:00 -05005025
5026 /* Complete the job if active */
5027
5028 if (job) {
5029 job->reply->result = rc;
5030 job->job_done(job);
5031 }
5032
James Smarte2aed292010-02-26 14:15:00 -05005033 return;
5034}
5035
5036/**
5037 * lpfc_menlo_cmd - send an ioctl for menlo hardware
5038 * @job: fc_bsg_job to handle
5039 *
5040 * This function issues a gen request 64 CR ioctl for all menlo cmd requests,
5041 * all the command completions will return the xri for the command.
5042 * For menlo data requests a gen request 64 CX is used to continue the exchange
5043 * supplied in the menlo request header xri field.
5044 **/
5045static int
5046lpfc_menlo_cmd(struct fc_bsg_job *job)
5047{
5048 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
5049 struct lpfc_hba *phba = vport->phba;
James Smarta33c4f72013-03-01 16:36:00 -05005050 struct lpfc_iocbq *cmdiocbq;
5051 IOCB_t *cmd;
James Smarte2aed292010-02-26 14:15:00 -05005052 int rc = 0;
5053 struct menlo_command *menlo_cmd;
5054 struct menlo_response *menlo_resp;
James Smarta33c4f72013-03-01 16:36:00 -05005055 struct lpfc_dmabuf *bmp = NULL, *cmp = NULL, *rmp = NULL;
James Smarte2aed292010-02-26 14:15:00 -05005056 int request_nseg;
5057 int reply_nseg;
James Smarte2aed292010-02-26 14:15:00 -05005058 struct bsg_job_data *dd_data;
5059 struct ulp_bde64 *bpl = NULL;
5060
5061 /* in case no data is returned return just the return code */
5062 job->reply->reply_payload_rcv_len = 0;
5063
5064 if (job->request_len <
5065 sizeof(struct fc_bsg_request) +
5066 sizeof(struct menlo_command)) {
5067 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5068 "2784 Received MENLO_CMD request below "
5069 "minimum size\n");
5070 rc = -ERANGE;
5071 goto no_dd_data;
5072 }
5073
5074 if (job->reply_len <
5075 sizeof(struct fc_bsg_request) + sizeof(struct menlo_response)) {
5076 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5077 "2785 Received MENLO_CMD reply below "
5078 "minimum size\n");
5079 rc = -ERANGE;
5080 goto no_dd_data;
5081 }
5082
5083 if (!(phba->menlo_flag & HBA_MENLO_SUPPORT)) {
5084 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5085 "2786 Adapter does not support menlo "
5086 "commands\n");
5087 rc = -EPERM;
5088 goto no_dd_data;
5089 }
5090
5091 menlo_cmd = (struct menlo_command *)
5092 job->request->rqst_data.h_vendor.vendor_cmd;
5093
5094 menlo_resp = (struct menlo_response *)
5095 job->reply->reply_data.vendor_reply.vendor_rsp;
5096
5097 /* allocate our bsg tracking structure */
5098 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
5099 if (!dd_data) {
5100 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5101 "2787 Failed allocation of dd_data\n");
5102 rc = -ENOMEM;
5103 goto no_dd_data;
5104 }
5105
5106 bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5107 if (!bmp) {
5108 rc = -ENOMEM;
5109 goto free_dd;
5110 }
5111
James Smarta33c4f72013-03-01 16:36:00 -05005112 bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
5113 if (!bmp->virt) {
James Smarte2aed292010-02-26 14:15:00 -05005114 rc = -ENOMEM;
5115 goto free_bmp;
5116 }
5117
James Smarte2aed292010-02-26 14:15:00 -05005118 INIT_LIST_HEAD(&bmp->list);
James Smarta33c4f72013-03-01 16:36:00 -05005119
5120 bpl = (struct ulp_bde64 *)bmp->virt;
5121 request_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64);
5122 cmp = lpfc_alloc_bsg_buffers(phba, job->request_payload.payload_len,
5123 1, bpl, &request_nseg);
5124 if (!cmp) {
5125 rc = -ENOMEM;
5126 goto free_bmp;
5127 }
5128 lpfc_bsg_copy_data(cmp, &job->request_payload,
5129 job->request_payload.payload_len, 1);
5130
5131 bpl += request_nseg;
5132 reply_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64) - request_nseg;
5133 rmp = lpfc_alloc_bsg_buffers(phba, job->reply_payload.payload_len, 0,
5134 bpl, &reply_nseg);
5135 if (!rmp) {
5136 rc = -ENOMEM;
5137 goto free_cmp;
James Smarte2aed292010-02-26 14:15:00 -05005138 }
5139
James Smarta33c4f72013-03-01 16:36:00 -05005140 cmdiocbq = lpfc_sli_get_iocbq(phba);
5141 if (!cmdiocbq) {
5142 rc = -ENOMEM;
5143 goto free_rmp;
James Smarte2aed292010-02-26 14:15:00 -05005144 }
5145
5146 cmd = &cmdiocbq->iocb;
5147 cmd->un.genreq64.bdl.ulpIoTag32 = 0;
5148 cmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
5149 cmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
5150 cmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
5151 cmd->un.genreq64.bdl.bdeSize =
5152 (request_nseg + reply_nseg) * sizeof(struct ulp_bde64);
5153 cmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
5154 cmd->un.genreq64.w5.hcsw.Dfctl = 0;
5155 cmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CMD;
5156 cmd->un.genreq64.w5.hcsw.Type = MENLO_TRANSPORT_TYPE; /* 0xfe */
5157 cmd->ulpBdeCount = 1;
5158 cmd->ulpClass = CLASS3;
5159 cmd->ulpOwner = OWN_CHIP;
5160 cmd->ulpLe = 1; /* Limited Edition */
5161 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
5162 cmdiocbq->vport = phba->pport;
5163 /* We want the firmware to timeout before we do */
5164 cmd->ulpTimeout = MENLO_TIMEOUT - 5;
James Smarte2aed292010-02-26 14:15:00 -05005165 cmdiocbq->iocb_cmpl = lpfc_bsg_menlo_cmd_cmp;
5166 cmdiocbq->context1 = dd_data;
James Smarta33c4f72013-03-01 16:36:00 -05005167 cmdiocbq->context2 = cmp;
5168 cmdiocbq->context3 = bmp;
James Smarte2aed292010-02-26 14:15:00 -05005169 if (menlo_cmd->cmd == LPFC_BSG_VENDOR_MENLO_CMD) {
5170 cmd->ulpCommand = CMD_GEN_REQUEST64_CR;
5171 cmd->ulpPU = MENLO_PU; /* 3 */
5172 cmd->un.ulpWord[4] = MENLO_DID; /* 0x0000FC0E */
5173 cmd->ulpContext = MENLO_CONTEXT; /* 0 */
5174 } else {
5175 cmd->ulpCommand = CMD_GEN_REQUEST64_CX;
5176 cmd->ulpPU = 1;
5177 cmd->un.ulpWord[4] = 0;
5178 cmd->ulpContext = menlo_cmd->xri;
5179 }
5180
5181 dd_data->type = TYPE_MENLO;
James Smarta33c4f72013-03-01 16:36:00 -05005182 dd_data->set_job = job;
James Smarte2aed292010-02-26 14:15:00 -05005183 dd_data->context_un.menlo.cmdiocbq = cmdiocbq;
James Smarta33c4f72013-03-01 16:36:00 -05005184 dd_data->context_un.menlo.rmp = rmp;
5185 job->dd_data = dd_data;
James Smarte2aed292010-02-26 14:15:00 -05005186
5187 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq,
5188 MENLO_TIMEOUT - 5);
5189 if (rc == IOCB_SUCCESS)
5190 return 0; /* done for now */
5191
James Smarte2aed292010-02-26 14:15:00 -05005192 lpfc_sli_release_iocbq(phba, cmdiocbq);
James Smarta33c4f72013-03-01 16:36:00 -05005193
5194free_rmp:
5195 lpfc_free_bsg_buffers(phba, rmp);
5196free_cmp:
5197 lpfc_free_bsg_buffers(phba, cmp);
James Smarte2aed292010-02-26 14:15:00 -05005198free_bmp:
James Smarta33c4f72013-03-01 16:36:00 -05005199 if (bmp->virt)
5200 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
James Smarte2aed292010-02-26 14:15:00 -05005201 kfree(bmp);
5202free_dd:
5203 kfree(dd_data);
5204no_dd_data:
5205 /* make error code available to userspace */
5206 job->reply->result = rc;
5207 job->dd_data = NULL;
5208 return rc;
5209}
James Smartb6e3b9c2011-04-16 11:03:43 -04005210
James Smarte2aed292010-02-26 14:15:00 -05005211/**
James Smartf1c3b0f2009-07-19 10:01:32 -04005212 * lpfc_bsg_hst_vendor - process a vendor-specific fc_bsg_job
5213 * @job: fc_bsg_job to handle
James Smart3b5dd522010-01-26 23:10:15 -05005214 **/
James Smartf1c3b0f2009-07-19 10:01:32 -04005215static int
5216lpfc_bsg_hst_vendor(struct fc_bsg_job *job)
5217{
5218 int command = job->request->rqst_data.h_vendor.vendor_cmd[0];
James Smart4cc0e562010-01-26 23:09:48 -05005219 int rc;
James Smartf1c3b0f2009-07-19 10:01:32 -04005220
5221 switch (command) {
5222 case LPFC_BSG_VENDOR_SET_CT_EVENT:
James Smart4cc0e562010-01-26 23:09:48 -05005223 rc = lpfc_bsg_hba_set_event(job);
James Smartf1c3b0f2009-07-19 10:01:32 -04005224 break;
James Smartf1c3b0f2009-07-19 10:01:32 -04005225 case LPFC_BSG_VENDOR_GET_CT_EVENT:
James Smart4cc0e562010-01-26 23:09:48 -05005226 rc = lpfc_bsg_hba_get_event(job);
James Smartf1c3b0f2009-07-19 10:01:32 -04005227 break;
James Smart3b5dd522010-01-26 23:10:15 -05005228 case LPFC_BSG_VENDOR_SEND_MGMT_RESP:
5229 rc = lpfc_bsg_send_mgmt_rsp(job);
5230 break;
5231 case LPFC_BSG_VENDOR_DIAG_MODE:
James Smart7ad20aa2011-05-24 11:44:28 -04005232 rc = lpfc_bsg_diag_loopback_mode(job);
James Smart3b5dd522010-01-26 23:10:15 -05005233 break;
James Smart7ad20aa2011-05-24 11:44:28 -04005234 case LPFC_BSG_VENDOR_DIAG_MODE_END:
5235 rc = lpfc_sli4_bsg_diag_mode_end(job);
5236 break;
5237 case LPFC_BSG_VENDOR_DIAG_RUN_LOOPBACK:
5238 rc = lpfc_bsg_diag_loopback_run(job);
5239 break;
5240 case LPFC_BSG_VENDOR_LINK_DIAG_TEST:
5241 rc = lpfc_sli4_bsg_link_diag_test(job);
James Smart3b5dd522010-01-26 23:10:15 -05005242 break;
5243 case LPFC_BSG_VENDOR_GET_MGMT_REV:
5244 rc = lpfc_bsg_get_dfc_rev(job);
5245 break;
5246 case LPFC_BSG_VENDOR_MBOX:
5247 rc = lpfc_bsg_mbox_cmd(job);
5248 break;
James Smarte2aed292010-02-26 14:15:00 -05005249 case LPFC_BSG_VENDOR_MENLO_CMD:
5250 case LPFC_BSG_VENDOR_MENLO_DATA:
5251 rc = lpfc_menlo_cmd(job);
5252 break;
James Smartf1c3b0f2009-07-19 10:01:32 -04005253 default:
James Smart4cc0e562010-01-26 23:09:48 -05005254 rc = -EINVAL;
5255 job->reply->reply_payload_rcv_len = 0;
5256 /* make error code available to userspace */
5257 job->reply->result = rc;
5258 break;
James Smartf1c3b0f2009-07-19 10:01:32 -04005259 }
James Smart4cc0e562010-01-26 23:09:48 -05005260
5261 return rc;
James Smartf1c3b0f2009-07-19 10:01:32 -04005262}
5263
5264/**
5265 * lpfc_bsg_request - handle a bsg request from the FC transport
5266 * @job: fc_bsg_job to handle
James Smart3b5dd522010-01-26 23:10:15 -05005267 **/
James Smartf1c3b0f2009-07-19 10:01:32 -04005268int
5269lpfc_bsg_request(struct fc_bsg_job *job)
5270{
5271 uint32_t msgcode;
James Smart4cc0e562010-01-26 23:09:48 -05005272 int rc;
James Smartf1c3b0f2009-07-19 10:01:32 -04005273
5274 msgcode = job->request->msgcode;
James Smartf1c3b0f2009-07-19 10:01:32 -04005275 switch (msgcode) {
5276 case FC_BSG_HST_VENDOR:
5277 rc = lpfc_bsg_hst_vendor(job);
5278 break;
5279 case FC_BSG_RPT_ELS:
5280 rc = lpfc_bsg_rport_els(job);
5281 break;
5282 case FC_BSG_RPT_CT:
James Smart4cc0e562010-01-26 23:09:48 -05005283 rc = lpfc_bsg_send_mgmt_cmd(job);
James Smartf1c3b0f2009-07-19 10:01:32 -04005284 break;
5285 default:
James Smart4cc0e562010-01-26 23:09:48 -05005286 rc = -EINVAL;
5287 job->reply->reply_payload_rcv_len = 0;
5288 /* make error code available to userspace */
5289 job->reply->result = rc;
James Smartf1c3b0f2009-07-19 10:01:32 -04005290 break;
5291 }
5292
5293 return rc;
5294}
5295
5296/**
5297 * lpfc_bsg_timeout - handle timeout of a bsg request from the FC transport
5298 * @job: fc_bsg_job that has timed out
5299 *
5300 * This function just aborts the job's IOCB. The aborted IOCB will return to
5301 * the waiting function which will handle passing the error back to userspace
James Smart3b5dd522010-01-26 23:10:15 -05005302 **/
James Smartf1c3b0f2009-07-19 10:01:32 -04005303int
5304lpfc_bsg_timeout(struct fc_bsg_job *job)
5305{
5306 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
5307 struct lpfc_hba *phba = vport->phba;
James Smart4cc0e562010-01-26 23:09:48 -05005308 struct lpfc_iocbq *cmdiocb;
James Smartf1c3b0f2009-07-19 10:01:32 -04005309 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart4cc0e562010-01-26 23:09:48 -05005310 struct bsg_job_data *dd_data;
5311 unsigned long flags;
James Smarta33c4f72013-03-01 16:36:00 -05005312 int rc = 0;
5313 LIST_HEAD(completions);
5314 struct lpfc_iocbq *check_iocb, *next_iocb;
5315
5316 /* if job's driver data is NULL, the command completed or is in the
5317 * the process of completing. In this case, return status to request
5318 * so the timeout is retried. This avoids double completion issues
5319 * and the request will be pulled off the timer queue when the
5320 * command's completion handler executes. Otherwise, prevent the
5321 * command's completion handler from executing the job done callback
5322 * and continue processing to abort the outstanding the command.
5323 */
James Smartf1c3b0f2009-07-19 10:01:32 -04005324
James Smart4cc0e562010-01-26 23:09:48 -05005325 spin_lock_irqsave(&phba->ct_ev_lock, flags);
5326 dd_data = (struct bsg_job_data *)job->dd_data;
James Smarta33c4f72013-03-01 16:36:00 -05005327 if (dd_data) {
5328 dd_data->set_job = NULL;
5329 job->dd_data = NULL;
5330 } else {
James Smart4cc0e562010-01-26 23:09:48 -05005331 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smarta33c4f72013-03-01 16:36:00 -05005332 return -EAGAIN;
James Smart4cc0e562010-01-26 23:09:48 -05005333 }
5334
5335 switch (dd_data->type) {
5336 case TYPE_IOCB:
James Smarta33c4f72013-03-01 16:36:00 -05005337 /* Check to see if IOCB was issued to the port or not. If not,
5338 * remove it from the txq queue and call cancel iocbs.
5339 * Otherwise, call abort iotag
James Smart4cc0e562010-01-26 23:09:48 -05005340 */
James Smarta33c4f72013-03-01 16:36:00 -05005341 cmdiocb = dd_data->context_un.iocb.cmdiocbq;
James Smartb5a9b2d2013-09-06 12:19:45 -04005342 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5343
5344 spin_lock_irqsave(&phba->hbalock, flags);
5345 /* make sure the I/O abort window is still open */
James Smart1b8d11a2013-09-06 12:20:51 -04005346 if (!(cmdiocb->iocb_flag & LPFC_IO_CMD_OUTSTANDING)) {
James Smartb5a9b2d2013-09-06 12:19:45 -04005347 spin_unlock_irqrestore(&phba->hbalock, flags);
5348 return -EAGAIN;
5349 }
James Smarta33c4f72013-03-01 16:36:00 -05005350 list_for_each_entry_safe(check_iocb, next_iocb, &pring->txq,
5351 list) {
5352 if (check_iocb == cmdiocb) {
5353 list_move_tail(&check_iocb->list, &completions);
5354 break;
5355 }
5356 }
5357 if (list_empty(&completions))
5358 lpfc_sli_issue_abort_iotag(phba, pring, cmdiocb);
James Smartb5a9b2d2013-09-06 12:19:45 -04005359 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarta33c4f72013-03-01 16:36:00 -05005360 if (!list_empty(&completions)) {
5361 lpfc_sli_cancel_iocbs(phba, &completions,
5362 IOSTAT_LOCAL_REJECT,
5363 IOERR_SLI_ABORTED);
5364 }
James Smart4cc0e562010-01-26 23:09:48 -05005365 break;
James Smarta33c4f72013-03-01 16:36:00 -05005366
5367 case TYPE_EVT:
James Smart3b5dd522010-01-26 23:10:15 -05005368 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smarta33c4f72013-03-01 16:36:00 -05005369 break;
5370
5371 case TYPE_MBOX:
5372 /* Update the ext buf ctx state if needed */
5373
James Smart7ad20aa2011-05-24 11:44:28 -04005374 if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_PORT)
5375 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_ABTS;
James Smarta33c4f72013-03-01 16:36:00 -05005376 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smart3b5dd522010-01-26 23:10:15 -05005377 break;
James Smarte2aed292010-02-26 14:15:00 -05005378 case TYPE_MENLO:
James Smarta33c4f72013-03-01 16:36:00 -05005379 /* Check to see if IOCB was issued to the port or not. If not,
5380 * remove it from the txq queue and call cancel iocbs.
5381 * Otherwise, call abort iotag.
5382 */
James Smarta33c4f72013-03-01 16:36:00 -05005383 cmdiocb = dd_data->context_un.menlo.cmdiocbq;
James Smartb5a9b2d2013-09-06 12:19:45 -04005384 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5385
5386 spin_lock_irqsave(&phba->hbalock, flags);
James Smarta33c4f72013-03-01 16:36:00 -05005387 list_for_each_entry_safe(check_iocb, next_iocb, &pring->txq,
5388 list) {
5389 if (check_iocb == cmdiocb) {
5390 list_move_tail(&check_iocb->list, &completions);
5391 break;
5392 }
5393 }
5394 if (list_empty(&completions))
5395 lpfc_sli_issue_abort_iotag(phba, pring, cmdiocb);
James Smartb5a9b2d2013-09-06 12:19:45 -04005396 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarta33c4f72013-03-01 16:36:00 -05005397 if (!list_empty(&completions)) {
5398 lpfc_sli_cancel_iocbs(phba, &completions,
5399 IOSTAT_LOCAL_REJECT,
5400 IOERR_SLI_ABORTED);
5401 }
James Smarte2aed292010-02-26 14:15:00 -05005402 break;
James Smart4cc0e562010-01-26 23:09:48 -05005403 default:
5404 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5405 break;
5406 }
James Smartf1c3b0f2009-07-19 10:01:32 -04005407
James Smart4cc0e562010-01-26 23:09:48 -05005408 /* scsi transport fc fc_bsg_job_timeout expects a zero return code,
5409 * otherwise an error message will be displayed on the console
5410 * so always return success (zero)
5411 */
James Smarta33c4f72013-03-01 16:36:00 -05005412 return rc;
James Smartf1c3b0f2009-07-19 10:01:32 -04005413}