blob: 261d0ebf0093bbfe0306e6b69da3fa3fae592398 [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;
James Smart5e5b5112019-01-28 11:14:22 -0800528 struct lpfc_sli4_hdw_queue *qp;
James Smart1151e3e2011-02-16 12:39:35 -0500529 unsigned long iflag = 0;
James Smart5e5b5112019-01-28 11:14:22 -0800530 int idx;
James Smart1151e3e2011-02-16 12:39:35 -0500531
James Smart895427b2017-02-12 13:52:30 -0800532 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
533 return;
James Smart5e5b5112019-01-28 11:14:22 -0800534
James Smart1151e3e2011-02-16 12:39:35 -0500535 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart5e5b5112019-01-28 11:14:22 -0800536 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
537 qp = &phba->sli4_hba.hdwq[idx];
538
539 spin_lock(&qp->abts_scsi_buf_list_lock);
540 list_for_each_entry_safe(psb, next_psb,
541 &qp->lpfc_abts_scsi_buf_list, list) {
542 if (psb->rdata && psb->rdata->pnode &&
543 psb->rdata->pnode->vport == vport)
544 psb->rdata = NULL;
545 }
546 spin_unlock(&qp->abts_scsi_buf_list_lock);
James Smart1151e3e2011-02-16 12:39:35 -0500547 }
James Smart1151e3e2011-02-16 12:39:35 -0500548 spin_unlock_irqrestore(&phba->hbalock, iflag);
549}
550
551/**
James Smartda0436e2009-05-22 14:51:39 -0400552 * lpfc_sli4_fcp_xri_aborted - Fast-path process of fcp xri abort
553 * @phba: pointer to lpfc hba data structure.
554 * @axri: pointer to the fcp xri abort wcqe structure.
555 *
556 * This routine is invoked by the worker thread to process a SLI4 fast-path
557 * FCP aborted xri.
558 **/
559void
560lpfc_sli4_fcp_xri_aborted(struct lpfc_hba *phba,
James Smart5e5b5112019-01-28 11:14:22 -0800561 struct sli4_wcqe_xri_aborted *axri, int idx)
James Smartda0436e2009-05-22 14:51:39 -0400562{
563 uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
James Smart19ca7602010-11-20 23:11:55 -0500564 uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
James Smartda0436e2009-05-22 14:51:39 -0400565 struct lpfc_scsi_buf *psb, *next_psb;
James Smart5e5b5112019-01-28 11:14:22 -0800566 struct lpfc_sli4_hdw_queue *qp;
James Smartda0436e2009-05-22 14:51:39 -0400567 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -0500568 struct lpfc_iocbq *iocbq;
569 int i;
James Smart19ca7602010-11-20 23:11:55 -0500570 struct lpfc_nodelist *ndlp;
571 int rrq_empty = 0;
James Smart895427b2017-02-12 13:52:30 -0800572 struct lpfc_sli_ring *pring = phba->sli4_hba.els_wq->pring;
James Smartda0436e2009-05-22 14:51:39 -0400573
James Smart895427b2017-02-12 13:52:30 -0800574 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
575 return;
James Smart5e5b5112019-01-28 11:14:22 -0800576
577 qp = &phba->sli4_hba.hdwq[idx];
James Smart0f65ff62010-02-26 14:14:23 -0500578 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart5e5b5112019-01-28 11:14:22 -0800579 spin_lock(&qp->abts_scsi_buf_list_lock);
James Smartda0436e2009-05-22 14:51:39 -0400580 list_for_each_entry_safe(psb, next_psb,
James Smart5e5b5112019-01-28 11:14:22 -0800581 &qp->lpfc_abts_scsi_buf_list, list) {
James Smartda0436e2009-05-22 14:51:39 -0400582 if (psb->cur_iocbq.sli4_xritag == xri) {
583 list_del(&psb->list);
James Smart5e5b5112019-01-28 11:14:22 -0800584 qp->abts_scsi_io_bufs--;
James Smart341af102010-01-26 23:07:37 -0500585 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400586 psb->status = IOSTAT_SUCCESS;
James Smart0f65ff62010-02-26 14:14:23 -0500587 spin_unlock(
James Smart5e5b5112019-01-28 11:14:22 -0800588 &qp->abts_scsi_buf_list_lock);
James Smart1151e3e2011-02-16 12:39:35 -0500589 if (psb->rdata && psb->rdata->pnode)
590 ndlp = psb->rdata->pnode;
591 else
592 ndlp = NULL;
593
James Smart19ca7602010-11-20 23:11:55 -0500594 rrq_empty = list_empty(&phba->active_rrq_list);
James Smart0f65ff62010-02-26 14:14:23 -0500595 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartcb69f7d2011-12-13 13:21:57 -0500596 if (ndlp) {
James Smartee0f4fe2012-05-09 21:19:14 -0400597 lpfc_set_rrq_active(phba, ndlp,
598 psb->cur_iocbq.sli4_lxritag, rxid, 1);
James Smartcb69f7d2011-12-13 13:21:57 -0500599 lpfc_sli4_abts_err_handler(phba, ndlp, axri);
600 }
James Smartda0436e2009-05-22 14:51:39 -0400601 lpfc_release_scsi_buf_s4(phba, psb);
James Smart19ca7602010-11-20 23:11:55 -0500602 if (rrq_empty)
603 lpfc_worker_wake_up(phba);
James Smartda0436e2009-05-22 14:51:39 -0400604 return;
605 }
606 }
James Smart5e5b5112019-01-28 11:14:22 -0800607 spin_unlock(&qp->abts_scsi_buf_list_lock);
James Smart0f65ff62010-02-26 14:14:23 -0500608 for (i = 1; i <= phba->sli.last_iotag; i++) {
609 iocbq = phba->sli.iocbq_lookup[i];
610
611 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
612 (iocbq->iocb_flag & LPFC_IO_LIBDFC))
613 continue;
614 if (iocbq->sli4_xritag != xri)
615 continue;
616 psb = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
617 psb->exch_busy = 0;
618 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart0e9bb8d2013-03-01 16:35:12 -0500619 if (!list_empty(&pring->txq))
James Smart589a52d2010-07-14 15:30:54 -0400620 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -0500621 return;
622
623 }
624 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartda0436e2009-05-22 14:51:39 -0400625}
626
627/**
James Smart19ca7602010-11-20 23:11:55 -0500628 * lpfc_get_scsi_buf_s3 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
James Smart3772a992009-05-22 14:50:54 -0400629 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -0500630 *
631 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
632 * and returns to caller.
633 *
634 * Return codes:
635 * NULL - Error
636 * Pointer to lpfc_scsi_buf - Success
637 **/
Adrian Bunk455c53e2006-01-06 20:21:28 +0100638static struct lpfc_scsi_buf*
James Smart19ca7602010-11-20 23:11:55 -0500639lpfc_get_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -0500640{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400641 struct lpfc_scsi_buf * lpfc_cmd = NULL;
James Smarta40fc5f2013-04-17 20:17:40 -0400642 struct list_head *scsi_buf_list_get = &phba->lpfc_scsi_buf_list_get;
James Smart164cecd2013-09-06 12:22:38 -0400643 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500644
James Smart164cecd2013-09-06 12:22:38 -0400645 spin_lock_irqsave(&phba->scsi_buf_list_get_lock, iflag);
James Smarta40fc5f2013-04-17 20:17:40 -0400646 list_remove_head(scsi_buf_list_get, lpfc_cmd, struct lpfc_scsi_buf,
647 list);
648 if (!lpfc_cmd) {
James Smart164cecd2013-09-06 12:22:38 -0400649 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -0400650 list_splice(&phba->lpfc_scsi_buf_list_put,
651 &phba->lpfc_scsi_buf_list_get);
652 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
653 list_remove_head(scsi_buf_list_get, lpfc_cmd,
654 struct lpfc_scsi_buf, list);
James Smart164cecd2013-09-06 12:22:38 -0400655 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smart1dcb58e2007-04-25 09:51:30 -0400656 }
James Smart164cecd2013-09-06 12:22:38 -0400657 spin_unlock_irqrestore(&phba->scsi_buf_list_get_lock, iflag);
James Smart2a5b7d62018-07-31 17:23:22 -0700658
659 if (lpfc_ndlp_check_qdepth(phba, ndlp) && lpfc_cmd) {
660 atomic_inc(&ndlp->cmd_pending);
661 lpfc_cmd->flags |= LPFC_SBUF_BUMP_QDEPTH;
662 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400663 return lpfc_cmd;
664}
James Smart19ca7602010-11-20 23:11:55 -0500665/**
James Smart5e5b5112019-01-28 11:14:22 -0800666 * lpfc_get_scsi_buf_s4 - Get a scsi buffer from io_buf_list of the HBA
James Smart19ca7602010-11-20 23:11:55 -0500667 * @phba: The HBA for which this call is being executed.
668 *
James Smart5e5b5112019-01-28 11:14:22 -0800669 * This routine removes a scsi buffer from head of @hdwq io_buf_list
James Smart19ca7602010-11-20 23:11:55 -0500670 * and returns to caller.
671 *
672 * Return codes:
673 * NULL - Error
674 * Pointer to lpfc_scsi_buf - Success
675 **/
676static struct lpfc_scsi_buf*
677lpfc_get_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
678{
James Smart3be30e02013-05-31 17:05:17 -0400679 struct lpfc_scsi_buf *lpfc_cmd, *lpfc_cmd_next;
James Smart5e5b5112019-01-28 11:14:22 -0800680 struct lpfc_sli4_hdw_queue *qp;
James Smart164cecd2013-09-06 12:22:38 -0400681 unsigned long iflag = 0;
James Smart0794d602019-01-28 11:14:19 -0800682 struct sli4_sge *sgl;
683 IOCB_t *iocb;
684 dma_addr_t pdma_phys_fcp_rsp;
685 dma_addr_t pdma_phys_fcp_cmd;
James Smart5e5b5112019-01-28 11:14:22 -0800686 uint32_t sgl_size, cpu, idx;
James Smart19ca7602010-11-20 23:11:55 -0500687 int found = 0;
688
James Smart5e5b5112019-01-28 11:14:22 -0800689 cpu = smp_processor_id();
690 if (cpu < phba->cfg_hdw_queue)
691 idx = cpu;
692 else
693 idx = cpu % phba->cfg_hdw_queue;
694
695 qp = &phba->sli4_hba.hdwq[idx];
696 spin_lock_irqsave(&qp->io_buf_list_get_lock, iflag);
James Smart3be30e02013-05-31 17:05:17 -0400697 list_for_each_entry_safe(lpfc_cmd, lpfc_cmd_next,
James Smart5e5b5112019-01-28 11:14:22 -0800698 &qp->lpfc_io_buf_list_get, list) {
James Smart19ca7602010-11-20 23:11:55 -0500699 if (lpfc_test_rrq_active(phba, ndlp,
James Smartee0f4fe2012-05-09 21:19:14 -0400700 lpfc_cmd->cur_iocbq.sli4_lxritag))
James Smart1151e3e2011-02-16 12:39:35 -0500701 continue;
James Smart59c68ea2018-04-09 14:24:25 -0700702 list_del_init(&lpfc_cmd->list);
James Smart5e5b5112019-01-28 11:14:22 -0800703 qp->get_io_bufs--;
James Smart19ca7602010-11-20 23:11:55 -0500704 found = 1;
James Smart1151e3e2011-02-16 12:39:35 -0500705 break;
James Smart19ca7602010-11-20 23:11:55 -0500706 }
James Smarta40fc5f2013-04-17 20:17:40 -0400707 if (!found) {
James Smart5e5b5112019-01-28 11:14:22 -0800708 spin_lock(&qp->io_buf_list_put_lock);
709 list_splice(&qp->lpfc_io_buf_list_put,
710 &qp->lpfc_io_buf_list_get);
711 qp->get_io_bufs += qp->put_io_bufs;
712 INIT_LIST_HEAD(&qp->lpfc_io_buf_list_put);
713 qp->put_io_bufs = 0;
714 spin_unlock(&qp->io_buf_list_put_lock);
James Smart3be30e02013-05-31 17:05:17 -0400715 list_for_each_entry_safe(lpfc_cmd, lpfc_cmd_next,
James Smart5e5b5112019-01-28 11:14:22 -0800716 &qp->lpfc_io_buf_list_get,
James Smart0794d602019-01-28 11:14:19 -0800717 list) {
James Smarta40fc5f2013-04-17 20:17:40 -0400718 if (lpfc_test_rrq_active(
719 phba, ndlp, lpfc_cmd->cur_iocbq.sli4_lxritag))
720 continue;
James Smart59c68ea2018-04-09 14:24:25 -0700721 list_del_init(&lpfc_cmd->list);
James Smart5e5b5112019-01-28 11:14:22 -0800722 qp->get_io_bufs--;
James Smarta40fc5f2013-04-17 20:17:40 -0400723 found = 1;
724 break;
725 }
726 }
James Smart5e5b5112019-01-28 11:14:22 -0800727 spin_unlock_irqrestore(&qp->io_buf_list_get_lock, iflag);
728 if (!found) {
729 qp->empty_io_bufs++;
James Smart1151e3e2011-02-16 12:39:35 -0500730 return NULL;
James Smart5e5b5112019-01-28 11:14:22 -0800731 }
James Smart2a5b7d62018-07-31 17:23:22 -0700732
James Smart0794d602019-01-28 11:14:19 -0800733 sgl_size = phba->cfg_sg_dma_buf_size -
734 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
735
736 /* Setup key fields in buffer that may have been changed
737 * if other protocols used this buffer.
738 */
739 lpfc_cmd->cur_iocbq.iocb_flag = LPFC_IO_FCP;
740 lpfc_cmd->prot_seg_cnt = 0;
741 lpfc_cmd->seg_cnt = 0;
742 lpfc_cmd->waitq = NULL;
743 lpfc_cmd->timeout = 0;
744 lpfc_cmd->flags = 0;
745 lpfc_cmd->start_time = jiffies;
746 lpfc_cmd->waitq = NULL;
James Smart5e5b5112019-01-28 11:14:22 -0800747 lpfc_cmd->cpu = cpu;
James Smart0794d602019-01-28 11:14:19 -0800748#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
749 lpfc_cmd->prot_data_type = 0;
750#endif
James Smart5e5b5112019-01-28 11:14:22 -0800751 lpfc_cmd->hdwq = idx;
James Smart0794d602019-01-28 11:14:19 -0800752
753 lpfc_cmd->fcp_cmnd = (lpfc_cmd->data + sgl_size);
754 lpfc_cmd->fcp_rsp = (struct fcp_rsp *)((uint8_t *)lpfc_cmd->fcp_cmnd +
755 sizeof(struct fcp_cmnd));
756
757 /*
758 * The first two SGEs are the FCP_CMD and FCP_RSP.
759 * The balance are sg list bdes. Initialize the
760 * first two and leave the rest for queuecommand.
761 */
762 sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
763 pdma_phys_fcp_cmd = (lpfc_cmd->dma_handle + sgl_size);
764 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
765 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
766 sgl->word2 = le32_to_cpu(sgl->word2);
767 bf_set(lpfc_sli4_sge_last, sgl, 0);
768 sgl->word2 = cpu_to_le32(sgl->word2);
769 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_cmnd));
770 sgl++;
771
772 /* Setup the physical region for the FCP RSP */
773 pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
774 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
775 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
776 sgl->word2 = le32_to_cpu(sgl->word2);
777 bf_set(lpfc_sli4_sge_last, sgl, 1);
778 sgl->word2 = cpu_to_le32(sgl->word2);
779 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
780
781 /*
782 * Since the IOCB for the FCP I/O is built into this
783 * lpfc_scsi_buf, initialize it with all known data now.
784 */
785 iocb = &lpfc_cmd->cur_iocbq.iocb;
786 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
787 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
788 /* setting the BLP size to 2 * sizeof BDE may not be correct.
789 * We are setting the bpl to point to out sgl. An sgl's
790 * entries are 16 bytes, a bpl entries are 12 bytes.
791 */
792 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
793 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
794 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
795 iocb->ulpBdeCount = 1;
796 iocb->ulpLe = 1;
797 iocb->ulpClass = CLASS3;
798
Dan Carpenter26c724a2018-08-23 16:56:33 +0300799 if (lpfc_ndlp_check_qdepth(phba, ndlp)) {
James Smart2a5b7d62018-07-31 17:23:22 -0700800 atomic_inc(&ndlp->cmd_pending);
801 lpfc_cmd->flags |= LPFC_SBUF_BUMP_QDEPTH;
802 }
James Smarta40fc5f2013-04-17 20:17:40 -0400803 return lpfc_cmd;
James Smart19ca7602010-11-20 23:11:55 -0500804}
805/**
806 * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
807 * @phba: The HBA for which this call is being executed.
808 *
809 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
810 * and returns to caller.
811 *
812 * Return codes:
813 * NULL - Error
814 * Pointer to lpfc_scsi_buf - Success
815 **/
816static struct lpfc_scsi_buf*
817lpfc_get_scsi_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
818{
819 return phba->lpfc_get_scsi_buf(phba, ndlp);
820}
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400821
James Smart9bad7672008-12-04 22:39:02 -0500822/**
James Smart3772a992009-05-22 14:50:54 -0400823 * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
James Smart9bad7672008-12-04 22:39:02 -0500824 * @phba: The Hba for which this call is being executed.
825 * @psb: The scsi buffer which is being released.
826 *
827 * This routine releases @psb scsi buffer by adding it to tail of @phba
828 * lpfc_scsi_buf_list list.
829 **/
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400830static void
James Smart3772a992009-05-22 14:50:54 -0400831lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400832{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500833 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500834
James Smarta40fc5f2013-04-17 20:17:40 -0400835 psb->seg_cnt = 0;
James Smarta40fc5f2013-04-17 20:17:40 -0400836 psb->prot_seg_cnt = 0;
837
838 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400839 psb->pCmd = NULL;
James Smart6a485eb2013-04-17 20:19:00 -0400840 psb->cur_iocbq.iocb_flag = LPFC_IO_FCP;
James Smarta40fc5f2013-04-17 20:17:40 -0400841 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list_put);
842 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
dea31012005-04-17 16:05:31 -0500843}
844
James Smart9bad7672008-12-04 22:39:02 -0500845/**
James Smartda0436e2009-05-22 14:51:39 -0400846 * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
847 * @phba: The Hba for which this call is being executed.
848 * @psb: The scsi buffer which is being released.
849 *
James Smart5e5b5112019-01-28 11:14:22 -0800850 * This routine releases @psb scsi buffer by adding it to tail of @hdwq
851 * io_buf_list list. For SLI4 XRI's are tied to the scsi buffer
James Smartda0436e2009-05-22 14:51:39 -0400852 * and cannot be reused for at least RA_TOV amount of time if it was
853 * aborted.
854 **/
855static void
856lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
857{
James Smart5e5b5112019-01-28 11:14:22 -0800858 struct lpfc_sli4_hdw_queue *qp;
James Smartda0436e2009-05-22 14:51:39 -0400859 unsigned long iflag = 0;
860
James Smarta40fc5f2013-04-17 20:17:40 -0400861 psb->seg_cnt = 0;
James Smarta40fc5f2013-04-17 20:17:40 -0400862 psb->prot_seg_cnt = 0;
863
James Smart5e5b5112019-01-28 11:14:22 -0800864 qp = &phba->sli4_hba.hdwq[psb->hdwq];
James Smart341af102010-01-26 23:07:37 -0500865 if (psb->exch_busy) {
James Smart5e5b5112019-01-28 11:14:22 -0800866 spin_lock_irqsave(&qp->abts_scsi_buf_list_lock, iflag);
James Smartda0436e2009-05-22 14:51:39 -0400867 psb->pCmd = NULL;
James Smart5e5b5112019-01-28 11:14:22 -0800868 list_add_tail(&psb->list, &qp->lpfc_abts_scsi_buf_list);
869 qp->abts_scsi_io_bufs++;
870 spin_unlock_irqrestore(&qp->abts_scsi_buf_list_lock, iflag);
James Smartda0436e2009-05-22 14:51:39 -0400871 } else {
James Smart0794d602019-01-28 11:14:19 -0800872 /* MUST zero fields if buffer is reused by another protocol */
James Smartda0436e2009-05-22 14:51:39 -0400873 psb->pCmd = NULL;
James Smart0794d602019-01-28 11:14:19 -0800874 psb->cur_iocbq.iocb_cmpl = NULL;
James Smart5e5b5112019-01-28 11:14:22 -0800875
876 spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag);
877 list_add_tail(&psb->list, &qp->lpfc_io_buf_list_put);
878 qp->put_io_bufs++;
879 spin_unlock_irqrestore(&qp->io_buf_list_put_lock, iflag);
James Smartda0436e2009-05-22 14:51:39 -0400880 }
881}
882
883/**
James Smart3772a992009-05-22 14:50:54 -0400884 * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
885 * @phba: The Hba for which this call is being executed.
886 * @psb: The scsi buffer which is being released.
887 *
888 * This routine releases @psb scsi buffer by adding it to tail of @phba
889 * lpfc_scsi_buf_list list.
890 **/
891static void
892lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
893{
James Smart2a5b7d62018-07-31 17:23:22 -0700894 if ((psb->flags & LPFC_SBUF_BUMP_QDEPTH) && psb->ndlp)
895 atomic_dec(&psb->ndlp->cmd_pending);
James Smart3772a992009-05-22 14:50:54 -0400896
James Smart2a5b7d62018-07-31 17:23:22 -0700897 psb->flags &= ~LPFC_SBUF_BUMP_QDEPTH;
James Smart3772a992009-05-22 14:50:54 -0400898 phba->lpfc_release_scsi_buf(phba, psb);
899}
900
901/**
902 * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -0500903 * @phba: The Hba for which this call is being executed.
904 * @lpfc_cmd: The scsi buffer which is going to be mapped.
905 *
906 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
James Smart3772a992009-05-22 14:50:54 -0400907 * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -0700908 * through sg elements and format the bde. This routine also initializes all
James Smart3772a992009-05-22 14:50:54 -0400909 * IOCB fields which are dependent on scsi command request buffer.
James Smart9bad7672008-12-04 22:39:02 -0500910 *
911 * Return codes:
912 * 1 - Error
913 * 0 - Success
914 **/
dea31012005-04-17 16:05:31 -0500915static int
James Smart3772a992009-05-22 14:50:54 -0400916lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
dea31012005-04-17 16:05:31 -0500917{
918 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
919 struct scatterlist *sgel = NULL;
920 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart0794d602019-01-28 11:14:19 -0800921 struct ulp_bde64 *bpl = lpfc_cmd->dma_sgl;
James Smart0f65ff62010-02-26 14:14:23 -0500922 struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
dea31012005-04-17 16:05:31 -0500923 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart34b02dc2008-08-24 21:49:55 -0400924 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
dea31012005-04-17 16:05:31 -0500925 dma_addr_t physaddr;
James Smart34b02dc2008-08-24 21:49:55 -0400926 uint32_t num_bde = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500927 int nseg, datadir = scsi_cmnd->sc_data_direction;
dea31012005-04-17 16:05:31 -0500928
929 /*
930 * There are three possibilities here - use scatter-gather segment, use
931 * the single mapping, or neither. Start the lpfc command prep by
932 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
933 * data bde entry.
934 */
935 bpl += 2;
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -0700936 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -0500937 /*
938 * The driver stores the segment count returned from pci_map_sg
939 * because this a count of dma-mappings used to map the use_sg
940 * pages. They are not guaranteed to be the same for those
941 * architectures that implement an IOMMU.
942 */
dea31012005-04-17 16:05:31 -0500943
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -0700944 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
945 scsi_sg_count(scsi_cmnd), datadir);
946 if (unlikely(!nseg))
947 return 1;
948
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500949 lpfc_cmd->seg_cnt = nseg;
dea31012005-04-17 16:05:31 -0500950 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -0400951 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
952 "9064 BLKGRD: %s: Too many sg segments from "
James Smarte2a0a9d2008-12-04 22:40:02 -0500953 "dma_map_sg. Config %d, seg_cnt %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700954 __func__, phba->cfg_sg_seg_cnt,
dea31012005-04-17 16:05:31 -0500955 lpfc_cmd->seg_cnt);
James Smart96f70772013-04-17 20:16:15 -0400956 lpfc_cmd->seg_cnt = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500957 scsi_dma_unmap(scsi_cmnd);
dea31012005-04-17 16:05:31 -0500958 return 1;
959 }
960
961 /*
962 * The driver established a maximum scatter-gather segment count
963 * during probe that limits the number of sg elements in any
964 * single scsi command. Just run through the seg_cnt and format
965 * the bde's.
James Smart34b02dc2008-08-24 21:49:55 -0400966 * When using SLI-3 the driver will try to fit all the BDEs into
967 * the IOCB. If it can't then the BDEs get added to a BPL as it
968 * does for SLI-2 mode.
dea31012005-04-17 16:05:31 -0500969 */
James Smart34b02dc2008-08-24 21:49:55 -0400970 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
dea31012005-04-17 16:05:31 -0500971 physaddr = sg_dma_address(sgel);
James Smart34b02dc2008-08-24 21:49:55 -0400972 if (phba->sli_rev == 3 &&
James Smarte2a0a9d2008-12-04 22:40:02 -0500973 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
James Smart0f65ff62010-02-26 14:14:23 -0500974 !(iocbq->iocb_flag & DSS_SECURITY_OP) &&
James Smart34b02dc2008-08-24 21:49:55 -0400975 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
976 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
977 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
978 data_bde->addrLow = putPaddrLow(physaddr);
979 data_bde->addrHigh = putPaddrHigh(physaddr);
980 data_bde++;
981 } else {
982 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
983 bpl->tus.f.bdeSize = sg_dma_len(sgel);
984 bpl->tus.w = le32_to_cpu(bpl->tus.w);
985 bpl->addrLow =
986 le32_to_cpu(putPaddrLow(physaddr));
987 bpl->addrHigh =
988 le32_to_cpu(putPaddrHigh(physaddr));
989 bpl++;
990 }
dea31012005-04-17 16:05:31 -0500991 }
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -0700992 }
dea31012005-04-17 16:05:31 -0500993
994 /*
995 * Finish initializing those IOCB fields that are dependent on the
James Smart34b02dc2008-08-24 21:49:55 -0400996 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
997 * explicitly reinitialized and for SLI-3 the extended bde count is
998 * explicitly reinitialized since all iocb memory resources are reused.
dea31012005-04-17 16:05:31 -0500999 */
James Smarte2a0a9d2008-12-04 22:40:02 -05001000 if (phba->sli_rev == 3 &&
James Smart0f65ff62010-02-26 14:14:23 -05001001 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
1002 !(iocbq->iocb_flag & DSS_SECURITY_OP)) {
James Smart34b02dc2008-08-24 21:49:55 -04001003 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
1004 /*
1005 * The extended IOCB format can only fit 3 BDE or a BPL.
1006 * This I/O has more than 3 BDE so the 1st data bde will
1007 * be a BPL that is filled in here.
1008 */
1009 physaddr = lpfc_cmd->dma_handle;
1010 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
1011 data_bde->tus.f.bdeSize = (num_bde *
1012 sizeof(struct ulp_bde64));
1013 physaddr += (sizeof(struct fcp_cmnd) +
1014 sizeof(struct fcp_rsp) +
1015 (2 * sizeof(struct ulp_bde64)));
1016 data_bde->addrHigh = putPaddrHigh(physaddr);
1017 data_bde->addrLow = putPaddrLow(physaddr);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001018 /* ebde count includes the response bde and data bpl */
James Smart34b02dc2008-08-24 21:49:55 -04001019 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
1020 } else {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001021 /* ebde count includes the response bde and data bdes */
James Smart34b02dc2008-08-24 21:49:55 -04001022 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1023 }
1024 } else {
1025 iocb_cmd->un.fcpi64.bdl.bdeSize =
1026 ((num_bde + 2) * sizeof(struct ulp_bde64));
James Smart0f65ff62010-02-26 14:14:23 -05001027 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
James Smart34b02dc2008-08-24 21:49:55 -04001028 }
James Smart09372822008-01-11 01:52:54 -05001029 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
James Smarte2a0a9d2008-12-04 22:40:02 -05001030
1031 /*
1032 * Due to difference in data length between DIF/non-DIF paths,
1033 * we need to set word 4 of IOCB here
1034 */
James Smarta257bf92009-04-06 18:48:10 -04001035 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001036 return 0;
1037}
1038
James Smartf9bb2da2011-10-10 21:34:11 -04001039#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001040
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -07001041/* Return BG_ERR_INIT if error injection is detected by Initiator */
James Smart9a6b09c2012-03-01 22:37:42 -05001042#define BG_ERR_INIT 0x1
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -07001043/* Return BG_ERR_TGT if error injection is detected by Target */
James Smart9a6b09c2012-03-01 22:37:42 -05001044#define BG_ERR_TGT 0x2
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -07001045/* Return BG_ERR_SWAP if swapping CSUM<-->CRC is required for error injection */
James Smart9a6b09c2012-03-01 22:37:42 -05001046#define BG_ERR_SWAP 0x10
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -07001047/**
1048 * Return BG_ERR_CHECK if disabling Guard/Ref/App checking is required for
1049 * error injection
1050 **/
James Smart9a6b09c2012-03-01 22:37:42 -05001051#define BG_ERR_CHECK 0x20
James Smartacd68592012-01-18 16:25:09 -05001052
1053/**
1054 * lpfc_bg_err_inject - Determine if we should inject an error
1055 * @phba: The Hba for which this call is being executed.
James Smartf9bb2da2011-10-10 21:34:11 -04001056 * @sc: The SCSI command to examine
1057 * @reftag: (out) BlockGuard reference tag for transmitted data
1058 * @apptag: (out) BlockGuard application tag for transmitted data
1059 * @new_guard (in) Value to replace CRC with if needed
1060 *
James Smart9a6b09c2012-03-01 22:37:42 -05001061 * Returns BG_ERR_* bit mask or 0 if request ignored
James Smartacd68592012-01-18 16:25:09 -05001062 **/
James Smartf9bb2da2011-10-10 21:34:11 -04001063static int
1064lpfc_bg_err_inject(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1065 uint32_t *reftag, uint16_t *apptag, uint32_t new_guard)
1066{
1067 struct scatterlist *sgpe; /* s/g prot entry */
James Smart9a6b09c2012-03-01 22:37:42 -05001068 struct lpfc_scsi_buf *lpfc_cmd = NULL;
James Smartacd68592012-01-18 16:25:09 -05001069 struct scsi_dif_tuple *src = NULL;
James Smart4ac9b222012-03-01 22:38:29 -05001070 struct lpfc_nodelist *ndlp;
1071 struct lpfc_rport_data *rdata;
James Smartf9bb2da2011-10-10 21:34:11 -04001072 uint32_t op = scsi_get_prot_op(sc);
1073 uint32_t blksize;
1074 uint32_t numblks;
1075 sector_t lba;
1076 int rc = 0;
James Smartacd68592012-01-18 16:25:09 -05001077 int blockoff = 0;
James Smartf9bb2da2011-10-10 21:34:11 -04001078
1079 if (op == SCSI_PROT_NORMAL)
1080 return 0;
1081
James Smartacd68592012-01-18 16:25:09 -05001082 sgpe = scsi_prot_sglist(sc);
James Smartf9bb2da2011-10-10 21:34:11 -04001083 lba = scsi_get_lba(sc);
James Smart4ac9b222012-03-01 22:38:29 -05001084
1085 /* First check if we need to match the LBA */
James Smartf9bb2da2011-10-10 21:34:11 -04001086 if (phba->lpfc_injerr_lba != LPFC_INJERR_LBA_OFF) {
1087 blksize = lpfc_cmd_blksize(sc);
1088 numblks = (scsi_bufflen(sc) + blksize - 1) / blksize;
1089
1090 /* Make sure we have the right LBA if one is specified */
1091 if ((phba->lpfc_injerr_lba < lba) ||
1092 (phba->lpfc_injerr_lba >= (lba + numblks)))
1093 return 0;
James Smartacd68592012-01-18 16:25:09 -05001094 if (sgpe) {
1095 blockoff = phba->lpfc_injerr_lba - lba;
1096 numblks = sg_dma_len(sgpe) /
1097 sizeof(struct scsi_dif_tuple);
1098 if (numblks < blockoff)
1099 blockoff = numblks;
James Smartacd68592012-01-18 16:25:09 -05001100 }
James Smartf9bb2da2011-10-10 21:34:11 -04001101 }
1102
James Smart4ac9b222012-03-01 22:38:29 -05001103 /* Next check if we need to match the remote NPortID or WWPN */
James Smart1ba981f2014-02-20 09:56:45 -05001104 rdata = lpfc_rport_data_from_scsi_device(sc->device);
James Smart4ac9b222012-03-01 22:38:29 -05001105 if (rdata && rdata->pnode) {
1106 ndlp = rdata->pnode;
1107
1108 /* Make sure we have the right NPortID if one is specified */
1109 if (phba->lpfc_injerr_nportid &&
1110 (phba->lpfc_injerr_nportid != ndlp->nlp_DID))
1111 return 0;
1112
1113 /*
1114 * Make sure we have the right WWPN if one is specified.
1115 * wwn[0] should be a non-zero NAA in a good WWPN.
1116 */
1117 if (phba->lpfc_injerr_wwpn.u.wwn[0] &&
1118 (memcmp(&ndlp->nlp_portname, &phba->lpfc_injerr_wwpn,
1119 sizeof(struct lpfc_name)) != 0))
1120 return 0;
1121 }
1122
1123 /* Setup a ptr to the protection data if the SCSI host provides it */
1124 if (sgpe) {
1125 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
1126 src += blockoff;
1127 lpfc_cmd = (struct lpfc_scsi_buf *)sc->host_scribble;
1128 }
1129
James Smartf9bb2da2011-10-10 21:34:11 -04001130 /* Should we change the Reference Tag */
1131 if (reftag) {
James Smartacd68592012-01-18 16:25:09 -05001132 if (phba->lpfc_injerr_wref_cnt) {
1133 switch (op) {
1134 case SCSI_PROT_WRITE_PASS:
James Smart9a6b09c2012-03-01 22:37:42 -05001135 if (src) {
1136 /*
1137 * For WRITE_PASS, force the error
1138 * to be sent on the wire. It should
1139 * be detected by the Target.
1140 * If blockoff != 0 error will be
1141 * inserted in middle of the IO.
1142 */
James Smartf9bb2da2011-10-10 21:34:11 -04001143
James Smartacd68592012-01-18 16:25:09 -05001144 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1145 "9076 BLKGRD: Injecting reftag error: "
1146 "write lba x%lx + x%x oldrefTag x%x\n",
1147 (unsigned long)lba, blockoff,
James Smart9a6b09c2012-03-01 22:37:42 -05001148 be32_to_cpu(src->ref_tag));
James Smartacd68592012-01-18 16:25:09 -05001149
1150 /*
James Smart9a6b09c2012-03-01 22:37:42 -05001151 * Save the old ref_tag so we can
1152 * restore it on completion.
James Smartacd68592012-01-18 16:25:09 -05001153 */
James Smart9a6b09c2012-03-01 22:37:42 -05001154 if (lpfc_cmd) {
1155 lpfc_cmd->prot_data_type =
1156 LPFC_INJERR_REFTAG;
1157 lpfc_cmd->prot_data_segment =
1158 src;
1159 lpfc_cmd->prot_data =
1160 src->ref_tag;
1161 }
1162 src->ref_tag = cpu_to_be32(0xDEADBEEF);
James Smartacd68592012-01-18 16:25:09 -05001163 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001164 if (phba->lpfc_injerr_wref_cnt == 0) {
1165 phba->lpfc_injerr_nportid = 0;
1166 phba->lpfc_injerr_lba =
1167 LPFC_INJERR_LBA_OFF;
1168 memset(&phba->lpfc_injerr_wwpn,
1169 0, sizeof(struct lpfc_name));
1170 }
James Smart9a6b09c2012-03-01 22:37:42 -05001171 rc = BG_ERR_TGT | BG_ERR_CHECK;
1172
James Smartacd68592012-01-18 16:25:09 -05001173 break;
1174 }
1175 /* Drop thru */
James Smart9a6b09c2012-03-01 22:37:42 -05001176 case SCSI_PROT_WRITE_INSERT:
1177 /*
1178 * For WRITE_INSERT, force the error
1179 * to be sent on the wire. It should be
1180 * detected by the Target.
1181 */
1182 /* DEADBEEF will be the reftag on the wire */
1183 *reftag = 0xDEADBEEF;
1184 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001185 if (phba->lpfc_injerr_wref_cnt == 0) {
1186 phba->lpfc_injerr_nportid = 0;
1187 phba->lpfc_injerr_lba =
1188 LPFC_INJERR_LBA_OFF;
1189 memset(&phba->lpfc_injerr_wwpn,
1190 0, sizeof(struct lpfc_name));
1191 }
James Smart9a6b09c2012-03-01 22:37:42 -05001192 rc = BG_ERR_TGT | BG_ERR_CHECK;
1193
1194 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1195 "9078 BLKGRD: Injecting reftag error: "
1196 "write lba x%lx\n", (unsigned long)lba);
1197 break;
James Smartacd68592012-01-18 16:25:09 -05001198 case SCSI_PROT_WRITE_STRIP:
1199 /*
1200 * For WRITE_STRIP and WRITE_PASS,
1201 * force the error on data
1202 * being copied from SLI-Host to SLI-Port.
1203 */
1204 *reftag = 0xDEADBEEF;
1205 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001206 if (phba->lpfc_injerr_wref_cnt == 0) {
1207 phba->lpfc_injerr_nportid = 0;
1208 phba->lpfc_injerr_lba =
1209 LPFC_INJERR_LBA_OFF;
1210 memset(&phba->lpfc_injerr_wwpn,
1211 0, sizeof(struct lpfc_name));
1212 }
James Smartacd68592012-01-18 16:25:09 -05001213 rc = BG_ERR_INIT;
1214
1215 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1216 "9077 BLKGRD: Injecting reftag error: "
1217 "write lba x%lx\n", (unsigned long)lba);
1218 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001219 }
James Smartacd68592012-01-18 16:25:09 -05001220 }
1221 if (phba->lpfc_injerr_rref_cnt) {
1222 switch (op) {
1223 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001224 case SCSI_PROT_READ_STRIP:
1225 case SCSI_PROT_READ_PASS:
1226 /*
1227 * For READ_STRIP and READ_PASS, force the
1228 * error on data being read off the wire. It
1229 * should force an IO error to the driver.
1230 */
James Smartf9bb2da2011-10-10 21:34:11 -04001231 *reftag = 0xDEADBEEF;
1232 phba->lpfc_injerr_rref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001233 if (phba->lpfc_injerr_rref_cnt == 0) {
1234 phba->lpfc_injerr_nportid = 0;
1235 phba->lpfc_injerr_lba =
1236 LPFC_INJERR_LBA_OFF;
1237 memset(&phba->lpfc_injerr_wwpn,
1238 0, sizeof(struct lpfc_name));
1239 }
James Smartacd68592012-01-18 16:25:09 -05001240 rc = BG_ERR_INIT;
James Smartf9bb2da2011-10-10 21:34:11 -04001241
1242 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smartacd68592012-01-18 16:25:09 -05001243 "9079 BLKGRD: Injecting reftag error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001244 "read lba x%lx\n", (unsigned long)lba);
James Smartacd68592012-01-18 16:25:09 -05001245 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001246 }
1247 }
1248 }
1249
1250 /* Should we change the Application Tag */
1251 if (apptag) {
James Smartacd68592012-01-18 16:25:09 -05001252 if (phba->lpfc_injerr_wapp_cnt) {
1253 switch (op) {
1254 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001255 if (src) {
James Smart9a6b09c2012-03-01 22:37:42 -05001256 /*
1257 * For WRITE_PASS, force the error
1258 * to be sent on the wire. It should
1259 * be detected by the Target.
1260 * If blockoff != 0 error will be
1261 * inserted in middle of the IO.
1262 */
1263
James Smartacd68592012-01-18 16:25:09 -05001264 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1265 "9080 BLKGRD: Injecting apptag error: "
1266 "write lba x%lx + x%x oldappTag x%x\n",
1267 (unsigned long)lba, blockoff,
James Smart9a6b09c2012-03-01 22:37:42 -05001268 be16_to_cpu(src->app_tag));
James Smartacd68592012-01-18 16:25:09 -05001269
1270 /*
James Smart9a6b09c2012-03-01 22:37:42 -05001271 * Save the old app_tag so we can
1272 * restore it on completion.
James Smartacd68592012-01-18 16:25:09 -05001273 */
James Smart9a6b09c2012-03-01 22:37:42 -05001274 if (lpfc_cmd) {
1275 lpfc_cmd->prot_data_type =
1276 LPFC_INJERR_APPTAG;
1277 lpfc_cmd->prot_data_segment =
1278 src;
1279 lpfc_cmd->prot_data =
1280 src->app_tag;
1281 }
1282 src->app_tag = cpu_to_be16(0xDEAD);
James Smartacd68592012-01-18 16:25:09 -05001283 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001284 if (phba->lpfc_injerr_wapp_cnt == 0) {
1285 phba->lpfc_injerr_nportid = 0;
1286 phba->lpfc_injerr_lba =
1287 LPFC_INJERR_LBA_OFF;
1288 memset(&phba->lpfc_injerr_wwpn,
1289 0, sizeof(struct lpfc_name));
1290 }
James Smart9a6b09c2012-03-01 22:37:42 -05001291 rc = BG_ERR_TGT | BG_ERR_CHECK;
James Smartacd68592012-01-18 16:25:09 -05001292 break;
1293 }
1294 /* Drop thru */
James Smart9a6b09c2012-03-01 22:37:42 -05001295 case SCSI_PROT_WRITE_INSERT:
1296 /*
1297 * For WRITE_INSERT, force the
1298 * error to be sent on the wire. It should be
1299 * detected by the Target.
1300 */
1301 /* DEAD will be the apptag on the wire */
1302 *apptag = 0xDEAD;
1303 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001304 if (phba->lpfc_injerr_wapp_cnt == 0) {
1305 phba->lpfc_injerr_nportid = 0;
1306 phba->lpfc_injerr_lba =
1307 LPFC_INJERR_LBA_OFF;
1308 memset(&phba->lpfc_injerr_wwpn,
1309 0, sizeof(struct lpfc_name));
1310 }
James Smart9a6b09c2012-03-01 22:37:42 -05001311 rc = BG_ERR_TGT | BG_ERR_CHECK;
1312
1313 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1314 "0813 BLKGRD: Injecting apptag error: "
1315 "write lba x%lx\n", (unsigned long)lba);
1316 break;
James Smartacd68592012-01-18 16:25:09 -05001317 case SCSI_PROT_WRITE_STRIP:
1318 /*
1319 * For WRITE_STRIP and WRITE_PASS,
1320 * force the error on data
1321 * being copied from SLI-Host to SLI-Port.
1322 */
1323 *apptag = 0xDEAD;
1324 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001325 if (phba->lpfc_injerr_wapp_cnt == 0) {
1326 phba->lpfc_injerr_nportid = 0;
1327 phba->lpfc_injerr_lba =
1328 LPFC_INJERR_LBA_OFF;
1329 memset(&phba->lpfc_injerr_wwpn,
1330 0, sizeof(struct lpfc_name));
1331 }
James Smartacd68592012-01-18 16:25:09 -05001332 rc = BG_ERR_INIT;
1333
1334 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1335 "0812 BLKGRD: Injecting apptag error: "
1336 "write lba x%lx\n", (unsigned long)lba);
1337 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001338 }
James Smartacd68592012-01-18 16:25:09 -05001339 }
1340 if (phba->lpfc_injerr_rapp_cnt) {
1341 switch (op) {
1342 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001343 case SCSI_PROT_READ_STRIP:
1344 case SCSI_PROT_READ_PASS:
1345 /*
1346 * For READ_STRIP and READ_PASS, force the
1347 * error on data being read off the wire. It
1348 * should force an IO error to the driver.
1349 */
James Smartf9bb2da2011-10-10 21:34:11 -04001350 *apptag = 0xDEAD;
1351 phba->lpfc_injerr_rapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001352 if (phba->lpfc_injerr_rapp_cnt == 0) {
1353 phba->lpfc_injerr_nportid = 0;
1354 phba->lpfc_injerr_lba =
1355 LPFC_INJERR_LBA_OFF;
1356 memset(&phba->lpfc_injerr_wwpn,
1357 0, sizeof(struct lpfc_name));
1358 }
James Smartacd68592012-01-18 16:25:09 -05001359 rc = BG_ERR_INIT;
James Smartf9bb2da2011-10-10 21:34:11 -04001360
1361 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smartacd68592012-01-18 16:25:09 -05001362 "0814 BLKGRD: Injecting apptag error: "
1363 "read lba x%lx\n", (unsigned long)lba);
1364 break;
1365 }
1366 }
1367 }
1368
1369
1370 /* Should we change the Guard Tag */
1371 if (new_guard) {
1372 if (phba->lpfc_injerr_wgrd_cnt) {
1373 switch (op) {
1374 case SCSI_PROT_WRITE_PASS:
James Smart9a6b09c2012-03-01 22:37:42 -05001375 rc = BG_ERR_CHECK;
James Smartacd68592012-01-18 16:25:09 -05001376 /* Drop thru */
James Smartacd68592012-01-18 16:25:09 -05001377
James Smartacd68592012-01-18 16:25:09 -05001378 case SCSI_PROT_WRITE_INSERT:
1379 /*
1380 * For WRITE_INSERT, force the
1381 * error to be sent on the wire. It should be
1382 * detected by the Target.
1383 */
1384 phba->lpfc_injerr_wgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001385 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1386 phba->lpfc_injerr_nportid = 0;
1387 phba->lpfc_injerr_lba =
1388 LPFC_INJERR_LBA_OFF;
1389 memset(&phba->lpfc_injerr_wwpn,
1390 0, sizeof(struct lpfc_name));
1391 }
James Smartacd68592012-01-18 16:25:09 -05001392
James Smart9a6b09c2012-03-01 22:37:42 -05001393 rc |= BG_ERR_TGT | BG_ERR_SWAP;
James Smartacd68592012-01-18 16:25:09 -05001394 /* Signals the caller to swap CRC->CSUM */
1395
1396 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1397 "0817 BLKGRD: Injecting guard error: "
1398 "write lba x%lx\n", (unsigned long)lba);
1399 break;
James Smart9a6b09c2012-03-01 22:37:42 -05001400 case SCSI_PROT_WRITE_STRIP:
1401 /*
1402 * For WRITE_STRIP and WRITE_PASS,
1403 * force the error on data
1404 * being copied from SLI-Host to SLI-Port.
1405 */
1406 phba->lpfc_injerr_wgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001407 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1408 phba->lpfc_injerr_nportid = 0;
1409 phba->lpfc_injerr_lba =
1410 LPFC_INJERR_LBA_OFF;
1411 memset(&phba->lpfc_injerr_wwpn,
1412 0, sizeof(struct lpfc_name));
1413 }
James Smart9a6b09c2012-03-01 22:37:42 -05001414
1415 rc = BG_ERR_INIT | BG_ERR_SWAP;
1416 /* Signals the caller to swap CRC->CSUM */
1417
1418 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1419 "0816 BLKGRD: Injecting guard error: "
1420 "write lba x%lx\n", (unsigned long)lba);
1421 break;
James Smartacd68592012-01-18 16:25:09 -05001422 }
1423 }
1424 if (phba->lpfc_injerr_rgrd_cnt) {
1425 switch (op) {
1426 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001427 case SCSI_PROT_READ_STRIP:
1428 case SCSI_PROT_READ_PASS:
1429 /*
1430 * For READ_STRIP and READ_PASS, force the
1431 * error on data being read off the wire. It
1432 * should force an IO error to the driver.
1433 */
James Smartacd68592012-01-18 16:25:09 -05001434 phba->lpfc_injerr_rgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001435 if (phba->lpfc_injerr_rgrd_cnt == 0) {
1436 phba->lpfc_injerr_nportid = 0;
1437 phba->lpfc_injerr_lba =
1438 LPFC_INJERR_LBA_OFF;
1439 memset(&phba->lpfc_injerr_wwpn,
1440 0, sizeof(struct lpfc_name));
1441 }
James Smartacd68592012-01-18 16:25:09 -05001442
James Smart9a6b09c2012-03-01 22:37:42 -05001443 rc = BG_ERR_INIT | BG_ERR_SWAP;
James Smartacd68592012-01-18 16:25:09 -05001444 /* Signals the caller to swap CRC->CSUM */
1445
1446 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1447 "0818 BLKGRD: Injecting guard error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001448 "read lba x%lx\n", (unsigned long)lba);
1449 }
1450 }
1451 }
1452
James Smartf9bb2da2011-10-10 21:34:11 -04001453 return rc;
1454}
1455#endif
1456
James Smartacd68592012-01-18 16:25:09 -05001457/**
1458 * lpfc_sc_to_bg_opcodes - Determine the BlockGuard opcodes to be used with
1459 * the specified SCSI command.
1460 * @phba: The Hba for which this call is being executed.
James Smart6c8eea52010-04-06 14:49:53 -04001461 * @sc: The SCSI command to examine
1462 * @txopt: (out) BlockGuard operation for transmitted data
1463 * @rxopt: (out) BlockGuard operation for received data
1464 *
1465 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1466 *
James Smartacd68592012-01-18 16:25:09 -05001467 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001468static int
James Smart6c8eea52010-04-06 14:49:53 -04001469lpfc_sc_to_bg_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1470 uint8_t *txop, uint8_t *rxop)
James Smarte2a0a9d2008-12-04 22:40:02 -05001471{
James Smart6c8eea52010-04-06 14:49:53 -04001472 uint8_t ret = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05001473
James Smart9c6aa9d2013-05-31 17:03:39 -04001474 if (lpfc_cmd_guard_csum(sc)) {
James Smarte2a0a9d2008-12-04 22:40:02 -05001475 switch (scsi_get_prot_op(sc)) {
1476 case SCSI_PROT_READ_INSERT:
1477 case SCSI_PROT_WRITE_STRIP:
James Smart6c8eea52010-04-06 14:49:53 -04001478 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001479 *txop = BG_OP_IN_CSUM_OUT_NODIF;
James Smarte2a0a9d2008-12-04 22:40:02 -05001480 break;
1481
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
Martin K. Petersenc6af4042009-09-18 17:32:59 -04001488 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_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001491 *txop = BG_OP_IN_CSUM_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001492 break;
1493
James Smarte2a0a9d2008-12-04 22:40:02 -05001494 case SCSI_PROT_NORMAL:
1495 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001496 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001497 "9063 BLKGRD: Bad op/guard:%d/IP combination\n",
1498 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001499 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001500 break;
1501
1502 }
James Smart7c56b9f2011-07-22 18:36:25 -04001503 } else {
James Smarte2a0a9d2008-12-04 22:40:02 -05001504 switch (scsi_get_prot_op(sc)) {
1505 case SCSI_PROT_READ_STRIP:
1506 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001507 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001508 *txop = BG_OP_IN_NODIF_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001509 break;
1510
1511 case SCSI_PROT_READ_PASS:
1512 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001513 *rxop = BG_OP_IN_CRC_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001514 *txop = BG_OP_IN_CRC_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001515 break;
1516
James Smarte2a0a9d2008-12-04 22:40:02 -05001517 case SCSI_PROT_READ_INSERT:
1518 case SCSI_PROT_WRITE_STRIP:
James Smart7c56b9f2011-07-22 18:36:25 -04001519 *rxop = BG_OP_IN_NODIF_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001520 *txop = BG_OP_IN_CRC_OUT_NODIF;
James Smart7c56b9f2011-07-22 18:36:25 -04001521 break;
1522
James Smarte2a0a9d2008-12-04 22:40:02 -05001523 case SCSI_PROT_NORMAL:
1524 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001525 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001526 "9075 BLKGRD: Bad op/guard:%d/CRC combination\n",
1527 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001528 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001529 break;
1530 }
James Smarte2a0a9d2008-12-04 22:40:02 -05001531 }
1532
James Smart6c8eea52010-04-06 14:49:53 -04001533 return ret;
James Smarte2a0a9d2008-12-04 22:40:02 -05001534}
1535
James Smartacd68592012-01-18 16:25:09 -05001536#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1537/**
1538 * lpfc_bg_err_opcodes - reDetermine the BlockGuard opcodes to be used with
1539 * the specified SCSI command in order to force a guard tag error.
1540 * @phba: The Hba for which this call is being executed.
1541 * @sc: The SCSI command to examine
1542 * @txopt: (out) BlockGuard operation for transmitted data
1543 * @rxopt: (out) BlockGuard operation for received data
1544 *
1545 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1546 *
1547 **/
1548static int
1549lpfc_bg_err_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1550 uint8_t *txop, uint8_t *rxop)
1551{
James Smartacd68592012-01-18 16:25:09 -05001552 uint8_t ret = 0;
1553
James Smart9c6aa9d2013-05-31 17:03:39 -04001554 if (lpfc_cmd_guard_csum(sc)) {
James Smartacd68592012-01-18 16:25:09 -05001555 switch (scsi_get_prot_op(sc)) {
1556 case SCSI_PROT_READ_INSERT:
1557 case SCSI_PROT_WRITE_STRIP:
James Smartacd68592012-01-18 16:25:09 -05001558 *rxop = BG_OP_IN_NODIF_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001559 *txop = BG_OP_IN_CRC_OUT_NODIF;
James Smartacd68592012-01-18 16:25:09 -05001560 break;
1561
1562 case SCSI_PROT_READ_STRIP:
1563 case SCSI_PROT_WRITE_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001564 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001565 *txop = BG_OP_IN_NODIF_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001566 break;
1567
1568 case SCSI_PROT_READ_PASS:
1569 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001570 *rxop = BG_OP_IN_CSUM_OUT_CRC;
James Smart9a6b09c2012-03-01 22:37:42 -05001571 *txop = BG_OP_IN_CRC_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001572 break;
1573
1574 case SCSI_PROT_NORMAL:
1575 default:
1576 break;
1577
1578 }
1579 } else {
1580 switch (scsi_get_prot_op(sc)) {
1581 case SCSI_PROT_READ_STRIP:
1582 case SCSI_PROT_WRITE_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001583 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001584 *txop = BG_OP_IN_NODIF_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001585 break;
1586
1587 case SCSI_PROT_READ_PASS:
1588 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001589 *rxop = BG_OP_IN_CSUM_OUT_CSUM;
James Smart9a6b09c2012-03-01 22:37:42 -05001590 *txop = BG_OP_IN_CSUM_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001591 break;
1592
1593 case SCSI_PROT_READ_INSERT:
1594 case SCSI_PROT_WRITE_STRIP:
James Smartacd68592012-01-18 16:25:09 -05001595 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001596 *txop = BG_OP_IN_CSUM_OUT_NODIF;
James Smartacd68592012-01-18 16:25:09 -05001597 break;
1598
1599 case SCSI_PROT_NORMAL:
1600 default:
1601 break;
1602 }
1603 }
1604
1605 return ret;
1606}
1607#endif
1608
1609/**
1610 * lpfc_bg_setup_bpl - Setup BlockGuard BPL with no protection data
1611 * @phba: The Hba for which this call is being executed.
1612 * @sc: pointer to scsi command we're working on
1613 * @bpl: pointer to buffer list for protection groups
1614 * @datacnt: number of segments of data that have been dma mapped
1615 *
1616 * This function sets up BPL buffer list for protection groups of
James Smarte2a0a9d2008-12-04 22:40:02 -05001617 * type LPFC_PG_TYPE_NO_DIF
1618 *
1619 * This is usually used when the HBA is instructed to generate
1620 * DIFs and insert them into data stream (or strip DIF from
1621 * incoming data stream)
1622 *
1623 * The buffer list consists of just one protection group described
1624 * below:
1625 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001626 * start of prot group --> | PDE_5 |
1627 * +-------------------------+
1628 * | PDE_6 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001629 * +-------------------------+
1630 * | Data BDE |
1631 * +-------------------------+
1632 * |more Data BDE's ... (opt)|
1633 * +-------------------------+
1634 *
James Smarte2a0a9d2008-12-04 22:40:02 -05001635 *
1636 * Note: Data s/g buffers have been dma mapped
James Smartacd68592012-01-18 16:25:09 -05001637 *
1638 * Returns the number of BDEs added to the BPL.
1639 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001640static int
1641lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1642 struct ulp_bde64 *bpl, int datasegcnt)
1643{
1644 struct scatterlist *sgde = NULL; /* s/g data entry */
James Smart6c8eea52010-04-06 14:49:53 -04001645 struct lpfc_pde5 *pde5 = NULL;
1646 struct lpfc_pde6 *pde6 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05001647 dma_addr_t physaddr;
James Smart6c8eea52010-04-06 14:49:53 -04001648 int i = 0, num_bde = 0, status;
James Smarte2a0a9d2008-12-04 22:40:02 -05001649 int datadir = sc->sc_data_direction;
James Smart0829a192012-05-09 21:18:12 -04001650#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001651 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04001652#endif
James Smartacd68592012-01-18 16:25:09 -05001653 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001654 uint32_t reftag;
James Smart6c8eea52010-04-06 14:49:53 -04001655 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05001656
James Smart6c8eea52010-04-06 14:49:53 -04001657 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1658 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05001659 goto out;
1660
James Smart6c8eea52010-04-06 14:49:53 -04001661 /* extract some info from the scsi command for pde*/
James Smartacd68592012-01-18 16:25:09 -05001662 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
James Smarte2a0a9d2008-12-04 22:40:02 -05001663
James Smartf9bb2da2011-10-10 21:34:11 -04001664#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05001665 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05001666 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05001667 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05001668 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05001669 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05001670 checking = 0;
1671 }
James Smartf9bb2da2011-10-10 21:34:11 -04001672#endif
1673
James Smart6c8eea52010-04-06 14:49:53 -04001674 /* setup PDE5 with what we have */
1675 pde5 = (struct lpfc_pde5 *) bpl;
1676 memset(pde5, 0, sizeof(struct lpfc_pde5));
1677 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05001678
James Smartbc739052010-08-04 16:11:18 -04001679 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04001680 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04001681 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04001682
James Smart6c8eea52010-04-06 14:49:53 -04001683 /* advance bpl and increment bde count */
1684 num_bde++;
1685 bpl++;
1686 pde6 = (struct lpfc_pde6 *) bpl;
1687
1688 /* setup PDE6 with the rest of the info */
1689 memset(pde6, 0, sizeof(struct lpfc_pde6));
1690 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
1691 bf_set(pde6_optx, pde6, txop);
1692 bf_set(pde6_oprx, pde6, rxop);
James Smarta6887e22013-04-17 20:18:07 -04001693
1694 /*
1695 * We only need to check the data on READs, for WRITEs
1696 * protection data is automatically generated, not checked.
1697 */
James Smart6c8eea52010-04-06 14:49:53 -04001698 if (datadir == DMA_FROM_DEVICE) {
James Smart9c6aa9d2013-05-31 17:03:39 -04001699 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
James Smarta6887e22013-04-17 20:18:07 -04001700 bf_set(pde6_ce, pde6, checking);
1701 else
1702 bf_set(pde6_ce, pde6, 0);
1703
James Smart9c6aa9d2013-05-31 17:03:39 -04001704 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04001705 bf_set(pde6_re, pde6, checking);
1706 else
1707 bf_set(pde6_re, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04001708 }
1709 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04001710 bf_set(pde6_ae, pde6, 0);
1711 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04001712
James Smartbc739052010-08-04 16:11:18 -04001713 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04001714 pde6->word0 = cpu_to_le32(pde6->word0);
1715 pde6->word1 = cpu_to_le32(pde6->word1);
1716 pde6->word2 = cpu_to_le32(pde6->word2);
1717
James Smart6c8eea52010-04-06 14:49:53 -04001718 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05001719 num_bde++;
1720 bpl++;
1721
1722 /* assumption: caller has already run dma_map_sg on command data */
1723 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
1724 physaddr = sg_dma_address(sgde);
1725 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
1726 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1727 bpl->tus.f.bdeSize = sg_dma_len(sgde);
1728 if (datadir == DMA_TO_DEVICE)
1729 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1730 else
1731 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1732 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1733 bpl++;
1734 num_bde++;
1735 }
1736
1737out:
1738 return num_bde;
1739}
1740
James Smartacd68592012-01-18 16:25:09 -05001741/**
1742 * lpfc_bg_setup_bpl_prot - Setup BlockGuard BPL with protection data
1743 * @phba: The Hba for which this call is being executed.
1744 * @sc: pointer to scsi command we're working on
1745 * @bpl: pointer to buffer list for protection groups
1746 * @datacnt: number of segments of data that have been dma mapped
1747 * @protcnt: number of segment of protection data that have been dma mapped
1748 *
1749 * This function sets up BPL buffer list for protection groups of
1750 * type LPFC_PG_TYPE_DIF
James Smarte2a0a9d2008-12-04 22:40:02 -05001751 *
1752 * This is usually used when DIFs are in their own buffers,
1753 * separate from the data. The HBA can then by instructed
1754 * to place the DIFs in the outgoing stream. For read operations,
1755 * The HBA could extract the DIFs and place it in DIF buffers.
1756 *
1757 * The buffer list for this type consists of one or more of the
1758 * protection groups described below:
1759 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001760 * start of first prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001761 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001762 * | PDE_6 |
1763 * +-------------------------+
1764 * | PDE_7 (Prot BDE) |
James Smarte2a0a9d2008-12-04 22:40:02 -05001765 * +-------------------------+
1766 * | Data BDE |
1767 * +-------------------------+
1768 * |more Data BDE's ... (opt)|
1769 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001770 * start of new prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001771 * +-------------------------+
1772 * | ... |
1773 * +-------------------------+
1774 *
James Smarte2a0a9d2008-12-04 22:40:02 -05001775 * Note: It is assumed that both data and protection s/g buffers have been
1776 * mapped for DMA
James Smartacd68592012-01-18 16:25:09 -05001777 *
1778 * Returns the number of BDEs added to the BPL.
1779 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001780static int
1781lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1782 struct ulp_bde64 *bpl, int datacnt, int protcnt)
1783{
1784 struct scatterlist *sgde = NULL; /* s/g data entry */
1785 struct scatterlist *sgpe = NULL; /* s/g prot entry */
James Smart6c8eea52010-04-06 14:49:53 -04001786 struct lpfc_pde5 *pde5 = NULL;
1787 struct lpfc_pde6 *pde6 = NULL;
James Smart7f860592011-03-11 16:05:52 -05001788 struct lpfc_pde7 *pde7 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05001789 dma_addr_t dataphysaddr, protphysaddr;
1790 unsigned short curr_data = 0, curr_prot = 0;
James Smart7f860592011-03-11 16:05:52 -05001791 unsigned int split_offset;
1792 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
James Smarte2a0a9d2008-12-04 22:40:02 -05001793 unsigned int protgrp_blks, protgrp_bytes;
1794 unsigned int remainder, subtotal;
James Smart6c8eea52010-04-06 14:49:53 -04001795 int status;
James Smarte2a0a9d2008-12-04 22:40:02 -05001796 int datadir = sc->sc_data_direction;
1797 unsigned char pgdone = 0, alldone = 0;
1798 unsigned blksize;
James Smart0829a192012-05-09 21:18:12 -04001799#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001800 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04001801#endif
James Smartacd68592012-01-18 16:25:09 -05001802 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001803 uint32_t reftag;
James Smart6c8eea52010-04-06 14:49:53 -04001804 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05001805 int num_bde = 0;
1806
1807 sgpe = scsi_prot_sglist(sc);
1808 sgde = scsi_sglist(sc);
1809
1810 if (!sgpe || !sgde) {
1811 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1812 "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
1813 sgpe, sgde);
1814 return 0;
1815 }
1816
James Smart6c8eea52010-04-06 14:49:53 -04001817 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1818 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05001819 goto out;
1820
James Smart6c8eea52010-04-06 14:49:53 -04001821 /* extract some info from the scsi command */
James Smarte2a0a9d2008-12-04 22:40:02 -05001822 blksize = lpfc_cmd_blksize(sc);
James Smartacd68592012-01-18 16:25:09 -05001823 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
James Smarte2a0a9d2008-12-04 22:40:02 -05001824
James Smartf9bb2da2011-10-10 21:34:11 -04001825#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05001826 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05001827 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05001828 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05001829 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05001830 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05001831 checking = 0;
1832 }
James Smartf9bb2da2011-10-10 21:34:11 -04001833#endif
1834
James Smarte2a0a9d2008-12-04 22:40:02 -05001835 split_offset = 0;
1836 do {
James Smart96f70772013-04-17 20:16:15 -04001837 /* Check to see if we ran out of space */
1838 if (num_bde >= (phba->cfg_total_seg_cnt - 2))
1839 return num_bde + 3;
1840
James Smart6c8eea52010-04-06 14:49:53 -04001841 /* setup PDE5 with what we have */
1842 pde5 = (struct lpfc_pde5 *) bpl;
1843 memset(pde5, 0, sizeof(struct lpfc_pde5));
1844 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05001845
James Smartbc739052010-08-04 16:11:18 -04001846 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04001847 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04001848 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04001849
James Smart6c8eea52010-04-06 14:49:53 -04001850 /* advance bpl and increment bde count */
1851 num_bde++;
1852 bpl++;
1853 pde6 = (struct lpfc_pde6 *) bpl;
James Smarte2a0a9d2008-12-04 22:40:02 -05001854
James Smart6c8eea52010-04-06 14:49:53 -04001855 /* setup PDE6 with the rest of the info */
1856 memset(pde6, 0, sizeof(struct lpfc_pde6));
1857 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
1858 bf_set(pde6_optx, pde6, txop);
1859 bf_set(pde6_oprx, pde6, rxop);
James Smarta6887e22013-04-17 20:18:07 -04001860
James Smart9c6aa9d2013-05-31 17:03:39 -04001861 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
James Smarta6887e22013-04-17 20:18:07 -04001862 bf_set(pde6_ce, pde6, checking);
1863 else
1864 bf_set(pde6_ce, pde6, 0);
1865
James Smart9c6aa9d2013-05-31 17:03:39 -04001866 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04001867 bf_set(pde6_re, pde6, checking);
1868 else
1869 bf_set(pde6_re, pde6, 0);
1870
James Smart6c8eea52010-04-06 14:49:53 -04001871 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04001872 bf_set(pde6_ae, pde6, 0);
1873 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04001874
James Smartbc739052010-08-04 16:11:18 -04001875 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04001876 pde6->word0 = cpu_to_le32(pde6->word0);
1877 pde6->word1 = cpu_to_le32(pde6->word1);
1878 pde6->word2 = cpu_to_le32(pde6->word2);
1879
James Smart6c8eea52010-04-06 14:49:53 -04001880 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05001881 num_bde++;
1882 bpl++;
1883
1884 /* setup the first BDE that points to protection buffer */
James Smart7f860592011-03-11 16:05:52 -05001885 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
1886 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
James Smarte2a0a9d2008-12-04 22:40:02 -05001887
James Smarte2a0a9d2008-12-04 22:40:02 -05001888 /* must be integer multiple of the DIF block length */
1889 BUG_ON(protgroup_len % 8);
1890
James Smart7f860592011-03-11 16:05:52 -05001891 pde7 = (struct lpfc_pde7 *) bpl;
1892 memset(pde7, 0, sizeof(struct lpfc_pde7));
1893 bf_set(pde7_type, pde7, LPFC_PDE7_DESCRIPTOR);
1894
James Smart7c56b9f2011-07-22 18:36:25 -04001895 pde7->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
1896 pde7->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
James Smart7f860592011-03-11 16:05:52 -05001897
James Smarte2a0a9d2008-12-04 22:40:02 -05001898 protgrp_blks = protgroup_len / 8;
1899 protgrp_bytes = protgrp_blks * blksize;
1900
James Smart7f860592011-03-11 16:05:52 -05001901 /* check if this pde is crossing the 4K boundary; if so split */
1902 if ((pde7->addrLow & 0xfff) + protgroup_len > 0x1000) {
1903 protgroup_remainder = 0x1000 - (pde7->addrLow & 0xfff);
1904 protgroup_offset += protgroup_remainder;
1905 protgrp_blks = protgroup_remainder / 8;
James Smart7c56b9f2011-07-22 18:36:25 -04001906 protgrp_bytes = protgrp_blks * blksize;
James Smart7f860592011-03-11 16:05:52 -05001907 } else {
1908 protgroup_offset = 0;
1909 curr_prot++;
1910 }
James Smarte2a0a9d2008-12-04 22:40:02 -05001911
James Smarte2a0a9d2008-12-04 22:40:02 -05001912 num_bde++;
1913
1914 /* setup BDE's for data blocks associated with DIF data */
1915 pgdone = 0;
1916 subtotal = 0; /* total bytes processed for current prot grp */
1917 while (!pgdone) {
James Smart96f70772013-04-17 20:16:15 -04001918 /* Check to see if we ran out of space */
1919 if (num_bde >= phba->cfg_total_seg_cnt)
1920 return num_bde + 1;
1921
James Smarte2a0a9d2008-12-04 22:40:02 -05001922 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -04001923 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1924 "9065 BLKGRD:%s Invalid data segment\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001925 __func__);
1926 return 0;
1927 }
1928 bpl++;
1929 dataphysaddr = sg_dma_address(sgde) + split_offset;
1930 bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
1931 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
1932
1933 remainder = sg_dma_len(sgde) - split_offset;
1934
1935 if ((subtotal + remainder) <= protgrp_bytes) {
1936 /* we can use this whole buffer */
1937 bpl->tus.f.bdeSize = remainder;
1938 split_offset = 0;
1939
1940 if ((subtotal + remainder) == protgrp_bytes)
1941 pgdone = 1;
1942 } else {
1943 /* must split this buffer with next prot grp */
1944 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
1945 split_offset += bpl->tus.f.bdeSize;
1946 }
1947
1948 subtotal += bpl->tus.f.bdeSize;
1949
1950 if (datadir == DMA_TO_DEVICE)
1951 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1952 else
1953 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1954 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1955
1956 num_bde++;
1957 curr_data++;
1958
1959 if (split_offset)
1960 break;
1961
1962 /* Move to the next s/g segment if possible */
1963 sgde = sg_next(sgde);
James Smart6c8eea52010-04-06 14:49:53 -04001964
James Smarte2a0a9d2008-12-04 22:40:02 -05001965 }
1966
James Smart7f860592011-03-11 16:05:52 -05001967 if (protgroup_offset) {
1968 /* update the reference tag */
1969 reftag += protgrp_blks;
1970 bpl++;
1971 continue;
1972 }
1973
James Smarte2a0a9d2008-12-04 22:40:02 -05001974 /* are we done ? */
1975 if (curr_prot == protcnt) {
1976 alldone = 1;
1977 } else if (curr_prot < protcnt) {
1978 /* advance to next prot buffer */
1979 sgpe = sg_next(sgpe);
1980 bpl++;
1981
1982 /* update the reference tag */
1983 reftag += protgrp_blks;
1984 } else {
1985 /* if we're here, we have a bug */
James Smart6a9c52c2009-10-02 15:16:51 -04001986 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1987 "9054 BLKGRD: bug in %s\n", __func__);
James Smarte2a0a9d2008-12-04 22:40:02 -05001988 }
1989
1990 } while (!alldone);
James Smarte2a0a9d2008-12-04 22:40:02 -05001991out:
1992
James Smarte2a0a9d2008-12-04 22:40:02 -05001993 return num_bde;
1994}
James Smart7f860592011-03-11 16:05:52 -05001995
James Smartacd68592012-01-18 16:25:09 -05001996/**
1997 * lpfc_bg_setup_sgl - Setup BlockGuard SGL with no protection data
1998 * @phba: The Hba for which this call is being executed.
1999 * @sc: pointer to scsi command we're working on
2000 * @sgl: pointer to buffer list for protection groups
2001 * @datacnt: number of segments of data that have been dma mapped
2002 *
2003 * This function sets up SGL buffer list for protection groups of
2004 * type LPFC_PG_TYPE_NO_DIF
2005 *
2006 * This is usually used when the HBA is instructed to generate
2007 * DIFs and insert them into data stream (or strip DIF from
2008 * incoming data stream)
2009 *
2010 * The buffer list consists of just one protection group described
2011 * below:
2012 * +-------------------------+
2013 * start of prot group --> | DI_SEED |
2014 * +-------------------------+
2015 * | Data SGE |
2016 * +-------------------------+
2017 * |more Data SGE's ... (opt)|
2018 * +-------------------------+
2019 *
2020 *
2021 * Note: Data s/g buffers have been dma mapped
2022 *
2023 * Returns the number of SGEs added to the SGL.
2024 **/
2025static int
2026lpfc_bg_setup_sgl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2027 struct sli4_sge *sgl, int datasegcnt)
2028{
2029 struct scatterlist *sgde = NULL; /* s/g data entry */
2030 struct sli4_sge_diseed *diseed = NULL;
2031 dma_addr_t physaddr;
2032 int i = 0, num_sge = 0, status;
James Smartacd68592012-01-18 16:25:09 -05002033 uint32_t reftag;
James Smartacd68592012-01-18 16:25:09 -05002034 uint8_t txop, rxop;
James Smart0829a192012-05-09 21:18:12 -04002035#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002036 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002037#endif
James Smartacd68592012-01-18 16:25:09 -05002038 uint32_t checking = 1;
2039 uint32_t dma_len;
2040 uint32_t dma_offset = 0;
2041
2042 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2043 if (status)
2044 goto out;
2045
2046 /* extract some info from the scsi command for pde*/
James Smartacd68592012-01-18 16:25:09 -05002047 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2048
2049#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002050 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002051 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002052 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002053 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002054 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002055 checking = 0;
2056 }
2057#endif
2058
2059 /* setup DISEED with what we have */
2060 diseed = (struct sli4_sge_diseed *) sgl;
2061 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2062 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2063
2064 /* Endianness conversion if necessary */
2065 diseed->ref_tag = cpu_to_le32(reftag);
2066 diseed->ref_tag_tran = diseed->ref_tag;
2067
James Smarta6887e22013-04-17 20:18:07 -04002068 /*
2069 * We only need to check the data on READs, for WRITEs
2070 * protection data is automatically generated, not checked.
2071 */
2072 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
James Smart9c6aa9d2013-05-31 17:03:39 -04002073 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
James Smarta6887e22013-04-17 20:18:07 -04002074 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2075 else
2076 bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2077
James Smart9c6aa9d2013-05-31 17:03:39 -04002078 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04002079 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2080 else
2081 bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2082 }
2083
James Smartacd68592012-01-18 16:25:09 -05002084 /* setup DISEED with the rest of the info */
2085 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2086 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002087
James Smartacd68592012-01-18 16:25:09 -05002088 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2089 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2090
2091 /* Endianness conversion if necessary for DISEED */
2092 diseed->word2 = cpu_to_le32(diseed->word2);
2093 diseed->word3 = cpu_to_le32(diseed->word3);
2094
2095 /* advance bpl and increment sge count */
2096 num_sge++;
2097 sgl++;
2098
2099 /* assumption: caller has already run dma_map_sg on command data */
2100 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
2101 physaddr = sg_dma_address(sgde);
2102 dma_len = sg_dma_len(sgde);
2103 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
2104 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
2105 if ((i + 1) == datasegcnt)
2106 bf_set(lpfc_sli4_sge_last, sgl, 1);
2107 else
2108 bf_set(lpfc_sli4_sge_last, sgl, 0);
2109 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2110 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2111
2112 sgl->sge_len = cpu_to_le32(dma_len);
2113 dma_offset += dma_len;
2114
2115 sgl++;
2116 num_sge++;
2117 }
2118
2119out:
2120 return num_sge;
2121}
2122
2123/**
2124 * lpfc_bg_setup_sgl_prot - Setup BlockGuard SGL with protection data
2125 * @phba: The Hba for which this call is being executed.
2126 * @sc: pointer to scsi command we're working on
2127 * @sgl: pointer to buffer list for protection groups
2128 * @datacnt: number of segments of data that have been dma mapped
2129 * @protcnt: number of segment of protection data that have been dma mapped
2130 *
2131 * This function sets up SGL buffer list for protection groups of
2132 * type LPFC_PG_TYPE_DIF
2133 *
2134 * This is usually used when DIFs are in their own buffers,
2135 * separate from the data. The HBA can then by instructed
2136 * to place the DIFs in the outgoing stream. For read operations,
2137 * The HBA could extract the DIFs and place it in DIF buffers.
2138 *
2139 * The buffer list for this type consists of one or more of the
2140 * protection groups described below:
2141 * +-------------------------+
2142 * start of first prot group --> | DISEED |
2143 * +-------------------------+
2144 * | DIF (Prot SGE) |
2145 * +-------------------------+
2146 * | Data SGE |
2147 * +-------------------------+
2148 * |more Data SGE's ... (opt)|
2149 * +-------------------------+
2150 * start of new prot group --> | DISEED |
2151 * +-------------------------+
2152 * | ... |
2153 * +-------------------------+
2154 *
2155 * Note: It is assumed that both data and protection s/g buffers have been
2156 * mapped for DMA
2157 *
2158 * Returns the number of SGEs added to the SGL.
2159 **/
2160static int
2161lpfc_bg_setup_sgl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2162 struct sli4_sge *sgl, int datacnt, int protcnt)
2163{
2164 struct scatterlist *sgde = NULL; /* s/g data entry */
2165 struct scatterlist *sgpe = NULL; /* s/g prot entry */
2166 struct sli4_sge_diseed *diseed = NULL;
2167 dma_addr_t dataphysaddr, protphysaddr;
2168 unsigned short curr_data = 0, curr_prot = 0;
2169 unsigned int split_offset;
2170 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
2171 unsigned int protgrp_blks, protgrp_bytes;
2172 unsigned int remainder, subtotal;
2173 int status;
2174 unsigned char pgdone = 0, alldone = 0;
2175 unsigned blksize;
2176 uint32_t reftag;
2177 uint8_t txop, rxop;
2178 uint32_t dma_len;
James Smart0829a192012-05-09 21:18:12 -04002179#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002180 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002181#endif
James Smartacd68592012-01-18 16:25:09 -05002182 uint32_t checking = 1;
2183 uint32_t dma_offset = 0;
2184 int num_sge = 0;
2185
2186 sgpe = scsi_prot_sglist(sc);
2187 sgde = scsi_sglist(sc);
2188
2189 if (!sgpe || !sgde) {
2190 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2191 "9082 Invalid s/g entry: data=0x%p prot=0x%p\n",
2192 sgpe, sgde);
2193 return 0;
2194 }
2195
2196 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2197 if (status)
2198 goto out;
2199
2200 /* extract some info from the scsi command */
2201 blksize = lpfc_cmd_blksize(sc);
2202 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2203
2204#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002205 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002206 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002207 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002208 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002209 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002210 checking = 0;
2211 }
2212#endif
2213
2214 split_offset = 0;
2215 do {
James Smart96f70772013-04-17 20:16:15 -04002216 /* Check to see if we ran out of space */
2217 if (num_sge >= (phba->cfg_total_seg_cnt - 2))
2218 return num_sge + 3;
2219
James Smartacd68592012-01-18 16:25:09 -05002220 /* setup DISEED with what we have */
2221 diseed = (struct sli4_sge_diseed *) sgl;
2222 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2223 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2224
2225 /* Endianness conversion if necessary */
2226 diseed->ref_tag = cpu_to_le32(reftag);
2227 diseed->ref_tag_tran = diseed->ref_tag;
2228
James Smart9c6aa9d2013-05-31 17:03:39 -04002229 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD)) {
James Smarta6887e22013-04-17 20:18:07 -04002230 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2231
2232 } else {
2233 bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2234 /*
2235 * When in this mode, the hardware will replace
2236 * the guard tag from the host with a
2237 * newly generated good CRC for the wire.
2238 * Switch to raw mode here to avoid this
2239 * behavior. What the host sends gets put on the wire.
2240 */
2241 if (txop == BG_OP_IN_CRC_OUT_CRC) {
2242 txop = BG_OP_RAW_MODE;
2243 rxop = BG_OP_RAW_MODE;
2244 }
2245 }
2246
2247
James Smart9c6aa9d2013-05-31 17:03:39 -04002248 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04002249 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2250 else
2251 bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2252
James Smartacd68592012-01-18 16:25:09 -05002253 /* setup DISEED with the rest of the info */
2254 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2255 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002256
James Smartacd68592012-01-18 16:25:09 -05002257 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2258 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2259
2260 /* Endianness conversion if necessary for DISEED */
2261 diseed->word2 = cpu_to_le32(diseed->word2);
2262 diseed->word3 = cpu_to_le32(diseed->word3);
2263
2264 /* advance sgl and increment bde count */
2265 num_sge++;
2266 sgl++;
2267
2268 /* setup the first BDE that points to protection buffer */
2269 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2270 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
2271
2272 /* must be integer multiple of the DIF block length */
2273 BUG_ON(protgroup_len % 8);
2274
2275 /* Now setup DIF SGE */
2276 sgl->word2 = 0;
2277 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DIF);
2278 sgl->addr_hi = le32_to_cpu(putPaddrHigh(protphysaddr));
2279 sgl->addr_lo = le32_to_cpu(putPaddrLow(protphysaddr));
2280 sgl->word2 = cpu_to_le32(sgl->word2);
2281
2282 protgrp_blks = protgroup_len / 8;
2283 protgrp_bytes = protgrp_blks * blksize;
2284
2285 /* check if DIF SGE is crossing the 4K boundary; if so split */
2286 if ((sgl->addr_lo & 0xfff) + protgroup_len > 0x1000) {
2287 protgroup_remainder = 0x1000 - (sgl->addr_lo & 0xfff);
2288 protgroup_offset += protgroup_remainder;
2289 protgrp_blks = protgroup_remainder / 8;
2290 protgrp_bytes = protgrp_blks * blksize;
2291 } else {
2292 protgroup_offset = 0;
2293 curr_prot++;
2294 }
2295
2296 num_sge++;
2297
2298 /* setup SGE's for data blocks associated with DIF data */
2299 pgdone = 0;
2300 subtotal = 0; /* total bytes processed for current prot grp */
2301 while (!pgdone) {
James Smart96f70772013-04-17 20:16:15 -04002302 /* Check to see if we ran out of space */
2303 if (num_sge >= phba->cfg_total_seg_cnt)
2304 return num_sge + 1;
2305
James Smartacd68592012-01-18 16:25:09 -05002306 if (!sgde) {
2307 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2308 "9086 BLKGRD:%s Invalid data segment\n",
2309 __func__);
2310 return 0;
2311 }
2312 sgl++;
2313 dataphysaddr = sg_dma_address(sgde) + split_offset;
2314
2315 remainder = sg_dma_len(sgde) - split_offset;
2316
2317 if ((subtotal + remainder) <= protgrp_bytes) {
2318 /* we can use this whole buffer */
2319 dma_len = remainder;
2320 split_offset = 0;
2321
2322 if ((subtotal + remainder) == protgrp_bytes)
2323 pgdone = 1;
2324 } else {
2325 /* must split this buffer with next prot grp */
2326 dma_len = protgrp_bytes - subtotal;
2327 split_offset += dma_len;
2328 }
2329
2330 subtotal += dma_len;
2331
2332 sgl->addr_lo = cpu_to_le32(putPaddrLow(dataphysaddr));
2333 sgl->addr_hi = cpu_to_le32(putPaddrHigh(dataphysaddr));
2334 bf_set(lpfc_sli4_sge_last, sgl, 0);
2335 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2336 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2337
2338 sgl->sge_len = cpu_to_le32(dma_len);
2339 dma_offset += dma_len;
2340
2341 num_sge++;
2342 curr_data++;
2343
2344 if (split_offset)
2345 break;
2346
2347 /* Move to the next s/g segment if possible */
2348 sgde = sg_next(sgde);
2349 }
2350
2351 if (protgroup_offset) {
2352 /* update the reference tag */
2353 reftag += protgrp_blks;
2354 sgl++;
2355 continue;
2356 }
2357
2358 /* are we done ? */
2359 if (curr_prot == protcnt) {
2360 bf_set(lpfc_sli4_sge_last, sgl, 1);
2361 alldone = 1;
2362 } else if (curr_prot < protcnt) {
2363 /* advance to next prot buffer */
2364 sgpe = sg_next(sgpe);
2365 sgl++;
2366
2367 /* update the reference tag */
2368 reftag += protgrp_blks;
2369 } else {
2370 /* if we're here, we have a bug */
2371 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2372 "9085 BLKGRD: bug in %s\n", __func__);
2373 }
2374
2375 } while (!alldone);
2376
2377out:
2378
2379 return num_sge;
2380}
2381
2382/**
2383 * lpfc_prot_group_type - Get prtotection group type of SCSI command
2384 * @phba: The Hba for which this call is being executed.
2385 * @sc: pointer to scsi command we're working on
2386 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002387 * Given a SCSI command that supports DIF, determine composition of protection
2388 * groups involved in setting up buffer lists
2389 *
James Smartacd68592012-01-18 16:25:09 -05002390 * Returns: Protection group type (with or without DIF)
2391 *
2392 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002393static int
2394lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
2395{
2396 int ret = LPFC_PG_TYPE_INVALID;
2397 unsigned char op = scsi_get_prot_op(sc);
2398
2399 switch (op) {
2400 case SCSI_PROT_READ_STRIP:
2401 case SCSI_PROT_WRITE_INSERT:
2402 ret = LPFC_PG_TYPE_NO_DIF;
2403 break;
2404 case SCSI_PROT_READ_INSERT:
2405 case SCSI_PROT_WRITE_STRIP:
2406 case SCSI_PROT_READ_PASS:
2407 case SCSI_PROT_WRITE_PASS:
James Smarte2a0a9d2008-12-04 22:40:02 -05002408 ret = LPFC_PG_TYPE_DIF_BUF;
2409 break;
2410 default:
James Smart9c6aa9d2013-05-31 17:03:39 -04002411 if (phba)
2412 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2413 "9021 Unsupported protection op:%d\n",
2414 op);
James Smarte2a0a9d2008-12-04 22:40:02 -05002415 break;
2416 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002417 return ret;
2418}
2419
James Smartacd68592012-01-18 16:25:09 -05002420/**
James Smarta6887e22013-04-17 20:18:07 -04002421 * lpfc_bg_scsi_adjust_dl - Adjust SCSI data length for BlockGuard
2422 * @phba: The Hba for which this call is being executed.
2423 * @lpfc_cmd: The scsi buffer which is going to be adjusted.
2424 *
2425 * Adjust the data length to account for how much data
2426 * is actually on the wire.
2427 *
2428 * returns the adjusted data length
2429 **/
2430static int
2431lpfc_bg_scsi_adjust_dl(struct lpfc_hba *phba,
2432 struct lpfc_scsi_buf *lpfc_cmd)
2433{
2434 struct scsi_cmnd *sc = lpfc_cmd->pCmd;
2435 int fcpdl;
2436
2437 fcpdl = scsi_bufflen(sc);
2438
2439 /* Check if there is protection data on the wire */
2440 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
James Smart9c6aa9d2013-05-31 17:03:39 -04002441 /* Read check for protection data */
James Smarta6887e22013-04-17 20:18:07 -04002442 if (scsi_get_prot_op(sc) == SCSI_PROT_READ_INSERT)
2443 return fcpdl;
2444
2445 } else {
James Smart9c6aa9d2013-05-31 17:03:39 -04002446 /* Write check for protection data */
James Smarta6887e22013-04-17 20:18:07 -04002447 if (scsi_get_prot_op(sc) == SCSI_PROT_WRITE_STRIP)
2448 return fcpdl;
2449 }
2450
2451 /*
2452 * If we are in DIF Type 1 mode every data block has a 8 byte
James Smart9c6aa9d2013-05-31 17:03:39 -04002453 * DIF (trailer) attached to it. Must ajust FCP data length
2454 * to account for the protection data.
James Smarta6887e22013-04-17 20:18:07 -04002455 */
James Smart9c6aa9d2013-05-31 17:03:39 -04002456 fcpdl += (fcpdl / lpfc_cmd_blksize(sc)) * 8;
James Smarta6887e22013-04-17 20:18:07 -04002457
2458 return fcpdl;
2459}
2460
2461/**
James Smartacd68592012-01-18 16:25:09 -05002462 * lpfc_bg_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
2463 * @phba: The Hba for which this call is being executed.
2464 * @lpfc_cmd: The scsi buffer which is going to be prep'ed.
2465 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002466 * This is the protection/DIF aware version of
2467 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
2468 * two functions eventually, but for now, it's here
James Smartacd68592012-01-18 16:25:09 -05002469 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002470static int
James Smartacd68592012-01-18 16:25:09 -05002471lpfc_bg_scsi_prep_dma_buf_s3(struct lpfc_hba *phba,
James Smarte2a0a9d2008-12-04 22:40:02 -05002472 struct lpfc_scsi_buf *lpfc_cmd)
2473{
2474 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2475 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart0794d602019-01-28 11:14:19 -08002476 struct ulp_bde64 *bpl = lpfc_cmd->dma_sgl;
James Smarte2a0a9d2008-12-04 22:40:02 -05002477 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2478 uint32_t num_bde = 0;
2479 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
2480 int prot_group_type = 0;
James Smarta6887e22013-04-17 20:18:07 -04002481 int fcpdl;
James Smart7c4042a2018-11-29 16:09:40 -08002482 struct lpfc_vport *vport = phba->pport;
James Smarte2a0a9d2008-12-04 22:40:02 -05002483
2484 /*
2485 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
2486 * fcp_rsp regions to the first data bde entry
2487 */
2488 bpl += 2;
2489 if (scsi_sg_count(scsi_cmnd)) {
2490 /*
2491 * The driver stores the segment count returned from pci_map_sg
2492 * because this a count of dma-mappings used to map the use_sg
2493 * pages. They are not guaranteed to be the same for those
2494 * architectures that implement an IOMMU.
2495 */
2496 datasegcnt = dma_map_sg(&phba->pcidev->dev,
2497 scsi_sglist(scsi_cmnd),
2498 scsi_sg_count(scsi_cmnd), datadir);
2499 if (unlikely(!datasegcnt))
2500 return 1;
2501
2502 lpfc_cmd->seg_cnt = datasegcnt;
James Smart96f70772013-04-17 20:16:15 -04002503
2504 /* First check if data segment count from SCSI Layer is good */
2505 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt)
2506 goto err;
James Smarte2a0a9d2008-12-04 22:40:02 -05002507
2508 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
2509
2510 switch (prot_group_type) {
2511 case LPFC_PG_TYPE_NO_DIF:
James Smart96f70772013-04-17 20:16:15 -04002512
2513 /* Here we need to add a PDE5 and PDE6 to the count */
2514 if ((lpfc_cmd->seg_cnt + 2) > phba->cfg_total_seg_cnt)
2515 goto err;
2516
James Smarte2a0a9d2008-12-04 22:40:02 -05002517 num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
2518 datasegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002519 /* we should have 2 or more entries in buffer list */
James Smarte2a0a9d2008-12-04 22:40:02 -05002520 if (num_bde < 2)
2521 goto err;
2522 break;
James Smart96f70772013-04-17 20:16:15 -04002523
2524 case LPFC_PG_TYPE_DIF_BUF:
James Smarte2a0a9d2008-12-04 22:40:02 -05002525 /*
2526 * This type indicates that protection buffers are
2527 * passed to the driver, so that needs to be prepared
2528 * for DMA
2529 */
2530 protsegcnt = dma_map_sg(&phba->pcidev->dev,
2531 scsi_prot_sglist(scsi_cmnd),
2532 scsi_prot_sg_count(scsi_cmnd), datadir);
2533 if (unlikely(!protsegcnt)) {
2534 scsi_dma_unmap(scsi_cmnd);
2535 return 1;
2536 }
2537
2538 lpfc_cmd->prot_seg_cnt = protsegcnt;
James Smart96f70772013-04-17 20:16:15 -04002539
2540 /*
2541 * There is a minimun of 4 BPLs used for every
2542 * protection data segment.
2543 */
2544 if ((lpfc_cmd->prot_seg_cnt * 4) >
2545 (phba->cfg_total_seg_cnt - 2))
2546 goto err;
James Smarte2a0a9d2008-12-04 22:40:02 -05002547
2548 num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
2549 datasegcnt, protsegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002550 /* we should have 3 or more entries in buffer list */
James Smart96f70772013-04-17 20:16:15 -04002551 if ((num_bde < 3) ||
2552 (num_bde > phba->cfg_total_seg_cnt))
James Smarte2a0a9d2008-12-04 22:40:02 -05002553 goto err;
2554 break;
James Smart96f70772013-04-17 20:16:15 -04002555
James Smarte2a0a9d2008-12-04 22:40:02 -05002556 case LPFC_PG_TYPE_INVALID:
2557 default:
James Smart96f70772013-04-17 20:16:15 -04002558 scsi_dma_unmap(scsi_cmnd);
2559 lpfc_cmd->seg_cnt = 0;
2560
James Smarte2a0a9d2008-12-04 22:40:02 -05002561 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2562 "9022 Unexpected protection group %i\n",
2563 prot_group_type);
2564 return 1;
2565 }
2566 }
2567
2568 /*
2569 * Finish initializing those IOCB fields that are dependent on the
2570 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
2571 * reinitialized since all iocb memory resources are used many times
2572 * for transmit, receive, and continuation bpl's.
2573 */
2574 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
2575 iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
2576 iocb_cmd->ulpBdeCount = 1;
2577 iocb_cmd->ulpLe = 1;
2578
James Smarta6887e22013-04-17 20:18:07 -04002579 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002580 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
2581
2582 /*
2583 * Due to difference in data length between DIF/non-DIF paths,
2584 * we need to set word 4 of IOCB here
2585 */
2586 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
2587
James Smart7c4042a2018-11-29 16:09:40 -08002588 /*
2589 * For First burst, we may need to adjust the initial transfer
2590 * length for DIF
2591 */
2592 if (iocb_cmd->un.fcpi.fcpi_XRdy &&
2593 (fcpdl < vport->cfg_first_burst_size))
2594 iocb_cmd->un.fcpi.fcpi_XRdy = fcpdl;
2595
James Smarte2a0a9d2008-12-04 22:40:02 -05002596 return 0;
2597err:
James Smart96f70772013-04-17 20:16:15 -04002598 if (lpfc_cmd->seg_cnt)
2599 scsi_dma_unmap(scsi_cmnd);
2600 if (lpfc_cmd->prot_seg_cnt)
2601 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
2602 scsi_prot_sg_count(scsi_cmnd),
2603 scsi_cmnd->sc_data_direction);
2604
James Smarte2a0a9d2008-12-04 22:40:02 -05002605 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart96f70772013-04-17 20:16:15 -04002606 "9023 Cannot setup S/G List for HBA"
2607 "IO segs %d/%d BPL %d SCSI %d: %d %d\n",
2608 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
2609 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
James Smarte2a0a9d2008-12-04 22:40:02 -05002610 prot_group_type, num_bde);
James Smart96f70772013-04-17 20:16:15 -04002611
2612 lpfc_cmd->seg_cnt = 0;
2613 lpfc_cmd->prot_seg_cnt = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05002614 return 1;
2615}
2616
2617/*
James Smart737d4242013-04-17 20:14:49 -04002618 * This function calcuates the T10 DIF guard tag
2619 * on the specified data using a CRC algorithmn
2620 * using crc_t10dif.
2621 */
Rashika Kheria7bfe7812014-09-03 12:55:36 -04002622static uint16_t
James Smart737d4242013-04-17 20:14:49 -04002623lpfc_bg_crc(uint8_t *data, int count)
2624{
2625 uint16_t crc = 0;
2626 uint16_t x;
2627
2628 crc = crc_t10dif(data, count);
2629 x = cpu_to_be16(crc);
2630 return x;
2631}
2632
2633/*
2634 * This function calcuates the T10 DIF guard tag
2635 * on the specified data using a CSUM algorithmn
2636 * using ip_compute_csum.
2637 */
Rashika Kheria7bfe7812014-09-03 12:55:36 -04002638static uint16_t
James Smart737d4242013-04-17 20:14:49 -04002639lpfc_bg_csum(uint8_t *data, int count)
2640{
2641 uint16_t ret;
2642
2643 ret = ip_compute_csum(data, count);
2644 return ret;
2645}
2646
2647/*
2648 * This function examines the protection data to try to determine
2649 * what type of T10-DIF error occurred.
2650 */
Rashika Kheria7bfe7812014-09-03 12:55:36 -04002651static void
James Smart737d4242013-04-17 20:14:49 -04002652lpfc_calc_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
2653{
2654 struct scatterlist *sgpe; /* s/g prot entry */
2655 struct scatterlist *sgde; /* s/g data entry */
2656 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
2657 struct scsi_dif_tuple *src = NULL;
2658 uint8_t *data_src = NULL;
Sebastian Herbsztdb6f1c22015-08-31 16:48:14 -04002659 uint16_t guard_tag;
James Smart737d4242013-04-17 20:14:49 -04002660 uint16_t start_app_tag, app_tag;
2661 uint32_t start_ref_tag, ref_tag;
2662 int prot, protsegcnt;
2663 int err_type, len, data_len;
2664 int chk_ref, chk_app, chk_guard;
2665 uint16_t sum;
2666 unsigned blksize;
2667
2668 err_type = BGS_GUARD_ERR_MASK;
2669 sum = 0;
2670 guard_tag = 0;
2671
2672 /* First check to see if there is protection data to examine */
2673 prot = scsi_get_prot_op(cmd);
2674 if ((prot == SCSI_PROT_READ_STRIP) ||
2675 (prot == SCSI_PROT_WRITE_INSERT) ||
2676 (prot == SCSI_PROT_NORMAL))
2677 goto out;
2678
2679 /* Currently the driver just supports ref_tag and guard_tag checking */
2680 chk_ref = 1;
2681 chk_app = 0;
2682 chk_guard = 0;
2683
2684 /* Setup a ptr to the protection data provided by the SCSI host */
2685 sgpe = scsi_prot_sglist(cmd);
2686 protsegcnt = lpfc_cmd->prot_seg_cnt;
2687
2688 if (sgpe && protsegcnt) {
2689
2690 /*
2691 * We will only try to verify guard tag if the segment
2692 * data length is a multiple of the blksize.
2693 */
2694 sgde = scsi_sglist(cmd);
2695 blksize = lpfc_cmd_blksize(cmd);
2696 data_src = (uint8_t *)sg_virt(sgde);
2697 data_len = sgde->length;
2698 if ((data_len & (blksize - 1)) == 0)
2699 chk_guard = 1;
James Smart737d4242013-04-17 20:14:49 -04002700
James Smarte85d8f92013-05-31 17:04:10 -04002701 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
James Smarta6887e22013-04-17 20:18:07 -04002702 start_ref_tag = (uint32_t)scsi_get_lba(cmd); /* Truncate LBA */
James Smart737d4242013-04-17 20:14:49 -04002703 start_app_tag = src->app_tag;
James Smart737d4242013-04-17 20:14:49 -04002704 len = sgpe->length;
2705 while (src && protsegcnt) {
2706 while (len) {
2707
2708 /*
2709 * First check to see if a protection data
2710 * check is valid
2711 */
Dmitry Monakhov128b6f92017-06-29 11:31:12 -07002712 if ((src->ref_tag == T10_PI_REF_ESCAPE) ||
2713 (src->app_tag == T10_PI_APP_ESCAPE)) {
James Smart737d4242013-04-17 20:14:49 -04002714 start_ref_tag++;
2715 goto skipit;
2716 }
2717
James Smart9c6aa9d2013-05-31 17:03:39 -04002718 /* First Guard Tag checking */
2719 if (chk_guard) {
2720 guard_tag = src->guard_tag;
2721 if (lpfc_cmd_guard_csum(cmd))
2722 sum = lpfc_bg_csum(data_src,
2723 blksize);
2724 else
2725 sum = lpfc_bg_crc(data_src,
2726 blksize);
2727 if ((guard_tag != sum)) {
2728 err_type = BGS_GUARD_ERR_MASK;
2729 goto out;
2730 }
James Smart737d4242013-04-17 20:14:49 -04002731 }
2732
2733 /* Reference Tag checking */
2734 ref_tag = be32_to_cpu(src->ref_tag);
2735 if (chk_ref && (ref_tag != start_ref_tag)) {
2736 err_type = BGS_REFTAG_ERR_MASK;
2737 goto out;
2738 }
2739 start_ref_tag++;
2740
James Smart9c6aa9d2013-05-31 17:03:39 -04002741 /* App Tag checking */
2742 app_tag = src->app_tag;
2743 if (chk_app && (app_tag != start_app_tag)) {
2744 err_type = BGS_APPTAG_ERR_MASK;
2745 goto out;
James Smart737d4242013-04-17 20:14:49 -04002746 }
2747skipit:
2748 len -= sizeof(struct scsi_dif_tuple);
2749 if (len < 0)
2750 len = 0;
2751 src++;
2752
2753 data_src += blksize;
2754 data_len -= blksize;
2755
2756 /*
2757 * Are we at the end of the Data segment?
2758 * The data segment is only used for Guard
2759 * tag checking.
2760 */
2761 if (chk_guard && (data_len == 0)) {
2762 chk_guard = 0;
2763 sgde = sg_next(sgde);
2764 if (!sgde)
2765 goto out;
2766
2767 data_src = (uint8_t *)sg_virt(sgde);
2768 data_len = sgde->length;
2769 if ((data_len & (blksize - 1)) == 0)
2770 chk_guard = 1;
2771 }
2772 }
2773
2774 /* Goto the next Protection data segment */
2775 sgpe = sg_next(sgpe);
2776 if (sgpe) {
2777 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
2778 len = sgpe->length;
2779 } else {
2780 src = NULL;
2781 }
2782 protsegcnt--;
2783 }
2784 }
2785out:
2786 if (err_type == BGS_GUARD_ERR_MASK) {
2787 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2788 0x10, 0x1);
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 phba->bg_guard_err_cnt++;
2792 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2793 "9069 BLKGRD: LBA %lx grd_tag error %x != %x\n",
2794 (unsigned long)scsi_get_lba(cmd),
2795 sum, guard_tag);
2796
2797 } else if (err_type == BGS_REFTAG_ERR_MASK) {
2798 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2799 0x10, 0x3);
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02002800 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
2801 SAM_STAT_CHECK_CONDITION;
James Smart737d4242013-04-17 20:14:49 -04002802
2803 phba->bg_reftag_err_cnt++;
2804 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2805 "9066 BLKGRD: LBA %lx ref_tag error %x != %x\n",
2806 (unsigned long)scsi_get_lba(cmd),
2807 ref_tag, start_ref_tag);
2808
2809 } else if (err_type == BGS_APPTAG_ERR_MASK) {
2810 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2811 0x10, 0x2);
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02002812 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
2813 SAM_STAT_CHECK_CONDITION;
James Smart737d4242013-04-17 20:14:49 -04002814
2815 phba->bg_apptag_err_cnt++;
2816 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2817 "9041 BLKGRD: LBA %lx app_tag error %x != %x\n",
2818 (unsigned long)scsi_get_lba(cmd),
2819 app_tag, start_app_tag);
2820 }
2821}
2822
2823
2824/*
James Smarte2a0a9d2008-12-04 22:40:02 -05002825 * This function checks for BlockGuard errors detected by
2826 * the HBA. In case of errors, the ASC/ASCQ fields in the
2827 * sense buffer will be set accordingly, paired with
2828 * ILLEGAL_REQUEST to signal to the kernel that the HBA
2829 * detected corruption.
2830 *
2831 * Returns:
2832 * 0 - No error found
2833 * 1 - BlockGuard error found
2834 * -1 - Internal error (bad profile, ...etc)
2835 */
2836static int
2837lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
2838 struct lpfc_iocbq *pIocbOut)
2839{
2840 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
2841 struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
2842 int ret = 0;
2843 uint32_t bghm = bgf->bghm;
2844 uint32_t bgstat = bgf->bgstat;
2845 uint64_t failing_sector = 0;
2846
James Smarte2a0a9d2008-12-04 22:40:02 -05002847 spin_lock(&_dump_buf_lock);
2848 if (!_dump_buf_done) {
James Smart6a9c52c2009-10-02 15:16:51 -04002849 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9070 BLKGRD: Saving"
2850 " Data for %u blocks to debugfs\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002851 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
James Smart6a9c52c2009-10-02 15:16:51 -04002852 lpfc_debug_save_data(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002853
2854 /* If we have a prot sgl, save the DIF buffer */
2855 if (lpfc_prot_group_type(phba, cmd) ==
2856 LPFC_PG_TYPE_DIF_BUF) {
James Smart6a9c52c2009-10-02 15:16:51 -04002857 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9071 BLKGRD: "
2858 "Saving DIF for %u blocks to debugfs\n",
2859 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
2860 lpfc_debug_save_dif(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002861 }
2862
2863 _dump_buf_done = 1;
2864 }
2865 spin_unlock(&_dump_buf_lock);
2866
2867 if (lpfc_bgs_get_invalid_prof(bgstat)) {
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02002868 cmd->result = DID_ERROR << 16;
James Smart737d4242013-04-17 20:14:49 -04002869 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2870 "9072 BLKGRD: Invalid BG Profile in cmd"
2871 " 0x%x lba 0x%llx blk cnt 0x%x "
2872 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
2873 (unsigned long long)scsi_get_lba(cmd),
2874 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05002875 ret = (-1);
2876 goto out;
2877 }
2878
2879 if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02002880 cmd->result = DID_ERROR << 16;
James Smart737d4242013-04-17 20:14:49 -04002881 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2882 "9073 BLKGRD: Invalid BG PDIF Block in cmd"
2883 " 0x%x lba 0x%llx blk cnt 0x%x "
2884 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
2885 (unsigned long long)scsi_get_lba(cmd),
2886 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05002887 ret = (-1);
2888 goto out;
2889 }
2890
2891 if (lpfc_bgs_get_guard_err(bgstat)) {
2892 ret = 1;
2893
2894 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2895 0x10, 0x1);
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02002896 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
2897 SAM_STAT_CHECK_CONDITION;
James Smarte2a0a9d2008-12-04 22:40:02 -05002898 phba->bg_guard_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04002899 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2900 "9055 BLKGRD: Guard Tag error in cmd"
2901 " 0x%x lba 0x%llx blk cnt 0x%x "
2902 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
2903 (unsigned long long)scsi_get_lba(cmd),
2904 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05002905 }
2906
2907 if (lpfc_bgs_get_reftag_err(bgstat)) {
2908 ret = 1;
2909
2910 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2911 0x10, 0x3);
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02002912 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
2913 SAM_STAT_CHECK_CONDITION;
James Smarte2a0a9d2008-12-04 22:40:02 -05002914
2915 phba->bg_reftag_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04002916 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2917 "9056 BLKGRD: Ref Tag error in cmd"
2918 " 0x%x lba 0x%llx blk cnt 0x%x "
2919 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
2920 (unsigned long long)scsi_get_lba(cmd),
2921 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05002922 }
2923
2924 if (lpfc_bgs_get_apptag_err(bgstat)) {
2925 ret = 1;
2926
2927 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2928 0x10, 0x2);
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02002929 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
2930 SAM_STAT_CHECK_CONDITION;
James Smarte2a0a9d2008-12-04 22:40:02 -05002931
2932 phba->bg_apptag_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04002933 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2934 "9061 BLKGRD: App Tag error in cmd"
2935 " 0x%x lba 0x%llx blk cnt 0x%x "
2936 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
2937 (unsigned long long)scsi_get_lba(cmd),
2938 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05002939 }
2940
2941 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
2942 /*
2943 * setup sense data descriptor 0 per SPC-4 as an information
James Smart7c56b9f2011-07-22 18:36:25 -04002944 * field, and put the failing LBA in it.
2945 * This code assumes there was also a guard/app/ref tag error
2946 * indication.
James Smarte2a0a9d2008-12-04 22:40:02 -05002947 */
James Smart7c56b9f2011-07-22 18:36:25 -04002948 cmd->sense_buffer[7] = 0xc; /* Additional sense length */
2949 cmd->sense_buffer[8] = 0; /* Information descriptor type */
2950 cmd->sense_buffer[9] = 0xa; /* Additional descriptor length */
2951 cmd->sense_buffer[10] = 0x80; /* Validity bit */
James Smartacd68592012-01-18 16:25:09 -05002952
2953 /* bghm is a "on the wire" FC frame based count */
2954 switch (scsi_get_prot_op(cmd)) {
2955 case SCSI_PROT_READ_INSERT:
2956 case SCSI_PROT_WRITE_STRIP:
2957 bghm /= cmd->device->sector_size;
2958 break;
2959 case SCSI_PROT_READ_STRIP:
2960 case SCSI_PROT_WRITE_INSERT:
2961 case SCSI_PROT_READ_PASS:
2962 case SCSI_PROT_WRITE_PASS:
2963 bghm /= (cmd->device->sector_size +
2964 sizeof(struct scsi_dif_tuple));
2965 break;
2966 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002967
2968 failing_sector = scsi_get_lba(cmd);
2969 failing_sector += bghm;
2970
James Smart7c56b9f2011-07-22 18:36:25 -04002971 /* Descriptor Information */
2972 put_unaligned_be64(failing_sector, &cmd->sense_buffer[12]);
James Smarte2a0a9d2008-12-04 22:40:02 -05002973 }
2974
2975 if (!ret) {
2976 /* No error was reported - problem in FW? */
James Smart737d4242013-04-17 20:14:49 -04002977 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2978 "9057 BLKGRD: Unknown error in cmd"
2979 " 0x%x lba 0x%llx blk cnt 0x%x "
2980 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
2981 (unsigned long long)scsi_get_lba(cmd),
2982 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05002983
James Smart737d4242013-04-17 20:14:49 -04002984 /* Calcuate what type of error it was */
2985 lpfc_calc_bg_err(phba, lpfc_cmd);
2986 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002987out:
2988 return ret;
2989}
2990
James Smartea2151b2008-09-07 11:52:10 -04002991/**
James Smartda0436e2009-05-22 14:51:39 -04002992 * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
2993 * @phba: The Hba for which this call is being executed.
2994 * @lpfc_cmd: The scsi buffer which is going to be mapped.
2995 *
2996 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
2997 * field of @lpfc_cmd for device with SLI-4 interface spec.
2998 *
2999 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04003000 * 1 - Error
3001 * 0 - Success
James Smartda0436e2009-05-22 14:51:39 -04003002 **/
3003static int
3004lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3005{
3006 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3007 struct scatterlist *sgel = NULL;
3008 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart0794d602019-01-28 11:14:19 -08003009 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
James Smartfedd3b72011-02-16 12:39:24 -05003010 struct sli4_sge *first_data_sgl;
James Smartda0436e2009-05-22 14:51:39 -04003011 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
3012 dma_addr_t physaddr;
3013 uint32_t num_bde = 0;
3014 uint32_t dma_len;
3015 uint32_t dma_offset = 0;
3016 int nseg;
James Smartfedd3b72011-02-16 12:39:24 -05003017 struct ulp_bde64 *bde;
James Smartda0436e2009-05-22 14:51:39 -04003018
3019 /*
3020 * There are three possibilities here - use scatter-gather segment, use
3021 * the single mapping, or neither. Start the lpfc command prep by
3022 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
3023 * data bde entry.
3024 */
3025 if (scsi_sg_count(scsi_cmnd)) {
3026 /*
3027 * The driver stores the segment count returned from pci_map_sg
3028 * because this a count of dma-mappings used to map the use_sg
3029 * pages. They are not guaranteed to be the same for those
3030 * architectures that implement an IOMMU.
3031 */
3032
3033 nseg = scsi_dma_map(scsi_cmnd);
James Smart5116fbf2015-05-22 10:42:39 -04003034 if (unlikely(nseg <= 0))
James Smartda0436e2009-05-22 14:51:39 -04003035 return 1;
3036 sgl += 1;
3037 /* clear the last flag in the fcp_rsp map entry */
3038 sgl->word2 = le32_to_cpu(sgl->word2);
3039 bf_set(lpfc_sli4_sge_last, sgl, 0);
3040 sgl->word2 = cpu_to_le32(sgl->word2);
3041 sgl += 1;
James Smartfedd3b72011-02-16 12:39:24 -05003042 first_data_sgl = sgl;
James Smartda0436e2009-05-22 14:51:39 -04003043 lpfc_cmd->seg_cnt = nseg;
3044 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04003045 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
3046 " %s: Too many sg segments from "
3047 "dma_map_sg. Config %d, seg_cnt %d\n",
3048 __func__, phba->cfg_sg_seg_cnt,
James Smartda0436e2009-05-22 14:51:39 -04003049 lpfc_cmd->seg_cnt);
James Smart96f70772013-04-17 20:16:15 -04003050 lpfc_cmd->seg_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04003051 scsi_dma_unmap(scsi_cmnd);
3052 return 1;
3053 }
3054
3055 /*
3056 * The driver established a maximum scatter-gather segment count
3057 * during probe that limits the number of sg elements in any
3058 * single scsi command. Just run through the seg_cnt and format
3059 * the sge's.
3060 * When using SLI-3 the driver will try to fit all the BDEs into
3061 * the IOCB. If it can't then the BDEs get added to a BPL as it
3062 * does for SLI-2 mode.
3063 */
3064 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
3065 physaddr = sg_dma_address(sgel);
3066 dma_len = sg_dma_len(sgel);
James Smartda0436e2009-05-22 14:51:39 -04003067 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
3068 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
James Smart05580562011-05-24 11:40:48 -04003069 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -04003070 if ((num_bde + 1) == nseg)
3071 bf_set(lpfc_sli4_sge_last, sgl, 1);
3072 else
3073 bf_set(lpfc_sli4_sge_last, sgl, 0);
3074 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
James Smartf9bb2da2011-10-10 21:34:11 -04003075 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
James Smartda0436e2009-05-22 14:51:39 -04003076 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05003077 sgl->sge_len = cpu_to_le32(dma_len);
James Smartda0436e2009-05-22 14:51:39 -04003078 dma_offset += dma_len;
3079 sgl++;
3080 }
James Smart0bc2b7c2018-02-22 08:18:48 -08003081 /*
3082 * Setup the first Payload BDE. For FCoE we just key off
James Smart414abe02018-06-26 08:24:26 -07003083 * Performance Hints, for FC we use lpfc_enable_pbde.
3084 * We populate words 13-15 of IOCB/WQE.
James Smart0bc2b7c2018-02-22 08:18:48 -08003085 */
3086 if ((phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) ||
James Smart414abe02018-06-26 08:24:26 -07003087 phba->cfg_enable_pbde) {
James Smartfedd3b72011-02-16 12:39:24 -05003088 bde = (struct ulp_bde64 *)
James Smart414abe02018-06-26 08:24:26 -07003089 &(iocb_cmd->unsli3.sli3Words[5]);
James Smartfedd3b72011-02-16 12:39:24 -05003090 bde->addrLow = first_data_sgl->addr_lo;
3091 bde->addrHigh = first_data_sgl->addr_hi;
3092 bde->tus.f.bdeSize =
3093 le32_to_cpu(first_data_sgl->sge_len);
3094 bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
3095 bde->tus.w = cpu_to_le32(bde->tus.w);
3096 }
James Smartda0436e2009-05-22 14:51:39 -04003097 } else {
3098 sgl += 1;
3099 /* clear the last flag in the fcp_rsp map entry */
3100 sgl->word2 = le32_to_cpu(sgl->word2);
3101 bf_set(lpfc_sli4_sge_last, sgl, 1);
3102 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart414abe02018-06-26 08:24:26 -07003103
3104 if ((phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) ||
3105 phba->cfg_enable_pbde) {
3106 bde = (struct ulp_bde64 *)
3107 &(iocb_cmd->unsli3.sli3Words[5]);
3108 memset(bde, 0, (sizeof(uint32_t) * 3));
3109 }
James Smartda0436e2009-05-22 14:51:39 -04003110 }
3111
3112 /*
3113 * Finish initializing those IOCB fields that are dependent on the
3114 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
3115 * explicitly reinitialized.
3116 * all iocb memory resources are reused.
3117 */
3118 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
3119
3120 /*
3121 * Due to difference in data length between DIF/non-DIF paths,
3122 * we need to set word 4 of IOCB here
3123 */
3124 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
James Smart1ba981f2014-02-20 09:56:45 -05003125
3126 /*
3127 * If the OAS driver feature is enabled and the lun is enabled for
3128 * OAS, set the oas iocb related flags.
3129 */
James Smartf38fa0b2014-04-04 13:52:21 -04003130 if ((phba->cfg_fof) && ((struct lpfc_device_data *)
James Smartc92c8412016-07-06 12:36:05 -07003131 scsi_cmnd->device->hostdata)->oas_enabled) {
James Smart9bd2bff52014-09-03 12:57:30 -04003132 lpfc_cmd->cur_iocbq.iocb_flag |= (LPFC_IO_OAS | LPFC_IO_FOF);
James Smartc92c8412016-07-06 12:36:05 -07003133 lpfc_cmd->cur_iocbq.priority = ((struct lpfc_device_data *)
3134 scsi_cmnd->device->hostdata)->priority;
3135 }
James Smartda0436e2009-05-22 14:51:39 -04003136 return 0;
3137}
3138
3139/**
James Smartacd68592012-01-18 16:25:09 -05003140 * lpfc_bg_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3141 * @phba: The Hba for which this call is being executed.
3142 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3143 *
3144 * This is the protection/DIF aware version of
3145 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
3146 * two functions eventually, but for now, it's here
3147 **/
3148static int
3149lpfc_bg_scsi_prep_dma_buf_s4(struct lpfc_hba *phba,
3150 struct lpfc_scsi_buf *lpfc_cmd)
3151{
3152 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3153 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart0794d602019-01-28 11:14:19 -08003154 struct sli4_sge *sgl = (struct sli4_sge *)(lpfc_cmd->dma_sgl);
James Smartacd68592012-01-18 16:25:09 -05003155 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart96f70772013-04-17 20:16:15 -04003156 uint32_t num_sge = 0;
James Smartacd68592012-01-18 16:25:09 -05003157 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
3158 int prot_group_type = 0;
3159 int fcpdl;
James Smart7c4042a2018-11-29 16:09:40 -08003160 struct lpfc_vport *vport = phba->pport;
James Smartacd68592012-01-18 16:25:09 -05003161
3162 /*
3163 * Start the lpfc command prep by bumping the sgl beyond fcp_cmnd
James Smart96f70772013-04-17 20:16:15 -04003164 * fcp_rsp regions to the first data sge entry
James Smartacd68592012-01-18 16:25:09 -05003165 */
3166 if (scsi_sg_count(scsi_cmnd)) {
3167 /*
3168 * The driver stores the segment count returned from pci_map_sg
3169 * because this a count of dma-mappings used to map the use_sg
3170 * pages. They are not guaranteed to be the same for those
3171 * architectures that implement an IOMMU.
3172 */
3173 datasegcnt = dma_map_sg(&phba->pcidev->dev,
3174 scsi_sglist(scsi_cmnd),
3175 scsi_sg_count(scsi_cmnd), datadir);
3176 if (unlikely(!datasegcnt))
3177 return 1;
3178
3179 sgl += 1;
3180 /* clear the last flag in the fcp_rsp map entry */
3181 sgl->word2 = le32_to_cpu(sgl->word2);
3182 bf_set(lpfc_sli4_sge_last, sgl, 0);
3183 sgl->word2 = cpu_to_le32(sgl->word2);
3184
3185 sgl += 1;
3186 lpfc_cmd->seg_cnt = datasegcnt;
James Smart96f70772013-04-17 20:16:15 -04003187
3188 /* First check if data segment count from SCSI Layer is good */
3189 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt)
3190 goto err;
James Smartacd68592012-01-18 16:25:09 -05003191
3192 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
3193
3194 switch (prot_group_type) {
3195 case LPFC_PG_TYPE_NO_DIF:
James Smart96f70772013-04-17 20:16:15 -04003196 /* Here we need to add a DISEED to the count */
3197 if ((lpfc_cmd->seg_cnt + 1) > phba->cfg_total_seg_cnt)
3198 goto err;
3199
3200 num_sge = lpfc_bg_setup_sgl(phba, scsi_cmnd, sgl,
James Smartacd68592012-01-18 16:25:09 -05003201 datasegcnt);
James Smart96f70772013-04-17 20:16:15 -04003202
James Smartacd68592012-01-18 16:25:09 -05003203 /* we should have 2 or more entries in buffer list */
James Smart96f70772013-04-17 20:16:15 -04003204 if (num_sge < 2)
James Smartacd68592012-01-18 16:25:09 -05003205 goto err;
3206 break;
James Smart96f70772013-04-17 20:16:15 -04003207
3208 case LPFC_PG_TYPE_DIF_BUF:
James Smartacd68592012-01-18 16:25:09 -05003209 /*
3210 * This type indicates that protection buffers are
3211 * passed to the driver, so that needs to be prepared
3212 * for DMA
3213 */
3214 protsegcnt = dma_map_sg(&phba->pcidev->dev,
3215 scsi_prot_sglist(scsi_cmnd),
3216 scsi_prot_sg_count(scsi_cmnd), datadir);
3217 if (unlikely(!protsegcnt)) {
3218 scsi_dma_unmap(scsi_cmnd);
3219 return 1;
3220 }
3221
3222 lpfc_cmd->prot_seg_cnt = protsegcnt;
James Smart96f70772013-04-17 20:16:15 -04003223 /*
3224 * There is a minimun of 3 SGEs used for every
3225 * protection data segment.
3226 */
3227 if ((lpfc_cmd->prot_seg_cnt * 3) >
3228 (phba->cfg_total_seg_cnt - 2))
3229 goto err;
James Smartacd68592012-01-18 16:25:09 -05003230
James Smart96f70772013-04-17 20:16:15 -04003231 num_sge = lpfc_bg_setup_sgl_prot(phba, scsi_cmnd, sgl,
James Smartacd68592012-01-18 16:25:09 -05003232 datasegcnt, protsegcnt);
James Smart96f70772013-04-17 20:16:15 -04003233
James Smartacd68592012-01-18 16:25:09 -05003234 /* we should have 3 or more entries in buffer list */
James Smart96f70772013-04-17 20:16:15 -04003235 if ((num_sge < 3) ||
3236 (num_sge > phba->cfg_total_seg_cnt))
James Smartacd68592012-01-18 16:25:09 -05003237 goto err;
3238 break;
James Smart96f70772013-04-17 20:16:15 -04003239
James Smartacd68592012-01-18 16:25:09 -05003240 case LPFC_PG_TYPE_INVALID:
3241 default:
James Smart96f70772013-04-17 20:16:15 -04003242 scsi_dma_unmap(scsi_cmnd);
3243 lpfc_cmd->seg_cnt = 0;
3244
James Smartacd68592012-01-18 16:25:09 -05003245 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
3246 "9083 Unexpected protection group %i\n",
3247 prot_group_type);
3248 return 1;
3249 }
3250 }
3251
James Smart8012cc32012-10-31 14:44:49 -04003252 switch (scsi_get_prot_op(scsi_cmnd)) {
3253 case SCSI_PROT_WRITE_STRIP:
3254 case SCSI_PROT_READ_STRIP:
3255 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_STRIP;
3256 break;
3257 case SCSI_PROT_WRITE_INSERT:
3258 case SCSI_PROT_READ_INSERT:
3259 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_INSERT;
3260 break;
3261 case SCSI_PROT_WRITE_PASS:
3262 case SCSI_PROT_READ_PASS:
3263 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_PASS;
3264 break;
3265 }
3266
James Smartacd68592012-01-18 16:25:09 -05003267 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
James Smartacd68592012-01-18 16:25:09 -05003268 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
3269
3270 /*
3271 * Due to difference in data length between DIF/non-DIF paths,
3272 * we need to set word 4 of IOCB here
3273 */
3274 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
James Smartacd68592012-01-18 16:25:09 -05003275
James Smart9bd2bff52014-09-03 12:57:30 -04003276 /*
James Smart7c4042a2018-11-29 16:09:40 -08003277 * For First burst, we may need to adjust the initial transfer
3278 * length for DIF
3279 */
3280 if (iocb_cmd->un.fcpi.fcpi_XRdy &&
3281 (fcpdl < vport->cfg_first_burst_size))
3282 iocb_cmd->un.fcpi.fcpi_XRdy = fcpdl;
3283
3284 /*
James Smart9bd2bff52014-09-03 12:57:30 -04003285 * If the OAS driver feature is enabled and the lun is enabled for
3286 * OAS, set the oas iocb related flags.
3287 */
3288 if ((phba->cfg_fof) && ((struct lpfc_device_data *)
3289 scsi_cmnd->device->hostdata)->oas_enabled)
3290 lpfc_cmd->cur_iocbq.iocb_flag |= (LPFC_IO_OAS | LPFC_IO_FOF);
3291
James Smartacd68592012-01-18 16:25:09 -05003292 return 0;
3293err:
James Smart96f70772013-04-17 20:16:15 -04003294 if (lpfc_cmd->seg_cnt)
3295 scsi_dma_unmap(scsi_cmnd);
3296 if (lpfc_cmd->prot_seg_cnt)
3297 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
3298 scsi_prot_sg_count(scsi_cmnd),
3299 scsi_cmnd->sc_data_direction);
3300
James Smartacd68592012-01-18 16:25:09 -05003301 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart96f70772013-04-17 20:16:15 -04003302 "9084 Cannot setup S/G List for HBA"
3303 "IO segs %d/%d SGL %d SCSI %d: %d %d\n",
3304 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
3305 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
3306 prot_group_type, num_sge);
3307
3308 lpfc_cmd->seg_cnt = 0;
3309 lpfc_cmd->prot_seg_cnt = 0;
James Smartacd68592012-01-18 16:25:09 -05003310 return 1;
3311}
3312
3313/**
James Smart3772a992009-05-22 14:50:54 -04003314 * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3315 * @phba: The Hba for which this call is being executed.
3316 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3317 *
3318 * This routine wraps the actual DMA mapping function pointer from the
3319 * lpfc_hba struct.
3320 *
3321 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04003322 * 1 - Error
3323 * 0 - Success
James Smart3772a992009-05-22 14:50:54 -04003324 **/
3325static inline int
3326lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3327{
3328 return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
3329}
3330
3331/**
James Smartacd68592012-01-18 16:25:09 -05003332 * lpfc_bg_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3333 * using BlockGuard.
3334 * @phba: The Hba for which this call is being executed.
3335 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3336 *
3337 * This routine wraps the actual DMA mapping function pointer from the
3338 * lpfc_hba struct.
3339 *
3340 * Return codes:
3341 * 1 - Error
3342 * 0 - Success
3343 **/
3344static inline int
3345lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3346{
3347 return phba->lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
3348}
3349
3350/**
James Smart3621a712009-04-06 18:47:14 -04003351 * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
James Smartea2151b2008-09-07 11:52:10 -04003352 * @phba: Pointer to hba context object.
3353 * @vport: Pointer to vport object.
3354 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
3355 * @rsp_iocb: Pointer to response iocb object which reported error.
3356 *
3357 * This function posts an event when there is a SCSI command reporting
3358 * error from the scsi device.
3359 **/
3360static void
3361lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
3362 struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
3363 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3364 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
3365 uint32_t resp_info = fcprsp->rspStatus2;
3366 uint32_t scsi_status = fcprsp->rspStatus3;
3367 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
3368 struct lpfc_fast_path_event *fast_path_evt = NULL;
3369 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
3370 unsigned long flags;
3371
James Smart5989b8d2010-10-22 11:06:56 -04003372 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3373 return;
3374
James Smartea2151b2008-09-07 11:52:10 -04003375 /* If there is queuefull or busy condition send a scsi event */
3376 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
3377 (cmnd->result == SAM_STAT_BUSY)) {
3378 fast_path_evt = lpfc_alloc_fast_evt(phba);
3379 if (!fast_path_evt)
3380 return;
3381 fast_path_evt->un.scsi_evt.event_type =
3382 FC_REG_SCSI_EVENT;
3383 fast_path_evt->un.scsi_evt.subcategory =
3384 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
3385 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
3386 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
3387 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
3388 &pnode->nlp_portname, sizeof(struct lpfc_name));
3389 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
3390 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3391 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
3392 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
3393 fast_path_evt = lpfc_alloc_fast_evt(phba);
3394 if (!fast_path_evt)
3395 return;
3396 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
3397 FC_REG_SCSI_EVENT;
3398 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
3399 LPFC_EVENT_CHECK_COND;
3400 fast_path_evt->un.check_cond_evt.scsi_event.lun =
3401 cmnd->device->lun;
3402 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
3403 &pnode->nlp_portname, sizeof(struct lpfc_name));
3404 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
3405 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3406 fast_path_evt->un.check_cond_evt.sense_key =
3407 cmnd->sense_buffer[2] & 0xf;
3408 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
3409 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
3410 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3411 fcpi_parm &&
3412 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
3413 ((scsi_status == SAM_STAT_GOOD) &&
3414 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
3415 /*
3416 * If status is good or resid does not match with fcp_param and
3417 * there is valid fcpi_parm, then there is a read_check error
3418 */
3419 fast_path_evt = lpfc_alloc_fast_evt(phba);
3420 if (!fast_path_evt)
3421 return;
3422 fast_path_evt->un.read_check_error.header.event_type =
3423 FC_REG_FABRIC_EVENT;
3424 fast_path_evt->un.read_check_error.header.subcategory =
3425 LPFC_EVENT_FCPRDCHKERR;
3426 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
3427 &pnode->nlp_portname, sizeof(struct lpfc_name));
3428 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
3429 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3430 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
3431 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
3432 fast_path_evt->un.read_check_error.fcpiparam =
3433 fcpi_parm;
3434 } else
3435 return;
3436
3437 fast_path_evt->vport = vport;
3438 spin_lock_irqsave(&phba->hbalock, flags);
3439 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
3440 spin_unlock_irqrestore(&phba->hbalock, flags);
3441 lpfc_worker_wake_up(phba);
3442 return;
3443}
James Smart9bad7672008-12-04 22:39:02 -05003444
3445/**
James Smartf1126682009-06-10 17:22:44 -04003446 * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
James Smart3772a992009-05-22 14:50:54 -04003447 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -05003448 * @psb: The scsi buffer which is going to be un-mapped.
3449 *
3450 * This routine does DMA un-mapping of scatter gather list of scsi command
James Smart3772a992009-05-22 14:50:54 -04003451 * field of @lpfc_cmd for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05003452 **/
dea31012005-04-17 16:05:31 -05003453static void
James Smartf1126682009-06-10 17:22:44 -04003454lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James Smartbcf4dbf2006-07-06 15:50:08 -04003455{
3456 /*
3457 * There are only two special cases to consider. (1) the scsi command
3458 * requested scatter-gather usage or (2) the scsi command allocated
3459 * a request buffer, but did not request use_sg. There is a third
3460 * case, but it does not require resource deallocation.
3461 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003462 if (psb->seg_cnt > 0)
3463 scsi_dma_unmap(psb->pCmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003464 if (psb->prot_seg_cnt > 0)
3465 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
3466 scsi_prot_sg_count(psb->pCmd),
3467 psb->pCmd->sc_data_direction);
James Smartbcf4dbf2006-07-06 15:50:08 -04003468}
3469
James Smart9bad7672008-12-04 22:39:02 -05003470/**
James Smart3621a712009-04-06 18:47:14 -04003471 * lpfc_handler_fcp_err - FCP response handler
James Smart9bad7672008-12-04 22:39:02 -05003472 * @vport: The virtual port for which this call is being executed.
3473 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
3474 * @rsp_iocb: The response IOCB which contains FCP error.
3475 *
3476 * This routine is called to process response IOCB with status field
3477 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
3478 * based upon SCSI and FCP error.
3479 **/
James Smartbcf4dbf2006-07-06 15:50:08 -04003480static void
James Smart2e0fef82007-06-17 19:56:36 -05003481lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
3482 struct lpfc_iocbq *rsp_iocb)
dea31012005-04-17 16:05:31 -05003483{
James Smart5afab6b2015-12-16 18:12:01 -05003484 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003485 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3486 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
3487 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
James Smart7054a602007-04-25 09:52:34 -04003488 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
dea31012005-04-17 16:05:31 -05003489 uint32_t resp_info = fcprsp->rspStatus2;
3490 uint32_t scsi_status = fcprsp->rspStatus3;
James Smartc7743952006-12-02 13:34:42 -05003491 uint32_t *lp;
dea31012005-04-17 16:05:31 -05003492 uint32_t host_status = DID_OK;
3493 uint32_t rsplen = 0;
James Smart5afab6b2015-12-16 18:12:01 -05003494 uint32_t fcpDl;
James Smartc7743952006-12-02 13:34:42 -05003495 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea31012005-04-17 16:05:31 -05003496
James Smartea2151b2008-09-07 11:52:10 -04003497
dea31012005-04-17 16:05:31 -05003498 /*
3499 * If this is a task management command, there is no
3500 * scsi packet associated with this lpfc_cmd. The driver
3501 * consumes it.
3502 */
3503 if (fcpcmd->fcpCntl2) {
3504 scsi_status = 0;
3505 goto out;
3506 }
3507
James Smart6a9c52c2009-10-02 15:16:51 -04003508 if (resp_info & RSP_LEN_VALID) {
3509 rsplen = be32_to_cpu(fcprsp->rspRspLen);
James Smarte40a02c2010-02-26 14:13:54 -05003510 if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
James Smart6a9c52c2009-10-02 15:16:51 -04003511 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3512 "2719 Invalid response length: "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02003513 "tgt x%x lun x%llx cmnd x%x rsplen x%x\n",
James Smart6a9c52c2009-10-02 15:16:51 -04003514 cmnd->device->id,
3515 cmnd->device->lun, cmnd->cmnd[0],
3516 rsplen);
3517 host_status = DID_ERROR;
3518 goto out;
3519 }
James Smarte40a02c2010-02-26 14:13:54 -05003520 if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
3521 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3522 "2757 Protocol failure detected during "
3523 "processing of FCP I/O op: "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02003524 "tgt x%x lun x%llx cmnd x%x rspInfo3 x%x\n",
James Smarte40a02c2010-02-26 14:13:54 -05003525 cmnd->device->id,
3526 cmnd->device->lun, cmnd->cmnd[0],
3527 fcprsp->rspInfo3);
3528 host_status = DID_ERROR;
3529 goto out;
3530 }
James Smart6a9c52c2009-10-02 15:16:51 -04003531 }
3532
James Smartc7743952006-12-02 13:34:42 -05003533 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
3534 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
3535 if (snslen > SCSI_SENSE_BUFFERSIZE)
3536 snslen = SCSI_SENSE_BUFFERSIZE;
3537
3538 if (resp_info & RSP_LEN_VALID)
3539 rsplen = be32_to_cpu(fcprsp->rspRspLen);
3540 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
3541 }
3542 lp = (uint32_t *)cmnd->sense_buffer;
3543
James Smartaa1c7ee2012-08-14 14:26:06 -04003544 /* special handling for under run conditions */
3545 if (!scsi_status && (resp_info & RESID_UNDER)) {
3546 /* don't log under runs if fcp set... */
3547 if (vport->cfg_log_verbose & LOG_FCP)
3548 logit = LOG_FCP_ERROR;
3549 /* unless operator says so */
3550 if (vport->cfg_log_verbose & LOG_FCP_UNDER)
3551 logit = LOG_FCP_UNDER;
3552 }
James Smartc7743952006-12-02 13:34:42 -05003553
James Smarte8b62012007-08-02 11:10:09 -04003554 lpfc_printf_vlog(vport, KERN_WARNING, logit,
James Smarte2a0a9d2008-12-04 22:40:02 -05003555 "9024 FCP command x%x failed: x%x SNS x%x x%x "
James Smarte8b62012007-08-02 11:10:09 -04003556 "Data: x%x x%x x%x x%x x%x\n",
3557 cmnd->cmnd[0], scsi_status,
3558 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
3559 be32_to_cpu(fcprsp->rspResId),
3560 be32_to_cpu(fcprsp->rspSnsLen),
3561 be32_to_cpu(fcprsp->rspRspLen),
3562 fcprsp->rspInfo3);
dea31012005-04-17 16:05:31 -05003563
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003564 scsi_set_resid(cmnd, 0);
James Smart5afab6b2015-12-16 18:12:01 -05003565 fcpDl = be32_to_cpu(fcpcmd->fcpDl);
dea31012005-04-17 16:05:31 -05003566 if (resp_info & RESID_UNDER) {
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003567 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea31012005-04-17 16:05:31 -05003568
James Smart73d91e52011-10-10 21:32:10 -04003569 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_UNDER,
James Smart45634a862018-01-30 15:59:00 -08003570 "9025 FCP Underrun, expected %d, "
James Smarte8b62012007-08-02 11:10:09 -04003571 "residual %d Data: x%x x%x x%x\n",
James Smart5afab6b2015-12-16 18:12:01 -05003572 fcpDl,
James Smarte8b62012007-08-02 11:10:09 -04003573 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
3574 cmnd->underflow);
dea31012005-04-17 16:05:31 -05003575
3576 /*
James Smart45634a862018-01-30 15:59:00 -08003577 * If there is an under run, check if under run reported by
James Smart7054a602007-04-25 09:52:34 -04003578 * storage array is same as the under run reported by HBA.
3579 * If this is not same, there is a dropped frame.
3580 */
James Smart45634a862018-01-30 15:59:00 -08003581 if (fcpi_parm && (scsi_get_resid(cmnd) != fcpi_parm)) {
James Smarte8b62012007-08-02 11:10:09 -04003582 lpfc_printf_vlog(vport, KERN_WARNING,
3583 LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05003584 "9026 FCP Read Check Error "
James Smarte8b62012007-08-02 11:10:09 -04003585 "and Underrun Data: x%x x%x x%x x%x\n",
James Smart5afab6b2015-12-16 18:12:01 -05003586 fcpDl,
James Smarte8b62012007-08-02 11:10:09 -04003587 scsi_get_resid(cmnd), fcpi_parm,
3588 cmnd->cmnd[0]);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003589 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
James Smart7054a602007-04-25 09:52:34 -04003590 host_status = DID_ERROR;
3591 }
3592 /*
dea31012005-04-17 16:05:31 -05003593 * The cmnd->underflow is the minimum number of bytes that must
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003594 * be transferred for this command. Provided a sense condition
dea31012005-04-17 16:05:31 -05003595 * is not present, make sure the actual amount transferred is at
3596 * least the underflow value or fail.
3597 */
3598 if (!(resp_info & SNS_LEN_VALID) &&
3599 (scsi_status == SAM_STAT_GOOD) &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003600 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
3601 < cmnd->underflow)) {
James Smarte8b62012007-08-02 11:10:09 -04003602 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05003603 "9027 FCP command x%x residual "
James Smarte8b62012007-08-02 11:10:09 -04003604 "underrun converted to error "
3605 "Data: x%x x%x x%x\n",
James Smart66dbfbe2007-08-05 06:08:38 -04003606 cmnd->cmnd[0], scsi_bufflen(cmnd),
James Smarte8b62012007-08-02 11:10:09 -04003607 scsi_get_resid(cmnd), cmnd->underflow);
dea31012005-04-17 16:05:31 -05003608 host_status = DID_ERROR;
3609 }
3610 } else if (resp_info & RESID_OVER) {
James Smarte8b62012007-08-02 11:10:09 -04003611 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05003612 "9028 FCP command x%x residual overrun error. "
James Smarte4e74272009-07-19 10:01:38 -04003613 "Data: x%x x%x\n", cmnd->cmnd[0],
James Smarte8b62012007-08-02 11:10:09 -04003614 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea31012005-04-17 16:05:31 -05003615 host_status = DID_ERROR;
3616
3617 /*
3618 * Check SLI validation that all the transfer was actually done
James Smart26373d22013-09-06 12:19:17 -04003619 * (fcpi_parm should be zero). Apply check only to reads.
dea31012005-04-17 16:05:31 -05003620 */
James Smart5afab6b2015-12-16 18:12:01 -05003621 } else if (fcpi_parm) {
James Smarte8b62012007-08-02 11:10:09 -04003622 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
James Smart5afab6b2015-12-16 18:12:01 -05003623 "9029 FCP %s Check Error xri x%x Data: "
James Smarteee88772010-09-29 11:19:08 -04003624 "x%x x%x x%x x%x x%x\n",
James Smart5afab6b2015-12-16 18:12:01 -05003625 ((cmnd->sc_data_direction == DMA_FROM_DEVICE) ?
3626 "Read" : "Write"),
3627 ((phba->sli_rev == LPFC_SLI_REV4) ?
3628 lpfc_cmd->cur_iocbq.sli4_xritag :
3629 rsp_iocb->iocb.ulpContext),
3630 fcpDl, be32_to_cpu(fcprsp->rspResId),
James Smarteee88772010-09-29 11:19:08 -04003631 fcpi_parm, cmnd->cmnd[0], scsi_status);
James Smart5afab6b2015-12-16 18:12:01 -05003632
3633 /* There is some issue with the LPe12000 that causes it
3634 * to miscalculate the fcpi_parm and falsely trip this
3635 * recovery logic. Detect this case and don't error when true.
3636 */
3637 if (fcpi_parm > fcpDl)
3638 goto out;
3639
James Smarteee88772010-09-29 11:19:08 -04003640 switch (scsi_status) {
3641 case SAM_STAT_GOOD:
3642 case SAM_STAT_CHECK_CONDITION:
3643 /* Fabric dropped a data frame. Fail any successful
3644 * command in which we detected dropped frames.
3645 * A status of good or some check conditions could
3646 * be considered a successful command.
3647 */
3648 host_status = DID_ERROR;
3649 break;
3650 }
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003651 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea31012005-04-17 16:05:31 -05003652 }
3653
3654 out:
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02003655 cmnd->result = host_status << 16 | scsi_status;
James Smartea2151b2008-09-07 11:52:10 -04003656 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
dea31012005-04-17 16:05:31 -05003657}
3658
James Smart9bad7672008-12-04 22:39:02 -05003659/**
James Smart8b0dff12015-05-22 10:42:38 -04003660 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
3661 * @phba: Pointer to HBA context object.
3662 *
3663 * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
3664 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
3665 * held.
3666 * If scsi-mq is enabled, get the default block layer mapping of software queues
3667 * to hardware queues. This information is saved in request tag.
3668 *
3669 * Return: index into SLI4 fast-path FCP queue index.
3670 **/
3671int lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba,
3672 struct lpfc_scsi_buf *lpfc_cmd)
3673{
3674 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3675 struct lpfc_vector_map_info *cpup;
3676 int chann, cpu;
3677 uint32_t tag;
3678 uint16_t hwq;
3679
Jens Axboef664a3c2018-11-01 16:36:27 -06003680 if (cmnd) {
James Smart8b0dff12015-05-22 10:42:38 -04003681 tag = blk_mq_unique_tag(cmnd->request);
3682 hwq = blk_mq_unique_tag_to_hwq(tag);
3683
3684 return hwq;
3685 }
3686
James Smartcdb42be2019-01-28 11:14:21 -08003687 if (phba->cfg_fcp_io_sched == LPFC_FCP_SCHED_BY_CPU &&
3688 phba->cfg_hdw_queue > 1) {
James Smart0794d602019-01-28 11:14:19 -08003689 cpu = lpfc_cmd->cpu;
James Smart8b0dff12015-05-22 10:42:38 -04003690 if (cpu < phba->sli4_hba.num_present_cpu) {
3691 cpup = phba->sli4_hba.cpu_map;
3692 cpup += cpu;
3693 return cpup->channel_id;
3694 }
3695 }
3696 chann = atomic_add_return(1, &phba->fcp_qidx);
James Smartcdb42be2019-01-28 11:14:21 -08003697 chann = chann % phba->cfg_hdw_queue;
James Smart8b0dff12015-05-22 10:42:38 -04003698 return chann;
3699}
3700
3701
3702/**
James Smart3621a712009-04-06 18:47:14 -04003703 * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
James Smart9bad7672008-12-04 22:39:02 -05003704 * @phba: The Hba for which this call is being executed.
3705 * @pIocbIn: The command IOCBQ for the scsi cmnd.
James Smart3772a992009-05-22 14:50:54 -04003706 * @pIocbOut: The response IOCBQ for the scsi cmnd.
James Smart9bad7672008-12-04 22:39:02 -05003707 *
3708 * This routine assigns scsi command result by looking into response IOCB
3709 * status field appropriately. This routine handles QUEUE FULL condition as
3710 * well by ramping down device queue depth.
3711 **/
dea31012005-04-17 16:05:31 -05003712static void
3713lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
3714 struct lpfc_iocbq *pIocbOut)
3715{
3716 struct lpfc_scsi_buf *lpfc_cmd =
3717 (struct lpfc_scsi_buf *) pIocbIn->context1;
James Smart2e0fef82007-06-17 19:56:36 -05003718 struct lpfc_vport *vport = pIocbIn->vport;
dea31012005-04-17 16:05:31 -05003719 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
3720 struct lpfc_nodelist *pnode = rdata->pnode;
James Smart75baf692010-06-08 18:31:21 -04003721 struct scsi_cmnd *cmd;
James Smartfa61a542008-01-11 01:52:42 -05003722 unsigned long flags;
James Smartea2151b2008-09-07 11:52:10 -04003723 struct lpfc_fast_path_event *fast_path_evt;
James Smart75baf692010-06-08 18:31:21 -04003724 struct Scsi_Host *shost;
James Smart73d91e52011-10-10 21:32:10 -04003725 uint32_t logit = LOG_FCP;
dea31012005-04-17 16:05:31 -05003726
James Smart2cee7802017-06-01 21:07:02 -07003727 atomic_inc(&phba->fc4ScsiIoCmpls);
James Smart895427b2017-02-12 13:52:30 -08003728
James Smart75baf692010-06-08 18:31:21 -04003729 /* Sanity check on return of outstanding command */
James Smart75baf692010-06-08 18:31:21 -04003730 cmd = lpfc_cmd->pCmd;
James Smartc90261d2015-12-16 18:11:57 -05003731 if (!cmd)
3732 return;
James Smart75baf692010-06-08 18:31:21 -04003733 shost = cmd->device->host;
3734
James Smarte3d2b802012-08-14 14:25:43 -04003735 lpfc_cmd->result = (pIocbOut->iocb.un.ulpWord[4] & IOERR_PARAM_MASK);
dea31012005-04-17 16:05:31 -05003736 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
James Smart341af102010-01-26 23:07:37 -05003737 /* pick up SLI4 exhange busy status from HBA */
3738 lpfc_cmd->exch_busy = pIocbOut->iocb_flag & LPFC_EXCHANGE_BUSY;
3739
James Smart9a6b09c2012-03-01 22:37:42 -05003740#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
3741 if (lpfc_cmd->prot_data_type) {
3742 struct scsi_dif_tuple *src = NULL;
3743
3744 src = (struct scsi_dif_tuple *)lpfc_cmd->prot_data_segment;
3745 /*
3746 * Used to restore any changes to protection
3747 * data for error injection.
3748 */
3749 switch (lpfc_cmd->prot_data_type) {
3750 case LPFC_INJERR_REFTAG:
3751 src->ref_tag =
3752 lpfc_cmd->prot_data;
3753 break;
3754 case LPFC_INJERR_APPTAG:
3755 src->app_tag =
3756 (uint16_t)lpfc_cmd->prot_data;
3757 break;
3758 case LPFC_INJERR_GUARD:
3759 src->guard_tag =
3760 (uint16_t)lpfc_cmd->prot_data;
3761 break;
3762 default:
3763 break;
3764 }
3765
3766 lpfc_cmd->prot_data = 0;
3767 lpfc_cmd->prot_data_type = 0;
3768 lpfc_cmd->prot_data_segment = NULL;
3769 }
3770#endif
James Smart2ea259e2017-02-12 13:52:27 -08003771
dea31012005-04-17 16:05:31 -05003772 if (lpfc_cmd->status) {
3773 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
3774 (lpfc_cmd->result & IOERR_DRVR_MASK))
3775 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
3776 else if (lpfc_cmd->status >= IOSTAT_CNT)
3777 lpfc_cmd->status = IOSTAT_DEFAULT;
James Smartaa1c7ee2012-08-14 14:26:06 -04003778 if (lpfc_cmd->status == IOSTAT_FCP_RSP_ERROR &&
3779 !lpfc_cmd->fcp_rsp->rspStatus3 &&
3780 (lpfc_cmd->fcp_rsp->rspStatus2 & RESID_UNDER) &&
3781 !(vport->cfg_log_verbose & LOG_FCP_UNDER))
James Smart73d91e52011-10-10 21:32:10 -04003782 logit = 0;
3783 else
3784 logit = LOG_FCP | LOG_FCP_UNDER;
3785 lpfc_printf_vlog(vport, KERN_WARNING, logit,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02003786 "9030 FCP cmd x%x failed <%d/%lld> "
James Smart5a0d80f2012-05-09 21:18:20 -04003787 "status: x%x result: x%x "
3788 "sid: x%x did: x%x oxid: x%x "
3789 "Data: x%x x%x\n",
James Smart73d91e52011-10-10 21:32:10 -04003790 cmd->cmnd[0],
3791 cmd->device ? cmd->device->id : 0xffff,
3792 cmd->device ? cmd->device->lun : 0xffff,
3793 lpfc_cmd->status, lpfc_cmd->result,
James Smart3bf41ba2013-05-31 17:03:18 -04003794 vport->fc_myDID,
3795 (pnode) ? pnode->nlp_DID : 0,
James Smart5a0d80f2012-05-09 21:18:20 -04003796 phba->sli_rev == LPFC_SLI_REV4 ?
3797 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
James Smart73d91e52011-10-10 21:32:10 -04003798 pIocbOut->iocb.ulpContext,
3799 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
dea31012005-04-17 16:05:31 -05003800
3801 switch (lpfc_cmd->status) {
3802 case IOSTAT_FCP_RSP_ERROR:
3803 /* Call FCP RSP handler to determine result */
James Smart2e0fef82007-06-17 19:56:36 -05003804 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea31012005-04-17 16:05:31 -05003805 break;
3806 case IOSTAT_NPORT_BSY:
3807 case IOSTAT_FABRIC_BSY:
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02003808 cmd->result = DID_TRANSPORT_DISRUPTED << 16;
James Smartea2151b2008-09-07 11:52:10 -04003809 fast_path_evt = lpfc_alloc_fast_evt(phba);
3810 if (!fast_path_evt)
3811 break;
3812 fast_path_evt->un.fabric_evt.event_type =
3813 FC_REG_FABRIC_EVENT;
3814 fast_path_evt->un.fabric_evt.subcategory =
3815 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
3816 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
3817 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
3818 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
3819 &pnode->nlp_portname,
3820 sizeof(struct lpfc_name));
3821 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
3822 &pnode->nlp_nodename,
3823 sizeof(struct lpfc_name));
3824 }
3825 fast_path_evt->vport = vport;
3826 fast_path_evt->work_evt.evt =
3827 LPFC_EVT_FASTPATH_MGMT_EVT;
3828 spin_lock_irqsave(&phba->hbalock, flags);
3829 list_add_tail(&fast_path_evt->work_evt.evt_listp,
3830 &phba->work_list);
3831 spin_unlock_irqrestore(&phba->hbalock, flags);
3832 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05003833 break;
James Smart92d7f7b2007-06-17 19:56:38 -05003834 case IOSTAT_LOCAL_REJECT:
James Smart1151e3e2011-02-16 12:39:35 -05003835 case IOSTAT_REMOTE_STOP:
James Smartab56dc22011-02-16 12:39:57 -05003836 if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
3837 lpfc_cmd->result ==
3838 IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
3839 lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
3840 lpfc_cmd->result ==
3841 IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02003842 cmd->result = DID_NO_CONNECT << 16;
James Smartab56dc22011-02-16 12:39:57 -05003843 break;
3844 }
James Smartd7c255b2008-08-24 21:50:00 -04003845 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
James Smart92d7f7b2007-06-17 19:56:38 -05003846 lpfc_cmd->result == IOERR_NO_RESOURCES ||
James Smartb92938b2010-06-07 15:24:12 -04003847 lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
3848 lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02003849 cmd->result = DID_REQUEUE << 16;
James Smart58da1ff2008-04-07 10:15:56 -04003850 break;
James Smarte2a0a9d2008-12-04 22:40:02 -05003851 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003852 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
3853 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
3854 pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
3855 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
3856 /*
3857 * This is a response for a BG enabled
3858 * cmd. Parse BG error
3859 */
3860 lpfc_parse_bg_err(phba, lpfc_cmd,
3861 pIocbOut);
3862 break;
3863 } else {
3864 lpfc_printf_vlog(vport, KERN_WARNING,
3865 LOG_BG,
3866 "9031 non-zero BGSTAT "
James Smart6a9c52c2009-10-02 15:16:51 -04003867 "on unprotected cmd\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05003868 }
3869 }
James Smart1151e3e2011-02-16 12:39:35 -05003870 if ((lpfc_cmd->status == IOSTAT_REMOTE_STOP)
3871 && (phba->sli_rev == LPFC_SLI_REV4)
3872 && (pnode && NLP_CHK_NODE_ACT(pnode))) {
3873 /* This IO was aborted by the target, we don't
3874 * know the rxid and because we did not send the
3875 * ABTS we cannot generate and RRQ.
3876 */
3877 lpfc_set_rrq_active(phba, pnode,
James Smartee0f4fe2012-05-09 21:19:14 -04003878 lpfc_cmd->cur_iocbq.sli4_lxritag,
3879 0, 0);
James Smart1151e3e2011-02-16 12:39:35 -05003880 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003881 /* else: fall through */
dea31012005-04-17 16:05:31 -05003882 default:
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02003883 cmd->result = DID_ERROR << 16;
dea31012005-04-17 16:05:31 -05003884 break;
3885 }
3886
James Smart58da1ff2008-04-07 10:15:56 -04003887 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003888 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02003889 cmd->result = DID_TRANSPORT_DISRUPTED << 16 |
3890 SAM_STAT_BUSY;
James Smartab56dc22011-02-16 12:39:57 -05003891 } else
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02003892 cmd->result = DID_OK << 16;
dea31012005-04-17 16:05:31 -05003893
3894 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
3895 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
3896
James Smarte8b62012007-08-02 11:10:09 -04003897 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02003898 "0710 Iodone <%d/%llu> cmd %p, error "
James Smarte8b62012007-08-02 11:10:09 -04003899 "x%x SNS x%x x%x Data: x%x x%x\n",
3900 cmd->device->id, cmd->device->lun, cmd,
3901 cmd->result, *lp, *(lp + 3), cmd->retries,
3902 scsi_get_resid(cmd));
dea31012005-04-17 16:05:31 -05003903 }
3904
James Smartea2151b2008-09-07 11:52:10 -04003905 lpfc_update_stats(phba, lpfc_cmd);
James Smart977b5a02008-09-07 11:52:04 -04003906 if (vport->cfg_max_scsicmpl_time &&
3907 time_after(jiffies, lpfc_cmd->start_time +
3908 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
James Smarta257bf92009-04-06 18:48:10 -04003909 spin_lock_irqsave(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05003910 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
3911 if (pnode->cmd_qdepth >
3912 atomic_read(&pnode->cmd_pending) &&
3913 (atomic_read(&pnode->cmd_pending) >
3914 LPFC_MIN_TGT_QDEPTH) &&
3915 ((cmd->cmnd[0] == READ_10) ||
3916 (cmd->cmnd[0] == WRITE_10)))
3917 pnode->cmd_qdepth =
3918 atomic_read(&pnode->cmd_pending);
James Smart977b5a02008-09-07 11:52:04 -04003919
James Smart109f6ed2008-12-04 22:39:08 -05003920 pnode->last_change_time = jiffies;
3921 }
James Smarta257bf92009-04-06 18:48:10 -04003922 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart977b5a02008-09-07 11:52:04 -04003923 }
James Smart1dcb58e2007-04-25 09:51:30 -04003924 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James Smarta257bf92009-04-06 18:48:10 -04003925
James Smartca7fb762018-09-10 10:30:44 -07003926 /* If pCmd was set to NULL from abort path, do not call scsi_done */
3927 if (xchg(&lpfc_cmd->pCmd, NULL) == NULL) {
3928 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smart2c4c9142018-11-29 16:09:31 -08003929 "5688 FCP cmd already NULL, sid: 0x%06x, "
James Smartca7fb762018-09-10 10:30:44 -07003930 "did: 0x%06x, oxid: 0x%04x\n",
3931 vport->fc_myDID,
3932 (pnode) ? pnode->nlp_DID : 0,
3933 phba->sli_rev == LPFC_SLI_REV4 ?
3934 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff);
3935 return;
3936 }
James Smart92e3af62012-08-14 14:26:28 -04003937
James Smart89533e92016-10-13 15:06:15 -07003938 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
3939 cmd->scsi_done(cmd);
3940
James Smartfa61a542008-01-11 01:52:42 -05003941 /*
3942 * If there is a thread waiting for command completion
3943 * wake up the thread.
3944 */
James Smarta257bf92009-04-06 18:48:10 -04003945 spin_lock_irqsave(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05003946 if (lpfc_cmd->waitq)
3947 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04003948 spin_unlock_irqrestore(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05003949
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003950 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05003951}
3952
James Smart34b02dc2008-08-24 21:49:55 -04003953/**
Alexey Kardashevskiy8b2564e2015-04-28 18:26:22 +10003954 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
3955 * @data: A pointer to the immediate command data portion of the IOCB.
3956 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
3957 *
3958 * The routine copies the entire FCP command from @fcp_cmnd to @data while
3959 * byte swapping the data to big endian format for transmission on the wire.
3960 **/
3961static void
3962lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
3963{
3964 int i, j;
3965 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
3966 i += sizeof(uint32_t), j++) {
3967 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
3968 }
3969}
3970
3971/**
James Smartf1126682009-06-10 17:22:44 -04003972 * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05003973 * @vport: The virtual port for which this call is being executed.
3974 * @lpfc_cmd: The scsi command which needs to send.
3975 * @pnode: Pointer to lpfc_nodelist.
3976 *
3977 * This routine initializes fcp_cmnd and iocb data structure from scsi command
James Smart3772a992009-05-22 14:50:54 -04003978 * to transfer for device with SLI3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05003979 **/
dea31012005-04-17 16:05:31 -05003980static void
James Smartf1126682009-06-10 17:22:44 -04003981lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
James Smart2e0fef82007-06-17 19:56:36 -05003982 struct lpfc_nodelist *pnode)
dea31012005-04-17 16:05:31 -05003983{
James Smart2e0fef82007-06-17 19:56:36 -05003984 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003985 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3986 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3987 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
3988 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
3989 int datadir = scsi_cmnd->sc_data_direction;
James Smart027140e2012-08-03 12:35:44 -04003990 uint8_t *ptr;
3991 bool sli4;
James Smart98bbf5f2013-09-06 12:18:45 -04003992 uint32_t fcpdl;
dea31012005-04-17 16:05:31 -05003993
James Smart58da1ff2008-04-07 10:15:56 -04003994 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3995 return;
3996
dea31012005-04-17 16:05:31 -05003997 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
James.Smart@Emulex.Com69859dc2005-08-10 15:02:37 -04003998 /* clear task management bits */
3999 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea31012005-04-17 16:05:31 -05004000
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -04004001 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
4002 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05004003
James Smart027140e2012-08-03 12:35:44 -04004004 ptr = &fcp_cmnd->fcpCdb[0];
4005 memcpy(ptr, scsi_cmnd->cmnd, scsi_cmnd->cmd_len);
4006 if (scsi_cmnd->cmd_len < LPFC_FCP_CDB_LEN) {
4007 ptr += scsi_cmnd->cmd_len;
4008 memset(ptr, 0, (LPFC_FCP_CDB_LEN - scsi_cmnd->cmd_len));
4009 }
4010
Christoph Hellwig50668632014-10-30 14:30:06 +01004011 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
dea31012005-04-17 16:05:31 -05004012
James Smart027140e2012-08-03 12:35:44 -04004013 sli4 = (phba->sli_rev == LPFC_SLI_REV4);
James Smart6acb3482014-04-04 13:51:25 -04004014 piocbq->iocb.un.fcpi.fcpi_XRdy = 0;
James Smart027140e2012-08-03 12:35:44 -04004015
dea31012005-04-17 16:05:31 -05004016 /*
4017 * There are three possibilities here - use scatter-gather segment, use
4018 * the single mapping, or neither. Start the lpfc command prep by
4019 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
4020 * data bde entry.
4021 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05004022 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05004023 if (datadir == DMA_TO_DEVICE) {
4024 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
James Smart182ba752013-07-15 18:34:05 -04004025 iocb_cmd->ulpPU = PARM_READ_CHECK;
James Smart3cb01c52013-07-15 18:35:04 -04004026 if (vport->cfg_first_burst_size &&
4027 (pnode->nlp_flag & NLP_FIRSTBURST)) {
James Smart98bbf5f2013-09-06 12:18:45 -04004028 fcpdl = scsi_bufflen(scsi_cmnd);
4029 if (fcpdl < vport->cfg_first_burst_size)
4030 piocbq->iocb.un.fcpi.fcpi_XRdy = fcpdl;
4031 else
4032 piocbq->iocb.un.fcpi.fcpi_XRdy =
4033 vport->cfg_first_burst_size;
James Smart3cb01c52013-07-15 18:35:04 -04004034 }
dea31012005-04-17 16:05:31 -05004035 fcp_cmnd->fcpCntl3 = WRITE_DATA;
James Smart2cee7802017-06-01 21:07:02 -07004036 atomic_inc(&phba->fc4ScsiOutputRequests);
dea31012005-04-17 16:05:31 -05004037 } else {
4038 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
4039 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05004040 fcp_cmnd->fcpCntl3 = READ_DATA;
James Smart2cee7802017-06-01 21:07:02 -07004041 atomic_inc(&phba->fc4ScsiInputRequests);
dea31012005-04-17 16:05:31 -05004042 }
4043 } else {
4044 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
4045 iocb_cmd->un.fcpi.fcpi_parm = 0;
4046 iocb_cmd->ulpPU = 0;
4047 fcp_cmnd->fcpCntl3 = 0;
James Smart2cee7802017-06-01 21:07:02 -07004048 atomic_inc(&phba->fc4ScsiControlRequests);
dea31012005-04-17 16:05:31 -05004049 }
Alexey Kardashevskiy8b2564e2015-04-28 18:26:22 +10004050 if (phba->sli_rev == 3 &&
4051 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
4052 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05004053 /*
4054 * Finish initializing those IOCB fields that are independent
4055 * of the scsi_cmnd request_buffer
4056 */
4057 piocbq->iocb.ulpContext = pnode->nlp_rpi;
James Smart027140e2012-08-03 12:35:44 -04004058 if (sli4)
James Smart6d368e52011-05-24 11:44:12 -04004059 piocbq->iocb.ulpContext =
4060 phba->sli4_hba.rpi_ids[pnode->nlp_rpi];
dea31012005-04-17 16:05:31 -05004061 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
4062 piocbq->iocb.ulpFCP2Rcvy = 1;
James Smart09372822008-01-11 01:52:54 -05004063 else
4064 piocbq->iocb.ulpFCP2Rcvy = 0;
dea31012005-04-17 16:05:31 -05004065
4066 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
4067 piocbq->context1 = lpfc_cmd;
4068 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
4069 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
James Smart2e0fef82007-06-17 19:56:36 -05004070 piocbq->vport = vport;
dea31012005-04-17 16:05:31 -05004071}
4072
James Smart9bad7672008-12-04 22:39:02 -05004073/**
James Smart6d368e52011-05-24 11:44:12 -04004074 * lpfc_scsi_prep_task_mgmt_cmd - Convert SLI3 scsi TM cmd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05004075 * @vport: The virtual port for which this call is being executed.
4076 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
4077 * @lun: Logical unit number.
4078 * @task_mgmt_cmd: SCSI task management command.
4079 *
James Smart3772a992009-05-22 14:50:54 -04004080 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
4081 * for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05004082 *
4083 * Return codes:
4084 * 0 - Error
4085 * 1 - Success
4086 **/
dea31012005-04-17 16:05:31 -05004087static int
James Smartf1126682009-06-10 17:22:44 -04004088lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
dea31012005-04-17 16:05:31 -05004089 struct lpfc_scsi_buf *lpfc_cmd,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004090 uint64_t lun,
dea31012005-04-17 16:05:31 -05004091 uint8_t task_mgmt_cmd)
4092{
dea31012005-04-17 16:05:31 -05004093 struct lpfc_iocbq *piocbq;
4094 IOCB_t *piocb;
4095 struct fcp_cmnd *fcp_cmnd;
James Smart0b18ac42006-05-01 21:50:40 -04004096 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea31012005-04-17 16:05:31 -05004097 struct lpfc_nodelist *ndlp = rdata->pnode;
4098
James Smart58da1ff2008-04-07 10:15:56 -04004099 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
4100 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05004101 return 0;
dea31012005-04-17 16:05:31 -05004102
dea31012005-04-17 16:05:31 -05004103 piocbq = &(lpfc_cmd->cur_iocbq);
James Smart2e0fef82007-06-17 19:56:36 -05004104 piocbq->vport = vport;
4105
dea31012005-04-17 16:05:31 -05004106 piocb = &piocbq->iocb;
4107
4108 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart34b02dc2008-08-24 21:49:55 -04004109 /* Clear out any old data in the FCP command area */
4110 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
4111 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05004112 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
James Smarte2a0a9d2008-12-04 22:40:02 -05004113 if (vport->phba->sli_rev == 3 &&
4114 !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04004115 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05004116 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
dea31012005-04-17 16:05:31 -05004117 piocb->ulpContext = ndlp->nlp_rpi;
James Smart6d368e52011-05-24 11:44:12 -04004118 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
4119 piocb->ulpContext =
4120 vport->phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
4121 }
James Smart53151bb2013-10-10 12:24:07 -04004122 piocb->ulpFCP2Rcvy = (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) ? 1 : 0;
dea31012005-04-17 16:05:31 -05004123 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
James Smartf9226c22014-02-20 09:57:28 -05004124 piocb->ulpPU = 0;
4125 piocb->un.fcpi.fcpi_parm = 0;
dea31012005-04-17 16:05:31 -05004126
4127 /* ulpTimeout is only one byte */
4128 if (lpfc_cmd->timeout > 0xff) {
4129 /*
4130 * Do not timeout the command at the firmware level.
4131 * The driver will provide the timeout mechanism.
4132 */
4133 piocb->ulpTimeout = 0;
James Smartf1126682009-06-10 17:22:44 -04004134 } else
dea31012005-04-17 16:05:31 -05004135 piocb->ulpTimeout = lpfc_cmd->timeout;
James Smartf1126682009-06-10 17:22:44 -04004136
4137 if (vport->phba->sli_rev == LPFC_SLI_REV4)
4138 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004139
James Smart2e0fef82007-06-17 19:56:36 -05004140 return 1;
dea31012005-04-17 16:05:31 -05004141}
4142
James Smart9bad7672008-12-04 22:39:02 -05004143/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004144 * lpfc_scsi_api_table_setup - Set up scsi api function jump table
James Smart3772a992009-05-22 14:50:54 -04004145 * @phba: The hba struct for which this call is being executed.
4146 * @dev_grp: The HBA PCI-Device group number.
4147 *
4148 * This routine sets up the SCSI interface API function jump table in @phba
4149 * struct.
4150 * Returns: 0 - success, -ENODEV - failure.
4151 **/
4152int
4153lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4154{
4155
James Smartf1126682009-06-10 17:22:44 -04004156 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
4157 phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
James Smartf1126682009-06-10 17:22:44 -04004158
James Smart3772a992009-05-22 14:50:54 -04004159 switch (dev_grp) {
4160 case LPFC_PCI_DEV_LP:
James Smart3772a992009-05-22 14:50:54 -04004161 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
James Smartacd68592012-01-18 16:25:09 -05004162 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04004163 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
James Smart19ca7602010-11-20 23:11:55 -05004164 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04004165 break;
James Smartda0436e2009-05-22 14:51:39 -04004166 case LPFC_PCI_DEV_OC:
James Smartda0436e2009-05-22 14:51:39 -04004167 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
James Smartacd68592012-01-18 16:25:09 -05004168 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04004169 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
James Smart19ca7602010-11-20 23:11:55 -05004170 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04004171 break;
James Smart3772a992009-05-22 14:50:54 -04004172 default:
4173 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4174 "1418 Invalid HBA PCI-device group: 0x%x\n",
4175 dev_grp);
4176 return -ENODEV;
4177 break;
4178 }
James Smart3772a992009-05-22 14:50:54 -04004179 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
James Smart84d1b002010-02-12 14:42:33 -05004180 phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
James Smart3772a992009-05-22 14:50:54 -04004181 return 0;
4182}
4183
4184/**
James Smart3621a712009-04-06 18:47:14 -04004185 * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
James Smart9bad7672008-12-04 22:39:02 -05004186 * @phba: The Hba for which this call is being executed.
4187 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
4188 * @rspiocbq: Pointer to lpfc_iocbq data structure.
4189 *
4190 * This routine is IOCB completion routine for device reset and target reset
4191 * routine. This routine release scsi buffer associated with lpfc_cmd.
4192 **/
James Smart7054a602007-04-25 09:52:34 -04004193static void
4194lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
4195 struct lpfc_iocbq *cmdiocbq,
4196 struct lpfc_iocbq *rspiocbq)
4197{
4198 struct lpfc_scsi_buf *lpfc_cmd =
4199 (struct lpfc_scsi_buf *) cmdiocbq->context1;
4200 if (lpfc_cmd)
4201 lpfc_release_scsi_buf(phba, lpfc_cmd);
4202 return;
4203}
4204
James Smart9bad7672008-12-04 22:39:02 -05004205/**
James Smart50212672018-12-13 15:17:57 -08004206 * lpfc_check_pci_resettable - Walks list of devices on pci_dev's bus to check
4207 * if issuing a pci_bus_reset is possibly unsafe
4208 * @phba: lpfc_hba pointer.
4209 *
4210 * Description:
4211 * Walks the bus_list to ensure only PCI devices with Emulex
4212 * vendor id, device ids that support hot reset, and only one occurrence
4213 * of function 0.
4214 *
4215 * Returns:
4216 * -EBADSLT, detected invalid device
4217 * 0, successful
4218 */
4219int
4220lpfc_check_pci_resettable(const struct lpfc_hba *phba)
4221{
4222 const struct pci_dev *pdev = phba->pcidev;
4223 struct pci_dev *ptr = NULL;
4224 u8 counter = 0;
4225
4226 /* Walk the list of devices on the pci_dev's bus */
4227 list_for_each_entry(ptr, &pdev->bus->devices, bus_list) {
4228 /* Check for Emulex Vendor ID */
4229 if (ptr->vendor != PCI_VENDOR_ID_EMULEX) {
4230 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4231 "8346 Non-Emulex vendor found: "
4232 "0x%04x\n", ptr->vendor);
4233 return -EBADSLT;
4234 }
4235
4236 /* Check for valid Emulex Device ID */
4237 switch (ptr->device) {
4238 case PCI_DEVICE_ID_LANCER_FC:
4239 case PCI_DEVICE_ID_LANCER_G6_FC:
4240 case PCI_DEVICE_ID_LANCER_G7_FC:
4241 break;
4242 default:
4243 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4244 "8347 Invalid device found: "
4245 "0x%04x\n", ptr->device);
4246 return -EBADSLT;
4247 }
4248
4249 /* Check for only one function 0 ID to ensure only one HBA on
4250 * secondary bus
4251 */
4252 if (ptr->devfn == 0) {
4253 if (++counter > 1) {
4254 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4255 "8348 More than one device on "
4256 "secondary bus found\n");
4257 return -EBADSLT;
4258 }
4259 }
4260 }
4261
4262 return 0;
4263}
4264
4265/**
James Smart3621a712009-04-06 18:47:14 -04004266 * lpfc_info - Info entry point of scsi_host_template data structure
James Smart9bad7672008-12-04 22:39:02 -05004267 * @host: The scsi host for which this call is being executed.
4268 *
4269 * This routine provides module information about hba.
4270 *
4271 * Reutrn code:
4272 * Pointer to char - Success.
4273 **/
dea31012005-04-17 16:05:31 -05004274const char *
4275lpfc_info(struct Scsi_Host *host)
4276{
James Smart2e0fef82007-06-17 19:56:36 -05004277 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
4278 struct lpfc_hba *phba = vport->phba;
James Smart50212672018-12-13 15:17:57 -08004279 int link_speed = 0;
4280 static char lpfcinfobuf[384];
4281 char tmp[384] = {0};
dea31012005-04-17 16:05:31 -05004282
James Smart50212672018-12-13 15:17:57 -08004283 memset(lpfcinfobuf, 0, sizeof(lpfcinfobuf));
dea31012005-04-17 16:05:31 -05004284 if (phba && phba->pcidev){
James Smart50212672018-12-13 15:17:57 -08004285 /* Model Description */
4286 scnprintf(tmp, sizeof(tmp), phba->ModelDesc);
4287 if (strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf)) >=
4288 sizeof(lpfcinfobuf))
4289 goto buffer_done;
4290
4291 /* PCI Info */
4292 scnprintf(tmp, sizeof(tmp),
4293 " on PCI bus %02x device %02x irq %d",
4294 phba->pcidev->bus->number, phba->pcidev->devfn,
4295 phba->pcidev->irq);
4296 if (strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf)) >=
4297 sizeof(lpfcinfobuf))
4298 goto buffer_done;
4299
4300 /* Port Number */
dea31012005-04-17 16:05:31 -05004301 if (phba->Port[0]) {
James Smart50212672018-12-13 15:17:57 -08004302 scnprintf(tmp, sizeof(tmp), " port %s", phba->Port);
4303 if (strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf)) >=
4304 sizeof(lpfcinfobuf))
4305 goto buffer_done;
dea31012005-04-17 16:05:31 -05004306 }
James Smart50212672018-12-13 15:17:57 -08004307
4308 /* Link Speed */
James Smarta085e872015-12-16 18:12:02 -05004309 link_speed = lpfc_sli_port_speed_get(phba);
James Smart50212672018-12-13 15:17:57 -08004310 if (link_speed != 0) {
4311 scnprintf(tmp, sizeof(tmp),
4312 " Logical Link Speed: %d Mbps", link_speed);
4313 if (strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf)) >=
4314 sizeof(lpfcinfobuf))
4315 goto buffer_done;
4316 }
4317
4318 /* PCI resettable */
4319 if (!lpfc_check_pci_resettable(phba)) {
4320 scnprintf(tmp, sizeof(tmp), " PCI resettable");
4321 strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf));
4322 }
dea31012005-04-17 16:05:31 -05004323 }
James Smart50212672018-12-13 15:17:57 -08004324
4325buffer_done:
dea31012005-04-17 16:05:31 -05004326 return lpfcinfobuf;
4327}
4328
James Smart9bad7672008-12-04 22:39:02 -05004329/**
James Smart3621a712009-04-06 18:47:14 -04004330 * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
James Smart9bad7672008-12-04 22:39:02 -05004331 * @phba: The Hba for which this call is being executed.
4332 *
4333 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
4334 * The default value of cfg_poll_tmo is 10 milliseconds.
4335 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004336static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
4337{
4338 unsigned long poll_tmo_expires =
4339 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
4340
James Smart895427b2017-02-12 13:52:30 -08004341 if (!list_empty(&phba->sli.sli3_ring[LPFC_FCP_RING].txcmplq))
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004342 mod_timer(&phba->fcp_poll_timer,
4343 poll_tmo_expires);
4344}
4345
James Smart9bad7672008-12-04 22:39:02 -05004346/**
James Smart3621a712009-04-06 18:47:14 -04004347 * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
James Smart9bad7672008-12-04 22:39:02 -05004348 * @phba: The Hba for which this call is being executed.
4349 *
4350 * This routine starts the fcp_poll_timer of @phba.
4351 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004352void lpfc_poll_start_timer(struct lpfc_hba * phba)
4353{
4354 lpfc_poll_rearm_timer(phba);
4355}
4356
James Smart9bad7672008-12-04 22:39:02 -05004357/**
James Smart3621a712009-04-06 18:47:14 -04004358 * lpfc_poll_timeout - Restart polling timer
James Smart9bad7672008-12-04 22:39:02 -05004359 * @ptr: Map to lpfc_hba data structure pointer.
4360 *
4361 * This routine restarts fcp_poll timer, when FCP ring polling is enable
4362 * and FCP Ring interrupt is disable.
4363 **/
4364
Kees Cookf22eb4d2017-09-06 20:24:26 -07004365void lpfc_poll_timeout(struct timer_list *t)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004366{
Kees Cookf22eb4d2017-09-06 20:24:26 -07004367 struct lpfc_hba *phba = from_timer(phba, t, fcp_poll_timer);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004368
4369 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004370 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -08004371 &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
James Smart45ed1192009-10-02 15:17:02 -04004372
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004373 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4374 lpfc_poll_rearm_timer(phba);
4375 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004376}
4377
James Smart9bad7672008-12-04 22:39:02 -05004378/**
James Smart3621a712009-04-06 18:47:14 -04004379 * lpfc_queuecommand - scsi_host_template queuecommand entry point
James Smart9bad7672008-12-04 22:39:02 -05004380 * @cmnd: Pointer to scsi_cmnd data structure.
4381 * @done: Pointer to done routine.
4382 *
4383 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
4384 * This routine prepares an IOCB from scsi command and provides to firmware.
4385 * The @done callback is invoked after driver finished processing the command.
4386 *
4387 * Return value :
4388 * 0 - Success
4389 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
4390 **/
dea31012005-04-17 16:05:31 -05004391static int
James Smartb9a7c632012-08-03 12:35:24 -04004392lpfc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004393{
James Smart2e0fef82007-06-17 19:56:36 -05004394 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4395 struct lpfc_hba *phba = vport->phba;
James Smart1ba981f2014-02-20 09:56:45 -05004396 struct lpfc_rport_data *rdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05004397 struct lpfc_nodelist *ndlp;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004398 struct lpfc_scsi_buf *lpfc_cmd;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004399 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004400 int err;
dea31012005-04-17 16:05:31 -05004401
James Smart1ba981f2014-02-20 09:56:45 -05004402 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smartb0e83012018-06-26 08:24:29 -07004403
4404 /* sanity check on references */
4405 if (unlikely(!rdata) || unlikely(!rport))
4406 goto out_fail_command;
4407
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004408 err = fc_remote_port_chkready(rport);
4409 if (err) {
4410 cmnd->result = err;
dea31012005-04-17 16:05:31 -05004411 goto out_fail_command;
4412 }
James Smart1c6f4ef52009-11-18 15:40:49 -05004413 ndlp = rdata->pnode;
dea31012005-04-17 16:05:31 -05004414
James Smartbf086112011-08-21 21:48:13 -04004415 if ((scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) &&
James Smartacd68592012-01-18 16:25:09 -05004416 (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED))) {
James Smarte2a0a9d2008-12-04 22:40:02 -05004417
James Smart6a9c52c2009-10-02 15:16:51 -04004418 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4419 "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
4420 " op:%02x str=%s without registering for"
4421 " BlockGuard - Rejecting command\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05004422 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
4423 dif_op_str[scsi_get_prot_op(cmnd)]);
4424 goto out_fail_command;
4425 }
4426
dea31012005-04-17 16:05:31 -05004427 /*
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004428 * Catch race where our node has transitioned, but the
4429 * transport is still transitioning.
dea31012005-04-17 16:05:31 -05004430 */
James Smart6b415f52012-06-12 13:54:59 -04004431 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
4432 goto out_tgt_busy;
James Smart2a5b7d62018-07-31 17:23:22 -07004433 if (lpfc_ndlp_check_qdepth(phba, ndlp)) {
4434 if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth) {
4435 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_ERROR,
4436 "3377 Target Queue Full, scsi Id:%d "
4437 "Qdepth:%d Pending command:%d"
4438 " WWNN:%02x:%02x:%02x:%02x:"
4439 "%02x:%02x:%02x:%02x, "
4440 " WWPN:%02x:%02x:%02x:%02x:"
4441 "%02x:%02x:%02x:%02x",
4442 ndlp->nlp_sid, ndlp->cmd_qdepth,
4443 atomic_read(&ndlp->cmd_pending),
4444 ndlp->nlp_nodename.u.wwn[0],
4445 ndlp->nlp_nodename.u.wwn[1],
4446 ndlp->nlp_nodename.u.wwn[2],
4447 ndlp->nlp_nodename.u.wwn[3],
4448 ndlp->nlp_nodename.u.wwn[4],
4449 ndlp->nlp_nodename.u.wwn[5],
4450 ndlp->nlp_nodename.u.wwn[6],
4451 ndlp->nlp_nodename.u.wwn[7],
4452 ndlp->nlp_portname.u.wwn[0],
4453 ndlp->nlp_portname.u.wwn[1],
4454 ndlp->nlp_portname.u.wwn[2],
4455 ndlp->nlp_portname.u.wwn[3],
4456 ndlp->nlp_portname.u.wwn[4],
4457 ndlp->nlp_portname.u.wwn[5],
4458 ndlp->nlp_portname.u.wwn[6],
4459 ndlp->nlp_portname.u.wwn[7]);
4460 goto out_tgt_busy;
4461 }
James Smart64bf0092018-01-30 15:58:53 -08004462 }
James Smartf91bc592018-04-09 14:24:22 -07004463
James Smart19ca7602010-11-20 23:11:55 -05004464 lpfc_cmd = lpfc_get_scsi_buf(phba, ndlp);
dea31012005-04-17 16:05:31 -05004465 if (lpfc_cmd == NULL) {
James Smarteaf15d52008-12-04 22:39:29 -05004466 lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05004467
James Smart895427b2017-02-12 13:52:30 -08004468 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_ERROR,
James Smarte8b62012007-08-02 11:10:09 -04004469 "0707 driver's buffer pool is empty, "
4470 "IO busied\n");
dea31012005-04-17 16:05:31 -05004471 goto out_host_busy;
4472 }
4473
4474 /*
4475 * Store the midlayer's command structure for the completion phase
4476 * and complete the command initialization.
4477 */
4478 lpfc_cmd->pCmd = cmnd;
4479 lpfc_cmd->rdata = rdata;
James Smart2a5b7d62018-07-31 17:23:22 -07004480 lpfc_cmd->ndlp = ndlp;
dea31012005-04-17 16:05:31 -05004481 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
dea31012005-04-17 16:05:31 -05004482
James Smarte2a0a9d2008-12-04 22:40:02 -05004483 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
James Smart6a9c52c2009-10-02 15:16:51 -04004484 if (vport->phba->cfg_enable_bg) {
James Smart737d4242013-04-17 20:14:49 -04004485 lpfc_printf_vlog(vport,
4486 KERN_INFO, LOG_SCSI_CMD,
James Smart26134702012-08-14 14:25:50 -04004487 "9033 BLKGRD: rcvd %s cmd:x%x "
4488 "sector x%llx cnt %u pt %x\n",
4489 dif_op_str[scsi_get_prot_op(cmnd)],
4490 cmnd->cmnd[0],
4491 (unsigned long long)scsi_get_lba(cmnd),
4492 blk_rq_sectors(cmnd->request),
4493 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004494 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004495 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
4496 } else {
James Smart6a9c52c2009-10-02 15:16:51 -04004497 if (vport->phba->cfg_enable_bg) {
James Smart737d4242013-04-17 20:14:49 -04004498 lpfc_printf_vlog(vport,
4499 KERN_INFO, LOG_SCSI_CMD,
James Smart26134702012-08-14 14:25:50 -04004500 "9038 BLKGRD: rcvd PROT_NORMAL cmd: "
4501 "x%x sector x%llx cnt %u pt %x\n",
4502 cmnd->cmnd[0],
4503 (unsigned long long)scsi_get_lba(cmnd),
James Smart9a6b09c2012-03-01 22:37:42 -05004504 blk_rq_sectors(cmnd->request),
James Smart26134702012-08-14 14:25:50 -04004505 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004506 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004507 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
4508 }
4509
dea31012005-04-17 16:05:31 -05004510 if (err)
4511 goto out_host_busy_free_buf;
4512
James Smart2e0fef82007-06-17 19:56:36 -05004513 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea31012005-04-17 16:05:31 -05004514
James Smart3772a992009-05-22 14:50:54 -04004515 err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
James Smart92d7f7b2007-06-17 19:56:38 -05004516 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
James Smarteaf15d52008-12-04 22:39:29 -05004517 if (err) {
James Smart76f96b62013-12-17 20:29:01 -05004518 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4519 "3376 FCP could not issue IOCB err %x"
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004520 "FCP cmd x%x <%d/%llu> "
James Smart76f96b62013-12-17 20:29:01 -05004521 "sid: x%x did: x%x oxid: x%x "
4522 "Data: x%x x%x x%x x%x\n",
4523 err, cmnd->cmnd[0],
4524 cmnd->device ? cmnd->device->id : 0xffff,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004525 cmnd->device ? cmnd->device->lun : (u64) -1,
James Smart76f96b62013-12-17 20:29:01 -05004526 vport->fc_myDID, ndlp->nlp_DID,
4527 phba->sli_rev == LPFC_SLI_REV4 ?
4528 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
4529 lpfc_cmd->cur_iocbq.iocb.ulpContext,
4530 lpfc_cmd->cur_iocbq.iocb.ulpIoTag,
4531 lpfc_cmd->cur_iocbq.iocb.ulpTimeout,
4532 (uint32_t)
4533 (cmnd->request->timeout / 1000));
4534
James Smart2cee7802017-06-01 21:07:02 -07004535 switch (lpfc_cmd->fcp_cmnd->fcpCntl3) {
4536 case WRITE_DATA:
4537 atomic_dec(&phba->fc4ScsiOutputRequests);
4538 break;
4539 case READ_DATA:
4540 atomic_dec(&phba->fc4ScsiInputRequests);
4541 break;
4542 default:
4543 atomic_dec(&phba->fc4ScsiControlRequests);
4544 }
dea31012005-04-17 16:05:31 -05004545 goto out_host_busy_free_buf;
James Smarteaf15d52008-12-04 22:39:29 -05004546 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004547 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004548 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -08004549 &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
James Smart45ed1192009-10-02 15:17:02 -04004550
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004551 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4552 lpfc_poll_rearm_timer(phba);
4553 }
4554
dea31012005-04-17 16:05:31 -05004555 return 0;
4556
4557 out_host_busy_free_buf:
James Smartbcf4dbf2006-07-06 15:50:08 -04004558 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004559 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004560 out_host_busy:
4561 return SCSI_MLQUEUE_HOST_BUSY;
4562
Mike Christie34963432011-02-25 14:04:28 -06004563 out_tgt_busy:
4564 return SCSI_MLQUEUE_TARGET_BUSY;
4565
dea31012005-04-17 16:05:31 -05004566 out_fail_command:
James Smartb9a7c632012-08-03 12:35:24 -04004567 cmnd->scsi_done(cmnd);
dea31012005-04-17 16:05:31 -05004568 return 0;
4569}
4570
Jeff Garzikf2812332010-11-16 02:10:29 -05004571
James Smart9bad7672008-12-04 22:39:02 -05004572/**
James Smart3621a712009-04-06 18:47:14 -04004573 * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05004574 * @cmnd: Pointer to scsi_cmnd data structure.
4575 *
4576 * This routine aborts @cmnd pending in base driver.
4577 *
4578 * Return code :
4579 * 0x2003 - Error
4580 * 0x2002 - Success
4581 **/
dea31012005-04-17 16:05:31 -05004582static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05004583lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004584{
James Smart2e0fef82007-06-17 19:56:36 -05004585 struct Scsi_Host *shost = cmnd->device->host;
4586 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4587 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004588 struct lpfc_iocbq *iocb;
4589 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05004590 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05004591 IOCB_t *cmd, *icmd;
James Smart3a707302012-06-12 13:54:42 -04004592 int ret = SUCCESS, status = 0;
James Smart8931c732018-07-31 17:23:20 -07004593 struct lpfc_sli_ring *pring_s4 = NULL;
James Smart895427b2017-02-12 13:52:30 -08004594 int ret_val;
James Smart59c68ea2018-04-09 14:24:25 -07004595 unsigned long flags;
James Smartfa61a542008-01-11 01:52:42 -05004596 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004597
James Smart3a707302012-06-12 13:54:42 -04004598 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04004599 if (status != 0 && status != SUCCESS)
James Smart3a707302012-06-12 13:54:42 -04004600 return status;
James Smart4f2e66c2012-05-09 21:17:07 -04004601
James Smart876dd7d2012-09-29 11:31:28 -04004602 spin_lock_irqsave(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004603 /* driver queued commands are in process of being flushed */
4604 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
James Smart876dd7d2012-09-29 11:31:28 -04004605 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004606 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4607 "3168 SCSI Layer abort requested I/O has been "
4608 "flushed by LLD.\n");
4609 return FAILED;
4610 }
4611
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004612 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
James Smart92e3af62012-08-14 14:26:28 -04004613 if (!lpfc_cmd || !lpfc_cmd->pCmd) {
James Smart876dd7d2012-09-29 11:31:28 -04004614 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarteee88772010-09-29 11:19:08 -04004615 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4616 "2873 SCSI Layer I/O Abort Request IO CMPL Status "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004617 "x%x ID %d LUN %llu\n",
James Smart3a707302012-06-12 13:54:42 -04004618 SUCCESS, cmnd->device->id, cmnd->device->lun);
James Smarteee88772010-09-29 11:19:08 -04004619 return SUCCESS;
4620 }
dea31012005-04-17 16:05:31 -05004621
James Smart4f2e66c2012-05-09 21:17:07 -04004622 iocb = &lpfc_cmd->cur_iocbq;
James Smart8931c732018-07-31 17:23:20 -07004623 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartcdb42be2019-01-28 11:14:21 -08004624 pring_s4 = phba->sli4_hba.hdwq[iocb->hba_wqidx].fcp_wq->pring;
James Smart8931c732018-07-31 17:23:20 -07004625 if (!pring_s4) {
4626 ret = FAILED;
4627 goto out_unlock;
4628 }
4629 spin_lock(&pring_s4->ring_lock);
4630 }
James Smart4f2e66c2012-05-09 21:17:07 -04004631 /* the command is in process of being cancelled */
4632 if (!(iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
James Smart8931c732018-07-31 17:23:20 -07004633 if (phba->sli_rev == LPFC_SLI_REV4)
4634 spin_unlock(&pring_s4->ring_lock);
James Smart876dd7d2012-09-29 11:31:28 -04004635 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004636 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4637 "3169 SCSI Layer abort requested I/O has been "
4638 "cancelled by LLD.\n");
4639 return FAILED;
4640 }
dea31012005-04-17 16:05:31 -05004641 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004642 * If pCmd field of the corresponding lpfc_scsi_buf structure
4643 * points to a different SCSI command, then the driver has
4644 * already completed this command, but the midlayer did not
James Smart4f2e66c2012-05-09 21:17:07 -04004645 * see the completion before the eh fired. Just return SUCCESS.
dea31012005-04-17 16:05:31 -05004646 */
James Smart4f2e66c2012-05-09 21:17:07 -04004647 if (lpfc_cmd->pCmd != cmnd) {
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 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4651 "3170 SCSI Layer abort requested I/O has been "
4652 "completed by LLD.\n");
4653 goto out_unlock;
4654 }
dea31012005-04-17 16:05:31 -05004655
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004656 BUG_ON(iocb->context1 != lpfc_cmd);
dea31012005-04-17 16:05:31 -05004657
James Smartee620212014-04-04 13:51:53 -04004658 /* abort issued in recovery is still in progress */
4659 if (iocb->iocb_flag & LPFC_DRIVER_ABORTED) {
4660 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4661 "3389 SCSI Layer I/O Abort Request is pending\n");
James Smart8931c732018-07-31 17:23:20 -07004662 if (phba->sli_rev == LPFC_SLI_REV4)
4663 spin_unlock(&pring_s4->ring_lock);
James Smartee620212014-04-04 13:51:53 -04004664 spin_unlock_irqrestore(&phba->hbalock, flags);
4665 goto wait_for_cmpl;
4666 }
4667
James Smart4f2e66c2012-05-09 21:17:07 -04004668 abtsiocb = __lpfc_sli_get_iocbq(phba);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004669 if (abtsiocb == NULL) {
4670 ret = FAILED;
James Smart8931c732018-07-31 17:23:20 -07004671 if (phba->sli_rev == LPFC_SLI_REV4)
4672 spin_unlock(&pring_s4->ring_lock);
James Smart4f2e66c2012-05-09 21:17:07 -04004673 goto out_unlock;
dea31012005-04-17 16:05:31 -05004674 }
4675
James Smartafbd8d82013-09-06 12:22:13 -04004676 /* Indicate the IO is being aborted by the driver. */
4677 iocb->iocb_flag |= LPFC_DRIVER_ABORTED;
4678
dea31012005-04-17 16:05:31 -05004679 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004680 * The scsi command can not be in txq and it is in flight because the
4681 * pCmd is still pointig at the SCSI command we have to abort. There
4682 * is no need to search the txcmplq. Just send an abort to the FW.
dea31012005-04-17 16:05:31 -05004683 */
dea31012005-04-17 16:05:31 -05004684
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004685 cmd = &iocb->iocb;
4686 icmd = &abtsiocb->iocb;
4687 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
4688 icmd->un.acxri.abortContextTag = cmd->ulpContext;
James Smart3772a992009-05-22 14:50:54 -04004689 if (phba->sli_rev == LPFC_SLI_REV4)
4690 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
4691 else
4692 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05004693
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004694 icmd->ulpLe = 1;
4695 icmd->ulpClass = cmd->ulpClass;
James Smart5ffc2662009-11-18 15:39:44 -05004696
4697 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -08004698 abtsiocb->hba_wqidx = iocb->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -05004699 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -04004700 if (iocb->iocb_flag & LPFC_IO_FOF)
4701 abtsiocb->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -05004702
James Smart2e0fef82007-06-17 19:56:36 -05004703 if (lpfc_is_link_up(phba))
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004704 icmd->ulpCommand = CMD_ABORT_XRI_CN;
4705 else
4706 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea31012005-04-17 16:05:31 -05004707
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004708 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smart2e0fef82007-06-17 19:56:36 -05004709 abtsiocb->vport = vport;
James Smart8931c732018-07-31 17:23:20 -07004710 lpfc_cmd->waitq = &waitq;
James Smart98912dda2014-04-04 13:52:31 -04004711 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart98912dda2014-04-04 13:52:31 -04004712 /* Note: both hbalock and ring_lock must be set here */
James Smart98912dda2014-04-04 13:52:31 -04004713 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
4714 abtsiocb, 0);
James Smart59c68ea2018-04-09 14:24:25 -07004715 spin_unlock(&pring_s4->ring_lock);
James Smart98912dda2014-04-04 13:52:31 -04004716 } else {
4717 ret_val = __lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
4718 abtsiocb, 0);
4719 }
James Smart4f2e66c2012-05-09 21:17:07 -04004720 /* no longer need the lock after this point */
James Smart876dd7d2012-09-29 11:31:28 -04004721 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004722
James Smart98912dda2014-04-04 13:52:31 -04004723
4724 if (ret_val == IOCB_ERROR) {
James Smart8931c732018-07-31 17:23:20 -07004725 if (phba->sli_rev == LPFC_SLI_REV4)
4726 spin_lock_irqsave(&pring_s4->ring_lock, flags);
4727 else
4728 spin_lock_irqsave(&phba->hbalock, flags);
4729 /* Indicate the IO is not being aborted by the driver. */
4730 iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
4731 lpfc_cmd->waitq = NULL;
4732 if (phba->sli_rev == LPFC_SLI_REV4)
4733 spin_unlock_irqrestore(&pring_s4->ring_lock, flags);
4734 else
4735 spin_unlock_irqrestore(&phba->hbalock, flags);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004736 lpfc_sli_release_iocbq(phba, abtsiocb);
4737 ret = FAILED;
4738 goto out;
4739 }
4740
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004741 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
James Smart45ed1192009-10-02 15:17:02 -04004742 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -08004743 &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004744
James Smartee620212014-04-04 13:51:53 -04004745wait_for_cmpl:
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004746 /* Wait for abort to complete */
James Smartfa61a542008-01-11 01:52:42 -05004747 wait_event_timeout(waitq,
4748 (lpfc_cmd->pCmd != cmnd),
James Smart256ec0d2013-04-17 20:14:58 -04004749 msecs_to_jiffies(2*vport->cfg_devloss_tmo*1000));
James Smartee620212014-04-04 13:51:53 -04004750
4751 spin_lock_irqsave(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05004752 lpfc_cmd->waitq = NULL;
James Smartee620212014-04-04 13:51:53 -04004753 spin_unlock_irqrestore(shost->host_lock, flags);
dea31012005-04-17 16:05:31 -05004754
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004755 if (lpfc_cmd->pCmd == cmnd) {
4756 ret = FAILED;
James Smarte8b62012007-08-02 11:10:09 -04004757 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4758 "0748 abort handler timed out waiting "
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -07004759 "for aborting I/O (xri:x%x) to complete: "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004760 "ret %#x, ID %d, LUN %llu\n",
James Smart247ca942012-08-14 14:26:13 -04004761 iocb->sli4_xritag, ret,
4762 cmnd->device->id, cmnd->device->lun);
dea31012005-04-17 16:05:31 -05004763 }
James Smart4f2e66c2012-05-09 21:17:07 -04004764 goto out;
dea31012005-04-17 16:05:31 -05004765
James Smart4f2e66c2012-05-09 21:17:07 -04004766out_unlock:
James Smart876dd7d2012-09-29 11:31:28 -04004767 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004768out:
James Smarte8b62012007-08-02 11:10:09 -04004769 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4770 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004771 "LUN %llu\n", ret, cmnd->device->id,
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04004772 cmnd->device->lun);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004773 return ret;
dea31012005-04-17 16:05:31 -05004774}
4775
James Smartbbb9d182009-06-10 17:23:16 -04004776static char *
4777lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
4778{
4779 switch (task_mgmt_cmd) {
4780 case FCP_ABORT_TASK_SET:
4781 return "ABORT_TASK_SET";
4782 case FCP_CLEAR_TASK_SET:
4783 return "FCP_CLEAR_TASK_SET";
4784 case FCP_BUS_RESET:
4785 return "FCP_BUS_RESET";
4786 case FCP_LUN_RESET:
4787 return "FCP_LUN_RESET";
4788 case FCP_TARGET_RESET:
4789 return "FCP_TARGET_RESET";
4790 case FCP_CLEAR_ACA:
4791 return "FCP_CLEAR_ACA";
4792 case FCP_TERMINATE_TASK:
4793 return "FCP_TERMINATE_TASK";
4794 default:
4795 return "unknown";
4796 }
4797}
4798
James Smart53151bb2013-10-10 12:24:07 -04004799
4800/**
4801 * lpfc_check_fcp_rsp - check the returned fcp_rsp to see if task failed
4802 * @vport: The virtual port for which this call is being executed.
4803 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
4804 *
4805 * This routine checks the FCP RSP INFO to see if the tsk mgmt command succeded
4806 *
4807 * Return code :
4808 * 0x2003 - Error
4809 * 0x2002 - Success
4810 **/
4811static int
4812lpfc_check_fcp_rsp(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd)
4813{
4814 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
4815 uint32_t rsp_info;
4816 uint32_t rsp_len;
4817 uint8_t rsp_info_code;
4818 int ret = FAILED;
4819
4820
4821 if (fcprsp == NULL)
4822 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4823 "0703 fcp_rsp is missing\n");
4824 else {
4825 rsp_info = fcprsp->rspStatus2;
4826 rsp_len = be32_to_cpu(fcprsp->rspRspLen);
4827 rsp_info_code = fcprsp->rspInfo3;
4828
4829
4830 lpfc_printf_vlog(vport, KERN_INFO,
4831 LOG_FCP,
4832 "0706 fcp_rsp valid 0x%x,"
4833 " rsp len=%d code 0x%x\n",
4834 rsp_info,
4835 rsp_len, rsp_info_code);
4836
4837 if ((fcprsp->rspStatus2&RSP_LEN_VALID) && (rsp_len == 8)) {
4838 switch (rsp_info_code) {
4839 case RSP_NO_FAILURE:
4840 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4841 "0715 Task Mgmt No Failure\n");
4842 ret = SUCCESS;
4843 break;
4844 case RSP_TM_NOT_SUPPORTED: /* TM rejected */
4845 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4846 "0716 Task Mgmt Target "
4847 "reject\n");
4848 break;
4849 case RSP_TM_NOT_COMPLETED: /* TM failed */
4850 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4851 "0717 Task Mgmt Target "
4852 "failed TM\n");
4853 break;
4854 case RSP_TM_INVALID_LU: /* TM to invalid LU! */
4855 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4856 "0718 Task Mgmt to invalid "
4857 "LUN\n");
4858 break;
4859 }
4860 }
4861 }
4862 return ret;
4863}
4864
4865
James Smartbbb9d182009-06-10 17:23:16 -04004866/**
4867 * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
4868 * @vport: The virtual port for which this call is being executed.
4869 * @rdata: Pointer to remote port local data
4870 * @tgt_id: Target ID of remote device.
4871 * @lun_id: Lun number for the TMF
4872 * @task_mgmt_cmd: type of TMF to send
4873 *
4874 * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
4875 * a remote port.
4876 *
4877 * Return Code:
4878 * 0x2003 - Error
4879 * 0x2002 - Success.
4880 **/
4881static int
James Smarteed695d2016-10-13 15:06:04 -07004882lpfc_send_taskmgmt(struct lpfc_vport *vport, struct scsi_cmnd *cmnd,
4883 unsigned int tgt_id, uint64_t lun_id,
4884 uint8_t task_mgmt_cmd)
James Smartbbb9d182009-06-10 17:23:16 -04004885{
4886 struct lpfc_hba *phba = vport->phba;
4887 struct lpfc_scsi_buf *lpfc_cmd;
4888 struct lpfc_iocbq *iocbq;
4889 struct lpfc_iocbq *iocbqrsp;
James Smarteed695d2016-10-13 15:06:04 -07004890 struct lpfc_rport_data *rdata;
4891 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004892 int ret;
4893 int status;
4894
James Smarteed695d2016-10-13 15:06:04 -07004895 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
4896 if (!rdata || !rdata->pnode || !NLP_CHK_NODE_ACT(rdata->pnode))
James Smartbbb9d182009-06-10 17:23:16 -04004897 return FAILED;
James Smarteed695d2016-10-13 15:06:04 -07004898 pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004899
James Smarteed695d2016-10-13 15:06:04 -07004900 lpfc_cmd = lpfc_get_scsi_buf(phba, pnode);
James Smartbbb9d182009-06-10 17:23:16 -04004901 if (lpfc_cmd == NULL)
4902 return FAILED;
James Smart0c411222013-09-06 12:22:46 -04004903 lpfc_cmd->timeout = phba->cfg_task_mgmt_tmo;
James Smartbbb9d182009-06-10 17:23:16 -04004904 lpfc_cmd->rdata = rdata;
James Smarteed695d2016-10-13 15:06:04 -07004905 lpfc_cmd->pCmd = cmnd;
James Smart2a5b7d62018-07-31 17:23:22 -07004906 lpfc_cmd->ndlp = pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004907
4908 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
4909 task_mgmt_cmd);
4910 if (!status) {
4911 lpfc_release_scsi_buf(phba, lpfc_cmd);
4912 return FAILED;
4913 }
4914
4915 iocbq = &lpfc_cmd->cur_iocbq;
4916 iocbqrsp = lpfc_sli_get_iocbq(phba);
4917 if (iocbqrsp == NULL) {
4918 lpfc_release_scsi_buf(phba, lpfc_cmd);
4919 return FAILED;
4920 }
James Smart5a0916b2013-07-15 18:31:42 -04004921 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
James Smartbbb9d182009-06-10 17:23:16 -04004922
4923 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004924 "0702 Issue %s to TGT %d LUN %llu "
James Smart6d368e52011-05-24 11:44:12 -04004925 "rpi x%x nlp_flag x%x Data: x%x x%x\n",
James Smartbbb9d182009-06-10 17:23:16 -04004926 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
James Smart6d368e52011-05-24 11:44:12 -04004927 pnode->nlp_rpi, pnode->nlp_flag, iocbq->sli4_xritag,
4928 iocbq->iocb_flag);
James Smartbbb9d182009-06-10 17:23:16 -04004929
4930 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
4931 iocbq, iocbqrsp, lpfc_cmd->timeout);
James Smart53151bb2013-10-10 12:24:07 -04004932 if ((status != IOCB_SUCCESS) ||
4933 (iocbqrsp->iocb.ulpStatus != IOSTAT_SUCCESS)) {
James Smartae374a32015-05-22 10:42:41 -04004934 if (status != IOCB_SUCCESS ||
4935 iocbqrsp->iocb.ulpStatus != IOSTAT_FCP_RSP_ERROR)
4936 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4937 "0727 TMF %s to TGT %d LUN %llu "
4938 "failed (%d, %d) iocb_flag x%x\n",
4939 lpfc_taskmgmt_name(task_mgmt_cmd),
4940 tgt_id, lun_id,
4941 iocbqrsp->iocb.ulpStatus,
4942 iocbqrsp->iocb.un.ulpWord[4],
4943 iocbq->iocb_flag);
James Smart53151bb2013-10-10 12:24:07 -04004944 /* if ulpStatus != IOCB_SUCCESS, then status == IOCB_SUCCESS */
4945 if (status == IOCB_SUCCESS) {
4946 if (iocbqrsp->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
4947 /* Something in the FCP_RSP was invalid.
4948 * Check conditions */
4949 ret = lpfc_check_fcp_rsp(vport, lpfc_cmd);
4950 else
4951 ret = FAILED;
4952 } else if (status == IOCB_TIMEDOUT) {
4953 ret = TIMEOUT_ERROR;
4954 } else {
4955 ret = FAILED;
4956 }
James Smart53151bb2013-10-10 12:24:07 -04004957 } else
James Smartbbb9d182009-06-10 17:23:16 -04004958 ret = SUCCESS;
4959
4960 lpfc_sli_release_iocbq(phba, iocbqrsp);
4961
4962 if (ret != TIMEOUT_ERROR)
4963 lpfc_release_scsi_buf(phba, lpfc_cmd);
4964
4965 return ret;
4966}
4967
4968/**
4969 * lpfc_chk_tgt_mapped -
4970 * @vport: The virtual port to check on
4971 * @cmnd: Pointer to scsi_cmnd data structure.
4972 *
4973 * This routine delays until the scsi target (aka rport) for the
4974 * command exists (is present and logged in) or we declare it non-existent.
4975 *
4976 * Return code :
4977 * 0x2003 - Error
4978 * 0x2002 - Success
4979 **/
4980static int
4981lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
4982{
James Smart1ba981f2014-02-20 09:56:45 -05004983 struct lpfc_rport_data *rdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05004984 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004985 unsigned long later;
4986
James Smart1ba981f2014-02-20 09:56:45 -05004987 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smart1c6f4ef52009-11-18 15:40:49 -05004988 if (!rdata) {
4989 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4990 "0797 Tgt Map rport failure: rdata x%p\n", rdata);
4991 return FAILED;
4992 }
4993 pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004994 /*
4995 * If target is not in a MAPPED state, delay until
4996 * target is rediscovered or devloss timeout expires.
4997 */
4998 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
4999 while (time_after(later, jiffies)) {
5000 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
5001 return FAILED;
5002 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
5003 return SUCCESS;
5004 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
James Smart1ba981f2014-02-20 09:56:45 -05005005 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smartbbb9d182009-06-10 17:23:16 -04005006 if (!rdata)
5007 return FAILED;
5008 pnode = rdata->pnode;
5009 }
5010 if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
5011 (pnode->nlp_state != NLP_STE_MAPPED_NODE))
5012 return FAILED;
5013 return SUCCESS;
5014}
5015
5016/**
5017 * lpfc_reset_flush_io_context -
5018 * @vport: The virtual port (scsi_host) for the flush context
5019 * @tgt_id: If aborting by Target contect - specifies the target id
5020 * @lun_id: If aborting by Lun context - specifies the lun id
5021 * @context: specifies the context level to flush at.
5022 *
5023 * After a reset condition via TMF, we need to flush orphaned i/o
5024 * contexts from the adapter. This routine aborts any contexts
5025 * outstanding, then waits for their completions. The wait is
5026 * bounded by devloss_tmo though.
5027 *
5028 * Return code :
5029 * 0x2003 - Error
5030 * 0x2002 - Success
5031 **/
5032static int
5033lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
5034 uint64_t lun_id, lpfc_ctx_cmd context)
5035{
5036 struct lpfc_hba *phba = vport->phba;
5037 unsigned long later;
5038 int cnt;
5039
5040 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
5041 if (cnt)
James Smart98912dda2014-04-04 13:52:31 -04005042 lpfc_sli_abort_taskmgmt(vport,
James Smart895427b2017-02-12 13:52:30 -08005043 &phba->sli.sli3_ring[LPFC_FCP_RING],
James Smart98912dda2014-04-04 13:52:31 -04005044 tgt_id, lun_id, context);
James Smartbbb9d182009-06-10 17:23:16 -04005045 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
5046 while (time_after(later, jiffies) && cnt) {
5047 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
5048 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
5049 }
5050 if (cnt) {
5051 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5052 "0724 I/O flush failure for context %s : cnt x%x\n",
5053 ((context == LPFC_CTX_LUN) ? "LUN" :
5054 ((context == LPFC_CTX_TGT) ? "TGT" :
5055 ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
5056 cnt);
5057 return FAILED;
5058 }
5059 return SUCCESS;
5060}
5061
James Smart9bad7672008-12-04 22:39:02 -05005062/**
James Smart3621a712009-04-06 18:47:14 -04005063 * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
James Smart9bad7672008-12-04 22:39:02 -05005064 * @cmnd: Pointer to scsi_cmnd data structure.
5065 *
James Smartbbb9d182009-06-10 17:23:16 -04005066 * This routine does a device reset by sending a LUN_RESET task management
James Smart9bad7672008-12-04 22:39:02 -05005067 * command.
5068 *
5069 * Return code :
5070 * 0x2003 - Error
James Smart3621a712009-04-06 18:47:14 -04005071 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05005072 **/
dea31012005-04-17 16:05:31 -05005073static int
James Smart7054a602007-04-25 09:52:34 -04005074lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05005075{
James Smart2e0fef82007-06-17 19:56:36 -05005076 struct Scsi_Host *shost = cmnd->device->host;
5077 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smart1ba981f2014-02-20 09:56:45 -05005078 struct lpfc_rport_data *rdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005079 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005080 unsigned tgt_id = cmnd->device->id;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005081 uint64_t lun_id = cmnd->device->lun;
James Smartea2151b2008-09-07 11:52:10 -04005082 struct lpfc_scsi_event_header scsi_event;
James Smart53151bb2013-10-10 12:24:07 -04005083 int status;
dea31012005-04-17 16:05:31 -05005084
James Smart1ba981f2014-02-20 09:56:45 -05005085 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smartad490b62015-04-07 15:07:26 -04005086 if (!rdata || !rdata->pnode) {
James Smart1c6f4ef52009-11-18 15:40:49 -05005087 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smartad490b62015-04-07 15:07:26 -04005088 "0798 Device Reset rport failure: rdata x%p\n",
5089 rdata);
James Smart1c6f4ef52009-11-18 15:40:49 -05005090 return FAILED;
5091 }
5092 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04005093 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005094 if (status != 0 && status != SUCCESS)
James Smart589a52d2010-07-14 15:30:54 -04005095 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005096
5097 status = lpfc_chk_tgt_mapped(vport, cmnd);
5098 if (status == FAILED) {
5099 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5100 "0721 Device Reset rport failure: rdata x%p\n", rdata);
5101 return FAILED;
5102 }
5103
5104 scsi_event.event_type = FC_REG_SCSI_EVENT;
5105 scsi_event.subcategory = LPFC_EVENT_LUNRESET;
5106 scsi_event.lun = lun_id;
5107 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5108 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5109
5110 fc_host_post_vendor_event(shost, fc_get_event_number(),
5111 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
5112
James Smarteed695d2016-10-13 15:06:04 -07005113 status = lpfc_send_taskmgmt(vport, cmnd, tgt_id, lun_id,
James Smartbbb9d182009-06-10 17:23:16 -04005114 FCP_LUN_RESET);
5115
5116 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005117 "0713 SCSI layer issued Device Reset (%d, %llu) "
James Smartbbb9d182009-06-10 17:23:16 -04005118 "return x%x\n", tgt_id, lun_id, status);
5119
dea31012005-04-17 16:05:31 -05005120 /*
James Smartbbb9d182009-06-10 17:23:16 -04005121 * We have to clean up i/o as : they may be orphaned by the TMF;
5122 * or if the TMF failed, they may be in an indeterminate state.
5123 * So, continue on.
5124 * We will report success if all the i/o aborts successfully.
dea31012005-04-17 16:05:31 -05005125 */
James Smart53151bb2013-10-10 12:24:07 -04005126 if (status == SUCCESS)
5127 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
James Smartbbb9d182009-06-10 17:23:16 -04005128 LPFC_CTX_LUN);
James Smart53151bb2013-10-10 12:24:07 -04005129
5130 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005131}
5132
5133/**
5134 * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
5135 * @cmnd: Pointer to scsi_cmnd data structure.
5136 *
5137 * This routine does a target reset by sending a TARGET_RESET task management
5138 * command.
5139 *
5140 * Return code :
5141 * 0x2003 - Error
5142 * 0x2002 - Success
5143 **/
5144static int
5145lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
5146{
5147 struct Scsi_Host *shost = cmnd->device->host;
5148 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smart1ba981f2014-02-20 09:56:45 -05005149 struct lpfc_rport_data *rdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005150 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005151 unsigned tgt_id = cmnd->device->id;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005152 uint64_t lun_id = cmnd->device->lun;
James Smartbbb9d182009-06-10 17:23:16 -04005153 struct lpfc_scsi_event_header scsi_event;
James Smart53151bb2013-10-10 12:24:07 -04005154 int status;
James Smartbbb9d182009-06-10 17:23:16 -04005155
James Smart1ba981f2014-02-20 09:56:45 -05005156 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smart1c6f4ef52009-11-18 15:40:49 -05005157 if (!rdata) {
5158 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5159 "0799 Target Reset rport failure: rdata x%p\n", rdata);
5160 return FAILED;
5161 }
5162 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04005163 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005164 if (status != 0 && status != SUCCESS)
James Smart589a52d2010-07-14 15:30:54 -04005165 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005166
5167 status = lpfc_chk_tgt_mapped(vport, cmnd);
5168 if (status == FAILED) {
5169 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5170 "0722 Target Reset rport failure: rdata x%p\n", rdata);
James Smart63e480f2015-04-07 15:07:20 -04005171 if (pnode) {
5172 spin_lock_irq(shost->host_lock);
5173 pnode->nlp_flag &= ~NLP_NPR_ADISC;
5174 pnode->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
5175 spin_unlock_irq(shost->host_lock);
5176 }
James Smart8c50d252014-09-03 12:58:16 -04005177 lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
5178 LPFC_CTX_TGT);
5179 return FAST_IO_FAIL;
dea31012005-04-17 16:05:31 -05005180 }
James Smartea2151b2008-09-07 11:52:10 -04005181
5182 scsi_event.event_type = FC_REG_SCSI_EVENT;
5183 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
5184 scsi_event.lun = 0;
5185 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5186 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5187
James Smartbbb9d182009-06-10 17:23:16 -04005188 fc_host_post_vendor_event(shost, fc_get_event_number(),
5189 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -04005190
James Smarteed695d2016-10-13 15:06:04 -07005191 status = lpfc_send_taskmgmt(vport, cmnd, tgt_id, lun_id,
James Smartbbb9d182009-06-10 17:23:16 -04005192 FCP_TARGET_RESET);
dea31012005-04-17 16:05:31 -05005193
James Smarte8b62012007-08-02 11:10:09 -04005194 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005195 "0723 SCSI layer issued Target Reset (%d, %llu) "
James Smartbbb9d182009-06-10 17:23:16 -04005196 "return x%x\n", tgt_id, lun_id, status);
5197
5198 /*
5199 * We have to clean up i/o as : they may be orphaned by the TMF;
5200 * or if the TMF failed, they may be in an indeterminate state.
5201 * So, continue on.
5202 * We will report success if all the i/o aborts successfully.
5203 */
James Smart53151bb2013-10-10 12:24:07 -04005204 if (status == SUCCESS)
5205 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
James Smart3a707302012-06-12 13:54:42 -04005206 LPFC_CTX_TGT);
James Smart53151bb2013-10-10 12:24:07 -04005207 return status;
dea31012005-04-17 16:05:31 -05005208}
5209
James Smart9bad7672008-12-04 22:39:02 -05005210/**
James Smart3621a712009-04-06 18:47:14 -04005211 * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05005212 * @cmnd: Pointer to scsi_cmnd data structure.
5213 *
James Smartbbb9d182009-06-10 17:23:16 -04005214 * This routine does target reset to all targets on @cmnd->device->host.
5215 * This emulates Parallel SCSI Bus Reset Semantics.
James Smart9bad7672008-12-04 22:39:02 -05005216 *
James Smartbbb9d182009-06-10 17:23:16 -04005217 * Return code :
5218 * 0x2003 - Error
5219 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05005220 **/
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04005221static int
James Smart7054a602007-04-25 09:52:34 -04005222lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05005223{
James Smart2e0fef82007-06-17 19:56:36 -05005224 struct Scsi_Host *shost = cmnd->device->host;
5225 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05005226 struct lpfc_nodelist *ndlp = NULL;
James Smartea2151b2008-09-07 11:52:10 -04005227 struct lpfc_scsi_event_header scsi_event;
James Smartbbb9d182009-06-10 17:23:16 -04005228 int match;
5229 int ret = SUCCESS, status, i;
James Smartea2151b2008-09-07 11:52:10 -04005230
5231 scsi_event.event_type = FC_REG_SCSI_EVENT;
5232 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
5233 scsi_event.lun = 0;
5234 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
5235 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
5236
James Smartbbb9d182009-06-10 17:23:16 -04005237 fc_host_post_vendor_event(shost, fc_get_event_number(),
5238 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
dea31012005-04-17 16:05:31 -05005239
James Smartbf086112011-08-21 21:48:13 -04005240 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005241 if (status != 0 && status != SUCCESS)
James Smartbf086112011-08-21 21:48:13 -04005242 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005243
dea31012005-04-17 16:05:31 -05005244 /*
5245 * Since the driver manages a single bus device, reset all
5246 * targets known to the driver. Should any target reset
5247 * fail, this routine returns failure to the midlayer.
5248 */
James Smarte17da182006-07-06 15:49:25 -04005249 for (i = 0; i < LPFC_MAX_TARGET; i++) {
James Smart685f0bf2007-04-25 09:53:08 -04005250 /* Search for mapped node by target ID */
dea31012005-04-17 16:05:31 -05005251 match = 0;
James Smart2e0fef82007-06-17 19:56:36 -05005252 spin_lock_irq(shost->host_lock);
5253 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05005254 if (!NLP_CHK_NODE_ACT(ndlp))
5255 continue;
James Smarta6571c62012-10-31 14:44:42 -04005256 if (vport->phba->cfg_fcp2_no_tgt_reset &&
5257 (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE))
5258 continue;
James Smart685f0bf2007-04-25 09:53:08 -04005259 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
James Smart915caaa2008-06-14 22:52:38 -04005260 ndlp->nlp_sid == i &&
James Smarta0f2d3e2017-02-12 13:52:31 -08005261 ndlp->rport &&
5262 ndlp->nlp_type & NLP_FCP_TARGET) {
dea31012005-04-17 16:05:31 -05005263 match = 1;
5264 break;
5265 }
5266 }
James Smart2e0fef82007-06-17 19:56:36 -05005267 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005268 if (!match)
5269 continue;
James Smartbbb9d182009-06-10 17:23:16 -04005270
James Smarteed695d2016-10-13 15:06:04 -07005271 status = lpfc_send_taskmgmt(vport, cmnd,
James Smartbbb9d182009-06-10 17:23:16 -04005272 i, 0, FCP_TARGET_RESET);
5273
5274 if (status != SUCCESS) {
James Smarte8b62012007-08-02 11:10:09 -04005275 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5276 "0700 Bus Reset on target %d failed\n",
5277 i);
James Smart915caaa2008-06-14 22:52:38 -04005278 ret = FAILED;
dea31012005-04-17 16:05:31 -05005279 }
5280 }
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005281 /*
James Smartbbb9d182009-06-10 17:23:16 -04005282 * We have to clean up i/o as : they may be orphaned by the TMFs
5283 * above; or if any of the TMFs failed, they may be in an
5284 * indeterminate state.
5285 * We will report success if all the i/o aborts successfully.
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005286 */
James Smartbbb9d182009-06-10 17:23:16 -04005287
5288 status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
5289 if (status != SUCCESS)
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005290 ret = FAILED;
James Smartbbb9d182009-06-10 17:23:16 -04005291
James Smarte8b62012007-08-02 11:10:09 -04005292 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5293 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea31012005-04-17 16:05:31 -05005294 return ret;
5295}
5296
James Smart9bad7672008-12-04 22:39:02 -05005297/**
James Smart27b01b82012-05-09 21:19:44 -04005298 * lpfc_host_reset_handler - scsi_host_template eh_host_reset_handler entry pt
5299 * @cmnd: Pointer to scsi_cmnd data structure.
5300 *
5301 * This routine does host reset to the adaptor port. It brings the HBA
5302 * offline, performs a board restart, and then brings the board back online.
5303 * The lpfc_offline calls lpfc_sli_hba_down which will abort and local
5304 * reject all outstanding SCSI commands to the host and error returned
5305 * back to SCSI mid-level. As this will be SCSI mid-level's last resort
5306 * of error handling, it will only return error if resetting of the adapter
5307 * is not successful; in all other cases, will return success.
5308 *
5309 * Return code :
5310 * 0x2003 - Error
5311 * 0x2002 - Success
5312 **/
5313static int
5314lpfc_host_reset_handler(struct scsi_cmnd *cmnd)
5315{
5316 struct Scsi_Host *shost = cmnd->device->host;
5317 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5318 struct lpfc_hba *phba = vport->phba;
5319 int rc, ret = SUCCESS;
5320
James Smarta88dbb62013-04-17 20:18:39 -04005321 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5322 "3172 SCSI layer issued Host Reset Data:\n");
5323
James Smart618a5232012-06-12 13:54:36 -04005324 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart27b01b82012-05-09 21:19:44 -04005325 lpfc_offline(phba);
5326 rc = lpfc_sli_brdrestart(phba);
5327 if (rc)
5328 ret = FAILED;
James Smarta88dbb62013-04-17 20:18:39 -04005329 rc = lpfc_online(phba);
5330 if (rc)
5331 ret = FAILED;
James Smart27b01b82012-05-09 21:19:44 -04005332 lpfc_unblock_mgmt_io(phba);
5333
James Smarta88dbb62013-04-17 20:18:39 -04005334 if (ret == FAILED) {
5335 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5336 "3323 Failed host reset, bring it offline\n");
5337 lpfc_sli4_offline_eratt(phba);
5338 }
James Smart27b01b82012-05-09 21:19:44 -04005339 return ret;
5340}
5341
5342/**
James Smart3621a712009-04-06 18:47:14 -04005343 * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
James Smart9bad7672008-12-04 22:39:02 -05005344 * @sdev: Pointer to scsi_device.
5345 *
5346 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
5347 * globally available list of scsi buffers. This routine also makes sure scsi
5348 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
5349 * of scsi buffer exists for the lifetime of the driver.
5350 *
5351 * Return codes:
5352 * non-0 - Error
5353 * 0 - Success
5354 **/
dea31012005-04-17 16:05:31 -05005355static int
dea31012005-04-17 16:05:31 -05005356lpfc_slave_alloc(struct scsi_device *sdev)
5357{
James Smart2e0fef82007-06-17 19:56:36 -05005358 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5359 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005360 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
James Smart3772a992009-05-22 14:50:54 -04005361 uint32_t total = 0;
dea31012005-04-17 16:05:31 -05005362 uint32_t num_to_alloc = 0;
James Smart3772a992009-05-22 14:50:54 -04005363 int num_allocated = 0;
James Smartd7c47992010-06-08 18:31:54 -04005364 uint32_t sdev_cnt;
James Smart1ba981f2014-02-20 09:56:45 -05005365 struct lpfc_device_data *device_data;
5366 unsigned long flags;
5367 struct lpfc_name target_wwpn;
dea31012005-04-17 16:05:31 -05005368
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005369 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05005370 return -ENXIO;
5371
James Smartf38fa0b2014-04-04 13:52:21 -04005372 if (phba->cfg_fof) {
James Smart1ba981f2014-02-20 09:56:45 -05005373
5374 /*
5375 * Check to see if the device data structure for the lun
5376 * exists. If not, create one.
5377 */
5378
5379 u64_to_wwn(rport->port_name, target_wwpn.u.wwn);
5380 spin_lock_irqsave(&phba->devicelock, flags);
5381 device_data = __lpfc_get_device_data(phba,
5382 &phba->luns,
5383 &vport->fc_portname,
5384 &target_wwpn,
5385 sdev->lun);
5386 if (!device_data) {
5387 spin_unlock_irqrestore(&phba->devicelock, flags);
5388 device_data = lpfc_create_device_data(phba,
5389 &vport->fc_portname,
5390 &target_wwpn,
James Smartb5749fe2016-12-19 15:07:26 -08005391 sdev->lun,
5392 phba->cfg_XLanePriority,
5393 true);
James Smart1ba981f2014-02-20 09:56:45 -05005394 if (!device_data)
5395 return -ENOMEM;
5396 spin_lock_irqsave(&phba->devicelock, flags);
5397 list_add_tail(&device_data->listentry, &phba->luns);
5398 }
5399 device_data->rport_data = rport->dd_data;
5400 device_data->available = true;
5401 spin_unlock_irqrestore(&phba->devicelock, flags);
5402 sdev->hostdata = device_data;
5403 } else {
5404 sdev->hostdata = rport->dd_data;
5405 }
James Smartd7c47992010-06-08 18:31:54 -04005406 sdev_cnt = atomic_inc_return(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05005407
James Smart0794d602019-01-28 11:14:19 -08005408 /* For SLI4, all IO buffers are pre-allocated */
5409 if (phba->sli_rev == LPFC_SLI_REV4)
5410 return 0;
5411
5412 /* This code path is now ONLY for SLI3 adapters */
5413
dea31012005-04-17 16:05:31 -05005414 /*
5415 * Populate the cmds_per_lun count scsi_bufs into this host's globally
5416 * available list of scsi buffers. Don't allocate more than the
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04005417 * HBA limit conveyed to the midlayer via the host structure. The
5418 * formula accounts for the lun_queue_depth + error handlers + 1
5419 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea31012005-04-17 16:05:31 -05005420 */
5421 total = phba->total_scsi_bufs;
James Smart3de2a652007-08-02 11:09:59 -04005422 num_to_alloc = vport->cfg_lun_queue_depth + 2;
James Smart92d7f7b2007-06-17 19:56:38 -05005423
James Smartd7c47992010-06-08 18:31:54 -04005424 /* If allocated buffers are enough do nothing */
5425 if ((sdev_cnt * (vport->cfg_lun_queue_depth + 2)) < total)
5426 return 0;
5427
James Smart92d7f7b2007-06-17 19:56:38 -05005428 /* Allow some exchanges to be available always to complete discovery */
5429 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04005430 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5431 "0704 At limitation of %d preallocated "
5432 "command buffers\n", total);
dea31012005-04-17 16:05:31 -05005433 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05005434 /* Allow some exchanges to be available always to complete discovery */
5435 } else if (total + num_to_alloc >
5436 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04005437 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5438 "0705 Allocation request of %d "
5439 "command buffers will exceed max of %d. "
5440 "Reducing allocation request to %d.\n",
5441 num_to_alloc, phba->cfg_hba_queue_depth,
5442 (phba->cfg_hba_queue_depth - total));
dea31012005-04-17 16:05:31 -05005443 num_to_alloc = phba->cfg_hba_queue_depth - total;
5444 }
James Smart0794d602019-01-28 11:14:19 -08005445 num_allocated = lpfc_new_scsi_buf_s3(vport, num_to_alloc);
James Smart3772a992009-05-22 14:50:54 -04005446 if (num_to_alloc != num_allocated) {
James Smart96f70772013-04-17 20:16:15 -04005447 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5448 "0708 Allocation request of %d "
5449 "command buffers did not succeed. "
5450 "Allocated %d buffers.\n",
5451 num_to_alloc, num_allocated);
dea31012005-04-17 16:05:31 -05005452 }
James Smart1c6f4ef52009-11-18 15:40:49 -05005453 if (num_allocated > 0)
5454 phba->total_scsi_bufs += num_allocated;
dea31012005-04-17 16:05:31 -05005455 return 0;
5456}
5457
James Smart9bad7672008-12-04 22:39:02 -05005458/**
James Smart3621a712009-04-06 18:47:14 -04005459 * lpfc_slave_configure - scsi_host_template slave_configure entry point
James Smart9bad7672008-12-04 22:39:02 -05005460 * @sdev: Pointer to scsi_device.
5461 *
5462 * This routine configures following items
5463 * - Tag command queuing support for @sdev if supported.
James Smart9bad7672008-12-04 22:39:02 -05005464 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
5465 *
5466 * Return codes:
5467 * 0 - Success
5468 **/
dea31012005-04-17 16:05:31 -05005469static int
5470lpfc_slave_configure(struct scsi_device *sdev)
5471{
James Smart2e0fef82007-06-17 19:56:36 -05005472 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5473 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05005474
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01005475 scsi_change_queue_depth(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05005476
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005477 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04005478 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -08005479 &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005480 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
5481 lpfc_poll_rearm_timer(phba);
5482 }
5483
dea31012005-04-17 16:05:31 -05005484 return 0;
5485}
5486
James Smart9bad7672008-12-04 22:39:02 -05005487/**
James Smart3621a712009-04-06 18:47:14 -04005488 * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
James Smart9bad7672008-12-04 22:39:02 -05005489 * @sdev: Pointer to scsi_device.
5490 *
5491 * This routine sets @sdev hostatdata filed to null.
5492 **/
dea31012005-04-17 16:05:31 -05005493static void
5494lpfc_slave_destroy(struct scsi_device *sdev)
5495{
James Smartd7c47992010-06-08 18:31:54 -04005496 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5497 struct lpfc_hba *phba = vport->phba;
James Smart1ba981f2014-02-20 09:56:45 -05005498 unsigned long flags;
5499 struct lpfc_device_data *device_data = sdev->hostdata;
5500
James Smartd7c47992010-06-08 18:31:54 -04005501 atomic_dec(&phba->sdev_cnt);
James Smartf38fa0b2014-04-04 13:52:21 -04005502 if ((phba->cfg_fof) && (device_data)) {
James Smart1ba981f2014-02-20 09:56:45 -05005503 spin_lock_irqsave(&phba->devicelock, flags);
5504 device_data->available = false;
5505 if (!device_data->oas_enabled)
5506 lpfc_delete_device_data(phba, device_data);
5507 spin_unlock_irqrestore(&phba->devicelock, flags);
5508 }
dea31012005-04-17 16:05:31 -05005509 sdev->hostdata = NULL;
5510 return;
5511}
5512
James Smart1ba981f2014-02-20 09:56:45 -05005513/**
5514 * lpfc_create_device_data - creates and initializes device data structure for OAS
5515 * @pha: Pointer to host bus adapter structure.
5516 * @vport_wwpn: Pointer to vport's wwpn information
5517 * @target_wwpn: Pointer to target's wwpn information
5518 * @lun: Lun on target
5519 * @atomic_create: Flag to indicate if memory should be allocated using the
5520 * GFP_ATOMIC flag or not.
5521 *
5522 * This routine creates a device data structure which will contain identifying
5523 * information for the device (host wwpn, target wwpn, lun), state of OAS,
5524 * whether or not the corresponding lun is available by the system,
5525 * and pointer to the rport data.
5526 *
5527 * Return codes:
5528 * NULL - Error
5529 * Pointer to lpfc_device_data - Success
5530 **/
5531struct lpfc_device_data*
5532lpfc_create_device_data(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
5533 struct lpfc_name *target_wwpn, uint64_t lun,
James Smartb5749fe2016-12-19 15:07:26 -08005534 uint32_t pri, bool atomic_create)
James Smart1ba981f2014-02-20 09:56:45 -05005535{
5536
5537 struct lpfc_device_data *lun_info;
5538 int memory_flags;
5539
5540 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
James Smartf38fa0b2014-04-04 13:52:21 -04005541 !(phba->cfg_fof))
James Smart1ba981f2014-02-20 09:56:45 -05005542 return NULL;
5543
5544 /* Attempt to create the device data to contain lun info */
5545
5546 if (atomic_create)
5547 memory_flags = GFP_ATOMIC;
5548 else
5549 memory_flags = GFP_KERNEL;
5550 lun_info = mempool_alloc(phba->device_data_mem_pool, memory_flags);
5551 if (!lun_info)
5552 return NULL;
5553 INIT_LIST_HEAD(&lun_info->listentry);
5554 lun_info->rport_data = NULL;
5555 memcpy(&lun_info->device_id.vport_wwpn, vport_wwpn,
5556 sizeof(struct lpfc_name));
5557 memcpy(&lun_info->device_id.target_wwpn, target_wwpn,
5558 sizeof(struct lpfc_name));
5559 lun_info->device_id.lun = lun;
5560 lun_info->oas_enabled = false;
James Smartb5749fe2016-12-19 15:07:26 -08005561 lun_info->priority = pri;
James Smart1ba981f2014-02-20 09:56:45 -05005562 lun_info->available = false;
5563 return lun_info;
5564}
5565
5566/**
5567 * lpfc_delete_device_data - frees a device data structure for OAS
5568 * @pha: Pointer to host bus adapter structure.
5569 * @lun_info: Pointer to device data structure to free.
5570 *
5571 * This routine frees the previously allocated device data structure passed.
5572 *
5573 **/
5574void
5575lpfc_delete_device_data(struct lpfc_hba *phba,
5576 struct lpfc_device_data *lun_info)
5577{
5578
5579 if (unlikely(!phba) || !lun_info ||
James Smartf38fa0b2014-04-04 13:52:21 -04005580 !(phba->cfg_fof))
James Smart1ba981f2014-02-20 09:56:45 -05005581 return;
5582
5583 if (!list_empty(&lun_info->listentry))
5584 list_del(&lun_info->listentry);
5585 mempool_free(lun_info, phba->device_data_mem_pool);
5586 return;
5587}
5588
5589/**
5590 * __lpfc_get_device_data - returns the device data for the specified lun
5591 * @pha: Pointer to host bus adapter structure.
5592 * @list: Point to list to search.
5593 * @vport_wwpn: Pointer to vport's wwpn information
5594 * @target_wwpn: Pointer to target's wwpn information
5595 * @lun: Lun on target
5596 *
5597 * This routine searches the list passed for the specified lun's device data.
5598 * This function does not hold locks, it is the responsibility of the caller
5599 * to ensure the proper lock is held before calling the function.
5600 *
5601 * Return codes:
5602 * NULL - Error
5603 * Pointer to lpfc_device_data - Success
5604 **/
5605struct lpfc_device_data*
5606__lpfc_get_device_data(struct lpfc_hba *phba, struct list_head *list,
5607 struct lpfc_name *vport_wwpn,
5608 struct lpfc_name *target_wwpn, uint64_t lun)
5609{
5610
5611 struct lpfc_device_data *lun_info;
5612
5613 if (unlikely(!phba) || !list || !vport_wwpn || !target_wwpn ||
James Smartf38fa0b2014-04-04 13:52:21 -04005614 !phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005615 return NULL;
5616
5617 /* Check to see if the lun is already enabled for OAS. */
5618
5619 list_for_each_entry(lun_info, list, listentry) {
5620 if ((memcmp(&lun_info->device_id.vport_wwpn, vport_wwpn,
5621 sizeof(struct lpfc_name)) == 0) &&
5622 (memcmp(&lun_info->device_id.target_wwpn, target_wwpn,
5623 sizeof(struct lpfc_name)) == 0) &&
5624 (lun_info->device_id.lun == lun))
5625 return lun_info;
5626 }
5627
5628 return NULL;
5629}
5630
5631/**
5632 * lpfc_find_next_oas_lun - searches for the next oas lun
5633 * @pha: Pointer to host bus adapter structure.
5634 * @vport_wwpn: Pointer to vport's wwpn information
5635 * @target_wwpn: Pointer to target's wwpn information
5636 * @starting_lun: Pointer to the lun to start searching for
5637 * @found_vport_wwpn: Pointer to the found lun's vport wwpn information
5638 * @found_target_wwpn: Pointer to the found lun's target wwpn information
5639 * @found_lun: Pointer to the found lun.
5640 * @found_lun_status: Pointer to status of the found lun.
5641 *
5642 * This routine searches the luns list for the specified lun
5643 * or the first lun for the vport/target. If the vport wwpn contains
5644 * a zero value then a specific vport is not specified. In this case
5645 * any vport which contains the lun will be considered a match. If the
5646 * target wwpn contains a zero value then a specific target is not specified.
5647 * In this case any target which contains the lun will be considered a
5648 * match. If the lun is found, the lun, vport wwpn, target wwpn and lun status
5649 * are returned. The function will also return the next lun if available.
5650 * If the next lun is not found, starting_lun parameter will be set to
5651 * NO_MORE_OAS_LUN.
5652 *
5653 * Return codes:
5654 * non-0 - Error
5655 * 0 - Success
5656 **/
5657bool
5658lpfc_find_next_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
5659 struct lpfc_name *target_wwpn, uint64_t *starting_lun,
5660 struct lpfc_name *found_vport_wwpn,
5661 struct lpfc_name *found_target_wwpn,
5662 uint64_t *found_lun,
James Smartb5749fe2016-12-19 15:07:26 -08005663 uint32_t *found_lun_status,
5664 uint32_t *found_lun_pri)
James Smart1ba981f2014-02-20 09:56:45 -05005665{
5666
5667 unsigned long flags;
5668 struct lpfc_device_data *lun_info;
5669 struct lpfc_device_id *device_id;
5670 uint64_t lun;
5671 bool found = false;
5672
5673 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
5674 !starting_lun || !found_vport_wwpn ||
5675 !found_target_wwpn || !found_lun || !found_lun_status ||
5676 (*starting_lun == NO_MORE_OAS_LUN) ||
James Smartf38fa0b2014-04-04 13:52:21 -04005677 !phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005678 return false;
5679
5680 lun = *starting_lun;
5681 *found_lun = NO_MORE_OAS_LUN;
5682 *starting_lun = NO_MORE_OAS_LUN;
5683
5684 /* Search for lun or the lun closet in value */
5685
5686 spin_lock_irqsave(&phba->devicelock, flags);
5687 list_for_each_entry(lun_info, &phba->luns, listentry) {
5688 if (((wwn_to_u64(vport_wwpn->u.wwn) == 0) ||
5689 (memcmp(&lun_info->device_id.vport_wwpn, vport_wwpn,
5690 sizeof(struct lpfc_name)) == 0)) &&
5691 ((wwn_to_u64(target_wwpn->u.wwn) == 0) ||
5692 (memcmp(&lun_info->device_id.target_wwpn, target_wwpn,
5693 sizeof(struct lpfc_name)) == 0)) &&
5694 (lun_info->oas_enabled)) {
5695 device_id = &lun_info->device_id;
5696 if ((!found) &&
5697 ((lun == FIND_FIRST_OAS_LUN) ||
5698 (device_id->lun == lun))) {
5699 *found_lun = device_id->lun;
5700 memcpy(found_vport_wwpn,
5701 &device_id->vport_wwpn,
5702 sizeof(struct lpfc_name));
5703 memcpy(found_target_wwpn,
5704 &device_id->target_wwpn,
5705 sizeof(struct lpfc_name));
5706 if (lun_info->available)
5707 *found_lun_status =
5708 OAS_LUN_STATUS_EXISTS;
5709 else
5710 *found_lun_status = 0;
James Smartb5749fe2016-12-19 15:07:26 -08005711 *found_lun_pri = lun_info->priority;
James Smart1ba981f2014-02-20 09:56:45 -05005712 if (phba->cfg_oas_flags & OAS_FIND_ANY_VPORT)
5713 memset(vport_wwpn, 0x0,
5714 sizeof(struct lpfc_name));
5715 if (phba->cfg_oas_flags & OAS_FIND_ANY_TARGET)
5716 memset(target_wwpn, 0x0,
5717 sizeof(struct lpfc_name));
5718 found = true;
5719 } else if (found) {
5720 *starting_lun = device_id->lun;
5721 memcpy(vport_wwpn, &device_id->vport_wwpn,
5722 sizeof(struct lpfc_name));
5723 memcpy(target_wwpn, &device_id->target_wwpn,
5724 sizeof(struct lpfc_name));
5725 break;
5726 }
5727 }
5728 }
5729 spin_unlock_irqrestore(&phba->devicelock, flags);
5730 return found;
5731}
5732
5733/**
5734 * lpfc_enable_oas_lun - enables a lun for OAS operations
5735 * @pha: Pointer to host bus adapter structure.
5736 * @vport_wwpn: Pointer to vport's wwpn information
5737 * @target_wwpn: Pointer to target's wwpn information
5738 * @lun: Lun
5739 *
5740 * This routine enables a lun for oas operations. The routines does so by
5741 * doing the following :
5742 *
5743 * 1) Checks to see if the device data for the lun has been created.
5744 * 2) If found, sets the OAS enabled flag if not set and returns.
5745 * 3) Otherwise, creates a device data structure.
5746 * 4) If successfully created, indicates the device data is for an OAS lun,
5747 * indicates the lun is not available and add to the list of luns.
5748 *
5749 * Return codes:
5750 * false - Error
5751 * true - Success
5752 **/
5753bool
5754lpfc_enable_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
James Smartc92c8412016-07-06 12:36:05 -07005755 struct lpfc_name *target_wwpn, uint64_t lun, uint8_t pri)
James Smart1ba981f2014-02-20 09:56:45 -05005756{
5757
5758 struct lpfc_device_data *lun_info;
5759 unsigned long flags;
5760
5761 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
James Smartf38fa0b2014-04-04 13:52:21 -04005762 !phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005763 return false;
5764
5765 spin_lock_irqsave(&phba->devicelock, flags);
5766
5767 /* Check to see if the device data for the lun has been created */
5768 lun_info = __lpfc_get_device_data(phba, &phba->luns, vport_wwpn,
5769 target_wwpn, lun);
5770 if (lun_info) {
5771 if (!lun_info->oas_enabled)
5772 lun_info->oas_enabled = true;
James Smartb5749fe2016-12-19 15:07:26 -08005773 lun_info->priority = pri;
James Smart1ba981f2014-02-20 09:56:45 -05005774 spin_unlock_irqrestore(&phba->devicelock, flags);
5775 return true;
5776 }
5777
5778 /* Create an lun info structure and add to list of luns */
5779 lun_info = lpfc_create_device_data(phba, vport_wwpn, target_wwpn, lun,
James Smartb5749fe2016-12-19 15:07:26 -08005780 pri, false);
James Smart1ba981f2014-02-20 09:56:45 -05005781 if (lun_info) {
5782 lun_info->oas_enabled = true;
James Smartc92c8412016-07-06 12:36:05 -07005783 lun_info->priority = pri;
James Smart1ba981f2014-02-20 09:56:45 -05005784 lun_info->available = false;
5785 list_add_tail(&lun_info->listentry, &phba->luns);
5786 spin_unlock_irqrestore(&phba->devicelock, flags);
5787 return true;
5788 }
5789 spin_unlock_irqrestore(&phba->devicelock, flags);
5790 return false;
5791}
5792
5793/**
5794 * lpfc_disable_oas_lun - disables a lun for OAS operations
5795 * @pha: Pointer to host bus adapter structure.
5796 * @vport_wwpn: Pointer to vport's wwpn information
5797 * @target_wwpn: Pointer to target's wwpn information
5798 * @lun: Lun
5799 *
5800 * This routine disables a lun for oas operations. The routines does so by
5801 * doing the following :
5802 *
5803 * 1) Checks to see if the device data for the lun is created.
5804 * 2) If present, clears the flag indicating this lun is for OAS.
5805 * 3) If the lun is not available by the system, the device data is
5806 * freed.
5807 *
5808 * Return codes:
5809 * false - Error
5810 * true - Success
5811 **/
5812bool
5813lpfc_disable_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
James Smartb5749fe2016-12-19 15:07:26 -08005814 struct lpfc_name *target_wwpn, uint64_t lun, uint8_t pri)
James Smart1ba981f2014-02-20 09:56:45 -05005815{
5816
5817 struct lpfc_device_data *lun_info;
5818 unsigned long flags;
5819
5820 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
James Smartf38fa0b2014-04-04 13:52:21 -04005821 !phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005822 return false;
5823
5824 spin_lock_irqsave(&phba->devicelock, flags);
5825
5826 /* Check to see if the lun is available. */
5827 lun_info = __lpfc_get_device_data(phba,
5828 &phba->luns, vport_wwpn,
5829 target_wwpn, lun);
5830 if (lun_info) {
5831 lun_info->oas_enabled = false;
James Smartb5749fe2016-12-19 15:07:26 -08005832 lun_info->priority = pri;
James Smart1ba981f2014-02-20 09:56:45 -05005833 if (!lun_info->available)
5834 lpfc_delete_device_data(phba, lun_info);
5835 spin_unlock_irqrestore(&phba->devicelock, flags);
5836 return true;
5837 }
5838
5839 spin_unlock_irqrestore(&phba->devicelock, flags);
5840 return false;
5841}
James Smart92d7f7b2007-06-17 19:56:38 -05005842
James Smart895427b2017-02-12 13:52:30 -08005843static int
5844lpfc_no_command(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
5845{
5846 return SCSI_MLQUEUE_HOST_BUSY;
5847}
5848
5849static int
5850lpfc_no_handler(struct scsi_cmnd *cmnd)
5851{
5852 return FAILED;
5853}
5854
5855static int
5856lpfc_no_slave(struct scsi_device *sdev)
5857{
5858 return -ENODEV;
5859}
5860
5861struct scsi_host_template lpfc_template_nvme = {
5862 .module = THIS_MODULE,
5863 .name = LPFC_DRIVER_NAME,
5864 .proc_name = LPFC_DRIVER_NAME,
5865 .info = lpfc_info,
5866 .queuecommand = lpfc_no_command,
5867 .eh_abort_handler = lpfc_no_handler,
5868 .eh_device_reset_handler = lpfc_no_handler,
5869 .eh_target_reset_handler = lpfc_no_handler,
5870 .eh_bus_reset_handler = lpfc_no_handler,
5871 .eh_host_reset_handler = lpfc_no_handler,
5872 .slave_alloc = lpfc_no_slave,
5873 .slave_configure = lpfc_no_slave,
5874 .scan_finished = lpfc_scan_finished,
5875 .this_id = -1,
5876 .sg_tablesize = 1,
5877 .cmd_per_lun = 1,
James Smart895427b2017-02-12 13:52:30 -08005878 .shost_attrs = lpfc_hba_attrs,
5879 .max_sectors = 0xFFFF,
5880 .vendor_id = LPFC_NL_VENDOR_ID,
5881 .track_queue_depth = 0,
5882};
5883
James Smart96418b52017-03-04 09:30:31 -08005884struct scsi_host_template lpfc_template_no_hr = {
James Smartea4142f2015-04-07 15:07:13 -04005885 .module = THIS_MODULE,
5886 .name = LPFC_DRIVER_NAME,
James Smart08dcd4c2016-07-06 12:35:59 -07005887 .proc_name = LPFC_DRIVER_NAME,
James Smartea4142f2015-04-07 15:07:13 -04005888 .info = lpfc_info,
5889 .queuecommand = lpfc_queuecommand,
James Smart856984b2017-03-04 09:30:32 -08005890 .eh_timed_out = fc_eh_timed_out,
James Smartea4142f2015-04-07 15:07:13 -04005891 .eh_abort_handler = lpfc_abort_handler,
5892 .eh_device_reset_handler = lpfc_device_reset_handler,
5893 .eh_target_reset_handler = lpfc_target_reset_handler,
5894 .eh_bus_reset_handler = lpfc_bus_reset_handler,
5895 .slave_alloc = lpfc_slave_alloc,
5896 .slave_configure = lpfc_slave_configure,
5897 .slave_destroy = lpfc_slave_destroy,
5898 .scan_finished = lpfc_scan_finished,
5899 .this_id = -1,
5900 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
5901 .cmd_per_lun = LPFC_CMD_PER_LUN,
James Smartea4142f2015-04-07 15:07:13 -04005902 .shost_attrs = lpfc_hba_attrs,
5903 .max_sectors = 0xFFFF,
5904 .vendor_id = LPFC_NL_VENDOR_ID,
5905 .change_queue_depth = scsi_change_queue_depth,
James Smartea4142f2015-04-07 15:07:13 -04005906 .track_queue_depth = 1,
5907};
5908
dea31012005-04-17 16:05:31 -05005909struct scsi_host_template lpfc_template = {
5910 .module = THIS_MODULE,
5911 .name = LPFC_DRIVER_NAME,
James Smart08dcd4c2016-07-06 12:35:59 -07005912 .proc_name = LPFC_DRIVER_NAME,
dea31012005-04-17 16:05:31 -05005913 .info = lpfc_info,
5914 .queuecommand = lpfc_queuecommand,
Christoph Hellwigb6a05c82017-01-30 13:18:58 +01005915 .eh_timed_out = fc_eh_timed_out,
dea31012005-04-17 16:05:31 -05005916 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04005917 .eh_device_reset_handler = lpfc_device_reset_handler,
5918 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart7054a602007-04-25 09:52:34 -04005919 .eh_bus_reset_handler = lpfc_bus_reset_handler,
James Smart27b01b82012-05-09 21:19:44 -04005920 .eh_host_reset_handler = lpfc_host_reset_handler,
dea31012005-04-17 16:05:31 -05005921 .slave_alloc = lpfc_slave_alloc,
5922 .slave_configure = lpfc_slave_configure,
5923 .slave_destroy = lpfc_slave_destroy,
James Smart47a86172007-04-25 09:53:22 -04005924 .scan_finished = lpfc_scan_finished,
dea31012005-04-17 16:05:31 -05005925 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05005926 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
dea31012005-04-17 16:05:31 -05005927 .cmd_per_lun = LPFC_CMD_PER_LUN,
James Smart2e0fef82007-06-17 19:56:36 -05005928 .shost_attrs = lpfc_hba_attrs,
James.Smart@Emulex.Com564b2962005-06-25 10:34:17 -04005929 .max_sectors = 0xFFFF,
James Smartf1c3b0f2009-07-19 10:01:32 -04005930 .vendor_id = LPFC_NL_VENDOR_ID,
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,
dea31012005-04-17 16:05:31 -05005933};
James Smart3de2a652007-08-02 11:09:59 -04005934
5935struct scsi_host_template lpfc_vport_template = {
5936 .module = THIS_MODULE,
5937 .name = LPFC_DRIVER_NAME,
James Smart08dcd4c2016-07-06 12:35:59 -07005938 .proc_name = LPFC_DRIVER_NAME,
James Smart3de2a652007-08-02 11:09:59 -04005939 .info = lpfc_info,
5940 .queuecommand = lpfc_queuecommand,
Christoph Hellwigb6a05c82017-01-30 13:18:58 +01005941 .eh_timed_out = fc_eh_timed_out,
James Smart3de2a652007-08-02 11:09:59 -04005942 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04005943 .eh_device_reset_handler = lpfc_device_reset_handler,
5944 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart3de2a652007-08-02 11:09:59 -04005945 .slave_alloc = lpfc_slave_alloc,
5946 .slave_configure = lpfc_slave_configure,
5947 .slave_destroy = lpfc_slave_destroy,
5948 .scan_finished = lpfc_scan_finished,
5949 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05005950 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
James Smart3de2a652007-08-02 11:09:59 -04005951 .cmd_per_lun = LPFC_CMD_PER_LUN,
James Smart3de2a652007-08-02 11:09:59 -04005952 .shost_attrs = lpfc_vport_attrs,
5953 .max_sectors = 0xFFFF,
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01005954 .change_queue_depth = scsi_change_queue_depth,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01005955 .track_queue_depth = 1,
James Smart3de2a652007-08-02 11:09:59 -04005956};