blob: 89a0d8016bbd6d038eafb14314b4ebbdb1e29b68 [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),
2579 ((phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT) * HZ);
2580 if (list_empty(&evt->events_to_see))
James Smartd439d282010-09-29 11:18:45 -04002581 ret_val = (time_left) ? -EINTR : -ETIMEDOUT;
James Smart3b5dd522010-01-26 23:10:15 -05002582 else {
James Smart3b5dd522010-01-26 23:10:15 -05002583 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2584 list_move(evt->events_to_see.prev, &evt->events_to_get);
2585 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2586 *rxxri = (list_entry(evt->events_to_get.prev,
2587 typeof(struct event_data),
2588 node))->immed_dat;
2589 }
2590 evt->waiting = 0;
2591
2592err_get_xri_exit:
2593 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2594 lpfc_bsg_event_unref(evt); /* release ref */
2595 lpfc_bsg_event_unref(evt); /* delete */
2596 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2597
2598 if (dmabuf) {
2599 if (dmabuf->virt)
2600 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
2601 kfree(dmabuf);
2602 }
2603
James Smartd439d282010-09-29 11:18:45 -04002604 if (cmdiocbq && (iocb_stat != IOCB_TIMEDOUT))
James Smart3b5dd522010-01-26 23:10:15 -05002605 lpfc_sli_release_iocbq(phba, cmdiocbq);
2606 if (rspiocbq)
2607 lpfc_sli_release_iocbq(phba, rspiocbq);
2608 return ret_val;
2609}
2610
2611/**
James Smart7ad20aa2011-05-24 11:44:28 -04002612 * lpfc_bsg_dma_page_alloc - allocate a bsg mbox page sized dma buffers
2613 * @phba: Pointer to HBA context object
2614 *
2615 * This function allocates BSG_MBOX_SIZE (4KB) page size dma buffer and.
2616 * retruns the pointer to the buffer.
2617 **/
2618static struct lpfc_dmabuf *
2619lpfc_bsg_dma_page_alloc(struct lpfc_hba *phba)
2620{
2621 struct lpfc_dmabuf *dmabuf;
2622 struct pci_dev *pcidev = phba->pcidev;
2623
2624 /* allocate dma buffer struct */
2625 dmabuf = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2626 if (!dmabuf)
2627 return NULL;
2628
2629 INIT_LIST_HEAD(&dmabuf->list);
2630
2631 /* now, allocate dma buffer */
2632 dmabuf->virt = dma_alloc_coherent(&pcidev->dev, BSG_MBOX_SIZE,
2633 &(dmabuf->phys), GFP_KERNEL);
2634
2635 if (!dmabuf->virt) {
2636 kfree(dmabuf);
2637 return NULL;
2638 }
2639 memset((uint8_t *)dmabuf->virt, 0, BSG_MBOX_SIZE);
2640
2641 return dmabuf;
2642}
2643
2644/**
2645 * lpfc_bsg_dma_page_free - free a bsg mbox page sized dma buffer
2646 * @phba: Pointer to HBA context object.
2647 * @dmabuf: Pointer to the bsg mbox page sized dma buffer descriptor.
2648 *
2649 * This routine just simply frees a dma buffer and its associated buffer
2650 * descriptor referred by @dmabuf.
2651 **/
2652static void
2653lpfc_bsg_dma_page_free(struct lpfc_hba *phba, struct lpfc_dmabuf *dmabuf)
2654{
2655 struct pci_dev *pcidev = phba->pcidev;
2656
2657 if (!dmabuf)
2658 return;
2659
2660 if (dmabuf->virt)
2661 dma_free_coherent(&pcidev->dev, BSG_MBOX_SIZE,
2662 dmabuf->virt, dmabuf->phys);
2663 kfree(dmabuf);
2664 return;
2665}
2666
2667/**
2668 * lpfc_bsg_dma_page_list_free - free a list of bsg mbox page sized dma buffers
2669 * @phba: Pointer to HBA context object.
2670 * @dmabuf_list: Pointer to a list of bsg mbox page sized dma buffer descs.
2671 *
2672 * This routine just simply frees all dma buffers and their associated buffer
2673 * descriptors referred by @dmabuf_list.
2674 **/
2675static void
2676lpfc_bsg_dma_page_list_free(struct lpfc_hba *phba,
2677 struct list_head *dmabuf_list)
2678{
2679 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
2680
2681 if (list_empty(dmabuf_list))
2682 return;
2683
2684 list_for_each_entry_safe(dmabuf, next_dmabuf, dmabuf_list, list) {
2685 list_del_init(&dmabuf->list);
2686 lpfc_bsg_dma_page_free(phba, dmabuf);
2687 }
2688 return;
2689}
2690
2691/**
James Smart3b5dd522010-01-26 23:10:15 -05002692 * diag_cmd_data_alloc - fills in a bde struct with dma buffers
2693 * @phba: Pointer to HBA context object
2694 * @bpl: Pointer to 64 bit bde structure
2695 * @size: Number of bytes to process
2696 * @nocopydata: Flag to copy user data into the allocated buffer
2697 *
2698 * This function allocates page size buffers and populates an lpfc_dmabufext.
2699 * If allowed the user data pointed to with indataptr is copied into the kernel
2700 * memory. The chained list of page size buffers is returned.
2701 **/
2702static struct lpfc_dmabufext *
2703diag_cmd_data_alloc(struct lpfc_hba *phba,
2704 struct ulp_bde64 *bpl, uint32_t size,
2705 int nocopydata)
2706{
2707 struct lpfc_dmabufext *mlist = NULL;
2708 struct lpfc_dmabufext *dmp;
2709 int cnt, offset = 0, i = 0;
2710 struct pci_dev *pcidev;
2711
2712 pcidev = phba->pcidev;
2713
2714 while (size) {
2715 /* We get chunks of 4K */
2716 if (size > BUF_SZ_4K)
2717 cnt = BUF_SZ_4K;
2718 else
2719 cnt = size;
2720
2721 /* allocate struct lpfc_dmabufext buffer header */
2722 dmp = kmalloc(sizeof(struct lpfc_dmabufext), GFP_KERNEL);
2723 if (!dmp)
2724 goto out;
2725
2726 INIT_LIST_HEAD(&dmp->dma.list);
2727
2728 /* Queue it to a linked list */
2729 if (mlist)
2730 list_add_tail(&dmp->dma.list, &mlist->dma.list);
2731 else
2732 mlist = dmp;
2733
2734 /* allocate buffer */
2735 dmp->dma.virt = dma_alloc_coherent(&pcidev->dev,
2736 cnt,
2737 &(dmp->dma.phys),
2738 GFP_KERNEL);
2739
2740 if (!dmp->dma.virt)
2741 goto out;
2742
2743 dmp->size = cnt;
2744
2745 if (nocopydata) {
2746 bpl->tus.f.bdeFlags = 0;
2747 pci_dma_sync_single_for_device(phba->pcidev,
2748 dmp->dma.phys, LPFC_BPL_SIZE, PCI_DMA_TODEVICE);
2749
2750 } else {
2751 memset((uint8_t *)dmp->dma.virt, 0, cnt);
2752 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2753 }
2754
2755 /* build buffer ptr list for IOCB */
2756 bpl->addrLow = le32_to_cpu(putPaddrLow(dmp->dma.phys));
2757 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dmp->dma.phys));
2758 bpl->tus.f.bdeSize = (ushort) cnt;
2759 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2760 bpl++;
2761
2762 i++;
2763 offset += cnt;
2764 size -= cnt;
2765 }
2766
2767 mlist->flag = i;
2768 return mlist;
2769out:
2770 diag_cmd_data_free(phba, mlist);
2771 return NULL;
2772}
2773
2774/**
2775 * lpfcdiag_loop_post_rxbufs - post the receive buffers for an unsol CT cmd
2776 * @phba: Pointer to HBA context object
2777 * @rxxri: Receive exchange id
2778 * @len: Number of data bytes
2779 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002780 * This function allocates and posts a data buffer of sufficient size to receive
James Smart3b5dd522010-01-26 23:10:15 -05002781 * an unsolicted CT command.
2782 **/
2783static int lpfcdiag_loop_post_rxbufs(struct lpfc_hba *phba, uint16_t rxxri,
2784 size_t len)
2785{
2786 struct lpfc_sli *psli = &phba->sli;
2787 struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
2788 struct lpfc_iocbq *cmdiocbq;
2789 IOCB_t *cmd = NULL;
2790 struct list_head head, *curr, *next;
2791 struct lpfc_dmabuf *rxbmp;
2792 struct lpfc_dmabuf *dmp;
2793 struct lpfc_dmabuf *mp[2] = {NULL, NULL};
2794 struct ulp_bde64 *rxbpl = NULL;
2795 uint32_t num_bde;
2796 struct lpfc_dmabufext *rxbuffer = NULL;
2797 int ret_val = 0;
James Smartd439d282010-09-29 11:18:45 -04002798 int iocb_stat;
James Smart3b5dd522010-01-26 23:10:15 -05002799 int i = 0;
2800
2801 cmdiocbq = lpfc_sli_get_iocbq(phba);
2802 rxbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2803 if (rxbmp != NULL) {
2804 rxbmp->virt = lpfc_mbuf_alloc(phba, 0, &rxbmp->phys);
James Smartc7495932010-04-06 15:05:28 -04002805 if (rxbmp->virt) {
2806 INIT_LIST_HEAD(&rxbmp->list);
2807 rxbpl = (struct ulp_bde64 *) rxbmp->virt;
2808 rxbuffer = diag_cmd_data_alloc(phba, rxbpl, len, 0);
2809 }
James Smart3b5dd522010-01-26 23:10:15 -05002810 }
2811
2812 if (!cmdiocbq || !rxbmp || !rxbpl || !rxbuffer) {
James Smartd439d282010-09-29 11:18:45 -04002813 ret_val = -ENOMEM;
James Smart3b5dd522010-01-26 23:10:15 -05002814 goto err_post_rxbufs_exit;
2815 }
2816
2817 /* Queue buffers for the receive exchange */
2818 num_bde = (uint32_t)rxbuffer->flag;
2819 dmp = &rxbuffer->dma;
2820
2821 cmd = &cmdiocbq->iocb;
2822 i = 0;
2823
2824 INIT_LIST_HEAD(&head);
2825 list_add_tail(&head, &dmp->list);
2826 list_for_each_safe(curr, next, &head) {
2827 mp[i] = list_entry(curr, struct lpfc_dmabuf, list);
2828 list_del(curr);
2829
2830 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
2831 mp[i]->buffer_tag = lpfc_sli_get_buffer_tag(phba);
2832 cmd->un.quexri64cx.buff.bde.addrHigh =
2833 putPaddrHigh(mp[i]->phys);
2834 cmd->un.quexri64cx.buff.bde.addrLow =
2835 putPaddrLow(mp[i]->phys);
2836 cmd->un.quexri64cx.buff.bde.tus.f.bdeSize =
2837 ((struct lpfc_dmabufext *)mp[i])->size;
2838 cmd->un.quexri64cx.buff.buffer_tag = mp[i]->buffer_tag;
2839 cmd->ulpCommand = CMD_QUE_XRI64_CX;
2840 cmd->ulpPU = 0;
2841 cmd->ulpLe = 1;
2842 cmd->ulpBdeCount = 1;
2843 cmd->unsli3.que_xri64cx_ext_words.ebde_count = 0;
2844
2845 } else {
2846 cmd->un.cont64[i].addrHigh = putPaddrHigh(mp[i]->phys);
2847 cmd->un.cont64[i].addrLow = putPaddrLow(mp[i]->phys);
2848 cmd->un.cont64[i].tus.f.bdeSize =
2849 ((struct lpfc_dmabufext *)mp[i])->size;
2850 cmd->ulpBdeCount = ++i;
2851
2852 if ((--num_bde > 0) && (i < 2))
2853 continue;
2854
2855 cmd->ulpCommand = CMD_QUE_XRI_BUF64_CX;
2856 cmd->ulpLe = 1;
2857 }
2858
2859 cmd->ulpClass = CLASS3;
2860 cmd->ulpContext = rxxri;
2861
James Smartd439d282010-09-29 11:18:45 -04002862 iocb_stat = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq,
2863 0);
2864 if (iocb_stat == IOCB_ERROR) {
James Smart3b5dd522010-01-26 23:10:15 -05002865 diag_cmd_data_free(phba,
2866 (struct lpfc_dmabufext *)mp[0]);
2867 if (mp[1])
2868 diag_cmd_data_free(phba,
2869 (struct lpfc_dmabufext *)mp[1]);
2870 dmp = list_entry(next, struct lpfc_dmabuf, list);
James Smartd439d282010-09-29 11:18:45 -04002871 ret_val = -EIO;
James Smart3b5dd522010-01-26 23:10:15 -05002872 goto err_post_rxbufs_exit;
2873 }
2874
2875 lpfc_sli_ringpostbuf_put(phba, pring, mp[0]);
2876 if (mp[1]) {
2877 lpfc_sli_ringpostbuf_put(phba, pring, mp[1]);
2878 mp[1] = NULL;
2879 }
2880
2881 /* The iocb was freed by lpfc_sli_issue_iocb */
2882 cmdiocbq = lpfc_sli_get_iocbq(phba);
2883 if (!cmdiocbq) {
2884 dmp = list_entry(next, struct lpfc_dmabuf, list);
James Smartd439d282010-09-29 11:18:45 -04002885 ret_val = -EIO;
James Smart3b5dd522010-01-26 23:10:15 -05002886 goto err_post_rxbufs_exit;
2887 }
2888
2889 cmd = &cmdiocbq->iocb;
2890 i = 0;
2891 }
2892 list_del(&head);
2893
2894err_post_rxbufs_exit:
2895
2896 if (rxbmp) {
2897 if (rxbmp->virt)
2898 lpfc_mbuf_free(phba, rxbmp->virt, rxbmp->phys);
2899 kfree(rxbmp);
2900 }
2901
2902 if (cmdiocbq)
2903 lpfc_sli_release_iocbq(phba, cmdiocbq);
2904 return ret_val;
2905}
2906
2907/**
James Smart7ad20aa2011-05-24 11:44:28 -04002908 * lpfc_bsg_diag_loopback_run - run loopback on a port by issue ct cmd to itself
James Smart3b5dd522010-01-26 23:10:15 -05002909 * @job: LPFC_BSG_VENDOR_DIAG_TEST fc_bsg_job
2910 *
2911 * This function receives a user data buffer to be transmitted and received on
2912 * the same port, the link must be up and in loopback mode prior
2913 * to being called.
2914 * 1. A kernel buffer is allocated to copy the user data into.
2915 * 2. The port registers with "itself".
2916 * 3. The transmit and receive exchange ids are obtained.
2917 * 4. The receive exchange id is posted.
2918 * 5. A new els loopback event is created.
2919 * 6. The command and response iocbs are allocated.
2920 * 7. The cmd iocb FsType is set to elx loopback and the CmdRsp to looppback.
2921 *
2922 * This function is meant to be called n times while the port is in loopback
2923 * so it is the apps responsibility to issue a reset to take the port out
2924 * of loopback mode.
2925 **/
2926static int
James Smart7ad20aa2011-05-24 11:44:28 -04002927lpfc_bsg_diag_loopback_run(struct fc_bsg_job *job)
James Smart3b5dd522010-01-26 23:10:15 -05002928{
2929 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
2930 struct lpfc_hba *phba = vport->phba;
2931 struct diag_mode_test *diag_mode;
2932 struct lpfc_bsg_event *evt;
2933 struct event_data *evdat;
2934 struct lpfc_sli *psli = &phba->sli;
2935 uint32_t size;
2936 uint32_t full_size;
2937 size_t segment_len = 0, segment_offset = 0, current_offset = 0;
James Smart40426292010-12-15 17:58:10 -05002938 uint16_t rpi = 0;
James Smart1b511972011-12-13 13:23:09 -05002939 struct lpfc_iocbq *cmdiocbq, *rspiocbq = NULL;
2940 IOCB_t *cmd, *rsp = NULL;
James Smart3b5dd522010-01-26 23:10:15 -05002941 struct lpfc_sli_ct_request *ctreq;
2942 struct lpfc_dmabuf *txbmp;
2943 struct ulp_bde64 *txbpl = NULL;
2944 struct lpfc_dmabufext *txbuffer = NULL;
2945 struct list_head head;
2946 struct lpfc_dmabuf *curr;
James Smart1b511972011-12-13 13:23:09 -05002947 uint16_t txxri = 0, rxxri;
James Smart3b5dd522010-01-26 23:10:15 -05002948 uint32_t num_bde;
2949 uint8_t *ptr = NULL, *rx_databuf = NULL;
2950 int rc = 0;
James Smartd439d282010-09-29 11:18:45 -04002951 int time_left;
2952 int iocb_stat;
James Smart3b5dd522010-01-26 23:10:15 -05002953 unsigned long flags;
2954 void *dataout = NULL;
2955 uint32_t total_mem;
2956
2957 /* in case no data is returned return just the return code */
2958 job->reply->reply_payload_rcv_len = 0;
2959
2960 if (job->request_len <
2961 sizeof(struct fc_bsg_request) + sizeof(struct diag_mode_test)) {
2962 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2963 "2739 Received DIAG TEST request below minimum "
2964 "size\n");
2965 rc = -EINVAL;
2966 goto loopback_test_exit;
2967 }
2968
2969 if (job->request_payload.payload_len !=
2970 job->reply_payload.payload_len) {
2971 rc = -EINVAL;
2972 goto loopback_test_exit;
2973 }
James Smart3b5dd522010-01-26 23:10:15 -05002974 diag_mode = (struct diag_mode_test *)
2975 job->request->rqst_data.h_vendor.vendor_cmd;
2976
2977 if ((phba->link_state == LPFC_HBA_ERROR) ||
2978 (psli->sli_flag & LPFC_BLOCK_MGMT_IO) ||
2979 (!(psli->sli_flag & LPFC_SLI_ACTIVE))) {
2980 rc = -EACCES;
2981 goto loopback_test_exit;
2982 }
2983
2984 if (!lpfc_is_link_up(phba) || !(phba->link_flag & LS_LOOPBACK_MODE)) {
2985 rc = -EACCES;
2986 goto loopback_test_exit;
2987 }
2988
2989 size = job->request_payload.payload_len;
2990 full_size = size + ELX_LOOPBACK_HEADER_SZ; /* plus the header */
2991
2992 if ((size == 0) || (size > 80 * BUF_SZ_4K)) {
2993 rc = -ERANGE;
2994 goto loopback_test_exit;
2995 }
2996
James Smart63e801c2010-11-20 23:14:19 -05002997 if (full_size >= BUF_SZ_4K) {
James Smart3b5dd522010-01-26 23:10:15 -05002998 /*
2999 * Allocate memory for ioctl data. If buffer is bigger than 64k,
3000 * then we allocate 64k and re-use that buffer over and over to
3001 * xfer the whole block. This is because Linux kernel has a
3002 * problem allocating more than 120k of kernel space memory. Saw
3003 * problem with GET_FCPTARGETMAPPING...
3004 */
3005 if (size <= (64 * 1024))
James Smart63e801c2010-11-20 23:14:19 -05003006 total_mem = full_size;
James Smart3b5dd522010-01-26 23:10:15 -05003007 else
3008 total_mem = 64 * 1024;
3009 } else
3010 /* Allocate memory for ioctl data */
3011 total_mem = BUF_SZ_4K;
3012
3013 dataout = kmalloc(total_mem, GFP_KERNEL);
3014 if (dataout == NULL) {
3015 rc = -ENOMEM;
3016 goto loopback_test_exit;
3017 }
3018
3019 ptr = dataout;
3020 ptr += ELX_LOOPBACK_HEADER_SZ;
3021 sg_copy_to_buffer(job->request_payload.sg_list,
3022 job->request_payload.sg_cnt,
3023 ptr, size);
James Smart3b5dd522010-01-26 23:10:15 -05003024 rc = lpfcdiag_loop_self_reg(phba, &rpi);
James Smartd439d282010-09-29 11:18:45 -04003025 if (rc)
James Smart3b5dd522010-01-26 23:10:15 -05003026 goto loopback_test_exit;
James Smart3b5dd522010-01-26 23:10:15 -05003027
James Smart1b511972011-12-13 13:23:09 -05003028 if (phba->sli_rev < LPFC_SLI_REV4) {
3029 rc = lpfcdiag_loop_get_xri(phba, rpi, &txxri, &rxxri);
3030 if (rc) {
3031 lpfcdiag_loop_self_unreg(phba, rpi);
3032 goto loopback_test_exit;
3033 }
James Smart3b5dd522010-01-26 23:10:15 -05003034
James Smart1b511972011-12-13 13:23:09 -05003035 rc = lpfcdiag_loop_post_rxbufs(phba, rxxri, full_size);
3036 if (rc) {
3037 lpfcdiag_loop_self_unreg(phba, rpi);
3038 goto loopback_test_exit;
3039 }
James Smart3b5dd522010-01-26 23:10:15 -05003040 }
James Smart3b5dd522010-01-26 23:10:15 -05003041 evt = lpfc_bsg_event_new(FC_REG_CT_EVENT, current->pid,
3042 SLI_CT_ELX_LOOPBACK);
3043 if (!evt) {
3044 lpfcdiag_loop_self_unreg(phba, rpi);
3045 rc = -ENOMEM;
3046 goto loopback_test_exit;
3047 }
3048
3049 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3050 list_add(&evt->node, &phba->ct_ev_waiters);
3051 lpfc_bsg_event_ref(evt);
3052 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3053
3054 cmdiocbq = lpfc_sli_get_iocbq(phba);
James Smart1b511972011-12-13 13:23:09 -05003055 if (phba->sli_rev < LPFC_SLI_REV4)
3056 rspiocbq = lpfc_sli_get_iocbq(phba);
James Smart3b5dd522010-01-26 23:10:15 -05003057 txbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3058
3059 if (txbmp) {
3060 txbmp->virt = lpfc_mbuf_alloc(phba, 0, &txbmp->phys);
James Smartc7495932010-04-06 15:05:28 -04003061 if (txbmp->virt) {
3062 INIT_LIST_HEAD(&txbmp->list);
3063 txbpl = (struct ulp_bde64 *) txbmp->virt;
James Smart3b5dd522010-01-26 23:10:15 -05003064 txbuffer = diag_cmd_data_alloc(phba,
3065 txbpl, full_size, 0);
James Smartc7495932010-04-06 15:05:28 -04003066 }
James Smart3b5dd522010-01-26 23:10:15 -05003067 }
3068
James Smart1b511972011-12-13 13:23:09 -05003069 if (!cmdiocbq || !txbmp || !txbpl || !txbuffer || !txbmp->virt) {
3070 rc = -ENOMEM;
3071 goto err_loopback_test_exit;
3072 }
3073 if ((phba->sli_rev < LPFC_SLI_REV4) && !rspiocbq) {
James Smart3b5dd522010-01-26 23:10:15 -05003074 rc = -ENOMEM;
3075 goto err_loopback_test_exit;
3076 }
3077
3078 cmd = &cmdiocbq->iocb;
James Smart1b511972011-12-13 13:23:09 -05003079 if (phba->sli_rev < LPFC_SLI_REV4)
3080 rsp = &rspiocbq->iocb;
James Smart3b5dd522010-01-26 23:10:15 -05003081
3082 INIT_LIST_HEAD(&head);
3083 list_add_tail(&head, &txbuffer->dma.list);
3084 list_for_each_entry(curr, &head, list) {
3085 segment_len = ((struct lpfc_dmabufext *)curr)->size;
3086 if (current_offset == 0) {
3087 ctreq = curr->virt;
3088 memset(ctreq, 0, ELX_LOOPBACK_HEADER_SZ);
3089 ctreq->RevisionId.bits.Revision = SLI_CT_REVISION;
3090 ctreq->RevisionId.bits.InId = 0;
3091 ctreq->FsType = SLI_CT_ELX_LOOPBACK;
3092 ctreq->FsSubType = 0;
3093 ctreq->CommandResponse.bits.CmdRsp = ELX_LOOPBACK_DATA;
3094 ctreq->CommandResponse.bits.Size = size;
3095 segment_offset = ELX_LOOPBACK_HEADER_SZ;
3096 } else
3097 segment_offset = 0;
3098
3099 BUG_ON(segment_offset >= segment_len);
3100 memcpy(curr->virt + segment_offset,
3101 ptr + current_offset,
3102 segment_len - segment_offset);
3103
3104 current_offset += segment_len - segment_offset;
3105 BUG_ON(current_offset > size);
3106 }
3107 list_del(&head);
3108
3109 /* Build the XMIT_SEQUENCE iocb */
James Smart3b5dd522010-01-26 23:10:15 -05003110 num_bde = (uint32_t)txbuffer->flag;
3111
3112 cmd->un.xseq64.bdl.addrHigh = putPaddrHigh(txbmp->phys);
3113 cmd->un.xseq64.bdl.addrLow = putPaddrLow(txbmp->phys);
3114 cmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
3115 cmd->un.xseq64.bdl.bdeSize = (num_bde * sizeof(struct ulp_bde64));
3116
3117 cmd->un.xseq64.w5.hcsw.Fctl = (LS | LA);
3118 cmd->un.xseq64.w5.hcsw.Dfctl = 0;
3119 cmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
3120 cmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
3121
3122 cmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;
3123 cmd->ulpBdeCount = 1;
3124 cmd->ulpLe = 1;
3125 cmd->ulpClass = CLASS3;
James Smart3b5dd522010-01-26 23:10:15 -05003126
James Smart1b511972011-12-13 13:23:09 -05003127 if (phba->sli_rev < LPFC_SLI_REV4) {
3128 cmd->ulpContext = txxri;
3129 } else {
3130 cmd->un.xseq64.bdl.ulpIoTag32 = 0;
3131 cmd->un.ulpWord[3] = phba->sli4_hba.rpi_ids[rpi];
3132 cmdiocbq->context3 = txbmp;
3133 cmdiocbq->sli4_xritag = NO_XRI;
3134 cmd->unsli3.rcvsli3.ox_id = 0xffff;
3135 }
James Smart3b5dd522010-01-26 23:10:15 -05003136 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
3137 cmdiocbq->vport = phba->pport;
James Smartd439d282010-09-29 11:18:45 -04003138 iocb_stat = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq,
3139 rspiocbq, (phba->fc_ratov * 2) +
3140 LPFC_DRVR_TIMEOUT);
James Smart3b5dd522010-01-26 23:10:15 -05003141
James Smart1b511972011-12-13 13:23:09 -05003142 if ((iocb_stat != IOCB_SUCCESS) || ((phba->sli_rev < LPFC_SLI_REV4) &&
3143 (rsp->ulpStatus != IOCB_SUCCESS))) {
3144 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3145 "3126 Failed loopback test issue iocb: "
3146 "iocb_stat:x%x\n", iocb_stat);
James Smart3b5dd522010-01-26 23:10:15 -05003147 rc = -EIO;
3148 goto err_loopback_test_exit;
3149 }
3150
3151 evt->waiting = 1;
James Smartd439d282010-09-29 11:18:45 -04003152 time_left = wait_event_interruptible_timeout(
James Smart3b5dd522010-01-26 23:10:15 -05003153 evt->wq, !list_empty(&evt->events_to_see),
3154 ((phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT) * HZ);
3155 evt->waiting = 0;
James Smart1b511972011-12-13 13:23:09 -05003156 if (list_empty(&evt->events_to_see)) {
James Smartd439d282010-09-29 11:18:45 -04003157 rc = (time_left) ? -EINTR : -ETIMEDOUT;
James Smart1b511972011-12-13 13:23:09 -05003158 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3159 "3125 Not receiving unsolicited event, "
3160 "rc:x%x\n", rc);
3161 } else {
James Smart3b5dd522010-01-26 23:10:15 -05003162 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3163 list_move(evt->events_to_see.prev, &evt->events_to_get);
3164 evdat = list_entry(evt->events_to_get.prev,
3165 typeof(*evdat), node);
3166 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3167 rx_databuf = evdat->data;
3168 if (evdat->len != full_size) {
3169 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3170 "1603 Loopback test did not receive expected "
3171 "data length. actual length 0x%x expected "
3172 "length 0x%x\n",
3173 evdat->len, full_size);
3174 rc = -EIO;
3175 } else if (rx_databuf == NULL)
3176 rc = -EIO;
3177 else {
3178 rc = IOCB_SUCCESS;
3179 /* skip over elx loopback header */
3180 rx_databuf += ELX_LOOPBACK_HEADER_SZ;
3181 job->reply->reply_payload_rcv_len =
3182 sg_copy_from_buffer(job->reply_payload.sg_list,
3183 job->reply_payload.sg_cnt,
3184 rx_databuf, size);
3185 job->reply->reply_payload_rcv_len = size;
3186 }
3187 }
3188
3189err_loopback_test_exit:
3190 lpfcdiag_loop_self_unreg(phba, rpi);
3191
3192 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3193 lpfc_bsg_event_unref(evt); /* release ref */
3194 lpfc_bsg_event_unref(evt); /* delete */
3195 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3196
3197 if (cmdiocbq != NULL)
3198 lpfc_sli_release_iocbq(phba, cmdiocbq);
3199
3200 if (rspiocbq != NULL)
3201 lpfc_sli_release_iocbq(phba, rspiocbq);
3202
3203 if (txbmp != NULL) {
3204 if (txbpl != NULL) {
3205 if (txbuffer != NULL)
3206 diag_cmd_data_free(phba, txbuffer);
3207 lpfc_mbuf_free(phba, txbmp->virt, txbmp->phys);
3208 }
3209 kfree(txbmp);
3210 }
3211
3212loopback_test_exit:
3213 kfree(dataout);
3214 /* make error code available to userspace */
3215 job->reply->result = rc;
3216 job->dd_data = NULL;
3217 /* complete the job back to userspace if no error */
James Smart1b511972011-12-13 13:23:09 -05003218 if (rc == IOCB_SUCCESS)
James Smart3b5dd522010-01-26 23:10:15 -05003219 job->job_done(job);
3220 return rc;
3221}
3222
3223/**
3224 * lpfc_bsg_get_dfc_rev - process a GET_DFC_REV bsg vendor command
3225 * @job: GET_DFC_REV fc_bsg_job
3226 **/
3227static int
3228lpfc_bsg_get_dfc_rev(struct fc_bsg_job *job)
3229{
3230 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
3231 struct lpfc_hba *phba = vport->phba;
3232 struct get_mgmt_rev *event_req;
3233 struct get_mgmt_rev_reply *event_reply;
3234 int rc = 0;
3235
3236 if (job->request_len <
3237 sizeof(struct fc_bsg_request) + sizeof(struct get_mgmt_rev)) {
3238 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3239 "2740 Received GET_DFC_REV request below "
3240 "minimum size\n");
3241 rc = -EINVAL;
3242 goto job_error;
3243 }
3244
3245 event_req = (struct get_mgmt_rev *)
3246 job->request->rqst_data.h_vendor.vendor_cmd;
3247
3248 event_reply = (struct get_mgmt_rev_reply *)
3249 job->reply->reply_data.vendor_reply.vendor_rsp;
3250
3251 if (job->reply_len <
3252 sizeof(struct fc_bsg_request) + sizeof(struct get_mgmt_rev_reply)) {
3253 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3254 "2741 Received GET_DFC_REV reply below "
3255 "minimum size\n");
3256 rc = -EINVAL;
3257 goto job_error;
3258 }
3259
3260 event_reply->info.a_Major = MANAGEMENT_MAJOR_REV;
3261 event_reply->info.a_Minor = MANAGEMENT_MINOR_REV;
3262job_error:
3263 job->reply->result = rc;
3264 if (rc == 0)
3265 job->job_done(job);
3266 return rc;
3267}
3268
3269/**
James Smart7ad20aa2011-05-24 11:44:28 -04003270 * lpfc_bsg_issue_mbox_cmpl - lpfc_bsg_issue_mbox mbox completion handler
James Smart3b5dd522010-01-26 23:10:15 -05003271 * @phba: Pointer to HBA context object.
3272 * @pmboxq: Pointer to mailbox command.
3273 *
3274 * This is completion handler function for mailbox commands issued from
3275 * lpfc_bsg_issue_mbox function. This function is called by the
3276 * mailbox event handler function with no lock held. This function
3277 * will wake up thread waiting on the wait queue pointed by context1
3278 * of the mailbox.
3279 **/
3280void
James Smart7ad20aa2011-05-24 11:44:28 -04003281lpfc_bsg_issue_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
James Smart3b5dd522010-01-26 23:10:15 -05003282{
3283 struct bsg_job_data *dd_data;
James Smart3b5dd522010-01-26 23:10:15 -05003284 struct fc_bsg_job *job;
3285 uint32_t size;
3286 unsigned long flags;
James Smart7ad20aa2011-05-24 11:44:28 -04003287 uint8_t *pmb, *pmb_buf;
James Smart3b5dd522010-01-26 23:10:15 -05003288
James Smart3b5dd522010-01-26 23:10:15 -05003289 dd_data = pmboxq->context1;
James Smart3b5dd522010-01-26 23:10:15 -05003290
James Smart7ad20aa2011-05-24 11:44:28 -04003291 /*
3292 * The outgoing buffer is readily referred from the dma buffer,
3293 * just need to get header part from mailboxq structure.
James Smart7a470272010-03-15 11:25:20 -04003294 */
James Smart7ad20aa2011-05-24 11:44:28 -04003295 pmb = (uint8_t *)&pmboxq->u.mb;
3296 pmb_buf = (uint8_t *)dd_data->context_un.mbox.mb;
3297 memcpy(pmb_buf, pmb, sizeof(MAILBOX_t));
James Smart515e0aa2010-09-29 11:19:00 -04003298
James Smarta33c4f72013-03-01 16:36:00 -05003299 /* Determine if job has been aborted */
3300
3301 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3302 job = dd_data->set_job;
3303 if (job) {
3304 /* Prevent timeout handling from trying to abort job */
3305 job->dd_data = NULL;
3306 }
3307 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3308
3309 /* Copy the mailbox data to the job if it is still active */
3310
James Smart5a6f1332011-03-11 16:05:35 -05003311 if (job) {
3312 size = job->reply_payload.payload_len;
3313 job->reply->reply_payload_rcv_len =
3314 sg_copy_from_buffer(job->reply_payload.sg_list,
James Smart7ad20aa2011-05-24 11:44:28 -04003315 job->reply_payload.sg_cnt,
3316 pmb_buf, size);
James Smart5a6f1332011-03-11 16:05:35 -05003317 }
James Smart7a470272010-03-15 11:25:20 -04003318
James Smarta33c4f72013-03-01 16:36:00 -05003319 dd_data->set_job = NULL;
James Smart3b5dd522010-01-26 23:10:15 -05003320 mempool_free(dd_data->context_un.mbox.pmboxq, phba->mbox_mem_pool);
James Smart7ad20aa2011-05-24 11:44:28 -04003321 lpfc_bsg_dma_page_free(phba, dd_data->context_un.mbox.dmabuffers);
James Smart3b5dd522010-01-26 23:10:15 -05003322 kfree(dd_data);
James Smart7ad20aa2011-05-24 11:44:28 -04003323
James Smarta33c4f72013-03-01 16:36:00 -05003324 /* Complete the job if the job is still active */
3325
James Smart7ad20aa2011-05-24 11:44:28 -04003326 if (job) {
3327 job->reply->result = 0;
3328 job->job_done(job);
3329 }
James Smart3b5dd522010-01-26 23:10:15 -05003330 return;
3331}
3332
3333/**
3334 * lpfc_bsg_check_cmd_access - test for a supported mailbox command
3335 * @phba: Pointer to HBA context object.
3336 * @mb: Pointer to a mailbox object.
3337 * @vport: Pointer to a vport object.
3338 *
3339 * Some commands require the port to be offline, some may not be called from
3340 * the application.
3341 **/
3342static int lpfc_bsg_check_cmd_access(struct lpfc_hba *phba,
3343 MAILBOX_t *mb, struct lpfc_vport *vport)
3344{
3345 /* return negative error values for bsg job */
3346 switch (mb->mbxCommand) {
3347 /* Offline only */
3348 case MBX_INIT_LINK:
3349 case MBX_DOWN_LINK:
3350 case MBX_CONFIG_LINK:
3351 case MBX_CONFIG_RING:
3352 case MBX_RESET_RING:
3353 case MBX_UNREG_LOGIN:
3354 case MBX_CLEAR_LA:
3355 case MBX_DUMP_CONTEXT:
3356 case MBX_RUN_DIAGS:
3357 case MBX_RESTART:
3358 case MBX_SET_MASK:
3359 if (!(vport->fc_flag & FC_OFFLINE_MODE)) {
3360 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3361 "2743 Command 0x%x is illegal in on-line "
3362 "state\n",
3363 mb->mbxCommand);
3364 return -EPERM;
3365 }
3366 case MBX_WRITE_NV:
3367 case MBX_WRITE_VPARMS:
3368 case MBX_LOAD_SM:
3369 case MBX_READ_NV:
3370 case MBX_READ_CONFIG:
3371 case MBX_READ_RCONFIG:
3372 case MBX_READ_STATUS:
3373 case MBX_READ_XRI:
3374 case MBX_READ_REV:
3375 case MBX_READ_LNK_STAT:
3376 case MBX_DUMP_MEMORY:
3377 case MBX_DOWN_LOAD:
3378 case MBX_UPDATE_CFG:
3379 case MBX_KILL_BOARD:
3380 case MBX_LOAD_AREA:
3381 case MBX_LOAD_EXP_ROM:
3382 case MBX_BEACON:
3383 case MBX_DEL_LD_ENTRY:
3384 case MBX_SET_DEBUG:
3385 case MBX_WRITE_WWN:
3386 case MBX_SLI4_CONFIG:
James Smartc7495932010-04-06 15:05:28 -04003387 case MBX_READ_EVENT_LOG:
James Smart3b5dd522010-01-26 23:10:15 -05003388 case MBX_READ_EVENT_LOG_STATUS:
3389 case MBX_WRITE_EVENT_LOG:
3390 case MBX_PORT_CAPABILITIES:
3391 case MBX_PORT_IOV_CONTROL:
James Smart7a470272010-03-15 11:25:20 -04003392 case MBX_RUN_BIU_DIAG64:
James Smart3b5dd522010-01-26 23:10:15 -05003393 break;
3394 case MBX_SET_VARIABLE:
James Smarte2aed292010-02-26 14:15:00 -05003395 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3396 "1226 mbox: set_variable 0x%x, 0x%x\n",
3397 mb->un.varWords[0],
3398 mb->un.varWords[1]);
3399 if ((mb->un.varWords[0] == SETVAR_MLOMNT)
3400 && (mb->un.varWords[1] == 1)) {
3401 phba->wait_4_mlo_maint_flg = 1;
3402 } else if (mb->un.varWords[0] == SETVAR_MLORST) {
James Smart1b511972011-12-13 13:23:09 -05003403 spin_lock_irq(&phba->hbalock);
James Smarte2aed292010-02-26 14:15:00 -05003404 phba->link_flag &= ~LS_LOOPBACK_MODE;
James Smart1b511972011-12-13 13:23:09 -05003405 spin_unlock_irq(&phba->hbalock);
James Smart76a95d72010-11-20 23:11:48 -05003406 phba->fc_topology = LPFC_TOPOLOGY_PT_PT;
James Smarte2aed292010-02-26 14:15:00 -05003407 }
3408 break;
James Smart3b5dd522010-01-26 23:10:15 -05003409 case MBX_READ_SPARM64:
James Smart76a95d72010-11-20 23:11:48 -05003410 case MBX_READ_TOPOLOGY:
James Smart3b5dd522010-01-26 23:10:15 -05003411 case MBX_REG_LOGIN:
3412 case MBX_REG_LOGIN64:
3413 case MBX_CONFIG_PORT:
3414 case MBX_RUN_BIU_DIAG:
3415 default:
3416 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3417 "2742 Unknown Command 0x%x\n",
3418 mb->mbxCommand);
3419 return -EPERM;
3420 }
3421
3422 return 0; /* ok */
3423}
3424
3425/**
James Smart7ad20aa2011-05-24 11:44:28 -04003426 * lpfc_bsg_mbox_ext_cleanup - clean up context of multi-buffer mbox session
3427 * @phba: Pointer to HBA context object.
3428 *
3429 * This is routine clean up and reset BSG handling of multi-buffer mbox
3430 * command session.
3431 **/
3432static void
3433lpfc_bsg_mbox_ext_session_reset(struct lpfc_hba *phba)
3434{
3435 if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_IDLE)
3436 return;
3437
3438 /* free all memory, including dma buffers */
3439 lpfc_bsg_dma_page_list_free(phba,
3440 &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
3441 lpfc_bsg_dma_page_free(phba, phba->mbox_ext_buf_ctx.mbx_dmabuf);
3442 /* multi-buffer write mailbox command pass-through complete */
3443 memset((char *)&phba->mbox_ext_buf_ctx, 0,
3444 sizeof(struct lpfc_mbox_ext_buf_ctx));
3445 INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
3446
3447 return;
3448}
3449
3450/**
3451 * lpfc_bsg_issue_mbox_ext_handle_job - job handler for multi-buffer mbox cmpl
3452 * @phba: Pointer to HBA context object.
3453 * @pmboxq: Pointer to mailbox command.
3454 *
3455 * This is routine handles BSG job for mailbox commands completions with
3456 * multiple external buffers.
3457 **/
3458static struct fc_bsg_job *
3459lpfc_bsg_issue_mbox_ext_handle_job(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
3460{
3461 struct bsg_job_data *dd_data;
3462 struct fc_bsg_job *job;
3463 uint8_t *pmb, *pmb_buf;
3464 unsigned long flags;
3465 uint32_t size;
3466 int rc = 0;
James Smart026abb82011-12-13 13:20:45 -05003467 struct lpfc_dmabuf *dmabuf;
3468 struct lpfc_sli_config_mbox *sli_cfg_mbx;
3469 uint8_t *pmbx;
James Smart7ad20aa2011-05-24 11:44:28 -04003470
James Smart7ad20aa2011-05-24 11:44:28 -04003471 dd_data = pmboxq->context1;
James Smarta33c4f72013-03-01 16:36:00 -05003472
3473 /* Determine if job has been aborted */
3474 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3475 job = dd_data->set_job;
3476 if (job) {
3477 /* Prevent timeout handling from trying to abort job */
3478 job->dd_data = NULL;
James Smart7ad20aa2011-05-24 11:44:28 -04003479 }
James Smarta33c4f72013-03-01 16:36:00 -05003480 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smart7ad20aa2011-05-24 11:44:28 -04003481
3482 /*
3483 * The outgoing buffer is readily referred from the dma buffer,
3484 * just need to get header part from mailboxq structure.
3485 */
James Smarta33c4f72013-03-01 16:36:00 -05003486
James Smart7ad20aa2011-05-24 11:44:28 -04003487 pmb = (uint8_t *)&pmboxq->u.mb;
3488 pmb_buf = (uint8_t *)dd_data->context_un.mbox.mb;
James Smart026abb82011-12-13 13:20:45 -05003489 /* Copy the byte swapped response mailbox back to the user */
James Smart7ad20aa2011-05-24 11:44:28 -04003490 memcpy(pmb_buf, pmb, sizeof(MAILBOX_t));
James Smart026abb82011-12-13 13:20:45 -05003491 /* if there is any non-embedded extended data copy that too */
3492 dmabuf = phba->mbox_ext_buf_ctx.mbx_dmabuf;
3493 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
3494 if (!bsg_bf_get(lpfc_mbox_hdr_emb,
3495 &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) {
3496 pmbx = (uint8_t *)dmabuf->virt;
3497 /* byte swap the extended data following the mailbox command */
3498 lpfc_sli_pcimem_bcopy(&pmbx[sizeof(MAILBOX_t)],
3499 &pmbx[sizeof(MAILBOX_t)],
3500 sli_cfg_mbx->un.sli_config_emb0_subsys.mse[0].buf_len);
3501 }
James Smart7ad20aa2011-05-24 11:44:28 -04003502
James Smarta33c4f72013-03-01 16:36:00 -05003503 /* Complete the job if the job is still active */
3504
James Smart7ad20aa2011-05-24 11:44:28 -04003505 if (job) {
3506 size = job->reply_payload.payload_len;
3507 job->reply->reply_payload_rcv_len =
3508 sg_copy_from_buffer(job->reply_payload.sg_list,
3509 job->reply_payload.sg_cnt,
3510 pmb_buf, size);
James Smarta33c4f72013-03-01 16:36:00 -05003511
James Smart7ad20aa2011-05-24 11:44:28 -04003512 /* result for successful */
3513 job->reply->result = 0;
James Smarta33c4f72013-03-01 16:36:00 -05003514
James Smart7ad20aa2011-05-24 11:44:28 -04003515 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3516 "2937 SLI_CONFIG ext-buffer maibox command "
3517 "(x%x/x%x) complete bsg job done, bsize:%d\n",
3518 phba->mbox_ext_buf_ctx.nembType,
3519 phba->mbox_ext_buf_ctx.mboxType, size);
James Smartb76f2dc2011-07-22 18:37:42 -04003520 lpfc_idiag_mbxacc_dump_bsg_mbox(phba,
3521 phba->mbox_ext_buf_ctx.nembType,
3522 phba->mbox_ext_buf_ctx.mboxType,
3523 dma_ebuf, sta_pos_addr,
3524 phba->mbox_ext_buf_ctx.mbx_dmabuf, 0);
James Smarta33c4f72013-03-01 16:36:00 -05003525 } else {
James Smart7ad20aa2011-05-24 11:44:28 -04003526 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3527 "2938 SLI_CONFIG ext-buffer maibox "
3528 "command (x%x/x%x) failure, rc:x%x\n",
3529 phba->mbox_ext_buf_ctx.nembType,
3530 phba->mbox_ext_buf_ctx.mboxType, rc);
James Smarta33c4f72013-03-01 16:36:00 -05003531 }
3532
3533
James Smart7ad20aa2011-05-24 11:44:28 -04003534 /* state change */
3535 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_DONE;
3536 kfree(dd_data);
James Smart7ad20aa2011-05-24 11:44:28 -04003537 return job;
3538}
3539
3540/**
3541 * lpfc_bsg_issue_read_mbox_ext_cmpl - compl handler for multi-buffer read mbox
3542 * @phba: Pointer to HBA context object.
3543 * @pmboxq: Pointer to mailbox command.
3544 *
3545 * This is completion handler function for mailbox read commands with multiple
3546 * external buffers.
3547 **/
3548static void
3549lpfc_bsg_issue_read_mbox_ext_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
3550{
3551 struct fc_bsg_job *job;
3552
James Smarta33c4f72013-03-01 16:36:00 -05003553 job = lpfc_bsg_issue_mbox_ext_handle_job(phba, pmboxq);
3554
James Smart7ad20aa2011-05-24 11:44:28 -04003555 /* handle the BSG job with mailbox command */
James Smarta33c4f72013-03-01 16:36:00 -05003556 if (!job)
James Smart7ad20aa2011-05-24 11:44:28 -04003557 pmboxq->u.mb.mbxStatus = MBXERR_ERROR;
3558
3559 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3560 "2939 SLI_CONFIG ext-buffer rd maibox command "
3561 "complete, ctxState:x%x, mbxStatus:x%x\n",
3562 phba->mbox_ext_buf_ctx.state, pmboxq->u.mb.mbxStatus);
3563
James Smart7ad20aa2011-05-24 11:44:28 -04003564 if (pmboxq->u.mb.mbxStatus || phba->mbox_ext_buf_ctx.numBuf == 1)
3565 lpfc_bsg_mbox_ext_session_reset(phba);
3566
3567 /* free base driver mailbox structure memory */
3568 mempool_free(pmboxq, phba->mbox_mem_pool);
3569
James Smarta33c4f72013-03-01 16:36:00 -05003570 /* if the job is still active, call job done */
James Smart7ad20aa2011-05-24 11:44:28 -04003571 if (job)
3572 job->job_done(job);
3573
3574 return;
3575}
3576
3577/**
3578 * lpfc_bsg_issue_write_mbox_ext_cmpl - cmpl handler for multi-buffer write mbox
3579 * @phba: Pointer to HBA context object.
3580 * @pmboxq: Pointer to mailbox command.
3581 *
3582 * This is completion handler function for mailbox write commands with multiple
3583 * external buffers.
3584 **/
3585static void
3586lpfc_bsg_issue_write_mbox_ext_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
3587{
3588 struct fc_bsg_job *job;
3589
James Smarta33c4f72013-03-01 16:36:00 -05003590 job = lpfc_bsg_issue_mbox_ext_handle_job(phba, pmboxq);
3591
James Smart7ad20aa2011-05-24 11:44:28 -04003592 /* handle the BSG job with the mailbox command */
James Smarta33c4f72013-03-01 16:36:00 -05003593 if (!job)
James Smart7ad20aa2011-05-24 11:44:28 -04003594 pmboxq->u.mb.mbxStatus = MBXERR_ERROR;
3595
3596 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3597 "2940 SLI_CONFIG ext-buffer wr maibox command "
3598 "complete, ctxState:x%x, mbxStatus:x%x\n",
3599 phba->mbox_ext_buf_ctx.state, pmboxq->u.mb.mbxStatus);
3600
James Smart7ad20aa2011-05-24 11:44:28 -04003601 /* free all memory, including dma buffers */
3602 mempool_free(pmboxq, phba->mbox_mem_pool);
3603 lpfc_bsg_mbox_ext_session_reset(phba);
3604
James Smarta33c4f72013-03-01 16:36:00 -05003605 /* if the job is still active, call job done */
James Smart7ad20aa2011-05-24 11:44:28 -04003606 if (job)
3607 job->job_done(job);
3608
3609 return;
3610}
3611
3612static void
3613lpfc_bsg_sli_cfg_dma_desc_setup(struct lpfc_hba *phba, enum nemb_type nemb_tp,
3614 uint32_t index, struct lpfc_dmabuf *mbx_dmabuf,
3615 struct lpfc_dmabuf *ext_dmabuf)
3616{
3617 struct lpfc_sli_config_mbox *sli_cfg_mbx;
3618
3619 /* pointer to the start of mailbox command */
3620 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)mbx_dmabuf->virt;
3621
3622 if (nemb_tp == nemb_mse) {
3623 if (index == 0) {
3624 sli_cfg_mbx->un.sli_config_emb0_subsys.
3625 mse[index].pa_hi =
3626 putPaddrHigh(mbx_dmabuf->phys +
3627 sizeof(MAILBOX_t));
3628 sli_cfg_mbx->un.sli_config_emb0_subsys.
3629 mse[index].pa_lo =
3630 putPaddrLow(mbx_dmabuf->phys +
3631 sizeof(MAILBOX_t));
3632 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3633 "2943 SLI_CONFIG(mse)[%d], "
3634 "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3635 index,
3636 sli_cfg_mbx->un.sli_config_emb0_subsys.
3637 mse[index].buf_len,
3638 sli_cfg_mbx->un.sli_config_emb0_subsys.
3639 mse[index].pa_hi,
3640 sli_cfg_mbx->un.sli_config_emb0_subsys.
3641 mse[index].pa_lo);
3642 } else {
3643 sli_cfg_mbx->un.sli_config_emb0_subsys.
3644 mse[index].pa_hi =
3645 putPaddrHigh(ext_dmabuf->phys);
3646 sli_cfg_mbx->un.sli_config_emb0_subsys.
3647 mse[index].pa_lo =
3648 putPaddrLow(ext_dmabuf->phys);
3649 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3650 "2944 SLI_CONFIG(mse)[%d], "
3651 "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3652 index,
3653 sli_cfg_mbx->un.sli_config_emb0_subsys.
3654 mse[index].buf_len,
3655 sli_cfg_mbx->un.sli_config_emb0_subsys.
3656 mse[index].pa_hi,
3657 sli_cfg_mbx->un.sli_config_emb0_subsys.
3658 mse[index].pa_lo);
3659 }
3660 } else {
3661 if (index == 0) {
3662 sli_cfg_mbx->un.sli_config_emb1_subsys.
3663 hbd[index].pa_hi =
3664 putPaddrHigh(mbx_dmabuf->phys +
3665 sizeof(MAILBOX_t));
3666 sli_cfg_mbx->un.sli_config_emb1_subsys.
3667 hbd[index].pa_lo =
3668 putPaddrLow(mbx_dmabuf->phys +
3669 sizeof(MAILBOX_t));
3670 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3671 "3007 SLI_CONFIG(hbd)[%d], "
3672 "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3673 index,
3674 bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
3675 &sli_cfg_mbx->un.
3676 sli_config_emb1_subsys.hbd[index]),
3677 sli_cfg_mbx->un.sli_config_emb1_subsys.
3678 hbd[index].pa_hi,
3679 sli_cfg_mbx->un.sli_config_emb1_subsys.
3680 hbd[index].pa_lo);
3681
3682 } else {
3683 sli_cfg_mbx->un.sli_config_emb1_subsys.
3684 hbd[index].pa_hi =
3685 putPaddrHigh(ext_dmabuf->phys);
3686 sli_cfg_mbx->un.sli_config_emb1_subsys.
3687 hbd[index].pa_lo =
3688 putPaddrLow(ext_dmabuf->phys);
3689 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3690 "3008 SLI_CONFIG(hbd)[%d], "
3691 "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3692 index,
3693 bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
3694 &sli_cfg_mbx->un.
3695 sli_config_emb1_subsys.hbd[index]),
3696 sli_cfg_mbx->un.sli_config_emb1_subsys.
3697 hbd[index].pa_hi,
3698 sli_cfg_mbx->un.sli_config_emb1_subsys.
3699 hbd[index].pa_lo);
3700 }
3701 }
3702 return;
3703}
3704
3705/**
3706 * lpfc_bsg_sli_cfg_mse_read_cmd_ext - sli_config non-embedded mailbox cmd read
3707 * @phba: Pointer to HBA context object.
3708 * @mb: Pointer to a BSG mailbox object.
3709 * @nemb_tp: Enumerate of non-embedded mailbox command type.
3710 * @dmabuff: Pointer to a DMA buffer descriptor.
3711 *
3712 * This routine performs SLI_CONFIG (0x9B) read mailbox command operation with
3713 * non-embedded external bufffers.
3714 **/
3715static int
3716lpfc_bsg_sli_cfg_read_cmd_ext(struct lpfc_hba *phba, struct fc_bsg_job *job,
3717 enum nemb_type nemb_tp,
3718 struct lpfc_dmabuf *dmabuf)
3719{
3720 struct lpfc_sli_config_mbox *sli_cfg_mbx;
3721 struct dfc_mbox_req *mbox_req;
3722 struct lpfc_dmabuf *curr_dmabuf, *next_dmabuf;
3723 uint32_t ext_buf_cnt, ext_buf_index;
3724 struct lpfc_dmabuf *ext_dmabuf = NULL;
3725 struct bsg_job_data *dd_data = NULL;
3726 LPFC_MBOXQ_t *pmboxq = NULL;
3727 MAILBOX_t *pmb;
3728 uint8_t *pmbx;
3729 int rc, i;
3730
3731 mbox_req =
3732 (struct dfc_mbox_req *)job->request->rqst_data.h_vendor.vendor_cmd;
3733
3734 /* pointer to the start of mailbox command */
3735 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
3736
3737 if (nemb_tp == nemb_mse) {
3738 ext_buf_cnt = bsg_bf_get(lpfc_mbox_hdr_mse_cnt,
3739 &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr);
3740 if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_MSE) {
3741 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3742 "2945 Handled SLI_CONFIG(mse) rd, "
3743 "ext_buf_cnt(%d) out of range(%d)\n",
3744 ext_buf_cnt,
3745 LPFC_MBX_SLI_CONFIG_MAX_MSE);
3746 rc = -ERANGE;
3747 goto job_error;
3748 }
3749 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3750 "2941 Handled SLI_CONFIG(mse) rd, "
3751 "ext_buf_cnt:%d\n", ext_buf_cnt);
3752 } else {
3753 /* sanity check on interface type for support */
3754 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
3755 LPFC_SLI_INTF_IF_TYPE_2) {
3756 rc = -ENODEV;
3757 goto job_error;
3758 }
3759 /* nemb_tp == nemb_hbd */
3760 ext_buf_cnt = sli_cfg_mbx->un.sli_config_emb1_subsys.hbd_count;
3761 if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_HBD) {
3762 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3763 "2946 Handled SLI_CONFIG(hbd) rd, "
3764 "ext_buf_cnt(%d) out of range(%d)\n",
3765 ext_buf_cnt,
3766 LPFC_MBX_SLI_CONFIG_MAX_HBD);
3767 rc = -ERANGE;
3768 goto job_error;
3769 }
3770 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3771 "2942 Handled SLI_CONFIG(hbd) rd, "
3772 "ext_buf_cnt:%d\n", ext_buf_cnt);
3773 }
3774
James Smartb76f2dc2011-07-22 18:37:42 -04003775 /* before dma descriptor setup */
3776 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_rd, dma_mbox,
3777 sta_pre_addr, dmabuf, ext_buf_cnt);
3778
James Smart7ad20aa2011-05-24 11:44:28 -04003779 /* reject non-embedded mailbox command with none external buffer */
3780 if (ext_buf_cnt == 0) {
3781 rc = -EPERM;
3782 goto job_error;
3783 } else if (ext_buf_cnt > 1) {
3784 /* additional external read buffers */
3785 for (i = 1; i < ext_buf_cnt; i++) {
3786 ext_dmabuf = lpfc_bsg_dma_page_alloc(phba);
3787 if (!ext_dmabuf) {
3788 rc = -ENOMEM;
3789 goto job_error;
3790 }
3791 list_add_tail(&ext_dmabuf->list,
3792 &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
3793 }
3794 }
3795
3796 /* bsg tracking structure */
3797 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
3798 if (!dd_data) {
3799 rc = -ENOMEM;
3800 goto job_error;
3801 }
3802
3803 /* mailbox command structure for base driver */
3804 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3805 if (!pmboxq) {
3806 rc = -ENOMEM;
3807 goto job_error;
3808 }
3809 memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
3810
3811 /* for the first external buffer */
3812 lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, 0, dmabuf, dmabuf);
3813
3814 /* for the rest of external buffer descriptors if any */
3815 if (ext_buf_cnt > 1) {
3816 ext_buf_index = 1;
3817 list_for_each_entry_safe(curr_dmabuf, next_dmabuf,
3818 &phba->mbox_ext_buf_ctx.ext_dmabuf_list, list) {
3819 lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp,
3820 ext_buf_index, dmabuf,
3821 curr_dmabuf);
3822 ext_buf_index++;
3823 }
3824 }
3825
James Smartb76f2dc2011-07-22 18:37:42 -04003826 /* after dma descriptor setup */
3827 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_rd, dma_mbox,
3828 sta_pos_addr, dmabuf, ext_buf_cnt);
3829
James Smart7ad20aa2011-05-24 11:44:28 -04003830 /* construct base driver mbox command */
3831 pmb = &pmboxq->u.mb;
3832 pmbx = (uint8_t *)dmabuf->virt;
3833 memcpy(pmb, pmbx, sizeof(*pmb));
3834 pmb->mbxOwner = OWN_HOST;
3835 pmboxq->vport = phba->pport;
3836
3837 /* multi-buffer handling context */
3838 phba->mbox_ext_buf_ctx.nembType = nemb_tp;
3839 phba->mbox_ext_buf_ctx.mboxType = mbox_rd;
3840 phba->mbox_ext_buf_ctx.numBuf = ext_buf_cnt;
3841 phba->mbox_ext_buf_ctx.mbxTag = mbox_req->extMboxTag;
3842 phba->mbox_ext_buf_ctx.seqNum = mbox_req->extSeqNum;
3843 phba->mbox_ext_buf_ctx.mbx_dmabuf = dmabuf;
3844
3845 /* callback for multi-buffer read mailbox command */
3846 pmboxq->mbox_cmpl = lpfc_bsg_issue_read_mbox_ext_cmpl;
3847
3848 /* context fields to callback function */
3849 pmboxq->context1 = dd_data;
3850 dd_data->type = TYPE_MBOX;
James Smarta33c4f72013-03-01 16:36:00 -05003851 dd_data->set_job = job;
James Smart7ad20aa2011-05-24 11:44:28 -04003852 dd_data->context_un.mbox.pmboxq = pmboxq;
3853 dd_data->context_un.mbox.mb = (MAILBOX_t *)pmbx;
James Smart7ad20aa2011-05-24 11:44:28 -04003854 job->dd_data = dd_data;
3855
3856 /* state change */
3857 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
3858
James Smart026abb82011-12-13 13:20:45 -05003859 /*
3860 * Non-embedded mailbox subcommand data gets byte swapped here because
3861 * the lower level driver code only does the first 64 mailbox words.
3862 */
3863 if ((!bsg_bf_get(lpfc_mbox_hdr_emb,
3864 &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) &&
3865 (nemb_tp == nemb_mse))
3866 lpfc_sli_pcimem_bcopy(&pmbx[sizeof(MAILBOX_t)],
3867 &pmbx[sizeof(MAILBOX_t)],
3868 sli_cfg_mbx->un.sli_config_emb0_subsys.
3869 mse[0].buf_len);
3870
James Smart7ad20aa2011-05-24 11:44:28 -04003871 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
3872 if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
3873 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3874 "2947 Issued SLI_CONFIG ext-buffer "
3875 "maibox command, rc:x%x\n", rc);
James Smart88a2cfb2011-07-22 18:36:33 -04003876 return SLI_CONFIG_HANDLED;
James Smart7ad20aa2011-05-24 11:44:28 -04003877 }
3878 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3879 "2948 Failed to issue SLI_CONFIG ext-buffer "
3880 "maibox command, rc:x%x\n", rc);
3881 rc = -EPIPE;
3882
3883job_error:
3884 if (pmboxq)
3885 mempool_free(pmboxq, phba->mbox_mem_pool);
3886 lpfc_bsg_dma_page_list_free(phba,
3887 &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
3888 kfree(dd_data);
3889 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_IDLE;
3890 return rc;
3891}
3892
3893/**
3894 * lpfc_bsg_sli_cfg_write_cmd_ext - sli_config non-embedded mailbox cmd write
3895 * @phba: Pointer to HBA context object.
3896 * @mb: Pointer to a BSG mailbox object.
3897 * @dmabuff: Pointer to a DMA buffer descriptor.
3898 *
3899 * This routine performs SLI_CONFIG (0x9B) write mailbox command operation with
3900 * non-embedded external bufffers.
3901 **/
3902static int
3903lpfc_bsg_sli_cfg_write_cmd_ext(struct lpfc_hba *phba, struct fc_bsg_job *job,
3904 enum nemb_type nemb_tp,
3905 struct lpfc_dmabuf *dmabuf)
3906{
3907 struct dfc_mbox_req *mbox_req;
3908 struct lpfc_sli_config_mbox *sli_cfg_mbx;
3909 uint32_t ext_buf_cnt;
3910 struct bsg_job_data *dd_data = NULL;
3911 LPFC_MBOXQ_t *pmboxq = NULL;
3912 MAILBOX_t *pmb;
3913 uint8_t *mbx;
James Smart88a2cfb2011-07-22 18:36:33 -04003914 int rc = SLI_CONFIG_NOT_HANDLED, i;
James Smart7ad20aa2011-05-24 11:44:28 -04003915
3916 mbox_req =
3917 (struct dfc_mbox_req *)job->request->rqst_data.h_vendor.vendor_cmd;
3918
3919 /* pointer to the start of mailbox command */
3920 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
3921
3922 if (nemb_tp == nemb_mse) {
3923 ext_buf_cnt = bsg_bf_get(lpfc_mbox_hdr_mse_cnt,
3924 &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr);
3925 if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_MSE) {
3926 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
James Smart026abb82011-12-13 13:20:45 -05003927 "2953 Failed SLI_CONFIG(mse) wr, "
James Smart7ad20aa2011-05-24 11:44:28 -04003928 "ext_buf_cnt(%d) out of range(%d)\n",
3929 ext_buf_cnt,
3930 LPFC_MBX_SLI_CONFIG_MAX_MSE);
3931 return -ERANGE;
3932 }
3933 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3934 "2949 Handled SLI_CONFIG(mse) wr, "
3935 "ext_buf_cnt:%d\n", ext_buf_cnt);
3936 } else {
3937 /* sanity check on interface type for support */
3938 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
3939 LPFC_SLI_INTF_IF_TYPE_2)
3940 return -ENODEV;
3941 /* nemb_tp == nemb_hbd */
3942 ext_buf_cnt = sli_cfg_mbx->un.sli_config_emb1_subsys.hbd_count;
3943 if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_HBD) {
3944 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
James Smart026abb82011-12-13 13:20:45 -05003945 "2954 Failed SLI_CONFIG(hbd) wr, "
James Smart7ad20aa2011-05-24 11:44:28 -04003946 "ext_buf_cnt(%d) out of range(%d)\n",
3947 ext_buf_cnt,
3948 LPFC_MBX_SLI_CONFIG_MAX_HBD);
3949 return -ERANGE;
3950 }
3951 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3952 "2950 Handled SLI_CONFIG(hbd) wr, "
3953 "ext_buf_cnt:%d\n", ext_buf_cnt);
3954 }
3955
James Smartb76f2dc2011-07-22 18:37:42 -04003956 /* before dma buffer descriptor setup */
3957 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_wr, dma_mbox,
3958 sta_pre_addr, dmabuf, ext_buf_cnt);
3959
James Smart7ad20aa2011-05-24 11:44:28 -04003960 if (ext_buf_cnt == 0)
3961 return -EPERM;
3962
3963 /* for the first external buffer */
3964 lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, 0, dmabuf, dmabuf);
3965
James Smartb76f2dc2011-07-22 18:37:42 -04003966 /* after dma descriptor setup */
3967 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_wr, dma_mbox,
3968 sta_pos_addr, dmabuf, ext_buf_cnt);
3969
James Smart7ad20aa2011-05-24 11:44:28 -04003970 /* log for looking forward */
3971 for (i = 1; i < ext_buf_cnt; i++) {
3972 if (nemb_tp == nemb_mse)
3973 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3974 "2951 SLI_CONFIG(mse), buf[%d]-length:%d\n",
3975 i, sli_cfg_mbx->un.sli_config_emb0_subsys.
3976 mse[i].buf_len);
3977 else
3978 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3979 "2952 SLI_CONFIG(hbd), buf[%d]-length:%d\n",
3980 i, bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
3981 &sli_cfg_mbx->un.sli_config_emb1_subsys.
3982 hbd[i]));
3983 }
3984
3985 /* multi-buffer handling context */
3986 phba->mbox_ext_buf_ctx.nembType = nemb_tp;
3987 phba->mbox_ext_buf_ctx.mboxType = mbox_wr;
3988 phba->mbox_ext_buf_ctx.numBuf = ext_buf_cnt;
3989 phba->mbox_ext_buf_ctx.mbxTag = mbox_req->extMboxTag;
3990 phba->mbox_ext_buf_ctx.seqNum = mbox_req->extSeqNum;
3991 phba->mbox_ext_buf_ctx.mbx_dmabuf = dmabuf;
3992
3993 if (ext_buf_cnt == 1) {
3994 /* bsg tracking structure */
3995 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
3996 if (!dd_data) {
3997 rc = -ENOMEM;
3998 goto job_error;
3999 }
4000
4001 /* mailbox command structure for base driver */
4002 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4003 if (!pmboxq) {
4004 rc = -ENOMEM;
4005 goto job_error;
4006 }
4007 memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
4008 pmb = &pmboxq->u.mb;
4009 mbx = (uint8_t *)dmabuf->virt;
4010 memcpy(pmb, mbx, sizeof(*pmb));
4011 pmb->mbxOwner = OWN_HOST;
4012 pmboxq->vport = phba->pport;
4013
4014 /* callback for multi-buffer read mailbox command */
4015 pmboxq->mbox_cmpl = lpfc_bsg_issue_write_mbox_ext_cmpl;
4016
4017 /* context fields to callback function */
4018 pmboxq->context1 = dd_data;
4019 dd_data->type = TYPE_MBOX;
James Smarta33c4f72013-03-01 16:36:00 -05004020 dd_data->set_job = job;
James Smart7ad20aa2011-05-24 11:44:28 -04004021 dd_data->context_un.mbox.pmboxq = pmboxq;
4022 dd_data->context_un.mbox.mb = (MAILBOX_t *)mbx;
James Smart7ad20aa2011-05-24 11:44:28 -04004023 job->dd_data = dd_data;
4024
4025 /* state change */
James Smart7ad20aa2011-05-24 11:44:28 -04004026
James Smarta33c4f72013-03-01 16:36:00 -05004027 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
James Smart7ad20aa2011-05-24 11:44:28 -04004028 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
4029 if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
4030 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4031 "2955 Issued SLI_CONFIG ext-buffer "
4032 "maibox command, rc:x%x\n", rc);
James Smart88a2cfb2011-07-22 18:36:33 -04004033 return SLI_CONFIG_HANDLED;
James Smart7ad20aa2011-05-24 11:44:28 -04004034 }
4035 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4036 "2956 Failed to issue SLI_CONFIG ext-buffer "
4037 "maibox command, rc:x%x\n", rc);
4038 rc = -EPIPE;
James Smart026abb82011-12-13 13:20:45 -05004039 goto job_error;
James Smart7ad20aa2011-05-24 11:44:28 -04004040 }
4041
James Smart88a2cfb2011-07-22 18:36:33 -04004042 /* wait for additoinal external buffers */
James Smarta33c4f72013-03-01 16:36:00 -05004043
James Smart88a2cfb2011-07-22 18:36:33 -04004044 job->reply->result = 0;
4045 job->job_done(job);
4046 return SLI_CONFIG_HANDLED;
4047
James Smart7ad20aa2011-05-24 11:44:28 -04004048job_error:
4049 if (pmboxq)
4050 mempool_free(pmboxq, phba->mbox_mem_pool);
4051 kfree(dd_data);
4052
4053 return rc;
4054}
4055
4056/**
4057 * lpfc_bsg_handle_sli_cfg_mbox - handle sli-cfg mailbox cmd with ext buffer
4058 * @phba: Pointer to HBA context object.
4059 * @mb: Pointer to a BSG mailbox object.
4060 * @dmabuff: Pointer to a DMA buffer descriptor.
4061 *
4062 * This routine handles SLI_CONFIG (0x9B) mailbox command with non-embedded
4063 * external bufffers, including both 0x9B with non-embedded MSEs and 0x9B
4064 * with embedded sussystem 0x1 and opcodes with external HBDs.
4065 **/
4066static int
4067lpfc_bsg_handle_sli_cfg_mbox(struct lpfc_hba *phba, struct fc_bsg_job *job,
4068 struct lpfc_dmabuf *dmabuf)
4069{
4070 struct lpfc_sli_config_mbox *sli_cfg_mbx;
4071 uint32_t subsys;
4072 uint32_t opcode;
4073 int rc = SLI_CONFIG_NOT_HANDLED;
4074
James Smart026abb82011-12-13 13:20:45 -05004075 /* state change on new multi-buffer pass-through mailbox command */
James Smart7ad20aa2011-05-24 11:44:28 -04004076 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_HOST;
4077
4078 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
4079
4080 if (!bsg_bf_get(lpfc_mbox_hdr_emb,
4081 &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) {
4082 subsys = bsg_bf_get(lpfc_emb0_subcmnd_subsys,
4083 &sli_cfg_mbx->un.sli_config_emb0_subsys);
4084 opcode = bsg_bf_get(lpfc_emb0_subcmnd_opcode,
4085 &sli_cfg_mbx->un.sli_config_emb0_subsys);
4086 if (subsys == SLI_CONFIG_SUBSYS_FCOE) {
4087 switch (opcode) {
4088 case FCOE_OPCODE_READ_FCF:
4089 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4090 "2957 Handled SLI_CONFIG "
4091 "subsys_fcoe, opcode:x%x\n",
4092 opcode);
4093 rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
4094 nemb_mse, dmabuf);
4095 break;
4096 case FCOE_OPCODE_ADD_FCF:
4097 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4098 "2958 Handled SLI_CONFIG "
4099 "subsys_fcoe, opcode:x%x\n",
4100 opcode);
4101 rc = lpfc_bsg_sli_cfg_write_cmd_ext(phba, job,
4102 nemb_mse, dmabuf);
4103 break;
4104 default:
4105 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
James Smart026abb82011-12-13 13:20:45 -05004106 "2959 Reject SLI_CONFIG "
James Smart7ad20aa2011-05-24 11:44:28 -04004107 "subsys_fcoe, opcode:x%x\n",
4108 opcode);
James Smart026abb82011-12-13 13:20:45 -05004109 rc = -EPERM;
4110 break;
4111 }
4112 } else if (subsys == SLI_CONFIG_SUBSYS_COMN) {
4113 switch (opcode) {
4114 case COMN_OPCODE_GET_CNTL_ADDL_ATTRIBUTES:
James Smartb99570d2012-05-09 21:16:24 -04004115 case COMN_OPCODE_GET_CNTL_ATTRIBUTES:
James Smart026abb82011-12-13 13:20:45 -05004116 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4117 "3106 Handled SLI_CONFIG "
James Smart6b5151f2012-01-18 16:24:06 -05004118 "subsys_comn, opcode:x%x\n",
James Smart026abb82011-12-13 13:20:45 -05004119 opcode);
4120 rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
4121 nemb_mse, dmabuf);
4122 break;
4123 default:
4124 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4125 "3107 Reject SLI_CONFIG "
James Smart6b5151f2012-01-18 16:24:06 -05004126 "subsys_comn, opcode:x%x\n",
James Smart026abb82011-12-13 13:20:45 -05004127 opcode);
4128 rc = -EPERM;
James Smart7ad20aa2011-05-24 11:44:28 -04004129 break;
4130 }
4131 } else {
4132 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
James Smart026abb82011-12-13 13:20:45 -05004133 "2977 Reject SLI_CONFIG "
James Smart7ad20aa2011-05-24 11:44:28 -04004134 "subsys:x%d, opcode:x%x\n",
4135 subsys, opcode);
James Smart026abb82011-12-13 13:20:45 -05004136 rc = -EPERM;
James Smart7ad20aa2011-05-24 11:44:28 -04004137 }
4138 } else {
4139 subsys = bsg_bf_get(lpfc_emb1_subcmnd_subsys,
4140 &sli_cfg_mbx->un.sli_config_emb1_subsys);
4141 opcode = bsg_bf_get(lpfc_emb1_subcmnd_opcode,
4142 &sli_cfg_mbx->un.sli_config_emb1_subsys);
4143 if (subsys == SLI_CONFIG_SUBSYS_COMN) {
4144 switch (opcode) {
4145 case COMN_OPCODE_READ_OBJECT:
4146 case COMN_OPCODE_READ_OBJECT_LIST:
4147 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4148 "2960 Handled SLI_CONFIG "
4149 "subsys_comn, opcode:x%x\n",
4150 opcode);
4151 rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
4152 nemb_hbd, dmabuf);
4153 break;
4154 case COMN_OPCODE_WRITE_OBJECT:
4155 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4156 "2961 Handled SLI_CONFIG "
4157 "subsys_comn, opcode:x%x\n",
4158 opcode);
4159 rc = lpfc_bsg_sli_cfg_write_cmd_ext(phba, job,
4160 nemb_hbd, dmabuf);
4161 break;
4162 default:
4163 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4164 "2962 Not handled SLI_CONFIG "
4165 "subsys_comn, opcode:x%x\n",
4166 opcode);
4167 rc = SLI_CONFIG_NOT_HANDLED;
4168 break;
4169 }
4170 } else {
4171 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
James Smart026abb82011-12-13 13:20:45 -05004172 "2978 Not handled SLI_CONFIG "
James Smart7ad20aa2011-05-24 11:44:28 -04004173 "subsys:x%d, opcode:x%x\n",
4174 subsys, opcode);
4175 rc = SLI_CONFIG_NOT_HANDLED;
4176 }
4177 }
James Smart026abb82011-12-13 13:20:45 -05004178
4179 /* state reset on not handled new multi-buffer mailbox command */
4180 if (rc != SLI_CONFIG_HANDLED)
4181 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_IDLE;
4182
James Smart7ad20aa2011-05-24 11:44:28 -04004183 return rc;
4184}
4185
4186/**
4187 * lpfc_bsg_mbox_ext_abort_req - request to abort mbox command with ext buffers
4188 * @phba: Pointer to HBA context object.
4189 *
4190 * This routine is for requesting to abort a pass-through mailbox command with
4191 * multiple external buffers due to error condition.
4192 **/
4193static void
4194lpfc_bsg_mbox_ext_abort(struct lpfc_hba *phba)
4195{
4196 if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_PORT)
4197 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_ABTS;
4198 else
4199 lpfc_bsg_mbox_ext_session_reset(phba);
4200 return;
4201}
4202
4203/**
4204 * lpfc_bsg_read_ebuf_get - get the next mailbox read external buffer
4205 * @phba: Pointer to HBA context object.
4206 * @dmabuf: Pointer to a DMA buffer descriptor.
4207 *
4208 * This routine extracts the next mailbox read external buffer back to
4209 * user space through BSG.
4210 **/
4211static int
4212lpfc_bsg_read_ebuf_get(struct lpfc_hba *phba, struct fc_bsg_job *job)
4213{
4214 struct lpfc_sli_config_mbox *sli_cfg_mbx;
4215 struct lpfc_dmabuf *dmabuf;
4216 uint8_t *pbuf;
4217 uint32_t size;
4218 uint32_t index;
4219
4220 index = phba->mbox_ext_buf_ctx.seqNum;
4221 phba->mbox_ext_buf_ctx.seqNum++;
4222
4223 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)
4224 phba->mbox_ext_buf_ctx.mbx_dmabuf->virt;
4225
4226 if (phba->mbox_ext_buf_ctx.nembType == nemb_mse) {
4227 size = bsg_bf_get(lpfc_mbox_sli_config_mse_len,
4228 &sli_cfg_mbx->un.sli_config_emb0_subsys.mse[index]);
4229 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4230 "2963 SLI_CONFIG (mse) ext-buffer rd get "
4231 "buffer[%d], size:%d\n", index, size);
4232 } else {
4233 size = bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
4234 &sli_cfg_mbx->un.sli_config_emb1_subsys.hbd[index]);
4235 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4236 "2964 SLI_CONFIG (hbd) ext-buffer rd get "
4237 "buffer[%d], size:%d\n", index, size);
4238 }
4239 if (list_empty(&phba->mbox_ext_buf_ctx.ext_dmabuf_list))
4240 return -EPIPE;
4241 dmabuf = list_first_entry(&phba->mbox_ext_buf_ctx.ext_dmabuf_list,
4242 struct lpfc_dmabuf, list);
4243 list_del_init(&dmabuf->list);
James Smartb76f2dc2011-07-22 18:37:42 -04004244
4245 /* after dma buffer descriptor setup */
4246 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, phba->mbox_ext_buf_ctx.nembType,
4247 mbox_rd, dma_ebuf, sta_pos_addr,
4248 dmabuf, index);
4249
James Smart7ad20aa2011-05-24 11:44:28 -04004250 pbuf = (uint8_t *)dmabuf->virt;
4251 job->reply->reply_payload_rcv_len =
4252 sg_copy_from_buffer(job->reply_payload.sg_list,
4253 job->reply_payload.sg_cnt,
4254 pbuf, size);
4255
4256 lpfc_bsg_dma_page_free(phba, dmabuf);
4257
4258 if (phba->mbox_ext_buf_ctx.seqNum == phba->mbox_ext_buf_ctx.numBuf) {
4259 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4260 "2965 SLI_CONFIG (hbd) ext-buffer rd mbox "
4261 "command session done\n");
4262 lpfc_bsg_mbox_ext_session_reset(phba);
4263 }
4264
4265 job->reply->result = 0;
4266 job->job_done(job);
4267
4268 return SLI_CONFIG_HANDLED;
4269}
4270
4271/**
4272 * lpfc_bsg_write_ebuf_set - set the next mailbox write external buffer
4273 * @phba: Pointer to HBA context object.
4274 * @dmabuf: Pointer to a DMA buffer descriptor.
4275 *
4276 * This routine sets up the next mailbox read external buffer obtained
4277 * from user space through BSG.
4278 **/
4279static int
4280lpfc_bsg_write_ebuf_set(struct lpfc_hba *phba, struct fc_bsg_job *job,
4281 struct lpfc_dmabuf *dmabuf)
4282{
4283 struct lpfc_sli_config_mbox *sli_cfg_mbx;
4284 struct bsg_job_data *dd_data = NULL;
4285 LPFC_MBOXQ_t *pmboxq = NULL;
4286 MAILBOX_t *pmb;
4287 enum nemb_type nemb_tp;
4288 uint8_t *pbuf;
4289 uint32_t size;
4290 uint32_t index;
4291 int rc;
4292
4293 index = phba->mbox_ext_buf_ctx.seqNum;
4294 phba->mbox_ext_buf_ctx.seqNum++;
4295 nemb_tp = phba->mbox_ext_buf_ctx.nembType;
4296
4297 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)
4298 phba->mbox_ext_buf_ctx.mbx_dmabuf->virt;
4299
4300 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
4301 if (!dd_data) {
4302 rc = -ENOMEM;
4303 goto job_error;
4304 }
4305
4306 pbuf = (uint8_t *)dmabuf->virt;
4307 size = job->request_payload.payload_len;
4308 sg_copy_to_buffer(job->request_payload.sg_list,
4309 job->request_payload.sg_cnt,
4310 pbuf, size);
4311
4312 if (phba->mbox_ext_buf_ctx.nembType == nemb_mse) {
4313 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4314 "2966 SLI_CONFIG (mse) ext-buffer wr set "
4315 "buffer[%d], size:%d\n",
4316 phba->mbox_ext_buf_ctx.seqNum, size);
4317
4318 } else {
4319 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4320 "2967 SLI_CONFIG (hbd) ext-buffer wr set "
4321 "buffer[%d], size:%d\n",
4322 phba->mbox_ext_buf_ctx.seqNum, size);
4323
4324 }
4325
4326 /* set up external buffer descriptor and add to external buffer list */
4327 lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, index,
4328 phba->mbox_ext_buf_ctx.mbx_dmabuf,
4329 dmabuf);
4330 list_add_tail(&dmabuf->list, &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
4331
James Smartb76f2dc2011-07-22 18:37:42 -04004332 /* after write dma buffer */
4333 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, phba->mbox_ext_buf_ctx.nembType,
4334 mbox_wr, dma_ebuf, sta_pos_addr,
4335 dmabuf, index);
4336
James Smart7ad20aa2011-05-24 11:44:28 -04004337 if (phba->mbox_ext_buf_ctx.seqNum == phba->mbox_ext_buf_ctx.numBuf) {
4338 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4339 "2968 SLI_CONFIG ext-buffer wr all %d "
4340 "ebuffers received\n",
4341 phba->mbox_ext_buf_ctx.numBuf);
4342 /* mailbox command structure for base driver */
4343 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4344 if (!pmboxq) {
4345 rc = -ENOMEM;
4346 goto job_error;
4347 }
4348 memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
4349 pbuf = (uint8_t *)phba->mbox_ext_buf_ctx.mbx_dmabuf->virt;
4350 pmb = &pmboxq->u.mb;
4351 memcpy(pmb, pbuf, sizeof(*pmb));
4352 pmb->mbxOwner = OWN_HOST;
4353 pmboxq->vport = phba->pport;
4354
4355 /* callback for multi-buffer write mailbox command */
4356 pmboxq->mbox_cmpl = lpfc_bsg_issue_write_mbox_ext_cmpl;
4357
4358 /* context fields to callback function */
4359 pmboxq->context1 = dd_data;
4360 dd_data->type = TYPE_MBOX;
James Smarta33c4f72013-03-01 16:36:00 -05004361 dd_data->set_job = job;
James Smart7ad20aa2011-05-24 11:44:28 -04004362 dd_data->context_un.mbox.pmboxq = pmboxq;
4363 dd_data->context_un.mbox.mb = (MAILBOX_t *)pbuf;
James Smart7ad20aa2011-05-24 11:44:28 -04004364 job->dd_data = dd_data;
4365
4366 /* state change */
4367 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
4368
4369 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
4370 if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
4371 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4372 "2969 Issued SLI_CONFIG ext-buffer "
4373 "maibox command, rc:x%x\n", rc);
James Smart88a2cfb2011-07-22 18:36:33 -04004374 return SLI_CONFIG_HANDLED;
James Smart7ad20aa2011-05-24 11:44:28 -04004375 }
4376 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4377 "2970 Failed to issue SLI_CONFIG ext-buffer "
4378 "maibox command, rc:x%x\n", rc);
4379 rc = -EPIPE;
4380 goto job_error;
4381 }
4382
4383 /* wait for additoinal external buffers */
4384 job->reply->result = 0;
4385 job->job_done(job);
4386 return SLI_CONFIG_HANDLED;
4387
4388job_error:
4389 lpfc_bsg_dma_page_free(phba, dmabuf);
4390 kfree(dd_data);
4391
4392 return rc;
4393}
4394
4395/**
4396 * lpfc_bsg_handle_sli_cfg_ebuf - handle ext buffer with sli-cfg mailbox cmd
4397 * @phba: Pointer to HBA context object.
4398 * @mb: Pointer to a BSG mailbox object.
4399 * @dmabuff: Pointer to a DMA buffer descriptor.
4400 *
4401 * This routine handles the external buffer with SLI_CONFIG (0x9B) mailbox
4402 * command with multiple non-embedded external buffers.
4403 **/
4404static int
4405lpfc_bsg_handle_sli_cfg_ebuf(struct lpfc_hba *phba, struct fc_bsg_job *job,
4406 struct lpfc_dmabuf *dmabuf)
4407{
4408 int rc;
4409
4410 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4411 "2971 SLI_CONFIG buffer (type:x%x)\n",
4412 phba->mbox_ext_buf_ctx.mboxType);
4413
4414 if (phba->mbox_ext_buf_ctx.mboxType == mbox_rd) {
4415 if (phba->mbox_ext_buf_ctx.state != LPFC_BSG_MBOX_DONE) {
4416 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4417 "2972 SLI_CONFIG rd buffer state "
4418 "mismatch:x%x\n",
4419 phba->mbox_ext_buf_ctx.state);
4420 lpfc_bsg_mbox_ext_abort(phba);
4421 return -EPIPE;
4422 }
4423 rc = lpfc_bsg_read_ebuf_get(phba, job);
4424 if (rc == SLI_CONFIG_HANDLED)
4425 lpfc_bsg_dma_page_free(phba, dmabuf);
4426 } else { /* phba->mbox_ext_buf_ctx.mboxType == mbox_wr */
4427 if (phba->mbox_ext_buf_ctx.state != LPFC_BSG_MBOX_HOST) {
4428 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4429 "2973 SLI_CONFIG wr buffer state "
4430 "mismatch:x%x\n",
4431 phba->mbox_ext_buf_ctx.state);
4432 lpfc_bsg_mbox_ext_abort(phba);
4433 return -EPIPE;
4434 }
4435 rc = lpfc_bsg_write_ebuf_set(phba, job, dmabuf);
4436 }
4437 return rc;
4438}
4439
4440/**
4441 * lpfc_bsg_handle_sli_cfg_ext - handle sli-cfg mailbox with external buffer
4442 * @phba: Pointer to HBA context object.
4443 * @mb: Pointer to a BSG mailbox object.
4444 * @dmabuff: Pointer to a DMA buffer descriptor.
4445 *
4446 * This routine checkes and handles non-embedded multi-buffer SLI_CONFIG
4447 * (0x9B) mailbox commands and external buffers.
4448 **/
4449static int
4450lpfc_bsg_handle_sli_cfg_ext(struct lpfc_hba *phba, struct fc_bsg_job *job,
4451 struct lpfc_dmabuf *dmabuf)
4452{
4453 struct dfc_mbox_req *mbox_req;
James Smart88a2cfb2011-07-22 18:36:33 -04004454 int rc = SLI_CONFIG_NOT_HANDLED;
James Smart7ad20aa2011-05-24 11:44:28 -04004455
4456 mbox_req =
4457 (struct dfc_mbox_req *)job->request->rqst_data.h_vendor.vendor_cmd;
4458
4459 /* mbox command with/without single external buffer */
4460 if (mbox_req->extMboxTag == 0 && mbox_req->extSeqNum == 0)
James Smart88a2cfb2011-07-22 18:36:33 -04004461 return rc;
James Smart7ad20aa2011-05-24 11:44:28 -04004462
4463 /* mbox command and first external buffer */
4464 if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_IDLE) {
4465 if (mbox_req->extSeqNum == 1) {
4466 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4467 "2974 SLI_CONFIG mailbox: tag:%d, "
4468 "seq:%d\n", mbox_req->extMboxTag,
4469 mbox_req->extSeqNum);
4470 rc = lpfc_bsg_handle_sli_cfg_mbox(phba, job, dmabuf);
4471 return rc;
4472 } else
4473 goto sli_cfg_ext_error;
4474 }
4475
4476 /*
4477 * handle additional external buffers
4478 */
4479
4480 /* check broken pipe conditions */
4481 if (mbox_req->extMboxTag != phba->mbox_ext_buf_ctx.mbxTag)
4482 goto sli_cfg_ext_error;
4483 if (mbox_req->extSeqNum > phba->mbox_ext_buf_ctx.numBuf)
4484 goto sli_cfg_ext_error;
4485 if (mbox_req->extSeqNum != phba->mbox_ext_buf_ctx.seqNum + 1)
4486 goto sli_cfg_ext_error;
4487
4488 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4489 "2975 SLI_CONFIG mailbox external buffer: "
4490 "extSta:x%x, tag:%d, seq:%d\n",
4491 phba->mbox_ext_buf_ctx.state, mbox_req->extMboxTag,
4492 mbox_req->extSeqNum);
4493 rc = lpfc_bsg_handle_sli_cfg_ebuf(phba, job, dmabuf);
4494 return rc;
4495
4496sli_cfg_ext_error:
4497 /* all other cases, broken pipe */
4498 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4499 "2976 SLI_CONFIG mailbox broken pipe: "
4500 "ctxSta:x%x, ctxNumBuf:%d "
4501 "ctxTag:%d, ctxSeq:%d, tag:%d, seq:%d\n",
4502 phba->mbox_ext_buf_ctx.state,
4503 phba->mbox_ext_buf_ctx.numBuf,
4504 phba->mbox_ext_buf_ctx.mbxTag,
4505 phba->mbox_ext_buf_ctx.seqNum,
4506 mbox_req->extMboxTag, mbox_req->extSeqNum);
4507
4508 lpfc_bsg_mbox_ext_session_reset(phba);
4509
4510 return -EPIPE;
4511}
4512
4513/**
James Smart3b5dd522010-01-26 23:10:15 -05004514 * lpfc_bsg_issue_mbox - issues a mailbox command on behalf of an app
4515 * @phba: Pointer to HBA context object.
4516 * @mb: Pointer to a mailbox object.
4517 * @vport: Pointer to a vport object.
4518 *
4519 * Allocate a tracking object, mailbox command memory, get a mailbox
4520 * from the mailbox pool, copy the caller mailbox command.
4521 *
4522 * If offline and the sli is active we need to poll for the command (port is
4523 * being reset) and com-plete the job, otherwise issue the mailbox command and
4524 * let our completion handler finish the command.
4525 **/
4526static uint32_t
4527lpfc_bsg_issue_mbox(struct lpfc_hba *phba, struct fc_bsg_job *job,
4528 struct lpfc_vport *vport)
4529{
James Smart7a470272010-03-15 11:25:20 -04004530 LPFC_MBOXQ_t *pmboxq = NULL; /* internal mailbox queue */
4531 MAILBOX_t *pmb; /* shortcut to the pmboxq mailbox */
4532 /* a 4k buffer to hold the mb and extended data from/to the bsg */
James Smart7ad20aa2011-05-24 11:44:28 -04004533 uint8_t *pmbx = NULL;
James Smart7a470272010-03-15 11:25:20 -04004534 struct bsg_job_data *dd_data = NULL; /* bsg data tracking structure */
James Smart7ad20aa2011-05-24 11:44:28 -04004535 struct lpfc_dmabuf *dmabuf = NULL;
4536 struct dfc_mbox_req *mbox_req;
James Smartb6e3b9c2011-04-16 11:03:43 -04004537 struct READ_EVENT_LOG_VAR *rdEventLog;
4538 uint32_t transmit_length, receive_length, mode;
James Smart7ad20aa2011-05-24 11:44:28 -04004539 struct lpfc_mbx_sli4_config *sli4_config;
James Smartb6e3b9c2011-04-16 11:03:43 -04004540 struct lpfc_mbx_nembed_cmd *nembed_sge;
4541 struct mbox_header *header;
4542 struct ulp_bde64 *bde;
James Smart7a470272010-03-15 11:25:20 -04004543 uint8_t *ext = NULL;
James Smart3b5dd522010-01-26 23:10:15 -05004544 int rc = 0;
James Smart7a470272010-03-15 11:25:20 -04004545 uint8_t *from;
James Smart7ad20aa2011-05-24 11:44:28 -04004546 uint32_t size;
4547
James Smart7a470272010-03-15 11:25:20 -04004548 /* in case no data is transferred */
4549 job->reply->reply_payload_rcv_len = 0;
4550
James Smartb6e3b9c2011-04-16 11:03:43 -04004551 /* sanity check to protect driver */
4552 if (job->reply_payload.payload_len > BSG_MBOX_SIZE ||
4553 job->request_payload.payload_len > BSG_MBOX_SIZE) {
4554 rc = -ERANGE;
4555 goto job_done;
4556 }
4557
James Smart7ad20aa2011-05-24 11:44:28 -04004558 /*
4559 * Don't allow mailbox commands to be sent when blocked or when in
4560 * the middle of discovery
4561 */
4562 if (phba->sli.sli_flag & LPFC_BLOCK_MGMT_IO) {
4563 rc = -EAGAIN;
4564 goto job_done;
4565 }
4566
4567 mbox_req =
4568 (struct dfc_mbox_req *)job->request->rqst_data.h_vendor.vendor_cmd;
4569
James Smart7a470272010-03-15 11:25:20 -04004570 /* check if requested extended data lengths are valid */
James Smartb6e3b9c2011-04-16 11:03:43 -04004571 if ((mbox_req->inExtWLen > BSG_MBOX_SIZE/sizeof(uint32_t)) ||
4572 (mbox_req->outExtWLen > BSG_MBOX_SIZE/sizeof(uint32_t))) {
James Smart7a470272010-03-15 11:25:20 -04004573 rc = -ERANGE;
4574 goto job_done;
4575 }
James Smart3b5dd522010-01-26 23:10:15 -05004576
James Smart7ad20aa2011-05-24 11:44:28 -04004577 dmabuf = lpfc_bsg_dma_page_alloc(phba);
4578 if (!dmabuf || !dmabuf->virt) {
4579 rc = -ENOMEM;
4580 goto job_done;
4581 }
4582
4583 /* Get the mailbox command or external buffer from BSG */
4584 pmbx = (uint8_t *)dmabuf->virt;
4585 size = job->request_payload.payload_len;
4586 sg_copy_to_buffer(job->request_payload.sg_list,
4587 job->request_payload.sg_cnt, pmbx, size);
4588
4589 /* Handle possible SLI_CONFIG with non-embedded payloads */
4590 if (phba->sli_rev == LPFC_SLI_REV4) {
4591 rc = lpfc_bsg_handle_sli_cfg_ext(phba, job, dmabuf);
4592 if (rc == SLI_CONFIG_HANDLED)
4593 goto job_cont;
4594 if (rc)
4595 goto job_done;
4596 /* SLI_CONFIG_NOT_HANDLED for other mailbox commands */
4597 }
4598
4599 rc = lpfc_bsg_check_cmd_access(phba, (MAILBOX_t *)pmbx, vport);
4600 if (rc != 0)
4601 goto job_done; /* must be negative */
4602
James Smart3b5dd522010-01-26 23:10:15 -05004603 /* allocate our bsg tracking structure */
4604 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
4605 if (!dd_data) {
4606 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
4607 "2727 Failed allocation of dd_data\n");
James Smart7a470272010-03-15 11:25:20 -04004608 rc = -ENOMEM;
4609 goto job_done;
James Smart3b5dd522010-01-26 23:10:15 -05004610 }
4611
James Smart3b5dd522010-01-26 23:10:15 -05004612 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4613 if (!pmboxq) {
James Smart7a470272010-03-15 11:25:20 -04004614 rc = -ENOMEM;
4615 goto job_done;
James Smart3b5dd522010-01-26 23:10:15 -05004616 }
James Smart7a470272010-03-15 11:25:20 -04004617 memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
James Smart3b5dd522010-01-26 23:10:15 -05004618
James Smart3b5dd522010-01-26 23:10:15 -05004619 pmb = &pmboxq->u.mb;
James Smart7ad20aa2011-05-24 11:44:28 -04004620 memcpy(pmb, pmbx, sizeof(*pmb));
James Smart3b5dd522010-01-26 23:10:15 -05004621 pmb->mbxOwner = OWN_HOST;
James Smart3b5dd522010-01-26 23:10:15 -05004622 pmboxq->vport = vport;
4623
James Smartc7495932010-04-06 15:05:28 -04004624 /* If HBA encountered an error attention, allow only DUMP
4625 * or RESTART mailbox commands until the HBA is restarted.
4626 */
4627 if (phba->pport->stopped &&
4628 pmb->mbxCommand != MBX_DUMP_MEMORY &&
4629 pmb->mbxCommand != MBX_RESTART &&
4630 pmb->mbxCommand != MBX_WRITE_VPARMS &&
4631 pmb->mbxCommand != MBX_WRITE_WWN)
4632 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
4633 "2797 mbox: Issued mailbox cmd "
4634 "0x%x while in stopped state.\n",
4635 pmb->mbxCommand);
4636
James Smart7a470272010-03-15 11:25:20 -04004637 /* extended mailbox commands will need an extended buffer */
James Smartc7495932010-04-06 15:05:28 -04004638 if (mbox_req->inExtWLen || mbox_req->outExtWLen) {
James Smart026abb82011-12-13 13:20:45 -05004639 from = pmbx;
4640 ext = from + sizeof(MAILBOX_t);
James Smart7a470272010-03-15 11:25:20 -04004641 pmboxq->context2 = ext;
4642 pmboxq->in_ext_byte_len =
James Smart7a470272010-03-15 11:25:20 -04004643 mbox_req->inExtWLen * sizeof(uint32_t);
4644 pmboxq->out_ext_byte_len =
James Smartc7495932010-04-06 15:05:28 -04004645 mbox_req->outExtWLen * sizeof(uint32_t);
James Smart7a470272010-03-15 11:25:20 -04004646 pmboxq->mbox_offset_word = mbox_req->mbOffset;
4647 }
4648
4649 /* biu diag will need a kernel buffer to transfer the data
4650 * allocate our own buffer and setup the mailbox command to
4651 * use ours
4652 */
4653 if (pmb->mbxCommand == MBX_RUN_BIU_DIAG64) {
James Smartb6e3b9c2011-04-16 11:03:43 -04004654 transmit_length = pmb->un.varWords[1];
4655 receive_length = pmb->un.varWords[4];
James Smartc7495932010-04-06 15:05:28 -04004656 /* transmit length cannot be greater than receive length or
4657 * mailbox extension size
4658 */
4659 if ((transmit_length > receive_length) ||
James Smart88a2cfb2011-07-22 18:36:33 -04004660 (transmit_length > BSG_MBOX_SIZE - sizeof(MAILBOX_t))) {
James Smartc7495932010-04-06 15:05:28 -04004661 rc = -ERANGE;
4662 goto job_done;
4663 }
James Smart7a470272010-03-15 11:25:20 -04004664 pmb->un.varBIUdiag.un.s2.xmit_bde64.addrHigh =
James Smart7ad20aa2011-05-24 11:44:28 -04004665 putPaddrHigh(dmabuf->phys + sizeof(MAILBOX_t));
James Smart7a470272010-03-15 11:25:20 -04004666 pmb->un.varBIUdiag.un.s2.xmit_bde64.addrLow =
James Smart7ad20aa2011-05-24 11:44:28 -04004667 putPaddrLow(dmabuf->phys + sizeof(MAILBOX_t));
James Smart7a470272010-03-15 11:25:20 -04004668
4669 pmb->un.varBIUdiag.un.s2.rcv_bde64.addrHigh =
James Smart7ad20aa2011-05-24 11:44:28 -04004670 putPaddrHigh(dmabuf->phys + sizeof(MAILBOX_t)
4671 + pmb->un.varBIUdiag.un.s2.xmit_bde64.tus.f.bdeSize);
James Smart7a470272010-03-15 11:25:20 -04004672 pmb->un.varBIUdiag.un.s2.rcv_bde64.addrLow =
James Smart7ad20aa2011-05-24 11:44:28 -04004673 putPaddrLow(dmabuf->phys + sizeof(MAILBOX_t)
4674 + pmb->un.varBIUdiag.un.s2.xmit_bde64.tus.f.bdeSize);
James Smartc7495932010-04-06 15:05:28 -04004675 } else if (pmb->mbxCommand == MBX_READ_EVENT_LOG) {
James Smartb6e3b9c2011-04-16 11:03:43 -04004676 rdEventLog = &pmb->un.varRdEventLog;
4677 receive_length = rdEventLog->rcv_bde64.tus.f.bdeSize;
4678 mode = bf_get(lpfc_event_log, rdEventLog);
James Smartc7495932010-04-06 15:05:28 -04004679
4680 /* receive length cannot be greater than mailbox
4681 * extension size
4682 */
James Smart88a2cfb2011-07-22 18:36:33 -04004683 if (receive_length > BSG_MBOX_SIZE - sizeof(MAILBOX_t)) {
James Smartc7495932010-04-06 15:05:28 -04004684 rc = -ERANGE;
4685 goto job_done;
4686 }
4687
4688 /* mode zero uses a bde like biu diags command */
4689 if (mode == 0) {
James Smart7ad20aa2011-05-24 11:44:28 -04004690 pmb->un.varWords[3] = putPaddrLow(dmabuf->phys
4691 + sizeof(MAILBOX_t));
4692 pmb->un.varWords[4] = putPaddrHigh(dmabuf->phys
4693 + sizeof(MAILBOX_t));
James Smartc7495932010-04-06 15:05:28 -04004694 }
4695 } else if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart3ef6d242012-01-18 16:23:48 -05004696 /* Let type 4 (well known data) through because the data is
4697 * returned in varwords[4-8]
4698 * otherwise check the recieve length and fetch the buffer addr
4699 */
4700 if ((pmb->mbxCommand == MBX_DUMP_MEMORY) &&
4701 (pmb->un.varDmp.type != DMP_WELL_KNOWN)) {
James Smartc7495932010-04-06 15:05:28 -04004702 /* rebuild the command for sli4 using our own buffers
4703 * like we do for biu diags
4704 */
James Smartb6e3b9c2011-04-16 11:03:43 -04004705 receive_length = pmb->un.varWords[2];
James Smartc7495932010-04-06 15:05:28 -04004706 /* receive length cannot be greater than mailbox
4707 * extension size
4708 */
James Smart7ad20aa2011-05-24 11:44:28 -04004709 if (receive_length == 0) {
James Smartc7495932010-04-06 15:05:28 -04004710 rc = -ERANGE;
4711 goto job_done;
4712 }
James Smart7ad20aa2011-05-24 11:44:28 -04004713 pmb->un.varWords[3] = putPaddrLow(dmabuf->phys
4714 + sizeof(MAILBOX_t));
4715 pmb->un.varWords[4] = putPaddrHigh(dmabuf->phys
4716 + sizeof(MAILBOX_t));
James Smartc7495932010-04-06 15:05:28 -04004717 } else if ((pmb->mbxCommand == MBX_UPDATE_CFG) &&
4718 pmb->un.varUpdateCfg.co) {
James Smartb6e3b9c2011-04-16 11:03:43 -04004719 bde = (struct ulp_bde64 *)&pmb->un.varWords[4];
James Smartc7495932010-04-06 15:05:28 -04004720
4721 /* bde size cannot be greater than mailbox ext size */
James Smart88a2cfb2011-07-22 18:36:33 -04004722 if (bde->tus.f.bdeSize >
4723 BSG_MBOX_SIZE - sizeof(MAILBOX_t)) {
James Smartc7495932010-04-06 15:05:28 -04004724 rc = -ERANGE;
4725 goto job_done;
4726 }
James Smart7ad20aa2011-05-24 11:44:28 -04004727 bde->addrHigh = putPaddrHigh(dmabuf->phys
4728 + sizeof(MAILBOX_t));
4729 bde->addrLow = putPaddrLow(dmabuf->phys
4730 + sizeof(MAILBOX_t));
James Smart515e0aa2010-09-29 11:19:00 -04004731 } else if (pmb->mbxCommand == MBX_SLI4_CONFIG) {
James Smart7ad20aa2011-05-24 11:44:28 -04004732 /* Handling non-embedded SLI_CONFIG mailbox command */
4733 sli4_config = &pmboxq->u.mqe.un.sli4_config;
4734 if (!bf_get(lpfc_mbox_hdr_emb,
4735 &sli4_config->header.cfg_mhdr)) {
4736 /* rebuild the command for sli4 using our
4737 * own buffers like we do for biu diags
4738 */
4739 header = (struct mbox_header *)
4740 &pmb->un.varWords[0];
4741 nembed_sge = (struct lpfc_mbx_nembed_cmd *)
4742 &pmb->un.varWords[0];
4743 receive_length = nembed_sge->sge[0].length;
James Smart515e0aa2010-09-29 11:19:00 -04004744
James Smart7ad20aa2011-05-24 11:44:28 -04004745 /* receive length cannot be greater than
4746 * mailbox extension size
4747 */
4748 if ((receive_length == 0) ||
James Smart88a2cfb2011-07-22 18:36:33 -04004749 (receive_length >
4750 BSG_MBOX_SIZE - sizeof(MAILBOX_t))) {
James Smart7ad20aa2011-05-24 11:44:28 -04004751 rc = -ERANGE;
4752 goto job_done;
4753 }
4754
4755 nembed_sge->sge[0].pa_hi =
4756 putPaddrHigh(dmabuf->phys
4757 + sizeof(MAILBOX_t));
4758 nembed_sge->sge[0].pa_lo =
4759 putPaddrLow(dmabuf->phys
4760 + sizeof(MAILBOX_t));
James Smart515e0aa2010-09-29 11:19:00 -04004761 }
James Smartc7495932010-04-06 15:05:28 -04004762 }
James Smart3b5dd522010-01-26 23:10:15 -05004763 }
4764
James Smart7ad20aa2011-05-24 11:44:28 -04004765 dd_data->context_un.mbox.dmabuffers = dmabuf;
James Smartc7495932010-04-06 15:05:28 -04004766
James Smart3b5dd522010-01-26 23:10:15 -05004767 /* setup wake call as IOCB callback */
James Smart7ad20aa2011-05-24 11:44:28 -04004768 pmboxq->mbox_cmpl = lpfc_bsg_issue_mbox_cmpl;
James Smart7a470272010-03-15 11:25:20 -04004769
James Smart3b5dd522010-01-26 23:10:15 -05004770 /* setup context field to pass wait_queue pointer to wake function */
4771 pmboxq->context1 = dd_data;
4772 dd_data->type = TYPE_MBOX;
James Smarta33c4f72013-03-01 16:36:00 -05004773 dd_data->set_job = job;
James Smart3b5dd522010-01-26 23:10:15 -05004774 dd_data->context_un.mbox.pmboxq = pmboxq;
James Smart7ad20aa2011-05-24 11:44:28 -04004775 dd_data->context_un.mbox.mb = (MAILBOX_t *)pmbx;
James Smart7a470272010-03-15 11:25:20 -04004776 dd_data->context_un.mbox.ext = ext;
4777 dd_data->context_un.mbox.mbOffset = mbox_req->mbOffset;
4778 dd_data->context_un.mbox.inExtWLen = mbox_req->inExtWLen;
James Smartc7495932010-04-06 15:05:28 -04004779 dd_data->context_un.mbox.outExtWLen = mbox_req->outExtWLen;
James Smart3b5dd522010-01-26 23:10:15 -05004780 job->dd_data = dd_data;
James Smart7a470272010-03-15 11:25:20 -04004781
4782 if ((vport->fc_flag & FC_OFFLINE_MODE) ||
4783 (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))) {
4784 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
4785 if (rc != MBX_SUCCESS) {
4786 rc = (rc == MBX_TIMEOUT) ? -ETIME : -ENODEV;
4787 goto job_done;
4788 }
4789
4790 /* job finished, copy the data */
James Smart7ad20aa2011-05-24 11:44:28 -04004791 memcpy(pmbx, pmb, sizeof(*pmb));
James Smart7a470272010-03-15 11:25:20 -04004792 job->reply->reply_payload_rcv_len =
4793 sg_copy_from_buffer(job->reply_payload.sg_list,
James Smart7ad20aa2011-05-24 11:44:28 -04004794 job->reply_payload.sg_cnt,
4795 pmbx, size);
James Smart7a470272010-03-15 11:25:20 -04004796 /* not waiting mbox already done */
4797 rc = 0;
4798 goto job_done;
James Smart3b5dd522010-01-26 23:10:15 -05004799 }
4800
James Smart7a470272010-03-15 11:25:20 -04004801 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
4802 if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY))
4803 return 1; /* job started */
4804
4805job_done:
4806 /* common exit for error or job completed inline */
James Smart7a470272010-03-15 11:25:20 -04004807 if (pmboxq)
4808 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart7ad20aa2011-05-24 11:44:28 -04004809 lpfc_bsg_dma_page_free(phba, dmabuf);
James Smart7a470272010-03-15 11:25:20 -04004810 kfree(dd_data);
4811
James Smart7ad20aa2011-05-24 11:44:28 -04004812job_cont:
James Smart7a470272010-03-15 11:25:20 -04004813 return rc;
James Smart3b5dd522010-01-26 23:10:15 -05004814}
4815
4816/**
4817 * lpfc_bsg_mbox_cmd - process an fc bsg LPFC_BSG_VENDOR_MBOX command
4818 * @job: MBOX fc_bsg_job for LPFC_BSG_VENDOR_MBOX.
4819 **/
4820static int
4821lpfc_bsg_mbox_cmd(struct fc_bsg_job *job)
4822{
4823 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
4824 struct lpfc_hba *phba = vport->phba;
James Smart7ad20aa2011-05-24 11:44:28 -04004825 struct dfc_mbox_req *mbox_req;
James Smart3b5dd522010-01-26 23:10:15 -05004826 int rc = 0;
4827
James Smart7ad20aa2011-05-24 11:44:28 -04004828 /* mix-and-match backward compatibility */
James Smart3b5dd522010-01-26 23:10:15 -05004829 job->reply->reply_payload_rcv_len = 0;
4830 if (job->request_len <
4831 sizeof(struct fc_bsg_request) + sizeof(struct dfc_mbox_req)) {
James Smart7ad20aa2011-05-24 11:44:28 -04004832 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4833 "2737 Mix-and-match backward compability "
4834 "between MBOX_REQ old size:%d and "
4835 "new request size:%d\n",
4836 (int)(job->request_len -
4837 sizeof(struct fc_bsg_request)),
4838 (int)sizeof(struct dfc_mbox_req));
4839 mbox_req = (struct dfc_mbox_req *)
4840 job->request->rqst_data.h_vendor.vendor_cmd;
4841 mbox_req->extMboxTag = 0;
4842 mbox_req->extSeqNum = 0;
James Smart3b5dd522010-01-26 23:10:15 -05004843 }
4844
4845 rc = lpfc_bsg_issue_mbox(phba, job, vport);
4846
James Smart3b5dd522010-01-26 23:10:15 -05004847 if (rc == 0) {
4848 /* job done */
4849 job->reply->result = 0;
4850 job->dd_data = NULL;
4851 job->job_done(job);
4852 } else if (rc == 1)
4853 /* job submitted, will complete later*/
4854 rc = 0; /* return zero, no error */
4855 else {
4856 /* some error occurred */
4857 job->reply->result = rc;
4858 job->dd_data = NULL;
4859 }
4860
4861 return rc;
4862}
4863
4864/**
James Smarte2aed292010-02-26 14:15:00 -05004865 * lpfc_bsg_menlo_cmd_cmp - lpfc_menlo_cmd completion handler
4866 * @phba: Pointer to HBA context object.
4867 * @cmdiocbq: Pointer to command iocb.
4868 * @rspiocbq: Pointer to response iocb.
4869 *
4870 * This function is the completion handler for iocbs issued using
4871 * lpfc_menlo_cmd function. This function is called by the
4872 * ring event handler function without any lock held. This function
4873 * can be called from both worker thread context and interrupt
4874 * context. This function also can be called from another thread which
4875 * cleans up the SLI layer objects.
4876 * This function copies the contents of the response iocb to the
4877 * response iocb memory object provided by the caller of
4878 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
4879 * sleeps for the iocb completion.
4880 **/
4881static void
4882lpfc_bsg_menlo_cmd_cmp(struct lpfc_hba *phba,
4883 struct lpfc_iocbq *cmdiocbq,
4884 struct lpfc_iocbq *rspiocbq)
4885{
4886 struct bsg_job_data *dd_data;
4887 struct fc_bsg_job *job;
4888 IOCB_t *rsp;
James Smarta33c4f72013-03-01 16:36:00 -05004889 struct lpfc_dmabuf *bmp, *cmp, *rmp;
James Smarte2aed292010-02-26 14:15:00 -05004890 struct lpfc_bsg_menlo *menlo;
4891 unsigned long flags;
4892 struct menlo_response *menlo_resp;
James Smarta33c4f72013-03-01 16:36:00 -05004893 unsigned int rsp_size;
James Smarte2aed292010-02-26 14:15:00 -05004894 int rc = 0;
4895
James Smarte2aed292010-02-26 14:15:00 -05004896 dd_data = cmdiocbq->context1;
James Smarta33c4f72013-03-01 16:36:00 -05004897 cmp = cmdiocbq->context2;
4898 bmp = cmdiocbq->context3;
James Smarte2aed292010-02-26 14:15:00 -05004899 menlo = &dd_data->context_un.menlo;
James Smarta33c4f72013-03-01 16:36:00 -05004900 rmp = menlo->rmp;
James Smarte2aed292010-02-26 14:15:00 -05004901 rsp = &rspiocbq->iocb;
4902
James Smarta33c4f72013-03-01 16:36:00 -05004903 /* Determine if job has been aborted */
4904 spin_lock_irqsave(&phba->ct_ev_lock, flags);
4905 job = dd_data->set_job;
4906 if (job) {
4907 /* Prevent timeout handling from trying to abort job */
4908 job->dd_data = NULL;
4909 }
4910 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smarte2aed292010-02-26 14:15:00 -05004911
James Smarta33c4f72013-03-01 16:36:00 -05004912 /* Copy the job data or set the failing status for the job */
4913
4914 if (job) {
4915 /* always return the xri, this would be used in the case
4916 * of a menlo download to allow the data to be sent as a
4917 * continuation of the exchange.
4918 */
4919
4920 menlo_resp = (struct menlo_response *)
4921 job->reply->reply_data.vendor_reply.vendor_rsp;
4922 menlo_resp->xri = rsp->ulpContext;
4923 if (rsp->ulpStatus) {
4924 if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
4925 switch (rsp->un.ulpWord[4] & IOERR_PARAM_MASK) {
4926 case IOERR_SEQUENCE_TIMEOUT:
4927 rc = -ETIMEDOUT;
4928 break;
4929 case IOERR_INVALID_RPI:
4930 rc = -EFAULT;
4931 break;
4932 default:
4933 rc = -EACCES;
4934 break;
4935 }
4936 } else {
James Smarte2aed292010-02-26 14:15:00 -05004937 rc = -EACCES;
James Smarte2aed292010-02-26 14:15:00 -05004938 }
James Smarta33c4f72013-03-01 16:36:00 -05004939 } else {
4940 rsp_size = rsp->un.genreq64.bdl.bdeSize;
4941 job->reply->reply_payload_rcv_len =
4942 lpfc_bsg_copy_data(rmp, &job->reply_payload,
4943 rsp_size, 0);
4944 }
James Smarte2aed292010-02-26 14:15:00 -05004945
James Smarta33c4f72013-03-01 16:36:00 -05004946 }
4947
James Smarte2aed292010-02-26 14:15:00 -05004948 lpfc_sli_release_iocbq(phba, cmdiocbq);
James Smarta33c4f72013-03-01 16:36:00 -05004949 lpfc_free_bsg_buffers(phba, cmp);
4950 lpfc_free_bsg_buffers(phba, rmp);
4951 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
James Smarte2aed292010-02-26 14:15:00 -05004952 kfree(bmp);
4953 kfree(dd_data);
James Smarta33c4f72013-03-01 16:36:00 -05004954
4955 /* Complete the job if active */
4956
4957 if (job) {
4958 job->reply->result = rc;
4959 job->job_done(job);
4960 }
4961
James Smarte2aed292010-02-26 14:15:00 -05004962 return;
4963}
4964
4965/**
4966 * lpfc_menlo_cmd - send an ioctl for menlo hardware
4967 * @job: fc_bsg_job to handle
4968 *
4969 * This function issues a gen request 64 CR ioctl for all menlo cmd requests,
4970 * all the command completions will return the xri for the command.
4971 * For menlo data requests a gen request 64 CX is used to continue the exchange
4972 * supplied in the menlo request header xri field.
4973 **/
4974static int
4975lpfc_menlo_cmd(struct fc_bsg_job *job)
4976{
4977 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
4978 struct lpfc_hba *phba = vport->phba;
James Smarta33c4f72013-03-01 16:36:00 -05004979 struct lpfc_iocbq *cmdiocbq;
4980 IOCB_t *cmd;
James Smarte2aed292010-02-26 14:15:00 -05004981 int rc = 0;
4982 struct menlo_command *menlo_cmd;
4983 struct menlo_response *menlo_resp;
James Smarta33c4f72013-03-01 16:36:00 -05004984 struct lpfc_dmabuf *bmp = NULL, *cmp = NULL, *rmp = NULL;
James Smarte2aed292010-02-26 14:15:00 -05004985 int request_nseg;
4986 int reply_nseg;
James Smarte2aed292010-02-26 14:15:00 -05004987 struct bsg_job_data *dd_data;
4988 struct ulp_bde64 *bpl = NULL;
4989
4990 /* in case no data is returned return just the return code */
4991 job->reply->reply_payload_rcv_len = 0;
4992
4993 if (job->request_len <
4994 sizeof(struct fc_bsg_request) +
4995 sizeof(struct menlo_command)) {
4996 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
4997 "2784 Received MENLO_CMD request below "
4998 "minimum size\n");
4999 rc = -ERANGE;
5000 goto no_dd_data;
5001 }
5002
5003 if (job->reply_len <
5004 sizeof(struct fc_bsg_request) + sizeof(struct menlo_response)) {
5005 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5006 "2785 Received MENLO_CMD reply below "
5007 "minimum size\n");
5008 rc = -ERANGE;
5009 goto no_dd_data;
5010 }
5011
5012 if (!(phba->menlo_flag & HBA_MENLO_SUPPORT)) {
5013 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5014 "2786 Adapter does not support menlo "
5015 "commands\n");
5016 rc = -EPERM;
5017 goto no_dd_data;
5018 }
5019
5020 menlo_cmd = (struct menlo_command *)
5021 job->request->rqst_data.h_vendor.vendor_cmd;
5022
5023 menlo_resp = (struct menlo_response *)
5024 job->reply->reply_data.vendor_reply.vendor_rsp;
5025
5026 /* allocate our bsg tracking structure */
5027 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
5028 if (!dd_data) {
5029 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5030 "2787 Failed allocation of dd_data\n");
5031 rc = -ENOMEM;
5032 goto no_dd_data;
5033 }
5034
5035 bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5036 if (!bmp) {
5037 rc = -ENOMEM;
5038 goto free_dd;
5039 }
5040
James Smarta33c4f72013-03-01 16:36:00 -05005041 bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
5042 if (!bmp->virt) {
James Smarte2aed292010-02-26 14:15:00 -05005043 rc = -ENOMEM;
5044 goto free_bmp;
5045 }
5046
James Smarte2aed292010-02-26 14:15:00 -05005047 INIT_LIST_HEAD(&bmp->list);
James Smarta33c4f72013-03-01 16:36:00 -05005048
5049 bpl = (struct ulp_bde64 *)bmp->virt;
5050 request_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64);
5051 cmp = lpfc_alloc_bsg_buffers(phba, job->request_payload.payload_len,
5052 1, bpl, &request_nseg);
5053 if (!cmp) {
5054 rc = -ENOMEM;
5055 goto free_bmp;
5056 }
5057 lpfc_bsg_copy_data(cmp, &job->request_payload,
5058 job->request_payload.payload_len, 1);
5059
5060 bpl += request_nseg;
5061 reply_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64) - request_nseg;
5062 rmp = lpfc_alloc_bsg_buffers(phba, job->reply_payload.payload_len, 0,
5063 bpl, &reply_nseg);
5064 if (!rmp) {
5065 rc = -ENOMEM;
5066 goto free_cmp;
James Smarte2aed292010-02-26 14:15:00 -05005067 }
5068
James Smarta33c4f72013-03-01 16:36:00 -05005069 cmdiocbq = lpfc_sli_get_iocbq(phba);
5070 if (!cmdiocbq) {
5071 rc = -ENOMEM;
5072 goto free_rmp;
James Smarte2aed292010-02-26 14:15:00 -05005073 }
5074
5075 cmd = &cmdiocbq->iocb;
5076 cmd->un.genreq64.bdl.ulpIoTag32 = 0;
5077 cmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
5078 cmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
5079 cmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
5080 cmd->un.genreq64.bdl.bdeSize =
5081 (request_nseg + reply_nseg) * sizeof(struct ulp_bde64);
5082 cmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
5083 cmd->un.genreq64.w5.hcsw.Dfctl = 0;
5084 cmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CMD;
5085 cmd->un.genreq64.w5.hcsw.Type = MENLO_TRANSPORT_TYPE; /* 0xfe */
5086 cmd->ulpBdeCount = 1;
5087 cmd->ulpClass = CLASS3;
5088 cmd->ulpOwner = OWN_CHIP;
5089 cmd->ulpLe = 1; /* Limited Edition */
5090 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
5091 cmdiocbq->vport = phba->pport;
5092 /* We want the firmware to timeout before we do */
5093 cmd->ulpTimeout = MENLO_TIMEOUT - 5;
James Smarte2aed292010-02-26 14:15:00 -05005094 cmdiocbq->iocb_cmpl = lpfc_bsg_menlo_cmd_cmp;
5095 cmdiocbq->context1 = dd_data;
James Smarta33c4f72013-03-01 16:36:00 -05005096 cmdiocbq->context2 = cmp;
5097 cmdiocbq->context3 = bmp;
James Smarte2aed292010-02-26 14:15:00 -05005098 if (menlo_cmd->cmd == LPFC_BSG_VENDOR_MENLO_CMD) {
5099 cmd->ulpCommand = CMD_GEN_REQUEST64_CR;
5100 cmd->ulpPU = MENLO_PU; /* 3 */
5101 cmd->un.ulpWord[4] = MENLO_DID; /* 0x0000FC0E */
5102 cmd->ulpContext = MENLO_CONTEXT; /* 0 */
5103 } else {
5104 cmd->ulpCommand = CMD_GEN_REQUEST64_CX;
5105 cmd->ulpPU = 1;
5106 cmd->un.ulpWord[4] = 0;
5107 cmd->ulpContext = menlo_cmd->xri;
5108 }
5109
5110 dd_data->type = TYPE_MENLO;
James Smarta33c4f72013-03-01 16:36:00 -05005111 dd_data->set_job = job;
James Smarte2aed292010-02-26 14:15:00 -05005112 dd_data->context_un.menlo.cmdiocbq = cmdiocbq;
James Smarta33c4f72013-03-01 16:36:00 -05005113 dd_data->context_un.menlo.rmp = rmp;
5114 job->dd_data = dd_data;
James Smarte2aed292010-02-26 14:15:00 -05005115
5116 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq,
5117 MENLO_TIMEOUT - 5);
5118 if (rc == IOCB_SUCCESS)
5119 return 0; /* done for now */
5120
James Smarte2aed292010-02-26 14:15:00 -05005121 lpfc_sli_release_iocbq(phba, cmdiocbq);
James Smarta33c4f72013-03-01 16:36:00 -05005122
5123free_rmp:
5124 lpfc_free_bsg_buffers(phba, rmp);
5125free_cmp:
5126 lpfc_free_bsg_buffers(phba, cmp);
James Smarte2aed292010-02-26 14:15:00 -05005127free_bmp:
James Smarta33c4f72013-03-01 16:36:00 -05005128 if (bmp->virt)
5129 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
James Smarte2aed292010-02-26 14:15:00 -05005130 kfree(bmp);
5131free_dd:
5132 kfree(dd_data);
5133no_dd_data:
5134 /* make error code available to userspace */
5135 job->reply->result = rc;
5136 job->dd_data = NULL;
5137 return rc;
5138}
James Smartb6e3b9c2011-04-16 11:03:43 -04005139
James Smarte2aed292010-02-26 14:15:00 -05005140/**
James Smartf1c3b0f2009-07-19 10:01:32 -04005141 * lpfc_bsg_hst_vendor - process a vendor-specific fc_bsg_job
5142 * @job: fc_bsg_job to handle
James Smart3b5dd522010-01-26 23:10:15 -05005143 **/
James Smartf1c3b0f2009-07-19 10:01:32 -04005144static int
5145lpfc_bsg_hst_vendor(struct fc_bsg_job *job)
5146{
5147 int command = job->request->rqst_data.h_vendor.vendor_cmd[0];
James Smart4cc0e562010-01-26 23:09:48 -05005148 int rc;
James Smartf1c3b0f2009-07-19 10:01:32 -04005149
5150 switch (command) {
5151 case LPFC_BSG_VENDOR_SET_CT_EVENT:
James Smart4cc0e562010-01-26 23:09:48 -05005152 rc = lpfc_bsg_hba_set_event(job);
James Smartf1c3b0f2009-07-19 10:01:32 -04005153 break;
James Smartf1c3b0f2009-07-19 10:01:32 -04005154 case LPFC_BSG_VENDOR_GET_CT_EVENT:
James Smart4cc0e562010-01-26 23:09:48 -05005155 rc = lpfc_bsg_hba_get_event(job);
James Smartf1c3b0f2009-07-19 10:01:32 -04005156 break;
James Smart3b5dd522010-01-26 23:10:15 -05005157 case LPFC_BSG_VENDOR_SEND_MGMT_RESP:
5158 rc = lpfc_bsg_send_mgmt_rsp(job);
5159 break;
5160 case LPFC_BSG_VENDOR_DIAG_MODE:
James Smart7ad20aa2011-05-24 11:44:28 -04005161 rc = lpfc_bsg_diag_loopback_mode(job);
James Smart3b5dd522010-01-26 23:10:15 -05005162 break;
James Smart7ad20aa2011-05-24 11:44:28 -04005163 case LPFC_BSG_VENDOR_DIAG_MODE_END:
5164 rc = lpfc_sli4_bsg_diag_mode_end(job);
5165 break;
5166 case LPFC_BSG_VENDOR_DIAG_RUN_LOOPBACK:
5167 rc = lpfc_bsg_diag_loopback_run(job);
5168 break;
5169 case LPFC_BSG_VENDOR_LINK_DIAG_TEST:
5170 rc = lpfc_sli4_bsg_link_diag_test(job);
James Smart3b5dd522010-01-26 23:10:15 -05005171 break;
5172 case LPFC_BSG_VENDOR_GET_MGMT_REV:
5173 rc = lpfc_bsg_get_dfc_rev(job);
5174 break;
5175 case LPFC_BSG_VENDOR_MBOX:
5176 rc = lpfc_bsg_mbox_cmd(job);
5177 break;
James Smarte2aed292010-02-26 14:15:00 -05005178 case LPFC_BSG_VENDOR_MENLO_CMD:
5179 case LPFC_BSG_VENDOR_MENLO_DATA:
5180 rc = lpfc_menlo_cmd(job);
5181 break;
James Smartf1c3b0f2009-07-19 10:01:32 -04005182 default:
James Smart4cc0e562010-01-26 23:09:48 -05005183 rc = -EINVAL;
5184 job->reply->reply_payload_rcv_len = 0;
5185 /* make error code available to userspace */
5186 job->reply->result = rc;
5187 break;
James Smartf1c3b0f2009-07-19 10:01:32 -04005188 }
James Smart4cc0e562010-01-26 23:09:48 -05005189
5190 return rc;
James Smartf1c3b0f2009-07-19 10:01:32 -04005191}
5192
5193/**
5194 * lpfc_bsg_request - handle a bsg request from the FC transport
5195 * @job: fc_bsg_job to handle
James Smart3b5dd522010-01-26 23:10:15 -05005196 **/
James Smartf1c3b0f2009-07-19 10:01:32 -04005197int
5198lpfc_bsg_request(struct fc_bsg_job *job)
5199{
5200 uint32_t msgcode;
James Smart4cc0e562010-01-26 23:09:48 -05005201 int rc;
James Smartf1c3b0f2009-07-19 10:01:32 -04005202
5203 msgcode = job->request->msgcode;
James Smartf1c3b0f2009-07-19 10:01:32 -04005204 switch (msgcode) {
5205 case FC_BSG_HST_VENDOR:
5206 rc = lpfc_bsg_hst_vendor(job);
5207 break;
5208 case FC_BSG_RPT_ELS:
5209 rc = lpfc_bsg_rport_els(job);
5210 break;
5211 case FC_BSG_RPT_CT:
James Smart4cc0e562010-01-26 23:09:48 -05005212 rc = lpfc_bsg_send_mgmt_cmd(job);
James Smartf1c3b0f2009-07-19 10:01:32 -04005213 break;
5214 default:
James Smart4cc0e562010-01-26 23:09:48 -05005215 rc = -EINVAL;
5216 job->reply->reply_payload_rcv_len = 0;
5217 /* make error code available to userspace */
5218 job->reply->result = rc;
James Smartf1c3b0f2009-07-19 10:01:32 -04005219 break;
5220 }
5221
5222 return rc;
5223}
5224
5225/**
5226 * lpfc_bsg_timeout - handle timeout of a bsg request from the FC transport
5227 * @job: fc_bsg_job that has timed out
5228 *
5229 * This function just aborts the job's IOCB. The aborted IOCB will return to
5230 * the waiting function which will handle passing the error back to userspace
James Smart3b5dd522010-01-26 23:10:15 -05005231 **/
James Smartf1c3b0f2009-07-19 10:01:32 -04005232int
5233lpfc_bsg_timeout(struct fc_bsg_job *job)
5234{
5235 struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
5236 struct lpfc_hba *phba = vport->phba;
James Smart4cc0e562010-01-26 23:09:48 -05005237 struct lpfc_iocbq *cmdiocb;
James Smartf1c3b0f2009-07-19 10:01:32 -04005238 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart4cc0e562010-01-26 23:09:48 -05005239 struct bsg_job_data *dd_data;
5240 unsigned long flags;
James Smarta33c4f72013-03-01 16:36:00 -05005241 int rc = 0;
5242 LIST_HEAD(completions);
5243 struct lpfc_iocbq *check_iocb, *next_iocb;
5244
5245 /* if job's driver data is NULL, the command completed or is in the
5246 * the process of completing. In this case, return status to request
5247 * so the timeout is retried. This avoids double completion issues
5248 * and the request will be pulled off the timer queue when the
5249 * command's completion handler executes. Otherwise, prevent the
5250 * command's completion handler from executing the job done callback
5251 * and continue processing to abort the outstanding the command.
5252 */
James Smartf1c3b0f2009-07-19 10:01:32 -04005253
James Smart4cc0e562010-01-26 23:09:48 -05005254 spin_lock_irqsave(&phba->ct_ev_lock, flags);
5255 dd_data = (struct bsg_job_data *)job->dd_data;
James Smarta33c4f72013-03-01 16:36:00 -05005256 if (dd_data) {
5257 dd_data->set_job = NULL;
5258 job->dd_data = NULL;
5259 } else {
James Smart4cc0e562010-01-26 23:09:48 -05005260 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smarta33c4f72013-03-01 16:36:00 -05005261 return -EAGAIN;
James Smart4cc0e562010-01-26 23:09:48 -05005262 }
5263
5264 switch (dd_data->type) {
5265 case TYPE_IOCB:
James Smarta33c4f72013-03-01 16:36:00 -05005266 /* Check to see if IOCB was issued to the port or not. If not,
5267 * remove it from the txq queue and call cancel iocbs.
5268 * Otherwise, call abort iotag
James Smart4cc0e562010-01-26 23:09:48 -05005269 */
James Smarta33c4f72013-03-01 16:36:00 -05005270
5271 cmdiocb = dd_data->context_un.iocb.cmdiocbq;
5272 spin_lock_irq(&phba->hbalock);
5273 list_for_each_entry_safe(check_iocb, next_iocb, &pring->txq,
5274 list) {
5275 if (check_iocb == cmdiocb) {
5276 list_move_tail(&check_iocb->list, &completions);
5277 break;
5278 }
5279 }
5280 if (list_empty(&completions))
5281 lpfc_sli_issue_abort_iotag(phba, pring, cmdiocb);
5282 spin_unlock_irq(&phba->hbalock);
James Smart4cc0e562010-01-26 23:09:48 -05005283 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smarta33c4f72013-03-01 16:36:00 -05005284 if (!list_empty(&completions)) {
5285 lpfc_sli_cancel_iocbs(phba, &completions,
5286 IOSTAT_LOCAL_REJECT,
5287 IOERR_SLI_ABORTED);
5288 }
James Smart4cc0e562010-01-26 23:09:48 -05005289 break;
James Smarta33c4f72013-03-01 16:36:00 -05005290
5291 case TYPE_EVT:
James Smart3b5dd522010-01-26 23:10:15 -05005292 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smarta33c4f72013-03-01 16:36:00 -05005293 break;
5294
5295 case TYPE_MBOX:
5296 /* Update the ext buf ctx state if needed */
5297
James Smart7ad20aa2011-05-24 11:44:28 -04005298 if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_PORT)
5299 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_ABTS;
James Smarta33c4f72013-03-01 16:36:00 -05005300 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smart3b5dd522010-01-26 23:10:15 -05005301 break;
James Smarte2aed292010-02-26 14:15:00 -05005302 case TYPE_MENLO:
James Smarta33c4f72013-03-01 16:36:00 -05005303 /* Check to see if IOCB was issued to the port or not. If not,
5304 * remove it from the txq queue and call cancel iocbs.
5305 * Otherwise, call abort iotag.
5306 */
5307
5308 cmdiocb = dd_data->context_un.menlo.cmdiocbq;
James Smarte2aed292010-02-26 14:15:00 -05005309 spin_lock_irq(&phba->hbalock);
James Smarta33c4f72013-03-01 16:36:00 -05005310 list_for_each_entry_safe(check_iocb, next_iocb, &pring->txq,
5311 list) {
5312 if (check_iocb == cmdiocb) {
5313 list_move_tail(&check_iocb->list, &completions);
5314 break;
5315 }
5316 }
5317 if (list_empty(&completions))
5318 lpfc_sli_issue_abort_iotag(phba, pring, cmdiocb);
James Smarte2aed292010-02-26 14:15:00 -05005319 spin_unlock_irq(&phba->hbalock);
James Smarta33c4f72013-03-01 16:36:00 -05005320 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5321 if (!list_empty(&completions)) {
5322 lpfc_sli_cancel_iocbs(phba, &completions,
5323 IOSTAT_LOCAL_REJECT,
5324 IOERR_SLI_ABORTED);
5325 }
James Smarte2aed292010-02-26 14:15:00 -05005326 break;
James Smart4cc0e562010-01-26 23:09:48 -05005327 default:
5328 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5329 break;
5330 }
James Smartf1c3b0f2009-07-19 10:01:32 -04005331
James Smart4cc0e562010-01-26 23:09:48 -05005332 /* scsi transport fc fc_bsg_job_timeout expects a zero return code,
5333 * otherwise an error message will be displayed on the console
5334 * so always return success (zero)
5335 */
James Smarta33c4f72013-03-01 16:36:00 -05005336 return rc;
James Smartf1c3b0f2009-07-19 10:01:32 -04005337}