blob: a5ad6d972e1ba333c325171804deb23f608bc967 [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 Smart0d041212019-01-28 11:14:41 -08004 * Copyright (C) 2017-2019 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 Smart737d4242013-04-17 20:14:49 -040056int _dump_buf_done = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -050057
58static char *dif_op_str[] = {
James Smart9a6b09c2012-03-01 22:37:42 -050059 "PROT_NORMAL",
60 "PROT_READ_INSERT",
61 "PROT_WRITE_STRIP",
62 "PROT_READ_STRIP",
63 "PROT_WRITE_INSERT",
64 "PROT_READ_PASS",
65 "PROT_WRITE_PASS",
66};
67
James Smartf9bb2da2011-10-10 21:34:11 -040068struct scsi_dif_tuple {
69 __be16 guard_tag; /* Checksum */
70 __be16 app_tag; /* Opaque storage */
71 __be32 ref_tag; /* Target LBA or indirect LBA */
72};
73
James Smart1ba981f2014-02-20 09:56:45 -050074static struct lpfc_rport_data *
75lpfc_rport_data_from_scsi_device(struct scsi_device *sdev)
76{
77 struct lpfc_vport *vport = (struct lpfc_vport *)sdev->host->hostdata;
78
James Smartf38fa0b2014-04-04 13:52:21 -040079 if (vport->phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -050080 return ((struct lpfc_device_data *)sdev->hostdata)->rport_data;
81 else
82 return (struct lpfc_rport_data *)sdev->hostdata;
83}
84
James Smartda0436e2009-05-22 14:51:39 -040085static void
James Smartc4908502019-01-28 11:14:28 -080086lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_io_buf *psb);
James Smart1c6f4ef52009-11-18 15:40:49 -050087static void
James Smartc4908502019-01-28 11:14:28 -080088lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_io_buf *psb);
James Smart9c6aa9d2013-05-31 17:03:39 -040089static int
90lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc);
James Smarte2a0a9d2008-12-04 22:40:02 -050091
92static void
James Smart6a9c52c2009-10-02 15:16:51 -040093lpfc_debug_save_data(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
James Smarte2a0a9d2008-12-04 22:40:02 -050094{
95 void *src, *dst;
96 struct scatterlist *sgde = scsi_sglist(cmnd);
97
98 if (!_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -040099 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
100 "9050 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
James Smarte2a0a9d2008-12-04 22:40:02 -0500101 __func__);
102 return;
103 }
104
105
106 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -0400107 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
108 "9051 BLKGRD: ERROR: data scatterlist is null\n");
James Smarte2a0a9d2008-12-04 22:40:02 -0500109 return;
110 }
111
112 dst = (void *) _dump_buf_data;
113 while (sgde) {
114 src = sg_virt(sgde);
115 memcpy(dst, src, sgde->length);
116 dst += sgde->length;
117 sgde = sg_next(sgde);
118 }
119}
120
121static void
James Smart6a9c52c2009-10-02 15:16:51 -0400122lpfc_debug_save_dif(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
James Smarte2a0a9d2008-12-04 22:40:02 -0500123{
124 void *src, *dst;
125 struct scatterlist *sgde = scsi_prot_sglist(cmnd);
126
127 if (!_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -0400128 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
129 "9052 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
James Smarte2a0a9d2008-12-04 22:40:02 -0500130 __func__);
131 return;
132 }
133
134 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -0400135 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
136 "9053 BLKGRD: ERROR: prot scatterlist is null\n");
James Smarte2a0a9d2008-12-04 22:40:02 -0500137 return;
138 }
139
140 dst = _dump_buf_dif;
141 while (sgde) {
142 src = sg_virt(sgde);
143 memcpy(dst, src, sgde->length);
144 dst += sgde->length;
145 sgde = sg_next(sgde);
146 }
147}
148
James Smart9c6aa9d2013-05-31 17:03:39 -0400149static inline unsigned
150lpfc_cmd_blksize(struct scsi_cmnd *sc)
151{
152 return sc->device->sector_size;
153}
154
155#define LPFC_CHECK_PROTECT_GUARD 1
156#define LPFC_CHECK_PROTECT_REF 2
157static inline unsigned
158lpfc_cmd_protect(struct scsi_cmnd *sc, int flag)
159{
160 return 1;
161}
162
163static inline unsigned
164lpfc_cmd_guard_csum(struct scsi_cmnd *sc)
165{
166 if (lpfc_prot_group_type(NULL, sc) == LPFC_PG_TYPE_NO_DIF)
167 return 0;
168 if (scsi_host_get_guard(sc->device->host) == SHOST_DIX_GUARD_IP)
169 return 1;
170 return 0;
171}
172
James Smartea2151b2008-09-07 11:52:10 -0400173/**
James Smartf1126682009-06-10 17:22:44 -0400174 * lpfc_sli4_set_rsp_sgl_last - Set the last bit in the response sge.
175 * @phba: Pointer to HBA object.
176 * @lpfc_cmd: lpfc scsi command object pointer.
177 *
178 * This function is called from the lpfc_prep_task_mgmt_cmd function to
179 * set the last bit in the response sge entry.
180 **/
181static void
182lpfc_sli4_set_rsp_sgl_last(struct lpfc_hba *phba,
James Smartc4908502019-01-28 11:14:28 -0800183 struct lpfc_io_buf *lpfc_cmd)
James Smartf1126682009-06-10 17:22:44 -0400184{
James Smart0794d602019-01-28 11:14:19 -0800185 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
James Smartf1126682009-06-10 17:22:44 -0400186 if (sgl) {
187 sgl += 1;
188 sgl->word2 = le32_to_cpu(sgl->word2);
189 bf_set(lpfc_sli4_sge_last, sgl, 1);
190 sgl->word2 = cpu_to_le32(sgl->word2);
191 }
192}
193
194/**
James Smart3621a712009-04-06 18:47:14 -0400195 * lpfc_update_stats - Update statistical data for the command completion
James Smartea2151b2008-09-07 11:52:10 -0400196 * @phba: Pointer to HBA object.
197 * @lpfc_cmd: lpfc scsi command object pointer.
198 *
199 * This function is called when there is a command completion and this
200 * function updates the statistical data for the command completion.
201 **/
202static void
James Smartc4908502019-01-28 11:14:28 -0800203lpfc_update_stats(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
James Smartea2151b2008-09-07 11:52:10 -0400204{
James Smart18027a82018-09-10 10:30:49 -0700205 struct lpfc_rport_data *rdata;
206 struct lpfc_nodelist *pnode;
James Smartea2151b2008-09-07 11:52:10 -0400207 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
208 unsigned long flags;
209 struct Scsi_Host *shost = cmd->device->host;
210 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
211 unsigned long latency;
212 int i;
213
James Smart18027a82018-09-10 10:30:49 -0700214 if (!vport->stat_data_enabled ||
215 vport->stat_data_blocked ||
216 (cmd->result))
James Smartea2151b2008-09-07 11:52:10 -0400217 return;
218
James Smart9f1e1b52008-12-04 22:39:40 -0500219 latency = jiffies_to_msecs((long)jiffies - (long)lpfc_cmd->start_time);
James Smart18027a82018-09-10 10:30:49 -0700220 rdata = lpfc_cmd->rdata;
221 pnode = rdata->pnode;
James Smart9f1e1b52008-12-04 22:39:40 -0500222
James Smartea2151b2008-09-07 11:52:10 -0400223 spin_lock_irqsave(shost->host_lock, flags);
James Smart18027a82018-09-10 10:30:49 -0700224 if (!pnode ||
225 !pnode->lat_data ||
226 (phba->bucket_type == LPFC_NO_BUCKET)) {
James Smartea2151b2008-09-07 11:52:10 -0400227 spin_unlock_irqrestore(shost->host_lock, flags);
228 return;
229 }
James Smartea2151b2008-09-07 11:52:10 -0400230
231 if (phba->bucket_type == LPFC_LINEAR_BUCKET) {
232 i = (latency + phba->bucket_step - 1 - phba->bucket_base)/
233 phba->bucket_step;
James Smart9f1e1b52008-12-04 22:39:40 -0500234 /* check array subscript bounds */
235 if (i < 0)
236 i = 0;
237 else if (i >= LPFC_MAX_BUCKET_COUNT)
238 i = LPFC_MAX_BUCKET_COUNT - 1;
James Smartea2151b2008-09-07 11:52:10 -0400239 } else {
240 for (i = 0; i < LPFC_MAX_BUCKET_COUNT-1; i++)
241 if (latency <= (phba->bucket_base +
242 ((1<<i)*phba->bucket_step)))
243 break;
244 }
245
246 pnode->lat_data[i].cmd_count++;
247 spin_unlock_irqrestore(shost->host_lock, flags);
248}
249
James Smartea2151b2008-09-07 11:52:10 -0400250/**
James Smart3621a712009-04-06 18:47:14 -0400251 * lpfc_rampdown_queue_depth - Post RAMP_DOWN_QUEUE event to worker thread
James Smart9bad7672008-12-04 22:39:02 -0500252 * @phba: The Hba for which this call is being executed.
253 *
254 * This routine is called when there is resource error in driver or firmware.
255 * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
256 * posts at most 1 event each second. This routine wakes up worker thread of
257 * @phba to process WORKER_RAM_DOWN_EVENT event.
258 *
259 * This routine should be called with no lock held.
260 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500261void
James Smarteaf15d52008-12-04 22:39:29 -0500262lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
James Smart92d7f7b2007-06-17 19:56:38 -0500263{
264 unsigned long flags;
James Smart5e9d9b82008-06-14 22:52:53 -0400265 uint32_t evt_posted;
Manuel Schölling0d4aec12014-09-03 12:55:58 -0400266 unsigned long expires;
James Smart92d7f7b2007-06-17 19:56:38 -0500267
268 spin_lock_irqsave(&phba->hbalock, flags);
269 atomic_inc(&phba->num_rsrc_err);
270 phba->last_rsrc_error_time = jiffies;
271
Manuel Schölling0d4aec12014-09-03 12:55:58 -0400272 expires = phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL;
273 if (time_after(expires, jiffies)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500274 spin_unlock_irqrestore(&phba->hbalock, flags);
275 return;
276 }
277
278 phba->last_ramp_down_time = jiffies;
279
280 spin_unlock_irqrestore(&phba->hbalock, flags);
281
282 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400283 evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
284 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500285 phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500286 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
287
James Smart5e9d9b82008-06-14 22:52:53 -0400288 if (!evt_posted)
289 lpfc_worker_wake_up(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500290 return;
291}
292
James Smart9bad7672008-12-04 22:39:02 -0500293/**
James Smart3621a712009-04-06 18:47:14 -0400294 * lpfc_ramp_down_queue_handler - WORKER_RAMP_DOWN_QUEUE event handler
James Smart9bad7672008-12-04 22:39:02 -0500295 * @phba: The Hba for which this call is being executed.
296 *
297 * This routine is called to process WORKER_RAMP_DOWN_QUEUE event for worker
298 * thread.This routine reduces queue depth for all scsi device on each vport
299 * associated with @phba.
300 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500301void
302lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
303{
James Smart549e55c2007-08-02 11:09:51 -0400304 struct lpfc_vport **vports;
305 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500306 struct scsi_device *sdev;
James Smart5ffc2662009-11-18 15:39:44 -0500307 unsigned long new_queue_depth;
James Smart92d7f7b2007-06-17 19:56:38 -0500308 unsigned long num_rsrc_err, num_cmd_success;
James Smart549e55c2007-08-02 11:09:51 -0400309 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500310
311 num_rsrc_err = atomic_read(&phba->num_rsrc_err);
312 num_cmd_success = atomic_read(&phba->num_cmd_success);
313
James Smart75ad83a2012-05-09 21:18:40 -0400314 /*
315 * The error and success command counters are global per
316 * driver instance. If another handler has already
317 * operated on this error event, just exit.
318 */
319 if (num_rsrc_err == 0)
320 return;
321
James Smart549e55c2007-08-02 11:09:51 -0400322 vports = lpfc_create_vport_work_array(phba);
323 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400324 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400325 shost = lpfc_shost_from_vport(vports[i]);
326 shost_for_each_device(sdev, shost) {
James Smart92d7f7b2007-06-17 19:56:38 -0500327 new_queue_depth =
James Smart549e55c2007-08-02 11:09:51 -0400328 sdev->queue_depth * num_rsrc_err /
329 (num_rsrc_err + num_cmd_success);
330 if (!new_queue_depth)
331 new_queue_depth = sdev->queue_depth - 1;
332 else
333 new_queue_depth = sdev->queue_depth -
334 new_queue_depth;
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +0100335 scsi_change_queue_depth(sdev, new_queue_depth);
James Smart549e55c2007-08-02 11:09:51 -0400336 }
James Smart92d7f7b2007-06-17 19:56:38 -0500337 }
James Smart09372822008-01-11 01:52:54 -0500338 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500339 atomic_set(&phba->num_rsrc_err, 0);
340 atomic_set(&phba->num_cmd_success, 0);
341}
342
James Smart9bad7672008-12-04 22:39:02 -0500343/**
James Smart3621a712009-04-06 18:47:14 -0400344 * lpfc_scsi_dev_block - set all scsi hosts to block state
James Smarta8e497d2008-08-24 21:50:11 -0400345 * @phba: Pointer to HBA context object.
346 *
347 * This function walks vport list and set each SCSI host to block state
348 * by invoking fc_remote_port_delete() routine. This function is invoked
349 * with EEH when device's PCI slot has been permanently disabled.
350 **/
351void
352lpfc_scsi_dev_block(struct lpfc_hba *phba)
353{
354 struct lpfc_vport **vports;
355 struct Scsi_Host *shost;
356 struct scsi_device *sdev;
357 struct fc_rport *rport;
358 int i;
359
360 vports = lpfc_create_vport_work_array(phba);
361 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400362 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8e497d2008-08-24 21:50:11 -0400363 shost = lpfc_shost_from_vport(vports[i]);
364 shost_for_each_device(sdev, shost) {
365 rport = starget_to_rport(scsi_target(sdev));
366 fc_remote_port_delete(rport);
367 }
368 }
369 lpfc_destroy_vport_work_array(phba, vports);
370}
371
James Smart9bad7672008-12-04 22:39:02 -0500372/**
James Smart3772a992009-05-22 14:50:54 -0400373 * lpfc_new_scsi_buf_s3 - Scsi buffer allocator for HBA with SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -0500374 * @vport: The virtual port for which this call being executed.
James Smart3772a992009-05-22 14:50:54 -0400375 * @num_to_allocate: The requested number of buffers to allocate.
James Smart9bad7672008-12-04 22:39:02 -0500376 *
James Smart3772a992009-05-22 14:50:54 -0400377 * This routine allocates a scsi buffer for device with SLI-3 interface spec,
378 * the scsi buffer contains all the necessary information needed to initiate
379 * a SCSI I/O. The non-DMAable buffer region contains information to build
380 * the IOCB. The DMAable region contains memory for the FCP CMND, FCP RSP,
381 * and the initial BPL. In addition to allocating memory, the FCP CMND and
382 * FCP RSP BDEs are setup in the BPL and the BPL BDE is setup in the IOCB.
James Smart9bad7672008-12-04 22:39:02 -0500383 *
384 * Return codes:
James Smart3772a992009-05-22 14:50:54 -0400385 * int - number of scsi buffers that were allocated.
386 * 0 = failure, less than num_to_alloc is a partial failure.
James Smart9bad7672008-12-04 22:39:02 -0500387 **/
James Smart3772a992009-05-22 14:50:54 -0400388static int
389lpfc_new_scsi_buf_s3(struct lpfc_vport *vport, int num_to_alloc)
dea31012005-04-17 16:05:31 -0500390{
James Smart2e0fef82007-06-17 19:56:36 -0500391 struct lpfc_hba *phba = vport->phba;
James Smartc4908502019-01-28 11:14:28 -0800392 struct lpfc_io_buf *psb;
dea31012005-04-17 16:05:31 -0500393 struct ulp_bde64 *bpl;
394 IOCB_t *iocb;
James Smart34b02dc2008-08-24 21:49:55 -0400395 dma_addr_t pdma_phys_fcp_cmd;
396 dma_addr_t pdma_phys_fcp_rsp;
James Smart0794d602019-01-28 11:14:19 -0800397 dma_addr_t pdma_phys_sgl;
James Bottomley604a3e32005-10-29 10:28:33 -0500398 uint16_t iotag;
James Smart96f70772013-04-17 20:16:15 -0400399 int bcnt, bpl_size;
400
401 bpl_size = phba->cfg_sg_dma_buf_size -
402 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
403
404 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
405 "9067 ALLOC %d scsi_bufs: %d (%d + %d + %d)\n",
406 num_to_alloc, phba->cfg_sg_dma_buf_size,
407 (int)sizeof(struct fcp_cmnd),
408 (int)sizeof(struct fcp_rsp), bpl_size);
dea31012005-04-17 16:05:31 -0500409
James Smart3772a992009-05-22 14:50:54 -0400410 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
James Smartc4908502019-01-28 11:14:28 -0800411 psb = kzalloc(sizeof(struct lpfc_io_buf), GFP_KERNEL);
James Smart3772a992009-05-22 14:50:54 -0400412 if (!psb)
413 break;
dea31012005-04-17 16:05:31 -0500414
James Smart3772a992009-05-22 14:50:54 -0400415 /*
416 * Get memory from the pci pool to map the virt space to pci
417 * bus space for an I/O. The DMA buffer includes space for the
418 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
419 * necessary to support the sg_tablesize.
420 */
Romain Perier771db5c2017-07-06 10:13:05 +0200421 psb->data = dma_pool_zalloc(phba->lpfc_sg_dma_buf_pool,
James Smart3772a992009-05-22 14:50:54 -0400422 GFP_KERNEL, &psb->dma_handle);
423 if (!psb->data) {
424 kfree(psb);
425 break;
426 }
dea31012005-04-17 16:05:31 -0500427
dea31012005-04-17 16:05:31 -0500428
James Smart3772a992009-05-22 14:50:54 -0400429 /* Allocate iotag for psb->cur_iocbq. */
430 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
431 if (iotag == 0) {
Romain Perier771db5c2017-07-06 10:13:05 +0200432 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
James Smart895427b2017-02-12 13:52:30 -0800433 psb->data, psb->dma_handle);
James Smart3772a992009-05-22 14:50:54 -0400434 kfree(psb);
435 break;
436 }
437 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Bottomley604a3e32005-10-29 10:28:33 -0500438
James Smart3772a992009-05-22 14:50:54 -0400439 psb->fcp_cmnd = psb->data;
440 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
James Smart0794d602019-01-28 11:14:19 -0800441 psb->dma_sgl = psb->data + sizeof(struct fcp_cmnd) +
James Smart34b02dc2008-08-24 21:49:55 -0400442 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500443
James Smart3772a992009-05-22 14:50:54 -0400444 /* Initialize local short-hand pointers. */
James Smartc4908502019-01-28 11:14:28 -0800445 bpl = (struct ulp_bde64 *)psb->dma_sgl;
James Smart3772a992009-05-22 14:50:54 -0400446 pdma_phys_fcp_cmd = psb->dma_handle;
447 pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
James Smart0794d602019-01-28 11:14:19 -0800448 pdma_phys_sgl = psb->dma_handle + sizeof(struct fcp_cmnd) +
James Smart3772a992009-05-22 14:50:54 -0400449 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500450
James Smart3772a992009-05-22 14:50:54 -0400451 /*
452 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
453 * are sg list bdes. Initialize the first two and leave the
454 * rest for queuecommand.
455 */
456 bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
457 bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
458 bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
459 bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
460 bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
dea31012005-04-17 16:05:31 -0500461
James Smart3772a992009-05-22 14:50:54 -0400462 /* Setup the physical region for the FCP RSP */
463 bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
464 bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
465 bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
466 bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
467 bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
468
469 /*
470 * Since the IOCB for the FCP I/O is built into this
471 * lpfc_scsi_buf, initialize it with all known data now.
472 */
473 iocb = &psb->cur_iocbq.iocb;
474 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
475 if ((phba->sli_rev == 3) &&
476 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
477 /* fill in immediate fcp command BDE */
478 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
479 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
480 iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
481 unsli3.fcp_ext.icd);
482 iocb->un.fcpi64.bdl.addrHigh = 0;
483 iocb->ulpBdeCount = 0;
484 iocb->ulpLe = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300485 /* fill in response BDE */
James Smart3772a992009-05-22 14:50:54 -0400486 iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags =
487 BUFF_TYPE_BDE_64;
488 iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
489 sizeof(struct fcp_rsp);
490 iocb->unsli3.fcp_ext.rbde.addrLow =
491 putPaddrLow(pdma_phys_fcp_rsp);
492 iocb->unsli3.fcp_ext.rbde.addrHigh =
493 putPaddrHigh(pdma_phys_fcp_rsp);
494 } else {
495 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
496 iocb->un.fcpi64.bdl.bdeSize =
497 (2 * sizeof(struct ulp_bde64));
498 iocb->un.fcpi64.bdl.addrLow =
James Smart0794d602019-01-28 11:14:19 -0800499 putPaddrLow(pdma_phys_sgl);
James Smart3772a992009-05-22 14:50:54 -0400500 iocb->un.fcpi64.bdl.addrHigh =
James Smart0794d602019-01-28 11:14:19 -0800501 putPaddrHigh(pdma_phys_sgl);
James Smart3772a992009-05-22 14:50:54 -0400502 iocb->ulpBdeCount = 1;
503 iocb->ulpLe = 1;
504 }
505 iocb->ulpClass = CLASS3;
506 psb->status = IOSTAT_SUCCESS;
James Smartda0436e2009-05-22 14:51:39 -0400507 /* Put it back into the SCSI buffer list */
James Smarteee88772010-09-29 11:19:08 -0400508 psb->cur_iocbq.context1 = psb;
James Smartc2017262019-01-28 11:14:37 -0800509 spin_lock_init(&psb->buf_lock);
James Smart1c6f4ef52009-11-18 15:40:49 -0500510 lpfc_release_scsi_buf_s3(phba, psb);
James Smart3772a992009-05-22 14:50:54 -0400511
James Smart34b02dc2008-08-24 21:49:55 -0400512 }
dea31012005-04-17 16:05:31 -0500513
James Smart3772a992009-05-22 14:50:54 -0400514 return bcnt;
dea31012005-04-17 16:05:31 -0500515}
516
James Smart9bad7672008-12-04 22:39:02 -0500517/**
James Smart1151e3e2011-02-16 12:39:35 -0500518 * lpfc_sli4_vport_delete_fcp_xri_aborted -Remove all ndlp references for vport
519 * @vport: pointer to lpfc vport data structure.
520 *
521 * This routine is invoked by the vport cleanup for deletions and the cleanup
522 * for an ndlp on removal.
523 **/
524void
525lpfc_sli4_vport_delete_fcp_xri_aborted(struct lpfc_vport *vport)
526{
527 struct lpfc_hba *phba = vport->phba;
James Smartc4908502019-01-28 11:14:28 -0800528 struct lpfc_io_buf *psb, *next_psb;
James Smart5e5b5112019-01-28 11:14:22 -0800529 struct lpfc_sli4_hdw_queue *qp;
James Smart1151e3e2011-02-16 12:39:35 -0500530 unsigned long iflag = 0;
James Smart5e5b5112019-01-28 11:14:22 -0800531 int idx;
James Smart1151e3e2011-02-16 12:39:35 -0500532
James Smartf6e84792019-01-28 11:14:38 -0800533 if (!(vport->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
James Smart895427b2017-02-12 13:52:30 -0800534 return;
James Smart5e5b5112019-01-28 11:14:22 -0800535
James Smart1151e3e2011-02-16 12:39:35 -0500536 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart5e5b5112019-01-28 11:14:22 -0800537 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
538 qp = &phba->sli4_hba.hdwq[idx];
539
540 spin_lock(&qp->abts_scsi_buf_list_lock);
541 list_for_each_entry_safe(psb, next_psb,
542 &qp->lpfc_abts_scsi_buf_list, list) {
543 if (psb->rdata && psb->rdata->pnode &&
544 psb->rdata->pnode->vport == vport)
545 psb->rdata = NULL;
546 }
547 spin_unlock(&qp->abts_scsi_buf_list_lock);
James Smart1151e3e2011-02-16 12:39:35 -0500548 }
James Smart1151e3e2011-02-16 12:39:35 -0500549 spin_unlock_irqrestore(&phba->hbalock, iflag);
550}
551
552/**
James Smartda0436e2009-05-22 14:51:39 -0400553 * lpfc_sli4_fcp_xri_aborted - Fast-path process of fcp xri abort
554 * @phba: pointer to lpfc hba data structure.
555 * @axri: pointer to the fcp xri abort wcqe structure.
556 *
557 * This routine is invoked by the worker thread to process a SLI4 fast-path
558 * FCP aborted xri.
559 **/
560void
561lpfc_sli4_fcp_xri_aborted(struct lpfc_hba *phba,
James Smart5e5b5112019-01-28 11:14:22 -0800562 struct sli4_wcqe_xri_aborted *axri, int idx)
James Smartda0436e2009-05-22 14:51:39 -0400563{
564 uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
James Smart19ca7602010-11-20 23:11:55 -0500565 uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
James Smartc4908502019-01-28 11:14:28 -0800566 struct lpfc_io_buf *psb, *next_psb;
James Smart5e5b5112019-01-28 11:14:22 -0800567 struct lpfc_sli4_hdw_queue *qp;
James Smartda0436e2009-05-22 14:51:39 -0400568 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -0500569 struct lpfc_iocbq *iocbq;
570 int i;
James Smart19ca7602010-11-20 23:11:55 -0500571 struct lpfc_nodelist *ndlp;
572 int rrq_empty = 0;
James Smart895427b2017-02-12 13:52:30 -0800573 struct lpfc_sli_ring *pring = phba->sli4_hba.els_wq->pring;
James Smartda0436e2009-05-22 14:51:39 -0400574
James Smart895427b2017-02-12 13:52:30 -0800575 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
576 return;
James Smart5e5b5112019-01-28 11:14:22 -0800577
578 qp = &phba->sli4_hba.hdwq[idx];
James Smart0f65ff62010-02-26 14:14:23 -0500579 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart5e5b5112019-01-28 11:14:22 -0800580 spin_lock(&qp->abts_scsi_buf_list_lock);
James Smartda0436e2009-05-22 14:51:39 -0400581 list_for_each_entry_safe(psb, next_psb,
James Smart5e5b5112019-01-28 11:14:22 -0800582 &qp->lpfc_abts_scsi_buf_list, list) {
James Smartda0436e2009-05-22 14:51:39 -0400583 if (psb->cur_iocbq.sli4_xritag == xri) {
584 list_del(&psb->list);
James Smart5e5b5112019-01-28 11:14:22 -0800585 qp->abts_scsi_io_bufs--;
James Smart341af102010-01-26 23:07:37 -0500586 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400587 psb->status = IOSTAT_SUCCESS;
James Smart0f65ff62010-02-26 14:14:23 -0500588 spin_unlock(
James Smart5e5b5112019-01-28 11:14:22 -0800589 &qp->abts_scsi_buf_list_lock);
James Smart1151e3e2011-02-16 12:39:35 -0500590 if (psb->rdata && psb->rdata->pnode)
591 ndlp = psb->rdata->pnode;
592 else
593 ndlp = NULL;
594
James Smart19ca7602010-11-20 23:11:55 -0500595 rrq_empty = list_empty(&phba->active_rrq_list);
James Smart0f65ff62010-02-26 14:14:23 -0500596 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartcb69f7d2011-12-13 13:21:57 -0500597 if (ndlp) {
James Smartee0f4fe2012-05-09 21:19:14 -0400598 lpfc_set_rrq_active(phba, ndlp,
599 psb->cur_iocbq.sli4_lxritag, rxid, 1);
James Smartcb69f7d2011-12-13 13:21:57 -0500600 lpfc_sli4_abts_err_handler(phba, ndlp, axri);
601 }
James Smartda0436e2009-05-22 14:51:39 -0400602 lpfc_release_scsi_buf_s4(phba, psb);
James Smart19ca7602010-11-20 23:11:55 -0500603 if (rrq_empty)
604 lpfc_worker_wake_up(phba);
James Smartda0436e2009-05-22 14:51:39 -0400605 return;
606 }
607 }
James Smart5e5b5112019-01-28 11:14:22 -0800608 spin_unlock(&qp->abts_scsi_buf_list_lock);
James Smart0f65ff62010-02-26 14:14:23 -0500609 for (i = 1; i <= phba->sli.last_iotag; i++) {
610 iocbq = phba->sli.iocbq_lookup[i];
611
612 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
613 (iocbq->iocb_flag & LPFC_IO_LIBDFC))
614 continue;
615 if (iocbq->sli4_xritag != xri)
616 continue;
James Smartc4908502019-01-28 11:14:28 -0800617 psb = container_of(iocbq, struct lpfc_io_buf, cur_iocbq);
James Smart0f65ff62010-02-26 14:14:23 -0500618 psb->exch_busy = 0;
619 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart0e9bb8d2013-03-01 16:35:12 -0500620 if (!list_empty(&pring->txq))
James Smart589a52d2010-07-14 15:30:54 -0400621 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -0500622 return;
623
624 }
625 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartda0436e2009-05-22 14:51:39 -0400626}
627
628/**
James Smart19ca7602010-11-20 23:11:55 -0500629 * lpfc_get_scsi_buf_s3 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
James Smart3772a992009-05-22 14:50:54 -0400630 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -0500631 *
632 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
633 * and returns to caller.
634 *
635 * Return codes:
636 * NULL - Error
637 * Pointer to lpfc_scsi_buf - Success
638 **/
James Smartc4908502019-01-28 11:14:28 -0800639static struct lpfc_io_buf *
James Smartace44e42019-01-28 11:14:27 -0800640lpfc_get_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
641 struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -0500642{
James Smartc4908502019-01-28 11:14:28 -0800643 struct lpfc_io_buf *lpfc_cmd = NULL;
James Smarta40fc5f2013-04-17 20:17:40 -0400644 struct list_head *scsi_buf_list_get = &phba->lpfc_scsi_buf_list_get;
James Smart164cecd2013-09-06 12:22:38 -0400645 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500646
James Smart164cecd2013-09-06 12:22:38 -0400647 spin_lock_irqsave(&phba->scsi_buf_list_get_lock, iflag);
James Smartc4908502019-01-28 11:14:28 -0800648 list_remove_head(scsi_buf_list_get, lpfc_cmd, struct lpfc_io_buf,
James Smarta40fc5f2013-04-17 20:17:40 -0400649 list);
650 if (!lpfc_cmd) {
James Smart164cecd2013-09-06 12:22:38 -0400651 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -0400652 list_splice(&phba->lpfc_scsi_buf_list_put,
653 &phba->lpfc_scsi_buf_list_get);
654 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
655 list_remove_head(scsi_buf_list_get, lpfc_cmd,
James Smartc4908502019-01-28 11:14:28 -0800656 struct lpfc_io_buf, list);
James Smart164cecd2013-09-06 12:22:38 -0400657 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smart1dcb58e2007-04-25 09:51:30 -0400658 }
James Smart164cecd2013-09-06 12:22:38 -0400659 spin_unlock_irqrestore(&phba->scsi_buf_list_get_lock, iflag);
James Smart2a5b7d62018-07-31 17:23:22 -0700660
661 if (lpfc_ndlp_check_qdepth(phba, ndlp) && lpfc_cmd) {
662 atomic_inc(&ndlp->cmd_pending);
663 lpfc_cmd->flags |= LPFC_SBUF_BUMP_QDEPTH;
664 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400665 return lpfc_cmd;
666}
James Smart19ca7602010-11-20 23:11:55 -0500667/**
James Smart5e5b5112019-01-28 11:14:22 -0800668 * lpfc_get_scsi_buf_s4 - Get a scsi buffer from io_buf_list of the HBA
James Smart19ca7602010-11-20 23:11:55 -0500669 * @phba: The HBA for which this call is being executed.
670 *
James Smart5e5b5112019-01-28 11:14:22 -0800671 * This routine removes a scsi buffer from head of @hdwq io_buf_list
James Smart19ca7602010-11-20 23:11:55 -0500672 * and returns to caller.
673 *
674 * Return codes:
675 * NULL - Error
676 * Pointer to lpfc_scsi_buf - Success
677 **/
James Smartc4908502019-01-28 11:14:28 -0800678static struct lpfc_io_buf *
James Smartace44e42019-01-28 11:14:27 -0800679lpfc_get_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
680 struct scsi_cmnd *cmnd)
James Smart19ca7602010-11-20 23:11:55 -0500681{
James Smartc4908502019-01-28 11:14:28 -0800682 struct lpfc_io_buf *lpfc_cmd;
James Smart5e5b5112019-01-28 11:14:22 -0800683 struct lpfc_sli4_hdw_queue *qp;
James Smart0794d602019-01-28 11:14:19 -0800684 struct sli4_sge *sgl;
685 IOCB_t *iocb;
686 dma_addr_t pdma_phys_fcp_rsp;
687 dma_addr_t pdma_phys_fcp_cmd;
James Smart5e5b5112019-01-28 11:14:22 -0800688 uint32_t sgl_size, cpu, idx;
James Smartace44e42019-01-28 11:14:27 -0800689 int tag;
James Smart19ca7602010-11-20 23:11:55 -0500690
James Smart5e5b5112019-01-28 11:14:22 -0800691 cpu = smp_processor_id();
James Smart45aa3122019-01-28 11:14:29 -0800692 if (cmnd && phba->cfg_fcp_io_sched == LPFC_FCP_SCHED_BY_HDWQ) {
James Smartace44e42019-01-28 11:14:27 -0800693 tag = blk_mq_unique_tag(cmnd->request);
694 idx = blk_mq_unique_tag_to_hwq(tag);
695 } else {
James Smart6a828b02019-01-28 11:14:31 -0800696 idx = phba->sli4_hba.cpu_map[cpu].hdwq;
James Smartace44e42019-01-28 11:14:27 -0800697 }
James Smart5e5b5112019-01-28 11:14:22 -0800698
James Smartc4908502019-01-28 11:14:28 -0800699 lpfc_cmd = lpfc_get_io_buf(phba, ndlp, idx,
700 !phba->cfg_xri_rebalancing);
701 if (!lpfc_cmd) {
702 qp = &phba->sli4_hba.hdwq[idx];
James Smart5e5b5112019-01-28 11:14:22 -0800703 qp->empty_io_bufs++;
James Smart1151e3e2011-02-16 12:39:35 -0500704 return NULL;
James Smart5e5b5112019-01-28 11:14:22 -0800705 }
James Smart2a5b7d62018-07-31 17:23:22 -0700706
James Smart0794d602019-01-28 11:14:19 -0800707 sgl_size = phba->cfg_sg_dma_buf_size -
708 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
709
710 /* Setup key fields in buffer that may have been changed
711 * if other protocols used this buffer.
712 */
713 lpfc_cmd->cur_iocbq.iocb_flag = LPFC_IO_FCP;
714 lpfc_cmd->prot_seg_cnt = 0;
715 lpfc_cmd->seg_cnt = 0;
James Smart0794d602019-01-28 11:14:19 -0800716 lpfc_cmd->timeout = 0;
717 lpfc_cmd->flags = 0;
718 lpfc_cmd->start_time = jiffies;
719 lpfc_cmd->waitq = NULL;
James Smart5e5b5112019-01-28 11:14:22 -0800720 lpfc_cmd->cpu = cpu;
James Smart0794d602019-01-28 11:14:19 -0800721#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
722 lpfc_cmd->prot_data_type = 0;
723#endif
James Smart0794d602019-01-28 11:14:19 -0800724 lpfc_cmd->fcp_cmnd = (lpfc_cmd->data + sgl_size);
725 lpfc_cmd->fcp_rsp = (struct fcp_rsp *)((uint8_t *)lpfc_cmd->fcp_cmnd +
726 sizeof(struct fcp_cmnd));
727
728 /*
729 * The first two SGEs are the FCP_CMD and FCP_RSP.
730 * The balance are sg list bdes. Initialize the
731 * first two and leave the rest for queuecommand.
732 */
733 sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
734 pdma_phys_fcp_cmd = (lpfc_cmd->dma_handle + sgl_size);
735 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
736 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
737 sgl->word2 = le32_to_cpu(sgl->word2);
738 bf_set(lpfc_sli4_sge_last, sgl, 0);
739 sgl->word2 = cpu_to_le32(sgl->word2);
740 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_cmnd));
741 sgl++;
742
743 /* Setup the physical region for the FCP RSP */
744 pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
745 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
746 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
747 sgl->word2 = le32_to_cpu(sgl->word2);
748 bf_set(lpfc_sli4_sge_last, sgl, 1);
749 sgl->word2 = cpu_to_le32(sgl->word2);
750 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
751
752 /*
753 * Since the IOCB for the FCP I/O is built into this
James Smartc4908502019-01-28 11:14:28 -0800754 * lpfc_io_buf, initialize it with all known data now.
James Smart0794d602019-01-28 11:14:19 -0800755 */
756 iocb = &lpfc_cmd->cur_iocbq.iocb;
757 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
758 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
759 /* setting the BLP size to 2 * sizeof BDE may not be correct.
760 * We are setting the bpl to point to out sgl. An sgl's
761 * entries are 16 bytes, a bpl entries are 12 bytes.
762 */
763 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
764 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
765 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
766 iocb->ulpBdeCount = 1;
767 iocb->ulpLe = 1;
768 iocb->ulpClass = CLASS3;
769
Dan Carpenter26c724a2018-08-23 16:56:33 +0300770 if (lpfc_ndlp_check_qdepth(phba, ndlp)) {
James Smart2a5b7d62018-07-31 17:23:22 -0700771 atomic_inc(&ndlp->cmd_pending);
772 lpfc_cmd->flags |= LPFC_SBUF_BUMP_QDEPTH;
773 }
James Smarta40fc5f2013-04-17 20:17:40 -0400774 return lpfc_cmd;
James Smart19ca7602010-11-20 23:11:55 -0500775}
776/**
777 * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
778 * @phba: The HBA for which this call is being executed.
779 *
780 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
781 * and returns to caller.
782 *
783 * Return codes:
784 * NULL - Error
785 * Pointer to lpfc_scsi_buf - Success
786 **/
James Smartc4908502019-01-28 11:14:28 -0800787static struct lpfc_io_buf*
James Smartace44e42019-01-28 11:14:27 -0800788lpfc_get_scsi_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
789 struct scsi_cmnd *cmnd)
James Smart19ca7602010-11-20 23:11:55 -0500790{
James Smartace44e42019-01-28 11:14:27 -0800791 return phba->lpfc_get_scsi_buf(phba, ndlp, cmnd);
James Smart19ca7602010-11-20 23:11:55 -0500792}
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400793
James Smart9bad7672008-12-04 22:39:02 -0500794/**
James Smart3772a992009-05-22 14:50:54 -0400795 * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
James Smart9bad7672008-12-04 22:39:02 -0500796 * @phba: The Hba for which this call is being executed.
797 * @psb: The scsi buffer which is being released.
798 *
799 * This routine releases @psb scsi buffer by adding it to tail of @phba
800 * lpfc_scsi_buf_list list.
801 **/
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400802static void
James Smartc4908502019-01-28 11:14:28 -0800803lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_io_buf *psb)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400804{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500805 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500806
James Smarta40fc5f2013-04-17 20:17:40 -0400807 psb->seg_cnt = 0;
James Smarta40fc5f2013-04-17 20:17:40 -0400808 psb->prot_seg_cnt = 0;
809
810 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400811 psb->pCmd = NULL;
James Smart6a485eb2013-04-17 20:19:00 -0400812 psb->cur_iocbq.iocb_flag = LPFC_IO_FCP;
James Smarta40fc5f2013-04-17 20:17:40 -0400813 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list_put);
814 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
dea31012005-04-17 16:05:31 -0500815}
816
James Smart9bad7672008-12-04 22:39:02 -0500817/**
James Smartda0436e2009-05-22 14:51:39 -0400818 * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
819 * @phba: The Hba for which this call is being executed.
820 * @psb: The scsi buffer which is being released.
821 *
James Smart5e5b5112019-01-28 11:14:22 -0800822 * This routine releases @psb scsi buffer by adding it to tail of @hdwq
823 * io_buf_list list. For SLI4 XRI's are tied to the scsi buffer
James Smartda0436e2009-05-22 14:51:39 -0400824 * and cannot be reused for at least RA_TOV amount of time if it was
825 * aborted.
826 **/
827static void
James Smartc4908502019-01-28 11:14:28 -0800828lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_io_buf *psb)
James Smartda0436e2009-05-22 14:51:39 -0400829{
James Smart5e5b5112019-01-28 11:14:22 -0800830 struct lpfc_sli4_hdw_queue *qp;
James Smartda0436e2009-05-22 14:51:39 -0400831 unsigned long iflag = 0;
832
James Smarta40fc5f2013-04-17 20:17:40 -0400833 psb->seg_cnt = 0;
James Smarta40fc5f2013-04-17 20:17:40 -0400834 psb->prot_seg_cnt = 0;
835
James Smart1fbf9742019-01-28 11:14:26 -0800836 qp = psb->hdwq;
James Smart341af102010-01-26 23:07:37 -0500837 if (psb->exch_busy) {
James Smart5e5b5112019-01-28 11:14:22 -0800838 spin_lock_irqsave(&qp->abts_scsi_buf_list_lock, iflag);
James Smartda0436e2009-05-22 14:51:39 -0400839 psb->pCmd = NULL;
James Smart5e5b5112019-01-28 11:14:22 -0800840 list_add_tail(&psb->list, &qp->lpfc_abts_scsi_buf_list);
841 qp->abts_scsi_io_bufs++;
842 spin_unlock_irqrestore(&qp->abts_scsi_buf_list_lock, iflag);
James Smartda0436e2009-05-22 14:51:39 -0400843 } else {
James Smartc4908502019-01-28 11:14:28 -0800844 lpfc_release_io_buf(phba, (struct lpfc_io_buf *)psb, qp);
James Smartda0436e2009-05-22 14:51:39 -0400845 }
846}
847
848/**
James Smart3772a992009-05-22 14:50:54 -0400849 * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
850 * @phba: The Hba for which this call is being executed.
851 * @psb: The scsi buffer which is being released.
852 *
853 * This routine releases @psb scsi buffer by adding it to tail of @phba
854 * lpfc_scsi_buf_list list.
855 **/
856static void
James Smartc4908502019-01-28 11:14:28 -0800857lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_io_buf *psb)
James Smart3772a992009-05-22 14:50:54 -0400858{
James Smart2a5b7d62018-07-31 17:23:22 -0700859 if ((psb->flags & LPFC_SBUF_BUMP_QDEPTH) && psb->ndlp)
860 atomic_dec(&psb->ndlp->cmd_pending);
James Smart3772a992009-05-22 14:50:54 -0400861
James Smart2a5b7d62018-07-31 17:23:22 -0700862 psb->flags &= ~LPFC_SBUF_BUMP_QDEPTH;
James Smart3772a992009-05-22 14:50:54 -0400863 phba->lpfc_release_scsi_buf(phba, psb);
864}
865
866/**
867 * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -0500868 * @phba: The Hba for which this call is being executed.
869 * @lpfc_cmd: The scsi buffer which is going to be mapped.
870 *
871 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
James Smart3772a992009-05-22 14:50:54 -0400872 * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -0700873 * through sg elements and format the bde. This routine also initializes all
James Smart3772a992009-05-22 14:50:54 -0400874 * IOCB fields which are dependent on scsi command request buffer.
James Smart9bad7672008-12-04 22:39:02 -0500875 *
876 * Return codes:
877 * 1 - Error
878 * 0 - Success
879 **/
dea31012005-04-17 16:05:31 -0500880static int
James Smartc4908502019-01-28 11:14:28 -0800881lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
dea31012005-04-17 16:05:31 -0500882{
883 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
884 struct scatterlist *sgel = NULL;
885 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smartc4908502019-01-28 11:14:28 -0800886 struct ulp_bde64 *bpl = (struct ulp_bde64 *)lpfc_cmd->dma_sgl;
James Smart0f65ff62010-02-26 14:14:23 -0500887 struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
dea31012005-04-17 16:05:31 -0500888 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart34b02dc2008-08-24 21:49:55 -0400889 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
dea31012005-04-17 16:05:31 -0500890 dma_addr_t physaddr;
James Smart34b02dc2008-08-24 21:49:55 -0400891 uint32_t num_bde = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500892 int nseg, datadir = scsi_cmnd->sc_data_direction;
dea31012005-04-17 16:05:31 -0500893
894 /*
895 * There are three possibilities here - use scatter-gather segment, use
896 * the single mapping, or neither. Start the lpfc command prep by
897 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
898 * data bde entry.
899 */
900 bpl += 2;
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -0700901 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -0500902 /*
903 * The driver stores the segment count returned from pci_map_sg
904 * because this a count of dma-mappings used to map the use_sg
905 * pages. They are not guaranteed to be the same for those
906 * architectures that implement an IOMMU.
907 */
dea31012005-04-17 16:05:31 -0500908
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -0700909 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
910 scsi_sg_count(scsi_cmnd), datadir);
911 if (unlikely(!nseg))
912 return 1;
913
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500914 lpfc_cmd->seg_cnt = nseg;
dea31012005-04-17 16:05:31 -0500915 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -0400916 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
917 "9064 BLKGRD: %s: Too many sg segments from "
James Smarte2a0a9d2008-12-04 22:40:02 -0500918 "dma_map_sg. Config %d, seg_cnt %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700919 __func__, phba->cfg_sg_seg_cnt,
dea31012005-04-17 16:05:31 -0500920 lpfc_cmd->seg_cnt);
James Smart96f70772013-04-17 20:16:15 -0400921 lpfc_cmd->seg_cnt = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500922 scsi_dma_unmap(scsi_cmnd);
dea31012005-04-17 16:05:31 -0500923 return 1;
924 }
925
926 /*
927 * The driver established a maximum scatter-gather segment count
928 * during probe that limits the number of sg elements in any
929 * single scsi command. Just run through the seg_cnt and format
930 * the bde's.
James Smart34b02dc2008-08-24 21:49:55 -0400931 * When using SLI-3 the driver will try to fit all the BDEs into
932 * the IOCB. If it can't then the BDEs get added to a BPL as it
933 * does for SLI-2 mode.
dea31012005-04-17 16:05:31 -0500934 */
James Smart34b02dc2008-08-24 21:49:55 -0400935 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
dea31012005-04-17 16:05:31 -0500936 physaddr = sg_dma_address(sgel);
James Smart34b02dc2008-08-24 21:49:55 -0400937 if (phba->sli_rev == 3 &&
James Smarte2a0a9d2008-12-04 22:40:02 -0500938 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
James Smart0f65ff62010-02-26 14:14:23 -0500939 !(iocbq->iocb_flag & DSS_SECURITY_OP) &&
James Smart34b02dc2008-08-24 21:49:55 -0400940 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
941 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
942 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
943 data_bde->addrLow = putPaddrLow(physaddr);
944 data_bde->addrHigh = putPaddrHigh(physaddr);
945 data_bde++;
946 } else {
947 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
948 bpl->tus.f.bdeSize = sg_dma_len(sgel);
949 bpl->tus.w = le32_to_cpu(bpl->tus.w);
950 bpl->addrLow =
951 le32_to_cpu(putPaddrLow(physaddr));
952 bpl->addrHigh =
953 le32_to_cpu(putPaddrHigh(physaddr));
954 bpl++;
955 }
dea31012005-04-17 16:05:31 -0500956 }
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -0700957 }
dea31012005-04-17 16:05:31 -0500958
959 /*
960 * Finish initializing those IOCB fields that are dependent on the
James Smart34b02dc2008-08-24 21:49:55 -0400961 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
962 * explicitly reinitialized and for SLI-3 the extended bde count is
963 * explicitly reinitialized since all iocb memory resources are reused.
dea31012005-04-17 16:05:31 -0500964 */
James Smarte2a0a9d2008-12-04 22:40:02 -0500965 if (phba->sli_rev == 3 &&
James Smart0f65ff62010-02-26 14:14:23 -0500966 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
967 !(iocbq->iocb_flag & DSS_SECURITY_OP)) {
James Smart34b02dc2008-08-24 21:49:55 -0400968 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
969 /*
970 * The extended IOCB format can only fit 3 BDE or a BPL.
971 * This I/O has more than 3 BDE so the 1st data bde will
972 * be a BPL that is filled in here.
973 */
974 physaddr = lpfc_cmd->dma_handle;
975 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
976 data_bde->tus.f.bdeSize = (num_bde *
977 sizeof(struct ulp_bde64));
978 physaddr += (sizeof(struct fcp_cmnd) +
979 sizeof(struct fcp_rsp) +
980 (2 * sizeof(struct ulp_bde64)));
981 data_bde->addrHigh = putPaddrHigh(physaddr);
982 data_bde->addrLow = putPaddrLow(physaddr);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300983 /* ebde count includes the response bde and data bpl */
James Smart34b02dc2008-08-24 21:49:55 -0400984 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
985 } else {
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300986 /* ebde count includes the response bde and data bdes */
James Smart34b02dc2008-08-24 21:49:55 -0400987 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
988 }
989 } else {
990 iocb_cmd->un.fcpi64.bdl.bdeSize =
991 ((num_bde + 2) * sizeof(struct ulp_bde64));
James Smart0f65ff62010-02-26 14:14:23 -0500992 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
James Smart34b02dc2008-08-24 21:49:55 -0400993 }
James Smart09372822008-01-11 01:52:54 -0500994 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
James Smarte2a0a9d2008-12-04 22:40:02 -0500995
996 /*
997 * Due to difference in data length between DIF/non-DIF paths,
998 * we need to set word 4 of IOCB here
999 */
James Smarta257bf92009-04-06 18:48:10 -04001000 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001001 return 0;
1002}
1003
James Smartf9bb2da2011-10-10 21:34:11 -04001004#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001005
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -07001006/* Return BG_ERR_INIT if error injection is detected by Initiator */
James Smart9a6b09c2012-03-01 22:37:42 -05001007#define BG_ERR_INIT 0x1
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -07001008/* Return BG_ERR_TGT if error injection is detected by Target */
James Smart9a6b09c2012-03-01 22:37:42 -05001009#define BG_ERR_TGT 0x2
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -07001010/* Return BG_ERR_SWAP if swapping CSUM<-->CRC is required for error injection */
James Smart9a6b09c2012-03-01 22:37:42 -05001011#define BG_ERR_SWAP 0x10
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -07001012/**
1013 * Return BG_ERR_CHECK if disabling Guard/Ref/App checking is required for
1014 * error injection
1015 **/
James Smart9a6b09c2012-03-01 22:37:42 -05001016#define BG_ERR_CHECK 0x20
James Smartacd68592012-01-18 16:25:09 -05001017
1018/**
1019 * lpfc_bg_err_inject - Determine if we should inject an error
1020 * @phba: The Hba for which this call is being executed.
James Smartf9bb2da2011-10-10 21:34:11 -04001021 * @sc: The SCSI command to examine
1022 * @reftag: (out) BlockGuard reference tag for transmitted data
1023 * @apptag: (out) BlockGuard application tag for transmitted data
1024 * @new_guard (in) Value to replace CRC with if needed
1025 *
James Smart9a6b09c2012-03-01 22:37:42 -05001026 * Returns BG_ERR_* bit mask or 0 if request ignored
James Smartacd68592012-01-18 16:25:09 -05001027 **/
James Smartf9bb2da2011-10-10 21:34:11 -04001028static int
1029lpfc_bg_err_inject(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1030 uint32_t *reftag, uint16_t *apptag, uint32_t new_guard)
1031{
1032 struct scatterlist *sgpe; /* s/g prot entry */
James Smartc4908502019-01-28 11:14:28 -08001033 struct lpfc_io_buf *lpfc_cmd = NULL;
James Smartacd68592012-01-18 16:25:09 -05001034 struct scsi_dif_tuple *src = NULL;
James Smart4ac9b222012-03-01 22:38:29 -05001035 struct lpfc_nodelist *ndlp;
1036 struct lpfc_rport_data *rdata;
James Smartf9bb2da2011-10-10 21:34:11 -04001037 uint32_t op = scsi_get_prot_op(sc);
1038 uint32_t blksize;
1039 uint32_t numblks;
1040 sector_t lba;
1041 int rc = 0;
James Smartacd68592012-01-18 16:25:09 -05001042 int blockoff = 0;
James Smartf9bb2da2011-10-10 21:34:11 -04001043
1044 if (op == SCSI_PROT_NORMAL)
1045 return 0;
1046
James Smartacd68592012-01-18 16:25:09 -05001047 sgpe = scsi_prot_sglist(sc);
James Smartf9bb2da2011-10-10 21:34:11 -04001048 lba = scsi_get_lba(sc);
James Smart4ac9b222012-03-01 22:38:29 -05001049
1050 /* First check if we need to match the LBA */
James Smartf9bb2da2011-10-10 21:34:11 -04001051 if (phba->lpfc_injerr_lba != LPFC_INJERR_LBA_OFF) {
1052 blksize = lpfc_cmd_blksize(sc);
1053 numblks = (scsi_bufflen(sc) + blksize - 1) / blksize;
1054
1055 /* Make sure we have the right LBA if one is specified */
1056 if ((phba->lpfc_injerr_lba < lba) ||
1057 (phba->lpfc_injerr_lba >= (lba + numblks)))
1058 return 0;
James Smartacd68592012-01-18 16:25:09 -05001059 if (sgpe) {
1060 blockoff = phba->lpfc_injerr_lba - lba;
1061 numblks = sg_dma_len(sgpe) /
1062 sizeof(struct scsi_dif_tuple);
1063 if (numblks < blockoff)
1064 blockoff = numblks;
James Smartacd68592012-01-18 16:25:09 -05001065 }
James Smartf9bb2da2011-10-10 21:34:11 -04001066 }
1067
James Smart4ac9b222012-03-01 22:38:29 -05001068 /* Next check if we need to match the remote NPortID or WWPN */
James Smart1ba981f2014-02-20 09:56:45 -05001069 rdata = lpfc_rport_data_from_scsi_device(sc->device);
James Smart4ac9b222012-03-01 22:38:29 -05001070 if (rdata && rdata->pnode) {
1071 ndlp = rdata->pnode;
1072
1073 /* Make sure we have the right NPortID if one is specified */
1074 if (phba->lpfc_injerr_nportid &&
1075 (phba->lpfc_injerr_nportid != ndlp->nlp_DID))
1076 return 0;
1077
1078 /*
1079 * Make sure we have the right WWPN if one is specified.
1080 * wwn[0] should be a non-zero NAA in a good WWPN.
1081 */
1082 if (phba->lpfc_injerr_wwpn.u.wwn[0] &&
1083 (memcmp(&ndlp->nlp_portname, &phba->lpfc_injerr_wwpn,
1084 sizeof(struct lpfc_name)) != 0))
1085 return 0;
1086 }
1087
1088 /* Setup a ptr to the protection data if the SCSI host provides it */
1089 if (sgpe) {
1090 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
1091 src += blockoff;
James Smartc4908502019-01-28 11:14:28 -08001092 lpfc_cmd = (struct lpfc_io_buf *)sc->host_scribble;
James Smart4ac9b222012-03-01 22:38:29 -05001093 }
1094
James Smartf9bb2da2011-10-10 21:34:11 -04001095 /* Should we change the Reference Tag */
1096 if (reftag) {
James Smartacd68592012-01-18 16:25:09 -05001097 if (phba->lpfc_injerr_wref_cnt) {
1098 switch (op) {
1099 case SCSI_PROT_WRITE_PASS:
James Smart9a6b09c2012-03-01 22:37:42 -05001100 if (src) {
1101 /*
1102 * For WRITE_PASS, force the error
1103 * to be sent on the wire. It should
1104 * be detected by the Target.
1105 * If blockoff != 0 error will be
1106 * inserted in middle of the IO.
1107 */
James Smartf9bb2da2011-10-10 21:34:11 -04001108
James Smartacd68592012-01-18 16:25:09 -05001109 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1110 "9076 BLKGRD: Injecting reftag error: "
1111 "write lba x%lx + x%x oldrefTag x%x\n",
1112 (unsigned long)lba, blockoff,
James Smart9a6b09c2012-03-01 22:37:42 -05001113 be32_to_cpu(src->ref_tag));
James Smartacd68592012-01-18 16:25:09 -05001114
1115 /*
James Smart9a6b09c2012-03-01 22:37:42 -05001116 * Save the old ref_tag so we can
1117 * restore it on completion.
James Smartacd68592012-01-18 16:25:09 -05001118 */
James Smart9a6b09c2012-03-01 22:37:42 -05001119 if (lpfc_cmd) {
1120 lpfc_cmd->prot_data_type =
1121 LPFC_INJERR_REFTAG;
1122 lpfc_cmd->prot_data_segment =
1123 src;
1124 lpfc_cmd->prot_data =
1125 src->ref_tag;
1126 }
1127 src->ref_tag = cpu_to_be32(0xDEADBEEF);
James Smartacd68592012-01-18 16:25:09 -05001128 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001129 if (phba->lpfc_injerr_wref_cnt == 0) {
1130 phba->lpfc_injerr_nportid = 0;
1131 phba->lpfc_injerr_lba =
1132 LPFC_INJERR_LBA_OFF;
1133 memset(&phba->lpfc_injerr_wwpn,
1134 0, sizeof(struct lpfc_name));
1135 }
James Smart9a6b09c2012-03-01 22:37:42 -05001136 rc = BG_ERR_TGT | BG_ERR_CHECK;
1137
James Smartacd68592012-01-18 16:25:09 -05001138 break;
1139 }
Bart Van Asschecd05c152019-03-28 11:06:18 -07001140 /* fall through */
James Smart9a6b09c2012-03-01 22:37:42 -05001141 case SCSI_PROT_WRITE_INSERT:
1142 /*
1143 * For WRITE_INSERT, force the error
1144 * to be sent on the wire. It should be
1145 * detected by the Target.
1146 */
1147 /* DEADBEEF will be the reftag on the wire */
1148 *reftag = 0xDEADBEEF;
1149 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001150 if (phba->lpfc_injerr_wref_cnt == 0) {
1151 phba->lpfc_injerr_nportid = 0;
1152 phba->lpfc_injerr_lba =
1153 LPFC_INJERR_LBA_OFF;
1154 memset(&phba->lpfc_injerr_wwpn,
1155 0, sizeof(struct lpfc_name));
1156 }
James Smart9a6b09c2012-03-01 22:37:42 -05001157 rc = BG_ERR_TGT | BG_ERR_CHECK;
1158
1159 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1160 "9078 BLKGRD: Injecting reftag error: "
1161 "write lba x%lx\n", (unsigned long)lba);
1162 break;
James Smartacd68592012-01-18 16:25:09 -05001163 case SCSI_PROT_WRITE_STRIP:
1164 /*
1165 * For WRITE_STRIP and WRITE_PASS,
1166 * force the error on data
1167 * being copied from SLI-Host to SLI-Port.
1168 */
1169 *reftag = 0xDEADBEEF;
1170 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001171 if (phba->lpfc_injerr_wref_cnt == 0) {
1172 phba->lpfc_injerr_nportid = 0;
1173 phba->lpfc_injerr_lba =
1174 LPFC_INJERR_LBA_OFF;
1175 memset(&phba->lpfc_injerr_wwpn,
1176 0, sizeof(struct lpfc_name));
1177 }
James Smartacd68592012-01-18 16:25:09 -05001178 rc = BG_ERR_INIT;
1179
1180 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1181 "9077 BLKGRD: Injecting reftag error: "
1182 "write lba x%lx\n", (unsigned long)lba);
1183 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001184 }
James Smartacd68592012-01-18 16:25:09 -05001185 }
1186 if (phba->lpfc_injerr_rref_cnt) {
1187 switch (op) {
1188 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001189 case SCSI_PROT_READ_STRIP:
1190 case SCSI_PROT_READ_PASS:
1191 /*
1192 * For READ_STRIP and READ_PASS, force the
1193 * error on data being read off the wire. It
1194 * should force an IO error to the driver.
1195 */
James Smartf9bb2da2011-10-10 21:34:11 -04001196 *reftag = 0xDEADBEEF;
1197 phba->lpfc_injerr_rref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001198 if (phba->lpfc_injerr_rref_cnt == 0) {
1199 phba->lpfc_injerr_nportid = 0;
1200 phba->lpfc_injerr_lba =
1201 LPFC_INJERR_LBA_OFF;
1202 memset(&phba->lpfc_injerr_wwpn,
1203 0, sizeof(struct lpfc_name));
1204 }
James Smartacd68592012-01-18 16:25:09 -05001205 rc = BG_ERR_INIT;
James Smartf9bb2da2011-10-10 21:34:11 -04001206
1207 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smartacd68592012-01-18 16:25:09 -05001208 "9079 BLKGRD: Injecting reftag error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001209 "read lba x%lx\n", (unsigned long)lba);
James Smartacd68592012-01-18 16:25:09 -05001210 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001211 }
1212 }
1213 }
1214
1215 /* Should we change the Application Tag */
1216 if (apptag) {
James Smartacd68592012-01-18 16:25:09 -05001217 if (phba->lpfc_injerr_wapp_cnt) {
1218 switch (op) {
1219 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001220 if (src) {
James Smart9a6b09c2012-03-01 22:37:42 -05001221 /*
1222 * For WRITE_PASS, force the error
1223 * to be sent on the wire. It should
1224 * be detected by the Target.
1225 * If blockoff != 0 error will be
1226 * inserted in middle of the IO.
1227 */
1228
James Smartacd68592012-01-18 16:25:09 -05001229 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1230 "9080 BLKGRD: Injecting apptag error: "
1231 "write lba x%lx + x%x oldappTag x%x\n",
1232 (unsigned long)lba, blockoff,
James Smart9a6b09c2012-03-01 22:37:42 -05001233 be16_to_cpu(src->app_tag));
James Smartacd68592012-01-18 16:25:09 -05001234
1235 /*
James Smart9a6b09c2012-03-01 22:37:42 -05001236 * Save the old app_tag so we can
1237 * restore it on completion.
James Smartacd68592012-01-18 16:25:09 -05001238 */
James Smart9a6b09c2012-03-01 22:37:42 -05001239 if (lpfc_cmd) {
1240 lpfc_cmd->prot_data_type =
1241 LPFC_INJERR_APPTAG;
1242 lpfc_cmd->prot_data_segment =
1243 src;
1244 lpfc_cmd->prot_data =
1245 src->app_tag;
1246 }
1247 src->app_tag = cpu_to_be16(0xDEAD);
James Smartacd68592012-01-18 16:25:09 -05001248 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001249 if (phba->lpfc_injerr_wapp_cnt == 0) {
1250 phba->lpfc_injerr_nportid = 0;
1251 phba->lpfc_injerr_lba =
1252 LPFC_INJERR_LBA_OFF;
1253 memset(&phba->lpfc_injerr_wwpn,
1254 0, sizeof(struct lpfc_name));
1255 }
James Smart9a6b09c2012-03-01 22:37:42 -05001256 rc = BG_ERR_TGT | BG_ERR_CHECK;
James Smartacd68592012-01-18 16:25:09 -05001257 break;
1258 }
Bart Van Asschecd05c152019-03-28 11:06:18 -07001259 /* fall through */
James Smart9a6b09c2012-03-01 22:37:42 -05001260 case SCSI_PROT_WRITE_INSERT:
1261 /*
1262 * For WRITE_INSERT, force the
1263 * error to be sent on the wire. It should be
1264 * detected by the Target.
1265 */
1266 /* DEAD will be the apptag on the wire */
1267 *apptag = 0xDEAD;
1268 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001269 if (phba->lpfc_injerr_wapp_cnt == 0) {
1270 phba->lpfc_injerr_nportid = 0;
1271 phba->lpfc_injerr_lba =
1272 LPFC_INJERR_LBA_OFF;
1273 memset(&phba->lpfc_injerr_wwpn,
1274 0, sizeof(struct lpfc_name));
1275 }
James Smart9a6b09c2012-03-01 22:37:42 -05001276 rc = BG_ERR_TGT | BG_ERR_CHECK;
1277
1278 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1279 "0813 BLKGRD: Injecting apptag error: "
1280 "write lba x%lx\n", (unsigned long)lba);
1281 break;
James Smartacd68592012-01-18 16:25:09 -05001282 case SCSI_PROT_WRITE_STRIP:
1283 /*
1284 * For WRITE_STRIP and WRITE_PASS,
1285 * force the error on data
1286 * being copied from SLI-Host to SLI-Port.
1287 */
1288 *apptag = 0xDEAD;
1289 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001290 if (phba->lpfc_injerr_wapp_cnt == 0) {
1291 phba->lpfc_injerr_nportid = 0;
1292 phba->lpfc_injerr_lba =
1293 LPFC_INJERR_LBA_OFF;
1294 memset(&phba->lpfc_injerr_wwpn,
1295 0, sizeof(struct lpfc_name));
1296 }
James Smartacd68592012-01-18 16:25:09 -05001297 rc = BG_ERR_INIT;
1298
1299 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1300 "0812 BLKGRD: Injecting apptag error: "
1301 "write lba x%lx\n", (unsigned long)lba);
1302 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001303 }
James Smartacd68592012-01-18 16:25:09 -05001304 }
1305 if (phba->lpfc_injerr_rapp_cnt) {
1306 switch (op) {
1307 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001308 case SCSI_PROT_READ_STRIP:
1309 case SCSI_PROT_READ_PASS:
1310 /*
1311 * For READ_STRIP and READ_PASS, force the
1312 * error on data being read off the wire. It
1313 * should force an IO error to the driver.
1314 */
James Smartf9bb2da2011-10-10 21:34:11 -04001315 *apptag = 0xDEAD;
1316 phba->lpfc_injerr_rapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001317 if (phba->lpfc_injerr_rapp_cnt == 0) {
1318 phba->lpfc_injerr_nportid = 0;
1319 phba->lpfc_injerr_lba =
1320 LPFC_INJERR_LBA_OFF;
1321 memset(&phba->lpfc_injerr_wwpn,
1322 0, sizeof(struct lpfc_name));
1323 }
James Smartacd68592012-01-18 16:25:09 -05001324 rc = BG_ERR_INIT;
James Smartf9bb2da2011-10-10 21:34:11 -04001325
1326 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smartacd68592012-01-18 16:25:09 -05001327 "0814 BLKGRD: Injecting apptag error: "
1328 "read lba x%lx\n", (unsigned long)lba);
1329 break;
1330 }
1331 }
1332 }
1333
1334
1335 /* Should we change the Guard Tag */
1336 if (new_guard) {
1337 if (phba->lpfc_injerr_wgrd_cnt) {
1338 switch (op) {
1339 case SCSI_PROT_WRITE_PASS:
James Smart9a6b09c2012-03-01 22:37:42 -05001340 rc = BG_ERR_CHECK;
Bart Van Asschecd05c152019-03-28 11:06:18 -07001341 /* fall through */
James Smartacd68592012-01-18 16:25:09 -05001342
James Smartacd68592012-01-18 16:25:09 -05001343 case SCSI_PROT_WRITE_INSERT:
1344 /*
1345 * For WRITE_INSERT, force the
1346 * error to be sent on the wire. It should be
1347 * detected by the Target.
1348 */
1349 phba->lpfc_injerr_wgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001350 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1351 phba->lpfc_injerr_nportid = 0;
1352 phba->lpfc_injerr_lba =
1353 LPFC_INJERR_LBA_OFF;
1354 memset(&phba->lpfc_injerr_wwpn,
1355 0, sizeof(struct lpfc_name));
1356 }
James Smartacd68592012-01-18 16:25:09 -05001357
James Smart9a6b09c2012-03-01 22:37:42 -05001358 rc |= BG_ERR_TGT | BG_ERR_SWAP;
James Smartacd68592012-01-18 16:25:09 -05001359 /* Signals the caller to swap CRC->CSUM */
1360
1361 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1362 "0817 BLKGRD: Injecting guard error: "
1363 "write lba x%lx\n", (unsigned long)lba);
1364 break;
James Smart9a6b09c2012-03-01 22:37:42 -05001365 case SCSI_PROT_WRITE_STRIP:
1366 /*
1367 * For WRITE_STRIP and WRITE_PASS,
1368 * force the error on data
1369 * being copied from SLI-Host to SLI-Port.
1370 */
1371 phba->lpfc_injerr_wgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001372 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1373 phba->lpfc_injerr_nportid = 0;
1374 phba->lpfc_injerr_lba =
1375 LPFC_INJERR_LBA_OFF;
1376 memset(&phba->lpfc_injerr_wwpn,
1377 0, sizeof(struct lpfc_name));
1378 }
James Smart9a6b09c2012-03-01 22:37:42 -05001379
1380 rc = BG_ERR_INIT | BG_ERR_SWAP;
1381 /* Signals the caller to swap CRC->CSUM */
1382
1383 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1384 "0816 BLKGRD: Injecting guard error: "
1385 "write lba x%lx\n", (unsigned long)lba);
1386 break;
James Smartacd68592012-01-18 16:25:09 -05001387 }
1388 }
1389 if (phba->lpfc_injerr_rgrd_cnt) {
1390 switch (op) {
1391 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001392 case SCSI_PROT_READ_STRIP:
1393 case SCSI_PROT_READ_PASS:
1394 /*
1395 * For READ_STRIP and READ_PASS, force the
1396 * error on data being read off the wire. It
1397 * should force an IO error to the driver.
1398 */
James Smartacd68592012-01-18 16:25:09 -05001399 phba->lpfc_injerr_rgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001400 if (phba->lpfc_injerr_rgrd_cnt == 0) {
1401 phba->lpfc_injerr_nportid = 0;
1402 phba->lpfc_injerr_lba =
1403 LPFC_INJERR_LBA_OFF;
1404 memset(&phba->lpfc_injerr_wwpn,
1405 0, sizeof(struct lpfc_name));
1406 }
James Smartacd68592012-01-18 16:25:09 -05001407
James Smart9a6b09c2012-03-01 22:37:42 -05001408 rc = BG_ERR_INIT | BG_ERR_SWAP;
James Smartacd68592012-01-18 16:25:09 -05001409 /* Signals the caller to swap CRC->CSUM */
1410
1411 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1412 "0818 BLKGRD: Injecting guard error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001413 "read lba x%lx\n", (unsigned long)lba);
1414 }
1415 }
1416 }
1417
James Smartf9bb2da2011-10-10 21:34:11 -04001418 return rc;
1419}
1420#endif
1421
James Smartacd68592012-01-18 16:25:09 -05001422/**
1423 * lpfc_sc_to_bg_opcodes - Determine the BlockGuard opcodes to be used with
1424 * the specified SCSI command.
1425 * @phba: The Hba for which this call is being executed.
James Smart6c8eea52010-04-06 14:49:53 -04001426 * @sc: The SCSI command to examine
1427 * @txopt: (out) BlockGuard operation for transmitted data
1428 * @rxopt: (out) BlockGuard operation for received data
1429 *
1430 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1431 *
James Smartacd68592012-01-18 16:25:09 -05001432 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001433static int
James Smart6c8eea52010-04-06 14:49:53 -04001434lpfc_sc_to_bg_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1435 uint8_t *txop, uint8_t *rxop)
James Smarte2a0a9d2008-12-04 22:40:02 -05001436{
James Smart6c8eea52010-04-06 14:49:53 -04001437 uint8_t ret = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05001438
James Smart9c6aa9d2013-05-31 17:03:39 -04001439 if (lpfc_cmd_guard_csum(sc)) {
James Smarte2a0a9d2008-12-04 22:40:02 -05001440 switch (scsi_get_prot_op(sc)) {
1441 case SCSI_PROT_READ_INSERT:
1442 case SCSI_PROT_WRITE_STRIP:
James Smart6c8eea52010-04-06 14:49:53 -04001443 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001444 *txop = BG_OP_IN_CSUM_OUT_NODIF;
James Smarte2a0a9d2008-12-04 22:40:02 -05001445 break;
1446
1447 case SCSI_PROT_READ_STRIP:
1448 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001449 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001450 *txop = BG_OP_IN_NODIF_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001451 break;
1452
Martin K. Petersenc6af4042009-09-18 17:32:59 -04001453 case SCSI_PROT_READ_PASS:
1454 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001455 *rxop = BG_OP_IN_CRC_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001456 *txop = BG_OP_IN_CSUM_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001457 break;
1458
James Smarte2a0a9d2008-12-04 22:40:02 -05001459 case SCSI_PROT_NORMAL:
1460 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001461 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001462 "9063 BLKGRD: Bad op/guard:%d/IP combination\n",
1463 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001464 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001465 break;
1466
1467 }
James Smart7c56b9f2011-07-22 18:36:25 -04001468 } else {
James Smarte2a0a9d2008-12-04 22:40:02 -05001469 switch (scsi_get_prot_op(sc)) {
1470 case SCSI_PROT_READ_STRIP:
1471 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001472 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001473 *txop = BG_OP_IN_NODIF_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001474 break;
1475
1476 case SCSI_PROT_READ_PASS:
1477 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001478 *rxop = BG_OP_IN_CRC_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001479 *txop = BG_OP_IN_CRC_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001480 break;
1481
James Smarte2a0a9d2008-12-04 22:40:02 -05001482 case SCSI_PROT_READ_INSERT:
1483 case SCSI_PROT_WRITE_STRIP:
James Smart7c56b9f2011-07-22 18:36:25 -04001484 *rxop = BG_OP_IN_NODIF_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001485 *txop = BG_OP_IN_CRC_OUT_NODIF;
James Smart7c56b9f2011-07-22 18:36:25 -04001486 break;
1487
James Smarte2a0a9d2008-12-04 22:40:02 -05001488 case SCSI_PROT_NORMAL:
1489 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001490 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001491 "9075 BLKGRD: Bad op/guard:%d/CRC combination\n",
1492 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001493 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001494 break;
1495 }
James Smarte2a0a9d2008-12-04 22:40:02 -05001496 }
1497
James Smart6c8eea52010-04-06 14:49:53 -04001498 return ret;
James Smarte2a0a9d2008-12-04 22:40:02 -05001499}
1500
James Smartacd68592012-01-18 16:25:09 -05001501#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1502/**
1503 * lpfc_bg_err_opcodes - reDetermine the BlockGuard opcodes to be used with
1504 * the specified SCSI command in order to force a guard tag error.
1505 * @phba: The Hba for which this call is being executed.
1506 * @sc: The SCSI command to examine
1507 * @txopt: (out) BlockGuard operation for transmitted data
1508 * @rxopt: (out) BlockGuard operation for received data
1509 *
1510 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1511 *
1512 **/
1513static int
1514lpfc_bg_err_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1515 uint8_t *txop, uint8_t *rxop)
1516{
James Smartacd68592012-01-18 16:25:09 -05001517 uint8_t ret = 0;
1518
James Smart9c6aa9d2013-05-31 17:03:39 -04001519 if (lpfc_cmd_guard_csum(sc)) {
James Smartacd68592012-01-18 16:25:09 -05001520 switch (scsi_get_prot_op(sc)) {
1521 case SCSI_PROT_READ_INSERT:
1522 case SCSI_PROT_WRITE_STRIP:
James Smartacd68592012-01-18 16:25:09 -05001523 *rxop = BG_OP_IN_NODIF_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001524 *txop = BG_OP_IN_CRC_OUT_NODIF;
James Smartacd68592012-01-18 16:25:09 -05001525 break;
1526
1527 case SCSI_PROT_READ_STRIP:
1528 case SCSI_PROT_WRITE_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001529 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001530 *txop = BG_OP_IN_NODIF_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001531 break;
1532
1533 case SCSI_PROT_READ_PASS:
1534 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001535 *rxop = BG_OP_IN_CSUM_OUT_CRC;
James Smart9a6b09c2012-03-01 22:37:42 -05001536 *txop = BG_OP_IN_CRC_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001537 break;
1538
1539 case SCSI_PROT_NORMAL:
1540 default:
1541 break;
1542
1543 }
1544 } else {
1545 switch (scsi_get_prot_op(sc)) {
1546 case SCSI_PROT_READ_STRIP:
1547 case SCSI_PROT_WRITE_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001548 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001549 *txop = BG_OP_IN_NODIF_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001550 break;
1551
1552 case SCSI_PROT_READ_PASS:
1553 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001554 *rxop = BG_OP_IN_CSUM_OUT_CSUM;
James Smart9a6b09c2012-03-01 22:37:42 -05001555 *txop = BG_OP_IN_CSUM_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001556 break;
1557
1558 case SCSI_PROT_READ_INSERT:
1559 case SCSI_PROT_WRITE_STRIP:
James Smartacd68592012-01-18 16:25:09 -05001560 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001561 *txop = BG_OP_IN_CSUM_OUT_NODIF;
James Smartacd68592012-01-18 16:25:09 -05001562 break;
1563
1564 case SCSI_PROT_NORMAL:
1565 default:
1566 break;
1567 }
1568 }
1569
1570 return ret;
1571}
1572#endif
1573
1574/**
1575 * lpfc_bg_setup_bpl - Setup BlockGuard BPL with no protection data
1576 * @phba: The Hba for which this call is being executed.
1577 * @sc: pointer to scsi command we're working on
1578 * @bpl: pointer to buffer list for protection groups
1579 * @datacnt: number of segments of data that have been dma mapped
1580 *
1581 * This function sets up BPL buffer list for protection groups of
James Smarte2a0a9d2008-12-04 22:40:02 -05001582 * type LPFC_PG_TYPE_NO_DIF
1583 *
1584 * This is usually used when the HBA is instructed to generate
1585 * DIFs and insert them into data stream (or strip DIF from
1586 * incoming data stream)
1587 *
1588 * The buffer list consists of just one protection group described
1589 * below:
1590 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001591 * start of prot group --> | PDE_5 |
1592 * +-------------------------+
1593 * | PDE_6 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001594 * +-------------------------+
1595 * | Data BDE |
1596 * +-------------------------+
1597 * |more Data BDE's ... (opt)|
1598 * +-------------------------+
1599 *
James Smarte2a0a9d2008-12-04 22:40:02 -05001600 *
1601 * Note: Data s/g buffers have been dma mapped
James Smartacd68592012-01-18 16:25:09 -05001602 *
1603 * Returns the number of BDEs added to the BPL.
1604 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001605static int
1606lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1607 struct ulp_bde64 *bpl, int datasegcnt)
1608{
1609 struct scatterlist *sgde = NULL; /* s/g data entry */
James Smart6c8eea52010-04-06 14:49:53 -04001610 struct lpfc_pde5 *pde5 = NULL;
1611 struct lpfc_pde6 *pde6 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05001612 dma_addr_t physaddr;
James Smart6c8eea52010-04-06 14:49:53 -04001613 int i = 0, num_bde = 0, status;
James Smarte2a0a9d2008-12-04 22:40:02 -05001614 int datadir = sc->sc_data_direction;
James Smart0829a192012-05-09 21:18:12 -04001615#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001616 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04001617#endif
James Smartacd68592012-01-18 16:25:09 -05001618 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001619 uint32_t reftag;
James Smart6c8eea52010-04-06 14:49:53 -04001620 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05001621
James Smart6c8eea52010-04-06 14:49:53 -04001622 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1623 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05001624 goto out;
1625
James Smart6c8eea52010-04-06 14:49:53 -04001626 /* extract some info from the scsi command for pde*/
James Smartacd68592012-01-18 16:25:09 -05001627 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
James Smarte2a0a9d2008-12-04 22:40:02 -05001628
James Smartf9bb2da2011-10-10 21:34:11 -04001629#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05001630 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05001631 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05001632 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05001633 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05001634 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05001635 checking = 0;
1636 }
James Smartf9bb2da2011-10-10 21:34:11 -04001637#endif
1638
James Smart6c8eea52010-04-06 14:49:53 -04001639 /* setup PDE5 with what we have */
1640 pde5 = (struct lpfc_pde5 *) bpl;
1641 memset(pde5, 0, sizeof(struct lpfc_pde5));
1642 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05001643
James Smartbc739052010-08-04 16:11:18 -04001644 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04001645 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04001646 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04001647
James Smart6c8eea52010-04-06 14:49:53 -04001648 /* advance bpl and increment bde count */
1649 num_bde++;
1650 bpl++;
1651 pde6 = (struct lpfc_pde6 *) bpl;
1652
1653 /* setup PDE6 with the rest of the info */
1654 memset(pde6, 0, sizeof(struct lpfc_pde6));
1655 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
1656 bf_set(pde6_optx, pde6, txop);
1657 bf_set(pde6_oprx, pde6, rxop);
James Smarta6887e22013-04-17 20:18:07 -04001658
1659 /*
1660 * We only need to check the data on READs, for WRITEs
1661 * protection data is automatically generated, not checked.
1662 */
James Smart6c8eea52010-04-06 14:49:53 -04001663 if (datadir == DMA_FROM_DEVICE) {
James Smart9c6aa9d2013-05-31 17:03:39 -04001664 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
James Smarta6887e22013-04-17 20:18:07 -04001665 bf_set(pde6_ce, pde6, checking);
1666 else
1667 bf_set(pde6_ce, pde6, 0);
1668
James Smart9c6aa9d2013-05-31 17:03:39 -04001669 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04001670 bf_set(pde6_re, pde6, checking);
1671 else
1672 bf_set(pde6_re, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04001673 }
1674 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04001675 bf_set(pde6_ae, pde6, 0);
1676 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04001677
James Smartbc739052010-08-04 16:11:18 -04001678 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04001679 pde6->word0 = cpu_to_le32(pde6->word0);
1680 pde6->word1 = cpu_to_le32(pde6->word1);
1681 pde6->word2 = cpu_to_le32(pde6->word2);
1682
James Smart6c8eea52010-04-06 14:49:53 -04001683 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05001684 num_bde++;
1685 bpl++;
1686
1687 /* assumption: caller has already run dma_map_sg on command data */
1688 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
1689 physaddr = sg_dma_address(sgde);
1690 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
1691 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1692 bpl->tus.f.bdeSize = sg_dma_len(sgde);
1693 if (datadir == DMA_TO_DEVICE)
1694 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1695 else
1696 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1697 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1698 bpl++;
1699 num_bde++;
1700 }
1701
1702out:
1703 return num_bde;
1704}
1705
James Smartacd68592012-01-18 16:25:09 -05001706/**
1707 * lpfc_bg_setup_bpl_prot - Setup BlockGuard BPL with protection data
1708 * @phba: The Hba for which this call is being executed.
1709 * @sc: pointer to scsi command we're working on
1710 * @bpl: pointer to buffer list for protection groups
1711 * @datacnt: number of segments of data that have been dma mapped
1712 * @protcnt: number of segment of protection data that have been dma mapped
1713 *
1714 * This function sets up BPL buffer list for protection groups of
1715 * type LPFC_PG_TYPE_DIF
James Smarte2a0a9d2008-12-04 22:40:02 -05001716 *
1717 * This is usually used when DIFs are in their own buffers,
1718 * separate from the data. The HBA can then by instructed
1719 * to place the DIFs in the outgoing stream. For read operations,
1720 * The HBA could extract the DIFs and place it in DIF buffers.
1721 *
1722 * The buffer list for this type consists of one or more of the
1723 * protection groups described below:
1724 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001725 * start of first prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001726 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001727 * | PDE_6 |
1728 * +-------------------------+
1729 * | PDE_7 (Prot BDE) |
James Smarte2a0a9d2008-12-04 22:40:02 -05001730 * +-------------------------+
1731 * | Data BDE |
1732 * +-------------------------+
1733 * |more Data BDE's ... (opt)|
1734 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001735 * start of new prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001736 * +-------------------------+
1737 * | ... |
1738 * +-------------------------+
1739 *
James Smarte2a0a9d2008-12-04 22:40:02 -05001740 * Note: It is assumed that both data and protection s/g buffers have been
1741 * mapped for DMA
James Smartacd68592012-01-18 16:25:09 -05001742 *
1743 * Returns the number of BDEs added to the BPL.
1744 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001745static int
1746lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1747 struct ulp_bde64 *bpl, int datacnt, int protcnt)
1748{
1749 struct scatterlist *sgde = NULL; /* s/g data entry */
1750 struct scatterlist *sgpe = NULL; /* s/g prot entry */
James Smart6c8eea52010-04-06 14:49:53 -04001751 struct lpfc_pde5 *pde5 = NULL;
1752 struct lpfc_pde6 *pde6 = NULL;
James Smart7f860592011-03-11 16:05:52 -05001753 struct lpfc_pde7 *pde7 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05001754 dma_addr_t dataphysaddr, protphysaddr;
1755 unsigned short curr_data = 0, curr_prot = 0;
James Smart7f860592011-03-11 16:05:52 -05001756 unsigned int split_offset;
1757 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
James Smarte2a0a9d2008-12-04 22:40:02 -05001758 unsigned int protgrp_blks, protgrp_bytes;
1759 unsigned int remainder, subtotal;
James Smart6c8eea52010-04-06 14:49:53 -04001760 int status;
James Smarte2a0a9d2008-12-04 22:40:02 -05001761 int datadir = sc->sc_data_direction;
1762 unsigned char pgdone = 0, alldone = 0;
1763 unsigned blksize;
James Smart0829a192012-05-09 21:18:12 -04001764#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001765 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04001766#endif
James Smartacd68592012-01-18 16:25:09 -05001767 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001768 uint32_t reftag;
James Smart6c8eea52010-04-06 14:49:53 -04001769 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05001770 int num_bde = 0;
1771
1772 sgpe = scsi_prot_sglist(sc);
1773 sgde = scsi_sglist(sc);
1774
1775 if (!sgpe || !sgde) {
1776 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1777 "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
1778 sgpe, sgde);
1779 return 0;
1780 }
1781
James Smart6c8eea52010-04-06 14:49:53 -04001782 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1783 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05001784 goto out;
1785
James Smart6c8eea52010-04-06 14:49:53 -04001786 /* extract some info from the scsi command */
James Smarte2a0a9d2008-12-04 22:40:02 -05001787 blksize = lpfc_cmd_blksize(sc);
James Smartacd68592012-01-18 16:25:09 -05001788 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
James Smarte2a0a9d2008-12-04 22:40:02 -05001789
James Smartf9bb2da2011-10-10 21:34:11 -04001790#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05001791 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05001792 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05001793 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05001794 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05001795 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05001796 checking = 0;
1797 }
James Smartf9bb2da2011-10-10 21:34:11 -04001798#endif
1799
James Smarte2a0a9d2008-12-04 22:40:02 -05001800 split_offset = 0;
1801 do {
James Smart96f70772013-04-17 20:16:15 -04001802 /* Check to see if we ran out of space */
1803 if (num_bde >= (phba->cfg_total_seg_cnt - 2))
1804 return num_bde + 3;
1805
James Smart6c8eea52010-04-06 14:49:53 -04001806 /* setup PDE5 with what we have */
1807 pde5 = (struct lpfc_pde5 *) bpl;
1808 memset(pde5, 0, sizeof(struct lpfc_pde5));
1809 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05001810
James Smartbc739052010-08-04 16:11:18 -04001811 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04001812 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04001813 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04001814
James Smart6c8eea52010-04-06 14:49:53 -04001815 /* advance bpl and increment bde count */
1816 num_bde++;
1817 bpl++;
1818 pde6 = (struct lpfc_pde6 *) bpl;
James Smarte2a0a9d2008-12-04 22:40:02 -05001819
James Smart6c8eea52010-04-06 14:49:53 -04001820 /* setup PDE6 with the rest of the info */
1821 memset(pde6, 0, sizeof(struct lpfc_pde6));
1822 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
1823 bf_set(pde6_optx, pde6, txop);
1824 bf_set(pde6_oprx, pde6, rxop);
James Smarta6887e22013-04-17 20:18:07 -04001825
James Smart9c6aa9d2013-05-31 17:03:39 -04001826 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
James Smarta6887e22013-04-17 20:18:07 -04001827 bf_set(pde6_ce, pde6, checking);
1828 else
1829 bf_set(pde6_ce, pde6, 0);
1830
James Smart9c6aa9d2013-05-31 17:03:39 -04001831 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04001832 bf_set(pde6_re, pde6, checking);
1833 else
1834 bf_set(pde6_re, pde6, 0);
1835
James Smart6c8eea52010-04-06 14:49:53 -04001836 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04001837 bf_set(pde6_ae, pde6, 0);
1838 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04001839
James Smartbc739052010-08-04 16:11:18 -04001840 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04001841 pde6->word0 = cpu_to_le32(pde6->word0);
1842 pde6->word1 = cpu_to_le32(pde6->word1);
1843 pde6->word2 = cpu_to_le32(pde6->word2);
1844
James Smart6c8eea52010-04-06 14:49:53 -04001845 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05001846 num_bde++;
1847 bpl++;
1848
1849 /* setup the first BDE that points to protection buffer */
James Smart7f860592011-03-11 16:05:52 -05001850 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
1851 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
James Smarte2a0a9d2008-12-04 22:40:02 -05001852
James Smarte2a0a9d2008-12-04 22:40:02 -05001853 /* must be integer multiple of the DIF block length */
1854 BUG_ON(protgroup_len % 8);
1855
James Smart7f860592011-03-11 16:05:52 -05001856 pde7 = (struct lpfc_pde7 *) bpl;
1857 memset(pde7, 0, sizeof(struct lpfc_pde7));
1858 bf_set(pde7_type, pde7, LPFC_PDE7_DESCRIPTOR);
1859
James Smart7c56b9f2011-07-22 18:36:25 -04001860 pde7->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
1861 pde7->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
James Smart7f860592011-03-11 16:05:52 -05001862
James Smarte2a0a9d2008-12-04 22:40:02 -05001863 protgrp_blks = protgroup_len / 8;
1864 protgrp_bytes = protgrp_blks * blksize;
1865
James Smart7f860592011-03-11 16:05:52 -05001866 /* check if this pde is crossing the 4K boundary; if so split */
1867 if ((pde7->addrLow & 0xfff) + protgroup_len > 0x1000) {
1868 protgroup_remainder = 0x1000 - (pde7->addrLow & 0xfff);
1869 protgroup_offset += protgroup_remainder;
1870 protgrp_blks = protgroup_remainder / 8;
James Smart7c56b9f2011-07-22 18:36:25 -04001871 protgrp_bytes = protgrp_blks * blksize;
James Smart7f860592011-03-11 16:05:52 -05001872 } else {
1873 protgroup_offset = 0;
1874 curr_prot++;
1875 }
James Smarte2a0a9d2008-12-04 22:40:02 -05001876
James Smarte2a0a9d2008-12-04 22:40:02 -05001877 num_bde++;
1878
1879 /* setup BDE's for data blocks associated with DIF data */
1880 pgdone = 0;
1881 subtotal = 0; /* total bytes processed for current prot grp */
1882 while (!pgdone) {
James Smart96f70772013-04-17 20:16:15 -04001883 /* Check to see if we ran out of space */
1884 if (num_bde >= phba->cfg_total_seg_cnt)
1885 return num_bde + 1;
1886
James Smarte2a0a9d2008-12-04 22:40:02 -05001887 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -04001888 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1889 "9065 BLKGRD:%s Invalid data segment\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001890 __func__);
1891 return 0;
1892 }
1893 bpl++;
1894 dataphysaddr = sg_dma_address(sgde) + split_offset;
1895 bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
1896 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
1897
1898 remainder = sg_dma_len(sgde) - split_offset;
1899
1900 if ((subtotal + remainder) <= protgrp_bytes) {
1901 /* we can use this whole buffer */
1902 bpl->tus.f.bdeSize = remainder;
1903 split_offset = 0;
1904
1905 if ((subtotal + remainder) == protgrp_bytes)
1906 pgdone = 1;
1907 } else {
1908 /* must split this buffer with next prot grp */
1909 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
1910 split_offset += bpl->tus.f.bdeSize;
1911 }
1912
1913 subtotal += bpl->tus.f.bdeSize;
1914
1915 if (datadir == DMA_TO_DEVICE)
1916 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1917 else
1918 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1919 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1920
1921 num_bde++;
1922 curr_data++;
1923
1924 if (split_offset)
1925 break;
1926
1927 /* Move to the next s/g segment if possible */
1928 sgde = sg_next(sgde);
James Smart6c8eea52010-04-06 14:49:53 -04001929
James Smarte2a0a9d2008-12-04 22:40:02 -05001930 }
1931
James Smart7f860592011-03-11 16:05:52 -05001932 if (protgroup_offset) {
1933 /* update the reference tag */
1934 reftag += protgrp_blks;
1935 bpl++;
1936 continue;
1937 }
1938
James Smarte2a0a9d2008-12-04 22:40:02 -05001939 /* are we done ? */
1940 if (curr_prot == protcnt) {
1941 alldone = 1;
1942 } else if (curr_prot < protcnt) {
1943 /* advance to next prot buffer */
1944 sgpe = sg_next(sgpe);
1945 bpl++;
1946
1947 /* update the reference tag */
1948 reftag += protgrp_blks;
1949 } else {
1950 /* if we're here, we have a bug */
James Smart6a9c52c2009-10-02 15:16:51 -04001951 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1952 "9054 BLKGRD: bug in %s\n", __func__);
James Smarte2a0a9d2008-12-04 22:40:02 -05001953 }
1954
1955 } while (!alldone);
James Smarte2a0a9d2008-12-04 22:40:02 -05001956out:
1957
James Smarte2a0a9d2008-12-04 22:40:02 -05001958 return num_bde;
1959}
James Smart7f860592011-03-11 16:05:52 -05001960
James Smartacd68592012-01-18 16:25:09 -05001961/**
1962 * lpfc_bg_setup_sgl - Setup BlockGuard SGL with no protection data
1963 * @phba: The Hba for which this call is being executed.
1964 * @sc: pointer to scsi command we're working on
1965 * @sgl: pointer to buffer list for protection groups
1966 * @datacnt: number of segments of data that have been dma mapped
1967 *
1968 * This function sets up SGL buffer list for protection groups of
1969 * type LPFC_PG_TYPE_NO_DIF
1970 *
1971 * This is usually used when the HBA is instructed to generate
1972 * DIFs and insert them into data stream (or strip DIF from
1973 * incoming data stream)
1974 *
1975 * The buffer list consists of just one protection group described
1976 * below:
1977 * +-------------------------+
1978 * start of prot group --> | DI_SEED |
1979 * +-------------------------+
1980 * | Data SGE |
1981 * +-------------------------+
1982 * |more Data SGE's ... (opt)|
1983 * +-------------------------+
1984 *
1985 *
1986 * Note: Data s/g buffers have been dma mapped
1987 *
1988 * Returns the number of SGEs added to the SGL.
1989 **/
1990static int
1991lpfc_bg_setup_sgl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1992 struct sli4_sge *sgl, int datasegcnt)
1993{
1994 struct scatterlist *sgde = NULL; /* s/g data entry */
1995 struct sli4_sge_diseed *diseed = NULL;
1996 dma_addr_t physaddr;
1997 int i = 0, num_sge = 0, status;
James Smartacd68592012-01-18 16:25:09 -05001998 uint32_t reftag;
James Smartacd68592012-01-18 16:25:09 -05001999 uint8_t txop, rxop;
James Smart0829a192012-05-09 21:18:12 -04002000#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002001 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002002#endif
James Smartacd68592012-01-18 16:25:09 -05002003 uint32_t checking = 1;
2004 uint32_t dma_len;
2005 uint32_t dma_offset = 0;
2006
2007 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2008 if (status)
2009 goto out;
2010
2011 /* extract some info from the scsi command for pde*/
James Smartacd68592012-01-18 16:25:09 -05002012 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2013
2014#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002015 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002016 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002017 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002018 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002019 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002020 checking = 0;
2021 }
2022#endif
2023
2024 /* setup DISEED with what we have */
2025 diseed = (struct sli4_sge_diseed *) sgl;
2026 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2027 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2028
2029 /* Endianness conversion if necessary */
2030 diseed->ref_tag = cpu_to_le32(reftag);
2031 diseed->ref_tag_tran = diseed->ref_tag;
2032
James Smarta6887e22013-04-17 20:18:07 -04002033 /*
2034 * We only need to check the data on READs, for WRITEs
2035 * protection data is automatically generated, not checked.
2036 */
2037 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
James Smart9c6aa9d2013-05-31 17:03:39 -04002038 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
James Smarta6887e22013-04-17 20:18:07 -04002039 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2040 else
2041 bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2042
James Smart9c6aa9d2013-05-31 17:03:39 -04002043 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04002044 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2045 else
2046 bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2047 }
2048
James Smartacd68592012-01-18 16:25:09 -05002049 /* setup DISEED with the rest of the info */
2050 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2051 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002052
James Smartacd68592012-01-18 16:25:09 -05002053 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2054 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2055
2056 /* Endianness conversion if necessary for DISEED */
2057 diseed->word2 = cpu_to_le32(diseed->word2);
2058 diseed->word3 = cpu_to_le32(diseed->word3);
2059
2060 /* advance bpl and increment sge count */
2061 num_sge++;
2062 sgl++;
2063
2064 /* assumption: caller has already run dma_map_sg on command data */
2065 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
2066 physaddr = sg_dma_address(sgde);
2067 dma_len = sg_dma_len(sgde);
2068 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
2069 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
2070 if ((i + 1) == datasegcnt)
2071 bf_set(lpfc_sli4_sge_last, sgl, 1);
2072 else
2073 bf_set(lpfc_sli4_sge_last, sgl, 0);
2074 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2075 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2076
2077 sgl->sge_len = cpu_to_le32(dma_len);
2078 dma_offset += dma_len;
2079
2080 sgl++;
2081 num_sge++;
2082 }
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,
2127 struct sli4_sge *sgl, int datacnt, int protcnt)
2128{
2129 struct scatterlist *sgde = NULL; /* s/g data entry */
2130 struct scatterlist *sgpe = NULL; /* s/g prot entry */
2131 struct sli4_sge_diseed *diseed = NULL;
2132 dma_addr_t dataphysaddr, protphysaddr;
2133 unsigned short curr_data = 0, curr_prot = 0;
2134 unsigned int split_offset;
2135 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
2136 unsigned int protgrp_blks, protgrp_bytes;
2137 unsigned int remainder, subtotal;
2138 int status;
2139 unsigned char pgdone = 0, alldone = 0;
2140 unsigned blksize;
2141 uint32_t reftag;
2142 uint8_t txop, rxop;
2143 uint32_t dma_len;
James Smart0829a192012-05-09 21:18:12 -04002144#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002145 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002146#endif
James Smartacd68592012-01-18 16:25:09 -05002147 uint32_t checking = 1;
2148 uint32_t dma_offset = 0;
2149 int num_sge = 0;
2150
2151 sgpe = scsi_prot_sglist(sc);
2152 sgde = scsi_sglist(sc);
2153
2154 if (!sgpe || !sgde) {
2155 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2156 "9082 Invalid s/g entry: data=0x%p prot=0x%p\n",
2157 sgpe, sgde);
2158 return 0;
2159 }
2160
2161 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2162 if (status)
2163 goto out;
2164
2165 /* extract some info from the scsi command */
2166 blksize = lpfc_cmd_blksize(sc);
2167 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2168
2169#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002170 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002171 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002172 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002173 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002174 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002175 checking = 0;
2176 }
2177#endif
2178
2179 split_offset = 0;
2180 do {
James Smart96f70772013-04-17 20:16:15 -04002181 /* Check to see if we ran out of space */
2182 if (num_sge >= (phba->cfg_total_seg_cnt - 2))
2183 return num_sge + 3;
2184
James Smartacd68592012-01-18 16:25:09 -05002185 /* setup DISEED with what we have */
2186 diseed = (struct sli4_sge_diseed *) sgl;
2187 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2188 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2189
2190 /* Endianness conversion if necessary */
2191 diseed->ref_tag = cpu_to_le32(reftag);
2192 diseed->ref_tag_tran = diseed->ref_tag;
2193
James Smart9c6aa9d2013-05-31 17:03:39 -04002194 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD)) {
James Smarta6887e22013-04-17 20:18:07 -04002195 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2196
2197 } else {
2198 bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2199 /*
2200 * When in this mode, the hardware will replace
2201 * the guard tag from the host with a
2202 * newly generated good CRC for the wire.
2203 * Switch to raw mode here to avoid this
2204 * behavior. What the host sends gets put on the wire.
2205 */
2206 if (txop == BG_OP_IN_CRC_OUT_CRC) {
2207 txop = BG_OP_RAW_MODE;
2208 rxop = BG_OP_RAW_MODE;
2209 }
2210 }
2211
2212
James Smart9c6aa9d2013-05-31 17:03:39 -04002213 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04002214 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2215 else
2216 bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2217
James Smartacd68592012-01-18 16:25:09 -05002218 /* setup DISEED with the rest of the info */
2219 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2220 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002221
James Smartacd68592012-01-18 16:25:09 -05002222 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2223 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2224
2225 /* Endianness conversion if necessary for DISEED */
2226 diseed->word2 = cpu_to_le32(diseed->word2);
2227 diseed->word3 = cpu_to_le32(diseed->word3);
2228
2229 /* advance sgl and increment bde count */
2230 num_sge++;
2231 sgl++;
2232
2233 /* setup the first BDE that points to protection buffer */
2234 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2235 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
2236
2237 /* must be integer multiple of the DIF block length */
2238 BUG_ON(protgroup_len % 8);
2239
2240 /* Now setup DIF SGE */
2241 sgl->word2 = 0;
2242 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DIF);
2243 sgl->addr_hi = le32_to_cpu(putPaddrHigh(protphysaddr));
2244 sgl->addr_lo = le32_to_cpu(putPaddrLow(protphysaddr));
2245 sgl->word2 = cpu_to_le32(sgl->word2);
2246
2247 protgrp_blks = protgroup_len / 8;
2248 protgrp_bytes = protgrp_blks * blksize;
2249
2250 /* check if DIF SGE is crossing the 4K boundary; if so split */
2251 if ((sgl->addr_lo & 0xfff) + protgroup_len > 0x1000) {
2252 protgroup_remainder = 0x1000 - (sgl->addr_lo & 0xfff);
2253 protgroup_offset += protgroup_remainder;
2254 protgrp_blks = protgroup_remainder / 8;
2255 protgrp_bytes = protgrp_blks * blksize;
2256 } else {
2257 protgroup_offset = 0;
2258 curr_prot++;
2259 }
2260
2261 num_sge++;
2262
2263 /* setup SGE's for data blocks associated with DIF data */
2264 pgdone = 0;
2265 subtotal = 0; /* total bytes processed for current prot grp */
2266 while (!pgdone) {
James Smart96f70772013-04-17 20:16:15 -04002267 /* Check to see if we ran out of space */
2268 if (num_sge >= phba->cfg_total_seg_cnt)
2269 return num_sge + 1;
2270
James Smartacd68592012-01-18 16:25:09 -05002271 if (!sgde) {
2272 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2273 "9086 BLKGRD:%s Invalid data segment\n",
2274 __func__);
2275 return 0;
2276 }
2277 sgl++;
2278 dataphysaddr = sg_dma_address(sgde) + split_offset;
2279
2280 remainder = sg_dma_len(sgde) - split_offset;
2281
2282 if ((subtotal + remainder) <= protgrp_bytes) {
2283 /* we can use this whole buffer */
2284 dma_len = remainder;
2285 split_offset = 0;
2286
2287 if ((subtotal + remainder) == protgrp_bytes)
2288 pgdone = 1;
2289 } else {
2290 /* must split this buffer with next prot grp */
2291 dma_len = protgrp_bytes - subtotal;
2292 split_offset += dma_len;
2293 }
2294
2295 subtotal += dma_len;
2296
2297 sgl->addr_lo = cpu_to_le32(putPaddrLow(dataphysaddr));
2298 sgl->addr_hi = cpu_to_le32(putPaddrHigh(dataphysaddr));
2299 bf_set(lpfc_sli4_sge_last, sgl, 0);
2300 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2301 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2302
2303 sgl->sge_len = cpu_to_le32(dma_len);
2304 dma_offset += dma_len;
2305
2306 num_sge++;
2307 curr_data++;
2308
2309 if (split_offset)
2310 break;
2311
2312 /* Move to the next s/g segment if possible */
2313 sgde = sg_next(sgde);
2314 }
2315
2316 if (protgroup_offset) {
2317 /* update the reference tag */
2318 reftag += protgrp_blks;
2319 sgl++;
2320 continue;
2321 }
2322
2323 /* are we done ? */
2324 if (curr_prot == protcnt) {
2325 bf_set(lpfc_sli4_sge_last, sgl, 1);
2326 alldone = 1;
2327 } else if (curr_prot < protcnt) {
2328 /* advance to next prot buffer */
2329 sgpe = sg_next(sgpe);
2330 sgl++;
2331
2332 /* update the reference tag */
2333 reftag += protgrp_blks;
2334 } else {
2335 /* if we're here, we have a bug */
2336 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2337 "9085 BLKGRD: bug in %s\n", __func__);
2338 }
2339
2340 } while (!alldone);
2341
2342out:
2343
2344 return num_sge;
2345}
2346
2347/**
2348 * lpfc_prot_group_type - Get prtotection group type of SCSI command
2349 * @phba: The Hba for which this call is being executed.
2350 * @sc: pointer to scsi command we're working on
2351 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002352 * Given a SCSI command that supports DIF, determine composition of protection
2353 * groups involved in setting up buffer lists
2354 *
James Smartacd68592012-01-18 16:25:09 -05002355 * Returns: Protection group type (with or without DIF)
2356 *
2357 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002358static int
2359lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
2360{
2361 int ret = LPFC_PG_TYPE_INVALID;
2362 unsigned char op = scsi_get_prot_op(sc);
2363
2364 switch (op) {
2365 case SCSI_PROT_READ_STRIP:
2366 case SCSI_PROT_WRITE_INSERT:
2367 ret = LPFC_PG_TYPE_NO_DIF;
2368 break;
2369 case SCSI_PROT_READ_INSERT:
2370 case SCSI_PROT_WRITE_STRIP:
2371 case SCSI_PROT_READ_PASS:
2372 case SCSI_PROT_WRITE_PASS:
James Smarte2a0a9d2008-12-04 22:40:02 -05002373 ret = LPFC_PG_TYPE_DIF_BUF;
2374 break;
2375 default:
James Smart9c6aa9d2013-05-31 17:03:39 -04002376 if (phba)
2377 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2378 "9021 Unsupported protection op:%d\n",
2379 op);
James Smarte2a0a9d2008-12-04 22:40:02 -05002380 break;
2381 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002382 return ret;
2383}
2384
James Smartacd68592012-01-18 16:25:09 -05002385/**
James Smarta6887e22013-04-17 20:18:07 -04002386 * lpfc_bg_scsi_adjust_dl - Adjust SCSI data length for BlockGuard
2387 * @phba: The Hba for which this call is being executed.
2388 * @lpfc_cmd: The scsi buffer which is going to be adjusted.
2389 *
2390 * Adjust the data length to account for how much data
2391 * is actually on the wire.
2392 *
2393 * returns the adjusted data length
2394 **/
2395static int
2396lpfc_bg_scsi_adjust_dl(struct lpfc_hba *phba,
James Smartc4908502019-01-28 11:14:28 -08002397 struct lpfc_io_buf *lpfc_cmd)
James Smarta6887e22013-04-17 20:18:07 -04002398{
2399 struct scsi_cmnd *sc = lpfc_cmd->pCmd;
2400 int fcpdl;
2401
2402 fcpdl = scsi_bufflen(sc);
2403
2404 /* Check if there is protection data on the wire */
2405 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
James Smart9c6aa9d2013-05-31 17:03:39 -04002406 /* Read check for protection data */
James Smarta6887e22013-04-17 20:18:07 -04002407 if (scsi_get_prot_op(sc) == SCSI_PROT_READ_INSERT)
2408 return fcpdl;
2409
2410 } else {
James Smart9c6aa9d2013-05-31 17:03:39 -04002411 /* Write check for protection data */
James Smarta6887e22013-04-17 20:18:07 -04002412 if (scsi_get_prot_op(sc) == SCSI_PROT_WRITE_STRIP)
2413 return fcpdl;
2414 }
2415
2416 /*
2417 * If we are in DIF Type 1 mode every data block has a 8 byte
James Smart9c6aa9d2013-05-31 17:03:39 -04002418 * DIF (trailer) attached to it. Must ajust FCP data length
2419 * to account for the protection data.
James Smarta6887e22013-04-17 20:18:07 -04002420 */
James Smart9c6aa9d2013-05-31 17:03:39 -04002421 fcpdl += (fcpdl / lpfc_cmd_blksize(sc)) * 8;
James Smarta6887e22013-04-17 20:18:07 -04002422
2423 return fcpdl;
2424}
2425
2426/**
James Smartacd68592012-01-18 16:25:09 -05002427 * lpfc_bg_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
2428 * @phba: The Hba for which this call is being executed.
2429 * @lpfc_cmd: The scsi buffer which is going to be prep'ed.
2430 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002431 * This is the protection/DIF aware version of
2432 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
2433 * two functions eventually, but for now, it's here
James Smartacd68592012-01-18 16:25:09 -05002434 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002435static int
James Smartacd68592012-01-18 16:25:09 -05002436lpfc_bg_scsi_prep_dma_buf_s3(struct lpfc_hba *phba,
James Smartc4908502019-01-28 11:14:28 -08002437 struct lpfc_io_buf *lpfc_cmd)
James Smarte2a0a9d2008-12-04 22:40:02 -05002438{
2439 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2440 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smartc4908502019-01-28 11:14:28 -08002441 struct ulp_bde64 *bpl = (struct ulp_bde64 *)lpfc_cmd->dma_sgl;
James Smarte2a0a9d2008-12-04 22:40:02 -05002442 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2443 uint32_t num_bde = 0;
2444 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
2445 int prot_group_type = 0;
James Smarta6887e22013-04-17 20:18:07 -04002446 int fcpdl;
James Smart7c4042a2018-11-29 16:09:40 -08002447 struct lpfc_vport *vport = phba->pport;
James Smarte2a0a9d2008-12-04 22:40:02 -05002448
2449 /*
2450 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
2451 * fcp_rsp regions to the first data bde entry
2452 */
2453 bpl += 2;
2454 if (scsi_sg_count(scsi_cmnd)) {
2455 /*
2456 * The driver stores the segment count returned from pci_map_sg
2457 * because this a count of dma-mappings used to map the use_sg
2458 * pages. They are not guaranteed to be the same for those
2459 * architectures that implement an IOMMU.
2460 */
2461 datasegcnt = dma_map_sg(&phba->pcidev->dev,
2462 scsi_sglist(scsi_cmnd),
2463 scsi_sg_count(scsi_cmnd), datadir);
2464 if (unlikely(!datasegcnt))
2465 return 1;
2466
2467 lpfc_cmd->seg_cnt = datasegcnt;
James Smart96f70772013-04-17 20:16:15 -04002468
2469 /* First check if data segment count from SCSI Layer is good */
2470 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt)
2471 goto err;
James Smarte2a0a9d2008-12-04 22:40:02 -05002472
2473 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
2474
2475 switch (prot_group_type) {
2476 case LPFC_PG_TYPE_NO_DIF:
James Smart96f70772013-04-17 20:16:15 -04002477
2478 /* Here we need to add a PDE5 and PDE6 to the count */
2479 if ((lpfc_cmd->seg_cnt + 2) > phba->cfg_total_seg_cnt)
2480 goto err;
2481
James Smarte2a0a9d2008-12-04 22:40:02 -05002482 num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
2483 datasegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002484 /* we should have 2 or more entries in buffer list */
James Smarte2a0a9d2008-12-04 22:40:02 -05002485 if (num_bde < 2)
2486 goto err;
2487 break;
James Smart96f70772013-04-17 20:16:15 -04002488
2489 case LPFC_PG_TYPE_DIF_BUF:
James Smarte2a0a9d2008-12-04 22:40:02 -05002490 /*
2491 * This type indicates that protection buffers are
2492 * passed to the driver, so that needs to be prepared
2493 * for DMA
2494 */
2495 protsegcnt = dma_map_sg(&phba->pcidev->dev,
2496 scsi_prot_sglist(scsi_cmnd),
2497 scsi_prot_sg_count(scsi_cmnd), datadir);
2498 if (unlikely(!protsegcnt)) {
2499 scsi_dma_unmap(scsi_cmnd);
2500 return 1;
2501 }
2502
2503 lpfc_cmd->prot_seg_cnt = protsegcnt;
James Smart96f70772013-04-17 20:16:15 -04002504
2505 /*
2506 * There is a minimun of 4 BPLs used for every
2507 * protection data segment.
2508 */
2509 if ((lpfc_cmd->prot_seg_cnt * 4) >
2510 (phba->cfg_total_seg_cnt - 2))
2511 goto err;
James Smarte2a0a9d2008-12-04 22:40:02 -05002512
2513 num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
2514 datasegcnt, protsegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002515 /* we should have 3 or more entries in buffer list */
James Smart96f70772013-04-17 20:16:15 -04002516 if ((num_bde < 3) ||
2517 (num_bde > phba->cfg_total_seg_cnt))
James Smarte2a0a9d2008-12-04 22:40:02 -05002518 goto err;
2519 break;
James Smart96f70772013-04-17 20:16:15 -04002520
James Smarte2a0a9d2008-12-04 22:40:02 -05002521 case LPFC_PG_TYPE_INVALID:
2522 default:
James Smart96f70772013-04-17 20:16:15 -04002523 scsi_dma_unmap(scsi_cmnd);
2524 lpfc_cmd->seg_cnt = 0;
2525
James Smarte2a0a9d2008-12-04 22:40:02 -05002526 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2527 "9022 Unexpected protection group %i\n",
2528 prot_group_type);
2529 return 1;
2530 }
2531 }
2532
2533 /*
2534 * Finish initializing those IOCB fields that are dependent on the
2535 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
2536 * reinitialized since all iocb memory resources are used many times
2537 * for transmit, receive, and continuation bpl's.
2538 */
2539 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
2540 iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
2541 iocb_cmd->ulpBdeCount = 1;
2542 iocb_cmd->ulpLe = 1;
2543
James Smarta6887e22013-04-17 20:18:07 -04002544 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002545 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
2546
2547 /*
2548 * Due to difference in data length between DIF/non-DIF paths,
2549 * we need to set word 4 of IOCB here
2550 */
2551 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
2552
James Smart7c4042a2018-11-29 16:09:40 -08002553 /*
2554 * For First burst, we may need to adjust the initial transfer
2555 * length for DIF
2556 */
2557 if (iocb_cmd->un.fcpi.fcpi_XRdy &&
2558 (fcpdl < vport->cfg_first_burst_size))
2559 iocb_cmd->un.fcpi.fcpi_XRdy = fcpdl;
2560
James Smarte2a0a9d2008-12-04 22:40:02 -05002561 return 0;
2562err:
James Smart96f70772013-04-17 20:16:15 -04002563 if (lpfc_cmd->seg_cnt)
2564 scsi_dma_unmap(scsi_cmnd);
2565 if (lpfc_cmd->prot_seg_cnt)
2566 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
2567 scsi_prot_sg_count(scsi_cmnd),
2568 scsi_cmnd->sc_data_direction);
2569
James Smarte2a0a9d2008-12-04 22:40:02 -05002570 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart96f70772013-04-17 20:16:15 -04002571 "9023 Cannot setup S/G List for HBA"
2572 "IO segs %d/%d BPL %d SCSI %d: %d %d\n",
2573 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
2574 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
James Smarte2a0a9d2008-12-04 22:40:02 -05002575 prot_group_type, num_bde);
James Smart96f70772013-04-17 20:16:15 -04002576
2577 lpfc_cmd->seg_cnt = 0;
2578 lpfc_cmd->prot_seg_cnt = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05002579 return 1;
2580}
2581
2582/*
James Smart737d4242013-04-17 20:14:49 -04002583 * This function calcuates the T10 DIF guard tag
2584 * on the specified data using a CRC algorithmn
2585 * using crc_t10dif.
2586 */
Rashika Kheria7bfe7812014-09-03 12:55:36 -04002587static uint16_t
James Smart737d4242013-04-17 20:14:49 -04002588lpfc_bg_crc(uint8_t *data, int count)
2589{
2590 uint16_t crc = 0;
2591 uint16_t x;
2592
2593 crc = crc_t10dif(data, count);
2594 x = cpu_to_be16(crc);
2595 return x;
2596}
2597
2598/*
2599 * This function calcuates the T10 DIF guard tag
2600 * on the specified data using a CSUM algorithmn
2601 * using ip_compute_csum.
2602 */
Rashika Kheria7bfe7812014-09-03 12:55:36 -04002603static uint16_t
James Smart737d4242013-04-17 20:14:49 -04002604lpfc_bg_csum(uint8_t *data, int count)
2605{
2606 uint16_t ret;
2607
2608 ret = ip_compute_csum(data, count);
2609 return ret;
2610}
2611
2612/*
2613 * This function examines the protection data to try to determine
2614 * what type of T10-DIF error occurred.
2615 */
Rashika Kheria7bfe7812014-09-03 12:55:36 -04002616static void
James Smartc4908502019-01-28 11:14:28 -08002617lpfc_calc_bg_err(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
James Smart737d4242013-04-17 20:14:49 -04002618{
2619 struct scatterlist *sgpe; /* s/g prot entry */
2620 struct scatterlist *sgde; /* s/g data entry */
2621 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
2622 struct scsi_dif_tuple *src = NULL;
2623 uint8_t *data_src = NULL;
Sebastian Herbsztdb6f1c22015-08-31 16:48:14 -04002624 uint16_t guard_tag;
James Smart737d4242013-04-17 20:14:49 -04002625 uint16_t start_app_tag, app_tag;
2626 uint32_t start_ref_tag, ref_tag;
2627 int prot, protsegcnt;
2628 int err_type, len, data_len;
2629 int chk_ref, chk_app, chk_guard;
2630 uint16_t sum;
2631 unsigned blksize;
2632
2633 err_type = BGS_GUARD_ERR_MASK;
2634 sum = 0;
2635 guard_tag = 0;
2636
2637 /* First check to see if there is protection data to examine */
2638 prot = scsi_get_prot_op(cmd);
2639 if ((prot == SCSI_PROT_READ_STRIP) ||
2640 (prot == SCSI_PROT_WRITE_INSERT) ||
2641 (prot == SCSI_PROT_NORMAL))
2642 goto out;
2643
2644 /* Currently the driver just supports ref_tag and guard_tag checking */
2645 chk_ref = 1;
2646 chk_app = 0;
2647 chk_guard = 0;
2648
2649 /* Setup a ptr to the protection data provided by the SCSI host */
2650 sgpe = scsi_prot_sglist(cmd);
2651 protsegcnt = lpfc_cmd->prot_seg_cnt;
2652
2653 if (sgpe && protsegcnt) {
2654
2655 /*
2656 * We will only try to verify guard tag if the segment
2657 * data length is a multiple of the blksize.
2658 */
2659 sgde = scsi_sglist(cmd);
2660 blksize = lpfc_cmd_blksize(cmd);
2661 data_src = (uint8_t *)sg_virt(sgde);
2662 data_len = sgde->length;
2663 if ((data_len & (blksize - 1)) == 0)
2664 chk_guard = 1;
James Smart737d4242013-04-17 20:14:49 -04002665
James Smarte85d8f92013-05-31 17:04:10 -04002666 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
James Smarta6887e22013-04-17 20:18:07 -04002667 start_ref_tag = (uint32_t)scsi_get_lba(cmd); /* Truncate LBA */
James Smart737d4242013-04-17 20:14:49 -04002668 start_app_tag = src->app_tag;
James Smart737d4242013-04-17 20:14:49 -04002669 len = sgpe->length;
2670 while (src && protsegcnt) {
2671 while (len) {
2672
2673 /*
2674 * First check to see if a protection data
2675 * check is valid
2676 */
Dmitry Monakhov128b6f92017-06-29 11:31:12 -07002677 if ((src->ref_tag == T10_PI_REF_ESCAPE) ||
2678 (src->app_tag == T10_PI_APP_ESCAPE)) {
James Smart737d4242013-04-17 20:14:49 -04002679 start_ref_tag++;
2680 goto skipit;
2681 }
2682
James Smart9c6aa9d2013-05-31 17:03:39 -04002683 /* First Guard Tag checking */
2684 if (chk_guard) {
2685 guard_tag = src->guard_tag;
2686 if (lpfc_cmd_guard_csum(cmd))
2687 sum = lpfc_bg_csum(data_src,
2688 blksize);
2689 else
2690 sum = lpfc_bg_crc(data_src,
2691 blksize);
2692 if ((guard_tag != sum)) {
2693 err_type = BGS_GUARD_ERR_MASK;
2694 goto out;
2695 }
James Smart737d4242013-04-17 20:14:49 -04002696 }
2697
2698 /* Reference Tag checking */
2699 ref_tag = be32_to_cpu(src->ref_tag);
2700 if (chk_ref && (ref_tag != start_ref_tag)) {
2701 err_type = BGS_REFTAG_ERR_MASK;
2702 goto out;
2703 }
2704 start_ref_tag++;
2705
James Smart9c6aa9d2013-05-31 17:03:39 -04002706 /* App Tag checking */
2707 app_tag = src->app_tag;
2708 if (chk_app && (app_tag != start_app_tag)) {
2709 err_type = BGS_APPTAG_ERR_MASK;
2710 goto out;
James Smart737d4242013-04-17 20:14:49 -04002711 }
2712skipit:
2713 len -= sizeof(struct scsi_dif_tuple);
2714 if (len < 0)
2715 len = 0;
2716 src++;
2717
2718 data_src += blksize;
2719 data_len -= blksize;
2720
2721 /*
2722 * Are we at the end of the Data segment?
2723 * The data segment is only used for Guard
2724 * tag checking.
2725 */
2726 if (chk_guard && (data_len == 0)) {
2727 chk_guard = 0;
2728 sgde = sg_next(sgde);
2729 if (!sgde)
2730 goto out;
2731
2732 data_src = (uint8_t *)sg_virt(sgde);
2733 data_len = sgde->length;
2734 if ((data_len & (blksize - 1)) == 0)
2735 chk_guard = 1;
2736 }
2737 }
2738
2739 /* Goto the next Protection data segment */
2740 sgpe = sg_next(sgpe);
2741 if (sgpe) {
2742 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
2743 len = sgpe->length;
2744 } else {
2745 src = NULL;
2746 }
2747 protsegcnt--;
2748 }
2749 }
2750out:
2751 if (err_type == BGS_GUARD_ERR_MASK) {
2752 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2753 0x10, 0x1);
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02002754 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
2755 SAM_STAT_CHECK_CONDITION;
James Smart737d4242013-04-17 20:14:49 -04002756 phba->bg_guard_err_cnt++;
2757 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2758 "9069 BLKGRD: LBA %lx grd_tag error %x != %x\n",
2759 (unsigned long)scsi_get_lba(cmd),
2760 sum, guard_tag);
2761
2762 } else if (err_type == BGS_REFTAG_ERR_MASK) {
2763 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2764 0x10, 0x3);
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02002765 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
2766 SAM_STAT_CHECK_CONDITION;
James Smart737d4242013-04-17 20:14:49 -04002767
2768 phba->bg_reftag_err_cnt++;
2769 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2770 "9066 BLKGRD: LBA %lx ref_tag error %x != %x\n",
2771 (unsigned long)scsi_get_lba(cmd),
2772 ref_tag, start_ref_tag);
2773
2774 } else if (err_type == BGS_APPTAG_ERR_MASK) {
2775 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2776 0x10, 0x2);
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02002777 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
2778 SAM_STAT_CHECK_CONDITION;
James Smart737d4242013-04-17 20:14:49 -04002779
2780 phba->bg_apptag_err_cnt++;
2781 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2782 "9041 BLKGRD: LBA %lx app_tag error %x != %x\n",
2783 (unsigned long)scsi_get_lba(cmd),
2784 app_tag, start_app_tag);
2785 }
2786}
2787
2788
2789/*
James Smarte2a0a9d2008-12-04 22:40:02 -05002790 * This function checks for BlockGuard errors detected by
2791 * the HBA. In case of errors, the ASC/ASCQ fields in the
2792 * sense buffer will be set accordingly, paired with
2793 * ILLEGAL_REQUEST to signal to the kernel that the HBA
2794 * detected corruption.
2795 *
2796 * Returns:
2797 * 0 - No error found
2798 * 1 - BlockGuard error found
2799 * -1 - Internal error (bad profile, ...etc)
2800 */
2801static int
James Smartc4908502019-01-28 11:14:28 -08002802lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd,
2803 struct lpfc_iocbq *pIocbOut)
James Smarte2a0a9d2008-12-04 22:40:02 -05002804{
2805 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
2806 struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
2807 int ret = 0;
2808 uint32_t bghm = bgf->bghm;
2809 uint32_t bgstat = bgf->bgstat;
2810 uint64_t failing_sector = 0;
2811
James Smarte2a0a9d2008-12-04 22:40:02 -05002812 spin_lock(&_dump_buf_lock);
2813 if (!_dump_buf_done) {
James Smart6a9c52c2009-10-02 15:16:51 -04002814 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9070 BLKGRD: Saving"
2815 " Data for %u blocks to debugfs\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002816 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
James Smart6a9c52c2009-10-02 15:16:51 -04002817 lpfc_debug_save_data(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002818
2819 /* If we have a prot sgl, save the DIF buffer */
2820 if (lpfc_prot_group_type(phba, cmd) ==
2821 LPFC_PG_TYPE_DIF_BUF) {
James Smart6a9c52c2009-10-02 15:16:51 -04002822 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9071 BLKGRD: "
2823 "Saving DIF for %u blocks to debugfs\n",
2824 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
2825 lpfc_debug_save_dif(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002826 }
2827
2828 _dump_buf_done = 1;
2829 }
2830 spin_unlock(&_dump_buf_lock);
2831
2832 if (lpfc_bgs_get_invalid_prof(bgstat)) {
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02002833 cmd->result = DID_ERROR << 16;
James Smart737d4242013-04-17 20:14:49 -04002834 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2835 "9072 BLKGRD: Invalid BG Profile in cmd"
2836 " 0x%x lba 0x%llx blk cnt 0x%x "
2837 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
2838 (unsigned long long)scsi_get_lba(cmd),
2839 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05002840 ret = (-1);
2841 goto out;
2842 }
2843
2844 if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02002845 cmd->result = DID_ERROR << 16;
James Smart737d4242013-04-17 20:14:49 -04002846 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2847 "9073 BLKGRD: Invalid BG PDIF Block in cmd"
2848 " 0x%x lba 0x%llx blk cnt 0x%x "
2849 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
2850 (unsigned long long)scsi_get_lba(cmd),
2851 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05002852 ret = (-1);
2853 goto out;
2854 }
2855
2856 if (lpfc_bgs_get_guard_err(bgstat)) {
2857 ret = 1;
2858
2859 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2860 0x10, 0x1);
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02002861 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
2862 SAM_STAT_CHECK_CONDITION;
James Smarte2a0a9d2008-12-04 22:40:02 -05002863 phba->bg_guard_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04002864 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2865 "9055 BLKGRD: Guard Tag error in cmd"
2866 " 0x%x lba 0x%llx blk cnt 0x%x "
2867 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
2868 (unsigned long long)scsi_get_lba(cmd),
2869 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05002870 }
2871
2872 if (lpfc_bgs_get_reftag_err(bgstat)) {
2873 ret = 1;
2874
2875 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2876 0x10, 0x3);
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02002877 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
2878 SAM_STAT_CHECK_CONDITION;
James Smarte2a0a9d2008-12-04 22:40:02 -05002879
2880 phba->bg_reftag_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04002881 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2882 "9056 BLKGRD: Ref Tag error in cmd"
2883 " 0x%x lba 0x%llx blk cnt 0x%x "
2884 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
2885 (unsigned long long)scsi_get_lba(cmd),
2886 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05002887 }
2888
2889 if (lpfc_bgs_get_apptag_err(bgstat)) {
2890 ret = 1;
2891
2892 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2893 0x10, 0x2);
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02002894 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
2895 SAM_STAT_CHECK_CONDITION;
James Smarte2a0a9d2008-12-04 22:40:02 -05002896
2897 phba->bg_apptag_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04002898 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2899 "9061 BLKGRD: App Tag error in cmd"
2900 " 0x%x lba 0x%llx blk cnt 0x%x "
2901 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
2902 (unsigned long long)scsi_get_lba(cmd),
2903 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05002904 }
2905
2906 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
2907 /*
2908 * setup sense data descriptor 0 per SPC-4 as an information
James Smart7c56b9f2011-07-22 18:36:25 -04002909 * field, and put the failing LBA in it.
2910 * This code assumes there was also a guard/app/ref tag error
2911 * indication.
James Smarte2a0a9d2008-12-04 22:40:02 -05002912 */
James Smart7c56b9f2011-07-22 18:36:25 -04002913 cmd->sense_buffer[7] = 0xc; /* Additional sense length */
2914 cmd->sense_buffer[8] = 0; /* Information descriptor type */
2915 cmd->sense_buffer[9] = 0xa; /* Additional descriptor length */
2916 cmd->sense_buffer[10] = 0x80; /* Validity bit */
James Smartacd68592012-01-18 16:25:09 -05002917
2918 /* bghm is a "on the wire" FC frame based count */
2919 switch (scsi_get_prot_op(cmd)) {
2920 case SCSI_PROT_READ_INSERT:
2921 case SCSI_PROT_WRITE_STRIP:
2922 bghm /= cmd->device->sector_size;
2923 break;
2924 case SCSI_PROT_READ_STRIP:
2925 case SCSI_PROT_WRITE_INSERT:
2926 case SCSI_PROT_READ_PASS:
2927 case SCSI_PROT_WRITE_PASS:
2928 bghm /= (cmd->device->sector_size +
2929 sizeof(struct scsi_dif_tuple));
2930 break;
2931 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002932
2933 failing_sector = scsi_get_lba(cmd);
2934 failing_sector += bghm;
2935
James Smart7c56b9f2011-07-22 18:36:25 -04002936 /* Descriptor Information */
2937 put_unaligned_be64(failing_sector, &cmd->sense_buffer[12]);
James Smarte2a0a9d2008-12-04 22:40:02 -05002938 }
2939
2940 if (!ret) {
2941 /* No error was reported - problem in FW? */
James Smart737d4242013-04-17 20:14:49 -04002942 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2943 "9057 BLKGRD: Unknown error in cmd"
2944 " 0x%x lba 0x%llx blk cnt 0x%x "
2945 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
2946 (unsigned long long)scsi_get_lba(cmd),
2947 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05002948
James Smart737d4242013-04-17 20:14:49 -04002949 /* Calcuate what type of error it was */
2950 lpfc_calc_bg_err(phba, lpfc_cmd);
2951 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002952out:
2953 return ret;
2954}
2955
James Smartea2151b2008-09-07 11:52:10 -04002956/**
James Smartda0436e2009-05-22 14:51:39 -04002957 * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
2958 * @phba: The Hba for which this call is being executed.
2959 * @lpfc_cmd: The scsi buffer which is going to be mapped.
2960 *
2961 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
2962 * field of @lpfc_cmd for device with SLI-4 interface spec.
2963 *
2964 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04002965 * 1 - Error
2966 * 0 - Success
James Smartda0436e2009-05-22 14:51:39 -04002967 **/
2968static int
James Smartc4908502019-01-28 11:14:28 -08002969lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
James Smartda0436e2009-05-22 14:51:39 -04002970{
2971 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2972 struct scatterlist *sgel = NULL;
2973 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart0794d602019-01-28 11:14:19 -08002974 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
James Smartfedd3b72011-02-16 12:39:24 -05002975 struct sli4_sge *first_data_sgl;
James Smartda0436e2009-05-22 14:51:39 -04002976 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2977 dma_addr_t physaddr;
2978 uint32_t num_bde = 0;
2979 uint32_t dma_len;
2980 uint32_t dma_offset = 0;
2981 int nseg;
James Smartfedd3b72011-02-16 12:39:24 -05002982 struct ulp_bde64 *bde;
James Smartda0436e2009-05-22 14:51:39 -04002983
2984 /*
2985 * There are three possibilities here - use scatter-gather segment, use
2986 * the single mapping, or neither. Start the lpfc command prep by
2987 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
2988 * data bde entry.
2989 */
2990 if (scsi_sg_count(scsi_cmnd)) {
2991 /*
2992 * The driver stores the segment count returned from pci_map_sg
2993 * because this a count of dma-mappings used to map the use_sg
2994 * pages. They are not guaranteed to be the same for those
2995 * architectures that implement an IOMMU.
2996 */
2997
2998 nseg = scsi_dma_map(scsi_cmnd);
James Smart5116fbf2015-05-22 10:42:39 -04002999 if (unlikely(nseg <= 0))
James Smartda0436e2009-05-22 14:51:39 -04003000 return 1;
3001 sgl += 1;
3002 /* clear the last flag in the fcp_rsp map entry */
3003 sgl->word2 = le32_to_cpu(sgl->word2);
3004 bf_set(lpfc_sli4_sge_last, sgl, 0);
3005 sgl->word2 = cpu_to_le32(sgl->word2);
3006 sgl += 1;
James Smartfedd3b72011-02-16 12:39:24 -05003007 first_data_sgl = sgl;
James Smartda0436e2009-05-22 14:51:39 -04003008 lpfc_cmd->seg_cnt = nseg;
3009 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04003010 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
3011 " %s: Too many sg segments from "
3012 "dma_map_sg. Config %d, seg_cnt %d\n",
3013 __func__, phba->cfg_sg_seg_cnt,
James Smartda0436e2009-05-22 14:51:39 -04003014 lpfc_cmd->seg_cnt);
James Smart96f70772013-04-17 20:16:15 -04003015 lpfc_cmd->seg_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04003016 scsi_dma_unmap(scsi_cmnd);
3017 return 1;
3018 }
3019
3020 /*
3021 * The driver established a maximum scatter-gather segment count
3022 * during probe that limits the number of sg elements in any
3023 * single scsi command. Just run through the seg_cnt and format
3024 * the sge's.
3025 * When using SLI-3 the driver will try to fit all the BDEs into
3026 * the IOCB. If it can't then the BDEs get added to a BPL as it
3027 * does for SLI-2 mode.
3028 */
3029 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
3030 physaddr = sg_dma_address(sgel);
3031 dma_len = sg_dma_len(sgel);
James Smartda0436e2009-05-22 14:51:39 -04003032 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
3033 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
James Smart05580562011-05-24 11:40:48 -04003034 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -04003035 if ((num_bde + 1) == nseg)
3036 bf_set(lpfc_sli4_sge_last, sgl, 1);
3037 else
3038 bf_set(lpfc_sli4_sge_last, sgl, 0);
3039 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
James Smartf9bb2da2011-10-10 21:34:11 -04003040 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
James Smartda0436e2009-05-22 14:51:39 -04003041 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05003042 sgl->sge_len = cpu_to_le32(dma_len);
James Smartda0436e2009-05-22 14:51:39 -04003043 dma_offset += dma_len;
3044 sgl++;
3045 }
James Smart0bc2b7c2018-02-22 08:18:48 -08003046 /*
3047 * Setup the first Payload BDE. For FCoE we just key off
James Smart414abe02018-06-26 08:24:26 -07003048 * Performance Hints, for FC we use lpfc_enable_pbde.
3049 * We populate words 13-15 of IOCB/WQE.
James Smart0bc2b7c2018-02-22 08:18:48 -08003050 */
3051 if ((phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) ||
James Smart414abe02018-06-26 08:24:26 -07003052 phba->cfg_enable_pbde) {
James Smartfedd3b72011-02-16 12:39:24 -05003053 bde = (struct ulp_bde64 *)
James Smart414abe02018-06-26 08:24:26 -07003054 &(iocb_cmd->unsli3.sli3Words[5]);
James Smartfedd3b72011-02-16 12:39:24 -05003055 bde->addrLow = first_data_sgl->addr_lo;
3056 bde->addrHigh = first_data_sgl->addr_hi;
3057 bde->tus.f.bdeSize =
3058 le32_to_cpu(first_data_sgl->sge_len);
3059 bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
3060 bde->tus.w = cpu_to_le32(bde->tus.w);
3061 }
James Smartda0436e2009-05-22 14:51:39 -04003062 } else {
3063 sgl += 1;
3064 /* clear the last flag in the fcp_rsp map entry */
3065 sgl->word2 = le32_to_cpu(sgl->word2);
3066 bf_set(lpfc_sli4_sge_last, sgl, 1);
3067 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart414abe02018-06-26 08:24:26 -07003068
3069 if ((phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) ||
3070 phba->cfg_enable_pbde) {
3071 bde = (struct ulp_bde64 *)
3072 &(iocb_cmd->unsli3.sli3Words[5]);
3073 memset(bde, 0, (sizeof(uint32_t) * 3));
3074 }
James Smartda0436e2009-05-22 14:51:39 -04003075 }
3076
3077 /*
3078 * Finish initializing those IOCB fields that are dependent on the
3079 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
3080 * explicitly reinitialized.
3081 * all iocb memory resources are reused.
3082 */
3083 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
3084
3085 /*
3086 * Due to difference in data length between DIF/non-DIF paths,
3087 * we need to set word 4 of IOCB here
3088 */
3089 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
James Smart1ba981f2014-02-20 09:56:45 -05003090
3091 /*
3092 * If the OAS driver feature is enabled and the lun is enabled for
3093 * OAS, set the oas iocb related flags.
3094 */
James Smartf38fa0b2014-04-04 13:52:21 -04003095 if ((phba->cfg_fof) && ((struct lpfc_device_data *)
James Smartc92c8412016-07-06 12:36:05 -07003096 scsi_cmnd->device->hostdata)->oas_enabled) {
James Smart9bd2bff52014-09-03 12:57:30 -04003097 lpfc_cmd->cur_iocbq.iocb_flag |= (LPFC_IO_OAS | LPFC_IO_FOF);
James Smartc92c8412016-07-06 12:36:05 -07003098 lpfc_cmd->cur_iocbq.priority = ((struct lpfc_device_data *)
3099 scsi_cmnd->device->hostdata)->priority;
3100 }
James Smartc4908502019-01-28 11:14:28 -08003101
James Smartda0436e2009-05-22 14:51:39 -04003102 return 0;
3103}
3104
3105/**
James Smartacd68592012-01-18 16:25:09 -05003106 * lpfc_bg_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3107 * @phba: The Hba for which this call is being executed.
3108 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3109 *
3110 * This is the protection/DIF aware version of
3111 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
3112 * two functions eventually, but for now, it's here
3113 **/
3114static int
3115lpfc_bg_scsi_prep_dma_buf_s4(struct lpfc_hba *phba,
James Smartc4908502019-01-28 11:14:28 -08003116 struct lpfc_io_buf *lpfc_cmd)
James Smartacd68592012-01-18 16:25:09 -05003117{
3118 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3119 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart0794d602019-01-28 11:14:19 -08003120 struct sli4_sge *sgl = (struct sli4_sge *)(lpfc_cmd->dma_sgl);
James Smartacd68592012-01-18 16:25:09 -05003121 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart96f70772013-04-17 20:16:15 -04003122 uint32_t num_sge = 0;
James Smartacd68592012-01-18 16:25:09 -05003123 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
3124 int prot_group_type = 0;
3125 int fcpdl;
James Smart7c4042a2018-11-29 16:09:40 -08003126 struct lpfc_vport *vport = phba->pport;
James Smartacd68592012-01-18 16:25:09 -05003127
3128 /*
3129 * Start the lpfc command prep by bumping the sgl beyond fcp_cmnd
James Smart96f70772013-04-17 20:16:15 -04003130 * fcp_rsp regions to the first data sge entry
James Smartacd68592012-01-18 16:25:09 -05003131 */
3132 if (scsi_sg_count(scsi_cmnd)) {
3133 /*
3134 * The driver stores the segment count returned from pci_map_sg
3135 * because this a count of dma-mappings used to map the use_sg
3136 * pages. They are not guaranteed to be the same for those
3137 * architectures that implement an IOMMU.
3138 */
3139 datasegcnt = dma_map_sg(&phba->pcidev->dev,
3140 scsi_sglist(scsi_cmnd),
3141 scsi_sg_count(scsi_cmnd), datadir);
3142 if (unlikely(!datasegcnt))
3143 return 1;
3144
3145 sgl += 1;
3146 /* clear the last flag in the fcp_rsp map entry */
3147 sgl->word2 = le32_to_cpu(sgl->word2);
3148 bf_set(lpfc_sli4_sge_last, sgl, 0);
3149 sgl->word2 = cpu_to_le32(sgl->word2);
3150
3151 sgl += 1;
3152 lpfc_cmd->seg_cnt = datasegcnt;
James Smart96f70772013-04-17 20:16:15 -04003153
3154 /* First check if data segment count from SCSI Layer is good */
3155 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt)
3156 goto err;
James Smartacd68592012-01-18 16:25:09 -05003157
3158 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
3159
3160 switch (prot_group_type) {
3161 case LPFC_PG_TYPE_NO_DIF:
James Smart96f70772013-04-17 20:16:15 -04003162 /* Here we need to add a DISEED to the count */
3163 if ((lpfc_cmd->seg_cnt + 1) > phba->cfg_total_seg_cnt)
3164 goto err;
3165
3166 num_sge = lpfc_bg_setup_sgl(phba, scsi_cmnd, sgl,
James Smartacd68592012-01-18 16:25:09 -05003167 datasegcnt);
James Smart96f70772013-04-17 20:16:15 -04003168
James Smartacd68592012-01-18 16:25:09 -05003169 /* we should have 2 or more entries in buffer list */
James Smart96f70772013-04-17 20:16:15 -04003170 if (num_sge < 2)
James Smartacd68592012-01-18 16:25:09 -05003171 goto err;
3172 break;
James Smart96f70772013-04-17 20:16:15 -04003173
3174 case LPFC_PG_TYPE_DIF_BUF:
James Smartacd68592012-01-18 16:25:09 -05003175 /*
3176 * This type indicates that protection buffers are
3177 * passed to the driver, so that needs to be prepared
3178 * for DMA
3179 */
3180 protsegcnt = dma_map_sg(&phba->pcidev->dev,
3181 scsi_prot_sglist(scsi_cmnd),
3182 scsi_prot_sg_count(scsi_cmnd), datadir);
3183 if (unlikely(!protsegcnt)) {
3184 scsi_dma_unmap(scsi_cmnd);
3185 return 1;
3186 }
3187
3188 lpfc_cmd->prot_seg_cnt = protsegcnt;
James Smart96f70772013-04-17 20:16:15 -04003189 /*
3190 * There is a minimun of 3 SGEs used for every
3191 * protection data segment.
3192 */
3193 if ((lpfc_cmd->prot_seg_cnt * 3) >
3194 (phba->cfg_total_seg_cnt - 2))
3195 goto err;
James Smartacd68592012-01-18 16:25:09 -05003196
James Smart96f70772013-04-17 20:16:15 -04003197 num_sge = lpfc_bg_setup_sgl_prot(phba, scsi_cmnd, sgl,
James Smartacd68592012-01-18 16:25:09 -05003198 datasegcnt, protsegcnt);
James Smart96f70772013-04-17 20:16:15 -04003199
James Smartacd68592012-01-18 16:25:09 -05003200 /* we should have 3 or more entries in buffer list */
James Smart96f70772013-04-17 20:16:15 -04003201 if ((num_sge < 3) ||
3202 (num_sge > phba->cfg_total_seg_cnt))
James Smartacd68592012-01-18 16:25:09 -05003203 goto err;
3204 break;
James Smart96f70772013-04-17 20:16:15 -04003205
James Smartacd68592012-01-18 16:25:09 -05003206 case LPFC_PG_TYPE_INVALID:
3207 default:
James Smart96f70772013-04-17 20:16:15 -04003208 scsi_dma_unmap(scsi_cmnd);
3209 lpfc_cmd->seg_cnt = 0;
3210
James Smartacd68592012-01-18 16:25:09 -05003211 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
3212 "9083 Unexpected protection group %i\n",
3213 prot_group_type);
3214 return 1;
3215 }
3216 }
3217
James Smart8012cc32012-10-31 14:44:49 -04003218 switch (scsi_get_prot_op(scsi_cmnd)) {
3219 case SCSI_PROT_WRITE_STRIP:
3220 case SCSI_PROT_READ_STRIP:
3221 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_STRIP;
3222 break;
3223 case SCSI_PROT_WRITE_INSERT:
3224 case SCSI_PROT_READ_INSERT:
3225 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_INSERT;
3226 break;
3227 case SCSI_PROT_WRITE_PASS:
3228 case SCSI_PROT_READ_PASS:
3229 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_PASS;
3230 break;
3231 }
3232
James Smartacd68592012-01-18 16:25:09 -05003233 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
James Smartacd68592012-01-18 16:25:09 -05003234 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
3235
3236 /*
3237 * Due to difference in data length between DIF/non-DIF paths,
3238 * we need to set word 4 of IOCB here
3239 */
3240 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
James Smartacd68592012-01-18 16:25:09 -05003241
James Smart9bd2bff52014-09-03 12:57:30 -04003242 /*
James Smart7c4042a2018-11-29 16:09:40 -08003243 * For First burst, we may need to adjust the initial transfer
3244 * length for DIF
3245 */
3246 if (iocb_cmd->un.fcpi.fcpi_XRdy &&
3247 (fcpdl < vport->cfg_first_burst_size))
3248 iocb_cmd->un.fcpi.fcpi_XRdy = fcpdl;
3249
3250 /*
James Smart9bd2bff52014-09-03 12:57:30 -04003251 * If the OAS driver feature is enabled and the lun is enabled for
3252 * OAS, set the oas iocb related flags.
3253 */
3254 if ((phba->cfg_fof) && ((struct lpfc_device_data *)
3255 scsi_cmnd->device->hostdata)->oas_enabled)
3256 lpfc_cmd->cur_iocbq.iocb_flag |= (LPFC_IO_OAS | LPFC_IO_FOF);
3257
James Smartacd68592012-01-18 16:25:09 -05003258 return 0;
3259err:
James Smart96f70772013-04-17 20:16:15 -04003260 if (lpfc_cmd->seg_cnt)
3261 scsi_dma_unmap(scsi_cmnd);
3262 if (lpfc_cmd->prot_seg_cnt)
3263 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
3264 scsi_prot_sg_count(scsi_cmnd),
3265 scsi_cmnd->sc_data_direction);
3266
James Smartacd68592012-01-18 16:25:09 -05003267 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart96f70772013-04-17 20:16:15 -04003268 "9084 Cannot setup S/G List for HBA"
3269 "IO segs %d/%d SGL %d SCSI %d: %d %d\n",
3270 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
3271 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
3272 prot_group_type, num_sge);
3273
3274 lpfc_cmd->seg_cnt = 0;
3275 lpfc_cmd->prot_seg_cnt = 0;
James Smartacd68592012-01-18 16:25:09 -05003276 return 1;
3277}
3278
3279/**
James Smart3772a992009-05-22 14:50:54 -04003280 * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3281 * @phba: The Hba for which this call is being executed.
3282 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3283 *
3284 * This routine wraps the actual DMA mapping function pointer from the
3285 * lpfc_hba struct.
3286 *
3287 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04003288 * 1 - Error
3289 * 0 - Success
James Smart3772a992009-05-22 14:50:54 -04003290 **/
3291static inline int
James Smartc4908502019-01-28 11:14:28 -08003292lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
James Smart3772a992009-05-22 14:50:54 -04003293{
3294 return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
3295}
3296
3297/**
James Smartacd68592012-01-18 16:25:09 -05003298 * lpfc_bg_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3299 * using BlockGuard.
3300 * @phba: The Hba for which this call is being executed.
3301 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3302 *
3303 * This routine wraps the actual DMA mapping function pointer from the
3304 * lpfc_hba struct.
3305 *
3306 * Return codes:
3307 * 1 - Error
3308 * 0 - Success
3309 **/
3310static inline int
James Smartc4908502019-01-28 11:14:28 -08003311lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
James Smartacd68592012-01-18 16:25:09 -05003312{
3313 return phba->lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
3314}
3315
3316/**
James Smart3621a712009-04-06 18:47:14 -04003317 * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
James Smartea2151b2008-09-07 11:52:10 -04003318 * @phba: Pointer to hba context object.
3319 * @vport: Pointer to vport object.
3320 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
3321 * @rsp_iocb: Pointer to response iocb object which reported error.
3322 *
3323 * This function posts an event when there is a SCSI command reporting
3324 * error from the scsi device.
3325 **/
3326static void
3327lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
James Smartc4908502019-01-28 11:14:28 -08003328 struct lpfc_io_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
James Smartea2151b2008-09-07 11:52:10 -04003329 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3330 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
3331 uint32_t resp_info = fcprsp->rspStatus2;
3332 uint32_t scsi_status = fcprsp->rspStatus3;
3333 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
3334 struct lpfc_fast_path_event *fast_path_evt = NULL;
3335 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
3336 unsigned long flags;
3337
James Smart5989b8d2010-10-22 11:06:56 -04003338 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3339 return;
3340
James Smartea2151b2008-09-07 11:52:10 -04003341 /* If there is queuefull or busy condition send a scsi event */
3342 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
3343 (cmnd->result == SAM_STAT_BUSY)) {
3344 fast_path_evt = lpfc_alloc_fast_evt(phba);
3345 if (!fast_path_evt)
3346 return;
3347 fast_path_evt->un.scsi_evt.event_type =
3348 FC_REG_SCSI_EVENT;
3349 fast_path_evt->un.scsi_evt.subcategory =
3350 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
3351 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
3352 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
3353 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
3354 &pnode->nlp_portname, sizeof(struct lpfc_name));
3355 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
3356 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3357 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
3358 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
3359 fast_path_evt = lpfc_alloc_fast_evt(phba);
3360 if (!fast_path_evt)
3361 return;
3362 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
3363 FC_REG_SCSI_EVENT;
3364 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
3365 LPFC_EVENT_CHECK_COND;
3366 fast_path_evt->un.check_cond_evt.scsi_event.lun =
3367 cmnd->device->lun;
3368 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
3369 &pnode->nlp_portname, sizeof(struct lpfc_name));
3370 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
3371 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3372 fast_path_evt->un.check_cond_evt.sense_key =
3373 cmnd->sense_buffer[2] & 0xf;
3374 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
3375 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
3376 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3377 fcpi_parm &&
3378 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
3379 ((scsi_status == SAM_STAT_GOOD) &&
3380 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
3381 /*
3382 * If status is good or resid does not match with fcp_param and
3383 * there is valid fcpi_parm, then there is a read_check error
3384 */
3385 fast_path_evt = lpfc_alloc_fast_evt(phba);
3386 if (!fast_path_evt)
3387 return;
3388 fast_path_evt->un.read_check_error.header.event_type =
3389 FC_REG_FABRIC_EVENT;
3390 fast_path_evt->un.read_check_error.header.subcategory =
3391 LPFC_EVENT_FCPRDCHKERR;
3392 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
3393 &pnode->nlp_portname, sizeof(struct lpfc_name));
3394 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
3395 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3396 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
3397 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
3398 fast_path_evt->un.read_check_error.fcpiparam =
3399 fcpi_parm;
3400 } else
3401 return;
3402
3403 fast_path_evt->vport = vport;
3404 spin_lock_irqsave(&phba->hbalock, flags);
3405 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
3406 spin_unlock_irqrestore(&phba->hbalock, flags);
3407 lpfc_worker_wake_up(phba);
3408 return;
3409}
James Smart9bad7672008-12-04 22:39:02 -05003410
3411/**
James Smartf1126682009-06-10 17:22:44 -04003412 * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
James Smart3772a992009-05-22 14:50:54 -04003413 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -05003414 * @psb: The scsi buffer which is going to be un-mapped.
3415 *
3416 * This routine does DMA un-mapping of scatter gather list of scsi command
James Smart3772a992009-05-22 14:50:54 -04003417 * field of @lpfc_cmd for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05003418 **/
dea31012005-04-17 16:05:31 -05003419static void
James Smartc4908502019-01-28 11:14:28 -08003420lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_io_buf *psb)
James Smartbcf4dbf2006-07-06 15:50:08 -04003421{
3422 /*
3423 * There are only two special cases to consider. (1) the scsi command
3424 * requested scatter-gather usage or (2) the scsi command allocated
3425 * a request buffer, but did not request use_sg. There is a third
3426 * case, but it does not require resource deallocation.
3427 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003428 if (psb->seg_cnt > 0)
3429 scsi_dma_unmap(psb->pCmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003430 if (psb->prot_seg_cnt > 0)
3431 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
3432 scsi_prot_sg_count(psb->pCmd),
3433 psb->pCmd->sc_data_direction);
James Smartbcf4dbf2006-07-06 15:50:08 -04003434}
3435
James Smart9bad7672008-12-04 22:39:02 -05003436/**
James Smart3621a712009-04-06 18:47:14 -04003437 * lpfc_handler_fcp_err - FCP response handler
James Smart9bad7672008-12-04 22:39:02 -05003438 * @vport: The virtual port for which this call is being executed.
James Smartc4908502019-01-28 11:14:28 -08003439 * @lpfc_cmd: Pointer to lpfc_io_buf data structure.
James Smart9bad7672008-12-04 22:39:02 -05003440 * @rsp_iocb: The response IOCB which contains FCP error.
3441 *
3442 * This routine is called to process response IOCB with status field
3443 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
3444 * based upon SCSI and FCP error.
3445 **/
James Smartbcf4dbf2006-07-06 15:50:08 -04003446static void
James Smartc4908502019-01-28 11:14:28 -08003447lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_io_buf *lpfc_cmd,
James Smart2e0fef82007-06-17 19:56:36 -05003448 struct lpfc_iocbq *rsp_iocb)
dea31012005-04-17 16:05:31 -05003449{
James Smart5afab6b2015-12-16 18:12:01 -05003450 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003451 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3452 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
3453 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
James Smart7054a602007-04-25 09:52:34 -04003454 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
dea31012005-04-17 16:05:31 -05003455 uint32_t resp_info = fcprsp->rspStatus2;
3456 uint32_t scsi_status = fcprsp->rspStatus3;
James Smartc7743952006-12-02 13:34:42 -05003457 uint32_t *lp;
dea31012005-04-17 16:05:31 -05003458 uint32_t host_status = DID_OK;
3459 uint32_t rsplen = 0;
James Smart5afab6b2015-12-16 18:12:01 -05003460 uint32_t fcpDl;
James Smartc7743952006-12-02 13:34:42 -05003461 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea31012005-04-17 16:05:31 -05003462
James Smartea2151b2008-09-07 11:52:10 -04003463
dea31012005-04-17 16:05:31 -05003464 /*
3465 * If this is a task management command, there is no
3466 * scsi packet associated with this lpfc_cmd. The driver
3467 * consumes it.
3468 */
3469 if (fcpcmd->fcpCntl2) {
3470 scsi_status = 0;
3471 goto out;
3472 }
3473
James Smart6a9c52c2009-10-02 15:16:51 -04003474 if (resp_info & RSP_LEN_VALID) {
3475 rsplen = be32_to_cpu(fcprsp->rspRspLen);
James Smarte40a02c2010-02-26 14:13:54 -05003476 if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
James Smart6a9c52c2009-10-02 15:16:51 -04003477 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3478 "2719 Invalid response length: "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02003479 "tgt x%x lun x%llx cmnd x%x rsplen x%x\n",
James Smart6a9c52c2009-10-02 15:16:51 -04003480 cmnd->device->id,
3481 cmnd->device->lun, cmnd->cmnd[0],
3482 rsplen);
3483 host_status = DID_ERROR;
3484 goto out;
3485 }
James Smarte40a02c2010-02-26 14:13:54 -05003486 if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
3487 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3488 "2757 Protocol failure detected during "
3489 "processing of FCP I/O op: "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02003490 "tgt x%x lun x%llx cmnd x%x rspInfo3 x%x\n",
James Smarte40a02c2010-02-26 14:13:54 -05003491 cmnd->device->id,
3492 cmnd->device->lun, cmnd->cmnd[0],
3493 fcprsp->rspInfo3);
3494 host_status = DID_ERROR;
3495 goto out;
3496 }
James Smart6a9c52c2009-10-02 15:16:51 -04003497 }
3498
James Smartc7743952006-12-02 13:34:42 -05003499 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
3500 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
3501 if (snslen > SCSI_SENSE_BUFFERSIZE)
3502 snslen = SCSI_SENSE_BUFFERSIZE;
3503
3504 if (resp_info & RSP_LEN_VALID)
3505 rsplen = be32_to_cpu(fcprsp->rspRspLen);
3506 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
3507 }
3508 lp = (uint32_t *)cmnd->sense_buffer;
3509
James Smartaa1c7ee2012-08-14 14:26:06 -04003510 /* special handling for under run conditions */
3511 if (!scsi_status && (resp_info & RESID_UNDER)) {
3512 /* don't log under runs if fcp set... */
3513 if (vport->cfg_log_verbose & LOG_FCP)
3514 logit = LOG_FCP_ERROR;
3515 /* unless operator says so */
3516 if (vport->cfg_log_verbose & LOG_FCP_UNDER)
3517 logit = LOG_FCP_UNDER;
3518 }
James Smartc7743952006-12-02 13:34:42 -05003519
James Smarte8b62012007-08-02 11:10:09 -04003520 lpfc_printf_vlog(vport, KERN_WARNING, logit,
James Smarte2a0a9d2008-12-04 22:40:02 -05003521 "9024 FCP command x%x failed: x%x SNS x%x x%x "
James Smarte8b62012007-08-02 11:10:09 -04003522 "Data: x%x x%x x%x x%x x%x\n",
3523 cmnd->cmnd[0], scsi_status,
3524 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
3525 be32_to_cpu(fcprsp->rspResId),
3526 be32_to_cpu(fcprsp->rspSnsLen),
3527 be32_to_cpu(fcprsp->rspRspLen),
3528 fcprsp->rspInfo3);
dea31012005-04-17 16:05:31 -05003529
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003530 scsi_set_resid(cmnd, 0);
James Smart5afab6b2015-12-16 18:12:01 -05003531 fcpDl = be32_to_cpu(fcpcmd->fcpDl);
dea31012005-04-17 16:05:31 -05003532 if (resp_info & RESID_UNDER) {
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003533 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea31012005-04-17 16:05:31 -05003534
James Smart73d91e52011-10-10 21:32:10 -04003535 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_UNDER,
James Smart45634a862018-01-30 15:59:00 -08003536 "9025 FCP Underrun, expected %d, "
James Smarte8b62012007-08-02 11:10:09 -04003537 "residual %d Data: x%x x%x x%x\n",
James Smart5afab6b2015-12-16 18:12:01 -05003538 fcpDl,
James Smarte8b62012007-08-02 11:10:09 -04003539 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
3540 cmnd->underflow);
dea31012005-04-17 16:05:31 -05003541
3542 /*
James Smart45634a862018-01-30 15:59:00 -08003543 * If there is an under run, check if under run reported by
James Smart7054a602007-04-25 09:52:34 -04003544 * storage array is same as the under run reported by HBA.
3545 * If this is not same, there is a dropped frame.
3546 */
James Smart45634a862018-01-30 15:59:00 -08003547 if (fcpi_parm && (scsi_get_resid(cmnd) != fcpi_parm)) {
James Smarte8b62012007-08-02 11:10:09 -04003548 lpfc_printf_vlog(vport, KERN_WARNING,
3549 LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05003550 "9026 FCP Read Check Error "
James Smarte8b62012007-08-02 11:10:09 -04003551 "and Underrun Data: x%x x%x x%x x%x\n",
James Smart5afab6b2015-12-16 18:12:01 -05003552 fcpDl,
James Smarte8b62012007-08-02 11:10:09 -04003553 scsi_get_resid(cmnd), fcpi_parm,
3554 cmnd->cmnd[0]);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003555 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
James Smart7054a602007-04-25 09:52:34 -04003556 host_status = DID_ERROR;
3557 }
3558 /*
dea31012005-04-17 16:05:31 -05003559 * The cmnd->underflow is the minimum number of bytes that must
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003560 * be transferred for this command. Provided a sense condition
dea31012005-04-17 16:05:31 -05003561 * is not present, make sure the actual amount transferred is at
3562 * least the underflow value or fail.
3563 */
3564 if (!(resp_info & SNS_LEN_VALID) &&
3565 (scsi_status == SAM_STAT_GOOD) &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003566 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
3567 < cmnd->underflow)) {
James Smarte8b62012007-08-02 11:10:09 -04003568 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05003569 "9027 FCP command x%x residual "
James Smarte8b62012007-08-02 11:10:09 -04003570 "underrun converted to error "
3571 "Data: x%x x%x x%x\n",
James Smart66dbfbe2007-08-05 06:08:38 -04003572 cmnd->cmnd[0], scsi_bufflen(cmnd),
James Smarte8b62012007-08-02 11:10:09 -04003573 scsi_get_resid(cmnd), cmnd->underflow);
dea31012005-04-17 16:05:31 -05003574 host_status = DID_ERROR;
3575 }
3576 } else if (resp_info & RESID_OVER) {
James Smarte8b62012007-08-02 11:10:09 -04003577 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05003578 "9028 FCP command x%x residual overrun error. "
James Smarte4e74272009-07-19 10:01:38 -04003579 "Data: x%x x%x\n", cmnd->cmnd[0],
James Smarte8b62012007-08-02 11:10:09 -04003580 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea31012005-04-17 16:05:31 -05003581 host_status = DID_ERROR;
3582
3583 /*
3584 * Check SLI validation that all the transfer was actually done
James Smart26373d22013-09-06 12:19:17 -04003585 * (fcpi_parm should be zero). Apply check only to reads.
dea31012005-04-17 16:05:31 -05003586 */
James Smart5afab6b2015-12-16 18:12:01 -05003587 } else if (fcpi_parm) {
James Smarte8b62012007-08-02 11:10:09 -04003588 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
James Smart5afab6b2015-12-16 18:12:01 -05003589 "9029 FCP %s Check Error xri x%x Data: "
James Smarteee88772010-09-29 11:19:08 -04003590 "x%x x%x x%x x%x x%x\n",
James Smart5afab6b2015-12-16 18:12:01 -05003591 ((cmnd->sc_data_direction == DMA_FROM_DEVICE) ?
3592 "Read" : "Write"),
3593 ((phba->sli_rev == LPFC_SLI_REV4) ?
3594 lpfc_cmd->cur_iocbq.sli4_xritag :
3595 rsp_iocb->iocb.ulpContext),
3596 fcpDl, be32_to_cpu(fcprsp->rspResId),
James Smarteee88772010-09-29 11:19:08 -04003597 fcpi_parm, cmnd->cmnd[0], scsi_status);
James Smart5afab6b2015-12-16 18:12:01 -05003598
3599 /* There is some issue with the LPe12000 that causes it
3600 * to miscalculate the fcpi_parm and falsely trip this
3601 * recovery logic. Detect this case and don't error when true.
3602 */
3603 if (fcpi_parm > fcpDl)
3604 goto out;
3605
James Smarteee88772010-09-29 11:19:08 -04003606 switch (scsi_status) {
3607 case SAM_STAT_GOOD:
3608 case SAM_STAT_CHECK_CONDITION:
3609 /* Fabric dropped a data frame. Fail any successful
3610 * command in which we detected dropped frames.
3611 * A status of good or some check conditions could
3612 * be considered a successful command.
3613 */
3614 host_status = DID_ERROR;
3615 break;
3616 }
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003617 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea31012005-04-17 16:05:31 -05003618 }
3619
3620 out:
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02003621 cmnd->result = host_status << 16 | scsi_status;
James Smartea2151b2008-09-07 11:52:10 -04003622 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
dea31012005-04-17 16:05:31 -05003623}
3624
James Smart9bad7672008-12-04 22:39:02 -05003625/**
James Smart3621a712009-04-06 18:47:14 -04003626 * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
James Smart9bad7672008-12-04 22:39:02 -05003627 * @phba: The Hba for which this call is being executed.
3628 * @pIocbIn: The command IOCBQ for the scsi cmnd.
James Smart3772a992009-05-22 14:50:54 -04003629 * @pIocbOut: The response IOCBQ for the scsi cmnd.
James Smart9bad7672008-12-04 22:39:02 -05003630 *
3631 * This routine assigns scsi command result by looking into response IOCB
3632 * status field appropriately. This routine handles QUEUE FULL condition as
3633 * well by ramping down device queue depth.
3634 **/
dea31012005-04-17 16:05:31 -05003635static void
3636lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
3637 struct lpfc_iocbq *pIocbOut)
3638{
James Smartc4908502019-01-28 11:14:28 -08003639 struct lpfc_io_buf *lpfc_cmd =
3640 (struct lpfc_io_buf *) pIocbIn->context1;
James Smart2e0fef82007-06-17 19:56:36 -05003641 struct lpfc_vport *vport = pIocbIn->vport;
dea31012005-04-17 16:05:31 -05003642 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
3643 struct lpfc_nodelist *pnode = rdata->pnode;
James Smart75baf692010-06-08 18:31:21 -04003644 struct scsi_cmnd *cmd;
James Smartfa61a542008-01-11 01:52:42 -05003645 unsigned long flags;
James Smartea2151b2008-09-07 11:52:10 -04003646 struct lpfc_fast_path_event *fast_path_evt;
James Smart75baf692010-06-08 18:31:21 -04003647 struct Scsi_Host *shost;
James Smart4c47efc2019-01-28 11:14:25 -08003648 int idx;
James Smart73d91e52011-10-10 21:32:10 -04003649 uint32_t logit = LOG_FCP;
James Smart6a828b02019-01-28 11:14:31 -08003650#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
3651 int cpu;
3652#endif
dea31012005-04-17 16:05:31 -05003653
James Smartc2017262019-01-28 11:14:37 -08003654 /* Guard against abort handler being called at same time */
3655 spin_lock(&lpfc_cmd->buf_lock);
3656
James Smart75baf692010-06-08 18:31:21 -04003657 /* Sanity check on return of outstanding command */
James Smart75baf692010-06-08 18:31:21 -04003658 cmd = lpfc_cmd->pCmd;
James Smartc2017262019-01-28 11:14:37 -08003659 if (!cmd) {
3660 lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
3661 "2621 IO completion: Not an active IO\n");
3662 spin_unlock(&lpfc_cmd->buf_lock);
James Smartc90261d2015-12-16 18:11:57 -05003663 return;
James Smartc2017262019-01-28 11:14:37 -08003664 }
James Smart4c47efc2019-01-28 11:14:25 -08003665
3666 idx = lpfc_cmd->cur_iocbq.hba_wqidx;
3667 if (phba->sli4_hba.hdwq)
3668 phba->sli4_hba.hdwq[idx].scsi_cstat.io_cmpls++;
3669
James Smart6a828b02019-01-28 11:14:31 -08003670#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
3671 if (phba->cpucheck_on & LPFC_CHECK_SCSI_IO) {
3672 cpu = smp_processor_id();
James Smartff6bf892019-03-12 16:30:04 -07003673 if (cpu < LPFC_CHECK_CPU_CNT && phba->sli4_hba.hdwq)
James Smart6a828b02019-01-28 11:14:31 -08003674 phba->sli4_hba.hdwq[idx].cpucheck_cmpl_io[cpu]++;
3675 }
3676#endif
James Smart75baf692010-06-08 18:31:21 -04003677 shost = cmd->device->host;
3678
James Smarte3d2b802012-08-14 14:25:43 -04003679 lpfc_cmd->result = (pIocbOut->iocb.un.ulpWord[4] & IOERR_PARAM_MASK);
dea31012005-04-17 16:05:31 -05003680 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
James Smart341af102010-01-26 23:07:37 -05003681 /* pick up SLI4 exhange busy status from HBA */
3682 lpfc_cmd->exch_busy = pIocbOut->iocb_flag & LPFC_EXCHANGE_BUSY;
3683
James Smart9a6b09c2012-03-01 22:37:42 -05003684#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
3685 if (lpfc_cmd->prot_data_type) {
3686 struct scsi_dif_tuple *src = NULL;
3687
3688 src = (struct scsi_dif_tuple *)lpfc_cmd->prot_data_segment;
3689 /*
3690 * Used to restore any changes to protection
3691 * data for error injection.
3692 */
3693 switch (lpfc_cmd->prot_data_type) {
3694 case LPFC_INJERR_REFTAG:
3695 src->ref_tag =
3696 lpfc_cmd->prot_data;
3697 break;
3698 case LPFC_INJERR_APPTAG:
3699 src->app_tag =
3700 (uint16_t)lpfc_cmd->prot_data;
3701 break;
3702 case LPFC_INJERR_GUARD:
3703 src->guard_tag =
3704 (uint16_t)lpfc_cmd->prot_data;
3705 break;
3706 default:
3707 break;
3708 }
3709
3710 lpfc_cmd->prot_data = 0;
3711 lpfc_cmd->prot_data_type = 0;
3712 lpfc_cmd->prot_data_segment = NULL;
3713 }
3714#endif
James Smart2ea259e2017-02-12 13:52:27 -08003715
dea31012005-04-17 16:05:31 -05003716 if (lpfc_cmd->status) {
3717 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
3718 (lpfc_cmd->result & IOERR_DRVR_MASK))
3719 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
3720 else if (lpfc_cmd->status >= IOSTAT_CNT)
3721 lpfc_cmd->status = IOSTAT_DEFAULT;
James Smartaa1c7ee2012-08-14 14:26:06 -04003722 if (lpfc_cmd->status == IOSTAT_FCP_RSP_ERROR &&
3723 !lpfc_cmd->fcp_rsp->rspStatus3 &&
3724 (lpfc_cmd->fcp_rsp->rspStatus2 & RESID_UNDER) &&
3725 !(vport->cfg_log_verbose & LOG_FCP_UNDER))
James Smart73d91e52011-10-10 21:32:10 -04003726 logit = 0;
3727 else
3728 logit = LOG_FCP | LOG_FCP_UNDER;
3729 lpfc_printf_vlog(vport, KERN_WARNING, logit,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02003730 "9030 FCP cmd x%x failed <%d/%lld> "
James Smart5a0d80f2012-05-09 21:18:20 -04003731 "status: x%x result: x%x "
3732 "sid: x%x did: x%x oxid: x%x "
3733 "Data: x%x x%x\n",
James Smart73d91e52011-10-10 21:32:10 -04003734 cmd->cmnd[0],
3735 cmd->device ? cmd->device->id : 0xffff,
3736 cmd->device ? cmd->device->lun : 0xffff,
3737 lpfc_cmd->status, lpfc_cmd->result,
James Smart3bf41ba2013-05-31 17:03:18 -04003738 vport->fc_myDID,
3739 (pnode) ? pnode->nlp_DID : 0,
James Smart5a0d80f2012-05-09 21:18:20 -04003740 phba->sli_rev == LPFC_SLI_REV4 ?
3741 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
James Smart73d91e52011-10-10 21:32:10 -04003742 pIocbOut->iocb.ulpContext,
3743 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
dea31012005-04-17 16:05:31 -05003744
3745 switch (lpfc_cmd->status) {
3746 case IOSTAT_FCP_RSP_ERROR:
3747 /* Call FCP RSP handler to determine result */
James Smart2e0fef82007-06-17 19:56:36 -05003748 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea31012005-04-17 16:05:31 -05003749 break;
3750 case IOSTAT_NPORT_BSY:
3751 case IOSTAT_FABRIC_BSY:
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02003752 cmd->result = DID_TRANSPORT_DISRUPTED << 16;
James Smartea2151b2008-09-07 11:52:10 -04003753 fast_path_evt = lpfc_alloc_fast_evt(phba);
3754 if (!fast_path_evt)
3755 break;
3756 fast_path_evt->un.fabric_evt.event_type =
3757 FC_REG_FABRIC_EVENT;
3758 fast_path_evt->un.fabric_evt.subcategory =
3759 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
3760 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
3761 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
3762 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
3763 &pnode->nlp_portname,
3764 sizeof(struct lpfc_name));
3765 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
3766 &pnode->nlp_nodename,
3767 sizeof(struct lpfc_name));
3768 }
3769 fast_path_evt->vport = vport;
3770 fast_path_evt->work_evt.evt =
3771 LPFC_EVT_FASTPATH_MGMT_EVT;
3772 spin_lock_irqsave(&phba->hbalock, flags);
3773 list_add_tail(&fast_path_evt->work_evt.evt_listp,
3774 &phba->work_list);
3775 spin_unlock_irqrestore(&phba->hbalock, flags);
3776 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05003777 break;
James Smart92d7f7b2007-06-17 19:56:38 -05003778 case IOSTAT_LOCAL_REJECT:
James Smart1151e3e2011-02-16 12:39:35 -05003779 case IOSTAT_REMOTE_STOP:
James Smartab56dc22011-02-16 12:39:57 -05003780 if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
3781 lpfc_cmd->result ==
3782 IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
3783 lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
3784 lpfc_cmd->result ==
3785 IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02003786 cmd->result = DID_NO_CONNECT << 16;
James Smartab56dc22011-02-16 12:39:57 -05003787 break;
3788 }
James Smartd7c255b2008-08-24 21:50:00 -04003789 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
James Smart92d7f7b2007-06-17 19:56:38 -05003790 lpfc_cmd->result == IOERR_NO_RESOURCES ||
James Smartb92938b2010-06-07 15:24:12 -04003791 lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
3792 lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02003793 cmd->result = DID_REQUEUE << 16;
James Smart58da1ff2008-04-07 10:15:56 -04003794 break;
James Smarte2a0a9d2008-12-04 22:40:02 -05003795 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003796 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
3797 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
3798 pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
3799 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
3800 /*
3801 * This is a response for a BG enabled
3802 * cmd. Parse BG error
3803 */
3804 lpfc_parse_bg_err(phba, lpfc_cmd,
3805 pIocbOut);
3806 break;
3807 } else {
3808 lpfc_printf_vlog(vport, KERN_WARNING,
3809 LOG_BG,
3810 "9031 non-zero BGSTAT "
James Smart6a9c52c2009-10-02 15:16:51 -04003811 "on unprotected cmd\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05003812 }
3813 }
James Smart1151e3e2011-02-16 12:39:35 -05003814 if ((lpfc_cmd->status == IOSTAT_REMOTE_STOP)
3815 && (phba->sli_rev == LPFC_SLI_REV4)
3816 && (pnode && NLP_CHK_NODE_ACT(pnode))) {
3817 /* This IO was aborted by the target, we don't
3818 * know the rxid and because we did not send the
3819 * ABTS we cannot generate and RRQ.
3820 */
3821 lpfc_set_rrq_active(phba, pnode,
James Smartee0f4fe2012-05-09 21:19:14 -04003822 lpfc_cmd->cur_iocbq.sli4_lxritag,
3823 0, 0);
James Smart1151e3e2011-02-16 12:39:35 -05003824 }
Bart Van Asschecd05c152019-03-28 11:06:18 -07003825 /* fall through */
dea31012005-04-17 16:05:31 -05003826 default:
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02003827 cmd->result = DID_ERROR << 16;
dea31012005-04-17 16:05:31 -05003828 break;
3829 }
3830
James Smart58da1ff2008-04-07 10:15:56 -04003831 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003832 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02003833 cmd->result = DID_TRANSPORT_DISRUPTED << 16 |
3834 SAM_STAT_BUSY;
James Smartab56dc22011-02-16 12:39:57 -05003835 } else
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02003836 cmd->result = DID_OK << 16;
dea31012005-04-17 16:05:31 -05003837
3838 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
3839 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
3840
James Smarte8b62012007-08-02 11:10:09 -04003841 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02003842 "0710 Iodone <%d/%llu> cmd %p, error "
James Smarte8b62012007-08-02 11:10:09 -04003843 "x%x SNS x%x x%x Data: x%x x%x\n",
3844 cmd->device->id, cmd->device->lun, cmd,
3845 cmd->result, *lp, *(lp + 3), cmd->retries,
3846 scsi_get_resid(cmd));
dea31012005-04-17 16:05:31 -05003847 }
3848
James Smartea2151b2008-09-07 11:52:10 -04003849 lpfc_update_stats(phba, lpfc_cmd);
James Smart977b5a02008-09-07 11:52:04 -04003850 if (vport->cfg_max_scsicmpl_time &&
3851 time_after(jiffies, lpfc_cmd->start_time +
3852 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
James Smarta257bf92009-04-06 18:48:10 -04003853 spin_lock_irqsave(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05003854 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
3855 if (pnode->cmd_qdepth >
3856 atomic_read(&pnode->cmd_pending) &&
3857 (atomic_read(&pnode->cmd_pending) >
3858 LPFC_MIN_TGT_QDEPTH) &&
3859 ((cmd->cmnd[0] == READ_10) ||
3860 (cmd->cmnd[0] == WRITE_10)))
3861 pnode->cmd_qdepth =
3862 atomic_read(&pnode->cmd_pending);
James Smart977b5a02008-09-07 11:52:04 -04003863
James Smart109f6ed2008-12-04 22:39:08 -05003864 pnode->last_change_time = jiffies;
3865 }
James Smarta257bf92009-04-06 18:48:10 -04003866 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart977b5a02008-09-07 11:52:04 -04003867 }
James Smart1dcb58e2007-04-25 09:51:30 -04003868 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James Smarta257bf92009-04-06 18:48:10 -04003869
James Smartc2017262019-01-28 11:14:37 -08003870 lpfc_cmd->pCmd = NULL;
3871 spin_unlock(&lpfc_cmd->buf_lock);
James Smart92e3af62012-08-14 14:26:28 -04003872
James Smart89533e92016-10-13 15:06:15 -07003873 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
3874 cmd->scsi_done(cmd);
3875
James Smartfa61a542008-01-11 01:52:42 -05003876 /*
James Smartc2017262019-01-28 11:14:37 -08003877 * If there is an abort thread waiting for command completion
James Smartfa61a542008-01-11 01:52:42 -05003878 * wake up the thread.
3879 */
James Smartc2017262019-01-28 11:14:37 -08003880 spin_lock(&lpfc_cmd->buf_lock);
3881 if (unlikely(lpfc_cmd->cur_iocbq.iocb_flag & LPFC_DRIVER_ABORTED)) {
3882 lpfc_cmd->cur_iocbq.iocb_flag &= ~LPFC_DRIVER_ABORTED;
3883 if (lpfc_cmd->waitq)
3884 wake_up(lpfc_cmd->waitq);
3885 lpfc_cmd->waitq = NULL;
3886 }
3887 spin_unlock(&lpfc_cmd->buf_lock);
James Smartfa61a542008-01-11 01:52:42 -05003888
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003889 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05003890}
3891
James Smart34b02dc2008-08-24 21:49:55 -04003892/**
Alexey Kardashevskiy8b2564e2015-04-28 18:26:22 +10003893 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
3894 * @data: A pointer to the immediate command data portion of the IOCB.
3895 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
3896 *
3897 * The routine copies the entire FCP command from @fcp_cmnd to @data while
3898 * byte swapping the data to big endian format for transmission on the wire.
3899 **/
3900static void
3901lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
3902{
3903 int i, j;
3904 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
3905 i += sizeof(uint32_t), j++) {
3906 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
3907 }
3908}
3909
3910/**
James Smartf1126682009-06-10 17:22:44 -04003911 * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05003912 * @vport: The virtual port for which this call is being executed.
3913 * @lpfc_cmd: The scsi command which needs to send.
3914 * @pnode: Pointer to lpfc_nodelist.
3915 *
3916 * This routine initializes fcp_cmnd and iocb data structure from scsi command
James Smart3772a992009-05-22 14:50:54 -04003917 * to transfer for device with SLI3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05003918 **/
dea31012005-04-17 16:05:31 -05003919static void
James Smartc4908502019-01-28 11:14:28 -08003920lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_io_buf *lpfc_cmd,
James Smart2e0fef82007-06-17 19:56:36 -05003921 struct lpfc_nodelist *pnode)
dea31012005-04-17 16:05:31 -05003922{
James Smart2e0fef82007-06-17 19:56:36 -05003923 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003924 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3925 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3926 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
3927 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
James Smart4c47efc2019-01-28 11:14:25 -08003928 struct lpfc_sli4_hdw_queue *hdwq = NULL;
dea31012005-04-17 16:05:31 -05003929 int datadir = scsi_cmnd->sc_data_direction;
James Smart4c47efc2019-01-28 11:14:25 -08003930 int idx;
James Smart027140e2012-08-03 12:35:44 -04003931 uint8_t *ptr;
3932 bool sli4;
James Smart98bbf5f2013-09-06 12:18:45 -04003933 uint32_t fcpdl;
dea31012005-04-17 16:05:31 -05003934
James Smart58da1ff2008-04-07 10:15:56 -04003935 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3936 return;
3937
dea31012005-04-17 16:05:31 -05003938 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
James.Smart@Emulex.Com69859dc2005-08-10 15:02:37 -04003939 /* clear task management bits */
3940 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea31012005-04-17 16:05:31 -05003941
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -04003942 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
3943 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05003944
James Smart027140e2012-08-03 12:35:44 -04003945 ptr = &fcp_cmnd->fcpCdb[0];
3946 memcpy(ptr, scsi_cmnd->cmnd, scsi_cmnd->cmd_len);
3947 if (scsi_cmnd->cmd_len < LPFC_FCP_CDB_LEN) {
3948 ptr += scsi_cmnd->cmd_len;
3949 memset(ptr, 0, (LPFC_FCP_CDB_LEN - scsi_cmnd->cmd_len));
3950 }
3951
Christoph Hellwig50668632014-10-30 14:30:06 +01003952 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
dea31012005-04-17 16:05:31 -05003953
James Smart027140e2012-08-03 12:35:44 -04003954 sli4 = (phba->sli_rev == LPFC_SLI_REV4);
James Smart6acb3482014-04-04 13:51:25 -04003955 piocbq->iocb.un.fcpi.fcpi_XRdy = 0;
James Smart1fbf9742019-01-28 11:14:26 -08003956 idx = lpfc_cmd->hdwq_no;
James Smart4c47efc2019-01-28 11:14:25 -08003957 if (phba->sli4_hba.hdwq)
3958 hdwq = &phba->sli4_hba.hdwq[idx];
James Smart027140e2012-08-03 12:35:44 -04003959
dea31012005-04-17 16:05:31 -05003960 /*
3961 * There are three possibilities here - use scatter-gather segment, use
3962 * the single mapping, or neither. Start the lpfc command prep by
3963 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
3964 * data bde entry.
3965 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003966 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05003967 if (datadir == DMA_TO_DEVICE) {
3968 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
James Smart182ba752013-07-15 18:34:05 -04003969 iocb_cmd->ulpPU = PARM_READ_CHECK;
James Smart3cb01c52013-07-15 18:35:04 -04003970 if (vport->cfg_first_burst_size &&
3971 (pnode->nlp_flag & NLP_FIRSTBURST)) {
James Smart98bbf5f2013-09-06 12:18:45 -04003972 fcpdl = scsi_bufflen(scsi_cmnd);
3973 if (fcpdl < vport->cfg_first_burst_size)
3974 piocbq->iocb.un.fcpi.fcpi_XRdy = fcpdl;
3975 else
3976 piocbq->iocb.un.fcpi.fcpi_XRdy =
3977 vport->cfg_first_burst_size;
James Smart3cb01c52013-07-15 18:35:04 -04003978 }
dea31012005-04-17 16:05:31 -05003979 fcp_cmnd->fcpCntl3 = WRITE_DATA;
James Smart4c47efc2019-01-28 11:14:25 -08003980 if (hdwq)
3981 hdwq->scsi_cstat.output_requests++;
dea31012005-04-17 16:05:31 -05003982 } else {
3983 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
3984 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05003985 fcp_cmnd->fcpCntl3 = READ_DATA;
James Smart4c47efc2019-01-28 11:14:25 -08003986 if (hdwq)
3987 hdwq->scsi_cstat.input_requests++;
dea31012005-04-17 16:05:31 -05003988 }
3989 } else {
3990 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
3991 iocb_cmd->un.fcpi.fcpi_parm = 0;
3992 iocb_cmd->ulpPU = 0;
3993 fcp_cmnd->fcpCntl3 = 0;
James Smart4c47efc2019-01-28 11:14:25 -08003994 if (hdwq)
3995 hdwq->scsi_cstat.control_requests++;
dea31012005-04-17 16:05:31 -05003996 }
Alexey Kardashevskiy8b2564e2015-04-28 18:26:22 +10003997 if (phba->sli_rev == 3 &&
3998 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
3999 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05004000 /*
4001 * Finish initializing those IOCB fields that are independent
4002 * of the scsi_cmnd request_buffer
4003 */
4004 piocbq->iocb.ulpContext = pnode->nlp_rpi;
James Smart027140e2012-08-03 12:35:44 -04004005 if (sli4)
James Smart6d368e52011-05-24 11:44:12 -04004006 piocbq->iocb.ulpContext =
4007 phba->sli4_hba.rpi_ids[pnode->nlp_rpi];
dea31012005-04-17 16:05:31 -05004008 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
4009 piocbq->iocb.ulpFCP2Rcvy = 1;
James Smart09372822008-01-11 01:52:54 -05004010 else
4011 piocbq->iocb.ulpFCP2Rcvy = 0;
dea31012005-04-17 16:05:31 -05004012
4013 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
4014 piocbq->context1 = lpfc_cmd;
4015 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
4016 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
James Smart2e0fef82007-06-17 19:56:36 -05004017 piocbq->vport = vport;
dea31012005-04-17 16:05:31 -05004018}
4019
James Smart9bad7672008-12-04 22:39:02 -05004020/**
James Smart6d368e52011-05-24 11:44:12 -04004021 * lpfc_scsi_prep_task_mgmt_cmd - Convert SLI3 scsi TM cmd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05004022 * @vport: The virtual port for which this call is being executed.
James Smartc4908502019-01-28 11:14:28 -08004023 * @lpfc_cmd: Pointer to lpfc_io_buf data structure.
James Smart9bad7672008-12-04 22:39:02 -05004024 * @lun: Logical unit number.
4025 * @task_mgmt_cmd: SCSI task management command.
4026 *
James Smart3772a992009-05-22 14:50:54 -04004027 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
4028 * for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05004029 *
4030 * Return codes:
4031 * 0 - Error
4032 * 1 - Success
4033 **/
dea31012005-04-17 16:05:31 -05004034static int
James Smartf1126682009-06-10 17:22:44 -04004035lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
James Smartc4908502019-01-28 11:14:28 -08004036 struct lpfc_io_buf *lpfc_cmd,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004037 uint64_t lun,
dea31012005-04-17 16:05:31 -05004038 uint8_t task_mgmt_cmd)
4039{
dea31012005-04-17 16:05:31 -05004040 struct lpfc_iocbq *piocbq;
4041 IOCB_t *piocb;
4042 struct fcp_cmnd *fcp_cmnd;
James Smart0b18ac42006-05-01 21:50:40 -04004043 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea31012005-04-17 16:05:31 -05004044 struct lpfc_nodelist *ndlp = rdata->pnode;
4045
James Smart58da1ff2008-04-07 10:15:56 -04004046 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
4047 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05004048 return 0;
dea31012005-04-17 16:05:31 -05004049
dea31012005-04-17 16:05:31 -05004050 piocbq = &(lpfc_cmd->cur_iocbq);
James Smart2e0fef82007-06-17 19:56:36 -05004051 piocbq->vport = vport;
4052
dea31012005-04-17 16:05:31 -05004053 piocb = &piocbq->iocb;
4054
4055 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart34b02dc2008-08-24 21:49:55 -04004056 /* Clear out any old data in the FCP command area */
4057 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
4058 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05004059 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
James Smarte2a0a9d2008-12-04 22:40:02 -05004060 if (vport->phba->sli_rev == 3 &&
4061 !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04004062 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05004063 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
dea31012005-04-17 16:05:31 -05004064 piocb->ulpContext = ndlp->nlp_rpi;
James Smart6d368e52011-05-24 11:44:12 -04004065 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
4066 piocb->ulpContext =
4067 vport->phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
4068 }
James Smart53151bb2013-10-10 12:24:07 -04004069 piocb->ulpFCP2Rcvy = (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) ? 1 : 0;
dea31012005-04-17 16:05:31 -05004070 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
James Smartf9226c22014-02-20 09:57:28 -05004071 piocb->ulpPU = 0;
4072 piocb->un.fcpi.fcpi_parm = 0;
dea31012005-04-17 16:05:31 -05004073
4074 /* ulpTimeout is only one byte */
4075 if (lpfc_cmd->timeout > 0xff) {
4076 /*
4077 * Do not timeout the command at the firmware level.
4078 * The driver will provide the timeout mechanism.
4079 */
4080 piocb->ulpTimeout = 0;
James Smartf1126682009-06-10 17:22:44 -04004081 } else
dea31012005-04-17 16:05:31 -05004082 piocb->ulpTimeout = lpfc_cmd->timeout;
James Smartf1126682009-06-10 17:22:44 -04004083
4084 if (vport->phba->sli_rev == LPFC_SLI_REV4)
4085 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004086
James Smart2e0fef82007-06-17 19:56:36 -05004087 return 1;
dea31012005-04-17 16:05:31 -05004088}
4089
James Smart9bad7672008-12-04 22:39:02 -05004090/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004091 * lpfc_scsi_api_table_setup - Set up scsi api function jump table
James Smart3772a992009-05-22 14:50:54 -04004092 * @phba: The hba struct for which this call is being executed.
4093 * @dev_grp: The HBA PCI-Device group number.
4094 *
4095 * This routine sets up the SCSI interface API function jump table in @phba
4096 * struct.
4097 * Returns: 0 - success, -ENODEV - failure.
4098 **/
4099int
4100lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4101{
4102
James Smartf1126682009-06-10 17:22:44 -04004103 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
4104 phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
James Smartf1126682009-06-10 17:22:44 -04004105
James Smart3772a992009-05-22 14:50:54 -04004106 switch (dev_grp) {
4107 case LPFC_PCI_DEV_LP:
James Smart3772a992009-05-22 14:50:54 -04004108 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
James Smartacd68592012-01-18 16:25:09 -05004109 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04004110 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
James Smart19ca7602010-11-20 23:11:55 -05004111 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04004112 break;
James Smartda0436e2009-05-22 14:51:39 -04004113 case LPFC_PCI_DEV_OC:
James Smartda0436e2009-05-22 14:51:39 -04004114 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
James Smartacd68592012-01-18 16:25:09 -05004115 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04004116 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
James Smart19ca7602010-11-20 23:11:55 -05004117 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04004118 break;
James Smart3772a992009-05-22 14:50:54 -04004119 default:
4120 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4121 "1418 Invalid HBA PCI-device group: 0x%x\n",
4122 dev_grp);
4123 return -ENODEV;
4124 break;
4125 }
James Smart3772a992009-05-22 14:50:54 -04004126 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
James Smart84d1b002010-02-12 14:42:33 -05004127 phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
James Smart3772a992009-05-22 14:50:54 -04004128 return 0;
4129}
4130
4131/**
James Smart3621a712009-04-06 18:47:14 -04004132 * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
James Smart9bad7672008-12-04 22:39:02 -05004133 * @phba: The Hba for which this call is being executed.
4134 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
4135 * @rspiocbq: Pointer to lpfc_iocbq data structure.
4136 *
4137 * This routine is IOCB completion routine for device reset and target reset
4138 * routine. This routine release scsi buffer associated with lpfc_cmd.
4139 **/
James Smart7054a602007-04-25 09:52:34 -04004140static void
4141lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
4142 struct lpfc_iocbq *cmdiocbq,
4143 struct lpfc_iocbq *rspiocbq)
4144{
James Smartc4908502019-01-28 11:14:28 -08004145 struct lpfc_io_buf *lpfc_cmd =
4146 (struct lpfc_io_buf *) cmdiocbq->context1;
James Smart7054a602007-04-25 09:52:34 -04004147 if (lpfc_cmd)
4148 lpfc_release_scsi_buf(phba, lpfc_cmd);
4149 return;
4150}
4151
James Smart9bad7672008-12-04 22:39:02 -05004152/**
James Smart50212672018-12-13 15:17:57 -08004153 * lpfc_check_pci_resettable - Walks list of devices on pci_dev's bus to check
4154 * if issuing a pci_bus_reset is possibly unsafe
4155 * @phba: lpfc_hba pointer.
4156 *
4157 * Description:
4158 * Walks the bus_list to ensure only PCI devices with Emulex
4159 * vendor id, device ids that support hot reset, and only one occurrence
4160 * of function 0.
4161 *
4162 * Returns:
4163 * -EBADSLT, detected invalid device
4164 * 0, successful
4165 */
4166int
4167lpfc_check_pci_resettable(const struct lpfc_hba *phba)
4168{
4169 const struct pci_dev *pdev = phba->pcidev;
4170 struct pci_dev *ptr = NULL;
4171 u8 counter = 0;
4172
4173 /* Walk the list of devices on the pci_dev's bus */
4174 list_for_each_entry(ptr, &pdev->bus->devices, bus_list) {
4175 /* Check for Emulex Vendor ID */
4176 if (ptr->vendor != PCI_VENDOR_ID_EMULEX) {
4177 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4178 "8346 Non-Emulex vendor found: "
4179 "0x%04x\n", ptr->vendor);
4180 return -EBADSLT;
4181 }
4182
4183 /* Check for valid Emulex Device ID */
4184 switch (ptr->device) {
4185 case PCI_DEVICE_ID_LANCER_FC:
4186 case PCI_DEVICE_ID_LANCER_G6_FC:
4187 case PCI_DEVICE_ID_LANCER_G7_FC:
4188 break;
4189 default:
4190 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4191 "8347 Invalid device found: "
4192 "0x%04x\n", ptr->device);
4193 return -EBADSLT;
4194 }
4195
4196 /* Check for only one function 0 ID to ensure only one HBA on
4197 * secondary bus
4198 */
4199 if (ptr->devfn == 0) {
4200 if (++counter > 1) {
4201 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4202 "8348 More than one device on "
4203 "secondary bus found\n");
4204 return -EBADSLT;
4205 }
4206 }
4207 }
4208
4209 return 0;
4210}
4211
4212/**
James Smart3621a712009-04-06 18:47:14 -04004213 * lpfc_info - Info entry point of scsi_host_template data structure
James Smart9bad7672008-12-04 22:39:02 -05004214 * @host: The scsi host for which this call is being executed.
4215 *
4216 * This routine provides module information about hba.
4217 *
4218 * Reutrn code:
4219 * Pointer to char - Success.
4220 **/
dea31012005-04-17 16:05:31 -05004221const char *
4222lpfc_info(struct Scsi_Host *host)
4223{
James Smart2e0fef82007-06-17 19:56:36 -05004224 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
4225 struct lpfc_hba *phba = vport->phba;
James Smart50212672018-12-13 15:17:57 -08004226 int link_speed = 0;
4227 static char lpfcinfobuf[384];
4228 char tmp[384] = {0};
dea31012005-04-17 16:05:31 -05004229
James Smart50212672018-12-13 15:17:57 -08004230 memset(lpfcinfobuf, 0, sizeof(lpfcinfobuf));
dea31012005-04-17 16:05:31 -05004231 if (phba && phba->pcidev){
James Smart50212672018-12-13 15:17:57 -08004232 /* Model Description */
4233 scnprintf(tmp, sizeof(tmp), phba->ModelDesc);
4234 if (strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf)) >=
4235 sizeof(lpfcinfobuf))
4236 goto buffer_done;
4237
4238 /* PCI Info */
4239 scnprintf(tmp, sizeof(tmp),
4240 " on PCI bus %02x device %02x irq %d",
4241 phba->pcidev->bus->number, phba->pcidev->devfn,
4242 phba->pcidev->irq);
4243 if (strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf)) >=
4244 sizeof(lpfcinfobuf))
4245 goto buffer_done;
4246
4247 /* Port Number */
dea31012005-04-17 16:05:31 -05004248 if (phba->Port[0]) {
James Smart50212672018-12-13 15:17:57 -08004249 scnprintf(tmp, sizeof(tmp), " port %s", phba->Port);
4250 if (strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf)) >=
4251 sizeof(lpfcinfobuf))
4252 goto buffer_done;
dea31012005-04-17 16:05:31 -05004253 }
James Smart50212672018-12-13 15:17:57 -08004254
4255 /* Link Speed */
James Smarta085e872015-12-16 18:12:02 -05004256 link_speed = lpfc_sli_port_speed_get(phba);
James Smart50212672018-12-13 15:17:57 -08004257 if (link_speed != 0) {
4258 scnprintf(tmp, sizeof(tmp),
4259 " Logical Link Speed: %d Mbps", link_speed);
4260 if (strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf)) >=
4261 sizeof(lpfcinfobuf))
4262 goto buffer_done;
4263 }
4264
4265 /* PCI resettable */
4266 if (!lpfc_check_pci_resettable(phba)) {
4267 scnprintf(tmp, sizeof(tmp), " PCI resettable");
4268 strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf));
4269 }
dea31012005-04-17 16:05:31 -05004270 }
James Smart50212672018-12-13 15:17:57 -08004271
4272buffer_done:
dea31012005-04-17 16:05:31 -05004273 return lpfcinfobuf;
4274}
4275
James Smart9bad7672008-12-04 22:39:02 -05004276/**
James Smart3621a712009-04-06 18:47:14 -04004277 * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
James Smart9bad7672008-12-04 22:39:02 -05004278 * @phba: The Hba for which this call is being executed.
4279 *
4280 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
4281 * The default value of cfg_poll_tmo is 10 milliseconds.
4282 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004283static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
4284{
4285 unsigned long poll_tmo_expires =
4286 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
4287
James Smart895427b2017-02-12 13:52:30 -08004288 if (!list_empty(&phba->sli.sli3_ring[LPFC_FCP_RING].txcmplq))
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004289 mod_timer(&phba->fcp_poll_timer,
4290 poll_tmo_expires);
4291}
4292
James Smart9bad7672008-12-04 22:39:02 -05004293/**
James Smart3621a712009-04-06 18:47:14 -04004294 * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
James Smart9bad7672008-12-04 22:39:02 -05004295 * @phba: The Hba for which this call is being executed.
4296 *
4297 * This routine starts the fcp_poll_timer of @phba.
4298 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004299void lpfc_poll_start_timer(struct lpfc_hba * phba)
4300{
4301 lpfc_poll_rearm_timer(phba);
4302}
4303
James Smart9bad7672008-12-04 22:39:02 -05004304/**
James Smart3621a712009-04-06 18:47:14 -04004305 * lpfc_poll_timeout - Restart polling timer
James Smart9bad7672008-12-04 22:39:02 -05004306 * @ptr: Map to lpfc_hba data structure pointer.
4307 *
4308 * This routine restarts fcp_poll timer, when FCP ring polling is enable
4309 * and FCP Ring interrupt is disable.
4310 **/
4311
Kees Cookf22eb4d2017-09-06 20:24:26 -07004312void lpfc_poll_timeout(struct timer_list *t)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004313{
Kees Cookf22eb4d2017-09-06 20:24:26 -07004314 struct lpfc_hba *phba = from_timer(phba, t, fcp_poll_timer);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004315
4316 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004317 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -08004318 &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
James Smart45ed1192009-10-02 15:17:02 -04004319
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004320 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4321 lpfc_poll_rearm_timer(phba);
4322 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004323}
4324
James Smart9bad7672008-12-04 22:39:02 -05004325/**
James Smart3621a712009-04-06 18:47:14 -04004326 * lpfc_queuecommand - scsi_host_template queuecommand entry point
James Smart9bad7672008-12-04 22:39:02 -05004327 * @cmnd: Pointer to scsi_cmnd data structure.
4328 * @done: Pointer to done routine.
4329 *
4330 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
4331 * This routine prepares an IOCB from scsi command and provides to firmware.
4332 * The @done callback is invoked after driver finished processing the command.
4333 *
4334 * Return value :
4335 * 0 - Success
4336 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
4337 **/
dea31012005-04-17 16:05:31 -05004338static int
James Smartb9a7c632012-08-03 12:35:24 -04004339lpfc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004340{
James Smart2e0fef82007-06-17 19:56:36 -05004341 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4342 struct lpfc_hba *phba = vport->phba;
James Smart1ba981f2014-02-20 09:56:45 -05004343 struct lpfc_rport_data *rdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05004344 struct lpfc_nodelist *ndlp;
James Smartc4908502019-01-28 11:14:28 -08004345 struct lpfc_io_buf *lpfc_cmd;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004346 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
James Smart4c47efc2019-01-28 11:14:25 -08004347 int err, idx;
James Smart6a828b02019-01-28 11:14:31 -08004348#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4349 int cpu;
4350#endif
dea31012005-04-17 16:05:31 -05004351
James Smart1ba981f2014-02-20 09:56:45 -05004352 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smartb0e83012018-06-26 08:24:29 -07004353
4354 /* sanity check on references */
4355 if (unlikely(!rdata) || unlikely(!rport))
4356 goto out_fail_command;
4357
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004358 err = fc_remote_port_chkready(rport);
4359 if (err) {
4360 cmnd->result = err;
dea31012005-04-17 16:05:31 -05004361 goto out_fail_command;
4362 }
James Smart1c6f4ef52009-11-18 15:40:49 -05004363 ndlp = rdata->pnode;
dea31012005-04-17 16:05:31 -05004364
James Smartbf086112011-08-21 21:48:13 -04004365 if ((scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) &&
James Smartacd68592012-01-18 16:25:09 -05004366 (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED))) {
James Smarte2a0a9d2008-12-04 22:40:02 -05004367
James Smart6a9c52c2009-10-02 15:16:51 -04004368 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4369 "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
4370 " op:%02x str=%s without registering for"
4371 " BlockGuard - Rejecting command\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05004372 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
4373 dif_op_str[scsi_get_prot_op(cmnd)]);
4374 goto out_fail_command;
4375 }
4376
dea31012005-04-17 16:05:31 -05004377 /*
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004378 * Catch race where our node has transitioned, but the
4379 * transport is still transitioning.
dea31012005-04-17 16:05:31 -05004380 */
James Smart6b415f52012-06-12 13:54:59 -04004381 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
4382 goto out_tgt_busy;
James Smart2a5b7d62018-07-31 17:23:22 -07004383 if (lpfc_ndlp_check_qdepth(phba, ndlp)) {
4384 if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth) {
4385 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_ERROR,
4386 "3377 Target Queue Full, scsi Id:%d "
4387 "Qdepth:%d Pending command:%d"
4388 " WWNN:%02x:%02x:%02x:%02x:"
4389 "%02x:%02x:%02x:%02x, "
4390 " WWPN:%02x:%02x:%02x:%02x:"
4391 "%02x:%02x:%02x:%02x",
4392 ndlp->nlp_sid, ndlp->cmd_qdepth,
4393 atomic_read(&ndlp->cmd_pending),
4394 ndlp->nlp_nodename.u.wwn[0],
4395 ndlp->nlp_nodename.u.wwn[1],
4396 ndlp->nlp_nodename.u.wwn[2],
4397 ndlp->nlp_nodename.u.wwn[3],
4398 ndlp->nlp_nodename.u.wwn[4],
4399 ndlp->nlp_nodename.u.wwn[5],
4400 ndlp->nlp_nodename.u.wwn[6],
4401 ndlp->nlp_nodename.u.wwn[7],
4402 ndlp->nlp_portname.u.wwn[0],
4403 ndlp->nlp_portname.u.wwn[1],
4404 ndlp->nlp_portname.u.wwn[2],
4405 ndlp->nlp_portname.u.wwn[3],
4406 ndlp->nlp_portname.u.wwn[4],
4407 ndlp->nlp_portname.u.wwn[5],
4408 ndlp->nlp_portname.u.wwn[6],
4409 ndlp->nlp_portname.u.wwn[7]);
4410 goto out_tgt_busy;
4411 }
James Smart64bf0092018-01-30 15:58:53 -08004412 }
James Smartf91bc592018-04-09 14:24:22 -07004413
James Smartace44e42019-01-28 11:14:27 -08004414 lpfc_cmd = lpfc_get_scsi_buf(phba, ndlp, cmnd);
dea31012005-04-17 16:05:31 -05004415 if (lpfc_cmd == NULL) {
James Smarteaf15d52008-12-04 22:39:29 -05004416 lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05004417
James Smart895427b2017-02-12 13:52:30 -08004418 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_ERROR,
James Smarte8b62012007-08-02 11:10:09 -04004419 "0707 driver's buffer pool is empty, "
4420 "IO busied\n");
dea31012005-04-17 16:05:31 -05004421 goto out_host_busy;
4422 }
4423
4424 /*
4425 * Store the midlayer's command structure for the completion phase
4426 * and complete the command initialization.
4427 */
4428 lpfc_cmd->pCmd = cmnd;
4429 lpfc_cmd->rdata = rdata;
James Smart2a5b7d62018-07-31 17:23:22 -07004430 lpfc_cmd->ndlp = ndlp;
dea31012005-04-17 16:05:31 -05004431 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
dea31012005-04-17 16:05:31 -05004432
James Smarte2a0a9d2008-12-04 22:40:02 -05004433 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
James Smart6a9c52c2009-10-02 15:16:51 -04004434 if (vport->phba->cfg_enable_bg) {
James Smart737d4242013-04-17 20:14:49 -04004435 lpfc_printf_vlog(vport,
4436 KERN_INFO, LOG_SCSI_CMD,
James Smart26134702012-08-14 14:25:50 -04004437 "9033 BLKGRD: rcvd %s cmd:x%x "
4438 "sector x%llx cnt %u pt %x\n",
4439 dif_op_str[scsi_get_prot_op(cmnd)],
4440 cmnd->cmnd[0],
4441 (unsigned long long)scsi_get_lba(cmnd),
4442 blk_rq_sectors(cmnd->request),
4443 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004444 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004445 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
4446 } else {
James Smart6a9c52c2009-10-02 15:16:51 -04004447 if (vport->phba->cfg_enable_bg) {
James Smart737d4242013-04-17 20:14:49 -04004448 lpfc_printf_vlog(vport,
4449 KERN_INFO, LOG_SCSI_CMD,
James Smart26134702012-08-14 14:25:50 -04004450 "9038 BLKGRD: rcvd PROT_NORMAL cmd: "
4451 "x%x sector x%llx cnt %u pt %x\n",
4452 cmnd->cmnd[0],
4453 (unsigned long long)scsi_get_lba(cmnd),
James Smart9a6b09c2012-03-01 22:37:42 -05004454 blk_rq_sectors(cmnd->request),
James Smart26134702012-08-14 14:25:50 -04004455 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004456 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004457 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
4458 }
4459
dea31012005-04-17 16:05:31 -05004460 if (err)
4461 goto out_host_busy_free_buf;
4462
James Smart2e0fef82007-06-17 19:56:36 -05004463 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea31012005-04-17 16:05:31 -05004464
James Smart6a828b02019-01-28 11:14:31 -08004465#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4466 if (phba->cpucheck_on & LPFC_CHECK_SCSI_IO) {
4467 cpu = smp_processor_id();
4468 if (cpu < LPFC_CHECK_CPU_CNT) {
4469 struct lpfc_sli4_hdw_queue *hdwq =
4470 &phba->sli4_hba.hdwq[lpfc_cmd->hdwq_no];
4471 hdwq->cpucheck_xmt_io[cpu]++;
4472 }
4473 }
4474#endif
James Smart3772a992009-05-22 14:50:54 -04004475 err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
James Smart92d7f7b2007-06-17 19:56:38 -05004476 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
James Smarteaf15d52008-12-04 22:39:29 -05004477 if (err) {
James Smart76f96b62013-12-17 20:29:01 -05004478 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4479 "3376 FCP could not issue IOCB err %x"
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004480 "FCP cmd x%x <%d/%llu> "
James Smart76f96b62013-12-17 20:29:01 -05004481 "sid: x%x did: x%x oxid: x%x "
4482 "Data: x%x x%x x%x x%x\n",
4483 err, cmnd->cmnd[0],
4484 cmnd->device ? cmnd->device->id : 0xffff,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004485 cmnd->device ? cmnd->device->lun : (u64) -1,
James Smart76f96b62013-12-17 20:29:01 -05004486 vport->fc_myDID, ndlp->nlp_DID,
4487 phba->sli_rev == LPFC_SLI_REV4 ?
4488 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
4489 lpfc_cmd->cur_iocbq.iocb.ulpContext,
4490 lpfc_cmd->cur_iocbq.iocb.ulpIoTag,
4491 lpfc_cmd->cur_iocbq.iocb.ulpTimeout,
4492 (uint32_t)
4493 (cmnd->request->timeout / 1000));
4494
dea31012005-04-17 16:05:31 -05004495 goto out_host_busy_free_buf;
James Smarteaf15d52008-12-04 22:39:29 -05004496 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004497 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004498 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -08004499 &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
James Smart45ed1192009-10-02 15:17:02 -04004500
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004501 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4502 lpfc_poll_rearm_timer(phba);
4503 }
4504
James Smartc4908502019-01-28 11:14:28 -08004505 if (phba->cfg_xri_rebalancing)
4506 lpfc_keep_pvt_pool_above_lowwm(phba, lpfc_cmd->hdwq_no);
4507
dea31012005-04-17 16:05:31 -05004508 return 0;
4509
4510 out_host_busy_free_buf:
James Smart1fbf9742019-01-28 11:14:26 -08004511 idx = lpfc_cmd->hdwq_no;
James Smartbcf4dbf2006-07-06 15:50:08 -04004512 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James Smart4c47efc2019-01-28 11:14:25 -08004513 if (phba->sli4_hba.hdwq) {
4514 switch (lpfc_cmd->fcp_cmnd->fcpCntl3) {
4515 case WRITE_DATA:
4516 phba->sli4_hba.hdwq[idx].scsi_cstat.output_requests--;
4517 break;
4518 case READ_DATA:
4519 phba->sli4_hba.hdwq[idx].scsi_cstat.input_requests--;
4520 break;
4521 default:
4522 phba->sli4_hba.hdwq[idx].scsi_cstat.control_requests--;
4523 }
4524 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004525 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004526 out_host_busy:
4527 return SCSI_MLQUEUE_HOST_BUSY;
4528
Mike Christie34963432011-02-25 14:04:28 -06004529 out_tgt_busy:
4530 return SCSI_MLQUEUE_TARGET_BUSY;
4531
dea31012005-04-17 16:05:31 -05004532 out_fail_command:
James Smartb9a7c632012-08-03 12:35:24 -04004533 cmnd->scsi_done(cmnd);
dea31012005-04-17 16:05:31 -05004534 return 0;
4535}
4536
Jeff Garzikf2812332010-11-16 02:10:29 -05004537
James Smart9bad7672008-12-04 22:39:02 -05004538/**
James Smart3621a712009-04-06 18:47:14 -04004539 * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05004540 * @cmnd: Pointer to scsi_cmnd data structure.
4541 *
4542 * This routine aborts @cmnd pending in base driver.
4543 *
4544 * Return code :
4545 * 0x2003 - Error
4546 * 0x2002 - Success
4547 **/
dea31012005-04-17 16:05:31 -05004548static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05004549lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004550{
James Smart2e0fef82007-06-17 19:56:36 -05004551 struct Scsi_Host *shost = cmnd->device->host;
4552 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4553 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004554 struct lpfc_iocbq *iocb;
4555 struct lpfc_iocbq *abtsiocb;
James Smartc4908502019-01-28 11:14:28 -08004556 struct lpfc_io_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05004557 IOCB_t *cmd, *icmd;
James Smart3a707302012-06-12 13:54:42 -04004558 int ret = SUCCESS, status = 0;
James Smart8931c732018-07-31 17:23:20 -07004559 struct lpfc_sli_ring *pring_s4 = NULL;
James Smart895427b2017-02-12 13:52:30 -08004560 int ret_val;
James Smart59c68ea2018-04-09 14:24:25 -07004561 unsigned long flags;
James Smartfa61a542008-01-11 01:52:42 -05004562 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004563
James Smart3a707302012-06-12 13:54:42 -04004564 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04004565 if (status != 0 && status != SUCCESS)
James Smart3a707302012-06-12 13:54:42 -04004566 return status;
James Smart4f2e66c2012-05-09 21:17:07 -04004567
James Smartc2017262019-01-28 11:14:37 -08004568 lpfc_cmd = (struct lpfc_io_buf *)cmnd->host_scribble;
4569 if (!lpfc_cmd)
4570 return ret;
4571
James Smart876dd7d2012-09-29 11:31:28 -04004572 spin_lock_irqsave(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004573 /* driver queued commands are in process of being flushed */
4574 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
James Smart4f2e66c2012-05-09 21:17:07 -04004575 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4576 "3168 SCSI Layer abort requested I/O has been "
4577 "flushed by LLD.\n");
James Smartc2017262019-01-28 11:14:37 -08004578 ret = FAILED;
4579 goto out_unlock;
James Smart4f2e66c2012-05-09 21:17:07 -04004580 }
4581
James Smartc2017262019-01-28 11:14:37 -08004582 /* Guard against IO completion being called at same time */
4583 spin_lock(&lpfc_cmd->buf_lock);
4584
4585 if (!lpfc_cmd->pCmd) {
James Smarteee88772010-09-29 11:19:08 -04004586 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4587 "2873 SCSI Layer I/O Abort Request IO CMPL Status "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004588 "x%x ID %d LUN %llu\n",
James Smart3a707302012-06-12 13:54:42 -04004589 SUCCESS, cmnd->device->id, cmnd->device->lun);
James Smartc2017262019-01-28 11:14:37 -08004590 goto out_unlock_buf;
James Smarteee88772010-09-29 11:19:08 -04004591 }
dea31012005-04-17 16:05:31 -05004592
James Smart4f2e66c2012-05-09 21:17:07 -04004593 iocb = &lpfc_cmd->cur_iocbq;
James Smart8931c732018-07-31 17:23:20 -07004594 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartcdb42be2019-01-28 11:14:21 -08004595 pring_s4 = phba->sli4_hba.hdwq[iocb->hba_wqidx].fcp_wq->pring;
James Smart8931c732018-07-31 17:23:20 -07004596 if (!pring_s4) {
4597 ret = FAILED;
James Smartc2017262019-01-28 11:14:37 -08004598 goto out_unlock_buf;
James Smart8931c732018-07-31 17:23:20 -07004599 }
4600 spin_lock(&pring_s4->ring_lock);
4601 }
James Smart4f2e66c2012-05-09 21:17:07 -04004602 /* the command is in process of being cancelled */
4603 if (!(iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
James Smart4f2e66c2012-05-09 21:17:07 -04004604 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4605 "3169 SCSI Layer abort requested I/O has been "
4606 "cancelled by LLD.\n");
James Smartc2017262019-01-28 11:14:37 -08004607 ret = FAILED;
4608 goto out_unlock_ring;
James Smart4f2e66c2012-05-09 21:17:07 -04004609 }
dea31012005-04-17 16:05:31 -05004610 /*
James Smartc4908502019-01-28 11:14:28 -08004611 * If pCmd field of the corresponding lpfc_io_buf structure
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004612 * points to a different SCSI command, then the driver has
4613 * already completed this command, but the midlayer did not
James Smart4f2e66c2012-05-09 21:17:07 -04004614 * see the completion before the eh fired. Just return SUCCESS.
dea31012005-04-17 16:05:31 -05004615 */
James Smart4f2e66c2012-05-09 21:17:07 -04004616 if (lpfc_cmd->pCmd != cmnd) {
4617 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4618 "3170 SCSI Layer abort requested I/O has been "
4619 "completed by LLD.\n");
James Smartc2017262019-01-28 11:14:37 -08004620 goto out_unlock_ring;
James Smart4f2e66c2012-05-09 21:17:07 -04004621 }
dea31012005-04-17 16:05:31 -05004622
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004623 BUG_ON(iocb->context1 != lpfc_cmd);
dea31012005-04-17 16:05:31 -05004624
James Smartee620212014-04-04 13:51:53 -04004625 /* abort issued in recovery is still in progress */
4626 if (iocb->iocb_flag & LPFC_DRIVER_ABORTED) {
4627 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4628 "3389 SCSI Layer I/O Abort Request is pending\n");
James Smart8931c732018-07-31 17:23:20 -07004629 if (phba->sli_rev == LPFC_SLI_REV4)
4630 spin_unlock(&pring_s4->ring_lock);
James Smartc2017262019-01-28 11:14:37 -08004631 spin_unlock(&lpfc_cmd->buf_lock);
James Smartee620212014-04-04 13:51:53 -04004632 spin_unlock_irqrestore(&phba->hbalock, flags);
4633 goto wait_for_cmpl;
4634 }
4635
James Smart4f2e66c2012-05-09 21:17:07 -04004636 abtsiocb = __lpfc_sli_get_iocbq(phba);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004637 if (abtsiocb == NULL) {
4638 ret = FAILED;
James Smartc2017262019-01-28 11:14:37 -08004639 goto out_unlock_ring;
dea31012005-04-17 16:05:31 -05004640 }
4641
James Smartafbd8d82013-09-06 12:22:13 -04004642 /* Indicate the IO is being aborted by the driver. */
4643 iocb->iocb_flag |= LPFC_DRIVER_ABORTED;
4644
dea31012005-04-17 16:05:31 -05004645 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004646 * The scsi command can not be in txq and it is in flight because the
4647 * pCmd is still pointig at the SCSI command we have to abort. There
4648 * is no need to search the txcmplq. Just send an abort to the FW.
dea31012005-04-17 16:05:31 -05004649 */
dea31012005-04-17 16:05:31 -05004650
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004651 cmd = &iocb->iocb;
4652 icmd = &abtsiocb->iocb;
4653 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
4654 icmd->un.acxri.abortContextTag = cmd->ulpContext;
James Smart3772a992009-05-22 14:50:54 -04004655 if (phba->sli_rev == LPFC_SLI_REV4)
4656 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
4657 else
4658 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05004659
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004660 icmd->ulpLe = 1;
4661 icmd->ulpClass = cmd->ulpClass;
James Smart5ffc2662009-11-18 15:39:44 -05004662
4663 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -08004664 abtsiocb->hba_wqidx = iocb->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -05004665 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -04004666 if (iocb->iocb_flag & LPFC_IO_FOF)
4667 abtsiocb->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -05004668
James Smart2e0fef82007-06-17 19:56:36 -05004669 if (lpfc_is_link_up(phba))
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004670 icmd->ulpCommand = CMD_ABORT_XRI_CN;
4671 else
4672 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea31012005-04-17 16:05:31 -05004673
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004674 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smart2e0fef82007-06-17 19:56:36 -05004675 abtsiocb->vport = vport;
James Smart8931c732018-07-31 17:23:20 -07004676 lpfc_cmd->waitq = &waitq;
James Smart98912dda2014-04-04 13:52:31 -04004677 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart98912dda2014-04-04 13:52:31 -04004678 /* Note: both hbalock and ring_lock must be set here */
James Smart98912dda2014-04-04 13:52:31 -04004679 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
4680 abtsiocb, 0);
James Smart59c68ea2018-04-09 14:24:25 -07004681 spin_unlock(&pring_s4->ring_lock);
James Smart98912dda2014-04-04 13:52:31 -04004682 } else {
4683 ret_val = __lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
4684 abtsiocb, 0);
4685 }
James Smart4f2e66c2012-05-09 21:17:07 -04004686 /* no longer need the lock after this point */
James Smart876dd7d2012-09-29 11:31:28 -04004687 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004688
James Smart98912dda2014-04-04 13:52:31 -04004689 if (ret_val == IOCB_ERROR) {
James Smart8931c732018-07-31 17:23:20 -07004690 /* Indicate the IO is not being aborted by the driver. */
4691 iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
4692 lpfc_cmd->waitq = NULL;
James Smartc2017262019-01-28 11:14:37 -08004693 spin_unlock(&lpfc_cmd->buf_lock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004694 lpfc_sli_release_iocbq(phba, abtsiocb);
4695 ret = FAILED;
4696 goto out;
4697 }
4698
James Smartc2017262019-01-28 11:14:37 -08004699 spin_unlock(&lpfc_cmd->buf_lock);
4700
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004701 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
James Smart45ed1192009-10-02 15:17:02 -04004702 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -08004703 &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004704
James Smartee620212014-04-04 13:51:53 -04004705wait_for_cmpl:
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004706 /* Wait for abort to complete */
James Smartfa61a542008-01-11 01:52:42 -05004707 wait_event_timeout(waitq,
4708 (lpfc_cmd->pCmd != cmnd),
James Smart256ec0d2013-04-17 20:14:58 -04004709 msecs_to_jiffies(2*vport->cfg_devloss_tmo*1000));
James Smartee620212014-04-04 13:51:53 -04004710
James Smartc2017262019-01-28 11:14:37 -08004711 spin_lock(&lpfc_cmd->buf_lock);
dea31012005-04-17 16:05:31 -05004712
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004713 if (lpfc_cmd->pCmd == cmnd) {
4714 ret = FAILED;
James Smarte8b62012007-08-02 11:10:09 -04004715 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4716 "0748 abort handler timed out waiting "
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -07004717 "for aborting I/O (xri:x%x) to complete: "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004718 "ret %#x, ID %d, LUN %llu\n",
James Smart247ca942012-08-14 14:26:13 -04004719 iocb->sli4_xritag, ret,
4720 cmnd->device->id, cmnd->device->lun);
dea31012005-04-17 16:05:31 -05004721 }
James Smartc2017262019-01-28 11:14:37 -08004722 spin_unlock(&lpfc_cmd->buf_lock);
James Smart4f2e66c2012-05-09 21:17:07 -04004723 goto out;
dea31012005-04-17 16:05:31 -05004724
James Smartc2017262019-01-28 11:14:37 -08004725out_unlock_ring:
4726 if (phba->sli_rev == LPFC_SLI_REV4)
4727 spin_unlock(&pring_s4->ring_lock);
4728out_unlock_buf:
4729 spin_unlock(&lpfc_cmd->buf_lock);
James Smart4f2e66c2012-05-09 21:17:07 -04004730out_unlock:
James Smart876dd7d2012-09-29 11:31:28 -04004731 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004732out:
James Smarte8b62012007-08-02 11:10:09 -04004733 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4734 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004735 "LUN %llu\n", ret, cmnd->device->id,
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04004736 cmnd->device->lun);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004737 return ret;
dea31012005-04-17 16:05:31 -05004738}
4739
James Smartbbb9d182009-06-10 17:23:16 -04004740static char *
4741lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
4742{
4743 switch (task_mgmt_cmd) {
4744 case FCP_ABORT_TASK_SET:
4745 return "ABORT_TASK_SET";
4746 case FCP_CLEAR_TASK_SET:
4747 return "FCP_CLEAR_TASK_SET";
4748 case FCP_BUS_RESET:
4749 return "FCP_BUS_RESET";
4750 case FCP_LUN_RESET:
4751 return "FCP_LUN_RESET";
4752 case FCP_TARGET_RESET:
4753 return "FCP_TARGET_RESET";
4754 case FCP_CLEAR_ACA:
4755 return "FCP_CLEAR_ACA";
4756 case FCP_TERMINATE_TASK:
4757 return "FCP_TERMINATE_TASK";
4758 default:
4759 return "unknown";
4760 }
4761}
4762
James Smart53151bb2013-10-10 12:24:07 -04004763
4764/**
4765 * lpfc_check_fcp_rsp - check the returned fcp_rsp to see if task failed
4766 * @vport: The virtual port for which this call is being executed.
James Smartc4908502019-01-28 11:14:28 -08004767 * @lpfc_cmd: Pointer to lpfc_io_buf data structure.
James Smart53151bb2013-10-10 12:24:07 -04004768 *
4769 * This routine checks the FCP RSP INFO to see if the tsk mgmt command succeded
4770 *
4771 * Return code :
4772 * 0x2003 - Error
4773 * 0x2002 - Success
4774 **/
4775static int
James Smartc4908502019-01-28 11:14:28 -08004776lpfc_check_fcp_rsp(struct lpfc_vport *vport, struct lpfc_io_buf *lpfc_cmd)
James Smart53151bb2013-10-10 12:24:07 -04004777{
4778 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
4779 uint32_t rsp_info;
4780 uint32_t rsp_len;
4781 uint8_t rsp_info_code;
4782 int ret = FAILED;
4783
4784
4785 if (fcprsp == NULL)
4786 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4787 "0703 fcp_rsp is missing\n");
4788 else {
4789 rsp_info = fcprsp->rspStatus2;
4790 rsp_len = be32_to_cpu(fcprsp->rspRspLen);
4791 rsp_info_code = fcprsp->rspInfo3;
4792
4793
4794 lpfc_printf_vlog(vport, KERN_INFO,
4795 LOG_FCP,
4796 "0706 fcp_rsp valid 0x%x,"
4797 " rsp len=%d code 0x%x\n",
4798 rsp_info,
4799 rsp_len, rsp_info_code);
4800
4801 if ((fcprsp->rspStatus2&RSP_LEN_VALID) && (rsp_len == 8)) {
4802 switch (rsp_info_code) {
4803 case RSP_NO_FAILURE:
4804 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4805 "0715 Task Mgmt No Failure\n");
4806 ret = SUCCESS;
4807 break;
4808 case RSP_TM_NOT_SUPPORTED: /* TM rejected */
4809 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4810 "0716 Task Mgmt Target "
4811 "reject\n");
4812 break;
4813 case RSP_TM_NOT_COMPLETED: /* TM failed */
4814 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4815 "0717 Task Mgmt Target "
4816 "failed TM\n");
4817 break;
4818 case RSP_TM_INVALID_LU: /* TM to invalid LU! */
4819 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4820 "0718 Task Mgmt to invalid "
4821 "LUN\n");
4822 break;
4823 }
4824 }
4825 }
4826 return ret;
4827}
4828
4829
James Smartbbb9d182009-06-10 17:23:16 -04004830/**
4831 * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
4832 * @vport: The virtual port for which this call is being executed.
4833 * @rdata: Pointer to remote port local data
4834 * @tgt_id: Target ID of remote device.
4835 * @lun_id: Lun number for the TMF
4836 * @task_mgmt_cmd: type of TMF to send
4837 *
4838 * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
4839 * a remote port.
4840 *
4841 * Return Code:
4842 * 0x2003 - Error
4843 * 0x2002 - Success.
4844 **/
4845static int
James Smarteed695d2016-10-13 15:06:04 -07004846lpfc_send_taskmgmt(struct lpfc_vport *vport, struct scsi_cmnd *cmnd,
4847 unsigned int tgt_id, uint64_t lun_id,
4848 uint8_t task_mgmt_cmd)
James Smartbbb9d182009-06-10 17:23:16 -04004849{
4850 struct lpfc_hba *phba = vport->phba;
James Smartc4908502019-01-28 11:14:28 -08004851 struct lpfc_io_buf *lpfc_cmd;
James Smartbbb9d182009-06-10 17:23:16 -04004852 struct lpfc_iocbq *iocbq;
4853 struct lpfc_iocbq *iocbqrsp;
James Smarteed695d2016-10-13 15:06:04 -07004854 struct lpfc_rport_data *rdata;
4855 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004856 int ret;
4857 int status;
4858
James Smarteed695d2016-10-13 15:06:04 -07004859 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
4860 if (!rdata || !rdata->pnode || !NLP_CHK_NODE_ACT(rdata->pnode))
James Smartbbb9d182009-06-10 17:23:16 -04004861 return FAILED;
James Smarteed695d2016-10-13 15:06:04 -07004862 pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004863
James Smartace44e42019-01-28 11:14:27 -08004864 lpfc_cmd = lpfc_get_scsi_buf(phba, pnode, NULL);
James Smartbbb9d182009-06-10 17:23:16 -04004865 if (lpfc_cmd == NULL)
4866 return FAILED;
James Smart0c411222013-09-06 12:22:46 -04004867 lpfc_cmd->timeout = phba->cfg_task_mgmt_tmo;
James Smartbbb9d182009-06-10 17:23:16 -04004868 lpfc_cmd->rdata = rdata;
James Smarteed695d2016-10-13 15:06:04 -07004869 lpfc_cmd->pCmd = cmnd;
James Smart2a5b7d62018-07-31 17:23:22 -07004870 lpfc_cmd->ndlp = pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004871
4872 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
4873 task_mgmt_cmd);
4874 if (!status) {
4875 lpfc_release_scsi_buf(phba, lpfc_cmd);
4876 return FAILED;
4877 }
4878
4879 iocbq = &lpfc_cmd->cur_iocbq;
4880 iocbqrsp = lpfc_sli_get_iocbq(phba);
4881 if (iocbqrsp == NULL) {
4882 lpfc_release_scsi_buf(phba, lpfc_cmd);
4883 return FAILED;
4884 }
James Smart5a0916b2013-07-15 18:31:42 -04004885 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
James Smartbbb9d182009-06-10 17:23:16 -04004886
4887 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004888 "0702 Issue %s to TGT %d LUN %llu "
James Smart6d368e52011-05-24 11:44:12 -04004889 "rpi x%x nlp_flag x%x Data: x%x x%x\n",
James Smartbbb9d182009-06-10 17:23:16 -04004890 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
James Smart6d368e52011-05-24 11:44:12 -04004891 pnode->nlp_rpi, pnode->nlp_flag, iocbq->sli4_xritag,
4892 iocbq->iocb_flag);
James Smartbbb9d182009-06-10 17:23:16 -04004893
4894 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
4895 iocbq, iocbqrsp, lpfc_cmd->timeout);
James Smart53151bb2013-10-10 12:24:07 -04004896 if ((status != IOCB_SUCCESS) ||
4897 (iocbqrsp->iocb.ulpStatus != IOSTAT_SUCCESS)) {
James Smartae374a32015-05-22 10:42:41 -04004898 if (status != IOCB_SUCCESS ||
4899 iocbqrsp->iocb.ulpStatus != IOSTAT_FCP_RSP_ERROR)
4900 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4901 "0727 TMF %s to TGT %d LUN %llu "
4902 "failed (%d, %d) iocb_flag x%x\n",
4903 lpfc_taskmgmt_name(task_mgmt_cmd),
4904 tgt_id, lun_id,
4905 iocbqrsp->iocb.ulpStatus,
4906 iocbqrsp->iocb.un.ulpWord[4],
4907 iocbq->iocb_flag);
James Smart53151bb2013-10-10 12:24:07 -04004908 /* if ulpStatus != IOCB_SUCCESS, then status == IOCB_SUCCESS */
4909 if (status == IOCB_SUCCESS) {
4910 if (iocbqrsp->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
4911 /* Something in the FCP_RSP was invalid.
4912 * Check conditions */
4913 ret = lpfc_check_fcp_rsp(vport, lpfc_cmd);
4914 else
4915 ret = FAILED;
4916 } else if (status == IOCB_TIMEDOUT) {
4917 ret = TIMEOUT_ERROR;
4918 } else {
4919 ret = FAILED;
4920 }
James Smart53151bb2013-10-10 12:24:07 -04004921 } else
James Smartbbb9d182009-06-10 17:23:16 -04004922 ret = SUCCESS;
4923
4924 lpfc_sli_release_iocbq(phba, iocbqrsp);
4925
4926 if (ret != TIMEOUT_ERROR)
4927 lpfc_release_scsi_buf(phba, lpfc_cmd);
4928
4929 return ret;
4930}
4931
4932/**
4933 * lpfc_chk_tgt_mapped -
4934 * @vport: The virtual port to check on
4935 * @cmnd: Pointer to scsi_cmnd data structure.
4936 *
4937 * This routine delays until the scsi target (aka rport) for the
4938 * command exists (is present and logged in) or we declare it non-existent.
4939 *
4940 * Return code :
4941 * 0x2003 - Error
4942 * 0x2002 - Success
4943 **/
4944static int
4945lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
4946{
James Smart1ba981f2014-02-20 09:56:45 -05004947 struct lpfc_rport_data *rdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05004948 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004949 unsigned long later;
4950
James Smart1ba981f2014-02-20 09:56:45 -05004951 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smart1c6f4ef52009-11-18 15:40:49 -05004952 if (!rdata) {
4953 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4954 "0797 Tgt Map rport failure: rdata x%p\n", rdata);
4955 return FAILED;
4956 }
4957 pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004958 /*
4959 * If target is not in a MAPPED state, delay until
4960 * target is rediscovered or devloss timeout expires.
4961 */
4962 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
4963 while (time_after(later, jiffies)) {
4964 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
4965 return FAILED;
4966 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
4967 return SUCCESS;
4968 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
James Smart1ba981f2014-02-20 09:56:45 -05004969 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smartbbb9d182009-06-10 17:23:16 -04004970 if (!rdata)
4971 return FAILED;
4972 pnode = rdata->pnode;
4973 }
4974 if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
4975 (pnode->nlp_state != NLP_STE_MAPPED_NODE))
4976 return FAILED;
4977 return SUCCESS;
4978}
4979
4980/**
4981 * lpfc_reset_flush_io_context -
4982 * @vport: The virtual port (scsi_host) for the flush context
4983 * @tgt_id: If aborting by Target contect - specifies the target id
4984 * @lun_id: If aborting by Lun context - specifies the lun id
4985 * @context: specifies the context level to flush at.
4986 *
4987 * After a reset condition via TMF, we need to flush orphaned i/o
4988 * contexts from the adapter. This routine aborts any contexts
4989 * outstanding, then waits for their completions. The wait is
4990 * bounded by devloss_tmo though.
4991 *
4992 * Return code :
4993 * 0x2003 - Error
4994 * 0x2002 - Success
4995 **/
4996static int
4997lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
4998 uint64_t lun_id, lpfc_ctx_cmd context)
4999{
5000 struct lpfc_hba *phba = vport->phba;
5001 unsigned long later;
5002 int cnt;
5003
5004 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
5005 if (cnt)
James Smart98912dda2014-04-04 13:52:31 -04005006 lpfc_sli_abort_taskmgmt(vport,
James Smart895427b2017-02-12 13:52:30 -08005007 &phba->sli.sli3_ring[LPFC_FCP_RING],
James Smart98912dda2014-04-04 13:52:31 -04005008 tgt_id, lun_id, context);
James Smartbbb9d182009-06-10 17:23:16 -04005009 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
5010 while (time_after(later, jiffies) && cnt) {
5011 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
5012 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
5013 }
5014 if (cnt) {
5015 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5016 "0724 I/O flush failure for context %s : cnt x%x\n",
5017 ((context == LPFC_CTX_LUN) ? "LUN" :
5018 ((context == LPFC_CTX_TGT) ? "TGT" :
5019 ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
5020 cnt);
5021 return FAILED;
5022 }
5023 return SUCCESS;
5024}
5025
James Smart9bad7672008-12-04 22:39:02 -05005026/**
James Smart3621a712009-04-06 18:47:14 -04005027 * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
James Smart9bad7672008-12-04 22:39:02 -05005028 * @cmnd: Pointer to scsi_cmnd data structure.
5029 *
James Smartbbb9d182009-06-10 17:23:16 -04005030 * This routine does a device reset by sending a LUN_RESET task management
James Smart9bad7672008-12-04 22:39:02 -05005031 * command.
5032 *
5033 * Return code :
5034 * 0x2003 - Error
James Smart3621a712009-04-06 18:47:14 -04005035 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05005036 **/
dea31012005-04-17 16:05:31 -05005037static int
James Smart7054a602007-04-25 09:52:34 -04005038lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05005039{
James Smart2e0fef82007-06-17 19:56:36 -05005040 struct Scsi_Host *shost = cmnd->device->host;
5041 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smart1ba981f2014-02-20 09:56:45 -05005042 struct lpfc_rport_data *rdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005043 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005044 unsigned tgt_id = cmnd->device->id;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005045 uint64_t lun_id = cmnd->device->lun;
James Smartea2151b2008-09-07 11:52:10 -04005046 struct lpfc_scsi_event_header scsi_event;
James Smart53151bb2013-10-10 12:24:07 -04005047 int status;
dea31012005-04-17 16:05:31 -05005048
James Smart1ba981f2014-02-20 09:56:45 -05005049 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smartad490b62015-04-07 15:07:26 -04005050 if (!rdata || !rdata->pnode) {
James Smart1c6f4ef52009-11-18 15:40:49 -05005051 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smartbbd3d732019-03-12 16:30:17 -07005052 "0798 Device Reset rdata failure: rdata x%p\n",
James Smartad490b62015-04-07 15:07:26 -04005053 rdata);
James Smart1c6f4ef52009-11-18 15:40:49 -05005054 return FAILED;
5055 }
5056 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04005057 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005058 if (status != 0 && status != SUCCESS)
James Smart589a52d2010-07-14 15:30:54 -04005059 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005060
5061 status = lpfc_chk_tgt_mapped(vport, cmnd);
5062 if (status == FAILED) {
5063 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5064 "0721 Device Reset rport failure: rdata x%p\n", rdata);
5065 return FAILED;
5066 }
5067
5068 scsi_event.event_type = FC_REG_SCSI_EVENT;
5069 scsi_event.subcategory = LPFC_EVENT_LUNRESET;
5070 scsi_event.lun = lun_id;
5071 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5072 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5073
5074 fc_host_post_vendor_event(shost, fc_get_event_number(),
5075 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
5076
James Smarteed695d2016-10-13 15:06:04 -07005077 status = lpfc_send_taskmgmt(vport, cmnd, tgt_id, lun_id,
James Smartbbb9d182009-06-10 17:23:16 -04005078 FCP_LUN_RESET);
5079
5080 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005081 "0713 SCSI layer issued Device Reset (%d, %llu) "
James Smartbbb9d182009-06-10 17:23:16 -04005082 "return x%x\n", tgt_id, lun_id, status);
5083
dea31012005-04-17 16:05:31 -05005084 /*
James Smartbbb9d182009-06-10 17:23:16 -04005085 * We have to clean up i/o as : they may be orphaned by the TMF;
5086 * or if the TMF failed, they may be in an indeterminate state.
5087 * So, continue on.
5088 * We will report success if all the i/o aborts successfully.
dea31012005-04-17 16:05:31 -05005089 */
James Smart53151bb2013-10-10 12:24:07 -04005090 if (status == SUCCESS)
5091 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
James Smartbbb9d182009-06-10 17:23:16 -04005092 LPFC_CTX_LUN);
James Smart53151bb2013-10-10 12:24:07 -04005093
5094 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005095}
5096
5097/**
5098 * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
5099 * @cmnd: Pointer to scsi_cmnd data structure.
5100 *
5101 * This routine does a target reset by sending a TARGET_RESET task management
5102 * command.
5103 *
5104 * Return code :
5105 * 0x2003 - Error
5106 * 0x2002 - Success
5107 **/
5108static int
5109lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
5110{
5111 struct Scsi_Host *shost = cmnd->device->host;
5112 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smart1ba981f2014-02-20 09:56:45 -05005113 struct lpfc_rport_data *rdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005114 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005115 unsigned tgt_id = cmnd->device->id;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005116 uint64_t lun_id = cmnd->device->lun;
James Smartbbb9d182009-06-10 17:23:16 -04005117 struct lpfc_scsi_event_header scsi_event;
James Smart53151bb2013-10-10 12:24:07 -04005118 int status;
James Smartbbb9d182009-06-10 17:23:16 -04005119
James Smart1ba981f2014-02-20 09:56:45 -05005120 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smartbbd3d732019-03-12 16:30:17 -07005121 if (!rdata || !rdata->pnode) {
James Smart1c6f4ef52009-11-18 15:40:49 -05005122 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smartbbd3d732019-03-12 16:30:17 -07005123 "0799 Target Reset rdata failure: rdata x%p\n",
5124 rdata);
James Smart1c6f4ef52009-11-18 15:40:49 -05005125 return FAILED;
5126 }
5127 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04005128 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005129 if (status != 0 && status != SUCCESS)
James Smart589a52d2010-07-14 15:30:54 -04005130 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005131
5132 status = lpfc_chk_tgt_mapped(vport, cmnd);
5133 if (status == FAILED) {
5134 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5135 "0722 Target Reset rport failure: rdata x%p\n", rdata);
James Smart63e480f2015-04-07 15:07:20 -04005136 if (pnode) {
5137 spin_lock_irq(shost->host_lock);
5138 pnode->nlp_flag &= ~NLP_NPR_ADISC;
5139 pnode->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
5140 spin_unlock_irq(shost->host_lock);
5141 }
James Smart8c50d252014-09-03 12:58:16 -04005142 lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
5143 LPFC_CTX_TGT);
5144 return FAST_IO_FAIL;
dea31012005-04-17 16:05:31 -05005145 }
James Smartea2151b2008-09-07 11:52:10 -04005146
5147 scsi_event.event_type = FC_REG_SCSI_EVENT;
5148 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
5149 scsi_event.lun = 0;
5150 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5151 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5152
James Smartbbb9d182009-06-10 17:23:16 -04005153 fc_host_post_vendor_event(shost, fc_get_event_number(),
5154 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -04005155
James Smarteed695d2016-10-13 15:06:04 -07005156 status = lpfc_send_taskmgmt(vport, cmnd, tgt_id, lun_id,
James Smartbbb9d182009-06-10 17:23:16 -04005157 FCP_TARGET_RESET);
dea31012005-04-17 16:05:31 -05005158
James Smarte8b62012007-08-02 11:10:09 -04005159 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005160 "0723 SCSI layer issued Target Reset (%d, %llu) "
James Smartbbb9d182009-06-10 17:23:16 -04005161 "return x%x\n", tgt_id, lun_id, status);
5162
5163 /*
5164 * We have to clean up i/o as : they may be orphaned by the TMF;
5165 * or if the TMF failed, they may be in an indeterminate state.
5166 * So, continue on.
5167 * We will report success if all the i/o aborts successfully.
5168 */
James Smart53151bb2013-10-10 12:24:07 -04005169 if (status == SUCCESS)
5170 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
James Smart3a707302012-06-12 13:54:42 -04005171 LPFC_CTX_TGT);
James Smart53151bb2013-10-10 12:24:07 -04005172 return status;
dea31012005-04-17 16:05:31 -05005173}
5174
James Smart9bad7672008-12-04 22:39:02 -05005175/**
James Smart3621a712009-04-06 18:47:14 -04005176 * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05005177 * @cmnd: Pointer to scsi_cmnd data structure.
5178 *
James Smartbbb9d182009-06-10 17:23:16 -04005179 * This routine does target reset to all targets on @cmnd->device->host.
5180 * This emulates Parallel SCSI Bus Reset Semantics.
James Smart9bad7672008-12-04 22:39:02 -05005181 *
James Smartbbb9d182009-06-10 17:23:16 -04005182 * Return code :
5183 * 0x2003 - Error
5184 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05005185 **/
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04005186static int
James Smart7054a602007-04-25 09:52:34 -04005187lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05005188{
James Smart2e0fef82007-06-17 19:56:36 -05005189 struct Scsi_Host *shost = cmnd->device->host;
5190 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05005191 struct lpfc_nodelist *ndlp = NULL;
James Smartea2151b2008-09-07 11:52:10 -04005192 struct lpfc_scsi_event_header scsi_event;
James Smartbbb9d182009-06-10 17:23:16 -04005193 int match;
5194 int ret = SUCCESS, status, i;
James Smartea2151b2008-09-07 11:52:10 -04005195
5196 scsi_event.event_type = FC_REG_SCSI_EVENT;
5197 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
5198 scsi_event.lun = 0;
5199 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
5200 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
5201
James Smartbbb9d182009-06-10 17:23:16 -04005202 fc_host_post_vendor_event(shost, fc_get_event_number(),
5203 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
dea31012005-04-17 16:05:31 -05005204
James Smartbf086112011-08-21 21:48:13 -04005205 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005206 if (status != 0 && status != SUCCESS)
James Smartbf086112011-08-21 21:48:13 -04005207 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005208
dea31012005-04-17 16:05:31 -05005209 /*
5210 * Since the driver manages a single bus device, reset all
5211 * targets known to the driver. Should any target reset
5212 * fail, this routine returns failure to the midlayer.
5213 */
James Smarte17da182006-07-06 15:49:25 -04005214 for (i = 0; i < LPFC_MAX_TARGET; i++) {
James Smart685f0bf2007-04-25 09:53:08 -04005215 /* Search for mapped node by target ID */
dea31012005-04-17 16:05:31 -05005216 match = 0;
James Smart2e0fef82007-06-17 19:56:36 -05005217 spin_lock_irq(shost->host_lock);
5218 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05005219 if (!NLP_CHK_NODE_ACT(ndlp))
5220 continue;
James Smarta6571c62012-10-31 14:44:42 -04005221 if (vport->phba->cfg_fcp2_no_tgt_reset &&
5222 (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE))
5223 continue;
James Smart685f0bf2007-04-25 09:53:08 -04005224 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
James Smart915caaa2008-06-14 22:52:38 -04005225 ndlp->nlp_sid == i &&
James Smarta0f2d3e2017-02-12 13:52:31 -08005226 ndlp->rport &&
5227 ndlp->nlp_type & NLP_FCP_TARGET) {
dea31012005-04-17 16:05:31 -05005228 match = 1;
5229 break;
5230 }
5231 }
James Smart2e0fef82007-06-17 19:56:36 -05005232 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005233 if (!match)
5234 continue;
James Smartbbb9d182009-06-10 17:23:16 -04005235
James Smarteed695d2016-10-13 15:06:04 -07005236 status = lpfc_send_taskmgmt(vport, cmnd,
James Smartbbb9d182009-06-10 17:23:16 -04005237 i, 0, FCP_TARGET_RESET);
5238
5239 if (status != SUCCESS) {
James Smarte8b62012007-08-02 11:10:09 -04005240 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5241 "0700 Bus Reset on target %d failed\n",
5242 i);
James Smart915caaa2008-06-14 22:52:38 -04005243 ret = FAILED;
dea31012005-04-17 16:05:31 -05005244 }
5245 }
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005246 /*
James Smartbbb9d182009-06-10 17:23:16 -04005247 * We have to clean up i/o as : they may be orphaned by the TMFs
5248 * above; or if any of the TMFs failed, they may be in an
5249 * indeterminate state.
5250 * We will report success if all the i/o aborts successfully.
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005251 */
James Smartbbb9d182009-06-10 17:23:16 -04005252
5253 status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
5254 if (status != SUCCESS)
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005255 ret = FAILED;
James Smartbbb9d182009-06-10 17:23:16 -04005256
James Smarte8b62012007-08-02 11:10:09 -04005257 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5258 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea31012005-04-17 16:05:31 -05005259 return ret;
5260}
5261
James Smart9bad7672008-12-04 22:39:02 -05005262/**
James Smart27b01b82012-05-09 21:19:44 -04005263 * lpfc_host_reset_handler - scsi_host_template eh_host_reset_handler entry pt
5264 * @cmnd: Pointer to scsi_cmnd data structure.
5265 *
5266 * This routine does host reset to the adaptor port. It brings the HBA
5267 * offline, performs a board restart, and then brings the board back online.
5268 * The lpfc_offline calls lpfc_sli_hba_down which will abort and local
5269 * reject all outstanding SCSI commands to the host and error returned
5270 * back to SCSI mid-level. As this will be SCSI mid-level's last resort
5271 * of error handling, it will only return error if resetting of the adapter
5272 * is not successful; in all other cases, will return success.
5273 *
5274 * Return code :
5275 * 0x2003 - Error
5276 * 0x2002 - Success
5277 **/
5278static int
5279lpfc_host_reset_handler(struct scsi_cmnd *cmnd)
5280{
5281 struct Scsi_Host *shost = cmnd->device->host;
5282 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5283 struct lpfc_hba *phba = vport->phba;
5284 int rc, ret = SUCCESS;
5285
James Smarta88dbb62013-04-17 20:18:39 -04005286 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5287 "3172 SCSI layer issued Host Reset Data:\n");
5288
James Smart618a5232012-06-12 13:54:36 -04005289 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart27b01b82012-05-09 21:19:44 -04005290 lpfc_offline(phba);
5291 rc = lpfc_sli_brdrestart(phba);
5292 if (rc)
5293 ret = FAILED;
James Smarta88dbb62013-04-17 20:18:39 -04005294 rc = lpfc_online(phba);
5295 if (rc)
5296 ret = FAILED;
James Smart27b01b82012-05-09 21:19:44 -04005297 lpfc_unblock_mgmt_io(phba);
5298
James Smarta88dbb62013-04-17 20:18:39 -04005299 if (ret == FAILED) {
5300 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5301 "3323 Failed host reset, bring it offline\n");
5302 lpfc_sli4_offline_eratt(phba);
5303 }
James Smart27b01b82012-05-09 21:19:44 -04005304 return ret;
5305}
5306
5307/**
James Smart3621a712009-04-06 18:47:14 -04005308 * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
James Smart9bad7672008-12-04 22:39:02 -05005309 * @sdev: Pointer to scsi_device.
5310 *
5311 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
5312 * globally available list of scsi buffers. This routine also makes sure scsi
5313 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
5314 * of scsi buffer exists for the lifetime of the driver.
5315 *
5316 * Return codes:
5317 * non-0 - Error
5318 * 0 - Success
5319 **/
dea31012005-04-17 16:05:31 -05005320static int
dea31012005-04-17 16:05:31 -05005321lpfc_slave_alloc(struct scsi_device *sdev)
5322{
James Smart2e0fef82007-06-17 19:56:36 -05005323 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5324 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005325 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
James Smart3772a992009-05-22 14:50:54 -04005326 uint32_t total = 0;
dea31012005-04-17 16:05:31 -05005327 uint32_t num_to_alloc = 0;
James Smart3772a992009-05-22 14:50:54 -04005328 int num_allocated = 0;
James Smartd7c47992010-06-08 18:31:54 -04005329 uint32_t sdev_cnt;
James Smart1ba981f2014-02-20 09:56:45 -05005330 struct lpfc_device_data *device_data;
5331 unsigned long flags;
5332 struct lpfc_name target_wwpn;
dea31012005-04-17 16:05:31 -05005333
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005334 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05005335 return -ENXIO;
5336
James Smartf38fa0b2014-04-04 13:52:21 -04005337 if (phba->cfg_fof) {
James Smart1ba981f2014-02-20 09:56:45 -05005338
5339 /*
5340 * Check to see if the device data structure for the lun
5341 * exists. If not, create one.
5342 */
5343
5344 u64_to_wwn(rport->port_name, target_wwpn.u.wwn);
5345 spin_lock_irqsave(&phba->devicelock, flags);
5346 device_data = __lpfc_get_device_data(phba,
5347 &phba->luns,
5348 &vport->fc_portname,
5349 &target_wwpn,
5350 sdev->lun);
5351 if (!device_data) {
5352 spin_unlock_irqrestore(&phba->devicelock, flags);
5353 device_data = lpfc_create_device_data(phba,
5354 &vport->fc_portname,
5355 &target_wwpn,
James Smartb5749fe2016-12-19 15:07:26 -08005356 sdev->lun,
5357 phba->cfg_XLanePriority,
5358 true);
James Smart1ba981f2014-02-20 09:56:45 -05005359 if (!device_data)
5360 return -ENOMEM;
5361 spin_lock_irqsave(&phba->devicelock, flags);
5362 list_add_tail(&device_data->listentry, &phba->luns);
5363 }
5364 device_data->rport_data = rport->dd_data;
5365 device_data->available = true;
5366 spin_unlock_irqrestore(&phba->devicelock, flags);
5367 sdev->hostdata = device_data;
5368 } else {
5369 sdev->hostdata = rport->dd_data;
5370 }
James Smartd7c47992010-06-08 18:31:54 -04005371 sdev_cnt = atomic_inc_return(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05005372
James Smart0794d602019-01-28 11:14:19 -08005373 /* For SLI4, all IO buffers are pre-allocated */
5374 if (phba->sli_rev == LPFC_SLI_REV4)
5375 return 0;
5376
5377 /* This code path is now ONLY for SLI3 adapters */
5378
dea31012005-04-17 16:05:31 -05005379 /*
5380 * Populate the cmds_per_lun count scsi_bufs into this host's globally
5381 * available list of scsi buffers. Don't allocate more than the
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04005382 * HBA limit conveyed to the midlayer via the host structure. The
5383 * formula accounts for the lun_queue_depth + error handlers + 1
5384 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea31012005-04-17 16:05:31 -05005385 */
5386 total = phba->total_scsi_bufs;
James Smart3de2a652007-08-02 11:09:59 -04005387 num_to_alloc = vport->cfg_lun_queue_depth + 2;
James Smart92d7f7b2007-06-17 19:56:38 -05005388
James Smartd7c47992010-06-08 18:31:54 -04005389 /* If allocated buffers are enough do nothing */
5390 if ((sdev_cnt * (vport->cfg_lun_queue_depth + 2)) < total)
5391 return 0;
5392
James Smart92d7f7b2007-06-17 19:56:38 -05005393 /* Allow some exchanges to be available always to complete discovery */
5394 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04005395 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5396 "0704 At limitation of %d preallocated "
5397 "command buffers\n", total);
dea31012005-04-17 16:05:31 -05005398 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05005399 /* Allow some exchanges to be available always to complete discovery */
5400 } else if (total + num_to_alloc >
5401 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04005402 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5403 "0705 Allocation request of %d "
5404 "command buffers will exceed max of %d. "
5405 "Reducing allocation request to %d.\n",
5406 num_to_alloc, phba->cfg_hba_queue_depth,
5407 (phba->cfg_hba_queue_depth - total));
dea31012005-04-17 16:05:31 -05005408 num_to_alloc = phba->cfg_hba_queue_depth - total;
5409 }
James Smart0794d602019-01-28 11:14:19 -08005410 num_allocated = lpfc_new_scsi_buf_s3(vport, num_to_alloc);
James Smart3772a992009-05-22 14:50:54 -04005411 if (num_to_alloc != num_allocated) {
James Smart96f70772013-04-17 20:16:15 -04005412 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5413 "0708 Allocation request of %d "
5414 "command buffers did not succeed. "
5415 "Allocated %d buffers.\n",
5416 num_to_alloc, num_allocated);
dea31012005-04-17 16:05:31 -05005417 }
James Smart1c6f4ef52009-11-18 15:40:49 -05005418 if (num_allocated > 0)
5419 phba->total_scsi_bufs += num_allocated;
dea31012005-04-17 16:05:31 -05005420 return 0;
5421}
5422
James Smart9bad7672008-12-04 22:39:02 -05005423/**
James Smart3621a712009-04-06 18:47:14 -04005424 * lpfc_slave_configure - scsi_host_template slave_configure entry point
James Smart9bad7672008-12-04 22:39:02 -05005425 * @sdev: Pointer to scsi_device.
5426 *
5427 * This routine configures following items
5428 * - Tag command queuing support for @sdev if supported.
James Smart9bad7672008-12-04 22:39:02 -05005429 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
5430 *
5431 * Return codes:
5432 * 0 - Success
5433 **/
dea31012005-04-17 16:05:31 -05005434static int
5435lpfc_slave_configure(struct scsi_device *sdev)
5436{
James Smart2e0fef82007-06-17 19:56:36 -05005437 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5438 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05005439
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01005440 scsi_change_queue_depth(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05005441
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005442 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04005443 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -08005444 &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005445 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
5446 lpfc_poll_rearm_timer(phba);
5447 }
5448
dea31012005-04-17 16:05:31 -05005449 return 0;
5450}
5451
James Smart9bad7672008-12-04 22:39:02 -05005452/**
James Smart3621a712009-04-06 18:47:14 -04005453 * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
James Smart9bad7672008-12-04 22:39:02 -05005454 * @sdev: Pointer to scsi_device.
5455 *
5456 * This routine sets @sdev hostatdata filed to null.
5457 **/
dea31012005-04-17 16:05:31 -05005458static void
5459lpfc_slave_destroy(struct scsi_device *sdev)
5460{
James Smartd7c47992010-06-08 18:31:54 -04005461 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5462 struct lpfc_hba *phba = vport->phba;
James Smart1ba981f2014-02-20 09:56:45 -05005463 unsigned long flags;
5464 struct lpfc_device_data *device_data = sdev->hostdata;
5465
James Smartd7c47992010-06-08 18:31:54 -04005466 atomic_dec(&phba->sdev_cnt);
James Smartf38fa0b2014-04-04 13:52:21 -04005467 if ((phba->cfg_fof) && (device_data)) {
James Smart1ba981f2014-02-20 09:56:45 -05005468 spin_lock_irqsave(&phba->devicelock, flags);
5469 device_data->available = false;
5470 if (!device_data->oas_enabled)
5471 lpfc_delete_device_data(phba, device_data);
5472 spin_unlock_irqrestore(&phba->devicelock, flags);
5473 }
dea31012005-04-17 16:05:31 -05005474 sdev->hostdata = NULL;
5475 return;
5476}
5477
James Smart1ba981f2014-02-20 09:56:45 -05005478/**
5479 * lpfc_create_device_data - creates and initializes device data structure for OAS
5480 * @pha: Pointer to host bus adapter structure.
5481 * @vport_wwpn: Pointer to vport's wwpn information
5482 * @target_wwpn: Pointer to target's wwpn information
5483 * @lun: Lun on target
5484 * @atomic_create: Flag to indicate if memory should be allocated using the
5485 * GFP_ATOMIC flag or not.
5486 *
5487 * This routine creates a device data structure which will contain identifying
5488 * information for the device (host wwpn, target wwpn, lun), state of OAS,
5489 * whether or not the corresponding lun is available by the system,
5490 * and pointer to the rport data.
5491 *
5492 * Return codes:
5493 * NULL - Error
5494 * Pointer to lpfc_device_data - Success
5495 **/
5496struct lpfc_device_data*
5497lpfc_create_device_data(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
5498 struct lpfc_name *target_wwpn, uint64_t lun,
James Smartb5749fe2016-12-19 15:07:26 -08005499 uint32_t pri, bool atomic_create)
James Smart1ba981f2014-02-20 09:56:45 -05005500{
5501
5502 struct lpfc_device_data *lun_info;
5503 int memory_flags;
5504
5505 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
James Smartf38fa0b2014-04-04 13:52:21 -04005506 !(phba->cfg_fof))
James Smart1ba981f2014-02-20 09:56:45 -05005507 return NULL;
5508
5509 /* Attempt to create the device data to contain lun info */
5510
5511 if (atomic_create)
5512 memory_flags = GFP_ATOMIC;
5513 else
5514 memory_flags = GFP_KERNEL;
5515 lun_info = mempool_alloc(phba->device_data_mem_pool, memory_flags);
5516 if (!lun_info)
5517 return NULL;
5518 INIT_LIST_HEAD(&lun_info->listentry);
5519 lun_info->rport_data = NULL;
5520 memcpy(&lun_info->device_id.vport_wwpn, vport_wwpn,
5521 sizeof(struct lpfc_name));
5522 memcpy(&lun_info->device_id.target_wwpn, target_wwpn,
5523 sizeof(struct lpfc_name));
5524 lun_info->device_id.lun = lun;
5525 lun_info->oas_enabled = false;
James Smartb5749fe2016-12-19 15:07:26 -08005526 lun_info->priority = pri;
James Smart1ba981f2014-02-20 09:56:45 -05005527 lun_info->available = false;
5528 return lun_info;
5529}
5530
5531/**
5532 * lpfc_delete_device_data - frees a device data structure for OAS
5533 * @pha: Pointer to host bus adapter structure.
5534 * @lun_info: Pointer to device data structure to free.
5535 *
5536 * This routine frees the previously allocated device data structure passed.
5537 *
5538 **/
5539void
5540lpfc_delete_device_data(struct lpfc_hba *phba,
5541 struct lpfc_device_data *lun_info)
5542{
5543
5544 if (unlikely(!phba) || !lun_info ||
James Smartf38fa0b2014-04-04 13:52:21 -04005545 !(phba->cfg_fof))
James Smart1ba981f2014-02-20 09:56:45 -05005546 return;
5547
5548 if (!list_empty(&lun_info->listentry))
5549 list_del(&lun_info->listentry);
5550 mempool_free(lun_info, phba->device_data_mem_pool);
5551 return;
5552}
5553
5554/**
5555 * __lpfc_get_device_data - returns the device data for the specified lun
5556 * @pha: Pointer to host bus adapter structure.
5557 * @list: Point to list to search.
5558 * @vport_wwpn: Pointer to vport's wwpn information
5559 * @target_wwpn: Pointer to target's wwpn information
5560 * @lun: Lun on target
5561 *
5562 * This routine searches the list passed for the specified lun's device data.
5563 * This function does not hold locks, it is the responsibility of the caller
5564 * to ensure the proper lock is held before calling the function.
5565 *
5566 * Return codes:
5567 * NULL - Error
5568 * Pointer to lpfc_device_data - Success
5569 **/
5570struct lpfc_device_data*
5571__lpfc_get_device_data(struct lpfc_hba *phba, struct list_head *list,
5572 struct lpfc_name *vport_wwpn,
5573 struct lpfc_name *target_wwpn, uint64_t lun)
5574{
5575
5576 struct lpfc_device_data *lun_info;
5577
5578 if (unlikely(!phba) || !list || !vport_wwpn || !target_wwpn ||
James Smartf38fa0b2014-04-04 13:52:21 -04005579 !phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005580 return NULL;
5581
5582 /* Check to see if the lun is already enabled for OAS. */
5583
5584 list_for_each_entry(lun_info, list, listentry) {
5585 if ((memcmp(&lun_info->device_id.vport_wwpn, vport_wwpn,
5586 sizeof(struct lpfc_name)) == 0) &&
5587 (memcmp(&lun_info->device_id.target_wwpn, target_wwpn,
5588 sizeof(struct lpfc_name)) == 0) &&
5589 (lun_info->device_id.lun == lun))
5590 return lun_info;
5591 }
5592
5593 return NULL;
5594}
5595
5596/**
5597 * lpfc_find_next_oas_lun - searches for the next oas lun
5598 * @pha: Pointer to host bus adapter structure.
5599 * @vport_wwpn: Pointer to vport's wwpn information
5600 * @target_wwpn: Pointer to target's wwpn information
5601 * @starting_lun: Pointer to the lun to start searching for
5602 * @found_vport_wwpn: Pointer to the found lun's vport wwpn information
5603 * @found_target_wwpn: Pointer to the found lun's target wwpn information
5604 * @found_lun: Pointer to the found lun.
5605 * @found_lun_status: Pointer to status of the found lun.
5606 *
5607 * This routine searches the luns list for the specified lun
5608 * or the first lun for the vport/target. If the vport wwpn contains
5609 * a zero value then a specific vport is not specified. In this case
5610 * any vport which contains the lun will be considered a match. If the
5611 * target wwpn contains a zero value then a specific target is not specified.
5612 * In this case any target which contains the lun will be considered a
5613 * match. If the lun is found, the lun, vport wwpn, target wwpn and lun status
5614 * are returned. The function will also return the next lun if available.
5615 * If the next lun is not found, starting_lun parameter will be set to
5616 * NO_MORE_OAS_LUN.
5617 *
5618 * Return codes:
5619 * non-0 - Error
5620 * 0 - Success
5621 **/
5622bool
5623lpfc_find_next_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
5624 struct lpfc_name *target_wwpn, uint64_t *starting_lun,
5625 struct lpfc_name *found_vport_wwpn,
5626 struct lpfc_name *found_target_wwpn,
5627 uint64_t *found_lun,
James Smartb5749fe2016-12-19 15:07:26 -08005628 uint32_t *found_lun_status,
5629 uint32_t *found_lun_pri)
James Smart1ba981f2014-02-20 09:56:45 -05005630{
5631
5632 unsigned long flags;
5633 struct lpfc_device_data *lun_info;
5634 struct lpfc_device_id *device_id;
5635 uint64_t lun;
5636 bool found = false;
5637
5638 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
5639 !starting_lun || !found_vport_wwpn ||
5640 !found_target_wwpn || !found_lun || !found_lun_status ||
5641 (*starting_lun == NO_MORE_OAS_LUN) ||
James Smartf38fa0b2014-04-04 13:52:21 -04005642 !phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005643 return false;
5644
5645 lun = *starting_lun;
5646 *found_lun = NO_MORE_OAS_LUN;
5647 *starting_lun = NO_MORE_OAS_LUN;
5648
5649 /* Search for lun or the lun closet in value */
5650
5651 spin_lock_irqsave(&phba->devicelock, flags);
5652 list_for_each_entry(lun_info, &phba->luns, listentry) {
5653 if (((wwn_to_u64(vport_wwpn->u.wwn) == 0) ||
5654 (memcmp(&lun_info->device_id.vport_wwpn, vport_wwpn,
5655 sizeof(struct lpfc_name)) == 0)) &&
5656 ((wwn_to_u64(target_wwpn->u.wwn) == 0) ||
5657 (memcmp(&lun_info->device_id.target_wwpn, target_wwpn,
5658 sizeof(struct lpfc_name)) == 0)) &&
5659 (lun_info->oas_enabled)) {
5660 device_id = &lun_info->device_id;
5661 if ((!found) &&
5662 ((lun == FIND_FIRST_OAS_LUN) ||
5663 (device_id->lun == lun))) {
5664 *found_lun = device_id->lun;
5665 memcpy(found_vport_wwpn,
5666 &device_id->vport_wwpn,
5667 sizeof(struct lpfc_name));
5668 memcpy(found_target_wwpn,
5669 &device_id->target_wwpn,
5670 sizeof(struct lpfc_name));
5671 if (lun_info->available)
5672 *found_lun_status =
5673 OAS_LUN_STATUS_EXISTS;
5674 else
5675 *found_lun_status = 0;
James Smartb5749fe2016-12-19 15:07:26 -08005676 *found_lun_pri = lun_info->priority;
James Smart1ba981f2014-02-20 09:56:45 -05005677 if (phba->cfg_oas_flags & OAS_FIND_ANY_VPORT)
5678 memset(vport_wwpn, 0x0,
5679 sizeof(struct lpfc_name));
5680 if (phba->cfg_oas_flags & OAS_FIND_ANY_TARGET)
5681 memset(target_wwpn, 0x0,
5682 sizeof(struct lpfc_name));
5683 found = true;
5684 } else if (found) {
5685 *starting_lun = device_id->lun;
5686 memcpy(vport_wwpn, &device_id->vport_wwpn,
5687 sizeof(struct lpfc_name));
5688 memcpy(target_wwpn, &device_id->target_wwpn,
5689 sizeof(struct lpfc_name));
5690 break;
5691 }
5692 }
5693 }
5694 spin_unlock_irqrestore(&phba->devicelock, flags);
5695 return found;
5696}
5697
5698/**
5699 * lpfc_enable_oas_lun - enables a lun for OAS operations
5700 * @pha: Pointer to host bus adapter structure.
5701 * @vport_wwpn: Pointer to vport's wwpn information
5702 * @target_wwpn: Pointer to target's wwpn information
5703 * @lun: Lun
5704 *
5705 * This routine enables a lun for oas operations. The routines does so by
5706 * doing the following :
5707 *
5708 * 1) Checks to see if the device data for the lun has been created.
5709 * 2) If found, sets the OAS enabled flag if not set and returns.
5710 * 3) Otherwise, creates a device data structure.
5711 * 4) If successfully created, indicates the device data is for an OAS lun,
5712 * indicates the lun is not available and add to the list of luns.
5713 *
5714 * Return codes:
5715 * false - Error
5716 * true - Success
5717 **/
5718bool
5719lpfc_enable_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
James Smartc92c8412016-07-06 12:36:05 -07005720 struct lpfc_name *target_wwpn, uint64_t lun, uint8_t pri)
James Smart1ba981f2014-02-20 09:56:45 -05005721{
5722
5723 struct lpfc_device_data *lun_info;
5724 unsigned long flags;
5725
5726 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
James Smartf38fa0b2014-04-04 13:52:21 -04005727 !phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005728 return false;
5729
5730 spin_lock_irqsave(&phba->devicelock, flags);
5731
5732 /* Check to see if the device data for the lun has been created */
5733 lun_info = __lpfc_get_device_data(phba, &phba->luns, vport_wwpn,
5734 target_wwpn, lun);
5735 if (lun_info) {
5736 if (!lun_info->oas_enabled)
5737 lun_info->oas_enabled = true;
James Smartb5749fe2016-12-19 15:07:26 -08005738 lun_info->priority = pri;
James Smart1ba981f2014-02-20 09:56:45 -05005739 spin_unlock_irqrestore(&phba->devicelock, flags);
5740 return true;
5741 }
5742
5743 /* Create an lun info structure and add to list of luns */
5744 lun_info = lpfc_create_device_data(phba, vport_wwpn, target_wwpn, lun,
James Smartb5749fe2016-12-19 15:07:26 -08005745 pri, false);
James Smart1ba981f2014-02-20 09:56:45 -05005746 if (lun_info) {
5747 lun_info->oas_enabled = true;
James Smartc92c8412016-07-06 12:36:05 -07005748 lun_info->priority = pri;
James Smart1ba981f2014-02-20 09:56:45 -05005749 lun_info->available = false;
5750 list_add_tail(&lun_info->listentry, &phba->luns);
5751 spin_unlock_irqrestore(&phba->devicelock, flags);
5752 return true;
5753 }
5754 spin_unlock_irqrestore(&phba->devicelock, flags);
5755 return false;
5756}
5757
5758/**
5759 * lpfc_disable_oas_lun - disables a lun for OAS operations
5760 * @pha: Pointer to host bus adapter structure.
5761 * @vport_wwpn: Pointer to vport's wwpn information
5762 * @target_wwpn: Pointer to target's wwpn information
5763 * @lun: Lun
5764 *
5765 * This routine disables a lun for oas operations. The routines does so by
5766 * doing the following :
5767 *
5768 * 1) Checks to see if the device data for the lun is created.
5769 * 2) If present, clears the flag indicating this lun is for OAS.
5770 * 3) If the lun is not available by the system, the device data is
5771 * freed.
5772 *
5773 * Return codes:
5774 * false - Error
5775 * true - Success
5776 **/
5777bool
5778lpfc_disable_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
James Smartb5749fe2016-12-19 15:07:26 -08005779 struct lpfc_name *target_wwpn, uint64_t lun, uint8_t pri)
James Smart1ba981f2014-02-20 09:56:45 -05005780{
5781
5782 struct lpfc_device_data *lun_info;
5783 unsigned long flags;
5784
5785 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
James Smartf38fa0b2014-04-04 13:52:21 -04005786 !phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005787 return false;
5788
5789 spin_lock_irqsave(&phba->devicelock, flags);
5790
5791 /* Check to see if the lun is available. */
5792 lun_info = __lpfc_get_device_data(phba,
5793 &phba->luns, vport_wwpn,
5794 target_wwpn, lun);
5795 if (lun_info) {
5796 lun_info->oas_enabled = false;
James Smartb5749fe2016-12-19 15:07:26 -08005797 lun_info->priority = pri;
James Smart1ba981f2014-02-20 09:56:45 -05005798 if (!lun_info->available)
5799 lpfc_delete_device_data(phba, lun_info);
5800 spin_unlock_irqrestore(&phba->devicelock, flags);
5801 return true;
5802 }
5803
5804 spin_unlock_irqrestore(&phba->devicelock, flags);
5805 return false;
5806}
James Smart92d7f7b2007-06-17 19:56:38 -05005807
James Smart895427b2017-02-12 13:52:30 -08005808static int
5809lpfc_no_command(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
5810{
5811 return SCSI_MLQUEUE_HOST_BUSY;
5812}
5813
5814static int
5815lpfc_no_handler(struct scsi_cmnd *cmnd)
5816{
5817 return FAILED;
5818}
5819
5820static int
5821lpfc_no_slave(struct scsi_device *sdev)
5822{
5823 return -ENODEV;
5824}
5825
5826struct scsi_host_template lpfc_template_nvme = {
5827 .module = THIS_MODULE,
5828 .name = LPFC_DRIVER_NAME,
5829 .proc_name = LPFC_DRIVER_NAME,
5830 .info = lpfc_info,
5831 .queuecommand = lpfc_no_command,
5832 .eh_abort_handler = lpfc_no_handler,
5833 .eh_device_reset_handler = lpfc_no_handler,
5834 .eh_target_reset_handler = lpfc_no_handler,
5835 .eh_bus_reset_handler = lpfc_no_handler,
5836 .eh_host_reset_handler = lpfc_no_handler,
5837 .slave_alloc = lpfc_no_slave,
5838 .slave_configure = lpfc_no_slave,
5839 .scan_finished = lpfc_scan_finished,
5840 .this_id = -1,
5841 .sg_tablesize = 1,
5842 .cmd_per_lun = 1,
James Smart895427b2017-02-12 13:52:30 -08005843 .shost_attrs = lpfc_hba_attrs,
5844 .max_sectors = 0xFFFF,
5845 .vendor_id = LPFC_NL_VENDOR_ID,
5846 .track_queue_depth = 0,
5847};
5848
James Smart96418b52017-03-04 09:30:31 -08005849struct scsi_host_template lpfc_template_no_hr = {
James Smartea4142f2015-04-07 15:07:13 -04005850 .module = THIS_MODULE,
5851 .name = LPFC_DRIVER_NAME,
James Smart08dcd4c2016-07-06 12:35:59 -07005852 .proc_name = LPFC_DRIVER_NAME,
James Smartea4142f2015-04-07 15:07:13 -04005853 .info = lpfc_info,
5854 .queuecommand = lpfc_queuecommand,
James Smart856984b2017-03-04 09:30:32 -08005855 .eh_timed_out = fc_eh_timed_out,
James Smartea4142f2015-04-07 15:07:13 -04005856 .eh_abort_handler = lpfc_abort_handler,
5857 .eh_device_reset_handler = lpfc_device_reset_handler,
5858 .eh_target_reset_handler = lpfc_target_reset_handler,
5859 .eh_bus_reset_handler = lpfc_bus_reset_handler,
5860 .slave_alloc = lpfc_slave_alloc,
5861 .slave_configure = lpfc_slave_configure,
5862 .slave_destroy = lpfc_slave_destroy,
5863 .scan_finished = lpfc_scan_finished,
5864 .this_id = -1,
5865 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
5866 .cmd_per_lun = LPFC_CMD_PER_LUN,
James Smartea4142f2015-04-07 15:07:13 -04005867 .shost_attrs = lpfc_hba_attrs,
5868 .max_sectors = 0xFFFF,
5869 .vendor_id = LPFC_NL_VENDOR_ID,
5870 .change_queue_depth = scsi_change_queue_depth,
James Smartea4142f2015-04-07 15:07:13 -04005871 .track_queue_depth = 1,
5872};
5873
dea31012005-04-17 16:05:31 -05005874struct scsi_host_template lpfc_template = {
5875 .module = THIS_MODULE,
5876 .name = LPFC_DRIVER_NAME,
James Smart08dcd4c2016-07-06 12:35:59 -07005877 .proc_name = LPFC_DRIVER_NAME,
dea31012005-04-17 16:05:31 -05005878 .info = lpfc_info,
5879 .queuecommand = lpfc_queuecommand,
Christoph Hellwigb6a05c82017-01-30 13:18:58 +01005880 .eh_timed_out = fc_eh_timed_out,
dea31012005-04-17 16:05:31 -05005881 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04005882 .eh_device_reset_handler = lpfc_device_reset_handler,
5883 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart7054a602007-04-25 09:52:34 -04005884 .eh_bus_reset_handler = lpfc_bus_reset_handler,
James Smart27b01b82012-05-09 21:19:44 -04005885 .eh_host_reset_handler = lpfc_host_reset_handler,
dea31012005-04-17 16:05:31 -05005886 .slave_alloc = lpfc_slave_alloc,
5887 .slave_configure = lpfc_slave_configure,
5888 .slave_destroy = lpfc_slave_destroy,
James Smart47a86172007-04-25 09:53:22 -04005889 .scan_finished = lpfc_scan_finished,
dea31012005-04-17 16:05:31 -05005890 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05005891 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
dea31012005-04-17 16:05:31 -05005892 .cmd_per_lun = LPFC_CMD_PER_LUN,
James Smart2e0fef82007-06-17 19:56:36 -05005893 .shost_attrs = lpfc_hba_attrs,
James.Smart@Emulex.Com564b2962005-06-25 10:34:17 -04005894 .max_sectors = 0xFFFF,
James Smartf1c3b0f2009-07-19 10:01:32 -04005895 .vendor_id = LPFC_NL_VENDOR_ID,
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01005896 .change_queue_depth = scsi_change_queue_depth,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01005897 .track_queue_depth = 1,
dea31012005-04-17 16:05:31 -05005898};
James Smart3de2a652007-08-02 11:09:59 -04005899
5900struct scsi_host_template lpfc_vport_template = {
5901 .module = THIS_MODULE,
5902 .name = LPFC_DRIVER_NAME,
James Smart08dcd4c2016-07-06 12:35:59 -07005903 .proc_name = LPFC_DRIVER_NAME,
James Smart3de2a652007-08-02 11:09:59 -04005904 .info = lpfc_info,
5905 .queuecommand = lpfc_queuecommand,
Christoph Hellwigb6a05c82017-01-30 13:18:58 +01005906 .eh_timed_out = fc_eh_timed_out,
James Smart3de2a652007-08-02 11:09:59 -04005907 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04005908 .eh_device_reset_handler = lpfc_device_reset_handler,
5909 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart3de2a652007-08-02 11:09:59 -04005910 .slave_alloc = lpfc_slave_alloc,
5911 .slave_configure = lpfc_slave_configure,
5912 .slave_destroy = lpfc_slave_destroy,
5913 .scan_finished = lpfc_scan_finished,
5914 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05005915 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
James Smart3de2a652007-08-02 11:09:59 -04005916 .cmd_per_lun = LPFC_CMD_PER_LUN,
James Smart3de2a652007-08-02 11:09:59 -04005917 .shost_attrs = lpfc_vport_attrs,
5918 .max_sectors = 0xFFFF,
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01005919 .change_queue_depth = scsi_change_queue_depth,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01005920 .track_queue_depth = 1,
James Smart3de2a652007-08-02 11:09:59 -04005921};