blob: 63841f49affbfad00064d3fd2a917954b1308afd [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 Smart128bdda2018-01-30 15:59:03 -08004 * Copyright (C) 2017-2018 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
86lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
James Smart1c6f4ef52009-11-18 15:40:49 -050087static void
88lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_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,
183 struct lpfc_scsi_buf *lpfc_cmd)
184{
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
203lpfc_update_stats(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
204{
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;
dea31012005-04-17 16:05:31 -0500392 struct lpfc_scsi_buf *psb;
393 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++) {
411 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
412 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 Smart0794d602019-01-28 11:14:19 -0800445 bpl = 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 Smart1c6f4ef52009-11-18 15:40:49 -0500509 lpfc_release_scsi_buf_s3(phba, psb);
James Smart3772a992009-05-22 14:50:54 -0400510
James Smart34b02dc2008-08-24 21:49:55 -0400511 }
dea31012005-04-17 16:05:31 -0500512
James Smart3772a992009-05-22 14:50:54 -0400513 return bcnt;
dea31012005-04-17 16:05:31 -0500514}
515
James Smart9bad7672008-12-04 22:39:02 -0500516/**
James Smart1151e3e2011-02-16 12:39:35 -0500517 * lpfc_sli4_vport_delete_fcp_xri_aborted -Remove all ndlp references for vport
518 * @vport: pointer to lpfc vport data structure.
519 *
520 * This routine is invoked by the vport cleanup for deletions and the cleanup
521 * for an ndlp on removal.
522 **/
523void
524lpfc_sli4_vport_delete_fcp_xri_aborted(struct lpfc_vport *vport)
525{
526 struct lpfc_hba *phba = vport->phba;
527 struct lpfc_scsi_buf *psb, *next_psb;
528 unsigned long iflag = 0;
529
James Smart895427b2017-02-12 13:52:30 -0800530 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
531 return;
James Smart1151e3e2011-02-16 12:39:35 -0500532 spin_lock_irqsave(&phba->hbalock, iflag);
533 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
534 list_for_each_entry_safe(psb, next_psb,
535 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
536 if (psb->rdata && psb->rdata->pnode
537 && psb->rdata->pnode->vport == vport)
538 psb->rdata = NULL;
539 }
540 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
541 spin_unlock_irqrestore(&phba->hbalock, iflag);
542}
543
544/**
James Smartda0436e2009-05-22 14:51:39 -0400545 * lpfc_sli4_fcp_xri_aborted - Fast-path process of fcp xri abort
546 * @phba: pointer to lpfc hba data structure.
547 * @axri: pointer to the fcp xri abort wcqe structure.
548 *
549 * This routine is invoked by the worker thread to process a SLI4 fast-path
550 * FCP aborted xri.
551 **/
552void
553lpfc_sli4_fcp_xri_aborted(struct lpfc_hba *phba,
554 struct sli4_wcqe_xri_aborted *axri)
555{
556 uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
James Smart19ca7602010-11-20 23:11:55 -0500557 uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
James Smartda0436e2009-05-22 14:51:39 -0400558 struct lpfc_scsi_buf *psb, *next_psb;
559 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -0500560 struct lpfc_iocbq *iocbq;
561 int i;
James Smart19ca7602010-11-20 23:11:55 -0500562 struct lpfc_nodelist *ndlp;
563 int rrq_empty = 0;
James Smart895427b2017-02-12 13:52:30 -0800564 struct lpfc_sli_ring *pring = phba->sli4_hba.els_wq->pring;
James Smartda0436e2009-05-22 14:51:39 -0400565
James Smart895427b2017-02-12 13:52:30 -0800566 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
567 return;
James Smart0f65ff62010-02-26 14:14:23 -0500568 spin_lock_irqsave(&phba->hbalock, iflag);
569 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
James Smartda0436e2009-05-22 14:51:39 -0400570 list_for_each_entry_safe(psb, next_psb,
571 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
572 if (psb->cur_iocbq.sli4_xritag == xri) {
573 list_del(&psb->list);
James Smart341af102010-01-26 23:07:37 -0500574 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400575 psb->status = IOSTAT_SUCCESS;
James Smart0f65ff62010-02-26 14:14:23 -0500576 spin_unlock(
577 &phba->sli4_hba.abts_scsi_buf_list_lock);
James Smart1151e3e2011-02-16 12:39:35 -0500578 if (psb->rdata && psb->rdata->pnode)
579 ndlp = psb->rdata->pnode;
580 else
581 ndlp = NULL;
582
James Smart19ca7602010-11-20 23:11:55 -0500583 rrq_empty = list_empty(&phba->active_rrq_list);
James Smart0f65ff62010-02-26 14:14:23 -0500584 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartcb69f7d2011-12-13 13:21:57 -0500585 if (ndlp) {
James Smartee0f4fe2012-05-09 21:19:14 -0400586 lpfc_set_rrq_active(phba, ndlp,
587 psb->cur_iocbq.sli4_lxritag, rxid, 1);
James Smartcb69f7d2011-12-13 13:21:57 -0500588 lpfc_sli4_abts_err_handler(phba, ndlp, axri);
589 }
James Smartda0436e2009-05-22 14:51:39 -0400590 lpfc_release_scsi_buf_s4(phba, psb);
James Smart19ca7602010-11-20 23:11:55 -0500591 if (rrq_empty)
592 lpfc_worker_wake_up(phba);
James Smartda0436e2009-05-22 14:51:39 -0400593 return;
594 }
595 }
James Smart0f65ff62010-02-26 14:14:23 -0500596 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
597 for (i = 1; i <= phba->sli.last_iotag; i++) {
598 iocbq = phba->sli.iocbq_lookup[i];
599
600 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
601 (iocbq->iocb_flag & LPFC_IO_LIBDFC))
602 continue;
603 if (iocbq->sli4_xritag != xri)
604 continue;
605 psb = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
606 psb->exch_busy = 0;
607 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart0e9bb8d2013-03-01 16:35:12 -0500608 if (!list_empty(&pring->txq))
James Smart589a52d2010-07-14 15:30:54 -0400609 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -0500610 return;
611
612 }
613 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartda0436e2009-05-22 14:51:39 -0400614}
615
616/**
James Smart19ca7602010-11-20 23:11:55 -0500617 * lpfc_get_scsi_buf_s3 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
James Smart3772a992009-05-22 14:50:54 -0400618 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -0500619 *
620 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
621 * and returns to caller.
622 *
623 * Return codes:
624 * NULL - Error
625 * Pointer to lpfc_scsi_buf - Success
626 **/
Adrian Bunk455c53e2006-01-06 20:21:28 +0100627static struct lpfc_scsi_buf*
James Smart19ca7602010-11-20 23:11:55 -0500628lpfc_get_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -0500629{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400630 struct lpfc_scsi_buf * lpfc_cmd = NULL;
James Smarta40fc5f2013-04-17 20:17:40 -0400631 struct list_head *scsi_buf_list_get = &phba->lpfc_scsi_buf_list_get;
James Smart164cecd2013-09-06 12:22:38 -0400632 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500633
James Smart164cecd2013-09-06 12:22:38 -0400634 spin_lock_irqsave(&phba->scsi_buf_list_get_lock, iflag);
James Smarta40fc5f2013-04-17 20:17:40 -0400635 list_remove_head(scsi_buf_list_get, lpfc_cmd, struct lpfc_scsi_buf,
636 list);
637 if (!lpfc_cmd) {
James Smart164cecd2013-09-06 12:22:38 -0400638 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -0400639 list_splice(&phba->lpfc_scsi_buf_list_put,
640 &phba->lpfc_scsi_buf_list_get);
641 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
642 list_remove_head(scsi_buf_list_get, lpfc_cmd,
643 struct lpfc_scsi_buf, list);
James Smart164cecd2013-09-06 12:22:38 -0400644 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smart1dcb58e2007-04-25 09:51:30 -0400645 }
James Smart164cecd2013-09-06 12:22:38 -0400646 spin_unlock_irqrestore(&phba->scsi_buf_list_get_lock, iflag);
James Smart2a5b7d62018-07-31 17:23:22 -0700647
648 if (lpfc_ndlp_check_qdepth(phba, ndlp) && lpfc_cmd) {
649 atomic_inc(&ndlp->cmd_pending);
650 lpfc_cmd->flags |= LPFC_SBUF_BUMP_QDEPTH;
651 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400652 return lpfc_cmd;
653}
James Smart19ca7602010-11-20 23:11:55 -0500654/**
James Smart0794d602019-01-28 11:14:19 -0800655 * lpfc_get_scsi_buf_s4 - Get a scsi buffer from lpfc_common_buf_list of the HBA
James Smart19ca7602010-11-20 23:11:55 -0500656 * @phba: The HBA for which this call is being executed.
657 *
James Smart0794d602019-01-28 11:14:19 -0800658 * This routine removes a scsi buffer from head of @phba lpfc_common_buf_list
James Smart19ca7602010-11-20 23:11:55 -0500659 * and returns to caller.
660 *
661 * Return codes:
662 * NULL - Error
663 * Pointer to lpfc_scsi_buf - Success
664 **/
665static struct lpfc_scsi_buf*
666lpfc_get_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
667{
James Smart3be30e02013-05-31 17:05:17 -0400668 struct lpfc_scsi_buf *lpfc_cmd, *lpfc_cmd_next;
James Smart164cecd2013-09-06 12:22:38 -0400669 unsigned long iflag = 0;
James Smart0794d602019-01-28 11:14:19 -0800670 struct sli4_sge *sgl;
671 IOCB_t *iocb;
672 dma_addr_t pdma_phys_fcp_rsp;
673 dma_addr_t pdma_phys_fcp_cmd;
674 uint32_t sgl_size;
James Smart19ca7602010-11-20 23:11:55 -0500675 int found = 0;
676
James Smart0794d602019-01-28 11:14:19 -0800677 spin_lock_irqsave(&phba->common_buf_list_get_lock, iflag);
James Smart3be30e02013-05-31 17:05:17 -0400678 list_for_each_entry_safe(lpfc_cmd, lpfc_cmd_next,
James Smart0794d602019-01-28 11:14:19 -0800679 &phba->lpfc_common_buf_list_get, list) {
James Smart19ca7602010-11-20 23:11:55 -0500680 if (lpfc_test_rrq_active(phba, ndlp,
James Smartee0f4fe2012-05-09 21:19:14 -0400681 lpfc_cmd->cur_iocbq.sli4_lxritag))
James Smart1151e3e2011-02-16 12:39:35 -0500682 continue;
James Smart59c68ea2018-04-09 14:24:25 -0700683 list_del_init(&lpfc_cmd->list);
James Smart0794d602019-01-28 11:14:19 -0800684 phba->get_common_bufs--;
James Smart19ca7602010-11-20 23:11:55 -0500685 found = 1;
James Smart1151e3e2011-02-16 12:39:35 -0500686 break;
James Smart19ca7602010-11-20 23:11:55 -0500687 }
James Smarta40fc5f2013-04-17 20:17:40 -0400688 if (!found) {
James Smart0794d602019-01-28 11:14:19 -0800689 spin_lock(&phba->common_buf_list_put_lock);
690 list_splice(&phba->lpfc_common_buf_list_put,
691 &phba->lpfc_common_buf_list_get);
692 phba->get_common_bufs += phba->put_common_bufs;
693 INIT_LIST_HEAD(&phba->lpfc_common_buf_list_put);
694 phba->put_common_bufs = 0;
695 spin_unlock(&phba->common_buf_list_put_lock);
James Smart3be30e02013-05-31 17:05:17 -0400696 list_for_each_entry_safe(lpfc_cmd, lpfc_cmd_next,
James Smart0794d602019-01-28 11:14:19 -0800697 &phba->lpfc_common_buf_list_get,
698 list) {
James Smarta40fc5f2013-04-17 20:17:40 -0400699 if (lpfc_test_rrq_active(
700 phba, ndlp, lpfc_cmd->cur_iocbq.sli4_lxritag))
701 continue;
James Smart59c68ea2018-04-09 14:24:25 -0700702 list_del_init(&lpfc_cmd->list);
James Smart0794d602019-01-28 11:14:19 -0800703 phba->get_common_bufs--;
James Smarta40fc5f2013-04-17 20:17:40 -0400704 found = 1;
705 break;
706 }
707 }
James Smart0794d602019-01-28 11:14:19 -0800708 spin_unlock_irqrestore(&phba->common_buf_list_get_lock, iflag);
James Smart1151e3e2011-02-16 12:39:35 -0500709 if (!found)
710 return NULL;
James Smart2a5b7d62018-07-31 17:23:22 -0700711
James Smart0794d602019-01-28 11:14:19 -0800712 sgl_size = phba->cfg_sg_dma_buf_size -
713 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
714
715 /* Setup key fields in buffer that may have been changed
716 * if other protocols used this buffer.
717 */
718 lpfc_cmd->cur_iocbq.iocb_flag = LPFC_IO_FCP;
719 lpfc_cmd->prot_seg_cnt = 0;
720 lpfc_cmd->seg_cnt = 0;
721 lpfc_cmd->waitq = NULL;
722 lpfc_cmd->timeout = 0;
723 lpfc_cmd->flags = 0;
724 lpfc_cmd->start_time = jiffies;
725 lpfc_cmd->waitq = NULL;
726 lpfc_cmd->cpu = smp_processor_id();
727#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
728 lpfc_cmd->prot_data_type = 0;
729#endif
730
731 lpfc_cmd->fcp_cmnd = (lpfc_cmd->data + sgl_size);
732 lpfc_cmd->fcp_rsp = (struct fcp_rsp *)((uint8_t *)lpfc_cmd->fcp_cmnd +
733 sizeof(struct fcp_cmnd));
734
735 /*
736 * The first two SGEs are the FCP_CMD and FCP_RSP.
737 * The balance are sg list bdes. Initialize the
738 * first two and leave the rest for queuecommand.
739 */
740 sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
741 pdma_phys_fcp_cmd = (lpfc_cmd->dma_handle + sgl_size);
742 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
743 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
744 sgl->word2 = le32_to_cpu(sgl->word2);
745 bf_set(lpfc_sli4_sge_last, sgl, 0);
746 sgl->word2 = cpu_to_le32(sgl->word2);
747 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_cmnd));
748 sgl++;
749
750 /* Setup the physical region for the FCP RSP */
751 pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
752 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
753 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
754 sgl->word2 = le32_to_cpu(sgl->word2);
755 bf_set(lpfc_sli4_sge_last, sgl, 1);
756 sgl->word2 = cpu_to_le32(sgl->word2);
757 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
758
759 /*
760 * Since the IOCB for the FCP I/O is built into this
761 * lpfc_scsi_buf, initialize it with all known data now.
762 */
763 iocb = &lpfc_cmd->cur_iocbq.iocb;
764 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
765 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
766 /* setting the BLP size to 2 * sizeof BDE may not be correct.
767 * We are setting the bpl to point to out sgl. An sgl's
768 * entries are 16 bytes, a bpl entries are 12 bytes.
769 */
770 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
771 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
772 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
773 iocb->ulpBdeCount = 1;
774 iocb->ulpLe = 1;
775 iocb->ulpClass = CLASS3;
776
Dan Carpenter26c724a2018-08-23 16:56:33 +0300777 if (lpfc_ndlp_check_qdepth(phba, ndlp)) {
James Smart2a5b7d62018-07-31 17:23:22 -0700778 atomic_inc(&ndlp->cmd_pending);
779 lpfc_cmd->flags |= LPFC_SBUF_BUMP_QDEPTH;
780 }
James Smarta40fc5f2013-04-17 20:17:40 -0400781 return lpfc_cmd;
James Smart19ca7602010-11-20 23:11:55 -0500782}
783/**
784 * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
785 * @phba: The HBA for which this call is being executed.
786 *
787 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
788 * and returns to caller.
789 *
790 * Return codes:
791 * NULL - Error
792 * Pointer to lpfc_scsi_buf - Success
793 **/
794static struct lpfc_scsi_buf*
795lpfc_get_scsi_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
796{
797 return phba->lpfc_get_scsi_buf(phba, ndlp);
798}
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400799
James Smart9bad7672008-12-04 22:39:02 -0500800/**
James Smart3772a992009-05-22 14:50:54 -0400801 * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
James Smart9bad7672008-12-04 22:39:02 -0500802 * @phba: The Hba for which this call is being executed.
803 * @psb: The scsi buffer which is being released.
804 *
805 * This routine releases @psb scsi buffer by adding it to tail of @phba
806 * lpfc_scsi_buf_list list.
807 **/
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400808static void
James Smart3772a992009-05-22 14:50:54 -0400809lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400810{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500811 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500812
James Smarta40fc5f2013-04-17 20:17:40 -0400813 psb->seg_cnt = 0;
James Smarta40fc5f2013-04-17 20:17:40 -0400814 psb->prot_seg_cnt = 0;
815
816 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400817 psb->pCmd = NULL;
James Smart6a485eb2013-04-17 20:19:00 -0400818 psb->cur_iocbq.iocb_flag = LPFC_IO_FCP;
James Smarta40fc5f2013-04-17 20:17:40 -0400819 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list_put);
820 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
dea31012005-04-17 16:05:31 -0500821}
822
James Smart9bad7672008-12-04 22:39:02 -0500823/**
James Smartda0436e2009-05-22 14:51:39 -0400824 * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
825 * @phba: The Hba for which this call is being executed.
826 * @psb: The scsi buffer which is being released.
827 *
828 * This routine releases @psb scsi buffer by adding it to tail of @phba
James Smart0794d602019-01-28 11:14:19 -0800829 * lpfc_common_buf_list list. For SLI4 XRI's are tied to the scsi buffer
James Smartda0436e2009-05-22 14:51:39 -0400830 * and cannot be reused for at least RA_TOV amount of time if it was
831 * aborted.
832 **/
833static void
834lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
835{
836 unsigned long iflag = 0;
837
James Smarta40fc5f2013-04-17 20:17:40 -0400838 psb->seg_cnt = 0;
James Smarta40fc5f2013-04-17 20:17:40 -0400839 psb->prot_seg_cnt = 0;
840
James Smart341af102010-01-26 23:07:37 -0500841 if (psb->exch_busy) {
James Smartda0436e2009-05-22 14:51:39 -0400842 spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock,
843 iflag);
844 psb->pCmd = NULL;
845 list_add_tail(&psb->list,
846 &phba->sli4_hba.lpfc_abts_scsi_buf_list);
847 spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
848 iflag);
849 } else {
James Smart0794d602019-01-28 11:14:19 -0800850 /* MUST zero fields if buffer is reused by another protocol */
James Smartda0436e2009-05-22 14:51:39 -0400851 psb->pCmd = NULL;
James Smart0794d602019-01-28 11:14:19 -0800852 psb->cur_iocbq.iocb_cmpl = NULL;
853 spin_lock_irqsave(&phba->common_buf_list_put_lock, iflag);
854 list_add_tail(&psb->list, &phba->lpfc_common_buf_list_put);
855 phba->put_common_bufs++;
856 spin_unlock_irqrestore(&phba->common_buf_list_put_lock, iflag);
James Smartda0436e2009-05-22 14:51:39 -0400857 }
858}
859
860/**
James Smart3772a992009-05-22 14:50:54 -0400861 * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
862 * @phba: The Hba for which this call is being executed.
863 * @psb: The scsi buffer which is being released.
864 *
865 * This routine releases @psb scsi buffer by adding it to tail of @phba
866 * lpfc_scsi_buf_list list.
867 **/
868static void
869lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
870{
James Smart2a5b7d62018-07-31 17:23:22 -0700871 if ((psb->flags & LPFC_SBUF_BUMP_QDEPTH) && psb->ndlp)
872 atomic_dec(&psb->ndlp->cmd_pending);
James Smart3772a992009-05-22 14:50:54 -0400873
James Smart2a5b7d62018-07-31 17:23:22 -0700874 psb->flags &= ~LPFC_SBUF_BUMP_QDEPTH;
James Smart3772a992009-05-22 14:50:54 -0400875 phba->lpfc_release_scsi_buf(phba, psb);
876}
877
878/**
879 * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -0500880 * @phba: The Hba for which this call is being executed.
881 * @lpfc_cmd: The scsi buffer which is going to be mapped.
882 *
883 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
James Smart3772a992009-05-22 14:50:54 -0400884 * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -0700885 * through sg elements and format the bde. This routine also initializes all
James Smart3772a992009-05-22 14:50:54 -0400886 * IOCB fields which are dependent on scsi command request buffer.
James Smart9bad7672008-12-04 22:39:02 -0500887 *
888 * Return codes:
889 * 1 - Error
890 * 0 - Success
891 **/
dea31012005-04-17 16:05:31 -0500892static int
James Smart3772a992009-05-22 14:50:54 -0400893lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
dea31012005-04-17 16:05:31 -0500894{
895 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
896 struct scatterlist *sgel = NULL;
897 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart0794d602019-01-28 11:14:19 -0800898 struct ulp_bde64 *bpl = lpfc_cmd->dma_sgl;
James Smart0f65ff62010-02-26 14:14:23 -0500899 struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
dea31012005-04-17 16:05:31 -0500900 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart34b02dc2008-08-24 21:49:55 -0400901 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
dea31012005-04-17 16:05:31 -0500902 dma_addr_t physaddr;
James Smart34b02dc2008-08-24 21:49:55 -0400903 uint32_t num_bde = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500904 int nseg, datadir = scsi_cmnd->sc_data_direction;
dea31012005-04-17 16:05:31 -0500905
906 /*
907 * There are three possibilities here - use scatter-gather segment, use
908 * the single mapping, or neither. Start the lpfc command prep by
909 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
910 * data bde entry.
911 */
912 bpl += 2;
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -0700913 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -0500914 /*
915 * The driver stores the segment count returned from pci_map_sg
916 * because this a count of dma-mappings used to map the use_sg
917 * pages. They are not guaranteed to be the same for those
918 * architectures that implement an IOMMU.
919 */
dea31012005-04-17 16:05:31 -0500920
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -0700921 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
922 scsi_sg_count(scsi_cmnd), datadir);
923 if (unlikely(!nseg))
924 return 1;
925
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500926 lpfc_cmd->seg_cnt = nseg;
dea31012005-04-17 16:05:31 -0500927 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -0400928 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
929 "9064 BLKGRD: %s: Too many sg segments from "
James Smarte2a0a9d2008-12-04 22:40:02 -0500930 "dma_map_sg. Config %d, seg_cnt %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700931 __func__, phba->cfg_sg_seg_cnt,
dea31012005-04-17 16:05:31 -0500932 lpfc_cmd->seg_cnt);
James Smart96f70772013-04-17 20:16:15 -0400933 lpfc_cmd->seg_cnt = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500934 scsi_dma_unmap(scsi_cmnd);
dea31012005-04-17 16:05:31 -0500935 return 1;
936 }
937
938 /*
939 * The driver established a maximum scatter-gather segment count
940 * during probe that limits the number of sg elements in any
941 * single scsi command. Just run through the seg_cnt and format
942 * the bde's.
James Smart34b02dc2008-08-24 21:49:55 -0400943 * When using SLI-3 the driver will try to fit all the BDEs into
944 * the IOCB. If it can't then the BDEs get added to a BPL as it
945 * does for SLI-2 mode.
dea31012005-04-17 16:05:31 -0500946 */
James Smart34b02dc2008-08-24 21:49:55 -0400947 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
dea31012005-04-17 16:05:31 -0500948 physaddr = sg_dma_address(sgel);
James Smart34b02dc2008-08-24 21:49:55 -0400949 if (phba->sli_rev == 3 &&
James Smarte2a0a9d2008-12-04 22:40:02 -0500950 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
James Smart0f65ff62010-02-26 14:14:23 -0500951 !(iocbq->iocb_flag & DSS_SECURITY_OP) &&
James Smart34b02dc2008-08-24 21:49:55 -0400952 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
953 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
954 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
955 data_bde->addrLow = putPaddrLow(physaddr);
956 data_bde->addrHigh = putPaddrHigh(physaddr);
957 data_bde++;
958 } else {
959 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
960 bpl->tus.f.bdeSize = sg_dma_len(sgel);
961 bpl->tus.w = le32_to_cpu(bpl->tus.w);
962 bpl->addrLow =
963 le32_to_cpu(putPaddrLow(physaddr));
964 bpl->addrHigh =
965 le32_to_cpu(putPaddrHigh(physaddr));
966 bpl++;
967 }
dea31012005-04-17 16:05:31 -0500968 }
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -0700969 }
dea31012005-04-17 16:05:31 -0500970
971 /*
972 * Finish initializing those IOCB fields that are dependent on the
James Smart34b02dc2008-08-24 21:49:55 -0400973 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
974 * explicitly reinitialized and for SLI-3 the extended bde count is
975 * explicitly reinitialized since all iocb memory resources are reused.
dea31012005-04-17 16:05:31 -0500976 */
James Smarte2a0a9d2008-12-04 22:40:02 -0500977 if (phba->sli_rev == 3 &&
James Smart0f65ff62010-02-26 14:14:23 -0500978 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
979 !(iocbq->iocb_flag & DSS_SECURITY_OP)) {
James Smart34b02dc2008-08-24 21:49:55 -0400980 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
981 /*
982 * The extended IOCB format can only fit 3 BDE or a BPL.
983 * This I/O has more than 3 BDE so the 1st data bde will
984 * be a BPL that is filled in here.
985 */
986 physaddr = lpfc_cmd->dma_handle;
987 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
988 data_bde->tus.f.bdeSize = (num_bde *
989 sizeof(struct ulp_bde64));
990 physaddr += (sizeof(struct fcp_cmnd) +
991 sizeof(struct fcp_rsp) +
992 (2 * sizeof(struct ulp_bde64)));
993 data_bde->addrHigh = putPaddrHigh(physaddr);
994 data_bde->addrLow = putPaddrLow(physaddr);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300995 /* ebde count includes the response bde and data bpl */
James Smart34b02dc2008-08-24 21:49:55 -0400996 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
997 } else {
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300998 /* ebde count includes the response bde and data bdes */
James Smart34b02dc2008-08-24 21:49:55 -0400999 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1000 }
1001 } else {
1002 iocb_cmd->un.fcpi64.bdl.bdeSize =
1003 ((num_bde + 2) * sizeof(struct ulp_bde64));
James Smart0f65ff62010-02-26 14:14:23 -05001004 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
James Smart34b02dc2008-08-24 21:49:55 -04001005 }
James Smart09372822008-01-11 01:52:54 -05001006 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
James Smarte2a0a9d2008-12-04 22:40:02 -05001007
1008 /*
1009 * Due to difference in data length between DIF/non-DIF paths,
1010 * we need to set word 4 of IOCB here
1011 */
James Smarta257bf92009-04-06 18:48:10 -04001012 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001013 return 0;
1014}
1015
James Smartf9bb2da2011-10-10 21:34:11 -04001016#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001017
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -07001018/* Return BG_ERR_INIT if error injection is detected by Initiator */
James Smart9a6b09c2012-03-01 22:37:42 -05001019#define BG_ERR_INIT 0x1
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -07001020/* Return BG_ERR_TGT if error injection is detected by Target */
James Smart9a6b09c2012-03-01 22:37:42 -05001021#define BG_ERR_TGT 0x2
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -07001022/* Return BG_ERR_SWAP if swapping CSUM<-->CRC is required for error injection */
James Smart9a6b09c2012-03-01 22:37:42 -05001023#define BG_ERR_SWAP 0x10
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -07001024/**
1025 * Return BG_ERR_CHECK if disabling Guard/Ref/App checking is required for
1026 * error injection
1027 **/
James Smart9a6b09c2012-03-01 22:37:42 -05001028#define BG_ERR_CHECK 0x20
James Smartacd68592012-01-18 16:25:09 -05001029
1030/**
1031 * lpfc_bg_err_inject - Determine if we should inject an error
1032 * @phba: The Hba for which this call is being executed.
James Smartf9bb2da2011-10-10 21:34:11 -04001033 * @sc: The SCSI command to examine
1034 * @reftag: (out) BlockGuard reference tag for transmitted data
1035 * @apptag: (out) BlockGuard application tag for transmitted data
1036 * @new_guard (in) Value to replace CRC with if needed
1037 *
James Smart9a6b09c2012-03-01 22:37:42 -05001038 * Returns BG_ERR_* bit mask or 0 if request ignored
James Smartacd68592012-01-18 16:25:09 -05001039 **/
James Smartf9bb2da2011-10-10 21:34:11 -04001040static int
1041lpfc_bg_err_inject(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1042 uint32_t *reftag, uint16_t *apptag, uint32_t new_guard)
1043{
1044 struct scatterlist *sgpe; /* s/g prot entry */
James Smart9a6b09c2012-03-01 22:37:42 -05001045 struct lpfc_scsi_buf *lpfc_cmd = NULL;
James Smartacd68592012-01-18 16:25:09 -05001046 struct scsi_dif_tuple *src = NULL;
James Smart4ac9b222012-03-01 22:38:29 -05001047 struct lpfc_nodelist *ndlp;
1048 struct lpfc_rport_data *rdata;
James Smartf9bb2da2011-10-10 21:34:11 -04001049 uint32_t op = scsi_get_prot_op(sc);
1050 uint32_t blksize;
1051 uint32_t numblks;
1052 sector_t lba;
1053 int rc = 0;
James Smartacd68592012-01-18 16:25:09 -05001054 int blockoff = 0;
James Smartf9bb2da2011-10-10 21:34:11 -04001055
1056 if (op == SCSI_PROT_NORMAL)
1057 return 0;
1058
James Smartacd68592012-01-18 16:25:09 -05001059 sgpe = scsi_prot_sglist(sc);
James Smartf9bb2da2011-10-10 21:34:11 -04001060 lba = scsi_get_lba(sc);
James Smart4ac9b222012-03-01 22:38:29 -05001061
1062 /* First check if we need to match the LBA */
James Smartf9bb2da2011-10-10 21:34:11 -04001063 if (phba->lpfc_injerr_lba != LPFC_INJERR_LBA_OFF) {
1064 blksize = lpfc_cmd_blksize(sc);
1065 numblks = (scsi_bufflen(sc) + blksize - 1) / blksize;
1066
1067 /* Make sure we have the right LBA if one is specified */
1068 if ((phba->lpfc_injerr_lba < lba) ||
1069 (phba->lpfc_injerr_lba >= (lba + numblks)))
1070 return 0;
James Smartacd68592012-01-18 16:25:09 -05001071 if (sgpe) {
1072 blockoff = phba->lpfc_injerr_lba - lba;
1073 numblks = sg_dma_len(sgpe) /
1074 sizeof(struct scsi_dif_tuple);
1075 if (numblks < blockoff)
1076 blockoff = numblks;
James Smartacd68592012-01-18 16:25:09 -05001077 }
James Smartf9bb2da2011-10-10 21:34:11 -04001078 }
1079
James Smart4ac9b222012-03-01 22:38:29 -05001080 /* Next check if we need to match the remote NPortID or WWPN */
James Smart1ba981f2014-02-20 09:56:45 -05001081 rdata = lpfc_rport_data_from_scsi_device(sc->device);
James Smart4ac9b222012-03-01 22:38:29 -05001082 if (rdata && rdata->pnode) {
1083 ndlp = rdata->pnode;
1084
1085 /* Make sure we have the right NPortID if one is specified */
1086 if (phba->lpfc_injerr_nportid &&
1087 (phba->lpfc_injerr_nportid != ndlp->nlp_DID))
1088 return 0;
1089
1090 /*
1091 * Make sure we have the right WWPN if one is specified.
1092 * wwn[0] should be a non-zero NAA in a good WWPN.
1093 */
1094 if (phba->lpfc_injerr_wwpn.u.wwn[0] &&
1095 (memcmp(&ndlp->nlp_portname, &phba->lpfc_injerr_wwpn,
1096 sizeof(struct lpfc_name)) != 0))
1097 return 0;
1098 }
1099
1100 /* Setup a ptr to the protection data if the SCSI host provides it */
1101 if (sgpe) {
1102 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
1103 src += blockoff;
1104 lpfc_cmd = (struct lpfc_scsi_buf *)sc->host_scribble;
1105 }
1106
James Smartf9bb2da2011-10-10 21:34:11 -04001107 /* Should we change the Reference Tag */
1108 if (reftag) {
James Smartacd68592012-01-18 16:25:09 -05001109 if (phba->lpfc_injerr_wref_cnt) {
1110 switch (op) {
1111 case SCSI_PROT_WRITE_PASS:
James Smart9a6b09c2012-03-01 22:37:42 -05001112 if (src) {
1113 /*
1114 * For WRITE_PASS, force the error
1115 * to be sent on the wire. It should
1116 * be detected by the Target.
1117 * If blockoff != 0 error will be
1118 * inserted in middle of the IO.
1119 */
James Smartf9bb2da2011-10-10 21:34:11 -04001120
James Smartacd68592012-01-18 16:25:09 -05001121 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1122 "9076 BLKGRD: Injecting reftag error: "
1123 "write lba x%lx + x%x oldrefTag x%x\n",
1124 (unsigned long)lba, blockoff,
James Smart9a6b09c2012-03-01 22:37:42 -05001125 be32_to_cpu(src->ref_tag));
James Smartacd68592012-01-18 16:25:09 -05001126
1127 /*
James Smart9a6b09c2012-03-01 22:37:42 -05001128 * Save the old ref_tag so we can
1129 * restore it on completion.
James Smartacd68592012-01-18 16:25:09 -05001130 */
James Smart9a6b09c2012-03-01 22:37:42 -05001131 if (lpfc_cmd) {
1132 lpfc_cmd->prot_data_type =
1133 LPFC_INJERR_REFTAG;
1134 lpfc_cmd->prot_data_segment =
1135 src;
1136 lpfc_cmd->prot_data =
1137 src->ref_tag;
1138 }
1139 src->ref_tag = cpu_to_be32(0xDEADBEEF);
James Smartacd68592012-01-18 16:25:09 -05001140 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001141 if (phba->lpfc_injerr_wref_cnt == 0) {
1142 phba->lpfc_injerr_nportid = 0;
1143 phba->lpfc_injerr_lba =
1144 LPFC_INJERR_LBA_OFF;
1145 memset(&phba->lpfc_injerr_wwpn,
1146 0, sizeof(struct lpfc_name));
1147 }
James Smart9a6b09c2012-03-01 22:37:42 -05001148 rc = BG_ERR_TGT | BG_ERR_CHECK;
1149
James Smartacd68592012-01-18 16:25:09 -05001150 break;
1151 }
1152 /* Drop thru */
James Smart9a6b09c2012-03-01 22:37:42 -05001153 case SCSI_PROT_WRITE_INSERT:
1154 /*
1155 * For WRITE_INSERT, force the error
1156 * to be sent on the wire. It should be
1157 * detected by the Target.
1158 */
1159 /* DEADBEEF will be the reftag on the wire */
1160 *reftag = 0xDEADBEEF;
1161 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001162 if (phba->lpfc_injerr_wref_cnt == 0) {
1163 phba->lpfc_injerr_nportid = 0;
1164 phba->lpfc_injerr_lba =
1165 LPFC_INJERR_LBA_OFF;
1166 memset(&phba->lpfc_injerr_wwpn,
1167 0, sizeof(struct lpfc_name));
1168 }
James Smart9a6b09c2012-03-01 22:37:42 -05001169 rc = BG_ERR_TGT | BG_ERR_CHECK;
1170
1171 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1172 "9078 BLKGRD: Injecting reftag error: "
1173 "write lba x%lx\n", (unsigned long)lba);
1174 break;
James Smartacd68592012-01-18 16:25:09 -05001175 case SCSI_PROT_WRITE_STRIP:
1176 /*
1177 * For WRITE_STRIP and WRITE_PASS,
1178 * force the error on data
1179 * being copied from SLI-Host to SLI-Port.
1180 */
1181 *reftag = 0xDEADBEEF;
1182 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001183 if (phba->lpfc_injerr_wref_cnt == 0) {
1184 phba->lpfc_injerr_nportid = 0;
1185 phba->lpfc_injerr_lba =
1186 LPFC_INJERR_LBA_OFF;
1187 memset(&phba->lpfc_injerr_wwpn,
1188 0, sizeof(struct lpfc_name));
1189 }
James Smartacd68592012-01-18 16:25:09 -05001190 rc = BG_ERR_INIT;
1191
1192 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1193 "9077 BLKGRD: Injecting reftag error: "
1194 "write lba x%lx\n", (unsigned long)lba);
1195 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001196 }
James Smartacd68592012-01-18 16:25:09 -05001197 }
1198 if (phba->lpfc_injerr_rref_cnt) {
1199 switch (op) {
1200 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001201 case SCSI_PROT_READ_STRIP:
1202 case SCSI_PROT_READ_PASS:
1203 /*
1204 * For READ_STRIP and READ_PASS, force the
1205 * error on data being read off the wire. It
1206 * should force an IO error to the driver.
1207 */
James Smartf9bb2da2011-10-10 21:34:11 -04001208 *reftag = 0xDEADBEEF;
1209 phba->lpfc_injerr_rref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001210 if (phba->lpfc_injerr_rref_cnt == 0) {
1211 phba->lpfc_injerr_nportid = 0;
1212 phba->lpfc_injerr_lba =
1213 LPFC_INJERR_LBA_OFF;
1214 memset(&phba->lpfc_injerr_wwpn,
1215 0, sizeof(struct lpfc_name));
1216 }
James Smartacd68592012-01-18 16:25:09 -05001217 rc = BG_ERR_INIT;
James Smartf9bb2da2011-10-10 21:34:11 -04001218
1219 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smartacd68592012-01-18 16:25:09 -05001220 "9079 BLKGRD: Injecting reftag error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001221 "read lba x%lx\n", (unsigned long)lba);
James Smartacd68592012-01-18 16:25:09 -05001222 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001223 }
1224 }
1225 }
1226
1227 /* Should we change the Application Tag */
1228 if (apptag) {
James Smartacd68592012-01-18 16:25:09 -05001229 if (phba->lpfc_injerr_wapp_cnt) {
1230 switch (op) {
1231 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001232 if (src) {
James Smart9a6b09c2012-03-01 22:37:42 -05001233 /*
1234 * For WRITE_PASS, force the error
1235 * to be sent on the wire. It should
1236 * be detected by the Target.
1237 * If blockoff != 0 error will be
1238 * inserted in middle of the IO.
1239 */
1240
James Smartacd68592012-01-18 16:25:09 -05001241 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1242 "9080 BLKGRD: Injecting apptag error: "
1243 "write lba x%lx + x%x oldappTag x%x\n",
1244 (unsigned long)lba, blockoff,
James Smart9a6b09c2012-03-01 22:37:42 -05001245 be16_to_cpu(src->app_tag));
James Smartacd68592012-01-18 16:25:09 -05001246
1247 /*
James Smart9a6b09c2012-03-01 22:37:42 -05001248 * Save the old app_tag so we can
1249 * restore it on completion.
James Smartacd68592012-01-18 16:25:09 -05001250 */
James Smart9a6b09c2012-03-01 22:37:42 -05001251 if (lpfc_cmd) {
1252 lpfc_cmd->prot_data_type =
1253 LPFC_INJERR_APPTAG;
1254 lpfc_cmd->prot_data_segment =
1255 src;
1256 lpfc_cmd->prot_data =
1257 src->app_tag;
1258 }
1259 src->app_tag = cpu_to_be16(0xDEAD);
James Smartacd68592012-01-18 16:25:09 -05001260 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001261 if (phba->lpfc_injerr_wapp_cnt == 0) {
1262 phba->lpfc_injerr_nportid = 0;
1263 phba->lpfc_injerr_lba =
1264 LPFC_INJERR_LBA_OFF;
1265 memset(&phba->lpfc_injerr_wwpn,
1266 0, sizeof(struct lpfc_name));
1267 }
James Smart9a6b09c2012-03-01 22:37:42 -05001268 rc = BG_ERR_TGT | BG_ERR_CHECK;
James Smartacd68592012-01-18 16:25:09 -05001269 break;
1270 }
1271 /* Drop thru */
James Smart9a6b09c2012-03-01 22:37:42 -05001272 case SCSI_PROT_WRITE_INSERT:
1273 /*
1274 * For WRITE_INSERT, force the
1275 * error to be sent on the wire. It should be
1276 * detected by the Target.
1277 */
1278 /* DEAD will be the apptag on the wire */
1279 *apptag = 0xDEAD;
1280 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001281 if (phba->lpfc_injerr_wapp_cnt == 0) {
1282 phba->lpfc_injerr_nportid = 0;
1283 phba->lpfc_injerr_lba =
1284 LPFC_INJERR_LBA_OFF;
1285 memset(&phba->lpfc_injerr_wwpn,
1286 0, sizeof(struct lpfc_name));
1287 }
James Smart9a6b09c2012-03-01 22:37:42 -05001288 rc = BG_ERR_TGT | BG_ERR_CHECK;
1289
1290 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1291 "0813 BLKGRD: Injecting apptag error: "
1292 "write lba x%lx\n", (unsigned long)lba);
1293 break;
James Smartacd68592012-01-18 16:25:09 -05001294 case SCSI_PROT_WRITE_STRIP:
1295 /*
1296 * For WRITE_STRIP and WRITE_PASS,
1297 * force the error on data
1298 * being copied from SLI-Host to SLI-Port.
1299 */
1300 *apptag = 0xDEAD;
1301 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001302 if (phba->lpfc_injerr_wapp_cnt == 0) {
1303 phba->lpfc_injerr_nportid = 0;
1304 phba->lpfc_injerr_lba =
1305 LPFC_INJERR_LBA_OFF;
1306 memset(&phba->lpfc_injerr_wwpn,
1307 0, sizeof(struct lpfc_name));
1308 }
James Smartacd68592012-01-18 16:25:09 -05001309 rc = BG_ERR_INIT;
1310
1311 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1312 "0812 BLKGRD: Injecting apptag error: "
1313 "write lba x%lx\n", (unsigned long)lba);
1314 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001315 }
James Smartacd68592012-01-18 16:25:09 -05001316 }
1317 if (phba->lpfc_injerr_rapp_cnt) {
1318 switch (op) {
1319 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001320 case SCSI_PROT_READ_STRIP:
1321 case SCSI_PROT_READ_PASS:
1322 /*
1323 * For READ_STRIP and READ_PASS, force the
1324 * error on data being read off the wire. It
1325 * should force an IO error to the driver.
1326 */
James Smartf9bb2da2011-10-10 21:34:11 -04001327 *apptag = 0xDEAD;
1328 phba->lpfc_injerr_rapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001329 if (phba->lpfc_injerr_rapp_cnt == 0) {
1330 phba->lpfc_injerr_nportid = 0;
1331 phba->lpfc_injerr_lba =
1332 LPFC_INJERR_LBA_OFF;
1333 memset(&phba->lpfc_injerr_wwpn,
1334 0, sizeof(struct lpfc_name));
1335 }
James Smartacd68592012-01-18 16:25:09 -05001336 rc = BG_ERR_INIT;
James Smartf9bb2da2011-10-10 21:34:11 -04001337
1338 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smartacd68592012-01-18 16:25:09 -05001339 "0814 BLKGRD: Injecting apptag error: "
1340 "read lba x%lx\n", (unsigned long)lba);
1341 break;
1342 }
1343 }
1344 }
1345
1346
1347 /* Should we change the Guard Tag */
1348 if (new_guard) {
1349 if (phba->lpfc_injerr_wgrd_cnt) {
1350 switch (op) {
1351 case SCSI_PROT_WRITE_PASS:
James Smart9a6b09c2012-03-01 22:37:42 -05001352 rc = BG_ERR_CHECK;
James Smartacd68592012-01-18 16:25:09 -05001353 /* Drop thru */
James Smartacd68592012-01-18 16:25:09 -05001354
James Smartacd68592012-01-18 16:25:09 -05001355 case SCSI_PROT_WRITE_INSERT:
1356 /*
1357 * For WRITE_INSERT, force the
1358 * error to be sent on the wire. It should be
1359 * detected by the Target.
1360 */
1361 phba->lpfc_injerr_wgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001362 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1363 phba->lpfc_injerr_nportid = 0;
1364 phba->lpfc_injerr_lba =
1365 LPFC_INJERR_LBA_OFF;
1366 memset(&phba->lpfc_injerr_wwpn,
1367 0, sizeof(struct lpfc_name));
1368 }
James Smartacd68592012-01-18 16:25:09 -05001369
James Smart9a6b09c2012-03-01 22:37:42 -05001370 rc |= BG_ERR_TGT | BG_ERR_SWAP;
James Smartacd68592012-01-18 16:25:09 -05001371 /* Signals the caller to swap CRC->CSUM */
1372
1373 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1374 "0817 BLKGRD: Injecting guard error: "
1375 "write lba x%lx\n", (unsigned long)lba);
1376 break;
James Smart9a6b09c2012-03-01 22:37:42 -05001377 case SCSI_PROT_WRITE_STRIP:
1378 /*
1379 * For WRITE_STRIP and WRITE_PASS,
1380 * force the error on data
1381 * being copied from SLI-Host to SLI-Port.
1382 */
1383 phba->lpfc_injerr_wgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001384 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1385 phba->lpfc_injerr_nportid = 0;
1386 phba->lpfc_injerr_lba =
1387 LPFC_INJERR_LBA_OFF;
1388 memset(&phba->lpfc_injerr_wwpn,
1389 0, sizeof(struct lpfc_name));
1390 }
James Smart9a6b09c2012-03-01 22:37:42 -05001391
1392 rc = BG_ERR_INIT | BG_ERR_SWAP;
1393 /* Signals the caller to swap CRC->CSUM */
1394
1395 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1396 "0816 BLKGRD: Injecting guard error: "
1397 "write lba x%lx\n", (unsigned long)lba);
1398 break;
James Smartacd68592012-01-18 16:25:09 -05001399 }
1400 }
1401 if (phba->lpfc_injerr_rgrd_cnt) {
1402 switch (op) {
1403 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001404 case SCSI_PROT_READ_STRIP:
1405 case SCSI_PROT_READ_PASS:
1406 /*
1407 * For READ_STRIP and READ_PASS, force the
1408 * error on data being read off the wire. It
1409 * should force an IO error to the driver.
1410 */
James Smartacd68592012-01-18 16:25:09 -05001411 phba->lpfc_injerr_rgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001412 if (phba->lpfc_injerr_rgrd_cnt == 0) {
1413 phba->lpfc_injerr_nportid = 0;
1414 phba->lpfc_injerr_lba =
1415 LPFC_INJERR_LBA_OFF;
1416 memset(&phba->lpfc_injerr_wwpn,
1417 0, sizeof(struct lpfc_name));
1418 }
James Smartacd68592012-01-18 16:25:09 -05001419
James Smart9a6b09c2012-03-01 22:37:42 -05001420 rc = BG_ERR_INIT | BG_ERR_SWAP;
James Smartacd68592012-01-18 16:25:09 -05001421 /* Signals the caller to swap CRC->CSUM */
1422
1423 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1424 "0818 BLKGRD: Injecting guard error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001425 "read lba x%lx\n", (unsigned long)lba);
1426 }
1427 }
1428 }
1429
James Smartf9bb2da2011-10-10 21:34:11 -04001430 return rc;
1431}
1432#endif
1433
James Smartacd68592012-01-18 16:25:09 -05001434/**
1435 * lpfc_sc_to_bg_opcodes - Determine the BlockGuard opcodes to be used with
1436 * the specified SCSI command.
1437 * @phba: The Hba for which this call is being executed.
James Smart6c8eea52010-04-06 14:49:53 -04001438 * @sc: The SCSI command to examine
1439 * @txopt: (out) BlockGuard operation for transmitted data
1440 * @rxopt: (out) BlockGuard operation for received data
1441 *
1442 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1443 *
James Smartacd68592012-01-18 16:25:09 -05001444 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001445static int
James Smart6c8eea52010-04-06 14:49:53 -04001446lpfc_sc_to_bg_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1447 uint8_t *txop, uint8_t *rxop)
James Smarte2a0a9d2008-12-04 22:40:02 -05001448{
James Smart6c8eea52010-04-06 14:49:53 -04001449 uint8_t ret = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05001450
James Smart9c6aa9d2013-05-31 17:03:39 -04001451 if (lpfc_cmd_guard_csum(sc)) {
James Smarte2a0a9d2008-12-04 22:40:02 -05001452 switch (scsi_get_prot_op(sc)) {
1453 case SCSI_PROT_READ_INSERT:
1454 case SCSI_PROT_WRITE_STRIP:
James Smart6c8eea52010-04-06 14:49:53 -04001455 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001456 *txop = BG_OP_IN_CSUM_OUT_NODIF;
James Smarte2a0a9d2008-12-04 22:40:02 -05001457 break;
1458
1459 case SCSI_PROT_READ_STRIP:
1460 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001461 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001462 *txop = BG_OP_IN_NODIF_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001463 break;
1464
Martin K. Petersenc6af4042009-09-18 17:32:59 -04001465 case SCSI_PROT_READ_PASS:
1466 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001467 *rxop = BG_OP_IN_CRC_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001468 *txop = BG_OP_IN_CSUM_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001469 break;
1470
James Smarte2a0a9d2008-12-04 22:40:02 -05001471 case SCSI_PROT_NORMAL:
1472 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001473 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001474 "9063 BLKGRD: Bad op/guard:%d/IP combination\n",
1475 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001476 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001477 break;
1478
1479 }
James Smart7c56b9f2011-07-22 18:36:25 -04001480 } else {
James Smarte2a0a9d2008-12-04 22:40:02 -05001481 switch (scsi_get_prot_op(sc)) {
1482 case SCSI_PROT_READ_STRIP:
1483 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001484 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001485 *txop = BG_OP_IN_NODIF_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001486 break;
1487
1488 case SCSI_PROT_READ_PASS:
1489 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001490 *rxop = BG_OP_IN_CRC_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001491 *txop = BG_OP_IN_CRC_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001492 break;
1493
James Smarte2a0a9d2008-12-04 22:40:02 -05001494 case SCSI_PROT_READ_INSERT:
1495 case SCSI_PROT_WRITE_STRIP:
James Smart7c56b9f2011-07-22 18:36:25 -04001496 *rxop = BG_OP_IN_NODIF_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001497 *txop = BG_OP_IN_CRC_OUT_NODIF;
James Smart7c56b9f2011-07-22 18:36:25 -04001498 break;
1499
James Smarte2a0a9d2008-12-04 22:40:02 -05001500 case SCSI_PROT_NORMAL:
1501 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001502 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001503 "9075 BLKGRD: Bad op/guard:%d/CRC combination\n",
1504 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001505 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001506 break;
1507 }
James Smarte2a0a9d2008-12-04 22:40:02 -05001508 }
1509
James Smart6c8eea52010-04-06 14:49:53 -04001510 return ret;
James Smarte2a0a9d2008-12-04 22:40:02 -05001511}
1512
James Smartacd68592012-01-18 16:25:09 -05001513#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1514/**
1515 * lpfc_bg_err_opcodes - reDetermine the BlockGuard opcodes to be used with
1516 * the specified SCSI command in order to force a guard tag error.
1517 * @phba: The Hba for which this call is being executed.
1518 * @sc: The SCSI command to examine
1519 * @txopt: (out) BlockGuard operation for transmitted data
1520 * @rxopt: (out) BlockGuard operation for received data
1521 *
1522 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1523 *
1524 **/
1525static int
1526lpfc_bg_err_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1527 uint8_t *txop, uint8_t *rxop)
1528{
James Smartacd68592012-01-18 16:25:09 -05001529 uint8_t ret = 0;
1530
James Smart9c6aa9d2013-05-31 17:03:39 -04001531 if (lpfc_cmd_guard_csum(sc)) {
James Smartacd68592012-01-18 16:25:09 -05001532 switch (scsi_get_prot_op(sc)) {
1533 case SCSI_PROT_READ_INSERT:
1534 case SCSI_PROT_WRITE_STRIP:
James Smartacd68592012-01-18 16:25:09 -05001535 *rxop = BG_OP_IN_NODIF_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001536 *txop = BG_OP_IN_CRC_OUT_NODIF;
James Smartacd68592012-01-18 16:25:09 -05001537 break;
1538
1539 case SCSI_PROT_READ_STRIP:
1540 case SCSI_PROT_WRITE_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001541 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001542 *txop = BG_OP_IN_NODIF_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001543 break;
1544
1545 case SCSI_PROT_READ_PASS:
1546 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001547 *rxop = BG_OP_IN_CSUM_OUT_CRC;
James Smart9a6b09c2012-03-01 22:37:42 -05001548 *txop = BG_OP_IN_CRC_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001549 break;
1550
1551 case SCSI_PROT_NORMAL:
1552 default:
1553 break;
1554
1555 }
1556 } else {
1557 switch (scsi_get_prot_op(sc)) {
1558 case SCSI_PROT_READ_STRIP:
1559 case SCSI_PROT_WRITE_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001560 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001561 *txop = BG_OP_IN_NODIF_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001562 break;
1563
1564 case SCSI_PROT_READ_PASS:
1565 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001566 *rxop = BG_OP_IN_CSUM_OUT_CSUM;
James Smart9a6b09c2012-03-01 22:37:42 -05001567 *txop = BG_OP_IN_CSUM_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001568 break;
1569
1570 case SCSI_PROT_READ_INSERT:
1571 case SCSI_PROT_WRITE_STRIP:
James Smartacd68592012-01-18 16:25:09 -05001572 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001573 *txop = BG_OP_IN_CSUM_OUT_NODIF;
James Smartacd68592012-01-18 16:25:09 -05001574 break;
1575
1576 case SCSI_PROT_NORMAL:
1577 default:
1578 break;
1579 }
1580 }
1581
1582 return ret;
1583}
1584#endif
1585
1586/**
1587 * lpfc_bg_setup_bpl - Setup BlockGuard BPL with no protection data
1588 * @phba: The Hba for which this call is being executed.
1589 * @sc: pointer to scsi command we're working on
1590 * @bpl: pointer to buffer list for protection groups
1591 * @datacnt: number of segments of data that have been dma mapped
1592 *
1593 * This function sets up BPL buffer list for protection groups of
James Smarte2a0a9d2008-12-04 22:40:02 -05001594 * type LPFC_PG_TYPE_NO_DIF
1595 *
1596 * This is usually used when the HBA is instructed to generate
1597 * DIFs and insert them into data stream (or strip DIF from
1598 * incoming data stream)
1599 *
1600 * The buffer list consists of just one protection group described
1601 * below:
1602 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001603 * start of prot group --> | PDE_5 |
1604 * +-------------------------+
1605 * | PDE_6 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001606 * +-------------------------+
1607 * | Data BDE |
1608 * +-------------------------+
1609 * |more Data BDE's ... (opt)|
1610 * +-------------------------+
1611 *
James Smarte2a0a9d2008-12-04 22:40:02 -05001612 *
1613 * Note: Data s/g buffers have been dma mapped
James Smartacd68592012-01-18 16:25:09 -05001614 *
1615 * Returns the number of BDEs added to the BPL.
1616 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001617static int
1618lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1619 struct ulp_bde64 *bpl, int datasegcnt)
1620{
1621 struct scatterlist *sgde = NULL; /* s/g data entry */
James Smart6c8eea52010-04-06 14:49:53 -04001622 struct lpfc_pde5 *pde5 = NULL;
1623 struct lpfc_pde6 *pde6 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05001624 dma_addr_t physaddr;
James Smart6c8eea52010-04-06 14:49:53 -04001625 int i = 0, num_bde = 0, status;
James Smarte2a0a9d2008-12-04 22:40:02 -05001626 int datadir = sc->sc_data_direction;
James Smart0829a192012-05-09 21:18:12 -04001627#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001628 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04001629#endif
James Smartacd68592012-01-18 16:25:09 -05001630 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001631 uint32_t reftag;
James Smart6c8eea52010-04-06 14:49:53 -04001632 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05001633
James Smart6c8eea52010-04-06 14:49:53 -04001634 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1635 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05001636 goto out;
1637
James Smart6c8eea52010-04-06 14:49:53 -04001638 /* extract some info from the scsi command for pde*/
James Smartacd68592012-01-18 16:25:09 -05001639 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
James Smarte2a0a9d2008-12-04 22:40:02 -05001640
James Smartf9bb2da2011-10-10 21:34:11 -04001641#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05001642 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05001643 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05001644 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05001645 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05001646 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05001647 checking = 0;
1648 }
James Smartf9bb2da2011-10-10 21:34:11 -04001649#endif
1650
James Smart6c8eea52010-04-06 14:49:53 -04001651 /* setup PDE5 with what we have */
1652 pde5 = (struct lpfc_pde5 *) bpl;
1653 memset(pde5, 0, sizeof(struct lpfc_pde5));
1654 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05001655
James Smartbc739052010-08-04 16:11:18 -04001656 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04001657 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04001658 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04001659
James Smart6c8eea52010-04-06 14:49:53 -04001660 /* advance bpl and increment bde count */
1661 num_bde++;
1662 bpl++;
1663 pde6 = (struct lpfc_pde6 *) bpl;
1664
1665 /* setup PDE6 with the rest of the info */
1666 memset(pde6, 0, sizeof(struct lpfc_pde6));
1667 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
1668 bf_set(pde6_optx, pde6, txop);
1669 bf_set(pde6_oprx, pde6, rxop);
James Smarta6887e22013-04-17 20:18:07 -04001670
1671 /*
1672 * We only need to check the data on READs, for WRITEs
1673 * protection data is automatically generated, not checked.
1674 */
James Smart6c8eea52010-04-06 14:49:53 -04001675 if (datadir == DMA_FROM_DEVICE) {
James Smart9c6aa9d2013-05-31 17:03:39 -04001676 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
James Smarta6887e22013-04-17 20:18:07 -04001677 bf_set(pde6_ce, pde6, checking);
1678 else
1679 bf_set(pde6_ce, pde6, 0);
1680
James Smart9c6aa9d2013-05-31 17:03:39 -04001681 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04001682 bf_set(pde6_re, pde6, checking);
1683 else
1684 bf_set(pde6_re, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04001685 }
1686 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04001687 bf_set(pde6_ae, pde6, 0);
1688 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04001689
James Smartbc739052010-08-04 16:11:18 -04001690 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04001691 pde6->word0 = cpu_to_le32(pde6->word0);
1692 pde6->word1 = cpu_to_le32(pde6->word1);
1693 pde6->word2 = cpu_to_le32(pde6->word2);
1694
James Smart6c8eea52010-04-06 14:49:53 -04001695 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05001696 num_bde++;
1697 bpl++;
1698
1699 /* assumption: caller has already run dma_map_sg on command data */
1700 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
1701 physaddr = sg_dma_address(sgde);
1702 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
1703 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1704 bpl->tus.f.bdeSize = sg_dma_len(sgde);
1705 if (datadir == DMA_TO_DEVICE)
1706 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1707 else
1708 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1709 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1710 bpl++;
1711 num_bde++;
1712 }
1713
1714out:
1715 return num_bde;
1716}
1717
James Smartacd68592012-01-18 16:25:09 -05001718/**
1719 * lpfc_bg_setup_bpl_prot - Setup BlockGuard BPL with protection data
1720 * @phba: The Hba for which this call is being executed.
1721 * @sc: pointer to scsi command we're working on
1722 * @bpl: pointer to buffer list for protection groups
1723 * @datacnt: number of segments of data that have been dma mapped
1724 * @protcnt: number of segment of protection data that have been dma mapped
1725 *
1726 * This function sets up BPL buffer list for protection groups of
1727 * type LPFC_PG_TYPE_DIF
James Smarte2a0a9d2008-12-04 22:40:02 -05001728 *
1729 * This is usually used when DIFs are in their own buffers,
1730 * separate from the data. The HBA can then by instructed
1731 * to place the DIFs in the outgoing stream. For read operations,
1732 * The HBA could extract the DIFs and place it in DIF buffers.
1733 *
1734 * The buffer list for this type consists of one or more of the
1735 * protection groups described below:
1736 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001737 * start of first prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001738 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001739 * | PDE_6 |
1740 * +-------------------------+
1741 * | PDE_7 (Prot BDE) |
James Smarte2a0a9d2008-12-04 22:40:02 -05001742 * +-------------------------+
1743 * | Data BDE |
1744 * +-------------------------+
1745 * |more Data BDE's ... (opt)|
1746 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001747 * start of new prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001748 * +-------------------------+
1749 * | ... |
1750 * +-------------------------+
1751 *
James Smarte2a0a9d2008-12-04 22:40:02 -05001752 * Note: It is assumed that both data and protection s/g buffers have been
1753 * mapped for DMA
James Smartacd68592012-01-18 16:25:09 -05001754 *
1755 * Returns the number of BDEs added to the BPL.
1756 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001757static int
1758lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1759 struct ulp_bde64 *bpl, int datacnt, int protcnt)
1760{
1761 struct scatterlist *sgde = NULL; /* s/g data entry */
1762 struct scatterlist *sgpe = NULL; /* s/g prot entry */
James Smart6c8eea52010-04-06 14:49:53 -04001763 struct lpfc_pde5 *pde5 = NULL;
1764 struct lpfc_pde6 *pde6 = NULL;
James Smart7f860592011-03-11 16:05:52 -05001765 struct lpfc_pde7 *pde7 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05001766 dma_addr_t dataphysaddr, protphysaddr;
1767 unsigned short curr_data = 0, curr_prot = 0;
James Smart7f860592011-03-11 16:05:52 -05001768 unsigned int split_offset;
1769 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
James Smarte2a0a9d2008-12-04 22:40:02 -05001770 unsigned int protgrp_blks, protgrp_bytes;
1771 unsigned int remainder, subtotal;
James Smart6c8eea52010-04-06 14:49:53 -04001772 int status;
James Smarte2a0a9d2008-12-04 22:40:02 -05001773 int datadir = sc->sc_data_direction;
1774 unsigned char pgdone = 0, alldone = 0;
1775 unsigned blksize;
James Smart0829a192012-05-09 21:18:12 -04001776#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001777 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04001778#endif
James Smartacd68592012-01-18 16:25:09 -05001779 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001780 uint32_t reftag;
James Smart6c8eea52010-04-06 14:49:53 -04001781 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05001782 int num_bde = 0;
1783
1784 sgpe = scsi_prot_sglist(sc);
1785 sgde = scsi_sglist(sc);
1786
1787 if (!sgpe || !sgde) {
1788 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1789 "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
1790 sgpe, sgde);
1791 return 0;
1792 }
1793
James Smart6c8eea52010-04-06 14:49:53 -04001794 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1795 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05001796 goto out;
1797
James Smart6c8eea52010-04-06 14:49:53 -04001798 /* extract some info from the scsi command */
James Smarte2a0a9d2008-12-04 22:40:02 -05001799 blksize = lpfc_cmd_blksize(sc);
James Smartacd68592012-01-18 16:25:09 -05001800 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
James Smarte2a0a9d2008-12-04 22:40:02 -05001801
James Smartf9bb2da2011-10-10 21:34:11 -04001802#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05001803 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05001804 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05001805 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05001806 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05001807 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05001808 checking = 0;
1809 }
James Smartf9bb2da2011-10-10 21:34:11 -04001810#endif
1811
James Smarte2a0a9d2008-12-04 22:40:02 -05001812 split_offset = 0;
1813 do {
James Smart96f70772013-04-17 20:16:15 -04001814 /* Check to see if we ran out of space */
1815 if (num_bde >= (phba->cfg_total_seg_cnt - 2))
1816 return num_bde + 3;
1817
James Smart6c8eea52010-04-06 14:49:53 -04001818 /* setup PDE5 with what we have */
1819 pde5 = (struct lpfc_pde5 *) bpl;
1820 memset(pde5, 0, sizeof(struct lpfc_pde5));
1821 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05001822
James Smartbc739052010-08-04 16:11:18 -04001823 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04001824 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04001825 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04001826
James Smart6c8eea52010-04-06 14:49:53 -04001827 /* advance bpl and increment bde count */
1828 num_bde++;
1829 bpl++;
1830 pde6 = (struct lpfc_pde6 *) bpl;
James Smarte2a0a9d2008-12-04 22:40:02 -05001831
James Smart6c8eea52010-04-06 14:49:53 -04001832 /* setup PDE6 with the rest of the info */
1833 memset(pde6, 0, sizeof(struct lpfc_pde6));
1834 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
1835 bf_set(pde6_optx, pde6, txop);
1836 bf_set(pde6_oprx, pde6, rxop);
James Smarta6887e22013-04-17 20:18:07 -04001837
James Smart9c6aa9d2013-05-31 17:03:39 -04001838 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
James Smarta6887e22013-04-17 20:18:07 -04001839 bf_set(pde6_ce, pde6, checking);
1840 else
1841 bf_set(pde6_ce, pde6, 0);
1842
James Smart9c6aa9d2013-05-31 17:03:39 -04001843 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04001844 bf_set(pde6_re, pde6, checking);
1845 else
1846 bf_set(pde6_re, pde6, 0);
1847
James Smart6c8eea52010-04-06 14:49:53 -04001848 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04001849 bf_set(pde6_ae, pde6, 0);
1850 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04001851
James Smartbc739052010-08-04 16:11:18 -04001852 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04001853 pde6->word0 = cpu_to_le32(pde6->word0);
1854 pde6->word1 = cpu_to_le32(pde6->word1);
1855 pde6->word2 = cpu_to_le32(pde6->word2);
1856
James Smart6c8eea52010-04-06 14:49:53 -04001857 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05001858 num_bde++;
1859 bpl++;
1860
1861 /* setup the first BDE that points to protection buffer */
James Smart7f860592011-03-11 16:05:52 -05001862 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
1863 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
James Smarte2a0a9d2008-12-04 22:40:02 -05001864
James Smarte2a0a9d2008-12-04 22:40:02 -05001865 /* must be integer multiple of the DIF block length */
1866 BUG_ON(protgroup_len % 8);
1867
James Smart7f860592011-03-11 16:05:52 -05001868 pde7 = (struct lpfc_pde7 *) bpl;
1869 memset(pde7, 0, sizeof(struct lpfc_pde7));
1870 bf_set(pde7_type, pde7, LPFC_PDE7_DESCRIPTOR);
1871
James Smart7c56b9f2011-07-22 18:36:25 -04001872 pde7->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
1873 pde7->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
James Smart7f860592011-03-11 16:05:52 -05001874
James Smarte2a0a9d2008-12-04 22:40:02 -05001875 protgrp_blks = protgroup_len / 8;
1876 protgrp_bytes = protgrp_blks * blksize;
1877
James Smart7f860592011-03-11 16:05:52 -05001878 /* check if this pde is crossing the 4K boundary; if so split */
1879 if ((pde7->addrLow & 0xfff) + protgroup_len > 0x1000) {
1880 protgroup_remainder = 0x1000 - (pde7->addrLow & 0xfff);
1881 protgroup_offset += protgroup_remainder;
1882 protgrp_blks = protgroup_remainder / 8;
James Smart7c56b9f2011-07-22 18:36:25 -04001883 protgrp_bytes = protgrp_blks * blksize;
James Smart7f860592011-03-11 16:05:52 -05001884 } else {
1885 protgroup_offset = 0;
1886 curr_prot++;
1887 }
James Smarte2a0a9d2008-12-04 22:40:02 -05001888
James Smarte2a0a9d2008-12-04 22:40:02 -05001889 num_bde++;
1890
1891 /* setup BDE's for data blocks associated with DIF data */
1892 pgdone = 0;
1893 subtotal = 0; /* total bytes processed for current prot grp */
1894 while (!pgdone) {
James Smart96f70772013-04-17 20:16:15 -04001895 /* Check to see if we ran out of space */
1896 if (num_bde >= phba->cfg_total_seg_cnt)
1897 return num_bde + 1;
1898
James Smarte2a0a9d2008-12-04 22:40:02 -05001899 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -04001900 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1901 "9065 BLKGRD:%s Invalid data segment\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001902 __func__);
1903 return 0;
1904 }
1905 bpl++;
1906 dataphysaddr = sg_dma_address(sgde) + split_offset;
1907 bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
1908 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
1909
1910 remainder = sg_dma_len(sgde) - split_offset;
1911
1912 if ((subtotal + remainder) <= protgrp_bytes) {
1913 /* we can use this whole buffer */
1914 bpl->tus.f.bdeSize = remainder;
1915 split_offset = 0;
1916
1917 if ((subtotal + remainder) == protgrp_bytes)
1918 pgdone = 1;
1919 } else {
1920 /* must split this buffer with next prot grp */
1921 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
1922 split_offset += bpl->tus.f.bdeSize;
1923 }
1924
1925 subtotal += bpl->tus.f.bdeSize;
1926
1927 if (datadir == DMA_TO_DEVICE)
1928 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1929 else
1930 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1931 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1932
1933 num_bde++;
1934 curr_data++;
1935
1936 if (split_offset)
1937 break;
1938
1939 /* Move to the next s/g segment if possible */
1940 sgde = sg_next(sgde);
James Smart6c8eea52010-04-06 14:49:53 -04001941
James Smarte2a0a9d2008-12-04 22:40:02 -05001942 }
1943
James Smart7f860592011-03-11 16:05:52 -05001944 if (protgroup_offset) {
1945 /* update the reference tag */
1946 reftag += protgrp_blks;
1947 bpl++;
1948 continue;
1949 }
1950
James Smarte2a0a9d2008-12-04 22:40:02 -05001951 /* are we done ? */
1952 if (curr_prot == protcnt) {
1953 alldone = 1;
1954 } else if (curr_prot < protcnt) {
1955 /* advance to next prot buffer */
1956 sgpe = sg_next(sgpe);
1957 bpl++;
1958
1959 /* update the reference tag */
1960 reftag += protgrp_blks;
1961 } else {
1962 /* if we're here, we have a bug */
James Smart6a9c52c2009-10-02 15:16:51 -04001963 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1964 "9054 BLKGRD: bug in %s\n", __func__);
James Smarte2a0a9d2008-12-04 22:40:02 -05001965 }
1966
1967 } while (!alldone);
James Smarte2a0a9d2008-12-04 22:40:02 -05001968out:
1969
James Smarte2a0a9d2008-12-04 22:40:02 -05001970 return num_bde;
1971}
James Smart7f860592011-03-11 16:05:52 -05001972
James Smartacd68592012-01-18 16:25:09 -05001973/**
1974 * lpfc_bg_setup_sgl - Setup BlockGuard SGL with no protection data
1975 * @phba: The Hba for which this call is being executed.
1976 * @sc: pointer to scsi command we're working on
1977 * @sgl: pointer to buffer list for protection groups
1978 * @datacnt: number of segments of data that have been dma mapped
1979 *
1980 * This function sets up SGL buffer list for protection groups of
1981 * type LPFC_PG_TYPE_NO_DIF
1982 *
1983 * This is usually used when the HBA is instructed to generate
1984 * DIFs and insert them into data stream (or strip DIF from
1985 * incoming data stream)
1986 *
1987 * The buffer list consists of just one protection group described
1988 * below:
1989 * +-------------------------+
1990 * start of prot group --> | DI_SEED |
1991 * +-------------------------+
1992 * | Data SGE |
1993 * +-------------------------+
1994 * |more Data SGE's ... (opt)|
1995 * +-------------------------+
1996 *
1997 *
1998 * Note: Data s/g buffers have been dma mapped
1999 *
2000 * Returns the number of SGEs added to the SGL.
2001 **/
2002static int
2003lpfc_bg_setup_sgl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2004 struct sli4_sge *sgl, int datasegcnt)
2005{
2006 struct scatterlist *sgde = NULL; /* s/g data entry */
2007 struct sli4_sge_diseed *diseed = NULL;
2008 dma_addr_t physaddr;
2009 int i = 0, num_sge = 0, status;
James Smartacd68592012-01-18 16:25:09 -05002010 uint32_t reftag;
James Smartacd68592012-01-18 16:25:09 -05002011 uint8_t txop, rxop;
James Smart0829a192012-05-09 21:18:12 -04002012#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002013 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002014#endif
James Smartacd68592012-01-18 16:25:09 -05002015 uint32_t checking = 1;
2016 uint32_t dma_len;
2017 uint32_t dma_offset = 0;
2018
2019 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2020 if (status)
2021 goto out;
2022
2023 /* extract some info from the scsi command for pde*/
James Smartacd68592012-01-18 16:25:09 -05002024 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2025
2026#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002027 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002028 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002029 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002030 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002031 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002032 checking = 0;
2033 }
2034#endif
2035
2036 /* setup DISEED with what we have */
2037 diseed = (struct sli4_sge_diseed *) sgl;
2038 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2039 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2040
2041 /* Endianness conversion if necessary */
2042 diseed->ref_tag = cpu_to_le32(reftag);
2043 diseed->ref_tag_tran = diseed->ref_tag;
2044
James Smarta6887e22013-04-17 20:18:07 -04002045 /*
2046 * We only need to check the data on READs, for WRITEs
2047 * protection data is automatically generated, not checked.
2048 */
2049 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
James Smart9c6aa9d2013-05-31 17:03:39 -04002050 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
James Smarta6887e22013-04-17 20:18:07 -04002051 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2052 else
2053 bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2054
James Smart9c6aa9d2013-05-31 17:03:39 -04002055 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04002056 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2057 else
2058 bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2059 }
2060
James Smartacd68592012-01-18 16:25:09 -05002061 /* setup DISEED with the rest of the info */
2062 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2063 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002064
James Smartacd68592012-01-18 16:25:09 -05002065 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2066 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2067
2068 /* Endianness conversion if necessary for DISEED */
2069 diseed->word2 = cpu_to_le32(diseed->word2);
2070 diseed->word3 = cpu_to_le32(diseed->word3);
2071
2072 /* advance bpl and increment sge count */
2073 num_sge++;
2074 sgl++;
2075
2076 /* assumption: caller has already run dma_map_sg on command data */
2077 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
2078 physaddr = sg_dma_address(sgde);
2079 dma_len = sg_dma_len(sgde);
2080 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
2081 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
2082 if ((i + 1) == datasegcnt)
2083 bf_set(lpfc_sli4_sge_last, sgl, 1);
2084 else
2085 bf_set(lpfc_sli4_sge_last, sgl, 0);
2086 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2087 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2088
2089 sgl->sge_len = cpu_to_le32(dma_len);
2090 dma_offset += dma_len;
2091
2092 sgl++;
2093 num_sge++;
2094 }
2095
2096out:
2097 return num_sge;
2098}
2099
2100/**
2101 * lpfc_bg_setup_sgl_prot - Setup BlockGuard SGL with protection data
2102 * @phba: The Hba for which this call is being executed.
2103 * @sc: pointer to scsi command we're working on
2104 * @sgl: pointer to buffer list for protection groups
2105 * @datacnt: number of segments of data that have been dma mapped
2106 * @protcnt: number of segment of protection data that have been dma mapped
2107 *
2108 * This function sets up SGL buffer list for protection groups of
2109 * type LPFC_PG_TYPE_DIF
2110 *
2111 * This is usually used when DIFs are in their own buffers,
2112 * separate from the data. The HBA can then by instructed
2113 * to place the DIFs in the outgoing stream. For read operations,
2114 * The HBA could extract the DIFs and place it in DIF buffers.
2115 *
2116 * The buffer list for this type consists of one or more of the
2117 * protection groups described below:
2118 * +-------------------------+
2119 * start of first prot group --> | DISEED |
2120 * +-------------------------+
2121 * | DIF (Prot SGE) |
2122 * +-------------------------+
2123 * | Data SGE |
2124 * +-------------------------+
2125 * |more Data SGE's ... (opt)|
2126 * +-------------------------+
2127 * start of new prot group --> | DISEED |
2128 * +-------------------------+
2129 * | ... |
2130 * +-------------------------+
2131 *
2132 * Note: It is assumed that both data and protection s/g buffers have been
2133 * mapped for DMA
2134 *
2135 * Returns the number of SGEs added to the SGL.
2136 **/
2137static int
2138lpfc_bg_setup_sgl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2139 struct sli4_sge *sgl, int datacnt, int protcnt)
2140{
2141 struct scatterlist *sgde = NULL; /* s/g data entry */
2142 struct scatterlist *sgpe = NULL; /* s/g prot entry */
2143 struct sli4_sge_diseed *diseed = NULL;
2144 dma_addr_t dataphysaddr, protphysaddr;
2145 unsigned short curr_data = 0, curr_prot = 0;
2146 unsigned int split_offset;
2147 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
2148 unsigned int protgrp_blks, protgrp_bytes;
2149 unsigned int remainder, subtotal;
2150 int status;
2151 unsigned char pgdone = 0, alldone = 0;
2152 unsigned blksize;
2153 uint32_t reftag;
2154 uint8_t txop, rxop;
2155 uint32_t dma_len;
James Smart0829a192012-05-09 21:18:12 -04002156#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002157 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002158#endif
James Smartacd68592012-01-18 16:25:09 -05002159 uint32_t checking = 1;
2160 uint32_t dma_offset = 0;
2161 int num_sge = 0;
2162
2163 sgpe = scsi_prot_sglist(sc);
2164 sgde = scsi_sglist(sc);
2165
2166 if (!sgpe || !sgde) {
2167 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2168 "9082 Invalid s/g entry: data=0x%p prot=0x%p\n",
2169 sgpe, sgde);
2170 return 0;
2171 }
2172
2173 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2174 if (status)
2175 goto out;
2176
2177 /* extract some info from the scsi command */
2178 blksize = lpfc_cmd_blksize(sc);
2179 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2180
2181#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002182 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002183 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002184 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002185 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002186 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002187 checking = 0;
2188 }
2189#endif
2190
2191 split_offset = 0;
2192 do {
James Smart96f70772013-04-17 20:16:15 -04002193 /* Check to see if we ran out of space */
2194 if (num_sge >= (phba->cfg_total_seg_cnt - 2))
2195 return num_sge + 3;
2196
James Smartacd68592012-01-18 16:25:09 -05002197 /* setup DISEED with what we have */
2198 diseed = (struct sli4_sge_diseed *) sgl;
2199 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2200 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2201
2202 /* Endianness conversion if necessary */
2203 diseed->ref_tag = cpu_to_le32(reftag);
2204 diseed->ref_tag_tran = diseed->ref_tag;
2205
James Smart9c6aa9d2013-05-31 17:03:39 -04002206 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD)) {
James Smarta6887e22013-04-17 20:18:07 -04002207 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2208
2209 } else {
2210 bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2211 /*
2212 * When in this mode, the hardware will replace
2213 * the guard tag from the host with a
2214 * newly generated good CRC for the wire.
2215 * Switch to raw mode here to avoid this
2216 * behavior. What the host sends gets put on the wire.
2217 */
2218 if (txop == BG_OP_IN_CRC_OUT_CRC) {
2219 txop = BG_OP_RAW_MODE;
2220 rxop = BG_OP_RAW_MODE;
2221 }
2222 }
2223
2224
James Smart9c6aa9d2013-05-31 17:03:39 -04002225 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04002226 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2227 else
2228 bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2229
James Smartacd68592012-01-18 16:25:09 -05002230 /* setup DISEED with the rest of the info */
2231 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2232 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002233
James Smartacd68592012-01-18 16:25:09 -05002234 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2235 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2236
2237 /* Endianness conversion if necessary for DISEED */
2238 diseed->word2 = cpu_to_le32(diseed->word2);
2239 diseed->word3 = cpu_to_le32(diseed->word3);
2240
2241 /* advance sgl and increment bde count */
2242 num_sge++;
2243 sgl++;
2244
2245 /* setup the first BDE that points to protection buffer */
2246 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2247 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
2248
2249 /* must be integer multiple of the DIF block length */
2250 BUG_ON(protgroup_len % 8);
2251
2252 /* Now setup DIF SGE */
2253 sgl->word2 = 0;
2254 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DIF);
2255 sgl->addr_hi = le32_to_cpu(putPaddrHigh(protphysaddr));
2256 sgl->addr_lo = le32_to_cpu(putPaddrLow(protphysaddr));
2257 sgl->word2 = cpu_to_le32(sgl->word2);
2258
2259 protgrp_blks = protgroup_len / 8;
2260 protgrp_bytes = protgrp_blks * blksize;
2261
2262 /* check if DIF SGE is crossing the 4K boundary; if so split */
2263 if ((sgl->addr_lo & 0xfff) + protgroup_len > 0x1000) {
2264 protgroup_remainder = 0x1000 - (sgl->addr_lo & 0xfff);
2265 protgroup_offset += protgroup_remainder;
2266 protgrp_blks = protgroup_remainder / 8;
2267 protgrp_bytes = protgrp_blks * blksize;
2268 } else {
2269 protgroup_offset = 0;
2270 curr_prot++;
2271 }
2272
2273 num_sge++;
2274
2275 /* setup SGE's for data blocks associated with DIF data */
2276 pgdone = 0;
2277 subtotal = 0; /* total bytes processed for current prot grp */
2278 while (!pgdone) {
James Smart96f70772013-04-17 20:16:15 -04002279 /* Check to see if we ran out of space */
2280 if (num_sge >= phba->cfg_total_seg_cnt)
2281 return num_sge + 1;
2282
James Smartacd68592012-01-18 16:25:09 -05002283 if (!sgde) {
2284 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2285 "9086 BLKGRD:%s Invalid data segment\n",
2286 __func__);
2287 return 0;
2288 }
2289 sgl++;
2290 dataphysaddr = sg_dma_address(sgde) + split_offset;
2291
2292 remainder = sg_dma_len(sgde) - split_offset;
2293
2294 if ((subtotal + remainder) <= protgrp_bytes) {
2295 /* we can use this whole buffer */
2296 dma_len = remainder;
2297 split_offset = 0;
2298
2299 if ((subtotal + remainder) == protgrp_bytes)
2300 pgdone = 1;
2301 } else {
2302 /* must split this buffer with next prot grp */
2303 dma_len = protgrp_bytes - subtotal;
2304 split_offset += dma_len;
2305 }
2306
2307 subtotal += dma_len;
2308
2309 sgl->addr_lo = cpu_to_le32(putPaddrLow(dataphysaddr));
2310 sgl->addr_hi = cpu_to_le32(putPaddrHigh(dataphysaddr));
2311 bf_set(lpfc_sli4_sge_last, sgl, 0);
2312 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2313 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2314
2315 sgl->sge_len = cpu_to_le32(dma_len);
2316 dma_offset += dma_len;
2317
2318 num_sge++;
2319 curr_data++;
2320
2321 if (split_offset)
2322 break;
2323
2324 /* Move to the next s/g segment if possible */
2325 sgde = sg_next(sgde);
2326 }
2327
2328 if (protgroup_offset) {
2329 /* update the reference tag */
2330 reftag += protgrp_blks;
2331 sgl++;
2332 continue;
2333 }
2334
2335 /* are we done ? */
2336 if (curr_prot == protcnt) {
2337 bf_set(lpfc_sli4_sge_last, sgl, 1);
2338 alldone = 1;
2339 } else if (curr_prot < protcnt) {
2340 /* advance to next prot buffer */
2341 sgpe = sg_next(sgpe);
2342 sgl++;
2343
2344 /* update the reference tag */
2345 reftag += protgrp_blks;
2346 } else {
2347 /* if we're here, we have a bug */
2348 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2349 "9085 BLKGRD: bug in %s\n", __func__);
2350 }
2351
2352 } while (!alldone);
2353
2354out:
2355
2356 return num_sge;
2357}
2358
2359/**
2360 * lpfc_prot_group_type - Get prtotection group type of SCSI command
2361 * @phba: The Hba for which this call is being executed.
2362 * @sc: pointer to scsi command we're working on
2363 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002364 * Given a SCSI command that supports DIF, determine composition of protection
2365 * groups involved in setting up buffer lists
2366 *
James Smartacd68592012-01-18 16:25:09 -05002367 * Returns: Protection group type (with or without DIF)
2368 *
2369 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002370static int
2371lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
2372{
2373 int ret = LPFC_PG_TYPE_INVALID;
2374 unsigned char op = scsi_get_prot_op(sc);
2375
2376 switch (op) {
2377 case SCSI_PROT_READ_STRIP:
2378 case SCSI_PROT_WRITE_INSERT:
2379 ret = LPFC_PG_TYPE_NO_DIF;
2380 break;
2381 case SCSI_PROT_READ_INSERT:
2382 case SCSI_PROT_WRITE_STRIP:
2383 case SCSI_PROT_READ_PASS:
2384 case SCSI_PROT_WRITE_PASS:
James Smarte2a0a9d2008-12-04 22:40:02 -05002385 ret = LPFC_PG_TYPE_DIF_BUF;
2386 break;
2387 default:
James Smart9c6aa9d2013-05-31 17:03:39 -04002388 if (phba)
2389 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2390 "9021 Unsupported protection op:%d\n",
2391 op);
James Smarte2a0a9d2008-12-04 22:40:02 -05002392 break;
2393 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002394 return ret;
2395}
2396
James Smartacd68592012-01-18 16:25:09 -05002397/**
James Smarta6887e22013-04-17 20:18:07 -04002398 * lpfc_bg_scsi_adjust_dl - Adjust SCSI data length for BlockGuard
2399 * @phba: The Hba for which this call is being executed.
2400 * @lpfc_cmd: The scsi buffer which is going to be adjusted.
2401 *
2402 * Adjust the data length to account for how much data
2403 * is actually on the wire.
2404 *
2405 * returns the adjusted data length
2406 **/
2407static int
2408lpfc_bg_scsi_adjust_dl(struct lpfc_hba *phba,
2409 struct lpfc_scsi_buf *lpfc_cmd)
2410{
2411 struct scsi_cmnd *sc = lpfc_cmd->pCmd;
2412 int fcpdl;
2413
2414 fcpdl = scsi_bufflen(sc);
2415
2416 /* Check if there is protection data on the wire */
2417 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
James Smart9c6aa9d2013-05-31 17:03:39 -04002418 /* Read check for protection data */
James Smarta6887e22013-04-17 20:18:07 -04002419 if (scsi_get_prot_op(sc) == SCSI_PROT_READ_INSERT)
2420 return fcpdl;
2421
2422 } else {
James Smart9c6aa9d2013-05-31 17:03:39 -04002423 /* Write check for protection data */
James Smarta6887e22013-04-17 20:18:07 -04002424 if (scsi_get_prot_op(sc) == SCSI_PROT_WRITE_STRIP)
2425 return fcpdl;
2426 }
2427
2428 /*
2429 * If we are in DIF Type 1 mode every data block has a 8 byte
James Smart9c6aa9d2013-05-31 17:03:39 -04002430 * DIF (trailer) attached to it. Must ajust FCP data length
2431 * to account for the protection data.
James Smarta6887e22013-04-17 20:18:07 -04002432 */
James Smart9c6aa9d2013-05-31 17:03:39 -04002433 fcpdl += (fcpdl / lpfc_cmd_blksize(sc)) * 8;
James Smarta6887e22013-04-17 20:18:07 -04002434
2435 return fcpdl;
2436}
2437
2438/**
James Smartacd68592012-01-18 16:25:09 -05002439 * lpfc_bg_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
2440 * @phba: The Hba for which this call is being executed.
2441 * @lpfc_cmd: The scsi buffer which is going to be prep'ed.
2442 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002443 * This is the protection/DIF aware version of
2444 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
2445 * two functions eventually, but for now, it's here
James Smartacd68592012-01-18 16:25:09 -05002446 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002447static int
James Smartacd68592012-01-18 16:25:09 -05002448lpfc_bg_scsi_prep_dma_buf_s3(struct lpfc_hba *phba,
James Smarte2a0a9d2008-12-04 22:40:02 -05002449 struct lpfc_scsi_buf *lpfc_cmd)
2450{
2451 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2452 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart0794d602019-01-28 11:14:19 -08002453 struct ulp_bde64 *bpl = lpfc_cmd->dma_sgl;
James Smarte2a0a9d2008-12-04 22:40:02 -05002454 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2455 uint32_t num_bde = 0;
2456 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
2457 int prot_group_type = 0;
James Smarta6887e22013-04-17 20:18:07 -04002458 int fcpdl;
James Smart7c4042a2018-11-29 16:09:40 -08002459 struct lpfc_vport *vport = phba->pport;
James Smarte2a0a9d2008-12-04 22:40:02 -05002460
2461 /*
2462 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
2463 * fcp_rsp regions to the first data bde entry
2464 */
2465 bpl += 2;
2466 if (scsi_sg_count(scsi_cmnd)) {
2467 /*
2468 * The driver stores the segment count returned from pci_map_sg
2469 * because this a count of dma-mappings used to map the use_sg
2470 * pages. They are not guaranteed to be the same for those
2471 * architectures that implement an IOMMU.
2472 */
2473 datasegcnt = dma_map_sg(&phba->pcidev->dev,
2474 scsi_sglist(scsi_cmnd),
2475 scsi_sg_count(scsi_cmnd), datadir);
2476 if (unlikely(!datasegcnt))
2477 return 1;
2478
2479 lpfc_cmd->seg_cnt = datasegcnt;
James Smart96f70772013-04-17 20:16:15 -04002480
2481 /* First check if data segment count from SCSI Layer is good */
2482 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt)
2483 goto err;
James Smarte2a0a9d2008-12-04 22:40:02 -05002484
2485 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
2486
2487 switch (prot_group_type) {
2488 case LPFC_PG_TYPE_NO_DIF:
James Smart96f70772013-04-17 20:16:15 -04002489
2490 /* Here we need to add a PDE5 and PDE6 to the count */
2491 if ((lpfc_cmd->seg_cnt + 2) > phba->cfg_total_seg_cnt)
2492 goto err;
2493
James Smarte2a0a9d2008-12-04 22:40:02 -05002494 num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
2495 datasegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002496 /* we should have 2 or more entries in buffer list */
James Smarte2a0a9d2008-12-04 22:40:02 -05002497 if (num_bde < 2)
2498 goto err;
2499 break;
James Smart96f70772013-04-17 20:16:15 -04002500
2501 case LPFC_PG_TYPE_DIF_BUF:
James Smarte2a0a9d2008-12-04 22:40:02 -05002502 /*
2503 * This type indicates that protection buffers are
2504 * passed to the driver, so that needs to be prepared
2505 * for DMA
2506 */
2507 protsegcnt = dma_map_sg(&phba->pcidev->dev,
2508 scsi_prot_sglist(scsi_cmnd),
2509 scsi_prot_sg_count(scsi_cmnd), datadir);
2510 if (unlikely(!protsegcnt)) {
2511 scsi_dma_unmap(scsi_cmnd);
2512 return 1;
2513 }
2514
2515 lpfc_cmd->prot_seg_cnt = protsegcnt;
James Smart96f70772013-04-17 20:16:15 -04002516
2517 /*
2518 * There is a minimun of 4 BPLs used for every
2519 * protection data segment.
2520 */
2521 if ((lpfc_cmd->prot_seg_cnt * 4) >
2522 (phba->cfg_total_seg_cnt - 2))
2523 goto err;
James Smarte2a0a9d2008-12-04 22:40:02 -05002524
2525 num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
2526 datasegcnt, protsegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002527 /* we should have 3 or more entries in buffer list */
James Smart96f70772013-04-17 20:16:15 -04002528 if ((num_bde < 3) ||
2529 (num_bde > phba->cfg_total_seg_cnt))
James Smarte2a0a9d2008-12-04 22:40:02 -05002530 goto err;
2531 break;
James Smart96f70772013-04-17 20:16:15 -04002532
James Smarte2a0a9d2008-12-04 22:40:02 -05002533 case LPFC_PG_TYPE_INVALID:
2534 default:
James Smart96f70772013-04-17 20:16:15 -04002535 scsi_dma_unmap(scsi_cmnd);
2536 lpfc_cmd->seg_cnt = 0;
2537
James Smarte2a0a9d2008-12-04 22:40:02 -05002538 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2539 "9022 Unexpected protection group %i\n",
2540 prot_group_type);
2541 return 1;
2542 }
2543 }
2544
2545 /*
2546 * Finish initializing those IOCB fields that are dependent on the
2547 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
2548 * reinitialized since all iocb memory resources are used many times
2549 * for transmit, receive, and continuation bpl's.
2550 */
2551 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
2552 iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
2553 iocb_cmd->ulpBdeCount = 1;
2554 iocb_cmd->ulpLe = 1;
2555
James Smarta6887e22013-04-17 20:18:07 -04002556 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002557 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
2558
2559 /*
2560 * Due to difference in data length between DIF/non-DIF paths,
2561 * we need to set word 4 of IOCB here
2562 */
2563 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
2564
James Smart7c4042a2018-11-29 16:09:40 -08002565 /*
2566 * For First burst, we may need to adjust the initial transfer
2567 * length for DIF
2568 */
2569 if (iocb_cmd->un.fcpi.fcpi_XRdy &&
2570 (fcpdl < vport->cfg_first_burst_size))
2571 iocb_cmd->un.fcpi.fcpi_XRdy = fcpdl;
2572
James Smarte2a0a9d2008-12-04 22:40:02 -05002573 return 0;
2574err:
James Smart96f70772013-04-17 20:16:15 -04002575 if (lpfc_cmd->seg_cnt)
2576 scsi_dma_unmap(scsi_cmnd);
2577 if (lpfc_cmd->prot_seg_cnt)
2578 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
2579 scsi_prot_sg_count(scsi_cmnd),
2580 scsi_cmnd->sc_data_direction);
2581
James Smarte2a0a9d2008-12-04 22:40:02 -05002582 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart96f70772013-04-17 20:16:15 -04002583 "9023 Cannot setup S/G List for HBA"
2584 "IO segs %d/%d BPL %d SCSI %d: %d %d\n",
2585 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
2586 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
James Smarte2a0a9d2008-12-04 22:40:02 -05002587 prot_group_type, num_bde);
James Smart96f70772013-04-17 20:16:15 -04002588
2589 lpfc_cmd->seg_cnt = 0;
2590 lpfc_cmd->prot_seg_cnt = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05002591 return 1;
2592}
2593
2594/*
James Smart737d4242013-04-17 20:14:49 -04002595 * This function calcuates the T10 DIF guard tag
2596 * on the specified data using a CRC algorithmn
2597 * using crc_t10dif.
2598 */
Rashika Kheria7bfe7812014-09-03 12:55:36 -04002599static uint16_t
James Smart737d4242013-04-17 20:14:49 -04002600lpfc_bg_crc(uint8_t *data, int count)
2601{
2602 uint16_t crc = 0;
2603 uint16_t x;
2604
2605 crc = crc_t10dif(data, count);
2606 x = cpu_to_be16(crc);
2607 return x;
2608}
2609
2610/*
2611 * This function calcuates the T10 DIF guard tag
2612 * on the specified data using a CSUM algorithmn
2613 * using ip_compute_csum.
2614 */
Rashika Kheria7bfe7812014-09-03 12:55:36 -04002615static uint16_t
James Smart737d4242013-04-17 20:14:49 -04002616lpfc_bg_csum(uint8_t *data, int count)
2617{
2618 uint16_t ret;
2619
2620 ret = ip_compute_csum(data, count);
2621 return ret;
2622}
2623
2624/*
2625 * This function examines the protection data to try to determine
2626 * what type of T10-DIF error occurred.
2627 */
Rashika Kheria7bfe7812014-09-03 12:55:36 -04002628static void
James Smart737d4242013-04-17 20:14:49 -04002629lpfc_calc_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
2630{
2631 struct scatterlist *sgpe; /* s/g prot entry */
2632 struct scatterlist *sgde; /* s/g data entry */
2633 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
2634 struct scsi_dif_tuple *src = NULL;
2635 uint8_t *data_src = NULL;
Sebastian Herbsztdb6f1c22015-08-31 16:48:14 -04002636 uint16_t guard_tag;
James Smart737d4242013-04-17 20:14:49 -04002637 uint16_t start_app_tag, app_tag;
2638 uint32_t start_ref_tag, ref_tag;
2639 int prot, protsegcnt;
2640 int err_type, len, data_len;
2641 int chk_ref, chk_app, chk_guard;
2642 uint16_t sum;
2643 unsigned blksize;
2644
2645 err_type = BGS_GUARD_ERR_MASK;
2646 sum = 0;
2647 guard_tag = 0;
2648
2649 /* First check to see if there is protection data to examine */
2650 prot = scsi_get_prot_op(cmd);
2651 if ((prot == SCSI_PROT_READ_STRIP) ||
2652 (prot == SCSI_PROT_WRITE_INSERT) ||
2653 (prot == SCSI_PROT_NORMAL))
2654 goto out;
2655
2656 /* Currently the driver just supports ref_tag and guard_tag checking */
2657 chk_ref = 1;
2658 chk_app = 0;
2659 chk_guard = 0;
2660
2661 /* Setup a ptr to the protection data provided by the SCSI host */
2662 sgpe = scsi_prot_sglist(cmd);
2663 protsegcnt = lpfc_cmd->prot_seg_cnt;
2664
2665 if (sgpe && protsegcnt) {
2666
2667 /*
2668 * We will only try to verify guard tag if the segment
2669 * data length is a multiple of the blksize.
2670 */
2671 sgde = scsi_sglist(cmd);
2672 blksize = lpfc_cmd_blksize(cmd);
2673 data_src = (uint8_t *)sg_virt(sgde);
2674 data_len = sgde->length;
2675 if ((data_len & (blksize - 1)) == 0)
2676 chk_guard = 1;
James Smart737d4242013-04-17 20:14:49 -04002677
James Smarte85d8f92013-05-31 17:04:10 -04002678 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
James Smarta6887e22013-04-17 20:18:07 -04002679 start_ref_tag = (uint32_t)scsi_get_lba(cmd); /* Truncate LBA */
James Smart737d4242013-04-17 20:14:49 -04002680 start_app_tag = src->app_tag;
James Smart737d4242013-04-17 20:14:49 -04002681 len = sgpe->length;
2682 while (src && protsegcnt) {
2683 while (len) {
2684
2685 /*
2686 * First check to see if a protection data
2687 * check is valid
2688 */
Dmitry Monakhov128b6f92017-06-29 11:31:12 -07002689 if ((src->ref_tag == T10_PI_REF_ESCAPE) ||
2690 (src->app_tag == T10_PI_APP_ESCAPE)) {
James Smart737d4242013-04-17 20:14:49 -04002691 start_ref_tag++;
2692 goto skipit;
2693 }
2694
James Smart9c6aa9d2013-05-31 17:03:39 -04002695 /* First Guard Tag checking */
2696 if (chk_guard) {
2697 guard_tag = src->guard_tag;
2698 if (lpfc_cmd_guard_csum(cmd))
2699 sum = lpfc_bg_csum(data_src,
2700 blksize);
2701 else
2702 sum = lpfc_bg_crc(data_src,
2703 blksize);
2704 if ((guard_tag != sum)) {
2705 err_type = BGS_GUARD_ERR_MASK;
2706 goto out;
2707 }
James Smart737d4242013-04-17 20:14:49 -04002708 }
2709
2710 /* Reference Tag checking */
2711 ref_tag = be32_to_cpu(src->ref_tag);
2712 if (chk_ref && (ref_tag != start_ref_tag)) {
2713 err_type = BGS_REFTAG_ERR_MASK;
2714 goto out;
2715 }
2716 start_ref_tag++;
2717
James Smart9c6aa9d2013-05-31 17:03:39 -04002718 /* App Tag checking */
2719 app_tag = src->app_tag;
2720 if (chk_app && (app_tag != start_app_tag)) {
2721 err_type = BGS_APPTAG_ERR_MASK;
2722 goto out;
James Smart737d4242013-04-17 20:14:49 -04002723 }
2724skipit:
2725 len -= sizeof(struct scsi_dif_tuple);
2726 if (len < 0)
2727 len = 0;
2728 src++;
2729
2730 data_src += blksize;
2731 data_len -= blksize;
2732
2733 /*
2734 * Are we at the end of the Data segment?
2735 * The data segment is only used for Guard
2736 * tag checking.
2737 */
2738 if (chk_guard && (data_len == 0)) {
2739 chk_guard = 0;
2740 sgde = sg_next(sgde);
2741 if (!sgde)
2742 goto out;
2743
2744 data_src = (uint8_t *)sg_virt(sgde);
2745 data_len = sgde->length;
2746 if ((data_len & (blksize - 1)) == 0)
2747 chk_guard = 1;
2748 }
2749 }
2750
2751 /* Goto the next Protection data segment */
2752 sgpe = sg_next(sgpe);
2753 if (sgpe) {
2754 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
2755 len = sgpe->length;
2756 } else {
2757 src = NULL;
2758 }
2759 protsegcnt--;
2760 }
2761 }
2762out:
2763 if (err_type == BGS_GUARD_ERR_MASK) {
2764 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2765 0x10, 0x1);
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02002766 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
2767 SAM_STAT_CHECK_CONDITION;
James Smart737d4242013-04-17 20:14:49 -04002768 phba->bg_guard_err_cnt++;
2769 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2770 "9069 BLKGRD: LBA %lx grd_tag error %x != %x\n",
2771 (unsigned long)scsi_get_lba(cmd),
2772 sum, guard_tag);
2773
2774 } else if (err_type == BGS_REFTAG_ERR_MASK) {
2775 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2776 0x10, 0x3);
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02002777 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
2778 SAM_STAT_CHECK_CONDITION;
James Smart737d4242013-04-17 20:14:49 -04002779
2780 phba->bg_reftag_err_cnt++;
2781 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2782 "9066 BLKGRD: LBA %lx ref_tag error %x != %x\n",
2783 (unsigned long)scsi_get_lba(cmd),
2784 ref_tag, start_ref_tag);
2785
2786 } else if (err_type == BGS_APPTAG_ERR_MASK) {
2787 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2788 0x10, 0x2);
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02002789 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
2790 SAM_STAT_CHECK_CONDITION;
James Smart737d4242013-04-17 20:14:49 -04002791
2792 phba->bg_apptag_err_cnt++;
2793 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2794 "9041 BLKGRD: LBA %lx app_tag error %x != %x\n",
2795 (unsigned long)scsi_get_lba(cmd),
2796 app_tag, start_app_tag);
2797 }
2798}
2799
2800
2801/*
James Smarte2a0a9d2008-12-04 22:40:02 -05002802 * This function checks for BlockGuard errors detected by
2803 * the HBA. In case of errors, the ASC/ASCQ fields in the
2804 * sense buffer will be set accordingly, paired with
2805 * ILLEGAL_REQUEST to signal to the kernel that the HBA
2806 * detected corruption.
2807 *
2808 * Returns:
2809 * 0 - No error found
2810 * 1 - BlockGuard error found
2811 * -1 - Internal error (bad profile, ...etc)
2812 */
2813static int
2814lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
2815 struct lpfc_iocbq *pIocbOut)
2816{
2817 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
2818 struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
2819 int ret = 0;
2820 uint32_t bghm = bgf->bghm;
2821 uint32_t bgstat = bgf->bgstat;
2822 uint64_t failing_sector = 0;
2823
James Smarte2a0a9d2008-12-04 22:40:02 -05002824 spin_lock(&_dump_buf_lock);
2825 if (!_dump_buf_done) {
James Smart6a9c52c2009-10-02 15:16:51 -04002826 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9070 BLKGRD: Saving"
2827 " Data for %u blocks to debugfs\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002828 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
James Smart6a9c52c2009-10-02 15:16:51 -04002829 lpfc_debug_save_data(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002830
2831 /* If we have a prot sgl, save the DIF buffer */
2832 if (lpfc_prot_group_type(phba, cmd) ==
2833 LPFC_PG_TYPE_DIF_BUF) {
James Smart6a9c52c2009-10-02 15:16:51 -04002834 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9071 BLKGRD: "
2835 "Saving DIF for %u blocks to debugfs\n",
2836 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
2837 lpfc_debug_save_dif(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002838 }
2839
2840 _dump_buf_done = 1;
2841 }
2842 spin_unlock(&_dump_buf_lock);
2843
2844 if (lpfc_bgs_get_invalid_prof(bgstat)) {
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02002845 cmd->result = DID_ERROR << 16;
James Smart737d4242013-04-17 20:14:49 -04002846 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2847 "9072 BLKGRD: Invalid BG Profile in cmd"
2848 " 0x%x lba 0x%llx blk cnt 0x%x "
2849 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
2850 (unsigned long long)scsi_get_lba(cmd),
2851 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05002852 ret = (-1);
2853 goto out;
2854 }
2855
2856 if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02002857 cmd->result = DID_ERROR << 16;
James Smart737d4242013-04-17 20:14:49 -04002858 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2859 "9073 BLKGRD: Invalid BG PDIF Block in cmd"
2860 " 0x%x lba 0x%llx blk cnt 0x%x "
2861 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
2862 (unsigned long long)scsi_get_lba(cmd),
2863 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05002864 ret = (-1);
2865 goto out;
2866 }
2867
2868 if (lpfc_bgs_get_guard_err(bgstat)) {
2869 ret = 1;
2870
2871 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2872 0x10, 0x1);
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02002873 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
2874 SAM_STAT_CHECK_CONDITION;
James Smarte2a0a9d2008-12-04 22:40:02 -05002875 phba->bg_guard_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04002876 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2877 "9055 BLKGRD: Guard Tag error in cmd"
2878 " 0x%x lba 0x%llx blk cnt 0x%x "
2879 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
2880 (unsigned long long)scsi_get_lba(cmd),
2881 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05002882 }
2883
2884 if (lpfc_bgs_get_reftag_err(bgstat)) {
2885 ret = 1;
2886
2887 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2888 0x10, 0x3);
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02002889 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
2890 SAM_STAT_CHECK_CONDITION;
James Smarte2a0a9d2008-12-04 22:40:02 -05002891
2892 phba->bg_reftag_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04002893 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2894 "9056 BLKGRD: Ref Tag error in cmd"
2895 " 0x%x lba 0x%llx blk cnt 0x%x "
2896 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
2897 (unsigned long long)scsi_get_lba(cmd),
2898 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05002899 }
2900
2901 if (lpfc_bgs_get_apptag_err(bgstat)) {
2902 ret = 1;
2903
2904 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2905 0x10, 0x2);
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02002906 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
2907 SAM_STAT_CHECK_CONDITION;
James Smarte2a0a9d2008-12-04 22:40:02 -05002908
2909 phba->bg_apptag_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04002910 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2911 "9061 BLKGRD: App Tag error in cmd"
2912 " 0x%x lba 0x%llx blk cnt 0x%x "
2913 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
2914 (unsigned long long)scsi_get_lba(cmd),
2915 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05002916 }
2917
2918 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
2919 /*
2920 * setup sense data descriptor 0 per SPC-4 as an information
James Smart7c56b9f2011-07-22 18:36:25 -04002921 * field, and put the failing LBA in it.
2922 * This code assumes there was also a guard/app/ref tag error
2923 * indication.
James Smarte2a0a9d2008-12-04 22:40:02 -05002924 */
James Smart7c56b9f2011-07-22 18:36:25 -04002925 cmd->sense_buffer[7] = 0xc; /* Additional sense length */
2926 cmd->sense_buffer[8] = 0; /* Information descriptor type */
2927 cmd->sense_buffer[9] = 0xa; /* Additional descriptor length */
2928 cmd->sense_buffer[10] = 0x80; /* Validity bit */
James Smartacd68592012-01-18 16:25:09 -05002929
2930 /* bghm is a "on the wire" FC frame based count */
2931 switch (scsi_get_prot_op(cmd)) {
2932 case SCSI_PROT_READ_INSERT:
2933 case SCSI_PROT_WRITE_STRIP:
2934 bghm /= cmd->device->sector_size;
2935 break;
2936 case SCSI_PROT_READ_STRIP:
2937 case SCSI_PROT_WRITE_INSERT:
2938 case SCSI_PROT_READ_PASS:
2939 case SCSI_PROT_WRITE_PASS:
2940 bghm /= (cmd->device->sector_size +
2941 sizeof(struct scsi_dif_tuple));
2942 break;
2943 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002944
2945 failing_sector = scsi_get_lba(cmd);
2946 failing_sector += bghm;
2947
James Smart7c56b9f2011-07-22 18:36:25 -04002948 /* Descriptor Information */
2949 put_unaligned_be64(failing_sector, &cmd->sense_buffer[12]);
James Smarte2a0a9d2008-12-04 22:40:02 -05002950 }
2951
2952 if (!ret) {
2953 /* No error was reported - problem in FW? */
James Smart737d4242013-04-17 20:14:49 -04002954 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2955 "9057 BLKGRD: Unknown error in cmd"
2956 " 0x%x lba 0x%llx blk cnt 0x%x "
2957 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
2958 (unsigned long long)scsi_get_lba(cmd),
2959 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05002960
James Smart737d4242013-04-17 20:14:49 -04002961 /* Calcuate what type of error it was */
2962 lpfc_calc_bg_err(phba, lpfc_cmd);
2963 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002964out:
2965 return ret;
2966}
2967
James Smartea2151b2008-09-07 11:52:10 -04002968/**
James Smartda0436e2009-05-22 14:51:39 -04002969 * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
2970 * @phba: The Hba for which this call is being executed.
2971 * @lpfc_cmd: The scsi buffer which is going to be mapped.
2972 *
2973 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
2974 * field of @lpfc_cmd for device with SLI-4 interface spec.
2975 *
2976 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04002977 * 1 - Error
2978 * 0 - Success
James Smartda0436e2009-05-22 14:51:39 -04002979 **/
2980static int
2981lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
2982{
2983 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2984 struct scatterlist *sgel = NULL;
2985 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart0794d602019-01-28 11:14:19 -08002986 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
James Smartfedd3b72011-02-16 12:39:24 -05002987 struct sli4_sge *first_data_sgl;
James Smartda0436e2009-05-22 14:51:39 -04002988 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2989 dma_addr_t physaddr;
2990 uint32_t num_bde = 0;
2991 uint32_t dma_len;
2992 uint32_t dma_offset = 0;
2993 int nseg;
James Smartfedd3b72011-02-16 12:39:24 -05002994 struct ulp_bde64 *bde;
James Smartda0436e2009-05-22 14:51:39 -04002995
2996 /*
2997 * There are three possibilities here - use scatter-gather segment, use
2998 * the single mapping, or neither. Start the lpfc command prep by
2999 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
3000 * data bde entry.
3001 */
3002 if (scsi_sg_count(scsi_cmnd)) {
3003 /*
3004 * The driver stores the segment count returned from pci_map_sg
3005 * because this a count of dma-mappings used to map the use_sg
3006 * pages. They are not guaranteed to be the same for those
3007 * architectures that implement an IOMMU.
3008 */
3009
3010 nseg = scsi_dma_map(scsi_cmnd);
James Smart5116fbf2015-05-22 10:42:39 -04003011 if (unlikely(nseg <= 0))
James Smartda0436e2009-05-22 14:51:39 -04003012 return 1;
3013 sgl += 1;
3014 /* clear the last flag in the fcp_rsp map entry */
3015 sgl->word2 = le32_to_cpu(sgl->word2);
3016 bf_set(lpfc_sli4_sge_last, sgl, 0);
3017 sgl->word2 = cpu_to_le32(sgl->word2);
3018 sgl += 1;
James Smartfedd3b72011-02-16 12:39:24 -05003019 first_data_sgl = sgl;
James Smartda0436e2009-05-22 14:51:39 -04003020 lpfc_cmd->seg_cnt = nseg;
3021 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04003022 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
3023 " %s: Too many sg segments from "
3024 "dma_map_sg. Config %d, seg_cnt %d\n",
3025 __func__, phba->cfg_sg_seg_cnt,
James Smartda0436e2009-05-22 14:51:39 -04003026 lpfc_cmd->seg_cnt);
James Smart96f70772013-04-17 20:16:15 -04003027 lpfc_cmd->seg_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04003028 scsi_dma_unmap(scsi_cmnd);
3029 return 1;
3030 }
3031
3032 /*
3033 * The driver established a maximum scatter-gather segment count
3034 * during probe that limits the number of sg elements in any
3035 * single scsi command. Just run through the seg_cnt and format
3036 * the sge's.
3037 * When using SLI-3 the driver will try to fit all the BDEs into
3038 * the IOCB. If it can't then the BDEs get added to a BPL as it
3039 * does for SLI-2 mode.
3040 */
3041 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
3042 physaddr = sg_dma_address(sgel);
3043 dma_len = sg_dma_len(sgel);
James Smartda0436e2009-05-22 14:51:39 -04003044 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
3045 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
James Smart05580562011-05-24 11:40:48 -04003046 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -04003047 if ((num_bde + 1) == nseg)
3048 bf_set(lpfc_sli4_sge_last, sgl, 1);
3049 else
3050 bf_set(lpfc_sli4_sge_last, sgl, 0);
3051 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
James Smartf9bb2da2011-10-10 21:34:11 -04003052 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
James Smartda0436e2009-05-22 14:51:39 -04003053 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05003054 sgl->sge_len = cpu_to_le32(dma_len);
James Smartda0436e2009-05-22 14:51:39 -04003055 dma_offset += dma_len;
3056 sgl++;
3057 }
James Smart0bc2b7c2018-02-22 08:18:48 -08003058 /*
3059 * Setup the first Payload BDE. For FCoE we just key off
James Smart414abe02018-06-26 08:24:26 -07003060 * Performance Hints, for FC we use lpfc_enable_pbde.
3061 * We populate words 13-15 of IOCB/WQE.
James Smart0bc2b7c2018-02-22 08:18:48 -08003062 */
3063 if ((phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) ||
James Smart414abe02018-06-26 08:24:26 -07003064 phba->cfg_enable_pbde) {
James Smartfedd3b72011-02-16 12:39:24 -05003065 bde = (struct ulp_bde64 *)
James Smart414abe02018-06-26 08:24:26 -07003066 &(iocb_cmd->unsli3.sli3Words[5]);
James Smartfedd3b72011-02-16 12:39:24 -05003067 bde->addrLow = first_data_sgl->addr_lo;
3068 bde->addrHigh = first_data_sgl->addr_hi;
3069 bde->tus.f.bdeSize =
3070 le32_to_cpu(first_data_sgl->sge_len);
3071 bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
3072 bde->tus.w = cpu_to_le32(bde->tus.w);
3073 }
James Smartda0436e2009-05-22 14:51:39 -04003074 } else {
3075 sgl += 1;
3076 /* clear the last flag in the fcp_rsp map entry */
3077 sgl->word2 = le32_to_cpu(sgl->word2);
3078 bf_set(lpfc_sli4_sge_last, sgl, 1);
3079 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart414abe02018-06-26 08:24:26 -07003080
3081 if ((phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) ||
3082 phba->cfg_enable_pbde) {
3083 bde = (struct ulp_bde64 *)
3084 &(iocb_cmd->unsli3.sli3Words[5]);
3085 memset(bde, 0, (sizeof(uint32_t) * 3));
3086 }
James Smartda0436e2009-05-22 14:51:39 -04003087 }
3088
3089 /*
3090 * Finish initializing those IOCB fields that are dependent on the
3091 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
3092 * explicitly reinitialized.
3093 * all iocb memory resources are reused.
3094 */
3095 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
3096
3097 /*
3098 * Due to difference in data length between DIF/non-DIF paths,
3099 * we need to set word 4 of IOCB here
3100 */
3101 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
James Smart1ba981f2014-02-20 09:56:45 -05003102
3103 /*
3104 * If the OAS driver feature is enabled and the lun is enabled for
3105 * OAS, set the oas iocb related flags.
3106 */
James Smartf38fa0b2014-04-04 13:52:21 -04003107 if ((phba->cfg_fof) && ((struct lpfc_device_data *)
James Smartc92c8412016-07-06 12:36:05 -07003108 scsi_cmnd->device->hostdata)->oas_enabled) {
James Smart9bd2bff52014-09-03 12:57:30 -04003109 lpfc_cmd->cur_iocbq.iocb_flag |= (LPFC_IO_OAS | LPFC_IO_FOF);
James Smartc92c8412016-07-06 12:36:05 -07003110 lpfc_cmd->cur_iocbq.priority = ((struct lpfc_device_data *)
3111 scsi_cmnd->device->hostdata)->priority;
3112 }
James Smartda0436e2009-05-22 14:51:39 -04003113 return 0;
3114}
3115
3116/**
James Smartacd68592012-01-18 16:25:09 -05003117 * lpfc_bg_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3118 * @phba: The Hba for which this call is being executed.
3119 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3120 *
3121 * This is the protection/DIF aware version of
3122 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
3123 * two functions eventually, but for now, it's here
3124 **/
3125static int
3126lpfc_bg_scsi_prep_dma_buf_s4(struct lpfc_hba *phba,
3127 struct lpfc_scsi_buf *lpfc_cmd)
3128{
3129 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3130 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart0794d602019-01-28 11:14:19 -08003131 struct sli4_sge *sgl = (struct sli4_sge *)(lpfc_cmd->dma_sgl);
James Smartacd68592012-01-18 16:25:09 -05003132 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart96f70772013-04-17 20:16:15 -04003133 uint32_t num_sge = 0;
James Smartacd68592012-01-18 16:25:09 -05003134 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
3135 int prot_group_type = 0;
3136 int fcpdl;
James Smart7c4042a2018-11-29 16:09:40 -08003137 struct lpfc_vport *vport = phba->pport;
James Smartacd68592012-01-18 16:25:09 -05003138
3139 /*
3140 * Start the lpfc command prep by bumping the sgl beyond fcp_cmnd
James Smart96f70772013-04-17 20:16:15 -04003141 * fcp_rsp regions to the first data sge entry
James Smartacd68592012-01-18 16:25:09 -05003142 */
3143 if (scsi_sg_count(scsi_cmnd)) {
3144 /*
3145 * The driver stores the segment count returned from pci_map_sg
3146 * because this a count of dma-mappings used to map the use_sg
3147 * pages. They are not guaranteed to be the same for those
3148 * architectures that implement an IOMMU.
3149 */
3150 datasegcnt = dma_map_sg(&phba->pcidev->dev,
3151 scsi_sglist(scsi_cmnd),
3152 scsi_sg_count(scsi_cmnd), datadir);
3153 if (unlikely(!datasegcnt))
3154 return 1;
3155
3156 sgl += 1;
3157 /* clear the last flag in the fcp_rsp map entry */
3158 sgl->word2 = le32_to_cpu(sgl->word2);
3159 bf_set(lpfc_sli4_sge_last, sgl, 0);
3160 sgl->word2 = cpu_to_le32(sgl->word2);
3161
3162 sgl += 1;
3163 lpfc_cmd->seg_cnt = datasegcnt;
James Smart96f70772013-04-17 20:16:15 -04003164
3165 /* First check if data segment count from SCSI Layer is good */
3166 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt)
3167 goto err;
James Smartacd68592012-01-18 16:25:09 -05003168
3169 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
3170
3171 switch (prot_group_type) {
3172 case LPFC_PG_TYPE_NO_DIF:
James Smart96f70772013-04-17 20:16:15 -04003173 /* Here we need to add a DISEED to the count */
3174 if ((lpfc_cmd->seg_cnt + 1) > phba->cfg_total_seg_cnt)
3175 goto err;
3176
3177 num_sge = lpfc_bg_setup_sgl(phba, scsi_cmnd, sgl,
James Smartacd68592012-01-18 16:25:09 -05003178 datasegcnt);
James Smart96f70772013-04-17 20:16:15 -04003179
James Smartacd68592012-01-18 16:25:09 -05003180 /* we should have 2 or more entries in buffer list */
James Smart96f70772013-04-17 20:16:15 -04003181 if (num_sge < 2)
James Smartacd68592012-01-18 16:25:09 -05003182 goto err;
3183 break;
James Smart96f70772013-04-17 20:16:15 -04003184
3185 case LPFC_PG_TYPE_DIF_BUF:
James Smartacd68592012-01-18 16:25:09 -05003186 /*
3187 * This type indicates that protection buffers are
3188 * passed to the driver, so that needs to be prepared
3189 * for DMA
3190 */
3191 protsegcnt = dma_map_sg(&phba->pcidev->dev,
3192 scsi_prot_sglist(scsi_cmnd),
3193 scsi_prot_sg_count(scsi_cmnd), datadir);
3194 if (unlikely(!protsegcnt)) {
3195 scsi_dma_unmap(scsi_cmnd);
3196 return 1;
3197 }
3198
3199 lpfc_cmd->prot_seg_cnt = protsegcnt;
James Smart96f70772013-04-17 20:16:15 -04003200 /*
3201 * There is a minimun of 3 SGEs used for every
3202 * protection data segment.
3203 */
3204 if ((lpfc_cmd->prot_seg_cnt * 3) >
3205 (phba->cfg_total_seg_cnt - 2))
3206 goto err;
James Smartacd68592012-01-18 16:25:09 -05003207
James Smart96f70772013-04-17 20:16:15 -04003208 num_sge = lpfc_bg_setup_sgl_prot(phba, scsi_cmnd, sgl,
James Smartacd68592012-01-18 16:25:09 -05003209 datasegcnt, protsegcnt);
James Smart96f70772013-04-17 20:16:15 -04003210
James Smartacd68592012-01-18 16:25:09 -05003211 /* we should have 3 or more entries in buffer list */
James Smart96f70772013-04-17 20:16:15 -04003212 if ((num_sge < 3) ||
3213 (num_sge > phba->cfg_total_seg_cnt))
James Smartacd68592012-01-18 16:25:09 -05003214 goto err;
3215 break;
James Smart96f70772013-04-17 20:16:15 -04003216
James Smartacd68592012-01-18 16:25:09 -05003217 case LPFC_PG_TYPE_INVALID:
3218 default:
James Smart96f70772013-04-17 20:16:15 -04003219 scsi_dma_unmap(scsi_cmnd);
3220 lpfc_cmd->seg_cnt = 0;
3221
James Smartacd68592012-01-18 16:25:09 -05003222 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
3223 "9083 Unexpected protection group %i\n",
3224 prot_group_type);
3225 return 1;
3226 }
3227 }
3228
James Smart8012cc32012-10-31 14:44:49 -04003229 switch (scsi_get_prot_op(scsi_cmnd)) {
3230 case SCSI_PROT_WRITE_STRIP:
3231 case SCSI_PROT_READ_STRIP:
3232 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_STRIP;
3233 break;
3234 case SCSI_PROT_WRITE_INSERT:
3235 case SCSI_PROT_READ_INSERT:
3236 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_INSERT;
3237 break;
3238 case SCSI_PROT_WRITE_PASS:
3239 case SCSI_PROT_READ_PASS:
3240 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_PASS;
3241 break;
3242 }
3243
James Smartacd68592012-01-18 16:25:09 -05003244 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
James Smartacd68592012-01-18 16:25:09 -05003245 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
3246
3247 /*
3248 * Due to difference in data length between DIF/non-DIF paths,
3249 * we need to set word 4 of IOCB here
3250 */
3251 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
James Smartacd68592012-01-18 16:25:09 -05003252
James Smart9bd2bff52014-09-03 12:57:30 -04003253 /*
James Smart7c4042a2018-11-29 16:09:40 -08003254 * For First burst, we may need to adjust the initial transfer
3255 * length for DIF
3256 */
3257 if (iocb_cmd->un.fcpi.fcpi_XRdy &&
3258 (fcpdl < vport->cfg_first_burst_size))
3259 iocb_cmd->un.fcpi.fcpi_XRdy = fcpdl;
3260
3261 /*
James Smart9bd2bff52014-09-03 12:57:30 -04003262 * If the OAS driver feature is enabled and the lun is enabled for
3263 * OAS, set the oas iocb related flags.
3264 */
3265 if ((phba->cfg_fof) && ((struct lpfc_device_data *)
3266 scsi_cmnd->device->hostdata)->oas_enabled)
3267 lpfc_cmd->cur_iocbq.iocb_flag |= (LPFC_IO_OAS | LPFC_IO_FOF);
3268
James Smartacd68592012-01-18 16:25:09 -05003269 return 0;
3270err:
James Smart96f70772013-04-17 20:16:15 -04003271 if (lpfc_cmd->seg_cnt)
3272 scsi_dma_unmap(scsi_cmnd);
3273 if (lpfc_cmd->prot_seg_cnt)
3274 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
3275 scsi_prot_sg_count(scsi_cmnd),
3276 scsi_cmnd->sc_data_direction);
3277
James Smartacd68592012-01-18 16:25:09 -05003278 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart96f70772013-04-17 20:16:15 -04003279 "9084 Cannot setup S/G List for HBA"
3280 "IO segs %d/%d SGL %d SCSI %d: %d %d\n",
3281 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
3282 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
3283 prot_group_type, num_sge);
3284
3285 lpfc_cmd->seg_cnt = 0;
3286 lpfc_cmd->prot_seg_cnt = 0;
James Smartacd68592012-01-18 16:25:09 -05003287 return 1;
3288}
3289
3290/**
James Smart3772a992009-05-22 14:50:54 -04003291 * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3292 * @phba: The Hba for which this call is being executed.
3293 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3294 *
3295 * This routine wraps the actual DMA mapping function pointer from the
3296 * lpfc_hba struct.
3297 *
3298 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04003299 * 1 - Error
3300 * 0 - Success
James Smart3772a992009-05-22 14:50:54 -04003301 **/
3302static inline int
3303lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3304{
3305 return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
3306}
3307
3308/**
James Smartacd68592012-01-18 16:25:09 -05003309 * lpfc_bg_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3310 * using BlockGuard.
3311 * @phba: The Hba for which this call is being executed.
3312 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3313 *
3314 * This routine wraps the actual DMA mapping function pointer from the
3315 * lpfc_hba struct.
3316 *
3317 * Return codes:
3318 * 1 - Error
3319 * 0 - Success
3320 **/
3321static inline int
3322lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3323{
3324 return phba->lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
3325}
3326
3327/**
James Smart3621a712009-04-06 18:47:14 -04003328 * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
James Smartea2151b2008-09-07 11:52:10 -04003329 * @phba: Pointer to hba context object.
3330 * @vport: Pointer to vport object.
3331 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
3332 * @rsp_iocb: Pointer to response iocb object which reported error.
3333 *
3334 * This function posts an event when there is a SCSI command reporting
3335 * error from the scsi device.
3336 **/
3337static void
3338lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
3339 struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
3340 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3341 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
3342 uint32_t resp_info = fcprsp->rspStatus2;
3343 uint32_t scsi_status = fcprsp->rspStatus3;
3344 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
3345 struct lpfc_fast_path_event *fast_path_evt = NULL;
3346 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
3347 unsigned long flags;
3348
James Smart5989b8d2010-10-22 11:06:56 -04003349 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3350 return;
3351
James Smartea2151b2008-09-07 11:52:10 -04003352 /* If there is queuefull or busy condition send a scsi event */
3353 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
3354 (cmnd->result == SAM_STAT_BUSY)) {
3355 fast_path_evt = lpfc_alloc_fast_evt(phba);
3356 if (!fast_path_evt)
3357 return;
3358 fast_path_evt->un.scsi_evt.event_type =
3359 FC_REG_SCSI_EVENT;
3360 fast_path_evt->un.scsi_evt.subcategory =
3361 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
3362 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
3363 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
3364 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
3365 &pnode->nlp_portname, sizeof(struct lpfc_name));
3366 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
3367 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3368 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
3369 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
3370 fast_path_evt = lpfc_alloc_fast_evt(phba);
3371 if (!fast_path_evt)
3372 return;
3373 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
3374 FC_REG_SCSI_EVENT;
3375 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
3376 LPFC_EVENT_CHECK_COND;
3377 fast_path_evt->un.check_cond_evt.scsi_event.lun =
3378 cmnd->device->lun;
3379 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
3380 &pnode->nlp_portname, sizeof(struct lpfc_name));
3381 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
3382 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3383 fast_path_evt->un.check_cond_evt.sense_key =
3384 cmnd->sense_buffer[2] & 0xf;
3385 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
3386 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
3387 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3388 fcpi_parm &&
3389 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
3390 ((scsi_status == SAM_STAT_GOOD) &&
3391 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
3392 /*
3393 * If status is good or resid does not match with fcp_param and
3394 * there is valid fcpi_parm, then there is a read_check error
3395 */
3396 fast_path_evt = lpfc_alloc_fast_evt(phba);
3397 if (!fast_path_evt)
3398 return;
3399 fast_path_evt->un.read_check_error.header.event_type =
3400 FC_REG_FABRIC_EVENT;
3401 fast_path_evt->un.read_check_error.header.subcategory =
3402 LPFC_EVENT_FCPRDCHKERR;
3403 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
3404 &pnode->nlp_portname, sizeof(struct lpfc_name));
3405 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
3406 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3407 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
3408 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
3409 fast_path_evt->un.read_check_error.fcpiparam =
3410 fcpi_parm;
3411 } else
3412 return;
3413
3414 fast_path_evt->vport = vport;
3415 spin_lock_irqsave(&phba->hbalock, flags);
3416 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
3417 spin_unlock_irqrestore(&phba->hbalock, flags);
3418 lpfc_worker_wake_up(phba);
3419 return;
3420}
James Smart9bad7672008-12-04 22:39:02 -05003421
3422/**
James Smartf1126682009-06-10 17:22:44 -04003423 * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
James Smart3772a992009-05-22 14:50:54 -04003424 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -05003425 * @psb: The scsi buffer which is going to be un-mapped.
3426 *
3427 * This routine does DMA un-mapping of scatter gather list of scsi command
James Smart3772a992009-05-22 14:50:54 -04003428 * field of @lpfc_cmd for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05003429 **/
dea31012005-04-17 16:05:31 -05003430static void
James Smartf1126682009-06-10 17:22:44 -04003431lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James Smartbcf4dbf2006-07-06 15:50:08 -04003432{
3433 /*
3434 * There are only two special cases to consider. (1) the scsi command
3435 * requested scatter-gather usage or (2) the scsi command allocated
3436 * a request buffer, but did not request use_sg. There is a third
3437 * case, but it does not require resource deallocation.
3438 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003439 if (psb->seg_cnt > 0)
3440 scsi_dma_unmap(psb->pCmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003441 if (psb->prot_seg_cnt > 0)
3442 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
3443 scsi_prot_sg_count(psb->pCmd),
3444 psb->pCmd->sc_data_direction);
James Smartbcf4dbf2006-07-06 15:50:08 -04003445}
3446
James Smart9bad7672008-12-04 22:39:02 -05003447/**
James Smart3621a712009-04-06 18:47:14 -04003448 * lpfc_handler_fcp_err - FCP response handler
James Smart9bad7672008-12-04 22:39:02 -05003449 * @vport: The virtual port for which this call is being executed.
3450 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
3451 * @rsp_iocb: The response IOCB which contains FCP error.
3452 *
3453 * This routine is called to process response IOCB with status field
3454 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
3455 * based upon SCSI and FCP error.
3456 **/
James Smartbcf4dbf2006-07-06 15:50:08 -04003457static void
James Smart2e0fef82007-06-17 19:56:36 -05003458lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
3459 struct lpfc_iocbq *rsp_iocb)
dea31012005-04-17 16:05:31 -05003460{
James Smart5afab6b2015-12-16 18:12:01 -05003461 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003462 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3463 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
3464 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
James Smart7054a602007-04-25 09:52:34 -04003465 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
dea31012005-04-17 16:05:31 -05003466 uint32_t resp_info = fcprsp->rspStatus2;
3467 uint32_t scsi_status = fcprsp->rspStatus3;
James Smartc7743952006-12-02 13:34:42 -05003468 uint32_t *lp;
dea31012005-04-17 16:05:31 -05003469 uint32_t host_status = DID_OK;
3470 uint32_t rsplen = 0;
James Smart5afab6b2015-12-16 18:12:01 -05003471 uint32_t fcpDl;
James Smartc7743952006-12-02 13:34:42 -05003472 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea31012005-04-17 16:05:31 -05003473
James Smartea2151b2008-09-07 11:52:10 -04003474
dea31012005-04-17 16:05:31 -05003475 /*
3476 * If this is a task management command, there is no
3477 * scsi packet associated with this lpfc_cmd. The driver
3478 * consumes it.
3479 */
3480 if (fcpcmd->fcpCntl2) {
3481 scsi_status = 0;
3482 goto out;
3483 }
3484
James Smart6a9c52c2009-10-02 15:16:51 -04003485 if (resp_info & RSP_LEN_VALID) {
3486 rsplen = be32_to_cpu(fcprsp->rspRspLen);
James Smarte40a02c2010-02-26 14:13:54 -05003487 if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
James Smart6a9c52c2009-10-02 15:16:51 -04003488 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3489 "2719 Invalid response length: "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02003490 "tgt x%x lun x%llx cmnd x%x rsplen x%x\n",
James Smart6a9c52c2009-10-02 15:16:51 -04003491 cmnd->device->id,
3492 cmnd->device->lun, cmnd->cmnd[0],
3493 rsplen);
3494 host_status = DID_ERROR;
3495 goto out;
3496 }
James Smarte40a02c2010-02-26 14:13:54 -05003497 if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
3498 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3499 "2757 Protocol failure detected during "
3500 "processing of FCP I/O op: "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02003501 "tgt x%x lun x%llx cmnd x%x rspInfo3 x%x\n",
James Smarte40a02c2010-02-26 14:13:54 -05003502 cmnd->device->id,
3503 cmnd->device->lun, cmnd->cmnd[0],
3504 fcprsp->rspInfo3);
3505 host_status = DID_ERROR;
3506 goto out;
3507 }
James Smart6a9c52c2009-10-02 15:16:51 -04003508 }
3509
James Smartc7743952006-12-02 13:34:42 -05003510 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
3511 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
3512 if (snslen > SCSI_SENSE_BUFFERSIZE)
3513 snslen = SCSI_SENSE_BUFFERSIZE;
3514
3515 if (resp_info & RSP_LEN_VALID)
3516 rsplen = be32_to_cpu(fcprsp->rspRspLen);
3517 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
3518 }
3519 lp = (uint32_t *)cmnd->sense_buffer;
3520
James Smartaa1c7ee2012-08-14 14:26:06 -04003521 /* special handling for under run conditions */
3522 if (!scsi_status && (resp_info & RESID_UNDER)) {
3523 /* don't log under runs if fcp set... */
3524 if (vport->cfg_log_verbose & LOG_FCP)
3525 logit = LOG_FCP_ERROR;
3526 /* unless operator says so */
3527 if (vport->cfg_log_verbose & LOG_FCP_UNDER)
3528 logit = LOG_FCP_UNDER;
3529 }
James Smartc7743952006-12-02 13:34:42 -05003530
James Smarte8b62012007-08-02 11:10:09 -04003531 lpfc_printf_vlog(vport, KERN_WARNING, logit,
James Smarte2a0a9d2008-12-04 22:40:02 -05003532 "9024 FCP command x%x failed: x%x SNS x%x x%x "
James Smarte8b62012007-08-02 11:10:09 -04003533 "Data: x%x x%x x%x x%x x%x\n",
3534 cmnd->cmnd[0], scsi_status,
3535 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
3536 be32_to_cpu(fcprsp->rspResId),
3537 be32_to_cpu(fcprsp->rspSnsLen),
3538 be32_to_cpu(fcprsp->rspRspLen),
3539 fcprsp->rspInfo3);
dea31012005-04-17 16:05:31 -05003540
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003541 scsi_set_resid(cmnd, 0);
James Smart5afab6b2015-12-16 18:12:01 -05003542 fcpDl = be32_to_cpu(fcpcmd->fcpDl);
dea31012005-04-17 16:05:31 -05003543 if (resp_info & RESID_UNDER) {
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003544 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea31012005-04-17 16:05:31 -05003545
James Smart73d91e52011-10-10 21:32:10 -04003546 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_UNDER,
James Smart45634a862018-01-30 15:59:00 -08003547 "9025 FCP Underrun, expected %d, "
James Smarte8b62012007-08-02 11:10:09 -04003548 "residual %d Data: x%x x%x x%x\n",
James Smart5afab6b2015-12-16 18:12:01 -05003549 fcpDl,
James Smarte8b62012007-08-02 11:10:09 -04003550 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
3551 cmnd->underflow);
dea31012005-04-17 16:05:31 -05003552
3553 /*
James Smart45634a862018-01-30 15:59:00 -08003554 * If there is an under run, check if under run reported by
James Smart7054a602007-04-25 09:52:34 -04003555 * storage array is same as the under run reported by HBA.
3556 * If this is not same, there is a dropped frame.
3557 */
James Smart45634a862018-01-30 15:59:00 -08003558 if (fcpi_parm && (scsi_get_resid(cmnd) != fcpi_parm)) {
James Smarte8b62012007-08-02 11:10:09 -04003559 lpfc_printf_vlog(vport, KERN_WARNING,
3560 LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05003561 "9026 FCP Read Check Error "
James Smarte8b62012007-08-02 11:10:09 -04003562 "and Underrun Data: x%x x%x x%x x%x\n",
James Smart5afab6b2015-12-16 18:12:01 -05003563 fcpDl,
James Smarte8b62012007-08-02 11:10:09 -04003564 scsi_get_resid(cmnd), fcpi_parm,
3565 cmnd->cmnd[0]);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003566 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
James Smart7054a602007-04-25 09:52:34 -04003567 host_status = DID_ERROR;
3568 }
3569 /*
dea31012005-04-17 16:05:31 -05003570 * The cmnd->underflow is the minimum number of bytes that must
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003571 * be transferred for this command. Provided a sense condition
dea31012005-04-17 16:05:31 -05003572 * is not present, make sure the actual amount transferred is at
3573 * least the underflow value or fail.
3574 */
3575 if (!(resp_info & SNS_LEN_VALID) &&
3576 (scsi_status == SAM_STAT_GOOD) &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003577 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
3578 < cmnd->underflow)) {
James Smarte8b62012007-08-02 11:10:09 -04003579 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05003580 "9027 FCP command x%x residual "
James Smarte8b62012007-08-02 11:10:09 -04003581 "underrun converted to error "
3582 "Data: x%x x%x x%x\n",
James Smart66dbfbe2007-08-05 06:08:38 -04003583 cmnd->cmnd[0], scsi_bufflen(cmnd),
James Smarte8b62012007-08-02 11:10:09 -04003584 scsi_get_resid(cmnd), cmnd->underflow);
dea31012005-04-17 16:05:31 -05003585 host_status = DID_ERROR;
3586 }
3587 } else if (resp_info & RESID_OVER) {
James Smarte8b62012007-08-02 11:10:09 -04003588 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05003589 "9028 FCP command x%x residual overrun error. "
James Smarte4e74272009-07-19 10:01:38 -04003590 "Data: x%x x%x\n", cmnd->cmnd[0],
James Smarte8b62012007-08-02 11:10:09 -04003591 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea31012005-04-17 16:05:31 -05003592 host_status = DID_ERROR;
3593
3594 /*
3595 * Check SLI validation that all the transfer was actually done
James Smart26373d22013-09-06 12:19:17 -04003596 * (fcpi_parm should be zero). Apply check only to reads.
dea31012005-04-17 16:05:31 -05003597 */
James Smart5afab6b2015-12-16 18:12:01 -05003598 } else if (fcpi_parm) {
James Smarte8b62012007-08-02 11:10:09 -04003599 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
James Smart5afab6b2015-12-16 18:12:01 -05003600 "9029 FCP %s Check Error xri x%x Data: "
James Smarteee88772010-09-29 11:19:08 -04003601 "x%x x%x x%x x%x x%x\n",
James Smart5afab6b2015-12-16 18:12:01 -05003602 ((cmnd->sc_data_direction == DMA_FROM_DEVICE) ?
3603 "Read" : "Write"),
3604 ((phba->sli_rev == LPFC_SLI_REV4) ?
3605 lpfc_cmd->cur_iocbq.sli4_xritag :
3606 rsp_iocb->iocb.ulpContext),
3607 fcpDl, be32_to_cpu(fcprsp->rspResId),
James Smarteee88772010-09-29 11:19:08 -04003608 fcpi_parm, cmnd->cmnd[0], scsi_status);
James Smart5afab6b2015-12-16 18:12:01 -05003609
3610 /* There is some issue with the LPe12000 that causes it
3611 * to miscalculate the fcpi_parm and falsely trip this
3612 * recovery logic. Detect this case and don't error when true.
3613 */
3614 if (fcpi_parm > fcpDl)
3615 goto out;
3616
James Smarteee88772010-09-29 11:19:08 -04003617 switch (scsi_status) {
3618 case SAM_STAT_GOOD:
3619 case SAM_STAT_CHECK_CONDITION:
3620 /* Fabric dropped a data frame. Fail any successful
3621 * command in which we detected dropped frames.
3622 * A status of good or some check conditions could
3623 * be considered a successful command.
3624 */
3625 host_status = DID_ERROR;
3626 break;
3627 }
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003628 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea31012005-04-17 16:05:31 -05003629 }
3630
3631 out:
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02003632 cmnd->result = host_status << 16 | scsi_status;
James Smartea2151b2008-09-07 11:52:10 -04003633 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
dea31012005-04-17 16:05:31 -05003634}
3635
James Smart9bad7672008-12-04 22:39:02 -05003636/**
James Smart8b0dff12015-05-22 10:42:38 -04003637 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
3638 * @phba: Pointer to HBA context object.
3639 *
3640 * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
3641 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
3642 * held.
3643 * If scsi-mq is enabled, get the default block layer mapping of software queues
3644 * to hardware queues. This information is saved in request tag.
3645 *
3646 * Return: index into SLI4 fast-path FCP queue index.
3647 **/
3648int lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba,
3649 struct lpfc_scsi_buf *lpfc_cmd)
3650{
3651 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3652 struct lpfc_vector_map_info *cpup;
3653 int chann, cpu;
3654 uint32_t tag;
3655 uint16_t hwq;
3656
Jens Axboef664a3c2018-11-01 16:36:27 -06003657 if (cmnd) {
James Smart8b0dff12015-05-22 10:42:38 -04003658 tag = blk_mq_unique_tag(cmnd->request);
3659 hwq = blk_mq_unique_tag_to_hwq(tag);
3660
3661 return hwq;
3662 }
3663
James Smartcdb42be2019-01-28 11:14:21 -08003664 if (phba->cfg_fcp_io_sched == LPFC_FCP_SCHED_BY_CPU &&
3665 phba->cfg_hdw_queue > 1) {
James Smart0794d602019-01-28 11:14:19 -08003666 cpu = lpfc_cmd->cpu;
James Smart8b0dff12015-05-22 10:42:38 -04003667 if (cpu < phba->sli4_hba.num_present_cpu) {
3668 cpup = phba->sli4_hba.cpu_map;
3669 cpup += cpu;
3670 return cpup->channel_id;
3671 }
3672 }
3673 chann = atomic_add_return(1, &phba->fcp_qidx);
James Smartcdb42be2019-01-28 11:14:21 -08003674 chann = chann % phba->cfg_hdw_queue;
James Smart8b0dff12015-05-22 10:42:38 -04003675 return chann;
3676}
3677
3678
3679/**
James Smart3621a712009-04-06 18:47:14 -04003680 * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
James Smart9bad7672008-12-04 22:39:02 -05003681 * @phba: The Hba for which this call is being executed.
3682 * @pIocbIn: The command IOCBQ for the scsi cmnd.
James Smart3772a992009-05-22 14:50:54 -04003683 * @pIocbOut: The response IOCBQ for the scsi cmnd.
James Smart9bad7672008-12-04 22:39:02 -05003684 *
3685 * This routine assigns scsi command result by looking into response IOCB
3686 * status field appropriately. This routine handles QUEUE FULL condition as
3687 * well by ramping down device queue depth.
3688 **/
dea31012005-04-17 16:05:31 -05003689static void
3690lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
3691 struct lpfc_iocbq *pIocbOut)
3692{
3693 struct lpfc_scsi_buf *lpfc_cmd =
3694 (struct lpfc_scsi_buf *) pIocbIn->context1;
James Smart2e0fef82007-06-17 19:56:36 -05003695 struct lpfc_vport *vport = pIocbIn->vport;
dea31012005-04-17 16:05:31 -05003696 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
3697 struct lpfc_nodelist *pnode = rdata->pnode;
James Smart75baf692010-06-08 18:31:21 -04003698 struct scsi_cmnd *cmd;
James Smartfa61a542008-01-11 01:52:42 -05003699 unsigned long flags;
James Smartea2151b2008-09-07 11:52:10 -04003700 struct lpfc_fast_path_event *fast_path_evt;
James Smart75baf692010-06-08 18:31:21 -04003701 struct Scsi_Host *shost;
James Smart73d91e52011-10-10 21:32:10 -04003702 uint32_t logit = LOG_FCP;
dea31012005-04-17 16:05:31 -05003703
James Smart2cee7802017-06-01 21:07:02 -07003704 atomic_inc(&phba->fc4ScsiIoCmpls);
James Smart895427b2017-02-12 13:52:30 -08003705
James Smart75baf692010-06-08 18:31:21 -04003706 /* Sanity check on return of outstanding command */
James Smart75baf692010-06-08 18:31:21 -04003707 cmd = lpfc_cmd->pCmd;
James Smartc90261d2015-12-16 18:11:57 -05003708 if (!cmd)
3709 return;
James Smart75baf692010-06-08 18:31:21 -04003710 shost = cmd->device->host;
3711
James Smarte3d2b802012-08-14 14:25:43 -04003712 lpfc_cmd->result = (pIocbOut->iocb.un.ulpWord[4] & IOERR_PARAM_MASK);
dea31012005-04-17 16:05:31 -05003713 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
James Smart341af102010-01-26 23:07:37 -05003714 /* pick up SLI4 exhange busy status from HBA */
3715 lpfc_cmd->exch_busy = pIocbOut->iocb_flag & LPFC_EXCHANGE_BUSY;
3716
James Smart9a6b09c2012-03-01 22:37:42 -05003717#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
3718 if (lpfc_cmd->prot_data_type) {
3719 struct scsi_dif_tuple *src = NULL;
3720
3721 src = (struct scsi_dif_tuple *)lpfc_cmd->prot_data_segment;
3722 /*
3723 * Used to restore any changes to protection
3724 * data for error injection.
3725 */
3726 switch (lpfc_cmd->prot_data_type) {
3727 case LPFC_INJERR_REFTAG:
3728 src->ref_tag =
3729 lpfc_cmd->prot_data;
3730 break;
3731 case LPFC_INJERR_APPTAG:
3732 src->app_tag =
3733 (uint16_t)lpfc_cmd->prot_data;
3734 break;
3735 case LPFC_INJERR_GUARD:
3736 src->guard_tag =
3737 (uint16_t)lpfc_cmd->prot_data;
3738 break;
3739 default:
3740 break;
3741 }
3742
3743 lpfc_cmd->prot_data = 0;
3744 lpfc_cmd->prot_data_type = 0;
3745 lpfc_cmd->prot_data_segment = NULL;
3746 }
3747#endif
James Smart2ea259e2017-02-12 13:52:27 -08003748
dea31012005-04-17 16:05:31 -05003749 if (lpfc_cmd->status) {
3750 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
3751 (lpfc_cmd->result & IOERR_DRVR_MASK))
3752 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
3753 else if (lpfc_cmd->status >= IOSTAT_CNT)
3754 lpfc_cmd->status = IOSTAT_DEFAULT;
James Smartaa1c7ee2012-08-14 14:26:06 -04003755 if (lpfc_cmd->status == IOSTAT_FCP_RSP_ERROR &&
3756 !lpfc_cmd->fcp_rsp->rspStatus3 &&
3757 (lpfc_cmd->fcp_rsp->rspStatus2 & RESID_UNDER) &&
3758 !(vport->cfg_log_verbose & LOG_FCP_UNDER))
James Smart73d91e52011-10-10 21:32:10 -04003759 logit = 0;
3760 else
3761 logit = LOG_FCP | LOG_FCP_UNDER;
3762 lpfc_printf_vlog(vport, KERN_WARNING, logit,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02003763 "9030 FCP cmd x%x failed <%d/%lld> "
James Smart5a0d80f2012-05-09 21:18:20 -04003764 "status: x%x result: x%x "
3765 "sid: x%x did: x%x oxid: x%x "
3766 "Data: x%x x%x\n",
James Smart73d91e52011-10-10 21:32:10 -04003767 cmd->cmnd[0],
3768 cmd->device ? cmd->device->id : 0xffff,
3769 cmd->device ? cmd->device->lun : 0xffff,
3770 lpfc_cmd->status, lpfc_cmd->result,
James Smart3bf41ba2013-05-31 17:03:18 -04003771 vport->fc_myDID,
3772 (pnode) ? pnode->nlp_DID : 0,
James Smart5a0d80f2012-05-09 21:18:20 -04003773 phba->sli_rev == LPFC_SLI_REV4 ?
3774 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
James Smart73d91e52011-10-10 21:32:10 -04003775 pIocbOut->iocb.ulpContext,
3776 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
dea31012005-04-17 16:05:31 -05003777
3778 switch (lpfc_cmd->status) {
3779 case IOSTAT_FCP_RSP_ERROR:
3780 /* Call FCP RSP handler to determine result */
James Smart2e0fef82007-06-17 19:56:36 -05003781 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea31012005-04-17 16:05:31 -05003782 break;
3783 case IOSTAT_NPORT_BSY:
3784 case IOSTAT_FABRIC_BSY:
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02003785 cmd->result = DID_TRANSPORT_DISRUPTED << 16;
James Smartea2151b2008-09-07 11:52:10 -04003786 fast_path_evt = lpfc_alloc_fast_evt(phba);
3787 if (!fast_path_evt)
3788 break;
3789 fast_path_evt->un.fabric_evt.event_type =
3790 FC_REG_FABRIC_EVENT;
3791 fast_path_evt->un.fabric_evt.subcategory =
3792 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
3793 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
3794 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
3795 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
3796 &pnode->nlp_portname,
3797 sizeof(struct lpfc_name));
3798 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
3799 &pnode->nlp_nodename,
3800 sizeof(struct lpfc_name));
3801 }
3802 fast_path_evt->vport = vport;
3803 fast_path_evt->work_evt.evt =
3804 LPFC_EVT_FASTPATH_MGMT_EVT;
3805 spin_lock_irqsave(&phba->hbalock, flags);
3806 list_add_tail(&fast_path_evt->work_evt.evt_listp,
3807 &phba->work_list);
3808 spin_unlock_irqrestore(&phba->hbalock, flags);
3809 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05003810 break;
James Smart92d7f7b2007-06-17 19:56:38 -05003811 case IOSTAT_LOCAL_REJECT:
James Smart1151e3e2011-02-16 12:39:35 -05003812 case IOSTAT_REMOTE_STOP:
James Smartab56dc22011-02-16 12:39:57 -05003813 if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
3814 lpfc_cmd->result ==
3815 IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
3816 lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
3817 lpfc_cmd->result ==
3818 IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02003819 cmd->result = DID_NO_CONNECT << 16;
James Smartab56dc22011-02-16 12:39:57 -05003820 break;
3821 }
James Smartd7c255b2008-08-24 21:50:00 -04003822 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
James Smart92d7f7b2007-06-17 19:56:38 -05003823 lpfc_cmd->result == IOERR_NO_RESOURCES ||
James Smartb92938b2010-06-07 15:24:12 -04003824 lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
3825 lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02003826 cmd->result = DID_REQUEUE << 16;
James Smart58da1ff2008-04-07 10:15:56 -04003827 break;
James Smarte2a0a9d2008-12-04 22:40:02 -05003828 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003829 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
3830 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
3831 pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
3832 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
3833 /*
3834 * This is a response for a BG enabled
3835 * cmd. Parse BG error
3836 */
3837 lpfc_parse_bg_err(phba, lpfc_cmd,
3838 pIocbOut);
3839 break;
3840 } else {
3841 lpfc_printf_vlog(vport, KERN_WARNING,
3842 LOG_BG,
3843 "9031 non-zero BGSTAT "
James Smart6a9c52c2009-10-02 15:16:51 -04003844 "on unprotected cmd\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05003845 }
3846 }
James Smart1151e3e2011-02-16 12:39:35 -05003847 if ((lpfc_cmd->status == IOSTAT_REMOTE_STOP)
3848 && (phba->sli_rev == LPFC_SLI_REV4)
3849 && (pnode && NLP_CHK_NODE_ACT(pnode))) {
3850 /* This IO was aborted by the target, we don't
3851 * know the rxid and because we did not send the
3852 * ABTS we cannot generate and RRQ.
3853 */
3854 lpfc_set_rrq_active(phba, pnode,
James Smartee0f4fe2012-05-09 21:19:14 -04003855 lpfc_cmd->cur_iocbq.sli4_lxritag,
3856 0, 0);
James Smart1151e3e2011-02-16 12:39:35 -05003857 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003858 /* else: fall through */
dea31012005-04-17 16:05:31 -05003859 default:
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02003860 cmd->result = DID_ERROR << 16;
dea31012005-04-17 16:05:31 -05003861 break;
3862 }
3863
James Smart58da1ff2008-04-07 10:15:56 -04003864 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003865 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02003866 cmd->result = DID_TRANSPORT_DISRUPTED << 16 |
3867 SAM_STAT_BUSY;
James Smartab56dc22011-02-16 12:39:57 -05003868 } else
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02003869 cmd->result = DID_OK << 16;
dea31012005-04-17 16:05:31 -05003870
3871 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
3872 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
3873
James Smarte8b62012007-08-02 11:10:09 -04003874 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02003875 "0710 Iodone <%d/%llu> cmd %p, error "
James Smarte8b62012007-08-02 11:10:09 -04003876 "x%x SNS x%x x%x Data: x%x x%x\n",
3877 cmd->device->id, cmd->device->lun, cmd,
3878 cmd->result, *lp, *(lp + 3), cmd->retries,
3879 scsi_get_resid(cmd));
dea31012005-04-17 16:05:31 -05003880 }
3881
James Smartea2151b2008-09-07 11:52:10 -04003882 lpfc_update_stats(phba, lpfc_cmd);
James Smart977b5a02008-09-07 11:52:04 -04003883 if (vport->cfg_max_scsicmpl_time &&
3884 time_after(jiffies, lpfc_cmd->start_time +
3885 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
James Smarta257bf92009-04-06 18:48:10 -04003886 spin_lock_irqsave(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05003887 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
3888 if (pnode->cmd_qdepth >
3889 atomic_read(&pnode->cmd_pending) &&
3890 (atomic_read(&pnode->cmd_pending) >
3891 LPFC_MIN_TGT_QDEPTH) &&
3892 ((cmd->cmnd[0] == READ_10) ||
3893 (cmd->cmnd[0] == WRITE_10)))
3894 pnode->cmd_qdepth =
3895 atomic_read(&pnode->cmd_pending);
James Smart977b5a02008-09-07 11:52:04 -04003896
James Smart109f6ed2008-12-04 22:39:08 -05003897 pnode->last_change_time = jiffies;
3898 }
James Smarta257bf92009-04-06 18:48:10 -04003899 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart977b5a02008-09-07 11:52:04 -04003900 }
James Smart1dcb58e2007-04-25 09:51:30 -04003901 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James Smarta257bf92009-04-06 18:48:10 -04003902
James Smartca7fb762018-09-10 10:30:44 -07003903 /* If pCmd was set to NULL from abort path, do not call scsi_done */
3904 if (xchg(&lpfc_cmd->pCmd, NULL) == NULL) {
3905 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smart2c4c9142018-11-29 16:09:31 -08003906 "5688 FCP cmd already NULL, sid: 0x%06x, "
James Smartca7fb762018-09-10 10:30:44 -07003907 "did: 0x%06x, oxid: 0x%04x\n",
3908 vport->fc_myDID,
3909 (pnode) ? pnode->nlp_DID : 0,
3910 phba->sli_rev == LPFC_SLI_REV4 ?
3911 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff);
3912 return;
3913 }
James Smart92e3af62012-08-14 14:26:28 -04003914
James Smart89533e92016-10-13 15:06:15 -07003915 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
3916 cmd->scsi_done(cmd);
3917
James Smartfa61a542008-01-11 01:52:42 -05003918 /*
3919 * If there is a thread waiting for command completion
3920 * wake up the thread.
3921 */
James Smarta257bf92009-04-06 18:48:10 -04003922 spin_lock_irqsave(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05003923 if (lpfc_cmd->waitq)
3924 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04003925 spin_unlock_irqrestore(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05003926
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003927 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05003928}
3929
James Smart34b02dc2008-08-24 21:49:55 -04003930/**
Alexey Kardashevskiy8b2564e2015-04-28 18:26:22 +10003931 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
3932 * @data: A pointer to the immediate command data portion of the IOCB.
3933 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
3934 *
3935 * The routine copies the entire FCP command from @fcp_cmnd to @data while
3936 * byte swapping the data to big endian format for transmission on the wire.
3937 **/
3938static void
3939lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
3940{
3941 int i, j;
3942 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
3943 i += sizeof(uint32_t), j++) {
3944 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
3945 }
3946}
3947
3948/**
James Smartf1126682009-06-10 17:22:44 -04003949 * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05003950 * @vport: The virtual port for which this call is being executed.
3951 * @lpfc_cmd: The scsi command which needs to send.
3952 * @pnode: Pointer to lpfc_nodelist.
3953 *
3954 * This routine initializes fcp_cmnd and iocb data structure from scsi command
James Smart3772a992009-05-22 14:50:54 -04003955 * to transfer for device with SLI3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05003956 **/
dea31012005-04-17 16:05:31 -05003957static void
James Smartf1126682009-06-10 17:22:44 -04003958lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
James Smart2e0fef82007-06-17 19:56:36 -05003959 struct lpfc_nodelist *pnode)
dea31012005-04-17 16:05:31 -05003960{
James Smart2e0fef82007-06-17 19:56:36 -05003961 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003962 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3963 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3964 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
3965 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
3966 int datadir = scsi_cmnd->sc_data_direction;
James Smart027140e2012-08-03 12:35:44 -04003967 uint8_t *ptr;
3968 bool sli4;
James Smart98bbf5f2013-09-06 12:18:45 -04003969 uint32_t fcpdl;
dea31012005-04-17 16:05:31 -05003970
James Smart58da1ff2008-04-07 10:15:56 -04003971 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3972 return;
3973
dea31012005-04-17 16:05:31 -05003974 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
James.Smart@Emulex.Com69859dc2005-08-10 15:02:37 -04003975 /* clear task management bits */
3976 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea31012005-04-17 16:05:31 -05003977
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -04003978 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
3979 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05003980
James Smart027140e2012-08-03 12:35:44 -04003981 ptr = &fcp_cmnd->fcpCdb[0];
3982 memcpy(ptr, scsi_cmnd->cmnd, scsi_cmnd->cmd_len);
3983 if (scsi_cmnd->cmd_len < LPFC_FCP_CDB_LEN) {
3984 ptr += scsi_cmnd->cmd_len;
3985 memset(ptr, 0, (LPFC_FCP_CDB_LEN - scsi_cmnd->cmd_len));
3986 }
3987
Christoph Hellwig50668632014-10-30 14:30:06 +01003988 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
dea31012005-04-17 16:05:31 -05003989
James Smart027140e2012-08-03 12:35:44 -04003990 sli4 = (phba->sli_rev == LPFC_SLI_REV4);
James Smart6acb3482014-04-04 13:51:25 -04003991 piocbq->iocb.un.fcpi.fcpi_XRdy = 0;
James Smart027140e2012-08-03 12:35:44 -04003992
dea31012005-04-17 16:05:31 -05003993 /*
3994 * There are three possibilities here - use scatter-gather segment, use
3995 * the single mapping, or neither. Start the lpfc command prep by
3996 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
3997 * data bde entry.
3998 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003999 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05004000 if (datadir == DMA_TO_DEVICE) {
4001 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
James Smart182ba752013-07-15 18:34:05 -04004002 iocb_cmd->ulpPU = PARM_READ_CHECK;
James Smart3cb01c52013-07-15 18:35:04 -04004003 if (vport->cfg_first_burst_size &&
4004 (pnode->nlp_flag & NLP_FIRSTBURST)) {
James Smart98bbf5f2013-09-06 12:18:45 -04004005 fcpdl = scsi_bufflen(scsi_cmnd);
4006 if (fcpdl < vport->cfg_first_burst_size)
4007 piocbq->iocb.un.fcpi.fcpi_XRdy = fcpdl;
4008 else
4009 piocbq->iocb.un.fcpi.fcpi_XRdy =
4010 vport->cfg_first_burst_size;
James Smart3cb01c52013-07-15 18:35:04 -04004011 }
dea31012005-04-17 16:05:31 -05004012 fcp_cmnd->fcpCntl3 = WRITE_DATA;
James Smart2cee7802017-06-01 21:07:02 -07004013 atomic_inc(&phba->fc4ScsiOutputRequests);
dea31012005-04-17 16:05:31 -05004014 } else {
4015 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
4016 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05004017 fcp_cmnd->fcpCntl3 = READ_DATA;
James Smart2cee7802017-06-01 21:07:02 -07004018 atomic_inc(&phba->fc4ScsiInputRequests);
dea31012005-04-17 16:05:31 -05004019 }
4020 } else {
4021 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
4022 iocb_cmd->un.fcpi.fcpi_parm = 0;
4023 iocb_cmd->ulpPU = 0;
4024 fcp_cmnd->fcpCntl3 = 0;
James Smart2cee7802017-06-01 21:07:02 -07004025 atomic_inc(&phba->fc4ScsiControlRequests);
dea31012005-04-17 16:05:31 -05004026 }
Alexey Kardashevskiy8b2564e2015-04-28 18:26:22 +10004027 if (phba->sli_rev == 3 &&
4028 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
4029 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05004030 /*
4031 * Finish initializing those IOCB fields that are independent
4032 * of the scsi_cmnd request_buffer
4033 */
4034 piocbq->iocb.ulpContext = pnode->nlp_rpi;
James Smart027140e2012-08-03 12:35:44 -04004035 if (sli4)
James Smart6d368e52011-05-24 11:44:12 -04004036 piocbq->iocb.ulpContext =
4037 phba->sli4_hba.rpi_ids[pnode->nlp_rpi];
dea31012005-04-17 16:05:31 -05004038 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
4039 piocbq->iocb.ulpFCP2Rcvy = 1;
James Smart09372822008-01-11 01:52:54 -05004040 else
4041 piocbq->iocb.ulpFCP2Rcvy = 0;
dea31012005-04-17 16:05:31 -05004042
4043 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
4044 piocbq->context1 = lpfc_cmd;
4045 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
4046 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
James Smart2e0fef82007-06-17 19:56:36 -05004047 piocbq->vport = vport;
dea31012005-04-17 16:05:31 -05004048}
4049
James Smart9bad7672008-12-04 22:39:02 -05004050/**
James Smart6d368e52011-05-24 11:44:12 -04004051 * lpfc_scsi_prep_task_mgmt_cmd - Convert SLI3 scsi TM cmd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05004052 * @vport: The virtual port for which this call is being executed.
4053 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
4054 * @lun: Logical unit number.
4055 * @task_mgmt_cmd: SCSI task management command.
4056 *
James Smart3772a992009-05-22 14:50:54 -04004057 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
4058 * for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05004059 *
4060 * Return codes:
4061 * 0 - Error
4062 * 1 - Success
4063 **/
dea31012005-04-17 16:05:31 -05004064static int
James Smartf1126682009-06-10 17:22:44 -04004065lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
dea31012005-04-17 16:05:31 -05004066 struct lpfc_scsi_buf *lpfc_cmd,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004067 uint64_t lun,
dea31012005-04-17 16:05:31 -05004068 uint8_t task_mgmt_cmd)
4069{
dea31012005-04-17 16:05:31 -05004070 struct lpfc_iocbq *piocbq;
4071 IOCB_t *piocb;
4072 struct fcp_cmnd *fcp_cmnd;
James Smart0b18ac42006-05-01 21:50:40 -04004073 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea31012005-04-17 16:05:31 -05004074 struct lpfc_nodelist *ndlp = rdata->pnode;
4075
James Smart58da1ff2008-04-07 10:15:56 -04004076 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
4077 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05004078 return 0;
dea31012005-04-17 16:05:31 -05004079
dea31012005-04-17 16:05:31 -05004080 piocbq = &(lpfc_cmd->cur_iocbq);
James Smart2e0fef82007-06-17 19:56:36 -05004081 piocbq->vport = vport;
4082
dea31012005-04-17 16:05:31 -05004083 piocb = &piocbq->iocb;
4084
4085 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart34b02dc2008-08-24 21:49:55 -04004086 /* Clear out any old data in the FCP command area */
4087 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
4088 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05004089 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
James Smarte2a0a9d2008-12-04 22:40:02 -05004090 if (vport->phba->sli_rev == 3 &&
4091 !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04004092 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05004093 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
dea31012005-04-17 16:05:31 -05004094 piocb->ulpContext = ndlp->nlp_rpi;
James Smart6d368e52011-05-24 11:44:12 -04004095 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
4096 piocb->ulpContext =
4097 vport->phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
4098 }
James Smart53151bb2013-10-10 12:24:07 -04004099 piocb->ulpFCP2Rcvy = (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) ? 1 : 0;
dea31012005-04-17 16:05:31 -05004100 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
James Smartf9226c22014-02-20 09:57:28 -05004101 piocb->ulpPU = 0;
4102 piocb->un.fcpi.fcpi_parm = 0;
dea31012005-04-17 16:05:31 -05004103
4104 /* ulpTimeout is only one byte */
4105 if (lpfc_cmd->timeout > 0xff) {
4106 /*
4107 * Do not timeout the command at the firmware level.
4108 * The driver will provide the timeout mechanism.
4109 */
4110 piocb->ulpTimeout = 0;
James Smartf1126682009-06-10 17:22:44 -04004111 } else
dea31012005-04-17 16:05:31 -05004112 piocb->ulpTimeout = lpfc_cmd->timeout;
James Smartf1126682009-06-10 17:22:44 -04004113
4114 if (vport->phba->sli_rev == LPFC_SLI_REV4)
4115 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004116
James Smart2e0fef82007-06-17 19:56:36 -05004117 return 1;
dea31012005-04-17 16:05:31 -05004118}
4119
James Smart9bad7672008-12-04 22:39:02 -05004120/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004121 * lpfc_scsi_api_table_setup - Set up scsi api function jump table
James Smart3772a992009-05-22 14:50:54 -04004122 * @phba: The hba struct for which this call is being executed.
4123 * @dev_grp: The HBA PCI-Device group number.
4124 *
4125 * This routine sets up the SCSI interface API function jump table in @phba
4126 * struct.
4127 * Returns: 0 - success, -ENODEV - failure.
4128 **/
4129int
4130lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4131{
4132
James Smartf1126682009-06-10 17:22:44 -04004133 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
4134 phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
James Smartf1126682009-06-10 17:22:44 -04004135
James Smart3772a992009-05-22 14:50:54 -04004136 switch (dev_grp) {
4137 case LPFC_PCI_DEV_LP:
James Smart3772a992009-05-22 14:50:54 -04004138 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
James Smartacd68592012-01-18 16:25:09 -05004139 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04004140 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
James Smart19ca7602010-11-20 23:11:55 -05004141 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04004142 break;
James Smartda0436e2009-05-22 14:51:39 -04004143 case LPFC_PCI_DEV_OC:
James Smartda0436e2009-05-22 14:51:39 -04004144 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
James Smartacd68592012-01-18 16:25:09 -05004145 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04004146 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
James Smart19ca7602010-11-20 23:11:55 -05004147 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04004148 break;
James Smart3772a992009-05-22 14:50:54 -04004149 default:
4150 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4151 "1418 Invalid HBA PCI-device group: 0x%x\n",
4152 dev_grp);
4153 return -ENODEV;
4154 break;
4155 }
James Smart3772a992009-05-22 14:50:54 -04004156 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
James Smart84d1b002010-02-12 14:42:33 -05004157 phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
James Smart3772a992009-05-22 14:50:54 -04004158 return 0;
4159}
4160
4161/**
James Smart3621a712009-04-06 18:47:14 -04004162 * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
James Smart9bad7672008-12-04 22:39:02 -05004163 * @phba: The Hba for which this call is being executed.
4164 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
4165 * @rspiocbq: Pointer to lpfc_iocbq data structure.
4166 *
4167 * This routine is IOCB completion routine for device reset and target reset
4168 * routine. This routine release scsi buffer associated with lpfc_cmd.
4169 **/
James Smart7054a602007-04-25 09:52:34 -04004170static void
4171lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
4172 struct lpfc_iocbq *cmdiocbq,
4173 struct lpfc_iocbq *rspiocbq)
4174{
4175 struct lpfc_scsi_buf *lpfc_cmd =
4176 (struct lpfc_scsi_buf *) cmdiocbq->context1;
4177 if (lpfc_cmd)
4178 lpfc_release_scsi_buf(phba, lpfc_cmd);
4179 return;
4180}
4181
James Smart9bad7672008-12-04 22:39:02 -05004182/**
James Smart50212672018-12-13 15:17:57 -08004183 * lpfc_check_pci_resettable - Walks list of devices on pci_dev's bus to check
4184 * if issuing a pci_bus_reset is possibly unsafe
4185 * @phba: lpfc_hba pointer.
4186 *
4187 * Description:
4188 * Walks the bus_list to ensure only PCI devices with Emulex
4189 * vendor id, device ids that support hot reset, and only one occurrence
4190 * of function 0.
4191 *
4192 * Returns:
4193 * -EBADSLT, detected invalid device
4194 * 0, successful
4195 */
4196int
4197lpfc_check_pci_resettable(const struct lpfc_hba *phba)
4198{
4199 const struct pci_dev *pdev = phba->pcidev;
4200 struct pci_dev *ptr = NULL;
4201 u8 counter = 0;
4202
4203 /* Walk the list of devices on the pci_dev's bus */
4204 list_for_each_entry(ptr, &pdev->bus->devices, bus_list) {
4205 /* Check for Emulex Vendor ID */
4206 if (ptr->vendor != PCI_VENDOR_ID_EMULEX) {
4207 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4208 "8346 Non-Emulex vendor found: "
4209 "0x%04x\n", ptr->vendor);
4210 return -EBADSLT;
4211 }
4212
4213 /* Check for valid Emulex Device ID */
4214 switch (ptr->device) {
4215 case PCI_DEVICE_ID_LANCER_FC:
4216 case PCI_DEVICE_ID_LANCER_G6_FC:
4217 case PCI_DEVICE_ID_LANCER_G7_FC:
4218 break;
4219 default:
4220 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4221 "8347 Invalid device found: "
4222 "0x%04x\n", ptr->device);
4223 return -EBADSLT;
4224 }
4225
4226 /* Check for only one function 0 ID to ensure only one HBA on
4227 * secondary bus
4228 */
4229 if (ptr->devfn == 0) {
4230 if (++counter > 1) {
4231 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4232 "8348 More than one device on "
4233 "secondary bus found\n");
4234 return -EBADSLT;
4235 }
4236 }
4237 }
4238
4239 return 0;
4240}
4241
4242/**
James Smart3621a712009-04-06 18:47:14 -04004243 * lpfc_info - Info entry point of scsi_host_template data structure
James Smart9bad7672008-12-04 22:39:02 -05004244 * @host: The scsi host for which this call is being executed.
4245 *
4246 * This routine provides module information about hba.
4247 *
4248 * Reutrn code:
4249 * Pointer to char - Success.
4250 **/
dea31012005-04-17 16:05:31 -05004251const char *
4252lpfc_info(struct Scsi_Host *host)
4253{
James Smart2e0fef82007-06-17 19:56:36 -05004254 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
4255 struct lpfc_hba *phba = vport->phba;
James Smart50212672018-12-13 15:17:57 -08004256 int link_speed = 0;
4257 static char lpfcinfobuf[384];
4258 char tmp[384] = {0};
dea31012005-04-17 16:05:31 -05004259
James Smart50212672018-12-13 15:17:57 -08004260 memset(lpfcinfobuf, 0, sizeof(lpfcinfobuf));
dea31012005-04-17 16:05:31 -05004261 if (phba && phba->pcidev){
James Smart50212672018-12-13 15:17:57 -08004262 /* Model Description */
4263 scnprintf(tmp, sizeof(tmp), phba->ModelDesc);
4264 if (strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf)) >=
4265 sizeof(lpfcinfobuf))
4266 goto buffer_done;
4267
4268 /* PCI Info */
4269 scnprintf(tmp, sizeof(tmp),
4270 " on PCI bus %02x device %02x irq %d",
4271 phba->pcidev->bus->number, phba->pcidev->devfn,
4272 phba->pcidev->irq);
4273 if (strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf)) >=
4274 sizeof(lpfcinfobuf))
4275 goto buffer_done;
4276
4277 /* Port Number */
dea31012005-04-17 16:05:31 -05004278 if (phba->Port[0]) {
James Smart50212672018-12-13 15:17:57 -08004279 scnprintf(tmp, sizeof(tmp), " port %s", phba->Port);
4280 if (strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf)) >=
4281 sizeof(lpfcinfobuf))
4282 goto buffer_done;
dea31012005-04-17 16:05:31 -05004283 }
James Smart50212672018-12-13 15:17:57 -08004284
4285 /* Link Speed */
James Smarta085e872015-12-16 18:12:02 -05004286 link_speed = lpfc_sli_port_speed_get(phba);
James Smart50212672018-12-13 15:17:57 -08004287 if (link_speed != 0) {
4288 scnprintf(tmp, sizeof(tmp),
4289 " Logical Link Speed: %d Mbps", link_speed);
4290 if (strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf)) >=
4291 sizeof(lpfcinfobuf))
4292 goto buffer_done;
4293 }
4294
4295 /* PCI resettable */
4296 if (!lpfc_check_pci_resettable(phba)) {
4297 scnprintf(tmp, sizeof(tmp), " PCI resettable");
4298 strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf));
4299 }
dea31012005-04-17 16:05:31 -05004300 }
James Smart50212672018-12-13 15:17:57 -08004301
4302buffer_done:
dea31012005-04-17 16:05:31 -05004303 return lpfcinfobuf;
4304}
4305
James Smart9bad7672008-12-04 22:39:02 -05004306/**
James Smart3621a712009-04-06 18:47:14 -04004307 * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
James Smart9bad7672008-12-04 22:39:02 -05004308 * @phba: The Hba for which this call is being executed.
4309 *
4310 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
4311 * The default value of cfg_poll_tmo is 10 milliseconds.
4312 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004313static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
4314{
4315 unsigned long poll_tmo_expires =
4316 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
4317
James Smart895427b2017-02-12 13:52:30 -08004318 if (!list_empty(&phba->sli.sli3_ring[LPFC_FCP_RING].txcmplq))
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004319 mod_timer(&phba->fcp_poll_timer,
4320 poll_tmo_expires);
4321}
4322
James Smart9bad7672008-12-04 22:39:02 -05004323/**
James Smart3621a712009-04-06 18:47:14 -04004324 * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
James Smart9bad7672008-12-04 22:39:02 -05004325 * @phba: The Hba for which this call is being executed.
4326 *
4327 * This routine starts the fcp_poll_timer of @phba.
4328 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004329void lpfc_poll_start_timer(struct lpfc_hba * phba)
4330{
4331 lpfc_poll_rearm_timer(phba);
4332}
4333
James Smart9bad7672008-12-04 22:39:02 -05004334/**
James Smart3621a712009-04-06 18:47:14 -04004335 * lpfc_poll_timeout - Restart polling timer
James Smart9bad7672008-12-04 22:39:02 -05004336 * @ptr: Map to lpfc_hba data structure pointer.
4337 *
4338 * This routine restarts fcp_poll timer, when FCP ring polling is enable
4339 * and FCP Ring interrupt is disable.
4340 **/
4341
Kees Cookf22eb4d2017-09-06 20:24:26 -07004342void lpfc_poll_timeout(struct timer_list *t)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004343{
Kees Cookf22eb4d2017-09-06 20:24:26 -07004344 struct lpfc_hba *phba = from_timer(phba, t, fcp_poll_timer);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004345
4346 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004347 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -08004348 &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
James Smart45ed1192009-10-02 15:17:02 -04004349
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004350 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4351 lpfc_poll_rearm_timer(phba);
4352 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004353}
4354
James Smart9bad7672008-12-04 22:39:02 -05004355/**
James Smart3621a712009-04-06 18:47:14 -04004356 * lpfc_queuecommand - scsi_host_template queuecommand entry point
James Smart9bad7672008-12-04 22:39:02 -05004357 * @cmnd: Pointer to scsi_cmnd data structure.
4358 * @done: Pointer to done routine.
4359 *
4360 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
4361 * This routine prepares an IOCB from scsi command and provides to firmware.
4362 * The @done callback is invoked after driver finished processing the command.
4363 *
4364 * Return value :
4365 * 0 - Success
4366 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
4367 **/
dea31012005-04-17 16:05:31 -05004368static int
James Smartb9a7c632012-08-03 12:35:24 -04004369lpfc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004370{
James Smart2e0fef82007-06-17 19:56:36 -05004371 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4372 struct lpfc_hba *phba = vport->phba;
James Smart1ba981f2014-02-20 09:56:45 -05004373 struct lpfc_rport_data *rdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05004374 struct lpfc_nodelist *ndlp;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004375 struct lpfc_scsi_buf *lpfc_cmd;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004376 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004377 int err;
dea31012005-04-17 16:05:31 -05004378
James Smart1ba981f2014-02-20 09:56:45 -05004379 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smartb0e83012018-06-26 08:24:29 -07004380
4381 /* sanity check on references */
4382 if (unlikely(!rdata) || unlikely(!rport))
4383 goto out_fail_command;
4384
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004385 err = fc_remote_port_chkready(rport);
4386 if (err) {
4387 cmnd->result = err;
dea31012005-04-17 16:05:31 -05004388 goto out_fail_command;
4389 }
James Smart1c6f4ef52009-11-18 15:40:49 -05004390 ndlp = rdata->pnode;
dea31012005-04-17 16:05:31 -05004391
James Smartbf086112011-08-21 21:48:13 -04004392 if ((scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) &&
James Smartacd68592012-01-18 16:25:09 -05004393 (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED))) {
James Smarte2a0a9d2008-12-04 22:40:02 -05004394
James Smart6a9c52c2009-10-02 15:16:51 -04004395 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4396 "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
4397 " op:%02x str=%s without registering for"
4398 " BlockGuard - Rejecting command\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05004399 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
4400 dif_op_str[scsi_get_prot_op(cmnd)]);
4401 goto out_fail_command;
4402 }
4403
dea31012005-04-17 16:05:31 -05004404 /*
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004405 * Catch race where our node has transitioned, but the
4406 * transport is still transitioning.
dea31012005-04-17 16:05:31 -05004407 */
James Smart6b415f52012-06-12 13:54:59 -04004408 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
4409 goto out_tgt_busy;
James Smart2a5b7d62018-07-31 17:23:22 -07004410 if (lpfc_ndlp_check_qdepth(phba, ndlp)) {
4411 if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth) {
4412 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_ERROR,
4413 "3377 Target Queue Full, scsi Id:%d "
4414 "Qdepth:%d Pending command:%d"
4415 " WWNN:%02x:%02x:%02x:%02x:"
4416 "%02x:%02x:%02x:%02x, "
4417 " WWPN:%02x:%02x:%02x:%02x:"
4418 "%02x:%02x:%02x:%02x",
4419 ndlp->nlp_sid, ndlp->cmd_qdepth,
4420 atomic_read(&ndlp->cmd_pending),
4421 ndlp->nlp_nodename.u.wwn[0],
4422 ndlp->nlp_nodename.u.wwn[1],
4423 ndlp->nlp_nodename.u.wwn[2],
4424 ndlp->nlp_nodename.u.wwn[3],
4425 ndlp->nlp_nodename.u.wwn[4],
4426 ndlp->nlp_nodename.u.wwn[5],
4427 ndlp->nlp_nodename.u.wwn[6],
4428 ndlp->nlp_nodename.u.wwn[7],
4429 ndlp->nlp_portname.u.wwn[0],
4430 ndlp->nlp_portname.u.wwn[1],
4431 ndlp->nlp_portname.u.wwn[2],
4432 ndlp->nlp_portname.u.wwn[3],
4433 ndlp->nlp_portname.u.wwn[4],
4434 ndlp->nlp_portname.u.wwn[5],
4435 ndlp->nlp_portname.u.wwn[6],
4436 ndlp->nlp_portname.u.wwn[7]);
4437 goto out_tgt_busy;
4438 }
James Smart64bf0092018-01-30 15:58:53 -08004439 }
James Smartf91bc592018-04-09 14:24:22 -07004440
James Smart19ca7602010-11-20 23:11:55 -05004441 lpfc_cmd = lpfc_get_scsi_buf(phba, ndlp);
dea31012005-04-17 16:05:31 -05004442 if (lpfc_cmd == NULL) {
James Smarteaf15d52008-12-04 22:39:29 -05004443 lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05004444
James Smart895427b2017-02-12 13:52:30 -08004445 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_ERROR,
James Smarte8b62012007-08-02 11:10:09 -04004446 "0707 driver's buffer pool is empty, "
4447 "IO busied\n");
dea31012005-04-17 16:05:31 -05004448 goto out_host_busy;
4449 }
4450
4451 /*
4452 * Store the midlayer's command structure for the completion phase
4453 * and complete the command initialization.
4454 */
4455 lpfc_cmd->pCmd = cmnd;
4456 lpfc_cmd->rdata = rdata;
James Smart2a5b7d62018-07-31 17:23:22 -07004457 lpfc_cmd->ndlp = ndlp;
dea31012005-04-17 16:05:31 -05004458 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
dea31012005-04-17 16:05:31 -05004459
James Smarte2a0a9d2008-12-04 22:40:02 -05004460 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
James Smart6a9c52c2009-10-02 15:16:51 -04004461 if (vport->phba->cfg_enable_bg) {
James Smart737d4242013-04-17 20:14:49 -04004462 lpfc_printf_vlog(vport,
4463 KERN_INFO, LOG_SCSI_CMD,
James Smart26134702012-08-14 14:25:50 -04004464 "9033 BLKGRD: rcvd %s cmd:x%x "
4465 "sector x%llx cnt %u pt %x\n",
4466 dif_op_str[scsi_get_prot_op(cmnd)],
4467 cmnd->cmnd[0],
4468 (unsigned long long)scsi_get_lba(cmnd),
4469 blk_rq_sectors(cmnd->request),
4470 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004471 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004472 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
4473 } else {
James Smart6a9c52c2009-10-02 15:16:51 -04004474 if (vport->phba->cfg_enable_bg) {
James Smart737d4242013-04-17 20:14:49 -04004475 lpfc_printf_vlog(vport,
4476 KERN_INFO, LOG_SCSI_CMD,
James Smart26134702012-08-14 14:25:50 -04004477 "9038 BLKGRD: rcvd PROT_NORMAL cmd: "
4478 "x%x sector x%llx cnt %u pt %x\n",
4479 cmnd->cmnd[0],
4480 (unsigned long long)scsi_get_lba(cmnd),
James Smart9a6b09c2012-03-01 22:37:42 -05004481 blk_rq_sectors(cmnd->request),
James Smart26134702012-08-14 14:25:50 -04004482 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004483 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004484 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
4485 }
4486
dea31012005-04-17 16:05:31 -05004487 if (err)
4488 goto out_host_busy_free_buf;
4489
James Smart2e0fef82007-06-17 19:56:36 -05004490 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea31012005-04-17 16:05:31 -05004491
James Smart3772a992009-05-22 14:50:54 -04004492 err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
James Smart92d7f7b2007-06-17 19:56:38 -05004493 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
James Smarteaf15d52008-12-04 22:39:29 -05004494 if (err) {
James Smart76f96b62013-12-17 20:29:01 -05004495 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4496 "3376 FCP could not issue IOCB err %x"
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004497 "FCP cmd x%x <%d/%llu> "
James Smart76f96b62013-12-17 20:29:01 -05004498 "sid: x%x did: x%x oxid: x%x "
4499 "Data: x%x x%x x%x x%x\n",
4500 err, cmnd->cmnd[0],
4501 cmnd->device ? cmnd->device->id : 0xffff,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004502 cmnd->device ? cmnd->device->lun : (u64) -1,
James Smart76f96b62013-12-17 20:29:01 -05004503 vport->fc_myDID, ndlp->nlp_DID,
4504 phba->sli_rev == LPFC_SLI_REV4 ?
4505 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
4506 lpfc_cmd->cur_iocbq.iocb.ulpContext,
4507 lpfc_cmd->cur_iocbq.iocb.ulpIoTag,
4508 lpfc_cmd->cur_iocbq.iocb.ulpTimeout,
4509 (uint32_t)
4510 (cmnd->request->timeout / 1000));
4511
James Smart2cee7802017-06-01 21:07:02 -07004512 switch (lpfc_cmd->fcp_cmnd->fcpCntl3) {
4513 case WRITE_DATA:
4514 atomic_dec(&phba->fc4ScsiOutputRequests);
4515 break;
4516 case READ_DATA:
4517 atomic_dec(&phba->fc4ScsiInputRequests);
4518 break;
4519 default:
4520 atomic_dec(&phba->fc4ScsiControlRequests);
4521 }
dea31012005-04-17 16:05:31 -05004522 goto out_host_busy_free_buf;
James Smarteaf15d52008-12-04 22:39:29 -05004523 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004524 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004525 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -08004526 &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
James Smart45ed1192009-10-02 15:17:02 -04004527
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004528 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4529 lpfc_poll_rearm_timer(phba);
4530 }
4531
dea31012005-04-17 16:05:31 -05004532 return 0;
4533
4534 out_host_busy_free_buf:
James Smartbcf4dbf2006-07-06 15:50:08 -04004535 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004536 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004537 out_host_busy:
4538 return SCSI_MLQUEUE_HOST_BUSY;
4539
Mike Christie34963432011-02-25 14:04:28 -06004540 out_tgt_busy:
4541 return SCSI_MLQUEUE_TARGET_BUSY;
4542
dea31012005-04-17 16:05:31 -05004543 out_fail_command:
James Smartb9a7c632012-08-03 12:35:24 -04004544 cmnd->scsi_done(cmnd);
dea31012005-04-17 16:05:31 -05004545 return 0;
4546}
4547
Jeff Garzikf2812332010-11-16 02:10:29 -05004548
James Smart9bad7672008-12-04 22:39:02 -05004549/**
James Smart3621a712009-04-06 18:47:14 -04004550 * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05004551 * @cmnd: Pointer to scsi_cmnd data structure.
4552 *
4553 * This routine aborts @cmnd pending in base driver.
4554 *
4555 * Return code :
4556 * 0x2003 - Error
4557 * 0x2002 - Success
4558 **/
dea31012005-04-17 16:05:31 -05004559static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05004560lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004561{
James Smart2e0fef82007-06-17 19:56:36 -05004562 struct Scsi_Host *shost = cmnd->device->host;
4563 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4564 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004565 struct lpfc_iocbq *iocb;
4566 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05004567 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05004568 IOCB_t *cmd, *icmd;
James Smart3a707302012-06-12 13:54:42 -04004569 int ret = SUCCESS, status = 0;
James Smart8931c732018-07-31 17:23:20 -07004570 struct lpfc_sli_ring *pring_s4 = NULL;
James Smart895427b2017-02-12 13:52:30 -08004571 int ret_val;
James Smart59c68ea2018-04-09 14:24:25 -07004572 unsigned long flags;
James Smartfa61a542008-01-11 01:52:42 -05004573 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004574
James Smart3a707302012-06-12 13:54:42 -04004575 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04004576 if (status != 0 && status != SUCCESS)
James Smart3a707302012-06-12 13:54:42 -04004577 return status;
James Smart4f2e66c2012-05-09 21:17:07 -04004578
James Smart876dd7d2012-09-29 11:31:28 -04004579 spin_lock_irqsave(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004580 /* driver queued commands are in process of being flushed */
4581 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
James Smart876dd7d2012-09-29 11:31:28 -04004582 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004583 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4584 "3168 SCSI Layer abort requested I/O has been "
4585 "flushed by LLD.\n");
4586 return FAILED;
4587 }
4588
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004589 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
James Smart92e3af62012-08-14 14:26:28 -04004590 if (!lpfc_cmd || !lpfc_cmd->pCmd) {
James Smart876dd7d2012-09-29 11:31:28 -04004591 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarteee88772010-09-29 11:19:08 -04004592 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4593 "2873 SCSI Layer I/O Abort Request IO CMPL Status "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004594 "x%x ID %d LUN %llu\n",
James Smart3a707302012-06-12 13:54:42 -04004595 SUCCESS, cmnd->device->id, cmnd->device->lun);
James Smarteee88772010-09-29 11:19:08 -04004596 return SUCCESS;
4597 }
dea31012005-04-17 16:05:31 -05004598
James Smart4f2e66c2012-05-09 21:17:07 -04004599 iocb = &lpfc_cmd->cur_iocbq;
James Smart8931c732018-07-31 17:23:20 -07004600 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartcdb42be2019-01-28 11:14:21 -08004601 pring_s4 = phba->sli4_hba.hdwq[iocb->hba_wqidx].fcp_wq->pring;
James Smart8931c732018-07-31 17:23:20 -07004602 if (!pring_s4) {
4603 ret = FAILED;
4604 goto out_unlock;
4605 }
4606 spin_lock(&pring_s4->ring_lock);
4607 }
James Smart4f2e66c2012-05-09 21:17:07 -04004608 /* the command is in process of being cancelled */
4609 if (!(iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
James Smart8931c732018-07-31 17:23:20 -07004610 if (phba->sli_rev == LPFC_SLI_REV4)
4611 spin_unlock(&pring_s4->ring_lock);
James Smart876dd7d2012-09-29 11:31:28 -04004612 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004613 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4614 "3169 SCSI Layer abort requested I/O has been "
4615 "cancelled by LLD.\n");
4616 return FAILED;
4617 }
dea31012005-04-17 16:05:31 -05004618 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004619 * If pCmd field of the corresponding lpfc_scsi_buf structure
4620 * points to a different SCSI command, then the driver has
4621 * already completed this command, but the midlayer did not
James Smart4f2e66c2012-05-09 21:17:07 -04004622 * see the completion before the eh fired. Just return SUCCESS.
dea31012005-04-17 16:05:31 -05004623 */
James Smart4f2e66c2012-05-09 21:17:07 -04004624 if (lpfc_cmd->pCmd != cmnd) {
James Smart8931c732018-07-31 17:23:20 -07004625 if (phba->sli_rev == LPFC_SLI_REV4)
4626 spin_unlock(&pring_s4->ring_lock);
James Smart4f2e66c2012-05-09 21:17:07 -04004627 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4628 "3170 SCSI Layer abort requested I/O has been "
4629 "completed by LLD.\n");
4630 goto out_unlock;
4631 }
dea31012005-04-17 16:05:31 -05004632
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004633 BUG_ON(iocb->context1 != lpfc_cmd);
dea31012005-04-17 16:05:31 -05004634
James Smartee620212014-04-04 13:51:53 -04004635 /* abort issued in recovery is still in progress */
4636 if (iocb->iocb_flag & LPFC_DRIVER_ABORTED) {
4637 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4638 "3389 SCSI Layer I/O Abort Request is pending\n");
James Smart8931c732018-07-31 17:23:20 -07004639 if (phba->sli_rev == LPFC_SLI_REV4)
4640 spin_unlock(&pring_s4->ring_lock);
James Smartee620212014-04-04 13:51:53 -04004641 spin_unlock_irqrestore(&phba->hbalock, flags);
4642 goto wait_for_cmpl;
4643 }
4644
James Smart4f2e66c2012-05-09 21:17:07 -04004645 abtsiocb = __lpfc_sli_get_iocbq(phba);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004646 if (abtsiocb == NULL) {
4647 ret = FAILED;
James Smart8931c732018-07-31 17:23:20 -07004648 if (phba->sli_rev == LPFC_SLI_REV4)
4649 spin_unlock(&pring_s4->ring_lock);
James Smart4f2e66c2012-05-09 21:17:07 -04004650 goto out_unlock;
dea31012005-04-17 16:05:31 -05004651 }
4652
James Smartafbd8d82013-09-06 12:22:13 -04004653 /* Indicate the IO is being aborted by the driver. */
4654 iocb->iocb_flag |= LPFC_DRIVER_ABORTED;
4655
dea31012005-04-17 16:05:31 -05004656 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004657 * The scsi command can not be in txq and it is in flight because the
4658 * pCmd is still pointig at the SCSI command we have to abort. There
4659 * is no need to search the txcmplq. Just send an abort to the FW.
dea31012005-04-17 16:05:31 -05004660 */
dea31012005-04-17 16:05:31 -05004661
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004662 cmd = &iocb->iocb;
4663 icmd = &abtsiocb->iocb;
4664 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
4665 icmd->un.acxri.abortContextTag = cmd->ulpContext;
James Smart3772a992009-05-22 14:50:54 -04004666 if (phba->sli_rev == LPFC_SLI_REV4)
4667 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
4668 else
4669 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05004670
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004671 icmd->ulpLe = 1;
4672 icmd->ulpClass = cmd->ulpClass;
James Smart5ffc2662009-11-18 15:39:44 -05004673
4674 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -08004675 abtsiocb->hba_wqidx = iocb->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -05004676 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -04004677 if (iocb->iocb_flag & LPFC_IO_FOF)
4678 abtsiocb->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -05004679
James Smart2e0fef82007-06-17 19:56:36 -05004680 if (lpfc_is_link_up(phba))
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004681 icmd->ulpCommand = CMD_ABORT_XRI_CN;
4682 else
4683 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea31012005-04-17 16:05:31 -05004684
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004685 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smart2e0fef82007-06-17 19:56:36 -05004686 abtsiocb->vport = vport;
James Smart8931c732018-07-31 17:23:20 -07004687 lpfc_cmd->waitq = &waitq;
James Smart98912dda2014-04-04 13:52:31 -04004688 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart98912dda2014-04-04 13:52:31 -04004689 /* Note: both hbalock and ring_lock must be set here */
James Smart98912dda2014-04-04 13:52:31 -04004690 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
4691 abtsiocb, 0);
James Smart59c68ea2018-04-09 14:24:25 -07004692 spin_unlock(&pring_s4->ring_lock);
James Smart98912dda2014-04-04 13:52:31 -04004693 } else {
4694 ret_val = __lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
4695 abtsiocb, 0);
4696 }
James Smart4f2e66c2012-05-09 21:17:07 -04004697 /* no longer need the lock after this point */
James Smart876dd7d2012-09-29 11:31:28 -04004698 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004699
James Smart98912dda2014-04-04 13:52:31 -04004700
4701 if (ret_val == IOCB_ERROR) {
James Smart8931c732018-07-31 17:23:20 -07004702 if (phba->sli_rev == LPFC_SLI_REV4)
4703 spin_lock_irqsave(&pring_s4->ring_lock, flags);
4704 else
4705 spin_lock_irqsave(&phba->hbalock, flags);
4706 /* Indicate the IO is not being aborted by the driver. */
4707 iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
4708 lpfc_cmd->waitq = NULL;
4709 if (phba->sli_rev == LPFC_SLI_REV4)
4710 spin_unlock_irqrestore(&pring_s4->ring_lock, flags);
4711 else
4712 spin_unlock_irqrestore(&phba->hbalock, flags);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004713 lpfc_sli_release_iocbq(phba, abtsiocb);
4714 ret = FAILED;
4715 goto out;
4716 }
4717
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004718 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
James Smart45ed1192009-10-02 15:17:02 -04004719 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -08004720 &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004721
James Smartee620212014-04-04 13:51:53 -04004722wait_for_cmpl:
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004723 /* Wait for abort to complete */
James Smartfa61a542008-01-11 01:52:42 -05004724 wait_event_timeout(waitq,
4725 (lpfc_cmd->pCmd != cmnd),
James Smart256ec0d2013-04-17 20:14:58 -04004726 msecs_to_jiffies(2*vport->cfg_devloss_tmo*1000));
James Smartee620212014-04-04 13:51:53 -04004727
4728 spin_lock_irqsave(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05004729 lpfc_cmd->waitq = NULL;
James Smartee620212014-04-04 13:51:53 -04004730 spin_unlock_irqrestore(shost->host_lock, flags);
dea31012005-04-17 16:05:31 -05004731
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004732 if (lpfc_cmd->pCmd == cmnd) {
4733 ret = FAILED;
James Smarte8b62012007-08-02 11:10:09 -04004734 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4735 "0748 abort handler timed out waiting "
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -07004736 "for aborting I/O (xri:x%x) to complete: "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004737 "ret %#x, ID %d, LUN %llu\n",
James Smart247ca942012-08-14 14:26:13 -04004738 iocb->sli4_xritag, ret,
4739 cmnd->device->id, cmnd->device->lun);
dea31012005-04-17 16:05:31 -05004740 }
James Smart4f2e66c2012-05-09 21:17:07 -04004741 goto out;
dea31012005-04-17 16:05:31 -05004742
James Smart4f2e66c2012-05-09 21:17:07 -04004743out_unlock:
James Smart876dd7d2012-09-29 11:31:28 -04004744 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004745out:
James Smarte8b62012007-08-02 11:10:09 -04004746 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4747 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004748 "LUN %llu\n", ret, cmnd->device->id,
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04004749 cmnd->device->lun);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004750 return ret;
dea31012005-04-17 16:05:31 -05004751}
4752
James Smartbbb9d182009-06-10 17:23:16 -04004753static char *
4754lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
4755{
4756 switch (task_mgmt_cmd) {
4757 case FCP_ABORT_TASK_SET:
4758 return "ABORT_TASK_SET";
4759 case FCP_CLEAR_TASK_SET:
4760 return "FCP_CLEAR_TASK_SET";
4761 case FCP_BUS_RESET:
4762 return "FCP_BUS_RESET";
4763 case FCP_LUN_RESET:
4764 return "FCP_LUN_RESET";
4765 case FCP_TARGET_RESET:
4766 return "FCP_TARGET_RESET";
4767 case FCP_CLEAR_ACA:
4768 return "FCP_CLEAR_ACA";
4769 case FCP_TERMINATE_TASK:
4770 return "FCP_TERMINATE_TASK";
4771 default:
4772 return "unknown";
4773 }
4774}
4775
James Smart53151bb2013-10-10 12:24:07 -04004776
4777/**
4778 * lpfc_check_fcp_rsp - check the returned fcp_rsp to see if task failed
4779 * @vport: The virtual port for which this call is being executed.
4780 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
4781 *
4782 * This routine checks the FCP RSP INFO to see if the tsk mgmt command succeded
4783 *
4784 * Return code :
4785 * 0x2003 - Error
4786 * 0x2002 - Success
4787 **/
4788static int
4789lpfc_check_fcp_rsp(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd)
4790{
4791 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
4792 uint32_t rsp_info;
4793 uint32_t rsp_len;
4794 uint8_t rsp_info_code;
4795 int ret = FAILED;
4796
4797
4798 if (fcprsp == NULL)
4799 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4800 "0703 fcp_rsp is missing\n");
4801 else {
4802 rsp_info = fcprsp->rspStatus2;
4803 rsp_len = be32_to_cpu(fcprsp->rspRspLen);
4804 rsp_info_code = fcprsp->rspInfo3;
4805
4806
4807 lpfc_printf_vlog(vport, KERN_INFO,
4808 LOG_FCP,
4809 "0706 fcp_rsp valid 0x%x,"
4810 " rsp len=%d code 0x%x\n",
4811 rsp_info,
4812 rsp_len, rsp_info_code);
4813
4814 if ((fcprsp->rspStatus2&RSP_LEN_VALID) && (rsp_len == 8)) {
4815 switch (rsp_info_code) {
4816 case RSP_NO_FAILURE:
4817 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4818 "0715 Task Mgmt No Failure\n");
4819 ret = SUCCESS;
4820 break;
4821 case RSP_TM_NOT_SUPPORTED: /* TM rejected */
4822 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4823 "0716 Task Mgmt Target "
4824 "reject\n");
4825 break;
4826 case RSP_TM_NOT_COMPLETED: /* TM failed */
4827 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4828 "0717 Task Mgmt Target "
4829 "failed TM\n");
4830 break;
4831 case RSP_TM_INVALID_LU: /* TM to invalid LU! */
4832 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4833 "0718 Task Mgmt to invalid "
4834 "LUN\n");
4835 break;
4836 }
4837 }
4838 }
4839 return ret;
4840}
4841
4842
James Smartbbb9d182009-06-10 17:23:16 -04004843/**
4844 * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
4845 * @vport: The virtual port for which this call is being executed.
4846 * @rdata: Pointer to remote port local data
4847 * @tgt_id: Target ID of remote device.
4848 * @lun_id: Lun number for the TMF
4849 * @task_mgmt_cmd: type of TMF to send
4850 *
4851 * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
4852 * a remote port.
4853 *
4854 * Return Code:
4855 * 0x2003 - Error
4856 * 0x2002 - Success.
4857 **/
4858static int
James Smarteed695d2016-10-13 15:06:04 -07004859lpfc_send_taskmgmt(struct lpfc_vport *vport, struct scsi_cmnd *cmnd,
4860 unsigned int tgt_id, uint64_t lun_id,
4861 uint8_t task_mgmt_cmd)
James Smartbbb9d182009-06-10 17:23:16 -04004862{
4863 struct lpfc_hba *phba = vport->phba;
4864 struct lpfc_scsi_buf *lpfc_cmd;
4865 struct lpfc_iocbq *iocbq;
4866 struct lpfc_iocbq *iocbqrsp;
James Smarteed695d2016-10-13 15:06:04 -07004867 struct lpfc_rport_data *rdata;
4868 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004869 int ret;
4870 int status;
4871
James Smarteed695d2016-10-13 15:06:04 -07004872 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
4873 if (!rdata || !rdata->pnode || !NLP_CHK_NODE_ACT(rdata->pnode))
James Smartbbb9d182009-06-10 17:23:16 -04004874 return FAILED;
James Smarteed695d2016-10-13 15:06:04 -07004875 pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004876
James Smarteed695d2016-10-13 15:06:04 -07004877 lpfc_cmd = lpfc_get_scsi_buf(phba, pnode);
James Smartbbb9d182009-06-10 17:23:16 -04004878 if (lpfc_cmd == NULL)
4879 return FAILED;
James Smart0c411222013-09-06 12:22:46 -04004880 lpfc_cmd->timeout = phba->cfg_task_mgmt_tmo;
James Smartbbb9d182009-06-10 17:23:16 -04004881 lpfc_cmd->rdata = rdata;
James Smarteed695d2016-10-13 15:06:04 -07004882 lpfc_cmd->pCmd = cmnd;
James Smart2a5b7d62018-07-31 17:23:22 -07004883 lpfc_cmd->ndlp = pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004884
4885 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
4886 task_mgmt_cmd);
4887 if (!status) {
4888 lpfc_release_scsi_buf(phba, lpfc_cmd);
4889 return FAILED;
4890 }
4891
4892 iocbq = &lpfc_cmd->cur_iocbq;
4893 iocbqrsp = lpfc_sli_get_iocbq(phba);
4894 if (iocbqrsp == NULL) {
4895 lpfc_release_scsi_buf(phba, lpfc_cmd);
4896 return FAILED;
4897 }
James Smart5a0916b2013-07-15 18:31:42 -04004898 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
James Smartbbb9d182009-06-10 17:23:16 -04004899
4900 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004901 "0702 Issue %s to TGT %d LUN %llu "
James Smart6d368e52011-05-24 11:44:12 -04004902 "rpi x%x nlp_flag x%x Data: x%x x%x\n",
James Smartbbb9d182009-06-10 17:23:16 -04004903 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
James Smart6d368e52011-05-24 11:44:12 -04004904 pnode->nlp_rpi, pnode->nlp_flag, iocbq->sli4_xritag,
4905 iocbq->iocb_flag);
James Smartbbb9d182009-06-10 17:23:16 -04004906
4907 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
4908 iocbq, iocbqrsp, lpfc_cmd->timeout);
James Smart53151bb2013-10-10 12:24:07 -04004909 if ((status != IOCB_SUCCESS) ||
4910 (iocbqrsp->iocb.ulpStatus != IOSTAT_SUCCESS)) {
James Smartae374a32015-05-22 10:42:41 -04004911 if (status != IOCB_SUCCESS ||
4912 iocbqrsp->iocb.ulpStatus != IOSTAT_FCP_RSP_ERROR)
4913 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4914 "0727 TMF %s to TGT %d LUN %llu "
4915 "failed (%d, %d) iocb_flag x%x\n",
4916 lpfc_taskmgmt_name(task_mgmt_cmd),
4917 tgt_id, lun_id,
4918 iocbqrsp->iocb.ulpStatus,
4919 iocbqrsp->iocb.un.ulpWord[4],
4920 iocbq->iocb_flag);
James Smart53151bb2013-10-10 12:24:07 -04004921 /* if ulpStatus != IOCB_SUCCESS, then status == IOCB_SUCCESS */
4922 if (status == IOCB_SUCCESS) {
4923 if (iocbqrsp->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
4924 /* Something in the FCP_RSP was invalid.
4925 * Check conditions */
4926 ret = lpfc_check_fcp_rsp(vport, lpfc_cmd);
4927 else
4928 ret = FAILED;
4929 } else if (status == IOCB_TIMEDOUT) {
4930 ret = TIMEOUT_ERROR;
4931 } else {
4932 ret = FAILED;
4933 }
James Smart53151bb2013-10-10 12:24:07 -04004934 } else
James Smartbbb9d182009-06-10 17:23:16 -04004935 ret = SUCCESS;
4936
4937 lpfc_sli_release_iocbq(phba, iocbqrsp);
4938
4939 if (ret != TIMEOUT_ERROR)
4940 lpfc_release_scsi_buf(phba, lpfc_cmd);
4941
4942 return ret;
4943}
4944
4945/**
4946 * lpfc_chk_tgt_mapped -
4947 * @vport: The virtual port to check on
4948 * @cmnd: Pointer to scsi_cmnd data structure.
4949 *
4950 * This routine delays until the scsi target (aka rport) for the
4951 * command exists (is present and logged in) or we declare it non-existent.
4952 *
4953 * Return code :
4954 * 0x2003 - Error
4955 * 0x2002 - Success
4956 **/
4957static int
4958lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
4959{
James Smart1ba981f2014-02-20 09:56:45 -05004960 struct lpfc_rport_data *rdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05004961 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004962 unsigned long later;
4963
James Smart1ba981f2014-02-20 09:56:45 -05004964 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smart1c6f4ef52009-11-18 15:40:49 -05004965 if (!rdata) {
4966 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4967 "0797 Tgt Map rport failure: rdata x%p\n", rdata);
4968 return FAILED;
4969 }
4970 pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004971 /*
4972 * If target is not in a MAPPED state, delay until
4973 * target is rediscovered or devloss timeout expires.
4974 */
4975 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
4976 while (time_after(later, jiffies)) {
4977 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
4978 return FAILED;
4979 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
4980 return SUCCESS;
4981 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
James Smart1ba981f2014-02-20 09:56:45 -05004982 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smartbbb9d182009-06-10 17:23:16 -04004983 if (!rdata)
4984 return FAILED;
4985 pnode = rdata->pnode;
4986 }
4987 if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
4988 (pnode->nlp_state != NLP_STE_MAPPED_NODE))
4989 return FAILED;
4990 return SUCCESS;
4991}
4992
4993/**
4994 * lpfc_reset_flush_io_context -
4995 * @vport: The virtual port (scsi_host) for the flush context
4996 * @tgt_id: If aborting by Target contect - specifies the target id
4997 * @lun_id: If aborting by Lun context - specifies the lun id
4998 * @context: specifies the context level to flush at.
4999 *
5000 * After a reset condition via TMF, we need to flush orphaned i/o
5001 * contexts from the adapter. This routine aborts any contexts
5002 * outstanding, then waits for their completions. The wait is
5003 * bounded by devloss_tmo though.
5004 *
5005 * Return code :
5006 * 0x2003 - Error
5007 * 0x2002 - Success
5008 **/
5009static int
5010lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
5011 uint64_t lun_id, lpfc_ctx_cmd context)
5012{
5013 struct lpfc_hba *phba = vport->phba;
5014 unsigned long later;
5015 int cnt;
5016
5017 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
5018 if (cnt)
James Smart98912dda2014-04-04 13:52:31 -04005019 lpfc_sli_abort_taskmgmt(vport,
James Smart895427b2017-02-12 13:52:30 -08005020 &phba->sli.sli3_ring[LPFC_FCP_RING],
James Smart98912dda2014-04-04 13:52:31 -04005021 tgt_id, lun_id, context);
James Smartbbb9d182009-06-10 17:23:16 -04005022 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
5023 while (time_after(later, jiffies) && cnt) {
5024 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
5025 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
5026 }
5027 if (cnt) {
5028 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5029 "0724 I/O flush failure for context %s : cnt x%x\n",
5030 ((context == LPFC_CTX_LUN) ? "LUN" :
5031 ((context == LPFC_CTX_TGT) ? "TGT" :
5032 ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
5033 cnt);
5034 return FAILED;
5035 }
5036 return SUCCESS;
5037}
5038
James Smart9bad7672008-12-04 22:39:02 -05005039/**
James Smart3621a712009-04-06 18:47:14 -04005040 * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
James Smart9bad7672008-12-04 22:39:02 -05005041 * @cmnd: Pointer to scsi_cmnd data structure.
5042 *
James Smartbbb9d182009-06-10 17:23:16 -04005043 * This routine does a device reset by sending a LUN_RESET task management
James Smart9bad7672008-12-04 22:39:02 -05005044 * command.
5045 *
5046 * Return code :
5047 * 0x2003 - Error
James Smart3621a712009-04-06 18:47:14 -04005048 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05005049 **/
dea31012005-04-17 16:05:31 -05005050static int
James Smart7054a602007-04-25 09:52:34 -04005051lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05005052{
James Smart2e0fef82007-06-17 19:56:36 -05005053 struct Scsi_Host *shost = cmnd->device->host;
5054 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smart1ba981f2014-02-20 09:56:45 -05005055 struct lpfc_rport_data *rdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005056 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005057 unsigned tgt_id = cmnd->device->id;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005058 uint64_t lun_id = cmnd->device->lun;
James Smartea2151b2008-09-07 11:52:10 -04005059 struct lpfc_scsi_event_header scsi_event;
James Smart53151bb2013-10-10 12:24:07 -04005060 int status;
dea31012005-04-17 16:05:31 -05005061
James Smart1ba981f2014-02-20 09:56:45 -05005062 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smartad490b62015-04-07 15:07:26 -04005063 if (!rdata || !rdata->pnode) {
James Smart1c6f4ef52009-11-18 15:40:49 -05005064 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smartad490b62015-04-07 15:07:26 -04005065 "0798 Device Reset rport failure: rdata x%p\n",
5066 rdata);
James Smart1c6f4ef52009-11-18 15:40:49 -05005067 return FAILED;
5068 }
5069 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04005070 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005071 if (status != 0 && status != SUCCESS)
James Smart589a52d2010-07-14 15:30:54 -04005072 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005073
5074 status = lpfc_chk_tgt_mapped(vport, cmnd);
5075 if (status == FAILED) {
5076 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5077 "0721 Device Reset rport failure: rdata x%p\n", rdata);
5078 return FAILED;
5079 }
5080
5081 scsi_event.event_type = FC_REG_SCSI_EVENT;
5082 scsi_event.subcategory = LPFC_EVENT_LUNRESET;
5083 scsi_event.lun = lun_id;
5084 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5085 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5086
5087 fc_host_post_vendor_event(shost, fc_get_event_number(),
5088 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
5089
James Smarteed695d2016-10-13 15:06:04 -07005090 status = lpfc_send_taskmgmt(vport, cmnd, tgt_id, lun_id,
James Smartbbb9d182009-06-10 17:23:16 -04005091 FCP_LUN_RESET);
5092
5093 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005094 "0713 SCSI layer issued Device Reset (%d, %llu) "
James Smartbbb9d182009-06-10 17:23:16 -04005095 "return x%x\n", tgt_id, lun_id, status);
5096
dea31012005-04-17 16:05:31 -05005097 /*
James Smartbbb9d182009-06-10 17:23:16 -04005098 * We have to clean up i/o as : they may be orphaned by the TMF;
5099 * or if the TMF failed, they may be in an indeterminate state.
5100 * So, continue on.
5101 * We will report success if all the i/o aborts successfully.
dea31012005-04-17 16:05:31 -05005102 */
James Smart53151bb2013-10-10 12:24:07 -04005103 if (status == SUCCESS)
5104 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
James Smartbbb9d182009-06-10 17:23:16 -04005105 LPFC_CTX_LUN);
James Smart53151bb2013-10-10 12:24:07 -04005106
5107 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005108}
5109
5110/**
5111 * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
5112 * @cmnd: Pointer to scsi_cmnd data structure.
5113 *
5114 * This routine does a target reset by sending a TARGET_RESET task management
5115 * command.
5116 *
5117 * Return code :
5118 * 0x2003 - Error
5119 * 0x2002 - Success
5120 **/
5121static int
5122lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
5123{
5124 struct Scsi_Host *shost = cmnd->device->host;
5125 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smart1ba981f2014-02-20 09:56:45 -05005126 struct lpfc_rport_data *rdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005127 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005128 unsigned tgt_id = cmnd->device->id;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005129 uint64_t lun_id = cmnd->device->lun;
James Smartbbb9d182009-06-10 17:23:16 -04005130 struct lpfc_scsi_event_header scsi_event;
James Smart53151bb2013-10-10 12:24:07 -04005131 int status;
James Smartbbb9d182009-06-10 17:23:16 -04005132
James Smart1ba981f2014-02-20 09:56:45 -05005133 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smart1c6f4ef52009-11-18 15:40:49 -05005134 if (!rdata) {
5135 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5136 "0799 Target Reset rport failure: rdata x%p\n", rdata);
5137 return FAILED;
5138 }
5139 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04005140 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005141 if (status != 0 && status != SUCCESS)
James Smart589a52d2010-07-14 15:30:54 -04005142 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005143
5144 status = lpfc_chk_tgt_mapped(vport, cmnd);
5145 if (status == FAILED) {
5146 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5147 "0722 Target Reset rport failure: rdata x%p\n", rdata);
James Smart63e480f2015-04-07 15:07:20 -04005148 if (pnode) {
5149 spin_lock_irq(shost->host_lock);
5150 pnode->nlp_flag &= ~NLP_NPR_ADISC;
5151 pnode->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
5152 spin_unlock_irq(shost->host_lock);
5153 }
James Smart8c50d252014-09-03 12:58:16 -04005154 lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
5155 LPFC_CTX_TGT);
5156 return FAST_IO_FAIL;
dea31012005-04-17 16:05:31 -05005157 }
James Smartea2151b2008-09-07 11:52:10 -04005158
5159 scsi_event.event_type = FC_REG_SCSI_EVENT;
5160 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
5161 scsi_event.lun = 0;
5162 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5163 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5164
James Smartbbb9d182009-06-10 17:23:16 -04005165 fc_host_post_vendor_event(shost, fc_get_event_number(),
5166 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -04005167
James Smarteed695d2016-10-13 15:06:04 -07005168 status = lpfc_send_taskmgmt(vport, cmnd, tgt_id, lun_id,
James Smartbbb9d182009-06-10 17:23:16 -04005169 FCP_TARGET_RESET);
dea31012005-04-17 16:05:31 -05005170
James Smarte8b62012007-08-02 11:10:09 -04005171 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005172 "0723 SCSI layer issued Target Reset (%d, %llu) "
James Smartbbb9d182009-06-10 17:23:16 -04005173 "return x%x\n", tgt_id, lun_id, status);
5174
5175 /*
5176 * We have to clean up i/o as : they may be orphaned by the TMF;
5177 * or if the TMF failed, they may be in an indeterminate state.
5178 * So, continue on.
5179 * We will report success if all the i/o aborts successfully.
5180 */
James Smart53151bb2013-10-10 12:24:07 -04005181 if (status == SUCCESS)
5182 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
James Smart3a707302012-06-12 13:54:42 -04005183 LPFC_CTX_TGT);
James Smart53151bb2013-10-10 12:24:07 -04005184 return status;
dea31012005-04-17 16:05:31 -05005185}
5186
James Smart9bad7672008-12-04 22:39:02 -05005187/**
James Smart3621a712009-04-06 18:47:14 -04005188 * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05005189 * @cmnd: Pointer to scsi_cmnd data structure.
5190 *
James Smartbbb9d182009-06-10 17:23:16 -04005191 * This routine does target reset to all targets on @cmnd->device->host.
5192 * This emulates Parallel SCSI Bus Reset Semantics.
James Smart9bad7672008-12-04 22:39:02 -05005193 *
James Smartbbb9d182009-06-10 17:23:16 -04005194 * Return code :
5195 * 0x2003 - Error
5196 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05005197 **/
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04005198static int
James Smart7054a602007-04-25 09:52:34 -04005199lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05005200{
James Smart2e0fef82007-06-17 19:56:36 -05005201 struct Scsi_Host *shost = cmnd->device->host;
5202 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05005203 struct lpfc_nodelist *ndlp = NULL;
James Smartea2151b2008-09-07 11:52:10 -04005204 struct lpfc_scsi_event_header scsi_event;
James Smartbbb9d182009-06-10 17:23:16 -04005205 int match;
5206 int ret = SUCCESS, status, i;
James Smartea2151b2008-09-07 11:52:10 -04005207
5208 scsi_event.event_type = FC_REG_SCSI_EVENT;
5209 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
5210 scsi_event.lun = 0;
5211 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
5212 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
5213
James Smartbbb9d182009-06-10 17:23:16 -04005214 fc_host_post_vendor_event(shost, fc_get_event_number(),
5215 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
dea31012005-04-17 16:05:31 -05005216
James Smartbf086112011-08-21 21:48:13 -04005217 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005218 if (status != 0 && status != SUCCESS)
James Smartbf086112011-08-21 21:48:13 -04005219 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005220
dea31012005-04-17 16:05:31 -05005221 /*
5222 * Since the driver manages a single bus device, reset all
5223 * targets known to the driver. Should any target reset
5224 * fail, this routine returns failure to the midlayer.
5225 */
James Smarte17da182006-07-06 15:49:25 -04005226 for (i = 0; i < LPFC_MAX_TARGET; i++) {
James Smart685f0bf2007-04-25 09:53:08 -04005227 /* Search for mapped node by target ID */
dea31012005-04-17 16:05:31 -05005228 match = 0;
James Smart2e0fef82007-06-17 19:56:36 -05005229 spin_lock_irq(shost->host_lock);
5230 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05005231 if (!NLP_CHK_NODE_ACT(ndlp))
5232 continue;
James Smarta6571c62012-10-31 14:44:42 -04005233 if (vport->phba->cfg_fcp2_no_tgt_reset &&
5234 (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE))
5235 continue;
James Smart685f0bf2007-04-25 09:53:08 -04005236 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
James Smart915caaa2008-06-14 22:52:38 -04005237 ndlp->nlp_sid == i &&
James Smarta0f2d3e2017-02-12 13:52:31 -08005238 ndlp->rport &&
5239 ndlp->nlp_type & NLP_FCP_TARGET) {
dea31012005-04-17 16:05:31 -05005240 match = 1;
5241 break;
5242 }
5243 }
James Smart2e0fef82007-06-17 19:56:36 -05005244 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005245 if (!match)
5246 continue;
James Smartbbb9d182009-06-10 17:23:16 -04005247
James Smarteed695d2016-10-13 15:06:04 -07005248 status = lpfc_send_taskmgmt(vport, cmnd,
James Smartbbb9d182009-06-10 17:23:16 -04005249 i, 0, FCP_TARGET_RESET);
5250
5251 if (status != SUCCESS) {
James Smarte8b62012007-08-02 11:10:09 -04005252 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5253 "0700 Bus Reset on target %d failed\n",
5254 i);
James Smart915caaa2008-06-14 22:52:38 -04005255 ret = FAILED;
dea31012005-04-17 16:05:31 -05005256 }
5257 }
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005258 /*
James Smartbbb9d182009-06-10 17:23:16 -04005259 * We have to clean up i/o as : they may be orphaned by the TMFs
5260 * above; or if any of the TMFs failed, they may be in an
5261 * indeterminate state.
5262 * We will report success if all the i/o aborts successfully.
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005263 */
James Smartbbb9d182009-06-10 17:23:16 -04005264
5265 status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
5266 if (status != SUCCESS)
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005267 ret = FAILED;
James Smartbbb9d182009-06-10 17:23:16 -04005268
James Smarte8b62012007-08-02 11:10:09 -04005269 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5270 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea31012005-04-17 16:05:31 -05005271 return ret;
5272}
5273
James Smart9bad7672008-12-04 22:39:02 -05005274/**
James Smart27b01b82012-05-09 21:19:44 -04005275 * lpfc_host_reset_handler - scsi_host_template eh_host_reset_handler entry pt
5276 * @cmnd: Pointer to scsi_cmnd data structure.
5277 *
5278 * This routine does host reset to the adaptor port. It brings the HBA
5279 * offline, performs a board restart, and then brings the board back online.
5280 * The lpfc_offline calls lpfc_sli_hba_down which will abort and local
5281 * reject all outstanding SCSI commands to the host and error returned
5282 * back to SCSI mid-level. As this will be SCSI mid-level's last resort
5283 * of error handling, it will only return error if resetting of the adapter
5284 * is not successful; in all other cases, will return success.
5285 *
5286 * Return code :
5287 * 0x2003 - Error
5288 * 0x2002 - Success
5289 **/
5290static int
5291lpfc_host_reset_handler(struct scsi_cmnd *cmnd)
5292{
5293 struct Scsi_Host *shost = cmnd->device->host;
5294 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5295 struct lpfc_hba *phba = vport->phba;
5296 int rc, ret = SUCCESS;
5297
James Smarta88dbb62013-04-17 20:18:39 -04005298 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5299 "3172 SCSI layer issued Host Reset Data:\n");
5300
James Smart618a5232012-06-12 13:54:36 -04005301 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart27b01b82012-05-09 21:19:44 -04005302 lpfc_offline(phba);
5303 rc = lpfc_sli_brdrestart(phba);
5304 if (rc)
5305 ret = FAILED;
James Smarta88dbb62013-04-17 20:18:39 -04005306 rc = lpfc_online(phba);
5307 if (rc)
5308 ret = FAILED;
James Smart27b01b82012-05-09 21:19:44 -04005309 lpfc_unblock_mgmt_io(phba);
5310
James Smarta88dbb62013-04-17 20:18:39 -04005311 if (ret == FAILED) {
5312 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5313 "3323 Failed host reset, bring it offline\n");
5314 lpfc_sli4_offline_eratt(phba);
5315 }
James Smart27b01b82012-05-09 21:19:44 -04005316 return ret;
5317}
5318
5319/**
James Smart3621a712009-04-06 18:47:14 -04005320 * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
James Smart9bad7672008-12-04 22:39:02 -05005321 * @sdev: Pointer to scsi_device.
5322 *
5323 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
5324 * globally available list of scsi buffers. This routine also makes sure scsi
5325 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
5326 * of scsi buffer exists for the lifetime of the driver.
5327 *
5328 * Return codes:
5329 * non-0 - Error
5330 * 0 - Success
5331 **/
dea31012005-04-17 16:05:31 -05005332static int
dea31012005-04-17 16:05:31 -05005333lpfc_slave_alloc(struct scsi_device *sdev)
5334{
James Smart2e0fef82007-06-17 19:56:36 -05005335 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5336 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005337 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
James Smart3772a992009-05-22 14:50:54 -04005338 uint32_t total = 0;
dea31012005-04-17 16:05:31 -05005339 uint32_t num_to_alloc = 0;
James Smart3772a992009-05-22 14:50:54 -04005340 int num_allocated = 0;
James Smartd7c47992010-06-08 18:31:54 -04005341 uint32_t sdev_cnt;
James Smart1ba981f2014-02-20 09:56:45 -05005342 struct lpfc_device_data *device_data;
5343 unsigned long flags;
5344 struct lpfc_name target_wwpn;
dea31012005-04-17 16:05:31 -05005345
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005346 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05005347 return -ENXIO;
5348
James Smartf38fa0b2014-04-04 13:52:21 -04005349 if (phba->cfg_fof) {
James Smart1ba981f2014-02-20 09:56:45 -05005350
5351 /*
5352 * Check to see if the device data structure for the lun
5353 * exists. If not, create one.
5354 */
5355
5356 u64_to_wwn(rport->port_name, target_wwpn.u.wwn);
5357 spin_lock_irqsave(&phba->devicelock, flags);
5358 device_data = __lpfc_get_device_data(phba,
5359 &phba->luns,
5360 &vport->fc_portname,
5361 &target_wwpn,
5362 sdev->lun);
5363 if (!device_data) {
5364 spin_unlock_irqrestore(&phba->devicelock, flags);
5365 device_data = lpfc_create_device_data(phba,
5366 &vport->fc_portname,
5367 &target_wwpn,
James Smartb5749fe2016-12-19 15:07:26 -08005368 sdev->lun,
5369 phba->cfg_XLanePriority,
5370 true);
James Smart1ba981f2014-02-20 09:56:45 -05005371 if (!device_data)
5372 return -ENOMEM;
5373 spin_lock_irqsave(&phba->devicelock, flags);
5374 list_add_tail(&device_data->listentry, &phba->luns);
5375 }
5376 device_data->rport_data = rport->dd_data;
5377 device_data->available = true;
5378 spin_unlock_irqrestore(&phba->devicelock, flags);
5379 sdev->hostdata = device_data;
5380 } else {
5381 sdev->hostdata = rport->dd_data;
5382 }
James Smartd7c47992010-06-08 18:31:54 -04005383 sdev_cnt = atomic_inc_return(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05005384
James Smart0794d602019-01-28 11:14:19 -08005385 /* For SLI4, all IO buffers are pre-allocated */
5386 if (phba->sli_rev == LPFC_SLI_REV4)
5387 return 0;
5388
5389 /* This code path is now ONLY for SLI3 adapters */
5390
dea31012005-04-17 16:05:31 -05005391 /*
5392 * Populate the cmds_per_lun count scsi_bufs into this host's globally
5393 * available list of scsi buffers. Don't allocate more than the
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04005394 * HBA limit conveyed to the midlayer via the host structure. The
5395 * formula accounts for the lun_queue_depth + error handlers + 1
5396 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea31012005-04-17 16:05:31 -05005397 */
5398 total = phba->total_scsi_bufs;
James Smart3de2a652007-08-02 11:09:59 -04005399 num_to_alloc = vport->cfg_lun_queue_depth + 2;
James Smart92d7f7b2007-06-17 19:56:38 -05005400
James Smartd7c47992010-06-08 18:31:54 -04005401 /* If allocated buffers are enough do nothing */
5402 if ((sdev_cnt * (vport->cfg_lun_queue_depth + 2)) < total)
5403 return 0;
5404
James Smart92d7f7b2007-06-17 19:56:38 -05005405 /* Allow some exchanges to be available always to complete discovery */
5406 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04005407 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5408 "0704 At limitation of %d preallocated "
5409 "command buffers\n", total);
dea31012005-04-17 16:05:31 -05005410 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05005411 /* Allow some exchanges to be available always to complete discovery */
5412 } else if (total + num_to_alloc >
5413 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04005414 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5415 "0705 Allocation request of %d "
5416 "command buffers will exceed max of %d. "
5417 "Reducing allocation request to %d.\n",
5418 num_to_alloc, phba->cfg_hba_queue_depth,
5419 (phba->cfg_hba_queue_depth - total));
dea31012005-04-17 16:05:31 -05005420 num_to_alloc = phba->cfg_hba_queue_depth - total;
5421 }
James Smart0794d602019-01-28 11:14:19 -08005422 num_allocated = lpfc_new_scsi_buf_s3(vport, num_to_alloc);
James Smart3772a992009-05-22 14:50:54 -04005423 if (num_to_alloc != num_allocated) {
James Smart96f70772013-04-17 20:16:15 -04005424 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5425 "0708 Allocation request of %d "
5426 "command buffers did not succeed. "
5427 "Allocated %d buffers.\n",
5428 num_to_alloc, num_allocated);
dea31012005-04-17 16:05:31 -05005429 }
James Smart1c6f4ef52009-11-18 15:40:49 -05005430 if (num_allocated > 0)
5431 phba->total_scsi_bufs += num_allocated;
dea31012005-04-17 16:05:31 -05005432 return 0;
5433}
5434
James Smart9bad7672008-12-04 22:39:02 -05005435/**
James Smart3621a712009-04-06 18:47:14 -04005436 * lpfc_slave_configure - scsi_host_template slave_configure entry point
James Smart9bad7672008-12-04 22:39:02 -05005437 * @sdev: Pointer to scsi_device.
5438 *
5439 * This routine configures following items
5440 * - Tag command queuing support for @sdev if supported.
James Smart9bad7672008-12-04 22:39:02 -05005441 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
5442 *
5443 * Return codes:
5444 * 0 - Success
5445 **/
dea31012005-04-17 16:05:31 -05005446static int
5447lpfc_slave_configure(struct scsi_device *sdev)
5448{
James Smart2e0fef82007-06-17 19:56:36 -05005449 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5450 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05005451
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01005452 scsi_change_queue_depth(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05005453
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005454 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04005455 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -08005456 &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005457 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
5458 lpfc_poll_rearm_timer(phba);
5459 }
5460
dea31012005-04-17 16:05:31 -05005461 return 0;
5462}
5463
James Smart9bad7672008-12-04 22:39:02 -05005464/**
James Smart3621a712009-04-06 18:47:14 -04005465 * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
James Smart9bad7672008-12-04 22:39:02 -05005466 * @sdev: Pointer to scsi_device.
5467 *
5468 * This routine sets @sdev hostatdata filed to null.
5469 **/
dea31012005-04-17 16:05:31 -05005470static void
5471lpfc_slave_destroy(struct scsi_device *sdev)
5472{
James Smartd7c47992010-06-08 18:31:54 -04005473 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5474 struct lpfc_hba *phba = vport->phba;
James Smart1ba981f2014-02-20 09:56:45 -05005475 unsigned long flags;
5476 struct lpfc_device_data *device_data = sdev->hostdata;
5477
James Smartd7c47992010-06-08 18:31:54 -04005478 atomic_dec(&phba->sdev_cnt);
James Smartf38fa0b2014-04-04 13:52:21 -04005479 if ((phba->cfg_fof) && (device_data)) {
James Smart1ba981f2014-02-20 09:56:45 -05005480 spin_lock_irqsave(&phba->devicelock, flags);
5481 device_data->available = false;
5482 if (!device_data->oas_enabled)
5483 lpfc_delete_device_data(phba, device_data);
5484 spin_unlock_irqrestore(&phba->devicelock, flags);
5485 }
dea31012005-04-17 16:05:31 -05005486 sdev->hostdata = NULL;
5487 return;
5488}
5489
James Smart1ba981f2014-02-20 09:56:45 -05005490/**
5491 * lpfc_create_device_data - creates and initializes device data structure for OAS
5492 * @pha: Pointer to host bus adapter structure.
5493 * @vport_wwpn: Pointer to vport's wwpn information
5494 * @target_wwpn: Pointer to target's wwpn information
5495 * @lun: Lun on target
5496 * @atomic_create: Flag to indicate if memory should be allocated using the
5497 * GFP_ATOMIC flag or not.
5498 *
5499 * This routine creates a device data structure which will contain identifying
5500 * information for the device (host wwpn, target wwpn, lun), state of OAS,
5501 * whether or not the corresponding lun is available by the system,
5502 * and pointer to the rport data.
5503 *
5504 * Return codes:
5505 * NULL - Error
5506 * Pointer to lpfc_device_data - Success
5507 **/
5508struct lpfc_device_data*
5509lpfc_create_device_data(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
5510 struct lpfc_name *target_wwpn, uint64_t lun,
James Smartb5749fe2016-12-19 15:07:26 -08005511 uint32_t pri, bool atomic_create)
James Smart1ba981f2014-02-20 09:56:45 -05005512{
5513
5514 struct lpfc_device_data *lun_info;
5515 int memory_flags;
5516
5517 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
James Smartf38fa0b2014-04-04 13:52:21 -04005518 !(phba->cfg_fof))
James Smart1ba981f2014-02-20 09:56:45 -05005519 return NULL;
5520
5521 /* Attempt to create the device data to contain lun info */
5522
5523 if (atomic_create)
5524 memory_flags = GFP_ATOMIC;
5525 else
5526 memory_flags = GFP_KERNEL;
5527 lun_info = mempool_alloc(phba->device_data_mem_pool, memory_flags);
5528 if (!lun_info)
5529 return NULL;
5530 INIT_LIST_HEAD(&lun_info->listentry);
5531 lun_info->rport_data = NULL;
5532 memcpy(&lun_info->device_id.vport_wwpn, vport_wwpn,
5533 sizeof(struct lpfc_name));
5534 memcpy(&lun_info->device_id.target_wwpn, target_wwpn,
5535 sizeof(struct lpfc_name));
5536 lun_info->device_id.lun = lun;
5537 lun_info->oas_enabled = false;
James Smartb5749fe2016-12-19 15:07:26 -08005538 lun_info->priority = pri;
James Smart1ba981f2014-02-20 09:56:45 -05005539 lun_info->available = false;
5540 return lun_info;
5541}
5542
5543/**
5544 * lpfc_delete_device_data - frees a device data structure for OAS
5545 * @pha: Pointer to host bus adapter structure.
5546 * @lun_info: Pointer to device data structure to free.
5547 *
5548 * This routine frees the previously allocated device data structure passed.
5549 *
5550 **/
5551void
5552lpfc_delete_device_data(struct lpfc_hba *phba,
5553 struct lpfc_device_data *lun_info)
5554{
5555
5556 if (unlikely(!phba) || !lun_info ||
James Smartf38fa0b2014-04-04 13:52:21 -04005557 !(phba->cfg_fof))
James Smart1ba981f2014-02-20 09:56:45 -05005558 return;
5559
5560 if (!list_empty(&lun_info->listentry))
5561 list_del(&lun_info->listentry);
5562 mempool_free(lun_info, phba->device_data_mem_pool);
5563 return;
5564}
5565
5566/**
5567 * __lpfc_get_device_data - returns the device data for the specified lun
5568 * @pha: Pointer to host bus adapter structure.
5569 * @list: Point to list to search.
5570 * @vport_wwpn: Pointer to vport's wwpn information
5571 * @target_wwpn: Pointer to target's wwpn information
5572 * @lun: Lun on target
5573 *
5574 * This routine searches the list passed for the specified lun's device data.
5575 * This function does not hold locks, it is the responsibility of the caller
5576 * to ensure the proper lock is held before calling the function.
5577 *
5578 * Return codes:
5579 * NULL - Error
5580 * Pointer to lpfc_device_data - Success
5581 **/
5582struct lpfc_device_data*
5583__lpfc_get_device_data(struct lpfc_hba *phba, struct list_head *list,
5584 struct lpfc_name *vport_wwpn,
5585 struct lpfc_name *target_wwpn, uint64_t lun)
5586{
5587
5588 struct lpfc_device_data *lun_info;
5589
5590 if (unlikely(!phba) || !list || !vport_wwpn || !target_wwpn ||
James Smartf38fa0b2014-04-04 13:52:21 -04005591 !phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005592 return NULL;
5593
5594 /* Check to see if the lun is already enabled for OAS. */
5595
5596 list_for_each_entry(lun_info, list, listentry) {
5597 if ((memcmp(&lun_info->device_id.vport_wwpn, vport_wwpn,
5598 sizeof(struct lpfc_name)) == 0) &&
5599 (memcmp(&lun_info->device_id.target_wwpn, target_wwpn,
5600 sizeof(struct lpfc_name)) == 0) &&
5601 (lun_info->device_id.lun == lun))
5602 return lun_info;
5603 }
5604
5605 return NULL;
5606}
5607
5608/**
5609 * lpfc_find_next_oas_lun - searches for the next oas lun
5610 * @pha: Pointer to host bus adapter structure.
5611 * @vport_wwpn: Pointer to vport's wwpn information
5612 * @target_wwpn: Pointer to target's wwpn information
5613 * @starting_lun: Pointer to the lun to start searching for
5614 * @found_vport_wwpn: Pointer to the found lun's vport wwpn information
5615 * @found_target_wwpn: Pointer to the found lun's target wwpn information
5616 * @found_lun: Pointer to the found lun.
5617 * @found_lun_status: Pointer to status of the found lun.
5618 *
5619 * This routine searches the luns list for the specified lun
5620 * or the first lun for the vport/target. If the vport wwpn contains
5621 * a zero value then a specific vport is not specified. In this case
5622 * any vport which contains the lun will be considered a match. If the
5623 * target wwpn contains a zero value then a specific target is not specified.
5624 * In this case any target which contains the lun will be considered a
5625 * match. If the lun is found, the lun, vport wwpn, target wwpn and lun status
5626 * are returned. The function will also return the next lun if available.
5627 * If the next lun is not found, starting_lun parameter will be set to
5628 * NO_MORE_OAS_LUN.
5629 *
5630 * Return codes:
5631 * non-0 - Error
5632 * 0 - Success
5633 **/
5634bool
5635lpfc_find_next_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
5636 struct lpfc_name *target_wwpn, uint64_t *starting_lun,
5637 struct lpfc_name *found_vport_wwpn,
5638 struct lpfc_name *found_target_wwpn,
5639 uint64_t *found_lun,
James Smartb5749fe2016-12-19 15:07:26 -08005640 uint32_t *found_lun_status,
5641 uint32_t *found_lun_pri)
James Smart1ba981f2014-02-20 09:56:45 -05005642{
5643
5644 unsigned long flags;
5645 struct lpfc_device_data *lun_info;
5646 struct lpfc_device_id *device_id;
5647 uint64_t lun;
5648 bool found = false;
5649
5650 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
5651 !starting_lun || !found_vport_wwpn ||
5652 !found_target_wwpn || !found_lun || !found_lun_status ||
5653 (*starting_lun == NO_MORE_OAS_LUN) ||
James Smartf38fa0b2014-04-04 13:52:21 -04005654 !phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005655 return false;
5656
5657 lun = *starting_lun;
5658 *found_lun = NO_MORE_OAS_LUN;
5659 *starting_lun = NO_MORE_OAS_LUN;
5660
5661 /* Search for lun or the lun closet in value */
5662
5663 spin_lock_irqsave(&phba->devicelock, flags);
5664 list_for_each_entry(lun_info, &phba->luns, listentry) {
5665 if (((wwn_to_u64(vport_wwpn->u.wwn) == 0) ||
5666 (memcmp(&lun_info->device_id.vport_wwpn, vport_wwpn,
5667 sizeof(struct lpfc_name)) == 0)) &&
5668 ((wwn_to_u64(target_wwpn->u.wwn) == 0) ||
5669 (memcmp(&lun_info->device_id.target_wwpn, target_wwpn,
5670 sizeof(struct lpfc_name)) == 0)) &&
5671 (lun_info->oas_enabled)) {
5672 device_id = &lun_info->device_id;
5673 if ((!found) &&
5674 ((lun == FIND_FIRST_OAS_LUN) ||
5675 (device_id->lun == lun))) {
5676 *found_lun = device_id->lun;
5677 memcpy(found_vport_wwpn,
5678 &device_id->vport_wwpn,
5679 sizeof(struct lpfc_name));
5680 memcpy(found_target_wwpn,
5681 &device_id->target_wwpn,
5682 sizeof(struct lpfc_name));
5683 if (lun_info->available)
5684 *found_lun_status =
5685 OAS_LUN_STATUS_EXISTS;
5686 else
5687 *found_lun_status = 0;
James Smartb5749fe2016-12-19 15:07:26 -08005688 *found_lun_pri = lun_info->priority;
James Smart1ba981f2014-02-20 09:56:45 -05005689 if (phba->cfg_oas_flags & OAS_FIND_ANY_VPORT)
5690 memset(vport_wwpn, 0x0,
5691 sizeof(struct lpfc_name));
5692 if (phba->cfg_oas_flags & OAS_FIND_ANY_TARGET)
5693 memset(target_wwpn, 0x0,
5694 sizeof(struct lpfc_name));
5695 found = true;
5696 } else if (found) {
5697 *starting_lun = device_id->lun;
5698 memcpy(vport_wwpn, &device_id->vport_wwpn,
5699 sizeof(struct lpfc_name));
5700 memcpy(target_wwpn, &device_id->target_wwpn,
5701 sizeof(struct lpfc_name));
5702 break;
5703 }
5704 }
5705 }
5706 spin_unlock_irqrestore(&phba->devicelock, flags);
5707 return found;
5708}
5709
5710/**
5711 * lpfc_enable_oas_lun - enables a lun for OAS operations
5712 * @pha: Pointer to host bus adapter structure.
5713 * @vport_wwpn: Pointer to vport's wwpn information
5714 * @target_wwpn: Pointer to target's wwpn information
5715 * @lun: Lun
5716 *
5717 * This routine enables a lun for oas operations. The routines does so by
5718 * doing the following :
5719 *
5720 * 1) Checks to see if the device data for the lun has been created.
5721 * 2) If found, sets the OAS enabled flag if not set and returns.
5722 * 3) Otherwise, creates a device data structure.
5723 * 4) If successfully created, indicates the device data is for an OAS lun,
5724 * indicates the lun is not available and add to the list of luns.
5725 *
5726 * Return codes:
5727 * false - Error
5728 * true - Success
5729 **/
5730bool
5731lpfc_enable_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
James Smartc92c8412016-07-06 12:36:05 -07005732 struct lpfc_name *target_wwpn, uint64_t lun, uint8_t pri)
James Smart1ba981f2014-02-20 09:56:45 -05005733{
5734
5735 struct lpfc_device_data *lun_info;
5736 unsigned long flags;
5737
5738 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
James Smartf38fa0b2014-04-04 13:52:21 -04005739 !phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005740 return false;
5741
5742 spin_lock_irqsave(&phba->devicelock, flags);
5743
5744 /* Check to see if the device data for the lun has been created */
5745 lun_info = __lpfc_get_device_data(phba, &phba->luns, vport_wwpn,
5746 target_wwpn, lun);
5747 if (lun_info) {
5748 if (!lun_info->oas_enabled)
5749 lun_info->oas_enabled = true;
James Smartb5749fe2016-12-19 15:07:26 -08005750 lun_info->priority = pri;
James Smart1ba981f2014-02-20 09:56:45 -05005751 spin_unlock_irqrestore(&phba->devicelock, flags);
5752 return true;
5753 }
5754
5755 /* Create an lun info structure and add to list of luns */
5756 lun_info = lpfc_create_device_data(phba, vport_wwpn, target_wwpn, lun,
James Smartb5749fe2016-12-19 15:07:26 -08005757 pri, false);
James Smart1ba981f2014-02-20 09:56:45 -05005758 if (lun_info) {
5759 lun_info->oas_enabled = true;
James Smartc92c8412016-07-06 12:36:05 -07005760 lun_info->priority = pri;
James Smart1ba981f2014-02-20 09:56:45 -05005761 lun_info->available = false;
5762 list_add_tail(&lun_info->listentry, &phba->luns);
5763 spin_unlock_irqrestore(&phba->devicelock, flags);
5764 return true;
5765 }
5766 spin_unlock_irqrestore(&phba->devicelock, flags);
5767 return false;
5768}
5769
5770/**
5771 * lpfc_disable_oas_lun - disables a lun for OAS operations
5772 * @pha: Pointer to host bus adapter structure.
5773 * @vport_wwpn: Pointer to vport's wwpn information
5774 * @target_wwpn: Pointer to target's wwpn information
5775 * @lun: Lun
5776 *
5777 * This routine disables a lun for oas operations. The routines does so by
5778 * doing the following :
5779 *
5780 * 1) Checks to see if the device data for the lun is created.
5781 * 2) If present, clears the flag indicating this lun is for OAS.
5782 * 3) If the lun is not available by the system, the device data is
5783 * freed.
5784 *
5785 * Return codes:
5786 * false - Error
5787 * true - Success
5788 **/
5789bool
5790lpfc_disable_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
James Smartb5749fe2016-12-19 15:07:26 -08005791 struct lpfc_name *target_wwpn, uint64_t lun, uint8_t pri)
James Smart1ba981f2014-02-20 09:56:45 -05005792{
5793
5794 struct lpfc_device_data *lun_info;
5795 unsigned long flags;
5796
5797 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
James Smartf38fa0b2014-04-04 13:52:21 -04005798 !phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005799 return false;
5800
5801 spin_lock_irqsave(&phba->devicelock, flags);
5802
5803 /* Check to see if the lun is available. */
5804 lun_info = __lpfc_get_device_data(phba,
5805 &phba->luns, vport_wwpn,
5806 target_wwpn, lun);
5807 if (lun_info) {
5808 lun_info->oas_enabled = false;
James Smartb5749fe2016-12-19 15:07:26 -08005809 lun_info->priority = pri;
James Smart1ba981f2014-02-20 09:56:45 -05005810 if (!lun_info->available)
5811 lpfc_delete_device_data(phba, lun_info);
5812 spin_unlock_irqrestore(&phba->devicelock, flags);
5813 return true;
5814 }
5815
5816 spin_unlock_irqrestore(&phba->devicelock, flags);
5817 return false;
5818}
James Smart92d7f7b2007-06-17 19:56:38 -05005819
James Smart895427b2017-02-12 13:52:30 -08005820static int
5821lpfc_no_command(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
5822{
5823 return SCSI_MLQUEUE_HOST_BUSY;
5824}
5825
5826static int
5827lpfc_no_handler(struct scsi_cmnd *cmnd)
5828{
5829 return FAILED;
5830}
5831
5832static int
5833lpfc_no_slave(struct scsi_device *sdev)
5834{
5835 return -ENODEV;
5836}
5837
5838struct scsi_host_template lpfc_template_nvme = {
5839 .module = THIS_MODULE,
5840 .name = LPFC_DRIVER_NAME,
5841 .proc_name = LPFC_DRIVER_NAME,
5842 .info = lpfc_info,
5843 .queuecommand = lpfc_no_command,
5844 .eh_abort_handler = lpfc_no_handler,
5845 .eh_device_reset_handler = lpfc_no_handler,
5846 .eh_target_reset_handler = lpfc_no_handler,
5847 .eh_bus_reset_handler = lpfc_no_handler,
5848 .eh_host_reset_handler = lpfc_no_handler,
5849 .slave_alloc = lpfc_no_slave,
5850 .slave_configure = lpfc_no_slave,
5851 .scan_finished = lpfc_scan_finished,
5852 .this_id = -1,
5853 .sg_tablesize = 1,
5854 .cmd_per_lun = 1,
James Smart895427b2017-02-12 13:52:30 -08005855 .shost_attrs = lpfc_hba_attrs,
5856 .max_sectors = 0xFFFF,
5857 .vendor_id = LPFC_NL_VENDOR_ID,
5858 .track_queue_depth = 0,
5859};
5860
James Smart96418b52017-03-04 09:30:31 -08005861struct scsi_host_template lpfc_template_no_hr = {
James Smartea4142f2015-04-07 15:07:13 -04005862 .module = THIS_MODULE,
5863 .name = LPFC_DRIVER_NAME,
James Smart08dcd4c2016-07-06 12:35:59 -07005864 .proc_name = LPFC_DRIVER_NAME,
James Smartea4142f2015-04-07 15:07:13 -04005865 .info = lpfc_info,
5866 .queuecommand = lpfc_queuecommand,
James Smart856984b2017-03-04 09:30:32 -08005867 .eh_timed_out = fc_eh_timed_out,
James Smartea4142f2015-04-07 15:07:13 -04005868 .eh_abort_handler = lpfc_abort_handler,
5869 .eh_device_reset_handler = lpfc_device_reset_handler,
5870 .eh_target_reset_handler = lpfc_target_reset_handler,
5871 .eh_bus_reset_handler = lpfc_bus_reset_handler,
5872 .slave_alloc = lpfc_slave_alloc,
5873 .slave_configure = lpfc_slave_configure,
5874 .slave_destroy = lpfc_slave_destroy,
5875 .scan_finished = lpfc_scan_finished,
5876 .this_id = -1,
5877 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
5878 .cmd_per_lun = LPFC_CMD_PER_LUN,
James Smartea4142f2015-04-07 15:07:13 -04005879 .shost_attrs = lpfc_hba_attrs,
5880 .max_sectors = 0xFFFF,
5881 .vendor_id = LPFC_NL_VENDOR_ID,
5882 .change_queue_depth = scsi_change_queue_depth,
James Smartea4142f2015-04-07 15:07:13 -04005883 .track_queue_depth = 1,
5884};
5885
dea31012005-04-17 16:05:31 -05005886struct scsi_host_template lpfc_template = {
5887 .module = THIS_MODULE,
5888 .name = LPFC_DRIVER_NAME,
James Smart08dcd4c2016-07-06 12:35:59 -07005889 .proc_name = LPFC_DRIVER_NAME,
dea31012005-04-17 16:05:31 -05005890 .info = lpfc_info,
5891 .queuecommand = lpfc_queuecommand,
Christoph Hellwigb6a05c82017-01-30 13:18:58 +01005892 .eh_timed_out = fc_eh_timed_out,
dea31012005-04-17 16:05:31 -05005893 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04005894 .eh_device_reset_handler = lpfc_device_reset_handler,
5895 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart7054a602007-04-25 09:52:34 -04005896 .eh_bus_reset_handler = lpfc_bus_reset_handler,
James Smart27b01b82012-05-09 21:19:44 -04005897 .eh_host_reset_handler = lpfc_host_reset_handler,
dea31012005-04-17 16:05:31 -05005898 .slave_alloc = lpfc_slave_alloc,
5899 .slave_configure = lpfc_slave_configure,
5900 .slave_destroy = lpfc_slave_destroy,
James Smart47a86172007-04-25 09:53:22 -04005901 .scan_finished = lpfc_scan_finished,
dea31012005-04-17 16:05:31 -05005902 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05005903 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
dea31012005-04-17 16:05:31 -05005904 .cmd_per_lun = LPFC_CMD_PER_LUN,
James Smart2e0fef82007-06-17 19:56:36 -05005905 .shost_attrs = lpfc_hba_attrs,
James.Smart@Emulex.Com564b2962005-06-25 10:34:17 -04005906 .max_sectors = 0xFFFF,
James Smartf1c3b0f2009-07-19 10:01:32 -04005907 .vendor_id = LPFC_NL_VENDOR_ID,
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01005908 .change_queue_depth = scsi_change_queue_depth,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01005909 .track_queue_depth = 1,
dea31012005-04-17 16:05:31 -05005910};
James Smart3de2a652007-08-02 11:09:59 -04005911
5912struct scsi_host_template lpfc_vport_template = {
5913 .module = THIS_MODULE,
5914 .name = LPFC_DRIVER_NAME,
James Smart08dcd4c2016-07-06 12:35:59 -07005915 .proc_name = LPFC_DRIVER_NAME,
James Smart3de2a652007-08-02 11:09:59 -04005916 .info = lpfc_info,
5917 .queuecommand = lpfc_queuecommand,
Christoph Hellwigb6a05c82017-01-30 13:18:58 +01005918 .eh_timed_out = fc_eh_timed_out,
James Smart3de2a652007-08-02 11:09:59 -04005919 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04005920 .eh_device_reset_handler = lpfc_device_reset_handler,
5921 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart3de2a652007-08-02 11:09:59 -04005922 .slave_alloc = lpfc_slave_alloc,
5923 .slave_configure = lpfc_slave_configure,
5924 .slave_destroy = lpfc_slave_destroy,
5925 .scan_finished = lpfc_scan_finished,
5926 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05005927 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
James Smart3de2a652007-08-02 11:09:59 -04005928 .cmd_per_lun = LPFC_CMD_PER_LUN,
James Smart3de2a652007-08-02 11:09:59 -04005929 .shost_attrs = lpfc_vport_attrs,
5930 .max_sectors = 0xFFFF,
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01005931 .change_queue_depth = scsi_change_queue_depth,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01005932 .track_queue_depth = 1,
James Smart3de2a652007-08-02 11:09:59 -04005933};