blob: cbac07622145695cbc7528556eef206d9afafbbe [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 Smart67073c62021-03-01 09:18:21 -08004 * Copyright (C) 2017-2021 Broadcom. All Rights Reserved. The term *
5 * “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>
Gaurav Srivastavadc507152021-06-08 10:05:51 +053031#include <linux/blk-cgroup.h>
James Smart737d4242013-04-17 20:14:49 -040032#include <net/checksum.h>
dea31012005-04-17 16:05:31 -050033
34#include <scsi/scsi.h>
35#include <scsi/scsi_device.h>
James Smarte2a0a9d2008-12-04 22:40:02 -050036#include <scsi/scsi_eh.h>
dea31012005-04-17 16:05:31 -050037#include <scsi/scsi_host.h>
38#include <scsi/scsi_tcq.h>
39#include <scsi/scsi_transport_fc.h>
40
41#include "lpfc_version.h"
James Smartda0436e2009-05-22 14:51:39 -040042#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050043#include "lpfc_hw.h"
44#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040045#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040046#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050047#include "lpfc_disc.h"
dea31012005-04-17 16:05:31 -050048#include "lpfc.h"
James Smart9a6b09c2012-03-01 22:37:42 -050049#include "lpfc_scsi.h"
dea31012005-04-17 16:05:31 -050050#include "lpfc_logmsg.h"
51#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050052#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050053
54#define LPFC_RESET_WAIT 2
55#define LPFC_ABORT_WAIT 2
56
James Smarte2a0a9d2008-12-04 22:40:02 -050057static char *dif_op_str[] = {
James Smart9a6b09c2012-03-01 22:37:42 -050058 "PROT_NORMAL",
59 "PROT_READ_INSERT",
60 "PROT_WRITE_STRIP",
61 "PROT_READ_STRIP",
62 "PROT_WRITE_INSERT",
63 "PROT_READ_PASS",
64 "PROT_WRITE_PASS",
65};
66
James Smartf9bb2da2011-10-10 21:34:11 -040067struct scsi_dif_tuple {
68 __be16 guard_tag; /* Checksum */
69 __be16 app_tag; /* Opaque storage */
70 __be32 ref_tag; /* Target LBA or indirect LBA */
71};
72
James Smart1ba981f2014-02-20 09:56:45 -050073static struct lpfc_rport_data *
74lpfc_rport_data_from_scsi_device(struct scsi_device *sdev)
75{
76 struct lpfc_vport *vport = (struct lpfc_vport *)sdev->host->hostdata;
77
James Smartf38fa0b2014-04-04 13:52:21 -040078 if (vport->phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -050079 return ((struct lpfc_device_data *)sdev->hostdata)->rport_data;
80 else
81 return (struct lpfc_rport_data *)sdev->hostdata;
82}
83
James Smartda0436e2009-05-22 14:51:39 -040084static void
James Smartc4908502019-01-28 11:14:28 -080085lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_io_buf *psb);
James Smart1c6f4ef52009-11-18 15:40:49 -050086static void
James Smartc4908502019-01-28 11:14:28 -080087lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_io_buf *psb);
James Smart9c6aa9d2013-05-31 17:03:39 -040088static int
89lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc);
Gaurav Srivastavadc507152021-06-08 10:05:51 +053090static void
91lpfc_put_vmid_in_hashtable(struct lpfc_vport *vport, u32 hash,
92 struct lpfc_vmid *vmp);
93static void lpfc_vmid_update_entry(struct lpfc_vport *vport, struct scsi_cmnd
94 *cmd, struct lpfc_vmid *vmp,
95 union lpfc_vmid_io_tag *tag);
96static void lpfc_vmid_assign_cs_ctl(struct lpfc_vport *vport,
97 struct lpfc_vmid *vmid);
James Smarte2a0a9d2008-12-04 22:40:02 -050098
James Smart9c6aa9d2013-05-31 17:03:39 -040099static inline unsigned
100lpfc_cmd_blksize(struct scsi_cmnd *sc)
101{
102 return sc->device->sector_size;
103}
104
105#define LPFC_CHECK_PROTECT_GUARD 1
106#define LPFC_CHECK_PROTECT_REF 2
107static inline unsigned
108lpfc_cmd_protect(struct scsi_cmnd *sc, int flag)
109{
110 return 1;
111}
112
113static inline unsigned
114lpfc_cmd_guard_csum(struct scsi_cmnd *sc)
115{
116 if (lpfc_prot_group_type(NULL, sc) == LPFC_PG_TYPE_NO_DIF)
117 return 0;
118 if (scsi_host_get_guard(sc->device->host) == SHOST_DIX_GUARD_IP)
119 return 1;
120 return 0;
121}
122
James Smartea2151b2008-09-07 11:52:10 -0400123/**
James Smartf1126682009-06-10 17:22:44 -0400124 * lpfc_sli4_set_rsp_sgl_last - Set the last bit in the response sge.
125 * @phba: Pointer to HBA object.
126 * @lpfc_cmd: lpfc scsi command object pointer.
127 *
128 * This function is called from the lpfc_prep_task_mgmt_cmd function to
129 * set the last bit in the response sge entry.
130 **/
131static void
132lpfc_sli4_set_rsp_sgl_last(struct lpfc_hba *phba,
James Smartc4908502019-01-28 11:14:28 -0800133 struct lpfc_io_buf *lpfc_cmd)
James Smartf1126682009-06-10 17:22:44 -0400134{
James Smart0794d602019-01-28 11:14:19 -0800135 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
James Smartf1126682009-06-10 17:22:44 -0400136 if (sgl) {
137 sgl += 1;
138 sgl->word2 = le32_to_cpu(sgl->word2);
139 bf_set(lpfc_sli4_sge_last, sgl, 1);
140 sgl->word2 = cpu_to_le32(sgl->word2);
141 }
142}
143
James Smart68a6a662021-03-01 09:18:02 -0800144#define LPFC_INVALID_REFTAG ((u32)-1)
145
James Smartf1126682009-06-10 17:22:44 -0400146/**
James Smart3621a712009-04-06 18:47:14 -0400147 * lpfc_update_stats - Update statistical data for the command completion
James Smart9df0a032019-09-21 20:58:54 -0700148 * @vport: The virtual port on which this call is executing.
James Smartea2151b2008-09-07 11:52:10 -0400149 * @lpfc_cmd: lpfc scsi command object pointer.
150 *
151 * This function is called when there is a command completion and this
152 * function updates the statistical data for the command completion.
153 **/
154static void
James Smart9df0a032019-09-21 20:58:54 -0700155lpfc_update_stats(struct lpfc_vport *vport, struct lpfc_io_buf *lpfc_cmd)
James Smartea2151b2008-09-07 11:52:10 -0400156{
James Smart9df0a032019-09-21 20:58:54 -0700157 struct lpfc_hba *phba = vport->phba;
James Smart18027a82018-09-10 10:30:49 -0700158 struct lpfc_rport_data *rdata;
159 struct lpfc_nodelist *pnode;
James Smartea2151b2008-09-07 11:52:10 -0400160 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
161 unsigned long flags;
James Smart9df0a032019-09-21 20:58:54 -0700162 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartea2151b2008-09-07 11:52:10 -0400163 unsigned long latency;
164 int i;
165
James Smart18027a82018-09-10 10:30:49 -0700166 if (!vport->stat_data_enabled ||
167 vport->stat_data_blocked ||
168 (cmd->result))
James Smartea2151b2008-09-07 11:52:10 -0400169 return;
170
James Smart9f1e1b52008-12-04 22:39:40 -0500171 latency = jiffies_to_msecs((long)jiffies - (long)lpfc_cmd->start_time);
James Smart18027a82018-09-10 10:30:49 -0700172 rdata = lpfc_cmd->rdata;
173 pnode = rdata->pnode;
James Smart9f1e1b52008-12-04 22:39:40 -0500174
James Smartea2151b2008-09-07 11:52:10 -0400175 spin_lock_irqsave(shost->host_lock, flags);
James Smart18027a82018-09-10 10:30:49 -0700176 if (!pnode ||
177 !pnode->lat_data ||
178 (phba->bucket_type == LPFC_NO_BUCKET)) {
James Smartea2151b2008-09-07 11:52:10 -0400179 spin_unlock_irqrestore(shost->host_lock, flags);
180 return;
181 }
James Smartea2151b2008-09-07 11:52:10 -0400182
183 if (phba->bucket_type == LPFC_LINEAR_BUCKET) {
184 i = (latency + phba->bucket_step - 1 - phba->bucket_base)/
185 phba->bucket_step;
James Smart9f1e1b52008-12-04 22:39:40 -0500186 /* check array subscript bounds */
187 if (i < 0)
188 i = 0;
189 else if (i >= LPFC_MAX_BUCKET_COUNT)
190 i = LPFC_MAX_BUCKET_COUNT - 1;
James Smartea2151b2008-09-07 11:52:10 -0400191 } else {
192 for (i = 0; i < LPFC_MAX_BUCKET_COUNT-1; i++)
193 if (latency <= (phba->bucket_base +
194 ((1<<i)*phba->bucket_step)))
195 break;
196 }
197
198 pnode->lat_data[i].cmd_count++;
199 spin_unlock_irqrestore(shost->host_lock, flags);
200}
201
James Smartea2151b2008-09-07 11:52:10 -0400202/**
James Smart3621a712009-04-06 18:47:14 -0400203 * lpfc_rampdown_queue_depth - Post RAMP_DOWN_QUEUE event to worker thread
James Smart9bad7672008-12-04 22:39:02 -0500204 * @phba: The Hba for which this call is being executed.
205 *
206 * This routine is called when there is resource error in driver or firmware.
207 * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
208 * posts at most 1 event each second. This routine wakes up worker thread of
209 * @phba to process WORKER_RAM_DOWN_EVENT event.
210 *
211 * This routine should be called with no lock held.
212 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500213void
James Smarteaf15d52008-12-04 22:39:29 -0500214lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
James Smart92d7f7b2007-06-17 19:56:38 -0500215{
216 unsigned long flags;
James Smart5e9d9b82008-06-14 22:52:53 -0400217 uint32_t evt_posted;
Manuel Schölling0d4aec12014-09-03 12:55:58 -0400218 unsigned long expires;
James Smart92d7f7b2007-06-17 19:56:38 -0500219
220 spin_lock_irqsave(&phba->hbalock, flags);
221 atomic_inc(&phba->num_rsrc_err);
222 phba->last_rsrc_error_time = jiffies;
223
Manuel Schölling0d4aec12014-09-03 12:55:58 -0400224 expires = phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL;
225 if (time_after(expires, jiffies)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500226 spin_unlock_irqrestore(&phba->hbalock, flags);
227 return;
228 }
229
230 phba->last_ramp_down_time = jiffies;
231
232 spin_unlock_irqrestore(&phba->hbalock, flags);
233
234 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400235 evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
236 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500237 phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500238 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
239
James Smart5e9d9b82008-06-14 22:52:53 -0400240 if (!evt_posted)
241 lpfc_worker_wake_up(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500242 return;
243}
244
James Smart9bad7672008-12-04 22:39:02 -0500245/**
James Smart3621a712009-04-06 18:47:14 -0400246 * lpfc_ramp_down_queue_handler - WORKER_RAMP_DOWN_QUEUE event handler
James Smart9bad7672008-12-04 22:39:02 -0500247 * @phba: The Hba for which this call is being executed.
248 *
249 * This routine is called to process WORKER_RAMP_DOWN_QUEUE event for worker
250 * thread.This routine reduces queue depth for all scsi device on each vport
251 * associated with @phba.
252 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500253void
254lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
255{
James Smart549e55c2007-08-02 11:09:51 -0400256 struct lpfc_vport **vports;
257 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500258 struct scsi_device *sdev;
James Smart5ffc2662009-11-18 15:39:44 -0500259 unsigned long new_queue_depth;
James Smart92d7f7b2007-06-17 19:56:38 -0500260 unsigned long num_rsrc_err, num_cmd_success;
James Smart549e55c2007-08-02 11:09:51 -0400261 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500262
263 num_rsrc_err = atomic_read(&phba->num_rsrc_err);
264 num_cmd_success = atomic_read(&phba->num_cmd_success);
265
James Smart75ad83a2012-05-09 21:18:40 -0400266 /*
267 * The error and success command counters are global per
268 * driver instance. If another handler has already
269 * operated on this error event, just exit.
270 */
271 if (num_rsrc_err == 0)
272 return;
273
James Smart549e55c2007-08-02 11:09:51 -0400274 vports = lpfc_create_vport_work_array(phba);
275 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400276 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400277 shost = lpfc_shost_from_vport(vports[i]);
278 shost_for_each_device(sdev, shost) {
James Smart92d7f7b2007-06-17 19:56:38 -0500279 new_queue_depth =
James Smart549e55c2007-08-02 11:09:51 -0400280 sdev->queue_depth * num_rsrc_err /
281 (num_rsrc_err + num_cmd_success);
282 if (!new_queue_depth)
283 new_queue_depth = sdev->queue_depth - 1;
284 else
285 new_queue_depth = sdev->queue_depth -
286 new_queue_depth;
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +0100287 scsi_change_queue_depth(sdev, new_queue_depth);
James Smart549e55c2007-08-02 11:09:51 -0400288 }
James Smart92d7f7b2007-06-17 19:56:38 -0500289 }
James Smart09372822008-01-11 01:52:54 -0500290 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500291 atomic_set(&phba->num_rsrc_err, 0);
292 atomic_set(&phba->num_cmd_success, 0);
293}
294
James Smart9bad7672008-12-04 22:39:02 -0500295/**
James Smart3621a712009-04-06 18:47:14 -0400296 * lpfc_scsi_dev_block - set all scsi hosts to block state
James Smarta8e497d2008-08-24 21:50:11 -0400297 * @phba: Pointer to HBA context object.
298 *
299 * This function walks vport list and set each SCSI host to block state
300 * by invoking fc_remote_port_delete() routine. This function is invoked
301 * with EEH when device's PCI slot has been permanently disabled.
302 **/
303void
304lpfc_scsi_dev_block(struct lpfc_hba *phba)
305{
306 struct lpfc_vport **vports;
307 struct Scsi_Host *shost;
308 struct scsi_device *sdev;
309 struct fc_rport *rport;
310 int i;
311
312 vports = lpfc_create_vport_work_array(phba);
313 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400314 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8e497d2008-08-24 21:50:11 -0400315 shost = lpfc_shost_from_vport(vports[i]);
316 shost_for_each_device(sdev, shost) {
317 rport = starget_to_rport(scsi_target(sdev));
318 fc_remote_port_delete(rport);
319 }
320 }
321 lpfc_destroy_vport_work_array(phba, vports);
322}
323
James Smart9bad7672008-12-04 22:39:02 -0500324/**
James Smart3772a992009-05-22 14:50:54 -0400325 * lpfc_new_scsi_buf_s3 - Scsi buffer allocator for HBA with SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -0500326 * @vport: The virtual port for which this call being executed.
Lee Joneseceee002020-11-02 14:23:43 +0000327 * @num_to_alloc: The requested number of buffers to allocate.
James Smart9bad7672008-12-04 22:39:02 -0500328 *
James Smart3772a992009-05-22 14:50:54 -0400329 * This routine allocates a scsi buffer for device with SLI-3 interface spec,
330 * the scsi buffer contains all the necessary information needed to initiate
331 * a SCSI I/O. The non-DMAable buffer region contains information to build
332 * the IOCB. The DMAable region contains memory for the FCP CMND, FCP RSP,
333 * and the initial BPL. In addition to allocating memory, the FCP CMND and
334 * FCP RSP BDEs are setup in the BPL and the BPL BDE is setup in the IOCB.
James Smart9bad7672008-12-04 22:39:02 -0500335 *
336 * Return codes:
James Smart3772a992009-05-22 14:50:54 -0400337 * int - number of scsi buffers that were allocated.
338 * 0 = failure, less than num_to_alloc is a partial failure.
James Smart9bad7672008-12-04 22:39:02 -0500339 **/
James Smart3772a992009-05-22 14:50:54 -0400340static int
341lpfc_new_scsi_buf_s3(struct lpfc_vport *vport, int num_to_alloc)
dea31012005-04-17 16:05:31 -0500342{
James Smart2e0fef82007-06-17 19:56:36 -0500343 struct lpfc_hba *phba = vport->phba;
James Smartc4908502019-01-28 11:14:28 -0800344 struct lpfc_io_buf *psb;
dea31012005-04-17 16:05:31 -0500345 struct ulp_bde64 *bpl;
346 IOCB_t *iocb;
James Smart34b02dc2008-08-24 21:49:55 -0400347 dma_addr_t pdma_phys_fcp_cmd;
348 dma_addr_t pdma_phys_fcp_rsp;
James Smart0794d602019-01-28 11:14:19 -0800349 dma_addr_t pdma_phys_sgl;
James Bottomley604a3e32005-10-29 10:28:33 -0500350 uint16_t iotag;
James Smart96f70772013-04-17 20:16:15 -0400351 int bcnt, bpl_size;
352
353 bpl_size = phba->cfg_sg_dma_buf_size -
354 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
355
356 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
357 "9067 ALLOC %d scsi_bufs: %d (%d + %d + %d)\n",
358 num_to_alloc, phba->cfg_sg_dma_buf_size,
359 (int)sizeof(struct fcp_cmnd),
360 (int)sizeof(struct fcp_rsp), bpl_size);
dea31012005-04-17 16:05:31 -0500361
James Smart3772a992009-05-22 14:50:54 -0400362 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
James Smartc4908502019-01-28 11:14:28 -0800363 psb = kzalloc(sizeof(struct lpfc_io_buf), GFP_KERNEL);
James Smart3772a992009-05-22 14:50:54 -0400364 if (!psb)
365 break;
dea31012005-04-17 16:05:31 -0500366
James Smart3772a992009-05-22 14:50:54 -0400367 /*
368 * Get memory from the pci pool to map the virt space to pci
369 * bus space for an I/O. The DMA buffer includes space for the
370 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
371 * necessary to support the sg_tablesize.
372 */
Romain Perier771db5c2017-07-06 10:13:05 +0200373 psb->data = dma_pool_zalloc(phba->lpfc_sg_dma_buf_pool,
James Smart3772a992009-05-22 14:50:54 -0400374 GFP_KERNEL, &psb->dma_handle);
375 if (!psb->data) {
376 kfree(psb);
377 break;
378 }
dea31012005-04-17 16:05:31 -0500379
dea31012005-04-17 16:05:31 -0500380
James Smart3772a992009-05-22 14:50:54 -0400381 /* Allocate iotag for psb->cur_iocbq. */
382 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
383 if (iotag == 0) {
Romain Perier771db5c2017-07-06 10:13:05 +0200384 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
James Smart895427b2017-02-12 13:52:30 -0800385 psb->data, psb->dma_handle);
James Smart3772a992009-05-22 14:50:54 -0400386 kfree(psb);
387 break;
388 }
389 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Bottomley604a3e32005-10-29 10:28:33 -0500390
James Smart3772a992009-05-22 14:50:54 -0400391 psb->fcp_cmnd = psb->data;
392 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
James Smart0794d602019-01-28 11:14:19 -0800393 psb->dma_sgl = psb->data + sizeof(struct fcp_cmnd) +
James Smart34b02dc2008-08-24 21:49:55 -0400394 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500395
James Smart3772a992009-05-22 14:50:54 -0400396 /* Initialize local short-hand pointers. */
James Smartc4908502019-01-28 11:14:28 -0800397 bpl = (struct ulp_bde64 *)psb->dma_sgl;
James Smart3772a992009-05-22 14:50:54 -0400398 pdma_phys_fcp_cmd = psb->dma_handle;
399 pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
James Smart0794d602019-01-28 11:14:19 -0800400 pdma_phys_sgl = psb->dma_handle + sizeof(struct fcp_cmnd) +
James Smart3772a992009-05-22 14:50:54 -0400401 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500402
James Smart3772a992009-05-22 14:50:54 -0400403 /*
404 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
405 * are sg list bdes. Initialize the first two and leave the
406 * rest for queuecommand.
407 */
408 bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
409 bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
410 bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
411 bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
412 bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
dea31012005-04-17 16:05:31 -0500413
James Smart3772a992009-05-22 14:50:54 -0400414 /* Setup the physical region for the FCP RSP */
415 bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
416 bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
417 bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
418 bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
419 bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
420
421 /*
422 * Since the IOCB for the FCP I/O is built into this
423 * lpfc_scsi_buf, initialize it with all known data now.
424 */
425 iocb = &psb->cur_iocbq.iocb;
426 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
427 if ((phba->sli_rev == 3) &&
428 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
429 /* fill in immediate fcp command BDE */
430 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
431 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
432 iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
433 unsli3.fcp_ext.icd);
434 iocb->un.fcpi64.bdl.addrHigh = 0;
435 iocb->ulpBdeCount = 0;
436 iocb->ulpLe = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300437 /* fill in response BDE */
James Smart3772a992009-05-22 14:50:54 -0400438 iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags =
439 BUFF_TYPE_BDE_64;
440 iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
441 sizeof(struct fcp_rsp);
442 iocb->unsli3.fcp_ext.rbde.addrLow =
443 putPaddrLow(pdma_phys_fcp_rsp);
444 iocb->unsli3.fcp_ext.rbde.addrHigh =
445 putPaddrHigh(pdma_phys_fcp_rsp);
446 } else {
447 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
448 iocb->un.fcpi64.bdl.bdeSize =
449 (2 * sizeof(struct ulp_bde64));
450 iocb->un.fcpi64.bdl.addrLow =
James Smart0794d602019-01-28 11:14:19 -0800451 putPaddrLow(pdma_phys_sgl);
James Smart3772a992009-05-22 14:50:54 -0400452 iocb->un.fcpi64.bdl.addrHigh =
James Smart0794d602019-01-28 11:14:19 -0800453 putPaddrHigh(pdma_phys_sgl);
James Smart3772a992009-05-22 14:50:54 -0400454 iocb->ulpBdeCount = 1;
455 iocb->ulpLe = 1;
456 }
457 iocb->ulpClass = CLASS3;
458 psb->status = IOSTAT_SUCCESS;
James Smartda0436e2009-05-22 14:51:39 -0400459 /* Put it back into the SCSI buffer list */
James Smarteee88772010-09-29 11:19:08 -0400460 psb->cur_iocbq.context1 = psb;
James Smartc2017262019-01-28 11:14:37 -0800461 spin_lock_init(&psb->buf_lock);
James Smart1c6f4ef52009-11-18 15:40:49 -0500462 lpfc_release_scsi_buf_s3(phba, psb);
James Smart3772a992009-05-22 14:50:54 -0400463
James Smart34b02dc2008-08-24 21:49:55 -0400464 }
dea31012005-04-17 16:05:31 -0500465
James Smart3772a992009-05-22 14:50:54 -0400466 return bcnt;
dea31012005-04-17 16:05:31 -0500467}
468
James Smart9bad7672008-12-04 22:39:02 -0500469/**
James Smart1151e3e2011-02-16 12:39:35 -0500470 * lpfc_sli4_vport_delete_fcp_xri_aborted -Remove all ndlp references for vport
471 * @vport: pointer to lpfc vport data structure.
472 *
473 * This routine is invoked by the vport cleanup for deletions and the cleanup
474 * for an ndlp on removal.
475 **/
476void
477lpfc_sli4_vport_delete_fcp_xri_aborted(struct lpfc_vport *vport)
478{
479 struct lpfc_hba *phba = vport->phba;
James Smartc4908502019-01-28 11:14:28 -0800480 struct lpfc_io_buf *psb, *next_psb;
James Smart5e5b5112019-01-28 11:14:22 -0800481 struct lpfc_sli4_hdw_queue *qp;
James Smart1151e3e2011-02-16 12:39:35 -0500482 unsigned long iflag = 0;
James Smart5e5b5112019-01-28 11:14:22 -0800483 int idx;
James Smart1151e3e2011-02-16 12:39:35 -0500484
James Smartf6e84792019-01-28 11:14:38 -0800485 if (!(vport->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
James Smart895427b2017-02-12 13:52:30 -0800486 return;
James Smart5e5b5112019-01-28 11:14:22 -0800487
James Smart1151e3e2011-02-16 12:39:35 -0500488 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart5e5b5112019-01-28 11:14:22 -0800489 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
490 qp = &phba->sli4_hba.hdwq[idx];
491
James Smartc00f62e2019-08-14 16:57:11 -0700492 spin_lock(&qp->abts_io_buf_list_lock);
James Smart5e5b5112019-01-28 11:14:22 -0800493 list_for_each_entry_safe(psb, next_psb,
James Smartc00f62e2019-08-14 16:57:11 -0700494 &qp->lpfc_abts_io_buf_list, list) {
James Smartc438d062019-12-18 15:58:07 -0800495 if (psb->cur_iocbq.iocb_flag & LPFC_IO_NVME)
James Smartc00f62e2019-08-14 16:57:11 -0700496 continue;
497
James Smart5e5b5112019-01-28 11:14:22 -0800498 if (psb->rdata && psb->rdata->pnode &&
499 psb->rdata->pnode->vport == vport)
500 psb->rdata = NULL;
501 }
James Smartc00f62e2019-08-14 16:57:11 -0700502 spin_unlock(&qp->abts_io_buf_list_lock);
James Smart1151e3e2011-02-16 12:39:35 -0500503 }
James Smart1151e3e2011-02-16 12:39:35 -0500504 spin_unlock_irqrestore(&phba->hbalock, iflag);
505}
506
507/**
James Smartc00f62e2019-08-14 16:57:11 -0700508 * lpfc_sli4_io_xri_aborted - Fast-path process of fcp xri abort
James Smartda0436e2009-05-22 14:51:39 -0400509 * @phba: pointer to lpfc hba data structure.
510 * @axri: pointer to the fcp xri abort wcqe structure.
Lee Joneseceee002020-11-02 14:23:43 +0000511 * @idx: index into hdwq
James Smartda0436e2009-05-22 14:51:39 -0400512 *
513 * This routine is invoked by the worker thread to process a SLI4 fast-path
James Smartc00f62e2019-08-14 16:57:11 -0700514 * FCP or NVME aborted xri.
James Smartda0436e2009-05-22 14:51:39 -0400515 **/
516void
James Smartc00f62e2019-08-14 16:57:11 -0700517lpfc_sli4_io_xri_aborted(struct lpfc_hba *phba,
518 struct sli4_wcqe_xri_aborted *axri, int idx)
James Smartda0436e2009-05-22 14:51:39 -0400519{
520 uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
James Smart19ca7602010-11-20 23:11:55 -0500521 uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
James Smartc4908502019-01-28 11:14:28 -0800522 struct lpfc_io_buf *psb, *next_psb;
James Smart5e5b5112019-01-28 11:14:22 -0800523 struct lpfc_sli4_hdw_queue *qp;
James Smartda0436e2009-05-22 14:51:39 -0400524 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -0500525 struct lpfc_iocbq *iocbq;
526 int i;
James Smart19ca7602010-11-20 23:11:55 -0500527 struct lpfc_nodelist *ndlp;
528 int rrq_empty = 0;
James Smart895427b2017-02-12 13:52:30 -0800529 struct lpfc_sli_ring *pring = phba->sli4_hba.els_wq->pring;
James Smart3e49af92021-05-14 12:55:57 -0700530 struct scsi_cmnd *cmd;
James Smartda0436e2009-05-22 14:51:39 -0400531
James Smart895427b2017-02-12 13:52:30 -0800532 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
533 return;
James Smart5e5b5112019-01-28 11:14:22 -0800534
535 qp = &phba->sli4_hba.hdwq[idx];
James Smart0f65ff62010-02-26 14:14:23 -0500536 spin_lock_irqsave(&phba->hbalock, iflag);
James Smartc00f62e2019-08-14 16:57:11 -0700537 spin_lock(&qp->abts_io_buf_list_lock);
James Smartda0436e2009-05-22 14:51:39 -0400538 list_for_each_entry_safe(psb, next_psb,
James Smartc00f62e2019-08-14 16:57:11 -0700539 &qp->lpfc_abts_io_buf_list, list) {
James Smartda0436e2009-05-22 14:51:39 -0400540 if (psb->cur_iocbq.sli4_xritag == xri) {
James Smartc00f62e2019-08-14 16:57:11 -0700541 list_del_init(&psb->list);
James Smart324e1c42019-10-18 14:18:21 -0700542 psb->flags &= ~LPFC_SBUF_XBUSY;
James Smartda0436e2009-05-22 14:51:39 -0400543 psb->status = IOSTAT_SUCCESS;
James Smartc438d062019-12-18 15:58:07 -0800544 if (psb->cur_iocbq.iocb_flag & LPFC_IO_NVME) {
James Smartc00f62e2019-08-14 16:57:11 -0700545 qp->abts_nvme_io_bufs--;
546 spin_unlock(&qp->abts_io_buf_list_lock);
547 spin_unlock_irqrestore(&phba->hbalock, iflag);
548 lpfc_sli4_nvme_xri_aborted(phba, axri, psb);
549 return;
550 }
James Smartc00f62e2019-08-14 16:57:11 -0700551 qp->abts_scsi_io_bufs--;
552 spin_unlock(&qp->abts_io_buf_list_lock);
553
James Smart1151e3e2011-02-16 12:39:35 -0500554 if (psb->rdata && psb->rdata->pnode)
555 ndlp = psb->rdata->pnode;
556 else
557 ndlp = NULL;
558
James Smart19ca7602010-11-20 23:11:55 -0500559 rrq_empty = list_empty(&phba->active_rrq_list);
James Smart0f65ff62010-02-26 14:14:23 -0500560 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartcb69f7d2011-12-13 13:21:57 -0500561 if (ndlp) {
James Smartee0f4fe2012-05-09 21:19:14 -0400562 lpfc_set_rrq_active(phba, ndlp,
563 psb->cur_iocbq.sli4_lxritag, rxid, 1);
James Smartcb69f7d2011-12-13 13:21:57 -0500564 lpfc_sli4_abts_err_handler(phba, ndlp, axri);
565 }
James Smart3e49af92021-05-14 12:55:57 -0700566
567 if (phba->cfg_fcp_wait_abts_rsp) {
568 spin_lock_irqsave(&psb->buf_lock, iflag);
569 cmd = psb->pCmd;
570 psb->pCmd = NULL;
571 spin_unlock_irqrestore(&psb->buf_lock, iflag);
572
573 /* The sdev is not guaranteed to be valid post
574 * scsi_done upcall.
575 */
576 if (cmd)
577 cmd->scsi_done(cmd);
578
579 /*
580 * We expect there is an abort thread waiting
581 * for command completion wake up the thread.
582 */
583 spin_lock_irqsave(&psb->buf_lock, iflag);
584 psb->cur_iocbq.iocb_flag &=
585 ~LPFC_DRIVER_ABORTED;
586 if (psb->waitq)
587 wake_up(psb->waitq);
588 spin_unlock_irqrestore(&psb->buf_lock, iflag);
589 }
590
James Smartda0436e2009-05-22 14:51:39 -0400591 lpfc_release_scsi_buf_s4(phba, psb);
James Smart19ca7602010-11-20 23:11:55 -0500592 if (rrq_empty)
593 lpfc_worker_wake_up(phba);
James Smartda0436e2009-05-22 14:51:39 -0400594 return;
595 }
596 }
James Smartc00f62e2019-08-14 16:57:11 -0700597 spin_unlock(&qp->abts_io_buf_list_lock);
James Smart0f65ff62010-02-26 14:14:23 -0500598 for (i = 1; i <= phba->sli.last_iotag; i++) {
599 iocbq = phba->sli.iocbq_lookup[i];
600
James Smartc00f62e2019-08-14 16:57:11 -0700601 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
602 (iocbq->iocb_flag & LPFC_IO_LIBDFC))
James Smart0f65ff62010-02-26 14:14:23 -0500603 continue;
604 if (iocbq->sli4_xritag != xri)
605 continue;
James Smartc4908502019-01-28 11:14:28 -0800606 psb = container_of(iocbq, struct lpfc_io_buf, cur_iocbq);
James Smart324e1c42019-10-18 14:18:21 -0700607 psb->flags &= ~LPFC_SBUF_XBUSY;
James Smart0f65ff62010-02-26 14:14:23 -0500608 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart0e9bb8d2013-03-01 16:35:12 -0500609 if (!list_empty(&pring->txq))
James Smart589a52d2010-07-14 15:30:54 -0400610 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -0500611 return;
612
613 }
614 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartda0436e2009-05-22 14:51:39 -0400615}
616
617/**
James Smart19ca7602010-11-20 23:11:55 -0500618 * lpfc_get_scsi_buf_s3 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
James Smart3772a992009-05-22 14:50:54 -0400619 * @phba: The HBA for which this call is being executed.
Lee Joneseceee002020-11-02 14:23:43 +0000620 * @ndlp: pointer to a node-list data structure.
621 * @cmnd: Pointer to scsi_cmnd data structure.
James Smart9bad7672008-12-04 22:39:02 -0500622 *
623 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
624 * and returns to caller.
625 *
626 * Return codes:
627 * NULL - Error
628 * Pointer to lpfc_scsi_buf - Success
629 **/
James Smartc4908502019-01-28 11:14:28 -0800630static struct lpfc_io_buf *
James Smartace44e42019-01-28 11:14:27 -0800631lpfc_get_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
632 struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -0500633{
James Smartc4908502019-01-28 11:14:28 -0800634 struct lpfc_io_buf *lpfc_cmd = NULL;
James Smarta40fc5f2013-04-17 20:17:40 -0400635 struct list_head *scsi_buf_list_get = &phba->lpfc_scsi_buf_list_get;
James Smart164cecd2013-09-06 12:22:38 -0400636 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500637
James Smart164cecd2013-09-06 12:22:38 -0400638 spin_lock_irqsave(&phba->scsi_buf_list_get_lock, iflag);
James Smartc4908502019-01-28 11:14:28 -0800639 list_remove_head(scsi_buf_list_get, lpfc_cmd, struct lpfc_io_buf,
James Smarta40fc5f2013-04-17 20:17:40 -0400640 list);
641 if (!lpfc_cmd) {
James Smart164cecd2013-09-06 12:22:38 -0400642 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -0400643 list_splice(&phba->lpfc_scsi_buf_list_put,
644 &phba->lpfc_scsi_buf_list_get);
645 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
646 list_remove_head(scsi_buf_list_get, lpfc_cmd,
James Smartc4908502019-01-28 11:14:28 -0800647 struct lpfc_io_buf, list);
James Smart164cecd2013-09-06 12:22:38 -0400648 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smart1dcb58e2007-04-25 09:51:30 -0400649 }
James Smart164cecd2013-09-06 12:22:38 -0400650 spin_unlock_irqrestore(&phba->scsi_buf_list_get_lock, iflag);
James Smart2a5b7d62018-07-31 17:23:22 -0700651
652 if (lpfc_ndlp_check_qdepth(phba, ndlp) && lpfc_cmd) {
653 atomic_inc(&ndlp->cmd_pending);
654 lpfc_cmd->flags |= LPFC_SBUF_BUMP_QDEPTH;
655 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400656 return lpfc_cmd;
657}
James Smart19ca7602010-11-20 23:11:55 -0500658/**
James Smart5e5b5112019-01-28 11:14:22 -0800659 * lpfc_get_scsi_buf_s4 - Get a scsi buffer from io_buf_list of the HBA
James Smart19ca7602010-11-20 23:11:55 -0500660 * @phba: The HBA for which this call is being executed.
Lee Joneseceee002020-11-02 14:23:43 +0000661 * @ndlp: pointer to a node-list data structure.
662 * @cmnd: Pointer to scsi_cmnd data structure.
James Smart19ca7602010-11-20 23:11:55 -0500663 *
James Smart5e5b5112019-01-28 11:14:22 -0800664 * This routine removes a scsi buffer from head of @hdwq io_buf_list
James Smart19ca7602010-11-20 23:11:55 -0500665 * and returns to caller.
666 *
667 * Return codes:
668 * NULL - Error
669 * Pointer to lpfc_scsi_buf - Success
670 **/
James Smartc4908502019-01-28 11:14:28 -0800671static struct lpfc_io_buf *
James Smartace44e42019-01-28 11:14:27 -0800672lpfc_get_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
673 struct scsi_cmnd *cmnd)
James Smart19ca7602010-11-20 23:11:55 -0500674{
James Smartc4908502019-01-28 11:14:28 -0800675 struct lpfc_io_buf *lpfc_cmd;
James Smart5e5b5112019-01-28 11:14:22 -0800676 struct lpfc_sli4_hdw_queue *qp;
James Smart0794d602019-01-28 11:14:19 -0800677 struct sli4_sge *sgl;
James Smart0794d602019-01-28 11:14:19 -0800678 dma_addr_t pdma_phys_fcp_rsp;
679 dma_addr_t pdma_phys_fcp_cmd;
James Smartd79c9e92019-08-14 16:57:09 -0700680 uint32_t cpu, idx;
James Smartace44e42019-01-28 11:14:27 -0800681 int tag;
James Smartd79c9e92019-08-14 16:57:09 -0700682 struct fcp_cmd_rsp_buf *tmp = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500683
Bart Van Assched6d189c2019-03-28 11:06:22 -0700684 cpu = raw_smp_processor_id();
James Smart45aa3122019-01-28 11:14:29 -0800685 if (cmnd && phba->cfg_fcp_io_sched == LPFC_FCP_SCHED_BY_HDWQ) {
Bart Van Assche4221c8a2021-08-09 16:03:30 -0700686 tag = blk_mq_unique_tag(scsi_cmd_to_rq(cmnd));
James Smartace44e42019-01-28 11:14:27 -0800687 idx = blk_mq_unique_tag_to_hwq(tag);
688 } else {
James Smart6a828b02019-01-28 11:14:31 -0800689 idx = phba->sli4_hba.cpu_map[cpu].hdwq;
James Smartace44e42019-01-28 11:14:27 -0800690 }
James Smart5e5b5112019-01-28 11:14:22 -0800691
James Smartc4908502019-01-28 11:14:28 -0800692 lpfc_cmd = lpfc_get_io_buf(phba, ndlp, idx,
693 !phba->cfg_xri_rebalancing);
694 if (!lpfc_cmd) {
695 qp = &phba->sli4_hba.hdwq[idx];
James Smart5e5b5112019-01-28 11:14:22 -0800696 qp->empty_io_bufs++;
James Smart1151e3e2011-02-16 12:39:35 -0500697 return NULL;
James Smart5e5b5112019-01-28 11:14:22 -0800698 }
James Smart2a5b7d62018-07-31 17:23:22 -0700699
James Smart0794d602019-01-28 11:14:19 -0800700 /* Setup key fields in buffer that may have been changed
701 * if other protocols used this buffer.
702 */
703 lpfc_cmd->cur_iocbq.iocb_flag = LPFC_IO_FCP;
704 lpfc_cmd->prot_seg_cnt = 0;
705 lpfc_cmd->seg_cnt = 0;
James Smart0794d602019-01-28 11:14:19 -0800706 lpfc_cmd->timeout = 0;
707 lpfc_cmd->flags = 0;
708 lpfc_cmd->start_time = jiffies;
709 lpfc_cmd->waitq = NULL;
James Smart5e5b5112019-01-28 11:14:22 -0800710 lpfc_cmd->cpu = cpu;
James Smart0794d602019-01-28 11:14:19 -0800711#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
712 lpfc_cmd->prot_data_type = 0;
713#endif
James Smartd79c9e92019-08-14 16:57:09 -0700714 tmp = lpfc_get_cmd_rsp_buf_per_hdwq(phba, lpfc_cmd);
James Smart0ab384a2020-01-27 16:23:02 -0800715 if (!tmp) {
716 lpfc_release_io_buf(phba, lpfc_cmd, lpfc_cmd->hdwq);
James Smartd79c9e92019-08-14 16:57:09 -0700717 return NULL;
James Smart0ab384a2020-01-27 16:23:02 -0800718 }
James Smartd79c9e92019-08-14 16:57:09 -0700719
720 lpfc_cmd->fcp_cmnd = tmp->fcp_cmnd;
721 lpfc_cmd->fcp_rsp = tmp->fcp_rsp;
James Smart0794d602019-01-28 11:14:19 -0800722
723 /*
724 * The first two SGEs are the FCP_CMD and FCP_RSP.
725 * The balance are sg list bdes. Initialize the
726 * first two and leave the rest for queuecommand.
727 */
728 sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
James Smartd79c9e92019-08-14 16:57:09 -0700729 pdma_phys_fcp_cmd = tmp->fcp_cmd_rsp_dma_handle;
James Smart0794d602019-01-28 11:14:19 -0800730 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
731 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
732 sgl->word2 = le32_to_cpu(sgl->word2);
733 bf_set(lpfc_sli4_sge_last, sgl, 0);
734 sgl->word2 = cpu_to_le32(sgl->word2);
735 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_cmnd));
736 sgl++;
737
738 /* Setup the physical region for the FCP RSP */
739 pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
740 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
741 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
742 sgl->word2 = le32_to_cpu(sgl->word2);
743 bf_set(lpfc_sli4_sge_last, sgl, 1);
744 sgl->word2 = cpu_to_le32(sgl->word2);
745 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
746
Dan Carpenter26c724a2018-08-23 16:56:33 +0300747 if (lpfc_ndlp_check_qdepth(phba, ndlp)) {
James Smart2a5b7d62018-07-31 17:23:22 -0700748 atomic_inc(&ndlp->cmd_pending);
749 lpfc_cmd->flags |= LPFC_SBUF_BUMP_QDEPTH;
750 }
James Smarta40fc5f2013-04-17 20:17:40 -0400751 return lpfc_cmd;
James Smart19ca7602010-11-20 23:11:55 -0500752}
753/**
754 * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
755 * @phba: The HBA for which this call is being executed.
Lee Joneseceee002020-11-02 14:23:43 +0000756 * @ndlp: pointer to a node-list data structure.
757 * @cmnd: Pointer to scsi_cmnd data structure.
James Smart19ca7602010-11-20 23:11:55 -0500758 *
759 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
760 * and returns to caller.
761 *
762 * Return codes:
763 * NULL - Error
764 * Pointer to lpfc_scsi_buf - Success
765 **/
James Smartc4908502019-01-28 11:14:28 -0800766static struct lpfc_io_buf*
James Smartace44e42019-01-28 11:14:27 -0800767lpfc_get_scsi_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
768 struct scsi_cmnd *cmnd)
James Smart19ca7602010-11-20 23:11:55 -0500769{
James Smartace44e42019-01-28 11:14:27 -0800770 return phba->lpfc_get_scsi_buf(phba, ndlp, cmnd);
James Smart19ca7602010-11-20 23:11:55 -0500771}
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400772
James Smart9bad7672008-12-04 22:39:02 -0500773/**
Lee Jones0bb87e02021-03-12 09:47:11 +0000774 * lpfc_release_scsi_buf_s3 - Return a scsi buffer back to hba scsi buf list
James Smart9bad7672008-12-04 22:39:02 -0500775 * @phba: The Hba for which this call is being executed.
776 * @psb: The scsi buffer which is being released.
777 *
778 * This routine releases @psb scsi buffer by adding it to tail of @phba
779 * lpfc_scsi_buf_list list.
780 **/
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400781static void
James Smartc4908502019-01-28 11:14:28 -0800782lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_io_buf *psb)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400783{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500784 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500785
James Smarta40fc5f2013-04-17 20:17:40 -0400786 psb->seg_cnt = 0;
James Smarta40fc5f2013-04-17 20:17:40 -0400787 psb->prot_seg_cnt = 0;
788
789 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400790 psb->pCmd = NULL;
James Smart6a485eb2013-04-17 20:19:00 -0400791 psb->cur_iocbq.iocb_flag = LPFC_IO_FCP;
James Smarta40fc5f2013-04-17 20:17:40 -0400792 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list_put);
793 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
dea31012005-04-17 16:05:31 -0500794}
795
James Smart9bad7672008-12-04 22:39:02 -0500796/**
James Smartda0436e2009-05-22 14:51:39 -0400797 * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
798 * @phba: The Hba for which this call is being executed.
799 * @psb: The scsi buffer which is being released.
800 *
James Smart5e5b5112019-01-28 11:14:22 -0800801 * This routine releases @psb scsi buffer by adding it to tail of @hdwq
802 * io_buf_list list. For SLI4 XRI's are tied to the scsi buffer
James Smartda0436e2009-05-22 14:51:39 -0400803 * and cannot be reused for at least RA_TOV amount of time if it was
804 * aborted.
805 **/
806static void
James Smartc4908502019-01-28 11:14:28 -0800807lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_io_buf *psb)
James Smartda0436e2009-05-22 14:51:39 -0400808{
James Smart5e5b5112019-01-28 11:14:22 -0800809 struct lpfc_sli4_hdw_queue *qp;
James Smartda0436e2009-05-22 14:51:39 -0400810 unsigned long iflag = 0;
811
James Smarta40fc5f2013-04-17 20:17:40 -0400812 psb->seg_cnt = 0;
James Smarta40fc5f2013-04-17 20:17:40 -0400813 psb->prot_seg_cnt = 0;
814
James Smart1fbf9742019-01-28 11:14:26 -0800815 qp = psb->hdwq;
James Smart324e1c42019-10-18 14:18:21 -0700816 if (psb->flags & LPFC_SBUF_XBUSY) {
James Smartc00f62e2019-08-14 16:57:11 -0700817 spin_lock_irqsave(&qp->abts_io_buf_list_lock, iflag);
James Smart3e49af92021-05-14 12:55:57 -0700818 if (!phba->cfg_fcp_wait_abts_rsp)
819 psb->pCmd = NULL;
James Smartc00f62e2019-08-14 16:57:11 -0700820 list_add_tail(&psb->list, &qp->lpfc_abts_io_buf_list);
James Smart5e5b5112019-01-28 11:14:22 -0800821 qp->abts_scsi_io_bufs++;
James Smartc00f62e2019-08-14 16:57:11 -0700822 spin_unlock_irqrestore(&qp->abts_io_buf_list_lock, iflag);
James Smartda0436e2009-05-22 14:51:39 -0400823 } else {
James Smartc4908502019-01-28 11:14:28 -0800824 lpfc_release_io_buf(phba, (struct lpfc_io_buf *)psb, qp);
James Smartda0436e2009-05-22 14:51:39 -0400825 }
826}
827
828/**
James Smart3772a992009-05-22 14:50:54 -0400829 * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
830 * @phba: The Hba for which this call is being executed.
831 * @psb: The scsi buffer which is being released.
832 *
833 * This routine releases @psb scsi buffer by adding it to tail of @phba
834 * lpfc_scsi_buf_list list.
835 **/
836static void
James Smartc4908502019-01-28 11:14:28 -0800837lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_io_buf *psb)
James Smart3772a992009-05-22 14:50:54 -0400838{
James Smart2a5b7d62018-07-31 17:23:22 -0700839 if ((psb->flags & LPFC_SBUF_BUMP_QDEPTH) && psb->ndlp)
840 atomic_dec(&psb->ndlp->cmd_pending);
James Smart3772a992009-05-22 14:50:54 -0400841
James Smart2a5b7d62018-07-31 17:23:22 -0700842 psb->flags &= ~LPFC_SBUF_BUMP_QDEPTH;
James Smart3772a992009-05-22 14:50:54 -0400843 phba->lpfc_release_scsi_buf(phba, psb);
844}
845
846/**
James Smartda255e22020-11-15 11:26:42 -0800847 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
848 * @data: A pointer to the immediate command data portion of the IOCB.
849 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
850 *
851 * The routine copies the entire FCP command from @fcp_cmnd to @data while
852 * byte swapping the data to big endian format for transmission on the wire.
853 **/
854static void
855lpfc_fcpcmd_to_iocb(u8 *data, struct fcp_cmnd *fcp_cmnd)
856{
857 int i, j;
858
859 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
860 i += sizeof(uint32_t), j++) {
861 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
862 }
863}
864
865/**
James Smart3772a992009-05-22 14:50:54 -0400866 * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -0500867 * @phba: The Hba for which this call is being executed.
868 * @lpfc_cmd: The scsi buffer which is going to be mapped.
869 *
870 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
James Smart3772a992009-05-22 14:50:54 -0400871 * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -0700872 * through sg elements and format the bde. This routine also initializes all
James Smart3772a992009-05-22 14:50:54 -0400873 * IOCB fields which are dependent on scsi command request buffer.
James Smart9bad7672008-12-04 22:39:02 -0500874 *
875 * Return codes:
876 * 1 - Error
877 * 0 - Success
878 **/
dea31012005-04-17 16:05:31 -0500879static int
James Smartc4908502019-01-28 11:14:28 -0800880lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
dea31012005-04-17 16:05:31 -0500881{
882 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
883 struct scatterlist *sgel = NULL;
884 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smartc4908502019-01-28 11:14:28 -0800885 struct ulp_bde64 *bpl = (struct ulp_bde64 *)lpfc_cmd->dma_sgl;
James Smart0f65ff62010-02-26 14:14:23 -0500886 struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
dea31012005-04-17 16:05:31 -0500887 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart34b02dc2008-08-24 21:49:55 -0400888 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
dea31012005-04-17 16:05:31 -0500889 dma_addr_t physaddr;
James Smart34b02dc2008-08-24 21:49:55 -0400890 uint32_t num_bde = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500891 int nseg, datadir = scsi_cmnd->sc_data_direction;
dea31012005-04-17 16:05:31 -0500892
893 /*
894 * There are three possibilities here - use scatter-gather segment, use
895 * the single mapping, or neither. Start the lpfc command prep by
896 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
897 * data bde entry.
898 */
899 bpl += 2;
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -0700900 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -0500901 /*
902 * The driver stores the segment count returned from pci_map_sg
903 * because this a count of dma-mappings used to map the use_sg
904 * pages. They are not guaranteed to be the same for those
905 * architectures that implement an IOMMU.
906 */
dea31012005-04-17 16:05:31 -0500907
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -0700908 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
909 scsi_sg_count(scsi_cmnd), datadir);
910 if (unlikely(!nseg))
911 return 1;
912
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500913 lpfc_cmd->seg_cnt = nseg;
dea31012005-04-17 16:05:31 -0500914 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
Dick Kennedy372c1872020-06-30 14:50:00 -0700915 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
916 "9064 BLKGRD: %s: Too many sg segments"
917 " from dma_map_sg. Config %d, seg_cnt"
918 " %d\n", __func__, phba->cfg_sg_seg_cnt,
919 lpfc_cmd->seg_cnt);
James Smart5e0e2312019-08-14 16:56:54 -0700920 WARN_ON_ONCE(lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt);
James Smart96f70772013-04-17 20:16:15 -0400921 lpfc_cmd->seg_cnt = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500922 scsi_dma_unmap(scsi_cmnd);
James Smart5e0e2312019-08-14 16:56:54 -0700923 return 2;
dea31012005-04-17 16:05:31 -0500924 }
925
926 /*
927 * The driver established a maximum scatter-gather segment count
928 * during probe that limits the number of sg elements in any
929 * single scsi command. Just run through the seg_cnt and format
930 * the bde's.
James Smart34b02dc2008-08-24 21:49:55 -0400931 * When using SLI-3 the driver will try to fit all the BDEs into
932 * the IOCB. If it can't then the BDEs get added to a BPL as it
933 * does for SLI-2 mode.
dea31012005-04-17 16:05:31 -0500934 */
James Smart34b02dc2008-08-24 21:49:55 -0400935 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
dea31012005-04-17 16:05:31 -0500936 physaddr = sg_dma_address(sgel);
James Smart34b02dc2008-08-24 21:49:55 -0400937 if (phba->sli_rev == 3 &&
James Smarte2a0a9d2008-12-04 22:40:02 -0500938 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
James Smart0f65ff62010-02-26 14:14:23 -0500939 !(iocbq->iocb_flag & DSS_SECURITY_OP) &&
James Smart34b02dc2008-08-24 21:49:55 -0400940 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
941 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
942 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
943 data_bde->addrLow = putPaddrLow(physaddr);
944 data_bde->addrHigh = putPaddrHigh(physaddr);
945 data_bde++;
946 } else {
947 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
948 bpl->tus.f.bdeSize = sg_dma_len(sgel);
949 bpl->tus.w = le32_to_cpu(bpl->tus.w);
950 bpl->addrLow =
951 le32_to_cpu(putPaddrLow(physaddr));
952 bpl->addrHigh =
953 le32_to_cpu(putPaddrHigh(physaddr));
954 bpl++;
955 }
dea31012005-04-17 16:05:31 -0500956 }
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -0700957 }
dea31012005-04-17 16:05:31 -0500958
959 /*
960 * Finish initializing those IOCB fields that are dependent on the
James Smart34b02dc2008-08-24 21:49:55 -0400961 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
962 * explicitly reinitialized and for SLI-3 the extended bde count is
963 * explicitly reinitialized since all iocb memory resources are reused.
dea31012005-04-17 16:05:31 -0500964 */
James Smarte2a0a9d2008-12-04 22:40:02 -0500965 if (phba->sli_rev == 3 &&
James Smart0f65ff62010-02-26 14:14:23 -0500966 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
967 !(iocbq->iocb_flag & DSS_SECURITY_OP)) {
James Smart34b02dc2008-08-24 21:49:55 -0400968 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
969 /*
970 * The extended IOCB format can only fit 3 BDE or a BPL.
971 * This I/O has more than 3 BDE so the 1st data bde will
972 * be a BPL that is filled in here.
973 */
974 physaddr = lpfc_cmd->dma_handle;
975 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
976 data_bde->tus.f.bdeSize = (num_bde *
977 sizeof(struct ulp_bde64));
978 physaddr += (sizeof(struct fcp_cmnd) +
979 sizeof(struct fcp_rsp) +
980 (2 * sizeof(struct ulp_bde64)));
981 data_bde->addrHigh = putPaddrHigh(physaddr);
982 data_bde->addrLow = putPaddrLow(physaddr);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300983 /* ebde count includes the response bde and data bpl */
James Smart34b02dc2008-08-24 21:49:55 -0400984 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
985 } else {
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300986 /* ebde count includes the response bde and data bdes */
James Smart34b02dc2008-08-24 21:49:55 -0400987 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
988 }
989 } else {
990 iocb_cmd->un.fcpi64.bdl.bdeSize =
991 ((num_bde + 2) * sizeof(struct ulp_bde64));
James Smart0f65ff62010-02-26 14:14:23 -0500992 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
James Smart34b02dc2008-08-24 21:49:55 -0400993 }
James Smart09372822008-01-11 01:52:54 -0500994 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
James Smarte2a0a9d2008-12-04 22:40:02 -0500995
996 /*
997 * Due to difference in data length between DIF/non-DIF paths,
998 * we need to set word 4 of IOCB here
999 */
James Smarta257bf92009-04-06 18:48:10 -04001000 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
James Smartda255e22020-11-15 11:26:42 -08001001 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05001002 return 0;
1003}
1004
James Smartf9bb2da2011-10-10 21:34:11 -04001005#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001006
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -07001007/* Return BG_ERR_INIT if error injection is detected by Initiator */
James Smart9a6b09c2012-03-01 22:37:42 -05001008#define BG_ERR_INIT 0x1
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -07001009/* Return BG_ERR_TGT if error injection is detected by Target */
James Smart9a6b09c2012-03-01 22:37:42 -05001010#define BG_ERR_TGT 0x2
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -07001011/* Return BG_ERR_SWAP if swapping CSUM<-->CRC is required for error injection */
James Smart9a6b09c2012-03-01 22:37:42 -05001012#define BG_ERR_SWAP 0x10
Lee Jones0bb87e02021-03-12 09:47:11 +00001013/*
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -07001014 * Return BG_ERR_CHECK if disabling Guard/Ref/App checking is required for
1015 * error injection
Lee Jones0bb87e02021-03-12 09:47:11 +00001016 */
James Smart9a6b09c2012-03-01 22:37:42 -05001017#define BG_ERR_CHECK 0x20
James Smartacd68592012-01-18 16:25:09 -05001018
1019/**
1020 * lpfc_bg_err_inject - Determine if we should inject an error
1021 * @phba: The Hba for which this call is being executed.
James Smartf9bb2da2011-10-10 21:34:11 -04001022 * @sc: The SCSI command to examine
1023 * @reftag: (out) BlockGuard reference tag for transmitted data
1024 * @apptag: (out) BlockGuard application tag for transmitted data
Lee Joneseceee002020-11-02 14:23:43 +00001025 * @new_guard: (in) Value to replace CRC with if needed
James Smartf9bb2da2011-10-10 21:34:11 -04001026 *
James Smart9a6b09c2012-03-01 22:37:42 -05001027 * Returns BG_ERR_* bit mask or 0 if request ignored
James Smartacd68592012-01-18 16:25:09 -05001028 **/
James Smartf9bb2da2011-10-10 21:34:11 -04001029static int
1030lpfc_bg_err_inject(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1031 uint32_t *reftag, uint16_t *apptag, uint32_t new_guard)
1032{
1033 struct scatterlist *sgpe; /* s/g prot entry */
James Smartc4908502019-01-28 11:14:28 -08001034 struct lpfc_io_buf *lpfc_cmd = NULL;
James Smartacd68592012-01-18 16:25:09 -05001035 struct scsi_dif_tuple *src = NULL;
James Smart4ac9b222012-03-01 22:38:29 -05001036 struct lpfc_nodelist *ndlp;
1037 struct lpfc_rport_data *rdata;
James Smartf9bb2da2011-10-10 21:34:11 -04001038 uint32_t op = scsi_get_prot_op(sc);
1039 uint32_t blksize;
1040 uint32_t numblks;
James Smart68a6a662021-03-01 09:18:02 -08001041 u32 lba;
James Smartf9bb2da2011-10-10 21:34:11 -04001042 int rc = 0;
James Smartacd68592012-01-18 16:25:09 -05001043 int blockoff = 0;
James Smartf9bb2da2011-10-10 21:34:11 -04001044
1045 if (op == SCSI_PROT_NORMAL)
1046 return 0;
1047
James Smartacd68592012-01-18 16:25:09 -05001048 sgpe = scsi_prot_sglist(sc);
Bart Van Assche4221c8a2021-08-09 16:03:30 -07001049 lba = t10_pi_ref_tag(scsi_cmd_to_rq(sc));
James Smart68a6a662021-03-01 09:18:02 -08001050 if (lba == LPFC_INVALID_REFTAG)
1051 return 0;
James Smart4ac9b222012-03-01 22:38:29 -05001052
1053 /* First check if we need to match the LBA */
James Smartf9bb2da2011-10-10 21:34:11 -04001054 if (phba->lpfc_injerr_lba != LPFC_INJERR_LBA_OFF) {
1055 blksize = lpfc_cmd_blksize(sc);
1056 numblks = (scsi_bufflen(sc) + blksize - 1) / blksize;
1057
1058 /* Make sure we have the right LBA if one is specified */
James Smart68a6a662021-03-01 09:18:02 -08001059 if (phba->lpfc_injerr_lba < (u64)lba ||
1060 (phba->lpfc_injerr_lba >= (u64)(lba + numblks)))
James Smartf9bb2da2011-10-10 21:34:11 -04001061 return 0;
James Smartacd68592012-01-18 16:25:09 -05001062 if (sgpe) {
James Smart68a6a662021-03-01 09:18:02 -08001063 blockoff = phba->lpfc_injerr_lba - (u64)lba;
James Smartacd68592012-01-18 16:25:09 -05001064 numblks = sg_dma_len(sgpe) /
1065 sizeof(struct scsi_dif_tuple);
1066 if (numblks < blockoff)
1067 blockoff = numblks;
James Smartacd68592012-01-18 16:25:09 -05001068 }
James Smartf9bb2da2011-10-10 21:34:11 -04001069 }
1070
James Smart4ac9b222012-03-01 22:38:29 -05001071 /* Next check if we need to match the remote NPortID or WWPN */
James Smart1ba981f2014-02-20 09:56:45 -05001072 rdata = lpfc_rport_data_from_scsi_device(sc->device);
James Smart4ac9b222012-03-01 22:38:29 -05001073 if (rdata && rdata->pnode) {
1074 ndlp = rdata->pnode;
1075
1076 /* Make sure we have the right NPortID if one is specified */
1077 if (phba->lpfc_injerr_nportid &&
1078 (phba->lpfc_injerr_nportid != ndlp->nlp_DID))
1079 return 0;
1080
1081 /*
1082 * Make sure we have the right WWPN if one is specified.
1083 * wwn[0] should be a non-zero NAA in a good WWPN.
1084 */
1085 if (phba->lpfc_injerr_wwpn.u.wwn[0] &&
1086 (memcmp(&ndlp->nlp_portname, &phba->lpfc_injerr_wwpn,
1087 sizeof(struct lpfc_name)) != 0))
1088 return 0;
1089 }
1090
1091 /* Setup a ptr to the protection data if the SCSI host provides it */
1092 if (sgpe) {
1093 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
1094 src += blockoff;
James Smartc4908502019-01-28 11:14:28 -08001095 lpfc_cmd = (struct lpfc_io_buf *)sc->host_scribble;
James Smart4ac9b222012-03-01 22:38:29 -05001096 }
1097
James Smartf9bb2da2011-10-10 21:34:11 -04001098 /* Should we change the Reference Tag */
1099 if (reftag) {
James Smartacd68592012-01-18 16:25:09 -05001100 if (phba->lpfc_injerr_wref_cnt) {
1101 switch (op) {
1102 case SCSI_PROT_WRITE_PASS:
James Smart9a6b09c2012-03-01 22:37:42 -05001103 if (src) {
1104 /*
1105 * For WRITE_PASS, force the error
1106 * to be sent on the wire. It should
1107 * be detected by the Target.
1108 * If blockoff != 0 error will be
1109 * inserted in middle of the IO.
1110 */
James Smartf9bb2da2011-10-10 21:34:11 -04001111
Dick Kennedy372c1872020-06-30 14:50:00 -07001112 lpfc_printf_log(phba, KERN_ERR,
1113 LOG_TRACE_EVENT,
James Smartacd68592012-01-18 16:25:09 -05001114 "9076 BLKGRD: Injecting reftag error: "
1115 "write lba x%lx + x%x oldrefTag x%x\n",
1116 (unsigned long)lba, blockoff,
James Smart9a6b09c2012-03-01 22:37:42 -05001117 be32_to_cpu(src->ref_tag));
James Smartacd68592012-01-18 16:25:09 -05001118
1119 /*
James Smart9a6b09c2012-03-01 22:37:42 -05001120 * Save the old ref_tag so we can
1121 * restore it on completion.
James Smartacd68592012-01-18 16:25:09 -05001122 */
James Smart9a6b09c2012-03-01 22:37:42 -05001123 if (lpfc_cmd) {
1124 lpfc_cmd->prot_data_type =
1125 LPFC_INJERR_REFTAG;
1126 lpfc_cmd->prot_data_segment =
1127 src;
1128 lpfc_cmd->prot_data =
1129 src->ref_tag;
1130 }
1131 src->ref_tag = cpu_to_be32(0xDEADBEEF);
James Smartacd68592012-01-18 16:25:09 -05001132 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001133 if (phba->lpfc_injerr_wref_cnt == 0) {
1134 phba->lpfc_injerr_nportid = 0;
1135 phba->lpfc_injerr_lba =
1136 LPFC_INJERR_LBA_OFF;
1137 memset(&phba->lpfc_injerr_wwpn,
1138 0, sizeof(struct lpfc_name));
1139 }
James Smart9a6b09c2012-03-01 22:37:42 -05001140 rc = BG_ERR_TGT | BG_ERR_CHECK;
1141
James Smartacd68592012-01-18 16:25:09 -05001142 break;
1143 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001144 fallthrough;
James Smart9a6b09c2012-03-01 22:37:42 -05001145 case SCSI_PROT_WRITE_INSERT:
1146 /*
1147 * For WRITE_INSERT, force the error
1148 * to be sent on the wire. It should be
1149 * detected by the Target.
1150 */
1151 /* DEADBEEF will be the reftag on the wire */
1152 *reftag = 0xDEADBEEF;
1153 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001154 if (phba->lpfc_injerr_wref_cnt == 0) {
1155 phba->lpfc_injerr_nportid = 0;
1156 phba->lpfc_injerr_lba =
1157 LPFC_INJERR_LBA_OFF;
1158 memset(&phba->lpfc_injerr_wwpn,
1159 0, sizeof(struct lpfc_name));
1160 }
James Smart9a6b09c2012-03-01 22:37:42 -05001161 rc = BG_ERR_TGT | BG_ERR_CHECK;
1162
Dick Kennedy372c1872020-06-30 14:50:00 -07001163 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart9a6b09c2012-03-01 22:37:42 -05001164 "9078 BLKGRD: Injecting reftag error: "
1165 "write lba x%lx\n", (unsigned long)lba);
1166 break;
James Smartacd68592012-01-18 16:25:09 -05001167 case SCSI_PROT_WRITE_STRIP:
1168 /*
1169 * For WRITE_STRIP and WRITE_PASS,
1170 * force the error on data
1171 * being copied from SLI-Host to SLI-Port.
1172 */
1173 *reftag = 0xDEADBEEF;
1174 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001175 if (phba->lpfc_injerr_wref_cnt == 0) {
1176 phba->lpfc_injerr_nportid = 0;
1177 phba->lpfc_injerr_lba =
1178 LPFC_INJERR_LBA_OFF;
1179 memset(&phba->lpfc_injerr_wwpn,
1180 0, sizeof(struct lpfc_name));
1181 }
James Smartacd68592012-01-18 16:25:09 -05001182 rc = BG_ERR_INIT;
1183
Dick Kennedy372c1872020-06-30 14:50:00 -07001184 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartacd68592012-01-18 16:25:09 -05001185 "9077 BLKGRD: Injecting reftag error: "
1186 "write lba x%lx\n", (unsigned long)lba);
1187 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001188 }
James Smartacd68592012-01-18 16:25:09 -05001189 }
1190 if (phba->lpfc_injerr_rref_cnt) {
1191 switch (op) {
1192 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001193 case SCSI_PROT_READ_STRIP:
1194 case SCSI_PROT_READ_PASS:
1195 /*
1196 * For READ_STRIP and READ_PASS, force the
1197 * error on data being read off the wire. It
1198 * should force an IO error to the driver.
1199 */
James Smartf9bb2da2011-10-10 21:34:11 -04001200 *reftag = 0xDEADBEEF;
1201 phba->lpfc_injerr_rref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001202 if (phba->lpfc_injerr_rref_cnt == 0) {
1203 phba->lpfc_injerr_nportid = 0;
1204 phba->lpfc_injerr_lba =
1205 LPFC_INJERR_LBA_OFF;
1206 memset(&phba->lpfc_injerr_wwpn,
1207 0, sizeof(struct lpfc_name));
1208 }
James Smartacd68592012-01-18 16:25:09 -05001209 rc = BG_ERR_INIT;
James Smartf9bb2da2011-10-10 21:34:11 -04001210
Dick Kennedy372c1872020-06-30 14:50:00 -07001211 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartacd68592012-01-18 16:25:09 -05001212 "9079 BLKGRD: Injecting reftag error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001213 "read lba x%lx\n", (unsigned long)lba);
James Smartacd68592012-01-18 16:25:09 -05001214 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001215 }
1216 }
1217 }
1218
1219 /* Should we change the Application Tag */
1220 if (apptag) {
James Smartacd68592012-01-18 16:25:09 -05001221 if (phba->lpfc_injerr_wapp_cnt) {
1222 switch (op) {
1223 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001224 if (src) {
James Smart9a6b09c2012-03-01 22:37:42 -05001225 /*
1226 * For WRITE_PASS, force the error
1227 * to be sent on the wire. It should
1228 * be detected by the Target.
1229 * If blockoff != 0 error will be
1230 * inserted in middle of the IO.
1231 */
1232
Dick Kennedy372c1872020-06-30 14:50:00 -07001233 lpfc_printf_log(phba, KERN_ERR,
1234 LOG_TRACE_EVENT,
James Smartacd68592012-01-18 16:25:09 -05001235 "9080 BLKGRD: Injecting apptag error: "
1236 "write lba x%lx + x%x oldappTag x%x\n",
1237 (unsigned long)lba, blockoff,
James Smart9a6b09c2012-03-01 22:37:42 -05001238 be16_to_cpu(src->app_tag));
James Smartacd68592012-01-18 16:25:09 -05001239
1240 /*
James Smart9a6b09c2012-03-01 22:37:42 -05001241 * Save the old app_tag so we can
1242 * restore it on completion.
James Smartacd68592012-01-18 16:25:09 -05001243 */
James Smart9a6b09c2012-03-01 22:37:42 -05001244 if (lpfc_cmd) {
1245 lpfc_cmd->prot_data_type =
1246 LPFC_INJERR_APPTAG;
1247 lpfc_cmd->prot_data_segment =
1248 src;
1249 lpfc_cmd->prot_data =
1250 src->app_tag;
1251 }
1252 src->app_tag = cpu_to_be16(0xDEAD);
James Smartacd68592012-01-18 16:25:09 -05001253 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 Smart9a6b09c2012-03-01 22:37:42 -05001261 rc = BG_ERR_TGT | BG_ERR_CHECK;
James Smartacd68592012-01-18 16:25:09 -05001262 break;
1263 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001264 fallthrough;
James Smart9a6b09c2012-03-01 22:37:42 -05001265 case SCSI_PROT_WRITE_INSERT:
1266 /*
1267 * For WRITE_INSERT, force the
1268 * error to be sent on the wire. It should be
1269 * detected by the Target.
1270 */
1271 /* DEAD will be the apptag on the wire */
1272 *apptag = 0xDEAD;
1273 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001274 if (phba->lpfc_injerr_wapp_cnt == 0) {
1275 phba->lpfc_injerr_nportid = 0;
1276 phba->lpfc_injerr_lba =
1277 LPFC_INJERR_LBA_OFF;
1278 memset(&phba->lpfc_injerr_wwpn,
1279 0, sizeof(struct lpfc_name));
1280 }
James Smart9a6b09c2012-03-01 22:37:42 -05001281 rc = BG_ERR_TGT | BG_ERR_CHECK;
1282
Dick Kennedy372c1872020-06-30 14:50:00 -07001283 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart9a6b09c2012-03-01 22:37:42 -05001284 "0813 BLKGRD: Injecting apptag error: "
1285 "write lba x%lx\n", (unsigned long)lba);
1286 break;
James Smartacd68592012-01-18 16:25:09 -05001287 case SCSI_PROT_WRITE_STRIP:
1288 /*
1289 * For WRITE_STRIP and WRITE_PASS,
1290 * force the error on data
1291 * being copied from SLI-Host to SLI-Port.
1292 */
1293 *apptag = 0xDEAD;
1294 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001295 if (phba->lpfc_injerr_wapp_cnt == 0) {
1296 phba->lpfc_injerr_nportid = 0;
1297 phba->lpfc_injerr_lba =
1298 LPFC_INJERR_LBA_OFF;
1299 memset(&phba->lpfc_injerr_wwpn,
1300 0, sizeof(struct lpfc_name));
1301 }
James Smartacd68592012-01-18 16:25:09 -05001302 rc = BG_ERR_INIT;
1303
Dick Kennedy372c1872020-06-30 14:50:00 -07001304 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartacd68592012-01-18 16:25:09 -05001305 "0812 BLKGRD: Injecting apptag error: "
1306 "write lba x%lx\n", (unsigned long)lba);
1307 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001308 }
James Smartacd68592012-01-18 16:25:09 -05001309 }
1310 if (phba->lpfc_injerr_rapp_cnt) {
1311 switch (op) {
1312 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001313 case SCSI_PROT_READ_STRIP:
1314 case SCSI_PROT_READ_PASS:
1315 /*
1316 * For READ_STRIP and READ_PASS, force the
1317 * error on data being read off the wire. It
1318 * should force an IO error to the driver.
1319 */
James Smartf9bb2da2011-10-10 21:34:11 -04001320 *apptag = 0xDEAD;
1321 phba->lpfc_injerr_rapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001322 if (phba->lpfc_injerr_rapp_cnt == 0) {
1323 phba->lpfc_injerr_nportid = 0;
1324 phba->lpfc_injerr_lba =
1325 LPFC_INJERR_LBA_OFF;
1326 memset(&phba->lpfc_injerr_wwpn,
1327 0, sizeof(struct lpfc_name));
1328 }
James Smartacd68592012-01-18 16:25:09 -05001329 rc = BG_ERR_INIT;
James Smartf9bb2da2011-10-10 21:34:11 -04001330
Dick Kennedy372c1872020-06-30 14:50:00 -07001331 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartacd68592012-01-18 16:25:09 -05001332 "0814 BLKGRD: Injecting apptag error: "
1333 "read lba x%lx\n", (unsigned long)lba);
1334 break;
1335 }
1336 }
1337 }
1338
1339
1340 /* Should we change the Guard Tag */
1341 if (new_guard) {
1342 if (phba->lpfc_injerr_wgrd_cnt) {
1343 switch (op) {
1344 case SCSI_PROT_WRITE_PASS:
James Smart9a6b09c2012-03-01 22:37:42 -05001345 rc = BG_ERR_CHECK;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001346 fallthrough;
James Smartacd68592012-01-18 16:25:09 -05001347
James Smartacd68592012-01-18 16:25:09 -05001348 case SCSI_PROT_WRITE_INSERT:
1349 /*
1350 * For WRITE_INSERT, force the
1351 * error to be sent on the wire. It should be
1352 * detected by the Target.
1353 */
1354 phba->lpfc_injerr_wgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001355 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1356 phba->lpfc_injerr_nportid = 0;
1357 phba->lpfc_injerr_lba =
1358 LPFC_INJERR_LBA_OFF;
1359 memset(&phba->lpfc_injerr_wwpn,
1360 0, sizeof(struct lpfc_name));
1361 }
James Smartacd68592012-01-18 16:25:09 -05001362
James Smart9a6b09c2012-03-01 22:37:42 -05001363 rc |= BG_ERR_TGT | BG_ERR_SWAP;
James Smartacd68592012-01-18 16:25:09 -05001364 /* Signals the caller to swap CRC->CSUM */
1365
Dick Kennedy372c1872020-06-30 14:50:00 -07001366 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartacd68592012-01-18 16:25:09 -05001367 "0817 BLKGRD: Injecting guard error: "
1368 "write lba x%lx\n", (unsigned long)lba);
1369 break;
James Smart9a6b09c2012-03-01 22:37:42 -05001370 case SCSI_PROT_WRITE_STRIP:
1371 /*
1372 * For WRITE_STRIP and WRITE_PASS,
1373 * force the error on data
1374 * being copied from SLI-Host to SLI-Port.
1375 */
1376 phba->lpfc_injerr_wgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001377 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1378 phba->lpfc_injerr_nportid = 0;
1379 phba->lpfc_injerr_lba =
1380 LPFC_INJERR_LBA_OFF;
1381 memset(&phba->lpfc_injerr_wwpn,
1382 0, sizeof(struct lpfc_name));
1383 }
James Smart9a6b09c2012-03-01 22:37:42 -05001384
1385 rc = BG_ERR_INIT | BG_ERR_SWAP;
1386 /* Signals the caller to swap CRC->CSUM */
1387
Dick Kennedy372c1872020-06-30 14:50:00 -07001388 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart9a6b09c2012-03-01 22:37:42 -05001389 "0816 BLKGRD: Injecting guard error: "
1390 "write lba x%lx\n", (unsigned long)lba);
1391 break;
James Smartacd68592012-01-18 16:25:09 -05001392 }
1393 }
1394 if (phba->lpfc_injerr_rgrd_cnt) {
1395 switch (op) {
1396 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001397 case SCSI_PROT_READ_STRIP:
1398 case SCSI_PROT_READ_PASS:
1399 /*
1400 * For READ_STRIP and READ_PASS, force the
1401 * error on data being read off the wire. It
1402 * should force an IO error to the driver.
1403 */
James Smartacd68592012-01-18 16:25:09 -05001404 phba->lpfc_injerr_rgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001405 if (phba->lpfc_injerr_rgrd_cnt == 0) {
1406 phba->lpfc_injerr_nportid = 0;
1407 phba->lpfc_injerr_lba =
1408 LPFC_INJERR_LBA_OFF;
1409 memset(&phba->lpfc_injerr_wwpn,
1410 0, sizeof(struct lpfc_name));
1411 }
James Smartacd68592012-01-18 16:25:09 -05001412
James Smart9a6b09c2012-03-01 22:37:42 -05001413 rc = BG_ERR_INIT | BG_ERR_SWAP;
James Smartacd68592012-01-18 16:25:09 -05001414 /* Signals the caller to swap CRC->CSUM */
1415
Dick Kennedy372c1872020-06-30 14:50:00 -07001416 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartacd68592012-01-18 16:25:09 -05001417 "0818 BLKGRD: Injecting guard error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001418 "read lba x%lx\n", (unsigned long)lba);
1419 }
1420 }
1421 }
1422
James Smartf9bb2da2011-10-10 21:34:11 -04001423 return rc;
1424}
1425#endif
1426
James Smartacd68592012-01-18 16:25:09 -05001427/**
1428 * lpfc_sc_to_bg_opcodes - Determine the BlockGuard opcodes to be used with
1429 * the specified SCSI command.
1430 * @phba: The Hba for which this call is being executed.
James Smart6c8eea52010-04-06 14:49:53 -04001431 * @sc: The SCSI command to examine
Lee Joneseceee002020-11-02 14:23:43 +00001432 * @txop: (out) BlockGuard operation for transmitted data
1433 * @rxop: (out) BlockGuard operation for received data
James Smart6c8eea52010-04-06 14:49:53 -04001434 *
1435 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1436 *
James Smartacd68592012-01-18 16:25:09 -05001437 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001438static int
James Smart6c8eea52010-04-06 14:49:53 -04001439lpfc_sc_to_bg_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1440 uint8_t *txop, uint8_t *rxop)
James Smarte2a0a9d2008-12-04 22:40:02 -05001441{
James Smart6c8eea52010-04-06 14:49:53 -04001442 uint8_t ret = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05001443
James Smart9c6aa9d2013-05-31 17:03:39 -04001444 if (lpfc_cmd_guard_csum(sc)) {
James Smarte2a0a9d2008-12-04 22:40:02 -05001445 switch (scsi_get_prot_op(sc)) {
1446 case SCSI_PROT_READ_INSERT:
1447 case SCSI_PROT_WRITE_STRIP:
James Smart6c8eea52010-04-06 14:49:53 -04001448 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001449 *txop = BG_OP_IN_CSUM_OUT_NODIF;
James Smarte2a0a9d2008-12-04 22:40:02 -05001450 break;
1451
1452 case SCSI_PROT_READ_STRIP:
1453 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001454 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001455 *txop = BG_OP_IN_NODIF_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001456 break;
1457
Martin K. Petersenc6af4042009-09-18 17:32:59 -04001458 case SCSI_PROT_READ_PASS:
1459 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001460 *rxop = BG_OP_IN_CRC_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001461 *txop = BG_OP_IN_CSUM_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001462 break;
1463
James Smarte2a0a9d2008-12-04 22:40:02 -05001464 case SCSI_PROT_NORMAL:
1465 default:
Dick Kennedy372c1872020-06-30 14:50:00 -07001466 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart7c56b9f2011-07-22 18:36:25 -04001467 "9063 BLKGRD: Bad op/guard:%d/IP combination\n",
1468 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001469 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001470 break;
1471
1472 }
James Smart7c56b9f2011-07-22 18:36:25 -04001473 } else {
James Smarte2a0a9d2008-12-04 22:40:02 -05001474 switch (scsi_get_prot_op(sc)) {
1475 case SCSI_PROT_READ_STRIP:
1476 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001477 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001478 *txop = BG_OP_IN_NODIF_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001479 break;
1480
1481 case SCSI_PROT_READ_PASS:
1482 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001483 *rxop = BG_OP_IN_CRC_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001484 *txop = BG_OP_IN_CRC_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001485 break;
1486
James Smarte2a0a9d2008-12-04 22:40:02 -05001487 case SCSI_PROT_READ_INSERT:
1488 case SCSI_PROT_WRITE_STRIP:
James Smart7c56b9f2011-07-22 18:36:25 -04001489 *rxop = BG_OP_IN_NODIF_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001490 *txop = BG_OP_IN_CRC_OUT_NODIF;
James Smart7c56b9f2011-07-22 18:36:25 -04001491 break;
1492
James Smarte2a0a9d2008-12-04 22:40:02 -05001493 case SCSI_PROT_NORMAL:
1494 default:
Dick Kennedy372c1872020-06-30 14:50:00 -07001495 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart7c56b9f2011-07-22 18:36:25 -04001496 "9075 BLKGRD: Bad op/guard:%d/CRC combination\n",
1497 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001498 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001499 break;
1500 }
James Smarte2a0a9d2008-12-04 22:40:02 -05001501 }
1502
James Smart6c8eea52010-04-06 14:49:53 -04001503 return ret;
James Smarte2a0a9d2008-12-04 22:40:02 -05001504}
1505
James Smartacd68592012-01-18 16:25:09 -05001506#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1507/**
1508 * lpfc_bg_err_opcodes - reDetermine the BlockGuard opcodes to be used with
1509 * the specified SCSI command in order to force a guard tag error.
1510 * @phba: The Hba for which this call is being executed.
1511 * @sc: The SCSI command to examine
Lee Joneseceee002020-11-02 14:23:43 +00001512 * @txop: (out) BlockGuard operation for transmitted data
1513 * @rxop: (out) BlockGuard operation for received data
James Smartacd68592012-01-18 16:25:09 -05001514 *
1515 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1516 *
1517 **/
1518static int
1519lpfc_bg_err_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1520 uint8_t *txop, uint8_t *rxop)
1521{
James Smartacd68592012-01-18 16:25:09 -05001522 uint8_t ret = 0;
1523
James Smart9c6aa9d2013-05-31 17:03:39 -04001524 if (lpfc_cmd_guard_csum(sc)) {
James Smartacd68592012-01-18 16:25:09 -05001525 switch (scsi_get_prot_op(sc)) {
1526 case SCSI_PROT_READ_INSERT:
1527 case SCSI_PROT_WRITE_STRIP:
James Smartacd68592012-01-18 16:25:09 -05001528 *rxop = BG_OP_IN_NODIF_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001529 *txop = BG_OP_IN_CRC_OUT_NODIF;
James Smartacd68592012-01-18 16:25:09 -05001530 break;
1531
1532 case SCSI_PROT_READ_STRIP:
1533 case SCSI_PROT_WRITE_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001534 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001535 *txop = BG_OP_IN_NODIF_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001536 break;
1537
1538 case SCSI_PROT_READ_PASS:
1539 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001540 *rxop = BG_OP_IN_CSUM_OUT_CRC;
James Smart9a6b09c2012-03-01 22:37:42 -05001541 *txop = BG_OP_IN_CRC_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001542 break;
1543
1544 case SCSI_PROT_NORMAL:
1545 default:
1546 break;
1547
1548 }
1549 } else {
1550 switch (scsi_get_prot_op(sc)) {
1551 case SCSI_PROT_READ_STRIP:
1552 case SCSI_PROT_WRITE_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001553 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001554 *txop = BG_OP_IN_NODIF_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001555 break;
1556
1557 case SCSI_PROT_READ_PASS:
1558 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001559 *rxop = BG_OP_IN_CSUM_OUT_CSUM;
James Smart9a6b09c2012-03-01 22:37:42 -05001560 *txop = BG_OP_IN_CSUM_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001561 break;
1562
1563 case SCSI_PROT_READ_INSERT:
1564 case SCSI_PROT_WRITE_STRIP:
James Smartacd68592012-01-18 16:25:09 -05001565 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001566 *txop = BG_OP_IN_CSUM_OUT_NODIF;
James Smartacd68592012-01-18 16:25:09 -05001567 break;
1568
1569 case SCSI_PROT_NORMAL:
1570 default:
1571 break;
1572 }
1573 }
1574
1575 return ret;
1576}
1577#endif
1578
1579/**
1580 * lpfc_bg_setup_bpl - Setup BlockGuard BPL with no protection data
1581 * @phba: The Hba for which this call is being executed.
1582 * @sc: pointer to scsi command we're working on
1583 * @bpl: pointer to buffer list for protection groups
Lee Joneseceee002020-11-02 14:23:43 +00001584 * @datasegcnt: number of segments of data that have been dma mapped
James Smartacd68592012-01-18 16:25:09 -05001585 *
1586 * This function sets up BPL buffer list for protection groups of
James Smarte2a0a9d2008-12-04 22:40:02 -05001587 * type LPFC_PG_TYPE_NO_DIF
1588 *
1589 * This is usually used when the HBA is instructed to generate
1590 * DIFs and insert them into data stream (or strip DIF from
1591 * incoming data stream)
1592 *
1593 * The buffer list consists of just one protection group described
1594 * below:
1595 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001596 * start of prot group --> | PDE_5 |
1597 * +-------------------------+
1598 * | PDE_6 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001599 * +-------------------------+
1600 * | Data BDE |
1601 * +-------------------------+
1602 * |more Data BDE's ... (opt)|
1603 * +-------------------------+
1604 *
James Smarte2a0a9d2008-12-04 22:40:02 -05001605 *
1606 * Note: Data s/g buffers have been dma mapped
James Smartacd68592012-01-18 16:25:09 -05001607 *
1608 * Returns the number of BDEs added to the BPL.
1609 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001610static int
1611lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1612 struct ulp_bde64 *bpl, int datasegcnt)
1613{
1614 struct scatterlist *sgde = NULL; /* s/g data entry */
James Smart6c8eea52010-04-06 14:49:53 -04001615 struct lpfc_pde5 *pde5 = NULL;
1616 struct lpfc_pde6 *pde6 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05001617 dma_addr_t physaddr;
James Smart6c8eea52010-04-06 14:49:53 -04001618 int i = 0, num_bde = 0, status;
James Smarte2a0a9d2008-12-04 22:40:02 -05001619 int datadir = sc->sc_data_direction;
James Smart0829a192012-05-09 21:18:12 -04001620#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001621 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04001622#endif
James Smartacd68592012-01-18 16:25:09 -05001623 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001624 uint32_t reftag;
James Smart6c8eea52010-04-06 14:49:53 -04001625 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05001626
James Smart6c8eea52010-04-06 14:49:53 -04001627 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1628 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05001629 goto out;
1630
James Smart6c8eea52010-04-06 14:49:53 -04001631 /* extract some info from the scsi command for pde*/
Bart Van Assche4221c8a2021-08-09 16:03:30 -07001632 reftag = t10_pi_ref_tag(scsi_cmd_to_rq(sc));
James Smart68a6a662021-03-01 09:18:02 -08001633 if (reftag == LPFC_INVALID_REFTAG)
1634 goto out;
James Smarte2a0a9d2008-12-04 22:40:02 -05001635
James Smartf9bb2da2011-10-10 21:34:11 -04001636#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05001637 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05001638 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05001639 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05001640 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05001641 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05001642 checking = 0;
1643 }
James Smartf9bb2da2011-10-10 21:34:11 -04001644#endif
1645
James Smart6c8eea52010-04-06 14:49:53 -04001646 /* setup PDE5 with what we have */
1647 pde5 = (struct lpfc_pde5 *) bpl;
1648 memset(pde5, 0, sizeof(struct lpfc_pde5));
1649 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05001650
James Smartbc739052010-08-04 16:11:18 -04001651 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04001652 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04001653 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04001654
James Smart6c8eea52010-04-06 14:49:53 -04001655 /* advance bpl and increment bde count */
1656 num_bde++;
1657 bpl++;
1658 pde6 = (struct lpfc_pde6 *) bpl;
1659
1660 /* setup PDE6 with the rest of the info */
1661 memset(pde6, 0, sizeof(struct lpfc_pde6));
1662 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
1663 bf_set(pde6_optx, pde6, txop);
1664 bf_set(pde6_oprx, pde6, rxop);
James Smarta6887e22013-04-17 20:18:07 -04001665
1666 /*
1667 * We only need to check the data on READs, for WRITEs
1668 * protection data is automatically generated, not checked.
1669 */
James Smart6c8eea52010-04-06 14:49:53 -04001670 if (datadir == DMA_FROM_DEVICE) {
James Smart9c6aa9d2013-05-31 17:03:39 -04001671 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
James Smarta6887e22013-04-17 20:18:07 -04001672 bf_set(pde6_ce, pde6, checking);
1673 else
1674 bf_set(pde6_ce, pde6, 0);
1675
James Smart9c6aa9d2013-05-31 17:03:39 -04001676 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04001677 bf_set(pde6_re, pde6, checking);
1678 else
1679 bf_set(pde6_re, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04001680 }
1681 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04001682 bf_set(pde6_ae, pde6, 0);
1683 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04001684
James Smartbc739052010-08-04 16:11:18 -04001685 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04001686 pde6->word0 = cpu_to_le32(pde6->word0);
1687 pde6->word1 = cpu_to_le32(pde6->word1);
1688 pde6->word2 = cpu_to_le32(pde6->word2);
1689
James Smart6c8eea52010-04-06 14:49:53 -04001690 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05001691 num_bde++;
1692 bpl++;
1693
1694 /* assumption: caller has already run dma_map_sg on command data */
1695 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
1696 physaddr = sg_dma_address(sgde);
1697 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
1698 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1699 bpl->tus.f.bdeSize = sg_dma_len(sgde);
1700 if (datadir == DMA_TO_DEVICE)
1701 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1702 else
1703 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1704 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1705 bpl++;
1706 num_bde++;
1707 }
1708
1709out:
1710 return num_bde;
1711}
1712
James Smartacd68592012-01-18 16:25:09 -05001713/**
1714 * lpfc_bg_setup_bpl_prot - Setup BlockGuard BPL with protection data
1715 * @phba: The Hba for which this call is being executed.
1716 * @sc: pointer to scsi command we're working on
1717 * @bpl: pointer to buffer list for protection groups
1718 * @datacnt: number of segments of data that have been dma mapped
1719 * @protcnt: number of segment of protection data that have been dma mapped
1720 *
1721 * This function sets up BPL buffer list for protection groups of
1722 * type LPFC_PG_TYPE_DIF
James Smarte2a0a9d2008-12-04 22:40:02 -05001723 *
1724 * This is usually used when DIFs are in their own buffers,
1725 * separate from the data. The HBA can then by instructed
1726 * to place the DIFs in the outgoing stream. For read operations,
1727 * The HBA could extract the DIFs and place it in DIF buffers.
1728 *
1729 * The buffer list for this type consists of one or more of the
1730 * protection groups described below:
1731 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001732 * start of first prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001733 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001734 * | PDE_6 |
1735 * +-------------------------+
1736 * | PDE_7 (Prot BDE) |
James Smarte2a0a9d2008-12-04 22:40:02 -05001737 * +-------------------------+
1738 * | Data BDE |
1739 * +-------------------------+
1740 * |more Data BDE's ... (opt)|
1741 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001742 * start of new prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001743 * +-------------------------+
1744 * | ... |
1745 * +-------------------------+
1746 *
James Smarte2a0a9d2008-12-04 22:40:02 -05001747 * Note: It is assumed that both data and protection s/g buffers have been
1748 * mapped for DMA
James Smartacd68592012-01-18 16:25:09 -05001749 *
1750 * Returns the number of BDEs added to the BPL.
1751 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001752static int
1753lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1754 struct ulp_bde64 *bpl, int datacnt, int protcnt)
1755{
1756 struct scatterlist *sgde = NULL; /* s/g data entry */
1757 struct scatterlist *sgpe = NULL; /* s/g prot entry */
James Smart6c8eea52010-04-06 14:49:53 -04001758 struct lpfc_pde5 *pde5 = NULL;
1759 struct lpfc_pde6 *pde6 = NULL;
James Smart7f860592011-03-11 16:05:52 -05001760 struct lpfc_pde7 *pde7 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05001761 dma_addr_t dataphysaddr, protphysaddr;
1762 unsigned short curr_data = 0, curr_prot = 0;
James Smart7f860592011-03-11 16:05:52 -05001763 unsigned int split_offset;
1764 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
James Smarte2a0a9d2008-12-04 22:40:02 -05001765 unsigned int protgrp_blks, protgrp_bytes;
1766 unsigned int remainder, subtotal;
James Smart6c8eea52010-04-06 14:49:53 -04001767 int status;
James Smarte2a0a9d2008-12-04 22:40:02 -05001768 int datadir = sc->sc_data_direction;
1769 unsigned char pgdone = 0, alldone = 0;
1770 unsigned blksize;
James Smart0829a192012-05-09 21:18:12 -04001771#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001772 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04001773#endif
James Smartacd68592012-01-18 16:25:09 -05001774 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001775 uint32_t reftag;
James Smart6c8eea52010-04-06 14:49:53 -04001776 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05001777 int num_bde = 0;
1778
1779 sgpe = scsi_prot_sglist(sc);
1780 sgde = scsi_sglist(sc);
1781
1782 if (!sgpe || !sgde) {
Dick Kennedy372c1872020-06-30 14:50:00 -07001783 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart32350662019-08-14 16:57:06 -07001784 "9020 Invalid s/g entry: data=x%px prot=x%px\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001785 sgpe, sgde);
1786 return 0;
1787 }
1788
James Smart6c8eea52010-04-06 14:49:53 -04001789 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1790 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05001791 goto out;
1792
James Smart6c8eea52010-04-06 14:49:53 -04001793 /* extract some info from the scsi command */
James Smarte2a0a9d2008-12-04 22:40:02 -05001794 blksize = lpfc_cmd_blksize(sc);
Bart Van Assche4221c8a2021-08-09 16:03:30 -07001795 reftag = t10_pi_ref_tag(scsi_cmd_to_rq(sc));
James Smart68a6a662021-03-01 09:18:02 -08001796 if (reftag == LPFC_INVALID_REFTAG)
1797 goto out;
James Smarte2a0a9d2008-12-04 22:40:02 -05001798
James Smartf9bb2da2011-10-10 21:34:11 -04001799#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05001800 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05001801 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05001802 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05001803 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05001804 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05001805 checking = 0;
1806 }
James Smartf9bb2da2011-10-10 21:34:11 -04001807#endif
1808
James Smarte2a0a9d2008-12-04 22:40:02 -05001809 split_offset = 0;
1810 do {
James Smart96f70772013-04-17 20:16:15 -04001811 /* Check to see if we ran out of space */
1812 if (num_bde >= (phba->cfg_total_seg_cnt - 2))
1813 return num_bde + 3;
1814
James Smart6c8eea52010-04-06 14:49:53 -04001815 /* setup PDE5 with what we have */
1816 pde5 = (struct lpfc_pde5 *) bpl;
1817 memset(pde5, 0, sizeof(struct lpfc_pde5));
1818 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05001819
James Smartbc739052010-08-04 16:11:18 -04001820 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04001821 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04001822 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04001823
James Smart6c8eea52010-04-06 14:49:53 -04001824 /* advance bpl and increment bde count */
1825 num_bde++;
1826 bpl++;
1827 pde6 = (struct lpfc_pde6 *) bpl;
James Smarte2a0a9d2008-12-04 22:40:02 -05001828
James Smart6c8eea52010-04-06 14:49:53 -04001829 /* setup PDE6 with the rest of the info */
1830 memset(pde6, 0, sizeof(struct lpfc_pde6));
1831 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
1832 bf_set(pde6_optx, pde6, txop);
1833 bf_set(pde6_oprx, pde6, rxop);
James Smarta6887e22013-04-17 20:18:07 -04001834
James Smart9c6aa9d2013-05-31 17:03:39 -04001835 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
James Smarta6887e22013-04-17 20:18:07 -04001836 bf_set(pde6_ce, pde6, checking);
1837 else
1838 bf_set(pde6_ce, pde6, 0);
1839
James Smart9c6aa9d2013-05-31 17:03:39 -04001840 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04001841 bf_set(pde6_re, pde6, checking);
1842 else
1843 bf_set(pde6_re, pde6, 0);
1844
James Smart6c8eea52010-04-06 14:49:53 -04001845 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04001846 bf_set(pde6_ae, pde6, 0);
1847 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04001848
James Smartbc739052010-08-04 16:11:18 -04001849 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04001850 pde6->word0 = cpu_to_le32(pde6->word0);
1851 pde6->word1 = cpu_to_le32(pde6->word1);
1852 pde6->word2 = cpu_to_le32(pde6->word2);
1853
James Smart6c8eea52010-04-06 14:49:53 -04001854 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05001855 num_bde++;
1856 bpl++;
1857
1858 /* setup the first BDE that points to protection buffer */
James Smart7f860592011-03-11 16:05:52 -05001859 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
1860 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
James Smarte2a0a9d2008-12-04 22:40:02 -05001861
James Smarte2a0a9d2008-12-04 22:40:02 -05001862 /* must be integer multiple of the DIF block length */
1863 BUG_ON(protgroup_len % 8);
1864
James Smart7f860592011-03-11 16:05:52 -05001865 pde7 = (struct lpfc_pde7 *) bpl;
1866 memset(pde7, 0, sizeof(struct lpfc_pde7));
1867 bf_set(pde7_type, pde7, LPFC_PDE7_DESCRIPTOR);
1868
James Smart7c56b9f2011-07-22 18:36:25 -04001869 pde7->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
1870 pde7->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
James Smart7f860592011-03-11 16:05:52 -05001871
James Smarte2a0a9d2008-12-04 22:40:02 -05001872 protgrp_blks = protgroup_len / 8;
1873 protgrp_bytes = protgrp_blks * blksize;
1874
James Smart7f860592011-03-11 16:05:52 -05001875 /* check if this pde is crossing the 4K boundary; if so split */
1876 if ((pde7->addrLow & 0xfff) + protgroup_len > 0x1000) {
1877 protgroup_remainder = 0x1000 - (pde7->addrLow & 0xfff);
1878 protgroup_offset += protgroup_remainder;
1879 protgrp_blks = protgroup_remainder / 8;
James Smart7c56b9f2011-07-22 18:36:25 -04001880 protgrp_bytes = protgrp_blks * blksize;
James Smart7f860592011-03-11 16:05:52 -05001881 } else {
1882 protgroup_offset = 0;
1883 curr_prot++;
1884 }
James Smarte2a0a9d2008-12-04 22:40:02 -05001885
James Smarte2a0a9d2008-12-04 22:40:02 -05001886 num_bde++;
1887
1888 /* setup BDE's for data blocks associated with DIF data */
1889 pgdone = 0;
1890 subtotal = 0; /* total bytes processed for current prot grp */
1891 while (!pgdone) {
James Smart96f70772013-04-17 20:16:15 -04001892 /* Check to see if we ran out of space */
1893 if (num_bde >= phba->cfg_total_seg_cnt)
1894 return num_bde + 1;
1895
James Smarte2a0a9d2008-12-04 22:40:02 -05001896 if (!sgde) {
Dick Kennedy372c1872020-06-30 14:50:00 -07001897 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6a9c52c2009-10-02 15:16:51 -04001898 "9065 BLKGRD:%s Invalid data segment\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001899 __func__);
1900 return 0;
1901 }
1902 bpl++;
1903 dataphysaddr = sg_dma_address(sgde) + split_offset;
1904 bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
1905 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
1906
1907 remainder = sg_dma_len(sgde) - split_offset;
1908
1909 if ((subtotal + remainder) <= protgrp_bytes) {
1910 /* we can use this whole buffer */
1911 bpl->tus.f.bdeSize = remainder;
1912 split_offset = 0;
1913
1914 if ((subtotal + remainder) == protgrp_bytes)
1915 pgdone = 1;
1916 } else {
1917 /* must split this buffer with next prot grp */
1918 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
1919 split_offset += bpl->tus.f.bdeSize;
1920 }
1921
1922 subtotal += bpl->tus.f.bdeSize;
1923
1924 if (datadir == DMA_TO_DEVICE)
1925 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1926 else
1927 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1928 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1929
1930 num_bde++;
1931 curr_data++;
1932
1933 if (split_offset)
1934 break;
1935
1936 /* Move to the next s/g segment if possible */
1937 sgde = sg_next(sgde);
James Smart6c8eea52010-04-06 14:49:53 -04001938
James Smarte2a0a9d2008-12-04 22:40:02 -05001939 }
1940
James Smart7f860592011-03-11 16:05:52 -05001941 if (protgroup_offset) {
1942 /* update the reference tag */
1943 reftag += protgrp_blks;
1944 bpl++;
1945 continue;
1946 }
1947
James Smarte2a0a9d2008-12-04 22:40:02 -05001948 /* are we done ? */
1949 if (curr_prot == protcnt) {
1950 alldone = 1;
1951 } else if (curr_prot < protcnt) {
1952 /* advance to next prot buffer */
1953 sgpe = sg_next(sgpe);
1954 bpl++;
1955
1956 /* update the reference tag */
1957 reftag += protgrp_blks;
1958 } else {
1959 /* if we're here, we have a bug */
Dick Kennedy372c1872020-06-30 14:50:00 -07001960 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1961 "9054 BLKGRD: bug in %s\n", __func__);
James Smarte2a0a9d2008-12-04 22:40:02 -05001962 }
1963
1964 } while (!alldone);
James Smarte2a0a9d2008-12-04 22:40:02 -05001965out:
1966
James Smarte2a0a9d2008-12-04 22:40:02 -05001967 return num_bde;
1968}
James Smart7f860592011-03-11 16:05:52 -05001969
James Smartacd68592012-01-18 16:25:09 -05001970/**
1971 * lpfc_bg_setup_sgl - Setup BlockGuard SGL with no protection data
1972 * @phba: The Hba for which this call is being executed.
1973 * @sc: pointer to scsi command we're working on
1974 * @sgl: pointer to buffer list for protection groups
Lee Joneseceee002020-11-02 14:23:43 +00001975 * @datasegcnt: number of segments of data that have been dma mapped
1976 * @lpfc_cmd: lpfc scsi command object pointer.
James Smartacd68592012-01-18 16:25:09 -05001977 *
1978 * This function sets up SGL buffer list for protection groups of
1979 * type LPFC_PG_TYPE_NO_DIF
1980 *
1981 * This is usually used when the HBA is instructed to generate
1982 * DIFs and insert them into data stream (or strip DIF from
1983 * incoming data stream)
1984 *
1985 * The buffer list consists of just one protection group described
1986 * below:
1987 * +-------------------------+
1988 * start of prot group --> | DI_SEED |
1989 * +-------------------------+
1990 * | Data SGE |
1991 * +-------------------------+
1992 * |more Data SGE's ... (opt)|
1993 * +-------------------------+
1994 *
1995 *
1996 * Note: Data s/g buffers have been dma mapped
1997 *
1998 * Returns the number of SGEs added to the SGL.
1999 **/
2000static int
2001lpfc_bg_setup_sgl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
James Smartd79c9e92019-08-14 16:57:09 -07002002 struct sli4_sge *sgl, int datasegcnt,
2003 struct lpfc_io_buf *lpfc_cmd)
James Smartacd68592012-01-18 16:25:09 -05002004{
2005 struct scatterlist *sgde = NULL; /* s/g data entry */
2006 struct sli4_sge_diseed *diseed = NULL;
2007 dma_addr_t physaddr;
2008 int i = 0, num_sge = 0, status;
James Smartacd68592012-01-18 16:25:09 -05002009 uint32_t reftag;
James Smartacd68592012-01-18 16:25:09 -05002010 uint8_t txop, rxop;
James Smart0829a192012-05-09 21:18:12 -04002011#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002012 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002013#endif
James Smartacd68592012-01-18 16:25:09 -05002014 uint32_t checking = 1;
2015 uint32_t dma_len;
2016 uint32_t dma_offset = 0;
James Smartd79c9e92019-08-14 16:57:09 -07002017 struct sli4_hybrid_sgl *sgl_xtra = NULL;
2018 int j;
2019 bool lsp_just_set = false;
James Smartacd68592012-01-18 16:25:09 -05002020
2021 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2022 if (status)
2023 goto out;
2024
2025 /* extract some info from the scsi command for pde*/
Bart Van Assche4221c8a2021-08-09 16:03:30 -07002026 reftag = t10_pi_ref_tag(scsi_cmd_to_rq(sc));
James Smart68a6a662021-03-01 09:18:02 -08002027 if (reftag == LPFC_INVALID_REFTAG)
2028 goto out;
James Smartacd68592012-01-18 16:25:09 -05002029
2030#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002031 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002032 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002033 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002034 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002035 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002036 checking = 0;
2037 }
2038#endif
2039
2040 /* setup DISEED with what we have */
2041 diseed = (struct sli4_sge_diseed *) sgl;
2042 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2043 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2044
2045 /* Endianness conversion if necessary */
2046 diseed->ref_tag = cpu_to_le32(reftag);
2047 diseed->ref_tag_tran = diseed->ref_tag;
2048
James Smarta6887e22013-04-17 20:18:07 -04002049 /*
2050 * We only need to check the data on READs, for WRITEs
2051 * protection data is automatically generated, not checked.
2052 */
2053 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
James Smart9c6aa9d2013-05-31 17:03:39 -04002054 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
James Smarta6887e22013-04-17 20:18:07 -04002055 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2056 else
2057 bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2058
James Smart9c6aa9d2013-05-31 17:03:39 -04002059 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04002060 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2061 else
2062 bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2063 }
2064
James Smartacd68592012-01-18 16:25:09 -05002065 /* setup DISEED with the rest of the info */
2066 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2067 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002068
James Smartacd68592012-01-18 16:25:09 -05002069 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2070 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2071
2072 /* Endianness conversion if necessary for DISEED */
2073 diseed->word2 = cpu_to_le32(diseed->word2);
2074 diseed->word3 = cpu_to_le32(diseed->word3);
2075
2076 /* advance bpl and increment sge count */
2077 num_sge++;
2078 sgl++;
2079
2080 /* assumption: caller has already run dma_map_sg on command data */
James Smartd79c9e92019-08-14 16:57:09 -07002081 sgde = scsi_sglist(sc);
2082 j = 3;
2083 for (i = 0; i < datasegcnt; i++) {
2084 /* clear it */
2085 sgl->word2 = 0;
James Smartacd68592012-01-18 16:25:09 -05002086
James Smartd79c9e92019-08-14 16:57:09 -07002087 /* do we need to expand the segment */
2088 if (!lsp_just_set && !((j + 1) % phba->border_sge_num) &&
2089 ((datasegcnt - 1) != i)) {
2090 /* set LSP type */
2091 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_LSP);
James Smartacd68592012-01-18 16:25:09 -05002092
James Smartd79c9e92019-08-14 16:57:09 -07002093 sgl_xtra = lpfc_get_sgl_per_hdwq(phba, lpfc_cmd);
2094
2095 if (unlikely(!sgl_xtra)) {
2096 lpfc_cmd->seg_cnt = 0;
2097 return 0;
2098 }
2099 sgl->addr_lo = cpu_to_le32(putPaddrLow(
2100 sgl_xtra->dma_phys_sgl));
2101 sgl->addr_hi = cpu_to_le32(putPaddrHigh(
2102 sgl_xtra->dma_phys_sgl));
2103
2104 } else {
2105 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2106 }
2107
2108 if (!(bf_get(lpfc_sli4_sge_type, sgl) & LPFC_SGE_TYPE_LSP)) {
2109 if ((datasegcnt - 1) == i)
2110 bf_set(lpfc_sli4_sge_last, sgl, 1);
2111 physaddr = sg_dma_address(sgde);
2112 dma_len = sg_dma_len(sgde);
2113 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
2114 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
2115
2116 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2117 sgl->word2 = cpu_to_le32(sgl->word2);
2118 sgl->sge_len = cpu_to_le32(dma_len);
2119
2120 dma_offset += dma_len;
2121 sgde = sg_next(sgde);
2122
2123 sgl++;
2124 num_sge++;
2125 lsp_just_set = false;
2126
2127 } else {
2128 sgl->word2 = cpu_to_le32(sgl->word2);
2129 sgl->sge_len = cpu_to_le32(phba->cfg_sg_dma_buf_size);
2130
2131 sgl = (struct sli4_sge *)sgl_xtra->dma_sgl;
2132 i = i - 1;
2133
2134 lsp_just_set = true;
2135 }
2136
2137 j++;
2138
James Smartacd68592012-01-18 16:25:09 -05002139 }
2140
2141out:
2142 return num_sge;
2143}
2144
2145/**
2146 * lpfc_bg_setup_sgl_prot - Setup BlockGuard SGL with protection data
2147 * @phba: The Hba for which this call is being executed.
2148 * @sc: pointer to scsi command we're working on
2149 * @sgl: pointer to buffer list for protection groups
2150 * @datacnt: number of segments of data that have been dma mapped
2151 * @protcnt: number of segment of protection data that have been dma mapped
Lee Joneseceee002020-11-02 14:23:43 +00002152 * @lpfc_cmd: lpfc scsi command object pointer.
James Smartacd68592012-01-18 16:25:09 -05002153 *
2154 * This function sets up SGL buffer list for protection groups of
2155 * type LPFC_PG_TYPE_DIF
2156 *
2157 * This is usually used when DIFs are in their own buffers,
2158 * separate from the data. The HBA can then by instructed
2159 * to place the DIFs in the outgoing stream. For read operations,
2160 * The HBA could extract the DIFs and place it in DIF buffers.
2161 *
2162 * The buffer list for this type consists of one or more of the
2163 * protection groups described below:
2164 * +-------------------------+
2165 * start of first prot group --> | DISEED |
2166 * +-------------------------+
2167 * | DIF (Prot SGE) |
2168 * +-------------------------+
2169 * | Data SGE |
2170 * +-------------------------+
2171 * |more Data SGE's ... (opt)|
2172 * +-------------------------+
2173 * start of new prot group --> | DISEED |
2174 * +-------------------------+
2175 * | ... |
2176 * +-------------------------+
2177 *
2178 * Note: It is assumed that both data and protection s/g buffers have been
2179 * mapped for DMA
2180 *
2181 * Returns the number of SGEs added to the SGL.
2182 **/
2183static int
2184lpfc_bg_setup_sgl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
James Smartd79c9e92019-08-14 16:57:09 -07002185 struct sli4_sge *sgl, int datacnt, int protcnt,
2186 struct lpfc_io_buf *lpfc_cmd)
James Smartacd68592012-01-18 16:25:09 -05002187{
2188 struct scatterlist *sgde = NULL; /* s/g data entry */
2189 struct scatterlist *sgpe = NULL; /* s/g prot entry */
2190 struct sli4_sge_diseed *diseed = NULL;
2191 dma_addr_t dataphysaddr, protphysaddr;
2192 unsigned short curr_data = 0, curr_prot = 0;
2193 unsigned int split_offset;
2194 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
2195 unsigned int protgrp_blks, protgrp_bytes;
2196 unsigned int remainder, subtotal;
2197 int status;
2198 unsigned char pgdone = 0, alldone = 0;
2199 unsigned blksize;
2200 uint32_t reftag;
2201 uint8_t txop, rxop;
2202 uint32_t dma_len;
James Smart0829a192012-05-09 21:18:12 -04002203#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002204 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002205#endif
James Smartacd68592012-01-18 16:25:09 -05002206 uint32_t checking = 1;
2207 uint32_t dma_offset = 0;
James Smartd79c9e92019-08-14 16:57:09 -07002208 int num_sge = 0, j = 2;
2209 struct sli4_hybrid_sgl *sgl_xtra = NULL;
James Smartacd68592012-01-18 16:25:09 -05002210
2211 sgpe = scsi_prot_sglist(sc);
2212 sgde = scsi_sglist(sc);
2213
2214 if (!sgpe || !sgde) {
Dick Kennedy372c1872020-06-30 14:50:00 -07002215 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart32350662019-08-14 16:57:06 -07002216 "9082 Invalid s/g entry: data=x%px prot=x%px\n",
James Smartacd68592012-01-18 16:25:09 -05002217 sgpe, sgde);
2218 return 0;
2219 }
2220
2221 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2222 if (status)
2223 goto out;
2224
2225 /* extract some info from the scsi command */
2226 blksize = lpfc_cmd_blksize(sc);
Bart Van Assche4221c8a2021-08-09 16:03:30 -07002227 reftag = t10_pi_ref_tag(scsi_cmd_to_rq(sc));
James Smart68a6a662021-03-01 09:18:02 -08002228 if (reftag == LPFC_INVALID_REFTAG)
2229 goto out;
James Smartacd68592012-01-18 16:25:09 -05002230
2231#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002232 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002233 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002234 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002235 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002236 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002237 checking = 0;
2238 }
2239#endif
2240
2241 split_offset = 0;
2242 do {
James Smart96f70772013-04-17 20:16:15 -04002243 /* Check to see if we ran out of space */
James Smartd79c9e92019-08-14 16:57:09 -07002244 if ((num_sge >= (phba->cfg_total_seg_cnt - 2)) &&
2245 !(phba->cfg_xpsgl))
James Smart96f70772013-04-17 20:16:15 -04002246 return num_sge + 3;
2247
James Smartd79c9e92019-08-14 16:57:09 -07002248 /* DISEED and DIF have to be together */
2249 if (!((j + 1) % phba->border_sge_num) ||
2250 !((j + 2) % phba->border_sge_num) ||
2251 !((j + 3) % phba->border_sge_num)) {
2252 sgl->word2 = 0;
2253
2254 /* set LSP type */
2255 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_LSP);
2256
2257 sgl_xtra = lpfc_get_sgl_per_hdwq(phba, lpfc_cmd);
2258
2259 if (unlikely(!sgl_xtra)) {
2260 goto out;
2261 } else {
2262 sgl->addr_lo = cpu_to_le32(putPaddrLow(
2263 sgl_xtra->dma_phys_sgl));
2264 sgl->addr_hi = cpu_to_le32(putPaddrHigh(
2265 sgl_xtra->dma_phys_sgl));
2266 }
2267
2268 sgl->word2 = cpu_to_le32(sgl->word2);
2269 sgl->sge_len = cpu_to_le32(phba->cfg_sg_dma_buf_size);
2270
2271 sgl = (struct sli4_sge *)sgl_xtra->dma_sgl;
2272 j = 0;
2273 }
2274
James Smartacd68592012-01-18 16:25:09 -05002275 /* setup DISEED with what we have */
2276 diseed = (struct sli4_sge_diseed *) sgl;
2277 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2278 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2279
2280 /* Endianness conversion if necessary */
2281 diseed->ref_tag = cpu_to_le32(reftag);
2282 diseed->ref_tag_tran = diseed->ref_tag;
2283
James Smart9c6aa9d2013-05-31 17:03:39 -04002284 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD)) {
James Smarta6887e22013-04-17 20:18:07 -04002285 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2286
2287 } else {
2288 bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2289 /*
2290 * When in this mode, the hardware will replace
2291 * the guard tag from the host with a
2292 * newly generated good CRC for the wire.
2293 * Switch to raw mode here to avoid this
2294 * behavior. What the host sends gets put on the wire.
2295 */
2296 if (txop == BG_OP_IN_CRC_OUT_CRC) {
2297 txop = BG_OP_RAW_MODE;
2298 rxop = BG_OP_RAW_MODE;
2299 }
2300 }
2301
2302
James Smart9c6aa9d2013-05-31 17:03:39 -04002303 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04002304 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2305 else
2306 bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2307
James Smartacd68592012-01-18 16:25:09 -05002308 /* setup DISEED with the rest of the info */
2309 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2310 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002311
James Smartacd68592012-01-18 16:25:09 -05002312 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2313 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2314
2315 /* Endianness conversion if necessary for DISEED */
2316 diseed->word2 = cpu_to_le32(diseed->word2);
2317 diseed->word3 = cpu_to_le32(diseed->word3);
2318
2319 /* advance sgl and increment bde count */
2320 num_sge++;
James Smartd79c9e92019-08-14 16:57:09 -07002321
James Smartacd68592012-01-18 16:25:09 -05002322 sgl++;
James Smartd79c9e92019-08-14 16:57:09 -07002323 j++;
James Smartacd68592012-01-18 16:25:09 -05002324
2325 /* setup the first BDE that points to protection buffer */
2326 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2327 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
2328
2329 /* must be integer multiple of the DIF block length */
2330 BUG_ON(protgroup_len % 8);
2331
2332 /* Now setup DIF SGE */
2333 sgl->word2 = 0;
2334 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DIF);
2335 sgl->addr_hi = le32_to_cpu(putPaddrHigh(protphysaddr));
2336 sgl->addr_lo = le32_to_cpu(putPaddrLow(protphysaddr));
2337 sgl->word2 = cpu_to_le32(sgl->word2);
James Smartd79c9e92019-08-14 16:57:09 -07002338 sgl->sge_len = 0;
James Smartacd68592012-01-18 16:25:09 -05002339
2340 protgrp_blks = protgroup_len / 8;
2341 protgrp_bytes = protgrp_blks * blksize;
2342
2343 /* check if DIF SGE is crossing the 4K boundary; if so split */
2344 if ((sgl->addr_lo & 0xfff) + protgroup_len > 0x1000) {
2345 protgroup_remainder = 0x1000 - (sgl->addr_lo & 0xfff);
2346 protgroup_offset += protgroup_remainder;
2347 protgrp_blks = protgroup_remainder / 8;
2348 protgrp_bytes = protgrp_blks * blksize;
2349 } else {
2350 protgroup_offset = 0;
2351 curr_prot++;
2352 }
2353
2354 num_sge++;
2355
2356 /* setup SGE's for data blocks associated with DIF data */
2357 pgdone = 0;
2358 subtotal = 0; /* total bytes processed for current prot grp */
James Smartd79c9e92019-08-14 16:57:09 -07002359
2360 sgl++;
2361 j++;
2362
James Smartacd68592012-01-18 16:25:09 -05002363 while (!pgdone) {
James Smart96f70772013-04-17 20:16:15 -04002364 /* Check to see if we ran out of space */
James Smartd79c9e92019-08-14 16:57:09 -07002365 if ((num_sge >= phba->cfg_total_seg_cnt) &&
2366 !phba->cfg_xpsgl)
James Smart96f70772013-04-17 20:16:15 -04002367 return num_sge + 1;
2368
James Smartacd68592012-01-18 16:25:09 -05002369 if (!sgde) {
Dick Kennedy372c1872020-06-30 14:50:00 -07002370 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartacd68592012-01-18 16:25:09 -05002371 "9086 BLKGRD:%s Invalid data segment\n",
2372 __func__);
2373 return 0;
2374 }
James Smartacd68592012-01-18 16:25:09 -05002375
James Smartd79c9e92019-08-14 16:57:09 -07002376 if (!((j + 1) % phba->border_sge_num)) {
2377 sgl->word2 = 0;
James Smartacd68592012-01-18 16:25:09 -05002378
James Smartd79c9e92019-08-14 16:57:09 -07002379 /* set LSP type */
2380 bf_set(lpfc_sli4_sge_type, sgl,
2381 LPFC_SGE_TYPE_LSP);
James Smartacd68592012-01-18 16:25:09 -05002382
James Smartd79c9e92019-08-14 16:57:09 -07002383 sgl_xtra = lpfc_get_sgl_per_hdwq(phba,
2384 lpfc_cmd);
2385
2386 if (unlikely(!sgl_xtra)) {
2387 goto out;
2388 } else {
2389 sgl->addr_lo = cpu_to_le32(
2390 putPaddrLow(sgl_xtra->dma_phys_sgl));
2391 sgl->addr_hi = cpu_to_le32(
2392 putPaddrHigh(sgl_xtra->dma_phys_sgl));
2393 }
2394
2395 sgl->word2 = cpu_to_le32(sgl->word2);
2396 sgl->sge_len = cpu_to_le32(
2397 phba->cfg_sg_dma_buf_size);
2398
2399 sgl = (struct sli4_sge *)sgl_xtra->dma_sgl;
James Smartacd68592012-01-18 16:25:09 -05002400 } else {
James Smartd79c9e92019-08-14 16:57:09 -07002401 dataphysaddr = sg_dma_address(sgde) +
2402 split_offset;
2403
2404 remainder = sg_dma_len(sgde) - split_offset;
2405
2406 if ((subtotal + remainder) <= protgrp_bytes) {
2407 /* we can use this whole buffer */
2408 dma_len = remainder;
2409 split_offset = 0;
2410
2411 if ((subtotal + remainder) ==
2412 protgrp_bytes)
2413 pgdone = 1;
2414 } else {
2415 /* must split this buffer with next
2416 * prot grp
2417 */
2418 dma_len = protgrp_bytes - subtotal;
2419 split_offset += dma_len;
2420 }
2421
2422 subtotal += dma_len;
2423
2424 sgl->word2 = 0;
2425 sgl->addr_lo = cpu_to_le32(putPaddrLow(
2426 dataphysaddr));
2427 sgl->addr_hi = cpu_to_le32(putPaddrHigh(
2428 dataphysaddr));
2429 bf_set(lpfc_sli4_sge_last, sgl, 0);
2430 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2431 bf_set(lpfc_sli4_sge_type, sgl,
2432 LPFC_SGE_TYPE_DATA);
2433
2434 sgl->sge_len = cpu_to_le32(dma_len);
2435 dma_offset += dma_len;
2436
2437 num_sge++;
2438 curr_data++;
2439
2440 if (split_offset) {
2441 sgl++;
2442 j++;
2443 break;
2444 }
2445
2446 /* Move to the next s/g segment if possible */
2447 sgde = sg_next(sgde);
2448
2449 sgl++;
James Smartacd68592012-01-18 16:25:09 -05002450 }
2451
James Smartd79c9e92019-08-14 16:57:09 -07002452 j++;
James Smartacd68592012-01-18 16:25:09 -05002453 }
2454
2455 if (protgroup_offset) {
2456 /* update the reference tag */
2457 reftag += protgrp_blks;
James Smartacd68592012-01-18 16:25:09 -05002458 continue;
2459 }
2460
2461 /* are we done ? */
2462 if (curr_prot == protcnt) {
James Smartd79c9e92019-08-14 16:57:09 -07002463 /* mark the last SGL */
2464 sgl--;
James Smartacd68592012-01-18 16:25:09 -05002465 bf_set(lpfc_sli4_sge_last, sgl, 1);
2466 alldone = 1;
2467 } else if (curr_prot < protcnt) {
2468 /* advance to next prot buffer */
2469 sgpe = sg_next(sgpe);
James Smartacd68592012-01-18 16:25:09 -05002470
2471 /* update the reference tag */
2472 reftag += protgrp_blks;
2473 } else {
2474 /* if we're here, we have a bug */
Dick Kennedy372c1872020-06-30 14:50:00 -07002475 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2476 "9085 BLKGRD: bug in %s\n", __func__);
James Smartacd68592012-01-18 16:25:09 -05002477 }
2478
2479 } while (!alldone);
2480
2481out:
2482
2483 return num_sge;
2484}
2485
2486/**
2487 * lpfc_prot_group_type - Get prtotection group type of SCSI command
2488 * @phba: The Hba for which this call is being executed.
2489 * @sc: pointer to scsi command we're working on
2490 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002491 * Given a SCSI command that supports DIF, determine composition of protection
2492 * groups involved in setting up buffer lists
2493 *
James Smartacd68592012-01-18 16:25:09 -05002494 * Returns: Protection group type (with or without DIF)
2495 *
2496 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002497static int
2498lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
2499{
2500 int ret = LPFC_PG_TYPE_INVALID;
2501 unsigned char op = scsi_get_prot_op(sc);
2502
2503 switch (op) {
2504 case SCSI_PROT_READ_STRIP:
2505 case SCSI_PROT_WRITE_INSERT:
2506 ret = LPFC_PG_TYPE_NO_DIF;
2507 break;
2508 case SCSI_PROT_READ_INSERT:
2509 case SCSI_PROT_WRITE_STRIP:
2510 case SCSI_PROT_READ_PASS:
2511 case SCSI_PROT_WRITE_PASS:
James Smarte2a0a9d2008-12-04 22:40:02 -05002512 ret = LPFC_PG_TYPE_DIF_BUF;
2513 break;
2514 default:
James Smart9c6aa9d2013-05-31 17:03:39 -04002515 if (phba)
Dick Kennedy372c1872020-06-30 14:50:00 -07002516 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart9c6aa9d2013-05-31 17:03:39 -04002517 "9021 Unsupported protection op:%d\n",
2518 op);
James Smarte2a0a9d2008-12-04 22:40:02 -05002519 break;
2520 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002521 return ret;
2522}
2523
James Smartacd68592012-01-18 16:25:09 -05002524/**
James Smarta6887e22013-04-17 20:18:07 -04002525 * lpfc_bg_scsi_adjust_dl - Adjust SCSI data length for BlockGuard
2526 * @phba: The Hba for which this call is being executed.
2527 * @lpfc_cmd: The scsi buffer which is going to be adjusted.
2528 *
2529 * Adjust the data length to account for how much data
2530 * is actually on the wire.
2531 *
2532 * returns the adjusted data length
2533 **/
2534static int
2535lpfc_bg_scsi_adjust_dl(struct lpfc_hba *phba,
James Smartc4908502019-01-28 11:14:28 -08002536 struct lpfc_io_buf *lpfc_cmd)
James Smarta6887e22013-04-17 20:18:07 -04002537{
2538 struct scsi_cmnd *sc = lpfc_cmd->pCmd;
2539 int fcpdl;
2540
2541 fcpdl = scsi_bufflen(sc);
2542
2543 /* Check if there is protection data on the wire */
2544 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
James Smart9c6aa9d2013-05-31 17:03:39 -04002545 /* Read check for protection data */
James Smarta6887e22013-04-17 20:18:07 -04002546 if (scsi_get_prot_op(sc) == SCSI_PROT_READ_INSERT)
2547 return fcpdl;
2548
2549 } else {
James Smart9c6aa9d2013-05-31 17:03:39 -04002550 /* Write check for protection data */
James Smarta6887e22013-04-17 20:18:07 -04002551 if (scsi_get_prot_op(sc) == SCSI_PROT_WRITE_STRIP)
2552 return fcpdl;
2553 }
2554
2555 /*
2556 * If we are in DIF Type 1 mode every data block has a 8 byte
James Smart9c6aa9d2013-05-31 17:03:39 -04002557 * DIF (trailer) attached to it. Must ajust FCP data length
2558 * to account for the protection data.
James Smarta6887e22013-04-17 20:18:07 -04002559 */
James Smart9c6aa9d2013-05-31 17:03:39 -04002560 fcpdl += (fcpdl / lpfc_cmd_blksize(sc)) * 8;
James Smarta6887e22013-04-17 20:18:07 -04002561
2562 return fcpdl;
2563}
2564
2565/**
James Smartacd68592012-01-18 16:25:09 -05002566 * lpfc_bg_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
2567 * @phba: The Hba for which this call is being executed.
2568 * @lpfc_cmd: The scsi buffer which is going to be prep'ed.
2569 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002570 * This is the protection/DIF aware version of
2571 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
James Smart5e0e2312019-08-14 16:56:54 -07002572 * two functions eventually, but for now, it's here.
2573 * RETURNS 0 - SUCCESS,
2574 * 1 - Failed DMA map, retry.
2575 * 2 - Invalid scsi cmd or prot-type. Do not rety.
James Smartacd68592012-01-18 16:25:09 -05002576 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002577static int
James Smartacd68592012-01-18 16:25:09 -05002578lpfc_bg_scsi_prep_dma_buf_s3(struct lpfc_hba *phba,
James Smartc4908502019-01-28 11:14:28 -08002579 struct lpfc_io_buf *lpfc_cmd)
James Smarte2a0a9d2008-12-04 22:40:02 -05002580{
2581 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2582 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smartc4908502019-01-28 11:14:28 -08002583 struct ulp_bde64 *bpl = (struct ulp_bde64 *)lpfc_cmd->dma_sgl;
James Smarte2a0a9d2008-12-04 22:40:02 -05002584 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2585 uint32_t num_bde = 0;
2586 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
2587 int prot_group_type = 0;
James Smarta6887e22013-04-17 20:18:07 -04002588 int fcpdl;
James Smart5e0e2312019-08-14 16:56:54 -07002589 int ret = 1;
James Smart7c4042a2018-11-29 16:09:40 -08002590 struct lpfc_vport *vport = phba->pport;
James Smarte2a0a9d2008-12-04 22:40:02 -05002591
2592 /*
2593 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
2594 * fcp_rsp regions to the first data bde entry
2595 */
2596 bpl += 2;
2597 if (scsi_sg_count(scsi_cmnd)) {
2598 /*
2599 * The driver stores the segment count returned from pci_map_sg
2600 * because this a count of dma-mappings used to map the use_sg
2601 * pages. They are not guaranteed to be the same for those
2602 * architectures that implement an IOMMU.
2603 */
2604 datasegcnt = dma_map_sg(&phba->pcidev->dev,
2605 scsi_sglist(scsi_cmnd),
2606 scsi_sg_count(scsi_cmnd), datadir);
2607 if (unlikely(!datasegcnt))
2608 return 1;
2609
2610 lpfc_cmd->seg_cnt = datasegcnt;
James Smart96f70772013-04-17 20:16:15 -04002611
2612 /* First check if data segment count from SCSI Layer is good */
James Smart5e0e2312019-08-14 16:56:54 -07002613 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
2614 WARN_ON_ONCE(lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt);
2615 ret = 2;
James Smart96f70772013-04-17 20:16:15 -04002616 goto err;
James Smart5e0e2312019-08-14 16:56:54 -07002617 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002618
2619 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
2620
2621 switch (prot_group_type) {
2622 case LPFC_PG_TYPE_NO_DIF:
James Smart96f70772013-04-17 20:16:15 -04002623
2624 /* Here we need to add a PDE5 and PDE6 to the count */
James Smart5e0e2312019-08-14 16:56:54 -07002625 if ((lpfc_cmd->seg_cnt + 2) > phba->cfg_total_seg_cnt) {
2626 ret = 2;
James Smart96f70772013-04-17 20:16:15 -04002627 goto err;
James Smart5e0e2312019-08-14 16:56:54 -07002628 }
James Smart96f70772013-04-17 20:16:15 -04002629
James Smarte2a0a9d2008-12-04 22:40:02 -05002630 num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
2631 datasegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002632 /* we should have 2 or more entries in buffer list */
James Smart5e0e2312019-08-14 16:56:54 -07002633 if (num_bde < 2) {
2634 ret = 2;
James Smarte2a0a9d2008-12-04 22:40:02 -05002635 goto err;
James Smart5e0e2312019-08-14 16:56:54 -07002636 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002637 break;
James Smart96f70772013-04-17 20:16:15 -04002638
2639 case LPFC_PG_TYPE_DIF_BUF:
James Smarte2a0a9d2008-12-04 22:40:02 -05002640 /*
2641 * This type indicates that protection buffers are
2642 * passed to the driver, so that needs to be prepared
2643 * for DMA
2644 */
2645 protsegcnt = dma_map_sg(&phba->pcidev->dev,
2646 scsi_prot_sglist(scsi_cmnd),
2647 scsi_prot_sg_count(scsi_cmnd), datadir);
2648 if (unlikely(!protsegcnt)) {
2649 scsi_dma_unmap(scsi_cmnd);
2650 return 1;
2651 }
2652
2653 lpfc_cmd->prot_seg_cnt = protsegcnt;
James Smart96f70772013-04-17 20:16:15 -04002654
2655 /*
2656 * There is a minimun of 4 BPLs used for every
2657 * protection data segment.
2658 */
2659 if ((lpfc_cmd->prot_seg_cnt * 4) >
James Smart5e0e2312019-08-14 16:56:54 -07002660 (phba->cfg_total_seg_cnt - 2)) {
2661 ret = 2;
James Smart96f70772013-04-17 20:16:15 -04002662 goto err;
James Smart5e0e2312019-08-14 16:56:54 -07002663 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002664
2665 num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
2666 datasegcnt, protsegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002667 /* we should have 3 or more entries in buffer list */
James Smart96f70772013-04-17 20:16:15 -04002668 if ((num_bde < 3) ||
James Smart5e0e2312019-08-14 16:56:54 -07002669 (num_bde > phba->cfg_total_seg_cnt)) {
2670 ret = 2;
James Smarte2a0a9d2008-12-04 22:40:02 -05002671 goto err;
James Smart5e0e2312019-08-14 16:56:54 -07002672 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002673 break;
James Smart96f70772013-04-17 20:16:15 -04002674
James Smarte2a0a9d2008-12-04 22:40:02 -05002675 case LPFC_PG_TYPE_INVALID:
2676 default:
James Smart96f70772013-04-17 20:16:15 -04002677 scsi_dma_unmap(scsi_cmnd);
2678 lpfc_cmd->seg_cnt = 0;
2679
Dick Kennedy372c1872020-06-30 14:50:00 -07002680 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte2a0a9d2008-12-04 22:40:02 -05002681 "9022 Unexpected protection group %i\n",
2682 prot_group_type);
James Smart5e0e2312019-08-14 16:56:54 -07002683 return 2;
James Smarte2a0a9d2008-12-04 22:40:02 -05002684 }
2685 }
2686
2687 /*
2688 * Finish initializing those IOCB fields that are dependent on the
2689 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
2690 * reinitialized since all iocb memory resources are used many times
2691 * for transmit, receive, and continuation bpl's.
2692 */
2693 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
2694 iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
2695 iocb_cmd->ulpBdeCount = 1;
2696 iocb_cmd->ulpLe = 1;
2697
James Smarta6887e22013-04-17 20:18:07 -04002698 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002699 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
2700
2701 /*
2702 * Due to difference in data length between DIF/non-DIF paths,
2703 * we need to set word 4 of IOCB here
2704 */
2705 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
2706
James Smart7c4042a2018-11-29 16:09:40 -08002707 /*
2708 * For First burst, we may need to adjust the initial transfer
2709 * length for DIF
2710 */
2711 if (iocb_cmd->un.fcpi.fcpi_XRdy &&
2712 (fcpdl < vport->cfg_first_burst_size))
2713 iocb_cmd->un.fcpi.fcpi_XRdy = fcpdl;
2714
James Smarte2a0a9d2008-12-04 22:40:02 -05002715 return 0;
2716err:
James Smart96f70772013-04-17 20:16:15 -04002717 if (lpfc_cmd->seg_cnt)
2718 scsi_dma_unmap(scsi_cmnd);
2719 if (lpfc_cmd->prot_seg_cnt)
2720 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
2721 scsi_prot_sg_count(scsi_cmnd),
2722 scsi_cmnd->sc_data_direction);
2723
Dick Kennedy372c1872020-06-30 14:50:00 -07002724 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart96f70772013-04-17 20:16:15 -04002725 "9023 Cannot setup S/G List for HBA"
2726 "IO segs %d/%d BPL %d SCSI %d: %d %d\n",
2727 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
2728 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
James Smarte2a0a9d2008-12-04 22:40:02 -05002729 prot_group_type, num_bde);
James Smart96f70772013-04-17 20:16:15 -04002730
2731 lpfc_cmd->seg_cnt = 0;
2732 lpfc_cmd->prot_seg_cnt = 0;
James Smart5e0e2312019-08-14 16:56:54 -07002733 return ret;
James Smarte2a0a9d2008-12-04 22:40:02 -05002734}
2735
2736/*
James Smart737d4242013-04-17 20:14:49 -04002737 * This function calcuates the T10 DIF guard tag
2738 * on the specified data using a CRC algorithmn
2739 * using crc_t10dif.
2740 */
Rashika Kheria7bfe7812014-09-03 12:55:36 -04002741static uint16_t
James Smart737d4242013-04-17 20:14:49 -04002742lpfc_bg_crc(uint8_t *data, int count)
2743{
2744 uint16_t crc = 0;
2745 uint16_t x;
2746
2747 crc = crc_t10dif(data, count);
2748 x = cpu_to_be16(crc);
2749 return x;
2750}
2751
2752/*
2753 * This function calcuates the T10 DIF guard tag
2754 * on the specified data using a CSUM algorithmn
2755 * using ip_compute_csum.
2756 */
Rashika Kheria7bfe7812014-09-03 12:55:36 -04002757static uint16_t
James Smart737d4242013-04-17 20:14:49 -04002758lpfc_bg_csum(uint8_t *data, int count)
2759{
2760 uint16_t ret;
2761
2762 ret = ip_compute_csum(data, count);
2763 return ret;
2764}
2765
2766/*
2767 * This function examines the protection data to try to determine
2768 * what type of T10-DIF error occurred.
2769 */
Rashika Kheria7bfe7812014-09-03 12:55:36 -04002770static void
James Smartc4908502019-01-28 11:14:28 -08002771lpfc_calc_bg_err(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
James Smart737d4242013-04-17 20:14:49 -04002772{
2773 struct scatterlist *sgpe; /* s/g prot entry */
2774 struct scatterlist *sgde; /* s/g data entry */
2775 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
2776 struct scsi_dif_tuple *src = NULL;
2777 uint8_t *data_src = NULL;
Sebastian Herbsztdb6f1c22015-08-31 16:48:14 -04002778 uint16_t guard_tag;
James Smart737d4242013-04-17 20:14:49 -04002779 uint16_t start_app_tag, app_tag;
2780 uint32_t start_ref_tag, ref_tag;
2781 int prot, protsegcnt;
2782 int err_type, len, data_len;
2783 int chk_ref, chk_app, chk_guard;
2784 uint16_t sum;
2785 unsigned blksize;
2786
2787 err_type = BGS_GUARD_ERR_MASK;
2788 sum = 0;
2789 guard_tag = 0;
2790
2791 /* First check to see if there is protection data to examine */
2792 prot = scsi_get_prot_op(cmd);
2793 if ((prot == SCSI_PROT_READ_STRIP) ||
2794 (prot == SCSI_PROT_WRITE_INSERT) ||
2795 (prot == SCSI_PROT_NORMAL))
2796 goto out;
2797
2798 /* Currently the driver just supports ref_tag and guard_tag checking */
2799 chk_ref = 1;
2800 chk_app = 0;
2801 chk_guard = 0;
2802
2803 /* Setup a ptr to the protection data provided by the SCSI host */
2804 sgpe = scsi_prot_sglist(cmd);
2805 protsegcnt = lpfc_cmd->prot_seg_cnt;
2806
2807 if (sgpe && protsegcnt) {
2808
2809 /*
2810 * We will only try to verify guard tag if the segment
2811 * data length is a multiple of the blksize.
2812 */
2813 sgde = scsi_sglist(cmd);
2814 blksize = lpfc_cmd_blksize(cmd);
2815 data_src = (uint8_t *)sg_virt(sgde);
2816 data_len = sgde->length;
2817 if ((data_len & (blksize - 1)) == 0)
2818 chk_guard = 1;
James Smart737d4242013-04-17 20:14:49 -04002819
James Smarte85d8f92013-05-31 17:04:10 -04002820 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
Bart Van Assche4221c8a2021-08-09 16:03:30 -07002821 start_ref_tag = t10_pi_ref_tag(scsi_cmd_to_rq(cmd));
James Smart68a6a662021-03-01 09:18:02 -08002822 if (start_ref_tag == LPFC_INVALID_REFTAG)
2823 goto out;
James Smart737d4242013-04-17 20:14:49 -04002824 start_app_tag = src->app_tag;
James Smart737d4242013-04-17 20:14:49 -04002825 len = sgpe->length;
2826 while (src && protsegcnt) {
2827 while (len) {
2828
2829 /*
2830 * First check to see if a protection data
2831 * check is valid
2832 */
Dmitry Monakhov128b6f92017-06-29 11:31:12 -07002833 if ((src->ref_tag == T10_PI_REF_ESCAPE) ||
2834 (src->app_tag == T10_PI_APP_ESCAPE)) {
James Smart737d4242013-04-17 20:14:49 -04002835 start_ref_tag++;
2836 goto skipit;
2837 }
2838
James Smart9c6aa9d2013-05-31 17:03:39 -04002839 /* First Guard Tag checking */
2840 if (chk_guard) {
2841 guard_tag = src->guard_tag;
2842 if (lpfc_cmd_guard_csum(cmd))
2843 sum = lpfc_bg_csum(data_src,
2844 blksize);
2845 else
2846 sum = lpfc_bg_crc(data_src,
2847 blksize);
2848 if ((guard_tag != sum)) {
2849 err_type = BGS_GUARD_ERR_MASK;
2850 goto out;
2851 }
James Smart737d4242013-04-17 20:14:49 -04002852 }
2853
2854 /* Reference Tag checking */
2855 ref_tag = be32_to_cpu(src->ref_tag);
2856 if (chk_ref && (ref_tag != start_ref_tag)) {
2857 err_type = BGS_REFTAG_ERR_MASK;
2858 goto out;
2859 }
2860 start_ref_tag++;
2861
James Smart9c6aa9d2013-05-31 17:03:39 -04002862 /* App Tag checking */
2863 app_tag = src->app_tag;
2864 if (chk_app && (app_tag != start_app_tag)) {
2865 err_type = BGS_APPTAG_ERR_MASK;
2866 goto out;
James Smart737d4242013-04-17 20:14:49 -04002867 }
2868skipit:
2869 len -= sizeof(struct scsi_dif_tuple);
2870 if (len < 0)
2871 len = 0;
2872 src++;
2873
2874 data_src += blksize;
2875 data_len -= blksize;
2876
2877 /*
2878 * Are we at the end of the Data segment?
2879 * The data segment is only used for Guard
2880 * tag checking.
2881 */
2882 if (chk_guard && (data_len == 0)) {
2883 chk_guard = 0;
2884 sgde = sg_next(sgde);
2885 if (!sgde)
2886 goto out;
2887
2888 data_src = (uint8_t *)sg_virt(sgde);
2889 data_len = sgde->length;
2890 if ((data_len & (blksize - 1)) == 0)
2891 chk_guard = 1;
2892 }
2893 }
2894
2895 /* Goto the next Protection data segment */
2896 sgpe = sg_next(sgpe);
2897 if (sgpe) {
2898 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
2899 len = sgpe->length;
2900 } else {
2901 src = NULL;
2902 }
2903 protsegcnt--;
2904 }
2905 }
2906out:
2907 if (err_type == BGS_GUARD_ERR_MASK) {
Hannes Reineckef2b1e9c2021-04-27 10:30:13 +02002908 scsi_build_sense(cmd, 1, ILLEGAL_REQUEST, 0x10, 0x1);
2909 set_host_byte(cmd, DID_ABORT);
James Smart737d4242013-04-17 20:14:49 -04002910 phba->bg_guard_err_cnt++;
2911 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
James Smart68a6a662021-03-01 09:18:02 -08002912 "9069 BLKGRD: reftag %x grd_tag err %x != %x\n",
Bart Van Assche4221c8a2021-08-09 16:03:30 -07002913 t10_pi_ref_tag(scsi_cmd_to_rq(cmd)),
James Smart737d4242013-04-17 20:14:49 -04002914 sum, guard_tag);
2915
2916 } else if (err_type == BGS_REFTAG_ERR_MASK) {
Hannes Reineckef2b1e9c2021-04-27 10:30:13 +02002917 scsi_build_sense(cmd, 1, ILLEGAL_REQUEST, 0x10, 0x3);
2918 set_host_byte(cmd, DID_ABORT);
James Smart737d4242013-04-17 20:14:49 -04002919
2920 phba->bg_reftag_err_cnt++;
2921 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
James Smart68a6a662021-03-01 09:18:02 -08002922 "9066 BLKGRD: reftag %x ref_tag err %x != %x\n",
Bart Van Assche4221c8a2021-08-09 16:03:30 -07002923 t10_pi_ref_tag(scsi_cmd_to_rq(cmd)),
James Smart737d4242013-04-17 20:14:49 -04002924 ref_tag, start_ref_tag);
2925
2926 } else if (err_type == BGS_APPTAG_ERR_MASK) {
Hannes Reineckef2b1e9c2021-04-27 10:30:13 +02002927 scsi_build_sense(cmd, 1, ILLEGAL_REQUEST, 0x10, 0x2);
2928 set_host_byte(cmd, DID_ABORT);
James Smart737d4242013-04-17 20:14:49 -04002929
2930 phba->bg_apptag_err_cnt++;
2931 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
James Smart68a6a662021-03-01 09:18:02 -08002932 "9041 BLKGRD: reftag %x app_tag err %x != %x\n",
Bart Van Assche4221c8a2021-08-09 16:03:30 -07002933 t10_pi_ref_tag(scsi_cmd_to_rq(cmd)),
James Smart737d4242013-04-17 20:14:49 -04002934 app_tag, start_app_tag);
2935 }
2936}
2937
James Smart96e209b2020-11-15 11:26:43 -08002938/*
2939 * This function checks for BlockGuard errors detected by
2940 * the HBA. In case of errors, the ASC/ASCQ fields in the
2941 * sense buffer will be set accordingly, paired with
2942 * ILLEGAL_REQUEST to signal to the kernel that the HBA
2943 * detected corruption.
2944 *
2945 * Returns:
2946 * 0 - No error found
2947 * 1 - BlockGuard error found
2948 * -1 - Internal error (bad profile, ...etc)
2949 */
2950static int
2951lpfc_sli4_parse_bg_err(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd,
2952 struct lpfc_wcqe_complete *wcqe)
2953{
2954 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
2955 int ret = 0;
2956 u32 status = bf_get(lpfc_wcqe_c_status, wcqe);
2957 u32 bghm = 0;
2958 u32 bgstat = 0;
2959 u64 failing_sector = 0;
2960
2961 if (status == CQE_STATUS_DI_ERROR) {
2962 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
2963 bgstat |= BGS_GUARD_ERR_MASK;
2964 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* AppTag Check failed */
2965 bgstat |= BGS_APPTAG_ERR_MASK;
2966 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* RefTag Check failed */
2967 bgstat |= BGS_REFTAG_ERR_MASK;
2968
2969 /* Check to see if there was any good data before the error */
2970 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
2971 bgstat |= BGS_HI_WATER_MARK_PRESENT_MASK;
2972 bghm = wcqe->total_data_placed;
2973 }
2974
2975 /*
2976 * Set ALL the error bits to indicate we don't know what
2977 * type of error it is.
2978 */
2979 if (!bgstat)
2980 bgstat |= (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
2981 BGS_GUARD_ERR_MASK);
2982 }
2983
2984 if (lpfc_bgs_get_guard_err(bgstat)) {
2985 ret = 1;
2986
Hannes Reineckef2b1e9c2021-04-27 10:30:13 +02002987 scsi_build_sense(cmd, 1, ILLEGAL_REQUEST, 0x10, 0x1);
2988 set_host_byte(cmd, DID_ABORT);
James Smart96e209b2020-11-15 11:26:43 -08002989 phba->bg_guard_err_cnt++;
2990 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2991 "9059 BLKGRD: Guard Tag error in cmd"
2992 " 0x%x lba 0x%llx blk cnt 0x%x "
2993 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
2994 (unsigned long long)scsi_get_lba(cmd),
Bart Van Assche4221c8a2021-08-09 16:03:30 -07002995 blk_rq_sectors(scsi_cmd_to_rq(cmd)), bgstat, bghm);
James Smart96e209b2020-11-15 11:26:43 -08002996 }
2997
2998 if (lpfc_bgs_get_reftag_err(bgstat)) {
2999 ret = 1;
3000
Hannes Reineckef2b1e9c2021-04-27 10:30:13 +02003001 scsi_build_sense(cmd, 1, ILLEGAL_REQUEST, 0x10, 0x3);
3002 set_host_byte(cmd, DID_ABORT);
James Smart96e209b2020-11-15 11:26:43 -08003003
3004 phba->bg_reftag_err_cnt++;
3005 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3006 "9060 BLKGRD: Ref Tag error in cmd"
3007 " 0x%x lba 0x%llx blk cnt 0x%x "
3008 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3009 (unsigned long long)scsi_get_lba(cmd),
Bart Van Assche4221c8a2021-08-09 16:03:30 -07003010 blk_rq_sectors(scsi_cmd_to_rq(cmd)), bgstat, bghm);
James Smart96e209b2020-11-15 11:26:43 -08003011 }
3012
3013 if (lpfc_bgs_get_apptag_err(bgstat)) {
3014 ret = 1;
3015
Hannes Reineckef2b1e9c2021-04-27 10:30:13 +02003016 scsi_build_sense(cmd, 1, ILLEGAL_REQUEST, 0x10, 0x2);
3017 set_host_byte(cmd, DID_ABORT);
James Smart96e209b2020-11-15 11:26:43 -08003018
3019 phba->bg_apptag_err_cnt++;
3020 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3021 "9062 BLKGRD: App Tag error in cmd"
3022 " 0x%x lba 0x%llx blk cnt 0x%x "
3023 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3024 (unsigned long long)scsi_get_lba(cmd),
Bart Van Assche4221c8a2021-08-09 16:03:30 -07003025 blk_rq_sectors(scsi_cmd_to_rq(cmd)), bgstat, bghm);
James Smart96e209b2020-11-15 11:26:43 -08003026 }
3027
3028 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
3029 /*
3030 * setup sense data descriptor 0 per SPC-4 as an information
3031 * field, and put the failing LBA in it.
3032 * This code assumes there was also a guard/app/ref tag error
3033 * indication.
3034 */
3035 cmd->sense_buffer[7] = 0xc; /* Additional sense length */
3036 cmd->sense_buffer[8] = 0; /* Information descriptor type */
3037 cmd->sense_buffer[9] = 0xa; /* Additional descriptor length */
3038 cmd->sense_buffer[10] = 0x80; /* Validity bit */
3039
3040 /* bghm is a "on the wire" FC frame based count */
3041 switch (scsi_get_prot_op(cmd)) {
3042 case SCSI_PROT_READ_INSERT:
3043 case SCSI_PROT_WRITE_STRIP:
3044 bghm /= cmd->device->sector_size;
3045 break;
3046 case SCSI_PROT_READ_STRIP:
3047 case SCSI_PROT_WRITE_INSERT:
3048 case SCSI_PROT_READ_PASS:
3049 case SCSI_PROT_WRITE_PASS:
3050 bghm /= (cmd->device->sector_size +
3051 sizeof(struct scsi_dif_tuple));
3052 break;
3053 }
3054
3055 failing_sector = scsi_get_lba(cmd);
3056 failing_sector += bghm;
3057
3058 /* Descriptor Information */
3059 put_unaligned_be64(failing_sector, &cmd->sense_buffer[12]);
3060 }
3061
3062 if (!ret) {
3063 /* No error was reported - problem in FW? */
3064 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3065 "9068 BLKGRD: Unknown error in cmd"
3066 " 0x%x lba 0x%llx blk cnt 0x%x "
3067 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3068 (unsigned long long)scsi_get_lba(cmd),
Bart Van Assche4221c8a2021-08-09 16:03:30 -07003069 blk_rq_sectors(scsi_cmd_to_rq(cmd)), bgstat, bghm);
James Smart96e209b2020-11-15 11:26:43 -08003070
3071 /* Calcuate what type of error it was */
3072 lpfc_calc_bg_err(phba, lpfc_cmd);
3073 }
3074 return ret;
3075}
James Smart737d4242013-04-17 20:14:49 -04003076
3077/*
James Smarte2a0a9d2008-12-04 22:40:02 -05003078 * This function checks for BlockGuard errors detected by
3079 * the HBA. In case of errors, the ASC/ASCQ fields in the
3080 * sense buffer will be set accordingly, paired with
3081 * ILLEGAL_REQUEST to signal to the kernel that the HBA
3082 * detected corruption.
3083 *
3084 * Returns:
3085 * 0 - No error found
3086 * 1 - BlockGuard error found
3087 * -1 - Internal error (bad profile, ...etc)
3088 */
3089static int
James Smartc4908502019-01-28 11:14:28 -08003090lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd,
3091 struct lpfc_iocbq *pIocbOut)
James Smarte2a0a9d2008-12-04 22:40:02 -05003092{
3093 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
3094 struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
3095 int ret = 0;
3096 uint32_t bghm = bgf->bghm;
3097 uint32_t bgstat = bgf->bgstat;
3098 uint64_t failing_sector = 0;
3099
James Smarte2a0a9d2008-12-04 22:40:02 -05003100 if (lpfc_bgs_get_invalid_prof(bgstat)) {
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02003101 cmd->result = DID_ERROR << 16;
James Smart737d4242013-04-17 20:14:49 -04003102 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
James Smart68a6a662021-03-01 09:18:02 -08003103 "9072 BLKGRD: Invalid BG Profile in cmd "
3104 "0x%x reftag 0x%x blk cnt 0x%x "
James Smart737d4242013-04-17 20:14:49 -04003105 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
Bart Van Assche4221c8a2021-08-09 16:03:30 -07003106 t10_pi_ref_tag(scsi_cmd_to_rq(cmd)),
3107 blk_rq_sectors(scsi_cmd_to_rq(cmd)), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003108 ret = (-1);
3109 goto out;
3110 }
3111
3112 if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02003113 cmd->result = DID_ERROR << 16;
James Smart737d4242013-04-17 20:14:49 -04003114 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
James Smart68a6a662021-03-01 09:18:02 -08003115 "9073 BLKGRD: Invalid BG PDIF Block in cmd "
3116 "0x%x reftag 0x%x blk cnt 0x%x "
James Smart737d4242013-04-17 20:14:49 -04003117 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
Bart Van Assche4221c8a2021-08-09 16:03:30 -07003118 t10_pi_ref_tag(scsi_cmd_to_rq(cmd)),
3119 blk_rq_sectors(scsi_cmd_to_rq(cmd)), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003120 ret = (-1);
3121 goto out;
3122 }
3123
3124 if (lpfc_bgs_get_guard_err(bgstat)) {
3125 ret = 1;
3126
Hannes Reineckef2b1e9c2021-04-27 10:30:13 +02003127 scsi_build_sense(cmd, 1, ILLEGAL_REQUEST, 0x10, 0x1);
3128 set_host_byte(cmd, DID_ABORT);
James Smarte2a0a9d2008-12-04 22:40:02 -05003129 phba->bg_guard_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04003130 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
James Smart68a6a662021-03-01 09:18:02 -08003131 "9055 BLKGRD: Guard Tag error in cmd "
3132 "0x%x reftag 0x%x blk cnt 0x%x "
James Smart737d4242013-04-17 20:14:49 -04003133 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
Bart Van Assche4221c8a2021-08-09 16:03:30 -07003134 t10_pi_ref_tag(scsi_cmd_to_rq(cmd)),
3135 blk_rq_sectors(scsi_cmd_to_rq(cmd)), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003136 }
3137
3138 if (lpfc_bgs_get_reftag_err(bgstat)) {
3139 ret = 1;
3140
Hannes Reineckef2b1e9c2021-04-27 10:30:13 +02003141 scsi_build_sense(cmd, 1, ILLEGAL_REQUEST, 0x10, 0x3);
3142 set_host_byte(cmd, DID_ABORT);
James Smarte2a0a9d2008-12-04 22:40:02 -05003143
3144 phba->bg_reftag_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04003145 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
James Smart68a6a662021-03-01 09:18:02 -08003146 "9056 BLKGRD: Ref Tag error in cmd "
3147 "0x%x reftag 0x%x blk cnt 0x%x "
James Smart737d4242013-04-17 20:14:49 -04003148 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
Bart Van Assche4221c8a2021-08-09 16:03:30 -07003149 t10_pi_ref_tag(scsi_cmd_to_rq(cmd)),
3150 blk_rq_sectors(scsi_cmd_to_rq(cmd)), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003151 }
3152
3153 if (lpfc_bgs_get_apptag_err(bgstat)) {
3154 ret = 1;
3155
Hannes Reineckef2b1e9c2021-04-27 10:30:13 +02003156 scsi_build_sense(cmd, 1, ILLEGAL_REQUEST, 0x10, 0x2);
3157 set_host_byte(cmd, DID_ABORT);
James Smarte2a0a9d2008-12-04 22:40:02 -05003158
3159 phba->bg_apptag_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04003160 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
James Smart68a6a662021-03-01 09:18:02 -08003161 "9061 BLKGRD: App Tag error in cmd "
3162 "0x%x reftag 0x%x blk cnt 0x%x "
James Smart737d4242013-04-17 20:14:49 -04003163 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
Bart Van Assche4221c8a2021-08-09 16:03:30 -07003164 t10_pi_ref_tag(scsi_cmd_to_rq(cmd)),
3165 blk_rq_sectors(scsi_cmd_to_rq(cmd)), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003166 }
3167
3168 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
3169 /*
3170 * setup sense data descriptor 0 per SPC-4 as an information
James Smart7c56b9f2011-07-22 18:36:25 -04003171 * field, and put the failing LBA in it.
3172 * This code assumes there was also a guard/app/ref tag error
3173 * indication.
James Smarte2a0a9d2008-12-04 22:40:02 -05003174 */
James Smart7c56b9f2011-07-22 18:36:25 -04003175 cmd->sense_buffer[7] = 0xc; /* Additional sense length */
3176 cmd->sense_buffer[8] = 0; /* Information descriptor type */
3177 cmd->sense_buffer[9] = 0xa; /* Additional descriptor length */
3178 cmd->sense_buffer[10] = 0x80; /* Validity bit */
James Smartacd68592012-01-18 16:25:09 -05003179
3180 /* bghm is a "on the wire" FC frame based count */
3181 switch (scsi_get_prot_op(cmd)) {
3182 case SCSI_PROT_READ_INSERT:
3183 case SCSI_PROT_WRITE_STRIP:
3184 bghm /= cmd->device->sector_size;
3185 break;
3186 case SCSI_PROT_READ_STRIP:
3187 case SCSI_PROT_WRITE_INSERT:
3188 case SCSI_PROT_READ_PASS:
3189 case SCSI_PROT_WRITE_PASS:
3190 bghm /= (cmd->device->sector_size +
3191 sizeof(struct scsi_dif_tuple));
3192 break;
3193 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003194
3195 failing_sector = scsi_get_lba(cmd);
3196 failing_sector += bghm;
3197
James Smart7c56b9f2011-07-22 18:36:25 -04003198 /* Descriptor Information */
3199 put_unaligned_be64(failing_sector, &cmd->sense_buffer[12]);
James Smarte2a0a9d2008-12-04 22:40:02 -05003200 }
3201
3202 if (!ret) {
3203 /* No error was reported - problem in FW? */
James Smart737d4242013-04-17 20:14:49 -04003204 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
James Smart68a6a662021-03-01 09:18:02 -08003205 "9057 BLKGRD: Unknown error in cmd "
3206 "0x%x reftag 0x%x blk cnt 0x%x "
James Smart737d4242013-04-17 20:14:49 -04003207 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
Bart Van Assche4221c8a2021-08-09 16:03:30 -07003208 t10_pi_ref_tag(scsi_cmd_to_rq(cmd)),
3209 blk_rq_sectors(scsi_cmd_to_rq(cmd)), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003210
James Smart737d4242013-04-17 20:14:49 -04003211 /* Calcuate what type of error it was */
3212 lpfc_calc_bg_err(phba, lpfc_cmd);
3213 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003214out:
3215 return ret;
3216}
3217
James Smartea2151b2008-09-07 11:52:10 -04003218/**
James Smartda0436e2009-05-22 14:51:39 -04003219 * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3220 * @phba: The Hba for which this call is being executed.
3221 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3222 *
3223 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
3224 * field of @lpfc_cmd for device with SLI-4 interface spec.
3225 *
3226 * Return codes:
James Smart5e0e2312019-08-14 16:56:54 -07003227 * 2 - Error - Do not retry
3228 * 1 - Error - Retry
James Smart6c8eea52010-04-06 14:49:53 -04003229 * 0 - Success
James Smartda0436e2009-05-22 14:51:39 -04003230 **/
3231static int
James Smartc4908502019-01-28 11:14:28 -08003232lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
James Smartda0436e2009-05-22 14:51:39 -04003233{
3234 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3235 struct scatterlist *sgel = NULL;
3236 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart0794d602019-01-28 11:14:19 -08003237 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
James Smartfedd3b72011-02-16 12:39:24 -05003238 struct sli4_sge *first_data_sgl;
James Smartda255e22020-11-15 11:26:42 -08003239 struct lpfc_iocbq *pwqeq = &lpfc_cmd->cur_iocbq;
3240 struct lpfc_vport *vport = phba->pport;
3241 union lpfc_wqe128 *wqe = &pwqeq->wqe;
James Smartda0436e2009-05-22 14:51:39 -04003242 dma_addr_t physaddr;
3243 uint32_t num_bde = 0;
3244 uint32_t dma_len;
3245 uint32_t dma_offset = 0;
James Smartd79c9e92019-08-14 16:57:09 -07003246 int nseg, i, j;
James Smartfedd3b72011-02-16 12:39:24 -05003247 struct ulp_bde64 *bde;
James Smartd79c9e92019-08-14 16:57:09 -07003248 bool lsp_just_set = false;
3249 struct sli4_hybrid_sgl *sgl_xtra = NULL;
James Smartda0436e2009-05-22 14:51:39 -04003250
3251 /*
3252 * There are three possibilities here - use scatter-gather segment, use
3253 * the single mapping, or neither. Start the lpfc command prep by
3254 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
3255 * data bde entry.
3256 */
3257 if (scsi_sg_count(scsi_cmnd)) {
3258 /*
3259 * The driver stores the segment count returned from pci_map_sg
3260 * because this a count of dma-mappings used to map the use_sg
3261 * pages. They are not guaranteed to be the same for those
3262 * architectures that implement an IOMMU.
3263 */
3264
3265 nseg = scsi_dma_map(scsi_cmnd);
James Smart5116fbf2015-05-22 10:42:39 -04003266 if (unlikely(nseg <= 0))
James Smartda0436e2009-05-22 14:51:39 -04003267 return 1;
3268 sgl += 1;
3269 /* clear the last flag in the fcp_rsp map entry */
3270 sgl->word2 = le32_to_cpu(sgl->word2);
3271 bf_set(lpfc_sli4_sge_last, sgl, 0);
3272 sgl->word2 = cpu_to_le32(sgl->word2);
3273 sgl += 1;
James Smartfedd3b72011-02-16 12:39:24 -05003274 first_data_sgl = sgl;
James Smartda0436e2009-05-22 14:51:39 -04003275 lpfc_cmd->seg_cnt = nseg;
James Smartd79c9e92019-08-14 16:57:09 -07003276 if (!phba->cfg_xpsgl &&
3277 lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
Dick Kennedy372c1872020-06-30 14:50:00 -07003278 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3279 "9074 BLKGRD:"
3280 " %s: Too many sg segments from "
3281 "dma_map_sg. Config %d, seg_cnt %d\n",
3282 __func__, phba->cfg_sg_seg_cnt,
3283 lpfc_cmd->seg_cnt);
James Smart5e0e2312019-08-14 16:56:54 -07003284 WARN_ON_ONCE(lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt);
James Smart96f70772013-04-17 20:16:15 -04003285 lpfc_cmd->seg_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04003286 scsi_dma_unmap(scsi_cmnd);
James Smart5e0e2312019-08-14 16:56:54 -07003287 return 2;
James Smartda0436e2009-05-22 14:51:39 -04003288 }
3289
3290 /*
3291 * The driver established a maximum scatter-gather segment count
3292 * during probe that limits the number of sg elements in any
3293 * single scsi command. Just run through the seg_cnt and format
3294 * the sge's.
3295 * When using SLI-3 the driver will try to fit all the BDEs into
3296 * the IOCB. If it can't then the BDEs get added to a BPL as it
3297 * does for SLI-2 mode.
3298 */
James Smartd79c9e92019-08-14 16:57:09 -07003299
3300 /* for tracking segment boundaries */
3301 sgel = scsi_sglist(scsi_cmnd);
3302 j = 2;
3303 for (i = 0; i < nseg; i++) {
3304 sgl->word2 = 0;
3305 if ((num_bde + 1) == nseg) {
James Smartda0436e2009-05-22 14:51:39 -04003306 bf_set(lpfc_sli4_sge_last, sgl, 1);
James Smartd79c9e92019-08-14 16:57:09 -07003307 bf_set(lpfc_sli4_sge_type, sgl,
3308 LPFC_SGE_TYPE_DATA);
3309 } else {
James Smartda0436e2009-05-22 14:51:39 -04003310 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smartd79c9e92019-08-14 16:57:09 -07003311
3312 /* do we need to expand the segment */
3313 if (!lsp_just_set &&
3314 !((j + 1) % phba->border_sge_num) &&
3315 ((nseg - 1) != i)) {
3316 /* set LSP type */
3317 bf_set(lpfc_sli4_sge_type, sgl,
3318 LPFC_SGE_TYPE_LSP);
3319
3320 sgl_xtra = lpfc_get_sgl_per_hdwq(
3321 phba, lpfc_cmd);
3322
3323 if (unlikely(!sgl_xtra)) {
3324 lpfc_cmd->seg_cnt = 0;
3325 scsi_dma_unmap(scsi_cmnd);
3326 return 1;
3327 }
3328 sgl->addr_lo = cpu_to_le32(putPaddrLow(
3329 sgl_xtra->dma_phys_sgl));
3330 sgl->addr_hi = cpu_to_le32(putPaddrHigh(
3331 sgl_xtra->dma_phys_sgl));
3332
3333 } else {
3334 bf_set(lpfc_sli4_sge_type, sgl,
3335 LPFC_SGE_TYPE_DATA);
3336 }
3337 }
3338
3339 if (!(bf_get(lpfc_sli4_sge_type, sgl) &
3340 LPFC_SGE_TYPE_LSP)) {
3341 if ((nseg - 1) == i)
3342 bf_set(lpfc_sli4_sge_last, sgl, 1);
3343
3344 physaddr = sg_dma_address(sgel);
3345 dma_len = sg_dma_len(sgel);
3346 sgl->addr_lo = cpu_to_le32(putPaddrLow(
3347 physaddr));
3348 sgl->addr_hi = cpu_to_le32(putPaddrHigh(
3349 physaddr));
3350
3351 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
3352 sgl->word2 = cpu_to_le32(sgl->word2);
3353 sgl->sge_len = cpu_to_le32(dma_len);
3354
3355 dma_offset += dma_len;
3356 sgel = sg_next(sgel);
3357
3358 sgl++;
3359 lsp_just_set = false;
3360
3361 } else {
3362 sgl->word2 = cpu_to_le32(sgl->word2);
3363 sgl->sge_len = cpu_to_le32(
3364 phba->cfg_sg_dma_buf_size);
3365
3366 sgl = (struct sli4_sge *)sgl_xtra->dma_sgl;
3367 i = i - 1;
3368
3369 lsp_just_set = true;
3370 }
3371
3372 j++;
James Smartda0436e2009-05-22 14:51:39 -04003373 }
James Smart0bc2b7c2018-02-22 08:18:48 -08003374 /*
3375 * Setup the first Payload BDE. For FCoE we just key off
James Smart414abe02018-06-26 08:24:26 -07003376 * Performance Hints, for FC we use lpfc_enable_pbde.
3377 * We populate words 13-15 of IOCB/WQE.
James Smart0bc2b7c2018-02-22 08:18:48 -08003378 */
3379 if ((phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) ||
James Smart414abe02018-06-26 08:24:26 -07003380 phba->cfg_enable_pbde) {
James Smartfedd3b72011-02-16 12:39:24 -05003381 bde = (struct ulp_bde64 *)
James Smartda255e22020-11-15 11:26:42 -08003382 &wqe->words[13];
James Smartfedd3b72011-02-16 12:39:24 -05003383 bde->addrLow = first_data_sgl->addr_lo;
3384 bde->addrHigh = first_data_sgl->addr_hi;
3385 bde->tus.f.bdeSize =
3386 le32_to_cpu(first_data_sgl->sge_len);
3387 bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
3388 bde->tus.w = cpu_to_le32(bde->tus.w);
James Smartda255e22020-11-15 11:26:42 -08003389
3390 } else {
3391 memset(&wqe->words[13], 0, (sizeof(uint32_t) * 3));
James Smartfedd3b72011-02-16 12:39:24 -05003392 }
James Smartda0436e2009-05-22 14:51:39 -04003393 } else {
3394 sgl += 1;
3395 /* clear the last flag in the fcp_rsp map entry */
3396 sgl->word2 = le32_to_cpu(sgl->word2);
3397 bf_set(lpfc_sli4_sge_last, sgl, 1);
3398 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart414abe02018-06-26 08:24:26 -07003399
3400 if ((phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) ||
3401 phba->cfg_enable_pbde) {
3402 bde = (struct ulp_bde64 *)
James Smartda255e22020-11-15 11:26:42 -08003403 &wqe->words[13];
James Smart414abe02018-06-26 08:24:26 -07003404 memset(bde, 0, (sizeof(uint32_t) * 3));
3405 }
James Smartda0436e2009-05-22 14:51:39 -04003406 }
3407
James Smartda255e22020-11-15 11:26:42 -08003408 /* Word 11 */
3409 if (phba->cfg_enable_pbde)
3410 bf_set(wqe_pbde, &wqe->generic.wqe_com, 1);
3411
James Smartda0436e2009-05-22 14:51:39 -04003412 /*
3413 * Finish initializing those IOCB fields that are dependent on the
3414 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
3415 * explicitly reinitialized.
3416 * all iocb memory resources are reused.
3417 */
3418 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
James Smartda255e22020-11-15 11:26:42 -08003419 /* Set first-burst provided it was successfully negotiated */
3420 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
3421 vport->cfg_first_burst_size &&
3422 scsi_cmnd->sc_data_direction == DMA_TO_DEVICE) {
3423 u32 init_len, total_len;
James Smartda0436e2009-05-22 14:51:39 -04003424
James Smartda255e22020-11-15 11:26:42 -08003425 total_len = be32_to_cpu(fcp_cmnd->fcpDl);
3426 init_len = min(total_len, vport->cfg_first_burst_size);
3427
3428 /* Word 4 & 5 */
3429 wqe->fcp_iwrite.initial_xfer_len = init_len;
3430 wqe->fcp_iwrite.total_xfer_len = total_len;
3431 } else {
3432 /* Word 4 */
3433 wqe->fcp_iwrite.total_xfer_len =
3434 be32_to_cpu(fcp_cmnd->fcpDl);
3435 }
James Smart1ba981f2014-02-20 09:56:45 -05003436
3437 /*
3438 * If the OAS driver feature is enabled and the lun is enabled for
3439 * OAS, set the oas iocb related flags.
3440 */
James Smartf38fa0b2014-04-04 13:52:21 -04003441 if ((phba->cfg_fof) && ((struct lpfc_device_data *)
James Smartc92c8412016-07-06 12:36:05 -07003442 scsi_cmnd->device->hostdata)->oas_enabled) {
James Smart9bd2bff52014-09-03 12:57:30 -04003443 lpfc_cmd->cur_iocbq.iocb_flag |= (LPFC_IO_OAS | LPFC_IO_FOF);
James Smartc92c8412016-07-06 12:36:05 -07003444 lpfc_cmd->cur_iocbq.priority = ((struct lpfc_device_data *)
3445 scsi_cmnd->device->hostdata)->priority;
James Smartda255e22020-11-15 11:26:42 -08003446
3447 /* Word 10 */
3448 bf_set(wqe_oas, &wqe->generic.wqe_com, 1);
3449 bf_set(wqe_ccpe, &wqe->generic.wqe_com, 1);
3450
3451 if (lpfc_cmd->cur_iocbq.priority)
3452 bf_set(wqe_ccp, &wqe->generic.wqe_com,
3453 (lpfc_cmd->cur_iocbq.priority << 1));
3454 else
3455 bf_set(wqe_ccp, &wqe->generic.wqe_com,
3456 (phba->cfg_XLanePriority << 1));
James Smartc92c8412016-07-06 12:36:05 -07003457 }
James Smartc4908502019-01-28 11:14:28 -08003458
James Smartda0436e2009-05-22 14:51:39 -04003459 return 0;
3460}
3461
3462/**
James Smartacd68592012-01-18 16:25:09 -05003463 * lpfc_bg_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3464 * @phba: The Hba for which this call is being executed.
3465 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3466 *
3467 * This is the protection/DIF aware version of
3468 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
3469 * two functions eventually, but for now, it's here
James Smart5e0e2312019-08-14 16:56:54 -07003470 * Return codes:
3471 * 2 - Error - Do not retry
3472 * 1 - Error - Retry
3473 * 0 - Success
James Smartacd68592012-01-18 16:25:09 -05003474 **/
3475static int
3476lpfc_bg_scsi_prep_dma_buf_s4(struct lpfc_hba *phba,
James Smartc4908502019-01-28 11:14:28 -08003477 struct lpfc_io_buf *lpfc_cmd)
James Smartacd68592012-01-18 16:25:09 -05003478{
3479 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3480 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart0794d602019-01-28 11:14:19 -08003481 struct sli4_sge *sgl = (struct sli4_sge *)(lpfc_cmd->dma_sgl);
James Smartda255e22020-11-15 11:26:42 -08003482 struct lpfc_iocbq *pwqeq = &lpfc_cmd->cur_iocbq;
3483 union lpfc_wqe128 *wqe = &pwqeq->wqe;
James Smart96f70772013-04-17 20:16:15 -04003484 uint32_t num_sge = 0;
James Smartacd68592012-01-18 16:25:09 -05003485 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
3486 int prot_group_type = 0;
3487 int fcpdl;
James Smart5e0e2312019-08-14 16:56:54 -07003488 int ret = 1;
James Smart7c4042a2018-11-29 16:09:40 -08003489 struct lpfc_vport *vport = phba->pport;
James Smartacd68592012-01-18 16:25:09 -05003490
3491 /*
3492 * Start the lpfc command prep by bumping the sgl beyond fcp_cmnd
James Smart96f70772013-04-17 20:16:15 -04003493 * fcp_rsp regions to the first data sge entry
James Smartacd68592012-01-18 16:25:09 -05003494 */
3495 if (scsi_sg_count(scsi_cmnd)) {
3496 /*
3497 * The driver stores the segment count returned from pci_map_sg
3498 * because this a count of dma-mappings used to map the use_sg
3499 * pages. They are not guaranteed to be the same for those
3500 * architectures that implement an IOMMU.
3501 */
3502 datasegcnt = dma_map_sg(&phba->pcidev->dev,
3503 scsi_sglist(scsi_cmnd),
3504 scsi_sg_count(scsi_cmnd), datadir);
3505 if (unlikely(!datasegcnt))
3506 return 1;
3507
3508 sgl += 1;
3509 /* clear the last flag in the fcp_rsp map entry */
3510 sgl->word2 = le32_to_cpu(sgl->word2);
3511 bf_set(lpfc_sli4_sge_last, sgl, 0);
3512 sgl->word2 = cpu_to_le32(sgl->word2);
3513
3514 sgl += 1;
3515 lpfc_cmd->seg_cnt = datasegcnt;
James Smart96f70772013-04-17 20:16:15 -04003516
3517 /* First check if data segment count from SCSI Layer is good */
James Smartd79c9e92019-08-14 16:57:09 -07003518 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt &&
3519 !phba->cfg_xpsgl) {
James Smart5e0e2312019-08-14 16:56:54 -07003520 WARN_ON_ONCE(lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt);
3521 ret = 2;
James Smart96f70772013-04-17 20:16:15 -04003522 goto err;
James Smart5e0e2312019-08-14 16:56:54 -07003523 }
James Smartacd68592012-01-18 16:25:09 -05003524
3525 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
3526
3527 switch (prot_group_type) {
3528 case LPFC_PG_TYPE_NO_DIF:
James Smart96f70772013-04-17 20:16:15 -04003529 /* Here we need to add a DISEED to the count */
James Smartd79c9e92019-08-14 16:57:09 -07003530 if (((lpfc_cmd->seg_cnt + 1) >
3531 phba->cfg_total_seg_cnt) &&
3532 !phba->cfg_xpsgl) {
James Smart5e0e2312019-08-14 16:56:54 -07003533 ret = 2;
James Smart96f70772013-04-17 20:16:15 -04003534 goto err;
James Smart5e0e2312019-08-14 16:56:54 -07003535 }
James Smart96f70772013-04-17 20:16:15 -04003536
3537 num_sge = lpfc_bg_setup_sgl(phba, scsi_cmnd, sgl,
James Smartd79c9e92019-08-14 16:57:09 -07003538 datasegcnt, lpfc_cmd);
James Smart96f70772013-04-17 20:16:15 -04003539
James Smartacd68592012-01-18 16:25:09 -05003540 /* we should have 2 or more entries in buffer list */
James Smart5e0e2312019-08-14 16:56:54 -07003541 if (num_sge < 2) {
3542 ret = 2;
James Smartacd68592012-01-18 16:25:09 -05003543 goto err;
James Smart5e0e2312019-08-14 16:56:54 -07003544 }
James Smartacd68592012-01-18 16:25:09 -05003545 break;
James Smart96f70772013-04-17 20:16:15 -04003546
3547 case LPFC_PG_TYPE_DIF_BUF:
James Smartacd68592012-01-18 16:25:09 -05003548 /*
3549 * This type indicates that protection buffers are
3550 * passed to the driver, so that needs to be prepared
3551 * for DMA
3552 */
3553 protsegcnt = dma_map_sg(&phba->pcidev->dev,
3554 scsi_prot_sglist(scsi_cmnd),
3555 scsi_prot_sg_count(scsi_cmnd), datadir);
3556 if (unlikely(!protsegcnt)) {
3557 scsi_dma_unmap(scsi_cmnd);
3558 return 1;
3559 }
3560
3561 lpfc_cmd->prot_seg_cnt = protsegcnt;
James Smart96f70772013-04-17 20:16:15 -04003562 /*
3563 * There is a minimun of 3 SGEs used for every
3564 * protection data segment.
3565 */
James Smartd79c9e92019-08-14 16:57:09 -07003566 if (((lpfc_cmd->prot_seg_cnt * 3) >
3567 (phba->cfg_total_seg_cnt - 2)) &&
3568 !phba->cfg_xpsgl) {
James Smart5e0e2312019-08-14 16:56:54 -07003569 ret = 2;
James Smart96f70772013-04-17 20:16:15 -04003570 goto err;
James Smart5e0e2312019-08-14 16:56:54 -07003571 }
James Smartacd68592012-01-18 16:25:09 -05003572
James Smart96f70772013-04-17 20:16:15 -04003573 num_sge = lpfc_bg_setup_sgl_prot(phba, scsi_cmnd, sgl,
James Smartd79c9e92019-08-14 16:57:09 -07003574 datasegcnt, protsegcnt, lpfc_cmd);
James Smart96f70772013-04-17 20:16:15 -04003575
James Smartacd68592012-01-18 16:25:09 -05003576 /* we should have 3 or more entries in buffer list */
James Smartd79c9e92019-08-14 16:57:09 -07003577 if (num_sge < 3 ||
3578 (num_sge > phba->cfg_total_seg_cnt &&
3579 !phba->cfg_xpsgl)) {
James Smart5e0e2312019-08-14 16:56:54 -07003580 ret = 2;
James Smartacd68592012-01-18 16:25:09 -05003581 goto err;
James Smart5e0e2312019-08-14 16:56:54 -07003582 }
James Smartacd68592012-01-18 16:25:09 -05003583 break;
James Smart96f70772013-04-17 20:16:15 -04003584
James Smartacd68592012-01-18 16:25:09 -05003585 case LPFC_PG_TYPE_INVALID:
3586 default:
James Smart96f70772013-04-17 20:16:15 -04003587 scsi_dma_unmap(scsi_cmnd);
3588 lpfc_cmd->seg_cnt = 0;
3589
Dick Kennedy372c1872020-06-30 14:50:00 -07003590 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartacd68592012-01-18 16:25:09 -05003591 "9083 Unexpected protection group %i\n",
3592 prot_group_type);
James Smart5e0e2312019-08-14 16:56:54 -07003593 return 2;
James Smartacd68592012-01-18 16:25:09 -05003594 }
3595 }
3596
James Smart8012cc32012-10-31 14:44:49 -04003597 switch (scsi_get_prot_op(scsi_cmnd)) {
3598 case SCSI_PROT_WRITE_STRIP:
3599 case SCSI_PROT_READ_STRIP:
3600 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_STRIP;
3601 break;
3602 case SCSI_PROT_WRITE_INSERT:
3603 case SCSI_PROT_READ_INSERT:
3604 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_INSERT;
3605 break;
3606 case SCSI_PROT_WRITE_PASS:
3607 case SCSI_PROT_READ_PASS:
3608 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_PASS;
3609 break;
3610 }
3611
James Smartacd68592012-01-18 16:25:09 -05003612 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
James Smartacd68592012-01-18 16:25:09 -05003613 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
3614
James Smartda255e22020-11-15 11:26:42 -08003615 /* Set first-burst provided it was successfully negotiated */
3616 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
3617 vport->cfg_first_burst_size &&
3618 scsi_cmnd->sc_data_direction == DMA_TO_DEVICE) {
3619 u32 init_len, total_len;
James Smartacd68592012-01-18 16:25:09 -05003620
James Smartda255e22020-11-15 11:26:42 -08003621 total_len = be32_to_cpu(fcp_cmnd->fcpDl);
3622 init_len = min(total_len, vport->cfg_first_burst_size);
3623
3624 /* Word 4 & 5 */
3625 wqe->fcp_iwrite.initial_xfer_len = init_len;
3626 wqe->fcp_iwrite.total_xfer_len = total_len;
3627 } else {
3628 /* Word 4 */
3629 wqe->fcp_iwrite.total_xfer_len =
3630 be32_to_cpu(fcp_cmnd->fcpDl);
3631 }
James Smart7c4042a2018-11-29 16:09:40 -08003632
3633 /*
James Smart9bd2bff52014-09-03 12:57:30 -04003634 * If the OAS driver feature is enabled and the lun is enabled for
3635 * OAS, set the oas iocb related flags.
3636 */
3637 if ((phba->cfg_fof) && ((struct lpfc_device_data *)
James Smartda255e22020-11-15 11:26:42 -08003638 scsi_cmnd->device->hostdata)->oas_enabled) {
James Smart9bd2bff52014-09-03 12:57:30 -04003639 lpfc_cmd->cur_iocbq.iocb_flag |= (LPFC_IO_OAS | LPFC_IO_FOF);
3640
James Smartda255e22020-11-15 11:26:42 -08003641 /* Word 10 */
3642 bf_set(wqe_oas, &wqe->generic.wqe_com, 1);
3643 bf_set(wqe_ccpe, &wqe->generic.wqe_com, 1);
3644 bf_set(wqe_ccp, &wqe->generic.wqe_com,
3645 (phba->cfg_XLanePriority << 1));
3646 }
3647
3648 /* Word 7. DIF Flags */
3649 if (lpfc_cmd->cur_iocbq.iocb_flag & LPFC_IO_DIF_PASS)
3650 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
3651 else if (lpfc_cmd->cur_iocbq.iocb_flag & LPFC_IO_DIF_STRIP)
3652 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
3653 else if (lpfc_cmd->cur_iocbq.iocb_flag & LPFC_IO_DIF_INSERT)
3654 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
3655
3656 lpfc_cmd->cur_iocbq.iocb_flag &= ~(LPFC_IO_DIF_PASS |
3657 LPFC_IO_DIF_STRIP | LPFC_IO_DIF_INSERT);
3658
James Smartacd68592012-01-18 16:25:09 -05003659 return 0;
3660err:
James Smart96f70772013-04-17 20:16:15 -04003661 if (lpfc_cmd->seg_cnt)
3662 scsi_dma_unmap(scsi_cmnd);
3663 if (lpfc_cmd->prot_seg_cnt)
3664 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
3665 scsi_prot_sg_count(scsi_cmnd),
3666 scsi_cmnd->sc_data_direction);
3667
Dick Kennedy372c1872020-06-30 14:50:00 -07003668 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart96f70772013-04-17 20:16:15 -04003669 "9084 Cannot setup S/G List for HBA"
3670 "IO segs %d/%d SGL %d SCSI %d: %d %d\n",
3671 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
3672 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
3673 prot_group_type, num_sge);
3674
3675 lpfc_cmd->seg_cnt = 0;
3676 lpfc_cmd->prot_seg_cnt = 0;
James Smart5e0e2312019-08-14 16:56:54 -07003677 return ret;
James Smartacd68592012-01-18 16:25:09 -05003678}
3679
3680/**
James Smart3772a992009-05-22 14:50:54 -04003681 * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3682 * @phba: The Hba for which this call is being executed.
3683 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3684 *
3685 * This routine wraps the actual DMA mapping function pointer from the
3686 * lpfc_hba struct.
3687 *
3688 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04003689 * 1 - Error
3690 * 0 - Success
James Smart3772a992009-05-22 14:50:54 -04003691 **/
3692static inline int
James Smartc4908502019-01-28 11:14:28 -08003693lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
James Smart3772a992009-05-22 14:50:54 -04003694{
3695 return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
3696}
3697
3698/**
James Smartacd68592012-01-18 16:25:09 -05003699 * lpfc_bg_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3700 * using BlockGuard.
3701 * @phba: The Hba for which this call is being executed.
3702 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3703 *
3704 * This routine wraps the actual DMA mapping function pointer from the
3705 * lpfc_hba struct.
3706 *
3707 * Return codes:
3708 * 1 - Error
3709 * 0 - Success
3710 **/
3711static inline int
James Smartc4908502019-01-28 11:14:28 -08003712lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
James Smartacd68592012-01-18 16:25:09 -05003713{
3714 return phba->lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
3715}
3716
3717/**
James Smartda255e22020-11-15 11:26:42 -08003718 * lpfc_scsi_prep_cmnd_buf - Wrapper function for IOCB/WQE mapping of scsi
3719 * buffer
Lee Jones0bb87e02021-03-12 09:47:11 +00003720 * @vport: Pointer to vport object.
James Smartda255e22020-11-15 11:26:42 -08003721 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3722 * @tmo: Timeout value for IO
3723 *
3724 * This routine initializes IOCB/WQE data structure from scsi command
3725 *
3726 * Return codes:
3727 * 1 - Error
3728 * 0 - Success
3729 **/
3730static inline int
3731lpfc_scsi_prep_cmnd_buf(struct lpfc_vport *vport, struct lpfc_io_buf *lpfc_cmd,
3732 uint8_t tmo)
3733{
3734 return vport->phba->lpfc_scsi_prep_cmnd_buf(vport, lpfc_cmd, tmo);
3735}
3736
3737/**
James Smart3621a712009-04-06 18:47:14 -04003738 * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
James Smartea2151b2008-09-07 11:52:10 -04003739 * @phba: Pointer to hba context object.
3740 * @vport: Pointer to vport object.
3741 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
Lee Jones0bb87e02021-03-12 09:47:11 +00003742 * @fcpi_parm: FCP Initiator parameter.
James Smartea2151b2008-09-07 11:52:10 -04003743 *
3744 * This function posts an event when there is a SCSI command reporting
3745 * error from the scsi device.
3746 **/
3747static void
3748lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
James Smart96e209b2020-11-15 11:26:43 -08003749 struct lpfc_io_buf *lpfc_cmd, uint32_t fcpi_parm) {
James Smartea2151b2008-09-07 11:52:10 -04003750 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3751 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
3752 uint32_t resp_info = fcprsp->rspStatus2;
3753 uint32_t scsi_status = fcprsp->rspStatus3;
James Smartea2151b2008-09-07 11:52:10 -04003754 struct lpfc_fast_path_event *fast_path_evt = NULL;
3755 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
3756 unsigned long flags;
3757
James Smart307e3382020-11-15 11:26:30 -08003758 if (!pnode)
James Smart5989b8d2010-10-22 11:06:56 -04003759 return;
3760
James Smartea2151b2008-09-07 11:52:10 -04003761 /* If there is queuefull or busy condition send a scsi event */
3762 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
3763 (cmnd->result == SAM_STAT_BUSY)) {
3764 fast_path_evt = lpfc_alloc_fast_evt(phba);
3765 if (!fast_path_evt)
3766 return;
3767 fast_path_evt->un.scsi_evt.event_type =
3768 FC_REG_SCSI_EVENT;
3769 fast_path_evt->un.scsi_evt.subcategory =
3770 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
3771 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
3772 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
3773 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
3774 &pnode->nlp_portname, sizeof(struct lpfc_name));
3775 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
3776 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3777 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
3778 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
3779 fast_path_evt = lpfc_alloc_fast_evt(phba);
3780 if (!fast_path_evt)
3781 return;
3782 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
3783 FC_REG_SCSI_EVENT;
3784 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
3785 LPFC_EVENT_CHECK_COND;
3786 fast_path_evt->un.check_cond_evt.scsi_event.lun =
3787 cmnd->device->lun;
3788 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
3789 &pnode->nlp_portname, sizeof(struct lpfc_name));
3790 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
3791 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3792 fast_path_evt->un.check_cond_evt.sense_key =
3793 cmnd->sense_buffer[2] & 0xf;
3794 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
3795 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
3796 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3797 fcpi_parm &&
3798 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
3799 ((scsi_status == SAM_STAT_GOOD) &&
3800 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
3801 /*
3802 * If status is good or resid does not match with fcp_param and
3803 * there is valid fcpi_parm, then there is a read_check error
3804 */
3805 fast_path_evt = lpfc_alloc_fast_evt(phba);
3806 if (!fast_path_evt)
3807 return;
3808 fast_path_evt->un.read_check_error.header.event_type =
3809 FC_REG_FABRIC_EVENT;
3810 fast_path_evt->un.read_check_error.header.subcategory =
3811 LPFC_EVENT_FCPRDCHKERR;
3812 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
3813 &pnode->nlp_portname, sizeof(struct lpfc_name));
3814 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
3815 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3816 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
3817 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
3818 fast_path_evt->un.read_check_error.fcpiparam =
3819 fcpi_parm;
3820 } else
3821 return;
3822
3823 fast_path_evt->vport = vport;
3824 spin_lock_irqsave(&phba->hbalock, flags);
3825 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
3826 spin_unlock_irqrestore(&phba->hbalock, flags);
3827 lpfc_worker_wake_up(phba);
3828 return;
3829}
James Smart9bad7672008-12-04 22:39:02 -05003830
3831/**
James Smartf1126682009-06-10 17:22:44 -04003832 * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
James Smart3772a992009-05-22 14:50:54 -04003833 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -05003834 * @psb: The scsi buffer which is going to be un-mapped.
3835 *
3836 * This routine does DMA un-mapping of scatter gather list of scsi command
James Smart3772a992009-05-22 14:50:54 -04003837 * field of @lpfc_cmd for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05003838 **/
dea31012005-04-17 16:05:31 -05003839static void
James Smartc4908502019-01-28 11:14:28 -08003840lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_io_buf *psb)
James Smartbcf4dbf2006-07-06 15:50:08 -04003841{
3842 /*
3843 * There are only two special cases to consider. (1) the scsi command
3844 * requested scatter-gather usage or (2) the scsi command allocated
3845 * a request buffer, but did not request use_sg. There is a third
3846 * case, but it does not require resource deallocation.
3847 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003848 if (psb->seg_cnt > 0)
3849 scsi_dma_unmap(psb->pCmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003850 if (psb->prot_seg_cnt > 0)
3851 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
3852 scsi_prot_sg_count(psb->pCmd),
3853 psb->pCmd->sc_data_direction);
James Smartbcf4dbf2006-07-06 15:50:08 -04003854}
3855
James Smart9bad7672008-12-04 22:39:02 -05003856/**
James Smart02243832021-08-16 09:28:54 -07003857 * lpfc_unblock_requests - allow further commands to be queued.
3858 * @phba: pointer to phba object
3859 *
3860 * For single vport, just call scsi_unblock_requests on physical port.
3861 * For multiple vports, send scsi_unblock_requests for all the vports.
3862 */
3863void
3864lpfc_unblock_requests(struct lpfc_hba *phba)
3865{
3866 struct lpfc_vport **vports;
3867 struct Scsi_Host *shost;
3868 int i;
3869
3870 if (phba->sli_rev == LPFC_SLI_REV4 &&
3871 !phba->sli4_hba.max_cfg_param.vpi_used) {
3872 shost = lpfc_shost_from_vport(phba->pport);
3873 scsi_unblock_requests(shost);
3874 return;
3875 }
3876
3877 vports = lpfc_create_vport_work_array(phba);
3878 if (vports != NULL)
3879 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
3880 shost = lpfc_shost_from_vport(vports[i]);
3881 scsi_unblock_requests(shost);
3882 }
3883 lpfc_destroy_vport_work_array(phba, vports);
3884}
3885
3886/**
3887 * lpfc_block_requests - prevent further commands from being queued.
3888 * @phba: pointer to phba object
3889 *
3890 * For single vport, just call scsi_block_requests on physical port.
3891 * For multiple vports, send scsi_block_requests for all the vports.
3892 */
3893void
3894lpfc_block_requests(struct lpfc_hba *phba)
3895{
3896 struct lpfc_vport **vports;
3897 struct Scsi_Host *shost;
3898 int i;
3899
3900 if (atomic_read(&phba->cmf_stop_io))
3901 return;
3902
3903 if (phba->sli_rev == LPFC_SLI_REV4 &&
3904 !phba->sli4_hba.max_cfg_param.vpi_used) {
3905 shost = lpfc_shost_from_vport(phba->pport);
3906 scsi_block_requests(shost);
3907 return;
3908 }
3909
3910 vports = lpfc_create_vport_work_array(phba);
3911 if (vports != NULL)
3912 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
3913 shost = lpfc_shost_from_vport(vports[i]);
3914 scsi_block_requests(shost);
3915 }
3916 lpfc_destroy_vport_work_array(phba, vports);
3917}
3918
3919/**
3920 * lpfc_update_cmf_cmpl - Adjust CMF counters for IO completion
3921 * @phba: The HBA for which this call is being executed.
3922 * @time: The latency of the IO that completed (in ns)
3923 * @size: The size of the IO that completed
3924 * @shost: SCSI host the IO completed on (NULL for a NVME IO)
3925 *
3926 * The routine adjusts the various Burst and Bandwidth counters used in
3927 * Congestion management and E2E. If time is set to LPFC_CGN_NOT_SENT,
3928 * that means the IO was never issued to the HBA, so this routine is
3929 * just being called to cleanup the counter from a previous
3930 * lpfc_update_cmf_cmd call.
3931 */
3932int
3933lpfc_update_cmf_cmpl(struct lpfc_hba *phba,
3934 uint64_t time, uint32_t size, struct Scsi_Host *shost)
3935{
3936 struct lpfc_cgn_stat *cgs;
3937
3938 if (time != LPFC_CGN_NOT_SENT) {
3939 /* lat is ns coming in, save latency in us */
3940 if (time < 1000)
3941 time = 1;
3942 else
3943 time = div_u64(time + 500, 1000); /* round it */
3944
3945 cgs = this_cpu_ptr(phba->cmf_stat);
3946 atomic64_add(size, &cgs->rcv_bytes);
3947 atomic64_add(time, &cgs->rx_latency);
3948 atomic_inc(&cgs->rx_io_cnt);
3949 }
3950 return 0;
3951}
3952
3953/**
3954 * lpfc_update_cmf_cmd - Adjust CMF counters for IO submission
3955 * @phba: The HBA for which this call is being executed.
3956 * @size: The size of the IO that will be issued
3957 *
3958 * The routine adjusts the various Burst and Bandwidth counters used in
3959 * Congestion management and E2E.
3960 */
3961int
3962lpfc_update_cmf_cmd(struct lpfc_hba *phba, uint32_t size)
3963{
3964 uint64_t total;
3965 struct lpfc_cgn_stat *cgs;
3966 int cpu;
3967
3968 /* At this point we are either LPFC_CFG_MANAGED or LPFC_CFG_MONITOR */
3969 if (phba->cmf_active_mode == LPFC_CFG_MANAGED) {
3970 total = 0;
3971 for_each_present_cpu(cpu) {
3972 cgs = per_cpu_ptr(phba->cmf_stat, cpu);
3973 total += atomic64_read(&cgs->total_bytes);
3974 }
3975 if (total >= phba->cmf_max_bytes_per_interval) {
3976 if (!atomic_xchg(&phba->cmf_bw_wait, 1)) {
3977 lpfc_block_requests(phba);
3978 phba->cmf_last_ts =
3979 lpfc_calc_cmf_latency(phba);
3980 }
3981 atomic_inc(&phba->cmf_busy);
3982 return -EBUSY;
3983 }
3984 }
3985
3986 cgs = this_cpu_ptr(phba->cmf_stat);
3987 atomic64_add(size, &cgs->total_bytes);
3988 return 0;
3989}
3990
3991/**
Lee Jones0bb87e02021-03-12 09:47:11 +00003992 * lpfc_handle_fcp_err - FCP response handler
James Smart9bad7672008-12-04 22:39:02 -05003993 * @vport: The virtual port for which this call is being executed.
James Smartc4908502019-01-28 11:14:28 -08003994 * @lpfc_cmd: Pointer to lpfc_io_buf data structure.
Lee Jones0bb87e02021-03-12 09:47:11 +00003995 * @fcpi_parm: FCP Initiator parameter.
James Smart9bad7672008-12-04 22:39:02 -05003996 *
3997 * This routine is called to process response IOCB with status field
3998 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
3999 * based upon SCSI and FCP error.
4000 **/
James Smartbcf4dbf2006-07-06 15:50:08 -04004001static void
James Smartc4908502019-01-28 11:14:28 -08004002lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_io_buf *lpfc_cmd,
James Smart96e209b2020-11-15 11:26:43 -08004003 uint32_t fcpi_parm)
dea31012005-04-17 16:05:31 -05004004{
4005 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
4006 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
4007 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
dea31012005-04-17 16:05:31 -05004008 uint32_t resp_info = fcprsp->rspStatus2;
4009 uint32_t scsi_status = fcprsp->rspStatus3;
James Smartc7743952006-12-02 13:34:42 -05004010 uint32_t *lp;
dea31012005-04-17 16:05:31 -05004011 uint32_t host_status = DID_OK;
4012 uint32_t rsplen = 0;
James Smart5afab6b2015-12-16 18:12:01 -05004013 uint32_t fcpDl;
James Smartc7743952006-12-02 13:34:42 -05004014 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea31012005-04-17 16:05:31 -05004015
James Smartea2151b2008-09-07 11:52:10 -04004016
dea31012005-04-17 16:05:31 -05004017 /*
4018 * If this is a task management command, there is no
4019 * scsi packet associated with this lpfc_cmd. The driver
4020 * consumes it.
4021 */
4022 if (fcpcmd->fcpCntl2) {
4023 scsi_status = 0;
4024 goto out;
4025 }
4026
James Smart6a9c52c2009-10-02 15:16:51 -04004027 if (resp_info & RSP_LEN_VALID) {
4028 rsplen = be32_to_cpu(fcprsp->rspRspLen);
James Smarte40a02c2010-02-26 14:13:54 -05004029 if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
Dick Kennedy372c1872020-06-30 14:50:00 -07004030 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
4031 "2719 Invalid response length: "
4032 "tgt x%x lun x%llx cmnd x%x rsplen "
4033 "x%x\n", cmnd->device->id,
4034 cmnd->device->lun, cmnd->cmnd[0],
4035 rsplen);
James Smart6a9c52c2009-10-02 15:16:51 -04004036 host_status = DID_ERROR;
4037 goto out;
4038 }
James Smarte40a02c2010-02-26 14:13:54 -05004039 if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
Dick Kennedy372c1872020-06-30 14:50:00 -07004040 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
James Smarte40a02c2010-02-26 14:13:54 -05004041 "2757 Protocol failure detected during "
4042 "processing of FCP I/O op: "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004043 "tgt x%x lun x%llx cmnd x%x rspInfo3 x%x\n",
James Smarte40a02c2010-02-26 14:13:54 -05004044 cmnd->device->id,
4045 cmnd->device->lun, cmnd->cmnd[0],
4046 fcprsp->rspInfo3);
4047 host_status = DID_ERROR;
4048 goto out;
4049 }
James Smart6a9c52c2009-10-02 15:16:51 -04004050 }
4051
James Smartc7743952006-12-02 13:34:42 -05004052 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
4053 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
4054 if (snslen > SCSI_SENSE_BUFFERSIZE)
4055 snslen = SCSI_SENSE_BUFFERSIZE;
4056
4057 if (resp_info & RSP_LEN_VALID)
4058 rsplen = be32_to_cpu(fcprsp->rspRspLen);
4059 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
4060 }
4061 lp = (uint32_t *)cmnd->sense_buffer;
4062
James Smartaa1c7ee2012-08-14 14:26:06 -04004063 /* special handling for under run conditions */
4064 if (!scsi_status && (resp_info & RESID_UNDER)) {
4065 /* don't log under runs if fcp set... */
4066 if (vport->cfg_log_verbose & LOG_FCP)
4067 logit = LOG_FCP_ERROR;
4068 /* unless operator says so */
4069 if (vport->cfg_log_verbose & LOG_FCP_UNDER)
4070 logit = LOG_FCP_UNDER;
4071 }
James Smartc7743952006-12-02 13:34:42 -05004072
James Smarte8b62012007-08-02 11:10:09 -04004073 lpfc_printf_vlog(vport, KERN_WARNING, logit,
James Smarte2a0a9d2008-12-04 22:40:02 -05004074 "9024 FCP command x%x failed: x%x SNS x%x x%x "
James Smarte8b62012007-08-02 11:10:09 -04004075 "Data: x%x x%x x%x x%x x%x\n",
4076 cmnd->cmnd[0], scsi_status,
4077 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
4078 be32_to_cpu(fcprsp->rspResId),
4079 be32_to_cpu(fcprsp->rspSnsLen),
4080 be32_to_cpu(fcprsp->rspRspLen),
4081 fcprsp->rspInfo3);
dea31012005-04-17 16:05:31 -05004082
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05004083 scsi_set_resid(cmnd, 0);
James Smart5afab6b2015-12-16 18:12:01 -05004084 fcpDl = be32_to_cpu(fcpcmd->fcpDl);
dea31012005-04-17 16:05:31 -05004085 if (resp_info & RESID_UNDER) {
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05004086 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea31012005-04-17 16:05:31 -05004087
James Smart73d91e52011-10-10 21:32:10 -04004088 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_UNDER,
James Smart45634a862018-01-30 15:59:00 -08004089 "9025 FCP Underrun, expected %d, "
James Smarte8b62012007-08-02 11:10:09 -04004090 "residual %d Data: x%x x%x x%x\n",
James Smart5afab6b2015-12-16 18:12:01 -05004091 fcpDl,
James Smarte8b62012007-08-02 11:10:09 -04004092 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
4093 cmnd->underflow);
dea31012005-04-17 16:05:31 -05004094
4095 /*
James Smart45634a862018-01-30 15:59:00 -08004096 * If there is an under run, check if under run reported by
James Smart7054a602007-04-25 09:52:34 -04004097 * storage array is same as the under run reported by HBA.
4098 * If this is not same, there is a dropped frame.
4099 */
James Smart45634a862018-01-30 15:59:00 -08004100 if (fcpi_parm && (scsi_get_resid(cmnd) != fcpi_parm)) {
James Smarte8b62012007-08-02 11:10:09 -04004101 lpfc_printf_vlog(vport, KERN_WARNING,
4102 LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05004103 "9026 FCP Read Check Error "
James Smarte8b62012007-08-02 11:10:09 -04004104 "and Underrun Data: x%x x%x x%x x%x\n",
James Smart5afab6b2015-12-16 18:12:01 -05004105 fcpDl,
James Smarte8b62012007-08-02 11:10:09 -04004106 scsi_get_resid(cmnd), fcpi_parm,
4107 cmnd->cmnd[0]);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05004108 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
James Smart7054a602007-04-25 09:52:34 -04004109 host_status = DID_ERROR;
4110 }
4111 /*
dea31012005-04-17 16:05:31 -05004112 * The cmnd->underflow is the minimum number of bytes that must
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004113 * be transferred for this command. Provided a sense condition
dea31012005-04-17 16:05:31 -05004114 * is not present, make sure the actual amount transferred is at
4115 * least the underflow value or fail.
4116 */
4117 if (!(resp_info & SNS_LEN_VALID) &&
4118 (scsi_status == SAM_STAT_GOOD) &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05004119 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
4120 < cmnd->underflow)) {
James Smarte8b62012007-08-02 11:10:09 -04004121 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05004122 "9027 FCP command x%x residual "
James Smarte8b62012007-08-02 11:10:09 -04004123 "underrun converted to error "
4124 "Data: x%x x%x x%x\n",
James Smart66dbfbe2007-08-05 06:08:38 -04004125 cmnd->cmnd[0], scsi_bufflen(cmnd),
James Smarte8b62012007-08-02 11:10:09 -04004126 scsi_get_resid(cmnd), cmnd->underflow);
dea31012005-04-17 16:05:31 -05004127 host_status = DID_ERROR;
4128 }
4129 } else if (resp_info & RESID_OVER) {
James Smarte8b62012007-08-02 11:10:09 -04004130 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05004131 "9028 FCP command x%x residual overrun error. "
James Smarte4e74272009-07-19 10:01:38 -04004132 "Data: x%x x%x\n", cmnd->cmnd[0],
James Smarte8b62012007-08-02 11:10:09 -04004133 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea31012005-04-17 16:05:31 -05004134 host_status = DID_ERROR;
4135
4136 /*
4137 * Check SLI validation that all the transfer was actually done
James Smart26373d22013-09-06 12:19:17 -04004138 * (fcpi_parm should be zero). Apply check only to reads.
dea31012005-04-17 16:05:31 -05004139 */
James Smart5afab6b2015-12-16 18:12:01 -05004140 } else if (fcpi_parm) {
James Smarte8b62012007-08-02 11:10:09 -04004141 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
James Smart96e209b2020-11-15 11:26:43 -08004142 "9029 FCP %s Check Error Data: "
James Smarteee88772010-09-29 11:19:08 -04004143 "x%x x%x x%x x%x x%x\n",
James Smart5afab6b2015-12-16 18:12:01 -05004144 ((cmnd->sc_data_direction == DMA_FROM_DEVICE) ?
4145 "Read" : "Write"),
James Smart5afab6b2015-12-16 18:12:01 -05004146 fcpDl, be32_to_cpu(fcprsp->rspResId),
James Smarteee88772010-09-29 11:19:08 -04004147 fcpi_parm, cmnd->cmnd[0], scsi_status);
James Smart5afab6b2015-12-16 18:12:01 -05004148
4149 /* There is some issue with the LPe12000 that causes it
4150 * to miscalculate the fcpi_parm and falsely trip this
4151 * recovery logic. Detect this case and don't error when true.
4152 */
4153 if (fcpi_parm > fcpDl)
4154 goto out;
4155
James Smarteee88772010-09-29 11:19:08 -04004156 switch (scsi_status) {
4157 case SAM_STAT_GOOD:
4158 case SAM_STAT_CHECK_CONDITION:
4159 /* Fabric dropped a data frame. Fail any successful
4160 * command in which we detected dropped frames.
4161 * A status of good or some check conditions could
4162 * be considered a successful command.
4163 */
4164 host_status = DID_ERROR;
4165 break;
4166 }
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05004167 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea31012005-04-17 16:05:31 -05004168 }
4169
4170 out:
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02004171 cmnd->result = host_status << 16 | scsi_status;
James Smart96e209b2020-11-15 11:26:43 -08004172 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, fcpi_parm);
4173}
4174
4175/**
4176 * lpfc_fcp_io_cmd_wqe_cmpl - Complete a FCP IO
4177 * @phba: The hba for which this call is being executed.
4178 * @pwqeIn: The command WQE for the scsi cmnd.
Lee Jones0bb87e02021-03-12 09:47:11 +00004179 * @wcqe: Pointer to driver response CQE object.
James Smart96e209b2020-11-15 11:26:43 -08004180 *
4181 * This routine assigns scsi command result by looking into response WQE
4182 * status field appropriately. This routine handles QUEUE FULL condition as
4183 * well by ramping down device queue depth.
4184 **/
4185static void
4186lpfc_fcp_io_cmd_wqe_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeIn,
4187 struct lpfc_wcqe_complete *wcqe)
4188{
4189 struct lpfc_io_buf *lpfc_cmd =
4190 (struct lpfc_io_buf *)pwqeIn->context1;
4191 struct lpfc_vport *vport = pwqeIn->vport;
Colin Ian King1e7dddb22020-11-18 13:13:45 +00004192 struct lpfc_rport_data *rdata;
4193 struct lpfc_nodelist *ndlp;
James Smart96e209b2020-11-15 11:26:43 -08004194 struct scsi_cmnd *cmd;
4195 unsigned long flags;
4196 struct lpfc_fast_path_event *fast_path_evt;
4197 struct Scsi_Host *shost;
4198 u32 logit = LOG_FCP;
4199 u32 status, idx;
4200 unsigned long iflags = 0;
James Smart02243832021-08-16 09:28:54 -07004201 u32 lat;
James Smart3e49af92021-05-14 12:55:57 -07004202 u8 wait_xb_clr = 0;
James Smart96e209b2020-11-15 11:26:43 -08004203
4204 /* Sanity check on return of outstanding command */
4205 if (!lpfc_cmd) {
4206 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
4207 "9032 Null lpfc_cmd pointer. No "
4208 "release, skip completion\n");
4209 return;
4210 }
4211
Colin Ian King1e7dddb22020-11-18 13:13:45 +00004212 rdata = lpfc_cmd->rdata;
4213 ndlp = rdata->pnode;
4214
James Smart96e209b2020-11-15 11:26:43 -08004215 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
4216 /* TOREMOVE - currently this flag is checked during
4217 * the release of lpfc_iocbq. Remove once we move
4218 * to lpfc_wqe_job construct.
4219 *
4220 * This needs to be done outside buf_lock
4221 */
4222 spin_lock_irqsave(&phba->hbalock, iflags);
4223 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_EXCHANGE_BUSY;
4224 spin_unlock_irqrestore(&phba->hbalock, iflags);
4225 }
4226
4227 /* Guard against abort handler being called at same time */
4228 spin_lock(&lpfc_cmd->buf_lock);
4229
4230 /* Sanity check on return of outstanding command */
4231 cmd = lpfc_cmd->pCmd;
James Smartae960d72021-03-01 09:18:07 -08004232 if (!cmd) {
James Smart96e209b2020-11-15 11:26:43 -08004233 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
4234 "9042 I/O completion: Not an active IO\n");
4235 spin_unlock(&lpfc_cmd->buf_lock);
4236 lpfc_release_scsi_buf(phba, lpfc_cmd);
4237 return;
4238 }
4239 idx = lpfc_cmd->cur_iocbq.hba_wqidx;
4240 if (phba->sli4_hba.hdwq)
4241 phba->sli4_hba.hdwq[idx].scsi_cstat.io_cmpls++;
4242
4243#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4244 if (unlikely(phba->hdwqstat_on & LPFC_CHECK_SCSI_IO))
4245 this_cpu_inc(phba->sli4_hba.c_stat->cmpl_io);
4246#endif
4247 shost = cmd->device->host;
4248
4249 status = bf_get(lpfc_wcqe_c_status, wcqe);
4250 lpfc_cmd->status = (status & LPFC_IOCB_STATUS_MASK);
4251 lpfc_cmd->result = (wcqe->parameter & IOERR_PARAM_MASK);
4252
4253 lpfc_cmd->flags &= ~LPFC_SBUF_XBUSY;
James Smart3e49af92021-05-14 12:55:57 -07004254 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
James Smart96e209b2020-11-15 11:26:43 -08004255 lpfc_cmd->flags |= LPFC_SBUF_XBUSY;
James Smart3e49af92021-05-14 12:55:57 -07004256 if (phba->cfg_fcp_wait_abts_rsp)
4257 wait_xb_clr = 1;
4258 }
James Smart96e209b2020-11-15 11:26:43 -08004259
4260#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4261 if (lpfc_cmd->prot_data_type) {
4262 struct scsi_dif_tuple *src = NULL;
4263
4264 src = (struct scsi_dif_tuple *)lpfc_cmd->prot_data_segment;
4265 /*
4266 * Used to restore any changes to protection
4267 * data for error injection.
4268 */
4269 switch (lpfc_cmd->prot_data_type) {
4270 case LPFC_INJERR_REFTAG:
4271 src->ref_tag =
4272 lpfc_cmd->prot_data;
4273 break;
4274 case LPFC_INJERR_APPTAG:
4275 src->app_tag =
4276 (uint16_t)lpfc_cmd->prot_data;
4277 break;
4278 case LPFC_INJERR_GUARD:
4279 src->guard_tag =
4280 (uint16_t)lpfc_cmd->prot_data;
4281 break;
4282 default:
4283 break;
4284 }
4285
4286 lpfc_cmd->prot_data = 0;
4287 lpfc_cmd->prot_data_type = 0;
4288 lpfc_cmd->prot_data_segment = NULL;
4289 }
4290#endif
4291 if (unlikely(lpfc_cmd->status)) {
4292 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
4293 (lpfc_cmd->result & IOERR_DRVR_MASK))
4294 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
4295 else if (lpfc_cmd->status >= IOSTAT_CNT)
4296 lpfc_cmd->status = IOSTAT_DEFAULT;
4297 if (lpfc_cmd->status == IOSTAT_FCP_RSP_ERROR &&
4298 !lpfc_cmd->fcp_rsp->rspStatus3 &&
4299 (lpfc_cmd->fcp_rsp->rspStatus2 & RESID_UNDER) &&
4300 !(vport->cfg_log_verbose & LOG_FCP_UNDER))
4301 logit = 0;
4302 else
4303 logit = LOG_FCP | LOG_FCP_UNDER;
4304 lpfc_printf_vlog(vport, KERN_WARNING, logit,
4305 "9034 FCP cmd x%x failed <%d/%lld> "
4306 "status: x%x result: x%x "
4307 "sid: x%x did: x%x oxid: x%x "
4308 "Data: x%x x%x x%x\n",
4309 cmd->cmnd[0],
4310 cmd->device ? cmd->device->id : 0xffff,
4311 cmd->device ? cmd->device->lun : 0xffff,
4312 lpfc_cmd->status, lpfc_cmd->result,
4313 vport->fc_myDID,
4314 (ndlp) ? ndlp->nlp_DID : 0,
4315 lpfc_cmd->cur_iocbq.sli4_xritag,
4316 wcqe->parameter, wcqe->total_data_placed,
4317 lpfc_cmd->cur_iocbq.iotag);
4318 }
4319
4320 switch (lpfc_cmd->status) {
4321 case IOSTAT_SUCCESS:
4322 cmd->result = DID_OK << 16;
4323 break;
4324 case IOSTAT_FCP_RSP_ERROR:
4325 lpfc_handle_fcp_err(vport, lpfc_cmd,
4326 pwqeIn->wqe.fcp_iread.total_xfer_len -
4327 wcqe->total_data_placed);
4328 break;
4329 case IOSTAT_NPORT_BSY:
4330 case IOSTAT_FABRIC_BSY:
4331 cmd->result = DID_TRANSPORT_DISRUPTED << 16;
4332 fast_path_evt = lpfc_alloc_fast_evt(phba);
4333 if (!fast_path_evt)
4334 break;
4335 fast_path_evt->un.fabric_evt.event_type =
4336 FC_REG_FABRIC_EVENT;
4337 fast_path_evt->un.fabric_evt.subcategory =
4338 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
4339 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
4340 if (ndlp) {
4341 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
4342 &ndlp->nlp_portname,
4343 sizeof(struct lpfc_name));
4344 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
4345 &ndlp->nlp_nodename,
4346 sizeof(struct lpfc_name));
4347 }
4348 fast_path_evt->vport = vport;
4349 fast_path_evt->work_evt.evt =
4350 LPFC_EVT_FASTPATH_MGMT_EVT;
4351 spin_lock_irqsave(&phba->hbalock, flags);
4352 list_add_tail(&fast_path_evt->work_evt.evt_listp,
4353 &phba->work_list);
4354 spin_unlock_irqrestore(&phba->hbalock, flags);
4355 lpfc_worker_wake_up(phba);
4356 lpfc_printf_vlog(vport, KERN_WARNING, logit,
4357 "9035 Fabric/Node busy FCP cmd x%x failed"
4358 " <%d/%lld> "
4359 "status: x%x result: x%x "
4360 "sid: x%x did: x%x oxid: x%x "
4361 "Data: x%x x%x x%x\n",
4362 cmd->cmnd[0],
4363 cmd->device ? cmd->device->id : 0xffff,
4364 cmd->device ? cmd->device->lun : 0xffff,
4365 lpfc_cmd->status, lpfc_cmd->result,
4366 vport->fc_myDID,
4367 (ndlp) ? ndlp->nlp_DID : 0,
4368 lpfc_cmd->cur_iocbq.sli4_xritag,
4369 wcqe->parameter,
4370 wcqe->total_data_placed,
4371 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
4372 break;
4373 case IOSTAT_REMOTE_STOP:
4374 if (ndlp) {
4375 /* This I/O was aborted by the target, we don't
4376 * know the rxid and because we did not send the
4377 * ABTS we cannot generate and RRQ.
4378 */
4379 lpfc_set_rrq_active(phba, ndlp,
4380 lpfc_cmd->cur_iocbq.sli4_lxritag,
4381 0, 0);
4382 }
4383 fallthrough;
4384 case IOSTAT_LOCAL_REJECT:
4385 if (lpfc_cmd->result & IOERR_DRVR_MASK)
4386 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
4387 if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
4388 lpfc_cmd->result ==
4389 IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
4390 lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
4391 lpfc_cmd->result ==
4392 IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
4393 cmd->result = DID_NO_CONNECT << 16;
4394 break;
4395 }
4396 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
4397 lpfc_cmd->result == IOERR_NO_RESOURCES ||
4398 lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
4399 lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
4400 cmd->result = DID_REQUEUE << 16;
4401 break;
4402 }
4403 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
4404 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
4405 status == CQE_STATUS_DI_ERROR) {
4406 if (scsi_get_prot_op(cmd) !=
4407 SCSI_PROT_NORMAL) {
4408 /*
4409 * This is a response for a BG enabled
4410 * cmd. Parse BG error
4411 */
4412 lpfc_sli4_parse_bg_err(phba, lpfc_cmd,
4413 wcqe);
4414 break;
4415 }
4416 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
4417 "9040 non-zero BGSTAT on unprotected cmd\n");
4418 }
4419 lpfc_printf_vlog(vport, KERN_WARNING, logit,
4420 "9036 Local Reject FCP cmd x%x failed"
4421 " <%d/%lld> "
4422 "status: x%x result: x%x "
4423 "sid: x%x did: x%x oxid: x%x "
4424 "Data: x%x x%x x%x\n",
4425 cmd->cmnd[0],
4426 cmd->device ? cmd->device->id : 0xffff,
4427 cmd->device ? cmd->device->lun : 0xffff,
4428 lpfc_cmd->status, lpfc_cmd->result,
4429 vport->fc_myDID,
4430 (ndlp) ? ndlp->nlp_DID : 0,
4431 lpfc_cmd->cur_iocbq.sli4_xritag,
4432 wcqe->parameter,
4433 wcqe->total_data_placed,
4434 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
4435 fallthrough;
4436 default:
4437 if (lpfc_cmd->status >= IOSTAT_CNT)
4438 lpfc_cmd->status = IOSTAT_DEFAULT;
4439 cmd->result = DID_ERROR << 16;
4440 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
4441 "9037 FCP Completion Error: xri %x "
4442 "status x%x result x%x [x%x] "
4443 "placed x%x\n",
4444 lpfc_cmd->cur_iocbq.sli4_xritag,
4445 lpfc_cmd->status, lpfc_cmd->result,
4446 wcqe->parameter,
4447 wcqe->total_data_placed);
4448 }
4449 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
4450 u32 *lp = (u32 *)cmd->sense_buffer;
4451
4452 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smartf1156122021-04-11 18:31:24 -07004453 "9039 Iodone <%d/%llu> cmd x%px, error "
James Smart96e209b2020-11-15 11:26:43 -08004454 "x%x SNS x%x x%x Data: x%x x%x\n",
4455 cmd->device->id, cmd->device->lun, cmd,
4456 cmd->result, *lp, *(lp + 3), cmd->retries,
4457 scsi_get_resid(cmd));
4458 }
4459
4460 lpfc_update_stats(vport, lpfc_cmd);
4461
4462 if (vport->cfg_max_scsicmpl_time &&
4463 time_after(jiffies, lpfc_cmd->start_time +
4464 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
4465 spin_lock_irqsave(shost->host_lock, flags);
4466 if (ndlp) {
4467 if (ndlp->cmd_qdepth >
4468 atomic_read(&ndlp->cmd_pending) &&
4469 (atomic_read(&ndlp->cmd_pending) >
4470 LPFC_MIN_TGT_QDEPTH) &&
4471 (cmd->cmnd[0] == READ_10 ||
4472 cmd->cmnd[0] == WRITE_10))
4473 ndlp->cmd_qdepth =
4474 atomic_read(&ndlp->cmd_pending);
4475
4476 ndlp->last_change_time = jiffies;
4477 }
4478 spin_unlock_irqrestore(shost->host_lock, flags);
4479 }
4480 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
4481
4482#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4483 if (lpfc_cmd->ts_cmd_start) {
4484 lpfc_cmd->ts_isr_cmpl = lpfc_cmd->cur_iocbq.isr_timestamp;
4485 lpfc_cmd->ts_data_io = ktime_get_ns();
4486 phba->ktime_last_cmd = lpfc_cmd->ts_data_io;
4487 lpfc_io_ktime(phba, lpfc_cmd);
4488 }
4489#endif
James Smart02243832021-08-16 09:28:54 -07004490 if (likely(!wait_xb_clr))
4491 lpfc_cmd->pCmd = NULL;
4492 spin_unlock(&lpfc_cmd->buf_lock);
4493
4494 /* Check if IO qualified for CMF */
4495 if (phba->cmf_active_mode != LPFC_CFG_OFF &&
4496 cmd->sc_data_direction == DMA_FROM_DEVICE &&
4497 (scsi_sg_count(cmd))) {
4498 /* Used when calculating average latency */
4499 lat = ktime_get_ns() - lpfc_cmd->rx_cmd_start;
4500 lpfc_update_cmf_cmpl(phba, lat, scsi_bufflen(cmd), shost);
4501 }
4502
James Smart3e49af92021-05-14 12:55:57 -07004503 if (wait_xb_clr)
4504 goto out;
James Smart96e209b2020-11-15 11:26:43 -08004505
4506 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
4507 cmd->scsi_done(cmd);
4508
4509 /*
4510 * If there is an abort thread waiting for command completion
4511 * wake up the thread.
4512 */
4513 spin_lock(&lpfc_cmd->buf_lock);
4514 lpfc_cmd->cur_iocbq.iocb_flag &= ~LPFC_DRIVER_ABORTED;
4515 if (lpfc_cmd->waitq)
4516 wake_up(lpfc_cmd->waitq);
4517 spin_unlock(&lpfc_cmd->buf_lock);
James Smart02243832021-08-16 09:28:54 -07004518out:
James Smart96e209b2020-11-15 11:26:43 -08004519 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004520}
4521
James Smart9bad7672008-12-04 22:39:02 -05004522/**
James Smart3621a712009-04-06 18:47:14 -04004523 * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
James Smart9bad7672008-12-04 22:39:02 -05004524 * @phba: The Hba for which this call is being executed.
4525 * @pIocbIn: The command IOCBQ for the scsi cmnd.
James Smart3772a992009-05-22 14:50:54 -04004526 * @pIocbOut: The response IOCBQ for the scsi cmnd.
James Smart9bad7672008-12-04 22:39:02 -05004527 *
4528 * This routine assigns scsi command result by looking into response IOCB
4529 * status field appropriately. This routine handles QUEUE FULL condition as
4530 * well by ramping down device queue depth.
4531 **/
dea31012005-04-17 16:05:31 -05004532static void
4533lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
4534 struct lpfc_iocbq *pIocbOut)
4535{
James Smartc4908502019-01-28 11:14:28 -08004536 struct lpfc_io_buf *lpfc_cmd =
4537 (struct lpfc_io_buf *) pIocbIn->context1;
James Smart2e0fef82007-06-17 19:56:36 -05004538 struct lpfc_vport *vport = pIocbIn->vport;
dea31012005-04-17 16:05:31 -05004539 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
4540 struct lpfc_nodelist *pnode = rdata->pnode;
James Smart75baf692010-06-08 18:31:21 -04004541 struct scsi_cmnd *cmd;
James Smartfa61a542008-01-11 01:52:42 -05004542 unsigned long flags;
James Smartea2151b2008-09-07 11:52:10 -04004543 struct lpfc_fast_path_event *fast_path_evt;
James Smart75baf692010-06-08 18:31:21 -04004544 struct Scsi_Host *shost;
James Smart4c47efc2019-01-28 11:14:25 -08004545 int idx;
James Smart73d91e52011-10-10 21:32:10 -04004546 uint32_t logit = LOG_FCP;
dea31012005-04-17 16:05:31 -05004547
James Smartc2017262019-01-28 11:14:37 -08004548 /* Guard against abort handler being called at same time */
4549 spin_lock(&lpfc_cmd->buf_lock);
4550
James Smart75baf692010-06-08 18:31:21 -04004551 /* Sanity check on return of outstanding command */
James Smart75baf692010-06-08 18:31:21 -04004552 cmd = lpfc_cmd->pCmd;
James Smart43bfea12019-09-21 20:58:57 -07004553 if (!cmd || !phba) {
Dick Kennedy372c1872020-06-30 14:50:00 -07004554 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
James Smartc2017262019-01-28 11:14:37 -08004555 "2621 IO completion: Not an active IO\n");
4556 spin_unlock(&lpfc_cmd->buf_lock);
James Smartc90261d2015-12-16 18:11:57 -05004557 return;
James Smartc2017262019-01-28 11:14:37 -08004558 }
James Smart4c47efc2019-01-28 11:14:25 -08004559
4560 idx = lpfc_cmd->cur_iocbq.hba_wqidx;
4561 if (phba->sli4_hba.hdwq)
4562 phba->sli4_hba.hdwq[idx].scsi_cstat.io_cmpls++;
4563
James Smart6a828b02019-01-28 11:14:31 -08004564#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart840eda92020-03-22 11:13:00 -07004565 if (unlikely(phba->hdwqstat_on & LPFC_CHECK_SCSI_IO))
4566 this_cpu_inc(phba->sli4_hba.c_stat->cmpl_io);
James Smart6a828b02019-01-28 11:14:31 -08004567#endif
James Smart75baf692010-06-08 18:31:21 -04004568 shost = cmd->device->host;
4569
James Smarte3d2b802012-08-14 14:25:43 -04004570 lpfc_cmd->result = (pIocbOut->iocb.un.ulpWord[4] & IOERR_PARAM_MASK);
dea31012005-04-17 16:05:31 -05004571 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
James Smart3e49af92021-05-14 12:55:57 -07004572 /* pick up SLI4 exchange busy status from HBA */
4573 lpfc_cmd->flags &= ~LPFC_SBUF_XBUSY;
James Smart324e1c42019-10-18 14:18:21 -07004574 if (pIocbOut->iocb_flag & LPFC_EXCHANGE_BUSY)
4575 lpfc_cmd->flags |= LPFC_SBUF_XBUSY;
James Smart341af102010-01-26 23:07:37 -05004576
James Smart9a6b09c2012-03-01 22:37:42 -05004577#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4578 if (lpfc_cmd->prot_data_type) {
4579 struct scsi_dif_tuple *src = NULL;
4580
4581 src = (struct scsi_dif_tuple *)lpfc_cmd->prot_data_segment;
4582 /*
4583 * Used to restore any changes to protection
4584 * data for error injection.
4585 */
4586 switch (lpfc_cmd->prot_data_type) {
4587 case LPFC_INJERR_REFTAG:
4588 src->ref_tag =
4589 lpfc_cmd->prot_data;
4590 break;
4591 case LPFC_INJERR_APPTAG:
4592 src->app_tag =
4593 (uint16_t)lpfc_cmd->prot_data;
4594 break;
4595 case LPFC_INJERR_GUARD:
4596 src->guard_tag =
4597 (uint16_t)lpfc_cmd->prot_data;
4598 break;
4599 default:
4600 break;
4601 }
4602
4603 lpfc_cmd->prot_data = 0;
4604 lpfc_cmd->prot_data_type = 0;
4605 lpfc_cmd->prot_data_segment = NULL;
4606 }
4607#endif
James Smart2ea259e2017-02-12 13:52:27 -08004608
James Smart22770cb2019-10-18 14:18:24 -07004609 if (unlikely(lpfc_cmd->status)) {
dea31012005-04-17 16:05:31 -05004610 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
4611 (lpfc_cmd->result & IOERR_DRVR_MASK))
4612 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
4613 else if (lpfc_cmd->status >= IOSTAT_CNT)
4614 lpfc_cmd->status = IOSTAT_DEFAULT;
James Smartaa1c7ee2012-08-14 14:26:06 -04004615 if (lpfc_cmd->status == IOSTAT_FCP_RSP_ERROR &&
4616 !lpfc_cmd->fcp_rsp->rspStatus3 &&
4617 (lpfc_cmd->fcp_rsp->rspStatus2 & RESID_UNDER) &&
4618 !(vport->cfg_log_verbose & LOG_FCP_UNDER))
James Smart73d91e52011-10-10 21:32:10 -04004619 logit = 0;
4620 else
4621 logit = LOG_FCP | LOG_FCP_UNDER;
4622 lpfc_printf_vlog(vport, KERN_WARNING, logit,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004623 "9030 FCP cmd x%x failed <%d/%lld> "
James Smart5a0d80f2012-05-09 21:18:20 -04004624 "status: x%x result: x%x "
4625 "sid: x%x did: x%x oxid: x%x "
4626 "Data: x%x x%x\n",
James Smart73d91e52011-10-10 21:32:10 -04004627 cmd->cmnd[0],
4628 cmd->device ? cmd->device->id : 0xffff,
4629 cmd->device ? cmd->device->lun : 0xffff,
4630 lpfc_cmd->status, lpfc_cmd->result,
James Smart3bf41ba2013-05-31 17:03:18 -04004631 vport->fc_myDID,
4632 (pnode) ? pnode->nlp_DID : 0,
James Smart5a0d80f2012-05-09 21:18:20 -04004633 phba->sli_rev == LPFC_SLI_REV4 ?
4634 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
James Smart73d91e52011-10-10 21:32:10 -04004635 pIocbOut->iocb.ulpContext,
4636 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
dea31012005-04-17 16:05:31 -05004637
4638 switch (lpfc_cmd->status) {
4639 case IOSTAT_FCP_RSP_ERROR:
4640 /* Call FCP RSP handler to determine result */
James Smart96e209b2020-11-15 11:26:43 -08004641 lpfc_handle_fcp_err(vport, lpfc_cmd,
4642 pIocbOut->iocb.un.fcpi.fcpi_parm);
dea31012005-04-17 16:05:31 -05004643 break;
4644 case IOSTAT_NPORT_BSY:
4645 case IOSTAT_FABRIC_BSY:
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02004646 cmd->result = DID_TRANSPORT_DISRUPTED << 16;
James Smartea2151b2008-09-07 11:52:10 -04004647 fast_path_evt = lpfc_alloc_fast_evt(phba);
4648 if (!fast_path_evt)
4649 break;
4650 fast_path_evt->un.fabric_evt.event_type =
4651 FC_REG_FABRIC_EVENT;
4652 fast_path_evt->un.fabric_evt.subcategory =
4653 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
4654 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
James Smart307e3382020-11-15 11:26:30 -08004655 if (pnode) {
James Smartea2151b2008-09-07 11:52:10 -04004656 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
4657 &pnode->nlp_portname,
4658 sizeof(struct lpfc_name));
4659 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
4660 &pnode->nlp_nodename,
4661 sizeof(struct lpfc_name));
4662 }
4663 fast_path_evt->vport = vport;
4664 fast_path_evt->work_evt.evt =
4665 LPFC_EVT_FASTPATH_MGMT_EVT;
4666 spin_lock_irqsave(&phba->hbalock, flags);
4667 list_add_tail(&fast_path_evt->work_evt.evt_listp,
4668 &phba->work_list);
4669 spin_unlock_irqrestore(&phba->hbalock, flags);
4670 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05004671 break;
James Smart92d7f7b2007-06-17 19:56:38 -05004672 case IOSTAT_LOCAL_REJECT:
James Smart1151e3e2011-02-16 12:39:35 -05004673 case IOSTAT_REMOTE_STOP:
James Smartab56dc22011-02-16 12:39:57 -05004674 if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
4675 lpfc_cmd->result ==
4676 IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
4677 lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
4678 lpfc_cmd->result ==
4679 IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02004680 cmd->result = DID_NO_CONNECT << 16;
James Smartab56dc22011-02-16 12:39:57 -05004681 break;
4682 }
James Smartd7c255b2008-08-24 21:50:00 -04004683 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
James Smart92d7f7b2007-06-17 19:56:38 -05004684 lpfc_cmd->result == IOERR_NO_RESOURCES ||
James Smartb92938b2010-06-07 15:24:12 -04004685 lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
4686 lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02004687 cmd->result = DID_REQUEUE << 16;
James Smart58da1ff2008-04-07 10:15:56 -04004688 break;
James Smarte2a0a9d2008-12-04 22:40:02 -05004689 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004690 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
4691 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
4692 pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
4693 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
4694 /*
4695 * This is a response for a BG enabled
4696 * cmd. Parse BG error
4697 */
4698 lpfc_parse_bg_err(phba, lpfc_cmd,
4699 pIocbOut);
4700 break;
4701 } else {
4702 lpfc_printf_vlog(vport, KERN_WARNING,
4703 LOG_BG,
4704 "9031 non-zero BGSTAT "
James Smart6a9c52c2009-10-02 15:16:51 -04004705 "on unprotected cmd\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05004706 }
4707 }
James Smart1151e3e2011-02-16 12:39:35 -05004708 if ((lpfc_cmd->status == IOSTAT_REMOTE_STOP)
4709 && (phba->sli_rev == LPFC_SLI_REV4)
James Smart307e3382020-11-15 11:26:30 -08004710 && pnode) {
James Smart1151e3e2011-02-16 12:39:35 -05004711 /* This IO was aborted by the target, we don't
4712 * know the rxid and because we did not send the
4713 * ABTS we cannot generate and RRQ.
4714 */
4715 lpfc_set_rrq_active(phba, pnode,
James Smartee0f4fe2012-05-09 21:19:14 -04004716 lpfc_cmd->cur_iocbq.sli4_lxritag,
4717 0, 0);
James Smart1151e3e2011-02-16 12:39:35 -05004718 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05004719 fallthrough;
dea31012005-04-17 16:05:31 -05004720 default:
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02004721 cmd->result = DID_ERROR << 16;
dea31012005-04-17 16:05:31 -05004722 break;
4723 }
4724
James Smart307e3382020-11-15 11:26:30 -08004725 if (!pnode || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02004726 cmd->result = DID_TRANSPORT_DISRUPTED << 16 |
4727 SAM_STAT_BUSY;
James Smartab56dc22011-02-16 12:39:57 -05004728 } else
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02004729 cmd->result = DID_OK << 16;
dea31012005-04-17 16:05:31 -05004730
4731 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
4732 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
4733
James Smarte8b62012007-08-02 11:10:09 -04004734 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smart32350662019-08-14 16:57:06 -07004735 "0710 Iodone <%d/%llu> cmd x%px, error "
James Smarte8b62012007-08-02 11:10:09 -04004736 "x%x SNS x%x x%x Data: x%x x%x\n",
4737 cmd->device->id, cmd->device->lun, cmd,
4738 cmd->result, *lp, *(lp + 3), cmd->retries,
4739 scsi_get_resid(cmd));
dea31012005-04-17 16:05:31 -05004740 }
4741
James Smart9df0a032019-09-21 20:58:54 -07004742 lpfc_update_stats(vport, lpfc_cmd);
James Smart977b5a02008-09-07 11:52:04 -04004743 if (vport->cfg_max_scsicmpl_time &&
4744 time_after(jiffies, lpfc_cmd->start_time +
4745 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
James Smarta257bf92009-04-06 18:48:10 -04004746 spin_lock_irqsave(shost->host_lock, flags);
James Smart307e3382020-11-15 11:26:30 -08004747 if (pnode) {
James Smart109f6ed2008-12-04 22:39:08 -05004748 if (pnode->cmd_qdepth >
4749 atomic_read(&pnode->cmd_pending) &&
4750 (atomic_read(&pnode->cmd_pending) >
4751 LPFC_MIN_TGT_QDEPTH) &&
4752 ((cmd->cmnd[0] == READ_10) ||
4753 (cmd->cmnd[0] == WRITE_10)))
4754 pnode->cmd_qdepth =
4755 atomic_read(&pnode->cmd_pending);
James Smart977b5a02008-09-07 11:52:04 -04004756
James Smart109f6ed2008-12-04 22:39:08 -05004757 pnode->last_change_time = jiffies;
4758 }
James Smarta257bf92009-04-06 18:48:10 -04004759 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart977b5a02008-09-07 11:52:04 -04004760 }
James Smart1dcb58e2007-04-25 09:51:30 -04004761 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James Smarta257bf92009-04-06 18:48:10 -04004762
James Smartc2017262019-01-28 11:14:37 -08004763 lpfc_cmd->pCmd = NULL;
4764 spin_unlock(&lpfc_cmd->buf_lock);
James Smart92e3af62012-08-14 14:26:28 -04004765
James Smart2fcbc562020-03-22 11:13:02 -07004766#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4767 if (lpfc_cmd->ts_cmd_start) {
4768 lpfc_cmd->ts_isr_cmpl = pIocbIn->isr_timestamp;
4769 lpfc_cmd->ts_data_io = ktime_get_ns();
4770 phba->ktime_last_cmd = lpfc_cmd->ts_data_io;
4771 lpfc_io_ktime(phba, lpfc_cmd);
4772 }
4773#endif
James Smart3e49af92021-05-14 12:55:57 -07004774
James Smart89533e92016-10-13 15:06:15 -07004775 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
4776 cmd->scsi_done(cmd);
4777
James Smartfa61a542008-01-11 01:52:42 -05004778 /*
James Smartc2017262019-01-28 11:14:37 -08004779 * If there is an abort thread waiting for command completion
James Smartfa61a542008-01-11 01:52:42 -05004780 * wake up the thread.
4781 */
James Smartc2017262019-01-28 11:14:37 -08004782 spin_lock(&lpfc_cmd->buf_lock);
James Smart4eb01532019-04-03 11:10:34 -07004783 lpfc_cmd->cur_iocbq.iocb_flag &= ~LPFC_DRIVER_ABORTED;
James Smartb9e5a2d2019-05-21 17:49:01 -07004784 if (lpfc_cmd->waitq)
James Smart4eb01532019-04-03 11:10:34 -07004785 wake_up(lpfc_cmd->waitq);
James Smartc2017262019-01-28 11:14:37 -08004786 spin_unlock(&lpfc_cmd->buf_lock);
James Smartfa61a542008-01-11 01:52:42 -05004787
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004788 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004789}
4790
James Smart34b02dc2008-08-24 21:49:55 -04004791/**
James Smartda255e22020-11-15 11:26:42 -08004792 * lpfc_scsi_prep_cmnd_buf_s3 - SLI-3 IOCB init for the IO
Lee Jones0bb87e02021-03-12 09:47:11 +00004793 * @vport: Pointer to vport object.
James Smartda255e22020-11-15 11:26:42 -08004794 * @lpfc_cmd: The scsi buffer which is going to be prep'ed.
4795 * @tmo: timeout value for the IO
Alexey Kardashevskiy8b2564e2015-04-28 18:26:22 +10004796 *
James Smartda255e22020-11-15 11:26:42 -08004797 * Based on the data-direction of the command, initialize IOCB
4798 * in the I/O buffer. Fill in the IOCB fields which are independent
4799 * of the scsi buffer
James Smart9bad7672008-12-04 22:39:02 -05004800 *
James Smartda255e22020-11-15 11:26:42 -08004801 * RETURNS 0 - SUCCESS,
James Smart9bad7672008-12-04 22:39:02 -05004802 **/
James Smartda255e22020-11-15 11:26:42 -08004803static int lpfc_scsi_prep_cmnd_buf_s3(struct lpfc_vport *vport,
4804 struct lpfc_io_buf *lpfc_cmd,
4805 uint8_t tmo)
dea31012005-04-17 16:05:31 -05004806{
James Smartda255e22020-11-15 11:26:42 -08004807 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
4808 struct lpfc_iocbq *piocbq = &lpfc_cmd->cur_iocbq;
dea31012005-04-17 16:05:31 -05004809 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
4810 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smartda255e22020-11-15 11:26:42 -08004811 struct lpfc_nodelist *pnode = lpfc_cmd->ndlp;
dea31012005-04-17 16:05:31 -05004812 int datadir = scsi_cmnd->sc_data_direction;
James Smartda255e22020-11-15 11:26:42 -08004813 u32 fcpdl;
dea31012005-04-17 16:05:31 -05004814
James Smart6acb3482014-04-04 13:51:25 -04004815 piocbq->iocb.un.fcpi.fcpi_XRdy = 0;
James Smart027140e2012-08-03 12:35:44 -04004816
dea31012005-04-17 16:05:31 -05004817 /*
4818 * There are three possibilities here - use scatter-gather segment, use
4819 * the single mapping, or neither. Start the lpfc command prep by
4820 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
4821 * data bde entry.
4822 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05004823 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05004824 if (datadir == DMA_TO_DEVICE) {
4825 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
James Smart182ba752013-07-15 18:34:05 -04004826 iocb_cmd->ulpPU = PARM_READ_CHECK;
James Smart3cb01c52013-07-15 18:35:04 -04004827 if (vport->cfg_first_burst_size &&
4828 (pnode->nlp_flag & NLP_FIRSTBURST)) {
James Smartda255e22020-11-15 11:26:42 -08004829 u32 xrdy_len;
4830
James Smart98bbf5f2013-09-06 12:18:45 -04004831 fcpdl = scsi_bufflen(scsi_cmnd);
James Smartda255e22020-11-15 11:26:42 -08004832 xrdy_len = min(fcpdl,
4833 vport->cfg_first_burst_size);
4834 piocbq->iocb.un.fcpi.fcpi_XRdy = xrdy_len;
James Smart3cb01c52013-07-15 18:35:04 -04004835 }
dea31012005-04-17 16:05:31 -05004836 fcp_cmnd->fcpCntl3 = WRITE_DATA;
dea31012005-04-17 16:05:31 -05004837 } else {
4838 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
4839 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05004840 fcp_cmnd->fcpCntl3 = READ_DATA;
dea31012005-04-17 16:05:31 -05004841 }
4842 } else {
4843 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
4844 iocb_cmd->un.fcpi.fcpi_parm = 0;
4845 iocb_cmd->ulpPU = 0;
4846 fcp_cmnd->fcpCntl3 = 0;
dea31012005-04-17 16:05:31 -05004847 }
James Smartda255e22020-11-15 11:26:42 -08004848
dea31012005-04-17 16:05:31 -05004849 /*
4850 * Finish initializing those IOCB fields that are independent
4851 * of the scsi_cmnd request_buffer
4852 */
4853 piocbq->iocb.ulpContext = pnode->nlp_rpi;
4854 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
4855 piocbq->iocb.ulpFCP2Rcvy = 1;
James Smart09372822008-01-11 01:52:54 -05004856 else
4857 piocbq->iocb.ulpFCP2Rcvy = 0;
dea31012005-04-17 16:05:31 -05004858
4859 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
4860 piocbq->context1 = lpfc_cmd;
James Smartda255e22020-11-15 11:26:42 -08004861 if (!piocbq->iocb_cmpl)
4862 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
4863 piocbq->iocb.ulpTimeout = tmo;
James Smart2e0fef82007-06-17 19:56:36 -05004864 piocbq->vport = vport;
James Smartda255e22020-11-15 11:26:42 -08004865 return 0;
4866}
4867
4868/**
4869 * lpfc_scsi_prep_cmnd_buf_s4 - SLI-4 WQE init for the IO
Lee Jones0bb87e02021-03-12 09:47:11 +00004870 * @vport: Pointer to vport object.
James Smartda255e22020-11-15 11:26:42 -08004871 * @lpfc_cmd: The scsi buffer which is going to be prep'ed.
4872 * @tmo: timeout value for the IO
4873 *
4874 * Based on the data-direction of the command copy WQE template
4875 * to I/O buffer WQE. Fill in the WQE fields which are independent
4876 * of the scsi buffer
4877 *
4878 * RETURNS 0 - SUCCESS,
4879 **/
4880static int lpfc_scsi_prep_cmnd_buf_s4(struct lpfc_vport *vport,
4881 struct lpfc_io_buf *lpfc_cmd,
4882 uint8_t tmo)
4883{
4884 struct lpfc_hba *phba = vport->phba;
4885 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
4886 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
4887 struct lpfc_sli4_hdw_queue *hdwq = NULL;
4888 struct lpfc_iocbq *pwqeq = &lpfc_cmd->cur_iocbq;
4889 struct lpfc_nodelist *pnode = lpfc_cmd->ndlp;
4890 union lpfc_wqe128 *wqe = &pwqeq->wqe;
4891 u16 idx = lpfc_cmd->hdwq_no;
4892 int datadir = scsi_cmnd->sc_data_direction;
4893
4894 hdwq = &phba->sli4_hba.hdwq[idx];
4895
4896 /* Initialize 64 bytes only */
4897 memset(wqe, 0, sizeof(union lpfc_wqe128));
4898
4899 /*
4900 * There are three possibilities here - use scatter-gather segment, use
4901 * the single mapping, or neither.
4902 */
4903 if (scsi_sg_count(scsi_cmnd)) {
4904 if (datadir == DMA_TO_DEVICE) {
4905 /* From the iwrite template, initialize words 7 - 11 */
4906 memcpy(&wqe->words[7],
4907 &lpfc_iwrite_cmd_template.words[7],
4908 sizeof(uint32_t) * 5);
4909
4910 fcp_cmnd->fcpCntl3 = WRITE_DATA;
4911 if (hdwq)
4912 hdwq->scsi_cstat.output_requests++;
4913 } else {
4914 /* From the iread template, initialize words 7 - 11 */
4915 memcpy(&wqe->words[7],
4916 &lpfc_iread_cmd_template.words[7],
4917 sizeof(uint32_t) * 5);
4918
4919 /* Word 7 */
4920 bf_set(wqe_tmo, &wqe->fcp_iread.wqe_com, tmo);
4921
4922 fcp_cmnd->fcpCntl3 = READ_DATA;
4923 if (hdwq)
4924 hdwq->scsi_cstat.input_requests++;
James Smart02243832021-08-16 09:28:54 -07004925
4926 /* For a CMF Managed port, iod must be zero'ed */
4927 if (phba->cmf_active_mode == LPFC_CFG_MANAGED)
4928 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com,
4929 LPFC_WQE_IOD_NONE);
James Smartda255e22020-11-15 11:26:42 -08004930 }
4931 } else {
4932 /* From the icmnd template, initialize words 4 - 11 */
4933 memcpy(&wqe->words[4], &lpfc_icmnd_cmd_template.words[4],
4934 sizeof(uint32_t) * 8);
4935
4936 /* Word 7 */
4937 bf_set(wqe_tmo, &wqe->fcp_icmd.wqe_com, tmo);
4938
4939 fcp_cmnd->fcpCntl3 = 0;
4940 if (hdwq)
4941 hdwq->scsi_cstat.control_requests++;
4942 }
4943
4944 /*
4945 * Finish initializing those WQE fields that are independent
4946 * of the request_buffer
4947 */
4948
4949 /* Word 3 */
4950 bf_set(payload_offset_len, &wqe->fcp_icmd,
4951 sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
4952
4953 /* Word 6 */
4954 bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com,
4955 phba->sli4_hba.rpi_ids[pnode->nlp_rpi]);
4956 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, pwqeq->sli4_xritag);
4957
4958 /* Word 7*/
4959 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
4960 bf_set(wqe_erp, &wqe->generic.wqe_com, 1);
4961
4962 bf_set(wqe_class, &wqe->generic.wqe_com,
4963 (pnode->nlp_fcp_info & 0x0f));
4964
4965 /* Word 8 */
4966 wqe->generic.wqe_com.abort_tag = pwqeq->iotag;
4967
4968 /* Word 9 */
4969 bf_set(wqe_reqtag, &wqe->generic.wqe_com, pwqeq->iotag);
4970
4971 pwqeq->vport = vport;
4972 pwqeq->vport = vport;
4973 pwqeq->context1 = lpfc_cmd;
4974 pwqeq->hba_wqidx = lpfc_cmd->hdwq_no;
James Smart96e209b2020-11-15 11:26:43 -08004975 pwqeq->wqe_cmpl = lpfc_fcp_io_cmd_wqe_cmpl;
James Smartda255e22020-11-15 11:26:42 -08004976
4977 return 0;
4978}
4979
4980/**
4981 * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
4982 * @vport: The virtual port for which this call is being executed.
4983 * @lpfc_cmd: The scsi command which needs to send.
4984 * @pnode: Pointer to lpfc_nodelist.
4985 *
4986 * This routine initializes fcp_cmnd and iocb data structure from scsi command
4987 * to transfer for device with SLI3 interface spec.
4988 **/
4989static int
4990lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_io_buf *lpfc_cmd,
4991 struct lpfc_nodelist *pnode)
4992{
4993 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
4994 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
4995 u8 *ptr;
4996
4997 if (!pnode)
4998 return 0;
4999
5000 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
5001 /* clear task management bits */
5002 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
5003
5004 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
5005 &lpfc_cmd->fcp_cmnd->fcp_lun);
5006
5007 ptr = &fcp_cmnd->fcpCdb[0];
5008 memcpy(ptr, scsi_cmnd->cmnd, scsi_cmnd->cmd_len);
5009 if (scsi_cmnd->cmd_len < LPFC_FCP_CDB_LEN) {
5010 ptr += scsi_cmnd->cmd_len;
5011 memset(ptr, 0, (LPFC_FCP_CDB_LEN - scsi_cmnd->cmd_len));
5012 }
5013
5014 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
5015
5016 lpfc_scsi_prep_cmnd_buf(vport, lpfc_cmd, lpfc_cmd->timeout);
5017
5018 return 0;
dea31012005-04-17 16:05:31 -05005019}
5020
James Smart9bad7672008-12-04 22:39:02 -05005021/**
James Smart6d368e52011-05-24 11:44:12 -04005022 * lpfc_scsi_prep_task_mgmt_cmd - Convert SLI3 scsi TM cmd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05005023 * @vport: The virtual port for which this call is being executed.
James Smartc4908502019-01-28 11:14:28 -08005024 * @lpfc_cmd: Pointer to lpfc_io_buf data structure.
James Smart9bad7672008-12-04 22:39:02 -05005025 * @lun: Logical unit number.
5026 * @task_mgmt_cmd: SCSI task management command.
5027 *
James Smart3772a992009-05-22 14:50:54 -04005028 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
5029 * for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05005030 *
5031 * Return codes:
5032 * 0 - Error
5033 * 1 - Success
5034 **/
dea31012005-04-17 16:05:31 -05005035static int
James Smartf1126682009-06-10 17:22:44 -04005036lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
James Smartc4908502019-01-28 11:14:28 -08005037 struct lpfc_io_buf *lpfc_cmd,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005038 uint64_t lun,
dea31012005-04-17 16:05:31 -05005039 uint8_t task_mgmt_cmd)
5040{
dea31012005-04-17 16:05:31 -05005041 struct lpfc_iocbq *piocbq;
5042 IOCB_t *piocb;
5043 struct fcp_cmnd *fcp_cmnd;
James Smart0b18ac42006-05-01 21:50:40 -04005044 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea31012005-04-17 16:05:31 -05005045 struct lpfc_nodelist *ndlp = rdata->pnode;
5046
James Smart307e3382020-11-15 11:26:30 -08005047 if (!ndlp || ndlp->nlp_state != NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05005048 return 0;
dea31012005-04-17 16:05:31 -05005049
dea31012005-04-17 16:05:31 -05005050 piocbq = &(lpfc_cmd->cur_iocbq);
James Smart2e0fef82007-06-17 19:56:36 -05005051 piocbq->vport = vport;
5052
dea31012005-04-17 16:05:31 -05005053 piocb = &piocbq->iocb;
5054
5055 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart34b02dc2008-08-24 21:49:55 -04005056 /* Clear out any old data in the FCP command area */
5057 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
5058 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05005059 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
James Smarte2a0a9d2008-12-04 22:40:02 -05005060 if (vport->phba->sli_rev == 3 &&
5061 !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04005062 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05005063 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
dea31012005-04-17 16:05:31 -05005064 piocb->ulpContext = ndlp->nlp_rpi;
James Smart6d368e52011-05-24 11:44:12 -04005065 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
5066 piocb->ulpContext =
5067 vport->phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
5068 }
James Smart53151bb2013-10-10 12:24:07 -04005069 piocb->ulpFCP2Rcvy = (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) ? 1 : 0;
dea31012005-04-17 16:05:31 -05005070 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
James Smartf9226c22014-02-20 09:57:28 -05005071 piocb->ulpPU = 0;
5072 piocb->un.fcpi.fcpi_parm = 0;
dea31012005-04-17 16:05:31 -05005073
5074 /* ulpTimeout is only one byte */
5075 if (lpfc_cmd->timeout > 0xff) {
5076 /*
5077 * Do not timeout the command at the firmware level.
5078 * The driver will provide the timeout mechanism.
5079 */
5080 piocb->ulpTimeout = 0;
James Smartf1126682009-06-10 17:22:44 -04005081 } else
dea31012005-04-17 16:05:31 -05005082 piocb->ulpTimeout = lpfc_cmd->timeout;
James Smartf1126682009-06-10 17:22:44 -04005083
5084 if (vport->phba->sli_rev == LPFC_SLI_REV4)
5085 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05005086
James Smart2e0fef82007-06-17 19:56:36 -05005087 return 1;
dea31012005-04-17 16:05:31 -05005088}
5089
James Smart9bad7672008-12-04 22:39:02 -05005090/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005091 * lpfc_scsi_api_table_setup - Set up scsi api function jump table
James Smart3772a992009-05-22 14:50:54 -04005092 * @phba: The hba struct for which this call is being executed.
5093 * @dev_grp: The HBA PCI-Device group number.
5094 *
5095 * This routine sets up the SCSI interface API function jump table in @phba
5096 * struct.
5097 * Returns: 0 - success, -ENODEV - failure.
5098 **/
5099int
5100lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5101{
5102
James Smartf1126682009-06-10 17:22:44 -04005103 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
James Smartf1126682009-06-10 17:22:44 -04005104
James Smart3772a992009-05-22 14:50:54 -04005105 switch (dev_grp) {
5106 case LPFC_PCI_DEV_LP:
James Smart3772a992009-05-22 14:50:54 -04005107 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
James Smartacd68592012-01-18 16:25:09 -05005108 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04005109 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
James Smart19ca7602010-11-20 23:11:55 -05005110 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s3;
James Smartda255e22020-11-15 11:26:42 -08005111 phba->lpfc_scsi_prep_cmnd_buf = lpfc_scsi_prep_cmnd_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04005112 break;
James Smartda0436e2009-05-22 14:51:39 -04005113 case LPFC_PCI_DEV_OC:
James Smartda0436e2009-05-22 14:51:39 -04005114 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
James Smartacd68592012-01-18 16:25:09 -05005115 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04005116 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
James Smart19ca7602010-11-20 23:11:55 -05005117 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s4;
James Smartda255e22020-11-15 11:26:42 -08005118 phba->lpfc_scsi_prep_cmnd_buf = lpfc_scsi_prep_cmnd_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04005119 break;
James Smart3772a992009-05-22 14:50:54 -04005120 default:
Dick Kennedy372c1872020-06-30 14:50:00 -07005121 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -04005122 "1418 Invalid HBA PCI-device group: 0x%x\n",
5123 dev_grp);
5124 return -ENODEV;
James Smart3772a992009-05-22 14:50:54 -04005125 }
James Smart3772a992009-05-22 14:50:54 -04005126 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
James Smart84d1b002010-02-12 14:42:33 -05005127 phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
James Smart3772a992009-05-22 14:50:54 -04005128 return 0;
5129}
5130
5131/**
Lee Jones0bb87e02021-03-12 09:47:11 +00005132 * lpfc_tskmgmt_def_cmpl - IOCB completion routine for task management command
James Smart9bad7672008-12-04 22:39:02 -05005133 * @phba: The Hba for which this call is being executed.
5134 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
5135 * @rspiocbq: Pointer to lpfc_iocbq data structure.
5136 *
5137 * This routine is IOCB completion routine for device reset and target reset
5138 * routine. This routine release scsi buffer associated with lpfc_cmd.
5139 **/
James Smart7054a602007-04-25 09:52:34 -04005140static void
5141lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
5142 struct lpfc_iocbq *cmdiocbq,
5143 struct lpfc_iocbq *rspiocbq)
5144{
James Smartc4908502019-01-28 11:14:28 -08005145 struct lpfc_io_buf *lpfc_cmd =
5146 (struct lpfc_io_buf *) cmdiocbq->context1;
James Smart7054a602007-04-25 09:52:34 -04005147 if (lpfc_cmd)
5148 lpfc_release_scsi_buf(phba, lpfc_cmd);
5149 return;
5150}
5151
James Smart9bad7672008-12-04 22:39:02 -05005152/**
James Smart50212672018-12-13 15:17:57 -08005153 * lpfc_check_pci_resettable - Walks list of devices on pci_dev's bus to check
5154 * if issuing a pci_bus_reset is possibly unsafe
5155 * @phba: lpfc_hba pointer.
5156 *
5157 * Description:
5158 * Walks the bus_list to ensure only PCI devices with Emulex
5159 * vendor id, device ids that support hot reset, and only one occurrence
5160 * of function 0.
5161 *
5162 * Returns:
5163 * -EBADSLT, detected invalid device
5164 * 0, successful
5165 */
5166int
Dick Kennedy372c1872020-06-30 14:50:00 -07005167lpfc_check_pci_resettable(struct lpfc_hba *phba)
James Smart50212672018-12-13 15:17:57 -08005168{
5169 const struct pci_dev *pdev = phba->pcidev;
5170 struct pci_dev *ptr = NULL;
5171 u8 counter = 0;
5172
5173 /* Walk the list of devices on the pci_dev's bus */
5174 list_for_each_entry(ptr, &pdev->bus->devices, bus_list) {
5175 /* Check for Emulex Vendor ID */
5176 if (ptr->vendor != PCI_VENDOR_ID_EMULEX) {
5177 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5178 "8346 Non-Emulex vendor found: "
5179 "0x%04x\n", ptr->vendor);
5180 return -EBADSLT;
5181 }
5182
5183 /* Check for valid Emulex Device ID */
James Smartf6c5e6c2021-07-22 15:17:18 -07005184 if (phba->sli_rev != LPFC_SLI_REV4 ||
5185 phba->hba_flag & HBA_FCOE_MODE) {
James Smart50212672018-12-13 15:17:57 -08005186 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta94a40e2021-03-01 09:18:17 -08005187 "8347 Incapable PCI reset device: "
James Smart50212672018-12-13 15:17:57 -08005188 "0x%04x\n", ptr->device);
5189 return -EBADSLT;
5190 }
5191
5192 /* Check for only one function 0 ID to ensure only one HBA on
5193 * secondary bus
5194 */
5195 if (ptr->devfn == 0) {
5196 if (++counter > 1) {
5197 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5198 "8348 More than one device on "
5199 "secondary bus found\n");
5200 return -EBADSLT;
5201 }
5202 }
5203 }
5204
5205 return 0;
5206}
5207
5208/**
James Smart3621a712009-04-06 18:47:14 -04005209 * lpfc_info - Info entry point of scsi_host_template data structure
James Smart9bad7672008-12-04 22:39:02 -05005210 * @host: The scsi host for which this call is being executed.
5211 *
5212 * This routine provides module information about hba.
5213 *
5214 * Reutrn code:
5215 * Pointer to char - Success.
5216 **/
dea31012005-04-17 16:05:31 -05005217const char *
5218lpfc_info(struct Scsi_Host *host)
5219{
James Smart2e0fef82007-06-17 19:56:36 -05005220 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
5221 struct lpfc_hba *phba = vport->phba;
James Smart50212672018-12-13 15:17:57 -08005222 int link_speed = 0;
5223 static char lpfcinfobuf[384];
5224 char tmp[384] = {0};
dea31012005-04-17 16:05:31 -05005225
James Smart50212672018-12-13 15:17:57 -08005226 memset(lpfcinfobuf, 0, sizeof(lpfcinfobuf));
dea31012005-04-17 16:05:31 -05005227 if (phba && phba->pcidev){
James Smart50212672018-12-13 15:17:57 -08005228 /* Model Description */
5229 scnprintf(tmp, sizeof(tmp), phba->ModelDesc);
5230 if (strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf)) >=
5231 sizeof(lpfcinfobuf))
5232 goto buffer_done;
5233
5234 /* PCI Info */
5235 scnprintf(tmp, sizeof(tmp),
5236 " on PCI bus %02x device %02x irq %d",
5237 phba->pcidev->bus->number, phba->pcidev->devfn,
5238 phba->pcidev->irq);
5239 if (strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf)) >=
5240 sizeof(lpfcinfobuf))
5241 goto buffer_done;
5242
5243 /* Port Number */
dea31012005-04-17 16:05:31 -05005244 if (phba->Port[0]) {
James Smart50212672018-12-13 15:17:57 -08005245 scnprintf(tmp, sizeof(tmp), " port %s", phba->Port);
5246 if (strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf)) >=
5247 sizeof(lpfcinfobuf))
5248 goto buffer_done;
dea31012005-04-17 16:05:31 -05005249 }
James Smart50212672018-12-13 15:17:57 -08005250
5251 /* Link Speed */
James Smarta085e872015-12-16 18:12:02 -05005252 link_speed = lpfc_sli_port_speed_get(phba);
James Smart50212672018-12-13 15:17:57 -08005253 if (link_speed != 0) {
5254 scnprintf(tmp, sizeof(tmp),
5255 " Logical Link Speed: %d Mbps", link_speed);
5256 if (strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf)) >=
5257 sizeof(lpfcinfobuf))
5258 goto buffer_done;
5259 }
5260
5261 /* PCI resettable */
5262 if (!lpfc_check_pci_resettable(phba)) {
5263 scnprintf(tmp, sizeof(tmp), " PCI resettable");
5264 strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf));
5265 }
dea31012005-04-17 16:05:31 -05005266 }
James Smart50212672018-12-13 15:17:57 -08005267
5268buffer_done:
dea31012005-04-17 16:05:31 -05005269 return lpfcinfobuf;
5270}
5271
James Smart9bad7672008-12-04 22:39:02 -05005272/**
Lee Jones0bb87e02021-03-12 09:47:11 +00005273 * lpfc_poll_rearm_timer - Routine to modify fcp_poll timer of hba
James Smart9bad7672008-12-04 22:39:02 -05005274 * @phba: The Hba for which this call is being executed.
5275 *
5276 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
5277 * The default value of cfg_poll_tmo is 10 milliseconds.
5278 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005279static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
5280{
5281 unsigned long poll_tmo_expires =
5282 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
5283
James Smart895427b2017-02-12 13:52:30 -08005284 if (!list_empty(&phba->sli.sli3_ring[LPFC_FCP_RING].txcmplq))
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005285 mod_timer(&phba->fcp_poll_timer,
5286 poll_tmo_expires);
5287}
5288
James Smart9bad7672008-12-04 22:39:02 -05005289/**
James Smart3621a712009-04-06 18:47:14 -04005290 * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
James Smart9bad7672008-12-04 22:39:02 -05005291 * @phba: The Hba for which this call is being executed.
5292 *
5293 * This routine starts the fcp_poll_timer of @phba.
5294 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005295void lpfc_poll_start_timer(struct lpfc_hba * phba)
5296{
5297 lpfc_poll_rearm_timer(phba);
5298}
5299
James Smart9bad7672008-12-04 22:39:02 -05005300/**
James Smart3621a712009-04-06 18:47:14 -04005301 * lpfc_poll_timeout - Restart polling timer
Lee Joneseceee002020-11-02 14:23:43 +00005302 * @t: Timer construct where lpfc_hba data structure pointer is obtained.
James Smart9bad7672008-12-04 22:39:02 -05005303 *
5304 * This routine restarts fcp_poll timer, when FCP ring polling is enable
5305 * and FCP Ring interrupt is disable.
5306 **/
Kees Cookf22eb4d2017-09-06 20:24:26 -07005307void lpfc_poll_timeout(struct timer_list *t)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005308{
Kees Cookf22eb4d2017-09-06 20:24:26 -07005309 struct lpfc_hba *phba = from_timer(phba, t, fcp_poll_timer);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005310
5311 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04005312 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -08005313 &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
James Smart45ed1192009-10-02 15:17:02 -04005314
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005315 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
5316 lpfc_poll_rearm_timer(phba);
5317 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005318}
5319
Gaurav Srivastavadc507152021-06-08 10:05:51 +05305320/*
5321 * lpfc_get_vmid_from_hashtable - search the UUID in the hash table
5322 * @vport: The virtual port for which this call is being executed.
5323 * @hash: calculated hash value
5324 * @buf: uuid associated with the VE
5325 * Return the VMID entry associated with the UUID
5326 * Make sure to acquire the appropriate lock before invoking this routine.
5327 */
5328struct lpfc_vmid *lpfc_get_vmid_from_hashtable(struct lpfc_vport *vport,
5329 u32 hash, u8 *buf)
5330{
5331 struct lpfc_vmid *vmp;
5332
5333 hash_for_each_possible(vport->hash_table, vmp, hnode, hash) {
5334 if (memcmp(&vmp->host_vmid[0], buf, 16) == 0)
5335 return vmp;
5336 }
5337 return NULL;
5338}
5339
5340/*
5341 * lpfc_put_vmid_in_hashtable - put the VMID in the hash table
5342 * @vport: The virtual port for which this call is being executed.
5343 * @hash - calculated hash value
5344 * @vmp: Pointer to a VMID entry representing a VM sending I/O
5345 *
5346 * This routine will insert the newly acquired VMID entity in the hash table.
5347 * Make sure to acquire the appropriate lock before invoking this routine.
5348 */
5349static void
5350lpfc_put_vmid_in_hashtable(struct lpfc_vport *vport, u32 hash,
5351 struct lpfc_vmid *vmp)
5352{
5353 hash_add(vport->hash_table, &vmp->hnode, hash);
5354}
5355
5356/*
5357 * lpfc_vmid_hash_fn - create a hash value of the UUID
5358 * @vmid: uuid associated with the VE
5359 * @len: length of the VMID string
5360 * Returns the calculated hash value
5361 */
5362int lpfc_vmid_hash_fn(const char *vmid, int len)
5363{
5364 int c;
5365 int hash = 0;
5366
5367 if (len == 0)
5368 return 0;
5369 while (len--) {
5370 c = *vmid++;
5371 if (c >= 'A' && c <= 'Z')
5372 c += 'a' - 'A';
5373
5374 hash = (hash + (c << LPFC_VMID_HASH_SHIFT) +
5375 (c >> LPFC_VMID_HASH_SHIFT)) * 19;
5376 }
5377
5378 return hash & LPFC_VMID_HASH_MASK;
5379}
5380
5381/*
5382 * lpfc_vmid_update_entry - update the vmid entry in the hash table
5383 * @vport: The virtual port for which this call is being executed.
5384 * @cmd: address of scsi cmd descriptor
5385 * @vmp: Pointer to a VMID entry representing a VM sending I/O
5386 * @tag: VMID tag
5387 */
5388static void lpfc_vmid_update_entry(struct lpfc_vport *vport, struct scsi_cmnd
5389 *cmd, struct lpfc_vmid *vmp,
5390 union lpfc_vmid_io_tag *tag)
5391{
5392 u64 *lta;
5393
5394 if (vport->vmid_priority_tagging)
5395 tag->cs_ctl_vmid = vmp->un.cs_ctl_vmid;
5396 else
5397 tag->app_id = vmp->un.app_id;
5398
5399 if (cmd->sc_data_direction == DMA_TO_DEVICE)
5400 vmp->io_wr_cnt++;
5401 else
5402 vmp->io_rd_cnt++;
5403
5404 /* update the last access timestamp in the table */
5405 lta = per_cpu_ptr(vmp->last_io_time, raw_smp_processor_id());
5406 *lta = jiffies;
5407}
5408
5409static void lpfc_vmid_assign_cs_ctl(struct lpfc_vport *vport,
5410 struct lpfc_vmid *vmid)
5411{
5412 u32 hash;
5413 struct lpfc_vmid *pvmid;
5414
5415 if (vport->port_type == LPFC_PHYSICAL_PORT) {
5416 vmid->un.cs_ctl_vmid = lpfc_vmid_get_cs_ctl(vport);
5417 } else {
5418 hash = lpfc_vmid_hash_fn(vmid->host_vmid, vmid->vmid_len);
5419 pvmid =
5420 lpfc_get_vmid_from_hashtable(vport->phba->pport, hash,
5421 vmid->host_vmid);
5422 if (pvmid)
5423 vmid->un.cs_ctl_vmid = pvmid->un.cs_ctl_vmid;
5424 else
5425 vmid->un.cs_ctl_vmid = lpfc_vmid_get_cs_ctl(vport);
5426 }
5427}
5428
Gaurav Srivastava33c79742021-06-08 10:05:56 +05305429/*
5430 * lpfc_vmid_get_appid - get the VMID associated with the UUID
5431 * @vport: The virtual port for which this call is being executed.
5432 * @uuid: UUID associated with the VE
5433 * @cmd: address of scsi_cmd descriptor
5434 * @tag: VMID tag
5435 * Returns status of the function
5436 */
5437static int lpfc_vmid_get_appid(struct lpfc_vport *vport, char *uuid, struct
5438 scsi_cmnd * cmd, union lpfc_vmid_io_tag *tag)
5439{
5440 struct lpfc_vmid *vmp = NULL;
5441 int hash, len, rc, i;
5442
5443 /* check if QFPA is complete */
5444 if (lpfc_vmid_is_type_priority_tag(vport) && !(vport->vmid_flag &
5445 LPFC_VMID_QFPA_CMPL)) {
5446 vport->work_port_events |= WORKER_CHECK_VMID_ISSUE_QFPA;
5447 return -EAGAIN;
5448 }
5449
5450 /* search if the UUID has already been mapped to the VMID */
5451 len = strlen(uuid);
5452 hash = lpfc_vmid_hash_fn(uuid, len);
5453
5454 /* search for the VMID in the table */
5455 read_lock(&vport->vmid_lock);
5456 vmp = lpfc_get_vmid_from_hashtable(vport, hash, uuid);
5457
5458 /* if found, check if its already registered */
5459 if (vmp && vmp->flag & LPFC_VMID_REGISTERED) {
5460 read_unlock(&vport->vmid_lock);
5461 lpfc_vmid_update_entry(vport, cmd, vmp, tag);
5462 rc = 0;
5463 } else if (vmp && (vmp->flag & LPFC_VMID_REQ_REGISTER ||
5464 vmp->flag & LPFC_VMID_DE_REGISTER)) {
5465 /* else if register or dereg request has already been sent */
5466 /* Hence VMID tag will not be added for this I/O */
5467 read_unlock(&vport->vmid_lock);
5468 rc = -EBUSY;
5469 } else {
5470 /* The VMID was not found in the hashtable. At this point, */
5471 /* drop the read lock first before proceeding further */
5472 read_unlock(&vport->vmid_lock);
5473 /* start the process to obtain one as per the */
5474 /* type of the VMID indicated */
5475 write_lock(&vport->vmid_lock);
5476 vmp = lpfc_get_vmid_from_hashtable(vport, hash, uuid);
5477
5478 /* while the read lock was released, in case the entry was */
5479 /* added by other context or is in process of being added */
5480 if (vmp && vmp->flag & LPFC_VMID_REGISTERED) {
5481 lpfc_vmid_update_entry(vport, cmd, vmp, tag);
5482 write_unlock(&vport->vmid_lock);
5483 return 0;
5484 } else if (vmp && vmp->flag & LPFC_VMID_REQ_REGISTER) {
5485 write_unlock(&vport->vmid_lock);
5486 return -EBUSY;
5487 }
5488
5489 /* else search and allocate a free slot in the hash table */
5490 if (vport->cur_vmid_cnt < vport->max_vmid) {
5491 for (i = 0; i < vport->max_vmid; i++) {
5492 vmp = vport->vmid + i;
5493 if (vmp->flag == LPFC_VMID_SLOT_FREE)
5494 break;
5495 }
5496 if (i == vport->max_vmid)
5497 vmp = NULL;
5498 } else {
5499 vmp = NULL;
5500 }
5501
5502 if (!vmp) {
5503 write_unlock(&vport->vmid_lock);
5504 return -ENOMEM;
5505 }
5506
5507 /* Add the vmid and register */
5508 lpfc_put_vmid_in_hashtable(vport, hash, vmp);
5509 vmp->vmid_len = len;
5510 memcpy(vmp->host_vmid, uuid, vmp->vmid_len);
5511 vmp->io_rd_cnt = 0;
5512 vmp->io_wr_cnt = 0;
5513 vmp->flag = LPFC_VMID_SLOT_USED;
5514
5515 vmp->delete_inactive =
5516 vport->vmid_inactivity_timeout ? 1 : 0;
5517
5518 /* if type priority tag, get next available VMID */
5519 if (lpfc_vmid_is_type_priority_tag(vport))
5520 lpfc_vmid_assign_cs_ctl(vport, vmp);
5521
5522 /* allocate the per cpu variable for holding */
5523 /* the last access time stamp only if VMID is enabled */
5524 if (!vmp->last_io_time)
5525 vmp->last_io_time = __alloc_percpu(sizeof(u64),
5526 __alignof__(struct
5527 lpfc_vmid));
5528 if (!vmp->last_io_time) {
5529 hash_del(&vmp->hnode);
5530 vmp->flag = LPFC_VMID_SLOT_FREE;
5531 write_unlock(&vport->vmid_lock);
5532 return -EIO;
5533 }
5534
5535 write_unlock(&vport->vmid_lock);
5536
5537 /* complete transaction with switch */
5538 if (lpfc_vmid_is_type_priority_tag(vport))
5539 rc = lpfc_vmid_uvem(vport, vmp, true);
5540 else
5541 rc = lpfc_vmid_cmd(vport, SLI_CTAS_RAPP_IDENT, vmp);
5542 if (!rc) {
5543 write_lock(&vport->vmid_lock);
5544 vport->cur_vmid_cnt++;
5545 vmp->flag |= LPFC_VMID_REQ_REGISTER;
5546 write_unlock(&vport->vmid_lock);
5547 } else {
5548 write_lock(&vport->vmid_lock);
5549 hash_del(&vmp->hnode);
5550 vmp->flag = LPFC_VMID_SLOT_FREE;
5551 free_percpu(vmp->last_io_time);
5552 write_unlock(&vport->vmid_lock);
5553 return -EIO;
5554 }
5555
5556 /* finally, enable the idle timer once */
5557 if (!(vport->phba->pport->vmid_flag & LPFC_VMID_TIMER_ENBLD)) {
5558 mod_timer(&vport->phba->inactive_vmid_poll,
5559 jiffies +
5560 msecs_to_jiffies(1000 * LPFC_VMID_TIMER));
5561 vport->phba->pport->vmid_flag |= LPFC_VMID_TIMER_ENBLD;
5562 }
5563 }
5564 return rc;
5565}
5566
5567/*
5568 * lpfc_is_command_vm_io - get the UUID from blk cgroup
5569 * @cmd: Pointer to scsi_cmnd data structure
5570 * Returns UUID if present, otherwise NULL
5571 */
5572static char *lpfc_is_command_vm_io(struct scsi_cmnd *cmd)
5573{
Bart Van Assche4221c8a2021-08-09 16:03:30 -07005574 struct bio *bio = scsi_cmd_to_rq(cmd)->bio;
Gaurav Srivastava33c79742021-06-08 10:05:56 +05305575
Bart Van Assche4221c8a2021-08-09 16:03:30 -07005576 return bio ? blkcg_get_fc_appid(bio) : NULL;
Gaurav Srivastava33c79742021-06-08 10:05:56 +05305577}
5578
James Smart9bad7672008-12-04 22:39:02 -05005579/**
James Smart3621a712009-04-06 18:47:14 -04005580 * lpfc_queuecommand - scsi_host_template queuecommand entry point
Lee Joneseceee002020-11-02 14:23:43 +00005581 * @shost: kernel scsi host pointer.
James Smart9bad7672008-12-04 22:39:02 -05005582 * @cmnd: Pointer to scsi_cmnd data structure.
James Smart9bad7672008-12-04 22:39:02 -05005583 *
5584 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
5585 * This routine prepares an IOCB from scsi command and provides to firmware.
5586 * The @done callback is invoked after driver finished processing the command.
5587 *
5588 * Return value :
5589 * 0 - Success
5590 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
5591 **/
dea31012005-04-17 16:05:31 -05005592static int
James Smartb9a7c632012-08-03 12:35:24 -04005593lpfc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05005594{
James Smart2e0fef82007-06-17 19:56:36 -05005595 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5596 struct lpfc_hba *phba = vport->phba;
James Smart1ba981f2014-02-20 09:56:45 -05005597 struct lpfc_rport_data *rdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005598 struct lpfc_nodelist *ndlp;
James Smartc4908502019-01-28 11:14:28 -08005599 struct lpfc_io_buf *lpfc_cmd;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005600 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
James Smart4c47efc2019-01-28 11:14:25 -08005601 int err, idx;
James Smart66b4d632021-06-18 10:18:42 -07005602 u8 *uuid = NULL;
James Smart2fcbc562020-03-22 11:13:02 -07005603#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
5604 uint64_t start = 0L;
5605
5606 if (phba->ktime_on)
5607 start = ktime_get_ns();
5608#endif
James Smart02243832021-08-16 09:28:54 -07005609 start = ktime_get_ns();
James Smart1ba981f2014-02-20 09:56:45 -05005610 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smartb0e83012018-06-26 08:24:29 -07005611
5612 /* sanity check on references */
5613 if (unlikely(!rdata) || unlikely(!rport))
5614 goto out_fail_command;
5615
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005616 err = fc_remote_port_chkready(rport);
5617 if (err) {
5618 cmnd->result = err;
dea31012005-04-17 16:05:31 -05005619 goto out_fail_command;
5620 }
James Smart1c6f4ef52009-11-18 15:40:49 -05005621 ndlp = rdata->pnode;
dea31012005-04-17 16:05:31 -05005622
James Smartbf086112011-08-21 21:48:13 -04005623 if ((scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) &&
James Smartacd68592012-01-18 16:25:09 -05005624 (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED))) {
James Smarte2a0a9d2008-12-04 22:40:02 -05005625
Dick Kennedy372c1872020-06-30 14:50:00 -07005626 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6a9c52c2009-10-02 15:16:51 -04005627 "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
5628 " op:%02x str=%s without registering for"
5629 " BlockGuard - Rejecting command\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05005630 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
5631 dif_op_str[scsi_get_prot_op(cmnd)]);
5632 goto out_fail_command;
5633 }
5634
dea31012005-04-17 16:05:31 -05005635 /*
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005636 * Catch race where our node has transitioned, but the
5637 * transport is still transitioning.
dea31012005-04-17 16:05:31 -05005638 */
James Smart307e3382020-11-15 11:26:30 -08005639 if (!ndlp)
James Smart02243832021-08-16 09:28:54 -07005640 goto out_tgt_busy1;
5641
5642 /* Check if IO qualifies for CMF */
5643 if (phba->cmf_active_mode != LPFC_CFG_OFF &&
5644 cmnd->sc_data_direction == DMA_FROM_DEVICE &&
5645 (scsi_sg_count(cmnd))) {
5646 /* Latency start time saved in rx_cmd_start later in routine */
5647 err = lpfc_update_cmf_cmd(phba, scsi_bufflen(cmnd));
5648 if (err)
5649 goto out_tgt_busy1;
5650 }
5651
James Smart2a5b7d62018-07-31 17:23:22 -07005652 if (lpfc_ndlp_check_qdepth(phba, ndlp)) {
5653 if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth) {
5654 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_ERROR,
5655 "3377 Target Queue Full, scsi Id:%d "
5656 "Qdepth:%d Pending command:%d"
5657 " WWNN:%02x:%02x:%02x:%02x:"
5658 "%02x:%02x:%02x:%02x, "
5659 " WWPN:%02x:%02x:%02x:%02x:"
5660 "%02x:%02x:%02x:%02x",
5661 ndlp->nlp_sid, ndlp->cmd_qdepth,
5662 atomic_read(&ndlp->cmd_pending),
5663 ndlp->nlp_nodename.u.wwn[0],
5664 ndlp->nlp_nodename.u.wwn[1],
5665 ndlp->nlp_nodename.u.wwn[2],
5666 ndlp->nlp_nodename.u.wwn[3],
5667 ndlp->nlp_nodename.u.wwn[4],
5668 ndlp->nlp_nodename.u.wwn[5],
5669 ndlp->nlp_nodename.u.wwn[6],
5670 ndlp->nlp_nodename.u.wwn[7],
5671 ndlp->nlp_portname.u.wwn[0],
5672 ndlp->nlp_portname.u.wwn[1],
5673 ndlp->nlp_portname.u.wwn[2],
5674 ndlp->nlp_portname.u.wwn[3],
5675 ndlp->nlp_portname.u.wwn[4],
5676 ndlp->nlp_portname.u.wwn[5],
5677 ndlp->nlp_portname.u.wwn[6],
5678 ndlp->nlp_portname.u.wwn[7]);
James Smart02243832021-08-16 09:28:54 -07005679 goto out_tgt_busy2;
James Smart2a5b7d62018-07-31 17:23:22 -07005680 }
James Smart64bf0092018-01-30 15:58:53 -08005681 }
James Smartf91bc592018-04-09 14:24:22 -07005682
James Smartace44e42019-01-28 11:14:27 -08005683 lpfc_cmd = lpfc_get_scsi_buf(phba, ndlp, cmnd);
dea31012005-04-17 16:05:31 -05005684 if (lpfc_cmd == NULL) {
James Smarteaf15d52008-12-04 22:39:29 -05005685 lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05005686
James Smart895427b2017-02-12 13:52:30 -08005687 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_ERROR,
James Smarte8b62012007-08-02 11:10:09 -04005688 "0707 driver's buffer pool is empty, "
5689 "IO busied\n");
dea31012005-04-17 16:05:31 -05005690 goto out_host_busy;
5691 }
James Smart02243832021-08-16 09:28:54 -07005692 lpfc_cmd->rx_cmd_start = start;
dea31012005-04-17 16:05:31 -05005693
5694 /*
5695 * Store the midlayer's command structure for the completion phase
5696 * and complete the command initialization.
5697 */
5698 lpfc_cmd->pCmd = cmnd;
5699 lpfc_cmd->rdata = rdata;
James Smart2a5b7d62018-07-31 17:23:22 -07005700 lpfc_cmd->ndlp = ndlp;
James Smartda255e22020-11-15 11:26:42 -08005701 lpfc_cmd->cur_iocbq.iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -05005702 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
dea31012005-04-17 16:05:31 -05005703
James Smartda255e22020-11-15 11:26:42 -08005704 err = lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
5705 if (err)
5706 goto out_host_busy_release_buf;
5707
James Smarte2a0a9d2008-12-04 22:40:02 -05005708 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
James Smart6a9c52c2009-10-02 15:16:51 -04005709 if (vport->phba->cfg_enable_bg) {
James Smart737d4242013-04-17 20:14:49 -04005710 lpfc_printf_vlog(vport,
5711 KERN_INFO, LOG_SCSI_CMD,
James Smart26134702012-08-14 14:25:50 -04005712 "9033 BLKGRD: rcvd %s cmd:x%x "
James Smart68a6a662021-03-01 09:18:02 -08005713 "reftag x%x cnt %u pt %x\n",
James Smart26134702012-08-14 14:25:50 -04005714 dif_op_str[scsi_get_prot_op(cmnd)],
5715 cmnd->cmnd[0],
Bart Van Assche4221c8a2021-08-09 16:03:30 -07005716 t10_pi_ref_tag(scsi_cmd_to_rq(cmnd)),
5717 blk_rq_sectors(scsi_cmd_to_rq(cmnd)),
James Smart26134702012-08-14 14:25:50 -04005718 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04005719 }
James Smarte2a0a9d2008-12-04 22:40:02 -05005720 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
5721 } else {
James Smart6a9c52c2009-10-02 15:16:51 -04005722 if (vport->phba->cfg_enable_bg) {
James Smart737d4242013-04-17 20:14:49 -04005723 lpfc_printf_vlog(vport,
5724 KERN_INFO, LOG_SCSI_CMD,
James Smart26134702012-08-14 14:25:50 -04005725 "9038 BLKGRD: rcvd PROT_NORMAL cmd: "
James Smart68a6a662021-03-01 09:18:02 -08005726 "x%x reftag x%x cnt %u pt %x\n",
James Smart26134702012-08-14 14:25:50 -04005727 cmnd->cmnd[0],
Bart Van Assche4221c8a2021-08-09 16:03:30 -07005728 t10_pi_ref_tag(scsi_cmd_to_rq(cmnd)),
5729 blk_rq_sectors(scsi_cmd_to_rq(cmnd)),
James Smart26134702012-08-14 14:25:50 -04005730 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04005731 }
James Smarte2a0a9d2008-12-04 22:40:02 -05005732 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
5733 }
5734
James Smart22770cb2019-10-18 14:18:24 -07005735 if (unlikely(err)) {
5736 if (err == 2) {
5737 cmnd->result = DID_ERROR << 16;
5738 goto out_fail_command_release_buf;
5739 }
dea31012005-04-17 16:05:31 -05005740 goto out_host_busy_free_buf;
James Smart5e0e2312019-08-14 16:56:54 -07005741 }
dea31012005-04-17 16:05:31 -05005742
dea31012005-04-17 16:05:31 -05005743
Gaurav Srivastava33c79742021-06-08 10:05:56 +05305744 /* check the necessary and sufficient condition to support VMID */
5745 if (lpfc_is_vmid_enabled(phba) &&
5746 (ndlp->vmid_support ||
5747 phba->pport->vmid_priority_tagging ==
5748 LPFC_VMID_PRIO_TAG_ALL_TARGETS)) {
5749 /* is the I/O generated by a VM, get the associated virtual */
5750 /* entity id */
5751 uuid = lpfc_is_command_vm_io(cmnd);
5752
5753 if (uuid) {
5754 err = lpfc_vmid_get_appid(vport, uuid, cmnd,
5755 (union lpfc_vmid_io_tag *)
5756 &lpfc_cmd->cur_iocbq.vmid_tag);
5757 if (!err)
5758 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_VMID;
5759 }
5760 }
5761
5762 atomic_inc(&ndlp->cmd_pending);
James Smart6a828b02019-01-28 11:14:31 -08005763#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart840eda92020-03-22 11:13:00 -07005764 if (unlikely(phba->hdwqstat_on & LPFC_CHECK_SCSI_IO))
5765 this_cpu_inc(phba->sli4_hba.c_stat->xmt_io);
James Smart6a828b02019-01-28 11:14:31 -08005766#endif
James Smart47ff4c52020-11-15 11:26:41 -08005767 /* Issue I/O to adapter */
5768 err = lpfc_sli_issue_fcp_io(phba, LPFC_FCP_RING,
5769 &lpfc_cmd->cur_iocbq,
5770 SLI_IOCB_RET_IOCB);
James Smart2fcbc562020-03-22 11:13:02 -07005771#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
5772 if (start) {
5773 lpfc_cmd->ts_cmd_start = start;
5774 lpfc_cmd->ts_last_cmd = phba->ktime_last_cmd;
5775 lpfc_cmd->ts_cmd_wqput = ktime_get_ns();
5776 } else {
5777 lpfc_cmd->ts_cmd_start = 0;
5778 }
5779#endif
James Smarteaf15d52008-12-04 22:39:29 -05005780 if (err) {
James Smart76f96b62013-12-17 20:29:01 -05005781 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smartda255e22020-11-15 11:26:42 -08005782 "3376 FCP could not issue IOCB err %x "
5783 "FCP cmd x%x <%d/%llu> "
5784 "sid: x%x did: x%x oxid: x%x "
5785 "Data: x%x x%x x%x x%x\n",
5786 err, cmnd->cmnd[0],
5787 cmnd->device ? cmnd->device->id : 0xffff,
5788 cmnd->device ? cmnd->device->lun : (u64)-1,
5789 vport->fc_myDID, ndlp->nlp_DID,
5790 phba->sli_rev == LPFC_SLI_REV4 ?
5791 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
5792 phba->sli_rev == LPFC_SLI_REV4 ?
5793 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi] :
5794 lpfc_cmd->cur_iocbq.iocb.ulpContext,
5795 lpfc_cmd->cur_iocbq.iotag,
5796 phba->sli_rev == LPFC_SLI_REV4 ?
5797 bf_get(wqe_tmo,
5798 &lpfc_cmd->cur_iocbq.wqe.generic.wqe_com) :
5799 lpfc_cmd->cur_iocbq.iocb.ulpTimeout,
Bart Van Assche4221c8a2021-08-09 16:03:30 -07005800 (uint32_t)(scsi_cmd_to_rq(cmnd)->timeout / 1000));
James Smart76f96b62013-12-17 20:29:01 -05005801
dea31012005-04-17 16:05:31 -05005802 goto out_host_busy_free_buf;
James Smarteaf15d52008-12-04 22:39:29 -05005803 }
James Smartda255e22020-11-15 11:26:42 -08005804
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005805 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04005806 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -08005807 &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
James Smart45ed1192009-10-02 15:17:02 -04005808
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005809 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
5810 lpfc_poll_rearm_timer(phba);
5811 }
5812
James Smartc4908502019-01-28 11:14:28 -08005813 if (phba->cfg_xri_rebalancing)
5814 lpfc_keep_pvt_pool_above_lowwm(phba, lpfc_cmd->hdwq_no);
5815
dea31012005-04-17 16:05:31 -05005816 return 0;
5817
5818 out_host_busy_free_buf:
James Smart1fbf9742019-01-28 11:14:26 -08005819 idx = lpfc_cmd->hdwq_no;
James Smartbcf4dbf2006-07-06 15:50:08 -04005820 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James Smart4c47efc2019-01-28 11:14:25 -08005821 if (phba->sli4_hba.hdwq) {
5822 switch (lpfc_cmd->fcp_cmnd->fcpCntl3) {
5823 case WRITE_DATA:
5824 phba->sli4_hba.hdwq[idx].scsi_cstat.output_requests--;
5825 break;
5826 case READ_DATA:
5827 phba->sli4_hba.hdwq[idx].scsi_cstat.input_requests--;
5828 break;
5829 default:
5830 phba->sli4_hba.hdwq[idx].scsi_cstat.control_requests--;
5831 }
5832 }
James Smartda255e22020-11-15 11:26:42 -08005833 out_host_busy_release_buf:
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04005834 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05005835 out_host_busy:
James Smart02243832021-08-16 09:28:54 -07005836 lpfc_update_cmf_cmpl(phba, LPFC_CGN_NOT_SENT, scsi_bufflen(cmnd),
5837 shost);
dea31012005-04-17 16:05:31 -05005838 return SCSI_MLQUEUE_HOST_BUSY;
5839
James Smart02243832021-08-16 09:28:54 -07005840 out_tgt_busy2:
5841 lpfc_update_cmf_cmpl(phba, LPFC_CGN_NOT_SENT, scsi_bufflen(cmnd),
5842 shost);
5843 out_tgt_busy1:
Mike Christie34963432011-02-25 14:04:28 -06005844 return SCSI_MLQUEUE_TARGET_BUSY;
5845
James Smart5e0e2312019-08-14 16:56:54 -07005846 out_fail_command_release_buf:
5847 lpfc_release_scsi_buf(phba, lpfc_cmd);
James Smart02243832021-08-16 09:28:54 -07005848 lpfc_update_cmf_cmpl(phba, LPFC_CGN_NOT_SENT, scsi_bufflen(cmnd),
5849 shost);
James Smart5e0e2312019-08-14 16:56:54 -07005850
dea31012005-04-17 16:05:31 -05005851 out_fail_command:
James Smartb9a7c632012-08-03 12:35:24 -04005852 cmnd->scsi_done(cmnd);
dea31012005-04-17 16:05:31 -05005853 return 0;
5854}
5855
Gaurav Srivastava5e633302021-06-08 10:05:49 +05305856/*
5857 * lpfc_vmid_vport_cleanup - cleans up the resources associated with a vport
5858 * @vport: The virtual port for which this call is being executed.
5859 */
5860void lpfc_vmid_vport_cleanup(struct lpfc_vport *vport)
5861{
5862 u32 bucket;
5863 struct lpfc_vmid *cur;
5864
5865 if (vport->port_type == LPFC_PHYSICAL_PORT)
5866 del_timer_sync(&vport->phba->inactive_vmid_poll);
5867
5868 kfree(vport->qfpa_res);
5869 kfree(vport->vmid_priority.vmid_range);
5870 kfree(vport->vmid);
5871
5872 if (!hash_empty(vport->hash_table))
5873 hash_for_each(vport->hash_table, bucket, cur, hnode)
5874 hash_del(&cur->hnode);
5875
5876 vport->qfpa_res = NULL;
5877 vport->vmid_priority.vmid_range = NULL;
5878 vport->vmid = NULL;
5879 vport->cur_vmid_cnt = 0;
5880}
Jeff Garzikf2812332010-11-16 02:10:29 -05005881
James Smart9bad7672008-12-04 22:39:02 -05005882/**
James Smart3621a712009-04-06 18:47:14 -04005883 * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05005884 * @cmnd: Pointer to scsi_cmnd data structure.
5885 *
5886 * This routine aborts @cmnd pending in base driver.
5887 *
5888 * Return code :
5889 * 0x2003 - Error
5890 * 0x2002 - Success
5891 **/
dea31012005-04-17 16:05:31 -05005892static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05005893lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05005894{
James Smart2e0fef82007-06-17 19:56:36 -05005895 struct Scsi_Host *shost = cmnd->device->host;
5896 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5897 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04005898 struct lpfc_iocbq *iocb;
James Smartc4908502019-01-28 11:14:28 -08005899 struct lpfc_io_buf *lpfc_cmd;
James Smart3a707302012-06-12 13:54:42 -04005900 int ret = SUCCESS, status = 0;
James Smart8931c732018-07-31 17:23:20 -07005901 struct lpfc_sli_ring *pring_s4 = NULL;
James Smartdb7531d2020-11-15 11:26:44 -08005902 struct lpfc_sli_ring *pring = NULL;
James Smart895427b2017-02-12 13:52:30 -08005903 int ret_val;
James Smart59c68ea2018-04-09 14:24:25 -07005904 unsigned long flags;
James Smartfa61a542008-01-11 01:52:42 -05005905 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04005906
James Smart3a707302012-06-12 13:54:42 -04005907 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005908 if (status != 0 && status != SUCCESS)
James Smart3a707302012-06-12 13:54:42 -04005909 return status;
James Smart4f2e66c2012-05-09 21:17:07 -04005910
James Smartc2017262019-01-28 11:14:37 -08005911 lpfc_cmd = (struct lpfc_io_buf *)cmnd->host_scribble;
5912 if (!lpfc_cmd)
5913 return ret;
5914
James Smart876dd7d2012-09-29 11:31:28 -04005915 spin_lock_irqsave(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04005916 /* driver queued commands are in process of being flushed */
James Smartc00f62e2019-08-14 16:57:11 -07005917 if (phba->hba_flag & HBA_IOQ_FLUSH) {
James Smart4f2e66c2012-05-09 21:17:07 -04005918 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5919 "3168 SCSI Layer abort requested I/O has been "
5920 "flushed by LLD.\n");
James Smartc2017262019-01-28 11:14:37 -08005921 ret = FAILED;
5922 goto out_unlock;
James Smart4f2e66c2012-05-09 21:17:07 -04005923 }
5924
James Smartc2017262019-01-28 11:14:37 -08005925 /* Guard against IO completion being called at same time */
5926 spin_lock(&lpfc_cmd->buf_lock);
5927
5928 if (!lpfc_cmd->pCmd) {
James Smarteee88772010-09-29 11:19:08 -04005929 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5930 "2873 SCSI Layer I/O Abort Request IO CMPL Status "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005931 "x%x ID %d LUN %llu\n",
James Smart3a707302012-06-12 13:54:42 -04005932 SUCCESS, cmnd->device->id, cmnd->device->lun);
James Smartc2017262019-01-28 11:14:37 -08005933 goto out_unlock_buf;
James Smarteee88772010-09-29 11:19:08 -04005934 }
dea31012005-04-17 16:05:31 -05005935
James Smart4f2e66c2012-05-09 21:17:07 -04005936 iocb = &lpfc_cmd->cur_iocbq;
James Smart8931c732018-07-31 17:23:20 -07005937 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartc00f62e2019-08-14 16:57:11 -07005938 pring_s4 = phba->sli4_hba.hdwq[iocb->hba_wqidx].io_wq->pring;
James Smart8931c732018-07-31 17:23:20 -07005939 if (!pring_s4) {
5940 ret = FAILED;
James Smartc2017262019-01-28 11:14:37 -08005941 goto out_unlock_buf;
James Smart8931c732018-07-31 17:23:20 -07005942 }
5943 spin_lock(&pring_s4->ring_lock);
5944 }
James Smart4f2e66c2012-05-09 21:17:07 -04005945 /* the command is in process of being cancelled */
5946 if (!(iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
James Smart4f2e66c2012-05-09 21:17:07 -04005947 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5948 "3169 SCSI Layer abort requested I/O has been "
5949 "cancelled by LLD.\n");
James Smartc2017262019-01-28 11:14:37 -08005950 ret = FAILED;
5951 goto out_unlock_ring;
James Smart4f2e66c2012-05-09 21:17:07 -04005952 }
dea31012005-04-17 16:05:31 -05005953 /*
James Smartc4908502019-01-28 11:14:28 -08005954 * If pCmd field of the corresponding lpfc_io_buf structure
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04005955 * points to a different SCSI command, then the driver has
5956 * already completed this command, but the midlayer did not
James Smart4f2e66c2012-05-09 21:17:07 -04005957 * see the completion before the eh fired. Just return SUCCESS.
dea31012005-04-17 16:05:31 -05005958 */
James Smart4f2e66c2012-05-09 21:17:07 -04005959 if (lpfc_cmd->pCmd != cmnd) {
5960 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5961 "3170 SCSI Layer abort requested I/O has been "
5962 "completed by LLD.\n");
James Smartc2017262019-01-28 11:14:37 -08005963 goto out_unlock_ring;
James Smart4f2e66c2012-05-09 21:17:07 -04005964 }
dea31012005-04-17 16:05:31 -05005965
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04005966 BUG_ON(iocb->context1 != lpfc_cmd);
dea31012005-04-17 16:05:31 -05005967
James Smartee620212014-04-04 13:51:53 -04005968 /* abort issued in recovery is still in progress */
5969 if (iocb->iocb_flag & LPFC_DRIVER_ABORTED) {
5970 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5971 "3389 SCSI Layer I/O Abort Request is pending\n");
James Smart8931c732018-07-31 17:23:20 -07005972 if (phba->sli_rev == LPFC_SLI_REV4)
5973 spin_unlock(&pring_s4->ring_lock);
James Smartc2017262019-01-28 11:14:37 -08005974 spin_unlock(&lpfc_cmd->buf_lock);
James Smartee620212014-04-04 13:51:53 -04005975 spin_unlock_irqrestore(&phba->hbalock, flags);
5976 goto wait_for_cmpl;
5977 }
5978
James Smart8931c732018-07-31 17:23:20 -07005979 lpfc_cmd->waitq = &waitq;
James Smart98912dda2014-04-04 13:52:31 -04005980 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart59c68ea2018-04-09 14:24:25 -07005981 spin_unlock(&pring_s4->ring_lock);
James Smartdb7531d2020-11-15 11:26:44 -08005982 ret_val = lpfc_sli4_issue_abort_iotag(phba, iocb,
5983 lpfc_sli4_abort_fcp_cmpl);
James Smart98912dda2014-04-04 13:52:31 -04005984 } else {
James Smartdb7531d2020-11-15 11:26:44 -08005985 pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
5986 ret_val = lpfc_sli_issue_abort_iotag(phba, pring, iocb,
5987 lpfc_sli_abort_fcp_cmpl);
James Smart98912dda2014-04-04 13:52:31 -04005988 }
James Smart4f2e66c2012-05-09 21:17:07 -04005989
James Smarta22d73b2021-01-04 10:02:38 -08005990 /* Make sure HBA is alive */
5991 lpfc_issue_hb_tmo(phba);
5992
James Smartdb7531d2020-11-15 11:26:44 -08005993 if (ret_val != IOCB_SUCCESS) {
James Smart8931c732018-07-31 17:23:20 -07005994 /* Indicate the IO is not being aborted by the driver. */
James Smart8931c732018-07-31 17:23:20 -07005995 lpfc_cmd->waitq = NULL;
James Smartc2017262019-01-28 11:14:37 -08005996 spin_unlock(&lpfc_cmd->buf_lock);
James Smart91a52b62019-10-18 14:18:22 -07005997 spin_unlock_irqrestore(&phba->hbalock, flags);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04005998 ret = FAILED;
5999 goto out;
6000 }
6001
James Smart91a52b62019-10-18 14:18:22 -07006002 /* no longer need the lock after this point */
James Smartc2017262019-01-28 11:14:37 -08006003 spin_unlock(&lpfc_cmd->buf_lock);
James Smart91a52b62019-10-18 14:18:22 -07006004 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartc2017262019-01-28 11:14:37 -08006005
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05006006 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
James Smart45ed1192009-10-02 15:17:02 -04006007 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -08006008 &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05006009
James Smartee620212014-04-04 13:51:53 -04006010wait_for_cmpl:
James Smartdb7531d2020-11-15 11:26:44 -08006011 /*
6012 * iocb_flag is set to LPFC_DRIVER_ABORTED before we wait
6013 * for abort to complete.
6014 */
James Smartfa61a542008-01-11 01:52:42 -05006015 wait_event_timeout(waitq,
6016 (lpfc_cmd->pCmd != cmnd),
James Smart256ec0d2013-04-17 20:14:58 -04006017 msecs_to_jiffies(2*vport->cfg_devloss_tmo*1000));
James Smartee620212014-04-04 13:51:53 -04006018
James Smartc2017262019-01-28 11:14:37 -08006019 spin_lock(&lpfc_cmd->buf_lock);
dea31012005-04-17 16:05:31 -05006020
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04006021 if (lpfc_cmd->pCmd == cmnd) {
6022 ret = FAILED;
Dick Kennedy372c1872020-06-30 14:50:00 -07006023 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04006024 "0748 abort handler timed out waiting "
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -07006025 "for aborting I/O (xri:x%x) to complete: "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02006026 "ret %#x, ID %d, LUN %llu\n",
James Smart247ca942012-08-14 14:26:13 -04006027 iocb->sli4_xritag, ret,
6028 cmnd->device->id, cmnd->device->lun);
dea31012005-04-17 16:05:31 -05006029 }
James Smartb9e5a2d2019-05-21 17:49:01 -07006030
6031 lpfc_cmd->waitq = NULL;
6032
James Smartc2017262019-01-28 11:14:37 -08006033 spin_unlock(&lpfc_cmd->buf_lock);
James Smart4f2e66c2012-05-09 21:17:07 -04006034 goto out;
dea31012005-04-17 16:05:31 -05006035
James Smartc2017262019-01-28 11:14:37 -08006036out_unlock_ring:
6037 if (phba->sli_rev == LPFC_SLI_REV4)
6038 spin_unlock(&pring_s4->ring_lock);
6039out_unlock_buf:
6040 spin_unlock(&lpfc_cmd->buf_lock);
James Smart4f2e66c2012-05-09 21:17:07 -04006041out_unlock:
James Smart876dd7d2012-09-29 11:31:28 -04006042 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04006043out:
James Smarte8b62012007-08-02 11:10:09 -04006044 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
6045 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02006046 "LUN %llu\n", ret, cmnd->device->id,
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04006047 cmnd->device->lun);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04006048 return ret;
dea31012005-04-17 16:05:31 -05006049}
6050
James Smartbbb9d182009-06-10 17:23:16 -04006051static char *
6052lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
6053{
6054 switch (task_mgmt_cmd) {
6055 case FCP_ABORT_TASK_SET:
6056 return "ABORT_TASK_SET";
6057 case FCP_CLEAR_TASK_SET:
6058 return "FCP_CLEAR_TASK_SET";
6059 case FCP_BUS_RESET:
6060 return "FCP_BUS_RESET";
6061 case FCP_LUN_RESET:
6062 return "FCP_LUN_RESET";
6063 case FCP_TARGET_RESET:
6064 return "FCP_TARGET_RESET";
6065 case FCP_CLEAR_ACA:
6066 return "FCP_CLEAR_ACA";
6067 case FCP_TERMINATE_TASK:
6068 return "FCP_TERMINATE_TASK";
6069 default:
6070 return "unknown";
6071 }
6072}
6073
James Smart53151bb2013-10-10 12:24:07 -04006074
6075/**
6076 * lpfc_check_fcp_rsp - check the returned fcp_rsp to see if task failed
6077 * @vport: The virtual port for which this call is being executed.
James Smartc4908502019-01-28 11:14:28 -08006078 * @lpfc_cmd: Pointer to lpfc_io_buf data structure.
James Smart53151bb2013-10-10 12:24:07 -04006079 *
6080 * This routine checks the FCP RSP INFO to see if the tsk mgmt command succeded
6081 *
6082 * Return code :
6083 * 0x2003 - Error
6084 * 0x2002 - Success
6085 **/
6086static int
James Smartc4908502019-01-28 11:14:28 -08006087lpfc_check_fcp_rsp(struct lpfc_vport *vport, struct lpfc_io_buf *lpfc_cmd)
James Smart53151bb2013-10-10 12:24:07 -04006088{
6089 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
6090 uint32_t rsp_info;
6091 uint32_t rsp_len;
6092 uint8_t rsp_info_code;
6093 int ret = FAILED;
6094
6095
6096 if (fcprsp == NULL)
6097 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
6098 "0703 fcp_rsp is missing\n");
6099 else {
6100 rsp_info = fcprsp->rspStatus2;
6101 rsp_len = be32_to_cpu(fcprsp->rspRspLen);
6102 rsp_info_code = fcprsp->rspInfo3;
6103
6104
6105 lpfc_printf_vlog(vport, KERN_INFO,
6106 LOG_FCP,
6107 "0706 fcp_rsp valid 0x%x,"
6108 " rsp len=%d code 0x%x\n",
6109 rsp_info,
6110 rsp_len, rsp_info_code);
6111
James Smart996a02a2019-05-21 17:49:07 -07006112 /* If FCP_RSP_LEN_VALID bit is one, then the FCP_RSP_LEN
6113 * field specifies the number of valid bytes of FCP_RSP_INFO.
6114 * The FCP_RSP_LEN field shall be set to 0x04 or 0x08
6115 */
6116 if ((fcprsp->rspStatus2 & RSP_LEN_VALID) &&
6117 ((rsp_len == 8) || (rsp_len == 4))) {
James Smart53151bb2013-10-10 12:24:07 -04006118 switch (rsp_info_code) {
6119 case RSP_NO_FAILURE:
6120 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
6121 "0715 Task Mgmt No Failure\n");
6122 ret = SUCCESS;
6123 break;
6124 case RSP_TM_NOT_SUPPORTED: /* TM rejected */
6125 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
6126 "0716 Task Mgmt Target "
6127 "reject\n");
6128 break;
6129 case RSP_TM_NOT_COMPLETED: /* TM failed */
6130 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
6131 "0717 Task Mgmt Target "
6132 "failed TM\n");
6133 break;
6134 case RSP_TM_INVALID_LU: /* TM to invalid LU! */
6135 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
6136 "0718 Task Mgmt to invalid "
6137 "LUN\n");
6138 break;
6139 }
6140 }
6141 }
6142 return ret;
6143}
6144
6145
James Smartbbb9d182009-06-10 17:23:16 -04006146/**
6147 * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
6148 * @vport: The virtual port for which this call is being executed.
Lee Joneseceee002020-11-02 14:23:43 +00006149 * @cmnd: Pointer to scsi_cmnd data structure.
James Smartbbb9d182009-06-10 17:23:16 -04006150 * @tgt_id: Target ID of remote device.
6151 * @lun_id: Lun number for the TMF
6152 * @task_mgmt_cmd: type of TMF to send
6153 *
6154 * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
6155 * a remote port.
6156 *
6157 * Return Code:
6158 * 0x2003 - Error
6159 * 0x2002 - Success.
6160 **/
6161static int
James Smarteed695d2016-10-13 15:06:04 -07006162lpfc_send_taskmgmt(struct lpfc_vport *vport, struct scsi_cmnd *cmnd,
6163 unsigned int tgt_id, uint64_t lun_id,
6164 uint8_t task_mgmt_cmd)
James Smartbbb9d182009-06-10 17:23:16 -04006165{
6166 struct lpfc_hba *phba = vport->phba;
James Smartc4908502019-01-28 11:14:28 -08006167 struct lpfc_io_buf *lpfc_cmd;
James Smartbbb9d182009-06-10 17:23:16 -04006168 struct lpfc_iocbq *iocbq;
6169 struct lpfc_iocbq *iocbqrsp;
James Smarteed695d2016-10-13 15:06:04 -07006170 struct lpfc_rport_data *rdata;
6171 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04006172 int ret;
6173 int status;
6174
James Smarteed695d2016-10-13 15:06:04 -07006175 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smart307e3382020-11-15 11:26:30 -08006176 if (!rdata || !rdata->pnode)
James Smartbbb9d182009-06-10 17:23:16 -04006177 return FAILED;
James Smarteed695d2016-10-13 15:06:04 -07006178 pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04006179
James Smartace44e42019-01-28 11:14:27 -08006180 lpfc_cmd = lpfc_get_scsi_buf(phba, pnode, NULL);
James Smartbbb9d182009-06-10 17:23:16 -04006181 if (lpfc_cmd == NULL)
6182 return FAILED;
James Smart0c411222013-09-06 12:22:46 -04006183 lpfc_cmd->timeout = phba->cfg_task_mgmt_tmo;
James Smartbbb9d182009-06-10 17:23:16 -04006184 lpfc_cmd->rdata = rdata;
James Smarteed695d2016-10-13 15:06:04 -07006185 lpfc_cmd->pCmd = cmnd;
James Smart2a5b7d62018-07-31 17:23:22 -07006186 lpfc_cmd->ndlp = pnode;
James Smartbbb9d182009-06-10 17:23:16 -04006187
6188 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
6189 task_mgmt_cmd);
6190 if (!status) {
6191 lpfc_release_scsi_buf(phba, lpfc_cmd);
6192 return FAILED;
6193 }
6194
6195 iocbq = &lpfc_cmd->cur_iocbq;
6196 iocbqrsp = lpfc_sli_get_iocbq(phba);
6197 if (iocbqrsp == NULL) {
6198 lpfc_release_scsi_buf(phba, lpfc_cmd);
6199 return FAILED;
6200 }
James Smart5a0916b2013-07-15 18:31:42 -04006201 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
James Smartbbb9d182009-06-10 17:23:16 -04006202
6203 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02006204 "0702 Issue %s to TGT %d LUN %llu "
James Smart6d368e52011-05-24 11:44:12 -04006205 "rpi x%x nlp_flag x%x Data: x%x x%x\n",
James Smartbbb9d182009-06-10 17:23:16 -04006206 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
James Smart6d368e52011-05-24 11:44:12 -04006207 pnode->nlp_rpi, pnode->nlp_flag, iocbq->sli4_xritag,
6208 iocbq->iocb_flag);
James Smartbbb9d182009-06-10 17:23:16 -04006209
6210 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
6211 iocbq, iocbqrsp, lpfc_cmd->timeout);
James Smart53151bb2013-10-10 12:24:07 -04006212 if ((status != IOCB_SUCCESS) ||
6213 (iocbqrsp->iocb.ulpStatus != IOSTAT_SUCCESS)) {
James Smartae374a32015-05-22 10:42:41 -04006214 if (status != IOCB_SUCCESS ||
6215 iocbqrsp->iocb.ulpStatus != IOSTAT_FCP_RSP_ERROR)
Dick Kennedy372c1872020-06-30 14:50:00 -07006216 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
James Smartae374a32015-05-22 10:42:41 -04006217 "0727 TMF %s to TGT %d LUN %llu "
6218 "failed (%d, %d) iocb_flag x%x\n",
6219 lpfc_taskmgmt_name(task_mgmt_cmd),
6220 tgt_id, lun_id,
6221 iocbqrsp->iocb.ulpStatus,
6222 iocbqrsp->iocb.un.ulpWord[4],
6223 iocbq->iocb_flag);
James Smart53151bb2013-10-10 12:24:07 -04006224 /* if ulpStatus != IOCB_SUCCESS, then status == IOCB_SUCCESS */
6225 if (status == IOCB_SUCCESS) {
6226 if (iocbqrsp->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
6227 /* Something in the FCP_RSP was invalid.
6228 * Check conditions */
6229 ret = lpfc_check_fcp_rsp(vport, lpfc_cmd);
6230 else
6231 ret = FAILED;
6232 } else if (status == IOCB_TIMEDOUT) {
6233 ret = TIMEOUT_ERROR;
6234 } else {
6235 ret = FAILED;
6236 }
James Smart53151bb2013-10-10 12:24:07 -04006237 } else
James Smartbbb9d182009-06-10 17:23:16 -04006238 ret = SUCCESS;
6239
6240 lpfc_sli_release_iocbq(phba, iocbqrsp);
6241
6242 if (ret != TIMEOUT_ERROR)
6243 lpfc_release_scsi_buf(phba, lpfc_cmd);
6244
6245 return ret;
6246}
6247
6248/**
6249 * lpfc_chk_tgt_mapped -
6250 * @vport: The virtual port to check on
6251 * @cmnd: Pointer to scsi_cmnd data structure.
6252 *
6253 * This routine delays until the scsi target (aka rport) for the
6254 * command exists (is present and logged in) or we declare it non-existent.
6255 *
6256 * Return code :
6257 * 0x2003 - Error
6258 * 0x2002 - Success
6259 **/
6260static int
6261lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
6262{
James Smart1ba981f2014-02-20 09:56:45 -05006263 struct lpfc_rport_data *rdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05006264 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04006265 unsigned long later;
6266
James Smart1ba981f2014-02-20 09:56:45 -05006267 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smart1c6f4ef52009-11-18 15:40:49 -05006268 if (!rdata) {
6269 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smart32350662019-08-14 16:57:06 -07006270 "0797 Tgt Map rport failure: rdata x%px\n", rdata);
James Smart1c6f4ef52009-11-18 15:40:49 -05006271 return FAILED;
6272 }
6273 pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04006274 /*
6275 * If target is not in a MAPPED state, delay until
6276 * target is rediscovered or devloss timeout expires.
6277 */
6278 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
6279 while (time_after(later, jiffies)) {
James Smart307e3382020-11-15 11:26:30 -08006280 if (!pnode)
James Smartbbb9d182009-06-10 17:23:16 -04006281 return FAILED;
6282 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
6283 return SUCCESS;
6284 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
James Smart1ba981f2014-02-20 09:56:45 -05006285 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smartbbb9d182009-06-10 17:23:16 -04006286 if (!rdata)
6287 return FAILED;
6288 pnode = rdata->pnode;
6289 }
James Smart307e3382020-11-15 11:26:30 -08006290 if (!pnode || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
James Smartbbb9d182009-06-10 17:23:16 -04006291 return FAILED;
6292 return SUCCESS;
6293}
6294
6295/**
6296 * lpfc_reset_flush_io_context -
6297 * @vport: The virtual port (scsi_host) for the flush context
6298 * @tgt_id: If aborting by Target contect - specifies the target id
6299 * @lun_id: If aborting by Lun context - specifies the lun id
6300 * @context: specifies the context level to flush at.
6301 *
6302 * After a reset condition via TMF, we need to flush orphaned i/o
6303 * contexts from the adapter. This routine aborts any contexts
6304 * outstanding, then waits for their completions. The wait is
6305 * bounded by devloss_tmo though.
6306 *
6307 * Return code :
6308 * 0x2003 - Error
6309 * 0x2002 - Success
6310 **/
6311static int
6312lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
6313 uint64_t lun_id, lpfc_ctx_cmd context)
6314{
6315 struct lpfc_hba *phba = vport->phba;
6316 unsigned long later;
6317 int cnt;
6318
6319 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
6320 if (cnt)
James Smart98912dda2014-04-04 13:52:31 -04006321 lpfc_sli_abort_taskmgmt(vport,
James Smart895427b2017-02-12 13:52:30 -08006322 &phba->sli.sli3_ring[LPFC_FCP_RING],
James Smart98912dda2014-04-04 13:52:31 -04006323 tgt_id, lun_id, context);
James Smartbbb9d182009-06-10 17:23:16 -04006324 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
6325 while (time_after(later, jiffies) && cnt) {
6326 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
6327 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
6328 }
6329 if (cnt) {
Dick Kennedy372c1872020-06-30 14:50:00 -07006330 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
James Smartbbb9d182009-06-10 17:23:16 -04006331 "0724 I/O flush failure for context %s : cnt x%x\n",
6332 ((context == LPFC_CTX_LUN) ? "LUN" :
6333 ((context == LPFC_CTX_TGT) ? "TGT" :
6334 ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
6335 cnt);
6336 return FAILED;
6337 }
6338 return SUCCESS;
6339}
6340
James Smart9bad7672008-12-04 22:39:02 -05006341/**
James Smart3621a712009-04-06 18:47:14 -04006342 * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
James Smart9bad7672008-12-04 22:39:02 -05006343 * @cmnd: Pointer to scsi_cmnd data structure.
6344 *
James Smartbbb9d182009-06-10 17:23:16 -04006345 * This routine does a device reset by sending a LUN_RESET task management
James Smart9bad7672008-12-04 22:39:02 -05006346 * command.
6347 *
6348 * Return code :
6349 * 0x2003 - Error
James Smart3621a712009-04-06 18:47:14 -04006350 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05006351 **/
dea31012005-04-17 16:05:31 -05006352static int
James Smart7054a602007-04-25 09:52:34 -04006353lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05006354{
James Smart2e0fef82007-06-17 19:56:36 -05006355 struct Scsi_Host *shost = cmnd->device->host;
6356 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smart1ba981f2014-02-20 09:56:45 -05006357 struct lpfc_rport_data *rdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05006358 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04006359 unsigned tgt_id = cmnd->device->id;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02006360 uint64_t lun_id = cmnd->device->lun;
James Smartea2151b2008-09-07 11:52:10 -04006361 struct lpfc_scsi_event_header scsi_event;
James Smart53151bb2013-10-10 12:24:07 -04006362 int status;
James Smartda09ae42021-01-04 10:02:33 -08006363 u32 logit = LOG_FCP;
dea31012005-04-17 16:05:31 -05006364
James Smart1ba981f2014-02-20 09:56:45 -05006365 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smartad490b62015-04-07 15:07:26 -04006366 if (!rdata || !rdata->pnode) {
Dick Kennedy372c1872020-06-30 14:50:00 -07006367 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
James Smart32350662019-08-14 16:57:06 -07006368 "0798 Device Reset rdata failure: rdata x%px\n",
James Smartad490b62015-04-07 15:07:26 -04006369 rdata);
James Smart1c6f4ef52009-11-18 15:40:49 -05006370 return FAILED;
6371 }
6372 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04006373 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04006374 if (status != 0 && status != SUCCESS)
James Smart589a52d2010-07-14 15:30:54 -04006375 return status;
James Smartbbb9d182009-06-10 17:23:16 -04006376
6377 status = lpfc_chk_tgt_mapped(vport, cmnd);
6378 if (status == FAILED) {
Dick Kennedy372c1872020-06-30 14:50:00 -07006379 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
James Smart32350662019-08-14 16:57:06 -07006380 "0721 Device Reset rport failure: rdata x%px\n", rdata);
James Smartbbb9d182009-06-10 17:23:16 -04006381 return FAILED;
6382 }
6383
6384 scsi_event.event_type = FC_REG_SCSI_EVENT;
6385 scsi_event.subcategory = LPFC_EVENT_LUNRESET;
6386 scsi_event.lun = lun_id;
6387 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
6388 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
6389
6390 fc_host_post_vendor_event(shost, fc_get_event_number(),
6391 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
6392
James Smarteed695d2016-10-13 15:06:04 -07006393 status = lpfc_send_taskmgmt(vport, cmnd, tgt_id, lun_id,
James Smartbbb9d182009-06-10 17:23:16 -04006394 FCP_LUN_RESET);
James Smartda09ae42021-01-04 10:02:33 -08006395 if (status != SUCCESS)
6396 logit = LOG_TRACE_EVENT;
James Smartbbb9d182009-06-10 17:23:16 -04006397
James Smartda09ae42021-01-04 10:02:33 -08006398 lpfc_printf_vlog(vport, KERN_ERR, logit,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02006399 "0713 SCSI layer issued Device Reset (%d, %llu) "
James Smartbbb9d182009-06-10 17:23:16 -04006400 "return x%x\n", tgt_id, lun_id, status);
6401
dea31012005-04-17 16:05:31 -05006402 /*
James Smartbbb9d182009-06-10 17:23:16 -04006403 * We have to clean up i/o as : they may be orphaned by the TMF;
6404 * or if the TMF failed, they may be in an indeterminate state.
6405 * So, continue on.
6406 * We will report success if all the i/o aborts successfully.
dea31012005-04-17 16:05:31 -05006407 */
James Smart53151bb2013-10-10 12:24:07 -04006408 if (status == SUCCESS)
6409 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
James Smartbbb9d182009-06-10 17:23:16 -04006410 LPFC_CTX_LUN);
James Smart53151bb2013-10-10 12:24:07 -04006411
6412 return status;
James Smartbbb9d182009-06-10 17:23:16 -04006413}
6414
6415/**
6416 * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
6417 * @cmnd: Pointer to scsi_cmnd data structure.
6418 *
6419 * This routine does a target reset by sending a TARGET_RESET task management
6420 * command.
6421 *
6422 * Return code :
6423 * 0x2003 - Error
6424 * 0x2002 - Success
6425 **/
6426static int
6427lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
6428{
6429 struct Scsi_Host *shost = cmnd->device->host;
6430 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smart1ba981f2014-02-20 09:56:45 -05006431 struct lpfc_rport_data *rdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05006432 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04006433 unsigned tgt_id = cmnd->device->id;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02006434 uint64_t lun_id = cmnd->device->lun;
James Smartbbb9d182009-06-10 17:23:16 -04006435 struct lpfc_scsi_event_header scsi_event;
James Smart53151bb2013-10-10 12:24:07 -04006436 int status;
James Smartda09ae42021-01-04 10:02:33 -08006437 u32 logit = LOG_FCP;
James Smart21990d32021-07-07 11:43:37 -07006438 u32 dev_loss_tmo = vport->cfg_devloss_tmo;
James Smart31051242021-01-04 10:02:34 -08006439 unsigned long flags;
6440 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
James Smartbbb9d182009-06-10 17:23:16 -04006441
James Smart1ba981f2014-02-20 09:56:45 -05006442 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smartbbd3d732019-03-12 16:30:17 -07006443 if (!rdata || !rdata->pnode) {
Dick Kennedy372c1872020-06-30 14:50:00 -07006444 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
James Smart32350662019-08-14 16:57:06 -07006445 "0799 Target Reset rdata failure: rdata x%px\n",
James Smartbbd3d732019-03-12 16:30:17 -07006446 rdata);
James Smart1c6f4ef52009-11-18 15:40:49 -05006447 return FAILED;
6448 }
6449 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04006450 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04006451 if (status != 0 && status != SUCCESS)
James Smart589a52d2010-07-14 15:30:54 -04006452 return status;
James Smartbbb9d182009-06-10 17:23:16 -04006453
6454 status = lpfc_chk_tgt_mapped(vport, cmnd);
6455 if (status == FAILED) {
Dick Kennedy372c1872020-06-30 14:50:00 -07006456 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
James Smart32350662019-08-14 16:57:06 -07006457 "0722 Target Reset rport failure: rdata x%px\n", rdata);
James Smart63e480f2015-04-07 15:07:20 -04006458 if (pnode) {
James Smart31051242021-01-04 10:02:34 -08006459 spin_lock_irqsave(&pnode->lock, flags);
James Smart63e480f2015-04-07 15:07:20 -04006460 pnode->nlp_flag &= ~NLP_NPR_ADISC;
6461 pnode->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
James Smart31051242021-01-04 10:02:34 -08006462 spin_unlock_irqrestore(&pnode->lock, flags);
James Smart63e480f2015-04-07 15:07:20 -04006463 }
James Smart8c50d252014-09-03 12:58:16 -04006464 lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
6465 LPFC_CTX_TGT);
6466 return FAST_IO_FAIL;
dea31012005-04-17 16:05:31 -05006467 }
James Smartea2151b2008-09-07 11:52:10 -04006468
6469 scsi_event.event_type = FC_REG_SCSI_EVENT;
6470 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
6471 scsi_event.lun = 0;
6472 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
6473 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
6474
James Smartbbb9d182009-06-10 17:23:16 -04006475 fc_host_post_vendor_event(shost, fc_get_event_number(),
6476 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -04006477
James Smarteed695d2016-10-13 15:06:04 -07006478 status = lpfc_send_taskmgmt(vport, cmnd, tgt_id, lun_id,
James Smartbbb9d182009-06-10 17:23:16 -04006479 FCP_TARGET_RESET);
James Smart21990d32021-07-07 11:43:37 -07006480 if (status != SUCCESS) {
6481 logit = LOG_TRACE_EVENT;
James Smart31051242021-01-04 10:02:34 -08006482
James Smart21990d32021-07-07 11:43:37 -07006483 /* Issue LOGO, if no LOGO is outstanding */
6484 spin_lock_irqsave(&pnode->lock, flags);
6485 if (!(pnode->upcall_flags & NLP_WAIT_FOR_LOGO) &&
6486 !pnode->logo_waitq) {
6487 pnode->logo_waitq = &waitq;
6488 pnode->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
6489 pnode->nlp_flag |= NLP_ISSUE_LOGO;
6490 pnode->upcall_flags |= NLP_WAIT_FOR_LOGO;
6491 spin_unlock_irqrestore(&pnode->lock, flags);
6492 lpfc_unreg_rpi(vport, pnode);
6493 wait_event_timeout(waitq,
6494 (!(pnode->upcall_flags &
6495 NLP_WAIT_FOR_LOGO)),
6496 msecs_to_jiffies(dev_loss_tmo *
6497 1000));
6498
6499 if (pnode->upcall_flags & NLP_WAIT_FOR_LOGO) {
6500 lpfc_printf_vlog(vport, KERN_ERR, logit,
6501 "0725 SCSI layer TGTRST "
6502 "failed & LOGO TMO (%d, %llu) "
6503 "return x%x\n",
6504 tgt_id, lun_id, status);
6505 spin_lock_irqsave(&pnode->lock, flags);
6506 pnode->upcall_flags &= ~NLP_WAIT_FOR_LOGO;
6507 } else {
6508 spin_lock_irqsave(&pnode->lock, flags);
6509 }
6510 pnode->logo_waitq = NULL;
6511 spin_unlock_irqrestore(&pnode->lock, flags);
6512 status = SUCCESS;
6513
James Smart31051242021-01-04 10:02:34 -08006514 } else {
James Smart21990d32021-07-07 11:43:37 -07006515 spin_unlock_irqrestore(&pnode->lock, flags);
6516 status = FAILED;
James Smart31051242021-01-04 10:02:34 -08006517 }
James Smart31051242021-01-04 10:02:34 -08006518 }
dea31012005-04-17 16:05:31 -05006519
James Smartda09ae42021-01-04 10:02:33 -08006520 lpfc_printf_vlog(vport, KERN_ERR, logit,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02006521 "0723 SCSI layer issued Target Reset (%d, %llu) "
James Smartbbb9d182009-06-10 17:23:16 -04006522 "return x%x\n", tgt_id, lun_id, status);
6523
6524 /*
6525 * We have to clean up i/o as : they may be orphaned by the TMF;
6526 * or if the TMF failed, they may be in an indeterminate state.
6527 * So, continue on.
6528 * We will report success if all the i/o aborts successfully.
6529 */
James Smart53151bb2013-10-10 12:24:07 -04006530 if (status == SUCCESS)
6531 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
James Smart3a707302012-06-12 13:54:42 -04006532 LPFC_CTX_TGT);
James Smart53151bb2013-10-10 12:24:07 -04006533 return status;
dea31012005-04-17 16:05:31 -05006534}
6535
James Smart9bad7672008-12-04 22:39:02 -05006536/**
James Smart3621a712009-04-06 18:47:14 -04006537 * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05006538 * @cmnd: Pointer to scsi_cmnd data structure.
6539 *
James Smartbbb9d182009-06-10 17:23:16 -04006540 * This routine does target reset to all targets on @cmnd->device->host.
6541 * This emulates Parallel SCSI Bus Reset Semantics.
James Smart9bad7672008-12-04 22:39:02 -05006542 *
James Smartbbb9d182009-06-10 17:23:16 -04006543 * Return code :
6544 * 0x2003 - Error
6545 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05006546 **/
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04006547static int
James Smart7054a602007-04-25 09:52:34 -04006548lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05006549{
James Smart2e0fef82007-06-17 19:56:36 -05006550 struct Scsi_Host *shost = cmnd->device->host;
6551 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05006552 struct lpfc_nodelist *ndlp = NULL;
James Smartea2151b2008-09-07 11:52:10 -04006553 struct lpfc_scsi_event_header scsi_event;
James Smartbbb9d182009-06-10 17:23:16 -04006554 int match;
6555 int ret = SUCCESS, status, i;
James Smartda09ae42021-01-04 10:02:33 -08006556 u32 logit = LOG_FCP;
James Smartea2151b2008-09-07 11:52:10 -04006557
6558 scsi_event.event_type = FC_REG_SCSI_EVENT;
6559 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
6560 scsi_event.lun = 0;
6561 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
6562 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
6563
James Smartbbb9d182009-06-10 17:23:16 -04006564 fc_host_post_vendor_event(shost, fc_get_event_number(),
6565 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
dea31012005-04-17 16:05:31 -05006566
James Smartbf086112011-08-21 21:48:13 -04006567 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04006568 if (status != 0 && status != SUCCESS)
James Smartbf086112011-08-21 21:48:13 -04006569 return status;
James Smartbbb9d182009-06-10 17:23:16 -04006570
dea31012005-04-17 16:05:31 -05006571 /*
6572 * Since the driver manages a single bus device, reset all
6573 * targets known to the driver. Should any target reset
6574 * fail, this routine returns failure to the midlayer.
6575 */
James Smarte17da182006-07-06 15:49:25 -04006576 for (i = 0; i < LPFC_MAX_TARGET; i++) {
James Smart685f0bf2007-04-25 09:53:08 -04006577 /* Search for mapped node by target ID */
dea31012005-04-17 16:05:31 -05006578 match = 0;
James Smart2e0fef82007-06-17 19:56:36 -05006579 spin_lock_irq(shost->host_lock);
6580 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smart307e3382020-11-15 11:26:30 -08006581
James Smarta6571c62012-10-31 14:44:42 -04006582 if (vport->phba->cfg_fcp2_no_tgt_reset &&
6583 (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE))
6584 continue;
James Smart685f0bf2007-04-25 09:53:08 -04006585 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
James Smart915caaa2008-06-14 22:52:38 -04006586 ndlp->nlp_sid == i &&
James Smarta0f2d3e2017-02-12 13:52:31 -08006587 ndlp->rport &&
6588 ndlp->nlp_type & NLP_FCP_TARGET) {
dea31012005-04-17 16:05:31 -05006589 match = 1;
6590 break;
6591 }
6592 }
James Smart2e0fef82007-06-17 19:56:36 -05006593 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05006594 if (!match)
6595 continue;
James Smartbbb9d182009-06-10 17:23:16 -04006596
James Smarteed695d2016-10-13 15:06:04 -07006597 status = lpfc_send_taskmgmt(vport, cmnd,
James Smartbbb9d182009-06-10 17:23:16 -04006598 i, 0, FCP_TARGET_RESET);
6599
6600 if (status != SUCCESS) {
Dick Kennedy372c1872020-06-30 14:50:00 -07006601 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04006602 "0700 Bus Reset on target %d failed\n",
6603 i);
James Smart915caaa2008-06-14 22:52:38 -04006604 ret = FAILED;
dea31012005-04-17 16:05:31 -05006605 }
6606 }
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05006607 /*
James Smartbbb9d182009-06-10 17:23:16 -04006608 * We have to clean up i/o as : they may be orphaned by the TMFs
6609 * above; or if any of the TMFs failed, they may be in an
6610 * indeterminate state.
6611 * We will report success if all the i/o aborts successfully.
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05006612 */
James Smartbbb9d182009-06-10 17:23:16 -04006613
6614 status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
6615 if (status != SUCCESS)
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05006616 ret = FAILED;
James Smartda09ae42021-01-04 10:02:33 -08006617 if (ret == FAILED)
6618 logit = LOG_TRACE_EVENT;
James Smartbbb9d182009-06-10 17:23:16 -04006619
James Smartda09ae42021-01-04 10:02:33 -08006620 lpfc_printf_vlog(vport, KERN_ERR, logit,
James Smarte8b62012007-08-02 11:10:09 -04006621 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea31012005-04-17 16:05:31 -05006622 return ret;
6623}
6624
James Smart9bad7672008-12-04 22:39:02 -05006625/**
James Smart27b01b82012-05-09 21:19:44 -04006626 * lpfc_host_reset_handler - scsi_host_template eh_host_reset_handler entry pt
6627 * @cmnd: Pointer to scsi_cmnd data structure.
6628 *
6629 * This routine does host reset to the adaptor port. It brings the HBA
6630 * offline, performs a board restart, and then brings the board back online.
6631 * The lpfc_offline calls lpfc_sli_hba_down which will abort and local
6632 * reject all outstanding SCSI commands to the host and error returned
6633 * back to SCSI mid-level. As this will be SCSI mid-level's last resort
6634 * of error handling, it will only return error if resetting of the adapter
6635 * is not successful; in all other cases, will return success.
6636 *
6637 * Return code :
6638 * 0x2003 - Error
6639 * 0x2002 - Success
6640 **/
6641static int
6642lpfc_host_reset_handler(struct scsi_cmnd *cmnd)
6643{
6644 struct Scsi_Host *shost = cmnd->device->host;
6645 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
6646 struct lpfc_hba *phba = vport->phba;
6647 int rc, ret = SUCCESS;
6648
James Smartda09ae42021-01-04 10:02:33 -08006649 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smarta88dbb62013-04-17 20:18:39 -04006650 "3172 SCSI layer issued Host Reset Data:\n");
6651
James Smart618a5232012-06-12 13:54:36 -04006652 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart27b01b82012-05-09 21:19:44 -04006653 lpfc_offline(phba);
6654 rc = lpfc_sli_brdrestart(phba);
6655 if (rc)
James Smart8c24a4f2019-08-14 16:56:53 -07006656 goto error;
6657
James Smarta88dbb62013-04-17 20:18:39 -04006658 rc = lpfc_online(phba);
6659 if (rc)
James Smart8c24a4f2019-08-14 16:56:53 -07006660 goto error;
6661
James Smart27b01b82012-05-09 21:19:44 -04006662 lpfc_unblock_mgmt_io(phba);
6663
James Smart27b01b82012-05-09 21:19:44 -04006664 return ret;
James Smart8c24a4f2019-08-14 16:56:53 -07006665error:
Dick Kennedy372c1872020-06-30 14:50:00 -07006666 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
James Smart8c24a4f2019-08-14 16:56:53 -07006667 "3323 Failed host reset\n");
6668 lpfc_unblock_mgmt_io(phba);
6669 return FAILED;
James Smart27b01b82012-05-09 21:19:44 -04006670}
6671
6672/**
James Smart3621a712009-04-06 18:47:14 -04006673 * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
James Smart9bad7672008-12-04 22:39:02 -05006674 * @sdev: Pointer to scsi_device.
6675 *
6676 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
6677 * globally available list of scsi buffers. This routine also makes sure scsi
6678 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
6679 * of scsi buffer exists for the lifetime of the driver.
6680 *
6681 * Return codes:
6682 * non-0 - Error
6683 * 0 - Success
6684 **/
dea31012005-04-17 16:05:31 -05006685static int
dea31012005-04-17 16:05:31 -05006686lpfc_slave_alloc(struct scsi_device *sdev)
6687{
James Smart2e0fef82007-06-17 19:56:36 -05006688 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
6689 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04006690 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
James Smart3772a992009-05-22 14:50:54 -04006691 uint32_t total = 0;
dea31012005-04-17 16:05:31 -05006692 uint32_t num_to_alloc = 0;
James Smart3772a992009-05-22 14:50:54 -04006693 int num_allocated = 0;
James Smartd7c47992010-06-08 18:31:54 -04006694 uint32_t sdev_cnt;
James Smart1ba981f2014-02-20 09:56:45 -05006695 struct lpfc_device_data *device_data;
6696 unsigned long flags;
6697 struct lpfc_name target_wwpn;
dea31012005-04-17 16:05:31 -05006698
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04006699 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05006700 return -ENXIO;
6701
James Smartf38fa0b2014-04-04 13:52:21 -04006702 if (phba->cfg_fof) {
James Smart1ba981f2014-02-20 09:56:45 -05006703
6704 /*
6705 * Check to see if the device data structure for the lun
6706 * exists. If not, create one.
6707 */
6708
6709 u64_to_wwn(rport->port_name, target_wwpn.u.wwn);
6710 spin_lock_irqsave(&phba->devicelock, flags);
6711 device_data = __lpfc_get_device_data(phba,
6712 &phba->luns,
6713 &vport->fc_portname,
6714 &target_wwpn,
6715 sdev->lun);
6716 if (!device_data) {
6717 spin_unlock_irqrestore(&phba->devicelock, flags);
6718 device_data = lpfc_create_device_data(phba,
6719 &vport->fc_portname,
6720 &target_wwpn,
James Smartb5749fe2016-12-19 15:07:26 -08006721 sdev->lun,
6722 phba->cfg_XLanePriority,
6723 true);
James Smart1ba981f2014-02-20 09:56:45 -05006724 if (!device_data)
6725 return -ENOMEM;
6726 spin_lock_irqsave(&phba->devicelock, flags);
6727 list_add_tail(&device_data->listentry, &phba->luns);
6728 }
6729 device_data->rport_data = rport->dd_data;
6730 device_data->available = true;
6731 spin_unlock_irqrestore(&phba->devicelock, flags);
6732 sdev->hostdata = device_data;
6733 } else {
6734 sdev->hostdata = rport->dd_data;
6735 }
James Smartd7c47992010-06-08 18:31:54 -04006736 sdev_cnt = atomic_inc_return(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05006737
James Smart0794d602019-01-28 11:14:19 -08006738 /* For SLI4, all IO buffers are pre-allocated */
6739 if (phba->sli_rev == LPFC_SLI_REV4)
6740 return 0;
6741
6742 /* This code path is now ONLY for SLI3 adapters */
6743
dea31012005-04-17 16:05:31 -05006744 /*
6745 * Populate the cmds_per_lun count scsi_bufs into this host's globally
6746 * available list of scsi buffers. Don't allocate more than the
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04006747 * HBA limit conveyed to the midlayer via the host structure. The
6748 * formula accounts for the lun_queue_depth + error handlers + 1
6749 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea31012005-04-17 16:05:31 -05006750 */
6751 total = phba->total_scsi_bufs;
James Smart3de2a652007-08-02 11:09:59 -04006752 num_to_alloc = vport->cfg_lun_queue_depth + 2;
James Smart92d7f7b2007-06-17 19:56:38 -05006753
James Smartd7c47992010-06-08 18:31:54 -04006754 /* If allocated buffers are enough do nothing */
6755 if ((sdev_cnt * (vport->cfg_lun_queue_depth + 2)) < total)
6756 return 0;
6757
James Smart92d7f7b2007-06-17 19:56:38 -05006758 /* Allow some exchanges to be available always to complete discovery */
6759 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04006760 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
6761 "0704 At limitation of %d preallocated "
6762 "command buffers\n", total);
dea31012005-04-17 16:05:31 -05006763 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05006764 /* Allow some exchanges to be available always to complete discovery */
6765 } else if (total + num_to_alloc >
6766 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04006767 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
6768 "0705 Allocation request of %d "
6769 "command buffers will exceed max of %d. "
6770 "Reducing allocation request to %d.\n",
6771 num_to_alloc, phba->cfg_hba_queue_depth,
6772 (phba->cfg_hba_queue_depth - total));
dea31012005-04-17 16:05:31 -05006773 num_to_alloc = phba->cfg_hba_queue_depth - total;
6774 }
James Smart0794d602019-01-28 11:14:19 -08006775 num_allocated = lpfc_new_scsi_buf_s3(vport, num_to_alloc);
James Smart3772a992009-05-22 14:50:54 -04006776 if (num_to_alloc != num_allocated) {
Dick Kennedy372c1872020-06-30 14:50:00 -07006777 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
James Smart96f70772013-04-17 20:16:15 -04006778 "0708 Allocation request of %d "
6779 "command buffers did not succeed. "
6780 "Allocated %d buffers.\n",
6781 num_to_alloc, num_allocated);
dea31012005-04-17 16:05:31 -05006782 }
James Smart1c6f4ef52009-11-18 15:40:49 -05006783 if (num_allocated > 0)
6784 phba->total_scsi_bufs += num_allocated;
dea31012005-04-17 16:05:31 -05006785 return 0;
6786}
6787
James Smart9bad7672008-12-04 22:39:02 -05006788/**
James Smart3621a712009-04-06 18:47:14 -04006789 * lpfc_slave_configure - scsi_host_template slave_configure entry point
James Smart9bad7672008-12-04 22:39:02 -05006790 * @sdev: Pointer to scsi_device.
6791 *
6792 * This routine configures following items
6793 * - Tag command queuing support for @sdev if supported.
James Smart9bad7672008-12-04 22:39:02 -05006794 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
6795 *
6796 * Return codes:
6797 * 0 - Success
6798 **/
dea31012005-04-17 16:05:31 -05006799static int
6800lpfc_slave_configure(struct scsi_device *sdev)
6801{
James Smart2e0fef82007-06-17 19:56:36 -05006802 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
6803 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05006804
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01006805 scsi_change_queue_depth(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05006806
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05006807 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04006808 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -08006809 &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05006810 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
6811 lpfc_poll_rearm_timer(phba);
6812 }
6813
dea31012005-04-17 16:05:31 -05006814 return 0;
6815}
6816
James Smart9bad7672008-12-04 22:39:02 -05006817/**
James Smart3621a712009-04-06 18:47:14 -04006818 * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
James Smart9bad7672008-12-04 22:39:02 -05006819 * @sdev: Pointer to scsi_device.
6820 *
6821 * This routine sets @sdev hostatdata filed to null.
6822 **/
dea31012005-04-17 16:05:31 -05006823static void
6824lpfc_slave_destroy(struct scsi_device *sdev)
6825{
James Smartd7c47992010-06-08 18:31:54 -04006826 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
6827 struct lpfc_hba *phba = vport->phba;
James Smart1ba981f2014-02-20 09:56:45 -05006828 unsigned long flags;
6829 struct lpfc_device_data *device_data = sdev->hostdata;
6830
James Smartd7c47992010-06-08 18:31:54 -04006831 atomic_dec(&phba->sdev_cnt);
James Smartf38fa0b2014-04-04 13:52:21 -04006832 if ((phba->cfg_fof) && (device_data)) {
James Smart1ba981f2014-02-20 09:56:45 -05006833 spin_lock_irqsave(&phba->devicelock, flags);
6834 device_data->available = false;
6835 if (!device_data->oas_enabled)
6836 lpfc_delete_device_data(phba, device_data);
6837 spin_unlock_irqrestore(&phba->devicelock, flags);
6838 }
dea31012005-04-17 16:05:31 -05006839 sdev->hostdata = NULL;
6840 return;
6841}
6842
James Smart1ba981f2014-02-20 09:56:45 -05006843/**
6844 * lpfc_create_device_data - creates and initializes device data structure for OAS
Lee Joneseceee002020-11-02 14:23:43 +00006845 * @phba: Pointer to host bus adapter structure.
James Smart1ba981f2014-02-20 09:56:45 -05006846 * @vport_wwpn: Pointer to vport's wwpn information
6847 * @target_wwpn: Pointer to target's wwpn information
6848 * @lun: Lun on target
Lee Joneseceee002020-11-02 14:23:43 +00006849 * @pri: Priority
James Smart1ba981f2014-02-20 09:56:45 -05006850 * @atomic_create: Flag to indicate if memory should be allocated using the
6851 * GFP_ATOMIC flag or not.
6852 *
6853 * This routine creates a device data structure which will contain identifying
6854 * information for the device (host wwpn, target wwpn, lun), state of OAS,
6855 * whether or not the corresponding lun is available by the system,
6856 * and pointer to the rport data.
6857 *
6858 * Return codes:
6859 * NULL - Error
6860 * Pointer to lpfc_device_data - Success
6861 **/
6862struct lpfc_device_data*
6863lpfc_create_device_data(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
6864 struct lpfc_name *target_wwpn, uint64_t lun,
James Smartb5749fe2016-12-19 15:07:26 -08006865 uint32_t pri, bool atomic_create)
James Smart1ba981f2014-02-20 09:56:45 -05006866{
6867
6868 struct lpfc_device_data *lun_info;
6869 int memory_flags;
6870
6871 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
James Smartf38fa0b2014-04-04 13:52:21 -04006872 !(phba->cfg_fof))
James Smart1ba981f2014-02-20 09:56:45 -05006873 return NULL;
6874
6875 /* Attempt to create the device data to contain lun info */
6876
6877 if (atomic_create)
6878 memory_flags = GFP_ATOMIC;
6879 else
6880 memory_flags = GFP_KERNEL;
6881 lun_info = mempool_alloc(phba->device_data_mem_pool, memory_flags);
6882 if (!lun_info)
6883 return NULL;
6884 INIT_LIST_HEAD(&lun_info->listentry);
6885 lun_info->rport_data = NULL;
6886 memcpy(&lun_info->device_id.vport_wwpn, vport_wwpn,
6887 sizeof(struct lpfc_name));
6888 memcpy(&lun_info->device_id.target_wwpn, target_wwpn,
6889 sizeof(struct lpfc_name));
6890 lun_info->device_id.lun = lun;
6891 lun_info->oas_enabled = false;
James Smartb5749fe2016-12-19 15:07:26 -08006892 lun_info->priority = pri;
James Smart1ba981f2014-02-20 09:56:45 -05006893 lun_info->available = false;
6894 return lun_info;
6895}
6896
6897/**
6898 * lpfc_delete_device_data - frees a device data structure for OAS
Lee Joneseceee002020-11-02 14:23:43 +00006899 * @phba: Pointer to host bus adapter structure.
James Smart1ba981f2014-02-20 09:56:45 -05006900 * @lun_info: Pointer to device data structure to free.
6901 *
6902 * This routine frees the previously allocated device data structure passed.
6903 *
6904 **/
6905void
6906lpfc_delete_device_data(struct lpfc_hba *phba,
6907 struct lpfc_device_data *lun_info)
6908{
6909
6910 if (unlikely(!phba) || !lun_info ||
James Smartf38fa0b2014-04-04 13:52:21 -04006911 !(phba->cfg_fof))
James Smart1ba981f2014-02-20 09:56:45 -05006912 return;
6913
6914 if (!list_empty(&lun_info->listentry))
6915 list_del(&lun_info->listentry);
6916 mempool_free(lun_info, phba->device_data_mem_pool);
6917 return;
6918}
6919
6920/**
6921 * __lpfc_get_device_data - returns the device data for the specified lun
Lee Joneseceee002020-11-02 14:23:43 +00006922 * @phba: Pointer to host bus adapter structure.
James Smart1ba981f2014-02-20 09:56:45 -05006923 * @list: Point to list to search.
6924 * @vport_wwpn: Pointer to vport's wwpn information
6925 * @target_wwpn: Pointer to target's wwpn information
6926 * @lun: Lun on target
6927 *
6928 * This routine searches the list passed for the specified lun's device data.
6929 * This function does not hold locks, it is the responsibility of the caller
6930 * to ensure the proper lock is held before calling the function.
6931 *
6932 * Return codes:
6933 * NULL - Error
6934 * Pointer to lpfc_device_data - Success
6935 **/
6936struct lpfc_device_data*
6937__lpfc_get_device_data(struct lpfc_hba *phba, struct list_head *list,
6938 struct lpfc_name *vport_wwpn,
6939 struct lpfc_name *target_wwpn, uint64_t lun)
6940{
6941
6942 struct lpfc_device_data *lun_info;
6943
6944 if (unlikely(!phba) || !list || !vport_wwpn || !target_wwpn ||
James Smartf38fa0b2014-04-04 13:52:21 -04006945 !phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05006946 return NULL;
6947
6948 /* Check to see if the lun is already enabled for OAS. */
6949
6950 list_for_each_entry(lun_info, list, listentry) {
6951 if ((memcmp(&lun_info->device_id.vport_wwpn, vport_wwpn,
6952 sizeof(struct lpfc_name)) == 0) &&
6953 (memcmp(&lun_info->device_id.target_wwpn, target_wwpn,
6954 sizeof(struct lpfc_name)) == 0) &&
6955 (lun_info->device_id.lun == lun))
6956 return lun_info;
6957 }
6958
6959 return NULL;
6960}
6961
6962/**
6963 * lpfc_find_next_oas_lun - searches for the next oas lun
Lee Joneseceee002020-11-02 14:23:43 +00006964 * @phba: Pointer to host bus adapter structure.
James Smart1ba981f2014-02-20 09:56:45 -05006965 * @vport_wwpn: Pointer to vport's wwpn information
6966 * @target_wwpn: Pointer to target's wwpn information
6967 * @starting_lun: Pointer to the lun to start searching for
6968 * @found_vport_wwpn: Pointer to the found lun's vport wwpn information
6969 * @found_target_wwpn: Pointer to the found lun's target wwpn information
6970 * @found_lun: Pointer to the found lun.
6971 * @found_lun_status: Pointer to status of the found lun.
Lee Joneseceee002020-11-02 14:23:43 +00006972 * @found_lun_pri: Pointer to priority of the found lun.
James Smart1ba981f2014-02-20 09:56:45 -05006973 *
6974 * This routine searches the luns list for the specified lun
6975 * or the first lun for the vport/target. If the vport wwpn contains
6976 * a zero value then a specific vport is not specified. In this case
6977 * any vport which contains the lun will be considered a match. If the
6978 * target wwpn contains a zero value then a specific target is not specified.
6979 * In this case any target which contains the lun will be considered a
6980 * match. If the lun is found, the lun, vport wwpn, target wwpn and lun status
6981 * are returned. The function will also return the next lun if available.
6982 * If the next lun is not found, starting_lun parameter will be set to
6983 * NO_MORE_OAS_LUN.
6984 *
6985 * Return codes:
6986 * non-0 - Error
6987 * 0 - Success
6988 **/
6989bool
6990lpfc_find_next_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
6991 struct lpfc_name *target_wwpn, uint64_t *starting_lun,
6992 struct lpfc_name *found_vport_wwpn,
6993 struct lpfc_name *found_target_wwpn,
6994 uint64_t *found_lun,
James Smartb5749fe2016-12-19 15:07:26 -08006995 uint32_t *found_lun_status,
6996 uint32_t *found_lun_pri)
James Smart1ba981f2014-02-20 09:56:45 -05006997{
6998
6999 unsigned long flags;
7000 struct lpfc_device_data *lun_info;
7001 struct lpfc_device_id *device_id;
7002 uint64_t lun;
7003 bool found = false;
7004
7005 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
7006 !starting_lun || !found_vport_wwpn ||
7007 !found_target_wwpn || !found_lun || !found_lun_status ||
7008 (*starting_lun == NO_MORE_OAS_LUN) ||
James Smartf38fa0b2014-04-04 13:52:21 -04007009 !phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05007010 return false;
7011
7012 lun = *starting_lun;
7013 *found_lun = NO_MORE_OAS_LUN;
7014 *starting_lun = NO_MORE_OAS_LUN;
7015
7016 /* Search for lun or the lun closet in value */
7017
7018 spin_lock_irqsave(&phba->devicelock, flags);
7019 list_for_each_entry(lun_info, &phba->luns, listentry) {
7020 if (((wwn_to_u64(vport_wwpn->u.wwn) == 0) ||
7021 (memcmp(&lun_info->device_id.vport_wwpn, vport_wwpn,
7022 sizeof(struct lpfc_name)) == 0)) &&
7023 ((wwn_to_u64(target_wwpn->u.wwn) == 0) ||
7024 (memcmp(&lun_info->device_id.target_wwpn, target_wwpn,
7025 sizeof(struct lpfc_name)) == 0)) &&
7026 (lun_info->oas_enabled)) {
7027 device_id = &lun_info->device_id;
7028 if ((!found) &&
7029 ((lun == FIND_FIRST_OAS_LUN) ||
7030 (device_id->lun == lun))) {
7031 *found_lun = device_id->lun;
7032 memcpy(found_vport_wwpn,
7033 &device_id->vport_wwpn,
7034 sizeof(struct lpfc_name));
7035 memcpy(found_target_wwpn,
7036 &device_id->target_wwpn,
7037 sizeof(struct lpfc_name));
7038 if (lun_info->available)
7039 *found_lun_status =
7040 OAS_LUN_STATUS_EXISTS;
7041 else
7042 *found_lun_status = 0;
James Smartb5749fe2016-12-19 15:07:26 -08007043 *found_lun_pri = lun_info->priority;
James Smart1ba981f2014-02-20 09:56:45 -05007044 if (phba->cfg_oas_flags & OAS_FIND_ANY_VPORT)
7045 memset(vport_wwpn, 0x0,
7046 sizeof(struct lpfc_name));
7047 if (phba->cfg_oas_flags & OAS_FIND_ANY_TARGET)
7048 memset(target_wwpn, 0x0,
7049 sizeof(struct lpfc_name));
7050 found = true;
7051 } else if (found) {
7052 *starting_lun = device_id->lun;
7053 memcpy(vport_wwpn, &device_id->vport_wwpn,
7054 sizeof(struct lpfc_name));
7055 memcpy(target_wwpn, &device_id->target_wwpn,
7056 sizeof(struct lpfc_name));
7057 break;
7058 }
7059 }
7060 }
7061 spin_unlock_irqrestore(&phba->devicelock, flags);
7062 return found;
7063}
7064
7065/**
7066 * lpfc_enable_oas_lun - enables a lun for OAS operations
Lee Joneseceee002020-11-02 14:23:43 +00007067 * @phba: Pointer to host bus adapter structure.
James Smart1ba981f2014-02-20 09:56:45 -05007068 * @vport_wwpn: Pointer to vport's wwpn information
7069 * @target_wwpn: Pointer to target's wwpn information
7070 * @lun: Lun
Lee Joneseceee002020-11-02 14:23:43 +00007071 * @pri: Priority
James Smart1ba981f2014-02-20 09:56:45 -05007072 *
7073 * This routine enables a lun for oas operations. The routines does so by
7074 * doing the following :
7075 *
7076 * 1) Checks to see if the device data for the lun has been created.
7077 * 2) If found, sets the OAS enabled flag if not set and returns.
7078 * 3) Otherwise, creates a device data structure.
7079 * 4) If successfully created, indicates the device data is for an OAS lun,
7080 * indicates the lun is not available and add to the list of luns.
7081 *
7082 * Return codes:
7083 * false - Error
7084 * true - Success
7085 **/
7086bool
7087lpfc_enable_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
James Smartc92c8412016-07-06 12:36:05 -07007088 struct lpfc_name *target_wwpn, uint64_t lun, uint8_t pri)
James Smart1ba981f2014-02-20 09:56:45 -05007089{
7090
7091 struct lpfc_device_data *lun_info;
7092 unsigned long flags;
7093
7094 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
James Smartf38fa0b2014-04-04 13:52:21 -04007095 !phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05007096 return false;
7097
7098 spin_lock_irqsave(&phba->devicelock, flags);
7099
7100 /* Check to see if the device data for the lun has been created */
7101 lun_info = __lpfc_get_device_data(phba, &phba->luns, vport_wwpn,
7102 target_wwpn, lun);
7103 if (lun_info) {
7104 if (!lun_info->oas_enabled)
7105 lun_info->oas_enabled = true;
James Smartb5749fe2016-12-19 15:07:26 -08007106 lun_info->priority = pri;
James Smart1ba981f2014-02-20 09:56:45 -05007107 spin_unlock_irqrestore(&phba->devicelock, flags);
7108 return true;
7109 }
7110
7111 /* Create an lun info structure and add to list of luns */
7112 lun_info = lpfc_create_device_data(phba, vport_wwpn, target_wwpn, lun,
James Smart2d71dc82019-05-21 17:48:51 -07007113 pri, true);
James Smart1ba981f2014-02-20 09:56:45 -05007114 if (lun_info) {
7115 lun_info->oas_enabled = true;
James Smartc92c8412016-07-06 12:36:05 -07007116 lun_info->priority = pri;
James Smart1ba981f2014-02-20 09:56:45 -05007117 lun_info->available = false;
7118 list_add_tail(&lun_info->listentry, &phba->luns);
7119 spin_unlock_irqrestore(&phba->devicelock, flags);
7120 return true;
7121 }
7122 spin_unlock_irqrestore(&phba->devicelock, flags);
7123 return false;
7124}
7125
7126/**
7127 * lpfc_disable_oas_lun - disables a lun for OAS operations
Lee Joneseceee002020-11-02 14:23:43 +00007128 * @phba: Pointer to host bus adapter structure.
James Smart1ba981f2014-02-20 09:56:45 -05007129 * @vport_wwpn: Pointer to vport's wwpn information
7130 * @target_wwpn: Pointer to target's wwpn information
7131 * @lun: Lun
Lee Joneseceee002020-11-02 14:23:43 +00007132 * @pri: Priority
James Smart1ba981f2014-02-20 09:56:45 -05007133 *
7134 * This routine disables a lun for oas operations. The routines does so by
7135 * doing the following :
7136 *
7137 * 1) Checks to see if the device data for the lun is created.
7138 * 2) If present, clears the flag indicating this lun is for OAS.
7139 * 3) If the lun is not available by the system, the device data is
7140 * freed.
7141 *
7142 * Return codes:
7143 * false - Error
7144 * true - Success
7145 **/
7146bool
7147lpfc_disable_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
James Smartb5749fe2016-12-19 15:07:26 -08007148 struct lpfc_name *target_wwpn, uint64_t lun, uint8_t pri)
James Smart1ba981f2014-02-20 09:56:45 -05007149{
7150
7151 struct lpfc_device_data *lun_info;
7152 unsigned long flags;
7153
7154 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
James Smartf38fa0b2014-04-04 13:52:21 -04007155 !phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05007156 return false;
7157
7158 spin_lock_irqsave(&phba->devicelock, flags);
7159
7160 /* Check to see if the lun is available. */
7161 lun_info = __lpfc_get_device_data(phba,
7162 &phba->luns, vport_wwpn,
7163 target_wwpn, lun);
7164 if (lun_info) {
7165 lun_info->oas_enabled = false;
James Smartb5749fe2016-12-19 15:07:26 -08007166 lun_info->priority = pri;
James Smart1ba981f2014-02-20 09:56:45 -05007167 if (!lun_info->available)
7168 lpfc_delete_device_data(phba, lun_info);
7169 spin_unlock_irqrestore(&phba->devicelock, flags);
7170 return true;
7171 }
7172
7173 spin_unlock_irqrestore(&phba->devicelock, flags);
7174 return false;
7175}
James Smart92d7f7b2007-06-17 19:56:38 -05007176
James Smart895427b2017-02-12 13:52:30 -08007177static int
7178lpfc_no_command(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
7179{
7180 return SCSI_MLQUEUE_HOST_BUSY;
7181}
7182
7183static int
7184lpfc_no_handler(struct scsi_cmnd *cmnd)
7185{
7186 return FAILED;
7187}
7188
7189static int
7190lpfc_no_slave(struct scsi_device *sdev)
7191{
7192 return -ENODEV;
7193}
7194
7195struct scsi_host_template lpfc_template_nvme = {
7196 .module = THIS_MODULE,
7197 .name = LPFC_DRIVER_NAME,
7198 .proc_name = LPFC_DRIVER_NAME,
7199 .info = lpfc_info,
7200 .queuecommand = lpfc_no_command,
7201 .eh_abort_handler = lpfc_no_handler,
7202 .eh_device_reset_handler = lpfc_no_handler,
7203 .eh_target_reset_handler = lpfc_no_handler,
7204 .eh_bus_reset_handler = lpfc_no_handler,
7205 .eh_host_reset_handler = lpfc_no_handler,
7206 .slave_alloc = lpfc_no_slave,
7207 .slave_configure = lpfc_no_slave,
7208 .scan_finished = lpfc_scan_finished,
7209 .this_id = -1,
7210 .sg_tablesize = 1,
7211 .cmd_per_lun = 1,
James Smart895427b2017-02-12 13:52:30 -08007212 .shost_attrs = lpfc_hba_attrs,
James Smart7c30bb62020-10-20 13:27:16 -07007213 .max_sectors = 0xFFFFFFFF,
James Smart895427b2017-02-12 13:52:30 -08007214 .vendor_id = LPFC_NL_VENDOR_ID,
7215 .track_queue_depth = 0,
7216};
7217
dea31012005-04-17 16:05:31 -05007218struct scsi_host_template lpfc_template = {
7219 .module = THIS_MODULE,
7220 .name = LPFC_DRIVER_NAME,
James Smart08dcd4c2016-07-06 12:35:59 -07007221 .proc_name = LPFC_DRIVER_NAME,
dea31012005-04-17 16:05:31 -05007222 .info = lpfc_info,
7223 .queuecommand = lpfc_queuecommand,
Christoph Hellwigb6a05c82017-01-30 13:18:58 +01007224 .eh_timed_out = fc_eh_timed_out,
Muneendra Kumar7f3a79a2021-01-07 03:19:08 +05307225 .eh_should_retry_cmd = fc_eh_should_retry_cmd,
dea31012005-04-17 16:05:31 -05007226 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04007227 .eh_device_reset_handler = lpfc_device_reset_handler,
7228 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart7054a602007-04-25 09:52:34 -04007229 .eh_bus_reset_handler = lpfc_bus_reset_handler,
James Smart27b01b82012-05-09 21:19:44 -04007230 .eh_host_reset_handler = lpfc_host_reset_handler,
dea31012005-04-17 16:05:31 -05007231 .slave_alloc = lpfc_slave_alloc,
7232 .slave_configure = lpfc_slave_configure,
7233 .slave_destroy = lpfc_slave_destroy,
James Smart47a86172007-04-25 09:53:22 -04007234 .scan_finished = lpfc_scan_finished,
dea31012005-04-17 16:05:31 -05007235 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05007236 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
dea31012005-04-17 16:05:31 -05007237 .cmd_per_lun = LPFC_CMD_PER_LUN,
James Smart2e0fef82007-06-17 19:56:36 -05007238 .shost_attrs = lpfc_hba_attrs,
James Smart7c30bb62020-10-20 13:27:16 -07007239 .max_sectors = 0xFFFFFFFF,
James Smartf1c3b0f2009-07-19 10:01:32 -04007240 .vendor_id = LPFC_NL_VENDOR_ID,
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01007241 .change_queue_depth = scsi_change_queue_depth,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01007242 .track_queue_depth = 1,
dea31012005-04-17 16:05:31 -05007243};