blob: 6b4cf315619e1cf7f141c280076de0012dae13a9 [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;
James Smartf1c3b0f2009-07-19 10:01:32 -0400659 struct lpfc_iocbq *cmdiocbq;
James Smartf1c3b0f2009-07-19 10:01:32 -0400660 uint16_t rpi = 0;
James Smart4cc0e562010-01-26 23:09:48 -0500661 struct bsg_job_data *dd_data;
James Smartb5a9b2d2013-09-06 12:19:45 -0400662 unsigned long flags;
James Smart4cc0e562010-01-26 23:09:48 -0500663 uint32_t creg_val;
James Smartf1c3b0f2009-07-19 10:01:32 -0400664 int rc = 0;
665
666 /* in case no data is transferred */
667 job->reply->reply_payload_rcv_len = 0;
668
James Smarta33c4f72013-03-01 16:36:00 -0500669 /* verify the els command is not greater than the
670 * maximum ELS transfer size.
671 */
672
673 if (job->request_payload.payload_len > FCELSSIZE) {
674 rc = -EINVAL;
675 goto no_dd_data;
676 }
677
James Smart4cc0e562010-01-26 23:09:48 -0500678 /* allocate our bsg tracking structure */
679 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
680 if (!dd_data) {
681 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
682 "2735 Failed allocation of dd_data\n");
683 rc = -ENOMEM;
684 goto no_dd_data;
685 }
686
James Smarta33c4f72013-03-01 16:36:00 -0500687 elscmd = job->request->rqst_data.r_els.els_code;
688 cmdsize = job->request_payload.payload_len;
James Smarta33c4f72013-03-01 16:36:00 -0500689
James Smartf1c3b0f2009-07-19 10:01:32 -0400690 if (!lpfc_nlp_get(ndlp)) {
691 rc = -ENODEV;
James Smart4cc0e562010-01-26 23:09:48 -0500692 goto free_dd_data;
James Smartf1c3b0f2009-07-19 10:01:32 -0400693 }
694
James Smarta33c4f72013-03-01 16:36:00 -0500695 /* We will use the allocated dma buffers by prep els iocb for command
696 * and response to ensure if the job times out and the request is freed,
697 * we won't be dma into memory that is no longer allocated to for the
698 * request.
699 */
James Smartf1c3b0f2009-07-19 10:01:32 -0400700
James Smart4cc0e562010-01-26 23:09:48 -0500701 cmdiocbq = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp,
James Smartf1c3b0f2009-07-19 10:01:32 -0400702 ndlp->nlp_DID, elscmd);
James Smartf1c3b0f2009-07-19 10:01:32 -0400703 if (!cmdiocbq) {
James Smart4cc0e562010-01-26 23:09:48 -0500704 rc = -EIO;
James Smarta33c4f72013-03-01 16:36:00 -0500705 goto release_ndlp;
James Smartf1c3b0f2009-07-19 10:01:32 -0400706 }
707
James Smarta33c4f72013-03-01 16:36:00 -0500708 rpi = ndlp->nlp_rpi;
James Smartf1c3b0f2009-07-19 10:01:32 -0400709
James Smarta33c4f72013-03-01 16:36:00 -0500710 /* Transfer the request payload to allocated command dma buffer */
James Smartf1c3b0f2009-07-19 10:01:32 -0400711
James Smarta33c4f72013-03-01 16:36:00 -0500712 sg_copy_to_buffer(job->request_payload.sg_list,
713 job->request_payload.sg_cnt,
714 ((struct lpfc_dmabuf *)cmdiocbq->context2)->virt,
715 cmdsize);
James Smartf1c3b0f2009-07-19 10:01:32 -0400716
James Smart3ef6d242012-01-18 16:23:48 -0500717 if (phba->sli_rev == LPFC_SLI_REV4)
718 cmdiocbq->iocb.ulpContext = phba->sli4_hba.rpi_ids[rpi];
719 else
720 cmdiocbq->iocb.ulpContext = rpi;
James Smartf1c3b0f2009-07-19 10:01:32 -0400721 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
James Smart4cc0e562010-01-26 23:09:48 -0500722 cmdiocbq->context1 = dd_data;
James Smart93d1379e2012-05-09 21:19:34 -0400723 cmdiocbq->context_un.ndlp = ndlp;
James Smarta33c4f72013-03-01 16:36:00 -0500724 cmdiocbq->iocb_cmpl = lpfc_bsg_rport_els_cmp;
James Smart4cc0e562010-01-26 23:09:48 -0500725 dd_data->type = TYPE_IOCB;
James Smarta33c4f72013-03-01 16:36:00 -0500726 dd_data->set_job = job;
James Smart4cc0e562010-01-26 23:09:48 -0500727 dd_data->context_un.iocb.cmdiocbq = cmdiocbq;
James Smart4cc0e562010-01-26 23:09:48 -0500728 dd_data->context_un.iocb.ndlp = ndlp;
James Smarta33c4f72013-03-01 16:36:00 -0500729 dd_data->context_un.iocb.rmp = NULL;
730 job->dd_data = dd_data;
James Smartf1c3b0f2009-07-19 10:01:32 -0400731
James Smart4cc0e562010-01-26 23:09:48 -0500732 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -0500733 if (lpfc_readl(phba->HCregaddr, &creg_val)) {
734 rc = -EIO;
735 goto linkdown_err;
736 }
James Smart4cc0e562010-01-26 23:09:48 -0500737 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
738 writel(creg_val, phba->HCregaddr);
739 readl(phba->HCregaddr); /* flush */
James Smartf1c3b0f2009-07-19 10:01:32 -0400740 }
James Smarta33c4f72013-03-01 16:36:00 -0500741
James Smart4cc0e562010-01-26 23:09:48 -0500742 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq, 0);
James Smarta33c4f72013-03-01 16:36:00 -0500743
James Smartb5a9b2d2013-09-06 12:19:45 -0400744 if (rc == IOCB_SUCCESS) {
745 spin_lock_irqsave(&phba->hbalock, flags);
746 /* make sure the I/O had not been completed/released */
747 if (cmdiocbq->iocb_flag & LPFC_IO_LIBDFC) {
748 /* open up abort window to timeout handler */
James Smart1b8d11a2013-09-06 12:20:51 -0400749 cmdiocbq->iocb_flag |= LPFC_IO_CMD_OUTSTANDING;
James Smartb5a9b2d2013-09-06 12:19:45 -0400750 }
751 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4cc0e562010-01-26 23:09:48 -0500752 return 0; /* done for now */
James Smartb5a9b2d2013-09-06 12:19:45 -0400753 } else if (rc == IOCB_BUSY) {
James Smartd439d282010-09-29 11:18:45 -0400754 rc = -EAGAIN;
James Smartb5a9b2d2013-09-06 12:19:45 -0400755 } else {
James Smartd439d282010-09-29 11:18:45 -0400756 rc = -EIO;
James Smartb5a9b2d2013-09-06 12:19:45 -0400757 }
758
759 /* iocb failed so cleanup */
760 job->dd_data = NULL;
James Smartf1c3b0f2009-07-19 10:01:32 -0400761
James Smart9940b972011-03-11 16:06:12 -0500762linkdown_err:
James Smarta33c4f72013-03-01 16:36:00 -0500763 cmdiocbq->context1 = ndlp;
764 lpfc_els_free_iocb(phba, cmdiocbq);
James Smartf1c3b0f2009-07-19 10:01:32 -0400765
James Smarta33c4f72013-03-01 16:36:00 -0500766release_ndlp:
767 lpfc_nlp_put(ndlp);
James Smartf1c3b0f2009-07-19 10:01:32 -0400768
James Smart4cc0e562010-01-26 23:09:48 -0500769free_dd_data:
770 kfree(dd_data);
771
772no_dd_data:
James Smartf1c3b0f2009-07-19 10:01:32 -0400773 /* make error code available to userspace */
774 job->reply->result = rc;
James Smart4cc0e562010-01-26 23:09:48 -0500775 job->dd_data = NULL;
776 return rc;
James Smartf1c3b0f2009-07-19 10:01:32 -0400777}
778
James Smart3b5dd522010-01-26 23:10:15 -0500779/**
780 * lpfc_bsg_event_free - frees an allocated event structure
781 * @kref: Pointer to a kref.
782 *
783 * Called from kref_put. Back cast the kref into an event structure address.
784 * Free any events to get, delete associated nodes, free any events to see,
785 * free any data then free the event itself.
786 **/
James Smartf1c3b0f2009-07-19 10:01:32 -0400787static void
James Smart4cc0e562010-01-26 23:09:48 -0500788lpfc_bsg_event_free(struct kref *kref)
James Smartf1c3b0f2009-07-19 10:01:32 -0400789{
James Smart4cc0e562010-01-26 23:09:48 -0500790 struct lpfc_bsg_event *evt = container_of(kref, struct lpfc_bsg_event,
791 kref);
James Smartf1c3b0f2009-07-19 10:01:32 -0400792 struct event_data *ed;
793
794 list_del(&evt->node);
795
796 while (!list_empty(&evt->events_to_get)) {
797 ed = list_entry(evt->events_to_get.next, typeof(*ed), node);
798 list_del(&ed->node);
799 kfree(ed->data);
800 kfree(ed);
801 }
802
803 while (!list_empty(&evt->events_to_see)) {
804 ed = list_entry(evt->events_to_see.next, typeof(*ed), node);
805 list_del(&ed->node);
806 kfree(ed->data);
807 kfree(ed);
808 }
809
James Smarta33c4f72013-03-01 16:36:00 -0500810 kfree(evt->dd_data);
James Smartf1c3b0f2009-07-19 10:01:32 -0400811 kfree(evt);
812}
813
James Smart3b5dd522010-01-26 23:10:15 -0500814/**
815 * lpfc_bsg_event_ref - increments the kref for an event
816 * @evt: Pointer to an event structure.
817 **/
James Smartf1c3b0f2009-07-19 10:01:32 -0400818static inline void
James Smart4cc0e562010-01-26 23:09:48 -0500819lpfc_bsg_event_ref(struct lpfc_bsg_event *evt)
James Smartf1c3b0f2009-07-19 10:01:32 -0400820{
James Smart4cc0e562010-01-26 23:09:48 -0500821 kref_get(&evt->kref);
James Smartf1c3b0f2009-07-19 10:01:32 -0400822}
823
James Smart3b5dd522010-01-26 23:10:15 -0500824/**
825 * lpfc_bsg_event_unref - Uses kref_put to free an event structure
826 * @evt: Pointer to an event structure.
827 **/
James Smartf1c3b0f2009-07-19 10:01:32 -0400828static inline void
James Smart4cc0e562010-01-26 23:09:48 -0500829lpfc_bsg_event_unref(struct lpfc_bsg_event *evt)
James Smartf1c3b0f2009-07-19 10:01:32 -0400830{
James Smart4cc0e562010-01-26 23:09:48 -0500831 kref_put(&evt->kref, lpfc_bsg_event_free);
James Smartf1c3b0f2009-07-19 10:01:32 -0400832}
833
James Smart3b5dd522010-01-26 23:10:15 -0500834/**
835 * lpfc_bsg_event_new - allocate and initialize a event structure
836 * @ev_mask: Mask of events.
837 * @ev_reg_id: Event reg id.
838 * @ev_req_id: Event request id.
839 **/
James Smart4cc0e562010-01-26 23:09:48 -0500840static struct lpfc_bsg_event *
841lpfc_bsg_event_new(uint32_t ev_mask, int ev_reg_id, uint32_t ev_req_id)
842{
843 struct lpfc_bsg_event *evt = kzalloc(sizeof(*evt), GFP_KERNEL);
James Smartf1c3b0f2009-07-19 10:01:32 -0400844
James Smart4cc0e562010-01-26 23:09:48 -0500845 if (!evt)
846 return NULL;
847
848 INIT_LIST_HEAD(&evt->events_to_get);
849 INIT_LIST_HEAD(&evt->events_to_see);
850 evt->type_mask = ev_mask;
851 evt->req_id = ev_req_id;
852 evt->reg_id = ev_reg_id;
853 evt->wait_time_stamp = jiffies;
James Smarta33c4f72013-03-01 16:36:00 -0500854 evt->dd_data = NULL;
James Smart4cc0e562010-01-26 23:09:48 -0500855 init_waitqueue_head(&evt->wq);
856 kref_init(&evt->kref);
857 return evt;
858}
859
James Smart3b5dd522010-01-26 23:10:15 -0500860/**
861 * diag_cmd_data_free - Frees an lpfc dma buffer extension
862 * @phba: Pointer to HBA context object.
863 * @mlist: Pointer to an lpfc dma buffer extension.
864 **/
James Smart4cc0e562010-01-26 23:09:48 -0500865static int
James Smart3b5dd522010-01-26 23:10:15 -0500866diag_cmd_data_free(struct lpfc_hba *phba, struct lpfc_dmabufext *mlist)
James Smart4cc0e562010-01-26 23:09:48 -0500867{
868 struct lpfc_dmabufext *mlast;
869 struct pci_dev *pcidev;
870 struct list_head head, *curr, *next;
871
872 if ((!mlist) || (!lpfc_is_link_up(phba) &&
873 (phba->link_flag & LS_LOOPBACK_MODE))) {
874 return 0;
875 }
876
877 pcidev = phba->pcidev;
878 list_add_tail(&head, &mlist->dma.list);
879
880 list_for_each_safe(curr, next, &head) {
881 mlast = list_entry(curr, struct lpfc_dmabufext , dma.list);
882 if (mlast->dma.virt)
883 dma_free_coherent(&pcidev->dev,
884 mlast->size,
885 mlast->dma.virt,
886 mlast->dma.phys);
887 kfree(mlast);
888 }
889 return 0;
890}
James Smartf1c3b0f2009-07-19 10:01:32 -0400891
892/**
893 * lpfc_bsg_ct_unsol_event - process an unsolicited CT command
894 * @phba:
895 * @pring:
896 * @piocbq:
897 *
898 * This function is called when an unsolicited CT command is received. It
James Smart4cc0e562010-01-26 23:09:48 -0500899 * forwards the event to any processes registered to receive CT events.
James Smart3b5dd522010-01-26 23:10:15 -0500900 **/
James Smart4fede782010-01-26 23:08:55 -0500901int
James Smartf1c3b0f2009-07-19 10:01:32 -0400902lpfc_bsg_ct_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
903 struct lpfc_iocbq *piocbq)
904{
905 uint32_t evt_req_id = 0;
906 uint32_t cmd;
907 uint32_t len;
908 struct lpfc_dmabuf *dmabuf = NULL;
James Smart4cc0e562010-01-26 23:09:48 -0500909 struct lpfc_bsg_event *evt;
James Smartf1c3b0f2009-07-19 10:01:32 -0400910 struct event_data *evt_dat = NULL;
911 struct lpfc_iocbq *iocbq;
912 size_t offset = 0;
913 struct list_head head;
914 struct ulp_bde64 *bde;
915 dma_addr_t dma_addr;
916 int i;
917 struct lpfc_dmabuf *bdeBuf1 = piocbq->context2;
918 struct lpfc_dmabuf *bdeBuf2 = piocbq->context3;
919 struct lpfc_hbq_entry *hbqe;
920 struct lpfc_sli_ct_request *ct_req;
James Smart4cc0e562010-01-26 23:09:48 -0500921 struct fc_bsg_job *job = NULL;
James Smarta33c4f72013-03-01 16:36:00 -0500922 struct bsg_job_data *dd_data = NULL;
James Smart4fede782010-01-26 23:08:55 -0500923 unsigned long flags;
James Smart4cc0e562010-01-26 23:09:48 -0500924 int size = 0;
James Smartf1c3b0f2009-07-19 10:01:32 -0400925
926 INIT_LIST_HEAD(&head);
927 list_add_tail(&head, &piocbq->list);
928
929 if (piocbq->iocb.ulpBdeCount == 0 ||
930 piocbq->iocb.un.cont64[0].tus.f.bdeSize == 0)
931 goto error_ct_unsol_exit;
932
James Smart4cc0e562010-01-26 23:09:48 -0500933 if (phba->link_state == LPFC_HBA_ERROR ||
934 (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)))
935 goto error_ct_unsol_exit;
936
James Smartf1c3b0f2009-07-19 10:01:32 -0400937 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
938 dmabuf = bdeBuf1;
939 else {
940 dma_addr = getPaddr(piocbq->iocb.un.cont64[0].addrHigh,
941 piocbq->iocb.un.cont64[0].addrLow);
942 dmabuf = lpfc_sli_ringpostbuf_get(phba, pring, dma_addr);
943 }
James Smart4cc0e562010-01-26 23:09:48 -0500944 if (dmabuf == NULL)
945 goto error_ct_unsol_exit;
James Smartf1c3b0f2009-07-19 10:01:32 -0400946 ct_req = (struct lpfc_sli_ct_request *)dmabuf->virt;
947 evt_req_id = ct_req->FsType;
948 cmd = ct_req->CommandResponse.bits.CmdRsp;
949 len = ct_req->CommandResponse.bits.Size;
950 if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
951 lpfc_sli_ringpostbuf_put(phba, pring, dmabuf);
952
James Smart4fede782010-01-26 23:08:55 -0500953 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -0400954 list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
James Smart4cc0e562010-01-26 23:09:48 -0500955 if (!(evt->type_mask & FC_REG_CT_EVENT) ||
956 evt->req_id != evt_req_id)
James Smartf1c3b0f2009-07-19 10:01:32 -0400957 continue;
958
James Smart4cc0e562010-01-26 23:09:48 -0500959 lpfc_bsg_event_ref(evt);
960 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -0400961 evt_dat = kzalloc(sizeof(*evt_dat), GFP_KERNEL);
James Smart4cc0e562010-01-26 23:09:48 -0500962 if (evt_dat == NULL) {
963 spin_lock_irqsave(&phba->ct_ev_lock, flags);
964 lpfc_bsg_event_unref(evt);
James Smartf1c3b0f2009-07-19 10:01:32 -0400965 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
966 "2614 Memory allocation failed for "
967 "CT event\n");
968 break;
969 }
970
James Smartf1c3b0f2009-07-19 10:01:32 -0400971 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
972 /* take accumulated byte count from the last iocbq */
973 iocbq = list_entry(head.prev, typeof(*iocbq), list);
974 evt_dat->len = iocbq->iocb.unsli3.rcvsli3.acc_len;
975 } else {
976 list_for_each_entry(iocbq, &head, list) {
977 for (i = 0; i < iocbq->iocb.ulpBdeCount; i++)
978 evt_dat->len +=
979 iocbq->iocb.un.cont64[i].tus.f.bdeSize;
980 }
981 }
982
983 evt_dat->data = kzalloc(evt_dat->len, GFP_KERNEL);
James Smart4cc0e562010-01-26 23:09:48 -0500984 if (evt_dat->data == NULL) {
James Smartf1c3b0f2009-07-19 10:01:32 -0400985 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
986 "2615 Memory allocation failed for "
987 "CT event data, size %d\n",
988 evt_dat->len);
989 kfree(evt_dat);
James Smart4fede782010-01-26 23:08:55 -0500990 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smart4cc0e562010-01-26 23:09:48 -0500991 lpfc_bsg_event_unref(evt);
James Smart4fede782010-01-26 23:08:55 -0500992 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -0400993 goto error_ct_unsol_exit;
994 }
995
996 list_for_each_entry(iocbq, &head, list) {
James Smart4cc0e562010-01-26 23:09:48 -0500997 size = 0;
James Smartf1c3b0f2009-07-19 10:01:32 -0400998 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
999 bdeBuf1 = iocbq->context2;
1000 bdeBuf2 = iocbq->context3;
1001 }
1002 for (i = 0; i < iocbq->iocb.ulpBdeCount; i++) {
James Smartf1c3b0f2009-07-19 10:01:32 -04001003 if (phba->sli3_options &
1004 LPFC_SLI3_HBQ_ENABLED) {
1005 if (i == 0) {
1006 hbqe = (struct lpfc_hbq_entry *)
1007 &iocbq->iocb.un.ulpWord[0];
1008 size = hbqe->bde.tus.f.bdeSize;
1009 dmabuf = bdeBuf1;
1010 } else if (i == 1) {
1011 hbqe = (struct lpfc_hbq_entry *)
1012 &iocbq->iocb.unsli3.
1013 sli3Words[4];
1014 size = hbqe->bde.tus.f.bdeSize;
1015 dmabuf = bdeBuf2;
1016 }
1017 if ((offset + size) > evt_dat->len)
1018 size = evt_dat->len - offset;
1019 } else {
1020 size = iocbq->iocb.un.cont64[i].
1021 tus.f.bdeSize;
1022 bde = &iocbq->iocb.un.cont64[i];
1023 dma_addr = getPaddr(bde->addrHigh,
1024 bde->addrLow);
1025 dmabuf = lpfc_sli_ringpostbuf_get(phba,
1026 pring, dma_addr);
1027 }
1028 if (!dmabuf) {
1029 lpfc_printf_log(phba, KERN_ERR,
1030 LOG_LIBDFC, "2616 No dmabuf "
1031 "found for iocbq 0x%p\n",
1032 iocbq);
1033 kfree(evt_dat->data);
1034 kfree(evt_dat);
James Smart4fede782010-01-26 23:08:55 -05001035 spin_lock_irqsave(&phba->ct_ev_lock,
1036 flags);
James Smart4cc0e562010-01-26 23:09:48 -05001037 lpfc_bsg_event_unref(evt);
James Smart4fede782010-01-26 23:08:55 -05001038 spin_unlock_irqrestore(
1039 &phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -04001040 goto error_ct_unsol_exit;
1041 }
1042 memcpy((char *)(evt_dat->data) + offset,
1043 dmabuf->virt, size);
1044 offset += size;
1045 if (evt_req_id != SLI_CT_ELX_LOOPBACK &&
1046 !(phba->sli3_options &
1047 LPFC_SLI3_HBQ_ENABLED)) {
1048 lpfc_sli_ringpostbuf_put(phba, pring,
1049 dmabuf);
1050 } else {
1051 switch (cmd) {
James Smart4cc0e562010-01-26 23:09:48 -05001052 case ELX_LOOPBACK_DATA:
James Smart1b511972011-12-13 13:23:09 -05001053 if (phba->sli_rev <
1054 LPFC_SLI_REV4)
1055 diag_cmd_data_free(phba,
1056 (struct lpfc_dmabufext
1057 *)dmabuf);
James Smart4cc0e562010-01-26 23:09:48 -05001058 break;
James Smartf1c3b0f2009-07-19 10:01:32 -04001059 case ELX_LOOPBACK_XRI_SETUP:
James Smart4cc0e562010-01-26 23:09:48 -05001060 if ((phba->sli_rev ==
1061 LPFC_SLI_REV2) ||
1062 (phba->sli3_options &
1063 LPFC_SLI3_HBQ_ENABLED
1064 )) {
1065 lpfc_in_buf_free(phba,
1066 dmabuf);
1067 } else {
James Smartf1c3b0f2009-07-19 10:01:32 -04001068 lpfc_post_buffer(phba,
1069 pring,
1070 1);
James Smart4cc0e562010-01-26 23:09:48 -05001071 }
James Smartf1c3b0f2009-07-19 10:01:32 -04001072 break;
1073 default:
1074 if (!(phba->sli3_options &
1075 LPFC_SLI3_HBQ_ENABLED))
1076 lpfc_post_buffer(phba,
1077 pring,
1078 1);
1079 break;
1080 }
1081 }
1082 }
1083 }
1084
James Smart4fede782010-01-26 23:08:55 -05001085 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -04001086 if (phba->sli_rev == LPFC_SLI_REV4) {
1087 evt_dat->immed_dat = phba->ctx_idx;
James Smart6dd9e312013-01-03 15:43:37 -05001088 phba->ctx_idx = (phba->ctx_idx + 1) % LPFC_CT_CTX_MAX;
James Smart589a52d2010-07-14 15:30:54 -04001089 /* Provide warning for over-run of the ct_ctx array */
James Smart6dd9e312013-01-03 15:43:37 -05001090 if (phba->ct_ctx[evt_dat->immed_dat].valid ==
James Smart589a52d2010-07-14 15:30:54 -04001091 UNSOL_VALID)
1092 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
1093 "2717 CT context array entry "
1094 "[%d] over-run: oxid:x%x, "
1095 "sid:x%x\n", phba->ctx_idx,
1096 phba->ct_ctx[
1097 evt_dat->immed_dat].oxid,
1098 phba->ct_ctx[
1099 evt_dat->immed_dat].SID);
James Smart7851fe22011-07-22 18:36:52 -04001100 phba->ct_ctx[evt_dat->immed_dat].rxid =
1101 piocbq->iocb.ulpContext;
James Smartf1c3b0f2009-07-19 10:01:32 -04001102 phba->ct_ctx[evt_dat->immed_dat].oxid =
James Smart7851fe22011-07-22 18:36:52 -04001103 piocbq->iocb.unsli3.rcvsli3.ox_id;
James Smartf1c3b0f2009-07-19 10:01:32 -04001104 phba->ct_ctx[evt_dat->immed_dat].SID =
1105 piocbq->iocb.un.rcvels.remoteID;
James Smart6dd9e312013-01-03 15:43:37 -05001106 phba->ct_ctx[evt_dat->immed_dat].valid = UNSOL_VALID;
James Smartf1c3b0f2009-07-19 10:01:32 -04001107 } else
1108 evt_dat->immed_dat = piocbq->iocb.ulpContext;
1109
1110 evt_dat->type = FC_REG_CT_EVENT;
1111 list_add(&evt_dat->node, &evt->events_to_see);
James Smart4cc0e562010-01-26 23:09:48 -05001112 if (evt_req_id == SLI_CT_ELX_LOOPBACK) {
1113 wake_up_interruptible(&evt->wq);
1114 lpfc_bsg_event_unref(evt);
James Smartf1c3b0f2009-07-19 10:01:32 -04001115 break;
James Smart4cc0e562010-01-26 23:09:48 -05001116 }
1117
1118 list_move(evt->events_to_see.prev, &evt->events_to_get);
James Smart4cc0e562010-01-26 23:09:48 -05001119
James Smarta33c4f72013-03-01 16:36:00 -05001120 dd_data = (struct bsg_job_data *)evt->dd_data;
1121 job = dd_data->set_job;
1122 dd_data->set_job = NULL;
1123 lpfc_bsg_event_unref(evt);
James Smart4cc0e562010-01-26 23:09:48 -05001124 if (job) {
1125 job->reply->reply_payload_rcv_len = size;
1126 /* make error code available to userspace */
1127 job->reply->result = 0;
1128 job->dd_data = NULL;
1129 /* complete the job back to userspace */
1130 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1131 job->job_done(job);
1132 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1133 }
James Smartf1c3b0f2009-07-19 10:01:32 -04001134 }
James Smart4fede782010-01-26 23:08:55 -05001135 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -04001136
1137error_ct_unsol_exit:
1138 if (!list_empty(&head))
1139 list_del(&head);
James Smart1b511972011-12-13 13:23:09 -05001140 if ((phba->sli_rev < LPFC_SLI_REV4) &&
1141 (evt_req_id == SLI_CT_ELX_LOOPBACK))
James Smart4cc0e562010-01-26 23:09:48 -05001142 return 0;
James Smart4fede782010-01-26 23:08:55 -05001143 return 1;
James Smartf1c3b0f2009-07-19 10:01:32 -04001144}
1145
1146/**
James Smart6dd9e312013-01-03 15:43:37 -05001147 * lpfc_bsg_ct_unsol_abort - handler ct abort to management plane
1148 * @phba: Pointer to HBA context object.
1149 * @dmabuf: pointer to a dmabuf that describes the FC sequence
1150 *
1151 * This function handles abort to the CT command toward management plane
1152 * for SLI4 port.
1153 *
1154 * If the pending context of a CT command to management plane present, clears
1155 * such context and returns 1 for handled; otherwise, it returns 0 indicating
1156 * no context exists.
1157 **/
1158int
1159lpfc_bsg_ct_unsol_abort(struct lpfc_hba *phba, struct hbq_dmabuf *dmabuf)
1160{
1161 struct fc_frame_header fc_hdr;
1162 struct fc_frame_header *fc_hdr_ptr = &fc_hdr;
1163 int ctx_idx, handled = 0;
1164 uint16_t oxid, rxid;
1165 uint32_t sid;
1166
1167 memcpy(fc_hdr_ptr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
1168 sid = sli4_sid_from_fc_hdr(fc_hdr_ptr);
1169 oxid = be16_to_cpu(fc_hdr_ptr->fh_ox_id);
1170 rxid = be16_to_cpu(fc_hdr_ptr->fh_rx_id);
1171
1172 for (ctx_idx = 0; ctx_idx < LPFC_CT_CTX_MAX; ctx_idx++) {
1173 if (phba->ct_ctx[ctx_idx].valid != UNSOL_VALID)
1174 continue;
1175 if (phba->ct_ctx[ctx_idx].rxid != rxid)
1176 continue;
1177 if (phba->ct_ctx[ctx_idx].oxid != oxid)
1178 continue;
1179 if (phba->ct_ctx[ctx_idx].SID != sid)
1180 continue;
1181 phba->ct_ctx[ctx_idx].valid = UNSOL_INVALID;
1182 handled = 1;
1183 }
1184 return handled;
1185}
1186
1187/**
James Smart4cc0e562010-01-26 23:09:48 -05001188 * lpfc_bsg_hba_set_event - process a SET_EVENT bsg vendor command
James Smartf1c3b0f2009-07-19 10:01:32 -04001189 * @job: SET_EVENT fc_bsg_job
James Smart3b5dd522010-01-26 23:10:15 -05001190 **/
James Smartf1c3b0f2009-07-19 10:01:32 -04001191static int
James Smart4cc0e562010-01-26 23:09:48 -05001192lpfc_bsg_hba_set_event(struct fc_bsg_job *job)
James Smartf1c3b0f2009-07-19 10:01:32 -04001193{
1194 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
1195 struct lpfc_hba *phba = vport->phba;
1196 struct set_ct_event *event_req;
James Smart4cc0e562010-01-26 23:09:48 -05001197 struct lpfc_bsg_event *evt;
James Smartf1c3b0f2009-07-19 10:01:32 -04001198 int rc = 0;
James Smart4cc0e562010-01-26 23:09:48 -05001199 struct bsg_job_data *dd_data = NULL;
1200 uint32_t ev_mask;
1201 unsigned long flags;
James Smartf1c3b0f2009-07-19 10:01:32 -04001202
1203 if (job->request_len <
1204 sizeof(struct fc_bsg_request) + sizeof(struct set_ct_event)) {
1205 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1206 "2612 Received SET_CT_EVENT below minimum "
1207 "size\n");
James Smart4cc0e562010-01-26 23:09:48 -05001208 rc = -EINVAL;
1209 goto job_error;
1210 }
1211
James Smartf1c3b0f2009-07-19 10:01:32 -04001212 event_req = (struct set_ct_event *)
1213 job->request->rqst_data.h_vendor.vendor_cmd;
James Smart4cc0e562010-01-26 23:09:48 -05001214 ev_mask = ((uint32_t)(unsigned long)event_req->type_mask &
1215 FC_REG_EVENT_MASK);
James Smart4fede782010-01-26 23:08:55 -05001216 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -04001217 list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
1218 if (evt->reg_id == event_req->ev_reg_id) {
James Smart4cc0e562010-01-26 23:09:48 -05001219 lpfc_bsg_event_ref(evt);
James Smartf1c3b0f2009-07-19 10:01:32 -04001220 evt->wait_time_stamp = jiffies;
James Smarta33c4f72013-03-01 16:36:00 -05001221 dd_data = (struct bsg_job_data *)evt->dd_data;
James Smartf1c3b0f2009-07-19 10:01:32 -04001222 break;
1223 }
1224 }
James Smart4fede782010-01-26 23:08:55 -05001225 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -04001226
1227 if (&evt->node == &phba->ct_ev_waiters) {
1228 /* no event waiting struct yet - first call */
James Smarta33c4f72013-03-01 16:36:00 -05001229 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
1230 if (dd_data == NULL) {
1231 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1232 "2734 Failed allocation of dd_data\n");
1233 rc = -ENOMEM;
1234 goto job_error;
1235 }
James Smart4cc0e562010-01-26 23:09:48 -05001236 evt = lpfc_bsg_event_new(ev_mask, event_req->ev_reg_id,
James Smartf1c3b0f2009-07-19 10:01:32 -04001237 event_req->ev_req_id);
1238 if (!evt) {
1239 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1240 "2617 Failed allocation of event "
1241 "waiter\n");
James Smart4cc0e562010-01-26 23:09:48 -05001242 rc = -ENOMEM;
1243 goto job_error;
James Smartf1c3b0f2009-07-19 10:01:32 -04001244 }
James Smarta33c4f72013-03-01 16:36:00 -05001245 dd_data->type = TYPE_EVT;
1246 dd_data->set_job = NULL;
1247 dd_data->context_un.evt = evt;
1248 evt->dd_data = (void *)dd_data;
James Smart4fede782010-01-26 23:08:55 -05001249 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -04001250 list_add(&evt->node, &phba->ct_ev_waiters);
James Smart4cc0e562010-01-26 23:09:48 -05001251 lpfc_bsg_event_ref(evt);
1252 evt->wait_time_stamp = jiffies;
James Smart4fede782010-01-26 23:08:55 -05001253 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -04001254 }
1255
James Smart4fede782010-01-26 23:08:55 -05001256 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smart4cc0e562010-01-26 23:09:48 -05001257 evt->waiting = 1;
James Smarta33c4f72013-03-01 16:36:00 -05001258 dd_data->set_job = job; /* for unsolicited command */
James Smart4cc0e562010-01-26 23:09:48 -05001259 job->dd_data = dd_data; /* for fc transport timeout callback*/
James Smart4fede782010-01-26 23:08:55 -05001260 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smart4cc0e562010-01-26 23:09:48 -05001261 return 0; /* call job done later */
James Smartf1c3b0f2009-07-19 10:01:32 -04001262
James Smart4cc0e562010-01-26 23:09:48 -05001263job_error:
1264 if (dd_data != NULL)
1265 kfree(dd_data);
James Smartf1c3b0f2009-07-19 10:01:32 -04001266
James Smart4cc0e562010-01-26 23:09:48 -05001267 job->dd_data = NULL;
1268 return rc;
James Smartf1c3b0f2009-07-19 10:01:32 -04001269}
1270
1271/**
James Smart4cc0e562010-01-26 23:09:48 -05001272 * lpfc_bsg_hba_get_event - process a GET_EVENT bsg vendor command
James Smartf1c3b0f2009-07-19 10:01:32 -04001273 * @job: GET_EVENT fc_bsg_job
James Smart3b5dd522010-01-26 23:10:15 -05001274 **/
James Smartf1c3b0f2009-07-19 10:01:32 -04001275static int
James Smart4cc0e562010-01-26 23:09:48 -05001276lpfc_bsg_hba_get_event(struct fc_bsg_job *job)
James Smartf1c3b0f2009-07-19 10:01:32 -04001277{
1278 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
1279 struct lpfc_hba *phba = vport->phba;
1280 struct get_ct_event *event_req;
1281 struct get_ct_event_reply *event_reply;
James Smart9a803a72013-09-06 12:17:56 -04001282 struct lpfc_bsg_event *evt, *evt_next;
James Smartf1c3b0f2009-07-19 10:01:32 -04001283 struct event_data *evt_dat = NULL;
James Smart4fede782010-01-26 23:08:55 -05001284 unsigned long flags;
James Smart4cc0e562010-01-26 23:09:48 -05001285 uint32_t rc = 0;
James Smartf1c3b0f2009-07-19 10:01:32 -04001286
1287 if (job->request_len <
1288 sizeof(struct fc_bsg_request) + sizeof(struct get_ct_event)) {
1289 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1290 "2613 Received GET_CT_EVENT request below "
1291 "minimum size\n");
James Smart4cc0e562010-01-26 23:09:48 -05001292 rc = -EINVAL;
1293 goto job_error;
James Smartf1c3b0f2009-07-19 10:01:32 -04001294 }
1295
1296 event_req = (struct get_ct_event *)
1297 job->request->rqst_data.h_vendor.vendor_cmd;
1298
1299 event_reply = (struct get_ct_event_reply *)
1300 job->reply->reply_data.vendor_reply.vendor_rsp;
James Smart4fede782010-01-26 23:08:55 -05001301 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smart9a803a72013-09-06 12:17:56 -04001302 list_for_each_entry_safe(evt, evt_next, &phba->ct_ev_waiters, node) {
James Smartf1c3b0f2009-07-19 10:01:32 -04001303 if (evt->reg_id == event_req->ev_reg_id) {
1304 if (list_empty(&evt->events_to_get))
1305 break;
James Smart4cc0e562010-01-26 23:09:48 -05001306 lpfc_bsg_event_ref(evt);
James Smartf1c3b0f2009-07-19 10:01:32 -04001307 evt->wait_time_stamp = jiffies;
1308 evt_dat = list_entry(evt->events_to_get.prev,
1309 struct event_data, node);
1310 list_del(&evt_dat->node);
1311 break;
1312 }
1313 }
James Smart4fede782010-01-26 23:08:55 -05001314 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -04001315
James Smart4cc0e562010-01-26 23:09:48 -05001316 /* The app may continue to ask for event data until it gets
1317 * an error indicating that there isn't anymore
1318 */
1319 if (evt_dat == NULL) {
James Smartf1c3b0f2009-07-19 10:01:32 -04001320 job->reply->reply_payload_rcv_len = 0;
1321 rc = -ENOENT;
James Smart4cc0e562010-01-26 23:09:48 -05001322 goto job_error;
James Smartf1c3b0f2009-07-19 10:01:32 -04001323 }
1324
James Smart4cc0e562010-01-26 23:09:48 -05001325 if (evt_dat->len > job->request_payload.payload_len) {
1326 evt_dat->len = job->request_payload.payload_len;
1327 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1328 "2618 Truncated event data at %d "
1329 "bytes\n",
1330 job->request_payload.payload_len);
James Smartf1c3b0f2009-07-19 10:01:32 -04001331 }
1332
James Smart4cc0e562010-01-26 23:09:48 -05001333 event_reply->type = evt_dat->type;
James Smartf1c3b0f2009-07-19 10:01:32 -04001334 event_reply->immed_data = evt_dat->immed_dat;
James Smartf1c3b0f2009-07-19 10:01:32 -04001335 if (evt_dat->len > 0)
1336 job->reply->reply_payload_rcv_len =
James Smart4cc0e562010-01-26 23:09:48 -05001337 sg_copy_from_buffer(job->request_payload.sg_list,
1338 job->request_payload.sg_cnt,
James Smartf1c3b0f2009-07-19 10:01:32 -04001339 evt_dat->data, evt_dat->len);
1340 else
1341 job->reply->reply_payload_rcv_len = 0;
James Smartf1c3b0f2009-07-19 10:01:32 -04001342
James Smart4cc0e562010-01-26 23:09:48 -05001343 if (evt_dat) {
James Smartf1c3b0f2009-07-19 10:01:32 -04001344 kfree(evt_dat->data);
James Smart4cc0e562010-01-26 23:09:48 -05001345 kfree(evt_dat);
1346 }
1347
James Smart4fede782010-01-26 23:08:55 -05001348 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smart4cc0e562010-01-26 23:09:48 -05001349 lpfc_bsg_event_unref(evt);
James Smart4fede782010-01-26 23:08:55 -05001350 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smart4cc0e562010-01-26 23:09:48 -05001351 job->dd_data = NULL;
1352 job->reply->result = 0;
James Smartf1c3b0f2009-07-19 10:01:32 -04001353 job->job_done(job);
James Smart4cc0e562010-01-26 23:09:48 -05001354 return 0;
James Smartf1c3b0f2009-07-19 10:01:32 -04001355
James Smart4cc0e562010-01-26 23:09:48 -05001356job_error:
1357 job->dd_data = NULL;
1358 job->reply->result = rc;
James Smartf1c3b0f2009-07-19 10:01:32 -04001359 return rc;
1360}
1361
1362/**
James Smart3b5dd522010-01-26 23:10:15 -05001363 * lpfc_issue_ct_rsp_cmp - lpfc_issue_ct_rsp's completion handler
1364 * @phba: Pointer to HBA context object.
1365 * @cmdiocbq: Pointer to command iocb.
1366 * @rspiocbq: Pointer to response iocb.
1367 *
1368 * This function is the completion handler for iocbs issued using
1369 * lpfc_issue_ct_rsp_cmp function. This function is called by the
1370 * ring event handler function without any lock held. This function
1371 * can be called from both worker thread context and interrupt
1372 * context. This function also can be called from other thread which
1373 * cleans up the SLI layer objects.
1374 * This function copy the contents of the response iocb to the
1375 * response iocb memory object provided by the caller of
1376 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
1377 * sleeps for the iocb completion.
1378 **/
1379static void
1380lpfc_issue_ct_rsp_cmp(struct lpfc_hba *phba,
1381 struct lpfc_iocbq *cmdiocbq,
1382 struct lpfc_iocbq *rspiocbq)
1383{
1384 struct bsg_job_data *dd_data;
1385 struct fc_bsg_job *job;
1386 IOCB_t *rsp;
James Smarta33c4f72013-03-01 16:36:00 -05001387 struct lpfc_dmabuf *bmp, *cmp;
James Smart3b5dd522010-01-26 23:10:15 -05001388 struct lpfc_nodelist *ndlp;
1389 unsigned long flags;
1390 int rc = 0;
1391
James Smarta33c4f72013-03-01 16:36:00 -05001392 dd_data = cmdiocbq->context1;
1393
1394 /* Determine if job has been aborted */
James Smart3b5dd522010-01-26 23:10:15 -05001395 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smarta33c4f72013-03-01 16:36:00 -05001396 job = dd_data->set_job;
1397 if (job) {
1398 /* Prevent timeout handling from trying to abort job */
1399 job->dd_data = NULL;
1400 }
1401 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1402
James Smartb5a9b2d2013-09-06 12:19:45 -04001403 /* Close the timeout handler abort window */
1404 spin_lock_irqsave(&phba->hbalock, flags);
James Smart1b8d11a2013-09-06 12:20:51 -04001405 cmdiocbq->iocb_flag &= ~LPFC_IO_CMD_OUTSTANDING;
James Smartb5a9b2d2013-09-06 12:19:45 -04001406 spin_unlock_irqrestore(&phba->hbalock, flags);
1407
James Smarta33c4f72013-03-01 16:36:00 -05001408 ndlp = dd_data->context_un.iocb.ndlp;
1409 cmp = cmdiocbq->context2;
1410 bmp = cmdiocbq->context3;
1411 rsp = &rspiocbq->iocb;
1412
1413 /* Copy the completed job data or set the error status */
1414
1415 if (job) {
1416 if (rsp->ulpStatus) {
1417 if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
1418 switch (rsp->un.ulpWord[4] & IOERR_PARAM_MASK) {
1419 case IOERR_SEQUENCE_TIMEOUT:
1420 rc = -ETIMEDOUT;
1421 break;
1422 case IOERR_INVALID_RPI:
1423 rc = -EFAULT;
1424 break;
1425 default:
1426 rc = -EACCES;
1427 break;
1428 }
1429 } else {
1430 rc = -EACCES;
1431 }
1432 } else {
1433 job->reply->reply_payload_rcv_len = 0;
1434 }
James Smart3b5dd522010-01-26 23:10:15 -05001435 }
1436
James Smarta33c4f72013-03-01 16:36:00 -05001437 lpfc_free_bsg_buffers(phba, cmp);
James Smart3b5dd522010-01-26 23:10:15 -05001438 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
James Smarta33c4f72013-03-01 16:36:00 -05001439 kfree(bmp);
James Smart3b5dd522010-01-26 23:10:15 -05001440 lpfc_sli_release_iocbq(phba, cmdiocbq);
1441 lpfc_nlp_put(ndlp);
James Smart3b5dd522010-01-26 23:10:15 -05001442 kfree(dd_data);
James Smarta33c4f72013-03-01 16:36:00 -05001443
1444 /* Complete the job if the job is still active */
1445
1446 if (job) {
1447 job->reply->result = rc;
1448 job->job_done(job);
1449 }
James Smart3b5dd522010-01-26 23:10:15 -05001450 return;
1451}
1452
1453/**
1454 * lpfc_issue_ct_rsp - issue a ct response
1455 * @phba: Pointer to HBA context object.
1456 * @job: Pointer to the job object.
1457 * @tag: tag index value into the ports context exchange array.
1458 * @bmp: Pointer to a dma buffer descriptor.
1459 * @num_entry: Number of enties in the bde.
1460 **/
1461static int
1462lpfc_issue_ct_rsp(struct lpfc_hba *phba, struct fc_bsg_job *job, uint32_t tag,
James Smarta33c4f72013-03-01 16:36:00 -05001463 struct lpfc_dmabuf *cmp, struct lpfc_dmabuf *bmp,
1464 int num_entry)
James Smart3b5dd522010-01-26 23:10:15 -05001465{
1466 IOCB_t *icmd;
1467 struct lpfc_iocbq *ctiocb = NULL;
1468 int rc = 0;
1469 struct lpfc_nodelist *ndlp = NULL;
1470 struct bsg_job_data *dd_data;
James Smartb5a9b2d2013-09-06 12:19:45 -04001471 unsigned long flags;
James Smart3b5dd522010-01-26 23:10:15 -05001472 uint32_t creg_val;
1473
1474 /* allocate our bsg tracking structure */
1475 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
1476 if (!dd_data) {
1477 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1478 "2736 Failed allocation of dd_data\n");
1479 rc = -ENOMEM;
1480 goto no_dd_data;
1481 }
1482
1483 /* Allocate buffer for command iocb */
1484 ctiocb = lpfc_sli_get_iocbq(phba);
1485 if (!ctiocb) {
James Smartd439d282010-09-29 11:18:45 -04001486 rc = -ENOMEM;
James Smart3b5dd522010-01-26 23:10:15 -05001487 goto no_ctiocb;
1488 }
1489
1490 icmd = &ctiocb->iocb;
1491 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
1492 icmd->un.xseq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
1493 icmd->un.xseq64.bdl.addrLow = putPaddrLow(bmp->phys);
1494 icmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
1495 icmd->un.xseq64.bdl.bdeSize = (num_entry * sizeof(struct ulp_bde64));
1496 icmd->un.xseq64.w5.hcsw.Fctl = (LS | LA);
1497 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
1498 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_SOL_CTL;
1499 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
1500
1501 /* Fill in rest of iocb */
1502 icmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;
1503 icmd->ulpBdeCount = 1;
1504 icmd->ulpLe = 1;
1505 icmd->ulpClass = CLASS3;
1506 if (phba->sli_rev == LPFC_SLI_REV4) {
1507 /* Do not issue unsol response if oxid not marked as valid */
James Smart6dd9e312013-01-03 15:43:37 -05001508 if (phba->ct_ctx[tag].valid != UNSOL_VALID) {
James Smart3b5dd522010-01-26 23:10:15 -05001509 rc = IOCB_ERROR;
1510 goto issue_ct_rsp_exit;
1511 }
James Smart7851fe22011-07-22 18:36:52 -04001512 icmd->ulpContext = phba->ct_ctx[tag].rxid;
1513 icmd->unsli3.rcvsli3.ox_id = phba->ct_ctx[tag].oxid;
James Smart3b5dd522010-01-26 23:10:15 -05001514 ndlp = lpfc_findnode_did(phba->pport, phba->ct_ctx[tag].SID);
1515 if (!ndlp) {
1516 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
1517 "2721 ndlp null for oxid %x SID %x\n",
1518 icmd->ulpContext,
1519 phba->ct_ctx[tag].SID);
1520 rc = IOCB_ERROR;
1521 goto issue_ct_rsp_exit;
1522 }
James Smart589a52d2010-07-14 15:30:54 -04001523
1524 /* Check if the ndlp is active */
1525 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smarta33c4f72013-03-01 16:36:00 -05001526 rc = IOCB_ERROR;
James Smart589a52d2010-07-14 15:30:54 -04001527 goto issue_ct_rsp_exit;
1528 }
1529
1530 /* get a refernece count so the ndlp doesn't go away while
1531 * we respond
1532 */
1533 if (!lpfc_nlp_get(ndlp)) {
James Smarta33c4f72013-03-01 16:36:00 -05001534 rc = IOCB_ERROR;
James Smart589a52d2010-07-14 15:30:54 -04001535 goto issue_ct_rsp_exit;
1536 }
1537
James Smart7851fe22011-07-22 18:36:52 -04001538 icmd->un.ulpWord[3] =
James Smart6d368e52011-05-24 11:44:12 -04001539 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
1540
James Smart3b5dd522010-01-26 23:10:15 -05001541 /* The exchange is done, mark the entry as invalid */
James Smart6dd9e312013-01-03 15:43:37 -05001542 phba->ct_ctx[tag].valid = UNSOL_INVALID;
James Smart3b5dd522010-01-26 23:10:15 -05001543 } else
1544 icmd->ulpContext = (ushort) tag;
1545
1546 icmd->ulpTimeout = phba->fc_ratov * 2;
1547
1548 /* Xmit CT response on exchange <xid> */
1549 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart7851fe22011-07-22 18:36:52 -04001550 "2722 Xmit CT response on exchange x%x Data: x%x x%x x%x\n",
1551 icmd->ulpContext, icmd->ulpIoTag, tag, phba->link_state);
James Smart3b5dd522010-01-26 23:10:15 -05001552
1553 ctiocb->iocb_cmpl = NULL;
1554 ctiocb->iocb_flag |= LPFC_IO_LIBDFC;
1555 ctiocb->vport = phba->pport;
James Smarta33c4f72013-03-01 16:36:00 -05001556 ctiocb->context1 = dd_data;
1557 ctiocb->context2 = cmp;
James Smart3b5dd522010-01-26 23:10:15 -05001558 ctiocb->context3 = bmp;
James Smartd5ce53b2013-04-17 20:17:26 -04001559 ctiocb->context_un.ndlp = ndlp;
James Smart3b5dd522010-01-26 23:10:15 -05001560 ctiocb->iocb_cmpl = lpfc_issue_ct_rsp_cmp;
James Smarta33c4f72013-03-01 16:36:00 -05001561
James Smart3b5dd522010-01-26 23:10:15 -05001562 dd_data->type = TYPE_IOCB;
James Smarta33c4f72013-03-01 16:36:00 -05001563 dd_data->set_job = job;
James Smart3b5dd522010-01-26 23:10:15 -05001564 dd_data->context_un.iocb.cmdiocbq = ctiocb;
James Smart3b5dd522010-01-26 23:10:15 -05001565 dd_data->context_un.iocb.ndlp = ndlp;
James Smarta33c4f72013-03-01 16:36:00 -05001566 dd_data->context_un.iocb.rmp = NULL;
1567 job->dd_data = dd_data;
James Smart3b5dd522010-01-26 23:10:15 -05001568
1569 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -05001570 if (lpfc_readl(phba->HCregaddr, &creg_val)) {
1571 rc = -IOCB_ERROR;
1572 goto issue_ct_rsp_exit;
1573 }
James Smart3b5dd522010-01-26 23:10:15 -05001574 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
1575 writel(creg_val, phba->HCregaddr);
1576 readl(phba->HCregaddr); /* flush */
1577 }
1578
1579 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
1580
James Smartb5a9b2d2013-09-06 12:19:45 -04001581 if (rc == IOCB_SUCCESS) {
1582 spin_lock_irqsave(&phba->hbalock, flags);
1583 /* make sure the I/O had not been completed/released */
1584 if (ctiocb->iocb_flag & LPFC_IO_LIBDFC) {
1585 /* open up abort window to timeout handler */
James Smart1b8d11a2013-09-06 12:20:51 -04001586 ctiocb->iocb_flag |= LPFC_IO_CMD_OUTSTANDING;
James Smartb5a9b2d2013-09-06 12:19:45 -04001587 }
1588 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart3b5dd522010-01-26 23:10:15 -05001589 return 0; /* done for now */
James Smartb5a9b2d2013-09-06 12:19:45 -04001590 }
1591
1592 /* iocb failed so cleanup */
1593 job->dd_data = NULL;
James Smart3b5dd522010-01-26 23:10:15 -05001594
1595issue_ct_rsp_exit:
1596 lpfc_sli_release_iocbq(phba, ctiocb);
1597no_ctiocb:
1598 kfree(dd_data);
1599no_dd_data:
1600 return rc;
1601}
1602
1603/**
1604 * lpfc_bsg_send_mgmt_rsp - process a SEND_MGMT_RESP bsg vendor command
1605 * @job: SEND_MGMT_RESP fc_bsg_job
1606 **/
1607static int
1608lpfc_bsg_send_mgmt_rsp(struct fc_bsg_job *job)
1609{
1610 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
1611 struct lpfc_hba *phba = vport->phba;
1612 struct send_mgmt_resp *mgmt_resp = (struct send_mgmt_resp *)
1613 job->request->rqst_data.h_vendor.vendor_cmd;
1614 struct ulp_bde64 *bpl;
James Smarta33c4f72013-03-01 16:36:00 -05001615 struct lpfc_dmabuf *bmp = NULL, *cmp = NULL;
1616 int bpl_entries;
James Smart3b5dd522010-01-26 23:10:15 -05001617 uint32_t tag = mgmt_resp->tag;
1618 unsigned long reqbfrcnt =
1619 (unsigned long)job->request_payload.payload_len;
1620 int rc = 0;
1621
1622 /* in case no data is transferred */
1623 job->reply->reply_payload_rcv_len = 0;
1624
1625 if (!reqbfrcnt || (reqbfrcnt > (80 * BUF_SZ_4K))) {
1626 rc = -ERANGE;
1627 goto send_mgmt_rsp_exit;
1628 }
1629
1630 bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
1631 if (!bmp) {
1632 rc = -ENOMEM;
1633 goto send_mgmt_rsp_exit;
1634 }
1635
1636 bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
1637 if (!bmp->virt) {
1638 rc = -ENOMEM;
1639 goto send_mgmt_rsp_free_bmp;
1640 }
1641
1642 INIT_LIST_HEAD(&bmp->list);
1643 bpl = (struct ulp_bde64 *) bmp->virt;
James Smarta33c4f72013-03-01 16:36:00 -05001644 bpl_entries = (LPFC_BPL_SIZE/sizeof(struct ulp_bde64));
1645 cmp = lpfc_alloc_bsg_buffers(phba, job->request_payload.payload_len,
1646 1, bpl, &bpl_entries);
1647 if (!cmp) {
1648 rc = -ENOMEM;
1649 goto send_mgmt_rsp_free_bmp;
James Smart3b5dd522010-01-26 23:10:15 -05001650 }
James Smarta33c4f72013-03-01 16:36:00 -05001651 lpfc_bsg_copy_data(cmp, &job->request_payload,
1652 job->request_payload.payload_len, 1);
James Smart3b5dd522010-01-26 23:10:15 -05001653
James Smarta33c4f72013-03-01 16:36:00 -05001654 rc = lpfc_issue_ct_rsp(phba, job, tag, cmp, bmp, bpl_entries);
James Smart3b5dd522010-01-26 23:10:15 -05001655
1656 if (rc == IOCB_SUCCESS)
1657 return 0; /* done for now */
1658
James Smart3b5dd522010-01-26 23:10:15 -05001659 rc = -EACCES;
James Smarta33c4f72013-03-01 16:36:00 -05001660
1661 lpfc_free_bsg_buffers(phba, cmp);
James Smart3b5dd522010-01-26 23:10:15 -05001662
1663send_mgmt_rsp_free_bmp:
James Smarta33c4f72013-03-01 16:36:00 -05001664 if (bmp->virt)
1665 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
James Smart3b5dd522010-01-26 23:10:15 -05001666 kfree(bmp);
1667send_mgmt_rsp_exit:
1668 /* make error code available to userspace */
1669 job->reply->result = rc;
1670 job->dd_data = NULL;
1671 return rc;
1672}
1673
1674/**
James Smart7ad20aa2011-05-24 11:44:28 -04001675 * lpfc_bsg_diag_mode_enter - process preparing into device diag loopback mode
1676 * @phba: Pointer to HBA context object.
James Smart3b5dd522010-01-26 23:10:15 -05001677 *
James Smart7ad20aa2011-05-24 11:44:28 -04001678 * This function is responsible for preparing driver for diag loopback
1679 * on device.
1680 */
1681static int
James Smart88a2cfb2011-07-22 18:36:33 -04001682lpfc_bsg_diag_mode_enter(struct lpfc_hba *phba)
James Smart7ad20aa2011-05-24 11:44:28 -04001683{
1684 struct lpfc_vport **vports;
1685 struct Scsi_Host *shost;
1686 struct lpfc_sli *psli;
1687 struct lpfc_sli_ring *pring;
1688 int i = 0;
1689
1690 psli = &phba->sli;
1691 if (!psli)
1692 return -ENODEV;
1693
1694 pring = &psli->ring[LPFC_FCP_RING];
1695 if (!pring)
1696 return -ENODEV;
1697
1698 if ((phba->link_state == LPFC_HBA_ERROR) ||
1699 (psli->sli_flag & LPFC_BLOCK_MGMT_IO) ||
1700 (!(psli->sli_flag & LPFC_SLI_ACTIVE)))
1701 return -EACCES;
1702
1703 vports = lpfc_create_vport_work_array(phba);
1704 if (vports) {
1705 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
1706 shost = lpfc_shost_from_vport(vports[i]);
1707 scsi_block_requests(shost);
1708 }
1709 lpfc_destroy_vport_work_array(phba, vports);
1710 } else {
1711 shost = lpfc_shost_from_vport(phba->pport);
1712 scsi_block_requests(shost);
1713 }
1714
James Smart0e9bb8d2013-03-01 16:35:12 -05001715 while (!list_empty(&pring->txcmplq)) {
James Smart7ad20aa2011-05-24 11:44:28 -04001716 if (i++ > 500) /* wait up to 5 seconds */
1717 break;
1718 msleep(10);
1719 }
1720 return 0;
1721}
1722
1723/**
1724 * lpfc_bsg_diag_mode_exit - exit process from device diag loopback mode
1725 * @phba: Pointer to HBA context object.
James Smart7ad20aa2011-05-24 11:44:28 -04001726 *
1727 * This function is responsible for driver exit processing of setting up
1728 * diag loopback mode on device.
1729 */
1730static void
1731lpfc_bsg_diag_mode_exit(struct lpfc_hba *phba)
1732{
1733 struct Scsi_Host *shost;
1734 struct lpfc_vport **vports;
1735 int i;
1736
1737 vports = lpfc_create_vport_work_array(phba);
1738 if (vports) {
1739 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
1740 shost = lpfc_shost_from_vport(vports[i]);
1741 scsi_unblock_requests(shost);
1742 }
1743 lpfc_destroy_vport_work_array(phba, vports);
1744 } else {
1745 shost = lpfc_shost_from_vport(phba->pport);
1746 scsi_unblock_requests(shost);
1747 }
1748 return;
1749}
1750
1751/**
1752 * lpfc_sli3_bsg_diag_loopback_mode - process an sli3 bsg vendor command
1753 * @phba: Pointer to HBA context object.
1754 * @job: LPFC_BSG_VENDOR_DIAG_MODE
1755 *
1756 * This function is responsible for placing an sli3 port into diagnostic
1757 * loopback mode in order to perform a diagnostic loopback test.
James Smart3b5dd522010-01-26 23:10:15 -05001758 * All new scsi requests are blocked, a small delay is used to allow the
1759 * scsi requests to complete then the link is brought down. If the link is
1760 * is placed in loopback mode then scsi requests are again allowed
1761 * so the scsi mid-layer doesn't give up on the port.
1762 * All of this is done in-line.
1763 */
1764static int
James Smart7ad20aa2011-05-24 11:44:28 -04001765lpfc_sli3_bsg_diag_loopback_mode(struct lpfc_hba *phba, struct fc_bsg_job *job)
James Smart3b5dd522010-01-26 23:10:15 -05001766{
James Smart3b5dd522010-01-26 23:10:15 -05001767 struct diag_mode_set *loopback_mode;
James Smart3b5dd522010-01-26 23:10:15 -05001768 uint32_t link_flags;
1769 uint32_t timeout;
James Smart1b511972011-12-13 13:23:09 -05001770 LPFC_MBOXQ_t *pmboxq = NULL;
James Smartb76f2dc2011-07-22 18:37:42 -04001771 int mbxstatus = MBX_SUCCESS;
James Smart3b5dd522010-01-26 23:10:15 -05001772 int i = 0;
1773 int rc = 0;
1774
1775 /* no data to return just the return code */
1776 job->reply->reply_payload_rcv_len = 0;
1777
James Smart7ad20aa2011-05-24 11:44:28 -04001778 if (job->request_len < sizeof(struct fc_bsg_request) +
1779 sizeof(struct diag_mode_set)) {
James Smart3b5dd522010-01-26 23:10:15 -05001780 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
James Smart7ad20aa2011-05-24 11:44:28 -04001781 "2738 Received DIAG MODE request size:%d "
1782 "below the minimum size:%d\n",
1783 job->request_len,
1784 (int)(sizeof(struct fc_bsg_request) +
1785 sizeof(struct diag_mode_set)));
James Smart3b5dd522010-01-26 23:10:15 -05001786 rc = -EINVAL;
1787 goto job_error;
1788 }
1789
James Smart88a2cfb2011-07-22 18:36:33 -04001790 rc = lpfc_bsg_diag_mode_enter(phba);
James Smart7ad20aa2011-05-24 11:44:28 -04001791 if (rc)
1792 goto job_error;
1793
1794 /* bring the link to diagnostic mode */
James Smart3b5dd522010-01-26 23:10:15 -05001795 loopback_mode = (struct diag_mode_set *)
1796 job->request->rqst_data.h_vendor.vendor_cmd;
1797 link_flags = loopback_mode->type;
James Smart515e0aa2010-09-29 11:19:00 -04001798 timeout = loopback_mode->timeout * 100;
James Smart3b5dd522010-01-26 23:10:15 -05001799
James Smart3b5dd522010-01-26 23:10:15 -05001800 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1801 if (!pmboxq) {
1802 rc = -ENOMEM;
James Smart7ad20aa2011-05-24 11:44:28 -04001803 goto loopback_mode_exit;
James Smart3b5dd522010-01-26 23:10:15 -05001804 }
James Smart3b5dd522010-01-26 23:10:15 -05001805 memset((void *)pmboxq, 0, sizeof(LPFC_MBOXQ_t));
1806 pmboxq->u.mb.mbxCommand = MBX_DOWN_LINK;
1807 pmboxq->u.mb.mbxOwner = OWN_HOST;
1808
1809 mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
1810
1811 if ((mbxstatus == MBX_SUCCESS) && (pmboxq->u.mb.mbxStatus == 0)) {
1812 /* wait for link down before proceeding */
1813 i = 0;
1814 while (phba->link_state != LPFC_LINK_DOWN) {
1815 if (i++ > timeout) {
1816 rc = -ETIMEDOUT;
1817 goto loopback_mode_exit;
1818 }
James Smart3b5dd522010-01-26 23:10:15 -05001819 msleep(10);
1820 }
1821
1822 memset((void *)pmboxq, 0, sizeof(LPFC_MBOXQ_t));
1823 if (link_flags == INTERNAL_LOOP_BACK)
1824 pmboxq->u.mb.un.varInitLnk.link_flags = FLAGS_LOCAL_LB;
1825 else
1826 pmboxq->u.mb.un.varInitLnk.link_flags =
1827 FLAGS_TOPOLOGY_MODE_LOOP;
1828
1829 pmboxq->u.mb.mbxCommand = MBX_INIT_LINK;
1830 pmboxq->u.mb.mbxOwner = OWN_HOST;
1831
1832 mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq,
1833 LPFC_MBOX_TMO);
1834
1835 if ((mbxstatus != MBX_SUCCESS) || (pmboxq->u.mb.mbxStatus))
1836 rc = -ENODEV;
1837 else {
James Smart1b511972011-12-13 13:23:09 -05001838 spin_lock_irq(&phba->hbalock);
James Smart3b5dd522010-01-26 23:10:15 -05001839 phba->link_flag |= LS_LOOPBACK_MODE;
James Smart1b511972011-12-13 13:23:09 -05001840 spin_unlock_irq(&phba->hbalock);
James Smart3b5dd522010-01-26 23:10:15 -05001841 /* wait for the link attention interrupt */
1842 msleep(100);
1843
1844 i = 0;
1845 while (phba->link_state != LPFC_HBA_READY) {
1846 if (i++ > timeout) {
1847 rc = -ETIMEDOUT;
1848 break;
1849 }
1850
1851 msleep(10);
1852 }
1853 }
1854
1855 } else
1856 rc = -ENODEV;
1857
1858loopback_mode_exit:
James Smart7ad20aa2011-05-24 11:44:28 -04001859 lpfc_bsg_diag_mode_exit(phba);
James Smart3b5dd522010-01-26 23:10:15 -05001860
1861 /*
1862 * Let SLI layer release mboxq if mbox command completed after timeout.
1863 */
James Smart1b511972011-12-13 13:23:09 -05001864 if (pmboxq && mbxstatus != MBX_TIMEOUT)
James Smart3b5dd522010-01-26 23:10:15 -05001865 mempool_free(pmboxq, phba->mbox_mem_pool);
1866
1867job_error:
1868 /* make error code available to userspace */
1869 job->reply->result = rc;
1870 /* complete the job back to userspace if no error */
1871 if (rc == 0)
1872 job->job_done(job);
1873 return rc;
1874}
1875
1876/**
James Smart7ad20aa2011-05-24 11:44:28 -04001877 * lpfc_sli4_bsg_set_link_diag_state - set sli4 link diag state
1878 * @phba: Pointer to HBA context object.
1879 * @diag: Flag for set link to diag or nomral operation state.
1880 *
1881 * This function is responsible for issuing a sli4 mailbox command for setting
1882 * link to either diag state or normal operation state.
1883 */
1884static int
1885lpfc_sli4_bsg_set_link_diag_state(struct lpfc_hba *phba, uint32_t diag)
1886{
1887 LPFC_MBOXQ_t *pmboxq;
1888 struct lpfc_mbx_set_link_diag_state *link_diag_state;
1889 uint32_t req_len, alloc_len;
1890 int mbxstatus = MBX_SUCCESS, rc;
1891
1892 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1893 if (!pmboxq)
1894 return -ENOMEM;
1895
1896 req_len = (sizeof(struct lpfc_mbx_set_link_diag_state) -
1897 sizeof(struct lpfc_sli4_cfg_mhdr));
1898 alloc_len = lpfc_sli4_config(phba, pmboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
1899 LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_STATE,
1900 req_len, LPFC_SLI4_MBX_EMBED);
1901 if (alloc_len != req_len) {
1902 rc = -ENOMEM;
1903 goto link_diag_state_set_out;
1904 }
James Smart1b511972011-12-13 13:23:09 -05001905 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
1906 "3128 Set link to diagnostic state:x%x (x%x/x%x)\n",
1907 diag, phba->sli4_hba.lnk_info.lnk_tp,
1908 phba->sli4_hba.lnk_info.lnk_no);
1909
James Smart7ad20aa2011-05-24 11:44:28 -04001910 link_diag_state = &pmboxq->u.mqe.un.link_diag_state;
James Smart97315922012-08-03 12:32:52 -04001911 bf_set(lpfc_mbx_set_diag_state_diag_bit_valid, &link_diag_state->u.req,
1912 LPFC_DIAG_STATE_DIAG_BIT_VALID_CHANGE);
James Smart7ad20aa2011-05-24 11:44:28 -04001913 bf_set(lpfc_mbx_set_diag_state_link_num, &link_diag_state->u.req,
James Smart1b511972011-12-13 13:23:09 -05001914 phba->sli4_hba.lnk_info.lnk_no);
James Smart7ad20aa2011-05-24 11:44:28 -04001915 bf_set(lpfc_mbx_set_diag_state_link_type, &link_diag_state->u.req,
James Smart1b511972011-12-13 13:23:09 -05001916 phba->sli4_hba.lnk_info.lnk_tp);
James Smart7ad20aa2011-05-24 11:44:28 -04001917 if (diag)
1918 bf_set(lpfc_mbx_set_diag_state_diag,
1919 &link_diag_state->u.req, 1);
1920 else
1921 bf_set(lpfc_mbx_set_diag_state_diag,
1922 &link_diag_state->u.req, 0);
1923
1924 mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
1925
1926 if ((mbxstatus == MBX_SUCCESS) && (pmboxq->u.mb.mbxStatus == 0))
1927 rc = 0;
1928 else
1929 rc = -ENODEV;
1930
1931link_diag_state_set_out:
1932 if (pmboxq && (mbxstatus != MBX_TIMEOUT))
1933 mempool_free(pmboxq, phba->mbox_mem_pool);
1934
1935 return rc;
1936}
1937
1938/**
James Smart1b511972011-12-13 13:23:09 -05001939 * lpfc_sli4_bsg_set_internal_loopback - set sli4 internal loopback diagnostic
1940 * @phba: Pointer to HBA context object.
1941 *
1942 * This function is responsible for issuing a sli4 mailbox command for setting
1943 * up internal loopback diagnostic.
1944 */
1945static int
1946lpfc_sli4_bsg_set_internal_loopback(struct lpfc_hba *phba)
1947{
1948 LPFC_MBOXQ_t *pmboxq;
1949 uint32_t req_len, alloc_len;
1950 struct lpfc_mbx_set_link_diag_loopback *link_diag_loopback;
1951 int mbxstatus = MBX_SUCCESS, rc = 0;
1952
1953 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1954 if (!pmboxq)
1955 return -ENOMEM;
1956 req_len = (sizeof(struct lpfc_mbx_set_link_diag_loopback) -
1957 sizeof(struct lpfc_sli4_cfg_mhdr));
1958 alloc_len = lpfc_sli4_config(phba, pmboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
1959 LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_LOOPBACK,
1960 req_len, LPFC_SLI4_MBX_EMBED);
1961 if (alloc_len != req_len) {
1962 mempool_free(pmboxq, phba->mbox_mem_pool);
1963 return -ENOMEM;
1964 }
1965 link_diag_loopback = &pmboxq->u.mqe.un.link_diag_loopback;
1966 bf_set(lpfc_mbx_set_diag_state_link_num,
1967 &link_diag_loopback->u.req, phba->sli4_hba.lnk_info.lnk_no);
1968 bf_set(lpfc_mbx_set_diag_state_link_type,
1969 &link_diag_loopback->u.req, phba->sli4_hba.lnk_info.lnk_tp);
1970 bf_set(lpfc_mbx_set_diag_lpbk_type, &link_diag_loopback->u.req,
James Smart3ef6d242012-01-18 16:23:48 -05001971 LPFC_DIAG_LOOPBACK_TYPE_INTERNAL);
James Smart1b511972011-12-13 13:23:09 -05001972
1973 mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
1974 if ((mbxstatus != MBX_SUCCESS) || (pmboxq->u.mb.mbxStatus)) {
1975 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1976 "3127 Failed setup loopback mode mailbox "
1977 "command, rc:x%x, status:x%x\n", mbxstatus,
1978 pmboxq->u.mb.mbxStatus);
1979 rc = -ENODEV;
1980 }
1981 if (pmboxq && (mbxstatus != MBX_TIMEOUT))
1982 mempool_free(pmboxq, phba->mbox_mem_pool);
1983 return rc;
1984}
1985
1986/**
1987 * lpfc_sli4_diag_fcport_reg_setup - setup port registrations for diagnostic
1988 * @phba: Pointer to HBA context object.
1989 *
1990 * This function set up SLI4 FC port registrations for diagnostic run, which
1991 * includes all the rpis, vfi, and also vpi.
1992 */
1993static int
1994lpfc_sli4_diag_fcport_reg_setup(struct lpfc_hba *phba)
1995{
1996 int rc;
1997
1998 if (phba->pport->fc_flag & FC_VFI_REGISTERED) {
1999 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2000 "3136 Port still had vfi registered: "
2001 "mydid:x%x, fcfi:%d, vfi:%d, vpi:%d\n",
2002 phba->pport->fc_myDID, phba->fcf.fcfi,
2003 phba->sli4_hba.vfi_ids[phba->pport->vfi],
2004 phba->vpi_ids[phba->pport->vpi]);
2005 return -EINVAL;
2006 }
2007 rc = lpfc_issue_reg_vfi(phba->pport);
2008 return rc;
2009}
2010
2011/**
James Smart7ad20aa2011-05-24 11:44:28 -04002012 * lpfc_sli4_bsg_diag_loopback_mode - process an sli4 bsg vendor command
2013 * @phba: Pointer to HBA context object.
2014 * @job: LPFC_BSG_VENDOR_DIAG_MODE
2015 *
2016 * This function is responsible for placing an sli4 port into diagnostic
2017 * loopback mode in order to perform a diagnostic loopback test.
2018 */
2019static int
2020lpfc_sli4_bsg_diag_loopback_mode(struct lpfc_hba *phba, struct fc_bsg_job *job)
2021{
2022 struct diag_mode_set *loopback_mode;
James Smart1b511972011-12-13 13:23:09 -05002023 uint32_t link_flags, timeout;
2024 int i, rc = 0;
James Smart7ad20aa2011-05-24 11:44:28 -04002025
2026 /* no data to return just the return code */
2027 job->reply->reply_payload_rcv_len = 0;
2028
2029 if (job->request_len < sizeof(struct fc_bsg_request) +
2030 sizeof(struct diag_mode_set)) {
2031 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2032 "3011 Received DIAG MODE request size:%d "
2033 "below the minimum size:%d\n",
2034 job->request_len,
2035 (int)(sizeof(struct fc_bsg_request) +
2036 sizeof(struct diag_mode_set)));
2037 rc = -EINVAL;
2038 goto job_error;
2039 }
2040
James Smart88a2cfb2011-07-22 18:36:33 -04002041 rc = lpfc_bsg_diag_mode_enter(phba);
James Smart7ad20aa2011-05-24 11:44:28 -04002042 if (rc)
2043 goto job_error;
2044
James Smart1b511972011-12-13 13:23:09 -05002045 /* indicate we are in loobpack diagnostic mode */
2046 spin_lock_irq(&phba->hbalock);
2047 phba->link_flag |= LS_LOOPBACK_MODE;
2048 spin_unlock_irq(&phba->hbalock);
2049
2050 /* reset port to start frome scratch */
2051 rc = lpfc_selective_reset(phba);
2052 if (rc)
2053 goto job_error;
2054
James Smart7ad20aa2011-05-24 11:44:28 -04002055 /* bring the link to diagnostic mode */
James Smart1b511972011-12-13 13:23:09 -05002056 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
2057 "3129 Bring link to diagnostic state.\n");
James Smart7ad20aa2011-05-24 11:44:28 -04002058 loopback_mode = (struct diag_mode_set *)
2059 job->request->rqst_data.h_vendor.vendor_cmd;
2060 link_flags = loopback_mode->type;
2061 timeout = loopback_mode->timeout * 100;
2062
2063 rc = lpfc_sli4_bsg_set_link_diag_state(phba, 1);
James Smart1b511972011-12-13 13:23:09 -05002064 if (rc) {
2065 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2066 "3130 Failed to bring link to diagnostic "
2067 "state, rc:x%x\n", rc);
James Smart7ad20aa2011-05-24 11:44:28 -04002068 goto loopback_mode_exit;
James Smart1b511972011-12-13 13:23:09 -05002069 }
James Smart7ad20aa2011-05-24 11:44:28 -04002070
2071 /* wait for link down before proceeding */
2072 i = 0;
2073 while (phba->link_state != LPFC_LINK_DOWN) {
2074 if (i++ > timeout) {
2075 rc = -ETIMEDOUT;
James Smart1b511972011-12-13 13:23:09 -05002076 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
2077 "3131 Timeout waiting for link to "
2078 "diagnostic mode, timeout:%d ms\n",
2079 timeout * 10);
James Smart7ad20aa2011-05-24 11:44:28 -04002080 goto loopback_mode_exit;
2081 }
2082 msleep(10);
2083 }
James Smart7ad20aa2011-05-24 11:44:28 -04002084
James Smart1b511972011-12-13 13:23:09 -05002085 /* set up loopback mode */
2086 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
2087 "3132 Set up loopback mode:x%x\n", link_flags);
2088
2089 if (link_flags == INTERNAL_LOOP_BACK)
2090 rc = lpfc_sli4_bsg_set_internal_loopback(phba);
2091 else if (link_flags == EXTERNAL_LOOP_BACK)
2092 rc = lpfc_hba_init_link_fc_topology(phba,
2093 FLAGS_TOPOLOGY_MODE_PT_PT,
2094 MBX_NOWAIT);
James Smart7ad20aa2011-05-24 11:44:28 -04002095 else {
James Smart1b511972011-12-13 13:23:09 -05002096 rc = -EINVAL;
2097 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
2098 "3141 Loopback mode:x%x not supported\n",
2099 link_flags);
2100 goto loopback_mode_exit;
2101 }
2102
2103 if (!rc) {
James Smart7ad20aa2011-05-24 11:44:28 -04002104 /* wait for the link attention interrupt */
2105 msleep(100);
2106 i = 0;
James Smart1b511972011-12-13 13:23:09 -05002107 while (phba->link_state < LPFC_LINK_UP) {
2108 if (i++ > timeout) {
2109 rc = -ETIMEDOUT;
2110 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
2111 "3137 Timeout waiting for link up "
2112 "in loopback mode, timeout:%d ms\n",
2113 timeout * 10);
2114 break;
2115 }
2116 msleep(10);
2117 }
2118 }
2119
2120 /* port resource registration setup for loopback diagnostic */
2121 if (!rc) {
2122 /* set up a none zero myDID for loopback test */
2123 phba->pport->fc_myDID = 1;
2124 rc = lpfc_sli4_diag_fcport_reg_setup(phba);
2125 } else
2126 goto loopback_mode_exit;
2127
2128 if (!rc) {
2129 /* wait for the port ready */
2130 msleep(100);
2131 i = 0;
James Smart7ad20aa2011-05-24 11:44:28 -04002132 while (phba->link_state != LPFC_HBA_READY) {
2133 if (i++ > timeout) {
2134 rc = -ETIMEDOUT;
James Smart1b511972011-12-13 13:23:09 -05002135 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
2136 "3133 Timeout waiting for port "
2137 "loopback mode ready, timeout:%d ms\n",
2138 timeout * 10);
James Smart7ad20aa2011-05-24 11:44:28 -04002139 break;
2140 }
2141 msleep(10);
2142 }
2143 }
2144
2145loopback_mode_exit:
James Smart1b511972011-12-13 13:23:09 -05002146 /* clear loopback diagnostic mode */
2147 if (rc) {
2148 spin_lock_irq(&phba->hbalock);
2149 phba->link_flag &= ~LS_LOOPBACK_MODE;
2150 spin_unlock_irq(&phba->hbalock);
2151 }
James Smart7ad20aa2011-05-24 11:44:28 -04002152 lpfc_bsg_diag_mode_exit(phba);
2153
James Smart7ad20aa2011-05-24 11:44:28 -04002154job_error:
2155 /* make error code available to userspace */
2156 job->reply->result = rc;
2157 /* complete the job back to userspace if no error */
2158 if (rc == 0)
2159 job->job_done(job);
2160 return rc;
2161}
2162
2163/**
2164 * lpfc_bsg_diag_loopback_mode - bsg vendor command for diag loopback mode
2165 * @job: LPFC_BSG_VENDOR_DIAG_MODE
2166 *
2167 * This function is responsible for responding to check and dispatch bsg diag
2168 * command from the user to proper driver action routines.
2169 */
2170static int
2171lpfc_bsg_diag_loopback_mode(struct fc_bsg_job *job)
2172{
2173 struct Scsi_Host *shost;
2174 struct lpfc_vport *vport;
2175 struct lpfc_hba *phba;
2176 int rc;
2177
2178 shost = job->shost;
2179 if (!shost)
2180 return -ENODEV;
2181 vport = (struct lpfc_vport *)job->shost->hostdata;
2182 if (!vport)
2183 return -ENODEV;
2184 phba = vport->phba;
2185 if (!phba)
2186 return -ENODEV;
2187
2188 if (phba->sli_rev < LPFC_SLI_REV4)
2189 rc = lpfc_sli3_bsg_diag_loopback_mode(phba, job);
2190 else if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
2191 LPFC_SLI_INTF_IF_TYPE_2)
2192 rc = lpfc_sli4_bsg_diag_loopback_mode(phba, job);
2193 else
2194 rc = -ENODEV;
2195
2196 return rc;
James Smart7ad20aa2011-05-24 11:44:28 -04002197}
2198
2199/**
2200 * lpfc_sli4_bsg_diag_mode_end - sli4 bsg vendor command for ending diag mode
2201 * @job: LPFC_BSG_VENDOR_DIAG_MODE_END
2202 *
2203 * This function is responsible for responding to check and dispatch bsg diag
2204 * command from the user to proper driver action routines.
2205 */
2206static int
2207lpfc_sli4_bsg_diag_mode_end(struct fc_bsg_job *job)
2208{
2209 struct Scsi_Host *shost;
2210 struct lpfc_vport *vport;
2211 struct lpfc_hba *phba;
James Smart1b511972011-12-13 13:23:09 -05002212 struct diag_mode_set *loopback_mode_end_cmd;
2213 uint32_t timeout;
2214 int rc, i;
James Smart7ad20aa2011-05-24 11:44:28 -04002215
2216 shost = job->shost;
2217 if (!shost)
2218 return -ENODEV;
2219 vport = (struct lpfc_vport *)job->shost->hostdata;
2220 if (!vport)
2221 return -ENODEV;
2222 phba = vport->phba;
2223 if (!phba)
2224 return -ENODEV;
2225
2226 if (phba->sli_rev < LPFC_SLI_REV4)
2227 return -ENODEV;
2228 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
2229 LPFC_SLI_INTF_IF_TYPE_2)
2230 return -ENODEV;
2231
James Smart1b511972011-12-13 13:23:09 -05002232 /* clear loopback diagnostic mode */
2233 spin_lock_irq(&phba->hbalock);
2234 phba->link_flag &= ~LS_LOOPBACK_MODE;
2235 spin_unlock_irq(&phba->hbalock);
2236 loopback_mode_end_cmd = (struct diag_mode_set *)
2237 job->request->rqst_data.h_vendor.vendor_cmd;
2238 timeout = loopback_mode_end_cmd->timeout * 100;
2239
James Smart7ad20aa2011-05-24 11:44:28 -04002240 rc = lpfc_sli4_bsg_set_link_diag_state(phba, 0);
James Smart1b511972011-12-13 13:23:09 -05002241 if (rc) {
2242 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2243 "3139 Failed to bring link to diagnostic "
2244 "state, rc:x%x\n", rc);
2245 goto loopback_mode_end_exit;
2246 }
James Smart7ad20aa2011-05-24 11:44:28 -04002247
James Smart1b511972011-12-13 13:23:09 -05002248 /* wait for link down before proceeding */
2249 i = 0;
2250 while (phba->link_state != LPFC_LINK_DOWN) {
2251 if (i++ > timeout) {
James Smart1b511972011-12-13 13:23:09 -05002252 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
2253 "3140 Timeout waiting for link to "
2254 "diagnostic mode_end, timeout:%d ms\n",
2255 timeout * 10);
2256 /* there is nothing much we can do here */
2257 break;
2258 }
2259 msleep(10);
2260 }
James Smart7ad20aa2011-05-24 11:44:28 -04002261
James Smart1b511972011-12-13 13:23:09 -05002262 /* reset port resource registrations */
2263 rc = lpfc_selective_reset(phba);
2264 phba->pport->fc_myDID = 0;
2265
2266loopback_mode_end_exit:
2267 /* make return code available to userspace */
2268 job->reply->result = rc;
2269 /* complete the job back to userspace if no error */
2270 if (rc == 0)
2271 job->job_done(job);
James Smart7ad20aa2011-05-24 11:44:28 -04002272 return rc;
2273}
2274
2275/**
2276 * lpfc_sli4_bsg_link_diag_test - sli4 bsg vendor command for diag link test
2277 * @job: LPFC_BSG_VENDOR_DIAG_LINK_TEST
2278 *
2279 * This function is to perform SLI4 diag link test request from the user
2280 * applicaiton.
2281 */
2282static int
2283lpfc_sli4_bsg_link_diag_test(struct fc_bsg_job *job)
2284{
2285 struct Scsi_Host *shost;
2286 struct lpfc_vport *vport;
2287 struct lpfc_hba *phba;
2288 LPFC_MBOXQ_t *pmboxq;
2289 struct sli4_link_diag *link_diag_test_cmd;
2290 uint32_t req_len, alloc_len;
James Smart7ad20aa2011-05-24 11:44:28 -04002291 struct lpfc_mbx_run_link_diag_test *run_link_diag_test;
2292 union lpfc_sli4_cfg_shdr *shdr;
2293 uint32_t shdr_status, shdr_add_status;
2294 struct diag_status *diag_status_reply;
2295 int mbxstatus, rc = 0;
2296
2297 shost = job->shost;
2298 if (!shost) {
2299 rc = -ENODEV;
2300 goto job_error;
2301 }
2302 vport = (struct lpfc_vport *)job->shost->hostdata;
2303 if (!vport) {
2304 rc = -ENODEV;
2305 goto job_error;
2306 }
2307 phba = vport->phba;
2308 if (!phba) {
2309 rc = -ENODEV;
2310 goto job_error;
2311 }
2312
2313 if (phba->sli_rev < LPFC_SLI_REV4) {
2314 rc = -ENODEV;
2315 goto job_error;
2316 }
2317 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
2318 LPFC_SLI_INTF_IF_TYPE_2) {
2319 rc = -ENODEV;
2320 goto job_error;
2321 }
2322
2323 if (job->request_len < sizeof(struct fc_bsg_request) +
2324 sizeof(struct sli4_link_diag)) {
2325 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2326 "3013 Received LINK DIAG TEST request "
2327 " size:%d below the minimum size:%d\n",
2328 job->request_len,
2329 (int)(sizeof(struct fc_bsg_request) +
2330 sizeof(struct sli4_link_diag)));
2331 rc = -EINVAL;
2332 goto job_error;
2333 }
2334
James Smart88a2cfb2011-07-22 18:36:33 -04002335 rc = lpfc_bsg_diag_mode_enter(phba);
James Smart7ad20aa2011-05-24 11:44:28 -04002336 if (rc)
2337 goto job_error;
2338
2339 link_diag_test_cmd = (struct sli4_link_diag *)
2340 job->request->rqst_data.h_vendor.vendor_cmd;
James Smart7ad20aa2011-05-24 11:44:28 -04002341
2342 rc = lpfc_sli4_bsg_set_link_diag_state(phba, 1);
2343
2344 if (rc)
2345 goto job_error;
2346
2347 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2348 if (!pmboxq) {
2349 rc = -ENOMEM;
2350 goto link_diag_test_exit;
2351 }
2352
2353 req_len = (sizeof(struct lpfc_mbx_set_link_diag_state) -
2354 sizeof(struct lpfc_sli4_cfg_mhdr));
2355 alloc_len = lpfc_sli4_config(phba, pmboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
2356 LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_STATE,
2357 req_len, LPFC_SLI4_MBX_EMBED);
2358 if (alloc_len != req_len) {
2359 rc = -ENOMEM;
2360 goto link_diag_test_exit;
2361 }
2362 run_link_diag_test = &pmboxq->u.mqe.un.link_diag_test;
2363 bf_set(lpfc_mbx_run_diag_test_link_num, &run_link_diag_test->u.req,
James Smart1b511972011-12-13 13:23:09 -05002364 phba->sli4_hba.lnk_info.lnk_no);
James Smart7ad20aa2011-05-24 11:44:28 -04002365 bf_set(lpfc_mbx_run_diag_test_link_type, &run_link_diag_test->u.req,
James Smart1b511972011-12-13 13:23:09 -05002366 phba->sli4_hba.lnk_info.lnk_tp);
James Smart7ad20aa2011-05-24 11:44:28 -04002367 bf_set(lpfc_mbx_run_diag_test_test_id, &run_link_diag_test->u.req,
2368 link_diag_test_cmd->test_id);
2369 bf_set(lpfc_mbx_run_diag_test_loops, &run_link_diag_test->u.req,
2370 link_diag_test_cmd->loops);
2371 bf_set(lpfc_mbx_run_diag_test_test_ver, &run_link_diag_test->u.req,
2372 link_diag_test_cmd->test_version);
2373 bf_set(lpfc_mbx_run_diag_test_err_act, &run_link_diag_test->u.req,
2374 link_diag_test_cmd->error_action);
2375
2376 mbxstatus = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
2377
2378 shdr = (union lpfc_sli4_cfg_shdr *)
2379 &pmboxq->u.mqe.un.sli4_config.header.cfg_shdr;
2380 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
2381 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
2382 if (shdr_status || shdr_add_status || mbxstatus) {
2383 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
2384 "3010 Run link diag test mailbox failed with "
2385 "mbx_status x%x status x%x, add_status x%x\n",
2386 mbxstatus, shdr_status, shdr_add_status);
2387 }
2388
2389 diag_status_reply = (struct diag_status *)
2390 job->reply->reply_data.vendor_reply.vendor_rsp;
2391
2392 if (job->reply_len <
2393 sizeof(struct fc_bsg_request) + sizeof(struct diag_status)) {
2394 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2395 "3012 Received Run link diag test reply "
2396 "below minimum size (%d): reply_len:%d\n",
2397 (int)(sizeof(struct fc_bsg_request) +
2398 sizeof(struct diag_status)),
2399 job->reply_len);
2400 rc = -EINVAL;
2401 goto job_error;
2402 }
2403
2404 diag_status_reply->mbox_status = mbxstatus;
2405 diag_status_reply->shdr_status = shdr_status;
2406 diag_status_reply->shdr_add_status = shdr_add_status;
2407
2408link_diag_test_exit:
2409 rc = lpfc_sli4_bsg_set_link_diag_state(phba, 0);
2410
2411 if (pmboxq)
2412 mempool_free(pmboxq, phba->mbox_mem_pool);
2413
2414 lpfc_bsg_diag_mode_exit(phba);
2415
2416job_error:
2417 /* make error code available to userspace */
2418 job->reply->result = rc;
2419 /* complete the job back to userspace if no error */
2420 if (rc == 0)
2421 job->job_done(job);
2422 return rc;
2423}
2424
2425/**
James Smart3b5dd522010-01-26 23:10:15 -05002426 * lpfcdiag_loop_self_reg - obtains a remote port login id
2427 * @phba: Pointer to HBA context object
2428 * @rpi: Pointer to a remote port login id
2429 *
2430 * This function obtains a remote port login id so the diag loopback test
2431 * can send and receive its own unsolicited CT command.
2432 **/
James Smart40426292010-12-15 17:58:10 -05002433static int lpfcdiag_loop_self_reg(struct lpfc_hba *phba, uint16_t *rpi)
James Smart3b5dd522010-01-26 23:10:15 -05002434{
2435 LPFC_MBOXQ_t *mbox;
2436 struct lpfc_dmabuf *dmabuff;
2437 int status;
2438
2439 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2440 if (!mbox)
James Smartd439d282010-09-29 11:18:45 -04002441 return -ENOMEM;
James Smart3b5dd522010-01-26 23:10:15 -05002442
James Smart1b511972011-12-13 13:23:09 -05002443 if (phba->sli_rev < LPFC_SLI_REV4)
2444 status = lpfc_reg_rpi(phba, 0, phba->pport->fc_myDID,
2445 (uint8_t *)&phba->pport->fc_sparam,
2446 mbox, *rpi);
2447 else {
James Smart40426292010-12-15 17:58:10 -05002448 *rpi = lpfc_sli4_alloc_rpi(phba);
James Smart1b511972011-12-13 13:23:09 -05002449 status = lpfc_reg_rpi(phba, phba->pport->vpi,
2450 phba->pport->fc_myDID,
2451 (uint8_t *)&phba->pport->fc_sparam,
2452 mbox, *rpi);
2453 }
2454
James Smart3b5dd522010-01-26 23:10:15 -05002455 if (status) {
2456 mempool_free(mbox, phba->mbox_mem_pool);
James Smart40426292010-12-15 17:58:10 -05002457 if (phba->sli_rev == LPFC_SLI_REV4)
2458 lpfc_sli4_free_rpi(phba, *rpi);
James Smartd439d282010-09-29 11:18:45 -04002459 return -ENOMEM;
James Smart3b5dd522010-01-26 23:10:15 -05002460 }
2461
2462 dmabuff = (struct lpfc_dmabuf *) mbox->context1;
2463 mbox->context1 = NULL;
James Smartd439d282010-09-29 11:18:45 -04002464 mbox->context2 = NULL;
James Smart3b5dd522010-01-26 23:10:15 -05002465 status = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
2466
2467 if ((status != MBX_SUCCESS) || (mbox->u.mb.mbxStatus)) {
2468 lpfc_mbuf_free(phba, dmabuff->virt, dmabuff->phys);
2469 kfree(dmabuff);
2470 if (status != MBX_TIMEOUT)
2471 mempool_free(mbox, phba->mbox_mem_pool);
James Smart40426292010-12-15 17:58:10 -05002472 if (phba->sli_rev == LPFC_SLI_REV4)
2473 lpfc_sli4_free_rpi(phba, *rpi);
James Smartd439d282010-09-29 11:18:45 -04002474 return -ENODEV;
James Smart3b5dd522010-01-26 23:10:15 -05002475 }
2476
James Smart1b511972011-12-13 13:23:09 -05002477 if (phba->sli_rev < LPFC_SLI_REV4)
2478 *rpi = mbox->u.mb.un.varWords[0];
James Smart3b5dd522010-01-26 23:10:15 -05002479
2480 lpfc_mbuf_free(phba, dmabuff->virt, dmabuff->phys);
2481 kfree(dmabuff);
2482 mempool_free(mbox, phba->mbox_mem_pool);
2483 return 0;
2484}
2485
2486/**
2487 * lpfcdiag_loop_self_unreg - unregs from the rpi
2488 * @phba: Pointer to HBA context object
2489 * @rpi: Remote port login id
2490 *
2491 * This function unregisters the rpi obtained in lpfcdiag_loop_self_reg
2492 **/
2493static int lpfcdiag_loop_self_unreg(struct lpfc_hba *phba, uint16_t rpi)
2494{
2495 LPFC_MBOXQ_t *mbox;
2496 int status;
2497
2498 /* Allocate mboxq structure */
2499 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2500 if (mbox == NULL)
James Smartd439d282010-09-29 11:18:45 -04002501 return -ENOMEM;
James Smart3b5dd522010-01-26 23:10:15 -05002502
James Smart1b511972011-12-13 13:23:09 -05002503 if (phba->sli_rev < LPFC_SLI_REV4)
2504 lpfc_unreg_login(phba, 0, rpi, mbox);
2505 else
2506 lpfc_unreg_login(phba, phba->pport->vpi,
2507 phba->sli4_hba.rpi_ids[rpi], mbox);
2508
James Smart3b5dd522010-01-26 23:10:15 -05002509 status = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
2510
2511 if ((status != MBX_SUCCESS) || (mbox->u.mb.mbxStatus)) {
2512 if (status != MBX_TIMEOUT)
2513 mempool_free(mbox, phba->mbox_mem_pool);
James Smartd439d282010-09-29 11:18:45 -04002514 return -EIO;
James Smart3b5dd522010-01-26 23:10:15 -05002515 }
James Smart3b5dd522010-01-26 23:10:15 -05002516 mempool_free(mbox, phba->mbox_mem_pool);
James Smart40426292010-12-15 17:58:10 -05002517 if (phba->sli_rev == LPFC_SLI_REV4)
2518 lpfc_sli4_free_rpi(phba, rpi);
James Smart3b5dd522010-01-26 23:10:15 -05002519 return 0;
2520}
2521
2522/**
2523 * lpfcdiag_loop_get_xri - obtains the transmit and receive ids
2524 * @phba: Pointer to HBA context object
2525 * @rpi: Remote port login id
2526 * @txxri: Pointer to transmit exchange id
2527 * @rxxri: Pointer to response exchabge id
2528 *
2529 * This function obtains the transmit and receive ids required to send
2530 * an unsolicited ct command with a payload. A special lpfc FsType and CmdRsp
2531 * flags are used to the unsolicted response handler is able to process
2532 * the ct command sent on the same port.
2533 **/
2534static int lpfcdiag_loop_get_xri(struct lpfc_hba *phba, uint16_t rpi,
2535 uint16_t *txxri, uint16_t * rxxri)
2536{
2537 struct lpfc_bsg_event *evt;
2538 struct lpfc_iocbq *cmdiocbq, *rspiocbq;
2539 IOCB_t *cmd, *rsp;
2540 struct lpfc_dmabuf *dmabuf;
2541 struct ulp_bde64 *bpl = NULL;
2542 struct lpfc_sli_ct_request *ctreq = NULL;
2543 int ret_val = 0;
James Smartd439d282010-09-29 11:18:45 -04002544 int time_left;
James Smart5a0916b2013-07-15 18:31:42 -04002545 int iocb_stat = IOCB_SUCCESS;
James Smart3b5dd522010-01-26 23:10:15 -05002546 unsigned long flags;
2547
2548 *txxri = 0;
2549 *rxxri = 0;
2550 evt = lpfc_bsg_event_new(FC_REG_CT_EVENT, current->pid,
2551 SLI_CT_ELX_LOOPBACK);
2552 if (!evt)
James Smartd439d282010-09-29 11:18:45 -04002553 return -ENOMEM;
James Smart3b5dd522010-01-26 23:10:15 -05002554
2555 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2556 list_add(&evt->node, &phba->ct_ev_waiters);
2557 lpfc_bsg_event_ref(evt);
2558 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2559
2560 cmdiocbq = lpfc_sli_get_iocbq(phba);
2561 rspiocbq = lpfc_sli_get_iocbq(phba);
2562
2563 dmabuf = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2564 if (dmabuf) {
2565 dmabuf->virt = lpfc_mbuf_alloc(phba, 0, &dmabuf->phys);
James Smartc7495932010-04-06 15:05:28 -04002566 if (dmabuf->virt) {
2567 INIT_LIST_HEAD(&dmabuf->list);
2568 bpl = (struct ulp_bde64 *) dmabuf->virt;
2569 memset(bpl, 0, sizeof(*bpl));
2570 ctreq = (struct lpfc_sli_ct_request *)(bpl + 1);
2571 bpl->addrHigh =
2572 le32_to_cpu(putPaddrHigh(dmabuf->phys +
2573 sizeof(*bpl)));
2574 bpl->addrLow =
2575 le32_to_cpu(putPaddrLow(dmabuf->phys +
2576 sizeof(*bpl)));
2577 bpl->tus.f.bdeFlags = 0;
2578 bpl->tus.f.bdeSize = ELX_LOOPBACK_HEADER_SZ;
2579 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2580 }
James Smart3b5dd522010-01-26 23:10:15 -05002581 }
2582
2583 if (cmdiocbq == NULL || rspiocbq == NULL ||
James Smartc7495932010-04-06 15:05:28 -04002584 dmabuf == NULL || bpl == NULL || ctreq == NULL ||
2585 dmabuf->virt == NULL) {
James Smartd439d282010-09-29 11:18:45 -04002586 ret_val = -ENOMEM;
James Smart3b5dd522010-01-26 23:10:15 -05002587 goto err_get_xri_exit;
2588 }
2589
2590 cmd = &cmdiocbq->iocb;
2591 rsp = &rspiocbq->iocb;
2592
2593 memset(ctreq, 0, ELX_LOOPBACK_HEADER_SZ);
2594
2595 ctreq->RevisionId.bits.Revision = SLI_CT_REVISION;
2596 ctreq->RevisionId.bits.InId = 0;
2597 ctreq->FsType = SLI_CT_ELX_LOOPBACK;
2598 ctreq->FsSubType = 0;
2599 ctreq->CommandResponse.bits.CmdRsp = ELX_LOOPBACK_XRI_SETUP;
2600 ctreq->CommandResponse.bits.Size = 0;
2601
2602
2603 cmd->un.xseq64.bdl.addrHigh = putPaddrHigh(dmabuf->phys);
2604 cmd->un.xseq64.bdl.addrLow = putPaddrLow(dmabuf->phys);
2605 cmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
2606 cmd->un.xseq64.bdl.bdeSize = sizeof(*bpl);
2607
2608 cmd->un.xseq64.w5.hcsw.Fctl = LA;
2609 cmd->un.xseq64.w5.hcsw.Dfctl = 0;
2610 cmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
2611 cmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
2612
2613 cmd->ulpCommand = CMD_XMIT_SEQUENCE64_CR;
2614 cmd->ulpBdeCount = 1;
2615 cmd->ulpLe = 1;
2616 cmd->ulpClass = CLASS3;
2617 cmd->ulpContext = rpi;
2618
2619 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
2620 cmdiocbq->vport = phba->pport;
James Smart5a0916b2013-07-15 18:31:42 -04002621 cmdiocbq->iocb_cmpl = NULL;
James Smart3b5dd522010-01-26 23:10:15 -05002622
James Smartd439d282010-09-29 11:18:45 -04002623 iocb_stat = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq,
James Smart3b5dd522010-01-26 23:10:15 -05002624 rspiocbq,
2625 (phba->fc_ratov * 2)
2626 + LPFC_DRVR_TIMEOUT);
James Smart53151bb2013-10-10 12:24:07 -04002627 if ((iocb_stat != IOCB_SUCCESS) || (rsp->ulpStatus != IOSTAT_SUCCESS)) {
James Smartd439d282010-09-29 11:18:45 -04002628 ret_val = -EIO;
James Smart3b5dd522010-01-26 23:10:15 -05002629 goto err_get_xri_exit;
James Smartd439d282010-09-29 11:18:45 -04002630 }
James Smart3b5dd522010-01-26 23:10:15 -05002631 *txxri = rsp->ulpContext;
2632
2633 evt->waiting = 1;
2634 evt->wait_time_stamp = jiffies;
James Smartd439d282010-09-29 11:18:45 -04002635 time_left = wait_event_interruptible_timeout(
James Smart3b5dd522010-01-26 23:10:15 -05002636 evt->wq, !list_empty(&evt->events_to_see),
James Smart256ec0d2013-04-17 20:14:58 -04002637 msecs_to_jiffies(1000 *
2638 ((phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT)));
James Smart3b5dd522010-01-26 23:10:15 -05002639 if (list_empty(&evt->events_to_see))
James Smartd439d282010-09-29 11:18:45 -04002640 ret_val = (time_left) ? -EINTR : -ETIMEDOUT;
James Smart3b5dd522010-01-26 23:10:15 -05002641 else {
James Smart3b5dd522010-01-26 23:10:15 -05002642 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2643 list_move(evt->events_to_see.prev, &evt->events_to_get);
2644 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2645 *rxxri = (list_entry(evt->events_to_get.prev,
2646 typeof(struct event_data),
2647 node))->immed_dat;
2648 }
2649 evt->waiting = 0;
2650
2651err_get_xri_exit:
2652 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2653 lpfc_bsg_event_unref(evt); /* release ref */
2654 lpfc_bsg_event_unref(evt); /* delete */
2655 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2656
2657 if (dmabuf) {
2658 if (dmabuf->virt)
2659 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
2660 kfree(dmabuf);
2661 }
2662
James Smartd439d282010-09-29 11:18:45 -04002663 if (cmdiocbq && (iocb_stat != IOCB_TIMEDOUT))
James Smart3b5dd522010-01-26 23:10:15 -05002664 lpfc_sli_release_iocbq(phba, cmdiocbq);
2665 if (rspiocbq)
2666 lpfc_sli_release_iocbq(phba, rspiocbq);
2667 return ret_val;
2668}
2669
2670/**
James Smart7ad20aa2011-05-24 11:44:28 -04002671 * lpfc_bsg_dma_page_alloc - allocate a bsg mbox page sized dma buffers
2672 * @phba: Pointer to HBA context object
2673 *
2674 * This function allocates BSG_MBOX_SIZE (4KB) page size dma buffer and.
Joe Perches9e03aa22013-09-03 13:45:58 -07002675 * returns the pointer to the buffer.
James Smart7ad20aa2011-05-24 11:44:28 -04002676 **/
2677static struct lpfc_dmabuf *
2678lpfc_bsg_dma_page_alloc(struct lpfc_hba *phba)
2679{
2680 struct lpfc_dmabuf *dmabuf;
2681 struct pci_dev *pcidev = phba->pcidev;
2682
2683 /* allocate dma buffer struct */
2684 dmabuf = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2685 if (!dmabuf)
2686 return NULL;
2687
2688 INIT_LIST_HEAD(&dmabuf->list);
2689
2690 /* now, allocate dma buffer */
Joe Perches1aee3832014-09-03 12:56:12 -04002691 dmabuf->virt = dma_zalloc_coherent(&pcidev->dev, BSG_MBOX_SIZE,
2692 &(dmabuf->phys), GFP_KERNEL);
James Smart7ad20aa2011-05-24 11:44:28 -04002693
2694 if (!dmabuf->virt) {
2695 kfree(dmabuf);
2696 return NULL;
2697 }
James Smart7ad20aa2011-05-24 11:44:28 -04002698
2699 return dmabuf;
2700}
2701
2702/**
2703 * lpfc_bsg_dma_page_free - free a bsg mbox page sized dma buffer
2704 * @phba: Pointer to HBA context object.
2705 * @dmabuf: Pointer to the bsg mbox page sized dma buffer descriptor.
2706 *
2707 * This routine just simply frees a dma buffer and its associated buffer
2708 * descriptor referred by @dmabuf.
2709 **/
2710static void
2711lpfc_bsg_dma_page_free(struct lpfc_hba *phba, struct lpfc_dmabuf *dmabuf)
2712{
2713 struct pci_dev *pcidev = phba->pcidev;
2714
2715 if (!dmabuf)
2716 return;
2717
2718 if (dmabuf->virt)
2719 dma_free_coherent(&pcidev->dev, BSG_MBOX_SIZE,
2720 dmabuf->virt, dmabuf->phys);
2721 kfree(dmabuf);
2722 return;
2723}
2724
2725/**
2726 * lpfc_bsg_dma_page_list_free - free a list of bsg mbox page sized dma buffers
2727 * @phba: Pointer to HBA context object.
2728 * @dmabuf_list: Pointer to a list of bsg mbox page sized dma buffer descs.
2729 *
2730 * This routine just simply frees all dma buffers and their associated buffer
2731 * descriptors referred by @dmabuf_list.
2732 **/
2733static void
2734lpfc_bsg_dma_page_list_free(struct lpfc_hba *phba,
2735 struct list_head *dmabuf_list)
2736{
2737 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
2738
2739 if (list_empty(dmabuf_list))
2740 return;
2741
2742 list_for_each_entry_safe(dmabuf, next_dmabuf, dmabuf_list, list) {
2743 list_del_init(&dmabuf->list);
2744 lpfc_bsg_dma_page_free(phba, dmabuf);
2745 }
2746 return;
2747}
2748
2749/**
James Smart3b5dd522010-01-26 23:10:15 -05002750 * diag_cmd_data_alloc - fills in a bde struct with dma buffers
2751 * @phba: Pointer to HBA context object
2752 * @bpl: Pointer to 64 bit bde structure
2753 * @size: Number of bytes to process
2754 * @nocopydata: Flag to copy user data into the allocated buffer
2755 *
2756 * This function allocates page size buffers and populates an lpfc_dmabufext.
2757 * If allowed the user data pointed to with indataptr is copied into the kernel
2758 * memory. The chained list of page size buffers is returned.
2759 **/
2760static struct lpfc_dmabufext *
2761diag_cmd_data_alloc(struct lpfc_hba *phba,
2762 struct ulp_bde64 *bpl, uint32_t size,
2763 int nocopydata)
2764{
2765 struct lpfc_dmabufext *mlist = NULL;
2766 struct lpfc_dmabufext *dmp;
2767 int cnt, offset = 0, i = 0;
2768 struct pci_dev *pcidev;
2769
2770 pcidev = phba->pcidev;
2771
2772 while (size) {
2773 /* We get chunks of 4K */
2774 if (size > BUF_SZ_4K)
2775 cnt = BUF_SZ_4K;
2776 else
2777 cnt = size;
2778
2779 /* allocate struct lpfc_dmabufext buffer header */
2780 dmp = kmalloc(sizeof(struct lpfc_dmabufext), GFP_KERNEL);
2781 if (!dmp)
2782 goto out;
2783
2784 INIT_LIST_HEAD(&dmp->dma.list);
2785
2786 /* Queue it to a linked list */
2787 if (mlist)
2788 list_add_tail(&dmp->dma.list, &mlist->dma.list);
2789 else
2790 mlist = dmp;
2791
2792 /* allocate buffer */
2793 dmp->dma.virt = dma_alloc_coherent(&pcidev->dev,
2794 cnt,
2795 &(dmp->dma.phys),
2796 GFP_KERNEL);
2797
2798 if (!dmp->dma.virt)
2799 goto out;
2800
2801 dmp->size = cnt;
2802
2803 if (nocopydata) {
2804 bpl->tus.f.bdeFlags = 0;
2805 pci_dma_sync_single_for_device(phba->pcidev,
2806 dmp->dma.phys, LPFC_BPL_SIZE, PCI_DMA_TODEVICE);
2807
2808 } else {
2809 memset((uint8_t *)dmp->dma.virt, 0, cnt);
2810 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2811 }
2812
2813 /* build buffer ptr list for IOCB */
2814 bpl->addrLow = le32_to_cpu(putPaddrLow(dmp->dma.phys));
2815 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dmp->dma.phys));
2816 bpl->tus.f.bdeSize = (ushort) cnt;
2817 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2818 bpl++;
2819
2820 i++;
2821 offset += cnt;
2822 size -= cnt;
2823 }
2824
James Smarta2fc4aef2014-09-03 12:57:55 -04002825 if (mlist) {
2826 mlist->flag = i;
2827 return mlist;
2828 }
James Smart3b5dd522010-01-26 23:10:15 -05002829out:
2830 diag_cmd_data_free(phba, mlist);
2831 return NULL;
2832}
2833
2834/**
2835 * lpfcdiag_loop_post_rxbufs - post the receive buffers for an unsol CT cmd
2836 * @phba: Pointer to HBA context object
2837 * @rxxri: Receive exchange id
2838 * @len: Number of data bytes
2839 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002840 * This function allocates and posts a data buffer of sufficient size to receive
James Smart3b5dd522010-01-26 23:10:15 -05002841 * an unsolicted CT command.
2842 **/
2843static int lpfcdiag_loop_post_rxbufs(struct lpfc_hba *phba, uint16_t rxxri,
2844 size_t len)
2845{
2846 struct lpfc_sli *psli = &phba->sli;
2847 struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
2848 struct lpfc_iocbq *cmdiocbq;
2849 IOCB_t *cmd = NULL;
2850 struct list_head head, *curr, *next;
2851 struct lpfc_dmabuf *rxbmp;
2852 struct lpfc_dmabuf *dmp;
2853 struct lpfc_dmabuf *mp[2] = {NULL, NULL};
2854 struct ulp_bde64 *rxbpl = NULL;
2855 uint32_t num_bde;
2856 struct lpfc_dmabufext *rxbuffer = NULL;
2857 int ret_val = 0;
James Smartd439d282010-09-29 11:18:45 -04002858 int iocb_stat;
James Smart3b5dd522010-01-26 23:10:15 -05002859 int i = 0;
2860
2861 cmdiocbq = lpfc_sli_get_iocbq(phba);
2862 rxbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2863 if (rxbmp != NULL) {
2864 rxbmp->virt = lpfc_mbuf_alloc(phba, 0, &rxbmp->phys);
James Smartc7495932010-04-06 15:05:28 -04002865 if (rxbmp->virt) {
2866 INIT_LIST_HEAD(&rxbmp->list);
2867 rxbpl = (struct ulp_bde64 *) rxbmp->virt;
2868 rxbuffer = diag_cmd_data_alloc(phba, rxbpl, len, 0);
2869 }
James Smart3b5dd522010-01-26 23:10:15 -05002870 }
2871
2872 if (!cmdiocbq || !rxbmp || !rxbpl || !rxbuffer) {
James Smartd439d282010-09-29 11:18:45 -04002873 ret_val = -ENOMEM;
James Smart3b5dd522010-01-26 23:10:15 -05002874 goto err_post_rxbufs_exit;
2875 }
2876
2877 /* Queue buffers for the receive exchange */
2878 num_bde = (uint32_t)rxbuffer->flag;
2879 dmp = &rxbuffer->dma;
2880
2881 cmd = &cmdiocbq->iocb;
2882 i = 0;
2883
2884 INIT_LIST_HEAD(&head);
2885 list_add_tail(&head, &dmp->list);
2886 list_for_each_safe(curr, next, &head) {
2887 mp[i] = list_entry(curr, struct lpfc_dmabuf, list);
2888 list_del(curr);
2889
2890 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
2891 mp[i]->buffer_tag = lpfc_sli_get_buffer_tag(phba);
2892 cmd->un.quexri64cx.buff.bde.addrHigh =
2893 putPaddrHigh(mp[i]->phys);
2894 cmd->un.quexri64cx.buff.bde.addrLow =
2895 putPaddrLow(mp[i]->phys);
2896 cmd->un.quexri64cx.buff.bde.tus.f.bdeSize =
2897 ((struct lpfc_dmabufext *)mp[i])->size;
2898 cmd->un.quexri64cx.buff.buffer_tag = mp[i]->buffer_tag;
2899 cmd->ulpCommand = CMD_QUE_XRI64_CX;
2900 cmd->ulpPU = 0;
2901 cmd->ulpLe = 1;
2902 cmd->ulpBdeCount = 1;
2903 cmd->unsli3.que_xri64cx_ext_words.ebde_count = 0;
2904
2905 } else {
2906 cmd->un.cont64[i].addrHigh = putPaddrHigh(mp[i]->phys);
2907 cmd->un.cont64[i].addrLow = putPaddrLow(mp[i]->phys);
2908 cmd->un.cont64[i].tus.f.bdeSize =
2909 ((struct lpfc_dmabufext *)mp[i])->size;
2910 cmd->ulpBdeCount = ++i;
2911
2912 if ((--num_bde > 0) && (i < 2))
2913 continue;
2914
2915 cmd->ulpCommand = CMD_QUE_XRI_BUF64_CX;
2916 cmd->ulpLe = 1;
2917 }
2918
2919 cmd->ulpClass = CLASS3;
2920 cmd->ulpContext = rxxri;
2921
James Smartd439d282010-09-29 11:18:45 -04002922 iocb_stat = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq,
2923 0);
2924 if (iocb_stat == IOCB_ERROR) {
James Smart3b5dd522010-01-26 23:10:15 -05002925 diag_cmd_data_free(phba,
2926 (struct lpfc_dmabufext *)mp[0]);
2927 if (mp[1])
2928 diag_cmd_data_free(phba,
2929 (struct lpfc_dmabufext *)mp[1]);
2930 dmp = list_entry(next, struct lpfc_dmabuf, list);
James Smartd439d282010-09-29 11:18:45 -04002931 ret_val = -EIO;
James Smart3b5dd522010-01-26 23:10:15 -05002932 goto err_post_rxbufs_exit;
2933 }
2934
2935 lpfc_sli_ringpostbuf_put(phba, pring, mp[0]);
2936 if (mp[1]) {
2937 lpfc_sli_ringpostbuf_put(phba, pring, mp[1]);
2938 mp[1] = NULL;
2939 }
2940
2941 /* The iocb was freed by lpfc_sli_issue_iocb */
2942 cmdiocbq = lpfc_sli_get_iocbq(phba);
2943 if (!cmdiocbq) {
2944 dmp = list_entry(next, struct lpfc_dmabuf, list);
James Smartd439d282010-09-29 11:18:45 -04002945 ret_val = -EIO;
James Smart3b5dd522010-01-26 23:10:15 -05002946 goto err_post_rxbufs_exit;
2947 }
2948
2949 cmd = &cmdiocbq->iocb;
2950 i = 0;
2951 }
2952 list_del(&head);
2953
2954err_post_rxbufs_exit:
2955
2956 if (rxbmp) {
2957 if (rxbmp->virt)
2958 lpfc_mbuf_free(phba, rxbmp->virt, rxbmp->phys);
2959 kfree(rxbmp);
2960 }
2961
2962 if (cmdiocbq)
2963 lpfc_sli_release_iocbq(phba, cmdiocbq);
2964 return ret_val;
2965}
2966
2967/**
James Smart7ad20aa2011-05-24 11:44:28 -04002968 * lpfc_bsg_diag_loopback_run - run loopback on a port by issue ct cmd to itself
James Smart3b5dd522010-01-26 23:10:15 -05002969 * @job: LPFC_BSG_VENDOR_DIAG_TEST fc_bsg_job
2970 *
2971 * This function receives a user data buffer to be transmitted and received on
2972 * the same port, the link must be up and in loopback mode prior
2973 * to being called.
2974 * 1. A kernel buffer is allocated to copy the user data into.
2975 * 2. The port registers with "itself".
2976 * 3. The transmit and receive exchange ids are obtained.
2977 * 4. The receive exchange id is posted.
2978 * 5. A new els loopback event is created.
2979 * 6. The command and response iocbs are allocated.
2980 * 7. The cmd iocb FsType is set to elx loopback and the CmdRsp to looppback.
2981 *
2982 * This function is meant to be called n times while the port is in loopback
2983 * so it is the apps responsibility to issue a reset to take the port out
2984 * of loopback mode.
2985 **/
2986static int
James Smart7ad20aa2011-05-24 11:44:28 -04002987lpfc_bsg_diag_loopback_run(struct fc_bsg_job *job)
James Smart3b5dd522010-01-26 23:10:15 -05002988{
2989 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
2990 struct lpfc_hba *phba = vport->phba;
2991 struct diag_mode_test *diag_mode;
2992 struct lpfc_bsg_event *evt;
2993 struct event_data *evdat;
2994 struct lpfc_sli *psli = &phba->sli;
2995 uint32_t size;
2996 uint32_t full_size;
2997 size_t segment_len = 0, segment_offset = 0, current_offset = 0;
James Smart40426292010-12-15 17:58:10 -05002998 uint16_t rpi = 0;
James Smart1b511972011-12-13 13:23:09 -05002999 struct lpfc_iocbq *cmdiocbq, *rspiocbq = NULL;
3000 IOCB_t *cmd, *rsp = NULL;
James Smart3b5dd522010-01-26 23:10:15 -05003001 struct lpfc_sli_ct_request *ctreq;
3002 struct lpfc_dmabuf *txbmp;
3003 struct ulp_bde64 *txbpl = NULL;
3004 struct lpfc_dmabufext *txbuffer = NULL;
3005 struct list_head head;
3006 struct lpfc_dmabuf *curr;
James Smart1b511972011-12-13 13:23:09 -05003007 uint16_t txxri = 0, rxxri;
James Smart3b5dd522010-01-26 23:10:15 -05003008 uint32_t num_bde;
3009 uint8_t *ptr = NULL, *rx_databuf = NULL;
3010 int rc = 0;
James Smartd439d282010-09-29 11:18:45 -04003011 int time_left;
James Smart5a0916b2013-07-15 18:31:42 -04003012 int iocb_stat = IOCB_SUCCESS;
James Smart3b5dd522010-01-26 23:10:15 -05003013 unsigned long flags;
3014 void *dataout = NULL;
3015 uint32_t total_mem;
3016
3017 /* in case no data is returned return just the return code */
3018 job->reply->reply_payload_rcv_len = 0;
3019
3020 if (job->request_len <
3021 sizeof(struct fc_bsg_request) + sizeof(struct diag_mode_test)) {
3022 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3023 "2739 Received DIAG TEST request below minimum "
3024 "size\n");
3025 rc = -EINVAL;
3026 goto loopback_test_exit;
3027 }
3028
3029 if (job->request_payload.payload_len !=
3030 job->reply_payload.payload_len) {
3031 rc = -EINVAL;
3032 goto loopback_test_exit;
3033 }
James Smart3b5dd522010-01-26 23:10:15 -05003034 diag_mode = (struct diag_mode_test *)
3035 job->request->rqst_data.h_vendor.vendor_cmd;
3036
3037 if ((phba->link_state == LPFC_HBA_ERROR) ||
3038 (psli->sli_flag & LPFC_BLOCK_MGMT_IO) ||
3039 (!(psli->sli_flag & LPFC_SLI_ACTIVE))) {
3040 rc = -EACCES;
3041 goto loopback_test_exit;
3042 }
3043
3044 if (!lpfc_is_link_up(phba) || !(phba->link_flag & LS_LOOPBACK_MODE)) {
3045 rc = -EACCES;
3046 goto loopback_test_exit;
3047 }
3048
3049 size = job->request_payload.payload_len;
3050 full_size = size + ELX_LOOPBACK_HEADER_SZ; /* plus the header */
3051
3052 if ((size == 0) || (size > 80 * BUF_SZ_4K)) {
3053 rc = -ERANGE;
3054 goto loopback_test_exit;
3055 }
3056
James Smart63e801c2010-11-20 23:14:19 -05003057 if (full_size >= BUF_SZ_4K) {
James Smart3b5dd522010-01-26 23:10:15 -05003058 /*
3059 * Allocate memory for ioctl data. If buffer is bigger than 64k,
3060 * then we allocate 64k and re-use that buffer over and over to
3061 * xfer the whole block. This is because Linux kernel has a
3062 * problem allocating more than 120k of kernel space memory. Saw
3063 * problem with GET_FCPTARGETMAPPING...
3064 */
3065 if (size <= (64 * 1024))
James Smart63e801c2010-11-20 23:14:19 -05003066 total_mem = full_size;
James Smart3b5dd522010-01-26 23:10:15 -05003067 else
3068 total_mem = 64 * 1024;
3069 } else
3070 /* Allocate memory for ioctl data */
3071 total_mem = BUF_SZ_4K;
3072
3073 dataout = kmalloc(total_mem, GFP_KERNEL);
3074 if (dataout == NULL) {
3075 rc = -ENOMEM;
3076 goto loopback_test_exit;
3077 }
3078
3079 ptr = dataout;
3080 ptr += ELX_LOOPBACK_HEADER_SZ;
3081 sg_copy_to_buffer(job->request_payload.sg_list,
3082 job->request_payload.sg_cnt,
3083 ptr, size);
James Smart3b5dd522010-01-26 23:10:15 -05003084 rc = lpfcdiag_loop_self_reg(phba, &rpi);
James Smartd439d282010-09-29 11:18:45 -04003085 if (rc)
James Smart3b5dd522010-01-26 23:10:15 -05003086 goto loopback_test_exit;
James Smart3b5dd522010-01-26 23:10:15 -05003087
James Smart1b511972011-12-13 13:23:09 -05003088 if (phba->sli_rev < LPFC_SLI_REV4) {
3089 rc = lpfcdiag_loop_get_xri(phba, rpi, &txxri, &rxxri);
3090 if (rc) {
3091 lpfcdiag_loop_self_unreg(phba, rpi);
3092 goto loopback_test_exit;
3093 }
James Smart3b5dd522010-01-26 23:10:15 -05003094
James Smart1b511972011-12-13 13:23:09 -05003095 rc = lpfcdiag_loop_post_rxbufs(phba, rxxri, full_size);
3096 if (rc) {
3097 lpfcdiag_loop_self_unreg(phba, rpi);
3098 goto loopback_test_exit;
3099 }
James Smart3b5dd522010-01-26 23:10:15 -05003100 }
James Smart3b5dd522010-01-26 23:10:15 -05003101 evt = lpfc_bsg_event_new(FC_REG_CT_EVENT, current->pid,
3102 SLI_CT_ELX_LOOPBACK);
3103 if (!evt) {
3104 lpfcdiag_loop_self_unreg(phba, rpi);
3105 rc = -ENOMEM;
3106 goto loopback_test_exit;
3107 }
3108
3109 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3110 list_add(&evt->node, &phba->ct_ev_waiters);
3111 lpfc_bsg_event_ref(evt);
3112 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3113
3114 cmdiocbq = lpfc_sli_get_iocbq(phba);
James Smart1b511972011-12-13 13:23:09 -05003115 if (phba->sli_rev < LPFC_SLI_REV4)
3116 rspiocbq = lpfc_sli_get_iocbq(phba);
James Smart3b5dd522010-01-26 23:10:15 -05003117 txbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3118
3119 if (txbmp) {
3120 txbmp->virt = lpfc_mbuf_alloc(phba, 0, &txbmp->phys);
James Smartc7495932010-04-06 15:05:28 -04003121 if (txbmp->virt) {
3122 INIT_LIST_HEAD(&txbmp->list);
3123 txbpl = (struct ulp_bde64 *) txbmp->virt;
James Smart3b5dd522010-01-26 23:10:15 -05003124 txbuffer = diag_cmd_data_alloc(phba,
3125 txbpl, full_size, 0);
James Smartc7495932010-04-06 15:05:28 -04003126 }
James Smart3b5dd522010-01-26 23:10:15 -05003127 }
3128
James Smart1b511972011-12-13 13:23:09 -05003129 if (!cmdiocbq || !txbmp || !txbpl || !txbuffer || !txbmp->virt) {
3130 rc = -ENOMEM;
3131 goto err_loopback_test_exit;
3132 }
3133 if ((phba->sli_rev < LPFC_SLI_REV4) && !rspiocbq) {
James Smart3b5dd522010-01-26 23:10:15 -05003134 rc = -ENOMEM;
3135 goto err_loopback_test_exit;
3136 }
3137
3138 cmd = &cmdiocbq->iocb;
James Smart1b511972011-12-13 13:23:09 -05003139 if (phba->sli_rev < LPFC_SLI_REV4)
3140 rsp = &rspiocbq->iocb;
James Smart3b5dd522010-01-26 23:10:15 -05003141
3142 INIT_LIST_HEAD(&head);
3143 list_add_tail(&head, &txbuffer->dma.list);
3144 list_for_each_entry(curr, &head, list) {
3145 segment_len = ((struct lpfc_dmabufext *)curr)->size;
3146 if (current_offset == 0) {
3147 ctreq = curr->virt;
3148 memset(ctreq, 0, ELX_LOOPBACK_HEADER_SZ);
3149 ctreq->RevisionId.bits.Revision = SLI_CT_REVISION;
3150 ctreq->RevisionId.bits.InId = 0;
3151 ctreq->FsType = SLI_CT_ELX_LOOPBACK;
3152 ctreq->FsSubType = 0;
3153 ctreq->CommandResponse.bits.CmdRsp = ELX_LOOPBACK_DATA;
3154 ctreq->CommandResponse.bits.Size = size;
3155 segment_offset = ELX_LOOPBACK_HEADER_SZ;
3156 } else
3157 segment_offset = 0;
3158
3159 BUG_ON(segment_offset >= segment_len);
3160 memcpy(curr->virt + segment_offset,
3161 ptr + current_offset,
3162 segment_len - segment_offset);
3163
3164 current_offset += segment_len - segment_offset;
3165 BUG_ON(current_offset > size);
3166 }
3167 list_del(&head);
3168
3169 /* Build the XMIT_SEQUENCE iocb */
James Smart3b5dd522010-01-26 23:10:15 -05003170 num_bde = (uint32_t)txbuffer->flag;
3171
3172 cmd->un.xseq64.bdl.addrHigh = putPaddrHigh(txbmp->phys);
3173 cmd->un.xseq64.bdl.addrLow = putPaddrLow(txbmp->phys);
3174 cmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
3175 cmd->un.xseq64.bdl.bdeSize = (num_bde * sizeof(struct ulp_bde64));
3176
3177 cmd->un.xseq64.w5.hcsw.Fctl = (LS | LA);
3178 cmd->un.xseq64.w5.hcsw.Dfctl = 0;
3179 cmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
3180 cmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
3181
3182 cmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;
3183 cmd->ulpBdeCount = 1;
3184 cmd->ulpLe = 1;
3185 cmd->ulpClass = CLASS3;
James Smart3b5dd522010-01-26 23:10:15 -05003186
James Smart1b511972011-12-13 13:23:09 -05003187 if (phba->sli_rev < LPFC_SLI_REV4) {
3188 cmd->ulpContext = txxri;
3189 } else {
3190 cmd->un.xseq64.bdl.ulpIoTag32 = 0;
3191 cmd->un.ulpWord[3] = phba->sli4_hba.rpi_ids[rpi];
3192 cmdiocbq->context3 = txbmp;
3193 cmdiocbq->sli4_xritag = NO_XRI;
3194 cmd->unsli3.rcvsli3.ox_id = 0xffff;
3195 }
James Smart3b5dd522010-01-26 23:10:15 -05003196 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
3197 cmdiocbq->vport = phba->pport;
James Smart5a0916b2013-07-15 18:31:42 -04003198 cmdiocbq->iocb_cmpl = NULL;
James Smartd439d282010-09-29 11:18:45 -04003199 iocb_stat = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq,
3200 rspiocbq, (phba->fc_ratov * 2) +
3201 LPFC_DRVR_TIMEOUT);
James Smart3b5dd522010-01-26 23:10:15 -05003202
James Smart53151bb2013-10-10 12:24:07 -04003203 if ((iocb_stat != IOCB_SUCCESS) ||
3204 ((phba->sli_rev < LPFC_SLI_REV4) &&
3205 (rsp->ulpStatus != IOSTAT_SUCCESS))) {
James Smart1b511972011-12-13 13:23:09 -05003206 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3207 "3126 Failed loopback test issue iocb: "
3208 "iocb_stat:x%x\n", iocb_stat);
James Smart3b5dd522010-01-26 23:10:15 -05003209 rc = -EIO;
3210 goto err_loopback_test_exit;
3211 }
3212
3213 evt->waiting = 1;
James Smartd439d282010-09-29 11:18:45 -04003214 time_left = wait_event_interruptible_timeout(
James Smart3b5dd522010-01-26 23:10:15 -05003215 evt->wq, !list_empty(&evt->events_to_see),
James Smart256ec0d2013-04-17 20:14:58 -04003216 msecs_to_jiffies(1000 *
3217 ((phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT)));
James Smart3b5dd522010-01-26 23:10:15 -05003218 evt->waiting = 0;
James Smart1b511972011-12-13 13:23:09 -05003219 if (list_empty(&evt->events_to_see)) {
James Smartd439d282010-09-29 11:18:45 -04003220 rc = (time_left) ? -EINTR : -ETIMEDOUT;
James Smart1b511972011-12-13 13:23:09 -05003221 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3222 "3125 Not receiving unsolicited event, "
3223 "rc:x%x\n", rc);
3224 } else {
James Smart3b5dd522010-01-26 23:10:15 -05003225 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3226 list_move(evt->events_to_see.prev, &evt->events_to_get);
3227 evdat = list_entry(evt->events_to_get.prev,
3228 typeof(*evdat), node);
3229 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3230 rx_databuf = evdat->data;
3231 if (evdat->len != full_size) {
3232 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3233 "1603 Loopback test did not receive expected "
3234 "data length. actual length 0x%x expected "
3235 "length 0x%x\n",
3236 evdat->len, full_size);
3237 rc = -EIO;
3238 } else if (rx_databuf == NULL)
3239 rc = -EIO;
3240 else {
3241 rc = IOCB_SUCCESS;
3242 /* skip over elx loopback header */
3243 rx_databuf += ELX_LOOPBACK_HEADER_SZ;
3244 job->reply->reply_payload_rcv_len =
3245 sg_copy_from_buffer(job->reply_payload.sg_list,
3246 job->reply_payload.sg_cnt,
3247 rx_databuf, size);
3248 job->reply->reply_payload_rcv_len = size;
3249 }
3250 }
3251
3252err_loopback_test_exit:
3253 lpfcdiag_loop_self_unreg(phba, rpi);
3254
3255 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3256 lpfc_bsg_event_unref(evt); /* release ref */
3257 lpfc_bsg_event_unref(evt); /* delete */
3258 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3259
James Smart5a0916b2013-07-15 18:31:42 -04003260 if ((cmdiocbq != NULL) && (iocb_stat != IOCB_TIMEDOUT))
James Smart3b5dd522010-01-26 23:10:15 -05003261 lpfc_sli_release_iocbq(phba, cmdiocbq);
3262
3263 if (rspiocbq != NULL)
3264 lpfc_sli_release_iocbq(phba, rspiocbq);
3265
3266 if (txbmp != NULL) {
3267 if (txbpl != NULL) {
3268 if (txbuffer != NULL)
3269 diag_cmd_data_free(phba, txbuffer);
3270 lpfc_mbuf_free(phba, txbmp->virt, txbmp->phys);
3271 }
3272 kfree(txbmp);
3273 }
3274
3275loopback_test_exit:
3276 kfree(dataout);
3277 /* make error code available to userspace */
3278 job->reply->result = rc;
3279 job->dd_data = NULL;
3280 /* complete the job back to userspace if no error */
James Smart1b511972011-12-13 13:23:09 -05003281 if (rc == IOCB_SUCCESS)
James Smart3b5dd522010-01-26 23:10:15 -05003282 job->job_done(job);
3283 return rc;
3284}
3285
3286/**
3287 * lpfc_bsg_get_dfc_rev - process a GET_DFC_REV bsg vendor command
3288 * @job: GET_DFC_REV fc_bsg_job
3289 **/
3290static int
3291lpfc_bsg_get_dfc_rev(struct fc_bsg_job *job)
3292{
3293 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
3294 struct lpfc_hba *phba = vport->phba;
3295 struct get_mgmt_rev *event_req;
3296 struct get_mgmt_rev_reply *event_reply;
3297 int rc = 0;
3298
3299 if (job->request_len <
3300 sizeof(struct fc_bsg_request) + sizeof(struct get_mgmt_rev)) {
3301 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3302 "2740 Received GET_DFC_REV request below "
3303 "minimum size\n");
3304 rc = -EINVAL;
3305 goto job_error;
3306 }
3307
3308 event_req = (struct get_mgmt_rev *)
3309 job->request->rqst_data.h_vendor.vendor_cmd;
3310
3311 event_reply = (struct get_mgmt_rev_reply *)
3312 job->reply->reply_data.vendor_reply.vendor_rsp;
3313
3314 if (job->reply_len <
3315 sizeof(struct fc_bsg_request) + sizeof(struct get_mgmt_rev_reply)) {
3316 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3317 "2741 Received GET_DFC_REV reply below "
3318 "minimum size\n");
3319 rc = -EINVAL;
3320 goto job_error;
3321 }
3322
3323 event_reply->info.a_Major = MANAGEMENT_MAJOR_REV;
3324 event_reply->info.a_Minor = MANAGEMENT_MINOR_REV;
3325job_error:
3326 job->reply->result = rc;
3327 if (rc == 0)
3328 job->job_done(job);
3329 return rc;
3330}
3331
3332/**
James Smart7ad20aa2011-05-24 11:44:28 -04003333 * lpfc_bsg_issue_mbox_cmpl - lpfc_bsg_issue_mbox mbox completion handler
James Smart3b5dd522010-01-26 23:10:15 -05003334 * @phba: Pointer to HBA context object.
3335 * @pmboxq: Pointer to mailbox command.
3336 *
3337 * This is completion handler function for mailbox commands issued from
3338 * lpfc_bsg_issue_mbox function. This function is called by the
3339 * mailbox event handler function with no lock held. This function
3340 * will wake up thread waiting on the wait queue pointed by context1
3341 * of the mailbox.
3342 **/
Rashika Kheria9ab9b132014-09-03 12:55:46 -04003343static void
James Smart7ad20aa2011-05-24 11:44:28 -04003344lpfc_bsg_issue_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
James Smart3b5dd522010-01-26 23:10:15 -05003345{
3346 struct bsg_job_data *dd_data;
James Smart3b5dd522010-01-26 23:10:15 -05003347 struct fc_bsg_job *job;
3348 uint32_t size;
3349 unsigned long flags;
James Smart7ad20aa2011-05-24 11:44:28 -04003350 uint8_t *pmb, *pmb_buf;
James Smart3b5dd522010-01-26 23:10:15 -05003351
James Smart3b5dd522010-01-26 23:10:15 -05003352 dd_data = pmboxq->context1;
James Smart3b5dd522010-01-26 23:10:15 -05003353
James Smart7ad20aa2011-05-24 11:44:28 -04003354 /*
3355 * The outgoing buffer is readily referred from the dma buffer,
3356 * just need to get header part from mailboxq structure.
James Smart7a470272010-03-15 11:25:20 -04003357 */
James Smart7ad20aa2011-05-24 11:44:28 -04003358 pmb = (uint8_t *)&pmboxq->u.mb;
3359 pmb_buf = (uint8_t *)dd_data->context_un.mbox.mb;
3360 memcpy(pmb_buf, pmb, sizeof(MAILBOX_t));
James Smart515e0aa2010-09-29 11:19:00 -04003361
James Smarta33c4f72013-03-01 16:36:00 -05003362 /* Determine if job has been aborted */
3363
3364 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3365 job = dd_data->set_job;
3366 if (job) {
3367 /* Prevent timeout handling from trying to abort job */
3368 job->dd_data = NULL;
3369 }
3370 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3371
3372 /* Copy the mailbox data to the job if it is still active */
3373
James Smart5a6f1332011-03-11 16:05:35 -05003374 if (job) {
3375 size = job->reply_payload.payload_len;
3376 job->reply->reply_payload_rcv_len =
3377 sg_copy_from_buffer(job->reply_payload.sg_list,
James Smart7ad20aa2011-05-24 11:44:28 -04003378 job->reply_payload.sg_cnt,
3379 pmb_buf, size);
James Smart5a6f1332011-03-11 16:05:35 -05003380 }
James Smart7a470272010-03-15 11:25:20 -04003381
James Smarta33c4f72013-03-01 16:36:00 -05003382 dd_data->set_job = NULL;
James Smart3b5dd522010-01-26 23:10:15 -05003383 mempool_free(dd_data->context_un.mbox.pmboxq, phba->mbox_mem_pool);
James Smart7ad20aa2011-05-24 11:44:28 -04003384 lpfc_bsg_dma_page_free(phba, dd_data->context_un.mbox.dmabuffers);
James Smart3b5dd522010-01-26 23:10:15 -05003385 kfree(dd_data);
James Smart7ad20aa2011-05-24 11:44:28 -04003386
James Smarta33c4f72013-03-01 16:36:00 -05003387 /* Complete the job if the job is still active */
3388
James Smart7ad20aa2011-05-24 11:44:28 -04003389 if (job) {
3390 job->reply->result = 0;
3391 job->job_done(job);
3392 }
James Smart3b5dd522010-01-26 23:10:15 -05003393 return;
3394}
3395
3396/**
3397 * lpfc_bsg_check_cmd_access - test for a supported mailbox command
3398 * @phba: Pointer to HBA context object.
3399 * @mb: Pointer to a mailbox object.
3400 * @vport: Pointer to a vport object.
3401 *
3402 * Some commands require the port to be offline, some may not be called from
3403 * the application.
3404 **/
3405static int lpfc_bsg_check_cmd_access(struct lpfc_hba *phba,
3406 MAILBOX_t *mb, struct lpfc_vport *vport)
3407{
3408 /* return negative error values for bsg job */
3409 switch (mb->mbxCommand) {
3410 /* Offline only */
3411 case MBX_INIT_LINK:
3412 case MBX_DOWN_LINK:
3413 case MBX_CONFIG_LINK:
3414 case MBX_CONFIG_RING:
3415 case MBX_RESET_RING:
3416 case MBX_UNREG_LOGIN:
3417 case MBX_CLEAR_LA:
3418 case MBX_DUMP_CONTEXT:
3419 case MBX_RUN_DIAGS:
3420 case MBX_RESTART:
3421 case MBX_SET_MASK:
3422 if (!(vport->fc_flag & FC_OFFLINE_MODE)) {
3423 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3424 "2743 Command 0x%x is illegal in on-line "
3425 "state\n",
3426 mb->mbxCommand);
3427 return -EPERM;
3428 }
3429 case MBX_WRITE_NV:
3430 case MBX_WRITE_VPARMS:
3431 case MBX_LOAD_SM:
3432 case MBX_READ_NV:
3433 case MBX_READ_CONFIG:
3434 case MBX_READ_RCONFIG:
3435 case MBX_READ_STATUS:
3436 case MBX_READ_XRI:
3437 case MBX_READ_REV:
3438 case MBX_READ_LNK_STAT:
3439 case MBX_DUMP_MEMORY:
3440 case MBX_DOWN_LOAD:
3441 case MBX_UPDATE_CFG:
3442 case MBX_KILL_BOARD:
James Smart06f35552013-05-31 17:04:50 -04003443 case MBX_READ_TOPOLOGY:
James Smart3b5dd522010-01-26 23:10:15 -05003444 case MBX_LOAD_AREA:
3445 case MBX_LOAD_EXP_ROM:
3446 case MBX_BEACON:
3447 case MBX_DEL_LD_ENTRY:
3448 case MBX_SET_DEBUG:
3449 case MBX_WRITE_WWN:
3450 case MBX_SLI4_CONFIG:
James Smartc7495932010-04-06 15:05:28 -04003451 case MBX_READ_EVENT_LOG:
James Smart3b5dd522010-01-26 23:10:15 -05003452 case MBX_READ_EVENT_LOG_STATUS:
3453 case MBX_WRITE_EVENT_LOG:
3454 case MBX_PORT_CAPABILITIES:
3455 case MBX_PORT_IOV_CONTROL:
James Smart7a470272010-03-15 11:25:20 -04003456 case MBX_RUN_BIU_DIAG64:
James Smart3b5dd522010-01-26 23:10:15 -05003457 break;
3458 case MBX_SET_VARIABLE:
James Smarte2aed292010-02-26 14:15:00 -05003459 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3460 "1226 mbox: set_variable 0x%x, 0x%x\n",
3461 mb->un.varWords[0],
3462 mb->un.varWords[1]);
3463 if ((mb->un.varWords[0] == SETVAR_MLOMNT)
3464 && (mb->un.varWords[1] == 1)) {
3465 phba->wait_4_mlo_maint_flg = 1;
3466 } else if (mb->un.varWords[0] == SETVAR_MLORST) {
James Smart1b511972011-12-13 13:23:09 -05003467 spin_lock_irq(&phba->hbalock);
James Smarte2aed292010-02-26 14:15:00 -05003468 phba->link_flag &= ~LS_LOOPBACK_MODE;
James Smart1b511972011-12-13 13:23:09 -05003469 spin_unlock_irq(&phba->hbalock);
James Smart76a95d72010-11-20 23:11:48 -05003470 phba->fc_topology = LPFC_TOPOLOGY_PT_PT;
James Smarte2aed292010-02-26 14:15:00 -05003471 }
3472 break;
James Smart3b5dd522010-01-26 23:10:15 -05003473 case MBX_READ_SPARM64:
James Smart3b5dd522010-01-26 23:10:15 -05003474 case MBX_REG_LOGIN:
3475 case MBX_REG_LOGIN64:
3476 case MBX_CONFIG_PORT:
3477 case MBX_RUN_BIU_DIAG:
3478 default:
3479 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3480 "2742 Unknown Command 0x%x\n",
3481 mb->mbxCommand);
3482 return -EPERM;
3483 }
3484
3485 return 0; /* ok */
3486}
3487
3488/**
James Smart7ad20aa2011-05-24 11:44:28 -04003489 * lpfc_bsg_mbox_ext_cleanup - clean up context of multi-buffer mbox session
3490 * @phba: Pointer to HBA context object.
3491 *
3492 * This is routine clean up and reset BSG handling of multi-buffer mbox
3493 * command session.
3494 **/
3495static void
3496lpfc_bsg_mbox_ext_session_reset(struct lpfc_hba *phba)
3497{
3498 if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_IDLE)
3499 return;
3500
3501 /* free all memory, including dma buffers */
3502 lpfc_bsg_dma_page_list_free(phba,
3503 &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
3504 lpfc_bsg_dma_page_free(phba, phba->mbox_ext_buf_ctx.mbx_dmabuf);
3505 /* multi-buffer write mailbox command pass-through complete */
3506 memset((char *)&phba->mbox_ext_buf_ctx, 0,
3507 sizeof(struct lpfc_mbox_ext_buf_ctx));
3508 INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
3509
3510 return;
3511}
3512
3513/**
3514 * lpfc_bsg_issue_mbox_ext_handle_job - job handler for multi-buffer mbox cmpl
3515 * @phba: Pointer to HBA context object.
3516 * @pmboxq: Pointer to mailbox command.
3517 *
3518 * This is routine handles BSG job for mailbox commands completions with
3519 * multiple external buffers.
3520 **/
3521static struct fc_bsg_job *
3522lpfc_bsg_issue_mbox_ext_handle_job(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
3523{
3524 struct bsg_job_data *dd_data;
3525 struct fc_bsg_job *job;
3526 uint8_t *pmb, *pmb_buf;
3527 unsigned long flags;
3528 uint32_t size;
3529 int rc = 0;
James Smart026abb82011-12-13 13:20:45 -05003530 struct lpfc_dmabuf *dmabuf;
3531 struct lpfc_sli_config_mbox *sli_cfg_mbx;
3532 uint8_t *pmbx;
James Smart7ad20aa2011-05-24 11:44:28 -04003533
James Smart7ad20aa2011-05-24 11:44:28 -04003534 dd_data = pmboxq->context1;
James Smarta33c4f72013-03-01 16:36:00 -05003535
3536 /* Determine if job has been aborted */
3537 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3538 job = dd_data->set_job;
3539 if (job) {
3540 /* Prevent timeout handling from trying to abort job */
3541 job->dd_data = NULL;
James Smart7ad20aa2011-05-24 11:44:28 -04003542 }
James Smarta33c4f72013-03-01 16:36:00 -05003543 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smart7ad20aa2011-05-24 11:44:28 -04003544
3545 /*
3546 * The outgoing buffer is readily referred from the dma buffer,
3547 * just need to get header part from mailboxq structure.
3548 */
James Smarta33c4f72013-03-01 16:36:00 -05003549
James Smart7ad20aa2011-05-24 11:44:28 -04003550 pmb = (uint8_t *)&pmboxq->u.mb;
3551 pmb_buf = (uint8_t *)dd_data->context_un.mbox.mb;
James Smart026abb82011-12-13 13:20:45 -05003552 /* Copy the byte swapped response mailbox back to the user */
James Smart7ad20aa2011-05-24 11:44:28 -04003553 memcpy(pmb_buf, pmb, sizeof(MAILBOX_t));
James Smart026abb82011-12-13 13:20:45 -05003554 /* if there is any non-embedded extended data copy that too */
3555 dmabuf = phba->mbox_ext_buf_ctx.mbx_dmabuf;
3556 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
3557 if (!bsg_bf_get(lpfc_mbox_hdr_emb,
3558 &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) {
3559 pmbx = (uint8_t *)dmabuf->virt;
3560 /* byte swap the extended data following the mailbox command */
3561 lpfc_sli_pcimem_bcopy(&pmbx[sizeof(MAILBOX_t)],
3562 &pmbx[sizeof(MAILBOX_t)],
3563 sli_cfg_mbx->un.sli_config_emb0_subsys.mse[0].buf_len);
3564 }
James Smart7ad20aa2011-05-24 11:44:28 -04003565
James Smarta33c4f72013-03-01 16:36:00 -05003566 /* Complete the job if the job is still active */
3567
James Smart7ad20aa2011-05-24 11:44:28 -04003568 if (job) {
3569 size = job->reply_payload.payload_len;
3570 job->reply->reply_payload_rcv_len =
3571 sg_copy_from_buffer(job->reply_payload.sg_list,
3572 job->reply_payload.sg_cnt,
3573 pmb_buf, size);
James Smarta33c4f72013-03-01 16:36:00 -05003574
James Smart7ad20aa2011-05-24 11:44:28 -04003575 /* result for successful */
3576 job->reply->result = 0;
James Smarta33c4f72013-03-01 16:36:00 -05003577
James Smart7ad20aa2011-05-24 11:44:28 -04003578 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3579 "2937 SLI_CONFIG ext-buffer maibox command "
3580 "(x%x/x%x) complete bsg job done, bsize:%d\n",
3581 phba->mbox_ext_buf_ctx.nembType,
3582 phba->mbox_ext_buf_ctx.mboxType, size);
James Smartb76f2dc2011-07-22 18:37:42 -04003583 lpfc_idiag_mbxacc_dump_bsg_mbox(phba,
3584 phba->mbox_ext_buf_ctx.nembType,
3585 phba->mbox_ext_buf_ctx.mboxType,
3586 dma_ebuf, sta_pos_addr,
3587 phba->mbox_ext_buf_ctx.mbx_dmabuf, 0);
James Smarta33c4f72013-03-01 16:36:00 -05003588 } else {
James Smart7ad20aa2011-05-24 11:44:28 -04003589 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3590 "2938 SLI_CONFIG ext-buffer maibox "
3591 "command (x%x/x%x) failure, rc:x%x\n",
3592 phba->mbox_ext_buf_ctx.nembType,
3593 phba->mbox_ext_buf_ctx.mboxType, rc);
James Smarta33c4f72013-03-01 16:36:00 -05003594 }
3595
3596
James Smart7ad20aa2011-05-24 11:44:28 -04003597 /* state change */
3598 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_DONE;
3599 kfree(dd_data);
James Smart7ad20aa2011-05-24 11:44:28 -04003600 return job;
3601}
3602
3603/**
3604 * lpfc_bsg_issue_read_mbox_ext_cmpl - compl handler for multi-buffer read mbox
3605 * @phba: Pointer to HBA context object.
3606 * @pmboxq: Pointer to mailbox command.
3607 *
3608 * This is completion handler function for mailbox read commands with multiple
3609 * external buffers.
3610 **/
3611static void
3612lpfc_bsg_issue_read_mbox_ext_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
3613{
3614 struct fc_bsg_job *job;
3615
James Smarta33c4f72013-03-01 16:36:00 -05003616 job = lpfc_bsg_issue_mbox_ext_handle_job(phba, pmboxq);
3617
James Smart7ad20aa2011-05-24 11:44:28 -04003618 /* handle the BSG job with mailbox command */
James Smarta33c4f72013-03-01 16:36:00 -05003619 if (!job)
James Smart7ad20aa2011-05-24 11:44:28 -04003620 pmboxq->u.mb.mbxStatus = MBXERR_ERROR;
3621
3622 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3623 "2939 SLI_CONFIG ext-buffer rd maibox command "
3624 "complete, ctxState:x%x, mbxStatus:x%x\n",
3625 phba->mbox_ext_buf_ctx.state, pmboxq->u.mb.mbxStatus);
3626
James Smart7ad20aa2011-05-24 11:44:28 -04003627 if (pmboxq->u.mb.mbxStatus || phba->mbox_ext_buf_ctx.numBuf == 1)
3628 lpfc_bsg_mbox_ext_session_reset(phba);
3629
3630 /* free base driver mailbox structure memory */
3631 mempool_free(pmboxq, phba->mbox_mem_pool);
3632
James Smarta33c4f72013-03-01 16:36:00 -05003633 /* if the job is still active, call job done */
James Smart7ad20aa2011-05-24 11:44:28 -04003634 if (job)
3635 job->job_done(job);
3636
3637 return;
3638}
3639
3640/**
3641 * lpfc_bsg_issue_write_mbox_ext_cmpl - cmpl handler for multi-buffer write mbox
3642 * @phba: Pointer to HBA context object.
3643 * @pmboxq: Pointer to mailbox command.
3644 *
3645 * This is completion handler function for mailbox write commands with multiple
3646 * external buffers.
3647 **/
3648static void
3649lpfc_bsg_issue_write_mbox_ext_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
3650{
3651 struct fc_bsg_job *job;
3652
James Smarta33c4f72013-03-01 16:36:00 -05003653 job = lpfc_bsg_issue_mbox_ext_handle_job(phba, pmboxq);
3654
James Smart7ad20aa2011-05-24 11:44:28 -04003655 /* handle the BSG job with the mailbox command */
James Smarta33c4f72013-03-01 16:36:00 -05003656 if (!job)
James Smart7ad20aa2011-05-24 11:44:28 -04003657 pmboxq->u.mb.mbxStatus = MBXERR_ERROR;
3658
3659 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3660 "2940 SLI_CONFIG ext-buffer wr maibox command "
3661 "complete, ctxState:x%x, mbxStatus:x%x\n",
3662 phba->mbox_ext_buf_ctx.state, pmboxq->u.mb.mbxStatus);
3663
James Smart7ad20aa2011-05-24 11:44:28 -04003664 /* free all memory, including dma buffers */
3665 mempool_free(pmboxq, phba->mbox_mem_pool);
3666 lpfc_bsg_mbox_ext_session_reset(phba);
3667
James Smarta33c4f72013-03-01 16:36:00 -05003668 /* if the job is still active, call job done */
James Smart7ad20aa2011-05-24 11:44:28 -04003669 if (job)
3670 job->job_done(job);
3671
3672 return;
3673}
3674
3675static void
3676lpfc_bsg_sli_cfg_dma_desc_setup(struct lpfc_hba *phba, enum nemb_type nemb_tp,
3677 uint32_t index, struct lpfc_dmabuf *mbx_dmabuf,
3678 struct lpfc_dmabuf *ext_dmabuf)
3679{
3680 struct lpfc_sli_config_mbox *sli_cfg_mbx;
3681
3682 /* pointer to the start of mailbox command */
3683 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)mbx_dmabuf->virt;
3684
3685 if (nemb_tp == nemb_mse) {
3686 if (index == 0) {
3687 sli_cfg_mbx->un.sli_config_emb0_subsys.
3688 mse[index].pa_hi =
3689 putPaddrHigh(mbx_dmabuf->phys +
3690 sizeof(MAILBOX_t));
3691 sli_cfg_mbx->un.sli_config_emb0_subsys.
3692 mse[index].pa_lo =
3693 putPaddrLow(mbx_dmabuf->phys +
3694 sizeof(MAILBOX_t));
3695 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3696 "2943 SLI_CONFIG(mse)[%d], "
3697 "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3698 index,
3699 sli_cfg_mbx->un.sli_config_emb0_subsys.
3700 mse[index].buf_len,
3701 sli_cfg_mbx->un.sli_config_emb0_subsys.
3702 mse[index].pa_hi,
3703 sli_cfg_mbx->un.sli_config_emb0_subsys.
3704 mse[index].pa_lo);
3705 } else {
3706 sli_cfg_mbx->un.sli_config_emb0_subsys.
3707 mse[index].pa_hi =
3708 putPaddrHigh(ext_dmabuf->phys);
3709 sli_cfg_mbx->un.sli_config_emb0_subsys.
3710 mse[index].pa_lo =
3711 putPaddrLow(ext_dmabuf->phys);
3712 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3713 "2944 SLI_CONFIG(mse)[%d], "
3714 "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3715 index,
3716 sli_cfg_mbx->un.sli_config_emb0_subsys.
3717 mse[index].buf_len,
3718 sli_cfg_mbx->un.sli_config_emb0_subsys.
3719 mse[index].pa_hi,
3720 sli_cfg_mbx->un.sli_config_emb0_subsys.
3721 mse[index].pa_lo);
3722 }
3723 } else {
3724 if (index == 0) {
3725 sli_cfg_mbx->un.sli_config_emb1_subsys.
3726 hbd[index].pa_hi =
3727 putPaddrHigh(mbx_dmabuf->phys +
3728 sizeof(MAILBOX_t));
3729 sli_cfg_mbx->un.sli_config_emb1_subsys.
3730 hbd[index].pa_lo =
3731 putPaddrLow(mbx_dmabuf->phys +
3732 sizeof(MAILBOX_t));
3733 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3734 "3007 SLI_CONFIG(hbd)[%d], "
3735 "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3736 index,
3737 bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
3738 &sli_cfg_mbx->un.
3739 sli_config_emb1_subsys.hbd[index]),
3740 sli_cfg_mbx->un.sli_config_emb1_subsys.
3741 hbd[index].pa_hi,
3742 sli_cfg_mbx->un.sli_config_emb1_subsys.
3743 hbd[index].pa_lo);
3744
3745 } else {
3746 sli_cfg_mbx->un.sli_config_emb1_subsys.
3747 hbd[index].pa_hi =
3748 putPaddrHigh(ext_dmabuf->phys);
3749 sli_cfg_mbx->un.sli_config_emb1_subsys.
3750 hbd[index].pa_lo =
3751 putPaddrLow(ext_dmabuf->phys);
3752 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3753 "3008 SLI_CONFIG(hbd)[%d], "
3754 "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3755 index,
3756 bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
3757 &sli_cfg_mbx->un.
3758 sli_config_emb1_subsys.hbd[index]),
3759 sli_cfg_mbx->un.sli_config_emb1_subsys.
3760 hbd[index].pa_hi,
3761 sli_cfg_mbx->un.sli_config_emb1_subsys.
3762 hbd[index].pa_lo);
3763 }
3764 }
3765 return;
3766}
3767
3768/**
3769 * lpfc_bsg_sli_cfg_mse_read_cmd_ext - sli_config non-embedded mailbox cmd read
3770 * @phba: Pointer to HBA context object.
3771 * @mb: Pointer to a BSG mailbox object.
3772 * @nemb_tp: Enumerate of non-embedded mailbox command type.
3773 * @dmabuff: Pointer to a DMA buffer descriptor.
3774 *
3775 * This routine performs SLI_CONFIG (0x9B) read mailbox command operation with
3776 * non-embedded external bufffers.
3777 **/
3778static int
3779lpfc_bsg_sli_cfg_read_cmd_ext(struct lpfc_hba *phba, struct fc_bsg_job *job,
3780 enum nemb_type nemb_tp,
3781 struct lpfc_dmabuf *dmabuf)
3782{
3783 struct lpfc_sli_config_mbox *sli_cfg_mbx;
3784 struct dfc_mbox_req *mbox_req;
3785 struct lpfc_dmabuf *curr_dmabuf, *next_dmabuf;
3786 uint32_t ext_buf_cnt, ext_buf_index;
3787 struct lpfc_dmabuf *ext_dmabuf = NULL;
3788 struct bsg_job_data *dd_data = NULL;
3789 LPFC_MBOXQ_t *pmboxq = NULL;
3790 MAILBOX_t *pmb;
3791 uint8_t *pmbx;
3792 int rc, i;
3793
3794 mbox_req =
3795 (struct dfc_mbox_req *)job->request->rqst_data.h_vendor.vendor_cmd;
3796
3797 /* pointer to the start of mailbox command */
3798 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
3799
3800 if (nemb_tp == nemb_mse) {
3801 ext_buf_cnt = bsg_bf_get(lpfc_mbox_hdr_mse_cnt,
3802 &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr);
3803 if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_MSE) {
3804 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3805 "2945 Handled SLI_CONFIG(mse) rd, "
3806 "ext_buf_cnt(%d) out of range(%d)\n",
3807 ext_buf_cnt,
3808 LPFC_MBX_SLI_CONFIG_MAX_MSE);
3809 rc = -ERANGE;
3810 goto job_error;
3811 }
3812 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3813 "2941 Handled SLI_CONFIG(mse) rd, "
3814 "ext_buf_cnt:%d\n", ext_buf_cnt);
3815 } else {
3816 /* sanity check on interface type for support */
3817 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
3818 LPFC_SLI_INTF_IF_TYPE_2) {
3819 rc = -ENODEV;
3820 goto job_error;
3821 }
3822 /* nemb_tp == nemb_hbd */
3823 ext_buf_cnt = sli_cfg_mbx->un.sli_config_emb1_subsys.hbd_count;
3824 if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_HBD) {
3825 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3826 "2946 Handled SLI_CONFIG(hbd) rd, "
3827 "ext_buf_cnt(%d) out of range(%d)\n",
3828 ext_buf_cnt,
3829 LPFC_MBX_SLI_CONFIG_MAX_HBD);
3830 rc = -ERANGE;
3831 goto job_error;
3832 }
3833 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3834 "2942 Handled SLI_CONFIG(hbd) rd, "
3835 "ext_buf_cnt:%d\n", ext_buf_cnt);
3836 }
3837
James Smartb76f2dc2011-07-22 18:37:42 -04003838 /* before dma descriptor setup */
3839 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_rd, dma_mbox,
3840 sta_pre_addr, dmabuf, ext_buf_cnt);
3841
James Smart7ad20aa2011-05-24 11:44:28 -04003842 /* reject non-embedded mailbox command with none external buffer */
3843 if (ext_buf_cnt == 0) {
3844 rc = -EPERM;
3845 goto job_error;
3846 } else if (ext_buf_cnt > 1) {
3847 /* additional external read buffers */
3848 for (i = 1; i < ext_buf_cnt; i++) {
3849 ext_dmabuf = lpfc_bsg_dma_page_alloc(phba);
3850 if (!ext_dmabuf) {
3851 rc = -ENOMEM;
3852 goto job_error;
3853 }
3854 list_add_tail(&ext_dmabuf->list,
3855 &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
3856 }
3857 }
3858
3859 /* bsg tracking structure */
3860 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
3861 if (!dd_data) {
3862 rc = -ENOMEM;
3863 goto job_error;
3864 }
3865
3866 /* mailbox command structure for base driver */
3867 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3868 if (!pmboxq) {
3869 rc = -ENOMEM;
3870 goto job_error;
3871 }
3872 memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
3873
3874 /* for the first external buffer */
3875 lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, 0, dmabuf, dmabuf);
3876
3877 /* for the rest of external buffer descriptors if any */
3878 if (ext_buf_cnt > 1) {
3879 ext_buf_index = 1;
3880 list_for_each_entry_safe(curr_dmabuf, next_dmabuf,
3881 &phba->mbox_ext_buf_ctx.ext_dmabuf_list, list) {
3882 lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp,
3883 ext_buf_index, dmabuf,
3884 curr_dmabuf);
3885 ext_buf_index++;
3886 }
3887 }
3888
James Smartb76f2dc2011-07-22 18:37:42 -04003889 /* after dma descriptor setup */
3890 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_rd, dma_mbox,
3891 sta_pos_addr, dmabuf, ext_buf_cnt);
3892
James Smart7ad20aa2011-05-24 11:44:28 -04003893 /* construct base driver mbox command */
3894 pmb = &pmboxq->u.mb;
3895 pmbx = (uint8_t *)dmabuf->virt;
3896 memcpy(pmb, pmbx, sizeof(*pmb));
3897 pmb->mbxOwner = OWN_HOST;
3898 pmboxq->vport = phba->pport;
3899
3900 /* multi-buffer handling context */
3901 phba->mbox_ext_buf_ctx.nembType = nemb_tp;
3902 phba->mbox_ext_buf_ctx.mboxType = mbox_rd;
3903 phba->mbox_ext_buf_ctx.numBuf = ext_buf_cnt;
3904 phba->mbox_ext_buf_ctx.mbxTag = mbox_req->extMboxTag;
3905 phba->mbox_ext_buf_ctx.seqNum = mbox_req->extSeqNum;
3906 phba->mbox_ext_buf_ctx.mbx_dmabuf = dmabuf;
3907
3908 /* callback for multi-buffer read mailbox command */
3909 pmboxq->mbox_cmpl = lpfc_bsg_issue_read_mbox_ext_cmpl;
3910
3911 /* context fields to callback function */
3912 pmboxq->context1 = dd_data;
3913 dd_data->type = TYPE_MBOX;
James Smarta33c4f72013-03-01 16:36:00 -05003914 dd_data->set_job = job;
James Smart7ad20aa2011-05-24 11:44:28 -04003915 dd_data->context_un.mbox.pmboxq = pmboxq;
3916 dd_data->context_un.mbox.mb = (MAILBOX_t *)pmbx;
James Smart7ad20aa2011-05-24 11:44:28 -04003917 job->dd_data = dd_data;
3918
3919 /* state change */
3920 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
3921
James Smart026abb82011-12-13 13:20:45 -05003922 /*
3923 * Non-embedded mailbox subcommand data gets byte swapped here because
3924 * the lower level driver code only does the first 64 mailbox words.
3925 */
3926 if ((!bsg_bf_get(lpfc_mbox_hdr_emb,
3927 &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) &&
3928 (nemb_tp == nemb_mse))
3929 lpfc_sli_pcimem_bcopy(&pmbx[sizeof(MAILBOX_t)],
3930 &pmbx[sizeof(MAILBOX_t)],
3931 sli_cfg_mbx->un.sli_config_emb0_subsys.
3932 mse[0].buf_len);
3933
James Smart7ad20aa2011-05-24 11:44:28 -04003934 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
3935 if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
3936 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3937 "2947 Issued SLI_CONFIG ext-buffer "
3938 "maibox command, rc:x%x\n", rc);
James Smart88a2cfb2011-07-22 18:36:33 -04003939 return SLI_CONFIG_HANDLED;
James Smart7ad20aa2011-05-24 11:44:28 -04003940 }
3941 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3942 "2948 Failed to issue SLI_CONFIG ext-buffer "
3943 "maibox command, rc:x%x\n", rc);
3944 rc = -EPIPE;
3945
3946job_error:
3947 if (pmboxq)
3948 mempool_free(pmboxq, phba->mbox_mem_pool);
3949 lpfc_bsg_dma_page_list_free(phba,
3950 &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
3951 kfree(dd_data);
3952 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_IDLE;
3953 return rc;
3954}
3955
3956/**
3957 * lpfc_bsg_sli_cfg_write_cmd_ext - sli_config non-embedded mailbox cmd write
3958 * @phba: Pointer to HBA context object.
3959 * @mb: Pointer to a BSG mailbox object.
3960 * @dmabuff: Pointer to a DMA buffer descriptor.
3961 *
3962 * This routine performs SLI_CONFIG (0x9B) write mailbox command operation with
3963 * non-embedded external bufffers.
3964 **/
3965static int
3966lpfc_bsg_sli_cfg_write_cmd_ext(struct lpfc_hba *phba, struct fc_bsg_job *job,
3967 enum nemb_type nemb_tp,
3968 struct lpfc_dmabuf *dmabuf)
3969{
3970 struct dfc_mbox_req *mbox_req;
3971 struct lpfc_sli_config_mbox *sli_cfg_mbx;
3972 uint32_t ext_buf_cnt;
3973 struct bsg_job_data *dd_data = NULL;
3974 LPFC_MBOXQ_t *pmboxq = NULL;
3975 MAILBOX_t *pmb;
3976 uint8_t *mbx;
James Smart88a2cfb2011-07-22 18:36:33 -04003977 int rc = SLI_CONFIG_NOT_HANDLED, i;
James Smart7ad20aa2011-05-24 11:44:28 -04003978
3979 mbox_req =
3980 (struct dfc_mbox_req *)job->request->rqst_data.h_vendor.vendor_cmd;
3981
3982 /* pointer to the start of mailbox command */
3983 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
3984
3985 if (nemb_tp == nemb_mse) {
3986 ext_buf_cnt = bsg_bf_get(lpfc_mbox_hdr_mse_cnt,
3987 &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr);
3988 if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_MSE) {
3989 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
James Smart026abb82011-12-13 13:20:45 -05003990 "2953 Failed SLI_CONFIG(mse) wr, "
James Smart7ad20aa2011-05-24 11:44:28 -04003991 "ext_buf_cnt(%d) out of range(%d)\n",
3992 ext_buf_cnt,
3993 LPFC_MBX_SLI_CONFIG_MAX_MSE);
3994 return -ERANGE;
3995 }
3996 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3997 "2949 Handled SLI_CONFIG(mse) wr, "
3998 "ext_buf_cnt:%d\n", ext_buf_cnt);
3999 } else {
4000 /* sanity check on interface type for support */
4001 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
4002 LPFC_SLI_INTF_IF_TYPE_2)
4003 return -ENODEV;
4004 /* nemb_tp == nemb_hbd */
4005 ext_buf_cnt = sli_cfg_mbx->un.sli_config_emb1_subsys.hbd_count;
4006 if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_HBD) {
4007 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
James Smart026abb82011-12-13 13:20:45 -05004008 "2954 Failed SLI_CONFIG(hbd) wr, "
James Smart7ad20aa2011-05-24 11:44:28 -04004009 "ext_buf_cnt(%d) out of range(%d)\n",
4010 ext_buf_cnt,
4011 LPFC_MBX_SLI_CONFIG_MAX_HBD);
4012 return -ERANGE;
4013 }
4014 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4015 "2950 Handled SLI_CONFIG(hbd) wr, "
4016 "ext_buf_cnt:%d\n", ext_buf_cnt);
4017 }
4018
James Smartb76f2dc2011-07-22 18:37:42 -04004019 /* before dma buffer descriptor setup */
4020 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_wr, dma_mbox,
4021 sta_pre_addr, dmabuf, ext_buf_cnt);
4022
James Smart7ad20aa2011-05-24 11:44:28 -04004023 if (ext_buf_cnt == 0)
4024 return -EPERM;
4025
4026 /* for the first external buffer */
4027 lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, 0, dmabuf, dmabuf);
4028
James Smartb76f2dc2011-07-22 18:37:42 -04004029 /* after dma descriptor setup */
4030 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_wr, dma_mbox,
4031 sta_pos_addr, dmabuf, ext_buf_cnt);
4032
James Smart7ad20aa2011-05-24 11:44:28 -04004033 /* log for looking forward */
4034 for (i = 1; i < ext_buf_cnt; i++) {
4035 if (nemb_tp == nemb_mse)
4036 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4037 "2951 SLI_CONFIG(mse), buf[%d]-length:%d\n",
4038 i, sli_cfg_mbx->un.sli_config_emb0_subsys.
4039 mse[i].buf_len);
4040 else
4041 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4042 "2952 SLI_CONFIG(hbd), buf[%d]-length:%d\n",
4043 i, bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
4044 &sli_cfg_mbx->un.sli_config_emb1_subsys.
4045 hbd[i]));
4046 }
4047
4048 /* multi-buffer handling context */
4049 phba->mbox_ext_buf_ctx.nembType = nemb_tp;
4050 phba->mbox_ext_buf_ctx.mboxType = mbox_wr;
4051 phba->mbox_ext_buf_ctx.numBuf = ext_buf_cnt;
4052 phba->mbox_ext_buf_ctx.mbxTag = mbox_req->extMboxTag;
4053 phba->mbox_ext_buf_ctx.seqNum = mbox_req->extSeqNum;
4054 phba->mbox_ext_buf_ctx.mbx_dmabuf = dmabuf;
4055
4056 if (ext_buf_cnt == 1) {
4057 /* bsg tracking structure */
4058 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
4059 if (!dd_data) {
4060 rc = -ENOMEM;
4061 goto job_error;
4062 }
4063
4064 /* mailbox command structure for base driver */
4065 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4066 if (!pmboxq) {
4067 rc = -ENOMEM;
4068 goto job_error;
4069 }
4070 memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
4071 pmb = &pmboxq->u.mb;
4072 mbx = (uint8_t *)dmabuf->virt;
4073 memcpy(pmb, mbx, sizeof(*pmb));
4074 pmb->mbxOwner = OWN_HOST;
4075 pmboxq->vport = phba->pport;
4076
4077 /* callback for multi-buffer read mailbox command */
4078 pmboxq->mbox_cmpl = lpfc_bsg_issue_write_mbox_ext_cmpl;
4079
4080 /* context fields to callback function */
4081 pmboxq->context1 = dd_data;
4082 dd_data->type = TYPE_MBOX;
James Smarta33c4f72013-03-01 16:36:00 -05004083 dd_data->set_job = job;
James Smart7ad20aa2011-05-24 11:44:28 -04004084 dd_data->context_un.mbox.pmboxq = pmboxq;
4085 dd_data->context_un.mbox.mb = (MAILBOX_t *)mbx;
James Smart7ad20aa2011-05-24 11:44:28 -04004086 job->dd_data = dd_data;
4087
4088 /* state change */
James Smart7ad20aa2011-05-24 11:44:28 -04004089
James Smarta33c4f72013-03-01 16:36:00 -05004090 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
James Smart7ad20aa2011-05-24 11:44:28 -04004091 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
4092 if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
4093 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4094 "2955 Issued SLI_CONFIG ext-buffer "
4095 "maibox command, rc:x%x\n", rc);
James Smart88a2cfb2011-07-22 18:36:33 -04004096 return SLI_CONFIG_HANDLED;
James Smart7ad20aa2011-05-24 11:44:28 -04004097 }
4098 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4099 "2956 Failed to issue SLI_CONFIG ext-buffer "
4100 "maibox command, rc:x%x\n", rc);
4101 rc = -EPIPE;
James Smart026abb82011-12-13 13:20:45 -05004102 goto job_error;
James Smart7ad20aa2011-05-24 11:44:28 -04004103 }
4104
James Smart88a2cfb2011-07-22 18:36:33 -04004105 /* wait for additoinal external buffers */
James Smarta33c4f72013-03-01 16:36:00 -05004106
James Smart88a2cfb2011-07-22 18:36:33 -04004107 job->reply->result = 0;
4108 job->job_done(job);
4109 return SLI_CONFIG_HANDLED;
4110
James Smart7ad20aa2011-05-24 11:44:28 -04004111job_error:
4112 if (pmboxq)
4113 mempool_free(pmboxq, phba->mbox_mem_pool);
4114 kfree(dd_data);
4115
4116 return rc;
4117}
4118
4119/**
4120 * lpfc_bsg_handle_sli_cfg_mbox - handle sli-cfg mailbox cmd with ext buffer
4121 * @phba: Pointer to HBA context object.
4122 * @mb: Pointer to a BSG mailbox object.
4123 * @dmabuff: Pointer to a DMA buffer descriptor.
4124 *
4125 * This routine handles SLI_CONFIG (0x9B) mailbox command with non-embedded
4126 * external bufffers, including both 0x9B with non-embedded MSEs and 0x9B
4127 * with embedded sussystem 0x1 and opcodes with external HBDs.
4128 **/
4129static int
4130lpfc_bsg_handle_sli_cfg_mbox(struct lpfc_hba *phba, struct fc_bsg_job *job,
4131 struct lpfc_dmabuf *dmabuf)
4132{
4133 struct lpfc_sli_config_mbox *sli_cfg_mbx;
4134 uint32_t subsys;
4135 uint32_t opcode;
4136 int rc = SLI_CONFIG_NOT_HANDLED;
4137
James Smart026abb82011-12-13 13:20:45 -05004138 /* state change on new multi-buffer pass-through mailbox command */
James Smart7ad20aa2011-05-24 11:44:28 -04004139 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_HOST;
4140
4141 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
4142
4143 if (!bsg_bf_get(lpfc_mbox_hdr_emb,
4144 &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) {
4145 subsys = bsg_bf_get(lpfc_emb0_subcmnd_subsys,
4146 &sli_cfg_mbx->un.sli_config_emb0_subsys);
4147 opcode = bsg_bf_get(lpfc_emb0_subcmnd_opcode,
4148 &sli_cfg_mbx->un.sli_config_emb0_subsys);
4149 if (subsys == SLI_CONFIG_SUBSYS_FCOE) {
4150 switch (opcode) {
4151 case FCOE_OPCODE_READ_FCF:
James Smart2a2719d2014-02-20 09:56:28 -05004152 case FCOE_OPCODE_GET_DPORT_RESULTS:
James Smart7ad20aa2011-05-24 11:44:28 -04004153 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4154 "2957 Handled SLI_CONFIG "
4155 "subsys_fcoe, opcode:x%x\n",
4156 opcode);
4157 rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
4158 nemb_mse, dmabuf);
4159 break;
4160 case FCOE_OPCODE_ADD_FCF:
James Smart2a2719d2014-02-20 09:56:28 -05004161 case FCOE_OPCODE_SET_DPORT_MODE:
4162 case LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_STATE:
James Smart7ad20aa2011-05-24 11:44:28 -04004163 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4164 "2958 Handled SLI_CONFIG "
4165 "subsys_fcoe, opcode:x%x\n",
4166 opcode);
4167 rc = lpfc_bsg_sli_cfg_write_cmd_ext(phba, job,
4168 nemb_mse, dmabuf);
4169 break;
4170 default:
4171 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
James Smart026abb82011-12-13 13:20:45 -05004172 "2959 Reject SLI_CONFIG "
James Smart7ad20aa2011-05-24 11:44:28 -04004173 "subsys_fcoe, opcode:x%x\n",
4174 opcode);
James Smart026abb82011-12-13 13:20:45 -05004175 rc = -EPERM;
4176 break;
4177 }
4178 } else if (subsys == SLI_CONFIG_SUBSYS_COMN) {
4179 switch (opcode) {
4180 case COMN_OPCODE_GET_CNTL_ADDL_ATTRIBUTES:
James Smartb99570d2012-05-09 21:16:24 -04004181 case COMN_OPCODE_GET_CNTL_ATTRIBUTES:
James Smartc6377972015-04-07 15:07:10 -04004182 case COMN_OPCODE_GET_PROFILE_CONFIG:
James Smart026abb82011-12-13 13:20:45 -05004183 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4184 "3106 Handled SLI_CONFIG "
James Smart6b5151f2012-01-18 16:24:06 -05004185 "subsys_comn, opcode:x%x\n",
James Smart026abb82011-12-13 13:20:45 -05004186 opcode);
4187 rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
4188 nemb_mse, dmabuf);
4189 break;
4190 default:
4191 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4192 "3107 Reject SLI_CONFIG "
James Smart6b5151f2012-01-18 16:24:06 -05004193 "subsys_comn, opcode:x%x\n",
James Smart026abb82011-12-13 13:20:45 -05004194 opcode);
4195 rc = -EPERM;
James Smart7ad20aa2011-05-24 11:44:28 -04004196 break;
4197 }
4198 } else {
4199 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
James Smart026abb82011-12-13 13:20:45 -05004200 "2977 Reject SLI_CONFIG "
James Smart7ad20aa2011-05-24 11:44:28 -04004201 "subsys:x%d, opcode:x%x\n",
4202 subsys, opcode);
James Smart026abb82011-12-13 13:20:45 -05004203 rc = -EPERM;
James Smart7ad20aa2011-05-24 11:44:28 -04004204 }
4205 } else {
4206 subsys = bsg_bf_get(lpfc_emb1_subcmnd_subsys,
4207 &sli_cfg_mbx->un.sli_config_emb1_subsys);
4208 opcode = bsg_bf_get(lpfc_emb1_subcmnd_opcode,
4209 &sli_cfg_mbx->un.sli_config_emb1_subsys);
4210 if (subsys == SLI_CONFIG_SUBSYS_COMN) {
4211 switch (opcode) {
4212 case COMN_OPCODE_READ_OBJECT:
4213 case COMN_OPCODE_READ_OBJECT_LIST:
4214 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4215 "2960 Handled SLI_CONFIG "
4216 "subsys_comn, opcode:x%x\n",
4217 opcode);
4218 rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
4219 nemb_hbd, dmabuf);
4220 break;
4221 case COMN_OPCODE_WRITE_OBJECT:
4222 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4223 "2961 Handled SLI_CONFIG "
4224 "subsys_comn, opcode:x%x\n",
4225 opcode);
4226 rc = lpfc_bsg_sli_cfg_write_cmd_ext(phba, job,
4227 nemb_hbd, dmabuf);
4228 break;
4229 default:
4230 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4231 "2962 Not handled SLI_CONFIG "
4232 "subsys_comn, opcode:x%x\n",
4233 opcode);
4234 rc = SLI_CONFIG_NOT_HANDLED;
4235 break;
4236 }
4237 } else {
4238 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
James Smart026abb82011-12-13 13:20:45 -05004239 "2978 Not handled SLI_CONFIG "
James Smart7ad20aa2011-05-24 11:44:28 -04004240 "subsys:x%d, opcode:x%x\n",
4241 subsys, opcode);
4242 rc = SLI_CONFIG_NOT_HANDLED;
4243 }
4244 }
James Smart026abb82011-12-13 13:20:45 -05004245
4246 /* state reset on not handled new multi-buffer mailbox command */
4247 if (rc != SLI_CONFIG_HANDLED)
4248 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_IDLE;
4249
James Smart7ad20aa2011-05-24 11:44:28 -04004250 return rc;
4251}
4252
4253/**
4254 * lpfc_bsg_mbox_ext_abort_req - request to abort mbox command with ext buffers
4255 * @phba: Pointer to HBA context object.
4256 *
4257 * This routine is for requesting to abort a pass-through mailbox command with
4258 * multiple external buffers due to error condition.
4259 **/
4260static void
4261lpfc_bsg_mbox_ext_abort(struct lpfc_hba *phba)
4262{
4263 if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_PORT)
4264 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_ABTS;
4265 else
4266 lpfc_bsg_mbox_ext_session_reset(phba);
4267 return;
4268}
4269
4270/**
4271 * lpfc_bsg_read_ebuf_get - get the next mailbox read external buffer
4272 * @phba: Pointer to HBA context object.
4273 * @dmabuf: Pointer to a DMA buffer descriptor.
4274 *
4275 * This routine extracts the next mailbox read external buffer back to
4276 * user space through BSG.
4277 **/
4278static int
4279lpfc_bsg_read_ebuf_get(struct lpfc_hba *phba, struct fc_bsg_job *job)
4280{
4281 struct lpfc_sli_config_mbox *sli_cfg_mbx;
4282 struct lpfc_dmabuf *dmabuf;
4283 uint8_t *pbuf;
4284 uint32_t size;
4285 uint32_t index;
4286
4287 index = phba->mbox_ext_buf_ctx.seqNum;
4288 phba->mbox_ext_buf_ctx.seqNum++;
4289
4290 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)
4291 phba->mbox_ext_buf_ctx.mbx_dmabuf->virt;
4292
4293 if (phba->mbox_ext_buf_ctx.nembType == nemb_mse) {
4294 size = bsg_bf_get(lpfc_mbox_sli_config_mse_len,
4295 &sli_cfg_mbx->un.sli_config_emb0_subsys.mse[index]);
4296 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4297 "2963 SLI_CONFIG (mse) ext-buffer rd get "
4298 "buffer[%d], size:%d\n", index, size);
4299 } else {
4300 size = bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
4301 &sli_cfg_mbx->un.sli_config_emb1_subsys.hbd[index]);
4302 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4303 "2964 SLI_CONFIG (hbd) ext-buffer rd get "
4304 "buffer[%d], size:%d\n", index, size);
4305 }
4306 if (list_empty(&phba->mbox_ext_buf_ctx.ext_dmabuf_list))
4307 return -EPIPE;
4308 dmabuf = list_first_entry(&phba->mbox_ext_buf_ctx.ext_dmabuf_list,
4309 struct lpfc_dmabuf, list);
4310 list_del_init(&dmabuf->list);
James Smartb76f2dc2011-07-22 18:37:42 -04004311
4312 /* after dma buffer descriptor setup */
4313 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, phba->mbox_ext_buf_ctx.nembType,
4314 mbox_rd, dma_ebuf, sta_pos_addr,
4315 dmabuf, index);
4316
James Smart7ad20aa2011-05-24 11:44:28 -04004317 pbuf = (uint8_t *)dmabuf->virt;
4318 job->reply->reply_payload_rcv_len =
4319 sg_copy_from_buffer(job->reply_payload.sg_list,
4320 job->reply_payload.sg_cnt,
4321 pbuf, size);
4322
4323 lpfc_bsg_dma_page_free(phba, dmabuf);
4324
4325 if (phba->mbox_ext_buf_ctx.seqNum == phba->mbox_ext_buf_ctx.numBuf) {
4326 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4327 "2965 SLI_CONFIG (hbd) ext-buffer rd mbox "
4328 "command session done\n");
4329 lpfc_bsg_mbox_ext_session_reset(phba);
4330 }
4331
4332 job->reply->result = 0;
4333 job->job_done(job);
4334
4335 return SLI_CONFIG_HANDLED;
4336}
4337
4338/**
4339 * lpfc_bsg_write_ebuf_set - set the next mailbox write external buffer
4340 * @phba: Pointer to HBA context object.
4341 * @dmabuf: Pointer to a DMA buffer descriptor.
4342 *
4343 * This routine sets up the next mailbox read external buffer obtained
4344 * from user space through BSG.
4345 **/
4346static int
4347lpfc_bsg_write_ebuf_set(struct lpfc_hba *phba, struct fc_bsg_job *job,
4348 struct lpfc_dmabuf *dmabuf)
4349{
4350 struct lpfc_sli_config_mbox *sli_cfg_mbx;
4351 struct bsg_job_data *dd_data = NULL;
4352 LPFC_MBOXQ_t *pmboxq = NULL;
4353 MAILBOX_t *pmb;
4354 enum nemb_type nemb_tp;
4355 uint8_t *pbuf;
4356 uint32_t size;
4357 uint32_t index;
4358 int rc;
4359
4360 index = phba->mbox_ext_buf_ctx.seqNum;
4361 phba->mbox_ext_buf_ctx.seqNum++;
4362 nemb_tp = phba->mbox_ext_buf_ctx.nembType;
4363
4364 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)
4365 phba->mbox_ext_buf_ctx.mbx_dmabuf->virt;
4366
4367 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
4368 if (!dd_data) {
4369 rc = -ENOMEM;
4370 goto job_error;
4371 }
4372
4373 pbuf = (uint8_t *)dmabuf->virt;
4374 size = job->request_payload.payload_len;
4375 sg_copy_to_buffer(job->request_payload.sg_list,
4376 job->request_payload.sg_cnt,
4377 pbuf, size);
4378
4379 if (phba->mbox_ext_buf_ctx.nembType == nemb_mse) {
4380 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4381 "2966 SLI_CONFIG (mse) ext-buffer wr set "
4382 "buffer[%d], size:%d\n",
4383 phba->mbox_ext_buf_ctx.seqNum, size);
4384
4385 } else {
4386 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4387 "2967 SLI_CONFIG (hbd) ext-buffer wr set "
4388 "buffer[%d], size:%d\n",
4389 phba->mbox_ext_buf_ctx.seqNum, size);
4390
4391 }
4392
4393 /* set up external buffer descriptor and add to external buffer list */
4394 lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, index,
4395 phba->mbox_ext_buf_ctx.mbx_dmabuf,
4396 dmabuf);
4397 list_add_tail(&dmabuf->list, &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
4398
James Smartb76f2dc2011-07-22 18:37:42 -04004399 /* after write dma buffer */
4400 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, phba->mbox_ext_buf_ctx.nembType,
4401 mbox_wr, dma_ebuf, sta_pos_addr,
4402 dmabuf, index);
4403
James Smart7ad20aa2011-05-24 11:44:28 -04004404 if (phba->mbox_ext_buf_ctx.seqNum == phba->mbox_ext_buf_ctx.numBuf) {
4405 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4406 "2968 SLI_CONFIG ext-buffer wr all %d "
4407 "ebuffers received\n",
4408 phba->mbox_ext_buf_ctx.numBuf);
4409 /* mailbox command structure for base driver */
4410 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4411 if (!pmboxq) {
4412 rc = -ENOMEM;
4413 goto job_error;
4414 }
4415 memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
4416 pbuf = (uint8_t *)phba->mbox_ext_buf_ctx.mbx_dmabuf->virt;
4417 pmb = &pmboxq->u.mb;
4418 memcpy(pmb, pbuf, sizeof(*pmb));
4419 pmb->mbxOwner = OWN_HOST;
4420 pmboxq->vport = phba->pport;
4421
4422 /* callback for multi-buffer write mailbox command */
4423 pmboxq->mbox_cmpl = lpfc_bsg_issue_write_mbox_ext_cmpl;
4424
4425 /* context fields to callback function */
4426 pmboxq->context1 = dd_data;
4427 dd_data->type = TYPE_MBOX;
James Smarta33c4f72013-03-01 16:36:00 -05004428 dd_data->set_job = job;
James Smart7ad20aa2011-05-24 11:44:28 -04004429 dd_data->context_un.mbox.pmboxq = pmboxq;
4430 dd_data->context_un.mbox.mb = (MAILBOX_t *)pbuf;
James Smart7ad20aa2011-05-24 11:44:28 -04004431 job->dd_data = dd_data;
4432
4433 /* state change */
4434 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
4435
4436 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
4437 if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
4438 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4439 "2969 Issued SLI_CONFIG ext-buffer "
4440 "maibox command, rc:x%x\n", rc);
James Smart88a2cfb2011-07-22 18:36:33 -04004441 return SLI_CONFIG_HANDLED;
James Smart7ad20aa2011-05-24 11:44:28 -04004442 }
4443 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4444 "2970 Failed to issue SLI_CONFIG ext-buffer "
4445 "maibox command, rc:x%x\n", rc);
4446 rc = -EPIPE;
4447 goto job_error;
4448 }
4449
4450 /* wait for additoinal external buffers */
4451 job->reply->result = 0;
4452 job->job_done(job);
4453 return SLI_CONFIG_HANDLED;
4454
4455job_error:
4456 lpfc_bsg_dma_page_free(phba, dmabuf);
4457 kfree(dd_data);
4458
4459 return rc;
4460}
4461
4462/**
4463 * lpfc_bsg_handle_sli_cfg_ebuf - handle ext buffer with sli-cfg mailbox cmd
4464 * @phba: Pointer to HBA context object.
4465 * @mb: Pointer to a BSG mailbox object.
4466 * @dmabuff: Pointer to a DMA buffer descriptor.
4467 *
4468 * This routine handles the external buffer with SLI_CONFIG (0x9B) mailbox
4469 * command with multiple non-embedded external buffers.
4470 **/
4471static int
4472lpfc_bsg_handle_sli_cfg_ebuf(struct lpfc_hba *phba, struct fc_bsg_job *job,
4473 struct lpfc_dmabuf *dmabuf)
4474{
4475 int rc;
4476
4477 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4478 "2971 SLI_CONFIG buffer (type:x%x)\n",
4479 phba->mbox_ext_buf_ctx.mboxType);
4480
4481 if (phba->mbox_ext_buf_ctx.mboxType == mbox_rd) {
4482 if (phba->mbox_ext_buf_ctx.state != LPFC_BSG_MBOX_DONE) {
4483 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4484 "2972 SLI_CONFIG rd buffer state "
4485 "mismatch:x%x\n",
4486 phba->mbox_ext_buf_ctx.state);
4487 lpfc_bsg_mbox_ext_abort(phba);
4488 return -EPIPE;
4489 }
4490 rc = lpfc_bsg_read_ebuf_get(phba, job);
4491 if (rc == SLI_CONFIG_HANDLED)
4492 lpfc_bsg_dma_page_free(phba, dmabuf);
4493 } else { /* phba->mbox_ext_buf_ctx.mboxType == mbox_wr */
4494 if (phba->mbox_ext_buf_ctx.state != LPFC_BSG_MBOX_HOST) {
4495 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4496 "2973 SLI_CONFIG wr buffer state "
4497 "mismatch:x%x\n",
4498 phba->mbox_ext_buf_ctx.state);
4499 lpfc_bsg_mbox_ext_abort(phba);
4500 return -EPIPE;
4501 }
4502 rc = lpfc_bsg_write_ebuf_set(phba, job, dmabuf);
4503 }
4504 return rc;
4505}
4506
4507/**
4508 * lpfc_bsg_handle_sli_cfg_ext - handle sli-cfg mailbox with external buffer
4509 * @phba: Pointer to HBA context object.
4510 * @mb: Pointer to a BSG mailbox object.
4511 * @dmabuff: Pointer to a DMA buffer descriptor.
4512 *
4513 * This routine checkes and handles non-embedded multi-buffer SLI_CONFIG
4514 * (0x9B) mailbox commands and external buffers.
4515 **/
4516static int
4517lpfc_bsg_handle_sli_cfg_ext(struct lpfc_hba *phba, struct fc_bsg_job *job,
4518 struct lpfc_dmabuf *dmabuf)
4519{
4520 struct dfc_mbox_req *mbox_req;
James Smart88a2cfb2011-07-22 18:36:33 -04004521 int rc = SLI_CONFIG_NOT_HANDLED;
James Smart7ad20aa2011-05-24 11:44:28 -04004522
4523 mbox_req =
4524 (struct dfc_mbox_req *)job->request->rqst_data.h_vendor.vendor_cmd;
4525
4526 /* mbox command with/without single external buffer */
4527 if (mbox_req->extMboxTag == 0 && mbox_req->extSeqNum == 0)
James Smart88a2cfb2011-07-22 18:36:33 -04004528 return rc;
James Smart7ad20aa2011-05-24 11:44:28 -04004529
4530 /* mbox command and first external buffer */
4531 if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_IDLE) {
4532 if (mbox_req->extSeqNum == 1) {
4533 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4534 "2974 SLI_CONFIG mailbox: tag:%d, "
4535 "seq:%d\n", mbox_req->extMboxTag,
4536 mbox_req->extSeqNum);
4537 rc = lpfc_bsg_handle_sli_cfg_mbox(phba, job, dmabuf);
4538 return rc;
4539 } else
4540 goto sli_cfg_ext_error;
4541 }
4542
4543 /*
4544 * handle additional external buffers
4545 */
4546
4547 /* check broken pipe conditions */
4548 if (mbox_req->extMboxTag != phba->mbox_ext_buf_ctx.mbxTag)
4549 goto sli_cfg_ext_error;
4550 if (mbox_req->extSeqNum > phba->mbox_ext_buf_ctx.numBuf)
4551 goto sli_cfg_ext_error;
4552 if (mbox_req->extSeqNum != phba->mbox_ext_buf_ctx.seqNum + 1)
4553 goto sli_cfg_ext_error;
4554
4555 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4556 "2975 SLI_CONFIG mailbox external buffer: "
4557 "extSta:x%x, tag:%d, seq:%d\n",
4558 phba->mbox_ext_buf_ctx.state, mbox_req->extMboxTag,
4559 mbox_req->extSeqNum);
4560 rc = lpfc_bsg_handle_sli_cfg_ebuf(phba, job, dmabuf);
4561 return rc;
4562
4563sli_cfg_ext_error:
4564 /* all other cases, broken pipe */
4565 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4566 "2976 SLI_CONFIG mailbox broken pipe: "
4567 "ctxSta:x%x, ctxNumBuf:%d "
4568 "ctxTag:%d, ctxSeq:%d, tag:%d, seq:%d\n",
4569 phba->mbox_ext_buf_ctx.state,
4570 phba->mbox_ext_buf_ctx.numBuf,
4571 phba->mbox_ext_buf_ctx.mbxTag,
4572 phba->mbox_ext_buf_ctx.seqNum,
4573 mbox_req->extMboxTag, mbox_req->extSeqNum);
4574
4575 lpfc_bsg_mbox_ext_session_reset(phba);
4576
4577 return -EPIPE;
4578}
4579
4580/**
James Smart3b5dd522010-01-26 23:10:15 -05004581 * lpfc_bsg_issue_mbox - issues a mailbox command on behalf of an app
4582 * @phba: Pointer to HBA context object.
4583 * @mb: Pointer to a mailbox object.
4584 * @vport: Pointer to a vport object.
4585 *
4586 * Allocate a tracking object, mailbox command memory, get a mailbox
4587 * from the mailbox pool, copy the caller mailbox command.
4588 *
4589 * If offline and the sli is active we need to poll for the command (port is
4590 * being reset) and com-plete the job, otherwise issue the mailbox command and
4591 * let our completion handler finish the command.
4592 **/
James Smarta2fc4aef2014-09-03 12:57:55 -04004593static int
James Smart3b5dd522010-01-26 23:10:15 -05004594lpfc_bsg_issue_mbox(struct lpfc_hba *phba, struct fc_bsg_job *job,
4595 struct lpfc_vport *vport)
4596{
James Smart7a470272010-03-15 11:25:20 -04004597 LPFC_MBOXQ_t *pmboxq = NULL; /* internal mailbox queue */
4598 MAILBOX_t *pmb; /* shortcut to the pmboxq mailbox */
4599 /* a 4k buffer to hold the mb and extended data from/to the bsg */
James Smart7ad20aa2011-05-24 11:44:28 -04004600 uint8_t *pmbx = NULL;
James Smart7a470272010-03-15 11:25:20 -04004601 struct bsg_job_data *dd_data = NULL; /* bsg data tracking structure */
James Smart7ad20aa2011-05-24 11:44:28 -04004602 struct lpfc_dmabuf *dmabuf = NULL;
4603 struct dfc_mbox_req *mbox_req;
James Smartb6e3b9c2011-04-16 11:03:43 -04004604 struct READ_EVENT_LOG_VAR *rdEventLog;
4605 uint32_t transmit_length, receive_length, mode;
James Smart7ad20aa2011-05-24 11:44:28 -04004606 struct lpfc_mbx_sli4_config *sli4_config;
James Smartb6e3b9c2011-04-16 11:03:43 -04004607 struct lpfc_mbx_nembed_cmd *nembed_sge;
4608 struct mbox_header *header;
4609 struct ulp_bde64 *bde;
James Smart7a470272010-03-15 11:25:20 -04004610 uint8_t *ext = NULL;
James Smart3b5dd522010-01-26 23:10:15 -05004611 int rc = 0;
James Smart7a470272010-03-15 11:25:20 -04004612 uint8_t *from;
James Smart7ad20aa2011-05-24 11:44:28 -04004613 uint32_t size;
4614
James Smart7a470272010-03-15 11:25:20 -04004615 /* in case no data is transferred */
4616 job->reply->reply_payload_rcv_len = 0;
4617
James Smartb6e3b9c2011-04-16 11:03:43 -04004618 /* sanity check to protect driver */
4619 if (job->reply_payload.payload_len > BSG_MBOX_SIZE ||
4620 job->request_payload.payload_len > BSG_MBOX_SIZE) {
4621 rc = -ERANGE;
4622 goto job_done;
4623 }
4624
James Smart7ad20aa2011-05-24 11:44:28 -04004625 /*
4626 * Don't allow mailbox commands to be sent when blocked or when in
4627 * the middle of discovery
4628 */
4629 if (phba->sli.sli_flag & LPFC_BLOCK_MGMT_IO) {
4630 rc = -EAGAIN;
4631 goto job_done;
4632 }
4633
4634 mbox_req =
4635 (struct dfc_mbox_req *)job->request->rqst_data.h_vendor.vendor_cmd;
4636
James Smart7a470272010-03-15 11:25:20 -04004637 /* check if requested extended data lengths are valid */
James Smartb6e3b9c2011-04-16 11:03:43 -04004638 if ((mbox_req->inExtWLen > BSG_MBOX_SIZE/sizeof(uint32_t)) ||
4639 (mbox_req->outExtWLen > BSG_MBOX_SIZE/sizeof(uint32_t))) {
James Smart7a470272010-03-15 11:25:20 -04004640 rc = -ERANGE;
4641 goto job_done;
4642 }
James Smart3b5dd522010-01-26 23:10:15 -05004643
James Smart7ad20aa2011-05-24 11:44:28 -04004644 dmabuf = lpfc_bsg_dma_page_alloc(phba);
4645 if (!dmabuf || !dmabuf->virt) {
4646 rc = -ENOMEM;
4647 goto job_done;
4648 }
4649
4650 /* Get the mailbox command or external buffer from BSG */
4651 pmbx = (uint8_t *)dmabuf->virt;
4652 size = job->request_payload.payload_len;
4653 sg_copy_to_buffer(job->request_payload.sg_list,
4654 job->request_payload.sg_cnt, pmbx, size);
4655
4656 /* Handle possible SLI_CONFIG with non-embedded payloads */
4657 if (phba->sli_rev == LPFC_SLI_REV4) {
4658 rc = lpfc_bsg_handle_sli_cfg_ext(phba, job, dmabuf);
4659 if (rc == SLI_CONFIG_HANDLED)
4660 goto job_cont;
4661 if (rc)
4662 goto job_done;
4663 /* SLI_CONFIG_NOT_HANDLED for other mailbox commands */
4664 }
4665
4666 rc = lpfc_bsg_check_cmd_access(phba, (MAILBOX_t *)pmbx, vport);
4667 if (rc != 0)
4668 goto job_done; /* must be negative */
4669
James Smart3b5dd522010-01-26 23:10:15 -05004670 /* allocate our bsg tracking structure */
4671 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
4672 if (!dd_data) {
4673 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
4674 "2727 Failed allocation of dd_data\n");
James Smart7a470272010-03-15 11:25:20 -04004675 rc = -ENOMEM;
4676 goto job_done;
James Smart3b5dd522010-01-26 23:10:15 -05004677 }
4678
James Smart3b5dd522010-01-26 23:10:15 -05004679 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4680 if (!pmboxq) {
James Smart7a470272010-03-15 11:25:20 -04004681 rc = -ENOMEM;
4682 goto job_done;
James Smart3b5dd522010-01-26 23:10:15 -05004683 }
James Smart7a470272010-03-15 11:25:20 -04004684 memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
James Smart3b5dd522010-01-26 23:10:15 -05004685
James Smart3b5dd522010-01-26 23:10:15 -05004686 pmb = &pmboxq->u.mb;
James Smart7ad20aa2011-05-24 11:44:28 -04004687 memcpy(pmb, pmbx, sizeof(*pmb));
James Smart3b5dd522010-01-26 23:10:15 -05004688 pmb->mbxOwner = OWN_HOST;
James Smart3b5dd522010-01-26 23:10:15 -05004689 pmboxq->vport = vport;
4690
James Smartc7495932010-04-06 15:05:28 -04004691 /* If HBA encountered an error attention, allow only DUMP
4692 * or RESTART mailbox commands until the HBA is restarted.
4693 */
4694 if (phba->pport->stopped &&
4695 pmb->mbxCommand != MBX_DUMP_MEMORY &&
4696 pmb->mbxCommand != MBX_RESTART &&
4697 pmb->mbxCommand != MBX_WRITE_VPARMS &&
4698 pmb->mbxCommand != MBX_WRITE_WWN)
4699 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
4700 "2797 mbox: Issued mailbox cmd "
4701 "0x%x while in stopped state.\n",
4702 pmb->mbxCommand);
4703
James Smart7a470272010-03-15 11:25:20 -04004704 /* extended mailbox commands will need an extended buffer */
James Smartc7495932010-04-06 15:05:28 -04004705 if (mbox_req->inExtWLen || mbox_req->outExtWLen) {
James Smart026abb82011-12-13 13:20:45 -05004706 from = pmbx;
4707 ext = from + sizeof(MAILBOX_t);
James Smart7a470272010-03-15 11:25:20 -04004708 pmboxq->context2 = ext;
4709 pmboxq->in_ext_byte_len =
James Smart7a470272010-03-15 11:25:20 -04004710 mbox_req->inExtWLen * sizeof(uint32_t);
4711 pmboxq->out_ext_byte_len =
James Smartc7495932010-04-06 15:05:28 -04004712 mbox_req->outExtWLen * sizeof(uint32_t);
James Smart7a470272010-03-15 11:25:20 -04004713 pmboxq->mbox_offset_word = mbox_req->mbOffset;
4714 }
4715
4716 /* biu diag will need a kernel buffer to transfer the data
4717 * allocate our own buffer and setup the mailbox command to
4718 * use ours
4719 */
4720 if (pmb->mbxCommand == MBX_RUN_BIU_DIAG64) {
James Smartb6e3b9c2011-04-16 11:03:43 -04004721 transmit_length = pmb->un.varWords[1];
4722 receive_length = pmb->un.varWords[4];
James Smartc7495932010-04-06 15:05:28 -04004723 /* transmit length cannot be greater than receive length or
4724 * mailbox extension size
4725 */
4726 if ((transmit_length > receive_length) ||
James Smart88a2cfb2011-07-22 18:36:33 -04004727 (transmit_length > BSG_MBOX_SIZE - sizeof(MAILBOX_t))) {
James Smartc7495932010-04-06 15:05:28 -04004728 rc = -ERANGE;
4729 goto job_done;
4730 }
James Smart7a470272010-03-15 11:25:20 -04004731 pmb->un.varBIUdiag.un.s2.xmit_bde64.addrHigh =
James Smart7ad20aa2011-05-24 11:44:28 -04004732 putPaddrHigh(dmabuf->phys + sizeof(MAILBOX_t));
James Smart7a470272010-03-15 11:25:20 -04004733 pmb->un.varBIUdiag.un.s2.xmit_bde64.addrLow =
James Smart7ad20aa2011-05-24 11:44:28 -04004734 putPaddrLow(dmabuf->phys + sizeof(MAILBOX_t));
James Smart7a470272010-03-15 11:25:20 -04004735
4736 pmb->un.varBIUdiag.un.s2.rcv_bde64.addrHigh =
James Smart7ad20aa2011-05-24 11:44:28 -04004737 putPaddrHigh(dmabuf->phys + sizeof(MAILBOX_t)
4738 + pmb->un.varBIUdiag.un.s2.xmit_bde64.tus.f.bdeSize);
James Smart7a470272010-03-15 11:25:20 -04004739 pmb->un.varBIUdiag.un.s2.rcv_bde64.addrLow =
James Smart7ad20aa2011-05-24 11:44:28 -04004740 putPaddrLow(dmabuf->phys + sizeof(MAILBOX_t)
4741 + pmb->un.varBIUdiag.un.s2.xmit_bde64.tus.f.bdeSize);
James Smartc7495932010-04-06 15:05:28 -04004742 } else if (pmb->mbxCommand == MBX_READ_EVENT_LOG) {
James Smartb6e3b9c2011-04-16 11:03:43 -04004743 rdEventLog = &pmb->un.varRdEventLog;
4744 receive_length = rdEventLog->rcv_bde64.tus.f.bdeSize;
4745 mode = bf_get(lpfc_event_log, rdEventLog);
James Smartc7495932010-04-06 15:05:28 -04004746
4747 /* receive length cannot be greater than mailbox
4748 * extension size
4749 */
James Smart88a2cfb2011-07-22 18:36:33 -04004750 if (receive_length > BSG_MBOX_SIZE - sizeof(MAILBOX_t)) {
James Smartc7495932010-04-06 15:05:28 -04004751 rc = -ERANGE;
4752 goto job_done;
4753 }
4754
4755 /* mode zero uses a bde like biu diags command */
4756 if (mode == 0) {
James Smart7ad20aa2011-05-24 11:44:28 -04004757 pmb->un.varWords[3] = putPaddrLow(dmabuf->phys
4758 + sizeof(MAILBOX_t));
4759 pmb->un.varWords[4] = putPaddrHigh(dmabuf->phys
4760 + sizeof(MAILBOX_t));
James Smartc7495932010-04-06 15:05:28 -04004761 }
4762 } else if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart3ef6d242012-01-18 16:23:48 -05004763 /* Let type 4 (well known data) through because the data is
4764 * returned in varwords[4-8]
4765 * otherwise check the recieve length and fetch the buffer addr
4766 */
4767 if ((pmb->mbxCommand == MBX_DUMP_MEMORY) &&
4768 (pmb->un.varDmp.type != DMP_WELL_KNOWN)) {
James Smartc7495932010-04-06 15:05:28 -04004769 /* rebuild the command for sli4 using our own buffers
4770 * like we do for biu diags
4771 */
James Smartb6e3b9c2011-04-16 11:03:43 -04004772 receive_length = pmb->un.varWords[2];
James Smartc7495932010-04-06 15:05:28 -04004773 /* receive length cannot be greater than mailbox
4774 * extension size
4775 */
James Smart7ad20aa2011-05-24 11:44:28 -04004776 if (receive_length == 0) {
James Smartc7495932010-04-06 15:05:28 -04004777 rc = -ERANGE;
4778 goto job_done;
4779 }
James Smart7ad20aa2011-05-24 11:44:28 -04004780 pmb->un.varWords[3] = putPaddrLow(dmabuf->phys
4781 + sizeof(MAILBOX_t));
4782 pmb->un.varWords[4] = putPaddrHigh(dmabuf->phys
4783 + sizeof(MAILBOX_t));
James Smartc7495932010-04-06 15:05:28 -04004784 } else if ((pmb->mbxCommand == MBX_UPDATE_CFG) &&
4785 pmb->un.varUpdateCfg.co) {
James Smartb6e3b9c2011-04-16 11:03:43 -04004786 bde = (struct ulp_bde64 *)&pmb->un.varWords[4];
James Smartc7495932010-04-06 15:05:28 -04004787
4788 /* bde size cannot be greater than mailbox ext size */
James Smart88a2cfb2011-07-22 18:36:33 -04004789 if (bde->tus.f.bdeSize >
4790 BSG_MBOX_SIZE - sizeof(MAILBOX_t)) {
James Smartc7495932010-04-06 15:05:28 -04004791 rc = -ERANGE;
4792 goto job_done;
4793 }
James Smart7ad20aa2011-05-24 11:44:28 -04004794 bde->addrHigh = putPaddrHigh(dmabuf->phys
4795 + sizeof(MAILBOX_t));
4796 bde->addrLow = putPaddrLow(dmabuf->phys
4797 + sizeof(MAILBOX_t));
James Smart515e0aa2010-09-29 11:19:00 -04004798 } else if (pmb->mbxCommand == MBX_SLI4_CONFIG) {
James Smart7ad20aa2011-05-24 11:44:28 -04004799 /* Handling non-embedded SLI_CONFIG mailbox command */
4800 sli4_config = &pmboxq->u.mqe.un.sli4_config;
4801 if (!bf_get(lpfc_mbox_hdr_emb,
4802 &sli4_config->header.cfg_mhdr)) {
4803 /* rebuild the command for sli4 using our
4804 * own buffers like we do for biu diags
4805 */
4806 header = (struct mbox_header *)
4807 &pmb->un.varWords[0];
4808 nembed_sge = (struct lpfc_mbx_nembed_cmd *)
4809 &pmb->un.varWords[0];
4810 receive_length = nembed_sge->sge[0].length;
James Smart515e0aa2010-09-29 11:19:00 -04004811
James Smart7ad20aa2011-05-24 11:44:28 -04004812 /* receive length cannot be greater than
4813 * mailbox extension size
4814 */
4815 if ((receive_length == 0) ||
James Smart88a2cfb2011-07-22 18:36:33 -04004816 (receive_length >
4817 BSG_MBOX_SIZE - sizeof(MAILBOX_t))) {
James Smart7ad20aa2011-05-24 11:44:28 -04004818 rc = -ERANGE;
4819 goto job_done;
4820 }
4821
4822 nembed_sge->sge[0].pa_hi =
4823 putPaddrHigh(dmabuf->phys
4824 + sizeof(MAILBOX_t));
4825 nembed_sge->sge[0].pa_lo =
4826 putPaddrLow(dmabuf->phys
4827 + sizeof(MAILBOX_t));
James Smart515e0aa2010-09-29 11:19:00 -04004828 }
James Smartc7495932010-04-06 15:05:28 -04004829 }
James Smart3b5dd522010-01-26 23:10:15 -05004830 }
4831
James Smart7ad20aa2011-05-24 11:44:28 -04004832 dd_data->context_un.mbox.dmabuffers = dmabuf;
James Smartc7495932010-04-06 15:05:28 -04004833
James Smart3b5dd522010-01-26 23:10:15 -05004834 /* setup wake call as IOCB callback */
James Smart7ad20aa2011-05-24 11:44:28 -04004835 pmboxq->mbox_cmpl = lpfc_bsg_issue_mbox_cmpl;
James Smart7a470272010-03-15 11:25:20 -04004836
James Smart3b5dd522010-01-26 23:10:15 -05004837 /* setup context field to pass wait_queue pointer to wake function */
4838 pmboxq->context1 = dd_data;
4839 dd_data->type = TYPE_MBOX;
James Smarta33c4f72013-03-01 16:36:00 -05004840 dd_data->set_job = job;
James Smart3b5dd522010-01-26 23:10:15 -05004841 dd_data->context_un.mbox.pmboxq = pmboxq;
James Smart7ad20aa2011-05-24 11:44:28 -04004842 dd_data->context_un.mbox.mb = (MAILBOX_t *)pmbx;
James Smart7a470272010-03-15 11:25:20 -04004843 dd_data->context_un.mbox.ext = ext;
4844 dd_data->context_un.mbox.mbOffset = mbox_req->mbOffset;
4845 dd_data->context_un.mbox.inExtWLen = mbox_req->inExtWLen;
James Smartc7495932010-04-06 15:05:28 -04004846 dd_data->context_un.mbox.outExtWLen = mbox_req->outExtWLen;
James Smart3b5dd522010-01-26 23:10:15 -05004847 job->dd_data = dd_data;
James Smart7a470272010-03-15 11:25:20 -04004848
4849 if ((vport->fc_flag & FC_OFFLINE_MODE) ||
4850 (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))) {
4851 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
4852 if (rc != MBX_SUCCESS) {
4853 rc = (rc == MBX_TIMEOUT) ? -ETIME : -ENODEV;
4854 goto job_done;
4855 }
4856
4857 /* job finished, copy the data */
James Smart7ad20aa2011-05-24 11:44:28 -04004858 memcpy(pmbx, pmb, sizeof(*pmb));
James Smart7a470272010-03-15 11:25:20 -04004859 job->reply->reply_payload_rcv_len =
4860 sg_copy_from_buffer(job->reply_payload.sg_list,
James Smart7ad20aa2011-05-24 11:44:28 -04004861 job->reply_payload.sg_cnt,
4862 pmbx, size);
James Smart7a470272010-03-15 11:25:20 -04004863 /* not waiting mbox already done */
4864 rc = 0;
4865 goto job_done;
James Smart3b5dd522010-01-26 23:10:15 -05004866 }
4867
James Smart7a470272010-03-15 11:25:20 -04004868 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
4869 if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY))
4870 return 1; /* job started */
4871
4872job_done:
4873 /* common exit for error or job completed inline */
James Smart7a470272010-03-15 11:25:20 -04004874 if (pmboxq)
4875 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart7ad20aa2011-05-24 11:44:28 -04004876 lpfc_bsg_dma_page_free(phba, dmabuf);
James Smart7a470272010-03-15 11:25:20 -04004877 kfree(dd_data);
4878
James Smart7ad20aa2011-05-24 11:44:28 -04004879job_cont:
James Smart7a470272010-03-15 11:25:20 -04004880 return rc;
James Smart3b5dd522010-01-26 23:10:15 -05004881}
4882
4883/**
4884 * lpfc_bsg_mbox_cmd - process an fc bsg LPFC_BSG_VENDOR_MBOX command
4885 * @job: MBOX fc_bsg_job for LPFC_BSG_VENDOR_MBOX.
4886 **/
4887static int
4888lpfc_bsg_mbox_cmd(struct fc_bsg_job *job)
4889{
4890 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
4891 struct lpfc_hba *phba = vport->phba;
James Smart7ad20aa2011-05-24 11:44:28 -04004892 struct dfc_mbox_req *mbox_req;
James Smart3b5dd522010-01-26 23:10:15 -05004893 int rc = 0;
4894
James Smart7ad20aa2011-05-24 11:44:28 -04004895 /* mix-and-match backward compatibility */
James Smart3b5dd522010-01-26 23:10:15 -05004896 job->reply->reply_payload_rcv_len = 0;
4897 if (job->request_len <
4898 sizeof(struct fc_bsg_request) + sizeof(struct dfc_mbox_req)) {
James Smart7ad20aa2011-05-24 11:44:28 -04004899 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
Masanari Iida1051e9b2013-03-31 01:23:50 +09004900 "2737 Mix-and-match backward compatibility "
James Smart7ad20aa2011-05-24 11:44:28 -04004901 "between MBOX_REQ old size:%d and "
4902 "new request size:%d\n",
4903 (int)(job->request_len -
4904 sizeof(struct fc_bsg_request)),
4905 (int)sizeof(struct dfc_mbox_req));
4906 mbox_req = (struct dfc_mbox_req *)
4907 job->request->rqst_data.h_vendor.vendor_cmd;
4908 mbox_req->extMboxTag = 0;
4909 mbox_req->extSeqNum = 0;
James Smart3b5dd522010-01-26 23:10:15 -05004910 }
4911
4912 rc = lpfc_bsg_issue_mbox(phba, job, vport);
4913
James Smart3b5dd522010-01-26 23:10:15 -05004914 if (rc == 0) {
4915 /* job done */
4916 job->reply->result = 0;
4917 job->dd_data = NULL;
4918 job->job_done(job);
4919 } else if (rc == 1)
4920 /* job submitted, will complete later*/
4921 rc = 0; /* return zero, no error */
4922 else {
4923 /* some error occurred */
4924 job->reply->result = rc;
4925 job->dd_data = NULL;
4926 }
4927
4928 return rc;
4929}
4930
4931/**
James Smarte2aed292010-02-26 14:15:00 -05004932 * lpfc_bsg_menlo_cmd_cmp - lpfc_menlo_cmd completion handler
4933 * @phba: Pointer to HBA context object.
4934 * @cmdiocbq: Pointer to command iocb.
4935 * @rspiocbq: Pointer to response iocb.
4936 *
4937 * This function is the completion handler for iocbs issued using
4938 * lpfc_menlo_cmd function. This function is called by the
4939 * ring event handler function without any lock held. This function
4940 * can be called from both worker thread context and interrupt
4941 * context. This function also can be called from another thread which
4942 * cleans up the SLI layer objects.
4943 * This function copies the contents of the response iocb to the
4944 * response iocb memory object provided by the caller of
4945 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
4946 * sleeps for the iocb completion.
4947 **/
4948static void
4949lpfc_bsg_menlo_cmd_cmp(struct lpfc_hba *phba,
4950 struct lpfc_iocbq *cmdiocbq,
4951 struct lpfc_iocbq *rspiocbq)
4952{
4953 struct bsg_job_data *dd_data;
4954 struct fc_bsg_job *job;
4955 IOCB_t *rsp;
James Smarta33c4f72013-03-01 16:36:00 -05004956 struct lpfc_dmabuf *bmp, *cmp, *rmp;
James Smarte2aed292010-02-26 14:15:00 -05004957 struct lpfc_bsg_menlo *menlo;
4958 unsigned long flags;
4959 struct menlo_response *menlo_resp;
James Smarta33c4f72013-03-01 16:36:00 -05004960 unsigned int rsp_size;
James Smarte2aed292010-02-26 14:15:00 -05004961 int rc = 0;
4962
James Smarte2aed292010-02-26 14:15:00 -05004963 dd_data = cmdiocbq->context1;
James Smarta33c4f72013-03-01 16:36:00 -05004964 cmp = cmdiocbq->context2;
4965 bmp = cmdiocbq->context3;
James Smarte2aed292010-02-26 14:15:00 -05004966 menlo = &dd_data->context_un.menlo;
James Smarta33c4f72013-03-01 16:36:00 -05004967 rmp = menlo->rmp;
James Smarte2aed292010-02-26 14:15:00 -05004968 rsp = &rspiocbq->iocb;
4969
James Smarta33c4f72013-03-01 16:36:00 -05004970 /* Determine if job has been aborted */
4971 spin_lock_irqsave(&phba->ct_ev_lock, flags);
4972 job = dd_data->set_job;
4973 if (job) {
4974 /* Prevent timeout handling from trying to abort job */
4975 job->dd_data = NULL;
4976 }
4977 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smarte2aed292010-02-26 14:15:00 -05004978
James Smarta33c4f72013-03-01 16:36:00 -05004979 /* Copy the job data or set the failing status for the job */
4980
4981 if (job) {
4982 /* always return the xri, this would be used in the case
4983 * of a menlo download to allow the data to be sent as a
4984 * continuation of the exchange.
4985 */
4986
4987 menlo_resp = (struct menlo_response *)
4988 job->reply->reply_data.vendor_reply.vendor_rsp;
4989 menlo_resp->xri = rsp->ulpContext;
4990 if (rsp->ulpStatus) {
4991 if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
4992 switch (rsp->un.ulpWord[4] & IOERR_PARAM_MASK) {
4993 case IOERR_SEQUENCE_TIMEOUT:
4994 rc = -ETIMEDOUT;
4995 break;
4996 case IOERR_INVALID_RPI:
4997 rc = -EFAULT;
4998 break;
4999 default:
5000 rc = -EACCES;
5001 break;
5002 }
5003 } else {
James Smarte2aed292010-02-26 14:15:00 -05005004 rc = -EACCES;
James Smarte2aed292010-02-26 14:15:00 -05005005 }
James Smarta33c4f72013-03-01 16:36:00 -05005006 } else {
5007 rsp_size = rsp->un.genreq64.bdl.bdeSize;
5008 job->reply->reply_payload_rcv_len =
5009 lpfc_bsg_copy_data(rmp, &job->reply_payload,
5010 rsp_size, 0);
5011 }
James Smarte2aed292010-02-26 14:15:00 -05005012
James Smarta33c4f72013-03-01 16:36:00 -05005013 }
5014
James Smarte2aed292010-02-26 14:15:00 -05005015 lpfc_sli_release_iocbq(phba, cmdiocbq);
James Smarta33c4f72013-03-01 16:36:00 -05005016 lpfc_free_bsg_buffers(phba, cmp);
5017 lpfc_free_bsg_buffers(phba, rmp);
5018 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
James Smarte2aed292010-02-26 14:15:00 -05005019 kfree(bmp);
5020 kfree(dd_data);
James Smarta33c4f72013-03-01 16:36:00 -05005021
5022 /* Complete the job if active */
5023
5024 if (job) {
5025 job->reply->result = rc;
5026 job->job_done(job);
5027 }
5028
James Smarte2aed292010-02-26 14:15:00 -05005029 return;
5030}
5031
5032/**
5033 * lpfc_menlo_cmd - send an ioctl for menlo hardware
5034 * @job: fc_bsg_job to handle
5035 *
5036 * This function issues a gen request 64 CR ioctl for all menlo cmd requests,
5037 * all the command completions will return the xri for the command.
5038 * For menlo data requests a gen request 64 CX is used to continue the exchange
5039 * supplied in the menlo request header xri field.
5040 **/
5041static int
5042lpfc_menlo_cmd(struct fc_bsg_job *job)
5043{
5044 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
5045 struct lpfc_hba *phba = vport->phba;
James Smarta33c4f72013-03-01 16:36:00 -05005046 struct lpfc_iocbq *cmdiocbq;
5047 IOCB_t *cmd;
James Smarte2aed292010-02-26 14:15:00 -05005048 int rc = 0;
5049 struct menlo_command *menlo_cmd;
5050 struct menlo_response *menlo_resp;
James Smarta33c4f72013-03-01 16:36:00 -05005051 struct lpfc_dmabuf *bmp = NULL, *cmp = NULL, *rmp = NULL;
James Smarte2aed292010-02-26 14:15:00 -05005052 int request_nseg;
5053 int reply_nseg;
James Smarte2aed292010-02-26 14:15:00 -05005054 struct bsg_job_data *dd_data;
5055 struct ulp_bde64 *bpl = NULL;
5056
5057 /* in case no data is returned return just the return code */
5058 job->reply->reply_payload_rcv_len = 0;
5059
5060 if (job->request_len <
5061 sizeof(struct fc_bsg_request) +
5062 sizeof(struct menlo_command)) {
5063 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5064 "2784 Received MENLO_CMD request below "
5065 "minimum size\n");
5066 rc = -ERANGE;
5067 goto no_dd_data;
5068 }
5069
5070 if (job->reply_len <
5071 sizeof(struct fc_bsg_request) + sizeof(struct menlo_response)) {
5072 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5073 "2785 Received MENLO_CMD reply below "
5074 "minimum size\n");
5075 rc = -ERANGE;
5076 goto no_dd_data;
5077 }
5078
5079 if (!(phba->menlo_flag & HBA_MENLO_SUPPORT)) {
5080 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5081 "2786 Adapter does not support menlo "
5082 "commands\n");
5083 rc = -EPERM;
5084 goto no_dd_data;
5085 }
5086
5087 menlo_cmd = (struct menlo_command *)
5088 job->request->rqst_data.h_vendor.vendor_cmd;
5089
5090 menlo_resp = (struct menlo_response *)
5091 job->reply->reply_data.vendor_reply.vendor_rsp;
5092
5093 /* allocate our bsg tracking structure */
5094 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
5095 if (!dd_data) {
5096 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5097 "2787 Failed allocation of dd_data\n");
5098 rc = -ENOMEM;
5099 goto no_dd_data;
5100 }
5101
5102 bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5103 if (!bmp) {
5104 rc = -ENOMEM;
5105 goto free_dd;
5106 }
5107
James Smarta33c4f72013-03-01 16:36:00 -05005108 bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
5109 if (!bmp->virt) {
James Smarte2aed292010-02-26 14:15:00 -05005110 rc = -ENOMEM;
5111 goto free_bmp;
5112 }
5113
James Smarte2aed292010-02-26 14:15:00 -05005114 INIT_LIST_HEAD(&bmp->list);
James Smarta33c4f72013-03-01 16:36:00 -05005115
5116 bpl = (struct ulp_bde64 *)bmp->virt;
5117 request_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64);
5118 cmp = lpfc_alloc_bsg_buffers(phba, job->request_payload.payload_len,
5119 1, bpl, &request_nseg);
5120 if (!cmp) {
5121 rc = -ENOMEM;
5122 goto free_bmp;
5123 }
5124 lpfc_bsg_copy_data(cmp, &job->request_payload,
5125 job->request_payload.payload_len, 1);
5126
5127 bpl += request_nseg;
5128 reply_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64) - request_nseg;
5129 rmp = lpfc_alloc_bsg_buffers(phba, job->reply_payload.payload_len, 0,
5130 bpl, &reply_nseg);
5131 if (!rmp) {
5132 rc = -ENOMEM;
5133 goto free_cmp;
James Smarte2aed292010-02-26 14:15:00 -05005134 }
5135
James Smarta33c4f72013-03-01 16:36:00 -05005136 cmdiocbq = lpfc_sli_get_iocbq(phba);
5137 if (!cmdiocbq) {
5138 rc = -ENOMEM;
5139 goto free_rmp;
James Smarte2aed292010-02-26 14:15:00 -05005140 }
5141
5142 cmd = &cmdiocbq->iocb;
5143 cmd->un.genreq64.bdl.ulpIoTag32 = 0;
5144 cmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
5145 cmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
5146 cmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
5147 cmd->un.genreq64.bdl.bdeSize =
5148 (request_nseg + reply_nseg) * sizeof(struct ulp_bde64);
5149 cmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
5150 cmd->un.genreq64.w5.hcsw.Dfctl = 0;
5151 cmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CMD;
5152 cmd->un.genreq64.w5.hcsw.Type = MENLO_TRANSPORT_TYPE; /* 0xfe */
5153 cmd->ulpBdeCount = 1;
5154 cmd->ulpClass = CLASS3;
5155 cmd->ulpOwner = OWN_CHIP;
5156 cmd->ulpLe = 1; /* Limited Edition */
5157 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
5158 cmdiocbq->vport = phba->pport;
5159 /* We want the firmware to timeout before we do */
5160 cmd->ulpTimeout = MENLO_TIMEOUT - 5;
James Smarte2aed292010-02-26 14:15:00 -05005161 cmdiocbq->iocb_cmpl = lpfc_bsg_menlo_cmd_cmp;
5162 cmdiocbq->context1 = dd_data;
James Smarta33c4f72013-03-01 16:36:00 -05005163 cmdiocbq->context2 = cmp;
5164 cmdiocbq->context3 = bmp;
James Smarte2aed292010-02-26 14:15:00 -05005165 if (menlo_cmd->cmd == LPFC_BSG_VENDOR_MENLO_CMD) {
5166 cmd->ulpCommand = CMD_GEN_REQUEST64_CR;
5167 cmd->ulpPU = MENLO_PU; /* 3 */
5168 cmd->un.ulpWord[4] = MENLO_DID; /* 0x0000FC0E */
5169 cmd->ulpContext = MENLO_CONTEXT; /* 0 */
5170 } else {
5171 cmd->ulpCommand = CMD_GEN_REQUEST64_CX;
5172 cmd->ulpPU = 1;
5173 cmd->un.ulpWord[4] = 0;
5174 cmd->ulpContext = menlo_cmd->xri;
5175 }
5176
5177 dd_data->type = TYPE_MENLO;
James Smarta33c4f72013-03-01 16:36:00 -05005178 dd_data->set_job = job;
James Smarte2aed292010-02-26 14:15:00 -05005179 dd_data->context_un.menlo.cmdiocbq = cmdiocbq;
James Smarta33c4f72013-03-01 16:36:00 -05005180 dd_data->context_un.menlo.rmp = rmp;
5181 job->dd_data = dd_data;
James Smarte2aed292010-02-26 14:15:00 -05005182
5183 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq,
5184 MENLO_TIMEOUT - 5);
5185 if (rc == IOCB_SUCCESS)
5186 return 0; /* done for now */
5187
James Smarte2aed292010-02-26 14:15:00 -05005188 lpfc_sli_release_iocbq(phba, cmdiocbq);
James Smarta33c4f72013-03-01 16:36:00 -05005189
5190free_rmp:
5191 lpfc_free_bsg_buffers(phba, rmp);
5192free_cmp:
5193 lpfc_free_bsg_buffers(phba, cmp);
James Smarte2aed292010-02-26 14:15:00 -05005194free_bmp:
James Smarta33c4f72013-03-01 16:36:00 -05005195 if (bmp->virt)
5196 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
James Smarte2aed292010-02-26 14:15:00 -05005197 kfree(bmp);
5198free_dd:
5199 kfree(dd_data);
5200no_dd_data:
5201 /* make error code available to userspace */
5202 job->reply->result = rc;
5203 job->dd_data = NULL;
5204 return rc;
5205}
James Smartb6e3b9c2011-04-16 11:03:43 -04005206
James Smarte2aed292010-02-26 14:15:00 -05005207/**
James Smartf1c3b0f2009-07-19 10:01:32 -04005208 * lpfc_bsg_hst_vendor - process a vendor-specific fc_bsg_job
5209 * @job: fc_bsg_job to handle
James Smart3b5dd522010-01-26 23:10:15 -05005210 **/
James Smartf1c3b0f2009-07-19 10:01:32 -04005211static int
5212lpfc_bsg_hst_vendor(struct fc_bsg_job *job)
5213{
5214 int command = job->request->rqst_data.h_vendor.vendor_cmd[0];
James Smart4cc0e562010-01-26 23:09:48 -05005215 int rc;
James Smartf1c3b0f2009-07-19 10:01:32 -04005216
5217 switch (command) {
5218 case LPFC_BSG_VENDOR_SET_CT_EVENT:
James Smart4cc0e562010-01-26 23:09:48 -05005219 rc = lpfc_bsg_hba_set_event(job);
James Smartf1c3b0f2009-07-19 10:01:32 -04005220 break;
James Smartf1c3b0f2009-07-19 10:01:32 -04005221 case LPFC_BSG_VENDOR_GET_CT_EVENT:
James Smart4cc0e562010-01-26 23:09:48 -05005222 rc = lpfc_bsg_hba_get_event(job);
James Smartf1c3b0f2009-07-19 10:01:32 -04005223 break;
James Smart3b5dd522010-01-26 23:10:15 -05005224 case LPFC_BSG_VENDOR_SEND_MGMT_RESP:
5225 rc = lpfc_bsg_send_mgmt_rsp(job);
5226 break;
5227 case LPFC_BSG_VENDOR_DIAG_MODE:
James Smart7ad20aa2011-05-24 11:44:28 -04005228 rc = lpfc_bsg_diag_loopback_mode(job);
James Smart3b5dd522010-01-26 23:10:15 -05005229 break;
James Smart7ad20aa2011-05-24 11:44:28 -04005230 case LPFC_BSG_VENDOR_DIAG_MODE_END:
5231 rc = lpfc_sli4_bsg_diag_mode_end(job);
5232 break;
5233 case LPFC_BSG_VENDOR_DIAG_RUN_LOOPBACK:
5234 rc = lpfc_bsg_diag_loopback_run(job);
5235 break;
5236 case LPFC_BSG_VENDOR_LINK_DIAG_TEST:
5237 rc = lpfc_sli4_bsg_link_diag_test(job);
James Smart3b5dd522010-01-26 23:10:15 -05005238 break;
5239 case LPFC_BSG_VENDOR_GET_MGMT_REV:
5240 rc = lpfc_bsg_get_dfc_rev(job);
5241 break;
5242 case LPFC_BSG_VENDOR_MBOX:
5243 rc = lpfc_bsg_mbox_cmd(job);
5244 break;
James Smarte2aed292010-02-26 14:15:00 -05005245 case LPFC_BSG_VENDOR_MENLO_CMD:
5246 case LPFC_BSG_VENDOR_MENLO_DATA:
5247 rc = lpfc_menlo_cmd(job);
5248 break;
James Smartf1c3b0f2009-07-19 10:01:32 -04005249 default:
James Smart4cc0e562010-01-26 23:09:48 -05005250 rc = -EINVAL;
5251 job->reply->reply_payload_rcv_len = 0;
5252 /* make error code available to userspace */
5253 job->reply->result = rc;
5254 break;
James Smartf1c3b0f2009-07-19 10:01:32 -04005255 }
James Smart4cc0e562010-01-26 23:09:48 -05005256
5257 return rc;
James Smartf1c3b0f2009-07-19 10:01:32 -04005258}
5259
5260/**
5261 * lpfc_bsg_request - handle a bsg request from the FC transport
5262 * @job: fc_bsg_job to handle
James Smart3b5dd522010-01-26 23:10:15 -05005263 **/
James Smartf1c3b0f2009-07-19 10:01:32 -04005264int
5265lpfc_bsg_request(struct fc_bsg_job *job)
5266{
5267 uint32_t msgcode;
James Smart4cc0e562010-01-26 23:09:48 -05005268 int rc;
James Smartf1c3b0f2009-07-19 10:01:32 -04005269
5270 msgcode = job->request->msgcode;
James Smartf1c3b0f2009-07-19 10:01:32 -04005271 switch (msgcode) {
5272 case FC_BSG_HST_VENDOR:
5273 rc = lpfc_bsg_hst_vendor(job);
5274 break;
5275 case FC_BSG_RPT_ELS:
5276 rc = lpfc_bsg_rport_els(job);
5277 break;
5278 case FC_BSG_RPT_CT:
James Smart4cc0e562010-01-26 23:09:48 -05005279 rc = lpfc_bsg_send_mgmt_cmd(job);
James Smartf1c3b0f2009-07-19 10:01:32 -04005280 break;
5281 default:
James Smart4cc0e562010-01-26 23:09:48 -05005282 rc = -EINVAL;
5283 job->reply->reply_payload_rcv_len = 0;
5284 /* make error code available to userspace */
5285 job->reply->result = rc;
James Smartf1c3b0f2009-07-19 10:01:32 -04005286 break;
5287 }
5288
5289 return rc;
5290}
5291
5292/**
5293 * lpfc_bsg_timeout - handle timeout of a bsg request from the FC transport
5294 * @job: fc_bsg_job that has timed out
5295 *
5296 * This function just aborts the job's IOCB. The aborted IOCB will return to
5297 * the waiting function which will handle passing the error back to userspace
James Smart3b5dd522010-01-26 23:10:15 -05005298 **/
James Smartf1c3b0f2009-07-19 10:01:32 -04005299int
5300lpfc_bsg_timeout(struct fc_bsg_job *job)
5301{
5302 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
5303 struct lpfc_hba *phba = vport->phba;
James Smart4cc0e562010-01-26 23:09:48 -05005304 struct lpfc_iocbq *cmdiocb;
James Smartf1c3b0f2009-07-19 10:01:32 -04005305 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart4cc0e562010-01-26 23:09:48 -05005306 struct bsg_job_data *dd_data;
5307 unsigned long flags;
James Smarta33c4f72013-03-01 16:36:00 -05005308 int rc = 0;
5309 LIST_HEAD(completions);
5310 struct lpfc_iocbq *check_iocb, *next_iocb;
5311
5312 /* if job's driver data is NULL, the command completed or is in the
5313 * the process of completing. In this case, return status to request
5314 * so the timeout is retried. This avoids double completion issues
5315 * and the request will be pulled off the timer queue when the
5316 * command's completion handler executes. Otherwise, prevent the
5317 * command's completion handler from executing the job done callback
5318 * and continue processing to abort the outstanding the command.
5319 */
James Smartf1c3b0f2009-07-19 10:01:32 -04005320
James Smart4cc0e562010-01-26 23:09:48 -05005321 spin_lock_irqsave(&phba->ct_ev_lock, flags);
5322 dd_data = (struct bsg_job_data *)job->dd_data;
James Smarta33c4f72013-03-01 16:36:00 -05005323 if (dd_data) {
5324 dd_data->set_job = NULL;
5325 job->dd_data = NULL;
5326 } else {
James Smart4cc0e562010-01-26 23:09:48 -05005327 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smarta33c4f72013-03-01 16:36:00 -05005328 return -EAGAIN;
James Smart4cc0e562010-01-26 23:09:48 -05005329 }
5330
5331 switch (dd_data->type) {
5332 case TYPE_IOCB:
James Smarta33c4f72013-03-01 16:36:00 -05005333 /* Check to see if IOCB was issued to the port or not. If not,
5334 * remove it from the txq queue and call cancel iocbs.
5335 * Otherwise, call abort iotag
James Smart4cc0e562010-01-26 23:09:48 -05005336 */
James Smarta33c4f72013-03-01 16:36:00 -05005337 cmdiocb = dd_data->context_un.iocb.cmdiocbq;
James Smartb5a9b2d2013-09-06 12:19:45 -04005338 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5339
5340 spin_lock_irqsave(&phba->hbalock, flags);
5341 /* make sure the I/O abort window is still open */
James Smart1b8d11a2013-09-06 12:20:51 -04005342 if (!(cmdiocb->iocb_flag & LPFC_IO_CMD_OUTSTANDING)) {
James Smartb5a9b2d2013-09-06 12:19:45 -04005343 spin_unlock_irqrestore(&phba->hbalock, flags);
5344 return -EAGAIN;
5345 }
James Smarta33c4f72013-03-01 16:36:00 -05005346 list_for_each_entry_safe(check_iocb, next_iocb, &pring->txq,
5347 list) {
5348 if (check_iocb == cmdiocb) {
5349 list_move_tail(&check_iocb->list, &completions);
5350 break;
5351 }
5352 }
5353 if (list_empty(&completions))
5354 lpfc_sli_issue_abort_iotag(phba, pring, cmdiocb);
James Smartb5a9b2d2013-09-06 12:19:45 -04005355 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarta33c4f72013-03-01 16:36:00 -05005356 if (!list_empty(&completions)) {
5357 lpfc_sli_cancel_iocbs(phba, &completions,
5358 IOSTAT_LOCAL_REJECT,
5359 IOERR_SLI_ABORTED);
5360 }
James Smart4cc0e562010-01-26 23:09:48 -05005361 break;
James Smarta33c4f72013-03-01 16:36:00 -05005362
5363 case TYPE_EVT:
James Smart3b5dd522010-01-26 23:10:15 -05005364 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smarta33c4f72013-03-01 16:36:00 -05005365 break;
5366
5367 case TYPE_MBOX:
5368 /* Update the ext buf ctx state if needed */
5369
James Smart7ad20aa2011-05-24 11:44:28 -04005370 if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_PORT)
5371 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_ABTS;
James Smarta33c4f72013-03-01 16:36:00 -05005372 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smart3b5dd522010-01-26 23:10:15 -05005373 break;
James Smarte2aed292010-02-26 14:15:00 -05005374 case TYPE_MENLO:
James Smarta33c4f72013-03-01 16:36:00 -05005375 /* Check to see if IOCB was issued to the port or not. If not,
5376 * remove it from the txq queue and call cancel iocbs.
5377 * Otherwise, call abort iotag.
5378 */
James Smarta33c4f72013-03-01 16:36:00 -05005379 cmdiocb = dd_data->context_un.menlo.cmdiocbq;
James Smartb5a9b2d2013-09-06 12:19:45 -04005380 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5381
5382 spin_lock_irqsave(&phba->hbalock, flags);
James Smarta33c4f72013-03-01 16:36:00 -05005383 list_for_each_entry_safe(check_iocb, next_iocb, &pring->txq,
5384 list) {
5385 if (check_iocb == cmdiocb) {
5386 list_move_tail(&check_iocb->list, &completions);
5387 break;
5388 }
5389 }
5390 if (list_empty(&completions))
5391 lpfc_sli_issue_abort_iotag(phba, pring, cmdiocb);
James Smartb5a9b2d2013-09-06 12:19:45 -04005392 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarta33c4f72013-03-01 16:36:00 -05005393 if (!list_empty(&completions)) {
5394 lpfc_sli_cancel_iocbs(phba, &completions,
5395 IOSTAT_LOCAL_REJECT,
5396 IOERR_SLI_ABORTED);
5397 }
James Smarte2aed292010-02-26 14:15:00 -05005398 break;
James Smart4cc0e562010-01-26 23:09:48 -05005399 default:
5400 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5401 break;
5402 }
James Smartf1c3b0f2009-07-19 10:01:32 -04005403
James Smart4cc0e562010-01-26 23:09:48 -05005404 /* scsi transport fc fc_bsg_job_timeout expects a zero return code,
5405 * otherwise an error message will be displayed on the console
5406 * so always return success (zero)
5407 */
James Smarta33c4f72013-03-01 16:36:00 -05005408 return rc;
James Smartf1c3b0f2009-07-19 10:01:32 -04005409}