blob: bc270639c1c3a6c1f3b83c83be48ae99acaabcb5 [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 Smart92c13f22013-05-31 17:05:45 -04004 * Copyright (C) 2009-2013 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
320 iocb = &dd_data->context_un.iocb;
James Smarta33c4f72013-03-01 16:36:00 -0500321 ndlp = iocb->ndlp;
322 rmp = iocb->rmp;
323 cmp = cmdiocbq->context2;
324 bmp = cmdiocbq->context3;
James Smart4cc0e562010-01-26 23:09:48 -0500325 rsp = &rspiocbq->iocb;
James Smart4cc0e562010-01-26 23:09:48 -0500326
James Smarta33c4f72013-03-01 16:36:00 -0500327 /* Copy the completed data or set the error status */
James Smart4cc0e562010-01-26 23:09:48 -0500328
James Smarta33c4f72013-03-01 16:36:00 -0500329 if (job) {
330 if (rsp->ulpStatus) {
331 if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
332 switch (rsp->un.ulpWord[4] & IOERR_PARAM_MASK) {
333 case IOERR_SEQUENCE_TIMEOUT:
334 rc = -ETIMEDOUT;
335 break;
336 case IOERR_INVALID_RPI:
337 rc = -EFAULT;
338 break;
339 default:
340 rc = -EACCES;
341 break;
342 }
343 } else {
James Smart4cc0e562010-01-26 23:09:48 -0500344 rc = -EACCES;
James Smart4cc0e562010-01-26 23:09:48 -0500345 }
James Smarta33c4f72013-03-01 16:36:00 -0500346 } else {
347 rsp_size = rsp->un.genreq64.bdl.bdeSize;
348 job->reply->reply_payload_rcv_len =
349 lpfc_bsg_copy_data(rmp, &job->reply_payload,
350 rsp_size, 0);
351 }
352 }
James Smart4cc0e562010-01-26 23:09:48 -0500353
James Smarta33c4f72013-03-01 16:36:00 -0500354 lpfc_free_bsg_buffers(phba, cmp);
355 lpfc_free_bsg_buffers(phba, rmp);
James Smart4cc0e562010-01-26 23:09:48 -0500356 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
James Smarta33c4f72013-03-01 16:36:00 -0500357 kfree(bmp);
James Smart4cc0e562010-01-26 23:09:48 -0500358 lpfc_sli_release_iocbq(phba, cmdiocbq);
359 lpfc_nlp_put(ndlp);
James Smart4cc0e562010-01-26 23:09:48 -0500360 kfree(dd_data);
James Smarta33c4f72013-03-01 16:36:00 -0500361
362 /* Complete the job if the job is still active */
363
364 if (job) {
365 job->reply->result = rc;
366 job->job_done(job);
367 }
James Smart4cc0e562010-01-26 23:09:48 -0500368 return;
369}
370
371/**
372 * lpfc_bsg_send_mgmt_cmd - send a CT command from a bsg request
James Smartf1c3b0f2009-07-19 10:01:32 -0400373 * @job: fc_bsg_job to handle
James Smart3b5dd522010-01-26 23:10:15 -0500374 **/
James Smartf1c3b0f2009-07-19 10:01:32 -0400375static int
James Smart4cc0e562010-01-26 23:09:48 -0500376lpfc_bsg_send_mgmt_cmd(struct fc_bsg_job *job)
James Smartf1c3b0f2009-07-19 10:01:32 -0400377{
James Smartf1c3b0f2009-07-19 10:01:32 -0400378 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
379 struct lpfc_hba *phba = vport->phba;
380 struct lpfc_rport_data *rdata = job->rport->dd_data;
381 struct lpfc_nodelist *ndlp = rdata->pnode;
382 struct ulp_bde64 *bpl = NULL;
383 uint32_t timeout;
384 struct lpfc_iocbq *cmdiocbq = NULL;
James Smartf1c3b0f2009-07-19 10:01:32 -0400385 IOCB_t *cmd;
James Smarta33c4f72013-03-01 16:36:00 -0500386 struct lpfc_dmabuf *bmp = NULL, *cmp = NULL, *rmp = NULL;
James Smartf1c3b0f2009-07-19 10:01:32 -0400387 int request_nseg;
388 int reply_nseg;
James Smart4cc0e562010-01-26 23:09:48 -0500389 struct bsg_job_data *dd_data;
390 uint32_t creg_val;
James Smartf1c3b0f2009-07-19 10:01:32 -0400391 int rc = 0;
James Smartd439d282010-09-29 11:18:45 -0400392 int iocb_stat;
James Smartf1c3b0f2009-07-19 10:01:32 -0400393
394 /* in case no data is transferred */
395 job->reply->reply_payload_rcv_len = 0;
396
James Smart4cc0e562010-01-26 23:09:48 -0500397 /* allocate our bsg tracking structure */
398 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
399 if (!dd_data) {
400 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
401 "2733 Failed allocation of dd_data\n");
402 rc = -ENOMEM;
403 goto no_dd_data;
404 }
405
James Smartf1c3b0f2009-07-19 10:01:32 -0400406 if (!lpfc_nlp_get(ndlp)) {
James Smart4cc0e562010-01-26 23:09:48 -0500407 rc = -ENODEV;
408 goto no_ndlp;
409 }
410
James Smartf1c3b0f2009-07-19 10:01:32 -0400411 if (ndlp->nlp_flag & NLP_ELS_SND_MASK) {
412 rc = -ENODEV;
James Smarta33c4f72013-03-01 16:36:00 -0500413 goto free_ndlp;
James Smartf1c3b0f2009-07-19 10:01:32 -0400414 }
415
James Smartf1c3b0f2009-07-19 10:01:32 -0400416 cmdiocbq = lpfc_sli_get_iocbq(phba);
417 if (!cmdiocbq) {
418 rc = -ENOMEM;
James Smarta33c4f72013-03-01 16:36:00 -0500419 goto free_ndlp;
James Smartf1c3b0f2009-07-19 10:01:32 -0400420 }
James Smartf1c3b0f2009-07-19 10:01:32 -0400421
James Smart4cc0e562010-01-26 23:09:48 -0500422 cmd = &cmdiocbq->iocb;
James Smarta33c4f72013-03-01 16:36:00 -0500423
424 bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
425 if (!bmp) {
James Smartf1c3b0f2009-07-19 10:01:32 -0400426 rc = -ENOMEM;
James Smartbe858b62010-12-15 17:57:20 -0500427 goto free_cmdiocbq;
James Smartf1c3b0f2009-07-19 10:01:32 -0400428 }
James Smarta33c4f72013-03-01 16:36:00 -0500429 bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
430 if (!bmp->virt) {
431 rc = -ENOMEM;
432 goto free_bmp;
James Smartf1c3b0f2009-07-19 10:01:32 -0400433 }
434
James Smarta33c4f72013-03-01 16:36:00 -0500435 INIT_LIST_HEAD(&bmp->list);
436
437 bpl = (struct ulp_bde64 *) bmp->virt;
438 request_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64);
439 cmp = lpfc_alloc_bsg_buffers(phba, job->request_payload.payload_len,
440 1, bpl, &request_nseg);
441 if (!cmp) {
442 rc = -ENOMEM;
443 goto free_bmp;
444 }
445 lpfc_bsg_copy_data(cmp, &job->request_payload,
446 job->request_payload.payload_len, 1);
447
448 bpl += request_nseg;
449 reply_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64) - request_nseg;
450 rmp = lpfc_alloc_bsg_buffers(phba, job->reply_payload.payload_len, 0,
451 bpl, &reply_nseg);
452 if (!rmp) {
453 rc = -ENOMEM;
454 goto free_cmp;
James Smartf1c3b0f2009-07-19 10:01:32 -0400455 }
456
457 cmd->un.genreq64.bdl.ulpIoTag32 = 0;
458 cmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
459 cmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
460 cmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
461 cmd->un.genreq64.bdl.bdeSize =
462 (request_nseg + reply_nseg) * sizeof(struct ulp_bde64);
463 cmd->ulpCommand = CMD_GEN_REQUEST64_CR;
464 cmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
465 cmd->un.genreq64.w5.hcsw.Dfctl = 0;
James Smart6a9c52c2009-10-02 15:16:51 -0400466 cmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
467 cmd->un.genreq64.w5.hcsw.Type = FC_TYPE_CT;
James Smartf1c3b0f2009-07-19 10:01:32 -0400468 cmd->ulpBdeCount = 1;
469 cmd->ulpLe = 1;
470 cmd->ulpClass = CLASS3;
471 cmd->ulpContext = ndlp->nlp_rpi;
James Smart6d368e52011-05-24 11:44:12 -0400472 if (phba->sli_rev == LPFC_SLI_REV4)
473 cmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smartf1c3b0f2009-07-19 10:01:32 -0400474 cmd->ulpOwner = OWN_CHIP;
475 cmdiocbq->vport = phba->pport;
James Smart4cc0e562010-01-26 23:09:48 -0500476 cmdiocbq->context3 = bmp;
James Smartf1c3b0f2009-07-19 10:01:32 -0400477 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
James Smartf1c3b0f2009-07-19 10:01:32 -0400478 timeout = phba->fc_ratov * 2;
James Smart4cc0e562010-01-26 23:09:48 -0500479 cmd->ulpTimeout = timeout;
James Smartf1c3b0f2009-07-19 10:01:32 -0400480
James Smart4cc0e562010-01-26 23:09:48 -0500481 cmdiocbq->iocb_cmpl = lpfc_bsg_send_mgmt_cmd_cmp;
James Smarta33c4f72013-03-01 16:36:00 -0500482 cmdiocbq->context1 = dd_data;
483 cmdiocbq->context2 = cmp;
484 cmdiocbq->context3 = bmp;
James Smartd5ce53b2013-04-17 20:17:26 -0400485 cmdiocbq->context_un.ndlp = ndlp;
James Smart4cc0e562010-01-26 23:09:48 -0500486 dd_data->type = TYPE_IOCB;
James Smarta33c4f72013-03-01 16:36:00 -0500487 dd_data->set_job = job;
James Smart4cc0e562010-01-26 23:09:48 -0500488 dd_data->context_un.iocb.cmdiocbq = cmdiocbq;
James Smarta33c4f72013-03-01 16:36:00 -0500489 dd_data->context_un.iocb.ndlp = ndlp;
490 dd_data->context_un.iocb.rmp = rmp;
491 job->dd_data = dd_data;
James Smartf1c3b0f2009-07-19 10:01:32 -0400492
James Smart4cc0e562010-01-26 23:09:48 -0500493 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -0500494 if (lpfc_readl(phba->HCregaddr, &creg_val)) {
495 rc = -EIO ;
James Smarta33c4f72013-03-01 16:36:00 -0500496 goto free_rmp;
James Smart9940b972011-03-11 16:06:12 -0500497 }
James Smart4cc0e562010-01-26 23:09:48 -0500498 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
499 writel(creg_val, phba->HCregaddr);
500 readl(phba->HCregaddr); /* flush */
James Smartf1c3b0f2009-07-19 10:01:32 -0400501 }
502
James Smartd439d282010-09-29 11:18:45 -0400503 iocb_stat = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq, 0);
504 if (iocb_stat == IOCB_SUCCESS)
James Smart4cc0e562010-01-26 23:09:48 -0500505 return 0; /* done for now */
James Smartd439d282010-09-29 11:18:45 -0400506 else if (iocb_stat == IOCB_BUSY)
507 rc = -EAGAIN;
James Smart2a9bf3d2010-06-07 15:24:45 -0400508 else
James Smartd439d282010-09-29 11:18:45 -0400509 rc = -EIO;
James Smart2a9bf3d2010-06-07 15:24:45 -0400510
James Smart4cc0e562010-01-26 23:09:48 -0500511 /* iocb failed so cleanup */
James Smartf1c3b0f2009-07-19 10:01:32 -0400512
James Smarta33c4f72013-03-01 16:36:00 -0500513free_rmp:
514 lpfc_free_bsg_buffers(phba, rmp);
515free_cmp:
516 lpfc_free_bsg_buffers(phba, cmp);
517free_bmp:
518 if (bmp->virt)
519 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
520 kfree(bmp);
James Smartf1c3b0f2009-07-19 10:01:32 -0400521free_cmdiocbq:
522 lpfc_sli_release_iocbq(phba, cmdiocbq);
James Smart4cc0e562010-01-26 23:09:48 -0500523free_ndlp:
James Smartf1c3b0f2009-07-19 10:01:32 -0400524 lpfc_nlp_put(ndlp);
James Smart4cc0e562010-01-26 23:09:48 -0500525no_ndlp:
526 kfree(dd_data);
527no_dd_data:
James Smartf1c3b0f2009-07-19 10:01:32 -0400528 /* make error code available to userspace */
529 job->reply->result = rc;
James Smart4cc0e562010-01-26 23:09:48 -0500530 job->dd_data = NULL;
531 return rc;
532}
533
534/**
535 * lpfc_bsg_rport_els_cmp - lpfc_bsg_rport_els's completion handler
536 * @phba: Pointer to HBA context object.
537 * @cmdiocbq: Pointer to command iocb.
538 * @rspiocbq: Pointer to response iocb.
539 *
540 * This function is the completion handler for iocbs issued using
541 * lpfc_bsg_rport_els_cmp function. This function is called by the
542 * ring event handler function without any lock held. This function
543 * can be called from both worker thread context and interrupt
544 * context. This function also can be called from other thread which
545 * cleans up the SLI layer objects.
James Smart3b5dd522010-01-26 23:10:15 -0500546 * This function copies the contents of the response iocb to the
James Smart4cc0e562010-01-26 23:09:48 -0500547 * response iocb memory object provided by the caller of
548 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
549 * sleeps for the iocb completion.
550 **/
551static void
552lpfc_bsg_rport_els_cmp(struct lpfc_hba *phba,
553 struct lpfc_iocbq *cmdiocbq,
554 struct lpfc_iocbq *rspiocbq)
555{
556 struct bsg_job_data *dd_data;
557 struct fc_bsg_job *job;
558 IOCB_t *rsp;
559 struct lpfc_nodelist *ndlp;
James Smarta33c4f72013-03-01 16:36:00 -0500560 struct lpfc_dmabuf *pcmd = NULL, *prsp = NULL;
James Smart4cc0e562010-01-26 23:09:48 -0500561 struct fc_bsg_ctels_reply *els_reply;
562 uint8_t *rjt_data;
563 unsigned long flags;
James Smarta33c4f72013-03-01 16:36:00 -0500564 unsigned int rsp_size;
James Smart4cc0e562010-01-26 23:09:48 -0500565 int rc = 0;
566
James Smart4cc0e562010-01-26 23:09:48 -0500567 dd_data = cmdiocbq->context1;
James Smarta33c4f72013-03-01 16:36:00 -0500568 ndlp = dd_data->context_un.iocb.ndlp;
569 cmdiocbq->context1 = ndlp;
570
571 /* Determine if job has been aborted */
572 spin_lock_irqsave(&phba->ct_ev_lock, flags);
573 job = dd_data->set_job;
574 if (job) {
575 /* Prevent timeout handling from trying to abort job */
576 job->dd_data = NULL;
577 }
578 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
579
580 rsp = &rspiocbq->iocb;
581 pcmd = (struct lpfc_dmabuf *)cmdiocbq->context2;
582 prsp = (struct lpfc_dmabuf *)pcmd->list.next;
583
584 /* Copy the completed job data or determine the job status if job is
585 * still active
586 */
587
588 if (job) {
589 if (rsp->ulpStatus == IOSTAT_SUCCESS) {
590 rsp_size = rsp->un.elsreq64.bdl.bdeSize;
591 job->reply->reply_payload_rcv_len =
592 sg_copy_from_buffer(job->reply_payload.sg_list,
593 job->reply_payload.sg_cnt,
594 prsp->virt,
595 rsp_size);
596 } else if (rsp->ulpStatus == IOSTAT_LS_RJT) {
597 job->reply->reply_payload_rcv_len =
598 sizeof(struct fc_bsg_ctels_reply);
599 /* LS_RJT data returned in word 4 */
600 rjt_data = (uint8_t *)&rsp->un.ulpWord[4];
601 els_reply = &job->reply->reply_data.ctels_reply;
602 els_reply->status = FC_CTELS_STATUS_REJECT;
603 els_reply->rjt_data.action = rjt_data[3];
604 els_reply->rjt_data.reason_code = rjt_data[2];
605 els_reply->rjt_data.reason_explanation = rjt_data[1];
606 els_reply->rjt_data.vendor_unique = rjt_data[0];
607 } else {
608 rc = -EIO;
609 }
James Smart4cc0e562010-01-26 23:09:48 -0500610 }
611
James Smart4cc0e562010-01-26 23:09:48 -0500612 lpfc_nlp_put(ndlp);
James Smarta33c4f72013-03-01 16:36:00 -0500613 lpfc_els_free_iocb(phba, cmdiocbq);
James Smart4cc0e562010-01-26 23:09:48 -0500614 kfree(dd_data);
James Smarta33c4f72013-03-01 16:36:00 -0500615
616 /* Complete the job if the job is still active */
617
618 if (job) {
619 job->reply->result = rc;
620 job->job_done(job);
621 }
James Smart4cc0e562010-01-26 23:09:48 -0500622 return;
James Smartf1c3b0f2009-07-19 10:01:32 -0400623}
624
625/**
626 * lpfc_bsg_rport_els - send an ELS command from a bsg request
627 * @job: fc_bsg_job to handle
James Smart3b5dd522010-01-26 23:10:15 -0500628 **/
James Smartf1c3b0f2009-07-19 10:01:32 -0400629static int
630lpfc_bsg_rport_els(struct fc_bsg_job *job)
631{
632 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
633 struct lpfc_hba *phba = vport->phba;
634 struct lpfc_rport_data *rdata = job->rport->dd_data;
635 struct lpfc_nodelist *ndlp = rdata->pnode;
James Smartf1c3b0f2009-07-19 10:01:32 -0400636 uint32_t elscmd;
637 uint32_t cmdsize;
638 uint32_t rspsize;
James Smartf1c3b0f2009-07-19 10:01:32 -0400639 struct lpfc_iocbq *cmdiocbq;
James Smartf1c3b0f2009-07-19 10:01:32 -0400640 uint16_t rpi = 0;
James Smart4cc0e562010-01-26 23:09:48 -0500641 struct bsg_job_data *dd_data;
642 uint32_t creg_val;
James Smartf1c3b0f2009-07-19 10:01:32 -0400643 int rc = 0;
644
645 /* in case no data is transferred */
646 job->reply->reply_payload_rcv_len = 0;
647
James Smarta33c4f72013-03-01 16:36:00 -0500648 /* verify the els command is not greater than the
649 * maximum ELS transfer size.
650 */
651
652 if (job->request_payload.payload_len > FCELSSIZE) {
653 rc = -EINVAL;
654 goto no_dd_data;
655 }
656
James Smart4cc0e562010-01-26 23:09:48 -0500657 /* allocate our bsg tracking structure */
658 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
659 if (!dd_data) {
660 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
661 "2735 Failed allocation of dd_data\n");
662 rc = -ENOMEM;
663 goto no_dd_data;
664 }
665
James Smarta33c4f72013-03-01 16:36:00 -0500666 elscmd = job->request->rqst_data.r_els.els_code;
667 cmdsize = job->request_payload.payload_len;
668 rspsize = job->reply_payload.payload_len;
669
James Smartf1c3b0f2009-07-19 10:01:32 -0400670 if (!lpfc_nlp_get(ndlp)) {
671 rc = -ENODEV;
James Smart4cc0e562010-01-26 23:09:48 -0500672 goto free_dd_data;
James Smartf1c3b0f2009-07-19 10:01:32 -0400673 }
674
James Smarta33c4f72013-03-01 16:36:00 -0500675 /* We will use the allocated dma buffers by prep els iocb for command
676 * and response to ensure if the job times out and the request is freed,
677 * we won't be dma into memory that is no longer allocated to for the
678 * request.
679 */
James Smartf1c3b0f2009-07-19 10:01:32 -0400680
James Smart4cc0e562010-01-26 23:09:48 -0500681 cmdiocbq = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp,
James Smartf1c3b0f2009-07-19 10:01:32 -0400682 ndlp->nlp_DID, elscmd);
James Smartf1c3b0f2009-07-19 10:01:32 -0400683 if (!cmdiocbq) {
James Smart4cc0e562010-01-26 23:09:48 -0500684 rc = -EIO;
James Smarta33c4f72013-03-01 16:36:00 -0500685 goto release_ndlp;
James Smartf1c3b0f2009-07-19 10:01:32 -0400686 }
687
James Smarta33c4f72013-03-01 16:36:00 -0500688 rpi = ndlp->nlp_rpi;
James Smartf1c3b0f2009-07-19 10:01:32 -0400689
James Smarta33c4f72013-03-01 16:36:00 -0500690 /* Transfer the request payload to allocated command dma buffer */
James Smartf1c3b0f2009-07-19 10:01:32 -0400691
James Smarta33c4f72013-03-01 16:36:00 -0500692 sg_copy_to_buffer(job->request_payload.sg_list,
693 job->request_payload.sg_cnt,
694 ((struct lpfc_dmabuf *)cmdiocbq->context2)->virt,
695 cmdsize);
James Smartf1c3b0f2009-07-19 10:01:32 -0400696
James Smart3ef6d242012-01-18 16:23:48 -0500697 if (phba->sli_rev == LPFC_SLI_REV4)
698 cmdiocbq->iocb.ulpContext = phba->sli4_hba.rpi_ids[rpi];
699 else
700 cmdiocbq->iocb.ulpContext = rpi;
James Smartf1c3b0f2009-07-19 10:01:32 -0400701 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
James Smart4cc0e562010-01-26 23:09:48 -0500702 cmdiocbq->context1 = dd_data;
James Smart93d1379e2012-05-09 21:19:34 -0400703 cmdiocbq->context_un.ndlp = ndlp;
James Smarta33c4f72013-03-01 16:36:00 -0500704 cmdiocbq->iocb_cmpl = lpfc_bsg_rport_els_cmp;
James Smart4cc0e562010-01-26 23:09:48 -0500705 dd_data->type = TYPE_IOCB;
James Smarta33c4f72013-03-01 16:36:00 -0500706 dd_data->set_job = job;
James Smart4cc0e562010-01-26 23:09:48 -0500707 dd_data->context_un.iocb.cmdiocbq = cmdiocbq;
James Smart4cc0e562010-01-26 23:09:48 -0500708 dd_data->context_un.iocb.ndlp = ndlp;
James Smarta33c4f72013-03-01 16:36:00 -0500709 dd_data->context_un.iocb.rmp = NULL;
710 job->dd_data = dd_data;
James Smartf1c3b0f2009-07-19 10:01:32 -0400711
James Smart4cc0e562010-01-26 23:09:48 -0500712 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -0500713 if (lpfc_readl(phba->HCregaddr, &creg_val)) {
714 rc = -EIO;
715 goto linkdown_err;
716 }
James Smart4cc0e562010-01-26 23:09:48 -0500717 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
718 writel(creg_val, phba->HCregaddr);
719 readl(phba->HCregaddr); /* flush */
James Smartf1c3b0f2009-07-19 10:01:32 -0400720 }
James Smarta33c4f72013-03-01 16:36:00 -0500721
James Smart4cc0e562010-01-26 23:09:48 -0500722 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq, 0);
James Smarta33c4f72013-03-01 16:36:00 -0500723
James Smart4cc0e562010-01-26 23:09:48 -0500724 if (rc == IOCB_SUCCESS)
725 return 0; /* done for now */
James Smart2a9bf3d2010-06-07 15:24:45 -0400726 else if (rc == IOCB_BUSY)
James Smartd439d282010-09-29 11:18:45 -0400727 rc = -EAGAIN;
James Smart2a9bf3d2010-06-07 15:24:45 -0400728 else
James Smartd439d282010-09-29 11:18:45 -0400729 rc = -EIO;
James Smartf1c3b0f2009-07-19 10:01:32 -0400730
James Smart9940b972011-03-11 16:06:12 -0500731linkdown_err:
James Smartf1c3b0f2009-07-19 10:01:32 -0400732
James Smarta33c4f72013-03-01 16:36:00 -0500733 cmdiocbq->context1 = ndlp;
734 lpfc_els_free_iocb(phba, cmdiocbq);
James Smartf1c3b0f2009-07-19 10:01:32 -0400735
James Smarta33c4f72013-03-01 16:36:00 -0500736release_ndlp:
737 lpfc_nlp_put(ndlp);
James Smartf1c3b0f2009-07-19 10:01:32 -0400738
James Smart4cc0e562010-01-26 23:09:48 -0500739free_dd_data:
740 kfree(dd_data);
741
742no_dd_data:
James Smartf1c3b0f2009-07-19 10:01:32 -0400743 /* make error code available to userspace */
744 job->reply->result = rc;
James Smart4cc0e562010-01-26 23:09:48 -0500745 job->dd_data = NULL;
746 return rc;
James Smartf1c3b0f2009-07-19 10:01:32 -0400747}
748
James Smart3b5dd522010-01-26 23:10:15 -0500749/**
750 * lpfc_bsg_event_free - frees an allocated event structure
751 * @kref: Pointer to a kref.
752 *
753 * Called from kref_put. Back cast the kref into an event structure address.
754 * Free any events to get, delete associated nodes, free any events to see,
755 * free any data then free the event itself.
756 **/
James Smartf1c3b0f2009-07-19 10:01:32 -0400757static void
James Smart4cc0e562010-01-26 23:09:48 -0500758lpfc_bsg_event_free(struct kref *kref)
James Smartf1c3b0f2009-07-19 10:01:32 -0400759{
James Smart4cc0e562010-01-26 23:09:48 -0500760 struct lpfc_bsg_event *evt = container_of(kref, struct lpfc_bsg_event,
761 kref);
James Smartf1c3b0f2009-07-19 10:01:32 -0400762 struct event_data *ed;
763
764 list_del(&evt->node);
765
766 while (!list_empty(&evt->events_to_get)) {
767 ed = list_entry(evt->events_to_get.next, typeof(*ed), node);
768 list_del(&ed->node);
769 kfree(ed->data);
770 kfree(ed);
771 }
772
773 while (!list_empty(&evt->events_to_see)) {
774 ed = list_entry(evt->events_to_see.next, typeof(*ed), node);
775 list_del(&ed->node);
776 kfree(ed->data);
777 kfree(ed);
778 }
779
James Smarta33c4f72013-03-01 16:36:00 -0500780 kfree(evt->dd_data);
James Smartf1c3b0f2009-07-19 10:01:32 -0400781 kfree(evt);
782}
783
James Smart3b5dd522010-01-26 23:10:15 -0500784/**
785 * lpfc_bsg_event_ref - increments the kref for an event
786 * @evt: Pointer to an event structure.
787 **/
James Smartf1c3b0f2009-07-19 10:01:32 -0400788static inline void
James Smart4cc0e562010-01-26 23:09:48 -0500789lpfc_bsg_event_ref(struct lpfc_bsg_event *evt)
James Smartf1c3b0f2009-07-19 10:01:32 -0400790{
James Smart4cc0e562010-01-26 23:09:48 -0500791 kref_get(&evt->kref);
James Smartf1c3b0f2009-07-19 10:01:32 -0400792}
793
James Smart3b5dd522010-01-26 23:10:15 -0500794/**
795 * lpfc_bsg_event_unref - Uses kref_put to free an event structure
796 * @evt: Pointer to an event structure.
797 **/
James Smartf1c3b0f2009-07-19 10:01:32 -0400798static inline void
James Smart4cc0e562010-01-26 23:09:48 -0500799lpfc_bsg_event_unref(struct lpfc_bsg_event *evt)
James Smartf1c3b0f2009-07-19 10:01:32 -0400800{
James Smart4cc0e562010-01-26 23:09:48 -0500801 kref_put(&evt->kref, lpfc_bsg_event_free);
James Smartf1c3b0f2009-07-19 10:01:32 -0400802}
803
James Smart3b5dd522010-01-26 23:10:15 -0500804/**
805 * lpfc_bsg_event_new - allocate and initialize a event structure
806 * @ev_mask: Mask of events.
807 * @ev_reg_id: Event reg id.
808 * @ev_req_id: Event request id.
809 **/
James Smart4cc0e562010-01-26 23:09:48 -0500810static struct lpfc_bsg_event *
811lpfc_bsg_event_new(uint32_t ev_mask, int ev_reg_id, uint32_t ev_req_id)
812{
813 struct lpfc_bsg_event *evt = kzalloc(sizeof(*evt), GFP_KERNEL);
James Smartf1c3b0f2009-07-19 10:01:32 -0400814
James Smart4cc0e562010-01-26 23:09:48 -0500815 if (!evt)
816 return NULL;
817
818 INIT_LIST_HEAD(&evt->events_to_get);
819 INIT_LIST_HEAD(&evt->events_to_see);
820 evt->type_mask = ev_mask;
821 evt->req_id = ev_req_id;
822 evt->reg_id = ev_reg_id;
823 evt->wait_time_stamp = jiffies;
James Smarta33c4f72013-03-01 16:36:00 -0500824 evt->dd_data = NULL;
James Smart4cc0e562010-01-26 23:09:48 -0500825 init_waitqueue_head(&evt->wq);
826 kref_init(&evt->kref);
827 return evt;
828}
829
James Smart3b5dd522010-01-26 23:10:15 -0500830/**
831 * diag_cmd_data_free - Frees an lpfc dma buffer extension
832 * @phba: Pointer to HBA context object.
833 * @mlist: Pointer to an lpfc dma buffer extension.
834 **/
James Smart4cc0e562010-01-26 23:09:48 -0500835static int
James Smart3b5dd522010-01-26 23:10:15 -0500836diag_cmd_data_free(struct lpfc_hba *phba, struct lpfc_dmabufext *mlist)
James Smart4cc0e562010-01-26 23:09:48 -0500837{
838 struct lpfc_dmabufext *mlast;
839 struct pci_dev *pcidev;
840 struct list_head head, *curr, *next;
841
842 if ((!mlist) || (!lpfc_is_link_up(phba) &&
843 (phba->link_flag & LS_LOOPBACK_MODE))) {
844 return 0;
845 }
846
847 pcidev = phba->pcidev;
848 list_add_tail(&head, &mlist->dma.list);
849
850 list_for_each_safe(curr, next, &head) {
851 mlast = list_entry(curr, struct lpfc_dmabufext , dma.list);
852 if (mlast->dma.virt)
853 dma_free_coherent(&pcidev->dev,
854 mlast->size,
855 mlast->dma.virt,
856 mlast->dma.phys);
857 kfree(mlast);
858 }
859 return 0;
860}
James Smartf1c3b0f2009-07-19 10:01:32 -0400861
862/**
863 * lpfc_bsg_ct_unsol_event - process an unsolicited CT command
864 * @phba:
865 * @pring:
866 * @piocbq:
867 *
868 * This function is called when an unsolicited CT command is received. It
James Smart4cc0e562010-01-26 23:09:48 -0500869 * forwards the event to any processes registered to receive CT events.
James Smart3b5dd522010-01-26 23:10:15 -0500870 **/
James Smart4fede782010-01-26 23:08:55 -0500871int
James Smartf1c3b0f2009-07-19 10:01:32 -0400872lpfc_bsg_ct_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
873 struct lpfc_iocbq *piocbq)
874{
875 uint32_t evt_req_id = 0;
876 uint32_t cmd;
877 uint32_t len;
878 struct lpfc_dmabuf *dmabuf = NULL;
James Smart4cc0e562010-01-26 23:09:48 -0500879 struct lpfc_bsg_event *evt;
James Smartf1c3b0f2009-07-19 10:01:32 -0400880 struct event_data *evt_dat = NULL;
881 struct lpfc_iocbq *iocbq;
882 size_t offset = 0;
883 struct list_head head;
884 struct ulp_bde64 *bde;
885 dma_addr_t dma_addr;
886 int i;
887 struct lpfc_dmabuf *bdeBuf1 = piocbq->context2;
888 struct lpfc_dmabuf *bdeBuf2 = piocbq->context3;
889 struct lpfc_hbq_entry *hbqe;
890 struct lpfc_sli_ct_request *ct_req;
James Smart4cc0e562010-01-26 23:09:48 -0500891 struct fc_bsg_job *job = NULL;
James Smarta33c4f72013-03-01 16:36:00 -0500892 struct bsg_job_data *dd_data = NULL;
James Smart4fede782010-01-26 23:08:55 -0500893 unsigned long flags;
James Smart4cc0e562010-01-26 23:09:48 -0500894 int size = 0;
James Smartf1c3b0f2009-07-19 10:01:32 -0400895
896 INIT_LIST_HEAD(&head);
897 list_add_tail(&head, &piocbq->list);
898
899 if (piocbq->iocb.ulpBdeCount == 0 ||
900 piocbq->iocb.un.cont64[0].tus.f.bdeSize == 0)
901 goto error_ct_unsol_exit;
902
James Smart4cc0e562010-01-26 23:09:48 -0500903 if (phba->link_state == LPFC_HBA_ERROR ||
904 (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)))
905 goto error_ct_unsol_exit;
906
James Smartf1c3b0f2009-07-19 10:01:32 -0400907 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
908 dmabuf = bdeBuf1;
909 else {
910 dma_addr = getPaddr(piocbq->iocb.un.cont64[0].addrHigh,
911 piocbq->iocb.un.cont64[0].addrLow);
912 dmabuf = lpfc_sli_ringpostbuf_get(phba, pring, dma_addr);
913 }
James Smart4cc0e562010-01-26 23:09:48 -0500914 if (dmabuf == NULL)
915 goto error_ct_unsol_exit;
James Smartf1c3b0f2009-07-19 10:01:32 -0400916 ct_req = (struct lpfc_sli_ct_request *)dmabuf->virt;
917 evt_req_id = ct_req->FsType;
918 cmd = ct_req->CommandResponse.bits.CmdRsp;
919 len = ct_req->CommandResponse.bits.Size;
920 if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
921 lpfc_sli_ringpostbuf_put(phba, pring, dmabuf);
922
James Smart4fede782010-01-26 23:08:55 -0500923 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -0400924 list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
James Smart4cc0e562010-01-26 23:09:48 -0500925 if (!(evt->type_mask & FC_REG_CT_EVENT) ||
926 evt->req_id != evt_req_id)
James Smartf1c3b0f2009-07-19 10:01:32 -0400927 continue;
928
James Smart4cc0e562010-01-26 23:09:48 -0500929 lpfc_bsg_event_ref(evt);
930 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -0400931 evt_dat = kzalloc(sizeof(*evt_dat), GFP_KERNEL);
James Smart4cc0e562010-01-26 23:09:48 -0500932 if (evt_dat == NULL) {
933 spin_lock_irqsave(&phba->ct_ev_lock, flags);
934 lpfc_bsg_event_unref(evt);
James Smartf1c3b0f2009-07-19 10:01:32 -0400935 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
936 "2614 Memory allocation failed for "
937 "CT event\n");
938 break;
939 }
940
James Smartf1c3b0f2009-07-19 10:01:32 -0400941 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
942 /* take accumulated byte count from the last iocbq */
943 iocbq = list_entry(head.prev, typeof(*iocbq), list);
944 evt_dat->len = iocbq->iocb.unsli3.rcvsli3.acc_len;
945 } else {
946 list_for_each_entry(iocbq, &head, list) {
947 for (i = 0; i < iocbq->iocb.ulpBdeCount; i++)
948 evt_dat->len +=
949 iocbq->iocb.un.cont64[i].tus.f.bdeSize;
950 }
951 }
952
953 evt_dat->data = kzalloc(evt_dat->len, GFP_KERNEL);
James Smart4cc0e562010-01-26 23:09:48 -0500954 if (evt_dat->data == NULL) {
James Smartf1c3b0f2009-07-19 10:01:32 -0400955 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
956 "2615 Memory allocation failed for "
957 "CT event data, size %d\n",
958 evt_dat->len);
959 kfree(evt_dat);
James Smart4fede782010-01-26 23:08:55 -0500960 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smart4cc0e562010-01-26 23:09:48 -0500961 lpfc_bsg_event_unref(evt);
James Smart4fede782010-01-26 23:08:55 -0500962 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -0400963 goto error_ct_unsol_exit;
964 }
965
966 list_for_each_entry(iocbq, &head, list) {
James Smart4cc0e562010-01-26 23:09:48 -0500967 size = 0;
James Smartf1c3b0f2009-07-19 10:01:32 -0400968 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
969 bdeBuf1 = iocbq->context2;
970 bdeBuf2 = iocbq->context3;
971 }
972 for (i = 0; i < iocbq->iocb.ulpBdeCount; i++) {
James Smartf1c3b0f2009-07-19 10:01:32 -0400973 if (phba->sli3_options &
974 LPFC_SLI3_HBQ_ENABLED) {
975 if (i == 0) {
976 hbqe = (struct lpfc_hbq_entry *)
977 &iocbq->iocb.un.ulpWord[0];
978 size = hbqe->bde.tus.f.bdeSize;
979 dmabuf = bdeBuf1;
980 } else if (i == 1) {
981 hbqe = (struct lpfc_hbq_entry *)
982 &iocbq->iocb.unsli3.
983 sli3Words[4];
984 size = hbqe->bde.tus.f.bdeSize;
985 dmabuf = bdeBuf2;
986 }
987 if ((offset + size) > evt_dat->len)
988 size = evt_dat->len - offset;
989 } else {
990 size = iocbq->iocb.un.cont64[i].
991 tus.f.bdeSize;
992 bde = &iocbq->iocb.un.cont64[i];
993 dma_addr = getPaddr(bde->addrHigh,
994 bde->addrLow);
995 dmabuf = lpfc_sli_ringpostbuf_get(phba,
996 pring, dma_addr);
997 }
998 if (!dmabuf) {
999 lpfc_printf_log(phba, KERN_ERR,
1000 LOG_LIBDFC, "2616 No dmabuf "
1001 "found for iocbq 0x%p\n",
1002 iocbq);
1003 kfree(evt_dat->data);
1004 kfree(evt_dat);
James Smart4fede782010-01-26 23:08:55 -05001005 spin_lock_irqsave(&phba->ct_ev_lock,
1006 flags);
James Smart4cc0e562010-01-26 23:09:48 -05001007 lpfc_bsg_event_unref(evt);
James Smart4fede782010-01-26 23:08:55 -05001008 spin_unlock_irqrestore(
1009 &phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -04001010 goto error_ct_unsol_exit;
1011 }
1012 memcpy((char *)(evt_dat->data) + offset,
1013 dmabuf->virt, size);
1014 offset += size;
1015 if (evt_req_id != SLI_CT_ELX_LOOPBACK &&
1016 !(phba->sli3_options &
1017 LPFC_SLI3_HBQ_ENABLED)) {
1018 lpfc_sli_ringpostbuf_put(phba, pring,
1019 dmabuf);
1020 } else {
1021 switch (cmd) {
James Smart4cc0e562010-01-26 23:09:48 -05001022 case ELX_LOOPBACK_DATA:
James Smart1b511972011-12-13 13:23:09 -05001023 if (phba->sli_rev <
1024 LPFC_SLI_REV4)
1025 diag_cmd_data_free(phba,
1026 (struct lpfc_dmabufext
1027 *)dmabuf);
James Smart4cc0e562010-01-26 23:09:48 -05001028 break;
James Smartf1c3b0f2009-07-19 10:01:32 -04001029 case ELX_LOOPBACK_XRI_SETUP:
James Smart4cc0e562010-01-26 23:09:48 -05001030 if ((phba->sli_rev ==
1031 LPFC_SLI_REV2) ||
1032 (phba->sli3_options &
1033 LPFC_SLI3_HBQ_ENABLED
1034 )) {
1035 lpfc_in_buf_free(phba,
1036 dmabuf);
1037 } else {
James Smartf1c3b0f2009-07-19 10:01:32 -04001038 lpfc_post_buffer(phba,
1039 pring,
1040 1);
James Smart4cc0e562010-01-26 23:09:48 -05001041 }
James Smartf1c3b0f2009-07-19 10:01:32 -04001042 break;
1043 default:
1044 if (!(phba->sli3_options &
1045 LPFC_SLI3_HBQ_ENABLED))
1046 lpfc_post_buffer(phba,
1047 pring,
1048 1);
1049 break;
1050 }
1051 }
1052 }
1053 }
1054
James Smart4fede782010-01-26 23:08:55 -05001055 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -04001056 if (phba->sli_rev == LPFC_SLI_REV4) {
1057 evt_dat->immed_dat = phba->ctx_idx;
James Smart6dd9e312013-01-03 15:43:37 -05001058 phba->ctx_idx = (phba->ctx_idx + 1) % LPFC_CT_CTX_MAX;
James Smart589a52d2010-07-14 15:30:54 -04001059 /* Provide warning for over-run of the ct_ctx array */
James Smart6dd9e312013-01-03 15:43:37 -05001060 if (phba->ct_ctx[evt_dat->immed_dat].valid ==
James Smart589a52d2010-07-14 15:30:54 -04001061 UNSOL_VALID)
1062 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
1063 "2717 CT context array entry "
1064 "[%d] over-run: oxid:x%x, "
1065 "sid:x%x\n", phba->ctx_idx,
1066 phba->ct_ctx[
1067 evt_dat->immed_dat].oxid,
1068 phba->ct_ctx[
1069 evt_dat->immed_dat].SID);
James Smart7851fe22011-07-22 18:36:52 -04001070 phba->ct_ctx[evt_dat->immed_dat].rxid =
1071 piocbq->iocb.ulpContext;
James Smartf1c3b0f2009-07-19 10:01:32 -04001072 phba->ct_ctx[evt_dat->immed_dat].oxid =
James Smart7851fe22011-07-22 18:36:52 -04001073 piocbq->iocb.unsli3.rcvsli3.ox_id;
James Smartf1c3b0f2009-07-19 10:01:32 -04001074 phba->ct_ctx[evt_dat->immed_dat].SID =
1075 piocbq->iocb.un.rcvels.remoteID;
James Smart6dd9e312013-01-03 15:43:37 -05001076 phba->ct_ctx[evt_dat->immed_dat].valid = UNSOL_VALID;
James Smartf1c3b0f2009-07-19 10:01:32 -04001077 } else
1078 evt_dat->immed_dat = piocbq->iocb.ulpContext;
1079
1080 evt_dat->type = FC_REG_CT_EVENT;
1081 list_add(&evt_dat->node, &evt->events_to_see);
James Smart4cc0e562010-01-26 23:09:48 -05001082 if (evt_req_id == SLI_CT_ELX_LOOPBACK) {
1083 wake_up_interruptible(&evt->wq);
1084 lpfc_bsg_event_unref(evt);
James Smartf1c3b0f2009-07-19 10:01:32 -04001085 break;
James Smart4cc0e562010-01-26 23:09:48 -05001086 }
1087
1088 list_move(evt->events_to_see.prev, &evt->events_to_get);
James Smart4cc0e562010-01-26 23:09:48 -05001089
James Smarta33c4f72013-03-01 16:36:00 -05001090 dd_data = (struct bsg_job_data *)evt->dd_data;
1091 job = dd_data->set_job;
1092 dd_data->set_job = NULL;
1093 lpfc_bsg_event_unref(evt);
James Smart4cc0e562010-01-26 23:09:48 -05001094 if (job) {
1095 job->reply->reply_payload_rcv_len = size;
1096 /* make error code available to userspace */
1097 job->reply->result = 0;
1098 job->dd_data = NULL;
1099 /* complete the job back to userspace */
1100 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1101 job->job_done(job);
1102 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1103 }
James Smartf1c3b0f2009-07-19 10:01:32 -04001104 }
James Smart4fede782010-01-26 23:08:55 -05001105 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -04001106
1107error_ct_unsol_exit:
1108 if (!list_empty(&head))
1109 list_del(&head);
James Smart1b511972011-12-13 13:23:09 -05001110 if ((phba->sli_rev < LPFC_SLI_REV4) &&
1111 (evt_req_id == SLI_CT_ELX_LOOPBACK))
James Smart4cc0e562010-01-26 23:09:48 -05001112 return 0;
James Smart4fede782010-01-26 23:08:55 -05001113 return 1;
James Smartf1c3b0f2009-07-19 10:01:32 -04001114}
1115
1116/**
James Smart6dd9e312013-01-03 15:43:37 -05001117 * lpfc_bsg_ct_unsol_abort - handler ct abort to management plane
1118 * @phba: Pointer to HBA context object.
1119 * @dmabuf: pointer to a dmabuf that describes the FC sequence
1120 *
1121 * This function handles abort to the CT command toward management plane
1122 * for SLI4 port.
1123 *
1124 * If the pending context of a CT command to management plane present, clears
1125 * such context and returns 1 for handled; otherwise, it returns 0 indicating
1126 * no context exists.
1127 **/
1128int
1129lpfc_bsg_ct_unsol_abort(struct lpfc_hba *phba, struct hbq_dmabuf *dmabuf)
1130{
1131 struct fc_frame_header fc_hdr;
1132 struct fc_frame_header *fc_hdr_ptr = &fc_hdr;
1133 int ctx_idx, handled = 0;
1134 uint16_t oxid, rxid;
1135 uint32_t sid;
1136
1137 memcpy(fc_hdr_ptr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
1138 sid = sli4_sid_from_fc_hdr(fc_hdr_ptr);
1139 oxid = be16_to_cpu(fc_hdr_ptr->fh_ox_id);
1140 rxid = be16_to_cpu(fc_hdr_ptr->fh_rx_id);
1141
1142 for (ctx_idx = 0; ctx_idx < LPFC_CT_CTX_MAX; ctx_idx++) {
1143 if (phba->ct_ctx[ctx_idx].valid != UNSOL_VALID)
1144 continue;
1145 if (phba->ct_ctx[ctx_idx].rxid != rxid)
1146 continue;
1147 if (phba->ct_ctx[ctx_idx].oxid != oxid)
1148 continue;
1149 if (phba->ct_ctx[ctx_idx].SID != sid)
1150 continue;
1151 phba->ct_ctx[ctx_idx].valid = UNSOL_INVALID;
1152 handled = 1;
1153 }
1154 return handled;
1155}
1156
1157/**
James Smart4cc0e562010-01-26 23:09:48 -05001158 * lpfc_bsg_hba_set_event - process a SET_EVENT bsg vendor command
James Smartf1c3b0f2009-07-19 10:01:32 -04001159 * @job: SET_EVENT fc_bsg_job
James Smart3b5dd522010-01-26 23:10:15 -05001160 **/
James Smartf1c3b0f2009-07-19 10:01:32 -04001161static int
James Smart4cc0e562010-01-26 23:09:48 -05001162lpfc_bsg_hba_set_event(struct fc_bsg_job *job)
James Smartf1c3b0f2009-07-19 10:01:32 -04001163{
1164 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
1165 struct lpfc_hba *phba = vport->phba;
1166 struct set_ct_event *event_req;
James Smart4cc0e562010-01-26 23:09:48 -05001167 struct lpfc_bsg_event *evt;
James Smartf1c3b0f2009-07-19 10:01:32 -04001168 int rc = 0;
James Smart4cc0e562010-01-26 23:09:48 -05001169 struct bsg_job_data *dd_data = NULL;
1170 uint32_t ev_mask;
1171 unsigned long flags;
James Smartf1c3b0f2009-07-19 10:01:32 -04001172
1173 if (job->request_len <
1174 sizeof(struct fc_bsg_request) + sizeof(struct set_ct_event)) {
1175 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1176 "2612 Received SET_CT_EVENT below minimum "
1177 "size\n");
James Smart4cc0e562010-01-26 23:09:48 -05001178 rc = -EINVAL;
1179 goto job_error;
1180 }
1181
James Smartf1c3b0f2009-07-19 10:01:32 -04001182 event_req = (struct set_ct_event *)
1183 job->request->rqst_data.h_vendor.vendor_cmd;
James Smart4cc0e562010-01-26 23:09:48 -05001184 ev_mask = ((uint32_t)(unsigned long)event_req->type_mask &
1185 FC_REG_EVENT_MASK);
James Smart4fede782010-01-26 23:08:55 -05001186 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -04001187 list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
1188 if (evt->reg_id == event_req->ev_reg_id) {
James Smart4cc0e562010-01-26 23:09:48 -05001189 lpfc_bsg_event_ref(evt);
James Smartf1c3b0f2009-07-19 10:01:32 -04001190 evt->wait_time_stamp = jiffies;
James Smarta33c4f72013-03-01 16:36:00 -05001191 dd_data = (struct bsg_job_data *)evt->dd_data;
James Smartf1c3b0f2009-07-19 10:01:32 -04001192 break;
1193 }
1194 }
James Smart4fede782010-01-26 23:08:55 -05001195 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -04001196
1197 if (&evt->node == &phba->ct_ev_waiters) {
1198 /* no event waiting struct yet - first call */
James Smarta33c4f72013-03-01 16:36:00 -05001199 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
1200 if (dd_data == NULL) {
1201 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1202 "2734 Failed allocation of dd_data\n");
1203 rc = -ENOMEM;
1204 goto job_error;
1205 }
James Smart4cc0e562010-01-26 23:09:48 -05001206 evt = lpfc_bsg_event_new(ev_mask, event_req->ev_reg_id,
James Smartf1c3b0f2009-07-19 10:01:32 -04001207 event_req->ev_req_id);
1208 if (!evt) {
1209 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1210 "2617 Failed allocation of event "
1211 "waiter\n");
James Smart4cc0e562010-01-26 23:09:48 -05001212 rc = -ENOMEM;
1213 goto job_error;
James Smartf1c3b0f2009-07-19 10:01:32 -04001214 }
James Smarta33c4f72013-03-01 16:36:00 -05001215 dd_data->type = TYPE_EVT;
1216 dd_data->set_job = NULL;
1217 dd_data->context_un.evt = evt;
1218 evt->dd_data = (void *)dd_data;
James Smart4fede782010-01-26 23:08:55 -05001219 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -04001220 list_add(&evt->node, &phba->ct_ev_waiters);
James Smart4cc0e562010-01-26 23:09:48 -05001221 lpfc_bsg_event_ref(evt);
1222 evt->wait_time_stamp = jiffies;
James Smart4fede782010-01-26 23:08:55 -05001223 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -04001224 }
1225
James Smart4fede782010-01-26 23:08:55 -05001226 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smart4cc0e562010-01-26 23:09:48 -05001227 evt->waiting = 1;
James Smarta33c4f72013-03-01 16:36:00 -05001228 dd_data->set_job = job; /* for unsolicited command */
James Smart4cc0e562010-01-26 23:09:48 -05001229 job->dd_data = dd_data; /* for fc transport timeout callback*/
James Smart4fede782010-01-26 23:08:55 -05001230 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smart4cc0e562010-01-26 23:09:48 -05001231 return 0; /* call job done later */
James Smartf1c3b0f2009-07-19 10:01:32 -04001232
James Smart4cc0e562010-01-26 23:09:48 -05001233job_error:
1234 if (dd_data != NULL)
1235 kfree(dd_data);
James Smartf1c3b0f2009-07-19 10:01:32 -04001236
James Smart4cc0e562010-01-26 23:09:48 -05001237 job->dd_data = NULL;
1238 return rc;
James Smartf1c3b0f2009-07-19 10:01:32 -04001239}
1240
1241/**
James Smart4cc0e562010-01-26 23:09:48 -05001242 * lpfc_bsg_hba_get_event - process a GET_EVENT bsg vendor command
James Smartf1c3b0f2009-07-19 10:01:32 -04001243 * @job: GET_EVENT fc_bsg_job
James Smart3b5dd522010-01-26 23:10:15 -05001244 **/
James Smartf1c3b0f2009-07-19 10:01:32 -04001245static int
James Smart4cc0e562010-01-26 23:09:48 -05001246lpfc_bsg_hba_get_event(struct fc_bsg_job *job)
James Smartf1c3b0f2009-07-19 10:01:32 -04001247{
1248 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
1249 struct lpfc_hba *phba = vport->phba;
1250 struct get_ct_event *event_req;
1251 struct get_ct_event_reply *event_reply;
James Smart4cc0e562010-01-26 23:09:48 -05001252 struct lpfc_bsg_event *evt;
James Smartf1c3b0f2009-07-19 10:01:32 -04001253 struct event_data *evt_dat = NULL;
James Smart4fede782010-01-26 23:08:55 -05001254 unsigned long flags;
James Smart4cc0e562010-01-26 23:09:48 -05001255 uint32_t rc = 0;
James Smartf1c3b0f2009-07-19 10:01:32 -04001256
1257 if (job->request_len <
1258 sizeof(struct fc_bsg_request) + sizeof(struct get_ct_event)) {
1259 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1260 "2613 Received GET_CT_EVENT request below "
1261 "minimum size\n");
James Smart4cc0e562010-01-26 23:09:48 -05001262 rc = -EINVAL;
1263 goto job_error;
James Smartf1c3b0f2009-07-19 10:01:32 -04001264 }
1265
1266 event_req = (struct get_ct_event *)
1267 job->request->rqst_data.h_vendor.vendor_cmd;
1268
1269 event_reply = (struct get_ct_event_reply *)
1270 job->reply->reply_data.vendor_reply.vendor_rsp;
James Smart4fede782010-01-26 23:08:55 -05001271 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -04001272 list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
1273 if (evt->reg_id == event_req->ev_reg_id) {
1274 if (list_empty(&evt->events_to_get))
1275 break;
James Smart4cc0e562010-01-26 23:09:48 -05001276 lpfc_bsg_event_ref(evt);
James Smartf1c3b0f2009-07-19 10:01:32 -04001277 evt->wait_time_stamp = jiffies;
1278 evt_dat = list_entry(evt->events_to_get.prev,
1279 struct event_data, node);
1280 list_del(&evt_dat->node);
1281 break;
1282 }
1283 }
James Smart4fede782010-01-26 23:08:55 -05001284 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -04001285
James Smart4cc0e562010-01-26 23:09:48 -05001286 /* The app may continue to ask for event data until it gets
1287 * an error indicating that there isn't anymore
1288 */
1289 if (evt_dat == NULL) {
James Smartf1c3b0f2009-07-19 10:01:32 -04001290 job->reply->reply_payload_rcv_len = 0;
1291 rc = -ENOENT;
James Smart4cc0e562010-01-26 23:09:48 -05001292 goto job_error;
James Smartf1c3b0f2009-07-19 10:01:32 -04001293 }
1294
James Smart4cc0e562010-01-26 23:09:48 -05001295 if (evt_dat->len > job->request_payload.payload_len) {
1296 evt_dat->len = job->request_payload.payload_len;
1297 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1298 "2618 Truncated event data at %d "
1299 "bytes\n",
1300 job->request_payload.payload_len);
James Smartf1c3b0f2009-07-19 10:01:32 -04001301 }
1302
James Smart4cc0e562010-01-26 23:09:48 -05001303 event_reply->type = evt_dat->type;
James Smartf1c3b0f2009-07-19 10:01:32 -04001304 event_reply->immed_data = evt_dat->immed_dat;
James Smartf1c3b0f2009-07-19 10:01:32 -04001305 if (evt_dat->len > 0)
1306 job->reply->reply_payload_rcv_len =
James Smart4cc0e562010-01-26 23:09:48 -05001307 sg_copy_from_buffer(job->request_payload.sg_list,
1308 job->request_payload.sg_cnt,
James Smartf1c3b0f2009-07-19 10:01:32 -04001309 evt_dat->data, evt_dat->len);
1310 else
1311 job->reply->reply_payload_rcv_len = 0;
James Smartf1c3b0f2009-07-19 10:01:32 -04001312
James Smart4cc0e562010-01-26 23:09:48 -05001313 if (evt_dat) {
James Smartf1c3b0f2009-07-19 10:01:32 -04001314 kfree(evt_dat->data);
James Smart4cc0e562010-01-26 23:09:48 -05001315 kfree(evt_dat);
1316 }
1317
James Smart4fede782010-01-26 23:08:55 -05001318 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smart4cc0e562010-01-26 23:09:48 -05001319 lpfc_bsg_event_unref(evt);
James Smart4fede782010-01-26 23:08:55 -05001320 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smart4cc0e562010-01-26 23:09:48 -05001321 job->dd_data = NULL;
1322 job->reply->result = 0;
James Smartf1c3b0f2009-07-19 10:01:32 -04001323 job->job_done(job);
James Smart4cc0e562010-01-26 23:09:48 -05001324 return 0;
James Smartf1c3b0f2009-07-19 10:01:32 -04001325
James Smart4cc0e562010-01-26 23:09:48 -05001326job_error:
1327 job->dd_data = NULL;
1328 job->reply->result = rc;
James Smartf1c3b0f2009-07-19 10:01:32 -04001329 return rc;
1330}
1331
1332/**
James Smart3b5dd522010-01-26 23:10:15 -05001333 * lpfc_issue_ct_rsp_cmp - lpfc_issue_ct_rsp's completion handler
1334 * @phba: Pointer to HBA context object.
1335 * @cmdiocbq: Pointer to command iocb.
1336 * @rspiocbq: Pointer to response iocb.
1337 *
1338 * This function is the completion handler for iocbs issued using
1339 * lpfc_issue_ct_rsp_cmp function. This function is called by the
1340 * ring event handler function without any lock held. This function
1341 * can be called from both worker thread context and interrupt
1342 * context. This function also can be called from other thread which
1343 * cleans up the SLI layer objects.
1344 * This function copy the contents of the response iocb to the
1345 * response iocb memory object provided by the caller of
1346 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
1347 * sleeps for the iocb completion.
1348 **/
1349static void
1350lpfc_issue_ct_rsp_cmp(struct lpfc_hba *phba,
1351 struct lpfc_iocbq *cmdiocbq,
1352 struct lpfc_iocbq *rspiocbq)
1353{
1354 struct bsg_job_data *dd_data;
1355 struct fc_bsg_job *job;
1356 IOCB_t *rsp;
James Smarta33c4f72013-03-01 16:36:00 -05001357 struct lpfc_dmabuf *bmp, *cmp;
James Smart3b5dd522010-01-26 23:10:15 -05001358 struct lpfc_nodelist *ndlp;
1359 unsigned long flags;
1360 int rc = 0;
1361
James Smarta33c4f72013-03-01 16:36:00 -05001362 dd_data = cmdiocbq->context1;
1363
1364 /* Determine if job has been aborted */
James Smart3b5dd522010-01-26 23:10:15 -05001365 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smarta33c4f72013-03-01 16:36:00 -05001366 job = dd_data->set_job;
1367 if (job) {
1368 /* Prevent timeout handling from trying to abort job */
1369 job->dd_data = NULL;
1370 }
1371 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1372
1373 ndlp = dd_data->context_un.iocb.ndlp;
1374 cmp = cmdiocbq->context2;
1375 bmp = cmdiocbq->context3;
1376 rsp = &rspiocbq->iocb;
1377
1378 /* Copy the completed job data or set the error status */
1379
1380 if (job) {
1381 if (rsp->ulpStatus) {
1382 if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
1383 switch (rsp->un.ulpWord[4] & IOERR_PARAM_MASK) {
1384 case IOERR_SEQUENCE_TIMEOUT:
1385 rc = -ETIMEDOUT;
1386 break;
1387 case IOERR_INVALID_RPI:
1388 rc = -EFAULT;
1389 break;
1390 default:
1391 rc = -EACCES;
1392 break;
1393 }
1394 } else {
1395 rc = -EACCES;
1396 }
1397 } else {
1398 job->reply->reply_payload_rcv_len = 0;
1399 }
James Smart3b5dd522010-01-26 23:10:15 -05001400 }
1401
James Smarta33c4f72013-03-01 16:36:00 -05001402 lpfc_free_bsg_buffers(phba, cmp);
James Smart3b5dd522010-01-26 23:10:15 -05001403 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
James Smarta33c4f72013-03-01 16:36:00 -05001404 kfree(bmp);
James Smart3b5dd522010-01-26 23:10:15 -05001405 lpfc_sli_release_iocbq(phba, cmdiocbq);
1406 lpfc_nlp_put(ndlp);
James Smart3b5dd522010-01-26 23:10:15 -05001407 kfree(dd_data);
James Smarta33c4f72013-03-01 16:36:00 -05001408
1409 /* Complete the job if the job is still active */
1410
1411 if (job) {
1412 job->reply->result = rc;
1413 job->job_done(job);
1414 }
James Smart3b5dd522010-01-26 23:10:15 -05001415 return;
1416}
1417
1418/**
1419 * lpfc_issue_ct_rsp - issue a ct response
1420 * @phba: Pointer to HBA context object.
1421 * @job: Pointer to the job object.
1422 * @tag: tag index value into the ports context exchange array.
1423 * @bmp: Pointer to a dma buffer descriptor.
1424 * @num_entry: Number of enties in the bde.
1425 **/
1426static int
1427lpfc_issue_ct_rsp(struct lpfc_hba *phba, struct fc_bsg_job *job, uint32_t tag,
James Smarta33c4f72013-03-01 16:36:00 -05001428 struct lpfc_dmabuf *cmp, struct lpfc_dmabuf *bmp,
1429 int num_entry)
James Smart3b5dd522010-01-26 23:10:15 -05001430{
1431 IOCB_t *icmd;
1432 struct lpfc_iocbq *ctiocb = NULL;
1433 int rc = 0;
1434 struct lpfc_nodelist *ndlp = NULL;
1435 struct bsg_job_data *dd_data;
1436 uint32_t creg_val;
1437
1438 /* allocate our bsg tracking structure */
1439 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
1440 if (!dd_data) {
1441 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1442 "2736 Failed allocation of dd_data\n");
1443 rc = -ENOMEM;
1444 goto no_dd_data;
1445 }
1446
1447 /* Allocate buffer for command iocb */
1448 ctiocb = lpfc_sli_get_iocbq(phba);
1449 if (!ctiocb) {
James Smartd439d282010-09-29 11:18:45 -04001450 rc = -ENOMEM;
James Smart3b5dd522010-01-26 23:10:15 -05001451 goto no_ctiocb;
1452 }
1453
1454 icmd = &ctiocb->iocb;
1455 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
1456 icmd->un.xseq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
1457 icmd->un.xseq64.bdl.addrLow = putPaddrLow(bmp->phys);
1458 icmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
1459 icmd->un.xseq64.bdl.bdeSize = (num_entry * sizeof(struct ulp_bde64));
1460 icmd->un.xseq64.w5.hcsw.Fctl = (LS | LA);
1461 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
1462 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_SOL_CTL;
1463 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
1464
1465 /* Fill in rest of iocb */
1466 icmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;
1467 icmd->ulpBdeCount = 1;
1468 icmd->ulpLe = 1;
1469 icmd->ulpClass = CLASS3;
1470 if (phba->sli_rev == LPFC_SLI_REV4) {
1471 /* Do not issue unsol response if oxid not marked as valid */
James Smart6dd9e312013-01-03 15:43:37 -05001472 if (phba->ct_ctx[tag].valid != UNSOL_VALID) {
James Smart3b5dd522010-01-26 23:10:15 -05001473 rc = IOCB_ERROR;
1474 goto issue_ct_rsp_exit;
1475 }
James Smart7851fe22011-07-22 18:36:52 -04001476 icmd->ulpContext = phba->ct_ctx[tag].rxid;
1477 icmd->unsli3.rcvsli3.ox_id = phba->ct_ctx[tag].oxid;
James Smart3b5dd522010-01-26 23:10:15 -05001478 ndlp = lpfc_findnode_did(phba->pport, phba->ct_ctx[tag].SID);
1479 if (!ndlp) {
1480 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
1481 "2721 ndlp null for oxid %x SID %x\n",
1482 icmd->ulpContext,
1483 phba->ct_ctx[tag].SID);
1484 rc = IOCB_ERROR;
1485 goto issue_ct_rsp_exit;
1486 }
James Smart589a52d2010-07-14 15:30:54 -04001487
1488 /* Check if the ndlp is active */
1489 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smarta33c4f72013-03-01 16:36:00 -05001490 rc = IOCB_ERROR;
James Smart589a52d2010-07-14 15:30:54 -04001491 goto issue_ct_rsp_exit;
1492 }
1493
1494 /* get a refernece count so the ndlp doesn't go away while
1495 * we respond
1496 */
1497 if (!lpfc_nlp_get(ndlp)) {
James Smarta33c4f72013-03-01 16:36:00 -05001498 rc = IOCB_ERROR;
James Smart589a52d2010-07-14 15:30:54 -04001499 goto issue_ct_rsp_exit;
1500 }
1501
James Smart7851fe22011-07-22 18:36:52 -04001502 icmd->un.ulpWord[3] =
James Smart6d368e52011-05-24 11:44:12 -04001503 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
1504
James Smart3b5dd522010-01-26 23:10:15 -05001505 /* The exchange is done, mark the entry as invalid */
James Smart6dd9e312013-01-03 15:43:37 -05001506 phba->ct_ctx[tag].valid = UNSOL_INVALID;
James Smart3b5dd522010-01-26 23:10:15 -05001507 } else
1508 icmd->ulpContext = (ushort) tag;
1509
1510 icmd->ulpTimeout = phba->fc_ratov * 2;
1511
1512 /* Xmit CT response on exchange <xid> */
1513 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart7851fe22011-07-22 18:36:52 -04001514 "2722 Xmit CT response on exchange x%x Data: x%x x%x x%x\n",
1515 icmd->ulpContext, icmd->ulpIoTag, tag, phba->link_state);
James Smart3b5dd522010-01-26 23:10:15 -05001516
1517 ctiocb->iocb_cmpl = NULL;
1518 ctiocb->iocb_flag |= LPFC_IO_LIBDFC;
1519 ctiocb->vport = phba->pport;
James Smarta33c4f72013-03-01 16:36:00 -05001520 ctiocb->context1 = dd_data;
1521 ctiocb->context2 = cmp;
James Smart3b5dd522010-01-26 23:10:15 -05001522 ctiocb->context3 = bmp;
James Smartd5ce53b2013-04-17 20:17:26 -04001523 ctiocb->context_un.ndlp = ndlp;
James Smart3b5dd522010-01-26 23:10:15 -05001524 ctiocb->iocb_cmpl = lpfc_issue_ct_rsp_cmp;
James Smarta33c4f72013-03-01 16:36:00 -05001525
James Smart3b5dd522010-01-26 23:10:15 -05001526 dd_data->type = TYPE_IOCB;
James Smarta33c4f72013-03-01 16:36:00 -05001527 dd_data->set_job = job;
James Smart3b5dd522010-01-26 23:10:15 -05001528 dd_data->context_un.iocb.cmdiocbq = ctiocb;
James Smart3b5dd522010-01-26 23:10:15 -05001529 dd_data->context_un.iocb.ndlp = ndlp;
James Smarta33c4f72013-03-01 16:36:00 -05001530 dd_data->context_un.iocb.rmp = NULL;
1531 job->dd_data = dd_data;
James Smart3b5dd522010-01-26 23:10:15 -05001532
1533 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -05001534 if (lpfc_readl(phba->HCregaddr, &creg_val)) {
1535 rc = -IOCB_ERROR;
1536 goto issue_ct_rsp_exit;
1537 }
James Smart3b5dd522010-01-26 23:10:15 -05001538 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
1539 writel(creg_val, phba->HCregaddr);
1540 readl(phba->HCregaddr); /* flush */
1541 }
1542
1543 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
1544
1545 if (rc == IOCB_SUCCESS)
1546 return 0; /* done for now */
1547
1548issue_ct_rsp_exit:
1549 lpfc_sli_release_iocbq(phba, ctiocb);
1550no_ctiocb:
1551 kfree(dd_data);
1552no_dd_data:
1553 return rc;
1554}
1555
1556/**
1557 * lpfc_bsg_send_mgmt_rsp - process a SEND_MGMT_RESP bsg vendor command
1558 * @job: SEND_MGMT_RESP fc_bsg_job
1559 **/
1560static int
1561lpfc_bsg_send_mgmt_rsp(struct fc_bsg_job *job)
1562{
1563 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
1564 struct lpfc_hba *phba = vport->phba;
1565 struct send_mgmt_resp *mgmt_resp = (struct send_mgmt_resp *)
1566 job->request->rqst_data.h_vendor.vendor_cmd;
1567 struct ulp_bde64 *bpl;
James Smarta33c4f72013-03-01 16:36:00 -05001568 struct lpfc_dmabuf *bmp = NULL, *cmp = NULL;
1569 int bpl_entries;
James Smart3b5dd522010-01-26 23:10:15 -05001570 uint32_t tag = mgmt_resp->tag;
1571 unsigned long reqbfrcnt =
1572 (unsigned long)job->request_payload.payload_len;
1573 int rc = 0;
1574
1575 /* in case no data is transferred */
1576 job->reply->reply_payload_rcv_len = 0;
1577
1578 if (!reqbfrcnt || (reqbfrcnt > (80 * BUF_SZ_4K))) {
1579 rc = -ERANGE;
1580 goto send_mgmt_rsp_exit;
1581 }
1582
1583 bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
1584 if (!bmp) {
1585 rc = -ENOMEM;
1586 goto send_mgmt_rsp_exit;
1587 }
1588
1589 bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
1590 if (!bmp->virt) {
1591 rc = -ENOMEM;
1592 goto send_mgmt_rsp_free_bmp;
1593 }
1594
1595 INIT_LIST_HEAD(&bmp->list);
1596 bpl = (struct ulp_bde64 *) bmp->virt;
James Smarta33c4f72013-03-01 16:36:00 -05001597 bpl_entries = (LPFC_BPL_SIZE/sizeof(struct ulp_bde64));
1598 cmp = lpfc_alloc_bsg_buffers(phba, job->request_payload.payload_len,
1599 1, bpl, &bpl_entries);
1600 if (!cmp) {
1601 rc = -ENOMEM;
1602 goto send_mgmt_rsp_free_bmp;
James Smart3b5dd522010-01-26 23:10:15 -05001603 }
James Smarta33c4f72013-03-01 16:36:00 -05001604 lpfc_bsg_copy_data(cmp, &job->request_payload,
1605 job->request_payload.payload_len, 1);
James Smart3b5dd522010-01-26 23:10:15 -05001606
James Smarta33c4f72013-03-01 16:36:00 -05001607 rc = lpfc_issue_ct_rsp(phba, job, tag, cmp, bmp, bpl_entries);
James Smart3b5dd522010-01-26 23:10:15 -05001608
1609 if (rc == IOCB_SUCCESS)
1610 return 0; /* done for now */
1611
James Smart3b5dd522010-01-26 23:10:15 -05001612 rc = -EACCES;
James Smarta33c4f72013-03-01 16:36:00 -05001613
1614 lpfc_free_bsg_buffers(phba, cmp);
James Smart3b5dd522010-01-26 23:10:15 -05001615
1616send_mgmt_rsp_free_bmp:
James Smarta33c4f72013-03-01 16:36:00 -05001617 if (bmp->virt)
1618 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
James Smart3b5dd522010-01-26 23:10:15 -05001619 kfree(bmp);
1620send_mgmt_rsp_exit:
1621 /* make error code available to userspace */
1622 job->reply->result = rc;
1623 job->dd_data = NULL;
1624 return rc;
1625}
1626
1627/**
James Smart7ad20aa2011-05-24 11:44:28 -04001628 * lpfc_bsg_diag_mode_enter - process preparing into device diag loopback mode
1629 * @phba: Pointer to HBA context object.
James Smart3b5dd522010-01-26 23:10:15 -05001630 *
James Smart7ad20aa2011-05-24 11:44:28 -04001631 * This function is responsible for preparing driver for diag loopback
1632 * on device.
1633 */
1634static int
James Smart88a2cfb2011-07-22 18:36:33 -04001635lpfc_bsg_diag_mode_enter(struct lpfc_hba *phba)
James Smart7ad20aa2011-05-24 11:44:28 -04001636{
1637 struct lpfc_vport **vports;
1638 struct Scsi_Host *shost;
1639 struct lpfc_sli *psli;
1640 struct lpfc_sli_ring *pring;
1641 int i = 0;
1642
1643 psli = &phba->sli;
1644 if (!psli)
1645 return -ENODEV;
1646
1647 pring = &psli->ring[LPFC_FCP_RING];
1648 if (!pring)
1649 return -ENODEV;
1650
1651 if ((phba->link_state == LPFC_HBA_ERROR) ||
1652 (psli->sli_flag & LPFC_BLOCK_MGMT_IO) ||
1653 (!(psli->sli_flag & LPFC_SLI_ACTIVE)))
1654 return -EACCES;
1655
1656 vports = lpfc_create_vport_work_array(phba);
1657 if (vports) {
1658 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
1659 shost = lpfc_shost_from_vport(vports[i]);
1660 scsi_block_requests(shost);
1661 }
1662 lpfc_destroy_vport_work_array(phba, vports);
1663 } else {
1664 shost = lpfc_shost_from_vport(phba->pport);
1665 scsi_block_requests(shost);
1666 }
1667
James Smart0e9bb8d2013-03-01 16:35:12 -05001668 while (!list_empty(&pring->txcmplq)) {
James Smart7ad20aa2011-05-24 11:44:28 -04001669 if (i++ > 500) /* wait up to 5 seconds */
1670 break;
1671 msleep(10);
1672 }
1673 return 0;
1674}
1675
1676/**
1677 * lpfc_bsg_diag_mode_exit - exit process from device diag loopback mode
1678 * @phba: Pointer to HBA context object.
James Smart7ad20aa2011-05-24 11:44:28 -04001679 *
1680 * This function is responsible for driver exit processing of setting up
1681 * diag loopback mode on device.
1682 */
1683static void
1684lpfc_bsg_diag_mode_exit(struct lpfc_hba *phba)
1685{
1686 struct Scsi_Host *shost;
1687 struct lpfc_vport **vports;
1688 int i;
1689
1690 vports = lpfc_create_vport_work_array(phba);
1691 if (vports) {
1692 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
1693 shost = lpfc_shost_from_vport(vports[i]);
1694 scsi_unblock_requests(shost);
1695 }
1696 lpfc_destroy_vport_work_array(phba, vports);
1697 } else {
1698 shost = lpfc_shost_from_vport(phba->pport);
1699 scsi_unblock_requests(shost);
1700 }
1701 return;
1702}
1703
1704/**
1705 * lpfc_sli3_bsg_diag_loopback_mode - process an sli3 bsg vendor command
1706 * @phba: Pointer to HBA context object.
1707 * @job: LPFC_BSG_VENDOR_DIAG_MODE
1708 *
1709 * This function is responsible for placing an sli3 port into diagnostic
1710 * loopback mode in order to perform a diagnostic loopback test.
James Smart3b5dd522010-01-26 23:10:15 -05001711 * All new scsi requests are blocked, a small delay is used to allow the
1712 * scsi requests to complete then the link is brought down. If the link is
1713 * is placed in loopback mode then scsi requests are again allowed
1714 * so the scsi mid-layer doesn't give up on the port.
1715 * All of this is done in-line.
1716 */
1717static int
James Smart7ad20aa2011-05-24 11:44:28 -04001718lpfc_sli3_bsg_diag_loopback_mode(struct lpfc_hba *phba, struct fc_bsg_job *job)
James Smart3b5dd522010-01-26 23:10:15 -05001719{
James Smart3b5dd522010-01-26 23:10:15 -05001720 struct diag_mode_set *loopback_mode;
James Smart3b5dd522010-01-26 23:10:15 -05001721 uint32_t link_flags;
1722 uint32_t timeout;
James Smart1b511972011-12-13 13:23:09 -05001723 LPFC_MBOXQ_t *pmboxq = NULL;
James Smartb76f2dc2011-07-22 18:37:42 -04001724 int mbxstatus = MBX_SUCCESS;
James Smart3b5dd522010-01-26 23:10:15 -05001725 int i = 0;
1726 int rc = 0;
1727
1728 /* no data to return just the return code */
1729 job->reply->reply_payload_rcv_len = 0;
1730
James Smart7ad20aa2011-05-24 11:44:28 -04001731 if (job->request_len < sizeof(struct fc_bsg_request) +
1732 sizeof(struct diag_mode_set)) {
James Smart3b5dd522010-01-26 23:10:15 -05001733 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
James Smart7ad20aa2011-05-24 11:44:28 -04001734 "2738 Received DIAG MODE request size:%d "
1735 "below the minimum size:%d\n",
1736 job->request_len,
1737 (int)(sizeof(struct fc_bsg_request) +
1738 sizeof(struct diag_mode_set)));
James Smart3b5dd522010-01-26 23:10:15 -05001739 rc = -EINVAL;
1740 goto job_error;
1741 }
1742
James Smart88a2cfb2011-07-22 18:36:33 -04001743 rc = lpfc_bsg_diag_mode_enter(phba);
James Smart7ad20aa2011-05-24 11:44:28 -04001744 if (rc)
1745 goto job_error;
1746
1747 /* bring the link to diagnostic mode */
James Smart3b5dd522010-01-26 23:10:15 -05001748 loopback_mode = (struct diag_mode_set *)
1749 job->request->rqst_data.h_vendor.vendor_cmd;
1750 link_flags = loopback_mode->type;
James Smart515e0aa2010-09-29 11:19:00 -04001751 timeout = loopback_mode->timeout * 100;
James Smart3b5dd522010-01-26 23:10:15 -05001752
James Smart3b5dd522010-01-26 23:10:15 -05001753 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1754 if (!pmboxq) {
1755 rc = -ENOMEM;
James Smart7ad20aa2011-05-24 11:44:28 -04001756 goto loopback_mode_exit;
James Smart3b5dd522010-01-26 23:10:15 -05001757 }
James Smart3b5dd522010-01-26 23:10:15 -05001758 memset((void *)pmboxq, 0, sizeof(LPFC_MBOXQ_t));
1759 pmboxq->u.mb.mbxCommand = MBX_DOWN_LINK;
1760 pmboxq->u.mb.mbxOwner = OWN_HOST;
1761
1762 mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
1763
1764 if ((mbxstatus == MBX_SUCCESS) && (pmboxq->u.mb.mbxStatus == 0)) {
1765 /* wait for link down before proceeding */
1766 i = 0;
1767 while (phba->link_state != LPFC_LINK_DOWN) {
1768 if (i++ > timeout) {
1769 rc = -ETIMEDOUT;
1770 goto loopback_mode_exit;
1771 }
James Smart3b5dd522010-01-26 23:10:15 -05001772 msleep(10);
1773 }
1774
1775 memset((void *)pmboxq, 0, sizeof(LPFC_MBOXQ_t));
1776 if (link_flags == INTERNAL_LOOP_BACK)
1777 pmboxq->u.mb.un.varInitLnk.link_flags = FLAGS_LOCAL_LB;
1778 else
1779 pmboxq->u.mb.un.varInitLnk.link_flags =
1780 FLAGS_TOPOLOGY_MODE_LOOP;
1781
1782 pmboxq->u.mb.mbxCommand = MBX_INIT_LINK;
1783 pmboxq->u.mb.mbxOwner = OWN_HOST;
1784
1785 mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq,
1786 LPFC_MBOX_TMO);
1787
1788 if ((mbxstatus != MBX_SUCCESS) || (pmboxq->u.mb.mbxStatus))
1789 rc = -ENODEV;
1790 else {
James Smart1b511972011-12-13 13:23:09 -05001791 spin_lock_irq(&phba->hbalock);
James Smart3b5dd522010-01-26 23:10:15 -05001792 phba->link_flag |= LS_LOOPBACK_MODE;
James Smart1b511972011-12-13 13:23:09 -05001793 spin_unlock_irq(&phba->hbalock);
James Smart3b5dd522010-01-26 23:10:15 -05001794 /* wait for the link attention interrupt */
1795 msleep(100);
1796
1797 i = 0;
1798 while (phba->link_state != LPFC_HBA_READY) {
1799 if (i++ > timeout) {
1800 rc = -ETIMEDOUT;
1801 break;
1802 }
1803
1804 msleep(10);
1805 }
1806 }
1807
1808 } else
1809 rc = -ENODEV;
1810
1811loopback_mode_exit:
James Smart7ad20aa2011-05-24 11:44:28 -04001812 lpfc_bsg_diag_mode_exit(phba);
James Smart3b5dd522010-01-26 23:10:15 -05001813
1814 /*
1815 * Let SLI layer release mboxq if mbox command completed after timeout.
1816 */
James Smart1b511972011-12-13 13:23:09 -05001817 if (pmboxq && mbxstatus != MBX_TIMEOUT)
James Smart3b5dd522010-01-26 23:10:15 -05001818 mempool_free(pmboxq, phba->mbox_mem_pool);
1819
1820job_error:
1821 /* make error code available to userspace */
1822 job->reply->result = rc;
1823 /* complete the job back to userspace if no error */
1824 if (rc == 0)
1825 job->job_done(job);
1826 return rc;
1827}
1828
1829/**
James Smart7ad20aa2011-05-24 11:44:28 -04001830 * lpfc_sli4_bsg_set_link_diag_state - set sli4 link diag state
1831 * @phba: Pointer to HBA context object.
1832 * @diag: Flag for set link to diag or nomral operation state.
1833 *
1834 * This function is responsible for issuing a sli4 mailbox command for setting
1835 * link to either diag state or normal operation state.
1836 */
1837static int
1838lpfc_sli4_bsg_set_link_diag_state(struct lpfc_hba *phba, uint32_t diag)
1839{
1840 LPFC_MBOXQ_t *pmboxq;
1841 struct lpfc_mbx_set_link_diag_state *link_diag_state;
1842 uint32_t req_len, alloc_len;
1843 int mbxstatus = MBX_SUCCESS, rc;
1844
1845 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1846 if (!pmboxq)
1847 return -ENOMEM;
1848
1849 req_len = (sizeof(struct lpfc_mbx_set_link_diag_state) -
1850 sizeof(struct lpfc_sli4_cfg_mhdr));
1851 alloc_len = lpfc_sli4_config(phba, pmboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
1852 LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_STATE,
1853 req_len, LPFC_SLI4_MBX_EMBED);
1854 if (alloc_len != req_len) {
1855 rc = -ENOMEM;
1856 goto link_diag_state_set_out;
1857 }
James Smart1b511972011-12-13 13:23:09 -05001858 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
1859 "3128 Set link to diagnostic state:x%x (x%x/x%x)\n",
1860 diag, phba->sli4_hba.lnk_info.lnk_tp,
1861 phba->sli4_hba.lnk_info.lnk_no);
1862
James Smart7ad20aa2011-05-24 11:44:28 -04001863 link_diag_state = &pmboxq->u.mqe.un.link_diag_state;
James Smart97315922012-08-03 12:32:52 -04001864 bf_set(lpfc_mbx_set_diag_state_diag_bit_valid, &link_diag_state->u.req,
1865 LPFC_DIAG_STATE_DIAG_BIT_VALID_CHANGE);
James Smart7ad20aa2011-05-24 11:44:28 -04001866 bf_set(lpfc_mbx_set_diag_state_link_num, &link_diag_state->u.req,
James Smart1b511972011-12-13 13:23:09 -05001867 phba->sli4_hba.lnk_info.lnk_no);
James Smart7ad20aa2011-05-24 11:44:28 -04001868 bf_set(lpfc_mbx_set_diag_state_link_type, &link_diag_state->u.req,
James Smart1b511972011-12-13 13:23:09 -05001869 phba->sli4_hba.lnk_info.lnk_tp);
James Smart7ad20aa2011-05-24 11:44:28 -04001870 if (diag)
1871 bf_set(lpfc_mbx_set_diag_state_diag,
1872 &link_diag_state->u.req, 1);
1873 else
1874 bf_set(lpfc_mbx_set_diag_state_diag,
1875 &link_diag_state->u.req, 0);
1876
1877 mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
1878
1879 if ((mbxstatus == MBX_SUCCESS) && (pmboxq->u.mb.mbxStatus == 0))
1880 rc = 0;
1881 else
1882 rc = -ENODEV;
1883
1884link_diag_state_set_out:
1885 if (pmboxq && (mbxstatus != MBX_TIMEOUT))
1886 mempool_free(pmboxq, phba->mbox_mem_pool);
1887
1888 return rc;
1889}
1890
1891/**
James Smart1b511972011-12-13 13:23:09 -05001892 * lpfc_sli4_bsg_set_internal_loopback - set sli4 internal loopback diagnostic
1893 * @phba: Pointer to HBA context object.
1894 *
1895 * This function is responsible for issuing a sli4 mailbox command for setting
1896 * up internal loopback diagnostic.
1897 */
1898static int
1899lpfc_sli4_bsg_set_internal_loopback(struct lpfc_hba *phba)
1900{
1901 LPFC_MBOXQ_t *pmboxq;
1902 uint32_t req_len, alloc_len;
1903 struct lpfc_mbx_set_link_diag_loopback *link_diag_loopback;
1904 int mbxstatus = MBX_SUCCESS, rc = 0;
1905
1906 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1907 if (!pmboxq)
1908 return -ENOMEM;
1909 req_len = (sizeof(struct lpfc_mbx_set_link_diag_loopback) -
1910 sizeof(struct lpfc_sli4_cfg_mhdr));
1911 alloc_len = lpfc_sli4_config(phba, pmboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
1912 LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_LOOPBACK,
1913 req_len, LPFC_SLI4_MBX_EMBED);
1914 if (alloc_len != req_len) {
1915 mempool_free(pmboxq, phba->mbox_mem_pool);
1916 return -ENOMEM;
1917 }
1918 link_diag_loopback = &pmboxq->u.mqe.un.link_diag_loopback;
1919 bf_set(lpfc_mbx_set_diag_state_link_num,
1920 &link_diag_loopback->u.req, phba->sli4_hba.lnk_info.lnk_no);
1921 bf_set(lpfc_mbx_set_diag_state_link_type,
1922 &link_diag_loopback->u.req, phba->sli4_hba.lnk_info.lnk_tp);
1923 bf_set(lpfc_mbx_set_diag_lpbk_type, &link_diag_loopback->u.req,
James Smart3ef6d242012-01-18 16:23:48 -05001924 LPFC_DIAG_LOOPBACK_TYPE_INTERNAL);
James Smart1b511972011-12-13 13:23:09 -05001925
1926 mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
1927 if ((mbxstatus != MBX_SUCCESS) || (pmboxq->u.mb.mbxStatus)) {
1928 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1929 "3127 Failed setup loopback mode mailbox "
1930 "command, rc:x%x, status:x%x\n", mbxstatus,
1931 pmboxq->u.mb.mbxStatus);
1932 rc = -ENODEV;
1933 }
1934 if (pmboxq && (mbxstatus != MBX_TIMEOUT))
1935 mempool_free(pmboxq, phba->mbox_mem_pool);
1936 return rc;
1937}
1938
1939/**
1940 * lpfc_sli4_diag_fcport_reg_setup - setup port registrations for diagnostic
1941 * @phba: Pointer to HBA context object.
1942 *
1943 * This function set up SLI4 FC port registrations for diagnostic run, which
1944 * includes all the rpis, vfi, and also vpi.
1945 */
1946static int
1947lpfc_sli4_diag_fcport_reg_setup(struct lpfc_hba *phba)
1948{
1949 int rc;
1950
1951 if (phba->pport->fc_flag & FC_VFI_REGISTERED) {
1952 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1953 "3136 Port still had vfi registered: "
1954 "mydid:x%x, fcfi:%d, vfi:%d, vpi:%d\n",
1955 phba->pport->fc_myDID, phba->fcf.fcfi,
1956 phba->sli4_hba.vfi_ids[phba->pport->vfi],
1957 phba->vpi_ids[phba->pport->vpi]);
1958 return -EINVAL;
1959 }
1960 rc = lpfc_issue_reg_vfi(phba->pport);
1961 return rc;
1962}
1963
1964/**
James Smart7ad20aa2011-05-24 11:44:28 -04001965 * lpfc_sli4_bsg_diag_loopback_mode - process an sli4 bsg vendor command
1966 * @phba: Pointer to HBA context object.
1967 * @job: LPFC_BSG_VENDOR_DIAG_MODE
1968 *
1969 * This function is responsible for placing an sli4 port into diagnostic
1970 * loopback mode in order to perform a diagnostic loopback test.
1971 */
1972static int
1973lpfc_sli4_bsg_diag_loopback_mode(struct lpfc_hba *phba, struct fc_bsg_job *job)
1974{
1975 struct diag_mode_set *loopback_mode;
James Smart1b511972011-12-13 13:23:09 -05001976 uint32_t link_flags, timeout;
1977 int i, rc = 0;
James Smart7ad20aa2011-05-24 11:44:28 -04001978
1979 /* no data to return just the return code */
1980 job->reply->reply_payload_rcv_len = 0;
1981
1982 if (job->request_len < sizeof(struct fc_bsg_request) +
1983 sizeof(struct diag_mode_set)) {
1984 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1985 "3011 Received DIAG MODE request size:%d "
1986 "below the minimum size:%d\n",
1987 job->request_len,
1988 (int)(sizeof(struct fc_bsg_request) +
1989 sizeof(struct diag_mode_set)));
1990 rc = -EINVAL;
1991 goto job_error;
1992 }
1993
James Smart88a2cfb2011-07-22 18:36:33 -04001994 rc = lpfc_bsg_diag_mode_enter(phba);
James Smart7ad20aa2011-05-24 11:44:28 -04001995 if (rc)
1996 goto job_error;
1997
James Smart1b511972011-12-13 13:23:09 -05001998 /* indicate we are in loobpack diagnostic mode */
1999 spin_lock_irq(&phba->hbalock);
2000 phba->link_flag |= LS_LOOPBACK_MODE;
2001 spin_unlock_irq(&phba->hbalock);
2002
2003 /* reset port to start frome scratch */
2004 rc = lpfc_selective_reset(phba);
2005 if (rc)
2006 goto job_error;
2007
James Smart7ad20aa2011-05-24 11:44:28 -04002008 /* bring the link to diagnostic mode */
James Smart1b511972011-12-13 13:23:09 -05002009 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
2010 "3129 Bring link to diagnostic state.\n");
James Smart7ad20aa2011-05-24 11:44:28 -04002011 loopback_mode = (struct diag_mode_set *)
2012 job->request->rqst_data.h_vendor.vendor_cmd;
2013 link_flags = loopback_mode->type;
2014 timeout = loopback_mode->timeout * 100;
2015
2016 rc = lpfc_sli4_bsg_set_link_diag_state(phba, 1);
James Smart1b511972011-12-13 13:23:09 -05002017 if (rc) {
2018 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2019 "3130 Failed to bring link to diagnostic "
2020 "state, rc:x%x\n", rc);
James Smart7ad20aa2011-05-24 11:44:28 -04002021 goto loopback_mode_exit;
James Smart1b511972011-12-13 13:23:09 -05002022 }
James Smart7ad20aa2011-05-24 11:44:28 -04002023
2024 /* wait for link down before proceeding */
2025 i = 0;
2026 while (phba->link_state != LPFC_LINK_DOWN) {
2027 if (i++ > timeout) {
2028 rc = -ETIMEDOUT;
James Smart1b511972011-12-13 13:23:09 -05002029 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
2030 "3131 Timeout waiting for link to "
2031 "diagnostic mode, timeout:%d ms\n",
2032 timeout * 10);
James Smart7ad20aa2011-05-24 11:44:28 -04002033 goto loopback_mode_exit;
2034 }
2035 msleep(10);
2036 }
James Smart7ad20aa2011-05-24 11:44:28 -04002037
James Smart1b511972011-12-13 13:23:09 -05002038 /* set up loopback mode */
2039 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
2040 "3132 Set up loopback mode:x%x\n", link_flags);
2041
2042 if (link_flags == INTERNAL_LOOP_BACK)
2043 rc = lpfc_sli4_bsg_set_internal_loopback(phba);
2044 else if (link_flags == EXTERNAL_LOOP_BACK)
2045 rc = lpfc_hba_init_link_fc_topology(phba,
2046 FLAGS_TOPOLOGY_MODE_PT_PT,
2047 MBX_NOWAIT);
James Smart7ad20aa2011-05-24 11:44:28 -04002048 else {
James Smart1b511972011-12-13 13:23:09 -05002049 rc = -EINVAL;
2050 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
2051 "3141 Loopback mode:x%x not supported\n",
2052 link_flags);
2053 goto loopback_mode_exit;
2054 }
2055
2056 if (!rc) {
James Smart7ad20aa2011-05-24 11:44:28 -04002057 /* wait for the link attention interrupt */
2058 msleep(100);
2059 i = 0;
James Smart1b511972011-12-13 13:23:09 -05002060 while (phba->link_state < LPFC_LINK_UP) {
2061 if (i++ > timeout) {
2062 rc = -ETIMEDOUT;
2063 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
2064 "3137 Timeout waiting for link up "
2065 "in loopback mode, timeout:%d ms\n",
2066 timeout * 10);
2067 break;
2068 }
2069 msleep(10);
2070 }
2071 }
2072
2073 /* port resource registration setup for loopback diagnostic */
2074 if (!rc) {
2075 /* set up a none zero myDID for loopback test */
2076 phba->pport->fc_myDID = 1;
2077 rc = lpfc_sli4_diag_fcport_reg_setup(phba);
2078 } else
2079 goto loopback_mode_exit;
2080
2081 if (!rc) {
2082 /* wait for the port ready */
2083 msleep(100);
2084 i = 0;
James Smart7ad20aa2011-05-24 11:44:28 -04002085 while (phba->link_state != LPFC_HBA_READY) {
2086 if (i++ > timeout) {
2087 rc = -ETIMEDOUT;
James Smart1b511972011-12-13 13:23:09 -05002088 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
2089 "3133 Timeout waiting for port "
2090 "loopback mode ready, timeout:%d ms\n",
2091 timeout * 10);
James Smart7ad20aa2011-05-24 11:44:28 -04002092 break;
2093 }
2094 msleep(10);
2095 }
2096 }
2097
2098loopback_mode_exit:
James Smart1b511972011-12-13 13:23:09 -05002099 /* clear loopback diagnostic mode */
2100 if (rc) {
2101 spin_lock_irq(&phba->hbalock);
2102 phba->link_flag &= ~LS_LOOPBACK_MODE;
2103 spin_unlock_irq(&phba->hbalock);
2104 }
James Smart7ad20aa2011-05-24 11:44:28 -04002105 lpfc_bsg_diag_mode_exit(phba);
2106
James Smart7ad20aa2011-05-24 11:44:28 -04002107job_error:
2108 /* make error code available to userspace */
2109 job->reply->result = rc;
2110 /* complete the job back to userspace if no error */
2111 if (rc == 0)
2112 job->job_done(job);
2113 return rc;
2114}
2115
2116/**
2117 * lpfc_bsg_diag_loopback_mode - bsg vendor command for diag loopback mode
2118 * @job: LPFC_BSG_VENDOR_DIAG_MODE
2119 *
2120 * This function is responsible for responding to check and dispatch bsg diag
2121 * command from the user to proper driver action routines.
2122 */
2123static int
2124lpfc_bsg_diag_loopback_mode(struct fc_bsg_job *job)
2125{
2126 struct Scsi_Host *shost;
2127 struct lpfc_vport *vport;
2128 struct lpfc_hba *phba;
2129 int rc;
2130
2131 shost = job->shost;
2132 if (!shost)
2133 return -ENODEV;
2134 vport = (struct lpfc_vport *)job->shost->hostdata;
2135 if (!vport)
2136 return -ENODEV;
2137 phba = vport->phba;
2138 if (!phba)
2139 return -ENODEV;
2140
2141 if (phba->sli_rev < LPFC_SLI_REV4)
2142 rc = lpfc_sli3_bsg_diag_loopback_mode(phba, job);
2143 else if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
2144 LPFC_SLI_INTF_IF_TYPE_2)
2145 rc = lpfc_sli4_bsg_diag_loopback_mode(phba, job);
2146 else
2147 rc = -ENODEV;
2148
2149 return rc;
James Smart7ad20aa2011-05-24 11:44:28 -04002150}
2151
2152/**
2153 * lpfc_sli4_bsg_diag_mode_end - sli4 bsg vendor command for ending diag mode
2154 * @job: LPFC_BSG_VENDOR_DIAG_MODE_END
2155 *
2156 * This function is responsible for responding to check and dispatch bsg diag
2157 * command from the user to proper driver action routines.
2158 */
2159static int
2160lpfc_sli4_bsg_diag_mode_end(struct fc_bsg_job *job)
2161{
2162 struct Scsi_Host *shost;
2163 struct lpfc_vport *vport;
2164 struct lpfc_hba *phba;
James Smart1b511972011-12-13 13:23:09 -05002165 struct diag_mode_set *loopback_mode_end_cmd;
2166 uint32_t timeout;
2167 int rc, i;
James Smart7ad20aa2011-05-24 11:44:28 -04002168
2169 shost = job->shost;
2170 if (!shost)
2171 return -ENODEV;
2172 vport = (struct lpfc_vport *)job->shost->hostdata;
2173 if (!vport)
2174 return -ENODEV;
2175 phba = vport->phba;
2176 if (!phba)
2177 return -ENODEV;
2178
2179 if (phba->sli_rev < LPFC_SLI_REV4)
2180 return -ENODEV;
2181 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
2182 LPFC_SLI_INTF_IF_TYPE_2)
2183 return -ENODEV;
2184
James Smart1b511972011-12-13 13:23:09 -05002185 /* clear loopback diagnostic mode */
2186 spin_lock_irq(&phba->hbalock);
2187 phba->link_flag &= ~LS_LOOPBACK_MODE;
2188 spin_unlock_irq(&phba->hbalock);
2189 loopback_mode_end_cmd = (struct diag_mode_set *)
2190 job->request->rqst_data.h_vendor.vendor_cmd;
2191 timeout = loopback_mode_end_cmd->timeout * 100;
2192
James Smart7ad20aa2011-05-24 11:44:28 -04002193 rc = lpfc_sli4_bsg_set_link_diag_state(phba, 0);
James Smart1b511972011-12-13 13:23:09 -05002194 if (rc) {
2195 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2196 "3139 Failed to bring link to diagnostic "
2197 "state, rc:x%x\n", rc);
2198 goto loopback_mode_end_exit;
2199 }
James Smart7ad20aa2011-05-24 11:44:28 -04002200
James Smart1b511972011-12-13 13:23:09 -05002201 /* wait for link down before proceeding */
2202 i = 0;
2203 while (phba->link_state != LPFC_LINK_DOWN) {
2204 if (i++ > timeout) {
2205 rc = -ETIMEDOUT;
2206 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
2207 "3140 Timeout waiting for link to "
2208 "diagnostic mode_end, timeout:%d ms\n",
2209 timeout * 10);
2210 /* there is nothing much we can do here */
2211 break;
2212 }
2213 msleep(10);
2214 }
James Smart7ad20aa2011-05-24 11:44:28 -04002215
James Smart1b511972011-12-13 13:23:09 -05002216 /* reset port resource registrations */
2217 rc = lpfc_selective_reset(phba);
2218 phba->pport->fc_myDID = 0;
2219
2220loopback_mode_end_exit:
2221 /* make return code available to userspace */
2222 job->reply->result = rc;
2223 /* complete the job back to userspace if no error */
2224 if (rc == 0)
2225 job->job_done(job);
James Smart7ad20aa2011-05-24 11:44:28 -04002226 return rc;
2227}
2228
2229/**
2230 * lpfc_sli4_bsg_link_diag_test - sli4 bsg vendor command for diag link test
2231 * @job: LPFC_BSG_VENDOR_DIAG_LINK_TEST
2232 *
2233 * This function is to perform SLI4 diag link test request from the user
2234 * applicaiton.
2235 */
2236static int
2237lpfc_sli4_bsg_link_diag_test(struct fc_bsg_job *job)
2238{
2239 struct Scsi_Host *shost;
2240 struct lpfc_vport *vport;
2241 struct lpfc_hba *phba;
2242 LPFC_MBOXQ_t *pmboxq;
2243 struct sli4_link_diag *link_diag_test_cmd;
2244 uint32_t req_len, alloc_len;
2245 uint32_t timeout;
2246 struct lpfc_mbx_run_link_diag_test *run_link_diag_test;
2247 union lpfc_sli4_cfg_shdr *shdr;
2248 uint32_t shdr_status, shdr_add_status;
2249 struct diag_status *diag_status_reply;
2250 int mbxstatus, rc = 0;
2251
2252 shost = job->shost;
2253 if (!shost) {
2254 rc = -ENODEV;
2255 goto job_error;
2256 }
2257 vport = (struct lpfc_vport *)job->shost->hostdata;
2258 if (!vport) {
2259 rc = -ENODEV;
2260 goto job_error;
2261 }
2262 phba = vport->phba;
2263 if (!phba) {
2264 rc = -ENODEV;
2265 goto job_error;
2266 }
2267
2268 if (phba->sli_rev < LPFC_SLI_REV4) {
2269 rc = -ENODEV;
2270 goto job_error;
2271 }
2272 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
2273 LPFC_SLI_INTF_IF_TYPE_2) {
2274 rc = -ENODEV;
2275 goto job_error;
2276 }
2277
2278 if (job->request_len < sizeof(struct fc_bsg_request) +
2279 sizeof(struct sli4_link_diag)) {
2280 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2281 "3013 Received LINK DIAG TEST request "
2282 " size:%d below the minimum size:%d\n",
2283 job->request_len,
2284 (int)(sizeof(struct fc_bsg_request) +
2285 sizeof(struct sli4_link_diag)));
2286 rc = -EINVAL;
2287 goto job_error;
2288 }
2289
James Smart88a2cfb2011-07-22 18:36:33 -04002290 rc = lpfc_bsg_diag_mode_enter(phba);
James Smart7ad20aa2011-05-24 11:44:28 -04002291 if (rc)
2292 goto job_error;
2293
2294 link_diag_test_cmd = (struct sli4_link_diag *)
2295 job->request->rqst_data.h_vendor.vendor_cmd;
2296 timeout = link_diag_test_cmd->timeout * 100;
2297
2298 rc = lpfc_sli4_bsg_set_link_diag_state(phba, 1);
2299
2300 if (rc)
2301 goto job_error;
2302
2303 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2304 if (!pmboxq) {
2305 rc = -ENOMEM;
2306 goto link_diag_test_exit;
2307 }
2308
2309 req_len = (sizeof(struct lpfc_mbx_set_link_diag_state) -
2310 sizeof(struct lpfc_sli4_cfg_mhdr));
2311 alloc_len = lpfc_sli4_config(phba, pmboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
2312 LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_STATE,
2313 req_len, LPFC_SLI4_MBX_EMBED);
2314 if (alloc_len != req_len) {
2315 rc = -ENOMEM;
2316 goto link_diag_test_exit;
2317 }
2318 run_link_diag_test = &pmboxq->u.mqe.un.link_diag_test;
2319 bf_set(lpfc_mbx_run_diag_test_link_num, &run_link_diag_test->u.req,
James Smart1b511972011-12-13 13:23:09 -05002320 phba->sli4_hba.lnk_info.lnk_no);
James Smart7ad20aa2011-05-24 11:44:28 -04002321 bf_set(lpfc_mbx_run_diag_test_link_type, &run_link_diag_test->u.req,
James Smart1b511972011-12-13 13:23:09 -05002322 phba->sli4_hba.lnk_info.lnk_tp);
James Smart7ad20aa2011-05-24 11:44:28 -04002323 bf_set(lpfc_mbx_run_diag_test_test_id, &run_link_diag_test->u.req,
2324 link_diag_test_cmd->test_id);
2325 bf_set(lpfc_mbx_run_diag_test_loops, &run_link_diag_test->u.req,
2326 link_diag_test_cmd->loops);
2327 bf_set(lpfc_mbx_run_diag_test_test_ver, &run_link_diag_test->u.req,
2328 link_diag_test_cmd->test_version);
2329 bf_set(lpfc_mbx_run_diag_test_err_act, &run_link_diag_test->u.req,
2330 link_diag_test_cmd->error_action);
2331
2332 mbxstatus = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
2333
2334 shdr = (union lpfc_sli4_cfg_shdr *)
2335 &pmboxq->u.mqe.un.sli4_config.header.cfg_shdr;
2336 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
2337 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
2338 if (shdr_status || shdr_add_status || mbxstatus) {
2339 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
2340 "3010 Run link diag test mailbox failed with "
2341 "mbx_status x%x status x%x, add_status x%x\n",
2342 mbxstatus, shdr_status, shdr_add_status);
2343 }
2344
2345 diag_status_reply = (struct diag_status *)
2346 job->reply->reply_data.vendor_reply.vendor_rsp;
2347
2348 if (job->reply_len <
2349 sizeof(struct fc_bsg_request) + sizeof(struct diag_status)) {
2350 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2351 "3012 Received Run link diag test reply "
2352 "below minimum size (%d): reply_len:%d\n",
2353 (int)(sizeof(struct fc_bsg_request) +
2354 sizeof(struct diag_status)),
2355 job->reply_len);
2356 rc = -EINVAL;
2357 goto job_error;
2358 }
2359
2360 diag_status_reply->mbox_status = mbxstatus;
2361 diag_status_reply->shdr_status = shdr_status;
2362 diag_status_reply->shdr_add_status = shdr_add_status;
2363
2364link_diag_test_exit:
2365 rc = lpfc_sli4_bsg_set_link_diag_state(phba, 0);
2366
2367 if (pmboxq)
2368 mempool_free(pmboxq, phba->mbox_mem_pool);
2369
2370 lpfc_bsg_diag_mode_exit(phba);
2371
2372job_error:
2373 /* make error code available to userspace */
2374 job->reply->result = rc;
2375 /* complete the job back to userspace if no error */
2376 if (rc == 0)
2377 job->job_done(job);
2378 return rc;
2379}
2380
2381/**
James Smart3b5dd522010-01-26 23:10:15 -05002382 * lpfcdiag_loop_self_reg - obtains a remote port login id
2383 * @phba: Pointer to HBA context object
2384 * @rpi: Pointer to a remote port login id
2385 *
2386 * This function obtains a remote port login id so the diag loopback test
2387 * can send and receive its own unsolicited CT command.
2388 **/
James Smart40426292010-12-15 17:58:10 -05002389static int lpfcdiag_loop_self_reg(struct lpfc_hba *phba, uint16_t *rpi)
James Smart3b5dd522010-01-26 23:10:15 -05002390{
2391 LPFC_MBOXQ_t *mbox;
2392 struct lpfc_dmabuf *dmabuff;
2393 int status;
2394
2395 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2396 if (!mbox)
James Smartd439d282010-09-29 11:18:45 -04002397 return -ENOMEM;
James Smart3b5dd522010-01-26 23:10:15 -05002398
James Smart1b511972011-12-13 13:23:09 -05002399 if (phba->sli_rev < LPFC_SLI_REV4)
2400 status = lpfc_reg_rpi(phba, 0, phba->pport->fc_myDID,
2401 (uint8_t *)&phba->pport->fc_sparam,
2402 mbox, *rpi);
2403 else {
James Smart40426292010-12-15 17:58:10 -05002404 *rpi = lpfc_sli4_alloc_rpi(phba);
James Smart1b511972011-12-13 13:23:09 -05002405 status = lpfc_reg_rpi(phba, phba->pport->vpi,
2406 phba->pport->fc_myDID,
2407 (uint8_t *)&phba->pport->fc_sparam,
2408 mbox, *rpi);
2409 }
2410
James Smart3b5dd522010-01-26 23:10:15 -05002411 if (status) {
2412 mempool_free(mbox, phba->mbox_mem_pool);
James Smart40426292010-12-15 17:58:10 -05002413 if (phba->sli_rev == LPFC_SLI_REV4)
2414 lpfc_sli4_free_rpi(phba, *rpi);
James Smartd439d282010-09-29 11:18:45 -04002415 return -ENOMEM;
James Smart3b5dd522010-01-26 23:10:15 -05002416 }
2417
2418 dmabuff = (struct lpfc_dmabuf *) mbox->context1;
2419 mbox->context1 = NULL;
James Smartd439d282010-09-29 11:18:45 -04002420 mbox->context2 = NULL;
James Smart3b5dd522010-01-26 23:10:15 -05002421 status = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
2422
2423 if ((status != MBX_SUCCESS) || (mbox->u.mb.mbxStatus)) {
2424 lpfc_mbuf_free(phba, dmabuff->virt, dmabuff->phys);
2425 kfree(dmabuff);
2426 if (status != MBX_TIMEOUT)
2427 mempool_free(mbox, phba->mbox_mem_pool);
James Smart40426292010-12-15 17:58:10 -05002428 if (phba->sli_rev == LPFC_SLI_REV4)
2429 lpfc_sli4_free_rpi(phba, *rpi);
James Smartd439d282010-09-29 11:18:45 -04002430 return -ENODEV;
James Smart3b5dd522010-01-26 23:10:15 -05002431 }
2432
James Smart1b511972011-12-13 13:23:09 -05002433 if (phba->sli_rev < LPFC_SLI_REV4)
2434 *rpi = mbox->u.mb.un.varWords[0];
James Smart3b5dd522010-01-26 23:10:15 -05002435
2436 lpfc_mbuf_free(phba, dmabuff->virt, dmabuff->phys);
2437 kfree(dmabuff);
2438 mempool_free(mbox, phba->mbox_mem_pool);
2439 return 0;
2440}
2441
2442/**
2443 * lpfcdiag_loop_self_unreg - unregs from the rpi
2444 * @phba: Pointer to HBA context object
2445 * @rpi: Remote port login id
2446 *
2447 * This function unregisters the rpi obtained in lpfcdiag_loop_self_reg
2448 **/
2449static int lpfcdiag_loop_self_unreg(struct lpfc_hba *phba, uint16_t rpi)
2450{
2451 LPFC_MBOXQ_t *mbox;
2452 int status;
2453
2454 /* Allocate mboxq structure */
2455 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2456 if (mbox == NULL)
James Smartd439d282010-09-29 11:18:45 -04002457 return -ENOMEM;
James Smart3b5dd522010-01-26 23:10:15 -05002458
James Smart1b511972011-12-13 13:23:09 -05002459 if (phba->sli_rev < LPFC_SLI_REV4)
2460 lpfc_unreg_login(phba, 0, rpi, mbox);
2461 else
2462 lpfc_unreg_login(phba, phba->pport->vpi,
2463 phba->sli4_hba.rpi_ids[rpi], mbox);
2464
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 if (status != MBX_TIMEOUT)
2469 mempool_free(mbox, phba->mbox_mem_pool);
James Smartd439d282010-09-29 11:18:45 -04002470 return -EIO;
James Smart3b5dd522010-01-26 23:10:15 -05002471 }
James Smart3b5dd522010-01-26 23:10:15 -05002472 mempool_free(mbox, phba->mbox_mem_pool);
James Smart40426292010-12-15 17:58:10 -05002473 if (phba->sli_rev == LPFC_SLI_REV4)
2474 lpfc_sli4_free_rpi(phba, rpi);
James Smart3b5dd522010-01-26 23:10:15 -05002475 return 0;
2476}
2477
2478/**
2479 * lpfcdiag_loop_get_xri - obtains the transmit and receive ids
2480 * @phba: Pointer to HBA context object
2481 * @rpi: Remote port login id
2482 * @txxri: Pointer to transmit exchange id
2483 * @rxxri: Pointer to response exchabge id
2484 *
2485 * This function obtains the transmit and receive ids required to send
2486 * an unsolicited ct command with a payload. A special lpfc FsType and CmdRsp
2487 * flags are used to the unsolicted response handler is able to process
2488 * the ct command sent on the same port.
2489 **/
2490static int lpfcdiag_loop_get_xri(struct lpfc_hba *phba, uint16_t rpi,
2491 uint16_t *txxri, uint16_t * rxxri)
2492{
2493 struct lpfc_bsg_event *evt;
2494 struct lpfc_iocbq *cmdiocbq, *rspiocbq;
2495 IOCB_t *cmd, *rsp;
2496 struct lpfc_dmabuf *dmabuf;
2497 struct ulp_bde64 *bpl = NULL;
2498 struct lpfc_sli_ct_request *ctreq = NULL;
2499 int ret_val = 0;
James Smartd439d282010-09-29 11:18:45 -04002500 int time_left;
James Smart5a0916b2013-07-15 18:31:42 -04002501 int iocb_stat = IOCB_SUCCESS;
James Smart3b5dd522010-01-26 23:10:15 -05002502 unsigned long flags;
2503
2504 *txxri = 0;
2505 *rxxri = 0;
2506 evt = lpfc_bsg_event_new(FC_REG_CT_EVENT, current->pid,
2507 SLI_CT_ELX_LOOPBACK);
2508 if (!evt)
James Smartd439d282010-09-29 11:18:45 -04002509 return -ENOMEM;
James Smart3b5dd522010-01-26 23:10:15 -05002510
2511 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2512 list_add(&evt->node, &phba->ct_ev_waiters);
2513 lpfc_bsg_event_ref(evt);
2514 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2515
2516 cmdiocbq = lpfc_sli_get_iocbq(phba);
2517 rspiocbq = lpfc_sli_get_iocbq(phba);
2518
2519 dmabuf = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2520 if (dmabuf) {
2521 dmabuf->virt = lpfc_mbuf_alloc(phba, 0, &dmabuf->phys);
James Smartc7495932010-04-06 15:05:28 -04002522 if (dmabuf->virt) {
2523 INIT_LIST_HEAD(&dmabuf->list);
2524 bpl = (struct ulp_bde64 *) dmabuf->virt;
2525 memset(bpl, 0, sizeof(*bpl));
2526 ctreq = (struct lpfc_sli_ct_request *)(bpl + 1);
2527 bpl->addrHigh =
2528 le32_to_cpu(putPaddrHigh(dmabuf->phys +
2529 sizeof(*bpl)));
2530 bpl->addrLow =
2531 le32_to_cpu(putPaddrLow(dmabuf->phys +
2532 sizeof(*bpl)));
2533 bpl->tus.f.bdeFlags = 0;
2534 bpl->tus.f.bdeSize = ELX_LOOPBACK_HEADER_SZ;
2535 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2536 }
James Smart3b5dd522010-01-26 23:10:15 -05002537 }
2538
2539 if (cmdiocbq == NULL || rspiocbq == NULL ||
James Smartc7495932010-04-06 15:05:28 -04002540 dmabuf == NULL || bpl == NULL || ctreq == NULL ||
2541 dmabuf->virt == NULL) {
James Smartd439d282010-09-29 11:18:45 -04002542 ret_val = -ENOMEM;
James Smart3b5dd522010-01-26 23:10:15 -05002543 goto err_get_xri_exit;
2544 }
2545
2546 cmd = &cmdiocbq->iocb;
2547 rsp = &rspiocbq->iocb;
2548
2549 memset(ctreq, 0, ELX_LOOPBACK_HEADER_SZ);
2550
2551 ctreq->RevisionId.bits.Revision = SLI_CT_REVISION;
2552 ctreq->RevisionId.bits.InId = 0;
2553 ctreq->FsType = SLI_CT_ELX_LOOPBACK;
2554 ctreq->FsSubType = 0;
2555 ctreq->CommandResponse.bits.CmdRsp = ELX_LOOPBACK_XRI_SETUP;
2556 ctreq->CommandResponse.bits.Size = 0;
2557
2558
2559 cmd->un.xseq64.bdl.addrHigh = putPaddrHigh(dmabuf->phys);
2560 cmd->un.xseq64.bdl.addrLow = putPaddrLow(dmabuf->phys);
2561 cmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
2562 cmd->un.xseq64.bdl.bdeSize = sizeof(*bpl);
2563
2564 cmd->un.xseq64.w5.hcsw.Fctl = LA;
2565 cmd->un.xseq64.w5.hcsw.Dfctl = 0;
2566 cmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
2567 cmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
2568
2569 cmd->ulpCommand = CMD_XMIT_SEQUENCE64_CR;
2570 cmd->ulpBdeCount = 1;
2571 cmd->ulpLe = 1;
2572 cmd->ulpClass = CLASS3;
2573 cmd->ulpContext = rpi;
2574
2575 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
2576 cmdiocbq->vport = phba->pport;
James Smart5a0916b2013-07-15 18:31:42 -04002577 cmdiocbq->iocb_cmpl = NULL;
James Smart3b5dd522010-01-26 23:10:15 -05002578
James Smartd439d282010-09-29 11:18:45 -04002579 iocb_stat = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq,
James Smart3b5dd522010-01-26 23:10:15 -05002580 rspiocbq,
2581 (phba->fc_ratov * 2)
2582 + LPFC_DRVR_TIMEOUT);
James Smartd439d282010-09-29 11:18:45 -04002583 if (iocb_stat) {
2584 ret_val = -EIO;
James Smart3b5dd522010-01-26 23:10:15 -05002585 goto err_get_xri_exit;
James Smartd439d282010-09-29 11:18:45 -04002586 }
James Smart3b5dd522010-01-26 23:10:15 -05002587 *txxri = rsp->ulpContext;
2588
2589 evt->waiting = 1;
2590 evt->wait_time_stamp = jiffies;
James Smartd439d282010-09-29 11:18:45 -04002591 time_left = wait_event_interruptible_timeout(
James Smart3b5dd522010-01-26 23:10:15 -05002592 evt->wq, !list_empty(&evt->events_to_see),
James Smart256ec0d2013-04-17 20:14:58 -04002593 msecs_to_jiffies(1000 *
2594 ((phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT)));
James Smart3b5dd522010-01-26 23:10:15 -05002595 if (list_empty(&evt->events_to_see))
James Smartd439d282010-09-29 11:18:45 -04002596 ret_val = (time_left) ? -EINTR : -ETIMEDOUT;
James Smart3b5dd522010-01-26 23:10:15 -05002597 else {
James Smart3b5dd522010-01-26 23:10:15 -05002598 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2599 list_move(evt->events_to_see.prev, &evt->events_to_get);
2600 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2601 *rxxri = (list_entry(evt->events_to_get.prev,
2602 typeof(struct event_data),
2603 node))->immed_dat;
2604 }
2605 evt->waiting = 0;
2606
2607err_get_xri_exit:
2608 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2609 lpfc_bsg_event_unref(evt); /* release ref */
2610 lpfc_bsg_event_unref(evt); /* delete */
2611 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2612
2613 if (dmabuf) {
2614 if (dmabuf->virt)
2615 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
2616 kfree(dmabuf);
2617 }
2618
James Smartd439d282010-09-29 11:18:45 -04002619 if (cmdiocbq && (iocb_stat != IOCB_TIMEDOUT))
James Smart3b5dd522010-01-26 23:10:15 -05002620 lpfc_sli_release_iocbq(phba, cmdiocbq);
2621 if (rspiocbq)
2622 lpfc_sli_release_iocbq(phba, rspiocbq);
2623 return ret_val;
2624}
2625
2626/**
James Smart7ad20aa2011-05-24 11:44:28 -04002627 * lpfc_bsg_dma_page_alloc - allocate a bsg mbox page sized dma buffers
2628 * @phba: Pointer to HBA context object
2629 *
2630 * This function allocates BSG_MBOX_SIZE (4KB) page size dma buffer and.
2631 * retruns the pointer to the buffer.
2632 **/
2633static struct lpfc_dmabuf *
2634lpfc_bsg_dma_page_alloc(struct lpfc_hba *phba)
2635{
2636 struct lpfc_dmabuf *dmabuf;
2637 struct pci_dev *pcidev = phba->pcidev;
2638
2639 /* allocate dma buffer struct */
2640 dmabuf = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2641 if (!dmabuf)
2642 return NULL;
2643
2644 INIT_LIST_HEAD(&dmabuf->list);
2645
2646 /* now, allocate dma buffer */
2647 dmabuf->virt = dma_alloc_coherent(&pcidev->dev, BSG_MBOX_SIZE,
2648 &(dmabuf->phys), GFP_KERNEL);
2649
2650 if (!dmabuf->virt) {
2651 kfree(dmabuf);
2652 return NULL;
2653 }
2654 memset((uint8_t *)dmabuf->virt, 0, BSG_MBOX_SIZE);
2655
2656 return dmabuf;
2657}
2658
2659/**
2660 * lpfc_bsg_dma_page_free - free a bsg mbox page sized dma buffer
2661 * @phba: Pointer to HBA context object.
2662 * @dmabuf: Pointer to the bsg mbox page sized dma buffer descriptor.
2663 *
2664 * This routine just simply frees a dma buffer and its associated buffer
2665 * descriptor referred by @dmabuf.
2666 **/
2667static void
2668lpfc_bsg_dma_page_free(struct lpfc_hba *phba, struct lpfc_dmabuf *dmabuf)
2669{
2670 struct pci_dev *pcidev = phba->pcidev;
2671
2672 if (!dmabuf)
2673 return;
2674
2675 if (dmabuf->virt)
2676 dma_free_coherent(&pcidev->dev, BSG_MBOX_SIZE,
2677 dmabuf->virt, dmabuf->phys);
2678 kfree(dmabuf);
2679 return;
2680}
2681
2682/**
2683 * lpfc_bsg_dma_page_list_free - free a list of bsg mbox page sized dma buffers
2684 * @phba: Pointer to HBA context object.
2685 * @dmabuf_list: Pointer to a list of bsg mbox page sized dma buffer descs.
2686 *
2687 * This routine just simply frees all dma buffers and their associated buffer
2688 * descriptors referred by @dmabuf_list.
2689 **/
2690static void
2691lpfc_bsg_dma_page_list_free(struct lpfc_hba *phba,
2692 struct list_head *dmabuf_list)
2693{
2694 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
2695
2696 if (list_empty(dmabuf_list))
2697 return;
2698
2699 list_for_each_entry_safe(dmabuf, next_dmabuf, dmabuf_list, list) {
2700 list_del_init(&dmabuf->list);
2701 lpfc_bsg_dma_page_free(phba, dmabuf);
2702 }
2703 return;
2704}
2705
2706/**
James Smart3b5dd522010-01-26 23:10:15 -05002707 * diag_cmd_data_alloc - fills in a bde struct with dma buffers
2708 * @phba: Pointer to HBA context object
2709 * @bpl: Pointer to 64 bit bde structure
2710 * @size: Number of bytes to process
2711 * @nocopydata: Flag to copy user data into the allocated buffer
2712 *
2713 * This function allocates page size buffers and populates an lpfc_dmabufext.
2714 * If allowed the user data pointed to with indataptr is copied into the kernel
2715 * memory. The chained list of page size buffers is returned.
2716 **/
2717static struct lpfc_dmabufext *
2718diag_cmd_data_alloc(struct lpfc_hba *phba,
2719 struct ulp_bde64 *bpl, uint32_t size,
2720 int nocopydata)
2721{
2722 struct lpfc_dmabufext *mlist = NULL;
2723 struct lpfc_dmabufext *dmp;
2724 int cnt, offset = 0, i = 0;
2725 struct pci_dev *pcidev;
2726
2727 pcidev = phba->pcidev;
2728
2729 while (size) {
2730 /* We get chunks of 4K */
2731 if (size > BUF_SZ_4K)
2732 cnt = BUF_SZ_4K;
2733 else
2734 cnt = size;
2735
2736 /* allocate struct lpfc_dmabufext buffer header */
2737 dmp = kmalloc(sizeof(struct lpfc_dmabufext), GFP_KERNEL);
2738 if (!dmp)
2739 goto out;
2740
2741 INIT_LIST_HEAD(&dmp->dma.list);
2742
2743 /* Queue it to a linked list */
2744 if (mlist)
2745 list_add_tail(&dmp->dma.list, &mlist->dma.list);
2746 else
2747 mlist = dmp;
2748
2749 /* allocate buffer */
2750 dmp->dma.virt = dma_alloc_coherent(&pcidev->dev,
2751 cnt,
2752 &(dmp->dma.phys),
2753 GFP_KERNEL);
2754
2755 if (!dmp->dma.virt)
2756 goto out;
2757
2758 dmp->size = cnt;
2759
2760 if (nocopydata) {
2761 bpl->tus.f.bdeFlags = 0;
2762 pci_dma_sync_single_for_device(phba->pcidev,
2763 dmp->dma.phys, LPFC_BPL_SIZE, PCI_DMA_TODEVICE);
2764
2765 } else {
2766 memset((uint8_t *)dmp->dma.virt, 0, cnt);
2767 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2768 }
2769
2770 /* build buffer ptr list for IOCB */
2771 bpl->addrLow = le32_to_cpu(putPaddrLow(dmp->dma.phys));
2772 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dmp->dma.phys));
2773 bpl->tus.f.bdeSize = (ushort) cnt;
2774 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2775 bpl++;
2776
2777 i++;
2778 offset += cnt;
2779 size -= cnt;
2780 }
2781
2782 mlist->flag = i;
2783 return mlist;
2784out:
2785 diag_cmd_data_free(phba, mlist);
2786 return NULL;
2787}
2788
2789/**
2790 * lpfcdiag_loop_post_rxbufs - post the receive buffers for an unsol CT cmd
2791 * @phba: Pointer to HBA context object
2792 * @rxxri: Receive exchange id
2793 * @len: Number of data bytes
2794 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002795 * This function allocates and posts a data buffer of sufficient size to receive
James Smart3b5dd522010-01-26 23:10:15 -05002796 * an unsolicted CT command.
2797 **/
2798static int lpfcdiag_loop_post_rxbufs(struct lpfc_hba *phba, uint16_t rxxri,
2799 size_t len)
2800{
2801 struct lpfc_sli *psli = &phba->sli;
2802 struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
2803 struct lpfc_iocbq *cmdiocbq;
2804 IOCB_t *cmd = NULL;
2805 struct list_head head, *curr, *next;
2806 struct lpfc_dmabuf *rxbmp;
2807 struct lpfc_dmabuf *dmp;
2808 struct lpfc_dmabuf *mp[2] = {NULL, NULL};
2809 struct ulp_bde64 *rxbpl = NULL;
2810 uint32_t num_bde;
2811 struct lpfc_dmabufext *rxbuffer = NULL;
2812 int ret_val = 0;
James Smartd439d282010-09-29 11:18:45 -04002813 int iocb_stat;
James Smart3b5dd522010-01-26 23:10:15 -05002814 int i = 0;
2815
2816 cmdiocbq = lpfc_sli_get_iocbq(phba);
2817 rxbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2818 if (rxbmp != NULL) {
2819 rxbmp->virt = lpfc_mbuf_alloc(phba, 0, &rxbmp->phys);
James Smartc7495932010-04-06 15:05:28 -04002820 if (rxbmp->virt) {
2821 INIT_LIST_HEAD(&rxbmp->list);
2822 rxbpl = (struct ulp_bde64 *) rxbmp->virt;
2823 rxbuffer = diag_cmd_data_alloc(phba, rxbpl, len, 0);
2824 }
James Smart3b5dd522010-01-26 23:10:15 -05002825 }
2826
2827 if (!cmdiocbq || !rxbmp || !rxbpl || !rxbuffer) {
James Smartd439d282010-09-29 11:18:45 -04002828 ret_val = -ENOMEM;
James Smart3b5dd522010-01-26 23:10:15 -05002829 goto err_post_rxbufs_exit;
2830 }
2831
2832 /* Queue buffers for the receive exchange */
2833 num_bde = (uint32_t)rxbuffer->flag;
2834 dmp = &rxbuffer->dma;
2835
2836 cmd = &cmdiocbq->iocb;
2837 i = 0;
2838
2839 INIT_LIST_HEAD(&head);
2840 list_add_tail(&head, &dmp->list);
2841 list_for_each_safe(curr, next, &head) {
2842 mp[i] = list_entry(curr, struct lpfc_dmabuf, list);
2843 list_del(curr);
2844
2845 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
2846 mp[i]->buffer_tag = lpfc_sli_get_buffer_tag(phba);
2847 cmd->un.quexri64cx.buff.bde.addrHigh =
2848 putPaddrHigh(mp[i]->phys);
2849 cmd->un.quexri64cx.buff.bde.addrLow =
2850 putPaddrLow(mp[i]->phys);
2851 cmd->un.quexri64cx.buff.bde.tus.f.bdeSize =
2852 ((struct lpfc_dmabufext *)mp[i])->size;
2853 cmd->un.quexri64cx.buff.buffer_tag = mp[i]->buffer_tag;
2854 cmd->ulpCommand = CMD_QUE_XRI64_CX;
2855 cmd->ulpPU = 0;
2856 cmd->ulpLe = 1;
2857 cmd->ulpBdeCount = 1;
2858 cmd->unsli3.que_xri64cx_ext_words.ebde_count = 0;
2859
2860 } else {
2861 cmd->un.cont64[i].addrHigh = putPaddrHigh(mp[i]->phys);
2862 cmd->un.cont64[i].addrLow = putPaddrLow(mp[i]->phys);
2863 cmd->un.cont64[i].tus.f.bdeSize =
2864 ((struct lpfc_dmabufext *)mp[i])->size;
2865 cmd->ulpBdeCount = ++i;
2866
2867 if ((--num_bde > 0) && (i < 2))
2868 continue;
2869
2870 cmd->ulpCommand = CMD_QUE_XRI_BUF64_CX;
2871 cmd->ulpLe = 1;
2872 }
2873
2874 cmd->ulpClass = CLASS3;
2875 cmd->ulpContext = rxxri;
2876
James Smartd439d282010-09-29 11:18:45 -04002877 iocb_stat = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq,
2878 0);
2879 if (iocb_stat == IOCB_ERROR) {
James Smart3b5dd522010-01-26 23:10:15 -05002880 diag_cmd_data_free(phba,
2881 (struct lpfc_dmabufext *)mp[0]);
2882 if (mp[1])
2883 diag_cmd_data_free(phba,
2884 (struct lpfc_dmabufext *)mp[1]);
2885 dmp = list_entry(next, struct lpfc_dmabuf, list);
James Smartd439d282010-09-29 11:18:45 -04002886 ret_val = -EIO;
James Smart3b5dd522010-01-26 23:10:15 -05002887 goto err_post_rxbufs_exit;
2888 }
2889
2890 lpfc_sli_ringpostbuf_put(phba, pring, mp[0]);
2891 if (mp[1]) {
2892 lpfc_sli_ringpostbuf_put(phba, pring, mp[1]);
2893 mp[1] = NULL;
2894 }
2895
2896 /* The iocb was freed by lpfc_sli_issue_iocb */
2897 cmdiocbq = lpfc_sli_get_iocbq(phba);
2898 if (!cmdiocbq) {
2899 dmp = list_entry(next, struct lpfc_dmabuf, list);
James Smartd439d282010-09-29 11:18:45 -04002900 ret_val = -EIO;
James Smart3b5dd522010-01-26 23:10:15 -05002901 goto err_post_rxbufs_exit;
2902 }
2903
2904 cmd = &cmdiocbq->iocb;
2905 i = 0;
2906 }
2907 list_del(&head);
2908
2909err_post_rxbufs_exit:
2910
2911 if (rxbmp) {
2912 if (rxbmp->virt)
2913 lpfc_mbuf_free(phba, rxbmp->virt, rxbmp->phys);
2914 kfree(rxbmp);
2915 }
2916
2917 if (cmdiocbq)
2918 lpfc_sli_release_iocbq(phba, cmdiocbq);
2919 return ret_val;
2920}
2921
2922/**
James Smart7ad20aa2011-05-24 11:44:28 -04002923 * lpfc_bsg_diag_loopback_run - run loopback on a port by issue ct cmd to itself
James Smart3b5dd522010-01-26 23:10:15 -05002924 * @job: LPFC_BSG_VENDOR_DIAG_TEST fc_bsg_job
2925 *
2926 * This function receives a user data buffer to be transmitted and received on
2927 * the same port, the link must be up and in loopback mode prior
2928 * to being called.
2929 * 1. A kernel buffer is allocated to copy the user data into.
2930 * 2. The port registers with "itself".
2931 * 3. The transmit and receive exchange ids are obtained.
2932 * 4. The receive exchange id is posted.
2933 * 5. A new els loopback event is created.
2934 * 6. The command and response iocbs are allocated.
2935 * 7. The cmd iocb FsType is set to elx loopback and the CmdRsp to looppback.
2936 *
2937 * This function is meant to be called n times while the port is in loopback
2938 * so it is the apps responsibility to issue a reset to take the port out
2939 * of loopback mode.
2940 **/
2941static int
James Smart7ad20aa2011-05-24 11:44:28 -04002942lpfc_bsg_diag_loopback_run(struct fc_bsg_job *job)
James Smart3b5dd522010-01-26 23:10:15 -05002943{
2944 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
2945 struct lpfc_hba *phba = vport->phba;
2946 struct diag_mode_test *diag_mode;
2947 struct lpfc_bsg_event *evt;
2948 struct event_data *evdat;
2949 struct lpfc_sli *psli = &phba->sli;
2950 uint32_t size;
2951 uint32_t full_size;
2952 size_t segment_len = 0, segment_offset = 0, current_offset = 0;
James Smart40426292010-12-15 17:58:10 -05002953 uint16_t rpi = 0;
James Smart1b511972011-12-13 13:23:09 -05002954 struct lpfc_iocbq *cmdiocbq, *rspiocbq = NULL;
2955 IOCB_t *cmd, *rsp = NULL;
James Smart3b5dd522010-01-26 23:10:15 -05002956 struct lpfc_sli_ct_request *ctreq;
2957 struct lpfc_dmabuf *txbmp;
2958 struct ulp_bde64 *txbpl = NULL;
2959 struct lpfc_dmabufext *txbuffer = NULL;
2960 struct list_head head;
2961 struct lpfc_dmabuf *curr;
James Smart1b511972011-12-13 13:23:09 -05002962 uint16_t txxri = 0, rxxri;
James Smart3b5dd522010-01-26 23:10:15 -05002963 uint32_t num_bde;
2964 uint8_t *ptr = NULL, *rx_databuf = NULL;
2965 int rc = 0;
James Smartd439d282010-09-29 11:18:45 -04002966 int time_left;
James Smart5a0916b2013-07-15 18:31:42 -04002967 int iocb_stat = IOCB_SUCCESS;
James Smart3b5dd522010-01-26 23:10:15 -05002968 unsigned long flags;
2969 void *dataout = NULL;
2970 uint32_t total_mem;
2971
2972 /* in case no data is returned return just the return code */
2973 job->reply->reply_payload_rcv_len = 0;
2974
2975 if (job->request_len <
2976 sizeof(struct fc_bsg_request) + sizeof(struct diag_mode_test)) {
2977 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2978 "2739 Received DIAG TEST request below minimum "
2979 "size\n");
2980 rc = -EINVAL;
2981 goto loopback_test_exit;
2982 }
2983
2984 if (job->request_payload.payload_len !=
2985 job->reply_payload.payload_len) {
2986 rc = -EINVAL;
2987 goto loopback_test_exit;
2988 }
James Smart3b5dd522010-01-26 23:10:15 -05002989 diag_mode = (struct diag_mode_test *)
2990 job->request->rqst_data.h_vendor.vendor_cmd;
2991
2992 if ((phba->link_state == LPFC_HBA_ERROR) ||
2993 (psli->sli_flag & LPFC_BLOCK_MGMT_IO) ||
2994 (!(psli->sli_flag & LPFC_SLI_ACTIVE))) {
2995 rc = -EACCES;
2996 goto loopback_test_exit;
2997 }
2998
2999 if (!lpfc_is_link_up(phba) || !(phba->link_flag & LS_LOOPBACK_MODE)) {
3000 rc = -EACCES;
3001 goto loopback_test_exit;
3002 }
3003
3004 size = job->request_payload.payload_len;
3005 full_size = size + ELX_LOOPBACK_HEADER_SZ; /* plus the header */
3006
3007 if ((size == 0) || (size > 80 * BUF_SZ_4K)) {
3008 rc = -ERANGE;
3009 goto loopback_test_exit;
3010 }
3011
James Smart63e801c2010-11-20 23:14:19 -05003012 if (full_size >= BUF_SZ_4K) {
James Smart3b5dd522010-01-26 23:10:15 -05003013 /*
3014 * Allocate memory for ioctl data. If buffer is bigger than 64k,
3015 * then we allocate 64k and re-use that buffer over and over to
3016 * xfer the whole block. This is because Linux kernel has a
3017 * problem allocating more than 120k of kernel space memory. Saw
3018 * problem with GET_FCPTARGETMAPPING...
3019 */
3020 if (size <= (64 * 1024))
James Smart63e801c2010-11-20 23:14:19 -05003021 total_mem = full_size;
James Smart3b5dd522010-01-26 23:10:15 -05003022 else
3023 total_mem = 64 * 1024;
3024 } else
3025 /* Allocate memory for ioctl data */
3026 total_mem = BUF_SZ_4K;
3027
3028 dataout = kmalloc(total_mem, GFP_KERNEL);
3029 if (dataout == NULL) {
3030 rc = -ENOMEM;
3031 goto loopback_test_exit;
3032 }
3033
3034 ptr = dataout;
3035 ptr += ELX_LOOPBACK_HEADER_SZ;
3036 sg_copy_to_buffer(job->request_payload.sg_list,
3037 job->request_payload.sg_cnt,
3038 ptr, size);
James Smart3b5dd522010-01-26 23:10:15 -05003039 rc = lpfcdiag_loop_self_reg(phba, &rpi);
James Smartd439d282010-09-29 11:18:45 -04003040 if (rc)
James Smart3b5dd522010-01-26 23:10:15 -05003041 goto loopback_test_exit;
James Smart3b5dd522010-01-26 23:10:15 -05003042
James Smart1b511972011-12-13 13:23:09 -05003043 if (phba->sli_rev < LPFC_SLI_REV4) {
3044 rc = lpfcdiag_loop_get_xri(phba, rpi, &txxri, &rxxri);
3045 if (rc) {
3046 lpfcdiag_loop_self_unreg(phba, rpi);
3047 goto loopback_test_exit;
3048 }
James Smart3b5dd522010-01-26 23:10:15 -05003049
James Smart1b511972011-12-13 13:23:09 -05003050 rc = lpfcdiag_loop_post_rxbufs(phba, rxxri, full_size);
3051 if (rc) {
3052 lpfcdiag_loop_self_unreg(phba, rpi);
3053 goto loopback_test_exit;
3054 }
James Smart3b5dd522010-01-26 23:10:15 -05003055 }
James Smart3b5dd522010-01-26 23:10:15 -05003056 evt = lpfc_bsg_event_new(FC_REG_CT_EVENT, current->pid,
3057 SLI_CT_ELX_LOOPBACK);
3058 if (!evt) {
3059 lpfcdiag_loop_self_unreg(phba, rpi);
3060 rc = -ENOMEM;
3061 goto loopback_test_exit;
3062 }
3063
3064 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3065 list_add(&evt->node, &phba->ct_ev_waiters);
3066 lpfc_bsg_event_ref(evt);
3067 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3068
3069 cmdiocbq = lpfc_sli_get_iocbq(phba);
James Smart1b511972011-12-13 13:23:09 -05003070 if (phba->sli_rev < LPFC_SLI_REV4)
3071 rspiocbq = lpfc_sli_get_iocbq(phba);
James Smart3b5dd522010-01-26 23:10:15 -05003072 txbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3073
3074 if (txbmp) {
3075 txbmp->virt = lpfc_mbuf_alloc(phba, 0, &txbmp->phys);
James Smartc7495932010-04-06 15:05:28 -04003076 if (txbmp->virt) {
3077 INIT_LIST_HEAD(&txbmp->list);
3078 txbpl = (struct ulp_bde64 *) txbmp->virt;
James Smart3b5dd522010-01-26 23:10:15 -05003079 txbuffer = diag_cmd_data_alloc(phba,
3080 txbpl, full_size, 0);
James Smartc7495932010-04-06 15:05:28 -04003081 }
James Smart3b5dd522010-01-26 23:10:15 -05003082 }
3083
James Smart1b511972011-12-13 13:23:09 -05003084 if (!cmdiocbq || !txbmp || !txbpl || !txbuffer || !txbmp->virt) {
3085 rc = -ENOMEM;
3086 goto err_loopback_test_exit;
3087 }
3088 if ((phba->sli_rev < LPFC_SLI_REV4) && !rspiocbq) {
James Smart3b5dd522010-01-26 23:10:15 -05003089 rc = -ENOMEM;
3090 goto err_loopback_test_exit;
3091 }
3092
3093 cmd = &cmdiocbq->iocb;
James Smart1b511972011-12-13 13:23:09 -05003094 if (phba->sli_rev < LPFC_SLI_REV4)
3095 rsp = &rspiocbq->iocb;
James Smart3b5dd522010-01-26 23:10:15 -05003096
3097 INIT_LIST_HEAD(&head);
3098 list_add_tail(&head, &txbuffer->dma.list);
3099 list_for_each_entry(curr, &head, list) {
3100 segment_len = ((struct lpfc_dmabufext *)curr)->size;
3101 if (current_offset == 0) {
3102 ctreq = curr->virt;
3103 memset(ctreq, 0, ELX_LOOPBACK_HEADER_SZ);
3104 ctreq->RevisionId.bits.Revision = SLI_CT_REVISION;
3105 ctreq->RevisionId.bits.InId = 0;
3106 ctreq->FsType = SLI_CT_ELX_LOOPBACK;
3107 ctreq->FsSubType = 0;
3108 ctreq->CommandResponse.bits.CmdRsp = ELX_LOOPBACK_DATA;
3109 ctreq->CommandResponse.bits.Size = size;
3110 segment_offset = ELX_LOOPBACK_HEADER_SZ;
3111 } else
3112 segment_offset = 0;
3113
3114 BUG_ON(segment_offset >= segment_len);
3115 memcpy(curr->virt + segment_offset,
3116 ptr + current_offset,
3117 segment_len - segment_offset);
3118
3119 current_offset += segment_len - segment_offset;
3120 BUG_ON(current_offset > size);
3121 }
3122 list_del(&head);
3123
3124 /* Build the XMIT_SEQUENCE iocb */
James Smart3b5dd522010-01-26 23:10:15 -05003125 num_bde = (uint32_t)txbuffer->flag;
3126
3127 cmd->un.xseq64.bdl.addrHigh = putPaddrHigh(txbmp->phys);
3128 cmd->un.xseq64.bdl.addrLow = putPaddrLow(txbmp->phys);
3129 cmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
3130 cmd->un.xseq64.bdl.bdeSize = (num_bde * sizeof(struct ulp_bde64));
3131
3132 cmd->un.xseq64.w5.hcsw.Fctl = (LS | LA);
3133 cmd->un.xseq64.w5.hcsw.Dfctl = 0;
3134 cmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
3135 cmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
3136
3137 cmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;
3138 cmd->ulpBdeCount = 1;
3139 cmd->ulpLe = 1;
3140 cmd->ulpClass = CLASS3;
James Smart3b5dd522010-01-26 23:10:15 -05003141
James Smart1b511972011-12-13 13:23:09 -05003142 if (phba->sli_rev < LPFC_SLI_REV4) {
3143 cmd->ulpContext = txxri;
3144 } else {
3145 cmd->un.xseq64.bdl.ulpIoTag32 = 0;
3146 cmd->un.ulpWord[3] = phba->sli4_hba.rpi_ids[rpi];
3147 cmdiocbq->context3 = txbmp;
3148 cmdiocbq->sli4_xritag = NO_XRI;
3149 cmd->unsli3.rcvsli3.ox_id = 0xffff;
3150 }
James Smart3b5dd522010-01-26 23:10:15 -05003151 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
3152 cmdiocbq->vport = phba->pport;
James Smart5a0916b2013-07-15 18:31:42 -04003153 cmdiocbq->iocb_cmpl = NULL;
James Smartd439d282010-09-29 11:18:45 -04003154 iocb_stat = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq,
3155 rspiocbq, (phba->fc_ratov * 2) +
3156 LPFC_DRVR_TIMEOUT);
James Smart3b5dd522010-01-26 23:10:15 -05003157
James Smart1b511972011-12-13 13:23:09 -05003158 if ((iocb_stat != IOCB_SUCCESS) || ((phba->sli_rev < LPFC_SLI_REV4) &&
3159 (rsp->ulpStatus != IOCB_SUCCESS))) {
3160 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3161 "3126 Failed loopback test issue iocb: "
3162 "iocb_stat:x%x\n", iocb_stat);
James Smart3b5dd522010-01-26 23:10:15 -05003163 rc = -EIO;
3164 goto err_loopback_test_exit;
3165 }
3166
3167 evt->waiting = 1;
James Smartd439d282010-09-29 11:18:45 -04003168 time_left = wait_event_interruptible_timeout(
James Smart3b5dd522010-01-26 23:10:15 -05003169 evt->wq, !list_empty(&evt->events_to_see),
James Smart256ec0d2013-04-17 20:14:58 -04003170 msecs_to_jiffies(1000 *
3171 ((phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT)));
James Smart3b5dd522010-01-26 23:10:15 -05003172 evt->waiting = 0;
James Smart1b511972011-12-13 13:23:09 -05003173 if (list_empty(&evt->events_to_see)) {
James Smartd439d282010-09-29 11:18:45 -04003174 rc = (time_left) ? -EINTR : -ETIMEDOUT;
James Smart1b511972011-12-13 13:23:09 -05003175 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3176 "3125 Not receiving unsolicited event, "
3177 "rc:x%x\n", rc);
3178 } else {
James Smart3b5dd522010-01-26 23:10:15 -05003179 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3180 list_move(evt->events_to_see.prev, &evt->events_to_get);
3181 evdat = list_entry(evt->events_to_get.prev,
3182 typeof(*evdat), node);
3183 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3184 rx_databuf = evdat->data;
3185 if (evdat->len != full_size) {
3186 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3187 "1603 Loopback test did not receive expected "
3188 "data length. actual length 0x%x expected "
3189 "length 0x%x\n",
3190 evdat->len, full_size);
3191 rc = -EIO;
3192 } else if (rx_databuf == NULL)
3193 rc = -EIO;
3194 else {
3195 rc = IOCB_SUCCESS;
3196 /* skip over elx loopback header */
3197 rx_databuf += ELX_LOOPBACK_HEADER_SZ;
3198 job->reply->reply_payload_rcv_len =
3199 sg_copy_from_buffer(job->reply_payload.sg_list,
3200 job->reply_payload.sg_cnt,
3201 rx_databuf, size);
3202 job->reply->reply_payload_rcv_len = size;
3203 }
3204 }
3205
3206err_loopback_test_exit:
3207 lpfcdiag_loop_self_unreg(phba, rpi);
3208
3209 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3210 lpfc_bsg_event_unref(evt); /* release ref */
3211 lpfc_bsg_event_unref(evt); /* delete */
3212 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3213
James Smart5a0916b2013-07-15 18:31:42 -04003214 if ((cmdiocbq != NULL) && (iocb_stat != IOCB_TIMEDOUT))
James Smart3b5dd522010-01-26 23:10:15 -05003215 lpfc_sli_release_iocbq(phba, cmdiocbq);
3216
3217 if (rspiocbq != NULL)
3218 lpfc_sli_release_iocbq(phba, rspiocbq);
3219
3220 if (txbmp != NULL) {
3221 if (txbpl != NULL) {
3222 if (txbuffer != NULL)
3223 diag_cmd_data_free(phba, txbuffer);
3224 lpfc_mbuf_free(phba, txbmp->virt, txbmp->phys);
3225 }
3226 kfree(txbmp);
3227 }
3228
3229loopback_test_exit:
3230 kfree(dataout);
3231 /* make error code available to userspace */
3232 job->reply->result = rc;
3233 job->dd_data = NULL;
3234 /* complete the job back to userspace if no error */
James Smart1b511972011-12-13 13:23:09 -05003235 if (rc == IOCB_SUCCESS)
James Smart3b5dd522010-01-26 23:10:15 -05003236 job->job_done(job);
3237 return rc;
3238}
3239
3240/**
3241 * lpfc_bsg_get_dfc_rev - process a GET_DFC_REV bsg vendor command
3242 * @job: GET_DFC_REV fc_bsg_job
3243 **/
3244static int
3245lpfc_bsg_get_dfc_rev(struct fc_bsg_job *job)
3246{
3247 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
3248 struct lpfc_hba *phba = vport->phba;
3249 struct get_mgmt_rev *event_req;
3250 struct get_mgmt_rev_reply *event_reply;
3251 int rc = 0;
3252
3253 if (job->request_len <
3254 sizeof(struct fc_bsg_request) + sizeof(struct get_mgmt_rev)) {
3255 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3256 "2740 Received GET_DFC_REV request below "
3257 "minimum size\n");
3258 rc = -EINVAL;
3259 goto job_error;
3260 }
3261
3262 event_req = (struct get_mgmt_rev *)
3263 job->request->rqst_data.h_vendor.vendor_cmd;
3264
3265 event_reply = (struct get_mgmt_rev_reply *)
3266 job->reply->reply_data.vendor_reply.vendor_rsp;
3267
3268 if (job->reply_len <
3269 sizeof(struct fc_bsg_request) + sizeof(struct get_mgmt_rev_reply)) {
3270 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3271 "2741 Received GET_DFC_REV reply below "
3272 "minimum size\n");
3273 rc = -EINVAL;
3274 goto job_error;
3275 }
3276
3277 event_reply->info.a_Major = MANAGEMENT_MAJOR_REV;
3278 event_reply->info.a_Minor = MANAGEMENT_MINOR_REV;
3279job_error:
3280 job->reply->result = rc;
3281 if (rc == 0)
3282 job->job_done(job);
3283 return rc;
3284}
3285
3286/**
James Smart7ad20aa2011-05-24 11:44:28 -04003287 * lpfc_bsg_issue_mbox_cmpl - lpfc_bsg_issue_mbox mbox completion handler
James Smart3b5dd522010-01-26 23:10:15 -05003288 * @phba: Pointer to HBA context object.
3289 * @pmboxq: Pointer to mailbox command.
3290 *
3291 * This is completion handler function for mailbox commands issued from
3292 * lpfc_bsg_issue_mbox function. This function is called by the
3293 * mailbox event handler function with no lock held. This function
3294 * will wake up thread waiting on the wait queue pointed by context1
3295 * of the mailbox.
3296 **/
3297void
James Smart7ad20aa2011-05-24 11:44:28 -04003298lpfc_bsg_issue_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
James Smart3b5dd522010-01-26 23:10:15 -05003299{
3300 struct bsg_job_data *dd_data;
James Smart3b5dd522010-01-26 23:10:15 -05003301 struct fc_bsg_job *job;
3302 uint32_t size;
3303 unsigned long flags;
James Smart7ad20aa2011-05-24 11:44:28 -04003304 uint8_t *pmb, *pmb_buf;
James Smart3b5dd522010-01-26 23:10:15 -05003305
James Smart3b5dd522010-01-26 23:10:15 -05003306 dd_data = pmboxq->context1;
James Smart3b5dd522010-01-26 23:10:15 -05003307
James Smart7ad20aa2011-05-24 11:44:28 -04003308 /*
3309 * The outgoing buffer is readily referred from the dma buffer,
3310 * just need to get header part from mailboxq structure.
James Smart7a470272010-03-15 11:25:20 -04003311 */
James Smart7ad20aa2011-05-24 11:44:28 -04003312 pmb = (uint8_t *)&pmboxq->u.mb;
3313 pmb_buf = (uint8_t *)dd_data->context_un.mbox.mb;
3314 memcpy(pmb_buf, pmb, sizeof(MAILBOX_t));
James Smart515e0aa2010-09-29 11:19:00 -04003315
James Smarta33c4f72013-03-01 16:36:00 -05003316 /* Determine if job has been aborted */
3317
3318 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3319 job = dd_data->set_job;
3320 if (job) {
3321 /* Prevent timeout handling from trying to abort job */
3322 job->dd_data = NULL;
3323 }
3324 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3325
3326 /* Copy the mailbox data to the job if it is still active */
3327
James Smart5a6f1332011-03-11 16:05:35 -05003328 if (job) {
3329 size = job->reply_payload.payload_len;
3330 job->reply->reply_payload_rcv_len =
3331 sg_copy_from_buffer(job->reply_payload.sg_list,
James Smart7ad20aa2011-05-24 11:44:28 -04003332 job->reply_payload.sg_cnt,
3333 pmb_buf, size);
James Smart5a6f1332011-03-11 16:05:35 -05003334 }
James Smart7a470272010-03-15 11:25:20 -04003335
James Smarta33c4f72013-03-01 16:36:00 -05003336 dd_data->set_job = NULL;
James Smart3b5dd522010-01-26 23:10:15 -05003337 mempool_free(dd_data->context_un.mbox.pmboxq, phba->mbox_mem_pool);
James Smart7ad20aa2011-05-24 11:44:28 -04003338 lpfc_bsg_dma_page_free(phba, dd_data->context_un.mbox.dmabuffers);
James Smart3b5dd522010-01-26 23:10:15 -05003339 kfree(dd_data);
James Smart7ad20aa2011-05-24 11:44:28 -04003340
James Smarta33c4f72013-03-01 16:36:00 -05003341 /* Complete the job if the job is still active */
3342
James Smart7ad20aa2011-05-24 11:44:28 -04003343 if (job) {
3344 job->reply->result = 0;
3345 job->job_done(job);
3346 }
James Smart3b5dd522010-01-26 23:10:15 -05003347 return;
3348}
3349
3350/**
3351 * lpfc_bsg_check_cmd_access - test for a supported mailbox command
3352 * @phba: Pointer to HBA context object.
3353 * @mb: Pointer to a mailbox object.
3354 * @vport: Pointer to a vport object.
3355 *
3356 * Some commands require the port to be offline, some may not be called from
3357 * the application.
3358 **/
3359static int lpfc_bsg_check_cmd_access(struct lpfc_hba *phba,
3360 MAILBOX_t *mb, struct lpfc_vport *vport)
3361{
3362 /* return negative error values for bsg job */
3363 switch (mb->mbxCommand) {
3364 /* Offline only */
3365 case MBX_INIT_LINK:
3366 case MBX_DOWN_LINK:
3367 case MBX_CONFIG_LINK:
3368 case MBX_CONFIG_RING:
3369 case MBX_RESET_RING:
3370 case MBX_UNREG_LOGIN:
3371 case MBX_CLEAR_LA:
3372 case MBX_DUMP_CONTEXT:
3373 case MBX_RUN_DIAGS:
3374 case MBX_RESTART:
3375 case MBX_SET_MASK:
3376 if (!(vport->fc_flag & FC_OFFLINE_MODE)) {
3377 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3378 "2743 Command 0x%x is illegal in on-line "
3379 "state\n",
3380 mb->mbxCommand);
3381 return -EPERM;
3382 }
3383 case MBX_WRITE_NV:
3384 case MBX_WRITE_VPARMS:
3385 case MBX_LOAD_SM:
3386 case MBX_READ_NV:
3387 case MBX_READ_CONFIG:
3388 case MBX_READ_RCONFIG:
3389 case MBX_READ_STATUS:
3390 case MBX_READ_XRI:
3391 case MBX_READ_REV:
3392 case MBX_READ_LNK_STAT:
3393 case MBX_DUMP_MEMORY:
3394 case MBX_DOWN_LOAD:
3395 case MBX_UPDATE_CFG:
3396 case MBX_KILL_BOARD:
James Smart06f35552013-05-31 17:04:50 -04003397 case MBX_READ_TOPOLOGY:
James Smart3b5dd522010-01-26 23:10:15 -05003398 case MBX_LOAD_AREA:
3399 case MBX_LOAD_EXP_ROM:
3400 case MBX_BEACON:
3401 case MBX_DEL_LD_ENTRY:
3402 case MBX_SET_DEBUG:
3403 case MBX_WRITE_WWN:
3404 case MBX_SLI4_CONFIG:
James Smartc7495932010-04-06 15:05:28 -04003405 case MBX_READ_EVENT_LOG:
James Smart3b5dd522010-01-26 23:10:15 -05003406 case MBX_READ_EVENT_LOG_STATUS:
3407 case MBX_WRITE_EVENT_LOG:
3408 case MBX_PORT_CAPABILITIES:
3409 case MBX_PORT_IOV_CONTROL:
James Smart7a470272010-03-15 11:25:20 -04003410 case MBX_RUN_BIU_DIAG64:
James Smart3b5dd522010-01-26 23:10:15 -05003411 break;
3412 case MBX_SET_VARIABLE:
James Smarte2aed292010-02-26 14:15:00 -05003413 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3414 "1226 mbox: set_variable 0x%x, 0x%x\n",
3415 mb->un.varWords[0],
3416 mb->un.varWords[1]);
3417 if ((mb->un.varWords[0] == SETVAR_MLOMNT)
3418 && (mb->un.varWords[1] == 1)) {
3419 phba->wait_4_mlo_maint_flg = 1;
3420 } else if (mb->un.varWords[0] == SETVAR_MLORST) {
James Smart1b511972011-12-13 13:23:09 -05003421 spin_lock_irq(&phba->hbalock);
James Smarte2aed292010-02-26 14:15:00 -05003422 phba->link_flag &= ~LS_LOOPBACK_MODE;
James Smart1b511972011-12-13 13:23:09 -05003423 spin_unlock_irq(&phba->hbalock);
James Smart76a95d72010-11-20 23:11:48 -05003424 phba->fc_topology = LPFC_TOPOLOGY_PT_PT;
James Smarte2aed292010-02-26 14:15:00 -05003425 }
3426 break;
James Smart3b5dd522010-01-26 23:10:15 -05003427 case MBX_READ_SPARM64:
James Smart3b5dd522010-01-26 23:10:15 -05003428 case MBX_REG_LOGIN:
3429 case MBX_REG_LOGIN64:
3430 case MBX_CONFIG_PORT:
3431 case MBX_RUN_BIU_DIAG:
3432 default:
3433 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3434 "2742 Unknown Command 0x%x\n",
3435 mb->mbxCommand);
3436 return -EPERM;
3437 }
3438
3439 return 0; /* ok */
3440}
3441
3442/**
James Smart7ad20aa2011-05-24 11:44:28 -04003443 * lpfc_bsg_mbox_ext_cleanup - clean up context of multi-buffer mbox session
3444 * @phba: Pointer to HBA context object.
3445 *
3446 * This is routine clean up and reset BSG handling of multi-buffer mbox
3447 * command session.
3448 **/
3449static void
3450lpfc_bsg_mbox_ext_session_reset(struct lpfc_hba *phba)
3451{
3452 if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_IDLE)
3453 return;
3454
3455 /* free all memory, including dma buffers */
3456 lpfc_bsg_dma_page_list_free(phba,
3457 &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
3458 lpfc_bsg_dma_page_free(phba, phba->mbox_ext_buf_ctx.mbx_dmabuf);
3459 /* multi-buffer write mailbox command pass-through complete */
3460 memset((char *)&phba->mbox_ext_buf_ctx, 0,
3461 sizeof(struct lpfc_mbox_ext_buf_ctx));
3462 INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
3463
3464 return;
3465}
3466
3467/**
3468 * lpfc_bsg_issue_mbox_ext_handle_job - job handler for multi-buffer mbox cmpl
3469 * @phba: Pointer to HBA context object.
3470 * @pmboxq: Pointer to mailbox command.
3471 *
3472 * This is routine handles BSG job for mailbox commands completions with
3473 * multiple external buffers.
3474 **/
3475static struct fc_bsg_job *
3476lpfc_bsg_issue_mbox_ext_handle_job(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
3477{
3478 struct bsg_job_data *dd_data;
3479 struct fc_bsg_job *job;
3480 uint8_t *pmb, *pmb_buf;
3481 unsigned long flags;
3482 uint32_t size;
3483 int rc = 0;
James Smart026abb82011-12-13 13:20:45 -05003484 struct lpfc_dmabuf *dmabuf;
3485 struct lpfc_sli_config_mbox *sli_cfg_mbx;
3486 uint8_t *pmbx;
James Smart7ad20aa2011-05-24 11:44:28 -04003487
James Smart7ad20aa2011-05-24 11:44:28 -04003488 dd_data = pmboxq->context1;
James Smarta33c4f72013-03-01 16:36:00 -05003489
3490 /* Determine if job has been aborted */
3491 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3492 job = dd_data->set_job;
3493 if (job) {
3494 /* Prevent timeout handling from trying to abort job */
3495 job->dd_data = NULL;
James Smart7ad20aa2011-05-24 11:44:28 -04003496 }
James Smarta33c4f72013-03-01 16:36:00 -05003497 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smart7ad20aa2011-05-24 11:44:28 -04003498
3499 /*
3500 * The outgoing buffer is readily referred from the dma buffer,
3501 * just need to get header part from mailboxq structure.
3502 */
James Smarta33c4f72013-03-01 16:36:00 -05003503
James Smart7ad20aa2011-05-24 11:44:28 -04003504 pmb = (uint8_t *)&pmboxq->u.mb;
3505 pmb_buf = (uint8_t *)dd_data->context_un.mbox.mb;
James Smart026abb82011-12-13 13:20:45 -05003506 /* Copy the byte swapped response mailbox back to the user */
James Smart7ad20aa2011-05-24 11:44:28 -04003507 memcpy(pmb_buf, pmb, sizeof(MAILBOX_t));
James Smart026abb82011-12-13 13:20:45 -05003508 /* if there is any non-embedded extended data copy that too */
3509 dmabuf = phba->mbox_ext_buf_ctx.mbx_dmabuf;
3510 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
3511 if (!bsg_bf_get(lpfc_mbox_hdr_emb,
3512 &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) {
3513 pmbx = (uint8_t *)dmabuf->virt;
3514 /* byte swap the extended data following the mailbox command */
3515 lpfc_sli_pcimem_bcopy(&pmbx[sizeof(MAILBOX_t)],
3516 &pmbx[sizeof(MAILBOX_t)],
3517 sli_cfg_mbx->un.sli_config_emb0_subsys.mse[0].buf_len);
3518 }
James Smart7ad20aa2011-05-24 11:44:28 -04003519
James Smarta33c4f72013-03-01 16:36:00 -05003520 /* Complete the job if the job is still active */
3521
James Smart7ad20aa2011-05-24 11:44:28 -04003522 if (job) {
3523 size = job->reply_payload.payload_len;
3524 job->reply->reply_payload_rcv_len =
3525 sg_copy_from_buffer(job->reply_payload.sg_list,
3526 job->reply_payload.sg_cnt,
3527 pmb_buf, size);
James Smarta33c4f72013-03-01 16:36:00 -05003528
James Smart7ad20aa2011-05-24 11:44:28 -04003529 /* result for successful */
3530 job->reply->result = 0;
James Smarta33c4f72013-03-01 16:36:00 -05003531
James Smart7ad20aa2011-05-24 11:44:28 -04003532 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3533 "2937 SLI_CONFIG ext-buffer maibox command "
3534 "(x%x/x%x) complete bsg job done, bsize:%d\n",
3535 phba->mbox_ext_buf_ctx.nembType,
3536 phba->mbox_ext_buf_ctx.mboxType, size);
James Smartb76f2dc2011-07-22 18:37:42 -04003537 lpfc_idiag_mbxacc_dump_bsg_mbox(phba,
3538 phba->mbox_ext_buf_ctx.nembType,
3539 phba->mbox_ext_buf_ctx.mboxType,
3540 dma_ebuf, sta_pos_addr,
3541 phba->mbox_ext_buf_ctx.mbx_dmabuf, 0);
James Smarta33c4f72013-03-01 16:36:00 -05003542 } else {
James Smart7ad20aa2011-05-24 11:44:28 -04003543 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3544 "2938 SLI_CONFIG ext-buffer maibox "
3545 "command (x%x/x%x) failure, rc:x%x\n",
3546 phba->mbox_ext_buf_ctx.nembType,
3547 phba->mbox_ext_buf_ctx.mboxType, rc);
James Smarta33c4f72013-03-01 16:36:00 -05003548 }
3549
3550
James Smart7ad20aa2011-05-24 11:44:28 -04003551 /* state change */
3552 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_DONE;
3553 kfree(dd_data);
James Smart7ad20aa2011-05-24 11:44:28 -04003554 return job;
3555}
3556
3557/**
3558 * lpfc_bsg_issue_read_mbox_ext_cmpl - compl handler for multi-buffer read mbox
3559 * @phba: Pointer to HBA context object.
3560 * @pmboxq: Pointer to mailbox command.
3561 *
3562 * This is completion handler function for mailbox read commands with multiple
3563 * external buffers.
3564 **/
3565static void
3566lpfc_bsg_issue_read_mbox_ext_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
3567{
3568 struct fc_bsg_job *job;
3569
James Smarta33c4f72013-03-01 16:36:00 -05003570 job = lpfc_bsg_issue_mbox_ext_handle_job(phba, pmboxq);
3571
James Smart7ad20aa2011-05-24 11:44:28 -04003572 /* handle the BSG job with mailbox command */
James Smarta33c4f72013-03-01 16:36:00 -05003573 if (!job)
James Smart7ad20aa2011-05-24 11:44:28 -04003574 pmboxq->u.mb.mbxStatus = MBXERR_ERROR;
3575
3576 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3577 "2939 SLI_CONFIG ext-buffer rd maibox command "
3578 "complete, ctxState:x%x, mbxStatus:x%x\n",
3579 phba->mbox_ext_buf_ctx.state, pmboxq->u.mb.mbxStatus);
3580
James Smart7ad20aa2011-05-24 11:44:28 -04003581 if (pmboxq->u.mb.mbxStatus || phba->mbox_ext_buf_ctx.numBuf == 1)
3582 lpfc_bsg_mbox_ext_session_reset(phba);
3583
3584 /* free base driver mailbox structure memory */
3585 mempool_free(pmboxq, phba->mbox_mem_pool);
3586
James Smarta33c4f72013-03-01 16:36:00 -05003587 /* if the job is still active, call job done */
James Smart7ad20aa2011-05-24 11:44:28 -04003588 if (job)
3589 job->job_done(job);
3590
3591 return;
3592}
3593
3594/**
3595 * lpfc_bsg_issue_write_mbox_ext_cmpl - cmpl handler for multi-buffer write mbox
3596 * @phba: Pointer to HBA context object.
3597 * @pmboxq: Pointer to mailbox command.
3598 *
3599 * This is completion handler function for mailbox write commands with multiple
3600 * external buffers.
3601 **/
3602static void
3603lpfc_bsg_issue_write_mbox_ext_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
3604{
3605 struct fc_bsg_job *job;
3606
James Smarta33c4f72013-03-01 16:36:00 -05003607 job = lpfc_bsg_issue_mbox_ext_handle_job(phba, pmboxq);
3608
James Smart7ad20aa2011-05-24 11:44:28 -04003609 /* handle the BSG job with the mailbox command */
James Smarta33c4f72013-03-01 16:36:00 -05003610 if (!job)
James Smart7ad20aa2011-05-24 11:44:28 -04003611 pmboxq->u.mb.mbxStatus = MBXERR_ERROR;
3612
3613 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3614 "2940 SLI_CONFIG ext-buffer wr maibox command "
3615 "complete, ctxState:x%x, mbxStatus:x%x\n",
3616 phba->mbox_ext_buf_ctx.state, pmboxq->u.mb.mbxStatus);
3617
James Smart7ad20aa2011-05-24 11:44:28 -04003618 /* free all memory, including dma buffers */
3619 mempool_free(pmboxq, phba->mbox_mem_pool);
3620 lpfc_bsg_mbox_ext_session_reset(phba);
3621
James Smarta33c4f72013-03-01 16:36:00 -05003622 /* if the job is still active, call job done */
James Smart7ad20aa2011-05-24 11:44:28 -04003623 if (job)
3624 job->job_done(job);
3625
3626 return;
3627}
3628
3629static void
3630lpfc_bsg_sli_cfg_dma_desc_setup(struct lpfc_hba *phba, enum nemb_type nemb_tp,
3631 uint32_t index, struct lpfc_dmabuf *mbx_dmabuf,
3632 struct lpfc_dmabuf *ext_dmabuf)
3633{
3634 struct lpfc_sli_config_mbox *sli_cfg_mbx;
3635
3636 /* pointer to the start of mailbox command */
3637 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)mbx_dmabuf->virt;
3638
3639 if (nemb_tp == nemb_mse) {
3640 if (index == 0) {
3641 sli_cfg_mbx->un.sli_config_emb0_subsys.
3642 mse[index].pa_hi =
3643 putPaddrHigh(mbx_dmabuf->phys +
3644 sizeof(MAILBOX_t));
3645 sli_cfg_mbx->un.sli_config_emb0_subsys.
3646 mse[index].pa_lo =
3647 putPaddrLow(mbx_dmabuf->phys +
3648 sizeof(MAILBOX_t));
3649 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3650 "2943 SLI_CONFIG(mse)[%d], "
3651 "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3652 index,
3653 sli_cfg_mbx->un.sli_config_emb0_subsys.
3654 mse[index].buf_len,
3655 sli_cfg_mbx->un.sli_config_emb0_subsys.
3656 mse[index].pa_hi,
3657 sli_cfg_mbx->un.sli_config_emb0_subsys.
3658 mse[index].pa_lo);
3659 } else {
3660 sli_cfg_mbx->un.sli_config_emb0_subsys.
3661 mse[index].pa_hi =
3662 putPaddrHigh(ext_dmabuf->phys);
3663 sli_cfg_mbx->un.sli_config_emb0_subsys.
3664 mse[index].pa_lo =
3665 putPaddrLow(ext_dmabuf->phys);
3666 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3667 "2944 SLI_CONFIG(mse)[%d], "
3668 "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3669 index,
3670 sli_cfg_mbx->un.sli_config_emb0_subsys.
3671 mse[index].buf_len,
3672 sli_cfg_mbx->un.sli_config_emb0_subsys.
3673 mse[index].pa_hi,
3674 sli_cfg_mbx->un.sli_config_emb0_subsys.
3675 mse[index].pa_lo);
3676 }
3677 } else {
3678 if (index == 0) {
3679 sli_cfg_mbx->un.sli_config_emb1_subsys.
3680 hbd[index].pa_hi =
3681 putPaddrHigh(mbx_dmabuf->phys +
3682 sizeof(MAILBOX_t));
3683 sli_cfg_mbx->un.sli_config_emb1_subsys.
3684 hbd[index].pa_lo =
3685 putPaddrLow(mbx_dmabuf->phys +
3686 sizeof(MAILBOX_t));
3687 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3688 "3007 SLI_CONFIG(hbd)[%d], "
3689 "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3690 index,
3691 bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
3692 &sli_cfg_mbx->un.
3693 sli_config_emb1_subsys.hbd[index]),
3694 sli_cfg_mbx->un.sli_config_emb1_subsys.
3695 hbd[index].pa_hi,
3696 sli_cfg_mbx->un.sli_config_emb1_subsys.
3697 hbd[index].pa_lo);
3698
3699 } else {
3700 sli_cfg_mbx->un.sli_config_emb1_subsys.
3701 hbd[index].pa_hi =
3702 putPaddrHigh(ext_dmabuf->phys);
3703 sli_cfg_mbx->un.sli_config_emb1_subsys.
3704 hbd[index].pa_lo =
3705 putPaddrLow(ext_dmabuf->phys);
3706 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3707 "3008 SLI_CONFIG(hbd)[%d], "
3708 "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3709 index,
3710 bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
3711 &sli_cfg_mbx->un.
3712 sli_config_emb1_subsys.hbd[index]),
3713 sli_cfg_mbx->un.sli_config_emb1_subsys.
3714 hbd[index].pa_hi,
3715 sli_cfg_mbx->un.sli_config_emb1_subsys.
3716 hbd[index].pa_lo);
3717 }
3718 }
3719 return;
3720}
3721
3722/**
3723 * lpfc_bsg_sli_cfg_mse_read_cmd_ext - sli_config non-embedded mailbox cmd read
3724 * @phba: Pointer to HBA context object.
3725 * @mb: Pointer to a BSG mailbox object.
3726 * @nemb_tp: Enumerate of non-embedded mailbox command type.
3727 * @dmabuff: Pointer to a DMA buffer descriptor.
3728 *
3729 * This routine performs SLI_CONFIG (0x9B) read mailbox command operation with
3730 * non-embedded external bufffers.
3731 **/
3732static int
3733lpfc_bsg_sli_cfg_read_cmd_ext(struct lpfc_hba *phba, struct fc_bsg_job *job,
3734 enum nemb_type nemb_tp,
3735 struct lpfc_dmabuf *dmabuf)
3736{
3737 struct lpfc_sli_config_mbox *sli_cfg_mbx;
3738 struct dfc_mbox_req *mbox_req;
3739 struct lpfc_dmabuf *curr_dmabuf, *next_dmabuf;
3740 uint32_t ext_buf_cnt, ext_buf_index;
3741 struct lpfc_dmabuf *ext_dmabuf = NULL;
3742 struct bsg_job_data *dd_data = NULL;
3743 LPFC_MBOXQ_t *pmboxq = NULL;
3744 MAILBOX_t *pmb;
3745 uint8_t *pmbx;
3746 int rc, i;
3747
3748 mbox_req =
3749 (struct dfc_mbox_req *)job->request->rqst_data.h_vendor.vendor_cmd;
3750
3751 /* pointer to the start of mailbox command */
3752 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
3753
3754 if (nemb_tp == nemb_mse) {
3755 ext_buf_cnt = bsg_bf_get(lpfc_mbox_hdr_mse_cnt,
3756 &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr);
3757 if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_MSE) {
3758 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3759 "2945 Handled SLI_CONFIG(mse) rd, "
3760 "ext_buf_cnt(%d) out of range(%d)\n",
3761 ext_buf_cnt,
3762 LPFC_MBX_SLI_CONFIG_MAX_MSE);
3763 rc = -ERANGE;
3764 goto job_error;
3765 }
3766 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3767 "2941 Handled SLI_CONFIG(mse) rd, "
3768 "ext_buf_cnt:%d\n", ext_buf_cnt);
3769 } else {
3770 /* sanity check on interface type for support */
3771 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
3772 LPFC_SLI_INTF_IF_TYPE_2) {
3773 rc = -ENODEV;
3774 goto job_error;
3775 }
3776 /* nemb_tp == nemb_hbd */
3777 ext_buf_cnt = sli_cfg_mbx->un.sli_config_emb1_subsys.hbd_count;
3778 if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_HBD) {
3779 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3780 "2946 Handled SLI_CONFIG(hbd) rd, "
3781 "ext_buf_cnt(%d) out of range(%d)\n",
3782 ext_buf_cnt,
3783 LPFC_MBX_SLI_CONFIG_MAX_HBD);
3784 rc = -ERANGE;
3785 goto job_error;
3786 }
3787 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3788 "2942 Handled SLI_CONFIG(hbd) rd, "
3789 "ext_buf_cnt:%d\n", ext_buf_cnt);
3790 }
3791
James Smartb76f2dc2011-07-22 18:37:42 -04003792 /* before dma descriptor setup */
3793 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_rd, dma_mbox,
3794 sta_pre_addr, dmabuf, ext_buf_cnt);
3795
James Smart7ad20aa2011-05-24 11:44:28 -04003796 /* reject non-embedded mailbox command with none external buffer */
3797 if (ext_buf_cnt == 0) {
3798 rc = -EPERM;
3799 goto job_error;
3800 } else if (ext_buf_cnt > 1) {
3801 /* additional external read buffers */
3802 for (i = 1; i < ext_buf_cnt; i++) {
3803 ext_dmabuf = lpfc_bsg_dma_page_alloc(phba);
3804 if (!ext_dmabuf) {
3805 rc = -ENOMEM;
3806 goto job_error;
3807 }
3808 list_add_tail(&ext_dmabuf->list,
3809 &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
3810 }
3811 }
3812
3813 /* bsg tracking structure */
3814 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
3815 if (!dd_data) {
3816 rc = -ENOMEM;
3817 goto job_error;
3818 }
3819
3820 /* mailbox command structure for base driver */
3821 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3822 if (!pmboxq) {
3823 rc = -ENOMEM;
3824 goto job_error;
3825 }
3826 memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
3827
3828 /* for the first external buffer */
3829 lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, 0, dmabuf, dmabuf);
3830
3831 /* for the rest of external buffer descriptors if any */
3832 if (ext_buf_cnt > 1) {
3833 ext_buf_index = 1;
3834 list_for_each_entry_safe(curr_dmabuf, next_dmabuf,
3835 &phba->mbox_ext_buf_ctx.ext_dmabuf_list, list) {
3836 lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp,
3837 ext_buf_index, dmabuf,
3838 curr_dmabuf);
3839 ext_buf_index++;
3840 }
3841 }
3842
James Smartb76f2dc2011-07-22 18:37:42 -04003843 /* after dma descriptor setup */
3844 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_rd, dma_mbox,
3845 sta_pos_addr, dmabuf, ext_buf_cnt);
3846
James Smart7ad20aa2011-05-24 11:44:28 -04003847 /* construct base driver mbox command */
3848 pmb = &pmboxq->u.mb;
3849 pmbx = (uint8_t *)dmabuf->virt;
3850 memcpy(pmb, pmbx, sizeof(*pmb));
3851 pmb->mbxOwner = OWN_HOST;
3852 pmboxq->vport = phba->pport;
3853
3854 /* multi-buffer handling context */
3855 phba->mbox_ext_buf_ctx.nembType = nemb_tp;
3856 phba->mbox_ext_buf_ctx.mboxType = mbox_rd;
3857 phba->mbox_ext_buf_ctx.numBuf = ext_buf_cnt;
3858 phba->mbox_ext_buf_ctx.mbxTag = mbox_req->extMboxTag;
3859 phba->mbox_ext_buf_ctx.seqNum = mbox_req->extSeqNum;
3860 phba->mbox_ext_buf_ctx.mbx_dmabuf = dmabuf;
3861
3862 /* callback for multi-buffer read mailbox command */
3863 pmboxq->mbox_cmpl = lpfc_bsg_issue_read_mbox_ext_cmpl;
3864
3865 /* context fields to callback function */
3866 pmboxq->context1 = dd_data;
3867 dd_data->type = TYPE_MBOX;
James Smarta33c4f72013-03-01 16:36:00 -05003868 dd_data->set_job = job;
James Smart7ad20aa2011-05-24 11:44:28 -04003869 dd_data->context_un.mbox.pmboxq = pmboxq;
3870 dd_data->context_un.mbox.mb = (MAILBOX_t *)pmbx;
James Smart7ad20aa2011-05-24 11:44:28 -04003871 job->dd_data = dd_data;
3872
3873 /* state change */
3874 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
3875
James Smart026abb82011-12-13 13:20:45 -05003876 /*
3877 * Non-embedded mailbox subcommand data gets byte swapped here because
3878 * the lower level driver code only does the first 64 mailbox words.
3879 */
3880 if ((!bsg_bf_get(lpfc_mbox_hdr_emb,
3881 &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) &&
3882 (nemb_tp == nemb_mse))
3883 lpfc_sli_pcimem_bcopy(&pmbx[sizeof(MAILBOX_t)],
3884 &pmbx[sizeof(MAILBOX_t)],
3885 sli_cfg_mbx->un.sli_config_emb0_subsys.
3886 mse[0].buf_len);
3887
James Smart7ad20aa2011-05-24 11:44:28 -04003888 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
3889 if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
3890 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3891 "2947 Issued SLI_CONFIG ext-buffer "
3892 "maibox command, rc:x%x\n", rc);
James Smart88a2cfb2011-07-22 18:36:33 -04003893 return SLI_CONFIG_HANDLED;
James Smart7ad20aa2011-05-24 11:44:28 -04003894 }
3895 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3896 "2948 Failed to issue SLI_CONFIG ext-buffer "
3897 "maibox command, rc:x%x\n", rc);
3898 rc = -EPIPE;
3899
3900job_error:
3901 if (pmboxq)
3902 mempool_free(pmboxq, phba->mbox_mem_pool);
3903 lpfc_bsg_dma_page_list_free(phba,
3904 &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
3905 kfree(dd_data);
3906 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_IDLE;
3907 return rc;
3908}
3909
3910/**
3911 * lpfc_bsg_sli_cfg_write_cmd_ext - sli_config non-embedded mailbox cmd write
3912 * @phba: Pointer to HBA context object.
3913 * @mb: Pointer to a BSG mailbox object.
3914 * @dmabuff: Pointer to a DMA buffer descriptor.
3915 *
3916 * This routine performs SLI_CONFIG (0x9B) write mailbox command operation with
3917 * non-embedded external bufffers.
3918 **/
3919static int
3920lpfc_bsg_sli_cfg_write_cmd_ext(struct lpfc_hba *phba, struct fc_bsg_job *job,
3921 enum nemb_type nemb_tp,
3922 struct lpfc_dmabuf *dmabuf)
3923{
3924 struct dfc_mbox_req *mbox_req;
3925 struct lpfc_sli_config_mbox *sli_cfg_mbx;
3926 uint32_t ext_buf_cnt;
3927 struct bsg_job_data *dd_data = NULL;
3928 LPFC_MBOXQ_t *pmboxq = NULL;
3929 MAILBOX_t *pmb;
3930 uint8_t *mbx;
James Smart88a2cfb2011-07-22 18:36:33 -04003931 int rc = SLI_CONFIG_NOT_HANDLED, i;
James Smart7ad20aa2011-05-24 11:44:28 -04003932
3933 mbox_req =
3934 (struct dfc_mbox_req *)job->request->rqst_data.h_vendor.vendor_cmd;
3935
3936 /* pointer to the start of mailbox command */
3937 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
3938
3939 if (nemb_tp == nemb_mse) {
3940 ext_buf_cnt = bsg_bf_get(lpfc_mbox_hdr_mse_cnt,
3941 &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr);
3942 if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_MSE) {
3943 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
James Smart026abb82011-12-13 13:20:45 -05003944 "2953 Failed SLI_CONFIG(mse) wr, "
James Smart7ad20aa2011-05-24 11:44:28 -04003945 "ext_buf_cnt(%d) out of range(%d)\n",
3946 ext_buf_cnt,
3947 LPFC_MBX_SLI_CONFIG_MAX_MSE);
3948 return -ERANGE;
3949 }
3950 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3951 "2949 Handled SLI_CONFIG(mse) wr, "
3952 "ext_buf_cnt:%d\n", ext_buf_cnt);
3953 } else {
3954 /* sanity check on interface type for support */
3955 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
3956 LPFC_SLI_INTF_IF_TYPE_2)
3957 return -ENODEV;
3958 /* nemb_tp == nemb_hbd */
3959 ext_buf_cnt = sli_cfg_mbx->un.sli_config_emb1_subsys.hbd_count;
3960 if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_HBD) {
3961 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
James Smart026abb82011-12-13 13:20:45 -05003962 "2954 Failed SLI_CONFIG(hbd) wr, "
James Smart7ad20aa2011-05-24 11:44:28 -04003963 "ext_buf_cnt(%d) out of range(%d)\n",
3964 ext_buf_cnt,
3965 LPFC_MBX_SLI_CONFIG_MAX_HBD);
3966 return -ERANGE;
3967 }
3968 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3969 "2950 Handled SLI_CONFIG(hbd) wr, "
3970 "ext_buf_cnt:%d\n", ext_buf_cnt);
3971 }
3972
James Smartb76f2dc2011-07-22 18:37:42 -04003973 /* before dma buffer descriptor setup */
3974 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_wr, dma_mbox,
3975 sta_pre_addr, dmabuf, ext_buf_cnt);
3976
James Smart7ad20aa2011-05-24 11:44:28 -04003977 if (ext_buf_cnt == 0)
3978 return -EPERM;
3979
3980 /* for the first external buffer */
3981 lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, 0, dmabuf, dmabuf);
3982
James Smartb76f2dc2011-07-22 18:37:42 -04003983 /* after dma descriptor setup */
3984 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_wr, dma_mbox,
3985 sta_pos_addr, dmabuf, ext_buf_cnt);
3986
James Smart7ad20aa2011-05-24 11:44:28 -04003987 /* log for looking forward */
3988 for (i = 1; i < ext_buf_cnt; i++) {
3989 if (nemb_tp == nemb_mse)
3990 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3991 "2951 SLI_CONFIG(mse), buf[%d]-length:%d\n",
3992 i, sli_cfg_mbx->un.sli_config_emb0_subsys.
3993 mse[i].buf_len);
3994 else
3995 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3996 "2952 SLI_CONFIG(hbd), buf[%d]-length:%d\n",
3997 i, bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
3998 &sli_cfg_mbx->un.sli_config_emb1_subsys.
3999 hbd[i]));
4000 }
4001
4002 /* multi-buffer handling context */
4003 phba->mbox_ext_buf_ctx.nembType = nemb_tp;
4004 phba->mbox_ext_buf_ctx.mboxType = mbox_wr;
4005 phba->mbox_ext_buf_ctx.numBuf = ext_buf_cnt;
4006 phba->mbox_ext_buf_ctx.mbxTag = mbox_req->extMboxTag;
4007 phba->mbox_ext_buf_ctx.seqNum = mbox_req->extSeqNum;
4008 phba->mbox_ext_buf_ctx.mbx_dmabuf = dmabuf;
4009
4010 if (ext_buf_cnt == 1) {
4011 /* bsg tracking structure */
4012 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
4013 if (!dd_data) {
4014 rc = -ENOMEM;
4015 goto job_error;
4016 }
4017
4018 /* mailbox command structure for base driver */
4019 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4020 if (!pmboxq) {
4021 rc = -ENOMEM;
4022 goto job_error;
4023 }
4024 memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
4025 pmb = &pmboxq->u.mb;
4026 mbx = (uint8_t *)dmabuf->virt;
4027 memcpy(pmb, mbx, sizeof(*pmb));
4028 pmb->mbxOwner = OWN_HOST;
4029 pmboxq->vport = phba->pport;
4030
4031 /* callback for multi-buffer read mailbox command */
4032 pmboxq->mbox_cmpl = lpfc_bsg_issue_write_mbox_ext_cmpl;
4033
4034 /* context fields to callback function */
4035 pmboxq->context1 = dd_data;
4036 dd_data->type = TYPE_MBOX;
James Smarta33c4f72013-03-01 16:36:00 -05004037 dd_data->set_job = job;
James Smart7ad20aa2011-05-24 11:44:28 -04004038 dd_data->context_un.mbox.pmboxq = pmboxq;
4039 dd_data->context_un.mbox.mb = (MAILBOX_t *)mbx;
James Smart7ad20aa2011-05-24 11:44:28 -04004040 job->dd_data = dd_data;
4041
4042 /* state change */
James Smart7ad20aa2011-05-24 11:44:28 -04004043
James Smarta33c4f72013-03-01 16:36:00 -05004044 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
James Smart7ad20aa2011-05-24 11:44:28 -04004045 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
4046 if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
4047 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4048 "2955 Issued SLI_CONFIG ext-buffer "
4049 "maibox command, rc:x%x\n", rc);
James Smart88a2cfb2011-07-22 18:36:33 -04004050 return SLI_CONFIG_HANDLED;
James Smart7ad20aa2011-05-24 11:44:28 -04004051 }
4052 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4053 "2956 Failed to issue SLI_CONFIG ext-buffer "
4054 "maibox command, rc:x%x\n", rc);
4055 rc = -EPIPE;
James Smart026abb82011-12-13 13:20:45 -05004056 goto job_error;
James Smart7ad20aa2011-05-24 11:44:28 -04004057 }
4058
James Smart88a2cfb2011-07-22 18:36:33 -04004059 /* wait for additoinal external buffers */
James Smarta33c4f72013-03-01 16:36:00 -05004060
James Smart88a2cfb2011-07-22 18:36:33 -04004061 job->reply->result = 0;
4062 job->job_done(job);
4063 return SLI_CONFIG_HANDLED;
4064
James Smart7ad20aa2011-05-24 11:44:28 -04004065job_error:
4066 if (pmboxq)
4067 mempool_free(pmboxq, phba->mbox_mem_pool);
4068 kfree(dd_data);
4069
4070 return rc;
4071}
4072
4073/**
4074 * lpfc_bsg_handle_sli_cfg_mbox - handle sli-cfg mailbox cmd with ext buffer
4075 * @phba: Pointer to HBA context object.
4076 * @mb: Pointer to a BSG mailbox object.
4077 * @dmabuff: Pointer to a DMA buffer descriptor.
4078 *
4079 * This routine handles SLI_CONFIG (0x9B) mailbox command with non-embedded
4080 * external bufffers, including both 0x9B with non-embedded MSEs and 0x9B
4081 * with embedded sussystem 0x1 and opcodes with external HBDs.
4082 **/
4083static int
4084lpfc_bsg_handle_sli_cfg_mbox(struct lpfc_hba *phba, struct fc_bsg_job *job,
4085 struct lpfc_dmabuf *dmabuf)
4086{
4087 struct lpfc_sli_config_mbox *sli_cfg_mbx;
4088 uint32_t subsys;
4089 uint32_t opcode;
4090 int rc = SLI_CONFIG_NOT_HANDLED;
4091
James Smart026abb82011-12-13 13:20:45 -05004092 /* state change on new multi-buffer pass-through mailbox command */
James Smart7ad20aa2011-05-24 11:44:28 -04004093 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_HOST;
4094
4095 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
4096
4097 if (!bsg_bf_get(lpfc_mbox_hdr_emb,
4098 &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) {
4099 subsys = bsg_bf_get(lpfc_emb0_subcmnd_subsys,
4100 &sli_cfg_mbx->un.sli_config_emb0_subsys);
4101 opcode = bsg_bf_get(lpfc_emb0_subcmnd_opcode,
4102 &sli_cfg_mbx->un.sli_config_emb0_subsys);
4103 if (subsys == SLI_CONFIG_SUBSYS_FCOE) {
4104 switch (opcode) {
4105 case FCOE_OPCODE_READ_FCF:
4106 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4107 "2957 Handled SLI_CONFIG "
4108 "subsys_fcoe, opcode:x%x\n",
4109 opcode);
4110 rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
4111 nemb_mse, dmabuf);
4112 break;
4113 case FCOE_OPCODE_ADD_FCF:
4114 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4115 "2958 Handled SLI_CONFIG "
4116 "subsys_fcoe, opcode:x%x\n",
4117 opcode);
4118 rc = lpfc_bsg_sli_cfg_write_cmd_ext(phba, job,
4119 nemb_mse, dmabuf);
4120 break;
4121 default:
4122 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
James Smart026abb82011-12-13 13:20:45 -05004123 "2959 Reject SLI_CONFIG "
James Smart7ad20aa2011-05-24 11:44:28 -04004124 "subsys_fcoe, opcode:x%x\n",
4125 opcode);
James Smart026abb82011-12-13 13:20:45 -05004126 rc = -EPERM;
4127 break;
4128 }
4129 } else if (subsys == SLI_CONFIG_SUBSYS_COMN) {
4130 switch (opcode) {
4131 case COMN_OPCODE_GET_CNTL_ADDL_ATTRIBUTES:
James Smartb99570d2012-05-09 21:16:24 -04004132 case COMN_OPCODE_GET_CNTL_ATTRIBUTES:
James Smart026abb82011-12-13 13:20:45 -05004133 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4134 "3106 Handled SLI_CONFIG "
James Smart6b5151f2012-01-18 16:24:06 -05004135 "subsys_comn, opcode:x%x\n",
James Smart026abb82011-12-13 13:20:45 -05004136 opcode);
4137 rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
4138 nemb_mse, dmabuf);
4139 break;
4140 default:
4141 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4142 "3107 Reject SLI_CONFIG "
James Smart6b5151f2012-01-18 16:24:06 -05004143 "subsys_comn, opcode:x%x\n",
James Smart026abb82011-12-13 13:20:45 -05004144 opcode);
4145 rc = -EPERM;
James Smart7ad20aa2011-05-24 11:44:28 -04004146 break;
4147 }
4148 } else {
4149 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
James Smart026abb82011-12-13 13:20:45 -05004150 "2977 Reject SLI_CONFIG "
James Smart7ad20aa2011-05-24 11:44:28 -04004151 "subsys:x%d, opcode:x%x\n",
4152 subsys, opcode);
James Smart026abb82011-12-13 13:20:45 -05004153 rc = -EPERM;
James Smart7ad20aa2011-05-24 11:44:28 -04004154 }
4155 } else {
4156 subsys = bsg_bf_get(lpfc_emb1_subcmnd_subsys,
4157 &sli_cfg_mbx->un.sli_config_emb1_subsys);
4158 opcode = bsg_bf_get(lpfc_emb1_subcmnd_opcode,
4159 &sli_cfg_mbx->un.sli_config_emb1_subsys);
4160 if (subsys == SLI_CONFIG_SUBSYS_COMN) {
4161 switch (opcode) {
4162 case COMN_OPCODE_READ_OBJECT:
4163 case COMN_OPCODE_READ_OBJECT_LIST:
4164 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4165 "2960 Handled SLI_CONFIG "
4166 "subsys_comn, opcode:x%x\n",
4167 opcode);
4168 rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
4169 nemb_hbd, dmabuf);
4170 break;
4171 case COMN_OPCODE_WRITE_OBJECT:
4172 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4173 "2961 Handled SLI_CONFIG "
4174 "subsys_comn, opcode:x%x\n",
4175 opcode);
4176 rc = lpfc_bsg_sli_cfg_write_cmd_ext(phba, job,
4177 nemb_hbd, dmabuf);
4178 break;
4179 default:
4180 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4181 "2962 Not handled SLI_CONFIG "
4182 "subsys_comn, opcode:x%x\n",
4183 opcode);
4184 rc = SLI_CONFIG_NOT_HANDLED;
4185 break;
4186 }
4187 } else {
4188 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
James Smart026abb82011-12-13 13:20:45 -05004189 "2978 Not handled SLI_CONFIG "
James Smart7ad20aa2011-05-24 11:44:28 -04004190 "subsys:x%d, opcode:x%x\n",
4191 subsys, opcode);
4192 rc = SLI_CONFIG_NOT_HANDLED;
4193 }
4194 }
James Smart026abb82011-12-13 13:20:45 -05004195
4196 /* state reset on not handled new multi-buffer mailbox command */
4197 if (rc != SLI_CONFIG_HANDLED)
4198 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_IDLE;
4199
James Smart7ad20aa2011-05-24 11:44:28 -04004200 return rc;
4201}
4202
4203/**
4204 * lpfc_bsg_mbox_ext_abort_req - request to abort mbox command with ext buffers
4205 * @phba: Pointer to HBA context object.
4206 *
4207 * This routine is for requesting to abort a pass-through mailbox command with
4208 * multiple external buffers due to error condition.
4209 **/
4210static void
4211lpfc_bsg_mbox_ext_abort(struct lpfc_hba *phba)
4212{
4213 if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_PORT)
4214 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_ABTS;
4215 else
4216 lpfc_bsg_mbox_ext_session_reset(phba);
4217 return;
4218}
4219
4220/**
4221 * lpfc_bsg_read_ebuf_get - get the next mailbox read external buffer
4222 * @phba: Pointer to HBA context object.
4223 * @dmabuf: Pointer to a DMA buffer descriptor.
4224 *
4225 * This routine extracts the next mailbox read external buffer back to
4226 * user space through BSG.
4227 **/
4228static int
4229lpfc_bsg_read_ebuf_get(struct lpfc_hba *phba, struct fc_bsg_job *job)
4230{
4231 struct lpfc_sli_config_mbox *sli_cfg_mbx;
4232 struct lpfc_dmabuf *dmabuf;
4233 uint8_t *pbuf;
4234 uint32_t size;
4235 uint32_t index;
4236
4237 index = phba->mbox_ext_buf_ctx.seqNum;
4238 phba->mbox_ext_buf_ctx.seqNum++;
4239
4240 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)
4241 phba->mbox_ext_buf_ctx.mbx_dmabuf->virt;
4242
4243 if (phba->mbox_ext_buf_ctx.nembType == nemb_mse) {
4244 size = bsg_bf_get(lpfc_mbox_sli_config_mse_len,
4245 &sli_cfg_mbx->un.sli_config_emb0_subsys.mse[index]);
4246 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4247 "2963 SLI_CONFIG (mse) ext-buffer rd get "
4248 "buffer[%d], size:%d\n", index, size);
4249 } else {
4250 size = bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
4251 &sli_cfg_mbx->un.sli_config_emb1_subsys.hbd[index]);
4252 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4253 "2964 SLI_CONFIG (hbd) ext-buffer rd get "
4254 "buffer[%d], size:%d\n", index, size);
4255 }
4256 if (list_empty(&phba->mbox_ext_buf_ctx.ext_dmabuf_list))
4257 return -EPIPE;
4258 dmabuf = list_first_entry(&phba->mbox_ext_buf_ctx.ext_dmabuf_list,
4259 struct lpfc_dmabuf, list);
4260 list_del_init(&dmabuf->list);
James Smartb76f2dc2011-07-22 18:37:42 -04004261
4262 /* after dma buffer descriptor setup */
4263 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, phba->mbox_ext_buf_ctx.nembType,
4264 mbox_rd, dma_ebuf, sta_pos_addr,
4265 dmabuf, index);
4266
James Smart7ad20aa2011-05-24 11:44:28 -04004267 pbuf = (uint8_t *)dmabuf->virt;
4268 job->reply->reply_payload_rcv_len =
4269 sg_copy_from_buffer(job->reply_payload.sg_list,
4270 job->reply_payload.sg_cnt,
4271 pbuf, size);
4272
4273 lpfc_bsg_dma_page_free(phba, dmabuf);
4274
4275 if (phba->mbox_ext_buf_ctx.seqNum == phba->mbox_ext_buf_ctx.numBuf) {
4276 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4277 "2965 SLI_CONFIG (hbd) ext-buffer rd mbox "
4278 "command session done\n");
4279 lpfc_bsg_mbox_ext_session_reset(phba);
4280 }
4281
4282 job->reply->result = 0;
4283 job->job_done(job);
4284
4285 return SLI_CONFIG_HANDLED;
4286}
4287
4288/**
4289 * lpfc_bsg_write_ebuf_set - set the next mailbox write external buffer
4290 * @phba: Pointer to HBA context object.
4291 * @dmabuf: Pointer to a DMA buffer descriptor.
4292 *
4293 * This routine sets up the next mailbox read external buffer obtained
4294 * from user space through BSG.
4295 **/
4296static int
4297lpfc_bsg_write_ebuf_set(struct lpfc_hba *phba, struct fc_bsg_job *job,
4298 struct lpfc_dmabuf *dmabuf)
4299{
4300 struct lpfc_sli_config_mbox *sli_cfg_mbx;
4301 struct bsg_job_data *dd_data = NULL;
4302 LPFC_MBOXQ_t *pmboxq = NULL;
4303 MAILBOX_t *pmb;
4304 enum nemb_type nemb_tp;
4305 uint8_t *pbuf;
4306 uint32_t size;
4307 uint32_t index;
4308 int rc;
4309
4310 index = phba->mbox_ext_buf_ctx.seqNum;
4311 phba->mbox_ext_buf_ctx.seqNum++;
4312 nemb_tp = phba->mbox_ext_buf_ctx.nembType;
4313
4314 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)
4315 phba->mbox_ext_buf_ctx.mbx_dmabuf->virt;
4316
4317 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
4318 if (!dd_data) {
4319 rc = -ENOMEM;
4320 goto job_error;
4321 }
4322
4323 pbuf = (uint8_t *)dmabuf->virt;
4324 size = job->request_payload.payload_len;
4325 sg_copy_to_buffer(job->request_payload.sg_list,
4326 job->request_payload.sg_cnt,
4327 pbuf, size);
4328
4329 if (phba->mbox_ext_buf_ctx.nembType == nemb_mse) {
4330 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4331 "2966 SLI_CONFIG (mse) ext-buffer wr set "
4332 "buffer[%d], size:%d\n",
4333 phba->mbox_ext_buf_ctx.seqNum, size);
4334
4335 } else {
4336 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4337 "2967 SLI_CONFIG (hbd) ext-buffer wr set "
4338 "buffer[%d], size:%d\n",
4339 phba->mbox_ext_buf_ctx.seqNum, size);
4340
4341 }
4342
4343 /* set up external buffer descriptor and add to external buffer list */
4344 lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, index,
4345 phba->mbox_ext_buf_ctx.mbx_dmabuf,
4346 dmabuf);
4347 list_add_tail(&dmabuf->list, &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
4348
James Smartb76f2dc2011-07-22 18:37:42 -04004349 /* after write dma buffer */
4350 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, phba->mbox_ext_buf_ctx.nembType,
4351 mbox_wr, dma_ebuf, sta_pos_addr,
4352 dmabuf, index);
4353
James Smart7ad20aa2011-05-24 11:44:28 -04004354 if (phba->mbox_ext_buf_ctx.seqNum == phba->mbox_ext_buf_ctx.numBuf) {
4355 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4356 "2968 SLI_CONFIG ext-buffer wr all %d "
4357 "ebuffers received\n",
4358 phba->mbox_ext_buf_ctx.numBuf);
4359 /* mailbox command structure for base driver */
4360 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4361 if (!pmboxq) {
4362 rc = -ENOMEM;
4363 goto job_error;
4364 }
4365 memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
4366 pbuf = (uint8_t *)phba->mbox_ext_buf_ctx.mbx_dmabuf->virt;
4367 pmb = &pmboxq->u.mb;
4368 memcpy(pmb, pbuf, sizeof(*pmb));
4369 pmb->mbxOwner = OWN_HOST;
4370 pmboxq->vport = phba->pport;
4371
4372 /* callback for multi-buffer write mailbox command */
4373 pmboxq->mbox_cmpl = lpfc_bsg_issue_write_mbox_ext_cmpl;
4374
4375 /* context fields to callback function */
4376 pmboxq->context1 = dd_data;
4377 dd_data->type = TYPE_MBOX;
James Smarta33c4f72013-03-01 16:36:00 -05004378 dd_data->set_job = job;
James Smart7ad20aa2011-05-24 11:44:28 -04004379 dd_data->context_un.mbox.pmboxq = pmboxq;
4380 dd_data->context_un.mbox.mb = (MAILBOX_t *)pbuf;
James Smart7ad20aa2011-05-24 11:44:28 -04004381 job->dd_data = dd_data;
4382
4383 /* state change */
4384 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
4385
4386 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
4387 if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
4388 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4389 "2969 Issued SLI_CONFIG ext-buffer "
4390 "maibox command, rc:x%x\n", rc);
James Smart88a2cfb2011-07-22 18:36:33 -04004391 return SLI_CONFIG_HANDLED;
James Smart7ad20aa2011-05-24 11:44:28 -04004392 }
4393 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4394 "2970 Failed to issue SLI_CONFIG ext-buffer "
4395 "maibox command, rc:x%x\n", rc);
4396 rc = -EPIPE;
4397 goto job_error;
4398 }
4399
4400 /* wait for additoinal external buffers */
4401 job->reply->result = 0;
4402 job->job_done(job);
4403 return SLI_CONFIG_HANDLED;
4404
4405job_error:
4406 lpfc_bsg_dma_page_free(phba, dmabuf);
4407 kfree(dd_data);
4408
4409 return rc;
4410}
4411
4412/**
4413 * lpfc_bsg_handle_sli_cfg_ebuf - handle ext buffer with sli-cfg mailbox cmd
4414 * @phba: Pointer to HBA context object.
4415 * @mb: Pointer to a BSG mailbox object.
4416 * @dmabuff: Pointer to a DMA buffer descriptor.
4417 *
4418 * This routine handles the external buffer with SLI_CONFIG (0x9B) mailbox
4419 * command with multiple non-embedded external buffers.
4420 **/
4421static int
4422lpfc_bsg_handle_sli_cfg_ebuf(struct lpfc_hba *phba, struct fc_bsg_job *job,
4423 struct lpfc_dmabuf *dmabuf)
4424{
4425 int rc;
4426
4427 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4428 "2971 SLI_CONFIG buffer (type:x%x)\n",
4429 phba->mbox_ext_buf_ctx.mboxType);
4430
4431 if (phba->mbox_ext_buf_ctx.mboxType == mbox_rd) {
4432 if (phba->mbox_ext_buf_ctx.state != LPFC_BSG_MBOX_DONE) {
4433 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4434 "2972 SLI_CONFIG rd buffer state "
4435 "mismatch:x%x\n",
4436 phba->mbox_ext_buf_ctx.state);
4437 lpfc_bsg_mbox_ext_abort(phba);
4438 return -EPIPE;
4439 }
4440 rc = lpfc_bsg_read_ebuf_get(phba, job);
4441 if (rc == SLI_CONFIG_HANDLED)
4442 lpfc_bsg_dma_page_free(phba, dmabuf);
4443 } else { /* phba->mbox_ext_buf_ctx.mboxType == mbox_wr */
4444 if (phba->mbox_ext_buf_ctx.state != LPFC_BSG_MBOX_HOST) {
4445 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4446 "2973 SLI_CONFIG wr buffer state "
4447 "mismatch:x%x\n",
4448 phba->mbox_ext_buf_ctx.state);
4449 lpfc_bsg_mbox_ext_abort(phba);
4450 return -EPIPE;
4451 }
4452 rc = lpfc_bsg_write_ebuf_set(phba, job, dmabuf);
4453 }
4454 return rc;
4455}
4456
4457/**
4458 * lpfc_bsg_handle_sli_cfg_ext - handle sli-cfg mailbox with external buffer
4459 * @phba: Pointer to HBA context object.
4460 * @mb: Pointer to a BSG mailbox object.
4461 * @dmabuff: Pointer to a DMA buffer descriptor.
4462 *
4463 * This routine checkes and handles non-embedded multi-buffer SLI_CONFIG
4464 * (0x9B) mailbox commands and external buffers.
4465 **/
4466static int
4467lpfc_bsg_handle_sli_cfg_ext(struct lpfc_hba *phba, struct fc_bsg_job *job,
4468 struct lpfc_dmabuf *dmabuf)
4469{
4470 struct dfc_mbox_req *mbox_req;
James Smart88a2cfb2011-07-22 18:36:33 -04004471 int rc = SLI_CONFIG_NOT_HANDLED;
James Smart7ad20aa2011-05-24 11:44:28 -04004472
4473 mbox_req =
4474 (struct dfc_mbox_req *)job->request->rqst_data.h_vendor.vendor_cmd;
4475
4476 /* mbox command with/without single external buffer */
4477 if (mbox_req->extMboxTag == 0 && mbox_req->extSeqNum == 0)
James Smart88a2cfb2011-07-22 18:36:33 -04004478 return rc;
James Smart7ad20aa2011-05-24 11:44:28 -04004479
4480 /* mbox command and first external buffer */
4481 if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_IDLE) {
4482 if (mbox_req->extSeqNum == 1) {
4483 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4484 "2974 SLI_CONFIG mailbox: tag:%d, "
4485 "seq:%d\n", mbox_req->extMboxTag,
4486 mbox_req->extSeqNum);
4487 rc = lpfc_bsg_handle_sli_cfg_mbox(phba, job, dmabuf);
4488 return rc;
4489 } else
4490 goto sli_cfg_ext_error;
4491 }
4492
4493 /*
4494 * handle additional external buffers
4495 */
4496
4497 /* check broken pipe conditions */
4498 if (mbox_req->extMboxTag != phba->mbox_ext_buf_ctx.mbxTag)
4499 goto sli_cfg_ext_error;
4500 if (mbox_req->extSeqNum > phba->mbox_ext_buf_ctx.numBuf)
4501 goto sli_cfg_ext_error;
4502 if (mbox_req->extSeqNum != phba->mbox_ext_buf_ctx.seqNum + 1)
4503 goto sli_cfg_ext_error;
4504
4505 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4506 "2975 SLI_CONFIG mailbox external buffer: "
4507 "extSta:x%x, tag:%d, seq:%d\n",
4508 phba->mbox_ext_buf_ctx.state, mbox_req->extMboxTag,
4509 mbox_req->extSeqNum);
4510 rc = lpfc_bsg_handle_sli_cfg_ebuf(phba, job, dmabuf);
4511 return rc;
4512
4513sli_cfg_ext_error:
4514 /* all other cases, broken pipe */
4515 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4516 "2976 SLI_CONFIG mailbox broken pipe: "
4517 "ctxSta:x%x, ctxNumBuf:%d "
4518 "ctxTag:%d, ctxSeq:%d, tag:%d, seq:%d\n",
4519 phba->mbox_ext_buf_ctx.state,
4520 phba->mbox_ext_buf_ctx.numBuf,
4521 phba->mbox_ext_buf_ctx.mbxTag,
4522 phba->mbox_ext_buf_ctx.seqNum,
4523 mbox_req->extMboxTag, mbox_req->extSeqNum);
4524
4525 lpfc_bsg_mbox_ext_session_reset(phba);
4526
4527 return -EPIPE;
4528}
4529
4530/**
James Smart3b5dd522010-01-26 23:10:15 -05004531 * lpfc_bsg_issue_mbox - issues a mailbox command on behalf of an app
4532 * @phba: Pointer to HBA context object.
4533 * @mb: Pointer to a mailbox object.
4534 * @vport: Pointer to a vport object.
4535 *
4536 * Allocate a tracking object, mailbox command memory, get a mailbox
4537 * from the mailbox pool, copy the caller mailbox command.
4538 *
4539 * If offline and the sli is active we need to poll for the command (port is
4540 * being reset) and com-plete the job, otherwise issue the mailbox command and
4541 * let our completion handler finish the command.
4542 **/
4543static uint32_t
4544lpfc_bsg_issue_mbox(struct lpfc_hba *phba, struct fc_bsg_job *job,
4545 struct lpfc_vport *vport)
4546{
James Smart7a470272010-03-15 11:25:20 -04004547 LPFC_MBOXQ_t *pmboxq = NULL; /* internal mailbox queue */
4548 MAILBOX_t *pmb; /* shortcut to the pmboxq mailbox */
4549 /* a 4k buffer to hold the mb and extended data from/to the bsg */
James Smart7ad20aa2011-05-24 11:44:28 -04004550 uint8_t *pmbx = NULL;
James Smart7a470272010-03-15 11:25:20 -04004551 struct bsg_job_data *dd_data = NULL; /* bsg data tracking structure */
James Smart7ad20aa2011-05-24 11:44:28 -04004552 struct lpfc_dmabuf *dmabuf = NULL;
4553 struct dfc_mbox_req *mbox_req;
James Smartb6e3b9c2011-04-16 11:03:43 -04004554 struct READ_EVENT_LOG_VAR *rdEventLog;
4555 uint32_t transmit_length, receive_length, mode;
James Smart7ad20aa2011-05-24 11:44:28 -04004556 struct lpfc_mbx_sli4_config *sli4_config;
James Smartb6e3b9c2011-04-16 11:03:43 -04004557 struct lpfc_mbx_nembed_cmd *nembed_sge;
4558 struct mbox_header *header;
4559 struct ulp_bde64 *bde;
James Smart7a470272010-03-15 11:25:20 -04004560 uint8_t *ext = NULL;
James Smart3b5dd522010-01-26 23:10:15 -05004561 int rc = 0;
James Smart7a470272010-03-15 11:25:20 -04004562 uint8_t *from;
James Smart7ad20aa2011-05-24 11:44:28 -04004563 uint32_t size;
4564
James Smart7a470272010-03-15 11:25:20 -04004565 /* in case no data is transferred */
4566 job->reply->reply_payload_rcv_len = 0;
4567
James Smartb6e3b9c2011-04-16 11:03:43 -04004568 /* sanity check to protect driver */
4569 if (job->reply_payload.payload_len > BSG_MBOX_SIZE ||
4570 job->request_payload.payload_len > BSG_MBOX_SIZE) {
4571 rc = -ERANGE;
4572 goto job_done;
4573 }
4574
James Smart7ad20aa2011-05-24 11:44:28 -04004575 /*
4576 * Don't allow mailbox commands to be sent when blocked or when in
4577 * the middle of discovery
4578 */
4579 if (phba->sli.sli_flag & LPFC_BLOCK_MGMT_IO) {
4580 rc = -EAGAIN;
4581 goto job_done;
4582 }
4583
4584 mbox_req =
4585 (struct dfc_mbox_req *)job->request->rqst_data.h_vendor.vendor_cmd;
4586
James Smart7a470272010-03-15 11:25:20 -04004587 /* check if requested extended data lengths are valid */
James Smartb6e3b9c2011-04-16 11:03:43 -04004588 if ((mbox_req->inExtWLen > BSG_MBOX_SIZE/sizeof(uint32_t)) ||
4589 (mbox_req->outExtWLen > BSG_MBOX_SIZE/sizeof(uint32_t))) {
James Smart7a470272010-03-15 11:25:20 -04004590 rc = -ERANGE;
4591 goto job_done;
4592 }
James Smart3b5dd522010-01-26 23:10:15 -05004593
James Smart7ad20aa2011-05-24 11:44:28 -04004594 dmabuf = lpfc_bsg_dma_page_alloc(phba);
4595 if (!dmabuf || !dmabuf->virt) {
4596 rc = -ENOMEM;
4597 goto job_done;
4598 }
4599
4600 /* Get the mailbox command or external buffer from BSG */
4601 pmbx = (uint8_t *)dmabuf->virt;
4602 size = job->request_payload.payload_len;
4603 sg_copy_to_buffer(job->request_payload.sg_list,
4604 job->request_payload.sg_cnt, pmbx, size);
4605
4606 /* Handle possible SLI_CONFIG with non-embedded payloads */
4607 if (phba->sli_rev == LPFC_SLI_REV4) {
4608 rc = lpfc_bsg_handle_sli_cfg_ext(phba, job, dmabuf);
4609 if (rc == SLI_CONFIG_HANDLED)
4610 goto job_cont;
4611 if (rc)
4612 goto job_done;
4613 /* SLI_CONFIG_NOT_HANDLED for other mailbox commands */
4614 }
4615
4616 rc = lpfc_bsg_check_cmd_access(phba, (MAILBOX_t *)pmbx, vport);
4617 if (rc != 0)
4618 goto job_done; /* must be negative */
4619
James Smart3b5dd522010-01-26 23:10:15 -05004620 /* allocate our bsg tracking structure */
4621 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
4622 if (!dd_data) {
4623 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
4624 "2727 Failed allocation of dd_data\n");
James Smart7a470272010-03-15 11:25:20 -04004625 rc = -ENOMEM;
4626 goto job_done;
James Smart3b5dd522010-01-26 23:10:15 -05004627 }
4628
James Smart3b5dd522010-01-26 23:10:15 -05004629 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4630 if (!pmboxq) {
James Smart7a470272010-03-15 11:25:20 -04004631 rc = -ENOMEM;
4632 goto job_done;
James Smart3b5dd522010-01-26 23:10:15 -05004633 }
James Smart7a470272010-03-15 11:25:20 -04004634 memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
James Smart3b5dd522010-01-26 23:10:15 -05004635
James Smart3b5dd522010-01-26 23:10:15 -05004636 pmb = &pmboxq->u.mb;
James Smart7ad20aa2011-05-24 11:44:28 -04004637 memcpy(pmb, pmbx, sizeof(*pmb));
James Smart3b5dd522010-01-26 23:10:15 -05004638 pmb->mbxOwner = OWN_HOST;
James Smart3b5dd522010-01-26 23:10:15 -05004639 pmboxq->vport = vport;
4640
James Smartc7495932010-04-06 15:05:28 -04004641 /* If HBA encountered an error attention, allow only DUMP
4642 * or RESTART mailbox commands until the HBA is restarted.
4643 */
4644 if (phba->pport->stopped &&
4645 pmb->mbxCommand != MBX_DUMP_MEMORY &&
4646 pmb->mbxCommand != MBX_RESTART &&
4647 pmb->mbxCommand != MBX_WRITE_VPARMS &&
4648 pmb->mbxCommand != MBX_WRITE_WWN)
4649 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
4650 "2797 mbox: Issued mailbox cmd "
4651 "0x%x while in stopped state.\n",
4652 pmb->mbxCommand);
4653
James Smart7a470272010-03-15 11:25:20 -04004654 /* extended mailbox commands will need an extended buffer */
James Smartc7495932010-04-06 15:05:28 -04004655 if (mbox_req->inExtWLen || mbox_req->outExtWLen) {
James Smart026abb82011-12-13 13:20:45 -05004656 from = pmbx;
4657 ext = from + sizeof(MAILBOX_t);
James Smart7a470272010-03-15 11:25:20 -04004658 pmboxq->context2 = ext;
4659 pmboxq->in_ext_byte_len =
James Smart7a470272010-03-15 11:25:20 -04004660 mbox_req->inExtWLen * sizeof(uint32_t);
4661 pmboxq->out_ext_byte_len =
James Smartc7495932010-04-06 15:05:28 -04004662 mbox_req->outExtWLen * sizeof(uint32_t);
James Smart7a470272010-03-15 11:25:20 -04004663 pmboxq->mbox_offset_word = mbox_req->mbOffset;
4664 }
4665
4666 /* biu diag will need a kernel buffer to transfer the data
4667 * allocate our own buffer and setup the mailbox command to
4668 * use ours
4669 */
4670 if (pmb->mbxCommand == MBX_RUN_BIU_DIAG64) {
James Smartb6e3b9c2011-04-16 11:03:43 -04004671 transmit_length = pmb->un.varWords[1];
4672 receive_length = pmb->un.varWords[4];
James Smartc7495932010-04-06 15:05:28 -04004673 /* transmit length cannot be greater than receive length or
4674 * mailbox extension size
4675 */
4676 if ((transmit_length > receive_length) ||
James Smart88a2cfb2011-07-22 18:36:33 -04004677 (transmit_length > BSG_MBOX_SIZE - sizeof(MAILBOX_t))) {
James Smartc7495932010-04-06 15:05:28 -04004678 rc = -ERANGE;
4679 goto job_done;
4680 }
James Smart7a470272010-03-15 11:25:20 -04004681 pmb->un.varBIUdiag.un.s2.xmit_bde64.addrHigh =
James Smart7ad20aa2011-05-24 11:44:28 -04004682 putPaddrHigh(dmabuf->phys + sizeof(MAILBOX_t));
James Smart7a470272010-03-15 11:25:20 -04004683 pmb->un.varBIUdiag.un.s2.xmit_bde64.addrLow =
James Smart7ad20aa2011-05-24 11:44:28 -04004684 putPaddrLow(dmabuf->phys + sizeof(MAILBOX_t));
James Smart7a470272010-03-15 11:25:20 -04004685
4686 pmb->un.varBIUdiag.un.s2.rcv_bde64.addrHigh =
James Smart7ad20aa2011-05-24 11:44:28 -04004687 putPaddrHigh(dmabuf->phys + sizeof(MAILBOX_t)
4688 + pmb->un.varBIUdiag.un.s2.xmit_bde64.tus.f.bdeSize);
James Smart7a470272010-03-15 11:25:20 -04004689 pmb->un.varBIUdiag.un.s2.rcv_bde64.addrLow =
James Smart7ad20aa2011-05-24 11:44:28 -04004690 putPaddrLow(dmabuf->phys + sizeof(MAILBOX_t)
4691 + pmb->un.varBIUdiag.un.s2.xmit_bde64.tus.f.bdeSize);
James Smartc7495932010-04-06 15:05:28 -04004692 } else if (pmb->mbxCommand == MBX_READ_EVENT_LOG) {
James Smartb6e3b9c2011-04-16 11:03:43 -04004693 rdEventLog = &pmb->un.varRdEventLog;
4694 receive_length = rdEventLog->rcv_bde64.tus.f.bdeSize;
4695 mode = bf_get(lpfc_event_log, rdEventLog);
James Smartc7495932010-04-06 15:05:28 -04004696
4697 /* receive length cannot be greater than mailbox
4698 * extension size
4699 */
James Smart88a2cfb2011-07-22 18:36:33 -04004700 if (receive_length > BSG_MBOX_SIZE - sizeof(MAILBOX_t)) {
James Smartc7495932010-04-06 15:05:28 -04004701 rc = -ERANGE;
4702 goto job_done;
4703 }
4704
4705 /* mode zero uses a bde like biu diags command */
4706 if (mode == 0) {
James Smart7ad20aa2011-05-24 11:44:28 -04004707 pmb->un.varWords[3] = putPaddrLow(dmabuf->phys
4708 + sizeof(MAILBOX_t));
4709 pmb->un.varWords[4] = putPaddrHigh(dmabuf->phys
4710 + sizeof(MAILBOX_t));
James Smartc7495932010-04-06 15:05:28 -04004711 }
4712 } else if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart3ef6d242012-01-18 16:23:48 -05004713 /* Let type 4 (well known data) through because the data is
4714 * returned in varwords[4-8]
4715 * otherwise check the recieve length and fetch the buffer addr
4716 */
4717 if ((pmb->mbxCommand == MBX_DUMP_MEMORY) &&
4718 (pmb->un.varDmp.type != DMP_WELL_KNOWN)) {
James Smartc7495932010-04-06 15:05:28 -04004719 /* rebuild the command for sli4 using our own buffers
4720 * like we do for biu diags
4721 */
James Smartb6e3b9c2011-04-16 11:03:43 -04004722 receive_length = pmb->un.varWords[2];
James Smartc7495932010-04-06 15:05:28 -04004723 /* receive length cannot be greater than mailbox
4724 * extension size
4725 */
James Smart7ad20aa2011-05-24 11:44:28 -04004726 if (receive_length == 0) {
James Smartc7495932010-04-06 15:05:28 -04004727 rc = -ERANGE;
4728 goto job_done;
4729 }
James Smart7ad20aa2011-05-24 11:44:28 -04004730 pmb->un.varWords[3] = putPaddrLow(dmabuf->phys
4731 + sizeof(MAILBOX_t));
4732 pmb->un.varWords[4] = putPaddrHigh(dmabuf->phys
4733 + sizeof(MAILBOX_t));
James Smartc7495932010-04-06 15:05:28 -04004734 } else if ((pmb->mbxCommand == MBX_UPDATE_CFG) &&
4735 pmb->un.varUpdateCfg.co) {
James Smartb6e3b9c2011-04-16 11:03:43 -04004736 bde = (struct ulp_bde64 *)&pmb->un.varWords[4];
James Smartc7495932010-04-06 15:05:28 -04004737
4738 /* bde size cannot be greater than mailbox ext size */
James Smart88a2cfb2011-07-22 18:36:33 -04004739 if (bde->tus.f.bdeSize >
4740 BSG_MBOX_SIZE - sizeof(MAILBOX_t)) {
James Smartc7495932010-04-06 15:05:28 -04004741 rc = -ERANGE;
4742 goto job_done;
4743 }
James Smart7ad20aa2011-05-24 11:44:28 -04004744 bde->addrHigh = putPaddrHigh(dmabuf->phys
4745 + sizeof(MAILBOX_t));
4746 bde->addrLow = putPaddrLow(dmabuf->phys
4747 + sizeof(MAILBOX_t));
James Smart515e0aa2010-09-29 11:19:00 -04004748 } else if (pmb->mbxCommand == MBX_SLI4_CONFIG) {
James Smart7ad20aa2011-05-24 11:44:28 -04004749 /* Handling non-embedded SLI_CONFIG mailbox command */
4750 sli4_config = &pmboxq->u.mqe.un.sli4_config;
4751 if (!bf_get(lpfc_mbox_hdr_emb,
4752 &sli4_config->header.cfg_mhdr)) {
4753 /* rebuild the command for sli4 using our
4754 * own buffers like we do for biu diags
4755 */
4756 header = (struct mbox_header *)
4757 &pmb->un.varWords[0];
4758 nembed_sge = (struct lpfc_mbx_nembed_cmd *)
4759 &pmb->un.varWords[0];
4760 receive_length = nembed_sge->sge[0].length;
James Smart515e0aa2010-09-29 11:19:00 -04004761
James Smart7ad20aa2011-05-24 11:44:28 -04004762 /* receive length cannot be greater than
4763 * mailbox extension size
4764 */
4765 if ((receive_length == 0) ||
James Smart88a2cfb2011-07-22 18:36:33 -04004766 (receive_length >
4767 BSG_MBOX_SIZE - sizeof(MAILBOX_t))) {
James Smart7ad20aa2011-05-24 11:44:28 -04004768 rc = -ERANGE;
4769 goto job_done;
4770 }
4771
4772 nembed_sge->sge[0].pa_hi =
4773 putPaddrHigh(dmabuf->phys
4774 + sizeof(MAILBOX_t));
4775 nembed_sge->sge[0].pa_lo =
4776 putPaddrLow(dmabuf->phys
4777 + sizeof(MAILBOX_t));
James Smart515e0aa2010-09-29 11:19:00 -04004778 }
James Smartc7495932010-04-06 15:05:28 -04004779 }
James Smart3b5dd522010-01-26 23:10:15 -05004780 }
4781
James Smart7ad20aa2011-05-24 11:44:28 -04004782 dd_data->context_un.mbox.dmabuffers = dmabuf;
James Smartc7495932010-04-06 15:05:28 -04004783
James Smart3b5dd522010-01-26 23:10:15 -05004784 /* setup wake call as IOCB callback */
James Smart7ad20aa2011-05-24 11:44:28 -04004785 pmboxq->mbox_cmpl = lpfc_bsg_issue_mbox_cmpl;
James Smart7a470272010-03-15 11:25:20 -04004786
James Smart3b5dd522010-01-26 23:10:15 -05004787 /* setup context field to pass wait_queue pointer to wake function */
4788 pmboxq->context1 = dd_data;
4789 dd_data->type = TYPE_MBOX;
James Smarta33c4f72013-03-01 16:36:00 -05004790 dd_data->set_job = job;
James Smart3b5dd522010-01-26 23:10:15 -05004791 dd_data->context_un.mbox.pmboxq = pmboxq;
James Smart7ad20aa2011-05-24 11:44:28 -04004792 dd_data->context_un.mbox.mb = (MAILBOX_t *)pmbx;
James Smart7a470272010-03-15 11:25:20 -04004793 dd_data->context_un.mbox.ext = ext;
4794 dd_data->context_un.mbox.mbOffset = mbox_req->mbOffset;
4795 dd_data->context_un.mbox.inExtWLen = mbox_req->inExtWLen;
James Smartc7495932010-04-06 15:05:28 -04004796 dd_data->context_un.mbox.outExtWLen = mbox_req->outExtWLen;
James Smart3b5dd522010-01-26 23:10:15 -05004797 job->dd_data = dd_data;
James Smart7a470272010-03-15 11:25:20 -04004798
4799 if ((vport->fc_flag & FC_OFFLINE_MODE) ||
4800 (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))) {
4801 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
4802 if (rc != MBX_SUCCESS) {
4803 rc = (rc == MBX_TIMEOUT) ? -ETIME : -ENODEV;
4804 goto job_done;
4805 }
4806
4807 /* job finished, copy the data */
James Smart7ad20aa2011-05-24 11:44:28 -04004808 memcpy(pmbx, pmb, sizeof(*pmb));
James Smart7a470272010-03-15 11:25:20 -04004809 job->reply->reply_payload_rcv_len =
4810 sg_copy_from_buffer(job->reply_payload.sg_list,
James Smart7ad20aa2011-05-24 11:44:28 -04004811 job->reply_payload.sg_cnt,
4812 pmbx, size);
James Smart7a470272010-03-15 11:25:20 -04004813 /* not waiting mbox already done */
4814 rc = 0;
4815 goto job_done;
James Smart3b5dd522010-01-26 23:10:15 -05004816 }
4817
James Smart7a470272010-03-15 11:25:20 -04004818 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
4819 if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY))
4820 return 1; /* job started */
4821
4822job_done:
4823 /* common exit for error or job completed inline */
James Smart7a470272010-03-15 11:25:20 -04004824 if (pmboxq)
4825 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart7ad20aa2011-05-24 11:44:28 -04004826 lpfc_bsg_dma_page_free(phba, dmabuf);
James Smart7a470272010-03-15 11:25:20 -04004827 kfree(dd_data);
4828
James Smart7ad20aa2011-05-24 11:44:28 -04004829job_cont:
James Smart7a470272010-03-15 11:25:20 -04004830 return rc;
James Smart3b5dd522010-01-26 23:10:15 -05004831}
4832
4833/**
4834 * lpfc_bsg_mbox_cmd - process an fc bsg LPFC_BSG_VENDOR_MBOX command
4835 * @job: MBOX fc_bsg_job for LPFC_BSG_VENDOR_MBOX.
4836 **/
4837static int
4838lpfc_bsg_mbox_cmd(struct fc_bsg_job *job)
4839{
4840 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
4841 struct lpfc_hba *phba = vport->phba;
James Smart7ad20aa2011-05-24 11:44:28 -04004842 struct dfc_mbox_req *mbox_req;
James Smart3b5dd522010-01-26 23:10:15 -05004843 int rc = 0;
4844
James Smart7ad20aa2011-05-24 11:44:28 -04004845 /* mix-and-match backward compatibility */
James Smart3b5dd522010-01-26 23:10:15 -05004846 job->reply->reply_payload_rcv_len = 0;
4847 if (job->request_len <
4848 sizeof(struct fc_bsg_request) + sizeof(struct dfc_mbox_req)) {
James Smart7ad20aa2011-05-24 11:44:28 -04004849 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
Masanari Iida1051e9b2013-03-31 01:23:50 +09004850 "2737 Mix-and-match backward compatibility "
James Smart7ad20aa2011-05-24 11:44:28 -04004851 "between MBOX_REQ old size:%d and "
4852 "new request size:%d\n",
4853 (int)(job->request_len -
4854 sizeof(struct fc_bsg_request)),
4855 (int)sizeof(struct dfc_mbox_req));
4856 mbox_req = (struct dfc_mbox_req *)
4857 job->request->rqst_data.h_vendor.vendor_cmd;
4858 mbox_req->extMboxTag = 0;
4859 mbox_req->extSeqNum = 0;
James Smart3b5dd522010-01-26 23:10:15 -05004860 }
4861
4862 rc = lpfc_bsg_issue_mbox(phba, job, vport);
4863
James Smart3b5dd522010-01-26 23:10:15 -05004864 if (rc == 0) {
4865 /* job done */
4866 job->reply->result = 0;
4867 job->dd_data = NULL;
4868 job->job_done(job);
4869 } else if (rc == 1)
4870 /* job submitted, will complete later*/
4871 rc = 0; /* return zero, no error */
4872 else {
4873 /* some error occurred */
4874 job->reply->result = rc;
4875 job->dd_data = NULL;
4876 }
4877
4878 return rc;
4879}
4880
4881/**
James Smarte2aed292010-02-26 14:15:00 -05004882 * lpfc_bsg_menlo_cmd_cmp - lpfc_menlo_cmd completion handler
4883 * @phba: Pointer to HBA context object.
4884 * @cmdiocbq: Pointer to command iocb.
4885 * @rspiocbq: Pointer to response iocb.
4886 *
4887 * This function is the completion handler for iocbs issued using
4888 * lpfc_menlo_cmd function. This function is called by the
4889 * ring event handler function without any lock held. This function
4890 * can be called from both worker thread context and interrupt
4891 * context. This function also can be called from another thread which
4892 * cleans up the SLI layer objects.
4893 * This function copies the contents of the response iocb to the
4894 * response iocb memory object provided by the caller of
4895 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
4896 * sleeps for the iocb completion.
4897 **/
4898static void
4899lpfc_bsg_menlo_cmd_cmp(struct lpfc_hba *phba,
4900 struct lpfc_iocbq *cmdiocbq,
4901 struct lpfc_iocbq *rspiocbq)
4902{
4903 struct bsg_job_data *dd_data;
4904 struct fc_bsg_job *job;
4905 IOCB_t *rsp;
James Smarta33c4f72013-03-01 16:36:00 -05004906 struct lpfc_dmabuf *bmp, *cmp, *rmp;
James Smarte2aed292010-02-26 14:15:00 -05004907 struct lpfc_bsg_menlo *menlo;
4908 unsigned long flags;
4909 struct menlo_response *menlo_resp;
James Smarta33c4f72013-03-01 16:36:00 -05004910 unsigned int rsp_size;
James Smarte2aed292010-02-26 14:15:00 -05004911 int rc = 0;
4912
James Smarte2aed292010-02-26 14:15:00 -05004913 dd_data = cmdiocbq->context1;
James Smarta33c4f72013-03-01 16:36:00 -05004914 cmp = cmdiocbq->context2;
4915 bmp = cmdiocbq->context3;
James Smarte2aed292010-02-26 14:15:00 -05004916 menlo = &dd_data->context_un.menlo;
James Smarta33c4f72013-03-01 16:36:00 -05004917 rmp = menlo->rmp;
James Smarte2aed292010-02-26 14:15:00 -05004918 rsp = &rspiocbq->iocb;
4919
James Smarta33c4f72013-03-01 16:36:00 -05004920 /* Determine if job has been aborted */
4921 spin_lock_irqsave(&phba->ct_ev_lock, flags);
4922 job = dd_data->set_job;
4923 if (job) {
4924 /* Prevent timeout handling from trying to abort job */
4925 job->dd_data = NULL;
4926 }
4927 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smarte2aed292010-02-26 14:15:00 -05004928
James Smarta33c4f72013-03-01 16:36:00 -05004929 /* Copy the job data or set the failing status for the job */
4930
4931 if (job) {
4932 /* always return the xri, this would be used in the case
4933 * of a menlo download to allow the data to be sent as a
4934 * continuation of the exchange.
4935 */
4936
4937 menlo_resp = (struct menlo_response *)
4938 job->reply->reply_data.vendor_reply.vendor_rsp;
4939 menlo_resp->xri = rsp->ulpContext;
4940 if (rsp->ulpStatus) {
4941 if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
4942 switch (rsp->un.ulpWord[4] & IOERR_PARAM_MASK) {
4943 case IOERR_SEQUENCE_TIMEOUT:
4944 rc = -ETIMEDOUT;
4945 break;
4946 case IOERR_INVALID_RPI:
4947 rc = -EFAULT;
4948 break;
4949 default:
4950 rc = -EACCES;
4951 break;
4952 }
4953 } else {
James Smarte2aed292010-02-26 14:15:00 -05004954 rc = -EACCES;
James Smarte2aed292010-02-26 14:15:00 -05004955 }
James Smarta33c4f72013-03-01 16:36:00 -05004956 } else {
4957 rsp_size = rsp->un.genreq64.bdl.bdeSize;
4958 job->reply->reply_payload_rcv_len =
4959 lpfc_bsg_copy_data(rmp, &job->reply_payload,
4960 rsp_size, 0);
4961 }
James Smarte2aed292010-02-26 14:15:00 -05004962
James Smarta33c4f72013-03-01 16:36:00 -05004963 }
4964
James Smarte2aed292010-02-26 14:15:00 -05004965 lpfc_sli_release_iocbq(phba, cmdiocbq);
James Smarta33c4f72013-03-01 16:36:00 -05004966 lpfc_free_bsg_buffers(phba, cmp);
4967 lpfc_free_bsg_buffers(phba, rmp);
4968 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
James Smarte2aed292010-02-26 14:15:00 -05004969 kfree(bmp);
4970 kfree(dd_data);
James Smarta33c4f72013-03-01 16:36:00 -05004971
4972 /* Complete the job if active */
4973
4974 if (job) {
4975 job->reply->result = rc;
4976 job->job_done(job);
4977 }
4978
James Smarte2aed292010-02-26 14:15:00 -05004979 return;
4980}
4981
4982/**
4983 * lpfc_menlo_cmd - send an ioctl for menlo hardware
4984 * @job: fc_bsg_job to handle
4985 *
4986 * This function issues a gen request 64 CR ioctl for all menlo cmd requests,
4987 * all the command completions will return the xri for the command.
4988 * For menlo data requests a gen request 64 CX is used to continue the exchange
4989 * supplied in the menlo request header xri field.
4990 **/
4991static int
4992lpfc_menlo_cmd(struct fc_bsg_job *job)
4993{
4994 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
4995 struct lpfc_hba *phba = vport->phba;
James Smarta33c4f72013-03-01 16:36:00 -05004996 struct lpfc_iocbq *cmdiocbq;
4997 IOCB_t *cmd;
James Smarte2aed292010-02-26 14:15:00 -05004998 int rc = 0;
4999 struct menlo_command *menlo_cmd;
5000 struct menlo_response *menlo_resp;
James Smarta33c4f72013-03-01 16:36:00 -05005001 struct lpfc_dmabuf *bmp = NULL, *cmp = NULL, *rmp = NULL;
James Smarte2aed292010-02-26 14:15:00 -05005002 int request_nseg;
5003 int reply_nseg;
James Smarte2aed292010-02-26 14:15:00 -05005004 struct bsg_job_data *dd_data;
5005 struct ulp_bde64 *bpl = NULL;
5006
5007 /* in case no data is returned return just the return code */
5008 job->reply->reply_payload_rcv_len = 0;
5009
5010 if (job->request_len <
5011 sizeof(struct fc_bsg_request) +
5012 sizeof(struct menlo_command)) {
5013 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5014 "2784 Received MENLO_CMD request below "
5015 "minimum size\n");
5016 rc = -ERANGE;
5017 goto no_dd_data;
5018 }
5019
5020 if (job->reply_len <
5021 sizeof(struct fc_bsg_request) + sizeof(struct menlo_response)) {
5022 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5023 "2785 Received MENLO_CMD reply below "
5024 "minimum size\n");
5025 rc = -ERANGE;
5026 goto no_dd_data;
5027 }
5028
5029 if (!(phba->menlo_flag & HBA_MENLO_SUPPORT)) {
5030 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5031 "2786 Adapter does not support menlo "
5032 "commands\n");
5033 rc = -EPERM;
5034 goto no_dd_data;
5035 }
5036
5037 menlo_cmd = (struct menlo_command *)
5038 job->request->rqst_data.h_vendor.vendor_cmd;
5039
5040 menlo_resp = (struct menlo_response *)
5041 job->reply->reply_data.vendor_reply.vendor_rsp;
5042
5043 /* allocate our bsg tracking structure */
5044 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
5045 if (!dd_data) {
5046 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5047 "2787 Failed allocation of dd_data\n");
5048 rc = -ENOMEM;
5049 goto no_dd_data;
5050 }
5051
5052 bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5053 if (!bmp) {
5054 rc = -ENOMEM;
5055 goto free_dd;
5056 }
5057
James Smarta33c4f72013-03-01 16:36:00 -05005058 bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
5059 if (!bmp->virt) {
James Smarte2aed292010-02-26 14:15:00 -05005060 rc = -ENOMEM;
5061 goto free_bmp;
5062 }
5063
James Smarte2aed292010-02-26 14:15:00 -05005064 INIT_LIST_HEAD(&bmp->list);
James Smarta33c4f72013-03-01 16:36:00 -05005065
5066 bpl = (struct ulp_bde64 *)bmp->virt;
5067 request_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64);
5068 cmp = lpfc_alloc_bsg_buffers(phba, job->request_payload.payload_len,
5069 1, bpl, &request_nseg);
5070 if (!cmp) {
5071 rc = -ENOMEM;
5072 goto free_bmp;
5073 }
5074 lpfc_bsg_copy_data(cmp, &job->request_payload,
5075 job->request_payload.payload_len, 1);
5076
5077 bpl += request_nseg;
5078 reply_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64) - request_nseg;
5079 rmp = lpfc_alloc_bsg_buffers(phba, job->reply_payload.payload_len, 0,
5080 bpl, &reply_nseg);
5081 if (!rmp) {
5082 rc = -ENOMEM;
5083 goto free_cmp;
James Smarte2aed292010-02-26 14:15:00 -05005084 }
5085
James Smarta33c4f72013-03-01 16:36:00 -05005086 cmdiocbq = lpfc_sli_get_iocbq(phba);
5087 if (!cmdiocbq) {
5088 rc = -ENOMEM;
5089 goto free_rmp;
James Smarte2aed292010-02-26 14:15:00 -05005090 }
5091
5092 cmd = &cmdiocbq->iocb;
5093 cmd->un.genreq64.bdl.ulpIoTag32 = 0;
5094 cmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
5095 cmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
5096 cmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
5097 cmd->un.genreq64.bdl.bdeSize =
5098 (request_nseg + reply_nseg) * sizeof(struct ulp_bde64);
5099 cmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
5100 cmd->un.genreq64.w5.hcsw.Dfctl = 0;
5101 cmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CMD;
5102 cmd->un.genreq64.w5.hcsw.Type = MENLO_TRANSPORT_TYPE; /* 0xfe */
5103 cmd->ulpBdeCount = 1;
5104 cmd->ulpClass = CLASS3;
5105 cmd->ulpOwner = OWN_CHIP;
5106 cmd->ulpLe = 1; /* Limited Edition */
5107 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
5108 cmdiocbq->vport = phba->pport;
5109 /* We want the firmware to timeout before we do */
5110 cmd->ulpTimeout = MENLO_TIMEOUT - 5;
James Smarte2aed292010-02-26 14:15:00 -05005111 cmdiocbq->iocb_cmpl = lpfc_bsg_menlo_cmd_cmp;
5112 cmdiocbq->context1 = dd_data;
James Smarta33c4f72013-03-01 16:36:00 -05005113 cmdiocbq->context2 = cmp;
5114 cmdiocbq->context3 = bmp;
James Smarte2aed292010-02-26 14:15:00 -05005115 if (menlo_cmd->cmd == LPFC_BSG_VENDOR_MENLO_CMD) {
5116 cmd->ulpCommand = CMD_GEN_REQUEST64_CR;
5117 cmd->ulpPU = MENLO_PU; /* 3 */
5118 cmd->un.ulpWord[4] = MENLO_DID; /* 0x0000FC0E */
5119 cmd->ulpContext = MENLO_CONTEXT; /* 0 */
5120 } else {
5121 cmd->ulpCommand = CMD_GEN_REQUEST64_CX;
5122 cmd->ulpPU = 1;
5123 cmd->un.ulpWord[4] = 0;
5124 cmd->ulpContext = menlo_cmd->xri;
5125 }
5126
5127 dd_data->type = TYPE_MENLO;
James Smarta33c4f72013-03-01 16:36:00 -05005128 dd_data->set_job = job;
James Smarte2aed292010-02-26 14:15:00 -05005129 dd_data->context_un.menlo.cmdiocbq = cmdiocbq;
James Smarta33c4f72013-03-01 16:36:00 -05005130 dd_data->context_un.menlo.rmp = rmp;
5131 job->dd_data = dd_data;
James Smarte2aed292010-02-26 14:15:00 -05005132
5133 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq,
5134 MENLO_TIMEOUT - 5);
5135 if (rc == IOCB_SUCCESS)
5136 return 0; /* done for now */
5137
James Smarte2aed292010-02-26 14:15:00 -05005138 lpfc_sli_release_iocbq(phba, cmdiocbq);
James Smarta33c4f72013-03-01 16:36:00 -05005139
5140free_rmp:
5141 lpfc_free_bsg_buffers(phba, rmp);
5142free_cmp:
5143 lpfc_free_bsg_buffers(phba, cmp);
James Smarte2aed292010-02-26 14:15:00 -05005144free_bmp:
James Smarta33c4f72013-03-01 16:36:00 -05005145 if (bmp->virt)
5146 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
James Smarte2aed292010-02-26 14:15:00 -05005147 kfree(bmp);
5148free_dd:
5149 kfree(dd_data);
5150no_dd_data:
5151 /* make error code available to userspace */
5152 job->reply->result = rc;
5153 job->dd_data = NULL;
5154 return rc;
5155}
James Smartb6e3b9c2011-04-16 11:03:43 -04005156
James Smarte2aed292010-02-26 14:15:00 -05005157/**
James Smartf1c3b0f2009-07-19 10:01:32 -04005158 * lpfc_bsg_hst_vendor - process a vendor-specific fc_bsg_job
5159 * @job: fc_bsg_job to handle
James Smart3b5dd522010-01-26 23:10:15 -05005160 **/
James Smartf1c3b0f2009-07-19 10:01:32 -04005161static int
5162lpfc_bsg_hst_vendor(struct fc_bsg_job *job)
5163{
5164 int command = job->request->rqst_data.h_vendor.vendor_cmd[0];
James Smart4cc0e562010-01-26 23:09:48 -05005165 int rc;
James Smartf1c3b0f2009-07-19 10:01:32 -04005166
5167 switch (command) {
5168 case LPFC_BSG_VENDOR_SET_CT_EVENT:
James Smart4cc0e562010-01-26 23:09:48 -05005169 rc = lpfc_bsg_hba_set_event(job);
James Smartf1c3b0f2009-07-19 10:01:32 -04005170 break;
James Smartf1c3b0f2009-07-19 10:01:32 -04005171 case LPFC_BSG_VENDOR_GET_CT_EVENT:
James Smart4cc0e562010-01-26 23:09:48 -05005172 rc = lpfc_bsg_hba_get_event(job);
James Smartf1c3b0f2009-07-19 10:01:32 -04005173 break;
James Smart3b5dd522010-01-26 23:10:15 -05005174 case LPFC_BSG_VENDOR_SEND_MGMT_RESP:
5175 rc = lpfc_bsg_send_mgmt_rsp(job);
5176 break;
5177 case LPFC_BSG_VENDOR_DIAG_MODE:
James Smart7ad20aa2011-05-24 11:44:28 -04005178 rc = lpfc_bsg_diag_loopback_mode(job);
James Smart3b5dd522010-01-26 23:10:15 -05005179 break;
James Smart7ad20aa2011-05-24 11:44:28 -04005180 case LPFC_BSG_VENDOR_DIAG_MODE_END:
5181 rc = lpfc_sli4_bsg_diag_mode_end(job);
5182 break;
5183 case LPFC_BSG_VENDOR_DIAG_RUN_LOOPBACK:
5184 rc = lpfc_bsg_diag_loopback_run(job);
5185 break;
5186 case LPFC_BSG_VENDOR_LINK_DIAG_TEST:
5187 rc = lpfc_sli4_bsg_link_diag_test(job);
James Smart3b5dd522010-01-26 23:10:15 -05005188 break;
5189 case LPFC_BSG_VENDOR_GET_MGMT_REV:
5190 rc = lpfc_bsg_get_dfc_rev(job);
5191 break;
5192 case LPFC_BSG_VENDOR_MBOX:
5193 rc = lpfc_bsg_mbox_cmd(job);
5194 break;
James Smarte2aed292010-02-26 14:15:00 -05005195 case LPFC_BSG_VENDOR_MENLO_CMD:
5196 case LPFC_BSG_VENDOR_MENLO_DATA:
5197 rc = lpfc_menlo_cmd(job);
5198 break;
James Smartf1c3b0f2009-07-19 10:01:32 -04005199 default:
James Smart4cc0e562010-01-26 23:09:48 -05005200 rc = -EINVAL;
5201 job->reply->reply_payload_rcv_len = 0;
5202 /* make error code available to userspace */
5203 job->reply->result = rc;
5204 break;
James Smartf1c3b0f2009-07-19 10:01:32 -04005205 }
James Smart4cc0e562010-01-26 23:09:48 -05005206
5207 return rc;
James Smartf1c3b0f2009-07-19 10:01:32 -04005208}
5209
5210/**
5211 * lpfc_bsg_request - handle a bsg request from the FC transport
5212 * @job: fc_bsg_job to handle
James Smart3b5dd522010-01-26 23:10:15 -05005213 **/
James Smartf1c3b0f2009-07-19 10:01:32 -04005214int
5215lpfc_bsg_request(struct fc_bsg_job *job)
5216{
5217 uint32_t msgcode;
James Smart4cc0e562010-01-26 23:09:48 -05005218 int rc;
James Smartf1c3b0f2009-07-19 10:01:32 -04005219
5220 msgcode = job->request->msgcode;
James Smartf1c3b0f2009-07-19 10:01:32 -04005221 switch (msgcode) {
5222 case FC_BSG_HST_VENDOR:
5223 rc = lpfc_bsg_hst_vendor(job);
5224 break;
5225 case FC_BSG_RPT_ELS:
5226 rc = lpfc_bsg_rport_els(job);
5227 break;
5228 case FC_BSG_RPT_CT:
James Smart4cc0e562010-01-26 23:09:48 -05005229 rc = lpfc_bsg_send_mgmt_cmd(job);
James Smartf1c3b0f2009-07-19 10:01:32 -04005230 break;
5231 default:
James Smart4cc0e562010-01-26 23:09:48 -05005232 rc = -EINVAL;
5233 job->reply->reply_payload_rcv_len = 0;
5234 /* make error code available to userspace */
5235 job->reply->result = rc;
James Smartf1c3b0f2009-07-19 10:01:32 -04005236 break;
5237 }
5238
5239 return rc;
5240}
5241
5242/**
5243 * lpfc_bsg_timeout - handle timeout of a bsg request from the FC transport
5244 * @job: fc_bsg_job that has timed out
5245 *
5246 * This function just aborts the job's IOCB. The aborted IOCB will return to
5247 * the waiting function which will handle passing the error back to userspace
James Smart3b5dd522010-01-26 23:10:15 -05005248 **/
James Smartf1c3b0f2009-07-19 10:01:32 -04005249int
5250lpfc_bsg_timeout(struct fc_bsg_job *job)
5251{
5252 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
5253 struct lpfc_hba *phba = vport->phba;
James Smart4cc0e562010-01-26 23:09:48 -05005254 struct lpfc_iocbq *cmdiocb;
James Smartf1c3b0f2009-07-19 10:01:32 -04005255 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart4cc0e562010-01-26 23:09:48 -05005256 struct bsg_job_data *dd_data;
5257 unsigned long flags;
James Smarta33c4f72013-03-01 16:36:00 -05005258 int rc = 0;
5259 LIST_HEAD(completions);
5260 struct lpfc_iocbq *check_iocb, *next_iocb;
5261
5262 /* if job's driver data is NULL, the command completed or is in the
5263 * the process of completing. In this case, return status to request
5264 * so the timeout is retried. This avoids double completion issues
5265 * and the request will be pulled off the timer queue when the
5266 * command's completion handler executes. Otherwise, prevent the
5267 * command's completion handler from executing the job done callback
5268 * and continue processing to abort the outstanding the command.
5269 */
James Smartf1c3b0f2009-07-19 10:01:32 -04005270
James Smart4cc0e562010-01-26 23:09:48 -05005271 spin_lock_irqsave(&phba->ct_ev_lock, flags);
5272 dd_data = (struct bsg_job_data *)job->dd_data;
James Smarta33c4f72013-03-01 16:36:00 -05005273 if (dd_data) {
5274 dd_data->set_job = NULL;
5275 job->dd_data = NULL;
5276 } else {
James Smart4cc0e562010-01-26 23:09:48 -05005277 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smarta33c4f72013-03-01 16:36:00 -05005278 return -EAGAIN;
James Smart4cc0e562010-01-26 23:09:48 -05005279 }
5280
5281 switch (dd_data->type) {
5282 case TYPE_IOCB:
James Smarta33c4f72013-03-01 16:36:00 -05005283 /* Check to see if IOCB was issued to the port or not. If not,
5284 * remove it from the txq queue and call cancel iocbs.
5285 * Otherwise, call abort iotag
James Smart4cc0e562010-01-26 23:09:48 -05005286 */
James Smarta33c4f72013-03-01 16:36:00 -05005287
5288 cmdiocb = dd_data->context_un.iocb.cmdiocbq;
5289 spin_lock_irq(&phba->hbalock);
5290 list_for_each_entry_safe(check_iocb, next_iocb, &pring->txq,
5291 list) {
5292 if (check_iocb == cmdiocb) {
5293 list_move_tail(&check_iocb->list, &completions);
5294 break;
5295 }
5296 }
5297 if (list_empty(&completions))
5298 lpfc_sli_issue_abort_iotag(phba, pring, cmdiocb);
5299 spin_unlock_irq(&phba->hbalock);
James Smart4cc0e562010-01-26 23:09:48 -05005300 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smarta33c4f72013-03-01 16:36:00 -05005301 if (!list_empty(&completions)) {
5302 lpfc_sli_cancel_iocbs(phba, &completions,
5303 IOSTAT_LOCAL_REJECT,
5304 IOERR_SLI_ABORTED);
5305 }
James Smart4cc0e562010-01-26 23:09:48 -05005306 break;
James Smarta33c4f72013-03-01 16:36:00 -05005307
5308 case TYPE_EVT:
James Smart3b5dd522010-01-26 23:10:15 -05005309 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smarta33c4f72013-03-01 16:36:00 -05005310 break;
5311
5312 case TYPE_MBOX:
5313 /* Update the ext buf ctx state if needed */
5314
James Smart7ad20aa2011-05-24 11:44:28 -04005315 if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_PORT)
5316 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_ABTS;
James Smarta33c4f72013-03-01 16:36:00 -05005317 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smart3b5dd522010-01-26 23:10:15 -05005318 break;
James Smarte2aed292010-02-26 14:15:00 -05005319 case TYPE_MENLO:
James Smarta33c4f72013-03-01 16:36:00 -05005320 /* Check to see if IOCB was issued to the port or not. If not,
5321 * remove it from the txq queue and call cancel iocbs.
5322 * Otherwise, call abort iotag.
5323 */
5324
5325 cmdiocb = dd_data->context_un.menlo.cmdiocbq;
James Smarte2aed292010-02-26 14:15:00 -05005326 spin_lock_irq(&phba->hbalock);
James Smarta33c4f72013-03-01 16:36:00 -05005327 list_for_each_entry_safe(check_iocb, next_iocb, &pring->txq,
5328 list) {
5329 if (check_iocb == cmdiocb) {
5330 list_move_tail(&check_iocb->list, &completions);
5331 break;
5332 }
5333 }
5334 if (list_empty(&completions))
5335 lpfc_sli_issue_abort_iotag(phba, pring, cmdiocb);
James Smarte2aed292010-02-26 14:15:00 -05005336 spin_unlock_irq(&phba->hbalock);
James Smarta33c4f72013-03-01 16:36:00 -05005337 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5338 if (!list_empty(&completions)) {
5339 lpfc_sli_cancel_iocbs(phba, &completions,
5340 IOSTAT_LOCAL_REJECT,
5341 IOERR_SLI_ABORTED);
5342 }
James Smarte2aed292010-02-26 14:15:00 -05005343 break;
James Smart4cc0e562010-01-26 23:09:48 -05005344 default:
5345 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5346 break;
5347 }
James Smartf1c3b0f2009-07-19 10:01:32 -04005348
James Smart4cc0e562010-01-26 23:09:48 -05005349 /* scsi transport fc fc_bsg_job_timeout expects a zero return code,
5350 * otherwise an error message will be displayed on the console
5351 * so always return success (zero)
5352 */
James Smarta33c4f72013-03-01 16:36:00 -05005353 return rc;
James Smartf1c3b0f2009-07-19 10:01:32 -04005354}