blob: 4fa6703a9ec9c7031b638245d7bcf6f391d0507f [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{
185 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
186 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;
397 dma_addr_t pdma_phys_bpl;
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);
441 psb->fcp_bpl = 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. */
445 bpl = psb->fcp_bpl;
446 pdma_phys_fcp_cmd = psb->dma_handle;
447 pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
448 pdma_phys_bpl = psb->dma_handle + sizeof(struct fcp_cmnd) +
449 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 =
499 putPaddrLow(pdma_phys_bpl);
500 iocb->un.fcpi64.bdl.addrHigh =
501 putPaddrHigh(pdma_phys_bpl);
502 iocb->ulpBdeCount = 1;
503 iocb->ulpLe = 1;
504 }
505 iocb->ulpClass = CLASS3;
506 psb->status = IOSTAT_SUCCESS;
James Smartda0436e2009-05-22 14:51:39 -0400507 /* Put it back into the SCSI buffer list */
James Smarteee88772010-09-29 11:19:08 -0400508 psb->cur_iocbq.context1 = psb;
James Smart1c6f4ef52009-11-18 15:40:49 -0500509 lpfc_release_scsi_buf_s3(phba, psb);
James Smart3772a992009-05-22 14:50:54 -0400510
James Smart34b02dc2008-08-24 21:49:55 -0400511 }
dea31012005-04-17 16:05:31 -0500512
James Smart3772a992009-05-22 14:50:54 -0400513 return bcnt;
dea31012005-04-17 16:05:31 -0500514}
515
James Smart9bad7672008-12-04 22:39:02 -0500516/**
James Smart1151e3e2011-02-16 12:39:35 -0500517 * lpfc_sli4_vport_delete_fcp_xri_aborted -Remove all ndlp references for vport
518 * @vport: pointer to lpfc vport data structure.
519 *
520 * This routine is invoked by the vport cleanup for deletions and the cleanup
521 * for an ndlp on removal.
522 **/
523void
524lpfc_sli4_vport_delete_fcp_xri_aborted(struct lpfc_vport *vport)
525{
526 struct lpfc_hba *phba = vport->phba;
527 struct lpfc_scsi_buf *psb, *next_psb;
528 unsigned long iflag = 0;
529
James Smart895427b2017-02-12 13:52:30 -0800530 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
531 return;
James Smart1151e3e2011-02-16 12:39:35 -0500532 spin_lock_irqsave(&phba->hbalock, iflag);
533 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
534 list_for_each_entry_safe(psb, next_psb,
535 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
536 if (psb->rdata && psb->rdata->pnode
537 && psb->rdata->pnode->vport == vport)
538 psb->rdata = NULL;
539 }
540 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
541 spin_unlock_irqrestore(&phba->hbalock, iflag);
542}
543
544/**
James Smartda0436e2009-05-22 14:51:39 -0400545 * lpfc_sli4_fcp_xri_aborted - Fast-path process of fcp xri abort
546 * @phba: pointer to lpfc hba data structure.
547 * @axri: pointer to the fcp xri abort wcqe structure.
548 *
549 * This routine is invoked by the worker thread to process a SLI4 fast-path
550 * FCP aborted xri.
551 **/
552void
553lpfc_sli4_fcp_xri_aborted(struct lpfc_hba *phba,
554 struct sli4_wcqe_xri_aborted *axri)
555{
556 uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
James Smart19ca7602010-11-20 23:11:55 -0500557 uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
James Smartda0436e2009-05-22 14:51:39 -0400558 struct lpfc_scsi_buf *psb, *next_psb;
559 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -0500560 struct lpfc_iocbq *iocbq;
561 int i;
James Smart19ca7602010-11-20 23:11:55 -0500562 struct lpfc_nodelist *ndlp;
563 int rrq_empty = 0;
James Smart895427b2017-02-12 13:52:30 -0800564 struct lpfc_sli_ring *pring = phba->sli4_hba.els_wq->pring;
James Smartda0436e2009-05-22 14:51:39 -0400565
James Smart895427b2017-02-12 13:52:30 -0800566 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
567 return;
James Smart0f65ff62010-02-26 14:14:23 -0500568 spin_lock_irqsave(&phba->hbalock, iflag);
569 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
James Smartda0436e2009-05-22 14:51:39 -0400570 list_for_each_entry_safe(psb, next_psb,
571 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
572 if (psb->cur_iocbq.sli4_xritag == xri) {
573 list_del(&psb->list);
James Smart341af102010-01-26 23:07:37 -0500574 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400575 psb->status = IOSTAT_SUCCESS;
James Smart0f65ff62010-02-26 14:14:23 -0500576 spin_unlock(
577 &phba->sli4_hba.abts_scsi_buf_list_lock);
James Smart1151e3e2011-02-16 12:39:35 -0500578 if (psb->rdata && psb->rdata->pnode)
579 ndlp = psb->rdata->pnode;
580 else
581 ndlp = NULL;
582
James Smart19ca7602010-11-20 23:11:55 -0500583 rrq_empty = list_empty(&phba->active_rrq_list);
James Smart0f65ff62010-02-26 14:14:23 -0500584 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartcb69f7d2011-12-13 13:21:57 -0500585 if (ndlp) {
James Smartee0f4fe2012-05-09 21:19:14 -0400586 lpfc_set_rrq_active(phba, ndlp,
587 psb->cur_iocbq.sli4_lxritag, rxid, 1);
James Smartcb69f7d2011-12-13 13:21:57 -0500588 lpfc_sli4_abts_err_handler(phba, ndlp, axri);
589 }
James Smartda0436e2009-05-22 14:51:39 -0400590 lpfc_release_scsi_buf_s4(phba, psb);
James Smart19ca7602010-11-20 23:11:55 -0500591 if (rrq_empty)
592 lpfc_worker_wake_up(phba);
James Smartda0436e2009-05-22 14:51:39 -0400593 return;
594 }
595 }
James Smart0f65ff62010-02-26 14:14:23 -0500596 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
597 for (i = 1; i <= phba->sli.last_iotag; i++) {
598 iocbq = phba->sli.iocbq_lookup[i];
599
600 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
601 (iocbq->iocb_flag & LPFC_IO_LIBDFC))
602 continue;
603 if (iocbq->sli4_xritag != xri)
604 continue;
605 psb = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
606 psb->exch_busy = 0;
607 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart0e9bb8d2013-03-01 16:35:12 -0500608 if (!list_empty(&pring->txq))
James Smart589a52d2010-07-14 15:30:54 -0400609 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -0500610 return;
611
612 }
613 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartda0436e2009-05-22 14:51:39 -0400614}
615
616/**
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -0700617 * lpfc_sli4_post_scsi_sgl_list - Post blocks of scsi buffer sgls from a list
James Smart8a9d2e82012-05-09 21:16:12 -0400618 * @phba: pointer to lpfc hba data structure.
619 * @post_sblist: pointer to the scsi buffer list.
620 *
621 * This routine walks a list of scsi buffers that was passed in. It attempts
622 * to construct blocks of scsi buffer sgls which contains contiguous xris and
623 * uses the non-embedded SGL block post mailbox commands to post to the port.
624 * For single SCSI buffer sgl with non-contiguous xri, if any, it shall use
625 * embedded SGL post mailbox command for posting. The @post_sblist passed in
626 * must be local list, thus no lock is needed when manipulate the list.
627 *
628 * Returns: 0 = failure, non-zero number of successfully posted buffers.
629 **/
Rashika Kheria7bfe7812014-09-03 12:55:36 -0400630static int
James Smart8a9d2e82012-05-09 21:16:12 -0400631lpfc_sli4_post_scsi_sgl_list(struct lpfc_hba *phba,
632 struct list_head *post_sblist, int sb_count)
633{
634 struct lpfc_scsi_buf *psb, *psb_next;
James Smart96f70772013-04-17 20:16:15 -0400635 int status, sgl_size;
James Smart8a9d2e82012-05-09 21:16:12 -0400636 int post_cnt = 0, block_cnt = 0, num_posting = 0, num_posted = 0;
637 dma_addr_t pdma_phys_bpl1;
638 int last_xritag = NO_XRI;
639 LIST_HEAD(prep_sblist);
640 LIST_HEAD(blck_sblist);
641 LIST_HEAD(scsi_sblist);
642
643 /* sanity check */
644 if (sb_count <= 0)
645 return -EINVAL;
646
James Smart96f70772013-04-17 20:16:15 -0400647 sgl_size = phba->cfg_sg_dma_buf_size -
648 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
649
James Smart8a9d2e82012-05-09 21:16:12 -0400650 list_for_each_entry_safe(psb, psb_next, post_sblist, list) {
651 list_del_init(&psb->list);
652 block_cnt++;
653 if ((last_xritag != NO_XRI) &&
654 (psb->cur_iocbq.sli4_xritag != last_xritag + 1)) {
655 /* a hole in xri block, form a sgl posting block */
656 list_splice_init(&prep_sblist, &blck_sblist);
657 post_cnt = block_cnt - 1;
658 /* prepare list for next posting block */
659 list_add_tail(&psb->list, &prep_sblist);
660 block_cnt = 1;
661 } else {
662 /* prepare list for next posting block */
663 list_add_tail(&psb->list, &prep_sblist);
664 /* enough sgls for non-embed sgl mbox command */
665 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
666 list_splice_init(&prep_sblist, &blck_sblist);
667 post_cnt = block_cnt;
668 block_cnt = 0;
669 }
670 }
671 num_posting++;
672 last_xritag = psb->cur_iocbq.sli4_xritag;
673
674 /* end of repost sgl list condition for SCSI buffers */
675 if (num_posting == sb_count) {
676 if (post_cnt == 0) {
677 /* last sgl posting block */
678 list_splice_init(&prep_sblist, &blck_sblist);
679 post_cnt = block_cnt;
680 } else if (block_cnt == 1) {
681 /* last single sgl with non-contiguous xri */
James Smart96f70772013-04-17 20:16:15 -0400682 if (sgl_size > SGL_PAGE_SIZE)
James Smart8a9d2e82012-05-09 21:16:12 -0400683 pdma_phys_bpl1 = psb->dma_phys_bpl +
684 SGL_PAGE_SIZE;
685 else
686 pdma_phys_bpl1 = 0;
687 status = lpfc_sli4_post_sgl(phba,
688 psb->dma_phys_bpl,
689 pdma_phys_bpl1,
690 psb->cur_iocbq.sli4_xritag);
691 if (status) {
692 /* failure, put on abort scsi list */
693 psb->exch_busy = 1;
694 } else {
695 /* success, put on SCSI buffer list */
696 psb->exch_busy = 0;
697 psb->status = IOSTAT_SUCCESS;
698 num_posted++;
699 }
700 /* success, put on SCSI buffer sgl list */
701 list_add_tail(&psb->list, &scsi_sblist);
702 }
703 }
704
705 /* continue until a nembed page worth of sgls */
706 if (post_cnt == 0)
707 continue;
708
709 /* post block of SCSI buffer list sgls */
710 status = lpfc_sli4_post_scsi_sgl_block(phba, &blck_sblist,
711 post_cnt);
712
713 /* don't reset xirtag due to hole in xri block */
714 if (block_cnt == 0)
715 last_xritag = NO_XRI;
716
717 /* reset SCSI buffer post count for next round of posting */
718 post_cnt = 0;
719
720 /* put posted SCSI buffer-sgl posted on SCSI buffer sgl list */
721 while (!list_empty(&blck_sblist)) {
722 list_remove_head(&blck_sblist, psb,
723 struct lpfc_scsi_buf, list);
724 if (status) {
725 /* failure, put on abort scsi list */
726 psb->exch_busy = 1;
727 } else {
728 /* success, put on SCSI buffer list */
729 psb->exch_busy = 0;
730 psb->status = IOSTAT_SUCCESS;
731 num_posted++;
732 }
733 list_add_tail(&psb->list, &scsi_sblist);
734 }
735 }
736 /* Push SCSI buffers with sgl posted to the availble list */
737 while (!list_empty(&scsi_sblist)) {
738 list_remove_head(&scsi_sblist, psb,
739 struct lpfc_scsi_buf, list);
740 lpfc_release_scsi_buf_s4(phba, psb);
741 }
742 return num_posted;
743}
744
745/**
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -0700746 * lpfc_sli4_repost_scsi_sgl_list - Repost all the allocated scsi buffer sgls
James Smartda0436e2009-05-22 14:51:39 -0400747 * @phba: pointer to lpfc hba data structure.
748 *
749 * This routine walks the list of scsi buffers that have been allocated and
James Smart8a9d2e82012-05-09 21:16:12 -0400750 * repost them to the port by using SGL block post. This is needed after a
James Smartda0436e2009-05-22 14:51:39 -0400751 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
752 * is responsible for moving all scsi buffers on the lpfc_abts_scsi_sgl_list
753 * to the lpfc_scsi_buf_list. If the repost fails, reject all scsi buffers.
754 *
755 * Returns: 0 = success, non-zero failure.
756 **/
757int
758lpfc_sli4_repost_scsi_sgl_list(struct lpfc_hba *phba)
759{
James Smart8a9d2e82012-05-09 21:16:12 -0400760 LIST_HEAD(post_sblist);
761 int num_posted, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -0400762
James Smart8a9d2e82012-05-09 21:16:12 -0400763 /* get all SCSI buffers need to repost to a local list */
James Smarta40fc5f2013-04-17 20:17:40 -0400764 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart164cecd2013-09-06 12:22:38 -0400765 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -0400766 list_splice_init(&phba->lpfc_scsi_buf_list_get, &post_sblist);
767 list_splice(&phba->lpfc_scsi_buf_list_put, &post_sblist);
James Smart164cecd2013-09-06 12:22:38 -0400768 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -0400769 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smartda0436e2009-05-22 14:51:39 -0400770
James Smart8a9d2e82012-05-09 21:16:12 -0400771 /* post the list of scsi buffer sgls to port if available */
772 if (!list_empty(&post_sblist)) {
773 num_posted = lpfc_sli4_post_scsi_sgl_list(phba, &post_sblist,
774 phba->sli4_hba.scsi_xri_cnt);
775 /* failed to post any scsi buffer, return error */
776 if (num_posted == 0)
777 rc = -EIO;
James Smartda0436e2009-05-22 14:51:39 -0400778 }
779 return rc;
780}
781
782/**
783 * lpfc_new_scsi_buf_s4 - Scsi buffer allocator for HBA with SLI4 IF spec
784 * @vport: The virtual port for which this call being executed.
785 * @num_to_allocate: The requested number of buffers to allocate.
786 *
James Smart8a9d2e82012-05-09 21:16:12 -0400787 * This routine allocates scsi buffers for device with SLI-4 interface spec,
James Smartda0436e2009-05-22 14:51:39 -0400788 * the scsi buffer contains all the necessary information needed to initiate
James Smart8a9d2e82012-05-09 21:16:12 -0400789 * a SCSI I/O. After allocating up to @num_to_allocate SCSI buffers and put
790 * them on a list, it post them to the port by using SGL block post.
James Smartda0436e2009-05-22 14:51:39 -0400791 *
792 * Return codes:
James Smart8a9d2e82012-05-09 21:16:12 -0400793 * int - number of scsi buffers that were allocated and posted.
James Smartda0436e2009-05-22 14:51:39 -0400794 * 0 = failure, less than num_to_alloc is a partial failure.
795 **/
796static int
797lpfc_new_scsi_buf_s4(struct lpfc_vport *vport, int num_to_alloc)
798{
799 struct lpfc_hba *phba = vport->phba;
800 struct lpfc_scsi_buf *psb;
801 struct sli4_sge *sgl;
802 IOCB_t *iocb;
803 dma_addr_t pdma_phys_fcp_cmd;
804 dma_addr_t pdma_phys_fcp_rsp;
James Smart96f70772013-04-17 20:16:15 -0400805 dma_addr_t pdma_phys_bpl;
James Smart8a9d2e82012-05-09 21:16:12 -0400806 uint16_t iotag, lxri = 0;
James Smart96f70772013-04-17 20:16:15 -0400807 int bcnt, num_posted, sgl_size;
James Smart8a9d2e82012-05-09 21:16:12 -0400808 LIST_HEAD(prep_sblist);
809 LIST_HEAD(post_sblist);
810 LIST_HEAD(scsi_sblist);
James Smartda0436e2009-05-22 14:51:39 -0400811
James Smart96f70772013-04-17 20:16:15 -0400812 sgl_size = phba->cfg_sg_dma_buf_size -
813 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
814
815 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
816 "9068 ALLOC %d scsi_bufs: %d (%d + %d + %d)\n",
817 num_to_alloc, phba->cfg_sg_dma_buf_size, sgl_size,
818 (int)sizeof(struct fcp_cmnd),
819 (int)sizeof(struct fcp_rsp));
820
James Smartda0436e2009-05-22 14:51:39 -0400821 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
822 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
823 if (!psb)
824 break;
James Smartda0436e2009-05-22 14:51:39 -0400825 /*
James Smart8a9d2e82012-05-09 21:16:12 -0400826 * Get memory from the pci pool to map the virt space to
827 * pci bus space for an I/O. The DMA buffer includes space
828 * for the struct fcp_cmnd, struct fcp_rsp and the number
829 * of bde's necessary to support the sg_tablesize.
James Smartda0436e2009-05-22 14:51:39 -0400830 */
Romain Perier771db5c2017-07-06 10:13:05 +0200831 psb->data = dma_pool_zalloc(phba->lpfc_sg_dma_buf_pool,
James Smartda0436e2009-05-22 14:51:39 -0400832 GFP_KERNEL, &psb->dma_handle);
833 if (!psb->data) {
834 kfree(psb);
835 break;
836 }
James Smartda0436e2009-05-22 14:51:39 -0400837
James Smart092cb032013-09-06 12:21:50 -0400838 /*
839 * 4K Page alignment is CRITICAL to BlockGuard, double check
840 * to be sure.
841 */
James Smartf44ac122018-03-05 12:04:08 -0800842 if ((phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
843 (((unsigned long)(psb->data) &
James Smart092cb032013-09-06 12:21:50 -0400844 (unsigned long)(SLI4_PAGE_SIZE - 1)) != 0)) {
James Smartf44ac122018-03-05 12:04:08 -0800845 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
846 "3369 Memory alignment error "
847 "addr=%lx\n",
848 (unsigned long)psb->data);
Romain Perier771db5c2017-07-06 10:13:05 +0200849 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
James Smart96f70772013-04-17 20:16:15 -0400850 psb->data, psb->dma_handle);
851 kfree(psb);
852 break;
853 }
854
James Smartda0436e2009-05-22 14:51:39 -0400855
James Smart6d368e52011-05-24 11:44:12 -0400856 lxri = lpfc_sli4_next_xritag(phba);
857 if (lxri == NO_XRI) {
Romain Perier771db5c2017-07-06 10:13:05 +0200858 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
James Smart895427b2017-02-12 13:52:30 -0800859 psb->data, psb->dma_handle);
James Smartda0436e2009-05-22 14:51:39 -0400860 kfree(psb);
861 break;
862 }
James Smartf7bc6432013-10-10 12:19:53 -0400863
864 /* Allocate iotag for psb->cur_iocbq. */
865 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
866 if (iotag == 0) {
Romain Perier771db5c2017-07-06 10:13:05 +0200867 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
James Smart895427b2017-02-12 13:52:30 -0800868 psb->data, psb->dma_handle);
James Smartf7bc6432013-10-10 12:19:53 -0400869 kfree(psb);
870 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -0700871 "3368 Failed to allocate IOTAG for"
James Smartf7bc6432013-10-10 12:19:53 -0400872 " XRI:0x%x\n", lxri);
873 lpfc_sli4_free_xri(phba, lxri);
874 break;
875 }
James Smart6d368e52011-05-24 11:44:12 -0400876 psb->cur_iocbq.sli4_lxritag = lxri;
877 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
James Smartda0436e2009-05-22 14:51:39 -0400878 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Smartda0436e2009-05-22 14:51:39 -0400879 psb->fcp_bpl = psb->data;
James Smart96f70772013-04-17 20:16:15 -0400880 psb->fcp_cmnd = (psb->data + sgl_size);
James Smartda0436e2009-05-22 14:51:39 -0400881 psb->fcp_rsp = (struct fcp_rsp *)((uint8_t *)psb->fcp_cmnd +
882 sizeof(struct fcp_cmnd));
883
884 /* Initialize local short-hand pointers. */
885 sgl = (struct sli4_sge *)psb->fcp_bpl;
886 pdma_phys_bpl = psb->dma_handle;
James Smart96f70772013-04-17 20:16:15 -0400887 pdma_phys_fcp_cmd = (psb->dma_handle + sgl_size);
James Smartda0436e2009-05-22 14:51:39 -0400888 pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
889
890 /*
James Smart8a9d2e82012-05-09 21:16:12 -0400891 * The first two bdes are the FCP_CMD and FCP_RSP.
892 * The balance are sg list bdes. Initialize the
893 * first two and leave the rest for queuecommand.
James Smartda0436e2009-05-22 14:51:39 -0400894 */
895 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
896 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
James Smart05580562011-05-24 11:40:48 -0400897 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -0400898 bf_set(lpfc_sli4_sge_last, sgl, 0);
899 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -0500900 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_cmnd));
James Smartda0436e2009-05-22 14:51:39 -0400901 sgl++;
902
903 /* Setup the physical region for the FCP RSP */
904 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
905 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
James Smart05580562011-05-24 11:40:48 -0400906 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -0400907 bf_set(lpfc_sli4_sge_last, sgl, 1);
908 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -0500909 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
James Smartda0436e2009-05-22 14:51:39 -0400910
911 /*
912 * Since the IOCB for the FCP I/O is built into this
913 * lpfc_scsi_buf, initialize it with all known data now.
914 */
915 iocb = &psb->cur_iocbq.iocb;
916 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
917 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
918 /* setting the BLP size to 2 * sizeof BDE may not be correct.
919 * We are setting the bpl to point to out sgl. An sgl's
920 * entries are 16 bytes, a bpl entries are 12 bytes.
921 */
922 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
923 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
924 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
925 iocb->ulpBdeCount = 1;
926 iocb->ulpLe = 1;
927 iocb->ulpClass = CLASS3;
James Smart8a9d2e82012-05-09 21:16:12 -0400928 psb->cur_iocbq.context1 = psb;
James Smartda0436e2009-05-22 14:51:39 -0400929 psb->dma_phys_bpl = pdma_phys_bpl;
James Smart6d368e52011-05-24 11:44:12 -0400930
James Smart8a9d2e82012-05-09 21:16:12 -0400931 /* add the scsi buffer to a post list */
932 list_add_tail(&psb->list, &post_sblist);
James Smarta40fc5f2013-04-17 20:17:40 -0400933 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -0400934 phba->sli4_hba.scsi_xri_cnt++;
James Smarta40fc5f2013-04-17 20:17:40 -0400935 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smartda0436e2009-05-22 14:51:39 -0400936 }
James Smart895427b2017-02-12 13:52:30 -0800937 lpfc_printf_log(phba, KERN_INFO, LOG_BG | LOG_FCP,
James Smart8a9d2e82012-05-09 21:16:12 -0400938 "3021 Allocate %d out of %d requested new SCSI "
939 "buffers\n", bcnt, num_to_alloc);
James Smartda0436e2009-05-22 14:51:39 -0400940
James Smart8a9d2e82012-05-09 21:16:12 -0400941 /* post the list of scsi buffer sgls to port if available */
942 if (!list_empty(&post_sblist))
943 num_posted = lpfc_sli4_post_scsi_sgl_list(phba,
944 &post_sblist, bcnt);
945 else
946 num_posted = 0;
947
948 return num_posted;
James Smartda0436e2009-05-22 14:51:39 -0400949}
950
951/**
James Smart3772a992009-05-22 14:50:54 -0400952 * lpfc_new_scsi_buf - Wrapper funciton for scsi buffer allocator
953 * @vport: The virtual port for which this call being executed.
954 * @num_to_allocate: The requested number of buffers to allocate.
955 *
956 * This routine wraps the actual SCSI buffer allocator function pointer from
957 * the lpfc_hba struct.
958 *
959 * Return codes:
960 * int - number of scsi buffers that were allocated.
961 * 0 = failure, less than num_to_alloc is a partial failure.
962 **/
963static inline int
964lpfc_new_scsi_buf(struct lpfc_vport *vport, int num_to_alloc)
965{
966 return vport->phba->lpfc_new_scsi_buf(vport, num_to_alloc);
967}
968
969/**
James Smart19ca7602010-11-20 23:11:55 -0500970 * lpfc_get_scsi_buf_s3 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
James Smart3772a992009-05-22 14:50:54 -0400971 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -0500972 *
973 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
974 * and returns to caller.
975 *
976 * Return codes:
977 * NULL - Error
978 * Pointer to lpfc_scsi_buf - Success
979 **/
Adrian Bunk455c53e2006-01-06 20:21:28 +0100980static struct lpfc_scsi_buf*
James Smart19ca7602010-11-20 23:11:55 -0500981lpfc_get_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -0500982{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400983 struct lpfc_scsi_buf * lpfc_cmd = NULL;
James Smarta40fc5f2013-04-17 20:17:40 -0400984 struct list_head *scsi_buf_list_get = &phba->lpfc_scsi_buf_list_get;
James Smart164cecd2013-09-06 12:22:38 -0400985 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500986
James Smart164cecd2013-09-06 12:22:38 -0400987 spin_lock_irqsave(&phba->scsi_buf_list_get_lock, iflag);
James Smarta40fc5f2013-04-17 20:17:40 -0400988 list_remove_head(scsi_buf_list_get, lpfc_cmd, struct lpfc_scsi_buf,
989 list);
990 if (!lpfc_cmd) {
James Smart164cecd2013-09-06 12:22:38 -0400991 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -0400992 list_splice(&phba->lpfc_scsi_buf_list_put,
993 &phba->lpfc_scsi_buf_list_get);
994 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
995 list_remove_head(scsi_buf_list_get, lpfc_cmd,
996 struct lpfc_scsi_buf, list);
James Smart164cecd2013-09-06 12:22:38 -0400997 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smart1dcb58e2007-04-25 09:51:30 -0400998 }
James Smart164cecd2013-09-06 12:22:38 -0400999 spin_unlock_irqrestore(&phba->scsi_buf_list_get_lock, iflag);
James Smart2a5b7d62018-07-31 17:23:22 -07001000
1001 if (lpfc_ndlp_check_qdepth(phba, ndlp) && lpfc_cmd) {
1002 atomic_inc(&ndlp->cmd_pending);
1003 lpfc_cmd->flags |= LPFC_SBUF_BUMP_QDEPTH;
1004 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001005 return lpfc_cmd;
1006}
James Smart19ca7602010-11-20 23:11:55 -05001007/**
1008 * lpfc_get_scsi_buf_s4 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1009 * @phba: The HBA for which this call is being executed.
1010 *
1011 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1012 * and returns to caller.
1013 *
1014 * Return codes:
1015 * NULL - Error
1016 * Pointer to lpfc_scsi_buf - Success
1017 **/
1018static struct lpfc_scsi_buf*
1019lpfc_get_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1020{
James Smart3be30e02013-05-31 17:05:17 -04001021 struct lpfc_scsi_buf *lpfc_cmd, *lpfc_cmd_next;
James Smart164cecd2013-09-06 12:22:38 -04001022 unsigned long iflag = 0;
James Smart19ca7602010-11-20 23:11:55 -05001023 int found = 0;
1024
James Smart164cecd2013-09-06 12:22:38 -04001025 spin_lock_irqsave(&phba->scsi_buf_list_get_lock, iflag);
James Smart3be30e02013-05-31 17:05:17 -04001026 list_for_each_entry_safe(lpfc_cmd, lpfc_cmd_next,
1027 &phba->lpfc_scsi_buf_list_get, list) {
James Smart19ca7602010-11-20 23:11:55 -05001028 if (lpfc_test_rrq_active(phba, ndlp,
James Smartee0f4fe2012-05-09 21:19:14 -04001029 lpfc_cmd->cur_iocbq.sli4_lxritag))
James Smart1151e3e2011-02-16 12:39:35 -05001030 continue;
James Smart59c68ea2018-04-09 14:24:25 -07001031 list_del_init(&lpfc_cmd->list);
James Smart19ca7602010-11-20 23:11:55 -05001032 found = 1;
James Smart1151e3e2011-02-16 12:39:35 -05001033 break;
James Smart19ca7602010-11-20 23:11:55 -05001034 }
James Smarta40fc5f2013-04-17 20:17:40 -04001035 if (!found) {
James Smart164cecd2013-09-06 12:22:38 -04001036 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04001037 list_splice(&phba->lpfc_scsi_buf_list_put,
1038 &phba->lpfc_scsi_buf_list_get);
1039 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
James Smart164cecd2013-09-06 12:22:38 -04001040 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smart3be30e02013-05-31 17:05:17 -04001041 list_for_each_entry_safe(lpfc_cmd, lpfc_cmd_next,
1042 &phba->lpfc_scsi_buf_list_get, list) {
James Smarta40fc5f2013-04-17 20:17:40 -04001043 if (lpfc_test_rrq_active(
1044 phba, ndlp, lpfc_cmd->cur_iocbq.sli4_lxritag))
1045 continue;
James Smart59c68ea2018-04-09 14:24:25 -07001046 list_del_init(&lpfc_cmd->list);
James Smarta40fc5f2013-04-17 20:17:40 -04001047 found = 1;
1048 break;
1049 }
1050 }
James Smart164cecd2013-09-06 12:22:38 -04001051 spin_unlock_irqrestore(&phba->scsi_buf_list_get_lock, iflag);
James Smart1151e3e2011-02-16 12:39:35 -05001052 if (!found)
1053 return NULL;
James Smart2a5b7d62018-07-31 17:23:22 -07001054
Dan Carpenter26c724a2018-08-23 16:56:33 +03001055 if (lpfc_ndlp_check_qdepth(phba, ndlp)) {
James Smart2a5b7d62018-07-31 17:23:22 -07001056 atomic_inc(&ndlp->cmd_pending);
1057 lpfc_cmd->flags |= LPFC_SBUF_BUMP_QDEPTH;
1058 }
James Smarta40fc5f2013-04-17 20:17:40 -04001059 return lpfc_cmd;
James Smart19ca7602010-11-20 23:11:55 -05001060}
1061/**
1062 * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1063 * @phba: The HBA for which this call is being executed.
1064 *
1065 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1066 * and returns to caller.
1067 *
1068 * Return codes:
1069 * NULL - Error
1070 * Pointer to lpfc_scsi_buf - Success
1071 **/
1072static struct lpfc_scsi_buf*
1073lpfc_get_scsi_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1074{
1075 return phba->lpfc_get_scsi_buf(phba, ndlp);
1076}
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001077
James Smart9bad7672008-12-04 22:39:02 -05001078/**
James Smart3772a992009-05-22 14:50:54 -04001079 * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
James Smart9bad7672008-12-04 22:39:02 -05001080 * @phba: The Hba for which this call is being executed.
1081 * @psb: The scsi buffer which is being released.
1082 *
1083 * This routine releases @psb scsi buffer by adding it to tail of @phba
1084 * lpfc_scsi_buf_list list.
1085 **/
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001086static void
James Smart3772a992009-05-22 14:50:54 -04001087lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001088{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001089 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -05001090
James Smarta40fc5f2013-04-17 20:17:40 -04001091 psb->seg_cnt = 0;
1092 psb->nonsg_phys = 0;
1093 psb->prot_seg_cnt = 0;
1094
1095 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001096 psb->pCmd = NULL;
James Smart6a485eb2013-04-17 20:19:00 -04001097 psb->cur_iocbq.iocb_flag = LPFC_IO_FCP;
James Smarta40fc5f2013-04-17 20:17:40 -04001098 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list_put);
1099 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
dea31012005-04-17 16:05:31 -05001100}
1101
James Smart9bad7672008-12-04 22:39:02 -05001102/**
James Smartda0436e2009-05-22 14:51:39 -04001103 * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
1104 * @phba: The Hba for which this call is being executed.
1105 * @psb: The scsi buffer which is being released.
1106 *
1107 * This routine releases @psb scsi buffer by adding it to tail of @phba
1108 * lpfc_scsi_buf_list list. For SLI4 XRI's are tied to the scsi buffer
1109 * and cannot be reused for at least RA_TOV amount of time if it was
1110 * aborted.
1111 **/
1112static void
1113lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1114{
1115 unsigned long iflag = 0;
1116
James Smarta40fc5f2013-04-17 20:17:40 -04001117 psb->seg_cnt = 0;
1118 psb->nonsg_phys = 0;
1119 psb->prot_seg_cnt = 0;
1120
James Smart341af102010-01-26 23:07:37 -05001121 if (psb->exch_busy) {
James Smartda0436e2009-05-22 14:51:39 -04001122 spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock,
1123 iflag);
1124 psb->pCmd = NULL;
1125 list_add_tail(&psb->list,
1126 &phba->sli4_hba.lpfc_abts_scsi_buf_list);
1127 spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
1128 iflag);
1129 } else {
James Smartda0436e2009-05-22 14:51:39 -04001130 psb->pCmd = NULL;
James Smart6a485eb2013-04-17 20:19:00 -04001131 psb->cur_iocbq.iocb_flag = LPFC_IO_FCP;
James Smarta40fc5f2013-04-17 20:17:40 -04001132 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
1133 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list_put);
1134 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
James Smartda0436e2009-05-22 14:51:39 -04001135 }
1136}
1137
1138/**
James Smart3772a992009-05-22 14:50:54 -04001139 * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
1140 * @phba: The Hba for which this call is being executed.
1141 * @psb: The scsi buffer which is being released.
1142 *
1143 * This routine releases @psb scsi buffer by adding it to tail of @phba
1144 * lpfc_scsi_buf_list list.
1145 **/
1146static void
1147lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1148{
James Smart2a5b7d62018-07-31 17:23:22 -07001149 if ((psb->flags & LPFC_SBUF_BUMP_QDEPTH) && psb->ndlp)
1150 atomic_dec(&psb->ndlp->cmd_pending);
James Smart3772a992009-05-22 14:50:54 -04001151
James Smart2a5b7d62018-07-31 17:23:22 -07001152 psb->flags &= ~LPFC_SBUF_BUMP_QDEPTH;
James Smart3772a992009-05-22 14:50:54 -04001153 phba->lpfc_release_scsi_buf(phba, psb);
1154}
1155
1156/**
1157 * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -05001158 * @phba: The Hba for which this call is being executed.
1159 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1160 *
1161 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
James Smart3772a992009-05-22 14:50:54 -04001162 * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -07001163 * through sg elements and format the bde. This routine also initializes all
James Smart3772a992009-05-22 14:50:54 -04001164 * IOCB fields which are dependent on scsi command request buffer.
James Smart9bad7672008-12-04 22:39:02 -05001165 *
1166 * Return codes:
1167 * 1 - Error
1168 * 0 - Success
1169 **/
dea31012005-04-17 16:05:31 -05001170static int
James Smart3772a992009-05-22 14:50:54 -04001171lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
dea31012005-04-17 16:05:31 -05001172{
1173 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1174 struct scatterlist *sgel = NULL;
1175 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1176 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
James Smart0f65ff62010-02-26 14:14:23 -05001177 struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
dea31012005-04-17 16:05:31 -05001178 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart34b02dc2008-08-24 21:49:55 -04001179 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
dea31012005-04-17 16:05:31 -05001180 dma_addr_t physaddr;
James Smart34b02dc2008-08-24 21:49:55 -04001181 uint32_t num_bde = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001182 int nseg, datadir = scsi_cmnd->sc_data_direction;
dea31012005-04-17 16:05:31 -05001183
1184 /*
1185 * There are three possibilities here - use scatter-gather segment, use
1186 * the single mapping, or neither. Start the lpfc command prep by
1187 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1188 * data bde entry.
1189 */
1190 bpl += 2;
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001191 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05001192 /*
1193 * The driver stores the segment count returned from pci_map_sg
1194 * because this a count of dma-mappings used to map the use_sg
1195 * pages. They are not guaranteed to be the same for those
1196 * architectures that implement an IOMMU.
1197 */
dea31012005-04-17 16:05:31 -05001198
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001199 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
1200 scsi_sg_count(scsi_cmnd), datadir);
1201 if (unlikely(!nseg))
1202 return 1;
1203
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001204 lpfc_cmd->seg_cnt = nseg;
dea31012005-04-17 16:05:31 -05001205 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001206 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1207 "9064 BLKGRD: %s: Too many sg segments from "
James Smarte2a0a9d2008-12-04 22:40:02 -05001208 "dma_map_sg. Config %d, seg_cnt %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001209 __func__, phba->cfg_sg_seg_cnt,
dea31012005-04-17 16:05:31 -05001210 lpfc_cmd->seg_cnt);
James Smart96f70772013-04-17 20:16:15 -04001211 lpfc_cmd->seg_cnt = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001212 scsi_dma_unmap(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001213 return 1;
1214 }
1215
1216 /*
1217 * The driver established a maximum scatter-gather segment count
1218 * during probe that limits the number of sg elements in any
1219 * single scsi command. Just run through the seg_cnt and format
1220 * the bde's.
James Smart34b02dc2008-08-24 21:49:55 -04001221 * When using SLI-3 the driver will try to fit all the BDEs into
1222 * the IOCB. If it can't then the BDEs get added to a BPL as it
1223 * does for SLI-2 mode.
dea31012005-04-17 16:05:31 -05001224 */
James Smart34b02dc2008-08-24 21:49:55 -04001225 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
dea31012005-04-17 16:05:31 -05001226 physaddr = sg_dma_address(sgel);
James Smart34b02dc2008-08-24 21:49:55 -04001227 if (phba->sli_rev == 3 &&
James Smarte2a0a9d2008-12-04 22:40:02 -05001228 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
James Smart0f65ff62010-02-26 14:14:23 -05001229 !(iocbq->iocb_flag & DSS_SECURITY_OP) &&
James Smart34b02dc2008-08-24 21:49:55 -04001230 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
1231 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1232 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
1233 data_bde->addrLow = putPaddrLow(physaddr);
1234 data_bde->addrHigh = putPaddrHigh(physaddr);
1235 data_bde++;
1236 } else {
1237 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1238 bpl->tus.f.bdeSize = sg_dma_len(sgel);
1239 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1240 bpl->addrLow =
1241 le32_to_cpu(putPaddrLow(physaddr));
1242 bpl->addrHigh =
1243 le32_to_cpu(putPaddrHigh(physaddr));
1244 bpl++;
1245 }
dea31012005-04-17 16:05:31 -05001246 }
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001247 }
dea31012005-04-17 16:05:31 -05001248
1249 /*
1250 * Finish initializing those IOCB fields that are dependent on the
James Smart34b02dc2008-08-24 21:49:55 -04001251 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
1252 * explicitly reinitialized and for SLI-3 the extended bde count is
1253 * explicitly reinitialized since all iocb memory resources are reused.
dea31012005-04-17 16:05:31 -05001254 */
James Smarte2a0a9d2008-12-04 22:40:02 -05001255 if (phba->sli_rev == 3 &&
James Smart0f65ff62010-02-26 14:14:23 -05001256 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
1257 !(iocbq->iocb_flag & DSS_SECURITY_OP)) {
James Smart34b02dc2008-08-24 21:49:55 -04001258 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
1259 /*
1260 * The extended IOCB format can only fit 3 BDE or a BPL.
1261 * This I/O has more than 3 BDE so the 1st data bde will
1262 * be a BPL that is filled in here.
1263 */
1264 physaddr = lpfc_cmd->dma_handle;
1265 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
1266 data_bde->tus.f.bdeSize = (num_bde *
1267 sizeof(struct ulp_bde64));
1268 physaddr += (sizeof(struct fcp_cmnd) +
1269 sizeof(struct fcp_rsp) +
1270 (2 * sizeof(struct ulp_bde64)));
1271 data_bde->addrHigh = putPaddrHigh(physaddr);
1272 data_bde->addrLow = putPaddrLow(physaddr);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001273 /* ebde count includes the response bde and data bpl */
James Smart34b02dc2008-08-24 21:49:55 -04001274 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
1275 } else {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001276 /* ebde count includes the response bde and data bdes */
James Smart34b02dc2008-08-24 21:49:55 -04001277 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1278 }
1279 } else {
1280 iocb_cmd->un.fcpi64.bdl.bdeSize =
1281 ((num_bde + 2) * sizeof(struct ulp_bde64));
James Smart0f65ff62010-02-26 14:14:23 -05001282 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
James Smart34b02dc2008-08-24 21:49:55 -04001283 }
James Smart09372822008-01-11 01:52:54 -05001284 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
James Smarte2a0a9d2008-12-04 22:40:02 -05001285
1286 /*
1287 * Due to difference in data length between DIF/non-DIF paths,
1288 * we need to set word 4 of IOCB here
1289 */
James Smarta257bf92009-04-06 18:48:10 -04001290 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001291 return 0;
1292}
1293
James Smartf9bb2da2011-10-10 21:34:11 -04001294#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001295
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -07001296/* Return BG_ERR_INIT if error injection is detected by Initiator */
James Smart9a6b09c2012-03-01 22:37:42 -05001297#define BG_ERR_INIT 0x1
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -07001298/* Return BG_ERR_TGT if error injection is detected by Target */
James Smart9a6b09c2012-03-01 22:37:42 -05001299#define BG_ERR_TGT 0x2
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -07001300/* Return BG_ERR_SWAP if swapping CSUM<-->CRC is required for error injection */
James Smart9a6b09c2012-03-01 22:37:42 -05001301#define BG_ERR_SWAP 0x10
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -07001302/**
1303 * Return BG_ERR_CHECK if disabling Guard/Ref/App checking is required for
1304 * error injection
1305 **/
James Smart9a6b09c2012-03-01 22:37:42 -05001306#define BG_ERR_CHECK 0x20
James Smartacd68592012-01-18 16:25:09 -05001307
1308/**
1309 * lpfc_bg_err_inject - Determine if we should inject an error
1310 * @phba: The Hba for which this call is being executed.
James Smartf9bb2da2011-10-10 21:34:11 -04001311 * @sc: The SCSI command to examine
1312 * @reftag: (out) BlockGuard reference tag for transmitted data
1313 * @apptag: (out) BlockGuard application tag for transmitted data
1314 * @new_guard (in) Value to replace CRC with if needed
1315 *
James Smart9a6b09c2012-03-01 22:37:42 -05001316 * Returns BG_ERR_* bit mask or 0 if request ignored
James Smartacd68592012-01-18 16:25:09 -05001317 **/
James Smartf9bb2da2011-10-10 21:34:11 -04001318static int
1319lpfc_bg_err_inject(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1320 uint32_t *reftag, uint16_t *apptag, uint32_t new_guard)
1321{
1322 struct scatterlist *sgpe; /* s/g prot entry */
James Smart9a6b09c2012-03-01 22:37:42 -05001323 struct lpfc_scsi_buf *lpfc_cmd = NULL;
James Smartacd68592012-01-18 16:25:09 -05001324 struct scsi_dif_tuple *src = NULL;
James Smart4ac9b222012-03-01 22:38:29 -05001325 struct lpfc_nodelist *ndlp;
1326 struct lpfc_rport_data *rdata;
James Smartf9bb2da2011-10-10 21:34:11 -04001327 uint32_t op = scsi_get_prot_op(sc);
1328 uint32_t blksize;
1329 uint32_t numblks;
1330 sector_t lba;
1331 int rc = 0;
James Smartacd68592012-01-18 16:25:09 -05001332 int blockoff = 0;
James Smartf9bb2da2011-10-10 21:34:11 -04001333
1334 if (op == SCSI_PROT_NORMAL)
1335 return 0;
1336
James Smartacd68592012-01-18 16:25:09 -05001337 sgpe = scsi_prot_sglist(sc);
James Smartf9bb2da2011-10-10 21:34:11 -04001338 lba = scsi_get_lba(sc);
James Smart4ac9b222012-03-01 22:38:29 -05001339
1340 /* First check if we need to match the LBA */
James Smartf9bb2da2011-10-10 21:34:11 -04001341 if (phba->lpfc_injerr_lba != LPFC_INJERR_LBA_OFF) {
1342 blksize = lpfc_cmd_blksize(sc);
1343 numblks = (scsi_bufflen(sc) + blksize - 1) / blksize;
1344
1345 /* Make sure we have the right LBA if one is specified */
1346 if ((phba->lpfc_injerr_lba < lba) ||
1347 (phba->lpfc_injerr_lba >= (lba + numblks)))
1348 return 0;
James Smartacd68592012-01-18 16:25:09 -05001349 if (sgpe) {
1350 blockoff = phba->lpfc_injerr_lba - lba;
1351 numblks = sg_dma_len(sgpe) /
1352 sizeof(struct scsi_dif_tuple);
1353 if (numblks < blockoff)
1354 blockoff = numblks;
James Smartacd68592012-01-18 16:25:09 -05001355 }
James Smartf9bb2da2011-10-10 21:34:11 -04001356 }
1357
James Smart4ac9b222012-03-01 22:38:29 -05001358 /* Next check if we need to match the remote NPortID or WWPN */
James Smart1ba981f2014-02-20 09:56:45 -05001359 rdata = lpfc_rport_data_from_scsi_device(sc->device);
James Smart4ac9b222012-03-01 22:38:29 -05001360 if (rdata && rdata->pnode) {
1361 ndlp = rdata->pnode;
1362
1363 /* Make sure we have the right NPortID if one is specified */
1364 if (phba->lpfc_injerr_nportid &&
1365 (phba->lpfc_injerr_nportid != ndlp->nlp_DID))
1366 return 0;
1367
1368 /*
1369 * Make sure we have the right WWPN if one is specified.
1370 * wwn[0] should be a non-zero NAA in a good WWPN.
1371 */
1372 if (phba->lpfc_injerr_wwpn.u.wwn[0] &&
1373 (memcmp(&ndlp->nlp_portname, &phba->lpfc_injerr_wwpn,
1374 sizeof(struct lpfc_name)) != 0))
1375 return 0;
1376 }
1377
1378 /* Setup a ptr to the protection data if the SCSI host provides it */
1379 if (sgpe) {
1380 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
1381 src += blockoff;
1382 lpfc_cmd = (struct lpfc_scsi_buf *)sc->host_scribble;
1383 }
1384
James Smartf9bb2da2011-10-10 21:34:11 -04001385 /* Should we change the Reference Tag */
1386 if (reftag) {
James Smartacd68592012-01-18 16:25:09 -05001387 if (phba->lpfc_injerr_wref_cnt) {
1388 switch (op) {
1389 case SCSI_PROT_WRITE_PASS:
James Smart9a6b09c2012-03-01 22:37:42 -05001390 if (src) {
1391 /*
1392 * For WRITE_PASS, force the error
1393 * to be sent on the wire. It should
1394 * be detected by the Target.
1395 * If blockoff != 0 error will be
1396 * inserted in middle of the IO.
1397 */
James Smartf9bb2da2011-10-10 21:34:11 -04001398
James Smartacd68592012-01-18 16:25:09 -05001399 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1400 "9076 BLKGRD: Injecting reftag error: "
1401 "write lba x%lx + x%x oldrefTag x%x\n",
1402 (unsigned long)lba, blockoff,
James Smart9a6b09c2012-03-01 22:37:42 -05001403 be32_to_cpu(src->ref_tag));
James Smartacd68592012-01-18 16:25:09 -05001404
1405 /*
James Smart9a6b09c2012-03-01 22:37:42 -05001406 * Save the old ref_tag so we can
1407 * restore it on completion.
James Smartacd68592012-01-18 16:25:09 -05001408 */
James Smart9a6b09c2012-03-01 22:37:42 -05001409 if (lpfc_cmd) {
1410 lpfc_cmd->prot_data_type =
1411 LPFC_INJERR_REFTAG;
1412 lpfc_cmd->prot_data_segment =
1413 src;
1414 lpfc_cmd->prot_data =
1415 src->ref_tag;
1416 }
1417 src->ref_tag = cpu_to_be32(0xDEADBEEF);
James Smartacd68592012-01-18 16:25:09 -05001418 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001419 if (phba->lpfc_injerr_wref_cnt == 0) {
1420 phba->lpfc_injerr_nportid = 0;
1421 phba->lpfc_injerr_lba =
1422 LPFC_INJERR_LBA_OFF;
1423 memset(&phba->lpfc_injerr_wwpn,
1424 0, sizeof(struct lpfc_name));
1425 }
James Smart9a6b09c2012-03-01 22:37:42 -05001426 rc = BG_ERR_TGT | BG_ERR_CHECK;
1427
James Smartacd68592012-01-18 16:25:09 -05001428 break;
1429 }
1430 /* Drop thru */
James Smart9a6b09c2012-03-01 22:37:42 -05001431 case SCSI_PROT_WRITE_INSERT:
1432 /*
1433 * For WRITE_INSERT, force the error
1434 * to be sent on the wire. It should be
1435 * detected by the Target.
1436 */
1437 /* DEADBEEF will be the reftag on the wire */
1438 *reftag = 0xDEADBEEF;
1439 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001440 if (phba->lpfc_injerr_wref_cnt == 0) {
1441 phba->lpfc_injerr_nportid = 0;
1442 phba->lpfc_injerr_lba =
1443 LPFC_INJERR_LBA_OFF;
1444 memset(&phba->lpfc_injerr_wwpn,
1445 0, sizeof(struct lpfc_name));
1446 }
James Smart9a6b09c2012-03-01 22:37:42 -05001447 rc = BG_ERR_TGT | BG_ERR_CHECK;
1448
1449 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1450 "9078 BLKGRD: Injecting reftag error: "
1451 "write lba x%lx\n", (unsigned long)lba);
1452 break;
James Smartacd68592012-01-18 16:25:09 -05001453 case SCSI_PROT_WRITE_STRIP:
1454 /*
1455 * For WRITE_STRIP and WRITE_PASS,
1456 * force the error on data
1457 * being copied from SLI-Host to SLI-Port.
1458 */
1459 *reftag = 0xDEADBEEF;
1460 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001461 if (phba->lpfc_injerr_wref_cnt == 0) {
1462 phba->lpfc_injerr_nportid = 0;
1463 phba->lpfc_injerr_lba =
1464 LPFC_INJERR_LBA_OFF;
1465 memset(&phba->lpfc_injerr_wwpn,
1466 0, sizeof(struct lpfc_name));
1467 }
James Smartacd68592012-01-18 16:25:09 -05001468 rc = BG_ERR_INIT;
1469
1470 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1471 "9077 BLKGRD: Injecting reftag error: "
1472 "write lba x%lx\n", (unsigned long)lba);
1473 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001474 }
James Smartacd68592012-01-18 16:25:09 -05001475 }
1476 if (phba->lpfc_injerr_rref_cnt) {
1477 switch (op) {
1478 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001479 case SCSI_PROT_READ_STRIP:
1480 case SCSI_PROT_READ_PASS:
1481 /*
1482 * For READ_STRIP and READ_PASS, force the
1483 * error on data being read off the wire. It
1484 * should force an IO error to the driver.
1485 */
James Smartf9bb2da2011-10-10 21:34:11 -04001486 *reftag = 0xDEADBEEF;
1487 phba->lpfc_injerr_rref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001488 if (phba->lpfc_injerr_rref_cnt == 0) {
1489 phba->lpfc_injerr_nportid = 0;
1490 phba->lpfc_injerr_lba =
1491 LPFC_INJERR_LBA_OFF;
1492 memset(&phba->lpfc_injerr_wwpn,
1493 0, sizeof(struct lpfc_name));
1494 }
James Smartacd68592012-01-18 16:25:09 -05001495 rc = BG_ERR_INIT;
James Smartf9bb2da2011-10-10 21:34:11 -04001496
1497 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smartacd68592012-01-18 16:25:09 -05001498 "9079 BLKGRD: Injecting reftag error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001499 "read lba x%lx\n", (unsigned long)lba);
James Smartacd68592012-01-18 16:25:09 -05001500 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001501 }
1502 }
1503 }
1504
1505 /* Should we change the Application Tag */
1506 if (apptag) {
James Smartacd68592012-01-18 16:25:09 -05001507 if (phba->lpfc_injerr_wapp_cnt) {
1508 switch (op) {
1509 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001510 if (src) {
James Smart9a6b09c2012-03-01 22:37:42 -05001511 /*
1512 * For WRITE_PASS, force the error
1513 * to be sent on the wire. It should
1514 * be detected by the Target.
1515 * If blockoff != 0 error will be
1516 * inserted in middle of the IO.
1517 */
1518
James Smartacd68592012-01-18 16:25:09 -05001519 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1520 "9080 BLKGRD: Injecting apptag error: "
1521 "write lba x%lx + x%x oldappTag x%x\n",
1522 (unsigned long)lba, blockoff,
James Smart9a6b09c2012-03-01 22:37:42 -05001523 be16_to_cpu(src->app_tag));
James Smartacd68592012-01-18 16:25:09 -05001524
1525 /*
James Smart9a6b09c2012-03-01 22:37:42 -05001526 * Save the old app_tag so we can
1527 * restore it on completion.
James Smartacd68592012-01-18 16:25:09 -05001528 */
James Smart9a6b09c2012-03-01 22:37:42 -05001529 if (lpfc_cmd) {
1530 lpfc_cmd->prot_data_type =
1531 LPFC_INJERR_APPTAG;
1532 lpfc_cmd->prot_data_segment =
1533 src;
1534 lpfc_cmd->prot_data =
1535 src->app_tag;
1536 }
1537 src->app_tag = cpu_to_be16(0xDEAD);
James Smartacd68592012-01-18 16:25:09 -05001538 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001539 if (phba->lpfc_injerr_wapp_cnt == 0) {
1540 phba->lpfc_injerr_nportid = 0;
1541 phba->lpfc_injerr_lba =
1542 LPFC_INJERR_LBA_OFF;
1543 memset(&phba->lpfc_injerr_wwpn,
1544 0, sizeof(struct lpfc_name));
1545 }
James Smart9a6b09c2012-03-01 22:37:42 -05001546 rc = BG_ERR_TGT | BG_ERR_CHECK;
James Smartacd68592012-01-18 16:25:09 -05001547 break;
1548 }
1549 /* Drop thru */
James Smart9a6b09c2012-03-01 22:37:42 -05001550 case SCSI_PROT_WRITE_INSERT:
1551 /*
1552 * For WRITE_INSERT, force the
1553 * error to be sent on the wire. It should be
1554 * detected by the Target.
1555 */
1556 /* DEAD will be the apptag on the wire */
1557 *apptag = 0xDEAD;
1558 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001559 if (phba->lpfc_injerr_wapp_cnt == 0) {
1560 phba->lpfc_injerr_nportid = 0;
1561 phba->lpfc_injerr_lba =
1562 LPFC_INJERR_LBA_OFF;
1563 memset(&phba->lpfc_injerr_wwpn,
1564 0, sizeof(struct lpfc_name));
1565 }
James Smart9a6b09c2012-03-01 22:37:42 -05001566 rc = BG_ERR_TGT | BG_ERR_CHECK;
1567
1568 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1569 "0813 BLKGRD: Injecting apptag error: "
1570 "write lba x%lx\n", (unsigned long)lba);
1571 break;
James Smartacd68592012-01-18 16:25:09 -05001572 case SCSI_PROT_WRITE_STRIP:
1573 /*
1574 * For WRITE_STRIP and WRITE_PASS,
1575 * force the error on data
1576 * being copied from SLI-Host to SLI-Port.
1577 */
1578 *apptag = 0xDEAD;
1579 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001580 if (phba->lpfc_injerr_wapp_cnt == 0) {
1581 phba->lpfc_injerr_nportid = 0;
1582 phba->lpfc_injerr_lba =
1583 LPFC_INJERR_LBA_OFF;
1584 memset(&phba->lpfc_injerr_wwpn,
1585 0, sizeof(struct lpfc_name));
1586 }
James Smartacd68592012-01-18 16:25:09 -05001587 rc = BG_ERR_INIT;
1588
1589 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1590 "0812 BLKGRD: Injecting apptag error: "
1591 "write lba x%lx\n", (unsigned long)lba);
1592 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001593 }
James Smartacd68592012-01-18 16:25:09 -05001594 }
1595 if (phba->lpfc_injerr_rapp_cnt) {
1596 switch (op) {
1597 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001598 case SCSI_PROT_READ_STRIP:
1599 case SCSI_PROT_READ_PASS:
1600 /*
1601 * For READ_STRIP and READ_PASS, force the
1602 * error on data being read off the wire. It
1603 * should force an IO error to the driver.
1604 */
James Smartf9bb2da2011-10-10 21:34:11 -04001605 *apptag = 0xDEAD;
1606 phba->lpfc_injerr_rapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001607 if (phba->lpfc_injerr_rapp_cnt == 0) {
1608 phba->lpfc_injerr_nportid = 0;
1609 phba->lpfc_injerr_lba =
1610 LPFC_INJERR_LBA_OFF;
1611 memset(&phba->lpfc_injerr_wwpn,
1612 0, sizeof(struct lpfc_name));
1613 }
James Smartacd68592012-01-18 16:25:09 -05001614 rc = BG_ERR_INIT;
James Smartf9bb2da2011-10-10 21:34:11 -04001615
1616 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smartacd68592012-01-18 16:25:09 -05001617 "0814 BLKGRD: Injecting apptag error: "
1618 "read lba x%lx\n", (unsigned long)lba);
1619 break;
1620 }
1621 }
1622 }
1623
1624
1625 /* Should we change the Guard Tag */
1626 if (new_guard) {
1627 if (phba->lpfc_injerr_wgrd_cnt) {
1628 switch (op) {
1629 case SCSI_PROT_WRITE_PASS:
James Smart9a6b09c2012-03-01 22:37:42 -05001630 rc = BG_ERR_CHECK;
James Smartacd68592012-01-18 16:25:09 -05001631 /* Drop thru */
James Smartacd68592012-01-18 16:25:09 -05001632
James Smartacd68592012-01-18 16:25:09 -05001633 case SCSI_PROT_WRITE_INSERT:
1634 /*
1635 * For WRITE_INSERT, force the
1636 * error to be sent on the wire. It should be
1637 * detected by the Target.
1638 */
1639 phba->lpfc_injerr_wgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001640 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1641 phba->lpfc_injerr_nportid = 0;
1642 phba->lpfc_injerr_lba =
1643 LPFC_INJERR_LBA_OFF;
1644 memset(&phba->lpfc_injerr_wwpn,
1645 0, sizeof(struct lpfc_name));
1646 }
James Smartacd68592012-01-18 16:25:09 -05001647
James Smart9a6b09c2012-03-01 22:37:42 -05001648 rc |= BG_ERR_TGT | BG_ERR_SWAP;
James Smartacd68592012-01-18 16:25:09 -05001649 /* Signals the caller to swap CRC->CSUM */
1650
1651 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1652 "0817 BLKGRD: Injecting guard error: "
1653 "write lba x%lx\n", (unsigned long)lba);
1654 break;
James Smart9a6b09c2012-03-01 22:37:42 -05001655 case SCSI_PROT_WRITE_STRIP:
1656 /*
1657 * For WRITE_STRIP and WRITE_PASS,
1658 * force the error on data
1659 * being copied from SLI-Host to SLI-Port.
1660 */
1661 phba->lpfc_injerr_wgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001662 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1663 phba->lpfc_injerr_nportid = 0;
1664 phba->lpfc_injerr_lba =
1665 LPFC_INJERR_LBA_OFF;
1666 memset(&phba->lpfc_injerr_wwpn,
1667 0, sizeof(struct lpfc_name));
1668 }
James Smart9a6b09c2012-03-01 22:37:42 -05001669
1670 rc = BG_ERR_INIT | BG_ERR_SWAP;
1671 /* Signals the caller to swap CRC->CSUM */
1672
1673 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1674 "0816 BLKGRD: Injecting guard error: "
1675 "write lba x%lx\n", (unsigned long)lba);
1676 break;
James Smartacd68592012-01-18 16:25:09 -05001677 }
1678 }
1679 if (phba->lpfc_injerr_rgrd_cnt) {
1680 switch (op) {
1681 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001682 case SCSI_PROT_READ_STRIP:
1683 case SCSI_PROT_READ_PASS:
1684 /*
1685 * For READ_STRIP and READ_PASS, force the
1686 * error on data being read off the wire. It
1687 * should force an IO error to the driver.
1688 */
James Smartacd68592012-01-18 16:25:09 -05001689 phba->lpfc_injerr_rgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001690 if (phba->lpfc_injerr_rgrd_cnt == 0) {
1691 phba->lpfc_injerr_nportid = 0;
1692 phba->lpfc_injerr_lba =
1693 LPFC_INJERR_LBA_OFF;
1694 memset(&phba->lpfc_injerr_wwpn,
1695 0, sizeof(struct lpfc_name));
1696 }
James Smartacd68592012-01-18 16:25:09 -05001697
James Smart9a6b09c2012-03-01 22:37:42 -05001698 rc = BG_ERR_INIT | BG_ERR_SWAP;
James Smartacd68592012-01-18 16:25:09 -05001699 /* Signals the caller to swap CRC->CSUM */
1700
1701 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1702 "0818 BLKGRD: Injecting guard error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001703 "read lba x%lx\n", (unsigned long)lba);
1704 }
1705 }
1706 }
1707
James Smartf9bb2da2011-10-10 21:34:11 -04001708 return rc;
1709}
1710#endif
1711
James Smartacd68592012-01-18 16:25:09 -05001712/**
1713 * lpfc_sc_to_bg_opcodes - Determine the BlockGuard opcodes to be used with
1714 * the specified SCSI command.
1715 * @phba: The Hba for which this call is being executed.
James Smart6c8eea52010-04-06 14:49:53 -04001716 * @sc: The SCSI command to examine
1717 * @txopt: (out) BlockGuard operation for transmitted data
1718 * @rxopt: (out) BlockGuard operation for received data
1719 *
1720 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1721 *
James Smartacd68592012-01-18 16:25:09 -05001722 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001723static int
James Smart6c8eea52010-04-06 14:49:53 -04001724lpfc_sc_to_bg_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1725 uint8_t *txop, uint8_t *rxop)
James Smarte2a0a9d2008-12-04 22:40:02 -05001726{
James Smart6c8eea52010-04-06 14:49:53 -04001727 uint8_t ret = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05001728
James Smart9c6aa9d2013-05-31 17:03:39 -04001729 if (lpfc_cmd_guard_csum(sc)) {
James Smarte2a0a9d2008-12-04 22:40:02 -05001730 switch (scsi_get_prot_op(sc)) {
1731 case SCSI_PROT_READ_INSERT:
1732 case SCSI_PROT_WRITE_STRIP:
James Smart6c8eea52010-04-06 14:49:53 -04001733 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001734 *txop = BG_OP_IN_CSUM_OUT_NODIF;
James Smarte2a0a9d2008-12-04 22:40:02 -05001735 break;
1736
1737 case SCSI_PROT_READ_STRIP:
1738 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001739 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001740 *txop = BG_OP_IN_NODIF_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001741 break;
1742
Martin K. Petersenc6af4042009-09-18 17:32:59 -04001743 case SCSI_PROT_READ_PASS:
1744 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001745 *rxop = BG_OP_IN_CRC_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001746 *txop = BG_OP_IN_CSUM_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001747 break;
1748
James Smarte2a0a9d2008-12-04 22:40:02 -05001749 case SCSI_PROT_NORMAL:
1750 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001751 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001752 "9063 BLKGRD: Bad op/guard:%d/IP combination\n",
1753 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001754 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001755 break;
1756
1757 }
James Smart7c56b9f2011-07-22 18:36:25 -04001758 } else {
James Smarte2a0a9d2008-12-04 22:40:02 -05001759 switch (scsi_get_prot_op(sc)) {
1760 case SCSI_PROT_READ_STRIP:
1761 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001762 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001763 *txop = BG_OP_IN_NODIF_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001764 break;
1765
1766 case SCSI_PROT_READ_PASS:
1767 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001768 *rxop = BG_OP_IN_CRC_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001769 *txop = BG_OP_IN_CRC_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001770 break;
1771
James Smarte2a0a9d2008-12-04 22:40:02 -05001772 case SCSI_PROT_READ_INSERT:
1773 case SCSI_PROT_WRITE_STRIP:
James Smart7c56b9f2011-07-22 18:36:25 -04001774 *rxop = BG_OP_IN_NODIF_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001775 *txop = BG_OP_IN_CRC_OUT_NODIF;
James Smart7c56b9f2011-07-22 18:36:25 -04001776 break;
1777
James Smarte2a0a9d2008-12-04 22:40:02 -05001778 case SCSI_PROT_NORMAL:
1779 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001780 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001781 "9075 BLKGRD: Bad op/guard:%d/CRC combination\n",
1782 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001783 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001784 break;
1785 }
James Smarte2a0a9d2008-12-04 22:40:02 -05001786 }
1787
James Smart6c8eea52010-04-06 14:49:53 -04001788 return ret;
James Smarte2a0a9d2008-12-04 22:40:02 -05001789}
1790
James Smartacd68592012-01-18 16:25:09 -05001791#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1792/**
1793 * lpfc_bg_err_opcodes - reDetermine the BlockGuard opcodes to be used with
1794 * the specified SCSI command in order to force a guard tag error.
1795 * @phba: The Hba for which this call is being executed.
1796 * @sc: The SCSI command to examine
1797 * @txopt: (out) BlockGuard operation for transmitted data
1798 * @rxopt: (out) BlockGuard operation for received data
1799 *
1800 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1801 *
1802 **/
1803static int
1804lpfc_bg_err_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1805 uint8_t *txop, uint8_t *rxop)
1806{
James Smartacd68592012-01-18 16:25:09 -05001807 uint8_t ret = 0;
1808
James Smart9c6aa9d2013-05-31 17:03:39 -04001809 if (lpfc_cmd_guard_csum(sc)) {
James Smartacd68592012-01-18 16:25:09 -05001810 switch (scsi_get_prot_op(sc)) {
1811 case SCSI_PROT_READ_INSERT:
1812 case SCSI_PROT_WRITE_STRIP:
James Smartacd68592012-01-18 16:25:09 -05001813 *rxop = BG_OP_IN_NODIF_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001814 *txop = BG_OP_IN_CRC_OUT_NODIF;
James Smartacd68592012-01-18 16:25:09 -05001815 break;
1816
1817 case SCSI_PROT_READ_STRIP:
1818 case SCSI_PROT_WRITE_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001819 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001820 *txop = BG_OP_IN_NODIF_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001821 break;
1822
1823 case SCSI_PROT_READ_PASS:
1824 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001825 *rxop = BG_OP_IN_CSUM_OUT_CRC;
James Smart9a6b09c2012-03-01 22:37:42 -05001826 *txop = BG_OP_IN_CRC_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001827 break;
1828
1829 case SCSI_PROT_NORMAL:
1830 default:
1831 break;
1832
1833 }
1834 } else {
1835 switch (scsi_get_prot_op(sc)) {
1836 case SCSI_PROT_READ_STRIP:
1837 case SCSI_PROT_WRITE_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001838 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001839 *txop = BG_OP_IN_NODIF_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001840 break;
1841
1842 case SCSI_PROT_READ_PASS:
1843 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001844 *rxop = BG_OP_IN_CSUM_OUT_CSUM;
James Smart9a6b09c2012-03-01 22:37:42 -05001845 *txop = BG_OP_IN_CSUM_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001846 break;
1847
1848 case SCSI_PROT_READ_INSERT:
1849 case SCSI_PROT_WRITE_STRIP:
James Smartacd68592012-01-18 16:25:09 -05001850 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001851 *txop = BG_OP_IN_CSUM_OUT_NODIF;
James Smartacd68592012-01-18 16:25:09 -05001852 break;
1853
1854 case SCSI_PROT_NORMAL:
1855 default:
1856 break;
1857 }
1858 }
1859
1860 return ret;
1861}
1862#endif
1863
1864/**
1865 * lpfc_bg_setup_bpl - Setup BlockGuard BPL with no protection data
1866 * @phba: The Hba for which this call is being executed.
1867 * @sc: pointer to scsi command we're working on
1868 * @bpl: pointer to buffer list for protection groups
1869 * @datacnt: number of segments of data that have been dma mapped
1870 *
1871 * This function sets up BPL buffer list for protection groups of
James Smarte2a0a9d2008-12-04 22:40:02 -05001872 * type LPFC_PG_TYPE_NO_DIF
1873 *
1874 * This is usually used when the HBA is instructed to generate
1875 * DIFs and insert them into data stream (or strip DIF from
1876 * incoming data stream)
1877 *
1878 * The buffer list consists of just one protection group described
1879 * below:
1880 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001881 * start of prot group --> | PDE_5 |
1882 * +-------------------------+
1883 * | PDE_6 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001884 * +-------------------------+
1885 * | Data BDE |
1886 * +-------------------------+
1887 * |more Data BDE's ... (opt)|
1888 * +-------------------------+
1889 *
James Smarte2a0a9d2008-12-04 22:40:02 -05001890 *
1891 * Note: Data s/g buffers have been dma mapped
James Smartacd68592012-01-18 16:25:09 -05001892 *
1893 * Returns the number of BDEs added to the BPL.
1894 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001895static int
1896lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1897 struct ulp_bde64 *bpl, int datasegcnt)
1898{
1899 struct scatterlist *sgde = NULL; /* s/g data entry */
James Smart6c8eea52010-04-06 14:49:53 -04001900 struct lpfc_pde5 *pde5 = NULL;
1901 struct lpfc_pde6 *pde6 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05001902 dma_addr_t physaddr;
James Smart6c8eea52010-04-06 14:49:53 -04001903 int i = 0, num_bde = 0, status;
James Smarte2a0a9d2008-12-04 22:40:02 -05001904 int datadir = sc->sc_data_direction;
James Smart0829a192012-05-09 21:18:12 -04001905#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001906 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04001907#endif
James Smartacd68592012-01-18 16:25:09 -05001908 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001909 uint32_t reftag;
James Smart6c8eea52010-04-06 14:49:53 -04001910 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05001911
James Smart6c8eea52010-04-06 14:49:53 -04001912 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1913 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05001914 goto out;
1915
James Smart6c8eea52010-04-06 14:49:53 -04001916 /* extract some info from the scsi command for pde*/
James Smartacd68592012-01-18 16:25:09 -05001917 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
James Smarte2a0a9d2008-12-04 22:40:02 -05001918
James Smartf9bb2da2011-10-10 21:34:11 -04001919#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05001920 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05001921 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05001922 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05001923 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05001924 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05001925 checking = 0;
1926 }
James Smartf9bb2da2011-10-10 21:34:11 -04001927#endif
1928
James Smart6c8eea52010-04-06 14:49:53 -04001929 /* setup PDE5 with what we have */
1930 pde5 = (struct lpfc_pde5 *) bpl;
1931 memset(pde5, 0, sizeof(struct lpfc_pde5));
1932 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05001933
James Smartbc739052010-08-04 16:11:18 -04001934 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04001935 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04001936 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04001937
James Smart6c8eea52010-04-06 14:49:53 -04001938 /* advance bpl and increment bde count */
1939 num_bde++;
1940 bpl++;
1941 pde6 = (struct lpfc_pde6 *) bpl;
1942
1943 /* setup PDE6 with the rest of the info */
1944 memset(pde6, 0, sizeof(struct lpfc_pde6));
1945 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
1946 bf_set(pde6_optx, pde6, txop);
1947 bf_set(pde6_oprx, pde6, rxop);
James Smarta6887e22013-04-17 20:18:07 -04001948
1949 /*
1950 * We only need to check the data on READs, for WRITEs
1951 * protection data is automatically generated, not checked.
1952 */
James Smart6c8eea52010-04-06 14:49:53 -04001953 if (datadir == DMA_FROM_DEVICE) {
James Smart9c6aa9d2013-05-31 17:03:39 -04001954 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
James Smarta6887e22013-04-17 20:18:07 -04001955 bf_set(pde6_ce, pde6, checking);
1956 else
1957 bf_set(pde6_ce, pde6, 0);
1958
James Smart9c6aa9d2013-05-31 17:03:39 -04001959 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04001960 bf_set(pde6_re, pde6, checking);
1961 else
1962 bf_set(pde6_re, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04001963 }
1964 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04001965 bf_set(pde6_ae, pde6, 0);
1966 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04001967
James Smartbc739052010-08-04 16:11:18 -04001968 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04001969 pde6->word0 = cpu_to_le32(pde6->word0);
1970 pde6->word1 = cpu_to_le32(pde6->word1);
1971 pde6->word2 = cpu_to_le32(pde6->word2);
1972
James Smart6c8eea52010-04-06 14:49:53 -04001973 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05001974 num_bde++;
1975 bpl++;
1976
1977 /* assumption: caller has already run dma_map_sg on command data */
1978 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
1979 physaddr = sg_dma_address(sgde);
1980 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
1981 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1982 bpl->tus.f.bdeSize = sg_dma_len(sgde);
1983 if (datadir == DMA_TO_DEVICE)
1984 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1985 else
1986 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1987 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1988 bpl++;
1989 num_bde++;
1990 }
1991
1992out:
1993 return num_bde;
1994}
1995
James Smartacd68592012-01-18 16:25:09 -05001996/**
1997 * lpfc_bg_setup_bpl_prot - Setup BlockGuard BPL with protection data
1998 * @phba: The Hba for which this call is being executed.
1999 * @sc: pointer to scsi command we're working on
2000 * @bpl: pointer to buffer list for protection groups
2001 * @datacnt: number of segments of data that have been dma mapped
2002 * @protcnt: number of segment of protection data that have been dma mapped
2003 *
2004 * This function sets up BPL buffer list for protection groups of
2005 * type LPFC_PG_TYPE_DIF
James Smarte2a0a9d2008-12-04 22:40:02 -05002006 *
2007 * This is usually used when DIFs are in their own buffers,
2008 * separate from the data. The HBA can then by instructed
2009 * to place the DIFs in the outgoing stream. For read operations,
2010 * The HBA could extract the DIFs and place it in DIF buffers.
2011 *
2012 * The buffer list for this type consists of one or more of the
2013 * protection groups described below:
2014 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002015 * start of first prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05002016 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002017 * | PDE_6 |
2018 * +-------------------------+
2019 * | PDE_7 (Prot BDE) |
James Smarte2a0a9d2008-12-04 22:40:02 -05002020 * +-------------------------+
2021 * | Data BDE |
2022 * +-------------------------+
2023 * |more Data BDE's ... (opt)|
2024 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002025 * start of new prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05002026 * +-------------------------+
2027 * | ... |
2028 * +-------------------------+
2029 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002030 * Note: It is assumed that both data and protection s/g buffers have been
2031 * mapped for DMA
James Smartacd68592012-01-18 16:25:09 -05002032 *
2033 * Returns the number of BDEs added to the BPL.
2034 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002035static int
2036lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2037 struct ulp_bde64 *bpl, int datacnt, int protcnt)
2038{
2039 struct scatterlist *sgde = NULL; /* s/g data entry */
2040 struct scatterlist *sgpe = NULL; /* s/g prot entry */
James Smart6c8eea52010-04-06 14:49:53 -04002041 struct lpfc_pde5 *pde5 = NULL;
2042 struct lpfc_pde6 *pde6 = NULL;
James Smart7f860592011-03-11 16:05:52 -05002043 struct lpfc_pde7 *pde7 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05002044 dma_addr_t dataphysaddr, protphysaddr;
2045 unsigned short curr_data = 0, curr_prot = 0;
James Smart7f860592011-03-11 16:05:52 -05002046 unsigned int split_offset;
2047 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
James Smarte2a0a9d2008-12-04 22:40:02 -05002048 unsigned int protgrp_blks, protgrp_bytes;
2049 unsigned int remainder, subtotal;
James Smart6c8eea52010-04-06 14:49:53 -04002050 int status;
James Smarte2a0a9d2008-12-04 22:40:02 -05002051 int datadir = sc->sc_data_direction;
2052 unsigned char pgdone = 0, alldone = 0;
2053 unsigned blksize;
James Smart0829a192012-05-09 21:18:12 -04002054#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002055 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002056#endif
James Smartacd68592012-01-18 16:25:09 -05002057 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05002058 uint32_t reftag;
James Smart6c8eea52010-04-06 14:49:53 -04002059 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05002060 int num_bde = 0;
2061
2062 sgpe = scsi_prot_sglist(sc);
2063 sgde = scsi_sglist(sc);
2064
2065 if (!sgpe || !sgde) {
2066 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2067 "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
2068 sgpe, sgde);
2069 return 0;
2070 }
2071
James Smart6c8eea52010-04-06 14:49:53 -04002072 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2073 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05002074 goto out;
2075
James Smart6c8eea52010-04-06 14:49:53 -04002076 /* extract some info from the scsi command */
James Smarte2a0a9d2008-12-04 22:40:02 -05002077 blksize = lpfc_cmd_blksize(sc);
James Smartacd68592012-01-18 16:25:09 -05002078 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
James Smarte2a0a9d2008-12-04 22:40:02 -05002079
James Smartf9bb2da2011-10-10 21:34:11 -04002080#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002081 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002082 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002083 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002084 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002085 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002086 checking = 0;
2087 }
James Smartf9bb2da2011-10-10 21:34:11 -04002088#endif
2089
James Smarte2a0a9d2008-12-04 22:40:02 -05002090 split_offset = 0;
2091 do {
James Smart96f70772013-04-17 20:16:15 -04002092 /* Check to see if we ran out of space */
2093 if (num_bde >= (phba->cfg_total_seg_cnt - 2))
2094 return num_bde + 3;
2095
James Smart6c8eea52010-04-06 14:49:53 -04002096 /* setup PDE5 with what we have */
2097 pde5 = (struct lpfc_pde5 *) bpl;
2098 memset(pde5, 0, sizeof(struct lpfc_pde5));
2099 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05002100
James Smartbc739052010-08-04 16:11:18 -04002101 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04002102 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04002103 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04002104
James Smart6c8eea52010-04-06 14:49:53 -04002105 /* advance bpl and increment bde count */
2106 num_bde++;
2107 bpl++;
2108 pde6 = (struct lpfc_pde6 *) bpl;
James Smarte2a0a9d2008-12-04 22:40:02 -05002109
James Smart6c8eea52010-04-06 14:49:53 -04002110 /* setup PDE6 with the rest of the info */
2111 memset(pde6, 0, sizeof(struct lpfc_pde6));
2112 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
2113 bf_set(pde6_optx, pde6, txop);
2114 bf_set(pde6_oprx, pde6, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002115
James Smart9c6aa9d2013-05-31 17:03:39 -04002116 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
James Smarta6887e22013-04-17 20:18:07 -04002117 bf_set(pde6_ce, pde6, checking);
2118 else
2119 bf_set(pde6_ce, pde6, 0);
2120
James Smart9c6aa9d2013-05-31 17:03:39 -04002121 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04002122 bf_set(pde6_re, pde6, checking);
2123 else
2124 bf_set(pde6_re, pde6, 0);
2125
James Smart6c8eea52010-04-06 14:49:53 -04002126 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04002127 bf_set(pde6_ae, pde6, 0);
2128 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04002129
James Smartbc739052010-08-04 16:11:18 -04002130 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04002131 pde6->word0 = cpu_to_le32(pde6->word0);
2132 pde6->word1 = cpu_to_le32(pde6->word1);
2133 pde6->word2 = cpu_to_le32(pde6->word2);
2134
James Smart6c8eea52010-04-06 14:49:53 -04002135 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05002136 num_bde++;
2137 bpl++;
2138
2139 /* setup the first BDE that points to protection buffer */
James Smart7f860592011-03-11 16:05:52 -05002140 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2141 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
James Smarte2a0a9d2008-12-04 22:40:02 -05002142
James Smarte2a0a9d2008-12-04 22:40:02 -05002143 /* must be integer multiple of the DIF block length */
2144 BUG_ON(protgroup_len % 8);
2145
James Smart7f860592011-03-11 16:05:52 -05002146 pde7 = (struct lpfc_pde7 *) bpl;
2147 memset(pde7, 0, sizeof(struct lpfc_pde7));
2148 bf_set(pde7_type, pde7, LPFC_PDE7_DESCRIPTOR);
2149
James Smart7c56b9f2011-07-22 18:36:25 -04002150 pde7->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
2151 pde7->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
James Smart7f860592011-03-11 16:05:52 -05002152
James Smarte2a0a9d2008-12-04 22:40:02 -05002153 protgrp_blks = protgroup_len / 8;
2154 protgrp_bytes = protgrp_blks * blksize;
2155
James Smart7f860592011-03-11 16:05:52 -05002156 /* check if this pde is crossing the 4K boundary; if so split */
2157 if ((pde7->addrLow & 0xfff) + protgroup_len > 0x1000) {
2158 protgroup_remainder = 0x1000 - (pde7->addrLow & 0xfff);
2159 protgroup_offset += protgroup_remainder;
2160 protgrp_blks = protgroup_remainder / 8;
James Smart7c56b9f2011-07-22 18:36:25 -04002161 protgrp_bytes = protgrp_blks * blksize;
James Smart7f860592011-03-11 16:05:52 -05002162 } else {
2163 protgroup_offset = 0;
2164 curr_prot++;
2165 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002166
James Smarte2a0a9d2008-12-04 22:40:02 -05002167 num_bde++;
2168
2169 /* setup BDE's for data blocks associated with DIF data */
2170 pgdone = 0;
2171 subtotal = 0; /* total bytes processed for current prot grp */
2172 while (!pgdone) {
James Smart96f70772013-04-17 20:16:15 -04002173 /* Check to see if we ran out of space */
2174 if (num_bde >= phba->cfg_total_seg_cnt)
2175 return num_bde + 1;
2176
James Smarte2a0a9d2008-12-04 22:40:02 -05002177 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -04002178 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2179 "9065 BLKGRD:%s Invalid data segment\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002180 __func__);
2181 return 0;
2182 }
2183 bpl++;
2184 dataphysaddr = sg_dma_address(sgde) + split_offset;
2185 bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
2186 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
2187
2188 remainder = sg_dma_len(sgde) - split_offset;
2189
2190 if ((subtotal + remainder) <= protgrp_bytes) {
2191 /* we can use this whole buffer */
2192 bpl->tus.f.bdeSize = remainder;
2193 split_offset = 0;
2194
2195 if ((subtotal + remainder) == protgrp_bytes)
2196 pgdone = 1;
2197 } else {
2198 /* must split this buffer with next prot grp */
2199 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
2200 split_offset += bpl->tus.f.bdeSize;
2201 }
2202
2203 subtotal += bpl->tus.f.bdeSize;
2204
2205 if (datadir == DMA_TO_DEVICE)
2206 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2207 else
2208 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2209 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2210
2211 num_bde++;
2212 curr_data++;
2213
2214 if (split_offset)
2215 break;
2216
2217 /* Move to the next s/g segment if possible */
2218 sgde = sg_next(sgde);
James Smart6c8eea52010-04-06 14:49:53 -04002219
James Smarte2a0a9d2008-12-04 22:40:02 -05002220 }
2221
James Smart7f860592011-03-11 16:05:52 -05002222 if (protgroup_offset) {
2223 /* update the reference tag */
2224 reftag += protgrp_blks;
2225 bpl++;
2226 continue;
2227 }
2228
James Smarte2a0a9d2008-12-04 22:40:02 -05002229 /* are we done ? */
2230 if (curr_prot == protcnt) {
2231 alldone = 1;
2232 } else if (curr_prot < protcnt) {
2233 /* advance to next prot buffer */
2234 sgpe = sg_next(sgpe);
2235 bpl++;
2236
2237 /* update the reference tag */
2238 reftag += protgrp_blks;
2239 } else {
2240 /* if we're here, we have a bug */
James Smart6a9c52c2009-10-02 15:16:51 -04002241 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2242 "9054 BLKGRD: bug in %s\n", __func__);
James Smarte2a0a9d2008-12-04 22:40:02 -05002243 }
2244
2245 } while (!alldone);
James Smarte2a0a9d2008-12-04 22:40:02 -05002246out:
2247
James Smarte2a0a9d2008-12-04 22:40:02 -05002248 return num_bde;
2249}
James Smart7f860592011-03-11 16:05:52 -05002250
James Smartacd68592012-01-18 16:25:09 -05002251/**
2252 * lpfc_bg_setup_sgl - Setup BlockGuard SGL with no protection data
2253 * @phba: The Hba for which this call is being executed.
2254 * @sc: pointer to scsi command we're working on
2255 * @sgl: pointer to buffer list for protection groups
2256 * @datacnt: number of segments of data that have been dma mapped
2257 *
2258 * This function sets up SGL buffer list for protection groups of
2259 * type LPFC_PG_TYPE_NO_DIF
2260 *
2261 * This is usually used when the HBA is instructed to generate
2262 * DIFs and insert them into data stream (or strip DIF from
2263 * incoming data stream)
2264 *
2265 * The buffer list consists of just one protection group described
2266 * below:
2267 * +-------------------------+
2268 * start of prot group --> | DI_SEED |
2269 * +-------------------------+
2270 * | Data SGE |
2271 * +-------------------------+
2272 * |more Data SGE's ... (opt)|
2273 * +-------------------------+
2274 *
2275 *
2276 * Note: Data s/g buffers have been dma mapped
2277 *
2278 * Returns the number of SGEs added to the SGL.
2279 **/
2280static int
2281lpfc_bg_setup_sgl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2282 struct sli4_sge *sgl, int datasegcnt)
2283{
2284 struct scatterlist *sgde = NULL; /* s/g data entry */
2285 struct sli4_sge_diseed *diseed = NULL;
2286 dma_addr_t physaddr;
2287 int i = 0, num_sge = 0, status;
James Smartacd68592012-01-18 16:25:09 -05002288 uint32_t reftag;
James Smartacd68592012-01-18 16:25:09 -05002289 uint8_t txop, rxop;
James Smart0829a192012-05-09 21:18:12 -04002290#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002291 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002292#endif
James Smartacd68592012-01-18 16:25:09 -05002293 uint32_t checking = 1;
2294 uint32_t dma_len;
2295 uint32_t dma_offset = 0;
2296
2297 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2298 if (status)
2299 goto out;
2300
2301 /* extract some info from the scsi command for pde*/
James Smartacd68592012-01-18 16:25:09 -05002302 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2303
2304#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002305 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002306 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002307 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002308 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002309 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002310 checking = 0;
2311 }
2312#endif
2313
2314 /* setup DISEED with what we have */
2315 diseed = (struct sli4_sge_diseed *) sgl;
2316 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2317 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2318
2319 /* Endianness conversion if necessary */
2320 diseed->ref_tag = cpu_to_le32(reftag);
2321 diseed->ref_tag_tran = diseed->ref_tag;
2322
James Smarta6887e22013-04-17 20:18:07 -04002323 /*
2324 * We only need to check the data on READs, for WRITEs
2325 * protection data is automatically generated, not checked.
2326 */
2327 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
James Smart9c6aa9d2013-05-31 17:03:39 -04002328 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
James Smarta6887e22013-04-17 20:18:07 -04002329 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2330 else
2331 bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2332
James Smart9c6aa9d2013-05-31 17:03:39 -04002333 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04002334 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2335 else
2336 bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2337 }
2338
James Smartacd68592012-01-18 16:25:09 -05002339 /* setup DISEED with the rest of the info */
2340 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2341 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002342
James Smartacd68592012-01-18 16:25:09 -05002343 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2344 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2345
2346 /* Endianness conversion if necessary for DISEED */
2347 diseed->word2 = cpu_to_le32(diseed->word2);
2348 diseed->word3 = cpu_to_le32(diseed->word3);
2349
2350 /* advance bpl and increment sge count */
2351 num_sge++;
2352 sgl++;
2353
2354 /* assumption: caller has already run dma_map_sg on command data */
2355 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
2356 physaddr = sg_dma_address(sgde);
2357 dma_len = sg_dma_len(sgde);
2358 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
2359 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
2360 if ((i + 1) == datasegcnt)
2361 bf_set(lpfc_sli4_sge_last, sgl, 1);
2362 else
2363 bf_set(lpfc_sli4_sge_last, sgl, 0);
2364 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2365 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2366
2367 sgl->sge_len = cpu_to_le32(dma_len);
2368 dma_offset += dma_len;
2369
2370 sgl++;
2371 num_sge++;
2372 }
2373
2374out:
2375 return num_sge;
2376}
2377
2378/**
2379 * lpfc_bg_setup_sgl_prot - Setup BlockGuard SGL with protection data
2380 * @phba: The Hba for which this call is being executed.
2381 * @sc: pointer to scsi command we're working on
2382 * @sgl: pointer to buffer list for protection groups
2383 * @datacnt: number of segments of data that have been dma mapped
2384 * @protcnt: number of segment of protection data that have been dma mapped
2385 *
2386 * This function sets up SGL buffer list for protection groups of
2387 * type LPFC_PG_TYPE_DIF
2388 *
2389 * This is usually used when DIFs are in their own buffers,
2390 * separate from the data. The HBA can then by instructed
2391 * to place the DIFs in the outgoing stream. For read operations,
2392 * The HBA could extract the DIFs and place it in DIF buffers.
2393 *
2394 * The buffer list for this type consists of one or more of the
2395 * protection groups described below:
2396 * +-------------------------+
2397 * start of first prot group --> | DISEED |
2398 * +-------------------------+
2399 * | DIF (Prot SGE) |
2400 * +-------------------------+
2401 * | Data SGE |
2402 * +-------------------------+
2403 * |more Data SGE's ... (opt)|
2404 * +-------------------------+
2405 * start of new prot group --> | DISEED |
2406 * +-------------------------+
2407 * | ... |
2408 * +-------------------------+
2409 *
2410 * Note: It is assumed that both data and protection s/g buffers have been
2411 * mapped for DMA
2412 *
2413 * Returns the number of SGEs added to the SGL.
2414 **/
2415static int
2416lpfc_bg_setup_sgl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2417 struct sli4_sge *sgl, int datacnt, int protcnt)
2418{
2419 struct scatterlist *sgde = NULL; /* s/g data entry */
2420 struct scatterlist *sgpe = NULL; /* s/g prot entry */
2421 struct sli4_sge_diseed *diseed = NULL;
2422 dma_addr_t dataphysaddr, protphysaddr;
2423 unsigned short curr_data = 0, curr_prot = 0;
2424 unsigned int split_offset;
2425 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
2426 unsigned int protgrp_blks, protgrp_bytes;
2427 unsigned int remainder, subtotal;
2428 int status;
2429 unsigned char pgdone = 0, alldone = 0;
2430 unsigned blksize;
2431 uint32_t reftag;
2432 uint8_t txop, rxop;
2433 uint32_t dma_len;
James Smart0829a192012-05-09 21:18:12 -04002434#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002435 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002436#endif
James Smartacd68592012-01-18 16:25:09 -05002437 uint32_t checking = 1;
2438 uint32_t dma_offset = 0;
2439 int num_sge = 0;
2440
2441 sgpe = scsi_prot_sglist(sc);
2442 sgde = scsi_sglist(sc);
2443
2444 if (!sgpe || !sgde) {
2445 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2446 "9082 Invalid s/g entry: data=0x%p prot=0x%p\n",
2447 sgpe, sgde);
2448 return 0;
2449 }
2450
2451 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2452 if (status)
2453 goto out;
2454
2455 /* extract some info from the scsi command */
2456 blksize = lpfc_cmd_blksize(sc);
2457 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2458
2459#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002460 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002461 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002462 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002463 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002464 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002465 checking = 0;
2466 }
2467#endif
2468
2469 split_offset = 0;
2470 do {
James Smart96f70772013-04-17 20:16:15 -04002471 /* Check to see if we ran out of space */
2472 if (num_sge >= (phba->cfg_total_seg_cnt - 2))
2473 return num_sge + 3;
2474
James Smartacd68592012-01-18 16:25:09 -05002475 /* setup DISEED with what we have */
2476 diseed = (struct sli4_sge_diseed *) sgl;
2477 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2478 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2479
2480 /* Endianness conversion if necessary */
2481 diseed->ref_tag = cpu_to_le32(reftag);
2482 diseed->ref_tag_tran = diseed->ref_tag;
2483
James Smart9c6aa9d2013-05-31 17:03:39 -04002484 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD)) {
James Smarta6887e22013-04-17 20:18:07 -04002485 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2486
2487 } else {
2488 bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2489 /*
2490 * When in this mode, the hardware will replace
2491 * the guard tag from the host with a
2492 * newly generated good CRC for the wire.
2493 * Switch to raw mode here to avoid this
2494 * behavior. What the host sends gets put on the wire.
2495 */
2496 if (txop == BG_OP_IN_CRC_OUT_CRC) {
2497 txop = BG_OP_RAW_MODE;
2498 rxop = BG_OP_RAW_MODE;
2499 }
2500 }
2501
2502
James Smart9c6aa9d2013-05-31 17:03:39 -04002503 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04002504 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2505 else
2506 bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2507
James Smartacd68592012-01-18 16:25:09 -05002508 /* setup DISEED with the rest of the info */
2509 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2510 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002511
James Smartacd68592012-01-18 16:25:09 -05002512 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2513 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2514
2515 /* Endianness conversion if necessary for DISEED */
2516 diseed->word2 = cpu_to_le32(diseed->word2);
2517 diseed->word3 = cpu_to_le32(diseed->word3);
2518
2519 /* advance sgl and increment bde count */
2520 num_sge++;
2521 sgl++;
2522
2523 /* setup the first BDE that points to protection buffer */
2524 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2525 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
2526
2527 /* must be integer multiple of the DIF block length */
2528 BUG_ON(protgroup_len % 8);
2529
2530 /* Now setup DIF SGE */
2531 sgl->word2 = 0;
2532 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DIF);
2533 sgl->addr_hi = le32_to_cpu(putPaddrHigh(protphysaddr));
2534 sgl->addr_lo = le32_to_cpu(putPaddrLow(protphysaddr));
2535 sgl->word2 = cpu_to_le32(sgl->word2);
2536
2537 protgrp_blks = protgroup_len / 8;
2538 protgrp_bytes = protgrp_blks * blksize;
2539
2540 /* check if DIF SGE is crossing the 4K boundary; if so split */
2541 if ((sgl->addr_lo & 0xfff) + protgroup_len > 0x1000) {
2542 protgroup_remainder = 0x1000 - (sgl->addr_lo & 0xfff);
2543 protgroup_offset += protgroup_remainder;
2544 protgrp_blks = protgroup_remainder / 8;
2545 protgrp_bytes = protgrp_blks * blksize;
2546 } else {
2547 protgroup_offset = 0;
2548 curr_prot++;
2549 }
2550
2551 num_sge++;
2552
2553 /* setup SGE's for data blocks associated with DIF data */
2554 pgdone = 0;
2555 subtotal = 0; /* total bytes processed for current prot grp */
2556 while (!pgdone) {
James Smart96f70772013-04-17 20:16:15 -04002557 /* Check to see if we ran out of space */
2558 if (num_sge >= phba->cfg_total_seg_cnt)
2559 return num_sge + 1;
2560
James Smartacd68592012-01-18 16:25:09 -05002561 if (!sgde) {
2562 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2563 "9086 BLKGRD:%s Invalid data segment\n",
2564 __func__);
2565 return 0;
2566 }
2567 sgl++;
2568 dataphysaddr = sg_dma_address(sgde) + split_offset;
2569
2570 remainder = sg_dma_len(sgde) - split_offset;
2571
2572 if ((subtotal + remainder) <= protgrp_bytes) {
2573 /* we can use this whole buffer */
2574 dma_len = remainder;
2575 split_offset = 0;
2576
2577 if ((subtotal + remainder) == protgrp_bytes)
2578 pgdone = 1;
2579 } else {
2580 /* must split this buffer with next prot grp */
2581 dma_len = protgrp_bytes - subtotal;
2582 split_offset += dma_len;
2583 }
2584
2585 subtotal += dma_len;
2586
2587 sgl->addr_lo = cpu_to_le32(putPaddrLow(dataphysaddr));
2588 sgl->addr_hi = cpu_to_le32(putPaddrHigh(dataphysaddr));
2589 bf_set(lpfc_sli4_sge_last, sgl, 0);
2590 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2591 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2592
2593 sgl->sge_len = cpu_to_le32(dma_len);
2594 dma_offset += dma_len;
2595
2596 num_sge++;
2597 curr_data++;
2598
2599 if (split_offset)
2600 break;
2601
2602 /* Move to the next s/g segment if possible */
2603 sgde = sg_next(sgde);
2604 }
2605
2606 if (protgroup_offset) {
2607 /* update the reference tag */
2608 reftag += protgrp_blks;
2609 sgl++;
2610 continue;
2611 }
2612
2613 /* are we done ? */
2614 if (curr_prot == protcnt) {
2615 bf_set(lpfc_sli4_sge_last, sgl, 1);
2616 alldone = 1;
2617 } else if (curr_prot < protcnt) {
2618 /* advance to next prot buffer */
2619 sgpe = sg_next(sgpe);
2620 sgl++;
2621
2622 /* update the reference tag */
2623 reftag += protgrp_blks;
2624 } else {
2625 /* if we're here, we have a bug */
2626 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2627 "9085 BLKGRD: bug in %s\n", __func__);
2628 }
2629
2630 } while (!alldone);
2631
2632out:
2633
2634 return num_sge;
2635}
2636
2637/**
2638 * lpfc_prot_group_type - Get prtotection group type of SCSI command
2639 * @phba: The Hba for which this call is being executed.
2640 * @sc: pointer to scsi command we're working on
2641 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002642 * Given a SCSI command that supports DIF, determine composition of protection
2643 * groups involved in setting up buffer lists
2644 *
James Smartacd68592012-01-18 16:25:09 -05002645 * Returns: Protection group type (with or without DIF)
2646 *
2647 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002648static int
2649lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
2650{
2651 int ret = LPFC_PG_TYPE_INVALID;
2652 unsigned char op = scsi_get_prot_op(sc);
2653
2654 switch (op) {
2655 case SCSI_PROT_READ_STRIP:
2656 case SCSI_PROT_WRITE_INSERT:
2657 ret = LPFC_PG_TYPE_NO_DIF;
2658 break;
2659 case SCSI_PROT_READ_INSERT:
2660 case SCSI_PROT_WRITE_STRIP:
2661 case SCSI_PROT_READ_PASS:
2662 case SCSI_PROT_WRITE_PASS:
James Smarte2a0a9d2008-12-04 22:40:02 -05002663 ret = LPFC_PG_TYPE_DIF_BUF;
2664 break;
2665 default:
James Smart9c6aa9d2013-05-31 17:03:39 -04002666 if (phba)
2667 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2668 "9021 Unsupported protection op:%d\n",
2669 op);
James Smarte2a0a9d2008-12-04 22:40:02 -05002670 break;
2671 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002672 return ret;
2673}
2674
James Smartacd68592012-01-18 16:25:09 -05002675/**
James Smarta6887e22013-04-17 20:18:07 -04002676 * lpfc_bg_scsi_adjust_dl - Adjust SCSI data length for BlockGuard
2677 * @phba: The Hba for which this call is being executed.
2678 * @lpfc_cmd: The scsi buffer which is going to be adjusted.
2679 *
2680 * Adjust the data length to account for how much data
2681 * is actually on the wire.
2682 *
2683 * returns the adjusted data length
2684 **/
2685static int
2686lpfc_bg_scsi_adjust_dl(struct lpfc_hba *phba,
2687 struct lpfc_scsi_buf *lpfc_cmd)
2688{
2689 struct scsi_cmnd *sc = lpfc_cmd->pCmd;
2690 int fcpdl;
2691
2692 fcpdl = scsi_bufflen(sc);
2693
2694 /* Check if there is protection data on the wire */
2695 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
James Smart9c6aa9d2013-05-31 17:03:39 -04002696 /* Read check for protection data */
James Smarta6887e22013-04-17 20:18:07 -04002697 if (scsi_get_prot_op(sc) == SCSI_PROT_READ_INSERT)
2698 return fcpdl;
2699
2700 } else {
James Smart9c6aa9d2013-05-31 17:03:39 -04002701 /* Write check for protection data */
James Smarta6887e22013-04-17 20:18:07 -04002702 if (scsi_get_prot_op(sc) == SCSI_PROT_WRITE_STRIP)
2703 return fcpdl;
2704 }
2705
2706 /*
2707 * If we are in DIF Type 1 mode every data block has a 8 byte
James Smart9c6aa9d2013-05-31 17:03:39 -04002708 * DIF (trailer) attached to it. Must ajust FCP data length
2709 * to account for the protection data.
James Smarta6887e22013-04-17 20:18:07 -04002710 */
James Smart9c6aa9d2013-05-31 17:03:39 -04002711 fcpdl += (fcpdl / lpfc_cmd_blksize(sc)) * 8;
James Smarta6887e22013-04-17 20:18:07 -04002712
2713 return fcpdl;
2714}
2715
2716/**
James Smartacd68592012-01-18 16:25:09 -05002717 * lpfc_bg_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
2718 * @phba: The Hba for which this call is being executed.
2719 * @lpfc_cmd: The scsi buffer which is going to be prep'ed.
2720 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002721 * This is the protection/DIF aware version of
2722 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
2723 * two functions eventually, but for now, it's here
James Smartacd68592012-01-18 16:25:09 -05002724 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002725static int
James Smartacd68592012-01-18 16:25:09 -05002726lpfc_bg_scsi_prep_dma_buf_s3(struct lpfc_hba *phba,
James Smarte2a0a9d2008-12-04 22:40:02 -05002727 struct lpfc_scsi_buf *lpfc_cmd)
2728{
2729 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2730 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2731 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
2732 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2733 uint32_t num_bde = 0;
2734 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
2735 int prot_group_type = 0;
James Smarta6887e22013-04-17 20:18:07 -04002736 int fcpdl;
James Smarte2a0a9d2008-12-04 22:40:02 -05002737
2738 /*
2739 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
2740 * fcp_rsp regions to the first data bde entry
2741 */
2742 bpl += 2;
2743 if (scsi_sg_count(scsi_cmnd)) {
2744 /*
2745 * The driver stores the segment count returned from pci_map_sg
2746 * because this a count of dma-mappings used to map the use_sg
2747 * pages. They are not guaranteed to be the same for those
2748 * architectures that implement an IOMMU.
2749 */
2750 datasegcnt = dma_map_sg(&phba->pcidev->dev,
2751 scsi_sglist(scsi_cmnd),
2752 scsi_sg_count(scsi_cmnd), datadir);
2753 if (unlikely(!datasegcnt))
2754 return 1;
2755
2756 lpfc_cmd->seg_cnt = datasegcnt;
James Smart96f70772013-04-17 20:16:15 -04002757
2758 /* First check if data segment count from SCSI Layer is good */
2759 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt)
2760 goto err;
James Smarte2a0a9d2008-12-04 22:40:02 -05002761
2762 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
2763
2764 switch (prot_group_type) {
2765 case LPFC_PG_TYPE_NO_DIF:
James Smart96f70772013-04-17 20:16:15 -04002766
2767 /* Here we need to add a PDE5 and PDE6 to the count */
2768 if ((lpfc_cmd->seg_cnt + 2) > phba->cfg_total_seg_cnt)
2769 goto err;
2770
James Smarte2a0a9d2008-12-04 22:40:02 -05002771 num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
2772 datasegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002773 /* we should have 2 or more entries in buffer list */
James Smarte2a0a9d2008-12-04 22:40:02 -05002774 if (num_bde < 2)
2775 goto err;
2776 break;
James Smart96f70772013-04-17 20:16:15 -04002777
2778 case LPFC_PG_TYPE_DIF_BUF:
James Smarte2a0a9d2008-12-04 22:40:02 -05002779 /*
2780 * This type indicates that protection buffers are
2781 * passed to the driver, so that needs to be prepared
2782 * for DMA
2783 */
2784 protsegcnt = dma_map_sg(&phba->pcidev->dev,
2785 scsi_prot_sglist(scsi_cmnd),
2786 scsi_prot_sg_count(scsi_cmnd), datadir);
2787 if (unlikely(!protsegcnt)) {
2788 scsi_dma_unmap(scsi_cmnd);
2789 return 1;
2790 }
2791
2792 lpfc_cmd->prot_seg_cnt = protsegcnt;
James Smart96f70772013-04-17 20:16:15 -04002793
2794 /*
2795 * There is a minimun of 4 BPLs used for every
2796 * protection data segment.
2797 */
2798 if ((lpfc_cmd->prot_seg_cnt * 4) >
2799 (phba->cfg_total_seg_cnt - 2))
2800 goto err;
James Smarte2a0a9d2008-12-04 22:40:02 -05002801
2802 num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
2803 datasegcnt, protsegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002804 /* we should have 3 or more entries in buffer list */
James Smart96f70772013-04-17 20:16:15 -04002805 if ((num_bde < 3) ||
2806 (num_bde > phba->cfg_total_seg_cnt))
James Smarte2a0a9d2008-12-04 22:40:02 -05002807 goto err;
2808 break;
James Smart96f70772013-04-17 20:16:15 -04002809
James Smarte2a0a9d2008-12-04 22:40:02 -05002810 case LPFC_PG_TYPE_INVALID:
2811 default:
James Smart96f70772013-04-17 20:16:15 -04002812 scsi_dma_unmap(scsi_cmnd);
2813 lpfc_cmd->seg_cnt = 0;
2814
James Smarte2a0a9d2008-12-04 22:40:02 -05002815 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2816 "9022 Unexpected protection group %i\n",
2817 prot_group_type);
2818 return 1;
2819 }
2820 }
2821
2822 /*
2823 * Finish initializing those IOCB fields that are dependent on the
2824 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
2825 * reinitialized since all iocb memory resources are used many times
2826 * for transmit, receive, and continuation bpl's.
2827 */
2828 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
2829 iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
2830 iocb_cmd->ulpBdeCount = 1;
2831 iocb_cmd->ulpLe = 1;
2832
James Smarta6887e22013-04-17 20:18:07 -04002833 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002834 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
2835
2836 /*
2837 * Due to difference in data length between DIF/non-DIF paths,
2838 * we need to set word 4 of IOCB here
2839 */
2840 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
2841
2842 return 0;
2843err:
James Smart96f70772013-04-17 20:16:15 -04002844 if (lpfc_cmd->seg_cnt)
2845 scsi_dma_unmap(scsi_cmnd);
2846 if (lpfc_cmd->prot_seg_cnt)
2847 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
2848 scsi_prot_sg_count(scsi_cmnd),
2849 scsi_cmnd->sc_data_direction);
2850
James Smarte2a0a9d2008-12-04 22:40:02 -05002851 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart96f70772013-04-17 20:16:15 -04002852 "9023 Cannot setup S/G List for HBA"
2853 "IO segs %d/%d BPL %d SCSI %d: %d %d\n",
2854 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
2855 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
James Smarte2a0a9d2008-12-04 22:40:02 -05002856 prot_group_type, num_bde);
James Smart96f70772013-04-17 20:16:15 -04002857
2858 lpfc_cmd->seg_cnt = 0;
2859 lpfc_cmd->prot_seg_cnt = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05002860 return 1;
2861}
2862
2863/*
James Smart737d4242013-04-17 20:14:49 -04002864 * This function calcuates the T10 DIF guard tag
2865 * on the specified data using a CRC algorithmn
2866 * using crc_t10dif.
2867 */
Rashika Kheria7bfe7812014-09-03 12:55:36 -04002868static uint16_t
James Smart737d4242013-04-17 20:14:49 -04002869lpfc_bg_crc(uint8_t *data, int count)
2870{
2871 uint16_t crc = 0;
2872 uint16_t x;
2873
2874 crc = crc_t10dif(data, count);
2875 x = cpu_to_be16(crc);
2876 return x;
2877}
2878
2879/*
2880 * This function calcuates the T10 DIF guard tag
2881 * on the specified data using a CSUM algorithmn
2882 * using ip_compute_csum.
2883 */
Rashika Kheria7bfe7812014-09-03 12:55:36 -04002884static uint16_t
James Smart737d4242013-04-17 20:14:49 -04002885lpfc_bg_csum(uint8_t *data, int count)
2886{
2887 uint16_t ret;
2888
2889 ret = ip_compute_csum(data, count);
2890 return ret;
2891}
2892
2893/*
2894 * This function examines the protection data to try to determine
2895 * what type of T10-DIF error occurred.
2896 */
Rashika Kheria7bfe7812014-09-03 12:55:36 -04002897static void
James Smart737d4242013-04-17 20:14:49 -04002898lpfc_calc_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
2899{
2900 struct scatterlist *sgpe; /* s/g prot entry */
2901 struct scatterlist *sgde; /* s/g data entry */
2902 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
2903 struct scsi_dif_tuple *src = NULL;
2904 uint8_t *data_src = NULL;
Sebastian Herbsztdb6f1c22015-08-31 16:48:14 -04002905 uint16_t guard_tag;
James Smart737d4242013-04-17 20:14:49 -04002906 uint16_t start_app_tag, app_tag;
2907 uint32_t start_ref_tag, ref_tag;
2908 int prot, protsegcnt;
2909 int err_type, len, data_len;
2910 int chk_ref, chk_app, chk_guard;
2911 uint16_t sum;
2912 unsigned blksize;
2913
2914 err_type = BGS_GUARD_ERR_MASK;
2915 sum = 0;
2916 guard_tag = 0;
2917
2918 /* First check to see if there is protection data to examine */
2919 prot = scsi_get_prot_op(cmd);
2920 if ((prot == SCSI_PROT_READ_STRIP) ||
2921 (prot == SCSI_PROT_WRITE_INSERT) ||
2922 (prot == SCSI_PROT_NORMAL))
2923 goto out;
2924
2925 /* Currently the driver just supports ref_tag and guard_tag checking */
2926 chk_ref = 1;
2927 chk_app = 0;
2928 chk_guard = 0;
2929
2930 /* Setup a ptr to the protection data provided by the SCSI host */
2931 sgpe = scsi_prot_sglist(cmd);
2932 protsegcnt = lpfc_cmd->prot_seg_cnt;
2933
2934 if (sgpe && protsegcnt) {
2935
2936 /*
2937 * We will only try to verify guard tag if the segment
2938 * data length is a multiple of the blksize.
2939 */
2940 sgde = scsi_sglist(cmd);
2941 blksize = lpfc_cmd_blksize(cmd);
2942 data_src = (uint8_t *)sg_virt(sgde);
2943 data_len = sgde->length;
2944 if ((data_len & (blksize - 1)) == 0)
2945 chk_guard = 1;
James Smart737d4242013-04-17 20:14:49 -04002946
James Smarte85d8f92013-05-31 17:04:10 -04002947 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
James Smarta6887e22013-04-17 20:18:07 -04002948 start_ref_tag = (uint32_t)scsi_get_lba(cmd); /* Truncate LBA */
James Smart737d4242013-04-17 20:14:49 -04002949 start_app_tag = src->app_tag;
James Smart737d4242013-04-17 20:14:49 -04002950 len = sgpe->length;
2951 while (src && protsegcnt) {
2952 while (len) {
2953
2954 /*
2955 * First check to see if a protection data
2956 * check is valid
2957 */
Dmitry Monakhov128b6f92017-06-29 11:31:12 -07002958 if ((src->ref_tag == T10_PI_REF_ESCAPE) ||
2959 (src->app_tag == T10_PI_APP_ESCAPE)) {
James Smart737d4242013-04-17 20:14:49 -04002960 start_ref_tag++;
2961 goto skipit;
2962 }
2963
James Smart9c6aa9d2013-05-31 17:03:39 -04002964 /* First Guard Tag checking */
2965 if (chk_guard) {
2966 guard_tag = src->guard_tag;
2967 if (lpfc_cmd_guard_csum(cmd))
2968 sum = lpfc_bg_csum(data_src,
2969 blksize);
2970 else
2971 sum = lpfc_bg_crc(data_src,
2972 blksize);
2973 if ((guard_tag != sum)) {
2974 err_type = BGS_GUARD_ERR_MASK;
2975 goto out;
2976 }
James Smart737d4242013-04-17 20:14:49 -04002977 }
2978
2979 /* Reference Tag checking */
2980 ref_tag = be32_to_cpu(src->ref_tag);
2981 if (chk_ref && (ref_tag != start_ref_tag)) {
2982 err_type = BGS_REFTAG_ERR_MASK;
2983 goto out;
2984 }
2985 start_ref_tag++;
2986
James Smart9c6aa9d2013-05-31 17:03:39 -04002987 /* App Tag checking */
2988 app_tag = src->app_tag;
2989 if (chk_app && (app_tag != start_app_tag)) {
2990 err_type = BGS_APPTAG_ERR_MASK;
2991 goto out;
James Smart737d4242013-04-17 20:14:49 -04002992 }
2993skipit:
2994 len -= sizeof(struct scsi_dif_tuple);
2995 if (len < 0)
2996 len = 0;
2997 src++;
2998
2999 data_src += blksize;
3000 data_len -= blksize;
3001
3002 /*
3003 * Are we at the end of the Data segment?
3004 * The data segment is only used for Guard
3005 * tag checking.
3006 */
3007 if (chk_guard && (data_len == 0)) {
3008 chk_guard = 0;
3009 sgde = sg_next(sgde);
3010 if (!sgde)
3011 goto out;
3012
3013 data_src = (uint8_t *)sg_virt(sgde);
3014 data_len = sgde->length;
3015 if ((data_len & (blksize - 1)) == 0)
3016 chk_guard = 1;
3017 }
3018 }
3019
3020 /* Goto the next Protection data segment */
3021 sgpe = sg_next(sgpe);
3022 if (sgpe) {
3023 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
3024 len = sgpe->length;
3025 } else {
3026 src = NULL;
3027 }
3028 protsegcnt--;
3029 }
3030 }
3031out:
3032 if (err_type == BGS_GUARD_ERR_MASK) {
3033 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3034 0x10, 0x1);
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02003035 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
3036 SAM_STAT_CHECK_CONDITION;
James Smart737d4242013-04-17 20:14:49 -04003037 phba->bg_guard_err_cnt++;
3038 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3039 "9069 BLKGRD: LBA %lx grd_tag error %x != %x\n",
3040 (unsigned long)scsi_get_lba(cmd),
3041 sum, guard_tag);
3042
3043 } else if (err_type == BGS_REFTAG_ERR_MASK) {
3044 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3045 0x10, 0x3);
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02003046 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
3047 SAM_STAT_CHECK_CONDITION;
James Smart737d4242013-04-17 20:14:49 -04003048
3049 phba->bg_reftag_err_cnt++;
3050 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3051 "9066 BLKGRD: LBA %lx ref_tag error %x != %x\n",
3052 (unsigned long)scsi_get_lba(cmd),
3053 ref_tag, start_ref_tag);
3054
3055 } else if (err_type == BGS_APPTAG_ERR_MASK) {
3056 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3057 0x10, 0x2);
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02003058 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
3059 SAM_STAT_CHECK_CONDITION;
James Smart737d4242013-04-17 20:14:49 -04003060
3061 phba->bg_apptag_err_cnt++;
3062 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3063 "9041 BLKGRD: LBA %lx app_tag error %x != %x\n",
3064 (unsigned long)scsi_get_lba(cmd),
3065 app_tag, start_app_tag);
3066 }
3067}
3068
3069
3070/*
James Smarte2a0a9d2008-12-04 22:40:02 -05003071 * This function checks for BlockGuard errors detected by
3072 * the HBA. In case of errors, the ASC/ASCQ fields in the
3073 * sense buffer will be set accordingly, paired with
3074 * ILLEGAL_REQUEST to signal to the kernel that the HBA
3075 * detected corruption.
3076 *
3077 * Returns:
3078 * 0 - No error found
3079 * 1 - BlockGuard error found
3080 * -1 - Internal error (bad profile, ...etc)
3081 */
3082static int
3083lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
3084 struct lpfc_iocbq *pIocbOut)
3085{
3086 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
3087 struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
3088 int ret = 0;
3089 uint32_t bghm = bgf->bghm;
3090 uint32_t bgstat = bgf->bgstat;
3091 uint64_t failing_sector = 0;
3092
James Smarte2a0a9d2008-12-04 22:40:02 -05003093 spin_lock(&_dump_buf_lock);
3094 if (!_dump_buf_done) {
James Smart6a9c52c2009-10-02 15:16:51 -04003095 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9070 BLKGRD: Saving"
3096 " Data for %u blocks to debugfs\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05003097 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
James Smart6a9c52c2009-10-02 15:16:51 -04003098 lpfc_debug_save_data(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003099
3100 /* If we have a prot sgl, save the DIF buffer */
3101 if (lpfc_prot_group_type(phba, cmd) ==
3102 LPFC_PG_TYPE_DIF_BUF) {
James Smart6a9c52c2009-10-02 15:16:51 -04003103 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9071 BLKGRD: "
3104 "Saving DIF for %u blocks to debugfs\n",
3105 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
3106 lpfc_debug_save_dif(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003107 }
3108
3109 _dump_buf_done = 1;
3110 }
3111 spin_unlock(&_dump_buf_lock);
3112
3113 if (lpfc_bgs_get_invalid_prof(bgstat)) {
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02003114 cmd->result = DID_ERROR << 16;
James Smart737d4242013-04-17 20:14:49 -04003115 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3116 "9072 BLKGRD: Invalid BG Profile in cmd"
3117 " 0x%x lba 0x%llx blk cnt 0x%x "
3118 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3119 (unsigned long long)scsi_get_lba(cmd),
3120 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003121 ret = (-1);
3122 goto out;
3123 }
3124
3125 if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02003126 cmd->result = DID_ERROR << 16;
James Smart737d4242013-04-17 20:14:49 -04003127 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3128 "9073 BLKGRD: Invalid BG PDIF Block in cmd"
3129 " 0x%x lba 0x%llx blk cnt 0x%x "
3130 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3131 (unsigned long long)scsi_get_lba(cmd),
3132 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003133 ret = (-1);
3134 goto out;
3135 }
3136
3137 if (lpfc_bgs_get_guard_err(bgstat)) {
3138 ret = 1;
3139
3140 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3141 0x10, 0x1);
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02003142 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
3143 SAM_STAT_CHECK_CONDITION;
James Smarte2a0a9d2008-12-04 22:40:02 -05003144 phba->bg_guard_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04003145 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3146 "9055 BLKGRD: Guard Tag error in cmd"
3147 " 0x%x lba 0x%llx blk cnt 0x%x "
3148 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3149 (unsigned long long)scsi_get_lba(cmd),
3150 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003151 }
3152
3153 if (lpfc_bgs_get_reftag_err(bgstat)) {
3154 ret = 1;
3155
3156 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3157 0x10, 0x3);
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02003158 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
3159 SAM_STAT_CHECK_CONDITION;
James Smarte2a0a9d2008-12-04 22:40:02 -05003160
3161 phba->bg_reftag_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04003162 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3163 "9056 BLKGRD: Ref Tag error in cmd"
3164 " 0x%x lba 0x%llx blk cnt 0x%x "
3165 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3166 (unsigned long long)scsi_get_lba(cmd),
3167 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003168 }
3169
3170 if (lpfc_bgs_get_apptag_err(bgstat)) {
3171 ret = 1;
3172
3173 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3174 0x10, 0x2);
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02003175 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
3176 SAM_STAT_CHECK_CONDITION;
James Smarte2a0a9d2008-12-04 22:40:02 -05003177
3178 phba->bg_apptag_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04003179 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3180 "9061 BLKGRD: App Tag error in cmd"
3181 " 0x%x lba 0x%llx blk cnt 0x%x "
3182 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3183 (unsigned long long)scsi_get_lba(cmd),
3184 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003185 }
3186
3187 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
3188 /*
3189 * setup sense data descriptor 0 per SPC-4 as an information
James Smart7c56b9f2011-07-22 18:36:25 -04003190 * field, and put the failing LBA in it.
3191 * This code assumes there was also a guard/app/ref tag error
3192 * indication.
James Smarte2a0a9d2008-12-04 22:40:02 -05003193 */
James Smart7c56b9f2011-07-22 18:36:25 -04003194 cmd->sense_buffer[7] = 0xc; /* Additional sense length */
3195 cmd->sense_buffer[8] = 0; /* Information descriptor type */
3196 cmd->sense_buffer[9] = 0xa; /* Additional descriptor length */
3197 cmd->sense_buffer[10] = 0x80; /* Validity bit */
James Smartacd68592012-01-18 16:25:09 -05003198
3199 /* bghm is a "on the wire" FC frame based count */
3200 switch (scsi_get_prot_op(cmd)) {
3201 case SCSI_PROT_READ_INSERT:
3202 case SCSI_PROT_WRITE_STRIP:
3203 bghm /= cmd->device->sector_size;
3204 break;
3205 case SCSI_PROT_READ_STRIP:
3206 case SCSI_PROT_WRITE_INSERT:
3207 case SCSI_PROT_READ_PASS:
3208 case SCSI_PROT_WRITE_PASS:
3209 bghm /= (cmd->device->sector_size +
3210 sizeof(struct scsi_dif_tuple));
3211 break;
3212 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003213
3214 failing_sector = scsi_get_lba(cmd);
3215 failing_sector += bghm;
3216
James Smart7c56b9f2011-07-22 18:36:25 -04003217 /* Descriptor Information */
3218 put_unaligned_be64(failing_sector, &cmd->sense_buffer[12]);
James Smarte2a0a9d2008-12-04 22:40:02 -05003219 }
3220
3221 if (!ret) {
3222 /* No error was reported - problem in FW? */
James Smart737d4242013-04-17 20:14:49 -04003223 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3224 "9057 BLKGRD: Unknown error in cmd"
3225 " 0x%x lba 0x%llx blk cnt 0x%x "
3226 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3227 (unsigned long long)scsi_get_lba(cmd),
3228 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003229
James Smart737d4242013-04-17 20:14:49 -04003230 /* Calcuate what type of error it was */
3231 lpfc_calc_bg_err(phba, lpfc_cmd);
3232 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003233out:
3234 return ret;
3235}
3236
James Smartea2151b2008-09-07 11:52:10 -04003237/**
James Smartda0436e2009-05-22 14:51:39 -04003238 * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3239 * @phba: The Hba for which this call is being executed.
3240 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3241 *
3242 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
3243 * field of @lpfc_cmd for device with SLI-4 interface spec.
3244 *
3245 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04003246 * 1 - Error
3247 * 0 - Success
James Smartda0436e2009-05-22 14:51:39 -04003248 **/
3249static int
3250lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3251{
3252 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3253 struct scatterlist *sgel = NULL;
3254 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3255 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
James Smartfedd3b72011-02-16 12:39:24 -05003256 struct sli4_sge *first_data_sgl;
James Smartda0436e2009-05-22 14:51:39 -04003257 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
3258 dma_addr_t physaddr;
3259 uint32_t num_bde = 0;
3260 uint32_t dma_len;
3261 uint32_t dma_offset = 0;
3262 int nseg;
James Smartfedd3b72011-02-16 12:39:24 -05003263 struct ulp_bde64 *bde;
James Smartda0436e2009-05-22 14:51:39 -04003264
3265 /*
3266 * There are three possibilities here - use scatter-gather segment, use
3267 * the single mapping, or neither. Start the lpfc command prep by
3268 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
3269 * data bde entry.
3270 */
3271 if (scsi_sg_count(scsi_cmnd)) {
3272 /*
3273 * The driver stores the segment count returned from pci_map_sg
3274 * because this a count of dma-mappings used to map the use_sg
3275 * pages. They are not guaranteed to be the same for those
3276 * architectures that implement an IOMMU.
3277 */
3278
3279 nseg = scsi_dma_map(scsi_cmnd);
James Smart5116fbf2015-05-22 10:42:39 -04003280 if (unlikely(nseg <= 0))
James Smartda0436e2009-05-22 14:51:39 -04003281 return 1;
3282 sgl += 1;
3283 /* clear the last flag in the fcp_rsp map entry */
3284 sgl->word2 = le32_to_cpu(sgl->word2);
3285 bf_set(lpfc_sli4_sge_last, sgl, 0);
3286 sgl->word2 = cpu_to_le32(sgl->word2);
3287 sgl += 1;
James Smartfedd3b72011-02-16 12:39:24 -05003288 first_data_sgl = sgl;
James Smartda0436e2009-05-22 14:51:39 -04003289 lpfc_cmd->seg_cnt = nseg;
3290 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04003291 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
3292 " %s: Too many sg segments from "
3293 "dma_map_sg. Config %d, seg_cnt %d\n",
3294 __func__, phba->cfg_sg_seg_cnt,
James Smartda0436e2009-05-22 14:51:39 -04003295 lpfc_cmd->seg_cnt);
James Smart96f70772013-04-17 20:16:15 -04003296 lpfc_cmd->seg_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04003297 scsi_dma_unmap(scsi_cmnd);
3298 return 1;
3299 }
3300
3301 /*
3302 * The driver established a maximum scatter-gather segment count
3303 * during probe that limits the number of sg elements in any
3304 * single scsi command. Just run through the seg_cnt and format
3305 * the sge's.
3306 * When using SLI-3 the driver will try to fit all the BDEs into
3307 * the IOCB. If it can't then the BDEs get added to a BPL as it
3308 * does for SLI-2 mode.
3309 */
3310 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
3311 physaddr = sg_dma_address(sgel);
3312 dma_len = sg_dma_len(sgel);
James Smartda0436e2009-05-22 14:51:39 -04003313 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
3314 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
James Smart05580562011-05-24 11:40:48 -04003315 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -04003316 if ((num_bde + 1) == nseg)
3317 bf_set(lpfc_sli4_sge_last, sgl, 1);
3318 else
3319 bf_set(lpfc_sli4_sge_last, sgl, 0);
3320 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
James Smartf9bb2da2011-10-10 21:34:11 -04003321 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
James Smartda0436e2009-05-22 14:51:39 -04003322 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05003323 sgl->sge_len = cpu_to_le32(dma_len);
James Smartda0436e2009-05-22 14:51:39 -04003324 dma_offset += dma_len;
3325 sgl++;
3326 }
James Smart0bc2b7c2018-02-22 08:18:48 -08003327 /*
3328 * Setup the first Payload BDE. For FCoE we just key off
James Smart414abe02018-06-26 08:24:26 -07003329 * Performance Hints, for FC we use lpfc_enable_pbde.
3330 * We populate words 13-15 of IOCB/WQE.
James Smart0bc2b7c2018-02-22 08:18:48 -08003331 */
3332 if ((phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) ||
James Smart414abe02018-06-26 08:24:26 -07003333 phba->cfg_enable_pbde) {
James Smartfedd3b72011-02-16 12:39:24 -05003334 bde = (struct ulp_bde64 *)
James Smart414abe02018-06-26 08:24:26 -07003335 &(iocb_cmd->unsli3.sli3Words[5]);
James Smartfedd3b72011-02-16 12:39:24 -05003336 bde->addrLow = first_data_sgl->addr_lo;
3337 bde->addrHigh = first_data_sgl->addr_hi;
3338 bde->tus.f.bdeSize =
3339 le32_to_cpu(first_data_sgl->sge_len);
3340 bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
3341 bde->tus.w = cpu_to_le32(bde->tus.w);
3342 }
James Smartda0436e2009-05-22 14:51:39 -04003343 } else {
3344 sgl += 1;
3345 /* clear the last flag in the fcp_rsp map entry */
3346 sgl->word2 = le32_to_cpu(sgl->word2);
3347 bf_set(lpfc_sli4_sge_last, sgl, 1);
3348 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart414abe02018-06-26 08:24:26 -07003349
3350 if ((phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) ||
3351 phba->cfg_enable_pbde) {
3352 bde = (struct ulp_bde64 *)
3353 &(iocb_cmd->unsli3.sli3Words[5]);
3354 memset(bde, 0, (sizeof(uint32_t) * 3));
3355 }
James Smartda0436e2009-05-22 14:51:39 -04003356 }
3357
3358 /*
3359 * Finish initializing those IOCB fields that are dependent on the
3360 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
3361 * explicitly reinitialized.
3362 * all iocb memory resources are reused.
3363 */
3364 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
3365
3366 /*
3367 * Due to difference in data length between DIF/non-DIF paths,
3368 * we need to set word 4 of IOCB here
3369 */
3370 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
James Smart1ba981f2014-02-20 09:56:45 -05003371
3372 /*
3373 * If the OAS driver feature is enabled and the lun is enabled for
3374 * OAS, set the oas iocb related flags.
3375 */
James Smartf38fa0b2014-04-04 13:52:21 -04003376 if ((phba->cfg_fof) && ((struct lpfc_device_data *)
James Smartc92c8412016-07-06 12:36:05 -07003377 scsi_cmnd->device->hostdata)->oas_enabled) {
James Smart9bd2bff52014-09-03 12:57:30 -04003378 lpfc_cmd->cur_iocbq.iocb_flag |= (LPFC_IO_OAS | LPFC_IO_FOF);
James Smartc92c8412016-07-06 12:36:05 -07003379 lpfc_cmd->cur_iocbq.priority = ((struct lpfc_device_data *)
3380 scsi_cmnd->device->hostdata)->priority;
3381 }
James Smartda0436e2009-05-22 14:51:39 -04003382 return 0;
3383}
3384
3385/**
James Smartacd68592012-01-18 16:25:09 -05003386 * lpfc_bg_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3387 * @phba: The Hba for which this call is being executed.
3388 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3389 *
3390 * This is the protection/DIF aware version of
3391 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
3392 * two functions eventually, but for now, it's here
3393 **/
3394static int
3395lpfc_bg_scsi_prep_dma_buf_s4(struct lpfc_hba *phba,
3396 struct lpfc_scsi_buf *lpfc_cmd)
3397{
3398 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3399 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3400 struct sli4_sge *sgl = (struct sli4_sge *)(lpfc_cmd->fcp_bpl);
3401 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart96f70772013-04-17 20:16:15 -04003402 uint32_t num_sge = 0;
James Smartacd68592012-01-18 16:25:09 -05003403 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
3404 int prot_group_type = 0;
3405 int fcpdl;
3406
3407 /*
3408 * Start the lpfc command prep by bumping the sgl beyond fcp_cmnd
James Smart96f70772013-04-17 20:16:15 -04003409 * fcp_rsp regions to the first data sge entry
James Smartacd68592012-01-18 16:25:09 -05003410 */
3411 if (scsi_sg_count(scsi_cmnd)) {
3412 /*
3413 * The driver stores the segment count returned from pci_map_sg
3414 * because this a count of dma-mappings used to map the use_sg
3415 * pages. They are not guaranteed to be the same for those
3416 * architectures that implement an IOMMU.
3417 */
3418 datasegcnt = dma_map_sg(&phba->pcidev->dev,
3419 scsi_sglist(scsi_cmnd),
3420 scsi_sg_count(scsi_cmnd), datadir);
3421 if (unlikely(!datasegcnt))
3422 return 1;
3423
3424 sgl += 1;
3425 /* clear the last flag in the fcp_rsp map entry */
3426 sgl->word2 = le32_to_cpu(sgl->word2);
3427 bf_set(lpfc_sli4_sge_last, sgl, 0);
3428 sgl->word2 = cpu_to_le32(sgl->word2);
3429
3430 sgl += 1;
3431 lpfc_cmd->seg_cnt = datasegcnt;
James Smart96f70772013-04-17 20:16:15 -04003432
3433 /* First check if data segment count from SCSI Layer is good */
3434 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt)
3435 goto err;
James Smartacd68592012-01-18 16:25:09 -05003436
3437 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
3438
3439 switch (prot_group_type) {
3440 case LPFC_PG_TYPE_NO_DIF:
James Smart96f70772013-04-17 20:16:15 -04003441 /* Here we need to add a DISEED to the count */
3442 if ((lpfc_cmd->seg_cnt + 1) > phba->cfg_total_seg_cnt)
3443 goto err;
3444
3445 num_sge = lpfc_bg_setup_sgl(phba, scsi_cmnd, sgl,
James Smartacd68592012-01-18 16:25:09 -05003446 datasegcnt);
James Smart96f70772013-04-17 20:16:15 -04003447
James Smartacd68592012-01-18 16:25:09 -05003448 /* we should have 2 or more entries in buffer list */
James Smart96f70772013-04-17 20:16:15 -04003449 if (num_sge < 2)
James Smartacd68592012-01-18 16:25:09 -05003450 goto err;
3451 break;
James Smart96f70772013-04-17 20:16:15 -04003452
3453 case LPFC_PG_TYPE_DIF_BUF:
James Smartacd68592012-01-18 16:25:09 -05003454 /*
3455 * This type indicates that protection buffers are
3456 * passed to the driver, so that needs to be prepared
3457 * for DMA
3458 */
3459 protsegcnt = dma_map_sg(&phba->pcidev->dev,
3460 scsi_prot_sglist(scsi_cmnd),
3461 scsi_prot_sg_count(scsi_cmnd), datadir);
3462 if (unlikely(!protsegcnt)) {
3463 scsi_dma_unmap(scsi_cmnd);
3464 return 1;
3465 }
3466
3467 lpfc_cmd->prot_seg_cnt = protsegcnt;
James Smart96f70772013-04-17 20:16:15 -04003468 /*
3469 * There is a minimun of 3 SGEs used for every
3470 * protection data segment.
3471 */
3472 if ((lpfc_cmd->prot_seg_cnt * 3) >
3473 (phba->cfg_total_seg_cnt - 2))
3474 goto err;
James Smartacd68592012-01-18 16:25:09 -05003475
James Smart96f70772013-04-17 20:16:15 -04003476 num_sge = lpfc_bg_setup_sgl_prot(phba, scsi_cmnd, sgl,
James Smartacd68592012-01-18 16:25:09 -05003477 datasegcnt, protsegcnt);
James Smart96f70772013-04-17 20:16:15 -04003478
James Smartacd68592012-01-18 16:25:09 -05003479 /* we should have 3 or more entries in buffer list */
James Smart96f70772013-04-17 20:16:15 -04003480 if ((num_sge < 3) ||
3481 (num_sge > phba->cfg_total_seg_cnt))
James Smartacd68592012-01-18 16:25:09 -05003482 goto err;
3483 break;
James Smart96f70772013-04-17 20:16:15 -04003484
James Smartacd68592012-01-18 16:25:09 -05003485 case LPFC_PG_TYPE_INVALID:
3486 default:
James Smart96f70772013-04-17 20:16:15 -04003487 scsi_dma_unmap(scsi_cmnd);
3488 lpfc_cmd->seg_cnt = 0;
3489
James Smartacd68592012-01-18 16:25:09 -05003490 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
3491 "9083 Unexpected protection group %i\n",
3492 prot_group_type);
3493 return 1;
3494 }
3495 }
3496
James Smart8012cc32012-10-31 14:44:49 -04003497 switch (scsi_get_prot_op(scsi_cmnd)) {
3498 case SCSI_PROT_WRITE_STRIP:
3499 case SCSI_PROT_READ_STRIP:
3500 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_STRIP;
3501 break;
3502 case SCSI_PROT_WRITE_INSERT:
3503 case SCSI_PROT_READ_INSERT:
3504 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_INSERT;
3505 break;
3506 case SCSI_PROT_WRITE_PASS:
3507 case SCSI_PROT_READ_PASS:
3508 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_PASS;
3509 break;
3510 }
3511
James Smartacd68592012-01-18 16:25:09 -05003512 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
James Smartacd68592012-01-18 16:25:09 -05003513 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
3514
3515 /*
3516 * Due to difference in data length between DIF/non-DIF paths,
3517 * we need to set word 4 of IOCB here
3518 */
3519 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
James Smartacd68592012-01-18 16:25:09 -05003520
James Smart9bd2bff52014-09-03 12:57:30 -04003521 /*
3522 * If the OAS driver feature is enabled and the lun is enabled for
3523 * OAS, set the oas iocb related flags.
3524 */
3525 if ((phba->cfg_fof) && ((struct lpfc_device_data *)
3526 scsi_cmnd->device->hostdata)->oas_enabled)
3527 lpfc_cmd->cur_iocbq.iocb_flag |= (LPFC_IO_OAS | LPFC_IO_FOF);
3528
James Smartacd68592012-01-18 16:25:09 -05003529 return 0;
3530err:
James Smart96f70772013-04-17 20:16:15 -04003531 if (lpfc_cmd->seg_cnt)
3532 scsi_dma_unmap(scsi_cmnd);
3533 if (lpfc_cmd->prot_seg_cnt)
3534 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
3535 scsi_prot_sg_count(scsi_cmnd),
3536 scsi_cmnd->sc_data_direction);
3537
James Smartacd68592012-01-18 16:25:09 -05003538 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart96f70772013-04-17 20:16:15 -04003539 "9084 Cannot setup S/G List for HBA"
3540 "IO segs %d/%d SGL %d SCSI %d: %d %d\n",
3541 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
3542 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
3543 prot_group_type, num_sge);
3544
3545 lpfc_cmd->seg_cnt = 0;
3546 lpfc_cmd->prot_seg_cnt = 0;
James Smartacd68592012-01-18 16:25:09 -05003547 return 1;
3548}
3549
3550/**
James Smart3772a992009-05-22 14:50:54 -04003551 * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3552 * @phba: The Hba for which this call is being executed.
3553 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3554 *
3555 * This routine wraps the actual DMA mapping function pointer from the
3556 * lpfc_hba struct.
3557 *
3558 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04003559 * 1 - Error
3560 * 0 - Success
James Smart3772a992009-05-22 14:50:54 -04003561 **/
3562static inline int
3563lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3564{
3565 return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
3566}
3567
3568/**
James Smartacd68592012-01-18 16:25:09 -05003569 * lpfc_bg_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3570 * using BlockGuard.
3571 * @phba: The Hba for which this call is being executed.
3572 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3573 *
3574 * This routine wraps the actual DMA mapping function pointer from the
3575 * lpfc_hba struct.
3576 *
3577 * Return codes:
3578 * 1 - Error
3579 * 0 - Success
3580 **/
3581static inline int
3582lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3583{
3584 return phba->lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
3585}
3586
3587/**
James Smart3621a712009-04-06 18:47:14 -04003588 * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
James Smartea2151b2008-09-07 11:52:10 -04003589 * @phba: Pointer to hba context object.
3590 * @vport: Pointer to vport object.
3591 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
3592 * @rsp_iocb: Pointer to response iocb object which reported error.
3593 *
3594 * This function posts an event when there is a SCSI command reporting
3595 * error from the scsi device.
3596 **/
3597static void
3598lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
3599 struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
3600 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3601 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
3602 uint32_t resp_info = fcprsp->rspStatus2;
3603 uint32_t scsi_status = fcprsp->rspStatus3;
3604 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
3605 struct lpfc_fast_path_event *fast_path_evt = NULL;
3606 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
3607 unsigned long flags;
3608
James Smart5989b8d2010-10-22 11:06:56 -04003609 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3610 return;
3611
James Smartea2151b2008-09-07 11:52:10 -04003612 /* If there is queuefull or busy condition send a scsi event */
3613 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
3614 (cmnd->result == SAM_STAT_BUSY)) {
3615 fast_path_evt = lpfc_alloc_fast_evt(phba);
3616 if (!fast_path_evt)
3617 return;
3618 fast_path_evt->un.scsi_evt.event_type =
3619 FC_REG_SCSI_EVENT;
3620 fast_path_evt->un.scsi_evt.subcategory =
3621 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
3622 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
3623 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
3624 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
3625 &pnode->nlp_portname, sizeof(struct lpfc_name));
3626 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
3627 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3628 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
3629 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
3630 fast_path_evt = lpfc_alloc_fast_evt(phba);
3631 if (!fast_path_evt)
3632 return;
3633 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
3634 FC_REG_SCSI_EVENT;
3635 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
3636 LPFC_EVENT_CHECK_COND;
3637 fast_path_evt->un.check_cond_evt.scsi_event.lun =
3638 cmnd->device->lun;
3639 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
3640 &pnode->nlp_portname, sizeof(struct lpfc_name));
3641 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
3642 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3643 fast_path_evt->un.check_cond_evt.sense_key =
3644 cmnd->sense_buffer[2] & 0xf;
3645 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
3646 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
3647 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3648 fcpi_parm &&
3649 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
3650 ((scsi_status == SAM_STAT_GOOD) &&
3651 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
3652 /*
3653 * If status is good or resid does not match with fcp_param and
3654 * there is valid fcpi_parm, then there is a read_check error
3655 */
3656 fast_path_evt = lpfc_alloc_fast_evt(phba);
3657 if (!fast_path_evt)
3658 return;
3659 fast_path_evt->un.read_check_error.header.event_type =
3660 FC_REG_FABRIC_EVENT;
3661 fast_path_evt->un.read_check_error.header.subcategory =
3662 LPFC_EVENT_FCPRDCHKERR;
3663 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
3664 &pnode->nlp_portname, sizeof(struct lpfc_name));
3665 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
3666 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3667 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
3668 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
3669 fast_path_evt->un.read_check_error.fcpiparam =
3670 fcpi_parm;
3671 } else
3672 return;
3673
3674 fast_path_evt->vport = vport;
3675 spin_lock_irqsave(&phba->hbalock, flags);
3676 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
3677 spin_unlock_irqrestore(&phba->hbalock, flags);
3678 lpfc_worker_wake_up(phba);
3679 return;
3680}
James Smart9bad7672008-12-04 22:39:02 -05003681
3682/**
James Smartf1126682009-06-10 17:22:44 -04003683 * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
James Smart3772a992009-05-22 14:50:54 -04003684 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -05003685 * @psb: The scsi buffer which is going to be un-mapped.
3686 *
3687 * This routine does DMA un-mapping of scatter gather list of scsi command
James Smart3772a992009-05-22 14:50:54 -04003688 * field of @lpfc_cmd for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05003689 **/
dea31012005-04-17 16:05:31 -05003690static void
James Smartf1126682009-06-10 17:22:44 -04003691lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James Smartbcf4dbf2006-07-06 15:50:08 -04003692{
3693 /*
3694 * There are only two special cases to consider. (1) the scsi command
3695 * requested scatter-gather usage or (2) the scsi command allocated
3696 * a request buffer, but did not request use_sg. There is a third
3697 * case, but it does not require resource deallocation.
3698 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003699 if (psb->seg_cnt > 0)
3700 scsi_dma_unmap(psb->pCmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003701 if (psb->prot_seg_cnt > 0)
3702 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
3703 scsi_prot_sg_count(psb->pCmd),
3704 psb->pCmd->sc_data_direction);
James Smartbcf4dbf2006-07-06 15:50:08 -04003705}
3706
James Smart9bad7672008-12-04 22:39:02 -05003707/**
James Smart3621a712009-04-06 18:47:14 -04003708 * lpfc_handler_fcp_err - FCP response handler
James Smart9bad7672008-12-04 22:39:02 -05003709 * @vport: The virtual port for which this call is being executed.
3710 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
3711 * @rsp_iocb: The response IOCB which contains FCP error.
3712 *
3713 * This routine is called to process response IOCB with status field
3714 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
3715 * based upon SCSI and FCP error.
3716 **/
James Smartbcf4dbf2006-07-06 15:50:08 -04003717static void
James Smart2e0fef82007-06-17 19:56:36 -05003718lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
3719 struct lpfc_iocbq *rsp_iocb)
dea31012005-04-17 16:05:31 -05003720{
James Smart5afab6b2015-12-16 18:12:01 -05003721 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003722 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3723 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
3724 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
James Smart7054a602007-04-25 09:52:34 -04003725 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
dea31012005-04-17 16:05:31 -05003726 uint32_t resp_info = fcprsp->rspStatus2;
3727 uint32_t scsi_status = fcprsp->rspStatus3;
James Smartc7743952006-12-02 13:34:42 -05003728 uint32_t *lp;
dea31012005-04-17 16:05:31 -05003729 uint32_t host_status = DID_OK;
3730 uint32_t rsplen = 0;
James Smart5afab6b2015-12-16 18:12:01 -05003731 uint32_t fcpDl;
James Smartc7743952006-12-02 13:34:42 -05003732 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea31012005-04-17 16:05:31 -05003733
James Smartea2151b2008-09-07 11:52:10 -04003734
dea31012005-04-17 16:05:31 -05003735 /*
3736 * If this is a task management command, there is no
3737 * scsi packet associated with this lpfc_cmd. The driver
3738 * consumes it.
3739 */
3740 if (fcpcmd->fcpCntl2) {
3741 scsi_status = 0;
3742 goto out;
3743 }
3744
James Smart6a9c52c2009-10-02 15:16:51 -04003745 if (resp_info & RSP_LEN_VALID) {
3746 rsplen = be32_to_cpu(fcprsp->rspRspLen);
James Smarte40a02c2010-02-26 14:13:54 -05003747 if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
James Smart6a9c52c2009-10-02 15:16:51 -04003748 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3749 "2719 Invalid response length: "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02003750 "tgt x%x lun x%llx cmnd x%x rsplen x%x\n",
James Smart6a9c52c2009-10-02 15:16:51 -04003751 cmnd->device->id,
3752 cmnd->device->lun, cmnd->cmnd[0],
3753 rsplen);
3754 host_status = DID_ERROR;
3755 goto out;
3756 }
James Smarte40a02c2010-02-26 14:13:54 -05003757 if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
3758 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3759 "2757 Protocol failure detected during "
3760 "processing of FCP I/O op: "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02003761 "tgt x%x lun x%llx cmnd x%x rspInfo3 x%x\n",
James Smarte40a02c2010-02-26 14:13:54 -05003762 cmnd->device->id,
3763 cmnd->device->lun, cmnd->cmnd[0],
3764 fcprsp->rspInfo3);
3765 host_status = DID_ERROR;
3766 goto out;
3767 }
James Smart6a9c52c2009-10-02 15:16:51 -04003768 }
3769
James Smartc7743952006-12-02 13:34:42 -05003770 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
3771 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
3772 if (snslen > SCSI_SENSE_BUFFERSIZE)
3773 snslen = SCSI_SENSE_BUFFERSIZE;
3774
3775 if (resp_info & RSP_LEN_VALID)
3776 rsplen = be32_to_cpu(fcprsp->rspRspLen);
3777 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
3778 }
3779 lp = (uint32_t *)cmnd->sense_buffer;
3780
James Smartaa1c7ee2012-08-14 14:26:06 -04003781 /* special handling for under run conditions */
3782 if (!scsi_status && (resp_info & RESID_UNDER)) {
3783 /* don't log under runs if fcp set... */
3784 if (vport->cfg_log_verbose & LOG_FCP)
3785 logit = LOG_FCP_ERROR;
3786 /* unless operator says so */
3787 if (vport->cfg_log_verbose & LOG_FCP_UNDER)
3788 logit = LOG_FCP_UNDER;
3789 }
James Smartc7743952006-12-02 13:34:42 -05003790
James Smarte8b62012007-08-02 11:10:09 -04003791 lpfc_printf_vlog(vport, KERN_WARNING, logit,
James Smarte2a0a9d2008-12-04 22:40:02 -05003792 "9024 FCP command x%x failed: x%x SNS x%x x%x "
James Smarte8b62012007-08-02 11:10:09 -04003793 "Data: x%x x%x x%x x%x x%x\n",
3794 cmnd->cmnd[0], scsi_status,
3795 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
3796 be32_to_cpu(fcprsp->rspResId),
3797 be32_to_cpu(fcprsp->rspSnsLen),
3798 be32_to_cpu(fcprsp->rspRspLen),
3799 fcprsp->rspInfo3);
dea31012005-04-17 16:05:31 -05003800
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003801 scsi_set_resid(cmnd, 0);
James Smart5afab6b2015-12-16 18:12:01 -05003802 fcpDl = be32_to_cpu(fcpcmd->fcpDl);
dea31012005-04-17 16:05:31 -05003803 if (resp_info & RESID_UNDER) {
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003804 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea31012005-04-17 16:05:31 -05003805
James Smart73d91e52011-10-10 21:32:10 -04003806 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_UNDER,
James Smart45634a862018-01-30 15:59:00 -08003807 "9025 FCP Underrun, expected %d, "
James Smarte8b62012007-08-02 11:10:09 -04003808 "residual %d Data: x%x x%x x%x\n",
James Smart5afab6b2015-12-16 18:12:01 -05003809 fcpDl,
James Smarte8b62012007-08-02 11:10:09 -04003810 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
3811 cmnd->underflow);
dea31012005-04-17 16:05:31 -05003812
3813 /*
James Smart45634a862018-01-30 15:59:00 -08003814 * If there is an under run, check if under run reported by
James Smart7054a602007-04-25 09:52:34 -04003815 * storage array is same as the under run reported by HBA.
3816 * If this is not same, there is a dropped frame.
3817 */
James Smart45634a862018-01-30 15:59:00 -08003818 if (fcpi_parm && (scsi_get_resid(cmnd) != fcpi_parm)) {
James Smarte8b62012007-08-02 11:10:09 -04003819 lpfc_printf_vlog(vport, KERN_WARNING,
3820 LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05003821 "9026 FCP Read Check Error "
James Smarte8b62012007-08-02 11:10:09 -04003822 "and Underrun Data: x%x x%x x%x x%x\n",
James Smart5afab6b2015-12-16 18:12:01 -05003823 fcpDl,
James Smarte8b62012007-08-02 11:10:09 -04003824 scsi_get_resid(cmnd), fcpi_parm,
3825 cmnd->cmnd[0]);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003826 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
James Smart7054a602007-04-25 09:52:34 -04003827 host_status = DID_ERROR;
3828 }
3829 /*
dea31012005-04-17 16:05:31 -05003830 * The cmnd->underflow is the minimum number of bytes that must
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003831 * be transferred for this command. Provided a sense condition
dea31012005-04-17 16:05:31 -05003832 * is not present, make sure the actual amount transferred is at
3833 * least the underflow value or fail.
3834 */
3835 if (!(resp_info & SNS_LEN_VALID) &&
3836 (scsi_status == SAM_STAT_GOOD) &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003837 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
3838 < cmnd->underflow)) {
James Smarte8b62012007-08-02 11:10:09 -04003839 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05003840 "9027 FCP command x%x residual "
James Smarte8b62012007-08-02 11:10:09 -04003841 "underrun converted to error "
3842 "Data: x%x x%x x%x\n",
James Smart66dbfbe2007-08-05 06:08:38 -04003843 cmnd->cmnd[0], scsi_bufflen(cmnd),
James Smarte8b62012007-08-02 11:10:09 -04003844 scsi_get_resid(cmnd), cmnd->underflow);
dea31012005-04-17 16:05:31 -05003845 host_status = DID_ERROR;
3846 }
3847 } else if (resp_info & RESID_OVER) {
James Smarte8b62012007-08-02 11:10:09 -04003848 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05003849 "9028 FCP command x%x residual overrun error. "
James Smarte4e74272009-07-19 10:01:38 -04003850 "Data: x%x x%x\n", cmnd->cmnd[0],
James Smarte8b62012007-08-02 11:10:09 -04003851 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea31012005-04-17 16:05:31 -05003852 host_status = DID_ERROR;
3853
3854 /*
3855 * Check SLI validation that all the transfer was actually done
James Smart26373d22013-09-06 12:19:17 -04003856 * (fcpi_parm should be zero). Apply check only to reads.
dea31012005-04-17 16:05:31 -05003857 */
James Smart5afab6b2015-12-16 18:12:01 -05003858 } else if (fcpi_parm) {
James Smarte8b62012007-08-02 11:10:09 -04003859 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
James Smart5afab6b2015-12-16 18:12:01 -05003860 "9029 FCP %s Check Error xri x%x Data: "
James Smarteee88772010-09-29 11:19:08 -04003861 "x%x x%x x%x x%x x%x\n",
James Smart5afab6b2015-12-16 18:12:01 -05003862 ((cmnd->sc_data_direction == DMA_FROM_DEVICE) ?
3863 "Read" : "Write"),
3864 ((phba->sli_rev == LPFC_SLI_REV4) ?
3865 lpfc_cmd->cur_iocbq.sli4_xritag :
3866 rsp_iocb->iocb.ulpContext),
3867 fcpDl, be32_to_cpu(fcprsp->rspResId),
James Smarteee88772010-09-29 11:19:08 -04003868 fcpi_parm, cmnd->cmnd[0], scsi_status);
James Smart5afab6b2015-12-16 18:12:01 -05003869
3870 /* There is some issue with the LPe12000 that causes it
3871 * to miscalculate the fcpi_parm and falsely trip this
3872 * recovery logic. Detect this case and don't error when true.
3873 */
3874 if (fcpi_parm > fcpDl)
3875 goto out;
3876
James Smarteee88772010-09-29 11:19:08 -04003877 switch (scsi_status) {
3878 case SAM_STAT_GOOD:
3879 case SAM_STAT_CHECK_CONDITION:
3880 /* Fabric dropped a data frame. Fail any successful
3881 * command in which we detected dropped frames.
3882 * A status of good or some check conditions could
3883 * be considered a successful command.
3884 */
3885 host_status = DID_ERROR;
3886 break;
3887 }
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003888 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea31012005-04-17 16:05:31 -05003889 }
3890
3891 out:
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02003892 cmnd->result = host_status << 16 | scsi_status;
James Smartea2151b2008-09-07 11:52:10 -04003893 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
dea31012005-04-17 16:05:31 -05003894}
3895
James Smart9bad7672008-12-04 22:39:02 -05003896/**
James Smart8b0dff12015-05-22 10:42:38 -04003897 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
3898 * @phba: Pointer to HBA context object.
3899 *
3900 * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
3901 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
3902 * held.
3903 * If scsi-mq is enabled, get the default block layer mapping of software queues
3904 * to hardware queues. This information is saved in request tag.
3905 *
3906 * Return: index into SLI4 fast-path FCP queue index.
3907 **/
3908int lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba,
3909 struct lpfc_scsi_buf *lpfc_cmd)
3910{
3911 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3912 struct lpfc_vector_map_info *cpup;
3913 int chann, cpu;
3914 uint32_t tag;
3915 uint16_t hwq;
3916
Mauricio Faria de Oliveira05a05872016-06-07 20:13:08 -03003917 if (cmnd && shost_use_blk_mq(cmnd->device->host)) {
James Smart8b0dff12015-05-22 10:42:38 -04003918 tag = blk_mq_unique_tag(cmnd->request);
3919 hwq = blk_mq_unique_tag_to_hwq(tag);
3920
3921 return hwq;
3922 }
3923
3924 if (phba->cfg_fcp_io_sched == LPFC_FCP_SCHED_BY_CPU
3925 && phba->cfg_fcp_io_channel > 1) {
3926 cpu = smp_processor_id();
3927 if (cpu < phba->sli4_hba.num_present_cpu) {
3928 cpup = phba->sli4_hba.cpu_map;
3929 cpup += cpu;
3930 return cpup->channel_id;
3931 }
3932 }
3933 chann = atomic_add_return(1, &phba->fcp_qidx);
James Smart2ea259e2017-02-12 13:52:27 -08003934 chann = chann % phba->cfg_fcp_io_channel;
James Smart8b0dff12015-05-22 10:42:38 -04003935 return chann;
3936}
3937
3938
3939/**
James Smart3621a712009-04-06 18:47:14 -04003940 * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
James Smart9bad7672008-12-04 22:39:02 -05003941 * @phba: The Hba for which this call is being executed.
3942 * @pIocbIn: The command IOCBQ for the scsi cmnd.
James Smart3772a992009-05-22 14:50:54 -04003943 * @pIocbOut: The response IOCBQ for the scsi cmnd.
James Smart9bad7672008-12-04 22:39:02 -05003944 *
3945 * This routine assigns scsi command result by looking into response IOCB
3946 * status field appropriately. This routine handles QUEUE FULL condition as
3947 * well by ramping down device queue depth.
3948 **/
dea31012005-04-17 16:05:31 -05003949static void
3950lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
3951 struct lpfc_iocbq *pIocbOut)
3952{
3953 struct lpfc_scsi_buf *lpfc_cmd =
3954 (struct lpfc_scsi_buf *) pIocbIn->context1;
James Smart2e0fef82007-06-17 19:56:36 -05003955 struct lpfc_vport *vport = pIocbIn->vport;
dea31012005-04-17 16:05:31 -05003956 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
3957 struct lpfc_nodelist *pnode = rdata->pnode;
James Smart75baf692010-06-08 18:31:21 -04003958 struct scsi_cmnd *cmd;
James Smartfa61a542008-01-11 01:52:42 -05003959 unsigned long flags;
James Smartea2151b2008-09-07 11:52:10 -04003960 struct lpfc_fast_path_event *fast_path_evt;
James Smart75baf692010-06-08 18:31:21 -04003961 struct Scsi_Host *shost;
James Smart73d91e52011-10-10 21:32:10 -04003962 uint32_t logit = LOG_FCP;
dea31012005-04-17 16:05:31 -05003963
James Smart2cee7802017-06-01 21:07:02 -07003964 atomic_inc(&phba->fc4ScsiIoCmpls);
James Smart895427b2017-02-12 13:52:30 -08003965
James Smart75baf692010-06-08 18:31:21 -04003966 /* Sanity check on return of outstanding command */
James Smart75baf692010-06-08 18:31:21 -04003967 cmd = lpfc_cmd->pCmd;
James Smartc90261d2015-12-16 18:11:57 -05003968 if (!cmd)
3969 return;
James Smart75baf692010-06-08 18:31:21 -04003970 shost = cmd->device->host;
3971
James Smarte3d2b802012-08-14 14:25:43 -04003972 lpfc_cmd->result = (pIocbOut->iocb.un.ulpWord[4] & IOERR_PARAM_MASK);
dea31012005-04-17 16:05:31 -05003973 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
James Smart341af102010-01-26 23:07:37 -05003974 /* pick up SLI4 exhange busy status from HBA */
3975 lpfc_cmd->exch_busy = pIocbOut->iocb_flag & LPFC_EXCHANGE_BUSY;
3976
James Smart9a6b09c2012-03-01 22:37:42 -05003977#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
3978 if (lpfc_cmd->prot_data_type) {
3979 struct scsi_dif_tuple *src = NULL;
3980
3981 src = (struct scsi_dif_tuple *)lpfc_cmd->prot_data_segment;
3982 /*
3983 * Used to restore any changes to protection
3984 * data for error injection.
3985 */
3986 switch (lpfc_cmd->prot_data_type) {
3987 case LPFC_INJERR_REFTAG:
3988 src->ref_tag =
3989 lpfc_cmd->prot_data;
3990 break;
3991 case LPFC_INJERR_APPTAG:
3992 src->app_tag =
3993 (uint16_t)lpfc_cmd->prot_data;
3994 break;
3995 case LPFC_INJERR_GUARD:
3996 src->guard_tag =
3997 (uint16_t)lpfc_cmd->prot_data;
3998 break;
3999 default:
4000 break;
4001 }
4002
4003 lpfc_cmd->prot_data = 0;
4004 lpfc_cmd->prot_data_type = 0;
4005 lpfc_cmd->prot_data_segment = NULL;
4006 }
4007#endif
James Smart2ea259e2017-02-12 13:52:27 -08004008
dea31012005-04-17 16:05:31 -05004009 if (lpfc_cmd->status) {
4010 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
4011 (lpfc_cmd->result & IOERR_DRVR_MASK))
4012 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
4013 else if (lpfc_cmd->status >= IOSTAT_CNT)
4014 lpfc_cmd->status = IOSTAT_DEFAULT;
James Smartaa1c7ee2012-08-14 14:26:06 -04004015 if (lpfc_cmd->status == IOSTAT_FCP_RSP_ERROR &&
4016 !lpfc_cmd->fcp_rsp->rspStatus3 &&
4017 (lpfc_cmd->fcp_rsp->rspStatus2 & RESID_UNDER) &&
4018 !(vport->cfg_log_verbose & LOG_FCP_UNDER))
James Smart73d91e52011-10-10 21:32:10 -04004019 logit = 0;
4020 else
4021 logit = LOG_FCP | LOG_FCP_UNDER;
4022 lpfc_printf_vlog(vport, KERN_WARNING, logit,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004023 "9030 FCP cmd x%x failed <%d/%lld> "
James Smart5a0d80f2012-05-09 21:18:20 -04004024 "status: x%x result: x%x "
4025 "sid: x%x did: x%x oxid: x%x "
4026 "Data: x%x x%x\n",
James Smart73d91e52011-10-10 21:32:10 -04004027 cmd->cmnd[0],
4028 cmd->device ? cmd->device->id : 0xffff,
4029 cmd->device ? cmd->device->lun : 0xffff,
4030 lpfc_cmd->status, lpfc_cmd->result,
James Smart3bf41ba2013-05-31 17:03:18 -04004031 vport->fc_myDID,
4032 (pnode) ? pnode->nlp_DID : 0,
James Smart5a0d80f2012-05-09 21:18:20 -04004033 phba->sli_rev == LPFC_SLI_REV4 ?
4034 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
James Smart73d91e52011-10-10 21:32:10 -04004035 pIocbOut->iocb.ulpContext,
4036 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
dea31012005-04-17 16:05:31 -05004037
4038 switch (lpfc_cmd->status) {
4039 case IOSTAT_FCP_RSP_ERROR:
4040 /* Call FCP RSP handler to determine result */
James Smart2e0fef82007-06-17 19:56:36 -05004041 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea31012005-04-17 16:05:31 -05004042 break;
4043 case IOSTAT_NPORT_BSY:
4044 case IOSTAT_FABRIC_BSY:
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02004045 cmd->result = DID_TRANSPORT_DISRUPTED << 16;
James Smartea2151b2008-09-07 11:52:10 -04004046 fast_path_evt = lpfc_alloc_fast_evt(phba);
4047 if (!fast_path_evt)
4048 break;
4049 fast_path_evt->un.fabric_evt.event_type =
4050 FC_REG_FABRIC_EVENT;
4051 fast_path_evt->un.fabric_evt.subcategory =
4052 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
4053 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
4054 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
4055 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
4056 &pnode->nlp_portname,
4057 sizeof(struct lpfc_name));
4058 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
4059 &pnode->nlp_nodename,
4060 sizeof(struct lpfc_name));
4061 }
4062 fast_path_evt->vport = vport;
4063 fast_path_evt->work_evt.evt =
4064 LPFC_EVT_FASTPATH_MGMT_EVT;
4065 spin_lock_irqsave(&phba->hbalock, flags);
4066 list_add_tail(&fast_path_evt->work_evt.evt_listp,
4067 &phba->work_list);
4068 spin_unlock_irqrestore(&phba->hbalock, flags);
4069 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05004070 break;
James Smart92d7f7b2007-06-17 19:56:38 -05004071 case IOSTAT_LOCAL_REJECT:
James Smart1151e3e2011-02-16 12:39:35 -05004072 case IOSTAT_REMOTE_STOP:
James Smartab56dc22011-02-16 12:39:57 -05004073 if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
4074 lpfc_cmd->result ==
4075 IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
4076 lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
4077 lpfc_cmd->result ==
4078 IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02004079 cmd->result = DID_NO_CONNECT << 16;
James Smartab56dc22011-02-16 12:39:57 -05004080 break;
4081 }
James Smartd7c255b2008-08-24 21:50:00 -04004082 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
James Smart92d7f7b2007-06-17 19:56:38 -05004083 lpfc_cmd->result == IOERR_NO_RESOURCES ||
James Smartb92938b2010-06-07 15:24:12 -04004084 lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
4085 lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02004086 cmd->result = DID_REQUEUE << 16;
James Smart58da1ff2008-04-07 10:15:56 -04004087 break;
James Smarte2a0a9d2008-12-04 22:40:02 -05004088 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004089 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
4090 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
4091 pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
4092 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
4093 /*
4094 * This is a response for a BG enabled
4095 * cmd. Parse BG error
4096 */
4097 lpfc_parse_bg_err(phba, lpfc_cmd,
4098 pIocbOut);
4099 break;
4100 } else {
4101 lpfc_printf_vlog(vport, KERN_WARNING,
4102 LOG_BG,
4103 "9031 non-zero BGSTAT "
James Smart6a9c52c2009-10-02 15:16:51 -04004104 "on unprotected cmd\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05004105 }
4106 }
James Smart1151e3e2011-02-16 12:39:35 -05004107 if ((lpfc_cmd->status == IOSTAT_REMOTE_STOP)
4108 && (phba->sli_rev == LPFC_SLI_REV4)
4109 && (pnode && NLP_CHK_NODE_ACT(pnode))) {
4110 /* This IO was aborted by the target, we don't
4111 * know the rxid and because we did not send the
4112 * ABTS we cannot generate and RRQ.
4113 */
4114 lpfc_set_rrq_active(phba, pnode,
James Smartee0f4fe2012-05-09 21:19:14 -04004115 lpfc_cmd->cur_iocbq.sli4_lxritag,
4116 0, 0);
James Smart1151e3e2011-02-16 12:39:35 -05004117 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004118 /* else: fall through */
dea31012005-04-17 16:05:31 -05004119 default:
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02004120 cmd->result = DID_ERROR << 16;
dea31012005-04-17 16:05:31 -05004121 break;
4122 }
4123
James Smart58da1ff2008-04-07 10:15:56 -04004124 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004125 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02004126 cmd->result = DID_TRANSPORT_DISRUPTED << 16 |
4127 SAM_STAT_BUSY;
James Smartab56dc22011-02-16 12:39:57 -05004128 } else
Johannes Thumshirnc6668ca2018-07-05 13:01:39 +02004129 cmd->result = DID_OK << 16;
dea31012005-04-17 16:05:31 -05004130
4131 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
4132 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
4133
James Smarte8b62012007-08-02 11:10:09 -04004134 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004135 "0710 Iodone <%d/%llu> cmd %p, error "
James Smarte8b62012007-08-02 11:10:09 -04004136 "x%x SNS x%x x%x Data: x%x x%x\n",
4137 cmd->device->id, cmd->device->lun, cmd,
4138 cmd->result, *lp, *(lp + 3), cmd->retries,
4139 scsi_get_resid(cmd));
dea31012005-04-17 16:05:31 -05004140 }
4141
James Smartea2151b2008-09-07 11:52:10 -04004142 lpfc_update_stats(phba, lpfc_cmd);
James Smart977b5a02008-09-07 11:52:04 -04004143 if (vport->cfg_max_scsicmpl_time &&
4144 time_after(jiffies, lpfc_cmd->start_time +
4145 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
James Smarta257bf92009-04-06 18:48:10 -04004146 spin_lock_irqsave(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05004147 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
4148 if (pnode->cmd_qdepth >
4149 atomic_read(&pnode->cmd_pending) &&
4150 (atomic_read(&pnode->cmd_pending) >
4151 LPFC_MIN_TGT_QDEPTH) &&
4152 ((cmd->cmnd[0] == READ_10) ||
4153 (cmd->cmnd[0] == WRITE_10)))
4154 pnode->cmd_qdepth =
4155 atomic_read(&pnode->cmd_pending);
James Smart977b5a02008-09-07 11:52:04 -04004156
James Smart109f6ed2008-12-04 22:39:08 -05004157 pnode->last_change_time = jiffies;
4158 }
James Smarta257bf92009-04-06 18:48:10 -04004159 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart977b5a02008-09-07 11:52:04 -04004160 }
James Smart1dcb58e2007-04-25 09:51:30 -04004161 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James Smarta257bf92009-04-06 18:48:10 -04004162
James Smartca7fb762018-09-10 10:30:44 -07004163 /* If pCmd was set to NULL from abort path, do not call scsi_done */
4164 if (xchg(&lpfc_cmd->pCmd, NULL) == NULL) {
4165 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4166 "0711 FCP cmd already NULL, sid: 0x%06x, "
4167 "did: 0x%06x, oxid: 0x%04x\n",
4168 vport->fc_myDID,
4169 (pnode) ? pnode->nlp_DID : 0,
4170 phba->sli_rev == LPFC_SLI_REV4 ?
4171 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff);
4172 return;
4173 }
James Smart92e3af62012-08-14 14:26:28 -04004174
James Smart89533e92016-10-13 15:06:15 -07004175 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
4176 cmd->scsi_done(cmd);
4177
James Smartfa61a542008-01-11 01:52:42 -05004178 /*
4179 * If there is a thread waiting for command completion
4180 * wake up the thread.
4181 */
James Smarta257bf92009-04-06 18:48:10 -04004182 spin_lock_irqsave(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05004183 if (lpfc_cmd->waitq)
4184 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04004185 spin_unlock_irqrestore(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05004186
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004187 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004188}
4189
James Smart34b02dc2008-08-24 21:49:55 -04004190/**
Alexey Kardashevskiy8b2564e2015-04-28 18:26:22 +10004191 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
4192 * @data: A pointer to the immediate command data portion of the IOCB.
4193 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
4194 *
4195 * The routine copies the entire FCP command from @fcp_cmnd to @data while
4196 * byte swapping the data to big endian format for transmission on the wire.
4197 **/
4198static void
4199lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
4200{
4201 int i, j;
4202 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
4203 i += sizeof(uint32_t), j++) {
4204 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
4205 }
4206}
4207
4208/**
James Smartf1126682009-06-10 17:22:44 -04004209 * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05004210 * @vport: The virtual port for which this call is being executed.
4211 * @lpfc_cmd: The scsi command which needs to send.
4212 * @pnode: Pointer to lpfc_nodelist.
4213 *
4214 * This routine initializes fcp_cmnd and iocb data structure from scsi command
James Smart3772a992009-05-22 14:50:54 -04004215 * to transfer for device with SLI3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05004216 **/
dea31012005-04-17 16:05:31 -05004217static void
James Smartf1126682009-06-10 17:22:44 -04004218lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
James Smart2e0fef82007-06-17 19:56:36 -05004219 struct lpfc_nodelist *pnode)
dea31012005-04-17 16:05:31 -05004220{
James Smart2e0fef82007-06-17 19:56:36 -05004221 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004222 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
4223 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
4224 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
4225 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
4226 int datadir = scsi_cmnd->sc_data_direction;
James Smart027140e2012-08-03 12:35:44 -04004227 uint8_t *ptr;
4228 bool sli4;
James Smart98bbf5f2013-09-06 12:18:45 -04004229 uint32_t fcpdl;
dea31012005-04-17 16:05:31 -05004230
James Smart58da1ff2008-04-07 10:15:56 -04004231 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
4232 return;
4233
dea31012005-04-17 16:05:31 -05004234 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
James.Smart@Emulex.Com69859dc2005-08-10 15:02:37 -04004235 /* clear task management bits */
4236 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea31012005-04-17 16:05:31 -05004237
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -04004238 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
4239 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05004240
James Smart027140e2012-08-03 12:35:44 -04004241 ptr = &fcp_cmnd->fcpCdb[0];
4242 memcpy(ptr, scsi_cmnd->cmnd, scsi_cmnd->cmd_len);
4243 if (scsi_cmnd->cmd_len < LPFC_FCP_CDB_LEN) {
4244 ptr += scsi_cmnd->cmd_len;
4245 memset(ptr, 0, (LPFC_FCP_CDB_LEN - scsi_cmnd->cmd_len));
4246 }
4247
Christoph Hellwig50668632014-10-30 14:30:06 +01004248 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
dea31012005-04-17 16:05:31 -05004249
James Smart027140e2012-08-03 12:35:44 -04004250 sli4 = (phba->sli_rev == LPFC_SLI_REV4);
James Smart6acb3482014-04-04 13:51:25 -04004251 piocbq->iocb.un.fcpi.fcpi_XRdy = 0;
James Smart027140e2012-08-03 12:35:44 -04004252
dea31012005-04-17 16:05:31 -05004253 /*
4254 * There are three possibilities here - use scatter-gather segment, use
4255 * the single mapping, or neither. Start the lpfc command prep by
4256 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
4257 * data bde entry.
4258 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05004259 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05004260 if (datadir == DMA_TO_DEVICE) {
4261 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
James Smart182ba752013-07-15 18:34:05 -04004262 iocb_cmd->ulpPU = PARM_READ_CHECK;
James Smart3cb01c52013-07-15 18:35:04 -04004263 if (vport->cfg_first_burst_size &&
4264 (pnode->nlp_flag & NLP_FIRSTBURST)) {
James Smart98bbf5f2013-09-06 12:18:45 -04004265 fcpdl = scsi_bufflen(scsi_cmnd);
4266 if (fcpdl < vport->cfg_first_burst_size)
4267 piocbq->iocb.un.fcpi.fcpi_XRdy = fcpdl;
4268 else
4269 piocbq->iocb.un.fcpi.fcpi_XRdy =
4270 vport->cfg_first_burst_size;
James Smart3cb01c52013-07-15 18:35:04 -04004271 }
dea31012005-04-17 16:05:31 -05004272 fcp_cmnd->fcpCntl3 = WRITE_DATA;
James Smart2cee7802017-06-01 21:07:02 -07004273 atomic_inc(&phba->fc4ScsiOutputRequests);
dea31012005-04-17 16:05:31 -05004274 } else {
4275 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
4276 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05004277 fcp_cmnd->fcpCntl3 = READ_DATA;
James Smart2cee7802017-06-01 21:07:02 -07004278 atomic_inc(&phba->fc4ScsiInputRequests);
dea31012005-04-17 16:05:31 -05004279 }
4280 } else {
4281 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
4282 iocb_cmd->un.fcpi.fcpi_parm = 0;
4283 iocb_cmd->ulpPU = 0;
4284 fcp_cmnd->fcpCntl3 = 0;
James Smart2cee7802017-06-01 21:07:02 -07004285 atomic_inc(&phba->fc4ScsiControlRequests);
dea31012005-04-17 16:05:31 -05004286 }
Alexey Kardashevskiy8b2564e2015-04-28 18:26:22 +10004287 if (phba->sli_rev == 3 &&
4288 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
4289 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05004290 /*
4291 * Finish initializing those IOCB fields that are independent
4292 * of the scsi_cmnd request_buffer
4293 */
4294 piocbq->iocb.ulpContext = pnode->nlp_rpi;
James Smart027140e2012-08-03 12:35:44 -04004295 if (sli4)
James Smart6d368e52011-05-24 11:44:12 -04004296 piocbq->iocb.ulpContext =
4297 phba->sli4_hba.rpi_ids[pnode->nlp_rpi];
dea31012005-04-17 16:05:31 -05004298 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
4299 piocbq->iocb.ulpFCP2Rcvy = 1;
James Smart09372822008-01-11 01:52:54 -05004300 else
4301 piocbq->iocb.ulpFCP2Rcvy = 0;
dea31012005-04-17 16:05:31 -05004302
4303 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
4304 piocbq->context1 = lpfc_cmd;
4305 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
4306 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
James Smart2e0fef82007-06-17 19:56:36 -05004307 piocbq->vport = vport;
dea31012005-04-17 16:05:31 -05004308}
4309
James Smart9bad7672008-12-04 22:39:02 -05004310/**
James Smart6d368e52011-05-24 11:44:12 -04004311 * lpfc_scsi_prep_task_mgmt_cmd - Convert SLI3 scsi TM cmd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05004312 * @vport: The virtual port for which this call is being executed.
4313 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
4314 * @lun: Logical unit number.
4315 * @task_mgmt_cmd: SCSI task management command.
4316 *
James Smart3772a992009-05-22 14:50:54 -04004317 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
4318 * for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05004319 *
4320 * Return codes:
4321 * 0 - Error
4322 * 1 - Success
4323 **/
dea31012005-04-17 16:05:31 -05004324static int
James Smartf1126682009-06-10 17:22:44 -04004325lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
dea31012005-04-17 16:05:31 -05004326 struct lpfc_scsi_buf *lpfc_cmd,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004327 uint64_t lun,
dea31012005-04-17 16:05:31 -05004328 uint8_t task_mgmt_cmd)
4329{
dea31012005-04-17 16:05:31 -05004330 struct lpfc_iocbq *piocbq;
4331 IOCB_t *piocb;
4332 struct fcp_cmnd *fcp_cmnd;
James Smart0b18ac42006-05-01 21:50:40 -04004333 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea31012005-04-17 16:05:31 -05004334 struct lpfc_nodelist *ndlp = rdata->pnode;
4335
James Smart58da1ff2008-04-07 10:15:56 -04004336 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
4337 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05004338 return 0;
dea31012005-04-17 16:05:31 -05004339
dea31012005-04-17 16:05:31 -05004340 piocbq = &(lpfc_cmd->cur_iocbq);
James Smart2e0fef82007-06-17 19:56:36 -05004341 piocbq->vport = vport;
4342
dea31012005-04-17 16:05:31 -05004343 piocb = &piocbq->iocb;
4344
4345 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart34b02dc2008-08-24 21:49:55 -04004346 /* Clear out any old data in the FCP command area */
4347 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
4348 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05004349 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
James Smarte2a0a9d2008-12-04 22:40:02 -05004350 if (vport->phba->sli_rev == 3 &&
4351 !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04004352 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05004353 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
dea31012005-04-17 16:05:31 -05004354 piocb->ulpContext = ndlp->nlp_rpi;
James Smart6d368e52011-05-24 11:44:12 -04004355 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
4356 piocb->ulpContext =
4357 vport->phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
4358 }
James Smart53151bb2013-10-10 12:24:07 -04004359 piocb->ulpFCP2Rcvy = (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) ? 1 : 0;
dea31012005-04-17 16:05:31 -05004360 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
James Smartf9226c22014-02-20 09:57:28 -05004361 piocb->ulpPU = 0;
4362 piocb->un.fcpi.fcpi_parm = 0;
dea31012005-04-17 16:05:31 -05004363
4364 /* ulpTimeout is only one byte */
4365 if (lpfc_cmd->timeout > 0xff) {
4366 /*
4367 * Do not timeout the command at the firmware level.
4368 * The driver will provide the timeout mechanism.
4369 */
4370 piocb->ulpTimeout = 0;
James Smartf1126682009-06-10 17:22:44 -04004371 } else
dea31012005-04-17 16:05:31 -05004372 piocb->ulpTimeout = lpfc_cmd->timeout;
James Smartf1126682009-06-10 17:22:44 -04004373
4374 if (vport->phba->sli_rev == LPFC_SLI_REV4)
4375 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004376
James Smart2e0fef82007-06-17 19:56:36 -05004377 return 1;
dea31012005-04-17 16:05:31 -05004378}
4379
James Smart9bad7672008-12-04 22:39:02 -05004380/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004381 * lpfc_scsi_api_table_setup - Set up scsi api function jump table
James Smart3772a992009-05-22 14:50:54 -04004382 * @phba: The hba struct for which this call is being executed.
4383 * @dev_grp: The HBA PCI-Device group number.
4384 *
4385 * This routine sets up the SCSI interface API function jump table in @phba
4386 * struct.
4387 * Returns: 0 - success, -ENODEV - failure.
4388 **/
4389int
4390lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4391{
4392
James Smartf1126682009-06-10 17:22:44 -04004393 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
4394 phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
James Smartf1126682009-06-10 17:22:44 -04004395
James Smart3772a992009-05-22 14:50:54 -04004396 switch (dev_grp) {
4397 case LPFC_PCI_DEV_LP:
4398 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s3;
4399 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
James Smartacd68592012-01-18 16:25:09 -05004400 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04004401 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
James Smart19ca7602010-11-20 23:11:55 -05004402 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04004403 break;
James Smartda0436e2009-05-22 14:51:39 -04004404 case LPFC_PCI_DEV_OC:
4405 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s4;
4406 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
James Smartacd68592012-01-18 16:25:09 -05004407 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04004408 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
James Smart19ca7602010-11-20 23:11:55 -05004409 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04004410 break;
James Smart3772a992009-05-22 14:50:54 -04004411 default:
4412 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4413 "1418 Invalid HBA PCI-device group: 0x%x\n",
4414 dev_grp);
4415 return -ENODEV;
4416 break;
4417 }
James Smart3772a992009-05-22 14:50:54 -04004418 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
James Smart84d1b002010-02-12 14:42:33 -05004419 phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
James Smart3772a992009-05-22 14:50:54 -04004420 return 0;
4421}
4422
4423/**
James Smart3621a712009-04-06 18:47:14 -04004424 * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
James Smart9bad7672008-12-04 22:39:02 -05004425 * @phba: The Hba for which this call is being executed.
4426 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
4427 * @rspiocbq: Pointer to lpfc_iocbq data structure.
4428 *
4429 * This routine is IOCB completion routine for device reset and target reset
4430 * routine. This routine release scsi buffer associated with lpfc_cmd.
4431 **/
James Smart7054a602007-04-25 09:52:34 -04004432static void
4433lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
4434 struct lpfc_iocbq *cmdiocbq,
4435 struct lpfc_iocbq *rspiocbq)
4436{
4437 struct lpfc_scsi_buf *lpfc_cmd =
4438 (struct lpfc_scsi_buf *) cmdiocbq->context1;
4439 if (lpfc_cmd)
4440 lpfc_release_scsi_buf(phba, lpfc_cmd);
4441 return;
4442}
4443
James Smart9bad7672008-12-04 22:39:02 -05004444/**
James Smart3621a712009-04-06 18:47:14 -04004445 * lpfc_info - Info entry point of scsi_host_template data structure
James Smart9bad7672008-12-04 22:39:02 -05004446 * @host: The scsi host for which this call is being executed.
4447 *
4448 * This routine provides module information about hba.
4449 *
4450 * Reutrn code:
4451 * Pointer to char - Success.
4452 **/
dea31012005-04-17 16:05:31 -05004453const char *
4454lpfc_info(struct Scsi_Host *host)
4455{
James Smart2e0fef82007-06-17 19:56:36 -05004456 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
4457 struct lpfc_hba *phba = vport->phba;
James Smart8b68cd52012-09-29 11:32:37 -04004458 int len, link_speed = 0;
dea31012005-04-17 16:05:31 -05004459 static char lpfcinfobuf[384];
4460
4461 memset(lpfcinfobuf,0,384);
4462 if (phba && phba->pcidev){
4463 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
4464 len = strlen(lpfcinfobuf);
4465 snprintf(lpfcinfobuf + len,
4466 384-len,
4467 " on PCI bus %02x device %02x irq %d",
4468 phba->pcidev->bus->number,
4469 phba->pcidev->devfn,
4470 phba->pcidev->irq);
4471 len = strlen(lpfcinfobuf);
4472 if (phba->Port[0]) {
4473 snprintf(lpfcinfobuf + len,
4474 384-len,
4475 " port %s",
4476 phba->Port);
4477 }
James Smart65467b62010-01-26 23:08:29 -05004478 len = strlen(lpfcinfobuf);
James Smarta085e872015-12-16 18:12:02 -05004479 link_speed = lpfc_sli_port_speed_get(phba);
James Smart8b68cd52012-09-29 11:32:37 -04004480 if (link_speed != 0)
4481 snprintf(lpfcinfobuf + len, 384-len,
4482 " Logical Link Speed: %d Mbps", link_speed);
dea31012005-04-17 16:05:31 -05004483 }
4484 return lpfcinfobuf;
4485}
4486
James Smart9bad7672008-12-04 22:39:02 -05004487/**
James Smart3621a712009-04-06 18:47:14 -04004488 * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
James Smart9bad7672008-12-04 22:39:02 -05004489 * @phba: The Hba for which this call is being executed.
4490 *
4491 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
4492 * The default value of cfg_poll_tmo is 10 milliseconds.
4493 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004494static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
4495{
4496 unsigned long poll_tmo_expires =
4497 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
4498
James Smart895427b2017-02-12 13:52:30 -08004499 if (!list_empty(&phba->sli.sli3_ring[LPFC_FCP_RING].txcmplq))
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004500 mod_timer(&phba->fcp_poll_timer,
4501 poll_tmo_expires);
4502}
4503
James Smart9bad7672008-12-04 22:39:02 -05004504/**
James Smart3621a712009-04-06 18:47:14 -04004505 * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
James Smart9bad7672008-12-04 22:39:02 -05004506 * @phba: The Hba for which this call is being executed.
4507 *
4508 * This routine starts the fcp_poll_timer of @phba.
4509 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004510void lpfc_poll_start_timer(struct lpfc_hba * phba)
4511{
4512 lpfc_poll_rearm_timer(phba);
4513}
4514
James Smart9bad7672008-12-04 22:39:02 -05004515/**
James Smart3621a712009-04-06 18:47:14 -04004516 * lpfc_poll_timeout - Restart polling timer
James Smart9bad7672008-12-04 22:39:02 -05004517 * @ptr: Map to lpfc_hba data structure pointer.
4518 *
4519 * This routine restarts fcp_poll timer, when FCP ring polling is enable
4520 * and FCP Ring interrupt is disable.
4521 **/
4522
Kees Cookf22eb4d2017-09-06 20:24:26 -07004523void lpfc_poll_timeout(struct timer_list *t)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004524{
Kees Cookf22eb4d2017-09-06 20:24:26 -07004525 struct lpfc_hba *phba = from_timer(phba, t, fcp_poll_timer);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004526
4527 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004528 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -08004529 &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
James Smart45ed1192009-10-02 15:17:02 -04004530
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004531 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4532 lpfc_poll_rearm_timer(phba);
4533 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004534}
4535
James Smart9bad7672008-12-04 22:39:02 -05004536/**
James Smart3621a712009-04-06 18:47:14 -04004537 * lpfc_queuecommand - scsi_host_template queuecommand entry point
James Smart9bad7672008-12-04 22:39:02 -05004538 * @cmnd: Pointer to scsi_cmnd data structure.
4539 * @done: Pointer to done routine.
4540 *
4541 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
4542 * This routine prepares an IOCB from scsi command and provides to firmware.
4543 * The @done callback is invoked after driver finished processing the command.
4544 *
4545 * Return value :
4546 * 0 - Success
4547 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
4548 **/
dea31012005-04-17 16:05:31 -05004549static int
James Smartb9a7c632012-08-03 12:35:24 -04004550lpfc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004551{
James Smart2e0fef82007-06-17 19:56:36 -05004552 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4553 struct lpfc_hba *phba = vport->phba;
James Smart1ba981f2014-02-20 09:56:45 -05004554 struct lpfc_rport_data *rdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05004555 struct lpfc_nodelist *ndlp;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004556 struct lpfc_scsi_buf *lpfc_cmd;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004557 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004558 int err;
dea31012005-04-17 16:05:31 -05004559
James Smart1ba981f2014-02-20 09:56:45 -05004560 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smartb0e83012018-06-26 08:24:29 -07004561
4562 /* sanity check on references */
4563 if (unlikely(!rdata) || unlikely(!rport))
4564 goto out_fail_command;
4565
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004566 err = fc_remote_port_chkready(rport);
4567 if (err) {
4568 cmnd->result = err;
dea31012005-04-17 16:05:31 -05004569 goto out_fail_command;
4570 }
James Smart1c6f4ef52009-11-18 15:40:49 -05004571 ndlp = rdata->pnode;
dea31012005-04-17 16:05:31 -05004572
James Smartbf086112011-08-21 21:48:13 -04004573 if ((scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) &&
James Smartacd68592012-01-18 16:25:09 -05004574 (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED))) {
James Smarte2a0a9d2008-12-04 22:40:02 -05004575
James Smart6a9c52c2009-10-02 15:16:51 -04004576 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4577 "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
4578 " op:%02x str=%s without registering for"
4579 " BlockGuard - Rejecting command\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05004580 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
4581 dif_op_str[scsi_get_prot_op(cmnd)]);
4582 goto out_fail_command;
4583 }
4584
dea31012005-04-17 16:05:31 -05004585 /*
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004586 * Catch race where our node has transitioned, but the
4587 * transport is still transitioning.
dea31012005-04-17 16:05:31 -05004588 */
James Smart6b415f52012-06-12 13:54:59 -04004589 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
4590 goto out_tgt_busy;
James Smart2a5b7d62018-07-31 17:23:22 -07004591 if (lpfc_ndlp_check_qdepth(phba, ndlp)) {
4592 if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth) {
4593 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_ERROR,
4594 "3377 Target Queue Full, scsi Id:%d "
4595 "Qdepth:%d Pending command:%d"
4596 " WWNN:%02x:%02x:%02x:%02x:"
4597 "%02x:%02x:%02x:%02x, "
4598 " WWPN:%02x:%02x:%02x:%02x:"
4599 "%02x:%02x:%02x:%02x",
4600 ndlp->nlp_sid, ndlp->cmd_qdepth,
4601 atomic_read(&ndlp->cmd_pending),
4602 ndlp->nlp_nodename.u.wwn[0],
4603 ndlp->nlp_nodename.u.wwn[1],
4604 ndlp->nlp_nodename.u.wwn[2],
4605 ndlp->nlp_nodename.u.wwn[3],
4606 ndlp->nlp_nodename.u.wwn[4],
4607 ndlp->nlp_nodename.u.wwn[5],
4608 ndlp->nlp_nodename.u.wwn[6],
4609 ndlp->nlp_nodename.u.wwn[7],
4610 ndlp->nlp_portname.u.wwn[0],
4611 ndlp->nlp_portname.u.wwn[1],
4612 ndlp->nlp_portname.u.wwn[2],
4613 ndlp->nlp_portname.u.wwn[3],
4614 ndlp->nlp_portname.u.wwn[4],
4615 ndlp->nlp_portname.u.wwn[5],
4616 ndlp->nlp_portname.u.wwn[6],
4617 ndlp->nlp_portname.u.wwn[7]);
4618 goto out_tgt_busy;
4619 }
James Smart64bf0092018-01-30 15:58:53 -08004620 }
James Smartf91bc592018-04-09 14:24:22 -07004621
James Smart19ca7602010-11-20 23:11:55 -05004622 lpfc_cmd = lpfc_get_scsi_buf(phba, ndlp);
dea31012005-04-17 16:05:31 -05004623 if (lpfc_cmd == NULL) {
James Smarteaf15d52008-12-04 22:39:29 -05004624 lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05004625
James Smart895427b2017-02-12 13:52:30 -08004626 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_ERROR,
James Smarte8b62012007-08-02 11:10:09 -04004627 "0707 driver's buffer pool is empty, "
4628 "IO busied\n");
dea31012005-04-17 16:05:31 -05004629 goto out_host_busy;
4630 }
4631
4632 /*
4633 * Store the midlayer's command structure for the completion phase
4634 * and complete the command initialization.
4635 */
4636 lpfc_cmd->pCmd = cmnd;
4637 lpfc_cmd->rdata = rdata;
James Smart2a5b7d62018-07-31 17:23:22 -07004638 lpfc_cmd->ndlp = ndlp;
dea31012005-04-17 16:05:31 -05004639 lpfc_cmd->timeout = 0;
James Smart977b5a02008-09-07 11:52:04 -04004640 lpfc_cmd->start_time = jiffies;
dea31012005-04-17 16:05:31 -05004641 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
dea31012005-04-17 16:05:31 -05004642
James Smarte2a0a9d2008-12-04 22:40:02 -05004643 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
James Smart6a9c52c2009-10-02 15:16:51 -04004644 if (vport->phba->cfg_enable_bg) {
James Smart737d4242013-04-17 20:14:49 -04004645 lpfc_printf_vlog(vport,
4646 KERN_INFO, LOG_SCSI_CMD,
James Smart26134702012-08-14 14:25:50 -04004647 "9033 BLKGRD: rcvd %s cmd:x%x "
4648 "sector x%llx cnt %u pt %x\n",
4649 dif_op_str[scsi_get_prot_op(cmnd)],
4650 cmnd->cmnd[0],
4651 (unsigned long long)scsi_get_lba(cmnd),
4652 blk_rq_sectors(cmnd->request),
4653 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004654 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004655 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
4656 } else {
James Smart6a9c52c2009-10-02 15:16:51 -04004657 if (vport->phba->cfg_enable_bg) {
James Smart737d4242013-04-17 20:14:49 -04004658 lpfc_printf_vlog(vport,
4659 KERN_INFO, LOG_SCSI_CMD,
James Smart26134702012-08-14 14:25:50 -04004660 "9038 BLKGRD: rcvd PROT_NORMAL cmd: "
4661 "x%x sector x%llx cnt %u pt %x\n",
4662 cmnd->cmnd[0],
4663 (unsigned long long)scsi_get_lba(cmnd),
James Smart9a6b09c2012-03-01 22:37:42 -05004664 blk_rq_sectors(cmnd->request),
James Smart26134702012-08-14 14:25:50 -04004665 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004666 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004667 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
4668 }
4669
dea31012005-04-17 16:05:31 -05004670 if (err)
4671 goto out_host_busy_free_buf;
4672
James Smart2e0fef82007-06-17 19:56:36 -05004673 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea31012005-04-17 16:05:31 -05004674
James Smart3772a992009-05-22 14:50:54 -04004675 err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
James Smart92d7f7b2007-06-17 19:56:38 -05004676 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
James Smarteaf15d52008-12-04 22:39:29 -05004677 if (err) {
James Smart76f96b62013-12-17 20:29:01 -05004678 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4679 "3376 FCP could not issue IOCB err %x"
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004680 "FCP cmd x%x <%d/%llu> "
James Smart76f96b62013-12-17 20:29:01 -05004681 "sid: x%x did: x%x oxid: x%x "
4682 "Data: x%x x%x x%x x%x\n",
4683 err, cmnd->cmnd[0],
4684 cmnd->device ? cmnd->device->id : 0xffff,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004685 cmnd->device ? cmnd->device->lun : (u64) -1,
James Smart76f96b62013-12-17 20:29:01 -05004686 vport->fc_myDID, ndlp->nlp_DID,
4687 phba->sli_rev == LPFC_SLI_REV4 ?
4688 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
4689 lpfc_cmd->cur_iocbq.iocb.ulpContext,
4690 lpfc_cmd->cur_iocbq.iocb.ulpIoTag,
4691 lpfc_cmd->cur_iocbq.iocb.ulpTimeout,
4692 (uint32_t)
4693 (cmnd->request->timeout / 1000));
4694
James Smart2cee7802017-06-01 21:07:02 -07004695 switch (lpfc_cmd->fcp_cmnd->fcpCntl3) {
4696 case WRITE_DATA:
4697 atomic_dec(&phba->fc4ScsiOutputRequests);
4698 break;
4699 case READ_DATA:
4700 atomic_dec(&phba->fc4ScsiInputRequests);
4701 break;
4702 default:
4703 atomic_dec(&phba->fc4ScsiControlRequests);
4704 }
dea31012005-04-17 16:05:31 -05004705 goto out_host_busy_free_buf;
James Smarteaf15d52008-12-04 22:39:29 -05004706 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004707 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004708 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -08004709 &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
James Smart45ed1192009-10-02 15:17:02 -04004710
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004711 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4712 lpfc_poll_rearm_timer(phba);
4713 }
4714
dea31012005-04-17 16:05:31 -05004715 return 0;
4716
4717 out_host_busy_free_buf:
James Smartbcf4dbf2006-07-06 15:50:08 -04004718 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004719 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004720 out_host_busy:
4721 return SCSI_MLQUEUE_HOST_BUSY;
4722
Mike Christie34963432011-02-25 14:04:28 -06004723 out_tgt_busy:
4724 return SCSI_MLQUEUE_TARGET_BUSY;
4725
dea31012005-04-17 16:05:31 -05004726 out_fail_command:
James Smartb9a7c632012-08-03 12:35:24 -04004727 cmnd->scsi_done(cmnd);
dea31012005-04-17 16:05:31 -05004728 return 0;
4729}
4730
Jeff Garzikf2812332010-11-16 02:10:29 -05004731
James Smart9bad7672008-12-04 22:39:02 -05004732/**
James Smart3621a712009-04-06 18:47:14 -04004733 * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05004734 * @cmnd: Pointer to scsi_cmnd data structure.
4735 *
4736 * This routine aborts @cmnd pending in base driver.
4737 *
4738 * Return code :
4739 * 0x2003 - Error
4740 * 0x2002 - Success
4741 **/
dea31012005-04-17 16:05:31 -05004742static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05004743lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004744{
James Smart2e0fef82007-06-17 19:56:36 -05004745 struct Scsi_Host *shost = cmnd->device->host;
4746 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4747 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004748 struct lpfc_iocbq *iocb;
4749 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05004750 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05004751 IOCB_t *cmd, *icmd;
James Smart3a707302012-06-12 13:54:42 -04004752 int ret = SUCCESS, status = 0;
James Smart8931c732018-07-31 17:23:20 -07004753 struct lpfc_sli_ring *pring_s4 = NULL;
James Smart895427b2017-02-12 13:52:30 -08004754 int ret_val;
James Smart59c68ea2018-04-09 14:24:25 -07004755 unsigned long flags;
James Smartfa61a542008-01-11 01:52:42 -05004756 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004757
James Smart3a707302012-06-12 13:54:42 -04004758 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04004759 if (status != 0 && status != SUCCESS)
James Smart3a707302012-06-12 13:54:42 -04004760 return status;
James Smart4f2e66c2012-05-09 21:17:07 -04004761
James Smart876dd7d2012-09-29 11:31:28 -04004762 spin_lock_irqsave(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004763 /* driver queued commands are in process of being flushed */
4764 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
James Smart876dd7d2012-09-29 11:31:28 -04004765 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004766 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4767 "3168 SCSI Layer abort requested I/O has been "
4768 "flushed by LLD.\n");
4769 return FAILED;
4770 }
4771
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004772 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
James Smart92e3af62012-08-14 14:26:28 -04004773 if (!lpfc_cmd || !lpfc_cmd->pCmd) {
James Smart876dd7d2012-09-29 11:31:28 -04004774 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarteee88772010-09-29 11:19:08 -04004775 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4776 "2873 SCSI Layer I/O Abort Request IO CMPL Status "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004777 "x%x ID %d LUN %llu\n",
James Smart3a707302012-06-12 13:54:42 -04004778 SUCCESS, cmnd->device->id, cmnd->device->lun);
James Smarteee88772010-09-29 11:19:08 -04004779 return SUCCESS;
4780 }
dea31012005-04-17 16:05:31 -05004781
James Smart4f2e66c2012-05-09 21:17:07 -04004782 iocb = &lpfc_cmd->cur_iocbq;
James Smart8931c732018-07-31 17:23:20 -07004783 if (phba->sli_rev == LPFC_SLI_REV4) {
4784 if (!(phba->cfg_fof) ||
4785 (!(iocb->iocb_flag & LPFC_IO_FOF))) {
4786 pring_s4 =
4787 phba->sli4_hba.fcp_wq[iocb->hba_wqidx]->pring;
4788 } else {
4789 iocb->hba_wqidx = 0;
4790 pring_s4 = phba->sli4_hba.oas_wq->pring;
4791 }
4792 if (!pring_s4) {
4793 ret = FAILED;
4794 goto out_unlock;
4795 }
4796 spin_lock(&pring_s4->ring_lock);
4797 }
James Smart4f2e66c2012-05-09 21:17:07 -04004798 /* the command is in process of being cancelled */
4799 if (!(iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
James Smart8931c732018-07-31 17:23:20 -07004800 if (phba->sli_rev == LPFC_SLI_REV4)
4801 spin_unlock(&pring_s4->ring_lock);
James Smart876dd7d2012-09-29 11:31:28 -04004802 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004803 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4804 "3169 SCSI Layer abort requested I/O has been "
4805 "cancelled by LLD.\n");
4806 return FAILED;
4807 }
dea31012005-04-17 16:05:31 -05004808 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004809 * If pCmd field of the corresponding lpfc_scsi_buf structure
4810 * points to a different SCSI command, then the driver has
4811 * already completed this command, but the midlayer did not
James Smart4f2e66c2012-05-09 21:17:07 -04004812 * see the completion before the eh fired. Just return SUCCESS.
dea31012005-04-17 16:05:31 -05004813 */
James Smart4f2e66c2012-05-09 21:17:07 -04004814 if (lpfc_cmd->pCmd != cmnd) {
James Smart8931c732018-07-31 17:23:20 -07004815 if (phba->sli_rev == LPFC_SLI_REV4)
4816 spin_unlock(&pring_s4->ring_lock);
James Smart4f2e66c2012-05-09 21:17:07 -04004817 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4818 "3170 SCSI Layer abort requested I/O has been "
4819 "completed by LLD.\n");
4820 goto out_unlock;
4821 }
dea31012005-04-17 16:05:31 -05004822
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004823 BUG_ON(iocb->context1 != lpfc_cmd);
dea31012005-04-17 16:05:31 -05004824
James Smartee620212014-04-04 13:51:53 -04004825 /* abort issued in recovery is still in progress */
4826 if (iocb->iocb_flag & LPFC_DRIVER_ABORTED) {
4827 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4828 "3389 SCSI Layer I/O Abort Request is pending\n");
James Smart8931c732018-07-31 17:23:20 -07004829 if (phba->sli_rev == LPFC_SLI_REV4)
4830 spin_unlock(&pring_s4->ring_lock);
James Smartee620212014-04-04 13:51:53 -04004831 spin_unlock_irqrestore(&phba->hbalock, flags);
4832 goto wait_for_cmpl;
4833 }
4834
James Smart4f2e66c2012-05-09 21:17:07 -04004835 abtsiocb = __lpfc_sli_get_iocbq(phba);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004836 if (abtsiocb == NULL) {
4837 ret = FAILED;
James Smart8931c732018-07-31 17:23:20 -07004838 if (phba->sli_rev == LPFC_SLI_REV4)
4839 spin_unlock(&pring_s4->ring_lock);
James Smart4f2e66c2012-05-09 21:17:07 -04004840 goto out_unlock;
dea31012005-04-17 16:05:31 -05004841 }
4842
James Smartafbd8d82013-09-06 12:22:13 -04004843 /* Indicate the IO is being aborted by the driver. */
4844 iocb->iocb_flag |= LPFC_DRIVER_ABORTED;
4845
dea31012005-04-17 16:05:31 -05004846 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004847 * The scsi command can not be in txq and it is in flight because the
4848 * pCmd is still pointig at the SCSI command we have to abort. There
4849 * is no need to search the txcmplq. Just send an abort to the FW.
dea31012005-04-17 16:05:31 -05004850 */
dea31012005-04-17 16:05:31 -05004851
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004852 cmd = &iocb->iocb;
4853 icmd = &abtsiocb->iocb;
4854 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
4855 icmd->un.acxri.abortContextTag = cmd->ulpContext;
James Smart3772a992009-05-22 14:50:54 -04004856 if (phba->sli_rev == LPFC_SLI_REV4)
4857 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
4858 else
4859 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05004860
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004861 icmd->ulpLe = 1;
4862 icmd->ulpClass = cmd->ulpClass;
James Smart5ffc2662009-11-18 15:39:44 -05004863
4864 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -08004865 abtsiocb->hba_wqidx = iocb->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -05004866 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -04004867 if (iocb->iocb_flag & LPFC_IO_FOF)
4868 abtsiocb->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -05004869
James Smart2e0fef82007-06-17 19:56:36 -05004870 if (lpfc_is_link_up(phba))
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004871 icmd->ulpCommand = CMD_ABORT_XRI_CN;
4872 else
4873 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea31012005-04-17 16:05:31 -05004874
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004875 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smart2e0fef82007-06-17 19:56:36 -05004876 abtsiocb->vport = vport;
James Smart8931c732018-07-31 17:23:20 -07004877 lpfc_cmd->waitq = &waitq;
James Smart98912dda2014-04-04 13:52:31 -04004878 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart98912dda2014-04-04 13:52:31 -04004879 /* Note: both hbalock and ring_lock must be set here */
James Smart98912dda2014-04-04 13:52:31 -04004880 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
4881 abtsiocb, 0);
James Smart59c68ea2018-04-09 14:24:25 -07004882 spin_unlock(&pring_s4->ring_lock);
James Smart98912dda2014-04-04 13:52:31 -04004883 } else {
4884 ret_val = __lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
4885 abtsiocb, 0);
4886 }
James Smart4f2e66c2012-05-09 21:17:07 -04004887 /* no longer need the lock after this point */
James Smart876dd7d2012-09-29 11:31:28 -04004888 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004889
James Smart98912dda2014-04-04 13:52:31 -04004890
4891 if (ret_val == IOCB_ERROR) {
James Smart8931c732018-07-31 17:23:20 -07004892 if (phba->sli_rev == LPFC_SLI_REV4)
4893 spin_lock_irqsave(&pring_s4->ring_lock, flags);
4894 else
4895 spin_lock_irqsave(&phba->hbalock, flags);
4896 /* Indicate the IO is not being aborted by the driver. */
4897 iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
4898 lpfc_cmd->waitq = NULL;
4899 if (phba->sli_rev == LPFC_SLI_REV4)
4900 spin_unlock_irqrestore(&pring_s4->ring_lock, flags);
4901 else
4902 spin_unlock_irqrestore(&phba->hbalock, flags);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004903 lpfc_sli_release_iocbq(phba, abtsiocb);
4904 ret = FAILED;
4905 goto out;
4906 }
4907
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004908 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
James Smart45ed1192009-10-02 15:17:02 -04004909 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -08004910 &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004911
James Smartee620212014-04-04 13:51:53 -04004912wait_for_cmpl:
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004913 /* Wait for abort to complete */
James Smartfa61a542008-01-11 01:52:42 -05004914 wait_event_timeout(waitq,
4915 (lpfc_cmd->pCmd != cmnd),
James Smart256ec0d2013-04-17 20:14:58 -04004916 msecs_to_jiffies(2*vport->cfg_devloss_tmo*1000));
James Smartee620212014-04-04 13:51:53 -04004917
4918 spin_lock_irqsave(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05004919 lpfc_cmd->waitq = NULL;
James Smartee620212014-04-04 13:51:53 -04004920 spin_unlock_irqrestore(shost->host_lock, flags);
dea31012005-04-17 16:05:31 -05004921
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004922 if (lpfc_cmd->pCmd == cmnd) {
4923 ret = FAILED;
James Smarte8b62012007-08-02 11:10:09 -04004924 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4925 "0748 abort handler timed out waiting "
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -07004926 "for aborting I/O (xri:x%x) to complete: "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004927 "ret %#x, ID %d, LUN %llu\n",
James Smart247ca942012-08-14 14:26:13 -04004928 iocb->sli4_xritag, ret,
4929 cmnd->device->id, cmnd->device->lun);
dea31012005-04-17 16:05:31 -05004930 }
James Smart4f2e66c2012-05-09 21:17:07 -04004931 goto out;
dea31012005-04-17 16:05:31 -05004932
James Smart4f2e66c2012-05-09 21:17:07 -04004933out_unlock:
James Smart876dd7d2012-09-29 11:31:28 -04004934 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004935out:
James Smarte8b62012007-08-02 11:10:09 -04004936 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4937 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004938 "LUN %llu\n", ret, cmnd->device->id,
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04004939 cmnd->device->lun);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004940 return ret;
dea31012005-04-17 16:05:31 -05004941}
4942
James Smartbbb9d182009-06-10 17:23:16 -04004943static char *
4944lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
4945{
4946 switch (task_mgmt_cmd) {
4947 case FCP_ABORT_TASK_SET:
4948 return "ABORT_TASK_SET";
4949 case FCP_CLEAR_TASK_SET:
4950 return "FCP_CLEAR_TASK_SET";
4951 case FCP_BUS_RESET:
4952 return "FCP_BUS_RESET";
4953 case FCP_LUN_RESET:
4954 return "FCP_LUN_RESET";
4955 case FCP_TARGET_RESET:
4956 return "FCP_TARGET_RESET";
4957 case FCP_CLEAR_ACA:
4958 return "FCP_CLEAR_ACA";
4959 case FCP_TERMINATE_TASK:
4960 return "FCP_TERMINATE_TASK";
4961 default:
4962 return "unknown";
4963 }
4964}
4965
James Smart53151bb2013-10-10 12:24:07 -04004966
4967/**
4968 * lpfc_check_fcp_rsp - check the returned fcp_rsp to see if task failed
4969 * @vport: The virtual port for which this call is being executed.
4970 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
4971 *
4972 * This routine checks the FCP RSP INFO to see if the tsk mgmt command succeded
4973 *
4974 * Return code :
4975 * 0x2003 - Error
4976 * 0x2002 - Success
4977 **/
4978static int
4979lpfc_check_fcp_rsp(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd)
4980{
4981 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
4982 uint32_t rsp_info;
4983 uint32_t rsp_len;
4984 uint8_t rsp_info_code;
4985 int ret = FAILED;
4986
4987
4988 if (fcprsp == NULL)
4989 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4990 "0703 fcp_rsp is missing\n");
4991 else {
4992 rsp_info = fcprsp->rspStatus2;
4993 rsp_len = be32_to_cpu(fcprsp->rspRspLen);
4994 rsp_info_code = fcprsp->rspInfo3;
4995
4996
4997 lpfc_printf_vlog(vport, KERN_INFO,
4998 LOG_FCP,
4999 "0706 fcp_rsp valid 0x%x,"
5000 " rsp len=%d code 0x%x\n",
5001 rsp_info,
5002 rsp_len, rsp_info_code);
5003
5004 if ((fcprsp->rspStatus2&RSP_LEN_VALID) && (rsp_len == 8)) {
5005 switch (rsp_info_code) {
5006 case RSP_NO_FAILURE:
5007 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5008 "0715 Task Mgmt No Failure\n");
5009 ret = SUCCESS;
5010 break;
5011 case RSP_TM_NOT_SUPPORTED: /* TM rejected */
5012 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5013 "0716 Task Mgmt Target "
5014 "reject\n");
5015 break;
5016 case RSP_TM_NOT_COMPLETED: /* TM failed */
5017 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5018 "0717 Task Mgmt Target "
5019 "failed TM\n");
5020 break;
5021 case RSP_TM_INVALID_LU: /* TM to invalid LU! */
5022 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5023 "0718 Task Mgmt to invalid "
5024 "LUN\n");
5025 break;
5026 }
5027 }
5028 }
5029 return ret;
5030}
5031
5032
James Smartbbb9d182009-06-10 17:23:16 -04005033/**
5034 * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
5035 * @vport: The virtual port for which this call is being executed.
5036 * @rdata: Pointer to remote port local data
5037 * @tgt_id: Target ID of remote device.
5038 * @lun_id: Lun number for the TMF
5039 * @task_mgmt_cmd: type of TMF to send
5040 *
5041 * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
5042 * a remote port.
5043 *
5044 * Return Code:
5045 * 0x2003 - Error
5046 * 0x2002 - Success.
5047 **/
5048static int
James Smarteed695d2016-10-13 15:06:04 -07005049lpfc_send_taskmgmt(struct lpfc_vport *vport, struct scsi_cmnd *cmnd,
5050 unsigned int tgt_id, uint64_t lun_id,
5051 uint8_t task_mgmt_cmd)
James Smartbbb9d182009-06-10 17:23:16 -04005052{
5053 struct lpfc_hba *phba = vport->phba;
5054 struct lpfc_scsi_buf *lpfc_cmd;
5055 struct lpfc_iocbq *iocbq;
5056 struct lpfc_iocbq *iocbqrsp;
James Smarteed695d2016-10-13 15:06:04 -07005057 struct lpfc_rport_data *rdata;
5058 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005059 int ret;
5060 int status;
5061
James Smarteed695d2016-10-13 15:06:04 -07005062 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
5063 if (!rdata || !rdata->pnode || !NLP_CHK_NODE_ACT(rdata->pnode))
James Smartbbb9d182009-06-10 17:23:16 -04005064 return FAILED;
James Smarteed695d2016-10-13 15:06:04 -07005065 pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005066
James Smarteed695d2016-10-13 15:06:04 -07005067 lpfc_cmd = lpfc_get_scsi_buf(phba, pnode);
James Smartbbb9d182009-06-10 17:23:16 -04005068 if (lpfc_cmd == NULL)
5069 return FAILED;
James Smart0c411222013-09-06 12:22:46 -04005070 lpfc_cmd->timeout = phba->cfg_task_mgmt_tmo;
James Smartbbb9d182009-06-10 17:23:16 -04005071 lpfc_cmd->rdata = rdata;
James Smarteed695d2016-10-13 15:06:04 -07005072 lpfc_cmd->pCmd = cmnd;
James Smart2a5b7d62018-07-31 17:23:22 -07005073 lpfc_cmd->ndlp = pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005074
5075 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
5076 task_mgmt_cmd);
5077 if (!status) {
5078 lpfc_release_scsi_buf(phba, lpfc_cmd);
5079 return FAILED;
5080 }
5081
5082 iocbq = &lpfc_cmd->cur_iocbq;
5083 iocbqrsp = lpfc_sli_get_iocbq(phba);
5084 if (iocbqrsp == NULL) {
5085 lpfc_release_scsi_buf(phba, lpfc_cmd);
5086 return FAILED;
5087 }
James Smart5a0916b2013-07-15 18:31:42 -04005088 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
James Smartbbb9d182009-06-10 17:23:16 -04005089
5090 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005091 "0702 Issue %s to TGT %d LUN %llu "
James Smart6d368e52011-05-24 11:44:12 -04005092 "rpi x%x nlp_flag x%x Data: x%x x%x\n",
James Smartbbb9d182009-06-10 17:23:16 -04005093 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
James Smart6d368e52011-05-24 11:44:12 -04005094 pnode->nlp_rpi, pnode->nlp_flag, iocbq->sli4_xritag,
5095 iocbq->iocb_flag);
James Smartbbb9d182009-06-10 17:23:16 -04005096
5097 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
5098 iocbq, iocbqrsp, lpfc_cmd->timeout);
James Smart53151bb2013-10-10 12:24:07 -04005099 if ((status != IOCB_SUCCESS) ||
5100 (iocbqrsp->iocb.ulpStatus != IOSTAT_SUCCESS)) {
James Smartae374a32015-05-22 10:42:41 -04005101 if (status != IOCB_SUCCESS ||
5102 iocbqrsp->iocb.ulpStatus != IOSTAT_FCP_RSP_ERROR)
5103 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5104 "0727 TMF %s to TGT %d LUN %llu "
5105 "failed (%d, %d) iocb_flag x%x\n",
5106 lpfc_taskmgmt_name(task_mgmt_cmd),
5107 tgt_id, lun_id,
5108 iocbqrsp->iocb.ulpStatus,
5109 iocbqrsp->iocb.un.ulpWord[4],
5110 iocbq->iocb_flag);
James Smart53151bb2013-10-10 12:24:07 -04005111 /* if ulpStatus != IOCB_SUCCESS, then status == IOCB_SUCCESS */
5112 if (status == IOCB_SUCCESS) {
5113 if (iocbqrsp->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
5114 /* Something in the FCP_RSP was invalid.
5115 * Check conditions */
5116 ret = lpfc_check_fcp_rsp(vport, lpfc_cmd);
5117 else
5118 ret = FAILED;
5119 } else if (status == IOCB_TIMEDOUT) {
5120 ret = TIMEOUT_ERROR;
5121 } else {
5122 ret = FAILED;
5123 }
James Smart53151bb2013-10-10 12:24:07 -04005124 } else
James Smartbbb9d182009-06-10 17:23:16 -04005125 ret = SUCCESS;
5126
5127 lpfc_sli_release_iocbq(phba, iocbqrsp);
5128
5129 if (ret != TIMEOUT_ERROR)
5130 lpfc_release_scsi_buf(phba, lpfc_cmd);
5131
5132 return ret;
5133}
5134
5135/**
5136 * lpfc_chk_tgt_mapped -
5137 * @vport: The virtual port to check on
5138 * @cmnd: Pointer to scsi_cmnd data structure.
5139 *
5140 * This routine delays until the scsi target (aka rport) for the
5141 * command exists (is present and logged in) or we declare it non-existent.
5142 *
5143 * Return code :
5144 * 0x2003 - Error
5145 * 0x2002 - Success
5146 **/
5147static int
5148lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
5149{
James Smart1ba981f2014-02-20 09:56:45 -05005150 struct lpfc_rport_data *rdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005151 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005152 unsigned long later;
5153
James Smart1ba981f2014-02-20 09:56:45 -05005154 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smart1c6f4ef52009-11-18 15:40:49 -05005155 if (!rdata) {
5156 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5157 "0797 Tgt Map rport failure: rdata x%p\n", rdata);
5158 return FAILED;
5159 }
5160 pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005161 /*
5162 * If target is not in a MAPPED state, delay until
5163 * target is rediscovered or devloss timeout expires.
5164 */
5165 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
5166 while (time_after(later, jiffies)) {
5167 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
5168 return FAILED;
5169 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
5170 return SUCCESS;
5171 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
James Smart1ba981f2014-02-20 09:56:45 -05005172 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smartbbb9d182009-06-10 17:23:16 -04005173 if (!rdata)
5174 return FAILED;
5175 pnode = rdata->pnode;
5176 }
5177 if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
5178 (pnode->nlp_state != NLP_STE_MAPPED_NODE))
5179 return FAILED;
5180 return SUCCESS;
5181}
5182
5183/**
5184 * lpfc_reset_flush_io_context -
5185 * @vport: The virtual port (scsi_host) for the flush context
5186 * @tgt_id: If aborting by Target contect - specifies the target id
5187 * @lun_id: If aborting by Lun context - specifies the lun id
5188 * @context: specifies the context level to flush at.
5189 *
5190 * After a reset condition via TMF, we need to flush orphaned i/o
5191 * contexts from the adapter. This routine aborts any contexts
5192 * outstanding, then waits for their completions. The wait is
5193 * bounded by devloss_tmo though.
5194 *
5195 * Return code :
5196 * 0x2003 - Error
5197 * 0x2002 - Success
5198 **/
5199static int
5200lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
5201 uint64_t lun_id, lpfc_ctx_cmd context)
5202{
5203 struct lpfc_hba *phba = vport->phba;
5204 unsigned long later;
5205 int cnt;
5206
5207 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
5208 if (cnt)
James Smart98912dda2014-04-04 13:52:31 -04005209 lpfc_sli_abort_taskmgmt(vport,
James Smart895427b2017-02-12 13:52:30 -08005210 &phba->sli.sli3_ring[LPFC_FCP_RING],
James Smart98912dda2014-04-04 13:52:31 -04005211 tgt_id, lun_id, context);
James Smartbbb9d182009-06-10 17:23:16 -04005212 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
5213 while (time_after(later, jiffies) && cnt) {
5214 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
5215 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
5216 }
5217 if (cnt) {
5218 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5219 "0724 I/O flush failure for context %s : cnt x%x\n",
5220 ((context == LPFC_CTX_LUN) ? "LUN" :
5221 ((context == LPFC_CTX_TGT) ? "TGT" :
5222 ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
5223 cnt);
5224 return FAILED;
5225 }
5226 return SUCCESS;
5227}
5228
James Smart9bad7672008-12-04 22:39:02 -05005229/**
James Smart3621a712009-04-06 18:47:14 -04005230 * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
James Smart9bad7672008-12-04 22:39:02 -05005231 * @cmnd: Pointer to scsi_cmnd data structure.
5232 *
James Smartbbb9d182009-06-10 17:23:16 -04005233 * This routine does a device reset by sending a LUN_RESET task management
James Smart9bad7672008-12-04 22:39:02 -05005234 * command.
5235 *
5236 * Return code :
5237 * 0x2003 - Error
James Smart3621a712009-04-06 18:47:14 -04005238 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05005239 **/
dea31012005-04-17 16:05:31 -05005240static int
James Smart7054a602007-04-25 09:52:34 -04005241lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05005242{
James Smart2e0fef82007-06-17 19:56:36 -05005243 struct Scsi_Host *shost = cmnd->device->host;
5244 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smart1ba981f2014-02-20 09:56:45 -05005245 struct lpfc_rport_data *rdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005246 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005247 unsigned tgt_id = cmnd->device->id;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005248 uint64_t lun_id = cmnd->device->lun;
James Smartea2151b2008-09-07 11:52:10 -04005249 struct lpfc_scsi_event_header scsi_event;
James Smart53151bb2013-10-10 12:24:07 -04005250 int status;
dea31012005-04-17 16:05:31 -05005251
James Smart1ba981f2014-02-20 09:56:45 -05005252 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smartad490b62015-04-07 15:07:26 -04005253 if (!rdata || !rdata->pnode) {
James Smart1c6f4ef52009-11-18 15:40:49 -05005254 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smartad490b62015-04-07 15:07:26 -04005255 "0798 Device Reset rport failure: rdata x%p\n",
5256 rdata);
James Smart1c6f4ef52009-11-18 15:40:49 -05005257 return FAILED;
5258 }
5259 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04005260 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005261 if (status != 0 && status != SUCCESS)
James Smart589a52d2010-07-14 15:30:54 -04005262 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005263
5264 status = lpfc_chk_tgt_mapped(vport, cmnd);
5265 if (status == FAILED) {
5266 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5267 "0721 Device Reset rport failure: rdata x%p\n", rdata);
5268 return FAILED;
5269 }
5270
5271 scsi_event.event_type = FC_REG_SCSI_EVENT;
5272 scsi_event.subcategory = LPFC_EVENT_LUNRESET;
5273 scsi_event.lun = lun_id;
5274 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5275 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5276
5277 fc_host_post_vendor_event(shost, fc_get_event_number(),
5278 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
5279
James Smarteed695d2016-10-13 15:06:04 -07005280 status = lpfc_send_taskmgmt(vport, cmnd, tgt_id, lun_id,
James Smartbbb9d182009-06-10 17:23:16 -04005281 FCP_LUN_RESET);
5282
5283 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005284 "0713 SCSI layer issued Device Reset (%d, %llu) "
James Smartbbb9d182009-06-10 17:23:16 -04005285 "return x%x\n", tgt_id, lun_id, status);
5286
dea31012005-04-17 16:05:31 -05005287 /*
James Smartbbb9d182009-06-10 17:23:16 -04005288 * We have to clean up i/o as : they may be orphaned by the TMF;
5289 * or if the TMF failed, they may be in an indeterminate state.
5290 * So, continue on.
5291 * We will report success if all the i/o aborts successfully.
dea31012005-04-17 16:05:31 -05005292 */
James Smart53151bb2013-10-10 12:24:07 -04005293 if (status == SUCCESS)
5294 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
James Smartbbb9d182009-06-10 17:23:16 -04005295 LPFC_CTX_LUN);
James Smart53151bb2013-10-10 12:24:07 -04005296
5297 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005298}
5299
5300/**
5301 * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
5302 * @cmnd: Pointer to scsi_cmnd data structure.
5303 *
5304 * This routine does a target reset by sending a TARGET_RESET task management
5305 * command.
5306 *
5307 * Return code :
5308 * 0x2003 - Error
5309 * 0x2002 - Success
5310 **/
5311static int
5312lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
5313{
5314 struct Scsi_Host *shost = cmnd->device->host;
5315 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smart1ba981f2014-02-20 09:56:45 -05005316 struct lpfc_rport_data *rdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005317 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005318 unsigned tgt_id = cmnd->device->id;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005319 uint64_t lun_id = cmnd->device->lun;
James Smartbbb9d182009-06-10 17:23:16 -04005320 struct lpfc_scsi_event_header scsi_event;
James Smart53151bb2013-10-10 12:24:07 -04005321 int status;
James Smartbbb9d182009-06-10 17:23:16 -04005322
James Smart1ba981f2014-02-20 09:56:45 -05005323 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smart1c6f4ef52009-11-18 15:40:49 -05005324 if (!rdata) {
5325 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5326 "0799 Target Reset rport failure: rdata x%p\n", rdata);
5327 return FAILED;
5328 }
5329 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04005330 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005331 if (status != 0 && status != SUCCESS)
James Smart589a52d2010-07-14 15:30:54 -04005332 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005333
5334 status = lpfc_chk_tgt_mapped(vport, cmnd);
5335 if (status == FAILED) {
5336 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5337 "0722 Target Reset rport failure: rdata x%p\n", rdata);
James Smart63e480f2015-04-07 15:07:20 -04005338 if (pnode) {
5339 spin_lock_irq(shost->host_lock);
5340 pnode->nlp_flag &= ~NLP_NPR_ADISC;
5341 pnode->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
5342 spin_unlock_irq(shost->host_lock);
5343 }
James Smart8c50d252014-09-03 12:58:16 -04005344 lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
5345 LPFC_CTX_TGT);
5346 return FAST_IO_FAIL;
dea31012005-04-17 16:05:31 -05005347 }
James Smartea2151b2008-09-07 11:52:10 -04005348
5349 scsi_event.event_type = FC_REG_SCSI_EVENT;
5350 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
5351 scsi_event.lun = 0;
5352 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5353 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5354
James Smartbbb9d182009-06-10 17:23:16 -04005355 fc_host_post_vendor_event(shost, fc_get_event_number(),
5356 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -04005357
James Smarteed695d2016-10-13 15:06:04 -07005358 status = lpfc_send_taskmgmt(vport, cmnd, tgt_id, lun_id,
James Smartbbb9d182009-06-10 17:23:16 -04005359 FCP_TARGET_RESET);
dea31012005-04-17 16:05:31 -05005360
James Smarte8b62012007-08-02 11:10:09 -04005361 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005362 "0723 SCSI layer issued Target Reset (%d, %llu) "
James Smartbbb9d182009-06-10 17:23:16 -04005363 "return x%x\n", tgt_id, lun_id, status);
5364
5365 /*
5366 * We have to clean up i/o as : they may be orphaned by the TMF;
5367 * or if the TMF failed, they may be in an indeterminate state.
5368 * So, continue on.
5369 * We will report success if all the i/o aborts successfully.
5370 */
James Smart53151bb2013-10-10 12:24:07 -04005371 if (status == SUCCESS)
5372 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
James Smart3a707302012-06-12 13:54:42 -04005373 LPFC_CTX_TGT);
James Smart53151bb2013-10-10 12:24:07 -04005374 return status;
dea31012005-04-17 16:05:31 -05005375}
5376
James Smart9bad7672008-12-04 22:39:02 -05005377/**
James Smart3621a712009-04-06 18:47:14 -04005378 * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05005379 * @cmnd: Pointer to scsi_cmnd data structure.
5380 *
James Smartbbb9d182009-06-10 17:23:16 -04005381 * This routine does target reset to all targets on @cmnd->device->host.
5382 * This emulates Parallel SCSI Bus Reset Semantics.
James Smart9bad7672008-12-04 22:39:02 -05005383 *
James Smartbbb9d182009-06-10 17:23:16 -04005384 * Return code :
5385 * 0x2003 - Error
5386 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05005387 **/
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04005388static int
James Smart7054a602007-04-25 09:52:34 -04005389lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05005390{
James Smart2e0fef82007-06-17 19:56:36 -05005391 struct Scsi_Host *shost = cmnd->device->host;
5392 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05005393 struct lpfc_nodelist *ndlp = NULL;
James Smartea2151b2008-09-07 11:52:10 -04005394 struct lpfc_scsi_event_header scsi_event;
James Smartbbb9d182009-06-10 17:23:16 -04005395 int match;
5396 int ret = SUCCESS, status, i;
James Smartea2151b2008-09-07 11:52:10 -04005397
5398 scsi_event.event_type = FC_REG_SCSI_EVENT;
5399 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
5400 scsi_event.lun = 0;
5401 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
5402 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
5403
James Smartbbb9d182009-06-10 17:23:16 -04005404 fc_host_post_vendor_event(shost, fc_get_event_number(),
5405 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
dea31012005-04-17 16:05:31 -05005406
James Smartbf086112011-08-21 21:48:13 -04005407 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005408 if (status != 0 && status != SUCCESS)
James Smartbf086112011-08-21 21:48:13 -04005409 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005410
dea31012005-04-17 16:05:31 -05005411 /*
5412 * Since the driver manages a single bus device, reset all
5413 * targets known to the driver. Should any target reset
5414 * fail, this routine returns failure to the midlayer.
5415 */
James Smarte17da182006-07-06 15:49:25 -04005416 for (i = 0; i < LPFC_MAX_TARGET; i++) {
James Smart685f0bf2007-04-25 09:53:08 -04005417 /* Search for mapped node by target ID */
dea31012005-04-17 16:05:31 -05005418 match = 0;
James Smart2e0fef82007-06-17 19:56:36 -05005419 spin_lock_irq(shost->host_lock);
5420 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05005421 if (!NLP_CHK_NODE_ACT(ndlp))
5422 continue;
James Smarta6571c62012-10-31 14:44:42 -04005423 if (vport->phba->cfg_fcp2_no_tgt_reset &&
5424 (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE))
5425 continue;
James Smart685f0bf2007-04-25 09:53:08 -04005426 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
James Smart915caaa2008-06-14 22:52:38 -04005427 ndlp->nlp_sid == i &&
James Smarta0f2d3e2017-02-12 13:52:31 -08005428 ndlp->rport &&
5429 ndlp->nlp_type & NLP_FCP_TARGET) {
dea31012005-04-17 16:05:31 -05005430 match = 1;
5431 break;
5432 }
5433 }
James Smart2e0fef82007-06-17 19:56:36 -05005434 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005435 if (!match)
5436 continue;
James Smartbbb9d182009-06-10 17:23:16 -04005437
James Smarteed695d2016-10-13 15:06:04 -07005438 status = lpfc_send_taskmgmt(vport, cmnd,
James Smartbbb9d182009-06-10 17:23:16 -04005439 i, 0, FCP_TARGET_RESET);
5440
5441 if (status != SUCCESS) {
James Smarte8b62012007-08-02 11:10:09 -04005442 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5443 "0700 Bus Reset on target %d failed\n",
5444 i);
James Smart915caaa2008-06-14 22:52:38 -04005445 ret = FAILED;
dea31012005-04-17 16:05:31 -05005446 }
5447 }
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005448 /*
James Smartbbb9d182009-06-10 17:23:16 -04005449 * We have to clean up i/o as : they may be orphaned by the TMFs
5450 * above; or if any of the TMFs failed, they may be in an
5451 * indeterminate state.
5452 * We will report success if all the i/o aborts successfully.
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005453 */
James Smartbbb9d182009-06-10 17:23:16 -04005454
5455 status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
5456 if (status != SUCCESS)
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005457 ret = FAILED;
James Smartbbb9d182009-06-10 17:23:16 -04005458
James Smarte8b62012007-08-02 11:10:09 -04005459 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5460 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea31012005-04-17 16:05:31 -05005461 return ret;
5462}
5463
James Smart9bad7672008-12-04 22:39:02 -05005464/**
James Smart27b01b82012-05-09 21:19:44 -04005465 * lpfc_host_reset_handler - scsi_host_template eh_host_reset_handler entry pt
5466 * @cmnd: Pointer to scsi_cmnd data structure.
5467 *
5468 * This routine does host reset to the adaptor port. It brings the HBA
5469 * offline, performs a board restart, and then brings the board back online.
5470 * The lpfc_offline calls lpfc_sli_hba_down which will abort and local
5471 * reject all outstanding SCSI commands to the host and error returned
5472 * back to SCSI mid-level. As this will be SCSI mid-level's last resort
5473 * of error handling, it will only return error if resetting of the adapter
5474 * is not successful; in all other cases, will return success.
5475 *
5476 * Return code :
5477 * 0x2003 - Error
5478 * 0x2002 - Success
5479 **/
5480static int
5481lpfc_host_reset_handler(struct scsi_cmnd *cmnd)
5482{
5483 struct Scsi_Host *shost = cmnd->device->host;
5484 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5485 struct lpfc_hba *phba = vport->phba;
5486 int rc, ret = SUCCESS;
5487
James Smarta88dbb62013-04-17 20:18:39 -04005488 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5489 "3172 SCSI layer issued Host Reset Data:\n");
5490
James Smart618a5232012-06-12 13:54:36 -04005491 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart27b01b82012-05-09 21:19:44 -04005492 lpfc_offline(phba);
5493 rc = lpfc_sli_brdrestart(phba);
5494 if (rc)
5495 ret = FAILED;
James Smarta88dbb62013-04-17 20:18:39 -04005496 rc = lpfc_online(phba);
5497 if (rc)
5498 ret = FAILED;
James Smart27b01b82012-05-09 21:19:44 -04005499 lpfc_unblock_mgmt_io(phba);
5500
James Smarta88dbb62013-04-17 20:18:39 -04005501 if (ret == FAILED) {
5502 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5503 "3323 Failed host reset, bring it offline\n");
5504 lpfc_sli4_offline_eratt(phba);
5505 }
James Smart27b01b82012-05-09 21:19:44 -04005506 return ret;
5507}
5508
5509/**
James Smart3621a712009-04-06 18:47:14 -04005510 * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
James Smart9bad7672008-12-04 22:39:02 -05005511 * @sdev: Pointer to scsi_device.
5512 *
5513 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
5514 * globally available list of scsi buffers. This routine also makes sure scsi
5515 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
5516 * of scsi buffer exists for the lifetime of the driver.
5517 *
5518 * Return codes:
5519 * non-0 - Error
5520 * 0 - Success
5521 **/
dea31012005-04-17 16:05:31 -05005522static int
dea31012005-04-17 16:05:31 -05005523lpfc_slave_alloc(struct scsi_device *sdev)
5524{
James Smart2e0fef82007-06-17 19:56:36 -05005525 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5526 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005527 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
James Smart3772a992009-05-22 14:50:54 -04005528 uint32_t total = 0;
dea31012005-04-17 16:05:31 -05005529 uint32_t num_to_alloc = 0;
James Smart3772a992009-05-22 14:50:54 -04005530 int num_allocated = 0;
James Smartd7c47992010-06-08 18:31:54 -04005531 uint32_t sdev_cnt;
James Smart1ba981f2014-02-20 09:56:45 -05005532 struct lpfc_device_data *device_data;
5533 unsigned long flags;
5534 struct lpfc_name target_wwpn;
dea31012005-04-17 16:05:31 -05005535
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005536 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05005537 return -ENXIO;
5538
James Smartf38fa0b2014-04-04 13:52:21 -04005539 if (phba->cfg_fof) {
James Smart1ba981f2014-02-20 09:56:45 -05005540
5541 /*
5542 * Check to see if the device data structure for the lun
5543 * exists. If not, create one.
5544 */
5545
5546 u64_to_wwn(rport->port_name, target_wwpn.u.wwn);
5547 spin_lock_irqsave(&phba->devicelock, flags);
5548 device_data = __lpfc_get_device_data(phba,
5549 &phba->luns,
5550 &vport->fc_portname,
5551 &target_wwpn,
5552 sdev->lun);
5553 if (!device_data) {
5554 spin_unlock_irqrestore(&phba->devicelock, flags);
5555 device_data = lpfc_create_device_data(phba,
5556 &vport->fc_portname,
5557 &target_wwpn,
James Smartb5749fe2016-12-19 15:07:26 -08005558 sdev->lun,
5559 phba->cfg_XLanePriority,
5560 true);
James Smart1ba981f2014-02-20 09:56:45 -05005561 if (!device_data)
5562 return -ENOMEM;
5563 spin_lock_irqsave(&phba->devicelock, flags);
5564 list_add_tail(&device_data->listentry, &phba->luns);
5565 }
5566 device_data->rport_data = rport->dd_data;
5567 device_data->available = true;
5568 spin_unlock_irqrestore(&phba->devicelock, flags);
5569 sdev->hostdata = device_data;
5570 } else {
5571 sdev->hostdata = rport->dd_data;
5572 }
James Smartd7c47992010-06-08 18:31:54 -04005573 sdev_cnt = atomic_inc_return(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05005574
5575 /*
5576 * Populate the cmds_per_lun count scsi_bufs into this host's globally
5577 * available list of scsi buffers. Don't allocate more than the
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04005578 * HBA limit conveyed to the midlayer via the host structure. The
5579 * formula accounts for the lun_queue_depth + error handlers + 1
5580 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea31012005-04-17 16:05:31 -05005581 */
5582 total = phba->total_scsi_bufs;
James Smart3de2a652007-08-02 11:09:59 -04005583 num_to_alloc = vport->cfg_lun_queue_depth + 2;
James Smart92d7f7b2007-06-17 19:56:38 -05005584
James Smartd7c47992010-06-08 18:31:54 -04005585 /* If allocated buffers are enough do nothing */
5586 if ((sdev_cnt * (vport->cfg_lun_queue_depth + 2)) < total)
5587 return 0;
5588
James Smart92d7f7b2007-06-17 19:56:38 -05005589 /* Allow some exchanges to be available always to complete discovery */
5590 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04005591 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5592 "0704 At limitation of %d preallocated "
5593 "command buffers\n", total);
dea31012005-04-17 16:05:31 -05005594 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05005595 /* Allow some exchanges to be available always to complete discovery */
5596 } else if (total + num_to_alloc >
5597 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04005598 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5599 "0705 Allocation request of %d "
5600 "command buffers will exceed max of %d. "
5601 "Reducing allocation request to %d.\n",
5602 num_to_alloc, phba->cfg_hba_queue_depth,
5603 (phba->cfg_hba_queue_depth - total));
dea31012005-04-17 16:05:31 -05005604 num_to_alloc = phba->cfg_hba_queue_depth - total;
5605 }
James Smart3772a992009-05-22 14:50:54 -04005606 num_allocated = lpfc_new_scsi_buf(vport, num_to_alloc);
5607 if (num_to_alloc != num_allocated) {
James Smart96f70772013-04-17 20:16:15 -04005608 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5609 "0708 Allocation request of %d "
5610 "command buffers did not succeed. "
5611 "Allocated %d buffers.\n",
5612 num_to_alloc, num_allocated);
dea31012005-04-17 16:05:31 -05005613 }
James Smart1c6f4ef52009-11-18 15:40:49 -05005614 if (num_allocated > 0)
5615 phba->total_scsi_bufs += num_allocated;
dea31012005-04-17 16:05:31 -05005616 return 0;
5617}
5618
James Smart9bad7672008-12-04 22:39:02 -05005619/**
James Smart3621a712009-04-06 18:47:14 -04005620 * lpfc_slave_configure - scsi_host_template slave_configure entry point
James Smart9bad7672008-12-04 22:39:02 -05005621 * @sdev: Pointer to scsi_device.
5622 *
5623 * This routine configures following items
5624 * - Tag command queuing support for @sdev if supported.
James Smart9bad7672008-12-04 22:39:02 -05005625 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
5626 *
5627 * Return codes:
5628 * 0 - Success
5629 **/
dea31012005-04-17 16:05:31 -05005630static int
5631lpfc_slave_configure(struct scsi_device *sdev)
5632{
James Smart2e0fef82007-06-17 19:56:36 -05005633 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5634 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05005635
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01005636 scsi_change_queue_depth(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05005637
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005638 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04005639 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -08005640 &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005641 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
5642 lpfc_poll_rearm_timer(phba);
5643 }
5644
dea31012005-04-17 16:05:31 -05005645 return 0;
5646}
5647
James Smart9bad7672008-12-04 22:39:02 -05005648/**
James Smart3621a712009-04-06 18:47:14 -04005649 * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
James Smart9bad7672008-12-04 22:39:02 -05005650 * @sdev: Pointer to scsi_device.
5651 *
5652 * This routine sets @sdev hostatdata filed to null.
5653 **/
dea31012005-04-17 16:05:31 -05005654static void
5655lpfc_slave_destroy(struct scsi_device *sdev)
5656{
James Smartd7c47992010-06-08 18:31:54 -04005657 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5658 struct lpfc_hba *phba = vport->phba;
James Smart1ba981f2014-02-20 09:56:45 -05005659 unsigned long flags;
5660 struct lpfc_device_data *device_data = sdev->hostdata;
5661
James Smartd7c47992010-06-08 18:31:54 -04005662 atomic_dec(&phba->sdev_cnt);
James Smartf38fa0b2014-04-04 13:52:21 -04005663 if ((phba->cfg_fof) && (device_data)) {
James Smart1ba981f2014-02-20 09:56:45 -05005664 spin_lock_irqsave(&phba->devicelock, flags);
5665 device_data->available = false;
5666 if (!device_data->oas_enabled)
5667 lpfc_delete_device_data(phba, device_data);
5668 spin_unlock_irqrestore(&phba->devicelock, flags);
5669 }
dea31012005-04-17 16:05:31 -05005670 sdev->hostdata = NULL;
5671 return;
5672}
5673
James Smart1ba981f2014-02-20 09:56:45 -05005674/**
5675 * lpfc_create_device_data - creates and initializes device data structure for OAS
5676 * @pha: Pointer to host bus adapter structure.
5677 * @vport_wwpn: Pointer to vport's wwpn information
5678 * @target_wwpn: Pointer to target's wwpn information
5679 * @lun: Lun on target
5680 * @atomic_create: Flag to indicate if memory should be allocated using the
5681 * GFP_ATOMIC flag or not.
5682 *
5683 * This routine creates a device data structure which will contain identifying
5684 * information for the device (host wwpn, target wwpn, lun), state of OAS,
5685 * whether or not the corresponding lun is available by the system,
5686 * and pointer to the rport data.
5687 *
5688 * Return codes:
5689 * NULL - Error
5690 * Pointer to lpfc_device_data - Success
5691 **/
5692struct lpfc_device_data*
5693lpfc_create_device_data(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
5694 struct lpfc_name *target_wwpn, uint64_t lun,
James Smartb5749fe2016-12-19 15:07:26 -08005695 uint32_t pri, bool atomic_create)
James Smart1ba981f2014-02-20 09:56:45 -05005696{
5697
5698 struct lpfc_device_data *lun_info;
5699 int memory_flags;
5700
5701 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
James Smartf38fa0b2014-04-04 13:52:21 -04005702 !(phba->cfg_fof))
James Smart1ba981f2014-02-20 09:56:45 -05005703 return NULL;
5704
5705 /* Attempt to create the device data to contain lun info */
5706
5707 if (atomic_create)
5708 memory_flags = GFP_ATOMIC;
5709 else
5710 memory_flags = GFP_KERNEL;
5711 lun_info = mempool_alloc(phba->device_data_mem_pool, memory_flags);
5712 if (!lun_info)
5713 return NULL;
5714 INIT_LIST_HEAD(&lun_info->listentry);
5715 lun_info->rport_data = NULL;
5716 memcpy(&lun_info->device_id.vport_wwpn, vport_wwpn,
5717 sizeof(struct lpfc_name));
5718 memcpy(&lun_info->device_id.target_wwpn, target_wwpn,
5719 sizeof(struct lpfc_name));
5720 lun_info->device_id.lun = lun;
5721 lun_info->oas_enabled = false;
James Smartb5749fe2016-12-19 15:07:26 -08005722 lun_info->priority = pri;
James Smart1ba981f2014-02-20 09:56:45 -05005723 lun_info->available = false;
5724 return lun_info;
5725}
5726
5727/**
5728 * lpfc_delete_device_data - frees a device data structure for OAS
5729 * @pha: Pointer to host bus adapter structure.
5730 * @lun_info: Pointer to device data structure to free.
5731 *
5732 * This routine frees the previously allocated device data structure passed.
5733 *
5734 **/
5735void
5736lpfc_delete_device_data(struct lpfc_hba *phba,
5737 struct lpfc_device_data *lun_info)
5738{
5739
5740 if (unlikely(!phba) || !lun_info ||
James Smartf38fa0b2014-04-04 13:52:21 -04005741 !(phba->cfg_fof))
James Smart1ba981f2014-02-20 09:56:45 -05005742 return;
5743
5744 if (!list_empty(&lun_info->listentry))
5745 list_del(&lun_info->listentry);
5746 mempool_free(lun_info, phba->device_data_mem_pool);
5747 return;
5748}
5749
5750/**
5751 * __lpfc_get_device_data - returns the device data for the specified lun
5752 * @pha: Pointer to host bus adapter structure.
5753 * @list: Point to list to search.
5754 * @vport_wwpn: Pointer to vport's wwpn information
5755 * @target_wwpn: Pointer to target's wwpn information
5756 * @lun: Lun on target
5757 *
5758 * This routine searches the list passed for the specified lun's device data.
5759 * This function does not hold locks, it is the responsibility of the caller
5760 * to ensure the proper lock is held before calling the function.
5761 *
5762 * Return codes:
5763 * NULL - Error
5764 * Pointer to lpfc_device_data - Success
5765 **/
5766struct lpfc_device_data*
5767__lpfc_get_device_data(struct lpfc_hba *phba, struct list_head *list,
5768 struct lpfc_name *vport_wwpn,
5769 struct lpfc_name *target_wwpn, uint64_t lun)
5770{
5771
5772 struct lpfc_device_data *lun_info;
5773
5774 if (unlikely(!phba) || !list || !vport_wwpn || !target_wwpn ||
James Smartf38fa0b2014-04-04 13:52:21 -04005775 !phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005776 return NULL;
5777
5778 /* Check to see if the lun is already enabled for OAS. */
5779
5780 list_for_each_entry(lun_info, list, listentry) {
5781 if ((memcmp(&lun_info->device_id.vport_wwpn, vport_wwpn,
5782 sizeof(struct lpfc_name)) == 0) &&
5783 (memcmp(&lun_info->device_id.target_wwpn, target_wwpn,
5784 sizeof(struct lpfc_name)) == 0) &&
5785 (lun_info->device_id.lun == lun))
5786 return lun_info;
5787 }
5788
5789 return NULL;
5790}
5791
5792/**
5793 * lpfc_find_next_oas_lun - searches for the next oas lun
5794 * @pha: Pointer to host bus adapter structure.
5795 * @vport_wwpn: Pointer to vport's wwpn information
5796 * @target_wwpn: Pointer to target's wwpn information
5797 * @starting_lun: Pointer to the lun to start searching for
5798 * @found_vport_wwpn: Pointer to the found lun's vport wwpn information
5799 * @found_target_wwpn: Pointer to the found lun's target wwpn information
5800 * @found_lun: Pointer to the found lun.
5801 * @found_lun_status: Pointer to status of the found lun.
5802 *
5803 * This routine searches the luns list for the specified lun
5804 * or the first lun for the vport/target. If the vport wwpn contains
5805 * a zero value then a specific vport is not specified. In this case
5806 * any vport which contains the lun will be considered a match. If the
5807 * target wwpn contains a zero value then a specific target is not specified.
5808 * In this case any target which contains the lun will be considered a
5809 * match. If the lun is found, the lun, vport wwpn, target wwpn and lun status
5810 * are returned. The function will also return the next lun if available.
5811 * If the next lun is not found, starting_lun parameter will be set to
5812 * NO_MORE_OAS_LUN.
5813 *
5814 * Return codes:
5815 * non-0 - Error
5816 * 0 - Success
5817 **/
5818bool
5819lpfc_find_next_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
5820 struct lpfc_name *target_wwpn, uint64_t *starting_lun,
5821 struct lpfc_name *found_vport_wwpn,
5822 struct lpfc_name *found_target_wwpn,
5823 uint64_t *found_lun,
James Smartb5749fe2016-12-19 15:07:26 -08005824 uint32_t *found_lun_status,
5825 uint32_t *found_lun_pri)
James Smart1ba981f2014-02-20 09:56:45 -05005826{
5827
5828 unsigned long flags;
5829 struct lpfc_device_data *lun_info;
5830 struct lpfc_device_id *device_id;
5831 uint64_t lun;
5832 bool found = false;
5833
5834 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
5835 !starting_lun || !found_vport_wwpn ||
5836 !found_target_wwpn || !found_lun || !found_lun_status ||
5837 (*starting_lun == NO_MORE_OAS_LUN) ||
James Smartf38fa0b2014-04-04 13:52:21 -04005838 !phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005839 return false;
5840
5841 lun = *starting_lun;
5842 *found_lun = NO_MORE_OAS_LUN;
5843 *starting_lun = NO_MORE_OAS_LUN;
5844
5845 /* Search for lun or the lun closet in value */
5846
5847 spin_lock_irqsave(&phba->devicelock, flags);
5848 list_for_each_entry(lun_info, &phba->luns, listentry) {
5849 if (((wwn_to_u64(vport_wwpn->u.wwn) == 0) ||
5850 (memcmp(&lun_info->device_id.vport_wwpn, vport_wwpn,
5851 sizeof(struct lpfc_name)) == 0)) &&
5852 ((wwn_to_u64(target_wwpn->u.wwn) == 0) ||
5853 (memcmp(&lun_info->device_id.target_wwpn, target_wwpn,
5854 sizeof(struct lpfc_name)) == 0)) &&
5855 (lun_info->oas_enabled)) {
5856 device_id = &lun_info->device_id;
5857 if ((!found) &&
5858 ((lun == FIND_FIRST_OAS_LUN) ||
5859 (device_id->lun == lun))) {
5860 *found_lun = device_id->lun;
5861 memcpy(found_vport_wwpn,
5862 &device_id->vport_wwpn,
5863 sizeof(struct lpfc_name));
5864 memcpy(found_target_wwpn,
5865 &device_id->target_wwpn,
5866 sizeof(struct lpfc_name));
5867 if (lun_info->available)
5868 *found_lun_status =
5869 OAS_LUN_STATUS_EXISTS;
5870 else
5871 *found_lun_status = 0;
James Smartb5749fe2016-12-19 15:07:26 -08005872 *found_lun_pri = lun_info->priority;
James Smart1ba981f2014-02-20 09:56:45 -05005873 if (phba->cfg_oas_flags & OAS_FIND_ANY_VPORT)
5874 memset(vport_wwpn, 0x0,
5875 sizeof(struct lpfc_name));
5876 if (phba->cfg_oas_flags & OAS_FIND_ANY_TARGET)
5877 memset(target_wwpn, 0x0,
5878 sizeof(struct lpfc_name));
5879 found = true;
5880 } else if (found) {
5881 *starting_lun = device_id->lun;
5882 memcpy(vport_wwpn, &device_id->vport_wwpn,
5883 sizeof(struct lpfc_name));
5884 memcpy(target_wwpn, &device_id->target_wwpn,
5885 sizeof(struct lpfc_name));
5886 break;
5887 }
5888 }
5889 }
5890 spin_unlock_irqrestore(&phba->devicelock, flags);
5891 return found;
5892}
5893
5894/**
5895 * lpfc_enable_oas_lun - enables a lun for OAS operations
5896 * @pha: Pointer to host bus adapter structure.
5897 * @vport_wwpn: Pointer to vport's wwpn information
5898 * @target_wwpn: Pointer to target's wwpn information
5899 * @lun: Lun
5900 *
5901 * This routine enables a lun for oas operations. The routines does so by
5902 * doing the following :
5903 *
5904 * 1) Checks to see if the device data for the lun has been created.
5905 * 2) If found, sets the OAS enabled flag if not set and returns.
5906 * 3) Otherwise, creates a device data structure.
5907 * 4) If successfully created, indicates the device data is for an OAS lun,
5908 * indicates the lun is not available and add to the list of luns.
5909 *
5910 * Return codes:
5911 * false - Error
5912 * true - Success
5913 **/
5914bool
5915lpfc_enable_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
James Smartc92c8412016-07-06 12:36:05 -07005916 struct lpfc_name *target_wwpn, uint64_t lun, uint8_t pri)
James Smart1ba981f2014-02-20 09:56:45 -05005917{
5918
5919 struct lpfc_device_data *lun_info;
5920 unsigned long flags;
5921
5922 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
James Smartf38fa0b2014-04-04 13:52:21 -04005923 !phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005924 return false;
5925
5926 spin_lock_irqsave(&phba->devicelock, flags);
5927
5928 /* Check to see if the device data for the lun has been created */
5929 lun_info = __lpfc_get_device_data(phba, &phba->luns, vport_wwpn,
5930 target_wwpn, lun);
5931 if (lun_info) {
5932 if (!lun_info->oas_enabled)
5933 lun_info->oas_enabled = true;
James Smartb5749fe2016-12-19 15:07:26 -08005934 lun_info->priority = pri;
James Smart1ba981f2014-02-20 09:56:45 -05005935 spin_unlock_irqrestore(&phba->devicelock, flags);
5936 return true;
5937 }
5938
5939 /* Create an lun info structure and add to list of luns */
5940 lun_info = lpfc_create_device_data(phba, vport_wwpn, target_wwpn, lun,
James Smartb5749fe2016-12-19 15:07:26 -08005941 pri, false);
James Smart1ba981f2014-02-20 09:56:45 -05005942 if (lun_info) {
5943 lun_info->oas_enabled = true;
James Smartc92c8412016-07-06 12:36:05 -07005944 lun_info->priority = pri;
James Smart1ba981f2014-02-20 09:56:45 -05005945 lun_info->available = false;
5946 list_add_tail(&lun_info->listentry, &phba->luns);
5947 spin_unlock_irqrestore(&phba->devicelock, flags);
5948 return true;
5949 }
5950 spin_unlock_irqrestore(&phba->devicelock, flags);
5951 return false;
5952}
5953
5954/**
5955 * lpfc_disable_oas_lun - disables a lun for OAS operations
5956 * @pha: Pointer to host bus adapter structure.
5957 * @vport_wwpn: Pointer to vport's wwpn information
5958 * @target_wwpn: Pointer to target's wwpn information
5959 * @lun: Lun
5960 *
5961 * This routine disables a lun for oas operations. The routines does so by
5962 * doing the following :
5963 *
5964 * 1) Checks to see if the device data for the lun is created.
5965 * 2) If present, clears the flag indicating this lun is for OAS.
5966 * 3) If the lun is not available by the system, the device data is
5967 * freed.
5968 *
5969 * Return codes:
5970 * false - Error
5971 * true - Success
5972 **/
5973bool
5974lpfc_disable_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
James Smartb5749fe2016-12-19 15:07:26 -08005975 struct lpfc_name *target_wwpn, uint64_t lun, uint8_t pri)
James Smart1ba981f2014-02-20 09:56:45 -05005976{
5977
5978 struct lpfc_device_data *lun_info;
5979 unsigned long flags;
5980
5981 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
James Smartf38fa0b2014-04-04 13:52:21 -04005982 !phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005983 return false;
5984
5985 spin_lock_irqsave(&phba->devicelock, flags);
5986
5987 /* Check to see if the lun is available. */
5988 lun_info = __lpfc_get_device_data(phba,
5989 &phba->luns, vport_wwpn,
5990 target_wwpn, lun);
5991 if (lun_info) {
5992 lun_info->oas_enabled = false;
James Smartb5749fe2016-12-19 15:07:26 -08005993 lun_info->priority = pri;
James Smart1ba981f2014-02-20 09:56:45 -05005994 if (!lun_info->available)
5995 lpfc_delete_device_data(phba, lun_info);
5996 spin_unlock_irqrestore(&phba->devicelock, flags);
5997 return true;
5998 }
5999
6000 spin_unlock_irqrestore(&phba->devicelock, flags);
6001 return false;
6002}
James Smart92d7f7b2007-06-17 19:56:38 -05006003
James Smart895427b2017-02-12 13:52:30 -08006004static int
6005lpfc_no_command(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
6006{
6007 return SCSI_MLQUEUE_HOST_BUSY;
6008}
6009
6010static int
6011lpfc_no_handler(struct scsi_cmnd *cmnd)
6012{
6013 return FAILED;
6014}
6015
6016static int
6017lpfc_no_slave(struct scsi_device *sdev)
6018{
6019 return -ENODEV;
6020}
6021
6022struct scsi_host_template lpfc_template_nvme = {
6023 .module = THIS_MODULE,
6024 .name = LPFC_DRIVER_NAME,
6025 .proc_name = LPFC_DRIVER_NAME,
6026 .info = lpfc_info,
6027 .queuecommand = lpfc_no_command,
6028 .eh_abort_handler = lpfc_no_handler,
6029 .eh_device_reset_handler = lpfc_no_handler,
6030 .eh_target_reset_handler = lpfc_no_handler,
6031 .eh_bus_reset_handler = lpfc_no_handler,
6032 .eh_host_reset_handler = lpfc_no_handler,
6033 .slave_alloc = lpfc_no_slave,
6034 .slave_configure = lpfc_no_slave,
6035 .scan_finished = lpfc_scan_finished,
6036 .this_id = -1,
6037 .sg_tablesize = 1,
6038 .cmd_per_lun = 1,
6039 .use_clustering = ENABLE_CLUSTERING,
6040 .shost_attrs = lpfc_hba_attrs,
6041 .max_sectors = 0xFFFF,
6042 .vendor_id = LPFC_NL_VENDOR_ID,
6043 .track_queue_depth = 0,
6044};
6045
James Smart96418b52017-03-04 09:30:31 -08006046struct scsi_host_template lpfc_template_no_hr = {
James Smartea4142f2015-04-07 15:07:13 -04006047 .module = THIS_MODULE,
6048 .name = LPFC_DRIVER_NAME,
James Smart08dcd4c2016-07-06 12:35:59 -07006049 .proc_name = LPFC_DRIVER_NAME,
James Smartea4142f2015-04-07 15:07:13 -04006050 .info = lpfc_info,
6051 .queuecommand = lpfc_queuecommand,
James Smart856984b2017-03-04 09:30:32 -08006052 .eh_timed_out = fc_eh_timed_out,
James Smartea4142f2015-04-07 15:07:13 -04006053 .eh_abort_handler = lpfc_abort_handler,
6054 .eh_device_reset_handler = lpfc_device_reset_handler,
6055 .eh_target_reset_handler = lpfc_target_reset_handler,
6056 .eh_bus_reset_handler = lpfc_bus_reset_handler,
6057 .slave_alloc = lpfc_slave_alloc,
6058 .slave_configure = lpfc_slave_configure,
6059 .slave_destroy = lpfc_slave_destroy,
6060 .scan_finished = lpfc_scan_finished,
6061 .this_id = -1,
6062 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
6063 .cmd_per_lun = LPFC_CMD_PER_LUN,
6064 .use_clustering = ENABLE_CLUSTERING,
6065 .shost_attrs = lpfc_hba_attrs,
6066 .max_sectors = 0xFFFF,
6067 .vendor_id = LPFC_NL_VENDOR_ID,
6068 .change_queue_depth = scsi_change_queue_depth,
James Smartea4142f2015-04-07 15:07:13 -04006069 .track_queue_depth = 1,
6070};
6071
dea31012005-04-17 16:05:31 -05006072struct scsi_host_template lpfc_template = {
6073 .module = THIS_MODULE,
6074 .name = LPFC_DRIVER_NAME,
James Smart08dcd4c2016-07-06 12:35:59 -07006075 .proc_name = LPFC_DRIVER_NAME,
dea31012005-04-17 16:05:31 -05006076 .info = lpfc_info,
6077 .queuecommand = lpfc_queuecommand,
Christoph Hellwigb6a05c82017-01-30 13:18:58 +01006078 .eh_timed_out = fc_eh_timed_out,
dea31012005-04-17 16:05:31 -05006079 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04006080 .eh_device_reset_handler = lpfc_device_reset_handler,
6081 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart7054a602007-04-25 09:52:34 -04006082 .eh_bus_reset_handler = lpfc_bus_reset_handler,
James Smart27b01b82012-05-09 21:19:44 -04006083 .eh_host_reset_handler = lpfc_host_reset_handler,
dea31012005-04-17 16:05:31 -05006084 .slave_alloc = lpfc_slave_alloc,
6085 .slave_configure = lpfc_slave_configure,
6086 .slave_destroy = lpfc_slave_destroy,
James Smart47a86172007-04-25 09:53:22 -04006087 .scan_finished = lpfc_scan_finished,
dea31012005-04-17 16:05:31 -05006088 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05006089 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
dea31012005-04-17 16:05:31 -05006090 .cmd_per_lun = LPFC_CMD_PER_LUN,
6091 .use_clustering = ENABLE_CLUSTERING,
James Smart2e0fef82007-06-17 19:56:36 -05006092 .shost_attrs = lpfc_hba_attrs,
James.Smart@Emulex.Com564b2962005-06-25 10:34:17 -04006093 .max_sectors = 0xFFFF,
James Smartf1c3b0f2009-07-19 10:01:32 -04006094 .vendor_id = LPFC_NL_VENDOR_ID,
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01006095 .change_queue_depth = scsi_change_queue_depth,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01006096 .track_queue_depth = 1,
dea31012005-04-17 16:05:31 -05006097};
James Smart3de2a652007-08-02 11:09:59 -04006098
6099struct scsi_host_template lpfc_vport_template = {
6100 .module = THIS_MODULE,
6101 .name = LPFC_DRIVER_NAME,
James Smart08dcd4c2016-07-06 12:35:59 -07006102 .proc_name = LPFC_DRIVER_NAME,
James Smart3de2a652007-08-02 11:09:59 -04006103 .info = lpfc_info,
6104 .queuecommand = lpfc_queuecommand,
Christoph Hellwigb6a05c82017-01-30 13:18:58 +01006105 .eh_timed_out = fc_eh_timed_out,
James Smart3de2a652007-08-02 11:09:59 -04006106 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04006107 .eh_device_reset_handler = lpfc_device_reset_handler,
6108 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart3de2a652007-08-02 11:09:59 -04006109 .slave_alloc = lpfc_slave_alloc,
6110 .slave_configure = lpfc_slave_configure,
6111 .slave_destroy = lpfc_slave_destroy,
6112 .scan_finished = lpfc_scan_finished,
6113 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05006114 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
James Smart3de2a652007-08-02 11:09:59 -04006115 .cmd_per_lun = LPFC_CMD_PER_LUN,
6116 .use_clustering = ENABLE_CLUSTERING,
6117 .shost_attrs = lpfc_vport_attrs,
6118 .max_sectors = 0xFFFF,
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01006119 .change_queue_depth = scsi_change_queue_depth,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01006120 .track_queue_depth = 1,
James Smart3de2a652007-08-02 11:09:59 -04006121};