blob: 7c65bd652c4dd3483a352e322933ab7ee1e874d5 [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
James Smartc00f62e2019-08-14 16:57:11 -0700540 spin_lock(&qp->abts_io_buf_list_lock);
James Smart5e5b5112019-01-28 11:14:22 -0800541 list_for_each_entry_safe(psb, next_psb,
James Smartc00f62e2019-08-14 16:57:11 -0700542 &qp->lpfc_abts_io_buf_list, list) {
543 if (psb->cur_iocbq.iocb_flag == LPFC_IO_NVME)
544 continue;
545
James Smart5e5b5112019-01-28 11:14:22 -0800546 if (psb->rdata && psb->rdata->pnode &&
547 psb->rdata->pnode->vport == vport)
548 psb->rdata = NULL;
549 }
James Smartc00f62e2019-08-14 16:57:11 -0700550 spin_unlock(&qp->abts_io_buf_list_lock);
James Smart1151e3e2011-02-16 12:39:35 -0500551 }
James Smart1151e3e2011-02-16 12:39:35 -0500552 spin_unlock_irqrestore(&phba->hbalock, iflag);
553}
554
555/**
James Smartc00f62e2019-08-14 16:57:11 -0700556 * lpfc_sli4_io_xri_aborted - Fast-path process of fcp xri abort
James Smartda0436e2009-05-22 14:51:39 -0400557 * @phba: pointer to lpfc hba data structure.
558 * @axri: pointer to the fcp xri abort wcqe structure.
559 *
560 * This routine is invoked by the worker thread to process a SLI4 fast-path
James Smartc00f62e2019-08-14 16:57:11 -0700561 * FCP or NVME aborted xri.
James Smartda0436e2009-05-22 14:51:39 -0400562 **/
563void
James Smartc00f62e2019-08-14 16:57:11 -0700564lpfc_sli4_io_xri_aborted(struct lpfc_hba *phba,
565 struct sli4_wcqe_xri_aborted *axri, int idx)
James Smartda0436e2009-05-22 14:51:39 -0400566{
567 uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
James Smart19ca7602010-11-20 23:11:55 -0500568 uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
James Smartc4908502019-01-28 11:14:28 -0800569 struct lpfc_io_buf *psb, *next_psb;
James Smart5e5b5112019-01-28 11:14:22 -0800570 struct lpfc_sli4_hdw_queue *qp;
James Smartda0436e2009-05-22 14:51:39 -0400571 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -0500572 struct lpfc_iocbq *iocbq;
573 int i;
James Smart19ca7602010-11-20 23:11:55 -0500574 struct lpfc_nodelist *ndlp;
575 int rrq_empty = 0;
James Smart895427b2017-02-12 13:52:30 -0800576 struct lpfc_sli_ring *pring = phba->sli4_hba.els_wq->pring;
James Smartda0436e2009-05-22 14:51:39 -0400577
James Smart895427b2017-02-12 13:52:30 -0800578 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
579 return;
James Smart5e5b5112019-01-28 11:14:22 -0800580
581 qp = &phba->sli4_hba.hdwq[idx];
James Smart0f65ff62010-02-26 14:14:23 -0500582 spin_lock_irqsave(&phba->hbalock, iflag);
James Smartc00f62e2019-08-14 16:57:11 -0700583 spin_lock(&qp->abts_io_buf_list_lock);
James Smartda0436e2009-05-22 14:51:39 -0400584 list_for_each_entry_safe(psb, next_psb,
James Smartc00f62e2019-08-14 16:57:11 -0700585 &qp->lpfc_abts_io_buf_list, list) {
James Smartda0436e2009-05-22 14:51:39 -0400586 if (psb->cur_iocbq.sli4_xritag == xri) {
James Smartc00f62e2019-08-14 16:57:11 -0700587 list_del_init(&psb->list);
James Smart341af102010-01-26 23:07:37 -0500588 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400589 psb->status = IOSTAT_SUCCESS;
James Smartc00f62e2019-08-14 16:57:11 -0700590#ifdef BUILD_NVME
591 if (psb->cur_iocbq.iocb_flag == LPFC_IO_NVME) {
592 qp->abts_nvme_io_bufs--;
593 spin_unlock(&qp->abts_io_buf_list_lock);
594 spin_unlock_irqrestore(&phba->hbalock, iflag);
595 lpfc_sli4_nvme_xri_aborted(phba, axri, psb);
596 return;
597 }
598#endif
599 qp->abts_scsi_io_bufs--;
600 spin_unlock(&qp->abts_io_buf_list_lock);
601
James Smart1151e3e2011-02-16 12:39:35 -0500602 if (psb->rdata && psb->rdata->pnode)
603 ndlp = psb->rdata->pnode;
604 else
605 ndlp = NULL;
606
James Smart19ca7602010-11-20 23:11:55 -0500607 rrq_empty = list_empty(&phba->active_rrq_list);
James Smart0f65ff62010-02-26 14:14:23 -0500608 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartcb69f7d2011-12-13 13:21:57 -0500609 if (ndlp) {
James Smartee0f4fe2012-05-09 21:19:14 -0400610 lpfc_set_rrq_active(phba, ndlp,
611 psb->cur_iocbq.sli4_lxritag, rxid, 1);
James Smartcb69f7d2011-12-13 13:21:57 -0500612 lpfc_sli4_abts_err_handler(phba, ndlp, axri);
613 }
James Smartda0436e2009-05-22 14:51:39 -0400614 lpfc_release_scsi_buf_s4(phba, psb);
James Smart19ca7602010-11-20 23:11:55 -0500615 if (rrq_empty)
616 lpfc_worker_wake_up(phba);
James Smartda0436e2009-05-22 14:51:39 -0400617 return;
618 }
619 }
James Smartc00f62e2019-08-14 16:57:11 -0700620 spin_unlock(&qp->abts_io_buf_list_lock);
James Smart0f65ff62010-02-26 14:14:23 -0500621 for (i = 1; i <= phba->sli.last_iotag; i++) {
622 iocbq = phba->sli.iocbq_lookup[i];
623
James Smartc00f62e2019-08-14 16:57:11 -0700624 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
625 (iocbq->iocb_flag & LPFC_IO_LIBDFC))
James Smart0f65ff62010-02-26 14:14:23 -0500626 continue;
627 if (iocbq->sli4_xritag != xri)
628 continue;
James Smartc4908502019-01-28 11:14:28 -0800629 psb = container_of(iocbq, struct lpfc_io_buf, cur_iocbq);
James Smart0f65ff62010-02-26 14:14:23 -0500630 psb->exch_busy = 0;
631 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart0e9bb8d2013-03-01 16:35:12 -0500632 if (!list_empty(&pring->txq))
James Smart589a52d2010-07-14 15:30:54 -0400633 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -0500634 return;
635
636 }
637 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartda0436e2009-05-22 14:51:39 -0400638}
639
640/**
James Smart19ca7602010-11-20 23:11:55 -0500641 * lpfc_get_scsi_buf_s3 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
James Smart3772a992009-05-22 14:50:54 -0400642 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -0500643 *
644 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
645 * and returns to caller.
646 *
647 * Return codes:
648 * NULL - Error
649 * Pointer to lpfc_scsi_buf - Success
650 **/
James Smartc4908502019-01-28 11:14:28 -0800651static struct lpfc_io_buf *
James Smartace44e42019-01-28 11:14:27 -0800652lpfc_get_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
653 struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -0500654{
James Smartc4908502019-01-28 11:14:28 -0800655 struct lpfc_io_buf *lpfc_cmd = NULL;
James Smarta40fc5f2013-04-17 20:17:40 -0400656 struct list_head *scsi_buf_list_get = &phba->lpfc_scsi_buf_list_get;
James Smart164cecd2013-09-06 12:22:38 -0400657 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500658
James Smart164cecd2013-09-06 12:22:38 -0400659 spin_lock_irqsave(&phba->scsi_buf_list_get_lock, iflag);
James Smartc4908502019-01-28 11:14:28 -0800660 list_remove_head(scsi_buf_list_get, lpfc_cmd, struct lpfc_io_buf,
James Smarta40fc5f2013-04-17 20:17:40 -0400661 list);
662 if (!lpfc_cmd) {
James Smart164cecd2013-09-06 12:22:38 -0400663 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -0400664 list_splice(&phba->lpfc_scsi_buf_list_put,
665 &phba->lpfc_scsi_buf_list_get);
666 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
667 list_remove_head(scsi_buf_list_get, lpfc_cmd,
James Smartc4908502019-01-28 11:14:28 -0800668 struct lpfc_io_buf, list);
James Smart164cecd2013-09-06 12:22:38 -0400669 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smart1dcb58e2007-04-25 09:51:30 -0400670 }
James Smart164cecd2013-09-06 12:22:38 -0400671 spin_unlock_irqrestore(&phba->scsi_buf_list_get_lock, iflag);
James Smart2a5b7d62018-07-31 17:23:22 -0700672
673 if (lpfc_ndlp_check_qdepth(phba, ndlp) && lpfc_cmd) {
674 atomic_inc(&ndlp->cmd_pending);
675 lpfc_cmd->flags |= LPFC_SBUF_BUMP_QDEPTH;
676 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400677 return lpfc_cmd;
678}
James Smart19ca7602010-11-20 23:11:55 -0500679/**
James Smart5e5b5112019-01-28 11:14:22 -0800680 * lpfc_get_scsi_buf_s4 - Get a scsi buffer from io_buf_list of the HBA
James Smart19ca7602010-11-20 23:11:55 -0500681 * @phba: The HBA for which this call is being executed.
682 *
James Smart5e5b5112019-01-28 11:14:22 -0800683 * This routine removes a scsi buffer from head of @hdwq io_buf_list
James Smart19ca7602010-11-20 23:11:55 -0500684 * and returns to caller.
685 *
686 * Return codes:
687 * NULL - Error
688 * Pointer to lpfc_scsi_buf - Success
689 **/
James Smartc4908502019-01-28 11:14:28 -0800690static struct lpfc_io_buf *
James Smartace44e42019-01-28 11:14:27 -0800691lpfc_get_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
692 struct scsi_cmnd *cmnd)
James Smart19ca7602010-11-20 23:11:55 -0500693{
James Smartc4908502019-01-28 11:14:28 -0800694 struct lpfc_io_buf *lpfc_cmd;
James Smart5e5b5112019-01-28 11:14:22 -0800695 struct lpfc_sli4_hdw_queue *qp;
James Smart0794d602019-01-28 11:14:19 -0800696 struct sli4_sge *sgl;
697 IOCB_t *iocb;
698 dma_addr_t pdma_phys_fcp_rsp;
699 dma_addr_t pdma_phys_fcp_cmd;
James Smartd79c9e92019-08-14 16:57:09 -0700700 uint32_t cpu, idx;
James Smartace44e42019-01-28 11:14:27 -0800701 int tag;
James Smartd79c9e92019-08-14 16:57:09 -0700702 struct fcp_cmd_rsp_buf *tmp = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500703
Bart Van Assched6d189c2019-03-28 11:06:22 -0700704 cpu = raw_smp_processor_id();
James Smart45aa3122019-01-28 11:14:29 -0800705 if (cmnd && phba->cfg_fcp_io_sched == LPFC_FCP_SCHED_BY_HDWQ) {
James Smartace44e42019-01-28 11:14:27 -0800706 tag = blk_mq_unique_tag(cmnd->request);
707 idx = blk_mq_unique_tag_to_hwq(tag);
708 } else {
James Smart6a828b02019-01-28 11:14:31 -0800709 idx = phba->sli4_hba.cpu_map[cpu].hdwq;
James Smartace44e42019-01-28 11:14:27 -0800710 }
James Smart5e5b5112019-01-28 11:14:22 -0800711
James Smartc4908502019-01-28 11:14:28 -0800712 lpfc_cmd = lpfc_get_io_buf(phba, ndlp, idx,
713 !phba->cfg_xri_rebalancing);
714 if (!lpfc_cmd) {
715 qp = &phba->sli4_hba.hdwq[idx];
James Smart5e5b5112019-01-28 11:14:22 -0800716 qp->empty_io_bufs++;
James Smart1151e3e2011-02-16 12:39:35 -0500717 return NULL;
James Smart5e5b5112019-01-28 11:14:22 -0800718 }
James Smart2a5b7d62018-07-31 17:23:22 -0700719
James Smart0794d602019-01-28 11:14:19 -0800720 /* Setup key fields in buffer that may have been changed
721 * if other protocols used this buffer.
722 */
723 lpfc_cmd->cur_iocbq.iocb_flag = LPFC_IO_FCP;
724 lpfc_cmd->prot_seg_cnt = 0;
725 lpfc_cmd->seg_cnt = 0;
James Smart0794d602019-01-28 11:14:19 -0800726 lpfc_cmd->timeout = 0;
727 lpfc_cmd->flags = 0;
728 lpfc_cmd->start_time = jiffies;
729 lpfc_cmd->waitq = NULL;
James Smart5e5b5112019-01-28 11:14:22 -0800730 lpfc_cmd->cpu = cpu;
James Smart0794d602019-01-28 11:14:19 -0800731#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
732 lpfc_cmd->prot_data_type = 0;
733#endif
James Smartd79c9e92019-08-14 16:57:09 -0700734 tmp = lpfc_get_cmd_rsp_buf_per_hdwq(phba, lpfc_cmd);
735 if (!tmp)
736 return NULL;
737
738 lpfc_cmd->fcp_cmnd = tmp->fcp_cmnd;
739 lpfc_cmd->fcp_rsp = tmp->fcp_rsp;
James Smart0794d602019-01-28 11:14:19 -0800740
741 /*
742 * The first two SGEs are the FCP_CMD and FCP_RSP.
743 * The balance are sg list bdes. Initialize the
744 * first two and leave the rest for queuecommand.
745 */
746 sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
James Smartd79c9e92019-08-14 16:57:09 -0700747 pdma_phys_fcp_cmd = tmp->fcp_cmd_rsp_dma_handle;
James Smart0794d602019-01-28 11:14:19 -0800748 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
749 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
750 sgl->word2 = le32_to_cpu(sgl->word2);
751 bf_set(lpfc_sli4_sge_last, sgl, 0);
752 sgl->word2 = cpu_to_le32(sgl->word2);
753 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_cmnd));
754 sgl++;
755
756 /* Setup the physical region for the FCP RSP */
757 pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
758 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
759 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
760 sgl->word2 = le32_to_cpu(sgl->word2);
761 bf_set(lpfc_sli4_sge_last, sgl, 1);
762 sgl->word2 = cpu_to_le32(sgl->word2);
763 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
764
765 /*
766 * Since the IOCB for the FCP I/O is built into this
James Smartc4908502019-01-28 11:14:28 -0800767 * lpfc_io_buf, initialize it with all known data now.
James Smart0794d602019-01-28 11:14:19 -0800768 */
769 iocb = &lpfc_cmd->cur_iocbq.iocb;
770 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
771 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
772 /* setting the BLP size to 2 * sizeof BDE may not be correct.
773 * We are setting the bpl to point to out sgl. An sgl's
774 * entries are 16 bytes, a bpl entries are 12 bytes.
775 */
776 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
777 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
778 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
779 iocb->ulpBdeCount = 1;
780 iocb->ulpLe = 1;
781 iocb->ulpClass = CLASS3;
782
Dan Carpenter26c724a2018-08-23 16:56:33 +0300783 if (lpfc_ndlp_check_qdepth(phba, ndlp)) {
James Smart2a5b7d62018-07-31 17:23:22 -0700784 atomic_inc(&ndlp->cmd_pending);
785 lpfc_cmd->flags |= LPFC_SBUF_BUMP_QDEPTH;
786 }
James Smarta40fc5f2013-04-17 20:17:40 -0400787 return lpfc_cmd;
James Smart19ca7602010-11-20 23:11:55 -0500788}
789/**
790 * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
791 * @phba: The HBA for which this call is being executed.
792 *
793 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
794 * and returns to caller.
795 *
796 * Return codes:
797 * NULL - Error
798 * Pointer to lpfc_scsi_buf - Success
799 **/
James Smartc4908502019-01-28 11:14:28 -0800800static struct lpfc_io_buf*
James Smartace44e42019-01-28 11:14:27 -0800801lpfc_get_scsi_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
802 struct scsi_cmnd *cmnd)
James Smart19ca7602010-11-20 23:11:55 -0500803{
James Smartace44e42019-01-28 11:14:27 -0800804 return phba->lpfc_get_scsi_buf(phba, ndlp, cmnd);
James Smart19ca7602010-11-20 23:11:55 -0500805}
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400806
James Smart9bad7672008-12-04 22:39:02 -0500807/**
James Smart3772a992009-05-22 14:50:54 -0400808 * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
James Smart9bad7672008-12-04 22:39:02 -0500809 * @phba: The Hba for which this call is being executed.
810 * @psb: The scsi buffer which is being released.
811 *
812 * This routine releases @psb scsi buffer by adding it to tail of @phba
813 * lpfc_scsi_buf_list list.
814 **/
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400815static void
James Smartc4908502019-01-28 11:14:28 -0800816lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_io_buf *psb)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400817{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500818 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500819
James Smarta40fc5f2013-04-17 20:17:40 -0400820 psb->seg_cnt = 0;
James Smarta40fc5f2013-04-17 20:17:40 -0400821 psb->prot_seg_cnt = 0;
822
823 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400824 psb->pCmd = NULL;
James Smart6a485eb2013-04-17 20:19:00 -0400825 psb->cur_iocbq.iocb_flag = LPFC_IO_FCP;
James Smarta40fc5f2013-04-17 20:17:40 -0400826 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list_put);
827 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
dea31012005-04-17 16:05:31 -0500828}
829
James Smart9bad7672008-12-04 22:39:02 -0500830/**
James Smartda0436e2009-05-22 14:51:39 -0400831 * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
832 * @phba: The Hba for which this call is being executed.
833 * @psb: The scsi buffer which is being released.
834 *
James Smart5e5b5112019-01-28 11:14:22 -0800835 * This routine releases @psb scsi buffer by adding it to tail of @hdwq
836 * io_buf_list list. For SLI4 XRI's are tied to the scsi buffer
James Smartda0436e2009-05-22 14:51:39 -0400837 * and cannot be reused for at least RA_TOV amount of time if it was
838 * aborted.
839 **/
840static void
James Smartc4908502019-01-28 11:14:28 -0800841lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_io_buf *psb)
James Smartda0436e2009-05-22 14:51:39 -0400842{
James Smart5e5b5112019-01-28 11:14:22 -0800843 struct lpfc_sli4_hdw_queue *qp;
James Smartda0436e2009-05-22 14:51:39 -0400844 unsigned long iflag = 0;
845
James Smarta40fc5f2013-04-17 20:17:40 -0400846 psb->seg_cnt = 0;
James Smarta40fc5f2013-04-17 20:17:40 -0400847 psb->prot_seg_cnt = 0;
848
James Smart1fbf9742019-01-28 11:14:26 -0800849 qp = psb->hdwq;
James Smart341af102010-01-26 23:07:37 -0500850 if (psb->exch_busy) {
James Smartc00f62e2019-08-14 16:57:11 -0700851 spin_lock_irqsave(&qp->abts_io_buf_list_lock, iflag);
James Smartda0436e2009-05-22 14:51:39 -0400852 psb->pCmd = NULL;
James Smartc00f62e2019-08-14 16:57:11 -0700853 list_add_tail(&psb->list, &qp->lpfc_abts_io_buf_list);
James Smart5e5b5112019-01-28 11:14:22 -0800854 qp->abts_scsi_io_bufs++;
James Smartc00f62e2019-08-14 16:57:11 -0700855 spin_unlock_irqrestore(&qp->abts_io_buf_list_lock, iflag);
James Smartda0436e2009-05-22 14:51:39 -0400856 } else {
James Smartc4908502019-01-28 11:14:28 -0800857 lpfc_release_io_buf(phba, (struct lpfc_io_buf *)psb, qp);
James Smartda0436e2009-05-22 14:51:39 -0400858 }
859}
860
861/**
James Smart3772a992009-05-22 14:50:54 -0400862 * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
863 * @phba: The Hba for which this call is being executed.
864 * @psb: The scsi buffer which is being released.
865 *
866 * This routine releases @psb scsi buffer by adding it to tail of @phba
867 * lpfc_scsi_buf_list list.
868 **/
869static void
James Smartc4908502019-01-28 11:14:28 -0800870lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_io_buf *psb)
James Smart3772a992009-05-22 14:50:54 -0400871{
James Smart2a5b7d62018-07-31 17:23:22 -0700872 if ((psb->flags & LPFC_SBUF_BUMP_QDEPTH) && psb->ndlp)
873 atomic_dec(&psb->ndlp->cmd_pending);
James Smart3772a992009-05-22 14:50:54 -0400874
James Smart2a5b7d62018-07-31 17:23:22 -0700875 psb->flags &= ~LPFC_SBUF_BUMP_QDEPTH;
James Smart3772a992009-05-22 14:50:54 -0400876 phba->lpfc_release_scsi_buf(phba, psb);
877}
878
879/**
880 * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -0500881 * @phba: The Hba for which this call is being executed.
882 * @lpfc_cmd: The scsi buffer which is going to be mapped.
883 *
884 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
James Smart3772a992009-05-22 14:50:54 -0400885 * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -0700886 * through sg elements and format the bde. This routine also initializes all
James Smart3772a992009-05-22 14:50:54 -0400887 * IOCB fields which are dependent on scsi command request buffer.
James Smart9bad7672008-12-04 22:39:02 -0500888 *
889 * Return codes:
890 * 1 - Error
891 * 0 - Success
892 **/
dea31012005-04-17 16:05:31 -0500893static int
James Smartc4908502019-01-28 11:14:28 -0800894lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
dea31012005-04-17 16:05:31 -0500895{
896 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
897 struct scatterlist *sgel = NULL;
898 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smartc4908502019-01-28 11:14:28 -0800899 struct ulp_bde64 *bpl = (struct ulp_bde64 *)lpfc_cmd->dma_sgl;
James Smart0f65ff62010-02-26 14:14:23 -0500900 struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
dea31012005-04-17 16:05:31 -0500901 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart34b02dc2008-08-24 21:49:55 -0400902 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
dea31012005-04-17 16:05:31 -0500903 dma_addr_t physaddr;
James Smart34b02dc2008-08-24 21:49:55 -0400904 uint32_t num_bde = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500905 int nseg, datadir = scsi_cmnd->sc_data_direction;
dea31012005-04-17 16:05:31 -0500906
907 /*
908 * There are three possibilities here - use scatter-gather segment, use
909 * the single mapping, or neither. Start the lpfc command prep by
910 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
911 * data bde entry.
912 */
913 bpl += 2;
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -0700914 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -0500915 /*
916 * The driver stores the segment count returned from pci_map_sg
917 * because this a count of dma-mappings used to map the use_sg
918 * pages. They are not guaranteed to be the same for those
919 * architectures that implement an IOMMU.
920 */
dea31012005-04-17 16:05:31 -0500921
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -0700922 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
923 scsi_sg_count(scsi_cmnd), datadir);
924 if (unlikely(!nseg))
925 return 1;
926
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500927 lpfc_cmd->seg_cnt = nseg;
dea31012005-04-17 16:05:31 -0500928 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -0400929 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
930 "9064 BLKGRD: %s: Too many sg segments from "
James Smarte2a0a9d2008-12-04 22:40:02 -0500931 "dma_map_sg. Config %d, seg_cnt %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700932 __func__, phba->cfg_sg_seg_cnt,
dea31012005-04-17 16:05:31 -0500933 lpfc_cmd->seg_cnt);
James Smart5e0e2312019-08-14 16:56:54 -0700934 WARN_ON_ONCE(lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt);
James Smart96f70772013-04-17 20:16:15 -0400935 lpfc_cmd->seg_cnt = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500936 scsi_dma_unmap(scsi_cmnd);
James Smart5e0e2312019-08-14 16:56:54 -0700937 return 2;
dea31012005-04-17 16:05:31 -0500938 }
939
940 /*
941 * The driver established a maximum scatter-gather segment count
942 * during probe that limits the number of sg elements in any
943 * single scsi command. Just run through the seg_cnt and format
944 * the bde's.
James Smart34b02dc2008-08-24 21:49:55 -0400945 * When using SLI-3 the driver will try to fit all the BDEs into
946 * the IOCB. If it can't then the BDEs get added to a BPL as it
947 * does for SLI-2 mode.
dea31012005-04-17 16:05:31 -0500948 */
James Smart34b02dc2008-08-24 21:49:55 -0400949 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
dea31012005-04-17 16:05:31 -0500950 physaddr = sg_dma_address(sgel);
James Smart34b02dc2008-08-24 21:49:55 -0400951 if (phba->sli_rev == 3 &&
James Smarte2a0a9d2008-12-04 22:40:02 -0500952 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
James Smart0f65ff62010-02-26 14:14:23 -0500953 !(iocbq->iocb_flag & DSS_SECURITY_OP) &&
James Smart34b02dc2008-08-24 21:49:55 -0400954 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
955 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
956 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
957 data_bde->addrLow = putPaddrLow(physaddr);
958 data_bde->addrHigh = putPaddrHigh(physaddr);
959 data_bde++;
960 } else {
961 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
962 bpl->tus.f.bdeSize = sg_dma_len(sgel);
963 bpl->tus.w = le32_to_cpu(bpl->tus.w);
964 bpl->addrLow =
965 le32_to_cpu(putPaddrLow(physaddr));
966 bpl->addrHigh =
967 le32_to_cpu(putPaddrHigh(physaddr));
968 bpl++;
969 }
dea31012005-04-17 16:05:31 -0500970 }
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -0700971 }
dea31012005-04-17 16:05:31 -0500972
973 /*
974 * Finish initializing those IOCB fields that are dependent on the
James Smart34b02dc2008-08-24 21:49:55 -0400975 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
976 * explicitly reinitialized and for SLI-3 the extended bde count is
977 * explicitly reinitialized since all iocb memory resources are reused.
dea31012005-04-17 16:05:31 -0500978 */
James Smarte2a0a9d2008-12-04 22:40:02 -0500979 if (phba->sli_rev == 3 &&
James Smart0f65ff62010-02-26 14:14:23 -0500980 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
981 !(iocbq->iocb_flag & DSS_SECURITY_OP)) {
James Smart34b02dc2008-08-24 21:49:55 -0400982 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
983 /*
984 * The extended IOCB format can only fit 3 BDE or a BPL.
985 * This I/O has more than 3 BDE so the 1st data bde will
986 * be a BPL that is filled in here.
987 */
988 physaddr = lpfc_cmd->dma_handle;
989 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
990 data_bde->tus.f.bdeSize = (num_bde *
991 sizeof(struct ulp_bde64));
992 physaddr += (sizeof(struct fcp_cmnd) +
993 sizeof(struct fcp_rsp) +
994 (2 * sizeof(struct ulp_bde64)));
995 data_bde->addrHigh = putPaddrHigh(physaddr);
996 data_bde->addrLow = putPaddrLow(physaddr);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300997 /* ebde count includes the response bde and data bpl */
James Smart34b02dc2008-08-24 21:49:55 -0400998 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
999 } else {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001000 /* ebde count includes the response bde and data bdes */
James Smart34b02dc2008-08-24 21:49:55 -04001001 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1002 }
1003 } else {
1004 iocb_cmd->un.fcpi64.bdl.bdeSize =
1005 ((num_bde + 2) * sizeof(struct ulp_bde64));
James Smart0f65ff62010-02-26 14:14:23 -05001006 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
James Smart34b02dc2008-08-24 21:49:55 -04001007 }
James Smart09372822008-01-11 01:52:54 -05001008 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
James Smarte2a0a9d2008-12-04 22:40:02 -05001009
1010 /*
1011 * Due to difference in data length between DIF/non-DIF paths,
1012 * we need to set word 4 of IOCB here
1013 */
James Smarta257bf92009-04-06 18:48:10 -04001014 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001015 return 0;
1016}
1017
James Smartf9bb2da2011-10-10 21:34:11 -04001018#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001019
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -07001020/* Return BG_ERR_INIT if error injection is detected by Initiator */
James Smart9a6b09c2012-03-01 22:37:42 -05001021#define BG_ERR_INIT 0x1
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -07001022/* Return BG_ERR_TGT if error injection is detected by Target */
James Smart9a6b09c2012-03-01 22:37:42 -05001023#define BG_ERR_TGT 0x2
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -07001024/* Return BG_ERR_SWAP if swapping CSUM<-->CRC is required for error injection */
James Smart9a6b09c2012-03-01 22:37:42 -05001025#define BG_ERR_SWAP 0x10
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -07001026/**
1027 * Return BG_ERR_CHECK if disabling Guard/Ref/App checking is required for
1028 * error injection
1029 **/
James Smart9a6b09c2012-03-01 22:37:42 -05001030#define BG_ERR_CHECK 0x20
James Smartacd68592012-01-18 16:25:09 -05001031
1032/**
1033 * lpfc_bg_err_inject - Determine if we should inject an error
1034 * @phba: The Hba for which this call is being executed.
James Smartf9bb2da2011-10-10 21:34:11 -04001035 * @sc: The SCSI command to examine
1036 * @reftag: (out) BlockGuard reference tag for transmitted data
1037 * @apptag: (out) BlockGuard application tag for transmitted data
1038 * @new_guard (in) Value to replace CRC with if needed
1039 *
James Smart9a6b09c2012-03-01 22:37:42 -05001040 * Returns BG_ERR_* bit mask or 0 if request ignored
James Smartacd68592012-01-18 16:25:09 -05001041 **/
James Smartf9bb2da2011-10-10 21:34:11 -04001042static int
1043lpfc_bg_err_inject(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1044 uint32_t *reftag, uint16_t *apptag, uint32_t new_guard)
1045{
1046 struct scatterlist *sgpe; /* s/g prot entry */
James Smartc4908502019-01-28 11:14:28 -08001047 struct lpfc_io_buf *lpfc_cmd = NULL;
James Smartacd68592012-01-18 16:25:09 -05001048 struct scsi_dif_tuple *src = NULL;
James Smart4ac9b222012-03-01 22:38:29 -05001049 struct lpfc_nodelist *ndlp;
1050 struct lpfc_rport_data *rdata;
James Smartf9bb2da2011-10-10 21:34:11 -04001051 uint32_t op = scsi_get_prot_op(sc);
1052 uint32_t blksize;
1053 uint32_t numblks;
1054 sector_t lba;
1055 int rc = 0;
James Smartacd68592012-01-18 16:25:09 -05001056 int blockoff = 0;
James Smartf9bb2da2011-10-10 21:34:11 -04001057
1058 if (op == SCSI_PROT_NORMAL)
1059 return 0;
1060
James Smartacd68592012-01-18 16:25:09 -05001061 sgpe = scsi_prot_sglist(sc);
James Smartf9bb2da2011-10-10 21:34:11 -04001062 lba = scsi_get_lba(sc);
James Smart4ac9b222012-03-01 22:38:29 -05001063
1064 /* First check if we need to match the LBA */
James Smartf9bb2da2011-10-10 21:34:11 -04001065 if (phba->lpfc_injerr_lba != LPFC_INJERR_LBA_OFF) {
1066 blksize = lpfc_cmd_blksize(sc);
1067 numblks = (scsi_bufflen(sc) + blksize - 1) / blksize;
1068
1069 /* Make sure we have the right LBA if one is specified */
1070 if ((phba->lpfc_injerr_lba < lba) ||
1071 (phba->lpfc_injerr_lba >= (lba + numblks)))
1072 return 0;
James Smartacd68592012-01-18 16:25:09 -05001073 if (sgpe) {
1074 blockoff = phba->lpfc_injerr_lba - lba;
1075 numblks = sg_dma_len(sgpe) /
1076 sizeof(struct scsi_dif_tuple);
1077 if (numblks < blockoff)
1078 blockoff = numblks;
James Smartacd68592012-01-18 16:25:09 -05001079 }
James Smartf9bb2da2011-10-10 21:34:11 -04001080 }
1081
James Smart4ac9b222012-03-01 22:38:29 -05001082 /* Next check if we need to match the remote NPortID or WWPN */
James Smart1ba981f2014-02-20 09:56:45 -05001083 rdata = lpfc_rport_data_from_scsi_device(sc->device);
James Smart4ac9b222012-03-01 22:38:29 -05001084 if (rdata && rdata->pnode) {
1085 ndlp = rdata->pnode;
1086
1087 /* Make sure we have the right NPortID if one is specified */
1088 if (phba->lpfc_injerr_nportid &&
1089 (phba->lpfc_injerr_nportid != ndlp->nlp_DID))
1090 return 0;
1091
1092 /*
1093 * Make sure we have the right WWPN if one is specified.
1094 * wwn[0] should be a non-zero NAA in a good WWPN.
1095 */
1096 if (phba->lpfc_injerr_wwpn.u.wwn[0] &&
1097 (memcmp(&ndlp->nlp_portname, &phba->lpfc_injerr_wwpn,
1098 sizeof(struct lpfc_name)) != 0))
1099 return 0;
1100 }
1101
1102 /* Setup a ptr to the protection data if the SCSI host provides it */
1103 if (sgpe) {
1104 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
1105 src += blockoff;
James Smartc4908502019-01-28 11:14:28 -08001106 lpfc_cmd = (struct lpfc_io_buf *)sc->host_scribble;
James Smart4ac9b222012-03-01 22:38:29 -05001107 }
1108
James Smartf9bb2da2011-10-10 21:34:11 -04001109 /* Should we change the Reference Tag */
1110 if (reftag) {
James Smartacd68592012-01-18 16:25:09 -05001111 if (phba->lpfc_injerr_wref_cnt) {
1112 switch (op) {
1113 case SCSI_PROT_WRITE_PASS:
James Smart9a6b09c2012-03-01 22:37:42 -05001114 if (src) {
1115 /*
1116 * For WRITE_PASS, force the error
1117 * to be sent on the wire. It should
1118 * be detected by the Target.
1119 * If blockoff != 0 error will be
1120 * inserted in middle of the IO.
1121 */
James Smartf9bb2da2011-10-10 21:34:11 -04001122
James Smartacd68592012-01-18 16:25:09 -05001123 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1124 "9076 BLKGRD: Injecting reftag error: "
1125 "write lba x%lx + x%x oldrefTag x%x\n",
1126 (unsigned long)lba, blockoff,
James Smart9a6b09c2012-03-01 22:37:42 -05001127 be32_to_cpu(src->ref_tag));
James Smartacd68592012-01-18 16:25:09 -05001128
1129 /*
James Smart9a6b09c2012-03-01 22:37:42 -05001130 * Save the old ref_tag so we can
1131 * restore it on completion.
James Smartacd68592012-01-18 16:25:09 -05001132 */
James Smart9a6b09c2012-03-01 22:37:42 -05001133 if (lpfc_cmd) {
1134 lpfc_cmd->prot_data_type =
1135 LPFC_INJERR_REFTAG;
1136 lpfc_cmd->prot_data_segment =
1137 src;
1138 lpfc_cmd->prot_data =
1139 src->ref_tag;
1140 }
1141 src->ref_tag = cpu_to_be32(0xDEADBEEF);
James Smartacd68592012-01-18 16:25:09 -05001142 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001143 if (phba->lpfc_injerr_wref_cnt == 0) {
1144 phba->lpfc_injerr_nportid = 0;
1145 phba->lpfc_injerr_lba =
1146 LPFC_INJERR_LBA_OFF;
1147 memset(&phba->lpfc_injerr_wwpn,
1148 0, sizeof(struct lpfc_name));
1149 }
James Smart9a6b09c2012-03-01 22:37:42 -05001150 rc = BG_ERR_TGT | BG_ERR_CHECK;
1151
James Smartacd68592012-01-18 16:25:09 -05001152 break;
1153 }
Bart Van Asschecd05c152019-03-28 11:06:18 -07001154 /* fall through */
James Smart9a6b09c2012-03-01 22:37:42 -05001155 case SCSI_PROT_WRITE_INSERT:
1156 /*
1157 * For WRITE_INSERT, force the error
1158 * to be sent on the wire. It should be
1159 * detected by the Target.
1160 */
1161 /* DEADBEEF will be the reftag on the wire */
1162 *reftag = 0xDEADBEEF;
1163 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001164 if (phba->lpfc_injerr_wref_cnt == 0) {
1165 phba->lpfc_injerr_nportid = 0;
1166 phba->lpfc_injerr_lba =
1167 LPFC_INJERR_LBA_OFF;
1168 memset(&phba->lpfc_injerr_wwpn,
1169 0, sizeof(struct lpfc_name));
1170 }
James Smart9a6b09c2012-03-01 22:37:42 -05001171 rc = BG_ERR_TGT | BG_ERR_CHECK;
1172
1173 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1174 "9078 BLKGRD: Injecting reftag error: "
1175 "write lba x%lx\n", (unsigned long)lba);
1176 break;
James Smartacd68592012-01-18 16:25:09 -05001177 case SCSI_PROT_WRITE_STRIP:
1178 /*
1179 * For WRITE_STRIP and WRITE_PASS,
1180 * force the error on data
1181 * being copied from SLI-Host to SLI-Port.
1182 */
1183 *reftag = 0xDEADBEEF;
1184 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001185 if (phba->lpfc_injerr_wref_cnt == 0) {
1186 phba->lpfc_injerr_nportid = 0;
1187 phba->lpfc_injerr_lba =
1188 LPFC_INJERR_LBA_OFF;
1189 memset(&phba->lpfc_injerr_wwpn,
1190 0, sizeof(struct lpfc_name));
1191 }
James Smartacd68592012-01-18 16:25:09 -05001192 rc = BG_ERR_INIT;
1193
1194 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1195 "9077 BLKGRD: Injecting reftag error: "
1196 "write lba x%lx\n", (unsigned long)lba);
1197 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001198 }
James Smartacd68592012-01-18 16:25:09 -05001199 }
1200 if (phba->lpfc_injerr_rref_cnt) {
1201 switch (op) {
1202 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001203 case SCSI_PROT_READ_STRIP:
1204 case SCSI_PROT_READ_PASS:
1205 /*
1206 * For READ_STRIP and READ_PASS, force the
1207 * error on data being read off the wire. It
1208 * should force an IO error to the driver.
1209 */
James Smartf9bb2da2011-10-10 21:34:11 -04001210 *reftag = 0xDEADBEEF;
1211 phba->lpfc_injerr_rref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001212 if (phba->lpfc_injerr_rref_cnt == 0) {
1213 phba->lpfc_injerr_nportid = 0;
1214 phba->lpfc_injerr_lba =
1215 LPFC_INJERR_LBA_OFF;
1216 memset(&phba->lpfc_injerr_wwpn,
1217 0, sizeof(struct lpfc_name));
1218 }
James Smartacd68592012-01-18 16:25:09 -05001219 rc = BG_ERR_INIT;
James Smartf9bb2da2011-10-10 21:34:11 -04001220
1221 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smartacd68592012-01-18 16:25:09 -05001222 "9079 BLKGRD: Injecting reftag error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001223 "read lba x%lx\n", (unsigned long)lba);
James Smartacd68592012-01-18 16:25:09 -05001224 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001225 }
1226 }
1227 }
1228
1229 /* Should we change the Application Tag */
1230 if (apptag) {
James Smartacd68592012-01-18 16:25:09 -05001231 if (phba->lpfc_injerr_wapp_cnt) {
1232 switch (op) {
1233 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001234 if (src) {
James Smart9a6b09c2012-03-01 22:37:42 -05001235 /*
1236 * For WRITE_PASS, force the error
1237 * to be sent on the wire. It should
1238 * be detected by the Target.
1239 * If blockoff != 0 error will be
1240 * inserted in middle of the IO.
1241 */
1242
James Smartacd68592012-01-18 16:25:09 -05001243 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1244 "9080 BLKGRD: Injecting apptag error: "
1245 "write lba x%lx + x%x oldappTag x%x\n",
1246 (unsigned long)lba, blockoff,
James Smart9a6b09c2012-03-01 22:37:42 -05001247 be16_to_cpu(src->app_tag));
James Smartacd68592012-01-18 16:25:09 -05001248
1249 /*
James Smart9a6b09c2012-03-01 22:37:42 -05001250 * Save the old app_tag so we can
1251 * restore it on completion.
James Smartacd68592012-01-18 16:25:09 -05001252 */
James Smart9a6b09c2012-03-01 22:37:42 -05001253 if (lpfc_cmd) {
1254 lpfc_cmd->prot_data_type =
1255 LPFC_INJERR_APPTAG;
1256 lpfc_cmd->prot_data_segment =
1257 src;
1258 lpfc_cmd->prot_data =
1259 src->app_tag;
1260 }
1261 src->app_tag = cpu_to_be16(0xDEAD);
James Smartacd68592012-01-18 16:25:09 -05001262 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001263 if (phba->lpfc_injerr_wapp_cnt == 0) {
1264 phba->lpfc_injerr_nportid = 0;
1265 phba->lpfc_injerr_lba =
1266 LPFC_INJERR_LBA_OFF;
1267 memset(&phba->lpfc_injerr_wwpn,
1268 0, sizeof(struct lpfc_name));
1269 }
James Smart9a6b09c2012-03-01 22:37:42 -05001270 rc = BG_ERR_TGT | BG_ERR_CHECK;
James Smartacd68592012-01-18 16:25:09 -05001271 break;
1272 }
Bart Van Asschecd05c152019-03-28 11:06:18 -07001273 /* fall through */
James Smart9a6b09c2012-03-01 22:37:42 -05001274 case SCSI_PROT_WRITE_INSERT:
1275 /*
1276 * For WRITE_INSERT, force the
1277 * error to be sent on the wire. It should be
1278 * detected by the Target.
1279 */
1280 /* DEAD will be the apptag on the wire */
1281 *apptag = 0xDEAD;
1282 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001283 if (phba->lpfc_injerr_wapp_cnt == 0) {
1284 phba->lpfc_injerr_nportid = 0;
1285 phba->lpfc_injerr_lba =
1286 LPFC_INJERR_LBA_OFF;
1287 memset(&phba->lpfc_injerr_wwpn,
1288 0, sizeof(struct lpfc_name));
1289 }
James Smart9a6b09c2012-03-01 22:37:42 -05001290 rc = BG_ERR_TGT | BG_ERR_CHECK;
1291
1292 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1293 "0813 BLKGRD: Injecting apptag error: "
1294 "write lba x%lx\n", (unsigned long)lba);
1295 break;
James Smartacd68592012-01-18 16:25:09 -05001296 case SCSI_PROT_WRITE_STRIP:
1297 /*
1298 * For WRITE_STRIP and WRITE_PASS,
1299 * force the error on data
1300 * being copied from SLI-Host to SLI-Port.
1301 */
1302 *apptag = 0xDEAD;
1303 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001304 if (phba->lpfc_injerr_wapp_cnt == 0) {
1305 phba->lpfc_injerr_nportid = 0;
1306 phba->lpfc_injerr_lba =
1307 LPFC_INJERR_LBA_OFF;
1308 memset(&phba->lpfc_injerr_wwpn,
1309 0, sizeof(struct lpfc_name));
1310 }
James Smartacd68592012-01-18 16:25:09 -05001311 rc = BG_ERR_INIT;
1312
1313 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1314 "0812 BLKGRD: Injecting apptag error: "
1315 "write lba x%lx\n", (unsigned long)lba);
1316 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001317 }
James Smartacd68592012-01-18 16:25:09 -05001318 }
1319 if (phba->lpfc_injerr_rapp_cnt) {
1320 switch (op) {
1321 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001322 case SCSI_PROT_READ_STRIP:
1323 case SCSI_PROT_READ_PASS:
1324 /*
1325 * For READ_STRIP and READ_PASS, force the
1326 * error on data being read off the wire. It
1327 * should force an IO error to the driver.
1328 */
James Smartf9bb2da2011-10-10 21:34:11 -04001329 *apptag = 0xDEAD;
1330 phba->lpfc_injerr_rapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001331 if (phba->lpfc_injerr_rapp_cnt == 0) {
1332 phba->lpfc_injerr_nportid = 0;
1333 phba->lpfc_injerr_lba =
1334 LPFC_INJERR_LBA_OFF;
1335 memset(&phba->lpfc_injerr_wwpn,
1336 0, sizeof(struct lpfc_name));
1337 }
James Smartacd68592012-01-18 16:25:09 -05001338 rc = BG_ERR_INIT;
James Smartf9bb2da2011-10-10 21:34:11 -04001339
1340 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smartacd68592012-01-18 16:25:09 -05001341 "0814 BLKGRD: Injecting apptag error: "
1342 "read lba x%lx\n", (unsigned long)lba);
1343 break;
1344 }
1345 }
1346 }
1347
1348
1349 /* Should we change the Guard Tag */
1350 if (new_guard) {
1351 if (phba->lpfc_injerr_wgrd_cnt) {
1352 switch (op) {
1353 case SCSI_PROT_WRITE_PASS:
James Smart9a6b09c2012-03-01 22:37:42 -05001354 rc = BG_ERR_CHECK;
Bart Van Asschecd05c152019-03-28 11:06:18 -07001355 /* fall through */
James Smartacd68592012-01-18 16:25:09 -05001356
James Smartacd68592012-01-18 16:25:09 -05001357 case SCSI_PROT_WRITE_INSERT:
1358 /*
1359 * For WRITE_INSERT, force the
1360 * error to be sent on the wire. It should be
1361 * detected by the Target.
1362 */
1363 phba->lpfc_injerr_wgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001364 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1365 phba->lpfc_injerr_nportid = 0;
1366 phba->lpfc_injerr_lba =
1367 LPFC_INJERR_LBA_OFF;
1368 memset(&phba->lpfc_injerr_wwpn,
1369 0, sizeof(struct lpfc_name));
1370 }
James Smartacd68592012-01-18 16:25:09 -05001371
James Smart9a6b09c2012-03-01 22:37:42 -05001372 rc |= BG_ERR_TGT | BG_ERR_SWAP;
James Smartacd68592012-01-18 16:25:09 -05001373 /* Signals the caller to swap CRC->CSUM */
1374
1375 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1376 "0817 BLKGRD: Injecting guard error: "
1377 "write lba x%lx\n", (unsigned long)lba);
1378 break;
James Smart9a6b09c2012-03-01 22:37:42 -05001379 case SCSI_PROT_WRITE_STRIP:
1380 /*
1381 * For WRITE_STRIP and WRITE_PASS,
1382 * force the error on data
1383 * being copied from SLI-Host to SLI-Port.
1384 */
1385 phba->lpfc_injerr_wgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001386 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1387 phba->lpfc_injerr_nportid = 0;
1388 phba->lpfc_injerr_lba =
1389 LPFC_INJERR_LBA_OFF;
1390 memset(&phba->lpfc_injerr_wwpn,
1391 0, sizeof(struct lpfc_name));
1392 }
James Smart9a6b09c2012-03-01 22:37:42 -05001393
1394 rc = BG_ERR_INIT | BG_ERR_SWAP;
1395 /* Signals the caller to swap CRC->CSUM */
1396
1397 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1398 "0816 BLKGRD: Injecting guard error: "
1399 "write lba x%lx\n", (unsigned long)lba);
1400 break;
James Smartacd68592012-01-18 16:25:09 -05001401 }
1402 }
1403 if (phba->lpfc_injerr_rgrd_cnt) {
1404 switch (op) {
1405 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001406 case SCSI_PROT_READ_STRIP:
1407 case SCSI_PROT_READ_PASS:
1408 /*
1409 * For READ_STRIP and READ_PASS, force the
1410 * error on data being read off the wire. It
1411 * should force an IO error to the driver.
1412 */
James Smartacd68592012-01-18 16:25:09 -05001413 phba->lpfc_injerr_rgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001414 if (phba->lpfc_injerr_rgrd_cnt == 0) {
1415 phba->lpfc_injerr_nportid = 0;
1416 phba->lpfc_injerr_lba =
1417 LPFC_INJERR_LBA_OFF;
1418 memset(&phba->lpfc_injerr_wwpn,
1419 0, sizeof(struct lpfc_name));
1420 }
James Smartacd68592012-01-18 16:25:09 -05001421
James Smart9a6b09c2012-03-01 22:37:42 -05001422 rc = BG_ERR_INIT | BG_ERR_SWAP;
James Smartacd68592012-01-18 16:25:09 -05001423 /* Signals the caller to swap CRC->CSUM */
1424
1425 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1426 "0818 BLKGRD: Injecting guard error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001427 "read lba x%lx\n", (unsigned long)lba);
1428 }
1429 }
1430 }
1431
James Smartf9bb2da2011-10-10 21:34:11 -04001432 return rc;
1433}
1434#endif
1435
James Smartacd68592012-01-18 16:25:09 -05001436/**
1437 * lpfc_sc_to_bg_opcodes - Determine the BlockGuard opcodes to be used with
1438 * the specified SCSI command.
1439 * @phba: The Hba for which this call is being executed.
James Smart6c8eea52010-04-06 14:49:53 -04001440 * @sc: The SCSI command to examine
1441 * @txopt: (out) BlockGuard operation for transmitted data
1442 * @rxopt: (out) BlockGuard operation for received data
1443 *
1444 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1445 *
James Smartacd68592012-01-18 16:25:09 -05001446 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001447static int
James Smart6c8eea52010-04-06 14:49:53 -04001448lpfc_sc_to_bg_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1449 uint8_t *txop, uint8_t *rxop)
James Smarte2a0a9d2008-12-04 22:40:02 -05001450{
James Smart6c8eea52010-04-06 14:49:53 -04001451 uint8_t ret = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05001452
James Smart9c6aa9d2013-05-31 17:03:39 -04001453 if (lpfc_cmd_guard_csum(sc)) {
James Smarte2a0a9d2008-12-04 22:40:02 -05001454 switch (scsi_get_prot_op(sc)) {
1455 case SCSI_PROT_READ_INSERT:
1456 case SCSI_PROT_WRITE_STRIP:
James Smart6c8eea52010-04-06 14:49:53 -04001457 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001458 *txop = BG_OP_IN_CSUM_OUT_NODIF;
James Smarte2a0a9d2008-12-04 22:40:02 -05001459 break;
1460
1461 case SCSI_PROT_READ_STRIP:
1462 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001463 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001464 *txop = BG_OP_IN_NODIF_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001465 break;
1466
Martin K. Petersenc6af4042009-09-18 17:32:59 -04001467 case SCSI_PROT_READ_PASS:
1468 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001469 *rxop = BG_OP_IN_CRC_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001470 *txop = BG_OP_IN_CSUM_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001471 break;
1472
James Smarte2a0a9d2008-12-04 22:40:02 -05001473 case SCSI_PROT_NORMAL:
1474 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001475 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001476 "9063 BLKGRD: Bad op/guard:%d/IP combination\n",
1477 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001478 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001479 break;
1480
1481 }
James Smart7c56b9f2011-07-22 18:36:25 -04001482 } else {
James Smarte2a0a9d2008-12-04 22:40:02 -05001483 switch (scsi_get_prot_op(sc)) {
1484 case SCSI_PROT_READ_STRIP:
1485 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001486 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001487 *txop = BG_OP_IN_NODIF_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001488 break;
1489
1490 case SCSI_PROT_READ_PASS:
1491 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001492 *rxop = BG_OP_IN_CRC_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001493 *txop = BG_OP_IN_CRC_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001494 break;
1495
James Smarte2a0a9d2008-12-04 22:40:02 -05001496 case SCSI_PROT_READ_INSERT:
1497 case SCSI_PROT_WRITE_STRIP:
James Smart7c56b9f2011-07-22 18:36:25 -04001498 *rxop = BG_OP_IN_NODIF_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001499 *txop = BG_OP_IN_CRC_OUT_NODIF;
James Smart7c56b9f2011-07-22 18:36:25 -04001500 break;
1501
James Smarte2a0a9d2008-12-04 22:40:02 -05001502 case SCSI_PROT_NORMAL:
1503 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001504 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001505 "9075 BLKGRD: Bad op/guard:%d/CRC combination\n",
1506 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001507 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001508 break;
1509 }
James Smarte2a0a9d2008-12-04 22:40:02 -05001510 }
1511
James Smart6c8eea52010-04-06 14:49:53 -04001512 return ret;
James Smarte2a0a9d2008-12-04 22:40:02 -05001513}
1514
James Smartacd68592012-01-18 16:25:09 -05001515#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1516/**
1517 * lpfc_bg_err_opcodes - reDetermine the BlockGuard opcodes to be used with
1518 * the specified SCSI command in order to force a guard tag error.
1519 * @phba: The Hba for which this call is being executed.
1520 * @sc: The SCSI command to examine
1521 * @txopt: (out) BlockGuard operation for transmitted data
1522 * @rxopt: (out) BlockGuard operation for received data
1523 *
1524 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1525 *
1526 **/
1527static int
1528lpfc_bg_err_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1529 uint8_t *txop, uint8_t *rxop)
1530{
James Smartacd68592012-01-18 16:25:09 -05001531 uint8_t ret = 0;
1532
James Smart9c6aa9d2013-05-31 17:03:39 -04001533 if (lpfc_cmd_guard_csum(sc)) {
James Smartacd68592012-01-18 16:25:09 -05001534 switch (scsi_get_prot_op(sc)) {
1535 case SCSI_PROT_READ_INSERT:
1536 case SCSI_PROT_WRITE_STRIP:
James Smartacd68592012-01-18 16:25:09 -05001537 *rxop = BG_OP_IN_NODIF_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001538 *txop = BG_OP_IN_CRC_OUT_NODIF;
James Smartacd68592012-01-18 16:25:09 -05001539 break;
1540
1541 case SCSI_PROT_READ_STRIP:
1542 case SCSI_PROT_WRITE_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001543 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001544 *txop = BG_OP_IN_NODIF_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001545 break;
1546
1547 case SCSI_PROT_READ_PASS:
1548 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001549 *rxop = BG_OP_IN_CSUM_OUT_CRC;
James Smart9a6b09c2012-03-01 22:37:42 -05001550 *txop = BG_OP_IN_CRC_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001551 break;
1552
1553 case SCSI_PROT_NORMAL:
1554 default:
1555 break;
1556
1557 }
1558 } else {
1559 switch (scsi_get_prot_op(sc)) {
1560 case SCSI_PROT_READ_STRIP:
1561 case SCSI_PROT_WRITE_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001562 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001563 *txop = BG_OP_IN_NODIF_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001564 break;
1565
1566 case SCSI_PROT_READ_PASS:
1567 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001568 *rxop = BG_OP_IN_CSUM_OUT_CSUM;
James Smart9a6b09c2012-03-01 22:37:42 -05001569 *txop = BG_OP_IN_CSUM_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001570 break;
1571
1572 case SCSI_PROT_READ_INSERT:
1573 case SCSI_PROT_WRITE_STRIP:
James Smartacd68592012-01-18 16:25:09 -05001574 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001575 *txop = BG_OP_IN_CSUM_OUT_NODIF;
James Smartacd68592012-01-18 16:25:09 -05001576 break;
1577
1578 case SCSI_PROT_NORMAL:
1579 default:
1580 break;
1581 }
1582 }
1583
1584 return ret;
1585}
1586#endif
1587
1588/**
1589 * lpfc_bg_setup_bpl - Setup BlockGuard BPL with no protection data
1590 * @phba: The Hba for which this call is being executed.
1591 * @sc: pointer to scsi command we're working on
1592 * @bpl: pointer to buffer list for protection groups
1593 * @datacnt: number of segments of data that have been dma mapped
1594 *
1595 * This function sets up BPL buffer list for protection groups of
James Smarte2a0a9d2008-12-04 22:40:02 -05001596 * type LPFC_PG_TYPE_NO_DIF
1597 *
1598 * This is usually used when the HBA is instructed to generate
1599 * DIFs and insert them into data stream (or strip DIF from
1600 * incoming data stream)
1601 *
1602 * The buffer list consists of just one protection group described
1603 * below:
1604 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001605 * start of prot group --> | PDE_5 |
1606 * +-------------------------+
1607 * | PDE_6 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001608 * +-------------------------+
1609 * | Data BDE |
1610 * +-------------------------+
1611 * |more Data BDE's ... (opt)|
1612 * +-------------------------+
1613 *
James Smarte2a0a9d2008-12-04 22:40:02 -05001614 *
1615 * Note: Data s/g buffers have been dma mapped
James Smartacd68592012-01-18 16:25:09 -05001616 *
1617 * Returns the number of BDEs added to the BPL.
1618 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001619static int
1620lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1621 struct ulp_bde64 *bpl, int datasegcnt)
1622{
1623 struct scatterlist *sgde = NULL; /* s/g data entry */
James Smart6c8eea52010-04-06 14:49:53 -04001624 struct lpfc_pde5 *pde5 = NULL;
1625 struct lpfc_pde6 *pde6 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05001626 dma_addr_t physaddr;
James Smart6c8eea52010-04-06 14:49:53 -04001627 int i = 0, num_bde = 0, status;
James Smarte2a0a9d2008-12-04 22:40:02 -05001628 int datadir = sc->sc_data_direction;
James Smart0829a192012-05-09 21:18:12 -04001629#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001630 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04001631#endif
James Smartacd68592012-01-18 16:25:09 -05001632 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001633 uint32_t reftag;
James Smart6c8eea52010-04-06 14:49:53 -04001634 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05001635
James Smart6c8eea52010-04-06 14:49:53 -04001636 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1637 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05001638 goto out;
1639
James Smart6c8eea52010-04-06 14:49:53 -04001640 /* extract some info from the scsi command for pde*/
James Smartacd68592012-01-18 16:25:09 -05001641 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
James Smarte2a0a9d2008-12-04 22:40:02 -05001642
James Smartf9bb2da2011-10-10 21:34:11 -04001643#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05001644 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05001645 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05001646 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05001647 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05001648 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05001649 checking = 0;
1650 }
James Smartf9bb2da2011-10-10 21:34:11 -04001651#endif
1652
James Smart6c8eea52010-04-06 14:49:53 -04001653 /* setup PDE5 with what we have */
1654 pde5 = (struct lpfc_pde5 *) bpl;
1655 memset(pde5, 0, sizeof(struct lpfc_pde5));
1656 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05001657
James Smartbc739052010-08-04 16:11:18 -04001658 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04001659 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04001660 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04001661
James Smart6c8eea52010-04-06 14:49:53 -04001662 /* advance bpl and increment bde count */
1663 num_bde++;
1664 bpl++;
1665 pde6 = (struct lpfc_pde6 *) bpl;
1666
1667 /* setup PDE6 with the rest of the info */
1668 memset(pde6, 0, sizeof(struct lpfc_pde6));
1669 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
1670 bf_set(pde6_optx, pde6, txop);
1671 bf_set(pde6_oprx, pde6, rxop);
James Smarta6887e22013-04-17 20:18:07 -04001672
1673 /*
1674 * We only need to check the data on READs, for WRITEs
1675 * protection data is automatically generated, not checked.
1676 */
James Smart6c8eea52010-04-06 14:49:53 -04001677 if (datadir == DMA_FROM_DEVICE) {
James Smart9c6aa9d2013-05-31 17:03:39 -04001678 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
James Smarta6887e22013-04-17 20:18:07 -04001679 bf_set(pde6_ce, pde6, checking);
1680 else
1681 bf_set(pde6_ce, pde6, 0);
1682
James Smart9c6aa9d2013-05-31 17:03:39 -04001683 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04001684 bf_set(pde6_re, pde6, checking);
1685 else
1686 bf_set(pde6_re, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04001687 }
1688 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04001689 bf_set(pde6_ae, pde6, 0);
1690 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04001691
James Smartbc739052010-08-04 16:11:18 -04001692 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04001693 pde6->word0 = cpu_to_le32(pde6->word0);
1694 pde6->word1 = cpu_to_le32(pde6->word1);
1695 pde6->word2 = cpu_to_le32(pde6->word2);
1696
James Smart6c8eea52010-04-06 14:49:53 -04001697 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05001698 num_bde++;
1699 bpl++;
1700
1701 /* assumption: caller has already run dma_map_sg on command data */
1702 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
1703 physaddr = sg_dma_address(sgde);
1704 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
1705 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1706 bpl->tus.f.bdeSize = sg_dma_len(sgde);
1707 if (datadir == DMA_TO_DEVICE)
1708 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1709 else
1710 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1711 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1712 bpl++;
1713 num_bde++;
1714 }
1715
1716out:
1717 return num_bde;
1718}
1719
James Smartacd68592012-01-18 16:25:09 -05001720/**
1721 * lpfc_bg_setup_bpl_prot - Setup BlockGuard BPL with protection data
1722 * @phba: The Hba for which this call is being executed.
1723 * @sc: pointer to scsi command we're working on
1724 * @bpl: pointer to buffer list for protection groups
1725 * @datacnt: number of segments of data that have been dma mapped
1726 * @protcnt: number of segment of protection data that have been dma mapped
1727 *
1728 * This function sets up BPL buffer list for protection groups of
1729 * type LPFC_PG_TYPE_DIF
James Smarte2a0a9d2008-12-04 22:40:02 -05001730 *
1731 * This is usually used when DIFs are in their own buffers,
1732 * separate from the data. The HBA can then by instructed
1733 * to place the DIFs in the outgoing stream. For read operations,
1734 * The HBA could extract the DIFs and place it in DIF buffers.
1735 *
1736 * The buffer list for this type consists of one or more of the
1737 * protection groups described below:
1738 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001739 * start of first prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001740 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001741 * | PDE_6 |
1742 * +-------------------------+
1743 * | PDE_7 (Prot BDE) |
James Smarte2a0a9d2008-12-04 22:40:02 -05001744 * +-------------------------+
1745 * | Data BDE |
1746 * +-------------------------+
1747 * |more Data BDE's ... (opt)|
1748 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001749 * start of new prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001750 * +-------------------------+
1751 * | ... |
1752 * +-------------------------+
1753 *
James Smarte2a0a9d2008-12-04 22:40:02 -05001754 * Note: It is assumed that both data and protection s/g buffers have been
1755 * mapped for DMA
James Smartacd68592012-01-18 16:25:09 -05001756 *
1757 * Returns the number of BDEs added to the BPL.
1758 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001759static int
1760lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1761 struct ulp_bde64 *bpl, int datacnt, int protcnt)
1762{
1763 struct scatterlist *sgde = NULL; /* s/g data entry */
1764 struct scatterlist *sgpe = NULL; /* s/g prot entry */
James Smart6c8eea52010-04-06 14:49:53 -04001765 struct lpfc_pde5 *pde5 = NULL;
1766 struct lpfc_pde6 *pde6 = NULL;
James Smart7f860592011-03-11 16:05:52 -05001767 struct lpfc_pde7 *pde7 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05001768 dma_addr_t dataphysaddr, protphysaddr;
1769 unsigned short curr_data = 0, curr_prot = 0;
James Smart7f860592011-03-11 16:05:52 -05001770 unsigned int split_offset;
1771 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
James Smarte2a0a9d2008-12-04 22:40:02 -05001772 unsigned int protgrp_blks, protgrp_bytes;
1773 unsigned int remainder, subtotal;
James Smart6c8eea52010-04-06 14:49:53 -04001774 int status;
James Smarte2a0a9d2008-12-04 22:40:02 -05001775 int datadir = sc->sc_data_direction;
1776 unsigned char pgdone = 0, alldone = 0;
1777 unsigned blksize;
James Smart0829a192012-05-09 21:18:12 -04001778#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001779 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04001780#endif
James Smartacd68592012-01-18 16:25:09 -05001781 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001782 uint32_t reftag;
James Smart6c8eea52010-04-06 14:49:53 -04001783 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05001784 int num_bde = 0;
1785
1786 sgpe = scsi_prot_sglist(sc);
1787 sgde = scsi_sglist(sc);
1788
1789 if (!sgpe || !sgde) {
1790 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart32350662019-08-14 16:57:06 -07001791 "9020 Invalid s/g entry: data=x%px prot=x%px\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001792 sgpe, sgde);
1793 return 0;
1794 }
1795
James Smart6c8eea52010-04-06 14:49:53 -04001796 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1797 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05001798 goto out;
1799
James Smart6c8eea52010-04-06 14:49:53 -04001800 /* extract some info from the scsi command */
James Smarte2a0a9d2008-12-04 22:40:02 -05001801 blksize = lpfc_cmd_blksize(sc);
James Smartacd68592012-01-18 16:25:09 -05001802 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
James Smarte2a0a9d2008-12-04 22:40:02 -05001803
James Smartf9bb2da2011-10-10 21:34:11 -04001804#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05001805 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05001806 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05001807 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05001808 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05001809 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05001810 checking = 0;
1811 }
James Smartf9bb2da2011-10-10 21:34:11 -04001812#endif
1813
James Smarte2a0a9d2008-12-04 22:40:02 -05001814 split_offset = 0;
1815 do {
James Smart96f70772013-04-17 20:16:15 -04001816 /* Check to see if we ran out of space */
1817 if (num_bde >= (phba->cfg_total_seg_cnt - 2))
1818 return num_bde + 3;
1819
James Smart6c8eea52010-04-06 14:49:53 -04001820 /* setup PDE5 with what we have */
1821 pde5 = (struct lpfc_pde5 *) bpl;
1822 memset(pde5, 0, sizeof(struct lpfc_pde5));
1823 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05001824
James Smartbc739052010-08-04 16:11:18 -04001825 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04001826 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04001827 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04001828
James Smart6c8eea52010-04-06 14:49:53 -04001829 /* advance bpl and increment bde count */
1830 num_bde++;
1831 bpl++;
1832 pde6 = (struct lpfc_pde6 *) bpl;
James Smarte2a0a9d2008-12-04 22:40:02 -05001833
James Smart6c8eea52010-04-06 14:49:53 -04001834 /* setup PDE6 with the rest of the info */
1835 memset(pde6, 0, sizeof(struct lpfc_pde6));
1836 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
1837 bf_set(pde6_optx, pde6, txop);
1838 bf_set(pde6_oprx, pde6, rxop);
James Smarta6887e22013-04-17 20:18:07 -04001839
James Smart9c6aa9d2013-05-31 17:03:39 -04001840 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
James Smarta6887e22013-04-17 20:18:07 -04001841 bf_set(pde6_ce, pde6, checking);
1842 else
1843 bf_set(pde6_ce, pde6, 0);
1844
James Smart9c6aa9d2013-05-31 17:03:39 -04001845 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04001846 bf_set(pde6_re, pde6, checking);
1847 else
1848 bf_set(pde6_re, pde6, 0);
1849
James Smart6c8eea52010-04-06 14:49:53 -04001850 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04001851 bf_set(pde6_ae, pde6, 0);
1852 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04001853
James Smartbc739052010-08-04 16:11:18 -04001854 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04001855 pde6->word0 = cpu_to_le32(pde6->word0);
1856 pde6->word1 = cpu_to_le32(pde6->word1);
1857 pde6->word2 = cpu_to_le32(pde6->word2);
1858
James Smart6c8eea52010-04-06 14:49:53 -04001859 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05001860 num_bde++;
1861 bpl++;
1862
1863 /* setup the first BDE that points to protection buffer */
James Smart7f860592011-03-11 16:05:52 -05001864 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
1865 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
James Smarte2a0a9d2008-12-04 22:40:02 -05001866
James Smarte2a0a9d2008-12-04 22:40:02 -05001867 /* must be integer multiple of the DIF block length */
1868 BUG_ON(protgroup_len % 8);
1869
James Smart7f860592011-03-11 16:05:52 -05001870 pde7 = (struct lpfc_pde7 *) bpl;
1871 memset(pde7, 0, sizeof(struct lpfc_pde7));
1872 bf_set(pde7_type, pde7, LPFC_PDE7_DESCRIPTOR);
1873
James Smart7c56b9f2011-07-22 18:36:25 -04001874 pde7->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
1875 pde7->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
James Smart7f860592011-03-11 16:05:52 -05001876
James Smarte2a0a9d2008-12-04 22:40:02 -05001877 protgrp_blks = protgroup_len / 8;
1878 protgrp_bytes = protgrp_blks * blksize;
1879
James Smart7f860592011-03-11 16:05:52 -05001880 /* check if this pde is crossing the 4K boundary; if so split */
1881 if ((pde7->addrLow & 0xfff) + protgroup_len > 0x1000) {
1882 protgroup_remainder = 0x1000 - (pde7->addrLow & 0xfff);
1883 protgroup_offset += protgroup_remainder;
1884 protgrp_blks = protgroup_remainder / 8;
James Smart7c56b9f2011-07-22 18:36:25 -04001885 protgrp_bytes = protgrp_blks * blksize;
James Smart7f860592011-03-11 16:05:52 -05001886 } else {
1887 protgroup_offset = 0;
1888 curr_prot++;
1889 }
James Smarte2a0a9d2008-12-04 22:40:02 -05001890
James Smarte2a0a9d2008-12-04 22:40:02 -05001891 num_bde++;
1892
1893 /* setup BDE's for data blocks associated with DIF data */
1894 pgdone = 0;
1895 subtotal = 0; /* total bytes processed for current prot grp */
1896 while (!pgdone) {
James Smart96f70772013-04-17 20:16:15 -04001897 /* Check to see if we ran out of space */
1898 if (num_bde >= phba->cfg_total_seg_cnt)
1899 return num_bde + 1;
1900
James Smarte2a0a9d2008-12-04 22:40:02 -05001901 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -04001902 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1903 "9065 BLKGRD:%s Invalid data segment\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001904 __func__);
1905 return 0;
1906 }
1907 bpl++;
1908 dataphysaddr = sg_dma_address(sgde) + split_offset;
1909 bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
1910 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
1911
1912 remainder = sg_dma_len(sgde) - split_offset;
1913
1914 if ((subtotal + remainder) <= protgrp_bytes) {
1915 /* we can use this whole buffer */
1916 bpl->tus.f.bdeSize = remainder;
1917 split_offset = 0;
1918
1919 if ((subtotal + remainder) == protgrp_bytes)
1920 pgdone = 1;
1921 } else {
1922 /* must split this buffer with next prot grp */
1923 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
1924 split_offset += bpl->tus.f.bdeSize;
1925 }
1926
1927 subtotal += bpl->tus.f.bdeSize;
1928
1929 if (datadir == DMA_TO_DEVICE)
1930 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1931 else
1932 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1933 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1934
1935 num_bde++;
1936 curr_data++;
1937
1938 if (split_offset)
1939 break;
1940
1941 /* Move to the next s/g segment if possible */
1942 sgde = sg_next(sgde);
James Smart6c8eea52010-04-06 14:49:53 -04001943
James Smarte2a0a9d2008-12-04 22:40:02 -05001944 }
1945
James Smart7f860592011-03-11 16:05:52 -05001946 if (protgroup_offset) {
1947 /* update the reference tag */
1948 reftag += protgrp_blks;
1949 bpl++;
1950 continue;
1951 }
1952
James Smarte2a0a9d2008-12-04 22:40:02 -05001953 /* are we done ? */
1954 if (curr_prot == protcnt) {
1955 alldone = 1;
1956 } else if (curr_prot < protcnt) {
1957 /* advance to next prot buffer */
1958 sgpe = sg_next(sgpe);
1959 bpl++;
1960
1961 /* update the reference tag */
1962 reftag += protgrp_blks;
1963 } else {
1964 /* if we're here, we have a bug */
James Smart6a9c52c2009-10-02 15:16:51 -04001965 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1966 "9054 BLKGRD: bug in %s\n", __func__);
James Smarte2a0a9d2008-12-04 22:40:02 -05001967 }
1968
1969 } while (!alldone);
James Smarte2a0a9d2008-12-04 22:40:02 -05001970out:
1971
James Smarte2a0a9d2008-12-04 22:40:02 -05001972 return num_bde;
1973}
James Smart7f860592011-03-11 16:05:52 -05001974
James Smartacd68592012-01-18 16:25:09 -05001975/**
1976 * lpfc_bg_setup_sgl - Setup BlockGuard SGL with no protection data
1977 * @phba: The Hba for which this call is being executed.
1978 * @sc: pointer to scsi command we're working on
1979 * @sgl: pointer to buffer list for protection groups
1980 * @datacnt: number of segments of data that have been dma mapped
1981 *
1982 * This function sets up SGL buffer list for protection groups of
1983 * type LPFC_PG_TYPE_NO_DIF
1984 *
1985 * This is usually used when the HBA is instructed to generate
1986 * DIFs and insert them into data stream (or strip DIF from
1987 * incoming data stream)
1988 *
1989 * The buffer list consists of just one protection group described
1990 * below:
1991 * +-------------------------+
1992 * start of prot group --> | DI_SEED |
1993 * +-------------------------+
1994 * | Data SGE |
1995 * +-------------------------+
1996 * |more Data SGE's ... (opt)|
1997 * +-------------------------+
1998 *
1999 *
2000 * Note: Data s/g buffers have been dma mapped
2001 *
2002 * Returns the number of SGEs added to the SGL.
2003 **/
2004static int
2005lpfc_bg_setup_sgl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
James Smartd79c9e92019-08-14 16:57:09 -07002006 struct sli4_sge *sgl, int datasegcnt,
2007 struct lpfc_io_buf *lpfc_cmd)
James Smartacd68592012-01-18 16:25:09 -05002008{
2009 struct scatterlist *sgde = NULL; /* s/g data entry */
2010 struct sli4_sge_diseed *diseed = NULL;
2011 dma_addr_t physaddr;
2012 int i = 0, num_sge = 0, status;
James Smartacd68592012-01-18 16:25:09 -05002013 uint32_t reftag;
James Smartacd68592012-01-18 16:25:09 -05002014 uint8_t txop, rxop;
James Smart0829a192012-05-09 21:18:12 -04002015#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002016 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002017#endif
James Smartacd68592012-01-18 16:25:09 -05002018 uint32_t checking = 1;
2019 uint32_t dma_len;
2020 uint32_t dma_offset = 0;
James Smartd79c9e92019-08-14 16:57:09 -07002021 struct sli4_hybrid_sgl *sgl_xtra = NULL;
2022 int j;
2023 bool lsp_just_set = false;
James Smartacd68592012-01-18 16:25:09 -05002024
2025 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2026 if (status)
2027 goto out;
2028
2029 /* extract some info from the scsi command for pde*/
James Smartacd68592012-01-18 16:25:09 -05002030 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2031
2032#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002033 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002034 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002035 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002036 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002037 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002038 checking = 0;
2039 }
2040#endif
2041
2042 /* setup DISEED with what we have */
2043 diseed = (struct sli4_sge_diseed *) sgl;
2044 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2045 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2046
2047 /* Endianness conversion if necessary */
2048 diseed->ref_tag = cpu_to_le32(reftag);
2049 diseed->ref_tag_tran = diseed->ref_tag;
2050
James Smarta6887e22013-04-17 20:18:07 -04002051 /*
2052 * We only need to check the data on READs, for WRITEs
2053 * protection data is automatically generated, not checked.
2054 */
2055 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
James Smart9c6aa9d2013-05-31 17:03:39 -04002056 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
James Smarta6887e22013-04-17 20:18:07 -04002057 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2058 else
2059 bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2060
James Smart9c6aa9d2013-05-31 17:03:39 -04002061 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04002062 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2063 else
2064 bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2065 }
2066
James Smartacd68592012-01-18 16:25:09 -05002067 /* setup DISEED with the rest of the info */
2068 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2069 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002070
James Smartacd68592012-01-18 16:25:09 -05002071 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2072 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2073
2074 /* Endianness conversion if necessary for DISEED */
2075 diseed->word2 = cpu_to_le32(diseed->word2);
2076 diseed->word3 = cpu_to_le32(diseed->word3);
2077
2078 /* advance bpl and increment sge count */
2079 num_sge++;
2080 sgl++;
2081
2082 /* assumption: caller has already run dma_map_sg on command data */
James Smartd79c9e92019-08-14 16:57:09 -07002083 sgde = scsi_sglist(sc);
2084 j = 3;
2085 for (i = 0; i < datasegcnt; i++) {
2086 /* clear it */
2087 sgl->word2 = 0;
James Smartacd68592012-01-18 16:25:09 -05002088
James Smartd79c9e92019-08-14 16:57:09 -07002089 /* do we need to expand the segment */
2090 if (!lsp_just_set && !((j + 1) % phba->border_sge_num) &&
2091 ((datasegcnt - 1) != i)) {
2092 /* set LSP type */
2093 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_LSP);
James Smartacd68592012-01-18 16:25:09 -05002094
James Smartd79c9e92019-08-14 16:57:09 -07002095 sgl_xtra = lpfc_get_sgl_per_hdwq(phba, lpfc_cmd);
2096
2097 if (unlikely(!sgl_xtra)) {
2098 lpfc_cmd->seg_cnt = 0;
2099 return 0;
2100 }
2101 sgl->addr_lo = cpu_to_le32(putPaddrLow(
2102 sgl_xtra->dma_phys_sgl));
2103 sgl->addr_hi = cpu_to_le32(putPaddrHigh(
2104 sgl_xtra->dma_phys_sgl));
2105
2106 } else {
2107 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2108 }
2109
2110 if (!(bf_get(lpfc_sli4_sge_type, sgl) & LPFC_SGE_TYPE_LSP)) {
2111 if ((datasegcnt - 1) == i)
2112 bf_set(lpfc_sli4_sge_last, sgl, 1);
2113 physaddr = sg_dma_address(sgde);
2114 dma_len = sg_dma_len(sgde);
2115 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
2116 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
2117
2118 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2119 sgl->word2 = cpu_to_le32(sgl->word2);
2120 sgl->sge_len = cpu_to_le32(dma_len);
2121
2122 dma_offset += dma_len;
2123 sgde = sg_next(sgde);
2124
2125 sgl++;
2126 num_sge++;
2127 lsp_just_set = false;
2128
2129 } else {
2130 sgl->word2 = cpu_to_le32(sgl->word2);
2131 sgl->sge_len = cpu_to_le32(phba->cfg_sg_dma_buf_size);
2132
2133 sgl = (struct sli4_sge *)sgl_xtra->dma_sgl;
2134 i = i - 1;
2135
2136 lsp_just_set = true;
2137 }
2138
2139 j++;
2140
James Smartacd68592012-01-18 16:25:09 -05002141 }
2142
2143out:
2144 return num_sge;
2145}
2146
2147/**
2148 * lpfc_bg_setup_sgl_prot - Setup BlockGuard SGL with protection data
2149 * @phba: The Hba for which this call is being executed.
2150 * @sc: pointer to scsi command we're working on
2151 * @sgl: pointer to buffer list for protection groups
2152 * @datacnt: number of segments of data that have been dma mapped
2153 * @protcnt: number of segment of protection data that have been dma mapped
2154 *
2155 * This function sets up SGL buffer list for protection groups of
2156 * type LPFC_PG_TYPE_DIF
2157 *
2158 * This is usually used when DIFs are in their own buffers,
2159 * separate from the data. The HBA can then by instructed
2160 * to place the DIFs in the outgoing stream. For read operations,
2161 * The HBA could extract the DIFs and place it in DIF buffers.
2162 *
2163 * The buffer list for this type consists of one or more of the
2164 * protection groups described below:
2165 * +-------------------------+
2166 * start of first prot group --> | DISEED |
2167 * +-------------------------+
2168 * | DIF (Prot SGE) |
2169 * +-------------------------+
2170 * | Data SGE |
2171 * +-------------------------+
2172 * |more Data SGE's ... (opt)|
2173 * +-------------------------+
2174 * start of new prot group --> | DISEED |
2175 * +-------------------------+
2176 * | ... |
2177 * +-------------------------+
2178 *
2179 * Note: It is assumed that both data and protection s/g buffers have been
2180 * mapped for DMA
2181 *
2182 * Returns the number of SGEs added to the SGL.
2183 **/
2184static int
2185lpfc_bg_setup_sgl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
James Smartd79c9e92019-08-14 16:57:09 -07002186 struct sli4_sge *sgl, int datacnt, int protcnt,
2187 struct lpfc_io_buf *lpfc_cmd)
James Smartacd68592012-01-18 16:25:09 -05002188{
2189 struct scatterlist *sgde = NULL; /* s/g data entry */
2190 struct scatterlist *sgpe = NULL; /* s/g prot entry */
2191 struct sli4_sge_diseed *diseed = NULL;
2192 dma_addr_t dataphysaddr, protphysaddr;
2193 unsigned short curr_data = 0, curr_prot = 0;
2194 unsigned int split_offset;
2195 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
2196 unsigned int protgrp_blks, protgrp_bytes;
2197 unsigned int remainder, subtotal;
2198 int status;
2199 unsigned char pgdone = 0, alldone = 0;
2200 unsigned blksize;
2201 uint32_t reftag;
2202 uint8_t txop, rxop;
2203 uint32_t dma_len;
James Smart0829a192012-05-09 21:18:12 -04002204#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002205 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002206#endif
James Smartacd68592012-01-18 16:25:09 -05002207 uint32_t checking = 1;
2208 uint32_t dma_offset = 0;
James Smartd79c9e92019-08-14 16:57:09 -07002209 int num_sge = 0, j = 2;
2210 struct sli4_hybrid_sgl *sgl_xtra = NULL;
James Smartacd68592012-01-18 16:25:09 -05002211
2212 sgpe = scsi_prot_sglist(sc);
2213 sgde = scsi_sglist(sc);
2214
2215 if (!sgpe || !sgde) {
2216 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart32350662019-08-14 16:57:06 -07002217 "9082 Invalid s/g entry: data=x%px prot=x%px\n",
James Smartacd68592012-01-18 16:25:09 -05002218 sgpe, sgde);
2219 return 0;
2220 }
2221
2222 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2223 if (status)
2224 goto out;
2225
2226 /* extract some info from the scsi command */
2227 blksize = lpfc_cmd_blksize(sc);
2228 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2229
2230#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002231 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002232 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002233 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002234 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002235 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002236 checking = 0;
2237 }
2238#endif
2239
2240 split_offset = 0;
2241 do {
James Smart96f70772013-04-17 20:16:15 -04002242 /* Check to see if we ran out of space */
James Smartd79c9e92019-08-14 16:57:09 -07002243 if ((num_sge >= (phba->cfg_total_seg_cnt - 2)) &&
2244 !(phba->cfg_xpsgl))
James Smart96f70772013-04-17 20:16:15 -04002245 return num_sge + 3;
2246
James Smartd79c9e92019-08-14 16:57:09 -07002247 /* DISEED and DIF have to be together */
2248 if (!((j + 1) % phba->border_sge_num) ||
2249 !((j + 2) % phba->border_sge_num) ||
2250 !((j + 3) % phba->border_sge_num)) {
2251 sgl->word2 = 0;
2252
2253 /* set LSP type */
2254 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_LSP);
2255
2256 sgl_xtra = lpfc_get_sgl_per_hdwq(phba, lpfc_cmd);
2257
2258 if (unlikely(!sgl_xtra)) {
2259 goto out;
2260 } else {
2261 sgl->addr_lo = cpu_to_le32(putPaddrLow(
2262 sgl_xtra->dma_phys_sgl));
2263 sgl->addr_hi = cpu_to_le32(putPaddrHigh(
2264 sgl_xtra->dma_phys_sgl));
2265 }
2266
2267 sgl->word2 = cpu_to_le32(sgl->word2);
2268 sgl->sge_len = cpu_to_le32(phba->cfg_sg_dma_buf_size);
2269
2270 sgl = (struct sli4_sge *)sgl_xtra->dma_sgl;
2271 j = 0;
2272 }
2273
James Smartacd68592012-01-18 16:25:09 -05002274 /* setup DISEED with what we have */
2275 diseed = (struct sli4_sge_diseed *) sgl;
2276 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2277 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2278
2279 /* Endianness conversion if necessary */
2280 diseed->ref_tag = cpu_to_le32(reftag);
2281 diseed->ref_tag_tran = diseed->ref_tag;
2282
James Smart9c6aa9d2013-05-31 17:03:39 -04002283 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD)) {
James Smarta6887e22013-04-17 20:18:07 -04002284 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2285
2286 } else {
2287 bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2288 /*
2289 * When in this mode, the hardware will replace
2290 * the guard tag from the host with a
2291 * newly generated good CRC for the wire.
2292 * Switch to raw mode here to avoid this
2293 * behavior. What the host sends gets put on the wire.
2294 */
2295 if (txop == BG_OP_IN_CRC_OUT_CRC) {
2296 txop = BG_OP_RAW_MODE;
2297 rxop = BG_OP_RAW_MODE;
2298 }
2299 }
2300
2301
James Smart9c6aa9d2013-05-31 17:03:39 -04002302 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04002303 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2304 else
2305 bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2306
James Smartacd68592012-01-18 16:25:09 -05002307 /* setup DISEED with the rest of the info */
2308 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2309 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002310
James Smartacd68592012-01-18 16:25:09 -05002311 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2312 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2313
2314 /* Endianness conversion if necessary for DISEED */
2315 diseed->word2 = cpu_to_le32(diseed->word2);
2316 diseed->word3 = cpu_to_le32(diseed->word3);
2317
2318 /* advance sgl and increment bde count */
2319 num_sge++;
James Smartd79c9e92019-08-14 16:57:09 -07002320
James Smartacd68592012-01-18 16:25:09 -05002321 sgl++;
James Smartd79c9e92019-08-14 16:57:09 -07002322 j++;
James Smartacd68592012-01-18 16:25:09 -05002323
2324 /* setup the first BDE that points to protection buffer */
2325 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2326 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
2327
2328 /* must be integer multiple of the DIF block length */
2329 BUG_ON(protgroup_len % 8);
2330
2331 /* Now setup DIF SGE */
2332 sgl->word2 = 0;
2333 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DIF);
2334 sgl->addr_hi = le32_to_cpu(putPaddrHigh(protphysaddr));
2335 sgl->addr_lo = le32_to_cpu(putPaddrLow(protphysaddr));
2336 sgl->word2 = cpu_to_le32(sgl->word2);
James Smartd79c9e92019-08-14 16:57:09 -07002337 sgl->sge_len = 0;
James Smartacd68592012-01-18 16:25:09 -05002338
2339 protgrp_blks = protgroup_len / 8;
2340 protgrp_bytes = protgrp_blks * blksize;
2341
2342 /* check if DIF SGE is crossing the 4K boundary; if so split */
2343 if ((sgl->addr_lo & 0xfff) + protgroup_len > 0x1000) {
2344 protgroup_remainder = 0x1000 - (sgl->addr_lo & 0xfff);
2345 protgroup_offset += protgroup_remainder;
2346 protgrp_blks = protgroup_remainder / 8;
2347 protgrp_bytes = protgrp_blks * blksize;
2348 } else {
2349 protgroup_offset = 0;
2350 curr_prot++;
2351 }
2352
2353 num_sge++;
2354
2355 /* setup SGE's for data blocks associated with DIF data */
2356 pgdone = 0;
2357 subtotal = 0; /* total bytes processed for current prot grp */
James Smartd79c9e92019-08-14 16:57:09 -07002358
2359 sgl++;
2360 j++;
2361
James Smartacd68592012-01-18 16:25:09 -05002362 while (!pgdone) {
James Smart96f70772013-04-17 20:16:15 -04002363 /* Check to see if we ran out of space */
James Smartd79c9e92019-08-14 16:57:09 -07002364 if ((num_sge >= phba->cfg_total_seg_cnt) &&
2365 !phba->cfg_xpsgl)
James Smart96f70772013-04-17 20:16:15 -04002366 return num_sge + 1;
2367
James Smartacd68592012-01-18 16:25:09 -05002368 if (!sgde) {
2369 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2370 "9086 BLKGRD:%s Invalid data segment\n",
2371 __func__);
2372 return 0;
2373 }
James Smartacd68592012-01-18 16:25:09 -05002374
James Smartd79c9e92019-08-14 16:57:09 -07002375 if (!((j + 1) % phba->border_sge_num)) {
2376 sgl->word2 = 0;
James Smartacd68592012-01-18 16:25:09 -05002377
James Smartd79c9e92019-08-14 16:57:09 -07002378 /* set LSP type */
2379 bf_set(lpfc_sli4_sge_type, sgl,
2380 LPFC_SGE_TYPE_LSP);
James Smartacd68592012-01-18 16:25:09 -05002381
James Smartd79c9e92019-08-14 16:57:09 -07002382 sgl_xtra = lpfc_get_sgl_per_hdwq(phba,
2383 lpfc_cmd);
2384
2385 if (unlikely(!sgl_xtra)) {
2386 goto out;
2387 } else {
2388 sgl->addr_lo = cpu_to_le32(
2389 putPaddrLow(sgl_xtra->dma_phys_sgl));
2390 sgl->addr_hi = cpu_to_le32(
2391 putPaddrHigh(sgl_xtra->dma_phys_sgl));
2392 }
2393
2394 sgl->word2 = cpu_to_le32(sgl->word2);
2395 sgl->sge_len = cpu_to_le32(
2396 phba->cfg_sg_dma_buf_size);
2397
2398 sgl = (struct sli4_sge *)sgl_xtra->dma_sgl;
James Smartacd68592012-01-18 16:25:09 -05002399 } else {
James Smartd79c9e92019-08-14 16:57:09 -07002400 dataphysaddr = sg_dma_address(sgde) +
2401 split_offset;
2402
2403 remainder = sg_dma_len(sgde) - split_offset;
2404
2405 if ((subtotal + remainder) <= protgrp_bytes) {
2406 /* we can use this whole buffer */
2407 dma_len = remainder;
2408 split_offset = 0;
2409
2410 if ((subtotal + remainder) ==
2411 protgrp_bytes)
2412 pgdone = 1;
2413 } else {
2414 /* must split this buffer with next
2415 * prot grp
2416 */
2417 dma_len = protgrp_bytes - subtotal;
2418 split_offset += dma_len;
2419 }
2420
2421 subtotal += dma_len;
2422
2423 sgl->word2 = 0;
2424 sgl->addr_lo = cpu_to_le32(putPaddrLow(
2425 dataphysaddr));
2426 sgl->addr_hi = cpu_to_le32(putPaddrHigh(
2427 dataphysaddr));
2428 bf_set(lpfc_sli4_sge_last, sgl, 0);
2429 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2430 bf_set(lpfc_sli4_sge_type, sgl,
2431 LPFC_SGE_TYPE_DATA);
2432
2433 sgl->sge_len = cpu_to_le32(dma_len);
2434 dma_offset += dma_len;
2435
2436 num_sge++;
2437 curr_data++;
2438
2439 if (split_offset) {
2440 sgl++;
2441 j++;
2442 break;
2443 }
2444
2445 /* Move to the next s/g segment if possible */
2446 sgde = sg_next(sgde);
2447
2448 sgl++;
James Smartacd68592012-01-18 16:25:09 -05002449 }
2450
James Smartd79c9e92019-08-14 16:57:09 -07002451 j++;
James Smartacd68592012-01-18 16:25:09 -05002452 }
2453
2454 if (protgroup_offset) {
2455 /* update the reference tag */
2456 reftag += protgrp_blks;
James Smartacd68592012-01-18 16:25:09 -05002457 continue;
2458 }
2459
2460 /* are we done ? */
2461 if (curr_prot == protcnt) {
James Smartd79c9e92019-08-14 16:57:09 -07002462 /* mark the last SGL */
2463 sgl--;
James Smartacd68592012-01-18 16:25:09 -05002464 bf_set(lpfc_sli4_sge_last, sgl, 1);
2465 alldone = 1;
2466 } else if (curr_prot < protcnt) {
2467 /* advance to next prot buffer */
2468 sgpe = sg_next(sgpe);
James Smartacd68592012-01-18 16:25:09 -05002469
2470 /* update the reference tag */
2471 reftag += protgrp_blks;
2472 } else {
2473 /* if we're here, we have a bug */
2474 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2475 "9085 BLKGRD: bug in %s\n", __func__);
2476 }
2477
2478 } while (!alldone);
2479
2480out:
2481
2482 return num_sge;
2483}
2484
2485/**
2486 * lpfc_prot_group_type - Get prtotection group type of SCSI command
2487 * @phba: The Hba for which this call is being executed.
2488 * @sc: pointer to scsi command we're working on
2489 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002490 * Given a SCSI command that supports DIF, determine composition of protection
2491 * groups involved in setting up buffer lists
2492 *
James Smartacd68592012-01-18 16:25:09 -05002493 * Returns: Protection group type (with or without DIF)
2494 *
2495 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002496static int
2497lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
2498{
2499 int ret = LPFC_PG_TYPE_INVALID;
2500 unsigned char op = scsi_get_prot_op(sc);
2501
2502 switch (op) {
2503 case SCSI_PROT_READ_STRIP:
2504 case SCSI_PROT_WRITE_INSERT:
2505 ret = LPFC_PG_TYPE_NO_DIF;
2506 break;
2507 case SCSI_PROT_READ_INSERT:
2508 case SCSI_PROT_WRITE_STRIP:
2509 case SCSI_PROT_READ_PASS:
2510 case SCSI_PROT_WRITE_PASS:
James Smarte2a0a9d2008-12-04 22:40:02 -05002511 ret = LPFC_PG_TYPE_DIF_BUF;
2512 break;
2513 default:
James Smart9c6aa9d2013-05-31 17:03:39 -04002514 if (phba)
2515 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2516 "9021 Unsupported protection op:%d\n",
2517 op);
James Smarte2a0a9d2008-12-04 22:40:02 -05002518 break;
2519 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002520 return ret;
2521}
2522
James Smartacd68592012-01-18 16:25:09 -05002523/**
James Smarta6887e22013-04-17 20:18:07 -04002524 * lpfc_bg_scsi_adjust_dl - Adjust SCSI data length for BlockGuard
2525 * @phba: The Hba for which this call is being executed.
2526 * @lpfc_cmd: The scsi buffer which is going to be adjusted.
2527 *
2528 * Adjust the data length to account for how much data
2529 * is actually on the wire.
2530 *
2531 * returns the adjusted data length
2532 **/
2533static int
2534lpfc_bg_scsi_adjust_dl(struct lpfc_hba *phba,
James Smartc4908502019-01-28 11:14:28 -08002535 struct lpfc_io_buf *lpfc_cmd)
James Smarta6887e22013-04-17 20:18:07 -04002536{
2537 struct scsi_cmnd *sc = lpfc_cmd->pCmd;
2538 int fcpdl;
2539
2540 fcpdl = scsi_bufflen(sc);
2541
2542 /* Check if there is protection data on the wire */
2543 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
James Smart9c6aa9d2013-05-31 17:03:39 -04002544 /* Read check for protection data */
James Smarta6887e22013-04-17 20:18:07 -04002545 if (scsi_get_prot_op(sc) == SCSI_PROT_READ_INSERT)
2546 return fcpdl;
2547
2548 } else {
James Smart9c6aa9d2013-05-31 17:03:39 -04002549 /* Write check for protection data */
James Smarta6887e22013-04-17 20:18:07 -04002550 if (scsi_get_prot_op(sc) == SCSI_PROT_WRITE_STRIP)
2551 return fcpdl;
2552 }
2553
2554 /*
2555 * If we are in DIF Type 1 mode every data block has a 8 byte
James Smart9c6aa9d2013-05-31 17:03:39 -04002556 * DIF (trailer) attached to it. Must ajust FCP data length
2557 * to account for the protection data.
James Smarta6887e22013-04-17 20:18:07 -04002558 */
James Smart9c6aa9d2013-05-31 17:03:39 -04002559 fcpdl += (fcpdl / lpfc_cmd_blksize(sc)) * 8;
James Smarta6887e22013-04-17 20:18:07 -04002560
2561 return fcpdl;
2562}
2563
2564/**
James Smartacd68592012-01-18 16:25:09 -05002565 * lpfc_bg_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
2566 * @phba: The Hba for which this call is being executed.
2567 * @lpfc_cmd: The scsi buffer which is going to be prep'ed.
2568 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002569 * This is the protection/DIF aware version of
2570 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
James Smart5e0e2312019-08-14 16:56:54 -07002571 * two functions eventually, but for now, it's here.
2572 * RETURNS 0 - SUCCESS,
2573 * 1 - Failed DMA map, retry.
2574 * 2 - Invalid scsi cmd or prot-type. Do not rety.
James Smartacd68592012-01-18 16:25:09 -05002575 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002576static int
James Smartacd68592012-01-18 16:25:09 -05002577lpfc_bg_scsi_prep_dma_buf_s3(struct lpfc_hba *phba,
James Smartc4908502019-01-28 11:14:28 -08002578 struct lpfc_io_buf *lpfc_cmd)
James Smarte2a0a9d2008-12-04 22:40:02 -05002579{
2580 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2581 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smartc4908502019-01-28 11:14:28 -08002582 struct ulp_bde64 *bpl = (struct ulp_bde64 *)lpfc_cmd->dma_sgl;
James Smarte2a0a9d2008-12-04 22:40:02 -05002583 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2584 uint32_t num_bde = 0;
2585 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
2586 int prot_group_type = 0;
James Smarta6887e22013-04-17 20:18:07 -04002587 int fcpdl;
James Smart5e0e2312019-08-14 16:56:54 -07002588 int ret = 1;
James Smart7c4042a2018-11-29 16:09:40 -08002589 struct lpfc_vport *vport = phba->pport;
James Smarte2a0a9d2008-12-04 22:40:02 -05002590
2591 /*
2592 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
2593 * fcp_rsp regions to the first data bde entry
2594 */
2595 bpl += 2;
2596 if (scsi_sg_count(scsi_cmnd)) {
2597 /*
2598 * The driver stores the segment count returned from pci_map_sg
2599 * because this a count of dma-mappings used to map the use_sg
2600 * pages. They are not guaranteed to be the same for those
2601 * architectures that implement an IOMMU.
2602 */
2603 datasegcnt = dma_map_sg(&phba->pcidev->dev,
2604 scsi_sglist(scsi_cmnd),
2605 scsi_sg_count(scsi_cmnd), datadir);
2606 if (unlikely(!datasegcnt))
2607 return 1;
2608
2609 lpfc_cmd->seg_cnt = datasegcnt;
James Smart96f70772013-04-17 20:16:15 -04002610
2611 /* First check if data segment count from SCSI Layer is good */
James Smart5e0e2312019-08-14 16:56:54 -07002612 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
2613 WARN_ON_ONCE(lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt);
2614 ret = 2;
James Smart96f70772013-04-17 20:16:15 -04002615 goto err;
James Smart5e0e2312019-08-14 16:56:54 -07002616 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002617
2618 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
2619
2620 switch (prot_group_type) {
2621 case LPFC_PG_TYPE_NO_DIF:
James Smart96f70772013-04-17 20:16:15 -04002622
2623 /* Here we need to add a PDE5 and PDE6 to the count */
James Smart5e0e2312019-08-14 16:56:54 -07002624 if ((lpfc_cmd->seg_cnt + 2) > phba->cfg_total_seg_cnt) {
2625 ret = 2;
James Smart96f70772013-04-17 20:16:15 -04002626 goto err;
James Smart5e0e2312019-08-14 16:56:54 -07002627 }
James Smart96f70772013-04-17 20:16:15 -04002628
James Smarte2a0a9d2008-12-04 22:40:02 -05002629 num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
2630 datasegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002631 /* we should have 2 or more entries in buffer list */
James Smart5e0e2312019-08-14 16:56:54 -07002632 if (num_bde < 2) {
2633 ret = 2;
James Smarte2a0a9d2008-12-04 22:40:02 -05002634 goto err;
James Smart5e0e2312019-08-14 16:56:54 -07002635 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002636 break;
James Smart96f70772013-04-17 20:16:15 -04002637
2638 case LPFC_PG_TYPE_DIF_BUF:
James Smarte2a0a9d2008-12-04 22:40:02 -05002639 /*
2640 * This type indicates that protection buffers are
2641 * passed to the driver, so that needs to be prepared
2642 * for DMA
2643 */
2644 protsegcnt = dma_map_sg(&phba->pcidev->dev,
2645 scsi_prot_sglist(scsi_cmnd),
2646 scsi_prot_sg_count(scsi_cmnd), datadir);
2647 if (unlikely(!protsegcnt)) {
2648 scsi_dma_unmap(scsi_cmnd);
2649 return 1;
2650 }
2651
2652 lpfc_cmd->prot_seg_cnt = protsegcnt;
James Smart96f70772013-04-17 20:16:15 -04002653
2654 /*
2655 * There is a minimun of 4 BPLs used for every
2656 * protection data segment.
2657 */
2658 if ((lpfc_cmd->prot_seg_cnt * 4) >
James Smart5e0e2312019-08-14 16:56:54 -07002659 (phba->cfg_total_seg_cnt - 2)) {
2660 ret = 2;
James Smart96f70772013-04-17 20:16:15 -04002661 goto err;
James Smart5e0e2312019-08-14 16:56:54 -07002662 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002663
2664 num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
2665 datasegcnt, protsegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002666 /* we should have 3 or more entries in buffer list */
James Smart96f70772013-04-17 20:16:15 -04002667 if ((num_bde < 3) ||
James Smart5e0e2312019-08-14 16:56:54 -07002668 (num_bde > phba->cfg_total_seg_cnt)) {
2669 ret = 2;
James Smarte2a0a9d2008-12-04 22:40:02 -05002670 goto err;
James Smart5e0e2312019-08-14 16:56:54 -07002671 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002672 break;
James Smart96f70772013-04-17 20:16:15 -04002673
James Smarte2a0a9d2008-12-04 22:40:02 -05002674 case LPFC_PG_TYPE_INVALID:
2675 default:
James Smart96f70772013-04-17 20:16:15 -04002676 scsi_dma_unmap(scsi_cmnd);
2677 lpfc_cmd->seg_cnt = 0;
2678
James Smarte2a0a9d2008-12-04 22:40:02 -05002679 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2680 "9022 Unexpected protection group %i\n",
2681 prot_group_type);
James Smart5e0e2312019-08-14 16:56:54 -07002682 return 2;
James Smarte2a0a9d2008-12-04 22:40:02 -05002683 }
2684 }
2685
2686 /*
2687 * Finish initializing those IOCB fields that are dependent on the
2688 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
2689 * reinitialized since all iocb memory resources are used many times
2690 * for transmit, receive, and continuation bpl's.
2691 */
2692 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
2693 iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
2694 iocb_cmd->ulpBdeCount = 1;
2695 iocb_cmd->ulpLe = 1;
2696
James Smarta6887e22013-04-17 20:18:07 -04002697 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002698 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
2699
2700 /*
2701 * Due to difference in data length between DIF/non-DIF paths,
2702 * we need to set word 4 of IOCB here
2703 */
2704 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
2705
James Smart7c4042a2018-11-29 16:09:40 -08002706 /*
2707 * For First burst, we may need to adjust the initial transfer
2708 * length for DIF
2709 */
2710 if (iocb_cmd->un.fcpi.fcpi_XRdy &&
2711 (fcpdl < vport->cfg_first_burst_size))
2712 iocb_cmd->un.fcpi.fcpi_XRdy = fcpdl;
2713
James Smarte2a0a9d2008-12-04 22:40:02 -05002714 return 0;
2715err:
James Smart96f70772013-04-17 20:16:15 -04002716 if (lpfc_cmd->seg_cnt)
2717 scsi_dma_unmap(scsi_cmnd);
2718 if (lpfc_cmd->prot_seg_cnt)
2719 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
2720 scsi_prot_sg_count(scsi_cmnd),
2721 scsi_cmnd->sc_data_direction);
2722
James Smarte2a0a9d2008-12-04 22:40:02 -05002723 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart96f70772013-04-17 20:16:15 -04002724 "9023 Cannot setup S/G List for HBA"
2725 "IO segs %d/%d BPL %d SCSI %d: %d %d\n",
2726 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
2727 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
James Smarte2a0a9d2008-12-04 22:40:02 -05002728 prot_group_type, num_bde);
James Smart96f70772013-04-17 20:16:15 -04002729
2730 lpfc_cmd->seg_cnt = 0;
2731 lpfc_cmd->prot_seg_cnt = 0;
James Smart5e0e2312019-08-14 16:56:54 -07002732 return ret;
James Smarte2a0a9d2008-12-04 22:40:02 -05002733}
2734
2735/*
James Smart737d4242013-04-17 20:14:49 -04002736 * This function calcuates the T10 DIF guard tag
2737 * on the specified data using a CRC algorithmn
2738 * using crc_t10dif.
2739 */
Rashika Kheria7bfe7812014-09-03 12:55:36 -04002740static uint16_t
James Smart737d4242013-04-17 20:14:49 -04002741lpfc_bg_crc(uint8_t *data, int count)
2742{
2743 uint16_t crc = 0;
2744 uint16_t x;
2745
2746 crc = crc_t10dif(data, count);
2747 x = cpu_to_be16(crc);
2748 return x;
2749}
2750
2751/*
2752 * This function calcuates the T10 DIF guard tag
2753 * on the specified data using a CSUM algorithmn
2754 * using ip_compute_csum.
2755 */
Rashika Kheria7bfe7812014-09-03 12:55:36 -04002756static uint16_t
James Smart737d4242013-04-17 20:14:49 -04002757lpfc_bg_csum(uint8_t *data, int count)
2758{
2759 uint16_t ret;
2760
2761 ret = ip_compute_csum(data, count);
2762 return ret;
2763}
2764
2765/*
2766 * This function examines the protection data to try to determine
2767 * what type of T10-DIF error occurred.
2768 */
Rashika Kheria7bfe7812014-09-03 12:55:36 -04002769static void
James Smartc4908502019-01-28 11:14:28 -08002770lpfc_calc_bg_err(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
James Smart737d4242013-04-17 20:14:49 -04002771{
2772 struct scatterlist *sgpe; /* s/g prot entry */
2773 struct scatterlist *sgde; /* s/g data entry */
2774 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
2775 struct scsi_dif_tuple *src = NULL;
2776 uint8_t *data_src = NULL;
Sebastian Herbsztdb6f1c22015-08-31 16:48:14 -04002777 uint16_t guard_tag;
James Smart737d4242013-04-17 20:14:49 -04002778 uint16_t start_app_tag, app_tag;
2779 uint32_t start_ref_tag, ref_tag;
2780 int prot, protsegcnt;
2781 int err_type, len, data_len;
2782 int chk_ref, chk_app, chk_guard;
2783 uint16_t sum;
2784 unsigned blksize;
2785
2786 err_type = BGS_GUARD_ERR_MASK;
2787 sum = 0;
2788 guard_tag = 0;
2789
2790 /* First check to see if there is protection data to examine */
2791 prot = scsi_get_prot_op(cmd);
2792 if ((prot == SCSI_PROT_READ_STRIP) ||
2793 (prot == SCSI_PROT_WRITE_INSERT) ||
2794 (prot == SCSI_PROT_NORMAL))
2795 goto out;
2796
2797 /* Currently the driver just supports ref_tag and guard_tag checking */
2798 chk_ref = 1;
2799 chk_app = 0;
2800 chk_guard = 0;
2801
2802 /* Setup a ptr to the protection data provided by the SCSI host */
2803 sgpe = scsi_prot_sglist(cmd);
2804 protsegcnt = lpfc_cmd->prot_seg_cnt;
2805
2806 if (sgpe && protsegcnt) {
2807
2808 /*
2809 * We will only try to verify guard tag if the segment
2810 * data length is a multiple of the blksize.
2811 */
2812 sgde = scsi_sglist(cmd);
2813 blksize = lpfc_cmd_blksize(cmd);
2814 data_src = (uint8_t *)sg_virt(sgde);
2815 data_len = sgde->length;
2816 if ((data_len & (blksize - 1)) == 0)
2817 chk_guard = 1;
James Smart737d4242013-04-17 20:14:49 -04002818
James Smarte85d8f92013-05-31 17:04:10 -04002819 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
James Smarta6887e22013-04-17 20:18:07 -04002820 start_ref_tag = (uint32_t)scsi_get_lba(cmd); /* Truncate LBA */
James Smart737d4242013-04-17 20:14:49 -04002821 start_app_tag = src->app_tag;
James Smart737d4242013-04-17 20:14:49 -04002822 len = sgpe->length;
2823 while (src && protsegcnt) {
2824 while (len) {
2825
2826 /*
2827 * First check to see if a protection data
2828 * check is valid
2829 */
Dmitry Monakhov128b6f92017-06-29 11:31:12 -07002830 if ((src->ref_tag == T10_PI_REF_ESCAPE) ||
2831 (src->app_tag == T10_PI_APP_ESCAPE)) {
James Smart737d4242013-04-17 20:14:49 -04002832 start_ref_tag++;
2833 goto skipit;
2834 }
2835
James Smart9c6aa9d2013-05-31 17:03:39 -04002836 /* First Guard Tag checking */
2837 if (chk_guard) {
2838 guard_tag = src->guard_tag;
2839 if (lpfc_cmd_guard_csum(cmd))
2840 sum = lpfc_bg_csum(data_src,
2841 blksize);
2842 else
2843 sum = lpfc_bg_crc(data_src,
2844 blksize);
2845 if ((guard_tag != sum)) {
2846 err_type = BGS_GUARD_ERR_MASK;
2847 goto out;
2848 }
James Smart737d4242013-04-17 20:14:49 -04002849 }
2850
2851 /* Reference Tag checking */
2852 ref_tag = be32_to_cpu(src->ref_tag);
2853 if (chk_ref && (ref_tag != start_ref_tag)) {
2854 err_type = BGS_REFTAG_ERR_MASK;
2855 goto out;
2856 }
2857 start_ref_tag++;
2858
James Smart9c6aa9d2013-05-31 17:03:39 -04002859 /* App Tag checking */
2860 app_tag = src->app_tag;
2861 if (chk_app && (app_tag != start_app_tag)) {
2862 err_type = BGS_APPTAG_ERR_MASK;
2863 goto out;
James Smart737d4242013-04-17 20:14:49 -04002864 }
2865skipit:
2866 len -= sizeof(struct scsi_dif_tuple);
2867 if (len < 0)
2868 len = 0;
2869 src++;
2870
2871 data_src += blksize;
2872 data_len -= blksize;
2873
2874 /*
2875 * Are we at the end of the Data segment?
2876 * The data segment is only used for Guard
2877 * tag checking.
2878 */
2879 if (chk_guard && (data_len == 0)) {
2880 chk_guard = 0;
2881 sgde = sg_next(sgde);
2882 if (!sgde)
2883 goto out;
2884
2885 data_src = (uint8_t *)sg_virt(sgde);
2886 data_len = sgde->length;
2887 if ((data_len & (blksize - 1)) == 0)
2888 chk_guard = 1;
2889 }
2890 }
2891
2892 /* Goto the next Protection data segment */
2893 sgpe = sg_next(sgpe);
2894 if (sgpe) {
2895 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
2896 len = sgpe->length;
2897 } else {
2898 src = NULL;
2899 }
2900 protsegcnt--;
2901 }
2902 }
2903out:
2904 if (err_type == BGS_GUARD_ERR_MASK) {
2905 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2906 0x10, 0x1);
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02002907 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
2908 SAM_STAT_CHECK_CONDITION;
James Smart737d4242013-04-17 20:14:49 -04002909 phba->bg_guard_err_cnt++;
2910 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2911 "9069 BLKGRD: LBA %lx grd_tag error %x != %x\n",
2912 (unsigned long)scsi_get_lba(cmd),
2913 sum, guard_tag);
2914
2915 } else if (err_type == BGS_REFTAG_ERR_MASK) {
2916 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2917 0x10, 0x3);
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02002918 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
2919 SAM_STAT_CHECK_CONDITION;
James Smart737d4242013-04-17 20:14:49 -04002920
2921 phba->bg_reftag_err_cnt++;
2922 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2923 "9066 BLKGRD: LBA %lx ref_tag error %x != %x\n",
2924 (unsigned long)scsi_get_lba(cmd),
2925 ref_tag, start_ref_tag);
2926
2927 } else if (err_type == BGS_APPTAG_ERR_MASK) {
2928 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2929 0x10, 0x2);
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02002930 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
2931 SAM_STAT_CHECK_CONDITION;
James Smart737d4242013-04-17 20:14:49 -04002932
2933 phba->bg_apptag_err_cnt++;
2934 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2935 "9041 BLKGRD: LBA %lx app_tag error %x != %x\n",
2936 (unsigned long)scsi_get_lba(cmd),
2937 app_tag, start_app_tag);
2938 }
2939}
2940
2941
2942/*
James Smarte2a0a9d2008-12-04 22:40:02 -05002943 * This function checks for BlockGuard errors detected by
2944 * the HBA. In case of errors, the ASC/ASCQ fields in the
2945 * sense buffer will be set accordingly, paired with
2946 * ILLEGAL_REQUEST to signal to the kernel that the HBA
2947 * detected corruption.
2948 *
2949 * Returns:
2950 * 0 - No error found
2951 * 1 - BlockGuard error found
2952 * -1 - Internal error (bad profile, ...etc)
2953 */
2954static int
James Smartc4908502019-01-28 11:14:28 -08002955lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd,
2956 struct lpfc_iocbq *pIocbOut)
James Smarte2a0a9d2008-12-04 22:40:02 -05002957{
2958 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
2959 struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
2960 int ret = 0;
2961 uint32_t bghm = bgf->bghm;
2962 uint32_t bgstat = bgf->bgstat;
2963 uint64_t failing_sector = 0;
2964
James Smarte2a0a9d2008-12-04 22:40:02 -05002965 spin_lock(&_dump_buf_lock);
2966 if (!_dump_buf_done) {
James Smart6a9c52c2009-10-02 15:16:51 -04002967 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9070 BLKGRD: Saving"
2968 " Data for %u blocks to debugfs\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002969 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
James Smart6a9c52c2009-10-02 15:16:51 -04002970 lpfc_debug_save_data(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002971
2972 /* If we have a prot sgl, save the DIF buffer */
2973 if (lpfc_prot_group_type(phba, cmd) ==
2974 LPFC_PG_TYPE_DIF_BUF) {
James Smart6a9c52c2009-10-02 15:16:51 -04002975 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9071 BLKGRD: "
2976 "Saving DIF for %u blocks to debugfs\n",
2977 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
2978 lpfc_debug_save_dif(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002979 }
2980
2981 _dump_buf_done = 1;
2982 }
2983 spin_unlock(&_dump_buf_lock);
2984
2985 if (lpfc_bgs_get_invalid_prof(bgstat)) {
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02002986 cmd->result = DID_ERROR << 16;
James Smart737d4242013-04-17 20:14:49 -04002987 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2988 "9072 BLKGRD: Invalid BG Profile in cmd"
2989 " 0x%x lba 0x%llx blk cnt 0x%x "
2990 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
2991 (unsigned long long)scsi_get_lba(cmd),
2992 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05002993 ret = (-1);
2994 goto out;
2995 }
2996
2997 if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02002998 cmd->result = DID_ERROR << 16;
James Smart737d4242013-04-17 20:14:49 -04002999 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3000 "9073 BLKGRD: Invalid BG PDIF Block in cmd"
3001 " 0x%x lba 0x%llx blk cnt 0x%x "
3002 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3003 (unsigned long long)scsi_get_lba(cmd),
3004 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003005 ret = (-1);
3006 goto out;
3007 }
3008
3009 if (lpfc_bgs_get_guard_err(bgstat)) {
3010 ret = 1;
3011
3012 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3013 0x10, 0x1);
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02003014 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
3015 SAM_STAT_CHECK_CONDITION;
James Smarte2a0a9d2008-12-04 22:40:02 -05003016 phba->bg_guard_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04003017 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3018 "9055 BLKGRD: Guard Tag error in cmd"
3019 " 0x%x lba 0x%llx blk cnt 0x%x "
3020 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3021 (unsigned long long)scsi_get_lba(cmd),
3022 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003023 }
3024
3025 if (lpfc_bgs_get_reftag_err(bgstat)) {
3026 ret = 1;
3027
3028 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3029 0x10, 0x3);
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02003030 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
3031 SAM_STAT_CHECK_CONDITION;
James Smarte2a0a9d2008-12-04 22:40:02 -05003032
3033 phba->bg_reftag_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04003034 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3035 "9056 BLKGRD: Ref Tag error in cmd"
3036 " 0x%x lba 0x%llx blk cnt 0x%x "
3037 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3038 (unsigned long long)scsi_get_lba(cmd),
3039 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003040 }
3041
3042 if (lpfc_bgs_get_apptag_err(bgstat)) {
3043 ret = 1;
3044
3045 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3046 0x10, 0x2);
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02003047 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
3048 SAM_STAT_CHECK_CONDITION;
James Smarte2a0a9d2008-12-04 22:40:02 -05003049
3050 phba->bg_apptag_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04003051 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3052 "9061 BLKGRD: App Tag error in cmd"
3053 " 0x%x lba 0x%llx blk cnt 0x%x "
3054 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3055 (unsigned long long)scsi_get_lba(cmd),
3056 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003057 }
3058
3059 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
3060 /*
3061 * setup sense data descriptor 0 per SPC-4 as an information
James Smart7c56b9f2011-07-22 18:36:25 -04003062 * field, and put the failing LBA in it.
3063 * This code assumes there was also a guard/app/ref tag error
3064 * indication.
James Smarte2a0a9d2008-12-04 22:40:02 -05003065 */
James Smart7c56b9f2011-07-22 18:36:25 -04003066 cmd->sense_buffer[7] = 0xc; /* Additional sense length */
3067 cmd->sense_buffer[8] = 0; /* Information descriptor type */
3068 cmd->sense_buffer[9] = 0xa; /* Additional descriptor length */
3069 cmd->sense_buffer[10] = 0x80; /* Validity bit */
James Smartacd68592012-01-18 16:25:09 -05003070
3071 /* bghm is a "on the wire" FC frame based count */
3072 switch (scsi_get_prot_op(cmd)) {
3073 case SCSI_PROT_READ_INSERT:
3074 case SCSI_PROT_WRITE_STRIP:
3075 bghm /= cmd->device->sector_size;
3076 break;
3077 case SCSI_PROT_READ_STRIP:
3078 case SCSI_PROT_WRITE_INSERT:
3079 case SCSI_PROT_READ_PASS:
3080 case SCSI_PROT_WRITE_PASS:
3081 bghm /= (cmd->device->sector_size +
3082 sizeof(struct scsi_dif_tuple));
3083 break;
3084 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003085
3086 failing_sector = scsi_get_lba(cmd);
3087 failing_sector += bghm;
3088
James Smart7c56b9f2011-07-22 18:36:25 -04003089 /* Descriptor Information */
3090 put_unaligned_be64(failing_sector, &cmd->sense_buffer[12]);
James Smarte2a0a9d2008-12-04 22:40:02 -05003091 }
3092
3093 if (!ret) {
3094 /* No error was reported - problem in FW? */
James Smart737d4242013-04-17 20:14:49 -04003095 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3096 "9057 BLKGRD: Unknown error in cmd"
3097 " 0x%x lba 0x%llx blk cnt 0x%x "
3098 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3099 (unsigned long long)scsi_get_lba(cmd),
3100 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003101
James Smart737d4242013-04-17 20:14:49 -04003102 /* Calcuate what type of error it was */
3103 lpfc_calc_bg_err(phba, lpfc_cmd);
3104 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003105out:
3106 return ret;
3107}
3108
James Smartea2151b2008-09-07 11:52:10 -04003109/**
James Smartda0436e2009-05-22 14:51:39 -04003110 * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3111 * @phba: The Hba for which this call is being executed.
3112 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3113 *
3114 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
3115 * field of @lpfc_cmd for device with SLI-4 interface spec.
3116 *
3117 * Return codes:
James Smart5e0e2312019-08-14 16:56:54 -07003118 * 2 - Error - Do not retry
3119 * 1 - Error - Retry
James Smart6c8eea52010-04-06 14:49:53 -04003120 * 0 - Success
James Smartda0436e2009-05-22 14:51:39 -04003121 **/
3122static int
James Smartc4908502019-01-28 11:14:28 -08003123lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
James Smartda0436e2009-05-22 14:51:39 -04003124{
3125 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3126 struct scatterlist *sgel = NULL;
3127 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart0794d602019-01-28 11:14:19 -08003128 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
James Smartfedd3b72011-02-16 12:39:24 -05003129 struct sli4_sge *first_data_sgl;
James Smartda0436e2009-05-22 14:51:39 -04003130 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
3131 dma_addr_t physaddr;
3132 uint32_t num_bde = 0;
3133 uint32_t dma_len;
3134 uint32_t dma_offset = 0;
James Smartd79c9e92019-08-14 16:57:09 -07003135 int nseg, i, j;
James Smartfedd3b72011-02-16 12:39:24 -05003136 struct ulp_bde64 *bde;
James Smartd79c9e92019-08-14 16:57:09 -07003137 bool lsp_just_set = false;
3138 struct sli4_hybrid_sgl *sgl_xtra = NULL;
James Smartda0436e2009-05-22 14:51:39 -04003139
3140 /*
3141 * There are three possibilities here - use scatter-gather segment, use
3142 * the single mapping, or neither. Start the lpfc command prep by
3143 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
3144 * data bde entry.
3145 */
3146 if (scsi_sg_count(scsi_cmnd)) {
3147 /*
3148 * The driver stores the segment count returned from pci_map_sg
3149 * because this a count of dma-mappings used to map the use_sg
3150 * pages. They are not guaranteed to be the same for those
3151 * architectures that implement an IOMMU.
3152 */
3153
3154 nseg = scsi_dma_map(scsi_cmnd);
James Smart5116fbf2015-05-22 10:42:39 -04003155 if (unlikely(nseg <= 0))
James Smartda0436e2009-05-22 14:51:39 -04003156 return 1;
3157 sgl += 1;
3158 /* clear the last flag in the fcp_rsp map entry */
3159 sgl->word2 = le32_to_cpu(sgl->word2);
3160 bf_set(lpfc_sli4_sge_last, sgl, 0);
3161 sgl->word2 = cpu_to_le32(sgl->word2);
3162 sgl += 1;
James Smartfedd3b72011-02-16 12:39:24 -05003163 first_data_sgl = sgl;
James Smartda0436e2009-05-22 14:51:39 -04003164 lpfc_cmd->seg_cnt = nseg;
James Smartd79c9e92019-08-14 16:57:09 -07003165 if (!phba->cfg_xpsgl &&
3166 lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04003167 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
3168 " %s: Too many sg segments from "
3169 "dma_map_sg. Config %d, seg_cnt %d\n",
3170 __func__, phba->cfg_sg_seg_cnt,
James Smartda0436e2009-05-22 14:51:39 -04003171 lpfc_cmd->seg_cnt);
James Smart5e0e2312019-08-14 16:56:54 -07003172 WARN_ON_ONCE(lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt);
James Smart96f70772013-04-17 20:16:15 -04003173 lpfc_cmd->seg_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04003174 scsi_dma_unmap(scsi_cmnd);
James Smart5e0e2312019-08-14 16:56:54 -07003175 return 2;
James Smartda0436e2009-05-22 14:51:39 -04003176 }
3177
3178 /*
3179 * The driver established a maximum scatter-gather segment count
3180 * during probe that limits the number of sg elements in any
3181 * single scsi command. Just run through the seg_cnt and format
3182 * the sge's.
3183 * When using SLI-3 the driver will try to fit all the BDEs into
3184 * the IOCB. If it can't then the BDEs get added to a BPL as it
3185 * does for SLI-2 mode.
3186 */
James Smartd79c9e92019-08-14 16:57:09 -07003187
3188 /* for tracking segment boundaries */
3189 sgel = scsi_sglist(scsi_cmnd);
3190 j = 2;
3191 for (i = 0; i < nseg; i++) {
3192 sgl->word2 = 0;
3193 if ((num_bde + 1) == nseg) {
James Smartda0436e2009-05-22 14:51:39 -04003194 bf_set(lpfc_sli4_sge_last, sgl, 1);
James Smartd79c9e92019-08-14 16:57:09 -07003195 bf_set(lpfc_sli4_sge_type, sgl,
3196 LPFC_SGE_TYPE_DATA);
3197 } else {
James Smartda0436e2009-05-22 14:51:39 -04003198 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smartd79c9e92019-08-14 16:57:09 -07003199
3200 /* do we need to expand the segment */
3201 if (!lsp_just_set &&
3202 !((j + 1) % phba->border_sge_num) &&
3203 ((nseg - 1) != i)) {
3204 /* set LSP type */
3205 bf_set(lpfc_sli4_sge_type, sgl,
3206 LPFC_SGE_TYPE_LSP);
3207
3208 sgl_xtra = lpfc_get_sgl_per_hdwq(
3209 phba, lpfc_cmd);
3210
3211 if (unlikely(!sgl_xtra)) {
3212 lpfc_cmd->seg_cnt = 0;
3213 scsi_dma_unmap(scsi_cmnd);
3214 return 1;
3215 }
3216 sgl->addr_lo = cpu_to_le32(putPaddrLow(
3217 sgl_xtra->dma_phys_sgl));
3218 sgl->addr_hi = cpu_to_le32(putPaddrHigh(
3219 sgl_xtra->dma_phys_sgl));
3220
3221 } else {
3222 bf_set(lpfc_sli4_sge_type, sgl,
3223 LPFC_SGE_TYPE_DATA);
3224 }
3225 }
3226
3227 if (!(bf_get(lpfc_sli4_sge_type, sgl) &
3228 LPFC_SGE_TYPE_LSP)) {
3229 if ((nseg - 1) == i)
3230 bf_set(lpfc_sli4_sge_last, sgl, 1);
3231
3232 physaddr = sg_dma_address(sgel);
3233 dma_len = sg_dma_len(sgel);
3234 sgl->addr_lo = cpu_to_le32(putPaddrLow(
3235 physaddr));
3236 sgl->addr_hi = cpu_to_le32(putPaddrHigh(
3237 physaddr));
3238
3239 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
3240 sgl->word2 = cpu_to_le32(sgl->word2);
3241 sgl->sge_len = cpu_to_le32(dma_len);
3242
3243 dma_offset += dma_len;
3244 sgel = sg_next(sgel);
3245
3246 sgl++;
3247 lsp_just_set = false;
3248
3249 } else {
3250 sgl->word2 = cpu_to_le32(sgl->word2);
3251 sgl->sge_len = cpu_to_le32(
3252 phba->cfg_sg_dma_buf_size);
3253
3254 sgl = (struct sli4_sge *)sgl_xtra->dma_sgl;
3255 i = i - 1;
3256
3257 lsp_just_set = true;
3258 }
3259
3260 j++;
James Smartda0436e2009-05-22 14:51:39 -04003261 }
James Smart0bc2b7c2018-02-22 08:18:48 -08003262 /*
3263 * Setup the first Payload BDE. For FCoE we just key off
James Smart414abe02018-06-26 08:24:26 -07003264 * Performance Hints, for FC we use lpfc_enable_pbde.
3265 * We populate words 13-15 of IOCB/WQE.
James Smart0bc2b7c2018-02-22 08:18:48 -08003266 */
3267 if ((phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) ||
James Smart414abe02018-06-26 08:24:26 -07003268 phba->cfg_enable_pbde) {
James Smartfedd3b72011-02-16 12:39:24 -05003269 bde = (struct ulp_bde64 *)
James Smart414abe02018-06-26 08:24:26 -07003270 &(iocb_cmd->unsli3.sli3Words[5]);
James Smartfedd3b72011-02-16 12:39:24 -05003271 bde->addrLow = first_data_sgl->addr_lo;
3272 bde->addrHigh = first_data_sgl->addr_hi;
3273 bde->tus.f.bdeSize =
3274 le32_to_cpu(first_data_sgl->sge_len);
3275 bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
3276 bde->tus.w = cpu_to_le32(bde->tus.w);
3277 }
James Smartda0436e2009-05-22 14:51:39 -04003278 } else {
3279 sgl += 1;
3280 /* clear the last flag in the fcp_rsp map entry */
3281 sgl->word2 = le32_to_cpu(sgl->word2);
3282 bf_set(lpfc_sli4_sge_last, sgl, 1);
3283 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart414abe02018-06-26 08:24:26 -07003284
3285 if ((phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) ||
3286 phba->cfg_enable_pbde) {
3287 bde = (struct ulp_bde64 *)
3288 &(iocb_cmd->unsli3.sli3Words[5]);
3289 memset(bde, 0, (sizeof(uint32_t) * 3));
3290 }
James Smartda0436e2009-05-22 14:51:39 -04003291 }
3292
3293 /*
3294 * Finish initializing those IOCB fields that are dependent on the
3295 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
3296 * explicitly reinitialized.
3297 * all iocb memory resources are reused.
3298 */
3299 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
3300
3301 /*
3302 * Due to difference in data length between DIF/non-DIF paths,
3303 * we need to set word 4 of IOCB here
3304 */
3305 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
James Smart1ba981f2014-02-20 09:56:45 -05003306
3307 /*
3308 * If the OAS driver feature is enabled and the lun is enabled for
3309 * OAS, set the oas iocb related flags.
3310 */
James Smartf38fa0b2014-04-04 13:52:21 -04003311 if ((phba->cfg_fof) && ((struct lpfc_device_data *)
James Smartc92c8412016-07-06 12:36:05 -07003312 scsi_cmnd->device->hostdata)->oas_enabled) {
James Smart9bd2bff52014-09-03 12:57:30 -04003313 lpfc_cmd->cur_iocbq.iocb_flag |= (LPFC_IO_OAS | LPFC_IO_FOF);
James Smartc92c8412016-07-06 12:36:05 -07003314 lpfc_cmd->cur_iocbq.priority = ((struct lpfc_device_data *)
3315 scsi_cmnd->device->hostdata)->priority;
3316 }
James Smartc4908502019-01-28 11:14:28 -08003317
James Smartda0436e2009-05-22 14:51:39 -04003318 return 0;
3319}
3320
3321/**
James Smartacd68592012-01-18 16:25:09 -05003322 * lpfc_bg_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3323 * @phba: The Hba for which this call is being executed.
3324 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3325 *
3326 * This is the protection/DIF aware version of
3327 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
3328 * two functions eventually, but for now, it's here
James Smart5e0e2312019-08-14 16:56:54 -07003329 * Return codes:
3330 * 2 - Error - Do not retry
3331 * 1 - Error - Retry
3332 * 0 - Success
James Smartacd68592012-01-18 16:25:09 -05003333 **/
3334static int
3335lpfc_bg_scsi_prep_dma_buf_s4(struct lpfc_hba *phba,
James Smartc4908502019-01-28 11:14:28 -08003336 struct lpfc_io_buf *lpfc_cmd)
James Smartacd68592012-01-18 16:25:09 -05003337{
3338 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3339 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart0794d602019-01-28 11:14:19 -08003340 struct sli4_sge *sgl = (struct sli4_sge *)(lpfc_cmd->dma_sgl);
James Smartacd68592012-01-18 16:25:09 -05003341 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart96f70772013-04-17 20:16:15 -04003342 uint32_t num_sge = 0;
James Smartacd68592012-01-18 16:25:09 -05003343 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
3344 int prot_group_type = 0;
3345 int fcpdl;
James Smart5e0e2312019-08-14 16:56:54 -07003346 int ret = 1;
James Smart7c4042a2018-11-29 16:09:40 -08003347 struct lpfc_vport *vport = phba->pport;
James Smartacd68592012-01-18 16:25:09 -05003348
3349 /*
3350 * Start the lpfc command prep by bumping the sgl beyond fcp_cmnd
James Smart96f70772013-04-17 20:16:15 -04003351 * fcp_rsp regions to the first data sge entry
James Smartacd68592012-01-18 16:25:09 -05003352 */
3353 if (scsi_sg_count(scsi_cmnd)) {
3354 /*
3355 * The driver stores the segment count returned from pci_map_sg
3356 * because this a count of dma-mappings used to map the use_sg
3357 * pages. They are not guaranteed to be the same for those
3358 * architectures that implement an IOMMU.
3359 */
3360 datasegcnt = dma_map_sg(&phba->pcidev->dev,
3361 scsi_sglist(scsi_cmnd),
3362 scsi_sg_count(scsi_cmnd), datadir);
3363 if (unlikely(!datasegcnt))
3364 return 1;
3365
3366 sgl += 1;
3367 /* clear the last flag in the fcp_rsp map entry */
3368 sgl->word2 = le32_to_cpu(sgl->word2);
3369 bf_set(lpfc_sli4_sge_last, sgl, 0);
3370 sgl->word2 = cpu_to_le32(sgl->word2);
3371
3372 sgl += 1;
3373 lpfc_cmd->seg_cnt = datasegcnt;
James Smart96f70772013-04-17 20:16:15 -04003374
3375 /* First check if data segment count from SCSI Layer is good */
James Smartd79c9e92019-08-14 16:57:09 -07003376 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt &&
3377 !phba->cfg_xpsgl) {
James Smart5e0e2312019-08-14 16:56:54 -07003378 WARN_ON_ONCE(lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt);
3379 ret = 2;
James Smart96f70772013-04-17 20:16:15 -04003380 goto err;
James Smart5e0e2312019-08-14 16:56:54 -07003381 }
James Smartacd68592012-01-18 16:25:09 -05003382
3383 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
3384
3385 switch (prot_group_type) {
3386 case LPFC_PG_TYPE_NO_DIF:
James Smart96f70772013-04-17 20:16:15 -04003387 /* Here we need to add a DISEED to the count */
James Smartd79c9e92019-08-14 16:57:09 -07003388 if (((lpfc_cmd->seg_cnt + 1) >
3389 phba->cfg_total_seg_cnt) &&
3390 !phba->cfg_xpsgl) {
James Smart5e0e2312019-08-14 16:56:54 -07003391 ret = 2;
James Smart96f70772013-04-17 20:16:15 -04003392 goto err;
James Smart5e0e2312019-08-14 16:56:54 -07003393 }
James Smart96f70772013-04-17 20:16:15 -04003394
3395 num_sge = lpfc_bg_setup_sgl(phba, scsi_cmnd, sgl,
James Smartd79c9e92019-08-14 16:57:09 -07003396 datasegcnt, lpfc_cmd);
James Smart96f70772013-04-17 20:16:15 -04003397
James Smartacd68592012-01-18 16:25:09 -05003398 /* we should have 2 or more entries in buffer list */
James Smart5e0e2312019-08-14 16:56:54 -07003399 if (num_sge < 2) {
3400 ret = 2;
James Smartacd68592012-01-18 16:25:09 -05003401 goto err;
James Smart5e0e2312019-08-14 16:56:54 -07003402 }
James Smartacd68592012-01-18 16:25:09 -05003403 break;
James Smart96f70772013-04-17 20:16:15 -04003404
3405 case LPFC_PG_TYPE_DIF_BUF:
James Smartacd68592012-01-18 16:25:09 -05003406 /*
3407 * This type indicates that protection buffers are
3408 * passed to the driver, so that needs to be prepared
3409 * for DMA
3410 */
3411 protsegcnt = dma_map_sg(&phba->pcidev->dev,
3412 scsi_prot_sglist(scsi_cmnd),
3413 scsi_prot_sg_count(scsi_cmnd), datadir);
3414 if (unlikely(!protsegcnt)) {
3415 scsi_dma_unmap(scsi_cmnd);
3416 return 1;
3417 }
3418
3419 lpfc_cmd->prot_seg_cnt = protsegcnt;
James Smart96f70772013-04-17 20:16:15 -04003420 /*
3421 * There is a minimun of 3 SGEs used for every
3422 * protection data segment.
3423 */
James Smartd79c9e92019-08-14 16:57:09 -07003424 if (((lpfc_cmd->prot_seg_cnt * 3) >
3425 (phba->cfg_total_seg_cnt - 2)) &&
3426 !phba->cfg_xpsgl) {
James Smart5e0e2312019-08-14 16:56:54 -07003427 ret = 2;
James Smart96f70772013-04-17 20:16:15 -04003428 goto err;
James Smart5e0e2312019-08-14 16:56:54 -07003429 }
James Smartacd68592012-01-18 16:25:09 -05003430
James Smart96f70772013-04-17 20:16:15 -04003431 num_sge = lpfc_bg_setup_sgl_prot(phba, scsi_cmnd, sgl,
James Smartd79c9e92019-08-14 16:57:09 -07003432 datasegcnt, protsegcnt, lpfc_cmd);
James Smart96f70772013-04-17 20:16:15 -04003433
James Smartacd68592012-01-18 16:25:09 -05003434 /* we should have 3 or more entries in buffer list */
James Smartd79c9e92019-08-14 16:57:09 -07003435 if (num_sge < 3 ||
3436 (num_sge > phba->cfg_total_seg_cnt &&
3437 !phba->cfg_xpsgl)) {
James Smart5e0e2312019-08-14 16:56:54 -07003438 ret = 2;
James Smartacd68592012-01-18 16:25:09 -05003439 goto err;
James Smart5e0e2312019-08-14 16:56:54 -07003440 }
James Smartacd68592012-01-18 16:25:09 -05003441 break;
James Smart96f70772013-04-17 20:16:15 -04003442
James Smartacd68592012-01-18 16:25:09 -05003443 case LPFC_PG_TYPE_INVALID:
3444 default:
James Smart96f70772013-04-17 20:16:15 -04003445 scsi_dma_unmap(scsi_cmnd);
3446 lpfc_cmd->seg_cnt = 0;
3447
James Smartacd68592012-01-18 16:25:09 -05003448 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
3449 "9083 Unexpected protection group %i\n",
3450 prot_group_type);
James Smart5e0e2312019-08-14 16:56:54 -07003451 return 2;
James Smartacd68592012-01-18 16:25:09 -05003452 }
3453 }
3454
James Smart8012cc32012-10-31 14:44:49 -04003455 switch (scsi_get_prot_op(scsi_cmnd)) {
3456 case SCSI_PROT_WRITE_STRIP:
3457 case SCSI_PROT_READ_STRIP:
3458 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_STRIP;
3459 break;
3460 case SCSI_PROT_WRITE_INSERT:
3461 case SCSI_PROT_READ_INSERT:
3462 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_INSERT;
3463 break;
3464 case SCSI_PROT_WRITE_PASS:
3465 case SCSI_PROT_READ_PASS:
3466 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_PASS;
3467 break;
3468 }
3469
James Smartacd68592012-01-18 16:25:09 -05003470 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
James Smartacd68592012-01-18 16:25:09 -05003471 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
3472
3473 /*
3474 * Due to difference in data length between DIF/non-DIF paths,
3475 * we need to set word 4 of IOCB here
3476 */
3477 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
James Smartacd68592012-01-18 16:25:09 -05003478
James Smart9bd2bff52014-09-03 12:57:30 -04003479 /*
James Smart7c4042a2018-11-29 16:09:40 -08003480 * For First burst, we may need to adjust the initial transfer
3481 * length for DIF
3482 */
3483 if (iocb_cmd->un.fcpi.fcpi_XRdy &&
3484 (fcpdl < vport->cfg_first_burst_size))
3485 iocb_cmd->un.fcpi.fcpi_XRdy = fcpdl;
3486
3487 /*
James Smart9bd2bff52014-09-03 12:57:30 -04003488 * If the OAS driver feature is enabled and the lun is enabled for
3489 * OAS, set the oas iocb related flags.
3490 */
3491 if ((phba->cfg_fof) && ((struct lpfc_device_data *)
3492 scsi_cmnd->device->hostdata)->oas_enabled)
3493 lpfc_cmd->cur_iocbq.iocb_flag |= (LPFC_IO_OAS | LPFC_IO_FOF);
3494
James Smartacd68592012-01-18 16:25:09 -05003495 return 0;
3496err:
James Smart96f70772013-04-17 20:16:15 -04003497 if (lpfc_cmd->seg_cnt)
3498 scsi_dma_unmap(scsi_cmnd);
3499 if (lpfc_cmd->prot_seg_cnt)
3500 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
3501 scsi_prot_sg_count(scsi_cmnd),
3502 scsi_cmnd->sc_data_direction);
3503
James Smartacd68592012-01-18 16:25:09 -05003504 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart96f70772013-04-17 20:16:15 -04003505 "9084 Cannot setup S/G List for HBA"
3506 "IO segs %d/%d SGL %d SCSI %d: %d %d\n",
3507 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
3508 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
3509 prot_group_type, num_sge);
3510
3511 lpfc_cmd->seg_cnt = 0;
3512 lpfc_cmd->prot_seg_cnt = 0;
James Smart5e0e2312019-08-14 16:56:54 -07003513 return ret;
James Smartacd68592012-01-18 16:25:09 -05003514}
3515
3516/**
James Smart3772a992009-05-22 14:50:54 -04003517 * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3518 * @phba: The Hba for which this call is being executed.
3519 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3520 *
3521 * This routine wraps the actual DMA mapping function pointer from the
3522 * lpfc_hba struct.
3523 *
3524 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04003525 * 1 - Error
3526 * 0 - Success
James Smart3772a992009-05-22 14:50:54 -04003527 **/
3528static inline int
James Smartc4908502019-01-28 11:14:28 -08003529lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
James Smart3772a992009-05-22 14:50:54 -04003530{
3531 return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
3532}
3533
3534/**
James Smartacd68592012-01-18 16:25:09 -05003535 * lpfc_bg_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3536 * using BlockGuard.
3537 * @phba: The Hba for which this call is being executed.
3538 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3539 *
3540 * This routine wraps the actual DMA mapping function pointer from the
3541 * lpfc_hba struct.
3542 *
3543 * Return codes:
3544 * 1 - Error
3545 * 0 - Success
3546 **/
3547static inline int
James Smartc4908502019-01-28 11:14:28 -08003548lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
James Smartacd68592012-01-18 16:25:09 -05003549{
3550 return phba->lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
3551}
3552
3553/**
James Smart3621a712009-04-06 18:47:14 -04003554 * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
James Smartea2151b2008-09-07 11:52:10 -04003555 * @phba: Pointer to hba context object.
3556 * @vport: Pointer to vport object.
3557 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
3558 * @rsp_iocb: Pointer to response iocb object which reported error.
3559 *
3560 * This function posts an event when there is a SCSI command reporting
3561 * error from the scsi device.
3562 **/
3563static void
3564lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
James Smartc4908502019-01-28 11:14:28 -08003565 struct lpfc_io_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
James Smartea2151b2008-09-07 11:52:10 -04003566 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3567 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
3568 uint32_t resp_info = fcprsp->rspStatus2;
3569 uint32_t scsi_status = fcprsp->rspStatus3;
3570 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
3571 struct lpfc_fast_path_event *fast_path_evt = NULL;
3572 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
3573 unsigned long flags;
3574
James Smart5989b8d2010-10-22 11:06:56 -04003575 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3576 return;
3577
James Smartea2151b2008-09-07 11:52:10 -04003578 /* If there is queuefull or busy condition send a scsi event */
3579 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
3580 (cmnd->result == SAM_STAT_BUSY)) {
3581 fast_path_evt = lpfc_alloc_fast_evt(phba);
3582 if (!fast_path_evt)
3583 return;
3584 fast_path_evt->un.scsi_evt.event_type =
3585 FC_REG_SCSI_EVENT;
3586 fast_path_evt->un.scsi_evt.subcategory =
3587 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
3588 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
3589 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
3590 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
3591 &pnode->nlp_portname, sizeof(struct lpfc_name));
3592 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
3593 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3594 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
3595 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
3596 fast_path_evt = lpfc_alloc_fast_evt(phba);
3597 if (!fast_path_evt)
3598 return;
3599 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
3600 FC_REG_SCSI_EVENT;
3601 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
3602 LPFC_EVENT_CHECK_COND;
3603 fast_path_evt->un.check_cond_evt.scsi_event.lun =
3604 cmnd->device->lun;
3605 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
3606 &pnode->nlp_portname, sizeof(struct lpfc_name));
3607 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
3608 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3609 fast_path_evt->un.check_cond_evt.sense_key =
3610 cmnd->sense_buffer[2] & 0xf;
3611 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
3612 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
3613 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3614 fcpi_parm &&
3615 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
3616 ((scsi_status == SAM_STAT_GOOD) &&
3617 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
3618 /*
3619 * If status is good or resid does not match with fcp_param and
3620 * there is valid fcpi_parm, then there is a read_check error
3621 */
3622 fast_path_evt = lpfc_alloc_fast_evt(phba);
3623 if (!fast_path_evt)
3624 return;
3625 fast_path_evt->un.read_check_error.header.event_type =
3626 FC_REG_FABRIC_EVENT;
3627 fast_path_evt->un.read_check_error.header.subcategory =
3628 LPFC_EVENT_FCPRDCHKERR;
3629 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
3630 &pnode->nlp_portname, sizeof(struct lpfc_name));
3631 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
3632 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3633 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
3634 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
3635 fast_path_evt->un.read_check_error.fcpiparam =
3636 fcpi_parm;
3637 } else
3638 return;
3639
3640 fast_path_evt->vport = vport;
3641 spin_lock_irqsave(&phba->hbalock, flags);
3642 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
3643 spin_unlock_irqrestore(&phba->hbalock, flags);
3644 lpfc_worker_wake_up(phba);
3645 return;
3646}
James Smart9bad7672008-12-04 22:39:02 -05003647
3648/**
James Smartf1126682009-06-10 17:22:44 -04003649 * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
James Smart3772a992009-05-22 14:50:54 -04003650 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -05003651 * @psb: The scsi buffer which is going to be un-mapped.
3652 *
3653 * This routine does DMA un-mapping of scatter gather list of scsi command
James Smart3772a992009-05-22 14:50:54 -04003654 * field of @lpfc_cmd for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05003655 **/
dea31012005-04-17 16:05:31 -05003656static void
James Smartc4908502019-01-28 11:14:28 -08003657lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_io_buf *psb)
James Smartbcf4dbf2006-07-06 15:50:08 -04003658{
3659 /*
3660 * There are only two special cases to consider. (1) the scsi command
3661 * requested scatter-gather usage or (2) the scsi command allocated
3662 * a request buffer, but did not request use_sg. There is a third
3663 * case, but it does not require resource deallocation.
3664 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003665 if (psb->seg_cnt > 0)
3666 scsi_dma_unmap(psb->pCmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003667 if (psb->prot_seg_cnt > 0)
3668 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
3669 scsi_prot_sg_count(psb->pCmd),
3670 psb->pCmd->sc_data_direction);
James Smartbcf4dbf2006-07-06 15:50:08 -04003671}
3672
James Smart9bad7672008-12-04 22:39:02 -05003673/**
James Smart3621a712009-04-06 18:47:14 -04003674 * lpfc_handler_fcp_err - FCP response handler
James Smart9bad7672008-12-04 22:39:02 -05003675 * @vport: The virtual port for which this call is being executed.
James Smartc4908502019-01-28 11:14:28 -08003676 * @lpfc_cmd: Pointer to lpfc_io_buf data structure.
James Smart9bad7672008-12-04 22:39:02 -05003677 * @rsp_iocb: The response IOCB which contains FCP error.
3678 *
3679 * This routine is called to process response IOCB with status field
3680 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
3681 * based upon SCSI and FCP error.
3682 **/
James Smartbcf4dbf2006-07-06 15:50:08 -04003683static void
James Smartc4908502019-01-28 11:14:28 -08003684lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_io_buf *lpfc_cmd,
James Smart2e0fef82007-06-17 19:56:36 -05003685 struct lpfc_iocbq *rsp_iocb)
dea31012005-04-17 16:05:31 -05003686{
James Smart5afab6b2015-12-16 18:12:01 -05003687 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003688 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3689 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
3690 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
James Smart7054a602007-04-25 09:52:34 -04003691 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
dea31012005-04-17 16:05:31 -05003692 uint32_t resp_info = fcprsp->rspStatus2;
3693 uint32_t scsi_status = fcprsp->rspStatus3;
James Smartc7743952006-12-02 13:34:42 -05003694 uint32_t *lp;
dea31012005-04-17 16:05:31 -05003695 uint32_t host_status = DID_OK;
3696 uint32_t rsplen = 0;
James Smart5afab6b2015-12-16 18:12:01 -05003697 uint32_t fcpDl;
James Smartc7743952006-12-02 13:34:42 -05003698 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea31012005-04-17 16:05:31 -05003699
James Smartea2151b2008-09-07 11:52:10 -04003700
dea31012005-04-17 16:05:31 -05003701 /*
3702 * If this is a task management command, there is no
3703 * scsi packet associated with this lpfc_cmd. The driver
3704 * consumes it.
3705 */
3706 if (fcpcmd->fcpCntl2) {
3707 scsi_status = 0;
3708 goto out;
3709 }
3710
James Smart6a9c52c2009-10-02 15:16:51 -04003711 if (resp_info & RSP_LEN_VALID) {
3712 rsplen = be32_to_cpu(fcprsp->rspRspLen);
James Smarte40a02c2010-02-26 14:13:54 -05003713 if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
James Smart6a9c52c2009-10-02 15:16:51 -04003714 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3715 "2719 Invalid response length: "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02003716 "tgt x%x lun x%llx cmnd x%x rsplen x%x\n",
James Smart6a9c52c2009-10-02 15:16:51 -04003717 cmnd->device->id,
3718 cmnd->device->lun, cmnd->cmnd[0],
3719 rsplen);
3720 host_status = DID_ERROR;
3721 goto out;
3722 }
James Smarte40a02c2010-02-26 14:13:54 -05003723 if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
3724 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3725 "2757 Protocol failure detected during "
3726 "processing of FCP I/O op: "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02003727 "tgt x%x lun x%llx cmnd x%x rspInfo3 x%x\n",
James Smarte40a02c2010-02-26 14:13:54 -05003728 cmnd->device->id,
3729 cmnd->device->lun, cmnd->cmnd[0],
3730 fcprsp->rspInfo3);
3731 host_status = DID_ERROR;
3732 goto out;
3733 }
James Smart6a9c52c2009-10-02 15:16:51 -04003734 }
3735
James Smartc7743952006-12-02 13:34:42 -05003736 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
3737 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
3738 if (snslen > SCSI_SENSE_BUFFERSIZE)
3739 snslen = SCSI_SENSE_BUFFERSIZE;
3740
3741 if (resp_info & RSP_LEN_VALID)
3742 rsplen = be32_to_cpu(fcprsp->rspRspLen);
3743 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
3744 }
3745 lp = (uint32_t *)cmnd->sense_buffer;
3746
James Smartaa1c7ee2012-08-14 14:26:06 -04003747 /* special handling for under run conditions */
3748 if (!scsi_status && (resp_info & RESID_UNDER)) {
3749 /* don't log under runs if fcp set... */
3750 if (vport->cfg_log_verbose & LOG_FCP)
3751 logit = LOG_FCP_ERROR;
3752 /* unless operator says so */
3753 if (vport->cfg_log_verbose & LOG_FCP_UNDER)
3754 logit = LOG_FCP_UNDER;
3755 }
James Smartc7743952006-12-02 13:34:42 -05003756
James Smarte8b62012007-08-02 11:10:09 -04003757 lpfc_printf_vlog(vport, KERN_WARNING, logit,
James Smarte2a0a9d2008-12-04 22:40:02 -05003758 "9024 FCP command x%x failed: x%x SNS x%x x%x "
James Smarte8b62012007-08-02 11:10:09 -04003759 "Data: x%x x%x x%x x%x x%x\n",
3760 cmnd->cmnd[0], scsi_status,
3761 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
3762 be32_to_cpu(fcprsp->rspResId),
3763 be32_to_cpu(fcprsp->rspSnsLen),
3764 be32_to_cpu(fcprsp->rspRspLen),
3765 fcprsp->rspInfo3);
dea31012005-04-17 16:05:31 -05003766
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003767 scsi_set_resid(cmnd, 0);
James Smart5afab6b2015-12-16 18:12:01 -05003768 fcpDl = be32_to_cpu(fcpcmd->fcpDl);
dea31012005-04-17 16:05:31 -05003769 if (resp_info & RESID_UNDER) {
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003770 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea31012005-04-17 16:05:31 -05003771
James Smart73d91e52011-10-10 21:32:10 -04003772 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_UNDER,
James Smart45634a862018-01-30 15:59:00 -08003773 "9025 FCP Underrun, expected %d, "
James Smarte8b62012007-08-02 11:10:09 -04003774 "residual %d Data: x%x x%x x%x\n",
James Smart5afab6b2015-12-16 18:12:01 -05003775 fcpDl,
James Smarte8b62012007-08-02 11:10:09 -04003776 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
3777 cmnd->underflow);
dea31012005-04-17 16:05:31 -05003778
3779 /*
James Smart45634a862018-01-30 15:59:00 -08003780 * If there is an under run, check if under run reported by
James Smart7054a602007-04-25 09:52:34 -04003781 * storage array is same as the under run reported by HBA.
3782 * If this is not same, there is a dropped frame.
3783 */
James Smart45634a862018-01-30 15:59:00 -08003784 if (fcpi_parm && (scsi_get_resid(cmnd) != fcpi_parm)) {
James Smarte8b62012007-08-02 11:10:09 -04003785 lpfc_printf_vlog(vport, KERN_WARNING,
3786 LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05003787 "9026 FCP Read Check Error "
James Smarte8b62012007-08-02 11:10:09 -04003788 "and Underrun Data: x%x x%x x%x x%x\n",
James Smart5afab6b2015-12-16 18:12:01 -05003789 fcpDl,
James Smarte8b62012007-08-02 11:10:09 -04003790 scsi_get_resid(cmnd), fcpi_parm,
3791 cmnd->cmnd[0]);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003792 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
James Smart7054a602007-04-25 09:52:34 -04003793 host_status = DID_ERROR;
3794 }
3795 /*
dea31012005-04-17 16:05:31 -05003796 * The cmnd->underflow is the minimum number of bytes that must
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003797 * be transferred for this command. Provided a sense condition
dea31012005-04-17 16:05:31 -05003798 * is not present, make sure the actual amount transferred is at
3799 * least the underflow value or fail.
3800 */
3801 if (!(resp_info & SNS_LEN_VALID) &&
3802 (scsi_status == SAM_STAT_GOOD) &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003803 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
3804 < cmnd->underflow)) {
James Smarte8b62012007-08-02 11:10:09 -04003805 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05003806 "9027 FCP command x%x residual "
James Smarte8b62012007-08-02 11:10:09 -04003807 "underrun converted to error "
3808 "Data: x%x x%x x%x\n",
James Smart66dbfbe2007-08-05 06:08:38 -04003809 cmnd->cmnd[0], scsi_bufflen(cmnd),
James Smarte8b62012007-08-02 11:10:09 -04003810 scsi_get_resid(cmnd), cmnd->underflow);
dea31012005-04-17 16:05:31 -05003811 host_status = DID_ERROR;
3812 }
3813 } else if (resp_info & RESID_OVER) {
James Smarte8b62012007-08-02 11:10:09 -04003814 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05003815 "9028 FCP command x%x residual overrun error. "
James Smarte4e74272009-07-19 10:01:38 -04003816 "Data: x%x x%x\n", cmnd->cmnd[0],
James Smarte8b62012007-08-02 11:10:09 -04003817 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea31012005-04-17 16:05:31 -05003818 host_status = DID_ERROR;
3819
3820 /*
3821 * Check SLI validation that all the transfer was actually done
James Smart26373d22013-09-06 12:19:17 -04003822 * (fcpi_parm should be zero). Apply check only to reads.
dea31012005-04-17 16:05:31 -05003823 */
James Smart5afab6b2015-12-16 18:12:01 -05003824 } else if (fcpi_parm) {
James Smarte8b62012007-08-02 11:10:09 -04003825 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
James Smart5afab6b2015-12-16 18:12:01 -05003826 "9029 FCP %s Check Error xri x%x Data: "
James Smarteee88772010-09-29 11:19:08 -04003827 "x%x x%x x%x x%x x%x\n",
James Smart5afab6b2015-12-16 18:12:01 -05003828 ((cmnd->sc_data_direction == DMA_FROM_DEVICE) ?
3829 "Read" : "Write"),
3830 ((phba->sli_rev == LPFC_SLI_REV4) ?
3831 lpfc_cmd->cur_iocbq.sli4_xritag :
3832 rsp_iocb->iocb.ulpContext),
3833 fcpDl, be32_to_cpu(fcprsp->rspResId),
James Smarteee88772010-09-29 11:19:08 -04003834 fcpi_parm, cmnd->cmnd[0], scsi_status);
James Smart5afab6b2015-12-16 18:12:01 -05003835
3836 /* There is some issue with the LPe12000 that causes it
3837 * to miscalculate the fcpi_parm and falsely trip this
3838 * recovery logic. Detect this case and don't error when true.
3839 */
3840 if (fcpi_parm > fcpDl)
3841 goto out;
3842
James Smarteee88772010-09-29 11:19:08 -04003843 switch (scsi_status) {
3844 case SAM_STAT_GOOD:
3845 case SAM_STAT_CHECK_CONDITION:
3846 /* Fabric dropped a data frame. Fail any successful
3847 * command in which we detected dropped frames.
3848 * A status of good or some check conditions could
3849 * be considered a successful command.
3850 */
3851 host_status = DID_ERROR;
3852 break;
3853 }
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003854 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea31012005-04-17 16:05:31 -05003855 }
3856
3857 out:
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02003858 cmnd->result = host_status << 16 | scsi_status;
James Smartea2151b2008-09-07 11:52:10 -04003859 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
dea31012005-04-17 16:05:31 -05003860}
3861
James Smart9bad7672008-12-04 22:39:02 -05003862/**
James Smart3621a712009-04-06 18:47:14 -04003863 * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
James Smart9bad7672008-12-04 22:39:02 -05003864 * @phba: The Hba for which this call is being executed.
3865 * @pIocbIn: The command IOCBQ for the scsi cmnd.
James Smart3772a992009-05-22 14:50:54 -04003866 * @pIocbOut: The response IOCBQ for the scsi cmnd.
James Smart9bad7672008-12-04 22:39:02 -05003867 *
3868 * This routine assigns scsi command result by looking into response IOCB
3869 * status field appropriately. This routine handles QUEUE FULL condition as
3870 * well by ramping down device queue depth.
3871 **/
dea31012005-04-17 16:05:31 -05003872static void
3873lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
3874 struct lpfc_iocbq *pIocbOut)
3875{
James Smartc4908502019-01-28 11:14:28 -08003876 struct lpfc_io_buf *lpfc_cmd =
3877 (struct lpfc_io_buf *) pIocbIn->context1;
James Smart2e0fef82007-06-17 19:56:36 -05003878 struct lpfc_vport *vport = pIocbIn->vport;
dea31012005-04-17 16:05:31 -05003879 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
3880 struct lpfc_nodelist *pnode = rdata->pnode;
James Smart75baf692010-06-08 18:31:21 -04003881 struct scsi_cmnd *cmd;
James Smartfa61a542008-01-11 01:52:42 -05003882 unsigned long flags;
James Smartea2151b2008-09-07 11:52:10 -04003883 struct lpfc_fast_path_event *fast_path_evt;
James Smart75baf692010-06-08 18:31:21 -04003884 struct Scsi_Host *shost;
James Smart4c47efc2019-01-28 11:14:25 -08003885 int idx;
James Smart73d91e52011-10-10 21:32:10 -04003886 uint32_t logit = LOG_FCP;
James Smart6a828b02019-01-28 11:14:31 -08003887#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
3888 int cpu;
3889#endif
dea31012005-04-17 16:05:31 -05003890
James Smartc2017262019-01-28 11:14:37 -08003891 /* Guard against abort handler being called at same time */
3892 spin_lock(&lpfc_cmd->buf_lock);
3893
James Smart75baf692010-06-08 18:31:21 -04003894 /* Sanity check on return of outstanding command */
James Smart75baf692010-06-08 18:31:21 -04003895 cmd = lpfc_cmd->pCmd;
James Smartc2017262019-01-28 11:14:37 -08003896 if (!cmd) {
3897 lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
3898 "2621 IO completion: Not an active IO\n");
3899 spin_unlock(&lpfc_cmd->buf_lock);
James Smartc90261d2015-12-16 18:11:57 -05003900 return;
James Smartc2017262019-01-28 11:14:37 -08003901 }
James Smart4c47efc2019-01-28 11:14:25 -08003902
3903 idx = lpfc_cmd->cur_iocbq.hba_wqidx;
3904 if (phba->sli4_hba.hdwq)
3905 phba->sli4_hba.hdwq[idx].scsi_cstat.io_cmpls++;
3906
James Smart6a828b02019-01-28 11:14:31 -08003907#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
3908 if (phba->cpucheck_on & LPFC_CHECK_SCSI_IO) {
Bart Van Assched6d189c2019-03-28 11:06:22 -07003909 cpu = raw_smp_processor_id();
James Smartff6bf892019-03-12 16:30:04 -07003910 if (cpu < LPFC_CHECK_CPU_CNT && phba->sli4_hba.hdwq)
James Smart6a828b02019-01-28 11:14:31 -08003911 phba->sli4_hba.hdwq[idx].cpucheck_cmpl_io[cpu]++;
3912 }
3913#endif
James Smart75baf692010-06-08 18:31:21 -04003914 shost = cmd->device->host;
3915
James Smarte3d2b802012-08-14 14:25:43 -04003916 lpfc_cmd->result = (pIocbOut->iocb.un.ulpWord[4] & IOERR_PARAM_MASK);
dea31012005-04-17 16:05:31 -05003917 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
James Smart341af102010-01-26 23:07:37 -05003918 /* pick up SLI4 exhange busy status from HBA */
3919 lpfc_cmd->exch_busy = pIocbOut->iocb_flag & LPFC_EXCHANGE_BUSY;
3920
James Smart9a6b09c2012-03-01 22:37:42 -05003921#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
3922 if (lpfc_cmd->prot_data_type) {
3923 struct scsi_dif_tuple *src = NULL;
3924
3925 src = (struct scsi_dif_tuple *)lpfc_cmd->prot_data_segment;
3926 /*
3927 * Used to restore any changes to protection
3928 * data for error injection.
3929 */
3930 switch (lpfc_cmd->prot_data_type) {
3931 case LPFC_INJERR_REFTAG:
3932 src->ref_tag =
3933 lpfc_cmd->prot_data;
3934 break;
3935 case LPFC_INJERR_APPTAG:
3936 src->app_tag =
3937 (uint16_t)lpfc_cmd->prot_data;
3938 break;
3939 case LPFC_INJERR_GUARD:
3940 src->guard_tag =
3941 (uint16_t)lpfc_cmd->prot_data;
3942 break;
3943 default:
3944 break;
3945 }
3946
3947 lpfc_cmd->prot_data = 0;
3948 lpfc_cmd->prot_data_type = 0;
3949 lpfc_cmd->prot_data_segment = NULL;
3950 }
3951#endif
James Smart2ea259e2017-02-12 13:52:27 -08003952
dea31012005-04-17 16:05:31 -05003953 if (lpfc_cmd->status) {
3954 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
3955 (lpfc_cmd->result & IOERR_DRVR_MASK))
3956 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
3957 else if (lpfc_cmd->status >= IOSTAT_CNT)
3958 lpfc_cmd->status = IOSTAT_DEFAULT;
James Smartaa1c7ee2012-08-14 14:26:06 -04003959 if (lpfc_cmd->status == IOSTAT_FCP_RSP_ERROR &&
3960 !lpfc_cmd->fcp_rsp->rspStatus3 &&
3961 (lpfc_cmd->fcp_rsp->rspStatus2 & RESID_UNDER) &&
3962 !(vport->cfg_log_verbose & LOG_FCP_UNDER))
James Smart73d91e52011-10-10 21:32:10 -04003963 logit = 0;
3964 else
3965 logit = LOG_FCP | LOG_FCP_UNDER;
3966 lpfc_printf_vlog(vport, KERN_WARNING, logit,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02003967 "9030 FCP cmd x%x failed <%d/%lld> "
James Smart5a0d80f2012-05-09 21:18:20 -04003968 "status: x%x result: x%x "
3969 "sid: x%x did: x%x oxid: x%x "
3970 "Data: x%x x%x\n",
James Smart73d91e52011-10-10 21:32:10 -04003971 cmd->cmnd[0],
3972 cmd->device ? cmd->device->id : 0xffff,
3973 cmd->device ? cmd->device->lun : 0xffff,
3974 lpfc_cmd->status, lpfc_cmd->result,
James Smart3bf41ba2013-05-31 17:03:18 -04003975 vport->fc_myDID,
3976 (pnode) ? pnode->nlp_DID : 0,
James Smart5a0d80f2012-05-09 21:18:20 -04003977 phba->sli_rev == LPFC_SLI_REV4 ?
3978 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
James Smart73d91e52011-10-10 21:32:10 -04003979 pIocbOut->iocb.ulpContext,
3980 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
dea31012005-04-17 16:05:31 -05003981
3982 switch (lpfc_cmd->status) {
3983 case IOSTAT_FCP_RSP_ERROR:
3984 /* Call FCP RSP handler to determine result */
James Smart2e0fef82007-06-17 19:56:36 -05003985 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea31012005-04-17 16:05:31 -05003986 break;
3987 case IOSTAT_NPORT_BSY:
3988 case IOSTAT_FABRIC_BSY:
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02003989 cmd->result = DID_TRANSPORT_DISRUPTED << 16;
James Smartea2151b2008-09-07 11:52:10 -04003990 fast_path_evt = lpfc_alloc_fast_evt(phba);
3991 if (!fast_path_evt)
3992 break;
3993 fast_path_evt->un.fabric_evt.event_type =
3994 FC_REG_FABRIC_EVENT;
3995 fast_path_evt->un.fabric_evt.subcategory =
3996 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
3997 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
3998 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
3999 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
4000 &pnode->nlp_portname,
4001 sizeof(struct lpfc_name));
4002 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
4003 &pnode->nlp_nodename,
4004 sizeof(struct lpfc_name));
4005 }
4006 fast_path_evt->vport = vport;
4007 fast_path_evt->work_evt.evt =
4008 LPFC_EVT_FASTPATH_MGMT_EVT;
4009 spin_lock_irqsave(&phba->hbalock, flags);
4010 list_add_tail(&fast_path_evt->work_evt.evt_listp,
4011 &phba->work_list);
4012 spin_unlock_irqrestore(&phba->hbalock, flags);
4013 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05004014 break;
James Smart92d7f7b2007-06-17 19:56:38 -05004015 case IOSTAT_LOCAL_REJECT:
James Smart1151e3e2011-02-16 12:39:35 -05004016 case IOSTAT_REMOTE_STOP:
James Smartab56dc22011-02-16 12:39:57 -05004017 if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
4018 lpfc_cmd->result ==
4019 IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
4020 lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
4021 lpfc_cmd->result ==
4022 IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02004023 cmd->result = DID_NO_CONNECT << 16;
James Smartab56dc22011-02-16 12:39:57 -05004024 break;
4025 }
James Smartd7c255b2008-08-24 21:50:00 -04004026 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
James Smart92d7f7b2007-06-17 19:56:38 -05004027 lpfc_cmd->result == IOERR_NO_RESOURCES ||
James Smartb92938b2010-06-07 15:24:12 -04004028 lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
4029 lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02004030 cmd->result = DID_REQUEUE << 16;
James Smart58da1ff2008-04-07 10:15:56 -04004031 break;
James Smarte2a0a9d2008-12-04 22:40:02 -05004032 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004033 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
4034 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
4035 pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
4036 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
4037 /*
4038 * This is a response for a BG enabled
4039 * cmd. Parse BG error
4040 */
4041 lpfc_parse_bg_err(phba, lpfc_cmd,
4042 pIocbOut);
4043 break;
4044 } else {
4045 lpfc_printf_vlog(vport, KERN_WARNING,
4046 LOG_BG,
4047 "9031 non-zero BGSTAT "
James Smart6a9c52c2009-10-02 15:16:51 -04004048 "on unprotected cmd\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05004049 }
4050 }
James Smart1151e3e2011-02-16 12:39:35 -05004051 if ((lpfc_cmd->status == IOSTAT_REMOTE_STOP)
4052 && (phba->sli_rev == LPFC_SLI_REV4)
4053 && (pnode && NLP_CHK_NODE_ACT(pnode))) {
4054 /* This IO was aborted by the target, we don't
4055 * know the rxid and because we did not send the
4056 * ABTS we cannot generate and RRQ.
4057 */
4058 lpfc_set_rrq_active(phba, pnode,
James Smartee0f4fe2012-05-09 21:19:14 -04004059 lpfc_cmd->cur_iocbq.sli4_lxritag,
4060 0, 0);
James Smart1151e3e2011-02-16 12:39:35 -05004061 }
Bart Van Asschecd05c152019-03-28 11:06:18 -07004062 /* fall through */
dea31012005-04-17 16:05:31 -05004063 default:
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02004064 cmd->result = DID_ERROR << 16;
dea31012005-04-17 16:05:31 -05004065 break;
4066 }
4067
James Smart58da1ff2008-04-07 10:15:56 -04004068 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004069 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02004070 cmd->result = DID_TRANSPORT_DISRUPTED << 16 |
4071 SAM_STAT_BUSY;
James Smartab56dc22011-02-16 12:39:57 -05004072 } else
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02004073 cmd->result = DID_OK << 16;
dea31012005-04-17 16:05:31 -05004074
4075 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
4076 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
4077
James Smarte8b62012007-08-02 11:10:09 -04004078 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smart32350662019-08-14 16:57:06 -07004079 "0710 Iodone <%d/%llu> cmd x%px, error "
James Smarte8b62012007-08-02 11:10:09 -04004080 "x%x SNS x%x x%x Data: x%x x%x\n",
4081 cmd->device->id, cmd->device->lun, cmd,
4082 cmd->result, *lp, *(lp + 3), cmd->retries,
4083 scsi_get_resid(cmd));
dea31012005-04-17 16:05:31 -05004084 }
4085
James Smartea2151b2008-09-07 11:52:10 -04004086 lpfc_update_stats(phba, lpfc_cmd);
James Smart977b5a02008-09-07 11:52:04 -04004087 if (vport->cfg_max_scsicmpl_time &&
4088 time_after(jiffies, lpfc_cmd->start_time +
4089 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
James Smarta257bf92009-04-06 18:48:10 -04004090 spin_lock_irqsave(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05004091 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
4092 if (pnode->cmd_qdepth >
4093 atomic_read(&pnode->cmd_pending) &&
4094 (atomic_read(&pnode->cmd_pending) >
4095 LPFC_MIN_TGT_QDEPTH) &&
4096 ((cmd->cmnd[0] == READ_10) ||
4097 (cmd->cmnd[0] == WRITE_10)))
4098 pnode->cmd_qdepth =
4099 atomic_read(&pnode->cmd_pending);
James Smart977b5a02008-09-07 11:52:04 -04004100
James Smart109f6ed2008-12-04 22:39:08 -05004101 pnode->last_change_time = jiffies;
4102 }
James Smarta257bf92009-04-06 18:48:10 -04004103 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart977b5a02008-09-07 11:52:04 -04004104 }
James Smart1dcb58e2007-04-25 09:51:30 -04004105 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James Smarta257bf92009-04-06 18:48:10 -04004106
James Smartc2017262019-01-28 11:14:37 -08004107 lpfc_cmd->pCmd = NULL;
4108 spin_unlock(&lpfc_cmd->buf_lock);
James Smart92e3af62012-08-14 14:26:28 -04004109
James Smart89533e92016-10-13 15:06:15 -07004110 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
4111 cmd->scsi_done(cmd);
4112
James Smartfa61a542008-01-11 01:52:42 -05004113 /*
James Smartc2017262019-01-28 11:14:37 -08004114 * If there is an abort thread waiting for command completion
James Smartfa61a542008-01-11 01:52:42 -05004115 * wake up the thread.
4116 */
James Smartc2017262019-01-28 11:14:37 -08004117 spin_lock(&lpfc_cmd->buf_lock);
James Smart4eb01532019-04-03 11:10:34 -07004118 lpfc_cmd->cur_iocbq.iocb_flag &= ~LPFC_DRIVER_ABORTED;
James Smartb9e5a2d2019-05-21 17:49:01 -07004119 if (lpfc_cmd->waitq)
James Smart4eb01532019-04-03 11:10:34 -07004120 wake_up(lpfc_cmd->waitq);
James Smartc2017262019-01-28 11:14:37 -08004121 spin_unlock(&lpfc_cmd->buf_lock);
James Smartfa61a542008-01-11 01:52:42 -05004122
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004123 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004124}
4125
James Smart34b02dc2008-08-24 21:49:55 -04004126/**
Alexey Kardashevskiy8b2564e2015-04-28 18:26:22 +10004127 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
4128 * @data: A pointer to the immediate command data portion of the IOCB.
4129 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
4130 *
4131 * The routine copies the entire FCP command from @fcp_cmnd to @data while
4132 * byte swapping the data to big endian format for transmission on the wire.
4133 **/
4134static void
4135lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
4136{
4137 int i, j;
4138 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
4139 i += sizeof(uint32_t), j++) {
4140 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
4141 }
4142}
4143
4144/**
James Smartf1126682009-06-10 17:22:44 -04004145 * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05004146 * @vport: The virtual port for which this call is being executed.
4147 * @lpfc_cmd: The scsi command which needs to send.
4148 * @pnode: Pointer to lpfc_nodelist.
4149 *
4150 * This routine initializes fcp_cmnd and iocb data structure from scsi command
James Smart3772a992009-05-22 14:50:54 -04004151 * to transfer for device with SLI3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05004152 **/
dea31012005-04-17 16:05:31 -05004153static void
James Smartc4908502019-01-28 11:14:28 -08004154lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_io_buf *lpfc_cmd,
James Smart2e0fef82007-06-17 19:56:36 -05004155 struct lpfc_nodelist *pnode)
dea31012005-04-17 16:05:31 -05004156{
James Smart2e0fef82007-06-17 19:56:36 -05004157 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004158 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
4159 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
4160 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
4161 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
James Smart4c47efc2019-01-28 11:14:25 -08004162 struct lpfc_sli4_hdw_queue *hdwq = NULL;
dea31012005-04-17 16:05:31 -05004163 int datadir = scsi_cmnd->sc_data_direction;
James Smart4c47efc2019-01-28 11:14:25 -08004164 int idx;
James Smart027140e2012-08-03 12:35:44 -04004165 uint8_t *ptr;
4166 bool sli4;
James Smart98bbf5f2013-09-06 12:18:45 -04004167 uint32_t fcpdl;
dea31012005-04-17 16:05:31 -05004168
James Smart58da1ff2008-04-07 10:15:56 -04004169 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
4170 return;
4171
dea31012005-04-17 16:05:31 -05004172 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
James.Smart@Emulex.Com69859dc2005-08-10 15:02:37 -04004173 /* clear task management bits */
4174 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea31012005-04-17 16:05:31 -05004175
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -04004176 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
4177 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05004178
James Smart027140e2012-08-03 12:35:44 -04004179 ptr = &fcp_cmnd->fcpCdb[0];
4180 memcpy(ptr, scsi_cmnd->cmnd, scsi_cmnd->cmd_len);
4181 if (scsi_cmnd->cmd_len < LPFC_FCP_CDB_LEN) {
4182 ptr += scsi_cmnd->cmd_len;
4183 memset(ptr, 0, (LPFC_FCP_CDB_LEN - scsi_cmnd->cmd_len));
4184 }
4185
Christoph Hellwig50668632014-10-30 14:30:06 +01004186 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
dea31012005-04-17 16:05:31 -05004187
James Smart027140e2012-08-03 12:35:44 -04004188 sli4 = (phba->sli_rev == LPFC_SLI_REV4);
James Smart6acb3482014-04-04 13:51:25 -04004189 piocbq->iocb.un.fcpi.fcpi_XRdy = 0;
James Smart1fbf9742019-01-28 11:14:26 -08004190 idx = lpfc_cmd->hdwq_no;
James Smart4c47efc2019-01-28 11:14:25 -08004191 if (phba->sli4_hba.hdwq)
4192 hdwq = &phba->sli4_hba.hdwq[idx];
James Smart027140e2012-08-03 12:35:44 -04004193
dea31012005-04-17 16:05:31 -05004194 /*
4195 * There are three possibilities here - use scatter-gather segment, use
4196 * the single mapping, or neither. Start the lpfc command prep by
4197 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
4198 * data bde entry.
4199 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05004200 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05004201 if (datadir == DMA_TO_DEVICE) {
4202 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
James Smart182ba752013-07-15 18:34:05 -04004203 iocb_cmd->ulpPU = PARM_READ_CHECK;
James Smart3cb01c52013-07-15 18:35:04 -04004204 if (vport->cfg_first_burst_size &&
4205 (pnode->nlp_flag & NLP_FIRSTBURST)) {
James Smart98bbf5f2013-09-06 12:18:45 -04004206 fcpdl = scsi_bufflen(scsi_cmnd);
4207 if (fcpdl < vport->cfg_first_burst_size)
4208 piocbq->iocb.un.fcpi.fcpi_XRdy = fcpdl;
4209 else
4210 piocbq->iocb.un.fcpi.fcpi_XRdy =
4211 vport->cfg_first_burst_size;
James Smart3cb01c52013-07-15 18:35:04 -04004212 }
dea31012005-04-17 16:05:31 -05004213 fcp_cmnd->fcpCntl3 = WRITE_DATA;
James Smart4c47efc2019-01-28 11:14:25 -08004214 if (hdwq)
4215 hdwq->scsi_cstat.output_requests++;
dea31012005-04-17 16:05:31 -05004216 } else {
4217 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
4218 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05004219 fcp_cmnd->fcpCntl3 = READ_DATA;
James Smart4c47efc2019-01-28 11:14:25 -08004220 if (hdwq)
4221 hdwq->scsi_cstat.input_requests++;
dea31012005-04-17 16:05:31 -05004222 }
4223 } else {
4224 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
4225 iocb_cmd->un.fcpi.fcpi_parm = 0;
4226 iocb_cmd->ulpPU = 0;
4227 fcp_cmnd->fcpCntl3 = 0;
James Smart4c47efc2019-01-28 11:14:25 -08004228 if (hdwq)
4229 hdwq->scsi_cstat.control_requests++;
dea31012005-04-17 16:05:31 -05004230 }
Alexey Kardashevskiy8b2564e2015-04-28 18:26:22 +10004231 if (phba->sli_rev == 3 &&
4232 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
4233 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05004234 /*
4235 * Finish initializing those IOCB fields that are independent
4236 * of the scsi_cmnd request_buffer
4237 */
4238 piocbq->iocb.ulpContext = pnode->nlp_rpi;
James Smart027140e2012-08-03 12:35:44 -04004239 if (sli4)
James Smart6d368e52011-05-24 11:44:12 -04004240 piocbq->iocb.ulpContext =
4241 phba->sli4_hba.rpi_ids[pnode->nlp_rpi];
dea31012005-04-17 16:05:31 -05004242 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
4243 piocbq->iocb.ulpFCP2Rcvy = 1;
James Smart09372822008-01-11 01:52:54 -05004244 else
4245 piocbq->iocb.ulpFCP2Rcvy = 0;
dea31012005-04-17 16:05:31 -05004246
4247 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
4248 piocbq->context1 = lpfc_cmd;
4249 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
4250 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
James Smart2e0fef82007-06-17 19:56:36 -05004251 piocbq->vport = vport;
dea31012005-04-17 16:05:31 -05004252}
4253
James Smart9bad7672008-12-04 22:39:02 -05004254/**
James Smart6d368e52011-05-24 11:44:12 -04004255 * lpfc_scsi_prep_task_mgmt_cmd - Convert SLI3 scsi TM cmd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05004256 * @vport: The virtual port for which this call is being executed.
James Smartc4908502019-01-28 11:14:28 -08004257 * @lpfc_cmd: Pointer to lpfc_io_buf data structure.
James Smart9bad7672008-12-04 22:39:02 -05004258 * @lun: Logical unit number.
4259 * @task_mgmt_cmd: SCSI task management command.
4260 *
James Smart3772a992009-05-22 14:50:54 -04004261 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
4262 * for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05004263 *
4264 * Return codes:
4265 * 0 - Error
4266 * 1 - Success
4267 **/
dea31012005-04-17 16:05:31 -05004268static int
James Smartf1126682009-06-10 17:22:44 -04004269lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
James Smartc4908502019-01-28 11:14:28 -08004270 struct lpfc_io_buf *lpfc_cmd,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004271 uint64_t lun,
dea31012005-04-17 16:05:31 -05004272 uint8_t task_mgmt_cmd)
4273{
dea31012005-04-17 16:05:31 -05004274 struct lpfc_iocbq *piocbq;
4275 IOCB_t *piocb;
4276 struct fcp_cmnd *fcp_cmnd;
James Smart0b18ac42006-05-01 21:50:40 -04004277 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea31012005-04-17 16:05:31 -05004278 struct lpfc_nodelist *ndlp = rdata->pnode;
4279
James Smart58da1ff2008-04-07 10:15:56 -04004280 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
4281 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05004282 return 0;
dea31012005-04-17 16:05:31 -05004283
dea31012005-04-17 16:05:31 -05004284 piocbq = &(lpfc_cmd->cur_iocbq);
James Smart2e0fef82007-06-17 19:56:36 -05004285 piocbq->vport = vport;
4286
dea31012005-04-17 16:05:31 -05004287 piocb = &piocbq->iocb;
4288
4289 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart34b02dc2008-08-24 21:49:55 -04004290 /* Clear out any old data in the FCP command area */
4291 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
4292 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05004293 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
James Smarte2a0a9d2008-12-04 22:40:02 -05004294 if (vport->phba->sli_rev == 3 &&
4295 !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04004296 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05004297 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
dea31012005-04-17 16:05:31 -05004298 piocb->ulpContext = ndlp->nlp_rpi;
James Smart6d368e52011-05-24 11:44:12 -04004299 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
4300 piocb->ulpContext =
4301 vport->phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
4302 }
James Smart53151bb2013-10-10 12:24:07 -04004303 piocb->ulpFCP2Rcvy = (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) ? 1 : 0;
dea31012005-04-17 16:05:31 -05004304 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
James Smartf9226c22014-02-20 09:57:28 -05004305 piocb->ulpPU = 0;
4306 piocb->un.fcpi.fcpi_parm = 0;
dea31012005-04-17 16:05:31 -05004307
4308 /* ulpTimeout is only one byte */
4309 if (lpfc_cmd->timeout > 0xff) {
4310 /*
4311 * Do not timeout the command at the firmware level.
4312 * The driver will provide the timeout mechanism.
4313 */
4314 piocb->ulpTimeout = 0;
James Smartf1126682009-06-10 17:22:44 -04004315 } else
dea31012005-04-17 16:05:31 -05004316 piocb->ulpTimeout = lpfc_cmd->timeout;
James Smartf1126682009-06-10 17:22:44 -04004317
4318 if (vport->phba->sli_rev == LPFC_SLI_REV4)
4319 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004320
James Smart2e0fef82007-06-17 19:56:36 -05004321 return 1;
dea31012005-04-17 16:05:31 -05004322}
4323
James Smart9bad7672008-12-04 22:39:02 -05004324/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004325 * lpfc_scsi_api_table_setup - Set up scsi api function jump table
James Smart3772a992009-05-22 14:50:54 -04004326 * @phba: The hba struct for which this call is being executed.
4327 * @dev_grp: The HBA PCI-Device group number.
4328 *
4329 * This routine sets up the SCSI interface API function jump table in @phba
4330 * struct.
4331 * Returns: 0 - success, -ENODEV - failure.
4332 **/
4333int
4334lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4335{
4336
James Smartf1126682009-06-10 17:22:44 -04004337 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
4338 phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
James Smartf1126682009-06-10 17:22:44 -04004339
James Smart3772a992009-05-22 14:50:54 -04004340 switch (dev_grp) {
4341 case LPFC_PCI_DEV_LP:
James Smart3772a992009-05-22 14:50:54 -04004342 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
James Smartacd68592012-01-18 16:25:09 -05004343 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04004344 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
James Smart19ca7602010-11-20 23:11:55 -05004345 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04004346 break;
James Smartda0436e2009-05-22 14:51:39 -04004347 case LPFC_PCI_DEV_OC:
James Smartda0436e2009-05-22 14:51:39 -04004348 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
James Smartacd68592012-01-18 16:25:09 -05004349 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04004350 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
James Smart19ca7602010-11-20 23:11:55 -05004351 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04004352 break;
James Smart3772a992009-05-22 14:50:54 -04004353 default:
4354 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4355 "1418 Invalid HBA PCI-device group: 0x%x\n",
4356 dev_grp);
4357 return -ENODEV;
4358 break;
4359 }
James Smart3772a992009-05-22 14:50:54 -04004360 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
James Smart84d1b002010-02-12 14:42:33 -05004361 phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
James Smart3772a992009-05-22 14:50:54 -04004362 return 0;
4363}
4364
4365/**
James Smart3621a712009-04-06 18:47:14 -04004366 * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
James Smart9bad7672008-12-04 22:39:02 -05004367 * @phba: The Hba for which this call is being executed.
4368 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
4369 * @rspiocbq: Pointer to lpfc_iocbq data structure.
4370 *
4371 * This routine is IOCB completion routine for device reset and target reset
4372 * routine. This routine release scsi buffer associated with lpfc_cmd.
4373 **/
James Smart7054a602007-04-25 09:52:34 -04004374static void
4375lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
4376 struct lpfc_iocbq *cmdiocbq,
4377 struct lpfc_iocbq *rspiocbq)
4378{
James Smartc4908502019-01-28 11:14:28 -08004379 struct lpfc_io_buf *lpfc_cmd =
4380 (struct lpfc_io_buf *) cmdiocbq->context1;
James Smart7054a602007-04-25 09:52:34 -04004381 if (lpfc_cmd)
4382 lpfc_release_scsi_buf(phba, lpfc_cmd);
4383 return;
4384}
4385
James Smart9bad7672008-12-04 22:39:02 -05004386/**
James Smart50212672018-12-13 15:17:57 -08004387 * lpfc_check_pci_resettable - Walks list of devices on pci_dev's bus to check
4388 * if issuing a pci_bus_reset is possibly unsafe
4389 * @phba: lpfc_hba pointer.
4390 *
4391 * Description:
4392 * Walks the bus_list to ensure only PCI devices with Emulex
4393 * vendor id, device ids that support hot reset, and only one occurrence
4394 * of function 0.
4395 *
4396 * Returns:
4397 * -EBADSLT, detected invalid device
4398 * 0, successful
4399 */
4400int
4401lpfc_check_pci_resettable(const struct lpfc_hba *phba)
4402{
4403 const struct pci_dev *pdev = phba->pcidev;
4404 struct pci_dev *ptr = NULL;
4405 u8 counter = 0;
4406
4407 /* Walk the list of devices on the pci_dev's bus */
4408 list_for_each_entry(ptr, &pdev->bus->devices, bus_list) {
4409 /* Check for Emulex Vendor ID */
4410 if (ptr->vendor != PCI_VENDOR_ID_EMULEX) {
4411 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4412 "8346 Non-Emulex vendor found: "
4413 "0x%04x\n", ptr->vendor);
4414 return -EBADSLT;
4415 }
4416
4417 /* Check for valid Emulex Device ID */
4418 switch (ptr->device) {
4419 case PCI_DEVICE_ID_LANCER_FC:
4420 case PCI_DEVICE_ID_LANCER_G6_FC:
4421 case PCI_DEVICE_ID_LANCER_G7_FC:
4422 break;
4423 default:
4424 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4425 "8347 Invalid device found: "
4426 "0x%04x\n", ptr->device);
4427 return -EBADSLT;
4428 }
4429
4430 /* Check for only one function 0 ID to ensure only one HBA on
4431 * secondary bus
4432 */
4433 if (ptr->devfn == 0) {
4434 if (++counter > 1) {
4435 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4436 "8348 More than one device on "
4437 "secondary bus found\n");
4438 return -EBADSLT;
4439 }
4440 }
4441 }
4442
4443 return 0;
4444}
4445
4446/**
James Smart3621a712009-04-06 18:47:14 -04004447 * lpfc_info - Info entry point of scsi_host_template data structure
James Smart9bad7672008-12-04 22:39:02 -05004448 * @host: The scsi host for which this call is being executed.
4449 *
4450 * This routine provides module information about hba.
4451 *
4452 * Reutrn code:
4453 * Pointer to char - Success.
4454 **/
dea31012005-04-17 16:05:31 -05004455const char *
4456lpfc_info(struct Scsi_Host *host)
4457{
James Smart2e0fef82007-06-17 19:56:36 -05004458 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
4459 struct lpfc_hba *phba = vport->phba;
James Smart50212672018-12-13 15:17:57 -08004460 int link_speed = 0;
4461 static char lpfcinfobuf[384];
4462 char tmp[384] = {0};
dea31012005-04-17 16:05:31 -05004463
James Smart50212672018-12-13 15:17:57 -08004464 memset(lpfcinfobuf, 0, sizeof(lpfcinfobuf));
dea31012005-04-17 16:05:31 -05004465 if (phba && phba->pcidev){
James Smart50212672018-12-13 15:17:57 -08004466 /* Model Description */
4467 scnprintf(tmp, sizeof(tmp), phba->ModelDesc);
4468 if (strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf)) >=
4469 sizeof(lpfcinfobuf))
4470 goto buffer_done;
4471
4472 /* PCI Info */
4473 scnprintf(tmp, sizeof(tmp),
4474 " on PCI bus %02x device %02x irq %d",
4475 phba->pcidev->bus->number, phba->pcidev->devfn,
4476 phba->pcidev->irq);
4477 if (strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf)) >=
4478 sizeof(lpfcinfobuf))
4479 goto buffer_done;
4480
4481 /* Port Number */
dea31012005-04-17 16:05:31 -05004482 if (phba->Port[0]) {
James Smart50212672018-12-13 15:17:57 -08004483 scnprintf(tmp, sizeof(tmp), " port %s", phba->Port);
4484 if (strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf)) >=
4485 sizeof(lpfcinfobuf))
4486 goto buffer_done;
dea31012005-04-17 16:05:31 -05004487 }
James Smart50212672018-12-13 15:17:57 -08004488
4489 /* Link Speed */
James Smarta085e872015-12-16 18:12:02 -05004490 link_speed = lpfc_sli_port_speed_get(phba);
James Smart50212672018-12-13 15:17:57 -08004491 if (link_speed != 0) {
4492 scnprintf(tmp, sizeof(tmp),
4493 " Logical Link Speed: %d Mbps", link_speed);
4494 if (strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf)) >=
4495 sizeof(lpfcinfobuf))
4496 goto buffer_done;
4497 }
4498
4499 /* PCI resettable */
4500 if (!lpfc_check_pci_resettable(phba)) {
4501 scnprintf(tmp, sizeof(tmp), " PCI resettable");
4502 strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf));
4503 }
dea31012005-04-17 16:05:31 -05004504 }
James Smart50212672018-12-13 15:17:57 -08004505
4506buffer_done:
dea31012005-04-17 16:05:31 -05004507 return lpfcinfobuf;
4508}
4509
James Smart9bad7672008-12-04 22:39:02 -05004510/**
James Smart3621a712009-04-06 18:47:14 -04004511 * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
James Smart9bad7672008-12-04 22:39:02 -05004512 * @phba: The Hba for which this call is being executed.
4513 *
4514 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
4515 * The default value of cfg_poll_tmo is 10 milliseconds.
4516 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004517static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
4518{
4519 unsigned long poll_tmo_expires =
4520 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
4521
James Smart895427b2017-02-12 13:52:30 -08004522 if (!list_empty(&phba->sli.sli3_ring[LPFC_FCP_RING].txcmplq))
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004523 mod_timer(&phba->fcp_poll_timer,
4524 poll_tmo_expires);
4525}
4526
James Smart9bad7672008-12-04 22:39:02 -05004527/**
James Smart3621a712009-04-06 18:47:14 -04004528 * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
James Smart9bad7672008-12-04 22:39:02 -05004529 * @phba: The Hba for which this call is being executed.
4530 *
4531 * This routine starts the fcp_poll_timer of @phba.
4532 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004533void lpfc_poll_start_timer(struct lpfc_hba * phba)
4534{
4535 lpfc_poll_rearm_timer(phba);
4536}
4537
James Smart9bad7672008-12-04 22:39:02 -05004538/**
James Smart3621a712009-04-06 18:47:14 -04004539 * lpfc_poll_timeout - Restart polling timer
James Smart9bad7672008-12-04 22:39:02 -05004540 * @ptr: Map to lpfc_hba data structure pointer.
4541 *
4542 * This routine restarts fcp_poll timer, when FCP ring polling is enable
4543 * and FCP Ring interrupt is disable.
4544 **/
4545
Kees Cookf22eb4d2017-09-06 20:24:26 -07004546void lpfc_poll_timeout(struct timer_list *t)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004547{
Kees Cookf22eb4d2017-09-06 20:24:26 -07004548 struct lpfc_hba *phba = from_timer(phba, t, fcp_poll_timer);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004549
4550 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004551 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -08004552 &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
James Smart45ed1192009-10-02 15:17:02 -04004553
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004554 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4555 lpfc_poll_rearm_timer(phba);
4556 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004557}
4558
James Smart9bad7672008-12-04 22:39:02 -05004559/**
James Smart3621a712009-04-06 18:47:14 -04004560 * lpfc_queuecommand - scsi_host_template queuecommand entry point
James Smart9bad7672008-12-04 22:39:02 -05004561 * @cmnd: Pointer to scsi_cmnd data structure.
4562 * @done: Pointer to done routine.
4563 *
4564 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
4565 * This routine prepares an IOCB from scsi command and provides to firmware.
4566 * The @done callback is invoked after driver finished processing the command.
4567 *
4568 * Return value :
4569 * 0 - Success
4570 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
4571 **/
dea31012005-04-17 16:05:31 -05004572static int
James Smartb9a7c632012-08-03 12:35:24 -04004573lpfc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004574{
James Smart2e0fef82007-06-17 19:56:36 -05004575 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4576 struct lpfc_hba *phba = vport->phba;
James Smart1ba981f2014-02-20 09:56:45 -05004577 struct lpfc_rport_data *rdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05004578 struct lpfc_nodelist *ndlp;
James Smartc4908502019-01-28 11:14:28 -08004579 struct lpfc_io_buf *lpfc_cmd;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004580 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
James Smart4c47efc2019-01-28 11:14:25 -08004581 int err, idx;
James Smart6a828b02019-01-28 11:14:31 -08004582#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4583 int cpu;
4584#endif
dea31012005-04-17 16:05:31 -05004585
James Smart1ba981f2014-02-20 09:56:45 -05004586 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smartb0e83012018-06-26 08:24:29 -07004587
4588 /* sanity check on references */
4589 if (unlikely(!rdata) || unlikely(!rport))
4590 goto out_fail_command;
4591
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004592 err = fc_remote_port_chkready(rport);
4593 if (err) {
4594 cmnd->result = err;
dea31012005-04-17 16:05:31 -05004595 goto out_fail_command;
4596 }
James Smart1c6f4ef52009-11-18 15:40:49 -05004597 ndlp = rdata->pnode;
dea31012005-04-17 16:05:31 -05004598
James Smartbf086112011-08-21 21:48:13 -04004599 if ((scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) &&
James Smartacd68592012-01-18 16:25:09 -05004600 (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED))) {
James Smarte2a0a9d2008-12-04 22:40:02 -05004601
James Smart6a9c52c2009-10-02 15:16:51 -04004602 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4603 "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
4604 " op:%02x str=%s without registering for"
4605 " BlockGuard - Rejecting command\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05004606 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
4607 dif_op_str[scsi_get_prot_op(cmnd)]);
4608 goto out_fail_command;
4609 }
4610
dea31012005-04-17 16:05:31 -05004611 /*
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004612 * Catch race where our node has transitioned, but the
4613 * transport is still transitioning.
dea31012005-04-17 16:05:31 -05004614 */
James Smart6b415f52012-06-12 13:54:59 -04004615 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
4616 goto out_tgt_busy;
James Smart2a5b7d62018-07-31 17:23:22 -07004617 if (lpfc_ndlp_check_qdepth(phba, ndlp)) {
4618 if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth) {
4619 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_ERROR,
4620 "3377 Target Queue Full, scsi Id:%d "
4621 "Qdepth:%d Pending command:%d"
4622 " WWNN:%02x:%02x:%02x:%02x:"
4623 "%02x:%02x:%02x:%02x, "
4624 " WWPN:%02x:%02x:%02x:%02x:"
4625 "%02x:%02x:%02x:%02x",
4626 ndlp->nlp_sid, ndlp->cmd_qdepth,
4627 atomic_read(&ndlp->cmd_pending),
4628 ndlp->nlp_nodename.u.wwn[0],
4629 ndlp->nlp_nodename.u.wwn[1],
4630 ndlp->nlp_nodename.u.wwn[2],
4631 ndlp->nlp_nodename.u.wwn[3],
4632 ndlp->nlp_nodename.u.wwn[4],
4633 ndlp->nlp_nodename.u.wwn[5],
4634 ndlp->nlp_nodename.u.wwn[6],
4635 ndlp->nlp_nodename.u.wwn[7],
4636 ndlp->nlp_portname.u.wwn[0],
4637 ndlp->nlp_portname.u.wwn[1],
4638 ndlp->nlp_portname.u.wwn[2],
4639 ndlp->nlp_portname.u.wwn[3],
4640 ndlp->nlp_portname.u.wwn[4],
4641 ndlp->nlp_portname.u.wwn[5],
4642 ndlp->nlp_portname.u.wwn[6],
4643 ndlp->nlp_portname.u.wwn[7]);
4644 goto out_tgt_busy;
4645 }
James Smart64bf0092018-01-30 15:58:53 -08004646 }
James Smartf91bc592018-04-09 14:24:22 -07004647
James Smartace44e42019-01-28 11:14:27 -08004648 lpfc_cmd = lpfc_get_scsi_buf(phba, ndlp, cmnd);
dea31012005-04-17 16:05:31 -05004649 if (lpfc_cmd == NULL) {
James Smarteaf15d52008-12-04 22:39:29 -05004650 lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05004651
James Smart895427b2017-02-12 13:52:30 -08004652 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_ERROR,
James Smarte8b62012007-08-02 11:10:09 -04004653 "0707 driver's buffer pool is empty, "
4654 "IO busied\n");
dea31012005-04-17 16:05:31 -05004655 goto out_host_busy;
4656 }
4657
4658 /*
4659 * Store the midlayer's command structure for the completion phase
4660 * and complete the command initialization.
4661 */
4662 lpfc_cmd->pCmd = cmnd;
4663 lpfc_cmd->rdata = rdata;
James Smart2a5b7d62018-07-31 17:23:22 -07004664 lpfc_cmd->ndlp = ndlp;
dea31012005-04-17 16:05:31 -05004665 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
dea31012005-04-17 16:05:31 -05004666
James Smarte2a0a9d2008-12-04 22:40:02 -05004667 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
James Smart6a9c52c2009-10-02 15:16:51 -04004668 if (vport->phba->cfg_enable_bg) {
James Smart737d4242013-04-17 20:14:49 -04004669 lpfc_printf_vlog(vport,
4670 KERN_INFO, LOG_SCSI_CMD,
James Smart26134702012-08-14 14:25:50 -04004671 "9033 BLKGRD: rcvd %s cmd:x%x "
4672 "sector x%llx cnt %u pt %x\n",
4673 dif_op_str[scsi_get_prot_op(cmnd)],
4674 cmnd->cmnd[0],
4675 (unsigned long long)scsi_get_lba(cmnd),
4676 blk_rq_sectors(cmnd->request),
4677 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004678 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004679 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
4680 } else {
James Smart6a9c52c2009-10-02 15:16:51 -04004681 if (vport->phba->cfg_enable_bg) {
James Smart737d4242013-04-17 20:14:49 -04004682 lpfc_printf_vlog(vport,
4683 KERN_INFO, LOG_SCSI_CMD,
James Smart26134702012-08-14 14:25:50 -04004684 "9038 BLKGRD: rcvd PROT_NORMAL cmd: "
4685 "x%x sector x%llx cnt %u pt %x\n",
4686 cmnd->cmnd[0],
4687 (unsigned long long)scsi_get_lba(cmnd),
James Smart9a6b09c2012-03-01 22:37:42 -05004688 blk_rq_sectors(cmnd->request),
James Smart26134702012-08-14 14:25:50 -04004689 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004690 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004691 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
4692 }
4693
James Smart5e0e2312019-08-14 16:56:54 -07004694 if (err == 2) {
4695 cmnd->result = DID_ERROR << 16;
4696 goto out_fail_command_release_buf;
4697 } else if (err) {
dea31012005-04-17 16:05:31 -05004698 goto out_host_busy_free_buf;
James Smart5e0e2312019-08-14 16:56:54 -07004699 }
dea31012005-04-17 16:05:31 -05004700
James Smart2e0fef82007-06-17 19:56:36 -05004701 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea31012005-04-17 16:05:31 -05004702
James Smart6a828b02019-01-28 11:14:31 -08004703#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4704 if (phba->cpucheck_on & LPFC_CHECK_SCSI_IO) {
Bart Van Assched6d189c2019-03-28 11:06:22 -07004705 cpu = raw_smp_processor_id();
James Smart6a828b02019-01-28 11:14:31 -08004706 if (cpu < LPFC_CHECK_CPU_CNT) {
4707 struct lpfc_sli4_hdw_queue *hdwq =
4708 &phba->sli4_hba.hdwq[lpfc_cmd->hdwq_no];
4709 hdwq->cpucheck_xmt_io[cpu]++;
4710 }
4711 }
4712#endif
James Smart3772a992009-05-22 14:50:54 -04004713 err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
James Smart92d7f7b2007-06-17 19:56:38 -05004714 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
James Smarteaf15d52008-12-04 22:39:29 -05004715 if (err) {
James Smart76f96b62013-12-17 20:29:01 -05004716 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4717 "3376 FCP could not issue IOCB err %x"
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004718 "FCP cmd x%x <%d/%llu> "
James Smart76f96b62013-12-17 20:29:01 -05004719 "sid: x%x did: x%x oxid: x%x "
4720 "Data: x%x x%x x%x x%x\n",
4721 err, cmnd->cmnd[0],
4722 cmnd->device ? cmnd->device->id : 0xffff,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004723 cmnd->device ? cmnd->device->lun : (u64) -1,
James Smart76f96b62013-12-17 20:29:01 -05004724 vport->fc_myDID, ndlp->nlp_DID,
4725 phba->sli_rev == LPFC_SLI_REV4 ?
4726 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
4727 lpfc_cmd->cur_iocbq.iocb.ulpContext,
4728 lpfc_cmd->cur_iocbq.iocb.ulpIoTag,
4729 lpfc_cmd->cur_iocbq.iocb.ulpTimeout,
4730 (uint32_t)
4731 (cmnd->request->timeout / 1000));
4732
dea31012005-04-17 16:05:31 -05004733 goto out_host_busy_free_buf;
James Smarteaf15d52008-12-04 22:39:29 -05004734 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004735 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004736 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -08004737 &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
James Smart45ed1192009-10-02 15:17:02 -04004738
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004739 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4740 lpfc_poll_rearm_timer(phba);
4741 }
4742
James Smartc4908502019-01-28 11:14:28 -08004743 if (phba->cfg_xri_rebalancing)
4744 lpfc_keep_pvt_pool_above_lowwm(phba, lpfc_cmd->hdwq_no);
4745
dea31012005-04-17 16:05:31 -05004746 return 0;
4747
4748 out_host_busy_free_buf:
James Smart1fbf9742019-01-28 11:14:26 -08004749 idx = lpfc_cmd->hdwq_no;
James Smartbcf4dbf2006-07-06 15:50:08 -04004750 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James Smart4c47efc2019-01-28 11:14:25 -08004751 if (phba->sli4_hba.hdwq) {
4752 switch (lpfc_cmd->fcp_cmnd->fcpCntl3) {
4753 case WRITE_DATA:
4754 phba->sli4_hba.hdwq[idx].scsi_cstat.output_requests--;
4755 break;
4756 case READ_DATA:
4757 phba->sli4_hba.hdwq[idx].scsi_cstat.input_requests--;
4758 break;
4759 default:
4760 phba->sli4_hba.hdwq[idx].scsi_cstat.control_requests--;
4761 }
4762 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004763 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004764 out_host_busy:
4765 return SCSI_MLQUEUE_HOST_BUSY;
4766
Mike Christie34963432011-02-25 14:04:28 -06004767 out_tgt_busy:
4768 return SCSI_MLQUEUE_TARGET_BUSY;
4769
James Smart5e0e2312019-08-14 16:56:54 -07004770 out_fail_command_release_buf:
4771 lpfc_release_scsi_buf(phba, lpfc_cmd);
4772
dea31012005-04-17 16:05:31 -05004773 out_fail_command:
James Smartb9a7c632012-08-03 12:35:24 -04004774 cmnd->scsi_done(cmnd);
dea31012005-04-17 16:05:31 -05004775 return 0;
4776}
4777
Jeff Garzikf2812332010-11-16 02:10:29 -05004778
James Smart9bad7672008-12-04 22:39:02 -05004779/**
James Smart3621a712009-04-06 18:47:14 -04004780 * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05004781 * @cmnd: Pointer to scsi_cmnd data structure.
4782 *
4783 * This routine aborts @cmnd pending in base driver.
4784 *
4785 * Return code :
4786 * 0x2003 - Error
4787 * 0x2002 - Success
4788 **/
dea31012005-04-17 16:05:31 -05004789static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05004790lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004791{
James Smart2e0fef82007-06-17 19:56:36 -05004792 struct Scsi_Host *shost = cmnd->device->host;
4793 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4794 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004795 struct lpfc_iocbq *iocb;
4796 struct lpfc_iocbq *abtsiocb;
James Smartc4908502019-01-28 11:14:28 -08004797 struct lpfc_io_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05004798 IOCB_t *cmd, *icmd;
James Smart3a707302012-06-12 13:54:42 -04004799 int ret = SUCCESS, status = 0;
James Smart8931c732018-07-31 17:23:20 -07004800 struct lpfc_sli_ring *pring_s4 = NULL;
James Smart895427b2017-02-12 13:52:30 -08004801 int ret_val;
James Smart59c68ea2018-04-09 14:24:25 -07004802 unsigned long flags;
James Smartfa61a542008-01-11 01:52:42 -05004803 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004804
James Smart3a707302012-06-12 13:54:42 -04004805 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04004806 if (status != 0 && status != SUCCESS)
James Smart3a707302012-06-12 13:54:42 -04004807 return status;
James Smart4f2e66c2012-05-09 21:17:07 -04004808
James Smartc2017262019-01-28 11:14:37 -08004809 lpfc_cmd = (struct lpfc_io_buf *)cmnd->host_scribble;
4810 if (!lpfc_cmd)
4811 return ret;
4812
James Smart876dd7d2012-09-29 11:31:28 -04004813 spin_lock_irqsave(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004814 /* driver queued commands are in process of being flushed */
James Smartc00f62e2019-08-14 16:57:11 -07004815 if (phba->hba_flag & HBA_IOQ_FLUSH) {
James Smart4f2e66c2012-05-09 21:17:07 -04004816 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4817 "3168 SCSI Layer abort requested I/O has been "
4818 "flushed by LLD.\n");
James Smartc2017262019-01-28 11:14:37 -08004819 ret = FAILED;
4820 goto out_unlock;
James Smart4f2e66c2012-05-09 21:17:07 -04004821 }
4822
James Smartc2017262019-01-28 11:14:37 -08004823 /* Guard against IO completion being called at same time */
4824 spin_lock(&lpfc_cmd->buf_lock);
4825
4826 if (!lpfc_cmd->pCmd) {
James Smarteee88772010-09-29 11:19:08 -04004827 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4828 "2873 SCSI Layer I/O Abort Request IO CMPL Status "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004829 "x%x ID %d LUN %llu\n",
James Smart3a707302012-06-12 13:54:42 -04004830 SUCCESS, cmnd->device->id, cmnd->device->lun);
James Smartc2017262019-01-28 11:14:37 -08004831 goto out_unlock_buf;
James Smarteee88772010-09-29 11:19:08 -04004832 }
dea31012005-04-17 16:05:31 -05004833
James Smart4f2e66c2012-05-09 21:17:07 -04004834 iocb = &lpfc_cmd->cur_iocbq;
James Smart8931c732018-07-31 17:23:20 -07004835 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartc00f62e2019-08-14 16:57:11 -07004836 pring_s4 = phba->sli4_hba.hdwq[iocb->hba_wqidx].io_wq->pring;
James Smart8931c732018-07-31 17:23:20 -07004837 if (!pring_s4) {
4838 ret = FAILED;
James Smartc2017262019-01-28 11:14:37 -08004839 goto out_unlock_buf;
James Smart8931c732018-07-31 17:23:20 -07004840 }
4841 spin_lock(&pring_s4->ring_lock);
4842 }
James Smart4f2e66c2012-05-09 21:17:07 -04004843 /* the command is in process of being cancelled */
4844 if (!(iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
James Smart4f2e66c2012-05-09 21:17:07 -04004845 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4846 "3169 SCSI Layer abort requested I/O has been "
4847 "cancelled by LLD.\n");
James Smartc2017262019-01-28 11:14:37 -08004848 ret = FAILED;
4849 goto out_unlock_ring;
James Smart4f2e66c2012-05-09 21:17:07 -04004850 }
dea31012005-04-17 16:05:31 -05004851 /*
James Smartc4908502019-01-28 11:14:28 -08004852 * If pCmd field of the corresponding lpfc_io_buf structure
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004853 * points to a different SCSI command, then the driver has
4854 * already completed this command, but the midlayer did not
James Smart4f2e66c2012-05-09 21:17:07 -04004855 * see the completion before the eh fired. Just return SUCCESS.
dea31012005-04-17 16:05:31 -05004856 */
James Smart4f2e66c2012-05-09 21:17:07 -04004857 if (lpfc_cmd->pCmd != cmnd) {
4858 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4859 "3170 SCSI Layer abort requested I/O has been "
4860 "completed by LLD.\n");
James Smartc2017262019-01-28 11:14:37 -08004861 goto out_unlock_ring;
James Smart4f2e66c2012-05-09 21:17:07 -04004862 }
dea31012005-04-17 16:05:31 -05004863
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004864 BUG_ON(iocb->context1 != lpfc_cmd);
dea31012005-04-17 16:05:31 -05004865
James Smartee620212014-04-04 13:51:53 -04004866 /* abort issued in recovery is still in progress */
4867 if (iocb->iocb_flag & LPFC_DRIVER_ABORTED) {
4868 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4869 "3389 SCSI Layer I/O Abort Request is pending\n");
James Smart8931c732018-07-31 17:23:20 -07004870 if (phba->sli_rev == LPFC_SLI_REV4)
4871 spin_unlock(&pring_s4->ring_lock);
James Smartc2017262019-01-28 11:14:37 -08004872 spin_unlock(&lpfc_cmd->buf_lock);
James Smartee620212014-04-04 13:51:53 -04004873 spin_unlock_irqrestore(&phba->hbalock, flags);
4874 goto wait_for_cmpl;
4875 }
4876
James Smart4f2e66c2012-05-09 21:17:07 -04004877 abtsiocb = __lpfc_sli_get_iocbq(phba);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004878 if (abtsiocb == NULL) {
4879 ret = FAILED;
James Smartc2017262019-01-28 11:14:37 -08004880 goto out_unlock_ring;
dea31012005-04-17 16:05:31 -05004881 }
4882
James Smartafbd8d82013-09-06 12:22:13 -04004883 /* Indicate the IO is being aborted by the driver. */
4884 iocb->iocb_flag |= LPFC_DRIVER_ABORTED;
4885
dea31012005-04-17 16:05:31 -05004886 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004887 * The scsi command can not be in txq and it is in flight because the
4888 * pCmd is still pointig at the SCSI command we have to abort. There
4889 * is no need to search the txcmplq. Just send an abort to the FW.
dea31012005-04-17 16:05:31 -05004890 */
dea31012005-04-17 16:05:31 -05004891
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004892 cmd = &iocb->iocb;
4893 icmd = &abtsiocb->iocb;
4894 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
4895 icmd->un.acxri.abortContextTag = cmd->ulpContext;
James Smart3772a992009-05-22 14:50:54 -04004896 if (phba->sli_rev == LPFC_SLI_REV4)
4897 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
4898 else
4899 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05004900
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004901 icmd->ulpLe = 1;
4902 icmd->ulpClass = cmd->ulpClass;
James Smart5ffc2662009-11-18 15:39:44 -05004903
4904 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -08004905 abtsiocb->hba_wqidx = iocb->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -05004906 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -04004907 if (iocb->iocb_flag & LPFC_IO_FOF)
4908 abtsiocb->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -05004909
James Smart2e0fef82007-06-17 19:56:36 -05004910 if (lpfc_is_link_up(phba))
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004911 icmd->ulpCommand = CMD_ABORT_XRI_CN;
4912 else
4913 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea31012005-04-17 16:05:31 -05004914
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004915 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smart2e0fef82007-06-17 19:56:36 -05004916 abtsiocb->vport = vport;
James Smart8931c732018-07-31 17:23:20 -07004917 lpfc_cmd->waitq = &waitq;
James Smart98912dda2014-04-04 13:52:31 -04004918 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart98912dda2014-04-04 13:52:31 -04004919 /* Note: both hbalock and ring_lock must be set here */
James Smart98912dda2014-04-04 13:52:31 -04004920 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
4921 abtsiocb, 0);
James Smart59c68ea2018-04-09 14:24:25 -07004922 spin_unlock(&pring_s4->ring_lock);
James Smart98912dda2014-04-04 13:52:31 -04004923 } else {
4924 ret_val = __lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
4925 abtsiocb, 0);
4926 }
James Smart4f2e66c2012-05-09 21:17:07 -04004927 /* no longer need the lock after this point */
James Smart876dd7d2012-09-29 11:31:28 -04004928 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004929
James Smart98912dda2014-04-04 13:52:31 -04004930 if (ret_val == IOCB_ERROR) {
James Smart8931c732018-07-31 17:23:20 -07004931 /* Indicate the IO is not being aborted by the driver. */
4932 iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
4933 lpfc_cmd->waitq = NULL;
James Smartc2017262019-01-28 11:14:37 -08004934 spin_unlock(&lpfc_cmd->buf_lock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004935 lpfc_sli_release_iocbq(phba, abtsiocb);
4936 ret = FAILED;
4937 goto out;
4938 }
4939
James Smartc2017262019-01-28 11:14:37 -08004940 spin_unlock(&lpfc_cmd->buf_lock);
4941
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004942 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
James Smart45ed1192009-10-02 15:17:02 -04004943 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -08004944 &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004945
James Smartee620212014-04-04 13:51:53 -04004946wait_for_cmpl:
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004947 /* Wait for abort to complete */
James Smartfa61a542008-01-11 01:52:42 -05004948 wait_event_timeout(waitq,
4949 (lpfc_cmd->pCmd != cmnd),
James Smart256ec0d2013-04-17 20:14:58 -04004950 msecs_to_jiffies(2*vport->cfg_devloss_tmo*1000));
James Smartee620212014-04-04 13:51:53 -04004951
James Smartc2017262019-01-28 11:14:37 -08004952 spin_lock(&lpfc_cmd->buf_lock);
dea31012005-04-17 16:05:31 -05004953
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004954 if (lpfc_cmd->pCmd == cmnd) {
4955 ret = FAILED;
James Smarte8b62012007-08-02 11:10:09 -04004956 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4957 "0748 abort handler timed out waiting "
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -07004958 "for aborting I/O (xri:x%x) to complete: "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004959 "ret %#x, ID %d, LUN %llu\n",
James Smart247ca942012-08-14 14:26:13 -04004960 iocb->sli4_xritag, ret,
4961 cmnd->device->id, cmnd->device->lun);
dea31012005-04-17 16:05:31 -05004962 }
James Smartb9e5a2d2019-05-21 17:49:01 -07004963
4964 lpfc_cmd->waitq = NULL;
4965
James Smartc2017262019-01-28 11:14:37 -08004966 spin_unlock(&lpfc_cmd->buf_lock);
James Smart4f2e66c2012-05-09 21:17:07 -04004967 goto out;
dea31012005-04-17 16:05:31 -05004968
James Smartc2017262019-01-28 11:14:37 -08004969out_unlock_ring:
4970 if (phba->sli_rev == LPFC_SLI_REV4)
4971 spin_unlock(&pring_s4->ring_lock);
4972out_unlock_buf:
4973 spin_unlock(&lpfc_cmd->buf_lock);
James Smart4f2e66c2012-05-09 21:17:07 -04004974out_unlock:
James Smart876dd7d2012-09-29 11:31:28 -04004975 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004976out:
James Smarte8b62012007-08-02 11:10:09 -04004977 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4978 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004979 "LUN %llu\n", ret, cmnd->device->id,
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04004980 cmnd->device->lun);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004981 return ret;
dea31012005-04-17 16:05:31 -05004982}
4983
James Smartbbb9d182009-06-10 17:23:16 -04004984static char *
4985lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
4986{
4987 switch (task_mgmt_cmd) {
4988 case FCP_ABORT_TASK_SET:
4989 return "ABORT_TASK_SET";
4990 case FCP_CLEAR_TASK_SET:
4991 return "FCP_CLEAR_TASK_SET";
4992 case FCP_BUS_RESET:
4993 return "FCP_BUS_RESET";
4994 case FCP_LUN_RESET:
4995 return "FCP_LUN_RESET";
4996 case FCP_TARGET_RESET:
4997 return "FCP_TARGET_RESET";
4998 case FCP_CLEAR_ACA:
4999 return "FCP_CLEAR_ACA";
5000 case FCP_TERMINATE_TASK:
5001 return "FCP_TERMINATE_TASK";
5002 default:
5003 return "unknown";
5004 }
5005}
5006
James Smart53151bb2013-10-10 12:24:07 -04005007
5008/**
5009 * lpfc_check_fcp_rsp - check the returned fcp_rsp to see if task failed
5010 * @vport: The virtual port for which this call is being executed.
James Smartc4908502019-01-28 11:14:28 -08005011 * @lpfc_cmd: Pointer to lpfc_io_buf data structure.
James Smart53151bb2013-10-10 12:24:07 -04005012 *
5013 * This routine checks the FCP RSP INFO to see if the tsk mgmt command succeded
5014 *
5015 * Return code :
5016 * 0x2003 - Error
5017 * 0x2002 - Success
5018 **/
5019static int
James Smartc4908502019-01-28 11:14:28 -08005020lpfc_check_fcp_rsp(struct lpfc_vport *vport, struct lpfc_io_buf *lpfc_cmd)
James Smart53151bb2013-10-10 12:24:07 -04005021{
5022 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
5023 uint32_t rsp_info;
5024 uint32_t rsp_len;
5025 uint8_t rsp_info_code;
5026 int ret = FAILED;
5027
5028
5029 if (fcprsp == NULL)
5030 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5031 "0703 fcp_rsp is missing\n");
5032 else {
5033 rsp_info = fcprsp->rspStatus2;
5034 rsp_len = be32_to_cpu(fcprsp->rspRspLen);
5035 rsp_info_code = fcprsp->rspInfo3;
5036
5037
5038 lpfc_printf_vlog(vport, KERN_INFO,
5039 LOG_FCP,
5040 "0706 fcp_rsp valid 0x%x,"
5041 " rsp len=%d code 0x%x\n",
5042 rsp_info,
5043 rsp_len, rsp_info_code);
5044
James Smart996a02a2019-05-21 17:49:07 -07005045 /* If FCP_RSP_LEN_VALID bit is one, then the FCP_RSP_LEN
5046 * field specifies the number of valid bytes of FCP_RSP_INFO.
5047 * The FCP_RSP_LEN field shall be set to 0x04 or 0x08
5048 */
5049 if ((fcprsp->rspStatus2 & RSP_LEN_VALID) &&
5050 ((rsp_len == 8) || (rsp_len == 4))) {
James Smart53151bb2013-10-10 12:24:07 -04005051 switch (rsp_info_code) {
5052 case RSP_NO_FAILURE:
5053 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5054 "0715 Task Mgmt No Failure\n");
5055 ret = SUCCESS;
5056 break;
5057 case RSP_TM_NOT_SUPPORTED: /* TM rejected */
5058 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5059 "0716 Task Mgmt Target "
5060 "reject\n");
5061 break;
5062 case RSP_TM_NOT_COMPLETED: /* TM failed */
5063 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5064 "0717 Task Mgmt Target "
5065 "failed TM\n");
5066 break;
5067 case RSP_TM_INVALID_LU: /* TM to invalid LU! */
5068 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5069 "0718 Task Mgmt to invalid "
5070 "LUN\n");
5071 break;
5072 }
5073 }
5074 }
5075 return ret;
5076}
5077
5078
James Smartbbb9d182009-06-10 17:23:16 -04005079/**
5080 * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
5081 * @vport: The virtual port for which this call is being executed.
5082 * @rdata: Pointer to remote port local data
5083 * @tgt_id: Target ID of remote device.
5084 * @lun_id: Lun number for the TMF
5085 * @task_mgmt_cmd: type of TMF to send
5086 *
5087 * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
5088 * a remote port.
5089 *
5090 * Return Code:
5091 * 0x2003 - Error
5092 * 0x2002 - Success.
5093 **/
5094static int
James Smarteed695d2016-10-13 15:06:04 -07005095lpfc_send_taskmgmt(struct lpfc_vport *vport, struct scsi_cmnd *cmnd,
5096 unsigned int tgt_id, uint64_t lun_id,
5097 uint8_t task_mgmt_cmd)
James Smartbbb9d182009-06-10 17:23:16 -04005098{
5099 struct lpfc_hba *phba = vport->phba;
James Smartc4908502019-01-28 11:14:28 -08005100 struct lpfc_io_buf *lpfc_cmd;
James Smartbbb9d182009-06-10 17:23:16 -04005101 struct lpfc_iocbq *iocbq;
5102 struct lpfc_iocbq *iocbqrsp;
James Smarteed695d2016-10-13 15:06:04 -07005103 struct lpfc_rport_data *rdata;
5104 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005105 int ret;
5106 int status;
5107
James Smarteed695d2016-10-13 15:06:04 -07005108 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
5109 if (!rdata || !rdata->pnode || !NLP_CHK_NODE_ACT(rdata->pnode))
James Smartbbb9d182009-06-10 17:23:16 -04005110 return FAILED;
James Smarteed695d2016-10-13 15:06:04 -07005111 pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005112
James Smartace44e42019-01-28 11:14:27 -08005113 lpfc_cmd = lpfc_get_scsi_buf(phba, pnode, NULL);
James Smartbbb9d182009-06-10 17:23:16 -04005114 if (lpfc_cmd == NULL)
5115 return FAILED;
James Smart0c411222013-09-06 12:22:46 -04005116 lpfc_cmd->timeout = phba->cfg_task_mgmt_tmo;
James Smartbbb9d182009-06-10 17:23:16 -04005117 lpfc_cmd->rdata = rdata;
James Smarteed695d2016-10-13 15:06:04 -07005118 lpfc_cmd->pCmd = cmnd;
James Smart2a5b7d62018-07-31 17:23:22 -07005119 lpfc_cmd->ndlp = pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005120
5121 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
5122 task_mgmt_cmd);
5123 if (!status) {
5124 lpfc_release_scsi_buf(phba, lpfc_cmd);
5125 return FAILED;
5126 }
5127
5128 iocbq = &lpfc_cmd->cur_iocbq;
5129 iocbqrsp = lpfc_sli_get_iocbq(phba);
5130 if (iocbqrsp == NULL) {
5131 lpfc_release_scsi_buf(phba, lpfc_cmd);
5132 return FAILED;
5133 }
James Smart5a0916b2013-07-15 18:31:42 -04005134 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
James Smartbbb9d182009-06-10 17:23:16 -04005135
5136 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005137 "0702 Issue %s to TGT %d LUN %llu "
James Smart6d368e52011-05-24 11:44:12 -04005138 "rpi x%x nlp_flag x%x Data: x%x x%x\n",
James Smartbbb9d182009-06-10 17:23:16 -04005139 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
James Smart6d368e52011-05-24 11:44:12 -04005140 pnode->nlp_rpi, pnode->nlp_flag, iocbq->sli4_xritag,
5141 iocbq->iocb_flag);
James Smartbbb9d182009-06-10 17:23:16 -04005142
5143 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
5144 iocbq, iocbqrsp, lpfc_cmd->timeout);
James Smart53151bb2013-10-10 12:24:07 -04005145 if ((status != IOCB_SUCCESS) ||
5146 (iocbqrsp->iocb.ulpStatus != IOSTAT_SUCCESS)) {
James Smartae374a32015-05-22 10:42:41 -04005147 if (status != IOCB_SUCCESS ||
5148 iocbqrsp->iocb.ulpStatus != IOSTAT_FCP_RSP_ERROR)
5149 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5150 "0727 TMF %s to TGT %d LUN %llu "
5151 "failed (%d, %d) iocb_flag x%x\n",
5152 lpfc_taskmgmt_name(task_mgmt_cmd),
5153 tgt_id, lun_id,
5154 iocbqrsp->iocb.ulpStatus,
5155 iocbqrsp->iocb.un.ulpWord[4],
5156 iocbq->iocb_flag);
James Smart53151bb2013-10-10 12:24:07 -04005157 /* if ulpStatus != IOCB_SUCCESS, then status == IOCB_SUCCESS */
5158 if (status == IOCB_SUCCESS) {
5159 if (iocbqrsp->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
5160 /* Something in the FCP_RSP was invalid.
5161 * Check conditions */
5162 ret = lpfc_check_fcp_rsp(vport, lpfc_cmd);
5163 else
5164 ret = FAILED;
5165 } else if (status == IOCB_TIMEDOUT) {
5166 ret = TIMEOUT_ERROR;
5167 } else {
5168 ret = FAILED;
5169 }
James Smart53151bb2013-10-10 12:24:07 -04005170 } else
James Smartbbb9d182009-06-10 17:23:16 -04005171 ret = SUCCESS;
5172
5173 lpfc_sli_release_iocbq(phba, iocbqrsp);
5174
5175 if (ret != TIMEOUT_ERROR)
5176 lpfc_release_scsi_buf(phba, lpfc_cmd);
5177
5178 return ret;
5179}
5180
5181/**
5182 * lpfc_chk_tgt_mapped -
5183 * @vport: The virtual port to check on
5184 * @cmnd: Pointer to scsi_cmnd data structure.
5185 *
5186 * This routine delays until the scsi target (aka rport) for the
5187 * command exists (is present and logged in) or we declare it non-existent.
5188 *
5189 * Return code :
5190 * 0x2003 - Error
5191 * 0x2002 - Success
5192 **/
5193static int
5194lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
5195{
James Smart1ba981f2014-02-20 09:56:45 -05005196 struct lpfc_rport_data *rdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005197 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005198 unsigned long later;
5199
James Smart1ba981f2014-02-20 09:56:45 -05005200 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smart1c6f4ef52009-11-18 15:40:49 -05005201 if (!rdata) {
5202 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smart32350662019-08-14 16:57:06 -07005203 "0797 Tgt Map rport failure: rdata x%px\n", rdata);
James Smart1c6f4ef52009-11-18 15:40:49 -05005204 return FAILED;
5205 }
5206 pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005207 /*
5208 * If target is not in a MAPPED state, delay until
5209 * target is rediscovered or devloss timeout expires.
5210 */
5211 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
5212 while (time_after(later, jiffies)) {
5213 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
5214 return FAILED;
5215 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
5216 return SUCCESS;
5217 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
James Smart1ba981f2014-02-20 09:56:45 -05005218 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smartbbb9d182009-06-10 17:23:16 -04005219 if (!rdata)
5220 return FAILED;
5221 pnode = rdata->pnode;
5222 }
5223 if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
5224 (pnode->nlp_state != NLP_STE_MAPPED_NODE))
5225 return FAILED;
5226 return SUCCESS;
5227}
5228
5229/**
5230 * lpfc_reset_flush_io_context -
5231 * @vport: The virtual port (scsi_host) for the flush context
5232 * @tgt_id: If aborting by Target contect - specifies the target id
5233 * @lun_id: If aborting by Lun context - specifies the lun id
5234 * @context: specifies the context level to flush at.
5235 *
5236 * After a reset condition via TMF, we need to flush orphaned i/o
5237 * contexts from the adapter. This routine aborts any contexts
5238 * outstanding, then waits for their completions. The wait is
5239 * bounded by devloss_tmo though.
5240 *
5241 * Return code :
5242 * 0x2003 - Error
5243 * 0x2002 - Success
5244 **/
5245static int
5246lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
5247 uint64_t lun_id, lpfc_ctx_cmd context)
5248{
5249 struct lpfc_hba *phba = vport->phba;
5250 unsigned long later;
5251 int cnt;
5252
5253 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
5254 if (cnt)
James Smart98912dda2014-04-04 13:52:31 -04005255 lpfc_sli_abort_taskmgmt(vport,
James Smart895427b2017-02-12 13:52:30 -08005256 &phba->sli.sli3_ring[LPFC_FCP_RING],
James Smart98912dda2014-04-04 13:52:31 -04005257 tgt_id, lun_id, context);
James Smartbbb9d182009-06-10 17:23:16 -04005258 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
5259 while (time_after(later, jiffies) && cnt) {
5260 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
5261 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
5262 }
5263 if (cnt) {
5264 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5265 "0724 I/O flush failure for context %s : cnt x%x\n",
5266 ((context == LPFC_CTX_LUN) ? "LUN" :
5267 ((context == LPFC_CTX_TGT) ? "TGT" :
5268 ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
5269 cnt);
5270 return FAILED;
5271 }
5272 return SUCCESS;
5273}
5274
James Smart9bad7672008-12-04 22:39:02 -05005275/**
James Smart3621a712009-04-06 18:47:14 -04005276 * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
James Smart9bad7672008-12-04 22:39:02 -05005277 * @cmnd: Pointer to scsi_cmnd data structure.
5278 *
James Smartbbb9d182009-06-10 17:23:16 -04005279 * This routine does a device reset by sending a LUN_RESET task management
James Smart9bad7672008-12-04 22:39:02 -05005280 * command.
5281 *
5282 * Return code :
5283 * 0x2003 - Error
James Smart3621a712009-04-06 18:47:14 -04005284 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05005285 **/
dea31012005-04-17 16:05:31 -05005286static int
James Smart7054a602007-04-25 09:52:34 -04005287lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05005288{
James Smart2e0fef82007-06-17 19:56:36 -05005289 struct Scsi_Host *shost = cmnd->device->host;
5290 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smart1ba981f2014-02-20 09:56:45 -05005291 struct lpfc_rport_data *rdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005292 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005293 unsigned tgt_id = cmnd->device->id;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005294 uint64_t lun_id = cmnd->device->lun;
James Smartea2151b2008-09-07 11:52:10 -04005295 struct lpfc_scsi_event_header scsi_event;
James Smart53151bb2013-10-10 12:24:07 -04005296 int status;
dea31012005-04-17 16:05:31 -05005297
James Smart1ba981f2014-02-20 09:56:45 -05005298 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smartad490b62015-04-07 15:07:26 -04005299 if (!rdata || !rdata->pnode) {
James Smart1c6f4ef52009-11-18 15:40:49 -05005300 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smart32350662019-08-14 16:57:06 -07005301 "0798 Device Reset rdata failure: rdata x%px\n",
James Smartad490b62015-04-07 15:07:26 -04005302 rdata);
James Smart1c6f4ef52009-11-18 15:40:49 -05005303 return FAILED;
5304 }
5305 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04005306 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005307 if (status != 0 && status != SUCCESS)
James Smart589a52d2010-07-14 15:30:54 -04005308 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005309
5310 status = lpfc_chk_tgt_mapped(vport, cmnd);
5311 if (status == FAILED) {
5312 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smart32350662019-08-14 16:57:06 -07005313 "0721 Device Reset rport failure: rdata x%px\n", rdata);
James Smartbbb9d182009-06-10 17:23:16 -04005314 return FAILED;
5315 }
5316
5317 scsi_event.event_type = FC_REG_SCSI_EVENT;
5318 scsi_event.subcategory = LPFC_EVENT_LUNRESET;
5319 scsi_event.lun = lun_id;
5320 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5321 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5322
5323 fc_host_post_vendor_event(shost, fc_get_event_number(),
5324 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
5325
James Smarteed695d2016-10-13 15:06:04 -07005326 status = lpfc_send_taskmgmt(vport, cmnd, tgt_id, lun_id,
James Smartbbb9d182009-06-10 17:23:16 -04005327 FCP_LUN_RESET);
5328
5329 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005330 "0713 SCSI layer issued Device Reset (%d, %llu) "
James Smartbbb9d182009-06-10 17:23:16 -04005331 "return x%x\n", tgt_id, lun_id, status);
5332
dea31012005-04-17 16:05:31 -05005333 /*
James Smartbbb9d182009-06-10 17:23:16 -04005334 * We have to clean up i/o as : they may be orphaned by the TMF;
5335 * or if the TMF failed, they may be in an indeterminate state.
5336 * So, continue on.
5337 * We will report success if all the i/o aborts successfully.
dea31012005-04-17 16:05:31 -05005338 */
James Smart53151bb2013-10-10 12:24:07 -04005339 if (status == SUCCESS)
5340 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
James Smartbbb9d182009-06-10 17:23:16 -04005341 LPFC_CTX_LUN);
James Smart53151bb2013-10-10 12:24:07 -04005342
5343 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005344}
5345
5346/**
5347 * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
5348 * @cmnd: Pointer to scsi_cmnd data structure.
5349 *
5350 * This routine does a target reset by sending a TARGET_RESET task management
5351 * command.
5352 *
5353 * Return code :
5354 * 0x2003 - Error
5355 * 0x2002 - Success
5356 **/
5357static int
5358lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
5359{
5360 struct Scsi_Host *shost = cmnd->device->host;
5361 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smart1ba981f2014-02-20 09:56:45 -05005362 struct lpfc_rport_data *rdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005363 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005364 unsigned tgt_id = cmnd->device->id;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005365 uint64_t lun_id = cmnd->device->lun;
James Smartbbb9d182009-06-10 17:23:16 -04005366 struct lpfc_scsi_event_header scsi_event;
James Smart53151bb2013-10-10 12:24:07 -04005367 int status;
James Smartbbb9d182009-06-10 17:23:16 -04005368
James Smart1ba981f2014-02-20 09:56:45 -05005369 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smartbbd3d732019-03-12 16:30:17 -07005370 if (!rdata || !rdata->pnode) {
James Smart1c6f4ef52009-11-18 15:40:49 -05005371 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smart32350662019-08-14 16:57:06 -07005372 "0799 Target Reset rdata failure: rdata x%px\n",
James Smartbbd3d732019-03-12 16:30:17 -07005373 rdata);
James Smart1c6f4ef52009-11-18 15:40:49 -05005374 return FAILED;
5375 }
5376 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04005377 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005378 if (status != 0 && status != SUCCESS)
James Smart589a52d2010-07-14 15:30:54 -04005379 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005380
5381 status = lpfc_chk_tgt_mapped(vport, cmnd);
5382 if (status == FAILED) {
5383 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smart32350662019-08-14 16:57:06 -07005384 "0722 Target Reset rport failure: rdata x%px\n", rdata);
James Smart63e480f2015-04-07 15:07:20 -04005385 if (pnode) {
5386 spin_lock_irq(shost->host_lock);
5387 pnode->nlp_flag &= ~NLP_NPR_ADISC;
5388 pnode->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
5389 spin_unlock_irq(shost->host_lock);
5390 }
James Smart8c50d252014-09-03 12:58:16 -04005391 lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
5392 LPFC_CTX_TGT);
5393 return FAST_IO_FAIL;
dea31012005-04-17 16:05:31 -05005394 }
James Smartea2151b2008-09-07 11:52:10 -04005395
5396 scsi_event.event_type = FC_REG_SCSI_EVENT;
5397 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
5398 scsi_event.lun = 0;
5399 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5400 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5401
James Smartbbb9d182009-06-10 17:23:16 -04005402 fc_host_post_vendor_event(shost, fc_get_event_number(),
5403 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -04005404
James Smarteed695d2016-10-13 15:06:04 -07005405 status = lpfc_send_taskmgmt(vport, cmnd, tgt_id, lun_id,
James Smartbbb9d182009-06-10 17:23:16 -04005406 FCP_TARGET_RESET);
dea31012005-04-17 16:05:31 -05005407
James Smarte8b62012007-08-02 11:10:09 -04005408 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005409 "0723 SCSI layer issued Target Reset (%d, %llu) "
James Smartbbb9d182009-06-10 17:23:16 -04005410 "return x%x\n", tgt_id, lun_id, status);
5411
5412 /*
5413 * We have to clean up i/o as : they may be orphaned by the TMF;
5414 * or if the TMF failed, they may be in an indeterminate state.
5415 * So, continue on.
5416 * We will report success if all the i/o aborts successfully.
5417 */
James Smart53151bb2013-10-10 12:24:07 -04005418 if (status == SUCCESS)
5419 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
James Smart3a707302012-06-12 13:54:42 -04005420 LPFC_CTX_TGT);
James Smart53151bb2013-10-10 12:24:07 -04005421 return status;
dea31012005-04-17 16:05:31 -05005422}
5423
James Smart9bad7672008-12-04 22:39:02 -05005424/**
James Smart3621a712009-04-06 18:47:14 -04005425 * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05005426 * @cmnd: Pointer to scsi_cmnd data structure.
5427 *
James Smartbbb9d182009-06-10 17:23:16 -04005428 * This routine does target reset to all targets on @cmnd->device->host.
5429 * This emulates Parallel SCSI Bus Reset Semantics.
James Smart9bad7672008-12-04 22:39:02 -05005430 *
James Smartbbb9d182009-06-10 17:23:16 -04005431 * Return code :
5432 * 0x2003 - Error
5433 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05005434 **/
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04005435static int
James Smart7054a602007-04-25 09:52:34 -04005436lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05005437{
James Smart2e0fef82007-06-17 19:56:36 -05005438 struct Scsi_Host *shost = cmnd->device->host;
5439 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05005440 struct lpfc_nodelist *ndlp = NULL;
James Smartea2151b2008-09-07 11:52:10 -04005441 struct lpfc_scsi_event_header scsi_event;
James Smartbbb9d182009-06-10 17:23:16 -04005442 int match;
5443 int ret = SUCCESS, status, i;
James Smartea2151b2008-09-07 11:52:10 -04005444
5445 scsi_event.event_type = FC_REG_SCSI_EVENT;
5446 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
5447 scsi_event.lun = 0;
5448 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
5449 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
5450
James Smartbbb9d182009-06-10 17:23:16 -04005451 fc_host_post_vendor_event(shost, fc_get_event_number(),
5452 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
dea31012005-04-17 16:05:31 -05005453
James Smartbf086112011-08-21 21:48:13 -04005454 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005455 if (status != 0 && status != SUCCESS)
James Smartbf086112011-08-21 21:48:13 -04005456 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005457
dea31012005-04-17 16:05:31 -05005458 /*
5459 * Since the driver manages a single bus device, reset all
5460 * targets known to the driver. Should any target reset
5461 * fail, this routine returns failure to the midlayer.
5462 */
James Smarte17da182006-07-06 15:49:25 -04005463 for (i = 0; i < LPFC_MAX_TARGET; i++) {
James Smart685f0bf2007-04-25 09:53:08 -04005464 /* Search for mapped node by target ID */
dea31012005-04-17 16:05:31 -05005465 match = 0;
James Smart2e0fef82007-06-17 19:56:36 -05005466 spin_lock_irq(shost->host_lock);
5467 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05005468 if (!NLP_CHK_NODE_ACT(ndlp))
5469 continue;
James Smarta6571c62012-10-31 14:44:42 -04005470 if (vport->phba->cfg_fcp2_no_tgt_reset &&
5471 (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE))
5472 continue;
James Smart685f0bf2007-04-25 09:53:08 -04005473 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
James Smart915caaa2008-06-14 22:52:38 -04005474 ndlp->nlp_sid == i &&
James Smarta0f2d3e2017-02-12 13:52:31 -08005475 ndlp->rport &&
5476 ndlp->nlp_type & NLP_FCP_TARGET) {
dea31012005-04-17 16:05:31 -05005477 match = 1;
5478 break;
5479 }
5480 }
James Smart2e0fef82007-06-17 19:56:36 -05005481 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005482 if (!match)
5483 continue;
James Smartbbb9d182009-06-10 17:23:16 -04005484
James Smarteed695d2016-10-13 15:06:04 -07005485 status = lpfc_send_taskmgmt(vport, cmnd,
James Smartbbb9d182009-06-10 17:23:16 -04005486 i, 0, FCP_TARGET_RESET);
5487
5488 if (status != SUCCESS) {
James Smarte8b62012007-08-02 11:10:09 -04005489 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5490 "0700 Bus Reset on target %d failed\n",
5491 i);
James Smart915caaa2008-06-14 22:52:38 -04005492 ret = FAILED;
dea31012005-04-17 16:05:31 -05005493 }
5494 }
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005495 /*
James Smartbbb9d182009-06-10 17:23:16 -04005496 * We have to clean up i/o as : they may be orphaned by the TMFs
5497 * above; or if any of the TMFs failed, they may be in an
5498 * indeterminate state.
5499 * We will report success if all the i/o aborts successfully.
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005500 */
James Smartbbb9d182009-06-10 17:23:16 -04005501
5502 status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
5503 if (status != SUCCESS)
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005504 ret = FAILED;
James Smartbbb9d182009-06-10 17:23:16 -04005505
James Smarte8b62012007-08-02 11:10:09 -04005506 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5507 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea31012005-04-17 16:05:31 -05005508 return ret;
5509}
5510
James Smart9bad7672008-12-04 22:39:02 -05005511/**
James Smart27b01b82012-05-09 21:19:44 -04005512 * lpfc_host_reset_handler - scsi_host_template eh_host_reset_handler entry pt
5513 * @cmnd: Pointer to scsi_cmnd data structure.
5514 *
5515 * This routine does host reset to the adaptor port. It brings the HBA
5516 * offline, performs a board restart, and then brings the board back online.
5517 * The lpfc_offline calls lpfc_sli_hba_down which will abort and local
5518 * reject all outstanding SCSI commands to the host and error returned
5519 * back to SCSI mid-level. As this will be SCSI mid-level's last resort
5520 * of error handling, it will only return error if resetting of the adapter
5521 * is not successful; in all other cases, will return success.
5522 *
5523 * Return code :
5524 * 0x2003 - Error
5525 * 0x2002 - Success
5526 **/
5527static int
5528lpfc_host_reset_handler(struct scsi_cmnd *cmnd)
5529{
5530 struct Scsi_Host *shost = cmnd->device->host;
5531 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5532 struct lpfc_hba *phba = vport->phba;
5533 int rc, ret = SUCCESS;
5534
James Smarta88dbb62013-04-17 20:18:39 -04005535 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5536 "3172 SCSI layer issued Host Reset Data:\n");
5537
James Smart618a5232012-06-12 13:54:36 -04005538 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart27b01b82012-05-09 21:19:44 -04005539 lpfc_offline(phba);
5540 rc = lpfc_sli_brdrestart(phba);
5541 if (rc)
James Smart8c24a4f2019-08-14 16:56:53 -07005542 goto error;
5543
James Smarta88dbb62013-04-17 20:18:39 -04005544 rc = lpfc_online(phba);
5545 if (rc)
James Smart8c24a4f2019-08-14 16:56:53 -07005546 goto error;
5547
James Smart27b01b82012-05-09 21:19:44 -04005548 lpfc_unblock_mgmt_io(phba);
5549
James Smart27b01b82012-05-09 21:19:44 -04005550 return ret;
James Smart8c24a4f2019-08-14 16:56:53 -07005551error:
5552 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5553 "3323 Failed host reset\n");
5554 lpfc_unblock_mgmt_io(phba);
5555 return FAILED;
James Smart27b01b82012-05-09 21:19:44 -04005556}
5557
5558/**
James Smart3621a712009-04-06 18:47:14 -04005559 * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
James Smart9bad7672008-12-04 22:39:02 -05005560 * @sdev: Pointer to scsi_device.
5561 *
5562 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
5563 * globally available list of scsi buffers. This routine also makes sure scsi
5564 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
5565 * of scsi buffer exists for the lifetime of the driver.
5566 *
5567 * Return codes:
5568 * non-0 - Error
5569 * 0 - Success
5570 **/
dea31012005-04-17 16:05:31 -05005571static int
dea31012005-04-17 16:05:31 -05005572lpfc_slave_alloc(struct scsi_device *sdev)
5573{
James Smart2e0fef82007-06-17 19:56:36 -05005574 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5575 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005576 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
James Smart3772a992009-05-22 14:50:54 -04005577 uint32_t total = 0;
dea31012005-04-17 16:05:31 -05005578 uint32_t num_to_alloc = 0;
James Smart3772a992009-05-22 14:50:54 -04005579 int num_allocated = 0;
James Smartd7c47992010-06-08 18:31:54 -04005580 uint32_t sdev_cnt;
James Smart1ba981f2014-02-20 09:56:45 -05005581 struct lpfc_device_data *device_data;
5582 unsigned long flags;
5583 struct lpfc_name target_wwpn;
dea31012005-04-17 16:05:31 -05005584
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005585 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05005586 return -ENXIO;
5587
James Smartf38fa0b2014-04-04 13:52:21 -04005588 if (phba->cfg_fof) {
James Smart1ba981f2014-02-20 09:56:45 -05005589
5590 /*
5591 * Check to see if the device data structure for the lun
5592 * exists. If not, create one.
5593 */
5594
5595 u64_to_wwn(rport->port_name, target_wwpn.u.wwn);
5596 spin_lock_irqsave(&phba->devicelock, flags);
5597 device_data = __lpfc_get_device_data(phba,
5598 &phba->luns,
5599 &vport->fc_portname,
5600 &target_wwpn,
5601 sdev->lun);
5602 if (!device_data) {
5603 spin_unlock_irqrestore(&phba->devicelock, flags);
5604 device_data = lpfc_create_device_data(phba,
5605 &vport->fc_portname,
5606 &target_wwpn,
James Smartb5749fe2016-12-19 15:07:26 -08005607 sdev->lun,
5608 phba->cfg_XLanePriority,
5609 true);
James Smart1ba981f2014-02-20 09:56:45 -05005610 if (!device_data)
5611 return -ENOMEM;
5612 spin_lock_irqsave(&phba->devicelock, flags);
5613 list_add_tail(&device_data->listentry, &phba->luns);
5614 }
5615 device_data->rport_data = rport->dd_data;
5616 device_data->available = true;
5617 spin_unlock_irqrestore(&phba->devicelock, flags);
5618 sdev->hostdata = device_data;
5619 } else {
5620 sdev->hostdata = rport->dd_data;
5621 }
James Smartd7c47992010-06-08 18:31:54 -04005622 sdev_cnt = atomic_inc_return(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05005623
James Smart0794d602019-01-28 11:14:19 -08005624 /* For SLI4, all IO buffers are pre-allocated */
5625 if (phba->sli_rev == LPFC_SLI_REV4)
5626 return 0;
5627
5628 /* This code path is now ONLY for SLI3 adapters */
5629
dea31012005-04-17 16:05:31 -05005630 /*
5631 * Populate the cmds_per_lun count scsi_bufs into this host's globally
5632 * available list of scsi buffers. Don't allocate more than the
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04005633 * HBA limit conveyed to the midlayer via the host structure. The
5634 * formula accounts for the lun_queue_depth + error handlers + 1
5635 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea31012005-04-17 16:05:31 -05005636 */
5637 total = phba->total_scsi_bufs;
James Smart3de2a652007-08-02 11:09:59 -04005638 num_to_alloc = vport->cfg_lun_queue_depth + 2;
James Smart92d7f7b2007-06-17 19:56:38 -05005639
James Smartd7c47992010-06-08 18:31:54 -04005640 /* If allocated buffers are enough do nothing */
5641 if ((sdev_cnt * (vport->cfg_lun_queue_depth + 2)) < total)
5642 return 0;
5643
James Smart92d7f7b2007-06-17 19:56:38 -05005644 /* Allow some exchanges to be available always to complete discovery */
5645 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04005646 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5647 "0704 At limitation of %d preallocated "
5648 "command buffers\n", total);
dea31012005-04-17 16:05:31 -05005649 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05005650 /* Allow some exchanges to be available always to complete discovery */
5651 } else if (total + num_to_alloc >
5652 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04005653 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5654 "0705 Allocation request of %d "
5655 "command buffers will exceed max of %d. "
5656 "Reducing allocation request to %d.\n",
5657 num_to_alloc, phba->cfg_hba_queue_depth,
5658 (phba->cfg_hba_queue_depth - total));
dea31012005-04-17 16:05:31 -05005659 num_to_alloc = phba->cfg_hba_queue_depth - total;
5660 }
James Smart0794d602019-01-28 11:14:19 -08005661 num_allocated = lpfc_new_scsi_buf_s3(vport, num_to_alloc);
James Smart3772a992009-05-22 14:50:54 -04005662 if (num_to_alloc != num_allocated) {
James Smart96f70772013-04-17 20:16:15 -04005663 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5664 "0708 Allocation request of %d "
5665 "command buffers did not succeed. "
5666 "Allocated %d buffers.\n",
5667 num_to_alloc, num_allocated);
dea31012005-04-17 16:05:31 -05005668 }
James Smart1c6f4ef52009-11-18 15:40:49 -05005669 if (num_allocated > 0)
5670 phba->total_scsi_bufs += num_allocated;
dea31012005-04-17 16:05:31 -05005671 return 0;
5672}
5673
James Smart9bad7672008-12-04 22:39:02 -05005674/**
James Smart3621a712009-04-06 18:47:14 -04005675 * lpfc_slave_configure - scsi_host_template slave_configure entry point
James Smart9bad7672008-12-04 22:39:02 -05005676 * @sdev: Pointer to scsi_device.
5677 *
5678 * This routine configures following items
5679 * - Tag command queuing support for @sdev if supported.
James Smart9bad7672008-12-04 22:39:02 -05005680 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
5681 *
5682 * Return codes:
5683 * 0 - Success
5684 **/
dea31012005-04-17 16:05:31 -05005685static int
5686lpfc_slave_configure(struct scsi_device *sdev)
5687{
James Smart2e0fef82007-06-17 19:56:36 -05005688 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5689 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05005690
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01005691 scsi_change_queue_depth(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05005692
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005693 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04005694 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -08005695 &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005696 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
5697 lpfc_poll_rearm_timer(phba);
5698 }
5699
dea31012005-04-17 16:05:31 -05005700 return 0;
5701}
5702
James Smart9bad7672008-12-04 22:39:02 -05005703/**
James Smart3621a712009-04-06 18:47:14 -04005704 * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
James Smart9bad7672008-12-04 22:39:02 -05005705 * @sdev: Pointer to scsi_device.
5706 *
5707 * This routine sets @sdev hostatdata filed to null.
5708 **/
dea31012005-04-17 16:05:31 -05005709static void
5710lpfc_slave_destroy(struct scsi_device *sdev)
5711{
James Smartd7c47992010-06-08 18:31:54 -04005712 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5713 struct lpfc_hba *phba = vport->phba;
James Smart1ba981f2014-02-20 09:56:45 -05005714 unsigned long flags;
5715 struct lpfc_device_data *device_data = sdev->hostdata;
5716
James Smartd7c47992010-06-08 18:31:54 -04005717 atomic_dec(&phba->sdev_cnt);
James Smartf38fa0b2014-04-04 13:52:21 -04005718 if ((phba->cfg_fof) && (device_data)) {
James Smart1ba981f2014-02-20 09:56:45 -05005719 spin_lock_irqsave(&phba->devicelock, flags);
5720 device_data->available = false;
5721 if (!device_data->oas_enabled)
5722 lpfc_delete_device_data(phba, device_data);
5723 spin_unlock_irqrestore(&phba->devicelock, flags);
5724 }
dea31012005-04-17 16:05:31 -05005725 sdev->hostdata = NULL;
5726 return;
5727}
5728
James Smart1ba981f2014-02-20 09:56:45 -05005729/**
5730 * lpfc_create_device_data - creates and initializes device data structure for OAS
5731 * @pha: Pointer to host bus adapter structure.
5732 * @vport_wwpn: Pointer to vport's wwpn information
5733 * @target_wwpn: Pointer to target's wwpn information
5734 * @lun: Lun on target
5735 * @atomic_create: Flag to indicate if memory should be allocated using the
5736 * GFP_ATOMIC flag or not.
5737 *
5738 * This routine creates a device data structure which will contain identifying
5739 * information for the device (host wwpn, target wwpn, lun), state of OAS,
5740 * whether or not the corresponding lun is available by the system,
5741 * and pointer to the rport data.
5742 *
5743 * Return codes:
5744 * NULL - Error
5745 * Pointer to lpfc_device_data - Success
5746 **/
5747struct lpfc_device_data*
5748lpfc_create_device_data(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
5749 struct lpfc_name *target_wwpn, uint64_t lun,
James Smartb5749fe2016-12-19 15:07:26 -08005750 uint32_t pri, bool atomic_create)
James Smart1ba981f2014-02-20 09:56:45 -05005751{
5752
5753 struct lpfc_device_data *lun_info;
5754 int memory_flags;
5755
5756 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
James Smartf38fa0b2014-04-04 13:52:21 -04005757 !(phba->cfg_fof))
James Smart1ba981f2014-02-20 09:56:45 -05005758 return NULL;
5759
5760 /* Attempt to create the device data to contain lun info */
5761
5762 if (atomic_create)
5763 memory_flags = GFP_ATOMIC;
5764 else
5765 memory_flags = GFP_KERNEL;
5766 lun_info = mempool_alloc(phba->device_data_mem_pool, memory_flags);
5767 if (!lun_info)
5768 return NULL;
5769 INIT_LIST_HEAD(&lun_info->listentry);
5770 lun_info->rport_data = NULL;
5771 memcpy(&lun_info->device_id.vport_wwpn, vport_wwpn,
5772 sizeof(struct lpfc_name));
5773 memcpy(&lun_info->device_id.target_wwpn, target_wwpn,
5774 sizeof(struct lpfc_name));
5775 lun_info->device_id.lun = lun;
5776 lun_info->oas_enabled = false;
James Smartb5749fe2016-12-19 15:07:26 -08005777 lun_info->priority = pri;
James Smart1ba981f2014-02-20 09:56:45 -05005778 lun_info->available = false;
5779 return lun_info;
5780}
5781
5782/**
5783 * lpfc_delete_device_data - frees a device data structure for OAS
5784 * @pha: Pointer to host bus adapter structure.
5785 * @lun_info: Pointer to device data structure to free.
5786 *
5787 * This routine frees the previously allocated device data structure passed.
5788 *
5789 **/
5790void
5791lpfc_delete_device_data(struct lpfc_hba *phba,
5792 struct lpfc_device_data *lun_info)
5793{
5794
5795 if (unlikely(!phba) || !lun_info ||
James Smartf38fa0b2014-04-04 13:52:21 -04005796 !(phba->cfg_fof))
James Smart1ba981f2014-02-20 09:56:45 -05005797 return;
5798
5799 if (!list_empty(&lun_info->listentry))
5800 list_del(&lun_info->listentry);
5801 mempool_free(lun_info, phba->device_data_mem_pool);
5802 return;
5803}
5804
5805/**
5806 * __lpfc_get_device_data - returns the device data for the specified lun
5807 * @pha: Pointer to host bus adapter structure.
5808 * @list: Point to list to search.
5809 * @vport_wwpn: Pointer to vport's wwpn information
5810 * @target_wwpn: Pointer to target's wwpn information
5811 * @lun: Lun on target
5812 *
5813 * This routine searches the list passed for the specified lun's device data.
5814 * This function does not hold locks, it is the responsibility of the caller
5815 * to ensure the proper lock is held before calling the function.
5816 *
5817 * Return codes:
5818 * NULL - Error
5819 * Pointer to lpfc_device_data - Success
5820 **/
5821struct lpfc_device_data*
5822__lpfc_get_device_data(struct lpfc_hba *phba, struct list_head *list,
5823 struct lpfc_name *vport_wwpn,
5824 struct lpfc_name *target_wwpn, uint64_t lun)
5825{
5826
5827 struct lpfc_device_data *lun_info;
5828
5829 if (unlikely(!phba) || !list || !vport_wwpn || !target_wwpn ||
James Smartf38fa0b2014-04-04 13:52:21 -04005830 !phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005831 return NULL;
5832
5833 /* Check to see if the lun is already enabled for OAS. */
5834
5835 list_for_each_entry(lun_info, list, listentry) {
5836 if ((memcmp(&lun_info->device_id.vport_wwpn, vport_wwpn,
5837 sizeof(struct lpfc_name)) == 0) &&
5838 (memcmp(&lun_info->device_id.target_wwpn, target_wwpn,
5839 sizeof(struct lpfc_name)) == 0) &&
5840 (lun_info->device_id.lun == lun))
5841 return lun_info;
5842 }
5843
5844 return NULL;
5845}
5846
5847/**
5848 * lpfc_find_next_oas_lun - searches for the next oas lun
5849 * @pha: Pointer to host bus adapter structure.
5850 * @vport_wwpn: Pointer to vport's wwpn information
5851 * @target_wwpn: Pointer to target's wwpn information
5852 * @starting_lun: Pointer to the lun to start searching for
5853 * @found_vport_wwpn: Pointer to the found lun's vport wwpn information
5854 * @found_target_wwpn: Pointer to the found lun's target wwpn information
5855 * @found_lun: Pointer to the found lun.
5856 * @found_lun_status: Pointer to status of the found lun.
5857 *
5858 * This routine searches the luns list for the specified lun
5859 * or the first lun for the vport/target. If the vport wwpn contains
5860 * a zero value then a specific vport is not specified. In this case
5861 * any vport which contains the lun will be considered a match. If the
5862 * target wwpn contains a zero value then a specific target is not specified.
5863 * In this case any target which contains the lun will be considered a
5864 * match. If the lun is found, the lun, vport wwpn, target wwpn and lun status
5865 * are returned. The function will also return the next lun if available.
5866 * If the next lun is not found, starting_lun parameter will be set to
5867 * NO_MORE_OAS_LUN.
5868 *
5869 * Return codes:
5870 * non-0 - Error
5871 * 0 - Success
5872 **/
5873bool
5874lpfc_find_next_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
5875 struct lpfc_name *target_wwpn, uint64_t *starting_lun,
5876 struct lpfc_name *found_vport_wwpn,
5877 struct lpfc_name *found_target_wwpn,
5878 uint64_t *found_lun,
James Smartb5749fe2016-12-19 15:07:26 -08005879 uint32_t *found_lun_status,
5880 uint32_t *found_lun_pri)
James Smart1ba981f2014-02-20 09:56:45 -05005881{
5882
5883 unsigned long flags;
5884 struct lpfc_device_data *lun_info;
5885 struct lpfc_device_id *device_id;
5886 uint64_t lun;
5887 bool found = false;
5888
5889 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
5890 !starting_lun || !found_vport_wwpn ||
5891 !found_target_wwpn || !found_lun || !found_lun_status ||
5892 (*starting_lun == NO_MORE_OAS_LUN) ||
James Smartf38fa0b2014-04-04 13:52:21 -04005893 !phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005894 return false;
5895
5896 lun = *starting_lun;
5897 *found_lun = NO_MORE_OAS_LUN;
5898 *starting_lun = NO_MORE_OAS_LUN;
5899
5900 /* Search for lun or the lun closet in value */
5901
5902 spin_lock_irqsave(&phba->devicelock, flags);
5903 list_for_each_entry(lun_info, &phba->luns, listentry) {
5904 if (((wwn_to_u64(vport_wwpn->u.wwn) == 0) ||
5905 (memcmp(&lun_info->device_id.vport_wwpn, vport_wwpn,
5906 sizeof(struct lpfc_name)) == 0)) &&
5907 ((wwn_to_u64(target_wwpn->u.wwn) == 0) ||
5908 (memcmp(&lun_info->device_id.target_wwpn, target_wwpn,
5909 sizeof(struct lpfc_name)) == 0)) &&
5910 (lun_info->oas_enabled)) {
5911 device_id = &lun_info->device_id;
5912 if ((!found) &&
5913 ((lun == FIND_FIRST_OAS_LUN) ||
5914 (device_id->lun == lun))) {
5915 *found_lun = device_id->lun;
5916 memcpy(found_vport_wwpn,
5917 &device_id->vport_wwpn,
5918 sizeof(struct lpfc_name));
5919 memcpy(found_target_wwpn,
5920 &device_id->target_wwpn,
5921 sizeof(struct lpfc_name));
5922 if (lun_info->available)
5923 *found_lun_status =
5924 OAS_LUN_STATUS_EXISTS;
5925 else
5926 *found_lun_status = 0;
James Smartb5749fe2016-12-19 15:07:26 -08005927 *found_lun_pri = lun_info->priority;
James Smart1ba981f2014-02-20 09:56:45 -05005928 if (phba->cfg_oas_flags & OAS_FIND_ANY_VPORT)
5929 memset(vport_wwpn, 0x0,
5930 sizeof(struct lpfc_name));
5931 if (phba->cfg_oas_flags & OAS_FIND_ANY_TARGET)
5932 memset(target_wwpn, 0x0,
5933 sizeof(struct lpfc_name));
5934 found = true;
5935 } else if (found) {
5936 *starting_lun = device_id->lun;
5937 memcpy(vport_wwpn, &device_id->vport_wwpn,
5938 sizeof(struct lpfc_name));
5939 memcpy(target_wwpn, &device_id->target_wwpn,
5940 sizeof(struct lpfc_name));
5941 break;
5942 }
5943 }
5944 }
5945 spin_unlock_irqrestore(&phba->devicelock, flags);
5946 return found;
5947}
5948
5949/**
5950 * lpfc_enable_oas_lun - enables a lun for OAS operations
5951 * @pha: Pointer to host bus adapter structure.
5952 * @vport_wwpn: Pointer to vport's wwpn information
5953 * @target_wwpn: Pointer to target's wwpn information
5954 * @lun: Lun
5955 *
5956 * This routine enables a lun for oas operations. The routines does so by
5957 * doing the following :
5958 *
5959 * 1) Checks to see if the device data for the lun has been created.
5960 * 2) If found, sets the OAS enabled flag if not set and returns.
5961 * 3) Otherwise, creates a device data structure.
5962 * 4) If successfully created, indicates the device data is for an OAS lun,
5963 * indicates the lun is not available and add to the list of luns.
5964 *
5965 * Return codes:
5966 * false - Error
5967 * true - Success
5968 **/
5969bool
5970lpfc_enable_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
James Smartc92c8412016-07-06 12:36:05 -07005971 struct lpfc_name *target_wwpn, uint64_t lun, uint8_t pri)
James Smart1ba981f2014-02-20 09:56:45 -05005972{
5973
5974 struct lpfc_device_data *lun_info;
5975 unsigned long flags;
5976
5977 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
James Smartf38fa0b2014-04-04 13:52:21 -04005978 !phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005979 return false;
5980
5981 spin_lock_irqsave(&phba->devicelock, flags);
5982
5983 /* Check to see if the device data for the lun has been created */
5984 lun_info = __lpfc_get_device_data(phba, &phba->luns, vport_wwpn,
5985 target_wwpn, lun);
5986 if (lun_info) {
5987 if (!lun_info->oas_enabled)
5988 lun_info->oas_enabled = true;
James Smartb5749fe2016-12-19 15:07:26 -08005989 lun_info->priority = pri;
James Smart1ba981f2014-02-20 09:56:45 -05005990 spin_unlock_irqrestore(&phba->devicelock, flags);
5991 return true;
5992 }
5993
5994 /* Create an lun info structure and add to list of luns */
5995 lun_info = lpfc_create_device_data(phba, vport_wwpn, target_wwpn, lun,
James Smart2d71dc82019-05-21 17:48:51 -07005996 pri, true);
James Smart1ba981f2014-02-20 09:56:45 -05005997 if (lun_info) {
5998 lun_info->oas_enabled = true;
James Smartc92c8412016-07-06 12:36:05 -07005999 lun_info->priority = pri;
James Smart1ba981f2014-02-20 09:56:45 -05006000 lun_info->available = false;
6001 list_add_tail(&lun_info->listentry, &phba->luns);
6002 spin_unlock_irqrestore(&phba->devicelock, flags);
6003 return true;
6004 }
6005 spin_unlock_irqrestore(&phba->devicelock, flags);
6006 return false;
6007}
6008
6009/**
6010 * lpfc_disable_oas_lun - disables a lun for OAS operations
6011 * @pha: Pointer to host bus adapter structure.
6012 * @vport_wwpn: Pointer to vport's wwpn information
6013 * @target_wwpn: Pointer to target's wwpn information
6014 * @lun: Lun
6015 *
6016 * This routine disables a lun for oas operations. The routines does so by
6017 * doing the following :
6018 *
6019 * 1) Checks to see if the device data for the lun is created.
6020 * 2) If present, clears the flag indicating this lun is for OAS.
6021 * 3) If the lun is not available by the system, the device data is
6022 * freed.
6023 *
6024 * Return codes:
6025 * false - Error
6026 * true - Success
6027 **/
6028bool
6029lpfc_disable_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
James Smartb5749fe2016-12-19 15:07:26 -08006030 struct lpfc_name *target_wwpn, uint64_t lun, uint8_t pri)
James Smart1ba981f2014-02-20 09:56:45 -05006031{
6032
6033 struct lpfc_device_data *lun_info;
6034 unsigned long flags;
6035
6036 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
James Smartf38fa0b2014-04-04 13:52:21 -04006037 !phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05006038 return false;
6039
6040 spin_lock_irqsave(&phba->devicelock, flags);
6041
6042 /* Check to see if the lun is available. */
6043 lun_info = __lpfc_get_device_data(phba,
6044 &phba->luns, vport_wwpn,
6045 target_wwpn, lun);
6046 if (lun_info) {
6047 lun_info->oas_enabled = false;
James Smartb5749fe2016-12-19 15:07:26 -08006048 lun_info->priority = pri;
James Smart1ba981f2014-02-20 09:56:45 -05006049 if (!lun_info->available)
6050 lpfc_delete_device_data(phba, lun_info);
6051 spin_unlock_irqrestore(&phba->devicelock, flags);
6052 return true;
6053 }
6054
6055 spin_unlock_irqrestore(&phba->devicelock, flags);
6056 return false;
6057}
James Smart92d7f7b2007-06-17 19:56:38 -05006058
James Smart895427b2017-02-12 13:52:30 -08006059static int
6060lpfc_no_command(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
6061{
6062 return SCSI_MLQUEUE_HOST_BUSY;
6063}
6064
6065static int
6066lpfc_no_handler(struct scsi_cmnd *cmnd)
6067{
6068 return FAILED;
6069}
6070
6071static int
6072lpfc_no_slave(struct scsi_device *sdev)
6073{
6074 return -ENODEV;
6075}
6076
6077struct scsi_host_template lpfc_template_nvme = {
6078 .module = THIS_MODULE,
6079 .name = LPFC_DRIVER_NAME,
6080 .proc_name = LPFC_DRIVER_NAME,
6081 .info = lpfc_info,
6082 .queuecommand = lpfc_no_command,
6083 .eh_abort_handler = lpfc_no_handler,
6084 .eh_device_reset_handler = lpfc_no_handler,
6085 .eh_target_reset_handler = lpfc_no_handler,
6086 .eh_bus_reset_handler = lpfc_no_handler,
6087 .eh_host_reset_handler = lpfc_no_handler,
6088 .slave_alloc = lpfc_no_slave,
6089 .slave_configure = lpfc_no_slave,
6090 .scan_finished = lpfc_scan_finished,
6091 .this_id = -1,
6092 .sg_tablesize = 1,
6093 .cmd_per_lun = 1,
James Smart895427b2017-02-12 13:52:30 -08006094 .shost_attrs = lpfc_hba_attrs,
6095 .max_sectors = 0xFFFF,
6096 .vendor_id = LPFC_NL_VENDOR_ID,
6097 .track_queue_depth = 0,
6098};
6099
James Smart96418b52017-03-04 09:30:31 -08006100struct scsi_host_template lpfc_template_no_hr = {
James Smartea4142f2015-04-07 15:07:13 -04006101 .module = THIS_MODULE,
6102 .name = LPFC_DRIVER_NAME,
James Smart08dcd4c2016-07-06 12:35:59 -07006103 .proc_name = LPFC_DRIVER_NAME,
James Smartea4142f2015-04-07 15:07:13 -04006104 .info = lpfc_info,
6105 .queuecommand = lpfc_queuecommand,
James Smart856984b2017-03-04 09:30:32 -08006106 .eh_timed_out = fc_eh_timed_out,
James Smartea4142f2015-04-07 15:07:13 -04006107 .eh_abort_handler = lpfc_abort_handler,
6108 .eh_device_reset_handler = lpfc_device_reset_handler,
6109 .eh_target_reset_handler = lpfc_target_reset_handler,
6110 .eh_bus_reset_handler = lpfc_bus_reset_handler,
6111 .slave_alloc = lpfc_slave_alloc,
6112 .slave_configure = lpfc_slave_configure,
6113 .slave_destroy = lpfc_slave_destroy,
6114 .scan_finished = lpfc_scan_finished,
6115 .this_id = -1,
6116 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
6117 .cmd_per_lun = LPFC_CMD_PER_LUN,
James Smartea4142f2015-04-07 15:07:13 -04006118 .shost_attrs = lpfc_hba_attrs,
James Smartd79c9e92019-08-14 16:57:09 -07006119 .max_sectors = 0xFFFFFFFF,
James Smartea4142f2015-04-07 15:07:13 -04006120 .vendor_id = LPFC_NL_VENDOR_ID,
6121 .change_queue_depth = scsi_change_queue_depth,
James Smartea4142f2015-04-07 15:07:13 -04006122 .track_queue_depth = 1,
6123};
6124
dea31012005-04-17 16:05:31 -05006125struct scsi_host_template lpfc_template = {
6126 .module = THIS_MODULE,
6127 .name = LPFC_DRIVER_NAME,
James Smart08dcd4c2016-07-06 12:35:59 -07006128 .proc_name = LPFC_DRIVER_NAME,
dea31012005-04-17 16:05:31 -05006129 .info = lpfc_info,
6130 .queuecommand = lpfc_queuecommand,
Christoph Hellwigb6a05c82017-01-30 13:18:58 +01006131 .eh_timed_out = fc_eh_timed_out,
dea31012005-04-17 16:05:31 -05006132 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04006133 .eh_device_reset_handler = lpfc_device_reset_handler,
6134 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart7054a602007-04-25 09:52:34 -04006135 .eh_bus_reset_handler = lpfc_bus_reset_handler,
James Smart27b01b82012-05-09 21:19:44 -04006136 .eh_host_reset_handler = lpfc_host_reset_handler,
dea31012005-04-17 16:05:31 -05006137 .slave_alloc = lpfc_slave_alloc,
6138 .slave_configure = lpfc_slave_configure,
6139 .slave_destroy = lpfc_slave_destroy,
James Smart47a86172007-04-25 09:53:22 -04006140 .scan_finished = lpfc_scan_finished,
dea31012005-04-17 16:05:31 -05006141 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05006142 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
dea31012005-04-17 16:05:31 -05006143 .cmd_per_lun = LPFC_CMD_PER_LUN,
James Smart2e0fef82007-06-17 19:56:36 -05006144 .shost_attrs = lpfc_hba_attrs,
James.Smart@Emulex.Com564b2962005-06-25 10:34:17 -04006145 .max_sectors = 0xFFFF,
James Smartf1c3b0f2009-07-19 10:01:32 -04006146 .vendor_id = LPFC_NL_VENDOR_ID,
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01006147 .change_queue_depth = scsi_change_queue_depth,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01006148 .track_queue_depth = 1,
dea31012005-04-17 16:05:31 -05006149};
James Smart3de2a652007-08-02 11:09:59 -04006150
6151struct scsi_host_template lpfc_vport_template = {
6152 .module = THIS_MODULE,
6153 .name = LPFC_DRIVER_NAME,
James Smart08dcd4c2016-07-06 12:35:59 -07006154 .proc_name = LPFC_DRIVER_NAME,
James Smart3de2a652007-08-02 11:09:59 -04006155 .info = lpfc_info,
6156 .queuecommand = lpfc_queuecommand,
Christoph Hellwigb6a05c82017-01-30 13:18:58 +01006157 .eh_timed_out = fc_eh_timed_out,
James Smart3de2a652007-08-02 11:09:59 -04006158 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04006159 .eh_device_reset_handler = lpfc_device_reset_handler,
6160 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart3de2a652007-08-02 11:09:59 -04006161 .slave_alloc = lpfc_slave_alloc,
6162 .slave_configure = lpfc_slave_configure,
6163 .slave_destroy = lpfc_slave_destroy,
6164 .scan_finished = lpfc_scan_finished,
6165 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05006166 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
James Smart3de2a652007-08-02 11:09:59 -04006167 .cmd_per_lun = LPFC_CMD_PER_LUN,
James Smart3de2a652007-08-02 11:09:59 -04006168 .shost_attrs = lpfc_vport_attrs,
6169 .max_sectors = 0xFFFF,
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01006170 .change_queue_depth = scsi_change_queue_depth,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01006171 .track_queue_depth = 1,
James Smart3de2a652007-08-02 11:09:59 -04006172};