blob: b7216d694bff1598dfa955b238a0a8241a524b82 [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 Smart95df18c2019-03-12 16:30:32 -07004 * Copyright (C) 2017-2019 Broadcom. All Rights Reserved. The term *
James Smart4ae2ebd2018-06-26 08:24:31 -07005 * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. *
James Smartf25e8e72015-04-07 15:07:28 -04006 * Copyright (C) 2009-2015 Emulex. All rights reserved. *
James Smartf1c3b0f2009-07-19 10:01:32 -04007 * EMULEX and SLI are trademarks of Emulex. *
James Smartd080abe2017-02-12 13:52:39 -08008 * www.broadcom.com *
James Smartf1c3b0f2009-07-19 10:01:32 -04009 * *
10 * This program is free software; you can redistribute it and/or *
11 * modify it under the terms of version 2 of the GNU General *
12 * Public License as published by the Free Software Foundation. *
13 * This program is distributed in the hope that it will be useful. *
14 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
15 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
16 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
17 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
18 * TO BE LEGALLY INVALID. See the GNU General Public License for *
19 * more details, a copy of which can be found in the file COPYING *
20 * included with this package. *
21 *******************************************************************/
22
23#include <linux/interrupt.h>
24#include <linux/mempool.h>
25#include <linux/pci.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
James Smart277e76f2010-02-18 11:07:15 -050027#include <linux/delay.h>
James Smart7ad20aa2011-05-24 11:44:28 -040028#include <linux/list.h>
Johannes Thumshirneb340942016-11-17 10:31:11 +010029#include <linux/bsg-lib.h>
James Smartd2cc9bc2018-09-10 10:30:50 -070030#include <linux/vmalloc.h>
James Smartf1c3b0f2009-07-19 10:01:32 -040031
32#include <scsi/scsi.h>
33#include <scsi/scsi_host.h>
34#include <scsi/scsi_transport_fc.h>
35#include <scsi/scsi_bsg_fc.h>
James Smart6a9c52c2009-10-02 15:16:51 -040036#include <scsi/fc/fc_fs.h>
James Smartf1c3b0f2009-07-19 10:01:32 -040037
38#include "lpfc_hw4.h"
39#include "lpfc_hw.h"
40#include "lpfc_sli.h"
41#include "lpfc_sli4.h"
42#include "lpfc_nl.h"
James Smart4fede782010-01-26 23:08:55 -050043#include "lpfc_bsg.h"
James Smartf1c3b0f2009-07-19 10:01:32 -040044#include "lpfc_disc.h"
45#include "lpfc_scsi.h"
46#include "lpfc.h"
47#include "lpfc_logmsg.h"
48#include "lpfc_crtn.h"
James Smartb76f2dc2011-07-22 18:37:42 -040049#include "lpfc_debugfs.h"
James Smartf1c3b0f2009-07-19 10:01:32 -040050#include "lpfc_vport.h"
51#include "lpfc_version.h"
52
James Smart4cc0e562010-01-26 23:09:48 -050053struct lpfc_bsg_event {
54 struct list_head node;
55 struct kref kref;
56 wait_queue_head_t wq;
57
58 /* Event type and waiter identifiers */
59 uint32_t type_mask;
60 uint32_t req_id;
61 uint32_t reg_id;
62
63 /* next two flags are here for the auto-delete logic */
64 unsigned long wait_time_stamp;
65 int waiting;
66
67 /* seen and not seen events */
68 struct list_head events_to_get;
69 struct list_head events_to_see;
70
James Smarta33c4f72013-03-01 16:36:00 -050071 /* driver data associated with the job */
72 void *dd_data;
James Smart4cc0e562010-01-26 23:09:48 -050073};
74
75struct lpfc_bsg_iocb {
76 struct lpfc_iocbq *cmdiocbq;
James Smarta33c4f72013-03-01 16:36:00 -050077 struct lpfc_dmabuf *rmp;
James Smart4cc0e562010-01-26 23:09:48 -050078 struct lpfc_nodelist *ndlp;
James Smart4cc0e562010-01-26 23:09:48 -050079};
80
James Smart3b5dd522010-01-26 23:10:15 -050081struct lpfc_bsg_mbox {
82 LPFC_MBOXQ_t *pmboxq;
83 MAILBOX_t *mb;
James Smart7ad20aa2011-05-24 11:44:28 -040084 struct lpfc_dmabuf *dmabuffers; /* for BIU diags */
James Smart7a470272010-03-15 11:25:20 -040085 uint8_t *ext; /* extended mailbox data */
86 uint32_t mbOffset; /* from app */
87 uint32_t inExtWLen; /* from app */
James Smartc7495932010-04-06 15:05:28 -040088 uint32_t outExtWLen; /* from app */
James Smart3b5dd522010-01-26 23:10:15 -050089};
90
James Smarte2aed292010-02-26 14:15:00 -050091#define MENLO_DID 0x0000FC0E
92
93struct lpfc_bsg_menlo {
94 struct lpfc_iocbq *cmdiocbq;
James Smarta33c4f72013-03-01 16:36:00 -050095 struct lpfc_dmabuf *rmp;
James Smarte2aed292010-02-26 14:15:00 -050096};
97
James Smart4cc0e562010-01-26 23:09:48 -050098#define TYPE_EVT 1
99#define TYPE_IOCB 2
James Smart3b5dd522010-01-26 23:10:15 -0500100#define TYPE_MBOX 3
James Smarte2aed292010-02-26 14:15:00 -0500101#define TYPE_MENLO 4
James Smart4cc0e562010-01-26 23:09:48 -0500102struct bsg_job_data {
103 uint32_t type;
Johannes Thumshirn75cc8cf2016-11-17 10:31:19 +0100104 struct bsg_job *set_job; /* job waiting for this iocb to finish */
James Smart4cc0e562010-01-26 23:09:48 -0500105 union {
106 struct lpfc_bsg_event *evt;
107 struct lpfc_bsg_iocb iocb;
James Smart3b5dd522010-01-26 23:10:15 -0500108 struct lpfc_bsg_mbox mbox;
James Smarte2aed292010-02-26 14:15:00 -0500109 struct lpfc_bsg_menlo menlo;
James Smart4cc0e562010-01-26 23:09:48 -0500110 } context_un;
111};
112
113struct event_data {
114 struct list_head node;
115 uint32_t type;
116 uint32_t immed_dat;
117 void *data;
118 uint32_t len;
119};
120
James Smart3b5dd522010-01-26 23:10:15 -0500121#define BUF_SZ_4K 4096
James Smart4cc0e562010-01-26 23:09:48 -0500122#define SLI_CT_ELX_LOOPBACK 0x10
123
124enum ELX_LOOPBACK_CMD {
125 ELX_LOOPBACK_XRI_SETUP,
126 ELX_LOOPBACK_DATA,
127};
128
James Smart3b5dd522010-01-26 23:10:15 -0500129#define ELX_LOOPBACK_HEADER_SZ \
130 (size_t)(&((struct lpfc_sli_ct_request *)NULL)->un)
131
James Smart4cc0e562010-01-26 23:09:48 -0500132struct lpfc_dmabufext {
133 struct lpfc_dmabuf dma;
134 uint32_t size;
135 uint32_t flag;
136};
137
James Smarta33c4f72013-03-01 16:36:00 -0500138static void
139lpfc_free_bsg_buffers(struct lpfc_hba *phba, struct lpfc_dmabuf *mlist)
140{
141 struct lpfc_dmabuf *mlast, *next_mlast;
142
143 if (mlist) {
144 list_for_each_entry_safe(mlast, next_mlast, &mlist->list,
145 list) {
146 lpfc_mbuf_free(phba, mlast->virt, mlast->phys);
147 list_del(&mlast->list);
148 kfree(mlast);
149 }
150 lpfc_mbuf_free(phba, mlist->virt, mlist->phys);
151 kfree(mlist);
152 }
153 return;
154}
155
156static struct lpfc_dmabuf *
157lpfc_alloc_bsg_buffers(struct lpfc_hba *phba, unsigned int size,
158 int outbound_buffers, struct ulp_bde64 *bpl,
159 int *bpl_entries)
160{
161 struct lpfc_dmabuf *mlist = NULL;
162 struct lpfc_dmabuf *mp;
163 unsigned int bytes_left = size;
164
165 /* Verify we can support the size specified */
166 if (!size || (size > (*bpl_entries * LPFC_BPL_SIZE)))
167 return NULL;
168
169 /* Determine the number of dma buffers to allocate */
170 *bpl_entries = (size % LPFC_BPL_SIZE ? size/LPFC_BPL_SIZE + 1 :
171 size/LPFC_BPL_SIZE);
172
173 /* Allocate dma buffer and place in BPL passed */
174 while (bytes_left) {
175 /* Allocate dma buffer */
176 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
177 if (!mp) {
178 if (mlist)
179 lpfc_free_bsg_buffers(phba, mlist);
180 return NULL;
181 }
182
183 INIT_LIST_HEAD(&mp->list);
184 mp->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &(mp->phys));
185
186 if (!mp->virt) {
187 kfree(mp);
188 if (mlist)
189 lpfc_free_bsg_buffers(phba, mlist);
190 return NULL;
191 }
192
193 /* Queue it to a linked list */
194 if (!mlist)
195 mlist = mp;
196 else
197 list_add_tail(&mp->list, &mlist->list);
198
199 /* Add buffer to buffer pointer list */
200 if (outbound_buffers)
201 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
202 else
203 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
204 bpl->addrLow = le32_to_cpu(putPaddrLow(mp->phys));
205 bpl->addrHigh = le32_to_cpu(putPaddrHigh(mp->phys));
206 bpl->tus.f.bdeSize = (uint16_t)
207 (bytes_left >= LPFC_BPL_SIZE ? LPFC_BPL_SIZE :
208 bytes_left);
209 bytes_left -= bpl->tus.f.bdeSize;
210 bpl->tus.w = le32_to_cpu(bpl->tus.w);
211 bpl++;
212 }
213 return mlist;
214}
215
216static unsigned int
217lpfc_bsg_copy_data(struct lpfc_dmabuf *dma_buffers,
Johannes Thumshirneb340942016-11-17 10:31:11 +0100218 struct bsg_buffer *bsg_buffers,
James Smarta33c4f72013-03-01 16:36:00 -0500219 unsigned int bytes_to_transfer, int to_buffers)
220{
221
222 struct lpfc_dmabuf *mp;
223 unsigned int transfer_bytes, bytes_copied = 0;
224 unsigned int sg_offset, dma_offset;
225 unsigned char *dma_address, *sg_address;
James Smarta33c4f72013-03-01 16:36:00 -0500226 LIST_HEAD(temp_list);
James Smartd5ce53b2013-04-17 20:17:26 -0400227 struct sg_mapping_iter miter;
228 unsigned long flags;
229 unsigned int sg_flags = SG_MITER_ATOMIC;
230 bool sg_valid;
James Smarta33c4f72013-03-01 16:36:00 -0500231
232 list_splice_init(&dma_buffers->list, &temp_list);
233 list_add(&dma_buffers->list, &temp_list);
234 sg_offset = 0;
James Smartd5ce53b2013-04-17 20:17:26 -0400235 if (to_buffers)
236 sg_flags |= SG_MITER_FROM_SG;
237 else
238 sg_flags |= SG_MITER_TO_SG;
239 sg_miter_start(&miter, bsg_buffers->sg_list, bsg_buffers->sg_cnt,
240 sg_flags);
241 local_irq_save(flags);
242 sg_valid = sg_miter_next(&miter);
James Smarta33c4f72013-03-01 16:36:00 -0500243 list_for_each_entry(mp, &temp_list, list) {
244 dma_offset = 0;
James Smartd5ce53b2013-04-17 20:17:26 -0400245 while (bytes_to_transfer && sg_valid &&
James Smarta33c4f72013-03-01 16:36:00 -0500246 (dma_offset < LPFC_BPL_SIZE)) {
247 dma_address = mp->virt + dma_offset;
248 if (sg_offset) {
249 /* Continue previous partial transfer of sg */
James Smartd5ce53b2013-04-17 20:17:26 -0400250 sg_address = miter.addr + sg_offset;
251 transfer_bytes = miter.length - sg_offset;
James Smarta33c4f72013-03-01 16:36:00 -0500252 } else {
James Smartd5ce53b2013-04-17 20:17:26 -0400253 sg_address = miter.addr;
254 transfer_bytes = miter.length;
James Smarta33c4f72013-03-01 16:36:00 -0500255 }
256 if (bytes_to_transfer < transfer_bytes)
257 transfer_bytes = bytes_to_transfer;
258 if (transfer_bytes > (LPFC_BPL_SIZE - dma_offset))
259 transfer_bytes = LPFC_BPL_SIZE - dma_offset;
260 if (to_buffers)
261 memcpy(dma_address, sg_address, transfer_bytes);
262 else
263 memcpy(sg_address, dma_address, transfer_bytes);
264 dma_offset += transfer_bytes;
265 sg_offset += transfer_bytes;
266 bytes_to_transfer -= transfer_bytes;
267 bytes_copied += transfer_bytes;
James Smartd5ce53b2013-04-17 20:17:26 -0400268 if (sg_offset >= miter.length) {
James Smarta33c4f72013-03-01 16:36:00 -0500269 sg_offset = 0;
James Smartd5ce53b2013-04-17 20:17:26 -0400270 sg_valid = sg_miter_next(&miter);
James Smarta33c4f72013-03-01 16:36:00 -0500271 }
272 }
273 }
James Smartd5ce53b2013-04-17 20:17:26 -0400274 sg_miter_stop(&miter);
275 local_irq_restore(flags);
James Smarta33c4f72013-03-01 16:36:00 -0500276 list_del_init(&dma_buffers->list);
277 list_splice(&temp_list, &dma_buffers->list);
278 return bytes_copied;
279}
280
James Smartf1c3b0f2009-07-19 10:01:32 -0400281/**
James Smart4cc0e562010-01-26 23:09:48 -0500282 * lpfc_bsg_send_mgmt_cmd_cmp - lpfc_bsg_send_mgmt_cmd's completion handler
283 * @phba: Pointer to HBA context object.
284 * @cmdiocbq: Pointer to command iocb.
285 * @rspiocbq: Pointer to response iocb.
286 *
287 * This function is the completion handler for iocbs issued using
288 * lpfc_bsg_send_mgmt_cmd function. This function is called by the
289 * ring event handler function without any lock held. This function
290 * can be called from both worker thread context and interrupt
291 * context. This function also can be called from another thread which
292 * cleans up the SLI layer objects.
293 * This function copies the contents of the response iocb to the
294 * response iocb memory object provided by the caller of
295 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
296 * sleeps for the iocb completion.
297 **/
298static void
299lpfc_bsg_send_mgmt_cmd_cmp(struct lpfc_hba *phba,
300 struct lpfc_iocbq *cmdiocbq,
301 struct lpfc_iocbq *rspiocbq)
302{
James Smart4cc0e562010-01-26 23:09:48 -0500303 struct bsg_job_data *dd_data;
Johannes Thumshirn75cc8cf2016-11-17 10:31:19 +0100304 struct bsg_job *job;
Johannes Thumshirn01e0e152016-11-17 10:31:12 +0100305 struct fc_bsg_reply *bsg_reply;
James Smart4cc0e562010-01-26 23:09:48 -0500306 IOCB_t *rsp;
James Smarta33c4f72013-03-01 16:36:00 -0500307 struct lpfc_dmabuf *bmp, *cmp, *rmp;
James Smart4cc0e562010-01-26 23:09:48 -0500308 struct lpfc_nodelist *ndlp;
309 struct lpfc_bsg_iocb *iocb;
310 unsigned long flags;
James Smarta33c4f72013-03-01 16:36:00 -0500311 unsigned int rsp_size;
James Smart4cc0e562010-01-26 23:09:48 -0500312 int rc = 0;
313
James Smarta33c4f72013-03-01 16:36:00 -0500314 dd_data = cmdiocbq->context1;
315
316 /* Determine if job has been aborted */
James Smart4cc0e562010-01-26 23:09:48 -0500317 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smarta33c4f72013-03-01 16:36:00 -0500318 job = dd_data->set_job;
319 if (job) {
Johannes Thumshirn01e0e152016-11-17 10:31:12 +0100320 bsg_reply = job->reply;
James Smarta33c4f72013-03-01 16:36:00 -0500321 /* Prevent timeout handling from trying to abort job */
322 job->dd_data = NULL;
James Smart4cc0e562010-01-26 23:09:48 -0500323 }
James Smarta33c4f72013-03-01 16:36:00 -0500324 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smart4cc0e562010-01-26 23:09:48 -0500325
James Smartb5a9b2d2013-09-06 12:19:45 -0400326 /* Close the timeout handler abort window */
327 spin_lock_irqsave(&phba->hbalock, flags);
James Smart1b8d11a2013-09-06 12:20:51 -0400328 cmdiocbq->iocb_flag &= ~LPFC_IO_CMD_OUTSTANDING;
James Smartb5a9b2d2013-09-06 12:19:45 -0400329 spin_unlock_irqrestore(&phba->hbalock, flags);
330
James Smart4cc0e562010-01-26 23:09:48 -0500331 iocb = &dd_data->context_un.iocb;
James Smarta33c4f72013-03-01 16:36:00 -0500332 ndlp = iocb->ndlp;
333 rmp = iocb->rmp;
334 cmp = cmdiocbq->context2;
335 bmp = cmdiocbq->context3;
James Smart4cc0e562010-01-26 23:09:48 -0500336 rsp = &rspiocbq->iocb;
James Smart4cc0e562010-01-26 23:09:48 -0500337
James Smarta33c4f72013-03-01 16:36:00 -0500338 /* Copy the completed data or set the error status */
James Smart4cc0e562010-01-26 23:09:48 -0500339
James Smarta33c4f72013-03-01 16:36:00 -0500340 if (job) {
341 if (rsp->ulpStatus) {
342 if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
343 switch (rsp->un.ulpWord[4] & IOERR_PARAM_MASK) {
344 case IOERR_SEQUENCE_TIMEOUT:
345 rc = -ETIMEDOUT;
346 break;
347 case IOERR_INVALID_RPI:
348 rc = -EFAULT;
349 break;
350 default:
351 rc = -EACCES;
352 break;
353 }
354 } else {
James Smart4cc0e562010-01-26 23:09:48 -0500355 rc = -EACCES;
James Smart4cc0e562010-01-26 23:09:48 -0500356 }
James Smarta33c4f72013-03-01 16:36:00 -0500357 } else {
358 rsp_size = rsp->un.genreq64.bdl.bdeSize;
Johannes Thumshirn01e0e152016-11-17 10:31:12 +0100359 bsg_reply->reply_payload_rcv_len =
James Smarta33c4f72013-03-01 16:36:00 -0500360 lpfc_bsg_copy_data(rmp, &job->reply_payload,
361 rsp_size, 0);
362 }
363 }
James Smart4cc0e562010-01-26 23:09:48 -0500364
James Smarta33c4f72013-03-01 16:36:00 -0500365 lpfc_free_bsg_buffers(phba, cmp);
366 lpfc_free_bsg_buffers(phba, rmp);
James Smart4cc0e562010-01-26 23:09:48 -0500367 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
James Smarta33c4f72013-03-01 16:36:00 -0500368 kfree(bmp);
James Smart4cc0e562010-01-26 23:09:48 -0500369 lpfc_sli_release_iocbq(phba, cmdiocbq);
370 lpfc_nlp_put(ndlp);
James Smart4cc0e562010-01-26 23:09:48 -0500371 kfree(dd_data);
James Smarta33c4f72013-03-01 16:36:00 -0500372
373 /* Complete the job if the job is still active */
374
375 if (job) {
Johannes Thumshirn01e0e152016-11-17 10:31:12 +0100376 bsg_reply->result = rc;
Johannes Thumshirn06548162016-11-17 10:31:22 +0100377 bsg_job_done(job, bsg_reply->result,
Johannes Thumshirn1abaede2016-11-17 10:31:13 +0100378 bsg_reply->reply_payload_rcv_len);
James Smarta33c4f72013-03-01 16:36:00 -0500379 }
James Smart4cc0e562010-01-26 23:09:48 -0500380 return;
381}
382
383/**
384 * lpfc_bsg_send_mgmt_cmd - send a CT command from a bsg request
James Smartf1c3b0f2009-07-19 10:01:32 -0400385 * @job: fc_bsg_job to handle
James Smart3b5dd522010-01-26 23:10:15 -0500386 **/
James Smartf1c3b0f2009-07-19 10:01:32 -0400387static int
Johannes Thumshirn75cc8cf2016-11-17 10:31:19 +0100388lpfc_bsg_send_mgmt_cmd(struct bsg_job *job)
James Smartf1c3b0f2009-07-19 10:01:32 -0400389{
Johannes Thumshirncd21c602016-11-17 10:31:14 +0100390 struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
James Smartf1c3b0f2009-07-19 10:01:32 -0400391 struct lpfc_hba *phba = vport->phba;
Johannes Thumshirn1d69b122016-11-17 10:31:15 +0100392 struct lpfc_rport_data *rdata = fc_bsg_to_rport(job)->dd_data;
James Smartf1c3b0f2009-07-19 10:01:32 -0400393 struct lpfc_nodelist *ndlp = rdata->pnode;
Johannes Thumshirn01e0e152016-11-17 10:31:12 +0100394 struct fc_bsg_reply *bsg_reply = job->reply;
James Smartf1c3b0f2009-07-19 10:01:32 -0400395 struct ulp_bde64 *bpl = NULL;
396 uint32_t timeout;
397 struct lpfc_iocbq *cmdiocbq = NULL;
James Smartf1c3b0f2009-07-19 10:01:32 -0400398 IOCB_t *cmd;
James Smarta33c4f72013-03-01 16:36:00 -0500399 struct lpfc_dmabuf *bmp = NULL, *cmp = NULL, *rmp = NULL;
James Smartf1c3b0f2009-07-19 10:01:32 -0400400 int request_nseg;
401 int reply_nseg;
James Smart4cc0e562010-01-26 23:09:48 -0500402 struct bsg_job_data *dd_data;
James Smartb5a9b2d2013-09-06 12:19:45 -0400403 unsigned long flags;
James Smart4cc0e562010-01-26 23:09:48 -0500404 uint32_t creg_val;
James Smartf1c3b0f2009-07-19 10:01:32 -0400405 int rc = 0;
James Smartd439d282010-09-29 11:18:45 -0400406 int iocb_stat;
James Smartf1c3b0f2009-07-19 10:01:32 -0400407
408 /* in case no data is transferred */
Johannes Thumshirn01e0e152016-11-17 10:31:12 +0100409 bsg_reply->reply_payload_rcv_len = 0;
James Smartf1c3b0f2009-07-19 10:01:32 -0400410
James Smart4cc0e562010-01-26 23:09:48 -0500411 /* allocate our bsg tracking structure */
412 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
413 if (!dd_data) {
414 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
415 "2733 Failed allocation of dd_data\n");
416 rc = -ENOMEM;
417 goto no_dd_data;
418 }
419
James Smartf1c3b0f2009-07-19 10:01:32 -0400420 if (!lpfc_nlp_get(ndlp)) {
James Smart4cc0e562010-01-26 23:09:48 -0500421 rc = -ENODEV;
422 goto no_ndlp;
423 }
424
James Smartf1c3b0f2009-07-19 10:01:32 -0400425 if (ndlp->nlp_flag & NLP_ELS_SND_MASK) {
426 rc = -ENODEV;
James Smarta33c4f72013-03-01 16:36:00 -0500427 goto free_ndlp;
James Smartf1c3b0f2009-07-19 10:01:32 -0400428 }
429
James Smartf1c3b0f2009-07-19 10:01:32 -0400430 cmdiocbq = lpfc_sli_get_iocbq(phba);
431 if (!cmdiocbq) {
432 rc = -ENOMEM;
James Smarta33c4f72013-03-01 16:36:00 -0500433 goto free_ndlp;
James Smartf1c3b0f2009-07-19 10:01:32 -0400434 }
James Smartf1c3b0f2009-07-19 10:01:32 -0400435
James Smart4cc0e562010-01-26 23:09:48 -0500436 cmd = &cmdiocbq->iocb;
James Smarta33c4f72013-03-01 16:36:00 -0500437
438 bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
439 if (!bmp) {
James Smartf1c3b0f2009-07-19 10:01:32 -0400440 rc = -ENOMEM;
James Smartbe858b62010-12-15 17:57:20 -0500441 goto free_cmdiocbq;
James Smartf1c3b0f2009-07-19 10:01:32 -0400442 }
James Smarta33c4f72013-03-01 16:36:00 -0500443 bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
444 if (!bmp->virt) {
445 rc = -ENOMEM;
446 goto free_bmp;
James Smartf1c3b0f2009-07-19 10:01:32 -0400447 }
448
James Smarta33c4f72013-03-01 16:36:00 -0500449 INIT_LIST_HEAD(&bmp->list);
450
451 bpl = (struct ulp_bde64 *) bmp->virt;
452 request_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64);
453 cmp = lpfc_alloc_bsg_buffers(phba, job->request_payload.payload_len,
454 1, bpl, &request_nseg);
455 if (!cmp) {
456 rc = -ENOMEM;
457 goto free_bmp;
458 }
459 lpfc_bsg_copy_data(cmp, &job->request_payload,
460 job->request_payload.payload_len, 1);
461
462 bpl += request_nseg;
463 reply_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64) - request_nseg;
464 rmp = lpfc_alloc_bsg_buffers(phba, job->reply_payload.payload_len, 0,
465 bpl, &reply_nseg);
466 if (!rmp) {
467 rc = -ENOMEM;
468 goto free_cmp;
James Smartf1c3b0f2009-07-19 10:01:32 -0400469 }
470
471 cmd->un.genreq64.bdl.ulpIoTag32 = 0;
472 cmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
473 cmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
474 cmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
475 cmd->un.genreq64.bdl.bdeSize =
476 (request_nseg + reply_nseg) * sizeof(struct ulp_bde64);
477 cmd->ulpCommand = CMD_GEN_REQUEST64_CR;
478 cmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
479 cmd->un.genreq64.w5.hcsw.Dfctl = 0;
James Smart6a9c52c2009-10-02 15:16:51 -0400480 cmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
481 cmd->un.genreq64.w5.hcsw.Type = FC_TYPE_CT;
James Smartf1c3b0f2009-07-19 10:01:32 -0400482 cmd->ulpBdeCount = 1;
483 cmd->ulpLe = 1;
484 cmd->ulpClass = CLASS3;
485 cmd->ulpContext = ndlp->nlp_rpi;
James Smart6d368e52011-05-24 11:44:12 -0400486 if (phba->sli_rev == LPFC_SLI_REV4)
487 cmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smartf1c3b0f2009-07-19 10:01:32 -0400488 cmd->ulpOwner = OWN_CHIP;
489 cmdiocbq->vport = phba->pport;
James Smart4cc0e562010-01-26 23:09:48 -0500490 cmdiocbq->context3 = bmp;
James Smartf1c3b0f2009-07-19 10:01:32 -0400491 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
James Smartf1c3b0f2009-07-19 10:01:32 -0400492 timeout = phba->fc_ratov * 2;
James Smart4cc0e562010-01-26 23:09:48 -0500493 cmd->ulpTimeout = timeout;
James Smartf1c3b0f2009-07-19 10:01:32 -0400494
James Smart4cc0e562010-01-26 23:09:48 -0500495 cmdiocbq->iocb_cmpl = lpfc_bsg_send_mgmt_cmd_cmp;
James Smarta33c4f72013-03-01 16:36:00 -0500496 cmdiocbq->context1 = dd_data;
497 cmdiocbq->context2 = cmp;
498 cmdiocbq->context3 = bmp;
James Smartd5ce53b2013-04-17 20:17:26 -0400499 cmdiocbq->context_un.ndlp = ndlp;
James Smart4cc0e562010-01-26 23:09:48 -0500500 dd_data->type = TYPE_IOCB;
James Smarta33c4f72013-03-01 16:36:00 -0500501 dd_data->set_job = job;
James Smart4cc0e562010-01-26 23:09:48 -0500502 dd_data->context_un.iocb.cmdiocbq = cmdiocbq;
James Smarta33c4f72013-03-01 16:36:00 -0500503 dd_data->context_un.iocb.ndlp = ndlp;
504 dd_data->context_un.iocb.rmp = rmp;
505 job->dd_data = dd_data;
James Smartf1c3b0f2009-07-19 10:01:32 -0400506
James Smart4cc0e562010-01-26 23:09:48 -0500507 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -0500508 if (lpfc_readl(phba->HCregaddr, &creg_val)) {
509 rc = -EIO ;
James Smarta33c4f72013-03-01 16:36:00 -0500510 goto free_rmp;
James Smart9940b972011-03-11 16:06:12 -0500511 }
James Smart4cc0e562010-01-26 23:09:48 -0500512 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
513 writel(creg_val, phba->HCregaddr);
514 readl(phba->HCregaddr); /* flush */
James Smartf1c3b0f2009-07-19 10:01:32 -0400515 }
516
James Smartd439d282010-09-29 11:18:45 -0400517 iocb_stat = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq, 0);
James Smartb5a9b2d2013-09-06 12:19:45 -0400518
519 if (iocb_stat == IOCB_SUCCESS) {
520 spin_lock_irqsave(&phba->hbalock, flags);
521 /* make sure the I/O had not been completed yet */
522 if (cmdiocbq->iocb_flag & LPFC_IO_LIBDFC) {
523 /* open up abort window to timeout handler */
James Smart1b8d11a2013-09-06 12:20:51 -0400524 cmdiocbq->iocb_flag |= LPFC_IO_CMD_OUTSTANDING;
James Smartb5a9b2d2013-09-06 12:19:45 -0400525 }
526 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4cc0e562010-01-26 23:09:48 -0500527 return 0; /* done for now */
James Smartb5a9b2d2013-09-06 12:19:45 -0400528 } else if (iocb_stat == IOCB_BUSY) {
James Smartd439d282010-09-29 11:18:45 -0400529 rc = -EAGAIN;
James Smartb5a9b2d2013-09-06 12:19:45 -0400530 } else {
James Smartd439d282010-09-29 11:18:45 -0400531 rc = -EIO;
James Smartb5a9b2d2013-09-06 12:19:45 -0400532 }
James Smart2a9bf3d2010-06-07 15:24:45 -0400533
James Smart4cc0e562010-01-26 23:09:48 -0500534 /* iocb failed so cleanup */
James Smartb5a9b2d2013-09-06 12:19:45 -0400535 job->dd_data = NULL;
James Smartf1c3b0f2009-07-19 10:01:32 -0400536
James Smarta33c4f72013-03-01 16:36:00 -0500537free_rmp:
538 lpfc_free_bsg_buffers(phba, rmp);
539free_cmp:
540 lpfc_free_bsg_buffers(phba, cmp);
541free_bmp:
542 if (bmp->virt)
543 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
544 kfree(bmp);
James Smartf1c3b0f2009-07-19 10:01:32 -0400545free_cmdiocbq:
546 lpfc_sli_release_iocbq(phba, cmdiocbq);
James Smart4cc0e562010-01-26 23:09:48 -0500547free_ndlp:
James Smartf1c3b0f2009-07-19 10:01:32 -0400548 lpfc_nlp_put(ndlp);
James Smart4cc0e562010-01-26 23:09:48 -0500549no_ndlp:
550 kfree(dd_data);
551no_dd_data:
James Smartf1c3b0f2009-07-19 10:01:32 -0400552 /* make error code available to userspace */
Johannes Thumshirn01e0e152016-11-17 10:31:12 +0100553 bsg_reply->result = rc;
James Smart4cc0e562010-01-26 23:09:48 -0500554 job->dd_data = NULL;
555 return rc;
556}
557
558/**
559 * lpfc_bsg_rport_els_cmp - lpfc_bsg_rport_els's completion handler
560 * @phba: Pointer to HBA context object.
561 * @cmdiocbq: Pointer to command iocb.
562 * @rspiocbq: Pointer to response iocb.
563 *
564 * This function is the completion handler for iocbs issued using
565 * lpfc_bsg_rport_els_cmp function. This function is called by the
566 * ring event handler function without any lock held. This function
567 * can be called from both worker thread context and interrupt
568 * context. This function also can be called from other thread which
569 * cleans up the SLI layer objects.
James Smart3b5dd522010-01-26 23:10:15 -0500570 * This function copies the contents of the response iocb to the
James Smart4cc0e562010-01-26 23:09:48 -0500571 * response iocb memory object provided by the caller of
572 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
573 * sleeps for the iocb completion.
574 **/
575static void
576lpfc_bsg_rport_els_cmp(struct lpfc_hba *phba,
577 struct lpfc_iocbq *cmdiocbq,
578 struct lpfc_iocbq *rspiocbq)
579{
580 struct bsg_job_data *dd_data;
Johannes Thumshirn75cc8cf2016-11-17 10:31:19 +0100581 struct bsg_job *job;
Johannes Thumshirn01e0e152016-11-17 10:31:12 +0100582 struct fc_bsg_reply *bsg_reply;
James Smart4cc0e562010-01-26 23:09:48 -0500583 IOCB_t *rsp;
584 struct lpfc_nodelist *ndlp;
James Smarta33c4f72013-03-01 16:36:00 -0500585 struct lpfc_dmabuf *pcmd = NULL, *prsp = NULL;
James Smart4cc0e562010-01-26 23:09:48 -0500586 struct fc_bsg_ctels_reply *els_reply;
587 uint8_t *rjt_data;
588 unsigned long flags;
James Smarta33c4f72013-03-01 16:36:00 -0500589 unsigned int rsp_size;
James Smart4cc0e562010-01-26 23:09:48 -0500590 int rc = 0;
591
James Smart4cc0e562010-01-26 23:09:48 -0500592 dd_data = cmdiocbq->context1;
James Smarta33c4f72013-03-01 16:36:00 -0500593 ndlp = dd_data->context_un.iocb.ndlp;
594 cmdiocbq->context1 = ndlp;
595
596 /* Determine if job has been aborted */
597 spin_lock_irqsave(&phba->ct_ev_lock, flags);
598 job = dd_data->set_job;
599 if (job) {
Johannes Thumshirn01e0e152016-11-17 10:31:12 +0100600 bsg_reply = job->reply;
James Smarta33c4f72013-03-01 16:36:00 -0500601 /* Prevent timeout handling from trying to abort job */
602 job->dd_data = NULL;
603 }
604 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
605
James Smartb5a9b2d2013-09-06 12:19:45 -0400606 /* Close the timeout handler abort window */
607 spin_lock_irqsave(&phba->hbalock, flags);
James Smart1b8d11a2013-09-06 12:20:51 -0400608 cmdiocbq->iocb_flag &= ~LPFC_IO_CMD_OUTSTANDING;
James Smartb5a9b2d2013-09-06 12:19:45 -0400609 spin_unlock_irqrestore(&phba->hbalock, flags);
610
James Smarta33c4f72013-03-01 16:36:00 -0500611 rsp = &rspiocbq->iocb;
612 pcmd = (struct lpfc_dmabuf *)cmdiocbq->context2;
613 prsp = (struct lpfc_dmabuf *)pcmd->list.next;
614
615 /* Copy the completed job data or determine the job status if job is
616 * still active
617 */
618
619 if (job) {
620 if (rsp->ulpStatus == IOSTAT_SUCCESS) {
621 rsp_size = rsp->un.elsreq64.bdl.bdeSize;
Johannes Thumshirn01e0e152016-11-17 10:31:12 +0100622 bsg_reply->reply_payload_rcv_len =
James Smarta33c4f72013-03-01 16:36:00 -0500623 sg_copy_from_buffer(job->reply_payload.sg_list,
624 job->reply_payload.sg_cnt,
625 prsp->virt,
626 rsp_size);
627 } else if (rsp->ulpStatus == IOSTAT_LS_RJT) {
Johannes Thumshirn01e0e152016-11-17 10:31:12 +0100628 bsg_reply->reply_payload_rcv_len =
James Smarta33c4f72013-03-01 16:36:00 -0500629 sizeof(struct fc_bsg_ctels_reply);
630 /* LS_RJT data returned in word 4 */
631 rjt_data = (uint8_t *)&rsp->un.ulpWord[4];
Johannes Thumshirn01e0e152016-11-17 10:31:12 +0100632 els_reply = &bsg_reply->reply_data.ctels_reply;
James Smarta33c4f72013-03-01 16:36:00 -0500633 els_reply->status = FC_CTELS_STATUS_REJECT;
634 els_reply->rjt_data.action = rjt_data[3];
635 els_reply->rjt_data.reason_code = rjt_data[2];
636 els_reply->rjt_data.reason_explanation = rjt_data[1];
637 els_reply->rjt_data.vendor_unique = rjt_data[0];
638 } else {
639 rc = -EIO;
640 }
James Smart4cc0e562010-01-26 23:09:48 -0500641 }
642
James Smart4cc0e562010-01-26 23:09:48 -0500643 lpfc_nlp_put(ndlp);
James Smarta33c4f72013-03-01 16:36:00 -0500644 lpfc_els_free_iocb(phba, cmdiocbq);
James Smart4cc0e562010-01-26 23:09:48 -0500645 kfree(dd_data);
James Smarta33c4f72013-03-01 16:36:00 -0500646
647 /* Complete the job if the job is still active */
648
649 if (job) {
Johannes Thumshirn01e0e152016-11-17 10:31:12 +0100650 bsg_reply->result = rc;
Johannes Thumshirn06548162016-11-17 10:31:22 +0100651 bsg_job_done(job, bsg_reply->result,
Johannes Thumshirn1abaede2016-11-17 10:31:13 +0100652 bsg_reply->reply_payload_rcv_len);
James Smarta33c4f72013-03-01 16:36:00 -0500653 }
James Smart4cc0e562010-01-26 23:09:48 -0500654 return;
James Smartf1c3b0f2009-07-19 10:01:32 -0400655}
656
657/**
658 * lpfc_bsg_rport_els - send an ELS command from a bsg request
659 * @job: fc_bsg_job to handle
James Smart3b5dd522010-01-26 23:10:15 -0500660 **/
James Smartf1c3b0f2009-07-19 10:01:32 -0400661static int
Johannes Thumshirn75cc8cf2016-11-17 10:31:19 +0100662lpfc_bsg_rport_els(struct bsg_job *job)
James Smartf1c3b0f2009-07-19 10:01:32 -0400663{
Johannes Thumshirncd21c602016-11-17 10:31:14 +0100664 struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
James Smartf1c3b0f2009-07-19 10:01:32 -0400665 struct lpfc_hba *phba = vport->phba;
Johannes Thumshirn1d69b122016-11-17 10:31:15 +0100666 struct lpfc_rport_data *rdata = fc_bsg_to_rport(job)->dd_data;
James Smartf1c3b0f2009-07-19 10:01:32 -0400667 struct lpfc_nodelist *ndlp = rdata->pnode;
Johannes Thumshirn01e0e152016-11-17 10:31:12 +0100668 struct fc_bsg_request *bsg_request = job->request;
669 struct fc_bsg_reply *bsg_reply = job->reply;
James Smartf1c3b0f2009-07-19 10:01:32 -0400670 uint32_t elscmd;
671 uint32_t cmdsize;
James Smartf1c3b0f2009-07-19 10:01:32 -0400672 struct lpfc_iocbq *cmdiocbq;
James Smartf1c3b0f2009-07-19 10:01:32 -0400673 uint16_t rpi = 0;
James Smart4cc0e562010-01-26 23:09:48 -0500674 struct bsg_job_data *dd_data;
James Smartb5a9b2d2013-09-06 12:19:45 -0400675 unsigned long flags;
James Smart4cc0e562010-01-26 23:09:48 -0500676 uint32_t creg_val;
James Smartf1c3b0f2009-07-19 10:01:32 -0400677 int rc = 0;
678
679 /* in case no data is transferred */
Johannes Thumshirn01e0e152016-11-17 10:31:12 +0100680 bsg_reply->reply_payload_rcv_len = 0;
James Smartf1c3b0f2009-07-19 10:01:32 -0400681
James Smarta33c4f72013-03-01 16:36:00 -0500682 /* verify the els command is not greater than the
683 * maximum ELS transfer size.
684 */
685
686 if (job->request_payload.payload_len > FCELSSIZE) {
687 rc = -EINVAL;
688 goto no_dd_data;
689 }
690
James Smart4cc0e562010-01-26 23:09:48 -0500691 /* allocate our bsg tracking structure */
692 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
693 if (!dd_data) {
694 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
695 "2735 Failed allocation of dd_data\n");
696 rc = -ENOMEM;
697 goto no_dd_data;
698 }
699
Johannes Thumshirn01e0e152016-11-17 10:31:12 +0100700 elscmd = bsg_request->rqst_data.r_els.els_code;
James Smarta33c4f72013-03-01 16:36:00 -0500701 cmdsize = job->request_payload.payload_len;
James Smarta33c4f72013-03-01 16:36:00 -0500702
James Smartf1c3b0f2009-07-19 10:01:32 -0400703 if (!lpfc_nlp_get(ndlp)) {
704 rc = -ENODEV;
James Smart4cc0e562010-01-26 23:09:48 -0500705 goto free_dd_data;
James Smartf1c3b0f2009-07-19 10:01:32 -0400706 }
707
James Smarta33c4f72013-03-01 16:36:00 -0500708 /* We will use the allocated dma buffers by prep els iocb for command
709 * and response to ensure if the job times out and the request is freed,
710 * we won't be dma into memory that is no longer allocated to for the
711 * request.
712 */
James Smartf1c3b0f2009-07-19 10:01:32 -0400713
James Smart4cc0e562010-01-26 23:09:48 -0500714 cmdiocbq = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp,
James Smartf1c3b0f2009-07-19 10:01:32 -0400715 ndlp->nlp_DID, elscmd);
James Smartf1c3b0f2009-07-19 10:01:32 -0400716 if (!cmdiocbq) {
James Smart4cc0e562010-01-26 23:09:48 -0500717 rc = -EIO;
James Smarta33c4f72013-03-01 16:36:00 -0500718 goto release_ndlp;
James Smartf1c3b0f2009-07-19 10:01:32 -0400719 }
720
James Smarta33c4f72013-03-01 16:36:00 -0500721 rpi = ndlp->nlp_rpi;
James Smartf1c3b0f2009-07-19 10:01:32 -0400722
James Smarta33c4f72013-03-01 16:36:00 -0500723 /* Transfer the request payload to allocated command dma buffer */
James Smartf1c3b0f2009-07-19 10:01:32 -0400724
James Smarta33c4f72013-03-01 16:36:00 -0500725 sg_copy_to_buffer(job->request_payload.sg_list,
726 job->request_payload.sg_cnt,
727 ((struct lpfc_dmabuf *)cmdiocbq->context2)->virt,
728 cmdsize);
James Smartf1c3b0f2009-07-19 10:01:32 -0400729
James Smart3ef6d242012-01-18 16:23:48 -0500730 if (phba->sli_rev == LPFC_SLI_REV4)
731 cmdiocbq->iocb.ulpContext = phba->sli4_hba.rpi_ids[rpi];
732 else
733 cmdiocbq->iocb.ulpContext = rpi;
James Smartf1c3b0f2009-07-19 10:01:32 -0400734 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
James Smart4cc0e562010-01-26 23:09:48 -0500735 cmdiocbq->context1 = dd_data;
James Smart93d1379e2012-05-09 21:19:34 -0400736 cmdiocbq->context_un.ndlp = ndlp;
James Smarta33c4f72013-03-01 16:36:00 -0500737 cmdiocbq->iocb_cmpl = lpfc_bsg_rport_els_cmp;
James Smart4cc0e562010-01-26 23:09:48 -0500738 dd_data->type = TYPE_IOCB;
James Smarta33c4f72013-03-01 16:36:00 -0500739 dd_data->set_job = job;
James Smart4cc0e562010-01-26 23:09:48 -0500740 dd_data->context_un.iocb.cmdiocbq = cmdiocbq;
James Smart4cc0e562010-01-26 23:09:48 -0500741 dd_data->context_un.iocb.ndlp = ndlp;
James Smarta33c4f72013-03-01 16:36:00 -0500742 dd_data->context_un.iocb.rmp = NULL;
743 job->dd_data = dd_data;
James Smartf1c3b0f2009-07-19 10:01:32 -0400744
James Smart4cc0e562010-01-26 23:09:48 -0500745 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -0500746 if (lpfc_readl(phba->HCregaddr, &creg_val)) {
747 rc = -EIO;
748 goto linkdown_err;
749 }
James Smart4cc0e562010-01-26 23:09:48 -0500750 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
751 writel(creg_val, phba->HCregaddr);
752 readl(phba->HCregaddr); /* flush */
James Smartf1c3b0f2009-07-19 10:01:32 -0400753 }
James Smarta33c4f72013-03-01 16:36:00 -0500754
James Smart4cc0e562010-01-26 23:09:48 -0500755 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq, 0);
James Smarta33c4f72013-03-01 16:36:00 -0500756
James Smartb5a9b2d2013-09-06 12:19:45 -0400757 if (rc == IOCB_SUCCESS) {
758 spin_lock_irqsave(&phba->hbalock, flags);
759 /* make sure the I/O had not been completed/released */
760 if (cmdiocbq->iocb_flag & LPFC_IO_LIBDFC) {
761 /* open up abort window to timeout handler */
James Smart1b8d11a2013-09-06 12:20:51 -0400762 cmdiocbq->iocb_flag |= LPFC_IO_CMD_OUTSTANDING;
James Smartb5a9b2d2013-09-06 12:19:45 -0400763 }
764 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4cc0e562010-01-26 23:09:48 -0500765 return 0; /* done for now */
James Smartb5a9b2d2013-09-06 12:19:45 -0400766 } else if (rc == IOCB_BUSY) {
James Smartd439d282010-09-29 11:18:45 -0400767 rc = -EAGAIN;
James Smartb5a9b2d2013-09-06 12:19:45 -0400768 } else {
James Smartd439d282010-09-29 11:18:45 -0400769 rc = -EIO;
James Smartb5a9b2d2013-09-06 12:19:45 -0400770 }
771
772 /* iocb failed so cleanup */
773 job->dd_data = NULL;
James Smartf1c3b0f2009-07-19 10:01:32 -0400774
James Smart9940b972011-03-11 16:06:12 -0500775linkdown_err:
James Smarta33c4f72013-03-01 16:36:00 -0500776 cmdiocbq->context1 = ndlp;
777 lpfc_els_free_iocb(phba, cmdiocbq);
James Smartf1c3b0f2009-07-19 10:01:32 -0400778
James Smarta33c4f72013-03-01 16:36:00 -0500779release_ndlp:
780 lpfc_nlp_put(ndlp);
James Smartf1c3b0f2009-07-19 10:01:32 -0400781
James Smart4cc0e562010-01-26 23:09:48 -0500782free_dd_data:
783 kfree(dd_data);
784
785no_dd_data:
James Smartf1c3b0f2009-07-19 10:01:32 -0400786 /* make error code available to userspace */
Johannes Thumshirn01e0e152016-11-17 10:31:12 +0100787 bsg_reply->result = rc;
James Smart4cc0e562010-01-26 23:09:48 -0500788 job->dd_data = NULL;
789 return rc;
James Smartf1c3b0f2009-07-19 10:01:32 -0400790}
791
James Smart3b5dd522010-01-26 23:10:15 -0500792/**
793 * lpfc_bsg_event_free - frees an allocated event structure
794 * @kref: Pointer to a kref.
795 *
796 * Called from kref_put. Back cast the kref into an event structure address.
797 * Free any events to get, delete associated nodes, free any events to see,
798 * free any data then free the event itself.
799 **/
James Smartf1c3b0f2009-07-19 10:01:32 -0400800static void
James Smart4cc0e562010-01-26 23:09:48 -0500801lpfc_bsg_event_free(struct kref *kref)
James Smartf1c3b0f2009-07-19 10:01:32 -0400802{
James Smart4cc0e562010-01-26 23:09:48 -0500803 struct lpfc_bsg_event *evt = container_of(kref, struct lpfc_bsg_event,
804 kref);
James Smartf1c3b0f2009-07-19 10:01:32 -0400805 struct event_data *ed;
806
807 list_del(&evt->node);
808
809 while (!list_empty(&evt->events_to_get)) {
810 ed = list_entry(evt->events_to_get.next, typeof(*ed), node);
811 list_del(&ed->node);
812 kfree(ed->data);
813 kfree(ed);
814 }
815
816 while (!list_empty(&evt->events_to_see)) {
817 ed = list_entry(evt->events_to_see.next, typeof(*ed), node);
818 list_del(&ed->node);
819 kfree(ed->data);
820 kfree(ed);
821 }
822
James Smarta33c4f72013-03-01 16:36:00 -0500823 kfree(evt->dd_data);
James Smartf1c3b0f2009-07-19 10:01:32 -0400824 kfree(evt);
825}
826
James Smart3b5dd522010-01-26 23:10:15 -0500827/**
828 * lpfc_bsg_event_ref - increments the kref for an event
829 * @evt: Pointer to an event structure.
830 **/
James Smartf1c3b0f2009-07-19 10:01:32 -0400831static inline void
James Smart4cc0e562010-01-26 23:09:48 -0500832lpfc_bsg_event_ref(struct lpfc_bsg_event *evt)
James Smartf1c3b0f2009-07-19 10:01:32 -0400833{
James Smart4cc0e562010-01-26 23:09:48 -0500834 kref_get(&evt->kref);
James Smartf1c3b0f2009-07-19 10:01:32 -0400835}
836
James Smart3b5dd522010-01-26 23:10:15 -0500837/**
838 * lpfc_bsg_event_unref - Uses kref_put to free an event structure
839 * @evt: Pointer to an event structure.
840 **/
James Smartf1c3b0f2009-07-19 10:01:32 -0400841static inline void
James Smart4cc0e562010-01-26 23:09:48 -0500842lpfc_bsg_event_unref(struct lpfc_bsg_event *evt)
James Smartf1c3b0f2009-07-19 10:01:32 -0400843{
James Smart4cc0e562010-01-26 23:09:48 -0500844 kref_put(&evt->kref, lpfc_bsg_event_free);
James Smartf1c3b0f2009-07-19 10:01:32 -0400845}
846
James Smart3b5dd522010-01-26 23:10:15 -0500847/**
848 * lpfc_bsg_event_new - allocate and initialize a event structure
849 * @ev_mask: Mask of events.
850 * @ev_reg_id: Event reg id.
851 * @ev_req_id: Event request id.
852 **/
James Smart4cc0e562010-01-26 23:09:48 -0500853static struct lpfc_bsg_event *
854lpfc_bsg_event_new(uint32_t ev_mask, int ev_reg_id, uint32_t ev_req_id)
855{
856 struct lpfc_bsg_event *evt = kzalloc(sizeof(*evt), GFP_KERNEL);
James Smartf1c3b0f2009-07-19 10:01:32 -0400857
James Smart4cc0e562010-01-26 23:09:48 -0500858 if (!evt)
859 return NULL;
860
861 INIT_LIST_HEAD(&evt->events_to_get);
862 INIT_LIST_HEAD(&evt->events_to_see);
863 evt->type_mask = ev_mask;
864 evt->req_id = ev_req_id;
865 evt->reg_id = ev_reg_id;
866 evt->wait_time_stamp = jiffies;
James Smarta33c4f72013-03-01 16:36:00 -0500867 evt->dd_data = NULL;
James Smart4cc0e562010-01-26 23:09:48 -0500868 init_waitqueue_head(&evt->wq);
869 kref_init(&evt->kref);
870 return evt;
871}
872
James Smart3b5dd522010-01-26 23:10:15 -0500873/**
874 * diag_cmd_data_free - Frees an lpfc dma buffer extension
875 * @phba: Pointer to HBA context object.
876 * @mlist: Pointer to an lpfc dma buffer extension.
877 **/
James Smart4cc0e562010-01-26 23:09:48 -0500878static int
James Smart3b5dd522010-01-26 23:10:15 -0500879diag_cmd_data_free(struct lpfc_hba *phba, struct lpfc_dmabufext *mlist)
James Smart4cc0e562010-01-26 23:09:48 -0500880{
881 struct lpfc_dmabufext *mlast;
882 struct pci_dev *pcidev;
883 struct list_head head, *curr, *next;
884
885 if ((!mlist) || (!lpfc_is_link_up(phba) &&
886 (phba->link_flag & LS_LOOPBACK_MODE))) {
887 return 0;
888 }
889
890 pcidev = phba->pcidev;
891 list_add_tail(&head, &mlist->dma.list);
892
893 list_for_each_safe(curr, next, &head) {
894 mlast = list_entry(curr, struct lpfc_dmabufext , dma.list);
895 if (mlast->dma.virt)
896 dma_free_coherent(&pcidev->dev,
897 mlast->size,
898 mlast->dma.virt,
899 mlast->dma.phys);
900 kfree(mlast);
901 }
902 return 0;
903}
James Smartf1c3b0f2009-07-19 10:01:32 -0400904
905/**
906 * lpfc_bsg_ct_unsol_event - process an unsolicited CT command
907 * @phba:
908 * @pring:
909 * @piocbq:
910 *
911 * This function is called when an unsolicited CT command is received. It
James Smart4cc0e562010-01-26 23:09:48 -0500912 * forwards the event to any processes registered to receive CT events.
James Smart3b5dd522010-01-26 23:10:15 -0500913 **/
James Smart4fede782010-01-26 23:08:55 -0500914int
James Smartf1c3b0f2009-07-19 10:01:32 -0400915lpfc_bsg_ct_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
916 struct lpfc_iocbq *piocbq)
917{
918 uint32_t evt_req_id = 0;
919 uint32_t cmd;
James Smartf1c3b0f2009-07-19 10:01:32 -0400920 struct lpfc_dmabuf *dmabuf = NULL;
James Smart4cc0e562010-01-26 23:09:48 -0500921 struct lpfc_bsg_event *evt;
James Smartf1c3b0f2009-07-19 10:01:32 -0400922 struct event_data *evt_dat = NULL;
923 struct lpfc_iocbq *iocbq;
924 size_t offset = 0;
925 struct list_head head;
926 struct ulp_bde64 *bde;
927 dma_addr_t dma_addr;
928 int i;
929 struct lpfc_dmabuf *bdeBuf1 = piocbq->context2;
930 struct lpfc_dmabuf *bdeBuf2 = piocbq->context3;
931 struct lpfc_hbq_entry *hbqe;
932 struct lpfc_sli_ct_request *ct_req;
Johannes Thumshirn75cc8cf2016-11-17 10:31:19 +0100933 struct bsg_job *job = NULL;
Johannes Thumshirn01e0e152016-11-17 10:31:12 +0100934 struct fc_bsg_reply *bsg_reply;
James Smarta33c4f72013-03-01 16:36:00 -0500935 struct bsg_job_data *dd_data = NULL;
James Smart4fede782010-01-26 23:08:55 -0500936 unsigned long flags;
James Smart4cc0e562010-01-26 23:09:48 -0500937 int size = 0;
James Smartf1c3b0f2009-07-19 10:01:32 -0400938
939 INIT_LIST_HEAD(&head);
940 list_add_tail(&head, &piocbq->list);
941
942 if (piocbq->iocb.ulpBdeCount == 0 ||
943 piocbq->iocb.un.cont64[0].tus.f.bdeSize == 0)
944 goto error_ct_unsol_exit;
945
James Smart4cc0e562010-01-26 23:09:48 -0500946 if (phba->link_state == LPFC_HBA_ERROR ||
947 (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)))
948 goto error_ct_unsol_exit;
949
James Smartf1c3b0f2009-07-19 10:01:32 -0400950 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
951 dmabuf = bdeBuf1;
952 else {
953 dma_addr = getPaddr(piocbq->iocb.un.cont64[0].addrHigh,
954 piocbq->iocb.un.cont64[0].addrLow);
955 dmabuf = lpfc_sli_ringpostbuf_get(phba, pring, dma_addr);
956 }
James Smart4cc0e562010-01-26 23:09:48 -0500957 if (dmabuf == NULL)
958 goto error_ct_unsol_exit;
James Smartf1c3b0f2009-07-19 10:01:32 -0400959 ct_req = (struct lpfc_sli_ct_request *)dmabuf->virt;
960 evt_req_id = ct_req->FsType;
961 cmd = ct_req->CommandResponse.bits.CmdRsp;
James Smartf1c3b0f2009-07-19 10:01:32 -0400962 if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
963 lpfc_sli_ringpostbuf_put(phba, pring, dmabuf);
964
James Smart4fede782010-01-26 23:08:55 -0500965 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -0400966 list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
James Smart4cc0e562010-01-26 23:09:48 -0500967 if (!(evt->type_mask & FC_REG_CT_EVENT) ||
968 evt->req_id != evt_req_id)
James Smartf1c3b0f2009-07-19 10:01:32 -0400969 continue;
970
James Smart4cc0e562010-01-26 23:09:48 -0500971 lpfc_bsg_event_ref(evt);
972 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -0400973 evt_dat = kzalloc(sizeof(*evt_dat), GFP_KERNEL);
James Smart4cc0e562010-01-26 23:09:48 -0500974 if (evt_dat == NULL) {
975 spin_lock_irqsave(&phba->ct_ev_lock, flags);
976 lpfc_bsg_event_unref(evt);
James Smartf1c3b0f2009-07-19 10:01:32 -0400977 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
978 "2614 Memory allocation failed for "
979 "CT event\n");
980 break;
981 }
982
James Smartf1c3b0f2009-07-19 10:01:32 -0400983 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
984 /* take accumulated byte count from the last iocbq */
985 iocbq = list_entry(head.prev, typeof(*iocbq), list);
986 evt_dat->len = iocbq->iocb.unsli3.rcvsli3.acc_len;
987 } else {
988 list_for_each_entry(iocbq, &head, list) {
989 for (i = 0; i < iocbq->iocb.ulpBdeCount; i++)
990 evt_dat->len +=
991 iocbq->iocb.un.cont64[i].tus.f.bdeSize;
992 }
993 }
994
995 evt_dat->data = kzalloc(evt_dat->len, GFP_KERNEL);
James Smart4cc0e562010-01-26 23:09:48 -0500996 if (evt_dat->data == NULL) {
James Smartf1c3b0f2009-07-19 10:01:32 -0400997 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
998 "2615 Memory allocation failed for "
999 "CT event data, size %d\n",
1000 evt_dat->len);
1001 kfree(evt_dat);
James Smart4fede782010-01-26 23:08:55 -05001002 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smart4cc0e562010-01-26 23:09:48 -05001003 lpfc_bsg_event_unref(evt);
James Smart4fede782010-01-26 23:08:55 -05001004 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -04001005 goto error_ct_unsol_exit;
1006 }
1007
1008 list_for_each_entry(iocbq, &head, list) {
James Smart4cc0e562010-01-26 23:09:48 -05001009 size = 0;
James Smartf1c3b0f2009-07-19 10:01:32 -04001010 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
1011 bdeBuf1 = iocbq->context2;
1012 bdeBuf2 = iocbq->context3;
1013 }
1014 for (i = 0; i < iocbq->iocb.ulpBdeCount; i++) {
James Smartf1c3b0f2009-07-19 10:01:32 -04001015 if (phba->sli3_options &
1016 LPFC_SLI3_HBQ_ENABLED) {
1017 if (i == 0) {
1018 hbqe = (struct lpfc_hbq_entry *)
1019 &iocbq->iocb.un.ulpWord[0];
1020 size = hbqe->bde.tus.f.bdeSize;
1021 dmabuf = bdeBuf1;
1022 } else if (i == 1) {
1023 hbqe = (struct lpfc_hbq_entry *)
1024 &iocbq->iocb.unsli3.
1025 sli3Words[4];
1026 size = hbqe->bde.tus.f.bdeSize;
1027 dmabuf = bdeBuf2;
1028 }
1029 if ((offset + size) > evt_dat->len)
1030 size = evt_dat->len - offset;
1031 } else {
1032 size = iocbq->iocb.un.cont64[i].
1033 tus.f.bdeSize;
1034 bde = &iocbq->iocb.un.cont64[i];
1035 dma_addr = getPaddr(bde->addrHigh,
1036 bde->addrLow);
1037 dmabuf = lpfc_sli_ringpostbuf_get(phba,
1038 pring, dma_addr);
1039 }
1040 if (!dmabuf) {
1041 lpfc_printf_log(phba, KERN_ERR,
1042 LOG_LIBDFC, "2616 No dmabuf "
1043 "found for iocbq 0x%p\n",
1044 iocbq);
1045 kfree(evt_dat->data);
1046 kfree(evt_dat);
James Smart4fede782010-01-26 23:08:55 -05001047 spin_lock_irqsave(&phba->ct_ev_lock,
1048 flags);
James Smart4cc0e562010-01-26 23:09:48 -05001049 lpfc_bsg_event_unref(evt);
James Smart4fede782010-01-26 23:08:55 -05001050 spin_unlock_irqrestore(
1051 &phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -04001052 goto error_ct_unsol_exit;
1053 }
1054 memcpy((char *)(evt_dat->data) + offset,
1055 dmabuf->virt, size);
1056 offset += size;
1057 if (evt_req_id != SLI_CT_ELX_LOOPBACK &&
1058 !(phba->sli3_options &
1059 LPFC_SLI3_HBQ_ENABLED)) {
1060 lpfc_sli_ringpostbuf_put(phba, pring,
1061 dmabuf);
1062 } else {
1063 switch (cmd) {
James Smart4cc0e562010-01-26 23:09:48 -05001064 case ELX_LOOPBACK_DATA:
James Smart1b511972011-12-13 13:23:09 -05001065 if (phba->sli_rev <
1066 LPFC_SLI_REV4)
1067 diag_cmd_data_free(phba,
1068 (struct lpfc_dmabufext
1069 *)dmabuf);
James Smart4cc0e562010-01-26 23:09:48 -05001070 break;
James Smartf1c3b0f2009-07-19 10:01:32 -04001071 case ELX_LOOPBACK_XRI_SETUP:
James Smart4cc0e562010-01-26 23:09:48 -05001072 if ((phba->sli_rev ==
1073 LPFC_SLI_REV2) ||
1074 (phba->sli3_options &
1075 LPFC_SLI3_HBQ_ENABLED
1076 )) {
1077 lpfc_in_buf_free(phba,
1078 dmabuf);
1079 } else {
James Smartf1c3b0f2009-07-19 10:01:32 -04001080 lpfc_post_buffer(phba,
1081 pring,
1082 1);
James Smart4cc0e562010-01-26 23:09:48 -05001083 }
James Smartf1c3b0f2009-07-19 10:01:32 -04001084 break;
1085 default:
1086 if (!(phba->sli3_options &
1087 LPFC_SLI3_HBQ_ENABLED))
1088 lpfc_post_buffer(phba,
1089 pring,
1090 1);
1091 break;
1092 }
1093 }
1094 }
1095 }
1096
James Smart4fede782010-01-26 23:08:55 -05001097 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -04001098 if (phba->sli_rev == LPFC_SLI_REV4) {
1099 evt_dat->immed_dat = phba->ctx_idx;
James Smart6dd9e312013-01-03 15:43:37 -05001100 phba->ctx_idx = (phba->ctx_idx + 1) % LPFC_CT_CTX_MAX;
James Smart589a52d2010-07-14 15:30:54 -04001101 /* Provide warning for over-run of the ct_ctx array */
James Smart6dd9e312013-01-03 15:43:37 -05001102 if (phba->ct_ctx[evt_dat->immed_dat].valid ==
James Smart589a52d2010-07-14 15:30:54 -04001103 UNSOL_VALID)
1104 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
1105 "2717 CT context array entry "
1106 "[%d] over-run: oxid:x%x, "
1107 "sid:x%x\n", phba->ctx_idx,
1108 phba->ct_ctx[
1109 evt_dat->immed_dat].oxid,
1110 phba->ct_ctx[
1111 evt_dat->immed_dat].SID);
James Smart7851fe22011-07-22 18:36:52 -04001112 phba->ct_ctx[evt_dat->immed_dat].rxid =
1113 piocbq->iocb.ulpContext;
James Smartf1c3b0f2009-07-19 10:01:32 -04001114 phba->ct_ctx[evt_dat->immed_dat].oxid =
James Smart7851fe22011-07-22 18:36:52 -04001115 piocbq->iocb.unsli3.rcvsli3.ox_id;
James Smartf1c3b0f2009-07-19 10:01:32 -04001116 phba->ct_ctx[evt_dat->immed_dat].SID =
1117 piocbq->iocb.un.rcvels.remoteID;
James Smart6dd9e312013-01-03 15:43:37 -05001118 phba->ct_ctx[evt_dat->immed_dat].valid = UNSOL_VALID;
James Smartf1c3b0f2009-07-19 10:01:32 -04001119 } else
1120 evt_dat->immed_dat = piocbq->iocb.ulpContext;
1121
1122 evt_dat->type = FC_REG_CT_EVENT;
1123 list_add(&evt_dat->node, &evt->events_to_see);
James Smart4cc0e562010-01-26 23:09:48 -05001124 if (evt_req_id == SLI_CT_ELX_LOOPBACK) {
1125 wake_up_interruptible(&evt->wq);
1126 lpfc_bsg_event_unref(evt);
James Smartf1c3b0f2009-07-19 10:01:32 -04001127 break;
James Smart4cc0e562010-01-26 23:09:48 -05001128 }
1129
1130 list_move(evt->events_to_see.prev, &evt->events_to_get);
James Smart4cc0e562010-01-26 23:09:48 -05001131
James Smarta33c4f72013-03-01 16:36:00 -05001132 dd_data = (struct bsg_job_data *)evt->dd_data;
1133 job = dd_data->set_job;
1134 dd_data->set_job = NULL;
1135 lpfc_bsg_event_unref(evt);
James Smart4cc0e562010-01-26 23:09:48 -05001136 if (job) {
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01001137 bsg_reply = job->reply;
1138 bsg_reply->reply_payload_rcv_len = size;
James Smart4cc0e562010-01-26 23:09:48 -05001139 /* make error code available to userspace */
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01001140 bsg_reply->result = 0;
James Smart4cc0e562010-01-26 23:09:48 -05001141 job->dd_data = NULL;
1142 /* complete the job back to userspace */
1143 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
Johannes Thumshirn06548162016-11-17 10:31:22 +01001144 bsg_job_done(job, bsg_reply->result,
Johannes Thumshirn1abaede2016-11-17 10:31:13 +01001145 bsg_reply->reply_payload_rcv_len);
James Smart4cc0e562010-01-26 23:09:48 -05001146 spin_lock_irqsave(&phba->ct_ev_lock, flags);
1147 }
James Smartf1c3b0f2009-07-19 10:01:32 -04001148 }
James Smart4fede782010-01-26 23:08:55 -05001149 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -04001150
1151error_ct_unsol_exit:
1152 if (!list_empty(&head))
1153 list_del(&head);
James Smart1b511972011-12-13 13:23:09 -05001154 if ((phba->sli_rev < LPFC_SLI_REV4) &&
1155 (evt_req_id == SLI_CT_ELX_LOOPBACK))
James Smart4cc0e562010-01-26 23:09:48 -05001156 return 0;
James Smart4fede782010-01-26 23:08:55 -05001157 return 1;
James Smartf1c3b0f2009-07-19 10:01:32 -04001158}
1159
1160/**
James Smart6dd9e312013-01-03 15:43:37 -05001161 * lpfc_bsg_ct_unsol_abort - handler ct abort to management plane
1162 * @phba: Pointer to HBA context object.
1163 * @dmabuf: pointer to a dmabuf that describes the FC sequence
1164 *
1165 * This function handles abort to the CT command toward management plane
1166 * for SLI4 port.
1167 *
1168 * If the pending context of a CT command to management plane present, clears
1169 * such context and returns 1 for handled; otherwise, it returns 0 indicating
1170 * no context exists.
1171 **/
1172int
1173lpfc_bsg_ct_unsol_abort(struct lpfc_hba *phba, struct hbq_dmabuf *dmabuf)
1174{
1175 struct fc_frame_header fc_hdr;
1176 struct fc_frame_header *fc_hdr_ptr = &fc_hdr;
1177 int ctx_idx, handled = 0;
1178 uint16_t oxid, rxid;
1179 uint32_t sid;
1180
1181 memcpy(fc_hdr_ptr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
1182 sid = sli4_sid_from_fc_hdr(fc_hdr_ptr);
1183 oxid = be16_to_cpu(fc_hdr_ptr->fh_ox_id);
1184 rxid = be16_to_cpu(fc_hdr_ptr->fh_rx_id);
1185
1186 for (ctx_idx = 0; ctx_idx < LPFC_CT_CTX_MAX; ctx_idx++) {
1187 if (phba->ct_ctx[ctx_idx].valid != UNSOL_VALID)
1188 continue;
1189 if (phba->ct_ctx[ctx_idx].rxid != rxid)
1190 continue;
1191 if (phba->ct_ctx[ctx_idx].oxid != oxid)
1192 continue;
1193 if (phba->ct_ctx[ctx_idx].SID != sid)
1194 continue;
1195 phba->ct_ctx[ctx_idx].valid = UNSOL_INVALID;
1196 handled = 1;
1197 }
1198 return handled;
1199}
1200
1201/**
James Smart4cc0e562010-01-26 23:09:48 -05001202 * lpfc_bsg_hba_set_event - process a SET_EVENT bsg vendor command
James Smartf1c3b0f2009-07-19 10:01:32 -04001203 * @job: SET_EVENT fc_bsg_job
James Smart3b5dd522010-01-26 23:10:15 -05001204 **/
James Smartf1c3b0f2009-07-19 10:01:32 -04001205static int
Johannes Thumshirn75cc8cf2016-11-17 10:31:19 +01001206lpfc_bsg_hba_set_event(struct bsg_job *job)
James Smartf1c3b0f2009-07-19 10:01:32 -04001207{
Johannes Thumshirncd21c602016-11-17 10:31:14 +01001208 struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
James Smartf1c3b0f2009-07-19 10:01:32 -04001209 struct lpfc_hba *phba = vport->phba;
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01001210 struct fc_bsg_request *bsg_request = job->request;
James Smartf1c3b0f2009-07-19 10:01:32 -04001211 struct set_ct_event *event_req;
James Smart4cc0e562010-01-26 23:09:48 -05001212 struct lpfc_bsg_event *evt;
James Smartf1c3b0f2009-07-19 10:01:32 -04001213 int rc = 0;
James Smart4cc0e562010-01-26 23:09:48 -05001214 struct bsg_job_data *dd_data = NULL;
1215 uint32_t ev_mask;
1216 unsigned long flags;
James Smartf1c3b0f2009-07-19 10:01:32 -04001217
1218 if (job->request_len <
1219 sizeof(struct fc_bsg_request) + sizeof(struct set_ct_event)) {
1220 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1221 "2612 Received SET_CT_EVENT below minimum "
1222 "size\n");
James Smart4cc0e562010-01-26 23:09:48 -05001223 rc = -EINVAL;
1224 goto job_error;
1225 }
1226
James Smartf1c3b0f2009-07-19 10:01:32 -04001227 event_req = (struct set_ct_event *)
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01001228 bsg_request->rqst_data.h_vendor.vendor_cmd;
James Smart4cc0e562010-01-26 23:09:48 -05001229 ev_mask = ((uint32_t)(unsigned long)event_req->type_mask &
1230 FC_REG_EVENT_MASK);
James Smart4fede782010-01-26 23:08:55 -05001231 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -04001232 list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
1233 if (evt->reg_id == event_req->ev_reg_id) {
James Smart4cc0e562010-01-26 23:09:48 -05001234 lpfc_bsg_event_ref(evt);
James Smartf1c3b0f2009-07-19 10:01:32 -04001235 evt->wait_time_stamp = jiffies;
James Smarta33c4f72013-03-01 16:36:00 -05001236 dd_data = (struct bsg_job_data *)evt->dd_data;
James Smartf1c3b0f2009-07-19 10:01:32 -04001237 break;
1238 }
1239 }
James Smart4fede782010-01-26 23:08:55 -05001240 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -04001241
1242 if (&evt->node == &phba->ct_ev_waiters) {
1243 /* no event waiting struct yet - first call */
James Smarta33c4f72013-03-01 16:36:00 -05001244 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
1245 if (dd_data == NULL) {
1246 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1247 "2734 Failed allocation of dd_data\n");
1248 rc = -ENOMEM;
1249 goto job_error;
1250 }
James Smart4cc0e562010-01-26 23:09:48 -05001251 evt = lpfc_bsg_event_new(ev_mask, event_req->ev_reg_id,
James Smartf1c3b0f2009-07-19 10:01:32 -04001252 event_req->ev_req_id);
1253 if (!evt) {
1254 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1255 "2617 Failed allocation of event "
1256 "waiter\n");
James Smart4cc0e562010-01-26 23:09:48 -05001257 rc = -ENOMEM;
1258 goto job_error;
James Smartf1c3b0f2009-07-19 10:01:32 -04001259 }
James Smarta33c4f72013-03-01 16:36:00 -05001260 dd_data->type = TYPE_EVT;
1261 dd_data->set_job = NULL;
1262 dd_data->context_un.evt = evt;
1263 evt->dd_data = (void *)dd_data;
James Smart4fede782010-01-26 23:08:55 -05001264 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -04001265 list_add(&evt->node, &phba->ct_ev_waiters);
James Smart4cc0e562010-01-26 23:09:48 -05001266 lpfc_bsg_event_ref(evt);
1267 evt->wait_time_stamp = jiffies;
James Smart4fede782010-01-26 23:08:55 -05001268 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -04001269 }
1270
James Smart4fede782010-01-26 23:08:55 -05001271 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smart4cc0e562010-01-26 23:09:48 -05001272 evt->waiting = 1;
James Smarta33c4f72013-03-01 16:36:00 -05001273 dd_data->set_job = job; /* for unsolicited command */
James Smart4cc0e562010-01-26 23:09:48 -05001274 job->dd_data = dd_data; /* for fc transport timeout callback*/
James Smart4fede782010-01-26 23:08:55 -05001275 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smart4cc0e562010-01-26 23:09:48 -05001276 return 0; /* call job done later */
James Smartf1c3b0f2009-07-19 10:01:32 -04001277
James Smart4cc0e562010-01-26 23:09:48 -05001278job_error:
1279 if (dd_data != NULL)
1280 kfree(dd_data);
James Smartf1c3b0f2009-07-19 10:01:32 -04001281
James Smart4cc0e562010-01-26 23:09:48 -05001282 job->dd_data = NULL;
1283 return rc;
James Smartf1c3b0f2009-07-19 10:01:32 -04001284}
1285
1286/**
James Smart4cc0e562010-01-26 23:09:48 -05001287 * lpfc_bsg_hba_get_event - process a GET_EVENT bsg vendor command
James Smartf1c3b0f2009-07-19 10:01:32 -04001288 * @job: GET_EVENT fc_bsg_job
James Smart3b5dd522010-01-26 23:10:15 -05001289 **/
James Smartf1c3b0f2009-07-19 10:01:32 -04001290static int
Johannes Thumshirn75cc8cf2016-11-17 10:31:19 +01001291lpfc_bsg_hba_get_event(struct bsg_job *job)
James Smartf1c3b0f2009-07-19 10:01:32 -04001292{
Johannes Thumshirncd21c602016-11-17 10:31:14 +01001293 struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
James Smartf1c3b0f2009-07-19 10:01:32 -04001294 struct lpfc_hba *phba = vport->phba;
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01001295 struct fc_bsg_request *bsg_request = job->request;
1296 struct fc_bsg_reply *bsg_reply = job->reply;
James Smartf1c3b0f2009-07-19 10:01:32 -04001297 struct get_ct_event *event_req;
1298 struct get_ct_event_reply *event_reply;
James Smart9a803a72013-09-06 12:17:56 -04001299 struct lpfc_bsg_event *evt, *evt_next;
James Smartf1c3b0f2009-07-19 10:01:32 -04001300 struct event_data *evt_dat = NULL;
James Smart4fede782010-01-26 23:08:55 -05001301 unsigned long flags;
James Smart4cc0e562010-01-26 23:09:48 -05001302 uint32_t rc = 0;
James Smartf1c3b0f2009-07-19 10:01:32 -04001303
1304 if (job->request_len <
1305 sizeof(struct fc_bsg_request) + sizeof(struct get_ct_event)) {
1306 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1307 "2613 Received GET_CT_EVENT request below "
1308 "minimum size\n");
James Smart4cc0e562010-01-26 23:09:48 -05001309 rc = -EINVAL;
1310 goto job_error;
James Smartf1c3b0f2009-07-19 10:01:32 -04001311 }
1312
1313 event_req = (struct get_ct_event *)
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01001314 bsg_request->rqst_data.h_vendor.vendor_cmd;
James Smartf1c3b0f2009-07-19 10:01:32 -04001315
1316 event_reply = (struct get_ct_event_reply *)
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01001317 bsg_reply->reply_data.vendor_reply.vendor_rsp;
James Smart4fede782010-01-26 23:08:55 -05001318 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smart9a803a72013-09-06 12:17:56 -04001319 list_for_each_entry_safe(evt, evt_next, &phba->ct_ev_waiters, node) {
James Smartf1c3b0f2009-07-19 10:01:32 -04001320 if (evt->reg_id == event_req->ev_reg_id) {
1321 if (list_empty(&evt->events_to_get))
1322 break;
James Smart4cc0e562010-01-26 23:09:48 -05001323 lpfc_bsg_event_ref(evt);
James Smartf1c3b0f2009-07-19 10:01:32 -04001324 evt->wait_time_stamp = jiffies;
1325 evt_dat = list_entry(evt->events_to_get.prev,
1326 struct event_data, node);
1327 list_del(&evt_dat->node);
1328 break;
1329 }
1330 }
James Smart4fede782010-01-26 23:08:55 -05001331 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smartf1c3b0f2009-07-19 10:01:32 -04001332
James Smart4cc0e562010-01-26 23:09:48 -05001333 /* The app may continue to ask for event data until it gets
1334 * an error indicating that there isn't anymore
1335 */
1336 if (evt_dat == NULL) {
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01001337 bsg_reply->reply_payload_rcv_len = 0;
James Smartf1c3b0f2009-07-19 10:01:32 -04001338 rc = -ENOENT;
James Smart4cc0e562010-01-26 23:09:48 -05001339 goto job_error;
James Smartf1c3b0f2009-07-19 10:01:32 -04001340 }
1341
James Smart4cc0e562010-01-26 23:09:48 -05001342 if (evt_dat->len > job->request_payload.payload_len) {
1343 evt_dat->len = job->request_payload.payload_len;
1344 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1345 "2618 Truncated event data at %d "
1346 "bytes\n",
1347 job->request_payload.payload_len);
James Smartf1c3b0f2009-07-19 10:01:32 -04001348 }
1349
James Smart4cc0e562010-01-26 23:09:48 -05001350 event_reply->type = evt_dat->type;
James Smartf1c3b0f2009-07-19 10:01:32 -04001351 event_reply->immed_data = evt_dat->immed_dat;
James Smartf1c3b0f2009-07-19 10:01:32 -04001352 if (evt_dat->len > 0)
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01001353 bsg_reply->reply_payload_rcv_len =
James Smart4cc0e562010-01-26 23:09:48 -05001354 sg_copy_from_buffer(job->request_payload.sg_list,
1355 job->request_payload.sg_cnt,
James Smartf1c3b0f2009-07-19 10:01:32 -04001356 evt_dat->data, evt_dat->len);
1357 else
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01001358 bsg_reply->reply_payload_rcv_len = 0;
James Smartf1c3b0f2009-07-19 10:01:32 -04001359
James Smart4cc0e562010-01-26 23:09:48 -05001360 if (evt_dat) {
James Smartf1c3b0f2009-07-19 10:01:32 -04001361 kfree(evt_dat->data);
James Smart4cc0e562010-01-26 23:09:48 -05001362 kfree(evt_dat);
1363 }
1364
James Smart4fede782010-01-26 23:08:55 -05001365 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smart4cc0e562010-01-26 23:09:48 -05001366 lpfc_bsg_event_unref(evt);
James Smart4fede782010-01-26 23:08:55 -05001367 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smart4cc0e562010-01-26 23:09:48 -05001368 job->dd_data = NULL;
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01001369 bsg_reply->result = 0;
Johannes Thumshirn06548162016-11-17 10:31:22 +01001370 bsg_job_done(job, bsg_reply->result,
Johannes Thumshirn1abaede2016-11-17 10:31:13 +01001371 bsg_reply->reply_payload_rcv_len);
James Smart4cc0e562010-01-26 23:09:48 -05001372 return 0;
James Smartf1c3b0f2009-07-19 10:01:32 -04001373
James Smart4cc0e562010-01-26 23:09:48 -05001374job_error:
1375 job->dd_data = NULL;
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01001376 bsg_reply->result = rc;
James Smartf1c3b0f2009-07-19 10:01:32 -04001377 return rc;
1378}
1379
1380/**
James Smart3b5dd522010-01-26 23:10:15 -05001381 * lpfc_issue_ct_rsp_cmp - lpfc_issue_ct_rsp's completion handler
1382 * @phba: Pointer to HBA context object.
1383 * @cmdiocbq: Pointer to command iocb.
1384 * @rspiocbq: Pointer to response iocb.
1385 *
1386 * This function is the completion handler for iocbs issued using
1387 * lpfc_issue_ct_rsp_cmp function. This function is called by the
1388 * ring event handler function without any lock held. This function
1389 * can be called from both worker thread context and interrupt
1390 * context. This function also can be called from other thread which
1391 * cleans up the SLI layer objects.
1392 * This function copy the contents of the response iocb to the
1393 * response iocb memory object provided by the caller of
1394 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
1395 * sleeps for the iocb completion.
1396 **/
1397static void
1398lpfc_issue_ct_rsp_cmp(struct lpfc_hba *phba,
1399 struct lpfc_iocbq *cmdiocbq,
1400 struct lpfc_iocbq *rspiocbq)
1401{
1402 struct bsg_job_data *dd_data;
Johannes Thumshirn75cc8cf2016-11-17 10:31:19 +01001403 struct bsg_job *job;
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01001404 struct fc_bsg_reply *bsg_reply;
James Smart3b5dd522010-01-26 23:10:15 -05001405 IOCB_t *rsp;
James Smarta33c4f72013-03-01 16:36:00 -05001406 struct lpfc_dmabuf *bmp, *cmp;
James Smart3b5dd522010-01-26 23:10:15 -05001407 struct lpfc_nodelist *ndlp;
1408 unsigned long flags;
1409 int rc = 0;
1410
James Smarta33c4f72013-03-01 16:36:00 -05001411 dd_data = cmdiocbq->context1;
1412
1413 /* Determine if job has been aborted */
James Smart3b5dd522010-01-26 23:10:15 -05001414 spin_lock_irqsave(&phba->ct_ev_lock, flags);
James Smarta33c4f72013-03-01 16:36:00 -05001415 job = dd_data->set_job;
1416 if (job) {
1417 /* Prevent timeout handling from trying to abort job */
1418 job->dd_data = NULL;
1419 }
1420 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1421
James Smartb5a9b2d2013-09-06 12:19:45 -04001422 /* Close the timeout handler abort window */
1423 spin_lock_irqsave(&phba->hbalock, flags);
James Smart1b8d11a2013-09-06 12:20:51 -04001424 cmdiocbq->iocb_flag &= ~LPFC_IO_CMD_OUTSTANDING;
James Smartb5a9b2d2013-09-06 12:19:45 -04001425 spin_unlock_irqrestore(&phba->hbalock, flags);
1426
James Smarta33c4f72013-03-01 16:36:00 -05001427 ndlp = dd_data->context_un.iocb.ndlp;
1428 cmp = cmdiocbq->context2;
1429 bmp = cmdiocbq->context3;
1430 rsp = &rspiocbq->iocb;
1431
1432 /* Copy the completed job data or set the error status */
1433
1434 if (job) {
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01001435 bsg_reply = job->reply;
James Smarta33c4f72013-03-01 16:36:00 -05001436 if (rsp->ulpStatus) {
1437 if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
1438 switch (rsp->un.ulpWord[4] & IOERR_PARAM_MASK) {
1439 case IOERR_SEQUENCE_TIMEOUT:
1440 rc = -ETIMEDOUT;
1441 break;
1442 case IOERR_INVALID_RPI:
1443 rc = -EFAULT;
1444 break;
1445 default:
1446 rc = -EACCES;
1447 break;
1448 }
1449 } else {
1450 rc = -EACCES;
1451 }
1452 } else {
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01001453 bsg_reply->reply_payload_rcv_len = 0;
James Smarta33c4f72013-03-01 16:36:00 -05001454 }
James Smart3b5dd522010-01-26 23:10:15 -05001455 }
1456
James Smarta33c4f72013-03-01 16:36:00 -05001457 lpfc_free_bsg_buffers(phba, cmp);
James Smart3b5dd522010-01-26 23:10:15 -05001458 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
James Smarta33c4f72013-03-01 16:36:00 -05001459 kfree(bmp);
James Smart3b5dd522010-01-26 23:10:15 -05001460 lpfc_sli_release_iocbq(phba, cmdiocbq);
1461 lpfc_nlp_put(ndlp);
James Smart3b5dd522010-01-26 23:10:15 -05001462 kfree(dd_data);
James Smarta33c4f72013-03-01 16:36:00 -05001463
1464 /* Complete the job if the job is still active */
1465
1466 if (job) {
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01001467 bsg_reply->result = rc;
Johannes Thumshirn06548162016-11-17 10:31:22 +01001468 bsg_job_done(job, bsg_reply->result,
Johannes Thumshirn1abaede2016-11-17 10:31:13 +01001469 bsg_reply->reply_payload_rcv_len);
James Smarta33c4f72013-03-01 16:36:00 -05001470 }
James Smart3b5dd522010-01-26 23:10:15 -05001471 return;
1472}
1473
1474/**
1475 * lpfc_issue_ct_rsp - issue a ct response
1476 * @phba: Pointer to HBA context object.
1477 * @job: Pointer to the job object.
1478 * @tag: tag index value into the ports context exchange array.
1479 * @bmp: Pointer to a dma buffer descriptor.
1480 * @num_entry: Number of enties in the bde.
1481 **/
1482static int
Johannes Thumshirn75cc8cf2016-11-17 10:31:19 +01001483lpfc_issue_ct_rsp(struct lpfc_hba *phba, struct bsg_job *job, uint32_t tag,
James Smarta33c4f72013-03-01 16:36:00 -05001484 struct lpfc_dmabuf *cmp, struct lpfc_dmabuf *bmp,
1485 int num_entry)
James Smart3b5dd522010-01-26 23:10:15 -05001486{
1487 IOCB_t *icmd;
1488 struct lpfc_iocbq *ctiocb = NULL;
1489 int rc = 0;
1490 struct lpfc_nodelist *ndlp = NULL;
1491 struct bsg_job_data *dd_data;
James Smartb5a9b2d2013-09-06 12:19:45 -04001492 unsigned long flags;
James Smart3b5dd522010-01-26 23:10:15 -05001493 uint32_t creg_val;
1494
1495 /* allocate our bsg tracking structure */
1496 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
1497 if (!dd_data) {
1498 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1499 "2736 Failed allocation of dd_data\n");
1500 rc = -ENOMEM;
1501 goto no_dd_data;
1502 }
1503
1504 /* Allocate buffer for command iocb */
1505 ctiocb = lpfc_sli_get_iocbq(phba);
1506 if (!ctiocb) {
James Smartd439d282010-09-29 11:18:45 -04001507 rc = -ENOMEM;
James Smart3b5dd522010-01-26 23:10:15 -05001508 goto no_ctiocb;
1509 }
1510
1511 icmd = &ctiocb->iocb;
1512 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
1513 icmd->un.xseq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
1514 icmd->un.xseq64.bdl.addrLow = putPaddrLow(bmp->phys);
1515 icmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
1516 icmd->un.xseq64.bdl.bdeSize = (num_entry * sizeof(struct ulp_bde64));
1517 icmd->un.xseq64.w5.hcsw.Fctl = (LS | LA);
1518 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
1519 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_SOL_CTL;
1520 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
1521
1522 /* Fill in rest of iocb */
1523 icmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;
1524 icmd->ulpBdeCount = 1;
1525 icmd->ulpLe = 1;
1526 icmd->ulpClass = CLASS3;
1527 if (phba->sli_rev == LPFC_SLI_REV4) {
1528 /* Do not issue unsol response if oxid not marked as valid */
James Smart6dd9e312013-01-03 15:43:37 -05001529 if (phba->ct_ctx[tag].valid != UNSOL_VALID) {
James Smart3b5dd522010-01-26 23:10:15 -05001530 rc = IOCB_ERROR;
1531 goto issue_ct_rsp_exit;
1532 }
James Smart7851fe22011-07-22 18:36:52 -04001533 icmd->ulpContext = phba->ct_ctx[tag].rxid;
1534 icmd->unsli3.rcvsli3.ox_id = phba->ct_ctx[tag].oxid;
James Smart3b5dd522010-01-26 23:10:15 -05001535 ndlp = lpfc_findnode_did(phba->pport, phba->ct_ctx[tag].SID);
1536 if (!ndlp) {
1537 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
1538 "2721 ndlp null for oxid %x SID %x\n",
1539 icmd->ulpContext,
1540 phba->ct_ctx[tag].SID);
1541 rc = IOCB_ERROR;
1542 goto issue_ct_rsp_exit;
1543 }
James Smart589a52d2010-07-14 15:30:54 -04001544
1545 /* Check if the ndlp is active */
1546 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smarta33c4f72013-03-01 16:36:00 -05001547 rc = IOCB_ERROR;
James Smart589a52d2010-07-14 15:30:54 -04001548 goto issue_ct_rsp_exit;
1549 }
1550
1551 /* get a refernece count so the ndlp doesn't go away while
1552 * we respond
1553 */
1554 if (!lpfc_nlp_get(ndlp)) {
James Smarta33c4f72013-03-01 16:36:00 -05001555 rc = IOCB_ERROR;
James Smart589a52d2010-07-14 15:30:54 -04001556 goto issue_ct_rsp_exit;
1557 }
1558
James Smart7851fe22011-07-22 18:36:52 -04001559 icmd->un.ulpWord[3] =
James Smart6d368e52011-05-24 11:44:12 -04001560 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
1561
James Smart3b5dd522010-01-26 23:10:15 -05001562 /* The exchange is done, mark the entry as invalid */
James Smart6dd9e312013-01-03 15:43:37 -05001563 phba->ct_ctx[tag].valid = UNSOL_INVALID;
James Smart3b5dd522010-01-26 23:10:15 -05001564 } else
1565 icmd->ulpContext = (ushort) tag;
1566
1567 icmd->ulpTimeout = phba->fc_ratov * 2;
1568
1569 /* Xmit CT response on exchange <xid> */
1570 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart7851fe22011-07-22 18:36:52 -04001571 "2722 Xmit CT response on exchange x%x Data: x%x x%x x%x\n",
1572 icmd->ulpContext, icmd->ulpIoTag, tag, phba->link_state);
James Smart3b5dd522010-01-26 23:10:15 -05001573
1574 ctiocb->iocb_cmpl = NULL;
1575 ctiocb->iocb_flag |= LPFC_IO_LIBDFC;
1576 ctiocb->vport = phba->pport;
James Smarta33c4f72013-03-01 16:36:00 -05001577 ctiocb->context1 = dd_data;
1578 ctiocb->context2 = cmp;
James Smart3b5dd522010-01-26 23:10:15 -05001579 ctiocb->context3 = bmp;
James Smartd5ce53b2013-04-17 20:17:26 -04001580 ctiocb->context_un.ndlp = ndlp;
James Smart3b5dd522010-01-26 23:10:15 -05001581 ctiocb->iocb_cmpl = lpfc_issue_ct_rsp_cmp;
James Smarta33c4f72013-03-01 16:36:00 -05001582
James Smart3b5dd522010-01-26 23:10:15 -05001583 dd_data->type = TYPE_IOCB;
James Smarta33c4f72013-03-01 16:36:00 -05001584 dd_data->set_job = job;
James Smart3b5dd522010-01-26 23:10:15 -05001585 dd_data->context_un.iocb.cmdiocbq = ctiocb;
James Smart3b5dd522010-01-26 23:10:15 -05001586 dd_data->context_un.iocb.ndlp = ndlp;
James Smarta33c4f72013-03-01 16:36:00 -05001587 dd_data->context_un.iocb.rmp = NULL;
1588 job->dd_data = dd_data;
James Smart3b5dd522010-01-26 23:10:15 -05001589
1590 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -05001591 if (lpfc_readl(phba->HCregaddr, &creg_val)) {
1592 rc = -IOCB_ERROR;
1593 goto issue_ct_rsp_exit;
1594 }
James Smart3b5dd522010-01-26 23:10:15 -05001595 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
1596 writel(creg_val, phba->HCregaddr);
1597 readl(phba->HCregaddr); /* flush */
1598 }
1599
1600 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
1601
James Smartb5a9b2d2013-09-06 12:19:45 -04001602 if (rc == IOCB_SUCCESS) {
1603 spin_lock_irqsave(&phba->hbalock, flags);
1604 /* make sure the I/O had not been completed/released */
1605 if (ctiocb->iocb_flag & LPFC_IO_LIBDFC) {
1606 /* open up abort window to timeout handler */
James Smart1b8d11a2013-09-06 12:20:51 -04001607 ctiocb->iocb_flag |= LPFC_IO_CMD_OUTSTANDING;
James Smartb5a9b2d2013-09-06 12:19:45 -04001608 }
1609 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart3b5dd522010-01-26 23:10:15 -05001610 return 0; /* done for now */
James Smartb5a9b2d2013-09-06 12:19:45 -04001611 }
1612
1613 /* iocb failed so cleanup */
1614 job->dd_data = NULL;
James Smart3b5dd522010-01-26 23:10:15 -05001615
1616issue_ct_rsp_exit:
1617 lpfc_sli_release_iocbq(phba, ctiocb);
1618no_ctiocb:
1619 kfree(dd_data);
1620no_dd_data:
1621 return rc;
1622}
1623
1624/**
1625 * lpfc_bsg_send_mgmt_rsp - process a SEND_MGMT_RESP bsg vendor command
1626 * @job: SEND_MGMT_RESP fc_bsg_job
1627 **/
1628static int
Johannes Thumshirn75cc8cf2016-11-17 10:31:19 +01001629lpfc_bsg_send_mgmt_rsp(struct bsg_job *job)
James Smart3b5dd522010-01-26 23:10:15 -05001630{
Johannes Thumshirncd21c602016-11-17 10:31:14 +01001631 struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
James Smart3b5dd522010-01-26 23:10:15 -05001632 struct lpfc_hba *phba = vport->phba;
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01001633 struct fc_bsg_request *bsg_request = job->request;
1634 struct fc_bsg_reply *bsg_reply = job->reply;
James Smart3b5dd522010-01-26 23:10:15 -05001635 struct send_mgmt_resp *mgmt_resp = (struct send_mgmt_resp *)
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01001636 bsg_request->rqst_data.h_vendor.vendor_cmd;
James Smart3b5dd522010-01-26 23:10:15 -05001637 struct ulp_bde64 *bpl;
James Smarta33c4f72013-03-01 16:36:00 -05001638 struct lpfc_dmabuf *bmp = NULL, *cmp = NULL;
1639 int bpl_entries;
James Smart3b5dd522010-01-26 23:10:15 -05001640 uint32_t tag = mgmt_resp->tag;
1641 unsigned long reqbfrcnt =
1642 (unsigned long)job->request_payload.payload_len;
1643 int rc = 0;
1644
1645 /* in case no data is transferred */
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01001646 bsg_reply->reply_payload_rcv_len = 0;
James Smart3b5dd522010-01-26 23:10:15 -05001647
1648 if (!reqbfrcnt || (reqbfrcnt > (80 * BUF_SZ_4K))) {
1649 rc = -ERANGE;
1650 goto send_mgmt_rsp_exit;
1651 }
1652
1653 bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
1654 if (!bmp) {
1655 rc = -ENOMEM;
1656 goto send_mgmt_rsp_exit;
1657 }
1658
1659 bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
1660 if (!bmp->virt) {
1661 rc = -ENOMEM;
1662 goto send_mgmt_rsp_free_bmp;
1663 }
1664
1665 INIT_LIST_HEAD(&bmp->list);
1666 bpl = (struct ulp_bde64 *) bmp->virt;
James Smarta33c4f72013-03-01 16:36:00 -05001667 bpl_entries = (LPFC_BPL_SIZE/sizeof(struct ulp_bde64));
1668 cmp = lpfc_alloc_bsg_buffers(phba, job->request_payload.payload_len,
1669 1, bpl, &bpl_entries);
1670 if (!cmp) {
1671 rc = -ENOMEM;
1672 goto send_mgmt_rsp_free_bmp;
James Smart3b5dd522010-01-26 23:10:15 -05001673 }
James Smarta33c4f72013-03-01 16:36:00 -05001674 lpfc_bsg_copy_data(cmp, &job->request_payload,
1675 job->request_payload.payload_len, 1);
James Smart3b5dd522010-01-26 23:10:15 -05001676
James Smarta33c4f72013-03-01 16:36:00 -05001677 rc = lpfc_issue_ct_rsp(phba, job, tag, cmp, bmp, bpl_entries);
James Smart3b5dd522010-01-26 23:10:15 -05001678
1679 if (rc == IOCB_SUCCESS)
1680 return 0; /* done for now */
1681
James Smart3b5dd522010-01-26 23:10:15 -05001682 rc = -EACCES;
James Smarta33c4f72013-03-01 16:36:00 -05001683
1684 lpfc_free_bsg_buffers(phba, cmp);
James Smart3b5dd522010-01-26 23:10:15 -05001685
1686send_mgmt_rsp_free_bmp:
James Smarta33c4f72013-03-01 16:36:00 -05001687 if (bmp->virt)
1688 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
James Smart3b5dd522010-01-26 23:10:15 -05001689 kfree(bmp);
1690send_mgmt_rsp_exit:
1691 /* make error code available to userspace */
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01001692 bsg_reply->result = rc;
James Smart3b5dd522010-01-26 23:10:15 -05001693 job->dd_data = NULL;
1694 return rc;
1695}
1696
1697/**
James Smart7ad20aa2011-05-24 11:44:28 -04001698 * lpfc_bsg_diag_mode_enter - process preparing into device diag loopback mode
1699 * @phba: Pointer to HBA context object.
James Smart3b5dd522010-01-26 23:10:15 -05001700 *
James Smart7ad20aa2011-05-24 11:44:28 -04001701 * This function is responsible for preparing driver for diag loopback
1702 * on device.
1703 */
1704static int
James Smart88a2cfb2011-07-22 18:36:33 -04001705lpfc_bsg_diag_mode_enter(struct lpfc_hba *phba)
James Smart7ad20aa2011-05-24 11:44:28 -04001706{
1707 struct lpfc_vport **vports;
1708 struct Scsi_Host *shost;
1709 struct lpfc_sli *psli;
James Smart895427b2017-02-12 13:52:30 -08001710 struct lpfc_queue *qp = NULL;
James Smart7ad20aa2011-05-24 11:44:28 -04001711 struct lpfc_sli_ring *pring;
1712 int i = 0;
1713
1714 psli = &phba->sli;
1715 if (!psli)
1716 return -ENODEV;
1717
James Smart7ad20aa2011-05-24 11:44:28 -04001718
1719 if ((phba->link_state == LPFC_HBA_ERROR) ||
1720 (psli->sli_flag & LPFC_BLOCK_MGMT_IO) ||
1721 (!(psli->sli_flag & LPFC_SLI_ACTIVE)))
1722 return -EACCES;
1723
1724 vports = lpfc_create_vport_work_array(phba);
1725 if (vports) {
1726 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
1727 shost = lpfc_shost_from_vport(vports[i]);
1728 scsi_block_requests(shost);
1729 }
1730 lpfc_destroy_vport_work_array(phba, vports);
1731 } else {
1732 shost = lpfc_shost_from_vport(phba->pport);
1733 scsi_block_requests(shost);
1734 }
1735
James Smart895427b2017-02-12 13:52:30 -08001736 if (phba->sli_rev != LPFC_SLI_REV4) {
1737 pring = &psli->sli3_ring[LPFC_FCP_RING];
1738 lpfc_emptyq_wait(phba, &pring->txcmplq, &phba->hbalock);
1739 return 0;
1740 }
1741 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
1742 pring = qp->pring;
1743 if (!pring || (pring->ringno != LPFC_FCP_RING))
1744 continue;
1745 if (!lpfc_emptyq_wait(phba, &pring->txcmplq,
1746 &pring->ring_lock))
James Smart7ad20aa2011-05-24 11:44:28 -04001747 break;
James Smart7ad20aa2011-05-24 11:44:28 -04001748 }
1749 return 0;
1750}
1751
1752/**
1753 * lpfc_bsg_diag_mode_exit - exit process from device diag loopback mode
1754 * @phba: Pointer to HBA context object.
James Smart7ad20aa2011-05-24 11:44:28 -04001755 *
1756 * This function is responsible for driver exit processing of setting up
1757 * diag loopback mode on device.
1758 */
1759static void
1760lpfc_bsg_diag_mode_exit(struct lpfc_hba *phba)
1761{
1762 struct Scsi_Host *shost;
1763 struct lpfc_vport **vports;
1764 int i;
1765
1766 vports = lpfc_create_vport_work_array(phba);
1767 if (vports) {
1768 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
1769 shost = lpfc_shost_from_vport(vports[i]);
1770 scsi_unblock_requests(shost);
1771 }
1772 lpfc_destroy_vport_work_array(phba, vports);
1773 } else {
1774 shost = lpfc_shost_from_vport(phba->pport);
1775 scsi_unblock_requests(shost);
1776 }
1777 return;
1778}
1779
1780/**
1781 * lpfc_sli3_bsg_diag_loopback_mode - process an sli3 bsg vendor command
1782 * @phba: Pointer to HBA context object.
1783 * @job: LPFC_BSG_VENDOR_DIAG_MODE
1784 *
1785 * This function is responsible for placing an sli3 port into diagnostic
1786 * loopback mode in order to perform a diagnostic loopback test.
James Smart3b5dd522010-01-26 23:10:15 -05001787 * All new scsi requests are blocked, a small delay is used to allow the
1788 * scsi requests to complete then the link is brought down. If the link is
1789 * is placed in loopback mode then scsi requests are again allowed
1790 * so the scsi mid-layer doesn't give up on the port.
1791 * All of this is done in-line.
1792 */
1793static int
Johannes Thumshirn75cc8cf2016-11-17 10:31:19 +01001794lpfc_sli3_bsg_diag_loopback_mode(struct lpfc_hba *phba, struct bsg_job *job)
James Smart3b5dd522010-01-26 23:10:15 -05001795{
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01001796 struct fc_bsg_request *bsg_request = job->request;
1797 struct fc_bsg_reply *bsg_reply = job->reply;
James Smart3b5dd522010-01-26 23:10:15 -05001798 struct diag_mode_set *loopback_mode;
James Smart3b5dd522010-01-26 23:10:15 -05001799 uint32_t link_flags;
1800 uint32_t timeout;
James Smart1b511972011-12-13 13:23:09 -05001801 LPFC_MBOXQ_t *pmboxq = NULL;
James Smartb76f2dc2011-07-22 18:37:42 -04001802 int mbxstatus = MBX_SUCCESS;
James Smart3b5dd522010-01-26 23:10:15 -05001803 int i = 0;
1804 int rc = 0;
1805
1806 /* no data to return just the return code */
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01001807 bsg_reply->reply_payload_rcv_len = 0;
James Smart3b5dd522010-01-26 23:10:15 -05001808
James Smart7ad20aa2011-05-24 11:44:28 -04001809 if (job->request_len < sizeof(struct fc_bsg_request) +
1810 sizeof(struct diag_mode_set)) {
James Smart3b5dd522010-01-26 23:10:15 -05001811 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
James Smart7ad20aa2011-05-24 11:44:28 -04001812 "2738 Received DIAG MODE request size:%d "
1813 "below the minimum size:%d\n",
1814 job->request_len,
1815 (int)(sizeof(struct fc_bsg_request) +
1816 sizeof(struct diag_mode_set)));
James Smart3b5dd522010-01-26 23:10:15 -05001817 rc = -EINVAL;
1818 goto job_error;
1819 }
1820
James Smart88a2cfb2011-07-22 18:36:33 -04001821 rc = lpfc_bsg_diag_mode_enter(phba);
James Smart7ad20aa2011-05-24 11:44:28 -04001822 if (rc)
1823 goto job_error;
1824
1825 /* bring the link to diagnostic mode */
James Smart3b5dd522010-01-26 23:10:15 -05001826 loopback_mode = (struct diag_mode_set *)
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01001827 bsg_request->rqst_data.h_vendor.vendor_cmd;
James Smart3b5dd522010-01-26 23:10:15 -05001828 link_flags = loopback_mode->type;
James Smart515e0aa2010-09-29 11:19:00 -04001829 timeout = loopback_mode->timeout * 100;
James Smart3b5dd522010-01-26 23:10:15 -05001830
James Smart3b5dd522010-01-26 23:10:15 -05001831 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1832 if (!pmboxq) {
1833 rc = -ENOMEM;
James Smart7ad20aa2011-05-24 11:44:28 -04001834 goto loopback_mode_exit;
James Smart3b5dd522010-01-26 23:10:15 -05001835 }
James Smart3b5dd522010-01-26 23:10:15 -05001836 memset((void *)pmboxq, 0, sizeof(LPFC_MBOXQ_t));
1837 pmboxq->u.mb.mbxCommand = MBX_DOWN_LINK;
1838 pmboxq->u.mb.mbxOwner = OWN_HOST;
1839
1840 mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
1841
1842 if ((mbxstatus == MBX_SUCCESS) && (pmboxq->u.mb.mbxStatus == 0)) {
1843 /* wait for link down before proceeding */
1844 i = 0;
1845 while (phba->link_state != LPFC_LINK_DOWN) {
1846 if (i++ > timeout) {
1847 rc = -ETIMEDOUT;
1848 goto loopback_mode_exit;
1849 }
James Smart3b5dd522010-01-26 23:10:15 -05001850 msleep(10);
1851 }
1852
1853 memset((void *)pmboxq, 0, sizeof(LPFC_MBOXQ_t));
1854 if (link_flags == INTERNAL_LOOP_BACK)
1855 pmboxq->u.mb.un.varInitLnk.link_flags = FLAGS_LOCAL_LB;
1856 else
1857 pmboxq->u.mb.un.varInitLnk.link_flags =
1858 FLAGS_TOPOLOGY_MODE_LOOP;
1859
1860 pmboxq->u.mb.mbxCommand = MBX_INIT_LINK;
1861 pmboxq->u.mb.mbxOwner = OWN_HOST;
1862
1863 mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq,
1864 LPFC_MBOX_TMO);
1865
1866 if ((mbxstatus != MBX_SUCCESS) || (pmboxq->u.mb.mbxStatus))
1867 rc = -ENODEV;
1868 else {
James Smart1b511972011-12-13 13:23:09 -05001869 spin_lock_irq(&phba->hbalock);
James Smart3b5dd522010-01-26 23:10:15 -05001870 phba->link_flag |= LS_LOOPBACK_MODE;
James Smart1b511972011-12-13 13:23:09 -05001871 spin_unlock_irq(&phba->hbalock);
James Smart3b5dd522010-01-26 23:10:15 -05001872 /* wait for the link attention interrupt */
1873 msleep(100);
1874
1875 i = 0;
1876 while (phba->link_state != LPFC_HBA_READY) {
1877 if (i++ > timeout) {
1878 rc = -ETIMEDOUT;
1879 break;
1880 }
1881
1882 msleep(10);
1883 }
1884 }
1885
1886 } else
1887 rc = -ENODEV;
1888
1889loopback_mode_exit:
James Smart7ad20aa2011-05-24 11:44:28 -04001890 lpfc_bsg_diag_mode_exit(phba);
James Smart3b5dd522010-01-26 23:10:15 -05001891
1892 /*
1893 * Let SLI layer release mboxq if mbox command completed after timeout.
1894 */
James Smart1b511972011-12-13 13:23:09 -05001895 if (pmboxq && mbxstatus != MBX_TIMEOUT)
James Smart3b5dd522010-01-26 23:10:15 -05001896 mempool_free(pmboxq, phba->mbox_mem_pool);
1897
1898job_error:
1899 /* make error code available to userspace */
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01001900 bsg_reply->result = rc;
James Smart3b5dd522010-01-26 23:10:15 -05001901 /* complete the job back to userspace if no error */
1902 if (rc == 0)
Johannes Thumshirn06548162016-11-17 10:31:22 +01001903 bsg_job_done(job, bsg_reply->result,
Johannes Thumshirn1abaede2016-11-17 10:31:13 +01001904 bsg_reply->reply_payload_rcv_len);
James Smart3b5dd522010-01-26 23:10:15 -05001905 return rc;
1906}
1907
1908/**
James Smart7ad20aa2011-05-24 11:44:28 -04001909 * lpfc_sli4_bsg_set_link_diag_state - set sli4 link diag state
1910 * @phba: Pointer to HBA context object.
1911 * @diag: Flag for set link to diag or nomral operation state.
1912 *
1913 * This function is responsible for issuing a sli4 mailbox command for setting
1914 * link to either diag state or normal operation state.
1915 */
1916static int
1917lpfc_sli4_bsg_set_link_diag_state(struct lpfc_hba *phba, uint32_t diag)
1918{
1919 LPFC_MBOXQ_t *pmboxq;
1920 struct lpfc_mbx_set_link_diag_state *link_diag_state;
1921 uint32_t req_len, alloc_len;
1922 int mbxstatus = MBX_SUCCESS, rc;
1923
1924 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1925 if (!pmboxq)
1926 return -ENOMEM;
1927
1928 req_len = (sizeof(struct lpfc_mbx_set_link_diag_state) -
1929 sizeof(struct lpfc_sli4_cfg_mhdr));
1930 alloc_len = lpfc_sli4_config(phba, pmboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
1931 LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_STATE,
1932 req_len, LPFC_SLI4_MBX_EMBED);
1933 if (alloc_len != req_len) {
1934 rc = -ENOMEM;
1935 goto link_diag_state_set_out;
1936 }
James Smart1b511972011-12-13 13:23:09 -05001937 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
1938 "3128 Set link to diagnostic state:x%x (x%x/x%x)\n",
1939 diag, phba->sli4_hba.lnk_info.lnk_tp,
1940 phba->sli4_hba.lnk_info.lnk_no);
1941
James Smart7ad20aa2011-05-24 11:44:28 -04001942 link_diag_state = &pmboxq->u.mqe.un.link_diag_state;
James Smart97315922012-08-03 12:32:52 -04001943 bf_set(lpfc_mbx_set_diag_state_diag_bit_valid, &link_diag_state->u.req,
1944 LPFC_DIAG_STATE_DIAG_BIT_VALID_CHANGE);
James Smart7ad20aa2011-05-24 11:44:28 -04001945 bf_set(lpfc_mbx_set_diag_state_link_num, &link_diag_state->u.req,
James Smart1b511972011-12-13 13:23:09 -05001946 phba->sli4_hba.lnk_info.lnk_no);
James Smart7ad20aa2011-05-24 11:44:28 -04001947 bf_set(lpfc_mbx_set_diag_state_link_type, &link_diag_state->u.req,
James Smart1b511972011-12-13 13:23:09 -05001948 phba->sli4_hba.lnk_info.lnk_tp);
James Smart7ad20aa2011-05-24 11:44:28 -04001949 if (diag)
1950 bf_set(lpfc_mbx_set_diag_state_diag,
1951 &link_diag_state->u.req, 1);
1952 else
1953 bf_set(lpfc_mbx_set_diag_state_diag,
1954 &link_diag_state->u.req, 0);
1955
1956 mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
1957
1958 if ((mbxstatus == MBX_SUCCESS) && (pmboxq->u.mb.mbxStatus == 0))
1959 rc = 0;
1960 else
1961 rc = -ENODEV;
1962
1963link_diag_state_set_out:
1964 if (pmboxq && (mbxstatus != MBX_TIMEOUT))
1965 mempool_free(pmboxq, phba->mbox_mem_pool);
1966
1967 return rc;
1968}
1969
1970/**
James Smart9a66d992019-03-12 16:30:27 -07001971 * lpfc_sli4_bsg_set_loopback_mode - set sli4 internal loopback diagnostic
James Smart1b511972011-12-13 13:23:09 -05001972 * @phba: Pointer to HBA context object.
James Smart9a66d992019-03-12 16:30:27 -07001973 * @mode: loopback mode to set
1974 * @link_no: link number for loopback mode to set
James Smart1b511972011-12-13 13:23:09 -05001975 *
1976 * This function is responsible for issuing a sli4 mailbox command for setting
James Smart9a66d992019-03-12 16:30:27 -07001977 * up loopback diagnostic for a link.
James Smart1b511972011-12-13 13:23:09 -05001978 */
1979static int
James Smart9a66d992019-03-12 16:30:27 -07001980lpfc_sli4_bsg_set_loopback_mode(struct lpfc_hba *phba, int mode,
1981 uint32_t link_no)
James Smart1b511972011-12-13 13:23:09 -05001982{
1983 LPFC_MBOXQ_t *pmboxq;
1984 uint32_t req_len, alloc_len;
1985 struct lpfc_mbx_set_link_diag_loopback *link_diag_loopback;
1986 int mbxstatus = MBX_SUCCESS, rc = 0;
1987
1988 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1989 if (!pmboxq)
1990 return -ENOMEM;
1991 req_len = (sizeof(struct lpfc_mbx_set_link_diag_loopback) -
1992 sizeof(struct lpfc_sli4_cfg_mhdr));
1993 alloc_len = lpfc_sli4_config(phba, pmboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
1994 LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_LOOPBACK,
1995 req_len, LPFC_SLI4_MBX_EMBED);
1996 if (alloc_len != req_len) {
1997 mempool_free(pmboxq, phba->mbox_mem_pool);
1998 return -ENOMEM;
1999 }
2000 link_diag_loopback = &pmboxq->u.mqe.un.link_diag_loopback;
2001 bf_set(lpfc_mbx_set_diag_state_link_num,
James Smart9a66d992019-03-12 16:30:27 -07002002 &link_diag_loopback->u.req, link_no);
2003
2004 if (phba->sli4_hba.conf_trunk & (1 << link_no)) {
2005 bf_set(lpfc_mbx_set_diag_state_link_type,
2006 &link_diag_loopback->u.req, LPFC_LNK_FC_TRUNKED);
2007 } else {
2008 bf_set(lpfc_mbx_set_diag_state_link_type,
2009 &link_diag_loopback->u.req,
2010 phba->sli4_hba.lnk_info.lnk_tp);
2011 }
2012
James Smart1b511972011-12-13 13:23:09 -05002013 bf_set(lpfc_mbx_set_diag_lpbk_type, &link_diag_loopback->u.req,
James Smart9a66d992019-03-12 16:30:27 -07002014 mode);
James Smart1b511972011-12-13 13:23:09 -05002015
2016 mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
2017 if ((mbxstatus != MBX_SUCCESS) || (pmboxq->u.mb.mbxStatus)) {
2018 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2019 "3127 Failed setup loopback mode mailbox "
2020 "command, rc:x%x, status:x%x\n", mbxstatus,
2021 pmboxq->u.mb.mbxStatus);
2022 rc = -ENODEV;
2023 }
2024 if (pmboxq && (mbxstatus != MBX_TIMEOUT))
2025 mempool_free(pmboxq, phba->mbox_mem_pool);
2026 return rc;
2027}
2028
2029/**
2030 * lpfc_sli4_diag_fcport_reg_setup - setup port registrations for diagnostic
2031 * @phba: Pointer to HBA context object.
2032 *
2033 * This function set up SLI4 FC port registrations for diagnostic run, which
2034 * includes all the rpis, vfi, and also vpi.
2035 */
2036static int
2037lpfc_sli4_diag_fcport_reg_setup(struct lpfc_hba *phba)
2038{
2039 int rc;
2040
2041 if (phba->pport->fc_flag & FC_VFI_REGISTERED) {
2042 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2043 "3136 Port still had vfi registered: "
2044 "mydid:x%x, fcfi:%d, vfi:%d, vpi:%d\n",
2045 phba->pport->fc_myDID, phba->fcf.fcfi,
2046 phba->sli4_hba.vfi_ids[phba->pport->vfi],
2047 phba->vpi_ids[phba->pport->vpi]);
2048 return -EINVAL;
2049 }
2050 rc = lpfc_issue_reg_vfi(phba->pport);
2051 return rc;
2052}
2053
2054/**
James Smart7ad20aa2011-05-24 11:44:28 -04002055 * lpfc_sli4_bsg_diag_loopback_mode - process an sli4 bsg vendor command
2056 * @phba: Pointer to HBA context object.
2057 * @job: LPFC_BSG_VENDOR_DIAG_MODE
2058 *
2059 * This function is responsible for placing an sli4 port into diagnostic
2060 * loopback mode in order to perform a diagnostic loopback test.
2061 */
2062static int
Johannes Thumshirn75cc8cf2016-11-17 10:31:19 +01002063lpfc_sli4_bsg_diag_loopback_mode(struct lpfc_hba *phba, struct bsg_job *job)
James Smart7ad20aa2011-05-24 11:44:28 -04002064{
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01002065 struct fc_bsg_request *bsg_request = job->request;
2066 struct fc_bsg_reply *bsg_reply = job->reply;
James Smart7ad20aa2011-05-24 11:44:28 -04002067 struct diag_mode_set *loopback_mode;
James Smart9a66d992019-03-12 16:30:27 -07002068 uint32_t link_flags, timeout, link_no;
James Smart1b511972011-12-13 13:23:09 -05002069 int i, rc = 0;
James Smart7ad20aa2011-05-24 11:44:28 -04002070
2071 /* no data to return just the return code */
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01002072 bsg_reply->reply_payload_rcv_len = 0;
James Smart7ad20aa2011-05-24 11:44:28 -04002073
2074 if (job->request_len < sizeof(struct fc_bsg_request) +
2075 sizeof(struct diag_mode_set)) {
2076 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2077 "3011 Received DIAG MODE request size:%d "
2078 "below the minimum size:%d\n",
2079 job->request_len,
2080 (int)(sizeof(struct fc_bsg_request) +
2081 sizeof(struct diag_mode_set)));
2082 rc = -EINVAL;
James Smart9a66d992019-03-12 16:30:27 -07002083 goto job_done;
2084 }
2085
2086 loopback_mode = (struct diag_mode_set *)
2087 bsg_request->rqst_data.h_vendor.vendor_cmd;
2088 link_flags = loopback_mode->type;
2089 timeout = loopback_mode->timeout * 100;
2090
2091 if (loopback_mode->physical_link == -1)
2092 link_no = phba->sli4_hba.lnk_info.lnk_no;
2093 else
2094 link_no = loopback_mode->physical_link;
2095
2096 if (link_flags == DISABLE_LOOP_BACK) {
2097 rc = lpfc_sli4_bsg_set_loopback_mode(phba,
2098 LPFC_DIAG_LOOPBACK_TYPE_DISABLE,
2099 link_no);
2100 if (!rc) {
2101 /* Unset the need disable bit */
2102 phba->sli4_hba.conf_trunk &= ~((1 << link_no) << 4);
2103 }
2104 goto job_done;
2105 } else {
2106 /* Check if we need to disable the loopback state */
2107 if (phba->sli4_hba.conf_trunk & ((1 << link_no) << 4)) {
2108 rc = -EPERM;
2109 goto job_done;
2110 }
James Smart7ad20aa2011-05-24 11:44:28 -04002111 }
2112
James Smart88a2cfb2011-07-22 18:36:33 -04002113 rc = lpfc_bsg_diag_mode_enter(phba);
James Smart7ad20aa2011-05-24 11:44:28 -04002114 if (rc)
James Smart9a66d992019-03-12 16:30:27 -07002115 goto job_done;
James Smart7ad20aa2011-05-24 11:44:28 -04002116
James Smart1b511972011-12-13 13:23:09 -05002117 /* indicate we are in loobpack diagnostic mode */
2118 spin_lock_irq(&phba->hbalock);
2119 phba->link_flag |= LS_LOOPBACK_MODE;
2120 spin_unlock_irq(&phba->hbalock);
2121
2122 /* reset port to start frome scratch */
2123 rc = lpfc_selective_reset(phba);
2124 if (rc)
James Smart9a66d992019-03-12 16:30:27 -07002125 goto job_done;
James Smart1b511972011-12-13 13:23:09 -05002126
James Smart7ad20aa2011-05-24 11:44:28 -04002127 /* bring the link to diagnostic mode */
James Smart1b511972011-12-13 13:23:09 -05002128 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
2129 "3129 Bring link to diagnostic state.\n");
James Smart7ad20aa2011-05-24 11:44:28 -04002130
2131 rc = lpfc_sli4_bsg_set_link_diag_state(phba, 1);
James Smart1b511972011-12-13 13:23:09 -05002132 if (rc) {
2133 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2134 "3130 Failed to bring link to diagnostic "
2135 "state, rc:x%x\n", rc);
James Smart7ad20aa2011-05-24 11:44:28 -04002136 goto loopback_mode_exit;
James Smart1b511972011-12-13 13:23:09 -05002137 }
James Smart7ad20aa2011-05-24 11:44:28 -04002138
2139 /* wait for link down before proceeding */
2140 i = 0;
2141 while (phba->link_state != LPFC_LINK_DOWN) {
2142 if (i++ > timeout) {
2143 rc = -ETIMEDOUT;
James Smart1b511972011-12-13 13:23:09 -05002144 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
2145 "3131 Timeout waiting for link to "
2146 "diagnostic mode, timeout:%d ms\n",
2147 timeout * 10);
James Smart7ad20aa2011-05-24 11:44:28 -04002148 goto loopback_mode_exit;
2149 }
2150 msleep(10);
2151 }
James Smart7ad20aa2011-05-24 11:44:28 -04002152
James Smart1b511972011-12-13 13:23:09 -05002153 /* set up loopback mode */
2154 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
2155 "3132 Set up loopback mode:x%x\n", link_flags);
2156
James Smart9a66d992019-03-12 16:30:27 -07002157 switch (link_flags) {
2158 case INTERNAL_LOOP_BACK:
2159 if (phba->sli4_hba.conf_trunk & (1 << link_no)) {
2160 rc = lpfc_sli4_bsg_set_loopback_mode(phba,
2161 LPFC_DIAG_LOOPBACK_TYPE_INTERNAL,
2162 link_no);
2163 } else {
2164 /* Trunk is configured, but link is not in this trunk */
2165 if (phba->sli4_hba.conf_trunk) {
2166 rc = -ELNRNG;
2167 goto loopback_mode_exit;
2168 }
2169
2170 rc = lpfc_sli4_bsg_set_loopback_mode(phba,
2171 LPFC_DIAG_LOOPBACK_TYPE_INTERNAL,
2172 link_no);
2173 }
2174
2175 if (!rc) {
2176 /* Set the need disable bit */
2177 phba->sli4_hba.conf_trunk |= (1 << link_no) << 4;
2178 }
2179
2180 break;
2181 case EXTERNAL_LOOP_BACK:
2182 if (phba->sli4_hba.conf_trunk & (1 << link_no)) {
2183 rc = lpfc_sli4_bsg_set_loopback_mode(phba,
2184 LPFC_DIAG_LOOPBACK_TYPE_EXTERNAL_TRUNKED,
2185 link_no);
2186 } else {
2187 /* Trunk is configured, but link is not in this trunk */
2188 if (phba->sli4_hba.conf_trunk) {
2189 rc = -ELNRNG;
2190 goto loopback_mode_exit;
2191 }
2192
2193 rc = lpfc_sli4_bsg_set_loopback_mode(phba,
2194 LPFC_DIAG_LOOPBACK_TYPE_SERDES,
2195 link_no);
2196 }
2197
2198 if (!rc) {
2199 /* Set the need disable bit */
2200 phba->sli4_hba.conf_trunk |= (1 << link_no) << 4;
2201 }
2202
2203 break;
2204 default:
James Smart1b511972011-12-13 13:23:09 -05002205 rc = -EINVAL;
2206 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
2207 "3141 Loopback mode:x%x not supported\n",
2208 link_flags);
2209 goto loopback_mode_exit;
2210 }
2211
2212 if (!rc) {
James Smart7ad20aa2011-05-24 11:44:28 -04002213 /* wait for the link attention interrupt */
2214 msleep(100);
2215 i = 0;
James Smart1b511972011-12-13 13:23:09 -05002216 while (phba->link_state < LPFC_LINK_UP) {
2217 if (i++ > timeout) {
2218 rc = -ETIMEDOUT;
2219 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
2220 "3137 Timeout waiting for link up "
2221 "in loopback mode, timeout:%d ms\n",
2222 timeout * 10);
2223 break;
2224 }
2225 msleep(10);
2226 }
2227 }
2228
2229 /* port resource registration setup for loopback diagnostic */
2230 if (!rc) {
2231 /* set up a none zero myDID for loopback test */
2232 phba->pport->fc_myDID = 1;
2233 rc = lpfc_sli4_diag_fcport_reg_setup(phba);
2234 } else
2235 goto loopback_mode_exit;
2236
2237 if (!rc) {
2238 /* wait for the port ready */
2239 msleep(100);
2240 i = 0;
James Smart7ad20aa2011-05-24 11:44:28 -04002241 while (phba->link_state != LPFC_HBA_READY) {
2242 if (i++ > timeout) {
2243 rc = -ETIMEDOUT;
James Smart1b511972011-12-13 13:23:09 -05002244 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
2245 "3133 Timeout waiting for port "
2246 "loopback mode ready, timeout:%d ms\n",
2247 timeout * 10);
James Smart7ad20aa2011-05-24 11:44:28 -04002248 break;
2249 }
2250 msleep(10);
2251 }
2252 }
2253
2254loopback_mode_exit:
James Smart1b511972011-12-13 13:23:09 -05002255 /* clear loopback diagnostic mode */
2256 if (rc) {
2257 spin_lock_irq(&phba->hbalock);
2258 phba->link_flag &= ~LS_LOOPBACK_MODE;
2259 spin_unlock_irq(&phba->hbalock);
2260 }
James Smart7ad20aa2011-05-24 11:44:28 -04002261 lpfc_bsg_diag_mode_exit(phba);
2262
James Smart9a66d992019-03-12 16:30:27 -07002263job_done:
James Smart7ad20aa2011-05-24 11:44:28 -04002264 /* make error code available to userspace */
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01002265 bsg_reply->result = rc;
James Smart7ad20aa2011-05-24 11:44:28 -04002266 /* complete the job back to userspace if no error */
2267 if (rc == 0)
Johannes Thumshirn06548162016-11-17 10:31:22 +01002268 bsg_job_done(job, bsg_reply->result,
Johannes Thumshirn1abaede2016-11-17 10:31:13 +01002269 bsg_reply->reply_payload_rcv_len);
James Smart7ad20aa2011-05-24 11:44:28 -04002270 return rc;
2271}
2272
2273/**
2274 * lpfc_bsg_diag_loopback_mode - bsg vendor command for diag loopback mode
2275 * @job: LPFC_BSG_VENDOR_DIAG_MODE
2276 *
2277 * This function is responsible for responding to check and dispatch bsg diag
2278 * command from the user to proper driver action routines.
2279 */
2280static int
Johannes Thumshirn75cc8cf2016-11-17 10:31:19 +01002281lpfc_bsg_diag_loopback_mode(struct bsg_job *job)
James Smart7ad20aa2011-05-24 11:44:28 -04002282{
2283 struct Scsi_Host *shost;
2284 struct lpfc_vport *vport;
2285 struct lpfc_hba *phba;
2286 int rc;
2287
Johannes Thumshirncd21c602016-11-17 10:31:14 +01002288 shost = fc_bsg_to_shost(job);
James Smart7ad20aa2011-05-24 11:44:28 -04002289 if (!shost)
2290 return -ENODEV;
Johannes Thumshirncd21c602016-11-17 10:31:14 +01002291 vport = shost_priv(shost);
James Smart7ad20aa2011-05-24 11:44:28 -04002292 if (!vport)
2293 return -ENODEV;
2294 phba = vport->phba;
2295 if (!phba)
2296 return -ENODEV;
2297
2298 if (phba->sli_rev < LPFC_SLI_REV4)
2299 rc = lpfc_sli3_bsg_diag_loopback_mode(phba, job);
James Smart719162b2018-12-10 19:37:01 -08002300 else if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) >=
James Smart7ad20aa2011-05-24 11:44:28 -04002301 LPFC_SLI_INTF_IF_TYPE_2)
2302 rc = lpfc_sli4_bsg_diag_loopback_mode(phba, job);
2303 else
2304 rc = -ENODEV;
2305
2306 return rc;
James Smart7ad20aa2011-05-24 11:44:28 -04002307}
2308
2309/**
2310 * lpfc_sli4_bsg_diag_mode_end - sli4 bsg vendor command for ending diag mode
2311 * @job: LPFC_BSG_VENDOR_DIAG_MODE_END
2312 *
2313 * This function is responsible for responding to check and dispatch bsg diag
2314 * command from the user to proper driver action routines.
2315 */
2316static int
Johannes Thumshirn75cc8cf2016-11-17 10:31:19 +01002317lpfc_sli4_bsg_diag_mode_end(struct bsg_job *job)
James Smart7ad20aa2011-05-24 11:44:28 -04002318{
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01002319 struct fc_bsg_request *bsg_request = job->request;
2320 struct fc_bsg_reply *bsg_reply = job->reply;
James Smart7ad20aa2011-05-24 11:44:28 -04002321 struct Scsi_Host *shost;
2322 struct lpfc_vport *vport;
2323 struct lpfc_hba *phba;
James Smart1b511972011-12-13 13:23:09 -05002324 struct diag_mode_set *loopback_mode_end_cmd;
2325 uint32_t timeout;
2326 int rc, i;
James Smart7ad20aa2011-05-24 11:44:28 -04002327
Johannes Thumshirncd21c602016-11-17 10:31:14 +01002328 shost = fc_bsg_to_shost(job);
James Smart7ad20aa2011-05-24 11:44:28 -04002329 if (!shost)
2330 return -ENODEV;
Johannes Thumshirncd21c602016-11-17 10:31:14 +01002331 vport = shost_priv(shost);
James Smart7ad20aa2011-05-24 11:44:28 -04002332 if (!vport)
2333 return -ENODEV;
2334 phba = vport->phba;
2335 if (!phba)
2336 return -ENODEV;
2337
2338 if (phba->sli_rev < LPFC_SLI_REV4)
2339 return -ENODEV;
James Smart719162b2018-12-10 19:37:01 -08002340 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
James Smart7ad20aa2011-05-24 11:44:28 -04002341 LPFC_SLI_INTF_IF_TYPE_2)
2342 return -ENODEV;
2343
James Smart1b511972011-12-13 13:23:09 -05002344 /* clear loopback diagnostic mode */
2345 spin_lock_irq(&phba->hbalock);
2346 phba->link_flag &= ~LS_LOOPBACK_MODE;
2347 spin_unlock_irq(&phba->hbalock);
2348 loopback_mode_end_cmd = (struct diag_mode_set *)
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01002349 bsg_request->rqst_data.h_vendor.vendor_cmd;
James Smart1b511972011-12-13 13:23:09 -05002350 timeout = loopback_mode_end_cmd->timeout * 100;
2351
James Smart7ad20aa2011-05-24 11:44:28 -04002352 rc = lpfc_sli4_bsg_set_link_diag_state(phba, 0);
James Smart1b511972011-12-13 13:23:09 -05002353 if (rc) {
2354 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2355 "3139 Failed to bring link to diagnostic "
2356 "state, rc:x%x\n", rc);
2357 goto loopback_mode_end_exit;
2358 }
James Smart7ad20aa2011-05-24 11:44:28 -04002359
James Smart1b511972011-12-13 13:23:09 -05002360 /* wait for link down before proceeding */
2361 i = 0;
2362 while (phba->link_state != LPFC_LINK_DOWN) {
2363 if (i++ > timeout) {
James Smart1b511972011-12-13 13:23:09 -05002364 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
2365 "3140 Timeout waiting for link to "
2366 "diagnostic mode_end, timeout:%d ms\n",
2367 timeout * 10);
2368 /* there is nothing much we can do here */
2369 break;
2370 }
2371 msleep(10);
2372 }
James Smart7ad20aa2011-05-24 11:44:28 -04002373
James Smart1b511972011-12-13 13:23:09 -05002374 /* reset port resource registrations */
2375 rc = lpfc_selective_reset(phba);
2376 phba->pport->fc_myDID = 0;
2377
2378loopback_mode_end_exit:
2379 /* make return code available to userspace */
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01002380 bsg_reply->result = rc;
James Smart1b511972011-12-13 13:23:09 -05002381 /* complete the job back to userspace if no error */
2382 if (rc == 0)
Johannes Thumshirn06548162016-11-17 10:31:22 +01002383 bsg_job_done(job, bsg_reply->result,
Johannes Thumshirn1abaede2016-11-17 10:31:13 +01002384 bsg_reply->reply_payload_rcv_len);
James Smart7ad20aa2011-05-24 11:44:28 -04002385 return rc;
2386}
2387
2388/**
2389 * lpfc_sli4_bsg_link_diag_test - sli4 bsg vendor command for diag link test
2390 * @job: LPFC_BSG_VENDOR_DIAG_LINK_TEST
2391 *
2392 * This function is to perform SLI4 diag link test request from the user
2393 * applicaiton.
2394 */
2395static int
Johannes Thumshirn75cc8cf2016-11-17 10:31:19 +01002396lpfc_sli4_bsg_link_diag_test(struct bsg_job *job)
James Smart7ad20aa2011-05-24 11:44:28 -04002397{
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01002398 struct fc_bsg_request *bsg_request = job->request;
2399 struct fc_bsg_reply *bsg_reply = job->reply;
James Smart7ad20aa2011-05-24 11:44:28 -04002400 struct Scsi_Host *shost;
2401 struct lpfc_vport *vport;
2402 struct lpfc_hba *phba;
2403 LPFC_MBOXQ_t *pmboxq;
2404 struct sli4_link_diag *link_diag_test_cmd;
2405 uint32_t req_len, alloc_len;
James Smart7ad20aa2011-05-24 11:44:28 -04002406 struct lpfc_mbx_run_link_diag_test *run_link_diag_test;
2407 union lpfc_sli4_cfg_shdr *shdr;
2408 uint32_t shdr_status, shdr_add_status;
2409 struct diag_status *diag_status_reply;
2410 int mbxstatus, rc = 0;
2411
Johannes Thumshirncd21c602016-11-17 10:31:14 +01002412 shost = fc_bsg_to_shost(job);
James Smart7ad20aa2011-05-24 11:44:28 -04002413 if (!shost) {
2414 rc = -ENODEV;
2415 goto job_error;
2416 }
Johannes Thumshirncd21c602016-11-17 10:31:14 +01002417 vport = shost_priv(shost);
James Smart7ad20aa2011-05-24 11:44:28 -04002418 if (!vport) {
2419 rc = -ENODEV;
2420 goto job_error;
2421 }
2422 phba = vport->phba;
2423 if (!phba) {
2424 rc = -ENODEV;
2425 goto job_error;
2426 }
2427
2428 if (phba->sli_rev < LPFC_SLI_REV4) {
2429 rc = -ENODEV;
2430 goto job_error;
2431 }
James Smart719162b2018-12-10 19:37:01 -08002432 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
James Smart7ad20aa2011-05-24 11:44:28 -04002433 LPFC_SLI_INTF_IF_TYPE_2) {
2434 rc = -ENODEV;
2435 goto job_error;
2436 }
2437
2438 if (job->request_len < sizeof(struct fc_bsg_request) +
2439 sizeof(struct sli4_link_diag)) {
2440 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2441 "3013 Received LINK DIAG TEST request "
2442 " size:%d below the minimum size:%d\n",
2443 job->request_len,
2444 (int)(sizeof(struct fc_bsg_request) +
2445 sizeof(struct sli4_link_diag)));
2446 rc = -EINVAL;
2447 goto job_error;
2448 }
2449
James Smart88a2cfb2011-07-22 18:36:33 -04002450 rc = lpfc_bsg_diag_mode_enter(phba);
James Smart7ad20aa2011-05-24 11:44:28 -04002451 if (rc)
2452 goto job_error;
2453
2454 link_diag_test_cmd = (struct sli4_link_diag *)
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01002455 bsg_request->rqst_data.h_vendor.vendor_cmd;
James Smart7ad20aa2011-05-24 11:44:28 -04002456
2457 rc = lpfc_sli4_bsg_set_link_diag_state(phba, 1);
2458
2459 if (rc)
2460 goto job_error;
2461
2462 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
Gustavo A. R. Silva44ed33e2017-08-22 15:53:53 -05002463 if (!pmboxq)
James Smart7ad20aa2011-05-24 11:44:28 -04002464 goto link_diag_test_exit;
James Smart7ad20aa2011-05-24 11:44:28 -04002465
2466 req_len = (sizeof(struct lpfc_mbx_set_link_diag_state) -
2467 sizeof(struct lpfc_sli4_cfg_mhdr));
2468 alloc_len = lpfc_sli4_config(phba, pmboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
2469 LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_STATE,
2470 req_len, LPFC_SLI4_MBX_EMBED);
Gustavo A. R. Silva44ed33e2017-08-22 15:53:53 -05002471 if (alloc_len != req_len)
James Smart7ad20aa2011-05-24 11:44:28 -04002472 goto link_diag_test_exit;
Gustavo A. R. Silva44ed33e2017-08-22 15:53:53 -05002473
James Smart7ad20aa2011-05-24 11:44:28 -04002474 run_link_diag_test = &pmboxq->u.mqe.un.link_diag_test;
2475 bf_set(lpfc_mbx_run_diag_test_link_num, &run_link_diag_test->u.req,
James Smart1b511972011-12-13 13:23:09 -05002476 phba->sli4_hba.lnk_info.lnk_no);
James Smart7ad20aa2011-05-24 11:44:28 -04002477 bf_set(lpfc_mbx_run_diag_test_link_type, &run_link_diag_test->u.req,
James Smart1b511972011-12-13 13:23:09 -05002478 phba->sli4_hba.lnk_info.lnk_tp);
James Smart7ad20aa2011-05-24 11:44:28 -04002479 bf_set(lpfc_mbx_run_diag_test_test_id, &run_link_diag_test->u.req,
2480 link_diag_test_cmd->test_id);
2481 bf_set(lpfc_mbx_run_diag_test_loops, &run_link_diag_test->u.req,
2482 link_diag_test_cmd->loops);
2483 bf_set(lpfc_mbx_run_diag_test_test_ver, &run_link_diag_test->u.req,
2484 link_diag_test_cmd->test_version);
2485 bf_set(lpfc_mbx_run_diag_test_err_act, &run_link_diag_test->u.req,
2486 link_diag_test_cmd->error_action);
2487
2488 mbxstatus = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
2489
2490 shdr = (union lpfc_sli4_cfg_shdr *)
2491 &pmboxq->u.mqe.un.sli4_config.header.cfg_shdr;
2492 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
2493 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
2494 if (shdr_status || shdr_add_status || mbxstatus) {
2495 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
2496 "3010 Run link diag test mailbox failed with "
2497 "mbx_status x%x status x%x, add_status x%x\n",
2498 mbxstatus, shdr_status, shdr_add_status);
2499 }
2500
2501 diag_status_reply = (struct diag_status *)
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01002502 bsg_reply->reply_data.vendor_reply.vendor_rsp;
James Smart7ad20aa2011-05-24 11:44:28 -04002503
2504 if (job->reply_len <
2505 sizeof(struct fc_bsg_request) + sizeof(struct diag_status)) {
2506 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2507 "3012 Received Run link diag test reply "
2508 "below minimum size (%d): reply_len:%d\n",
2509 (int)(sizeof(struct fc_bsg_request) +
2510 sizeof(struct diag_status)),
2511 job->reply_len);
2512 rc = -EINVAL;
2513 goto job_error;
2514 }
2515
2516 diag_status_reply->mbox_status = mbxstatus;
2517 diag_status_reply->shdr_status = shdr_status;
2518 diag_status_reply->shdr_add_status = shdr_add_status;
2519
2520link_diag_test_exit:
2521 rc = lpfc_sli4_bsg_set_link_diag_state(phba, 0);
2522
2523 if (pmboxq)
2524 mempool_free(pmboxq, phba->mbox_mem_pool);
2525
2526 lpfc_bsg_diag_mode_exit(phba);
2527
2528job_error:
2529 /* make error code available to userspace */
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01002530 bsg_reply->result = rc;
James Smart7ad20aa2011-05-24 11:44:28 -04002531 /* complete the job back to userspace if no error */
2532 if (rc == 0)
Johannes Thumshirn06548162016-11-17 10:31:22 +01002533 bsg_job_done(job, bsg_reply->result,
Johannes Thumshirn1abaede2016-11-17 10:31:13 +01002534 bsg_reply->reply_payload_rcv_len);
James Smart7ad20aa2011-05-24 11:44:28 -04002535 return rc;
2536}
2537
2538/**
James Smart3b5dd522010-01-26 23:10:15 -05002539 * lpfcdiag_loop_self_reg - obtains a remote port login id
2540 * @phba: Pointer to HBA context object
2541 * @rpi: Pointer to a remote port login id
2542 *
2543 * This function obtains a remote port login id so the diag loopback test
2544 * can send and receive its own unsolicited CT command.
2545 **/
James Smart40426292010-12-15 17:58:10 -05002546static int lpfcdiag_loop_self_reg(struct lpfc_hba *phba, uint16_t *rpi)
James Smart3b5dd522010-01-26 23:10:15 -05002547{
2548 LPFC_MBOXQ_t *mbox;
2549 struct lpfc_dmabuf *dmabuff;
2550 int status;
2551
2552 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2553 if (!mbox)
James Smartd439d282010-09-29 11:18:45 -04002554 return -ENOMEM;
James Smart3b5dd522010-01-26 23:10:15 -05002555
James Smart1b511972011-12-13 13:23:09 -05002556 if (phba->sli_rev < LPFC_SLI_REV4)
2557 status = lpfc_reg_rpi(phba, 0, phba->pport->fc_myDID,
2558 (uint8_t *)&phba->pport->fc_sparam,
2559 mbox, *rpi);
2560 else {
James Smart40426292010-12-15 17:58:10 -05002561 *rpi = lpfc_sli4_alloc_rpi(phba);
James Smart9d3d3402017-04-21 16:05:00 -07002562 if (*rpi == LPFC_RPI_ALLOC_ERROR) {
2563 mempool_free(mbox, phba->mbox_mem_pool);
2564 return -EBUSY;
2565 }
James Smart1b511972011-12-13 13:23:09 -05002566 status = lpfc_reg_rpi(phba, phba->pport->vpi,
2567 phba->pport->fc_myDID,
2568 (uint8_t *)&phba->pport->fc_sparam,
2569 mbox, *rpi);
2570 }
2571
James Smart3b5dd522010-01-26 23:10:15 -05002572 if (status) {
2573 mempool_free(mbox, phba->mbox_mem_pool);
James Smart40426292010-12-15 17:58:10 -05002574 if (phba->sli_rev == LPFC_SLI_REV4)
2575 lpfc_sli4_free_rpi(phba, *rpi);
James Smartd439d282010-09-29 11:18:45 -04002576 return -ENOMEM;
James Smart3b5dd522010-01-26 23:10:15 -05002577 }
2578
James Smart3e1f0712018-11-29 16:09:29 -08002579 dmabuff = (struct lpfc_dmabuf *)mbox->ctx_buf;
2580 mbox->ctx_buf = NULL;
2581 mbox->ctx_ndlp = NULL;
James Smart3b5dd522010-01-26 23:10:15 -05002582 status = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
2583
2584 if ((status != MBX_SUCCESS) || (mbox->u.mb.mbxStatus)) {
2585 lpfc_mbuf_free(phba, dmabuff->virt, dmabuff->phys);
2586 kfree(dmabuff);
2587 if (status != MBX_TIMEOUT)
2588 mempool_free(mbox, phba->mbox_mem_pool);
James Smart40426292010-12-15 17:58:10 -05002589 if (phba->sli_rev == LPFC_SLI_REV4)
2590 lpfc_sli4_free_rpi(phba, *rpi);
James Smartd439d282010-09-29 11:18:45 -04002591 return -ENODEV;
James Smart3b5dd522010-01-26 23:10:15 -05002592 }
2593
James Smart1b511972011-12-13 13:23:09 -05002594 if (phba->sli_rev < LPFC_SLI_REV4)
2595 *rpi = mbox->u.mb.un.varWords[0];
James Smart3b5dd522010-01-26 23:10:15 -05002596
2597 lpfc_mbuf_free(phba, dmabuff->virt, dmabuff->phys);
2598 kfree(dmabuff);
2599 mempool_free(mbox, phba->mbox_mem_pool);
2600 return 0;
2601}
2602
2603/**
2604 * lpfcdiag_loop_self_unreg - unregs from the rpi
2605 * @phba: Pointer to HBA context object
2606 * @rpi: Remote port login id
2607 *
2608 * This function unregisters the rpi obtained in lpfcdiag_loop_self_reg
2609 **/
2610static int lpfcdiag_loop_self_unreg(struct lpfc_hba *phba, uint16_t rpi)
2611{
2612 LPFC_MBOXQ_t *mbox;
2613 int status;
2614
2615 /* Allocate mboxq structure */
2616 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2617 if (mbox == NULL)
James Smartd439d282010-09-29 11:18:45 -04002618 return -ENOMEM;
James Smart3b5dd522010-01-26 23:10:15 -05002619
James Smart1b511972011-12-13 13:23:09 -05002620 if (phba->sli_rev < LPFC_SLI_REV4)
2621 lpfc_unreg_login(phba, 0, rpi, mbox);
2622 else
2623 lpfc_unreg_login(phba, phba->pport->vpi,
2624 phba->sli4_hba.rpi_ids[rpi], mbox);
2625
James Smart3b5dd522010-01-26 23:10:15 -05002626 status = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
2627
2628 if ((status != MBX_SUCCESS) || (mbox->u.mb.mbxStatus)) {
2629 if (status != MBX_TIMEOUT)
2630 mempool_free(mbox, phba->mbox_mem_pool);
James Smartd439d282010-09-29 11:18:45 -04002631 return -EIO;
James Smart3b5dd522010-01-26 23:10:15 -05002632 }
James Smart3b5dd522010-01-26 23:10:15 -05002633 mempool_free(mbox, phba->mbox_mem_pool);
James Smart40426292010-12-15 17:58:10 -05002634 if (phba->sli_rev == LPFC_SLI_REV4)
2635 lpfc_sli4_free_rpi(phba, rpi);
James Smart3b5dd522010-01-26 23:10:15 -05002636 return 0;
2637}
2638
2639/**
2640 * lpfcdiag_loop_get_xri - obtains the transmit and receive ids
2641 * @phba: Pointer to HBA context object
2642 * @rpi: Remote port login id
2643 * @txxri: Pointer to transmit exchange id
2644 * @rxxri: Pointer to response exchabge id
2645 *
2646 * This function obtains the transmit and receive ids required to send
2647 * an unsolicited ct command with a payload. A special lpfc FsType and CmdRsp
2648 * flags are used to the unsolicted response handler is able to process
2649 * the ct command sent on the same port.
2650 **/
2651static int lpfcdiag_loop_get_xri(struct lpfc_hba *phba, uint16_t rpi,
2652 uint16_t *txxri, uint16_t * rxxri)
2653{
2654 struct lpfc_bsg_event *evt;
2655 struct lpfc_iocbq *cmdiocbq, *rspiocbq;
2656 IOCB_t *cmd, *rsp;
2657 struct lpfc_dmabuf *dmabuf;
2658 struct ulp_bde64 *bpl = NULL;
2659 struct lpfc_sli_ct_request *ctreq = NULL;
2660 int ret_val = 0;
James Smartd439d282010-09-29 11:18:45 -04002661 int time_left;
James Smart5a0916b2013-07-15 18:31:42 -04002662 int iocb_stat = IOCB_SUCCESS;
James Smart3b5dd522010-01-26 23:10:15 -05002663 unsigned long flags;
2664
2665 *txxri = 0;
2666 *rxxri = 0;
2667 evt = lpfc_bsg_event_new(FC_REG_CT_EVENT, current->pid,
2668 SLI_CT_ELX_LOOPBACK);
2669 if (!evt)
James Smartd439d282010-09-29 11:18:45 -04002670 return -ENOMEM;
James Smart3b5dd522010-01-26 23:10:15 -05002671
2672 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2673 list_add(&evt->node, &phba->ct_ev_waiters);
2674 lpfc_bsg_event_ref(evt);
2675 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2676
2677 cmdiocbq = lpfc_sli_get_iocbq(phba);
2678 rspiocbq = lpfc_sli_get_iocbq(phba);
2679
2680 dmabuf = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2681 if (dmabuf) {
2682 dmabuf->virt = lpfc_mbuf_alloc(phba, 0, &dmabuf->phys);
James Smartc7495932010-04-06 15:05:28 -04002683 if (dmabuf->virt) {
2684 INIT_LIST_HEAD(&dmabuf->list);
2685 bpl = (struct ulp_bde64 *) dmabuf->virt;
2686 memset(bpl, 0, sizeof(*bpl));
2687 ctreq = (struct lpfc_sli_ct_request *)(bpl + 1);
2688 bpl->addrHigh =
2689 le32_to_cpu(putPaddrHigh(dmabuf->phys +
2690 sizeof(*bpl)));
2691 bpl->addrLow =
2692 le32_to_cpu(putPaddrLow(dmabuf->phys +
2693 sizeof(*bpl)));
2694 bpl->tus.f.bdeFlags = 0;
2695 bpl->tus.f.bdeSize = ELX_LOOPBACK_HEADER_SZ;
2696 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2697 }
James Smart3b5dd522010-01-26 23:10:15 -05002698 }
2699
2700 if (cmdiocbq == NULL || rspiocbq == NULL ||
James Smartc7495932010-04-06 15:05:28 -04002701 dmabuf == NULL || bpl == NULL || ctreq == NULL ||
2702 dmabuf->virt == NULL) {
James Smartd439d282010-09-29 11:18:45 -04002703 ret_val = -ENOMEM;
James Smart3b5dd522010-01-26 23:10:15 -05002704 goto err_get_xri_exit;
2705 }
2706
2707 cmd = &cmdiocbq->iocb;
2708 rsp = &rspiocbq->iocb;
2709
2710 memset(ctreq, 0, ELX_LOOPBACK_HEADER_SZ);
2711
2712 ctreq->RevisionId.bits.Revision = SLI_CT_REVISION;
2713 ctreq->RevisionId.bits.InId = 0;
2714 ctreq->FsType = SLI_CT_ELX_LOOPBACK;
2715 ctreq->FsSubType = 0;
2716 ctreq->CommandResponse.bits.CmdRsp = ELX_LOOPBACK_XRI_SETUP;
2717 ctreq->CommandResponse.bits.Size = 0;
2718
2719
2720 cmd->un.xseq64.bdl.addrHigh = putPaddrHigh(dmabuf->phys);
2721 cmd->un.xseq64.bdl.addrLow = putPaddrLow(dmabuf->phys);
2722 cmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
2723 cmd->un.xseq64.bdl.bdeSize = sizeof(*bpl);
2724
2725 cmd->un.xseq64.w5.hcsw.Fctl = LA;
2726 cmd->un.xseq64.w5.hcsw.Dfctl = 0;
2727 cmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
2728 cmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
2729
2730 cmd->ulpCommand = CMD_XMIT_SEQUENCE64_CR;
2731 cmd->ulpBdeCount = 1;
2732 cmd->ulpLe = 1;
2733 cmd->ulpClass = CLASS3;
2734 cmd->ulpContext = rpi;
2735
2736 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
2737 cmdiocbq->vport = phba->pport;
James Smart5a0916b2013-07-15 18:31:42 -04002738 cmdiocbq->iocb_cmpl = NULL;
James Smart3b5dd522010-01-26 23:10:15 -05002739
James Smartd439d282010-09-29 11:18:45 -04002740 iocb_stat = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq,
James Smart3b5dd522010-01-26 23:10:15 -05002741 rspiocbq,
2742 (phba->fc_ratov * 2)
2743 + LPFC_DRVR_TIMEOUT);
James Smart53151bb2013-10-10 12:24:07 -04002744 if ((iocb_stat != IOCB_SUCCESS) || (rsp->ulpStatus != IOSTAT_SUCCESS)) {
James Smartd439d282010-09-29 11:18:45 -04002745 ret_val = -EIO;
James Smart3b5dd522010-01-26 23:10:15 -05002746 goto err_get_xri_exit;
James Smartd439d282010-09-29 11:18:45 -04002747 }
James Smart3b5dd522010-01-26 23:10:15 -05002748 *txxri = rsp->ulpContext;
2749
2750 evt->waiting = 1;
2751 evt->wait_time_stamp = jiffies;
James Smartd439d282010-09-29 11:18:45 -04002752 time_left = wait_event_interruptible_timeout(
James Smart3b5dd522010-01-26 23:10:15 -05002753 evt->wq, !list_empty(&evt->events_to_see),
James Smart256ec0d2013-04-17 20:14:58 -04002754 msecs_to_jiffies(1000 *
2755 ((phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT)));
James Smart3b5dd522010-01-26 23:10:15 -05002756 if (list_empty(&evt->events_to_see))
James Smartd439d282010-09-29 11:18:45 -04002757 ret_val = (time_left) ? -EINTR : -ETIMEDOUT;
James Smart3b5dd522010-01-26 23:10:15 -05002758 else {
James Smart3b5dd522010-01-26 23:10:15 -05002759 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2760 list_move(evt->events_to_see.prev, &evt->events_to_get);
2761 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2762 *rxxri = (list_entry(evt->events_to_get.prev,
2763 typeof(struct event_data),
2764 node))->immed_dat;
2765 }
2766 evt->waiting = 0;
2767
2768err_get_xri_exit:
2769 spin_lock_irqsave(&phba->ct_ev_lock, flags);
2770 lpfc_bsg_event_unref(evt); /* release ref */
2771 lpfc_bsg_event_unref(evt); /* delete */
2772 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2773
2774 if (dmabuf) {
2775 if (dmabuf->virt)
2776 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
2777 kfree(dmabuf);
2778 }
2779
James Smartd439d282010-09-29 11:18:45 -04002780 if (cmdiocbq && (iocb_stat != IOCB_TIMEDOUT))
James Smart3b5dd522010-01-26 23:10:15 -05002781 lpfc_sli_release_iocbq(phba, cmdiocbq);
2782 if (rspiocbq)
2783 lpfc_sli_release_iocbq(phba, rspiocbq);
2784 return ret_val;
2785}
2786
2787/**
James Smart7ad20aa2011-05-24 11:44:28 -04002788 * lpfc_bsg_dma_page_alloc - allocate a bsg mbox page sized dma buffers
2789 * @phba: Pointer to HBA context object
2790 *
James Smart2ea259e2017-02-12 13:52:27 -08002791 * This function allocates BSG_MBOX_SIZE (4KB) page size dma buffer and
Joe Perches9e03aa22013-09-03 13:45:58 -07002792 * returns the pointer to the buffer.
James Smart7ad20aa2011-05-24 11:44:28 -04002793 **/
2794static struct lpfc_dmabuf *
2795lpfc_bsg_dma_page_alloc(struct lpfc_hba *phba)
2796{
2797 struct lpfc_dmabuf *dmabuf;
2798 struct pci_dev *pcidev = phba->pcidev;
2799
2800 /* allocate dma buffer struct */
2801 dmabuf = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2802 if (!dmabuf)
2803 return NULL;
2804
2805 INIT_LIST_HEAD(&dmabuf->list);
2806
2807 /* now, allocate dma buffer */
Luis Chamberlain750afb02019-01-04 09:23:09 +01002808 dmabuf->virt = dma_alloc_coherent(&pcidev->dev, BSG_MBOX_SIZE,
2809 &(dmabuf->phys), GFP_KERNEL);
James Smart7ad20aa2011-05-24 11:44:28 -04002810
2811 if (!dmabuf->virt) {
2812 kfree(dmabuf);
2813 return NULL;
2814 }
James Smart7ad20aa2011-05-24 11:44:28 -04002815
2816 return dmabuf;
2817}
2818
2819/**
2820 * lpfc_bsg_dma_page_free - free a bsg mbox page sized dma buffer
2821 * @phba: Pointer to HBA context object.
2822 * @dmabuf: Pointer to the bsg mbox page sized dma buffer descriptor.
2823 *
2824 * This routine just simply frees a dma buffer and its associated buffer
2825 * descriptor referred by @dmabuf.
2826 **/
2827static void
2828lpfc_bsg_dma_page_free(struct lpfc_hba *phba, struct lpfc_dmabuf *dmabuf)
2829{
2830 struct pci_dev *pcidev = phba->pcidev;
2831
2832 if (!dmabuf)
2833 return;
2834
2835 if (dmabuf->virt)
2836 dma_free_coherent(&pcidev->dev, BSG_MBOX_SIZE,
2837 dmabuf->virt, dmabuf->phys);
2838 kfree(dmabuf);
2839 return;
2840}
2841
2842/**
2843 * lpfc_bsg_dma_page_list_free - free a list of bsg mbox page sized dma buffers
2844 * @phba: Pointer to HBA context object.
2845 * @dmabuf_list: Pointer to a list of bsg mbox page sized dma buffer descs.
2846 *
2847 * This routine just simply frees all dma buffers and their associated buffer
2848 * descriptors referred by @dmabuf_list.
2849 **/
2850static void
2851lpfc_bsg_dma_page_list_free(struct lpfc_hba *phba,
2852 struct list_head *dmabuf_list)
2853{
2854 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
2855
2856 if (list_empty(dmabuf_list))
2857 return;
2858
2859 list_for_each_entry_safe(dmabuf, next_dmabuf, dmabuf_list, list) {
2860 list_del_init(&dmabuf->list);
2861 lpfc_bsg_dma_page_free(phba, dmabuf);
2862 }
2863 return;
2864}
2865
2866/**
James Smart3b5dd522010-01-26 23:10:15 -05002867 * diag_cmd_data_alloc - fills in a bde struct with dma buffers
2868 * @phba: Pointer to HBA context object
2869 * @bpl: Pointer to 64 bit bde structure
2870 * @size: Number of bytes to process
2871 * @nocopydata: Flag to copy user data into the allocated buffer
2872 *
2873 * This function allocates page size buffers and populates an lpfc_dmabufext.
2874 * If allowed the user data pointed to with indataptr is copied into the kernel
2875 * memory. The chained list of page size buffers is returned.
2876 **/
2877static struct lpfc_dmabufext *
2878diag_cmd_data_alloc(struct lpfc_hba *phba,
2879 struct ulp_bde64 *bpl, uint32_t size,
2880 int nocopydata)
2881{
2882 struct lpfc_dmabufext *mlist = NULL;
2883 struct lpfc_dmabufext *dmp;
2884 int cnt, offset = 0, i = 0;
2885 struct pci_dev *pcidev;
2886
2887 pcidev = phba->pcidev;
2888
2889 while (size) {
2890 /* We get chunks of 4K */
2891 if (size > BUF_SZ_4K)
2892 cnt = BUF_SZ_4K;
2893 else
2894 cnt = size;
2895
2896 /* allocate struct lpfc_dmabufext buffer header */
2897 dmp = kmalloc(sizeof(struct lpfc_dmabufext), GFP_KERNEL);
2898 if (!dmp)
2899 goto out;
2900
2901 INIT_LIST_HEAD(&dmp->dma.list);
2902
2903 /* Queue it to a linked list */
2904 if (mlist)
2905 list_add_tail(&dmp->dma.list, &mlist->dma.list);
2906 else
2907 mlist = dmp;
2908
2909 /* allocate buffer */
2910 dmp->dma.virt = dma_alloc_coherent(&pcidev->dev,
2911 cnt,
2912 &(dmp->dma.phys),
2913 GFP_KERNEL);
2914
2915 if (!dmp->dma.virt)
2916 goto out;
2917
2918 dmp->size = cnt;
2919
2920 if (nocopydata) {
2921 bpl->tus.f.bdeFlags = 0;
James Smart3b5dd522010-01-26 23:10:15 -05002922 } else {
2923 memset((uint8_t *)dmp->dma.virt, 0, cnt);
2924 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2925 }
2926
2927 /* build buffer ptr list for IOCB */
2928 bpl->addrLow = le32_to_cpu(putPaddrLow(dmp->dma.phys));
2929 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dmp->dma.phys));
2930 bpl->tus.f.bdeSize = (ushort) cnt;
2931 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2932 bpl++;
2933
2934 i++;
2935 offset += cnt;
2936 size -= cnt;
2937 }
2938
James Smarta2fc4aef2014-09-03 12:57:55 -04002939 if (mlist) {
2940 mlist->flag = i;
2941 return mlist;
2942 }
James Smart3b5dd522010-01-26 23:10:15 -05002943out:
2944 diag_cmd_data_free(phba, mlist);
2945 return NULL;
2946}
2947
2948/**
2949 * lpfcdiag_loop_post_rxbufs - post the receive buffers for an unsol CT cmd
2950 * @phba: Pointer to HBA context object
2951 * @rxxri: Receive exchange id
2952 * @len: Number of data bytes
2953 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002954 * This function allocates and posts a data buffer of sufficient size to receive
James Smart3b5dd522010-01-26 23:10:15 -05002955 * an unsolicted CT command.
2956 **/
2957static int lpfcdiag_loop_post_rxbufs(struct lpfc_hba *phba, uint16_t rxxri,
2958 size_t len)
2959{
James Smart895427b2017-02-12 13:52:30 -08002960 struct lpfc_sli_ring *pring;
James Smart3b5dd522010-01-26 23:10:15 -05002961 struct lpfc_iocbq *cmdiocbq;
2962 IOCB_t *cmd = NULL;
2963 struct list_head head, *curr, *next;
2964 struct lpfc_dmabuf *rxbmp;
2965 struct lpfc_dmabuf *dmp;
2966 struct lpfc_dmabuf *mp[2] = {NULL, NULL};
2967 struct ulp_bde64 *rxbpl = NULL;
2968 uint32_t num_bde;
2969 struct lpfc_dmabufext *rxbuffer = NULL;
2970 int ret_val = 0;
James Smartd439d282010-09-29 11:18:45 -04002971 int iocb_stat;
James Smart3b5dd522010-01-26 23:10:15 -05002972 int i = 0;
2973
James Smart895427b2017-02-12 13:52:30 -08002974 pring = lpfc_phba_elsring(phba);
2975
James Smart3b5dd522010-01-26 23:10:15 -05002976 cmdiocbq = lpfc_sli_get_iocbq(phba);
2977 rxbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2978 if (rxbmp != NULL) {
2979 rxbmp->virt = lpfc_mbuf_alloc(phba, 0, &rxbmp->phys);
James Smartc7495932010-04-06 15:05:28 -04002980 if (rxbmp->virt) {
2981 INIT_LIST_HEAD(&rxbmp->list);
2982 rxbpl = (struct ulp_bde64 *) rxbmp->virt;
2983 rxbuffer = diag_cmd_data_alloc(phba, rxbpl, len, 0);
2984 }
James Smart3b5dd522010-01-26 23:10:15 -05002985 }
2986
Dick Kennedy1234a6d2017-09-29 17:34:29 -07002987 if (!cmdiocbq || !rxbmp || !rxbpl || !rxbuffer || !pring) {
James Smartd439d282010-09-29 11:18:45 -04002988 ret_val = -ENOMEM;
James Smart3b5dd522010-01-26 23:10:15 -05002989 goto err_post_rxbufs_exit;
2990 }
2991
2992 /* Queue buffers for the receive exchange */
2993 num_bde = (uint32_t)rxbuffer->flag;
2994 dmp = &rxbuffer->dma;
2995
2996 cmd = &cmdiocbq->iocb;
2997 i = 0;
2998
2999 INIT_LIST_HEAD(&head);
3000 list_add_tail(&head, &dmp->list);
3001 list_for_each_safe(curr, next, &head) {
3002 mp[i] = list_entry(curr, struct lpfc_dmabuf, list);
3003 list_del(curr);
3004
3005 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
3006 mp[i]->buffer_tag = lpfc_sli_get_buffer_tag(phba);
3007 cmd->un.quexri64cx.buff.bde.addrHigh =
3008 putPaddrHigh(mp[i]->phys);
3009 cmd->un.quexri64cx.buff.bde.addrLow =
3010 putPaddrLow(mp[i]->phys);
3011 cmd->un.quexri64cx.buff.bde.tus.f.bdeSize =
3012 ((struct lpfc_dmabufext *)mp[i])->size;
3013 cmd->un.quexri64cx.buff.buffer_tag = mp[i]->buffer_tag;
3014 cmd->ulpCommand = CMD_QUE_XRI64_CX;
3015 cmd->ulpPU = 0;
3016 cmd->ulpLe = 1;
3017 cmd->ulpBdeCount = 1;
3018 cmd->unsli3.que_xri64cx_ext_words.ebde_count = 0;
3019
3020 } else {
3021 cmd->un.cont64[i].addrHigh = putPaddrHigh(mp[i]->phys);
3022 cmd->un.cont64[i].addrLow = putPaddrLow(mp[i]->phys);
3023 cmd->un.cont64[i].tus.f.bdeSize =
3024 ((struct lpfc_dmabufext *)mp[i])->size;
Colin Ian King258f84f2019-02-12 15:29:45 +00003025 cmd->ulpBdeCount = ++i;
James Smart3b5dd522010-01-26 23:10:15 -05003026
3027 if ((--num_bde > 0) && (i < 2))
3028 continue;
3029
3030 cmd->ulpCommand = CMD_QUE_XRI_BUF64_CX;
3031 cmd->ulpLe = 1;
3032 }
3033
3034 cmd->ulpClass = CLASS3;
3035 cmd->ulpContext = rxxri;
3036
James Smartd439d282010-09-29 11:18:45 -04003037 iocb_stat = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq,
3038 0);
3039 if (iocb_stat == IOCB_ERROR) {
James Smart3b5dd522010-01-26 23:10:15 -05003040 diag_cmd_data_free(phba,
3041 (struct lpfc_dmabufext *)mp[0]);
3042 if (mp[1])
3043 diag_cmd_data_free(phba,
3044 (struct lpfc_dmabufext *)mp[1]);
3045 dmp = list_entry(next, struct lpfc_dmabuf, list);
James Smartd439d282010-09-29 11:18:45 -04003046 ret_val = -EIO;
James Smart3b5dd522010-01-26 23:10:15 -05003047 goto err_post_rxbufs_exit;
3048 }
3049
3050 lpfc_sli_ringpostbuf_put(phba, pring, mp[0]);
3051 if (mp[1]) {
3052 lpfc_sli_ringpostbuf_put(phba, pring, mp[1]);
3053 mp[1] = NULL;
3054 }
3055
3056 /* The iocb was freed by lpfc_sli_issue_iocb */
3057 cmdiocbq = lpfc_sli_get_iocbq(phba);
3058 if (!cmdiocbq) {
3059 dmp = list_entry(next, struct lpfc_dmabuf, list);
James Smartd439d282010-09-29 11:18:45 -04003060 ret_val = -EIO;
James Smart3b5dd522010-01-26 23:10:15 -05003061 goto err_post_rxbufs_exit;
3062 }
3063
3064 cmd = &cmdiocbq->iocb;
3065 i = 0;
3066 }
3067 list_del(&head);
3068
3069err_post_rxbufs_exit:
3070
3071 if (rxbmp) {
3072 if (rxbmp->virt)
3073 lpfc_mbuf_free(phba, rxbmp->virt, rxbmp->phys);
3074 kfree(rxbmp);
3075 }
3076
3077 if (cmdiocbq)
3078 lpfc_sli_release_iocbq(phba, cmdiocbq);
3079 return ret_val;
3080}
3081
3082/**
James Smart7ad20aa2011-05-24 11:44:28 -04003083 * lpfc_bsg_diag_loopback_run - run loopback on a port by issue ct cmd to itself
James Smart3b5dd522010-01-26 23:10:15 -05003084 * @job: LPFC_BSG_VENDOR_DIAG_TEST fc_bsg_job
3085 *
3086 * This function receives a user data buffer to be transmitted and received on
3087 * the same port, the link must be up and in loopback mode prior
3088 * to being called.
3089 * 1. A kernel buffer is allocated to copy the user data into.
3090 * 2. The port registers with "itself".
3091 * 3. The transmit and receive exchange ids are obtained.
3092 * 4. The receive exchange id is posted.
3093 * 5. A new els loopback event is created.
3094 * 6. The command and response iocbs are allocated.
3095 * 7. The cmd iocb FsType is set to elx loopback and the CmdRsp to looppback.
3096 *
3097 * This function is meant to be called n times while the port is in loopback
3098 * so it is the apps responsibility to issue a reset to take the port out
3099 * of loopback mode.
3100 **/
3101static int
Johannes Thumshirn75cc8cf2016-11-17 10:31:19 +01003102lpfc_bsg_diag_loopback_run(struct bsg_job *job)
James Smart3b5dd522010-01-26 23:10:15 -05003103{
Johannes Thumshirncd21c602016-11-17 10:31:14 +01003104 struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01003105 struct fc_bsg_reply *bsg_reply = job->reply;
James Smart3b5dd522010-01-26 23:10:15 -05003106 struct lpfc_hba *phba = vport->phba;
James Smart3b5dd522010-01-26 23:10:15 -05003107 struct lpfc_bsg_event *evt;
3108 struct event_data *evdat;
3109 struct lpfc_sli *psli = &phba->sli;
3110 uint32_t size;
3111 uint32_t full_size;
3112 size_t segment_len = 0, segment_offset = 0, current_offset = 0;
James Smart40426292010-12-15 17:58:10 -05003113 uint16_t rpi = 0;
James Smart1b511972011-12-13 13:23:09 -05003114 struct lpfc_iocbq *cmdiocbq, *rspiocbq = NULL;
3115 IOCB_t *cmd, *rsp = NULL;
James Smart3b5dd522010-01-26 23:10:15 -05003116 struct lpfc_sli_ct_request *ctreq;
3117 struct lpfc_dmabuf *txbmp;
3118 struct ulp_bde64 *txbpl = NULL;
3119 struct lpfc_dmabufext *txbuffer = NULL;
3120 struct list_head head;
3121 struct lpfc_dmabuf *curr;
James Smart1b511972011-12-13 13:23:09 -05003122 uint16_t txxri = 0, rxxri;
James Smart3b5dd522010-01-26 23:10:15 -05003123 uint32_t num_bde;
3124 uint8_t *ptr = NULL, *rx_databuf = NULL;
3125 int rc = 0;
James Smartd439d282010-09-29 11:18:45 -04003126 int time_left;
James Smart5a0916b2013-07-15 18:31:42 -04003127 int iocb_stat = IOCB_SUCCESS;
James Smart3b5dd522010-01-26 23:10:15 -05003128 unsigned long flags;
3129 void *dataout = NULL;
3130 uint32_t total_mem;
3131
3132 /* in case no data is returned return just the return code */
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01003133 bsg_reply->reply_payload_rcv_len = 0;
James Smart3b5dd522010-01-26 23:10:15 -05003134
3135 if (job->request_len <
3136 sizeof(struct fc_bsg_request) + sizeof(struct diag_mode_test)) {
3137 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3138 "2739 Received DIAG TEST request below minimum "
3139 "size\n");
3140 rc = -EINVAL;
3141 goto loopback_test_exit;
3142 }
3143
3144 if (job->request_payload.payload_len !=
3145 job->reply_payload.payload_len) {
3146 rc = -EINVAL;
3147 goto loopback_test_exit;
3148 }
James Smart3b5dd522010-01-26 23:10:15 -05003149
3150 if ((phba->link_state == LPFC_HBA_ERROR) ||
3151 (psli->sli_flag & LPFC_BLOCK_MGMT_IO) ||
3152 (!(psli->sli_flag & LPFC_SLI_ACTIVE))) {
3153 rc = -EACCES;
3154 goto loopback_test_exit;
3155 }
3156
3157 if (!lpfc_is_link_up(phba) || !(phba->link_flag & LS_LOOPBACK_MODE)) {
3158 rc = -EACCES;
3159 goto loopback_test_exit;
3160 }
3161
3162 size = job->request_payload.payload_len;
3163 full_size = size + ELX_LOOPBACK_HEADER_SZ; /* plus the header */
3164
3165 if ((size == 0) || (size > 80 * BUF_SZ_4K)) {
3166 rc = -ERANGE;
3167 goto loopback_test_exit;
3168 }
3169
James Smart63e801c2010-11-20 23:14:19 -05003170 if (full_size >= BUF_SZ_4K) {
James Smart3b5dd522010-01-26 23:10:15 -05003171 /*
3172 * Allocate memory for ioctl data. If buffer is bigger than 64k,
3173 * then we allocate 64k and re-use that buffer over and over to
3174 * xfer the whole block. This is because Linux kernel has a
3175 * problem allocating more than 120k of kernel space memory. Saw
3176 * problem with GET_FCPTARGETMAPPING...
3177 */
3178 if (size <= (64 * 1024))
James Smart63e801c2010-11-20 23:14:19 -05003179 total_mem = full_size;
James Smart3b5dd522010-01-26 23:10:15 -05003180 else
3181 total_mem = 64 * 1024;
3182 } else
3183 /* Allocate memory for ioctl data */
3184 total_mem = BUF_SZ_4K;
3185
3186 dataout = kmalloc(total_mem, GFP_KERNEL);
3187 if (dataout == NULL) {
3188 rc = -ENOMEM;
3189 goto loopback_test_exit;
3190 }
3191
3192 ptr = dataout;
3193 ptr += ELX_LOOPBACK_HEADER_SZ;
3194 sg_copy_to_buffer(job->request_payload.sg_list,
3195 job->request_payload.sg_cnt,
3196 ptr, size);
James Smart3b5dd522010-01-26 23:10:15 -05003197 rc = lpfcdiag_loop_self_reg(phba, &rpi);
James Smartd439d282010-09-29 11:18:45 -04003198 if (rc)
James Smart3b5dd522010-01-26 23:10:15 -05003199 goto loopback_test_exit;
James Smart3b5dd522010-01-26 23:10:15 -05003200
James Smart1b511972011-12-13 13:23:09 -05003201 if (phba->sli_rev < LPFC_SLI_REV4) {
3202 rc = lpfcdiag_loop_get_xri(phba, rpi, &txxri, &rxxri);
3203 if (rc) {
3204 lpfcdiag_loop_self_unreg(phba, rpi);
3205 goto loopback_test_exit;
3206 }
James Smart3b5dd522010-01-26 23:10:15 -05003207
James Smart1b511972011-12-13 13:23:09 -05003208 rc = lpfcdiag_loop_post_rxbufs(phba, rxxri, full_size);
3209 if (rc) {
3210 lpfcdiag_loop_self_unreg(phba, rpi);
3211 goto loopback_test_exit;
3212 }
James Smart3b5dd522010-01-26 23:10:15 -05003213 }
James Smart3b5dd522010-01-26 23:10:15 -05003214 evt = lpfc_bsg_event_new(FC_REG_CT_EVENT, current->pid,
3215 SLI_CT_ELX_LOOPBACK);
3216 if (!evt) {
3217 lpfcdiag_loop_self_unreg(phba, rpi);
3218 rc = -ENOMEM;
3219 goto loopback_test_exit;
3220 }
3221
3222 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3223 list_add(&evt->node, &phba->ct_ev_waiters);
3224 lpfc_bsg_event_ref(evt);
3225 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3226
3227 cmdiocbq = lpfc_sli_get_iocbq(phba);
James Smart1b511972011-12-13 13:23:09 -05003228 if (phba->sli_rev < LPFC_SLI_REV4)
3229 rspiocbq = lpfc_sli_get_iocbq(phba);
James Smart3b5dd522010-01-26 23:10:15 -05003230 txbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3231
3232 if (txbmp) {
3233 txbmp->virt = lpfc_mbuf_alloc(phba, 0, &txbmp->phys);
James Smartc7495932010-04-06 15:05:28 -04003234 if (txbmp->virt) {
3235 INIT_LIST_HEAD(&txbmp->list);
3236 txbpl = (struct ulp_bde64 *) txbmp->virt;
James Smart3b5dd522010-01-26 23:10:15 -05003237 txbuffer = diag_cmd_data_alloc(phba,
3238 txbpl, full_size, 0);
James Smartc7495932010-04-06 15:05:28 -04003239 }
James Smart3b5dd522010-01-26 23:10:15 -05003240 }
3241
James Smart1b511972011-12-13 13:23:09 -05003242 if (!cmdiocbq || !txbmp || !txbpl || !txbuffer || !txbmp->virt) {
3243 rc = -ENOMEM;
3244 goto err_loopback_test_exit;
3245 }
3246 if ((phba->sli_rev < LPFC_SLI_REV4) && !rspiocbq) {
James Smart3b5dd522010-01-26 23:10:15 -05003247 rc = -ENOMEM;
3248 goto err_loopback_test_exit;
3249 }
3250
3251 cmd = &cmdiocbq->iocb;
James Smart1b511972011-12-13 13:23:09 -05003252 if (phba->sli_rev < LPFC_SLI_REV4)
3253 rsp = &rspiocbq->iocb;
James Smart3b5dd522010-01-26 23:10:15 -05003254
3255 INIT_LIST_HEAD(&head);
3256 list_add_tail(&head, &txbuffer->dma.list);
3257 list_for_each_entry(curr, &head, list) {
3258 segment_len = ((struct lpfc_dmabufext *)curr)->size;
3259 if (current_offset == 0) {
3260 ctreq = curr->virt;
3261 memset(ctreq, 0, ELX_LOOPBACK_HEADER_SZ);
3262 ctreq->RevisionId.bits.Revision = SLI_CT_REVISION;
3263 ctreq->RevisionId.bits.InId = 0;
3264 ctreq->FsType = SLI_CT_ELX_LOOPBACK;
3265 ctreq->FsSubType = 0;
3266 ctreq->CommandResponse.bits.CmdRsp = ELX_LOOPBACK_DATA;
3267 ctreq->CommandResponse.bits.Size = size;
3268 segment_offset = ELX_LOOPBACK_HEADER_SZ;
3269 } else
3270 segment_offset = 0;
3271
3272 BUG_ON(segment_offset >= segment_len);
3273 memcpy(curr->virt + segment_offset,
3274 ptr + current_offset,
3275 segment_len - segment_offset);
3276
3277 current_offset += segment_len - segment_offset;
3278 BUG_ON(current_offset > size);
3279 }
3280 list_del(&head);
3281
3282 /* Build the XMIT_SEQUENCE iocb */
James Smart3b5dd522010-01-26 23:10:15 -05003283 num_bde = (uint32_t)txbuffer->flag;
3284
3285 cmd->un.xseq64.bdl.addrHigh = putPaddrHigh(txbmp->phys);
3286 cmd->un.xseq64.bdl.addrLow = putPaddrLow(txbmp->phys);
3287 cmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
3288 cmd->un.xseq64.bdl.bdeSize = (num_bde * sizeof(struct ulp_bde64));
3289
3290 cmd->un.xseq64.w5.hcsw.Fctl = (LS | LA);
3291 cmd->un.xseq64.w5.hcsw.Dfctl = 0;
3292 cmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
3293 cmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
3294
3295 cmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;
3296 cmd->ulpBdeCount = 1;
3297 cmd->ulpLe = 1;
3298 cmd->ulpClass = CLASS3;
James Smart3b5dd522010-01-26 23:10:15 -05003299
James Smart1b511972011-12-13 13:23:09 -05003300 if (phba->sli_rev < LPFC_SLI_REV4) {
3301 cmd->ulpContext = txxri;
3302 } else {
3303 cmd->un.xseq64.bdl.ulpIoTag32 = 0;
3304 cmd->un.ulpWord[3] = phba->sli4_hba.rpi_ids[rpi];
3305 cmdiocbq->context3 = txbmp;
3306 cmdiocbq->sli4_xritag = NO_XRI;
3307 cmd->unsli3.rcvsli3.ox_id = 0xffff;
3308 }
James Smart3b5dd522010-01-26 23:10:15 -05003309 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
James Smart6c7cf482015-04-07 15:07:25 -04003310 cmdiocbq->iocb_flag |= LPFC_IO_LOOPBACK;
James Smart3b5dd522010-01-26 23:10:15 -05003311 cmdiocbq->vport = phba->pport;
James Smart5a0916b2013-07-15 18:31:42 -04003312 cmdiocbq->iocb_cmpl = NULL;
James Smartd439d282010-09-29 11:18:45 -04003313 iocb_stat = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq,
3314 rspiocbq, (phba->fc_ratov * 2) +
3315 LPFC_DRVR_TIMEOUT);
James Smart3b5dd522010-01-26 23:10:15 -05003316
James Smart53151bb2013-10-10 12:24:07 -04003317 if ((iocb_stat != IOCB_SUCCESS) ||
3318 ((phba->sli_rev < LPFC_SLI_REV4) &&
3319 (rsp->ulpStatus != IOSTAT_SUCCESS))) {
James Smart1b511972011-12-13 13:23:09 -05003320 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3321 "3126 Failed loopback test issue iocb: "
3322 "iocb_stat:x%x\n", iocb_stat);
James Smart3b5dd522010-01-26 23:10:15 -05003323 rc = -EIO;
3324 goto err_loopback_test_exit;
3325 }
3326
3327 evt->waiting = 1;
James Smartd439d282010-09-29 11:18:45 -04003328 time_left = wait_event_interruptible_timeout(
James Smart3b5dd522010-01-26 23:10:15 -05003329 evt->wq, !list_empty(&evt->events_to_see),
James Smart256ec0d2013-04-17 20:14:58 -04003330 msecs_to_jiffies(1000 *
3331 ((phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT)));
James Smart3b5dd522010-01-26 23:10:15 -05003332 evt->waiting = 0;
James Smart1b511972011-12-13 13:23:09 -05003333 if (list_empty(&evt->events_to_see)) {
James Smartd439d282010-09-29 11:18:45 -04003334 rc = (time_left) ? -EINTR : -ETIMEDOUT;
James Smart1b511972011-12-13 13:23:09 -05003335 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3336 "3125 Not receiving unsolicited event, "
3337 "rc:x%x\n", rc);
3338 } else {
James Smart3b5dd522010-01-26 23:10:15 -05003339 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3340 list_move(evt->events_to_see.prev, &evt->events_to_get);
3341 evdat = list_entry(evt->events_to_get.prev,
3342 typeof(*evdat), node);
3343 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3344 rx_databuf = evdat->data;
3345 if (evdat->len != full_size) {
3346 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3347 "1603 Loopback test did not receive expected "
3348 "data length. actual length 0x%x expected "
3349 "length 0x%x\n",
3350 evdat->len, full_size);
3351 rc = -EIO;
3352 } else if (rx_databuf == NULL)
3353 rc = -EIO;
3354 else {
3355 rc = IOCB_SUCCESS;
3356 /* skip over elx loopback header */
3357 rx_databuf += ELX_LOOPBACK_HEADER_SZ;
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01003358 bsg_reply->reply_payload_rcv_len =
James Smart3b5dd522010-01-26 23:10:15 -05003359 sg_copy_from_buffer(job->reply_payload.sg_list,
3360 job->reply_payload.sg_cnt,
3361 rx_databuf, size);
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01003362 bsg_reply->reply_payload_rcv_len = size;
James Smart3b5dd522010-01-26 23:10:15 -05003363 }
3364 }
3365
3366err_loopback_test_exit:
3367 lpfcdiag_loop_self_unreg(phba, rpi);
3368
3369 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3370 lpfc_bsg_event_unref(evt); /* release ref */
3371 lpfc_bsg_event_unref(evt); /* delete */
3372 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3373
James Smart5a0916b2013-07-15 18:31:42 -04003374 if ((cmdiocbq != NULL) && (iocb_stat != IOCB_TIMEDOUT))
James Smart3b5dd522010-01-26 23:10:15 -05003375 lpfc_sli_release_iocbq(phba, cmdiocbq);
3376
3377 if (rspiocbq != NULL)
3378 lpfc_sli_release_iocbq(phba, rspiocbq);
3379
3380 if (txbmp != NULL) {
3381 if (txbpl != NULL) {
3382 if (txbuffer != NULL)
3383 diag_cmd_data_free(phba, txbuffer);
3384 lpfc_mbuf_free(phba, txbmp->virt, txbmp->phys);
3385 }
3386 kfree(txbmp);
3387 }
3388
3389loopback_test_exit:
3390 kfree(dataout);
3391 /* make error code available to userspace */
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01003392 bsg_reply->result = rc;
James Smart3b5dd522010-01-26 23:10:15 -05003393 job->dd_data = NULL;
3394 /* complete the job back to userspace if no error */
James Smart1b511972011-12-13 13:23:09 -05003395 if (rc == IOCB_SUCCESS)
Johannes Thumshirn06548162016-11-17 10:31:22 +01003396 bsg_job_done(job, bsg_reply->result,
Johannes Thumshirn1abaede2016-11-17 10:31:13 +01003397 bsg_reply->reply_payload_rcv_len);
James Smart3b5dd522010-01-26 23:10:15 -05003398 return rc;
3399}
3400
3401/**
3402 * lpfc_bsg_get_dfc_rev - process a GET_DFC_REV bsg vendor command
3403 * @job: GET_DFC_REV fc_bsg_job
3404 **/
3405static int
Johannes Thumshirn75cc8cf2016-11-17 10:31:19 +01003406lpfc_bsg_get_dfc_rev(struct bsg_job *job)
James Smart3b5dd522010-01-26 23:10:15 -05003407{
Johannes Thumshirncd21c602016-11-17 10:31:14 +01003408 struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01003409 struct fc_bsg_reply *bsg_reply = job->reply;
James Smart3b5dd522010-01-26 23:10:15 -05003410 struct lpfc_hba *phba = vport->phba;
James Smart3b5dd522010-01-26 23:10:15 -05003411 struct get_mgmt_rev_reply *event_reply;
3412 int rc = 0;
3413
3414 if (job->request_len <
3415 sizeof(struct fc_bsg_request) + sizeof(struct get_mgmt_rev)) {
3416 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3417 "2740 Received GET_DFC_REV request below "
3418 "minimum size\n");
3419 rc = -EINVAL;
3420 goto job_error;
3421 }
3422
James Smart3b5dd522010-01-26 23:10:15 -05003423 event_reply = (struct get_mgmt_rev_reply *)
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01003424 bsg_reply->reply_data.vendor_reply.vendor_rsp;
James Smart3b5dd522010-01-26 23:10:15 -05003425
3426 if (job->reply_len <
3427 sizeof(struct fc_bsg_request) + sizeof(struct get_mgmt_rev_reply)) {
3428 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3429 "2741 Received GET_DFC_REV reply below "
3430 "minimum size\n");
3431 rc = -EINVAL;
3432 goto job_error;
3433 }
3434
3435 event_reply->info.a_Major = MANAGEMENT_MAJOR_REV;
3436 event_reply->info.a_Minor = MANAGEMENT_MINOR_REV;
3437job_error:
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01003438 bsg_reply->result = rc;
James Smart3b5dd522010-01-26 23:10:15 -05003439 if (rc == 0)
Johannes Thumshirn06548162016-11-17 10:31:22 +01003440 bsg_job_done(job, bsg_reply->result,
Johannes Thumshirn1abaede2016-11-17 10:31:13 +01003441 bsg_reply->reply_payload_rcv_len);
James Smart3b5dd522010-01-26 23:10:15 -05003442 return rc;
3443}
3444
3445/**
James Smart7ad20aa2011-05-24 11:44:28 -04003446 * lpfc_bsg_issue_mbox_cmpl - lpfc_bsg_issue_mbox mbox completion handler
James Smart3b5dd522010-01-26 23:10:15 -05003447 * @phba: Pointer to HBA context object.
3448 * @pmboxq: Pointer to mailbox command.
3449 *
3450 * This is completion handler function for mailbox commands issued from
3451 * lpfc_bsg_issue_mbox function. This function is called by the
3452 * mailbox event handler function with no lock held. This function
3453 * will wake up thread waiting on the wait queue pointed by context1
3454 * of the mailbox.
3455 **/
Rashika Kheria9ab9b132014-09-03 12:55:46 -04003456static void
James Smart7ad20aa2011-05-24 11:44:28 -04003457lpfc_bsg_issue_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
James Smart3b5dd522010-01-26 23:10:15 -05003458{
3459 struct bsg_job_data *dd_data;
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01003460 struct fc_bsg_reply *bsg_reply;
Johannes Thumshirn75cc8cf2016-11-17 10:31:19 +01003461 struct bsg_job *job;
James Smart3b5dd522010-01-26 23:10:15 -05003462 uint32_t size;
3463 unsigned long flags;
James Smart7ad20aa2011-05-24 11:44:28 -04003464 uint8_t *pmb, *pmb_buf;
James Smart3b5dd522010-01-26 23:10:15 -05003465
James Smart3e1f0712018-11-29 16:09:29 -08003466 dd_data = pmboxq->ctx_ndlp;
James Smart3b5dd522010-01-26 23:10:15 -05003467
James Smart7ad20aa2011-05-24 11:44:28 -04003468 /*
3469 * The outgoing buffer is readily referred from the dma buffer,
3470 * just need to get header part from mailboxq structure.
James Smart7a470272010-03-15 11:25:20 -04003471 */
James Smart7ad20aa2011-05-24 11:44:28 -04003472 pmb = (uint8_t *)&pmboxq->u.mb;
3473 pmb_buf = (uint8_t *)dd_data->context_un.mbox.mb;
3474 memcpy(pmb_buf, pmb, sizeof(MAILBOX_t));
James Smart515e0aa2010-09-29 11:19:00 -04003475
James Smarta33c4f72013-03-01 16:36:00 -05003476 /* Determine if job has been aborted */
3477
3478 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3479 job = dd_data->set_job;
3480 if (job) {
3481 /* Prevent timeout handling from trying to abort job */
3482 job->dd_data = NULL;
3483 }
3484 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3485
3486 /* Copy the mailbox data to the job if it is still active */
3487
James Smart5a6f1332011-03-11 16:05:35 -05003488 if (job) {
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01003489 bsg_reply = job->reply;
James Smart5a6f1332011-03-11 16:05:35 -05003490 size = job->reply_payload.payload_len;
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01003491 bsg_reply->reply_payload_rcv_len =
James Smart5a6f1332011-03-11 16:05:35 -05003492 sg_copy_from_buffer(job->reply_payload.sg_list,
James Smart7ad20aa2011-05-24 11:44:28 -04003493 job->reply_payload.sg_cnt,
3494 pmb_buf, size);
James Smart5a6f1332011-03-11 16:05:35 -05003495 }
James Smart7a470272010-03-15 11:25:20 -04003496
James Smarta33c4f72013-03-01 16:36:00 -05003497 dd_data->set_job = NULL;
James Smart3b5dd522010-01-26 23:10:15 -05003498 mempool_free(dd_data->context_un.mbox.pmboxq, phba->mbox_mem_pool);
James Smart7ad20aa2011-05-24 11:44:28 -04003499 lpfc_bsg_dma_page_free(phba, dd_data->context_un.mbox.dmabuffers);
James Smart3b5dd522010-01-26 23:10:15 -05003500 kfree(dd_data);
James Smart7ad20aa2011-05-24 11:44:28 -04003501
James Smarta33c4f72013-03-01 16:36:00 -05003502 /* Complete the job if the job is still active */
3503
James Smart7ad20aa2011-05-24 11:44:28 -04003504 if (job) {
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01003505 bsg_reply->result = 0;
Johannes Thumshirn06548162016-11-17 10:31:22 +01003506 bsg_job_done(job, bsg_reply->result,
Johannes Thumshirn1abaede2016-11-17 10:31:13 +01003507 bsg_reply->reply_payload_rcv_len);
James Smart7ad20aa2011-05-24 11:44:28 -04003508 }
James Smart3b5dd522010-01-26 23:10:15 -05003509 return;
3510}
3511
3512/**
3513 * lpfc_bsg_check_cmd_access - test for a supported mailbox command
3514 * @phba: Pointer to HBA context object.
3515 * @mb: Pointer to a mailbox object.
3516 * @vport: Pointer to a vport object.
3517 *
3518 * Some commands require the port to be offline, some may not be called from
3519 * the application.
3520 **/
3521static int lpfc_bsg_check_cmd_access(struct lpfc_hba *phba,
3522 MAILBOX_t *mb, struct lpfc_vport *vport)
3523{
3524 /* return negative error values for bsg job */
3525 switch (mb->mbxCommand) {
3526 /* Offline only */
3527 case MBX_INIT_LINK:
3528 case MBX_DOWN_LINK:
3529 case MBX_CONFIG_LINK:
3530 case MBX_CONFIG_RING:
3531 case MBX_RESET_RING:
3532 case MBX_UNREG_LOGIN:
3533 case MBX_CLEAR_LA:
3534 case MBX_DUMP_CONTEXT:
3535 case MBX_RUN_DIAGS:
3536 case MBX_RESTART:
3537 case MBX_SET_MASK:
3538 if (!(vport->fc_flag & FC_OFFLINE_MODE)) {
3539 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3540 "2743 Command 0x%x is illegal in on-line "
3541 "state\n",
3542 mb->mbxCommand);
3543 return -EPERM;
3544 }
3545 case MBX_WRITE_NV:
3546 case MBX_WRITE_VPARMS:
3547 case MBX_LOAD_SM:
3548 case MBX_READ_NV:
3549 case MBX_READ_CONFIG:
3550 case MBX_READ_RCONFIG:
3551 case MBX_READ_STATUS:
3552 case MBX_READ_XRI:
3553 case MBX_READ_REV:
3554 case MBX_READ_LNK_STAT:
3555 case MBX_DUMP_MEMORY:
3556 case MBX_DOWN_LOAD:
3557 case MBX_UPDATE_CFG:
3558 case MBX_KILL_BOARD:
James Smart06f35552013-05-31 17:04:50 -04003559 case MBX_READ_TOPOLOGY:
James Smart3b5dd522010-01-26 23:10:15 -05003560 case MBX_LOAD_AREA:
3561 case MBX_LOAD_EXP_ROM:
3562 case MBX_BEACON:
3563 case MBX_DEL_LD_ENTRY:
3564 case MBX_SET_DEBUG:
3565 case MBX_WRITE_WWN:
3566 case MBX_SLI4_CONFIG:
James Smartc7495932010-04-06 15:05:28 -04003567 case MBX_READ_EVENT_LOG:
James Smart3b5dd522010-01-26 23:10:15 -05003568 case MBX_READ_EVENT_LOG_STATUS:
3569 case MBX_WRITE_EVENT_LOG:
3570 case MBX_PORT_CAPABILITIES:
3571 case MBX_PORT_IOV_CONTROL:
James Smart7a470272010-03-15 11:25:20 -04003572 case MBX_RUN_BIU_DIAG64:
James Smart3b5dd522010-01-26 23:10:15 -05003573 break;
3574 case MBX_SET_VARIABLE:
James Smarte2aed292010-02-26 14:15:00 -05003575 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3576 "1226 mbox: set_variable 0x%x, 0x%x\n",
3577 mb->un.varWords[0],
3578 mb->un.varWords[1]);
3579 if ((mb->un.varWords[0] == SETVAR_MLOMNT)
3580 && (mb->un.varWords[1] == 1)) {
3581 phba->wait_4_mlo_maint_flg = 1;
3582 } else if (mb->un.varWords[0] == SETVAR_MLORST) {
James Smart1b511972011-12-13 13:23:09 -05003583 spin_lock_irq(&phba->hbalock);
James Smarte2aed292010-02-26 14:15:00 -05003584 phba->link_flag &= ~LS_LOOPBACK_MODE;
James Smart1b511972011-12-13 13:23:09 -05003585 spin_unlock_irq(&phba->hbalock);
James Smart76a95d72010-11-20 23:11:48 -05003586 phba->fc_topology = LPFC_TOPOLOGY_PT_PT;
James Smarte2aed292010-02-26 14:15:00 -05003587 }
3588 break;
James Smart3b5dd522010-01-26 23:10:15 -05003589 case MBX_READ_SPARM64:
James Smart3b5dd522010-01-26 23:10:15 -05003590 case MBX_REG_LOGIN:
3591 case MBX_REG_LOGIN64:
3592 case MBX_CONFIG_PORT:
3593 case MBX_RUN_BIU_DIAG:
3594 default:
3595 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3596 "2742 Unknown Command 0x%x\n",
3597 mb->mbxCommand);
3598 return -EPERM;
3599 }
3600
3601 return 0; /* ok */
3602}
3603
3604/**
James Smart7ad20aa2011-05-24 11:44:28 -04003605 * lpfc_bsg_mbox_ext_cleanup - clean up context of multi-buffer mbox session
3606 * @phba: Pointer to HBA context object.
3607 *
3608 * This is routine clean up and reset BSG handling of multi-buffer mbox
3609 * command session.
3610 **/
3611static void
3612lpfc_bsg_mbox_ext_session_reset(struct lpfc_hba *phba)
3613{
3614 if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_IDLE)
3615 return;
3616
3617 /* free all memory, including dma buffers */
3618 lpfc_bsg_dma_page_list_free(phba,
3619 &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
3620 lpfc_bsg_dma_page_free(phba, phba->mbox_ext_buf_ctx.mbx_dmabuf);
3621 /* multi-buffer write mailbox command pass-through complete */
3622 memset((char *)&phba->mbox_ext_buf_ctx, 0,
3623 sizeof(struct lpfc_mbox_ext_buf_ctx));
3624 INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
3625
3626 return;
3627}
3628
3629/**
3630 * lpfc_bsg_issue_mbox_ext_handle_job - job handler for multi-buffer mbox cmpl
3631 * @phba: Pointer to HBA context object.
3632 * @pmboxq: Pointer to mailbox command.
3633 *
3634 * This is routine handles BSG job for mailbox commands completions with
3635 * multiple external buffers.
3636 **/
Johannes Thumshirn75cc8cf2016-11-17 10:31:19 +01003637static struct bsg_job *
James Smart7ad20aa2011-05-24 11:44:28 -04003638lpfc_bsg_issue_mbox_ext_handle_job(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
3639{
3640 struct bsg_job_data *dd_data;
Johannes Thumshirn75cc8cf2016-11-17 10:31:19 +01003641 struct bsg_job *job;
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01003642 struct fc_bsg_reply *bsg_reply;
James Smart7ad20aa2011-05-24 11:44:28 -04003643 uint8_t *pmb, *pmb_buf;
3644 unsigned long flags;
3645 uint32_t size;
3646 int rc = 0;
James Smart026abb82011-12-13 13:20:45 -05003647 struct lpfc_dmabuf *dmabuf;
3648 struct lpfc_sli_config_mbox *sli_cfg_mbx;
3649 uint8_t *pmbx;
James Smart7ad20aa2011-05-24 11:44:28 -04003650
James Smart3e1f0712018-11-29 16:09:29 -08003651 dd_data = pmboxq->ctx_buf;
James Smarta33c4f72013-03-01 16:36:00 -05003652
3653 /* Determine if job has been aborted */
3654 spin_lock_irqsave(&phba->ct_ev_lock, flags);
3655 job = dd_data->set_job;
3656 if (job) {
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01003657 bsg_reply = job->reply;
James Smarta33c4f72013-03-01 16:36:00 -05003658 /* Prevent timeout handling from trying to abort job */
3659 job->dd_data = NULL;
James Smart7ad20aa2011-05-24 11:44:28 -04003660 }
James Smarta33c4f72013-03-01 16:36:00 -05003661 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smart7ad20aa2011-05-24 11:44:28 -04003662
3663 /*
3664 * The outgoing buffer is readily referred from the dma buffer,
3665 * just need to get header part from mailboxq structure.
3666 */
James Smarta33c4f72013-03-01 16:36:00 -05003667
James Smart7ad20aa2011-05-24 11:44:28 -04003668 pmb = (uint8_t *)&pmboxq->u.mb;
3669 pmb_buf = (uint8_t *)dd_data->context_un.mbox.mb;
James Smart026abb82011-12-13 13:20:45 -05003670 /* Copy the byte swapped response mailbox back to the user */
James Smart7ad20aa2011-05-24 11:44:28 -04003671 memcpy(pmb_buf, pmb, sizeof(MAILBOX_t));
James Smart026abb82011-12-13 13:20:45 -05003672 /* if there is any non-embedded extended data copy that too */
3673 dmabuf = phba->mbox_ext_buf_ctx.mbx_dmabuf;
3674 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
3675 if (!bsg_bf_get(lpfc_mbox_hdr_emb,
3676 &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) {
3677 pmbx = (uint8_t *)dmabuf->virt;
3678 /* byte swap the extended data following the mailbox command */
3679 lpfc_sli_pcimem_bcopy(&pmbx[sizeof(MAILBOX_t)],
3680 &pmbx[sizeof(MAILBOX_t)],
3681 sli_cfg_mbx->un.sli_config_emb0_subsys.mse[0].buf_len);
3682 }
James Smart7ad20aa2011-05-24 11:44:28 -04003683
James Smarta33c4f72013-03-01 16:36:00 -05003684 /* Complete the job if the job is still active */
3685
James Smart7ad20aa2011-05-24 11:44:28 -04003686 if (job) {
3687 size = job->reply_payload.payload_len;
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01003688 bsg_reply->reply_payload_rcv_len =
James Smart7ad20aa2011-05-24 11:44:28 -04003689 sg_copy_from_buffer(job->reply_payload.sg_list,
3690 job->reply_payload.sg_cnt,
3691 pmb_buf, size);
James Smarta33c4f72013-03-01 16:36:00 -05003692
James Smart7ad20aa2011-05-24 11:44:28 -04003693 /* result for successful */
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01003694 bsg_reply->result = 0;
James Smarta33c4f72013-03-01 16:36:00 -05003695
James Smart7ad20aa2011-05-24 11:44:28 -04003696 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
Colin Ian King7afc0ce2018-05-03 10:26:12 +01003697 "2937 SLI_CONFIG ext-buffer mailbox command "
James Smart7ad20aa2011-05-24 11:44:28 -04003698 "(x%x/x%x) complete bsg job done, bsize:%d\n",
3699 phba->mbox_ext_buf_ctx.nembType,
3700 phba->mbox_ext_buf_ctx.mboxType, size);
James Smartb76f2dc2011-07-22 18:37:42 -04003701 lpfc_idiag_mbxacc_dump_bsg_mbox(phba,
3702 phba->mbox_ext_buf_ctx.nembType,
3703 phba->mbox_ext_buf_ctx.mboxType,
3704 dma_ebuf, sta_pos_addr,
3705 phba->mbox_ext_buf_ctx.mbx_dmabuf, 0);
James Smarta33c4f72013-03-01 16:36:00 -05003706 } else {
James Smart7ad20aa2011-05-24 11:44:28 -04003707 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
Colin Ian King7afc0ce2018-05-03 10:26:12 +01003708 "2938 SLI_CONFIG ext-buffer mailbox "
James Smart7ad20aa2011-05-24 11:44:28 -04003709 "command (x%x/x%x) failure, rc:x%x\n",
3710 phba->mbox_ext_buf_ctx.nembType,
3711 phba->mbox_ext_buf_ctx.mboxType, rc);
James Smarta33c4f72013-03-01 16:36:00 -05003712 }
3713
3714
James Smart7ad20aa2011-05-24 11:44:28 -04003715 /* state change */
3716 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_DONE;
3717 kfree(dd_data);
James Smart7ad20aa2011-05-24 11:44:28 -04003718 return job;
3719}
3720
3721/**
3722 * lpfc_bsg_issue_read_mbox_ext_cmpl - compl handler for multi-buffer read mbox
3723 * @phba: Pointer to HBA context object.
3724 * @pmboxq: Pointer to mailbox command.
3725 *
3726 * This is completion handler function for mailbox read commands with multiple
3727 * external buffers.
3728 **/
3729static void
3730lpfc_bsg_issue_read_mbox_ext_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
3731{
Johannes Thumshirn75cc8cf2016-11-17 10:31:19 +01003732 struct bsg_job *job;
Johannes Thumshirn1abaede2016-11-17 10:31:13 +01003733 struct fc_bsg_reply *bsg_reply;
James Smart7ad20aa2011-05-24 11:44:28 -04003734
James Smarta33c4f72013-03-01 16:36:00 -05003735 job = lpfc_bsg_issue_mbox_ext_handle_job(phba, pmboxq);
3736
James Smart7ad20aa2011-05-24 11:44:28 -04003737 /* handle the BSG job with mailbox command */
James Smarta33c4f72013-03-01 16:36:00 -05003738 if (!job)
James Smart7ad20aa2011-05-24 11:44:28 -04003739 pmboxq->u.mb.mbxStatus = MBXERR_ERROR;
3740
3741 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
Colin Ian King7afc0ce2018-05-03 10:26:12 +01003742 "2939 SLI_CONFIG ext-buffer rd mailbox command "
James Smart7ad20aa2011-05-24 11:44:28 -04003743 "complete, ctxState:x%x, mbxStatus:x%x\n",
3744 phba->mbox_ext_buf_ctx.state, pmboxq->u.mb.mbxStatus);
3745
James Smart7ad20aa2011-05-24 11:44:28 -04003746 if (pmboxq->u.mb.mbxStatus || phba->mbox_ext_buf_ctx.numBuf == 1)
3747 lpfc_bsg_mbox_ext_session_reset(phba);
3748
3749 /* free base driver mailbox structure memory */
3750 mempool_free(pmboxq, phba->mbox_mem_pool);
3751
James Smarta33c4f72013-03-01 16:36:00 -05003752 /* if the job is still active, call job done */
Johannes Thumshirn1abaede2016-11-17 10:31:13 +01003753 if (job) {
3754 bsg_reply = job->reply;
Johannes Thumshirn06548162016-11-17 10:31:22 +01003755 bsg_job_done(job, bsg_reply->result,
Johannes Thumshirn1abaede2016-11-17 10:31:13 +01003756 bsg_reply->reply_payload_rcv_len);
3757 }
James Smart7ad20aa2011-05-24 11:44:28 -04003758 return;
3759}
3760
3761/**
3762 * lpfc_bsg_issue_write_mbox_ext_cmpl - cmpl handler for multi-buffer write mbox
3763 * @phba: Pointer to HBA context object.
3764 * @pmboxq: Pointer to mailbox command.
3765 *
3766 * This is completion handler function for mailbox write commands with multiple
3767 * external buffers.
3768 **/
3769static void
3770lpfc_bsg_issue_write_mbox_ext_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
3771{
Johannes Thumshirn75cc8cf2016-11-17 10:31:19 +01003772 struct bsg_job *job;
Johannes Thumshirn1abaede2016-11-17 10:31:13 +01003773 struct fc_bsg_reply *bsg_reply;
James Smart7ad20aa2011-05-24 11:44:28 -04003774
James Smarta33c4f72013-03-01 16:36:00 -05003775 job = lpfc_bsg_issue_mbox_ext_handle_job(phba, pmboxq);
3776
James Smart7ad20aa2011-05-24 11:44:28 -04003777 /* handle the BSG job with the mailbox command */
James Smarta33c4f72013-03-01 16:36:00 -05003778 if (!job)
James Smart7ad20aa2011-05-24 11:44:28 -04003779 pmboxq->u.mb.mbxStatus = MBXERR_ERROR;
3780
3781 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
Colin Ian King7afc0ce2018-05-03 10:26:12 +01003782 "2940 SLI_CONFIG ext-buffer wr mailbox command "
James Smart7ad20aa2011-05-24 11:44:28 -04003783 "complete, ctxState:x%x, mbxStatus:x%x\n",
3784 phba->mbox_ext_buf_ctx.state, pmboxq->u.mb.mbxStatus);
3785
James Smart7ad20aa2011-05-24 11:44:28 -04003786 /* free all memory, including dma buffers */
3787 mempool_free(pmboxq, phba->mbox_mem_pool);
3788 lpfc_bsg_mbox_ext_session_reset(phba);
3789
James Smarta33c4f72013-03-01 16:36:00 -05003790 /* if the job is still active, call job done */
Johannes Thumshirn1abaede2016-11-17 10:31:13 +01003791 if (job) {
3792 bsg_reply = job->reply;
Johannes Thumshirn06548162016-11-17 10:31:22 +01003793 bsg_job_done(job, bsg_reply->result,
Johannes Thumshirn1abaede2016-11-17 10:31:13 +01003794 bsg_reply->reply_payload_rcv_len);
3795 }
James Smart7ad20aa2011-05-24 11:44:28 -04003796
3797 return;
3798}
3799
3800static void
3801lpfc_bsg_sli_cfg_dma_desc_setup(struct lpfc_hba *phba, enum nemb_type nemb_tp,
3802 uint32_t index, struct lpfc_dmabuf *mbx_dmabuf,
3803 struct lpfc_dmabuf *ext_dmabuf)
3804{
3805 struct lpfc_sli_config_mbox *sli_cfg_mbx;
3806
3807 /* pointer to the start of mailbox command */
3808 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)mbx_dmabuf->virt;
3809
3810 if (nemb_tp == nemb_mse) {
3811 if (index == 0) {
3812 sli_cfg_mbx->un.sli_config_emb0_subsys.
3813 mse[index].pa_hi =
3814 putPaddrHigh(mbx_dmabuf->phys +
3815 sizeof(MAILBOX_t));
3816 sli_cfg_mbx->un.sli_config_emb0_subsys.
3817 mse[index].pa_lo =
3818 putPaddrLow(mbx_dmabuf->phys +
3819 sizeof(MAILBOX_t));
3820 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3821 "2943 SLI_CONFIG(mse)[%d], "
3822 "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3823 index,
3824 sli_cfg_mbx->un.sli_config_emb0_subsys.
3825 mse[index].buf_len,
3826 sli_cfg_mbx->un.sli_config_emb0_subsys.
3827 mse[index].pa_hi,
3828 sli_cfg_mbx->un.sli_config_emb0_subsys.
3829 mse[index].pa_lo);
3830 } else {
3831 sli_cfg_mbx->un.sli_config_emb0_subsys.
3832 mse[index].pa_hi =
3833 putPaddrHigh(ext_dmabuf->phys);
3834 sli_cfg_mbx->un.sli_config_emb0_subsys.
3835 mse[index].pa_lo =
3836 putPaddrLow(ext_dmabuf->phys);
3837 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3838 "2944 SLI_CONFIG(mse)[%d], "
3839 "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3840 index,
3841 sli_cfg_mbx->un.sli_config_emb0_subsys.
3842 mse[index].buf_len,
3843 sli_cfg_mbx->un.sli_config_emb0_subsys.
3844 mse[index].pa_hi,
3845 sli_cfg_mbx->un.sli_config_emb0_subsys.
3846 mse[index].pa_lo);
3847 }
3848 } else {
3849 if (index == 0) {
3850 sli_cfg_mbx->un.sli_config_emb1_subsys.
3851 hbd[index].pa_hi =
3852 putPaddrHigh(mbx_dmabuf->phys +
3853 sizeof(MAILBOX_t));
3854 sli_cfg_mbx->un.sli_config_emb1_subsys.
3855 hbd[index].pa_lo =
3856 putPaddrLow(mbx_dmabuf->phys +
3857 sizeof(MAILBOX_t));
3858 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3859 "3007 SLI_CONFIG(hbd)[%d], "
3860 "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3861 index,
3862 bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
3863 &sli_cfg_mbx->un.
3864 sli_config_emb1_subsys.hbd[index]),
3865 sli_cfg_mbx->un.sli_config_emb1_subsys.
3866 hbd[index].pa_hi,
3867 sli_cfg_mbx->un.sli_config_emb1_subsys.
3868 hbd[index].pa_lo);
3869
3870 } else {
3871 sli_cfg_mbx->un.sli_config_emb1_subsys.
3872 hbd[index].pa_hi =
3873 putPaddrHigh(ext_dmabuf->phys);
3874 sli_cfg_mbx->un.sli_config_emb1_subsys.
3875 hbd[index].pa_lo =
3876 putPaddrLow(ext_dmabuf->phys);
3877 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3878 "3008 SLI_CONFIG(hbd)[%d], "
3879 "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3880 index,
3881 bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
3882 &sli_cfg_mbx->un.
3883 sli_config_emb1_subsys.hbd[index]),
3884 sli_cfg_mbx->un.sli_config_emb1_subsys.
3885 hbd[index].pa_hi,
3886 sli_cfg_mbx->un.sli_config_emb1_subsys.
3887 hbd[index].pa_lo);
3888 }
3889 }
3890 return;
3891}
3892
3893/**
3894 * lpfc_bsg_sli_cfg_mse_read_cmd_ext - sli_config non-embedded mailbox cmd read
3895 * @phba: Pointer to HBA context object.
3896 * @mb: Pointer to a BSG mailbox object.
3897 * @nemb_tp: Enumerate of non-embedded mailbox command type.
3898 * @dmabuff: Pointer to a DMA buffer descriptor.
3899 *
3900 * This routine performs SLI_CONFIG (0x9B) read mailbox command operation with
3901 * non-embedded external bufffers.
3902 **/
3903static int
Johannes Thumshirn75cc8cf2016-11-17 10:31:19 +01003904lpfc_bsg_sli_cfg_read_cmd_ext(struct lpfc_hba *phba, struct bsg_job *job,
James Smart7ad20aa2011-05-24 11:44:28 -04003905 enum nemb_type nemb_tp,
3906 struct lpfc_dmabuf *dmabuf)
3907{
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01003908 struct fc_bsg_request *bsg_request = job->request;
James Smart7ad20aa2011-05-24 11:44:28 -04003909 struct lpfc_sli_config_mbox *sli_cfg_mbx;
3910 struct dfc_mbox_req *mbox_req;
3911 struct lpfc_dmabuf *curr_dmabuf, *next_dmabuf;
3912 uint32_t ext_buf_cnt, ext_buf_index;
3913 struct lpfc_dmabuf *ext_dmabuf = NULL;
3914 struct bsg_job_data *dd_data = NULL;
3915 LPFC_MBOXQ_t *pmboxq = NULL;
3916 MAILBOX_t *pmb;
3917 uint8_t *pmbx;
3918 int rc, i;
3919
3920 mbox_req =
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01003921 (struct dfc_mbox_req *)bsg_request->rqst_data.h_vendor.vendor_cmd;
James Smart7ad20aa2011-05-24 11:44:28 -04003922
3923 /* pointer to the start of mailbox command */
3924 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
3925
3926 if (nemb_tp == nemb_mse) {
3927 ext_buf_cnt = bsg_bf_get(lpfc_mbox_hdr_mse_cnt,
3928 &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr);
3929 if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_MSE) {
3930 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3931 "2945 Handled SLI_CONFIG(mse) rd, "
3932 "ext_buf_cnt(%d) out of range(%d)\n",
3933 ext_buf_cnt,
3934 LPFC_MBX_SLI_CONFIG_MAX_MSE);
3935 rc = -ERANGE;
3936 goto job_error;
3937 }
3938 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3939 "2941 Handled SLI_CONFIG(mse) rd, "
3940 "ext_buf_cnt:%d\n", ext_buf_cnt);
3941 } else {
3942 /* sanity check on interface type for support */
James Smart27d6ac02018-02-22 08:18:42 -08003943 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
James Smart7ad20aa2011-05-24 11:44:28 -04003944 LPFC_SLI_INTF_IF_TYPE_2) {
3945 rc = -ENODEV;
3946 goto job_error;
3947 }
3948 /* nemb_tp == nemb_hbd */
3949 ext_buf_cnt = sli_cfg_mbx->un.sli_config_emb1_subsys.hbd_count;
3950 if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_HBD) {
3951 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3952 "2946 Handled SLI_CONFIG(hbd) rd, "
3953 "ext_buf_cnt(%d) out of range(%d)\n",
3954 ext_buf_cnt,
3955 LPFC_MBX_SLI_CONFIG_MAX_HBD);
3956 rc = -ERANGE;
3957 goto job_error;
3958 }
3959 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3960 "2942 Handled SLI_CONFIG(hbd) rd, "
3961 "ext_buf_cnt:%d\n", ext_buf_cnt);
3962 }
3963
James Smartb76f2dc2011-07-22 18:37:42 -04003964 /* before dma descriptor setup */
3965 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_rd, dma_mbox,
3966 sta_pre_addr, dmabuf, ext_buf_cnt);
3967
James Smart7ad20aa2011-05-24 11:44:28 -04003968 /* reject non-embedded mailbox command with none external buffer */
3969 if (ext_buf_cnt == 0) {
3970 rc = -EPERM;
3971 goto job_error;
3972 } else if (ext_buf_cnt > 1) {
3973 /* additional external read buffers */
3974 for (i = 1; i < ext_buf_cnt; i++) {
3975 ext_dmabuf = lpfc_bsg_dma_page_alloc(phba);
3976 if (!ext_dmabuf) {
3977 rc = -ENOMEM;
3978 goto job_error;
3979 }
3980 list_add_tail(&ext_dmabuf->list,
3981 &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
3982 }
3983 }
3984
3985 /* bsg tracking structure */
3986 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
3987 if (!dd_data) {
3988 rc = -ENOMEM;
3989 goto job_error;
3990 }
3991
3992 /* mailbox command structure for base driver */
3993 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3994 if (!pmboxq) {
3995 rc = -ENOMEM;
3996 goto job_error;
3997 }
3998 memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
3999
4000 /* for the first external buffer */
4001 lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, 0, dmabuf, dmabuf);
4002
4003 /* for the rest of external buffer descriptors if any */
4004 if (ext_buf_cnt > 1) {
4005 ext_buf_index = 1;
4006 list_for_each_entry_safe(curr_dmabuf, next_dmabuf,
4007 &phba->mbox_ext_buf_ctx.ext_dmabuf_list, list) {
4008 lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp,
4009 ext_buf_index, dmabuf,
4010 curr_dmabuf);
4011 ext_buf_index++;
4012 }
4013 }
4014
James Smartb76f2dc2011-07-22 18:37:42 -04004015 /* after dma descriptor setup */
4016 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_rd, dma_mbox,
4017 sta_pos_addr, dmabuf, ext_buf_cnt);
4018
James Smart7ad20aa2011-05-24 11:44:28 -04004019 /* construct base driver mbox command */
4020 pmb = &pmboxq->u.mb;
4021 pmbx = (uint8_t *)dmabuf->virt;
4022 memcpy(pmb, pmbx, sizeof(*pmb));
4023 pmb->mbxOwner = OWN_HOST;
4024 pmboxq->vport = phba->pport;
4025
4026 /* multi-buffer handling context */
4027 phba->mbox_ext_buf_ctx.nembType = nemb_tp;
4028 phba->mbox_ext_buf_ctx.mboxType = mbox_rd;
4029 phba->mbox_ext_buf_ctx.numBuf = ext_buf_cnt;
4030 phba->mbox_ext_buf_ctx.mbxTag = mbox_req->extMboxTag;
4031 phba->mbox_ext_buf_ctx.seqNum = mbox_req->extSeqNum;
4032 phba->mbox_ext_buf_ctx.mbx_dmabuf = dmabuf;
4033
4034 /* callback for multi-buffer read mailbox command */
4035 pmboxq->mbox_cmpl = lpfc_bsg_issue_read_mbox_ext_cmpl;
4036
4037 /* context fields to callback function */
James Smart3e1f0712018-11-29 16:09:29 -08004038 pmboxq->ctx_buf = dd_data;
James Smart7ad20aa2011-05-24 11:44:28 -04004039 dd_data->type = TYPE_MBOX;
James Smarta33c4f72013-03-01 16:36:00 -05004040 dd_data->set_job = job;
James Smart7ad20aa2011-05-24 11:44:28 -04004041 dd_data->context_un.mbox.pmboxq = pmboxq;
4042 dd_data->context_un.mbox.mb = (MAILBOX_t *)pmbx;
James Smart7ad20aa2011-05-24 11:44:28 -04004043 job->dd_data = dd_data;
4044
4045 /* state change */
4046 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
4047
James Smart026abb82011-12-13 13:20:45 -05004048 /*
4049 * Non-embedded mailbox subcommand data gets byte swapped here because
4050 * the lower level driver code only does the first 64 mailbox words.
4051 */
4052 if ((!bsg_bf_get(lpfc_mbox_hdr_emb,
4053 &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) &&
4054 (nemb_tp == nemb_mse))
4055 lpfc_sli_pcimem_bcopy(&pmbx[sizeof(MAILBOX_t)],
4056 &pmbx[sizeof(MAILBOX_t)],
4057 sli_cfg_mbx->un.sli_config_emb0_subsys.
4058 mse[0].buf_len);
4059
James Smart7ad20aa2011-05-24 11:44:28 -04004060 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
4061 if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
4062 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4063 "2947 Issued SLI_CONFIG ext-buffer "
Colin Ian King7afc0ce2018-05-03 10:26:12 +01004064 "mailbox command, rc:x%x\n", rc);
James Smart88a2cfb2011-07-22 18:36:33 -04004065 return SLI_CONFIG_HANDLED;
James Smart7ad20aa2011-05-24 11:44:28 -04004066 }
4067 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4068 "2948 Failed to issue SLI_CONFIG ext-buffer "
Colin Ian King7afc0ce2018-05-03 10:26:12 +01004069 "mailbox command, rc:x%x\n", rc);
James Smart7ad20aa2011-05-24 11:44:28 -04004070 rc = -EPIPE;
4071
4072job_error:
4073 if (pmboxq)
4074 mempool_free(pmboxq, phba->mbox_mem_pool);
4075 lpfc_bsg_dma_page_list_free(phba,
4076 &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
4077 kfree(dd_data);
4078 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_IDLE;
4079 return rc;
4080}
4081
4082/**
4083 * lpfc_bsg_sli_cfg_write_cmd_ext - sli_config non-embedded mailbox cmd write
4084 * @phba: Pointer to HBA context object.
4085 * @mb: Pointer to a BSG mailbox object.
4086 * @dmabuff: Pointer to a DMA buffer descriptor.
4087 *
4088 * This routine performs SLI_CONFIG (0x9B) write mailbox command operation with
4089 * non-embedded external bufffers.
4090 **/
4091static int
Johannes Thumshirn75cc8cf2016-11-17 10:31:19 +01004092lpfc_bsg_sli_cfg_write_cmd_ext(struct lpfc_hba *phba, struct bsg_job *job,
James Smart7ad20aa2011-05-24 11:44:28 -04004093 enum nemb_type nemb_tp,
4094 struct lpfc_dmabuf *dmabuf)
4095{
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01004096 struct fc_bsg_request *bsg_request = job->request;
4097 struct fc_bsg_reply *bsg_reply = job->reply;
James Smart7ad20aa2011-05-24 11:44:28 -04004098 struct dfc_mbox_req *mbox_req;
4099 struct lpfc_sli_config_mbox *sli_cfg_mbx;
4100 uint32_t ext_buf_cnt;
4101 struct bsg_job_data *dd_data = NULL;
4102 LPFC_MBOXQ_t *pmboxq = NULL;
4103 MAILBOX_t *pmb;
4104 uint8_t *mbx;
James Smart88a2cfb2011-07-22 18:36:33 -04004105 int rc = SLI_CONFIG_NOT_HANDLED, i;
James Smart7ad20aa2011-05-24 11:44:28 -04004106
4107 mbox_req =
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01004108 (struct dfc_mbox_req *)bsg_request->rqst_data.h_vendor.vendor_cmd;
James Smart7ad20aa2011-05-24 11:44:28 -04004109
4110 /* pointer to the start of mailbox command */
4111 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
4112
4113 if (nemb_tp == nemb_mse) {
4114 ext_buf_cnt = bsg_bf_get(lpfc_mbox_hdr_mse_cnt,
4115 &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr);
4116 if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_MSE) {
4117 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
James Smart026abb82011-12-13 13:20:45 -05004118 "2953 Failed SLI_CONFIG(mse) wr, "
James Smart7ad20aa2011-05-24 11:44:28 -04004119 "ext_buf_cnt(%d) out of range(%d)\n",
4120 ext_buf_cnt,
4121 LPFC_MBX_SLI_CONFIG_MAX_MSE);
4122 return -ERANGE;
4123 }
4124 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4125 "2949 Handled SLI_CONFIG(mse) wr, "
4126 "ext_buf_cnt:%d\n", ext_buf_cnt);
4127 } else {
4128 /* sanity check on interface type for support */
James Smart27d6ac02018-02-22 08:18:42 -08004129 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
James Smart7ad20aa2011-05-24 11:44:28 -04004130 LPFC_SLI_INTF_IF_TYPE_2)
4131 return -ENODEV;
4132 /* nemb_tp == nemb_hbd */
4133 ext_buf_cnt = sli_cfg_mbx->un.sli_config_emb1_subsys.hbd_count;
4134 if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_HBD) {
4135 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
James Smart026abb82011-12-13 13:20:45 -05004136 "2954 Failed SLI_CONFIG(hbd) wr, "
James Smart7ad20aa2011-05-24 11:44:28 -04004137 "ext_buf_cnt(%d) out of range(%d)\n",
4138 ext_buf_cnt,
4139 LPFC_MBX_SLI_CONFIG_MAX_HBD);
4140 return -ERANGE;
4141 }
4142 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4143 "2950 Handled SLI_CONFIG(hbd) wr, "
4144 "ext_buf_cnt:%d\n", ext_buf_cnt);
4145 }
4146
James Smartb76f2dc2011-07-22 18:37:42 -04004147 /* before dma buffer descriptor setup */
4148 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_wr, dma_mbox,
4149 sta_pre_addr, dmabuf, ext_buf_cnt);
4150
James Smart7ad20aa2011-05-24 11:44:28 -04004151 if (ext_buf_cnt == 0)
4152 return -EPERM;
4153
4154 /* for the first external buffer */
4155 lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, 0, dmabuf, dmabuf);
4156
James Smartb76f2dc2011-07-22 18:37:42 -04004157 /* after dma descriptor setup */
4158 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_wr, dma_mbox,
4159 sta_pos_addr, dmabuf, ext_buf_cnt);
4160
James Smart7ad20aa2011-05-24 11:44:28 -04004161 /* log for looking forward */
4162 for (i = 1; i < ext_buf_cnt; i++) {
4163 if (nemb_tp == nemb_mse)
4164 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4165 "2951 SLI_CONFIG(mse), buf[%d]-length:%d\n",
4166 i, sli_cfg_mbx->un.sli_config_emb0_subsys.
4167 mse[i].buf_len);
4168 else
4169 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4170 "2952 SLI_CONFIG(hbd), buf[%d]-length:%d\n",
4171 i, bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
4172 &sli_cfg_mbx->un.sli_config_emb1_subsys.
4173 hbd[i]));
4174 }
4175
4176 /* multi-buffer handling context */
4177 phba->mbox_ext_buf_ctx.nembType = nemb_tp;
4178 phba->mbox_ext_buf_ctx.mboxType = mbox_wr;
4179 phba->mbox_ext_buf_ctx.numBuf = ext_buf_cnt;
4180 phba->mbox_ext_buf_ctx.mbxTag = mbox_req->extMboxTag;
4181 phba->mbox_ext_buf_ctx.seqNum = mbox_req->extSeqNum;
4182 phba->mbox_ext_buf_ctx.mbx_dmabuf = dmabuf;
4183
4184 if (ext_buf_cnt == 1) {
4185 /* bsg tracking structure */
4186 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
4187 if (!dd_data) {
4188 rc = -ENOMEM;
4189 goto job_error;
4190 }
4191
4192 /* mailbox command structure for base driver */
4193 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4194 if (!pmboxq) {
4195 rc = -ENOMEM;
4196 goto job_error;
4197 }
4198 memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
4199 pmb = &pmboxq->u.mb;
4200 mbx = (uint8_t *)dmabuf->virt;
4201 memcpy(pmb, mbx, sizeof(*pmb));
4202 pmb->mbxOwner = OWN_HOST;
4203 pmboxq->vport = phba->pport;
4204
4205 /* callback for multi-buffer read mailbox command */
4206 pmboxq->mbox_cmpl = lpfc_bsg_issue_write_mbox_ext_cmpl;
4207
4208 /* context fields to callback function */
James Smart3e1f0712018-11-29 16:09:29 -08004209 pmboxq->ctx_buf = dd_data;
James Smart7ad20aa2011-05-24 11:44:28 -04004210 dd_data->type = TYPE_MBOX;
James Smarta33c4f72013-03-01 16:36:00 -05004211 dd_data->set_job = job;
James Smart7ad20aa2011-05-24 11:44:28 -04004212 dd_data->context_un.mbox.pmboxq = pmboxq;
4213 dd_data->context_un.mbox.mb = (MAILBOX_t *)mbx;
James Smart7ad20aa2011-05-24 11:44:28 -04004214 job->dd_data = dd_data;
4215
4216 /* state change */
James Smart7ad20aa2011-05-24 11:44:28 -04004217
James Smarta33c4f72013-03-01 16:36:00 -05004218 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
James Smart7ad20aa2011-05-24 11:44:28 -04004219 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
4220 if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
4221 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4222 "2955 Issued SLI_CONFIG ext-buffer "
Colin Ian King7afc0ce2018-05-03 10:26:12 +01004223 "mailbox command, rc:x%x\n", rc);
James Smart88a2cfb2011-07-22 18:36:33 -04004224 return SLI_CONFIG_HANDLED;
James Smart7ad20aa2011-05-24 11:44:28 -04004225 }
4226 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4227 "2956 Failed to issue SLI_CONFIG ext-buffer "
Colin Ian King7afc0ce2018-05-03 10:26:12 +01004228 "mailbox command, rc:x%x\n", rc);
James Smart7ad20aa2011-05-24 11:44:28 -04004229 rc = -EPIPE;
James Smart026abb82011-12-13 13:20:45 -05004230 goto job_error;
James Smart7ad20aa2011-05-24 11:44:28 -04004231 }
4232
James Smart88a2cfb2011-07-22 18:36:33 -04004233 /* wait for additoinal external buffers */
James Smarta33c4f72013-03-01 16:36:00 -05004234
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01004235 bsg_reply->result = 0;
Johannes Thumshirn06548162016-11-17 10:31:22 +01004236 bsg_job_done(job, bsg_reply->result,
Johannes Thumshirn1abaede2016-11-17 10:31:13 +01004237 bsg_reply->reply_payload_rcv_len);
James Smart88a2cfb2011-07-22 18:36:33 -04004238 return SLI_CONFIG_HANDLED;
4239
James Smart7ad20aa2011-05-24 11:44:28 -04004240job_error:
4241 if (pmboxq)
4242 mempool_free(pmboxq, phba->mbox_mem_pool);
4243 kfree(dd_data);
4244
4245 return rc;
4246}
4247
4248/**
4249 * lpfc_bsg_handle_sli_cfg_mbox - handle sli-cfg mailbox cmd with ext buffer
4250 * @phba: Pointer to HBA context object.
4251 * @mb: Pointer to a BSG mailbox object.
4252 * @dmabuff: Pointer to a DMA buffer descriptor.
4253 *
4254 * This routine handles SLI_CONFIG (0x9B) mailbox command with non-embedded
4255 * external bufffers, including both 0x9B with non-embedded MSEs and 0x9B
4256 * with embedded sussystem 0x1 and opcodes with external HBDs.
4257 **/
4258static int
Johannes Thumshirn75cc8cf2016-11-17 10:31:19 +01004259lpfc_bsg_handle_sli_cfg_mbox(struct lpfc_hba *phba, struct bsg_job *job,
James Smart7ad20aa2011-05-24 11:44:28 -04004260 struct lpfc_dmabuf *dmabuf)
4261{
4262 struct lpfc_sli_config_mbox *sli_cfg_mbx;
4263 uint32_t subsys;
4264 uint32_t opcode;
4265 int rc = SLI_CONFIG_NOT_HANDLED;
4266
James Smart026abb82011-12-13 13:20:45 -05004267 /* state change on new multi-buffer pass-through mailbox command */
James Smart7ad20aa2011-05-24 11:44:28 -04004268 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_HOST;
4269
4270 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
4271
4272 if (!bsg_bf_get(lpfc_mbox_hdr_emb,
4273 &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) {
4274 subsys = bsg_bf_get(lpfc_emb0_subcmnd_subsys,
4275 &sli_cfg_mbx->un.sli_config_emb0_subsys);
4276 opcode = bsg_bf_get(lpfc_emb0_subcmnd_opcode,
4277 &sli_cfg_mbx->un.sli_config_emb0_subsys);
4278 if (subsys == SLI_CONFIG_SUBSYS_FCOE) {
4279 switch (opcode) {
4280 case FCOE_OPCODE_READ_FCF:
James Smart2a2719d2014-02-20 09:56:28 -05004281 case FCOE_OPCODE_GET_DPORT_RESULTS:
James Smart7ad20aa2011-05-24 11:44:28 -04004282 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4283 "2957 Handled SLI_CONFIG "
4284 "subsys_fcoe, opcode:x%x\n",
4285 opcode);
4286 rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
4287 nemb_mse, dmabuf);
4288 break;
4289 case FCOE_OPCODE_ADD_FCF:
James Smart2a2719d2014-02-20 09:56:28 -05004290 case FCOE_OPCODE_SET_DPORT_MODE:
4291 case LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_STATE:
James Smart7ad20aa2011-05-24 11:44:28 -04004292 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4293 "2958 Handled SLI_CONFIG "
4294 "subsys_fcoe, opcode:x%x\n",
4295 opcode);
4296 rc = lpfc_bsg_sli_cfg_write_cmd_ext(phba, job,
4297 nemb_mse, dmabuf);
4298 break;
4299 default:
4300 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
James Smart026abb82011-12-13 13:20:45 -05004301 "2959 Reject SLI_CONFIG "
James Smart7ad20aa2011-05-24 11:44:28 -04004302 "subsys_fcoe, opcode:x%x\n",
4303 opcode);
James Smart026abb82011-12-13 13:20:45 -05004304 rc = -EPERM;
4305 break;
4306 }
4307 } else if (subsys == SLI_CONFIG_SUBSYS_COMN) {
4308 switch (opcode) {
4309 case COMN_OPCODE_GET_CNTL_ADDL_ATTRIBUTES:
James Smartb99570d2012-05-09 21:16:24 -04004310 case COMN_OPCODE_GET_CNTL_ATTRIBUTES:
James Smartc6377972015-04-07 15:07:10 -04004311 case COMN_OPCODE_GET_PROFILE_CONFIG:
James Smart026abb82011-12-13 13:20:45 -05004312 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4313 "3106 Handled SLI_CONFIG "
James Smart6b5151f2012-01-18 16:24:06 -05004314 "subsys_comn, opcode:x%x\n",
James Smart026abb82011-12-13 13:20:45 -05004315 opcode);
4316 rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
4317 nemb_mse, dmabuf);
4318 break;
4319 default:
4320 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4321 "3107 Reject SLI_CONFIG "
James Smart6b5151f2012-01-18 16:24:06 -05004322 "subsys_comn, opcode:x%x\n",
James Smart026abb82011-12-13 13:20:45 -05004323 opcode);
4324 rc = -EPERM;
James Smart7ad20aa2011-05-24 11:44:28 -04004325 break;
4326 }
4327 } else {
4328 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
James Smart026abb82011-12-13 13:20:45 -05004329 "2977 Reject SLI_CONFIG "
James Smart7ad20aa2011-05-24 11:44:28 -04004330 "subsys:x%d, opcode:x%x\n",
4331 subsys, opcode);
James Smart026abb82011-12-13 13:20:45 -05004332 rc = -EPERM;
James Smart7ad20aa2011-05-24 11:44:28 -04004333 }
4334 } else {
4335 subsys = bsg_bf_get(lpfc_emb1_subcmnd_subsys,
4336 &sli_cfg_mbx->un.sli_config_emb1_subsys);
4337 opcode = bsg_bf_get(lpfc_emb1_subcmnd_opcode,
4338 &sli_cfg_mbx->un.sli_config_emb1_subsys);
4339 if (subsys == SLI_CONFIG_SUBSYS_COMN) {
4340 switch (opcode) {
4341 case COMN_OPCODE_READ_OBJECT:
4342 case COMN_OPCODE_READ_OBJECT_LIST:
4343 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4344 "2960 Handled SLI_CONFIG "
4345 "subsys_comn, opcode:x%x\n",
4346 opcode);
4347 rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
4348 nemb_hbd, dmabuf);
4349 break;
4350 case COMN_OPCODE_WRITE_OBJECT:
4351 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4352 "2961 Handled SLI_CONFIG "
4353 "subsys_comn, opcode:x%x\n",
4354 opcode);
4355 rc = lpfc_bsg_sli_cfg_write_cmd_ext(phba, job,
4356 nemb_hbd, dmabuf);
4357 break;
4358 default:
4359 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4360 "2962 Not handled SLI_CONFIG "
4361 "subsys_comn, opcode:x%x\n",
4362 opcode);
4363 rc = SLI_CONFIG_NOT_HANDLED;
4364 break;
4365 }
4366 } else {
4367 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
James Smart026abb82011-12-13 13:20:45 -05004368 "2978 Not handled SLI_CONFIG "
James Smart7ad20aa2011-05-24 11:44:28 -04004369 "subsys:x%d, opcode:x%x\n",
4370 subsys, opcode);
4371 rc = SLI_CONFIG_NOT_HANDLED;
4372 }
4373 }
James Smart026abb82011-12-13 13:20:45 -05004374
4375 /* state reset on not handled new multi-buffer mailbox command */
4376 if (rc != SLI_CONFIG_HANDLED)
4377 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_IDLE;
4378
James Smart7ad20aa2011-05-24 11:44:28 -04004379 return rc;
4380}
4381
4382/**
4383 * lpfc_bsg_mbox_ext_abort_req - request to abort mbox command with ext buffers
4384 * @phba: Pointer to HBA context object.
4385 *
4386 * This routine is for requesting to abort a pass-through mailbox command with
4387 * multiple external buffers due to error condition.
4388 **/
4389static void
4390lpfc_bsg_mbox_ext_abort(struct lpfc_hba *phba)
4391{
4392 if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_PORT)
4393 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_ABTS;
4394 else
4395 lpfc_bsg_mbox_ext_session_reset(phba);
4396 return;
4397}
4398
4399/**
4400 * lpfc_bsg_read_ebuf_get - get the next mailbox read external buffer
4401 * @phba: Pointer to HBA context object.
4402 * @dmabuf: Pointer to a DMA buffer descriptor.
4403 *
4404 * This routine extracts the next mailbox read external buffer back to
4405 * user space through BSG.
4406 **/
4407static int
Johannes Thumshirn75cc8cf2016-11-17 10:31:19 +01004408lpfc_bsg_read_ebuf_get(struct lpfc_hba *phba, struct bsg_job *job)
James Smart7ad20aa2011-05-24 11:44:28 -04004409{
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01004410 struct fc_bsg_reply *bsg_reply = job->reply;
James Smart7ad20aa2011-05-24 11:44:28 -04004411 struct lpfc_sli_config_mbox *sli_cfg_mbx;
4412 struct lpfc_dmabuf *dmabuf;
4413 uint8_t *pbuf;
4414 uint32_t size;
4415 uint32_t index;
4416
4417 index = phba->mbox_ext_buf_ctx.seqNum;
4418 phba->mbox_ext_buf_ctx.seqNum++;
4419
4420 sli_cfg_mbx = (struct lpfc_sli_config_mbox *)
4421 phba->mbox_ext_buf_ctx.mbx_dmabuf->virt;
4422
4423 if (phba->mbox_ext_buf_ctx.nembType == nemb_mse) {
4424 size = bsg_bf_get(lpfc_mbox_sli_config_mse_len,
4425 &sli_cfg_mbx->un.sli_config_emb0_subsys.mse[index]);
4426 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4427 "2963 SLI_CONFIG (mse) ext-buffer rd get "
4428 "buffer[%d], size:%d\n", index, size);
4429 } else {
4430 size = bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
4431 &sli_cfg_mbx->un.sli_config_emb1_subsys.hbd[index]);
4432 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4433 "2964 SLI_CONFIG (hbd) ext-buffer rd get "
4434 "buffer[%d], size:%d\n", index, size);
4435 }
4436 if (list_empty(&phba->mbox_ext_buf_ctx.ext_dmabuf_list))
4437 return -EPIPE;
4438 dmabuf = list_first_entry(&phba->mbox_ext_buf_ctx.ext_dmabuf_list,
4439 struct lpfc_dmabuf, list);
4440 list_del_init(&dmabuf->list);
James Smartb76f2dc2011-07-22 18:37:42 -04004441
4442 /* after dma buffer descriptor setup */
4443 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, phba->mbox_ext_buf_ctx.nembType,
4444 mbox_rd, dma_ebuf, sta_pos_addr,
4445 dmabuf, index);
4446
James Smart7ad20aa2011-05-24 11:44:28 -04004447 pbuf = (uint8_t *)dmabuf->virt;
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01004448 bsg_reply->reply_payload_rcv_len =
James Smart7ad20aa2011-05-24 11:44:28 -04004449 sg_copy_from_buffer(job->reply_payload.sg_list,
4450 job->reply_payload.sg_cnt,
4451 pbuf, size);
4452
4453 lpfc_bsg_dma_page_free(phba, dmabuf);
4454
4455 if (phba->mbox_ext_buf_ctx.seqNum == phba->mbox_ext_buf_ctx.numBuf) {
4456 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4457 "2965 SLI_CONFIG (hbd) ext-buffer rd mbox "
4458 "command session done\n");
4459 lpfc_bsg_mbox_ext_session_reset(phba);
4460 }
4461
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01004462 bsg_reply->result = 0;
Johannes Thumshirn06548162016-11-17 10:31:22 +01004463 bsg_job_done(job, bsg_reply->result,
Johannes Thumshirn1abaede2016-11-17 10:31:13 +01004464 bsg_reply->reply_payload_rcv_len);
James Smart7ad20aa2011-05-24 11:44:28 -04004465
4466 return SLI_CONFIG_HANDLED;
4467}
4468
4469/**
4470 * lpfc_bsg_write_ebuf_set - set the next mailbox write external buffer
4471 * @phba: Pointer to HBA context object.
4472 * @dmabuf: Pointer to a DMA buffer descriptor.
4473 *
4474 * This routine sets up the next mailbox read external buffer obtained
4475 * from user space through BSG.
4476 **/
4477static int
Johannes Thumshirn75cc8cf2016-11-17 10:31:19 +01004478lpfc_bsg_write_ebuf_set(struct lpfc_hba *phba, struct bsg_job *job,
James Smart7ad20aa2011-05-24 11:44:28 -04004479 struct lpfc_dmabuf *dmabuf)
4480{
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01004481 struct fc_bsg_reply *bsg_reply = job->reply;
James Smart7ad20aa2011-05-24 11:44:28 -04004482 struct bsg_job_data *dd_data = NULL;
4483 LPFC_MBOXQ_t *pmboxq = NULL;
4484 MAILBOX_t *pmb;
4485 enum nemb_type nemb_tp;
4486 uint8_t *pbuf;
4487 uint32_t size;
4488 uint32_t index;
4489 int rc;
4490
4491 index = phba->mbox_ext_buf_ctx.seqNum;
4492 phba->mbox_ext_buf_ctx.seqNum++;
4493 nemb_tp = phba->mbox_ext_buf_ctx.nembType;
4494
James Smart7ad20aa2011-05-24 11:44:28 -04004495 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
4496 if (!dd_data) {
4497 rc = -ENOMEM;
4498 goto job_error;
4499 }
4500
4501 pbuf = (uint8_t *)dmabuf->virt;
4502 size = job->request_payload.payload_len;
4503 sg_copy_to_buffer(job->request_payload.sg_list,
4504 job->request_payload.sg_cnt,
4505 pbuf, size);
4506
4507 if (phba->mbox_ext_buf_ctx.nembType == nemb_mse) {
4508 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4509 "2966 SLI_CONFIG (mse) ext-buffer wr set "
4510 "buffer[%d], size:%d\n",
4511 phba->mbox_ext_buf_ctx.seqNum, size);
4512
4513 } else {
4514 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4515 "2967 SLI_CONFIG (hbd) ext-buffer wr set "
4516 "buffer[%d], size:%d\n",
4517 phba->mbox_ext_buf_ctx.seqNum, size);
4518
4519 }
4520
4521 /* set up external buffer descriptor and add to external buffer list */
4522 lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, index,
4523 phba->mbox_ext_buf_ctx.mbx_dmabuf,
4524 dmabuf);
4525 list_add_tail(&dmabuf->list, &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
4526
James Smartb76f2dc2011-07-22 18:37:42 -04004527 /* after write dma buffer */
4528 lpfc_idiag_mbxacc_dump_bsg_mbox(phba, phba->mbox_ext_buf_ctx.nembType,
4529 mbox_wr, dma_ebuf, sta_pos_addr,
4530 dmabuf, index);
4531
James Smart7ad20aa2011-05-24 11:44:28 -04004532 if (phba->mbox_ext_buf_ctx.seqNum == phba->mbox_ext_buf_ctx.numBuf) {
4533 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4534 "2968 SLI_CONFIG ext-buffer wr all %d "
4535 "ebuffers received\n",
4536 phba->mbox_ext_buf_ctx.numBuf);
4537 /* mailbox command structure for base driver */
4538 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4539 if (!pmboxq) {
4540 rc = -ENOMEM;
4541 goto job_error;
4542 }
4543 memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
4544 pbuf = (uint8_t *)phba->mbox_ext_buf_ctx.mbx_dmabuf->virt;
4545 pmb = &pmboxq->u.mb;
4546 memcpy(pmb, pbuf, sizeof(*pmb));
4547 pmb->mbxOwner = OWN_HOST;
4548 pmboxq->vport = phba->pport;
4549
4550 /* callback for multi-buffer write mailbox command */
4551 pmboxq->mbox_cmpl = lpfc_bsg_issue_write_mbox_ext_cmpl;
4552
4553 /* context fields to callback function */
James Smart3e1f0712018-11-29 16:09:29 -08004554 pmboxq->ctx_buf = dd_data;
James Smart7ad20aa2011-05-24 11:44:28 -04004555 dd_data->type = TYPE_MBOX;
James Smarta33c4f72013-03-01 16:36:00 -05004556 dd_data->set_job = job;
James Smart7ad20aa2011-05-24 11:44:28 -04004557 dd_data->context_un.mbox.pmboxq = pmboxq;
4558 dd_data->context_un.mbox.mb = (MAILBOX_t *)pbuf;
James Smart7ad20aa2011-05-24 11:44:28 -04004559 job->dd_data = dd_data;
4560
4561 /* state change */
4562 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
4563
4564 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
4565 if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
4566 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4567 "2969 Issued SLI_CONFIG ext-buffer "
Colin Ian King7afc0ce2018-05-03 10:26:12 +01004568 "mailbox command, rc:x%x\n", rc);
James Smart88a2cfb2011-07-22 18:36:33 -04004569 return SLI_CONFIG_HANDLED;
James Smart7ad20aa2011-05-24 11:44:28 -04004570 }
4571 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4572 "2970 Failed to issue SLI_CONFIG ext-buffer "
Colin Ian King7afc0ce2018-05-03 10:26:12 +01004573 "mailbox command, rc:x%x\n", rc);
James Smart7ad20aa2011-05-24 11:44:28 -04004574 rc = -EPIPE;
4575 goto job_error;
4576 }
4577
4578 /* wait for additoinal external buffers */
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01004579 bsg_reply->result = 0;
Johannes Thumshirn06548162016-11-17 10:31:22 +01004580 bsg_job_done(job, bsg_reply->result,
Johannes Thumshirn1abaede2016-11-17 10:31:13 +01004581 bsg_reply->reply_payload_rcv_len);
James Smart7ad20aa2011-05-24 11:44:28 -04004582 return SLI_CONFIG_HANDLED;
4583
4584job_error:
4585 lpfc_bsg_dma_page_free(phba, dmabuf);
4586 kfree(dd_data);
4587
4588 return rc;
4589}
4590
4591/**
4592 * lpfc_bsg_handle_sli_cfg_ebuf - handle ext buffer with sli-cfg mailbox cmd
4593 * @phba: Pointer to HBA context object.
4594 * @mb: Pointer to a BSG mailbox object.
4595 * @dmabuff: Pointer to a DMA buffer descriptor.
4596 *
4597 * This routine handles the external buffer with SLI_CONFIG (0x9B) mailbox
4598 * command with multiple non-embedded external buffers.
4599 **/
4600static int
Johannes Thumshirn75cc8cf2016-11-17 10:31:19 +01004601lpfc_bsg_handle_sli_cfg_ebuf(struct lpfc_hba *phba, struct bsg_job *job,
James Smart7ad20aa2011-05-24 11:44:28 -04004602 struct lpfc_dmabuf *dmabuf)
4603{
4604 int rc;
4605
4606 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4607 "2971 SLI_CONFIG buffer (type:x%x)\n",
4608 phba->mbox_ext_buf_ctx.mboxType);
4609
4610 if (phba->mbox_ext_buf_ctx.mboxType == mbox_rd) {
4611 if (phba->mbox_ext_buf_ctx.state != LPFC_BSG_MBOX_DONE) {
4612 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4613 "2972 SLI_CONFIG rd buffer state "
4614 "mismatch:x%x\n",
4615 phba->mbox_ext_buf_ctx.state);
4616 lpfc_bsg_mbox_ext_abort(phba);
4617 return -EPIPE;
4618 }
4619 rc = lpfc_bsg_read_ebuf_get(phba, job);
4620 if (rc == SLI_CONFIG_HANDLED)
4621 lpfc_bsg_dma_page_free(phba, dmabuf);
4622 } else { /* phba->mbox_ext_buf_ctx.mboxType == mbox_wr */
4623 if (phba->mbox_ext_buf_ctx.state != LPFC_BSG_MBOX_HOST) {
4624 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4625 "2973 SLI_CONFIG wr buffer state "
4626 "mismatch:x%x\n",
4627 phba->mbox_ext_buf_ctx.state);
4628 lpfc_bsg_mbox_ext_abort(phba);
4629 return -EPIPE;
4630 }
4631 rc = lpfc_bsg_write_ebuf_set(phba, job, dmabuf);
4632 }
4633 return rc;
4634}
4635
4636/**
4637 * lpfc_bsg_handle_sli_cfg_ext - handle sli-cfg mailbox with external buffer
4638 * @phba: Pointer to HBA context object.
4639 * @mb: Pointer to a BSG mailbox object.
4640 * @dmabuff: Pointer to a DMA buffer descriptor.
4641 *
4642 * This routine checkes and handles non-embedded multi-buffer SLI_CONFIG
4643 * (0x9B) mailbox commands and external buffers.
4644 **/
4645static int
Johannes Thumshirn75cc8cf2016-11-17 10:31:19 +01004646lpfc_bsg_handle_sli_cfg_ext(struct lpfc_hba *phba, struct bsg_job *job,
James Smart7ad20aa2011-05-24 11:44:28 -04004647 struct lpfc_dmabuf *dmabuf)
4648{
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01004649 struct fc_bsg_request *bsg_request = job->request;
James Smart7ad20aa2011-05-24 11:44:28 -04004650 struct dfc_mbox_req *mbox_req;
James Smart88a2cfb2011-07-22 18:36:33 -04004651 int rc = SLI_CONFIG_NOT_HANDLED;
James Smart7ad20aa2011-05-24 11:44:28 -04004652
4653 mbox_req =
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01004654 (struct dfc_mbox_req *)bsg_request->rqst_data.h_vendor.vendor_cmd;
James Smart7ad20aa2011-05-24 11:44:28 -04004655
4656 /* mbox command with/without single external buffer */
4657 if (mbox_req->extMboxTag == 0 && mbox_req->extSeqNum == 0)
James Smart88a2cfb2011-07-22 18:36:33 -04004658 return rc;
James Smart7ad20aa2011-05-24 11:44:28 -04004659
4660 /* mbox command and first external buffer */
4661 if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_IDLE) {
4662 if (mbox_req->extSeqNum == 1) {
4663 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4664 "2974 SLI_CONFIG mailbox: tag:%d, "
4665 "seq:%d\n", mbox_req->extMboxTag,
4666 mbox_req->extSeqNum);
4667 rc = lpfc_bsg_handle_sli_cfg_mbox(phba, job, dmabuf);
4668 return rc;
4669 } else
4670 goto sli_cfg_ext_error;
4671 }
4672
4673 /*
4674 * handle additional external buffers
4675 */
4676
4677 /* check broken pipe conditions */
4678 if (mbox_req->extMboxTag != phba->mbox_ext_buf_ctx.mbxTag)
4679 goto sli_cfg_ext_error;
4680 if (mbox_req->extSeqNum > phba->mbox_ext_buf_ctx.numBuf)
4681 goto sli_cfg_ext_error;
4682 if (mbox_req->extSeqNum != phba->mbox_ext_buf_ctx.seqNum + 1)
4683 goto sli_cfg_ext_error;
4684
4685 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4686 "2975 SLI_CONFIG mailbox external buffer: "
4687 "extSta:x%x, tag:%d, seq:%d\n",
4688 phba->mbox_ext_buf_ctx.state, mbox_req->extMboxTag,
4689 mbox_req->extSeqNum);
4690 rc = lpfc_bsg_handle_sli_cfg_ebuf(phba, job, dmabuf);
4691 return rc;
4692
4693sli_cfg_ext_error:
4694 /* all other cases, broken pipe */
4695 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4696 "2976 SLI_CONFIG mailbox broken pipe: "
4697 "ctxSta:x%x, ctxNumBuf:%d "
4698 "ctxTag:%d, ctxSeq:%d, tag:%d, seq:%d\n",
4699 phba->mbox_ext_buf_ctx.state,
4700 phba->mbox_ext_buf_ctx.numBuf,
4701 phba->mbox_ext_buf_ctx.mbxTag,
4702 phba->mbox_ext_buf_ctx.seqNum,
4703 mbox_req->extMboxTag, mbox_req->extSeqNum);
4704
4705 lpfc_bsg_mbox_ext_session_reset(phba);
4706
4707 return -EPIPE;
4708}
4709
4710/**
James Smart3b5dd522010-01-26 23:10:15 -05004711 * lpfc_bsg_issue_mbox - issues a mailbox command on behalf of an app
4712 * @phba: Pointer to HBA context object.
4713 * @mb: Pointer to a mailbox object.
4714 * @vport: Pointer to a vport object.
4715 *
4716 * Allocate a tracking object, mailbox command memory, get a mailbox
4717 * from the mailbox pool, copy the caller mailbox command.
4718 *
4719 * If offline and the sli is active we need to poll for the command (port is
4720 * being reset) and com-plete the job, otherwise issue the mailbox command and
4721 * let our completion handler finish the command.
4722 **/
James Smarta2fc4aef2014-09-03 12:57:55 -04004723static int
Johannes Thumshirn75cc8cf2016-11-17 10:31:19 +01004724lpfc_bsg_issue_mbox(struct lpfc_hba *phba, struct bsg_job *job,
James Smart3b5dd522010-01-26 23:10:15 -05004725 struct lpfc_vport *vport)
4726{
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01004727 struct fc_bsg_request *bsg_request = job->request;
4728 struct fc_bsg_reply *bsg_reply = job->reply;
James Smart7a470272010-03-15 11:25:20 -04004729 LPFC_MBOXQ_t *pmboxq = NULL; /* internal mailbox queue */
4730 MAILBOX_t *pmb; /* shortcut to the pmboxq mailbox */
4731 /* a 4k buffer to hold the mb and extended data from/to the bsg */
James Smart7ad20aa2011-05-24 11:44:28 -04004732 uint8_t *pmbx = NULL;
James Smart7a470272010-03-15 11:25:20 -04004733 struct bsg_job_data *dd_data = NULL; /* bsg data tracking structure */
James Smart7ad20aa2011-05-24 11:44:28 -04004734 struct lpfc_dmabuf *dmabuf = NULL;
4735 struct dfc_mbox_req *mbox_req;
James Smartb6e3b9c2011-04-16 11:03:43 -04004736 struct READ_EVENT_LOG_VAR *rdEventLog;
4737 uint32_t transmit_length, receive_length, mode;
James Smart7ad20aa2011-05-24 11:44:28 -04004738 struct lpfc_mbx_sli4_config *sli4_config;
James Smartb6e3b9c2011-04-16 11:03:43 -04004739 struct lpfc_mbx_nembed_cmd *nembed_sge;
James Smartb6e3b9c2011-04-16 11:03:43 -04004740 struct ulp_bde64 *bde;
James Smart7a470272010-03-15 11:25:20 -04004741 uint8_t *ext = NULL;
James Smart3b5dd522010-01-26 23:10:15 -05004742 int rc = 0;
James Smart7a470272010-03-15 11:25:20 -04004743 uint8_t *from;
James Smart7ad20aa2011-05-24 11:44:28 -04004744 uint32_t size;
4745
James Smart7a470272010-03-15 11:25:20 -04004746 /* in case no data is transferred */
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01004747 bsg_reply->reply_payload_rcv_len = 0;
James Smart7a470272010-03-15 11:25:20 -04004748
James Smartb6e3b9c2011-04-16 11:03:43 -04004749 /* sanity check to protect driver */
4750 if (job->reply_payload.payload_len > BSG_MBOX_SIZE ||
4751 job->request_payload.payload_len > BSG_MBOX_SIZE) {
4752 rc = -ERANGE;
4753 goto job_done;
4754 }
4755
James Smart7ad20aa2011-05-24 11:44:28 -04004756 /*
4757 * Don't allow mailbox commands to be sent when blocked or when in
4758 * the middle of discovery
4759 */
Colin Ian King258f84f2019-02-12 15:29:45 +00004760 if (phba->sli.sli_flag & LPFC_BLOCK_MGMT_IO) {
James Smart7ad20aa2011-05-24 11:44:28 -04004761 rc = -EAGAIN;
4762 goto job_done;
4763 }
4764
4765 mbox_req =
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01004766 (struct dfc_mbox_req *)bsg_request->rqst_data.h_vendor.vendor_cmd;
James Smart7ad20aa2011-05-24 11:44:28 -04004767
James Smart7a470272010-03-15 11:25:20 -04004768 /* check if requested extended data lengths are valid */
James Smartb6e3b9c2011-04-16 11:03:43 -04004769 if ((mbox_req->inExtWLen > BSG_MBOX_SIZE/sizeof(uint32_t)) ||
4770 (mbox_req->outExtWLen > BSG_MBOX_SIZE/sizeof(uint32_t))) {
James Smart7a470272010-03-15 11:25:20 -04004771 rc = -ERANGE;
4772 goto job_done;
4773 }
James Smart3b5dd522010-01-26 23:10:15 -05004774
James Smart7ad20aa2011-05-24 11:44:28 -04004775 dmabuf = lpfc_bsg_dma_page_alloc(phba);
4776 if (!dmabuf || !dmabuf->virt) {
4777 rc = -ENOMEM;
4778 goto job_done;
4779 }
4780
4781 /* Get the mailbox command or external buffer from BSG */
4782 pmbx = (uint8_t *)dmabuf->virt;
4783 size = job->request_payload.payload_len;
4784 sg_copy_to_buffer(job->request_payload.sg_list,
4785 job->request_payload.sg_cnt, pmbx, size);
4786
4787 /* Handle possible SLI_CONFIG with non-embedded payloads */
4788 if (phba->sli_rev == LPFC_SLI_REV4) {
4789 rc = lpfc_bsg_handle_sli_cfg_ext(phba, job, dmabuf);
4790 if (rc == SLI_CONFIG_HANDLED)
4791 goto job_cont;
4792 if (rc)
4793 goto job_done;
4794 /* SLI_CONFIG_NOT_HANDLED for other mailbox commands */
4795 }
4796
4797 rc = lpfc_bsg_check_cmd_access(phba, (MAILBOX_t *)pmbx, vport);
4798 if (rc != 0)
4799 goto job_done; /* must be negative */
4800
James Smart3b5dd522010-01-26 23:10:15 -05004801 /* allocate our bsg tracking structure */
4802 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
4803 if (!dd_data) {
4804 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
4805 "2727 Failed allocation of dd_data\n");
James Smart7a470272010-03-15 11:25:20 -04004806 rc = -ENOMEM;
4807 goto job_done;
James Smart3b5dd522010-01-26 23:10:15 -05004808 }
4809
James Smart3b5dd522010-01-26 23:10:15 -05004810 pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4811 if (!pmboxq) {
James Smart7a470272010-03-15 11:25:20 -04004812 rc = -ENOMEM;
4813 goto job_done;
James Smart3b5dd522010-01-26 23:10:15 -05004814 }
James Smart7a470272010-03-15 11:25:20 -04004815 memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
James Smart3b5dd522010-01-26 23:10:15 -05004816
James Smart3b5dd522010-01-26 23:10:15 -05004817 pmb = &pmboxq->u.mb;
James Smart7ad20aa2011-05-24 11:44:28 -04004818 memcpy(pmb, pmbx, sizeof(*pmb));
James Smart3b5dd522010-01-26 23:10:15 -05004819 pmb->mbxOwner = OWN_HOST;
James Smart3b5dd522010-01-26 23:10:15 -05004820 pmboxq->vport = vport;
4821
James Smartc7495932010-04-06 15:05:28 -04004822 /* If HBA encountered an error attention, allow only DUMP
4823 * or RESTART mailbox commands until the HBA is restarted.
4824 */
4825 if (phba->pport->stopped &&
4826 pmb->mbxCommand != MBX_DUMP_MEMORY &&
4827 pmb->mbxCommand != MBX_RESTART &&
4828 pmb->mbxCommand != MBX_WRITE_VPARMS &&
4829 pmb->mbxCommand != MBX_WRITE_WWN)
4830 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
4831 "2797 mbox: Issued mailbox cmd "
4832 "0x%x while in stopped state.\n",
4833 pmb->mbxCommand);
4834
James Smart7a470272010-03-15 11:25:20 -04004835 /* extended mailbox commands will need an extended buffer */
James Smartc7495932010-04-06 15:05:28 -04004836 if (mbox_req->inExtWLen || mbox_req->outExtWLen) {
James Smart026abb82011-12-13 13:20:45 -05004837 from = pmbx;
4838 ext = from + sizeof(MAILBOX_t);
James Smart3e1f0712018-11-29 16:09:29 -08004839 pmboxq->ctx_buf = ext;
James Smart7a470272010-03-15 11:25:20 -04004840 pmboxq->in_ext_byte_len =
James Smart7a470272010-03-15 11:25:20 -04004841 mbox_req->inExtWLen * sizeof(uint32_t);
4842 pmboxq->out_ext_byte_len =
James Smartc7495932010-04-06 15:05:28 -04004843 mbox_req->outExtWLen * sizeof(uint32_t);
James Smart7a470272010-03-15 11:25:20 -04004844 pmboxq->mbox_offset_word = mbox_req->mbOffset;
4845 }
4846
4847 /* biu diag will need a kernel buffer to transfer the data
4848 * allocate our own buffer and setup the mailbox command to
4849 * use ours
4850 */
4851 if (pmb->mbxCommand == MBX_RUN_BIU_DIAG64) {
James Smartb6e3b9c2011-04-16 11:03:43 -04004852 transmit_length = pmb->un.varWords[1];
4853 receive_length = pmb->un.varWords[4];
James Smartc7495932010-04-06 15:05:28 -04004854 /* transmit length cannot be greater than receive length or
4855 * mailbox extension size
4856 */
4857 if ((transmit_length > receive_length) ||
James Smart88a2cfb2011-07-22 18:36:33 -04004858 (transmit_length > BSG_MBOX_SIZE - sizeof(MAILBOX_t))) {
James Smartc7495932010-04-06 15:05:28 -04004859 rc = -ERANGE;
4860 goto job_done;
4861 }
James Smart7a470272010-03-15 11:25:20 -04004862 pmb->un.varBIUdiag.un.s2.xmit_bde64.addrHigh =
James Smart7ad20aa2011-05-24 11:44:28 -04004863 putPaddrHigh(dmabuf->phys + sizeof(MAILBOX_t));
James Smart7a470272010-03-15 11:25:20 -04004864 pmb->un.varBIUdiag.un.s2.xmit_bde64.addrLow =
James Smart7ad20aa2011-05-24 11:44:28 -04004865 putPaddrLow(dmabuf->phys + sizeof(MAILBOX_t));
James Smart7a470272010-03-15 11:25:20 -04004866
4867 pmb->un.varBIUdiag.un.s2.rcv_bde64.addrHigh =
James Smart7ad20aa2011-05-24 11:44:28 -04004868 putPaddrHigh(dmabuf->phys + sizeof(MAILBOX_t)
4869 + pmb->un.varBIUdiag.un.s2.xmit_bde64.tus.f.bdeSize);
James Smart7a470272010-03-15 11:25:20 -04004870 pmb->un.varBIUdiag.un.s2.rcv_bde64.addrLow =
James Smart7ad20aa2011-05-24 11:44:28 -04004871 putPaddrLow(dmabuf->phys + sizeof(MAILBOX_t)
4872 + pmb->un.varBIUdiag.un.s2.xmit_bde64.tus.f.bdeSize);
James Smartc7495932010-04-06 15:05:28 -04004873 } else if (pmb->mbxCommand == MBX_READ_EVENT_LOG) {
James Smartb6e3b9c2011-04-16 11:03:43 -04004874 rdEventLog = &pmb->un.varRdEventLog;
4875 receive_length = rdEventLog->rcv_bde64.tus.f.bdeSize;
4876 mode = bf_get(lpfc_event_log, rdEventLog);
James Smartc7495932010-04-06 15:05:28 -04004877
4878 /* receive length cannot be greater than mailbox
4879 * extension size
4880 */
James Smart88a2cfb2011-07-22 18:36:33 -04004881 if (receive_length > BSG_MBOX_SIZE - sizeof(MAILBOX_t)) {
James Smartc7495932010-04-06 15:05:28 -04004882 rc = -ERANGE;
4883 goto job_done;
4884 }
4885
4886 /* mode zero uses a bde like biu diags command */
4887 if (mode == 0) {
James Smart7ad20aa2011-05-24 11:44:28 -04004888 pmb->un.varWords[3] = putPaddrLow(dmabuf->phys
4889 + sizeof(MAILBOX_t));
4890 pmb->un.varWords[4] = putPaddrHigh(dmabuf->phys
4891 + sizeof(MAILBOX_t));
James Smartc7495932010-04-06 15:05:28 -04004892 }
4893 } else if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart3ef6d242012-01-18 16:23:48 -05004894 /* Let type 4 (well known data) through because the data is
4895 * returned in varwords[4-8]
4896 * otherwise check the recieve length and fetch the buffer addr
4897 */
4898 if ((pmb->mbxCommand == MBX_DUMP_MEMORY) &&
4899 (pmb->un.varDmp.type != DMP_WELL_KNOWN)) {
James Smartc7495932010-04-06 15:05:28 -04004900 /* rebuild the command for sli4 using our own buffers
4901 * like we do for biu diags
4902 */
James Smartb6e3b9c2011-04-16 11:03:43 -04004903 receive_length = pmb->un.varWords[2];
James Smartc7495932010-04-06 15:05:28 -04004904 /* receive length cannot be greater than mailbox
4905 * extension size
4906 */
James Smart7ad20aa2011-05-24 11:44:28 -04004907 if (receive_length == 0) {
James Smartc7495932010-04-06 15:05:28 -04004908 rc = -ERANGE;
4909 goto job_done;
4910 }
James Smart7ad20aa2011-05-24 11:44:28 -04004911 pmb->un.varWords[3] = putPaddrLow(dmabuf->phys
4912 + sizeof(MAILBOX_t));
4913 pmb->un.varWords[4] = putPaddrHigh(dmabuf->phys
4914 + sizeof(MAILBOX_t));
James Smartc7495932010-04-06 15:05:28 -04004915 } else if ((pmb->mbxCommand == MBX_UPDATE_CFG) &&
4916 pmb->un.varUpdateCfg.co) {
James Smartb6e3b9c2011-04-16 11:03:43 -04004917 bde = (struct ulp_bde64 *)&pmb->un.varWords[4];
James Smartc7495932010-04-06 15:05:28 -04004918
4919 /* bde size cannot be greater than mailbox ext size */
James Smart88a2cfb2011-07-22 18:36:33 -04004920 if (bde->tus.f.bdeSize >
4921 BSG_MBOX_SIZE - sizeof(MAILBOX_t)) {
James Smartc7495932010-04-06 15:05:28 -04004922 rc = -ERANGE;
4923 goto job_done;
4924 }
James Smart7ad20aa2011-05-24 11:44:28 -04004925 bde->addrHigh = putPaddrHigh(dmabuf->phys
4926 + sizeof(MAILBOX_t));
4927 bde->addrLow = putPaddrLow(dmabuf->phys
4928 + sizeof(MAILBOX_t));
James Smart515e0aa2010-09-29 11:19:00 -04004929 } else if (pmb->mbxCommand == MBX_SLI4_CONFIG) {
James Smart7ad20aa2011-05-24 11:44:28 -04004930 /* Handling non-embedded SLI_CONFIG mailbox command */
4931 sli4_config = &pmboxq->u.mqe.un.sli4_config;
4932 if (!bf_get(lpfc_mbox_hdr_emb,
4933 &sli4_config->header.cfg_mhdr)) {
4934 /* rebuild the command for sli4 using our
4935 * own buffers like we do for biu diags
4936 */
James Smart7ad20aa2011-05-24 11:44:28 -04004937 nembed_sge = (struct lpfc_mbx_nembed_cmd *)
4938 &pmb->un.varWords[0];
4939 receive_length = nembed_sge->sge[0].length;
James Smart515e0aa2010-09-29 11:19:00 -04004940
James Smart7ad20aa2011-05-24 11:44:28 -04004941 /* receive length cannot be greater than
4942 * mailbox extension size
4943 */
4944 if ((receive_length == 0) ||
James Smart88a2cfb2011-07-22 18:36:33 -04004945 (receive_length >
4946 BSG_MBOX_SIZE - sizeof(MAILBOX_t))) {
James Smart7ad20aa2011-05-24 11:44:28 -04004947 rc = -ERANGE;
4948 goto job_done;
4949 }
4950
4951 nembed_sge->sge[0].pa_hi =
4952 putPaddrHigh(dmabuf->phys
4953 + sizeof(MAILBOX_t));
4954 nembed_sge->sge[0].pa_lo =
4955 putPaddrLow(dmabuf->phys
4956 + sizeof(MAILBOX_t));
James Smart515e0aa2010-09-29 11:19:00 -04004957 }
James Smartc7495932010-04-06 15:05:28 -04004958 }
James Smart3b5dd522010-01-26 23:10:15 -05004959 }
4960
James Smart7ad20aa2011-05-24 11:44:28 -04004961 dd_data->context_un.mbox.dmabuffers = dmabuf;
James Smartc7495932010-04-06 15:05:28 -04004962
James Smart3b5dd522010-01-26 23:10:15 -05004963 /* setup wake call as IOCB callback */
James Smart7ad20aa2011-05-24 11:44:28 -04004964 pmboxq->mbox_cmpl = lpfc_bsg_issue_mbox_cmpl;
James Smart7a470272010-03-15 11:25:20 -04004965
James Smart3b5dd522010-01-26 23:10:15 -05004966 /* setup context field to pass wait_queue pointer to wake function */
James Smart3e1f0712018-11-29 16:09:29 -08004967 pmboxq->ctx_ndlp = dd_data;
James Smart3b5dd522010-01-26 23:10:15 -05004968 dd_data->type = TYPE_MBOX;
James Smarta33c4f72013-03-01 16:36:00 -05004969 dd_data->set_job = job;
James Smart3b5dd522010-01-26 23:10:15 -05004970 dd_data->context_un.mbox.pmboxq = pmboxq;
James Smart7ad20aa2011-05-24 11:44:28 -04004971 dd_data->context_un.mbox.mb = (MAILBOX_t *)pmbx;
James Smart7a470272010-03-15 11:25:20 -04004972 dd_data->context_un.mbox.ext = ext;
4973 dd_data->context_un.mbox.mbOffset = mbox_req->mbOffset;
4974 dd_data->context_un.mbox.inExtWLen = mbox_req->inExtWLen;
James Smartc7495932010-04-06 15:05:28 -04004975 dd_data->context_un.mbox.outExtWLen = mbox_req->outExtWLen;
James Smart3b5dd522010-01-26 23:10:15 -05004976 job->dd_data = dd_data;
James Smart7a470272010-03-15 11:25:20 -04004977
4978 if ((vport->fc_flag & FC_OFFLINE_MODE) ||
4979 (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))) {
4980 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
4981 if (rc != MBX_SUCCESS) {
4982 rc = (rc == MBX_TIMEOUT) ? -ETIME : -ENODEV;
4983 goto job_done;
4984 }
4985
4986 /* job finished, copy the data */
James Smart7ad20aa2011-05-24 11:44:28 -04004987 memcpy(pmbx, pmb, sizeof(*pmb));
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01004988 bsg_reply->reply_payload_rcv_len =
James Smart7a470272010-03-15 11:25:20 -04004989 sg_copy_from_buffer(job->reply_payload.sg_list,
James Smart7ad20aa2011-05-24 11:44:28 -04004990 job->reply_payload.sg_cnt,
4991 pmbx, size);
James Smart7a470272010-03-15 11:25:20 -04004992 /* not waiting mbox already done */
4993 rc = 0;
4994 goto job_done;
James Smart3b5dd522010-01-26 23:10:15 -05004995 }
4996
James Smart7a470272010-03-15 11:25:20 -04004997 rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
4998 if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY))
4999 return 1; /* job started */
5000
5001job_done:
5002 /* common exit for error or job completed inline */
James Smart7a470272010-03-15 11:25:20 -04005003 if (pmboxq)
5004 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart7ad20aa2011-05-24 11:44:28 -04005005 lpfc_bsg_dma_page_free(phba, dmabuf);
James Smart7a470272010-03-15 11:25:20 -04005006 kfree(dd_data);
5007
James Smart7ad20aa2011-05-24 11:44:28 -04005008job_cont:
James Smart7a470272010-03-15 11:25:20 -04005009 return rc;
James Smart3b5dd522010-01-26 23:10:15 -05005010}
5011
5012/**
5013 * lpfc_bsg_mbox_cmd - process an fc bsg LPFC_BSG_VENDOR_MBOX command
5014 * @job: MBOX fc_bsg_job for LPFC_BSG_VENDOR_MBOX.
5015 **/
5016static int
Johannes Thumshirn75cc8cf2016-11-17 10:31:19 +01005017lpfc_bsg_mbox_cmd(struct bsg_job *job)
James Smart3b5dd522010-01-26 23:10:15 -05005018{
Johannes Thumshirncd21c602016-11-17 10:31:14 +01005019 struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01005020 struct fc_bsg_request *bsg_request = job->request;
5021 struct fc_bsg_reply *bsg_reply = job->reply;
James Smart3b5dd522010-01-26 23:10:15 -05005022 struct lpfc_hba *phba = vport->phba;
James Smart7ad20aa2011-05-24 11:44:28 -04005023 struct dfc_mbox_req *mbox_req;
James Smart3b5dd522010-01-26 23:10:15 -05005024 int rc = 0;
5025
James Smart7ad20aa2011-05-24 11:44:28 -04005026 /* mix-and-match backward compatibility */
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01005027 bsg_reply->reply_payload_rcv_len = 0;
James Smart3b5dd522010-01-26 23:10:15 -05005028 if (job->request_len <
5029 sizeof(struct fc_bsg_request) + sizeof(struct dfc_mbox_req)) {
James Smart7ad20aa2011-05-24 11:44:28 -04005030 lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
Masanari Iida1051e9b2013-03-31 01:23:50 +09005031 "2737 Mix-and-match backward compatibility "
James Smart7ad20aa2011-05-24 11:44:28 -04005032 "between MBOX_REQ old size:%d and "
5033 "new request size:%d\n",
5034 (int)(job->request_len -
5035 sizeof(struct fc_bsg_request)),
5036 (int)sizeof(struct dfc_mbox_req));
5037 mbox_req = (struct dfc_mbox_req *)
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01005038 bsg_request->rqst_data.h_vendor.vendor_cmd;
James Smart7ad20aa2011-05-24 11:44:28 -04005039 mbox_req->extMboxTag = 0;
5040 mbox_req->extSeqNum = 0;
James Smart3b5dd522010-01-26 23:10:15 -05005041 }
5042
5043 rc = lpfc_bsg_issue_mbox(phba, job, vport);
5044
James Smart3b5dd522010-01-26 23:10:15 -05005045 if (rc == 0) {
5046 /* job done */
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01005047 bsg_reply->result = 0;
James Smart3b5dd522010-01-26 23:10:15 -05005048 job->dd_data = NULL;
Johannes Thumshirn06548162016-11-17 10:31:22 +01005049 bsg_job_done(job, bsg_reply->result,
Johannes Thumshirn1abaede2016-11-17 10:31:13 +01005050 bsg_reply->reply_payload_rcv_len);
James Smart3b5dd522010-01-26 23:10:15 -05005051 } else if (rc == 1)
5052 /* job submitted, will complete later*/
5053 rc = 0; /* return zero, no error */
5054 else {
5055 /* some error occurred */
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01005056 bsg_reply->result = rc;
James Smart3b5dd522010-01-26 23:10:15 -05005057 job->dd_data = NULL;
5058 }
5059
5060 return rc;
5061}
5062
5063/**
James Smarte2aed292010-02-26 14:15:00 -05005064 * lpfc_bsg_menlo_cmd_cmp - lpfc_menlo_cmd completion handler
5065 * @phba: Pointer to HBA context object.
5066 * @cmdiocbq: Pointer to command iocb.
5067 * @rspiocbq: Pointer to response iocb.
5068 *
5069 * This function is the completion handler for iocbs issued using
5070 * lpfc_menlo_cmd function. This function is called by the
5071 * ring event handler function without any lock held. This function
5072 * can be called from both worker thread context and interrupt
5073 * context. This function also can be called from another thread which
5074 * cleans up the SLI layer objects.
5075 * This function copies the contents of the response iocb to the
5076 * response iocb memory object provided by the caller of
5077 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
5078 * sleeps for the iocb completion.
5079 **/
5080static void
5081lpfc_bsg_menlo_cmd_cmp(struct lpfc_hba *phba,
5082 struct lpfc_iocbq *cmdiocbq,
5083 struct lpfc_iocbq *rspiocbq)
5084{
5085 struct bsg_job_data *dd_data;
Johannes Thumshirn75cc8cf2016-11-17 10:31:19 +01005086 struct bsg_job *job;
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01005087 struct fc_bsg_reply *bsg_reply;
James Smarte2aed292010-02-26 14:15:00 -05005088 IOCB_t *rsp;
James Smarta33c4f72013-03-01 16:36:00 -05005089 struct lpfc_dmabuf *bmp, *cmp, *rmp;
James Smarte2aed292010-02-26 14:15:00 -05005090 struct lpfc_bsg_menlo *menlo;
5091 unsigned long flags;
5092 struct menlo_response *menlo_resp;
James Smarta33c4f72013-03-01 16:36:00 -05005093 unsigned int rsp_size;
James Smarte2aed292010-02-26 14:15:00 -05005094 int rc = 0;
5095
James Smarte2aed292010-02-26 14:15:00 -05005096 dd_data = cmdiocbq->context1;
James Smarta33c4f72013-03-01 16:36:00 -05005097 cmp = cmdiocbq->context2;
5098 bmp = cmdiocbq->context3;
James Smarte2aed292010-02-26 14:15:00 -05005099 menlo = &dd_data->context_un.menlo;
James Smarta33c4f72013-03-01 16:36:00 -05005100 rmp = menlo->rmp;
James Smarte2aed292010-02-26 14:15:00 -05005101 rsp = &rspiocbq->iocb;
5102
James Smarta33c4f72013-03-01 16:36:00 -05005103 /* Determine if job has been aborted */
5104 spin_lock_irqsave(&phba->ct_ev_lock, flags);
5105 job = dd_data->set_job;
5106 if (job) {
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01005107 bsg_reply = job->reply;
James Smarta33c4f72013-03-01 16:36:00 -05005108 /* Prevent timeout handling from trying to abort job */
5109 job->dd_data = NULL;
5110 }
5111 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smarte2aed292010-02-26 14:15:00 -05005112
James Smarta33c4f72013-03-01 16:36:00 -05005113 /* Copy the job data or set the failing status for the job */
5114
5115 if (job) {
5116 /* always return the xri, this would be used in the case
5117 * of a menlo download to allow the data to be sent as a
5118 * continuation of the exchange.
5119 */
5120
5121 menlo_resp = (struct menlo_response *)
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01005122 bsg_reply->reply_data.vendor_reply.vendor_rsp;
James Smarta33c4f72013-03-01 16:36:00 -05005123 menlo_resp->xri = rsp->ulpContext;
5124 if (rsp->ulpStatus) {
5125 if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
5126 switch (rsp->un.ulpWord[4] & IOERR_PARAM_MASK) {
5127 case IOERR_SEQUENCE_TIMEOUT:
5128 rc = -ETIMEDOUT;
5129 break;
5130 case IOERR_INVALID_RPI:
5131 rc = -EFAULT;
5132 break;
5133 default:
5134 rc = -EACCES;
5135 break;
5136 }
5137 } else {
James Smarte2aed292010-02-26 14:15:00 -05005138 rc = -EACCES;
James Smarte2aed292010-02-26 14:15:00 -05005139 }
James Smarta33c4f72013-03-01 16:36:00 -05005140 } else {
5141 rsp_size = rsp->un.genreq64.bdl.bdeSize;
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01005142 bsg_reply->reply_payload_rcv_len =
James Smarta33c4f72013-03-01 16:36:00 -05005143 lpfc_bsg_copy_data(rmp, &job->reply_payload,
5144 rsp_size, 0);
5145 }
James Smarte2aed292010-02-26 14:15:00 -05005146
James Smarta33c4f72013-03-01 16:36:00 -05005147 }
5148
James Smarte2aed292010-02-26 14:15:00 -05005149 lpfc_sli_release_iocbq(phba, cmdiocbq);
James Smarta33c4f72013-03-01 16:36:00 -05005150 lpfc_free_bsg_buffers(phba, cmp);
5151 lpfc_free_bsg_buffers(phba, rmp);
5152 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
James Smarte2aed292010-02-26 14:15:00 -05005153 kfree(bmp);
5154 kfree(dd_data);
James Smarta33c4f72013-03-01 16:36:00 -05005155
5156 /* Complete the job if active */
5157
5158 if (job) {
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01005159 bsg_reply->result = rc;
Johannes Thumshirn06548162016-11-17 10:31:22 +01005160 bsg_job_done(job, bsg_reply->result,
Johannes Thumshirn1abaede2016-11-17 10:31:13 +01005161 bsg_reply->reply_payload_rcv_len);
James Smarta33c4f72013-03-01 16:36:00 -05005162 }
5163
James Smarte2aed292010-02-26 14:15:00 -05005164 return;
5165}
5166
5167/**
5168 * lpfc_menlo_cmd - send an ioctl for menlo hardware
5169 * @job: fc_bsg_job to handle
5170 *
5171 * This function issues a gen request 64 CR ioctl for all menlo cmd requests,
5172 * all the command completions will return the xri for the command.
5173 * For menlo data requests a gen request 64 CX is used to continue the exchange
5174 * supplied in the menlo request header xri field.
5175 **/
5176static int
Johannes Thumshirn75cc8cf2016-11-17 10:31:19 +01005177lpfc_menlo_cmd(struct bsg_job *job)
James Smarte2aed292010-02-26 14:15:00 -05005178{
Johannes Thumshirncd21c602016-11-17 10:31:14 +01005179 struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01005180 struct fc_bsg_request *bsg_request = job->request;
5181 struct fc_bsg_reply *bsg_reply = job->reply;
James Smarte2aed292010-02-26 14:15:00 -05005182 struct lpfc_hba *phba = vport->phba;
James Smarta33c4f72013-03-01 16:36:00 -05005183 struct lpfc_iocbq *cmdiocbq;
5184 IOCB_t *cmd;
James Smarte2aed292010-02-26 14:15:00 -05005185 int rc = 0;
5186 struct menlo_command *menlo_cmd;
James Smarta33c4f72013-03-01 16:36:00 -05005187 struct lpfc_dmabuf *bmp = NULL, *cmp = NULL, *rmp = NULL;
James Smarte2aed292010-02-26 14:15:00 -05005188 int request_nseg;
5189 int reply_nseg;
James Smarte2aed292010-02-26 14:15:00 -05005190 struct bsg_job_data *dd_data;
5191 struct ulp_bde64 *bpl = NULL;
5192
5193 /* in case no data is returned return just the return code */
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01005194 bsg_reply->reply_payload_rcv_len = 0;
James Smarte2aed292010-02-26 14:15:00 -05005195
5196 if (job->request_len <
5197 sizeof(struct fc_bsg_request) +
5198 sizeof(struct menlo_command)) {
5199 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5200 "2784 Received MENLO_CMD request below "
5201 "minimum size\n");
5202 rc = -ERANGE;
5203 goto no_dd_data;
5204 }
5205
5206 if (job->reply_len <
5207 sizeof(struct fc_bsg_request) + sizeof(struct menlo_response)) {
5208 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5209 "2785 Received MENLO_CMD reply below "
5210 "minimum size\n");
5211 rc = -ERANGE;
5212 goto no_dd_data;
5213 }
5214
5215 if (!(phba->menlo_flag & HBA_MENLO_SUPPORT)) {
5216 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5217 "2786 Adapter does not support menlo "
5218 "commands\n");
5219 rc = -EPERM;
5220 goto no_dd_data;
5221 }
5222
5223 menlo_cmd = (struct menlo_command *)
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01005224 bsg_request->rqst_data.h_vendor.vendor_cmd;
James Smarte2aed292010-02-26 14:15:00 -05005225
James Smarte2aed292010-02-26 14:15:00 -05005226 /* allocate our bsg tracking structure */
5227 dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
5228 if (!dd_data) {
5229 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5230 "2787 Failed allocation of dd_data\n");
5231 rc = -ENOMEM;
5232 goto no_dd_data;
5233 }
5234
5235 bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5236 if (!bmp) {
5237 rc = -ENOMEM;
5238 goto free_dd;
5239 }
5240
James Smarta33c4f72013-03-01 16:36:00 -05005241 bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
5242 if (!bmp->virt) {
James Smarte2aed292010-02-26 14:15:00 -05005243 rc = -ENOMEM;
5244 goto free_bmp;
5245 }
5246
James Smarte2aed292010-02-26 14:15:00 -05005247 INIT_LIST_HEAD(&bmp->list);
James Smarta33c4f72013-03-01 16:36:00 -05005248
5249 bpl = (struct ulp_bde64 *)bmp->virt;
5250 request_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64);
5251 cmp = lpfc_alloc_bsg_buffers(phba, job->request_payload.payload_len,
5252 1, bpl, &request_nseg);
5253 if (!cmp) {
5254 rc = -ENOMEM;
5255 goto free_bmp;
5256 }
5257 lpfc_bsg_copy_data(cmp, &job->request_payload,
5258 job->request_payload.payload_len, 1);
5259
5260 bpl += request_nseg;
5261 reply_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64) - request_nseg;
5262 rmp = lpfc_alloc_bsg_buffers(phba, job->reply_payload.payload_len, 0,
5263 bpl, &reply_nseg);
5264 if (!rmp) {
5265 rc = -ENOMEM;
5266 goto free_cmp;
James Smarte2aed292010-02-26 14:15:00 -05005267 }
5268
James Smarta33c4f72013-03-01 16:36:00 -05005269 cmdiocbq = lpfc_sli_get_iocbq(phba);
5270 if (!cmdiocbq) {
5271 rc = -ENOMEM;
5272 goto free_rmp;
James Smarte2aed292010-02-26 14:15:00 -05005273 }
5274
5275 cmd = &cmdiocbq->iocb;
5276 cmd->un.genreq64.bdl.ulpIoTag32 = 0;
5277 cmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
5278 cmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
5279 cmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
5280 cmd->un.genreq64.bdl.bdeSize =
5281 (request_nseg + reply_nseg) * sizeof(struct ulp_bde64);
5282 cmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
5283 cmd->un.genreq64.w5.hcsw.Dfctl = 0;
5284 cmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CMD;
5285 cmd->un.genreq64.w5.hcsw.Type = MENLO_TRANSPORT_TYPE; /* 0xfe */
5286 cmd->ulpBdeCount = 1;
5287 cmd->ulpClass = CLASS3;
5288 cmd->ulpOwner = OWN_CHIP;
5289 cmd->ulpLe = 1; /* Limited Edition */
5290 cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
5291 cmdiocbq->vport = phba->pport;
5292 /* We want the firmware to timeout before we do */
5293 cmd->ulpTimeout = MENLO_TIMEOUT - 5;
James Smarte2aed292010-02-26 14:15:00 -05005294 cmdiocbq->iocb_cmpl = lpfc_bsg_menlo_cmd_cmp;
5295 cmdiocbq->context1 = dd_data;
James Smarta33c4f72013-03-01 16:36:00 -05005296 cmdiocbq->context2 = cmp;
5297 cmdiocbq->context3 = bmp;
James Smarte2aed292010-02-26 14:15:00 -05005298 if (menlo_cmd->cmd == LPFC_BSG_VENDOR_MENLO_CMD) {
5299 cmd->ulpCommand = CMD_GEN_REQUEST64_CR;
5300 cmd->ulpPU = MENLO_PU; /* 3 */
5301 cmd->un.ulpWord[4] = MENLO_DID; /* 0x0000FC0E */
5302 cmd->ulpContext = MENLO_CONTEXT; /* 0 */
5303 } else {
5304 cmd->ulpCommand = CMD_GEN_REQUEST64_CX;
5305 cmd->ulpPU = 1;
5306 cmd->un.ulpWord[4] = 0;
5307 cmd->ulpContext = menlo_cmd->xri;
5308 }
5309
5310 dd_data->type = TYPE_MENLO;
James Smarta33c4f72013-03-01 16:36:00 -05005311 dd_data->set_job = job;
James Smarte2aed292010-02-26 14:15:00 -05005312 dd_data->context_un.menlo.cmdiocbq = cmdiocbq;
James Smarta33c4f72013-03-01 16:36:00 -05005313 dd_data->context_un.menlo.rmp = rmp;
5314 job->dd_data = dd_data;
James Smarte2aed292010-02-26 14:15:00 -05005315
5316 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq,
5317 MENLO_TIMEOUT - 5);
5318 if (rc == IOCB_SUCCESS)
5319 return 0; /* done for now */
5320
James Smarte2aed292010-02-26 14:15:00 -05005321 lpfc_sli_release_iocbq(phba, cmdiocbq);
James Smarta33c4f72013-03-01 16:36:00 -05005322
5323free_rmp:
5324 lpfc_free_bsg_buffers(phba, rmp);
5325free_cmp:
5326 lpfc_free_bsg_buffers(phba, cmp);
James Smarte2aed292010-02-26 14:15:00 -05005327free_bmp:
James Smarta33c4f72013-03-01 16:36:00 -05005328 if (bmp->virt)
5329 lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
James Smarte2aed292010-02-26 14:15:00 -05005330 kfree(bmp);
5331free_dd:
5332 kfree(dd_data);
5333no_dd_data:
5334 /* make error code available to userspace */
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01005335 bsg_reply->result = rc;
James Smarte2aed292010-02-26 14:15:00 -05005336 job->dd_data = NULL;
5337 return rc;
5338}
James Smartb6e3b9c2011-04-16 11:03:43 -04005339
James Smartc6918162016-10-13 15:06:16 -07005340static int
Johannes Thumshirn75cc8cf2016-11-17 10:31:19 +01005341lpfc_forced_link_speed(struct bsg_job *job)
James Smartc6918162016-10-13 15:06:16 -07005342{
Johannes Thumshirncd21c602016-11-17 10:31:14 +01005343 struct Scsi_Host *shost = fc_bsg_to_shost(job);
James Smartc6918162016-10-13 15:06:16 -07005344 struct lpfc_vport *vport = shost_priv(shost);
5345 struct lpfc_hba *phba = vport->phba;
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01005346 struct fc_bsg_reply *bsg_reply = job->reply;
James Smartc6918162016-10-13 15:06:16 -07005347 struct forced_link_speed_support_reply *forced_reply;
5348 int rc = 0;
5349
5350 if (job->request_len <
5351 sizeof(struct fc_bsg_request) +
5352 sizeof(struct get_forced_link_speed_support)) {
5353 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5354 "0048 Received FORCED_LINK_SPEED request "
5355 "below minimum size\n");
5356 rc = -EINVAL;
5357 goto job_error;
5358 }
5359
5360 forced_reply = (struct forced_link_speed_support_reply *)
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01005361 bsg_reply->reply_data.vendor_reply.vendor_rsp;
James Smartc6918162016-10-13 15:06:16 -07005362
5363 if (job->reply_len <
5364 sizeof(struct fc_bsg_request) +
5365 sizeof(struct forced_link_speed_support_reply)) {
5366 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5367 "0049 Received FORCED_LINK_SPEED reply below "
5368 "minimum size\n");
5369 rc = -EINVAL;
5370 goto job_error;
5371 }
5372
5373 forced_reply->supported = (phba->hba_flag & HBA_FORCED_LINK_SPEED)
5374 ? LPFC_FORCED_LINK_SPEED_SUPPORTED
5375 : LPFC_FORCED_LINK_SPEED_NOT_SUPPORTED;
5376job_error:
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01005377 bsg_reply->result = rc;
James Smartc6918162016-10-13 15:06:16 -07005378 if (rc == 0)
Johannes Thumshirn06548162016-11-17 10:31:22 +01005379 bsg_job_done(job, bsg_reply->result,
Johannes Thumshirn1abaede2016-11-17 10:31:13 +01005380 bsg_reply->reply_payload_rcv_len);
James Smartc6918162016-10-13 15:06:16 -07005381 return rc;
5382}
5383
James Smarte2aed292010-02-26 14:15:00 -05005384/**
James Smartd2cc9bc2018-09-10 10:30:50 -07005385 * lpfc_check_fwlog_support: Check FW log support on the adapter
5386 * @phba: Pointer to HBA context object.
5387 *
5388 * Check if FW Logging support by the adapter
5389 **/
5390int
5391lpfc_check_fwlog_support(struct lpfc_hba *phba)
5392{
5393 struct lpfc_ras_fwlog *ras_fwlog = NULL;
5394
5395 ras_fwlog = &phba->ras_fwlog;
5396
5397 if (ras_fwlog->ras_hwsupport == false)
5398 return -EACCES;
5399 else if (ras_fwlog->ras_enabled == false)
5400 return -EPERM;
5401 else
5402 return 0;
5403}
5404
5405/**
5406 * lpfc_bsg_get_ras_config: Get RAS configuration settings
5407 * @job: fc_bsg_job to handle
5408 *
5409 * Get RAS configuration values set.
5410 **/
5411static int
5412lpfc_bsg_get_ras_config(struct bsg_job *job)
5413{
5414 struct Scsi_Host *shost = fc_bsg_to_shost(job);
5415 struct lpfc_vport *vport = shost_priv(shost);
5416 struct fc_bsg_reply *bsg_reply = job->reply;
5417 struct lpfc_hba *phba = vport->phba;
5418 struct lpfc_bsg_get_ras_config_reply *ras_reply;
5419 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
5420 int rc = 0;
5421
5422 if (job->request_len <
5423 sizeof(struct fc_bsg_request) +
5424 sizeof(struct lpfc_bsg_ras_req)) {
5425 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
James Smartcb349902018-11-29 16:09:27 -08005426 "6192 FW_LOG request received "
James Smartd2cc9bc2018-09-10 10:30:50 -07005427 "below minimum size\n");
5428 rc = -EINVAL;
5429 goto ras_job_error;
5430 }
5431
5432 /* Check FW log status */
5433 rc = lpfc_check_fwlog_support(phba);
James Smartcb349902018-11-29 16:09:27 -08005434 if (rc)
James Smartd2cc9bc2018-09-10 10:30:50 -07005435 goto ras_job_error;
5436
5437 ras_reply = (struct lpfc_bsg_get_ras_config_reply *)
5438 bsg_reply->reply_data.vendor_reply.vendor_rsp;
5439
5440 /* Current logging state */
5441 if (ras_fwlog->ras_active == true)
5442 ras_reply->state = LPFC_RASLOG_STATE_RUNNING;
5443 else
5444 ras_reply->state = LPFC_RASLOG_STATE_STOPPED;
5445
5446 ras_reply->log_level = phba->ras_fwlog.fw_loglevel;
5447 ras_reply->log_buff_sz = phba->cfg_ras_fwlog_buffsize;
5448
5449ras_job_error:
5450 /* make error code available to userspace */
5451 bsg_reply->result = rc;
5452
5453 /* complete the job back to userspace */
5454 bsg_job_done(job, bsg_reply->result, bsg_reply->reply_payload_rcv_len);
5455 return rc;
5456}
5457
5458/**
James Smartd2cc9bc2018-09-10 10:30:50 -07005459 * lpfc_bsg_set_ras_config: Set FW logging parameters
5460 * @job: fc_bsg_job to handle
5461 *
5462 * Set log-level parameters for FW-logging in host memory
5463 **/
5464static int
5465lpfc_bsg_set_ras_config(struct bsg_job *job)
5466{
5467 struct Scsi_Host *shost = fc_bsg_to_shost(job);
5468 struct lpfc_vport *vport = shost_priv(shost);
5469 struct lpfc_hba *phba = vport->phba;
5470 struct lpfc_bsg_set_ras_config_req *ras_req;
5471 struct fc_bsg_request *bsg_request = job->request;
5472 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
5473 struct fc_bsg_reply *bsg_reply = job->reply;
5474 uint8_t action = 0, log_level = 0;
James Smart191e2f72018-10-23 13:41:05 -07005475 int rc = 0, action_status = 0;
James Smartd2cc9bc2018-09-10 10:30:50 -07005476
5477 if (job->request_len <
5478 sizeof(struct fc_bsg_request) +
5479 sizeof(struct lpfc_bsg_set_ras_config_req)) {
5480 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
5481 "6182 Received RAS_LOG request "
5482 "below minimum size\n");
5483 rc = -EINVAL;
5484 goto ras_job_error;
5485 }
5486
5487 /* Check FW log status */
5488 rc = lpfc_check_fwlog_support(phba);
James Smartcb349902018-11-29 16:09:27 -08005489 if (rc)
James Smartd2cc9bc2018-09-10 10:30:50 -07005490 goto ras_job_error;
5491
5492 ras_req = (struct lpfc_bsg_set_ras_config_req *)
5493 bsg_request->rqst_data.h_vendor.vendor_cmd;
5494 action = ras_req->action;
5495 log_level = ras_req->log_level;
5496
5497 if (action == LPFC_RASACTION_STOP_LOGGING) {
5498 /* Check if already disabled */
5499 if (ras_fwlog->ras_active == false) {
5500 rc = -ESRCH;
5501 goto ras_job_error;
5502 }
5503
5504 /* Disable logging */
5505 lpfc_ras_stop_fwlog(phba);
5506 } else {
5507 /*action = LPFC_RASACTION_START_LOGGING*/
James Smart191e2f72018-10-23 13:41:05 -07005508
5509 /* Even though FW-logging is active re-initialize
5510 * FW-logging with new log-level. Return status
5511 * "Logging already Running" to caller.
5512 **/
5513 if (ras_fwlog->ras_active)
5514 action_status = -EINPROGRESS;
James Smartd2cc9bc2018-09-10 10:30:50 -07005515
5516 /* Enable logging */
5517 rc = lpfc_sli4_ras_fwlog_init(phba, log_level,
5518 LPFC_RAS_ENABLE_LOGGING);
James Smart191e2f72018-10-23 13:41:05 -07005519 if (rc) {
James Smartd2cc9bc2018-09-10 10:30:50 -07005520 rc = -EINVAL;
James Smart191e2f72018-10-23 13:41:05 -07005521 goto ras_job_error;
5522 }
5523
5524 /* Check if FW-logging is re-initialized */
5525 if (action_status == -EINPROGRESS)
5526 rc = action_status;
James Smartd2cc9bc2018-09-10 10:30:50 -07005527 }
5528ras_job_error:
5529 /* make error code available to userspace */
5530 bsg_reply->result = rc;
5531
5532 /* complete the job back to userspace */
5533 bsg_job_done(job, bsg_reply->result,
5534 bsg_reply->reply_payload_rcv_len);
5535
5536 return rc;
5537}
5538
5539/**
5540 * lpfc_bsg_get_ras_lwpd: Get log write position data
5541 * @job: fc_bsg_job to handle
5542 *
5543 * Get Offset/Wrap count of the log message written
5544 * in host memory
5545 **/
5546static int
5547lpfc_bsg_get_ras_lwpd(struct bsg_job *job)
5548{
5549 struct Scsi_Host *shost = fc_bsg_to_shost(job);
5550 struct lpfc_vport *vport = shost_priv(shost);
5551 struct lpfc_bsg_get_ras_lwpd *ras_reply;
5552 struct lpfc_hba *phba = vport->phba;
5553 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
5554 struct fc_bsg_reply *bsg_reply = job->reply;
James Smart191e2f72018-10-23 13:41:05 -07005555 u32 *lwpd_ptr = NULL;
James Smartd2cc9bc2018-09-10 10:30:50 -07005556 int rc = 0;
5557
5558 rc = lpfc_check_fwlog_support(phba);
James Smartcb349902018-11-29 16:09:27 -08005559 if (rc)
James Smartd2cc9bc2018-09-10 10:30:50 -07005560 goto ras_job_error;
5561
5562 if (job->request_len <
5563 sizeof(struct fc_bsg_request) +
5564 sizeof(struct lpfc_bsg_ras_req)) {
5565 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
5566 "6183 Received RAS_LOG request "
5567 "below minimum size\n");
5568 rc = -EINVAL;
5569 goto ras_job_error;
5570 }
5571
5572 ras_reply = (struct lpfc_bsg_get_ras_lwpd *)
5573 bsg_reply->reply_data.vendor_reply.vendor_rsp;
5574
James Smartcb349902018-11-29 16:09:27 -08005575 if (!ras_fwlog->lwpd.virt) {
5576 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
5577 "6193 Restart FW Logging\n");
James Smart1165a5c2018-11-29 16:09:39 -08005578 rc = -EINVAL;
5579 goto ras_job_error;
James Smartcb349902018-11-29 16:09:27 -08005580 }
5581
James Smart191e2f72018-10-23 13:41:05 -07005582 /* Get lwpd offset */
5583 lwpd_ptr = (uint32_t *)(ras_fwlog->lwpd.virt);
5584 ras_reply->offset = be32_to_cpu(*lwpd_ptr & 0xffffffff);
James Smartd2cc9bc2018-09-10 10:30:50 -07005585
James Smart191e2f72018-10-23 13:41:05 -07005586 /* Get wrap count */
5587 ras_reply->wrap_count = be32_to_cpu(*(++lwpd_ptr) & 0xffffffff);
James Smartd2cc9bc2018-09-10 10:30:50 -07005588
5589ras_job_error:
5590 /* make error code available to userspace */
5591 bsg_reply->result = rc;
5592
5593 /* complete the job back to userspace */
5594 bsg_job_done(job, bsg_reply->result, bsg_reply->reply_payload_rcv_len);
5595
5596 return rc;
5597}
5598
5599/**
5600 * lpfc_bsg_get_ras_fwlog: Read FW log
5601 * @job: fc_bsg_job to handle
5602 *
5603 * Copy the FW log into the passed buffer.
5604 **/
5605static int
5606lpfc_bsg_get_ras_fwlog(struct bsg_job *job)
5607{
5608 struct Scsi_Host *shost = fc_bsg_to_shost(job);
5609 struct lpfc_vport *vport = shost_priv(shost);
5610 struct lpfc_hba *phba = vport->phba;
5611 struct fc_bsg_request *bsg_request = job->request;
5612 struct fc_bsg_reply *bsg_reply = job->reply;
5613 struct lpfc_bsg_get_fwlog_req *ras_req;
James Smart191e2f72018-10-23 13:41:05 -07005614 u32 rd_offset, rd_index, offset;
5615 void *src, *fwlog_buff;
James Smartd2cc9bc2018-09-10 10:30:50 -07005616 struct lpfc_ras_fwlog *ras_fwlog = NULL;
5617 struct lpfc_dmabuf *dmabuf, *next;
5618 int rc = 0;
5619
5620 ras_fwlog = &phba->ras_fwlog;
5621
5622 rc = lpfc_check_fwlog_support(phba);
James Smartcb349902018-11-29 16:09:27 -08005623 if (rc)
James Smartd2cc9bc2018-09-10 10:30:50 -07005624 goto ras_job_error;
5625
5626 /* Logging to be stopped before reading */
5627 if (ras_fwlog->ras_active == true) {
5628 rc = -EINPROGRESS;
5629 goto ras_job_error;
5630 }
5631
5632 if (job->request_len <
5633 sizeof(struct fc_bsg_request) +
5634 sizeof(struct lpfc_bsg_get_fwlog_req)) {
5635 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
5636 "6184 Received RAS_LOG request "
5637 "below minimum size\n");
5638 rc = -EINVAL;
5639 goto ras_job_error;
5640 }
5641
5642 ras_req = (struct lpfc_bsg_get_fwlog_req *)
5643 bsg_request->rqst_data.h_vendor.vendor_cmd;
5644 rd_offset = ras_req->read_offset;
5645
5646 /* Allocate memory to read fw log*/
5647 fwlog_buff = vmalloc(ras_req->read_size);
5648 if (!fwlog_buff) {
5649 rc = -ENOMEM;
5650 goto ras_job_error;
5651 }
5652
5653 rd_index = (rd_offset / LPFC_RAS_MAX_ENTRY_SIZE);
5654 offset = (rd_offset % LPFC_RAS_MAX_ENTRY_SIZE);
James Smartd2cc9bc2018-09-10 10:30:50 -07005655
5656 list_for_each_entry_safe(dmabuf, next,
5657 &ras_fwlog->fwlog_buff_list, list) {
5658
5659 if (dmabuf->buffer_tag < rd_index)
5660 continue;
5661
James Smartd2cc9bc2018-09-10 10:30:50 -07005662 src = dmabuf->virt + offset;
James Smart191e2f72018-10-23 13:41:05 -07005663 memcpy(fwlog_buff, src, ras_req->read_size);
5664 break;
James Smartd2cc9bc2018-09-10 10:30:50 -07005665 }
5666
5667 bsg_reply->reply_payload_rcv_len =
5668 sg_copy_from_buffer(job->reply_payload.sg_list,
5669 job->reply_payload.sg_cnt,
5670 fwlog_buff, ras_req->read_size);
5671
5672 vfree(fwlog_buff);
5673
5674ras_job_error:
5675 bsg_reply->result = rc;
5676 bsg_job_done(job, bsg_reply->result, bsg_reply->reply_payload_rcv_len);
5677
5678 return rc;
5679}
5680
James Smart1dc5ec22018-10-23 13:41:11 -07005681static int
5682lpfc_get_trunk_info(struct bsg_job *job)
5683{
5684 struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
5685 struct lpfc_hba *phba = vport->phba;
5686 struct fc_bsg_reply *bsg_reply = job->reply;
5687 struct lpfc_trunk_info *event_reply;
5688 int rc = 0;
5689
5690 if (job->request_len <
5691 sizeof(struct fc_bsg_request) + sizeof(struct get_trunk_info_req)) {
5692 lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
5693 "2744 Received GET TRUNK _INFO request below "
5694 "minimum size\n");
5695 rc = -EINVAL;
5696 goto job_error;
5697 }
5698
5699 event_reply = (struct lpfc_trunk_info *)
5700 bsg_reply->reply_data.vendor_reply.vendor_rsp;
5701
5702 if (job->reply_len <
5703 sizeof(struct fc_bsg_request) + sizeof(struct lpfc_trunk_info)) {
5704 lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5705 "2728 Received GET TRUNK _INFO reply below "
5706 "minimum size\n");
5707 rc = -EINVAL;
5708 goto job_error;
5709 }
5710 if (event_reply == NULL) {
5711 rc = -EINVAL;
5712 goto job_error;
5713 }
5714
5715 bsg_bf_set(lpfc_trunk_info_link_status, event_reply,
5716 (phba->link_state >= LPFC_LINK_UP) ? 1 : 0);
5717
5718 bsg_bf_set(lpfc_trunk_info_trunk_active0, event_reply,
5719 (phba->trunk_link.link0.state == LPFC_LINK_UP) ? 1 : 0);
5720
5721 bsg_bf_set(lpfc_trunk_info_trunk_active1, event_reply,
5722 (phba->trunk_link.link1.state == LPFC_LINK_UP) ? 1 : 0);
5723
5724 bsg_bf_set(lpfc_trunk_info_trunk_active2, event_reply,
5725 (phba->trunk_link.link2.state == LPFC_LINK_UP) ? 1 : 0);
5726
5727 bsg_bf_set(lpfc_trunk_info_trunk_active3, event_reply,
5728 (phba->trunk_link.link3.state == LPFC_LINK_UP) ? 1 : 0);
5729
5730 bsg_bf_set(lpfc_trunk_info_trunk_config0, event_reply,
5731 bf_get(lpfc_conf_trunk_port0, &phba->sli4_hba));
5732
5733 bsg_bf_set(lpfc_trunk_info_trunk_config1, event_reply,
5734 bf_get(lpfc_conf_trunk_port1, &phba->sli4_hba));
5735
5736 bsg_bf_set(lpfc_trunk_info_trunk_config2, event_reply,
5737 bf_get(lpfc_conf_trunk_port2, &phba->sli4_hba));
5738
5739 bsg_bf_set(lpfc_trunk_info_trunk_config3, event_reply,
5740 bf_get(lpfc_conf_trunk_port3, &phba->sli4_hba));
5741
5742 event_reply->port_speed = phba->sli4_hba.link_state.speed / 1000;
5743 event_reply->logical_speed =
James Smartb8e6f132019-05-21 17:49:04 -07005744 phba->sli4_hba.link_state.logical_speed / 1000;
James Smart1dc5ec22018-10-23 13:41:11 -07005745job_error:
5746 bsg_reply->result = rc;
5747 bsg_job_done(job, bsg_reply->result,
5748 bsg_reply->reply_payload_rcv_len);
5749 return rc;
5750
5751}
James Smartd2cc9bc2018-09-10 10:30:50 -07005752
5753/**
James Smartf1c3b0f2009-07-19 10:01:32 -04005754 * lpfc_bsg_hst_vendor - process a vendor-specific fc_bsg_job
5755 * @job: fc_bsg_job to handle
James Smart3b5dd522010-01-26 23:10:15 -05005756 **/
James Smartf1c3b0f2009-07-19 10:01:32 -04005757static int
Johannes Thumshirn75cc8cf2016-11-17 10:31:19 +01005758lpfc_bsg_hst_vendor(struct bsg_job *job)
James Smartf1c3b0f2009-07-19 10:01:32 -04005759{
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01005760 struct fc_bsg_request *bsg_request = job->request;
5761 struct fc_bsg_reply *bsg_reply = job->reply;
5762 int command = bsg_request->rqst_data.h_vendor.vendor_cmd[0];
James Smart4cc0e562010-01-26 23:09:48 -05005763 int rc;
James Smartf1c3b0f2009-07-19 10:01:32 -04005764
5765 switch (command) {
5766 case LPFC_BSG_VENDOR_SET_CT_EVENT:
James Smart4cc0e562010-01-26 23:09:48 -05005767 rc = lpfc_bsg_hba_set_event(job);
James Smartf1c3b0f2009-07-19 10:01:32 -04005768 break;
James Smartf1c3b0f2009-07-19 10:01:32 -04005769 case LPFC_BSG_VENDOR_GET_CT_EVENT:
James Smart4cc0e562010-01-26 23:09:48 -05005770 rc = lpfc_bsg_hba_get_event(job);
James Smartf1c3b0f2009-07-19 10:01:32 -04005771 break;
James Smart3b5dd522010-01-26 23:10:15 -05005772 case LPFC_BSG_VENDOR_SEND_MGMT_RESP:
5773 rc = lpfc_bsg_send_mgmt_rsp(job);
5774 break;
5775 case LPFC_BSG_VENDOR_DIAG_MODE:
James Smart7ad20aa2011-05-24 11:44:28 -04005776 rc = lpfc_bsg_diag_loopback_mode(job);
James Smart3b5dd522010-01-26 23:10:15 -05005777 break;
James Smart7ad20aa2011-05-24 11:44:28 -04005778 case LPFC_BSG_VENDOR_DIAG_MODE_END:
5779 rc = lpfc_sli4_bsg_diag_mode_end(job);
5780 break;
5781 case LPFC_BSG_VENDOR_DIAG_RUN_LOOPBACK:
5782 rc = lpfc_bsg_diag_loopback_run(job);
5783 break;
5784 case LPFC_BSG_VENDOR_LINK_DIAG_TEST:
5785 rc = lpfc_sli4_bsg_link_diag_test(job);
James Smart3b5dd522010-01-26 23:10:15 -05005786 break;
5787 case LPFC_BSG_VENDOR_GET_MGMT_REV:
5788 rc = lpfc_bsg_get_dfc_rev(job);
5789 break;
5790 case LPFC_BSG_VENDOR_MBOX:
5791 rc = lpfc_bsg_mbox_cmd(job);
5792 break;
James Smarte2aed292010-02-26 14:15:00 -05005793 case LPFC_BSG_VENDOR_MENLO_CMD:
5794 case LPFC_BSG_VENDOR_MENLO_DATA:
5795 rc = lpfc_menlo_cmd(job);
5796 break;
James Smartc6918162016-10-13 15:06:16 -07005797 case LPFC_BSG_VENDOR_FORCED_LINK_SPEED:
5798 rc = lpfc_forced_link_speed(job);
5799 break;
James Smartd2cc9bc2018-09-10 10:30:50 -07005800 case LPFC_BSG_VENDOR_RAS_GET_LWPD:
5801 rc = lpfc_bsg_get_ras_lwpd(job);
5802 break;
5803 case LPFC_BSG_VENDOR_RAS_GET_FWLOG:
5804 rc = lpfc_bsg_get_ras_fwlog(job);
5805 break;
5806 case LPFC_BSG_VENDOR_RAS_GET_CONFIG:
5807 rc = lpfc_bsg_get_ras_config(job);
5808 break;
5809 case LPFC_BSG_VENDOR_RAS_SET_CONFIG:
5810 rc = lpfc_bsg_set_ras_config(job);
5811 break;
James Smart1dc5ec22018-10-23 13:41:11 -07005812 case LPFC_BSG_VENDOR_GET_TRUNK_INFO:
5813 rc = lpfc_get_trunk_info(job);
5814 break;
James Smartf1c3b0f2009-07-19 10:01:32 -04005815 default:
James Smart4cc0e562010-01-26 23:09:48 -05005816 rc = -EINVAL;
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01005817 bsg_reply->reply_payload_rcv_len = 0;
James Smart4cc0e562010-01-26 23:09:48 -05005818 /* make error code available to userspace */
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01005819 bsg_reply->result = rc;
James Smart4cc0e562010-01-26 23:09:48 -05005820 break;
James Smartf1c3b0f2009-07-19 10:01:32 -04005821 }
James Smart4cc0e562010-01-26 23:09:48 -05005822
5823 return rc;
James Smartf1c3b0f2009-07-19 10:01:32 -04005824}
5825
5826/**
5827 * lpfc_bsg_request - handle a bsg request from the FC transport
James Smartd2cc9bc2018-09-10 10:30:50 -07005828 * @job: bsg_job to handle
James Smart3b5dd522010-01-26 23:10:15 -05005829 **/
James Smartf1c3b0f2009-07-19 10:01:32 -04005830int
Johannes Thumshirn75cc8cf2016-11-17 10:31:19 +01005831lpfc_bsg_request(struct bsg_job *job)
James Smartf1c3b0f2009-07-19 10:01:32 -04005832{
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01005833 struct fc_bsg_request *bsg_request = job->request;
5834 struct fc_bsg_reply *bsg_reply = job->reply;
James Smartf1c3b0f2009-07-19 10:01:32 -04005835 uint32_t msgcode;
James Smart4cc0e562010-01-26 23:09:48 -05005836 int rc;
James Smartf1c3b0f2009-07-19 10:01:32 -04005837
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01005838 msgcode = bsg_request->msgcode;
James Smartf1c3b0f2009-07-19 10:01:32 -04005839 switch (msgcode) {
5840 case FC_BSG_HST_VENDOR:
5841 rc = lpfc_bsg_hst_vendor(job);
5842 break;
5843 case FC_BSG_RPT_ELS:
5844 rc = lpfc_bsg_rport_els(job);
5845 break;
5846 case FC_BSG_RPT_CT:
James Smart4cc0e562010-01-26 23:09:48 -05005847 rc = lpfc_bsg_send_mgmt_cmd(job);
James Smartf1c3b0f2009-07-19 10:01:32 -04005848 break;
5849 default:
James Smart4cc0e562010-01-26 23:09:48 -05005850 rc = -EINVAL;
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01005851 bsg_reply->reply_payload_rcv_len = 0;
James Smart4cc0e562010-01-26 23:09:48 -05005852 /* make error code available to userspace */
Johannes Thumshirn01e0e152016-11-17 10:31:12 +01005853 bsg_reply->result = rc;
James Smartf1c3b0f2009-07-19 10:01:32 -04005854 break;
5855 }
5856
5857 return rc;
5858}
5859
5860/**
5861 * lpfc_bsg_timeout - handle timeout of a bsg request from the FC transport
James Smartd2cc9bc2018-09-10 10:30:50 -07005862 * @job: bsg_job that has timed out
James Smartf1c3b0f2009-07-19 10:01:32 -04005863 *
5864 * This function just aborts the job's IOCB. The aborted IOCB will return to
5865 * the waiting function which will handle passing the error back to userspace
James Smart3b5dd522010-01-26 23:10:15 -05005866 **/
James Smartf1c3b0f2009-07-19 10:01:32 -04005867int
Johannes Thumshirn75cc8cf2016-11-17 10:31:19 +01005868lpfc_bsg_timeout(struct bsg_job *job)
James Smartf1c3b0f2009-07-19 10:01:32 -04005869{
Johannes Thumshirncd21c602016-11-17 10:31:14 +01005870 struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
James Smartf1c3b0f2009-07-19 10:01:32 -04005871 struct lpfc_hba *phba = vport->phba;
James Smart4cc0e562010-01-26 23:09:48 -05005872 struct lpfc_iocbq *cmdiocb;
James Smart895427b2017-02-12 13:52:30 -08005873 struct lpfc_sli_ring *pring;
James Smart4cc0e562010-01-26 23:09:48 -05005874 struct bsg_job_data *dd_data;
5875 unsigned long flags;
James Smarta33c4f72013-03-01 16:36:00 -05005876 int rc = 0;
5877 LIST_HEAD(completions);
5878 struct lpfc_iocbq *check_iocb, *next_iocb;
5879
James Smart895427b2017-02-12 13:52:30 -08005880 pring = lpfc_phba_elsring(phba);
Dick Kennedy1234a6d2017-09-29 17:34:29 -07005881 if (unlikely(!pring))
5882 return -EIO;
James Smart895427b2017-02-12 13:52:30 -08005883
James Smarta33c4f72013-03-01 16:36:00 -05005884 /* if job's driver data is NULL, the command completed or is in the
5885 * the process of completing. In this case, return status to request
5886 * so the timeout is retried. This avoids double completion issues
5887 * and the request will be pulled off the timer queue when the
5888 * command's completion handler executes. Otherwise, prevent the
5889 * command's completion handler from executing the job done callback
5890 * and continue processing to abort the outstanding the command.
5891 */
James Smartf1c3b0f2009-07-19 10:01:32 -04005892
James Smart4cc0e562010-01-26 23:09:48 -05005893 spin_lock_irqsave(&phba->ct_ev_lock, flags);
5894 dd_data = (struct bsg_job_data *)job->dd_data;
James Smarta33c4f72013-03-01 16:36:00 -05005895 if (dd_data) {
5896 dd_data->set_job = NULL;
5897 job->dd_data = NULL;
5898 } else {
James Smart4cc0e562010-01-26 23:09:48 -05005899 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smarta33c4f72013-03-01 16:36:00 -05005900 return -EAGAIN;
James Smart4cc0e562010-01-26 23:09:48 -05005901 }
5902
5903 switch (dd_data->type) {
5904 case TYPE_IOCB:
James Smarta33c4f72013-03-01 16:36:00 -05005905 /* Check to see if IOCB was issued to the port or not. If not,
5906 * remove it from the txq queue and call cancel iocbs.
5907 * Otherwise, call abort iotag
James Smart4cc0e562010-01-26 23:09:48 -05005908 */
James Smarta33c4f72013-03-01 16:36:00 -05005909 cmdiocb = dd_data->context_un.iocb.cmdiocbq;
James Smartb5a9b2d2013-09-06 12:19:45 -04005910 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5911
5912 spin_lock_irqsave(&phba->hbalock, flags);
5913 /* make sure the I/O abort window is still open */
James Smart1b8d11a2013-09-06 12:20:51 -04005914 if (!(cmdiocb->iocb_flag & LPFC_IO_CMD_OUTSTANDING)) {
James Smartb5a9b2d2013-09-06 12:19:45 -04005915 spin_unlock_irqrestore(&phba->hbalock, flags);
5916 return -EAGAIN;
5917 }
James Smarta33c4f72013-03-01 16:36:00 -05005918 list_for_each_entry_safe(check_iocb, next_iocb, &pring->txq,
5919 list) {
5920 if (check_iocb == cmdiocb) {
5921 list_move_tail(&check_iocb->list, &completions);
5922 break;
5923 }
5924 }
5925 if (list_empty(&completions))
5926 lpfc_sli_issue_abort_iotag(phba, pring, cmdiocb);
James Smartb5a9b2d2013-09-06 12:19:45 -04005927 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarta33c4f72013-03-01 16:36:00 -05005928 if (!list_empty(&completions)) {
5929 lpfc_sli_cancel_iocbs(phba, &completions,
5930 IOSTAT_LOCAL_REJECT,
5931 IOERR_SLI_ABORTED);
5932 }
James Smart4cc0e562010-01-26 23:09:48 -05005933 break;
James Smarta33c4f72013-03-01 16:36:00 -05005934
5935 case TYPE_EVT:
James Smart3b5dd522010-01-26 23:10:15 -05005936 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smarta33c4f72013-03-01 16:36:00 -05005937 break;
5938
5939 case TYPE_MBOX:
5940 /* Update the ext buf ctx state if needed */
5941
James Smart7ad20aa2011-05-24 11:44:28 -04005942 if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_PORT)
5943 phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_ABTS;
James Smarta33c4f72013-03-01 16:36:00 -05005944 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
James Smart3b5dd522010-01-26 23:10:15 -05005945 break;
James Smarte2aed292010-02-26 14:15:00 -05005946 case TYPE_MENLO:
James Smarta33c4f72013-03-01 16:36:00 -05005947 /* Check to see if IOCB was issued to the port or not. If not,
5948 * remove it from the txq queue and call cancel iocbs.
5949 * Otherwise, call abort iotag.
5950 */
James Smarta33c4f72013-03-01 16:36:00 -05005951 cmdiocb = dd_data->context_un.menlo.cmdiocbq;
James Smartb5a9b2d2013-09-06 12:19:45 -04005952 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5953
5954 spin_lock_irqsave(&phba->hbalock, flags);
James Smarta33c4f72013-03-01 16:36:00 -05005955 list_for_each_entry_safe(check_iocb, next_iocb, &pring->txq,
5956 list) {
5957 if (check_iocb == cmdiocb) {
5958 list_move_tail(&check_iocb->list, &completions);
5959 break;
5960 }
5961 }
5962 if (list_empty(&completions))
5963 lpfc_sli_issue_abort_iotag(phba, pring, cmdiocb);
James Smartb5a9b2d2013-09-06 12:19:45 -04005964 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarta33c4f72013-03-01 16:36:00 -05005965 if (!list_empty(&completions)) {
5966 lpfc_sli_cancel_iocbs(phba, &completions,
5967 IOSTAT_LOCAL_REJECT,
5968 IOERR_SLI_ABORTED);
5969 }
James Smarte2aed292010-02-26 14:15:00 -05005970 break;
James Smart4cc0e562010-01-26 23:09:48 -05005971 default:
5972 spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5973 break;
5974 }
James Smartf1c3b0f2009-07-19 10:01:32 -04005975
James Smart4cc0e562010-01-26 23:09:48 -05005976 /* scsi transport fc fc_bsg_job_timeout expects a zero return code,
5977 * otherwise an error message will be displayed on the console
5978 * so always return success (zero)
5979 */
James Smarta33c4f72013-03-01 16:36:00 -05005980 return rc;
James Smartf1c3b0f2009-07-19 10:01:32 -04005981}