blob: ad62fb3f3a54d5615d2cb1c8a9703579013302cd [file] [log] [blame]
James Smartd85296c2012-03-01 22:38:13 -05001/*******************************************************************
dea31012005-04-17 16:05:31 -05002 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
James Smart145e5a82020-01-27 16:23:12 -08004 * Copyright (C) 2017-2020 Broadcom. All Rights Reserved. The term *
James Smart3e21d1c2018-05-04 20:37:59 -07005 * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. *
James Smart51f4ca32016-07-06 12:36:13 -07006 * Copyright (C) 2004-2016 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * EMULEX and SLI are trademarks of Emulex. *
James Smartd080abe2017-02-12 13:52:39 -08008 * www.broadcom.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04009 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -050010 * *
11 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040012 * modify it under the terms of version 2 of the GNU General *
13 * Public License as published by the Free Software Foundation. *
14 * This program is distributed in the hope that it will be useful. *
15 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
16 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
17 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
18 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
19 * TO BE LEGALLY INVALID. See the GNU General Public License for *
20 * more details, a copy of which can be found in the file COPYING *
21 * included with this package. *
dea31012005-04-17 16:05:31 -050022 *******************************************************************/
dea31012005-04-17 16:05:31 -050023#include <linux/pci.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
dea31012005-04-17 16:05:31 -050025#include <linux/interrupt.h>
Paul Gortmaker09703662011-05-27 09:37:25 -040026#include <linux/export.h>
James Smarta90f5682006-08-17 11:58:04 -040027#include <linux/delay.h>
James Smarte2a0a9d2008-12-04 22:40:02 -050028#include <asm/unaligned.h>
Dmitry Monakhov128b6f92017-06-29 11:31:12 -070029#include <linux/t10-pi.h>
James Smart737d4242013-04-17 20:14:49 -040030#include <linux/crc-t10dif.h>
31#include <net/checksum.h>
dea31012005-04-17 16:05:31 -050032
33#include <scsi/scsi.h>
34#include <scsi/scsi_device.h>
James Smarte2a0a9d2008-12-04 22:40:02 -050035#include <scsi/scsi_eh.h>
dea31012005-04-17 16:05:31 -050036#include <scsi/scsi_host.h>
37#include <scsi/scsi_tcq.h>
38#include <scsi/scsi_transport_fc.h>
39
40#include "lpfc_version.h"
James Smartda0436e2009-05-22 14:51:39 -040041#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050042#include "lpfc_hw.h"
43#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040044#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040045#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050046#include "lpfc_disc.h"
dea31012005-04-17 16:05:31 -050047#include "lpfc.h"
James Smart9a6b09c2012-03-01 22:37:42 -050048#include "lpfc_scsi.h"
dea31012005-04-17 16:05:31 -050049#include "lpfc_logmsg.h"
50#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050051#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050052
53#define LPFC_RESET_WAIT 2
54#define LPFC_ABORT_WAIT 2
55
James Smarte2a0a9d2008-12-04 22:40:02 -050056static char *dif_op_str[] = {
James Smart9a6b09c2012-03-01 22:37:42 -050057 "PROT_NORMAL",
58 "PROT_READ_INSERT",
59 "PROT_WRITE_STRIP",
60 "PROT_READ_STRIP",
61 "PROT_WRITE_INSERT",
62 "PROT_READ_PASS",
63 "PROT_WRITE_PASS",
64};
65
James Smartf9bb2da2011-10-10 21:34:11 -040066struct scsi_dif_tuple {
67 __be16 guard_tag; /* Checksum */
68 __be16 app_tag; /* Opaque storage */
69 __be32 ref_tag; /* Target LBA or indirect LBA */
70};
71
James Smart1ba981f2014-02-20 09:56:45 -050072static struct lpfc_rport_data *
73lpfc_rport_data_from_scsi_device(struct scsi_device *sdev)
74{
75 struct lpfc_vport *vport = (struct lpfc_vport *)sdev->host->hostdata;
76
James Smartf38fa0b2014-04-04 13:52:21 -040077 if (vport->phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -050078 return ((struct lpfc_device_data *)sdev->hostdata)->rport_data;
79 else
80 return (struct lpfc_rport_data *)sdev->hostdata;
81}
82
James Smartda0436e2009-05-22 14:51:39 -040083static void
James Smartc4908502019-01-28 11:14:28 -080084lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_io_buf *psb);
James Smart1c6f4ef52009-11-18 15:40:49 -050085static void
James Smartc4908502019-01-28 11:14:28 -080086lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_io_buf *psb);
James Smart9c6aa9d2013-05-31 17:03:39 -040087static int
88lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc);
James Smarte2a0a9d2008-12-04 22:40:02 -050089
James Smart9c6aa9d2013-05-31 17:03:39 -040090static inline unsigned
91lpfc_cmd_blksize(struct scsi_cmnd *sc)
92{
93 return sc->device->sector_size;
94}
95
96#define LPFC_CHECK_PROTECT_GUARD 1
97#define LPFC_CHECK_PROTECT_REF 2
98static inline unsigned
99lpfc_cmd_protect(struct scsi_cmnd *sc, int flag)
100{
101 return 1;
102}
103
104static inline unsigned
105lpfc_cmd_guard_csum(struct scsi_cmnd *sc)
106{
107 if (lpfc_prot_group_type(NULL, sc) == LPFC_PG_TYPE_NO_DIF)
108 return 0;
109 if (scsi_host_get_guard(sc->device->host) == SHOST_DIX_GUARD_IP)
110 return 1;
111 return 0;
112}
113
James Smartea2151b2008-09-07 11:52:10 -0400114/**
James Smartf1126682009-06-10 17:22:44 -0400115 * lpfc_sli4_set_rsp_sgl_last - Set the last bit in the response sge.
116 * @phba: Pointer to HBA object.
117 * @lpfc_cmd: lpfc scsi command object pointer.
118 *
119 * This function is called from the lpfc_prep_task_mgmt_cmd function to
120 * set the last bit in the response sge entry.
121 **/
122static void
123lpfc_sli4_set_rsp_sgl_last(struct lpfc_hba *phba,
James Smartc4908502019-01-28 11:14:28 -0800124 struct lpfc_io_buf *lpfc_cmd)
James Smartf1126682009-06-10 17:22:44 -0400125{
James Smart0794d602019-01-28 11:14:19 -0800126 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
James Smartf1126682009-06-10 17:22:44 -0400127 if (sgl) {
128 sgl += 1;
129 sgl->word2 = le32_to_cpu(sgl->word2);
130 bf_set(lpfc_sli4_sge_last, sgl, 1);
131 sgl->word2 = cpu_to_le32(sgl->word2);
132 }
133}
134
135/**
James Smart3621a712009-04-06 18:47:14 -0400136 * lpfc_update_stats - Update statistical data for the command completion
James Smart9df0a032019-09-21 20:58:54 -0700137 * @vport: The virtual port on which this call is executing.
James Smartea2151b2008-09-07 11:52:10 -0400138 * @lpfc_cmd: lpfc scsi command object pointer.
139 *
140 * This function is called when there is a command completion and this
141 * function updates the statistical data for the command completion.
142 **/
143static void
James Smart9df0a032019-09-21 20:58:54 -0700144lpfc_update_stats(struct lpfc_vport *vport, struct lpfc_io_buf *lpfc_cmd)
James Smartea2151b2008-09-07 11:52:10 -0400145{
James Smart9df0a032019-09-21 20:58:54 -0700146 struct lpfc_hba *phba = vport->phba;
James Smart18027a82018-09-10 10:30:49 -0700147 struct lpfc_rport_data *rdata;
148 struct lpfc_nodelist *pnode;
James Smartea2151b2008-09-07 11:52:10 -0400149 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
150 unsigned long flags;
James Smart9df0a032019-09-21 20:58:54 -0700151 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartea2151b2008-09-07 11:52:10 -0400152 unsigned long latency;
153 int i;
154
James Smart18027a82018-09-10 10:30:49 -0700155 if (!vport->stat_data_enabled ||
156 vport->stat_data_blocked ||
157 (cmd->result))
James Smartea2151b2008-09-07 11:52:10 -0400158 return;
159
James Smart9f1e1b52008-12-04 22:39:40 -0500160 latency = jiffies_to_msecs((long)jiffies - (long)lpfc_cmd->start_time);
James Smart18027a82018-09-10 10:30:49 -0700161 rdata = lpfc_cmd->rdata;
162 pnode = rdata->pnode;
James Smart9f1e1b52008-12-04 22:39:40 -0500163
James Smartea2151b2008-09-07 11:52:10 -0400164 spin_lock_irqsave(shost->host_lock, flags);
James Smart18027a82018-09-10 10:30:49 -0700165 if (!pnode ||
166 !pnode->lat_data ||
167 (phba->bucket_type == LPFC_NO_BUCKET)) {
James Smartea2151b2008-09-07 11:52:10 -0400168 spin_unlock_irqrestore(shost->host_lock, flags);
169 return;
170 }
James Smartea2151b2008-09-07 11:52:10 -0400171
172 if (phba->bucket_type == LPFC_LINEAR_BUCKET) {
173 i = (latency + phba->bucket_step - 1 - phba->bucket_base)/
174 phba->bucket_step;
James Smart9f1e1b52008-12-04 22:39:40 -0500175 /* check array subscript bounds */
176 if (i < 0)
177 i = 0;
178 else if (i >= LPFC_MAX_BUCKET_COUNT)
179 i = LPFC_MAX_BUCKET_COUNT - 1;
James Smartea2151b2008-09-07 11:52:10 -0400180 } else {
181 for (i = 0; i < LPFC_MAX_BUCKET_COUNT-1; i++)
182 if (latency <= (phba->bucket_base +
183 ((1<<i)*phba->bucket_step)))
184 break;
185 }
186
187 pnode->lat_data[i].cmd_count++;
188 spin_unlock_irqrestore(shost->host_lock, flags);
189}
190
James Smartea2151b2008-09-07 11:52:10 -0400191/**
James Smart3621a712009-04-06 18:47:14 -0400192 * lpfc_rampdown_queue_depth - Post RAMP_DOWN_QUEUE event to worker thread
James Smart9bad7672008-12-04 22:39:02 -0500193 * @phba: The Hba for which this call is being executed.
194 *
195 * This routine is called when there is resource error in driver or firmware.
196 * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
197 * posts at most 1 event each second. This routine wakes up worker thread of
198 * @phba to process WORKER_RAM_DOWN_EVENT event.
199 *
200 * This routine should be called with no lock held.
201 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500202void
James Smarteaf15d52008-12-04 22:39:29 -0500203lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
James Smart92d7f7b2007-06-17 19:56:38 -0500204{
205 unsigned long flags;
James Smart5e9d9b82008-06-14 22:52:53 -0400206 uint32_t evt_posted;
Manuel Schölling0d4aec12014-09-03 12:55:58 -0400207 unsigned long expires;
James Smart92d7f7b2007-06-17 19:56:38 -0500208
209 spin_lock_irqsave(&phba->hbalock, flags);
210 atomic_inc(&phba->num_rsrc_err);
211 phba->last_rsrc_error_time = jiffies;
212
Manuel Schölling0d4aec12014-09-03 12:55:58 -0400213 expires = phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL;
214 if (time_after(expires, jiffies)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500215 spin_unlock_irqrestore(&phba->hbalock, flags);
216 return;
217 }
218
219 phba->last_ramp_down_time = jiffies;
220
221 spin_unlock_irqrestore(&phba->hbalock, flags);
222
223 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400224 evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
225 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500226 phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500227 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
228
James Smart5e9d9b82008-06-14 22:52:53 -0400229 if (!evt_posted)
230 lpfc_worker_wake_up(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500231 return;
232}
233
James Smart9bad7672008-12-04 22:39:02 -0500234/**
James Smart3621a712009-04-06 18:47:14 -0400235 * lpfc_ramp_down_queue_handler - WORKER_RAMP_DOWN_QUEUE event handler
James Smart9bad7672008-12-04 22:39:02 -0500236 * @phba: The Hba for which this call is being executed.
237 *
238 * This routine is called to process WORKER_RAMP_DOWN_QUEUE event for worker
239 * thread.This routine reduces queue depth for all scsi device on each vport
240 * associated with @phba.
241 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500242void
243lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
244{
James Smart549e55c2007-08-02 11:09:51 -0400245 struct lpfc_vport **vports;
246 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500247 struct scsi_device *sdev;
James Smart5ffc2662009-11-18 15:39:44 -0500248 unsigned long new_queue_depth;
James Smart92d7f7b2007-06-17 19:56:38 -0500249 unsigned long num_rsrc_err, num_cmd_success;
James Smart549e55c2007-08-02 11:09:51 -0400250 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500251
252 num_rsrc_err = atomic_read(&phba->num_rsrc_err);
253 num_cmd_success = atomic_read(&phba->num_cmd_success);
254
James Smart75ad83a2012-05-09 21:18:40 -0400255 /*
256 * The error and success command counters are global per
257 * driver instance. If another handler has already
258 * operated on this error event, just exit.
259 */
260 if (num_rsrc_err == 0)
261 return;
262
James Smart549e55c2007-08-02 11:09:51 -0400263 vports = lpfc_create_vport_work_array(phba);
264 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400265 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400266 shost = lpfc_shost_from_vport(vports[i]);
267 shost_for_each_device(sdev, shost) {
James Smart92d7f7b2007-06-17 19:56:38 -0500268 new_queue_depth =
James Smart549e55c2007-08-02 11:09:51 -0400269 sdev->queue_depth * num_rsrc_err /
270 (num_rsrc_err + num_cmd_success);
271 if (!new_queue_depth)
272 new_queue_depth = sdev->queue_depth - 1;
273 else
274 new_queue_depth = sdev->queue_depth -
275 new_queue_depth;
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +0100276 scsi_change_queue_depth(sdev, new_queue_depth);
James Smart549e55c2007-08-02 11:09:51 -0400277 }
James Smart92d7f7b2007-06-17 19:56:38 -0500278 }
James Smart09372822008-01-11 01:52:54 -0500279 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500280 atomic_set(&phba->num_rsrc_err, 0);
281 atomic_set(&phba->num_cmd_success, 0);
282}
283
James Smart9bad7672008-12-04 22:39:02 -0500284/**
James Smart3621a712009-04-06 18:47:14 -0400285 * lpfc_scsi_dev_block - set all scsi hosts to block state
James Smarta8e497d2008-08-24 21:50:11 -0400286 * @phba: Pointer to HBA context object.
287 *
288 * This function walks vport list and set each SCSI host to block state
289 * by invoking fc_remote_port_delete() routine. This function is invoked
290 * with EEH when device's PCI slot has been permanently disabled.
291 **/
292void
293lpfc_scsi_dev_block(struct lpfc_hba *phba)
294{
295 struct lpfc_vport **vports;
296 struct Scsi_Host *shost;
297 struct scsi_device *sdev;
298 struct fc_rport *rport;
299 int i;
300
301 vports = lpfc_create_vport_work_array(phba);
302 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400303 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8e497d2008-08-24 21:50:11 -0400304 shost = lpfc_shost_from_vport(vports[i]);
305 shost_for_each_device(sdev, shost) {
306 rport = starget_to_rport(scsi_target(sdev));
307 fc_remote_port_delete(rport);
308 }
309 }
310 lpfc_destroy_vport_work_array(phba, vports);
311}
312
James Smart9bad7672008-12-04 22:39:02 -0500313/**
James Smart3772a992009-05-22 14:50:54 -0400314 * lpfc_new_scsi_buf_s3 - Scsi buffer allocator for HBA with SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -0500315 * @vport: The virtual port for which this call being executed.
James Smart3772a992009-05-22 14:50:54 -0400316 * @num_to_allocate: The requested number of buffers to allocate.
James Smart9bad7672008-12-04 22:39:02 -0500317 *
James Smart3772a992009-05-22 14:50:54 -0400318 * This routine allocates a scsi buffer for device with SLI-3 interface spec,
319 * the scsi buffer contains all the necessary information needed to initiate
320 * a SCSI I/O. The non-DMAable buffer region contains information to build
321 * the IOCB. The DMAable region contains memory for the FCP CMND, FCP RSP,
322 * and the initial BPL. In addition to allocating memory, the FCP CMND and
323 * FCP RSP BDEs are setup in the BPL and the BPL BDE is setup in the IOCB.
James Smart9bad7672008-12-04 22:39:02 -0500324 *
325 * Return codes:
James Smart3772a992009-05-22 14:50:54 -0400326 * int - number of scsi buffers that were allocated.
327 * 0 = failure, less than num_to_alloc is a partial failure.
James Smart9bad7672008-12-04 22:39:02 -0500328 **/
James Smart3772a992009-05-22 14:50:54 -0400329static int
330lpfc_new_scsi_buf_s3(struct lpfc_vport *vport, int num_to_alloc)
dea31012005-04-17 16:05:31 -0500331{
James Smart2e0fef82007-06-17 19:56:36 -0500332 struct lpfc_hba *phba = vport->phba;
James Smartc4908502019-01-28 11:14:28 -0800333 struct lpfc_io_buf *psb;
dea31012005-04-17 16:05:31 -0500334 struct ulp_bde64 *bpl;
335 IOCB_t *iocb;
James Smart34b02dc2008-08-24 21:49:55 -0400336 dma_addr_t pdma_phys_fcp_cmd;
337 dma_addr_t pdma_phys_fcp_rsp;
James Smart0794d602019-01-28 11:14:19 -0800338 dma_addr_t pdma_phys_sgl;
James Bottomley604a3e32005-10-29 10:28:33 -0500339 uint16_t iotag;
James Smart96f70772013-04-17 20:16:15 -0400340 int bcnt, bpl_size;
341
342 bpl_size = phba->cfg_sg_dma_buf_size -
343 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
344
345 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
346 "9067 ALLOC %d scsi_bufs: %d (%d + %d + %d)\n",
347 num_to_alloc, phba->cfg_sg_dma_buf_size,
348 (int)sizeof(struct fcp_cmnd),
349 (int)sizeof(struct fcp_rsp), bpl_size);
dea31012005-04-17 16:05:31 -0500350
James Smart3772a992009-05-22 14:50:54 -0400351 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
James Smartc4908502019-01-28 11:14:28 -0800352 psb = kzalloc(sizeof(struct lpfc_io_buf), GFP_KERNEL);
James Smart3772a992009-05-22 14:50:54 -0400353 if (!psb)
354 break;
dea31012005-04-17 16:05:31 -0500355
James Smart3772a992009-05-22 14:50:54 -0400356 /*
357 * Get memory from the pci pool to map the virt space to pci
358 * bus space for an I/O. The DMA buffer includes space for the
359 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
360 * necessary to support the sg_tablesize.
361 */
Romain Perier771db5c2017-07-06 10:13:05 +0200362 psb->data = dma_pool_zalloc(phba->lpfc_sg_dma_buf_pool,
James Smart3772a992009-05-22 14:50:54 -0400363 GFP_KERNEL, &psb->dma_handle);
364 if (!psb->data) {
365 kfree(psb);
366 break;
367 }
dea31012005-04-17 16:05:31 -0500368
dea31012005-04-17 16:05:31 -0500369
James Smart3772a992009-05-22 14:50:54 -0400370 /* Allocate iotag for psb->cur_iocbq. */
371 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
372 if (iotag == 0) {
Romain Perier771db5c2017-07-06 10:13:05 +0200373 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
James Smart895427b2017-02-12 13:52:30 -0800374 psb->data, psb->dma_handle);
James Smart3772a992009-05-22 14:50:54 -0400375 kfree(psb);
376 break;
377 }
378 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Bottomley604a3e32005-10-29 10:28:33 -0500379
James Smart3772a992009-05-22 14:50:54 -0400380 psb->fcp_cmnd = psb->data;
381 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
James Smart0794d602019-01-28 11:14:19 -0800382 psb->dma_sgl = psb->data + sizeof(struct fcp_cmnd) +
James Smart34b02dc2008-08-24 21:49:55 -0400383 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500384
James Smart3772a992009-05-22 14:50:54 -0400385 /* Initialize local short-hand pointers. */
James Smartc4908502019-01-28 11:14:28 -0800386 bpl = (struct ulp_bde64 *)psb->dma_sgl;
James Smart3772a992009-05-22 14:50:54 -0400387 pdma_phys_fcp_cmd = psb->dma_handle;
388 pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
James Smart0794d602019-01-28 11:14:19 -0800389 pdma_phys_sgl = psb->dma_handle + sizeof(struct fcp_cmnd) +
James Smart3772a992009-05-22 14:50:54 -0400390 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500391
James Smart3772a992009-05-22 14:50:54 -0400392 /*
393 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
394 * are sg list bdes. Initialize the first two and leave the
395 * rest for queuecommand.
396 */
397 bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
398 bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
399 bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
400 bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
401 bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
dea31012005-04-17 16:05:31 -0500402
James Smart3772a992009-05-22 14:50:54 -0400403 /* Setup the physical region for the FCP RSP */
404 bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
405 bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
406 bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
407 bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
408 bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
409
410 /*
411 * Since the IOCB for the FCP I/O is built into this
412 * lpfc_scsi_buf, initialize it with all known data now.
413 */
414 iocb = &psb->cur_iocbq.iocb;
415 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
416 if ((phba->sli_rev == 3) &&
417 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
418 /* fill in immediate fcp command BDE */
419 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
420 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
421 iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
422 unsli3.fcp_ext.icd);
423 iocb->un.fcpi64.bdl.addrHigh = 0;
424 iocb->ulpBdeCount = 0;
425 iocb->ulpLe = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300426 /* fill in response BDE */
James Smart3772a992009-05-22 14:50:54 -0400427 iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags =
428 BUFF_TYPE_BDE_64;
429 iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
430 sizeof(struct fcp_rsp);
431 iocb->unsli3.fcp_ext.rbde.addrLow =
432 putPaddrLow(pdma_phys_fcp_rsp);
433 iocb->unsli3.fcp_ext.rbde.addrHigh =
434 putPaddrHigh(pdma_phys_fcp_rsp);
435 } else {
436 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
437 iocb->un.fcpi64.bdl.bdeSize =
438 (2 * sizeof(struct ulp_bde64));
439 iocb->un.fcpi64.bdl.addrLow =
James Smart0794d602019-01-28 11:14:19 -0800440 putPaddrLow(pdma_phys_sgl);
James Smart3772a992009-05-22 14:50:54 -0400441 iocb->un.fcpi64.bdl.addrHigh =
James Smart0794d602019-01-28 11:14:19 -0800442 putPaddrHigh(pdma_phys_sgl);
James Smart3772a992009-05-22 14:50:54 -0400443 iocb->ulpBdeCount = 1;
444 iocb->ulpLe = 1;
445 }
446 iocb->ulpClass = CLASS3;
447 psb->status = IOSTAT_SUCCESS;
James Smartda0436e2009-05-22 14:51:39 -0400448 /* Put it back into the SCSI buffer list */
James Smarteee88772010-09-29 11:19:08 -0400449 psb->cur_iocbq.context1 = psb;
James Smartc2017262019-01-28 11:14:37 -0800450 spin_lock_init(&psb->buf_lock);
James Smart1c6f4ef52009-11-18 15:40:49 -0500451 lpfc_release_scsi_buf_s3(phba, psb);
James Smart3772a992009-05-22 14:50:54 -0400452
James Smart34b02dc2008-08-24 21:49:55 -0400453 }
dea31012005-04-17 16:05:31 -0500454
James Smart3772a992009-05-22 14:50:54 -0400455 return bcnt;
dea31012005-04-17 16:05:31 -0500456}
457
James Smart9bad7672008-12-04 22:39:02 -0500458/**
James Smart1151e3e2011-02-16 12:39:35 -0500459 * lpfc_sli4_vport_delete_fcp_xri_aborted -Remove all ndlp references for vport
460 * @vport: pointer to lpfc vport data structure.
461 *
462 * This routine is invoked by the vport cleanup for deletions and the cleanup
463 * for an ndlp on removal.
464 **/
465void
466lpfc_sli4_vport_delete_fcp_xri_aborted(struct lpfc_vport *vport)
467{
468 struct lpfc_hba *phba = vport->phba;
James Smartc4908502019-01-28 11:14:28 -0800469 struct lpfc_io_buf *psb, *next_psb;
James Smart5e5b5112019-01-28 11:14:22 -0800470 struct lpfc_sli4_hdw_queue *qp;
James Smart1151e3e2011-02-16 12:39:35 -0500471 unsigned long iflag = 0;
James Smart5e5b5112019-01-28 11:14:22 -0800472 int idx;
James Smart1151e3e2011-02-16 12:39:35 -0500473
James Smartf6e84792019-01-28 11:14:38 -0800474 if (!(vport->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
James Smart895427b2017-02-12 13:52:30 -0800475 return;
James Smart5e5b5112019-01-28 11:14:22 -0800476
James Smart1151e3e2011-02-16 12:39:35 -0500477 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart5e5b5112019-01-28 11:14:22 -0800478 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
479 qp = &phba->sli4_hba.hdwq[idx];
480
James Smartc00f62e2019-08-14 16:57:11 -0700481 spin_lock(&qp->abts_io_buf_list_lock);
James Smart5e5b5112019-01-28 11:14:22 -0800482 list_for_each_entry_safe(psb, next_psb,
James Smartc00f62e2019-08-14 16:57:11 -0700483 &qp->lpfc_abts_io_buf_list, list) {
James Smartc438d062019-12-18 15:58:07 -0800484 if (psb->cur_iocbq.iocb_flag & LPFC_IO_NVME)
James Smartc00f62e2019-08-14 16:57:11 -0700485 continue;
486
James Smart5e5b5112019-01-28 11:14:22 -0800487 if (psb->rdata && psb->rdata->pnode &&
488 psb->rdata->pnode->vport == vport)
489 psb->rdata = NULL;
490 }
James Smartc00f62e2019-08-14 16:57:11 -0700491 spin_unlock(&qp->abts_io_buf_list_lock);
James Smart1151e3e2011-02-16 12:39:35 -0500492 }
James Smart1151e3e2011-02-16 12:39:35 -0500493 spin_unlock_irqrestore(&phba->hbalock, iflag);
494}
495
496/**
James Smartc00f62e2019-08-14 16:57:11 -0700497 * lpfc_sli4_io_xri_aborted - Fast-path process of fcp xri abort
James Smartda0436e2009-05-22 14:51:39 -0400498 * @phba: pointer to lpfc hba data structure.
499 * @axri: pointer to the fcp xri abort wcqe structure.
500 *
501 * This routine is invoked by the worker thread to process a SLI4 fast-path
James Smartc00f62e2019-08-14 16:57:11 -0700502 * FCP or NVME aborted xri.
James Smartda0436e2009-05-22 14:51:39 -0400503 **/
504void
James Smartc00f62e2019-08-14 16:57:11 -0700505lpfc_sli4_io_xri_aborted(struct lpfc_hba *phba,
506 struct sli4_wcqe_xri_aborted *axri, int idx)
James Smartda0436e2009-05-22 14:51:39 -0400507{
508 uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
James Smart19ca7602010-11-20 23:11:55 -0500509 uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
James Smartc4908502019-01-28 11:14:28 -0800510 struct lpfc_io_buf *psb, *next_psb;
James Smart5e5b5112019-01-28 11:14:22 -0800511 struct lpfc_sli4_hdw_queue *qp;
James Smartda0436e2009-05-22 14:51:39 -0400512 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -0500513 struct lpfc_iocbq *iocbq;
514 int i;
James Smart19ca7602010-11-20 23:11:55 -0500515 struct lpfc_nodelist *ndlp;
516 int rrq_empty = 0;
James Smart895427b2017-02-12 13:52:30 -0800517 struct lpfc_sli_ring *pring = phba->sli4_hba.els_wq->pring;
James Smartda0436e2009-05-22 14:51:39 -0400518
James Smart895427b2017-02-12 13:52:30 -0800519 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
520 return;
James Smart5e5b5112019-01-28 11:14:22 -0800521
522 qp = &phba->sli4_hba.hdwq[idx];
James Smart0f65ff62010-02-26 14:14:23 -0500523 spin_lock_irqsave(&phba->hbalock, iflag);
James Smartc00f62e2019-08-14 16:57:11 -0700524 spin_lock(&qp->abts_io_buf_list_lock);
James Smartda0436e2009-05-22 14:51:39 -0400525 list_for_each_entry_safe(psb, next_psb,
James Smartc00f62e2019-08-14 16:57:11 -0700526 &qp->lpfc_abts_io_buf_list, list) {
James Smartda0436e2009-05-22 14:51:39 -0400527 if (psb->cur_iocbq.sli4_xritag == xri) {
James Smartc00f62e2019-08-14 16:57:11 -0700528 list_del_init(&psb->list);
James Smart324e1c42019-10-18 14:18:21 -0700529 psb->flags &= ~LPFC_SBUF_XBUSY;
James Smartda0436e2009-05-22 14:51:39 -0400530 psb->status = IOSTAT_SUCCESS;
James Smartc438d062019-12-18 15:58:07 -0800531 if (psb->cur_iocbq.iocb_flag & LPFC_IO_NVME) {
James Smartc00f62e2019-08-14 16:57:11 -0700532 qp->abts_nvme_io_bufs--;
533 spin_unlock(&qp->abts_io_buf_list_lock);
534 spin_unlock_irqrestore(&phba->hbalock, iflag);
535 lpfc_sli4_nvme_xri_aborted(phba, axri, psb);
536 return;
537 }
James Smartc00f62e2019-08-14 16:57:11 -0700538 qp->abts_scsi_io_bufs--;
539 spin_unlock(&qp->abts_io_buf_list_lock);
540
James Smart1151e3e2011-02-16 12:39:35 -0500541 if (psb->rdata && psb->rdata->pnode)
542 ndlp = psb->rdata->pnode;
543 else
544 ndlp = NULL;
545
James Smart19ca7602010-11-20 23:11:55 -0500546 rrq_empty = list_empty(&phba->active_rrq_list);
James Smart0f65ff62010-02-26 14:14:23 -0500547 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartcb69f7d2011-12-13 13:21:57 -0500548 if (ndlp) {
James Smartee0f4fe2012-05-09 21:19:14 -0400549 lpfc_set_rrq_active(phba, ndlp,
550 psb->cur_iocbq.sli4_lxritag, rxid, 1);
James Smartcb69f7d2011-12-13 13:21:57 -0500551 lpfc_sli4_abts_err_handler(phba, ndlp, axri);
552 }
James Smartda0436e2009-05-22 14:51:39 -0400553 lpfc_release_scsi_buf_s4(phba, psb);
James Smart19ca7602010-11-20 23:11:55 -0500554 if (rrq_empty)
555 lpfc_worker_wake_up(phba);
James Smartda0436e2009-05-22 14:51:39 -0400556 return;
557 }
558 }
James Smartc00f62e2019-08-14 16:57:11 -0700559 spin_unlock(&qp->abts_io_buf_list_lock);
James Smart0f65ff62010-02-26 14:14:23 -0500560 for (i = 1; i <= phba->sli.last_iotag; i++) {
561 iocbq = phba->sli.iocbq_lookup[i];
562
James Smartc00f62e2019-08-14 16:57:11 -0700563 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
564 (iocbq->iocb_flag & LPFC_IO_LIBDFC))
James Smart0f65ff62010-02-26 14:14:23 -0500565 continue;
566 if (iocbq->sli4_xritag != xri)
567 continue;
James Smartc4908502019-01-28 11:14:28 -0800568 psb = container_of(iocbq, struct lpfc_io_buf, cur_iocbq);
James Smart324e1c42019-10-18 14:18:21 -0700569 psb->flags &= ~LPFC_SBUF_XBUSY;
James Smart0f65ff62010-02-26 14:14:23 -0500570 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart0e9bb8d2013-03-01 16:35:12 -0500571 if (!list_empty(&pring->txq))
James Smart589a52d2010-07-14 15:30:54 -0400572 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -0500573 return;
574
575 }
576 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartda0436e2009-05-22 14:51:39 -0400577}
578
579/**
James Smart19ca7602010-11-20 23:11:55 -0500580 * lpfc_get_scsi_buf_s3 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
James Smart3772a992009-05-22 14:50:54 -0400581 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -0500582 *
583 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
584 * and returns to caller.
585 *
586 * Return codes:
587 * NULL - Error
588 * Pointer to lpfc_scsi_buf - Success
589 **/
James Smartc4908502019-01-28 11:14:28 -0800590static struct lpfc_io_buf *
James Smartace44e42019-01-28 11:14:27 -0800591lpfc_get_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
592 struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -0500593{
James Smartc4908502019-01-28 11:14:28 -0800594 struct lpfc_io_buf *lpfc_cmd = NULL;
James Smarta40fc5f2013-04-17 20:17:40 -0400595 struct list_head *scsi_buf_list_get = &phba->lpfc_scsi_buf_list_get;
James Smart164cecd2013-09-06 12:22:38 -0400596 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500597
James Smart164cecd2013-09-06 12:22:38 -0400598 spin_lock_irqsave(&phba->scsi_buf_list_get_lock, iflag);
James Smartc4908502019-01-28 11:14:28 -0800599 list_remove_head(scsi_buf_list_get, lpfc_cmd, struct lpfc_io_buf,
James Smarta40fc5f2013-04-17 20:17:40 -0400600 list);
601 if (!lpfc_cmd) {
James Smart164cecd2013-09-06 12:22:38 -0400602 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -0400603 list_splice(&phba->lpfc_scsi_buf_list_put,
604 &phba->lpfc_scsi_buf_list_get);
605 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
606 list_remove_head(scsi_buf_list_get, lpfc_cmd,
James Smartc4908502019-01-28 11:14:28 -0800607 struct lpfc_io_buf, list);
James Smart164cecd2013-09-06 12:22:38 -0400608 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smart1dcb58e2007-04-25 09:51:30 -0400609 }
James Smart164cecd2013-09-06 12:22:38 -0400610 spin_unlock_irqrestore(&phba->scsi_buf_list_get_lock, iflag);
James Smart2a5b7d62018-07-31 17:23:22 -0700611
612 if (lpfc_ndlp_check_qdepth(phba, ndlp) && lpfc_cmd) {
613 atomic_inc(&ndlp->cmd_pending);
614 lpfc_cmd->flags |= LPFC_SBUF_BUMP_QDEPTH;
615 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400616 return lpfc_cmd;
617}
James Smart19ca7602010-11-20 23:11:55 -0500618/**
James Smart5e5b5112019-01-28 11:14:22 -0800619 * lpfc_get_scsi_buf_s4 - Get a scsi buffer from io_buf_list of the HBA
James Smart19ca7602010-11-20 23:11:55 -0500620 * @phba: The HBA for which this call is being executed.
621 *
James Smart5e5b5112019-01-28 11:14:22 -0800622 * This routine removes a scsi buffer from head of @hdwq io_buf_list
James Smart19ca7602010-11-20 23:11:55 -0500623 * and returns to caller.
624 *
625 * Return codes:
626 * NULL - Error
627 * Pointer to lpfc_scsi_buf - Success
628 **/
James Smartc4908502019-01-28 11:14:28 -0800629static struct lpfc_io_buf *
James Smartace44e42019-01-28 11:14:27 -0800630lpfc_get_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
631 struct scsi_cmnd *cmnd)
James Smart19ca7602010-11-20 23:11:55 -0500632{
James Smartc4908502019-01-28 11:14:28 -0800633 struct lpfc_io_buf *lpfc_cmd;
James Smart5e5b5112019-01-28 11:14:22 -0800634 struct lpfc_sli4_hdw_queue *qp;
James Smart0794d602019-01-28 11:14:19 -0800635 struct sli4_sge *sgl;
636 IOCB_t *iocb;
637 dma_addr_t pdma_phys_fcp_rsp;
638 dma_addr_t pdma_phys_fcp_cmd;
James Smartd79c9e92019-08-14 16:57:09 -0700639 uint32_t cpu, idx;
James Smartace44e42019-01-28 11:14:27 -0800640 int tag;
James Smartd79c9e92019-08-14 16:57:09 -0700641 struct fcp_cmd_rsp_buf *tmp = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500642
Bart Van Assched6d189c2019-03-28 11:06:22 -0700643 cpu = raw_smp_processor_id();
James Smart45aa3122019-01-28 11:14:29 -0800644 if (cmnd && phba->cfg_fcp_io_sched == LPFC_FCP_SCHED_BY_HDWQ) {
James Smartace44e42019-01-28 11:14:27 -0800645 tag = blk_mq_unique_tag(cmnd->request);
646 idx = blk_mq_unique_tag_to_hwq(tag);
647 } else {
James Smart6a828b02019-01-28 11:14:31 -0800648 idx = phba->sli4_hba.cpu_map[cpu].hdwq;
James Smartace44e42019-01-28 11:14:27 -0800649 }
James Smart5e5b5112019-01-28 11:14:22 -0800650
James Smartc4908502019-01-28 11:14:28 -0800651 lpfc_cmd = lpfc_get_io_buf(phba, ndlp, idx,
652 !phba->cfg_xri_rebalancing);
653 if (!lpfc_cmd) {
654 qp = &phba->sli4_hba.hdwq[idx];
James Smart5e5b5112019-01-28 11:14:22 -0800655 qp->empty_io_bufs++;
James Smart1151e3e2011-02-16 12:39:35 -0500656 return NULL;
James Smart5e5b5112019-01-28 11:14:22 -0800657 }
James Smart2a5b7d62018-07-31 17:23:22 -0700658
James Smart0794d602019-01-28 11:14:19 -0800659 /* Setup key fields in buffer that may have been changed
660 * if other protocols used this buffer.
661 */
662 lpfc_cmd->cur_iocbq.iocb_flag = LPFC_IO_FCP;
663 lpfc_cmd->prot_seg_cnt = 0;
664 lpfc_cmd->seg_cnt = 0;
James Smart0794d602019-01-28 11:14:19 -0800665 lpfc_cmd->timeout = 0;
666 lpfc_cmd->flags = 0;
667 lpfc_cmd->start_time = jiffies;
668 lpfc_cmd->waitq = NULL;
James Smart5e5b5112019-01-28 11:14:22 -0800669 lpfc_cmd->cpu = cpu;
James Smart0794d602019-01-28 11:14:19 -0800670#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
671 lpfc_cmd->prot_data_type = 0;
672#endif
James Smartd79c9e92019-08-14 16:57:09 -0700673 tmp = lpfc_get_cmd_rsp_buf_per_hdwq(phba, lpfc_cmd);
James Smart0ab384a2020-01-27 16:23:02 -0800674 if (!tmp) {
675 lpfc_release_io_buf(phba, lpfc_cmd, lpfc_cmd->hdwq);
James Smartd79c9e92019-08-14 16:57:09 -0700676 return NULL;
James Smart0ab384a2020-01-27 16:23:02 -0800677 }
James Smartd79c9e92019-08-14 16:57:09 -0700678
679 lpfc_cmd->fcp_cmnd = tmp->fcp_cmnd;
680 lpfc_cmd->fcp_rsp = tmp->fcp_rsp;
James Smart0794d602019-01-28 11:14:19 -0800681
682 /*
683 * The first two SGEs are the FCP_CMD and FCP_RSP.
684 * The balance are sg list bdes. Initialize the
685 * first two and leave the rest for queuecommand.
686 */
687 sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
James Smartd79c9e92019-08-14 16:57:09 -0700688 pdma_phys_fcp_cmd = tmp->fcp_cmd_rsp_dma_handle;
James Smart0794d602019-01-28 11:14:19 -0800689 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
690 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
691 sgl->word2 = le32_to_cpu(sgl->word2);
692 bf_set(lpfc_sli4_sge_last, sgl, 0);
693 sgl->word2 = cpu_to_le32(sgl->word2);
694 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_cmnd));
695 sgl++;
696
697 /* Setup the physical region for the FCP RSP */
698 pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
699 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
700 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
701 sgl->word2 = le32_to_cpu(sgl->word2);
702 bf_set(lpfc_sli4_sge_last, sgl, 1);
703 sgl->word2 = cpu_to_le32(sgl->word2);
704 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
705
706 /*
707 * Since the IOCB for the FCP I/O is built into this
James Smartc4908502019-01-28 11:14:28 -0800708 * lpfc_io_buf, initialize it with all known data now.
James Smart0794d602019-01-28 11:14:19 -0800709 */
710 iocb = &lpfc_cmd->cur_iocbq.iocb;
711 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
712 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
713 /* setting the BLP size to 2 * sizeof BDE may not be correct.
714 * We are setting the bpl to point to out sgl. An sgl's
715 * entries are 16 bytes, a bpl entries are 12 bytes.
716 */
717 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
718 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
719 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
720 iocb->ulpBdeCount = 1;
721 iocb->ulpLe = 1;
722 iocb->ulpClass = CLASS3;
723
Dan Carpenter26c724a2018-08-23 16:56:33 +0300724 if (lpfc_ndlp_check_qdepth(phba, ndlp)) {
James Smart2a5b7d62018-07-31 17:23:22 -0700725 atomic_inc(&ndlp->cmd_pending);
726 lpfc_cmd->flags |= LPFC_SBUF_BUMP_QDEPTH;
727 }
James Smarta40fc5f2013-04-17 20:17:40 -0400728 return lpfc_cmd;
James Smart19ca7602010-11-20 23:11:55 -0500729}
730/**
731 * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
732 * @phba: The HBA for which this call is being executed.
733 *
734 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
735 * and returns to caller.
736 *
737 * Return codes:
738 * NULL - Error
739 * Pointer to lpfc_scsi_buf - Success
740 **/
James Smartc4908502019-01-28 11:14:28 -0800741static struct lpfc_io_buf*
James Smartace44e42019-01-28 11:14:27 -0800742lpfc_get_scsi_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
743 struct scsi_cmnd *cmnd)
James Smart19ca7602010-11-20 23:11:55 -0500744{
James Smartace44e42019-01-28 11:14:27 -0800745 return phba->lpfc_get_scsi_buf(phba, ndlp, cmnd);
James Smart19ca7602010-11-20 23:11:55 -0500746}
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400747
James Smart9bad7672008-12-04 22:39:02 -0500748/**
James Smart3772a992009-05-22 14:50:54 -0400749 * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
James Smart9bad7672008-12-04 22:39:02 -0500750 * @phba: The Hba for which this call is being executed.
751 * @psb: The scsi buffer which is being released.
752 *
753 * This routine releases @psb scsi buffer by adding it to tail of @phba
754 * lpfc_scsi_buf_list list.
755 **/
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400756static void
James Smartc4908502019-01-28 11:14:28 -0800757lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_io_buf *psb)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400758{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500759 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500760
James Smarta40fc5f2013-04-17 20:17:40 -0400761 psb->seg_cnt = 0;
James Smarta40fc5f2013-04-17 20:17:40 -0400762 psb->prot_seg_cnt = 0;
763
764 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400765 psb->pCmd = NULL;
James Smart6a485eb2013-04-17 20:19:00 -0400766 psb->cur_iocbq.iocb_flag = LPFC_IO_FCP;
James Smarta40fc5f2013-04-17 20:17:40 -0400767 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list_put);
768 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
dea31012005-04-17 16:05:31 -0500769}
770
James Smart9bad7672008-12-04 22:39:02 -0500771/**
James Smartda0436e2009-05-22 14:51:39 -0400772 * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
773 * @phba: The Hba for which this call is being executed.
774 * @psb: The scsi buffer which is being released.
775 *
James Smart5e5b5112019-01-28 11:14:22 -0800776 * This routine releases @psb scsi buffer by adding it to tail of @hdwq
777 * io_buf_list list. For SLI4 XRI's are tied to the scsi buffer
James Smartda0436e2009-05-22 14:51:39 -0400778 * and cannot be reused for at least RA_TOV amount of time if it was
779 * aborted.
780 **/
781static void
James Smartc4908502019-01-28 11:14:28 -0800782lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_io_buf *psb)
James Smartda0436e2009-05-22 14:51:39 -0400783{
James Smart5e5b5112019-01-28 11:14:22 -0800784 struct lpfc_sli4_hdw_queue *qp;
James Smartda0436e2009-05-22 14:51:39 -0400785 unsigned long iflag = 0;
786
James Smarta40fc5f2013-04-17 20:17:40 -0400787 psb->seg_cnt = 0;
James Smarta40fc5f2013-04-17 20:17:40 -0400788 psb->prot_seg_cnt = 0;
789
James Smart1fbf9742019-01-28 11:14:26 -0800790 qp = psb->hdwq;
James Smart324e1c42019-10-18 14:18:21 -0700791 if (psb->flags & LPFC_SBUF_XBUSY) {
James Smartc00f62e2019-08-14 16:57:11 -0700792 spin_lock_irqsave(&qp->abts_io_buf_list_lock, iflag);
James Smartda0436e2009-05-22 14:51:39 -0400793 psb->pCmd = NULL;
James Smartc00f62e2019-08-14 16:57:11 -0700794 list_add_tail(&psb->list, &qp->lpfc_abts_io_buf_list);
James Smart5e5b5112019-01-28 11:14:22 -0800795 qp->abts_scsi_io_bufs++;
James Smartc00f62e2019-08-14 16:57:11 -0700796 spin_unlock_irqrestore(&qp->abts_io_buf_list_lock, iflag);
James Smartda0436e2009-05-22 14:51:39 -0400797 } else {
James Smartc4908502019-01-28 11:14:28 -0800798 lpfc_release_io_buf(phba, (struct lpfc_io_buf *)psb, qp);
James Smartda0436e2009-05-22 14:51:39 -0400799 }
800}
801
802/**
James Smart3772a992009-05-22 14:50:54 -0400803 * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
804 * @phba: The Hba for which this call is being executed.
805 * @psb: The scsi buffer which is being released.
806 *
807 * This routine releases @psb scsi buffer by adding it to tail of @phba
808 * lpfc_scsi_buf_list list.
809 **/
810static void
James Smartc4908502019-01-28 11:14:28 -0800811lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_io_buf *psb)
James Smart3772a992009-05-22 14:50:54 -0400812{
James Smart2a5b7d62018-07-31 17:23:22 -0700813 if ((psb->flags & LPFC_SBUF_BUMP_QDEPTH) && psb->ndlp)
814 atomic_dec(&psb->ndlp->cmd_pending);
James Smart3772a992009-05-22 14:50:54 -0400815
James Smart2a5b7d62018-07-31 17:23:22 -0700816 psb->flags &= ~LPFC_SBUF_BUMP_QDEPTH;
James Smart3772a992009-05-22 14:50:54 -0400817 phba->lpfc_release_scsi_buf(phba, psb);
818}
819
820/**
821 * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -0500822 * @phba: The Hba for which this call is being executed.
823 * @lpfc_cmd: The scsi buffer which is going to be mapped.
824 *
825 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
James Smart3772a992009-05-22 14:50:54 -0400826 * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -0700827 * through sg elements and format the bde. This routine also initializes all
James Smart3772a992009-05-22 14:50:54 -0400828 * IOCB fields which are dependent on scsi command request buffer.
James Smart9bad7672008-12-04 22:39:02 -0500829 *
830 * Return codes:
831 * 1 - Error
832 * 0 - Success
833 **/
dea31012005-04-17 16:05:31 -0500834static int
James Smartc4908502019-01-28 11:14:28 -0800835lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
dea31012005-04-17 16:05:31 -0500836{
837 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
838 struct scatterlist *sgel = NULL;
839 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smartc4908502019-01-28 11:14:28 -0800840 struct ulp_bde64 *bpl = (struct ulp_bde64 *)lpfc_cmd->dma_sgl;
James Smart0f65ff62010-02-26 14:14:23 -0500841 struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
dea31012005-04-17 16:05:31 -0500842 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart34b02dc2008-08-24 21:49:55 -0400843 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
dea31012005-04-17 16:05:31 -0500844 dma_addr_t physaddr;
James Smart34b02dc2008-08-24 21:49:55 -0400845 uint32_t num_bde = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500846 int nseg, datadir = scsi_cmnd->sc_data_direction;
dea31012005-04-17 16:05:31 -0500847
848 /*
849 * There are three possibilities here - use scatter-gather segment, use
850 * the single mapping, or neither. Start the lpfc command prep by
851 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
852 * data bde entry.
853 */
854 bpl += 2;
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -0700855 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -0500856 /*
857 * The driver stores the segment count returned from pci_map_sg
858 * because this a count of dma-mappings used to map the use_sg
859 * pages. They are not guaranteed to be the same for those
860 * architectures that implement an IOMMU.
861 */
dea31012005-04-17 16:05:31 -0500862
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -0700863 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
864 scsi_sg_count(scsi_cmnd), datadir);
865 if (unlikely(!nseg))
866 return 1;
867
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500868 lpfc_cmd->seg_cnt = nseg;
dea31012005-04-17 16:05:31 -0500869 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -0400870 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
871 "9064 BLKGRD: %s: Too many sg segments from "
James Smarte2a0a9d2008-12-04 22:40:02 -0500872 "dma_map_sg. Config %d, seg_cnt %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700873 __func__, phba->cfg_sg_seg_cnt,
dea31012005-04-17 16:05:31 -0500874 lpfc_cmd->seg_cnt);
James Smart5e0e2312019-08-14 16:56:54 -0700875 WARN_ON_ONCE(lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt);
James Smart96f70772013-04-17 20:16:15 -0400876 lpfc_cmd->seg_cnt = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500877 scsi_dma_unmap(scsi_cmnd);
James Smart5e0e2312019-08-14 16:56:54 -0700878 return 2;
dea31012005-04-17 16:05:31 -0500879 }
880
881 /*
882 * The driver established a maximum scatter-gather segment count
883 * during probe that limits the number of sg elements in any
884 * single scsi command. Just run through the seg_cnt and format
885 * the bde's.
James Smart34b02dc2008-08-24 21:49:55 -0400886 * When using SLI-3 the driver will try to fit all the BDEs into
887 * the IOCB. If it can't then the BDEs get added to a BPL as it
888 * does for SLI-2 mode.
dea31012005-04-17 16:05:31 -0500889 */
James Smart34b02dc2008-08-24 21:49:55 -0400890 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
dea31012005-04-17 16:05:31 -0500891 physaddr = sg_dma_address(sgel);
James Smart34b02dc2008-08-24 21:49:55 -0400892 if (phba->sli_rev == 3 &&
James Smarte2a0a9d2008-12-04 22:40:02 -0500893 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
James Smart0f65ff62010-02-26 14:14:23 -0500894 !(iocbq->iocb_flag & DSS_SECURITY_OP) &&
James Smart34b02dc2008-08-24 21:49:55 -0400895 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
896 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
897 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
898 data_bde->addrLow = putPaddrLow(physaddr);
899 data_bde->addrHigh = putPaddrHigh(physaddr);
900 data_bde++;
901 } else {
902 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
903 bpl->tus.f.bdeSize = sg_dma_len(sgel);
904 bpl->tus.w = le32_to_cpu(bpl->tus.w);
905 bpl->addrLow =
906 le32_to_cpu(putPaddrLow(physaddr));
907 bpl->addrHigh =
908 le32_to_cpu(putPaddrHigh(physaddr));
909 bpl++;
910 }
dea31012005-04-17 16:05:31 -0500911 }
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -0700912 }
dea31012005-04-17 16:05:31 -0500913
914 /*
915 * Finish initializing those IOCB fields that are dependent on the
James Smart34b02dc2008-08-24 21:49:55 -0400916 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
917 * explicitly reinitialized and for SLI-3 the extended bde count is
918 * explicitly reinitialized since all iocb memory resources are reused.
dea31012005-04-17 16:05:31 -0500919 */
James Smarte2a0a9d2008-12-04 22:40:02 -0500920 if (phba->sli_rev == 3 &&
James Smart0f65ff62010-02-26 14:14:23 -0500921 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
922 !(iocbq->iocb_flag & DSS_SECURITY_OP)) {
James Smart34b02dc2008-08-24 21:49:55 -0400923 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
924 /*
925 * The extended IOCB format can only fit 3 BDE or a BPL.
926 * This I/O has more than 3 BDE so the 1st data bde will
927 * be a BPL that is filled in here.
928 */
929 physaddr = lpfc_cmd->dma_handle;
930 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
931 data_bde->tus.f.bdeSize = (num_bde *
932 sizeof(struct ulp_bde64));
933 physaddr += (sizeof(struct fcp_cmnd) +
934 sizeof(struct fcp_rsp) +
935 (2 * sizeof(struct ulp_bde64)));
936 data_bde->addrHigh = putPaddrHigh(physaddr);
937 data_bde->addrLow = putPaddrLow(physaddr);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300938 /* ebde count includes the response bde and data bpl */
James Smart34b02dc2008-08-24 21:49:55 -0400939 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
940 } else {
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300941 /* ebde count includes the response bde and data bdes */
James Smart34b02dc2008-08-24 21:49:55 -0400942 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
943 }
944 } else {
945 iocb_cmd->un.fcpi64.bdl.bdeSize =
946 ((num_bde + 2) * sizeof(struct ulp_bde64));
James Smart0f65ff62010-02-26 14:14:23 -0500947 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
James Smart34b02dc2008-08-24 21:49:55 -0400948 }
James Smart09372822008-01-11 01:52:54 -0500949 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
James Smarte2a0a9d2008-12-04 22:40:02 -0500950
951 /*
952 * Due to difference in data length between DIF/non-DIF paths,
953 * we need to set word 4 of IOCB here
954 */
James Smarta257bf92009-04-06 18:48:10 -0400955 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
dea31012005-04-17 16:05:31 -0500956 return 0;
957}
958
James Smartf9bb2da2011-10-10 21:34:11 -0400959#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -0500960
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -0700961/* Return BG_ERR_INIT if error injection is detected by Initiator */
James Smart9a6b09c2012-03-01 22:37:42 -0500962#define BG_ERR_INIT 0x1
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -0700963/* Return BG_ERR_TGT if error injection is detected by Target */
James Smart9a6b09c2012-03-01 22:37:42 -0500964#define BG_ERR_TGT 0x2
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -0700965/* Return BG_ERR_SWAP if swapping CSUM<-->CRC is required for error injection */
James Smart9a6b09c2012-03-01 22:37:42 -0500966#define BG_ERR_SWAP 0x10
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -0700967/**
968 * Return BG_ERR_CHECK if disabling Guard/Ref/App checking is required for
969 * error injection
970 **/
James Smart9a6b09c2012-03-01 22:37:42 -0500971#define BG_ERR_CHECK 0x20
James Smartacd68592012-01-18 16:25:09 -0500972
973/**
974 * lpfc_bg_err_inject - Determine if we should inject an error
975 * @phba: The Hba for which this call is being executed.
James Smartf9bb2da2011-10-10 21:34:11 -0400976 * @sc: The SCSI command to examine
977 * @reftag: (out) BlockGuard reference tag for transmitted data
978 * @apptag: (out) BlockGuard application tag for transmitted data
979 * @new_guard (in) Value to replace CRC with if needed
980 *
James Smart9a6b09c2012-03-01 22:37:42 -0500981 * Returns BG_ERR_* bit mask or 0 if request ignored
James Smartacd68592012-01-18 16:25:09 -0500982 **/
James Smartf9bb2da2011-10-10 21:34:11 -0400983static int
984lpfc_bg_err_inject(struct lpfc_hba *phba, struct scsi_cmnd *sc,
985 uint32_t *reftag, uint16_t *apptag, uint32_t new_guard)
986{
987 struct scatterlist *sgpe; /* s/g prot entry */
James Smartc4908502019-01-28 11:14:28 -0800988 struct lpfc_io_buf *lpfc_cmd = NULL;
James Smartacd68592012-01-18 16:25:09 -0500989 struct scsi_dif_tuple *src = NULL;
James Smart4ac9b222012-03-01 22:38:29 -0500990 struct lpfc_nodelist *ndlp;
991 struct lpfc_rport_data *rdata;
James Smartf9bb2da2011-10-10 21:34:11 -0400992 uint32_t op = scsi_get_prot_op(sc);
993 uint32_t blksize;
994 uint32_t numblks;
995 sector_t lba;
996 int rc = 0;
James Smartacd68592012-01-18 16:25:09 -0500997 int blockoff = 0;
James Smartf9bb2da2011-10-10 21:34:11 -0400998
999 if (op == SCSI_PROT_NORMAL)
1000 return 0;
1001
James Smartacd68592012-01-18 16:25:09 -05001002 sgpe = scsi_prot_sglist(sc);
James Smartf9bb2da2011-10-10 21:34:11 -04001003 lba = scsi_get_lba(sc);
James Smart4ac9b222012-03-01 22:38:29 -05001004
1005 /* First check if we need to match the LBA */
James Smartf9bb2da2011-10-10 21:34:11 -04001006 if (phba->lpfc_injerr_lba != LPFC_INJERR_LBA_OFF) {
1007 blksize = lpfc_cmd_blksize(sc);
1008 numblks = (scsi_bufflen(sc) + blksize - 1) / blksize;
1009
1010 /* Make sure we have the right LBA if one is specified */
1011 if ((phba->lpfc_injerr_lba < lba) ||
1012 (phba->lpfc_injerr_lba >= (lba + numblks)))
1013 return 0;
James Smartacd68592012-01-18 16:25:09 -05001014 if (sgpe) {
1015 blockoff = phba->lpfc_injerr_lba - lba;
1016 numblks = sg_dma_len(sgpe) /
1017 sizeof(struct scsi_dif_tuple);
1018 if (numblks < blockoff)
1019 blockoff = numblks;
James Smartacd68592012-01-18 16:25:09 -05001020 }
James Smartf9bb2da2011-10-10 21:34:11 -04001021 }
1022
James Smart4ac9b222012-03-01 22:38:29 -05001023 /* Next check if we need to match the remote NPortID or WWPN */
James Smart1ba981f2014-02-20 09:56:45 -05001024 rdata = lpfc_rport_data_from_scsi_device(sc->device);
James Smart4ac9b222012-03-01 22:38:29 -05001025 if (rdata && rdata->pnode) {
1026 ndlp = rdata->pnode;
1027
1028 /* Make sure we have the right NPortID if one is specified */
1029 if (phba->lpfc_injerr_nportid &&
1030 (phba->lpfc_injerr_nportid != ndlp->nlp_DID))
1031 return 0;
1032
1033 /*
1034 * Make sure we have the right WWPN if one is specified.
1035 * wwn[0] should be a non-zero NAA in a good WWPN.
1036 */
1037 if (phba->lpfc_injerr_wwpn.u.wwn[0] &&
1038 (memcmp(&ndlp->nlp_portname, &phba->lpfc_injerr_wwpn,
1039 sizeof(struct lpfc_name)) != 0))
1040 return 0;
1041 }
1042
1043 /* Setup a ptr to the protection data if the SCSI host provides it */
1044 if (sgpe) {
1045 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
1046 src += blockoff;
James Smartc4908502019-01-28 11:14:28 -08001047 lpfc_cmd = (struct lpfc_io_buf *)sc->host_scribble;
James Smart4ac9b222012-03-01 22:38:29 -05001048 }
1049
James Smartf9bb2da2011-10-10 21:34:11 -04001050 /* Should we change the Reference Tag */
1051 if (reftag) {
James Smartacd68592012-01-18 16:25:09 -05001052 if (phba->lpfc_injerr_wref_cnt) {
1053 switch (op) {
1054 case SCSI_PROT_WRITE_PASS:
James Smart9a6b09c2012-03-01 22:37:42 -05001055 if (src) {
1056 /*
1057 * For WRITE_PASS, force the error
1058 * to be sent on the wire. It should
1059 * be detected by the Target.
1060 * If blockoff != 0 error will be
1061 * inserted in middle of the IO.
1062 */
James Smartf9bb2da2011-10-10 21:34:11 -04001063
James Smartacd68592012-01-18 16:25:09 -05001064 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1065 "9076 BLKGRD: Injecting reftag error: "
1066 "write lba x%lx + x%x oldrefTag x%x\n",
1067 (unsigned long)lba, blockoff,
James Smart9a6b09c2012-03-01 22:37:42 -05001068 be32_to_cpu(src->ref_tag));
James Smartacd68592012-01-18 16:25:09 -05001069
1070 /*
James Smart9a6b09c2012-03-01 22:37:42 -05001071 * Save the old ref_tag so we can
1072 * restore it on completion.
James Smartacd68592012-01-18 16:25:09 -05001073 */
James Smart9a6b09c2012-03-01 22:37:42 -05001074 if (lpfc_cmd) {
1075 lpfc_cmd->prot_data_type =
1076 LPFC_INJERR_REFTAG;
1077 lpfc_cmd->prot_data_segment =
1078 src;
1079 lpfc_cmd->prot_data =
1080 src->ref_tag;
1081 }
1082 src->ref_tag = cpu_to_be32(0xDEADBEEF);
James Smartacd68592012-01-18 16:25:09 -05001083 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001084 if (phba->lpfc_injerr_wref_cnt == 0) {
1085 phba->lpfc_injerr_nportid = 0;
1086 phba->lpfc_injerr_lba =
1087 LPFC_INJERR_LBA_OFF;
1088 memset(&phba->lpfc_injerr_wwpn,
1089 0, sizeof(struct lpfc_name));
1090 }
James Smart9a6b09c2012-03-01 22:37:42 -05001091 rc = BG_ERR_TGT | BG_ERR_CHECK;
1092
James Smartacd68592012-01-18 16:25:09 -05001093 break;
1094 }
Bart Van Asschecd05c152019-03-28 11:06:18 -07001095 /* fall through */
James Smart9a6b09c2012-03-01 22:37:42 -05001096 case SCSI_PROT_WRITE_INSERT:
1097 /*
1098 * For WRITE_INSERT, force the error
1099 * to be sent on the wire. It should be
1100 * detected by the Target.
1101 */
1102 /* DEADBEEF will be the reftag on the wire */
1103 *reftag = 0xDEADBEEF;
1104 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001105 if (phba->lpfc_injerr_wref_cnt == 0) {
1106 phba->lpfc_injerr_nportid = 0;
1107 phba->lpfc_injerr_lba =
1108 LPFC_INJERR_LBA_OFF;
1109 memset(&phba->lpfc_injerr_wwpn,
1110 0, sizeof(struct lpfc_name));
1111 }
James Smart9a6b09c2012-03-01 22:37:42 -05001112 rc = BG_ERR_TGT | BG_ERR_CHECK;
1113
1114 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1115 "9078 BLKGRD: Injecting reftag error: "
1116 "write lba x%lx\n", (unsigned long)lba);
1117 break;
James Smartacd68592012-01-18 16:25:09 -05001118 case SCSI_PROT_WRITE_STRIP:
1119 /*
1120 * For WRITE_STRIP and WRITE_PASS,
1121 * force the error on data
1122 * being copied from SLI-Host to SLI-Port.
1123 */
1124 *reftag = 0xDEADBEEF;
1125 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001126 if (phba->lpfc_injerr_wref_cnt == 0) {
1127 phba->lpfc_injerr_nportid = 0;
1128 phba->lpfc_injerr_lba =
1129 LPFC_INJERR_LBA_OFF;
1130 memset(&phba->lpfc_injerr_wwpn,
1131 0, sizeof(struct lpfc_name));
1132 }
James Smartacd68592012-01-18 16:25:09 -05001133 rc = BG_ERR_INIT;
1134
1135 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1136 "9077 BLKGRD: Injecting reftag error: "
1137 "write lba x%lx\n", (unsigned long)lba);
1138 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001139 }
James Smartacd68592012-01-18 16:25:09 -05001140 }
1141 if (phba->lpfc_injerr_rref_cnt) {
1142 switch (op) {
1143 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001144 case SCSI_PROT_READ_STRIP:
1145 case SCSI_PROT_READ_PASS:
1146 /*
1147 * For READ_STRIP and READ_PASS, force the
1148 * error on data being read off the wire. It
1149 * should force an IO error to the driver.
1150 */
James Smartf9bb2da2011-10-10 21:34:11 -04001151 *reftag = 0xDEADBEEF;
1152 phba->lpfc_injerr_rref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001153 if (phba->lpfc_injerr_rref_cnt == 0) {
1154 phba->lpfc_injerr_nportid = 0;
1155 phba->lpfc_injerr_lba =
1156 LPFC_INJERR_LBA_OFF;
1157 memset(&phba->lpfc_injerr_wwpn,
1158 0, sizeof(struct lpfc_name));
1159 }
James Smartacd68592012-01-18 16:25:09 -05001160 rc = BG_ERR_INIT;
James Smartf9bb2da2011-10-10 21:34:11 -04001161
1162 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smartacd68592012-01-18 16:25:09 -05001163 "9079 BLKGRD: Injecting reftag error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001164 "read lba x%lx\n", (unsigned long)lba);
James Smartacd68592012-01-18 16:25:09 -05001165 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001166 }
1167 }
1168 }
1169
1170 /* Should we change the Application Tag */
1171 if (apptag) {
James Smartacd68592012-01-18 16:25:09 -05001172 if (phba->lpfc_injerr_wapp_cnt) {
1173 switch (op) {
1174 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001175 if (src) {
James Smart9a6b09c2012-03-01 22:37:42 -05001176 /*
1177 * For WRITE_PASS, force the error
1178 * to be sent on the wire. It should
1179 * be detected by the Target.
1180 * If blockoff != 0 error will be
1181 * inserted in middle of the IO.
1182 */
1183
James Smartacd68592012-01-18 16:25:09 -05001184 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1185 "9080 BLKGRD: Injecting apptag error: "
1186 "write lba x%lx + x%x oldappTag x%x\n",
1187 (unsigned long)lba, blockoff,
James Smart9a6b09c2012-03-01 22:37:42 -05001188 be16_to_cpu(src->app_tag));
James Smartacd68592012-01-18 16:25:09 -05001189
1190 /*
James Smart9a6b09c2012-03-01 22:37:42 -05001191 * Save the old app_tag so we can
1192 * restore it on completion.
James Smartacd68592012-01-18 16:25:09 -05001193 */
James Smart9a6b09c2012-03-01 22:37:42 -05001194 if (lpfc_cmd) {
1195 lpfc_cmd->prot_data_type =
1196 LPFC_INJERR_APPTAG;
1197 lpfc_cmd->prot_data_segment =
1198 src;
1199 lpfc_cmd->prot_data =
1200 src->app_tag;
1201 }
1202 src->app_tag = cpu_to_be16(0xDEAD);
James Smartacd68592012-01-18 16:25:09 -05001203 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001204 if (phba->lpfc_injerr_wapp_cnt == 0) {
1205 phba->lpfc_injerr_nportid = 0;
1206 phba->lpfc_injerr_lba =
1207 LPFC_INJERR_LBA_OFF;
1208 memset(&phba->lpfc_injerr_wwpn,
1209 0, sizeof(struct lpfc_name));
1210 }
James Smart9a6b09c2012-03-01 22:37:42 -05001211 rc = BG_ERR_TGT | BG_ERR_CHECK;
James Smartacd68592012-01-18 16:25:09 -05001212 break;
1213 }
Bart Van Asschecd05c152019-03-28 11:06:18 -07001214 /* fall through */
James Smart9a6b09c2012-03-01 22:37:42 -05001215 case SCSI_PROT_WRITE_INSERT:
1216 /*
1217 * For WRITE_INSERT, force the
1218 * error to be sent on the wire. It should be
1219 * detected by the Target.
1220 */
1221 /* DEAD will be the apptag on the wire */
1222 *apptag = 0xDEAD;
1223 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001224 if (phba->lpfc_injerr_wapp_cnt == 0) {
1225 phba->lpfc_injerr_nportid = 0;
1226 phba->lpfc_injerr_lba =
1227 LPFC_INJERR_LBA_OFF;
1228 memset(&phba->lpfc_injerr_wwpn,
1229 0, sizeof(struct lpfc_name));
1230 }
James Smart9a6b09c2012-03-01 22:37:42 -05001231 rc = BG_ERR_TGT | BG_ERR_CHECK;
1232
1233 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1234 "0813 BLKGRD: Injecting apptag error: "
1235 "write lba x%lx\n", (unsigned long)lba);
1236 break;
James Smartacd68592012-01-18 16:25:09 -05001237 case SCSI_PROT_WRITE_STRIP:
1238 /*
1239 * For WRITE_STRIP and WRITE_PASS,
1240 * force the error on data
1241 * being copied from SLI-Host to SLI-Port.
1242 */
1243 *apptag = 0xDEAD;
1244 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001245 if (phba->lpfc_injerr_wapp_cnt == 0) {
1246 phba->lpfc_injerr_nportid = 0;
1247 phba->lpfc_injerr_lba =
1248 LPFC_INJERR_LBA_OFF;
1249 memset(&phba->lpfc_injerr_wwpn,
1250 0, sizeof(struct lpfc_name));
1251 }
James Smartacd68592012-01-18 16:25:09 -05001252 rc = BG_ERR_INIT;
1253
1254 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1255 "0812 BLKGRD: Injecting apptag error: "
1256 "write lba x%lx\n", (unsigned long)lba);
1257 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001258 }
James Smartacd68592012-01-18 16:25:09 -05001259 }
1260 if (phba->lpfc_injerr_rapp_cnt) {
1261 switch (op) {
1262 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001263 case SCSI_PROT_READ_STRIP:
1264 case SCSI_PROT_READ_PASS:
1265 /*
1266 * For READ_STRIP and READ_PASS, force the
1267 * error on data being read off the wire. It
1268 * should force an IO error to the driver.
1269 */
James Smartf9bb2da2011-10-10 21:34:11 -04001270 *apptag = 0xDEAD;
1271 phba->lpfc_injerr_rapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001272 if (phba->lpfc_injerr_rapp_cnt == 0) {
1273 phba->lpfc_injerr_nportid = 0;
1274 phba->lpfc_injerr_lba =
1275 LPFC_INJERR_LBA_OFF;
1276 memset(&phba->lpfc_injerr_wwpn,
1277 0, sizeof(struct lpfc_name));
1278 }
James Smartacd68592012-01-18 16:25:09 -05001279 rc = BG_ERR_INIT;
James Smartf9bb2da2011-10-10 21:34:11 -04001280
1281 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smartacd68592012-01-18 16:25:09 -05001282 "0814 BLKGRD: Injecting apptag error: "
1283 "read lba x%lx\n", (unsigned long)lba);
1284 break;
1285 }
1286 }
1287 }
1288
1289
1290 /* Should we change the Guard Tag */
1291 if (new_guard) {
1292 if (phba->lpfc_injerr_wgrd_cnt) {
1293 switch (op) {
1294 case SCSI_PROT_WRITE_PASS:
James Smart9a6b09c2012-03-01 22:37:42 -05001295 rc = BG_ERR_CHECK;
Bart Van Asschecd05c152019-03-28 11:06:18 -07001296 /* fall through */
James Smartacd68592012-01-18 16:25:09 -05001297
James Smartacd68592012-01-18 16:25:09 -05001298 case SCSI_PROT_WRITE_INSERT:
1299 /*
1300 * For WRITE_INSERT, force the
1301 * error to be sent on the wire. It should be
1302 * detected by the Target.
1303 */
1304 phba->lpfc_injerr_wgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001305 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1306 phba->lpfc_injerr_nportid = 0;
1307 phba->lpfc_injerr_lba =
1308 LPFC_INJERR_LBA_OFF;
1309 memset(&phba->lpfc_injerr_wwpn,
1310 0, sizeof(struct lpfc_name));
1311 }
James Smartacd68592012-01-18 16:25:09 -05001312
James Smart9a6b09c2012-03-01 22:37:42 -05001313 rc |= BG_ERR_TGT | BG_ERR_SWAP;
James Smartacd68592012-01-18 16:25:09 -05001314 /* Signals the caller to swap CRC->CSUM */
1315
1316 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1317 "0817 BLKGRD: Injecting guard error: "
1318 "write lba x%lx\n", (unsigned long)lba);
1319 break;
James Smart9a6b09c2012-03-01 22:37:42 -05001320 case SCSI_PROT_WRITE_STRIP:
1321 /*
1322 * For WRITE_STRIP and WRITE_PASS,
1323 * force the error on data
1324 * being copied from SLI-Host to SLI-Port.
1325 */
1326 phba->lpfc_injerr_wgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001327 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1328 phba->lpfc_injerr_nportid = 0;
1329 phba->lpfc_injerr_lba =
1330 LPFC_INJERR_LBA_OFF;
1331 memset(&phba->lpfc_injerr_wwpn,
1332 0, sizeof(struct lpfc_name));
1333 }
James Smart9a6b09c2012-03-01 22:37:42 -05001334
1335 rc = BG_ERR_INIT | BG_ERR_SWAP;
1336 /* Signals the caller to swap CRC->CSUM */
1337
1338 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1339 "0816 BLKGRD: Injecting guard error: "
1340 "write lba x%lx\n", (unsigned long)lba);
1341 break;
James Smartacd68592012-01-18 16:25:09 -05001342 }
1343 }
1344 if (phba->lpfc_injerr_rgrd_cnt) {
1345 switch (op) {
1346 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001347 case SCSI_PROT_READ_STRIP:
1348 case SCSI_PROT_READ_PASS:
1349 /*
1350 * For READ_STRIP and READ_PASS, force the
1351 * error on data being read off the wire. It
1352 * should force an IO error to the driver.
1353 */
James Smartacd68592012-01-18 16:25:09 -05001354 phba->lpfc_injerr_rgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001355 if (phba->lpfc_injerr_rgrd_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_INIT | BG_ERR_SWAP;
James Smartacd68592012-01-18 16:25:09 -05001364 /* Signals the caller to swap CRC->CSUM */
1365
1366 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1367 "0818 BLKGRD: Injecting guard error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001368 "read lba x%lx\n", (unsigned long)lba);
1369 }
1370 }
1371 }
1372
James Smartf9bb2da2011-10-10 21:34:11 -04001373 return rc;
1374}
1375#endif
1376
James Smartacd68592012-01-18 16:25:09 -05001377/**
1378 * lpfc_sc_to_bg_opcodes - Determine the BlockGuard opcodes to be used with
1379 * the specified SCSI command.
1380 * @phba: The Hba for which this call is being executed.
James Smart6c8eea52010-04-06 14:49:53 -04001381 * @sc: The SCSI command to examine
1382 * @txopt: (out) BlockGuard operation for transmitted data
1383 * @rxopt: (out) BlockGuard operation for received data
1384 *
1385 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1386 *
James Smartacd68592012-01-18 16:25:09 -05001387 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001388static int
James Smart6c8eea52010-04-06 14:49:53 -04001389lpfc_sc_to_bg_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1390 uint8_t *txop, uint8_t *rxop)
James Smarte2a0a9d2008-12-04 22:40:02 -05001391{
James Smart6c8eea52010-04-06 14:49:53 -04001392 uint8_t ret = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05001393
James Smart9c6aa9d2013-05-31 17:03:39 -04001394 if (lpfc_cmd_guard_csum(sc)) {
James Smarte2a0a9d2008-12-04 22:40:02 -05001395 switch (scsi_get_prot_op(sc)) {
1396 case SCSI_PROT_READ_INSERT:
1397 case SCSI_PROT_WRITE_STRIP:
James Smart6c8eea52010-04-06 14:49:53 -04001398 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001399 *txop = BG_OP_IN_CSUM_OUT_NODIF;
James Smarte2a0a9d2008-12-04 22:40:02 -05001400 break;
1401
1402 case SCSI_PROT_READ_STRIP:
1403 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001404 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001405 *txop = BG_OP_IN_NODIF_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001406 break;
1407
Martin K. Petersenc6af4042009-09-18 17:32:59 -04001408 case SCSI_PROT_READ_PASS:
1409 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001410 *rxop = BG_OP_IN_CRC_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001411 *txop = BG_OP_IN_CSUM_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001412 break;
1413
James Smarte2a0a9d2008-12-04 22:40:02 -05001414 case SCSI_PROT_NORMAL:
1415 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001416 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001417 "9063 BLKGRD: Bad op/guard:%d/IP combination\n",
1418 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001419 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001420 break;
1421
1422 }
James Smart7c56b9f2011-07-22 18:36:25 -04001423 } else {
James Smarte2a0a9d2008-12-04 22:40:02 -05001424 switch (scsi_get_prot_op(sc)) {
1425 case SCSI_PROT_READ_STRIP:
1426 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001427 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001428 *txop = BG_OP_IN_NODIF_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001429 break;
1430
1431 case SCSI_PROT_READ_PASS:
1432 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001433 *rxop = BG_OP_IN_CRC_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001434 *txop = BG_OP_IN_CRC_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001435 break;
1436
James Smarte2a0a9d2008-12-04 22:40:02 -05001437 case SCSI_PROT_READ_INSERT:
1438 case SCSI_PROT_WRITE_STRIP:
James Smart7c56b9f2011-07-22 18:36:25 -04001439 *rxop = BG_OP_IN_NODIF_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001440 *txop = BG_OP_IN_CRC_OUT_NODIF;
James Smart7c56b9f2011-07-22 18:36:25 -04001441 break;
1442
James Smarte2a0a9d2008-12-04 22:40:02 -05001443 case SCSI_PROT_NORMAL:
1444 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001445 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001446 "9075 BLKGRD: Bad op/guard:%d/CRC combination\n",
1447 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001448 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001449 break;
1450 }
James Smarte2a0a9d2008-12-04 22:40:02 -05001451 }
1452
James Smart6c8eea52010-04-06 14:49:53 -04001453 return ret;
James Smarte2a0a9d2008-12-04 22:40:02 -05001454}
1455
James Smartacd68592012-01-18 16:25:09 -05001456#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1457/**
1458 * lpfc_bg_err_opcodes - reDetermine the BlockGuard opcodes to be used with
1459 * the specified SCSI command in order to force a guard tag error.
1460 * @phba: The Hba for which this call is being executed.
1461 * @sc: The SCSI command to examine
1462 * @txopt: (out) BlockGuard operation for transmitted data
1463 * @rxopt: (out) BlockGuard operation for received data
1464 *
1465 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1466 *
1467 **/
1468static int
1469lpfc_bg_err_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1470 uint8_t *txop, uint8_t *rxop)
1471{
James Smartacd68592012-01-18 16:25:09 -05001472 uint8_t ret = 0;
1473
James Smart9c6aa9d2013-05-31 17:03:39 -04001474 if (lpfc_cmd_guard_csum(sc)) {
James Smartacd68592012-01-18 16:25:09 -05001475 switch (scsi_get_prot_op(sc)) {
1476 case SCSI_PROT_READ_INSERT:
1477 case SCSI_PROT_WRITE_STRIP:
James Smartacd68592012-01-18 16:25:09 -05001478 *rxop = BG_OP_IN_NODIF_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001479 *txop = BG_OP_IN_CRC_OUT_NODIF;
James Smartacd68592012-01-18 16:25:09 -05001480 break;
1481
1482 case SCSI_PROT_READ_STRIP:
1483 case SCSI_PROT_WRITE_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001484 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001485 *txop = BG_OP_IN_NODIF_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001486 break;
1487
1488 case SCSI_PROT_READ_PASS:
1489 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001490 *rxop = BG_OP_IN_CSUM_OUT_CRC;
James Smart9a6b09c2012-03-01 22:37:42 -05001491 *txop = BG_OP_IN_CRC_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001492 break;
1493
1494 case SCSI_PROT_NORMAL:
1495 default:
1496 break;
1497
1498 }
1499 } else {
1500 switch (scsi_get_prot_op(sc)) {
1501 case SCSI_PROT_READ_STRIP:
1502 case SCSI_PROT_WRITE_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001503 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001504 *txop = BG_OP_IN_NODIF_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001505 break;
1506
1507 case SCSI_PROT_READ_PASS:
1508 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001509 *rxop = BG_OP_IN_CSUM_OUT_CSUM;
James Smart9a6b09c2012-03-01 22:37:42 -05001510 *txop = BG_OP_IN_CSUM_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001511 break;
1512
1513 case SCSI_PROT_READ_INSERT:
1514 case SCSI_PROT_WRITE_STRIP:
James Smartacd68592012-01-18 16:25:09 -05001515 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001516 *txop = BG_OP_IN_CSUM_OUT_NODIF;
James Smartacd68592012-01-18 16:25:09 -05001517 break;
1518
1519 case SCSI_PROT_NORMAL:
1520 default:
1521 break;
1522 }
1523 }
1524
1525 return ret;
1526}
1527#endif
1528
1529/**
1530 * lpfc_bg_setup_bpl - Setup BlockGuard BPL with no protection data
1531 * @phba: The Hba for which this call is being executed.
1532 * @sc: pointer to scsi command we're working on
1533 * @bpl: pointer to buffer list for protection groups
1534 * @datacnt: number of segments of data that have been dma mapped
1535 *
1536 * This function sets up BPL buffer list for protection groups of
James Smarte2a0a9d2008-12-04 22:40:02 -05001537 * type LPFC_PG_TYPE_NO_DIF
1538 *
1539 * This is usually used when the HBA is instructed to generate
1540 * DIFs and insert them into data stream (or strip DIF from
1541 * incoming data stream)
1542 *
1543 * The buffer list consists of just one protection group described
1544 * below:
1545 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001546 * start of prot group --> | PDE_5 |
1547 * +-------------------------+
1548 * | PDE_6 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001549 * +-------------------------+
1550 * | Data BDE |
1551 * +-------------------------+
1552 * |more Data BDE's ... (opt)|
1553 * +-------------------------+
1554 *
James Smarte2a0a9d2008-12-04 22:40:02 -05001555 *
1556 * Note: Data s/g buffers have been dma mapped
James Smartacd68592012-01-18 16:25:09 -05001557 *
1558 * Returns the number of BDEs added to the BPL.
1559 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001560static int
1561lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1562 struct ulp_bde64 *bpl, int datasegcnt)
1563{
1564 struct scatterlist *sgde = NULL; /* s/g data entry */
James Smart6c8eea52010-04-06 14:49:53 -04001565 struct lpfc_pde5 *pde5 = NULL;
1566 struct lpfc_pde6 *pde6 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05001567 dma_addr_t physaddr;
James Smart6c8eea52010-04-06 14:49:53 -04001568 int i = 0, num_bde = 0, status;
James Smarte2a0a9d2008-12-04 22:40:02 -05001569 int datadir = sc->sc_data_direction;
James Smart0829a192012-05-09 21:18:12 -04001570#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001571 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04001572#endif
James Smartacd68592012-01-18 16:25:09 -05001573 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001574 uint32_t reftag;
James Smart6c8eea52010-04-06 14:49:53 -04001575 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05001576
James Smart6c8eea52010-04-06 14:49:53 -04001577 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1578 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05001579 goto out;
1580
James Smart6c8eea52010-04-06 14:49:53 -04001581 /* extract some info from the scsi command for pde*/
James Smartacd68592012-01-18 16:25:09 -05001582 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
James Smarte2a0a9d2008-12-04 22:40:02 -05001583
James Smartf9bb2da2011-10-10 21:34:11 -04001584#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05001585 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05001586 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05001587 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05001588 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05001589 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05001590 checking = 0;
1591 }
James Smartf9bb2da2011-10-10 21:34:11 -04001592#endif
1593
James Smart6c8eea52010-04-06 14:49:53 -04001594 /* setup PDE5 with what we have */
1595 pde5 = (struct lpfc_pde5 *) bpl;
1596 memset(pde5, 0, sizeof(struct lpfc_pde5));
1597 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05001598
James Smartbc739052010-08-04 16:11:18 -04001599 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04001600 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04001601 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04001602
James Smart6c8eea52010-04-06 14:49:53 -04001603 /* advance bpl and increment bde count */
1604 num_bde++;
1605 bpl++;
1606 pde6 = (struct lpfc_pde6 *) bpl;
1607
1608 /* setup PDE6 with the rest of the info */
1609 memset(pde6, 0, sizeof(struct lpfc_pde6));
1610 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
1611 bf_set(pde6_optx, pde6, txop);
1612 bf_set(pde6_oprx, pde6, rxop);
James Smarta6887e22013-04-17 20:18:07 -04001613
1614 /*
1615 * We only need to check the data on READs, for WRITEs
1616 * protection data is automatically generated, not checked.
1617 */
James Smart6c8eea52010-04-06 14:49:53 -04001618 if (datadir == DMA_FROM_DEVICE) {
James Smart9c6aa9d2013-05-31 17:03:39 -04001619 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
James Smarta6887e22013-04-17 20:18:07 -04001620 bf_set(pde6_ce, pde6, checking);
1621 else
1622 bf_set(pde6_ce, pde6, 0);
1623
James Smart9c6aa9d2013-05-31 17:03:39 -04001624 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04001625 bf_set(pde6_re, pde6, checking);
1626 else
1627 bf_set(pde6_re, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04001628 }
1629 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04001630 bf_set(pde6_ae, pde6, 0);
1631 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04001632
James Smartbc739052010-08-04 16:11:18 -04001633 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04001634 pde6->word0 = cpu_to_le32(pde6->word0);
1635 pde6->word1 = cpu_to_le32(pde6->word1);
1636 pde6->word2 = cpu_to_le32(pde6->word2);
1637
James Smart6c8eea52010-04-06 14:49:53 -04001638 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05001639 num_bde++;
1640 bpl++;
1641
1642 /* assumption: caller has already run dma_map_sg on command data */
1643 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
1644 physaddr = sg_dma_address(sgde);
1645 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
1646 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1647 bpl->tus.f.bdeSize = sg_dma_len(sgde);
1648 if (datadir == DMA_TO_DEVICE)
1649 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1650 else
1651 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1652 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1653 bpl++;
1654 num_bde++;
1655 }
1656
1657out:
1658 return num_bde;
1659}
1660
James Smartacd68592012-01-18 16:25:09 -05001661/**
1662 * lpfc_bg_setup_bpl_prot - Setup BlockGuard BPL with protection data
1663 * @phba: The Hba for which this call is being executed.
1664 * @sc: pointer to scsi command we're working on
1665 * @bpl: pointer to buffer list for protection groups
1666 * @datacnt: number of segments of data that have been dma mapped
1667 * @protcnt: number of segment of protection data that have been dma mapped
1668 *
1669 * This function sets up BPL buffer list for protection groups of
1670 * type LPFC_PG_TYPE_DIF
James Smarte2a0a9d2008-12-04 22:40:02 -05001671 *
1672 * This is usually used when DIFs are in their own buffers,
1673 * separate from the data. The HBA can then by instructed
1674 * to place the DIFs in the outgoing stream. For read operations,
1675 * The HBA could extract the DIFs and place it in DIF buffers.
1676 *
1677 * The buffer list for this type consists of one or more of the
1678 * protection groups described below:
1679 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001680 * start of first prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001681 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001682 * | PDE_6 |
1683 * +-------------------------+
1684 * | PDE_7 (Prot BDE) |
James Smarte2a0a9d2008-12-04 22:40:02 -05001685 * +-------------------------+
1686 * | Data BDE |
1687 * +-------------------------+
1688 * |more Data BDE's ... (opt)|
1689 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001690 * start of new prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001691 * +-------------------------+
1692 * | ... |
1693 * +-------------------------+
1694 *
James Smarte2a0a9d2008-12-04 22:40:02 -05001695 * Note: It is assumed that both data and protection s/g buffers have been
1696 * mapped for DMA
James Smartacd68592012-01-18 16:25:09 -05001697 *
1698 * Returns the number of BDEs added to the BPL.
1699 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001700static int
1701lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1702 struct ulp_bde64 *bpl, int datacnt, int protcnt)
1703{
1704 struct scatterlist *sgde = NULL; /* s/g data entry */
1705 struct scatterlist *sgpe = NULL; /* s/g prot entry */
James Smart6c8eea52010-04-06 14:49:53 -04001706 struct lpfc_pde5 *pde5 = NULL;
1707 struct lpfc_pde6 *pde6 = NULL;
James Smart7f860592011-03-11 16:05:52 -05001708 struct lpfc_pde7 *pde7 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05001709 dma_addr_t dataphysaddr, protphysaddr;
1710 unsigned short curr_data = 0, curr_prot = 0;
James Smart7f860592011-03-11 16:05:52 -05001711 unsigned int split_offset;
1712 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
James Smarte2a0a9d2008-12-04 22:40:02 -05001713 unsigned int protgrp_blks, protgrp_bytes;
1714 unsigned int remainder, subtotal;
James Smart6c8eea52010-04-06 14:49:53 -04001715 int status;
James Smarte2a0a9d2008-12-04 22:40:02 -05001716 int datadir = sc->sc_data_direction;
1717 unsigned char pgdone = 0, alldone = 0;
1718 unsigned blksize;
James Smart0829a192012-05-09 21:18:12 -04001719#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001720 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04001721#endif
James Smartacd68592012-01-18 16:25:09 -05001722 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001723 uint32_t reftag;
James Smart6c8eea52010-04-06 14:49:53 -04001724 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05001725 int num_bde = 0;
1726
1727 sgpe = scsi_prot_sglist(sc);
1728 sgde = scsi_sglist(sc);
1729
1730 if (!sgpe || !sgde) {
1731 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart32350662019-08-14 16:57:06 -07001732 "9020 Invalid s/g entry: data=x%px prot=x%px\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001733 sgpe, sgde);
1734 return 0;
1735 }
1736
James Smart6c8eea52010-04-06 14:49:53 -04001737 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1738 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05001739 goto out;
1740
James Smart6c8eea52010-04-06 14:49:53 -04001741 /* extract some info from the scsi command */
James Smarte2a0a9d2008-12-04 22:40:02 -05001742 blksize = lpfc_cmd_blksize(sc);
James Smartacd68592012-01-18 16:25:09 -05001743 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
James Smarte2a0a9d2008-12-04 22:40:02 -05001744
James Smartf9bb2da2011-10-10 21:34:11 -04001745#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05001746 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05001747 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05001748 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05001749 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05001750 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05001751 checking = 0;
1752 }
James Smartf9bb2da2011-10-10 21:34:11 -04001753#endif
1754
James Smarte2a0a9d2008-12-04 22:40:02 -05001755 split_offset = 0;
1756 do {
James Smart96f70772013-04-17 20:16:15 -04001757 /* Check to see if we ran out of space */
1758 if (num_bde >= (phba->cfg_total_seg_cnt - 2))
1759 return num_bde + 3;
1760
James Smart6c8eea52010-04-06 14:49:53 -04001761 /* setup PDE5 with what we have */
1762 pde5 = (struct lpfc_pde5 *) bpl;
1763 memset(pde5, 0, sizeof(struct lpfc_pde5));
1764 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05001765
James Smartbc739052010-08-04 16:11:18 -04001766 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04001767 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04001768 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04001769
James Smart6c8eea52010-04-06 14:49:53 -04001770 /* advance bpl and increment bde count */
1771 num_bde++;
1772 bpl++;
1773 pde6 = (struct lpfc_pde6 *) bpl;
James Smarte2a0a9d2008-12-04 22:40:02 -05001774
James Smart6c8eea52010-04-06 14:49:53 -04001775 /* setup PDE6 with the rest of the info */
1776 memset(pde6, 0, sizeof(struct lpfc_pde6));
1777 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
1778 bf_set(pde6_optx, pde6, txop);
1779 bf_set(pde6_oprx, pde6, rxop);
James Smarta6887e22013-04-17 20:18:07 -04001780
James Smart9c6aa9d2013-05-31 17:03:39 -04001781 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
James Smarta6887e22013-04-17 20:18:07 -04001782 bf_set(pde6_ce, pde6, checking);
1783 else
1784 bf_set(pde6_ce, pde6, 0);
1785
James Smart9c6aa9d2013-05-31 17:03:39 -04001786 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04001787 bf_set(pde6_re, pde6, checking);
1788 else
1789 bf_set(pde6_re, pde6, 0);
1790
James Smart6c8eea52010-04-06 14:49:53 -04001791 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04001792 bf_set(pde6_ae, pde6, 0);
1793 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04001794
James Smartbc739052010-08-04 16:11:18 -04001795 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04001796 pde6->word0 = cpu_to_le32(pde6->word0);
1797 pde6->word1 = cpu_to_le32(pde6->word1);
1798 pde6->word2 = cpu_to_le32(pde6->word2);
1799
James Smart6c8eea52010-04-06 14:49:53 -04001800 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05001801 num_bde++;
1802 bpl++;
1803
1804 /* setup the first BDE that points to protection buffer */
James Smart7f860592011-03-11 16:05:52 -05001805 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
1806 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
James Smarte2a0a9d2008-12-04 22:40:02 -05001807
James Smarte2a0a9d2008-12-04 22:40:02 -05001808 /* must be integer multiple of the DIF block length */
1809 BUG_ON(protgroup_len % 8);
1810
James Smart7f860592011-03-11 16:05:52 -05001811 pde7 = (struct lpfc_pde7 *) bpl;
1812 memset(pde7, 0, sizeof(struct lpfc_pde7));
1813 bf_set(pde7_type, pde7, LPFC_PDE7_DESCRIPTOR);
1814
James Smart7c56b9f2011-07-22 18:36:25 -04001815 pde7->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
1816 pde7->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
James Smart7f860592011-03-11 16:05:52 -05001817
James Smarte2a0a9d2008-12-04 22:40:02 -05001818 protgrp_blks = protgroup_len / 8;
1819 protgrp_bytes = protgrp_blks * blksize;
1820
James Smart7f860592011-03-11 16:05:52 -05001821 /* check if this pde is crossing the 4K boundary; if so split */
1822 if ((pde7->addrLow & 0xfff) + protgroup_len > 0x1000) {
1823 protgroup_remainder = 0x1000 - (pde7->addrLow & 0xfff);
1824 protgroup_offset += protgroup_remainder;
1825 protgrp_blks = protgroup_remainder / 8;
James Smart7c56b9f2011-07-22 18:36:25 -04001826 protgrp_bytes = protgrp_blks * blksize;
James Smart7f860592011-03-11 16:05:52 -05001827 } else {
1828 protgroup_offset = 0;
1829 curr_prot++;
1830 }
James Smarte2a0a9d2008-12-04 22:40:02 -05001831
James Smarte2a0a9d2008-12-04 22:40:02 -05001832 num_bde++;
1833
1834 /* setup BDE's for data blocks associated with DIF data */
1835 pgdone = 0;
1836 subtotal = 0; /* total bytes processed for current prot grp */
1837 while (!pgdone) {
James Smart96f70772013-04-17 20:16:15 -04001838 /* Check to see if we ran out of space */
1839 if (num_bde >= phba->cfg_total_seg_cnt)
1840 return num_bde + 1;
1841
James Smarte2a0a9d2008-12-04 22:40:02 -05001842 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -04001843 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1844 "9065 BLKGRD:%s Invalid data segment\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001845 __func__);
1846 return 0;
1847 }
1848 bpl++;
1849 dataphysaddr = sg_dma_address(sgde) + split_offset;
1850 bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
1851 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
1852
1853 remainder = sg_dma_len(sgde) - split_offset;
1854
1855 if ((subtotal + remainder) <= protgrp_bytes) {
1856 /* we can use this whole buffer */
1857 bpl->tus.f.bdeSize = remainder;
1858 split_offset = 0;
1859
1860 if ((subtotal + remainder) == protgrp_bytes)
1861 pgdone = 1;
1862 } else {
1863 /* must split this buffer with next prot grp */
1864 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
1865 split_offset += bpl->tus.f.bdeSize;
1866 }
1867
1868 subtotal += bpl->tus.f.bdeSize;
1869
1870 if (datadir == DMA_TO_DEVICE)
1871 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1872 else
1873 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1874 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1875
1876 num_bde++;
1877 curr_data++;
1878
1879 if (split_offset)
1880 break;
1881
1882 /* Move to the next s/g segment if possible */
1883 sgde = sg_next(sgde);
James Smart6c8eea52010-04-06 14:49:53 -04001884
James Smarte2a0a9d2008-12-04 22:40:02 -05001885 }
1886
James Smart7f860592011-03-11 16:05:52 -05001887 if (protgroup_offset) {
1888 /* update the reference tag */
1889 reftag += protgrp_blks;
1890 bpl++;
1891 continue;
1892 }
1893
James Smarte2a0a9d2008-12-04 22:40:02 -05001894 /* are we done ? */
1895 if (curr_prot == protcnt) {
1896 alldone = 1;
1897 } else if (curr_prot < protcnt) {
1898 /* advance to next prot buffer */
1899 sgpe = sg_next(sgpe);
1900 bpl++;
1901
1902 /* update the reference tag */
1903 reftag += protgrp_blks;
1904 } else {
1905 /* if we're here, we have a bug */
James Smart6a9c52c2009-10-02 15:16:51 -04001906 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1907 "9054 BLKGRD: bug in %s\n", __func__);
James Smarte2a0a9d2008-12-04 22:40:02 -05001908 }
1909
1910 } while (!alldone);
James Smarte2a0a9d2008-12-04 22:40:02 -05001911out:
1912
James Smarte2a0a9d2008-12-04 22:40:02 -05001913 return num_bde;
1914}
James Smart7f860592011-03-11 16:05:52 -05001915
James Smartacd68592012-01-18 16:25:09 -05001916/**
1917 * lpfc_bg_setup_sgl - Setup BlockGuard SGL with no protection data
1918 * @phba: The Hba for which this call is being executed.
1919 * @sc: pointer to scsi command we're working on
1920 * @sgl: pointer to buffer list for protection groups
1921 * @datacnt: number of segments of data that have been dma mapped
1922 *
1923 * This function sets up SGL buffer list for protection groups of
1924 * type LPFC_PG_TYPE_NO_DIF
1925 *
1926 * This is usually used when the HBA is instructed to generate
1927 * DIFs and insert them into data stream (or strip DIF from
1928 * incoming data stream)
1929 *
1930 * The buffer list consists of just one protection group described
1931 * below:
1932 * +-------------------------+
1933 * start of prot group --> | DI_SEED |
1934 * +-------------------------+
1935 * | Data SGE |
1936 * +-------------------------+
1937 * |more Data SGE's ... (opt)|
1938 * +-------------------------+
1939 *
1940 *
1941 * Note: Data s/g buffers have been dma mapped
1942 *
1943 * Returns the number of SGEs added to the SGL.
1944 **/
1945static int
1946lpfc_bg_setup_sgl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
James Smartd79c9e92019-08-14 16:57:09 -07001947 struct sli4_sge *sgl, int datasegcnt,
1948 struct lpfc_io_buf *lpfc_cmd)
James Smartacd68592012-01-18 16:25:09 -05001949{
1950 struct scatterlist *sgde = NULL; /* s/g data entry */
1951 struct sli4_sge_diseed *diseed = NULL;
1952 dma_addr_t physaddr;
1953 int i = 0, num_sge = 0, status;
James Smartacd68592012-01-18 16:25:09 -05001954 uint32_t reftag;
James Smartacd68592012-01-18 16:25:09 -05001955 uint8_t txop, rxop;
James Smart0829a192012-05-09 21:18:12 -04001956#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001957 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04001958#endif
James Smartacd68592012-01-18 16:25:09 -05001959 uint32_t checking = 1;
1960 uint32_t dma_len;
1961 uint32_t dma_offset = 0;
James Smartd79c9e92019-08-14 16:57:09 -07001962 struct sli4_hybrid_sgl *sgl_xtra = NULL;
1963 int j;
1964 bool lsp_just_set = false;
James Smartacd68592012-01-18 16:25:09 -05001965
1966 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1967 if (status)
1968 goto out;
1969
1970 /* extract some info from the scsi command for pde*/
James Smartacd68592012-01-18 16:25:09 -05001971 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
1972
1973#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05001974 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05001975 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05001976 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05001977 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05001978 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05001979 checking = 0;
1980 }
1981#endif
1982
1983 /* setup DISEED with what we have */
1984 diseed = (struct sli4_sge_diseed *) sgl;
1985 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
1986 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
1987
1988 /* Endianness conversion if necessary */
1989 diseed->ref_tag = cpu_to_le32(reftag);
1990 diseed->ref_tag_tran = diseed->ref_tag;
1991
James Smarta6887e22013-04-17 20:18:07 -04001992 /*
1993 * We only need to check the data on READs, for WRITEs
1994 * protection data is automatically generated, not checked.
1995 */
1996 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
James Smart9c6aa9d2013-05-31 17:03:39 -04001997 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
James Smarta6887e22013-04-17 20:18:07 -04001998 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
1999 else
2000 bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2001
James Smart9c6aa9d2013-05-31 17:03:39 -04002002 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04002003 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2004 else
2005 bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2006 }
2007
James Smartacd68592012-01-18 16:25:09 -05002008 /* setup DISEED with the rest of the info */
2009 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2010 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002011
James Smartacd68592012-01-18 16:25:09 -05002012 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2013 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2014
2015 /* Endianness conversion if necessary for DISEED */
2016 diseed->word2 = cpu_to_le32(diseed->word2);
2017 diseed->word3 = cpu_to_le32(diseed->word3);
2018
2019 /* advance bpl and increment sge count */
2020 num_sge++;
2021 sgl++;
2022
2023 /* assumption: caller has already run dma_map_sg on command data */
James Smartd79c9e92019-08-14 16:57:09 -07002024 sgde = scsi_sglist(sc);
2025 j = 3;
2026 for (i = 0; i < datasegcnt; i++) {
2027 /* clear it */
2028 sgl->word2 = 0;
James Smartacd68592012-01-18 16:25:09 -05002029
James Smartd79c9e92019-08-14 16:57:09 -07002030 /* do we need to expand the segment */
2031 if (!lsp_just_set && !((j + 1) % phba->border_sge_num) &&
2032 ((datasegcnt - 1) != i)) {
2033 /* set LSP type */
2034 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_LSP);
James Smartacd68592012-01-18 16:25:09 -05002035
James Smartd79c9e92019-08-14 16:57:09 -07002036 sgl_xtra = lpfc_get_sgl_per_hdwq(phba, lpfc_cmd);
2037
2038 if (unlikely(!sgl_xtra)) {
2039 lpfc_cmd->seg_cnt = 0;
2040 return 0;
2041 }
2042 sgl->addr_lo = cpu_to_le32(putPaddrLow(
2043 sgl_xtra->dma_phys_sgl));
2044 sgl->addr_hi = cpu_to_le32(putPaddrHigh(
2045 sgl_xtra->dma_phys_sgl));
2046
2047 } else {
2048 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2049 }
2050
2051 if (!(bf_get(lpfc_sli4_sge_type, sgl) & LPFC_SGE_TYPE_LSP)) {
2052 if ((datasegcnt - 1) == i)
2053 bf_set(lpfc_sli4_sge_last, sgl, 1);
2054 physaddr = sg_dma_address(sgde);
2055 dma_len = sg_dma_len(sgde);
2056 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
2057 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
2058
2059 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2060 sgl->word2 = cpu_to_le32(sgl->word2);
2061 sgl->sge_len = cpu_to_le32(dma_len);
2062
2063 dma_offset += dma_len;
2064 sgde = sg_next(sgde);
2065
2066 sgl++;
2067 num_sge++;
2068 lsp_just_set = false;
2069
2070 } else {
2071 sgl->word2 = cpu_to_le32(sgl->word2);
2072 sgl->sge_len = cpu_to_le32(phba->cfg_sg_dma_buf_size);
2073
2074 sgl = (struct sli4_sge *)sgl_xtra->dma_sgl;
2075 i = i - 1;
2076
2077 lsp_just_set = true;
2078 }
2079
2080 j++;
2081
James Smartacd68592012-01-18 16:25:09 -05002082 }
2083
2084out:
2085 return num_sge;
2086}
2087
2088/**
2089 * lpfc_bg_setup_sgl_prot - Setup BlockGuard SGL with protection data
2090 * @phba: The Hba for which this call is being executed.
2091 * @sc: pointer to scsi command we're working on
2092 * @sgl: pointer to buffer list for protection groups
2093 * @datacnt: number of segments of data that have been dma mapped
2094 * @protcnt: number of segment of protection data that have been dma mapped
2095 *
2096 * This function sets up SGL buffer list for protection groups of
2097 * type LPFC_PG_TYPE_DIF
2098 *
2099 * This is usually used when DIFs are in their own buffers,
2100 * separate from the data. The HBA can then by instructed
2101 * to place the DIFs in the outgoing stream. For read operations,
2102 * The HBA could extract the DIFs and place it in DIF buffers.
2103 *
2104 * The buffer list for this type consists of one or more of the
2105 * protection groups described below:
2106 * +-------------------------+
2107 * start of first prot group --> | DISEED |
2108 * +-------------------------+
2109 * | DIF (Prot SGE) |
2110 * +-------------------------+
2111 * | Data SGE |
2112 * +-------------------------+
2113 * |more Data SGE's ... (opt)|
2114 * +-------------------------+
2115 * start of new prot group --> | DISEED |
2116 * +-------------------------+
2117 * | ... |
2118 * +-------------------------+
2119 *
2120 * Note: It is assumed that both data and protection s/g buffers have been
2121 * mapped for DMA
2122 *
2123 * Returns the number of SGEs added to the SGL.
2124 **/
2125static int
2126lpfc_bg_setup_sgl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
James Smartd79c9e92019-08-14 16:57:09 -07002127 struct sli4_sge *sgl, int datacnt, int protcnt,
2128 struct lpfc_io_buf *lpfc_cmd)
James Smartacd68592012-01-18 16:25:09 -05002129{
2130 struct scatterlist *sgde = NULL; /* s/g data entry */
2131 struct scatterlist *sgpe = NULL; /* s/g prot entry */
2132 struct sli4_sge_diseed *diseed = NULL;
2133 dma_addr_t dataphysaddr, protphysaddr;
2134 unsigned short curr_data = 0, curr_prot = 0;
2135 unsigned int split_offset;
2136 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
2137 unsigned int protgrp_blks, protgrp_bytes;
2138 unsigned int remainder, subtotal;
2139 int status;
2140 unsigned char pgdone = 0, alldone = 0;
2141 unsigned blksize;
2142 uint32_t reftag;
2143 uint8_t txop, rxop;
2144 uint32_t dma_len;
James Smart0829a192012-05-09 21:18:12 -04002145#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002146 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002147#endif
James Smartacd68592012-01-18 16:25:09 -05002148 uint32_t checking = 1;
2149 uint32_t dma_offset = 0;
James Smartd79c9e92019-08-14 16:57:09 -07002150 int num_sge = 0, j = 2;
2151 struct sli4_hybrid_sgl *sgl_xtra = NULL;
James Smartacd68592012-01-18 16:25:09 -05002152
2153 sgpe = scsi_prot_sglist(sc);
2154 sgde = scsi_sglist(sc);
2155
2156 if (!sgpe || !sgde) {
2157 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart32350662019-08-14 16:57:06 -07002158 "9082 Invalid s/g entry: data=x%px prot=x%px\n",
James Smartacd68592012-01-18 16:25:09 -05002159 sgpe, sgde);
2160 return 0;
2161 }
2162
2163 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2164 if (status)
2165 goto out;
2166
2167 /* extract some info from the scsi command */
2168 blksize = lpfc_cmd_blksize(sc);
2169 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2170
2171#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002172 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002173 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002174 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002175 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002176 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002177 checking = 0;
2178 }
2179#endif
2180
2181 split_offset = 0;
2182 do {
James Smart96f70772013-04-17 20:16:15 -04002183 /* Check to see if we ran out of space */
James Smartd79c9e92019-08-14 16:57:09 -07002184 if ((num_sge >= (phba->cfg_total_seg_cnt - 2)) &&
2185 !(phba->cfg_xpsgl))
James Smart96f70772013-04-17 20:16:15 -04002186 return num_sge + 3;
2187
James Smartd79c9e92019-08-14 16:57:09 -07002188 /* DISEED and DIF have to be together */
2189 if (!((j + 1) % phba->border_sge_num) ||
2190 !((j + 2) % phba->border_sge_num) ||
2191 !((j + 3) % phba->border_sge_num)) {
2192 sgl->word2 = 0;
2193
2194 /* set LSP type */
2195 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_LSP);
2196
2197 sgl_xtra = lpfc_get_sgl_per_hdwq(phba, lpfc_cmd);
2198
2199 if (unlikely(!sgl_xtra)) {
2200 goto out;
2201 } else {
2202 sgl->addr_lo = cpu_to_le32(putPaddrLow(
2203 sgl_xtra->dma_phys_sgl));
2204 sgl->addr_hi = cpu_to_le32(putPaddrHigh(
2205 sgl_xtra->dma_phys_sgl));
2206 }
2207
2208 sgl->word2 = cpu_to_le32(sgl->word2);
2209 sgl->sge_len = cpu_to_le32(phba->cfg_sg_dma_buf_size);
2210
2211 sgl = (struct sli4_sge *)sgl_xtra->dma_sgl;
2212 j = 0;
2213 }
2214
James Smartacd68592012-01-18 16:25:09 -05002215 /* setup DISEED with what we have */
2216 diseed = (struct sli4_sge_diseed *) sgl;
2217 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2218 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2219
2220 /* Endianness conversion if necessary */
2221 diseed->ref_tag = cpu_to_le32(reftag);
2222 diseed->ref_tag_tran = diseed->ref_tag;
2223
James Smart9c6aa9d2013-05-31 17:03:39 -04002224 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD)) {
James Smarta6887e22013-04-17 20:18:07 -04002225 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2226
2227 } else {
2228 bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2229 /*
2230 * When in this mode, the hardware will replace
2231 * the guard tag from the host with a
2232 * newly generated good CRC for the wire.
2233 * Switch to raw mode here to avoid this
2234 * behavior. What the host sends gets put on the wire.
2235 */
2236 if (txop == BG_OP_IN_CRC_OUT_CRC) {
2237 txop = BG_OP_RAW_MODE;
2238 rxop = BG_OP_RAW_MODE;
2239 }
2240 }
2241
2242
James Smart9c6aa9d2013-05-31 17:03:39 -04002243 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04002244 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2245 else
2246 bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2247
James Smartacd68592012-01-18 16:25:09 -05002248 /* setup DISEED with the rest of the info */
2249 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2250 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002251
James Smartacd68592012-01-18 16:25:09 -05002252 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2253 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2254
2255 /* Endianness conversion if necessary for DISEED */
2256 diseed->word2 = cpu_to_le32(diseed->word2);
2257 diseed->word3 = cpu_to_le32(diseed->word3);
2258
2259 /* advance sgl and increment bde count */
2260 num_sge++;
James Smartd79c9e92019-08-14 16:57:09 -07002261
James Smartacd68592012-01-18 16:25:09 -05002262 sgl++;
James Smartd79c9e92019-08-14 16:57:09 -07002263 j++;
James Smartacd68592012-01-18 16:25:09 -05002264
2265 /* setup the first BDE that points to protection buffer */
2266 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2267 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
2268
2269 /* must be integer multiple of the DIF block length */
2270 BUG_ON(protgroup_len % 8);
2271
2272 /* Now setup DIF SGE */
2273 sgl->word2 = 0;
2274 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DIF);
2275 sgl->addr_hi = le32_to_cpu(putPaddrHigh(protphysaddr));
2276 sgl->addr_lo = le32_to_cpu(putPaddrLow(protphysaddr));
2277 sgl->word2 = cpu_to_le32(sgl->word2);
James Smartd79c9e92019-08-14 16:57:09 -07002278 sgl->sge_len = 0;
James Smartacd68592012-01-18 16:25:09 -05002279
2280 protgrp_blks = protgroup_len / 8;
2281 protgrp_bytes = protgrp_blks * blksize;
2282
2283 /* check if DIF SGE is crossing the 4K boundary; if so split */
2284 if ((sgl->addr_lo & 0xfff) + protgroup_len > 0x1000) {
2285 protgroup_remainder = 0x1000 - (sgl->addr_lo & 0xfff);
2286 protgroup_offset += protgroup_remainder;
2287 protgrp_blks = protgroup_remainder / 8;
2288 protgrp_bytes = protgrp_blks * blksize;
2289 } else {
2290 protgroup_offset = 0;
2291 curr_prot++;
2292 }
2293
2294 num_sge++;
2295
2296 /* setup SGE's for data blocks associated with DIF data */
2297 pgdone = 0;
2298 subtotal = 0; /* total bytes processed for current prot grp */
James Smartd79c9e92019-08-14 16:57:09 -07002299
2300 sgl++;
2301 j++;
2302
James Smartacd68592012-01-18 16:25:09 -05002303 while (!pgdone) {
James Smart96f70772013-04-17 20:16:15 -04002304 /* Check to see if we ran out of space */
James Smartd79c9e92019-08-14 16:57:09 -07002305 if ((num_sge >= phba->cfg_total_seg_cnt) &&
2306 !phba->cfg_xpsgl)
James Smart96f70772013-04-17 20:16:15 -04002307 return num_sge + 1;
2308
James Smartacd68592012-01-18 16:25:09 -05002309 if (!sgde) {
2310 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2311 "9086 BLKGRD:%s Invalid data segment\n",
2312 __func__);
2313 return 0;
2314 }
James Smartacd68592012-01-18 16:25:09 -05002315
James Smartd79c9e92019-08-14 16:57:09 -07002316 if (!((j + 1) % phba->border_sge_num)) {
2317 sgl->word2 = 0;
James Smartacd68592012-01-18 16:25:09 -05002318
James Smartd79c9e92019-08-14 16:57:09 -07002319 /* set LSP type */
2320 bf_set(lpfc_sli4_sge_type, sgl,
2321 LPFC_SGE_TYPE_LSP);
James Smartacd68592012-01-18 16:25:09 -05002322
James Smartd79c9e92019-08-14 16:57:09 -07002323 sgl_xtra = lpfc_get_sgl_per_hdwq(phba,
2324 lpfc_cmd);
2325
2326 if (unlikely(!sgl_xtra)) {
2327 goto out;
2328 } else {
2329 sgl->addr_lo = cpu_to_le32(
2330 putPaddrLow(sgl_xtra->dma_phys_sgl));
2331 sgl->addr_hi = cpu_to_le32(
2332 putPaddrHigh(sgl_xtra->dma_phys_sgl));
2333 }
2334
2335 sgl->word2 = cpu_to_le32(sgl->word2);
2336 sgl->sge_len = cpu_to_le32(
2337 phba->cfg_sg_dma_buf_size);
2338
2339 sgl = (struct sli4_sge *)sgl_xtra->dma_sgl;
James Smartacd68592012-01-18 16:25:09 -05002340 } else {
James Smartd79c9e92019-08-14 16:57:09 -07002341 dataphysaddr = sg_dma_address(sgde) +
2342 split_offset;
2343
2344 remainder = sg_dma_len(sgde) - split_offset;
2345
2346 if ((subtotal + remainder) <= protgrp_bytes) {
2347 /* we can use this whole buffer */
2348 dma_len = remainder;
2349 split_offset = 0;
2350
2351 if ((subtotal + remainder) ==
2352 protgrp_bytes)
2353 pgdone = 1;
2354 } else {
2355 /* must split this buffer with next
2356 * prot grp
2357 */
2358 dma_len = protgrp_bytes - subtotal;
2359 split_offset += dma_len;
2360 }
2361
2362 subtotal += dma_len;
2363
2364 sgl->word2 = 0;
2365 sgl->addr_lo = cpu_to_le32(putPaddrLow(
2366 dataphysaddr));
2367 sgl->addr_hi = cpu_to_le32(putPaddrHigh(
2368 dataphysaddr));
2369 bf_set(lpfc_sli4_sge_last, sgl, 0);
2370 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2371 bf_set(lpfc_sli4_sge_type, sgl,
2372 LPFC_SGE_TYPE_DATA);
2373
2374 sgl->sge_len = cpu_to_le32(dma_len);
2375 dma_offset += dma_len;
2376
2377 num_sge++;
2378 curr_data++;
2379
2380 if (split_offset) {
2381 sgl++;
2382 j++;
2383 break;
2384 }
2385
2386 /* Move to the next s/g segment if possible */
2387 sgde = sg_next(sgde);
2388
2389 sgl++;
James Smartacd68592012-01-18 16:25:09 -05002390 }
2391
James Smartd79c9e92019-08-14 16:57:09 -07002392 j++;
James Smartacd68592012-01-18 16:25:09 -05002393 }
2394
2395 if (protgroup_offset) {
2396 /* update the reference tag */
2397 reftag += protgrp_blks;
James Smartacd68592012-01-18 16:25:09 -05002398 continue;
2399 }
2400
2401 /* are we done ? */
2402 if (curr_prot == protcnt) {
James Smartd79c9e92019-08-14 16:57:09 -07002403 /* mark the last SGL */
2404 sgl--;
James Smartacd68592012-01-18 16:25:09 -05002405 bf_set(lpfc_sli4_sge_last, sgl, 1);
2406 alldone = 1;
2407 } else if (curr_prot < protcnt) {
2408 /* advance to next prot buffer */
2409 sgpe = sg_next(sgpe);
James Smartacd68592012-01-18 16:25:09 -05002410
2411 /* update the reference tag */
2412 reftag += protgrp_blks;
2413 } else {
2414 /* if we're here, we have a bug */
2415 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2416 "9085 BLKGRD: bug in %s\n", __func__);
2417 }
2418
2419 } while (!alldone);
2420
2421out:
2422
2423 return num_sge;
2424}
2425
2426/**
2427 * lpfc_prot_group_type - Get prtotection group type of SCSI command
2428 * @phba: The Hba for which this call is being executed.
2429 * @sc: pointer to scsi command we're working on
2430 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002431 * Given a SCSI command that supports DIF, determine composition of protection
2432 * groups involved in setting up buffer lists
2433 *
James Smartacd68592012-01-18 16:25:09 -05002434 * Returns: Protection group type (with or without DIF)
2435 *
2436 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002437static int
2438lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
2439{
2440 int ret = LPFC_PG_TYPE_INVALID;
2441 unsigned char op = scsi_get_prot_op(sc);
2442
2443 switch (op) {
2444 case SCSI_PROT_READ_STRIP:
2445 case SCSI_PROT_WRITE_INSERT:
2446 ret = LPFC_PG_TYPE_NO_DIF;
2447 break;
2448 case SCSI_PROT_READ_INSERT:
2449 case SCSI_PROT_WRITE_STRIP:
2450 case SCSI_PROT_READ_PASS:
2451 case SCSI_PROT_WRITE_PASS:
James Smarte2a0a9d2008-12-04 22:40:02 -05002452 ret = LPFC_PG_TYPE_DIF_BUF;
2453 break;
2454 default:
James Smart9c6aa9d2013-05-31 17:03:39 -04002455 if (phba)
2456 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2457 "9021 Unsupported protection op:%d\n",
2458 op);
James Smarte2a0a9d2008-12-04 22:40:02 -05002459 break;
2460 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002461 return ret;
2462}
2463
James Smartacd68592012-01-18 16:25:09 -05002464/**
James Smarta6887e22013-04-17 20:18:07 -04002465 * lpfc_bg_scsi_adjust_dl - Adjust SCSI data length for BlockGuard
2466 * @phba: The Hba for which this call is being executed.
2467 * @lpfc_cmd: The scsi buffer which is going to be adjusted.
2468 *
2469 * Adjust the data length to account for how much data
2470 * is actually on the wire.
2471 *
2472 * returns the adjusted data length
2473 **/
2474static int
2475lpfc_bg_scsi_adjust_dl(struct lpfc_hba *phba,
James Smartc4908502019-01-28 11:14:28 -08002476 struct lpfc_io_buf *lpfc_cmd)
James Smarta6887e22013-04-17 20:18:07 -04002477{
2478 struct scsi_cmnd *sc = lpfc_cmd->pCmd;
2479 int fcpdl;
2480
2481 fcpdl = scsi_bufflen(sc);
2482
2483 /* Check if there is protection data on the wire */
2484 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
James Smart9c6aa9d2013-05-31 17:03:39 -04002485 /* Read check for protection data */
James Smarta6887e22013-04-17 20:18:07 -04002486 if (scsi_get_prot_op(sc) == SCSI_PROT_READ_INSERT)
2487 return fcpdl;
2488
2489 } else {
James Smart9c6aa9d2013-05-31 17:03:39 -04002490 /* Write check for protection data */
James Smarta6887e22013-04-17 20:18:07 -04002491 if (scsi_get_prot_op(sc) == SCSI_PROT_WRITE_STRIP)
2492 return fcpdl;
2493 }
2494
2495 /*
2496 * If we are in DIF Type 1 mode every data block has a 8 byte
James Smart9c6aa9d2013-05-31 17:03:39 -04002497 * DIF (trailer) attached to it. Must ajust FCP data length
2498 * to account for the protection data.
James Smarta6887e22013-04-17 20:18:07 -04002499 */
James Smart9c6aa9d2013-05-31 17:03:39 -04002500 fcpdl += (fcpdl / lpfc_cmd_blksize(sc)) * 8;
James Smarta6887e22013-04-17 20:18:07 -04002501
2502 return fcpdl;
2503}
2504
2505/**
James Smartacd68592012-01-18 16:25:09 -05002506 * lpfc_bg_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
2507 * @phba: The Hba for which this call is being executed.
2508 * @lpfc_cmd: The scsi buffer which is going to be prep'ed.
2509 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002510 * This is the protection/DIF aware version of
2511 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
James Smart5e0e2312019-08-14 16:56:54 -07002512 * two functions eventually, but for now, it's here.
2513 * RETURNS 0 - SUCCESS,
2514 * 1 - Failed DMA map, retry.
2515 * 2 - Invalid scsi cmd or prot-type. Do not rety.
James Smartacd68592012-01-18 16:25:09 -05002516 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002517static int
James Smartacd68592012-01-18 16:25:09 -05002518lpfc_bg_scsi_prep_dma_buf_s3(struct lpfc_hba *phba,
James Smartc4908502019-01-28 11:14:28 -08002519 struct lpfc_io_buf *lpfc_cmd)
James Smarte2a0a9d2008-12-04 22:40:02 -05002520{
2521 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2522 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smartc4908502019-01-28 11:14:28 -08002523 struct ulp_bde64 *bpl = (struct ulp_bde64 *)lpfc_cmd->dma_sgl;
James Smarte2a0a9d2008-12-04 22:40:02 -05002524 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2525 uint32_t num_bde = 0;
2526 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
2527 int prot_group_type = 0;
James Smarta6887e22013-04-17 20:18:07 -04002528 int fcpdl;
James Smart5e0e2312019-08-14 16:56:54 -07002529 int ret = 1;
James Smart7c4042a2018-11-29 16:09:40 -08002530 struct lpfc_vport *vport = phba->pport;
James Smarte2a0a9d2008-12-04 22:40:02 -05002531
2532 /*
2533 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
2534 * fcp_rsp regions to the first data bde entry
2535 */
2536 bpl += 2;
2537 if (scsi_sg_count(scsi_cmnd)) {
2538 /*
2539 * The driver stores the segment count returned from pci_map_sg
2540 * because this a count of dma-mappings used to map the use_sg
2541 * pages. They are not guaranteed to be the same for those
2542 * architectures that implement an IOMMU.
2543 */
2544 datasegcnt = dma_map_sg(&phba->pcidev->dev,
2545 scsi_sglist(scsi_cmnd),
2546 scsi_sg_count(scsi_cmnd), datadir);
2547 if (unlikely(!datasegcnt))
2548 return 1;
2549
2550 lpfc_cmd->seg_cnt = datasegcnt;
James Smart96f70772013-04-17 20:16:15 -04002551
2552 /* First check if data segment count from SCSI Layer is good */
James Smart5e0e2312019-08-14 16:56:54 -07002553 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
2554 WARN_ON_ONCE(lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt);
2555 ret = 2;
James Smart96f70772013-04-17 20:16:15 -04002556 goto err;
James Smart5e0e2312019-08-14 16:56:54 -07002557 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002558
2559 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
2560
2561 switch (prot_group_type) {
2562 case LPFC_PG_TYPE_NO_DIF:
James Smart96f70772013-04-17 20:16:15 -04002563
2564 /* Here we need to add a PDE5 and PDE6 to the count */
James Smart5e0e2312019-08-14 16:56:54 -07002565 if ((lpfc_cmd->seg_cnt + 2) > phba->cfg_total_seg_cnt) {
2566 ret = 2;
James Smart96f70772013-04-17 20:16:15 -04002567 goto err;
James Smart5e0e2312019-08-14 16:56:54 -07002568 }
James Smart96f70772013-04-17 20:16:15 -04002569
James Smarte2a0a9d2008-12-04 22:40:02 -05002570 num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
2571 datasegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002572 /* we should have 2 or more entries in buffer list */
James Smart5e0e2312019-08-14 16:56:54 -07002573 if (num_bde < 2) {
2574 ret = 2;
James Smarte2a0a9d2008-12-04 22:40:02 -05002575 goto err;
James Smart5e0e2312019-08-14 16:56:54 -07002576 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002577 break;
James Smart96f70772013-04-17 20:16:15 -04002578
2579 case LPFC_PG_TYPE_DIF_BUF:
James Smarte2a0a9d2008-12-04 22:40:02 -05002580 /*
2581 * This type indicates that protection buffers are
2582 * passed to the driver, so that needs to be prepared
2583 * for DMA
2584 */
2585 protsegcnt = dma_map_sg(&phba->pcidev->dev,
2586 scsi_prot_sglist(scsi_cmnd),
2587 scsi_prot_sg_count(scsi_cmnd), datadir);
2588 if (unlikely(!protsegcnt)) {
2589 scsi_dma_unmap(scsi_cmnd);
2590 return 1;
2591 }
2592
2593 lpfc_cmd->prot_seg_cnt = protsegcnt;
James Smart96f70772013-04-17 20:16:15 -04002594
2595 /*
2596 * There is a minimun of 4 BPLs used for every
2597 * protection data segment.
2598 */
2599 if ((lpfc_cmd->prot_seg_cnt * 4) >
James Smart5e0e2312019-08-14 16:56:54 -07002600 (phba->cfg_total_seg_cnt - 2)) {
2601 ret = 2;
James Smart96f70772013-04-17 20:16:15 -04002602 goto err;
James Smart5e0e2312019-08-14 16:56:54 -07002603 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002604
2605 num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
2606 datasegcnt, protsegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002607 /* we should have 3 or more entries in buffer list */
James Smart96f70772013-04-17 20:16:15 -04002608 if ((num_bde < 3) ||
James Smart5e0e2312019-08-14 16:56:54 -07002609 (num_bde > phba->cfg_total_seg_cnt)) {
2610 ret = 2;
James Smarte2a0a9d2008-12-04 22:40:02 -05002611 goto err;
James Smart5e0e2312019-08-14 16:56:54 -07002612 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002613 break;
James Smart96f70772013-04-17 20:16:15 -04002614
James Smarte2a0a9d2008-12-04 22:40:02 -05002615 case LPFC_PG_TYPE_INVALID:
2616 default:
James Smart96f70772013-04-17 20:16:15 -04002617 scsi_dma_unmap(scsi_cmnd);
2618 lpfc_cmd->seg_cnt = 0;
2619
James Smarte2a0a9d2008-12-04 22:40:02 -05002620 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2621 "9022 Unexpected protection group %i\n",
2622 prot_group_type);
James Smart5e0e2312019-08-14 16:56:54 -07002623 return 2;
James Smarte2a0a9d2008-12-04 22:40:02 -05002624 }
2625 }
2626
2627 /*
2628 * Finish initializing those IOCB fields that are dependent on the
2629 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
2630 * reinitialized since all iocb memory resources are used many times
2631 * for transmit, receive, and continuation bpl's.
2632 */
2633 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
2634 iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
2635 iocb_cmd->ulpBdeCount = 1;
2636 iocb_cmd->ulpLe = 1;
2637
James Smarta6887e22013-04-17 20:18:07 -04002638 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002639 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
2640
2641 /*
2642 * Due to difference in data length between DIF/non-DIF paths,
2643 * we need to set word 4 of IOCB here
2644 */
2645 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
2646
James Smart7c4042a2018-11-29 16:09:40 -08002647 /*
2648 * For First burst, we may need to adjust the initial transfer
2649 * length for DIF
2650 */
2651 if (iocb_cmd->un.fcpi.fcpi_XRdy &&
2652 (fcpdl < vport->cfg_first_burst_size))
2653 iocb_cmd->un.fcpi.fcpi_XRdy = fcpdl;
2654
James Smarte2a0a9d2008-12-04 22:40:02 -05002655 return 0;
2656err:
James Smart96f70772013-04-17 20:16:15 -04002657 if (lpfc_cmd->seg_cnt)
2658 scsi_dma_unmap(scsi_cmnd);
2659 if (lpfc_cmd->prot_seg_cnt)
2660 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
2661 scsi_prot_sg_count(scsi_cmnd),
2662 scsi_cmnd->sc_data_direction);
2663
James Smarte2a0a9d2008-12-04 22:40:02 -05002664 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart96f70772013-04-17 20:16:15 -04002665 "9023 Cannot setup S/G List for HBA"
2666 "IO segs %d/%d BPL %d SCSI %d: %d %d\n",
2667 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
2668 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
James Smarte2a0a9d2008-12-04 22:40:02 -05002669 prot_group_type, num_bde);
James Smart96f70772013-04-17 20:16:15 -04002670
2671 lpfc_cmd->seg_cnt = 0;
2672 lpfc_cmd->prot_seg_cnt = 0;
James Smart5e0e2312019-08-14 16:56:54 -07002673 return ret;
James Smarte2a0a9d2008-12-04 22:40:02 -05002674}
2675
2676/*
James Smart737d4242013-04-17 20:14:49 -04002677 * This function calcuates the T10 DIF guard tag
2678 * on the specified data using a CRC algorithmn
2679 * using crc_t10dif.
2680 */
Rashika Kheria7bfe7812014-09-03 12:55:36 -04002681static uint16_t
James Smart737d4242013-04-17 20:14:49 -04002682lpfc_bg_crc(uint8_t *data, int count)
2683{
2684 uint16_t crc = 0;
2685 uint16_t x;
2686
2687 crc = crc_t10dif(data, count);
2688 x = cpu_to_be16(crc);
2689 return x;
2690}
2691
2692/*
2693 * This function calcuates the T10 DIF guard tag
2694 * on the specified data using a CSUM algorithmn
2695 * using ip_compute_csum.
2696 */
Rashika Kheria7bfe7812014-09-03 12:55:36 -04002697static uint16_t
James Smart737d4242013-04-17 20:14:49 -04002698lpfc_bg_csum(uint8_t *data, int count)
2699{
2700 uint16_t ret;
2701
2702 ret = ip_compute_csum(data, count);
2703 return ret;
2704}
2705
2706/*
2707 * This function examines the protection data to try to determine
2708 * what type of T10-DIF error occurred.
2709 */
Rashika Kheria7bfe7812014-09-03 12:55:36 -04002710static void
James Smartc4908502019-01-28 11:14:28 -08002711lpfc_calc_bg_err(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
James Smart737d4242013-04-17 20:14:49 -04002712{
2713 struct scatterlist *sgpe; /* s/g prot entry */
2714 struct scatterlist *sgde; /* s/g data entry */
2715 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
2716 struct scsi_dif_tuple *src = NULL;
2717 uint8_t *data_src = NULL;
Sebastian Herbsztdb6f1c22015-08-31 16:48:14 -04002718 uint16_t guard_tag;
James Smart737d4242013-04-17 20:14:49 -04002719 uint16_t start_app_tag, app_tag;
2720 uint32_t start_ref_tag, ref_tag;
2721 int prot, protsegcnt;
2722 int err_type, len, data_len;
2723 int chk_ref, chk_app, chk_guard;
2724 uint16_t sum;
2725 unsigned blksize;
2726
2727 err_type = BGS_GUARD_ERR_MASK;
2728 sum = 0;
2729 guard_tag = 0;
2730
2731 /* First check to see if there is protection data to examine */
2732 prot = scsi_get_prot_op(cmd);
2733 if ((prot == SCSI_PROT_READ_STRIP) ||
2734 (prot == SCSI_PROT_WRITE_INSERT) ||
2735 (prot == SCSI_PROT_NORMAL))
2736 goto out;
2737
2738 /* Currently the driver just supports ref_tag and guard_tag checking */
2739 chk_ref = 1;
2740 chk_app = 0;
2741 chk_guard = 0;
2742
2743 /* Setup a ptr to the protection data provided by the SCSI host */
2744 sgpe = scsi_prot_sglist(cmd);
2745 protsegcnt = lpfc_cmd->prot_seg_cnt;
2746
2747 if (sgpe && protsegcnt) {
2748
2749 /*
2750 * We will only try to verify guard tag if the segment
2751 * data length is a multiple of the blksize.
2752 */
2753 sgde = scsi_sglist(cmd);
2754 blksize = lpfc_cmd_blksize(cmd);
2755 data_src = (uint8_t *)sg_virt(sgde);
2756 data_len = sgde->length;
2757 if ((data_len & (blksize - 1)) == 0)
2758 chk_guard = 1;
James Smart737d4242013-04-17 20:14:49 -04002759
James Smarte85d8f92013-05-31 17:04:10 -04002760 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
James Smarta6887e22013-04-17 20:18:07 -04002761 start_ref_tag = (uint32_t)scsi_get_lba(cmd); /* Truncate LBA */
James Smart737d4242013-04-17 20:14:49 -04002762 start_app_tag = src->app_tag;
James Smart737d4242013-04-17 20:14:49 -04002763 len = sgpe->length;
2764 while (src && protsegcnt) {
2765 while (len) {
2766
2767 /*
2768 * First check to see if a protection data
2769 * check is valid
2770 */
Dmitry Monakhov128b6f92017-06-29 11:31:12 -07002771 if ((src->ref_tag == T10_PI_REF_ESCAPE) ||
2772 (src->app_tag == T10_PI_APP_ESCAPE)) {
James Smart737d4242013-04-17 20:14:49 -04002773 start_ref_tag++;
2774 goto skipit;
2775 }
2776
James Smart9c6aa9d2013-05-31 17:03:39 -04002777 /* First Guard Tag checking */
2778 if (chk_guard) {
2779 guard_tag = src->guard_tag;
2780 if (lpfc_cmd_guard_csum(cmd))
2781 sum = lpfc_bg_csum(data_src,
2782 blksize);
2783 else
2784 sum = lpfc_bg_crc(data_src,
2785 blksize);
2786 if ((guard_tag != sum)) {
2787 err_type = BGS_GUARD_ERR_MASK;
2788 goto out;
2789 }
James Smart737d4242013-04-17 20:14:49 -04002790 }
2791
2792 /* Reference Tag checking */
2793 ref_tag = be32_to_cpu(src->ref_tag);
2794 if (chk_ref && (ref_tag != start_ref_tag)) {
2795 err_type = BGS_REFTAG_ERR_MASK;
2796 goto out;
2797 }
2798 start_ref_tag++;
2799
James Smart9c6aa9d2013-05-31 17:03:39 -04002800 /* App Tag checking */
2801 app_tag = src->app_tag;
2802 if (chk_app && (app_tag != start_app_tag)) {
2803 err_type = BGS_APPTAG_ERR_MASK;
2804 goto out;
James Smart737d4242013-04-17 20:14:49 -04002805 }
2806skipit:
2807 len -= sizeof(struct scsi_dif_tuple);
2808 if (len < 0)
2809 len = 0;
2810 src++;
2811
2812 data_src += blksize;
2813 data_len -= blksize;
2814
2815 /*
2816 * Are we at the end of the Data segment?
2817 * The data segment is only used for Guard
2818 * tag checking.
2819 */
2820 if (chk_guard && (data_len == 0)) {
2821 chk_guard = 0;
2822 sgde = sg_next(sgde);
2823 if (!sgde)
2824 goto out;
2825
2826 data_src = (uint8_t *)sg_virt(sgde);
2827 data_len = sgde->length;
2828 if ((data_len & (blksize - 1)) == 0)
2829 chk_guard = 1;
2830 }
2831 }
2832
2833 /* Goto the next Protection data segment */
2834 sgpe = sg_next(sgpe);
2835 if (sgpe) {
2836 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
2837 len = sgpe->length;
2838 } else {
2839 src = NULL;
2840 }
2841 protsegcnt--;
2842 }
2843 }
2844out:
2845 if (err_type == BGS_GUARD_ERR_MASK) {
2846 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2847 0x10, 0x1);
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02002848 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
2849 SAM_STAT_CHECK_CONDITION;
James Smart737d4242013-04-17 20:14:49 -04002850 phba->bg_guard_err_cnt++;
2851 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2852 "9069 BLKGRD: LBA %lx grd_tag error %x != %x\n",
2853 (unsigned long)scsi_get_lba(cmd),
2854 sum, guard_tag);
2855
2856 } else if (err_type == BGS_REFTAG_ERR_MASK) {
2857 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2858 0x10, 0x3);
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02002859 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
2860 SAM_STAT_CHECK_CONDITION;
James Smart737d4242013-04-17 20:14:49 -04002861
2862 phba->bg_reftag_err_cnt++;
2863 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2864 "9066 BLKGRD: LBA %lx ref_tag error %x != %x\n",
2865 (unsigned long)scsi_get_lba(cmd),
2866 ref_tag, start_ref_tag);
2867
2868 } else if (err_type == BGS_APPTAG_ERR_MASK) {
2869 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2870 0x10, 0x2);
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02002871 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
2872 SAM_STAT_CHECK_CONDITION;
James Smart737d4242013-04-17 20:14:49 -04002873
2874 phba->bg_apptag_err_cnt++;
2875 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2876 "9041 BLKGRD: LBA %lx app_tag error %x != %x\n",
2877 (unsigned long)scsi_get_lba(cmd),
2878 app_tag, start_app_tag);
2879 }
2880}
2881
2882
2883/*
James Smarte2a0a9d2008-12-04 22:40:02 -05002884 * This function checks for BlockGuard errors detected by
2885 * the HBA. In case of errors, the ASC/ASCQ fields in the
2886 * sense buffer will be set accordingly, paired with
2887 * ILLEGAL_REQUEST to signal to the kernel that the HBA
2888 * detected corruption.
2889 *
2890 * Returns:
2891 * 0 - No error found
2892 * 1 - BlockGuard error found
2893 * -1 - Internal error (bad profile, ...etc)
2894 */
2895static int
James Smartc4908502019-01-28 11:14:28 -08002896lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd,
2897 struct lpfc_iocbq *pIocbOut)
James Smarte2a0a9d2008-12-04 22:40:02 -05002898{
2899 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
2900 struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
2901 int ret = 0;
2902 uint32_t bghm = bgf->bghm;
2903 uint32_t bgstat = bgf->bgstat;
2904 uint64_t failing_sector = 0;
2905
James Smarte2a0a9d2008-12-04 22:40:02 -05002906 if (lpfc_bgs_get_invalid_prof(bgstat)) {
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02002907 cmd->result = DID_ERROR << 16;
James Smart737d4242013-04-17 20:14:49 -04002908 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2909 "9072 BLKGRD: Invalid BG Profile in cmd"
2910 " 0x%x lba 0x%llx blk cnt 0x%x "
2911 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
2912 (unsigned long long)scsi_get_lba(cmd),
2913 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05002914 ret = (-1);
2915 goto out;
2916 }
2917
2918 if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02002919 cmd->result = DID_ERROR << 16;
James Smart737d4242013-04-17 20:14:49 -04002920 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2921 "9073 BLKGRD: Invalid BG PDIF Block in cmd"
2922 " 0x%x lba 0x%llx blk cnt 0x%x "
2923 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
2924 (unsigned long long)scsi_get_lba(cmd),
2925 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05002926 ret = (-1);
2927 goto out;
2928 }
2929
2930 if (lpfc_bgs_get_guard_err(bgstat)) {
2931 ret = 1;
2932
2933 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2934 0x10, 0x1);
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02002935 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
2936 SAM_STAT_CHECK_CONDITION;
James Smarte2a0a9d2008-12-04 22:40:02 -05002937 phba->bg_guard_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04002938 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2939 "9055 BLKGRD: Guard Tag error in cmd"
2940 " 0x%x lba 0x%llx blk cnt 0x%x "
2941 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
2942 (unsigned long long)scsi_get_lba(cmd),
2943 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05002944 }
2945
2946 if (lpfc_bgs_get_reftag_err(bgstat)) {
2947 ret = 1;
2948
2949 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2950 0x10, 0x3);
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02002951 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
2952 SAM_STAT_CHECK_CONDITION;
James Smarte2a0a9d2008-12-04 22:40:02 -05002953
2954 phba->bg_reftag_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04002955 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2956 "9056 BLKGRD: Ref Tag error in cmd"
2957 " 0x%x lba 0x%llx blk cnt 0x%x "
2958 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
2959 (unsigned long long)scsi_get_lba(cmd),
2960 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05002961 }
2962
2963 if (lpfc_bgs_get_apptag_err(bgstat)) {
2964 ret = 1;
2965
2966 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2967 0x10, 0x2);
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02002968 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
2969 SAM_STAT_CHECK_CONDITION;
James Smarte2a0a9d2008-12-04 22:40:02 -05002970
2971 phba->bg_apptag_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04002972 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2973 "9061 BLKGRD: App Tag error in cmd"
2974 " 0x%x lba 0x%llx blk cnt 0x%x "
2975 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
2976 (unsigned long long)scsi_get_lba(cmd),
2977 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05002978 }
2979
2980 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
2981 /*
2982 * setup sense data descriptor 0 per SPC-4 as an information
James Smart7c56b9f2011-07-22 18:36:25 -04002983 * field, and put the failing LBA in it.
2984 * This code assumes there was also a guard/app/ref tag error
2985 * indication.
James Smarte2a0a9d2008-12-04 22:40:02 -05002986 */
James Smart7c56b9f2011-07-22 18:36:25 -04002987 cmd->sense_buffer[7] = 0xc; /* Additional sense length */
2988 cmd->sense_buffer[8] = 0; /* Information descriptor type */
2989 cmd->sense_buffer[9] = 0xa; /* Additional descriptor length */
2990 cmd->sense_buffer[10] = 0x80; /* Validity bit */
James Smartacd68592012-01-18 16:25:09 -05002991
2992 /* bghm is a "on the wire" FC frame based count */
2993 switch (scsi_get_prot_op(cmd)) {
2994 case SCSI_PROT_READ_INSERT:
2995 case SCSI_PROT_WRITE_STRIP:
2996 bghm /= cmd->device->sector_size;
2997 break;
2998 case SCSI_PROT_READ_STRIP:
2999 case SCSI_PROT_WRITE_INSERT:
3000 case SCSI_PROT_READ_PASS:
3001 case SCSI_PROT_WRITE_PASS:
3002 bghm /= (cmd->device->sector_size +
3003 sizeof(struct scsi_dif_tuple));
3004 break;
3005 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003006
3007 failing_sector = scsi_get_lba(cmd);
3008 failing_sector += bghm;
3009
James Smart7c56b9f2011-07-22 18:36:25 -04003010 /* Descriptor Information */
3011 put_unaligned_be64(failing_sector, &cmd->sense_buffer[12]);
James Smarte2a0a9d2008-12-04 22:40:02 -05003012 }
3013
3014 if (!ret) {
3015 /* No error was reported - problem in FW? */
James Smart737d4242013-04-17 20:14:49 -04003016 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3017 "9057 BLKGRD: Unknown error in cmd"
3018 " 0x%x lba 0x%llx blk cnt 0x%x "
3019 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3020 (unsigned long long)scsi_get_lba(cmd),
3021 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003022
James Smart737d4242013-04-17 20:14:49 -04003023 /* Calcuate what type of error it was */
3024 lpfc_calc_bg_err(phba, lpfc_cmd);
3025 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003026out:
3027 return ret;
3028}
3029
James Smartea2151b2008-09-07 11:52:10 -04003030/**
James Smartda0436e2009-05-22 14:51:39 -04003031 * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3032 * @phba: The Hba for which this call is being executed.
3033 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3034 *
3035 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
3036 * field of @lpfc_cmd for device with SLI-4 interface spec.
3037 *
3038 * Return codes:
James Smart5e0e2312019-08-14 16:56:54 -07003039 * 2 - Error - Do not retry
3040 * 1 - Error - Retry
James Smart6c8eea52010-04-06 14:49:53 -04003041 * 0 - Success
James Smartda0436e2009-05-22 14:51:39 -04003042 **/
3043static int
James Smartc4908502019-01-28 11:14:28 -08003044lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
James Smartda0436e2009-05-22 14:51:39 -04003045{
3046 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3047 struct scatterlist *sgel = NULL;
3048 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart0794d602019-01-28 11:14:19 -08003049 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
James Smartfedd3b72011-02-16 12:39:24 -05003050 struct sli4_sge *first_data_sgl;
James Smartda0436e2009-05-22 14:51:39 -04003051 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
3052 dma_addr_t physaddr;
3053 uint32_t num_bde = 0;
3054 uint32_t dma_len;
3055 uint32_t dma_offset = 0;
James Smartd79c9e92019-08-14 16:57:09 -07003056 int nseg, i, j;
James Smartfedd3b72011-02-16 12:39:24 -05003057 struct ulp_bde64 *bde;
James Smartd79c9e92019-08-14 16:57:09 -07003058 bool lsp_just_set = false;
3059 struct sli4_hybrid_sgl *sgl_xtra = NULL;
James Smartda0436e2009-05-22 14:51:39 -04003060
3061 /*
3062 * There are three possibilities here - use scatter-gather segment, use
3063 * the single mapping, or neither. Start the lpfc command prep by
3064 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
3065 * data bde entry.
3066 */
3067 if (scsi_sg_count(scsi_cmnd)) {
3068 /*
3069 * The driver stores the segment count returned from pci_map_sg
3070 * because this a count of dma-mappings used to map the use_sg
3071 * pages. They are not guaranteed to be the same for those
3072 * architectures that implement an IOMMU.
3073 */
3074
3075 nseg = scsi_dma_map(scsi_cmnd);
James Smart5116fbf2015-05-22 10:42:39 -04003076 if (unlikely(nseg <= 0))
James Smartda0436e2009-05-22 14:51:39 -04003077 return 1;
3078 sgl += 1;
3079 /* clear the last flag in the fcp_rsp map entry */
3080 sgl->word2 = le32_to_cpu(sgl->word2);
3081 bf_set(lpfc_sli4_sge_last, sgl, 0);
3082 sgl->word2 = cpu_to_le32(sgl->word2);
3083 sgl += 1;
James Smartfedd3b72011-02-16 12:39:24 -05003084 first_data_sgl = sgl;
James Smartda0436e2009-05-22 14:51:39 -04003085 lpfc_cmd->seg_cnt = nseg;
James Smartd79c9e92019-08-14 16:57:09 -07003086 if (!phba->cfg_xpsgl &&
3087 lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04003088 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
3089 " %s: Too many sg segments from "
3090 "dma_map_sg. Config %d, seg_cnt %d\n",
3091 __func__, phba->cfg_sg_seg_cnt,
James Smartda0436e2009-05-22 14:51:39 -04003092 lpfc_cmd->seg_cnt);
James Smart5e0e2312019-08-14 16:56:54 -07003093 WARN_ON_ONCE(lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt);
James Smart96f70772013-04-17 20:16:15 -04003094 lpfc_cmd->seg_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04003095 scsi_dma_unmap(scsi_cmnd);
James Smart5e0e2312019-08-14 16:56:54 -07003096 return 2;
James Smartda0436e2009-05-22 14:51:39 -04003097 }
3098
3099 /*
3100 * The driver established a maximum scatter-gather segment count
3101 * during probe that limits the number of sg elements in any
3102 * single scsi command. Just run through the seg_cnt and format
3103 * the sge's.
3104 * When using SLI-3 the driver will try to fit all the BDEs into
3105 * the IOCB. If it can't then the BDEs get added to a BPL as it
3106 * does for SLI-2 mode.
3107 */
James Smartd79c9e92019-08-14 16:57:09 -07003108
3109 /* for tracking segment boundaries */
3110 sgel = scsi_sglist(scsi_cmnd);
3111 j = 2;
3112 for (i = 0; i < nseg; i++) {
3113 sgl->word2 = 0;
3114 if ((num_bde + 1) == nseg) {
James Smartda0436e2009-05-22 14:51:39 -04003115 bf_set(lpfc_sli4_sge_last, sgl, 1);
James Smartd79c9e92019-08-14 16:57:09 -07003116 bf_set(lpfc_sli4_sge_type, sgl,
3117 LPFC_SGE_TYPE_DATA);
3118 } else {
James Smartda0436e2009-05-22 14:51:39 -04003119 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smartd79c9e92019-08-14 16:57:09 -07003120
3121 /* do we need to expand the segment */
3122 if (!lsp_just_set &&
3123 !((j + 1) % phba->border_sge_num) &&
3124 ((nseg - 1) != i)) {
3125 /* set LSP type */
3126 bf_set(lpfc_sli4_sge_type, sgl,
3127 LPFC_SGE_TYPE_LSP);
3128
3129 sgl_xtra = lpfc_get_sgl_per_hdwq(
3130 phba, lpfc_cmd);
3131
3132 if (unlikely(!sgl_xtra)) {
3133 lpfc_cmd->seg_cnt = 0;
3134 scsi_dma_unmap(scsi_cmnd);
3135 return 1;
3136 }
3137 sgl->addr_lo = cpu_to_le32(putPaddrLow(
3138 sgl_xtra->dma_phys_sgl));
3139 sgl->addr_hi = cpu_to_le32(putPaddrHigh(
3140 sgl_xtra->dma_phys_sgl));
3141
3142 } else {
3143 bf_set(lpfc_sli4_sge_type, sgl,
3144 LPFC_SGE_TYPE_DATA);
3145 }
3146 }
3147
3148 if (!(bf_get(lpfc_sli4_sge_type, sgl) &
3149 LPFC_SGE_TYPE_LSP)) {
3150 if ((nseg - 1) == i)
3151 bf_set(lpfc_sli4_sge_last, sgl, 1);
3152
3153 physaddr = sg_dma_address(sgel);
3154 dma_len = sg_dma_len(sgel);
3155 sgl->addr_lo = cpu_to_le32(putPaddrLow(
3156 physaddr));
3157 sgl->addr_hi = cpu_to_le32(putPaddrHigh(
3158 physaddr));
3159
3160 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
3161 sgl->word2 = cpu_to_le32(sgl->word2);
3162 sgl->sge_len = cpu_to_le32(dma_len);
3163
3164 dma_offset += dma_len;
3165 sgel = sg_next(sgel);
3166
3167 sgl++;
3168 lsp_just_set = false;
3169
3170 } else {
3171 sgl->word2 = cpu_to_le32(sgl->word2);
3172 sgl->sge_len = cpu_to_le32(
3173 phba->cfg_sg_dma_buf_size);
3174
3175 sgl = (struct sli4_sge *)sgl_xtra->dma_sgl;
3176 i = i - 1;
3177
3178 lsp_just_set = true;
3179 }
3180
3181 j++;
James Smartda0436e2009-05-22 14:51:39 -04003182 }
James Smart0bc2b7c2018-02-22 08:18:48 -08003183 /*
3184 * Setup the first Payload BDE. For FCoE we just key off
James Smart414abe02018-06-26 08:24:26 -07003185 * Performance Hints, for FC we use lpfc_enable_pbde.
3186 * We populate words 13-15 of IOCB/WQE.
James Smart0bc2b7c2018-02-22 08:18:48 -08003187 */
3188 if ((phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) ||
James Smart414abe02018-06-26 08:24:26 -07003189 phba->cfg_enable_pbde) {
James Smartfedd3b72011-02-16 12:39:24 -05003190 bde = (struct ulp_bde64 *)
James Smart414abe02018-06-26 08:24:26 -07003191 &(iocb_cmd->unsli3.sli3Words[5]);
James Smartfedd3b72011-02-16 12:39:24 -05003192 bde->addrLow = first_data_sgl->addr_lo;
3193 bde->addrHigh = first_data_sgl->addr_hi;
3194 bde->tus.f.bdeSize =
3195 le32_to_cpu(first_data_sgl->sge_len);
3196 bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
3197 bde->tus.w = cpu_to_le32(bde->tus.w);
3198 }
James Smartda0436e2009-05-22 14:51:39 -04003199 } else {
3200 sgl += 1;
3201 /* clear the last flag in the fcp_rsp map entry */
3202 sgl->word2 = le32_to_cpu(sgl->word2);
3203 bf_set(lpfc_sli4_sge_last, sgl, 1);
3204 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart414abe02018-06-26 08:24:26 -07003205
3206 if ((phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) ||
3207 phba->cfg_enable_pbde) {
3208 bde = (struct ulp_bde64 *)
3209 &(iocb_cmd->unsli3.sli3Words[5]);
3210 memset(bde, 0, (sizeof(uint32_t) * 3));
3211 }
James Smartda0436e2009-05-22 14:51:39 -04003212 }
3213
3214 /*
3215 * Finish initializing those IOCB fields that are dependent on the
3216 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
3217 * explicitly reinitialized.
3218 * all iocb memory resources are reused.
3219 */
3220 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
3221
3222 /*
3223 * Due to difference in data length between DIF/non-DIF paths,
3224 * we need to set word 4 of IOCB here
3225 */
3226 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
James Smart1ba981f2014-02-20 09:56:45 -05003227
3228 /*
3229 * If the OAS driver feature is enabled and the lun is enabled for
3230 * OAS, set the oas iocb related flags.
3231 */
James Smartf38fa0b2014-04-04 13:52:21 -04003232 if ((phba->cfg_fof) && ((struct lpfc_device_data *)
James Smartc92c8412016-07-06 12:36:05 -07003233 scsi_cmnd->device->hostdata)->oas_enabled) {
James Smart9bd2bff52014-09-03 12:57:30 -04003234 lpfc_cmd->cur_iocbq.iocb_flag |= (LPFC_IO_OAS | LPFC_IO_FOF);
James Smartc92c8412016-07-06 12:36:05 -07003235 lpfc_cmd->cur_iocbq.priority = ((struct lpfc_device_data *)
3236 scsi_cmnd->device->hostdata)->priority;
3237 }
James Smartc4908502019-01-28 11:14:28 -08003238
James Smartda0436e2009-05-22 14:51:39 -04003239 return 0;
3240}
3241
3242/**
James Smartacd68592012-01-18 16:25:09 -05003243 * lpfc_bg_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3244 * @phba: The Hba for which this call is being executed.
3245 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3246 *
3247 * This is the protection/DIF aware version of
3248 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
3249 * two functions eventually, but for now, it's here
James Smart5e0e2312019-08-14 16:56:54 -07003250 * Return codes:
3251 * 2 - Error - Do not retry
3252 * 1 - Error - Retry
3253 * 0 - Success
James Smartacd68592012-01-18 16:25:09 -05003254 **/
3255static int
3256lpfc_bg_scsi_prep_dma_buf_s4(struct lpfc_hba *phba,
James Smartc4908502019-01-28 11:14:28 -08003257 struct lpfc_io_buf *lpfc_cmd)
James Smartacd68592012-01-18 16:25:09 -05003258{
3259 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3260 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart0794d602019-01-28 11:14:19 -08003261 struct sli4_sge *sgl = (struct sli4_sge *)(lpfc_cmd->dma_sgl);
James Smartacd68592012-01-18 16:25:09 -05003262 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart96f70772013-04-17 20:16:15 -04003263 uint32_t num_sge = 0;
James Smartacd68592012-01-18 16:25:09 -05003264 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
3265 int prot_group_type = 0;
3266 int fcpdl;
James Smart5e0e2312019-08-14 16:56:54 -07003267 int ret = 1;
James Smart7c4042a2018-11-29 16:09:40 -08003268 struct lpfc_vport *vport = phba->pport;
James Smartacd68592012-01-18 16:25:09 -05003269
3270 /*
3271 * Start the lpfc command prep by bumping the sgl beyond fcp_cmnd
James Smart96f70772013-04-17 20:16:15 -04003272 * fcp_rsp regions to the first data sge entry
James Smartacd68592012-01-18 16:25:09 -05003273 */
3274 if (scsi_sg_count(scsi_cmnd)) {
3275 /*
3276 * The driver stores the segment count returned from pci_map_sg
3277 * because this a count of dma-mappings used to map the use_sg
3278 * pages. They are not guaranteed to be the same for those
3279 * architectures that implement an IOMMU.
3280 */
3281 datasegcnt = dma_map_sg(&phba->pcidev->dev,
3282 scsi_sglist(scsi_cmnd),
3283 scsi_sg_count(scsi_cmnd), datadir);
3284 if (unlikely(!datasegcnt))
3285 return 1;
3286
3287 sgl += 1;
3288 /* clear the last flag in the fcp_rsp map entry */
3289 sgl->word2 = le32_to_cpu(sgl->word2);
3290 bf_set(lpfc_sli4_sge_last, sgl, 0);
3291 sgl->word2 = cpu_to_le32(sgl->word2);
3292
3293 sgl += 1;
3294 lpfc_cmd->seg_cnt = datasegcnt;
James Smart96f70772013-04-17 20:16:15 -04003295
3296 /* First check if data segment count from SCSI Layer is good */
James Smartd79c9e92019-08-14 16:57:09 -07003297 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt &&
3298 !phba->cfg_xpsgl) {
James Smart5e0e2312019-08-14 16:56:54 -07003299 WARN_ON_ONCE(lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt);
3300 ret = 2;
James Smart96f70772013-04-17 20:16:15 -04003301 goto err;
James Smart5e0e2312019-08-14 16:56:54 -07003302 }
James Smartacd68592012-01-18 16:25:09 -05003303
3304 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
3305
3306 switch (prot_group_type) {
3307 case LPFC_PG_TYPE_NO_DIF:
James Smart96f70772013-04-17 20:16:15 -04003308 /* Here we need to add a DISEED to the count */
James Smartd79c9e92019-08-14 16:57:09 -07003309 if (((lpfc_cmd->seg_cnt + 1) >
3310 phba->cfg_total_seg_cnt) &&
3311 !phba->cfg_xpsgl) {
James Smart5e0e2312019-08-14 16:56:54 -07003312 ret = 2;
James Smart96f70772013-04-17 20:16:15 -04003313 goto err;
James Smart5e0e2312019-08-14 16:56:54 -07003314 }
James Smart96f70772013-04-17 20:16:15 -04003315
3316 num_sge = lpfc_bg_setup_sgl(phba, scsi_cmnd, sgl,
James Smartd79c9e92019-08-14 16:57:09 -07003317 datasegcnt, lpfc_cmd);
James Smart96f70772013-04-17 20:16:15 -04003318
James Smartacd68592012-01-18 16:25:09 -05003319 /* we should have 2 or more entries in buffer list */
James Smart5e0e2312019-08-14 16:56:54 -07003320 if (num_sge < 2) {
3321 ret = 2;
James Smartacd68592012-01-18 16:25:09 -05003322 goto err;
James Smart5e0e2312019-08-14 16:56:54 -07003323 }
James Smartacd68592012-01-18 16:25:09 -05003324 break;
James Smart96f70772013-04-17 20:16:15 -04003325
3326 case LPFC_PG_TYPE_DIF_BUF:
James Smartacd68592012-01-18 16:25:09 -05003327 /*
3328 * This type indicates that protection buffers are
3329 * passed to the driver, so that needs to be prepared
3330 * for DMA
3331 */
3332 protsegcnt = dma_map_sg(&phba->pcidev->dev,
3333 scsi_prot_sglist(scsi_cmnd),
3334 scsi_prot_sg_count(scsi_cmnd), datadir);
3335 if (unlikely(!protsegcnt)) {
3336 scsi_dma_unmap(scsi_cmnd);
3337 return 1;
3338 }
3339
3340 lpfc_cmd->prot_seg_cnt = protsegcnt;
James Smart96f70772013-04-17 20:16:15 -04003341 /*
3342 * There is a minimun of 3 SGEs used for every
3343 * protection data segment.
3344 */
James Smartd79c9e92019-08-14 16:57:09 -07003345 if (((lpfc_cmd->prot_seg_cnt * 3) >
3346 (phba->cfg_total_seg_cnt - 2)) &&
3347 !phba->cfg_xpsgl) {
James Smart5e0e2312019-08-14 16:56:54 -07003348 ret = 2;
James Smart96f70772013-04-17 20:16:15 -04003349 goto err;
James Smart5e0e2312019-08-14 16:56:54 -07003350 }
James Smartacd68592012-01-18 16:25:09 -05003351
James Smart96f70772013-04-17 20:16:15 -04003352 num_sge = lpfc_bg_setup_sgl_prot(phba, scsi_cmnd, sgl,
James Smartd79c9e92019-08-14 16:57:09 -07003353 datasegcnt, protsegcnt, lpfc_cmd);
James Smart96f70772013-04-17 20:16:15 -04003354
James Smartacd68592012-01-18 16:25:09 -05003355 /* we should have 3 or more entries in buffer list */
James Smartd79c9e92019-08-14 16:57:09 -07003356 if (num_sge < 3 ||
3357 (num_sge > phba->cfg_total_seg_cnt &&
3358 !phba->cfg_xpsgl)) {
James Smart5e0e2312019-08-14 16:56:54 -07003359 ret = 2;
James Smartacd68592012-01-18 16:25:09 -05003360 goto err;
James Smart5e0e2312019-08-14 16:56:54 -07003361 }
James Smartacd68592012-01-18 16:25:09 -05003362 break;
James Smart96f70772013-04-17 20:16:15 -04003363
James Smartacd68592012-01-18 16:25:09 -05003364 case LPFC_PG_TYPE_INVALID:
3365 default:
James Smart96f70772013-04-17 20:16:15 -04003366 scsi_dma_unmap(scsi_cmnd);
3367 lpfc_cmd->seg_cnt = 0;
3368
James Smartacd68592012-01-18 16:25:09 -05003369 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
3370 "9083 Unexpected protection group %i\n",
3371 prot_group_type);
James Smart5e0e2312019-08-14 16:56:54 -07003372 return 2;
James Smartacd68592012-01-18 16:25:09 -05003373 }
3374 }
3375
James Smart8012cc32012-10-31 14:44:49 -04003376 switch (scsi_get_prot_op(scsi_cmnd)) {
3377 case SCSI_PROT_WRITE_STRIP:
3378 case SCSI_PROT_READ_STRIP:
3379 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_STRIP;
3380 break;
3381 case SCSI_PROT_WRITE_INSERT:
3382 case SCSI_PROT_READ_INSERT:
3383 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_INSERT;
3384 break;
3385 case SCSI_PROT_WRITE_PASS:
3386 case SCSI_PROT_READ_PASS:
3387 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_PASS;
3388 break;
3389 }
3390
James Smartacd68592012-01-18 16:25:09 -05003391 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
James Smartacd68592012-01-18 16:25:09 -05003392 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
3393
3394 /*
3395 * Due to difference in data length between DIF/non-DIF paths,
3396 * we need to set word 4 of IOCB here
3397 */
3398 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
James Smartacd68592012-01-18 16:25:09 -05003399
James Smart9bd2bff52014-09-03 12:57:30 -04003400 /*
James Smart7c4042a2018-11-29 16:09:40 -08003401 * For First burst, we may need to adjust the initial transfer
3402 * length for DIF
3403 */
3404 if (iocb_cmd->un.fcpi.fcpi_XRdy &&
3405 (fcpdl < vport->cfg_first_burst_size))
3406 iocb_cmd->un.fcpi.fcpi_XRdy = fcpdl;
3407
3408 /*
James Smart9bd2bff52014-09-03 12:57:30 -04003409 * If the OAS driver feature is enabled and the lun is enabled for
3410 * OAS, set the oas iocb related flags.
3411 */
3412 if ((phba->cfg_fof) && ((struct lpfc_device_data *)
3413 scsi_cmnd->device->hostdata)->oas_enabled)
3414 lpfc_cmd->cur_iocbq.iocb_flag |= (LPFC_IO_OAS | LPFC_IO_FOF);
3415
James Smartacd68592012-01-18 16:25:09 -05003416 return 0;
3417err:
James Smart96f70772013-04-17 20:16:15 -04003418 if (lpfc_cmd->seg_cnt)
3419 scsi_dma_unmap(scsi_cmnd);
3420 if (lpfc_cmd->prot_seg_cnt)
3421 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
3422 scsi_prot_sg_count(scsi_cmnd),
3423 scsi_cmnd->sc_data_direction);
3424
James Smartacd68592012-01-18 16:25:09 -05003425 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart96f70772013-04-17 20:16:15 -04003426 "9084 Cannot setup S/G List for HBA"
3427 "IO segs %d/%d SGL %d SCSI %d: %d %d\n",
3428 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
3429 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
3430 prot_group_type, num_sge);
3431
3432 lpfc_cmd->seg_cnt = 0;
3433 lpfc_cmd->prot_seg_cnt = 0;
James Smart5e0e2312019-08-14 16:56:54 -07003434 return ret;
James Smartacd68592012-01-18 16:25:09 -05003435}
3436
3437/**
James Smart3772a992009-05-22 14:50:54 -04003438 * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3439 * @phba: The Hba for which this call is being executed.
3440 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3441 *
3442 * This routine wraps the actual DMA mapping function pointer from the
3443 * lpfc_hba struct.
3444 *
3445 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04003446 * 1 - Error
3447 * 0 - Success
James Smart3772a992009-05-22 14:50:54 -04003448 **/
3449static inline int
James Smartc4908502019-01-28 11:14:28 -08003450lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
James Smart3772a992009-05-22 14:50:54 -04003451{
3452 return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
3453}
3454
3455/**
James Smartacd68592012-01-18 16:25:09 -05003456 * lpfc_bg_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3457 * using BlockGuard.
3458 * @phba: The Hba for which this call is being executed.
3459 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3460 *
3461 * This routine wraps the actual DMA mapping function pointer from the
3462 * lpfc_hba struct.
3463 *
3464 * Return codes:
3465 * 1 - Error
3466 * 0 - Success
3467 **/
3468static inline int
James Smartc4908502019-01-28 11:14:28 -08003469lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
James Smartacd68592012-01-18 16:25:09 -05003470{
3471 return phba->lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
3472}
3473
3474/**
James Smart3621a712009-04-06 18:47:14 -04003475 * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
James Smartea2151b2008-09-07 11:52:10 -04003476 * @phba: Pointer to hba context object.
3477 * @vport: Pointer to vport object.
3478 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
3479 * @rsp_iocb: Pointer to response iocb object which reported error.
3480 *
3481 * This function posts an event when there is a SCSI command reporting
3482 * error from the scsi device.
3483 **/
3484static void
3485lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
James Smartc4908502019-01-28 11:14:28 -08003486 struct lpfc_io_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
James Smartea2151b2008-09-07 11:52:10 -04003487 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3488 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
3489 uint32_t resp_info = fcprsp->rspStatus2;
3490 uint32_t scsi_status = fcprsp->rspStatus3;
3491 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
3492 struct lpfc_fast_path_event *fast_path_evt = NULL;
3493 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
3494 unsigned long flags;
3495
James Smart5989b8d2010-10-22 11:06:56 -04003496 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3497 return;
3498
James Smartea2151b2008-09-07 11:52:10 -04003499 /* If there is queuefull or busy condition send a scsi event */
3500 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
3501 (cmnd->result == SAM_STAT_BUSY)) {
3502 fast_path_evt = lpfc_alloc_fast_evt(phba);
3503 if (!fast_path_evt)
3504 return;
3505 fast_path_evt->un.scsi_evt.event_type =
3506 FC_REG_SCSI_EVENT;
3507 fast_path_evt->un.scsi_evt.subcategory =
3508 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
3509 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
3510 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
3511 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
3512 &pnode->nlp_portname, sizeof(struct lpfc_name));
3513 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
3514 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3515 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
3516 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
3517 fast_path_evt = lpfc_alloc_fast_evt(phba);
3518 if (!fast_path_evt)
3519 return;
3520 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
3521 FC_REG_SCSI_EVENT;
3522 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
3523 LPFC_EVENT_CHECK_COND;
3524 fast_path_evt->un.check_cond_evt.scsi_event.lun =
3525 cmnd->device->lun;
3526 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
3527 &pnode->nlp_portname, sizeof(struct lpfc_name));
3528 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
3529 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3530 fast_path_evt->un.check_cond_evt.sense_key =
3531 cmnd->sense_buffer[2] & 0xf;
3532 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
3533 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
3534 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3535 fcpi_parm &&
3536 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
3537 ((scsi_status == SAM_STAT_GOOD) &&
3538 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
3539 /*
3540 * If status is good or resid does not match with fcp_param and
3541 * there is valid fcpi_parm, then there is a read_check error
3542 */
3543 fast_path_evt = lpfc_alloc_fast_evt(phba);
3544 if (!fast_path_evt)
3545 return;
3546 fast_path_evt->un.read_check_error.header.event_type =
3547 FC_REG_FABRIC_EVENT;
3548 fast_path_evt->un.read_check_error.header.subcategory =
3549 LPFC_EVENT_FCPRDCHKERR;
3550 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
3551 &pnode->nlp_portname, sizeof(struct lpfc_name));
3552 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
3553 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3554 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
3555 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
3556 fast_path_evt->un.read_check_error.fcpiparam =
3557 fcpi_parm;
3558 } else
3559 return;
3560
3561 fast_path_evt->vport = vport;
3562 spin_lock_irqsave(&phba->hbalock, flags);
3563 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
3564 spin_unlock_irqrestore(&phba->hbalock, flags);
3565 lpfc_worker_wake_up(phba);
3566 return;
3567}
James Smart9bad7672008-12-04 22:39:02 -05003568
3569/**
James Smartf1126682009-06-10 17:22:44 -04003570 * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
James Smart3772a992009-05-22 14:50:54 -04003571 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -05003572 * @psb: The scsi buffer which is going to be un-mapped.
3573 *
3574 * This routine does DMA un-mapping of scatter gather list of scsi command
James Smart3772a992009-05-22 14:50:54 -04003575 * field of @lpfc_cmd for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05003576 **/
dea31012005-04-17 16:05:31 -05003577static void
James Smartc4908502019-01-28 11:14:28 -08003578lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_io_buf *psb)
James Smartbcf4dbf2006-07-06 15:50:08 -04003579{
3580 /*
3581 * There are only two special cases to consider. (1) the scsi command
3582 * requested scatter-gather usage or (2) the scsi command allocated
3583 * a request buffer, but did not request use_sg. There is a third
3584 * case, but it does not require resource deallocation.
3585 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003586 if (psb->seg_cnt > 0)
3587 scsi_dma_unmap(psb->pCmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003588 if (psb->prot_seg_cnt > 0)
3589 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
3590 scsi_prot_sg_count(psb->pCmd),
3591 psb->pCmd->sc_data_direction);
James Smartbcf4dbf2006-07-06 15:50:08 -04003592}
3593
James Smart9bad7672008-12-04 22:39:02 -05003594/**
James Smart3621a712009-04-06 18:47:14 -04003595 * lpfc_handler_fcp_err - FCP response handler
James Smart9bad7672008-12-04 22:39:02 -05003596 * @vport: The virtual port for which this call is being executed.
James Smartc4908502019-01-28 11:14:28 -08003597 * @lpfc_cmd: Pointer to lpfc_io_buf data structure.
James Smart9bad7672008-12-04 22:39:02 -05003598 * @rsp_iocb: The response IOCB which contains FCP error.
3599 *
3600 * This routine is called to process response IOCB with status field
3601 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
3602 * based upon SCSI and FCP error.
3603 **/
James Smartbcf4dbf2006-07-06 15:50:08 -04003604static void
James Smartc4908502019-01-28 11:14:28 -08003605lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_io_buf *lpfc_cmd,
James Smart2e0fef82007-06-17 19:56:36 -05003606 struct lpfc_iocbq *rsp_iocb)
dea31012005-04-17 16:05:31 -05003607{
James Smart5afab6b2015-12-16 18:12:01 -05003608 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003609 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3610 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
3611 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
James Smart7054a602007-04-25 09:52:34 -04003612 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
dea31012005-04-17 16:05:31 -05003613 uint32_t resp_info = fcprsp->rspStatus2;
3614 uint32_t scsi_status = fcprsp->rspStatus3;
James Smartc7743952006-12-02 13:34:42 -05003615 uint32_t *lp;
dea31012005-04-17 16:05:31 -05003616 uint32_t host_status = DID_OK;
3617 uint32_t rsplen = 0;
James Smart5afab6b2015-12-16 18:12:01 -05003618 uint32_t fcpDl;
James Smartc7743952006-12-02 13:34:42 -05003619 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea31012005-04-17 16:05:31 -05003620
James Smartea2151b2008-09-07 11:52:10 -04003621
dea31012005-04-17 16:05:31 -05003622 /*
3623 * If this is a task management command, there is no
3624 * scsi packet associated with this lpfc_cmd. The driver
3625 * consumes it.
3626 */
3627 if (fcpcmd->fcpCntl2) {
3628 scsi_status = 0;
3629 goto out;
3630 }
3631
James Smart6a9c52c2009-10-02 15:16:51 -04003632 if (resp_info & RSP_LEN_VALID) {
3633 rsplen = be32_to_cpu(fcprsp->rspRspLen);
James Smarte40a02c2010-02-26 14:13:54 -05003634 if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
James Smart6a9c52c2009-10-02 15:16:51 -04003635 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3636 "2719 Invalid response length: "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02003637 "tgt x%x lun x%llx cmnd x%x rsplen x%x\n",
James Smart6a9c52c2009-10-02 15:16:51 -04003638 cmnd->device->id,
3639 cmnd->device->lun, cmnd->cmnd[0],
3640 rsplen);
3641 host_status = DID_ERROR;
3642 goto out;
3643 }
James Smarte40a02c2010-02-26 14:13:54 -05003644 if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
3645 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3646 "2757 Protocol failure detected during "
3647 "processing of FCP I/O op: "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02003648 "tgt x%x lun x%llx cmnd x%x rspInfo3 x%x\n",
James Smarte40a02c2010-02-26 14:13:54 -05003649 cmnd->device->id,
3650 cmnd->device->lun, cmnd->cmnd[0],
3651 fcprsp->rspInfo3);
3652 host_status = DID_ERROR;
3653 goto out;
3654 }
James Smart6a9c52c2009-10-02 15:16:51 -04003655 }
3656
James Smartc7743952006-12-02 13:34:42 -05003657 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
3658 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
3659 if (snslen > SCSI_SENSE_BUFFERSIZE)
3660 snslen = SCSI_SENSE_BUFFERSIZE;
3661
3662 if (resp_info & RSP_LEN_VALID)
3663 rsplen = be32_to_cpu(fcprsp->rspRspLen);
3664 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
3665 }
3666 lp = (uint32_t *)cmnd->sense_buffer;
3667
James Smartaa1c7ee2012-08-14 14:26:06 -04003668 /* special handling for under run conditions */
3669 if (!scsi_status && (resp_info & RESID_UNDER)) {
3670 /* don't log under runs if fcp set... */
3671 if (vport->cfg_log_verbose & LOG_FCP)
3672 logit = LOG_FCP_ERROR;
3673 /* unless operator says so */
3674 if (vport->cfg_log_verbose & LOG_FCP_UNDER)
3675 logit = LOG_FCP_UNDER;
3676 }
James Smartc7743952006-12-02 13:34:42 -05003677
James Smarte8b62012007-08-02 11:10:09 -04003678 lpfc_printf_vlog(vport, KERN_WARNING, logit,
James Smarte2a0a9d2008-12-04 22:40:02 -05003679 "9024 FCP command x%x failed: x%x SNS x%x x%x "
James Smarte8b62012007-08-02 11:10:09 -04003680 "Data: x%x x%x x%x x%x x%x\n",
3681 cmnd->cmnd[0], scsi_status,
3682 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
3683 be32_to_cpu(fcprsp->rspResId),
3684 be32_to_cpu(fcprsp->rspSnsLen),
3685 be32_to_cpu(fcprsp->rspRspLen),
3686 fcprsp->rspInfo3);
dea31012005-04-17 16:05:31 -05003687
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003688 scsi_set_resid(cmnd, 0);
James Smart5afab6b2015-12-16 18:12:01 -05003689 fcpDl = be32_to_cpu(fcpcmd->fcpDl);
dea31012005-04-17 16:05:31 -05003690 if (resp_info & RESID_UNDER) {
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003691 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea31012005-04-17 16:05:31 -05003692
James Smart73d91e52011-10-10 21:32:10 -04003693 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_UNDER,
James Smart45634a862018-01-30 15:59:00 -08003694 "9025 FCP Underrun, expected %d, "
James Smarte8b62012007-08-02 11:10:09 -04003695 "residual %d Data: x%x x%x x%x\n",
James Smart5afab6b2015-12-16 18:12:01 -05003696 fcpDl,
James Smarte8b62012007-08-02 11:10:09 -04003697 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
3698 cmnd->underflow);
dea31012005-04-17 16:05:31 -05003699
3700 /*
James Smart45634a862018-01-30 15:59:00 -08003701 * If there is an under run, check if under run reported by
James Smart7054a602007-04-25 09:52:34 -04003702 * storage array is same as the under run reported by HBA.
3703 * If this is not same, there is a dropped frame.
3704 */
James Smart45634a862018-01-30 15:59:00 -08003705 if (fcpi_parm && (scsi_get_resid(cmnd) != fcpi_parm)) {
James Smarte8b62012007-08-02 11:10:09 -04003706 lpfc_printf_vlog(vport, KERN_WARNING,
3707 LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05003708 "9026 FCP Read Check Error "
James Smarte8b62012007-08-02 11:10:09 -04003709 "and Underrun Data: x%x x%x x%x x%x\n",
James Smart5afab6b2015-12-16 18:12:01 -05003710 fcpDl,
James Smarte8b62012007-08-02 11:10:09 -04003711 scsi_get_resid(cmnd), fcpi_parm,
3712 cmnd->cmnd[0]);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003713 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
James Smart7054a602007-04-25 09:52:34 -04003714 host_status = DID_ERROR;
3715 }
3716 /*
dea31012005-04-17 16:05:31 -05003717 * The cmnd->underflow is the minimum number of bytes that must
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003718 * be transferred for this command. Provided a sense condition
dea31012005-04-17 16:05:31 -05003719 * is not present, make sure the actual amount transferred is at
3720 * least the underflow value or fail.
3721 */
3722 if (!(resp_info & SNS_LEN_VALID) &&
3723 (scsi_status == SAM_STAT_GOOD) &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003724 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
3725 < cmnd->underflow)) {
James Smarte8b62012007-08-02 11:10:09 -04003726 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05003727 "9027 FCP command x%x residual "
James Smarte8b62012007-08-02 11:10:09 -04003728 "underrun converted to error "
3729 "Data: x%x x%x x%x\n",
James Smart66dbfbe2007-08-05 06:08:38 -04003730 cmnd->cmnd[0], scsi_bufflen(cmnd),
James Smarte8b62012007-08-02 11:10:09 -04003731 scsi_get_resid(cmnd), cmnd->underflow);
dea31012005-04-17 16:05:31 -05003732 host_status = DID_ERROR;
3733 }
3734 } else if (resp_info & RESID_OVER) {
James Smarte8b62012007-08-02 11:10:09 -04003735 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05003736 "9028 FCP command x%x residual overrun error. "
James Smarte4e74272009-07-19 10:01:38 -04003737 "Data: x%x x%x\n", cmnd->cmnd[0],
James Smarte8b62012007-08-02 11:10:09 -04003738 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea31012005-04-17 16:05:31 -05003739 host_status = DID_ERROR;
3740
3741 /*
3742 * Check SLI validation that all the transfer was actually done
James Smart26373d22013-09-06 12:19:17 -04003743 * (fcpi_parm should be zero). Apply check only to reads.
dea31012005-04-17 16:05:31 -05003744 */
James Smart5afab6b2015-12-16 18:12:01 -05003745 } else if (fcpi_parm) {
James Smarte8b62012007-08-02 11:10:09 -04003746 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
James Smart5afab6b2015-12-16 18:12:01 -05003747 "9029 FCP %s Check Error xri x%x Data: "
James Smarteee88772010-09-29 11:19:08 -04003748 "x%x x%x x%x x%x x%x\n",
James Smart5afab6b2015-12-16 18:12:01 -05003749 ((cmnd->sc_data_direction == DMA_FROM_DEVICE) ?
3750 "Read" : "Write"),
3751 ((phba->sli_rev == LPFC_SLI_REV4) ?
3752 lpfc_cmd->cur_iocbq.sli4_xritag :
3753 rsp_iocb->iocb.ulpContext),
3754 fcpDl, be32_to_cpu(fcprsp->rspResId),
James Smarteee88772010-09-29 11:19:08 -04003755 fcpi_parm, cmnd->cmnd[0], scsi_status);
James Smart5afab6b2015-12-16 18:12:01 -05003756
3757 /* There is some issue with the LPe12000 that causes it
3758 * to miscalculate the fcpi_parm and falsely trip this
3759 * recovery logic. Detect this case and don't error when true.
3760 */
3761 if (fcpi_parm > fcpDl)
3762 goto out;
3763
James Smarteee88772010-09-29 11:19:08 -04003764 switch (scsi_status) {
3765 case SAM_STAT_GOOD:
3766 case SAM_STAT_CHECK_CONDITION:
3767 /* Fabric dropped a data frame. Fail any successful
3768 * command in which we detected dropped frames.
3769 * A status of good or some check conditions could
3770 * be considered a successful command.
3771 */
3772 host_status = DID_ERROR;
3773 break;
3774 }
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003775 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea31012005-04-17 16:05:31 -05003776 }
3777
3778 out:
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02003779 cmnd->result = host_status << 16 | scsi_status;
James Smartea2151b2008-09-07 11:52:10 -04003780 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
dea31012005-04-17 16:05:31 -05003781}
3782
James Smart9bad7672008-12-04 22:39:02 -05003783/**
James Smart3621a712009-04-06 18:47:14 -04003784 * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
James Smart9bad7672008-12-04 22:39:02 -05003785 * @phba: The Hba for which this call is being executed.
3786 * @pIocbIn: The command IOCBQ for the scsi cmnd.
James Smart3772a992009-05-22 14:50:54 -04003787 * @pIocbOut: The response IOCBQ for the scsi cmnd.
James Smart9bad7672008-12-04 22:39:02 -05003788 *
3789 * This routine assigns scsi command result by looking into response IOCB
3790 * status field appropriately. This routine handles QUEUE FULL condition as
3791 * well by ramping down device queue depth.
3792 **/
dea31012005-04-17 16:05:31 -05003793static void
3794lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
3795 struct lpfc_iocbq *pIocbOut)
3796{
James Smartc4908502019-01-28 11:14:28 -08003797 struct lpfc_io_buf *lpfc_cmd =
3798 (struct lpfc_io_buf *) pIocbIn->context1;
James Smart2e0fef82007-06-17 19:56:36 -05003799 struct lpfc_vport *vport = pIocbIn->vport;
dea31012005-04-17 16:05:31 -05003800 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
3801 struct lpfc_nodelist *pnode = rdata->pnode;
James Smart75baf692010-06-08 18:31:21 -04003802 struct scsi_cmnd *cmd;
James Smartfa61a542008-01-11 01:52:42 -05003803 unsigned long flags;
James Smartea2151b2008-09-07 11:52:10 -04003804 struct lpfc_fast_path_event *fast_path_evt;
James Smart75baf692010-06-08 18:31:21 -04003805 struct Scsi_Host *shost;
James Smart4c47efc2019-01-28 11:14:25 -08003806 int idx;
James Smart73d91e52011-10-10 21:32:10 -04003807 uint32_t logit = LOG_FCP;
dea31012005-04-17 16:05:31 -05003808
James Smartc2017262019-01-28 11:14:37 -08003809 /* Guard against abort handler being called at same time */
3810 spin_lock(&lpfc_cmd->buf_lock);
3811
James Smart75baf692010-06-08 18:31:21 -04003812 /* Sanity check on return of outstanding command */
James Smart75baf692010-06-08 18:31:21 -04003813 cmd = lpfc_cmd->pCmd;
James Smart43bfea12019-09-21 20:58:57 -07003814 if (!cmd || !phba) {
James Smartc2017262019-01-28 11:14:37 -08003815 lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
3816 "2621 IO completion: Not an active IO\n");
3817 spin_unlock(&lpfc_cmd->buf_lock);
James Smartc90261d2015-12-16 18:11:57 -05003818 return;
James Smartc2017262019-01-28 11:14:37 -08003819 }
James Smart4c47efc2019-01-28 11:14:25 -08003820
3821 idx = lpfc_cmd->cur_iocbq.hba_wqidx;
3822 if (phba->sli4_hba.hdwq)
3823 phba->sli4_hba.hdwq[idx].scsi_cstat.io_cmpls++;
3824
James Smart6a828b02019-01-28 11:14:31 -08003825#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart840eda92020-03-22 11:13:00 -07003826 if (unlikely(phba->hdwqstat_on & LPFC_CHECK_SCSI_IO))
3827 this_cpu_inc(phba->sli4_hba.c_stat->cmpl_io);
James Smart6a828b02019-01-28 11:14:31 -08003828#endif
James Smart75baf692010-06-08 18:31:21 -04003829 shost = cmd->device->host;
3830
James Smarte3d2b802012-08-14 14:25:43 -04003831 lpfc_cmd->result = (pIocbOut->iocb.un.ulpWord[4] & IOERR_PARAM_MASK);
dea31012005-04-17 16:05:31 -05003832 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
James Smart341af102010-01-26 23:07:37 -05003833 /* pick up SLI4 exhange busy status from HBA */
James Smart324e1c42019-10-18 14:18:21 -07003834 if (pIocbOut->iocb_flag & LPFC_EXCHANGE_BUSY)
3835 lpfc_cmd->flags |= LPFC_SBUF_XBUSY;
3836 else
3837 lpfc_cmd->flags &= ~LPFC_SBUF_XBUSY;
James Smart341af102010-01-26 23:07:37 -05003838
James Smart9a6b09c2012-03-01 22:37:42 -05003839#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
3840 if (lpfc_cmd->prot_data_type) {
3841 struct scsi_dif_tuple *src = NULL;
3842
3843 src = (struct scsi_dif_tuple *)lpfc_cmd->prot_data_segment;
3844 /*
3845 * Used to restore any changes to protection
3846 * data for error injection.
3847 */
3848 switch (lpfc_cmd->prot_data_type) {
3849 case LPFC_INJERR_REFTAG:
3850 src->ref_tag =
3851 lpfc_cmd->prot_data;
3852 break;
3853 case LPFC_INJERR_APPTAG:
3854 src->app_tag =
3855 (uint16_t)lpfc_cmd->prot_data;
3856 break;
3857 case LPFC_INJERR_GUARD:
3858 src->guard_tag =
3859 (uint16_t)lpfc_cmd->prot_data;
3860 break;
3861 default:
3862 break;
3863 }
3864
3865 lpfc_cmd->prot_data = 0;
3866 lpfc_cmd->prot_data_type = 0;
3867 lpfc_cmd->prot_data_segment = NULL;
3868 }
3869#endif
James Smart2ea259e2017-02-12 13:52:27 -08003870
James Smart22770cb2019-10-18 14:18:24 -07003871 if (unlikely(lpfc_cmd->status)) {
dea31012005-04-17 16:05:31 -05003872 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
3873 (lpfc_cmd->result & IOERR_DRVR_MASK))
3874 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
3875 else if (lpfc_cmd->status >= IOSTAT_CNT)
3876 lpfc_cmd->status = IOSTAT_DEFAULT;
James Smartaa1c7ee2012-08-14 14:26:06 -04003877 if (lpfc_cmd->status == IOSTAT_FCP_RSP_ERROR &&
3878 !lpfc_cmd->fcp_rsp->rspStatus3 &&
3879 (lpfc_cmd->fcp_rsp->rspStatus2 & RESID_UNDER) &&
3880 !(vport->cfg_log_verbose & LOG_FCP_UNDER))
James Smart73d91e52011-10-10 21:32:10 -04003881 logit = 0;
3882 else
3883 logit = LOG_FCP | LOG_FCP_UNDER;
3884 lpfc_printf_vlog(vport, KERN_WARNING, logit,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02003885 "9030 FCP cmd x%x failed <%d/%lld> "
James Smart5a0d80f2012-05-09 21:18:20 -04003886 "status: x%x result: x%x "
3887 "sid: x%x did: x%x oxid: x%x "
3888 "Data: x%x x%x\n",
James Smart73d91e52011-10-10 21:32:10 -04003889 cmd->cmnd[0],
3890 cmd->device ? cmd->device->id : 0xffff,
3891 cmd->device ? cmd->device->lun : 0xffff,
3892 lpfc_cmd->status, lpfc_cmd->result,
James Smart3bf41ba2013-05-31 17:03:18 -04003893 vport->fc_myDID,
3894 (pnode) ? pnode->nlp_DID : 0,
James Smart5a0d80f2012-05-09 21:18:20 -04003895 phba->sli_rev == LPFC_SLI_REV4 ?
3896 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
James Smart73d91e52011-10-10 21:32:10 -04003897 pIocbOut->iocb.ulpContext,
3898 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
dea31012005-04-17 16:05:31 -05003899
3900 switch (lpfc_cmd->status) {
3901 case IOSTAT_FCP_RSP_ERROR:
3902 /* Call FCP RSP handler to determine result */
James Smart2e0fef82007-06-17 19:56:36 -05003903 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea31012005-04-17 16:05:31 -05003904 break;
3905 case IOSTAT_NPORT_BSY:
3906 case IOSTAT_FABRIC_BSY:
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02003907 cmd->result = DID_TRANSPORT_DISRUPTED << 16;
James Smartea2151b2008-09-07 11:52:10 -04003908 fast_path_evt = lpfc_alloc_fast_evt(phba);
3909 if (!fast_path_evt)
3910 break;
3911 fast_path_evt->un.fabric_evt.event_type =
3912 FC_REG_FABRIC_EVENT;
3913 fast_path_evt->un.fabric_evt.subcategory =
3914 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
3915 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
3916 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
3917 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
3918 &pnode->nlp_portname,
3919 sizeof(struct lpfc_name));
3920 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
3921 &pnode->nlp_nodename,
3922 sizeof(struct lpfc_name));
3923 }
3924 fast_path_evt->vport = vport;
3925 fast_path_evt->work_evt.evt =
3926 LPFC_EVT_FASTPATH_MGMT_EVT;
3927 spin_lock_irqsave(&phba->hbalock, flags);
3928 list_add_tail(&fast_path_evt->work_evt.evt_listp,
3929 &phba->work_list);
3930 spin_unlock_irqrestore(&phba->hbalock, flags);
3931 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05003932 break;
James Smart92d7f7b2007-06-17 19:56:38 -05003933 case IOSTAT_LOCAL_REJECT:
James Smart1151e3e2011-02-16 12:39:35 -05003934 case IOSTAT_REMOTE_STOP:
James Smartab56dc22011-02-16 12:39:57 -05003935 if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
3936 lpfc_cmd->result ==
3937 IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
3938 lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
3939 lpfc_cmd->result ==
3940 IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02003941 cmd->result = DID_NO_CONNECT << 16;
James Smartab56dc22011-02-16 12:39:57 -05003942 break;
3943 }
James Smartd7c255b2008-08-24 21:50:00 -04003944 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
James Smart92d7f7b2007-06-17 19:56:38 -05003945 lpfc_cmd->result == IOERR_NO_RESOURCES ||
James Smartb92938b2010-06-07 15:24:12 -04003946 lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
3947 lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02003948 cmd->result = DID_REQUEUE << 16;
James Smart58da1ff2008-04-07 10:15:56 -04003949 break;
James Smarte2a0a9d2008-12-04 22:40:02 -05003950 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003951 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
3952 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
3953 pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
3954 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
3955 /*
3956 * This is a response for a BG enabled
3957 * cmd. Parse BG error
3958 */
3959 lpfc_parse_bg_err(phba, lpfc_cmd,
3960 pIocbOut);
3961 break;
3962 } else {
3963 lpfc_printf_vlog(vport, KERN_WARNING,
3964 LOG_BG,
3965 "9031 non-zero BGSTAT "
James Smart6a9c52c2009-10-02 15:16:51 -04003966 "on unprotected cmd\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05003967 }
3968 }
James Smart1151e3e2011-02-16 12:39:35 -05003969 if ((lpfc_cmd->status == IOSTAT_REMOTE_STOP)
3970 && (phba->sli_rev == LPFC_SLI_REV4)
3971 && (pnode && NLP_CHK_NODE_ACT(pnode))) {
3972 /* This IO was aborted by the target, we don't
3973 * know the rxid and because we did not send the
3974 * ABTS we cannot generate and RRQ.
3975 */
3976 lpfc_set_rrq_active(phba, pnode,
James Smartee0f4fe2012-05-09 21:19:14 -04003977 lpfc_cmd->cur_iocbq.sli4_lxritag,
3978 0, 0);
James Smart1151e3e2011-02-16 12:39:35 -05003979 }
Bart Van Asschecd05c152019-03-28 11:06:18 -07003980 /* fall through */
dea31012005-04-17 16:05:31 -05003981 default:
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02003982 cmd->result = DID_ERROR << 16;
dea31012005-04-17 16:05:31 -05003983 break;
3984 }
3985
James Smart58da1ff2008-04-07 10:15:56 -04003986 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003987 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02003988 cmd->result = DID_TRANSPORT_DISRUPTED << 16 |
3989 SAM_STAT_BUSY;
James Smartab56dc22011-02-16 12:39:57 -05003990 } else
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02003991 cmd->result = DID_OK << 16;
dea31012005-04-17 16:05:31 -05003992
3993 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
3994 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
3995
James Smarte8b62012007-08-02 11:10:09 -04003996 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smart32350662019-08-14 16:57:06 -07003997 "0710 Iodone <%d/%llu> cmd x%px, error "
James Smarte8b62012007-08-02 11:10:09 -04003998 "x%x SNS x%x x%x Data: x%x x%x\n",
3999 cmd->device->id, cmd->device->lun, cmd,
4000 cmd->result, *lp, *(lp + 3), cmd->retries,
4001 scsi_get_resid(cmd));
dea31012005-04-17 16:05:31 -05004002 }
4003
James Smart9df0a032019-09-21 20:58:54 -07004004 lpfc_update_stats(vport, lpfc_cmd);
James Smart977b5a02008-09-07 11:52:04 -04004005 if (vport->cfg_max_scsicmpl_time &&
4006 time_after(jiffies, lpfc_cmd->start_time +
4007 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
James Smarta257bf92009-04-06 18:48:10 -04004008 spin_lock_irqsave(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05004009 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
4010 if (pnode->cmd_qdepth >
4011 atomic_read(&pnode->cmd_pending) &&
4012 (atomic_read(&pnode->cmd_pending) >
4013 LPFC_MIN_TGT_QDEPTH) &&
4014 ((cmd->cmnd[0] == READ_10) ||
4015 (cmd->cmnd[0] == WRITE_10)))
4016 pnode->cmd_qdepth =
4017 atomic_read(&pnode->cmd_pending);
James Smart977b5a02008-09-07 11:52:04 -04004018
James Smart109f6ed2008-12-04 22:39:08 -05004019 pnode->last_change_time = jiffies;
4020 }
James Smarta257bf92009-04-06 18:48:10 -04004021 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart977b5a02008-09-07 11:52:04 -04004022 }
James Smart1dcb58e2007-04-25 09:51:30 -04004023 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James Smarta257bf92009-04-06 18:48:10 -04004024
James Smartc2017262019-01-28 11:14:37 -08004025 lpfc_cmd->pCmd = NULL;
4026 spin_unlock(&lpfc_cmd->buf_lock);
James Smart92e3af62012-08-14 14:26:28 -04004027
James Smart2fcbc562020-03-22 11:13:02 -07004028#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4029 if (lpfc_cmd->ts_cmd_start) {
4030 lpfc_cmd->ts_isr_cmpl = pIocbIn->isr_timestamp;
4031 lpfc_cmd->ts_data_io = ktime_get_ns();
4032 phba->ktime_last_cmd = lpfc_cmd->ts_data_io;
4033 lpfc_io_ktime(phba, lpfc_cmd);
4034 }
4035#endif
James Smart89533e92016-10-13 15:06:15 -07004036 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
4037 cmd->scsi_done(cmd);
4038
James Smartfa61a542008-01-11 01:52:42 -05004039 /*
James Smartc2017262019-01-28 11:14:37 -08004040 * If there is an abort thread waiting for command completion
James Smartfa61a542008-01-11 01:52:42 -05004041 * wake up the thread.
4042 */
James Smartc2017262019-01-28 11:14:37 -08004043 spin_lock(&lpfc_cmd->buf_lock);
James Smart4eb01532019-04-03 11:10:34 -07004044 lpfc_cmd->cur_iocbq.iocb_flag &= ~LPFC_DRIVER_ABORTED;
James Smartb9e5a2d2019-05-21 17:49:01 -07004045 if (lpfc_cmd->waitq)
James Smart4eb01532019-04-03 11:10:34 -07004046 wake_up(lpfc_cmd->waitq);
James Smartc2017262019-01-28 11:14:37 -08004047 spin_unlock(&lpfc_cmd->buf_lock);
James Smartfa61a542008-01-11 01:52:42 -05004048
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004049 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004050}
4051
James Smart34b02dc2008-08-24 21:49:55 -04004052/**
Alexey Kardashevskiy8b2564e2015-04-28 18:26:22 +10004053 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
4054 * @data: A pointer to the immediate command data portion of the IOCB.
4055 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
4056 *
4057 * The routine copies the entire FCP command from @fcp_cmnd to @data while
4058 * byte swapping the data to big endian format for transmission on the wire.
4059 **/
4060static void
4061lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
4062{
4063 int i, j;
4064 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
4065 i += sizeof(uint32_t), j++) {
4066 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
4067 }
4068}
4069
4070/**
James Smartf1126682009-06-10 17:22:44 -04004071 * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05004072 * @vport: The virtual port for which this call is being executed.
4073 * @lpfc_cmd: The scsi command which needs to send.
4074 * @pnode: Pointer to lpfc_nodelist.
4075 *
4076 * This routine initializes fcp_cmnd and iocb data structure from scsi command
James Smart3772a992009-05-22 14:50:54 -04004077 * to transfer for device with SLI3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05004078 **/
dea31012005-04-17 16:05:31 -05004079static void
James Smartc4908502019-01-28 11:14:28 -08004080lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_io_buf *lpfc_cmd,
James Smart2e0fef82007-06-17 19:56:36 -05004081 struct lpfc_nodelist *pnode)
dea31012005-04-17 16:05:31 -05004082{
James Smart2e0fef82007-06-17 19:56:36 -05004083 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004084 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
4085 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
4086 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
4087 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
James Smart4c47efc2019-01-28 11:14:25 -08004088 struct lpfc_sli4_hdw_queue *hdwq = NULL;
dea31012005-04-17 16:05:31 -05004089 int datadir = scsi_cmnd->sc_data_direction;
James Smart4c47efc2019-01-28 11:14:25 -08004090 int idx;
James Smart027140e2012-08-03 12:35:44 -04004091 uint8_t *ptr;
4092 bool sli4;
James Smart98bbf5f2013-09-06 12:18:45 -04004093 uint32_t fcpdl;
dea31012005-04-17 16:05:31 -05004094
James Smart58da1ff2008-04-07 10:15:56 -04004095 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
4096 return;
4097
dea31012005-04-17 16:05:31 -05004098 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
James.Smart@Emulex.Com69859dc2005-08-10 15:02:37 -04004099 /* clear task management bits */
4100 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea31012005-04-17 16:05:31 -05004101
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -04004102 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
4103 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05004104
James Smart027140e2012-08-03 12:35:44 -04004105 ptr = &fcp_cmnd->fcpCdb[0];
4106 memcpy(ptr, scsi_cmnd->cmnd, scsi_cmnd->cmd_len);
4107 if (scsi_cmnd->cmd_len < LPFC_FCP_CDB_LEN) {
4108 ptr += scsi_cmnd->cmd_len;
4109 memset(ptr, 0, (LPFC_FCP_CDB_LEN - scsi_cmnd->cmd_len));
4110 }
4111
Christoph Hellwig50668632014-10-30 14:30:06 +01004112 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
dea31012005-04-17 16:05:31 -05004113
James Smart027140e2012-08-03 12:35:44 -04004114 sli4 = (phba->sli_rev == LPFC_SLI_REV4);
James Smart6acb3482014-04-04 13:51:25 -04004115 piocbq->iocb.un.fcpi.fcpi_XRdy = 0;
James Smart1fbf9742019-01-28 11:14:26 -08004116 idx = lpfc_cmd->hdwq_no;
James Smart4c47efc2019-01-28 11:14:25 -08004117 if (phba->sli4_hba.hdwq)
4118 hdwq = &phba->sli4_hba.hdwq[idx];
James Smart027140e2012-08-03 12:35:44 -04004119
dea31012005-04-17 16:05:31 -05004120 /*
4121 * There are three possibilities here - use scatter-gather segment, use
4122 * the single mapping, or neither. Start the lpfc command prep by
4123 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
4124 * data bde entry.
4125 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05004126 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05004127 if (datadir == DMA_TO_DEVICE) {
4128 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
James Smart182ba752013-07-15 18:34:05 -04004129 iocb_cmd->ulpPU = PARM_READ_CHECK;
James Smart3cb01c52013-07-15 18:35:04 -04004130 if (vport->cfg_first_burst_size &&
4131 (pnode->nlp_flag & NLP_FIRSTBURST)) {
James Smart98bbf5f2013-09-06 12:18:45 -04004132 fcpdl = scsi_bufflen(scsi_cmnd);
4133 if (fcpdl < vport->cfg_first_burst_size)
4134 piocbq->iocb.un.fcpi.fcpi_XRdy = fcpdl;
4135 else
4136 piocbq->iocb.un.fcpi.fcpi_XRdy =
4137 vport->cfg_first_burst_size;
James Smart3cb01c52013-07-15 18:35:04 -04004138 }
dea31012005-04-17 16:05:31 -05004139 fcp_cmnd->fcpCntl3 = WRITE_DATA;
James Smart4c47efc2019-01-28 11:14:25 -08004140 if (hdwq)
4141 hdwq->scsi_cstat.output_requests++;
dea31012005-04-17 16:05:31 -05004142 } else {
4143 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
4144 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05004145 fcp_cmnd->fcpCntl3 = READ_DATA;
James Smart4c47efc2019-01-28 11:14:25 -08004146 if (hdwq)
4147 hdwq->scsi_cstat.input_requests++;
dea31012005-04-17 16:05:31 -05004148 }
4149 } else {
4150 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
4151 iocb_cmd->un.fcpi.fcpi_parm = 0;
4152 iocb_cmd->ulpPU = 0;
4153 fcp_cmnd->fcpCntl3 = 0;
James Smart4c47efc2019-01-28 11:14:25 -08004154 if (hdwq)
4155 hdwq->scsi_cstat.control_requests++;
dea31012005-04-17 16:05:31 -05004156 }
Alexey Kardashevskiy8b2564e2015-04-28 18:26:22 +10004157 if (phba->sli_rev == 3 &&
4158 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
4159 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05004160 /*
4161 * Finish initializing those IOCB fields that are independent
4162 * of the scsi_cmnd request_buffer
4163 */
4164 piocbq->iocb.ulpContext = pnode->nlp_rpi;
James Smart027140e2012-08-03 12:35:44 -04004165 if (sli4)
James Smart6d368e52011-05-24 11:44:12 -04004166 piocbq->iocb.ulpContext =
4167 phba->sli4_hba.rpi_ids[pnode->nlp_rpi];
dea31012005-04-17 16:05:31 -05004168 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
4169 piocbq->iocb.ulpFCP2Rcvy = 1;
James Smart09372822008-01-11 01:52:54 -05004170 else
4171 piocbq->iocb.ulpFCP2Rcvy = 0;
dea31012005-04-17 16:05:31 -05004172
4173 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
4174 piocbq->context1 = lpfc_cmd;
4175 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
4176 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
James Smart2e0fef82007-06-17 19:56:36 -05004177 piocbq->vport = vport;
dea31012005-04-17 16:05:31 -05004178}
4179
James Smart9bad7672008-12-04 22:39:02 -05004180/**
James Smart6d368e52011-05-24 11:44:12 -04004181 * lpfc_scsi_prep_task_mgmt_cmd - Convert SLI3 scsi TM cmd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05004182 * @vport: The virtual port for which this call is being executed.
James Smartc4908502019-01-28 11:14:28 -08004183 * @lpfc_cmd: Pointer to lpfc_io_buf data structure.
James Smart9bad7672008-12-04 22:39:02 -05004184 * @lun: Logical unit number.
4185 * @task_mgmt_cmd: SCSI task management command.
4186 *
James Smart3772a992009-05-22 14:50:54 -04004187 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
4188 * for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05004189 *
4190 * Return codes:
4191 * 0 - Error
4192 * 1 - Success
4193 **/
dea31012005-04-17 16:05:31 -05004194static int
James Smartf1126682009-06-10 17:22:44 -04004195lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
James Smartc4908502019-01-28 11:14:28 -08004196 struct lpfc_io_buf *lpfc_cmd,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004197 uint64_t lun,
dea31012005-04-17 16:05:31 -05004198 uint8_t task_mgmt_cmd)
4199{
dea31012005-04-17 16:05:31 -05004200 struct lpfc_iocbq *piocbq;
4201 IOCB_t *piocb;
4202 struct fcp_cmnd *fcp_cmnd;
James Smart0b18ac42006-05-01 21:50:40 -04004203 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea31012005-04-17 16:05:31 -05004204 struct lpfc_nodelist *ndlp = rdata->pnode;
4205
James Smart58da1ff2008-04-07 10:15:56 -04004206 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
4207 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05004208 return 0;
dea31012005-04-17 16:05:31 -05004209
dea31012005-04-17 16:05:31 -05004210 piocbq = &(lpfc_cmd->cur_iocbq);
James Smart2e0fef82007-06-17 19:56:36 -05004211 piocbq->vport = vport;
4212
dea31012005-04-17 16:05:31 -05004213 piocb = &piocbq->iocb;
4214
4215 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart34b02dc2008-08-24 21:49:55 -04004216 /* Clear out any old data in the FCP command area */
4217 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
4218 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05004219 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
James Smarte2a0a9d2008-12-04 22:40:02 -05004220 if (vport->phba->sli_rev == 3 &&
4221 !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04004222 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05004223 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
dea31012005-04-17 16:05:31 -05004224 piocb->ulpContext = ndlp->nlp_rpi;
James Smart6d368e52011-05-24 11:44:12 -04004225 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
4226 piocb->ulpContext =
4227 vport->phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
4228 }
James Smart53151bb2013-10-10 12:24:07 -04004229 piocb->ulpFCP2Rcvy = (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) ? 1 : 0;
dea31012005-04-17 16:05:31 -05004230 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
James Smartf9226c22014-02-20 09:57:28 -05004231 piocb->ulpPU = 0;
4232 piocb->un.fcpi.fcpi_parm = 0;
dea31012005-04-17 16:05:31 -05004233
4234 /* ulpTimeout is only one byte */
4235 if (lpfc_cmd->timeout > 0xff) {
4236 /*
4237 * Do not timeout the command at the firmware level.
4238 * The driver will provide the timeout mechanism.
4239 */
4240 piocb->ulpTimeout = 0;
James Smartf1126682009-06-10 17:22:44 -04004241 } else
dea31012005-04-17 16:05:31 -05004242 piocb->ulpTimeout = lpfc_cmd->timeout;
James Smartf1126682009-06-10 17:22:44 -04004243
4244 if (vport->phba->sli_rev == LPFC_SLI_REV4)
4245 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004246
James Smart2e0fef82007-06-17 19:56:36 -05004247 return 1;
dea31012005-04-17 16:05:31 -05004248}
4249
James Smart9bad7672008-12-04 22:39:02 -05004250/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004251 * lpfc_scsi_api_table_setup - Set up scsi api function jump table
James Smart3772a992009-05-22 14:50:54 -04004252 * @phba: The hba struct for which this call is being executed.
4253 * @dev_grp: The HBA PCI-Device group number.
4254 *
4255 * This routine sets up the SCSI interface API function jump table in @phba
4256 * struct.
4257 * Returns: 0 - success, -ENODEV - failure.
4258 **/
4259int
4260lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4261{
4262
James Smartf1126682009-06-10 17:22:44 -04004263 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
4264 phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
James Smartf1126682009-06-10 17:22:44 -04004265
James Smart3772a992009-05-22 14:50:54 -04004266 switch (dev_grp) {
4267 case LPFC_PCI_DEV_LP:
James Smart3772a992009-05-22 14:50:54 -04004268 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
James Smartacd68592012-01-18 16:25:09 -05004269 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04004270 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
James Smart19ca7602010-11-20 23:11:55 -05004271 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04004272 break;
James Smartda0436e2009-05-22 14:51:39 -04004273 case LPFC_PCI_DEV_OC:
James Smartda0436e2009-05-22 14:51:39 -04004274 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
James Smartacd68592012-01-18 16:25:09 -05004275 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04004276 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
James Smart19ca7602010-11-20 23:11:55 -05004277 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04004278 break;
James Smart3772a992009-05-22 14:50:54 -04004279 default:
4280 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4281 "1418 Invalid HBA PCI-device group: 0x%x\n",
4282 dev_grp);
4283 return -ENODEV;
4284 break;
4285 }
James Smart3772a992009-05-22 14:50:54 -04004286 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
James Smart84d1b002010-02-12 14:42:33 -05004287 phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
James Smart3772a992009-05-22 14:50:54 -04004288 return 0;
4289}
4290
4291/**
James Smart3621a712009-04-06 18:47:14 -04004292 * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
James Smart9bad7672008-12-04 22:39:02 -05004293 * @phba: The Hba for which this call is being executed.
4294 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
4295 * @rspiocbq: Pointer to lpfc_iocbq data structure.
4296 *
4297 * This routine is IOCB completion routine for device reset and target reset
4298 * routine. This routine release scsi buffer associated with lpfc_cmd.
4299 **/
James Smart7054a602007-04-25 09:52:34 -04004300static void
4301lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
4302 struct lpfc_iocbq *cmdiocbq,
4303 struct lpfc_iocbq *rspiocbq)
4304{
James Smartc4908502019-01-28 11:14:28 -08004305 struct lpfc_io_buf *lpfc_cmd =
4306 (struct lpfc_io_buf *) cmdiocbq->context1;
James Smart7054a602007-04-25 09:52:34 -04004307 if (lpfc_cmd)
4308 lpfc_release_scsi_buf(phba, lpfc_cmd);
4309 return;
4310}
4311
James Smart9bad7672008-12-04 22:39:02 -05004312/**
James Smart50212672018-12-13 15:17:57 -08004313 * lpfc_check_pci_resettable - Walks list of devices on pci_dev's bus to check
4314 * if issuing a pci_bus_reset is possibly unsafe
4315 * @phba: lpfc_hba pointer.
4316 *
4317 * Description:
4318 * Walks the bus_list to ensure only PCI devices with Emulex
4319 * vendor id, device ids that support hot reset, and only one occurrence
4320 * of function 0.
4321 *
4322 * Returns:
4323 * -EBADSLT, detected invalid device
4324 * 0, successful
4325 */
4326int
4327lpfc_check_pci_resettable(const struct lpfc_hba *phba)
4328{
4329 const struct pci_dev *pdev = phba->pcidev;
4330 struct pci_dev *ptr = NULL;
4331 u8 counter = 0;
4332
4333 /* Walk the list of devices on the pci_dev's bus */
4334 list_for_each_entry(ptr, &pdev->bus->devices, bus_list) {
4335 /* Check for Emulex Vendor ID */
4336 if (ptr->vendor != PCI_VENDOR_ID_EMULEX) {
4337 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4338 "8346 Non-Emulex vendor found: "
4339 "0x%04x\n", ptr->vendor);
4340 return -EBADSLT;
4341 }
4342
4343 /* Check for valid Emulex Device ID */
4344 switch (ptr->device) {
4345 case PCI_DEVICE_ID_LANCER_FC:
4346 case PCI_DEVICE_ID_LANCER_G6_FC:
4347 case PCI_DEVICE_ID_LANCER_G7_FC:
4348 break;
4349 default:
4350 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4351 "8347 Invalid device found: "
4352 "0x%04x\n", ptr->device);
4353 return -EBADSLT;
4354 }
4355
4356 /* Check for only one function 0 ID to ensure only one HBA on
4357 * secondary bus
4358 */
4359 if (ptr->devfn == 0) {
4360 if (++counter > 1) {
4361 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4362 "8348 More than one device on "
4363 "secondary bus found\n");
4364 return -EBADSLT;
4365 }
4366 }
4367 }
4368
4369 return 0;
4370}
4371
4372/**
James Smart3621a712009-04-06 18:47:14 -04004373 * lpfc_info - Info entry point of scsi_host_template data structure
James Smart9bad7672008-12-04 22:39:02 -05004374 * @host: The scsi host for which this call is being executed.
4375 *
4376 * This routine provides module information about hba.
4377 *
4378 * Reutrn code:
4379 * Pointer to char - Success.
4380 **/
dea31012005-04-17 16:05:31 -05004381const char *
4382lpfc_info(struct Scsi_Host *host)
4383{
James Smart2e0fef82007-06-17 19:56:36 -05004384 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
4385 struct lpfc_hba *phba = vport->phba;
James Smart50212672018-12-13 15:17:57 -08004386 int link_speed = 0;
4387 static char lpfcinfobuf[384];
4388 char tmp[384] = {0};
dea31012005-04-17 16:05:31 -05004389
James Smart50212672018-12-13 15:17:57 -08004390 memset(lpfcinfobuf, 0, sizeof(lpfcinfobuf));
dea31012005-04-17 16:05:31 -05004391 if (phba && phba->pcidev){
James Smart50212672018-12-13 15:17:57 -08004392 /* Model Description */
4393 scnprintf(tmp, sizeof(tmp), phba->ModelDesc);
4394 if (strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf)) >=
4395 sizeof(lpfcinfobuf))
4396 goto buffer_done;
4397
4398 /* PCI Info */
4399 scnprintf(tmp, sizeof(tmp),
4400 " on PCI bus %02x device %02x irq %d",
4401 phba->pcidev->bus->number, phba->pcidev->devfn,
4402 phba->pcidev->irq);
4403 if (strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf)) >=
4404 sizeof(lpfcinfobuf))
4405 goto buffer_done;
4406
4407 /* Port Number */
dea31012005-04-17 16:05:31 -05004408 if (phba->Port[0]) {
James Smart50212672018-12-13 15:17:57 -08004409 scnprintf(tmp, sizeof(tmp), " port %s", phba->Port);
4410 if (strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf)) >=
4411 sizeof(lpfcinfobuf))
4412 goto buffer_done;
dea31012005-04-17 16:05:31 -05004413 }
James Smart50212672018-12-13 15:17:57 -08004414
4415 /* Link Speed */
James Smarta085e872015-12-16 18:12:02 -05004416 link_speed = lpfc_sli_port_speed_get(phba);
James Smart50212672018-12-13 15:17:57 -08004417 if (link_speed != 0) {
4418 scnprintf(tmp, sizeof(tmp),
4419 " Logical Link Speed: %d Mbps", link_speed);
4420 if (strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf)) >=
4421 sizeof(lpfcinfobuf))
4422 goto buffer_done;
4423 }
4424
4425 /* PCI resettable */
4426 if (!lpfc_check_pci_resettable(phba)) {
4427 scnprintf(tmp, sizeof(tmp), " PCI resettable");
4428 strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf));
4429 }
dea31012005-04-17 16:05:31 -05004430 }
James Smart50212672018-12-13 15:17:57 -08004431
4432buffer_done:
dea31012005-04-17 16:05:31 -05004433 return lpfcinfobuf;
4434}
4435
James Smart9bad7672008-12-04 22:39:02 -05004436/**
James Smart3621a712009-04-06 18:47:14 -04004437 * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
James Smart9bad7672008-12-04 22:39:02 -05004438 * @phba: The Hba for which this call is being executed.
4439 *
4440 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
4441 * The default value of cfg_poll_tmo is 10 milliseconds.
4442 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004443static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
4444{
4445 unsigned long poll_tmo_expires =
4446 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
4447
James Smart895427b2017-02-12 13:52:30 -08004448 if (!list_empty(&phba->sli.sli3_ring[LPFC_FCP_RING].txcmplq))
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004449 mod_timer(&phba->fcp_poll_timer,
4450 poll_tmo_expires);
4451}
4452
James Smart9bad7672008-12-04 22:39:02 -05004453/**
James Smart3621a712009-04-06 18:47:14 -04004454 * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
James Smart9bad7672008-12-04 22:39:02 -05004455 * @phba: The Hba for which this call is being executed.
4456 *
4457 * This routine starts the fcp_poll_timer of @phba.
4458 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004459void lpfc_poll_start_timer(struct lpfc_hba * phba)
4460{
4461 lpfc_poll_rearm_timer(phba);
4462}
4463
James Smart9bad7672008-12-04 22:39:02 -05004464/**
James Smart3621a712009-04-06 18:47:14 -04004465 * lpfc_poll_timeout - Restart polling timer
James Smart9bad7672008-12-04 22:39:02 -05004466 * @ptr: Map to lpfc_hba data structure pointer.
4467 *
4468 * This routine restarts fcp_poll timer, when FCP ring polling is enable
4469 * and FCP Ring interrupt is disable.
4470 **/
4471
Kees Cookf22eb4d2017-09-06 20:24:26 -07004472void lpfc_poll_timeout(struct timer_list *t)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004473{
Kees Cookf22eb4d2017-09-06 20:24:26 -07004474 struct lpfc_hba *phba = from_timer(phba, t, fcp_poll_timer);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004475
4476 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004477 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -08004478 &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
James Smart45ed1192009-10-02 15:17:02 -04004479
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004480 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4481 lpfc_poll_rearm_timer(phba);
4482 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004483}
4484
James Smart9bad7672008-12-04 22:39:02 -05004485/**
James Smart3621a712009-04-06 18:47:14 -04004486 * lpfc_queuecommand - scsi_host_template queuecommand entry point
James Smart9bad7672008-12-04 22:39:02 -05004487 * @cmnd: Pointer to scsi_cmnd data structure.
4488 * @done: Pointer to done routine.
4489 *
4490 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
4491 * This routine prepares an IOCB from scsi command and provides to firmware.
4492 * The @done callback is invoked after driver finished processing the command.
4493 *
4494 * Return value :
4495 * 0 - Success
4496 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
4497 **/
dea31012005-04-17 16:05:31 -05004498static int
James Smartb9a7c632012-08-03 12:35:24 -04004499lpfc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004500{
James Smart2e0fef82007-06-17 19:56:36 -05004501 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4502 struct lpfc_hba *phba = vport->phba;
James Smart1ba981f2014-02-20 09:56:45 -05004503 struct lpfc_rport_data *rdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05004504 struct lpfc_nodelist *ndlp;
James Smartc4908502019-01-28 11:14:28 -08004505 struct lpfc_io_buf *lpfc_cmd;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004506 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
James Smart4c47efc2019-01-28 11:14:25 -08004507 int err, idx;
James Smart2fcbc562020-03-22 11:13:02 -07004508#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4509 uint64_t start = 0L;
4510
4511 if (phba->ktime_on)
4512 start = ktime_get_ns();
4513#endif
dea31012005-04-17 16:05:31 -05004514
James Smart1ba981f2014-02-20 09:56:45 -05004515 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smartb0e83012018-06-26 08:24:29 -07004516
4517 /* sanity check on references */
4518 if (unlikely(!rdata) || unlikely(!rport))
4519 goto out_fail_command;
4520
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004521 err = fc_remote_port_chkready(rport);
4522 if (err) {
4523 cmnd->result = err;
dea31012005-04-17 16:05:31 -05004524 goto out_fail_command;
4525 }
James Smart1c6f4ef52009-11-18 15:40:49 -05004526 ndlp = rdata->pnode;
dea31012005-04-17 16:05:31 -05004527
James Smartbf086112011-08-21 21:48:13 -04004528 if ((scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) &&
James Smartacd68592012-01-18 16:25:09 -05004529 (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED))) {
James Smarte2a0a9d2008-12-04 22:40:02 -05004530
James Smart6a9c52c2009-10-02 15:16:51 -04004531 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4532 "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
4533 " op:%02x str=%s without registering for"
4534 " BlockGuard - Rejecting command\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05004535 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
4536 dif_op_str[scsi_get_prot_op(cmnd)]);
4537 goto out_fail_command;
4538 }
4539
dea31012005-04-17 16:05:31 -05004540 /*
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004541 * Catch race where our node has transitioned, but the
4542 * transport is still transitioning.
dea31012005-04-17 16:05:31 -05004543 */
James Smart6b415f52012-06-12 13:54:59 -04004544 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
4545 goto out_tgt_busy;
James Smart2a5b7d62018-07-31 17:23:22 -07004546 if (lpfc_ndlp_check_qdepth(phba, ndlp)) {
4547 if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth) {
4548 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_ERROR,
4549 "3377 Target Queue Full, scsi Id:%d "
4550 "Qdepth:%d Pending command:%d"
4551 " WWNN:%02x:%02x:%02x:%02x:"
4552 "%02x:%02x:%02x:%02x, "
4553 " WWPN:%02x:%02x:%02x:%02x:"
4554 "%02x:%02x:%02x:%02x",
4555 ndlp->nlp_sid, ndlp->cmd_qdepth,
4556 atomic_read(&ndlp->cmd_pending),
4557 ndlp->nlp_nodename.u.wwn[0],
4558 ndlp->nlp_nodename.u.wwn[1],
4559 ndlp->nlp_nodename.u.wwn[2],
4560 ndlp->nlp_nodename.u.wwn[3],
4561 ndlp->nlp_nodename.u.wwn[4],
4562 ndlp->nlp_nodename.u.wwn[5],
4563 ndlp->nlp_nodename.u.wwn[6],
4564 ndlp->nlp_nodename.u.wwn[7],
4565 ndlp->nlp_portname.u.wwn[0],
4566 ndlp->nlp_portname.u.wwn[1],
4567 ndlp->nlp_portname.u.wwn[2],
4568 ndlp->nlp_portname.u.wwn[3],
4569 ndlp->nlp_portname.u.wwn[4],
4570 ndlp->nlp_portname.u.wwn[5],
4571 ndlp->nlp_portname.u.wwn[6],
4572 ndlp->nlp_portname.u.wwn[7]);
4573 goto out_tgt_busy;
4574 }
James Smart64bf0092018-01-30 15:58:53 -08004575 }
James Smartf91bc592018-04-09 14:24:22 -07004576
James Smartace44e42019-01-28 11:14:27 -08004577 lpfc_cmd = lpfc_get_scsi_buf(phba, ndlp, cmnd);
dea31012005-04-17 16:05:31 -05004578 if (lpfc_cmd == NULL) {
James Smarteaf15d52008-12-04 22:39:29 -05004579 lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05004580
James Smart895427b2017-02-12 13:52:30 -08004581 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_ERROR,
James Smarte8b62012007-08-02 11:10:09 -04004582 "0707 driver's buffer pool is empty, "
4583 "IO busied\n");
dea31012005-04-17 16:05:31 -05004584 goto out_host_busy;
4585 }
4586
4587 /*
4588 * Store the midlayer's command structure for the completion phase
4589 * and complete the command initialization.
4590 */
4591 lpfc_cmd->pCmd = cmnd;
4592 lpfc_cmd->rdata = rdata;
James Smart2a5b7d62018-07-31 17:23:22 -07004593 lpfc_cmd->ndlp = ndlp;
dea31012005-04-17 16:05:31 -05004594 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
dea31012005-04-17 16:05:31 -05004595
James Smarte2a0a9d2008-12-04 22:40:02 -05004596 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
James Smart6a9c52c2009-10-02 15:16:51 -04004597 if (vport->phba->cfg_enable_bg) {
James Smart737d4242013-04-17 20:14:49 -04004598 lpfc_printf_vlog(vport,
4599 KERN_INFO, LOG_SCSI_CMD,
James Smart26134702012-08-14 14:25:50 -04004600 "9033 BLKGRD: rcvd %s cmd:x%x "
4601 "sector x%llx cnt %u pt %x\n",
4602 dif_op_str[scsi_get_prot_op(cmnd)],
4603 cmnd->cmnd[0],
4604 (unsigned long long)scsi_get_lba(cmnd),
4605 blk_rq_sectors(cmnd->request),
4606 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004607 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004608 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
4609 } else {
James Smart6a9c52c2009-10-02 15:16:51 -04004610 if (vport->phba->cfg_enable_bg) {
James Smart737d4242013-04-17 20:14:49 -04004611 lpfc_printf_vlog(vport,
4612 KERN_INFO, LOG_SCSI_CMD,
James Smart26134702012-08-14 14:25:50 -04004613 "9038 BLKGRD: rcvd PROT_NORMAL cmd: "
4614 "x%x sector x%llx cnt %u pt %x\n",
4615 cmnd->cmnd[0],
4616 (unsigned long long)scsi_get_lba(cmnd),
James Smart9a6b09c2012-03-01 22:37:42 -05004617 blk_rq_sectors(cmnd->request),
James Smart26134702012-08-14 14:25:50 -04004618 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004619 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004620 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
4621 }
4622
James Smart22770cb2019-10-18 14:18:24 -07004623 if (unlikely(err)) {
4624 if (err == 2) {
4625 cmnd->result = DID_ERROR << 16;
4626 goto out_fail_command_release_buf;
4627 }
dea31012005-04-17 16:05:31 -05004628 goto out_host_busy_free_buf;
James Smart5e0e2312019-08-14 16:56:54 -07004629 }
dea31012005-04-17 16:05:31 -05004630
James Smart2e0fef82007-06-17 19:56:36 -05004631 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea31012005-04-17 16:05:31 -05004632
James Smart6a828b02019-01-28 11:14:31 -08004633#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart840eda92020-03-22 11:13:00 -07004634 if (unlikely(phba->hdwqstat_on & LPFC_CHECK_SCSI_IO))
4635 this_cpu_inc(phba->sli4_hba.c_stat->xmt_io);
James Smart6a828b02019-01-28 11:14:31 -08004636#endif
James Smart3772a992009-05-22 14:50:54 -04004637 err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
James Smart92d7f7b2007-06-17 19:56:38 -05004638 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
James Smart2fcbc562020-03-22 11:13:02 -07004639#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4640 if (start) {
4641 lpfc_cmd->ts_cmd_start = start;
4642 lpfc_cmd->ts_last_cmd = phba->ktime_last_cmd;
4643 lpfc_cmd->ts_cmd_wqput = ktime_get_ns();
4644 } else {
4645 lpfc_cmd->ts_cmd_start = 0;
4646 }
4647#endif
James Smarteaf15d52008-12-04 22:39:29 -05004648 if (err) {
James Smart76f96b62013-12-17 20:29:01 -05004649 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4650 "3376 FCP could not issue IOCB err %x"
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004651 "FCP cmd x%x <%d/%llu> "
James Smart76f96b62013-12-17 20:29:01 -05004652 "sid: x%x did: x%x oxid: x%x "
4653 "Data: x%x x%x x%x x%x\n",
4654 err, cmnd->cmnd[0],
4655 cmnd->device ? cmnd->device->id : 0xffff,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004656 cmnd->device ? cmnd->device->lun : (u64) -1,
James Smart76f96b62013-12-17 20:29:01 -05004657 vport->fc_myDID, ndlp->nlp_DID,
4658 phba->sli_rev == LPFC_SLI_REV4 ?
4659 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
4660 lpfc_cmd->cur_iocbq.iocb.ulpContext,
4661 lpfc_cmd->cur_iocbq.iocb.ulpIoTag,
4662 lpfc_cmd->cur_iocbq.iocb.ulpTimeout,
4663 (uint32_t)
4664 (cmnd->request->timeout / 1000));
4665
dea31012005-04-17 16:05:31 -05004666 goto out_host_busy_free_buf;
James Smarteaf15d52008-12-04 22:39:29 -05004667 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004668 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004669 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -08004670 &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
James Smart45ed1192009-10-02 15:17:02 -04004671
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004672 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4673 lpfc_poll_rearm_timer(phba);
4674 }
4675
James Smartc4908502019-01-28 11:14:28 -08004676 if (phba->cfg_xri_rebalancing)
4677 lpfc_keep_pvt_pool_above_lowwm(phba, lpfc_cmd->hdwq_no);
4678
dea31012005-04-17 16:05:31 -05004679 return 0;
4680
4681 out_host_busy_free_buf:
James Smart1fbf9742019-01-28 11:14:26 -08004682 idx = lpfc_cmd->hdwq_no;
James Smartbcf4dbf2006-07-06 15:50:08 -04004683 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James Smart4c47efc2019-01-28 11:14:25 -08004684 if (phba->sli4_hba.hdwq) {
4685 switch (lpfc_cmd->fcp_cmnd->fcpCntl3) {
4686 case WRITE_DATA:
4687 phba->sli4_hba.hdwq[idx].scsi_cstat.output_requests--;
4688 break;
4689 case READ_DATA:
4690 phba->sli4_hba.hdwq[idx].scsi_cstat.input_requests--;
4691 break;
4692 default:
4693 phba->sli4_hba.hdwq[idx].scsi_cstat.control_requests--;
4694 }
4695 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004696 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004697 out_host_busy:
4698 return SCSI_MLQUEUE_HOST_BUSY;
4699
Mike Christie34963432011-02-25 14:04:28 -06004700 out_tgt_busy:
4701 return SCSI_MLQUEUE_TARGET_BUSY;
4702
James Smart5e0e2312019-08-14 16:56:54 -07004703 out_fail_command_release_buf:
4704 lpfc_release_scsi_buf(phba, lpfc_cmd);
4705
dea31012005-04-17 16:05:31 -05004706 out_fail_command:
James Smartb9a7c632012-08-03 12:35:24 -04004707 cmnd->scsi_done(cmnd);
dea31012005-04-17 16:05:31 -05004708 return 0;
4709}
4710
Jeff Garzikf2812332010-11-16 02:10:29 -05004711
James Smart9bad7672008-12-04 22:39:02 -05004712/**
James Smart3621a712009-04-06 18:47:14 -04004713 * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05004714 * @cmnd: Pointer to scsi_cmnd data structure.
4715 *
4716 * This routine aborts @cmnd pending in base driver.
4717 *
4718 * Return code :
4719 * 0x2003 - Error
4720 * 0x2002 - Success
4721 **/
dea31012005-04-17 16:05:31 -05004722static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05004723lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004724{
James Smart2e0fef82007-06-17 19:56:36 -05004725 struct Scsi_Host *shost = cmnd->device->host;
4726 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4727 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004728 struct lpfc_iocbq *iocb;
4729 struct lpfc_iocbq *abtsiocb;
James Smartc4908502019-01-28 11:14:28 -08004730 struct lpfc_io_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05004731 IOCB_t *cmd, *icmd;
James Smart3a707302012-06-12 13:54:42 -04004732 int ret = SUCCESS, status = 0;
James Smart8931c732018-07-31 17:23:20 -07004733 struct lpfc_sli_ring *pring_s4 = NULL;
James Smart895427b2017-02-12 13:52:30 -08004734 int ret_val;
James Smart59c68ea2018-04-09 14:24:25 -07004735 unsigned long flags;
James Smartfa61a542008-01-11 01:52:42 -05004736 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004737
James Smart3a707302012-06-12 13:54:42 -04004738 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04004739 if (status != 0 && status != SUCCESS)
James Smart3a707302012-06-12 13:54:42 -04004740 return status;
James Smart4f2e66c2012-05-09 21:17:07 -04004741
James Smartc2017262019-01-28 11:14:37 -08004742 lpfc_cmd = (struct lpfc_io_buf *)cmnd->host_scribble;
4743 if (!lpfc_cmd)
4744 return ret;
4745
James Smart876dd7d2012-09-29 11:31:28 -04004746 spin_lock_irqsave(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004747 /* driver queued commands are in process of being flushed */
James Smartc00f62e2019-08-14 16:57:11 -07004748 if (phba->hba_flag & HBA_IOQ_FLUSH) {
James Smart4f2e66c2012-05-09 21:17:07 -04004749 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4750 "3168 SCSI Layer abort requested I/O has been "
4751 "flushed by LLD.\n");
James Smartc2017262019-01-28 11:14:37 -08004752 ret = FAILED;
4753 goto out_unlock;
James Smart4f2e66c2012-05-09 21:17:07 -04004754 }
4755
James Smartc2017262019-01-28 11:14:37 -08004756 /* Guard against IO completion being called at same time */
4757 spin_lock(&lpfc_cmd->buf_lock);
4758
4759 if (!lpfc_cmd->pCmd) {
James Smarteee88772010-09-29 11:19:08 -04004760 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4761 "2873 SCSI Layer I/O Abort Request IO CMPL Status "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004762 "x%x ID %d LUN %llu\n",
James Smart3a707302012-06-12 13:54:42 -04004763 SUCCESS, cmnd->device->id, cmnd->device->lun);
James Smartc2017262019-01-28 11:14:37 -08004764 goto out_unlock_buf;
James Smarteee88772010-09-29 11:19:08 -04004765 }
dea31012005-04-17 16:05:31 -05004766
James Smart4f2e66c2012-05-09 21:17:07 -04004767 iocb = &lpfc_cmd->cur_iocbq;
James Smart8931c732018-07-31 17:23:20 -07004768 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartc00f62e2019-08-14 16:57:11 -07004769 pring_s4 = phba->sli4_hba.hdwq[iocb->hba_wqidx].io_wq->pring;
James Smart8931c732018-07-31 17:23:20 -07004770 if (!pring_s4) {
4771 ret = FAILED;
James Smartc2017262019-01-28 11:14:37 -08004772 goto out_unlock_buf;
James Smart8931c732018-07-31 17:23:20 -07004773 }
4774 spin_lock(&pring_s4->ring_lock);
4775 }
James Smart4f2e66c2012-05-09 21:17:07 -04004776 /* the command is in process of being cancelled */
4777 if (!(iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
James Smart4f2e66c2012-05-09 21:17:07 -04004778 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4779 "3169 SCSI Layer abort requested I/O has been "
4780 "cancelled by LLD.\n");
James Smartc2017262019-01-28 11:14:37 -08004781 ret = FAILED;
4782 goto out_unlock_ring;
James Smart4f2e66c2012-05-09 21:17:07 -04004783 }
dea31012005-04-17 16:05:31 -05004784 /*
James Smartc4908502019-01-28 11:14:28 -08004785 * If pCmd field of the corresponding lpfc_io_buf structure
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004786 * points to a different SCSI command, then the driver has
4787 * already completed this command, but the midlayer did not
James Smart4f2e66c2012-05-09 21:17:07 -04004788 * see the completion before the eh fired. Just return SUCCESS.
dea31012005-04-17 16:05:31 -05004789 */
James Smart4f2e66c2012-05-09 21:17:07 -04004790 if (lpfc_cmd->pCmd != cmnd) {
4791 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4792 "3170 SCSI Layer abort requested I/O has been "
4793 "completed by LLD.\n");
James Smartc2017262019-01-28 11:14:37 -08004794 goto out_unlock_ring;
James Smart4f2e66c2012-05-09 21:17:07 -04004795 }
dea31012005-04-17 16:05:31 -05004796
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004797 BUG_ON(iocb->context1 != lpfc_cmd);
dea31012005-04-17 16:05:31 -05004798
James Smartee620212014-04-04 13:51:53 -04004799 /* abort issued in recovery is still in progress */
4800 if (iocb->iocb_flag & LPFC_DRIVER_ABORTED) {
4801 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4802 "3389 SCSI Layer I/O Abort Request is pending\n");
James Smart8931c732018-07-31 17:23:20 -07004803 if (phba->sli_rev == LPFC_SLI_REV4)
4804 spin_unlock(&pring_s4->ring_lock);
James Smartc2017262019-01-28 11:14:37 -08004805 spin_unlock(&lpfc_cmd->buf_lock);
James Smartee620212014-04-04 13:51:53 -04004806 spin_unlock_irqrestore(&phba->hbalock, flags);
4807 goto wait_for_cmpl;
4808 }
4809
James Smart4f2e66c2012-05-09 21:17:07 -04004810 abtsiocb = __lpfc_sli_get_iocbq(phba);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004811 if (abtsiocb == NULL) {
4812 ret = FAILED;
James Smartc2017262019-01-28 11:14:37 -08004813 goto out_unlock_ring;
dea31012005-04-17 16:05:31 -05004814 }
4815
James Smartafbd8d82013-09-06 12:22:13 -04004816 /* Indicate the IO is being aborted by the driver. */
4817 iocb->iocb_flag |= LPFC_DRIVER_ABORTED;
4818
dea31012005-04-17 16:05:31 -05004819 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004820 * The scsi command can not be in txq and it is in flight because the
4821 * pCmd is still pointig at the SCSI command we have to abort. There
4822 * is no need to search the txcmplq. Just send an abort to the FW.
dea31012005-04-17 16:05:31 -05004823 */
dea31012005-04-17 16:05:31 -05004824
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004825 cmd = &iocb->iocb;
4826 icmd = &abtsiocb->iocb;
4827 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
4828 icmd->un.acxri.abortContextTag = cmd->ulpContext;
James Smart3772a992009-05-22 14:50:54 -04004829 if (phba->sli_rev == LPFC_SLI_REV4)
4830 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
4831 else
4832 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05004833
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004834 icmd->ulpLe = 1;
4835 icmd->ulpClass = cmd->ulpClass;
James Smart5ffc2662009-11-18 15:39:44 -05004836
4837 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -08004838 abtsiocb->hba_wqidx = iocb->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -05004839 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -04004840 if (iocb->iocb_flag & LPFC_IO_FOF)
4841 abtsiocb->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -05004842
James Smart2e0fef82007-06-17 19:56:36 -05004843 if (lpfc_is_link_up(phba))
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004844 icmd->ulpCommand = CMD_ABORT_XRI_CN;
4845 else
4846 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea31012005-04-17 16:05:31 -05004847
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004848 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smart2e0fef82007-06-17 19:56:36 -05004849 abtsiocb->vport = vport;
James Smart8931c732018-07-31 17:23:20 -07004850 lpfc_cmd->waitq = &waitq;
James Smart98912dda2014-04-04 13:52:31 -04004851 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart98912dda2014-04-04 13:52:31 -04004852 /* Note: both hbalock and ring_lock must be set here */
James Smart98912dda2014-04-04 13:52:31 -04004853 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
4854 abtsiocb, 0);
James Smart59c68ea2018-04-09 14:24:25 -07004855 spin_unlock(&pring_s4->ring_lock);
James Smart98912dda2014-04-04 13:52:31 -04004856 } else {
4857 ret_val = __lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
4858 abtsiocb, 0);
4859 }
James Smart4f2e66c2012-05-09 21:17:07 -04004860
James Smart98912dda2014-04-04 13:52:31 -04004861 if (ret_val == IOCB_ERROR) {
James Smart8931c732018-07-31 17:23:20 -07004862 /* Indicate the IO is not being aborted by the driver. */
4863 iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
4864 lpfc_cmd->waitq = NULL;
James Smartc2017262019-01-28 11:14:37 -08004865 spin_unlock(&lpfc_cmd->buf_lock);
James Smart91a52b62019-10-18 14:18:22 -07004866 spin_unlock_irqrestore(&phba->hbalock, flags);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004867 lpfc_sli_release_iocbq(phba, abtsiocb);
4868 ret = FAILED;
4869 goto out;
4870 }
4871
James Smart91a52b62019-10-18 14:18:22 -07004872 /* no longer need the lock after this point */
James Smartc2017262019-01-28 11:14:37 -08004873 spin_unlock(&lpfc_cmd->buf_lock);
James Smart91a52b62019-10-18 14:18:22 -07004874 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartc2017262019-01-28 11:14:37 -08004875
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004876 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
James Smart45ed1192009-10-02 15:17:02 -04004877 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -08004878 &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004879
James Smartee620212014-04-04 13:51:53 -04004880wait_for_cmpl:
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004881 /* Wait for abort to complete */
James Smartfa61a542008-01-11 01:52:42 -05004882 wait_event_timeout(waitq,
4883 (lpfc_cmd->pCmd != cmnd),
James Smart256ec0d2013-04-17 20:14:58 -04004884 msecs_to_jiffies(2*vport->cfg_devloss_tmo*1000));
James Smartee620212014-04-04 13:51:53 -04004885
James Smartc2017262019-01-28 11:14:37 -08004886 spin_lock(&lpfc_cmd->buf_lock);
dea31012005-04-17 16:05:31 -05004887
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004888 if (lpfc_cmd->pCmd == cmnd) {
4889 ret = FAILED;
James Smarte8b62012007-08-02 11:10:09 -04004890 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4891 "0748 abort handler timed out waiting "
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -07004892 "for aborting I/O (xri:x%x) to complete: "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004893 "ret %#x, ID %d, LUN %llu\n",
James Smart247ca942012-08-14 14:26:13 -04004894 iocb->sli4_xritag, ret,
4895 cmnd->device->id, cmnd->device->lun);
dea31012005-04-17 16:05:31 -05004896 }
James Smartb9e5a2d2019-05-21 17:49:01 -07004897
4898 lpfc_cmd->waitq = NULL;
4899
James Smartc2017262019-01-28 11:14:37 -08004900 spin_unlock(&lpfc_cmd->buf_lock);
James Smart4f2e66c2012-05-09 21:17:07 -04004901 goto out;
dea31012005-04-17 16:05:31 -05004902
James Smartc2017262019-01-28 11:14:37 -08004903out_unlock_ring:
4904 if (phba->sli_rev == LPFC_SLI_REV4)
4905 spin_unlock(&pring_s4->ring_lock);
4906out_unlock_buf:
4907 spin_unlock(&lpfc_cmd->buf_lock);
James Smart4f2e66c2012-05-09 21:17:07 -04004908out_unlock:
James Smart876dd7d2012-09-29 11:31:28 -04004909 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004910out:
James Smarte8b62012007-08-02 11:10:09 -04004911 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4912 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004913 "LUN %llu\n", ret, cmnd->device->id,
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04004914 cmnd->device->lun);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004915 return ret;
dea31012005-04-17 16:05:31 -05004916}
4917
James Smartbbb9d182009-06-10 17:23:16 -04004918static char *
4919lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
4920{
4921 switch (task_mgmt_cmd) {
4922 case FCP_ABORT_TASK_SET:
4923 return "ABORT_TASK_SET";
4924 case FCP_CLEAR_TASK_SET:
4925 return "FCP_CLEAR_TASK_SET";
4926 case FCP_BUS_RESET:
4927 return "FCP_BUS_RESET";
4928 case FCP_LUN_RESET:
4929 return "FCP_LUN_RESET";
4930 case FCP_TARGET_RESET:
4931 return "FCP_TARGET_RESET";
4932 case FCP_CLEAR_ACA:
4933 return "FCP_CLEAR_ACA";
4934 case FCP_TERMINATE_TASK:
4935 return "FCP_TERMINATE_TASK";
4936 default:
4937 return "unknown";
4938 }
4939}
4940
James Smart53151bb2013-10-10 12:24:07 -04004941
4942/**
4943 * lpfc_check_fcp_rsp - check the returned fcp_rsp to see if task failed
4944 * @vport: The virtual port for which this call is being executed.
James Smartc4908502019-01-28 11:14:28 -08004945 * @lpfc_cmd: Pointer to lpfc_io_buf data structure.
James Smart53151bb2013-10-10 12:24:07 -04004946 *
4947 * This routine checks the FCP RSP INFO to see if the tsk mgmt command succeded
4948 *
4949 * Return code :
4950 * 0x2003 - Error
4951 * 0x2002 - Success
4952 **/
4953static int
James Smartc4908502019-01-28 11:14:28 -08004954lpfc_check_fcp_rsp(struct lpfc_vport *vport, struct lpfc_io_buf *lpfc_cmd)
James Smart53151bb2013-10-10 12:24:07 -04004955{
4956 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
4957 uint32_t rsp_info;
4958 uint32_t rsp_len;
4959 uint8_t rsp_info_code;
4960 int ret = FAILED;
4961
4962
4963 if (fcprsp == NULL)
4964 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4965 "0703 fcp_rsp is missing\n");
4966 else {
4967 rsp_info = fcprsp->rspStatus2;
4968 rsp_len = be32_to_cpu(fcprsp->rspRspLen);
4969 rsp_info_code = fcprsp->rspInfo3;
4970
4971
4972 lpfc_printf_vlog(vport, KERN_INFO,
4973 LOG_FCP,
4974 "0706 fcp_rsp valid 0x%x,"
4975 " rsp len=%d code 0x%x\n",
4976 rsp_info,
4977 rsp_len, rsp_info_code);
4978
James Smart996a02a2019-05-21 17:49:07 -07004979 /* If FCP_RSP_LEN_VALID bit is one, then the FCP_RSP_LEN
4980 * field specifies the number of valid bytes of FCP_RSP_INFO.
4981 * The FCP_RSP_LEN field shall be set to 0x04 or 0x08
4982 */
4983 if ((fcprsp->rspStatus2 & RSP_LEN_VALID) &&
4984 ((rsp_len == 8) || (rsp_len == 4))) {
James Smart53151bb2013-10-10 12:24:07 -04004985 switch (rsp_info_code) {
4986 case RSP_NO_FAILURE:
4987 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4988 "0715 Task Mgmt No Failure\n");
4989 ret = SUCCESS;
4990 break;
4991 case RSP_TM_NOT_SUPPORTED: /* TM rejected */
4992 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4993 "0716 Task Mgmt Target "
4994 "reject\n");
4995 break;
4996 case RSP_TM_NOT_COMPLETED: /* TM failed */
4997 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4998 "0717 Task Mgmt Target "
4999 "failed TM\n");
5000 break;
5001 case RSP_TM_INVALID_LU: /* TM to invalid LU! */
5002 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5003 "0718 Task Mgmt to invalid "
5004 "LUN\n");
5005 break;
5006 }
5007 }
5008 }
5009 return ret;
5010}
5011
5012
James Smartbbb9d182009-06-10 17:23:16 -04005013/**
5014 * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
5015 * @vport: The virtual port for which this call is being executed.
5016 * @rdata: Pointer to remote port local data
5017 * @tgt_id: Target ID of remote device.
5018 * @lun_id: Lun number for the TMF
5019 * @task_mgmt_cmd: type of TMF to send
5020 *
5021 * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
5022 * a remote port.
5023 *
5024 * Return Code:
5025 * 0x2003 - Error
5026 * 0x2002 - Success.
5027 **/
5028static int
James Smarteed695d2016-10-13 15:06:04 -07005029lpfc_send_taskmgmt(struct lpfc_vport *vport, struct scsi_cmnd *cmnd,
5030 unsigned int tgt_id, uint64_t lun_id,
5031 uint8_t task_mgmt_cmd)
James Smartbbb9d182009-06-10 17:23:16 -04005032{
5033 struct lpfc_hba *phba = vport->phba;
James Smartc4908502019-01-28 11:14:28 -08005034 struct lpfc_io_buf *lpfc_cmd;
James Smartbbb9d182009-06-10 17:23:16 -04005035 struct lpfc_iocbq *iocbq;
5036 struct lpfc_iocbq *iocbqrsp;
James Smarteed695d2016-10-13 15:06:04 -07005037 struct lpfc_rport_data *rdata;
5038 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005039 int ret;
5040 int status;
5041
James Smarteed695d2016-10-13 15:06:04 -07005042 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
5043 if (!rdata || !rdata->pnode || !NLP_CHK_NODE_ACT(rdata->pnode))
James Smartbbb9d182009-06-10 17:23:16 -04005044 return FAILED;
James Smarteed695d2016-10-13 15:06:04 -07005045 pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005046
James Smartace44e42019-01-28 11:14:27 -08005047 lpfc_cmd = lpfc_get_scsi_buf(phba, pnode, NULL);
James Smartbbb9d182009-06-10 17:23:16 -04005048 if (lpfc_cmd == NULL)
5049 return FAILED;
James Smart0c411222013-09-06 12:22:46 -04005050 lpfc_cmd->timeout = phba->cfg_task_mgmt_tmo;
James Smartbbb9d182009-06-10 17:23:16 -04005051 lpfc_cmd->rdata = rdata;
James Smarteed695d2016-10-13 15:06:04 -07005052 lpfc_cmd->pCmd = cmnd;
James Smart2a5b7d62018-07-31 17:23:22 -07005053 lpfc_cmd->ndlp = pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005054
5055 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
5056 task_mgmt_cmd);
5057 if (!status) {
5058 lpfc_release_scsi_buf(phba, lpfc_cmd);
5059 return FAILED;
5060 }
5061
5062 iocbq = &lpfc_cmd->cur_iocbq;
5063 iocbqrsp = lpfc_sli_get_iocbq(phba);
5064 if (iocbqrsp == NULL) {
5065 lpfc_release_scsi_buf(phba, lpfc_cmd);
5066 return FAILED;
5067 }
James Smart5a0916b2013-07-15 18:31:42 -04005068 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
James Smartbbb9d182009-06-10 17:23:16 -04005069
5070 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005071 "0702 Issue %s to TGT %d LUN %llu "
James Smart6d368e52011-05-24 11:44:12 -04005072 "rpi x%x nlp_flag x%x Data: x%x x%x\n",
James Smartbbb9d182009-06-10 17:23:16 -04005073 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
James Smart6d368e52011-05-24 11:44:12 -04005074 pnode->nlp_rpi, pnode->nlp_flag, iocbq->sli4_xritag,
5075 iocbq->iocb_flag);
James Smartbbb9d182009-06-10 17:23:16 -04005076
5077 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
5078 iocbq, iocbqrsp, lpfc_cmd->timeout);
James Smart53151bb2013-10-10 12:24:07 -04005079 if ((status != IOCB_SUCCESS) ||
5080 (iocbqrsp->iocb.ulpStatus != IOSTAT_SUCCESS)) {
James Smartae374a32015-05-22 10:42:41 -04005081 if (status != IOCB_SUCCESS ||
5082 iocbqrsp->iocb.ulpStatus != IOSTAT_FCP_RSP_ERROR)
5083 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5084 "0727 TMF %s to TGT %d LUN %llu "
5085 "failed (%d, %d) iocb_flag x%x\n",
5086 lpfc_taskmgmt_name(task_mgmt_cmd),
5087 tgt_id, lun_id,
5088 iocbqrsp->iocb.ulpStatus,
5089 iocbqrsp->iocb.un.ulpWord[4],
5090 iocbq->iocb_flag);
James Smart53151bb2013-10-10 12:24:07 -04005091 /* if ulpStatus != IOCB_SUCCESS, then status == IOCB_SUCCESS */
5092 if (status == IOCB_SUCCESS) {
5093 if (iocbqrsp->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
5094 /* Something in the FCP_RSP was invalid.
5095 * Check conditions */
5096 ret = lpfc_check_fcp_rsp(vport, lpfc_cmd);
5097 else
5098 ret = FAILED;
5099 } else if (status == IOCB_TIMEDOUT) {
5100 ret = TIMEOUT_ERROR;
5101 } else {
5102 ret = FAILED;
5103 }
James Smart53151bb2013-10-10 12:24:07 -04005104 } else
James Smartbbb9d182009-06-10 17:23:16 -04005105 ret = SUCCESS;
5106
5107 lpfc_sli_release_iocbq(phba, iocbqrsp);
5108
5109 if (ret != TIMEOUT_ERROR)
5110 lpfc_release_scsi_buf(phba, lpfc_cmd);
5111
5112 return ret;
5113}
5114
5115/**
5116 * lpfc_chk_tgt_mapped -
5117 * @vport: The virtual port to check on
5118 * @cmnd: Pointer to scsi_cmnd data structure.
5119 *
5120 * This routine delays until the scsi target (aka rport) for the
5121 * command exists (is present and logged in) or we declare it non-existent.
5122 *
5123 * Return code :
5124 * 0x2003 - Error
5125 * 0x2002 - Success
5126 **/
5127static int
5128lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
5129{
James Smart1ba981f2014-02-20 09:56:45 -05005130 struct lpfc_rport_data *rdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005131 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005132 unsigned long later;
5133
James Smart1ba981f2014-02-20 09:56:45 -05005134 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smart1c6f4ef52009-11-18 15:40:49 -05005135 if (!rdata) {
5136 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smart32350662019-08-14 16:57:06 -07005137 "0797 Tgt Map rport failure: rdata x%px\n", rdata);
James Smart1c6f4ef52009-11-18 15:40:49 -05005138 return FAILED;
5139 }
5140 pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005141 /*
5142 * If target is not in a MAPPED state, delay until
5143 * target is rediscovered or devloss timeout expires.
5144 */
5145 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
5146 while (time_after(later, jiffies)) {
5147 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
5148 return FAILED;
5149 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
5150 return SUCCESS;
5151 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
James Smart1ba981f2014-02-20 09:56:45 -05005152 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smartbbb9d182009-06-10 17:23:16 -04005153 if (!rdata)
5154 return FAILED;
5155 pnode = rdata->pnode;
5156 }
5157 if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
5158 (pnode->nlp_state != NLP_STE_MAPPED_NODE))
5159 return FAILED;
5160 return SUCCESS;
5161}
5162
5163/**
5164 * lpfc_reset_flush_io_context -
5165 * @vport: The virtual port (scsi_host) for the flush context
5166 * @tgt_id: If aborting by Target contect - specifies the target id
5167 * @lun_id: If aborting by Lun context - specifies the lun id
5168 * @context: specifies the context level to flush at.
5169 *
5170 * After a reset condition via TMF, we need to flush orphaned i/o
5171 * contexts from the adapter. This routine aborts any contexts
5172 * outstanding, then waits for their completions. The wait is
5173 * bounded by devloss_tmo though.
5174 *
5175 * Return code :
5176 * 0x2003 - Error
5177 * 0x2002 - Success
5178 **/
5179static int
5180lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
5181 uint64_t lun_id, lpfc_ctx_cmd context)
5182{
5183 struct lpfc_hba *phba = vport->phba;
5184 unsigned long later;
5185 int cnt;
5186
5187 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
5188 if (cnt)
James Smart98912dda2014-04-04 13:52:31 -04005189 lpfc_sli_abort_taskmgmt(vport,
James Smart895427b2017-02-12 13:52:30 -08005190 &phba->sli.sli3_ring[LPFC_FCP_RING],
James Smart98912dda2014-04-04 13:52:31 -04005191 tgt_id, lun_id, context);
James Smartbbb9d182009-06-10 17:23:16 -04005192 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
5193 while (time_after(later, jiffies) && cnt) {
5194 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
5195 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
5196 }
5197 if (cnt) {
5198 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5199 "0724 I/O flush failure for context %s : cnt x%x\n",
5200 ((context == LPFC_CTX_LUN) ? "LUN" :
5201 ((context == LPFC_CTX_TGT) ? "TGT" :
5202 ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
5203 cnt);
5204 return FAILED;
5205 }
5206 return SUCCESS;
5207}
5208
James Smart9bad7672008-12-04 22:39:02 -05005209/**
James Smart3621a712009-04-06 18:47:14 -04005210 * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
James Smart9bad7672008-12-04 22:39:02 -05005211 * @cmnd: Pointer to scsi_cmnd data structure.
5212 *
James Smartbbb9d182009-06-10 17:23:16 -04005213 * This routine does a device reset by sending a LUN_RESET task management
James Smart9bad7672008-12-04 22:39:02 -05005214 * command.
5215 *
5216 * Return code :
5217 * 0x2003 - Error
James Smart3621a712009-04-06 18:47:14 -04005218 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05005219 **/
dea31012005-04-17 16:05:31 -05005220static int
James Smart7054a602007-04-25 09:52:34 -04005221lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05005222{
James Smart2e0fef82007-06-17 19:56:36 -05005223 struct Scsi_Host *shost = cmnd->device->host;
5224 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smart1ba981f2014-02-20 09:56:45 -05005225 struct lpfc_rport_data *rdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005226 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005227 unsigned tgt_id = cmnd->device->id;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005228 uint64_t lun_id = cmnd->device->lun;
James Smartea2151b2008-09-07 11:52:10 -04005229 struct lpfc_scsi_event_header scsi_event;
James Smart53151bb2013-10-10 12:24:07 -04005230 int status;
dea31012005-04-17 16:05:31 -05005231
James Smart1ba981f2014-02-20 09:56:45 -05005232 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smartad490b62015-04-07 15:07:26 -04005233 if (!rdata || !rdata->pnode) {
James Smart1c6f4ef52009-11-18 15:40:49 -05005234 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smart32350662019-08-14 16:57:06 -07005235 "0798 Device Reset rdata failure: rdata x%px\n",
James Smartad490b62015-04-07 15:07:26 -04005236 rdata);
James Smart1c6f4ef52009-11-18 15:40:49 -05005237 return FAILED;
5238 }
5239 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04005240 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005241 if (status != 0 && status != SUCCESS)
James Smart589a52d2010-07-14 15:30:54 -04005242 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005243
5244 status = lpfc_chk_tgt_mapped(vport, cmnd);
5245 if (status == FAILED) {
5246 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smart32350662019-08-14 16:57:06 -07005247 "0721 Device Reset rport failure: rdata x%px\n", rdata);
James Smartbbb9d182009-06-10 17:23:16 -04005248 return FAILED;
5249 }
5250
5251 scsi_event.event_type = FC_REG_SCSI_EVENT;
5252 scsi_event.subcategory = LPFC_EVENT_LUNRESET;
5253 scsi_event.lun = lun_id;
5254 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5255 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5256
5257 fc_host_post_vendor_event(shost, fc_get_event_number(),
5258 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
5259
James Smarteed695d2016-10-13 15:06:04 -07005260 status = lpfc_send_taskmgmt(vport, cmnd, tgt_id, lun_id,
James Smartbbb9d182009-06-10 17:23:16 -04005261 FCP_LUN_RESET);
5262
5263 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005264 "0713 SCSI layer issued Device Reset (%d, %llu) "
James Smartbbb9d182009-06-10 17:23:16 -04005265 "return x%x\n", tgt_id, lun_id, status);
5266
dea31012005-04-17 16:05:31 -05005267 /*
James Smartbbb9d182009-06-10 17:23:16 -04005268 * We have to clean up i/o as : they may be orphaned by the TMF;
5269 * or if the TMF failed, they may be in an indeterminate state.
5270 * So, continue on.
5271 * We will report success if all the i/o aborts successfully.
dea31012005-04-17 16:05:31 -05005272 */
James Smart53151bb2013-10-10 12:24:07 -04005273 if (status == SUCCESS)
5274 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
James Smartbbb9d182009-06-10 17:23:16 -04005275 LPFC_CTX_LUN);
James Smart53151bb2013-10-10 12:24:07 -04005276
5277 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005278}
5279
5280/**
5281 * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
5282 * @cmnd: Pointer to scsi_cmnd data structure.
5283 *
5284 * This routine does a target reset by sending a TARGET_RESET task management
5285 * command.
5286 *
5287 * Return code :
5288 * 0x2003 - Error
5289 * 0x2002 - Success
5290 **/
5291static int
5292lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
5293{
5294 struct Scsi_Host *shost = cmnd->device->host;
5295 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smart1ba981f2014-02-20 09:56:45 -05005296 struct lpfc_rport_data *rdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005297 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005298 unsigned tgt_id = cmnd->device->id;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005299 uint64_t lun_id = cmnd->device->lun;
James Smartbbb9d182009-06-10 17:23:16 -04005300 struct lpfc_scsi_event_header scsi_event;
James Smart53151bb2013-10-10 12:24:07 -04005301 int status;
James Smartbbb9d182009-06-10 17:23:16 -04005302
James Smart1ba981f2014-02-20 09:56:45 -05005303 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smartbbd3d732019-03-12 16:30:17 -07005304 if (!rdata || !rdata->pnode) {
James Smart1c6f4ef52009-11-18 15:40:49 -05005305 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smart32350662019-08-14 16:57:06 -07005306 "0799 Target Reset rdata failure: rdata x%px\n",
James Smartbbd3d732019-03-12 16:30:17 -07005307 rdata);
James Smart1c6f4ef52009-11-18 15:40:49 -05005308 return FAILED;
5309 }
5310 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04005311 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005312 if (status != 0 && status != SUCCESS)
James Smart589a52d2010-07-14 15:30:54 -04005313 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005314
5315 status = lpfc_chk_tgt_mapped(vport, cmnd);
5316 if (status == FAILED) {
5317 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smart32350662019-08-14 16:57:06 -07005318 "0722 Target Reset rport failure: rdata x%px\n", rdata);
James Smart63e480f2015-04-07 15:07:20 -04005319 if (pnode) {
5320 spin_lock_irq(shost->host_lock);
5321 pnode->nlp_flag &= ~NLP_NPR_ADISC;
5322 pnode->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
5323 spin_unlock_irq(shost->host_lock);
5324 }
James Smart8c50d252014-09-03 12:58:16 -04005325 lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
5326 LPFC_CTX_TGT);
5327 return FAST_IO_FAIL;
dea31012005-04-17 16:05:31 -05005328 }
James Smartea2151b2008-09-07 11:52:10 -04005329
5330 scsi_event.event_type = FC_REG_SCSI_EVENT;
5331 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
5332 scsi_event.lun = 0;
5333 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5334 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5335
James Smartbbb9d182009-06-10 17:23:16 -04005336 fc_host_post_vendor_event(shost, fc_get_event_number(),
5337 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -04005338
James Smarteed695d2016-10-13 15:06:04 -07005339 status = lpfc_send_taskmgmt(vport, cmnd, tgt_id, lun_id,
James Smartbbb9d182009-06-10 17:23:16 -04005340 FCP_TARGET_RESET);
dea31012005-04-17 16:05:31 -05005341
James Smarte8b62012007-08-02 11:10:09 -04005342 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005343 "0723 SCSI layer issued Target Reset (%d, %llu) "
James Smartbbb9d182009-06-10 17:23:16 -04005344 "return x%x\n", tgt_id, lun_id, status);
5345
5346 /*
5347 * We have to clean up i/o as : they may be orphaned by the TMF;
5348 * or if the TMF failed, they may be in an indeterminate state.
5349 * So, continue on.
5350 * We will report success if all the i/o aborts successfully.
5351 */
James Smart53151bb2013-10-10 12:24:07 -04005352 if (status == SUCCESS)
5353 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
James Smart3a707302012-06-12 13:54:42 -04005354 LPFC_CTX_TGT);
James Smart53151bb2013-10-10 12:24:07 -04005355 return status;
dea31012005-04-17 16:05:31 -05005356}
5357
James Smart9bad7672008-12-04 22:39:02 -05005358/**
James Smart3621a712009-04-06 18:47:14 -04005359 * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05005360 * @cmnd: Pointer to scsi_cmnd data structure.
5361 *
James Smartbbb9d182009-06-10 17:23:16 -04005362 * This routine does target reset to all targets on @cmnd->device->host.
5363 * This emulates Parallel SCSI Bus Reset Semantics.
James Smart9bad7672008-12-04 22:39:02 -05005364 *
James Smartbbb9d182009-06-10 17:23:16 -04005365 * Return code :
5366 * 0x2003 - Error
5367 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05005368 **/
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04005369static int
James Smart7054a602007-04-25 09:52:34 -04005370lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05005371{
James Smart2e0fef82007-06-17 19:56:36 -05005372 struct Scsi_Host *shost = cmnd->device->host;
5373 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05005374 struct lpfc_nodelist *ndlp = NULL;
James Smartea2151b2008-09-07 11:52:10 -04005375 struct lpfc_scsi_event_header scsi_event;
James Smartbbb9d182009-06-10 17:23:16 -04005376 int match;
5377 int ret = SUCCESS, status, i;
James Smartea2151b2008-09-07 11:52:10 -04005378
5379 scsi_event.event_type = FC_REG_SCSI_EVENT;
5380 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
5381 scsi_event.lun = 0;
5382 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
5383 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
5384
James Smartbbb9d182009-06-10 17:23:16 -04005385 fc_host_post_vendor_event(shost, fc_get_event_number(),
5386 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
dea31012005-04-17 16:05:31 -05005387
James Smartbf086112011-08-21 21:48:13 -04005388 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005389 if (status != 0 && status != SUCCESS)
James Smartbf086112011-08-21 21:48:13 -04005390 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005391
dea31012005-04-17 16:05:31 -05005392 /*
5393 * Since the driver manages a single bus device, reset all
5394 * targets known to the driver. Should any target reset
5395 * fail, this routine returns failure to the midlayer.
5396 */
James Smarte17da182006-07-06 15:49:25 -04005397 for (i = 0; i < LPFC_MAX_TARGET; i++) {
James Smart685f0bf2007-04-25 09:53:08 -04005398 /* Search for mapped node by target ID */
dea31012005-04-17 16:05:31 -05005399 match = 0;
James Smart2e0fef82007-06-17 19:56:36 -05005400 spin_lock_irq(shost->host_lock);
5401 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05005402 if (!NLP_CHK_NODE_ACT(ndlp))
5403 continue;
James Smarta6571c62012-10-31 14:44:42 -04005404 if (vport->phba->cfg_fcp2_no_tgt_reset &&
5405 (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE))
5406 continue;
James Smart685f0bf2007-04-25 09:53:08 -04005407 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
James Smart915caaa2008-06-14 22:52:38 -04005408 ndlp->nlp_sid == i &&
James Smarta0f2d3e2017-02-12 13:52:31 -08005409 ndlp->rport &&
5410 ndlp->nlp_type & NLP_FCP_TARGET) {
dea31012005-04-17 16:05:31 -05005411 match = 1;
5412 break;
5413 }
5414 }
James Smart2e0fef82007-06-17 19:56:36 -05005415 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005416 if (!match)
5417 continue;
James Smartbbb9d182009-06-10 17:23:16 -04005418
James Smarteed695d2016-10-13 15:06:04 -07005419 status = lpfc_send_taskmgmt(vport, cmnd,
James Smartbbb9d182009-06-10 17:23:16 -04005420 i, 0, FCP_TARGET_RESET);
5421
5422 if (status != SUCCESS) {
James Smarte8b62012007-08-02 11:10:09 -04005423 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5424 "0700 Bus Reset on target %d failed\n",
5425 i);
James Smart915caaa2008-06-14 22:52:38 -04005426 ret = FAILED;
dea31012005-04-17 16:05:31 -05005427 }
5428 }
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005429 /*
James Smartbbb9d182009-06-10 17:23:16 -04005430 * We have to clean up i/o as : they may be orphaned by the TMFs
5431 * above; or if any of the TMFs failed, they may be in an
5432 * indeterminate state.
5433 * We will report success if all the i/o aborts successfully.
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005434 */
James Smartbbb9d182009-06-10 17:23:16 -04005435
5436 status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
5437 if (status != SUCCESS)
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005438 ret = FAILED;
James Smartbbb9d182009-06-10 17:23:16 -04005439
James Smarte8b62012007-08-02 11:10:09 -04005440 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5441 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea31012005-04-17 16:05:31 -05005442 return ret;
5443}
5444
James Smart9bad7672008-12-04 22:39:02 -05005445/**
James Smart27b01b82012-05-09 21:19:44 -04005446 * lpfc_host_reset_handler - scsi_host_template eh_host_reset_handler entry pt
5447 * @cmnd: Pointer to scsi_cmnd data structure.
5448 *
5449 * This routine does host reset to the adaptor port. It brings the HBA
5450 * offline, performs a board restart, and then brings the board back online.
5451 * The lpfc_offline calls lpfc_sli_hba_down which will abort and local
5452 * reject all outstanding SCSI commands to the host and error returned
5453 * back to SCSI mid-level. As this will be SCSI mid-level's last resort
5454 * of error handling, it will only return error if resetting of the adapter
5455 * is not successful; in all other cases, will return success.
5456 *
5457 * Return code :
5458 * 0x2003 - Error
5459 * 0x2002 - Success
5460 **/
5461static int
5462lpfc_host_reset_handler(struct scsi_cmnd *cmnd)
5463{
5464 struct Scsi_Host *shost = cmnd->device->host;
5465 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5466 struct lpfc_hba *phba = vport->phba;
5467 int rc, ret = SUCCESS;
5468
James Smarta88dbb62013-04-17 20:18:39 -04005469 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5470 "3172 SCSI layer issued Host Reset Data:\n");
5471
James Smart618a5232012-06-12 13:54:36 -04005472 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart27b01b82012-05-09 21:19:44 -04005473 lpfc_offline(phba);
5474 rc = lpfc_sli_brdrestart(phba);
5475 if (rc)
James Smart8c24a4f2019-08-14 16:56:53 -07005476 goto error;
5477
James Smarta88dbb62013-04-17 20:18:39 -04005478 rc = lpfc_online(phba);
5479 if (rc)
James Smart8c24a4f2019-08-14 16:56:53 -07005480 goto error;
5481
James Smart27b01b82012-05-09 21:19:44 -04005482 lpfc_unblock_mgmt_io(phba);
5483
James Smart27b01b82012-05-09 21:19:44 -04005484 return ret;
James Smart8c24a4f2019-08-14 16:56:53 -07005485error:
5486 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5487 "3323 Failed host reset\n");
5488 lpfc_unblock_mgmt_io(phba);
5489 return FAILED;
James Smart27b01b82012-05-09 21:19:44 -04005490}
5491
5492/**
James Smart3621a712009-04-06 18:47:14 -04005493 * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
James Smart9bad7672008-12-04 22:39:02 -05005494 * @sdev: Pointer to scsi_device.
5495 *
5496 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
5497 * globally available list of scsi buffers. This routine also makes sure scsi
5498 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
5499 * of scsi buffer exists for the lifetime of the driver.
5500 *
5501 * Return codes:
5502 * non-0 - Error
5503 * 0 - Success
5504 **/
dea31012005-04-17 16:05:31 -05005505static int
dea31012005-04-17 16:05:31 -05005506lpfc_slave_alloc(struct scsi_device *sdev)
5507{
James Smart2e0fef82007-06-17 19:56:36 -05005508 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5509 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005510 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
James Smart3772a992009-05-22 14:50:54 -04005511 uint32_t total = 0;
dea31012005-04-17 16:05:31 -05005512 uint32_t num_to_alloc = 0;
James Smart3772a992009-05-22 14:50:54 -04005513 int num_allocated = 0;
James Smartd7c47992010-06-08 18:31:54 -04005514 uint32_t sdev_cnt;
James Smart1ba981f2014-02-20 09:56:45 -05005515 struct lpfc_device_data *device_data;
5516 unsigned long flags;
5517 struct lpfc_name target_wwpn;
dea31012005-04-17 16:05:31 -05005518
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005519 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05005520 return -ENXIO;
5521
James Smartf38fa0b2014-04-04 13:52:21 -04005522 if (phba->cfg_fof) {
James Smart1ba981f2014-02-20 09:56:45 -05005523
5524 /*
5525 * Check to see if the device data structure for the lun
5526 * exists. If not, create one.
5527 */
5528
5529 u64_to_wwn(rport->port_name, target_wwpn.u.wwn);
5530 spin_lock_irqsave(&phba->devicelock, flags);
5531 device_data = __lpfc_get_device_data(phba,
5532 &phba->luns,
5533 &vport->fc_portname,
5534 &target_wwpn,
5535 sdev->lun);
5536 if (!device_data) {
5537 spin_unlock_irqrestore(&phba->devicelock, flags);
5538 device_data = lpfc_create_device_data(phba,
5539 &vport->fc_portname,
5540 &target_wwpn,
James Smartb5749fe2016-12-19 15:07:26 -08005541 sdev->lun,
5542 phba->cfg_XLanePriority,
5543 true);
James Smart1ba981f2014-02-20 09:56:45 -05005544 if (!device_data)
5545 return -ENOMEM;
5546 spin_lock_irqsave(&phba->devicelock, flags);
5547 list_add_tail(&device_data->listentry, &phba->luns);
5548 }
5549 device_data->rport_data = rport->dd_data;
5550 device_data->available = true;
5551 spin_unlock_irqrestore(&phba->devicelock, flags);
5552 sdev->hostdata = device_data;
5553 } else {
5554 sdev->hostdata = rport->dd_data;
5555 }
James Smartd7c47992010-06-08 18:31:54 -04005556 sdev_cnt = atomic_inc_return(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05005557
James Smart0794d602019-01-28 11:14:19 -08005558 /* For SLI4, all IO buffers are pre-allocated */
5559 if (phba->sli_rev == LPFC_SLI_REV4)
5560 return 0;
5561
5562 /* This code path is now ONLY for SLI3 adapters */
5563
dea31012005-04-17 16:05:31 -05005564 /*
5565 * Populate the cmds_per_lun count scsi_bufs into this host's globally
5566 * available list of scsi buffers. Don't allocate more than the
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04005567 * HBA limit conveyed to the midlayer via the host structure. The
5568 * formula accounts for the lun_queue_depth + error handlers + 1
5569 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea31012005-04-17 16:05:31 -05005570 */
5571 total = phba->total_scsi_bufs;
James Smart3de2a652007-08-02 11:09:59 -04005572 num_to_alloc = vport->cfg_lun_queue_depth + 2;
James Smart92d7f7b2007-06-17 19:56:38 -05005573
James Smartd7c47992010-06-08 18:31:54 -04005574 /* If allocated buffers are enough do nothing */
5575 if ((sdev_cnt * (vport->cfg_lun_queue_depth + 2)) < total)
5576 return 0;
5577
James Smart92d7f7b2007-06-17 19:56:38 -05005578 /* Allow some exchanges to be available always to complete discovery */
5579 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04005580 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5581 "0704 At limitation of %d preallocated "
5582 "command buffers\n", total);
dea31012005-04-17 16:05:31 -05005583 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05005584 /* Allow some exchanges to be available always to complete discovery */
5585 } else if (total + num_to_alloc >
5586 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04005587 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5588 "0705 Allocation request of %d "
5589 "command buffers will exceed max of %d. "
5590 "Reducing allocation request to %d.\n",
5591 num_to_alloc, phba->cfg_hba_queue_depth,
5592 (phba->cfg_hba_queue_depth - total));
dea31012005-04-17 16:05:31 -05005593 num_to_alloc = phba->cfg_hba_queue_depth - total;
5594 }
James Smart0794d602019-01-28 11:14:19 -08005595 num_allocated = lpfc_new_scsi_buf_s3(vport, num_to_alloc);
James Smart3772a992009-05-22 14:50:54 -04005596 if (num_to_alloc != num_allocated) {
James Smart96f70772013-04-17 20:16:15 -04005597 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5598 "0708 Allocation request of %d "
5599 "command buffers did not succeed. "
5600 "Allocated %d buffers.\n",
5601 num_to_alloc, num_allocated);
dea31012005-04-17 16:05:31 -05005602 }
James Smart1c6f4ef52009-11-18 15:40:49 -05005603 if (num_allocated > 0)
5604 phba->total_scsi_bufs += num_allocated;
dea31012005-04-17 16:05:31 -05005605 return 0;
5606}
5607
James Smart9bad7672008-12-04 22:39:02 -05005608/**
James Smart3621a712009-04-06 18:47:14 -04005609 * lpfc_slave_configure - scsi_host_template slave_configure entry point
James Smart9bad7672008-12-04 22:39:02 -05005610 * @sdev: Pointer to scsi_device.
5611 *
5612 * This routine configures following items
5613 * - Tag command queuing support for @sdev if supported.
James Smart9bad7672008-12-04 22:39:02 -05005614 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
5615 *
5616 * Return codes:
5617 * 0 - Success
5618 **/
dea31012005-04-17 16:05:31 -05005619static int
5620lpfc_slave_configure(struct scsi_device *sdev)
5621{
James Smart2e0fef82007-06-17 19:56:36 -05005622 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5623 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05005624
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01005625 scsi_change_queue_depth(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05005626
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005627 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04005628 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -08005629 &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005630 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
5631 lpfc_poll_rearm_timer(phba);
5632 }
5633
dea31012005-04-17 16:05:31 -05005634 return 0;
5635}
5636
James Smart9bad7672008-12-04 22:39:02 -05005637/**
James Smart3621a712009-04-06 18:47:14 -04005638 * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
James Smart9bad7672008-12-04 22:39:02 -05005639 * @sdev: Pointer to scsi_device.
5640 *
5641 * This routine sets @sdev hostatdata filed to null.
5642 **/
dea31012005-04-17 16:05:31 -05005643static void
5644lpfc_slave_destroy(struct scsi_device *sdev)
5645{
James Smartd7c47992010-06-08 18:31:54 -04005646 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5647 struct lpfc_hba *phba = vport->phba;
James Smart1ba981f2014-02-20 09:56:45 -05005648 unsigned long flags;
5649 struct lpfc_device_data *device_data = sdev->hostdata;
5650
James Smartd7c47992010-06-08 18:31:54 -04005651 atomic_dec(&phba->sdev_cnt);
James Smartf38fa0b2014-04-04 13:52:21 -04005652 if ((phba->cfg_fof) && (device_data)) {
James Smart1ba981f2014-02-20 09:56:45 -05005653 spin_lock_irqsave(&phba->devicelock, flags);
5654 device_data->available = false;
5655 if (!device_data->oas_enabled)
5656 lpfc_delete_device_data(phba, device_data);
5657 spin_unlock_irqrestore(&phba->devicelock, flags);
5658 }
dea31012005-04-17 16:05:31 -05005659 sdev->hostdata = NULL;
5660 return;
5661}
5662
James Smart1ba981f2014-02-20 09:56:45 -05005663/**
5664 * lpfc_create_device_data - creates and initializes device data structure for OAS
5665 * @pha: Pointer to host bus adapter structure.
5666 * @vport_wwpn: Pointer to vport's wwpn information
5667 * @target_wwpn: Pointer to target's wwpn information
5668 * @lun: Lun on target
5669 * @atomic_create: Flag to indicate if memory should be allocated using the
5670 * GFP_ATOMIC flag or not.
5671 *
5672 * This routine creates a device data structure which will contain identifying
5673 * information for the device (host wwpn, target wwpn, lun), state of OAS,
5674 * whether or not the corresponding lun is available by the system,
5675 * and pointer to the rport data.
5676 *
5677 * Return codes:
5678 * NULL - Error
5679 * Pointer to lpfc_device_data - Success
5680 **/
5681struct lpfc_device_data*
5682lpfc_create_device_data(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
5683 struct lpfc_name *target_wwpn, uint64_t lun,
James Smartb5749fe2016-12-19 15:07:26 -08005684 uint32_t pri, bool atomic_create)
James Smart1ba981f2014-02-20 09:56:45 -05005685{
5686
5687 struct lpfc_device_data *lun_info;
5688 int memory_flags;
5689
5690 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
James Smartf38fa0b2014-04-04 13:52:21 -04005691 !(phba->cfg_fof))
James Smart1ba981f2014-02-20 09:56:45 -05005692 return NULL;
5693
5694 /* Attempt to create the device data to contain lun info */
5695
5696 if (atomic_create)
5697 memory_flags = GFP_ATOMIC;
5698 else
5699 memory_flags = GFP_KERNEL;
5700 lun_info = mempool_alloc(phba->device_data_mem_pool, memory_flags);
5701 if (!lun_info)
5702 return NULL;
5703 INIT_LIST_HEAD(&lun_info->listentry);
5704 lun_info->rport_data = NULL;
5705 memcpy(&lun_info->device_id.vport_wwpn, vport_wwpn,
5706 sizeof(struct lpfc_name));
5707 memcpy(&lun_info->device_id.target_wwpn, target_wwpn,
5708 sizeof(struct lpfc_name));
5709 lun_info->device_id.lun = lun;
5710 lun_info->oas_enabled = false;
James Smartb5749fe2016-12-19 15:07:26 -08005711 lun_info->priority = pri;
James Smart1ba981f2014-02-20 09:56:45 -05005712 lun_info->available = false;
5713 return lun_info;
5714}
5715
5716/**
5717 * lpfc_delete_device_data - frees a device data structure for OAS
5718 * @pha: Pointer to host bus adapter structure.
5719 * @lun_info: Pointer to device data structure to free.
5720 *
5721 * This routine frees the previously allocated device data structure passed.
5722 *
5723 **/
5724void
5725lpfc_delete_device_data(struct lpfc_hba *phba,
5726 struct lpfc_device_data *lun_info)
5727{
5728
5729 if (unlikely(!phba) || !lun_info ||
James Smartf38fa0b2014-04-04 13:52:21 -04005730 !(phba->cfg_fof))
James Smart1ba981f2014-02-20 09:56:45 -05005731 return;
5732
5733 if (!list_empty(&lun_info->listentry))
5734 list_del(&lun_info->listentry);
5735 mempool_free(lun_info, phba->device_data_mem_pool);
5736 return;
5737}
5738
5739/**
5740 * __lpfc_get_device_data - returns the device data for the specified lun
5741 * @pha: Pointer to host bus adapter structure.
5742 * @list: Point to list to search.
5743 * @vport_wwpn: Pointer to vport's wwpn information
5744 * @target_wwpn: Pointer to target's wwpn information
5745 * @lun: Lun on target
5746 *
5747 * This routine searches the list passed for the specified lun's device data.
5748 * This function does not hold locks, it is the responsibility of the caller
5749 * to ensure the proper lock is held before calling the function.
5750 *
5751 * Return codes:
5752 * NULL - Error
5753 * Pointer to lpfc_device_data - Success
5754 **/
5755struct lpfc_device_data*
5756__lpfc_get_device_data(struct lpfc_hba *phba, struct list_head *list,
5757 struct lpfc_name *vport_wwpn,
5758 struct lpfc_name *target_wwpn, uint64_t lun)
5759{
5760
5761 struct lpfc_device_data *lun_info;
5762
5763 if (unlikely(!phba) || !list || !vport_wwpn || !target_wwpn ||
James Smartf38fa0b2014-04-04 13:52:21 -04005764 !phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005765 return NULL;
5766
5767 /* Check to see if the lun is already enabled for OAS. */
5768
5769 list_for_each_entry(lun_info, list, listentry) {
5770 if ((memcmp(&lun_info->device_id.vport_wwpn, vport_wwpn,
5771 sizeof(struct lpfc_name)) == 0) &&
5772 (memcmp(&lun_info->device_id.target_wwpn, target_wwpn,
5773 sizeof(struct lpfc_name)) == 0) &&
5774 (lun_info->device_id.lun == lun))
5775 return lun_info;
5776 }
5777
5778 return NULL;
5779}
5780
5781/**
5782 * lpfc_find_next_oas_lun - searches for the next oas lun
5783 * @pha: Pointer to host bus adapter structure.
5784 * @vport_wwpn: Pointer to vport's wwpn information
5785 * @target_wwpn: Pointer to target's wwpn information
5786 * @starting_lun: Pointer to the lun to start searching for
5787 * @found_vport_wwpn: Pointer to the found lun's vport wwpn information
5788 * @found_target_wwpn: Pointer to the found lun's target wwpn information
5789 * @found_lun: Pointer to the found lun.
5790 * @found_lun_status: Pointer to status of the found lun.
5791 *
5792 * This routine searches the luns list for the specified lun
5793 * or the first lun for the vport/target. If the vport wwpn contains
5794 * a zero value then a specific vport is not specified. In this case
5795 * any vport which contains the lun will be considered a match. If the
5796 * target wwpn contains a zero value then a specific target is not specified.
5797 * In this case any target which contains the lun will be considered a
5798 * match. If the lun is found, the lun, vport wwpn, target wwpn and lun status
5799 * are returned. The function will also return the next lun if available.
5800 * If the next lun is not found, starting_lun parameter will be set to
5801 * NO_MORE_OAS_LUN.
5802 *
5803 * Return codes:
5804 * non-0 - Error
5805 * 0 - Success
5806 **/
5807bool
5808lpfc_find_next_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
5809 struct lpfc_name *target_wwpn, uint64_t *starting_lun,
5810 struct lpfc_name *found_vport_wwpn,
5811 struct lpfc_name *found_target_wwpn,
5812 uint64_t *found_lun,
James Smartb5749fe2016-12-19 15:07:26 -08005813 uint32_t *found_lun_status,
5814 uint32_t *found_lun_pri)
James Smart1ba981f2014-02-20 09:56:45 -05005815{
5816
5817 unsigned long flags;
5818 struct lpfc_device_data *lun_info;
5819 struct lpfc_device_id *device_id;
5820 uint64_t lun;
5821 bool found = false;
5822
5823 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
5824 !starting_lun || !found_vport_wwpn ||
5825 !found_target_wwpn || !found_lun || !found_lun_status ||
5826 (*starting_lun == NO_MORE_OAS_LUN) ||
James Smartf38fa0b2014-04-04 13:52:21 -04005827 !phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005828 return false;
5829
5830 lun = *starting_lun;
5831 *found_lun = NO_MORE_OAS_LUN;
5832 *starting_lun = NO_MORE_OAS_LUN;
5833
5834 /* Search for lun or the lun closet in value */
5835
5836 spin_lock_irqsave(&phba->devicelock, flags);
5837 list_for_each_entry(lun_info, &phba->luns, listentry) {
5838 if (((wwn_to_u64(vport_wwpn->u.wwn) == 0) ||
5839 (memcmp(&lun_info->device_id.vport_wwpn, vport_wwpn,
5840 sizeof(struct lpfc_name)) == 0)) &&
5841 ((wwn_to_u64(target_wwpn->u.wwn) == 0) ||
5842 (memcmp(&lun_info->device_id.target_wwpn, target_wwpn,
5843 sizeof(struct lpfc_name)) == 0)) &&
5844 (lun_info->oas_enabled)) {
5845 device_id = &lun_info->device_id;
5846 if ((!found) &&
5847 ((lun == FIND_FIRST_OAS_LUN) ||
5848 (device_id->lun == lun))) {
5849 *found_lun = device_id->lun;
5850 memcpy(found_vport_wwpn,
5851 &device_id->vport_wwpn,
5852 sizeof(struct lpfc_name));
5853 memcpy(found_target_wwpn,
5854 &device_id->target_wwpn,
5855 sizeof(struct lpfc_name));
5856 if (lun_info->available)
5857 *found_lun_status =
5858 OAS_LUN_STATUS_EXISTS;
5859 else
5860 *found_lun_status = 0;
James Smartb5749fe2016-12-19 15:07:26 -08005861 *found_lun_pri = lun_info->priority;
James Smart1ba981f2014-02-20 09:56:45 -05005862 if (phba->cfg_oas_flags & OAS_FIND_ANY_VPORT)
5863 memset(vport_wwpn, 0x0,
5864 sizeof(struct lpfc_name));
5865 if (phba->cfg_oas_flags & OAS_FIND_ANY_TARGET)
5866 memset(target_wwpn, 0x0,
5867 sizeof(struct lpfc_name));
5868 found = true;
5869 } else if (found) {
5870 *starting_lun = device_id->lun;
5871 memcpy(vport_wwpn, &device_id->vport_wwpn,
5872 sizeof(struct lpfc_name));
5873 memcpy(target_wwpn, &device_id->target_wwpn,
5874 sizeof(struct lpfc_name));
5875 break;
5876 }
5877 }
5878 }
5879 spin_unlock_irqrestore(&phba->devicelock, flags);
5880 return found;
5881}
5882
5883/**
5884 * lpfc_enable_oas_lun - enables a lun for OAS operations
5885 * @pha: Pointer to host bus adapter structure.
5886 * @vport_wwpn: Pointer to vport's wwpn information
5887 * @target_wwpn: Pointer to target's wwpn information
5888 * @lun: Lun
5889 *
5890 * This routine enables a lun for oas operations. The routines does so by
5891 * doing the following :
5892 *
5893 * 1) Checks to see if the device data for the lun has been created.
5894 * 2) If found, sets the OAS enabled flag if not set and returns.
5895 * 3) Otherwise, creates a device data structure.
5896 * 4) If successfully created, indicates the device data is for an OAS lun,
5897 * indicates the lun is not available and add to the list of luns.
5898 *
5899 * Return codes:
5900 * false - Error
5901 * true - Success
5902 **/
5903bool
5904lpfc_enable_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
James Smartc92c8412016-07-06 12:36:05 -07005905 struct lpfc_name *target_wwpn, uint64_t lun, uint8_t pri)
James Smart1ba981f2014-02-20 09:56:45 -05005906{
5907
5908 struct lpfc_device_data *lun_info;
5909 unsigned long flags;
5910
5911 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
James Smartf38fa0b2014-04-04 13:52:21 -04005912 !phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005913 return false;
5914
5915 spin_lock_irqsave(&phba->devicelock, flags);
5916
5917 /* Check to see if the device data for the lun has been created */
5918 lun_info = __lpfc_get_device_data(phba, &phba->luns, vport_wwpn,
5919 target_wwpn, lun);
5920 if (lun_info) {
5921 if (!lun_info->oas_enabled)
5922 lun_info->oas_enabled = true;
James Smartb5749fe2016-12-19 15:07:26 -08005923 lun_info->priority = pri;
James Smart1ba981f2014-02-20 09:56:45 -05005924 spin_unlock_irqrestore(&phba->devicelock, flags);
5925 return true;
5926 }
5927
5928 /* Create an lun info structure and add to list of luns */
5929 lun_info = lpfc_create_device_data(phba, vport_wwpn, target_wwpn, lun,
James Smart2d71dc82019-05-21 17:48:51 -07005930 pri, true);
James Smart1ba981f2014-02-20 09:56:45 -05005931 if (lun_info) {
5932 lun_info->oas_enabled = true;
James Smartc92c8412016-07-06 12:36:05 -07005933 lun_info->priority = pri;
James Smart1ba981f2014-02-20 09:56:45 -05005934 lun_info->available = false;
5935 list_add_tail(&lun_info->listentry, &phba->luns);
5936 spin_unlock_irqrestore(&phba->devicelock, flags);
5937 return true;
5938 }
5939 spin_unlock_irqrestore(&phba->devicelock, flags);
5940 return false;
5941}
5942
5943/**
5944 * lpfc_disable_oas_lun - disables a lun for OAS operations
5945 * @pha: Pointer to host bus adapter structure.
5946 * @vport_wwpn: Pointer to vport's wwpn information
5947 * @target_wwpn: Pointer to target's wwpn information
5948 * @lun: Lun
5949 *
5950 * This routine disables a lun for oas operations. The routines does so by
5951 * doing the following :
5952 *
5953 * 1) Checks to see if the device data for the lun is created.
5954 * 2) If present, clears the flag indicating this lun is for OAS.
5955 * 3) If the lun is not available by the system, the device data is
5956 * freed.
5957 *
5958 * Return codes:
5959 * false - Error
5960 * true - Success
5961 **/
5962bool
5963lpfc_disable_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
James Smartb5749fe2016-12-19 15:07:26 -08005964 struct lpfc_name *target_wwpn, uint64_t lun, uint8_t pri)
James Smart1ba981f2014-02-20 09:56:45 -05005965{
5966
5967 struct lpfc_device_data *lun_info;
5968 unsigned long flags;
5969
5970 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
James Smartf38fa0b2014-04-04 13:52:21 -04005971 !phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005972 return false;
5973
5974 spin_lock_irqsave(&phba->devicelock, flags);
5975
5976 /* Check to see if the lun is available. */
5977 lun_info = __lpfc_get_device_data(phba,
5978 &phba->luns, vport_wwpn,
5979 target_wwpn, lun);
5980 if (lun_info) {
5981 lun_info->oas_enabled = false;
James Smartb5749fe2016-12-19 15:07:26 -08005982 lun_info->priority = pri;
James Smart1ba981f2014-02-20 09:56:45 -05005983 if (!lun_info->available)
5984 lpfc_delete_device_data(phba, lun_info);
5985 spin_unlock_irqrestore(&phba->devicelock, flags);
5986 return true;
5987 }
5988
5989 spin_unlock_irqrestore(&phba->devicelock, flags);
5990 return false;
5991}
James Smart92d7f7b2007-06-17 19:56:38 -05005992
James Smart895427b2017-02-12 13:52:30 -08005993static int
5994lpfc_no_command(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
5995{
5996 return SCSI_MLQUEUE_HOST_BUSY;
5997}
5998
5999static int
6000lpfc_no_handler(struct scsi_cmnd *cmnd)
6001{
6002 return FAILED;
6003}
6004
6005static int
6006lpfc_no_slave(struct scsi_device *sdev)
6007{
6008 return -ENODEV;
6009}
6010
6011struct scsi_host_template lpfc_template_nvme = {
6012 .module = THIS_MODULE,
6013 .name = LPFC_DRIVER_NAME,
6014 .proc_name = LPFC_DRIVER_NAME,
6015 .info = lpfc_info,
6016 .queuecommand = lpfc_no_command,
6017 .eh_abort_handler = lpfc_no_handler,
6018 .eh_device_reset_handler = lpfc_no_handler,
6019 .eh_target_reset_handler = lpfc_no_handler,
6020 .eh_bus_reset_handler = lpfc_no_handler,
6021 .eh_host_reset_handler = lpfc_no_handler,
6022 .slave_alloc = lpfc_no_slave,
6023 .slave_configure = lpfc_no_slave,
6024 .scan_finished = lpfc_scan_finished,
6025 .this_id = -1,
6026 .sg_tablesize = 1,
6027 .cmd_per_lun = 1,
James Smart895427b2017-02-12 13:52:30 -08006028 .shost_attrs = lpfc_hba_attrs,
6029 .max_sectors = 0xFFFF,
6030 .vendor_id = LPFC_NL_VENDOR_ID,
6031 .track_queue_depth = 0,
6032};
6033
dea31012005-04-17 16:05:31 -05006034struct scsi_host_template lpfc_template = {
6035 .module = THIS_MODULE,
6036 .name = LPFC_DRIVER_NAME,
James Smart08dcd4c2016-07-06 12:35:59 -07006037 .proc_name = LPFC_DRIVER_NAME,
dea31012005-04-17 16:05:31 -05006038 .info = lpfc_info,
6039 .queuecommand = lpfc_queuecommand,
Christoph Hellwigb6a05c82017-01-30 13:18:58 +01006040 .eh_timed_out = fc_eh_timed_out,
dea31012005-04-17 16:05:31 -05006041 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04006042 .eh_device_reset_handler = lpfc_device_reset_handler,
6043 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart7054a602007-04-25 09:52:34 -04006044 .eh_bus_reset_handler = lpfc_bus_reset_handler,
James Smart27b01b82012-05-09 21:19:44 -04006045 .eh_host_reset_handler = lpfc_host_reset_handler,
dea31012005-04-17 16:05:31 -05006046 .slave_alloc = lpfc_slave_alloc,
6047 .slave_configure = lpfc_slave_configure,
6048 .slave_destroy = lpfc_slave_destroy,
James Smart47a86172007-04-25 09:53:22 -04006049 .scan_finished = lpfc_scan_finished,
dea31012005-04-17 16:05:31 -05006050 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05006051 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
dea31012005-04-17 16:05:31 -05006052 .cmd_per_lun = LPFC_CMD_PER_LUN,
James Smart2e0fef82007-06-17 19:56:36 -05006053 .shost_attrs = lpfc_hba_attrs,
James.Smart@Emulex.Com564b2962005-06-25 10:34:17 -04006054 .max_sectors = 0xFFFF,
James Smartf1c3b0f2009-07-19 10:01:32 -04006055 .vendor_id = LPFC_NL_VENDOR_ID,
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01006056 .change_queue_depth = scsi_change_queue_depth,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01006057 .track_queue_depth = 1,
dea31012005-04-17 16:05:31 -05006058};