blob: f79933366545a99fe9c77ade7070202af423a9d0 [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 Smartb99570d2012-05-09 21:16:24 -04004 * Copyright (C) 2009-2012 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;
222 struct scatterlist *sgel;
223 LIST_HEAD(temp_list);
224
225
226 list_splice_init(&dma_buffers->list, &temp_list);
227 list_add(&dma_buffers->list, &temp_list);
228 sg_offset = 0;
229 sgel = bsg_buffers->sg_list;
230 list_for_each_entry(mp, &temp_list, list) {
231 dma_offset = 0;
232 while (bytes_to_transfer && sgel &&
233 (dma_offset < LPFC_BPL_SIZE)) {
234 dma_address = mp->virt + dma_offset;
235 if (sg_offset) {
236 /* Continue previous partial transfer of sg */
237 sg_address = sg_virt(sgel) + sg_offset;
238 transfer_bytes = sgel->length - sg_offset;
239 } else {
240 sg_address = sg_virt(sgel);
241 transfer_bytes = sgel->length;
242 }
243 if (bytes_to_transfer < transfer_bytes)
244 transfer_bytes = bytes_to_transfer;
245 if (transfer_bytes > (LPFC_BPL_SIZE - dma_offset))
246 transfer_bytes = LPFC_BPL_SIZE - dma_offset;
247 if (to_buffers)
248 memcpy(dma_address, sg_address, transfer_bytes);
249 else
250 memcpy(sg_address, dma_address, transfer_bytes);
251 dma_offset += transfer_bytes;
252 sg_offset += transfer_bytes;
253 bytes_to_transfer -= transfer_bytes;
254 bytes_copied += transfer_bytes;
255 if (sg_offset >= sgel->length) {
256 sg_offset = 0;
257 sgel = sg_next(sgel);
258 }
259 }
260 }
261 list_del_init(&dma_buffers->list);
262 list_splice(&temp_list, &dma_buffers->list);
263 return bytes_copied;
264}
265
James Smartf1c3b0f2009-07-19 10:01:32 -0400266/**
James Smart4cc0e562010-01-26 23:09:48 -0500267 * lpfc_bsg_send_mgmt_cmd_cmp - lpfc_bsg_send_mgmt_cmd's completion handler
268 * @phba: Pointer to HBA context object.
269 * @cmdiocbq: Pointer to command iocb.
270 * @rspiocbq: Pointer to response iocb.
271 *
272 * This function is the completion handler for iocbs issued using
273 * lpfc_bsg_send_mgmt_cmd function. This function is called by the
274 * ring event handler function without any lock held. This function
275 * can be called from both worker thread context and interrupt
276 * context. This function also can be called from another thread which
277 * cleans up the SLI layer objects.
278 * This function copies the contents of the response iocb to the
279 * response iocb memory object provided by the caller of
280 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
281 * sleeps for the iocb completion.
282 **/
283static void
284lpfc_bsg_send_mgmt_cmd_cmp(struct lpfc_hba *phba,
285 struct lpfc_iocbq *cmdiocbq,
286 struct lpfc_iocbq *rspiocbq)
287{
James Smart4cc0e562010-01-26 23:09:48 -0500288 struct bsg_job_data *dd_data;
289 struct fc_bsg_job *job;
290 IOCB_t *rsp;
James Smarta33c4f72013-03-01 16:36:00 -0500291 struct lpfc_dmabuf *bmp, *cmp, *rmp;
James Smart4cc0e562010-01-26 23:09:48 -0500292 struct lpfc_nodelist *ndlp;
293 struct lpfc_bsg_iocb *iocb;
294 unsigned long flags;
James Smarta33c4f72013-03-01 16:36:00 -0500295 unsigned int rsp_size;
James Smart4cc0e562010-01-26 23:09:48 -0500296 int rc = 0;
297
James Smarta33c4f72013-03-01 16:36:00 -0500298 dd_data = cmdiocbq->context1;
299
300 /* Determine if job has been aborted */
James Smart4cc0e562010-01-26 23:09:48 -0500301 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smarta33c4f72013-03-01 16:36:00 -0500302 job = dd_data->set_job;
303 if (job) {
304 /* Prevent timeout handling from trying to abort job */
305 job->dd_data = NULL;
James Smart4cc0e562010-01-26 23:09:48 -0500306 }
James Smarta33c4f72013-03-01 16:36:00 -0500307 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smart4cc0e562010-01-26 23:09:48 -0500308
309 iocb = &dd_data->context_un.iocb;
James Smarta33c4f72013-03-01 16:36:00 -0500310 ndlp = iocb->ndlp;
311 rmp = iocb->rmp;
312 cmp = cmdiocbq->context2;
313 bmp = cmdiocbq->context3;
James Smart4cc0e562010-01-26 23:09:48 -0500314 rsp = &rspiocbq->iocb;
James Smart4cc0e562010-01-26 23:09:48 -0500315
James Smarta33c4f72013-03-01 16:36:00 -0500316 /* Copy the completed data or set the error status */
James Smart4cc0e562010-01-26 23:09:48 -0500317
James Smarta33c4f72013-03-01 16:36:00 -0500318 if (job) {
319 if (rsp->ulpStatus) {
320 if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
321 switch (rsp->un.ulpWord[4] & IOERR_PARAM_MASK) {
322 case IOERR_SEQUENCE_TIMEOUT:
323 rc = -ETIMEDOUT;
324 break;
325 case IOERR_INVALID_RPI:
326 rc = -EFAULT;
327 break;
328 default:
329 rc = -EACCES;
330 break;
331 }
332 } else {
James Smart4cc0e562010-01-26 23:09:48 -0500333 rc = -EACCES;
James Smart4cc0e562010-01-26 23:09:48 -0500334 }
James Smarta33c4f72013-03-01 16:36:00 -0500335 } else {
336 rsp_size = rsp->un.genreq64.bdl.bdeSize;
337 job->reply->reply_payload_rcv_len =
338 lpfc_bsg_copy_data(rmp, &job->reply_payload,
339 rsp_size, 0);
340 }
341 }
James Smart4cc0e562010-01-26 23:09:48 -0500342
James Smarta33c4f72013-03-01 16:36:00 -0500343 lpfc_free_bsg_buffers(phba, cmp);
344 lpfc_free_bsg_buffers(phba, rmp);
James Smart4cc0e562010-01-26 23:09:48 -0500345 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
James Smarta33c4f72013-03-01 16:36:00 -0500346 kfree(bmp);
James Smart4cc0e562010-01-26 23:09:48 -0500347 lpfc_sli_release_iocbq(phba, cmdiocbq);
348 lpfc_nlp_put(ndlp);
James Smart4cc0e562010-01-26 23:09:48 -0500349 kfree(dd_data);
James Smarta33c4f72013-03-01 16:36:00 -0500350
351 /* Complete the job if the job is still active */
352
353 if (job) {
354 job->reply->result = rc;
355 job->job_done(job);
356 }
James Smart4cc0e562010-01-26 23:09:48 -0500357 return;
358}
359
360/**
361 * lpfc_bsg_send_mgmt_cmd - send a CT command from a bsg request
James Smartf1c3b0f2009-07-19 10:01:32 -0400362 * @job: fc_bsg_job to handle
James Smart3b5dd522010-01-26 23:10:15 -0500363 **/
James Smartf1c3b0f2009-07-19 10:01:32 -0400364static int
James Smart4cc0e562010-01-26 23:09:48 -0500365lpfc_bsg_send_mgmt_cmd(struct fc_bsg_job *job)
James Smartf1c3b0f2009-07-19 10:01:32 -0400366{
James Smartf1c3b0f2009-07-19 10:01:32 -0400367 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
368 struct lpfc_hba *phba = vport->phba;
369 struct lpfc_rport_data *rdata = job->rport->dd_data;
370 struct lpfc_nodelist *ndlp = rdata->pnode;
371 struct ulp_bde64 *bpl = NULL;
372 uint32_t timeout;
373 struct lpfc_iocbq *cmdiocbq = NULL;
James Smartf1c3b0f2009-07-19 10:01:32 -0400374 IOCB_t *cmd;
James Smarta33c4f72013-03-01 16:36:00 -0500375 struct lpfc_dmabuf *bmp = NULL, *cmp = NULL, *rmp = NULL;
James Smartf1c3b0f2009-07-19 10:01:32 -0400376 int request_nseg;
377 int reply_nseg;
James Smart4cc0e562010-01-26 23:09:48 -0500378 struct bsg_job_data *dd_data;
379 uint32_t creg_val;
James Smartf1c3b0f2009-07-19 10:01:32 -0400380 int rc = 0;
James Smartd439d282010-09-29 11:18:45 -0400381 int iocb_stat;
James Smartf1c3b0f2009-07-19 10:01:32 -0400382
383 /* in case no data is transferred */
384 job->reply->reply_payload_rcv_len = 0;
385
James Smart4cc0e562010-01-26 23:09:48 -0500386 /* allocate our bsg tracking structure */
387 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
388 if (!dd_data) {
389 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
390 "2733 Failed allocation of dd_data\n");
391 rc = -ENOMEM;
392 goto no_dd_data;
393 }
394
James Smartf1c3b0f2009-07-19 10:01:32 -0400395 if (!lpfc_nlp_get(ndlp)) {
James Smart4cc0e562010-01-26 23:09:48 -0500396 rc = -ENODEV;
397 goto no_ndlp;
398 }
399
James Smartf1c3b0f2009-07-19 10:01:32 -0400400 if (ndlp->nlp_flag & NLP_ELS_SND_MASK) {
401 rc = -ENODEV;
James Smarta33c4f72013-03-01 16:36:00 -0500402 goto free_ndlp;
James Smartf1c3b0f2009-07-19 10:01:32 -0400403 }
404
James Smartf1c3b0f2009-07-19 10:01:32 -0400405 cmdiocbq = lpfc_sli_get_iocbq(phba);
406 if (!cmdiocbq) {
407 rc = -ENOMEM;
James Smarta33c4f72013-03-01 16:36:00 -0500408 goto free_ndlp;
James Smartf1c3b0f2009-07-19 10:01:32 -0400409 }
James Smartf1c3b0f2009-07-19 10:01:32 -0400410
James Smart4cc0e562010-01-26 23:09:48 -0500411 cmd = &cmdiocbq->iocb;
James Smarta33c4f72013-03-01 16:36:00 -0500412
413 bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
414 if (!bmp) {
James Smartf1c3b0f2009-07-19 10:01:32 -0400415 rc = -ENOMEM;
James Smartbe858b62010-12-15 17:57:20 -0500416 goto free_cmdiocbq;
James Smartf1c3b0f2009-07-19 10:01:32 -0400417 }
James Smarta33c4f72013-03-01 16:36:00 -0500418 bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
419 if (!bmp->virt) {
420 rc = -ENOMEM;
421 goto free_bmp;
James Smartf1c3b0f2009-07-19 10:01:32 -0400422 }
423
James Smarta33c4f72013-03-01 16:36:00 -0500424 INIT_LIST_HEAD(&bmp->list);
425
426 bpl = (struct ulp_bde64 *) bmp->virt;
427 request_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64);
428 cmp = lpfc_alloc_bsg_buffers(phba, job->request_payload.payload_len,
429 1, bpl, &request_nseg);
430 if (!cmp) {
431 rc = -ENOMEM;
432 goto free_bmp;
433 }
434 lpfc_bsg_copy_data(cmp, &job->request_payload,
435 job->request_payload.payload_len, 1);
436
437 bpl += request_nseg;
438 reply_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64) - request_nseg;
439 rmp = lpfc_alloc_bsg_buffers(phba, job->reply_payload.payload_len, 0,
440 bpl, &reply_nseg);
441 if (!rmp) {
442 rc = -ENOMEM;
443 goto free_cmp;
James Smartf1c3b0f2009-07-19 10:01:32 -0400444 }
445
446 cmd->un.genreq64.bdl.ulpIoTag32 = 0;
447 cmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
448 cmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
449 cmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
450 cmd->un.genreq64.bdl.bdeSize =
451 (request_nseg + reply_nseg) * sizeof(struct ulp_bde64);
452 cmd->ulpCommand = CMD_GEN_REQUEST64_CR;
453 cmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
454 cmd->un.genreq64.w5.hcsw.Dfctl = 0;
James Smart6a9c52c2009-10-02 15:16:51 -0400455 cmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
456 cmd->un.genreq64.w5.hcsw.Type = FC_TYPE_CT;
James Smartf1c3b0f2009-07-19 10:01:32 -0400457 cmd->ulpBdeCount = 1;
458 cmd->ulpLe = 1;
459 cmd->ulpClass = CLASS3;
460 cmd->ulpContext = ndlp->nlp_rpi;
James Smart6d368e52011-05-24 11:44:12 -0400461 if (phba->sli_rev == LPFC_SLI_REV4)
462 cmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smartf1c3b0f2009-07-19 10:01:32 -0400463 cmd->ulpOwner = OWN_CHIP;
464 cmdiocbq->vport = phba->pport;
James Smart4cc0e562010-01-26 23:09:48 -0500465 cmdiocbq->context3 = bmp;
James Smartf1c3b0f2009-07-19 10:01:32 -0400466 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
James Smartf1c3b0f2009-07-19 10:01:32 -0400467 timeout = phba->fc_ratov * 2;
James Smart4cc0e562010-01-26 23:09:48 -0500468 cmd->ulpTimeout = timeout;
James Smartf1c3b0f2009-07-19 10:01:32 -0400469
James Smart4cc0e562010-01-26 23:09:48 -0500470 cmdiocbq->iocb_cmpl = lpfc_bsg_send_mgmt_cmd_cmp;
James Smarta33c4f72013-03-01 16:36:00 -0500471 cmdiocbq->context1 = dd_data;
472 cmdiocbq->context2 = cmp;
473 cmdiocbq->context3 = bmp;
James Smart4cc0e562010-01-26 23:09:48 -0500474 dd_data->type = TYPE_IOCB;
James Smarta33c4f72013-03-01 16:36:00 -0500475 dd_data->set_job = job;
James Smart4cc0e562010-01-26 23:09:48 -0500476 dd_data->context_un.iocb.cmdiocbq = cmdiocbq;
James Smarta33c4f72013-03-01 16:36:00 -0500477 dd_data->context_un.iocb.ndlp = ndlp;
478 dd_data->context_un.iocb.rmp = rmp;
479 job->dd_data = dd_data;
James Smartf1c3b0f2009-07-19 10:01:32 -0400480
James Smart4cc0e562010-01-26 23:09:48 -0500481 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -0500482 if (lpfc_readl(phba->HCregaddr, &creg_val)) {
483 rc = -EIO ;
James Smarta33c4f72013-03-01 16:36:00 -0500484 goto free_rmp;
James Smart9940b972011-03-11 16:06:12 -0500485 }
James Smart4cc0e562010-01-26 23:09:48 -0500486 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
487 writel(creg_val, phba->HCregaddr);
488 readl(phba->HCregaddr); /* flush */
James Smartf1c3b0f2009-07-19 10:01:32 -0400489 }
490
James Smartd439d282010-09-29 11:18:45 -0400491 iocb_stat = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq, 0);
492 if (iocb_stat == IOCB_SUCCESS)
James Smart4cc0e562010-01-26 23:09:48 -0500493 return 0; /* done for now */
James Smartd439d282010-09-29 11:18:45 -0400494 else if (iocb_stat == IOCB_BUSY)
495 rc = -EAGAIN;
James Smart2a9bf3d2010-06-07 15:24:45 -0400496 else
James Smartd439d282010-09-29 11:18:45 -0400497 rc = -EIO;
James Smart2a9bf3d2010-06-07 15:24:45 -0400498
James Smart4cc0e562010-01-26 23:09:48 -0500499 /* iocb failed so cleanup */
James Smartf1c3b0f2009-07-19 10:01:32 -0400500
James Smarta33c4f72013-03-01 16:36:00 -0500501free_rmp:
502 lpfc_free_bsg_buffers(phba, rmp);
503free_cmp:
504 lpfc_free_bsg_buffers(phba, cmp);
505free_bmp:
506 if (bmp->virt)
507 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
508 kfree(bmp);
James Smartf1c3b0f2009-07-19 10:01:32 -0400509free_cmdiocbq:
510 lpfc_sli_release_iocbq(phba, cmdiocbq);
James Smart4cc0e562010-01-26 23:09:48 -0500511free_ndlp:
James Smartf1c3b0f2009-07-19 10:01:32 -0400512 lpfc_nlp_put(ndlp);
James Smart4cc0e562010-01-26 23:09:48 -0500513no_ndlp:
514 kfree(dd_data);
515no_dd_data:
James Smartf1c3b0f2009-07-19 10:01:32 -0400516 /* make error code available to userspace */
517 job->reply->result = rc;
James Smart4cc0e562010-01-26 23:09:48 -0500518 job->dd_data = NULL;
519 return rc;
520}
521
522/**
523 * lpfc_bsg_rport_els_cmp - lpfc_bsg_rport_els's completion handler
524 * @phba: Pointer to HBA context object.
525 * @cmdiocbq: Pointer to command iocb.
526 * @rspiocbq: Pointer to response iocb.
527 *
528 * This function is the completion handler for iocbs issued using
529 * lpfc_bsg_rport_els_cmp function. This function is called by the
530 * ring event handler function without any lock held. This function
531 * can be called from both worker thread context and interrupt
532 * context. This function also can be called from other thread which
533 * cleans up the SLI layer objects.
James Smart3b5dd522010-01-26 23:10:15 -0500534 * This function copies the contents of the response iocb to the
James Smart4cc0e562010-01-26 23:09:48 -0500535 * response iocb memory object provided by the caller of
536 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
537 * sleeps for the iocb completion.
538 **/
539static void
540lpfc_bsg_rport_els_cmp(struct lpfc_hba *phba,
541 struct lpfc_iocbq *cmdiocbq,
542 struct lpfc_iocbq *rspiocbq)
543{
544 struct bsg_job_data *dd_data;
545 struct fc_bsg_job *job;
546 IOCB_t *rsp;
547 struct lpfc_nodelist *ndlp;
James Smarta33c4f72013-03-01 16:36:00 -0500548 struct lpfc_dmabuf *pcmd = NULL, *prsp = NULL;
James Smart4cc0e562010-01-26 23:09:48 -0500549 struct fc_bsg_ctels_reply *els_reply;
550 uint8_t *rjt_data;
551 unsigned long flags;
James Smarta33c4f72013-03-01 16:36:00 -0500552 unsigned int rsp_size;
James Smart4cc0e562010-01-26 23:09:48 -0500553 int rc = 0;
554
James Smart4cc0e562010-01-26 23:09:48 -0500555 dd_data = cmdiocbq->context1;
James Smarta33c4f72013-03-01 16:36:00 -0500556 ndlp = dd_data->context_un.iocb.ndlp;
557 cmdiocbq->context1 = ndlp;
558
559 /* Determine if job has been aborted */
560 spin_lock_irqsave(&phba->ct_ev_lock, flags);
561 job = dd_data->set_job;
562 if (job) {
563 /* Prevent timeout handling from trying to abort job */
564 job->dd_data = NULL;
565 }
566 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
567
568 rsp = &rspiocbq->iocb;
569 pcmd = (struct lpfc_dmabuf *)cmdiocbq->context2;
570 prsp = (struct lpfc_dmabuf *)pcmd->list.next;
571
572 /* Copy the completed job data or determine the job status if job is
573 * still active
574 */
575
576 if (job) {
577 if (rsp->ulpStatus == IOSTAT_SUCCESS) {
578 rsp_size = rsp->un.elsreq64.bdl.bdeSize;
579 job->reply->reply_payload_rcv_len =
580 sg_copy_from_buffer(job->reply_payload.sg_list,
581 job->reply_payload.sg_cnt,
582 prsp->virt,
583 rsp_size);
584 } else if (rsp->ulpStatus == IOSTAT_LS_RJT) {
585 job->reply->reply_payload_rcv_len =
586 sizeof(struct fc_bsg_ctels_reply);
587 /* LS_RJT data returned in word 4 */
588 rjt_data = (uint8_t *)&rsp->un.ulpWord[4];
589 els_reply = &job->reply->reply_data.ctels_reply;
590 els_reply->status = FC_CTELS_STATUS_REJECT;
591 els_reply->rjt_data.action = rjt_data[3];
592 els_reply->rjt_data.reason_code = rjt_data[2];
593 els_reply->rjt_data.reason_explanation = rjt_data[1];
594 els_reply->rjt_data.vendor_unique = rjt_data[0];
595 } else {
596 rc = -EIO;
597 }
James Smart4cc0e562010-01-26 23:09:48 -0500598 }
599
James Smart4cc0e562010-01-26 23:09:48 -0500600 lpfc_nlp_put(ndlp);
James Smarta33c4f72013-03-01 16:36:00 -0500601 lpfc_els_free_iocb(phba, cmdiocbq);
James Smart4cc0e562010-01-26 23:09:48 -0500602 kfree(dd_data);
James Smarta33c4f72013-03-01 16:36:00 -0500603
604 /* Complete the job if the job is still active */
605
606 if (job) {
607 job->reply->result = rc;
608 job->job_done(job);
609 }
James Smart4cc0e562010-01-26 23:09:48 -0500610 return;
James Smartf1c3b0f2009-07-19 10:01:32 -0400611}
612
613/**
614 * lpfc_bsg_rport_els - send an ELS command from a bsg request
615 * @job: fc_bsg_job to handle
James Smart3b5dd522010-01-26 23:10:15 -0500616 **/
James Smartf1c3b0f2009-07-19 10:01:32 -0400617static int
618lpfc_bsg_rport_els(struct fc_bsg_job *job)
619{
620 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
621 struct lpfc_hba *phba = vport->phba;
622 struct lpfc_rport_data *rdata = job->rport->dd_data;
623 struct lpfc_nodelist *ndlp = rdata->pnode;
James Smartf1c3b0f2009-07-19 10:01:32 -0400624 uint32_t elscmd;
625 uint32_t cmdsize;
626 uint32_t rspsize;
James Smartf1c3b0f2009-07-19 10:01:32 -0400627 struct lpfc_iocbq *cmdiocbq;
James Smartf1c3b0f2009-07-19 10:01:32 -0400628 uint16_t rpi = 0;
James Smart4cc0e562010-01-26 23:09:48 -0500629 struct bsg_job_data *dd_data;
630 uint32_t creg_val;
James Smartf1c3b0f2009-07-19 10:01:32 -0400631 int rc = 0;
632
633 /* in case no data is transferred */
634 job->reply->reply_payload_rcv_len = 0;
635
James Smarta33c4f72013-03-01 16:36:00 -0500636 /* verify the els command is not greater than the
637 * maximum ELS transfer size.
638 */
639
640 if (job->request_payload.payload_len > FCELSSIZE) {
641 rc = -EINVAL;
642 goto no_dd_data;
643 }
644
James Smart4cc0e562010-01-26 23:09:48 -0500645 /* allocate our bsg tracking structure */
646 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
647 if (!dd_data) {
648 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
649 "2735 Failed allocation of dd_data\n");
650 rc = -ENOMEM;
651 goto no_dd_data;
652 }
653
James Smarta33c4f72013-03-01 16:36:00 -0500654 elscmd = job->request->rqst_data.r_els.els_code;
655 cmdsize = job->request_payload.payload_len;
656 rspsize = job->reply_payload.payload_len;
657
James Smartf1c3b0f2009-07-19 10:01:32 -0400658 if (!lpfc_nlp_get(ndlp)) {
659 rc = -ENODEV;
James Smart4cc0e562010-01-26 23:09:48 -0500660 goto free_dd_data;
James Smartf1c3b0f2009-07-19 10:01:32 -0400661 }
662
James Smarta33c4f72013-03-01 16:36:00 -0500663 /* We will use the allocated dma buffers by prep els iocb for command
664 * and response to ensure if the job times out and the request is freed,
665 * we won't be dma into memory that is no longer allocated to for the
666 * request.
667 */
James Smartf1c3b0f2009-07-19 10:01:32 -0400668
James Smart4cc0e562010-01-26 23:09:48 -0500669 cmdiocbq = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp,
James Smartf1c3b0f2009-07-19 10:01:32 -0400670 ndlp->nlp_DID, elscmd);
James Smartf1c3b0f2009-07-19 10:01:32 -0400671 if (!cmdiocbq) {
James Smart4cc0e562010-01-26 23:09:48 -0500672 rc = -EIO;
James Smarta33c4f72013-03-01 16:36:00 -0500673 goto release_ndlp;
James Smartf1c3b0f2009-07-19 10:01:32 -0400674 }
675
James Smarta33c4f72013-03-01 16:36:00 -0500676 rpi = ndlp->nlp_rpi;
James Smartf1c3b0f2009-07-19 10:01:32 -0400677
James Smarta33c4f72013-03-01 16:36:00 -0500678 /* Transfer the request payload to allocated command dma buffer */
James Smartf1c3b0f2009-07-19 10:01:32 -0400679
James Smarta33c4f72013-03-01 16:36:00 -0500680 sg_copy_to_buffer(job->request_payload.sg_list,
681 job->request_payload.sg_cnt,
682 ((struct lpfc_dmabuf *)cmdiocbq->context2)->virt,
683 cmdsize);
James Smartf1c3b0f2009-07-19 10:01:32 -0400684
James Smart3ef6d242012-01-18 16:23:48 -0500685 if (phba->sli_rev == LPFC_SLI_REV4)
686 cmdiocbq->iocb.ulpContext = phba->sli4_hba.rpi_ids[rpi];
687 else
688 cmdiocbq->iocb.ulpContext = rpi;
James Smartf1c3b0f2009-07-19 10:01:32 -0400689 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
James Smart4cc0e562010-01-26 23:09:48 -0500690 cmdiocbq->context1 = dd_data;
James Smart93d1379e2012-05-09 21:19:34 -0400691 cmdiocbq->context_un.ndlp = ndlp;
James Smarta33c4f72013-03-01 16:36:00 -0500692 cmdiocbq->iocb_cmpl = lpfc_bsg_rport_els_cmp;
James Smart4cc0e562010-01-26 23:09:48 -0500693 dd_data->type = TYPE_IOCB;
James Smarta33c4f72013-03-01 16:36:00 -0500694 dd_data->set_job = job;
James Smart4cc0e562010-01-26 23:09:48 -0500695 dd_data->context_un.iocb.cmdiocbq = cmdiocbq;
James Smart4cc0e562010-01-26 23:09:48 -0500696 dd_data->context_un.iocb.ndlp = ndlp;
James Smarta33c4f72013-03-01 16:36:00 -0500697 dd_data->context_un.iocb.rmp = NULL;
698 job->dd_data = dd_data;
James Smartf1c3b0f2009-07-19 10:01:32 -0400699
James Smart4cc0e562010-01-26 23:09:48 -0500700 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -0500701 if (lpfc_readl(phba->HCregaddr, &creg_val)) {
702 rc = -EIO;
703 goto linkdown_err;
704 }
James Smart4cc0e562010-01-26 23:09:48 -0500705 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
706 writel(creg_val, phba->HCregaddr);
707 readl(phba->HCregaddr); /* flush */
James Smartf1c3b0f2009-07-19 10:01:32 -0400708 }
James Smarta33c4f72013-03-01 16:36:00 -0500709
James Smart4cc0e562010-01-26 23:09:48 -0500710 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq, 0);
James Smarta33c4f72013-03-01 16:36:00 -0500711
James Smart4cc0e562010-01-26 23:09:48 -0500712 if (rc == IOCB_SUCCESS)
713 return 0; /* done for now */
James Smart2a9bf3d2010-06-07 15:24:45 -0400714 else if (rc == IOCB_BUSY)
James Smartd439d282010-09-29 11:18:45 -0400715 rc = -EAGAIN;
James Smart2a9bf3d2010-06-07 15:24:45 -0400716 else
James Smartd439d282010-09-29 11:18:45 -0400717 rc = -EIO;
James Smartf1c3b0f2009-07-19 10:01:32 -0400718
James Smart9940b972011-03-11 16:06:12 -0500719linkdown_err:
James Smartf1c3b0f2009-07-19 10:01:32 -0400720
James Smarta33c4f72013-03-01 16:36:00 -0500721 cmdiocbq->context1 = ndlp;
722 lpfc_els_free_iocb(phba, cmdiocbq);
James Smartf1c3b0f2009-07-19 10:01:32 -0400723
James Smarta33c4f72013-03-01 16:36:00 -0500724release_ndlp:
725 lpfc_nlp_put(ndlp);
James Smartf1c3b0f2009-07-19 10:01:32 -0400726
James Smart4cc0e562010-01-26 23:09:48 -0500727free_dd_data:
728 kfree(dd_data);
729
730no_dd_data:
James Smartf1c3b0f2009-07-19 10:01:32 -0400731 /* make error code available to userspace */
732 job->reply->result = rc;
James Smart4cc0e562010-01-26 23:09:48 -0500733 job->dd_data = NULL;
734 return rc;
James Smartf1c3b0f2009-07-19 10:01:32 -0400735}
736
James Smart3b5dd522010-01-26 23:10:15 -0500737/**
738 * lpfc_bsg_event_free - frees an allocated event structure
739 * @kref: Pointer to a kref.
740 *
741 * Called from kref_put. Back cast the kref into an event structure address.
742 * Free any events to get, delete associated nodes, free any events to see,
743 * free any data then free the event itself.
744 **/
James Smartf1c3b0f2009-07-19 10:01:32 -0400745static void
James Smart4cc0e562010-01-26 23:09:48 -0500746lpfc_bsg_event_free(struct kref *kref)
James Smartf1c3b0f2009-07-19 10:01:32 -0400747{
James Smart4cc0e562010-01-26 23:09:48 -0500748 struct lpfc_bsg_event *evt = container_of(kref, struct lpfc_bsg_event,
749 kref);
James Smartf1c3b0f2009-07-19 10:01:32 -0400750 struct event_data *ed;
751
752 list_del(&evt->node);
753
754 while (!list_empty(&evt->events_to_get)) {
755 ed = list_entry(evt->events_to_get.next, typeof(*ed), node);
756 list_del(&ed->node);
757 kfree(ed->data);
758 kfree(ed);
759 }
760
761 while (!list_empty(&evt->events_to_see)) {
762 ed = list_entry(evt->events_to_see.next, typeof(*ed), node);
763 list_del(&ed->node);
764 kfree(ed->data);
765 kfree(ed);
766 }
767
James Smarta33c4f72013-03-01 16:36:00 -0500768 kfree(evt->dd_data);
James Smartf1c3b0f2009-07-19 10:01:32 -0400769 kfree(evt);
770}
771
James Smart3b5dd522010-01-26 23:10:15 -0500772/**
773 * lpfc_bsg_event_ref - increments the kref for an event
774 * @evt: Pointer to an event structure.
775 **/
James Smartf1c3b0f2009-07-19 10:01:32 -0400776static inline void
James Smart4cc0e562010-01-26 23:09:48 -0500777lpfc_bsg_event_ref(struct lpfc_bsg_event *evt)
James Smartf1c3b0f2009-07-19 10:01:32 -0400778{
James Smart4cc0e562010-01-26 23:09:48 -0500779 kref_get(&evt->kref);
James Smartf1c3b0f2009-07-19 10:01:32 -0400780}
781
James Smart3b5dd522010-01-26 23:10:15 -0500782/**
783 * lpfc_bsg_event_unref - Uses kref_put to free an event structure
784 * @evt: Pointer to an event structure.
785 **/
James Smartf1c3b0f2009-07-19 10:01:32 -0400786static inline void
James Smart4cc0e562010-01-26 23:09:48 -0500787lpfc_bsg_event_unref(struct lpfc_bsg_event *evt)
James Smartf1c3b0f2009-07-19 10:01:32 -0400788{
James Smart4cc0e562010-01-26 23:09:48 -0500789 kref_put(&evt->kref, lpfc_bsg_event_free);
James Smartf1c3b0f2009-07-19 10:01:32 -0400790}
791
James Smart3b5dd522010-01-26 23:10:15 -0500792/**
793 * lpfc_bsg_event_new - allocate and initialize a event structure
794 * @ev_mask: Mask of events.
795 * @ev_reg_id: Event reg id.
796 * @ev_req_id: Event request id.
797 **/
James Smart4cc0e562010-01-26 23:09:48 -0500798static struct lpfc_bsg_event *
799lpfc_bsg_event_new(uint32_t ev_mask, int ev_reg_id, uint32_t ev_req_id)
800{
801 struct lpfc_bsg_event *evt = kzalloc(sizeof(*evt), GFP_KERNEL);
James Smartf1c3b0f2009-07-19 10:01:32 -0400802
James Smart4cc0e562010-01-26 23:09:48 -0500803 if (!evt)
804 return NULL;
805
806 INIT_LIST_HEAD(&evt->events_to_get);
807 INIT_LIST_HEAD(&evt->events_to_see);
808 evt->type_mask = ev_mask;
809 evt->req_id = ev_req_id;
810 evt->reg_id = ev_reg_id;
811 evt->wait_time_stamp = jiffies;
James Smarta33c4f72013-03-01 16:36:00 -0500812 evt->dd_data = NULL;
James Smart4cc0e562010-01-26 23:09:48 -0500813 init_waitqueue_head(&evt->wq);
814 kref_init(&evt->kref);
815 return evt;
816}
817
James Smart3b5dd522010-01-26 23:10:15 -0500818/**
819 * diag_cmd_data_free - Frees an lpfc dma buffer extension
820 * @phba: Pointer to HBA context object.
821 * @mlist: Pointer to an lpfc dma buffer extension.
822 **/
James Smart4cc0e562010-01-26 23:09:48 -0500823static int
James Smart3b5dd522010-01-26 23:10:15 -0500824diag_cmd_data_free(struct lpfc_hba *phba, struct lpfc_dmabufext *mlist)
James Smart4cc0e562010-01-26 23:09:48 -0500825{
826 struct lpfc_dmabufext *mlast;
827 struct pci_dev *pcidev;
828 struct list_head head, *curr, *next;
829
830 if ((!mlist) || (!lpfc_is_link_up(phba) &&
831 (phba->link_flag & LS_LOOPBACK_MODE))) {
832 return 0;
833 }
834
835 pcidev = phba->pcidev;
836 list_add_tail(&head, &mlist->dma.list);
837
838 list_for_each_safe(curr, next, &head) {
839 mlast = list_entry(curr, struct lpfc_dmabufext , dma.list);
840 if (mlast->dma.virt)
841 dma_free_coherent(&pcidev->dev,
842 mlast->size,
843 mlast->dma.virt,
844 mlast->dma.phys);
845 kfree(mlast);
846 }
847 return 0;
848}
James Smartf1c3b0f2009-07-19 10:01:32 -0400849
850/**
851 * lpfc_bsg_ct_unsol_event - process an unsolicited CT command
852 * @phba:
853 * @pring:
854 * @piocbq:
855 *
856 * This function is called when an unsolicited CT command is received. It
James Smart4cc0e562010-01-26 23:09:48 -0500857 * forwards the event to any processes registered to receive CT events.
James Smart3b5dd522010-01-26 23:10:15 -0500858 **/
James Smart4fede782010-01-26 23:08:55 -0500859int
James Smartf1c3b0f2009-07-19 10:01:32 -0400860lpfc_bsg_ct_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
861 struct lpfc_iocbq *piocbq)
862{
863 uint32_t evt_req_id = 0;
864 uint32_t cmd;
865 uint32_t len;
866 struct lpfc_dmabuf *dmabuf = NULL;
James Smart4cc0e562010-01-26 23:09:48 -0500867 struct lpfc_bsg_event *evt;
James Smartf1c3b0f2009-07-19 10:01:32 -0400868 struct event_data *evt_dat = NULL;
869 struct lpfc_iocbq *iocbq;
870 size_t offset = 0;
871 struct list_head head;
872 struct ulp_bde64 *bde;
873 dma_addr_t dma_addr;
874 int i;
875 struct lpfc_dmabuf *bdeBuf1 = piocbq->context2;
876 struct lpfc_dmabuf *bdeBuf2 = piocbq->context3;
877 struct lpfc_hbq_entry *hbqe;
878 struct lpfc_sli_ct_request *ct_req;
James Smart4cc0e562010-01-26 23:09:48 -0500879 struct fc_bsg_job *job = NULL;
James Smarta33c4f72013-03-01 16:36:00 -0500880 struct bsg_job_data *dd_data = NULL;
James Smart4fede782010-01-26 23:08:55 -0500881 unsigned long flags;
James Smart4cc0e562010-01-26 23:09:48 -0500882 int size = 0;
James Smartf1c3b0f2009-07-19 10:01:32 -0400883
884 INIT_LIST_HEAD(&head);
885 list_add_tail(&head, &piocbq->list);
886
887 if (piocbq->iocb.ulpBdeCount == 0 ||
888 piocbq->iocb.un.cont64[0].tus.f.bdeSize == 0)
889 goto error_ct_unsol_exit;
890
James Smart4cc0e562010-01-26 23:09:48 -0500891 if (phba->link_state == LPFC_HBA_ERROR ||
892 (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)))
893 goto error_ct_unsol_exit;
894
James Smartf1c3b0f2009-07-19 10:01:32 -0400895 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
896 dmabuf = bdeBuf1;
897 else {
898 dma_addr = getPaddr(piocbq->iocb.un.cont64[0].addrHigh,
899 piocbq->iocb.un.cont64[0].addrLow);
900 dmabuf = lpfc_sli_ringpostbuf_get(phba, pring, dma_addr);
901 }
James Smart4cc0e562010-01-26 23:09:48 -0500902 if (dmabuf == NULL)
903 goto error_ct_unsol_exit;
James Smartf1c3b0f2009-07-19 10:01:32 -0400904 ct_req = (struct lpfc_sli_ct_request *)dmabuf->virt;
905 evt_req_id = ct_req->FsType;
906 cmd = ct_req->CommandResponse.bits.CmdRsp;
907 len = ct_req->CommandResponse.bits.Size;
908 if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
909 lpfc_sli_ringpostbuf_put(phba, pring, dmabuf);
910
James Smart4fede782010-01-26 23:08:55 -0500911 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -0400912 list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
James Smart4cc0e562010-01-26 23:09:48 -0500913 if (!(evt->type_mask & FC_REG_CT_EVENT) ||
914 evt->req_id != evt_req_id)
James Smartf1c3b0f2009-07-19 10:01:32 -0400915 continue;
916
James Smart4cc0e562010-01-26 23:09:48 -0500917 lpfc_bsg_event_ref(evt);
918 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -0400919 evt_dat = kzalloc(sizeof(*evt_dat), GFP_KERNEL);
James Smart4cc0e562010-01-26 23:09:48 -0500920 if (evt_dat == NULL) {
921 spin_lock_irqsave(&phba->ct_ev_lock, flags);
922 lpfc_bsg_event_unref(evt);
James Smartf1c3b0f2009-07-19 10:01:32 -0400923 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
924 "2614 Memory allocation failed for "
925 "CT event\n");
926 break;
927 }
928
James Smartf1c3b0f2009-07-19 10:01:32 -0400929 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
930 /* take accumulated byte count from the last iocbq */
931 iocbq = list_entry(head.prev, typeof(*iocbq), list);
932 evt_dat->len = iocbq->iocb.unsli3.rcvsli3.acc_len;
933 } else {
934 list_for_each_entry(iocbq, &head, list) {
935 for (i = 0; i < iocbq->iocb.ulpBdeCount; i++)
936 evt_dat->len +=
937 iocbq->iocb.un.cont64[i].tus.f.bdeSize;
938 }
939 }
940
941 evt_dat->data = kzalloc(evt_dat->len, GFP_KERNEL);
James Smart4cc0e562010-01-26 23:09:48 -0500942 if (evt_dat->data == NULL) {
James Smartf1c3b0f2009-07-19 10:01:32 -0400943 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
944 "2615 Memory allocation failed for "
945 "CT event data, size %d\n",
946 evt_dat->len);
947 kfree(evt_dat);
James Smart4fede782010-01-26 23:08:55 -0500948 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smart4cc0e562010-01-26 23:09:48 -0500949 lpfc_bsg_event_unref(evt);
James Smart4fede782010-01-26 23:08:55 -0500950 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -0400951 goto error_ct_unsol_exit;
952 }
953
954 list_for_each_entry(iocbq, &head, list) {
James Smart4cc0e562010-01-26 23:09:48 -0500955 size = 0;
James Smartf1c3b0f2009-07-19 10:01:32 -0400956 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
957 bdeBuf1 = iocbq->context2;
958 bdeBuf2 = iocbq->context3;
959 }
960 for (i = 0; i < iocbq->iocb.ulpBdeCount; i++) {
James Smartf1c3b0f2009-07-19 10:01:32 -0400961 if (phba->sli3_options &
962 LPFC_SLI3_HBQ_ENABLED) {
963 if (i == 0) {
964 hbqe = (struct lpfc_hbq_entry *)
965 &iocbq->iocb.un.ulpWord[0];
966 size = hbqe->bde.tus.f.bdeSize;
967 dmabuf = bdeBuf1;
968 } else if (i == 1) {
969 hbqe = (struct lpfc_hbq_entry *)
970 &iocbq->iocb.unsli3.
971 sli3Words[4];
972 size = hbqe->bde.tus.f.bdeSize;
973 dmabuf = bdeBuf2;
974 }
975 if ((offset + size) > evt_dat->len)
976 size = evt_dat->len - offset;
977 } else {
978 size = iocbq->iocb.un.cont64[i].
979 tus.f.bdeSize;
980 bde = &iocbq->iocb.un.cont64[i];
981 dma_addr = getPaddr(bde->addrHigh,
982 bde->addrLow);
983 dmabuf = lpfc_sli_ringpostbuf_get(phba,
984 pring, dma_addr);
985 }
986 if (!dmabuf) {
987 lpfc_printf_log(phba, KERN_ERR,
988 LOG_LIBDFC, "2616 No dmabuf "
989 "found for iocbq 0x%p\n",
990 iocbq);
991 kfree(evt_dat->data);
992 kfree(evt_dat);
James Smart4fede782010-01-26 23:08:55 -0500993 spin_lock_irqsave(&phba->ct_ev_lock,
994 flags);
James Smart4cc0e562010-01-26 23:09:48 -0500995 lpfc_bsg_event_unref(evt);
James Smart4fede782010-01-26 23:08:55 -0500996 spin_unlock_irqrestore(
997 &phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -0400998 goto error_ct_unsol_exit;
999 }
1000 memcpy((char *)(evt_dat->data) + offset,
1001 dmabuf->virt, size);
1002 offset += size;
1003 if (evt_req_id != SLI_CT_ELX_LOOPBACK &&
1004 !(phba->sli3_options &
1005 LPFC_SLI3_HBQ_ENABLED)) {
1006 lpfc_sli_ringpostbuf_put(phba, pring,
1007 dmabuf);
1008 } else {
1009 switch (cmd) {
James Smart4cc0e562010-01-26 23:09:48 -05001010 case ELX_LOOPBACK_DATA:
James Smart1b511972011-12-13 13:23:09 -05001011 if (phba->sli_rev <
1012 LPFC_SLI_REV4)
1013 diag_cmd_data_free(phba,
1014 (struct lpfc_dmabufext
1015 *)dmabuf);
James Smart4cc0e562010-01-26 23:09:48 -05001016 break;
James Smartf1c3b0f2009-07-19 10:01:32 -04001017 case ELX_LOOPBACK_XRI_SETUP:
James Smart4cc0e562010-01-26 23:09:48 -05001018 if ((phba->sli_rev ==
1019 LPFC_SLI_REV2) ||
1020 (phba->sli3_options &
1021 LPFC_SLI3_HBQ_ENABLED
1022 )) {
1023 lpfc_in_buf_free(phba,
1024 dmabuf);
1025 } else {
James Smartf1c3b0f2009-07-19 10:01:32 -04001026 lpfc_post_buffer(phba,
1027 pring,
1028 1);
James Smart4cc0e562010-01-26 23:09:48 -05001029 }
James Smartf1c3b0f2009-07-19 10:01:32 -04001030 break;
1031 default:
1032 if (!(phba->sli3_options &
1033 LPFC_SLI3_HBQ_ENABLED))
1034 lpfc_post_buffer(phba,
1035 pring,
1036 1);
1037 break;
1038 }
1039 }
1040 }
1041 }
1042
James Smart4fede782010-01-26 23:08:55 -05001043 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -04001044 if (phba->sli_rev == LPFC_SLI_REV4) {
1045 evt_dat->immed_dat = phba->ctx_idx;
James Smart6dd9e312013-01-03 15:43:37 -05001046 phba->ctx_idx = (phba->ctx_idx + 1) % LPFC_CT_CTX_MAX;
James Smart589a52d2010-07-14 15:30:54 -04001047 /* Provide warning for over-run of the ct_ctx array */
James Smart6dd9e312013-01-03 15:43:37 -05001048 if (phba->ct_ctx[evt_dat->immed_dat].valid ==
James Smart589a52d2010-07-14 15:30:54 -04001049 UNSOL_VALID)
1050 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
1051 "2717 CT context array entry "
1052 "[%d] over-run: oxid:x%x, "
1053 "sid:x%x\n", phba->ctx_idx,
1054 phba->ct_ctx[
1055 evt_dat->immed_dat].oxid,
1056 phba->ct_ctx[
1057 evt_dat->immed_dat].SID);
James Smart7851fe22011-07-22 18:36:52 -04001058 phba->ct_ctx[evt_dat->immed_dat].rxid =
1059 piocbq->iocb.ulpContext;
James Smartf1c3b0f2009-07-19 10:01:32 -04001060 phba->ct_ctx[evt_dat->immed_dat].oxid =
James Smart7851fe22011-07-22 18:36:52 -04001061 piocbq->iocb.unsli3.rcvsli3.ox_id;
James Smartf1c3b0f2009-07-19 10:01:32 -04001062 phba->ct_ctx[evt_dat->immed_dat].SID =
1063 piocbq->iocb.un.rcvels.remoteID;
James Smart6dd9e312013-01-03 15:43:37 -05001064 phba->ct_ctx[evt_dat->immed_dat].valid = UNSOL_VALID;
James Smartf1c3b0f2009-07-19 10:01:32 -04001065 } else
1066 evt_dat->immed_dat = piocbq->iocb.ulpContext;
1067
1068 evt_dat->type = FC_REG_CT_EVENT;
1069 list_add(&evt_dat->node, &evt->events_to_see);
James Smart4cc0e562010-01-26 23:09:48 -05001070 if (evt_req_id == SLI_CT_ELX_LOOPBACK) {
1071 wake_up_interruptible(&evt->wq);
1072 lpfc_bsg_event_unref(evt);
James Smartf1c3b0f2009-07-19 10:01:32 -04001073 break;
James Smart4cc0e562010-01-26 23:09:48 -05001074 }
1075
1076 list_move(evt->events_to_see.prev, &evt->events_to_get);
James Smart4cc0e562010-01-26 23:09:48 -05001077
James Smarta33c4f72013-03-01 16:36:00 -05001078 dd_data = (struct bsg_job_data *)evt->dd_data;
1079 job = dd_data->set_job;
1080 dd_data->set_job = NULL;
1081 lpfc_bsg_event_unref(evt);
James Smart4cc0e562010-01-26 23:09:48 -05001082 if (job) {
1083 job->reply->reply_payload_rcv_len = size;
1084 /* make error code available to userspace */
1085 job->reply->result = 0;
1086 job->dd_data = NULL;
1087 /* complete the job back to userspace */
1088 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1089 job->job_done(job);
1090 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1091 }
James Smartf1c3b0f2009-07-19 10:01:32 -04001092 }
James Smart4fede782010-01-26 23:08:55 -05001093 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -04001094
1095error_ct_unsol_exit:
1096 if (!list_empty(&head))
1097 list_del(&head);
James Smart1b511972011-12-13 13:23:09 -05001098 if ((phba->sli_rev < LPFC_SLI_REV4) &&
1099 (evt_req_id == SLI_CT_ELX_LOOPBACK))
James Smart4cc0e562010-01-26 23:09:48 -05001100 return 0;
James Smart4fede782010-01-26 23:08:55 -05001101 return 1;
James Smartf1c3b0f2009-07-19 10:01:32 -04001102}
1103
1104/**
James Smart6dd9e312013-01-03 15:43:37 -05001105 * lpfc_bsg_ct_unsol_abort - handler ct abort to management plane
1106 * @phba: Pointer to HBA context object.
1107 * @dmabuf: pointer to a dmabuf that describes the FC sequence
1108 *
1109 * This function handles abort to the CT command toward management plane
1110 * for SLI4 port.
1111 *
1112 * If the pending context of a CT command to management plane present, clears
1113 * such context and returns 1 for handled; otherwise, it returns 0 indicating
1114 * no context exists.
1115 **/
1116int
1117lpfc_bsg_ct_unsol_abort(struct lpfc_hba *phba, struct hbq_dmabuf *dmabuf)
1118{
1119 struct fc_frame_header fc_hdr;
1120 struct fc_frame_header *fc_hdr_ptr = &fc_hdr;
1121 int ctx_idx, handled = 0;
1122 uint16_t oxid, rxid;
1123 uint32_t sid;
1124
1125 memcpy(fc_hdr_ptr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
1126 sid = sli4_sid_from_fc_hdr(fc_hdr_ptr);
1127 oxid = be16_to_cpu(fc_hdr_ptr->fh_ox_id);
1128 rxid = be16_to_cpu(fc_hdr_ptr->fh_rx_id);
1129
1130 for (ctx_idx = 0; ctx_idx < LPFC_CT_CTX_MAX; ctx_idx++) {
1131 if (phba->ct_ctx[ctx_idx].valid != UNSOL_VALID)
1132 continue;
1133 if (phba->ct_ctx[ctx_idx].rxid != rxid)
1134 continue;
1135 if (phba->ct_ctx[ctx_idx].oxid != oxid)
1136 continue;
1137 if (phba->ct_ctx[ctx_idx].SID != sid)
1138 continue;
1139 phba->ct_ctx[ctx_idx].valid = UNSOL_INVALID;
1140 handled = 1;
1141 }
1142 return handled;
1143}
1144
1145/**
James Smart4cc0e562010-01-26 23:09:48 -05001146 * lpfc_bsg_hba_set_event - process a SET_EVENT bsg vendor command
James Smartf1c3b0f2009-07-19 10:01:32 -04001147 * @job: SET_EVENT fc_bsg_job
James Smart3b5dd522010-01-26 23:10:15 -05001148 **/
James Smartf1c3b0f2009-07-19 10:01:32 -04001149static int
James Smart4cc0e562010-01-26 23:09:48 -05001150lpfc_bsg_hba_set_event(struct fc_bsg_job *job)
James Smartf1c3b0f2009-07-19 10:01:32 -04001151{
1152 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
1153 struct lpfc_hba *phba = vport->phba;
1154 struct set_ct_event *event_req;
James Smart4cc0e562010-01-26 23:09:48 -05001155 struct lpfc_bsg_event *evt;
James Smartf1c3b0f2009-07-19 10:01:32 -04001156 int rc = 0;
James Smart4cc0e562010-01-26 23:09:48 -05001157 struct bsg_job_data *dd_data = NULL;
1158 uint32_t ev_mask;
1159 unsigned long flags;
James Smartf1c3b0f2009-07-19 10:01:32 -04001160
1161 if (job->request_len <
1162 sizeof(struct fc_bsg_request) + sizeof(struct set_ct_event)) {
1163 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1164 "2612 Received SET_CT_EVENT below minimum "
1165 "size\n");
James Smart4cc0e562010-01-26 23:09:48 -05001166 rc = -EINVAL;
1167 goto job_error;
1168 }
1169
James Smartf1c3b0f2009-07-19 10:01:32 -04001170 event_req = (struct set_ct_event *)
1171 job->request->rqst_data.h_vendor.vendor_cmd;
James Smart4cc0e562010-01-26 23:09:48 -05001172 ev_mask = ((uint32_t)(unsigned long)event_req->type_mask &
1173 FC_REG_EVENT_MASK);
James Smart4fede782010-01-26 23:08:55 -05001174 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -04001175 list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
1176 if (evt->reg_id == event_req->ev_reg_id) {
James Smart4cc0e562010-01-26 23:09:48 -05001177 lpfc_bsg_event_ref(evt);
James Smartf1c3b0f2009-07-19 10:01:32 -04001178 evt->wait_time_stamp = jiffies;
James Smarta33c4f72013-03-01 16:36:00 -05001179 dd_data = (struct bsg_job_data *)evt->dd_data;
James Smartf1c3b0f2009-07-19 10:01:32 -04001180 break;
1181 }
1182 }
James Smart4fede782010-01-26 23:08:55 -05001183 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -04001184
1185 if (&evt->node == &phba->ct_ev_waiters) {
1186 /* no event waiting struct yet - first call */
James Smarta33c4f72013-03-01 16:36:00 -05001187 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
1188 if (dd_data == NULL) {
1189 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1190 "2734 Failed allocation of dd_data\n");
1191 rc = -ENOMEM;
1192 goto job_error;
1193 }
James Smart4cc0e562010-01-26 23:09:48 -05001194 evt = lpfc_bsg_event_new(ev_mask, event_req->ev_reg_id,
James Smartf1c3b0f2009-07-19 10:01:32 -04001195 event_req->ev_req_id);
1196 if (!evt) {
1197 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1198 "2617 Failed allocation of event "
1199 "waiter\n");
James Smart4cc0e562010-01-26 23:09:48 -05001200 rc = -ENOMEM;
1201 goto job_error;
James Smartf1c3b0f2009-07-19 10:01:32 -04001202 }
James Smarta33c4f72013-03-01 16:36:00 -05001203 dd_data->type = TYPE_EVT;
1204 dd_data->set_job = NULL;
1205 dd_data->context_un.evt = evt;
1206 evt->dd_data = (void *)dd_data;
James Smart4fede782010-01-26 23:08:55 -05001207 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -04001208 list_add(&evt->node, &phba->ct_ev_waiters);
James Smart4cc0e562010-01-26 23:09:48 -05001209 lpfc_bsg_event_ref(evt);
1210 evt->wait_time_stamp = jiffies;
James Smart4fede782010-01-26 23:08:55 -05001211 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -04001212 }
1213
James Smart4fede782010-01-26 23:08:55 -05001214 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smart4cc0e562010-01-26 23:09:48 -05001215 evt->waiting = 1;
James Smarta33c4f72013-03-01 16:36:00 -05001216 dd_data->set_job = job; /* for unsolicited command */
James Smart4cc0e562010-01-26 23:09:48 -05001217 job->dd_data = dd_data; /* for fc transport timeout callback*/
James Smart4fede782010-01-26 23:08:55 -05001218 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smart4cc0e562010-01-26 23:09:48 -05001219 return 0; /* call job done later */
James Smartf1c3b0f2009-07-19 10:01:32 -04001220
James Smart4cc0e562010-01-26 23:09:48 -05001221job_error:
1222 if (dd_data != NULL)
1223 kfree(dd_data);
James Smartf1c3b0f2009-07-19 10:01:32 -04001224
James Smart4cc0e562010-01-26 23:09:48 -05001225 job->dd_data = NULL;
1226 return rc;
James Smartf1c3b0f2009-07-19 10:01:32 -04001227}
1228
1229/**
James Smart4cc0e562010-01-26 23:09:48 -05001230 * lpfc_bsg_hba_get_event - process a GET_EVENT bsg vendor command
James Smartf1c3b0f2009-07-19 10:01:32 -04001231 * @job: GET_EVENT fc_bsg_job
James Smart3b5dd522010-01-26 23:10:15 -05001232 **/
James Smartf1c3b0f2009-07-19 10:01:32 -04001233static int
James Smart4cc0e562010-01-26 23:09:48 -05001234lpfc_bsg_hba_get_event(struct fc_bsg_job *job)
James Smartf1c3b0f2009-07-19 10:01:32 -04001235{
1236 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
1237 struct lpfc_hba *phba = vport->phba;
1238 struct get_ct_event *event_req;
1239 struct get_ct_event_reply *event_reply;
James Smart4cc0e562010-01-26 23:09:48 -05001240 struct lpfc_bsg_event *evt;
James Smartf1c3b0f2009-07-19 10:01:32 -04001241 struct event_data *evt_dat = NULL;
James Smart4fede782010-01-26 23:08:55 -05001242 unsigned long flags;
James Smart4cc0e562010-01-26 23:09:48 -05001243 uint32_t rc = 0;
James Smartf1c3b0f2009-07-19 10:01:32 -04001244
1245 if (job->request_len <
1246 sizeof(struct fc_bsg_request) + sizeof(struct get_ct_event)) {
1247 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1248 "2613 Received GET_CT_EVENT request below "
1249 "minimum size\n");
James Smart4cc0e562010-01-26 23:09:48 -05001250 rc = -EINVAL;
1251 goto job_error;
James Smartf1c3b0f2009-07-19 10:01:32 -04001252 }
1253
1254 event_req = (struct get_ct_event *)
1255 job->request->rqst_data.h_vendor.vendor_cmd;
1256
1257 event_reply = (struct get_ct_event_reply *)
1258 job->reply->reply_data.vendor_reply.vendor_rsp;
James Smart4fede782010-01-26 23:08:55 -05001259 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -04001260 list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
1261 if (evt->reg_id == event_req->ev_reg_id) {
1262 if (list_empty(&evt->events_to_get))
1263 break;
James Smart4cc0e562010-01-26 23:09:48 -05001264 lpfc_bsg_event_ref(evt);
James Smartf1c3b0f2009-07-19 10:01:32 -04001265 evt->wait_time_stamp = jiffies;
1266 evt_dat = list_entry(evt->events_to_get.prev,
1267 struct event_data, node);
1268 list_del(&evt_dat->node);
1269 break;
1270 }
1271 }
James Smart4fede782010-01-26 23:08:55 -05001272 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -04001273
James Smart4cc0e562010-01-26 23:09:48 -05001274 /* The app may continue to ask for event data until it gets
1275 * an error indicating that there isn't anymore
1276 */
1277 if (evt_dat == NULL) {
James Smartf1c3b0f2009-07-19 10:01:32 -04001278 job->reply->reply_payload_rcv_len = 0;
1279 rc = -ENOENT;
James Smart4cc0e562010-01-26 23:09:48 -05001280 goto job_error;
James Smartf1c3b0f2009-07-19 10:01:32 -04001281 }
1282
James Smart4cc0e562010-01-26 23:09:48 -05001283 if (evt_dat->len > job->request_payload.payload_len) {
1284 evt_dat->len = job->request_payload.payload_len;
1285 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1286 "2618 Truncated event data at %d "
1287 "bytes\n",
1288 job->request_payload.payload_len);
James Smartf1c3b0f2009-07-19 10:01:32 -04001289 }
1290
James Smart4cc0e562010-01-26 23:09:48 -05001291 event_reply->type = evt_dat->type;
James Smartf1c3b0f2009-07-19 10:01:32 -04001292 event_reply->immed_data = evt_dat->immed_dat;
James Smartf1c3b0f2009-07-19 10:01:32 -04001293 if (evt_dat->len > 0)
1294 job->reply->reply_payload_rcv_len =
James Smart4cc0e562010-01-26 23:09:48 -05001295 sg_copy_from_buffer(job->request_payload.sg_list,
1296 job->request_payload.sg_cnt,
James Smartf1c3b0f2009-07-19 10:01:32 -04001297 evt_dat->data, evt_dat->len);
1298 else
1299 job->reply->reply_payload_rcv_len = 0;
James Smartf1c3b0f2009-07-19 10:01:32 -04001300
James Smart4cc0e562010-01-26 23:09:48 -05001301 if (evt_dat) {
James Smartf1c3b0f2009-07-19 10:01:32 -04001302 kfree(evt_dat->data);
James Smart4cc0e562010-01-26 23:09:48 -05001303 kfree(evt_dat);
1304 }
1305
James Smart4fede782010-01-26 23:08:55 -05001306 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smart4cc0e562010-01-26 23:09:48 -05001307 lpfc_bsg_event_unref(evt);
James Smart4fede782010-01-26 23:08:55 -05001308 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smart4cc0e562010-01-26 23:09:48 -05001309 job->dd_data = NULL;
1310 job->reply->result = 0;
James Smartf1c3b0f2009-07-19 10:01:32 -04001311 job->job_done(job);
James Smart4cc0e562010-01-26 23:09:48 -05001312 return 0;
James Smartf1c3b0f2009-07-19 10:01:32 -04001313
James Smart4cc0e562010-01-26 23:09:48 -05001314job_error:
1315 job->dd_data = NULL;
1316 job->reply->result = rc;
James Smartf1c3b0f2009-07-19 10:01:32 -04001317 return rc;
1318}
1319
1320/**
James Smart3b5dd522010-01-26 23:10:15 -05001321 * lpfc_issue_ct_rsp_cmp - lpfc_issue_ct_rsp's completion handler
1322 * @phba: Pointer to HBA context object.
1323 * @cmdiocbq: Pointer to command iocb.
1324 * @rspiocbq: Pointer to response iocb.
1325 *
1326 * This function is the completion handler for iocbs issued using
1327 * lpfc_issue_ct_rsp_cmp function. This function is called by the
1328 * ring event handler function without any lock held. This function
1329 * can be called from both worker thread context and interrupt
1330 * context. This function also can be called from other thread which
1331 * cleans up the SLI layer objects.
1332 * This function copy the contents of the response iocb to the
1333 * response iocb memory object provided by the caller of
1334 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
1335 * sleeps for the iocb completion.
1336 **/
1337static void
1338lpfc_issue_ct_rsp_cmp(struct lpfc_hba *phba,
1339 struct lpfc_iocbq *cmdiocbq,
1340 struct lpfc_iocbq *rspiocbq)
1341{
1342 struct bsg_job_data *dd_data;
1343 struct fc_bsg_job *job;
1344 IOCB_t *rsp;
James Smarta33c4f72013-03-01 16:36:00 -05001345 struct lpfc_dmabuf *bmp, *cmp;
James Smart3b5dd522010-01-26 23:10:15 -05001346 struct lpfc_nodelist *ndlp;
1347 unsigned long flags;
1348 int rc = 0;
1349
James Smarta33c4f72013-03-01 16:36:00 -05001350 dd_data = cmdiocbq->context1;
1351
1352 /* Determine if job has been aborted */
James Smart3b5dd522010-01-26 23:10:15 -05001353 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smarta33c4f72013-03-01 16:36:00 -05001354 job = dd_data->set_job;
1355 if (job) {
1356 /* Prevent timeout handling from trying to abort job */
1357 job->dd_data = NULL;
1358 }
1359 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1360
1361 ndlp = dd_data->context_un.iocb.ndlp;
1362 cmp = cmdiocbq->context2;
1363 bmp = cmdiocbq->context3;
1364 rsp = &rspiocbq->iocb;
1365
1366 /* Copy the completed job data or set the error status */
1367
1368 if (job) {
1369 if (rsp->ulpStatus) {
1370 if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
1371 switch (rsp->un.ulpWord[4] & IOERR_PARAM_MASK) {
1372 case IOERR_SEQUENCE_TIMEOUT:
1373 rc = -ETIMEDOUT;
1374 break;
1375 case IOERR_INVALID_RPI:
1376 rc = -EFAULT;
1377 break;
1378 default:
1379 rc = -EACCES;
1380 break;
1381 }
1382 } else {
1383 rc = -EACCES;
1384 }
1385 } else {
1386 job->reply->reply_payload_rcv_len = 0;
1387 }
James Smart3b5dd522010-01-26 23:10:15 -05001388 }
1389
James Smarta33c4f72013-03-01 16:36:00 -05001390 lpfc_free_bsg_buffers(phba, cmp);
James Smart3b5dd522010-01-26 23:10:15 -05001391 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
James Smarta33c4f72013-03-01 16:36:00 -05001392 kfree(bmp);
James Smart3b5dd522010-01-26 23:10:15 -05001393 lpfc_sli_release_iocbq(phba, cmdiocbq);
1394 lpfc_nlp_put(ndlp);
James Smart3b5dd522010-01-26 23:10:15 -05001395 kfree(dd_data);
James Smarta33c4f72013-03-01 16:36:00 -05001396
1397 /* Complete the job if the job is still active */
1398
1399 if (job) {
1400 job->reply->result = rc;
1401 job->job_done(job);
1402 }
James Smart3b5dd522010-01-26 23:10:15 -05001403 return;
1404}
1405
1406/**
1407 * lpfc_issue_ct_rsp - issue a ct response
1408 * @phba: Pointer to HBA context object.
1409 * @job: Pointer to the job object.
1410 * @tag: tag index value into the ports context exchange array.
1411 * @bmp: Pointer to a dma buffer descriptor.
1412 * @num_entry: Number of enties in the bde.
1413 **/
1414static int
1415lpfc_issue_ct_rsp(struct lpfc_hba *phba, struct fc_bsg_job *job, uint32_t tag,
James Smarta33c4f72013-03-01 16:36:00 -05001416 struct lpfc_dmabuf *cmp, struct lpfc_dmabuf *bmp,
1417 int num_entry)
James Smart3b5dd522010-01-26 23:10:15 -05001418{
1419 IOCB_t *icmd;
1420 struct lpfc_iocbq *ctiocb = NULL;
1421 int rc = 0;
1422 struct lpfc_nodelist *ndlp = NULL;
1423 struct bsg_job_data *dd_data;
1424 uint32_t creg_val;
1425
1426 /* allocate our bsg tracking structure */
1427 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
1428 if (!dd_data) {
1429 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1430 "2736 Failed allocation of dd_data\n");
1431 rc = -ENOMEM;
1432 goto no_dd_data;
1433 }
1434
1435 /* Allocate buffer for command iocb */
1436 ctiocb = lpfc_sli_get_iocbq(phba);
1437 if (!ctiocb) {
James Smartd439d282010-09-29 11:18:45 -04001438 rc = -ENOMEM;
James Smart3b5dd522010-01-26 23:10:15 -05001439 goto no_ctiocb;
1440 }
1441
1442 icmd = &ctiocb->iocb;
1443 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
1444 icmd->un.xseq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
1445 icmd->un.xseq64.bdl.addrLow = putPaddrLow(bmp->phys);
1446 icmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
1447 icmd->un.xseq64.bdl.bdeSize = (num_entry * sizeof(struct ulp_bde64));
1448 icmd->un.xseq64.w5.hcsw.Fctl = (LS | LA);
1449 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
1450 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_SOL_CTL;
1451 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
1452
1453 /* Fill in rest of iocb */
1454 icmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;
1455 icmd->ulpBdeCount = 1;
1456 icmd->ulpLe = 1;
1457 icmd->ulpClass = CLASS3;
1458 if (phba->sli_rev == LPFC_SLI_REV4) {
1459 /* Do not issue unsol response if oxid not marked as valid */
James Smart6dd9e312013-01-03 15:43:37 -05001460 if (phba->ct_ctx[tag].valid != UNSOL_VALID) {
James Smart3b5dd522010-01-26 23:10:15 -05001461 rc = IOCB_ERROR;
1462 goto issue_ct_rsp_exit;
1463 }
James Smart7851fe22011-07-22 18:36:52 -04001464 icmd->ulpContext = phba->ct_ctx[tag].rxid;
1465 icmd->unsli3.rcvsli3.ox_id = phba->ct_ctx[tag].oxid;
James Smart3b5dd522010-01-26 23:10:15 -05001466 ndlp = lpfc_findnode_did(phba->pport, phba->ct_ctx[tag].SID);
1467 if (!ndlp) {
1468 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
1469 "2721 ndlp null for oxid %x SID %x\n",
1470 icmd->ulpContext,
1471 phba->ct_ctx[tag].SID);
1472 rc = IOCB_ERROR;
1473 goto issue_ct_rsp_exit;
1474 }
James Smart589a52d2010-07-14 15:30:54 -04001475
1476 /* Check if the ndlp is active */
1477 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smarta33c4f72013-03-01 16:36:00 -05001478 rc = IOCB_ERROR;
James Smart589a52d2010-07-14 15:30:54 -04001479 goto issue_ct_rsp_exit;
1480 }
1481
1482 /* get a refernece count so the ndlp doesn't go away while
1483 * we respond
1484 */
1485 if (!lpfc_nlp_get(ndlp)) {
James Smarta33c4f72013-03-01 16:36:00 -05001486 rc = IOCB_ERROR;
James Smart589a52d2010-07-14 15:30:54 -04001487 goto issue_ct_rsp_exit;
1488 }
1489
James Smart7851fe22011-07-22 18:36:52 -04001490 icmd->un.ulpWord[3] =
James Smart6d368e52011-05-24 11:44:12 -04001491 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
1492
James Smart3b5dd522010-01-26 23:10:15 -05001493 /* The exchange is done, mark the entry as invalid */
James Smart6dd9e312013-01-03 15:43:37 -05001494 phba->ct_ctx[tag].valid = UNSOL_INVALID;
James Smart3b5dd522010-01-26 23:10:15 -05001495 } else
1496 icmd->ulpContext = (ushort) tag;
1497
1498 icmd->ulpTimeout = phba->fc_ratov * 2;
1499
1500 /* Xmit CT response on exchange <xid> */
1501 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart7851fe22011-07-22 18:36:52 -04001502 "2722 Xmit CT response on exchange x%x Data: x%x x%x x%x\n",
1503 icmd->ulpContext, icmd->ulpIoTag, tag, phba->link_state);
James Smart3b5dd522010-01-26 23:10:15 -05001504
1505 ctiocb->iocb_cmpl = NULL;
1506 ctiocb->iocb_flag |= LPFC_IO_LIBDFC;
1507 ctiocb->vport = phba->pport;
James Smarta33c4f72013-03-01 16:36:00 -05001508 ctiocb->context1 = dd_data;
1509 ctiocb->context2 = cmp;
James Smart3b5dd522010-01-26 23:10:15 -05001510 ctiocb->context3 = bmp;
James Smart3b5dd522010-01-26 23:10:15 -05001511 ctiocb->iocb_cmpl = lpfc_issue_ct_rsp_cmp;
James Smarta33c4f72013-03-01 16:36:00 -05001512
James Smart3b5dd522010-01-26 23:10:15 -05001513 dd_data->type = TYPE_IOCB;
James Smarta33c4f72013-03-01 16:36:00 -05001514 dd_data->set_job = job;
James Smart3b5dd522010-01-26 23:10:15 -05001515 dd_data->context_un.iocb.cmdiocbq = ctiocb;
James Smart3b5dd522010-01-26 23:10:15 -05001516 dd_data->context_un.iocb.ndlp = ndlp;
James Smarta33c4f72013-03-01 16:36:00 -05001517 dd_data->context_un.iocb.rmp = NULL;
1518 job->dd_data = dd_data;
James Smart3b5dd522010-01-26 23:10:15 -05001519
1520 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -05001521 if (lpfc_readl(phba->HCregaddr, &creg_val)) {
1522 rc = -IOCB_ERROR;
1523 goto issue_ct_rsp_exit;
1524 }
James Smart3b5dd522010-01-26 23:10:15 -05001525 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
1526 writel(creg_val, phba->HCregaddr);
1527 readl(phba->HCregaddr); /* flush */
1528 }
1529
1530 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
1531
1532 if (rc == IOCB_SUCCESS)
1533 return 0; /* done for now */
1534
1535issue_ct_rsp_exit:
1536 lpfc_sli_release_iocbq(phba, ctiocb);
1537no_ctiocb:
1538 kfree(dd_data);
1539no_dd_data:
1540 return rc;
1541}
1542
1543/**
1544 * lpfc_bsg_send_mgmt_rsp - process a SEND_MGMT_RESP bsg vendor command
1545 * @job: SEND_MGMT_RESP fc_bsg_job
1546 **/
1547static int
1548lpfc_bsg_send_mgmt_rsp(struct fc_bsg_job *job)
1549{
1550 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
1551 struct lpfc_hba *phba = vport->phba;
1552 struct send_mgmt_resp *mgmt_resp = (struct send_mgmt_resp *)
1553 job->request->rqst_data.h_vendor.vendor_cmd;
1554 struct ulp_bde64 *bpl;
James Smarta33c4f72013-03-01 16:36:00 -05001555 struct lpfc_dmabuf *bmp = NULL, *cmp = NULL;
1556 int bpl_entries;
James Smart3b5dd522010-01-26 23:10:15 -05001557 uint32_t tag = mgmt_resp->tag;
1558 unsigned long reqbfrcnt =
1559 (unsigned long)job->request_payload.payload_len;
1560 int rc = 0;
1561
1562 /* in case no data is transferred */
1563 job->reply->reply_payload_rcv_len = 0;
1564
1565 if (!reqbfrcnt || (reqbfrcnt > (80 * BUF_SZ_4K))) {
1566 rc = -ERANGE;
1567 goto send_mgmt_rsp_exit;
1568 }
1569
1570 bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
1571 if (!bmp) {
1572 rc = -ENOMEM;
1573 goto send_mgmt_rsp_exit;
1574 }
1575
1576 bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
1577 if (!bmp->virt) {
1578 rc = -ENOMEM;
1579 goto send_mgmt_rsp_free_bmp;
1580 }
1581
1582 INIT_LIST_HEAD(&bmp->list);
1583 bpl = (struct ulp_bde64 *) bmp->virt;
James Smarta33c4f72013-03-01 16:36:00 -05001584 bpl_entries = (LPFC_BPL_SIZE/sizeof(struct ulp_bde64));
1585 cmp = lpfc_alloc_bsg_buffers(phba, job->request_payload.payload_len,
1586 1, bpl, &bpl_entries);
1587 if (!cmp) {
1588 rc = -ENOMEM;
1589 goto send_mgmt_rsp_free_bmp;
James Smart3b5dd522010-01-26 23:10:15 -05001590 }
James Smarta33c4f72013-03-01 16:36:00 -05001591 lpfc_bsg_copy_data(cmp, &job->request_payload,
1592 job->request_payload.payload_len, 1);
James Smart3b5dd522010-01-26 23:10:15 -05001593
James Smarta33c4f72013-03-01 16:36:00 -05001594 rc = lpfc_issue_ct_rsp(phba, job, tag, cmp, bmp, bpl_entries);
James Smart3b5dd522010-01-26 23:10:15 -05001595
1596 if (rc == IOCB_SUCCESS)
1597 return 0; /* done for now */
1598
James Smart3b5dd522010-01-26 23:10:15 -05001599 rc = -EACCES;
James Smarta33c4f72013-03-01 16:36:00 -05001600
1601 lpfc_free_bsg_buffers(phba, cmp);
James Smart3b5dd522010-01-26 23:10:15 -05001602
1603send_mgmt_rsp_free_bmp:
James Smarta33c4f72013-03-01 16:36:00 -05001604 if (bmp->virt)
1605 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
James Smart3b5dd522010-01-26 23:10:15 -05001606 kfree(bmp);
1607send_mgmt_rsp_exit:
1608 /* make error code available to userspace */
1609 job->reply->result = rc;
1610 job->dd_data = NULL;
1611 return rc;
1612}
1613
1614/**
James Smart7ad20aa2011-05-24 11:44:28 -04001615 * lpfc_bsg_diag_mode_enter - process preparing into device diag loopback mode
1616 * @phba: Pointer to HBA context object.
James Smart3b5dd522010-01-26 23:10:15 -05001617 *
James Smart7ad20aa2011-05-24 11:44:28 -04001618 * This function is responsible for preparing driver for diag loopback
1619 * on device.
1620 */
1621static int
James Smart88a2cfb2011-07-22 18:36:33 -04001622lpfc_bsg_diag_mode_enter(struct lpfc_hba *phba)
James Smart7ad20aa2011-05-24 11:44:28 -04001623{
1624 struct lpfc_vport **vports;
1625 struct Scsi_Host *shost;
1626 struct lpfc_sli *psli;
1627 struct lpfc_sli_ring *pring;
1628 int i = 0;
1629
1630 psli = &phba->sli;
1631 if (!psli)
1632 return -ENODEV;
1633
1634 pring = &psli->ring[LPFC_FCP_RING];
1635 if (!pring)
1636 return -ENODEV;
1637
1638 if ((phba->link_state == LPFC_HBA_ERROR) ||
1639 (psli->sli_flag & LPFC_BLOCK_MGMT_IO) ||
1640 (!(psli->sli_flag & LPFC_SLI_ACTIVE)))
1641 return -EACCES;
1642
1643 vports = lpfc_create_vport_work_array(phba);
1644 if (vports) {
1645 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
1646 shost = lpfc_shost_from_vport(vports[i]);
1647 scsi_block_requests(shost);
1648 }
1649 lpfc_destroy_vport_work_array(phba, vports);
1650 } else {
1651 shost = lpfc_shost_from_vport(phba->pport);
1652 scsi_block_requests(shost);
1653 }
1654
James Smart0e9bb8d2013-03-01 16:35:12 -05001655 while (!list_empty(&pring->txcmplq)) {
James Smart7ad20aa2011-05-24 11:44:28 -04001656 if (i++ > 500) /* wait up to 5 seconds */
1657 break;
1658 msleep(10);
1659 }
1660 return 0;
1661}
1662
1663/**
1664 * lpfc_bsg_diag_mode_exit - exit process from device diag loopback mode
1665 * @phba: Pointer to HBA context object.
James Smart7ad20aa2011-05-24 11:44:28 -04001666 *
1667 * This function is responsible for driver exit processing of setting up
1668 * diag loopback mode on device.
1669 */
1670static void
1671lpfc_bsg_diag_mode_exit(struct lpfc_hba *phba)
1672{
1673 struct Scsi_Host *shost;
1674 struct lpfc_vport **vports;
1675 int i;
1676
1677 vports = lpfc_create_vport_work_array(phba);
1678 if (vports) {
1679 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
1680 shost = lpfc_shost_from_vport(vports[i]);
1681 scsi_unblock_requests(shost);
1682 }
1683 lpfc_destroy_vport_work_array(phba, vports);
1684 } else {
1685 shost = lpfc_shost_from_vport(phba->pport);
1686 scsi_unblock_requests(shost);
1687 }
1688 return;
1689}
1690
1691/**
1692 * lpfc_sli3_bsg_diag_loopback_mode - process an sli3 bsg vendor command
1693 * @phba: Pointer to HBA context object.
1694 * @job: LPFC_BSG_VENDOR_DIAG_MODE
1695 *
1696 * This function is responsible for placing an sli3 port into diagnostic
1697 * loopback mode in order to perform a diagnostic loopback test.
James Smart3b5dd522010-01-26 23:10:15 -05001698 * All new scsi requests are blocked, a small delay is used to allow the
1699 * scsi requests to complete then the link is brought down. If the link is
1700 * is placed in loopback mode then scsi requests are again allowed
1701 * so the scsi mid-layer doesn't give up on the port.
1702 * All of this is done in-line.
1703 */
1704static int
James Smart7ad20aa2011-05-24 11:44:28 -04001705lpfc_sli3_bsg_diag_loopback_mode(struct lpfc_hba *phba, struct fc_bsg_job *job)
James Smart3b5dd522010-01-26 23:10:15 -05001706{
James Smart3b5dd522010-01-26 23:10:15 -05001707 struct diag_mode_set *loopback_mode;
James Smart3b5dd522010-01-26 23:10:15 -05001708 uint32_t link_flags;
1709 uint32_t timeout;
James Smart1b511972011-12-13 13:23:09 -05001710 LPFC_MBOXQ_t *pmboxq = NULL;
James Smartb76f2dc2011-07-22 18:37:42 -04001711 int mbxstatus = MBX_SUCCESS;
James Smart3b5dd522010-01-26 23:10:15 -05001712 int i = 0;
1713 int rc = 0;
1714
1715 /* no data to return just the return code */
1716 job->reply->reply_payload_rcv_len = 0;
1717
James Smart7ad20aa2011-05-24 11:44:28 -04001718 if (job->request_len < sizeof(struct fc_bsg_request) +
1719 sizeof(struct diag_mode_set)) {
James Smart3b5dd522010-01-26 23:10:15 -05001720 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
James Smart7ad20aa2011-05-24 11:44:28 -04001721 "2738 Received DIAG MODE request size:%d "
1722 "below the minimum size:%d\n",
1723 job->request_len,
1724 (int)(sizeof(struct fc_bsg_request) +
1725 sizeof(struct diag_mode_set)));
James Smart3b5dd522010-01-26 23:10:15 -05001726 rc = -EINVAL;
1727 goto job_error;
1728 }
1729
James Smart88a2cfb2011-07-22 18:36:33 -04001730 rc = lpfc_bsg_diag_mode_enter(phba);
James Smart7ad20aa2011-05-24 11:44:28 -04001731 if (rc)
1732 goto job_error;
1733
1734 /* bring the link to diagnostic mode */
James Smart3b5dd522010-01-26 23:10:15 -05001735 loopback_mode = (struct diag_mode_set *)
1736 job->request->rqst_data.h_vendor.vendor_cmd;
1737 link_flags = loopback_mode->type;
James Smart515e0aa2010-09-29 11:19:00 -04001738 timeout = loopback_mode->timeout * 100;
James Smart3b5dd522010-01-26 23:10:15 -05001739
James Smart3b5dd522010-01-26 23:10:15 -05001740 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1741 if (!pmboxq) {
1742 rc = -ENOMEM;
James Smart7ad20aa2011-05-24 11:44:28 -04001743 goto loopback_mode_exit;
James Smart3b5dd522010-01-26 23:10:15 -05001744 }
James Smart3b5dd522010-01-26 23:10:15 -05001745 memset((void *)pmboxq, 0, sizeof(LPFC_MBOXQ_t));
1746 pmboxq->u.mb.mbxCommand = MBX_DOWN_LINK;
1747 pmboxq->u.mb.mbxOwner = OWN_HOST;
1748
1749 mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
1750
1751 if ((mbxstatus == MBX_SUCCESS) && (pmboxq->u.mb.mbxStatus == 0)) {
1752 /* wait for link down before proceeding */
1753 i = 0;
1754 while (phba->link_state != LPFC_LINK_DOWN) {
1755 if (i++ > timeout) {
1756 rc = -ETIMEDOUT;
1757 goto loopback_mode_exit;
1758 }
James Smart3b5dd522010-01-26 23:10:15 -05001759 msleep(10);
1760 }
1761
1762 memset((void *)pmboxq, 0, sizeof(LPFC_MBOXQ_t));
1763 if (link_flags == INTERNAL_LOOP_BACK)
1764 pmboxq->u.mb.un.varInitLnk.link_flags = FLAGS_LOCAL_LB;
1765 else
1766 pmboxq->u.mb.un.varInitLnk.link_flags =
1767 FLAGS_TOPOLOGY_MODE_LOOP;
1768
1769 pmboxq->u.mb.mbxCommand = MBX_INIT_LINK;
1770 pmboxq->u.mb.mbxOwner = OWN_HOST;
1771
1772 mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq,
1773 LPFC_MBOX_TMO);
1774
1775 if ((mbxstatus != MBX_SUCCESS) || (pmboxq->u.mb.mbxStatus))
1776 rc = -ENODEV;
1777 else {
James Smart1b511972011-12-13 13:23:09 -05001778 spin_lock_irq(&phba->hbalock);
James Smart3b5dd522010-01-26 23:10:15 -05001779 phba->link_flag |= LS_LOOPBACK_MODE;
James Smart1b511972011-12-13 13:23:09 -05001780 spin_unlock_irq(&phba->hbalock);
James Smart3b5dd522010-01-26 23:10:15 -05001781 /* wait for the link attention interrupt */
1782 msleep(100);
1783
1784 i = 0;
1785 while (phba->link_state != LPFC_HBA_READY) {
1786 if (i++ > timeout) {
1787 rc = -ETIMEDOUT;
1788 break;
1789 }
1790
1791 msleep(10);
1792 }
1793 }
1794
1795 } else
1796 rc = -ENODEV;
1797
1798loopback_mode_exit:
James Smart7ad20aa2011-05-24 11:44:28 -04001799 lpfc_bsg_diag_mode_exit(phba);
James Smart3b5dd522010-01-26 23:10:15 -05001800
1801 /*
1802 * Let SLI layer release mboxq if mbox command completed after timeout.
1803 */
James Smart1b511972011-12-13 13:23:09 -05001804 if (pmboxq && mbxstatus != MBX_TIMEOUT)
James Smart3b5dd522010-01-26 23:10:15 -05001805 mempool_free(pmboxq, phba->mbox_mem_pool);
1806
1807job_error:
1808 /* make error code available to userspace */
1809 job->reply->result = rc;
1810 /* complete the job back to userspace if no error */
1811 if (rc == 0)
1812 job->job_done(job);
1813 return rc;
1814}
1815
1816/**
James Smart7ad20aa2011-05-24 11:44:28 -04001817 * lpfc_sli4_bsg_set_link_diag_state - set sli4 link diag state
1818 * @phba: Pointer to HBA context object.
1819 * @diag: Flag for set link to diag or nomral operation state.
1820 *
1821 * This function is responsible for issuing a sli4 mailbox command for setting
1822 * link to either diag state or normal operation state.
1823 */
1824static int
1825lpfc_sli4_bsg_set_link_diag_state(struct lpfc_hba *phba, uint32_t diag)
1826{
1827 LPFC_MBOXQ_t *pmboxq;
1828 struct lpfc_mbx_set_link_diag_state *link_diag_state;
1829 uint32_t req_len, alloc_len;
1830 int mbxstatus = MBX_SUCCESS, rc;
1831
1832 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1833 if (!pmboxq)
1834 return -ENOMEM;
1835
1836 req_len = (sizeof(struct lpfc_mbx_set_link_diag_state) -
1837 sizeof(struct lpfc_sli4_cfg_mhdr));
1838 alloc_len = lpfc_sli4_config(phba, pmboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
1839 LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_STATE,
1840 req_len, LPFC_SLI4_MBX_EMBED);
1841 if (alloc_len != req_len) {
1842 rc = -ENOMEM;
1843 goto link_diag_state_set_out;
1844 }
James Smart1b511972011-12-13 13:23:09 -05001845 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
1846 "3128 Set link to diagnostic state:x%x (x%x/x%x)\n",
1847 diag, phba->sli4_hba.lnk_info.lnk_tp,
1848 phba->sli4_hba.lnk_info.lnk_no);
1849
James Smart7ad20aa2011-05-24 11:44:28 -04001850 link_diag_state = &pmboxq->u.mqe.un.link_diag_state;
James Smart97315922012-08-03 12:32:52 -04001851 bf_set(lpfc_mbx_set_diag_state_diag_bit_valid, &link_diag_state->u.req,
1852 LPFC_DIAG_STATE_DIAG_BIT_VALID_CHANGE);
James Smart7ad20aa2011-05-24 11:44:28 -04001853 bf_set(lpfc_mbx_set_diag_state_link_num, &link_diag_state->u.req,
James Smart1b511972011-12-13 13:23:09 -05001854 phba->sli4_hba.lnk_info.lnk_no);
James Smart7ad20aa2011-05-24 11:44:28 -04001855 bf_set(lpfc_mbx_set_diag_state_link_type, &link_diag_state->u.req,
James Smart1b511972011-12-13 13:23:09 -05001856 phba->sli4_hba.lnk_info.lnk_tp);
James Smart7ad20aa2011-05-24 11:44:28 -04001857 if (diag)
1858 bf_set(lpfc_mbx_set_diag_state_diag,
1859 &link_diag_state->u.req, 1);
1860 else
1861 bf_set(lpfc_mbx_set_diag_state_diag,
1862 &link_diag_state->u.req, 0);
1863
1864 mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
1865
1866 if ((mbxstatus == MBX_SUCCESS) && (pmboxq->u.mb.mbxStatus == 0))
1867 rc = 0;
1868 else
1869 rc = -ENODEV;
1870
1871link_diag_state_set_out:
1872 if (pmboxq && (mbxstatus != MBX_TIMEOUT))
1873 mempool_free(pmboxq, phba->mbox_mem_pool);
1874
1875 return rc;
1876}
1877
1878/**
James Smart1b511972011-12-13 13:23:09 -05001879 * lpfc_sli4_bsg_set_internal_loopback - set sli4 internal loopback diagnostic
1880 * @phba: Pointer to HBA context object.
1881 *
1882 * This function is responsible for issuing a sli4 mailbox command for setting
1883 * up internal loopback diagnostic.
1884 */
1885static int
1886lpfc_sli4_bsg_set_internal_loopback(struct lpfc_hba *phba)
1887{
1888 LPFC_MBOXQ_t *pmboxq;
1889 uint32_t req_len, alloc_len;
1890 struct lpfc_mbx_set_link_diag_loopback *link_diag_loopback;
1891 int mbxstatus = MBX_SUCCESS, rc = 0;
1892
1893 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1894 if (!pmboxq)
1895 return -ENOMEM;
1896 req_len = (sizeof(struct lpfc_mbx_set_link_diag_loopback) -
1897 sizeof(struct lpfc_sli4_cfg_mhdr));
1898 alloc_len = lpfc_sli4_config(phba, pmboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
1899 LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_LOOPBACK,
1900 req_len, LPFC_SLI4_MBX_EMBED);
1901 if (alloc_len != req_len) {
1902 mempool_free(pmboxq, phba->mbox_mem_pool);
1903 return -ENOMEM;
1904 }
1905 link_diag_loopback = &pmboxq->u.mqe.un.link_diag_loopback;
1906 bf_set(lpfc_mbx_set_diag_state_link_num,
1907 &link_diag_loopback->u.req, phba->sli4_hba.lnk_info.lnk_no);
1908 bf_set(lpfc_mbx_set_diag_state_link_type,
1909 &link_diag_loopback->u.req, phba->sli4_hba.lnk_info.lnk_tp);
1910 bf_set(lpfc_mbx_set_diag_lpbk_type, &link_diag_loopback->u.req,
James Smart3ef6d242012-01-18 16:23:48 -05001911 LPFC_DIAG_LOOPBACK_TYPE_INTERNAL);
James Smart1b511972011-12-13 13:23:09 -05001912
1913 mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
1914 if ((mbxstatus != MBX_SUCCESS) || (pmboxq->u.mb.mbxStatus)) {
1915 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1916 "3127 Failed setup loopback mode mailbox "
1917 "command, rc:x%x, status:x%x\n", mbxstatus,
1918 pmboxq->u.mb.mbxStatus);
1919 rc = -ENODEV;
1920 }
1921 if (pmboxq && (mbxstatus != MBX_TIMEOUT))
1922 mempool_free(pmboxq, phba->mbox_mem_pool);
1923 return rc;
1924}
1925
1926/**
1927 * lpfc_sli4_diag_fcport_reg_setup - setup port registrations for diagnostic
1928 * @phba: Pointer to HBA context object.
1929 *
1930 * This function set up SLI4 FC port registrations for diagnostic run, which
1931 * includes all the rpis, vfi, and also vpi.
1932 */
1933static int
1934lpfc_sli4_diag_fcport_reg_setup(struct lpfc_hba *phba)
1935{
1936 int rc;
1937
1938 if (phba->pport->fc_flag & FC_VFI_REGISTERED) {
1939 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1940 "3136 Port still had vfi registered: "
1941 "mydid:x%x, fcfi:%d, vfi:%d, vpi:%d\n",
1942 phba->pport->fc_myDID, phba->fcf.fcfi,
1943 phba->sli4_hba.vfi_ids[phba->pport->vfi],
1944 phba->vpi_ids[phba->pport->vpi]);
1945 return -EINVAL;
1946 }
1947 rc = lpfc_issue_reg_vfi(phba->pport);
1948 return rc;
1949}
1950
1951/**
James Smart7ad20aa2011-05-24 11:44:28 -04001952 * lpfc_sli4_bsg_diag_loopback_mode - process an sli4 bsg vendor command
1953 * @phba: Pointer to HBA context object.
1954 * @job: LPFC_BSG_VENDOR_DIAG_MODE
1955 *
1956 * This function is responsible for placing an sli4 port into diagnostic
1957 * loopback mode in order to perform a diagnostic loopback test.
1958 */
1959static int
1960lpfc_sli4_bsg_diag_loopback_mode(struct lpfc_hba *phba, struct fc_bsg_job *job)
1961{
1962 struct diag_mode_set *loopback_mode;
James Smart1b511972011-12-13 13:23:09 -05001963 uint32_t link_flags, timeout;
1964 int i, rc = 0;
James Smart7ad20aa2011-05-24 11:44:28 -04001965
1966 /* no data to return just the return code */
1967 job->reply->reply_payload_rcv_len = 0;
1968
1969 if (job->request_len < sizeof(struct fc_bsg_request) +
1970 sizeof(struct diag_mode_set)) {
1971 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1972 "3011 Received DIAG MODE request size:%d "
1973 "below the minimum size:%d\n",
1974 job->request_len,
1975 (int)(sizeof(struct fc_bsg_request) +
1976 sizeof(struct diag_mode_set)));
1977 rc = -EINVAL;
1978 goto job_error;
1979 }
1980
James Smart88a2cfb2011-07-22 18:36:33 -04001981 rc = lpfc_bsg_diag_mode_enter(phba);
James Smart7ad20aa2011-05-24 11:44:28 -04001982 if (rc)
1983 goto job_error;
1984
James Smart1b511972011-12-13 13:23:09 -05001985 /* indicate we are in loobpack diagnostic mode */
1986 spin_lock_irq(&phba->hbalock);
1987 phba->link_flag |= LS_LOOPBACK_MODE;
1988 spin_unlock_irq(&phba->hbalock);
1989
1990 /* reset port to start frome scratch */
1991 rc = lpfc_selective_reset(phba);
1992 if (rc)
1993 goto job_error;
1994
James Smart7ad20aa2011-05-24 11:44:28 -04001995 /* bring the link to diagnostic mode */
James Smart1b511972011-12-13 13:23:09 -05001996 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
1997 "3129 Bring link to diagnostic state.\n");
James Smart7ad20aa2011-05-24 11:44:28 -04001998 loopback_mode = (struct diag_mode_set *)
1999 job->request->rqst_data.h_vendor.vendor_cmd;
2000 link_flags = loopback_mode->type;
2001 timeout = loopback_mode->timeout * 100;
2002
2003 rc = lpfc_sli4_bsg_set_link_diag_state(phba, 1);
James Smart1b511972011-12-13 13:23:09 -05002004 if (rc) {
2005 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2006 "3130 Failed to bring link to diagnostic "
2007 "state, rc:x%x\n", rc);
James Smart7ad20aa2011-05-24 11:44:28 -04002008 goto loopback_mode_exit;
James Smart1b511972011-12-13 13:23:09 -05002009 }
James Smart7ad20aa2011-05-24 11:44:28 -04002010
2011 /* wait for link down before proceeding */
2012 i = 0;
2013 while (phba->link_state != LPFC_LINK_DOWN) {
2014 if (i++ > timeout) {
2015 rc = -ETIMEDOUT;
James Smart1b511972011-12-13 13:23:09 -05002016 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
2017 "3131 Timeout waiting for link to "
2018 "diagnostic mode, timeout:%d ms\n",
2019 timeout * 10);
James Smart7ad20aa2011-05-24 11:44:28 -04002020 goto loopback_mode_exit;
2021 }
2022 msleep(10);
2023 }
James Smart7ad20aa2011-05-24 11:44:28 -04002024
James Smart1b511972011-12-13 13:23:09 -05002025 /* set up loopback mode */
2026 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
2027 "3132 Set up loopback mode:x%x\n", link_flags);
2028
2029 if (link_flags == INTERNAL_LOOP_BACK)
2030 rc = lpfc_sli4_bsg_set_internal_loopback(phba);
2031 else if (link_flags == EXTERNAL_LOOP_BACK)
2032 rc = lpfc_hba_init_link_fc_topology(phba,
2033 FLAGS_TOPOLOGY_MODE_PT_PT,
2034 MBX_NOWAIT);
James Smart7ad20aa2011-05-24 11:44:28 -04002035 else {
James Smart1b511972011-12-13 13:23:09 -05002036 rc = -EINVAL;
2037 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
2038 "3141 Loopback mode:x%x not supported\n",
2039 link_flags);
2040 goto loopback_mode_exit;
2041 }
2042
2043 if (!rc) {
James Smart7ad20aa2011-05-24 11:44:28 -04002044 /* wait for the link attention interrupt */
2045 msleep(100);
2046 i = 0;
James Smart1b511972011-12-13 13:23:09 -05002047 while (phba->link_state < LPFC_LINK_UP) {
2048 if (i++ > timeout) {
2049 rc = -ETIMEDOUT;
2050 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
2051 "3137 Timeout waiting for link up "
2052 "in loopback mode, timeout:%d ms\n",
2053 timeout * 10);
2054 break;
2055 }
2056 msleep(10);
2057 }
2058 }
2059
2060 /* port resource registration setup for loopback diagnostic */
2061 if (!rc) {
2062 /* set up a none zero myDID for loopback test */
2063 phba->pport->fc_myDID = 1;
2064 rc = lpfc_sli4_diag_fcport_reg_setup(phba);
2065 } else
2066 goto loopback_mode_exit;
2067
2068 if (!rc) {
2069 /* wait for the port ready */
2070 msleep(100);
2071 i = 0;
James Smart7ad20aa2011-05-24 11:44:28 -04002072 while (phba->link_state != LPFC_HBA_READY) {
2073 if (i++ > timeout) {
2074 rc = -ETIMEDOUT;
James Smart1b511972011-12-13 13:23:09 -05002075 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
2076 "3133 Timeout waiting for port "
2077 "loopback mode ready, timeout:%d ms\n",
2078 timeout * 10);
James Smart7ad20aa2011-05-24 11:44:28 -04002079 break;
2080 }
2081 msleep(10);
2082 }
2083 }
2084
2085loopback_mode_exit:
James Smart1b511972011-12-13 13:23:09 -05002086 /* clear loopback diagnostic mode */
2087 if (rc) {
2088 spin_lock_irq(&phba->hbalock);
2089 phba->link_flag &= ~LS_LOOPBACK_MODE;
2090 spin_unlock_irq(&phba->hbalock);
2091 }
James Smart7ad20aa2011-05-24 11:44:28 -04002092 lpfc_bsg_diag_mode_exit(phba);
2093
James Smart7ad20aa2011-05-24 11:44:28 -04002094job_error:
2095 /* make error code available to userspace */
2096 job->reply->result = rc;
2097 /* complete the job back to userspace if no error */
2098 if (rc == 0)
2099 job->job_done(job);
2100 return rc;
2101}
2102
2103/**
2104 * lpfc_bsg_diag_loopback_mode - bsg vendor command for diag loopback mode
2105 * @job: LPFC_BSG_VENDOR_DIAG_MODE
2106 *
2107 * This function is responsible for responding to check and dispatch bsg diag
2108 * command from the user to proper driver action routines.
2109 */
2110static int
2111lpfc_bsg_diag_loopback_mode(struct fc_bsg_job *job)
2112{
2113 struct Scsi_Host *shost;
2114 struct lpfc_vport *vport;
2115 struct lpfc_hba *phba;
2116 int rc;
2117
2118 shost = job->shost;
2119 if (!shost)
2120 return -ENODEV;
2121 vport = (struct lpfc_vport *)job->shost->hostdata;
2122 if (!vport)
2123 return -ENODEV;
2124 phba = vport->phba;
2125 if (!phba)
2126 return -ENODEV;
2127
2128 if (phba->sli_rev < LPFC_SLI_REV4)
2129 rc = lpfc_sli3_bsg_diag_loopback_mode(phba, job);
2130 else if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
2131 LPFC_SLI_INTF_IF_TYPE_2)
2132 rc = lpfc_sli4_bsg_diag_loopback_mode(phba, job);
2133 else
2134 rc = -ENODEV;
2135
2136 return rc;
James Smart7ad20aa2011-05-24 11:44:28 -04002137}
2138
2139/**
2140 * lpfc_sli4_bsg_diag_mode_end - sli4 bsg vendor command for ending diag mode
2141 * @job: LPFC_BSG_VENDOR_DIAG_MODE_END
2142 *
2143 * This function is responsible for responding to check and dispatch bsg diag
2144 * command from the user to proper driver action routines.
2145 */
2146static int
2147lpfc_sli4_bsg_diag_mode_end(struct fc_bsg_job *job)
2148{
2149 struct Scsi_Host *shost;
2150 struct lpfc_vport *vport;
2151 struct lpfc_hba *phba;
James Smart1b511972011-12-13 13:23:09 -05002152 struct diag_mode_set *loopback_mode_end_cmd;
2153 uint32_t timeout;
2154 int rc, i;
James Smart7ad20aa2011-05-24 11:44:28 -04002155
2156 shost = job->shost;
2157 if (!shost)
2158 return -ENODEV;
2159 vport = (struct lpfc_vport *)job->shost->hostdata;
2160 if (!vport)
2161 return -ENODEV;
2162 phba = vport->phba;
2163 if (!phba)
2164 return -ENODEV;
2165
2166 if (phba->sli_rev < LPFC_SLI_REV4)
2167 return -ENODEV;
2168 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
2169 LPFC_SLI_INTF_IF_TYPE_2)
2170 return -ENODEV;
2171
James Smart1b511972011-12-13 13:23:09 -05002172 /* clear loopback diagnostic mode */
2173 spin_lock_irq(&phba->hbalock);
2174 phba->link_flag &= ~LS_LOOPBACK_MODE;
2175 spin_unlock_irq(&phba->hbalock);
2176 loopback_mode_end_cmd = (struct diag_mode_set *)
2177 job->request->rqst_data.h_vendor.vendor_cmd;
2178 timeout = loopback_mode_end_cmd->timeout * 100;
2179
James Smart7ad20aa2011-05-24 11:44:28 -04002180 rc = lpfc_sli4_bsg_set_link_diag_state(phba, 0);
James Smart1b511972011-12-13 13:23:09 -05002181 if (rc) {
2182 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2183 "3139 Failed to bring link to diagnostic "
2184 "state, rc:x%x\n", rc);
2185 goto loopback_mode_end_exit;
2186 }
James Smart7ad20aa2011-05-24 11:44:28 -04002187
James Smart1b511972011-12-13 13:23:09 -05002188 /* wait for link down before proceeding */
2189 i = 0;
2190 while (phba->link_state != LPFC_LINK_DOWN) {
2191 if (i++ > timeout) {
2192 rc = -ETIMEDOUT;
2193 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
2194 "3140 Timeout waiting for link to "
2195 "diagnostic mode_end, timeout:%d ms\n",
2196 timeout * 10);
2197 /* there is nothing much we can do here */
2198 break;
2199 }
2200 msleep(10);
2201 }
James Smart7ad20aa2011-05-24 11:44:28 -04002202
James Smart1b511972011-12-13 13:23:09 -05002203 /* reset port resource registrations */
2204 rc = lpfc_selective_reset(phba);
2205 phba->pport->fc_myDID = 0;
2206
2207loopback_mode_end_exit:
2208 /* make return code available to userspace */
2209 job->reply->result = rc;
2210 /* complete the job back to userspace if no error */
2211 if (rc == 0)
2212 job->job_done(job);
James Smart7ad20aa2011-05-24 11:44:28 -04002213 return rc;
2214}
2215
2216/**
2217 * lpfc_sli4_bsg_link_diag_test - sli4 bsg vendor command for diag link test
2218 * @job: LPFC_BSG_VENDOR_DIAG_LINK_TEST
2219 *
2220 * This function is to perform SLI4 diag link test request from the user
2221 * applicaiton.
2222 */
2223static int
2224lpfc_sli4_bsg_link_diag_test(struct fc_bsg_job *job)
2225{
2226 struct Scsi_Host *shost;
2227 struct lpfc_vport *vport;
2228 struct lpfc_hba *phba;
2229 LPFC_MBOXQ_t *pmboxq;
2230 struct sli4_link_diag *link_diag_test_cmd;
2231 uint32_t req_len, alloc_len;
2232 uint32_t timeout;
2233 struct lpfc_mbx_run_link_diag_test *run_link_diag_test;
2234 union lpfc_sli4_cfg_shdr *shdr;
2235 uint32_t shdr_status, shdr_add_status;
2236 struct diag_status *diag_status_reply;
2237 int mbxstatus, rc = 0;
2238
2239 shost = job->shost;
2240 if (!shost) {
2241 rc = -ENODEV;
2242 goto job_error;
2243 }
2244 vport = (struct lpfc_vport *)job->shost->hostdata;
2245 if (!vport) {
2246 rc = -ENODEV;
2247 goto job_error;
2248 }
2249 phba = vport->phba;
2250 if (!phba) {
2251 rc = -ENODEV;
2252 goto job_error;
2253 }
2254
2255 if (phba->sli_rev < LPFC_SLI_REV4) {
2256 rc = -ENODEV;
2257 goto job_error;
2258 }
2259 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
2260 LPFC_SLI_INTF_IF_TYPE_2) {
2261 rc = -ENODEV;
2262 goto job_error;
2263 }
2264
2265 if (job->request_len < sizeof(struct fc_bsg_request) +
2266 sizeof(struct sli4_link_diag)) {
2267 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2268 "3013 Received LINK DIAG TEST request "
2269 " size:%d below the minimum size:%d\n",
2270 job->request_len,
2271 (int)(sizeof(struct fc_bsg_request) +
2272 sizeof(struct sli4_link_diag)));
2273 rc = -EINVAL;
2274 goto job_error;
2275 }
2276
James Smart88a2cfb2011-07-22 18:36:33 -04002277 rc = lpfc_bsg_diag_mode_enter(phba);
James Smart7ad20aa2011-05-24 11:44:28 -04002278 if (rc)
2279 goto job_error;
2280
2281 link_diag_test_cmd = (struct sli4_link_diag *)
2282 job->request->rqst_data.h_vendor.vendor_cmd;
2283 timeout = link_diag_test_cmd->timeout * 100;
2284
2285 rc = lpfc_sli4_bsg_set_link_diag_state(phba, 1);
2286
2287 if (rc)
2288 goto job_error;
2289
2290 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2291 if (!pmboxq) {
2292 rc = -ENOMEM;
2293 goto link_diag_test_exit;
2294 }
2295
2296 req_len = (sizeof(struct lpfc_mbx_set_link_diag_state) -
2297 sizeof(struct lpfc_sli4_cfg_mhdr));
2298 alloc_len = lpfc_sli4_config(phba, pmboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
2299 LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_STATE,
2300 req_len, LPFC_SLI4_MBX_EMBED);
2301 if (alloc_len != req_len) {
2302 rc = -ENOMEM;
2303 goto link_diag_test_exit;
2304 }
2305 run_link_diag_test = &pmboxq->u.mqe.un.link_diag_test;
2306 bf_set(lpfc_mbx_run_diag_test_link_num, &run_link_diag_test->u.req,
James Smart1b511972011-12-13 13:23:09 -05002307 phba->sli4_hba.lnk_info.lnk_no);
James Smart7ad20aa2011-05-24 11:44:28 -04002308 bf_set(lpfc_mbx_run_diag_test_link_type, &run_link_diag_test->u.req,
James Smart1b511972011-12-13 13:23:09 -05002309 phba->sli4_hba.lnk_info.lnk_tp);
James Smart7ad20aa2011-05-24 11:44:28 -04002310 bf_set(lpfc_mbx_run_diag_test_test_id, &run_link_diag_test->u.req,
2311 link_diag_test_cmd->test_id);
2312 bf_set(lpfc_mbx_run_diag_test_loops, &run_link_diag_test->u.req,
2313 link_diag_test_cmd->loops);
2314 bf_set(lpfc_mbx_run_diag_test_test_ver, &run_link_diag_test->u.req,
2315 link_diag_test_cmd->test_version);
2316 bf_set(lpfc_mbx_run_diag_test_err_act, &run_link_diag_test->u.req,
2317 link_diag_test_cmd->error_action);
2318
2319 mbxstatus = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
2320
2321 shdr = (union lpfc_sli4_cfg_shdr *)
2322 &pmboxq->u.mqe.un.sli4_config.header.cfg_shdr;
2323 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
2324 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
2325 if (shdr_status || shdr_add_status || mbxstatus) {
2326 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
2327 "3010 Run link diag test mailbox failed with "
2328 "mbx_status x%x status x%x, add_status x%x\n",
2329 mbxstatus, shdr_status, shdr_add_status);
2330 }
2331
2332 diag_status_reply = (struct diag_status *)
2333 job->reply->reply_data.vendor_reply.vendor_rsp;
2334
2335 if (job->reply_len <
2336 sizeof(struct fc_bsg_request) + sizeof(struct diag_status)) {
2337 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2338 "3012 Received Run link diag test reply "
2339 "below minimum size (%d): reply_len:%d\n",
2340 (int)(sizeof(struct fc_bsg_request) +
2341 sizeof(struct diag_status)),
2342 job->reply_len);
2343 rc = -EINVAL;
2344 goto job_error;
2345 }
2346
2347 diag_status_reply->mbox_status = mbxstatus;
2348 diag_status_reply->shdr_status = shdr_status;
2349 diag_status_reply->shdr_add_status = shdr_add_status;
2350
2351link_diag_test_exit:
2352 rc = lpfc_sli4_bsg_set_link_diag_state(phba, 0);
2353
2354 if (pmboxq)
2355 mempool_free(pmboxq, phba->mbox_mem_pool);
2356
2357 lpfc_bsg_diag_mode_exit(phba);
2358
2359job_error:
2360 /* make error code available to userspace */
2361 job->reply->result = rc;
2362 /* complete the job back to userspace if no error */
2363 if (rc == 0)
2364 job->job_done(job);
2365 return rc;
2366}
2367
2368/**
James Smart3b5dd522010-01-26 23:10:15 -05002369 * lpfcdiag_loop_self_reg - obtains a remote port login id
2370 * @phba: Pointer to HBA context object
2371 * @rpi: Pointer to a remote port login id
2372 *
2373 * This function obtains a remote port login id so the diag loopback test
2374 * can send and receive its own unsolicited CT command.
2375 **/
James Smart40426292010-12-15 17:58:10 -05002376static int lpfcdiag_loop_self_reg(struct lpfc_hba *phba, uint16_t *rpi)
James Smart3b5dd522010-01-26 23:10:15 -05002377{
2378 LPFC_MBOXQ_t *mbox;
2379 struct lpfc_dmabuf *dmabuff;
2380 int status;
2381
2382 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2383 if (!mbox)
James Smartd439d282010-09-29 11:18:45 -04002384 return -ENOMEM;
James Smart3b5dd522010-01-26 23:10:15 -05002385
James Smart1b511972011-12-13 13:23:09 -05002386 if (phba->sli_rev < LPFC_SLI_REV4)
2387 status = lpfc_reg_rpi(phba, 0, phba->pport->fc_myDID,
2388 (uint8_t *)&phba->pport->fc_sparam,
2389 mbox, *rpi);
2390 else {
James Smart40426292010-12-15 17:58:10 -05002391 *rpi = lpfc_sli4_alloc_rpi(phba);
James Smart1b511972011-12-13 13:23:09 -05002392 status = lpfc_reg_rpi(phba, phba->pport->vpi,
2393 phba->pport->fc_myDID,
2394 (uint8_t *)&phba->pport->fc_sparam,
2395 mbox, *rpi);
2396 }
2397
James Smart3b5dd522010-01-26 23:10:15 -05002398 if (status) {
2399 mempool_free(mbox, phba->mbox_mem_pool);
James Smart40426292010-12-15 17:58:10 -05002400 if (phba->sli_rev == LPFC_SLI_REV4)
2401 lpfc_sli4_free_rpi(phba, *rpi);
James Smartd439d282010-09-29 11:18:45 -04002402 return -ENOMEM;
James Smart3b5dd522010-01-26 23:10:15 -05002403 }
2404
2405 dmabuff = (struct lpfc_dmabuf *) mbox->context1;
2406 mbox->context1 = NULL;
James Smartd439d282010-09-29 11:18:45 -04002407 mbox->context2 = NULL;
James Smart3b5dd522010-01-26 23:10:15 -05002408 status = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
2409
2410 if ((status != MBX_SUCCESS) || (mbox->u.mb.mbxStatus)) {
2411 lpfc_mbuf_free(phba, dmabuff->virt, dmabuff->phys);
2412 kfree(dmabuff);
2413 if (status != MBX_TIMEOUT)
2414 mempool_free(mbox, phba->mbox_mem_pool);
James Smart40426292010-12-15 17:58:10 -05002415 if (phba->sli_rev == LPFC_SLI_REV4)
2416 lpfc_sli4_free_rpi(phba, *rpi);
James Smartd439d282010-09-29 11:18:45 -04002417 return -ENODEV;
James Smart3b5dd522010-01-26 23:10:15 -05002418 }
2419
James Smart1b511972011-12-13 13:23:09 -05002420 if (phba->sli_rev < LPFC_SLI_REV4)
2421 *rpi = mbox->u.mb.un.varWords[0];
James Smart3b5dd522010-01-26 23:10:15 -05002422
2423 lpfc_mbuf_free(phba, dmabuff->virt, dmabuff->phys);
2424 kfree(dmabuff);
2425 mempool_free(mbox, phba->mbox_mem_pool);
2426 return 0;
2427}
2428
2429/**
2430 * lpfcdiag_loop_self_unreg - unregs from the rpi
2431 * @phba: Pointer to HBA context object
2432 * @rpi: Remote port login id
2433 *
2434 * This function unregisters the rpi obtained in lpfcdiag_loop_self_reg
2435 **/
2436static int lpfcdiag_loop_self_unreg(struct lpfc_hba *phba, uint16_t rpi)
2437{
2438 LPFC_MBOXQ_t *mbox;
2439 int status;
2440
2441 /* Allocate mboxq structure */
2442 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2443 if (mbox == NULL)
James Smartd439d282010-09-29 11:18:45 -04002444 return -ENOMEM;
James Smart3b5dd522010-01-26 23:10:15 -05002445
James Smart1b511972011-12-13 13:23:09 -05002446 if (phba->sli_rev < LPFC_SLI_REV4)
2447 lpfc_unreg_login(phba, 0, rpi, mbox);
2448 else
2449 lpfc_unreg_login(phba, phba->pport->vpi,
2450 phba->sli4_hba.rpi_ids[rpi], mbox);
2451
James Smart3b5dd522010-01-26 23:10:15 -05002452 status = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
2453
2454 if ((status != MBX_SUCCESS) || (mbox->u.mb.mbxStatus)) {
2455 if (status != MBX_TIMEOUT)
2456 mempool_free(mbox, phba->mbox_mem_pool);
James Smartd439d282010-09-29 11:18:45 -04002457 return -EIO;
James Smart3b5dd522010-01-26 23:10:15 -05002458 }
James Smart3b5dd522010-01-26 23:10:15 -05002459 mempool_free(mbox, phba->mbox_mem_pool);
James Smart40426292010-12-15 17:58:10 -05002460 if (phba->sli_rev == LPFC_SLI_REV4)
2461 lpfc_sli4_free_rpi(phba, rpi);
James Smart3b5dd522010-01-26 23:10:15 -05002462 return 0;
2463}
2464
2465/**
2466 * lpfcdiag_loop_get_xri - obtains the transmit and receive ids
2467 * @phba: Pointer to HBA context object
2468 * @rpi: Remote port login id
2469 * @txxri: Pointer to transmit exchange id
2470 * @rxxri: Pointer to response exchabge id
2471 *
2472 * This function obtains the transmit and receive ids required to send
2473 * an unsolicited ct command with a payload. A special lpfc FsType and CmdRsp
2474 * flags are used to the unsolicted response handler is able to process
2475 * the ct command sent on the same port.
2476 **/
2477static int lpfcdiag_loop_get_xri(struct lpfc_hba *phba, uint16_t rpi,
2478 uint16_t *txxri, uint16_t * rxxri)
2479{
2480 struct lpfc_bsg_event *evt;
2481 struct lpfc_iocbq *cmdiocbq, *rspiocbq;
2482 IOCB_t *cmd, *rsp;
2483 struct lpfc_dmabuf *dmabuf;
2484 struct ulp_bde64 *bpl = NULL;
2485 struct lpfc_sli_ct_request *ctreq = NULL;
2486 int ret_val = 0;
James Smartd439d282010-09-29 11:18:45 -04002487 int time_left;
James Smart515e0aa2010-09-29 11:19:00 -04002488 int iocb_stat = 0;
James Smart3b5dd522010-01-26 23:10:15 -05002489 unsigned long flags;
2490
2491 *txxri = 0;
2492 *rxxri = 0;
2493 evt = lpfc_bsg_event_new(FC_REG_CT_EVENT, current->pid,
2494 SLI_CT_ELX_LOOPBACK);
2495 if (!evt)
James Smartd439d282010-09-29 11:18:45 -04002496 return -ENOMEM;
James Smart3b5dd522010-01-26 23:10:15 -05002497
2498 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2499 list_add(&evt->node, &phba->ct_ev_waiters);
2500 lpfc_bsg_event_ref(evt);
2501 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2502
2503 cmdiocbq = lpfc_sli_get_iocbq(phba);
2504 rspiocbq = lpfc_sli_get_iocbq(phba);
2505
2506 dmabuf = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2507 if (dmabuf) {
2508 dmabuf->virt = lpfc_mbuf_alloc(phba, 0, &dmabuf->phys);
James Smartc7495932010-04-06 15:05:28 -04002509 if (dmabuf->virt) {
2510 INIT_LIST_HEAD(&dmabuf->list);
2511 bpl = (struct ulp_bde64 *) dmabuf->virt;
2512 memset(bpl, 0, sizeof(*bpl));
2513 ctreq = (struct lpfc_sli_ct_request *)(bpl + 1);
2514 bpl->addrHigh =
2515 le32_to_cpu(putPaddrHigh(dmabuf->phys +
2516 sizeof(*bpl)));
2517 bpl->addrLow =
2518 le32_to_cpu(putPaddrLow(dmabuf->phys +
2519 sizeof(*bpl)));
2520 bpl->tus.f.bdeFlags = 0;
2521 bpl->tus.f.bdeSize = ELX_LOOPBACK_HEADER_SZ;
2522 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2523 }
James Smart3b5dd522010-01-26 23:10:15 -05002524 }
2525
2526 if (cmdiocbq == NULL || rspiocbq == NULL ||
James Smartc7495932010-04-06 15:05:28 -04002527 dmabuf == NULL || bpl == NULL || ctreq == NULL ||
2528 dmabuf->virt == NULL) {
James Smartd439d282010-09-29 11:18:45 -04002529 ret_val = -ENOMEM;
James Smart3b5dd522010-01-26 23:10:15 -05002530 goto err_get_xri_exit;
2531 }
2532
2533 cmd = &cmdiocbq->iocb;
2534 rsp = &rspiocbq->iocb;
2535
2536 memset(ctreq, 0, ELX_LOOPBACK_HEADER_SZ);
2537
2538 ctreq->RevisionId.bits.Revision = SLI_CT_REVISION;
2539 ctreq->RevisionId.bits.InId = 0;
2540 ctreq->FsType = SLI_CT_ELX_LOOPBACK;
2541 ctreq->FsSubType = 0;
2542 ctreq->CommandResponse.bits.CmdRsp = ELX_LOOPBACK_XRI_SETUP;
2543 ctreq->CommandResponse.bits.Size = 0;
2544
2545
2546 cmd->un.xseq64.bdl.addrHigh = putPaddrHigh(dmabuf->phys);
2547 cmd->un.xseq64.bdl.addrLow = putPaddrLow(dmabuf->phys);
2548 cmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
2549 cmd->un.xseq64.bdl.bdeSize = sizeof(*bpl);
2550
2551 cmd->un.xseq64.w5.hcsw.Fctl = LA;
2552 cmd->un.xseq64.w5.hcsw.Dfctl = 0;
2553 cmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
2554 cmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
2555
2556 cmd->ulpCommand = CMD_XMIT_SEQUENCE64_CR;
2557 cmd->ulpBdeCount = 1;
2558 cmd->ulpLe = 1;
2559 cmd->ulpClass = CLASS3;
2560 cmd->ulpContext = rpi;
2561
2562 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
2563 cmdiocbq->vport = phba->pport;
2564
James Smartd439d282010-09-29 11:18:45 -04002565 iocb_stat = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq,
James Smart3b5dd522010-01-26 23:10:15 -05002566 rspiocbq,
2567 (phba->fc_ratov * 2)
2568 + LPFC_DRVR_TIMEOUT);
James Smartd439d282010-09-29 11:18:45 -04002569 if (iocb_stat) {
2570 ret_val = -EIO;
James Smart3b5dd522010-01-26 23:10:15 -05002571 goto err_get_xri_exit;
James Smartd439d282010-09-29 11:18:45 -04002572 }
James Smart3b5dd522010-01-26 23:10:15 -05002573 *txxri = rsp->ulpContext;
2574
2575 evt->waiting = 1;
2576 evt->wait_time_stamp = jiffies;
James Smartd439d282010-09-29 11:18:45 -04002577 time_left = wait_event_interruptible_timeout(
James Smart3b5dd522010-01-26 23:10:15 -05002578 evt->wq, !list_empty(&evt->events_to_see),
James Smart256ec0d2013-04-17 20:14:58 -04002579 msecs_to_jiffies(1000 *
2580 ((phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT)));
James Smart3b5dd522010-01-26 23:10:15 -05002581 if (list_empty(&evt->events_to_see))
James Smartd439d282010-09-29 11:18:45 -04002582 ret_val = (time_left) ? -EINTR : -ETIMEDOUT;
James Smart3b5dd522010-01-26 23:10:15 -05002583 else {
James Smart3b5dd522010-01-26 23:10:15 -05002584 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2585 list_move(evt->events_to_see.prev, &evt->events_to_get);
2586 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2587 *rxxri = (list_entry(evt->events_to_get.prev,
2588 typeof(struct event_data),
2589 node))->immed_dat;
2590 }
2591 evt->waiting = 0;
2592
2593err_get_xri_exit:
2594 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2595 lpfc_bsg_event_unref(evt); /* release ref */
2596 lpfc_bsg_event_unref(evt); /* delete */
2597 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2598
2599 if (dmabuf) {
2600 if (dmabuf->virt)
2601 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
2602 kfree(dmabuf);
2603 }
2604
James Smartd439d282010-09-29 11:18:45 -04002605 if (cmdiocbq && (iocb_stat != IOCB_TIMEDOUT))
James Smart3b5dd522010-01-26 23:10:15 -05002606 lpfc_sli_release_iocbq(phba, cmdiocbq);
2607 if (rspiocbq)
2608 lpfc_sli_release_iocbq(phba, rspiocbq);
2609 return ret_val;
2610}
2611
2612/**
James Smart7ad20aa2011-05-24 11:44:28 -04002613 * lpfc_bsg_dma_page_alloc - allocate a bsg mbox page sized dma buffers
2614 * @phba: Pointer to HBA context object
2615 *
2616 * This function allocates BSG_MBOX_SIZE (4KB) page size dma buffer and.
2617 * retruns the pointer to the buffer.
2618 **/
2619static struct lpfc_dmabuf *
2620lpfc_bsg_dma_page_alloc(struct lpfc_hba *phba)
2621{
2622 struct lpfc_dmabuf *dmabuf;
2623 struct pci_dev *pcidev = phba->pcidev;
2624
2625 /* allocate dma buffer struct */
2626 dmabuf = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2627 if (!dmabuf)
2628 return NULL;
2629
2630 INIT_LIST_HEAD(&dmabuf->list);
2631
2632 /* now, allocate dma buffer */
2633 dmabuf->virt = dma_alloc_coherent(&pcidev->dev, BSG_MBOX_SIZE,
2634 &(dmabuf->phys), GFP_KERNEL);
2635
2636 if (!dmabuf->virt) {
2637 kfree(dmabuf);
2638 return NULL;
2639 }
2640 memset((uint8_t *)dmabuf->virt, 0, BSG_MBOX_SIZE);
2641
2642 return dmabuf;
2643}
2644
2645/**
2646 * lpfc_bsg_dma_page_free - free a bsg mbox page sized dma buffer
2647 * @phba: Pointer to HBA context object.
2648 * @dmabuf: Pointer to the bsg mbox page sized dma buffer descriptor.
2649 *
2650 * This routine just simply frees a dma buffer and its associated buffer
2651 * descriptor referred by @dmabuf.
2652 **/
2653static void
2654lpfc_bsg_dma_page_free(struct lpfc_hba *phba, struct lpfc_dmabuf *dmabuf)
2655{
2656 struct pci_dev *pcidev = phba->pcidev;
2657
2658 if (!dmabuf)
2659 return;
2660
2661 if (dmabuf->virt)
2662 dma_free_coherent(&pcidev->dev, BSG_MBOX_SIZE,
2663 dmabuf->virt, dmabuf->phys);
2664 kfree(dmabuf);
2665 return;
2666}
2667
2668/**
2669 * lpfc_bsg_dma_page_list_free - free a list of bsg mbox page sized dma buffers
2670 * @phba: Pointer to HBA context object.
2671 * @dmabuf_list: Pointer to a list of bsg mbox page sized dma buffer descs.
2672 *
2673 * This routine just simply frees all dma buffers and their associated buffer
2674 * descriptors referred by @dmabuf_list.
2675 **/
2676static void
2677lpfc_bsg_dma_page_list_free(struct lpfc_hba *phba,
2678 struct list_head *dmabuf_list)
2679{
2680 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
2681
2682 if (list_empty(dmabuf_list))
2683 return;
2684
2685 list_for_each_entry_safe(dmabuf, next_dmabuf, dmabuf_list, list) {
2686 list_del_init(&dmabuf->list);
2687 lpfc_bsg_dma_page_free(phba, dmabuf);
2688 }
2689 return;
2690}
2691
2692/**
James Smart3b5dd522010-01-26 23:10:15 -05002693 * diag_cmd_data_alloc - fills in a bde struct with dma buffers
2694 * @phba: Pointer to HBA context object
2695 * @bpl: Pointer to 64 bit bde structure
2696 * @size: Number of bytes to process
2697 * @nocopydata: Flag to copy user data into the allocated buffer
2698 *
2699 * This function allocates page size buffers and populates an lpfc_dmabufext.
2700 * If allowed the user data pointed to with indataptr is copied into the kernel
2701 * memory. The chained list of page size buffers is returned.
2702 **/
2703static struct lpfc_dmabufext *
2704diag_cmd_data_alloc(struct lpfc_hba *phba,
2705 struct ulp_bde64 *bpl, uint32_t size,
2706 int nocopydata)
2707{
2708 struct lpfc_dmabufext *mlist = NULL;
2709 struct lpfc_dmabufext *dmp;
2710 int cnt, offset = 0, i = 0;
2711 struct pci_dev *pcidev;
2712
2713 pcidev = phba->pcidev;
2714
2715 while (size) {
2716 /* We get chunks of 4K */
2717 if (size > BUF_SZ_4K)
2718 cnt = BUF_SZ_4K;
2719 else
2720 cnt = size;
2721
2722 /* allocate struct lpfc_dmabufext buffer header */
2723 dmp = kmalloc(sizeof(struct lpfc_dmabufext), GFP_KERNEL);
2724 if (!dmp)
2725 goto out;
2726
2727 INIT_LIST_HEAD(&dmp->dma.list);
2728
2729 /* Queue it to a linked list */
2730 if (mlist)
2731 list_add_tail(&dmp->dma.list, &mlist->dma.list);
2732 else
2733 mlist = dmp;
2734
2735 /* allocate buffer */
2736 dmp->dma.virt = dma_alloc_coherent(&pcidev->dev,
2737 cnt,
2738 &(dmp->dma.phys),
2739 GFP_KERNEL);
2740
2741 if (!dmp->dma.virt)
2742 goto out;
2743
2744 dmp->size = cnt;
2745
2746 if (nocopydata) {
2747 bpl->tus.f.bdeFlags = 0;
2748 pci_dma_sync_single_for_device(phba->pcidev,
2749 dmp->dma.phys, LPFC_BPL_SIZE, PCI_DMA_TODEVICE);
2750
2751 } else {
2752 memset((uint8_t *)dmp->dma.virt, 0, cnt);
2753 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2754 }
2755
2756 /* build buffer ptr list for IOCB */
2757 bpl->addrLow = le32_to_cpu(putPaddrLow(dmp->dma.phys));
2758 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dmp->dma.phys));
2759 bpl->tus.f.bdeSize = (ushort) cnt;
2760 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2761 bpl++;
2762
2763 i++;
2764 offset += cnt;
2765 size -= cnt;
2766 }
2767
2768 mlist->flag = i;
2769 return mlist;
2770out:
2771 diag_cmd_data_free(phba, mlist);
2772 return NULL;
2773}
2774
2775/**
2776 * lpfcdiag_loop_post_rxbufs - post the receive buffers for an unsol CT cmd
2777 * @phba: Pointer to HBA context object
2778 * @rxxri: Receive exchange id
2779 * @len: Number of data bytes
2780 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002781 * This function allocates and posts a data buffer of sufficient size to receive
James Smart3b5dd522010-01-26 23:10:15 -05002782 * an unsolicted CT command.
2783 **/
2784static int lpfcdiag_loop_post_rxbufs(struct lpfc_hba *phba, uint16_t rxxri,
2785 size_t len)
2786{
2787 struct lpfc_sli *psli = &phba->sli;
2788 struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
2789 struct lpfc_iocbq *cmdiocbq;
2790 IOCB_t *cmd = NULL;
2791 struct list_head head, *curr, *next;
2792 struct lpfc_dmabuf *rxbmp;
2793 struct lpfc_dmabuf *dmp;
2794 struct lpfc_dmabuf *mp[2] = {NULL, NULL};
2795 struct ulp_bde64 *rxbpl = NULL;
2796 uint32_t num_bde;
2797 struct lpfc_dmabufext *rxbuffer = NULL;
2798 int ret_val = 0;
James Smartd439d282010-09-29 11:18:45 -04002799 int iocb_stat;
James Smart3b5dd522010-01-26 23:10:15 -05002800 int i = 0;
2801
2802 cmdiocbq = lpfc_sli_get_iocbq(phba);
2803 rxbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2804 if (rxbmp != NULL) {
2805 rxbmp->virt = lpfc_mbuf_alloc(phba, 0, &rxbmp->phys);
James Smartc7495932010-04-06 15:05:28 -04002806 if (rxbmp->virt) {
2807 INIT_LIST_HEAD(&rxbmp->list);
2808 rxbpl = (struct ulp_bde64 *) rxbmp->virt;
2809 rxbuffer = diag_cmd_data_alloc(phba, rxbpl, len, 0);
2810 }
James Smart3b5dd522010-01-26 23:10:15 -05002811 }
2812
2813 if (!cmdiocbq || !rxbmp || !rxbpl || !rxbuffer) {
James Smartd439d282010-09-29 11:18:45 -04002814 ret_val = -ENOMEM;
James Smart3b5dd522010-01-26 23:10:15 -05002815 goto err_post_rxbufs_exit;
2816 }
2817
2818 /* Queue buffers for the receive exchange */
2819 num_bde = (uint32_t)rxbuffer->flag;
2820 dmp = &rxbuffer->dma;
2821
2822 cmd = &cmdiocbq->iocb;
2823 i = 0;
2824
2825 INIT_LIST_HEAD(&head);
2826 list_add_tail(&head, &dmp->list);
2827 list_for_each_safe(curr, next, &head) {
2828 mp[i] = list_entry(curr, struct lpfc_dmabuf, list);
2829 list_del(curr);
2830
2831 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
2832 mp[i]->buffer_tag = lpfc_sli_get_buffer_tag(phba);
2833 cmd->un.quexri64cx.buff.bde.addrHigh =
2834 putPaddrHigh(mp[i]->phys);
2835 cmd->un.quexri64cx.buff.bde.addrLow =
2836 putPaddrLow(mp[i]->phys);
2837 cmd->un.quexri64cx.buff.bde.tus.f.bdeSize =
2838 ((struct lpfc_dmabufext *)mp[i])->size;
2839 cmd->un.quexri64cx.buff.buffer_tag = mp[i]->buffer_tag;
2840 cmd->ulpCommand = CMD_QUE_XRI64_CX;
2841 cmd->ulpPU = 0;
2842 cmd->ulpLe = 1;
2843 cmd->ulpBdeCount = 1;
2844 cmd->unsli3.que_xri64cx_ext_words.ebde_count = 0;
2845
2846 } else {
2847 cmd->un.cont64[i].addrHigh = putPaddrHigh(mp[i]->phys);
2848 cmd->un.cont64[i].addrLow = putPaddrLow(mp[i]->phys);
2849 cmd->un.cont64[i].tus.f.bdeSize =
2850 ((struct lpfc_dmabufext *)mp[i])->size;
2851 cmd->ulpBdeCount = ++i;
2852
2853 if ((--num_bde > 0) && (i < 2))
2854 continue;
2855
2856 cmd->ulpCommand = CMD_QUE_XRI_BUF64_CX;
2857 cmd->ulpLe = 1;
2858 }
2859
2860 cmd->ulpClass = CLASS3;
2861 cmd->ulpContext = rxxri;
2862
James Smartd439d282010-09-29 11:18:45 -04002863 iocb_stat = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq,
2864 0);
2865 if (iocb_stat == IOCB_ERROR) {
James Smart3b5dd522010-01-26 23:10:15 -05002866 diag_cmd_data_free(phba,
2867 (struct lpfc_dmabufext *)mp[0]);
2868 if (mp[1])
2869 diag_cmd_data_free(phba,
2870 (struct lpfc_dmabufext *)mp[1]);
2871 dmp = list_entry(next, struct lpfc_dmabuf, list);
James Smartd439d282010-09-29 11:18:45 -04002872 ret_val = -EIO;
James Smart3b5dd522010-01-26 23:10:15 -05002873 goto err_post_rxbufs_exit;
2874 }
2875
2876 lpfc_sli_ringpostbuf_put(phba, pring, mp[0]);
2877 if (mp[1]) {
2878 lpfc_sli_ringpostbuf_put(phba, pring, mp[1]);
2879 mp[1] = NULL;
2880 }
2881
2882 /* The iocb was freed by lpfc_sli_issue_iocb */
2883 cmdiocbq = lpfc_sli_get_iocbq(phba);
2884 if (!cmdiocbq) {
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 cmd = &cmdiocbq->iocb;
2891 i = 0;
2892 }
2893 list_del(&head);
2894
2895err_post_rxbufs_exit:
2896
2897 if (rxbmp) {
2898 if (rxbmp->virt)
2899 lpfc_mbuf_free(phba, rxbmp->virt, rxbmp->phys);
2900 kfree(rxbmp);
2901 }
2902
2903 if (cmdiocbq)
2904 lpfc_sli_release_iocbq(phba, cmdiocbq);
2905 return ret_val;
2906}
2907
2908/**
James Smart7ad20aa2011-05-24 11:44:28 -04002909 * lpfc_bsg_diag_loopback_run - run loopback on a port by issue ct cmd to itself
James Smart3b5dd522010-01-26 23:10:15 -05002910 * @job: LPFC_BSG_VENDOR_DIAG_TEST fc_bsg_job
2911 *
2912 * This function receives a user data buffer to be transmitted and received on
2913 * the same port, the link must be up and in loopback mode prior
2914 * to being called.
2915 * 1. A kernel buffer is allocated to copy the user data into.
2916 * 2. The port registers with "itself".
2917 * 3. The transmit and receive exchange ids are obtained.
2918 * 4. The receive exchange id is posted.
2919 * 5. A new els loopback event is created.
2920 * 6. The command and response iocbs are allocated.
2921 * 7. The cmd iocb FsType is set to elx loopback and the CmdRsp to looppback.
2922 *
2923 * This function is meant to be called n times while the port is in loopback
2924 * so it is the apps responsibility to issue a reset to take the port out
2925 * of loopback mode.
2926 **/
2927static int
James Smart7ad20aa2011-05-24 11:44:28 -04002928lpfc_bsg_diag_loopback_run(struct fc_bsg_job *job)
James Smart3b5dd522010-01-26 23:10:15 -05002929{
2930 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
2931 struct lpfc_hba *phba = vport->phba;
2932 struct diag_mode_test *diag_mode;
2933 struct lpfc_bsg_event *evt;
2934 struct event_data *evdat;
2935 struct lpfc_sli *psli = &phba->sli;
2936 uint32_t size;
2937 uint32_t full_size;
2938 size_t segment_len = 0, segment_offset = 0, current_offset = 0;
James Smart40426292010-12-15 17:58:10 -05002939 uint16_t rpi = 0;
James Smart1b511972011-12-13 13:23:09 -05002940 struct lpfc_iocbq *cmdiocbq, *rspiocbq = NULL;
2941 IOCB_t *cmd, *rsp = NULL;
James Smart3b5dd522010-01-26 23:10:15 -05002942 struct lpfc_sli_ct_request *ctreq;
2943 struct lpfc_dmabuf *txbmp;
2944 struct ulp_bde64 *txbpl = NULL;
2945 struct lpfc_dmabufext *txbuffer = NULL;
2946 struct list_head head;
2947 struct lpfc_dmabuf *curr;
James Smart1b511972011-12-13 13:23:09 -05002948 uint16_t txxri = 0, rxxri;
James Smart3b5dd522010-01-26 23:10:15 -05002949 uint32_t num_bde;
2950 uint8_t *ptr = NULL, *rx_databuf = NULL;
2951 int rc = 0;
James Smartd439d282010-09-29 11:18:45 -04002952 int time_left;
2953 int iocb_stat;
James Smart3b5dd522010-01-26 23:10:15 -05002954 unsigned long flags;
2955 void *dataout = NULL;
2956 uint32_t total_mem;
2957
2958 /* in case no data is returned return just the return code */
2959 job->reply->reply_payload_rcv_len = 0;
2960
2961 if (job->request_len <
2962 sizeof(struct fc_bsg_request) + sizeof(struct diag_mode_test)) {
2963 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2964 "2739 Received DIAG TEST request below minimum "
2965 "size\n");
2966 rc = -EINVAL;
2967 goto loopback_test_exit;
2968 }
2969
2970 if (job->request_payload.payload_len !=
2971 job->reply_payload.payload_len) {
2972 rc = -EINVAL;
2973 goto loopback_test_exit;
2974 }
James Smart3b5dd522010-01-26 23:10:15 -05002975 diag_mode = (struct diag_mode_test *)
2976 job->request->rqst_data.h_vendor.vendor_cmd;
2977
2978 if ((phba->link_state == LPFC_HBA_ERROR) ||
2979 (psli->sli_flag & LPFC_BLOCK_MGMT_IO) ||
2980 (!(psli->sli_flag & LPFC_SLI_ACTIVE))) {
2981 rc = -EACCES;
2982 goto loopback_test_exit;
2983 }
2984
2985 if (!lpfc_is_link_up(phba) || !(phba->link_flag & LS_LOOPBACK_MODE)) {
2986 rc = -EACCES;
2987 goto loopback_test_exit;
2988 }
2989
2990 size = job->request_payload.payload_len;
2991 full_size = size + ELX_LOOPBACK_HEADER_SZ; /* plus the header */
2992
2993 if ((size == 0) || (size > 80 * BUF_SZ_4K)) {
2994 rc = -ERANGE;
2995 goto loopback_test_exit;
2996 }
2997
James Smart63e801c2010-11-20 23:14:19 -05002998 if (full_size >= BUF_SZ_4K) {
James Smart3b5dd522010-01-26 23:10:15 -05002999 /*
3000 * Allocate memory for ioctl data. If buffer is bigger than 64k,
3001 * then we allocate 64k and re-use that buffer over and over to
3002 * xfer the whole block. This is because Linux kernel has a
3003 * problem allocating more than 120k of kernel space memory. Saw
3004 * problem with GET_FCPTARGETMAPPING...
3005 */
3006 if (size <= (64 * 1024))
James Smart63e801c2010-11-20 23:14:19 -05003007 total_mem = full_size;
James Smart3b5dd522010-01-26 23:10:15 -05003008 else
3009 total_mem = 64 * 1024;
3010 } else
3011 /* Allocate memory for ioctl data */
3012 total_mem = BUF_SZ_4K;
3013
3014 dataout = kmalloc(total_mem, GFP_KERNEL);
3015 if (dataout == NULL) {
3016 rc = -ENOMEM;
3017 goto loopback_test_exit;
3018 }
3019
3020 ptr = dataout;
3021 ptr += ELX_LOOPBACK_HEADER_SZ;
3022 sg_copy_to_buffer(job->request_payload.sg_list,
3023 job->request_payload.sg_cnt,
3024 ptr, size);
James Smart3b5dd522010-01-26 23:10:15 -05003025 rc = lpfcdiag_loop_self_reg(phba, &rpi);
James Smartd439d282010-09-29 11:18:45 -04003026 if (rc)
James Smart3b5dd522010-01-26 23:10:15 -05003027 goto loopback_test_exit;
James Smart3b5dd522010-01-26 23:10:15 -05003028
James Smart1b511972011-12-13 13:23:09 -05003029 if (phba->sli_rev < LPFC_SLI_REV4) {
3030 rc = lpfcdiag_loop_get_xri(phba, rpi, &txxri, &rxxri);
3031 if (rc) {
3032 lpfcdiag_loop_self_unreg(phba, rpi);
3033 goto loopback_test_exit;
3034 }
James Smart3b5dd522010-01-26 23:10:15 -05003035
James Smart1b511972011-12-13 13:23:09 -05003036 rc = lpfcdiag_loop_post_rxbufs(phba, rxxri, full_size);
3037 if (rc) {
3038 lpfcdiag_loop_self_unreg(phba, rpi);
3039 goto loopback_test_exit;
3040 }
James Smart3b5dd522010-01-26 23:10:15 -05003041 }
James Smart3b5dd522010-01-26 23:10:15 -05003042 evt = lpfc_bsg_event_new(FC_REG_CT_EVENT, current->pid,
3043 SLI_CT_ELX_LOOPBACK);
3044 if (!evt) {
3045 lpfcdiag_loop_self_unreg(phba, rpi);
3046 rc = -ENOMEM;
3047 goto loopback_test_exit;
3048 }
3049
3050 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3051 list_add(&evt->node, &phba->ct_ev_waiters);
3052 lpfc_bsg_event_ref(evt);
3053 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3054
3055 cmdiocbq = lpfc_sli_get_iocbq(phba);
James Smart1b511972011-12-13 13:23:09 -05003056 if (phba->sli_rev < LPFC_SLI_REV4)
3057 rspiocbq = lpfc_sli_get_iocbq(phba);
James Smart3b5dd522010-01-26 23:10:15 -05003058 txbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3059
3060 if (txbmp) {
3061 txbmp->virt = lpfc_mbuf_alloc(phba, 0, &txbmp->phys);
James Smartc7495932010-04-06 15:05:28 -04003062 if (txbmp->virt) {
3063 INIT_LIST_HEAD(&txbmp->list);
3064 txbpl = (struct ulp_bde64 *) txbmp->virt;
James Smart3b5dd522010-01-26 23:10:15 -05003065 txbuffer = diag_cmd_data_alloc(phba,
3066 txbpl, full_size, 0);
James Smartc7495932010-04-06 15:05:28 -04003067 }
James Smart3b5dd522010-01-26 23:10:15 -05003068 }
3069
James Smart1b511972011-12-13 13:23:09 -05003070 if (!cmdiocbq || !txbmp || !txbpl || !txbuffer || !txbmp->virt) {
3071 rc = -ENOMEM;
3072 goto err_loopback_test_exit;
3073 }
3074 if ((phba->sli_rev < LPFC_SLI_REV4) && !rspiocbq) {
James Smart3b5dd522010-01-26 23:10:15 -05003075 rc = -ENOMEM;
3076 goto err_loopback_test_exit;
3077 }
3078
3079 cmd = &cmdiocbq->iocb;
James Smart1b511972011-12-13 13:23:09 -05003080 if (phba->sli_rev < LPFC_SLI_REV4)
3081 rsp = &rspiocbq->iocb;
James Smart3b5dd522010-01-26 23:10:15 -05003082
3083 INIT_LIST_HEAD(&head);
3084 list_add_tail(&head, &txbuffer->dma.list);
3085 list_for_each_entry(curr, &head, list) {
3086 segment_len = ((struct lpfc_dmabufext *)curr)->size;
3087 if (current_offset == 0) {
3088 ctreq = curr->virt;
3089 memset(ctreq, 0, ELX_LOOPBACK_HEADER_SZ);
3090 ctreq->RevisionId.bits.Revision = SLI_CT_REVISION;
3091 ctreq->RevisionId.bits.InId = 0;
3092 ctreq->FsType = SLI_CT_ELX_LOOPBACK;
3093 ctreq->FsSubType = 0;
3094 ctreq->CommandResponse.bits.CmdRsp = ELX_LOOPBACK_DATA;
3095 ctreq->CommandResponse.bits.Size = size;
3096 segment_offset = ELX_LOOPBACK_HEADER_SZ;
3097 } else
3098 segment_offset = 0;
3099
3100 BUG_ON(segment_offset >= segment_len);
3101 memcpy(curr->virt + segment_offset,
3102 ptr + current_offset,
3103 segment_len - segment_offset);
3104
3105 current_offset += segment_len - segment_offset;
3106 BUG_ON(current_offset > size);
3107 }
3108 list_del(&head);
3109
3110 /* Build the XMIT_SEQUENCE iocb */
James Smart3b5dd522010-01-26 23:10:15 -05003111 num_bde = (uint32_t)txbuffer->flag;
3112
3113 cmd->un.xseq64.bdl.addrHigh = putPaddrHigh(txbmp->phys);
3114 cmd->un.xseq64.bdl.addrLow = putPaddrLow(txbmp->phys);
3115 cmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
3116 cmd->un.xseq64.bdl.bdeSize = (num_bde * sizeof(struct ulp_bde64));
3117
3118 cmd->un.xseq64.w5.hcsw.Fctl = (LS | LA);
3119 cmd->un.xseq64.w5.hcsw.Dfctl = 0;
3120 cmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
3121 cmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
3122
3123 cmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;
3124 cmd->ulpBdeCount = 1;
3125 cmd->ulpLe = 1;
3126 cmd->ulpClass = CLASS3;
James Smart3b5dd522010-01-26 23:10:15 -05003127
James Smart1b511972011-12-13 13:23:09 -05003128 if (phba->sli_rev < LPFC_SLI_REV4) {
3129 cmd->ulpContext = txxri;
3130 } else {
3131 cmd->un.xseq64.bdl.ulpIoTag32 = 0;
3132 cmd->un.ulpWord[3] = phba->sli4_hba.rpi_ids[rpi];
3133 cmdiocbq->context3 = txbmp;
3134 cmdiocbq->sli4_xritag = NO_XRI;
3135 cmd->unsli3.rcvsli3.ox_id = 0xffff;
3136 }
James Smart3b5dd522010-01-26 23:10:15 -05003137 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
3138 cmdiocbq->vport = phba->pport;
James Smartd439d282010-09-29 11:18:45 -04003139 iocb_stat = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq,
3140 rspiocbq, (phba->fc_ratov * 2) +
3141 LPFC_DRVR_TIMEOUT);
James Smart3b5dd522010-01-26 23:10:15 -05003142
James Smart1b511972011-12-13 13:23:09 -05003143 if ((iocb_stat != IOCB_SUCCESS) || ((phba->sli_rev < LPFC_SLI_REV4) &&
3144 (rsp->ulpStatus != IOCB_SUCCESS))) {
3145 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3146 "3126 Failed loopback test issue iocb: "
3147 "iocb_stat:x%x\n", iocb_stat);
James Smart3b5dd522010-01-26 23:10:15 -05003148 rc = -EIO;
3149 goto err_loopback_test_exit;
3150 }
3151
3152 evt->waiting = 1;
James Smartd439d282010-09-29 11:18:45 -04003153 time_left = wait_event_interruptible_timeout(
James Smart3b5dd522010-01-26 23:10:15 -05003154 evt->wq, !list_empty(&evt->events_to_see),
James Smart256ec0d2013-04-17 20:14:58 -04003155 msecs_to_jiffies(1000 *
3156 ((phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT)));
James Smart3b5dd522010-01-26 23:10:15 -05003157 evt->waiting = 0;
James Smart1b511972011-12-13 13:23:09 -05003158 if (list_empty(&evt->events_to_see)) {
James Smartd439d282010-09-29 11:18:45 -04003159 rc = (time_left) ? -EINTR : -ETIMEDOUT;
James Smart1b511972011-12-13 13:23:09 -05003160 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3161 "3125 Not receiving unsolicited event, "
3162 "rc:x%x\n", rc);
3163 } else {
James Smart3b5dd522010-01-26 23:10:15 -05003164 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3165 list_move(evt->events_to_see.prev, &evt->events_to_get);
3166 evdat = list_entry(evt->events_to_get.prev,
3167 typeof(*evdat), node);
3168 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3169 rx_databuf = evdat->data;
3170 if (evdat->len != full_size) {
3171 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3172 "1603 Loopback test did not receive expected "
3173 "data length. actual length 0x%x expected "
3174 "length 0x%x\n",
3175 evdat->len, full_size);
3176 rc = -EIO;
3177 } else if (rx_databuf == NULL)
3178 rc = -EIO;
3179 else {
3180 rc = IOCB_SUCCESS;
3181 /* skip over elx loopback header */
3182 rx_databuf += ELX_LOOPBACK_HEADER_SZ;
3183 job->reply->reply_payload_rcv_len =
3184 sg_copy_from_buffer(job->reply_payload.sg_list,
3185 job->reply_payload.sg_cnt,
3186 rx_databuf, size);
3187 job->reply->reply_payload_rcv_len = size;
3188 }
3189 }
3190
3191err_loopback_test_exit:
3192 lpfcdiag_loop_self_unreg(phba, rpi);
3193
3194 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3195 lpfc_bsg_event_unref(evt); /* release ref */
3196 lpfc_bsg_event_unref(evt); /* delete */
3197 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3198
3199 if (cmdiocbq != NULL)
3200 lpfc_sli_release_iocbq(phba, cmdiocbq);
3201
3202 if (rspiocbq != NULL)
3203 lpfc_sli_release_iocbq(phba, rspiocbq);
3204
3205 if (txbmp != NULL) {
3206 if (txbpl != NULL) {
3207 if (txbuffer != NULL)
3208 diag_cmd_data_free(phba, txbuffer);
3209 lpfc_mbuf_free(phba, txbmp->virt, txbmp->phys);
3210 }
3211 kfree(txbmp);
3212 }
3213
3214loopback_test_exit:
3215 kfree(dataout);
3216 /* make error code available to userspace */
3217 job->reply->result = rc;
3218 job->dd_data = NULL;
3219 /* complete the job back to userspace if no error */
James Smart1b511972011-12-13 13:23:09 -05003220 if (rc == IOCB_SUCCESS)
James Smart3b5dd522010-01-26 23:10:15 -05003221 job->job_done(job);
3222 return rc;
3223}
3224
3225/**
3226 * lpfc_bsg_get_dfc_rev - process a GET_DFC_REV bsg vendor command
3227 * @job: GET_DFC_REV fc_bsg_job
3228 **/
3229static int
3230lpfc_bsg_get_dfc_rev(struct fc_bsg_job *job)
3231{
3232 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
3233 struct lpfc_hba *phba = vport->phba;
3234 struct get_mgmt_rev *event_req;
3235 struct get_mgmt_rev_reply *event_reply;
3236 int rc = 0;
3237
3238 if (job->request_len <
3239 sizeof(struct fc_bsg_request) + sizeof(struct get_mgmt_rev)) {
3240 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3241 "2740 Received GET_DFC_REV request below "
3242 "minimum size\n");
3243 rc = -EINVAL;
3244 goto job_error;
3245 }
3246
3247 event_req = (struct get_mgmt_rev *)
3248 job->request->rqst_data.h_vendor.vendor_cmd;
3249
3250 event_reply = (struct get_mgmt_rev_reply *)
3251 job->reply->reply_data.vendor_reply.vendor_rsp;
3252
3253 if (job->reply_len <
3254 sizeof(struct fc_bsg_request) + sizeof(struct get_mgmt_rev_reply)) {
3255 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3256 "2741 Received GET_DFC_REV reply below "
3257 "minimum size\n");
3258 rc = -EINVAL;
3259 goto job_error;
3260 }
3261
3262 event_reply->info.a_Major = MANAGEMENT_MAJOR_REV;
3263 event_reply->info.a_Minor = MANAGEMENT_MINOR_REV;
3264job_error:
3265 job->reply->result = rc;
3266 if (rc == 0)
3267 job->job_done(job);
3268 return rc;
3269}
3270
3271/**
James Smart7ad20aa2011-05-24 11:44:28 -04003272 * lpfc_bsg_issue_mbox_cmpl - lpfc_bsg_issue_mbox mbox completion handler
James Smart3b5dd522010-01-26 23:10:15 -05003273 * @phba: Pointer to HBA context object.
3274 * @pmboxq: Pointer to mailbox command.
3275 *
3276 * This is completion handler function for mailbox commands issued from
3277 * lpfc_bsg_issue_mbox function. This function is called by the
3278 * mailbox event handler function with no lock held. This function
3279 * will wake up thread waiting on the wait queue pointed by context1
3280 * of the mailbox.
3281 **/
3282void
James Smart7ad20aa2011-05-24 11:44:28 -04003283lpfc_bsg_issue_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
James Smart3b5dd522010-01-26 23:10:15 -05003284{
3285 struct bsg_job_data *dd_data;
James Smart3b5dd522010-01-26 23:10:15 -05003286 struct fc_bsg_job *job;
3287 uint32_t size;
3288 unsigned long flags;
James Smart7ad20aa2011-05-24 11:44:28 -04003289 uint8_t *pmb, *pmb_buf;
James Smart3b5dd522010-01-26 23:10:15 -05003290
James Smart3b5dd522010-01-26 23:10:15 -05003291 dd_data = pmboxq->context1;
James Smart3b5dd522010-01-26 23:10:15 -05003292
James Smart7ad20aa2011-05-24 11:44:28 -04003293 /*
3294 * The outgoing buffer is readily referred from the dma buffer,
3295 * just need to get header part from mailboxq structure.
James Smart7a470272010-03-15 11:25:20 -04003296 */
James Smart7ad20aa2011-05-24 11:44:28 -04003297 pmb = (uint8_t *)&pmboxq->u.mb;
3298 pmb_buf = (uint8_t *)dd_data->context_un.mbox.mb;
3299 memcpy(pmb_buf, pmb, sizeof(MAILBOX_t));
James Smart515e0aa2010-09-29 11:19:00 -04003300
James Smarta33c4f72013-03-01 16:36:00 -05003301 /* Determine if job has been aborted */
3302
3303 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3304 job = dd_data->set_job;
3305 if (job) {
3306 /* Prevent timeout handling from trying to abort job */
3307 job->dd_data = NULL;
3308 }
3309 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3310
3311 /* Copy the mailbox data to the job if it is still active */
3312
James Smart5a6f1332011-03-11 16:05:35 -05003313 if (job) {
3314 size = job->reply_payload.payload_len;
3315 job->reply->reply_payload_rcv_len =
3316 sg_copy_from_buffer(job->reply_payload.sg_list,
James Smart7ad20aa2011-05-24 11:44:28 -04003317 job->reply_payload.sg_cnt,
3318 pmb_buf, size);
James Smart5a6f1332011-03-11 16:05:35 -05003319 }
James Smart7a470272010-03-15 11:25:20 -04003320
James Smarta33c4f72013-03-01 16:36:00 -05003321 dd_data->set_job = NULL;
James Smart3b5dd522010-01-26 23:10:15 -05003322 mempool_free(dd_data->context_un.mbox.pmboxq, phba->mbox_mem_pool);
James Smart7ad20aa2011-05-24 11:44:28 -04003323 lpfc_bsg_dma_page_free(phba, dd_data->context_un.mbox.dmabuffers);
James Smart3b5dd522010-01-26 23:10:15 -05003324 kfree(dd_data);
James Smart7ad20aa2011-05-24 11:44:28 -04003325
James Smarta33c4f72013-03-01 16:36:00 -05003326 /* Complete the job if the job is still active */
3327
James Smart7ad20aa2011-05-24 11:44:28 -04003328 if (job) {
3329 job->reply->result = 0;
3330 job->job_done(job);
3331 }
James Smart3b5dd522010-01-26 23:10:15 -05003332 return;
3333}
3334
3335/**
3336 * lpfc_bsg_check_cmd_access - test for a supported mailbox command
3337 * @phba: Pointer to HBA context object.
3338 * @mb: Pointer to a mailbox object.
3339 * @vport: Pointer to a vport object.
3340 *
3341 * Some commands require the port to be offline, some may not be called from
3342 * the application.
3343 **/
3344static int lpfc_bsg_check_cmd_access(struct lpfc_hba *phba,
3345 MAILBOX_t *mb, struct lpfc_vport *vport)
3346{
3347 /* return negative error values for bsg job */
3348 switch (mb->mbxCommand) {
3349 /* Offline only */
3350 case MBX_INIT_LINK:
3351 case MBX_DOWN_LINK:
3352 case MBX_CONFIG_LINK:
3353 case MBX_CONFIG_RING:
3354 case MBX_RESET_RING:
3355 case MBX_UNREG_LOGIN:
3356 case MBX_CLEAR_LA:
3357 case MBX_DUMP_CONTEXT:
3358 case MBX_RUN_DIAGS:
3359 case MBX_RESTART:
3360 case MBX_SET_MASK:
3361 if (!(vport->fc_flag & FC_OFFLINE_MODE)) {
3362 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3363 "2743 Command 0x%x is illegal in on-line "
3364 "state\n",
3365 mb->mbxCommand);
3366 return -EPERM;
3367 }
3368 case MBX_WRITE_NV:
3369 case MBX_WRITE_VPARMS:
3370 case MBX_LOAD_SM:
3371 case MBX_READ_NV:
3372 case MBX_READ_CONFIG:
3373 case MBX_READ_RCONFIG:
3374 case MBX_READ_STATUS:
3375 case MBX_READ_XRI:
3376 case MBX_READ_REV:
3377 case MBX_READ_LNK_STAT:
3378 case MBX_DUMP_MEMORY:
3379 case MBX_DOWN_LOAD:
3380 case MBX_UPDATE_CFG:
3381 case MBX_KILL_BOARD:
3382 case MBX_LOAD_AREA:
3383 case MBX_LOAD_EXP_ROM:
3384 case MBX_BEACON:
3385 case MBX_DEL_LD_ENTRY:
3386 case MBX_SET_DEBUG:
3387 case MBX_WRITE_WWN:
3388 case MBX_SLI4_CONFIG:
James Smartc7495932010-04-06 15:05:28 -04003389 case MBX_READ_EVENT_LOG:
James Smart3b5dd522010-01-26 23:10:15 -05003390 case MBX_READ_EVENT_LOG_STATUS:
3391 case MBX_WRITE_EVENT_LOG:
3392 case MBX_PORT_CAPABILITIES:
3393 case MBX_PORT_IOV_CONTROL:
James Smart7a470272010-03-15 11:25:20 -04003394 case MBX_RUN_BIU_DIAG64:
James Smart3b5dd522010-01-26 23:10:15 -05003395 break;
3396 case MBX_SET_VARIABLE:
James Smarte2aed292010-02-26 14:15:00 -05003397 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3398 "1226 mbox: set_variable 0x%x, 0x%x\n",
3399 mb->un.varWords[0],
3400 mb->un.varWords[1]);
3401 if ((mb->un.varWords[0] == SETVAR_MLOMNT)
3402 && (mb->un.varWords[1] == 1)) {
3403 phba->wait_4_mlo_maint_flg = 1;
3404 } else if (mb->un.varWords[0] == SETVAR_MLORST) {
James Smart1b511972011-12-13 13:23:09 -05003405 spin_lock_irq(&phba->hbalock);
James Smarte2aed292010-02-26 14:15:00 -05003406 phba->link_flag &= ~LS_LOOPBACK_MODE;
James Smart1b511972011-12-13 13:23:09 -05003407 spin_unlock_irq(&phba->hbalock);
James Smart76a95d72010-11-20 23:11:48 -05003408 phba->fc_topology = LPFC_TOPOLOGY_PT_PT;
James Smarte2aed292010-02-26 14:15:00 -05003409 }
3410 break;
James Smart3b5dd522010-01-26 23:10:15 -05003411 case MBX_READ_SPARM64:
James Smart76a95d72010-11-20 23:11:48 -05003412 case MBX_READ_TOPOLOGY:
James Smart3b5dd522010-01-26 23:10:15 -05003413 case MBX_REG_LOGIN:
3414 case MBX_REG_LOGIN64:
3415 case MBX_CONFIG_PORT:
3416 case MBX_RUN_BIU_DIAG:
3417 default:
3418 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3419 "2742 Unknown Command 0x%x\n",
3420 mb->mbxCommand);
3421 return -EPERM;
3422 }
3423
3424 return 0; /* ok */
3425}
3426
3427/**
James Smart7ad20aa2011-05-24 11:44:28 -04003428 * lpfc_bsg_mbox_ext_cleanup - clean up context of multi-buffer mbox session
3429 * @phba: Pointer to HBA context object.
3430 *
3431 * This is routine clean up and reset BSG handling of multi-buffer mbox
3432 * command session.
3433 **/
3434static void
3435lpfc_bsg_mbox_ext_session_reset(struct lpfc_hba *phba)
3436{
3437 if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_IDLE)
3438 return;
3439
3440 /* free all memory, including dma buffers */
3441 lpfc_bsg_dma_page_list_free(phba,
3442 &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
3443 lpfc_bsg_dma_page_free(phba, phba->mbox_ext_buf_ctx.mbx_dmabuf);
3444 /* multi-buffer write mailbox command pass-through complete */
3445 memset((char *)&phba->mbox_ext_buf_ctx, 0,
3446 sizeof(struct lpfc_mbox_ext_buf_ctx));
3447 INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
3448
3449 return;
3450}
3451
3452/**
3453 * lpfc_bsg_issue_mbox_ext_handle_job - job handler for multi-buffer mbox cmpl
3454 * @phba: Pointer to HBA context object.
3455 * @pmboxq: Pointer to mailbox command.
3456 *
3457 * This is routine handles BSG job for mailbox commands completions with
3458 * multiple external buffers.
3459 **/
3460static struct fc_bsg_job *
3461lpfc_bsg_issue_mbox_ext_handle_job(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
3462{
3463 struct bsg_job_data *dd_data;
3464 struct fc_bsg_job *job;
3465 uint8_t *pmb, *pmb_buf;
3466 unsigned long flags;
3467 uint32_t size;
3468 int rc = 0;
James Smart026abb82011-12-13 13:20:45 -05003469 struct lpfc_dmabuf *dmabuf;
3470 struct lpfc_sli_config_mbox *sli_cfg_mbx;
3471 uint8_t *pmbx;
James Smart7ad20aa2011-05-24 11:44:28 -04003472
James Smart7ad20aa2011-05-24 11:44:28 -04003473 dd_data = pmboxq->context1;
James Smarta33c4f72013-03-01 16:36:00 -05003474
3475 /* Determine if job has been aborted */
3476 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3477 job = dd_data->set_job;
3478 if (job) {
3479 /* Prevent timeout handling from trying to abort job */
3480 job->dd_data = NULL;
James Smart7ad20aa2011-05-24 11:44:28 -04003481 }
James Smarta33c4f72013-03-01 16:36:00 -05003482 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smart7ad20aa2011-05-24 11:44:28 -04003483
3484 /*
3485 * The outgoing buffer is readily referred from the dma buffer,
3486 * just need to get header part from mailboxq structure.
3487 */
James Smarta33c4f72013-03-01 16:36:00 -05003488
James Smart7ad20aa2011-05-24 11:44:28 -04003489 pmb = (uint8_t *)&pmboxq->u.mb;
3490 pmb_buf = (uint8_t *)dd_data->context_un.mbox.mb;
James Smart026abb82011-12-13 13:20:45 -05003491 /* Copy the byte swapped response mailbox back to the user */
James Smart7ad20aa2011-05-24 11:44:28 -04003492 memcpy(pmb_buf, pmb, sizeof(MAILBOX_t));
James Smart026abb82011-12-13 13:20:45 -05003493 /* if there is any non-embedded extended data copy that too */
3494 dmabuf = phba->mbox_ext_buf_ctx.mbx_dmabuf;
3495 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
3496 if (!bsg_bf_get(lpfc_mbox_hdr_emb,
3497 &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) {
3498 pmbx = (uint8_t *)dmabuf->virt;
3499 /* byte swap the extended data following the mailbox command */
3500 lpfc_sli_pcimem_bcopy(&pmbx[sizeof(MAILBOX_t)],
3501 &pmbx[sizeof(MAILBOX_t)],
3502 sli_cfg_mbx->un.sli_config_emb0_subsys.mse[0].buf_len);
3503 }
James Smart7ad20aa2011-05-24 11:44:28 -04003504
James Smarta33c4f72013-03-01 16:36:00 -05003505 /* Complete the job if the job is still active */
3506
James Smart7ad20aa2011-05-24 11:44:28 -04003507 if (job) {
3508 size = job->reply_payload.payload_len;
3509 job->reply->reply_payload_rcv_len =
3510 sg_copy_from_buffer(job->reply_payload.sg_list,
3511 job->reply_payload.sg_cnt,
3512 pmb_buf, size);
James Smarta33c4f72013-03-01 16:36:00 -05003513
James Smart7ad20aa2011-05-24 11:44:28 -04003514 /* result for successful */
3515 job->reply->result = 0;
James Smarta33c4f72013-03-01 16:36:00 -05003516
James Smart7ad20aa2011-05-24 11:44:28 -04003517 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3518 "2937 SLI_CONFIG ext-buffer maibox command "
3519 "(x%x/x%x) complete bsg job done, bsize:%d\n",
3520 phba->mbox_ext_buf_ctx.nembType,
3521 phba->mbox_ext_buf_ctx.mboxType, size);
James Smartb76f2dc2011-07-22 18:37:42 -04003522 lpfc_idiag_mbxacc_dump_bsg_mbox(phba,
3523 phba->mbox_ext_buf_ctx.nembType,
3524 phba->mbox_ext_buf_ctx.mboxType,
3525 dma_ebuf, sta_pos_addr,
3526 phba->mbox_ext_buf_ctx.mbx_dmabuf, 0);
James Smarta33c4f72013-03-01 16:36:00 -05003527 } else {
James Smart7ad20aa2011-05-24 11:44:28 -04003528 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3529 "2938 SLI_CONFIG ext-buffer maibox "
3530 "command (x%x/x%x) failure, rc:x%x\n",
3531 phba->mbox_ext_buf_ctx.nembType,
3532 phba->mbox_ext_buf_ctx.mboxType, rc);
James Smarta33c4f72013-03-01 16:36:00 -05003533 }
3534
3535
James Smart7ad20aa2011-05-24 11:44:28 -04003536 /* state change */
3537 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_DONE;
3538 kfree(dd_data);
James Smart7ad20aa2011-05-24 11:44:28 -04003539 return job;
3540}
3541
3542/**
3543 * lpfc_bsg_issue_read_mbox_ext_cmpl - compl handler for multi-buffer read mbox
3544 * @phba: Pointer to HBA context object.
3545 * @pmboxq: Pointer to mailbox command.
3546 *
3547 * This is completion handler function for mailbox read commands with multiple
3548 * external buffers.
3549 **/
3550static void
3551lpfc_bsg_issue_read_mbox_ext_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
3552{
3553 struct fc_bsg_job *job;
3554
James Smarta33c4f72013-03-01 16:36:00 -05003555 job = lpfc_bsg_issue_mbox_ext_handle_job(phba, pmboxq);
3556
James Smart7ad20aa2011-05-24 11:44:28 -04003557 /* handle the BSG job with mailbox command */
James Smarta33c4f72013-03-01 16:36:00 -05003558 if (!job)
James Smart7ad20aa2011-05-24 11:44:28 -04003559 pmboxq->u.mb.mbxStatus = MBXERR_ERROR;
3560
3561 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3562 "2939 SLI_CONFIG ext-buffer rd maibox command "
3563 "complete, ctxState:x%x, mbxStatus:x%x\n",
3564 phba->mbox_ext_buf_ctx.state, pmboxq->u.mb.mbxStatus);
3565
James Smart7ad20aa2011-05-24 11:44:28 -04003566 if (pmboxq->u.mb.mbxStatus || phba->mbox_ext_buf_ctx.numBuf == 1)
3567 lpfc_bsg_mbox_ext_session_reset(phba);
3568
3569 /* free base driver mailbox structure memory */
3570 mempool_free(pmboxq, phba->mbox_mem_pool);
3571
James Smarta33c4f72013-03-01 16:36:00 -05003572 /* if the job is still active, call job done */
James Smart7ad20aa2011-05-24 11:44:28 -04003573 if (job)
3574 job->job_done(job);
3575
3576 return;
3577}
3578
3579/**
3580 * lpfc_bsg_issue_write_mbox_ext_cmpl - cmpl handler for multi-buffer write mbox
3581 * @phba: Pointer to HBA context object.
3582 * @pmboxq: Pointer to mailbox command.
3583 *
3584 * This is completion handler function for mailbox write commands with multiple
3585 * external buffers.
3586 **/
3587static void
3588lpfc_bsg_issue_write_mbox_ext_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
3589{
3590 struct fc_bsg_job *job;
3591
James Smarta33c4f72013-03-01 16:36:00 -05003592 job = lpfc_bsg_issue_mbox_ext_handle_job(phba, pmboxq);
3593
James Smart7ad20aa2011-05-24 11:44:28 -04003594 /* handle the BSG job with the mailbox command */
James Smarta33c4f72013-03-01 16:36:00 -05003595 if (!job)
James Smart7ad20aa2011-05-24 11:44:28 -04003596 pmboxq->u.mb.mbxStatus = MBXERR_ERROR;
3597
3598 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3599 "2940 SLI_CONFIG ext-buffer wr maibox command "
3600 "complete, ctxState:x%x, mbxStatus:x%x\n",
3601 phba->mbox_ext_buf_ctx.state, pmboxq->u.mb.mbxStatus);
3602
James Smart7ad20aa2011-05-24 11:44:28 -04003603 /* free all memory, including dma buffers */
3604 mempool_free(pmboxq, phba->mbox_mem_pool);
3605 lpfc_bsg_mbox_ext_session_reset(phba);
3606
James Smarta33c4f72013-03-01 16:36:00 -05003607 /* if the job is still active, call job done */
James Smart7ad20aa2011-05-24 11:44:28 -04003608 if (job)
3609 job->job_done(job);
3610
3611 return;
3612}
3613
3614static void
3615lpfc_bsg_sli_cfg_dma_desc_setup(struct lpfc_hba *phba, enum nemb_type nemb_tp,
3616 uint32_t index, struct lpfc_dmabuf *mbx_dmabuf,
3617 struct lpfc_dmabuf *ext_dmabuf)
3618{
3619 struct lpfc_sli_config_mbox *sli_cfg_mbx;
3620
3621 /* pointer to the start of mailbox command */
3622 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)mbx_dmabuf->virt;
3623
3624 if (nemb_tp == nemb_mse) {
3625 if (index == 0) {
3626 sli_cfg_mbx->un.sli_config_emb0_subsys.
3627 mse[index].pa_hi =
3628 putPaddrHigh(mbx_dmabuf->phys +
3629 sizeof(MAILBOX_t));
3630 sli_cfg_mbx->un.sli_config_emb0_subsys.
3631 mse[index].pa_lo =
3632 putPaddrLow(mbx_dmabuf->phys +
3633 sizeof(MAILBOX_t));
3634 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3635 "2943 SLI_CONFIG(mse)[%d], "
3636 "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3637 index,
3638 sli_cfg_mbx->un.sli_config_emb0_subsys.
3639 mse[index].buf_len,
3640 sli_cfg_mbx->un.sli_config_emb0_subsys.
3641 mse[index].pa_hi,
3642 sli_cfg_mbx->un.sli_config_emb0_subsys.
3643 mse[index].pa_lo);
3644 } else {
3645 sli_cfg_mbx->un.sli_config_emb0_subsys.
3646 mse[index].pa_hi =
3647 putPaddrHigh(ext_dmabuf->phys);
3648 sli_cfg_mbx->un.sli_config_emb0_subsys.
3649 mse[index].pa_lo =
3650 putPaddrLow(ext_dmabuf->phys);
3651 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3652 "2944 SLI_CONFIG(mse)[%d], "
3653 "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3654 index,
3655 sli_cfg_mbx->un.sli_config_emb0_subsys.
3656 mse[index].buf_len,
3657 sli_cfg_mbx->un.sli_config_emb0_subsys.
3658 mse[index].pa_hi,
3659 sli_cfg_mbx->un.sli_config_emb0_subsys.
3660 mse[index].pa_lo);
3661 }
3662 } else {
3663 if (index == 0) {
3664 sli_cfg_mbx->un.sli_config_emb1_subsys.
3665 hbd[index].pa_hi =
3666 putPaddrHigh(mbx_dmabuf->phys +
3667 sizeof(MAILBOX_t));
3668 sli_cfg_mbx->un.sli_config_emb1_subsys.
3669 hbd[index].pa_lo =
3670 putPaddrLow(mbx_dmabuf->phys +
3671 sizeof(MAILBOX_t));
3672 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3673 "3007 SLI_CONFIG(hbd)[%d], "
3674 "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3675 index,
3676 bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
3677 &sli_cfg_mbx->un.
3678 sli_config_emb1_subsys.hbd[index]),
3679 sli_cfg_mbx->un.sli_config_emb1_subsys.
3680 hbd[index].pa_hi,
3681 sli_cfg_mbx->un.sli_config_emb1_subsys.
3682 hbd[index].pa_lo);
3683
3684 } else {
3685 sli_cfg_mbx->un.sli_config_emb1_subsys.
3686 hbd[index].pa_hi =
3687 putPaddrHigh(ext_dmabuf->phys);
3688 sli_cfg_mbx->un.sli_config_emb1_subsys.
3689 hbd[index].pa_lo =
3690 putPaddrLow(ext_dmabuf->phys);
3691 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3692 "3008 SLI_CONFIG(hbd)[%d], "
3693 "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3694 index,
3695 bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
3696 &sli_cfg_mbx->un.
3697 sli_config_emb1_subsys.hbd[index]),
3698 sli_cfg_mbx->un.sli_config_emb1_subsys.
3699 hbd[index].pa_hi,
3700 sli_cfg_mbx->un.sli_config_emb1_subsys.
3701 hbd[index].pa_lo);
3702 }
3703 }
3704 return;
3705}
3706
3707/**
3708 * lpfc_bsg_sli_cfg_mse_read_cmd_ext - sli_config non-embedded mailbox cmd read
3709 * @phba: Pointer to HBA context object.
3710 * @mb: Pointer to a BSG mailbox object.
3711 * @nemb_tp: Enumerate of non-embedded mailbox command type.
3712 * @dmabuff: Pointer to a DMA buffer descriptor.
3713 *
3714 * This routine performs SLI_CONFIG (0x9B) read mailbox command operation with
3715 * non-embedded external bufffers.
3716 **/
3717static int
3718lpfc_bsg_sli_cfg_read_cmd_ext(struct lpfc_hba *phba, struct fc_bsg_job *job,
3719 enum nemb_type nemb_tp,
3720 struct lpfc_dmabuf *dmabuf)
3721{
3722 struct lpfc_sli_config_mbox *sli_cfg_mbx;
3723 struct dfc_mbox_req *mbox_req;
3724 struct lpfc_dmabuf *curr_dmabuf, *next_dmabuf;
3725 uint32_t ext_buf_cnt, ext_buf_index;
3726 struct lpfc_dmabuf *ext_dmabuf = NULL;
3727 struct bsg_job_data *dd_data = NULL;
3728 LPFC_MBOXQ_t *pmboxq = NULL;
3729 MAILBOX_t *pmb;
3730 uint8_t *pmbx;
3731 int rc, i;
3732
3733 mbox_req =
3734 (struct dfc_mbox_req *)job->request->rqst_data.h_vendor.vendor_cmd;
3735
3736 /* pointer to the start of mailbox command */
3737 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
3738
3739 if (nemb_tp == nemb_mse) {
3740 ext_buf_cnt = bsg_bf_get(lpfc_mbox_hdr_mse_cnt,
3741 &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr);
3742 if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_MSE) {
3743 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3744 "2945 Handled SLI_CONFIG(mse) rd, "
3745 "ext_buf_cnt(%d) out of range(%d)\n",
3746 ext_buf_cnt,
3747 LPFC_MBX_SLI_CONFIG_MAX_MSE);
3748 rc = -ERANGE;
3749 goto job_error;
3750 }
3751 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3752 "2941 Handled SLI_CONFIG(mse) rd, "
3753 "ext_buf_cnt:%d\n", ext_buf_cnt);
3754 } else {
3755 /* sanity check on interface type for support */
3756 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
3757 LPFC_SLI_INTF_IF_TYPE_2) {
3758 rc = -ENODEV;
3759 goto job_error;
3760 }
3761 /* nemb_tp == nemb_hbd */
3762 ext_buf_cnt = sli_cfg_mbx->un.sli_config_emb1_subsys.hbd_count;
3763 if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_HBD) {
3764 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3765 "2946 Handled SLI_CONFIG(hbd) rd, "
3766 "ext_buf_cnt(%d) out of range(%d)\n",
3767 ext_buf_cnt,
3768 LPFC_MBX_SLI_CONFIG_MAX_HBD);
3769 rc = -ERANGE;
3770 goto job_error;
3771 }
3772 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3773 "2942 Handled SLI_CONFIG(hbd) rd, "
3774 "ext_buf_cnt:%d\n", ext_buf_cnt);
3775 }
3776
James Smartb76f2dc2011-07-22 18:37:42 -04003777 /* before dma descriptor setup */
3778 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_rd, dma_mbox,
3779 sta_pre_addr, dmabuf, ext_buf_cnt);
3780
James Smart7ad20aa2011-05-24 11:44:28 -04003781 /* reject non-embedded mailbox command with none external buffer */
3782 if (ext_buf_cnt == 0) {
3783 rc = -EPERM;
3784 goto job_error;
3785 } else if (ext_buf_cnt > 1) {
3786 /* additional external read buffers */
3787 for (i = 1; i < ext_buf_cnt; i++) {
3788 ext_dmabuf = lpfc_bsg_dma_page_alloc(phba);
3789 if (!ext_dmabuf) {
3790 rc = -ENOMEM;
3791 goto job_error;
3792 }
3793 list_add_tail(&ext_dmabuf->list,
3794 &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
3795 }
3796 }
3797
3798 /* bsg tracking structure */
3799 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
3800 if (!dd_data) {
3801 rc = -ENOMEM;
3802 goto job_error;
3803 }
3804
3805 /* mailbox command structure for base driver */
3806 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3807 if (!pmboxq) {
3808 rc = -ENOMEM;
3809 goto job_error;
3810 }
3811 memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
3812
3813 /* for the first external buffer */
3814 lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, 0, dmabuf, dmabuf);
3815
3816 /* for the rest of external buffer descriptors if any */
3817 if (ext_buf_cnt > 1) {
3818 ext_buf_index = 1;
3819 list_for_each_entry_safe(curr_dmabuf, next_dmabuf,
3820 &phba->mbox_ext_buf_ctx.ext_dmabuf_list, list) {
3821 lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp,
3822 ext_buf_index, dmabuf,
3823 curr_dmabuf);
3824 ext_buf_index++;
3825 }
3826 }
3827
James Smartb76f2dc2011-07-22 18:37:42 -04003828 /* after dma descriptor setup */
3829 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_rd, dma_mbox,
3830 sta_pos_addr, dmabuf, ext_buf_cnt);
3831
James Smart7ad20aa2011-05-24 11:44:28 -04003832 /* construct base driver mbox command */
3833 pmb = &pmboxq->u.mb;
3834 pmbx = (uint8_t *)dmabuf->virt;
3835 memcpy(pmb, pmbx, sizeof(*pmb));
3836 pmb->mbxOwner = OWN_HOST;
3837 pmboxq->vport = phba->pport;
3838
3839 /* multi-buffer handling context */
3840 phba->mbox_ext_buf_ctx.nembType = nemb_tp;
3841 phba->mbox_ext_buf_ctx.mboxType = mbox_rd;
3842 phba->mbox_ext_buf_ctx.numBuf = ext_buf_cnt;
3843 phba->mbox_ext_buf_ctx.mbxTag = mbox_req->extMboxTag;
3844 phba->mbox_ext_buf_ctx.seqNum = mbox_req->extSeqNum;
3845 phba->mbox_ext_buf_ctx.mbx_dmabuf = dmabuf;
3846
3847 /* callback for multi-buffer read mailbox command */
3848 pmboxq->mbox_cmpl = lpfc_bsg_issue_read_mbox_ext_cmpl;
3849
3850 /* context fields to callback function */
3851 pmboxq->context1 = dd_data;
3852 dd_data->type = TYPE_MBOX;
James Smarta33c4f72013-03-01 16:36:00 -05003853 dd_data->set_job = job;
James Smart7ad20aa2011-05-24 11:44:28 -04003854 dd_data->context_un.mbox.pmboxq = pmboxq;
3855 dd_data->context_un.mbox.mb = (MAILBOX_t *)pmbx;
James Smart7ad20aa2011-05-24 11:44:28 -04003856 job->dd_data = dd_data;
3857
3858 /* state change */
3859 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
3860
James Smart026abb82011-12-13 13:20:45 -05003861 /*
3862 * Non-embedded mailbox subcommand data gets byte swapped here because
3863 * the lower level driver code only does the first 64 mailbox words.
3864 */
3865 if ((!bsg_bf_get(lpfc_mbox_hdr_emb,
3866 &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) &&
3867 (nemb_tp == nemb_mse))
3868 lpfc_sli_pcimem_bcopy(&pmbx[sizeof(MAILBOX_t)],
3869 &pmbx[sizeof(MAILBOX_t)],
3870 sli_cfg_mbx->un.sli_config_emb0_subsys.
3871 mse[0].buf_len);
3872
James Smart7ad20aa2011-05-24 11:44:28 -04003873 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
3874 if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
3875 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3876 "2947 Issued SLI_CONFIG ext-buffer "
3877 "maibox command, rc:x%x\n", rc);
James Smart88a2cfb2011-07-22 18:36:33 -04003878 return SLI_CONFIG_HANDLED;
James Smart7ad20aa2011-05-24 11:44:28 -04003879 }
3880 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3881 "2948 Failed to issue SLI_CONFIG ext-buffer "
3882 "maibox command, rc:x%x\n", rc);
3883 rc = -EPIPE;
3884
3885job_error:
3886 if (pmboxq)
3887 mempool_free(pmboxq, phba->mbox_mem_pool);
3888 lpfc_bsg_dma_page_list_free(phba,
3889 &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
3890 kfree(dd_data);
3891 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_IDLE;
3892 return rc;
3893}
3894
3895/**
3896 * lpfc_bsg_sli_cfg_write_cmd_ext - sli_config non-embedded mailbox cmd write
3897 * @phba: Pointer to HBA context object.
3898 * @mb: Pointer to a BSG mailbox object.
3899 * @dmabuff: Pointer to a DMA buffer descriptor.
3900 *
3901 * This routine performs SLI_CONFIG (0x9B) write mailbox command operation with
3902 * non-embedded external bufffers.
3903 **/
3904static int
3905lpfc_bsg_sli_cfg_write_cmd_ext(struct lpfc_hba *phba, struct fc_bsg_job *job,
3906 enum nemb_type nemb_tp,
3907 struct lpfc_dmabuf *dmabuf)
3908{
3909 struct dfc_mbox_req *mbox_req;
3910 struct lpfc_sli_config_mbox *sli_cfg_mbx;
3911 uint32_t ext_buf_cnt;
3912 struct bsg_job_data *dd_data = NULL;
3913 LPFC_MBOXQ_t *pmboxq = NULL;
3914 MAILBOX_t *pmb;
3915 uint8_t *mbx;
James Smart88a2cfb2011-07-22 18:36:33 -04003916 int rc = SLI_CONFIG_NOT_HANDLED, i;
James Smart7ad20aa2011-05-24 11:44:28 -04003917
3918 mbox_req =
3919 (struct dfc_mbox_req *)job->request->rqst_data.h_vendor.vendor_cmd;
3920
3921 /* pointer to the start of mailbox command */
3922 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
3923
3924 if (nemb_tp == nemb_mse) {
3925 ext_buf_cnt = bsg_bf_get(lpfc_mbox_hdr_mse_cnt,
3926 &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr);
3927 if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_MSE) {
3928 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
James Smart026abb82011-12-13 13:20:45 -05003929 "2953 Failed SLI_CONFIG(mse) wr, "
James Smart7ad20aa2011-05-24 11:44:28 -04003930 "ext_buf_cnt(%d) out of range(%d)\n",
3931 ext_buf_cnt,
3932 LPFC_MBX_SLI_CONFIG_MAX_MSE);
3933 return -ERANGE;
3934 }
3935 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3936 "2949 Handled SLI_CONFIG(mse) wr, "
3937 "ext_buf_cnt:%d\n", ext_buf_cnt);
3938 } else {
3939 /* sanity check on interface type for support */
3940 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
3941 LPFC_SLI_INTF_IF_TYPE_2)
3942 return -ENODEV;
3943 /* nemb_tp == nemb_hbd */
3944 ext_buf_cnt = sli_cfg_mbx->un.sli_config_emb1_subsys.hbd_count;
3945 if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_HBD) {
3946 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
James Smart026abb82011-12-13 13:20:45 -05003947 "2954 Failed SLI_CONFIG(hbd) wr, "
James Smart7ad20aa2011-05-24 11:44:28 -04003948 "ext_buf_cnt(%d) out of range(%d)\n",
3949 ext_buf_cnt,
3950 LPFC_MBX_SLI_CONFIG_MAX_HBD);
3951 return -ERANGE;
3952 }
3953 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3954 "2950 Handled SLI_CONFIG(hbd) wr, "
3955 "ext_buf_cnt:%d\n", ext_buf_cnt);
3956 }
3957
James Smartb76f2dc2011-07-22 18:37:42 -04003958 /* before dma buffer descriptor setup */
3959 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_wr, dma_mbox,
3960 sta_pre_addr, dmabuf, ext_buf_cnt);
3961
James Smart7ad20aa2011-05-24 11:44:28 -04003962 if (ext_buf_cnt == 0)
3963 return -EPERM;
3964
3965 /* for the first external buffer */
3966 lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, 0, dmabuf, dmabuf);
3967
James Smartb76f2dc2011-07-22 18:37:42 -04003968 /* after dma descriptor setup */
3969 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_wr, dma_mbox,
3970 sta_pos_addr, dmabuf, ext_buf_cnt);
3971
James Smart7ad20aa2011-05-24 11:44:28 -04003972 /* log for looking forward */
3973 for (i = 1; i < ext_buf_cnt; i++) {
3974 if (nemb_tp == nemb_mse)
3975 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3976 "2951 SLI_CONFIG(mse), buf[%d]-length:%d\n",
3977 i, sli_cfg_mbx->un.sli_config_emb0_subsys.
3978 mse[i].buf_len);
3979 else
3980 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3981 "2952 SLI_CONFIG(hbd), buf[%d]-length:%d\n",
3982 i, bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
3983 &sli_cfg_mbx->un.sli_config_emb1_subsys.
3984 hbd[i]));
3985 }
3986
3987 /* multi-buffer handling context */
3988 phba->mbox_ext_buf_ctx.nembType = nemb_tp;
3989 phba->mbox_ext_buf_ctx.mboxType = mbox_wr;
3990 phba->mbox_ext_buf_ctx.numBuf = ext_buf_cnt;
3991 phba->mbox_ext_buf_ctx.mbxTag = mbox_req->extMboxTag;
3992 phba->mbox_ext_buf_ctx.seqNum = mbox_req->extSeqNum;
3993 phba->mbox_ext_buf_ctx.mbx_dmabuf = dmabuf;
3994
3995 if (ext_buf_cnt == 1) {
3996 /* bsg tracking structure */
3997 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
3998 if (!dd_data) {
3999 rc = -ENOMEM;
4000 goto job_error;
4001 }
4002
4003 /* mailbox command structure for base driver */
4004 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4005 if (!pmboxq) {
4006 rc = -ENOMEM;
4007 goto job_error;
4008 }
4009 memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
4010 pmb = &pmboxq->u.mb;
4011 mbx = (uint8_t *)dmabuf->virt;
4012 memcpy(pmb, mbx, sizeof(*pmb));
4013 pmb->mbxOwner = OWN_HOST;
4014 pmboxq->vport = phba->pport;
4015
4016 /* callback for multi-buffer read mailbox command */
4017 pmboxq->mbox_cmpl = lpfc_bsg_issue_write_mbox_ext_cmpl;
4018
4019 /* context fields to callback function */
4020 pmboxq->context1 = dd_data;
4021 dd_data->type = TYPE_MBOX;
James Smarta33c4f72013-03-01 16:36:00 -05004022 dd_data->set_job = job;
James Smart7ad20aa2011-05-24 11:44:28 -04004023 dd_data->context_un.mbox.pmboxq = pmboxq;
4024 dd_data->context_un.mbox.mb = (MAILBOX_t *)mbx;
James Smart7ad20aa2011-05-24 11:44:28 -04004025 job->dd_data = dd_data;
4026
4027 /* state change */
James Smart7ad20aa2011-05-24 11:44:28 -04004028
James Smarta33c4f72013-03-01 16:36:00 -05004029 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
James Smart7ad20aa2011-05-24 11:44:28 -04004030 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
4031 if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
4032 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4033 "2955 Issued SLI_CONFIG ext-buffer "
4034 "maibox command, rc:x%x\n", rc);
James Smart88a2cfb2011-07-22 18:36:33 -04004035 return SLI_CONFIG_HANDLED;
James Smart7ad20aa2011-05-24 11:44:28 -04004036 }
4037 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4038 "2956 Failed to issue SLI_CONFIG ext-buffer "
4039 "maibox command, rc:x%x\n", rc);
4040 rc = -EPIPE;
James Smart026abb82011-12-13 13:20:45 -05004041 goto job_error;
James Smart7ad20aa2011-05-24 11:44:28 -04004042 }
4043
James Smart88a2cfb2011-07-22 18:36:33 -04004044 /* wait for additoinal external buffers */
James Smarta33c4f72013-03-01 16:36:00 -05004045
James Smart88a2cfb2011-07-22 18:36:33 -04004046 job->reply->result = 0;
4047 job->job_done(job);
4048 return SLI_CONFIG_HANDLED;
4049
James Smart7ad20aa2011-05-24 11:44:28 -04004050job_error:
4051 if (pmboxq)
4052 mempool_free(pmboxq, phba->mbox_mem_pool);
4053 kfree(dd_data);
4054
4055 return rc;
4056}
4057
4058/**
4059 * lpfc_bsg_handle_sli_cfg_mbox - handle sli-cfg mailbox cmd with ext buffer
4060 * @phba: Pointer to HBA context object.
4061 * @mb: Pointer to a BSG mailbox object.
4062 * @dmabuff: Pointer to a DMA buffer descriptor.
4063 *
4064 * This routine handles SLI_CONFIG (0x9B) mailbox command with non-embedded
4065 * external bufffers, including both 0x9B with non-embedded MSEs and 0x9B
4066 * with embedded sussystem 0x1 and opcodes with external HBDs.
4067 **/
4068static int
4069lpfc_bsg_handle_sli_cfg_mbox(struct lpfc_hba *phba, struct fc_bsg_job *job,
4070 struct lpfc_dmabuf *dmabuf)
4071{
4072 struct lpfc_sli_config_mbox *sli_cfg_mbx;
4073 uint32_t subsys;
4074 uint32_t opcode;
4075 int rc = SLI_CONFIG_NOT_HANDLED;
4076
James Smart026abb82011-12-13 13:20:45 -05004077 /* state change on new multi-buffer pass-through mailbox command */
James Smart7ad20aa2011-05-24 11:44:28 -04004078 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_HOST;
4079
4080 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
4081
4082 if (!bsg_bf_get(lpfc_mbox_hdr_emb,
4083 &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) {
4084 subsys = bsg_bf_get(lpfc_emb0_subcmnd_subsys,
4085 &sli_cfg_mbx->un.sli_config_emb0_subsys);
4086 opcode = bsg_bf_get(lpfc_emb0_subcmnd_opcode,
4087 &sli_cfg_mbx->un.sli_config_emb0_subsys);
4088 if (subsys == SLI_CONFIG_SUBSYS_FCOE) {
4089 switch (opcode) {
4090 case FCOE_OPCODE_READ_FCF:
4091 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4092 "2957 Handled SLI_CONFIG "
4093 "subsys_fcoe, opcode:x%x\n",
4094 opcode);
4095 rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
4096 nemb_mse, dmabuf);
4097 break;
4098 case FCOE_OPCODE_ADD_FCF:
4099 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4100 "2958 Handled SLI_CONFIG "
4101 "subsys_fcoe, opcode:x%x\n",
4102 opcode);
4103 rc = lpfc_bsg_sli_cfg_write_cmd_ext(phba, job,
4104 nemb_mse, dmabuf);
4105 break;
4106 default:
4107 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
James Smart026abb82011-12-13 13:20:45 -05004108 "2959 Reject SLI_CONFIG "
James Smart7ad20aa2011-05-24 11:44:28 -04004109 "subsys_fcoe, opcode:x%x\n",
4110 opcode);
James Smart026abb82011-12-13 13:20:45 -05004111 rc = -EPERM;
4112 break;
4113 }
4114 } else if (subsys == SLI_CONFIG_SUBSYS_COMN) {
4115 switch (opcode) {
4116 case COMN_OPCODE_GET_CNTL_ADDL_ATTRIBUTES:
James Smartb99570d2012-05-09 21:16:24 -04004117 case COMN_OPCODE_GET_CNTL_ATTRIBUTES:
James Smart026abb82011-12-13 13:20:45 -05004118 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4119 "3106 Handled SLI_CONFIG "
James Smart6b5151f2012-01-18 16:24:06 -05004120 "subsys_comn, opcode:x%x\n",
James Smart026abb82011-12-13 13:20:45 -05004121 opcode);
4122 rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
4123 nemb_mse, dmabuf);
4124 break;
4125 default:
4126 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4127 "3107 Reject SLI_CONFIG "
James Smart6b5151f2012-01-18 16:24:06 -05004128 "subsys_comn, opcode:x%x\n",
James Smart026abb82011-12-13 13:20:45 -05004129 opcode);
4130 rc = -EPERM;
James Smart7ad20aa2011-05-24 11:44:28 -04004131 break;
4132 }
4133 } else {
4134 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
James Smart026abb82011-12-13 13:20:45 -05004135 "2977 Reject SLI_CONFIG "
James Smart7ad20aa2011-05-24 11:44:28 -04004136 "subsys:x%d, opcode:x%x\n",
4137 subsys, opcode);
James Smart026abb82011-12-13 13:20:45 -05004138 rc = -EPERM;
James Smart7ad20aa2011-05-24 11:44:28 -04004139 }
4140 } else {
4141 subsys = bsg_bf_get(lpfc_emb1_subcmnd_subsys,
4142 &sli_cfg_mbx->un.sli_config_emb1_subsys);
4143 opcode = bsg_bf_get(lpfc_emb1_subcmnd_opcode,
4144 &sli_cfg_mbx->un.sli_config_emb1_subsys);
4145 if (subsys == SLI_CONFIG_SUBSYS_COMN) {
4146 switch (opcode) {
4147 case COMN_OPCODE_READ_OBJECT:
4148 case COMN_OPCODE_READ_OBJECT_LIST:
4149 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4150 "2960 Handled SLI_CONFIG "
4151 "subsys_comn, opcode:x%x\n",
4152 opcode);
4153 rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
4154 nemb_hbd, dmabuf);
4155 break;
4156 case COMN_OPCODE_WRITE_OBJECT:
4157 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4158 "2961 Handled SLI_CONFIG "
4159 "subsys_comn, opcode:x%x\n",
4160 opcode);
4161 rc = lpfc_bsg_sli_cfg_write_cmd_ext(phba, job,
4162 nemb_hbd, dmabuf);
4163 break;
4164 default:
4165 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4166 "2962 Not handled SLI_CONFIG "
4167 "subsys_comn, opcode:x%x\n",
4168 opcode);
4169 rc = SLI_CONFIG_NOT_HANDLED;
4170 break;
4171 }
4172 } else {
4173 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
James Smart026abb82011-12-13 13:20:45 -05004174 "2978 Not handled SLI_CONFIG "
James Smart7ad20aa2011-05-24 11:44:28 -04004175 "subsys:x%d, opcode:x%x\n",
4176 subsys, opcode);
4177 rc = SLI_CONFIG_NOT_HANDLED;
4178 }
4179 }
James Smart026abb82011-12-13 13:20:45 -05004180
4181 /* state reset on not handled new multi-buffer mailbox command */
4182 if (rc != SLI_CONFIG_HANDLED)
4183 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_IDLE;
4184
James Smart7ad20aa2011-05-24 11:44:28 -04004185 return rc;
4186}
4187
4188/**
4189 * lpfc_bsg_mbox_ext_abort_req - request to abort mbox command with ext buffers
4190 * @phba: Pointer to HBA context object.
4191 *
4192 * This routine is for requesting to abort a pass-through mailbox command with
4193 * multiple external buffers due to error condition.
4194 **/
4195static void
4196lpfc_bsg_mbox_ext_abort(struct lpfc_hba *phba)
4197{
4198 if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_PORT)
4199 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_ABTS;
4200 else
4201 lpfc_bsg_mbox_ext_session_reset(phba);
4202 return;
4203}
4204
4205/**
4206 * lpfc_bsg_read_ebuf_get - get the next mailbox read external buffer
4207 * @phba: Pointer to HBA context object.
4208 * @dmabuf: Pointer to a DMA buffer descriptor.
4209 *
4210 * This routine extracts the next mailbox read external buffer back to
4211 * user space through BSG.
4212 **/
4213static int
4214lpfc_bsg_read_ebuf_get(struct lpfc_hba *phba, struct fc_bsg_job *job)
4215{
4216 struct lpfc_sli_config_mbox *sli_cfg_mbx;
4217 struct lpfc_dmabuf *dmabuf;
4218 uint8_t *pbuf;
4219 uint32_t size;
4220 uint32_t index;
4221
4222 index = phba->mbox_ext_buf_ctx.seqNum;
4223 phba->mbox_ext_buf_ctx.seqNum++;
4224
4225 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)
4226 phba->mbox_ext_buf_ctx.mbx_dmabuf->virt;
4227
4228 if (phba->mbox_ext_buf_ctx.nembType == nemb_mse) {
4229 size = bsg_bf_get(lpfc_mbox_sli_config_mse_len,
4230 &sli_cfg_mbx->un.sli_config_emb0_subsys.mse[index]);
4231 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4232 "2963 SLI_CONFIG (mse) ext-buffer rd get "
4233 "buffer[%d], size:%d\n", index, size);
4234 } else {
4235 size = bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
4236 &sli_cfg_mbx->un.sli_config_emb1_subsys.hbd[index]);
4237 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4238 "2964 SLI_CONFIG (hbd) ext-buffer rd get "
4239 "buffer[%d], size:%d\n", index, size);
4240 }
4241 if (list_empty(&phba->mbox_ext_buf_ctx.ext_dmabuf_list))
4242 return -EPIPE;
4243 dmabuf = list_first_entry(&phba->mbox_ext_buf_ctx.ext_dmabuf_list,
4244 struct lpfc_dmabuf, list);
4245 list_del_init(&dmabuf->list);
James Smartb76f2dc2011-07-22 18:37:42 -04004246
4247 /* after dma buffer descriptor setup */
4248 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, phba->mbox_ext_buf_ctx.nembType,
4249 mbox_rd, dma_ebuf, sta_pos_addr,
4250 dmabuf, index);
4251
James Smart7ad20aa2011-05-24 11:44:28 -04004252 pbuf = (uint8_t *)dmabuf->virt;
4253 job->reply->reply_payload_rcv_len =
4254 sg_copy_from_buffer(job->reply_payload.sg_list,
4255 job->reply_payload.sg_cnt,
4256 pbuf, size);
4257
4258 lpfc_bsg_dma_page_free(phba, dmabuf);
4259
4260 if (phba->mbox_ext_buf_ctx.seqNum == phba->mbox_ext_buf_ctx.numBuf) {
4261 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4262 "2965 SLI_CONFIG (hbd) ext-buffer rd mbox "
4263 "command session done\n");
4264 lpfc_bsg_mbox_ext_session_reset(phba);
4265 }
4266
4267 job->reply->result = 0;
4268 job->job_done(job);
4269
4270 return SLI_CONFIG_HANDLED;
4271}
4272
4273/**
4274 * lpfc_bsg_write_ebuf_set - set the next mailbox write external buffer
4275 * @phba: Pointer to HBA context object.
4276 * @dmabuf: Pointer to a DMA buffer descriptor.
4277 *
4278 * This routine sets up the next mailbox read external buffer obtained
4279 * from user space through BSG.
4280 **/
4281static int
4282lpfc_bsg_write_ebuf_set(struct lpfc_hba *phba, struct fc_bsg_job *job,
4283 struct lpfc_dmabuf *dmabuf)
4284{
4285 struct lpfc_sli_config_mbox *sli_cfg_mbx;
4286 struct bsg_job_data *dd_data = NULL;
4287 LPFC_MBOXQ_t *pmboxq = NULL;
4288 MAILBOX_t *pmb;
4289 enum nemb_type nemb_tp;
4290 uint8_t *pbuf;
4291 uint32_t size;
4292 uint32_t index;
4293 int rc;
4294
4295 index = phba->mbox_ext_buf_ctx.seqNum;
4296 phba->mbox_ext_buf_ctx.seqNum++;
4297 nemb_tp = phba->mbox_ext_buf_ctx.nembType;
4298
4299 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)
4300 phba->mbox_ext_buf_ctx.mbx_dmabuf->virt;
4301
4302 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
4303 if (!dd_data) {
4304 rc = -ENOMEM;
4305 goto job_error;
4306 }
4307
4308 pbuf = (uint8_t *)dmabuf->virt;
4309 size = job->request_payload.payload_len;
4310 sg_copy_to_buffer(job->request_payload.sg_list,
4311 job->request_payload.sg_cnt,
4312 pbuf, size);
4313
4314 if (phba->mbox_ext_buf_ctx.nembType == nemb_mse) {
4315 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4316 "2966 SLI_CONFIG (mse) ext-buffer wr set "
4317 "buffer[%d], size:%d\n",
4318 phba->mbox_ext_buf_ctx.seqNum, size);
4319
4320 } else {
4321 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4322 "2967 SLI_CONFIG (hbd) ext-buffer wr set "
4323 "buffer[%d], size:%d\n",
4324 phba->mbox_ext_buf_ctx.seqNum, size);
4325
4326 }
4327
4328 /* set up external buffer descriptor and add to external buffer list */
4329 lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, index,
4330 phba->mbox_ext_buf_ctx.mbx_dmabuf,
4331 dmabuf);
4332 list_add_tail(&dmabuf->list, &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
4333
James Smartb76f2dc2011-07-22 18:37:42 -04004334 /* after write dma buffer */
4335 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, phba->mbox_ext_buf_ctx.nembType,
4336 mbox_wr, dma_ebuf, sta_pos_addr,
4337 dmabuf, index);
4338
James Smart7ad20aa2011-05-24 11:44:28 -04004339 if (phba->mbox_ext_buf_ctx.seqNum == phba->mbox_ext_buf_ctx.numBuf) {
4340 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4341 "2968 SLI_CONFIG ext-buffer wr all %d "
4342 "ebuffers received\n",
4343 phba->mbox_ext_buf_ctx.numBuf);
4344 /* mailbox command structure for base driver */
4345 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4346 if (!pmboxq) {
4347 rc = -ENOMEM;
4348 goto job_error;
4349 }
4350 memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
4351 pbuf = (uint8_t *)phba->mbox_ext_buf_ctx.mbx_dmabuf->virt;
4352 pmb = &pmboxq->u.mb;
4353 memcpy(pmb, pbuf, sizeof(*pmb));
4354 pmb->mbxOwner = OWN_HOST;
4355 pmboxq->vport = phba->pport;
4356
4357 /* callback for multi-buffer write mailbox command */
4358 pmboxq->mbox_cmpl = lpfc_bsg_issue_write_mbox_ext_cmpl;
4359
4360 /* context fields to callback function */
4361 pmboxq->context1 = dd_data;
4362 dd_data->type = TYPE_MBOX;
James Smarta33c4f72013-03-01 16:36:00 -05004363 dd_data->set_job = job;
James Smart7ad20aa2011-05-24 11:44:28 -04004364 dd_data->context_un.mbox.pmboxq = pmboxq;
4365 dd_data->context_un.mbox.mb = (MAILBOX_t *)pbuf;
James Smart7ad20aa2011-05-24 11:44:28 -04004366 job->dd_data = dd_data;
4367
4368 /* state change */
4369 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
4370
4371 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
4372 if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
4373 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4374 "2969 Issued SLI_CONFIG ext-buffer "
4375 "maibox command, rc:x%x\n", rc);
James Smart88a2cfb2011-07-22 18:36:33 -04004376 return SLI_CONFIG_HANDLED;
James Smart7ad20aa2011-05-24 11:44:28 -04004377 }
4378 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4379 "2970 Failed to issue SLI_CONFIG ext-buffer "
4380 "maibox command, rc:x%x\n", rc);
4381 rc = -EPIPE;
4382 goto job_error;
4383 }
4384
4385 /* wait for additoinal external buffers */
4386 job->reply->result = 0;
4387 job->job_done(job);
4388 return SLI_CONFIG_HANDLED;
4389
4390job_error:
4391 lpfc_bsg_dma_page_free(phba, dmabuf);
4392 kfree(dd_data);
4393
4394 return rc;
4395}
4396
4397/**
4398 * lpfc_bsg_handle_sli_cfg_ebuf - handle ext buffer with sli-cfg mailbox cmd
4399 * @phba: Pointer to HBA context object.
4400 * @mb: Pointer to a BSG mailbox object.
4401 * @dmabuff: Pointer to a DMA buffer descriptor.
4402 *
4403 * This routine handles the external buffer with SLI_CONFIG (0x9B) mailbox
4404 * command with multiple non-embedded external buffers.
4405 **/
4406static int
4407lpfc_bsg_handle_sli_cfg_ebuf(struct lpfc_hba *phba, struct fc_bsg_job *job,
4408 struct lpfc_dmabuf *dmabuf)
4409{
4410 int rc;
4411
4412 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4413 "2971 SLI_CONFIG buffer (type:x%x)\n",
4414 phba->mbox_ext_buf_ctx.mboxType);
4415
4416 if (phba->mbox_ext_buf_ctx.mboxType == mbox_rd) {
4417 if (phba->mbox_ext_buf_ctx.state != LPFC_BSG_MBOX_DONE) {
4418 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4419 "2972 SLI_CONFIG rd buffer state "
4420 "mismatch:x%x\n",
4421 phba->mbox_ext_buf_ctx.state);
4422 lpfc_bsg_mbox_ext_abort(phba);
4423 return -EPIPE;
4424 }
4425 rc = lpfc_bsg_read_ebuf_get(phba, job);
4426 if (rc == SLI_CONFIG_HANDLED)
4427 lpfc_bsg_dma_page_free(phba, dmabuf);
4428 } else { /* phba->mbox_ext_buf_ctx.mboxType == mbox_wr */
4429 if (phba->mbox_ext_buf_ctx.state != LPFC_BSG_MBOX_HOST) {
4430 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4431 "2973 SLI_CONFIG wr buffer state "
4432 "mismatch:x%x\n",
4433 phba->mbox_ext_buf_ctx.state);
4434 lpfc_bsg_mbox_ext_abort(phba);
4435 return -EPIPE;
4436 }
4437 rc = lpfc_bsg_write_ebuf_set(phba, job, dmabuf);
4438 }
4439 return rc;
4440}
4441
4442/**
4443 * lpfc_bsg_handle_sli_cfg_ext - handle sli-cfg mailbox with external buffer
4444 * @phba: Pointer to HBA context object.
4445 * @mb: Pointer to a BSG mailbox object.
4446 * @dmabuff: Pointer to a DMA buffer descriptor.
4447 *
4448 * This routine checkes and handles non-embedded multi-buffer SLI_CONFIG
4449 * (0x9B) mailbox commands and external buffers.
4450 **/
4451static int
4452lpfc_bsg_handle_sli_cfg_ext(struct lpfc_hba *phba, struct fc_bsg_job *job,
4453 struct lpfc_dmabuf *dmabuf)
4454{
4455 struct dfc_mbox_req *mbox_req;
James Smart88a2cfb2011-07-22 18:36:33 -04004456 int rc = SLI_CONFIG_NOT_HANDLED;
James Smart7ad20aa2011-05-24 11:44:28 -04004457
4458 mbox_req =
4459 (struct dfc_mbox_req *)job->request->rqst_data.h_vendor.vendor_cmd;
4460
4461 /* mbox command with/without single external buffer */
4462 if (mbox_req->extMboxTag == 0 && mbox_req->extSeqNum == 0)
James Smart88a2cfb2011-07-22 18:36:33 -04004463 return rc;
James Smart7ad20aa2011-05-24 11:44:28 -04004464
4465 /* mbox command and first external buffer */
4466 if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_IDLE) {
4467 if (mbox_req->extSeqNum == 1) {
4468 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4469 "2974 SLI_CONFIG mailbox: tag:%d, "
4470 "seq:%d\n", mbox_req->extMboxTag,
4471 mbox_req->extSeqNum);
4472 rc = lpfc_bsg_handle_sli_cfg_mbox(phba, job, dmabuf);
4473 return rc;
4474 } else
4475 goto sli_cfg_ext_error;
4476 }
4477
4478 /*
4479 * handle additional external buffers
4480 */
4481
4482 /* check broken pipe conditions */
4483 if (mbox_req->extMboxTag != phba->mbox_ext_buf_ctx.mbxTag)
4484 goto sli_cfg_ext_error;
4485 if (mbox_req->extSeqNum > phba->mbox_ext_buf_ctx.numBuf)
4486 goto sli_cfg_ext_error;
4487 if (mbox_req->extSeqNum != phba->mbox_ext_buf_ctx.seqNum + 1)
4488 goto sli_cfg_ext_error;
4489
4490 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4491 "2975 SLI_CONFIG mailbox external buffer: "
4492 "extSta:x%x, tag:%d, seq:%d\n",
4493 phba->mbox_ext_buf_ctx.state, mbox_req->extMboxTag,
4494 mbox_req->extSeqNum);
4495 rc = lpfc_bsg_handle_sli_cfg_ebuf(phba, job, dmabuf);
4496 return rc;
4497
4498sli_cfg_ext_error:
4499 /* all other cases, broken pipe */
4500 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4501 "2976 SLI_CONFIG mailbox broken pipe: "
4502 "ctxSta:x%x, ctxNumBuf:%d "
4503 "ctxTag:%d, ctxSeq:%d, tag:%d, seq:%d\n",
4504 phba->mbox_ext_buf_ctx.state,
4505 phba->mbox_ext_buf_ctx.numBuf,
4506 phba->mbox_ext_buf_ctx.mbxTag,
4507 phba->mbox_ext_buf_ctx.seqNum,
4508 mbox_req->extMboxTag, mbox_req->extSeqNum);
4509
4510 lpfc_bsg_mbox_ext_session_reset(phba);
4511
4512 return -EPIPE;
4513}
4514
4515/**
James Smart3b5dd522010-01-26 23:10:15 -05004516 * lpfc_bsg_issue_mbox - issues a mailbox command on behalf of an app
4517 * @phba: Pointer to HBA context object.
4518 * @mb: Pointer to a mailbox object.
4519 * @vport: Pointer to a vport object.
4520 *
4521 * Allocate a tracking object, mailbox command memory, get a mailbox
4522 * from the mailbox pool, copy the caller mailbox command.
4523 *
4524 * If offline and the sli is active we need to poll for the command (port is
4525 * being reset) and com-plete the job, otherwise issue the mailbox command and
4526 * let our completion handler finish the command.
4527 **/
4528static uint32_t
4529lpfc_bsg_issue_mbox(struct lpfc_hba *phba, struct fc_bsg_job *job,
4530 struct lpfc_vport *vport)
4531{
James Smart7a470272010-03-15 11:25:20 -04004532 LPFC_MBOXQ_t *pmboxq = NULL; /* internal mailbox queue */
4533 MAILBOX_t *pmb; /* shortcut to the pmboxq mailbox */
4534 /* a 4k buffer to hold the mb and extended data from/to the bsg */
James Smart7ad20aa2011-05-24 11:44:28 -04004535 uint8_t *pmbx = NULL;
James Smart7a470272010-03-15 11:25:20 -04004536 struct bsg_job_data *dd_data = NULL; /* bsg data tracking structure */
James Smart7ad20aa2011-05-24 11:44:28 -04004537 struct lpfc_dmabuf *dmabuf = NULL;
4538 struct dfc_mbox_req *mbox_req;
James Smartb6e3b9c2011-04-16 11:03:43 -04004539 struct READ_EVENT_LOG_VAR *rdEventLog;
4540 uint32_t transmit_length, receive_length, mode;
James Smart7ad20aa2011-05-24 11:44:28 -04004541 struct lpfc_mbx_sli4_config *sli4_config;
James Smartb6e3b9c2011-04-16 11:03:43 -04004542 struct lpfc_mbx_nembed_cmd *nembed_sge;
4543 struct mbox_header *header;
4544 struct ulp_bde64 *bde;
James Smart7a470272010-03-15 11:25:20 -04004545 uint8_t *ext = NULL;
James Smart3b5dd522010-01-26 23:10:15 -05004546 int rc = 0;
James Smart7a470272010-03-15 11:25:20 -04004547 uint8_t *from;
James Smart7ad20aa2011-05-24 11:44:28 -04004548 uint32_t size;
4549
James Smart7a470272010-03-15 11:25:20 -04004550 /* in case no data is transferred */
4551 job->reply->reply_payload_rcv_len = 0;
4552
James Smartb6e3b9c2011-04-16 11:03:43 -04004553 /* sanity check to protect driver */
4554 if (job->reply_payload.payload_len > BSG_MBOX_SIZE ||
4555 job->request_payload.payload_len > BSG_MBOX_SIZE) {
4556 rc = -ERANGE;
4557 goto job_done;
4558 }
4559
James Smart7ad20aa2011-05-24 11:44:28 -04004560 /*
4561 * Don't allow mailbox commands to be sent when blocked or when in
4562 * the middle of discovery
4563 */
4564 if (phba->sli.sli_flag & LPFC_BLOCK_MGMT_IO) {
4565 rc = -EAGAIN;
4566 goto job_done;
4567 }
4568
4569 mbox_req =
4570 (struct dfc_mbox_req *)job->request->rqst_data.h_vendor.vendor_cmd;
4571
James Smart7a470272010-03-15 11:25:20 -04004572 /* check if requested extended data lengths are valid */
James Smartb6e3b9c2011-04-16 11:03:43 -04004573 if ((mbox_req->inExtWLen > BSG_MBOX_SIZE/sizeof(uint32_t)) ||
4574 (mbox_req->outExtWLen > BSG_MBOX_SIZE/sizeof(uint32_t))) {
James Smart7a470272010-03-15 11:25:20 -04004575 rc = -ERANGE;
4576 goto job_done;
4577 }
James Smart3b5dd522010-01-26 23:10:15 -05004578
James Smart7ad20aa2011-05-24 11:44:28 -04004579 dmabuf = lpfc_bsg_dma_page_alloc(phba);
4580 if (!dmabuf || !dmabuf->virt) {
4581 rc = -ENOMEM;
4582 goto job_done;
4583 }
4584
4585 /* Get the mailbox command or external buffer from BSG */
4586 pmbx = (uint8_t *)dmabuf->virt;
4587 size = job->request_payload.payload_len;
4588 sg_copy_to_buffer(job->request_payload.sg_list,
4589 job->request_payload.sg_cnt, pmbx, size);
4590
4591 /* Handle possible SLI_CONFIG with non-embedded payloads */
4592 if (phba->sli_rev == LPFC_SLI_REV4) {
4593 rc = lpfc_bsg_handle_sli_cfg_ext(phba, job, dmabuf);
4594 if (rc == SLI_CONFIG_HANDLED)
4595 goto job_cont;
4596 if (rc)
4597 goto job_done;
4598 /* SLI_CONFIG_NOT_HANDLED for other mailbox commands */
4599 }
4600
4601 rc = lpfc_bsg_check_cmd_access(phba, (MAILBOX_t *)pmbx, vport);
4602 if (rc != 0)
4603 goto job_done; /* must be negative */
4604
James Smart3b5dd522010-01-26 23:10:15 -05004605 /* allocate our bsg tracking structure */
4606 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
4607 if (!dd_data) {
4608 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
4609 "2727 Failed allocation of dd_data\n");
James Smart7a470272010-03-15 11:25:20 -04004610 rc = -ENOMEM;
4611 goto job_done;
James Smart3b5dd522010-01-26 23:10:15 -05004612 }
4613
James Smart3b5dd522010-01-26 23:10:15 -05004614 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4615 if (!pmboxq) {
James Smart7a470272010-03-15 11:25:20 -04004616 rc = -ENOMEM;
4617 goto job_done;
James Smart3b5dd522010-01-26 23:10:15 -05004618 }
James Smart7a470272010-03-15 11:25:20 -04004619 memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
James Smart3b5dd522010-01-26 23:10:15 -05004620
James Smart3b5dd522010-01-26 23:10:15 -05004621 pmb = &pmboxq->u.mb;
James Smart7ad20aa2011-05-24 11:44:28 -04004622 memcpy(pmb, pmbx, sizeof(*pmb));
James Smart3b5dd522010-01-26 23:10:15 -05004623 pmb->mbxOwner = OWN_HOST;
James Smart3b5dd522010-01-26 23:10:15 -05004624 pmboxq->vport = vport;
4625
James Smartc7495932010-04-06 15:05:28 -04004626 /* If HBA encountered an error attention, allow only DUMP
4627 * or RESTART mailbox commands until the HBA is restarted.
4628 */
4629 if (phba->pport->stopped &&
4630 pmb->mbxCommand != MBX_DUMP_MEMORY &&
4631 pmb->mbxCommand != MBX_RESTART &&
4632 pmb->mbxCommand != MBX_WRITE_VPARMS &&
4633 pmb->mbxCommand != MBX_WRITE_WWN)
4634 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
4635 "2797 mbox: Issued mailbox cmd "
4636 "0x%x while in stopped state.\n",
4637 pmb->mbxCommand);
4638
James Smart7a470272010-03-15 11:25:20 -04004639 /* extended mailbox commands will need an extended buffer */
James Smartc7495932010-04-06 15:05:28 -04004640 if (mbox_req->inExtWLen || mbox_req->outExtWLen) {
James Smart026abb82011-12-13 13:20:45 -05004641 from = pmbx;
4642 ext = from + sizeof(MAILBOX_t);
James Smart7a470272010-03-15 11:25:20 -04004643 pmboxq->context2 = ext;
4644 pmboxq->in_ext_byte_len =
James Smart7a470272010-03-15 11:25:20 -04004645 mbox_req->inExtWLen * sizeof(uint32_t);
4646 pmboxq->out_ext_byte_len =
James Smartc7495932010-04-06 15:05:28 -04004647 mbox_req->outExtWLen * sizeof(uint32_t);
James Smart7a470272010-03-15 11:25:20 -04004648 pmboxq->mbox_offset_word = mbox_req->mbOffset;
4649 }
4650
4651 /* biu diag will need a kernel buffer to transfer the data
4652 * allocate our own buffer and setup the mailbox command to
4653 * use ours
4654 */
4655 if (pmb->mbxCommand == MBX_RUN_BIU_DIAG64) {
James Smartb6e3b9c2011-04-16 11:03:43 -04004656 transmit_length = pmb->un.varWords[1];
4657 receive_length = pmb->un.varWords[4];
James Smartc7495932010-04-06 15:05:28 -04004658 /* transmit length cannot be greater than receive length or
4659 * mailbox extension size
4660 */
4661 if ((transmit_length > receive_length) ||
James Smart88a2cfb2011-07-22 18:36:33 -04004662 (transmit_length > BSG_MBOX_SIZE - sizeof(MAILBOX_t))) {
James Smartc7495932010-04-06 15:05:28 -04004663 rc = -ERANGE;
4664 goto job_done;
4665 }
James Smart7a470272010-03-15 11:25:20 -04004666 pmb->un.varBIUdiag.un.s2.xmit_bde64.addrHigh =
James Smart7ad20aa2011-05-24 11:44:28 -04004667 putPaddrHigh(dmabuf->phys + sizeof(MAILBOX_t));
James Smart7a470272010-03-15 11:25:20 -04004668 pmb->un.varBIUdiag.un.s2.xmit_bde64.addrLow =
James Smart7ad20aa2011-05-24 11:44:28 -04004669 putPaddrLow(dmabuf->phys + sizeof(MAILBOX_t));
James Smart7a470272010-03-15 11:25:20 -04004670
4671 pmb->un.varBIUdiag.un.s2.rcv_bde64.addrHigh =
James Smart7ad20aa2011-05-24 11:44:28 -04004672 putPaddrHigh(dmabuf->phys + sizeof(MAILBOX_t)
4673 + pmb->un.varBIUdiag.un.s2.xmit_bde64.tus.f.bdeSize);
James Smart7a470272010-03-15 11:25:20 -04004674 pmb->un.varBIUdiag.un.s2.rcv_bde64.addrLow =
James Smart7ad20aa2011-05-24 11:44:28 -04004675 putPaddrLow(dmabuf->phys + sizeof(MAILBOX_t)
4676 + pmb->un.varBIUdiag.un.s2.xmit_bde64.tus.f.bdeSize);
James Smartc7495932010-04-06 15:05:28 -04004677 } else if (pmb->mbxCommand == MBX_READ_EVENT_LOG) {
James Smartb6e3b9c2011-04-16 11:03:43 -04004678 rdEventLog = &pmb->un.varRdEventLog;
4679 receive_length = rdEventLog->rcv_bde64.tus.f.bdeSize;
4680 mode = bf_get(lpfc_event_log, rdEventLog);
James Smartc7495932010-04-06 15:05:28 -04004681
4682 /* receive length cannot be greater than mailbox
4683 * extension size
4684 */
James Smart88a2cfb2011-07-22 18:36:33 -04004685 if (receive_length > BSG_MBOX_SIZE - sizeof(MAILBOX_t)) {
James Smartc7495932010-04-06 15:05:28 -04004686 rc = -ERANGE;
4687 goto job_done;
4688 }
4689
4690 /* mode zero uses a bde like biu diags command */
4691 if (mode == 0) {
James Smart7ad20aa2011-05-24 11:44:28 -04004692 pmb->un.varWords[3] = putPaddrLow(dmabuf->phys
4693 + sizeof(MAILBOX_t));
4694 pmb->un.varWords[4] = putPaddrHigh(dmabuf->phys
4695 + sizeof(MAILBOX_t));
James Smartc7495932010-04-06 15:05:28 -04004696 }
4697 } else if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart3ef6d242012-01-18 16:23:48 -05004698 /* Let type 4 (well known data) through because the data is
4699 * returned in varwords[4-8]
4700 * otherwise check the recieve length and fetch the buffer addr
4701 */
4702 if ((pmb->mbxCommand == MBX_DUMP_MEMORY) &&
4703 (pmb->un.varDmp.type != DMP_WELL_KNOWN)) {
James Smartc7495932010-04-06 15:05:28 -04004704 /* rebuild the command for sli4 using our own buffers
4705 * like we do for biu diags
4706 */
James Smartb6e3b9c2011-04-16 11:03:43 -04004707 receive_length = pmb->un.varWords[2];
James Smartc7495932010-04-06 15:05:28 -04004708 /* receive length cannot be greater than mailbox
4709 * extension size
4710 */
James Smart7ad20aa2011-05-24 11:44:28 -04004711 if (receive_length == 0) {
James Smartc7495932010-04-06 15:05:28 -04004712 rc = -ERANGE;
4713 goto job_done;
4714 }
James Smart7ad20aa2011-05-24 11:44:28 -04004715 pmb->un.varWords[3] = putPaddrLow(dmabuf->phys
4716 + sizeof(MAILBOX_t));
4717 pmb->un.varWords[4] = putPaddrHigh(dmabuf->phys
4718 + sizeof(MAILBOX_t));
James Smartc7495932010-04-06 15:05:28 -04004719 } else if ((pmb->mbxCommand == MBX_UPDATE_CFG) &&
4720 pmb->un.varUpdateCfg.co) {
James Smartb6e3b9c2011-04-16 11:03:43 -04004721 bde = (struct ulp_bde64 *)&pmb->un.varWords[4];
James Smartc7495932010-04-06 15:05:28 -04004722
4723 /* bde size cannot be greater than mailbox ext size */
James Smart88a2cfb2011-07-22 18:36:33 -04004724 if (bde->tus.f.bdeSize >
4725 BSG_MBOX_SIZE - sizeof(MAILBOX_t)) {
James Smartc7495932010-04-06 15:05:28 -04004726 rc = -ERANGE;
4727 goto job_done;
4728 }
James Smart7ad20aa2011-05-24 11:44:28 -04004729 bde->addrHigh = putPaddrHigh(dmabuf->phys
4730 + sizeof(MAILBOX_t));
4731 bde->addrLow = putPaddrLow(dmabuf->phys
4732 + sizeof(MAILBOX_t));
James Smart515e0aa2010-09-29 11:19:00 -04004733 } else if (pmb->mbxCommand == MBX_SLI4_CONFIG) {
James Smart7ad20aa2011-05-24 11:44:28 -04004734 /* Handling non-embedded SLI_CONFIG mailbox command */
4735 sli4_config = &pmboxq->u.mqe.un.sli4_config;
4736 if (!bf_get(lpfc_mbox_hdr_emb,
4737 &sli4_config->header.cfg_mhdr)) {
4738 /* rebuild the command for sli4 using our
4739 * own buffers like we do for biu diags
4740 */
4741 header = (struct mbox_header *)
4742 &pmb->un.varWords[0];
4743 nembed_sge = (struct lpfc_mbx_nembed_cmd *)
4744 &pmb->un.varWords[0];
4745 receive_length = nembed_sge->sge[0].length;
James Smart515e0aa2010-09-29 11:19:00 -04004746
James Smart7ad20aa2011-05-24 11:44:28 -04004747 /* receive length cannot be greater than
4748 * mailbox extension size
4749 */
4750 if ((receive_length == 0) ||
James Smart88a2cfb2011-07-22 18:36:33 -04004751 (receive_length >
4752 BSG_MBOX_SIZE - sizeof(MAILBOX_t))) {
James Smart7ad20aa2011-05-24 11:44:28 -04004753 rc = -ERANGE;
4754 goto job_done;
4755 }
4756
4757 nembed_sge->sge[0].pa_hi =
4758 putPaddrHigh(dmabuf->phys
4759 + sizeof(MAILBOX_t));
4760 nembed_sge->sge[0].pa_lo =
4761 putPaddrLow(dmabuf->phys
4762 + sizeof(MAILBOX_t));
James Smart515e0aa2010-09-29 11:19:00 -04004763 }
James Smartc7495932010-04-06 15:05:28 -04004764 }
James Smart3b5dd522010-01-26 23:10:15 -05004765 }
4766
James Smart7ad20aa2011-05-24 11:44:28 -04004767 dd_data->context_un.mbox.dmabuffers = dmabuf;
James Smartc7495932010-04-06 15:05:28 -04004768
James Smart3b5dd522010-01-26 23:10:15 -05004769 /* setup wake call as IOCB callback */
James Smart7ad20aa2011-05-24 11:44:28 -04004770 pmboxq->mbox_cmpl = lpfc_bsg_issue_mbox_cmpl;
James Smart7a470272010-03-15 11:25:20 -04004771
James Smart3b5dd522010-01-26 23:10:15 -05004772 /* setup context field to pass wait_queue pointer to wake function */
4773 pmboxq->context1 = dd_data;
4774 dd_data->type = TYPE_MBOX;
James Smarta33c4f72013-03-01 16:36:00 -05004775 dd_data->set_job = job;
James Smart3b5dd522010-01-26 23:10:15 -05004776 dd_data->context_un.mbox.pmboxq = pmboxq;
James Smart7ad20aa2011-05-24 11:44:28 -04004777 dd_data->context_un.mbox.mb = (MAILBOX_t *)pmbx;
James Smart7a470272010-03-15 11:25:20 -04004778 dd_data->context_un.mbox.ext = ext;
4779 dd_data->context_un.mbox.mbOffset = mbox_req->mbOffset;
4780 dd_data->context_un.mbox.inExtWLen = mbox_req->inExtWLen;
James Smartc7495932010-04-06 15:05:28 -04004781 dd_data->context_un.mbox.outExtWLen = mbox_req->outExtWLen;
James Smart3b5dd522010-01-26 23:10:15 -05004782 job->dd_data = dd_data;
James Smart7a470272010-03-15 11:25:20 -04004783
4784 if ((vport->fc_flag & FC_OFFLINE_MODE) ||
4785 (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))) {
4786 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
4787 if (rc != MBX_SUCCESS) {
4788 rc = (rc == MBX_TIMEOUT) ? -ETIME : -ENODEV;
4789 goto job_done;
4790 }
4791
4792 /* job finished, copy the data */
James Smart7ad20aa2011-05-24 11:44:28 -04004793 memcpy(pmbx, pmb, sizeof(*pmb));
James Smart7a470272010-03-15 11:25:20 -04004794 job->reply->reply_payload_rcv_len =
4795 sg_copy_from_buffer(job->reply_payload.sg_list,
James Smart7ad20aa2011-05-24 11:44:28 -04004796 job->reply_payload.sg_cnt,
4797 pmbx, size);
James Smart7a470272010-03-15 11:25:20 -04004798 /* not waiting mbox already done */
4799 rc = 0;
4800 goto job_done;
James Smart3b5dd522010-01-26 23:10:15 -05004801 }
4802
James Smart7a470272010-03-15 11:25:20 -04004803 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
4804 if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY))
4805 return 1; /* job started */
4806
4807job_done:
4808 /* common exit for error or job completed inline */
James Smart7a470272010-03-15 11:25:20 -04004809 if (pmboxq)
4810 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart7ad20aa2011-05-24 11:44:28 -04004811 lpfc_bsg_dma_page_free(phba, dmabuf);
James Smart7a470272010-03-15 11:25:20 -04004812 kfree(dd_data);
4813
James Smart7ad20aa2011-05-24 11:44:28 -04004814job_cont:
James Smart7a470272010-03-15 11:25:20 -04004815 return rc;
James Smart3b5dd522010-01-26 23:10:15 -05004816}
4817
4818/**
4819 * lpfc_bsg_mbox_cmd - process an fc bsg LPFC_BSG_VENDOR_MBOX command
4820 * @job: MBOX fc_bsg_job for LPFC_BSG_VENDOR_MBOX.
4821 **/
4822static int
4823lpfc_bsg_mbox_cmd(struct fc_bsg_job *job)
4824{
4825 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
4826 struct lpfc_hba *phba = vport->phba;
James Smart7ad20aa2011-05-24 11:44:28 -04004827 struct dfc_mbox_req *mbox_req;
James Smart3b5dd522010-01-26 23:10:15 -05004828 int rc = 0;
4829
James Smart7ad20aa2011-05-24 11:44:28 -04004830 /* mix-and-match backward compatibility */
James Smart3b5dd522010-01-26 23:10:15 -05004831 job->reply->reply_payload_rcv_len = 0;
4832 if (job->request_len <
4833 sizeof(struct fc_bsg_request) + sizeof(struct dfc_mbox_req)) {
James Smart7ad20aa2011-05-24 11:44:28 -04004834 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
Masanari Iida1051e9b2013-03-31 01:23:50 +09004835 "2737 Mix-and-match backward compatibility "
James Smart7ad20aa2011-05-24 11:44:28 -04004836 "between MBOX_REQ old size:%d and "
4837 "new request size:%d\n",
4838 (int)(job->request_len -
4839 sizeof(struct fc_bsg_request)),
4840 (int)sizeof(struct dfc_mbox_req));
4841 mbox_req = (struct dfc_mbox_req *)
4842 job->request->rqst_data.h_vendor.vendor_cmd;
4843 mbox_req->extMboxTag = 0;
4844 mbox_req->extSeqNum = 0;
James Smart3b5dd522010-01-26 23:10:15 -05004845 }
4846
4847 rc = lpfc_bsg_issue_mbox(phba, job, vport);
4848
James Smart3b5dd522010-01-26 23:10:15 -05004849 if (rc == 0) {
4850 /* job done */
4851 job->reply->result = 0;
4852 job->dd_data = NULL;
4853 job->job_done(job);
4854 } else if (rc == 1)
4855 /* job submitted, will complete later*/
4856 rc = 0; /* return zero, no error */
4857 else {
4858 /* some error occurred */
4859 job->reply->result = rc;
4860 job->dd_data = NULL;
4861 }
4862
4863 return rc;
4864}
4865
4866/**
James Smarte2aed292010-02-26 14:15:00 -05004867 * lpfc_bsg_menlo_cmd_cmp - lpfc_menlo_cmd completion handler
4868 * @phba: Pointer to HBA context object.
4869 * @cmdiocbq: Pointer to command iocb.
4870 * @rspiocbq: Pointer to response iocb.
4871 *
4872 * This function is the completion handler for iocbs issued using
4873 * lpfc_menlo_cmd function. This function is called by the
4874 * ring event handler function without any lock held. This function
4875 * can be called from both worker thread context and interrupt
4876 * context. This function also can be called from another thread which
4877 * cleans up the SLI layer objects.
4878 * This function copies the contents of the response iocb to the
4879 * response iocb memory object provided by the caller of
4880 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
4881 * sleeps for the iocb completion.
4882 **/
4883static void
4884lpfc_bsg_menlo_cmd_cmp(struct lpfc_hba *phba,
4885 struct lpfc_iocbq *cmdiocbq,
4886 struct lpfc_iocbq *rspiocbq)
4887{
4888 struct bsg_job_data *dd_data;
4889 struct fc_bsg_job *job;
4890 IOCB_t *rsp;
James Smarta33c4f72013-03-01 16:36:00 -05004891 struct lpfc_dmabuf *bmp, *cmp, *rmp;
James Smarte2aed292010-02-26 14:15:00 -05004892 struct lpfc_bsg_menlo *menlo;
4893 unsigned long flags;
4894 struct menlo_response *menlo_resp;
James Smarta33c4f72013-03-01 16:36:00 -05004895 unsigned int rsp_size;
James Smarte2aed292010-02-26 14:15:00 -05004896 int rc = 0;
4897
James Smarte2aed292010-02-26 14:15:00 -05004898 dd_data = cmdiocbq->context1;
James Smarta33c4f72013-03-01 16:36:00 -05004899 cmp = cmdiocbq->context2;
4900 bmp = cmdiocbq->context3;
James Smarte2aed292010-02-26 14:15:00 -05004901 menlo = &dd_data->context_un.menlo;
James Smarta33c4f72013-03-01 16:36:00 -05004902 rmp = menlo->rmp;
James Smarte2aed292010-02-26 14:15:00 -05004903 rsp = &rspiocbq->iocb;
4904
James Smarta33c4f72013-03-01 16:36:00 -05004905 /* Determine if job has been aborted */
4906 spin_lock_irqsave(&phba->ct_ev_lock, flags);
4907 job = dd_data->set_job;
4908 if (job) {
4909 /* Prevent timeout handling from trying to abort job */
4910 job->dd_data = NULL;
4911 }
4912 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smarte2aed292010-02-26 14:15:00 -05004913
James Smarta33c4f72013-03-01 16:36:00 -05004914 /* Copy the job data or set the failing status for the job */
4915
4916 if (job) {
4917 /* always return the xri, this would be used in the case
4918 * of a menlo download to allow the data to be sent as a
4919 * continuation of the exchange.
4920 */
4921
4922 menlo_resp = (struct menlo_response *)
4923 job->reply->reply_data.vendor_reply.vendor_rsp;
4924 menlo_resp->xri = rsp->ulpContext;
4925 if (rsp->ulpStatus) {
4926 if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
4927 switch (rsp->un.ulpWord[4] & IOERR_PARAM_MASK) {
4928 case IOERR_SEQUENCE_TIMEOUT:
4929 rc = -ETIMEDOUT;
4930 break;
4931 case IOERR_INVALID_RPI:
4932 rc = -EFAULT;
4933 break;
4934 default:
4935 rc = -EACCES;
4936 break;
4937 }
4938 } else {
James Smarte2aed292010-02-26 14:15:00 -05004939 rc = -EACCES;
James Smarte2aed292010-02-26 14:15:00 -05004940 }
James Smarta33c4f72013-03-01 16:36:00 -05004941 } else {
4942 rsp_size = rsp->un.genreq64.bdl.bdeSize;
4943 job->reply->reply_payload_rcv_len =
4944 lpfc_bsg_copy_data(rmp, &job->reply_payload,
4945 rsp_size, 0);
4946 }
James Smarte2aed292010-02-26 14:15:00 -05004947
James Smarta33c4f72013-03-01 16:36:00 -05004948 }
4949
James Smarte2aed292010-02-26 14:15:00 -05004950 lpfc_sli_release_iocbq(phba, cmdiocbq);
James Smarta33c4f72013-03-01 16:36:00 -05004951 lpfc_free_bsg_buffers(phba, cmp);
4952 lpfc_free_bsg_buffers(phba, rmp);
4953 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
James Smarte2aed292010-02-26 14:15:00 -05004954 kfree(bmp);
4955 kfree(dd_data);
James Smarta33c4f72013-03-01 16:36:00 -05004956
4957 /* Complete the job if active */
4958
4959 if (job) {
4960 job->reply->result = rc;
4961 job->job_done(job);
4962 }
4963
James Smarte2aed292010-02-26 14:15:00 -05004964 return;
4965}
4966
4967/**
4968 * lpfc_menlo_cmd - send an ioctl for menlo hardware
4969 * @job: fc_bsg_job to handle
4970 *
4971 * This function issues a gen request 64 CR ioctl for all menlo cmd requests,
4972 * all the command completions will return the xri for the command.
4973 * For menlo data requests a gen request 64 CX is used to continue the exchange
4974 * supplied in the menlo request header xri field.
4975 **/
4976static int
4977lpfc_menlo_cmd(struct fc_bsg_job *job)
4978{
4979 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
4980 struct lpfc_hba *phba = vport->phba;
James Smarta33c4f72013-03-01 16:36:00 -05004981 struct lpfc_iocbq *cmdiocbq;
4982 IOCB_t *cmd;
James Smarte2aed292010-02-26 14:15:00 -05004983 int rc = 0;
4984 struct menlo_command *menlo_cmd;
4985 struct menlo_response *menlo_resp;
James Smarta33c4f72013-03-01 16:36:00 -05004986 struct lpfc_dmabuf *bmp = NULL, *cmp = NULL, *rmp = NULL;
James Smarte2aed292010-02-26 14:15:00 -05004987 int request_nseg;
4988 int reply_nseg;
James Smarte2aed292010-02-26 14:15:00 -05004989 struct bsg_job_data *dd_data;
4990 struct ulp_bde64 *bpl = NULL;
4991
4992 /* in case no data is returned return just the return code */
4993 job->reply->reply_payload_rcv_len = 0;
4994
4995 if (job->request_len <
4996 sizeof(struct fc_bsg_request) +
4997 sizeof(struct menlo_command)) {
4998 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
4999 "2784 Received MENLO_CMD request below "
5000 "minimum size\n");
5001 rc = -ERANGE;
5002 goto no_dd_data;
5003 }
5004
5005 if (job->reply_len <
5006 sizeof(struct fc_bsg_request) + sizeof(struct menlo_response)) {
5007 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5008 "2785 Received MENLO_CMD reply below "
5009 "minimum size\n");
5010 rc = -ERANGE;
5011 goto no_dd_data;
5012 }
5013
5014 if (!(phba->menlo_flag & HBA_MENLO_SUPPORT)) {
5015 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5016 "2786 Adapter does not support menlo "
5017 "commands\n");
5018 rc = -EPERM;
5019 goto no_dd_data;
5020 }
5021
5022 menlo_cmd = (struct menlo_command *)
5023 job->request->rqst_data.h_vendor.vendor_cmd;
5024
5025 menlo_resp = (struct menlo_response *)
5026 job->reply->reply_data.vendor_reply.vendor_rsp;
5027
5028 /* allocate our bsg tracking structure */
5029 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
5030 if (!dd_data) {
5031 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5032 "2787 Failed allocation of dd_data\n");
5033 rc = -ENOMEM;
5034 goto no_dd_data;
5035 }
5036
5037 bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5038 if (!bmp) {
5039 rc = -ENOMEM;
5040 goto free_dd;
5041 }
5042
James Smarta33c4f72013-03-01 16:36:00 -05005043 bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
5044 if (!bmp->virt) {
James Smarte2aed292010-02-26 14:15:00 -05005045 rc = -ENOMEM;
5046 goto free_bmp;
5047 }
5048
James Smarte2aed292010-02-26 14:15:00 -05005049 INIT_LIST_HEAD(&bmp->list);
James Smarta33c4f72013-03-01 16:36:00 -05005050
5051 bpl = (struct ulp_bde64 *)bmp->virt;
5052 request_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64);
5053 cmp = lpfc_alloc_bsg_buffers(phba, job->request_payload.payload_len,
5054 1, bpl, &request_nseg);
5055 if (!cmp) {
5056 rc = -ENOMEM;
5057 goto free_bmp;
5058 }
5059 lpfc_bsg_copy_data(cmp, &job->request_payload,
5060 job->request_payload.payload_len, 1);
5061
5062 bpl += request_nseg;
5063 reply_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64) - request_nseg;
5064 rmp = lpfc_alloc_bsg_buffers(phba, job->reply_payload.payload_len, 0,
5065 bpl, &reply_nseg);
5066 if (!rmp) {
5067 rc = -ENOMEM;
5068 goto free_cmp;
James Smarte2aed292010-02-26 14:15:00 -05005069 }
5070
James Smarta33c4f72013-03-01 16:36:00 -05005071 cmdiocbq = lpfc_sli_get_iocbq(phba);
5072 if (!cmdiocbq) {
5073 rc = -ENOMEM;
5074 goto free_rmp;
James Smarte2aed292010-02-26 14:15:00 -05005075 }
5076
5077 cmd = &cmdiocbq->iocb;
5078 cmd->un.genreq64.bdl.ulpIoTag32 = 0;
5079 cmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
5080 cmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
5081 cmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
5082 cmd->un.genreq64.bdl.bdeSize =
5083 (request_nseg + reply_nseg) * sizeof(struct ulp_bde64);
5084 cmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
5085 cmd->un.genreq64.w5.hcsw.Dfctl = 0;
5086 cmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CMD;
5087 cmd->un.genreq64.w5.hcsw.Type = MENLO_TRANSPORT_TYPE; /* 0xfe */
5088 cmd->ulpBdeCount = 1;
5089 cmd->ulpClass = CLASS3;
5090 cmd->ulpOwner = OWN_CHIP;
5091 cmd->ulpLe = 1; /* Limited Edition */
5092 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
5093 cmdiocbq->vport = phba->pport;
5094 /* We want the firmware to timeout before we do */
5095 cmd->ulpTimeout = MENLO_TIMEOUT - 5;
James Smarte2aed292010-02-26 14:15:00 -05005096 cmdiocbq->iocb_cmpl = lpfc_bsg_menlo_cmd_cmp;
5097 cmdiocbq->context1 = dd_data;
James Smarta33c4f72013-03-01 16:36:00 -05005098 cmdiocbq->context2 = cmp;
5099 cmdiocbq->context3 = bmp;
James Smarte2aed292010-02-26 14:15:00 -05005100 if (menlo_cmd->cmd == LPFC_BSG_VENDOR_MENLO_CMD) {
5101 cmd->ulpCommand = CMD_GEN_REQUEST64_CR;
5102 cmd->ulpPU = MENLO_PU; /* 3 */
5103 cmd->un.ulpWord[4] = MENLO_DID; /* 0x0000FC0E */
5104 cmd->ulpContext = MENLO_CONTEXT; /* 0 */
5105 } else {
5106 cmd->ulpCommand = CMD_GEN_REQUEST64_CX;
5107 cmd->ulpPU = 1;
5108 cmd->un.ulpWord[4] = 0;
5109 cmd->ulpContext = menlo_cmd->xri;
5110 }
5111
5112 dd_data->type = TYPE_MENLO;
James Smarta33c4f72013-03-01 16:36:00 -05005113 dd_data->set_job = job;
James Smarte2aed292010-02-26 14:15:00 -05005114 dd_data->context_un.menlo.cmdiocbq = cmdiocbq;
James Smarta33c4f72013-03-01 16:36:00 -05005115 dd_data->context_un.menlo.rmp = rmp;
5116 job->dd_data = dd_data;
James Smarte2aed292010-02-26 14:15:00 -05005117
5118 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq,
5119 MENLO_TIMEOUT - 5);
5120 if (rc == IOCB_SUCCESS)
5121 return 0; /* done for now */
5122
James Smarte2aed292010-02-26 14:15:00 -05005123 lpfc_sli_release_iocbq(phba, cmdiocbq);
James Smarta33c4f72013-03-01 16:36:00 -05005124
5125free_rmp:
5126 lpfc_free_bsg_buffers(phba, rmp);
5127free_cmp:
5128 lpfc_free_bsg_buffers(phba, cmp);
James Smarte2aed292010-02-26 14:15:00 -05005129free_bmp:
James Smarta33c4f72013-03-01 16:36:00 -05005130 if (bmp->virt)
5131 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
James Smarte2aed292010-02-26 14:15:00 -05005132 kfree(bmp);
5133free_dd:
5134 kfree(dd_data);
5135no_dd_data:
5136 /* make error code available to userspace */
5137 job->reply->result = rc;
5138 job->dd_data = NULL;
5139 return rc;
5140}
James Smartb6e3b9c2011-04-16 11:03:43 -04005141
James Smarte2aed292010-02-26 14:15:00 -05005142/**
James Smartf1c3b0f2009-07-19 10:01:32 -04005143 * lpfc_bsg_hst_vendor - process a vendor-specific fc_bsg_job
5144 * @job: fc_bsg_job to handle
James Smart3b5dd522010-01-26 23:10:15 -05005145 **/
James Smartf1c3b0f2009-07-19 10:01:32 -04005146static int
5147lpfc_bsg_hst_vendor(struct fc_bsg_job *job)
5148{
5149 int command = job->request->rqst_data.h_vendor.vendor_cmd[0];
James Smart4cc0e562010-01-26 23:09:48 -05005150 int rc;
James Smartf1c3b0f2009-07-19 10:01:32 -04005151
5152 switch (command) {
5153 case LPFC_BSG_VENDOR_SET_CT_EVENT:
James Smart4cc0e562010-01-26 23:09:48 -05005154 rc = lpfc_bsg_hba_set_event(job);
James Smartf1c3b0f2009-07-19 10:01:32 -04005155 break;
James Smartf1c3b0f2009-07-19 10:01:32 -04005156 case LPFC_BSG_VENDOR_GET_CT_EVENT:
James Smart4cc0e562010-01-26 23:09:48 -05005157 rc = lpfc_bsg_hba_get_event(job);
James Smartf1c3b0f2009-07-19 10:01:32 -04005158 break;
James Smart3b5dd522010-01-26 23:10:15 -05005159 case LPFC_BSG_VENDOR_SEND_MGMT_RESP:
5160 rc = lpfc_bsg_send_mgmt_rsp(job);
5161 break;
5162 case LPFC_BSG_VENDOR_DIAG_MODE:
James Smart7ad20aa2011-05-24 11:44:28 -04005163 rc = lpfc_bsg_diag_loopback_mode(job);
James Smart3b5dd522010-01-26 23:10:15 -05005164 break;
James Smart7ad20aa2011-05-24 11:44:28 -04005165 case LPFC_BSG_VENDOR_DIAG_MODE_END:
5166 rc = lpfc_sli4_bsg_diag_mode_end(job);
5167 break;
5168 case LPFC_BSG_VENDOR_DIAG_RUN_LOOPBACK:
5169 rc = lpfc_bsg_diag_loopback_run(job);
5170 break;
5171 case LPFC_BSG_VENDOR_LINK_DIAG_TEST:
5172 rc = lpfc_sli4_bsg_link_diag_test(job);
James Smart3b5dd522010-01-26 23:10:15 -05005173 break;
5174 case LPFC_BSG_VENDOR_GET_MGMT_REV:
5175 rc = lpfc_bsg_get_dfc_rev(job);
5176 break;
5177 case LPFC_BSG_VENDOR_MBOX:
5178 rc = lpfc_bsg_mbox_cmd(job);
5179 break;
James Smarte2aed292010-02-26 14:15:00 -05005180 case LPFC_BSG_VENDOR_MENLO_CMD:
5181 case LPFC_BSG_VENDOR_MENLO_DATA:
5182 rc = lpfc_menlo_cmd(job);
5183 break;
James Smartf1c3b0f2009-07-19 10:01:32 -04005184 default:
James Smart4cc0e562010-01-26 23:09:48 -05005185 rc = -EINVAL;
5186 job->reply->reply_payload_rcv_len = 0;
5187 /* make error code available to userspace */
5188 job->reply->result = rc;
5189 break;
James Smartf1c3b0f2009-07-19 10:01:32 -04005190 }
James Smart4cc0e562010-01-26 23:09:48 -05005191
5192 return rc;
James Smartf1c3b0f2009-07-19 10:01:32 -04005193}
5194
5195/**
5196 * lpfc_bsg_request - handle a bsg request from the FC transport
5197 * @job: fc_bsg_job to handle
James Smart3b5dd522010-01-26 23:10:15 -05005198 **/
James Smartf1c3b0f2009-07-19 10:01:32 -04005199int
5200lpfc_bsg_request(struct fc_bsg_job *job)
5201{
5202 uint32_t msgcode;
James Smart4cc0e562010-01-26 23:09:48 -05005203 int rc;
James Smartf1c3b0f2009-07-19 10:01:32 -04005204
5205 msgcode = job->request->msgcode;
James Smartf1c3b0f2009-07-19 10:01:32 -04005206 switch (msgcode) {
5207 case FC_BSG_HST_VENDOR:
5208 rc = lpfc_bsg_hst_vendor(job);
5209 break;
5210 case FC_BSG_RPT_ELS:
5211 rc = lpfc_bsg_rport_els(job);
5212 break;
5213 case FC_BSG_RPT_CT:
James Smart4cc0e562010-01-26 23:09:48 -05005214 rc = lpfc_bsg_send_mgmt_cmd(job);
James Smartf1c3b0f2009-07-19 10:01:32 -04005215 break;
5216 default:
James Smart4cc0e562010-01-26 23:09:48 -05005217 rc = -EINVAL;
5218 job->reply->reply_payload_rcv_len = 0;
5219 /* make error code available to userspace */
5220 job->reply->result = rc;
James Smartf1c3b0f2009-07-19 10:01:32 -04005221 break;
5222 }
5223
5224 return rc;
5225}
5226
5227/**
5228 * lpfc_bsg_timeout - handle timeout of a bsg request from the FC transport
5229 * @job: fc_bsg_job that has timed out
5230 *
5231 * This function just aborts the job's IOCB. The aborted IOCB will return to
5232 * the waiting function which will handle passing the error back to userspace
James Smart3b5dd522010-01-26 23:10:15 -05005233 **/
James Smartf1c3b0f2009-07-19 10:01:32 -04005234int
5235lpfc_bsg_timeout(struct fc_bsg_job *job)
5236{
5237 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
5238 struct lpfc_hba *phba = vport->phba;
James Smart4cc0e562010-01-26 23:09:48 -05005239 struct lpfc_iocbq *cmdiocb;
James Smartf1c3b0f2009-07-19 10:01:32 -04005240 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart4cc0e562010-01-26 23:09:48 -05005241 struct bsg_job_data *dd_data;
5242 unsigned long flags;
James Smarta33c4f72013-03-01 16:36:00 -05005243 int rc = 0;
5244 LIST_HEAD(completions);
5245 struct lpfc_iocbq *check_iocb, *next_iocb;
5246
5247 /* if job's driver data is NULL, the command completed or is in the
5248 * the process of completing. In this case, return status to request
5249 * so the timeout is retried. This avoids double completion issues
5250 * and the request will be pulled off the timer queue when the
5251 * command's completion handler executes. Otherwise, prevent the
5252 * command's completion handler from executing the job done callback
5253 * and continue processing to abort the outstanding the command.
5254 */
James Smartf1c3b0f2009-07-19 10:01:32 -04005255
James Smart4cc0e562010-01-26 23:09:48 -05005256 spin_lock_irqsave(&phba->ct_ev_lock, flags);
5257 dd_data = (struct bsg_job_data *)job->dd_data;
James Smarta33c4f72013-03-01 16:36:00 -05005258 if (dd_data) {
5259 dd_data->set_job = NULL;
5260 job->dd_data = NULL;
5261 } else {
James Smart4cc0e562010-01-26 23:09:48 -05005262 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smarta33c4f72013-03-01 16:36:00 -05005263 return -EAGAIN;
James Smart4cc0e562010-01-26 23:09:48 -05005264 }
5265
5266 switch (dd_data->type) {
5267 case TYPE_IOCB:
James Smarta33c4f72013-03-01 16:36:00 -05005268 /* Check to see if IOCB was issued to the port or not. If not,
5269 * remove it from the txq queue and call cancel iocbs.
5270 * Otherwise, call abort iotag
James Smart4cc0e562010-01-26 23:09:48 -05005271 */
James Smarta33c4f72013-03-01 16:36:00 -05005272
5273 cmdiocb = dd_data->context_un.iocb.cmdiocbq;
5274 spin_lock_irq(&phba->hbalock);
5275 list_for_each_entry_safe(check_iocb, next_iocb, &pring->txq,
5276 list) {
5277 if (check_iocb == cmdiocb) {
5278 list_move_tail(&check_iocb->list, &completions);
5279 break;
5280 }
5281 }
5282 if (list_empty(&completions))
5283 lpfc_sli_issue_abort_iotag(phba, pring, cmdiocb);
5284 spin_unlock_irq(&phba->hbalock);
James Smart4cc0e562010-01-26 23:09:48 -05005285 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smarta33c4f72013-03-01 16:36:00 -05005286 if (!list_empty(&completions)) {
5287 lpfc_sli_cancel_iocbs(phba, &completions,
5288 IOSTAT_LOCAL_REJECT,
5289 IOERR_SLI_ABORTED);
5290 }
James Smart4cc0e562010-01-26 23:09:48 -05005291 break;
James Smarta33c4f72013-03-01 16:36:00 -05005292
5293 case TYPE_EVT:
James Smart3b5dd522010-01-26 23:10:15 -05005294 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smarta33c4f72013-03-01 16:36:00 -05005295 break;
5296
5297 case TYPE_MBOX:
5298 /* Update the ext buf ctx state if needed */
5299
James Smart7ad20aa2011-05-24 11:44:28 -04005300 if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_PORT)
5301 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_ABTS;
James Smarta33c4f72013-03-01 16:36:00 -05005302 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smart3b5dd522010-01-26 23:10:15 -05005303 break;
James Smarte2aed292010-02-26 14:15:00 -05005304 case TYPE_MENLO:
James Smarta33c4f72013-03-01 16:36:00 -05005305 /* Check to see if IOCB was issued to the port or not. If not,
5306 * remove it from the txq queue and call cancel iocbs.
5307 * Otherwise, call abort iotag.
5308 */
5309
5310 cmdiocb = dd_data->context_un.menlo.cmdiocbq;
James Smarte2aed292010-02-26 14:15:00 -05005311 spin_lock_irq(&phba->hbalock);
James Smarta33c4f72013-03-01 16:36:00 -05005312 list_for_each_entry_safe(check_iocb, next_iocb, &pring->txq,
5313 list) {
5314 if (check_iocb == cmdiocb) {
5315 list_move_tail(&check_iocb->list, &completions);
5316 break;
5317 }
5318 }
5319 if (list_empty(&completions))
5320 lpfc_sli_issue_abort_iotag(phba, pring, cmdiocb);
James Smarte2aed292010-02-26 14:15:00 -05005321 spin_unlock_irq(&phba->hbalock);
James Smarta33c4f72013-03-01 16:36:00 -05005322 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5323 if (!list_empty(&completions)) {
5324 lpfc_sli_cancel_iocbs(phba, &completions,
5325 IOSTAT_LOCAL_REJECT,
5326 IOERR_SLI_ABORTED);
5327 }
James Smarte2aed292010-02-26 14:15:00 -05005328 break;
James Smart4cc0e562010-01-26 23:09:48 -05005329 default:
5330 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5331 break;
5332 }
James Smartf1c3b0f2009-07-19 10:01:32 -04005333
James Smart4cc0e562010-01-26 23:09:48 -05005334 /* scsi transport fc fc_bsg_job_timeout expects a zero return code,
5335 * otherwise an error message will be displayed on the console
5336 * so always return success (zero)
5337 */
James Smarta33c4f72013-03-01 16:36:00 -05005338 return rc;
James Smartf1c3b0f2009-07-19 10:01:32 -04005339}