blob: d22ede4b9d740367c964c733e59541c7ec1790c8 [file] [log] [blame]
James Smartd85296c2012-03-01 22:38:13 -05001/*******************************************************************
dea31012005-04-17 16:05:31 -05002 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
James Smart145e5a82020-01-27 16:23:12 -08004 * Copyright (C) 2017-2020 Broadcom. All Rights Reserved. The term *
James Smart3e21d1c2018-05-04 20:37:59 -07005 * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. *
James Smart51f4ca32016-07-06 12:36:13 -07006 * Copyright (C) 2004-2016 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * EMULEX and SLI are trademarks of Emulex. *
James Smartd080abe2017-02-12 13:52:39 -08008 * www.broadcom.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04009 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -050010 * *
11 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040012 * modify it under the terms of version 2 of the GNU General *
13 * Public License as published by the Free Software Foundation. *
14 * This program is distributed in the hope that it will be useful. *
15 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
16 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
17 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
18 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
19 * TO BE LEGALLY INVALID. See the GNU General Public License for *
20 * more details, a copy of which can be found in the file COPYING *
21 * included with this package. *
dea31012005-04-17 16:05:31 -050022 *******************************************************************/
dea31012005-04-17 16:05:31 -050023#include <linux/pci.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
dea31012005-04-17 16:05:31 -050025#include <linux/interrupt.h>
Paul Gortmaker09703662011-05-27 09:37:25 -040026#include <linux/export.h>
James Smarta90f5682006-08-17 11:58:04 -040027#include <linux/delay.h>
James Smarte2a0a9d2008-12-04 22:40:02 -050028#include <asm/unaligned.h>
Dmitry Monakhov128b6f92017-06-29 11:31:12 -070029#include <linux/t10-pi.h>
James Smart737d4242013-04-17 20:14:49 -040030#include <linux/crc-t10dif.h>
31#include <net/checksum.h>
dea31012005-04-17 16:05:31 -050032
33#include <scsi/scsi.h>
34#include <scsi/scsi_device.h>
James Smarte2a0a9d2008-12-04 22:40:02 -050035#include <scsi/scsi_eh.h>
dea31012005-04-17 16:05:31 -050036#include <scsi/scsi_host.h>
37#include <scsi/scsi_tcq.h>
38#include <scsi/scsi_transport_fc.h>
39
40#include "lpfc_version.h"
James Smartda0436e2009-05-22 14:51:39 -040041#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050042#include "lpfc_hw.h"
43#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040044#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040045#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050046#include "lpfc_disc.h"
dea31012005-04-17 16:05:31 -050047#include "lpfc.h"
James Smart9a6b09c2012-03-01 22:37:42 -050048#include "lpfc_scsi.h"
dea31012005-04-17 16:05:31 -050049#include "lpfc_logmsg.h"
50#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050051#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050052
53#define LPFC_RESET_WAIT 2
54#define LPFC_ABORT_WAIT 2
55
James Smarte2a0a9d2008-12-04 22:40:02 -050056static char *dif_op_str[] = {
James Smart9a6b09c2012-03-01 22:37:42 -050057 "PROT_NORMAL",
58 "PROT_READ_INSERT",
59 "PROT_WRITE_STRIP",
60 "PROT_READ_STRIP",
61 "PROT_WRITE_INSERT",
62 "PROT_READ_PASS",
63 "PROT_WRITE_PASS",
64};
65
James Smartf9bb2da2011-10-10 21:34:11 -040066struct scsi_dif_tuple {
67 __be16 guard_tag; /* Checksum */
68 __be16 app_tag; /* Opaque storage */
69 __be32 ref_tag; /* Target LBA or indirect LBA */
70};
71
James Smart1ba981f2014-02-20 09:56:45 -050072static struct lpfc_rport_data *
73lpfc_rport_data_from_scsi_device(struct scsi_device *sdev)
74{
75 struct lpfc_vport *vport = (struct lpfc_vport *)sdev->host->hostdata;
76
James Smartf38fa0b2014-04-04 13:52:21 -040077 if (vport->phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -050078 return ((struct lpfc_device_data *)sdev->hostdata)->rport_data;
79 else
80 return (struct lpfc_rport_data *)sdev->hostdata;
81}
82
James Smartda0436e2009-05-22 14:51:39 -040083static void
James Smartc4908502019-01-28 11:14:28 -080084lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_io_buf *psb);
James Smart1c6f4ef52009-11-18 15:40:49 -050085static void
James Smartc4908502019-01-28 11:14:28 -080086lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_io_buf *psb);
James Smart9c6aa9d2013-05-31 17:03:39 -040087static int
88lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc);
James Smarte2a0a9d2008-12-04 22:40:02 -050089
James Smart9c6aa9d2013-05-31 17:03:39 -040090static inline unsigned
91lpfc_cmd_blksize(struct scsi_cmnd *sc)
92{
93 return sc->device->sector_size;
94}
95
96#define LPFC_CHECK_PROTECT_GUARD 1
97#define LPFC_CHECK_PROTECT_REF 2
98static inline unsigned
99lpfc_cmd_protect(struct scsi_cmnd *sc, int flag)
100{
101 return 1;
102}
103
104static inline unsigned
105lpfc_cmd_guard_csum(struct scsi_cmnd *sc)
106{
107 if (lpfc_prot_group_type(NULL, sc) == LPFC_PG_TYPE_NO_DIF)
108 return 0;
109 if (scsi_host_get_guard(sc->device->host) == SHOST_DIX_GUARD_IP)
110 return 1;
111 return 0;
112}
113
James Smartea2151b2008-09-07 11:52:10 -0400114/**
James Smartf1126682009-06-10 17:22:44 -0400115 * lpfc_sli4_set_rsp_sgl_last - Set the last bit in the response sge.
116 * @phba: Pointer to HBA object.
117 * @lpfc_cmd: lpfc scsi command object pointer.
118 *
119 * This function is called from the lpfc_prep_task_mgmt_cmd function to
120 * set the last bit in the response sge entry.
121 **/
122static void
123lpfc_sli4_set_rsp_sgl_last(struct lpfc_hba *phba,
James Smartc4908502019-01-28 11:14:28 -0800124 struct lpfc_io_buf *lpfc_cmd)
James Smartf1126682009-06-10 17:22:44 -0400125{
James Smart0794d602019-01-28 11:14:19 -0800126 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
James Smartf1126682009-06-10 17:22:44 -0400127 if (sgl) {
128 sgl += 1;
129 sgl->word2 = le32_to_cpu(sgl->word2);
130 bf_set(lpfc_sli4_sge_last, sgl, 1);
131 sgl->word2 = cpu_to_le32(sgl->word2);
132 }
133}
134
135/**
James Smart3621a712009-04-06 18:47:14 -0400136 * lpfc_update_stats - Update statistical data for the command completion
James Smart9df0a032019-09-21 20:58:54 -0700137 * @vport: The virtual port on which this call is executing.
James Smartea2151b2008-09-07 11:52:10 -0400138 * @lpfc_cmd: lpfc scsi command object pointer.
139 *
140 * This function is called when there is a command completion and this
141 * function updates the statistical data for the command completion.
142 **/
143static void
James Smart9df0a032019-09-21 20:58:54 -0700144lpfc_update_stats(struct lpfc_vport *vport, struct lpfc_io_buf *lpfc_cmd)
James Smartea2151b2008-09-07 11:52:10 -0400145{
James Smart9df0a032019-09-21 20:58:54 -0700146 struct lpfc_hba *phba = vport->phba;
James Smart18027a82018-09-10 10:30:49 -0700147 struct lpfc_rport_data *rdata;
148 struct lpfc_nodelist *pnode;
James Smartea2151b2008-09-07 11:52:10 -0400149 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
150 unsigned long flags;
James Smart9df0a032019-09-21 20:58:54 -0700151 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartea2151b2008-09-07 11:52:10 -0400152 unsigned long latency;
153 int i;
154
James Smart18027a82018-09-10 10:30:49 -0700155 if (!vport->stat_data_enabled ||
156 vport->stat_data_blocked ||
157 (cmd->result))
James Smartea2151b2008-09-07 11:52:10 -0400158 return;
159
James Smart9f1e1b52008-12-04 22:39:40 -0500160 latency = jiffies_to_msecs((long)jiffies - (long)lpfc_cmd->start_time);
James Smart18027a82018-09-10 10:30:49 -0700161 rdata = lpfc_cmd->rdata;
162 pnode = rdata->pnode;
James Smart9f1e1b52008-12-04 22:39:40 -0500163
James Smartea2151b2008-09-07 11:52:10 -0400164 spin_lock_irqsave(shost->host_lock, flags);
James Smart18027a82018-09-10 10:30:49 -0700165 if (!pnode ||
166 !pnode->lat_data ||
167 (phba->bucket_type == LPFC_NO_BUCKET)) {
James Smartea2151b2008-09-07 11:52:10 -0400168 spin_unlock_irqrestore(shost->host_lock, flags);
169 return;
170 }
James Smartea2151b2008-09-07 11:52:10 -0400171
172 if (phba->bucket_type == LPFC_LINEAR_BUCKET) {
173 i = (latency + phba->bucket_step - 1 - phba->bucket_base)/
174 phba->bucket_step;
James Smart9f1e1b52008-12-04 22:39:40 -0500175 /* check array subscript bounds */
176 if (i < 0)
177 i = 0;
178 else if (i >= LPFC_MAX_BUCKET_COUNT)
179 i = LPFC_MAX_BUCKET_COUNT - 1;
James Smartea2151b2008-09-07 11:52:10 -0400180 } else {
181 for (i = 0; i < LPFC_MAX_BUCKET_COUNT-1; i++)
182 if (latency <= (phba->bucket_base +
183 ((1<<i)*phba->bucket_step)))
184 break;
185 }
186
187 pnode->lat_data[i].cmd_count++;
188 spin_unlock_irqrestore(shost->host_lock, flags);
189}
190
James Smartea2151b2008-09-07 11:52:10 -0400191/**
James Smart3621a712009-04-06 18:47:14 -0400192 * lpfc_rampdown_queue_depth - Post RAMP_DOWN_QUEUE event to worker thread
James Smart9bad7672008-12-04 22:39:02 -0500193 * @phba: The Hba for which this call is being executed.
194 *
195 * This routine is called when there is resource error in driver or firmware.
196 * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
197 * posts at most 1 event each second. This routine wakes up worker thread of
198 * @phba to process WORKER_RAM_DOWN_EVENT event.
199 *
200 * This routine should be called with no lock held.
201 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500202void
James Smarteaf15d52008-12-04 22:39:29 -0500203lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
James Smart92d7f7b2007-06-17 19:56:38 -0500204{
205 unsigned long flags;
James Smart5e9d9b82008-06-14 22:52:53 -0400206 uint32_t evt_posted;
Manuel Schölling0d4aec12014-09-03 12:55:58 -0400207 unsigned long expires;
James Smart92d7f7b2007-06-17 19:56:38 -0500208
209 spin_lock_irqsave(&phba->hbalock, flags);
210 atomic_inc(&phba->num_rsrc_err);
211 phba->last_rsrc_error_time = jiffies;
212
Manuel Schölling0d4aec12014-09-03 12:55:58 -0400213 expires = phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL;
214 if (time_after(expires, jiffies)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500215 spin_unlock_irqrestore(&phba->hbalock, flags);
216 return;
217 }
218
219 phba->last_ramp_down_time = jiffies;
220
221 spin_unlock_irqrestore(&phba->hbalock, flags);
222
223 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400224 evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
225 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500226 phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500227 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
228
James Smart5e9d9b82008-06-14 22:52:53 -0400229 if (!evt_posted)
230 lpfc_worker_wake_up(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500231 return;
232}
233
James Smart9bad7672008-12-04 22:39:02 -0500234/**
James Smart3621a712009-04-06 18:47:14 -0400235 * lpfc_ramp_down_queue_handler - WORKER_RAMP_DOWN_QUEUE event handler
James Smart9bad7672008-12-04 22:39:02 -0500236 * @phba: The Hba for which this call is being executed.
237 *
238 * This routine is called to process WORKER_RAMP_DOWN_QUEUE event for worker
239 * thread.This routine reduces queue depth for all scsi device on each vport
240 * associated with @phba.
241 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500242void
243lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
244{
James Smart549e55c2007-08-02 11:09:51 -0400245 struct lpfc_vport **vports;
246 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500247 struct scsi_device *sdev;
James Smart5ffc2662009-11-18 15:39:44 -0500248 unsigned long new_queue_depth;
James Smart92d7f7b2007-06-17 19:56:38 -0500249 unsigned long num_rsrc_err, num_cmd_success;
James Smart549e55c2007-08-02 11:09:51 -0400250 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500251
252 num_rsrc_err = atomic_read(&phba->num_rsrc_err);
253 num_cmd_success = atomic_read(&phba->num_cmd_success);
254
James Smart75ad83a2012-05-09 21:18:40 -0400255 /*
256 * The error and success command counters are global per
257 * driver instance. If another handler has already
258 * operated on this error event, just exit.
259 */
260 if (num_rsrc_err == 0)
261 return;
262
James Smart549e55c2007-08-02 11:09:51 -0400263 vports = lpfc_create_vport_work_array(phba);
264 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400265 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400266 shost = lpfc_shost_from_vport(vports[i]);
267 shost_for_each_device(sdev, shost) {
James Smart92d7f7b2007-06-17 19:56:38 -0500268 new_queue_depth =
James Smart549e55c2007-08-02 11:09:51 -0400269 sdev->queue_depth * num_rsrc_err /
270 (num_rsrc_err + num_cmd_success);
271 if (!new_queue_depth)
272 new_queue_depth = sdev->queue_depth - 1;
273 else
274 new_queue_depth = sdev->queue_depth -
275 new_queue_depth;
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +0100276 scsi_change_queue_depth(sdev, new_queue_depth);
James Smart549e55c2007-08-02 11:09:51 -0400277 }
James Smart92d7f7b2007-06-17 19:56:38 -0500278 }
James Smart09372822008-01-11 01:52:54 -0500279 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500280 atomic_set(&phba->num_rsrc_err, 0);
281 atomic_set(&phba->num_cmd_success, 0);
282}
283
James Smart9bad7672008-12-04 22:39:02 -0500284/**
James Smart3621a712009-04-06 18:47:14 -0400285 * lpfc_scsi_dev_block - set all scsi hosts to block state
James Smarta8e497d2008-08-24 21:50:11 -0400286 * @phba: Pointer to HBA context object.
287 *
288 * This function walks vport list and set each SCSI host to block state
289 * by invoking fc_remote_port_delete() routine. This function is invoked
290 * with EEH when device's PCI slot has been permanently disabled.
291 **/
292void
293lpfc_scsi_dev_block(struct lpfc_hba *phba)
294{
295 struct lpfc_vport **vports;
296 struct Scsi_Host *shost;
297 struct scsi_device *sdev;
298 struct fc_rport *rport;
299 int i;
300
301 vports = lpfc_create_vport_work_array(phba);
302 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400303 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8e497d2008-08-24 21:50:11 -0400304 shost = lpfc_shost_from_vport(vports[i]);
305 shost_for_each_device(sdev, shost) {
306 rport = starget_to_rport(scsi_target(sdev));
307 fc_remote_port_delete(rport);
308 }
309 }
310 lpfc_destroy_vport_work_array(phba, vports);
311}
312
James Smart9bad7672008-12-04 22:39:02 -0500313/**
James Smart3772a992009-05-22 14:50:54 -0400314 * lpfc_new_scsi_buf_s3 - Scsi buffer allocator for HBA with SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -0500315 * @vport: The virtual port for which this call being executed.
Lee Joneseceee002020-11-02 14:23:43 +0000316 * @num_to_alloc: The requested number of buffers to allocate.
James Smart9bad7672008-12-04 22:39:02 -0500317 *
James Smart3772a992009-05-22 14:50:54 -0400318 * This routine allocates a scsi buffer for device with SLI-3 interface spec,
319 * the scsi buffer contains all the necessary information needed to initiate
320 * a SCSI I/O. The non-DMAable buffer region contains information to build
321 * the IOCB. The DMAable region contains memory for the FCP CMND, FCP RSP,
322 * and the initial BPL. In addition to allocating memory, the FCP CMND and
323 * FCP RSP BDEs are setup in the BPL and the BPL BDE is setup in the IOCB.
James Smart9bad7672008-12-04 22:39:02 -0500324 *
325 * Return codes:
James Smart3772a992009-05-22 14:50:54 -0400326 * int - number of scsi buffers that were allocated.
327 * 0 = failure, less than num_to_alloc is a partial failure.
James Smart9bad7672008-12-04 22:39:02 -0500328 **/
James Smart3772a992009-05-22 14:50:54 -0400329static int
330lpfc_new_scsi_buf_s3(struct lpfc_vport *vport, int num_to_alloc)
dea31012005-04-17 16:05:31 -0500331{
James Smart2e0fef82007-06-17 19:56:36 -0500332 struct lpfc_hba *phba = vport->phba;
James Smartc4908502019-01-28 11:14:28 -0800333 struct lpfc_io_buf *psb;
dea31012005-04-17 16:05:31 -0500334 struct ulp_bde64 *bpl;
335 IOCB_t *iocb;
James Smart34b02dc2008-08-24 21:49:55 -0400336 dma_addr_t pdma_phys_fcp_cmd;
337 dma_addr_t pdma_phys_fcp_rsp;
James Smart0794d602019-01-28 11:14:19 -0800338 dma_addr_t pdma_phys_sgl;
James Bottomley604a3e32005-10-29 10:28:33 -0500339 uint16_t iotag;
James Smart96f70772013-04-17 20:16:15 -0400340 int bcnt, bpl_size;
341
342 bpl_size = phba->cfg_sg_dma_buf_size -
343 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
344
345 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
346 "9067 ALLOC %d scsi_bufs: %d (%d + %d + %d)\n",
347 num_to_alloc, phba->cfg_sg_dma_buf_size,
348 (int)sizeof(struct fcp_cmnd),
349 (int)sizeof(struct fcp_rsp), bpl_size);
dea31012005-04-17 16:05:31 -0500350
James Smart3772a992009-05-22 14:50:54 -0400351 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
James Smartc4908502019-01-28 11:14:28 -0800352 psb = kzalloc(sizeof(struct lpfc_io_buf), GFP_KERNEL);
James Smart3772a992009-05-22 14:50:54 -0400353 if (!psb)
354 break;
dea31012005-04-17 16:05:31 -0500355
James Smart3772a992009-05-22 14:50:54 -0400356 /*
357 * Get memory from the pci pool to map the virt space to pci
358 * bus space for an I/O. The DMA buffer includes space for the
359 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
360 * necessary to support the sg_tablesize.
361 */
Romain Perier771db5c2017-07-06 10:13:05 +0200362 psb->data = dma_pool_zalloc(phba->lpfc_sg_dma_buf_pool,
James Smart3772a992009-05-22 14:50:54 -0400363 GFP_KERNEL, &psb->dma_handle);
364 if (!psb->data) {
365 kfree(psb);
366 break;
367 }
dea31012005-04-17 16:05:31 -0500368
dea31012005-04-17 16:05:31 -0500369
James Smart3772a992009-05-22 14:50:54 -0400370 /* Allocate iotag for psb->cur_iocbq. */
371 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
372 if (iotag == 0) {
Romain Perier771db5c2017-07-06 10:13:05 +0200373 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
James Smart895427b2017-02-12 13:52:30 -0800374 psb->data, psb->dma_handle);
James Smart3772a992009-05-22 14:50:54 -0400375 kfree(psb);
376 break;
377 }
378 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Bottomley604a3e32005-10-29 10:28:33 -0500379
James Smart3772a992009-05-22 14:50:54 -0400380 psb->fcp_cmnd = psb->data;
381 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
James Smart0794d602019-01-28 11:14:19 -0800382 psb->dma_sgl = psb->data + sizeof(struct fcp_cmnd) +
James Smart34b02dc2008-08-24 21:49:55 -0400383 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500384
James Smart3772a992009-05-22 14:50:54 -0400385 /* Initialize local short-hand pointers. */
James Smartc4908502019-01-28 11:14:28 -0800386 bpl = (struct ulp_bde64 *)psb->dma_sgl;
James Smart3772a992009-05-22 14:50:54 -0400387 pdma_phys_fcp_cmd = psb->dma_handle;
388 pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
James Smart0794d602019-01-28 11:14:19 -0800389 pdma_phys_sgl = psb->dma_handle + sizeof(struct fcp_cmnd) +
James Smart3772a992009-05-22 14:50:54 -0400390 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500391
James Smart3772a992009-05-22 14:50:54 -0400392 /*
393 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
394 * are sg list bdes. Initialize the first two and leave the
395 * rest for queuecommand.
396 */
397 bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
398 bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
399 bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
400 bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
401 bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
dea31012005-04-17 16:05:31 -0500402
James Smart3772a992009-05-22 14:50:54 -0400403 /* Setup the physical region for the FCP RSP */
404 bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
405 bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
406 bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
407 bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
408 bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
409
410 /*
411 * Since the IOCB for the FCP I/O is built into this
412 * lpfc_scsi_buf, initialize it with all known data now.
413 */
414 iocb = &psb->cur_iocbq.iocb;
415 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
416 if ((phba->sli_rev == 3) &&
417 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
418 /* fill in immediate fcp command BDE */
419 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
420 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
421 iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
422 unsli3.fcp_ext.icd);
423 iocb->un.fcpi64.bdl.addrHigh = 0;
424 iocb->ulpBdeCount = 0;
425 iocb->ulpLe = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300426 /* fill in response BDE */
James Smart3772a992009-05-22 14:50:54 -0400427 iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags =
428 BUFF_TYPE_BDE_64;
429 iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
430 sizeof(struct fcp_rsp);
431 iocb->unsli3.fcp_ext.rbde.addrLow =
432 putPaddrLow(pdma_phys_fcp_rsp);
433 iocb->unsli3.fcp_ext.rbde.addrHigh =
434 putPaddrHigh(pdma_phys_fcp_rsp);
435 } else {
436 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
437 iocb->un.fcpi64.bdl.bdeSize =
438 (2 * sizeof(struct ulp_bde64));
439 iocb->un.fcpi64.bdl.addrLow =
James Smart0794d602019-01-28 11:14:19 -0800440 putPaddrLow(pdma_phys_sgl);
James Smart3772a992009-05-22 14:50:54 -0400441 iocb->un.fcpi64.bdl.addrHigh =
James Smart0794d602019-01-28 11:14:19 -0800442 putPaddrHigh(pdma_phys_sgl);
James Smart3772a992009-05-22 14:50:54 -0400443 iocb->ulpBdeCount = 1;
444 iocb->ulpLe = 1;
445 }
446 iocb->ulpClass = CLASS3;
447 psb->status = IOSTAT_SUCCESS;
James Smartda0436e2009-05-22 14:51:39 -0400448 /* Put it back into the SCSI buffer list */
James Smarteee88772010-09-29 11:19:08 -0400449 psb->cur_iocbq.context1 = psb;
James Smartc2017262019-01-28 11:14:37 -0800450 spin_lock_init(&psb->buf_lock);
James Smart1c6f4ef52009-11-18 15:40:49 -0500451 lpfc_release_scsi_buf_s3(phba, psb);
James Smart3772a992009-05-22 14:50:54 -0400452
James Smart34b02dc2008-08-24 21:49:55 -0400453 }
dea31012005-04-17 16:05:31 -0500454
James Smart3772a992009-05-22 14:50:54 -0400455 return bcnt;
dea31012005-04-17 16:05:31 -0500456}
457
James Smart9bad7672008-12-04 22:39:02 -0500458/**
James Smart1151e3e2011-02-16 12:39:35 -0500459 * lpfc_sli4_vport_delete_fcp_xri_aborted -Remove all ndlp references for vport
460 * @vport: pointer to lpfc vport data structure.
461 *
462 * This routine is invoked by the vport cleanup for deletions and the cleanup
463 * for an ndlp on removal.
464 **/
465void
466lpfc_sli4_vport_delete_fcp_xri_aborted(struct lpfc_vport *vport)
467{
468 struct lpfc_hba *phba = vport->phba;
James Smartc4908502019-01-28 11:14:28 -0800469 struct lpfc_io_buf *psb, *next_psb;
James Smart5e5b5112019-01-28 11:14:22 -0800470 struct lpfc_sli4_hdw_queue *qp;
James Smart1151e3e2011-02-16 12:39:35 -0500471 unsigned long iflag = 0;
James Smart5e5b5112019-01-28 11:14:22 -0800472 int idx;
James Smart1151e3e2011-02-16 12:39:35 -0500473
James Smartf6e84792019-01-28 11:14:38 -0800474 if (!(vport->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
James Smart895427b2017-02-12 13:52:30 -0800475 return;
James Smart5e5b5112019-01-28 11:14:22 -0800476
James Smart1151e3e2011-02-16 12:39:35 -0500477 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart5e5b5112019-01-28 11:14:22 -0800478 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
479 qp = &phba->sli4_hba.hdwq[idx];
480
James Smartc00f62e2019-08-14 16:57:11 -0700481 spin_lock(&qp->abts_io_buf_list_lock);
James Smart5e5b5112019-01-28 11:14:22 -0800482 list_for_each_entry_safe(psb, next_psb,
James Smartc00f62e2019-08-14 16:57:11 -0700483 &qp->lpfc_abts_io_buf_list, list) {
James Smartc438d062019-12-18 15:58:07 -0800484 if (psb->cur_iocbq.iocb_flag & LPFC_IO_NVME)
James Smartc00f62e2019-08-14 16:57:11 -0700485 continue;
486
James Smart5e5b5112019-01-28 11:14:22 -0800487 if (psb->rdata && psb->rdata->pnode &&
488 psb->rdata->pnode->vport == vport)
489 psb->rdata = NULL;
490 }
James Smartc00f62e2019-08-14 16:57:11 -0700491 spin_unlock(&qp->abts_io_buf_list_lock);
James Smart1151e3e2011-02-16 12:39:35 -0500492 }
James Smart1151e3e2011-02-16 12:39:35 -0500493 spin_unlock_irqrestore(&phba->hbalock, iflag);
494}
495
496/**
James Smartc00f62e2019-08-14 16:57:11 -0700497 * lpfc_sli4_io_xri_aborted - Fast-path process of fcp xri abort
James Smartda0436e2009-05-22 14:51:39 -0400498 * @phba: pointer to lpfc hba data structure.
499 * @axri: pointer to the fcp xri abort wcqe structure.
Lee Joneseceee002020-11-02 14:23:43 +0000500 * @idx: index into hdwq
James Smartda0436e2009-05-22 14:51:39 -0400501 *
502 * This routine is invoked by the worker thread to process a SLI4 fast-path
James Smartc00f62e2019-08-14 16:57:11 -0700503 * FCP or NVME aborted xri.
James Smartda0436e2009-05-22 14:51:39 -0400504 **/
505void
James Smartc00f62e2019-08-14 16:57:11 -0700506lpfc_sli4_io_xri_aborted(struct lpfc_hba *phba,
507 struct sli4_wcqe_xri_aborted *axri, int idx)
James Smartda0436e2009-05-22 14:51:39 -0400508{
509 uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
James Smart19ca7602010-11-20 23:11:55 -0500510 uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
James Smartc4908502019-01-28 11:14:28 -0800511 struct lpfc_io_buf *psb, *next_psb;
James Smart5e5b5112019-01-28 11:14:22 -0800512 struct lpfc_sli4_hdw_queue *qp;
James Smartda0436e2009-05-22 14:51:39 -0400513 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -0500514 struct lpfc_iocbq *iocbq;
515 int i;
James Smart19ca7602010-11-20 23:11:55 -0500516 struct lpfc_nodelist *ndlp;
517 int rrq_empty = 0;
James Smart895427b2017-02-12 13:52:30 -0800518 struct lpfc_sli_ring *pring = phba->sli4_hba.els_wq->pring;
James Smartda0436e2009-05-22 14:51:39 -0400519
James Smart895427b2017-02-12 13:52:30 -0800520 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
521 return;
James Smart5e5b5112019-01-28 11:14:22 -0800522
523 qp = &phba->sli4_hba.hdwq[idx];
James Smart0f65ff62010-02-26 14:14:23 -0500524 spin_lock_irqsave(&phba->hbalock, iflag);
James Smartc00f62e2019-08-14 16:57:11 -0700525 spin_lock(&qp->abts_io_buf_list_lock);
James Smartda0436e2009-05-22 14:51:39 -0400526 list_for_each_entry_safe(psb, next_psb,
James Smartc00f62e2019-08-14 16:57:11 -0700527 &qp->lpfc_abts_io_buf_list, list) {
James Smartda0436e2009-05-22 14:51:39 -0400528 if (psb->cur_iocbq.sli4_xritag == xri) {
James Smartc00f62e2019-08-14 16:57:11 -0700529 list_del_init(&psb->list);
James Smart324e1c42019-10-18 14:18:21 -0700530 psb->flags &= ~LPFC_SBUF_XBUSY;
James Smartda0436e2009-05-22 14:51:39 -0400531 psb->status = IOSTAT_SUCCESS;
James Smartc438d062019-12-18 15:58:07 -0800532 if (psb->cur_iocbq.iocb_flag & LPFC_IO_NVME) {
James Smartc00f62e2019-08-14 16:57:11 -0700533 qp->abts_nvme_io_bufs--;
534 spin_unlock(&qp->abts_io_buf_list_lock);
535 spin_unlock_irqrestore(&phba->hbalock, iflag);
536 lpfc_sli4_nvme_xri_aborted(phba, axri, psb);
537 return;
538 }
James Smartc00f62e2019-08-14 16:57:11 -0700539 qp->abts_scsi_io_bufs--;
540 spin_unlock(&qp->abts_io_buf_list_lock);
541
James Smart1151e3e2011-02-16 12:39:35 -0500542 if (psb->rdata && psb->rdata->pnode)
543 ndlp = psb->rdata->pnode;
544 else
545 ndlp = NULL;
546
James Smart19ca7602010-11-20 23:11:55 -0500547 rrq_empty = list_empty(&phba->active_rrq_list);
James Smart0f65ff62010-02-26 14:14:23 -0500548 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartcb69f7d2011-12-13 13:21:57 -0500549 if (ndlp) {
James Smartee0f4fe2012-05-09 21:19:14 -0400550 lpfc_set_rrq_active(phba, ndlp,
551 psb->cur_iocbq.sli4_lxritag, rxid, 1);
James Smartcb69f7d2011-12-13 13:21:57 -0500552 lpfc_sli4_abts_err_handler(phba, ndlp, axri);
553 }
James Smartda0436e2009-05-22 14:51:39 -0400554 lpfc_release_scsi_buf_s4(phba, psb);
James Smart19ca7602010-11-20 23:11:55 -0500555 if (rrq_empty)
556 lpfc_worker_wake_up(phba);
James Smartda0436e2009-05-22 14:51:39 -0400557 return;
558 }
559 }
James Smartc00f62e2019-08-14 16:57:11 -0700560 spin_unlock(&qp->abts_io_buf_list_lock);
James Smart0f65ff62010-02-26 14:14:23 -0500561 for (i = 1; i <= phba->sli.last_iotag; i++) {
562 iocbq = phba->sli.iocbq_lookup[i];
563
James Smartc00f62e2019-08-14 16:57:11 -0700564 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
565 (iocbq->iocb_flag & LPFC_IO_LIBDFC))
James Smart0f65ff62010-02-26 14:14:23 -0500566 continue;
567 if (iocbq->sli4_xritag != xri)
568 continue;
James Smartc4908502019-01-28 11:14:28 -0800569 psb = container_of(iocbq, struct lpfc_io_buf, cur_iocbq);
James Smart324e1c42019-10-18 14:18:21 -0700570 psb->flags &= ~LPFC_SBUF_XBUSY;
James Smart0f65ff62010-02-26 14:14:23 -0500571 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart0e9bb8d2013-03-01 16:35:12 -0500572 if (!list_empty(&pring->txq))
James Smart589a52d2010-07-14 15:30:54 -0400573 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -0500574 return;
575
576 }
577 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartda0436e2009-05-22 14:51:39 -0400578}
579
580/**
James Smart19ca7602010-11-20 23:11:55 -0500581 * lpfc_get_scsi_buf_s3 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
James Smart3772a992009-05-22 14:50:54 -0400582 * @phba: The HBA for which this call is being executed.
Lee Joneseceee002020-11-02 14:23:43 +0000583 * @ndlp: pointer to a node-list data structure.
584 * @cmnd: Pointer to scsi_cmnd data structure.
James Smart9bad7672008-12-04 22:39:02 -0500585 *
586 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
587 * and returns to caller.
588 *
589 * Return codes:
590 * NULL - Error
591 * Pointer to lpfc_scsi_buf - Success
592 **/
James Smartc4908502019-01-28 11:14:28 -0800593static struct lpfc_io_buf *
James Smartace44e42019-01-28 11:14:27 -0800594lpfc_get_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
595 struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -0500596{
James Smartc4908502019-01-28 11:14:28 -0800597 struct lpfc_io_buf *lpfc_cmd = NULL;
James Smarta40fc5f2013-04-17 20:17:40 -0400598 struct list_head *scsi_buf_list_get = &phba->lpfc_scsi_buf_list_get;
James Smart164cecd2013-09-06 12:22:38 -0400599 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500600
James Smart164cecd2013-09-06 12:22:38 -0400601 spin_lock_irqsave(&phba->scsi_buf_list_get_lock, iflag);
James Smartc4908502019-01-28 11:14:28 -0800602 list_remove_head(scsi_buf_list_get, lpfc_cmd, struct lpfc_io_buf,
James Smarta40fc5f2013-04-17 20:17:40 -0400603 list);
604 if (!lpfc_cmd) {
James Smart164cecd2013-09-06 12:22:38 -0400605 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -0400606 list_splice(&phba->lpfc_scsi_buf_list_put,
607 &phba->lpfc_scsi_buf_list_get);
608 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
609 list_remove_head(scsi_buf_list_get, lpfc_cmd,
James Smartc4908502019-01-28 11:14:28 -0800610 struct lpfc_io_buf, list);
James Smart164cecd2013-09-06 12:22:38 -0400611 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smart1dcb58e2007-04-25 09:51:30 -0400612 }
James Smart164cecd2013-09-06 12:22:38 -0400613 spin_unlock_irqrestore(&phba->scsi_buf_list_get_lock, iflag);
James Smart2a5b7d62018-07-31 17:23:22 -0700614
615 if (lpfc_ndlp_check_qdepth(phba, ndlp) && lpfc_cmd) {
616 atomic_inc(&ndlp->cmd_pending);
617 lpfc_cmd->flags |= LPFC_SBUF_BUMP_QDEPTH;
618 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400619 return lpfc_cmd;
620}
James Smart19ca7602010-11-20 23:11:55 -0500621/**
James Smart5e5b5112019-01-28 11:14:22 -0800622 * lpfc_get_scsi_buf_s4 - Get a scsi buffer from io_buf_list of the HBA
James Smart19ca7602010-11-20 23:11:55 -0500623 * @phba: The HBA for which this call is being executed.
Lee Joneseceee002020-11-02 14:23:43 +0000624 * @ndlp: pointer to a node-list data structure.
625 * @cmnd: Pointer to scsi_cmnd data structure.
James Smart19ca7602010-11-20 23:11:55 -0500626 *
James Smart5e5b5112019-01-28 11:14:22 -0800627 * This routine removes a scsi buffer from head of @hdwq io_buf_list
James Smart19ca7602010-11-20 23:11:55 -0500628 * and returns to caller.
629 *
630 * Return codes:
631 * NULL - Error
632 * Pointer to lpfc_scsi_buf - Success
633 **/
James Smartc4908502019-01-28 11:14:28 -0800634static struct lpfc_io_buf *
James Smartace44e42019-01-28 11:14:27 -0800635lpfc_get_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
636 struct scsi_cmnd *cmnd)
James Smart19ca7602010-11-20 23:11:55 -0500637{
James Smartc4908502019-01-28 11:14:28 -0800638 struct lpfc_io_buf *lpfc_cmd;
James Smart5e5b5112019-01-28 11:14:22 -0800639 struct lpfc_sli4_hdw_queue *qp;
James Smart0794d602019-01-28 11:14:19 -0800640 struct sli4_sge *sgl;
641 IOCB_t *iocb;
642 dma_addr_t pdma_phys_fcp_rsp;
643 dma_addr_t pdma_phys_fcp_cmd;
James Smartd79c9e92019-08-14 16:57:09 -0700644 uint32_t cpu, idx;
James Smartace44e42019-01-28 11:14:27 -0800645 int tag;
James Smartd79c9e92019-08-14 16:57:09 -0700646 struct fcp_cmd_rsp_buf *tmp = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500647
Bart Van Assched6d189c2019-03-28 11:06:22 -0700648 cpu = raw_smp_processor_id();
James Smart45aa3122019-01-28 11:14:29 -0800649 if (cmnd && phba->cfg_fcp_io_sched == LPFC_FCP_SCHED_BY_HDWQ) {
James Smartace44e42019-01-28 11:14:27 -0800650 tag = blk_mq_unique_tag(cmnd->request);
651 idx = blk_mq_unique_tag_to_hwq(tag);
652 } else {
James Smart6a828b02019-01-28 11:14:31 -0800653 idx = phba->sli4_hba.cpu_map[cpu].hdwq;
James Smartace44e42019-01-28 11:14:27 -0800654 }
James Smart5e5b5112019-01-28 11:14:22 -0800655
James Smartc4908502019-01-28 11:14:28 -0800656 lpfc_cmd = lpfc_get_io_buf(phba, ndlp, idx,
657 !phba->cfg_xri_rebalancing);
658 if (!lpfc_cmd) {
659 qp = &phba->sli4_hba.hdwq[idx];
James Smart5e5b5112019-01-28 11:14:22 -0800660 qp->empty_io_bufs++;
James Smart1151e3e2011-02-16 12:39:35 -0500661 return NULL;
James Smart5e5b5112019-01-28 11:14:22 -0800662 }
James Smart2a5b7d62018-07-31 17:23:22 -0700663
James Smart0794d602019-01-28 11:14:19 -0800664 /* Setup key fields in buffer that may have been changed
665 * if other protocols used this buffer.
666 */
667 lpfc_cmd->cur_iocbq.iocb_flag = LPFC_IO_FCP;
668 lpfc_cmd->prot_seg_cnt = 0;
669 lpfc_cmd->seg_cnt = 0;
James Smart0794d602019-01-28 11:14:19 -0800670 lpfc_cmd->timeout = 0;
671 lpfc_cmd->flags = 0;
672 lpfc_cmd->start_time = jiffies;
673 lpfc_cmd->waitq = NULL;
James Smart5e5b5112019-01-28 11:14:22 -0800674 lpfc_cmd->cpu = cpu;
James Smart0794d602019-01-28 11:14:19 -0800675#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
676 lpfc_cmd->prot_data_type = 0;
677#endif
James Smartd79c9e92019-08-14 16:57:09 -0700678 tmp = lpfc_get_cmd_rsp_buf_per_hdwq(phba, lpfc_cmd);
James Smart0ab384a2020-01-27 16:23:02 -0800679 if (!tmp) {
680 lpfc_release_io_buf(phba, lpfc_cmd, lpfc_cmd->hdwq);
James Smartd79c9e92019-08-14 16:57:09 -0700681 return NULL;
James Smart0ab384a2020-01-27 16:23:02 -0800682 }
James Smartd79c9e92019-08-14 16:57:09 -0700683
684 lpfc_cmd->fcp_cmnd = tmp->fcp_cmnd;
685 lpfc_cmd->fcp_rsp = tmp->fcp_rsp;
James Smart0794d602019-01-28 11:14:19 -0800686
687 /*
688 * The first two SGEs are the FCP_CMD and FCP_RSP.
689 * The balance are sg list bdes. Initialize the
690 * first two and leave the rest for queuecommand.
691 */
692 sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
James Smartd79c9e92019-08-14 16:57:09 -0700693 pdma_phys_fcp_cmd = tmp->fcp_cmd_rsp_dma_handle;
James Smart0794d602019-01-28 11:14:19 -0800694 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
695 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
696 sgl->word2 = le32_to_cpu(sgl->word2);
697 bf_set(lpfc_sli4_sge_last, sgl, 0);
698 sgl->word2 = cpu_to_le32(sgl->word2);
699 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_cmnd));
700 sgl++;
701
702 /* Setup the physical region for the FCP RSP */
703 pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
704 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
705 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
706 sgl->word2 = le32_to_cpu(sgl->word2);
707 bf_set(lpfc_sli4_sge_last, sgl, 1);
708 sgl->word2 = cpu_to_le32(sgl->word2);
709 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
710
711 /*
712 * Since the IOCB for the FCP I/O is built into this
James Smartc4908502019-01-28 11:14:28 -0800713 * lpfc_io_buf, initialize it with all known data now.
James Smart0794d602019-01-28 11:14:19 -0800714 */
715 iocb = &lpfc_cmd->cur_iocbq.iocb;
716 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
717 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
718 /* setting the BLP size to 2 * sizeof BDE may not be correct.
719 * We are setting the bpl to point to out sgl. An sgl's
720 * entries are 16 bytes, a bpl entries are 12 bytes.
721 */
722 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
723 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
724 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
725 iocb->ulpBdeCount = 1;
726 iocb->ulpLe = 1;
727 iocb->ulpClass = CLASS3;
728
Dan Carpenter26c724a2018-08-23 16:56:33 +0300729 if (lpfc_ndlp_check_qdepth(phba, ndlp)) {
James Smart2a5b7d62018-07-31 17:23:22 -0700730 atomic_inc(&ndlp->cmd_pending);
731 lpfc_cmd->flags |= LPFC_SBUF_BUMP_QDEPTH;
732 }
James Smarta40fc5f2013-04-17 20:17:40 -0400733 return lpfc_cmd;
James Smart19ca7602010-11-20 23:11:55 -0500734}
735/**
736 * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
737 * @phba: The HBA for which this call is being executed.
Lee Joneseceee002020-11-02 14:23:43 +0000738 * @ndlp: pointer to a node-list data structure.
739 * @cmnd: Pointer to scsi_cmnd data structure.
James Smart19ca7602010-11-20 23:11:55 -0500740 *
741 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
742 * and returns to caller.
743 *
744 * Return codes:
745 * NULL - Error
746 * Pointer to lpfc_scsi_buf - Success
747 **/
James Smartc4908502019-01-28 11:14:28 -0800748static struct lpfc_io_buf*
James Smartace44e42019-01-28 11:14:27 -0800749lpfc_get_scsi_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
750 struct scsi_cmnd *cmnd)
James Smart19ca7602010-11-20 23:11:55 -0500751{
James Smartace44e42019-01-28 11:14:27 -0800752 return phba->lpfc_get_scsi_buf(phba, ndlp, cmnd);
James Smart19ca7602010-11-20 23:11:55 -0500753}
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400754
James Smart9bad7672008-12-04 22:39:02 -0500755/**
James Smart3772a992009-05-22 14:50:54 -0400756 * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
James Smart9bad7672008-12-04 22:39:02 -0500757 * @phba: The Hba for which this call is being executed.
758 * @psb: The scsi buffer which is being released.
759 *
760 * This routine releases @psb scsi buffer by adding it to tail of @phba
761 * lpfc_scsi_buf_list list.
762 **/
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400763static void
James Smartc4908502019-01-28 11:14:28 -0800764lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_io_buf *psb)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400765{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500766 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500767
James Smarta40fc5f2013-04-17 20:17:40 -0400768 psb->seg_cnt = 0;
James Smarta40fc5f2013-04-17 20:17:40 -0400769 psb->prot_seg_cnt = 0;
770
771 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400772 psb->pCmd = NULL;
James Smart6a485eb2013-04-17 20:19:00 -0400773 psb->cur_iocbq.iocb_flag = LPFC_IO_FCP;
James Smarta40fc5f2013-04-17 20:17:40 -0400774 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list_put);
775 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
dea31012005-04-17 16:05:31 -0500776}
777
James Smart9bad7672008-12-04 22:39:02 -0500778/**
James Smartda0436e2009-05-22 14:51:39 -0400779 * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
780 * @phba: The Hba for which this call is being executed.
781 * @psb: The scsi buffer which is being released.
782 *
James Smart5e5b5112019-01-28 11:14:22 -0800783 * This routine releases @psb scsi buffer by adding it to tail of @hdwq
784 * io_buf_list list. For SLI4 XRI's are tied to the scsi buffer
James Smartda0436e2009-05-22 14:51:39 -0400785 * and cannot be reused for at least RA_TOV amount of time if it was
786 * aborted.
787 **/
788static void
James Smartc4908502019-01-28 11:14:28 -0800789lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_io_buf *psb)
James Smartda0436e2009-05-22 14:51:39 -0400790{
James Smart5e5b5112019-01-28 11:14:22 -0800791 struct lpfc_sli4_hdw_queue *qp;
James Smartda0436e2009-05-22 14:51:39 -0400792 unsigned long iflag = 0;
793
James Smarta40fc5f2013-04-17 20:17:40 -0400794 psb->seg_cnt = 0;
James Smarta40fc5f2013-04-17 20:17:40 -0400795 psb->prot_seg_cnt = 0;
796
James Smart1fbf9742019-01-28 11:14:26 -0800797 qp = psb->hdwq;
James Smart324e1c42019-10-18 14:18:21 -0700798 if (psb->flags & LPFC_SBUF_XBUSY) {
James Smartc00f62e2019-08-14 16:57:11 -0700799 spin_lock_irqsave(&qp->abts_io_buf_list_lock, iflag);
James Smartda0436e2009-05-22 14:51:39 -0400800 psb->pCmd = NULL;
James Smartc00f62e2019-08-14 16:57:11 -0700801 list_add_tail(&psb->list, &qp->lpfc_abts_io_buf_list);
James Smart5e5b5112019-01-28 11:14:22 -0800802 qp->abts_scsi_io_bufs++;
James Smartc00f62e2019-08-14 16:57:11 -0700803 spin_unlock_irqrestore(&qp->abts_io_buf_list_lock, iflag);
James Smartda0436e2009-05-22 14:51:39 -0400804 } else {
James Smartc4908502019-01-28 11:14:28 -0800805 lpfc_release_io_buf(phba, (struct lpfc_io_buf *)psb, qp);
James Smartda0436e2009-05-22 14:51:39 -0400806 }
807}
808
809/**
James Smart3772a992009-05-22 14:50:54 -0400810 * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
811 * @phba: The Hba for which this call is being executed.
812 * @psb: The scsi buffer which is being released.
813 *
814 * This routine releases @psb scsi buffer by adding it to tail of @phba
815 * lpfc_scsi_buf_list list.
816 **/
817static void
James Smartc4908502019-01-28 11:14:28 -0800818lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_io_buf *psb)
James Smart3772a992009-05-22 14:50:54 -0400819{
James Smart2a5b7d62018-07-31 17:23:22 -0700820 if ((psb->flags & LPFC_SBUF_BUMP_QDEPTH) && psb->ndlp)
821 atomic_dec(&psb->ndlp->cmd_pending);
James Smart3772a992009-05-22 14:50:54 -0400822
James Smart2a5b7d62018-07-31 17:23:22 -0700823 psb->flags &= ~LPFC_SBUF_BUMP_QDEPTH;
James Smart3772a992009-05-22 14:50:54 -0400824 phba->lpfc_release_scsi_buf(phba, psb);
825}
826
827/**
828 * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -0500829 * @phba: The Hba for which this call is being executed.
830 * @lpfc_cmd: The scsi buffer which is going to be mapped.
831 *
832 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
James Smart3772a992009-05-22 14:50:54 -0400833 * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -0700834 * through sg elements and format the bde. This routine also initializes all
James Smart3772a992009-05-22 14:50:54 -0400835 * IOCB fields which are dependent on scsi command request buffer.
James Smart9bad7672008-12-04 22:39:02 -0500836 *
837 * Return codes:
838 * 1 - Error
839 * 0 - Success
840 **/
dea31012005-04-17 16:05:31 -0500841static int
James Smartc4908502019-01-28 11:14:28 -0800842lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
dea31012005-04-17 16:05:31 -0500843{
844 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
845 struct scatterlist *sgel = NULL;
846 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smartc4908502019-01-28 11:14:28 -0800847 struct ulp_bde64 *bpl = (struct ulp_bde64 *)lpfc_cmd->dma_sgl;
James Smart0f65ff62010-02-26 14:14:23 -0500848 struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
dea31012005-04-17 16:05:31 -0500849 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart34b02dc2008-08-24 21:49:55 -0400850 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
dea31012005-04-17 16:05:31 -0500851 dma_addr_t physaddr;
James Smart34b02dc2008-08-24 21:49:55 -0400852 uint32_t num_bde = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500853 int nseg, datadir = scsi_cmnd->sc_data_direction;
dea31012005-04-17 16:05:31 -0500854
855 /*
856 * There are three possibilities here - use scatter-gather segment, use
857 * the single mapping, or neither. Start the lpfc command prep by
858 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
859 * data bde entry.
860 */
861 bpl += 2;
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -0700862 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -0500863 /*
864 * The driver stores the segment count returned from pci_map_sg
865 * because this a count of dma-mappings used to map the use_sg
866 * pages. They are not guaranteed to be the same for those
867 * architectures that implement an IOMMU.
868 */
dea31012005-04-17 16:05:31 -0500869
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -0700870 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
871 scsi_sg_count(scsi_cmnd), datadir);
872 if (unlikely(!nseg))
873 return 1;
874
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500875 lpfc_cmd->seg_cnt = nseg;
dea31012005-04-17 16:05:31 -0500876 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
Dick Kennedy372c1872020-06-30 14:50:00 -0700877 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
878 "9064 BLKGRD: %s: Too many sg segments"
879 " from dma_map_sg. Config %d, seg_cnt"
880 " %d\n", __func__, phba->cfg_sg_seg_cnt,
881 lpfc_cmd->seg_cnt);
James Smart5e0e2312019-08-14 16:56:54 -0700882 WARN_ON_ONCE(lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt);
James Smart96f70772013-04-17 20:16:15 -0400883 lpfc_cmd->seg_cnt = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500884 scsi_dma_unmap(scsi_cmnd);
James Smart5e0e2312019-08-14 16:56:54 -0700885 return 2;
dea31012005-04-17 16:05:31 -0500886 }
887
888 /*
889 * The driver established a maximum scatter-gather segment count
890 * during probe that limits the number of sg elements in any
891 * single scsi command. Just run through the seg_cnt and format
892 * the bde's.
James Smart34b02dc2008-08-24 21:49:55 -0400893 * When using SLI-3 the driver will try to fit all the BDEs into
894 * the IOCB. If it can't then the BDEs get added to a BPL as it
895 * does for SLI-2 mode.
dea31012005-04-17 16:05:31 -0500896 */
James Smart34b02dc2008-08-24 21:49:55 -0400897 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
dea31012005-04-17 16:05:31 -0500898 physaddr = sg_dma_address(sgel);
James Smart34b02dc2008-08-24 21:49:55 -0400899 if (phba->sli_rev == 3 &&
James Smarte2a0a9d2008-12-04 22:40:02 -0500900 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
James Smart0f65ff62010-02-26 14:14:23 -0500901 !(iocbq->iocb_flag & DSS_SECURITY_OP) &&
James Smart34b02dc2008-08-24 21:49:55 -0400902 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
903 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
904 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
905 data_bde->addrLow = putPaddrLow(physaddr);
906 data_bde->addrHigh = putPaddrHigh(physaddr);
907 data_bde++;
908 } else {
909 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
910 bpl->tus.f.bdeSize = sg_dma_len(sgel);
911 bpl->tus.w = le32_to_cpu(bpl->tus.w);
912 bpl->addrLow =
913 le32_to_cpu(putPaddrLow(physaddr));
914 bpl->addrHigh =
915 le32_to_cpu(putPaddrHigh(physaddr));
916 bpl++;
917 }
dea31012005-04-17 16:05:31 -0500918 }
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -0700919 }
dea31012005-04-17 16:05:31 -0500920
921 /*
922 * Finish initializing those IOCB fields that are dependent on the
James Smart34b02dc2008-08-24 21:49:55 -0400923 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
924 * explicitly reinitialized and for SLI-3 the extended bde count is
925 * explicitly reinitialized since all iocb memory resources are reused.
dea31012005-04-17 16:05:31 -0500926 */
James Smarte2a0a9d2008-12-04 22:40:02 -0500927 if (phba->sli_rev == 3 &&
James Smart0f65ff62010-02-26 14:14:23 -0500928 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
929 !(iocbq->iocb_flag & DSS_SECURITY_OP)) {
James Smart34b02dc2008-08-24 21:49:55 -0400930 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
931 /*
932 * The extended IOCB format can only fit 3 BDE or a BPL.
933 * This I/O has more than 3 BDE so the 1st data bde will
934 * be a BPL that is filled in here.
935 */
936 physaddr = lpfc_cmd->dma_handle;
937 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
938 data_bde->tus.f.bdeSize = (num_bde *
939 sizeof(struct ulp_bde64));
940 physaddr += (sizeof(struct fcp_cmnd) +
941 sizeof(struct fcp_rsp) +
942 (2 * sizeof(struct ulp_bde64)));
943 data_bde->addrHigh = putPaddrHigh(physaddr);
944 data_bde->addrLow = putPaddrLow(physaddr);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300945 /* ebde count includes the response bde and data bpl */
James Smart34b02dc2008-08-24 21:49:55 -0400946 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
947 } else {
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300948 /* ebde count includes the response bde and data bdes */
James Smart34b02dc2008-08-24 21:49:55 -0400949 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
950 }
951 } else {
952 iocb_cmd->un.fcpi64.bdl.bdeSize =
953 ((num_bde + 2) * sizeof(struct ulp_bde64));
James Smart0f65ff62010-02-26 14:14:23 -0500954 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
James Smart34b02dc2008-08-24 21:49:55 -0400955 }
James Smart09372822008-01-11 01:52:54 -0500956 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
James Smarte2a0a9d2008-12-04 22:40:02 -0500957
958 /*
959 * Due to difference in data length between DIF/non-DIF paths,
960 * we need to set word 4 of IOCB here
961 */
James Smarta257bf92009-04-06 18:48:10 -0400962 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
dea31012005-04-17 16:05:31 -0500963 return 0;
964}
965
James Smartf9bb2da2011-10-10 21:34:11 -0400966#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -0500967
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -0700968/* Return BG_ERR_INIT if error injection is detected by Initiator */
James Smart9a6b09c2012-03-01 22:37:42 -0500969#define BG_ERR_INIT 0x1
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -0700970/* Return BG_ERR_TGT if error injection is detected by Target */
James Smart9a6b09c2012-03-01 22:37:42 -0500971#define BG_ERR_TGT 0x2
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -0700972/* Return BG_ERR_SWAP if swapping CSUM<-->CRC is required for error injection */
James Smart9a6b09c2012-03-01 22:37:42 -0500973#define BG_ERR_SWAP 0x10
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -0700974/**
975 * Return BG_ERR_CHECK if disabling Guard/Ref/App checking is required for
976 * error injection
977 **/
James Smart9a6b09c2012-03-01 22:37:42 -0500978#define BG_ERR_CHECK 0x20
James Smartacd68592012-01-18 16:25:09 -0500979
980/**
981 * lpfc_bg_err_inject - Determine if we should inject an error
982 * @phba: The Hba for which this call is being executed.
James Smartf9bb2da2011-10-10 21:34:11 -0400983 * @sc: The SCSI command to examine
984 * @reftag: (out) BlockGuard reference tag for transmitted data
985 * @apptag: (out) BlockGuard application tag for transmitted data
Lee Joneseceee002020-11-02 14:23:43 +0000986 * @new_guard: (in) Value to replace CRC with if needed
James Smartf9bb2da2011-10-10 21:34:11 -0400987 *
James Smart9a6b09c2012-03-01 22:37:42 -0500988 * Returns BG_ERR_* bit mask or 0 if request ignored
James Smartacd68592012-01-18 16:25:09 -0500989 **/
James Smartf9bb2da2011-10-10 21:34:11 -0400990static int
991lpfc_bg_err_inject(struct lpfc_hba *phba, struct scsi_cmnd *sc,
992 uint32_t *reftag, uint16_t *apptag, uint32_t new_guard)
993{
994 struct scatterlist *sgpe; /* s/g prot entry */
James Smartc4908502019-01-28 11:14:28 -0800995 struct lpfc_io_buf *lpfc_cmd = NULL;
James Smartacd68592012-01-18 16:25:09 -0500996 struct scsi_dif_tuple *src = NULL;
James Smart4ac9b222012-03-01 22:38:29 -0500997 struct lpfc_nodelist *ndlp;
998 struct lpfc_rport_data *rdata;
James Smartf9bb2da2011-10-10 21:34:11 -0400999 uint32_t op = scsi_get_prot_op(sc);
1000 uint32_t blksize;
1001 uint32_t numblks;
1002 sector_t lba;
1003 int rc = 0;
James Smartacd68592012-01-18 16:25:09 -05001004 int blockoff = 0;
James Smartf9bb2da2011-10-10 21:34:11 -04001005
1006 if (op == SCSI_PROT_NORMAL)
1007 return 0;
1008
James Smartacd68592012-01-18 16:25:09 -05001009 sgpe = scsi_prot_sglist(sc);
James Smartf9bb2da2011-10-10 21:34:11 -04001010 lba = scsi_get_lba(sc);
James Smart4ac9b222012-03-01 22:38:29 -05001011
1012 /* First check if we need to match the LBA */
James Smartf9bb2da2011-10-10 21:34:11 -04001013 if (phba->lpfc_injerr_lba != LPFC_INJERR_LBA_OFF) {
1014 blksize = lpfc_cmd_blksize(sc);
1015 numblks = (scsi_bufflen(sc) + blksize - 1) / blksize;
1016
1017 /* Make sure we have the right LBA if one is specified */
1018 if ((phba->lpfc_injerr_lba < lba) ||
1019 (phba->lpfc_injerr_lba >= (lba + numblks)))
1020 return 0;
James Smartacd68592012-01-18 16:25:09 -05001021 if (sgpe) {
1022 blockoff = phba->lpfc_injerr_lba - lba;
1023 numblks = sg_dma_len(sgpe) /
1024 sizeof(struct scsi_dif_tuple);
1025 if (numblks < blockoff)
1026 blockoff = numblks;
James Smartacd68592012-01-18 16:25:09 -05001027 }
James Smartf9bb2da2011-10-10 21:34:11 -04001028 }
1029
James Smart4ac9b222012-03-01 22:38:29 -05001030 /* Next check if we need to match the remote NPortID or WWPN */
James Smart1ba981f2014-02-20 09:56:45 -05001031 rdata = lpfc_rport_data_from_scsi_device(sc->device);
James Smart4ac9b222012-03-01 22:38:29 -05001032 if (rdata && rdata->pnode) {
1033 ndlp = rdata->pnode;
1034
1035 /* Make sure we have the right NPortID if one is specified */
1036 if (phba->lpfc_injerr_nportid &&
1037 (phba->lpfc_injerr_nportid != ndlp->nlp_DID))
1038 return 0;
1039
1040 /*
1041 * Make sure we have the right WWPN if one is specified.
1042 * wwn[0] should be a non-zero NAA in a good WWPN.
1043 */
1044 if (phba->lpfc_injerr_wwpn.u.wwn[0] &&
1045 (memcmp(&ndlp->nlp_portname, &phba->lpfc_injerr_wwpn,
1046 sizeof(struct lpfc_name)) != 0))
1047 return 0;
1048 }
1049
1050 /* Setup a ptr to the protection data if the SCSI host provides it */
1051 if (sgpe) {
1052 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
1053 src += blockoff;
James Smartc4908502019-01-28 11:14:28 -08001054 lpfc_cmd = (struct lpfc_io_buf *)sc->host_scribble;
James Smart4ac9b222012-03-01 22:38:29 -05001055 }
1056
James Smartf9bb2da2011-10-10 21:34:11 -04001057 /* Should we change the Reference Tag */
1058 if (reftag) {
James Smartacd68592012-01-18 16:25:09 -05001059 if (phba->lpfc_injerr_wref_cnt) {
1060 switch (op) {
1061 case SCSI_PROT_WRITE_PASS:
James Smart9a6b09c2012-03-01 22:37:42 -05001062 if (src) {
1063 /*
1064 * For WRITE_PASS, force the error
1065 * to be sent on the wire. It should
1066 * be detected by the Target.
1067 * If blockoff != 0 error will be
1068 * inserted in middle of the IO.
1069 */
James Smartf9bb2da2011-10-10 21:34:11 -04001070
Dick Kennedy372c1872020-06-30 14:50:00 -07001071 lpfc_printf_log(phba, KERN_ERR,
1072 LOG_TRACE_EVENT,
James Smartacd68592012-01-18 16:25:09 -05001073 "9076 BLKGRD: Injecting reftag error: "
1074 "write lba x%lx + x%x oldrefTag x%x\n",
1075 (unsigned long)lba, blockoff,
James Smart9a6b09c2012-03-01 22:37:42 -05001076 be32_to_cpu(src->ref_tag));
James Smartacd68592012-01-18 16:25:09 -05001077
1078 /*
James Smart9a6b09c2012-03-01 22:37:42 -05001079 * Save the old ref_tag so we can
1080 * restore it on completion.
James Smartacd68592012-01-18 16:25:09 -05001081 */
James Smart9a6b09c2012-03-01 22:37:42 -05001082 if (lpfc_cmd) {
1083 lpfc_cmd->prot_data_type =
1084 LPFC_INJERR_REFTAG;
1085 lpfc_cmd->prot_data_segment =
1086 src;
1087 lpfc_cmd->prot_data =
1088 src->ref_tag;
1089 }
1090 src->ref_tag = cpu_to_be32(0xDEADBEEF);
James Smartacd68592012-01-18 16:25:09 -05001091 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001092 if (phba->lpfc_injerr_wref_cnt == 0) {
1093 phba->lpfc_injerr_nportid = 0;
1094 phba->lpfc_injerr_lba =
1095 LPFC_INJERR_LBA_OFF;
1096 memset(&phba->lpfc_injerr_wwpn,
1097 0, sizeof(struct lpfc_name));
1098 }
James Smart9a6b09c2012-03-01 22:37:42 -05001099 rc = BG_ERR_TGT | BG_ERR_CHECK;
1100
James Smartacd68592012-01-18 16:25:09 -05001101 break;
1102 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001103 fallthrough;
James Smart9a6b09c2012-03-01 22:37:42 -05001104 case SCSI_PROT_WRITE_INSERT:
1105 /*
1106 * For WRITE_INSERT, force the error
1107 * to be sent on the wire. It should be
1108 * detected by the Target.
1109 */
1110 /* DEADBEEF will be the reftag on the wire */
1111 *reftag = 0xDEADBEEF;
1112 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001113 if (phba->lpfc_injerr_wref_cnt == 0) {
1114 phba->lpfc_injerr_nportid = 0;
1115 phba->lpfc_injerr_lba =
1116 LPFC_INJERR_LBA_OFF;
1117 memset(&phba->lpfc_injerr_wwpn,
1118 0, sizeof(struct lpfc_name));
1119 }
James Smart9a6b09c2012-03-01 22:37:42 -05001120 rc = BG_ERR_TGT | BG_ERR_CHECK;
1121
Dick Kennedy372c1872020-06-30 14:50:00 -07001122 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart9a6b09c2012-03-01 22:37:42 -05001123 "9078 BLKGRD: Injecting reftag error: "
1124 "write lba x%lx\n", (unsigned long)lba);
1125 break;
James Smartacd68592012-01-18 16:25:09 -05001126 case SCSI_PROT_WRITE_STRIP:
1127 /*
1128 * For WRITE_STRIP and WRITE_PASS,
1129 * force the error on data
1130 * being copied from SLI-Host to SLI-Port.
1131 */
1132 *reftag = 0xDEADBEEF;
1133 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001134 if (phba->lpfc_injerr_wref_cnt == 0) {
1135 phba->lpfc_injerr_nportid = 0;
1136 phba->lpfc_injerr_lba =
1137 LPFC_INJERR_LBA_OFF;
1138 memset(&phba->lpfc_injerr_wwpn,
1139 0, sizeof(struct lpfc_name));
1140 }
James Smartacd68592012-01-18 16:25:09 -05001141 rc = BG_ERR_INIT;
1142
Dick Kennedy372c1872020-06-30 14:50:00 -07001143 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartacd68592012-01-18 16:25:09 -05001144 "9077 BLKGRD: Injecting reftag error: "
1145 "write lba x%lx\n", (unsigned long)lba);
1146 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001147 }
James Smartacd68592012-01-18 16:25:09 -05001148 }
1149 if (phba->lpfc_injerr_rref_cnt) {
1150 switch (op) {
1151 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001152 case SCSI_PROT_READ_STRIP:
1153 case SCSI_PROT_READ_PASS:
1154 /*
1155 * For READ_STRIP and READ_PASS, force the
1156 * error on data being read off the wire. It
1157 * should force an IO error to the driver.
1158 */
James Smartf9bb2da2011-10-10 21:34:11 -04001159 *reftag = 0xDEADBEEF;
1160 phba->lpfc_injerr_rref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001161 if (phba->lpfc_injerr_rref_cnt == 0) {
1162 phba->lpfc_injerr_nportid = 0;
1163 phba->lpfc_injerr_lba =
1164 LPFC_INJERR_LBA_OFF;
1165 memset(&phba->lpfc_injerr_wwpn,
1166 0, sizeof(struct lpfc_name));
1167 }
James Smartacd68592012-01-18 16:25:09 -05001168 rc = BG_ERR_INIT;
James Smartf9bb2da2011-10-10 21:34:11 -04001169
Dick Kennedy372c1872020-06-30 14:50:00 -07001170 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartacd68592012-01-18 16:25:09 -05001171 "9079 BLKGRD: Injecting reftag error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001172 "read lba x%lx\n", (unsigned long)lba);
James Smartacd68592012-01-18 16:25:09 -05001173 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001174 }
1175 }
1176 }
1177
1178 /* Should we change the Application Tag */
1179 if (apptag) {
James Smartacd68592012-01-18 16:25:09 -05001180 if (phba->lpfc_injerr_wapp_cnt) {
1181 switch (op) {
1182 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001183 if (src) {
James Smart9a6b09c2012-03-01 22:37:42 -05001184 /*
1185 * For WRITE_PASS, force the error
1186 * to be sent on the wire. It should
1187 * be detected by the Target.
1188 * If blockoff != 0 error will be
1189 * inserted in middle of the IO.
1190 */
1191
Dick Kennedy372c1872020-06-30 14:50:00 -07001192 lpfc_printf_log(phba, KERN_ERR,
1193 LOG_TRACE_EVENT,
James Smartacd68592012-01-18 16:25:09 -05001194 "9080 BLKGRD: Injecting apptag error: "
1195 "write lba x%lx + x%x oldappTag x%x\n",
1196 (unsigned long)lba, blockoff,
James Smart9a6b09c2012-03-01 22:37:42 -05001197 be16_to_cpu(src->app_tag));
James Smartacd68592012-01-18 16:25:09 -05001198
1199 /*
James Smart9a6b09c2012-03-01 22:37:42 -05001200 * Save the old app_tag so we can
1201 * restore it on completion.
James Smartacd68592012-01-18 16:25:09 -05001202 */
James Smart9a6b09c2012-03-01 22:37:42 -05001203 if (lpfc_cmd) {
1204 lpfc_cmd->prot_data_type =
1205 LPFC_INJERR_APPTAG;
1206 lpfc_cmd->prot_data_segment =
1207 src;
1208 lpfc_cmd->prot_data =
1209 src->app_tag;
1210 }
1211 src->app_tag = cpu_to_be16(0xDEAD);
James Smartacd68592012-01-18 16:25:09 -05001212 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001213 if (phba->lpfc_injerr_wapp_cnt == 0) {
1214 phba->lpfc_injerr_nportid = 0;
1215 phba->lpfc_injerr_lba =
1216 LPFC_INJERR_LBA_OFF;
1217 memset(&phba->lpfc_injerr_wwpn,
1218 0, sizeof(struct lpfc_name));
1219 }
James Smart9a6b09c2012-03-01 22:37:42 -05001220 rc = BG_ERR_TGT | BG_ERR_CHECK;
James Smartacd68592012-01-18 16:25:09 -05001221 break;
1222 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001223 fallthrough;
James Smart9a6b09c2012-03-01 22:37:42 -05001224 case SCSI_PROT_WRITE_INSERT:
1225 /*
1226 * For WRITE_INSERT, force the
1227 * error to be sent on the wire. It should be
1228 * detected by the Target.
1229 */
1230 /* DEAD will be the apptag on the wire */
1231 *apptag = 0xDEAD;
1232 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001233 if (phba->lpfc_injerr_wapp_cnt == 0) {
1234 phba->lpfc_injerr_nportid = 0;
1235 phba->lpfc_injerr_lba =
1236 LPFC_INJERR_LBA_OFF;
1237 memset(&phba->lpfc_injerr_wwpn,
1238 0, sizeof(struct lpfc_name));
1239 }
James Smart9a6b09c2012-03-01 22:37:42 -05001240 rc = BG_ERR_TGT | BG_ERR_CHECK;
1241
Dick Kennedy372c1872020-06-30 14:50:00 -07001242 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart9a6b09c2012-03-01 22:37:42 -05001243 "0813 BLKGRD: Injecting apptag error: "
1244 "write lba x%lx\n", (unsigned long)lba);
1245 break;
James Smartacd68592012-01-18 16:25:09 -05001246 case SCSI_PROT_WRITE_STRIP:
1247 /*
1248 * For WRITE_STRIP and WRITE_PASS,
1249 * force the error on data
1250 * being copied from SLI-Host to SLI-Port.
1251 */
1252 *apptag = 0xDEAD;
1253 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001254 if (phba->lpfc_injerr_wapp_cnt == 0) {
1255 phba->lpfc_injerr_nportid = 0;
1256 phba->lpfc_injerr_lba =
1257 LPFC_INJERR_LBA_OFF;
1258 memset(&phba->lpfc_injerr_wwpn,
1259 0, sizeof(struct lpfc_name));
1260 }
James Smartacd68592012-01-18 16:25:09 -05001261 rc = BG_ERR_INIT;
1262
Dick Kennedy372c1872020-06-30 14:50:00 -07001263 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartacd68592012-01-18 16:25:09 -05001264 "0812 BLKGRD: Injecting apptag error: "
1265 "write lba x%lx\n", (unsigned long)lba);
1266 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001267 }
James Smartacd68592012-01-18 16:25:09 -05001268 }
1269 if (phba->lpfc_injerr_rapp_cnt) {
1270 switch (op) {
1271 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001272 case SCSI_PROT_READ_STRIP:
1273 case SCSI_PROT_READ_PASS:
1274 /*
1275 * For READ_STRIP and READ_PASS, force the
1276 * error on data being read off the wire. It
1277 * should force an IO error to the driver.
1278 */
James Smartf9bb2da2011-10-10 21:34:11 -04001279 *apptag = 0xDEAD;
1280 phba->lpfc_injerr_rapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001281 if (phba->lpfc_injerr_rapp_cnt == 0) {
1282 phba->lpfc_injerr_nportid = 0;
1283 phba->lpfc_injerr_lba =
1284 LPFC_INJERR_LBA_OFF;
1285 memset(&phba->lpfc_injerr_wwpn,
1286 0, sizeof(struct lpfc_name));
1287 }
James Smartacd68592012-01-18 16:25:09 -05001288 rc = BG_ERR_INIT;
James Smartf9bb2da2011-10-10 21:34:11 -04001289
Dick Kennedy372c1872020-06-30 14:50:00 -07001290 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartacd68592012-01-18 16:25:09 -05001291 "0814 BLKGRD: Injecting apptag error: "
1292 "read lba x%lx\n", (unsigned long)lba);
1293 break;
1294 }
1295 }
1296 }
1297
1298
1299 /* Should we change the Guard Tag */
1300 if (new_guard) {
1301 if (phba->lpfc_injerr_wgrd_cnt) {
1302 switch (op) {
1303 case SCSI_PROT_WRITE_PASS:
James Smart9a6b09c2012-03-01 22:37:42 -05001304 rc = BG_ERR_CHECK;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001305 fallthrough;
James Smartacd68592012-01-18 16:25:09 -05001306
James Smartacd68592012-01-18 16:25:09 -05001307 case SCSI_PROT_WRITE_INSERT:
1308 /*
1309 * For WRITE_INSERT, force the
1310 * error to be sent on the wire. It should be
1311 * detected by the Target.
1312 */
1313 phba->lpfc_injerr_wgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001314 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1315 phba->lpfc_injerr_nportid = 0;
1316 phba->lpfc_injerr_lba =
1317 LPFC_INJERR_LBA_OFF;
1318 memset(&phba->lpfc_injerr_wwpn,
1319 0, sizeof(struct lpfc_name));
1320 }
James Smartacd68592012-01-18 16:25:09 -05001321
James Smart9a6b09c2012-03-01 22:37:42 -05001322 rc |= BG_ERR_TGT | BG_ERR_SWAP;
James Smartacd68592012-01-18 16:25:09 -05001323 /* Signals the caller to swap CRC->CSUM */
1324
Dick Kennedy372c1872020-06-30 14:50:00 -07001325 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartacd68592012-01-18 16:25:09 -05001326 "0817 BLKGRD: Injecting guard error: "
1327 "write lba x%lx\n", (unsigned long)lba);
1328 break;
James Smart9a6b09c2012-03-01 22:37:42 -05001329 case SCSI_PROT_WRITE_STRIP:
1330 /*
1331 * For WRITE_STRIP and WRITE_PASS,
1332 * force the error on data
1333 * being copied from SLI-Host to SLI-Port.
1334 */
1335 phba->lpfc_injerr_wgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001336 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1337 phba->lpfc_injerr_nportid = 0;
1338 phba->lpfc_injerr_lba =
1339 LPFC_INJERR_LBA_OFF;
1340 memset(&phba->lpfc_injerr_wwpn,
1341 0, sizeof(struct lpfc_name));
1342 }
James Smart9a6b09c2012-03-01 22:37:42 -05001343
1344 rc = BG_ERR_INIT | BG_ERR_SWAP;
1345 /* Signals the caller to swap CRC->CSUM */
1346
Dick Kennedy372c1872020-06-30 14:50:00 -07001347 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart9a6b09c2012-03-01 22:37:42 -05001348 "0816 BLKGRD: Injecting guard error: "
1349 "write lba x%lx\n", (unsigned long)lba);
1350 break;
James Smartacd68592012-01-18 16:25:09 -05001351 }
1352 }
1353 if (phba->lpfc_injerr_rgrd_cnt) {
1354 switch (op) {
1355 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001356 case SCSI_PROT_READ_STRIP:
1357 case SCSI_PROT_READ_PASS:
1358 /*
1359 * For READ_STRIP and READ_PASS, force the
1360 * error on data being read off the wire. It
1361 * should force an IO error to the driver.
1362 */
James Smartacd68592012-01-18 16:25:09 -05001363 phba->lpfc_injerr_rgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001364 if (phba->lpfc_injerr_rgrd_cnt == 0) {
1365 phba->lpfc_injerr_nportid = 0;
1366 phba->lpfc_injerr_lba =
1367 LPFC_INJERR_LBA_OFF;
1368 memset(&phba->lpfc_injerr_wwpn,
1369 0, sizeof(struct lpfc_name));
1370 }
James Smartacd68592012-01-18 16:25:09 -05001371
James Smart9a6b09c2012-03-01 22:37:42 -05001372 rc = BG_ERR_INIT | BG_ERR_SWAP;
James Smartacd68592012-01-18 16:25:09 -05001373 /* Signals the caller to swap CRC->CSUM */
1374
Dick Kennedy372c1872020-06-30 14:50:00 -07001375 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartacd68592012-01-18 16:25:09 -05001376 "0818 BLKGRD: Injecting guard error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001377 "read lba x%lx\n", (unsigned long)lba);
1378 }
1379 }
1380 }
1381
James Smartf9bb2da2011-10-10 21:34:11 -04001382 return rc;
1383}
1384#endif
1385
James Smartacd68592012-01-18 16:25:09 -05001386/**
1387 * lpfc_sc_to_bg_opcodes - Determine the BlockGuard opcodes to be used with
1388 * the specified SCSI command.
1389 * @phba: The Hba for which this call is being executed.
James Smart6c8eea52010-04-06 14:49:53 -04001390 * @sc: The SCSI command to examine
Lee Joneseceee002020-11-02 14:23:43 +00001391 * @txop: (out) BlockGuard operation for transmitted data
1392 * @rxop: (out) BlockGuard operation for received data
James Smart6c8eea52010-04-06 14:49:53 -04001393 *
1394 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1395 *
James Smartacd68592012-01-18 16:25:09 -05001396 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001397static int
James Smart6c8eea52010-04-06 14:49:53 -04001398lpfc_sc_to_bg_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1399 uint8_t *txop, uint8_t *rxop)
James Smarte2a0a9d2008-12-04 22:40:02 -05001400{
James Smart6c8eea52010-04-06 14:49:53 -04001401 uint8_t ret = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05001402
James Smart9c6aa9d2013-05-31 17:03:39 -04001403 if (lpfc_cmd_guard_csum(sc)) {
James Smarte2a0a9d2008-12-04 22:40:02 -05001404 switch (scsi_get_prot_op(sc)) {
1405 case SCSI_PROT_READ_INSERT:
1406 case SCSI_PROT_WRITE_STRIP:
James Smart6c8eea52010-04-06 14:49:53 -04001407 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001408 *txop = BG_OP_IN_CSUM_OUT_NODIF;
James Smarte2a0a9d2008-12-04 22:40:02 -05001409 break;
1410
1411 case SCSI_PROT_READ_STRIP:
1412 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001413 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001414 *txop = BG_OP_IN_NODIF_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001415 break;
1416
Martin K. Petersenc6af4042009-09-18 17:32:59 -04001417 case SCSI_PROT_READ_PASS:
1418 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001419 *rxop = BG_OP_IN_CRC_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001420 *txop = BG_OP_IN_CSUM_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001421 break;
1422
James Smarte2a0a9d2008-12-04 22:40:02 -05001423 case SCSI_PROT_NORMAL:
1424 default:
Dick Kennedy372c1872020-06-30 14:50:00 -07001425 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart7c56b9f2011-07-22 18:36:25 -04001426 "9063 BLKGRD: Bad op/guard:%d/IP combination\n",
1427 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001428 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001429 break;
1430
1431 }
James Smart7c56b9f2011-07-22 18:36:25 -04001432 } else {
James Smarte2a0a9d2008-12-04 22:40:02 -05001433 switch (scsi_get_prot_op(sc)) {
1434 case SCSI_PROT_READ_STRIP:
1435 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001436 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001437 *txop = BG_OP_IN_NODIF_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001438 break;
1439
1440 case SCSI_PROT_READ_PASS:
1441 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001442 *rxop = BG_OP_IN_CRC_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001443 *txop = BG_OP_IN_CRC_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001444 break;
1445
James Smarte2a0a9d2008-12-04 22:40:02 -05001446 case SCSI_PROT_READ_INSERT:
1447 case SCSI_PROT_WRITE_STRIP:
James Smart7c56b9f2011-07-22 18:36:25 -04001448 *rxop = BG_OP_IN_NODIF_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001449 *txop = BG_OP_IN_CRC_OUT_NODIF;
James Smart7c56b9f2011-07-22 18:36:25 -04001450 break;
1451
James Smarte2a0a9d2008-12-04 22:40:02 -05001452 case SCSI_PROT_NORMAL:
1453 default:
Dick Kennedy372c1872020-06-30 14:50:00 -07001454 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart7c56b9f2011-07-22 18:36:25 -04001455 "9075 BLKGRD: Bad op/guard:%d/CRC combination\n",
1456 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001457 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001458 break;
1459 }
James Smarte2a0a9d2008-12-04 22:40:02 -05001460 }
1461
James Smart6c8eea52010-04-06 14:49:53 -04001462 return ret;
James Smarte2a0a9d2008-12-04 22:40:02 -05001463}
1464
James Smartacd68592012-01-18 16:25:09 -05001465#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1466/**
1467 * lpfc_bg_err_opcodes - reDetermine the BlockGuard opcodes to be used with
1468 * the specified SCSI command in order to force a guard tag error.
1469 * @phba: The Hba for which this call is being executed.
1470 * @sc: The SCSI command to examine
Lee Joneseceee002020-11-02 14:23:43 +00001471 * @txop: (out) BlockGuard operation for transmitted data
1472 * @rxop: (out) BlockGuard operation for received data
James Smartacd68592012-01-18 16:25:09 -05001473 *
1474 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1475 *
1476 **/
1477static int
1478lpfc_bg_err_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1479 uint8_t *txop, uint8_t *rxop)
1480{
James Smartacd68592012-01-18 16:25:09 -05001481 uint8_t ret = 0;
1482
James Smart9c6aa9d2013-05-31 17:03:39 -04001483 if (lpfc_cmd_guard_csum(sc)) {
James Smartacd68592012-01-18 16:25:09 -05001484 switch (scsi_get_prot_op(sc)) {
1485 case SCSI_PROT_READ_INSERT:
1486 case SCSI_PROT_WRITE_STRIP:
James Smartacd68592012-01-18 16:25:09 -05001487 *rxop = BG_OP_IN_NODIF_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001488 *txop = BG_OP_IN_CRC_OUT_NODIF;
James Smartacd68592012-01-18 16:25:09 -05001489 break;
1490
1491 case SCSI_PROT_READ_STRIP:
1492 case SCSI_PROT_WRITE_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001493 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001494 *txop = BG_OP_IN_NODIF_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001495 break;
1496
1497 case SCSI_PROT_READ_PASS:
1498 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001499 *rxop = BG_OP_IN_CSUM_OUT_CRC;
James Smart9a6b09c2012-03-01 22:37:42 -05001500 *txop = BG_OP_IN_CRC_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001501 break;
1502
1503 case SCSI_PROT_NORMAL:
1504 default:
1505 break;
1506
1507 }
1508 } else {
1509 switch (scsi_get_prot_op(sc)) {
1510 case SCSI_PROT_READ_STRIP:
1511 case SCSI_PROT_WRITE_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001512 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001513 *txop = BG_OP_IN_NODIF_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001514 break;
1515
1516 case SCSI_PROT_READ_PASS:
1517 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001518 *rxop = BG_OP_IN_CSUM_OUT_CSUM;
James Smart9a6b09c2012-03-01 22:37:42 -05001519 *txop = BG_OP_IN_CSUM_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001520 break;
1521
1522 case SCSI_PROT_READ_INSERT:
1523 case SCSI_PROT_WRITE_STRIP:
James Smartacd68592012-01-18 16:25:09 -05001524 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001525 *txop = BG_OP_IN_CSUM_OUT_NODIF;
James Smartacd68592012-01-18 16:25:09 -05001526 break;
1527
1528 case SCSI_PROT_NORMAL:
1529 default:
1530 break;
1531 }
1532 }
1533
1534 return ret;
1535}
1536#endif
1537
1538/**
1539 * lpfc_bg_setup_bpl - Setup BlockGuard BPL with no protection data
1540 * @phba: The Hba for which this call is being executed.
1541 * @sc: pointer to scsi command we're working on
1542 * @bpl: pointer to buffer list for protection groups
Lee Joneseceee002020-11-02 14:23:43 +00001543 * @datasegcnt: number of segments of data that have been dma mapped
James Smartacd68592012-01-18 16:25:09 -05001544 *
1545 * This function sets up BPL buffer list for protection groups of
James Smarte2a0a9d2008-12-04 22:40:02 -05001546 * type LPFC_PG_TYPE_NO_DIF
1547 *
1548 * This is usually used when the HBA is instructed to generate
1549 * DIFs and insert them into data stream (or strip DIF from
1550 * incoming data stream)
1551 *
1552 * The buffer list consists of just one protection group described
1553 * below:
1554 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001555 * start of prot group --> | PDE_5 |
1556 * +-------------------------+
1557 * | PDE_6 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001558 * +-------------------------+
1559 * | Data BDE |
1560 * +-------------------------+
1561 * |more Data BDE's ... (opt)|
1562 * +-------------------------+
1563 *
James Smarte2a0a9d2008-12-04 22:40:02 -05001564 *
1565 * Note: Data s/g buffers have been dma mapped
James Smartacd68592012-01-18 16:25:09 -05001566 *
1567 * Returns the number of BDEs added to the BPL.
1568 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001569static int
1570lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1571 struct ulp_bde64 *bpl, int datasegcnt)
1572{
1573 struct scatterlist *sgde = NULL; /* s/g data entry */
James Smart6c8eea52010-04-06 14:49:53 -04001574 struct lpfc_pde5 *pde5 = NULL;
1575 struct lpfc_pde6 *pde6 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05001576 dma_addr_t physaddr;
James Smart6c8eea52010-04-06 14:49:53 -04001577 int i = 0, num_bde = 0, status;
James Smarte2a0a9d2008-12-04 22:40:02 -05001578 int datadir = sc->sc_data_direction;
James Smart0829a192012-05-09 21:18:12 -04001579#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001580 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04001581#endif
James Smartacd68592012-01-18 16:25:09 -05001582 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001583 uint32_t reftag;
James Smart6c8eea52010-04-06 14:49:53 -04001584 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05001585
James Smart6c8eea52010-04-06 14:49:53 -04001586 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1587 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05001588 goto out;
1589
James Smart6c8eea52010-04-06 14:49:53 -04001590 /* extract some info from the scsi command for pde*/
James Smartacd68592012-01-18 16:25:09 -05001591 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
James Smarte2a0a9d2008-12-04 22:40:02 -05001592
James Smartf9bb2da2011-10-10 21:34:11 -04001593#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05001594 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05001595 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05001596 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05001597 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05001598 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05001599 checking = 0;
1600 }
James Smartf9bb2da2011-10-10 21:34:11 -04001601#endif
1602
James Smart6c8eea52010-04-06 14:49:53 -04001603 /* setup PDE5 with what we have */
1604 pde5 = (struct lpfc_pde5 *) bpl;
1605 memset(pde5, 0, sizeof(struct lpfc_pde5));
1606 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05001607
James Smartbc739052010-08-04 16:11:18 -04001608 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04001609 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04001610 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04001611
James Smart6c8eea52010-04-06 14:49:53 -04001612 /* advance bpl and increment bde count */
1613 num_bde++;
1614 bpl++;
1615 pde6 = (struct lpfc_pde6 *) bpl;
1616
1617 /* setup PDE6 with the rest of the info */
1618 memset(pde6, 0, sizeof(struct lpfc_pde6));
1619 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
1620 bf_set(pde6_optx, pde6, txop);
1621 bf_set(pde6_oprx, pde6, rxop);
James Smarta6887e22013-04-17 20:18:07 -04001622
1623 /*
1624 * We only need to check the data on READs, for WRITEs
1625 * protection data is automatically generated, not checked.
1626 */
James Smart6c8eea52010-04-06 14:49:53 -04001627 if (datadir == DMA_FROM_DEVICE) {
James Smart9c6aa9d2013-05-31 17:03:39 -04001628 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
James Smarta6887e22013-04-17 20:18:07 -04001629 bf_set(pde6_ce, pde6, checking);
1630 else
1631 bf_set(pde6_ce, pde6, 0);
1632
James Smart9c6aa9d2013-05-31 17:03:39 -04001633 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04001634 bf_set(pde6_re, pde6, checking);
1635 else
1636 bf_set(pde6_re, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04001637 }
1638 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04001639 bf_set(pde6_ae, pde6, 0);
1640 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04001641
James Smartbc739052010-08-04 16:11:18 -04001642 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04001643 pde6->word0 = cpu_to_le32(pde6->word0);
1644 pde6->word1 = cpu_to_le32(pde6->word1);
1645 pde6->word2 = cpu_to_le32(pde6->word2);
1646
James Smart6c8eea52010-04-06 14:49:53 -04001647 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05001648 num_bde++;
1649 bpl++;
1650
1651 /* assumption: caller has already run dma_map_sg on command data */
1652 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
1653 physaddr = sg_dma_address(sgde);
1654 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
1655 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1656 bpl->tus.f.bdeSize = sg_dma_len(sgde);
1657 if (datadir == DMA_TO_DEVICE)
1658 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1659 else
1660 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1661 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1662 bpl++;
1663 num_bde++;
1664 }
1665
1666out:
1667 return num_bde;
1668}
1669
James Smartacd68592012-01-18 16:25:09 -05001670/**
1671 * lpfc_bg_setup_bpl_prot - Setup BlockGuard BPL with protection data
1672 * @phba: The Hba for which this call is being executed.
1673 * @sc: pointer to scsi command we're working on
1674 * @bpl: pointer to buffer list for protection groups
1675 * @datacnt: number of segments of data that have been dma mapped
1676 * @protcnt: number of segment of protection data that have been dma mapped
1677 *
1678 * This function sets up BPL buffer list for protection groups of
1679 * type LPFC_PG_TYPE_DIF
James Smarte2a0a9d2008-12-04 22:40:02 -05001680 *
1681 * This is usually used when DIFs are in their own buffers,
1682 * separate from the data. The HBA can then by instructed
1683 * to place the DIFs in the outgoing stream. For read operations,
1684 * The HBA could extract the DIFs and place it in DIF buffers.
1685 *
1686 * The buffer list for this type consists of one or more of the
1687 * protection groups described below:
1688 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001689 * start of first prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001690 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001691 * | PDE_6 |
1692 * +-------------------------+
1693 * | PDE_7 (Prot BDE) |
James Smarte2a0a9d2008-12-04 22:40:02 -05001694 * +-------------------------+
1695 * | Data BDE |
1696 * +-------------------------+
1697 * |more Data BDE's ... (opt)|
1698 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001699 * start of new prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001700 * +-------------------------+
1701 * | ... |
1702 * +-------------------------+
1703 *
James Smarte2a0a9d2008-12-04 22:40:02 -05001704 * Note: It is assumed that both data and protection s/g buffers have been
1705 * mapped for DMA
James Smartacd68592012-01-18 16:25:09 -05001706 *
1707 * Returns the number of BDEs added to the BPL.
1708 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001709static int
1710lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1711 struct ulp_bde64 *bpl, int datacnt, int protcnt)
1712{
1713 struct scatterlist *sgde = NULL; /* s/g data entry */
1714 struct scatterlist *sgpe = NULL; /* s/g prot entry */
James Smart6c8eea52010-04-06 14:49:53 -04001715 struct lpfc_pde5 *pde5 = NULL;
1716 struct lpfc_pde6 *pde6 = NULL;
James Smart7f860592011-03-11 16:05:52 -05001717 struct lpfc_pde7 *pde7 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05001718 dma_addr_t dataphysaddr, protphysaddr;
1719 unsigned short curr_data = 0, curr_prot = 0;
James Smart7f860592011-03-11 16:05:52 -05001720 unsigned int split_offset;
1721 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
James Smarte2a0a9d2008-12-04 22:40:02 -05001722 unsigned int protgrp_blks, protgrp_bytes;
1723 unsigned int remainder, subtotal;
James Smart6c8eea52010-04-06 14:49:53 -04001724 int status;
James Smarte2a0a9d2008-12-04 22:40:02 -05001725 int datadir = sc->sc_data_direction;
1726 unsigned char pgdone = 0, alldone = 0;
1727 unsigned blksize;
James Smart0829a192012-05-09 21:18:12 -04001728#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001729 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04001730#endif
James Smartacd68592012-01-18 16:25:09 -05001731 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001732 uint32_t reftag;
James Smart6c8eea52010-04-06 14:49:53 -04001733 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05001734 int num_bde = 0;
1735
1736 sgpe = scsi_prot_sglist(sc);
1737 sgde = scsi_sglist(sc);
1738
1739 if (!sgpe || !sgde) {
Dick Kennedy372c1872020-06-30 14:50:00 -07001740 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart32350662019-08-14 16:57:06 -07001741 "9020 Invalid s/g entry: data=x%px prot=x%px\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001742 sgpe, sgde);
1743 return 0;
1744 }
1745
James Smart6c8eea52010-04-06 14:49:53 -04001746 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1747 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05001748 goto out;
1749
James Smart6c8eea52010-04-06 14:49:53 -04001750 /* extract some info from the scsi command */
James Smarte2a0a9d2008-12-04 22:40:02 -05001751 blksize = lpfc_cmd_blksize(sc);
James Smartacd68592012-01-18 16:25:09 -05001752 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
James Smarte2a0a9d2008-12-04 22:40:02 -05001753
James Smartf9bb2da2011-10-10 21:34:11 -04001754#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05001755 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05001756 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05001757 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05001758 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05001759 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05001760 checking = 0;
1761 }
James Smartf9bb2da2011-10-10 21:34:11 -04001762#endif
1763
James Smarte2a0a9d2008-12-04 22:40:02 -05001764 split_offset = 0;
1765 do {
James Smart96f70772013-04-17 20:16:15 -04001766 /* Check to see if we ran out of space */
1767 if (num_bde >= (phba->cfg_total_seg_cnt - 2))
1768 return num_bde + 3;
1769
James Smart6c8eea52010-04-06 14:49:53 -04001770 /* setup PDE5 with what we have */
1771 pde5 = (struct lpfc_pde5 *) bpl;
1772 memset(pde5, 0, sizeof(struct lpfc_pde5));
1773 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05001774
James Smartbc739052010-08-04 16:11:18 -04001775 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04001776 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04001777 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04001778
James Smart6c8eea52010-04-06 14:49:53 -04001779 /* advance bpl and increment bde count */
1780 num_bde++;
1781 bpl++;
1782 pde6 = (struct lpfc_pde6 *) bpl;
James Smarte2a0a9d2008-12-04 22:40:02 -05001783
James Smart6c8eea52010-04-06 14:49:53 -04001784 /* setup PDE6 with the rest of the info */
1785 memset(pde6, 0, sizeof(struct lpfc_pde6));
1786 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
1787 bf_set(pde6_optx, pde6, txop);
1788 bf_set(pde6_oprx, pde6, rxop);
James Smarta6887e22013-04-17 20:18:07 -04001789
James Smart9c6aa9d2013-05-31 17:03:39 -04001790 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
James Smarta6887e22013-04-17 20:18:07 -04001791 bf_set(pde6_ce, pde6, checking);
1792 else
1793 bf_set(pde6_ce, pde6, 0);
1794
James Smart9c6aa9d2013-05-31 17:03:39 -04001795 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04001796 bf_set(pde6_re, pde6, checking);
1797 else
1798 bf_set(pde6_re, pde6, 0);
1799
James Smart6c8eea52010-04-06 14:49:53 -04001800 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04001801 bf_set(pde6_ae, pde6, 0);
1802 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04001803
James Smartbc739052010-08-04 16:11:18 -04001804 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04001805 pde6->word0 = cpu_to_le32(pde6->word0);
1806 pde6->word1 = cpu_to_le32(pde6->word1);
1807 pde6->word2 = cpu_to_le32(pde6->word2);
1808
James Smart6c8eea52010-04-06 14:49:53 -04001809 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05001810 num_bde++;
1811 bpl++;
1812
1813 /* setup the first BDE that points to protection buffer */
James Smart7f860592011-03-11 16:05:52 -05001814 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
1815 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
James Smarte2a0a9d2008-12-04 22:40:02 -05001816
James Smarte2a0a9d2008-12-04 22:40:02 -05001817 /* must be integer multiple of the DIF block length */
1818 BUG_ON(protgroup_len % 8);
1819
James Smart7f860592011-03-11 16:05:52 -05001820 pde7 = (struct lpfc_pde7 *) bpl;
1821 memset(pde7, 0, sizeof(struct lpfc_pde7));
1822 bf_set(pde7_type, pde7, LPFC_PDE7_DESCRIPTOR);
1823
James Smart7c56b9f2011-07-22 18:36:25 -04001824 pde7->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
1825 pde7->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
James Smart7f860592011-03-11 16:05:52 -05001826
James Smarte2a0a9d2008-12-04 22:40:02 -05001827 protgrp_blks = protgroup_len / 8;
1828 protgrp_bytes = protgrp_blks * blksize;
1829
James Smart7f860592011-03-11 16:05:52 -05001830 /* check if this pde is crossing the 4K boundary; if so split */
1831 if ((pde7->addrLow & 0xfff) + protgroup_len > 0x1000) {
1832 protgroup_remainder = 0x1000 - (pde7->addrLow & 0xfff);
1833 protgroup_offset += protgroup_remainder;
1834 protgrp_blks = protgroup_remainder / 8;
James Smart7c56b9f2011-07-22 18:36:25 -04001835 protgrp_bytes = protgrp_blks * blksize;
James Smart7f860592011-03-11 16:05:52 -05001836 } else {
1837 protgroup_offset = 0;
1838 curr_prot++;
1839 }
James Smarte2a0a9d2008-12-04 22:40:02 -05001840
James Smarte2a0a9d2008-12-04 22:40:02 -05001841 num_bde++;
1842
1843 /* setup BDE's for data blocks associated with DIF data */
1844 pgdone = 0;
1845 subtotal = 0; /* total bytes processed for current prot grp */
1846 while (!pgdone) {
James Smart96f70772013-04-17 20:16:15 -04001847 /* Check to see if we ran out of space */
1848 if (num_bde >= phba->cfg_total_seg_cnt)
1849 return num_bde + 1;
1850
James Smarte2a0a9d2008-12-04 22:40:02 -05001851 if (!sgde) {
Dick Kennedy372c1872020-06-30 14:50:00 -07001852 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6a9c52c2009-10-02 15:16:51 -04001853 "9065 BLKGRD:%s Invalid data segment\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001854 __func__);
1855 return 0;
1856 }
1857 bpl++;
1858 dataphysaddr = sg_dma_address(sgde) + split_offset;
1859 bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
1860 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
1861
1862 remainder = sg_dma_len(sgde) - split_offset;
1863
1864 if ((subtotal + remainder) <= protgrp_bytes) {
1865 /* we can use this whole buffer */
1866 bpl->tus.f.bdeSize = remainder;
1867 split_offset = 0;
1868
1869 if ((subtotal + remainder) == protgrp_bytes)
1870 pgdone = 1;
1871 } else {
1872 /* must split this buffer with next prot grp */
1873 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
1874 split_offset += bpl->tus.f.bdeSize;
1875 }
1876
1877 subtotal += bpl->tus.f.bdeSize;
1878
1879 if (datadir == DMA_TO_DEVICE)
1880 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1881 else
1882 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1883 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1884
1885 num_bde++;
1886 curr_data++;
1887
1888 if (split_offset)
1889 break;
1890
1891 /* Move to the next s/g segment if possible */
1892 sgde = sg_next(sgde);
James Smart6c8eea52010-04-06 14:49:53 -04001893
James Smarte2a0a9d2008-12-04 22:40:02 -05001894 }
1895
James Smart7f860592011-03-11 16:05:52 -05001896 if (protgroup_offset) {
1897 /* update the reference tag */
1898 reftag += protgrp_blks;
1899 bpl++;
1900 continue;
1901 }
1902
James Smarte2a0a9d2008-12-04 22:40:02 -05001903 /* are we done ? */
1904 if (curr_prot == protcnt) {
1905 alldone = 1;
1906 } else if (curr_prot < protcnt) {
1907 /* advance to next prot buffer */
1908 sgpe = sg_next(sgpe);
1909 bpl++;
1910
1911 /* update the reference tag */
1912 reftag += protgrp_blks;
1913 } else {
1914 /* if we're here, we have a bug */
Dick Kennedy372c1872020-06-30 14:50:00 -07001915 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1916 "9054 BLKGRD: bug in %s\n", __func__);
James Smarte2a0a9d2008-12-04 22:40:02 -05001917 }
1918
1919 } while (!alldone);
James Smarte2a0a9d2008-12-04 22:40:02 -05001920out:
1921
James Smarte2a0a9d2008-12-04 22:40:02 -05001922 return num_bde;
1923}
James Smart7f860592011-03-11 16:05:52 -05001924
James Smartacd68592012-01-18 16:25:09 -05001925/**
1926 * lpfc_bg_setup_sgl - Setup BlockGuard SGL with no protection data
1927 * @phba: The Hba for which this call is being executed.
1928 * @sc: pointer to scsi command we're working on
1929 * @sgl: pointer to buffer list for protection groups
Lee Joneseceee002020-11-02 14:23:43 +00001930 * @datasegcnt: number of segments of data that have been dma mapped
1931 * @lpfc_cmd: lpfc scsi command object pointer.
James Smartacd68592012-01-18 16:25:09 -05001932 *
1933 * This function sets up SGL buffer list for protection groups of
1934 * type LPFC_PG_TYPE_NO_DIF
1935 *
1936 * This is usually used when the HBA is instructed to generate
1937 * DIFs and insert them into data stream (or strip DIF from
1938 * incoming data stream)
1939 *
1940 * The buffer list consists of just one protection group described
1941 * below:
1942 * +-------------------------+
1943 * start of prot group --> | DI_SEED |
1944 * +-------------------------+
1945 * | Data SGE |
1946 * +-------------------------+
1947 * |more Data SGE's ... (opt)|
1948 * +-------------------------+
1949 *
1950 *
1951 * Note: Data s/g buffers have been dma mapped
1952 *
1953 * Returns the number of SGEs added to the SGL.
1954 **/
1955static int
1956lpfc_bg_setup_sgl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
James Smartd79c9e92019-08-14 16:57:09 -07001957 struct sli4_sge *sgl, int datasegcnt,
1958 struct lpfc_io_buf *lpfc_cmd)
James Smartacd68592012-01-18 16:25:09 -05001959{
1960 struct scatterlist *sgde = NULL; /* s/g data entry */
1961 struct sli4_sge_diseed *diseed = NULL;
1962 dma_addr_t physaddr;
1963 int i = 0, num_sge = 0, status;
James Smartacd68592012-01-18 16:25:09 -05001964 uint32_t reftag;
James Smartacd68592012-01-18 16:25:09 -05001965 uint8_t txop, rxop;
James Smart0829a192012-05-09 21:18:12 -04001966#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001967 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04001968#endif
James Smartacd68592012-01-18 16:25:09 -05001969 uint32_t checking = 1;
1970 uint32_t dma_len;
1971 uint32_t dma_offset = 0;
James Smartd79c9e92019-08-14 16:57:09 -07001972 struct sli4_hybrid_sgl *sgl_xtra = NULL;
1973 int j;
1974 bool lsp_just_set = false;
James Smartacd68592012-01-18 16:25:09 -05001975
1976 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1977 if (status)
1978 goto out;
1979
1980 /* extract some info from the scsi command for pde*/
James Smartacd68592012-01-18 16:25:09 -05001981 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
1982
1983#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05001984 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05001985 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05001986 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05001987 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05001988 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05001989 checking = 0;
1990 }
1991#endif
1992
1993 /* setup DISEED with what we have */
1994 diseed = (struct sli4_sge_diseed *) sgl;
1995 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
1996 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
1997
1998 /* Endianness conversion if necessary */
1999 diseed->ref_tag = cpu_to_le32(reftag);
2000 diseed->ref_tag_tran = diseed->ref_tag;
2001
James Smarta6887e22013-04-17 20:18:07 -04002002 /*
2003 * We only need to check the data on READs, for WRITEs
2004 * protection data is automatically generated, not checked.
2005 */
2006 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
James Smart9c6aa9d2013-05-31 17:03:39 -04002007 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
James Smarta6887e22013-04-17 20:18:07 -04002008 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2009 else
2010 bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2011
James Smart9c6aa9d2013-05-31 17:03:39 -04002012 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04002013 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2014 else
2015 bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2016 }
2017
James Smartacd68592012-01-18 16:25:09 -05002018 /* setup DISEED with the rest of the info */
2019 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2020 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002021
James Smartacd68592012-01-18 16:25:09 -05002022 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2023 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2024
2025 /* Endianness conversion if necessary for DISEED */
2026 diseed->word2 = cpu_to_le32(diseed->word2);
2027 diseed->word3 = cpu_to_le32(diseed->word3);
2028
2029 /* advance bpl and increment sge count */
2030 num_sge++;
2031 sgl++;
2032
2033 /* assumption: caller has already run dma_map_sg on command data */
James Smartd79c9e92019-08-14 16:57:09 -07002034 sgde = scsi_sglist(sc);
2035 j = 3;
2036 for (i = 0; i < datasegcnt; i++) {
2037 /* clear it */
2038 sgl->word2 = 0;
James Smartacd68592012-01-18 16:25:09 -05002039
James Smartd79c9e92019-08-14 16:57:09 -07002040 /* do we need to expand the segment */
2041 if (!lsp_just_set && !((j + 1) % phba->border_sge_num) &&
2042 ((datasegcnt - 1) != i)) {
2043 /* set LSP type */
2044 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_LSP);
James Smartacd68592012-01-18 16:25:09 -05002045
James Smartd79c9e92019-08-14 16:57:09 -07002046 sgl_xtra = lpfc_get_sgl_per_hdwq(phba, lpfc_cmd);
2047
2048 if (unlikely(!sgl_xtra)) {
2049 lpfc_cmd->seg_cnt = 0;
2050 return 0;
2051 }
2052 sgl->addr_lo = cpu_to_le32(putPaddrLow(
2053 sgl_xtra->dma_phys_sgl));
2054 sgl->addr_hi = cpu_to_le32(putPaddrHigh(
2055 sgl_xtra->dma_phys_sgl));
2056
2057 } else {
2058 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2059 }
2060
2061 if (!(bf_get(lpfc_sli4_sge_type, sgl) & LPFC_SGE_TYPE_LSP)) {
2062 if ((datasegcnt - 1) == i)
2063 bf_set(lpfc_sli4_sge_last, sgl, 1);
2064 physaddr = sg_dma_address(sgde);
2065 dma_len = sg_dma_len(sgde);
2066 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
2067 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
2068
2069 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2070 sgl->word2 = cpu_to_le32(sgl->word2);
2071 sgl->sge_len = cpu_to_le32(dma_len);
2072
2073 dma_offset += dma_len;
2074 sgde = sg_next(sgde);
2075
2076 sgl++;
2077 num_sge++;
2078 lsp_just_set = false;
2079
2080 } else {
2081 sgl->word2 = cpu_to_le32(sgl->word2);
2082 sgl->sge_len = cpu_to_le32(phba->cfg_sg_dma_buf_size);
2083
2084 sgl = (struct sli4_sge *)sgl_xtra->dma_sgl;
2085 i = i - 1;
2086
2087 lsp_just_set = true;
2088 }
2089
2090 j++;
2091
James Smartacd68592012-01-18 16:25:09 -05002092 }
2093
2094out:
2095 return num_sge;
2096}
2097
2098/**
2099 * lpfc_bg_setup_sgl_prot - Setup BlockGuard SGL with protection data
2100 * @phba: The Hba for which this call is being executed.
2101 * @sc: pointer to scsi command we're working on
2102 * @sgl: pointer to buffer list for protection groups
2103 * @datacnt: number of segments of data that have been dma mapped
2104 * @protcnt: number of segment of protection data that have been dma mapped
Lee Joneseceee002020-11-02 14:23:43 +00002105 * @lpfc_cmd: lpfc scsi command object pointer.
James Smartacd68592012-01-18 16:25:09 -05002106 *
2107 * This function sets up SGL buffer list for protection groups of
2108 * type LPFC_PG_TYPE_DIF
2109 *
2110 * This is usually used when DIFs are in their own buffers,
2111 * separate from the data. The HBA can then by instructed
2112 * to place the DIFs in the outgoing stream. For read operations,
2113 * The HBA could extract the DIFs and place it in DIF buffers.
2114 *
2115 * The buffer list for this type consists of one or more of the
2116 * protection groups described below:
2117 * +-------------------------+
2118 * start of first prot group --> | DISEED |
2119 * +-------------------------+
2120 * | DIF (Prot SGE) |
2121 * +-------------------------+
2122 * | Data SGE |
2123 * +-------------------------+
2124 * |more Data SGE's ... (opt)|
2125 * +-------------------------+
2126 * start of new prot group --> | DISEED |
2127 * +-------------------------+
2128 * | ... |
2129 * +-------------------------+
2130 *
2131 * Note: It is assumed that both data and protection s/g buffers have been
2132 * mapped for DMA
2133 *
2134 * Returns the number of SGEs added to the SGL.
2135 **/
2136static int
2137lpfc_bg_setup_sgl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
James Smartd79c9e92019-08-14 16:57:09 -07002138 struct sli4_sge *sgl, int datacnt, int protcnt,
2139 struct lpfc_io_buf *lpfc_cmd)
James Smartacd68592012-01-18 16:25:09 -05002140{
2141 struct scatterlist *sgde = NULL; /* s/g data entry */
2142 struct scatterlist *sgpe = NULL; /* s/g prot entry */
2143 struct sli4_sge_diseed *diseed = NULL;
2144 dma_addr_t dataphysaddr, protphysaddr;
2145 unsigned short curr_data = 0, curr_prot = 0;
2146 unsigned int split_offset;
2147 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
2148 unsigned int protgrp_blks, protgrp_bytes;
2149 unsigned int remainder, subtotal;
2150 int status;
2151 unsigned char pgdone = 0, alldone = 0;
2152 unsigned blksize;
2153 uint32_t reftag;
2154 uint8_t txop, rxop;
2155 uint32_t dma_len;
James Smart0829a192012-05-09 21:18:12 -04002156#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002157 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002158#endif
James Smartacd68592012-01-18 16:25:09 -05002159 uint32_t checking = 1;
2160 uint32_t dma_offset = 0;
James Smartd79c9e92019-08-14 16:57:09 -07002161 int num_sge = 0, j = 2;
2162 struct sli4_hybrid_sgl *sgl_xtra = NULL;
James Smartacd68592012-01-18 16:25:09 -05002163
2164 sgpe = scsi_prot_sglist(sc);
2165 sgde = scsi_sglist(sc);
2166
2167 if (!sgpe || !sgde) {
Dick Kennedy372c1872020-06-30 14:50:00 -07002168 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart32350662019-08-14 16:57:06 -07002169 "9082 Invalid s/g entry: data=x%px prot=x%px\n",
James Smartacd68592012-01-18 16:25:09 -05002170 sgpe, sgde);
2171 return 0;
2172 }
2173
2174 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2175 if (status)
2176 goto out;
2177
2178 /* extract some info from the scsi command */
2179 blksize = lpfc_cmd_blksize(sc);
2180 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2181
2182#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002183 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002184 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002185 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002186 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002187 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002188 checking = 0;
2189 }
2190#endif
2191
2192 split_offset = 0;
2193 do {
James Smart96f70772013-04-17 20:16:15 -04002194 /* Check to see if we ran out of space */
James Smartd79c9e92019-08-14 16:57:09 -07002195 if ((num_sge >= (phba->cfg_total_seg_cnt - 2)) &&
2196 !(phba->cfg_xpsgl))
James Smart96f70772013-04-17 20:16:15 -04002197 return num_sge + 3;
2198
James Smartd79c9e92019-08-14 16:57:09 -07002199 /* DISEED and DIF have to be together */
2200 if (!((j + 1) % phba->border_sge_num) ||
2201 !((j + 2) % phba->border_sge_num) ||
2202 !((j + 3) % phba->border_sge_num)) {
2203 sgl->word2 = 0;
2204
2205 /* set LSP type */
2206 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_LSP);
2207
2208 sgl_xtra = lpfc_get_sgl_per_hdwq(phba, lpfc_cmd);
2209
2210 if (unlikely(!sgl_xtra)) {
2211 goto out;
2212 } else {
2213 sgl->addr_lo = cpu_to_le32(putPaddrLow(
2214 sgl_xtra->dma_phys_sgl));
2215 sgl->addr_hi = cpu_to_le32(putPaddrHigh(
2216 sgl_xtra->dma_phys_sgl));
2217 }
2218
2219 sgl->word2 = cpu_to_le32(sgl->word2);
2220 sgl->sge_len = cpu_to_le32(phba->cfg_sg_dma_buf_size);
2221
2222 sgl = (struct sli4_sge *)sgl_xtra->dma_sgl;
2223 j = 0;
2224 }
2225
James Smartacd68592012-01-18 16:25:09 -05002226 /* setup DISEED with what we have */
2227 diseed = (struct sli4_sge_diseed *) sgl;
2228 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2229 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2230
2231 /* Endianness conversion if necessary */
2232 diseed->ref_tag = cpu_to_le32(reftag);
2233 diseed->ref_tag_tran = diseed->ref_tag;
2234
James Smart9c6aa9d2013-05-31 17:03:39 -04002235 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD)) {
James Smarta6887e22013-04-17 20:18:07 -04002236 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2237
2238 } else {
2239 bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2240 /*
2241 * When in this mode, the hardware will replace
2242 * the guard tag from the host with a
2243 * newly generated good CRC for the wire.
2244 * Switch to raw mode here to avoid this
2245 * behavior. What the host sends gets put on the wire.
2246 */
2247 if (txop == BG_OP_IN_CRC_OUT_CRC) {
2248 txop = BG_OP_RAW_MODE;
2249 rxop = BG_OP_RAW_MODE;
2250 }
2251 }
2252
2253
James Smart9c6aa9d2013-05-31 17:03:39 -04002254 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04002255 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2256 else
2257 bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2258
James Smartacd68592012-01-18 16:25:09 -05002259 /* setup DISEED with the rest of the info */
2260 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2261 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002262
James Smartacd68592012-01-18 16:25:09 -05002263 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2264 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2265
2266 /* Endianness conversion if necessary for DISEED */
2267 diseed->word2 = cpu_to_le32(diseed->word2);
2268 diseed->word3 = cpu_to_le32(diseed->word3);
2269
2270 /* advance sgl and increment bde count */
2271 num_sge++;
James Smartd79c9e92019-08-14 16:57:09 -07002272
James Smartacd68592012-01-18 16:25:09 -05002273 sgl++;
James Smartd79c9e92019-08-14 16:57:09 -07002274 j++;
James Smartacd68592012-01-18 16:25:09 -05002275
2276 /* setup the first BDE that points to protection buffer */
2277 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2278 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
2279
2280 /* must be integer multiple of the DIF block length */
2281 BUG_ON(protgroup_len % 8);
2282
2283 /* Now setup DIF SGE */
2284 sgl->word2 = 0;
2285 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DIF);
2286 sgl->addr_hi = le32_to_cpu(putPaddrHigh(protphysaddr));
2287 sgl->addr_lo = le32_to_cpu(putPaddrLow(protphysaddr));
2288 sgl->word2 = cpu_to_le32(sgl->word2);
James Smartd79c9e92019-08-14 16:57:09 -07002289 sgl->sge_len = 0;
James Smartacd68592012-01-18 16:25:09 -05002290
2291 protgrp_blks = protgroup_len / 8;
2292 protgrp_bytes = protgrp_blks * blksize;
2293
2294 /* check if DIF SGE is crossing the 4K boundary; if so split */
2295 if ((sgl->addr_lo & 0xfff) + protgroup_len > 0x1000) {
2296 protgroup_remainder = 0x1000 - (sgl->addr_lo & 0xfff);
2297 protgroup_offset += protgroup_remainder;
2298 protgrp_blks = protgroup_remainder / 8;
2299 protgrp_bytes = protgrp_blks * blksize;
2300 } else {
2301 protgroup_offset = 0;
2302 curr_prot++;
2303 }
2304
2305 num_sge++;
2306
2307 /* setup SGE's for data blocks associated with DIF data */
2308 pgdone = 0;
2309 subtotal = 0; /* total bytes processed for current prot grp */
James Smartd79c9e92019-08-14 16:57:09 -07002310
2311 sgl++;
2312 j++;
2313
James Smartacd68592012-01-18 16:25:09 -05002314 while (!pgdone) {
James Smart96f70772013-04-17 20:16:15 -04002315 /* Check to see if we ran out of space */
James Smartd79c9e92019-08-14 16:57:09 -07002316 if ((num_sge >= phba->cfg_total_seg_cnt) &&
2317 !phba->cfg_xpsgl)
James Smart96f70772013-04-17 20:16:15 -04002318 return num_sge + 1;
2319
James Smartacd68592012-01-18 16:25:09 -05002320 if (!sgde) {
Dick Kennedy372c1872020-06-30 14:50:00 -07002321 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartacd68592012-01-18 16:25:09 -05002322 "9086 BLKGRD:%s Invalid data segment\n",
2323 __func__);
2324 return 0;
2325 }
James Smartacd68592012-01-18 16:25:09 -05002326
James Smartd79c9e92019-08-14 16:57:09 -07002327 if (!((j + 1) % phba->border_sge_num)) {
2328 sgl->word2 = 0;
James Smartacd68592012-01-18 16:25:09 -05002329
James Smartd79c9e92019-08-14 16:57:09 -07002330 /* set LSP type */
2331 bf_set(lpfc_sli4_sge_type, sgl,
2332 LPFC_SGE_TYPE_LSP);
James Smartacd68592012-01-18 16:25:09 -05002333
James Smartd79c9e92019-08-14 16:57:09 -07002334 sgl_xtra = lpfc_get_sgl_per_hdwq(phba,
2335 lpfc_cmd);
2336
2337 if (unlikely(!sgl_xtra)) {
2338 goto out;
2339 } else {
2340 sgl->addr_lo = cpu_to_le32(
2341 putPaddrLow(sgl_xtra->dma_phys_sgl));
2342 sgl->addr_hi = cpu_to_le32(
2343 putPaddrHigh(sgl_xtra->dma_phys_sgl));
2344 }
2345
2346 sgl->word2 = cpu_to_le32(sgl->word2);
2347 sgl->sge_len = cpu_to_le32(
2348 phba->cfg_sg_dma_buf_size);
2349
2350 sgl = (struct sli4_sge *)sgl_xtra->dma_sgl;
James Smartacd68592012-01-18 16:25:09 -05002351 } else {
James Smartd79c9e92019-08-14 16:57:09 -07002352 dataphysaddr = sg_dma_address(sgde) +
2353 split_offset;
2354
2355 remainder = sg_dma_len(sgde) - split_offset;
2356
2357 if ((subtotal + remainder) <= protgrp_bytes) {
2358 /* we can use this whole buffer */
2359 dma_len = remainder;
2360 split_offset = 0;
2361
2362 if ((subtotal + remainder) ==
2363 protgrp_bytes)
2364 pgdone = 1;
2365 } else {
2366 /* must split this buffer with next
2367 * prot grp
2368 */
2369 dma_len = protgrp_bytes - subtotal;
2370 split_offset += dma_len;
2371 }
2372
2373 subtotal += dma_len;
2374
2375 sgl->word2 = 0;
2376 sgl->addr_lo = cpu_to_le32(putPaddrLow(
2377 dataphysaddr));
2378 sgl->addr_hi = cpu_to_le32(putPaddrHigh(
2379 dataphysaddr));
2380 bf_set(lpfc_sli4_sge_last, sgl, 0);
2381 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2382 bf_set(lpfc_sli4_sge_type, sgl,
2383 LPFC_SGE_TYPE_DATA);
2384
2385 sgl->sge_len = cpu_to_le32(dma_len);
2386 dma_offset += dma_len;
2387
2388 num_sge++;
2389 curr_data++;
2390
2391 if (split_offset) {
2392 sgl++;
2393 j++;
2394 break;
2395 }
2396
2397 /* Move to the next s/g segment if possible */
2398 sgde = sg_next(sgde);
2399
2400 sgl++;
James Smartacd68592012-01-18 16:25:09 -05002401 }
2402
James Smartd79c9e92019-08-14 16:57:09 -07002403 j++;
James Smartacd68592012-01-18 16:25:09 -05002404 }
2405
2406 if (protgroup_offset) {
2407 /* update the reference tag */
2408 reftag += protgrp_blks;
James Smartacd68592012-01-18 16:25:09 -05002409 continue;
2410 }
2411
2412 /* are we done ? */
2413 if (curr_prot == protcnt) {
James Smartd79c9e92019-08-14 16:57:09 -07002414 /* mark the last SGL */
2415 sgl--;
James Smartacd68592012-01-18 16:25:09 -05002416 bf_set(lpfc_sli4_sge_last, sgl, 1);
2417 alldone = 1;
2418 } else if (curr_prot < protcnt) {
2419 /* advance to next prot buffer */
2420 sgpe = sg_next(sgpe);
James Smartacd68592012-01-18 16:25:09 -05002421
2422 /* update the reference tag */
2423 reftag += protgrp_blks;
2424 } else {
2425 /* if we're here, we have a bug */
Dick Kennedy372c1872020-06-30 14:50:00 -07002426 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2427 "9085 BLKGRD: bug in %s\n", __func__);
James Smartacd68592012-01-18 16:25:09 -05002428 }
2429
2430 } while (!alldone);
2431
2432out:
2433
2434 return num_sge;
2435}
2436
2437/**
2438 * lpfc_prot_group_type - Get prtotection group type of SCSI command
2439 * @phba: The Hba for which this call is being executed.
2440 * @sc: pointer to scsi command we're working on
2441 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002442 * Given a SCSI command that supports DIF, determine composition of protection
2443 * groups involved in setting up buffer lists
2444 *
James Smartacd68592012-01-18 16:25:09 -05002445 * Returns: Protection group type (with or without DIF)
2446 *
2447 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002448static int
2449lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
2450{
2451 int ret = LPFC_PG_TYPE_INVALID;
2452 unsigned char op = scsi_get_prot_op(sc);
2453
2454 switch (op) {
2455 case SCSI_PROT_READ_STRIP:
2456 case SCSI_PROT_WRITE_INSERT:
2457 ret = LPFC_PG_TYPE_NO_DIF;
2458 break;
2459 case SCSI_PROT_READ_INSERT:
2460 case SCSI_PROT_WRITE_STRIP:
2461 case SCSI_PROT_READ_PASS:
2462 case SCSI_PROT_WRITE_PASS:
James Smarte2a0a9d2008-12-04 22:40:02 -05002463 ret = LPFC_PG_TYPE_DIF_BUF;
2464 break;
2465 default:
James Smart9c6aa9d2013-05-31 17:03:39 -04002466 if (phba)
Dick Kennedy372c1872020-06-30 14:50:00 -07002467 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart9c6aa9d2013-05-31 17:03:39 -04002468 "9021 Unsupported protection op:%d\n",
2469 op);
James Smarte2a0a9d2008-12-04 22:40:02 -05002470 break;
2471 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002472 return ret;
2473}
2474
James Smartacd68592012-01-18 16:25:09 -05002475/**
James Smarta6887e22013-04-17 20:18:07 -04002476 * lpfc_bg_scsi_adjust_dl - Adjust SCSI data length for BlockGuard
2477 * @phba: The Hba for which this call is being executed.
2478 * @lpfc_cmd: The scsi buffer which is going to be adjusted.
2479 *
2480 * Adjust the data length to account for how much data
2481 * is actually on the wire.
2482 *
2483 * returns the adjusted data length
2484 **/
2485static int
2486lpfc_bg_scsi_adjust_dl(struct lpfc_hba *phba,
James Smartc4908502019-01-28 11:14:28 -08002487 struct lpfc_io_buf *lpfc_cmd)
James Smarta6887e22013-04-17 20:18:07 -04002488{
2489 struct scsi_cmnd *sc = lpfc_cmd->pCmd;
2490 int fcpdl;
2491
2492 fcpdl = scsi_bufflen(sc);
2493
2494 /* Check if there is protection data on the wire */
2495 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
James Smart9c6aa9d2013-05-31 17:03:39 -04002496 /* Read check for protection data */
James Smarta6887e22013-04-17 20:18:07 -04002497 if (scsi_get_prot_op(sc) == SCSI_PROT_READ_INSERT)
2498 return fcpdl;
2499
2500 } else {
James Smart9c6aa9d2013-05-31 17:03:39 -04002501 /* Write check for protection data */
James Smarta6887e22013-04-17 20:18:07 -04002502 if (scsi_get_prot_op(sc) == SCSI_PROT_WRITE_STRIP)
2503 return fcpdl;
2504 }
2505
2506 /*
2507 * If we are in DIF Type 1 mode every data block has a 8 byte
James Smart9c6aa9d2013-05-31 17:03:39 -04002508 * DIF (trailer) attached to it. Must ajust FCP data length
2509 * to account for the protection data.
James Smarta6887e22013-04-17 20:18:07 -04002510 */
James Smart9c6aa9d2013-05-31 17:03:39 -04002511 fcpdl += (fcpdl / lpfc_cmd_blksize(sc)) * 8;
James Smarta6887e22013-04-17 20:18:07 -04002512
2513 return fcpdl;
2514}
2515
2516/**
James Smartacd68592012-01-18 16:25:09 -05002517 * lpfc_bg_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
2518 * @phba: The Hba for which this call is being executed.
2519 * @lpfc_cmd: The scsi buffer which is going to be prep'ed.
2520 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002521 * This is the protection/DIF aware version of
2522 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
James Smart5e0e2312019-08-14 16:56:54 -07002523 * two functions eventually, but for now, it's here.
2524 * RETURNS 0 - SUCCESS,
2525 * 1 - Failed DMA map, retry.
2526 * 2 - Invalid scsi cmd or prot-type. Do not rety.
James Smartacd68592012-01-18 16:25:09 -05002527 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002528static int
James Smartacd68592012-01-18 16:25:09 -05002529lpfc_bg_scsi_prep_dma_buf_s3(struct lpfc_hba *phba,
James Smartc4908502019-01-28 11:14:28 -08002530 struct lpfc_io_buf *lpfc_cmd)
James Smarte2a0a9d2008-12-04 22:40:02 -05002531{
2532 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2533 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smartc4908502019-01-28 11:14:28 -08002534 struct ulp_bde64 *bpl = (struct ulp_bde64 *)lpfc_cmd->dma_sgl;
James Smarte2a0a9d2008-12-04 22:40:02 -05002535 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2536 uint32_t num_bde = 0;
2537 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
2538 int prot_group_type = 0;
James Smarta6887e22013-04-17 20:18:07 -04002539 int fcpdl;
James Smart5e0e2312019-08-14 16:56:54 -07002540 int ret = 1;
James Smart7c4042a2018-11-29 16:09:40 -08002541 struct lpfc_vport *vport = phba->pport;
James Smarte2a0a9d2008-12-04 22:40:02 -05002542
2543 /*
2544 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
2545 * fcp_rsp regions to the first data bde entry
2546 */
2547 bpl += 2;
2548 if (scsi_sg_count(scsi_cmnd)) {
2549 /*
2550 * The driver stores the segment count returned from pci_map_sg
2551 * because this a count of dma-mappings used to map the use_sg
2552 * pages. They are not guaranteed to be the same for those
2553 * architectures that implement an IOMMU.
2554 */
2555 datasegcnt = dma_map_sg(&phba->pcidev->dev,
2556 scsi_sglist(scsi_cmnd),
2557 scsi_sg_count(scsi_cmnd), datadir);
2558 if (unlikely(!datasegcnt))
2559 return 1;
2560
2561 lpfc_cmd->seg_cnt = datasegcnt;
James Smart96f70772013-04-17 20:16:15 -04002562
2563 /* First check if data segment count from SCSI Layer is good */
James Smart5e0e2312019-08-14 16:56:54 -07002564 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
2565 WARN_ON_ONCE(lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt);
2566 ret = 2;
James Smart96f70772013-04-17 20:16:15 -04002567 goto err;
James Smart5e0e2312019-08-14 16:56:54 -07002568 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002569
2570 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
2571
2572 switch (prot_group_type) {
2573 case LPFC_PG_TYPE_NO_DIF:
James Smart96f70772013-04-17 20:16:15 -04002574
2575 /* Here we need to add a PDE5 and PDE6 to the count */
James Smart5e0e2312019-08-14 16:56:54 -07002576 if ((lpfc_cmd->seg_cnt + 2) > phba->cfg_total_seg_cnt) {
2577 ret = 2;
James Smart96f70772013-04-17 20:16:15 -04002578 goto err;
James Smart5e0e2312019-08-14 16:56:54 -07002579 }
James Smart96f70772013-04-17 20:16:15 -04002580
James Smarte2a0a9d2008-12-04 22:40:02 -05002581 num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
2582 datasegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002583 /* we should have 2 or more entries in buffer list */
James Smart5e0e2312019-08-14 16:56:54 -07002584 if (num_bde < 2) {
2585 ret = 2;
James Smarte2a0a9d2008-12-04 22:40:02 -05002586 goto err;
James Smart5e0e2312019-08-14 16:56:54 -07002587 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002588 break;
James Smart96f70772013-04-17 20:16:15 -04002589
2590 case LPFC_PG_TYPE_DIF_BUF:
James Smarte2a0a9d2008-12-04 22:40:02 -05002591 /*
2592 * This type indicates that protection buffers are
2593 * passed to the driver, so that needs to be prepared
2594 * for DMA
2595 */
2596 protsegcnt = dma_map_sg(&phba->pcidev->dev,
2597 scsi_prot_sglist(scsi_cmnd),
2598 scsi_prot_sg_count(scsi_cmnd), datadir);
2599 if (unlikely(!protsegcnt)) {
2600 scsi_dma_unmap(scsi_cmnd);
2601 return 1;
2602 }
2603
2604 lpfc_cmd->prot_seg_cnt = protsegcnt;
James Smart96f70772013-04-17 20:16:15 -04002605
2606 /*
2607 * There is a minimun of 4 BPLs used for every
2608 * protection data segment.
2609 */
2610 if ((lpfc_cmd->prot_seg_cnt * 4) >
James Smart5e0e2312019-08-14 16:56:54 -07002611 (phba->cfg_total_seg_cnt - 2)) {
2612 ret = 2;
James Smart96f70772013-04-17 20:16:15 -04002613 goto err;
James Smart5e0e2312019-08-14 16:56:54 -07002614 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002615
2616 num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
2617 datasegcnt, protsegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002618 /* we should have 3 or more entries in buffer list */
James Smart96f70772013-04-17 20:16:15 -04002619 if ((num_bde < 3) ||
James Smart5e0e2312019-08-14 16:56:54 -07002620 (num_bde > phba->cfg_total_seg_cnt)) {
2621 ret = 2;
James Smarte2a0a9d2008-12-04 22:40:02 -05002622 goto err;
James Smart5e0e2312019-08-14 16:56:54 -07002623 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002624 break;
James Smart96f70772013-04-17 20:16:15 -04002625
James Smarte2a0a9d2008-12-04 22:40:02 -05002626 case LPFC_PG_TYPE_INVALID:
2627 default:
James Smart96f70772013-04-17 20:16:15 -04002628 scsi_dma_unmap(scsi_cmnd);
2629 lpfc_cmd->seg_cnt = 0;
2630
Dick Kennedy372c1872020-06-30 14:50:00 -07002631 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte2a0a9d2008-12-04 22:40:02 -05002632 "9022 Unexpected protection group %i\n",
2633 prot_group_type);
James Smart5e0e2312019-08-14 16:56:54 -07002634 return 2;
James Smarte2a0a9d2008-12-04 22:40:02 -05002635 }
2636 }
2637
2638 /*
2639 * Finish initializing those IOCB fields that are dependent on the
2640 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
2641 * reinitialized since all iocb memory resources are used many times
2642 * for transmit, receive, and continuation bpl's.
2643 */
2644 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
2645 iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
2646 iocb_cmd->ulpBdeCount = 1;
2647 iocb_cmd->ulpLe = 1;
2648
James Smarta6887e22013-04-17 20:18:07 -04002649 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002650 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
2651
2652 /*
2653 * Due to difference in data length between DIF/non-DIF paths,
2654 * we need to set word 4 of IOCB here
2655 */
2656 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
2657
James Smart7c4042a2018-11-29 16:09:40 -08002658 /*
2659 * For First burst, we may need to adjust the initial transfer
2660 * length for DIF
2661 */
2662 if (iocb_cmd->un.fcpi.fcpi_XRdy &&
2663 (fcpdl < vport->cfg_first_burst_size))
2664 iocb_cmd->un.fcpi.fcpi_XRdy = fcpdl;
2665
James Smarte2a0a9d2008-12-04 22:40:02 -05002666 return 0;
2667err:
James Smart96f70772013-04-17 20:16:15 -04002668 if (lpfc_cmd->seg_cnt)
2669 scsi_dma_unmap(scsi_cmnd);
2670 if (lpfc_cmd->prot_seg_cnt)
2671 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
2672 scsi_prot_sg_count(scsi_cmnd),
2673 scsi_cmnd->sc_data_direction);
2674
Dick Kennedy372c1872020-06-30 14:50:00 -07002675 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart96f70772013-04-17 20:16:15 -04002676 "9023 Cannot setup S/G List for HBA"
2677 "IO segs %d/%d BPL %d SCSI %d: %d %d\n",
2678 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
2679 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
James Smarte2a0a9d2008-12-04 22:40:02 -05002680 prot_group_type, num_bde);
James Smart96f70772013-04-17 20:16:15 -04002681
2682 lpfc_cmd->seg_cnt = 0;
2683 lpfc_cmd->prot_seg_cnt = 0;
James Smart5e0e2312019-08-14 16:56:54 -07002684 return ret;
James Smarte2a0a9d2008-12-04 22:40:02 -05002685}
2686
2687/*
James Smart737d4242013-04-17 20:14:49 -04002688 * This function calcuates the T10 DIF guard tag
2689 * on the specified data using a CRC algorithmn
2690 * using crc_t10dif.
2691 */
Rashika Kheria7bfe7812014-09-03 12:55:36 -04002692static uint16_t
James Smart737d4242013-04-17 20:14:49 -04002693lpfc_bg_crc(uint8_t *data, int count)
2694{
2695 uint16_t crc = 0;
2696 uint16_t x;
2697
2698 crc = crc_t10dif(data, count);
2699 x = cpu_to_be16(crc);
2700 return x;
2701}
2702
2703/*
2704 * This function calcuates the T10 DIF guard tag
2705 * on the specified data using a CSUM algorithmn
2706 * using ip_compute_csum.
2707 */
Rashika Kheria7bfe7812014-09-03 12:55:36 -04002708static uint16_t
James Smart737d4242013-04-17 20:14:49 -04002709lpfc_bg_csum(uint8_t *data, int count)
2710{
2711 uint16_t ret;
2712
2713 ret = ip_compute_csum(data, count);
2714 return ret;
2715}
2716
2717/*
2718 * This function examines the protection data to try to determine
2719 * what type of T10-DIF error occurred.
2720 */
Rashika Kheria7bfe7812014-09-03 12:55:36 -04002721static void
James Smartc4908502019-01-28 11:14:28 -08002722lpfc_calc_bg_err(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
James Smart737d4242013-04-17 20:14:49 -04002723{
2724 struct scatterlist *sgpe; /* s/g prot entry */
2725 struct scatterlist *sgde; /* s/g data entry */
2726 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
2727 struct scsi_dif_tuple *src = NULL;
2728 uint8_t *data_src = NULL;
Sebastian Herbsztdb6f1c22015-08-31 16:48:14 -04002729 uint16_t guard_tag;
James Smart737d4242013-04-17 20:14:49 -04002730 uint16_t start_app_tag, app_tag;
2731 uint32_t start_ref_tag, ref_tag;
2732 int prot, protsegcnt;
2733 int err_type, len, data_len;
2734 int chk_ref, chk_app, chk_guard;
2735 uint16_t sum;
2736 unsigned blksize;
2737
2738 err_type = BGS_GUARD_ERR_MASK;
2739 sum = 0;
2740 guard_tag = 0;
2741
2742 /* First check to see if there is protection data to examine */
2743 prot = scsi_get_prot_op(cmd);
2744 if ((prot == SCSI_PROT_READ_STRIP) ||
2745 (prot == SCSI_PROT_WRITE_INSERT) ||
2746 (prot == SCSI_PROT_NORMAL))
2747 goto out;
2748
2749 /* Currently the driver just supports ref_tag and guard_tag checking */
2750 chk_ref = 1;
2751 chk_app = 0;
2752 chk_guard = 0;
2753
2754 /* Setup a ptr to the protection data provided by the SCSI host */
2755 sgpe = scsi_prot_sglist(cmd);
2756 protsegcnt = lpfc_cmd->prot_seg_cnt;
2757
2758 if (sgpe && protsegcnt) {
2759
2760 /*
2761 * We will only try to verify guard tag if the segment
2762 * data length is a multiple of the blksize.
2763 */
2764 sgde = scsi_sglist(cmd);
2765 blksize = lpfc_cmd_blksize(cmd);
2766 data_src = (uint8_t *)sg_virt(sgde);
2767 data_len = sgde->length;
2768 if ((data_len & (blksize - 1)) == 0)
2769 chk_guard = 1;
James Smart737d4242013-04-17 20:14:49 -04002770
James Smarte85d8f92013-05-31 17:04:10 -04002771 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
James Smarta6887e22013-04-17 20:18:07 -04002772 start_ref_tag = (uint32_t)scsi_get_lba(cmd); /* Truncate LBA */
James Smart737d4242013-04-17 20:14:49 -04002773 start_app_tag = src->app_tag;
James Smart737d4242013-04-17 20:14:49 -04002774 len = sgpe->length;
2775 while (src && protsegcnt) {
2776 while (len) {
2777
2778 /*
2779 * First check to see if a protection data
2780 * check is valid
2781 */
Dmitry Monakhov128b6f92017-06-29 11:31:12 -07002782 if ((src->ref_tag == T10_PI_REF_ESCAPE) ||
2783 (src->app_tag == T10_PI_APP_ESCAPE)) {
James Smart737d4242013-04-17 20:14:49 -04002784 start_ref_tag++;
2785 goto skipit;
2786 }
2787
James Smart9c6aa9d2013-05-31 17:03:39 -04002788 /* First Guard Tag checking */
2789 if (chk_guard) {
2790 guard_tag = src->guard_tag;
2791 if (lpfc_cmd_guard_csum(cmd))
2792 sum = lpfc_bg_csum(data_src,
2793 blksize);
2794 else
2795 sum = lpfc_bg_crc(data_src,
2796 blksize);
2797 if ((guard_tag != sum)) {
2798 err_type = BGS_GUARD_ERR_MASK;
2799 goto out;
2800 }
James Smart737d4242013-04-17 20:14:49 -04002801 }
2802
2803 /* Reference Tag checking */
2804 ref_tag = be32_to_cpu(src->ref_tag);
2805 if (chk_ref && (ref_tag != start_ref_tag)) {
2806 err_type = BGS_REFTAG_ERR_MASK;
2807 goto out;
2808 }
2809 start_ref_tag++;
2810
James Smart9c6aa9d2013-05-31 17:03:39 -04002811 /* App Tag checking */
2812 app_tag = src->app_tag;
2813 if (chk_app && (app_tag != start_app_tag)) {
2814 err_type = BGS_APPTAG_ERR_MASK;
2815 goto out;
James Smart737d4242013-04-17 20:14:49 -04002816 }
2817skipit:
2818 len -= sizeof(struct scsi_dif_tuple);
2819 if (len < 0)
2820 len = 0;
2821 src++;
2822
2823 data_src += blksize;
2824 data_len -= blksize;
2825
2826 /*
2827 * Are we at the end of the Data segment?
2828 * The data segment is only used for Guard
2829 * tag checking.
2830 */
2831 if (chk_guard && (data_len == 0)) {
2832 chk_guard = 0;
2833 sgde = sg_next(sgde);
2834 if (!sgde)
2835 goto out;
2836
2837 data_src = (uint8_t *)sg_virt(sgde);
2838 data_len = sgde->length;
2839 if ((data_len & (blksize - 1)) == 0)
2840 chk_guard = 1;
2841 }
2842 }
2843
2844 /* Goto the next Protection data segment */
2845 sgpe = sg_next(sgpe);
2846 if (sgpe) {
2847 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
2848 len = sgpe->length;
2849 } else {
2850 src = NULL;
2851 }
2852 protsegcnt--;
2853 }
2854 }
2855out:
2856 if (err_type == BGS_GUARD_ERR_MASK) {
2857 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2858 0x10, 0x1);
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02002859 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
2860 SAM_STAT_CHECK_CONDITION;
James Smart737d4242013-04-17 20:14:49 -04002861 phba->bg_guard_err_cnt++;
2862 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2863 "9069 BLKGRD: LBA %lx grd_tag error %x != %x\n",
2864 (unsigned long)scsi_get_lba(cmd),
2865 sum, guard_tag);
2866
2867 } else if (err_type == BGS_REFTAG_ERR_MASK) {
2868 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2869 0x10, 0x3);
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02002870 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
2871 SAM_STAT_CHECK_CONDITION;
James Smart737d4242013-04-17 20:14:49 -04002872
2873 phba->bg_reftag_err_cnt++;
2874 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2875 "9066 BLKGRD: LBA %lx ref_tag error %x != %x\n",
2876 (unsigned long)scsi_get_lba(cmd),
2877 ref_tag, start_ref_tag);
2878
2879 } else if (err_type == BGS_APPTAG_ERR_MASK) {
2880 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2881 0x10, 0x2);
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02002882 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
2883 SAM_STAT_CHECK_CONDITION;
James Smart737d4242013-04-17 20:14:49 -04002884
2885 phba->bg_apptag_err_cnt++;
2886 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2887 "9041 BLKGRD: LBA %lx app_tag error %x != %x\n",
2888 (unsigned long)scsi_get_lba(cmd),
2889 app_tag, start_app_tag);
2890 }
2891}
2892
2893
2894/*
James Smarte2a0a9d2008-12-04 22:40:02 -05002895 * This function checks for BlockGuard errors detected by
2896 * the HBA. In case of errors, the ASC/ASCQ fields in the
2897 * sense buffer will be set accordingly, paired with
2898 * ILLEGAL_REQUEST to signal to the kernel that the HBA
2899 * detected corruption.
2900 *
2901 * Returns:
2902 * 0 - No error found
2903 * 1 - BlockGuard error found
2904 * -1 - Internal error (bad profile, ...etc)
2905 */
2906static int
James Smartc4908502019-01-28 11:14:28 -08002907lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd,
2908 struct lpfc_iocbq *pIocbOut)
James Smarte2a0a9d2008-12-04 22:40:02 -05002909{
2910 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
2911 struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
2912 int ret = 0;
2913 uint32_t bghm = bgf->bghm;
2914 uint32_t bgstat = bgf->bgstat;
2915 uint64_t failing_sector = 0;
2916
James Smarte2a0a9d2008-12-04 22:40:02 -05002917 if (lpfc_bgs_get_invalid_prof(bgstat)) {
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02002918 cmd->result = DID_ERROR << 16;
James Smart737d4242013-04-17 20:14:49 -04002919 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2920 "9072 BLKGRD: Invalid BG Profile in cmd"
2921 " 0x%x lba 0x%llx blk cnt 0x%x "
2922 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
2923 (unsigned long long)scsi_get_lba(cmd),
2924 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05002925 ret = (-1);
2926 goto out;
2927 }
2928
2929 if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02002930 cmd->result = DID_ERROR << 16;
James Smart737d4242013-04-17 20:14:49 -04002931 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2932 "9073 BLKGRD: Invalid BG PDIF Block in cmd"
2933 " 0x%x lba 0x%llx blk cnt 0x%x "
2934 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
2935 (unsigned long long)scsi_get_lba(cmd),
2936 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05002937 ret = (-1);
2938 goto out;
2939 }
2940
2941 if (lpfc_bgs_get_guard_err(bgstat)) {
2942 ret = 1;
2943
2944 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2945 0x10, 0x1);
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02002946 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
2947 SAM_STAT_CHECK_CONDITION;
James Smarte2a0a9d2008-12-04 22:40:02 -05002948 phba->bg_guard_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04002949 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2950 "9055 BLKGRD: Guard Tag error in cmd"
2951 " 0x%x lba 0x%llx blk cnt 0x%x "
2952 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
2953 (unsigned long long)scsi_get_lba(cmd),
2954 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05002955 }
2956
2957 if (lpfc_bgs_get_reftag_err(bgstat)) {
2958 ret = 1;
2959
2960 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2961 0x10, 0x3);
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02002962 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
2963 SAM_STAT_CHECK_CONDITION;
James Smarte2a0a9d2008-12-04 22:40:02 -05002964
2965 phba->bg_reftag_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04002966 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2967 "9056 BLKGRD: Ref Tag error in cmd"
2968 " 0x%x lba 0x%llx blk cnt 0x%x "
2969 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
2970 (unsigned long long)scsi_get_lba(cmd),
2971 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05002972 }
2973
2974 if (lpfc_bgs_get_apptag_err(bgstat)) {
2975 ret = 1;
2976
2977 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2978 0x10, 0x2);
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02002979 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
2980 SAM_STAT_CHECK_CONDITION;
James Smarte2a0a9d2008-12-04 22:40:02 -05002981
2982 phba->bg_apptag_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04002983 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2984 "9061 BLKGRD: App Tag error in cmd"
2985 " 0x%x lba 0x%llx blk cnt 0x%x "
2986 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
2987 (unsigned long long)scsi_get_lba(cmd),
2988 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05002989 }
2990
2991 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
2992 /*
2993 * setup sense data descriptor 0 per SPC-4 as an information
James Smart7c56b9f2011-07-22 18:36:25 -04002994 * field, and put the failing LBA in it.
2995 * This code assumes there was also a guard/app/ref tag error
2996 * indication.
James Smarte2a0a9d2008-12-04 22:40:02 -05002997 */
James Smart7c56b9f2011-07-22 18:36:25 -04002998 cmd->sense_buffer[7] = 0xc; /* Additional sense length */
2999 cmd->sense_buffer[8] = 0; /* Information descriptor type */
3000 cmd->sense_buffer[9] = 0xa; /* Additional descriptor length */
3001 cmd->sense_buffer[10] = 0x80; /* Validity bit */
James Smartacd68592012-01-18 16:25:09 -05003002
3003 /* bghm is a "on the wire" FC frame based count */
3004 switch (scsi_get_prot_op(cmd)) {
3005 case SCSI_PROT_READ_INSERT:
3006 case SCSI_PROT_WRITE_STRIP:
3007 bghm /= cmd->device->sector_size;
3008 break;
3009 case SCSI_PROT_READ_STRIP:
3010 case SCSI_PROT_WRITE_INSERT:
3011 case SCSI_PROT_READ_PASS:
3012 case SCSI_PROT_WRITE_PASS:
3013 bghm /= (cmd->device->sector_size +
3014 sizeof(struct scsi_dif_tuple));
3015 break;
3016 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003017
3018 failing_sector = scsi_get_lba(cmd);
3019 failing_sector += bghm;
3020
James Smart7c56b9f2011-07-22 18:36:25 -04003021 /* Descriptor Information */
3022 put_unaligned_be64(failing_sector, &cmd->sense_buffer[12]);
James Smarte2a0a9d2008-12-04 22:40:02 -05003023 }
3024
3025 if (!ret) {
3026 /* No error was reported - problem in FW? */
James Smart737d4242013-04-17 20:14:49 -04003027 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3028 "9057 BLKGRD: Unknown error in cmd"
3029 " 0x%x lba 0x%llx blk cnt 0x%x "
3030 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3031 (unsigned long long)scsi_get_lba(cmd),
3032 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003033
James Smart737d4242013-04-17 20:14:49 -04003034 /* Calcuate what type of error it was */
3035 lpfc_calc_bg_err(phba, lpfc_cmd);
3036 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003037out:
3038 return ret;
3039}
3040
James Smartea2151b2008-09-07 11:52:10 -04003041/**
James Smartda0436e2009-05-22 14:51:39 -04003042 * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3043 * @phba: The Hba for which this call is being executed.
3044 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3045 *
3046 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
3047 * field of @lpfc_cmd for device with SLI-4 interface spec.
3048 *
3049 * Return codes:
James Smart5e0e2312019-08-14 16:56:54 -07003050 * 2 - Error - Do not retry
3051 * 1 - Error - Retry
James Smart6c8eea52010-04-06 14:49:53 -04003052 * 0 - Success
James Smartda0436e2009-05-22 14:51:39 -04003053 **/
3054static int
James Smartc4908502019-01-28 11:14:28 -08003055lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
James Smartda0436e2009-05-22 14:51:39 -04003056{
3057 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3058 struct scatterlist *sgel = NULL;
3059 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart0794d602019-01-28 11:14:19 -08003060 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
James Smartfedd3b72011-02-16 12:39:24 -05003061 struct sli4_sge *first_data_sgl;
James Smartda0436e2009-05-22 14:51:39 -04003062 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
3063 dma_addr_t physaddr;
3064 uint32_t num_bde = 0;
3065 uint32_t dma_len;
3066 uint32_t dma_offset = 0;
James Smartd79c9e92019-08-14 16:57:09 -07003067 int nseg, i, j;
James Smartfedd3b72011-02-16 12:39:24 -05003068 struct ulp_bde64 *bde;
James Smartd79c9e92019-08-14 16:57:09 -07003069 bool lsp_just_set = false;
3070 struct sli4_hybrid_sgl *sgl_xtra = NULL;
James Smartda0436e2009-05-22 14:51:39 -04003071
3072 /*
3073 * There are three possibilities here - use scatter-gather segment, use
3074 * the single mapping, or neither. Start the lpfc command prep by
3075 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
3076 * data bde entry.
3077 */
3078 if (scsi_sg_count(scsi_cmnd)) {
3079 /*
3080 * The driver stores the segment count returned from pci_map_sg
3081 * because this a count of dma-mappings used to map the use_sg
3082 * pages. They are not guaranteed to be the same for those
3083 * architectures that implement an IOMMU.
3084 */
3085
3086 nseg = scsi_dma_map(scsi_cmnd);
James Smart5116fbf2015-05-22 10:42:39 -04003087 if (unlikely(nseg <= 0))
James Smartda0436e2009-05-22 14:51:39 -04003088 return 1;
3089 sgl += 1;
3090 /* clear the last flag in the fcp_rsp map entry */
3091 sgl->word2 = le32_to_cpu(sgl->word2);
3092 bf_set(lpfc_sli4_sge_last, sgl, 0);
3093 sgl->word2 = cpu_to_le32(sgl->word2);
3094 sgl += 1;
James Smartfedd3b72011-02-16 12:39:24 -05003095 first_data_sgl = sgl;
James Smartda0436e2009-05-22 14:51:39 -04003096 lpfc_cmd->seg_cnt = nseg;
James Smartd79c9e92019-08-14 16:57:09 -07003097 if (!phba->cfg_xpsgl &&
3098 lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
Dick Kennedy372c1872020-06-30 14:50:00 -07003099 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3100 "9074 BLKGRD:"
3101 " %s: Too many sg segments from "
3102 "dma_map_sg. Config %d, seg_cnt %d\n",
3103 __func__, phba->cfg_sg_seg_cnt,
3104 lpfc_cmd->seg_cnt);
James Smart5e0e2312019-08-14 16:56:54 -07003105 WARN_ON_ONCE(lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt);
James Smart96f70772013-04-17 20:16:15 -04003106 lpfc_cmd->seg_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04003107 scsi_dma_unmap(scsi_cmnd);
James Smart5e0e2312019-08-14 16:56:54 -07003108 return 2;
James Smartda0436e2009-05-22 14:51:39 -04003109 }
3110
3111 /*
3112 * The driver established a maximum scatter-gather segment count
3113 * during probe that limits the number of sg elements in any
3114 * single scsi command. Just run through the seg_cnt and format
3115 * the sge's.
3116 * When using SLI-3 the driver will try to fit all the BDEs into
3117 * the IOCB. If it can't then the BDEs get added to a BPL as it
3118 * does for SLI-2 mode.
3119 */
James Smartd79c9e92019-08-14 16:57:09 -07003120
3121 /* for tracking segment boundaries */
3122 sgel = scsi_sglist(scsi_cmnd);
3123 j = 2;
3124 for (i = 0; i < nseg; i++) {
3125 sgl->word2 = 0;
3126 if ((num_bde + 1) == nseg) {
James Smartda0436e2009-05-22 14:51:39 -04003127 bf_set(lpfc_sli4_sge_last, sgl, 1);
James Smartd79c9e92019-08-14 16:57:09 -07003128 bf_set(lpfc_sli4_sge_type, sgl,
3129 LPFC_SGE_TYPE_DATA);
3130 } else {
James Smartda0436e2009-05-22 14:51:39 -04003131 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smartd79c9e92019-08-14 16:57:09 -07003132
3133 /* do we need to expand the segment */
3134 if (!lsp_just_set &&
3135 !((j + 1) % phba->border_sge_num) &&
3136 ((nseg - 1) != i)) {
3137 /* set LSP type */
3138 bf_set(lpfc_sli4_sge_type, sgl,
3139 LPFC_SGE_TYPE_LSP);
3140
3141 sgl_xtra = lpfc_get_sgl_per_hdwq(
3142 phba, lpfc_cmd);
3143
3144 if (unlikely(!sgl_xtra)) {
3145 lpfc_cmd->seg_cnt = 0;
3146 scsi_dma_unmap(scsi_cmnd);
3147 return 1;
3148 }
3149 sgl->addr_lo = cpu_to_le32(putPaddrLow(
3150 sgl_xtra->dma_phys_sgl));
3151 sgl->addr_hi = cpu_to_le32(putPaddrHigh(
3152 sgl_xtra->dma_phys_sgl));
3153
3154 } else {
3155 bf_set(lpfc_sli4_sge_type, sgl,
3156 LPFC_SGE_TYPE_DATA);
3157 }
3158 }
3159
3160 if (!(bf_get(lpfc_sli4_sge_type, sgl) &
3161 LPFC_SGE_TYPE_LSP)) {
3162 if ((nseg - 1) == i)
3163 bf_set(lpfc_sli4_sge_last, sgl, 1);
3164
3165 physaddr = sg_dma_address(sgel);
3166 dma_len = sg_dma_len(sgel);
3167 sgl->addr_lo = cpu_to_le32(putPaddrLow(
3168 physaddr));
3169 sgl->addr_hi = cpu_to_le32(putPaddrHigh(
3170 physaddr));
3171
3172 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
3173 sgl->word2 = cpu_to_le32(sgl->word2);
3174 sgl->sge_len = cpu_to_le32(dma_len);
3175
3176 dma_offset += dma_len;
3177 sgel = sg_next(sgel);
3178
3179 sgl++;
3180 lsp_just_set = false;
3181
3182 } else {
3183 sgl->word2 = cpu_to_le32(sgl->word2);
3184 sgl->sge_len = cpu_to_le32(
3185 phba->cfg_sg_dma_buf_size);
3186
3187 sgl = (struct sli4_sge *)sgl_xtra->dma_sgl;
3188 i = i - 1;
3189
3190 lsp_just_set = true;
3191 }
3192
3193 j++;
James Smartda0436e2009-05-22 14:51:39 -04003194 }
James Smart0bc2b7c2018-02-22 08:18:48 -08003195 /*
3196 * Setup the first Payload BDE. For FCoE we just key off
James Smart414abe02018-06-26 08:24:26 -07003197 * Performance Hints, for FC we use lpfc_enable_pbde.
3198 * We populate words 13-15 of IOCB/WQE.
James Smart0bc2b7c2018-02-22 08:18:48 -08003199 */
3200 if ((phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) ||
James Smart414abe02018-06-26 08:24:26 -07003201 phba->cfg_enable_pbde) {
James Smartfedd3b72011-02-16 12:39:24 -05003202 bde = (struct ulp_bde64 *)
James Smart414abe02018-06-26 08:24:26 -07003203 &(iocb_cmd->unsli3.sli3Words[5]);
James Smartfedd3b72011-02-16 12:39:24 -05003204 bde->addrLow = first_data_sgl->addr_lo;
3205 bde->addrHigh = first_data_sgl->addr_hi;
3206 bde->tus.f.bdeSize =
3207 le32_to_cpu(first_data_sgl->sge_len);
3208 bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
3209 bde->tus.w = cpu_to_le32(bde->tus.w);
3210 }
James Smartda0436e2009-05-22 14:51:39 -04003211 } else {
3212 sgl += 1;
3213 /* clear the last flag in the fcp_rsp map entry */
3214 sgl->word2 = le32_to_cpu(sgl->word2);
3215 bf_set(lpfc_sli4_sge_last, sgl, 1);
3216 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart414abe02018-06-26 08:24:26 -07003217
3218 if ((phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) ||
3219 phba->cfg_enable_pbde) {
3220 bde = (struct ulp_bde64 *)
3221 &(iocb_cmd->unsli3.sli3Words[5]);
3222 memset(bde, 0, (sizeof(uint32_t) * 3));
3223 }
James Smartda0436e2009-05-22 14:51:39 -04003224 }
3225
3226 /*
3227 * Finish initializing those IOCB fields that are dependent on the
3228 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
3229 * explicitly reinitialized.
3230 * all iocb memory resources are reused.
3231 */
3232 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
3233
3234 /*
3235 * Due to difference in data length between DIF/non-DIF paths,
3236 * we need to set word 4 of IOCB here
3237 */
3238 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
James Smart1ba981f2014-02-20 09:56:45 -05003239
3240 /*
3241 * If the OAS driver feature is enabled and the lun is enabled for
3242 * OAS, set the oas iocb related flags.
3243 */
James Smartf38fa0b2014-04-04 13:52:21 -04003244 if ((phba->cfg_fof) && ((struct lpfc_device_data *)
James Smartc92c8412016-07-06 12:36:05 -07003245 scsi_cmnd->device->hostdata)->oas_enabled) {
James Smart9bd2bff52014-09-03 12:57:30 -04003246 lpfc_cmd->cur_iocbq.iocb_flag |= (LPFC_IO_OAS | LPFC_IO_FOF);
James Smartc92c8412016-07-06 12:36:05 -07003247 lpfc_cmd->cur_iocbq.priority = ((struct lpfc_device_data *)
3248 scsi_cmnd->device->hostdata)->priority;
3249 }
James Smartc4908502019-01-28 11:14:28 -08003250
James Smartda0436e2009-05-22 14:51:39 -04003251 return 0;
3252}
3253
3254/**
James Smartacd68592012-01-18 16:25:09 -05003255 * lpfc_bg_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3256 * @phba: The Hba for which this call is being executed.
3257 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3258 *
3259 * This is the protection/DIF aware version of
3260 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
3261 * two functions eventually, but for now, it's here
James Smart5e0e2312019-08-14 16:56:54 -07003262 * Return codes:
3263 * 2 - Error - Do not retry
3264 * 1 - Error - Retry
3265 * 0 - Success
James Smartacd68592012-01-18 16:25:09 -05003266 **/
3267static int
3268lpfc_bg_scsi_prep_dma_buf_s4(struct lpfc_hba *phba,
James Smartc4908502019-01-28 11:14:28 -08003269 struct lpfc_io_buf *lpfc_cmd)
James Smartacd68592012-01-18 16:25:09 -05003270{
3271 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3272 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart0794d602019-01-28 11:14:19 -08003273 struct sli4_sge *sgl = (struct sli4_sge *)(lpfc_cmd->dma_sgl);
James Smartacd68592012-01-18 16:25:09 -05003274 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart96f70772013-04-17 20:16:15 -04003275 uint32_t num_sge = 0;
James Smartacd68592012-01-18 16:25:09 -05003276 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
3277 int prot_group_type = 0;
3278 int fcpdl;
James Smart5e0e2312019-08-14 16:56:54 -07003279 int ret = 1;
James Smart7c4042a2018-11-29 16:09:40 -08003280 struct lpfc_vport *vport = phba->pport;
James Smartacd68592012-01-18 16:25:09 -05003281
3282 /*
3283 * Start the lpfc command prep by bumping the sgl beyond fcp_cmnd
James Smart96f70772013-04-17 20:16:15 -04003284 * fcp_rsp regions to the first data sge entry
James Smartacd68592012-01-18 16:25:09 -05003285 */
3286 if (scsi_sg_count(scsi_cmnd)) {
3287 /*
3288 * The driver stores the segment count returned from pci_map_sg
3289 * because this a count of dma-mappings used to map the use_sg
3290 * pages. They are not guaranteed to be the same for those
3291 * architectures that implement an IOMMU.
3292 */
3293 datasegcnt = dma_map_sg(&phba->pcidev->dev,
3294 scsi_sglist(scsi_cmnd),
3295 scsi_sg_count(scsi_cmnd), datadir);
3296 if (unlikely(!datasegcnt))
3297 return 1;
3298
3299 sgl += 1;
3300 /* clear the last flag in the fcp_rsp map entry */
3301 sgl->word2 = le32_to_cpu(sgl->word2);
3302 bf_set(lpfc_sli4_sge_last, sgl, 0);
3303 sgl->word2 = cpu_to_le32(sgl->word2);
3304
3305 sgl += 1;
3306 lpfc_cmd->seg_cnt = datasegcnt;
James Smart96f70772013-04-17 20:16:15 -04003307
3308 /* First check if data segment count from SCSI Layer is good */
James Smartd79c9e92019-08-14 16:57:09 -07003309 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt &&
3310 !phba->cfg_xpsgl) {
James Smart5e0e2312019-08-14 16:56:54 -07003311 WARN_ON_ONCE(lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt);
3312 ret = 2;
James Smart96f70772013-04-17 20:16:15 -04003313 goto err;
James Smart5e0e2312019-08-14 16:56:54 -07003314 }
James Smartacd68592012-01-18 16:25:09 -05003315
3316 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
3317
3318 switch (prot_group_type) {
3319 case LPFC_PG_TYPE_NO_DIF:
James Smart96f70772013-04-17 20:16:15 -04003320 /* Here we need to add a DISEED to the count */
James Smartd79c9e92019-08-14 16:57:09 -07003321 if (((lpfc_cmd->seg_cnt + 1) >
3322 phba->cfg_total_seg_cnt) &&
3323 !phba->cfg_xpsgl) {
James Smart5e0e2312019-08-14 16:56:54 -07003324 ret = 2;
James Smart96f70772013-04-17 20:16:15 -04003325 goto err;
James Smart5e0e2312019-08-14 16:56:54 -07003326 }
James Smart96f70772013-04-17 20:16:15 -04003327
3328 num_sge = lpfc_bg_setup_sgl(phba, scsi_cmnd, sgl,
James Smartd79c9e92019-08-14 16:57:09 -07003329 datasegcnt, lpfc_cmd);
James Smart96f70772013-04-17 20:16:15 -04003330
James Smartacd68592012-01-18 16:25:09 -05003331 /* we should have 2 or more entries in buffer list */
James Smart5e0e2312019-08-14 16:56:54 -07003332 if (num_sge < 2) {
3333 ret = 2;
James Smartacd68592012-01-18 16:25:09 -05003334 goto err;
James Smart5e0e2312019-08-14 16:56:54 -07003335 }
James Smartacd68592012-01-18 16:25:09 -05003336 break;
James Smart96f70772013-04-17 20:16:15 -04003337
3338 case LPFC_PG_TYPE_DIF_BUF:
James Smartacd68592012-01-18 16:25:09 -05003339 /*
3340 * This type indicates that protection buffers are
3341 * passed to the driver, so that needs to be prepared
3342 * for DMA
3343 */
3344 protsegcnt = dma_map_sg(&phba->pcidev->dev,
3345 scsi_prot_sglist(scsi_cmnd),
3346 scsi_prot_sg_count(scsi_cmnd), datadir);
3347 if (unlikely(!protsegcnt)) {
3348 scsi_dma_unmap(scsi_cmnd);
3349 return 1;
3350 }
3351
3352 lpfc_cmd->prot_seg_cnt = protsegcnt;
James Smart96f70772013-04-17 20:16:15 -04003353 /*
3354 * There is a minimun of 3 SGEs used for every
3355 * protection data segment.
3356 */
James Smartd79c9e92019-08-14 16:57:09 -07003357 if (((lpfc_cmd->prot_seg_cnt * 3) >
3358 (phba->cfg_total_seg_cnt - 2)) &&
3359 !phba->cfg_xpsgl) {
James Smart5e0e2312019-08-14 16:56:54 -07003360 ret = 2;
James Smart96f70772013-04-17 20:16:15 -04003361 goto err;
James Smart5e0e2312019-08-14 16:56:54 -07003362 }
James Smartacd68592012-01-18 16:25:09 -05003363
James Smart96f70772013-04-17 20:16:15 -04003364 num_sge = lpfc_bg_setup_sgl_prot(phba, scsi_cmnd, sgl,
James Smartd79c9e92019-08-14 16:57:09 -07003365 datasegcnt, protsegcnt, lpfc_cmd);
James Smart96f70772013-04-17 20:16:15 -04003366
James Smartacd68592012-01-18 16:25:09 -05003367 /* we should have 3 or more entries in buffer list */
James Smartd79c9e92019-08-14 16:57:09 -07003368 if (num_sge < 3 ||
3369 (num_sge > phba->cfg_total_seg_cnt &&
3370 !phba->cfg_xpsgl)) {
James Smart5e0e2312019-08-14 16:56:54 -07003371 ret = 2;
James Smartacd68592012-01-18 16:25:09 -05003372 goto err;
James Smart5e0e2312019-08-14 16:56:54 -07003373 }
James Smartacd68592012-01-18 16:25:09 -05003374 break;
James Smart96f70772013-04-17 20:16:15 -04003375
James Smartacd68592012-01-18 16:25:09 -05003376 case LPFC_PG_TYPE_INVALID:
3377 default:
James Smart96f70772013-04-17 20:16:15 -04003378 scsi_dma_unmap(scsi_cmnd);
3379 lpfc_cmd->seg_cnt = 0;
3380
Dick Kennedy372c1872020-06-30 14:50:00 -07003381 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartacd68592012-01-18 16:25:09 -05003382 "9083 Unexpected protection group %i\n",
3383 prot_group_type);
James Smart5e0e2312019-08-14 16:56:54 -07003384 return 2;
James Smartacd68592012-01-18 16:25:09 -05003385 }
3386 }
3387
James Smart8012cc32012-10-31 14:44:49 -04003388 switch (scsi_get_prot_op(scsi_cmnd)) {
3389 case SCSI_PROT_WRITE_STRIP:
3390 case SCSI_PROT_READ_STRIP:
3391 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_STRIP;
3392 break;
3393 case SCSI_PROT_WRITE_INSERT:
3394 case SCSI_PROT_READ_INSERT:
3395 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_INSERT;
3396 break;
3397 case SCSI_PROT_WRITE_PASS:
3398 case SCSI_PROT_READ_PASS:
3399 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_PASS;
3400 break;
3401 }
3402
James Smartacd68592012-01-18 16:25:09 -05003403 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
James Smartacd68592012-01-18 16:25:09 -05003404 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
3405
3406 /*
3407 * Due to difference in data length between DIF/non-DIF paths,
3408 * we need to set word 4 of IOCB here
3409 */
3410 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
James Smartacd68592012-01-18 16:25:09 -05003411
James Smart9bd2bff52014-09-03 12:57:30 -04003412 /*
James Smart7c4042a2018-11-29 16:09:40 -08003413 * For First burst, we may need to adjust the initial transfer
3414 * length for DIF
3415 */
3416 if (iocb_cmd->un.fcpi.fcpi_XRdy &&
3417 (fcpdl < vport->cfg_first_burst_size))
3418 iocb_cmd->un.fcpi.fcpi_XRdy = fcpdl;
3419
3420 /*
James Smart9bd2bff52014-09-03 12:57:30 -04003421 * If the OAS driver feature is enabled and the lun is enabled for
3422 * OAS, set the oas iocb related flags.
3423 */
3424 if ((phba->cfg_fof) && ((struct lpfc_device_data *)
3425 scsi_cmnd->device->hostdata)->oas_enabled)
3426 lpfc_cmd->cur_iocbq.iocb_flag |= (LPFC_IO_OAS | LPFC_IO_FOF);
3427
James Smartacd68592012-01-18 16:25:09 -05003428 return 0;
3429err:
James Smart96f70772013-04-17 20:16:15 -04003430 if (lpfc_cmd->seg_cnt)
3431 scsi_dma_unmap(scsi_cmnd);
3432 if (lpfc_cmd->prot_seg_cnt)
3433 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
3434 scsi_prot_sg_count(scsi_cmnd),
3435 scsi_cmnd->sc_data_direction);
3436
Dick Kennedy372c1872020-06-30 14:50:00 -07003437 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart96f70772013-04-17 20:16:15 -04003438 "9084 Cannot setup S/G List for HBA"
3439 "IO segs %d/%d SGL %d SCSI %d: %d %d\n",
3440 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
3441 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
3442 prot_group_type, num_sge);
3443
3444 lpfc_cmd->seg_cnt = 0;
3445 lpfc_cmd->prot_seg_cnt = 0;
James Smart5e0e2312019-08-14 16:56:54 -07003446 return ret;
James Smartacd68592012-01-18 16:25:09 -05003447}
3448
3449/**
James Smart3772a992009-05-22 14:50:54 -04003450 * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3451 * @phba: The Hba for which this call is being executed.
3452 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3453 *
3454 * This routine wraps the actual DMA mapping function pointer from the
3455 * lpfc_hba struct.
3456 *
3457 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04003458 * 1 - Error
3459 * 0 - Success
James Smart3772a992009-05-22 14:50:54 -04003460 **/
3461static inline int
James Smartc4908502019-01-28 11:14:28 -08003462lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
James Smart3772a992009-05-22 14:50:54 -04003463{
3464 return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
3465}
3466
3467/**
James Smartacd68592012-01-18 16:25:09 -05003468 * lpfc_bg_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3469 * using BlockGuard.
3470 * @phba: The Hba for which this call is being executed.
3471 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3472 *
3473 * This routine wraps the actual DMA mapping function pointer from the
3474 * lpfc_hba struct.
3475 *
3476 * Return codes:
3477 * 1 - Error
3478 * 0 - Success
3479 **/
3480static inline int
James Smartc4908502019-01-28 11:14:28 -08003481lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
James Smartacd68592012-01-18 16:25:09 -05003482{
3483 return phba->lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
3484}
3485
3486/**
James Smart3621a712009-04-06 18:47:14 -04003487 * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
James Smartea2151b2008-09-07 11:52:10 -04003488 * @phba: Pointer to hba context object.
3489 * @vport: Pointer to vport object.
3490 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
3491 * @rsp_iocb: Pointer to response iocb object which reported error.
3492 *
3493 * This function posts an event when there is a SCSI command reporting
3494 * error from the scsi device.
3495 **/
3496static void
3497lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
James Smartc4908502019-01-28 11:14:28 -08003498 struct lpfc_io_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
James Smartea2151b2008-09-07 11:52:10 -04003499 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3500 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
3501 uint32_t resp_info = fcprsp->rspStatus2;
3502 uint32_t scsi_status = fcprsp->rspStatus3;
3503 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
3504 struct lpfc_fast_path_event *fast_path_evt = NULL;
3505 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
3506 unsigned long flags;
3507
James Smart307e3382020-11-15 11:26:30 -08003508 if (!pnode)
James Smart5989b8d2010-10-22 11:06:56 -04003509 return;
3510
James Smartea2151b2008-09-07 11:52:10 -04003511 /* If there is queuefull or busy condition send a scsi event */
3512 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
3513 (cmnd->result == SAM_STAT_BUSY)) {
3514 fast_path_evt = lpfc_alloc_fast_evt(phba);
3515 if (!fast_path_evt)
3516 return;
3517 fast_path_evt->un.scsi_evt.event_type =
3518 FC_REG_SCSI_EVENT;
3519 fast_path_evt->un.scsi_evt.subcategory =
3520 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
3521 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
3522 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
3523 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
3524 &pnode->nlp_portname, sizeof(struct lpfc_name));
3525 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
3526 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3527 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
3528 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
3529 fast_path_evt = lpfc_alloc_fast_evt(phba);
3530 if (!fast_path_evt)
3531 return;
3532 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
3533 FC_REG_SCSI_EVENT;
3534 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
3535 LPFC_EVENT_CHECK_COND;
3536 fast_path_evt->un.check_cond_evt.scsi_event.lun =
3537 cmnd->device->lun;
3538 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
3539 &pnode->nlp_portname, sizeof(struct lpfc_name));
3540 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
3541 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3542 fast_path_evt->un.check_cond_evt.sense_key =
3543 cmnd->sense_buffer[2] & 0xf;
3544 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
3545 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
3546 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3547 fcpi_parm &&
3548 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
3549 ((scsi_status == SAM_STAT_GOOD) &&
3550 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
3551 /*
3552 * If status is good or resid does not match with fcp_param and
3553 * there is valid fcpi_parm, then there is a read_check error
3554 */
3555 fast_path_evt = lpfc_alloc_fast_evt(phba);
3556 if (!fast_path_evt)
3557 return;
3558 fast_path_evt->un.read_check_error.header.event_type =
3559 FC_REG_FABRIC_EVENT;
3560 fast_path_evt->un.read_check_error.header.subcategory =
3561 LPFC_EVENT_FCPRDCHKERR;
3562 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
3563 &pnode->nlp_portname, sizeof(struct lpfc_name));
3564 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
3565 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3566 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
3567 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
3568 fast_path_evt->un.read_check_error.fcpiparam =
3569 fcpi_parm;
3570 } else
3571 return;
3572
3573 fast_path_evt->vport = vport;
3574 spin_lock_irqsave(&phba->hbalock, flags);
3575 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
3576 spin_unlock_irqrestore(&phba->hbalock, flags);
3577 lpfc_worker_wake_up(phba);
3578 return;
3579}
James Smart9bad7672008-12-04 22:39:02 -05003580
3581/**
James Smartf1126682009-06-10 17:22:44 -04003582 * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
James Smart3772a992009-05-22 14:50:54 -04003583 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -05003584 * @psb: The scsi buffer which is going to be un-mapped.
3585 *
3586 * This routine does DMA un-mapping of scatter gather list of scsi command
James Smart3772a992009-05-22 14:50:54 -04003587 * field of @lpfc_cmd for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05003588 **/
dea31012005-04-17 16:05:31 -05003589static void
James Smartc4908502019-01-28 11:14:28 -08003590lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_io_buf *psb)
James Smartbcf4dbf2006-07-06 15:50:08 -04003591{
3592 /*
3593 * There are only two special cases to consider. (1) the scsi command
3594 * requested scatter-gather usage or (2) the scsi command allocated
3595 * a request buffer, but did not request use_sg. There is a third
3596 * case, but it does not require resource deallocation.
3597 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003598 if (psb->seg_cnt > 0)
3599 scsi_dma_unmap(psb->pCmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003600 if (psb->prot_seg_cnt > 0)
3601 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
3602 scsi_prot_sg_count(psb->pCmd),
3603 psb->pCmd->sc_data_direction);
James Smartbcf4dbf2006-07-06 15:50:08 -04003604}
3605
James Smart9bad7672008-12-04 22:39:02 -05003606/**
James Smart3621a712009-04-06 18:47:14 -04003607 * lpfc_handler_fcp_err - FCP response handler
James Smart9bad7672008-12-04 22:39:02 -05003608 * @vport: The virtual port for which this call is being executed.
James Smartc4908502019-01-28 11:14:28 -08003609 * @lpfc_cmd: Pointer to lpfc_io_buf data structure.
James Smart9bad7672008-12-04 22:39:02 -05003610 * @rsp_iocb: The response IOCB which contains FCP error.
3611 *
3612 * This routine is called to process response IOCB with status field
3613 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
3614 * based upon SCSI and FCP error.
3615 **/
James Smartbcf4dbf2006-07-06 15:50:08 -04003616static void
James Smartc4908502019-01-28 11:14:28 -08003617lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_io_buf *lpfc_cmd,
James Smart2e0fef82007-06-17 19:56:36 -05003618 struct lpfc_iocbq *rsp_iocb)
dea31012005-04-17 16:05:31 -05003619{
James Smart5afab6b2015-12-16 18:12:01 -05003620 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003621 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3622 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
3623 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
James Smart7054a602007-04-25 09:52:34 -04003624 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
dea31012005-04-17 16:05:31 -05003625 uint32_t resp_info = fcprsp->rspStatus2;
3626 uint32_t scsi_status = fcprsp->rspStatus3;
James Smartc7743952006-12-02 13:34:42 -05003627 uint32_t *lp;
dea31012005-04-17 16:05:31 -05003628 uint32_t host_status = DID_OK;
3629 uint32_t rsplen = 0;
James Smart5afab6b2015-12-16 18:12:01 -05003630 uint32_t fcpDl;
James Smartc7743952006-12-02 13:34:42 -05003631 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea31012005-04-17 16:05:31 -05003632
James Smartea2151b2008-09-07 11:52:10 -04003633
dea31012005-04-17 16:05:31 -05003634 /*
3635 * If this is a task management command, there is no
3636 * scsi packet associated with this lpfc_cmd. The driver
3637 * consumes it.
3638 */
3639 if (fcpcmd->fcpCntl2) {
3640 scsi_status = 0;
3641 goto out;
3642 }
3643
James Smart6a9c52c2009-10-02 15:16:51 -04003644 if (resp_info & RSP_LEN_VALID) {
3645 rsplen = be32_to_cpu(fcprsp->rspRspLen);
James Smarte40a02c2010-02-26 14:13:54 -05003646 if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
Dick Kennedy372c1872020-06-30 14:50:00 -07003647 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3648 "2719 Invalid response length: "
3649 "tgt x%x lun x%llx cmnd x%x rsplen "
3650 "x%x\n", cmnd->device->id,
3651 cmnd->device->lun, cmnd->cmnd[0],
3652 rsplen);
James Smart6a9c52c2009-10-02 15:16:51 -04003653 host_status = DID_ERROR;
3654 goto out;
3655 }
James Smarte40a02c2010-02-26 14:13:54 -05003656 if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
Dick Kennedy372c1872020-06-30 14:50:00 -07003657 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
James Smarte40a02c2010-02-26 14:13:54 -05003658 "2757 Protocol failure detected during "
3659 "processing of FCP I/O op: "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02003660 "tgt x%x lun x%llx cmnd x%x rspInfo3 x%x\n",
James Smarte40a02c2010-02-26 14:13:54 -05003661 cmnd->device->id,
3662 cmnd->device->lun, cmnd->cmnd[0],
3663 fcprsp->rspInfo3);
3664 host_status = DID_ERROR;
3665 goto out;
3666 }
James Smart6a9c52c2009-10-02 15:16:51 -04003667 }
3668
James Smartc7743952006-12-02 13:34:42 -05003669 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
3670 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
3671 if (snslen > SCSI_SENSE_BUFFERSIZE)
3672 snslen = SCSI_SENSE_BUFFERSIZE;
3673
3674 if (resp_info & RSP_LEN_VALID)
3675 rsplen = be32_to_cpu(fcprsp->rspRspLen);
3676 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
3677 }
3678 lp = (uint32_t *)cmnd->sense_buffer;
3679
James Smartaa1c7ee2012-08-14 14:26:06 -04003680 /* special handling for under run conditions */
3681 if (!scsi_status && (resp_info & RESID_UNDER)) {
3682 /* don't log under runs if fcp set... */
3683 if (vport->cfg_log_verbose & LOG_FCP)
3684 logit = LOG_FCP_ERROR;
3685 /* unless operator says so */
3686 if (vport->cfg_log_verbose & LOG_FCP_UNDER)
3687 logit = LOG_FCP_UNDER;
3688 }
James Smartc7743952006-12-02 13:34:42 -05003689
James Smarte8b62012007-08-02 11:10:09 -04003690 lpfc_printf_vlog(vport, KERN_WARNING, logit,
James Smarte2a0a9d2008-12-04 22:40:02 -05003691 "9024 FCP command x%x failed: x%x SNS x%x x%x "
James Smarte8b62012007-08-02 11:10:09 -04003692 "Data: x%x x%x x%x x%x x%x\n",
3693 cmnd->cmnd[0], scsi_status,
3694 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
3695 be32_to_cpu(fcprsp->rspResId),
3696 be32_to_cpu(fcprsp->rspSnsLen),
3697 be32_to_cpu(fcprsp->rspRspLen),
3698 fcprsp->rspInfo3);
dea31012005-04-17 16:05:31 -05003699
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003700 scsi_set_resid(cmnd, 0);
James Smart5afab6b2015-12-16 18:12:01 -05003701 fcpDl = be32_to_cpu(fcpcmd->fcpDl);
dea31012005-04-17 16:05:31 -05003702 if (resp_info & RESID_UNDER) {
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003703 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea31012005-04-17 16:05:31 -05003704
James Smart73d91e52011-10-10 21:32:10 -04003705 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_UNDER,
James Smart45634a862018-01-30 15:59:00 -08003706 "9025 FCP Underrun, expected %d, "
James Smarte8b62012007-08-02 11:10:09 -04003707 "residual %d Data: x%x x%x x%x\n",
James Smart5afab6b2015-12-16 18:12:01 -05003708 fcpDl,
James Smarte8b62012007-08-02 11:10:09 -04003709 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
3710 cmnd->underflow);
dea31012005-04-17 16:05:31 -05003711
3712 /*
James Smart45634a862018-01-30 15:59:00 -08003713 * If there is an under run, check if under run reported by
James Smart7054a602007-04-25 09:52:34 -04003714 * storage array is same as the under run reported by HBA.
3715 * If this is not same, there is a dropped frame.
3716 */
James Smart45634a862018-01-30 15:59:00 -08003717 if (fcpi_parm && (scsi_get_resid(cmnd) != fcpi_parm)) {
James Smarte8b62012007-08-02 11:10:09 -04003718 lpfc_printf_vlog(vport, KERN_WARNING,
3719 LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05003720 "9026 FCP Read Check Error "
James Smarte8b62012007-08-02 11:10:09 -04003721 "and Underrun Data: x%x x%x x%x x%x\n",
James Smart5afab6b2015-12-16 18:12:01 -05003722 fcpDl,
James Smarte8b62012007-08-02 11:10:09 -04003723 scsi_get_resid(cmnd), fcpi_parm,
3724 cmnd->cmnd[0]);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003725 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
James Smart7054a602007-04-25 09:52:34 -04003726 host_status = DID_ERROR;
3727 }
3728 /*
dea31012005-04-17 16:05:31 -05003729 * The cmnd->underflow is the minimum number of bytes that must
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003730 * be transferred for this command. Provided a sense condition
dea31012005-04-17 16:05:31 -05003731 * is not present, make sure the actual amount transferred is at
3732 * least the underflow value or fail.
3733 */
3734 if (!(resp_info & SNS_LEN_VALID) &&
3735 (scsi_status == SAM_STAT_GOOD) &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003736 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
3737 < cmnd->underflow)) {
James Smarte8b62012007-08-02 11:10:09 -04003738 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05003739 "9027 FCP command x%x residual "
James Smarte8b62012007-08-02 11:10:09 -04003740 "underrun converted to error "
3741 "Data: x%x x%x x%x\n",
James Smart66dbfbe2007-08-05 06:08:38 -04003742 cmnd->cmnd[0], scsi_bufflen(cmnd),
James Smarte8b62012007-08-02 11:10:09 -04003743 scsi_get_resid(cmnd), cmnd->underflow);
dea31012005-04-17 16:05:31 -05003744 host_status = DID_ERROR;
3745 }
3746 } else if (resp_info & RESID_OVER) {
James Smarte8b62012007-08-02 11:10:09 -04003747 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05003748 "9028 FCP command x%x residual overrun error. "
James Smarte4e74272009-07-19 10:01:38 -04003749 "Data: x%x x%x\n", cmnd->cmnd[0],
James Smarte8b62012007-08-02 11:10:09 -04003750 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea31012005-04-17 16:05:31 -05003751 host_status = DID_ERROR;
3752
3753 /*
3754 * Check SLI validation that all the transfer was actually done
James Smart26373d22013-09-06 12:19:17 -04003755 * (fcpi_parm should be zero). Apply check only to reads.
dea31012005-04-17 16:05:31 -05003756 */
James Smart5afab6b2015-12-16 18:12:01 -05003757 } else if (fcpi_parm) {
James Smarte8b62012007-08-02 11:10:09 -04003758 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
James Smart5afab6b2015-12-16 18:12:01 -05003759 "9029 FCP %s Check Error xri x%x Data: "
James Smarteee88772010-09-29 11:19:08 -04003760 "x%x x%x x%x x%x x%x\n",
James Smart5afab6b2015-12-16 18:12:01 -05003761 ((cmnd->sc_data_direction == DMA_FROM_DEVICE) ?
3762 "Read" : "Write"),
3763 ((phba->sli_rev == LPFC_SLI_REV4) ?
3764 lpfc_cmd->cur_iocbq.sli4_xritag :
3765 rsp_iocb->iocb.ulpContext),
3766 fcpDl, be32_to_cpu(fcprsp->rspResId),
James Smarteee88772010-09-29 11:19:08 -04003767 fcpi_parm, cmnd->cmnd[0], scsi_status);
James Smart5afab6b2015-12-16 18:12:01 -05003768
3769 /* There is some issue with the LPe12000 that causes it
3770 * to miscalculate the fcpi_parm and falsely trip this
3771 * recovery logic. Detect this case and don't error when true.
3772 */
3773 if (fcpi_parm > fcpDl)
3774 goto out;
3775
James Smarteee88772010-09-29 11:19:08 -04003776 switch (scsi_status) {
3777 case SAM_STAT_GOOD:
3778 case SAM_STAT_CHECK_CONDITION:
3779 /* Fabric dropped a data frame. Fail any successful
3780 * command in which we detected dropped frames.
3781 * A status of good or some check conditions could
3782 * be considered a successful command.
3783 */
3784 host_status = DID_ERROR;
3785 break;
3786 }
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003787 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea31012005-04-17 16:05:31 -05003788 }
3789
3790 out:
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02003791 cmnd->result = host_status << 16 | scsi_status;
James Smartea2151b2008-09-07 11:52:10 -04003792 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
dea31012005-04-17 16:05:31 -05003793}
3794
James Smart9bad7672008-12-04 22:39:02 -05003795/**
James Smart3621a712009-04-06 18:47:14 -04003796 * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
James Smart9bad7672008-12-04 22:39:02 -05003797 * @phba: The Hba for which this call is being executed.
3798 * @pIocbIn: The command IOCBQ for the scsi cmnd.
James Smart3772a992009-05-22 14:50:54 -04003799 * @pIocbOut: The response IOCBQ for the scsi cmnd.
James Smart9bad7672008-12-04 22:39:02 -05003800 *
3801 * This routine assigns scsi command result by looking into response IOCB
3802 * status field appropriately. This routine handles QUEUE FULL condition as
3803 * well by ramping down device queue depth.
3804 **/
dea31012005-04-17 16:05:31 -05003805static void
3806lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
3807 struct lpfc_iocbq *pIocbOut)
3808{
James Smartc4908502019-01-28 11:14:28 -08003809 struct lpfc_io_buf *lpfc_cmd =
3810 (struct lpfc_io_buf *) pIocbIn->context1;
James Smart2e0fef82007-06-17 19:56:36 -05003811 struct lpfc_vport *vport = pIocbIn->vport;
dea31012005-04-17 16:05:31 -05003812 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
3813 struct lpfc_nodelist *pnode = rdata->pnode;
James Smart75baf692010-06-08 18:31:21 -04003814 struct scsi_cmnd *cmd;
James Smartfa61a542008-01-11 01:52:42 -05003815 unsigned long flags;
James Smartea2151b2008-09-07 11:52:10 -04003816 struct lpfc_fast_path_event *fast_path_evt;
James Smart75baf692010-06-08 18:31:21 -04003817 struct Scsi_Host *shost;
James Smart4c47efc2019-01-28 11:14:25 -08003818 int idx;
James Smart73d91e52011-10-10 21:32:10 -04003819 uint32_t logit = LOG_FCP;
dea31012005-04-17 16:05:31 -05003820
James Smartc2017262019-01-28 11:14:37 -08003821 /* Guard against abort handler being called at same time */
3822 spin_lock(&lpfc_cmd->buf_lock);
3823
James Smart75baf692010-06-08 18:31:21 -04003824 /* Sanity check on return of outstanding command */
James Smart75baf692010-06-08 18:31:21 -04003825 cmd = lpfc_cmd->pCmd;
James Smart43bfea12019-09-21 20:58:57 -07003826 if (!cmd || !phba) {
Dick Kennedy372c1872020-06-30 14:50:00 -07003827 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
James Smartc2017262019-01-28 11:14:37 -08003828 "2621 IO completion: Not an active IO\n");
3829 spin_unlock(&lpfc_cmd->buf_lock);
James Smartc90261d2015-12-16 18:11:57 -05003830 return;
James Smartc2017262019-01-28 11:14:37 -08003831 }
James Smart4c47efc2019-01-28 11:14:25 -08003832
3833 idx = lpfc_cmd->cur_iocbq.hba_wqidx;
3834 if (phba->sli4_hba.hdwq)
3835 phba->sli4_hba.hdwq[idx].scsi_cstat.io_cmpls++;
3836
James Smart6a828b02019-01-28 11:14:31 -08003837#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart840eda92020-03-22 11:13:00 -07003838 if (unlikely(phba->hdwqstat_on & LPFC_CHECK_SCSI_IO))
3839 this_cpu_inc(phba->sli4_hba.c_stat->cmpl_io);
James Smart6a828b02019-01-28 11:14:31 -08003840#endif
James Smart75baf692010-06-08 18:31:21 -04003841 shost = cmd->device->host;
3842
James Smarte3d2b802012-08-14 14:25:43 -04003843 lpfc_cmd->result = (pIocbOut->iocb.un.ulpWord[4] & IOERR_PARAM_MASK);
dea31012005-04-17 16:05:31 -05003844 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
James Smart341af102010-01-26 23:07:37 -05003845 /* pick up SLI4 exhange busy status from HBA */
James Smart324e1c42019-10-18 14:18:21 -07003846 if (pIocbOut->iocb_flag & LPFC_EXCHANGE_BUSY)
3847 lpfc_cmd->flags |= LPFC_SBUF_XBUSY;
3848 else
3849 lpfc_cmd->flags &= ~LPFC_SBUF_XBUSY;
James Smart341af102010-01-26 23:07:37 -05003850
James Smart9a6b09c2012-03-01 22:37:42 -05003851#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
3852 if (lpfc_cmd->prot_data_type) {
3853 struct scsi_dif_tuple *src = NULL;
3854
3855 src = (struct scsi_dif_tuple *)lpfc_cmd->prot_data_segment;
3856 /*
3857 * Used to restore any changes to protection
3858 * data for error injection.
3859 */
3860 switch (lpfc_cmd->prot_data_type) {
3861 case LPFC_INJERR_REFTAG:
3862 src->ref_tag =
3863 lpfc_cmd->prot_data;
3864 break;
3865 case LPFC_INJERR_APPTAG:
3866 src->app_tag =
3867 (uint16_t)lpfc_cmd->prot_data;
3868 break;
3869 case LPFC_INJERR_GUARD:
3870 src->guard_tag =
3871 (uint16_t)lpfc_cmd->prot_data;
3872 break;
3873 default:
3874 break;
3875 }
3876
3877 lpfc_cmd->prot_data = 0;
3878 lpfc_cmd->prot_data_type = 0;
3879 lpfc_cmd->prot_data_segment = NULL;
3880 }
3881#endif
James Smart2ea259e2017-02-12 13:52:27 -08003882
James Smart22770cb2019-10-18 14:18:24 -07003883 if (unlikely(lpfc_cmd->status)) {
dea31012005-04-17 16:05:31 -05003884 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
3885 (lpfc_cmd->result & IOERR_DRVR_MASK))
3886 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
3887 else if (lpfc_cmd->status >= IOSTAT_CNT)
3888 lpfc_cmd->status = IOSTAT_DEFAULT;
James Smartaa1c7ee2012-08-14 14:26:06 -04003889 if (lpfc_cmd->status == IOSTAT_FCP_RSP_ERROR &&
3890 !lpfc_cmd->fcp_rsp->rspStatus3 &&
3891 (lpfc_cmd->fcp_rsp->rspStatus2 & RESID_UNDER) &&
3892 !(vport->cfg_log_verbose & LOG_FCP_UNDER))
James Smart73d91e52011-10-10 21:32:10 -04003893 logit = 0;
3894 else
3895 logit = LOG_FCP | LOG_FCP_UNDER;
3896 lpfc_printf_vlog(vport, KERN_WARNING, logit,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02003897 "9030 FCP cmd x%x failed <%d/%lld> "
James Smart5a0d80f2012-05-09 21:18:20 -04003898 "status: x%x result: x%x "
3899 "sid: x%x did: x%x oxid: x%x "
3900 "Data: x%x x%x\n",
James Smart73d91e52011-10-10 21:32:10 -04003901 cmd->cmnd[0],
3902 cmd->device ? cmd->device->id : 0xffff,
3903 cmd->device ? cmd->device->lun : 0xffff,
3904 lpfc_cmd->status, lpfc_cmd->result,
James Smart3bf41ba2013-05-31 17:03:18 -04003905 vport->fc_myDID,
3906 (pnode) ? pnode->nlp_DID : 0,
James Smart5a0d80f2012-05-09 21:18:20 -04003907 phba->sli_rev == LPFC_SLI_REV4 ?
3908 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
James Smart73d91e52011-10-10 21:32:10 -04003909 pIocbOut->iocb.ulpContext,
3910 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
dea31012005-04-17 16:05:31 -05003911
3912 switch (lpfc_cmd->status) {
3913 case IOSTAT_FCP_RSP_ERROR:
3914 /* Call FCP RSP handler to determine result */
James Smart2e0fef82007-06-17 19:56:36 -05003915 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea31012005-04-17 16:05:31 -05003916 break;
3917 case IOSTAT_NPORT_BSY:
3918 case IOSTAT_FABRIC_BSY:
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02003919 cmd->result = DID_TRANSPORT_DISRUPTED << 16;
James Smartea2151b2008-09-07 11:52:10 -04003920 fast_path_evt = lpfc_alloc_fast_evt(phba);
3921 if (!fast_path_evt)
3922 break;
3923 fast_path_evt->un.fabric_evt.event_type =
3924 FC_REG_FABRIC_EVENT;
3925 fast_path_evt->un.fabric_evt.subcategory =
3926 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
3927 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
James Smart307e3382020-11-15 11:26:30 -08003928 if (pnode) {
James Smartea2151b2008-09-07 11:52:10 -04003929 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
3930 &pnode->nlp_portname,
3931 sizeof(struct lpfc_name));
3932 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
3933 &pnode->nlp_nodename,
3934 sizeof(struct lpfc_name));
3935 }
3936 fast_path_evt->vport = vport;
3937 fast_path_evt->work_evt.evt =
3938 LPFC_EVT_FASTPATH_MGMT_EVT;
3939 spin_lock_irqsave(&phba->hbalock, flags);
3940 list_add_tail(&fast_path_evt->work_evt.evt_listp,
3941 &phba->work_list);
3942 spin_unlock_irqrestore(&phba->hbalock, flags);
3943 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05003944 break;
James Smart92d7f7b2007-06-17 19:56:38 -05003945 case IOSTAT_LOCAL_REJECT:
James Smart1151e3e2011-02-16 12:39:35 -05003946 case IOSTAT_REMOTE_STOP:
James Smartab56dc22011-02-16 12:39:57 -05003947 if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
3948 lpfc_cmd->result ==
3949 IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
3950 lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
3951 lpfc_cmd->result ==
3952 IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02003953 cmd->result = DID_NO_CONNECT << 16;
James Smartab56dc22011-02-16 12:39:57 -05003954 break;
3955 }
James Smartd7c255b2008-08-24 21:50:00 -04003956 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
James Smart92d7f7b2007-06-17 19:56:38 -05003957 lpfc_cmd->result == IOERR_NO_RESOURCES ||
James Smartb92938b2010-06-07 15:24:12 -04003958 lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
3959 lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02003960 cmd->result = DID_REQUEUE << 16;
James Smart58da1ff2008-04-07 10:15:56 -04003961 break;
James Smarte2a0a9d2008-12-04 22:40:02 -05003962 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003963 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
3964 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
3965 pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
3966 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
3967 /*
3968 * This is a response for a BG enabled
3969 * cmd. Parse BG error
3970 */
3971 lpfc_parse_bg_err(phba, lpfc_cmd,
3972 pIocbOut);
3973 break;
3974 } else {
3975 lpfc_printf_vlog(vport, KERN_WARNING,
3976 LOG_BG,
3977 "9031 non-zero BGSTAT "
James Smart6a9c52c2009-10-02 15:16:51 -04003978 "on unprotected cmd\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05003979 }
3980 }
James Smart1151e3e2011-02-16 12:39:35 -05003981 if ((lpfc_cmd->status == IOSTAT_REMOTE_STOP)
3982 && (phba->sli_rev == LPFC_SLI_REV4)
James Smart307e3382020-11-15 11:26:30 -08003983 && pnode) {
James Smart1151e3e2011-02-16 12:39:35 -05003984 /* This IO was aborted by the target, we don't
3985 * know the rxid and because we did not send the
3986 * ABTS we cannot generate and RRQ.
3987 */
3988 lpfc_set_rrq_active(phba, pnode,
James Smartee0f4fe2012-05-09 21:19:14 -04003989 lpfc_cmd->cur_iocbq.sli4_lxritag,
3990 0, 0);
James Smart1151e3e2011-02-16 12:39:35 -05003991 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003992 fallthrough;
dea31012005-04-17 16:05:31 -05003993 default:
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02003994 cmd->result = DID_ERROR << 16;
dea31012005-04-17 16:05:31 -05003995 break;
3996 }
3997
James Smart307e3382020-11-15 11:26:30 -08003998 if (!pnode || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02003999 cmd->result = DID_TRANSPORT_DISRUPTED << 16 |
4000 SAM_STAT_BUSY;
James Smartab56dc22011-02-16 12:39:57 -05004001 } else
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02004002 cmd->result = DID_OK << 16;
dea31012005-04-17 16:05:31 -05004003
4004 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
4005 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
4006
James Smarte8b62012007-08-02 11:10:09 -04004007 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smart32350662019-08-14 16:57:06 -07004008 "0710 Iodone <%d/%llu> cmd x%px, error "
James Smarte8b62012007-08-02 11:10:09 -04004009 "x%x SNS x%x x%x Data: x%x x%x\n",
4010 cmd->device->id, cmd->device->lun, cmd,
4011 cmd->result, *lp, *(lp + 3), cmd->retries,
4012 scsi_get_resid(cmd));
dea31012005-04-17 16:05:31 -05004013 }
4014
James Smart9df0a032019-09-21 20:58:54 -07004015 lpfc_update_stats(vport, lpfc_cmd);
James Smart977b5a02008-09-07 11:52:04 -04004016 if (vport->cfg_max_scsicmpl_time &&
4017 time_after(jiffies, lpfc_cmd->start_time +
4018 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
James Smarta257bf92009-04-06 18:48:10 -04004019 spin_lock_irqsave(shost->host_lock, flags);
James Smart307e3382020-11-15 11:26:30 -08004020 if (pnode) {
James Smart109f6ed2008-12-04 22:39:08 -05004021 if (pnode->cmd_qdepth >
4022 atomic_read(&pnode->cmd_pending) &&
4023 (atomic_read(&pnode->cmd_pending) >
4024 LPFC_MIN_TGT_QDEPTH) &&
4025 ((cmd->cmnd[0] == READ_10) ||
4026 (cmd->cmnd[0] == WRITE_10)))
4027 pnode->cmd_qdepth =
4028 atomic_read(&pnode->cmd_pending);
James Smart977b5a02008-09-07 11:52:04 -04004029
James Smart109f6ed2008-12-04 22:39:08 -05004030 pnode->last_change_time = jiffies;
4031 }
James Smarta257bf92009-04-06 18:48:10 -04004032 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart977b5a02008-09-07 11:52:04 -04004033 }
James Smart1dcb58e2007-04-25 09:51:30 -04004034 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James Smarta257bf92009-04-06 18:48:10 -04004035
James Smartc2017262019-01-28 11:14:37 -08004036 lpfc_cmd->pCmd = NULL;
4037 spin_unlock(&lpfc_cmd->buf_lock);
James Smart92e3af62012-08-14 14:26:28 -04004038
James Smart2fcbc562020-03-22 11:13:02 -07004039#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4040 if (lpfc_cmd->ts_cmd_start) {
4041 lpfc_cmd->ts_isr_cmpl = pIocbIn->isr_timestamp;
4042 lpfc_cmd->ts_data_io = ktime_get_ns();
4043 phba->ktime_last_cmd = lpfc_cmd->ts_data_io;
4044 lpfc_io_ktime(phba, lpfc_cmd);
4045 }
4046#endif
James Smart89533e92016-10-13 15:06:15 -07004047 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
4048 cmd->scsi_done(cmd);
4049
James Smartfa61a542008-01-11 01:52:42 -05004050 /*
James Smartc2017262019-01-28 11:14:37 -08004051 * If there is an abort thread waiting for command completion
James Smartfa61a542008-01-11 01:52:42 -05004052 * wake up the thread.
4053 */
James Smartc2017262019-01-28 11:14:37 -08004054 spin_lock(&lpfc_cmd->buf_lock);
James Smart4eb01532019-04-03 11:10:34 -07004055 lpfc_cmd->cur_iocbq.iocb_flag &= ~LPFC_DRIVER_ABORTED;
James Smartb9e5a2d2019-05-21 17:49:01 -07004056 if (lpfc_cmd->waitq)
James Smart4eb01532019-04-03 11:10:34 -07004057 wake_up(lpfc_cmd->waitq);
James Smartc2017262019-01-28 11:14:37 -08004058 spin_unlock(&lpfc_cmd->buf_lock);
James Smartfa61a542008-01-11 01:52:42 -05004059
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004060 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004061}
4062
James Smart34b02dc2008-08-24 21:49:55 -04004063/**
Alexey Kardashevskiy8b2564e2015-04-28 18:26:22 +10004064 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
4065 * @data: A pointer to the immediate command data portion of the IOCB.
4066 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
4067 *
4068 * The routine copies the entire FCP command from @fcp_cmnd to @data while
4069 * byte swapping the data to big endian format for transmission on the wire.
4070 **/
4071static void
4072lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
4073{
4074 int i, j;
4075 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
4076 i += sizeof(uint32_t), j++) {
4077 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
4078 }
4079}
4080
4081/**
James Smartf1126682009-06-10 17:22:44 -04004082 * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05004083 * @vport: The virtual port for which this call is being executed.
4084 * @lpfc_cmd: The scsi command which needs to send.
4085 * @pnode: Pointer to lpfc_nodelist.
4086 *
4087 * This routine initializes fcp_cmnd and iocb data structure from scsi command
James Smart3772a992009-05-22 14:50:54 -04004088 * to transfer for device with SLI3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05004089 **/
dea31012005-04-17 16:05:31 -05004090static void
James Smartc4908502019-01-28 11:14:28 -08004091lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_io_buf *lpfc_cmd,
James Smart2e0fef82007-06-17 19:56:36 -05004092 struct lpfc_nodelist *pnode)
dea31012005-04-17 16:05:31 -05004093{
James Smart2e0fef82007-06-17 19:56:36 -05004094 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004095 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
4096 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
4097 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
4098 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
James Smart4c47efc2019-01-28 11:14:25 -08004099 struct lpfc_sli4_hdw_queue *hdwq = NULL;
dea31012005-04-17 16:05:31 -05004100 int datadir = scsi_cmnd->sc_data_direction;
James Smart4c47efc2019-01-28 11:14:25 -08004101 int idx;
James Smart027140e2012-08-03 12:35:44 -04004102 uint8_t *ptr;
4103 bool sli4;
James Smart98bbf5f2013-09-06 12:18:45 -04004104 uint32_t fcpdl;
dea31012005-04-17 16:05:31 -05004105
James Smart307e3382020-11-15 11:26:30 -08004106 if (!pnode)
James Smart58da1ff2008-04-07 10:15:56 -04004107 return;
4108
dea31012005-04-17 16:05:31 -05004109 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
James.Smart@Emulex.Com69859dc2005-08-10 15:02:37 -04004110 /* clear task management bits */
4111 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea31012005-04-17 16:05:31 -05004112
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -04004113 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
4114 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05004115
James Smart027140e2012-08-03 12:35:44 -04004116 ptr = &fcp_cmnd->fcpCdb[0];
4117 memcpy(ptr, scsi_cmnd->cmnd, scsi_cmnd->cmd_len);
4118 if (scsi_cmnd->cmd_len < LPFC_FCP_CDB_LEN) {
4119 ptr += scsi_cmnd->cmd_len;
4120 memset(ptr, 0, (LPFC_FCP_CDB_LEN - scsi_cmnd->cmd_len));
4121 }
4122
Christoph Hellwig50668632014-10-30 14:30:06 +01004123 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
dea31012005-04-17 16:05:31 -05004124
James Smart027140e2012-08-03 12:35:44 -04004125 sli4 = (phba->sli_rev == LPFC_SLI_REV4);
James Smart6acb3482014-04-04 13:51:25 -04004126 piocbq->iocb.un.fcpi.fcpi_XRdy = 0;
James Smart1fbf9742019-01-28 11:14:26 -08004127 idx = lpfc_cmd->hdwq_no;
James Smart4c47efc2019-01-28 11:14:25 -08004128 if (phba->sli4_hba.hdwq)
4129 hdwq = &phba->sli4_hba.hdwq[idx];
James Smart027140e2012-08-03 12:35:44 -04004130
dea31012005-04-17 16:05:31 -05004131 /*
4132 * There are three possibilities here - use scatter-gather segment, use
4133 * the single mapping, or neither. Start the lpfc command prep by
4134 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
4135 * data bde entry.
4136 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05004137 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05004138 if (datadir == DMA_TO_DEVICE) {
4139 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
James Smart182ba752013-07-15 18:34:05 -04004140 iocb_cmd->ulpPU = PARM_READ_CHECK;
James Smart3cb01c52013-07-15 18:35:04 -04004141 if (vport->cfg_first_burst_size &&
4142 (pnode->nlp_flag & NLP_FIRSTBURST)) {
James Smart98bbf5f2013-09-06 12:18:45 -04004143 fcpdl = scsi_bufflen(scsi_cmnd);
4144 if (fcpdl < vport->cfg_first_burst_size)
4145 piocbq->iocb.un.fcpi.fcpi_XRdy = fcpdl;
4146 else
4147 piocbq->iocb.un.fcpi.fcpi_XRdy =
4148 vport->cfg_first_burst_size;
James Smart3cb01c52013-07-15 18:35:04 -04004149 }
dea31012005-04-17 16:05:31 -05004150 fcp_cmnd->fcpCntl3 = WRITE_DATA;
James Smart4c47efc2019-01-28 11:14:25 -08004151 if (hdwq)
4152 hdwq->scsi_cstat.output_requests++;
dea31012005-04-17 16:05:31 -05004153 } else {
4154 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
4155 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05004156 fcp_cmnd->fcpCntl3 = READ_DATA;
James Smart4c47efc2019-01-28 11:14:25 -08004157 if (hdwq)
4158 hdwq->scsi_cstat.input_requests++;
dea31012005-04-17 16:05:31 -05004159 }
4160 } else {
4161 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
4162 iocb_cmd->un.fcpi.fcpi_parm = 0;
4163 iocb_cmd->ulpPU = 0;
4164 fcp_cmnd->fcpCntl3 = 0;
James Smart4c47efc2019-01-28 11:14:25 -08004165 if (hdwq)
4166 hdwq->scsi_cstat.control_requests++;
dea31012005-04-17 16:05:31 -05004167 }
Alexey Kardashevskiy8b2564e2015-04-28 18:26:22 +10004168 if (phba->sli_rev == 3 &&
4169 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
4170 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05004171 /*
4172 * Finish initializing those IOCB fields that are independent
4173 * of the scsi_cmnd request_buffer
4174 */
4175 piocbq->iocb.ulpContext = pnode->nlp_rpi;
James Smart027140e2012-08-03 12:35:44 -04004176 if (sli4)
James Smart6d368e52011-05-24 11:44:12 -04004177 piocbq->iocb.ulpContext =
4178 phba->sli4_hba.rpi_ids[pnode->nlp_rpi];
dea31012005-04-17 16:05:31 -05004179 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
4180 piocbq->iocb.ulpFCP2Rcvy = 1;
James Smart09372822008-01-11 01:52:54 -05004181 else
4182 piocbq->iocb.ulpFCP2Rcvy = 0;
dea31012005-04-17 16:05:31 -05004183
4184 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
4185 piocbq->context1 = lpfc_cmd;
4186 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
4187 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
James Smart2e0fef82007-06-17 19:56:36 -05004188 piocbq->vport = vport;
dea31012005-04-17 16:05:31 -05004189}
4190
James Smart9bad7672008-12-04 22:39:02 -05004191/**
James Smart6d368e52011-05-24 11:44:12 -04004192 * lpfc_scsi_prep_task_mgmt_cmd - Convert SLI3 scsi TM cmd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05004193 * @vport: The virtual port for which this call is being executed.
James Smartc4908502019-01-28 11:14:28 -08004194 * @lpfc_cmd: Pointer to lpfc_io_buf data structure.
James Smart9bad7672008-12-04 22:39:02 -05004195 * @lun: Logical unit number.
4196 * @task_mgmt_cmd: SCSI task management command.
4197 *
James Smart3772a992009-05-22 14:50:54 -04004198 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
4199 * for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05004200 *
4201 * Return codes:
4202 * 0 - Error
4203 * 1 - Success
4204 **/
dea31012005-04-17 16:05:31 -05004205static int
James Smartf1126682009-06-10 17:22:44 -04004206lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
James Smartc4908502019-01-28 11:14:28 -08004207 struct lpfc_io_buf *lpfc_cmd,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004208 uint64_t lun,
dea31012005-04-17 16:05:31 -05004209 uint8_t task_mgmt_cmd)
4210{
dea31012005-04-17 16:05:31 -05004211 struct lpfc_iocbq *piocbq;
4212 IOCB_t *piocb;
4213 struct fcp_cmnd *fcp_cmnd;
James Smart0b18ac42006-05-01 21:50:40 -04004214 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea31012005-04-17 16:05:31 -05004215 struct lpfc_nodelist *ndlp = rdata->pnode;
4216
James Smart307e3382020-11-15 11:26:30 -08004217 if (!ndlp || ndlp->nlp_state != NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05004218 return 0;
dea31012005-04-17 16:05:31 -05004219
dea31012005-04-17 16:05:31 -05004220 piocbq = &(lpfc_cmd->cur_iocbq);
James Smart2e0fef82007-06-17 19:56:36 -05004221 piocbq->vport = vport;
4222
dea31012005-04-17 16:05:31 -05004223 piocb = &piocbq->iocb;
4224
4225 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart34b02dc2008-08-24 21:49:55 -04004226 /* Clear out any old data in the FCP command area */
4227 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
4228 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05004229 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
James Smarte2a0a9d2008-12-04 22:40:02 -05004230 if (vport->phba->sli_rev == 3 &&
4231 !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04004232 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05004233 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
dea31012005-04-17 16:05:31 -05004234 piocb->ulpContext = ndlp->nlp_rpi;
James Smart6d368e52011-05-24 11:44:12 -04004235 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
4236 piocb->ulpContext =
4237 vport->phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
4238 }
James Smart53151bb2013-10-10 12:24:07 -04004239 piocb->ulpFCP2Rcvy = (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) ? 1 : 0;
dea31012005-04-17 16:05:31 -05004240 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
James Smartf9226c22014-02-20 09:57:28 -05004241 piocb->ulpPU = 0;
4242 piocb->un.fcpi.fcpi_parm = 0;
dea31012005-04-17 16:05:31 -05004243
4244 /* ulpTimeout is only one byte */
4245 if (lpfc_cmd->timeout > 0xff) {
4246 /*
4247 * Do not timeout the command at the firmware level.
4248 * The driver will provide the timeout mechanism.
4249 */
4250 piocb->ulpTimeout = 0;
James Smartf1126682009-06-10 17:22:44 -04004251 } else
dea31012005-04-17 16:05:31 -05004252 piocb->ulpTimeout = lpfc_cmd->timeout;
James Smartf1126682009-06-10 17:22:44 -04004253
4254 if (vport->phba->sli_rev == LPFC_SLI_REV4)
4255 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004256
James Smart2e0fef82007-06-17 19:56:36 -05004257 return 1;
dea31012005-04-17 16:05:31 -05004258}
4259
James Smart9bad7672008-12-04 22:39:02 -05004260/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004261 * lpfc_scsi_api_table_setup - Set up scsi api function jump table
James Smart3772a992009-05-22 14:50:54 -04004262 * @phba: The hba struct for which this call is being executed.
4263 * @dev_grp: The HBA PCI-Device group number.
4264 *
4265 * This routine sets up the SCSI interface API function jump table in @phba
4266 * struct.
4267 * Returns: 0 - success, -ENODEV - failure.
4268 **/
4269int
4270lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4271{
4272
James Smartf1126682009-06-10 17:22:44 -04004273 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
4274 phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
James Smartf1126682009-06-10 17:22:44 -04004275
James Smart3772a992009-05-22 14:50:54 -04004276 switch (dev_grp) {
4277 case LPFC_PCI_DEV_LP:
James Smart3772a992009-05-22 14:50:54 -04004278 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
James Smartacd68592012-01-18 16:25:09 -05004279 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04004280 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
James Smart19ca7602010-11-20 23:11:55 -05004281 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04004282 break;
James Smartda0436e2009-05-22 14:51:39 -04004283 case LPFC_PCI_DEV_OC:
James Smartda0436e2009-05-22 14:51:39 -04004284 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
James Smartacd68592012-01-18 16:25:09 -05004285 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04004286 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
James Smart19ca7602010-11-20 23:11:55 -05004287 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04004288 break;
James Smart3772a992009-05-22 14:50:54 -04004289 default:
Dick Kennedy372c1872020-06-30 14:50:00 -07004290 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -04004291 "1418 Invalid HBA PCI-device group: 0x%x\n",
4292 dev_grp);
4293 return -ENODEV;
James Smart3772a992009-05-22 14:50:54 -04004294 }
James Smart3772a992009-05-22 14:50:54 -04004295 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
James Smart84d1b002010-02-12 14:42:33 -05004296 phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
James Smart3772a992009-05-22 14:50:54 -04004297 return 0;
4298}
4299
4300/**
James Smart3621a712009-04-06 18:47:14 -04004301 * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
James Smart9bad7672008-12-04 22:39:02 -05004302 * @phba: The Hba for which this call is being executed.
4303 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
4304 * @rspiocbq: Pointer to lpfc_iocbq data structure.
4305 *
4306 * This routine is IOCB completion routine for device reset and target reset
4307 * routine. This routine release scsi buffer associated with lpfc_cmd.
4308 **/
James Smart7054a602007-04-25 09:52:34 -04004309static void
4310lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
4311 struct lpfc_iocbq *cmdiocbq,
4312 struct lpfc_iocbq *rspiocbq)
4313{
James Smartc4908502019-01-28 11:14:28 -08004314 struct lpfc_io_buf *lpfc_cmd =
4315 (struct lpfc_io_buf *) cmdiocbq->context1;
James Smart7054a602007-04-25 09:52:34 -04004316 if (lpfc_cmd)
4317 lpfc_release_scsi_buf(phba, lpfc_cmd);
4318 return;
4319}
4320
James Smart9bad7672008-12-04 22:39:02 -05004321/**
James Smart50212672018-12-13 15:17:57 -08004322 * lpfc_check_pci_resettable - Walks list of devices on pci_dev's bus to check
4323 * if issuing a pci_bus_reset is possibly unsafe
4324 * @phba: lpfc_hba pointer.
4325 *
4326 * Description:
4327 * Walks the bus_list to ensure only PCI devices with Emulex
4328 * vendor id, device ids that support hot reset, and only one occurrence
4329 * of function 0.
4330 *
4331 * Returns:
4332 * -EBADSLT, detected invalid device
4333 * 0, successful
4334 */
4335int
Dick Kennedy372c1872020-06-30 14:50:00 -07004336lpfc_check_pci_resettable(struct lpfc_hba *phba)
James Smart50212672018-12-13 15:17:57 -08004337{
4338 const struct pci_dev *pdev = phba->pcidev;
4339 struct pci_dev *ptr = NULL;
4340 u8 counter = 0;
4341
4342 /* Walk the list of devices on the pci_dev's bus */
4343 list_for_each_entry(ptr, &pdev->bus->devices, bus_list) {
4344 /* Check for Emulex Vendor ID */
4345 if (ptr->vendor != PCI_VENDOR_ID_EMULEX) {
4346 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4347 "8346 Non-Emulex vendor found: "
4348 "0x%04x\n", ptr->vendor);
4349 return -EBADSLT;
4350 }
4351
4352 /* Check for valid Emulex Device ID */
4353 switch (ptr->device) {
4354 case PCI_DEVICE_ID_LANCER_FC:
4355 case PCI_DEVICE_ID_LANCER_G6_FC:
4356 case PCI_DEVICE_ID_LANCER_G7_FC:
4357 break;
4358 default:
4359 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4360 "8347 Invalid device found: "
4361 "0x%04x\n", ptr->device);
4362 return -EBADSLT;
4363 }
4364
4365 /* Check for only one function 0 ID to ensure only one HBA on
4366 * secondary bus
4367 */
4368 if (ptr->devfn == 0) {
4369 if (++counter > 1) {
4370 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4371 "8348 More than one device on "
4372 "secondary bus found\n");
4373 return -EBADSLT;
4374 }
4375 }
4376 }
4377
4378 return 0;
4379}
4380
4381/**
James Smart3621a712009-04-06 18:47:14 -04004382 * lpfc_info - Info entry point of scsi_host_template data structure
James Smart9bad7672008-12-04 22:39:02 -05004383 * @host: The scsi host for which this call is being executed.
4384 *
4385 * This routine provides module information about hba.
4386 *
4387 * Reutrn code:
4388 * Pointer to char - Success.
4389 **/
dea31012005-04-17 16:05:31 -05004390const char *
4391lpfc_info(struct Scsi_Host *host)
4392{
James Smart2e0fef82007-06-17 19:56:36 -05004393 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
4394 struct lpfc_hba *phba = vport->phba;
James Smart50212672018-12-13 15:17:57 -08004395 int link_speed = 0;
4396 static char lpfcinfobuf[384];
4397 char tmp[384] = {0};
dea31012005-04-17 16:05:31 -05004398
James Smart50212672018-12-13 15:17:57 -08004399 memset(lpfcinfobuf, 0, sizeof(lpfcinfobuf));
dea31012005-04-17 16:05:31 -05004400 if (phba && phba->pcidev){
James Smart50212672018-12-13 15:17:57 -08004401 /* Model Description */
4402 scnprintf(tmp, sizeof(tmp), phba->ModelDesc);
4403 if (strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf)) >=
4404 sizeof(lpfcinfobuf))
4405 goto buffer_done;
4406
4407 /* PCI Info */
4408 scnprintf(tmp, sizeof(tmp),
4409 " on PCI bus %02x device %02x irq %d",
4410 phba->pcidev->bus->number, phba->pcidev->devfn,
4411 phba->pcidev->irq);
4412 if (strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf)) >=
4413 sizeof(lpfcinfobuf))
4414 goto buffer_done;
4415
4416 /* Port Number */
dea31012005-04-17 16:05:31 -05004417 if (phba->Port[0]) {
James Smart50212672018-12-13 15:17:57 -08004418 scnprintf(tmp, sizeof(tmp), " port %s", phba->Port);
4419 if (strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf)) >=
4420 sizeof(lpfcinfobuf))
4421 goto buffer_done;
dea31012005-04-17 16:05:31 -05004422 }
James Smart50212672018-12-13 15:17:57 -08004423
4424 /* Link Speed */
James Smarta085e872015-12-16 18:12:02 -05004425 link_speed = lpfc_sli_port_speed_get(phba);
James Smart50212672018-12-13 15:17:57 -08004426 if (link_speed != 0) {
4427 scnprintf(tmp, sizeof(tmp),
4428 " Logical Link Speed: %d Mbps", link_speed);
4429 if (strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf)) >=
4430 sizeof(lpfcinfobuf))
4431 goto buffer_done;
4432 }
4433
4434 /* PCI resettable */
4435 if (!lpfc_check_pci_resettable(phba)) {
4436 scnprintf(tmp, sizeof(tmp), " PCI resettable");
4437 strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf));
4438 }
dea31012005-04-17 16:05:31 -05004439 }
James Smart50212672018-12-13 15:17:57 -08004440
4441buffer_done:
dea31012005-04-17 16:05:31 -05004442 return lpfcinfobuf;
4443}
4444
James Smart9bad7672008-12-04 22:39:02 -05004445/**
James Smart3621a712009-04-06 18:47:14 -04004446 * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
James Smart9bad7672008-12-04 22:39:02 -05004447 * @phba: The Hba for which this call is being executed.
4448 *
4449 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
4450 * The default value of cfg_poll_tmo is 10 milliseconds.
4451 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004452static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
4453{
4454 unsigned long poll_tmo_expires =
4455 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
4456
James Smart895427b2017-02-12 13:52:30 -08004457 if (!list_empty(&phba->sli.sli3_ring[LPFC_FCP_RING].txcmplq))
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004458 mod_timer(&phba->fcp_poll_timer,
4459 poll_tmo_expires);
4460}
4461
James Smart9bad7672008-12-04 22:39:02 -05004462/**
James Smart3621a712009-04-06 18:47:14 -04004463 * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
James Smart9bad7672008-12-04 22:39:02 -05004464 * @phba: The Hba for which this call is being executed.
4465 *
4466 * This routine starts the fcp_poll_timer of @phba.
4467 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004468void lpfc_poll_start_timer(struct lpfc_hba * phba)
4469{
4470 lpfc_poll_rearm_timer(phba);
4471}
4472
James Smart9bad7672008-12-04 22:39:02 -05004473/**
James Smart3621a712009-04-06 18:47:14 -04004474 * lpfc_poll_timeout - Restart polling timer
Lee Joneseceee002020-11-02 14:23:43 +00004475 * @t: Timer construct where lpfc_hba data structure pointer is obtained.
James Smart9bad7672008-12-04 22:39:02 -05004476 *
4477 * This routine restarts fcp_poll timer, when FCP ring polling is enable
4478 * and FCP Ring interrupt is disable.
4479 **/
Kees Cookf22eb4d2017-09-06 20:24:26 -07004480void lpfc_poll_timeout(struct timer_list *t)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004481{
Kees Cookf22eb4d2017-09-06 20:24:26 -07004482 struct lpfc_hba *phba = from_timer(phba, t, fcp_poll_timer);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004483
4484 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004485 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -08004486 &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
James Smart45ed1192009-10-02 15:17:02 -04004487
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004488 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4489 lpfc_poll_rearm_timer(phba);
4490 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004491}
4492
James Smart9bad7672008-12-04 22:39:02 -05004493/**
James Smart3621a712009-04-06 18:47:14 -04004494 * lpfc_queuecommand - scsi_host_template queuecommand entry point
Lee Joneseceee002020-11-02 14:23:43 +00004495 * @shost: kernel scsi host pointer.
James Smart9bad7672008-12-04 22:39:02 -05004496 * @cmnd: Pointer to scsi_cmnd data structure.
James Smart9bad7672008-12-04 22:39:02 -05004497 *
4498 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
4499 * This routine prepares an IOCB from scsi command and provides to firmware.
4500 * The @done callback is invoked after driver finished processing the command.
4501 *
4502 * Return value :
4503 * 0 - Success
4504 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
4505 **/
dea31012005-04-17 16:05:31 -05004506static int
James Smartb9a7c632012-08-03 12:35:24 -04004507lpfc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004508{
James Smart2e0fef82007-06-17 19:56:36 -05004509 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4510 struct lpfc_hba *phba = vport->phba;
James Smart1ba981f2014-02-20 09:56:45 -05004511 struct lpfc_rport_data *rdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05004512 struct lpfc_nodelist *ndlp;
James Smartc4908502019-01-28 11:14:28 -08004513 struct lpfc_io_buf *lpfc_cmd;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004514 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
James Smart4c47efc2019-01-28 11:14:25 -08004515 int err, idx;
James Smart2fcbc562020-03-22 11:13:02 -07004516#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4517 uint64_t start = 0L;
4518
4519 if (phba->ktime_on)
4520 start = ktime_get_ns();
4521#endif
dea31012005-04-17 16:05:31 -05004522
James Smart1ba981f2014-02-20 09:56:45 -05004523 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smartb0e83012018-06-26 08:24:29 -07004524
4525 /* sanity check on references */
4526 if (unlikely(!rdata) || unlikely(!rport))
4527 goto out_fail_command;
4528
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004529 err = fc_remote_port_chkready(rport);
4530 if (err) {
4531 cmnd->result = err;
dea31012005-04-17 16:05:31 -05004532 goto out_fail_command;
4533 }
James Smart1c6f4ef52009-11-18 15:40:49 -05004534 ndlp = rdata->pnode;
dea31012005-04-17 16:05:31 -05004535
James Smartbf086112011-08-21 21:48:13 -04004536 if ((scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) &&
James Smartacd68592012-01-18 16:25:09 -05004537 (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED))) {
James Smarte2a0a9d2008-12-04 22:40:02 -05004538
Dick Kennedy372c1872020-06-30 14:50:00 -07004539 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6a9c52c2009-10-02 15:16:51 -04004540 "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
4541 " op:%02x str=%s without registering for"
4542 " BlockGuard - Rejecting command\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05004543 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
4544 dif_op_str[scsi_get_prot_op(cmnd)]);
4545 goto out_fail_command;
4546 }
4547
dea31012005-04-17 16:05:31 -05004548 /*
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004549 * Catch race where our node has transitioned, but the
4550 * transport is still transitioning.
dea31012005-04-17 16:05:31 -05004551 */
James Smart307e3382020-11-15 11:26:30 -08004552 if (!ndlp)
James Smart6b415f52012-06-12 13:54:59 -04004553 goto out_tgt_busy;
James Smart2a5b7d62018-07-31 17:23:22 -07004554 if (lpfc_ndlp_check_qdepth(phba, ndlp)) {
4555 if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth) {
4556 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_ERROR,
4557 "3377 Target Queue Full, scsi Id:%d "
4558 "Qdepth:%d Pending command:%d"
4559 " WWNN:%02x:%02x:%02x:%02x:"
4560 "%02x:%02x:%02x:%02x, "
4561 " WWPN:%02x:%02x:%02x:%02x:"
4562 "%02x:%02x:%02x:%02x",
4563 ndlp->nlp_sid, ndlp->cmd_qdepth,
4564 atomic_read(&ndlp->cmd_pending),
4565 ndlp->nlp_nodename.u.wwn[0],
4566 ndlp->nlp_nodename.u.wwn[1],
4567 ndlp->nlp_nodename.u.wwn[2],
4568 ndlp->nlp_nodename.u.wwn[3],
4569 ndlp->nlp_nodename.u.wwn[4],
4570 ndlp->nlp_nodename.u.wwn[5],
4571 ndlp->nlp_nodename.u.wwn[6],
4572 ndlp->nlp_nodename.u.wwn[7],
4573 ndlp->nlp_portname.u.wwn[0],
4574 ndlp->nlp_portname.u.wwn[1],
4575 ndlp->nlp_portname.u.wwn[2],
4576 ndlp->nlp_portname.u.wwn[3],
4577 ndlp->nlp_portname.u.wwn[4],
4578 ndlp->nlp_portname.u.wwn[5],
4579 ndlp->nlp_portname.u.wwn[6],
4580 ndlp->nlp_portname.u.wwn[7]);
4581 goto out_tgt_busy;
4582 }
James Smart64bf0092018-01-30 15:58:53 -08004583 }
James Smartf91bc592018-04-09 14:24:22 -07004584
James Smartace44e42019-01-28 11:14:27 -08004585 lpfc_cmd = lpfc_get_scsi_buf(phba, ndlp, cmnd);
dea31012005-04-17 16:05:31 -05004586 if (lpfc_cmd == NULL) {
James Smarteaf15d52008-12-04 22:39:29 -05004587 lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05004588
James Smart895427b2017-02-12 13:52:30 -08004589 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_ERROR,
James Smarte8b62012007-08-02 11:10:09 -04004590 "0707 driver's buffer pool is empty, "
4591 "IO busied\n");
dea31012005-04-17 16:05:31 -05004592 goto out_host_busy;
4593 }
4594
4595 /*
4596 * Store the midlayer's command structure for the completion phase
4597 * and complete the command initialization.
4598 */
4599 lpfc_cmd->pCmd = cmnd;
4600 lpfc_cmd->rdata = rdata;
James Smart2a5b7d62018-07-31 17:23:22 -07004601 lpfc_cmd->ndlp = ndlp;
dea31012005-04-17 16:05:31 -05004602 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
dea31012005-04-17 16:05:31 -05004603
James Smarte2a0a9d2008-12-04 22:40:02 -05004604 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
James Smart6a9c52c2009-10-02 15:16:51 -04004605 if (vport->phba->cfg_enable_bg) {
James Smart737d4242013-04-17 20:14:49 -04004606 lpfc_printf_vlog(vport,
4607 KERN_INFO, LOG_SCSI_CMD,
James Smart26134702012-08-14 14:25:50 -04004608 "9033 BLKGRD: rcvd %s cmd:x%x "
4609 "sector x%llx cnt %u pt %x\n",
4610 dif_op_str[scsi_get_prot_op(cmnd)],
4611 cmnd->cmnd[0],
4612 (unsigned long long)scsi_get_lba(cmnd),
4613 blk_rq_sectors(cmnd->request),
4614 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004615 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004616 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
4617 } else {
James Smart6a9c52c2009-10-02 15:16:51 -04004618 if (vport->phba->cfg_enable_bg) {
James Smart737d4242013-04-17 20:14:49 -04004619 lpfc_printf_vlog(vport,
4620 KERN_INFO, LOG_SCSI_CMD,
James Smart26134702012-08-14 14:25:50 -04004621 "9038 BLKGRD: rcvd PROT_NORMAL cmd: "
4622 "x%x sector x%llx cnt %u pt %x\n",
4623 cmnd->cmnd[0],
4624 (unsigned long long)scsi_get_lba(cmnd),
James Smart9a6b09c2012-03-01 22:37:42 -05004625 blk_rq_sectors(cmnd->request),
James Smart26134702012-08-14 14:25:50 -04004626 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004627 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004628 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
4629 }
4630
James Smart22770cb2019-10-18 14:18:24 -07004631 if (unlikely(err)) {
4632 if (err == 2) {
4633 cmnd->result = DID_ERROR << 16;
4634 goto out_fail_command_release_buf;
4635 }
dea31012005-04-17 16:05:31 -05004636 goto out_host_busy_free_buf;
James Smart5e0e2312019-08-14 16:56:54 -07004637 }
dea31012005-04-17 16:05:31 -05004638
James Smart2e0fef82007-06-17 19:56:36 -05004639 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea31012005-04-17 16:05:31 -05004640
James Smart6a828b02019-01-28 11:14:31 -08004641#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart840eda92020-03-22 11:13:00 -07004642 if (unlikely(phba->hdwqstat_on & LPFC_CHECK_SCSI_IO))
4643 this_cpu_inc(phba->sli4_hba.c_stat->xmt_io);
James Smart6a828b02019-01-28 11:14:31 -08004644#endif
James Smart3772a992009-05-22 14:50:54 -04004645 err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
James Smart92d7f7b2007-06-17 19:56:38 -05004646 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
James Smart2fcbc562020-03-22 11:13:02 -07004647#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4648 if (start) {
4649 lpfc_cmd->ts_cmd_start = start;
4650 lpfc_cmd->ts_last_cmd = phba->ktime_last_cmd;
4651 lpfc_cmd->ts_cmd_wqput = ktime_get_ns();
4652 } else {
4653 lpfc_cmd->ts_cmd_start = 0;
4654 }
4655#endif
James Smarteaf15d52008-12-04 22:39:29 -05004656 if (err) {
James Smart76f96b62013-12-17 20:29:01 -05004657 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4658 "3376 FCP could not issue IOCB err %x"
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004659 "FCP cmd x%x <%d/%llu> "
James Smart76f96b62013-12-17 20:29:01 -05004660 "sid: x%x did: x%x oxid: x%x "
4661 "Data: x%x x%x x%x x%x\n",
4662 err, cmnd->cmnd[0],
4663 cmnd->device ? cmnd->device->id : 0xffff,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004664 cmnd->device ? cmnd->device->lun : (u64) -1,
James Smart76f96b62013-12-17 20:29:01 -05004665 vport->fc_myDID, ndlp->nlp_DID,
4666 phba->sli_rev == LPFC_SLI_REV4 ?
4667 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
4668 lpfc_cmd->cur_iocbq.iocb.ulpContext,
4669 lpfc_cmd->cur_iocbq.iocb.ulpIoTag,
4670 lpfc_cmd->cur_iocbq.iocb.ulpTimeout,
4671 (uint32_t)
4672 (cmnd->request->timeout / 1000));
4673
dea31012005-04-17 16:05:31 -05004674 goto out_host_busy_free_buf;
James Smarteaf15d52008-12-04 22:39:29 -05004675 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004676 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004677 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -08004678 &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
James Smart45ed1192009-10-02 15:17:02 -04004679
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004680 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4681 lpfc_poll_rearm_timer(phba);
4682 }
4683
James Smartc4908502019-01-28 11:14:28 -08004684 if (phba->cfg_xri_rebalancing)
4685 lpfc_keep_pvt_pool_above_lowwm(phba, lpfc_cmd->hdwq_no);
4686
dea31012005-04-17 16:05:31 -05004687 return 0;
4688
4689 out_host_busy_free_buf:
James Smart1fbf9742019-01-28 11:14:26 -08004690 idx = lpfc_cmd->hdwq_no;
James Smartbcf4dbf2006-07-06 15:50:08 -04004691 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James Smart4c47efc2019-01-28 11:14:25 -08004692 if (phba->sli4_hba.hdwq) {
4693 switch (lpfc_cmd->fcp_cmnd->fcpCntl3) {
4694 case WRITE_DATA:
4695 phba->sli4_hba.hdwq[idx].scsi_cstat.output_requests--;
4696 break;
4697 case READ_DATA:
4698 phba->sli4_hba.hdwq[idx].scsi_cstat.input_requests--;
4699 break;
4700 default:
4701 phba->sli4_hba.hdwq[idx].scsi_cstat.control_requests--;
4702 }
4703 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004704 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004705 out_host_busy:
4706 return SCSI_MLQUEUE_HOST_BUSY;
4707
Mike Christie34963432011-02-25 14:04:28 -06004708 out_tgt_busy:
4709 return SCSI_MLQUEUE_TARGET_BUSY;
4710
James Smart5e0e2312019-08-14 16:56:54 -07004711 out_fail_command_release_buf:
4712 lpfc_release_scsi_buf(phba, lpfc_cmd);
4713
dea31012005-04-17 16:05:31 -05004714 out_fail_command:
James Smartb9a7c632012-08-03 12:35:24 -04004715 cmnd->scsi_done(cmnd);
dea31012005-04-17 16:05:31 -05004716 return 0;
4717}
4718
Jeff Garzikf2812332010-11-16 02:10:29 -05004719
James Smart9bad7672008-12-04 22:39:02 -05004720/**
James Smart3621a712009-04-06 18:47:14 -04004721 * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05004722 * @cmnd: Pointer to scsi_cmnd data structure.
4723 *
4724 * This routine aborts @cmnd pending in base driver.
4725 *
4726 * Return code :
4727 * 0x2003 - Error
4728 * 0x2002 - Success
4729 **/
dea31012005-04-17 16:05:31 -05004730static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05004731lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004732{
James Smart2e0fef82007-06-17 19:56:36 -05004733 struct Scsi_Host *shost = cmnd->device->host;
4734 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4735 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004736 struct lpfc_iocbq *iocb;
4737 struct lpfc_iocbq *abtsiocb;
James Smartc4908502019-01-28 11:14:28 -08004738 struct lpfc_io_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05004739 IOCB_t *cmd, *icmd;
James Smart3a707302012-06-12 13:54:42 -04004740 int ret = SUCCESS, status = 0;
James Smart8931c732018-07-31 17:23:20 -07004741 struct lpfc_sli_ring *pring_s4 = NULL;
James Smart895427b2017-02-12 13:52:30 -08004742 int ret_val;
James Smart59c68ea2018-04-09 14:24:25 -07004743 unsigned long flags;
James Smartfa61a542008-01-11 01:52:42 -05004744 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004745
James Smart3a707302012-06-12 13:54:42 -04004746 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04004747 if (status != 0 && status != SUCCESS)
James Smart3a707302012-06-12 13:54:42 -04004748 return status;
James Smart4f2e66c2012-05-09 21:17:07 -04004749
James Smartc2017262019-01-28 11:14:37 -08004750 lpfc_cmd = (struct lpfc_io_buf *)cmnd->host_scribble;
4751 if (!lpfc_cmd)
4752 return ret;
4753
James Smart876dd7d2012-09-29 11:31:28 -04004754 spin_lock_irqsave(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004755 /* driver queued commands are in process of being flushed */
James Smartc00f62e2019-08-14 16:57:11 -07004756 if (phba->hba_flag & HBA_IOQ_FLUSH) {
James Smart4f2e66c2012-05-09 21:17:07 -04004757 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4758 "3168 SCSI Layer abort requested I/O has been "
4759 "flushed by LLD.\n");
James Smartc2017262019-01-28 11:14:37 -08004760 ret = FAILED;
4761 goto out_unlock;
James Smart4f2e66c2012-05-09 21:17:07 -04004762 }
4763
James Smartc2017262019-01-28 11:14:37 -08004764 /* Guard against IO completion being called at same time */
4765 spin_lock(&lpfc_cmd->buf_lock);
4766
4767 if (!lpfc_cmd->pCmd) {
James Smarteee88772010-09-29 11:19:08 -04004768 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4769 "2873 SCSI Layer I/O Abort Request IO CMPL Status "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004770 "x%x ID %d LUN %llu\n",
James Smart3a707302012-06-12 13:54:42 -04004771 SUCCESS, cmnd->device->id, cmnd->device->lun);
James Smartc2017262019-01-28 11:14:37 -08004772 goto out_unlock_buf;
James Smarteee88772010-09-29 11:19:08 -04004773 }
dea31012005-04-17 16:05:31 -05004774
James Smart4f2e66c2012-05-09 21:17:07 -04004775 iocb = &lpfc_cmd->cur_iocbq;
James Smart8931c732018-07-31 17:23:20 -07004776 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartc00f62e2019-08-14 16:57:11 -07004777 pring_s4 = phba->sli4_hba.hdwq[iocb->hba_wqidx].io_wq->pring;
James Smart8931c732018-07-31 17:23:20 -07004778 if (!pring_s4) {
4779 ret = FAILED;
James Smartc2017262019-01-28 11:14:37 -08004780 goto out_unlock_buf;
James Smart8931c732018-07-31 17:23:20 -07004781 }
4782 spin_lock(&pring_s4->ring_lock);
4783 }
James Smart4f2e66c2012-05-09 21:17:07 -04004784 /* the command is in process of being cancelled */
4785 if (!(iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
James Smart4f2e66c2012-05-09 21:17:07 -04004786 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4787 "3169 SCSI Layer abort requested I/O has been "
4788 "cancelled by LLD.\n");
James Smartc2017262019-01-28 11:14:37 -08004789 ret = FAILED;
4790 goto out_unlock_ring;
James Smart4f2e66c2012-05-09 21:17:07 -04004791 }
dea31012005-04-17 16:05:31 -05004792 /*
James Smartc4908502019-01-28 11:14:28 -08004793 * If pCmd field of the corresponding lpfc_io_buf structure
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004794 * points to a different SCSI command, then the driver has
4795 * already completed this command, but the midlayer did not
James Smart4f2e66c2012-05-09 21:17:07 -04004796 * see the completion before the eh fired. Just return SUCCESS.
dea31012005-04-17 16:05:31 -05004797 */
James Smart4f2e66c2012-05-09 21:17:07 -04004798 if (lpfc_cmd->pCmd != cmnd) {
4799 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4800 "3170 SCSI Layer abort requested I/O has been "
4801 "completed by LLD.\n");
James Smartc2017262019-01-28 11:14:37 -08004802 goto out_unlock_ring;
James Smart4f2e66c2012-05-09 21:17:07 -04004803 }
dea31012005-04-17 16:05:31 -05004804
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004805 BUG_ON(iocb->context1 != lpfc_cmd);
dea31012005-04-17 16:05:31 -05004806
James Smartee620212014-04-04 13:51:53 -04004807 /* abort issued in recovery is still in progress */
4808 if (iocb->iocb_flag & LPFC_DRIVER_ABORTED) {
4809 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4810 "3389 SCSI Layer I/O Abort Request is pending\n");
James Smart8931c732018-07-31 17:23:20 -07004811 if (phba->sli_rev == LPFC_SLI_REV4)
4812 spin_unlock(&pring_s4->ring_lock);
James Smartc2017262019-01-28 11:14:37 -08004813 spin_unlock(&lpfc_cmd->buf_lock);
James Smartee620212014-04-04 13:51:53 -04004814 spin_unlock_irqrestore(&phba->hbalock, flags);
4815 goto wait_for_cmpl;
4816 }
4817
James Smart4f2e66c2012-05-09 21:17:07 -04004818 abtsiocb = __lpfc_sli_get_iocbq(phba);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004819 if (abtsiocb == NULL) {
4820 ret = FAILED;
James Smartc2017262019-01-28 11:14:37 -08004821 goto out_unlock_ring;
dea31012005-04-17 16:05:31 -05004822 }
4823
James Smartafbd8d82013-09-06 12:22:13 -04004824 /* Indicate the IO is being aborted by the driver. */
4825 iocb->iocb_flag |= LPFC_DRIVER_ABORTED;
4826
dea31012005-04-17 16:05:31 -05004827 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004828 * The scsi command can not be in txq and it is in flight because the
4829 * pCmd is still pointig at the SCSI command we have to abort. There
4830 * is no need to search the txcmplq. Just send an abort to the FW.
dea31012005-04-17 16:05:31 -05004831 */
dea31012005-04-17 16:05:31 -05004832
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004833 cmd = &iocb->iocb;
4834 icmd = &abtsiocb->iocb;
4835 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
4836 icmd->un.acxri.abortContextTag = cmd->ulpContext;
James Smart3772a992009-05-22 14:50:54 -04004837 if (phba->sli_rev == LPFC_SLI_REV4)
4838 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
4839 else
4840 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05004841
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004842 icmd->ulpLe = 1;
4843 icmd->ulpClass = cmd->ulpClass;
James Smart5ffc2662009-11-18 15:39:44 -05004844
4845 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -08004846 abtsiocb->hba_wqidx = iocb->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -05004847 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -04004848 if (iocb->iocb_flag & LPFC_IO_FOF)
4849 abtsiocb->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -05004850
James Smart2e0fef82007-06-17 19:56:36 -05004851 if (lpfc_is_link_up(phba))
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004852 icmd->ulpCommand = CMD_ABORT_XRI_CN;
4853 else
4854 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea31012005-04-17 16:05:31 -05004855
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004856 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smart2e0fef82007-06-17 19:56:36 -05004857 abtsiocb->vport = vport;
James Smart8931c732018-07-31 17:23:20 -07004858 lpfc_cmd->waitq = &waitq;
James Smart98912dda2014-04-04 13:52:31 -04004859 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart98912dda2014-04-04 13:52:31 -04004860 /* Note: both hbalock and ring_lock must be set here */
James Smart98912dda2014-04-04 13:52:31 -04004861 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
4862 abtsiocb, 0);
James Smart59c68ea2018-04-09 14:24:25 -07004863 spin_unlock(&pring_s4->ring_lock);
James Smart98912dda2014-04-04 13:52:31 -04004864 } else {
4865 ret_val = __lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
4866 abtsiocb, 0);
4867 }
James Smart4f2e66c2012-05-09 21:17:07 -04004868
James Smart98912dda2014-04-04 13:52:31 -04004869 if (ret_val == IOCB_ERROR) {
James Smart8931c732018-07-31 17:23:20 -07004870 /* Indicate the IO is not being aborted by the driver. */
4871 iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
4872 lpfc_cmd->waitq = NULL;
James Smartc2017262019-01-28 11:14:37 -08004873 spin_unlock(&lpfc_cmd->buf_lock);
James Smart91a52b62019-10-18 14:18:22 -07004874 spin_unlock_irqrestore(&phba->hbalock, flags);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004875 lpfc_sli_release_iocbq(phba, abtsiocb);
4876 ret = FAILED;
4877 goto out;
4878 }
4879
James Smart91a52b62019-10-18 14:18:22 -07004880 /* no longer need the lock after this point */
James Smartc2017262019-01-28 11:14:37 -08004881 spin_unlock(&lpfc_cmd->buf_lock);
James Smart91a52b62019-10-18 14:18:22 -07004882 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartc2017262019-01-28 11:14:37 -08004883
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004884 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
James Smart45ed1192009-10-02 15:17:02 -04004885 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -08004886 &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004887
James Smartee620212014-04-04 13:51:53 -04004888wait_for_cmpl:
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004889 /* Wait for abort to complete */
James Smartfa61a542008-01-11 01:52:42 -05004890 wait_event_timeout(waitq,
4891 (lpfc_cmd->pCmd != cmnd),
James Smart256ec0d2013-04-17 20:14:58 -04004892 msecs_to_jiffies(2*vport->cfg_devloss_tmo*1000));
James Smartee620212014-04-04 13:51:53 -04004893
James Smartc2017262019-01-28 11:14:37 -08004894 spin_lock(&lpfc_cmd->buf_lock);
dea31012005-04-17 16:05:31 -05004895
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004896 if (lpfc_cmd->pCmd == cmnd) {
4897 ret = FAILED;
Dick Kennedy372c1872020-06-30 14:50:00 -07004898 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04004899 "0748 abort handler timed out waiting "
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -07004900 "for aborting I/O (xri:x%x) to complete: "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004901 "ret %#x, ID %d, LUN %llu\n",
James Smart247ca942012-08-14 14:26:13 -04004902 iocb->sli4_xritag, ret,
4903 cmnd->device->id, cmnd->device->lun);
dea31012005-04-17 16:05:31 -05004904 }
James Smartb9e5a2d2019-05-21 17:49:01 -07004905
4906 lpfc_cmd->waitq = NULL;
4907
James Smartc2017262019-01-28 11:14:37 -08004908 spin_unlock(&lpfc_cmd->buf_lock);
James Smart4f2e66c2012-05-09 21:17:07 -04004909 goto out;
dea31012005-04-17 16:05:31 -05004910
James Smartc2017262019-01-28 11:14:37 -08004911out_unlock_ring:
4912 if (phba->sli_rev == LPFC_SLI_REV4)
4913 spin_unlock(&pring_s4->ring_lock);
4914out_unlock_buf:
4915 spin_unlock(&lpfc_cmd->buf_lock);
James Smart4f2e66c2012-05-09 21:17:07 -04004916out_unlock:
James Smart876dd7d2012-09-29 11:31:28 -04004917 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004918out:
James Smarte8b62012007-08-02 11:10:09 -04004919 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4920 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004921 "LUN %llu\n", ret, cmnd->device->id,
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04004922 cmnd->device->lun);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004923 return ret;
dea31012005-04-17 16:05:31 -05004924}
4925
James Smartbbb9d182009-06-10 17:23:16 -04004926static char *
4927lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
4928{
4929 switch (task_mgmt_cmd) {
4930 case FCP_ABORT_TASK_SET:
4931 return "ABORT_TASK_SET";
4932 case FCP_CLEAR_TASK_SET:
4933 return "FCP_CLEAR_TASK_SET";
4934 case FCP_BUS_RESET:
4935 return "FCP_BUS_RESET";
4936 case FCP_LUN_RESET:
4937 return "FCP_LUN_RESET";
4938 case FCP_TARGET_RESET:
4939 return "FCP_TARGET_RESET";
4940 case FCP_CLEAR_ACA:
4941 return "FCP_CLEAR_ACA";
4942 case FCP_TERMINATE_TASK:
4943 return "FCP_TERMINATE_TASK";
4944 default:
4945 return "unknown";
4946 }
4947}
4948
James Smart53151bb2013-10-10 12:24:07 -04004949
4950/**
4951 * lpfc_check_fcp_rsp - check the returned fcp_rsp to see if task failed
4952 * @vport: The virtual port for which this call is being executed.
James Smartc4908502019-01-28 11:14:28 -08004953 * @lpfc_cmd: Pointer to lpfc_io_buf data structure.
James Smart53151bb2013-10-10 12:24:07 -04004954 *
4955 * This routine checks the FCP RSP INFO to see if the tsk mgmt command succeded
4956 *
4957 * Return code :
4958 * 0x2003 - Error
4959 * 0x2002 - Success
4960 **/
4961static int
James Smartc4908502019-01-28 11:14:28 -08004962lpfc_check_fcp_rsp(struct lpfc_vport *vport, struct lpfc_io_buf *lpfc_cmd)
James Smart53151bb2013-10-10 12:24:07 -04004963{
4964 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
4965 uint32_t rsp_info;
4966 uint32_t rsp_len;
4967 uint8_t rsp_info_code;
4968 int ret = FAILED;
4969
4970
4971 if (fcprsp == NULL)
4972 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4973 "0703 fcp_rsp is missing\n");
4974 else {
4975 rsp_info = fcprsp->rspStatus2;
4976 rsp_len = be32_to_cpu(fcprsp->rspRspLen);
4977 rsp_info_code = fcprsp->rspInfo3;
4978
4979
4980 lpfc_printf_vlog(vport, KERN_INFO,
4981 LOG_FCP,
4982 "0706 fcp_rsp valid 0x%x,"
4983 " rsp len=%d code 0x%x\n",
4984 rsp_info,
4985 rsp_len, rsp_info_code);
4986
James Smart996a02a2019-05-21 17:49:07 -07004987 /* If FCP_RSP_LEN_VALID bit is one, then the FCP_RSP_LEN
4988 * field specifies the number of valid bytes of FCP_RSP_INFO.
4989 * The FCP_RSP_LEN field shall be set to 0x04 or 0x08
4990 */
4991 if ((fcprsp->rspStatus2 & RSP_LEN_VALID) &&
4992 ((rsp_len == 8) || (rsp_len == 4))) {
James Smart53151bb2013-10-10 12:24:07 -04004993 switch (rsp_info_code) {
4994 case RSP_NO_FAILURE:
4995 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4996 "0715 Task Mgmt No Failure\n");
4997 ret = SUCCESS;
4998 break;
4999 case RSP_TM_NOT_SUPPORTED: /* TM rejected */
5000 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5001 "0716 Task Mgmt Target "
5002 "reject\n");
5003 break;
5004 case RSP_TM_NOT_COMPLETED: /* TM failed */
5005 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5006 "0717 Task Mgmt Target "
5007 "failed TM\n");
5008 break;
5009 case RSP_TM_INVALID_LU: /* TM to invalid LU! */
5010 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5011 "0718 Task Mgmt to invalid "
5012 "LUN\n");
5013 break;
5014 }
5015 }
5016 }
5017 return ret;
5018}
5019
5020
James Smartbbb9d182009-06-10 17:23:16 -04005021/**
5022 * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
5023 * @vport: The virtual port for which this call is being executed.
Lee Joneseceee002020-11-02 14:23:43 +00005024 * @cmnd: Pointer to scsi_cmnd data structure.
James Smartbbb9d182009-06-10 17:23:16 -04005025 * @tgt_id: Target ID of remote device.
5026 * @lun_id: Lun number for the TMF
5027 * @task_mgmt_cmd: type of TMF to send
5028 *
5029 * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
5030 * a remote port.
5031 *
5032 * Return Code:
5033 * 0x2003 - Error
5034 * 0x2002 - Success.
5035 **/
5036static int
James Smarteed695d2016-10-13 15:06:04 -07005037lpfc_send_taskmgmt(struct lpfc_vport *vport, struct scsi_cmnd *cmnd,
5038 unsigned int tgt_id, uint64_t lun_id,
5039 uint8_t task_mgmt_cmd)
James Smartbbb9d182009-06-10 17:23:16 -04005040{
5041 struct lpfc_hba *phba = vport->phba;
James Smartc4908502019-01-28 11:14:28 -08005042 struct lpfc_io_buf *lpfc_cmd;
James Smartbbb9d182009-06-10 17:23:16 -04005043 struct lpfc_iocbq *iocbq;
5044 struct lpfc_iocbq *iocbqrsp;
James Smarteed695d2016-10-13 15:06:04 -07005045 struct lpfc_rport_data *rdata;
5046 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005047 int ret;
5048 int status;
5049
James Smarteed695d2016-10-13 15:06:04 -07005050 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smart307e3382020-11-15 11:26:30 -08005051 if (!rdata || !rdata->pnode)
James Smartbbb9d182009-06-10 17:23:16 -04005052 return FAILED;
James Smarteed695d2016-10-13 15:06:04 -07005053 pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005054
James Smartace44e42019-01-28 11:14:27 -08005055 lpfc_cmd = lpfc_get_scsi_buf(phba, pnode, NULL);
James Smartbbb9d182009-06-10 17:23:16 -04005056 if (lpfc_cmd == NULL)
5057 return FAILED;
James Smart0c411222013-09-06 12:22:46 -04005058 lpfc_cmd->timeout = phba->cfg_task_mgmt_tmo;
James Smartbbb9d182009-06-10 17:23:16 -04005059 lpfc_cmd->rdata = rdata;
James Smarteed695d2016-10-13 15:06:04 -07005060 lpfc_cmd->pCmd = cmnd;
James Smart2a5b7d62018-07-31 17:23:22 -07005061 lpfc_cmd->ndlp = pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005062
5063 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
5064 task_mgmt_cmd);
5065 if (!status) {
5066 lpfc_release_scsi_buf(phba, lpfc_cmd);
5067 return FAILED;
5068 }
5069
5070 iocbq = &lpfc_cmd->cur_iocbq;
5071 iocbqrsp = lpfc_sli_get_iocbq(phba);
5072 if (iocbqrsp == NULL) {
5073 lpfc_release_scsi_buf(phba, lpfc_cmd);
5074 return FAILED;
5075 }
James Smart5a0916b2013-07-15 18:31:42 -04005076 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
James Smartbbb9d182009-06-10 17:23:16 -04005077
5078 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005079 "0702 Issue %s to TGT %d LUN %llu "
James Smart6d368e52011-05-24 11:44:12 -04005080 "rpi x%x nlp_flag x%x Data: x%x x%x\n",
James Smartbbb9d182009-06-10 17:23:16 -04005081 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
James Smart6d368e52011-05-24 11:44:12 -04005082 pnode->nlp_rpi, pnode->nlp_flag, iocbq->sli4_xritag,
5083 iocbq->iocb_flag);
James Smartbbb9d182009-06-10 17:23:16 -04005084
5085 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
5086 iocbq, iocbqrsp, lpfc_cmd->timeout);
James Smart53151bb2013-10-10 12:24:07 -04005087 if ((status != IOCB_SUCCESS) ||
5088 (iocbqrsp->iocb.ulpStatus != IOSTAT_SUCCESS)) {
James Smartae374a32015-05-22 10:42:41 -04005089 if (status != IOCB_SUCCESS ||
5090 iocbqrsp->iocb.ulpStatus != IOSTAT_FCP_RSP_ERROR)
Dick Kennedy372c1872020-06-30 14:50:00 -07005091 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
James Smartae374a32015-05-22 10:42:41 -04005092 "0727 TMF %s to TGT %d LUN %llu "
5093 "failed (%d, %d) iocb_flag x%x\n",
5094 lpfc_taskmgmt_name(task_mgmt_cmd),
5095 tgt_id, lun_id,
5096 iocbqrsp->iocb.ulpStatus,
5097 iocbqrsp->iocb.un.ulpWord[4],
5098 iocbq->iocb_flag);
James Smart53151bb2013-10-10 12:24:07 -04005099 /* if ulpStatus != IOCB_SUCCESS, then status == IOCB_SUCCESS */
5100 if (status == IOCB_SUCCESS) {
5101 if (iocbqrsp->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
5102 /* Something in the FCP_RSP was invalid.
5103 * Check conditions */
5104 ret = lpfc_check_fcp_rsp(vport, lpfc_cmd);
5105 else
5106 ret = FAILED;
5107 } else if (status == IOCB_TIMEDOUT) {
5108 ret = TIMEOUT_ERROR;
5109 } else {
5110 ret = FAILED;
5111 }
James Smart53151bb2013-10-10 12:24:07 -04005112 } else
James Smartbbb9d182009-06-10 17:23:16 -04005113 ret = SUCCESS;
5114
5115 lpfc_sli_release_iocbq(phba, iocbqrsp);
5116
5117 if (ret != TIMEOUT_ERROR)
5118 lpfc_release_scsi_buf(phba, lpfc_cmd);
5119
5120 return ret;
5121}
5122
5123/**
5124 * lpfc_chk_tgt_mapped -
5125 * @vport: The virtual port to check on
5126 * @cmnd: Pointer to scsi_cmnd data structure.
5127 *
5128 * This routine delays until the scsi target (aka rport) for the
5129 * command exists (is present and logged in) or we declare it non-existent.
5130 *
5131 * Return code :
5132 * 0x2003 - Error
5133 * 0x2002 - Success
5134 **/
5135static int
5136lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
5137{
James Smart1ba981f2014-02-20 09:56:45 -05005138 struct lpfc_rport_data *rdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005139 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005140 unsigned long later;
5141
James Smart1ba981f2014-02-20 09:56:45 -05005142 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smart1c6f4ef52009-11-18 15:40:49 -05005143 if (!rdata) {
5144 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smart32350662019-08-14 16:57:06 -07005145 "0797 Tgt Map rport failure: rdata x%px\n", rdata);
James Smart1c6f4ef52009-11-18 15:40:49 -05005146 return FAILED;
5147 }
5148 pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005149 /*
5150 * If target is not in a MAPPED state, delay until
5151 * target is rediscovered or devloss timeout expires.
5152 */
5153 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
5154 while (time_after(later, jiffies)) {
James Smart307e3382020-11-15 11:26:30 -08005155 if (!pnode)
James Smartbbb9d182009-06-10 17:23:16 -04005156 return FAILED;
5157 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
5158 return SUCCESS;
5159 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
James Smart1ba981f2014-02-20 09:56:45 -05005160 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smartbbb9d182009-06-10 17:23:16 -04005161 if (!rdata)
5162 return FAILED;
5163 pnode = rdata->pnode;
5164 }
James Smart307e3382020-11-15 11:26:30 -08005165 if (!pnode || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
James Smartbbb9d182009-06-10 17:23:16 -04005166 return FAILED;
5167 return SUCCESS;
5168}
5169
5170/**
5171 * lpfc_reset_flush_io_context -
5172 * @vport: The virtual port (scsi_host) for the flush context
5173 * @tgt_id: If aborting by Target contect - specifies the target id
5174 * @lun_id: If aborting by Lun context - specifies the lun id
5175 * @context: specifies the context level to flush at.
5176 *
5177 * After a reset condition via TMF, we need to flush orphaned i/o
5178 * contexts from the adapter. This routine aborts any contexts
5179 * outstanding, then waits for their completions. The wait is
5180 * bounded by devloss_tmo though.
5181 *
5182 * Return code :
5183 * 0x2003 - Error
5184 * 0x2002 - Success
5185 **/
5186static int
5187lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
5188 uint64_t lun_id, lpfc_ctx_cmd context)
5189{
5190 struct lpfc_hba *phba = vport->phba;
5191 unsigned long later;
5192 int cnt;
5193
5194 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
5195 if (cnt)
James Smart98912dda2014-04-04 13:52:31 -04005196 lpfc_sli_abort_taskmgmt(vport,
James Smart895427b2017-02-12 13:52:30 -08005197 &phba->sli.sli3_ring[LPFC_FCP_RING],
James Smart98912dda2014-04-04 13:52:31 -04005198 tgt_id, lun_id, context);
James Smartbbb9d182009-06-10 17:23:16 -04005199 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
5200 while (time_after(later, jiffies) && cnt) {
5201 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
5202 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
5203 }
5204 if (cnt) {
Dick Kennedy372c1872020-06-30 14:50:00 -07005205 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
James Smartbbb9d182009-06-10 17:23:16 -04005206 "0724 I/O flush failure for context %s : cnt x%x\n",
5207 ((context == LPFC_CTX_LUN) ? "LUN" :
5208 ((context == LPFC_CTX_TGT) ? "TGT" :
5209 ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
5210 cnt);
5211 return FAILED;
5212 }
5213 return SUCCESS;
5214}
5215
James Smart9bad7672008-12-04 22:39:02 -05005216/**
James Smart3621a712009-04-06 18:47:14 -04005217 * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
James Smart9bad7672008-12-04 22:39:02 -05005218 * @cmnd: Pointer to scsi_cmnd data structure.
5219 *
James Smartbbb9d182009-06-10 17:23:16 -04005220 * This routine does a device reset by sending a LUN_RESET task management
James Smart9bad7672008-12-04 22:39:02 -05005221 * command.
5222 *
5223 * Return code :
5224 * 0x2003 - Error
James Smart3621a712009-04-06 18:47:14 -04005225 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05005226 **/
dea31012005-04-17 16:05:31 -05005227static int
James Smart7054a602007-04-25 09:52:34 -04005228lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05005229{
James Smart2e0fef82007-06-17 19:56:36 -05005230 struct Scsi_Host *shost = cmnd->device->host;
5231 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smart1ba981f2014-02-20 09:56:45 -05005232 struct lpfc_rport_data *rdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005233 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005234 unsigned tgt_id = cmnd->device->id;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005235 uint64_t lun_id = cmnd->device->lun;
James Smartea2151b2008-09-07 11:52:10 -04005236 struct lpfc_scsi_event_header scsi_event;
James Smart53151bb2013-10-10 12:24:07 -04005237 int status;
dea31012005-04-17 16:05:31 -05005238
James Smart1ba981f2014-02-20 09:56:45 -05005239 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smartad490b62015-04-07 15:07:26 -04005240 if (!rdata || !rdata->pnode) {
Dick Kennedy372c1872020-06-30 14:50:00 -07005241 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
James Smart32350662019-08-14 16:57:06 -07005242 "0798 Device Reset rdata failure: rdata x%px\n",
James Smartad490b62015-04-07 15:07:26 -04005243 rdata);
James Smart1c6f4ef52009-11-18 15:40:49 -05005244 return FAILED;
5245 }
5246 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04005247 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005248 if (status != 0 && status != SUCCESS)
James Smart589a52d2010-07-14 15:30:54 -04005249 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005250
5251 status = lpfc_chk_tgt_mapped(vport, cmnd);
5252 if (status == FAILED) {
Dick Kennedy372c1872020-06-30 14:50:00 -07005253 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
James Smart32350662019-08-14 16:57:06 -07005254 "0721 Device Reset rport failure: rdata x%px\n", rdata);
James Smartbbb9d182009-06-10 17:23:16 -04005255 return FAILED;
5256 }
5257
5258 scsi_event.event_type = FC_REG_SCSI_EVENT;
5259 scsi_event.subcategory = LPFC_EVENT_LUNRESET;
5260 scsi_event.lun = lun_id;
5261 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5262 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5263
5264 fc_host_post_vendor_event(shost, fc_get_event_number(),
5265 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
5266
James Smarteed695d2016-10-13 15:06:04 -07005267 status = lpfc_send_taskmgmt(vport, cmnd, tgt_id, lun_id,
James Smartbbb9d182009-06-10 17:23:16 -04005268 FCP_LUN_RESET);
5269
Dick Kennedy372c1872020-06-30 14:50:00 -07005270 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005271 "0713 SCSI layer issued Device Reset (%d, %llu) "
James Smartbbb9d182009-06-10 17:23:16 -04005272 "return x%x\n", tgt_id, lun_id, status);
5273
dea31012005-04-17 16:05:31 -05005274 /*
James Smartbbb9d182009-06-10 17:23:16 -04005275 * We have to clean up i/o as : they may be orphaned by the TMF;
5276 * or if the TMF failed, they may be in an indeterminate state.
5277 * So, continue on.
5278 * We will report success if all the i/o aborts successfully.
dea31012005-04-17 16:05:31 -05005279 */
James Smart53151bb2013-10-10 12:24:07 -04005280 if (status == SUCCESS)
5281 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
James Smartbbb9d182009-06-10 17:23:16 -04005282 LPFC_CTX_LUN);
James Smart53151bb2013-10-10 12:24:07 -04005283
5284 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005285}
5286
5287/**
5288 * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
5289 * @cmnd: Pointer to scsi_cmnd data structure.
5290 *
5291 * This routine does a target reset by sending a TARGET_RESET task management
5292 * command.
5293 *
5294 * Return code :
5295 * 0x2003 - Error
5296 * 0x2002 - Success
5297 **/
5298static int
5299lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
5300{
5301 struct Scsi_Host *shost = cmnd->device->host;
5302 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smart1ba981f2014-02-20 09:56:45 -05005303 struct lpfc_rport_data *rdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005304 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005305 unsigned tgt_id = cmnd->device->id;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005306 uint64_t lun_id = cmnd->device->lun;
James Smartbbb9d182009-06-10 17:23:16 -04005307 struct lpfc_scsi_event_header scsi_event;
James Smart53151bb2013-10-10 12:24:07 -04005308 int status;
James Smartbbb9d182009-06-10 17:23:16 -04005309
James Smart1ba981f2014-02-20 09:56:45 -05005310 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smartbbd3d732019-03-12 16:30:17 -07005311 if (!rdata || !rdata->pnode) {
Dick Kennedy372c1872020-06-30 14:50:00 -07005312 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
James Smart32350662019-08-14 16:57:06 -07005313 "0799 Target Reset rdata failure: rdata x%px\n",
James Smartbbd3d732019-03-12 16:30:17 -07005314 rdata);
James Smart1c6f4ef52009-11-18 15:40:49 -05005315 return FAILED;
5316 }
5317 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04005318 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005319 if (status != 0 && status != SUCCESS)
James Smart589a52d2010-07-14 15:30:54 -04005320 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005321
5322 status = lpfc_chk_tgt_mapped(vport, cmnd);
5323 if (status == FAILED) {
Dick Kennedy372c1872020-06-30 14:50:00 -07005324 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
James Smart32350662019-08-14 16:57:06 -07005325 "0722 Target Reset rport failure: rdata x%px\n", rdata);
James Smart63e480f2015-04-07 15:07:20 -04005326 if (pnode) {
5327 spin_lock_irq(shost->host_lock);
5328 pnode->nlp_flag &= ~NLP_NPR_ADISC;
5329 pnode->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
5330 spin_unlock_irq(shost->host_lock);
5331 }
James Smart8c50d252014-09-03 12:58:16 -04005332 lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
5333 LPFC_CTX_TGT);
5334 return FAST_IO_FAIL;
dea31012005-04-17 16:05:31 -05005335 }
James Smartea2151b2008-09-07 11:52:10 -04005336
5337 scsi_event.event_type = FC_REG_SCSI_EVENT;
5338 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
5339 scsi_event.lun = 0;
5340 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5341 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5342
James Smartbbb9d182009-06-10 17:23:16 -04005343 fc_host_post_vendor_event(shost, fc_get_event_number(),
5344 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -04005345
James Smarteed695d2016-10-13 15:06:04 -07005346 status = lpfc_send_taskmgmt(vport, cmnd, tgt_id, lun_id,
James Smartbbb9d182009-06-10 17:23:16 -04005347 FCP_TARGET_RESET);
dea31012005-04-17 16:05:31 -05005348
Dick Kennedy372c1872020-06-30 14:50:00 -07005349 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005350 "0723 SCSI layer issued Target Reset (%d, %llu) "
James Smartbbb9d182009-06-10 17:23:16 -04005351 "return x%x\n", tgt_id, lun_id, status);
5352
5353 /*
5354 * We have to clean up i/o as : they may be orphaned by the TMF;
5355 * or if the TMF failed, they may be in an indeterminate state.
5356 * So, continue on.
5357 * We will report success if all the i/o aborts successfully.
5358 */
James Smart53151bb2013-10-10 12:24:07 -04005359 if (status == SUCCESS)
5360 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
James Smart3a707302012-06-12 13:54:42 -04005361 LPFC_CTX_TGT);
James Smart53151bb2013-10-10 12:24:07 -04005362 return status;
dea31012005-04-17 16:05:31 -05005363}
5364
James Smart9bad7672008-12-04 22:39:02 -05005365/**
James Smart3621a712009-04-06 18:47:14 -04005366 * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05005367 * @cmnd: Pointer to scsi_cmnd data structure.
5368 *
James Smartbbb9d182009-06-10 17:23:16 -04005369 * This routine does target reset to all targets on @cmnd->device->host.
5370 * This emulates Parallel SCSI Bus Reset Semantics.
James Smart9bad7672008-12-04 22:39:02 -05005371 *
James Smartbbb9d182009-06-10 17:23:16 -04005372 * Return code :
5373 * 0x2003 - Error
5374 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05005375 **/
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04005376static int
James Smart7054a602007-04-25 09:52:34 -04005377lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05005378{
James Smart2e0fef82007-06-17 19:56:36 -05005379 struct Scsi_Host *shost = cmnd->device->host;
5380 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05005381 struct lpfc_nodelist *ndlp = NULL;
James Smartea2151b2008-09-07 11:52:10 -04005382 struct lpfc_scsi_event_header scsi_event;
James Smartbbb9d182009-06-10 17:23:16 -04005383 int match;
5384 int ret = SUCCESS, status, i;
James Smartea2151b2008-09-07 11:52:10 -04005385
5386 scsi_event.event_type = FC_REG_SCSI_EVENT;
5387 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
5388 scsi_event.lun = 0;
5389 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
5390 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
5391
James Smartbbb9d182009-06-10 17:23:16 -04005392 fc_host_post_vendor_event(shost, fc_get_event_number(),
5393 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
dea31012005-04-17 16:05:31 -05005394
James Smartbf086112011-08-21 21:48:13 -04005395 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005396 if (status != 0 && status != SUCCESS)
James Smartbf086112011-08-21 21:48:13 -04005397 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005398
dea31012005-04-17 16:05:31 -05005399 /*
5400 * Since the driver manages a single bus device, reset all
5401 * targets known to the driver. Should any target reset
5402 * fail, this routine returns failure to the midlayer.
5403 */
James Smarte17da182006-07-06 15:49:25 -04005404 for (i = 0; i < LPFC_MAX_TARGET; i++) {
James Smart685f0bf2007-04-25 09:53:08 -04005405 /* Search for mapped node by target ID */
dea31012005-04-17 16:05:31 -05005406 match = 0;
James Smart2e0fef82007-06-17 19:56:36 -05005407 spin_lock_irq(shost->host_lock);
5408 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smart307e3382020-11-15 11:26:30 -08005409
James Smarta6571c62012-10-31 14:44:42 -04005410 if (vport->phba->cfg_fcp2_no_tgt_reset &&
5411 (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE))
5412 continue;
James Smart685f0bf2007-04-25 09:53:08 -04005413 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
James Smart915caaa2008-06-14 22:52:38 -04005414 ndlp->nlp_sid == i &&
James Smarta0f2d3e2017-02-12 13:52:31 -08005415 ndlp->rport &&
5416 ndlp->nlp_type & NLP_FCP_TARGET) {
dea31012005-04-17 16:05:31 -05005417 match = 1;
5418 break;
5419 }
5420 }
James Smart2e0fef82007-06-17 19:56:36 -05005421 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005422 if (!match)
5423 continue;
James Smartbbb9d182009-06-10 17:23:16 -04005424
James Smarteed695d2016-10-13 15:06:04 -07005425 status = lpfc_send_taskmgmt(vport, cmnd,
James Smartbbb9d182009-06-10 17:23:16 -04005426 i, 0, FCP_TARGET_RESET);
5427
5428 if (status != SUCCESS) {
Dick Kennedy372c1872020-06-30 14:50:00 -07005429 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04005430 "0700 Bus Reset on target %d failed\n",
5431 i);
James Smart915caaa2008-06-14 22:52:38 -04005432 ret = FAILED;
dea31012005-04-17 16:05:31 -05005433 }
5434 }
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005435 /*
James Smartbbb9d182009-06-10 17:23:16 -04005436 * We have to clean up i/o as : they may be orphaned by the TMFs
5437 * above; or if any of the TMFs failed, they may be in an
5438 * indeterminate state.
5439 * We will report success if all the i/o aborts successfully.
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005440 */
James Smartbbb9d182009-06-10 17:23:16 -04005441
5442 status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
5443 if (status != SUCCESS)
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005444 ret = FAILED;
James Smartbbb9d182009-06-10 17:23:16 -04005445
Dick Kennedy372c1872020-06-30 14:50:00 -07005446 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04005447 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea31012005-04-17 16:05:31 -05005448 return ret;
5449}
5450
James Smart9bad7672008-12-04 22:39:02 -05005451/**
James Smart27b01b82012-05-09 21:19:44 -04005452 * lpfc_host_reset_handler - scsi_host_template eh_host_reset_handler entry pt
5453 * @cmnd: Pointer to scsi_cmnd data structure.
5454 *
5455 * This routine does host reset to the adaptor port. It brings the HBA
5456 * offline, performs a board restart, and then brings the board back online.
5457 * The lpfc_offline calls lpfc_sli_hba_down which will abort and local
5458 * reject all outstanding SCSI commands to the host and error returned
5459 * back to SCSI mid-level. As this will be SCSI mid-level's last resort
5460 * of error handling, it will only return error if resetting of the adapter
5461 * is not successful; in all other cases, will return success.
5462 *
5463 * Return code :
5464 * 0x2003 - Error
5465 * 0x2002 - Success
5466 **/
5467static int
5468lpfc_host_reset_handler(struct scsi_cmnd *cmnd)
5469{
5470 struct Scsi_Host *shost = cmnd->device->host;
5471 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5472 struct lpfc_hba *phba = vport->phba;
5473 int rc, ret = SUCCESS;
5474
Dick Kennedy372c1872020-06-30 14:50:00 -07005475 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
James Smarta88dbb62013-04-17 20:18:39 -04005476 "3172 SCSI layer issued Host Reset Data:\n");
5477
James Smart618a5232012-06-12 13:54:36 -04005478 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart27b01b82012-05-09 21:19:44 -04005479 lpfc_offline(phba);
5480 rc = lpfc_sli_brdrestart(phba);
5481 if (rc)
James Smart8c24a4f2019-08-14 16:56:53 -07005482 goto error;
5483
James Smarta88dbb62013-04-17 20:18:39 -04005484 rc = lpfc_online(phba);
5485 if (rc)
James Smart8c24a4f2019-08-14 16:56:53 -07005486 goto error;
5487
James Smart27b01b82012-05-09 21:19:44 -04005488 lpfc_unblock_mgmt_io(phba);
5489
James Smart27b01b82012-05-09 21:19:44 -04005490 return ret;
James Smart8c24a4f2019-08-14 16:56:53 -07005491error:
Dick Kennedy372c1872020-06-30 14:50:00 -07005492 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
James Smart8c24a4f2019-08-14 16:56:53 -07005493 "3323 Failed host reset\n");
5494 lpfc_unblock_mgmt_io(phba);
5495 return FAILED;
James Smart27b01b82012-05-09 21:19:44 -04005496}
5497
5498/**
James Smart3621a712009-04-06 18:47:14 -04005499 * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
James Smart9bad7672008-12-04 22:39:02 -05005500 * @sdev: Pointer to scsi_device.
5501 *
5502 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
5503 * globally available list of scsi buffers. This routine also makes sure scsi
5504 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
5505 * of scsi buffer exists for the lifetime of the driver.
5506 *
5507 * Return codes:
5508 * non-0 - Error
5509 * 0 - Success
5510 **/
dea31012005-04-17 16:05:31 -05005511static int
dea31012005-04-17 16:05:31 -05005512lpfc_slave_alloc(struct scsi_device *sdev)
5513{
James Smart2e0fef82007-06-17 19:56:36 -05005514 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5515 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005516 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
James Smart3772a992009-05-22 14:50:54 -04005517 uint32_t total = 0;
dea31012005-04-17 16:05:31 -05005518 uint32_t num_to_alloc = 0;
James Smart3772a992009-05-22 14:50:54 -04005519 int num_allocated = 0;
James Smartd7c47992010-06-08 18:31:54 -04005520 uint32_t sdev_cnt;
James Smart1ba981f2014-02-20 09:56:45 -05005521 struct lpfc_device_data *device_data;
5522 unsigned long flags;
5523 struct lpfc_name target_wwpn;
dea31012005-04-17 16:05:31 -05005524
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005525 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05005526 return -ENXIO;
5527
James Smartf38fa0b2014-04-04 13:52:21 -04005528 if (phba->cfg_fof) {
James Smart1ba981f2014-02-20 09:56:45 -05005529
5530 /*
5531 * Check to see if the device data structure for the lun
5532 * exists. If not, create one.
5533 */
5534
5535 u64_to_wwn(rport->port_name, target_wwpn.u.wwn);
5536 spin_lock_irqsave(&phba->devicelock, flags);
5537 device_data = __lpfc_get_device_data(phba,
5538 &phba->luns,
5539 &vport->fc_portname,
5540 &target_wwpn,
5541 sdev->lun);
5542 if (!device_data) {
5543 spin_unlock_irqrestore(&phba->devicelock, flags);
5544 device_data = lpfc_create_device_data(phba,
5545 &vport->fc_portname,
5546 &target_wwpn,
James Smartb5749fe2016-12-19 15:07:26 -08005547 sdev->lun,
5548 phba->cfg_XLanePriority,
5549 true);
James Smart1ba981f2014-02-20 09:56:45 -05005550 if (!device_data)
5551 return -ENOMEM;
5552 spin_lock_irqsave(&phba->devicelock, flags);
5553 list_add_tail(&device_data->listentry, &phba->luns);
5554 }
5555 device_data->rport_data = rport->dd_data;
5556 device_data->available = true;
5557 spin_unlock_irqrestore(&phba->devicelock, flags);
5558 sdev->hostdata = device_data;
5559 } else {
5560 sdev->hostdata = rport->dd_data;
5561 }
James Smartd7c47992010-06-08 18:31:54 -04005562 sdev_cnt = atomic_inc_return(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05005563
James Smart0794d602019-01-28 11:14:19 -08005564 /* For SLI4, all IO buffers are pre-allocated */
5565 if (phba->sli_rev == LPFC_SLI_REV4)
5566 return 0;
5567
5568 /* This code path is now ONLY for SLI3 adapters */
5569
dea31012005-04-17 16:05:31 -05005570 /*
5571 * Populate the cmds_per_lun count scsi_bufs into this host's globally
5572 * available list of scsi buffers. Don't allocate more than the
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04005573 * HBA limit conveyed to the midlayer via the host structure. The
5574 * formula accounts for the lun_queue_depth + error handlers + 1
5575 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea31012005-04-17 16:05:31 -05005576 */
5577 total = phba->total_scsi_bufs;
James Smart3de2a652007-08-02 11:09:59 -04005578 num_to_alloc = vport->cfg_lun_queue_depth + 2;
James Smart92d7f7b2007-06-17 19:56:38 -05005579
James Smartd7c47992010-06-08 18:31:54 -04005580 /* If allocated buffers are enough do nothing */
5581 if ((sdev_cnt * (vport->cfg_lun_queue_depth + 2)) < total)
5582 return 0;
5583
James Smart92d7f7b2007-06-17 19:56:38 -05005584 /* Allow some exchanges to be available always to complete discovery */
5585 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04005586 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5587 "0704 At limitation of %d preallocated "
5588 "command buffers\n", total);
dea31012005-04-17 16:05:31 -05005589 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05005590 /* Allow some exchanges to be available always to complete discovery */
5591 } else if (total + num_to_alloc >
5592 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04005593 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5594 "0705 Allocation request of %d "
5595 "command buffers will exceed max of %d. "
5596 "Reducing allocation request to %d.\n",
5597 num_to_alloc, phba->cfg_hba_queue_depth,
5598 (phba->cfg_hba_queue_depth - total));
dea31012005-04-17 16:05:31 -05005599 num_to_alloc = phba->cfg_hba_queue_depth - total;
5600 }
James Smart0794d602019-01-28 11:14:19 -08005601 num_allocated = lpfc_new_scsi_buf_s3(vport, num_to_alloc);
James Smart3772a992009-05-22 14:50:54 -04005602 if (num_to_alloc != num_allocated) {
Dick Kennedy372c1872020-06-30 14:50:00 -07005603 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
James Smart96f70772013-04-17 20:16:15 -04005604 "0708 Allocation request of %d "
5605 "command buffers did not succeed. "
5606 "Allocated %d buffers.\n",
5607 num_to_alloc, num_allocated);
dea31012005-04-17 16:05:31 -05005608 }
James Smart1c6f4ef52009-11-18 15:40:49 -05005609 if (num_allocated > 0)
5610 phba->total_scsi_bufs += num_allocated;
dea31012005-04-17 16:05:31 -05005611 return 0;
5612}
5613
James Smart9bad7672008-12-04 22:39:02 -05005614/**
James Smart3621a712009-04-06 18:47:14 -04005615 * lpfc_slave_configure - scsi_host_template slave_configure entry point
James Smart9bad7672008-12-04 22:39:02 -05005616 * @sdev: Pointer to scsi_device.
5617 *
5618 * This routine configures following items
5619 * - Tag command queuing support for @sdev if supported.
James Smart9bad7672008-12-04 22:39:02 -05005620 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
5621 *
5622 * Return codes:
5623 * 0 - Success
5624 **/
dea31012005-04-17 16:05:31 -05005625static int
5626lpfc_slave_configure(struct scsi_device *sdev)
5627{
James Smart2e0fef82007-06-17 19:56:36 -05005628 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5629 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05005630
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01005631 scsi_change_queue_depth(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05005632
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005633 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04005634 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -08005635 &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005636 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
5637 lpfc_poll_rearm_timer(phba);
5638 }
5639
dea31012005-04-17 16:05:31 -05005640 return 0;
5641}
5642
James Smart9bad7672008-12-04 22:39:02 -05005643/**
James Smart3621a712009-04-06 18:47:14 -04005644 * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
James Smart9bad7672008-12-04 22:39:02 -05005645 * @sdev: Pointer to scsi_device.
5646 *
5647 * This routine sets @sdev hostatdata filed to null.
5648 **/
dea31012005-04-17 16:05:31 -05005649static void
5650lpfc_slave_destroy(struct scsi_device *sdev)
5651{
James Smartd7c47992010-06-08 18:31:54 -04005652 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5653 struct lpfc_hba *phba = vport->phba;
James Smart1ba981f2014-02-20 09:56:45 -05005654 unsigned long flags;
5655 struct lpfc_device_data *device_data = sdev->hostdata;
5656
James Smartd7c47992010-06-08 18:31:54 -04005657 atomic_dec(&phba->sdev_cnt);
James Smartf38fa0b2014-04-04 13:52:21 -04005658 if ((phba->cfg_fof) && (device_data)) {
James Smart1ba981f2014-02-20 09:56:45 -05005659 spin_lock_irqsave(&phba->devicelock, flags);
5660 device_data->available = false;
5661 if (!device_data->oas_enabled)
5662 lpfc_delete_device_data(phba, device_data);
5663 spin_unlock_irqrestore(&phba->devicelock, flags);
5664 }
dea31012005-04-17 16:05:31 -05005665 sdev->hostdata = NULL;
5666 return;
5667}
5668
James Smart1ba981f2014-02-20 09:56:45 -05005669/**
5670 * lpfc_create_device_data - creates and initializes device data structure for OAS
Lee Joneseceee002020-11-02 14:23:43 +00005671 * @phba: Pointer to host bus adapter structure.
James Smart1ba981f2014-02-20 09:56:45 -05005672 * @vport_wwpn: Pointer to vport's wwpn information
5673 * @target_wwpn: Pointer to target's wwpn information
5674 * @lun: Lun on target
Lee Joneseceee002020-11-02 14:23:43 +00005675 * @pri: Priority
James Smart1ba981f2014-02-20 09:56:45 -05005676 * @atomic_create: Flag to indicate if memory should be allocated using the
5677 * GFP_ATOMIC flag or not.
5678 *
5679 * This routine creates a device data structure which will contain identifying
5680 * information for the device (host wwpn, target wwpn, lun), state of OAS,
5681 * whether or not the corresponding lun is available by the system,
5682 * and pointer to the rport data.
5683 *
5684 * Return codes:
5685 * NULL - Error
5686 * Pointer to lpfc_device_data - Success
5687 **/
5688struct lpfc_device_data*
5689lpfc_create_device_data(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
5690 struct lpfc_name *target_wwpn, uint64_t lun,
James Smartb5749fe2016-12-19 15:07:26 -08005691 uint32_t pri, bool atomic_create)
James Smart1ba981f2014-02-20 09:56:45 -05005692{
5693
5694 struct lpfc_device_data *lun_info;
5695 int memory_flags;
5696
5697 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
James Smartf38fa0b2014-04-04 13:52:21 -04005698 !(phba->cfg_fof))
James Smart1ba981f2014-02-20 09:56:45 -05005699 return NULL;
5700
5701 /* Attempt to create the device data to contain lun info */
5702
5703 if (atomic_create)
5704 memory_flags = GFP_ATOMIC;
5705 else
5706 memory_flags = GFP_KERNEL;
5707 lun_info = mempool_alloc(phba->device_data_mem_pool, memory_flags);
5708 if (!lun_info)
5709 return NULL;
5710 INIT_LIST_HEAD(&lun_info->listentry);
5711 lun_info->rport_data = NULL;
5712 memcpy(&lun_info->device_id.vport_wwpn, vport_wwpn,
5713 sizeof(struct lpfc_name));
5714 memcpy(&lun_info->device_id.target_wwpn, target_wwpn,
5715 sizeof(struct lpfc_name));
5716 lun_info->device_id.lun = lun;
5717 lun_info->oas_enabled = false;
James Smartb5749fe2016-12-19 15:07:26 -08005718 lun_info->priority = pri;
James Smart1ba981f2014-02-20 09:56:45 -05005719 lun_info->available = false;
5720 return lun_info;
5721}
5722
5723/**
5724 * lpfc_delete_device_data - frees a device data structure for OAS
Lee Joneseceee002020-11-02 14:23:43 +00005725 * @phba: Pointer to host bus adapter structure.
James Smart1ba981f2014-02-20 09:56:45 -05005726 * @lun_info: Pointer to device data structure to free.
5727 *
5728 * This routine frees the previously allocated device data structure passed.
5729 *
5730 **/
5731void
5732lpfc_delete_device_data(struct lpfc_hba *phba,
5733 struct lpfc_device_data *lun_info)
5734{
5735
5736 if (unlikely(!phba) || !lun_info ||
James Smartf38fa0b2014-04-04 13:52:21 -04005737 !(phba->cfg_fof))
James Smart1ba981f2014-02-20 09:56:45 -05005738 return;
5739
5740 if (!list_empty(&lun_info->listentry))
5741 list_del(&lun_info->listentry);
5742 mempool_free(lun_info, phba->device_data_mem_pool);
5743 return;
5744}
5745
5746/**
5747 * __lpfc_get_device_data - returns the device data for the specified lun
Lee Joneseceee002020-11-02 14:23:43 +00005748 * @phba: Pointer to host bus adapter structure.
James Smart1ba981f2014-02-20 09:56:45 -05005749 * @list: Point to list to search.
5750 * @vport_wwpn: Pointer to vport's wwpn information
5751 * @target_wwpn: Pointer to target's wwpn information
5752 * @lun: Lun on target
5753 *
5754 * This routine searches the list passed for the specified lun's device data.
5755 * This function does not hold locks, it is the responsibility of the caller
5756 * to ensure the proper lock is held before calling the function.
5757 *
5758 * Return codes:
5759 * NULL - Error
5760 * Pointer to lpfc_device_data - Success
5761 **/
5762struct lpfc_device_data*
5763__lpfc_get_device_data(struct lpfc_hba *phba, struct list_head *list,
5764 struct lpfc_name *vport_wwpn,
5765 struct lpfc_name *target_wwpn, uint64_t lun)
5766{
5767
5768 struct lpfc_device_data *lun_info;
5769
5770 if (unlikely(!phba) || !list || !vport_wwpn || !target_wwpn ||
James Smartf38fa0b2014-04-04 13:52:21 -04005771 !phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005772 return NULL;
5773
5774 /* Check to see if the lun is already enabled for OAS. */
5775
5776 list_for_each_entry(lun_info, list, listentry) {
5777 if ((memcmp(&lun_info->device_id.vport_wwpn, vport_wwpn,
5778 sizeof(struct lpfc_name)) == 0) &&
5779 (memcmp(&lun_info->device_id.target_wwpn, target_wwpn,
5780 sizeof(struct lpfc_name)) == 0) &&
5781 (lun_info->device_id.lun == lun))
5782 return lun_info;
5783 }
5784
5785 return NULL;
5786}
5787
5788/**
5789 * lpfc_find_next_oas_lun - searches for the next oas lun
Lee Joneseceee002020-11-02 14:23:43 +00005790 * @phba: Pointer to host bus adapter structure.
James Smart1ba981f2014-02-20 09:56:45 -05005791 * @vport_wwpn: Pointer to vport's wwpn information
5792 * @target_wwpn: Pointer to target's wwpn information
5793 * @starting_lun: Pointer to the lun to start searching for
5794 * @found_vport_wwpn: Pointer to the found lun's vport wwpn information
5795 * @found_target_wwpn: Pointer to the found lun's target wwpn information
5796 * @found_lun: Pointer to the found lun.
5797 * @found_lun_status: Pointer to status of the found lun.
Lee Joneseceee002020-11-02 14:23:43 +00005798 * @found_lun_pri: Pointer to priority of the found lun.
James Smart1ba981f2014-02-20 09:56:45 -05005799 *
5800 * This routine searches the luns list for the specified lun
5801 * or the first lun for the vport/target. If the vport wwpn contains
5802 * a zero value then a specific vport is not specified. In this case
5803 * any vport which contains the lun will be considered a match. If the
5804 * target wwpn contains a zero value then a specific target is not specified.
5805 * In this case any target which contains the lun will be considered a
5806 * match. If the lun is found, the lun, vport wwpn, target wwpn and lun status
5807 * are returned. The function will also return the next lun if available.
5808 * If the next lun is not found, starting_lun parameter will be set to
5809 * NO_MORE_OAS_LUN.
5810 *
5811 * Return codes:
5812 * non-0 - Error
5813 * 0 - Success
5814 **/
5815bool
5816lpfc_find_next_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
5817 struct lpfc_name *target_wwpn, uint64_t *starting_lun,
5818 struct lpfc_name *found_vport_wwpn,
5819 struct lpfc_name *found_target_wwpn,
5820 uint64_t *found_lun,
James Smartb5749fe2016-12-19 15:07:26 -08005821 uint32_t *found_lun_status,
5822 uint32_t *found_lun_pri)
James Smart1ba981f2014-02-20 09:56:45 -05005823{
5824
5825 unsigned long flags;
5826 struct lpfc_device_data *lun_info;
5827 struct lpfc_device_id *device_id;
5828 uint64_t lun;
5829 bool found = false;
5830
5831 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
5832 !starting_lun || !found_vport_wwpn ||
5833 !found_target_wwpn || !found_lun || !found_lun_status ||
5834 (*starting_lun == NO_MORE_OAS_LUN) ||
James Smartf38fa0b2014-04-04 13:52:21 -04005835 !phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005836 return false;
5837
5838 lun = *starting_lun;
5839 *found_lun = NO_MORE_OAS_LUN;
5840 *starting_lun = NO_MORE_OAS_LUN;
5841
5842 /* Search for lun or the lun closet in value */
5843
5844 spin_lock_irqsave(&phba->devicelock, flags);
5845 list_for_each_entry(lun_info, &phba->luns, listentry) {
5846 if (((wwn_to_u64(vport_wwpn->u.wwn) == 0) ||
5847 (memcmp(&lun_info->device_id.vport_wwpn, vport_wwpn,
5848 sizeof(struct lpfc_name)) == 0)) &&
5849 ((wwn_to_u64(target_wwpn->u.wwn) == 0) ||
5850 (memcmp(&lun_info->device_id.target_wwpn, target_wwpn,
5851 sizeof(struct lpfc_name)) == 0)) &&
5852 (lun_info->oas_enabled)) {
5853 device_id = &lun_info->device_id;
5854 if ((!found) &&
5855 ((lun == FIND_FIRST_OAS_LUN) ||
5856 (device_id->lun == lun))) {
5857 *found_lun = device_id->lun;
5858 memcpy(found_vport_wwpn,
5859 &device_id->vport_wwpn,
5860 sizeof(struct lpfc_name));
5861 memcpy(found_target_wwpn,
5862 &device_id->target_wwpn,
5863 sizeof(struct lpfc_name));
5864 if (lun_info->available)
5865 *found_lun_status =
5866 OAS_LUN_STATUS_EXISTS;
5867 else
5868 *found_lun_status = 0;
James Smartb5749fe2016-12-19 15:07:26 -08005869 *found_lun_pri = lun_info->priority;
James Smart1ba981f2014-02-20 09:56:45 -05005870 if (phba->cfg_oas_flags & OAS_FIND_ANY_VPORT)
5871 memset(vport_wwpn, 0x0,
5872 sizeof(struct lpfc_name));
5873 if (phba->cfg_oas_flags & OAS_FIND_ANY_TARGET)
5874 memset(target_wwpn, 0x0,
5875 sizeof(struct lpfc_name));
5876 found = true;
5877 } else if (found) {
5878 *starting_lun = device_id->lun;
5879 memcpy(vport_wwpn, &device_id->vport_wwpn,
5880 sizeof(struct lpfc_name));
5881 memcpy(target_wwpn, &device_id->target_wwpn,
5882 sizeof(struct lpfc_name));
5883 break;
5884 }
5885 }
5886 }
5887 spin_unlock_irqrestore(&phba->devicelock, flags);
5888 return found;
5889}
5890
5891/**
5892 * lpfc_enable_oas_lun - enables a lun for OAS operations
Lee Joneseceee002020-11-02 14:23:43 +00005893 * @phba: Pointer to host bus adapter structure.
James Smart1ba981f2014-02-20 09:56:45 -05005894 * @vport_wwpn: Pointer to vport's wwpn information
5895 * @target_wwpn: Pointer to target's wwpn information
5896 * @lun: Lun
Lee Joneseceee002020-11-02 14:23:43 +00005897 * @pri: Priority
James Smart1ba981f2014-02-20 09:56:45 -05005898 *
5899 * This routine enables a lun for oas operations. The routines does so by
5900 * doing the following :
5901 *
5902 * 1) Checks to see if the device data for the lun has been created.
5903 * 2) If found, sets the OAS enabled flag if not set and returns.
5904 * 3) Otherwise, creates a device data structure.
5905 * 4) If successfully created, indicates the device data is for an OAS lun,
5906 * indicates the lun is not available and add to the list of luns.
5907 *
5908 * Return codes:
5909 * false - Error
5910 * true - Success
5911 **/
5912bool
5913lpfc_enable_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
James Smartc92c8412016-07-06 12:36:05 -07005914 struct lpfc_name *target_wwpn, uint64_t lun, uint8_t pri)
James Smart1ba981f2014-02-20 09:56:45 -05005915{
5916
5917 struct lpfc_device_data *lun_info;
5918 unsigned long flags;
5919
5920 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
James Smartf38fa0b2014-04-04 13:52:21 -04005921 !phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005922 return false;
5923
5924 spin_lock_irqsave(&phba->devicelock, flags);
5925
5926 /* Check to see if the device data for the lun has been created */
5927 lun_info = __lpfc_get_device_data(phba, &phba->luns, vport_wwpn,
5928 target_wwpn, lun);
5929 if (lun_info) {
5930 if (!lun_info->oas_enabled)
5931 lun_info->oas_enabled = true;
James Smartb5749fe2016-12-19 15:07:26 -08005932 lun_info->priority = pri;
James Smart1ba981f2014-02-20 09:56:45 -05005933 spin_unlock_irqrestore(&phba->devicelock, flags);
5934 return true;
5935 }
5936
5937 /* Create an lun info structure and add to list of luns */
5938 lun_info = lpfc_create_device_data(phba, vport_wwpn, target_wwpn, lun,
James Smart2d71dc82019-05-21 17:48:51 -07005939 pri, true);
James Smart1ba981f2014-02-20 09:56:45 -05005940 if (lun_info) {
5941 lun_info->oas_enabled = true;
James Smartc92c8412016-07-06 12:36:05 -07005942 lun_info->priority = pri;
James Smart1ba981f2014-02-20 09:56:45 -05005943 lun_info->available = false;
5944 list_add_tail(&lun_info->listentry, &phba->luns);
5945 spin_unlock_irqrestore(&phba->devicelock, flags);
5946 return true;
5947 }
5948 spin_unlock_irqrestore(&phba->devicelock, flags);
5949 return false;
5950}
5951
5952/**
5953 * lpfc_disable_oas_lun - disables a lun for OAS operations
Lee Joneseceee002020-11-02 14:23:43 +00005954 * @phba: Pointer to host bus adapter structure.
James Smart1ba981f2014-02-20 09:56:45 -05005955 * @vport_wwpn: Pointer to vport's wwpn information
5956 * @target_wwpn: Pointer to target's wwpn information
5957 * @lun: Lun
Lee Joneseceee002020-11-02 14:23:43 +00005958 * @pri: Priority
James Smart1ba981f2014-02-20 09:56:45 -05005959 *
5960 * This routine disables a lun for oas operations. The routines does so by
5961 * doing the following :
5962 *
5963 * 1) Checks to see if the device data for the lun is created.
5964 * 2) If present, clears the flag indicating this lun is for OAS.
5965 * 3) If the lun is not available by the system, the device data is
5966 * freed.
5967 *
5968 * Return codes:
5969 * false - Error
5970 * true - Success
5971 **/
5972bool
5973lpfc_disable_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
James Smartb5749fe2016-12-19 15:07:26 -08005974 struct lpfc_name *target_wwpn, uint64_t lun, uint8_t pri)
James Smart1ba981f2014-02-20 09:56:45 -05005975{
5976
5977 struct lpfc_device_data *lun_info;
5978 unsigned long flags;
5979
5980 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
James Smartf38fa0b2014-04-04 13:52:21 -04005981 !phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005982 return false;
5983
5984 spin_lock_irqsave(&phba->devicelock, flags);
5985
5986 /* Check to see if the lun is available. */
5987 lun_info = __lpfc_get_device_data(phba,
5988 &phba->luns, vport_wwpn,
5989 target_wwpn, lun);
5990 if (lun_info) {
5991 lun_info->oas_enabled = false;
James Smartb5749fe2016-12-19 15:07:26 -08005992 lun_info->priority = pri;
James Smart1ba981f2014-02-20 09:56:45 -05005993 if (!lun_info->available)
5994 lpfc_delete_device_data(phba, lun_info);
5995 spin_unlock_irqrestore(&phba->devicelock, flags);
5996 return true;
5997 }
5998
5999 spin_unlock_irqrestore(&phba->devicelock, flags);
6000 return false;
6001}
James Smart92d7f7b2007-06-17 19:56:38 -05006002
James Smart895427b2017-02-12 13:52:30 -08006003static int
6004lpfc_no_command(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
6005{
6006 return SCSI_MLQUEUE_HOST_BUSY;
6007}
6008
6009static int
6010lpfc_no_handler(struct scsi_cmnd *cmnd)
6011{
6012 return FAILED;
6013}
6014
6015static int
6016lpfc_no_slave(struct scsi_device *sdev)
6017{
6018 return -ENODEV;
6019}
6020
6021struct scsi_host_template lpfc_template_nvme = {
6022 .module = THIS_MODULE,
6023 .name = LPFC_DRIVER_NAME,
6024 .proc_name = LPFC_DRIVER_NAME,
6025 .info = lpfc_info,
6026 .queuecommand = lpfc_no_command,
6027 .eh_abort_handler = lpfc_no_handler,
6028 .eh_device_reset_handler = lpfc_no_handler,
6029 .eh_target_reset_handler = lpfc_no_handler,
6030 .eh_bus_reset_handler = lpfc_no_handler,
6031 .eh_host_reset_handler = lpfc_no_handler,
6032 .slave_alloc = lpfc_no_slave,
6033 .slave_configure = lpfc_no_slave,
6034 .scan_finished = lpfc_scan_finished,
6035 .this_id = -1,
6036 .sg_tablesize = 1,
6037 .cmd_per_lun = 1,
James Smart895427b2017-02-12 13:52:30 -08006038 .shost_attrs = lpfc_hba_attrs,
James Smart7c30bb62020-10-20 13:27:16 -07006039 .max_sectors = 0xFFFFFFFF,
James Smart895427b2017-02-12 13:52:30 -08006040 .vendor_id = LPFC_NL_VENDOR_ID,
6041 .track_queue_depth = 0,
6042};
6043
dea31012005-04-17 16:05:31 -05006044struct scsi_host_template lpfc_template = {
6045 .module = THIS_MODULE,
6046 .name = LPFC_DRIVER_NAME,
James Smart08dcd4c2016-07-06 12:35:59 -07006047 .proc_name = LPFC_DRIVER_NAME,
dea31012005-04-17 16:05:31 -05006048 .info = lpfc_info,
6049 .queuecommand = lpfc_queuecommand,
Christoph Hellwigb6a05c82017-01-30 13:18:58 +01006050 .eh_timed_out = fc_eh_timed_out,
dea31012005-04-17 16:05:31 -05006051 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04006052 .eh_device_reset_handler = lpfc_device_reset_handler,
6053 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart7054a602007-04-25 09:52:34 -04006054 .eh_bus_reset_handler = lpfc_bus_reset_handler,
James Smart27b01b82012-05-09 21:19:44 -04006055 .eh_host_reset_handler = lpfc_host_reset_handler,
dea31012005-04-17 16:05:31 -05006056 .slave_alloc = lpfc_slave_alloc,
6057 .slave_configure = lpfc_slave_configure,
6058 .slave_destroy = lpfc_slave_destroy,
James Smart47a86172007-04-25 09:53:22 -04006059 .scan_finished = lpfc_scan_finished,
dea31012005-04-17 16:05:31 -05006060 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05006061 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
dea31012005-04-17 16:05:31 -05006062 .cmd_per_lun = LPFC_CMD_PER_LUN,
James Smart2e0fef82007-06-17 19:56:36 -05006063 .shost_attrs = lpfc_hba_attrs,
James Smart7c30bb62020-10-20 13:27:16 -07006064 .max_sectors = 0xFFFFFFFF,
James Smartf1c3b0f2009-07-19 10:01:32 -04006065 .vendor_id = LPFC_NL_VENDOR_ID,
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01006066 .change_queue_depth = scsi_change_queue_depth,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01006067 .track_queue_depth = 1,
dea31012005-04-17 16:05:31 -05006068};