blob: 964a996dea2cc75a90315ec6ac5eb17e86170985 [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 Smartf25e8e72015-04-07 15:07:28 -04004 * Copyright (C) 2004-2015 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
dea31012005-04-17 16:05:31 -050021#include <linux/pci.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
dea31012005-04-17 16:05:31 -050023#include <linux/interrupt.h>
Paul Gortmaker09703662011-05-27 09:37:25 -040024#include <linux/export.h>
James Smarta90f5682006-08-17 11:58:04 -040025#include <linux/delay.h>
James Smarte2a0a9d2008-12-04 22:40:02 -050026#include <asm/unaligned.h>
James Smart737d4242013-04-17 20:14:49 -040027#include <linux/crc-t10dif.h>
28#include <net/checksum.h>
dea31012005-04-17 16:05:31 -050029
30#include <scsi/scsi.h>
31#include <scsi/scsi_device.h>
James Smarte2a0a9d2008-12-04 22:40:02 -050032#include <scsi/scsi_eh.h>
dea31012005-04-17 16:05:31 -050033#include <scsi/scsi_host.h>
34#include <scsi/scsi_tcq.h>
35#include <scsi/scsi_transport_fc.h>
36
37#include "lpfc_version.h"
James Smartda0436e2009-05-22 14:51:39 -040038#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050039#include "lpfc_hw.h"
40#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040041#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040042#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050043#include "lpfc_disc.h"
dea31012005-04-17 16:05:31 -050044#include "lpfc.h"
James Smart9a6b09c2012-03-01 22:37:42 -050045#include "lpfc_scsi.h"
dea31012005-04-17 16:05:31 -050046#include "lpfc_logmsg.h"
47#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050048#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050049
50#define LPFC_RESET_WAIT 2
51#define LPFC_ABORT_WAIT 2
52
James Smart737d4242013-04-17 20:14:49 -040053int _dump_buf_done = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -050054
55static char *dif_op_str[] = {
James Smart9a6b09c2012-03-01 22:37:42 -050056 "PROT_NORMAL",
57 "PROT_READ_INSERT",
58 "PROT_WRITE_STRIP",
59 "PROT_READ_STRIP",
60 "PROT_WRITE_INSERT",
61 "PROT_READ_PASS",
62 "PROT_WRITE_PASS",
63};
64
James Smartf9bb2da2011-10-10 21:34:11 -040065struct scsi_dif_tuple {
66 __be16 guard_tag; /* Checksum */
67 __be16 app_tag; /* Opaque storage */
68 __be32 ref_tag; /* Target LBA or indirect LBA */
69};
70
James Smart1ba981f2014-02-20 09:56:45 -050071static struct lpfc_rport_data *
72lpfc_rport_data_from_scsi_device(struct scsi_device *sdev)
73{
74 struct lpfc_vport *vport = (struct lpfc_vport *)sdev->host->hostdata;
75
James Smartf38fa0b2014-04-04 13:52:21 -040076 if (vport->phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -050077 return ((struct lpfc_device_data *)sdev->hostdata)->rport_data;
78 else
79 return (struct lpfc_rport_data *)sdev->hostdata;
80}
81
James Smartda0436e2009-05-22 14:51:39 -040082static void
83lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
James Smart1c6f4ef52009-11-18 15:40:49 -050084static void
85lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
James Smart9c6aa9d2013-05-31 17:03:39 -040086static int
87lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc);
James Smarte2a0a9d2008-12-04 22:40:02 -050088
89static void
James Smart6a9c52c2009-10-02 15:16:51 -040090lpfc_debug_save_data(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
James Smarte2a0a9d2008-12-04 22:40:02 -050091{
92 void *src, *dst;
93 struct scatterlist *sgde = scsi_sglist(cmnd);
94
95 if (!_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -040096 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
97 "9050 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
James Smarte2a0a9d2008-12-04 22:40:02 -050098 __func__);
99 return;
100 }
101
102
103 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -0400104 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
105 "9051 BLKGRD: ERROR: data scatterlist is null\n");
James Smarte2a0a9d2008-12-04 22:40:02 -0500106 return;
107 }
108
109 dst = (void *) _dump_buf_data;
110 while (sgde) {
111 src = sg_virt(sgde);
112 memcpy(dst, src, sgde->length);
113 dst += sgde->length;
114 sgde = sg_next(sgde);
115 }
116}
117
118static void
James Smart6a9c52c2009-10-02 15:16:51 -0400119lpfc_debug_save_dif(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
James Smarte2a0a9d2008-12-04 22:40:02 -0500120{
121 void *src, *dst;
122 struct scatterlist *sgde = scsi_prot_sglist(cmnd);
123
124 if (!_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -0400125 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
126 "9052 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
James Smarte2a0a9d2008-12-04 22:40:02 -0500127 __func__);
128 return;
129 }
130
131 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -0400132 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
133 "9053 BLKGRD: ERROR: prot scatterlist is null\n");
James Smarte2a0a9d2008-12-04 22:40:02 -0500134 return;
135 }
136
137 dst = _dump_buf_dif;
138 while (sgde) {
139 src = sg_virt(sgde);
140 memcpy(dst, src, sgde->length);
141 dst += sgde->length;
142 sgde = sg_next(sgde);
143 }
144}
145
James Smart9c6aa9d2013-05-31 17:03:39 -0400146static inline unsigned
147lpfc_cmd_blksize(struct scsi_cmnd *sc)
148{
149 return sc->device->sector_size;
150}
151
152#define LPFC_CHECK_PROTECT_GUARD 1
153#define LPFC_CHECK_PROTECT_REF 2
154static inline unsigned
155lpfc_cmd_protect(struct scsi_cmnd *sc, int flag)
156{
157 return 1;
158}
159
160static inline unsigned
161lpfc_cmd_guard_csum(struct scsi_cmnd *sc)
162{
163 if (lpfc_prot_group_type(NULL, sc) == LPFC_PG_TYPE_NO_DIF)
164 return 0;
165 if (scsi_host_get_guard(sc->device->host) == SHOST_DIX_GUARD_IP)
166 return 1;
167 return 0;
168}
169
James Smartea2151b2008-09-07 11:52:10 -0400170/**
James Smartf1126682009-06-10 17:22:44 -0400171 * lpfc_sli4_set_rsp_sgl_last - Set the last bit in the response sge.
172 * @phba: Pointer to HBA object.
173 * @lpfc_cmd: lpfc scsi command object pointer.
174 *
175 * This function is called from the lpfc_prep_task_mgmt_cmd function to
176 * set the last bit in the response sge entry.
177 **/
178static void
179lpfc_sli4_set_rsp_sgl_last(struct lpfc_hba *phba,
180 struct lpfc_scsi_buf *lpfc_cmd)
181{
182 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
183 if (sgl) {
184 sgl += 1;
185 sgl->word2 = le32_to_cpu(sgl->word2);
186 bf_set(lpfc_sli4_sge_last, sgl, 1);
187 sgl->word2 = cpu_to_le32(sgl->word2);
188 }
189}
190
191/**
James Smart3621a712009-04-06 18:47:14 -0400192 * lpfc_update_stats - Update statistical data for the command completion
James Smartea2151b2008-09-07 11:52:10 -0400193 * @phba: Pointer to HBA object.
194 * @lpfc_cmd: lpfc scsi command object pointer.
195 *
196 * This function is called when there is a command completion and this
197 * function updates the statistical data for the command completion.
198 **/
199static void
200lpfc_update_stats(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
201{
202 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
203 struct lpfc_nodelist *pnode = rdata->pnode;
204 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
205 unsigned long flags;
206 struct Scsi_Host *shost = cmd->device->host;
207 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
208 unsigned long latency;
209 int i;
210
211 if (cmd->result)
212 return;
213
James Smart9f1e1b52008-12-04 22:39:40 -0500214 latency = jiffies_to_msecs((long)jiffies - (long)lpfc_cmd->start_time);
215
James Smartea2151b2008-09-07 11:52:10 -0400216 spin_lock_irqsave(shost->host_lock, flags);
217 if (!vport->stat_data_enabled ||
218 vport->stat_data_blocked ||
James Smart5989b8d2010-10-22 11:06:56 -0400219 !pnode ||
James Smartea2151b2008-09-07 11:52:10 -0400220 !pnode->lat_data ||
221 (phba->bucket_type == LPFC_NO_BUCKET)) {
222 spin_unlock_irqrestore(shost->host_lock, flags);
223 return;
224 }
James Smartea2151b2008-09-07 11:52:10 -0400225
226 if (phba->bucket_type == LPFC_LINEAR_BUCKET) {
227 i = (latency + phba->bucket_step - 1 - phba->bucket_base)/
228 phba->bucket_step;
James Smart9f1e1b52008-12-04 22:39:40 -0500229 /* check array subscript bounds */
230 if (i < 0)
231 i = 0;
232 else if (i >= LPFC_MAX_BUCKET_COUNT)
233 i = LPFC_MAX_BUCKET_COUNT - 1;
James Smartea2151b2008-09-07 11:52:10 -0400234 } else {
235 for (i = 0; i < LPFC_MAX_BUCKET_COUNT-1; i++)
236 if (latency <= (phba->bucket_base +
237 ((1<<i)*phba->bucket_step)))
238 break;
239 }
240
241 pnode->lat_data[i].cmd_count++;
242 spin_unlock_irqrestore(shost->host_lock, flags);
243}
244
James Smartea2151b2008-09-07 11:52:10 -0400245/**
James Smart3621a712009-04-06 18:47:14 -0400246 * lpfc_rampdown_queue_depth - Post RAMP_DOWN_QUEUE event to worker thread
James Smart9bad7672008-12-04 22:39:02 -0500247 * @phba: The Hba for which this call is being executed.
248 *
249 * This routine is called when there is resource error in driver or firmware.
250 * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
251 * posts at most 1 event each second. This routine wakes up worker thread of
252 * @phba to process WORKER_RAM_DOWN_EVENT event.
253 *
254 * This routine should be called with no lock held.
255 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500256void
James Smarteaf15d52008-12-04 22:39:29 -0500257lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
James Smart92d7f7b2007-06-17 19:56:38 -0500258{
259 unsigned long flags;
James Smart5e9d9b82008-06-14 22:52:53 -0400260 uint32_t evt_posted;
Manuel Schölling0d4aec12014-09-03 12:55:58 -0400261 unsigned long expires;
James Smart92d7f7b2007-06-17 19:56:38 -0500262
263 spin_lock_irqsave(&phba->hbalock, flags);
264 atomic_inc(&phba->num_rsrc_err);
265 phba->last_rsrc_error_time = jiffies;
266
Manuel Schölling0d4aec12014-09-03 12:55:58 -0400267 expires = phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL;
268 if (time_after(expires, jiffies)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500269 spin_unlock_irqrestore(&phba->hbalock, flags);
270 return;
271 }
272
273 phba->last_ramp_down_time = jiffies;
274
275 spin_unlock_irqrestore(&phba->hbalock, flags);
276
277 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400278 evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
279 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500280 phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500281 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
282
James Smart5e9d9b82008-06-14 22:52:53 -0400283 if (!evt_posted)
284 lpfc_worker_wake_up(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500285 return;
286}
287
James Smart9bad7672008-12-04 22:39:02 -0500288/**
James Smart3621a712009-04-06 18:47:14 -0400289 * lpfc_ramp_down_queue_handler - WORKER_RAMP_DOWN_QUEUE event handler
James Smart9bad7672008-12-04 22:39:02 -0500290 * @phba: The Hba for which this call is being executed.
291 *
292 * This routine is called to process WORKER_RAMP_DOWN_QUEUE event for worker
293 * thread.This routine reduces queue depth for all scsi device on each vport
294 * associated with @phba.
295 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500296void
297lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
298{
James Smart549e55c2007-08-02 11:09:51 -0400299 struct lpfc_vport **vports;
300 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500301 struct scsi_device *sdev;
James Smart5ffc2662009-11-18 15:39:44 -0500302 unsigned long new_queue_depth;
James Smart92d7f7b2007-06-17 19:56:38 -0500303 unsigned long num_rsrc_err, num_cmd_success;
James Smart549e55c2007-08-02 11:09:51 -0400304 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500305
306 num_rsrc_err = atomic_read(&phba->num_rsrc_err);
307 num_cmd_success = atomic_read(&phba->num_cmd_success);
308
James Smart75ad83a2012-05-09 21:18:40 -0400309 /*
310 * The error and success command counters are global per
311 * driver instance. If another handler has already
312 * operated on this error event, just exit.
313 */
314 if (num_rsrc_err == 0)
315 return;
316
James Smart549e55c2007-08-02 11:09:51 -0400317 vports = lpfc_create_vport_work_array(phba);
318 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400319 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400320 shost = lpfc_shost_from_vport(vports[i]);
321 shost_for_each_device(sdev, shost) {
James Smart92d7f7b2007-06-17 19:56:38 -0500322 new_queue_depth =
James Smart549e55c2007-08-02 11:09:51 -0400323 sdev->queue_depth * num_rsrc_err /
324 (num_rsrc_err + num_cmd_success);
325 if (!new_queue_depth)
326 new_queue_depth = sdev->queue_depth - 1;
327 else
328 new_queue_depth = sdev->queue_depth -
329 new_queue_depth;
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +0100330 scsi_change_queue_depth(sdev, new_queue_depth);
James Smart549e55c2007-08-02 11:09:51 -0400331 }
James Smart92d7f7b2007-06-17 19:56:38 -0500332 }
James Smart09372822008-01-11 01:52:54 -0500333 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500334 atomic_set(&phba->num_rsrc_err, 0);
335 atomic_set(&phba->num_cmd_success, 0);
336}
337
James Smart9bad7672008-12-04 22:39:02 -0500338/**
James Smart3621a712009-04-06 18:47:14 -0400339 * lpfc_scsi_dev_block - set all scsi hosts to block state
James Smarta8e497d2008-08-24 21:50:11 -0400340 * @phba: Pointer to HBA context object.
341 *
342 * This function walks vport list and set each SCSI host to block state
343 * by invoking fc_remote_port_delete() routine. This function is invoked
344 * with EEH when device's PCI slot has been permanently disabled.
345 **/
346void
347lpfc_scsi_dev_block(struct lpfc_hba *phba)
348{
349 struct lpfc_vport **vports;
350 struct Scsi_Host *shost;
351 struct scsi_device *sdev;
352 struct fc_rport *rport;
353 int i;
354
355 vports = lpfc_create_vport_work_array(phba);
356 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400357 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8e497d2008-08-24 21:50:11 -0400358 shost = lpfc_shost_from_vport(vports[i]);
359 shost_for_each_device(sdev, shost) {
360 rport = starget_to_rport(scsi_target(sdev));
361 fc_remote_port_delete(rport);
362 }
363 }
364 lpfc_destroy_vport_work_array(phba, vports);
365}
366
James Smart9bad7672008-12-04 22:39:02 -0500367/**
James Smart3772a992009-05-22 14:50:54 -0400368 * lpfc_new_scsi_buf_s3 - Scsi buffer allocator for HBA with SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -0500369 * @vport: The virtual port for which this call being executed.
James Smart3772a992009-05-22 14:50:54 -0400370 * @num_to_allocate: The requested number of buffers to allocate.
James Smart9bad7672008-12-04 22:39:02 -0500371 *
James Smart3772a992009-05-22 14:50:54 -0400372 * This routine allocates a scsi buffer for device with SLI-3 interface spec,
373 * the scsi buffer contains all the necessary information needed to initiate
374 * a SCSI I/O. The non-DMAable buffer region contains information to build
375 * the IOCB. The DMAable region contains memory for the FCP CMND, FCP RSP,
376 * and the initial BPL. In addition to allocating memory, the FCP CMND and
377 * FCP RSP BDEs are setup in the BPL and the BPL BDE is setup in the IOCB.
James Smart9bad7672008-12-04 22:39:02 -0500378 *
379 * Return codes:
James Smart3772a992009-05-22 14:50:54 -0400380 * int - number of scsi buffers that were allocated.
381 * 0 = failure, less than num_to_alloc is a partial failure.
James Smart9bad7672008-12-04 22:39:02 -0500382 **/
James Smart3772a992009-05-22 14:50:54 -0400383static int
384lpfc_new_scsi_buf_s3(struct lpfc_vport *vport, int num_to_alloc)
dea31012005-04-17 16:05:31 -0500385{
James Smart2e0fef82007-06-17 19:56:36 -0500386 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500387 struct lpfc_scsi_buf *psb;
388 struct ulp_bde64 *bpl;
389 IOCB_t *iocb;
James Smart34b02dc2008-08-24 21:49:55 -0400390 dma_addr_t pdma_phys_fcp_cmd;
391 dma_addr_t pdma_phys_fcp_rsp;
392 dma_addr_t pdma_phys_bpl;
James Bottomley604a3e32005-10-29 10:28:33 -0500393 uint16_t iotag;
James Smart96f70772013-04-17 20:16:15 -0400394 int bcnt, bpl_size;
395
396 bpl_size = phba->cfg_sg_dma_buf_size -
397 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
398
399 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
400 "9067 ALLOC %d scsi_bufs: %d (%d + %d + %d)\n",
401 num_to_alloc, phba->cfg_sg_dma_buf_size,
402 (int)sizeof(struct fcp_cmnd),
403 (int)sizeof(struct fcp_rsp), bpl_size);
dea31012005-04-17 16:05:31 -0500404
James Smart3772a992009-05-22 14:50:54 -0400405 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
406 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
407 if (!psb)
408 break;
dea31012005-04-17 16:05:31 -0500409
James Smart3772a992009-05-22 14:50:54 -0400410 /*
411 * Get memory from the pci pool to map the virt space to pci
412 * bus space for an I/O. The DMA buffer includes space for the
413 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
414 * necessary to support the sg_tablesize.
415 */
416 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
417 GFP_KERNEL, &psb->dma_handle);
418 if (!psb->data) {
419 kfree(psb);
420 break;
421 }
dea31012005-04-17 16:05:31 -0500422
James Smart3772a992009-05-22 14:50:54 -0400423 /* Initialize virtual ptrs to dma_buf region. */
424 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
dea31012005-04-17 16:05:31 -0500425
James Smart3772a992009-05-22 14:50:54 -0400426 /* Allocate iotag for psb->cur_iocbq. */
427 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
428 if (iotag == 0) {
429 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
430 psb->data, psb->dma_handle);
431 kfree(psb);
432 break;
433 }
434 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Bottomley604a3e32005-10-29 10:28:33 -0500435
James Smart3772a992009-05-22 14:50:54 -0400436 psb->fcp_cmnd = psb->data;
437 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
438 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
James Smart34b02dc2008-08-24 21:49:55 -0400439 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500440
James Smart3772a992009-05-22 14:50:54 -0400441 /* Initialize local short-hand pointers. */
442 bpl = psb->fcp_bpl;
443 pdma_phys_fcp_cmd = psb->dma_handle;
444 pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
445 pdma_phys_bpl = psb->dma_handle + sizeof(struct fcp_cmnd) +
446 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500447
James Smart3772a992009-05-22 14:50:54 -0400448 /*
449 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
450 * are sg list bdes. Initialize the first two and leave the
451 * rest for queuecommand.
452 */
453 bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
454 bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
455 bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
456 bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
457 bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
dea31012005-04-17 16:05:31 -0500458
James Smart3772a992009-05-22 14:50:54 -0400459 /* Setup the physical region for the FCP RSP */
460 bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
461 bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
462 bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
463 bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
464 bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
465
466 /*
467 * Since the IOCB for the FCP I/O is built into this
468 * lpfc_scsi_buf, initialize it with all known data now.
469 */
470 iocb = &psb->cur_iocbq.iocb;
471 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
472 if ((phba->sli_rev == 3) &&
473 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
474 /* fill in immediate fcp command BDE */
475 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
476 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
477 iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
478 unsli3.fcp_ext.icd);
479 iocb->un.fcpi64.bdl.addrHigh = 0;
480 iocb->ulpBdeCount = 0;
481 iocb->ulpLe = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300482 /* fill in response BDE */
James Smart3772a992009-05-22 14:50:54 -0400483 iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags =
484 BUFF_TYPE_BDE_64;
485 iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
486 sizeof(struct fcp_rsp);
487 iocb->unsli3.fcp_ext.rbde.addrLow =
488 putPaddrLow(pdma_phys_fcp_rsp);
489 iocb->unsli3.fcp_ext.rbde.addrHigh =
490 putPaddrHigh(pdma_phys_fcp_rsp);
491 } else {
492 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
493 iocb->un.fcpi64.bdl.bdeSize =
494 (2 * sizeof(struct ulp_bde64));
495 iocb->un.fcpi64.bdl.addrLow =
496 putPaddrLow(pdma_phys_bpl);
497 iocb->un.fcpi64.bdl.addrHigh =
498 putPaddrHigh(pdma_phys_bpl);
499 iocb->ulpBdeCount = 1;
500 iocb->ulpLe = 1;
501 }
502 iocb->ulpClass = CLASS3;
503 psb->status = IOSTAT_SUCCESS;
James Smartda0436e2009-05-22 14:51:39 -0400504 /* Put it back into the SCSI buffer list */
James Smarteee88772010-09-29 11:19:08 -0400505 psb->cur_iocbq.context1 = psb;
James Smart1c6f4ef52009-11-18 15:40:49 -0500506 lpfc_release_scsi_buf_s3(phba, psb);
James Smart3772a992009-05-22 14:50:54 -0400507
James Smart34b02dc2008-08-24 21:49:55 -0400508 }
dea31012005-04-17 16:05:31 -0500509
James Smart3772a992009-05-22 14:50:54 -0400510 return bcnt;
dea31012005-04-17 16:05:31 -0500511}
512
James Smart9bad7672008-12-04 22:39:02 -0500513/**
James Smart1151e3e2011-02-16 12:39:35 -0500514 * lpfc_sli4_vport_delete_fcp_xri_aborted -Remove all ndlp references for vport
515 * @vport: pointer to lpfc vport data structure.
516 *
517 * This routine is invoked by the vport cleanup for deletions and the cleanup
518 * for an ndlp on removal.
519 **/
520void
521lpfc_sli4_vport_delete_fcp_xri_aborted(struct lpfc_vport *vport)
522{
523 struct lpfc_hba *phba = vport->phba;
524 struct lpfc_scsi_buf *psb, *next_psb;
525 unsigned long iflag = 0;
526
527 spin_lock_irqsave(&phba->hbalock, iflag);
528 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
529 list_for_each_entry_safe(psb, next_psb,
530 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
531 if (psb->rdata && psb->rdata->pnode
532 && psb->rdata->pnode->vport == vport)
533 psb->rdata = NULL;
534 }
535 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
536 spin_unlock_irqrestore(&phba->hbalock, iflag);
537}
538
539/**
James Smartda0436e2009-05-22 14:51:39 -0400540 * lpfc_sli4_fcp_xri_aborted - Fast-path process of fcp xri abort
541 * @phba: pointer to lpfc hba data structure.
542 * @axri: pointer to the fcp xri abort wcqe structure.
543 *
544 * This routine is invoked by the worker thread to process a SLI4 fast-path
545 * FCP aborted xri.
546 **/
547void
548lpfc_sli4_fcp_xri_aborted(struct lpfc_hba *phba,
549 struct sli4_wcqe_xri_aborted *axri)
550{
551 uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
James Smart19ca7602010-11-20 23:11:55 -0500552 uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
James Smartda0436e2009-05-22 14:51:39 -0400553 struct lpfc_scsi_buf *psb, *next_psb;
554 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -0500555 struct lpfc_iocbq *iocbq;
556 int i;
James Smart19ca7602010-11-20 23:11:55 -0500557 struct lpfc_nodelist *ndlp;
558 int rrq_empty = 0;
James Smart589a52d2010-07-14 15:30:54 -0400559 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smartda0436e2009-05-22 14:51:39 -0400560
James Smart0f65ff62010-02-26 14:14:23 -0500561 spin_lock_irqsave(&phba->hbalock, iflag);
562 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
James Smartda0436e2009-05-22 14:51:39 -0400563 list_for_each_entry_safe(psb, next_psb,
564 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
565 if (psb->cur_iocbq.sli4_xritag == xri) {
566 list_del(&psb->list);
James Smart341af102010-01-26 23:07:37 -0500567 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400568 psb->status = IOSTAT_SUCCESS;
James Smart0f65ff62010-02-26 14:14:23 -0500569 spin_unlock(
570 &phba->sli4_hba.abts_scsi_buf_list_lock);
James Smart1151e3e2011-02-16 12:39:35 -0500571 if (psb->rdata && psb->rdata->pnode)
572 ndlp = psb->rdata->pnode;
573 else
574 ndlp = NULL;
575
James Smart19ca7602010-11-20 23:11:55 -0500576 rrq_empty = list_empty(&phba->active_rrq_list);
James Smart0f65ff62010-02-26 14:14:23 -0500577 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartcb69f7d2011-12-13 13:21:57 -0500578 if (ndlp) {
James Smartee0f4fe2012-05-09 21:19:14 -0400579 lpfc_set_rrq_active(phba, ndlp,
580 psb->cur_iocbq.sli4_lxritag, rxid, 1);
James Smartcb69f7d2011-12-13 13:21:57 -0500581 lpfc_sli4_abts_err_handler(phba, ndlp, axri);
582 }
James Smartda0436e2009-05-22 14:51:39 -0400583 lpfc_release_scsi_buf_s4(phba, psb);
James Smart19ca7602010-11-20 23:11:55 -0500584 if (rrq_empty)
585 lpfc_worker_wake_up(phba);
James Smartda0436e2009-05-22 14:51:39 -0400586 return;
587 }
588 }
James Smart0f65ff62010-02-26 14:14:23 -0500589 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
590 for (i = 1; i <= phba->sli.last_iotag; i++) {
591 iocbq = phba->sli.iocbq_lookup[i];
592
593 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
594 (iocbq->iocb_flag & LPFC_IO_LIBDFC))
595 continue;
596 if (iocbq->sli4_xritag != xri)
597 continue;
598 psb = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
599 psb->exch_busy = 0;
600 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart0e9bb8d2013-03-01 16:35:12 -0500601 if (!list_empty(&pring->txq))
James Smart589a52d2010-07-14 15:30:54 -0400602 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -0500603 return;
604
605 }
606 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartda0436e2009-05-22 14:51:39 -0400607}
608
609/**
James Smart8a9d2e82012-05-09 21:16:12 -0400610 * lpfc_sli4_post_scsi_sgl_list - Psot blocks of scsi buffer sgls from a list
611 * @phba: pointer to lpfc hba data structure.
612 * @post_sblist: pointer to the scsi buffer list.
613 *
614 * This routine walks a list of scsi buffers that was passed in. It attempts
615 * to construct blocks of scsi buffer sgls which contains contiguous xris and
616 * uses the non-embedded SGL block post mailbox commands to post to the port.
617 * For single SCSI buffer sgl with non-contiguous xri, if any, it shall use
618 * embedded SGL post mailbox command for posting. The @post_sblist passed in
619 * must be local list, thus no lock is needed when manipulate the list.
620 *
621 * Returns: 0 = failure, non-zero number of successfully posted buffers.
622 **/
Rashika Kheria7bfe7812014-09-03 12:55:36 -0400623static int
James Smart8a9d2e82012-05-09 21:16:12 -0400624lpfc_sli4_post_scsi_sgl_list(struct lpfc_hba *phba,
625 struct list_head *post_sblist, int sb_count)
626{
627 struct lpfc_scsi_buf *psb, *psb_next;
James Smart96f70772013-04-17 20:16:15 -0400628 int status, sgl_size;
James Smart8a9d2e82012-05-09 21:16:12 -0400629 int post_cnt = 0, block_cnt = 0, num_posting = 0, num_posted = 0;
630 dma_addr_t pdma_phys_bpl1;
631 int last_xritag = NO_XRI;
632 LIST_HEAD(prep_sblist);
633 LIST_HEAD(blck_sblist);
634 LIST_HEAD(scsi_sblist);
635
636 /* sanity check */
637 if (sb_count <= 0)
638 return -EINVAL;
639
James Smart96f70772013-04-17 20:16:15 -0400640 sgl_size = phba->cfg_sg_dma_buf_size -
641 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
642
James Smart8a9d2e82012-05-09 21:16:12 -0400643 list_for_each_entry_safe(psb, psb_next, post_sblist, list) {
644 list_del_init(&psb->list);
645 block_cnt++;
646 if ((last_xritag != NO_XRI) &&
647 (psb->cur_iocbq.sli4_xritag != last_xritag + 1)) {
648 /* a hole in xri block, form a sgl posting block */
649 list_splice_init(&prep_sblist, &blck_sblist);
650 post_cnt = block_cnt - 1;
651 /* prepare list for next posting block */
652 list_add_tail(&psb->list, &prep_sblist);
653 block_cnt = 1;
654 } else {
655 /* prepare list for next posting block */
656 list_add_tail(&psb->list, &prep_sblist);
657 /* enough sgls for non-embed sgl mbox command */
658 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
659 list_splice_init(&prep_sblist, &blck_sblist);
660 post_cnt = block_cnt;
661 block_cnt = 0;
662 }
663 }
664 num_posting++;
665 last_xritag = psb->cur_iocbq.sli4_xritag;
666
667 /* end of repost sgl list condition for SCSI buffers */
668 if (num_posting == sb_count) {
669 if (post_cnt == 0) {
670 /* last sgl posting block */
671 list_splice_init(&prep_sblist, &blck_sblist);
672 post_cnt = block_cnt;
673 } else if (block_cnt == 1) {
674 /* last single sgl with non-contiguous xri */
James Smart96f70772013-04-17 20:16:15 -0400675 if (sgl_size > SGL_PAGE_SIZE)
James Smart8a9d2e82012-05-09 21:16:12 -0400676 pdma_phys_bpl1 = psb->dma_phys_bpl +
677 SGL_PAGE_SIZE;
678 else
679 pdma_phys_bpl1 = 0;
680 status = lpfc_sli4_post_sgl(phba,
681 psb->dma_phys_bpl,
682 pdma_phys_bpl1,
683 psb->cur_iocbq.sli4_xritag);
684 if (status) {
685 /* failure, put on abort scsi list */
686 psb->exch_busy = 1;
687 } else {
688 /* success, put on SCSI buffer list */
689 psb->exch_busy = 0;
690 psb->status = IOSTAT_SUCCESS;
691 num_posted++;
692 }
693 /* success, put on SCSI buffer sgl list */
694 list_add_tail(&psb->list, &scsi_sblist);
695 }
696 }
697
698 /* continue until a nembed page worth of sgls */
699 if (post_cnt == 0)
700 continue;
701
702 /* post block of SCSI buffer list sgls */
703 status = lpfc_sli4_post_scsi_sgl_block(phba, &blck_sblist,
704 post_cnt);
705
706 /* don't reset xirtag due to hole in xri block */
707 if (block_cnt == 0)
708 last_xritag = NO_XRI;
709
710 /* reset SCSI buffer post count for next round of posting */
711 post_cnt = 0;
712
713 /* put posted SCSI buffer-sgl posted on SCSI buffer sgl list */
714 while (!list_empty(&blck_sblist)) {
715 list_remove_head(&blck_sblist, psb,
716 struct lpfc_scsi_buf, list);
717 if (status) {
718 /* failure, put on abort scsi list */
719 psb->exch_busy = 1;
720 } else {
721 /* success, put on SCSI buffer list */
722 psb->exch_busy = 0;
723 psb->status = IOSTAT_SUCCESS;
724 num_posted++;
725 }
726 list_add_tail(&psb->list, &scsi_sblist);
727 }
728 }
729 /* Push SCSI buffers with sgl posted to the availble list */
730 while (!list_empty(&scsi_sblist)) {
731 list_remove_head(&scsi_sblist, psb,
732 struct lpfc_scsi_buf, list);
733 lpfc_release_scsi_buf_s4(phba, psb);
734 }
735 return num_posted;
736}
737
738/**
739 * lpfc_sli4_repost_scsi_sgl_list - Repsot all the allocated scsi buffer sgls
James Smartda0436e2009-05-22 14:51:39 -0400740 * @phba: pointer to lpfc hba data structure.
741 *
742 * This routine walks the list of scsi buffers that have been allocated and
James Smart8a9d2e82012-05-09 21:16:12 -0400743 * repost them to the port by using SGL block post. This is needed after a
James Smartda0436e2009-05-22 14:51:39 -0400744 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
745 * is responsible for moving all scsi buffers on the lpfc_abts_scsi_sgl_list
746 * to the lpfc_scsi_buf_list. If the repost fails, reject all scsi buffers.
747 *
748 * Returns: 0 = success, non-zero failure.
749 **/
750int
751lpfc_sli4_repost_scsi_sgl_list(struct lpfc_hba *phba)
752{
James Smart8a9d2e82012-05-09 21:16:12 -0400753 LIST_HEAD(post_sblist);
754 int num_posted, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -0400755
James Smart8a9d2e82012-05-09 21:16:12 -0400756 /* get all SCSI buffers need to repost to a local list */
James Smarta40fc5f2013-04-17 20:17:40 -0400757 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart164cecd2013-09-06 12:22:38 -0400758 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -0400759 list_splice_init(&phba->lpfc_scsi_buf_list_get, &post_sblist);
760 list_splice(&phba->lpfc_scsi_buf_list_put, &post_sblist);
James Smart164cecd2013-09-06 12:22:38 -0400761 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -0400762 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smartda0436e2009-05-22 14:51:39 -0400763
James Smart8a9d2e82012-05-09 21:16:12 -0400764 /* post the list of scsi buffer sgls to port if available */
765 if (!list_empty(&post_sblist)) {
766 num_posted = lpfc_sli4_post_scsi_sgl_list(phba, &post_sblist,
767 phba->sli4_hba.scsi_xri_cnt);
768 /* failed to post any scsi buffer, return error */
769 if (num_posted == 0)
770 rc = -EIO;
James Smartda0436e2009-05-22 14:51:39 -0400771 }
772 return rc;
773}
774
775/**
776 * lpfc_new_scsi_buf_s4 - Scsi buffer allocator for HBA with SLI4 IF spec
777 * @vport: The virtual port for which this call being executed.
778 * @num_to_allocate: The requested number of buffers to allocate.
779 *
James Smart8a9d2e82012-05-09 21:16:12 -0400780 * This routine allocates scsi buffers for device with SLI-4 interface spec,
James Smartda0436e2009-05-22 14:51:39 -0400781 * the scsi buffer contains all the necessary information needed to initiate
James Smart8a9d2e82012-05-09 21:16:12 -0400782 * a SCSI I/O. After allocating up to @num_to_allocate SCSI buffers and put
783 * them on a list, it post them to the port by using SGL block post.
James Smartda0436e2009-05-22 14:51:39 -0400784 *
785 * Return codes:
James Smart8a9d2e82012-05-09 21:16:12 -0400786 * int - number of scsi buffers that were allocated and posted.
James Smartda0436e2009-05-22 14:51:39 -0400787 * 0 = failure, less than num_to_alloc is a partial failure.
788 **/
789static int
790lpfc_new_scsi_buf_s4(struct lpfc_vport *vport, int num_to_alloc)
791{
792 struct lpfc_hba *phba = vport->phba;
793 struct lpfc_scsi_buf *psb;
794 struct sli4_sge *sgl;
795 IOCB_t *iocb;
796 dma_addr_t pdma_phys_fcp_cmd;
797 dma_addr_t pdma_phys_fcp_rsp;
James Smart96f70772013-04-17 20:16:15 -0400798 dma_addr_t pdma_phys_bpl;
James Smart8a9d2e82012-05-09 21:16:12 -0400799 uint16_t iotag, lxri = 0;
James Smart96f70772013-04-17 20:16:15 -0400800 int bcnt, num_posted, sgl_size;
James Smart8a9d2e82012-05-09 21:16:12 -0400801 LIST_HEAD(prep_sblist);
802 LIST_HEAD(post_sblist);
803 LIST_HEAD(scsi_sblist);
James Smartda0436e2009-05-22 14:51:39 -0400804
James Smart96f70772013-04-17 20:16:15 -0400805 sgl_size = phba->cfg_sg_dma_buf_size -
806 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
807
808 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
809 "9068 ALLOC %d scsi_bufs: %d (%d + %d + %d)\n",
810 num_to_alloc, phba->cfg_sg_dma_buf_size, sgl_size,
811 (int)sizeof(struct fcp_cmnd),
812 (int)sizeof(struct fcp_rsp));
813
James Smartda0436e2009-05-22 14:51:39 -0400814 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
815 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
816 if (!psb)
817 break;
James Smartda0436e2009-05-22 14:51:39 -0400818 /*
James Smart8a9d2e82012-05-09 21:16:12 -0400819 * Get memory from the pci pool to map the virt space to
820 * pci bus space for an I/O. The DMA buffer includes space
821 * for the struct fcp_cmnd, struct fcp_rsp and the number
822 * of bde's necessary to support the sg_tablesize.
James Smartda0436e2009-05-22 14:51:39 -0400823 */
824 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
825 GFP_KERNEL, &psb->dma_handle);
826 if (!psb->data) {
827 kfree(psb);
828 break;
829 }
James Smartda0436e2009-05-22 14:51:39 -0400830 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
831
James Smart092cb032013-09-06 12:21:50 -0400832 /*
833 * 4K Page alignment is CRITICAL to BlockGuard, double check
834 * to be sure.
835 */
836 if (phba->cfg_enable_bg && (((unsigned long)(psb->data) &
837 (unsigned long)(SLI4_PAGE_SIZE - 1)) != 0)) {
James Smart96f70772013-04-17 20:16:15 -0400838 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
839 psb->data, psb->dma_handle);
840 kfree(psb);
841 break;
842 }
843
James Smartda0436e2009-05-22 14:51:39 -0400844
James Smart6d368e52011-05-24 11:44:12 -0400845 lxri = lpfc_sli4_next_xritag(phba);
846 if (lxri == NO_XRI) {
James Smartda0436e2009-05-22 14:51:39 -0400847 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
848 psb->data, psb->dma_handle);
849 kfree(psb);
850 break;
851 }
James Smartf7bc6432013-10-10 12:19:53 -0400852
853 /* Allocate iotag for psb->cur_iocbq. */
854 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
855 if (iotag == 0) {
856 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
857 psb->data, psb->dma_handle);
858 kfree(psb);
859 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
860 "3368 Failed to allocated IOTAG for"
861 " XRI:0x%x\n", lxri);
862 lpfc_sli4_free_xri(phba, lxri);
863 break;
864 }
James Smart6d368e52011-05-24 11:44:12 -0400865 psb->cur_iocbq.sli4_lxritag = lxri;
866 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
James Smartda0436e2009-05-22 14:51:39 -0400867 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Smartda0436e2009-05-22 14:51:39 -0400868 psb->fcp_bpl = psb->data;
James Smart96f70772013-04-17 20:16:15 -0400869 psb->fcp_cmnd = (psb->data + sgl_size);
James Smartda0436e2009-05-22 14:51:39 -0400870 psb->fcp_rsp = (struct fcp_rsp *)((uint8_t *)psb->fcp_cmnd +
871 sizeof(struct fcp_cmnd));
872
873 /* Initialize local short-hand pointers. */
874 sgl = (struct sli4_sge *)psb->fcp_bpl;
875 pdma_phys_bpl = psb->dma_handle;
James Smart96f70772013-04-17 20:16:15 -0400876 pdma_phys_fcp_cmd = (psb->dma_handle + sgl_size);
James Smartda0436e2009-05-22 14:51:39 -0400877 pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
878
879 /*
James Smart8a9d2e82012-05-09 21:16:12 -0400880 * The first two bdes are the FCP_CMD and FCP_RSP.
881 * The balance are sg list bdes. Initialize the
882 * first two and leave the rest for queuecommand.
James Smartda0436e2009-05-22 14:51:39 -0400883 */
884 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
885 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
James Smart05580562011-05-24 11:40:48 -0400886 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -0400887 bf_set(lpfc_sli4_sge_last, sgl, 0);
888 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -0500889 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_cmnd));
James Smartda0436e2009-05-22 14:51:39 -0400890 sgl++;
891
892 /* Setup the physical region for the FCP RSP */
893 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
894 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
James Smart05580562011-05-24 11:40:48 -0400895 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -0400896 bf_set(lpfc_sli4_sge_last, sgl, 1);
897 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -0500898 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
James Smartda0436e2009-05-22 14:51:39 -0400899
900 /*
901 * Since the IOCB for the FCP I/O is built into this
902 * lpfc_scsi_buf, initialize it with all known data now.
903 */
904 iocb = &psb->cur_iocbq.iocb;
905 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
906 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
907 /* setting the BLP size to 2 * sizeof BDE may not be correct.
908 * We are setting the bpl to point to out sgl. An sgl's
909 * entries are 16 bytes, a bpl entries are 12 bytes.
910 */
911 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
912 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
913 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
914 iocb->ulpBdeCount = 1;
915 iocb->ulpLe = 1;
916 iocb->ulpClass = CLASS3;
James Smart8a9d2e82012-05-09 21:16:12 -0400917 psb->cur_iocbq.context1 = psb;
James Smartda0436e2009-05-22 14:51:39 -0400918 psb->dma_phys_bpl = pdma_phys_bpl;
James Smart6d368e52011-05-24 11:44:12 -0400919
James Smart8a9d2e82012-05-09 21:16:12 -0400920 /* add the scsi buffer to a post list */
921 list_add_tail(&psb->list, &post_sblist);
James Smarta40fc5f2013-04-17 20:17:40 -0400922 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -0400923 phba->sli4_hba.scsi_xri_cnt++;
James Smarta40fc5f2013-04-17 20:17:40 -0400924 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smartda0436e2009-05-22 14:51:39 -0400925 }
James Smart8a9d2e82012-05-09 21:16:12 -0400926 lpfc_printf_log(phba, KERN_INFO, LOG_BG,
927 "3021 Allocate %d out of %d requested new SCSI "
928 "buffers\n", bcnt, num_to_alloc);
James Smartda0436e2009-05-22 14:51:39 -0400929
James Smart8a9d2e82012-05-09 21:16:12 -0400930 /* post the list of scsi buffer sgls to port if available */
931 if (!list_empty(&post_sblist))
932 num_posted = lpfc_sli4_post_scsi_sgl_list(phba,
933 &post_sblist, bcnt);
934 else
935 num_posted = 0;
936
937 return num_posted;
James Smartda0436e2009-05-22 14:51:39 -0400938}
939
940/**
James Smart3772a992009-05-22 14:50:54 -0400941 * lpfc_new_scsi_buf - Wrapper funciton for scsi buffer allocator
942 * @vport: The virtual port for which this call being executed.
943 * @num_to_allocate: The requested number of buffers to allocate.
944 *
945 * This routine wraps the actual SCSI buffer allocator function pointer from
946 * the lpfc_hba struct.
947 *
948 * Return codes:
949 * int - number of scsi buffers that were allocated.
950 * 0 = failure, less than num_to_alloc is a partial failure.
951 **/
952static inline int
953lpfc_new_scsi_buf(struct lpfc_vport *vport, int num_to_alloc)
954{
955 return vport->phba->lpfc_new_scsi_buf(vport, num_to_alloc);
956}
957
958/**
James Smart19ca7602010-11-20 23:11:55 -0500959 * lpfc_get_scsi_buf_s3 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
James Smart3772a992009-05-22 14:50:54 -0400960 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -0500961 *
962 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
963 * and returns to caller.
964 *
965 * Return codes:
966 * NULL - Error
967 * Pointer to lpfc_scsi_buf - Success
968 **/
Adrian Bunk455c53e2006-01-06 20:21:28 +0100969static struct lpfc_scsi_buf*
James Smart19ca7602010-11-20 23:11:55 -0500970lpfc_get_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -0500971{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400972 struct lpfc_scsi_buf * lpfc_cmd = NULL;
James Smarta40fc5f2013-04-17 20:17:40 -0400973 struct list_head *scsi_buf_list_get = &phba->lpfc_scsi_buf_list_get;
James Smart164cecd2013-09-06 12:22:38 -0400974 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500975
James Smart164cecd2013-09-06 12:22:38 -0400976 spin_lock_irqsave(&phba->scsi_buf_list_get_lock, iflag);
James Smarta40fc5f2013-04-17 20:17:40 -0400977 list_remove_head(scsi_buf_list_get, lpfc_cmd, struct lpfc_scsi_buf,
978 list);
979 if (!lpfc_cmd) {
James Smart164cecd2013-09-06 12:22:38 -0400980 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -0400981 list_splice(&phba->lpfc_scsi_buf_list_put,
982 &phba->lpfc_scsi_buf_list_get);
983 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
984 list_remove_head(scsi_buf_list_get, lpfc_cmd,
985 struct lpfc_scsi_buf, list);
James Smart164cecd2013-09-06 12:22:38 -0400986 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smart1dcb58e2007-04-25 09:51:30 -0400987 }
James Smart164cecd2013-09-06 12:22:38 -0400988 spin_unlock_irqrestore(&phba->scsi_buf_list_get_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400989 return lpfc_cmd;
990}
James Smart19ca7602010-11-20 23:11:55 -0500991/**
992 * lpfc_get_scsi_buf_s4 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
993 * @phba: The HBA for which this call is being executed.
994 *
995 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
996 * and returns to caller.
997 *
998 * Return codes:
999 * NULL - Error
1000 * Pointer to lpfc_scsi_buf - Success
1001 **/
1002static struct lpfc_scsi_buf*
1003lpfc_get_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1004{
James Smart3be30e02013-05-31 17:05:17 -04001005 struct lpfc_scsi_buf *lpfc_cmd, *lpfc_cmd_next;
James Smart164cecd2013-09-06 12:22:38 -04001006 unsigned long iflag = 0;
James Smart19ca7602010-11-20 23:11:55 -05001007 int found = 0;
1008
James Smart164cecd2013-09-06 12:22:38 -04001009 spin_lock_irqsave(&phba->scsi_buf_list_get_lock, iflag);
James Smart3be30e02013-05-31 17:05:17 -04001010 list_for_each_entry_safe(lpfc_cmd, lpfc_cmd_next,
1011 &phba->lpfc_scsi_buf_list_get, list) {
James Smart19ca7602010-11-20 23:11:55 -05001012 if (lpfc_test_rrq_active(phba, ndlp,
James Smartee0f4fe2012-05-09 21:19:14 -04001013 lpfc_cmd->cur_iocbq.sli4_lxritag))
James Smart1151e3e2011-02-16 12:39:35 -05001014 continue;
1015 list_del(&lpfc_cmd->list);
James Smart19ca7602010-11-20 23:11:55 -05001016 found = 1;
James Smart1151e3e2011-02-16 12:39:35 -05001017 break;
James Smart19ca7602010-11-20 23:11:55 -05001018 }
James Smarta40fc5f2013-04-17 20:17:40 -04001019 if (!found) {
James Smart164cecd2013-09-06 12:22:38 -04001020 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04001021 list_splice(&phba->lpfc_scsi_buf_list_put,
1022 &phba->lpfc_scsi_buf_list_get);
1023 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
James Smart164cecd2013-09-06 12:22:38 -04001024 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smart3be30e02013-05-31 17:05:17 -04001025 list_for_each_entry_safe(lpfc_cmd, lpfc_cmd_next,
1026 &phba->lpfc_scsi_buf_list_get, list) {
James Smarta40fc5f2013-04-17 20:17:40 -04001027 if (lpfc_test_rrq_active(
1028 phba, ndlp, lpfc_cmd->cur_iocbq.sli4_lxritag))
1029 continue;
1030 list_del(&lpfc_cmd->list);
1031 found = 1;
1032 break;
1033 }
1034 }
James Smart164cecd2013-09-06 12:22:38 -04001035 spin_unlock_irqrestore(&phba->scsi_buf_list_get_lock, iflag);
James Smart1151e3e2011-02-16 12:39:35 -05001036 if (!found)
1037 return NULL;
James Smarta40fc5f2013-04-17 20:17:40 -04001038 return lpfc_cmd;
James Smart19ca7602010-11-20 23:11:55 -05001039}
1040/**
1041 * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1042 * @phba: The HBA for which this call is being executed.
1043 *
1044 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1045 * and returns to caller.
1046 *
1047 * Return codes:
1048 * NULL - Error
1049 * Pointer to lpfc_scsi_buf - Success
1050 **/
1051static struct lpfc_scsi_buf*
1052lpfc_get_scsi_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1053{
1054 return phba->lpfc_get_scsi_buf(phba, ndlp);
1055}
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001056
James Smart9bad7672008-12-04 22:39:02 -05001057/**
James Smart3772a992009-05-22 14:50:54 -04001058 * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
James Smart9bad7672008-12-04 22:39:02 -05001059 * @phba: The Hba for which this call is being executed.
1060 * @psb: The scsi buffer which is being released.
1061 *
1062 * This routine releases @psb scsi buffer by adding it to tail of @phba
1063 * lpfc_scsi_buf_list list.
1064 **/
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001065static void
James Smart3772a992009-05-22 14:50:54 -04001066lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001067{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001068 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -05001069
James Smarta40fc5f2013-04-17 20:17:40 -04001070 psb->seg_cnt = 0;
1071 psb->nonsg_phys = 0;
1072 psb->prot_seg_cnt = 0;
1073
1074 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001075 psb->pCmd = NULL;
James Smart6a485eb2013-04-17 20:19:00 -04001076 psb->cur_iocbq.iocb_flag = LPFC_IO_FCP;
James Smarta40fc5f2013-04-17 20:17:40 -04001077 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list_put);
1078 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
dea31012005-04-17 16:05:31 -05001079}
1080
James Smart9bad7672008-12-04 22:39:02 -05001081/**
James Smartda0436e2009-05-22 14:51:39 -04001082 * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
1083 * @phba: The Hba for which this call is being executed.
1084 * @psb: The scsi buffer which is being released.
1085 *
1086 * This routine releases @psb scsi buffer by adding it to tail of @phba
1087 * lpfc_scsi_buf_list list. For SLI4 XRI's are tied to the scsi buffer
1088 * and cannot be reused for at least RA_TOV amount of time if it was
1089 * aborted.
1090 **/
1091static void
1092lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1093{
1094 unsigned long iflag = 0;
1095
James Smarta40fc5f2013-04-17 20:17:40 -04001096 psb->seg_cnt = 0;
1097 psb->nonsg_phys = 0;
1098 psb->prot_seg_cnt = 0;
1099
James Smart341af102010-01-26 23:07:37 -05001100 if (psb->exch_busy) {
James Smartda0436e2009-05-22 14:51:39 -04001101 spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock,
1102 iflag);
1103 psb->pCmd = NULL;
1104 list_add_tail(&psb->list,
1105 &phba->sli4_hba.lpfc_abts_scsi_buf_list);
1106 spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
1107 iflag);
1108 } else {
James Smartda0436e2009-05-22 14:51:39 -04001109 psb->pCmd = NULL;
James Smart6a485eb2013-04-17 20:19:00 -04001110 psb->cur_iocbq.iocb_flag = LPFC_IO_FCP;
James Smarta40fc5f2013-04-17 20:17:40 -04001111 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
1112 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list_put);
1113 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
James Smartda0436e2009-05-22 14:51:39 -04001114 }
1115}
1116
1117/**
James Smart3772a992009-05-22 14:50:54 -04001118 * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
1119 * @phba: The Hba for which this call is being executed.
1120 * @psb: The scsi buffer which is being released.
1121 *
1122 * This routine releases @psb scsi buffer by adding it to tail of @phba
1123 * lpfc_scsi_buf_list list.
1124 **/
1125static void
1126lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1127{
1128
1129 phba->lpfc_release_scsi_buf(phba, psb);
1130}
1131
1132/**
1133 * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -05001134 * @phba: The Hba for which this call is being executed.
1135 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1136 *
1137 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
James Smart3772a992009-05-22 14:50:54 -04001138 * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
1139 * through sg elements and format the bdea. This routine also initializes all
1140 * IOCB fields which are dependent on scsi command request buffer.
James Smart9bad7672008-12-04 22:39:02 -05001141 *
1142 * Return codes:
1143 * 1 - Error
1144 * 0 - Success
1145 **/
dea31012005-04-17 16:05:31 -05001146static int
James Smart3772a992009-05-22 14:50:54 -04001147lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
dea31012005-04-17 16:05:31 -05001148{
1149 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1150 struct scatterlist *sgel = NULL;
1151 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1152 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
James Smart0f65ff62010-02-26 14:14:23 -05001153 struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
dea31012005-04-17 16:05:31 -05001154 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart34b02dc2008-08-24 21:49:55 -04001155 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
dea31012005-04-17 16:05:31 -05001156 dma_addr_t physaddr;
James Smart34b02dc2008-08-24 21:49:55 -04001157 uint32_t num_bde = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001158 int nseg, datadir = scsi_cmnd->sc_data_direction;
dea31012005-04-17 16:05:31 -05001159
1160 /*
1161 * There are three possibilities here - use scatter-gather segment, use
1162 * the single mapping, or neither. Start the lpfc command prep by
1163 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1164 * data bde entry.
1165 */
1166 bpl += 2;
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001167 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05001168 /*
1169 * The driver stores the segment count returned from pci_map_sg
1170 * because this a count of dma-mappings used to map the use_sg
1171 * pages. They are not guaranteed to be the same for those
1172 * architectures that implement an IOMMU.
1173 */
dea31012005-04-17 16:05:31 -05001174
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001175 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
1176 scsi_sg_count(scsi_cmnd), datadir);
1177 if (unlikely(!nseg))
1178 return 1;
1179
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001180 lpfc_cmd->seg_cnt = nseg;
dea31012005-04-17 16:05:31 -05001181 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001182 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1183 "9064 BLKGRD: %s: Too many sg segments from "
James Smarte2a0a9d2008-12-04 22:40:02 -05001184 "dma_map_sg. Config %d, seg_cnt %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001185 __func__, phba->cfg_sg_seg_cnt,
dea31012005-04-17 16:05:31 -05001186 lpfc_cmd->seg_cnt);
James Smart96f70772013-04-17 20:16:15 -04001187 lpfc_cmd->seg_cnt = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001188 scsi_dma_unmap(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001189 return 1;
1190 }
1191
1192 /*
1193 * The driver established a maximum scatter-gather segment count
1194 * during probe that limits the number of sg elements in any
1195 * single scsi command. Just run through the seg_cnt and format
1196 * the bde's.
James Smart34b02dc2008-08-24 21:49:55 -04001197 * When using SLI-3 the driver will try to fit all the BDEs into
1198 * the IOCB. If it can't then the BDEs get added to a BPL as it
1199 * does for SLI-2 mode.
dea31012005-04-17 16:05:31 -05001200 */
James Smart34b02dc2008-08-24 21:49:55 -04001201 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
dea31012005-04-17 16:05:31 -05001202 physaddr = sg_dma_address(sgel);
James Smart34b02dc2008-08-24 21:49:55 -04001203 if (phba->sli_rev == 3 &&
James Smarte2a0a9d2008-12-04 22:40:02 -05001204 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
James Smart0f65ff62010-02-26 14:14:23 -05001205 !(iocbq->iocb_flag & DSS_SECURITY_OP) &&
James Smart34b02dc2008-08-24 21:49:55 -04001206 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
1207 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1208 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
1209 data_bde->addrLow = putPaddrLow(physaddr);
1210 data_bde->addrHigh = putPaddrHigh(physaddr);
1211 data_bde++;
1212 } else {
1213 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1214 bpl->tus.f.bdeSize = sg_dma_len(sgel);
1215 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1216 bpl->addrLow =
1217 le32_to_cpu(putPaddrLow(physaddr));
1218 bpl->addrHigh =
1219 le32_to_cpu(putPaddrHigh(physaddr));
1220 bpl++;
1221 }
dea31012005-04-17 16:05:31 -05001222 }
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001223 }
dea31012005-04-17 16:05:31 -05001224
1225 /*
1226 * Finish initializing those IOCB fields that are dependent on the
James Smart34b02dc2008-08-24 21:49:55 -04001227 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
1228 * explicitly reinitialized and for SLI-3 the extended bde count is
1229 * explicitly reinitialized since all iocb memory resources are reused.
dea31012005-04-17 16:05:31 -05001230 */
James Smarte2a0a9d2008-12-04 22:40:02 -05001231 if (phba->sli_rev == 3 &&
James Smart0f65ff62010-02-26 14:14:23 -05001232 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
1233 !(iocbq->iocb_flag & DSS_SECURITY_OP)) {
James Smart34b02dc2008-08-24 21:49:55 -04001234 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
1235 /*
1236 * The extended IOCB format can only fit 3 BDE or a BPL.
1237 * This I/O has more than 3 BDE so the 1st data bde will
1238 * be a BPL that is filled in here.
1239 */
1240 physaddr = lpfc_cmd->dma_handle;
1241 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
1242 data_bde->tus.f.bdeSize = (num_bde *
1243 sizeof(struct ulp_bde64));
1244 physaddr += (sizeof(struct fcp_cmnd) +
1245 sizeof(struct fcp_rsp) +
1246 (2 * sizeof(struct ulp_bde64)));
1247 data_bde->addrHigh = putPaddrHigh(physaddr);
1248 data_bde->addrLow = putPaddrLow(physaddr);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001249 /* ebde count includes the response bde and data bpl */
James Smart34b02dc2008-08-24 21:49:55 -04001250 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
1251 } else {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001252 /* ebde count includes the response bde and data bdes */
James Smart34b02dc2008-08-24 21:49:55 -04001253 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1254 }
1255 } else {
1256 iocb_cmd->un.fcpi64.bdl.bdeSize =
1257 ((num_bde + 2) * sizeof(struct ulp_bde64));
James Smart0f65ff62010-02-26 14:14:23 -05001258 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
James Smart34b02dc2008-08-24 21:49:55 -04001259 }
James Smart09372822008-01-11 01:52:54 -05001260 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
James Smarte2a0a9d2008-12-04 22:40:02 -05001261
1262 /*
1263 * Due to difference in data length between DIF/non-DIF paths,
1264 * we need to set word 4 of IOCB here
1265 */
James Smarta257bf92009-04-06 18:48:10 -04001266 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001267 return 0;
1268}
1269
James Smartf9bb2da2011-10-10 21:34:11 -04001270#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001271
James Smart9a6b09c2012-03-01 22:37:42 -05001272/* Return if if error injection is detected by Initiator */
1273#define BG_ERR_INIT 0x1
1274/* Return if if error injection is detected by Target */
1275#define BG_ERR_TGT 0x2
1276/* Return if if swapping CSUM<-->CRC is required for error injection */
1277#define BG_ERR_SWAP 0x10
1278/* Return if disabling Guard/Ref/App checking is required for error injection */
1279#define BG_ERR_CHECK 0x20
James Smartacd68592012-01-18 16:25:09 -05001280
1281/**
1282 * lpfc_bg_err_inject - Determine if we should inject an error
1283 * @phba: The Hba for which this call is being executed.
James Smartf9bb2da2011-10-10 21:34:11 -04001284 * @sc: The SCSI command to examine
1285 * @reftag: (out) BlockGuard reference tag for transmitted data
1286 * @apptag: (out) BlockGuard application tag for transmitted data
1287 * @new_guard (in) Value to replace CRC with if needed
1288 *
James Smart9a6b09c2012-03-01 22:37:42 -05001289 * Returns BG_ERR_* bit mask or 0 if request ignored
James Smartacd68592012-01-18 16:25:09 -05001290 **/
James Smartf9bb2da2011-10-10 21:34:11 -04001291static int
1292lpfc_bg_err_inject(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1293 uint32_t *reftag, uint16_t *apptag, uint32_t new_guard)
1294{
1295 struct scatterlist *sgpe; /* s/g prot entry */
James Smart9a6b09c2012-03-01 22:37:42 -05001296 struct lpfc_scsi_buf *lpfc_cmd = NULL;
James Smartacd68592012-01-18 16:25:09 -05001297 struct scsi_dif_tuple *src = NULL;
James Smart4ac9b222012-03-01 22:38:29 -05001298 struct lpfc_nodelist *ndlp;
1299 struct lpfc_rport_data *rdata;
James Smartf9bb2da2011-10-10 21:34:11 -04001300 uint32_t op = scsi_get_prot_op(sc);
1301 uint32_t blksize;
1302 uint32_t numblks;
1303 sector_t lba;
1304 int rc = 0;
James Smartacd68592012-01-18 16:25:09 -05001305 int blockoff = 0;
James Smartf9bb2da2011-10-10 21:34:11 -04001306
1307 if (op == SCSI_PROT_NORMAL)
1308 return 0;
1309
James Smartacd68592012-01-18 16:25:09 -05001310 sgpe = scsi_prot_sglist(sc);
James Smartf9bb2da2011-10-10 21:34:11 -04001311 lba = scsi_get_lba(sc);
James Smart4ac9b222012-03-01 22:38:29 -05001312
1313 /* First check if we need to match the LBA */
James Smartf9bb2da2011-10-10 21:34:11 -04001314 if (phba->lpfc_injerr_lba != LPFC_INJERR_LBA_OFF) {
1315 blksize = lpfc_cmd_blksize(sc);
1316 numblks = (scsi_bufflen(sc) + blksize - 1) / blksize;
1317
1318 /* Make sure we have the right LBA if one is specified */
1319 if ((phba->lpfc_injerr_lba < lba) ||
1320 (phba->lpfc_injerr_lba >= (lba + numblks)))
1321 return 0;
James Smartacd68592012-01-18 16:25:09 -05001322 if (sgpe) {
1323 blockoff = phba->lpfc_injerr_lba - lba;
1324 numblks = sg_dma_len(sgpe) /
1325 sizeof(struct scsi_dif_tuple);
1326 if (numblks < blockoff)
1327 blockoff = numblks;
James Smartacd68592012-01-18 16:25:09 -05001328 }
James Smartf9bb2da2011-10-10 21:34:11 -04001329 }
1330
James Smart4ac9b222012-03-01 22:38:29 -05001331 /* Next check if we need to match the remote NPortID or WWPN */
James Smart1ba981f2014-02-20 09:56:45 -05001332 rdata = lpfc_rport_data_from_scsi_device(sc->device);
James Smart4ac9b222012-03-01 22:38:29 -05001333 if (rdata && rdata->pnode) {
1334 ndlp = rdata->pnode;
1335
1336 /* Make sure we have the right NPortID if one is specified */
1337 if (phba->lpfc_injerr_nportid &&
1338 (phba->lpfc_injerr_nportid != ndlp->nlp_DID))
1339 return 0;
1340
1341 /*
1342 * Make sure we have the right WWPN if one is specified.
1343 * wwn[0] should be a non-zero NAA in a good WWPN.
1344 */
1345 if (phba->lpfc_injerr_wwpn.u.wwn[0] &&
1346 (memcmp(&ndlp->nlp_portname, &phba->lpfc_injerr_wwpn,
1347 sizeof(struct lpfc_name)) != 0))
1348 return 0;
1349 }
1350
1351 /* Setup a ptr to the protection data if the SCSI host provides it */
1352 if (sgpe) {
1353 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
1354 src += blockoff;
1355 lpfc_cmd = (struct lpfc_scsi_buf *)sc->host_scribble;
1356 }
1357
James Smartf9bb2da2011-10-10 21:34:11 -04001358 /* Should we change the Reference Tag */
1359 if (reftag) {
James Smartacd68592012-01-18 16:25:09 -05001360 if (phba->lpfc_injerr_wref_cnt) {
1361 switch (op) {
1362 case SCSI_PROT_WRITE_PASS:
James Smart9a6b09c2012-03-01 22:37:42 -05001363 if (src) {
1364 /*
1365 * For WRITE_PASS, force the error
1366 * to be sent on the wire. It should
1367 * be detected by the Target.
1368 * If blockoff != 0 error will be
1369 * inserted in middle of the IO.
1370 */
James Smartf9bb2da2011-10-10 21:34:11 -04001371
James Smartacd68592012-01-18 16:25:09 -05001372 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1373 "9076 BLKGRD: Injecting reftag error: "
1374 "write lba x%lx + x%x oldrefTag x%x\n",
1375 (unsigned long)lba, blockoff,
James Smart9a6b09c2012-03-01 22:37:42 -05001376 be32_to_cpu(src->ref_tag));
James Smartacd68592012-01-18 16:25:09 -05001377
1378 /*
James Smart9a6b09c2012-03-01 22:37:42 -05001379 * Save the old ref_tag so we can
1380 * restore it on completion.
James Smartacd68592012-01-18 16:25:09 -05001381 */
James Smart9a6b09c2012-03-01 22:37:42 -05001382 if (lpfc_cmd) {
1383 lpfc_cmd->prot_data_type =
1384 LPFC_INJERR_REFTAG;
1385 lpfc_cmd->prot_data_segment =
1386 src;
1387 lpfc_cmd->prot_data =
1388 src->ref_tag;
1389 }
1390 src->ref_tag = cpu_to_be32(0xDEADBEEF);
James Smartacd68592012-01-18 16:25:09 -05001391 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001392 if (phba->lpfc_injerr_wref_cnt == 0) {
1393 phba->lpfc_injerr_nportid = 0;
1394 phba->lpfc_injerr_lba =
1395 LPFC_INJERR_LBA_OFF;
1396 memset(&phba->lpfc_injerr_wwpn,
1397 0, sizeof(struct lpfc_name));
1398 }
James Smart9a6b09c2012-03-01 22:37:42 -05001399 rc = BG_ERR_TGT | BG_ERR_CHECK;
1400
James Smartacd68592012-01-18 16:25:09 -05001401 break;
1402 }
1403 /* Drop thru */
James Smart9a6b09c2012-03-01 22:37:42 -05001404 case SCSI_PROT_WRITE_INSERT:
1405 /*
1406 * For WRITE_INSERT, force the error
1407 * to be sent on the wire. It should be
1408 * detected by the Target.
1409 */
1410 /* DEADBEEF will be the reftag on the wire */
1411 *reftag = 0xDEADBEEF;
1412 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001413 if (phba->lpfc_injerr_wref_cnt == 0) {
1414 phba->lpfc_injerr_nportid = 0;
1415 phba->lpfc_injerr_lba =
1416 LPFC_INJERR_LBA_OFF;
1417 memset(&phba->lpfc_injerr_wwpn,
1418 0, sizeof(struct lpfc_name));
1419 }
James Smart9a6b09c2012-03-01 22:37:42 -05001420 rc = BG_ERR_TGT | BG_ERR_CHECK;
1421
1422 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1423 "9078 BLKGRD: Injecting reftag error: "
1424 "write lba x%lx\n", (unsigned long)lba);
1425 break;
James Smartacd68592012-01-18 16:25:09 -05001426 case SCSI_PROT_WRITE_STRIP:
1427 /*
1428 * For WRITE_STRIP and WRITE_PASS,
1429 * force the error on data
1430 * being copied from SLI-Host to SLI-Port.
1431 */
1432 *reftag = 0xDEADBEEF;
1433 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001434 if (phba->lpfc_injerr_wref_cnt == 0) {
1435 phba->lpfc_injerr_nportid = 0;
1436 phba->lpfc_injerr_lba =
1437 LPFC_INJERR_LBA_OFF;
1438 memset(&phba->lpfc_injerr_wwpn,
1439 0, sizeof(struct lpfc_name));
1440 }
James Smartacd68592012-01-18 16:25:09 -05001441 rc = BG_ERR_INIT;
1442
1443 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1444 "9077 BLKGRD: Injecting reftag error: "
1445 "write lba x%lx\n", (unsigned long)lba);
1446 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001447 }
James Smartacd68592012-01-18 16:25:09 -05001448 }
1449 if (phba->lpfc_injerr_rref_cnt) {
1450 switch (op) {
1451 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001452 case SCSI_PROT_READ_STRIP:
1453 case SCSI_PROT_READ_PASS:
1454 /*
1455 * For READ_STRIP and READ_PASS, force the
1456 * error on data being read off the wire. It
1457 * should force an IO error to the driver.
1458 */
James Smartf9bb2da2011-10-10 21:34:11 -04001459 *reftag = 0xDEADBEEF;
1460 phba->lpfc_injerr_rref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001461 if (phba->lpfc_injerr_rref_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;
James Smartf9bb2da2011-10-10 21:34:11 -04001469
1470 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smartacd68592012-01-18 16:25:09 -05001471 "9079 BLKGRD: Injecting reftag error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001472 "read lba x%lx\n", (unsigned long)lba);
James Smartacd68592012-01-18 16:25:09 -05001473 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001474 }
1475 }
1476 }
1477
1478 /* Should we change the Application Tag */
1479 if (apptag) {
James Smartacd68592012-01-18 16:25:09 -05001480 if (phba->lpfc_injerr_wapp_cnt) {
1481 switch (op) {
1482 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001483 if (src) {
James Smart9a6b09c2012-03-01 22:37:42 -05001484 /*
1485 * For WRITE_PASS, force the error
1486 * to be sent on the wire. It should
1487 * be detected by the Target.
1488 * If blockoff != 0 error will be
1489 * inserted in middle of the IO.
1490 */
1491
James Smartacd68592012-01-18 16:25:09 -05001492 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1493 "9080 BLKGRD: Injecting apptag error: "
1494 "write lba x%lx + x%x oldappTag x%x\n",
1495 (unsigned long)lba, blockoff,
James Smart9a6b09c2012-03-01 22:37:42 -05001496 be16_to_cpu(src->app_tag));
James Smartacd68592012-01-18 16:25:09 -05001497
1498 /*
James Smart9a6b09c2012-03-01 22:37:42 -05001499 * Save the old app_tag so we can
1500 * restore it on completion.
James Smartacd68592012-01-18 16:25:09 -05001501 */
James Smart9a6b09c2012-03-01 22:37:42 -05001502 if (lpfc_cmd) {
1503 lpfc_cmd->prot_data_type =
1504 LPFC_INJERR_APPTAG;
1505 lpfc_cmd->prot_data_segment =
1506 src;
1507 lpfc_cmd->prot_data =
1508 src->app_tag;
1509 }
1510 src->app_tag = cpu_to_be16(0xDEAD);
James Smartacd68592012-01-18 16:25:09 -05001511 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001512 if (phba->lpfc_injerr_wapp_cnt == 0) {
1513 phba->lpfc_injerr_nportid = 0;
1514 phba->lpfc_injerr_lba =
1515 LPFC_INJERR_LBA_OFF;
1516 memset(&phba->lpfc_injerr_wwpn,
1517 0, sizeof(struct lpfc_name));
1518 }
James Smart9a6b09c2012-03-01 22:37:42 -05001519 rc = BG_ERR_TGT | BG_ERR_CHECK;
James Smartacd68592012-01-18 16:25:09 -05001520 break;
1521 }
1522 /* Drop thru */
James Smart9a6b09c2012-03-01 22:37:42 -05001523 case SCSI_PROT_WRITE_INSERT:
1524 /*
1525 * For WRITE_INSERT, force the
1526 * error to be sent on the wire. It should be
1527 * detected by the Target.
1528 */
1529 /* DEAD will be the apptag on the wire */
1530 *apptag = 0xDEAD;
1531 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001532 if (phba->lpfc_injerr_wapp_cnt == 0) {
1533 phba->lpfc_injerr_nportid = 0;
1534 phba->lpfc_injerr_lba =
1535 LPFC_INJERR_LBA_OFF;
1536 memset(&phba->lpfc_injerr_wwpn,
1537 0, sizeof(struct lpfc_name));
1538 }
James Smart9a6b09c2012-03-01 22:37:42 -05001539 rc = BG_ERR_TGT | BG_ERR_CHECK;
1540
1541 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1542 "0813 BLKGRD: Injecting apptag error: "
1543 "write lba x%lx\n", (unsigned long)lba);
1544 break;
James Smartacd68592012-01-18 16:25:09 -05001545 case SCSI_PROT_WRITE_STRIP:
1546 /*
1547 * For WRITE_STRIP and WRITE_PASS,
1548 * force the error on data
1549 * being copied from SLI-Host to SLI-Port.
1550 */
1551 *apptag = 0xDEAD;
1552 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001553 if (phba->lpfc_injerr_wapp_cnt == 0) {
1554 phba->lpfc_injerr_nportid = 0;
1555 phba->lpfc_injerr_lba =
1556 LPFC_INJERR_LBA_OFF;
1557 memset(&phba->lpfc_injerr_wwpn,
1558 0, sizeof(struct lpfc_name));
1559 }
James Smartacd68592012-01-18 16:25:09 -05001560 rc = BG_ERR_INIT;
1561
1562 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1563 "0812 BLKGRD: Injecting apptag error: "
1564 "write lba x%lx\n", (unsigned long)lba);
1565 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001566 }
James Smartacd68592012-01-18 16:25:09 -05001567 }
1568 if (phba->lpfc_injerr_rapp_cnt) {
1569 switch (op) {
1570 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001571 case SCSI_PROT_READ_STRIP:
1572 case SCSI_PROT_READ_PASS:
1573 /*
1574 * For READ_STRIP and READ_PASS, force the
1575 * error on data being read off the wire. It
1576 * should force an IO error to the driver.
1577 */
James Smartf9bb2da2011-10-10 21:34:11 -04001578 *apptag = 0xDEAD;
1579 phba->lpfc_injerr_rapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001580 if (phba->lpfc_injerr_rapp_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;
James Smartf9bb2da2011-10-10 21:34:11 -04001588
1589 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smartacd68592012-01-18 16:25:09 -05001590 "0814 BLKGRD: Injecting apptag error: "
1591 "read lba x%lx\n", (unsigned long)lba);
1592 break;
1593 }
1594 }
1595 }
1596
1597
1598 /* Should we change the Guard Tag */
1599 if (new_guard) {
1600 if (phba->lpfc_injerr_wgrd_cnt) {
1601 switch (op) {
1602 case SCSI_PROT_WRITE_PASS:
James Smart9a6b09c2012-03-01 22:37:42 -05001603 rc = BG_ERR_CHECK;
James Smartacd68592012-01-18 16:25:09 -05001604 /* Drop thru */
James Smartacd68592012-01-18 16:25:09 -05001605
James Smartacd68592012-01-18 16:25:09 -05001606 case SCSI_PROT_WRITE_INSERT:
1607 /*
1608 * For WRITE_INSERT, force the
1609 * error to be sent on the wire. It should be
1610 * detected by the Target.
1611 */
1612 phba->lpfc_injerr_wgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001613 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1614 phba->lpfc_injerr_nportid = 0;
1615 phba->lpfc_injerr_lba =
1616 LPFC_INJERR_LBA_OFF;
1617 memset(&phba->lpfc_injerr_wwpn,
1618 0, sizeof(struct lpfc_name));
1619 }
James Smartacd68592012-01-18 16:25:09 -05001620
James Smart9a6b09c2012-03-01 22:37:42 -05001621 rc |= BG_ERR_TGT | BG_ERR_SWAP;
James Smartacd68592012-01-18 16:25:09 -05001622 /* Signals the caller to swap CRC->CSUM */
1623
1624 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1625 "0817 BLKGRD: Injecting guard error: "
1626 "write lba x%lx\n", (unsigned long)lba);
1627 break;
James Smart9a6b09c2012-03-01 22:37:42 -05001628 case SCSI_PROT_WRITE_STRIP:
1629 /*
1630 * For WRITE_STRIP and WRITE_PASS,
1631 * force the error on data
1632 * being copied from SLI-Host to SLI-Port.
1633 */
1634 phba->lpfc_injerr_wgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001635 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1636 phba->lpfc_injerr_nportid = 0;
1637 phba->lpfc_injerr_lba =
1638 LPFC_INJERR_LBA_OFF;
1639 memset(&phba->lpfc_injerr_wwpn,
1640 0, sizeof(struct lpfc_name));
1641 }
James Smart9a6b09c2012-03-01 22:37:42 -05001642
1643 rc = BG_ERR_INIT | BG_ERR_SWAP;
1644 /* Signals the caller to swap CRC->CSUM */
1645
1646 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1647 "0816 BLKGRD: Injecting guard error: "
1648 "write lba x%lx\n", (unsigned long)lba);
1649 break;
James Smartacd68592012-01-18 16:25:09 -05001650 }
1651 }
1652 if (phba->lpfc_injerr_rgrd_cnt) {
1653 switch (op) {
1654 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001655 case SCSI_PROT_READ_STRIP:
1656 case SCSI_PROT_READ_PASS:
1657 /*
1658 * For READ_STRIP and READ_PASS, force the
1659 * error on data being read off the wire. It
1660 * should force an IO error to the driver.
1661 */
James Smartacd68592012-01-18 16:25:09 -05001662 phba->lpfc_injerr_rgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001663 if (phba->lpfc_injerr_rgrd_cnt == 0) {
1664 phba->lpfc_injerr_nportid = 0;
1665 phba->lpfc_injerr_lba =
1666 LPFC_INJERR_LBA_OFF;
1667 memset(&phba->lpfc_injerr_wwpn,
1668 0, sizeof(struct lpfc_name));
1669 }
James Smartacd68592012-01-18 16:25:09 -05001670
James Smart9a6b09c2012-03-01 22:37:42 -05001671 rc = BG_ERR_INIT | BG_ERR_SWAP;
James Smartacd68592012-01-18 16:25:09 -05001672 /* Signals the caller to swap CRC->CSUM */
1673
1674 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1675 "0818 BLKGRD: Injecting guard error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001676 "read lba x%lx\n", (unsigned long)lba);
1677 }
1678 }
1679 }
1680
James Smartf9bb2da2011-10-10 21:34:11 -04001681 return rc;
1682}
1683#endif
1684
James Smartacd68592012-01-18 16:25:09 -05001685/**
1686 * lpfc_sc_to_bg_opcodes - Determine the BlockGuard opcodes to be used with
1687 * the specified SCSI command.
1688 * @phba: The Hba for which this call is being executed.
James Smart6c8eea52010-04-06 14:49:53 -04001689 * @sc: The SCSI command to examine
1690 * @txopt: (out) BlockGuard operation for transmitted data
1691 * @rxopt: (out) BlockGuard operation for received data
1692 *
1693 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1694 *
James Smartacd68592012-01-18 16:25:09 -05001695 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001696static int
James Smart6c8eea52010-04-06 14:49:53 -04001697lpfc_sc_to_bg_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1698 uint8_t *txop, uint8_t *rxop)
James Smarte2a0a9d2008-12-04 22:40:02 -05001699{
James Smart6c8eea52010-04-06 14:49:53 -04001700 uint8_t ret = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05001701
James Smart9c6aa9d2013-05-31 17:03:39 -04001702 if (lpfc_cmd_guard_csum(sc)) {
James Smarte2a0a9d2008-12-04 22:40:02 -05001703 switch (scsi_get_prot_op(sc)) {
1704 case SCSI_PROT_READ_INSERT:
1705 case SCSI_PROT_WRITE_STRIP:
James Smart6c8eea52010-04-06 14:49:53 -04001706 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001707 *txop = BG_OP_IN_CSUM_OUT_NODIF;
James Smarte2a0a9d2008-12-04 22:40:02 -05001708 break;
1709
1710 case SCSI_PROT_READ_STRIP:
1711 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001712 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001713 *txop = BG_OP_IN_NODIF_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001714 break;
1715
Martin K. Petersenc6af4042009-09-18 17:32:59 -04001716 case SCSI_PROT_READ_PASS:
1717 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001718 *rxop = BG_OP_IN_CRC_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001719 *txop = BG_OP_IN_CSUM_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001720 break;
1721
James Smarte2a0a9d2008-12-04 22:40:02 -05001722 case SCSI_PROT_NORMAL:
1723 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001724 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001725 "9063 BLKGRD: Bad op/guard:%d/IP combination\n",
1726 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001727 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001728 break;
1729
1730 }
James Smart7c56b9f2011-07-22 18:36:25 -04001731 } else {
James Smarte2a0a9d2008-12-04 22:40:02 -05001732 switch (scsi_get_prot_op(sc)) {
1733 case SCSI_PROT_READ_STRIP:
1734 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001735 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001736 *txop = BG_OP_IN_NODIF_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001737 break;
1738
1739 case SCSI_PROT_READ_PASS:
1740 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001741 *rxop = BG_OP_IN_CRC_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001742 *txop = BG_OP_IN_CRC_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001743 break;
1744
James Smarte2a0a9d2008-12-04 22:40:02 -05001745 case SCSI_PROT_READ_INSERT:
1746 case SCSI_PROT_WRITE_STRIP:
James Smart7c56b9f2011-07-22 18:36:25 -04001747 *rxop = BG_OP_IN_NODIF_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001748 *txop = BG_OP_IN_CRC_OUT_NODIF;
James Smart7c56b9f2011-07-22 18:36:25 -04001749 break;
1750
James Smarte2a0a9d2008-12-04 22:40:02 -05001751 case SCSI_PROT_NORMAL:
1752 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001753 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001754 "9075 BLKGRD: Bad op/guard:%d/CRC combination\n",
1755 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001756 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001757 break;
1758 }
James Smarte2a0a9d2008-12-04 22:40:02 -05001759 }
1760
James Smart6c8eea52010-04-06 14:49:53 -04001761 return ret;
James Smarte2a0a9d2008-12-04 22:40:02 -05001762}
1763
James Smartacd68592012-01-18 16:25:09 -05001764#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1765/**
1766 * lpfc_bg_err_opcodes - reDetermine the BlockGuard opcodes to be used with
1767 * the specified SCSI command in order to force a guard tag error.
1768 * @phba: The Hba for which this call is being executed.
1769 * @sc: The SCSI command to examine
1770 * @txopt: (out) BlockGuard operation for transmitted data
1771 * @rxopt: (out) BlockGuard operation for received data
1772 *
1773 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1774 *
1775 **/
1776static int
1777lpfc_bg_err_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1778 uint8_t *txop, uint8_t *rxop)
1779{
James Smartacd68592012-01-18 16:25:09 -05001780 uint8_t ret = 0;
1781
James Smart9c6aa9d2013-05-31 17:03:39 -04001782 if (lpfc_cmd_guard_csum(sc)) {
James Smartacd68592012-01-18 16:25:09 -05001783 switch (scsi_get_prot_op(sc)) {
1784 case SCSI_PROT_READ_INSERT:
1785 case SCSI_PROT_WRITE_STRIP:
James Smartacd68592012-01-18 16:25:09 -05001786 *rxop = BG_OP_IN_NODIF_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001787 *txop = BG_OP_IN_CRC_OUT_NODIF;
James Smartacd68592012-01-18 16:25:09 -05001788 break;
1789
1790 case SCSI_PROT_READ_STRIP:
1791 case SCSI_PROT_WRITE_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001792 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001793 *txop = BG_OP_IN_NODIF_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001794 break;
1795
1796 case SCSI_PROT_READ_PASS:
1797 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001798 *rxop = BG_OP_IN_CSUM_OUT_CRC;
James Smart9a6b09c2012-03-01 22:37:42 -05001799 *txop = BG_OP_IN_CRC_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001800 break;
1801
1802 case SCSI_PROT_NORMAL:
1803 default:
1804 break;
1805
1806 }
1807 } else {
1808 switch (scsi_get_prot_op(sc)) {
1809 case SCSI_PROT_READ_STRIP:
1810 case SCSI_PROT_WRITE_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001811 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001812 *txop = BG_OP_IN_NODIF_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001813 break;
1814
1815 case SCSI_PROT_READ_PASS:
1816 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001817 *rxop = BG_OP_IN_CSUM_OUT_CSUM;
James Smart9a6b09c2012-03-01 22:37:42 -05001818 *txop = BG_OP_IN_CSUM_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001819 break;
1820
1821 case SCSI_PROT_READ_INSERT:
1822 case SCSI_PROT_WRITE_STRIP:
James Smartacd68592012-01-18 16:25:09 -05001823 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001824 *txop = BG_OP_IN_CSUM_OUT_NODIF;
James Smartacd68592012-01-18 16:25:09 -05001825 break;
1826
1827 case SCSI_PROT_NORMAL:
1828 default:
1829 break;
1830 }
1831 }
1832
1833 return ret;
1834}
1835#endif
1836
1837/**
1838 * lpfc_bg_setup_bpl - Setup BlockGuard BPL with no protection data
1839 * @phba: The Hba for which this call is being executed.
1840 * @sc: pointer to scsi command we're working on
1841 * @bpl: pointer to buffer list for protection groups
1842 * @datacnt: number of segments of data that have been dma mapped
1843 *
1844 * This function sets up BPL buffer list for protection groups of
James Smarte2a0a9d2008-12-04 22:40:02 -05001845 * type LPFC_PG_TYPE_NO_DIF
1846 *
1847 * This is usually used when the HBA is instructed to generate
1848 * DIFs and insert them into data stream (or strip DIF from
1849 * incoming data stream)
1850 *
1851 * The buffer list consists of just one protection group described
1852 * below:
1853 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001854 * start of prot group --> | PDE_5 |
1855 * +-------------------------+
1856 * | PDE_6 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001857 * +-------------------------+
1858 * | Data BDE |
1859 * +-------------------------+
1860 * |more Data BDE's ... (opt)|
1861 * +-------------------------+
1862 *
James Smarte2a0a9d2008-12-04 22:40:02 -05001863 *
1864 * Note: Data s/g buffers have been dma mapped
James Smartacd68592012-01-18 16:25:09 -05001865 *
1866 * Returns the number of BDEs added to the BPL.
1867 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001868static int
1869lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1870 struct ulp_bde64 *bpl, int datasegcnt)
1871{
1872 struct scatterlist *sgde = NULL; /* s/g data entry */
James Smart6c8eea52010-04-06 14:49:53 -04001873 struct lpfc_pde5 *pde5 = NULL;
1874 struct lpfc_pde6 *pde6 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05001875 dma_addr_t physaddr;
James Smart6c8eea52010-04-06 14:49:53 -04001876 int i = 0, num_bde = 0, status;
James Smarte2a0a9d2008-12-04 22:40:02 -05001877 int datadir = sc->sc_data_direction;
James Smart0829a192012-05-09 21:18:12 -04001878#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001879 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04001880#endif
James Smartacd68592012-01-18 16:25:09 -05001881 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001882 uint32_t reftag;
James Smart6c8eea52010-04-06 14:49:53 -04001883 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05001884
James Smart6c8eea52010-04-06 14:49:53 -04001885 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1886 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05001887 goto out;
1888
James Smart6c8eea52010-04-06 14:49:53 -04001889 /* extract some info from the scsi command for pde*/
James Smartacd68592012-01-18 16:25:09 -05001890 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
James Smarte2a0a9d2008-12-04 22:40:02 -05001891
James Smartf9bb2da2011-10-10 21:34:11 -04001892#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05001893 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05001894 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05001895 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05001896 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05001897 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05001898 checking = 0;
1899 }
James Smartf9bb2da2011-10-10 21:34:11 -04001900#endif
1901
James Smart6c8eea52010-04-06 14:49:53 -04001902 /* setup PDE5 with what we have */
1903 pde5 = (struct lpfc_pde5 *) bpl;
1904 memset(pde5, 0, sizeof(struct lpfc_pde5));
1905 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05001906
James Smartbc739052010-08-04 16:11:18 -04001907 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04001908 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04001909 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04001910
James Smart6c8eea52010-04-06 14:49:53 -04001911 /* advance bpl and increment bde count */
1912 num_bde++;
1913 bpl++;
1914 pde6 = (struct lpfc_pde6 *) bpl;
1915
1916 /* setup PDE6 with the rest of the info */
1917 memset(pde6, 0, sizeof(struct lpfc_pde6));
1918 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
1919 bf_set(pde6_optx, pde6, txop);
1920 bf_set(pde6_oprx, pde6, rxop);
James Smarta6887e22013-04-17 20:18:07 -04001921
1922 /*
1923 * We only need to check the data on READs, for WRITEs
1924 * protection data is automatically generated, not checked.
1925 */
James Smart6c8eea52010-04-06 14:49:53 -04001926 if (datadir == DMA_FROM_DEVICE) {
James Smart9c6aa9d2013-05-31 17:03:39 -04001927 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
James Smarta6887e22013-04-17 20:18:07 -04001928 bf_set(pde6_ce, pde6, checking);
1929 else
1930 bf_set(pde6_ce, pde6, 0);
1931
James Smart9c6aa9d2013-05-31 17:03:39 -04001932 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04001933 bf_set(pde6_re, pde6, checking);
1934 else
1935 bf_set(pde6_re, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04001936 }
1937 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04001938 bf_set(pde6_ae, pde6, 0);
1939 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04001940
James Smartbc739052010-08-04 16:11:18 -04001941 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04001942 pde6->word0 = cpu_to_le32(pde6->word0);
1943 pde6->word1 = cpu_to_le32(pde6->word1);
1944 pde6->word2 = cpu_to_le32(pde6->word2);
1945
James Smart6c8eea52010-04-06 14:49:53 -04001946 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05001947 num_bde++;
1948 bpl++;
1949
1950 /* assumption: caller has already run dma_map_sg on command data */
1951 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
1952 physaddr = sg_dma_address(sgde);
1953 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
1954 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1955 bpl->tus.f.bdeSize = sg_dma_len(sgde);
1956 if (datadir == DMA_TO_DEVICE)
1957 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1958 else
1959 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1960 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1961 bpl++;
1962 num_bde++;
1963 }
1964
1965out:
1966 return num_bde;
1967}
1968
James Smartacd68592012-01-18 16:25:09 -05001969/**
1970 * lpfc_bg_setup_bpl_prot - Setup BlockGuard BPL with protection data
1971 * @phba: The Hba for which this call is being executed.
1972 * @sc: pointer to scsi command we're working on
1973 * @bpl: pointer to buffer list for protection groups
1974 * @datacnt: number of segments of data that have been dma mapped
1975 * @protcnt: number of segment of protection data that have been dma mapped
1976 *
1977 * This function sets up BPL buffer list for protection groups of
1978 * type LPFC_PG_TYPE_DIF
James Smarte2a0a9d2008-12-04 22:40:02 -05001979 *
1980 * This is usually used when DIFs are in their own buffers,
1981 * separate from the data. The HBA can then by instructed
1982 * to place the DIFs in the outgoing stream. For read operations,
1983 * The HBA could extract the DIFs and place it in DIF buffers.
1984 *
1985 * The buffer list for this type consists of one or more of the
1986 * protection groups described below:
1987 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001988 * start of first prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001989 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001990 * | PDE_6 |
1991 * +-------------------------+
1992 * | PDE_7 (Prot BDE) |
James Smarte2a0a9d2008-12-04 22:40:02 -05001993 * +-------------------------+
1994 * | Data BDE |
1995 * +-------------------------+
1996 * |more Data BDE's ... (opt)|
1997 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001998 * start of new prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001999 * +-------------------------+
2000 * | ... |
2001 * +-------------------------+
2002 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002003 * Note: It is assumed that both data and protection s/g buffers have been
2004 * mapped for DMA
James Smartacd68592012-01-18 16:25:09 -05002005 *
2006 * Returns the number of BDEs added to the BPL.
2007 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002008static int
2009lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2010 struct ulp_bde64 *bpl, int datacnt, int protcnt)
2011{
2012 struct scatterlist *sgde = NULL; /* s/g data entry */
2013 struct scatterlist *sgpe = NULL; /* s/g prot entry */
James Smart6c8eea52010-04-06 14:49:53 -04002014 struct lpfc_pde5 *pde5 = NULL;
2015 struct lpfc_pde6 *pde6 = NULL;
James Smart7f860592011-03-11 16:05:52 -05002016 struct lpfc_pde7 *pde7 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05002017 dma_addr_t dataphysaddr, protphysaddr;
2018 unsigned short curr_data = 0, curr_prot = 0;
James Smart7f860592011-03-11 16:05:52 -05002019 unsigned int split_offset;
2020 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
James Smarte2a0a9d2008-12-04 22:40:02 -05002021 unsigned int protgrp_blks, protgrp_bytes;
2022 unsigned int remainder, subtotal;
James Smart6c8eea52010-04-06 14:49:53 -04002023 int status;
James Smarte2a0a9d2008-12-04 22:40:02 -05002024 int datadir = sc->sc_data_direction;
2025 unsigned char pgdone = 0, alldone = 0;
2026 unsigned blksize;
James Smart0829a192012-05-09 21:18:12 -04002027#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002028 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002029#endif
James Smartacd68592012-01-18 16:25:09 -05002030 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05002031 uint32_t reftag;
James Smart6c8eea52010-04-06 14:49:53 -04002032 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05002033 int num_bde = 0;
2034
2035 sgpe = scsi_prot_sglist(sc);
2036 sgde = scsi_sglist(sc);
2037
2038 if (!sgpe || !sgde) {
2039 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2040 "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
2041 sgpe, sgde);
2042 return 0;
2043 }
2044
James Smart6c8eea52010-04-06 14:49:53 -04002045 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2046 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05002047 goto out;
2048
James Smart6c8eea52010-04-06 14:49:53 -04002049 /* extract some info from the scsi command */
James Smarte2a0a9d2008-12-04 22:40:02 -05002050 blksize = lpfc_cmd_blksize(sc);
James Smartacd68592012-01-18 16:25:09 -05002051 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
James Smarte2a0a9d2008-12-04 22:40:02 -05002052
James Smartf9bb2da2011-10-10 21:34:11 -04002053#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002054 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002055 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002056 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002057 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002058 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002059 checking = 0;
2060 }
James Smartf9bb2da2011-10-10 21:34:11 -04002061#endif
2062
James Smarte2a0a9d2008-12-04 22:40:02 -05002063 split_offset = 0;
2064 do {
James Smart96f70772013-04-17 20:16:15 -04002065 /* Check to see if we ran out of space */
2066 if (num_bde >= (phba->cfg_total_seg_cnt - 2))
2067 return num_bde + 3;
2068
James Smart6c8eea52010-04-06 14:49:53 -04002069 /* setup PDE5 with what we have */
2070 pde5 = (struct lpfc_pde5 *) bpl;
2071 memset(pde5, 0, sizeof(struct lpfc_pde5));
2072 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05002073
James Smartbc739052010-08-04 16:11:18 -04002074 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04002075 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04002076 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04002077
James Smart6c8eea52010-04-06 14:49:53 -04002078 /* advance bpl and increment bde count */
2079 num_bde++;
2080 bpl++;
2081 pde6 = (struct lpfc_pde6 *) bpl;
James Smarte2a0a9d2008-12-04 22:40:02 -05002082
James Smart6c8eea52010-04-06 14:49:53 -04002083 /* setup PDE6 with the rest of the info */
2084 memset(pde6, 0, sizeof(struct lpfc_pde6));
2085 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
2086 bf_set(pde6_optx, pde6, txop);
2087 bf_set(pde6_oprx, pde6, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002088
James Smart9c6aa9d2013-05-31 17:03:39 -04002089 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
James Smarta6887e22013-04-17 20:18:07 -04002090 bf_set(pde6_ce, pde6, checking);
2091 else
2092 bf_set(pde6_ce, pde6, 0);
2093
James Smart9c6aa9d2013-05-31 17:03:39 -04002094 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04002095 bf_set(pde6_re, pde6, checking);
2096 else
2097 bf_set(pde6_re, pde6, 0);
2098
James Smart6c8eea52010-04-06 14:49:53 -04002099 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04002100 bf_set(pde6_ae, pde6, 0);
2101 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04002102
James Smartbc739052010-08-04 16:11:18 -04002103 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04002104 pde6->word0 = cpu_to_le32(pde6->word0);
2105 pde6->word1 = cpu_to_le32(pde6->word1);
2106 pde6->word2 = cpu_to_le32(pde6->word2);
2107
James Smart6c8eea52010-04-06 14:49:53 -04002108 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05002109 num_bde++;
2110 bpl++;
2111
2112 /* setup the first BDE that points to protection buffer */
James Smart7f860592011-03-11 16:05:52 -05002113 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2114 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
James Smarte2a0a9d2008-12-04 22:40:02 -05002115
James Smarte2a0a9d2008-12-04 22:40:02 -05002116 /* must be integer multiple of the DIF block length */
2117 BUG_ON(protgroup_len % 8);
2118
James Smart7f860592011-03-11 16:05:52 -05002119 pde7 = (struct lpfc_pde7 *) bpl;
2120 memset(pde7, 0, sizeof(struct lpfc_pde7));
2121 bf_set(pde7_type, pde7, LPFC_PDE7_DESCRIPTOR);
2122
James Smart7c56b9f2011-07-22 18:36:25 -04002123 pde7->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
2124 pde7->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
James Smart7f860592011-03-11 16:05:52 -05002125
James Smarte2a0a9d2008-12-04 22:40:02 -05002126 protgrp_blks = protgroup_len / 8;
2127 protgrp_bytes = protgrp_blks * blksize;
2128
James Smart7f860592011-03-11 16:05:52 -05002129 /* check if this pde is crossing the 4K boundary; if so split */
2130 if ((pde7->addrLow & 0xfff) + protgroup_len > 0x1000) {
2131 protgroup_remainder = 0x1000 - (pde7->addrLow & 0xfff);
2132 protgroup_offset += protgroup_remainder;
2133 protgrp_blks = protgroup_remainder / 8;
James Smart7c56b9f2011-07-22 18:36:25 -04002134 protgrp_bytes = protgrp_blks * blksize;
James Smart7f860592011-03-11 16:05:52 -05002135 } else {
2136 protgroup_offset = 0;
2137 curr_prot++;
2138 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002139
James Smarte2a0a9d2008-12-04 22:40:02 -05002140 num_bde++;
2141
2142 /* setup BDE's for data blocks associated with DIF data */
2143 pgdone = 0;
2144 subtotal = 0; /* total bytes processed for current prot grp */
2145 while (!pgdone) {
James Smart96f70772013-04-17 20:16:15 -04002146 /* Check to see if we ran out of space */
2147 if (num_bde >= phba->cfg_total_seg_cnt)
2148 return num_bde + 1;
2149
James Smarte2a0a9d2008-12-04 22:40:02 -05002150 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -04002151 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2152 "9065 BLKGRD:%s Invalid data segment\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002153 __func__);
2154 return 0;
2155 }
2156 bpl++;
2157 dataphysaddr = sg_dma_address(sgde) + split_offset;
2158 bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
2159 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
2160
2161 remainder = sg_dma_len(sgde) - split_offset;
2162
2163 if ((subtotal + remainder) <= protgrp_bytes) {
2164 /* we can use this whole buffer */
2165 bpl->tus.f.bdeSize = remainder;
2166 split_offset = 0;
2167
2168 if ((subtotal + remainder) == protgrp_bytes)
2169 pgdone = 1;
2170 } else {
2171 /* must split this buffer with next prot grp */
2172 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
2173 split_offset += bpl->tus.f.bdeSize;
2174 }
2175
2176 subtotal += bpl->tus.f.bdeSize;
2177
2178 if (datadir == DMA_TO_DEVICE)
2179 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2180 else
2181 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2182 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2183
2184 num_bde++;
2185 curr_data++;
2186
2187 if (split_offset)
2188 break;
2189
2190 /* Move to the next s/g segment if possible */
2191 sgde = sg_next(sgde);
James Smart6c8eea52010-04-06 14:49:53 -04002192
James Smarte2a0a9d2008-12-04 22:40:02 -05002193 }
2194
James Smart7f860592011-03-11 16:05:52 -05002195 if (protgroup_offset) {
2196 /* update the reference tag */
2197 reftag += protgrp_blks;
2198 bpl++;
2199 continue;
2200 }
2201
James Smarte2a0a9d2008-12-04 22:40:02 -05002202 /* are we done ? */
2203 if (curr_prot == protcnt) {
2204 alldone = 1;
2205 } else if (curr_prot < protcnt) {
2206 /* advance to next prot buffer */
2207 sgpe = sg_next(sgpe);
2208 bpl++;
2209
2210 /* update the reference tag */
2211 reftag += protgrp_blks;
2212 } else {
2213 /* if we're here, we have a bug */
James Smart6a9c52c2009-10-02 15:16:51 -04002214 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2215 "9054 BLKGRD: bug in %s\n", __func__);
James Smarte2a0a9d2008-12-04 22:40:02 -05002216 }
2217
2218 } while (!alldone);
James Smarte2a0a9d2008-12-04 22:40:02 -05002219out:
2220
James Smarte2a0a9d2008-12-04 22:40:02 -05002221 return num_bde;
2222}
James Smart7f860592011-03-11 16:05:52 -05002223
James Smartacd68592012-01-18 16:25:09 -05002224/**
2225 * lpfc_bg_setup_sgl - Setup BlockGuard SGL with no protection data
2226 * @phba: The Hba for which this call is being executed.
2227 * @sc: pointer to scsi command we're working on
2228 * @sgl: pointer to buffer list for protection groups
2229 * @datacnt: number of segments of data that have been dma mapped
2230 *
2231 * This function sets up SGL buffer list for protection groups of
2232 * type LPFC_PG_TYPE_NO_DIF
2233 *
2234 * This is usually used when the HBA is instructed to generate
2235 * DIFs and insert them into data stream (or strip DIF from
2236 * incoming data stream)
2237 *
2238 * The buffer list consists of just one protection group described
2239 * below:
2240 * +-------------------------+
2241 * start of prot group --> | DI_SEED |
2242 * +-------------------------+
2243 * | Data SGE |
2244 * +-------------------------+
2245 * |more Data SGE's ... (opt)|
2246 * +-------------------------+
2247 *
2248 *
2249 * Note: Data s/g buffers have been dma mapped
2250 *
2251 * Returns the number of SGEs added to the SGL.
2252 **/
2253static int
2254lpfc_bg_setup_sgl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2255 struct sli4_sge *sgl, int datasegcnt)
2256{
2257 struct scatterlist *sgde = NULL; /* s/g data entry */
2258 struct sli4_sge_diseed *diseed = NULL;
2259 dma_addr_t physaddr;
2260 int i = 0, num_sge = 0, status;
James Smartacd68592012-01-18 16:25:09 -05002261 uint32_t reftag;
James Smartacd68592012-01-18 16:25:09 -05002262 uint8_t txop, rxop;
James Smart0829a192012-05-09 21:18:12 -04002263#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002264 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002265#endif
James Smartacd68592012-01-18 16:25:09 -05002266 uint32_t checking = 1;
2267 uint32_t dma_len;
2268 uint32_t dma_offset = 0;
2269
2270 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2271 if (status)
2272 goto out;
2273
2274 /* extract some info from the scsi command for pde*/
James Smartacd68592012-01-18 16:25:09 -05002275 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2276
2277#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002278 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002279 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002280 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002281 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002282 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002283 checking = 0;
2284 }
2285#endif
2286
2287 /* setup DISEED with what we have */
2288 diseed = (struct sli4_sge_diseed *) sgl;
2289 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2290 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2291
2292 /* Endianness conversion if necessary */
2293 diseed->ref_tag = cpu_to_le32(reftag);
2294 diseed->ref_tag_tran = diseed->ref_tag;
2295
James Smarta6887e22013-04-17 20:18:07 -04002296 /*
2297 * We only need to check the data on READs, for WRITEs
2298 * protection data is automatically generated, not checked.
2299 */
2300 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
James Smart9c6aa9d2013-05-31 17:03:39 -04002301 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
James Smarta6887e22013-04-17 20:18:07 -04002302 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2303 else
2304 bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2305
James Smart9c6aa9d2013-05-31 17:03:39 -04002306 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04002307 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2308 else
2309 bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2310 }
2311
James Smartacd68592012-01-18 16:25:09 -05002312 /* setup DISEED with the rest of the info */
2313 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2314 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002315
James Smartacd68592012-01-18 16:25:09 -05002316 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2317 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2318
2319 /* Endianness conversion if necessary for DISEED */
2320 diseed->word2 = cpu_to_le32(diseed->word2);
2321 diseed->word3 = cpu_to_le32(diseed->word3);
2322
2323 /* advance bpl and increment sge count */
2324 num_sge++;
2325 sgl++;
2326
2327 /* assumption: caller has already run dma_map_sg on command data */
2328 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
2329 physaddr = sg_dma_address(sgde);
2330 dma_len = sg_dma_len(sgde);
2331 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
2332 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
2333 if ((i + 1) == datasegcnt)
2334 bf_set(lpfc_sli4_sge_last, sgl, 1);
2335 else
2336 bf_set(lpfc_sli4_sge_last, sgl, 0);
2337 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2338 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2339
2340 sgl->sge_len = cpu_to_le32(dma_len);
2341 dma_offset += dma_len;
2342
2343 sgl++;
2344 num_sge++;
2345 }
2346
2347out:
2348 return num_sge;
2349}
2350
2351/**
2352 * lpfc_bg_setup_sgl_prot - Setup BlockGuard SGL with protection data
2353 * @phba: The Hba for which this call is being executed.
2354 * @sc: pointer to scsi command we're working on
2355 * @sgl: pointer to buffer list for protection groups
2356 * @datacnt: number of segments of data that have been dma mapped
2357 * @protcnt: number of segment of protection data that have been dma mapped
2358 *
2359 * This function sets up SGL buffer list for protection groups of
2360 * type LPFC_PG_TYPE_DIF
2361 *
2362 * This is usually used when DIFs are in their own buffers,
2363 * separate from the data. The HBA can then by instructed
2364 * to place the DIFs in the outgoing stream. For read operations,
2365 * The HBA could extract the DIFs and place it in DIF buffers.
2366 *
2367 * The buffer list for this type consists of one or more of the
2368 * protection groups described below:
2369 * +-------------------------+
2370 * start of first prot group --> | DISEED |
2371 * +-------------------------+
2372 * | DIF (Prot SGE) |
2373 * +-------------------------+
2374 * | Data SGE |
2375 * +-------------------------+
2376 * |more Data SGE's ... (opt)|
2377 * +-------------------------+
2378 * start of new prot group --> | DISEED |
2379 * +-------------------------+
2380 * | ... |
2381 * +-------------------------+
2382 *
2383 * Note: It is assumed that both data and protection s/g buffers have been
2384 * mapped for DMA
2385 *
2386 * Returns the number of SGEs added to the SGL.
2387 **/
2388static int
2389lpfc_bg_setup_sgl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2390 struct sli4_sge *sgl, int datacnt, int protcnt)
2391{
2392 struct scatterlist *sgde = NULL; /* s/g data entry */
2393 struct scatterlist *sgpe = NULL; /* s/g prot entry */
2394 struct sli4_sge_diseed *diseed = NULL;
2395 dma_addr_t dataphysaddr, protphysaddr;
2396 unsigned short curr_data = 0, curr_prot = 0;
2397 unsigned int split_offset;
2398 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
2399 unsigned int protgrp_blks, protgrp_bytes;
2400 unsigned int remainder, subtotal;
2401 int status;
2402 unsigned char pgdone = 0, alldone = 0;
2403 unsigned blksize;
2404 uint32_t reftag;
2405 uint8_t txop, rxop;
2406 uint32_t dma_len;
James Smart0829a192012-05-09 21:18:12 -04002407#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002408 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002409#endif
James Smartacd68592012-01-18 16:25:09 -05002410 uint32_t checking = 1;
2411 uint32_t dma_offset = 0;
2412 int num_sge = 0;
2413
2414 sgpe = scsi_prot_sglist(sc);
2415 sgde = scsi_sglist(sc);
2416
2417 if (!sgpe || !sgde) {
2418 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2419 "9082 Invalid s/g entry: data=0x%p prot=0x%p\n",
2420 sgpe, sgde);
2421 return 0;
2422 }
2423
2424 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2425 if (status)
2426 goto out;
2427
2428 /* extract some info from the scsi command */
2429 blksize = lpfc_cmd_blksize(sc);
2430 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2431
2432#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002433 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002434 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002435 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002436 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002437 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002438 checking = 0;
2439 }
2440#endif
2441
2442 split_offset = 0;
2443 do {
James Smart96f70772013-04-17 20:16:15 -04002444 /* Check to see if we ran out of space */
2445 if (num_sge >= (phba->cfg_total_seg_cnt - 2))
2446 return num_sge + 3;
2447
James Smartacd68592012-01-18 16:25:09 -05002448 /* setup DISEED with what we have */
2449 diseed = (struct sli4_sge_diseed *) sgl;
2450 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2451 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2452
2453 /* Endianness conversion if necessary */
2454 diseed->ref_tag = cpu_to_le32(reftag);
2455 diseed->ref_tag_tran = diseed->ref_tag;
2456
James Smart9c6aa9d2013-05-31 17:03:39 -04002457 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD)) {
James Smarta6887e22013-04-17 20:18:07 -04002458 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2459
2460 } else {
2461 bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2462 /*
2463 * When in this mode, the hardware will replace
2464 * the guard tag from the host with a
2465 * newly generated good CRC for the wire.
2466 * Switch to raw mode here to avoid this
2467 * behavior. What the host sends gets put on the wire.
2468 */
2469 if (txop == BG_OP_IN_CRC_OUT_CRC) {
2470 txop = BG_OP_RAW_MODE;
2471 rxop = BG_OP_RAW_MODE;
2472 }
2473 }
2474
2475
James Smart9c6aa9d2013-05-31 17:03:39 -04002476 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04002477 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2478 else
2479 bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2480
James Smartacd68592012-01-18 16:25:09 -05002481 /* setup DISEED with the rest of the info */
2482 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2483 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002484
James Smartacd68592012-01-18 16:25:09 -05002485 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2486 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2487
2488 /* Endianness conversion if necessary for DISEED */
2489 diseed->word2 = cpu_to_le32(diseed->word2);
2490 diseed->word3 = cpu_to_le32(diseed->word3);
2491
2492 /* advance sgl and increment bde count */
2493 num_sge++;
2494 sgl++;
2495
2496 /* setup the first BDE that points to protection buffer */
2497 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2498 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
2499
2500 /* must be integer multiple of the DIF block length */
2501 BUG_ON(protgroup_len % 8);
2502
2503 /* Now setup DIF SGE */
2504 sgl->word2 = 0;
2505 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DIF);
2506 sgl->addr_hi = le32_to_cpu(putPaddrHigh(protphysaddr));
2507 sgl->addr_lo = le32_to_cpu(putPaddrLow(protphysaddr));
2508 sgl->word2 = cpu_to_le32(sgl->word2);
2509
2510 protgrp_blks = protgroup_len / 8;
2511 protgrp_bytes = protgrp_blks * blksize;
2512
2513 /* check if DIF SGE is crossing the 4K boundary; if so split */
2514 if ((sgl->addr_lo & 0xfff) + protgroup_len > 0x1000) {
2515 protgroup_remainder = 0x1000 - (sgl->addr_lo & 0xfff);
2516 protgroup_offset += protgroup_remainder;
2517 protgrp_blks = protgroup_remainder / 8;
2518 protgrp_bytes = protgrp_blks * blksize;
2519 } else {
2520 protgroup_offset = 0;
2521 curr_prot++;
2522 }
2523
2524 num_sge++;
2525
2526 /* setup SGE's for data blocks associated with DIF data */
2527 pgdone = 0;
2528 subtotal = 0; /* total bytes processed for current prot grp */
2529 while (!pgdone) {
James Smart96f70772013-04-17 20:16:15 -04002530 /* Check to see if we ran out of space */
2531 if (num_sge >= phba->cfg_total_seg_cnt)
2532 return num_sge + 1;
2533
James Smartacd68592012-01-18 16:25:09 -05002534 if (!sgde) {
2535 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2536 "9086 BLKGRD:%s Invalid data segment\n",
2537 __func__);
2538 return 0;
2539 }
2540 sgl++;
2541 dataphysaddr = sg_dma_address(sgde) + split_offset;
2542
2543 remainder = sg_dma_len(sgde) - split_offset;
2544
2545 if ((subtotal + remainder) <= protgrp_bytes) {
2546 /* we can use this whole buffer */
2547 dma_len = remainder;
2548 split_offset = 0;
2549
2550 if ((subtotal + remainder) == protgrp_bytes)
2551 pgdone = 1;
2552 } else {
2553 /* must split this buffer with next prot grp */
2554 dma_len = protgrp_bytes - subtotal;
2555 split_offset += dma_len;
2556 }
2557
2558 subtotal += dma_len;
2559
2560 sgl->addr_lo = cpu_to_le32(putPaddrLow(dataphysaddr));
2561 sgl->addr_hi = cpu_to_le32(putPaddrHigh(dataphysaddr));
2562 bf_set(lpfc_sli4_sge_last, sgl, 0);
2563 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2564 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2565
2566 sgl->sge_len = cpu_to_le32(dma_len);
2567 dma_offset += dma_len;
2568
2569 num_sge++;
2570 curr_data++;
2571
2572 if (split_offset)
2573 break;
2574
2575 /* Move to the next s/g segment if possible */
2576 sgde = sg_next(sgde);
2577 }
2578
2579 if (protgroup_offset) {
2580 /* update the reference tag */
2581 reftag += protgrp_blks;
2582 sgl++;
2583 continue;
2584 }
2585
2586 /* are we done ? */
2587 if (curr_prot == protcnt) {
2588 bf_set(lpfc_sli4_sge_last, sgl, 1);
2589 alldone = 1;
2590 } else if (curr_prot < protcnt) {
2591 /* advance to next prot buffer */
2592 sgpe = sg_next(sgpe);
2593 sgl++;
2594
2595 /* update the reference tag */
2596 reftag += protgrp_blks;
2597 } else {
2598 /* if we're here, we have a bug */
2599 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2600 "9085 BLKGRD: bug in %s\n", __func__);
2601 }
2602
2603 } while (!alldone);
2604
2605out:
2606
2607 return num_sge;
2608}
2609
2610/**
2611 * lpfc_prot_group_type - Get prtotection group type of SCSI command
2612 * @phba: The Hba for which this call is being executed.
2613 * @sc: pointer to scsi command we're working on
2614 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002615 * Given a SCSI command that supports DIF, determine composition of protection
2616 * groups involved in setting up buffer lists
2617 *
James Smartacd68592012-01-18 16:25:09 -05002618 * Returns: Protection group type (with or without DIF)
2619 *
2620 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002621static int
2622lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
2623{
2624 int ret = LPFC_PG_TYPE_INVALID;
2625 unsigned char op = scsi_get_prot_op(sc);
2626
2627 switch (op) {
2628 case SCSI_PROT_READ_STRIP:
2629 case SCSI_PROT_WRITE_INSERT:
2630 ret = LPFC_PG_TYPE_NO_DIF;
2631 break;
2632 case SCSI_PROT_READ_INSERT:
2633 case SCSI_PROT_WRITE_STRIP:
2634 case SCSI_PROT_READ_PASS:
2635 case SCSI_PROT_WRITE_PASS:
James Smarte2a0a9d2008-12-04 22:40:02 -05002636 ret = LPFC_PG_TYPE_DIF_BUF;
2637 break;
2638 default:
James Smart9c6aa9d2013-05-31 17:03:39 -04002639 if (phba)
2640 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2641 "9021 Unsupported protection op:%d\n",
2642 op);
James Smarte2a0a9d2008-12-04 22:40:02 -05002643 break;
2644 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002645 return ret;
2646}
2647
James Smartacd68592012-01-18 16:25:09 -05002648/**
James Smarta6887e22013-04-17 20:18:07 -04002649 * lpfc_bg_scsi_adjust_dl - Adjust SCSI data length for BlockGuard
2650 * @phba: The Hba for which this call is being executed.
2651 * @lpfc_cmd: The scsi buffer which is going to be adjusted.
2652 *
2653 * Adjust the data length to account for how much data
2654 * is actually on the wire.
2655 *
2656 * returns the adjusted data length
2657 **/
2658static int
2659lpfc_bg_scsi_adjust_dl(struct lpfc_hba *phba,
2660 struct lpfc_scsi_buf *lpfc_cmd)
2661{
2662 struct scsi_cmnd *sc = lpfc_cmd->pCmd;
2663 int fcpdl;
2664
2665 fcpdl = scsi_bufflen(sc);
2666
2667 /* Check if there is protection data on the wire */
2668 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
James Smart9c6aa9d2013-05-31 17:03:39 -04002669 /* Read check for protection data */
James Smarta6887e22013-04-17 20:18:07 -04002670 if (scsi_get_prot_op(sc) == SCSI_PROT_READ_INSERT)
2671 return fcpdl;
2672
2673 } else {
James Smart9c6aa9d2013-05-31 17:03:39 -04002674 /* Write check for protection data */
James Smarta6887e22013-04-17 20:18:07 -04002675 if (scsi_get_prot_op(sc) == SCSI_PROT_WRITE_STRIP)
2676 return fcpdl;
2677 }
2678
2679 /*
2680 * If we are in DIF Type 1 mode every data block has a 8 byte
James Smart9c6aa9d2013-05-31 17:03:39 -04002681 * DIF (trailer) attached to it. Must ajust FCP data length
2682 * to account for the protection data.
James Smarta6887e22013-04-17 20:18:07 -04002683 */
James Smart9c6aa9d2013-05-31 17:03:39 -04002684 fcpdl += (fcpdl / lpfc_cmd_blksize(sc)) * 8;
James Smarta6887e22013-04-17 20:18:07 -04002685
2686 return fcpdl;
2687}
2688
2689/**
James Smartacd68592012-01-18 16:25:09 -05002690 * lpfc_bg_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
2691 * @phba: The Hba for which this call is being executed.
2692 * @lpfc_cmd: The scsi buffer which is going to be prep'ed.
2693 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002694 * This is the protection/DIF aware version of
2695 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
2696 * two functions eventually, but for now, it's here
James Smartacd68592012-01-18 16:25:09 -05002697 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002698static int
James Smartacd68592012-01-18 16:25:09 -05002699lpfc_bg_scsi_prep_dma_buf_s3(struct lpfc_hba *phba,
James Smarte2a0a9d2008-12-04 22:40:02 -05002700 struct lpfc_scsi_buf *lpfc_cmd)
2701{
2702 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2703 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2704 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
2705 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2706 uint32_t num_bde = 0;
2707 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
2708 int prot_group_type = 0;
James Smarta6887e22013-04-17 20:18:07 -04002709 int fcpdl;
James Smarte2a0a9d2008-12-04 22:40:02 -05002710
2711 /*
2712 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
2713 * fcp_rsp regions to the first data bde entry
2714 */
2715 bpl += 2;
2716 if (scsi_sg_count(scsi_cmnd)) {
2717 /*
2718 * The driver stores the segment count returned from pci_map_sg
2719 * because this a count of dma-mappings used to map the use_sg
2720 * pages. They are not guaranteed to be the same for those
2721 * architectures that implement an IOMMU.
2722 */
2723 datasegcnt = dma_map_sg(&phba->pcidev->dev,
2724 scsi_sglist(scsi_cmnd),
2725 scsi_sg_count(scsi_cmnd), datadir);
2726 if (unlikely(!datasegcnt))
2727 return 1;
2728
2729 lpfc_cmd->seg_cnt = datasegcnt;
James Smart96f70772013-04-17 20:16:15 -04002730
2731 /* First check if data segment count from SCSI Layer is good */
2732 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt)
2733 goto err;
James Smarte2a0a9d2008-12-04 22:40:02 -05002734
2735 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
2736
2737 switch (prot_group_type) {
2738 case LPFC_PG_TYPE_NO_DIF:
James Smart96f70772013-04-17 20:16:15 -04002739
2740 /* Here we need to add a PDE5 and PDE6 to the count */
2741 if ((lpfc_cmd->seg_cnt + 2) > phba->cfg_total_seg_cnt)
2742 goto err;
2743
James Smarte2a0a9d2008-12-04 22:40:02 -05002744 num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
2745 datasegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002746 /* we should have 2 or more entries in buffer list */
James Smarte2a0a9d2008-12-04 22:40:02 -05002747 if (num_bde < 2)
2748 goto err;
2749 break;
James Smart96f70772013-04-17 20:16:15 -04002750
2751 case LPFC_PG_TYPE_DIF_BUF:
James Smarte2a0a9d2008-12-04 22:40:02 -05002752 /*
2753 * This type indicates that protection buffers are
2754 * passed to the driver, so that needs to be prepared
2755 * for DMA
2756 */
2757 protsegcnt = dma_map_sg(&phba->pcidev->dev,
2758 scsi_prot_sglist(scsi_cmnd),
2759 scsi_prot_sg_count(scsi_cmnd), datadir);
2760 if (unlikely(!protsegcnt)) {
2761 scsi_dma_unmap(scsi_cmnd);
2762 return 1;
2763 }
2764
2765 lpfc_cmd->prot_seg_cnt = protsegcnt;
James Smart96f70772013-04-17 20:16:15 -04002766
2767 /*
2768 * There is a minimun of 4 BPLs used for every
2769 * protection data segment.
2770 */
2771 if ((lpfc_cmd->prot_seg_cnt * 4) >
2772 (phba->cfg_total_seg_cnt - 2))
2773 goto err;
James Smarte2a0a9d2008-12-04 22:40:02 -05002774
2775 num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
2776 datasegcnt, protsegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002777 /* we should have 3 or more entries in buffer list */
James Smart96f70772013-04-17 20:16:15 -04002778 if ((num_bde < 3) ||
2779 (num_bde > phba->cfg_total_seg_cnt))
James Smarte2a0a9d2008-12-04 22:40:02 -05002780 goto err;
2781 break;
James Smart96f70772013-04-17 20:16:15 -04002782
James Smarte2a0a9d2008-12-04 22:40:02 -05002783 case LPFC_PG_TYPE_INVALID:
2784 default:
James Smart96f70772013-04-17 20:16:15 -04002785 scsi_dma_unmap(scsi_cmnd);
2786 lpfc_cmd->seg_cnt = 0;
2787
James Smarte2a0a9d2008-12-04 22:40:02 -05002788 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2789 "9022 Unexpected protection group %i\n",
2790 prot_group_type);
2791 return 1;
2792 }
2793 }
2794
2795 /*
2796 * Finish initializing those IOCB fields that are dependent on the
2797 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
2798 * reinitialized since all iocb memory resources are used many times
2799 * for transmit, receive, and continuation bpl's.
2800 */
2801 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
2802 iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
2803 iocb_cmd->ulpBdeCount = 1;
2804 iocb_cmd->ulpLe = 1;
2805
James Smarta6887e22013-04-17 20:18:07 -04002806 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002807 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
2808
2809 /*
2810 * Due to difference in data length between DIF/non-DIF paths,
2811 * we need to set word 4 of IOCB here
2812 */
2813 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
2814
2815 return 0;
2816err:
James Smart96f70772013-04-17 20:16:15 -04002817 if (lpfc_cmd->seg_cnt)
2818 scsi_dma_unmap(scsi_cmnd);
2819 if (lpfc_cmd->prot_seg_cnt)
2820 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
2821 scsi_prot_sg_count(scsi_cmnd),
2822 scsi_cmnd->sc_data_direction);
2823
James Smarte2a0a9d2008-12-04 22:40:02 -05002824 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart96f70772013-04-17 20:16:15 -04002825 "9023 Cannot setup S/G List for HBA"
2826 "IO segs %d/%d BPL %d SCSI %d: %d %d\n",
2827 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
2828 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
James Smarte2a0a9d2008-12-04 22:40:02 -05002829 prot_group_type, num_bde);
James Smart96f70772013-04-17 20:16:15 -04002830
2831 lpfc_cmd->seg_cnt = 0;
2832 lpfc_cmd->prot_seg_cnt = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05002833 return 1;
2834}
2835
2836/*
James Smart737d4242013-04-17 20:14:49 -04002837 * This function calcuates the T10 DIF guard tag
2838 * on the specified data using a CRC algorithmn
2839 * using crc_t10dif.
2840 */
Rashika Kheria7bfe7812014-09-03 12:55:36 -04002841static uint16_t
James Smart737d4242013-04-17 20:14:49 -04002842lpfc_bg_crc(uint8_t *data, int count)
2843{
2844 uint16_t crc = 0;
2845 uint16_t x;
2846
2847 crc = crc_t10dif(data, count);
2848 x = cpu_to_be16(crc);
2849 return x;
2850}
2851
2852/*
2853 * This function calcuates the T10 DIF guard tag
2854 * on the specified data using a CSUM algorithmn
2855 * using ip_compute_csum.
2856 */
Rashika Kheria7bfe7812014-09-03 12:55:36 -04002857static uint16_t
James Smart737d4242013-04-17 20:14:49 -04002858lpfc_bg_csum(uint8_t *data, int count)
2859{
2860 uint16_t ret;
2861
2862 ret = ip_compute_csum(data, count);
2863 return ret;
2864}
2865
2866/*
2867 * This function examines the protection data to try to determine
2868 * what type of T10-DIF error occurred.
2869 */
Rashika Kheria7bfe7812014-09-03 12:55:36 -04002870static void
James Smart737d4242013-04-17 20:14:49 -04002871lpfc_calc_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
2872{
2873 struct scatterlist *sgpe; /* s/g prot entry */
2874 struct scatterlist *sgde; /* s/g data entry */
2875 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
2876 struct scsi_dif_tuple *src = NULL;
2877 uint8_t *data_src = NULL;
Sebastian Herbsztdb6f1c22015-08-31 16:48:14 -04002878 uint16_t guard_tag;
James Smart737d4242013-04-17 20:14:49 -04002879 uint16_t start_app_tag, app_tag;
2880 uint32_t start_ref_tag, ref_tag;
2881 int prot, protsegcnt;
2882 int err_type, len, data_len;
2883 int chk_ref, chk_app, chk_guard;
2884 uint16_t sum;
2885 unsigned blksize;
2886
2887 err_type = BGS_GUARD_ERR_MASK;
2888 sum = 0;
2889 guard_tag = 0;
2890
2891 /* First check to see if there is protection data to examine */
2892 prot = scsi_get_prot_op(cmd);
2893 if ((prot == SCSI_PROT_READ_STRIP) ||
2894 (prot == SCSI_PROT_WRITE_INSERT) ||
2895 (prot == SCSI_PROT_NORMAL))
2896 goto out;
2897
2898 /* Currently the driver just supports ref_tag and guard_tag checking */
2899 chk_ref = 1;
2900 chk_app = 0;
2901 chk_guard = 0;
2902
2903 /* Setup a ptr to the protection data provided by the SCSI host */
2904 sgpe = scsi_prot_sglist(cmd);
2905 protsegcnt = lpfc_cmd->prot_seg_cnt;
2906
2907 if (sgpe && protsegcnt) {
2908
2909 /*
2910 * We will only try to verify guard tag if the segment
2911 * data length is a multiple of the blksize.
2912 */
2913 sgde = scsi_sglist(cmd);
2914 blksize = lpfc_cmd_blksize(cmd);
2915 data_src = (uint8_t *)sg_virt(sgde);
2916 data_len = sgde->length;
2917 if ((data_len & (blksize - 1)) == 0)
2918 chk_guard = 1;
James Smart737d4242013-04-17 20:14:49 -04002919
James Smarte85d8f92013-05-31 17:04:10 -04002920 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
James Smarta6887e22013-04-17 20:18:07 -04002921 start_ref_tag = (uint32_t)scsi_get_lba(cmd); /* Truncate LBA */
James Smart737d4242013-04-17 20:14:49 -04002922 start_app_tag = src->app_tag;
James Smart737d4242013-04-17 20:14:49 -04002923 len = sgpe->length;
2924 while (src && protsegcnt) {
2925 while (len) {
2926
2927 /*
2928 * First check to see if a protection data
2929 * check is valid
2930 */
2931 if ((src->ref_tag == 0xffffffff) ||
2932 (src->app_tag == 0xffff)) {
2933 start_ref_tag++;
2934 goto skipit;
2935 }
2936
James Smart9c6aa9d2013-05-31 17:03:39 -04002937 /* First Guard Tag checking */
2938 if (chk_guard) {
2939 guard_tag = src->guard_tag;
2940 if (lpfc_cmd_guard_csum(cmd))
2941 sum = lpfc_bg_csum(data_src,
2942 blksize);
2943 else
2944 sum = lpfc_bg_crc(data_src,
2945 blksize);
2946 if ((guard_tag != sum)) {
2947 err_type = BGS_GUARD_ERR_MASK;
2948 goto out;
2949 }
James Smart737d4242013-04-17 20:14:49 -04002950 }
2951
2952 /* Reference Tag checking */
2953 ref_tag = be32_to_cpu(src->ref_tag);
2954 if (chk_ref && (ref_tag != start_ref_tag)) {
2955 err_type = BGS_REFTAG_ERR_MASK;
2956 goto out;
2957 }
2958 start_ref_tag++;
2959
James Smart9c6aa9d2013-05-31 17:03:39 -04002960 /* App Tag checking */
2961 app_tag = src->app_tag;
2962 if (chk_app && (app_tag != start_app_tag)) {
2963 err_type = BGS_APPTAG_ERR_MASK;
2964 goto out;
James Smart737d4242013-04-17 20:14:49 -04002965 }
2966skipit:
2967 len -= sizeof(struct scsi_dif_tuple);
2968 if (len < 0)
2969 len = 0;
2970 src++;
2971
2972 data_src += blksize;
2973 data_len -= blksize;
2974
2975 /*
2976 * Are we at the end of the Data segment?
2977 * The data segment is only used for Guard
2978 * tag checking.
2979 */
2980 if (chk_guard && (data_len == 0)) {
2981 chk_guard = 0;
2982 sgde = sg_next(sgde);
2983 if (!sgde)
2984 goto out;
2985
2986 data_src = (uint8_t *)sg_virt(sgde);
2987 data_len = sgde->length;
2988 if ((data_len & (blksize - 1)) == 0)
2989 chk_guard = 1;
2990 }
2991 }
2992
2993 /* Goto the next Protection data segment */
2994 sgpe = sg_next(sgpe);
2995 if (sgpe) {
2996 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
2997 len = sgpe->length;
2998 } else {
2999 src = NULL;
3000 }
3001 protsegcnt--;
3002 }
3003 }
3004out:
3005 if (err_type == BGS_GUARD_ERR_MASK) {
3006 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3007 0x10, 0x1);
3008 cmd->result = DRIVER_SENSE << 24
3009 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3010 phba->bg_guard_err_cnt++;
3011 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3012 "9069 BLKGRD: LBA %lx grd_tag error %x != %x\n",
3013 (unsigned long)scsi_get_lba(cmd),
3014 sum, guard_tag);
3015
3016 } else if (err_type == BGS_REFTAG_ERR_MASK) {
3017 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3018 0x10, 0x3);
3019 cmd->result = DRIVER_SENSE << 24
3020 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3021
3022 phba->bg_reftag_err_cnt++;
3023 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3024 "9066 BLKGRD: LBA %lx ref_tag error %x != %x\n",
3025 (unsigned long)scsi_get_lba(cmd),
3026 ref_tag, start_ref_tag);
3027
3028 } else if (err_type == BGS_APPTAG_ERR_MASK) {
3029 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3030 0x10, 0x2);
3031 cmd->result = DRIVER_SENSE << 24
3032 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3033
3034 phba->bg_apptag_err_cnt++;
3035 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3036 "9041 BLKGRD: LBA %lx app_tag error %x != %x\n",
3037 (unsigned long)scsi_get_lba(cmd),
3038 app_tag, start_app_tag);
3039 }
3040}
3041
3042
3043/*
James Smarte2a0a9d2008-12-04 22:40:02 -05003044 * This function checks for BlockGuard errors detected by
3045 * the HBA. In case of errors, the ASC/ASCQ fields in the
3046 * sense buffer will be set accordingly, paired with
3047 * ILLEGAL_REQUEST to signal to the kernel that the HBA
3048 * detected corruption.
3049 *
3050 * Returns:
3051 * 0 - No error found
3052 * 1 - BlockGuard error found
3053 * -1 - Internal error (bad profile, ...etc)
3054 */
3055static int
3056lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
3057 struct lpfc_iocbq *pIocbOut)
3058{
3059 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
3060 struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
3061 int ret = 0;
3062 uint32_t bghm = bgf->bghm;
3063 uint32_t bgstat = bgf->bgstat;
3064 uint64_t failing_sector = 0;
3065
James Smarte2a0a9d2008-12-04 22:40:02 -05003066 spin_lock(&_dump_buf_lock);
3067 if (!_dump_buf_done) {
James Smart6a9c52c2009-10-02 15:16:51 -04003068 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9070 BLKGRD: Saving"
3069 " Data for %u blocks to debugfs\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05003070 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
James Smart6a9c52c2009-10-02 15:16:51 -04003071 lpfc_debug_save_data(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003072
3073 /* If we have a prot sgl, save the DIF buffer */
3074 if (lpfc_prot_group_type(phba, cmd) ==
3075 LPFC_PG_TYPE_DIF_BUF) {
James Smart6a9c52c2009-10-02 15:16:51 -04003076 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9071 BLKGRD: "
3077 "Saving DIF for %u blocks to debugfs\n",
3078 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
3079 lpfc_debug_save_dif(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003080 }
3081
3082 _dump_buf_done = 1;
3083 }
3084 spin_unlock(&_dump_buf_lock);
3085
3086 if (lpfc_bgs_get_invalid_prof(bgstat)) {
3087 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart737d4242013-04-17 20:14:49 -04003088 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3089 "9072 BLKGRD: Invalid BG Profile in cmd"
3090 " 0x%x lba 0x%llx blk cnt 0x%x "
3091 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3092 (unsigned long long)scsi_get_lba(cmd),
3093 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003094 ret = (-1);
3095 goto out;
3096 }
3097
3098 if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
3099 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart737d4242013-04-17 20:14:49 -04003100 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3101 "9073 BLKGRD: Invalid BG PDIF Block in cmd"
3102 " 0x%x lba 0x%llx blk cnt 0x%x "
3103 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3104 (unsigned long long)scsi_get_lba(cmd),
3105 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003106 ret = (-1);
3107 goto out;
3108 }
3109
3110 if (lpfc_bgs_get_guard_err(bgstat)) {
3111 ret = 1;
3112
3113 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3114 0x10, 0x1);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05003115 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05003116 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3117 phba->bg_guard_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04003118 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3119 "9055 BLKGRD: Guard Tag error in cmd"
3120 " 0x%x lba 0x%llx blk cnt 0x%x "
3121 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3122 (unsigned long long)scsi_get_lba(cmd),
3123 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003124 }
3125
3126 if (lpfc_bgs_get_reftag_err(bgstat)) {
3127 ret = 1;
3128
3129 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3130 0x10, 0x3);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05003131 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05003132 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3133
3134 phba->bg_reftag_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04003135 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3136 "9056 BLKGRD: Ref Tag error in cmd"
3137 " 0x%x lba 0x%llx blk cnt 0x%x "
3138 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3139 (unsigned long long)scsi_get_lba(cmd),
3140 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003141 }
3142
3143 if (lpfc_bgs_get_apptag_err(bgstat)) {
3144 ret = 1;
3145
3146 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3147 0x10, 0x2);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05003148 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05003149 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3150
3151 phba->bg_apptag_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04003152 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3153 "9061 BLKGRD: App Tag error in cmd"
3154 " 0x%x lba 0x%llx blk cnt 0x%x "
3155 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3156 (unsigned long long)scsi_get_lba(cmd),
3157 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003158 }
3159
3160 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
3161 /*
3162 * setup sense data descriptor 0 per SPC-4 as an information
James Smart7c56b9f2011-07-22 18:36:25 -04003163 * field, and put the failing LBA in it.
3164 * This code assumes there was also a guard/app/ref tag error
3165 * indication.
James Smarte2a0a9d2008-12-04 22:40:02 -05003166 */
James Smart7c56b9f2011-07-22 18:36:25 -04003167 cmd->sense_buffer[7] = 0xc; /* Additional sense length */
3168 cmd->sense_buffer[8] = 0; /* Information descriptor type */
3169 cmd->sense_buffer[9] = 0xa; /* Additional descriptor length */
3170 cmd->sense_buffer[10] = 0x80; /* Validity bit */
James Smartacd68592012-01-18 16:25:09 -05003171
3172 /* bghm is a "on the wire" FC frame based count */
3173 switch (scsi_get_prot_op(cmd)) {
3174 case SCSI_PROT_READ_INSERT:
3175 case SCSI_PROT_WRITE_STRIP:
3176 bghm /= cmd->device->sector_size;
3177 break;
3178 case SCSI_PROT_READ_STRIP:
3179 case SCSI_PROT_WRITE_INSERT:
3180 case SCSI_PROT_READ_PASS:
3181 case SCSI_PROT_WRITE_PASS:
3182 bghm /= (cmd->device->sector_size +
3183 sizeof(struct scsi_dif_tuple));
3184 break;
3185 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003186
3187 failing_sector = scsi_get_lba(cmd);
3188 failing_sector += bghm;
3189
James Smart7c56b9f2011-07-22 18:36:25 -04003190 /* Descriptor Information */
3191 put_unaligned_be64(failing_sector, &cmd->sense_buffer[12]);
James Smarte2a0a9d2008-12-04 22:40:02 -05003192 }
3193
3194 if (!ret) {
3195 /* No error was reported - problem in FW? */
James Smart737d4242013-04-17 20:14:49 -04003196 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3197 "9057 BLKGRD: Unknown error in cmd"
3198 " 0x%x lba 0x%llx blk cnt 0x%x "
3199 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3200 (unsigned long long)scsi_get_lba(cmd),
3201 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003202
James Smart737d4242013-04-17 20:14:49 -04003203 /* Calcuate what type of error it was */
3204 lpfc_calc_bg_err(phba, lpfc_cmd);
3205 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003206out:
3207 return ret;
3208}
3209
James Smartea2151b2008-09-07 11:52:10 -04003210/**
James Smartda0436e2009-05-22 14:51:39 -04003211 * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3212 * @phba: The Hba for which this call is being executed.
3213 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3214 *
3215 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
3216 * field of @lpfc_cmd for device with SLI-4 interface spec.
3217 *
3218 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04003219 * 1 - Error
3220 * 0 - Success
James Smartda0436e2009-05-22 14:51:39 -04003221 **/
3222static int
3223lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3224{
3225 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3226 struct scatterlist *sgel = NULL;
3227 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3228 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
James Smartfedd3b72011-02-16 12:39:24 -05003229 struct sli4_sge *first_data_sgl;
James Smartda0436e2009-05-22 14:51:39 -04003230 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
3231 dma_addr_t physaddr;
3232 uint32_t num_bde = 0;
3233 uint32_t dma_len;
3234 uint32_t dma_offset = 0;
3235 int nseg;
James Smartfedd3b72011-02-16 12:39:24 -05003236 struct ulp_bde64 *bde;
James Smartda0436e2009-05-22 14:51:39 -04003237
3238 /*
3239 * There are three possibilities here - use scatter-gather segment, use
3240 * the single mapping, or neither. Start the lpfc command prep by
3241 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
3242 * data bde entry.
3243 */
3244 if (scsi_sg_count(scsi_cmnd)) {
3245 /*
3246 * The driver stores the segment count returned from pci_map_sg
3247 * because this a count of dma-mappings used to map the use_sg
3248 * pages. They are not guaranteed to be the same for those
3249 * architectures that implement an IOMMU.
3250 */
3251
3252 nseg = scsi_dma_map(scsi_cmnd);
James Smart5116fbf2015-05-22 10:42:39 -04003253 if (unlikely(nseg <= 0))
James Smartda0436e2009-05-22 14:51:39 -04003254 return 1;
3255 sgl += 1;
3256 /* clear the last flag in the fcp_rsp map entry */
3257 sgl->word2 = le32_to_cpu(sgl->word2);
3258 bf_set(lpfc_sli4_sge_last, sgl, 0);
3259 sgl->word2 = cpu_to_le32(sgl->word2);
3260 sgl += 1;
James Smartfedd3b72011-02-16 12:39:24 -05003261 first_data_sgl = sgl;
James Smartda0436e2009-05-22 14:51:39 -04003262 lpfc_cmd->seg_cnt = nseg;
3263 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04003264 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
3265 " %s: Too many sg segments from "
3266 "dma_map_sg. Config %d, seg_cnt %d\n",
3267 __func__, phba->cfg_sg_seg_cnt,
James Smartda0436e2009-05-22 14:51:39 -04003268 lpfc_cmd->seg_cnt);
James Smart96f70772013-04-17 20:16:15 -04003269 lpfc_cmd->seg_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04003270 scsi_dma_unmap(scsi_cmnd);
3271 return 1;
3272 }
3273
3274 /*
3275 * The driver established a maximum scatter-gather segment count
3276 * during probe that limits the number of sg elements in any
3277 * single scsi command. Just run through the seg_cnt and format
3278 * the sge's.
3279 * When using SLI-3 the driver will try to fit all the BDEs into
3280 * the IOCB. If it can't then the BDEs get added to a BPL as it
3281 * does for SLI-2 mode.
3282 */
3283 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
3284 physaddr = sg_dma_address(sgel);
3285 dma_len = sg_dma_len(sgel);
James Smartda0436e2009-05-22 14:51:39 -04003286 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
3287 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
James Smart05580562011-05-24 11:40:48 -04003288 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -04003289 if ((num_bde + 1) == nseg)
3290 bf_set(lpfc_sli4_sge_last, sgl, 1);
3291 else
3292 bf_set(lpfc_sli4_sge_last, sgl, 0);
3293 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
James Smartf9bb2da2011-10-10 21:34:11 -04003294 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
James Smartda0436e2009-05-22 14:51:39 -04003295 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05003296 sgl->sge_len = cpu_to_le32(dma_len);
James Smartda0436e2009-05-22 14:51:39 -04003297 dma_offset += dma_len;
3298 sgl++;
3299 }
James Smartfedd3b72011-02-16 12:39:24 -05003300 /* setup the performance hint (first data BDE) if enabled */
3301 if (phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) {
3302 bde = (struct ulp_bde64 *)
3303 &(iocb_cmd->unsli3.sli3Words[5]);
3304 bde->addrLow = first_data_sgl->addr_lo;
3305 bde->addrHigh = first_data_sgl->addr_hi;
3306 bde->tus.f.bdeSize =
3307 le32_to_cpu(first_data_sgl->sge_len);
3308 bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
3309 bde->tus.w = cpu_to_le32(bde->tus.w);
3310 }
James Smartda0436e2009-05-22 14:51:39 -04003311 } else {
3312 sgl += 1;
3313 /* clear the last flag in the fcp_rsp map entry */
3314 sgl->word2 = le32_to_cpu(sgl->word2);
3315 bf_set(lpfc_sli4_sge_last, sgl, 1);
3316 sgl->word2 = cpu_to_le32(sgl->word2);
3317 }
3318
3319 /*
3320 * Finish initializing those IOCB fields that are dependent on the
3321 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
3322 * explicitly reinitialized.
3323 * all iocb memory resources are reused.
3324 */
3325 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
3326
3327 /*
3328 * Due to difference in data length between DIF/non-DIF paths,
3329 * we need to set word 4 of IOCB here
3330 */
3331 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
James Smart1ba981f2014-02-20 09:56:45 -05003332
3333 /*
3334 * If the OAS driver feature is enabled and the lun is enabled for
3335 * OAS, set the oas iocb related flags.
3336 */
James Smartf38fa0b2014-04-04 13:52:21 -04003337 if ((phba->cfg_fof) && ((struct lpfc_device_data *)
James Smart1ba981f2014-02-20 09:56:45 -05003338 scsi_cmnd->device->hostdata)->oas_enabled)
James Smart9bd2bff52014-09-03 12:57:30 -04003339 lpfc_cmd->cur_iocbq.iocb_flag |= (LPFC_IO_OAS | LPFC_IO_FOF);
James Smartda0436e2009-05-22 14:51:39 -04003340 return 0;
3341}
3342
3343/**
James Smartacd68592012-01-18 16:25:09 -05003344 * lpfc_bg_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3345 * @phba: The Hba for which this call is being executed.
3346 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3347 *
3348 * This is the protection/DIF aware version of
3349 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
3350 * two functions eventually, but for now, it's here
3351 **/
3352static int
3353lpfc_bg_scsi_prep_dma_buf_s4(struct lpfc_hba *phba,
3354 struct lpfc_scsi_buf *lpfc_cmd)
3355{
3356 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3357 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3358 struct sli4_sge *sgl = (struct sli4_sge *)(lpfc_cmd->fcp_bpl);
3359 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart96f70772013-04-17 20:16:15 -04003360 uint32_t num_sge = 0;
James Smartacd68592012-01-18 16:25:09 -05003361 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
3362 int prot_group_type = 0;
3363 int fcpdl;
3364
3365 /*
3366 * Start the lpfc command prep by bumping the sgl beyond fcp_cmnd
James Smart96f70772013-04-17 20:16:15 -04003367 * fcp_rsp regions to the first data sge entry
James Smartacd68592012-01-18 16:25:09 -05003368 */
3369 if (scsi_sg_count(scsi_cmnd)) {
3370 /*
3371 * The driver stores the segment count returned from pci_map_sg
3372 * because this a count of dma-mappings used to map the use_sg
3373 * pages. They are not guaranteed to be the same for those
3374 * architectures that implement an IOMMU.
3375 */
3376 datasegcnt = dma_map_sg(&phba->pcidev->dev,
3377 scsi_sglist(scsi_cmnd),
3378 scsi_sg_count(scsi_cmnd), datadir);
3379 if (unlikely(!datasegcnt))
3380 return 1;
3381
3382 sgl += 1;
3383 /* clear the last flag in the fcp_rsp map entry */
3384 sgl->word2 = le32_to_cpu(sgl->word2);
3385 bf_set(lpfc_sli4_sge_last, sgl, 0);
3386 sgl->word2 = cpu_to_le32(sgl->word2);
3387
3388 sgl += 1;
3389 lpfc_cmd->seg_cnt = datasegcnt;
James Smart96f70772013-04-17 20:16:15 -04003390
3391 /* First check if data segment count from SCSI Layer is good */
3392 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt)
3393 goto err;
James Smartacd68592012-01-18 16:25:09 -05003394
3395 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
3396
3397 switch (prot_group_type) {
3398 case LPFC_PG_TYPE_NO_DIF:
James Smart96f70772013-04-17 20:16:15 -04003399 /* Here we need to add a DISEED to the count */
3400 if ((lpfc_cmd->seg_cnt + 1) > phba->cfg_total_seg_cnt)
3401 goto err;
3402
3403 num_sge = lpfc_bg_setup_sgl(phba, scsi_cmnd, sgl,
James Smartacd68592012-01-18 16:25:09 -05003404 datasegcnt);
James Smart96f70772013-04-17 20:16:15 -04003405
James Smartacd68592012-01-18 16:25:09 -05003406 /* we should have 2 or more entries in buffer list */
James Smart96f70772013-04-17 20:16:15 -04003407 if (num_sge < 2)
James Smartacd68592012-01-18 16:25:09 -05003408 goto err;
3409 break;
James Smart96f70772013-04-17 20:16:15 -04003410
3411 case LPFC_PG_TYPE_DIF_BUF:
James Smartacd68592012-01-18 16:25:09 -05003412 /*
3413 * This type indicates that protection buffers are
3414 * passed to the driver, so that needs to be prepared
3415 * for DMA
3416 */
3417 protsegcnt = dma_map_sg(&phba->pcidev->dev,
3418 scsi_prot_sglist(scsi_cmnd),
3419 scsi_prot_sg_count(scsi_cmnd), datadir);
3420 if (unlikely(!protsegcnt)) {
3421 scsi_dma_unmap(scsi_cmnd);
3422 return 1;
3423 }
3424
3425 lpfc_cmd->prot_seg_cnt = protsegcnt;
James Smart96f70772013-04-17 20:16:15 -04003426 /*
3427 * There is a minimun of 3 SGEs used for every
3428 * protection data segment.
3429 */
3430 if ((lpfc_cmd->prot_seg_cnt * 3) >
3431 (phba->cfg_total_seg_cnt - 2))
3432 goto err;
James Smartacd68592012-01-18 16:25:09 -05003433
James Smart96f70772013-04-17 20:16:15 -04003434 num_sge = lpfc_bg_setup_sgl_prot(phba, scsi_cmnd, sgl,
James Smartacd68592012-01-18 16:25:09 -05003435 datasegcnt, protsegcnt);
James Smart96f70772013-04-17 20:16:15 -04003436
James Smartacd68592012-01-18 16:25:09 -05003437 /* we should have 3 or more entries in buffer list */
James Smart96f70772013-04-17 20:16:15 -04003438 if ((num_sge < 3) ||
3439 (num_sge > phba->cfg_total_seg_cnt))
James Smartacd68592012-01-18 16:25:09 -05003440 goto err;
3441 break;
James Smart96f70772013-04-17 20:16:15 -04003442
James Smartacd68592012-01-18 16:25:09 -05003443 case LPFC_PG_TYPE_INVALID:
3444 default:
James Smart96f70772013-04-17 20:16:15 -04003445 scsi_dma_unmap(scsi_cmnd);
3446 lpfc_cmd->seg_cnt = 0;
3447
James Smartacd68592012-01-18 16:25:09 -05003448 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
3449 "9083 Unexpected protection group %i\n",
3450 prot_group_type);
3451 return 1;
3452 }
3453 }
3454
James Smart8012cc32012-10-31 14:44:49 -04003455 switch (scsi_get_prot_op(scsi_cmnd)) {
3456 case SCSI_PROT_WRITE_STRIP:
3457 case SCSI_PROT_READ_STRIP:
3458 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_STRIP;
3459 break;
3460 case SCSI_PROT_WRITE_INSERT:
3461 case SCSI_PROT_READ_INSERT:
3462 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_INSERT;
3463 break;
3464 case SCSI_PROT_WRITE_PASS:
3465 case SCSI_PROT_READ_PASS:
3466 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_PASS;
3467 break;
3468 }
3469
James Smartacd68592012-01-18 16:25:09 -05003470 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
James Smartacd68592012-01-18 16:25:09 -05003471 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
3472
3473 /*
3474 * Due to difference in data length between DIF/non-DIF paths,
3475 * we need to set word 4 of IOCB here
3476 */
3477 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
James Smartacd68592012-01-18 16:25:09 -05003478
James Smart9bd2bff52014-09-03 12:57:30 -04003479 /*
3480 * If the OAS driver feature is enabled and the lun is enabled for
3481 * OAS, set the oas iocb related flags.
3482 */
3483 if ((phba->cfg_fof) && ((struct lpfc_device_data *)
3484 scsi_cmnd->device->hostdata)->oas_enabled)
3485 lpfc_cmd->cur_iocbq.iocb_flag |= (LPFC_IO_OAS | LPFC_IO_FOF);
3486
James Smartacd68592012-01-18 16:25:09 -05003487 return 0;
3488err:
James Smart96f70772013-04-17 20:16:15 -04003489 if (lpfc_cmd->seg_cnt)
3490 scsi_dma_unmap(scsi_cmnd);
3491 if (lpfc_cmd->prot_seg_cnt)
3492 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
3493 scsi_prot_sg_count(scsi_cmnd),
3494 scsi_cmnd->sc_data_direction);
3495
James Smartacd68592012-01-18 16:25:09 -05003496 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart96f70772013-04-17 20:16:15 -04003497 "9084 Cannot setup S/G List for HBA"
3498 "IO segs %d/%d SGL %d SCSI %d: %d %d\n",
3499 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
3500 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
3501 prot_group_type, num_sge);
3502
3503 lpfc_cmd->seg_cnt = 0;
3504 lpfc_cmd->prot_seg_cnt = 0;
James Smartacd68592012-01-18 16:25:09 -05003505 return 1;
3506}
3507
3508/**
James Smart3772a992009-05-22 14:50:54 -04003509 * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3510 * @phba: The Hba for which this call is being executed.
3511 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3512 *
3513 * This routine wraps the actual DMA mapping function pointer from the
3514 * lpfc_hba struct.
3515 *
3516 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04003517 * 1 - Error
3518 * 0 - Success
James Smart3772a992009-05-22 14:50:54 -04003519 **/
3520static inline int
3521lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3522{
3523 return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
3524}
3525
3526/**
James Smartacd68592012-01-18 16:25:09 -05003527 * lpfc_bg_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3528 * using BlockGuard.
3529 * @phba: The Hba for which this call is being executed.
3530 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3531 *
3532 * This routine wraps the actual DMA mapping function pointer from the
3533 * lpfc_hba struct.
3534 *
3535 * Return codes:
3536 * 1 - Error
3537 * 0 - Success
3538 **/
3539static inline int
3540lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3541{
3542 return phba->lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
3543}
3544
3545/**
James Smart3621a712009-04-06 18:47:14 -04003546 * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
James Smartea2151b2008-09-07 11:52:10 -04003547 * @phba: Pointer to hba context object.
3548 * @vport: Pointer to vport object.
3549 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
3550 * @rsp_iocb: Pointer to response iocb object which reported error.
3551 *
3552 * This function posts an event when there is a SCSI command reporting
3553 * error from the scsi device.
3554 **/
3555static void
3556lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
3557 struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
3558 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3559 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
3560 uint32_t resp_info = fcprsp->rspStatus2;
3561 uint32_t scsi_status = fcprsp->rspStatus3;
3562 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
3563 struct lpfc_fast_path_event *fast_path_evt = NULL;
3564 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
3565 unsigned long flags;
3566
James Smart5989b8d2010-10-22 11:06:56 -04003567 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3568 return;
3569
James Smartea2151b2008-09-07 11:52:10 -04003570 /* If there is queuefull or busy condition send a scsi event */
3571 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
3572 (cmnd->result == SAM_STAT_BUSY)) {
3573 fast_path_evt = lpfc_alloc_fast_evt(phba);
3574 if (!fast_path_evt)
3575 return;
3576 fast_path_evt->un.scsi_evt.event_type =
3577 FC_REG_SCSI_EVENT;
3578 fast_path_evt->un.scsi_evt.subcategory =
3579 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
3580 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
3581 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
3582 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
3583 &pnode->nlp_portname, sizeof(struct lpfc_name));
3584 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
3585 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3586 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
3587 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
3588 fast_path_evt = lpfc_alloc_fast_evt(phba);
3589 if (!fast_path_evt)
3590 return;
3591 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
3592 FC_REG_SCSI_EVENT;
3593 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
3594 LPFC_EVENT_CHECK_COND;
3595 fast_path_evt->un.check_cond_evt.scsi_event.lun =
3596 cmnd->device->lun;
3597 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
3598 &pnode->nlp_portname, sizeof(struct lpfc_name));
3599 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
3600 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3601 fast_path_evt->un.check_cond_evt.sense_key =
3602 cmnd->sense_buffer[2] & 0xf;
3603 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
3604 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
3605 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3606 fcpi_parm &&
3607 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
3608 ((scsi_status == SAM_STAT_GOOD) &&
3609 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
3610 /*
3611 * If status is good or resid does not match with fcp_param and
3612 * there is valid fcpi_parm, then there is a read_check error
3613 */
3614 fast_path_evt = lpfc_alloc_fast_evt(phba);
3615 if (!fast_path_evt)
3616 return;
3617 fast_path_evt->un.read_check_error.header.event_type =
3618 FC_REG_FABRIC_EVENT;
3619 fast_path_evt->un.read_check_error.header.subcategory =
3620 LPFC_EVENT_FCPRDCHKERR;
3621 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
3622 &pnode->nlp_portname, sizeof(struct lpfc_name));
3623 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
3624 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3625 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
3626 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
3627 fast_path_evt->un.read_check_error.fcpiparam =
3628 fcpi_parm;
3629 } else
3630 return;
3631
3632 fast_path_evt->vport = vport;
3633 spin_lock_irqsave(&phba->hbalock, flags);
3634 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
3635 spin_unlock_irqrestore(&phba->hbalock, flags);
3636 lpfc_worker_wake_up(phba);
3637 return;
3638}
James Smart9bad7672008-12-04 22:39:02 -05003639
3640/**
James Smartf1126682009-06-10 17:22:44 -04003641 * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
James Smart3772a992009-05-22 14:50:54 -04003642 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -05003643 * @psb: The scsi buffer which is going to be un-mapped.
3644 *
3645 * This routine does DMA un-mapping of scatter gather list of scsi command
James Smart3772a992009-05-22 14:50:54 -04003646 * field of @lpfc_cmd for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05003647 **/
dea31012005-04-17 16:05:31 -05003648static void
James Smartf1126682009-06-10 17:22:44 -04003649lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James Smartbcf4dbf2006-07-06 15:50:08 -04003650{
3651 /*
3652 * There are only two special cases to consider. (1) the scsi command
3653 * requested scatter-gather usage or (2) the scsi command allocated
3654 * a request buffer, but did not request use_sg. There is a third
3655 * case, but it does not require resource deallocation.
3656 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003657 if (psb->seg_cnt > 0)
3658 scsi_dma_unmap(psb->pCmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003659 if (psb->prot_seg_cnt > 0)
3660 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
3661 scsi_prot_sg_count(psb->pCmd),
3662 psb->pCmd->sc_data_direction);
James Smartbcf4dbf2006-07-06 15:50:08 -04003663}
3664
James Smart9bad7672008-12-04 22:39:02 -05003665/**
James Smart3621a712009-04-06 18:47:14 -04003666 * lpfc_handler_fcp_err - FCP response handler
James Smart9bad7672008-12-04 22:39:02 -05003667 * @vport: The virtual port for which this call is being executed.
3668 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
3669 * @rsp_iocb: The response IOCB which contains FCP error.
3670 *
3671 * This routine is called to process response IOCB with status field
3672 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
3673 * based upon SCSI and FCP error.
3674 **/
James Smartbcf4dbf2006-07-06 15:50:08 -04003675static void
James Smart2e0fef82007-06-17 19:56:36 -05003676lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
3677 struct lpfc_iocbq *rsp_iocb)
dea31012005-04-17 16:05:31 -05003678{
James Smart5afab6b2015-12-16 18:12:01 -05003679 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003680 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3681 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
3682 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
James Smart7054a602007-04-25 09:52:34 -04003683 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
dea31012005-04-17 16:05:31 -05003684 uint32_t resp_info = fcprsp->rspStatus2;
3685 uint32_t scsi_status = fcprsp->rspStatus3;
James Smartc7743952006-12-02 13:34:42 -05003686 uint32_t *lp;
dea31012005-04-17 16:05:31 -05003687 uint32_t host_status = DID_OK;
3688 uint32_t rsplen = 0;
James Smart5afab6b2015-12-16 18:12:01 -05003689 uint32_t fcpDl;
James Smartc7743952006-12-02 13:34:42 -05003690 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea31012005-04-17 16:05:31 -05003691
James Smartea2151b2008-09-07 11:52:10 -04003692
dea31012005-04-17 16:05:31 -05003693 /*
3694 * If this is a task management command, there is no
3695 * scsi packet associated with this lpfc_cmd. The driver
3696 * consumes it.
3697 */
3698 if (fcpcmd->fcpCntl2) {
3699 scsi_status = 0;
3700 goto out;
3701 }
3702
James Smart6a9c52c2009-10-02 15:16:51 -04003703 if (resp_info & RSP_LEN_VALID) {
3704 rsplen = be32_to_cpu(fcprsp->rspRspLen);
James Smarte40a02c2010-02-26 14:13:54 -05003705 if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
James Smart6a9c52c2009-10-02 15:16:51 -04003706 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3707 "2719 Invalid response length: "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02003708 "tgt x%x lun x%llx cmnd x%x rsplen x%x\n",
James Smart6a9c52c2009-10-02 15:16:51 -04003709 cmnd->device->id,
3710 cmnd->device->lun, cmnd->cmnd[0],
3711 rsplen);
3712 host_status = DID_ERROR;
3713 goto out;
3714 }
James Smarte40a02c2010-02-26 14:13:54 -05003715 if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
3716 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3717 "2757 Protocol failure detected during "
3718 "processing of FCP I/O op: "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02003719 "tgt x%x lun x%llx cmnd x%x rspInfo3 x%x\n",
James Smarte40a02c2010-02-26 14:13:54 -05003720 cmnd->device->id,
3721 cmnd->device->lun, cmnd->cmnd[0],
3722 fcprsp->rspInfo3);
3723 host_status = DID_ERROR;
3724 goto out;
3725 }
James Smart6a9c52c2009-10-02 15:16:51 -04003726 }
3727
James Smartc7743952006-12-02 13:34:42 -05003728 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
3729 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
3730 if (snslen > SCSI_SENSE_BUFFERSIZE)
3731 snslen = SCSI_SENSE_BUFFERSIZE;
3732
3733 if (resp_info & RSP_LEN_VALID)
3734 rsplen = be32_to_cpu(fcprsp->rspRspLen);
3735 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
3736 }
3737 lp = (uint32_t *)cmnd->sense_buffer;
3738
James Smartaa1c7ee2012-08-14 14:26:06 -04003739 /* special handling for under run conditions */
3740 if (!scsi_status && (resp_info & RESID_UNDER)) {
3741 /* don't log under runs if fcp set... */
3742 if (vport->cfg_log_verbose & LOG_FCP)
3743 logit = LOG_FCP_ERROR;
3744 /* unless operator says so */
3745 if (vport->cfg_log_verbose & LOG_FCP_UNDER)
3746 logit = LOG_FCP_UNDER;
3747 }
James Smartc7743952006-12-02 13:34:42 -05003748
James Smarte8b62012007-08-02 11:10:09 -04003749 lpfc_printf_vlog(vport, KERN_WARNING, logit,
James Smarte2a0a9d2008-12-04 22:40:02 -05003750 "9024 FCP command x%x failed: x%x SNS x%x x%x "
James Smarte8b62012007-08-02 11:10:09 -04003751 "Data: x%x x%x x%x x%x x%x\n",
3752 cmnd->cmnd[0], scsi_status,
3753 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
3754 be32_to_cpu(fcprsp->rspResId),
3755 be32_to_cpu(fcprsp->rspSnsLen),
3756 be32_to_cpu(fcprsp->rspRspLen),
3757 fcprsp->rspInfo3);
dea31012005-04-17 16:05:31 -05003758
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003759 scsi_set_resid(cmnd, 0);
James Smart5afab6b2015-12-16 18:12:01 -05003760 fcpDl = be32_to_cpu(fcpcmd->fcpDl);
dea31012005-04-17 16:05:31 -05003761 if (resp_info & RESID_UNDER) {
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003762 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea31012005-04-17 16:05:31 -05003763
James Smart73d91e52011-10-10 21:32:10 -04003764 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_UNDER,
James Smarte2a0a9d2008-12-04 22:40:02 -05003765 "9025 FCP Read Underrun, expected %d, "
James Smarte8b62012007-08-02 11:10:09 -04003766 "residual %d Data: x%x x%x x%x\n",
James Smart5afab6b2015-12-16 18:12:01 -05003767 fcpDl,
James Smarte8b62012007-08-02 11:10:09 -04003768 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
3769 cmnd->underflow);
dea31012005-04-17 16:05:31 -05003770
3771 /*
James Smart7054a602007-04-25 09:52:34 -04003772 * If there is an under run check if under run reported by
3773 * storage array is same as the under run reported by HBA.
3774 * If this is not same, there is a dropped frame.
3775 */
3776 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3777 fcpi_parm &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003778 (scsi_get_resid(cmnd) != fcpi_parm)) {
James Smarte8b62012007-08-02 11:10:09 -04003779 lpfc_printf_vlog(vport, KERN_WARNING,
3780 LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05003781 "9026 FCP Read Check Error "
James Smarte8b62012007-08-02 11:10:09 -04003782 "and Underrun Data: x%x x%x x%x x%x\n",
James Smart5afab6b2015-12-16 18:12:01 -05003783 fcpDl,
James Smarte8b62012007-08-02 11:10:09 -04003784 scsi_get_resid(cmnd), fcpi_parm,
3785 cmnd->cmnd[0]);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003786 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
James Smart7054a602007-04-25 09:52:34 -04003787 host_status = DID_ERROR;
3788 }
3789 /*
dea31012005-04-17 16:05:31 -05003790 * The cmnd->underflow is the minimum number of bytes that must
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003791 * be transferred for this command. Provided a sense condition
dea31012005-04-17 16:05:31 -05003792 * is not present, make sure the actual amount transferred is at
3793 * least the underflow value or fail.
3794 */
3795 if (!(resp_info & SNS_LEN_VALID) &&
3796 (scsi_status == SAM_STAT_GOOD) &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003797 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
3798 < cmnd->underflow)) {
James Smarte8b62012007-08-02 11:10:09 -04003799 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05003800 "9027 FCP command x%x residual "
James Smarte8b62012007-08-02 11:10:09 -04003801 "underrun converted to error "
3802 "Data: x%x x%x x%x\n",
James Smart66dbfbe2007-08-05 06:08:38 -04003803 cmnd->cmnd[0], scsi_bufflen(cmnd),
James Smarte8b62012007-08-02 11:10:09 -04003804 scsi_get_resid(cmnd), cmnd->underflow);
dea31012005-04-17 16:05:31 -05003805 host_status = DID_ERROR;
3806 }
3807 } else if (resp_info & RESID_OVER) {
James Smarte8b62012007-08-02 11:10:09 -04003808 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05003809 "9028 FCP command x%x residual overrun error. "
James Smarte4e74272009-07-19 10:01:38 -04003810 "Data: x%x x%x\n", cmnd->cmnd[0],
James Smarte8b62012007-08-02 11:10:09 -04003811 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea31012005-04-17 16:05:31 -05003812 host_status = DID_ERROR;
3813
3814 /*
3815 * Check SLI validation that all the transfer was actually done
James Smart26373d22013-09-06 12:19:17 -04003816 * (fcpi_parm should be zero). Apply check only to reads.
dea31012005-04-17 16:05:31 -05003817 */
James Smart5afab6b2015-12-16 18:12:01 -05003818 } else if (fcpi_parm) {
James Smarte8b62012007-08-02 11:10:09 -04003819 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
James Smart5afab6b2015-12-16 18:12:01 -05003820 "9029 FCP %s Check Error xri x%x Data: "
James Smarteee88772010-09-29 11:19:08 -04003821 "x%x x%x x%x x%x x%x\n",
James Smart5afab6b2015-12-16 18:12:01 -05003822 ((cmnd->sc_data_direction == DMA_FROM_DEVICE) ?
3823 "Read" : "Write"),
3824 ((phba->sli_rev == LPFC_SLI_REV4) ?
3825 lpfc_cmd->cur_iocbq.sli4_xritag :
3826 rsp_iocb->iocb.ulpContext),
3827 fcpDl, be32_to_cpu(fcprsp->rspResId),
James Smarteee88772010-09-29 11:19:08 -04003828 fcpi_parm, cmnd->cmnd[0], scsi_status);
James Smart5afab6b2015-12-16 18:12:01 -05003829
3830 /* There is some issue with the LPe12000 that causes it
3831 * to miscalculate the fcpi_parm and falsely trip this
3832 * recovery logic. Detect this case and don't error when true.
3833 */
3834 if (fcpi_parm > fcpDl)
3835 goto out;
3836
James Smarteee88772010-09-29 11:19:08 -04003837 switch (scsi_status) {
3838 case SAM_STAT_GOOD:
3839 case SAM_STAT_CHECK_CONDITION:
3840 /* Fabric dropped a data frame. Fail any successful
3841 * command in which we detected dropped frames.
3842 * A status of good or some check conditions could
3843 * be considered a successful command.
3844 */
3845 host_status = DID_ERROR;
3846 break;
3847 }
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003848 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea31012005-04-17 16:05:31 -05003849 }
3850
3851 out:
3852 cmnd->result = ScsiResult(host_status, scsi_status);
James Smartea2151b2008-09-07 11:52:10 -04003853 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
dea31012005-04-17 16:05:31 -05003854}
3855
James Smart9bad7672008-12-04 22:39:02 -05003856/**
James Smart8b0dff12015-05-22 10:42:38 -04003857 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
3858 * @phba: Pointer to HBA context object.
3859 *
3860 * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
3861 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
3862 * held.
3863 * If scsi-mq is enabled, get the default block layer mapping of software queues
3864 * to hardware queues. This information is saved in request tag.
3865 *
3866 * Return: index into SLI4 fast-path FCP queue index.
3867 **/
3868int lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba,
3869 struct lpfc_scsi_buf *lpfc_cmd)
3870{
3871 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3872 struct lpfc_vector_map_info *cpup;
3873 int chann, cpu;
3874 uint32_t tag;
3875 uint16_t hwq;
3876
3877 if (shost_use_blk_mq(cmnd->device->host)) {
3878 tag = blk_mq_unique_tag(cmnd->request);
3879 hwq = blk_mq_unique_tag_to_hwq(tag);
3880
3881 return hwq;
3882 }
3883
3884 if (phba->cfg_fcp_io_sched == LPFC_FCP_SCHED_BY_CPU
3885 && phba->cfg_fcp_io_channel > 1) {
3886 cpu = smp_processor_id();
3887 if (cpu < phba->sli4_hba.num_present_cpu) {
3888 cpup = phba->sli4_hba.cpu_map;
3889 cpup += cpu;
3890 return cpup->channel_id;
3891 }
3892 }
3893 chann = atomic_add_return(1, &phba->fcp_qidx);
3894 chann = (chann % phba->cfg_fcp_io_channel);
3895 return chann;
3896}
3897
3898
3899/**
James Smart3621a712009-04-06 18:47:14 -04003900 * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
James Smart9bad7672008-12-04 22:39:02 -05003901 * @phba: The Hba for which this call is being executed.
3902 * @pIocbIn: The command IOCBQ for the scsi cmnd.
James Smart3772a992009-05-22 14:50:54 -04003903 * @pIocbOut: The response IOCBQ for the scsi cmnd.
James Smart9bad7672008-12-04 22:39:02 -05003904 *
3905 * This routine assigns scsi command result by looking into response IOCB
3906 * status field appropriately. This routine handles QUEUE FULL condition as
3907 * well by ramping down device queue depth.
3908 **/
dea31012005-04-17 16:05:31 -05003909static void
3910lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
3911 struct lpfc_iocbq *pIocbOut)
3912{
3913 struct lpfc_scsi_buf *lpfc_cmd =
3914 (struct lpfc_scsi_buf *) pIocbIn->context1;
James Smart2e0fef82007-06-17 19:56:36 -05003915 struct lpfc_vport *vport = pIocbIn->vport;
dea31012005-04-17 16:05:31 -05003916 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
3917 struct lpfc_nodelist *pnode = rdata->pnode;
James Smart75baf692010-06-08 18:31:21 -04003918 struct scsi_cmnd *cmd;
James Smart5ffc2662009-11-18 15:39:44 -05003919 int depth;
James Smartfa61a542008-01-11 01:52:42 -05003920 unsigned long flags;
James Smartea2151b2008-09-07 11:52:10 -04003921 struct lpfc_fast_path_event *fast_path_evt;
James Smart75baf692010-06-08 18:31:21 -04003922 struct Scsi_Host *shost;
James Smart73d91e52011-10-10 21:32:10 -04003923 uint32_t logit = LOG_FCP;
dea31012005-04-17 16:05:31 -05003924
James Smart75baf692010-06-08 18:31:21 -04003925 /* Sanity check on return of outstanding command */
James Smart75baf692010-06-08 18:31:21 -04003926 cmd = lpfc_cmd->pCmd;
James Smartc90261d2015-12-16 18:11:57 -05003927 if (!cmd)
3928 return;
James Smart75baf692010-06-08 18:31:21 -04003929 shost = cmd->device->host;
3930
James Smarte3d2b802012-08-14 14:25:43 -04003931 lpfc_cmd->result = (pIocbOut->iocb.un.ulpWord[4] & IOERR_PARAM_MASK);
dea31012005-04-17 16:05:31 -05003932 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
James Smart341af102010-01-26 23:07:37 -05003933 /* pick up SLI4 exhange busy status from HBA */
3934 lpfc_cmd->exch_busy = pIocbOut->iocb_flag & LPFC_EXCHANGE_BUSY;
3935
James Smart9a6b09c2012-03-01 22:37:42 -05003936#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
3937 if (lpfc_cmd->prot_data_type) {
3938 struct scsi_dif_tuple *src = NULL;
3939
3940 src = (struct scsi_dif_tuple *)lpfc_cmd->prot_data_segment;
3941 /*
3942 * Used to restore any changes to protection
3943 * data for error injection.
3944 */
3945 switch (lpfc_cmd->prot_data_type) {
3946 case LPFC_INJERR_REFTAG:
3947 src->ref_tag =
3948 lpfc_cmd->prot_data;
3949 break;
3950 case LPFC_INJERR_APPTAG:
3951 src->app_tag =
3952 (uint16_t)lpfc_cmd->prot_data;
3953 break;
3954 case LPFC_INJERR_GUARD:
3955 src->guard_tag =
3956 (uint16_t)lpfc_cmd->prot_data;
3957 break;
3958 default:
3959 break;
3960 }
3961
3962 lpfc_cmd->prot_data = 0;
3963 lpfc_cmd->prot_data_type = 0;
3964 lpfc_cmd->prot_data_segment = NULL;
3965 }
3966#endif
James Smart109f6ed2008-12-04 22:39:08 -05003967 if (pnode && NLP_CHK_NODE_ACT(pnode))
3968 atomic_dec(&pnode->cmd_pending);
dea31012005-04-17 16:05:31 -05003969
3970 if (lpfc_cmd->status) {
3971 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
3972 (lpfc_cmd->result & IOERR_DRVR_MASK))
3973 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
3974 else if (lpfc_cmd->status >= IOSTAT_CNT)
3975 lpfc_cmd->status = IOSTAT_DEFAULT;
James Smartaa1c7ee2012-08-14 14:26:06 -04003976 if (lpfc_cmd->status == IOSTAT_FCP_RSP_ERROR &&
3977 !lpfc_cmd->fcp_rsp->rspStatus3 &&
3978 (lpfc_cmd->fcp_rsp->rspStatus2 & RESID_UNDER) &&
3979 !(vport->cfg_log_verbose & LOG_FCP_UNDER))
James Smart73d91e52011-10-10 21:32:10 -04003980 logit = 0;
3981 else
3982 logit = LOG_FCP | LOG_FCP_UNDER;
3983 lpfc_printf_vlog(vport, KERN_WARNING, logit,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02003984 "9030 FCP cmd x%x failed <%d/%lld> "
James Smart5a0d80f2012-05-09 21:18:20 -04003985 "status: x%x result: x%x "
3986 "sid: x%x did: x%x oxid: x%x "
3987 "Data: x%x x%x\n",
James Smart73d91e52011-10-10 21:32:10 -04003988 cmd->cmnd[0],
3989 cmd->device ? cmd->device->id : 0xffff,
3990 cmd->device ? cmd->device->lun : 0xffff,
3991 lpfc_cmd->status, lpfc_cmd->result,
James Smart3bf41ba2013-05-31 17:03:18 -04003992 vport->fc_myDID,
3993 (pnode) ? pnode->nlp_DID : 0,
James Smart5a0d80f2012-05-09 21:18:20 -04003994 phba->sli_rev == LPFC_SLI_REV4 ?
3995 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
James Smart73d91e52011-10-10 21:32:10 -04003996 pIocbOut->iocb.ulpContext,
3997 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
dea31012005-04-17 16:05:31 -05003998
3999 switch (lpfc_cmd->status) {
4000 case IOSTAT_FCP_RSP_ERROR:
4001 /* Call FCP RSP handler to determine result */
James Smart2e0fef82007-06-17 19:56:36 -05004002 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea31012005-04-17 16:05:31 -05004003 break;
4004 case IOSTAT_NPORT_BSY:
4005 case IOSTAT_FABRIC_BSY:
James Smart0f1f53a2008-08-24 21:50:18 -04004006 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
James Smartea2151b2008-09-07 11:52:10 -04004007 fast_path_evt = lpfc_alloc_fast_evt(phba);
4008 if (!fast_path_evt)
4009 break;
4010 fast_path_evt->un.fabric_evt.event_type =
4011 FC_REG_FABRIC_EVENT;
4012 fast_path_evt->un.fabric_evt.subcategory =
4013 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
4014 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
4015 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
4016 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
4017 &pnode->nlp_portname,
4018 sizeof(struct lpfc_name));
4019 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
4020 &pnode->nlp_nodename,
4021 sizeof(struct lpfc_name));
4022 }
4023 fast_path_evt->vport = vport;
4024 fast_path_evt->work_evt.evt =
4025 LPFC_EVT_FASTPATH_MGMT_EVT;
4026 spin_lock_irqsave(&phba->hbalock, flags);
4027 list_add_tail(&fast_path_evt->work_evt.evt_listp,
4028 &phba->work_list);
4029 spin_unlock_irqrestore(&phba->hbalock, flags);
4030 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05004031 break;
James Smart92d7f7b2007-06-17 19:56:38 -05004032 case IOSTAT_LOCAL_REJECT:
James Smart1151e3e2011-02-16 12:39:35 -05004033 case IOSTAT_REMOTE_STOP:
James Smartab56dc22011-02-16 12:39:57 -05004034 if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
4035 lpfc_cmd->result ==
4036 IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
4037 lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
4038 lpfc_cmd->result ==
4039 IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
4040 cmd->result = ScsiResult(DID_NO_CONNECT, 0);
4041 break;
4042 }
James Smartd7c255b2008-08-24 21:50:00 -04004043 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
James Smart92d7f7b2007-06-17 19:56:38 -05004044 lpfc_cmd->result == IOERR_NO_RESOURCES ||
James Smartb92938b2010-06-07 15:24:12 -04004045 lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
4046 lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
James Smart92d7f7b2007-06-17 19:56:38 -05004047 cmd->result = ScsiResult(DID_REQUEUE, 0);
James Smart58da1ff2008-04-07 10:15:56 -04004048 break;
James Smarte2a0a9d2008-12-04 22:40:02 -05004049 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004050 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
4051 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
4052 pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
4053 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
4054 /*
4055 * This is a response for a BG enabled
4056 * cmd. Parse BG error
4057 */
4058 lpfc_parse_bg_err(phba, lpfc_cmd,
4059 pIocbOut);
4060 break;
4061 } else {
4062 lpfc_printf_vlog(vport, KERN_WARNING,
4063 LOG_BG,
4064 "9031 non-zero BGSTAT "
James Smart6a9c52c2009-10-02 15:16:51 -04004065 "on unprotected cmd\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05004066 }
4067 }
James Smart1151e3e2011-02-16 12:39:35 -05004068 if ((lpfc_cmd->status == IOSTAT_REMOTE_STOP)
4069 && (phba->sli_rev == LPFC_SLI_REV4)
4070 && (pnode && NLP_CHK_NODE_ACT(pnode))) {
4071 /* This IO was aborted by the target, we don't
4072 * know the rxid and because we did not send the
4073 * ABTS we cannot generate and RRQ.
4074 */
4075 lpfc_set_rrq_active(phba, pnode,
James Smartee0f4fe2012-05-09 21:19:14 -04004076 lpfc_cmd->cur_iocbq.sli4_lxritag,
4077 0, 0);
James Smart1151e3e2011-02-16 12:39:35 -05004078 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004079 /* else: fall through */
dea31012005-04-17 16:05:31 -05004080 default:
4081 cmd->result = ScsiResult(DID_ERROR, 0);
4082 break;
4083 }
4084
James Smart58da1ff2008-04-07 10:15:56 -04004085 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004086 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
James Smart0f1f53a2008-08-24 21:50:18 -04004087 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
4088 SAM_STAT_BUSY);
James Smartab56dc22011-02-16 12:39:57 -05004089 } else
dea31012005-04-17 16:05:31 -05004090 cmd->result = ScsiResult(DID_OK, 0);
dea31012005-04-17 16:05:31 -05004091
4092 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
4093 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
4094
James Smarte8b62012007-08-02 11:10:09 -04004095 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004096 "0710 Iodone <%d/%llu> cmd %p, error "
James Smarte8b62012007-08-02 11:10:09 -04004097 "x%x SNS x%x x%x Data: x%x x%x\n",
4098 cmd->device->id, cmd->device->lun, cmd,
4099 cmd->result, *lp, *(lp + 3), cmd->retries,
4100 scsi_get_resid(cmd));
dea31012005-04-17 16:05:31 -05004101 }
4102
James Smartea2151b2008-09-07 11:52:10 -04004103 lpfc_update_stats(phba, lpfc_cmd);
James Smart977b5a02008-09-07 11:52:04 -04004104 if (vport->cfg_max_scsicmpl_time &&
4105 time_after(jiffies, lpfc_cmd->start_time +
4106 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
James Smarta257bf92009-04-06 18:48:10 -04004107 spin_lock_irqsave(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05004108 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
4109 if (pnode->cmd_qdepth >
4110 atomic_read(&pnode->cmd_pending) &&
4111 (atomic_read(&pnode->cmd_pending) >
4112 LPFC_MIN_TGT_QDEPTH) &&
4113 ((cmd->cmnd[0] == READ_10) ||
4114 (cmd->cmnd[0] == WRITE_10)))
4115 pnode->cmd_qdepth =
4116 atomic_read(&pnode->cmd_pending);
James Smart977b5a02008-09-07 11:52:04 -04004117
James Smart109f6ed2008-12-04 22:39:08 -05004118 pnode->last_change_time = jiffies;
4119 }
James Smarta257bf92009-04-06 18:48:10 -04004120 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05004121 } else if (pnode && NLP_CHK_NODE_ACT(pnode)) {
James Smart7dc517d2010-07-14 15:32:10 -04004122 if ((pnode->cmd_qdepth < vport->cfg_tgt_queue_depth) &&
James Smart977b5a02008-09-07 11:52:04 -04004123 time_after(jiffies, pnode->last_change_time +
James Smart109f6ed2008-12-04 22:39:08 -05004124 msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
James Smarta257bf92009-04-06 18:48:10 -04004125 spin_lock_irqsave(shost->host_lock, flags);
James Smart7dc517d2010-07-14 15:32:10 -04004126 depth = pnode->cmd_qdepth * LPFC_TGTQ_RAMPUP_PCENT
4127 / 100;
4128 depth = depth ? depth : 1;
4129 pnode->cmd_qdepth += depth;
4130 if (pnode->cmd_qdepth > vport->cfg_tgt_queue_depth)
4131 pnode->cmd_qdepth = vport->cfg_tgt_queue_depth;
James Smart109f6ed2008-12-04 22:39:08 -05004132 pnode->last_change_time = jiffies;
James Smarta257bf92009-04-06 18:48:10 -04004133 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05004134 }
James Smart977b5a02008-09-07 11:52:04 -04004135 }
4136
James Smart1dcb58e2007-04-25 09:51:30 -04004137 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James Smarta257bf92009-04-06 18:48:10 -04004138
4139 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
dea31012005-04-17 16:05:31 -05004140 cmd->scsi_done(cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004141
Jamie Wellnitzb8086082006-02-28 22:33:12 -05004142 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart876dd7d2012-09-29 11:31:28 -04004143 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92e3af62012-08-14 14:26:28 -04004144 lpfc_cmd->pCmd = NULL;
James Smart876dd7d2012-09-29 11:31:28 -04004145 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart92e3af62012-08-14 14:26:28 -04004146
James Smartfa61a542008-01-11 01:52:42 -05004147 /*
4148 * If there is a thread waiting for command completion
4149 * wake up the thread.
4150 */
James Smarta257bf92009-04-06 18:48:10 -04004151 spin_lock_irqsave(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05004152 if (lpfc_cmd->waitq)
4153 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04004154 spin_unlock_irqrestore(shost->host_lock, flags);
Jamie Wellnitzb8086082006-02-28 22:33:12 -05004155 lpfc_release_scsi_buf(phba, lpfc_cmd);
4156 return;
4157 }
4158
James Smart876dd7d2012-09-29 11:31:28 -04004159 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92e3af62012-08-14 14:26:28 -04004160 lpfc_cmd->pCmd = NULL;
James Smart876dd7d2012-09-29 11:31:28 -04004161 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart92e3af62012-08-14 14:26:28 -04004162
James Smartfa61a542008-01-11 01:52:42 -05004163 /*
4164 * If there is a thread waiting for command completion
4165 * wake up the thread.
4166 */
James Smarta257bf92009-04-06 18:48:10 -04004167 spin_lock_irqsave(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05004168 if (lpfc_cmd->waitq)
4169 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04004170 spin_unlock_irqrestore(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05004171
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004172 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004173}
4174
James Smart34b02dc2008-08-24 21:49:55 -04004175/**
Alexey Kardashevskiy8b2564e2015-04-28 18:26:22 +10004176 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
4177 * @data: A pointer to the immediate command data portion of the IOCB.
4178 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
4179 *
4180 * The routine copies the entire FCP command from @fcp_cmnd to @data while
4181 * byte swapping the data to big endian format for transmission on the wire.
4182 **/
4183static void
4184lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
4185{
4186 int i, j;
4187 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
4188 i += sizeof(uint32_t), j++) {
4189 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
4190 }
4191}
4192
4193/**
James Smartf1126682009-06-10 17:22:44 -04004194 * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05004195 * @vport: The virtual port for which this call is being executed.
4196 * @lpfc_cmd: The scsi command which needs to send.
4197 * @pnode: Pointer to lpfc_nodelist.
4198 *
4199 * This routine initializes fcp_cmnd and iocb data structure from scsi command
James Smart3772a992009-05-22 14:50:54 -04004200 * to transfer for device with SLI3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05004201 **/
dea31012005-04-17 16:05:31 -05004202static void
James Smartf1126682009-06-10 17:22:44 -04004203lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
James Smart2e0fef82007-06-17 19:56:36 -05004204 struct lpfc_nodelist *pnode)
dea31012005-04-17 16:05:31 -05004205{
James Smart2e0fef82007-06-17 19:56:36 -05004206 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004207 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
4208 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
4209 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
4210 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
4211 int datadir = scsi_cmnd->sc_data_direction;
James Smart027140e2012-08-03 12:35:44 -04004212 uint8_t *ptr;
4213 bool sli4;
James Smart98bbf5f2013-09-06 12:18:45 -04004214 uint32_t fcpdl;
dea31012005-04-17 16:05:31 -05004215
James Smart58da1ff2008-04-07 10:15:56 -04004216 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
4217 return;
4218
dea31012005-04-17 16:05:31 -05004219 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
James.Smart@Emulex.Com69859dc2005-08-10 15:02:37 -04004220 /* clear task management bits */
4221 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea31012005-04-17 16:05:31 -05004222
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -04004223 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
4224 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05004225
James Smart027140e2012-08-03 12:35:44 -04004226 ptr = &fcp_cmnd->fcpCdb[0];
4227 memcpy(ptr, scsi_cmnd->cmnd, scsi_cmnd->cmd_len);
4228 if (scsi_cmnd->cmd_len < LPFC_FCP_CDB_LEN) {
4229 ptr += scsi_cmnd->cmd_len;
4230 memset(ptr, 0, (LPFC_FCP_CDB_LEN - scsi_cmnd->cmd_len));
4231 }
4232
Christoph Hellwig50668632014-10-30 14:30:06 +01004233 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
dea31012005-04-17 16:05:31 -05004234
James Smart027140e2012-08-03 12:35:44 -04004235 sli4 = (phba->sli_rev == LPFC_SLI_REV4);
James Smart6acb3482014-04-04 13:51:25 -04004236 piocbq->iocb.un.fcpi.fcpi_XRdy = 0;
James Smart027140e2012-08-03 12:35:44 -04004237
dea31012005-04-17 16:05:31 -05004238 /*
4239 * There are three possibilities here - use scatter-gather segment, use
4240 * the single mapping, or neither. Start the lpfc command prep by
4241 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
4242 * data bde entry.
4243 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05004244 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05004245 if (datadir == DMA_TO_DEVICE) {
4246 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
James Smart182ba752013-07-15 18:34:05 -04004247 iocb_cmd->ulpPU = PARM_READ_CHECK;
James Smart3cb01c52013-07-15 18:35:04 -04004248 if (vport->cfg_first_burst_size &&
4249 (pnode->nlp_flag & NLP_FIRSTBURST)) {
James Smart98bbf5f2013-09-06 12:18:45 -04004250 fcpdl = scsi_bufflen(scsi_cmnd);
4251 if (fcpdl < vport->cfg_first_burst_size)
4252 piocbq->iocb.un.fcpi.fcpi_XRdy = fcpdl;
4253 else
4254 piocbq->iocb.un.fcpi.fcpi_XRdy =
4255 vport->cfg_first_burst_size;
James Smart3cb01c52013-07-15 18:35:04 -04004256 }
dea31012005-04-17 16:05:31 -05004257 fcp_cmnd->fcpCntl3 = WRITE_DATA;
4258 phba->fc4OutputRequests++;
4259 } else {
4260 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
4261 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05004262 fcp_cmnd->fcpCntl3 = READ_DATA;
4263 phba->fc4InputRequests++;
4264 }
4265 } else {
4266 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
4267 iocb_cmd->un.fcpi.fcpi_parm = 0;
4268 iocb_cmd->ulpPU = 0;
4269 fcp_cmnd->fcpCntl3 = 0;
4270 phba->fc4ControlRequests++;
4271 }
Alexey Kardashevskiy8b2564e2015-04-28 18:26:22 +10004272 if (phba->sli_rev == 3 &&
4273 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
4274 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05004275 /*
4276 * Finish initializing those IOCB fields that are independent
4277 * of the scsi_cmnd request_buffer
4278 */
4279 piocbq->iocb.ulpContext = pnode->nlp_rpi;
James Smart027140e2012-08-03 12:35:44 -04004280 if (sli4)
James Smart6d368e52011-05-24 11:44:12 -04004281 piocbq->iocb.ulpContext =
4282 phba->sli4_hba.rpi_ids[pnode->nlp_rpi];
dea31012005-04-17 16:05:31 -05004283 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
4284 piocbq->iocb.ulpFCP2Rcvy = 1;
James Smart09372822008-01-11 01:52:54 -05004285 else
4286 piocbq->iocb.ulpFCP2Rcvy = 0;
dea31012005-04-17 16:05:31 -05004287
4288 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
4289 piocbq->context1 = lpfc_cmd;
4290 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
4291 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
James Smart2e0fef82007-06-17 19:56:36 -05004292 piocbq->vport = vport;
dea31012005-04-17 16:05:31 -05004293}
4294
James Smart9bad7672008-12-04 22:39:02 -05004295/**
James Smart6d368e52011-05-24 11:44:12 -04004296 * lpfc_scsi_prep_task_mgmt_cmd - Convert SLI3 scsi TM cmd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05004297 * @vport: The virtual port for which this call is being executed.
4298 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
4299 * @lun: Logical unit number.
4300 * @task_mgmt_cmd: SCSI task management command.
4301 *
James Smart3772a992009-05-22 14:50:54 -04004302 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
4303 * for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05004304 *
4305 * Return codes:
4306 * 0 - Error
4307 * 1 - Success
4308 **/
dea31012005-04-17 16:05:31 -05004309static int
James Smartf1126682009-06-10 17:22:44 -04004310lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
dea31012005-04-17 16:05:31 -05004311 struct lpfc_scsi_buf *lpfc_cmd,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004312 uint64_t lun,
dea31012005-04-17 16:05:31 -05004313 uint8_t task_mgmt_cmd)
4314{
dea31012005-04-17 16:05:31 -05004315 struct lpfc_iocbq *piocbq;
4316 IOCB_t *piocb;
4317 struct fcp_cmnd *fcp_cmnd;
James Smart0b18ac42006-05-01 21:50:40 -04004318 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea31012005-04-17 16:05:31 -05004319 struct lpfc_nodelist *ndlp = rdata->pnode;
4320
James Smart58da1ff2008-04-07 10:15:56 -04004321 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
4322 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05004323 return 0;
dea31012005-04-17 16:05:31 -05004324
dea31012005-04-17 16:05:31 -05004325 piocbq = &(lpfc_cmd->cur_iocbq);
James Smart2e0fef82007-06-17 19:56:36 -05004326 piocbq->vport = vport;
4327
dea31012005-04-17 16:05:31 -05004328 piocb = &piocbq->iocb;
4329
4330 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart34b02dc2008-08-24 21:49:55 -04004331 /* Clear out any old data in the FCP command area */
4332 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
4333 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05004334 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
James Smarte2a0a9d2008-12-04 22:40:02 -05004335 if (vport->phba->sli_rev == 3 &&
4336 !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04004337 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05004338 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
dea31012005-04-17 16:05:31 -05004339 piocb->ulpContext = ndlp->nlp_rpi;
James Smart6d368e52011-05-24 11:44:12 -04004340 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
4341 piocb->ulpContext =
4342 vport->phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
4343 }
James Smart53151bb2013-10-10 12:24:07 -04004344 piocb->ulpFCP2Rcvy = (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) ? 1 : 0;
dea31012005-04-17 16:05:31 -05004345 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
James Smartf9226c22014-02-20 09:57:28 -05004346 piocb->ulpPU = 0;
4347 piocb->un.fcpi.fcpi_parm = 0;
dea31012005-04-17 16:05:31 -05004348
4349 /* ulpTimeout is only one byte */
4350 if (lpfc_cmd->timeout > 0xff) {
4351 /*
4352 * Do not timeout the command at the firmware level.
4353 * The driver will provide the timeout mechanism.
4354 */
4355 piocb->ulpTimeout = 0;
James Smartf1126682009-06-10 17:22:44 -04004356 } else
dea31012005-04-17 16:05:31 -05004357 piocb->ulpTimeout = lpfc_cmd->timeout;
James Smartf1126682009-06-10 17:22:44 -04004358
4359 if (vport->phba->sli_rev == LPFC_SLI_REV4)
4360 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004361
James Smart2e0fef82007-06-17 19:56:36 -05004362 return 1;
dea31012005-04-17 16:05:31 -05004363}
4364
James Smart9bad7672008-12-04 22:39:02 -05004365/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004366 * lpfc_scsi_api_table_setup - Set up scsi api function jump table
James Smart3772a992009-05-22 14:50:54 -04004367 * @phba: The hba struct for which this call is being executed.
4368 * @dev_grp: The HBA PCI-Device group number.
4369 *
4370 * This routine sets up the SCSI interface API function jump table in @phba
4371 * struct.
4372 * Returns: 0 - success, -ENODEV - failure.
4373 **/
4374int
4375lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4376{
4377
James Smartf1126682009-06-10 17:22:44 -04004378 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
4379 phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
James Smartf1126682009-06-10 17:22:44 -04004380
James Smart3772a992009-05-22 14:50:54 -04004381 switch (dev_grp) {
4382 case LPFC_PCI_DEV_LP:
4383 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s3;
4384 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
James Smartacd68592012-01-18 16:25:09 -05004385 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04004386 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
James Smart19ca7602010-11-20 23:11:55 -05004387 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04004388 break;
James Smartda0436e2009-05-22 14:51:39 -04004389 case LPFC_PCI_DEV_OC:
4390 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s4;
4391 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
James Smartacd68592012-01-18 16:25:09 -05004392 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04004393 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
James Smart19ca7602010-11-20 23:11:55 -05004394 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04004395 break;
James Smart3772a992009-05-22 14:50:54 -04004396 default:
4397 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4398 "1418 Invalid HBA PCI-device group: 0x%x\n",
4399 dev_grp);
4400 return -ENODEV;
4401 break;
4402 }
James Smart3772a992009-05-22 14:50:54 -04004403 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
James Smart84d1b002010-02-12 14:42:33 -05004404 phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
James Smart3772a992009-05-22 14:50:54 -04004405 return 0;
4406}
4407
4408/**
James Smart3621a712009-04-06 18:47:14 -04004409 * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
James Smart9bad7672008-12-04 22:39:02 -05004410 * @phba: The Hba for which this call is being executed.
4411 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
4412 * @rspiocbq: Pointer to lpfc_iocbq data structure.
4413 *
4414 * This routine is IOCB completion routine for device reset and target reset
4415 * routine. This routine release scsi buffer associated with lpfc_cmd.
4416 **/
James Smart7054a602007-04-25 09:52:34 -04004417static void
4418lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
4419 struct lpfc_iocbq *cmdiocbq,
4420 struct lpfc_iocbq *rspiocbq)
4421{
4422 struct lpfc_scsi_buf *lpfc_cmd =
4423 (struct lpfc_scsi_buf *) cmdiocbq->context1;
4424 if (lpfc_cmd)
4425 lpfc_release_scsi_buf(phba, lpfc_cmd);
4426 return;
4427}
4428
James Smart9bad7672008-12-04 22:39:02 -05004429/**
James Smart3621a712009-04-06 18:47:14 -04004430 * lpfc_info - Info entry point of scsi_host_template data structure
James Smart9bad7672008-12-04 22:39:02 -05004431 * @host: The scsi host for which this call is being executed.
4432 *
4433 * This routine provides module information about hba.
4434 *
4435 * Reutrn code:
4436 * Pointer to char - Success.
4437 **/
dea31012005-04-17 16:05:31 -05004438const char *
4439lpfc_info(struct Scsi_Host *host)
4440{
James Smart2e0fef82007-06-17 19:56:36 -05004441 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
4442 struct lpfc_hba *phba = vport->phba;
James Smart8b68cd52012-09-29 11:32:37 -04004443 int len, link_speed = 0;
dea31012005-04-17 16:05:31 -05004444 static char lpfcinfobuf[384];
4445
4446 memset(lpfcinfobuf,0,384);
4447 if (phba && phba->pcidev){
4448 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
4449 len = strlen(lpfcinfobuf);
4450 snprintf(lpfcinfobuf + len,
4451 384-len,
4452 " on PCI bus %02x device %02x irq %d",
4453 phba->pcidev->bus->number,
4454 phba->pcidev->devfn,
4455 phba->pcidev->irq);
4456 len = strlen(lpfcinfobuf);
4457 if (phba->Port[0]) {
4458 snprintf(lpfcinfobuf + len,
4459 384-len,
4460 " port %s",
4461 phba->Port);
4462 }
James Smart65467b62010-01-26 23:08:29 -05004463 len = strlen(lpfcinfobuf);
James Smart8b68cd52012-09-29 11:32:37 -04004464 if (phba->sli_rev <= LPFC_SLI_REV3) {
4465 link_speed = lpfc_sli_port_speed_get(phba);
4466 } else {
4467 if (phba->sli4_hba.link_state.logical_speed)
4468 link_speed =
4469 phba->sli4_hba.link_state.logical_speed;
4470 else
4471 link_speed = phba->sli4_hba.link_state.speed;
James Smart65467b62010-01-26 23:08:29 -05004472 }
James Smart8b68cd52012-09-29 11:32:37 -04004473 if (link_speed != 0)
4474 snprintf(lpfcinfobuf + len, 384-len,
4475 " Logical Link Speed: %d Mbps", link_speed);
dea31012005-04-17 16:05:31 -05004476 }
4477 return lpfcinfobuf;
4478}
4479
James Smart9bad7672008-12-04 22:39:02 -05004480/**
James Smart3621a712009-04-06 18:47:14 -04004481 * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
James Smart9bad7672008-12-04 22:39:02 -05004482 * @phba: The Hba for which this call is being executed.
4483 *
4484 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
4485 * The default value of cfg_poll_tmo is 10 milliseconds.
4486 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004487static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
4488{
4489 unsigned long poll_tmo_expires =
4490 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
4491
James Smart0e9bb8d2013-03-01 16:35:12 -05004492 if (!list_empty(&phba->sli.ring[LPFC_FCP_RING].txcmplq))
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004493 mod_timer(&phba->fcp_poll_timer,
4494 poll_tmo_expires);
4495}
4496
James Smart9bad7672008-12-04 22:39:02 -05004497/**
James Smart3621a712009-04-06 18:47:14 -04004498 * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
James Smart9bad7672008-12-04 22:39:02 -05004499 * @phba: The Hba for which this call is being executed.
4500 *
4501 * This routine starts the fcp_poll_timer of @phba.
4502 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004503void lpfc_poll_start_timer(struct lpfc_hba * phba)
4504{
4505 lpfc_poll_rearm_timer(phba);
4506}
4507
James Smart9bad7672008-12-04 22:39:02 -05004508/**
James Smart3621a712009-04-06 18:47:14 -04004509 * lpfc_poll_timeout - Restart polling timer
James Smart9bad7672008-12-04 22:39:02 -05004510 * @ptr: Map to lpfc_hba data structure pointer.
4511 *
4512 * This routine restarts fcp_poll timer, when FCP ring polling is enable
4513 * and FCP Ring interrupt is disable.
4514 **/
4515
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004516void lpfc_poll_timeout(unsigned long ptr)
4517{
James Smart2e0fef82007-06-17 19:56:36 -05004518 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004519
4520 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004521 lpfc_sli_handle_fast_ring_event(phba,
4522 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4523
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004524 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4525 lpfc_poll_rearm_timer(phba);
4526 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004527}
4528
James Smart9bad7672008-12-04 22:39:02 -05004529/**
James Smart3621a712009-04-06 18:47:14 -04004530 * lpfc_queuecommand - scsi_host_template queuecommand entry point
James Smart9bad7672008-12-04 22:39:02 -05004531 * @cmnd: Pointer to scsi_cmnd data structure.
4532 * @done: Pointer to done routine.
4533 *
4534 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
4535 * This routine prepares an IOCB from scsi command and provides to firmware.
4536 * The @done callback is invoked after driver finished processing the command.
4537 *
4538 * Return value :
4539 * 0 - Success
4540 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
4541 **/
dea31012005-04-17 16:05:31 -05004542static int
James Smartb9a7c632012-08-03 12:35:24 -04004543lpfc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004544{
James Smart2e0fef82007-06-17 19:56:36 -05004545 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4546 struct lpfc_hba *phba = vport->phba;
James Smart1ba981f2014-02-20 09:56:45 -05004547 struct lpfc_rport_data *rdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05004548 struct lpfc_nodelist *ndlp;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004549 struct lpfc_scsi_buf *lpfc_cmd;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004550 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004551 int err;
dea31012005-04-17 16:05:31 -05004552
James Smart1ba981f2014-02-20 09:56:45 -05004553 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004554 err = fc_remote_port_chkready(rport);
4555 if (err) {
4556 cmnd->result = err;
dea31012005-04-17 16:05:31 -05004557 goto out_fail_command;
4558 }
James Smart1c6f4ef52009-11-18 15:40:49 -05004559 ndlp = rdata->pnode;
dea31012005-04-17 16:05:31 -05004560
James Smartbf086112011-08-21 21:48:13 -04004561 if ((scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) &&
James Smartacd68592012-01-18 16:25:09 -05004562 (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED))) {
James Smarte2a0a9d2008-12-04 22:40:02 -05004563
James Smart6a9c52c2009-10-02 15:16:51 -04004564 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4565 "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
4566 " op:%02x str=%s without registering for"
4567 " BlockGuard - Rejecting command\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05004568 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
4569 dif_op_str[scsi_get_prot_op(cmnd)]);
4570 goto out_fail_command;
4571 }
4572
dea31012005-04-17 16:05:31 -05004573 /*
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004574 * Catch race where our node has transitioned, but the
4575 * transport is still transitioning.
dea31012005-04-17 16:05:31 -05004576 */
James Smart6b415f52012-06-12 13:54:59 -04004577 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
4578 goto out_tgt_busy;
James Smart7dc517d2010-07-14 15:32:10 -04004579 if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth)
Mike Christie34963432011-02-25 14:04:28 -06004580 goto out_tgt_busy;
Mike Christiea93ce022008-08-17 15:24:41 -05004581
James Smart19ca7602010-11-20 23:11:55 -05004582 lpfc_cmd = lpfc_get_scsi_buf(phba, ndlp);
dea31012005-04-17 16:05:31 -05004583 if (lpfc_cmd == NULL) {
James Smarteaf15d52008-12-04 22:39:29 -05004584 lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05004585
James Smartf9114622015-05-21 13:55:30 -04004586 lpfc_printf_vlog(vport, KERN_INFO, LOG_MISC,
James Smarte8b62012007-08-02 11:10:09 -04004587 "0707 driver's buffer pool is empty, "
4588 "IO busied\n");
dea31012005-04-17 16:05:31 -05004589 goto out_host_busy;
4590 }
4591
4592 /*
4593 * Store the midlayer's command structure for the completion phase
4594 * and complete the command initialization.
4595 */
4596 lpfc_cmd->pCmd = cmnd;
4597 lpfc_cmd->rdata = rdata;
4598 lpfc_cmd->timeout = 0;
James Smart977b5a02008-09-07 11:52:04 -04004599 lpfc_cmd->start_time = jiffies;
dea31012005-04-17 16:05:31 -05004600 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
dea31012005-04-17 16:05:31 -05004601
James Smarte2a0a9d2008-12-04 22:40:02 -05004602 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
James Smart6a9c52c2009-10-02 15:16:51 -04004603 if (vport->phba->cfg_enable_bg) {
James Smart737d4242013-04-17 20:14:49 -04004604 lpfc_printf_vlog(vport,
4605 KERN_INFO, LOG_SCSI_CMD,
James Smart26134702012-08-14 14:25:50 -04004606 "9033 BLKGRD: rcvd %s cmd:x%x "
4607 "sector x%llx cnt %u pt %x\n",
4608 dif_op_str[scsi_get_prot_op(cmnd)],
4609 cmnd->cmnd[0],
4610 (unsigned long long)scsi_get_lba(cmnd),
4611 blk_rq_sectors(cmnd->request),
4612 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004613 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004614 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
4615 } else {
James Smart6a9c52c2009-10-02 15:16:51 -04004616 if (vport->phba->cfg_enable_bg) {
James Smart737d4242013-04-17 20:14:49 -04004617 lpfc_printf_vlog(vport,
4618 KERN_INFO, LOG_SCSI_CMD,
James Smart26134702012-08-14 14:25:50 -04004619 "9038 BLKGRD: rcvd PROT_NORMAL cmd: "
4620 "x%x sector x%llx cnt %u pt %x\n",
4621 cmnd->cmnd[0],
4622 (unsigned long long)scsi_get_lba(cmnd),
James Smart9a6b09c2012-03-01 22:37:42 -05004623 blk_rq_sectors(cmnd->request),
James Smart26134702012-08-14 14:25:50 -04004624 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004625 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004626 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
4627 }
4628
dea31012005-04-17 16:05:31 -05004629 if (err)
4630 goto out_host_busy_free_buf;
4631
James Smart2e0fef82007-06-17 19:56:36 -05004632 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea31012005-04-17 16:05:31 -05004633
James Smart977b5a02008-09-07 11:52:04 -04004634 atomic_inc(&ndlp->cmd_pending);
James Smart3772a992009-05-22 14:50:54 -04004635 err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
James Smart92d7f7b2007-06-17 19:56:38 -05004636 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
James Smarteaf15d52008-12-04 22:39:29 -05004637 if (err) {
4638 atomic_dec(&ndlp->cmd_pending);
James Smart76f96b62013-12-17 20:29:01 -05004639 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4640 "3376 FCP could not issue IOCB err %x"
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004641 "FCP cmd x%x <%d/%llu> "
James Smart76f96b62013-12-17 20:29:01 -05004642 "sid: x%x did: x%x oxid: x%x "
4643 "Data: x%x x%x x%x x%x\n",
4644 err, cmnd->cmnd[0],
4645 cmnd->device ? cmnd->device->id : 0xffff,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004646 cmnd->device ? cmnd->device->lun : (u64) -1,
James Smart76f96b62013-12-17 20:29:01 -05004647 vport->fc_myDID, ndlp->nlp_DID,
4648 phba->sli_rev == LPFC_SLI_REV4 ?
4649 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
4650 lpfc_cmd->cur_iocbq.iocb.ulpContext,
4651 lpfc_cmd->cur_iocbq.iocb.ulpIoTag,
4652 lpfc_cmd->cur_iocbq.iocb.ulpTimeout,
4653 (uint32_t)
4654 (cmnd->request->timeout / 1000));
4655
4656
dea31012005-04-17 16:05:31 -05004657 goto out_host_busy_free_buf;
James Smarteaf15d52008-12-04 22:39:29 -05004658 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004659 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004660 lpfc_sli_handle_fast_ring_event(phba,
4661 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4662
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004663 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4664 lpfc_poll_rearm_timer(phba);
4665 }
4666
dea31012005-04-17 16:05:31 -05004667 return 0;
4668
4669 out_host_busy_free_buf:
James Smartbcf4dbf2006-07-06 15:50:08 -04004670 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004671 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004672 out_host_busy:
4673 return SCSI_MLQUEUE_HOST_BUSY;
4674
Mike Christie34963432011-02-25 14:04:28 -06004675 out_tgt_busy:
4676 return SCSI_MLQUEUE_TARGET_BUSY;
4677
dea31012005-04-17 16:05:31 -05004678 out_fail_command:
James Smartb9a7c632012-08-03 12:35:24 -04004679 cmnd->scsi_done(cmnd);
dea31012005-04-17 16:05:31 -05004680 return 0;
4681}
4682
Jeff Garzikf2812332010-11-16 02:10:29 -05004683
James Smart9bad7672008-12-04 22:39:02 -05004684/**
James Smart3621a712009-04-06 18:47:14 -04004685 * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05004686 * @cmnd: Pointer to scsi_cmnd data structure.
4687 *
4688 * This routine aborts @cmnd pending in base driver.
4689 *
4690 * Return code :
4691 * 0x2003 - Error
4692 * 0x2002 - Success
4693 **/
dea31012005-04-17 16:05:31 -05004694static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05004695lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004696{
James Smart2e0fef82007-06-17 19:56:36 -05004697 struct Scsi_Host *shost = cmnd->device->host;
4698 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4699 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004700 struct lpfc_iocbq *iocb;
4701 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05004702 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05004703 IOCB_t *cmd, *icmd;
James Smart3a707302012-06-12 13:54:42 -04004704 int ret = SUCCESS, status = 0;
James Smart98912dd2014-04-04 13:52:31 -04004705 struct lpfc_sli_ring *pring_s4;
4706 int ring_number, ret_val;
4707 unsigned long flags, iflags;
James Smartfa61a542008-01-11 01:52:42 -05004708 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004709
James Smart3a707302012-06-12 13:54:42 -04004710 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04004711 if (status != 0 && status != SUCCESS)
James Smart3a707302012-06-12 13:54:42 -04004712 return status;
James Smart4f2e66c2012-05-09 21:17:07 -04004713
James Smart876dd7d2012-09-29 11:31:28 -04004714 spin_lock_irqsave(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004715 /* driver queued commands are in process of being flushed */
4716 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
James Smart876dd7d2012-09-29 11:31:28 -04004717 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004718 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4719 "3168 SCSI Layer abort requested I/O has been "
4720 "flushed by LLD.\n");
4721 return FAILED;
4722 }
4723
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004724 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
James Smart92e3af62012-08-14 14:26:28 -04004725 if (!lpfc_cmd || !lpfc_cmd->pCmd) {
James Smart876dd7d2012-09-29 11:31:28 -04004726 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarteee88772010-09-29 11:19:08 -04004727 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4728 "2873 SCSI Layer I/O Abort Request IO CMPL Status "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004729 "x%x ID %d LUN %llu\n",
James Smart3a707302012-06-12 13:54:42 -04004730 SUCCESS, cmnd->device->id, cmnd->device->lun);
James Smarteee88772010-09-29 11:19:08 -04004731 return SUCCESS;
4732 }
dea31012005-04-17 16:05:31 -05004733
James Smart4f2e66c2012-05-09 21:17:07 -04004734 iocb = &lpfc_cmd->cur_iocbq;
4735 /* the command is in process of being cancelled */
4736 if (!(iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
James Smart876dd7d2012-09-29 11:31:28 -04004737 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004738 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4739 "3169 SCSI Layer abort requested I/O has been "
4740 "cancelled by LLD.\n");
4741 return FAILED;
4742 }
dea31012005-04-17 16:05:31 -05004743 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004744 * If pCmd field of the corresponding lpfc_scsi_buf structure
4745 * points to a different SCSI command, then the driver has
4746 * already completed this command, but the midlayer did not
James Smart4f2e66c2012-05-09 21:17:07 -04004747 * see the completion before the eh fired. Just return SUCCESS.
dea31012005-04-17 16:05:31 -05004748 */
James Smart4f2e66c2012-05-09 21:17:07 -04004749 if (lpfc_cmd->pCmd != cmnd) {
4750 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4751 "3170 SCSI Layer abort requested I/O has been "
4752 "completed by LLD.\n");
4753 goto out_unlock;
4754 }
dea31012005-04-17 16:05:31 -05004755
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004756 BUG_ON(iocb->context1 != lpfc_cmd);
dea31012005-04-17 16:05:31 -05004757
James Smartee620212014-04-04 13:51:53 -04004758 /* abort issued in recovery is still in progress */
4759 if (iocb->iocb_flag & LPFC_DRIVER_ABORTED) {
4760 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4761 "3389 SCSI Layer I/O Abort Request is pending\n");
4762 spin_unlock_irqrestore(&phba->hbalock, flags);
4763 goto wait_for_cmpl;
4764 }
4765
James Smart4f2e66c2012-05-09 21:17:07 -04004766 abtsiocb = __lpfc_sli_get_iocbq(phba);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004767 if (abtsiocb == NULL) {
4768 ret = FAILED;
James Smart4f2e66c2012-05-09 21:17:07 -04004769 goto out_unlock;
dea31012005-04-17 16:05:31 -05004770 }
4771
James Smartafbd8d82013-09-06 12:22:13 -04004772 /* Indicate the IO is being aborted by the driver. */
4773 iocb->iocb_flag |= LPFC_DRIVER_ABORTED;
4774
dea31012005-04-17 16:05:31 -05004775 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004776 * The scsi command can not be in txq and it is in flight because the
4777 * pCmd is still pointig at the SCSI command we have to abort. There
4778 * is no need to search the txcmplq. Just send an abort to the FW.
dea31012005-04-17 16:05:31 -05004779 */
dea31012005-04-17 16:05:31 -05004780
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004781 cmd = &iocb->iocb;
4782 icmd = &abtsiocb->iocb;
4783 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
4784 icmd->un.acxri.abortContextTag = cmd->ulpContext;
James Smart3772a992009-05-22 14:50:54 -04004785 if (phba->sli_rev == LPFC_SLI_REV4)
4786 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
4787 else
4788 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05004789
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004790 icmd->ulpLe = 1;
4791 icmd->ulpClass = cmd->ulpClass;
James Smart5ffc2662009-11-18 15:39:44 -05004792
4793 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
4794 abtsiocb->fcp_wqidx = iocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05004795 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -04004796 if (iocb->iocb_flag & LPFC_IO_FOF)
4797 abtsiocb->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -05004798
James Smart2e0fef82007-06-17 19:56:36 -05004799 if (lpfc_is_link_up(phba))
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004800 icmd->ulpCommand = CMD_ABORT_XRI_CN;
4801 else
4802 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea31012005-04-17 16:05:31 -05004803
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004804 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smart2e0fef82007-06-17 19:56:36 -05004805 abtsiocb->vport = vport;
James Smart98912dd2014-04-04 13:52:31 -04004806 if (phba->sli_rev == LPFC_SLI_REV4) {
4807 ring_number = MAX_SLI3_CONFIGURED_RINGS + iocb->fcp_wqidx;
4808 pring_s4 = &phba->sli.ring[ring_number];
4809 /* Note: both hbalock and ring_lock must be set here */
4810 spin_lock_irqsave(&pring_s4->ring_lock, iflags);
4811 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
4812 abtsiocb, 0);
4813 spin_unlock_irqrestore(&pring_s4->ring_lock, iflags);
4814 } else {
4815 ret_val = __lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
4816 abtsiocb, 0);
4817 }
James Smart4f2e66c2012-05-09 21:17:07 -04004818 /* no longer need the lock after this point */
James Smart876dd7d2012-09-29 11:31:28 -04004819 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004820
James Smart98912dd2014-04-04 13:52:31 -04004821
4822 if (ret_val == IOCB_ERROR) {
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004823 lpfc_sli_release_iocbq(phba, abtsiocb);
4824 ret = FAILED;
4825 goto out;
4826 }
4827
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004828 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
James Smart45ed1192009-10-02 15:17:02 -04004829 lpfc_sli_handle_fast_ring_event(phba,
4830 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004831
James Smartee620212014-04-04 13:51:53 -04004832wait_for_cmpl:
James Smartfa61a542008-01-11 01:52:42 -05004833 lpfc_cmd->waitq = &waitq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004834 /* Wait for abort to complete */
James Smartfa61a542008-01-11 01:52:42 -05004835 wait_event_timeout(waitq,
4836 (lpfc_cmd->pCmd != cmnd),
James Smart256ec0d2013-04-17 20:14:58 -04004837 msecs_to_jiffies(2*vport->cfg_devloss_tmo*1000));
James Smartee620212014-04-04 13:51:53 -04004838
4839 spin_lock_irqsave(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05004840 lpfc_cmd->waitq = NULL;
James Smartee620212014-04-04 13:51:53 -04004841 spin_unlock_irqrestore(shost->host_lock, flags);
dea31012005-04-17 16:05:31 -05004842
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004843 if (lpfc_cmd->pCmd == cmnd) {
4844 ret = FAILED;
James Smarte8b62012007-08-02 11:10:09 -04004845 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4846 "0748 abort handler timed out waiting "
James Smart247ca942012-08-14 14:26:13 -04004847 "for abortng I/O (xri:x%x) to complete: "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004848 "ret %#x, ID %d, LUN %llu\n",
James Smart247ca942012-08-14 14:26:13 -04004849 iocb->sli4_xritag, ret,
4850 cmnd->device->id, cmnd->device->lun);
dea31012005-04-17 16:05:31 -05004851 }
James Smart4f2e66c2012-05-09 21:17:07 -04004852 goto out;
dea31012005-04-17 16:05:31 -05004853
James Smart4f2e66c2012-05-09 21:17:07 -04004854out_unlock:
James Smart876dd7d2012-09-29 11:31:28 -04004855 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004856out:
James Smarte8b62012007-08-02 11:10:09 -04004857 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4858 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004859 "LUN %llu\n", ret, cmnd->device->id,
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04004860 cmnd->device->lun);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004861 return ret;
dea31012005-04-17 16:05:31 -05004862}
4863
James Smartbbb9d182009-06-10 17:23:16 -04004864static char *
4865lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
4866{
4867 switch (task_mgmt_cmd) {
4868 case FCP_ABORT_TASK_SET:
4869 return "ABORT_TASK_SET";
4870 case FCP_CLEAR_TASK_SET:
4871 return "FCP_CLEAR_TASK_SET";
4872 case FCP_BUS_RESET:
4873 return "FCP_BUS_RESET";
4874 case FCP_LUN_RESET:
4875 return "FCP_LUN_RESET";
4876 case FCP_TARGET_RESET:
4877 return "FCP_TARGET_RESET";
4878 case FCP_CLEAR_ACA:
4879 return "FCP_CLEAR_ACA";
4880 case FCP_TERMINATE_TASK:
4881 return "FCP_TERMINATE_TASK";
4882 default:
4883 return "unknown";
4884 }
4885}
4886
James Smart53151bb2013-10-10 12:24:07 -04004887
4888/**
4889 * lpfc_check_fcp_rsp - check the returned fcp_rsp to see if task failed
4890 * @vport: The virtual port for which this call is being executed.
4891 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
4892 *
4893 * This routine checks the FCP RSP INFO to see if the tsk mgmt command succeded
4894 *
4895 * Return code :
4896 * 0x2003 - Error
4897 * 0x2002 - Success
4898 **/
4899static int
4900lpfc_check_fcp_rsp(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd)
4901{
4902 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
4903 uint32_t rsp_info;
4904 uint32_t rsp_len;
4905 uint8_t rsp_info_code;
4906 int ret = FAILED;
4907
4908
4909 if (fcprsp == NULL)
4910 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4911 "0703 fcp_rsp is missing\n");
4912 else {
4913 rsp_info = fcprsp->rspStatus2;
4914 rsp_len = be32_to_cpu(fcprsp->rspRspLen);
4915 rsp_info_code = fcprsp->rspInfo3;
4916
4917
4918 lpfc_printf_vlog(vport, KERN_INFO,
4919 LOG_FCP,
4920 "0706 fcp_rsp valid 0x%x,"
4921 " rsp len=%d code 0x%x\n",
4922 rsp_info,
4923 rsp_len, rsp_info_code);
4924
4925 if ((fcprsp->rspStatus2&RSP_LEN_VALID) && (rsp_len == 8)) {
4926 switch (rsp_info_code) {
4927 case RSP_NO_FAILURE:
4928 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4929 "0715 Task Mgmt No Failure\n");
4930 ret = SUCCESS;
4931 break;
4932 case RSP_TM_NOT_SUPPORTED: /* TM rejected */
4933 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4934 "0716 Task Mgmt Target "
4935 "reject\n");
4936 break;
4937 case RSP_TM_NOT_COMPLETED: /* TM failed */
4938 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4939 "0717 Task Mgmt Target "
4940 "failed TM\n");
4941 break;
4942 case RSP_TM_INVALID_LU: /* TM to invalid LU! */
4943 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4944 "0718 Task Mgmt to invalid "
4945 "LUN\n");
4946 break;
4947 }
4948 }
4949 }
4950 return ret;
4951}
4952
4953
James Smartbbb9d182009-06-10 17:23:16 -04004954/**
4955 * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
4956 * @vport: The virtual port for which this call is being executed.
4957 * @rdata: Pointer to remote port local data
4958 * @tgt_id: Target ID of remote device.
4959 * @lun_id: Lun number for the TMF
4960 * @task_mgmt_cmd: type of TMF to send
4961 *
4962 * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
4963 * a remote port.
4964 *
4965 * Return Code:
4966 * 0x2003 - Error
4967 * 0x2002 - Success.
4968 **/
4969static int
4970lpfc_send_taskmgmt(struct lpfc_vport *vport, struct lpfc_rport_data *rdata,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004971 unsigned tgt_id, uint64_t lun_id,
James Smartbbb9d182009-06-10 17:23:16 -04004972 uint8_t task_mgmt_cmd)
4973{
4974 struct lpfc_hba *phba = vport->phba;
4975 struct lpfc_scsi_buf *lpfc_cmd;
4976 struct lpfc_iocbq *iocbq;
4977 struct lpfc_iocbq *iocbqrsp;
James Smart5989b8d2010-10-22 11:06:56 -04004978 struct lpfc_nodelist *pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004979 int ret;
4980 int status;
4981
James Smart5989b8d2010-10-22 11:06:56 -04004982 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
James Smartbbb9d182009-06-10 17:23:16 -04004983 return FAILED;
4984
James Smart19ca7602010-11-20 23:11:55 -05004985 lpfc_cmd = lpfc_get_scsi_buf(phba, rdata->pnode);
James Smartbbb9d182009-06-10 17:23:16 -04004986 if (lpfc_cmd == NULL)
4987 return FAILED;
James Smart0c411222013-09-06 12:22:46 -04004988 lpfc_cmd->timeout = phba->cfg_task_mgmt_tmo;
James Smartbbb9d182009-06-10 17:23:16 -04004989 lpfc_cmd->rdata = rdata;
4990
4991 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
4992 task_mgmt_cmd);
4993 if (!status) {
4994 lpfc_release_scsi_buf(phba, lpfc_cmd);
4995 return FAILED;
4996 }
4997
4998 iocbq = &lpfc_cmd->cur_iocbq;
4999 iocbqrsp = lpfc_sli_get_iocbq(phba);
5000 if (iocbqrsp == NULL) {
5001 lpfc_release_scsi_buf(phba, lpfc_cmd);
5002 return FAILED;
5003 }
James Smart5a0916b2013-07-15 18:31:42 -04005004 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
James Smartbbb9d182009-06-10 17:23:16 -04005005
5006 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005007 "0702 Issue %s to TGT %d LUN %llu "
James Smart6d368e52011-05-24 11:44:12 -04005008 "rpi x%x nlp_flag x%x Data: x%x x%x\n",
James Smartbbb9d182009-06-10 17:23:16 -04005009 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
James Smart6d368e52011-05-24 11:44:12 -04005010 pnode->nlp_rpi, pnode->nlp_flag, iocbq->sli4_xritag,
5011 iocbq->iocb_flag);
James Smartbbb9d182009-06-10 17:23:16 -04005012
5013 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
5014 iocbq, iocbqrsp, lpfc_cmd->timeout);
James Smart53151bb2013-10-10 12:24:07 -04005015 if ((status != IOCB_SUCCESS) ||
5016 (iocbqrsp->iocb.ulpStatus != IOSTAT_SUCCESS)) {
James Smartae374a32015-05-22 10:42:41 -04005017 if (status != IOCB_SUCCESS ||
5018 iocbqrsp->iocb.ulpStatus != IOSTAT_FCP_RSP_ERROR)
5019 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5020 "0727 TMF %s to TGT %d LUN %llu "
5021 "failed (%d, %d) iocb_flag x%x\n",
5022 lpfc_taskmgmt_name(task_mgmt_cmd),
5023 tgt_id, lun_id,
5024 iocbqrsp->iocb.ulpStatus,
5025 iocbqrsp->iocb.un.ulpWord[4],
5026 iocbq->iocb_flag);
James Smart53151bb2013-10-10 12:24:07 -04005027 /* if ulpStatus != IOCB_SUCCESS, then status == IOCB_SUCCESS */
5028 if (status == IOCB_SUCCESS) {
5029 if (iocbqrsp->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
5030 /* Something in the FCP_RSP was invalid.
5031 * Check conditions */
5032 ret = lpfc_check_fcp_rsp(vport, lpfc_cmd);
5033 else
5034 ret = FAILED;
5035 } else if (status == IOCB_TIMEDOUT) {
5036 ret = TIMEOUT_ERROR;
5037 } else {
5038 ret = FAILED;
5039 }
James Smart53151bb2013-10-10 12:24:07 -04005040 } else
James Smartbbb9d182009-06-10 17:23:16 -04005041 ret = SUCCESS;
5042
5043 lpfc_sli_release_iocbq(phba, iocbqrsp);
5044
5045 if (ret != TIMEOUT_ERROR)
5046 lpfc_release_scsi_buf(phba, lpfc_cmd);
5047
5048 return ret;
5049}
5050
5051/**
5052 * lpfc_chk_tgt_mapped -
5053 * @vport: The virtual port to check on
5054 * @cmnd: Pointer to scsi_cmnd data structure.
5055 *
5056 * This routine delays until the scsi target (aka rport) for the
5057 * command exists (is present and logged in) or we declare it non-existent.
5058 *
5059 * Return code :
5060 * 0x2003 - Error
5061 * 0x2002 - Success
5062 **/
5063static int
5064lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
5065{
James Smart1ba981f2014-02-20 09:56:45 -05005066 struct lpfc_rport_data *rdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005067 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005068 unsigned long later;
5069
James Smart1ba981f2014-02-20 09:56:45 -05005070 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smart1c6f4ef52009-11-18 15:40:49 -05005071 if (!rdata) {
5072 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5073 "0797 Tgt Map rport failure: rdata x%p\n", rdata);
5074 return FAILED;
5075 }
5076 pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005077 /*
5078 * If target is not in a MAPPED state, delay until
5079 * target is rediscovered or devloss timeout expires.
5080 */
5081 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
5082 while (time_after(later, jiffies)) {
5083 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
5084 return FAILED;
5085 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
5086 return SUCCESS;
5087 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
James Smart1ba981f2014-02-20 09:56:45 -05005088 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smartbbb9d182009-06-10 17:23:16 -04005089 if (!rdata)
5090 return FAILED;
5091 pnode = rdata->pnode;
5092 }
5093 if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
5094 (pnode->nlp_state != NLP_STE_MAPPED_NODE))
5095 return FAILED;
5096 return SUCCESS;
5097}
5098
5099/**
5100 * lpfc_reset_flush_io_context -
5101 * @vport: The virtual port (scsi_host) for the flush context
5102 * @tgt_id: If aborting by Target contect - specifies the target id
5103 * @lun_id: If aborting by Lun context - specifies the lun id
5104 * @context: specifies the context level to flush at.
5105 *
5106 * After a reset condition via TMF, we need to flush orphaned i/o
5107 * contexts from the adapter. This routine aborts any contexts
5108 * outstanding, then waits for their completions. The wait is
5109 * bounded by devloss_tmo though.
5110 *
5111 * Return code :
5112 * 0x2003 - Error
5113 * 0x2002 - Success
5114 **/
5115static int
5116lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
5117 uint64_t lun_id, lpfc_ctx_cmd context)
5118{
5119 struct lpfc_hba *phba = vport->phba;
5120 unsigned long later;
5121 int cnt;
5122
5123 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
5124 if (cnt)
James Smart98912dd2014-04-04 13:52:31 -04005125 lpfc_sli_abort_taskmgmt(vport,
5126 &phba->sli.ring[phba->sli.fcp_ring],
5127 tgt_id, lun_id, context);
James Smartbbb9d182009-06-10 17:23:16 -04005128 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
5129 while (time_after(later, jiffies) && cnt) {
5130 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
5131 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
5132 }
5133 if (cnt) {
5134 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5135 "0724 I/O flush failure for context %s : cnt x%x\n",
5136 ((context == LPFC_CTX_LUN) ? "LUN" :
5137 ((context == LPFC_CTX_TGT) ? "TGT" :
5138 ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
5139 cnt);
5140 return FAILED;
5141 }
5142 return SUCCESS;
5143}
5144
James Smart9bad7672008-12-04 22:39:02 -05005145/**
James Smart3621a712009-04-06 18:47:14 -04005146 * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
James Smart9bad7672008-12-04 22:39:02 -05005147 * @cmnd: Pointer to scsi_cmnd data structure.
5148 *
James Smartbbb9d182009-06-10 17:23:16 -04005149 * This routine does a device reset by sending a LUN_RESET task management
James Smart9bad7672008-12-04 22:39:02 -05005150 * command.
5151 *
5152 * Return code :
5153 * 0x2003 - Error
James Smart3621a712009-04-06 18:47:14 -04005154 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05005155 **/
dea31012005-04-17 16:05:31 -05005156static int
James Smart7054a602007-04-25 09:52:34 -04005157lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05005158{
James Smart2e0fef82007-06-17 19:56:36 -05005159 struct Scsi_Host *shost = cmnd->device->host;
5160 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smart1ba981f2014-02-20 09:56:45 -05005161 struct lpfc_rport_data *rdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005162 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005163 unsigned tgt_id = cmnd->device->id;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005164 uint64_t lun_id = cmnd->device->lun;
James Smartea2151b2008-09-07 11:52:10 -04005165 struct lpfc_scsi_event_header scsi_event;
James Smart53151bb2013-10-10 12:24:07 -04005166 int status;
dea31012005-04-17 16:05:31 -05005167
James Smart1ba981f2014-02-20 09:56:45 -05005168 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smartad490b62015-04-07 15:07:26 -04005169 if (!rdata || !rdata->pnode) {
James Smart1c6f4ef52009-11-18 15:40:49 -05005170 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smartad490b62015-04-07 15:07:26 -04005171 "0798 Device Reset rport failure: rdata x%p\n",
5172 rdata);
James Smart1c6f4ef52009-11-18 15:40:49 -05005173 return FAILED;
5174 }
5175 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04005176 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005177 if (status != 0 && status != SUCCESS)
James Smart589a52d2010-07-14 15:30:54 -04005178 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005179
5180 status = lpfc_chk_tgt_mapped(vport, cmnd);
5181 if (status == FAILED) {
5182 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5183 "0721 Device Reset rport failure: rdata x%p\n", rdata);
5184 return FAILED;
5185 }
5186
5187 scsi_event.event_type = FC_REG_SCSI_EVENT;
5188 scsi_event.subcategory = LPFC_EVENT_LUNRESET;
5189 scsi_event.lun = lun_id;
5190 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5191 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5192
5193 fc_host_post_vendor_event(shost, fc_get_event_number(),
5194 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
5195
5196 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
5197 FCP_LUN_RESET);
5198
5199 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005200 "0713 SCSI layer issued Device Reset (%d, %llu) "
James Smartbbb9d182009-06-10 17:23:16 -04005201 "return x%x\n", tgt_id, lun_id, status);
5202
dea31012005-04-17 16:05:31 -05005203 /*
James Smartbbb9d182009-06-10 17:23:16 -04005204 * We have to clean up i/o as : they may be orphaned by the TMF;
5205 * or if the TMF failed, they may be in an indeterminate state.
5206 * So, continue on.
5207 * We will report success if all the i/o aborts successfully.
dea31012005-04-17 16:05:31 -05005208 */
James Smart53151bb2013-10-10 12:24:07 -04005209 if (status == SUCCESS)
5210 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
James Smartbbb9d182009-06-10 17:23:16 -04005211 LPFC_CTX_LUN);
James Smart53151bb2013-10-10 12:24:07 -04005212
5213 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005214}
5215
5216/**
5217 * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
5218 * @cmnd: Pointer to scsi_cmnd data structure.
5219 *
5220 * This routine does a target reset by sending a TARGET_RESET task management
5221 * command.
5222 *
5223 * Return code :
5224 * 0x2003 - Error
5225 * 0x2002 - Success
5226 **/
5227static int
5228lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
5229{
5230 struct Scsi_Host *shost = cmnd->device->host;
5231 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smart1ba981f2014-02-20 09:56:45 -05005232 struct lpfc_rport_data *rdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005233 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005234 unsigned tgt_id = cmnd->device->id;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005235 uint64_t lun_id = cmnd->device->lun;
James Smartbbb9d182009-06-10 17:23:16 -04005236 struct lpfc_scsi_event_header scsi_event;
James Smart53151bb2013-10-10 12:24:07 -04005237 int status;
James Smartbbb9d182009-06-10 17:23:16 -04005238
James Smart1ba981f2014-02-20 09:56:45 -05005239 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smart1c6f4ef52009-11-18 15:40:49 -05005240 if (!rdata) {
5241 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5242 "0799 Target Reset rport failure: rdata x%p\n", rdata);
5243 return FAILED;
5244 }
5245 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04005246 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005247 if (status != 0 && status != SUCCESS)
James Smart589a52d2010-07-14 15:30:54 -04005248 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005249
5250 status = lpfc_chk_tgt_mapped(vport, cmnd);
5251 if (status == FAILED) {
5252 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5253 "0722 Target Reset rport failure: rdata x%p\n", rdata);
James Smart63e480f2015-04-07 15:07:20 -04005254 if (pnode) {
5255 spin_lock_irq(shost->host_lock);
5256 pnode->nlp_flag &= ~NLP_NPR_ADISC;
5257 pnode->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
5258 spin_unlock_irq(shost->host_lock);
5259 }
James Smart8c50d252014-09-03 12:58:16 -04005260 lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
5261 LPFC_CTX_TGT);
5262 return FAST_IO_FAIL;
dea31012005-04-17 16:05:31 -05005263 }
James Smartea2151b2008-09-07 11:52:10 -04005264
5265 scsi_event.event_type = FC_REG_SCSI_EVENT;
5266 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
5267 scsi_event.lun = 0;
5268 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5269 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5270
James Smartbbb9d182009-06-10 17:23:16 -04005271 fc_host_post_vendor_event(shost, fc_get_event_number(),
5272 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -04005273
James Smartbbb9d182009-06-10 17:23:16 -04005274 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
5275 FCP_TARGET_RESET);
dea31012005-04-17 16:05:31 -05005276
James Smarte8b62012007-08-02 11:10:09 -04005277 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005278 "0723 SCSI layer issued Target Reset (%d, %llu) "
James Smartbbb9d182009-06-10 17:23:16 -04005279 "return x%x\n", tgt_id, lun_id, status);
5280
5281 /*
5282 * We have to clean up i/o as : they may be orphaned by the TMF;
5283 * or if the TMF failed, they may be in an indeterminate state.
5284 * So, continue on.
5285 * We will report success if all the i/o aborts successfully.
5286 */
James Smart53151bb2013-10-10 12:24:07 -04005287 if (status == SUCCESS)
5288 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
James Smart3a707302012-06-12 13:54:42 -04005289 LPFC_CTX_TGT);
James Smart53151bb2013-10-10 12:24:07 -04005290 return status;
dea31012005-04-17 16:05:31 -05005291}
5292
James Smart9bad7672008-12-04 22:39:02 -05005293/**
James Smart3621a712009-04-06 18:47:14 -04005294 * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05005295 * @cmnd: Pointer to scsi_cmnd data structure.
5296 *
James Smartbbb9d182009-06-10 17:23:16 -04005297 * This routine does target reset to all targets on @cmnd->device->host.
5298 * This emulates Parallel SCSI Bus Reset Semantics.
James Smart9bad7672008-12-04 22:39:02 -05005299 *
James Smartbbb9d182009-06-10 17:23:16 -04005300 * Return code :
5301 * 0x2003 - Error
5302 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05005303 **/
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04005304static int
James Smart7054a602007-04-25 09:52:34 -04005305lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05005306{
James Smart2e0fef82007-06-17 19:56:36 -05005307 struct Scsi_Host *shost = cmnd->device->host;
5308 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05005309 struct lpfc_nodelist *ndlp = NULL;
James Smartea2151b2008-09-07 11:52:10 -04005310 struct lpfc_scsi_event_header scsi_event;
James Smartbbb9d182009-06-10 17:23:16 -04005311 int match;
5312 int ret = SUCCESS, status, i;
James Smartea2151b2008-09-07 11:52:10 -04005313
5314 scsi_event.event_type = FC_REG_SCSI_EVENT;
5315 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
5316 scsi_event.lun = 0;
5317 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
5318 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
5319
James Smartbbb9d182009-06-10 17:23:16 -04005320 fc_host_post_vendor_event(shost, fc_get_event_number(),
5321 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
dea31012005-04-17 16:05:31 -05005322
James Smartbf086112011-08-21 21:48:13 -04005323 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005324 if (status != 0 && status != SUCCESS)
James Smartbf086112011-08-21 21:48:13 -04005325 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005326
dea31012005-04-17 16:05:31 -05005327 /*
5328 * Since the driver manages a single bus device, reset all
5329 * targets known to the driver. Should any target reset
5330 * fail, this routine returns failure to the midlayer.
5331 */
James Smarte17da182006-07-06 15:49:25 -04005332 for (i = 0; i < LPFC_MAX_TARGET; i++) {
James Smart685f0bf2007-04-25 09:53:08 -04005333 /* Search for mapped node by target ID */
dea31012005-04-17 16:05:31 -05005334 match = 0;
James Smart2e0fef82007-06-17 19:56:36 -05005335 spin_lock_irq(shost->host_lock);
5336 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05005337 if (!NLP_CHK_NODE_ACT(ndlp))
5338 continue;
James Smarta6571c62012-10-31 14:44:42 -04005339 if (vport->phba->cfg_fcp2_no_tgt_reset &&
5340 (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE))
5341 continue;
James Smart685f0bf2007-04-25 09:53:08 -04005342 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
James Smart915caaa2008-06-14 22:52:38 -04005343 ndlp->nlp_sid == i &&
James Smart685f0bf2007-04-25 09:53:08 -04005344 ndlp->rport) {
dea31012005-04-17 16:05:31 -05005345 match = 1;
5346 break;
5347 }
5348 }
James Smart2e0fef82007-06-17 19:56:36 -05005349 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005350 if (!match)
5351 continue;
James Smartbbb9d182009-06-10 17:23:16 -04005352
5353 status = lpfc_send_taskmgmt(vport, ndlp->rport->dd_data,
5354 i, 0, FCP_TARGET_RESET);
5355
5356 if (status != SUCCESS) {
James Smarte8b62012007-08-02 11:10:09 -04005357 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5358 "0700 Bus Reset on target %d failed\n",
5359 i);
James Smart915caaa2008-06-14 22:52:38 -04005360 ret = FAILED;
dea31012005-04-17 16:05:31 -05005361 }
5362 }
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005363 /*
James Smartbbb9d182009-06-10 17:23:16 -04005364 * We have to clean up i/o as : they may be orphaned by the TMFs
5365 * above; or if any of the TMFs failed, they may be in an
5366 * indeterminate state.
5367 * We will report success if all the i/o aborts successfully.
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005368 */
James Smartbbb9d182009-06-10 17:23:16 -04005369
5370 status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
5371 if (status != SUCCESS)
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005372 ret = FAILED;
James Smartbbb9d182009-06-10 17:23:16 -04005373
James Smarte8b62012007-08-02 11:10:09 -04005374 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5375 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea31012005-04-17 16:05:31 -05005376 return ret;
5377}
5378
James Smart9bad7672008-12-04 22:39:02 -05005379/**
James Smart27b01b82012-05-09 21:19:44 -04005380 * lpfc_host_reset_handler - scsi_host_template eh_host_reset_handler entry pt
5381 * @cmnd: Pointer to scsi_cmnd data structure.
5382 *
5383 * This routine does host reset to the adaptor port. It brings the HBA
5384 * offline, performs a board restart, and then brings the board back online.
5385 * The lpfc_offline calls lpfc_sli_hba_down which will abort and local
5386 * reject all outstanding SCSI commands to the host and error returned
5387 * back to SCSI mid-level. As this will be SCSI mid-level's last resort
5388 * of error handling, it will only return error if resetting of the adapter
5389 * is not successful; in all other cases, will return success.
5390 *
5391 * Return code :
5392 * 0x2003 - Error
5393 * 0x2002 - Success
5394 **/
5395static int
5396lpfc_host_reset_handler(struct scsi_cmnd *cmnd)
5397{
5398 struct Scsi_Host *shost = cmnd->device->host;
5399 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5400 struct lpfc_hba *phba = vport->phba;
5401 int rc, ret = SUCCESS;
5402
James Smarta88dbb62013-04-17 20:18:39 -04005403 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5404 "3172 SCSI layer issued Host Reset Data:\n");
5405
James Smart618a5232012-06-12 13:54:36 -04005406 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart27b01b82012-05-09 21:19:44 -04005407 lpfc_offline(phba);
5408 rc = lpfc_sli_brdrestart(phba);
5409 if (rc)
5410 ret = FAILED;
James Smarta88dbb62013-04-17 20:18:39 -04005411 rc = lpfc_online(phba);
5412 if (rc)
5413 ret = FAILED;
James Smart27b01b82012-05-09 21:19:44 -04005414 lpfc_unblock_mgmt_io(phba);
5415
James Smarta88dbb62013-04-17 20:18:39 -04005416 if (ret == FAILED) {
5417 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5418 "3323 Failed host reset, bring it offline\n");
5419 lpfc_sli4_offline_eratt(phba);
5420 }
James Smart27b01b82012-05-09 21:19:44 -04005421 return ret;
5422}
5423
5424/**
James Smart3621a712009-04-06 18:47:14 -04005425 * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
James Smart9bad7672008-12-04 22:39:02 -05005426 * @sdev: Pointer to scsi_device.
5427 *
5428 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
5429 * globally available list of scsi buffers. This routine also makes sure scsi
5430 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
5431 * of scsi buffer exists for the lifetime of the driver.
5432 *
5433 * Return codes:
5434 * non-0 - Error
5435 * 0 - Success
5436 **/
dea31012005-04-17 16:05:31 -05005437static int
dea31012005-04-17 16:05:31 -05005438lpfc_slave_alloc(struct scsi_device *sdev)
5439{
James Smart2e0fef82007-06-17 19:56:36 -05005440 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5441 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005442 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
James Smart3772a992009-05-22 14:50:54 -04005443 uint32_t total = 0;
dea31012005-04-17 16:05:31 -05005444 uint32_t num_to_alloc = 0;
James Smart3772a992009-05-22 14:50:54 -04005445 int num_allocated = 0;
James Smartd7c47992010-06-08 18:31:54 -04005446 uint32_t sdev_cnt;
James Smart1ba981f2014-02-20 09:56:45 -05005447 struct lpfc_device_data *device_data;
5448 unsigned long flags;
5449 struct lpfc_name target_wwpn;
dea31012005-04-17 16:05:31 -05005450
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005451 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05005452 return -ENXIO;
5453
James Smartf38fa0b2014-04-04 13:52:21 -04005454 if (phba->cfg_fof) {
James Smart1ba981f2014-02-20 09:56:45 -05005455
5456 /*
5457 * Check to see if the device data structure for the lun
5458 * exists. If not, create one.
5459 */
5460
5461 u64_to_wwn(rport->port_name, target_wwpn.u.wwn);
5462 spin_lock_irqsave(&phba->devicelock, flags);
5463 device_data = __lpfc_get_device_data(phba,
5464 &phba->luns,
5465 &vport->fc_portname,
5466 &target_wwpn,
5467 sdev->lun);
5468 if (!device_data) {
5469 spin_unlock_irqrestore(&phba->devicelock, flags);
5470 device_data = lpfc_create_device_data(phba,
5471 &vport->fc_portname,
5472 &target_wwpn,
5473 sdev->lun, true);
5474 if (!device_data)
5475 return -ENOMEM;
5476 spin_lock_irqsave(&phba->devicelock, flags);
5477 list_add_tail(&device_data->listentry, &phba->luns);
5478 }
5479 device_data->rport_data = rport->dd_data;
5480 device_data->available = true;
5481 spin_unlock_irqrestore(&phba->devicelock, flags);
5482 sdev->hostdata = device_data;
5483 } else {
5484 sdev->hostdata = rport->dd_data;
5485 }
James Smartd7c47992010-06-08 18:31:54 -04005486 sdev_cnt = atomic_inc_return(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05005487
5488 /*
5489 * Populate the cmds_per_lun count scsi_bufs into this host's globally
5490 * available list of scsi buffers. Don't allocate more than the
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04005491 * HBA limit conveyed to the midlayer via the host structure. The
5492 * formula accounts for the lun_queue_depth + error handlers + 1
5493 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea31012005-04-17 16:05:31 -05005494 */
5495 total = phba->total_scsi_bufs;
James Smart3de2a652007-08-02 11:09:59 -04005496 num_to_alloc = vport->cfg_lun_queue_depth + 2;
James Smart92d7f7b2007-06-17 19:56:38 -05005497
James Smartd7c47992010-06-08 18:31:54 -04005498 /* If allocated buffers are enough do nothing */
5499 if ((sdev_cnt * (vport->cfg_lun_queue_depth + 2)) < total)
5500 return 0;
5501
James Smart92d7f7b2007-06-17 19:56:38 -05005502 /* Allow some exchanges to be available always to complete discovery */
5503 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04005504 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5505 "0704 At limitation of %d preallocated "
5506 "command buffers\n", total);
dea31012005-04-17 16:05:31 -05005507 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05005508 /* Allow some exchanges to be available always to complete discovery */
5509 } else if (total + num_to_alloc >
5510 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04005511 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5512 "0705 Allocation request of %d "
5513 "command buffers will exceed max of %d. "
5514 "Reducing allocation request to %d.\n",
5515 num_to_alloc, phba->cfg_hba_queue_depth,
5516 (phba->cfg_hba_queue_depth - total));
dea31012005-04-17 16:05:31 -05005517 num_to_alloc = phba->cfg_hba_queue_depth - total;
5518 }
James Smart3772a992009-05-22 14:50:54 -04005519 num_allocated = lpfc_new_scsi_buf(vport, num_to_alloc);
5520 if (num_to_alloc != num_allocated) {
James Smart96f70772013-04-17 20:16:15 -04005521 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5522 "0708 Allocation request of %d "
5523 "command buffers did not succeed. "
5524 "Allocated %d buffers.\n",
5525 num_to_alloc, num_allocated);
dea31012005-04-17 16:05:31 -05005526 }
James Smart1c6f4ef52009-11-18 15:40:49 -05005527 if (num_allocated > 0)
5528 phba->total_scsi_bufs += num_allocated;
dea31012005-04-17 16:05:31 -05005529 return 0;
5530}
5531
James Smart9bad7672008-12-04 22:39:02 -05005532/**
James Smart3621a712009-04-06 18:47:14 -04005533 * lpfc_slave_configure - scsi_host_template slave_configure entry point
James Smart9bad7672008-12-04 22:39:02 -05005534 * @sdev: Pointer to scsi_device.
5535 *
5536 * This routine configures following items
5537 * - Tag command queuing support for @sdev if supported.
James Smart9bad7672008-12-04 22:39:02 -05005538 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
5539 *
5540 * Return codes:
5541 * 0 - Success
5542 **/
dea31012005-04-17 16:05:31 -05005543static int
5544lpfc_slave_configure(struct scsi_device *sdev)
5545{
James Smart2e0fef82007-06-17 19:56:36 -05005546 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5547 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05005548
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01005549 scsi_change_queue_depth(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05005550
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005551 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04005552 lpfc_sli_handle_fast_ring_event(phba,
5553 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005554 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
5555 lpfc_poll_rearm_timer(phba);
5556 }
5557
dea31012005-04-17 16:05:31 -05005558 return 0;
5559}
5560
James Smart9bad7672008-12-04 22:39:02 -05005561/**
James Smart3621a712009-04-06 18:47:14 -04005562 * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
James Smart9bad7672008-12-04 22:39:02 -05005563 * @sdev: Pointer to scsi_device.
5564 *
5565 * This routine sets @sdev hostatdata filed to null.
5566 **/
dea31012005-04-17 16:05:31 -05005567static void
5568lpfc_slave_destroy(struct scsi_device *sdev)
5569{
James Smartd7c47992010-06-08 18:31:54 -04005570 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5571 struct lpfc_hba *phba = vport->phba;
James Smart1ba981f2014-02-20 09:56:45 -05005572 unsigned long flags;
5573 struct lpfc_device_data *device_data = sdev->hostdata;
5574
James Smartd7c47992010-06-08 18:31:54 -04005575 atomic_dec(&phba->sdev_cnt);
James Smartf38fa0b2014-04-04 13:52:21 -04005576 if ((phba->cfg_fof) && (device_data)) {
James Smart1ba981f2014-02-20 09:56:45 -05005577 spin_lock_irqsave(&phba->devicelock, flags);
5578 device_data->available = false;
5579 if (!device_data->oas_enabled)
5580 lpfc_delete_device_data(phba, device_data);
5581 spin_unlock_irqrestore(&phba->devicelock, flags);
5582 }
dea31012005-04-17 16:05:31 -05005583 sdev->hostdata = NULL;
5584 return;
5585}
5586
James Smart1ba981f2014-02-20 09:56:45 -05005587/**
5588 * lpfc_create_device_data - creates and initializes device data structure for OAS
5589 * @pha: Pointer to host bus adapter structure.
5590 * @vport_wwpn: Pointer to vport's wwpn information
5591 * @target_wwpn: Pointer to target's wwpn information
5592 * @lun: Lun on target
5593 * @atomic_create: Flag to indicate if memory should be allocated using the
5594 * GFP_ATOMIC flag or not.
5595 *
5596 * This routine creates a device data structure which will contain identifying
5597 * information for the device (host wwpn, target wwpn, lun), state of OAS,
5598 * whether or not the corresponding lun is available by the system,
5599 * and pointer to the rport data.
5600 *
5601 * Return codes:
5602 * NULL - Error
5603 * Pointer to lpfc_device_data - Success
5604 **/
5605struct lpfc_device_data*
5606lpfc_create_device_data(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
5607 struct lpfc_name *target_wwpn, uint64_t lun,
5608 bool atomic_create)
5609{
5610
5611 struct lpfc_device_data *lun_info;
5612 int memory_flags;
5613
5614 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
James Smartf38fa0b2014-04-04 13:52:21 -04005615 !(phba->cfg_fof))
James Smart1ba981f2014-02-20 09:56:45 -05005616 return NULL;
5617
5618 /* Attempt to create the device data to contain lun info */
5619
5620 if (atomic_create)
5621 memory_flags = GFP_ATOMIC;
5622 else
5623 memory_flags = GFP_KERNEL;
5624 lun_info = mempool_alloc(phba->device_data_mem_pool, memory_flags);
5625 if (!lun_info)
5626 return NULL;
5627 INIT_LIST_HEAD(&lun_info->listentry);
5628 lun_info->rport_data = NULL;
5629 memcpy(&lun_info->device_id.vport_wwpn, vport_wwpn,
5630 sizeof(struct lpfc_name));
5631 memcpy(&lun_info->device_id.target_wwpn, target_wwpn,
5632 sizeof(struct lpfc_name));
5633 lun_info->device_id.lun = lun;
5634 lun_info->oas_enabled = false;
5635 lun_info->available = false;
5636 return lun_info;
5637}
5638
5639/**
5640 * lpfc_delete_device_data - frees a device data structure for OAS
5641 * @pha: Pointer to host bus adapter structure.
5642 * @lun_info: Pointer to device data structure to free.
5643 *
5644 * This routine frees the previously allocated device data structure passed.
5645 *
5646 **/
5647void
5648lpfc_delete_device_data(struct lpfc_hba *phba,
5649 struct lpfc_device_data *lun_info)
5650{
5651
5652 if (unlikely(!phba) || !lun_info ||
James Smartf38fa0b2014-04-04 13:52:21 -04005653 !(phba->cfg_fof))
James Smart1ba981f2014-02-20 09:56:45 -05005654 return;
5655
5656 if (!list_empty(&lun_info->listentry))
5657 list_del(&lun_info->listentry);
5658 mempool_free(lun_info, phba->device_data_mem_pool);
5659 return;
5660}
5661
5662/**
5663 * __lpfc_get_device_data - returns the device data for the specified lun
5664 * @pha: Pointer to host bus adapter structure.
5665 * @list: Point to list to search.
5666 * @vport_wwpn: Pointer to vport's wwpn information
5667 * @target_wwpn: Pointer to target's wwpn information
5668 * @lun: Lun on target
5669 *
5670 * This routine searches the list passed for the specified lun's device data.
5671 * This function does not hold locks, it is the responsibility of the caller
5672 * to ensure the proper lock is held before calling the function.
5673 *
5674 * Return codes:
5675 * NULL - Error
5676 * Pointer to lpfc_device_data - Success
5677 **/
5678struct lpfc_device_data*
5679__lpfc_get_device_data(struct lpfc_hba *phba, struct list_head *list,
5680 struct lpfc_name *vport_wwpn,
5681 struct lpfc_name *target_wwpn, uint64_t lun)
5682{
5683
5684 struct lpfc_device_data *lun_info;
5685
5686 if (unlikely(!phba) || !list || !vport_wwpn || !target_wwpn ||
James Smartf38fa0b2014-04-04 13:52:21 -04005687 !phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005688 return NULL;
5689
5690 /* Check to see if the lun is already enabled for OAS. */
5691
5692 list_for_each_entry(lun_info, list, listentry) {
5693 if ((memcmp(&lun_info->device_id.vport_wwpn, vport_wwpn,
5694 sizeof(struct lpfc_name)) == 0) &&
5695 (memcmp(&lun_info->device_id.target_wwpn, target_wwpn,
5696 sizeof(struct lpfc_name)) == 0) &&
5697 (lun_info->device_id.lun == lun))
5698 return lun_info;
5699 }
5700
5701 return NULL;
5702}
5703
5704/**
5705 * lpfc_find_next_oas_lun - searches for the next oas lun
5706 * @pha: Pointer to host bus adapter structure.
5707 * @vport_wwpn: Pointer to vport's wwpn information
5708 * @target_wwpn: Pointer to target's wwpn information
5709 * @starting_lun: Pointer to the lun to start searching for
5710 * @found_vport_wwpn: Pointer to the found lun's vport wwpn information
5711 * @found_target_wwpn: Pointer to the found lun's target wwpn information
5712 * @found_lun: Pointer to the found lun.
5713 * @found_lun_status: Pointer to status of the found lun.
5714 *
5715 * This routine searches the luns list for the specified lun
5716 * or the first lun for the vport/target. If the vport wwpn contains
5717 * a zero value then a specific vport is not specified. In this case
5718 * any vport which contains the lun will be considered a match. If the
5719 * target wwpn contains a zero value then a specific target is not specified.
5720 * In this case any target which contains the lun will be considered a
5721 * match. If the lun is found, the lun, vport wwpn, target wwpn and lun status
5722 * are returned. The function will also return the next lun if available.
5723 * If the next lun is not found, starting_lun parameter will be set to
5724 * NO_MORE_OAS_LUN.
5725 *
5726 * Return codes:
5727 * non-0 - Error
5728 * 0 - Success
5729 **/
5730bool
5731lpfc_find_next_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
5732 struct lpfc_name *target_wwpn, uint64_t *starting_lun,
5733 struct lpfc_name *found_vport_wwpn,
5734 struct lpfc_name *found_target_wwpn,
5735 uint64_t *found_lun,
5736 uint32_t *found_lun_status)
5737{
5738
5739 unsigned long flags;
5740 struct lpfc_device_data *lun_info;
5741 struct lpfc_device_id *device_id;
5742 uint64_t lun;
5743 bool found = false;
5744
5745 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
5746 !starting_lun || !found_vport_wwpn ||
5747 !found_target_wwpn || !found_lun || !found_lun_status ||
5748 (*starting_lun == NO_MORE_OAS_LUN) ||
James Smartf38fa0b2014-04-04 13:52:21 -04005749 !phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005750 return false;
5751
5752 lun = *starting_lun;
5753 *found_lun = NO_MORE_OAS_LUN;
5754 *starting_lun = NO_MORE_OAS_LUN;
5755
5756 /* Search for lun or the lun closet in value */
5757
5758 spin_lock_irqsave(&phba->devicelock, flags);
5759 list_for_each_entry(lun_info, &phba->luns, listentry) {
5760 if (((wwn_to_u64(vport_wwpn->u.wwn) == 0) ||
5761 (memcmp(&lun_info->device_id.vport_wwpn, vport_wwpn,
5762 sizeof(struct lpfc_name)) == 0)) &&
5763 ((wwn_to_u64(target_wwpn->u.wwn) == 0) ||
5764 (memcmp(&lun_info->device_id.target_wwpn, target_wwpn,
5765 sizeof(struct lpfc_name)) == 0)) &&
5766 (lun_info->oas_enabled)) {
5767 device_id = &lun_info->device_id;
5768 if ((!found) &&
5769 ((lun == FIND_FIRST_OAS_LUN) ||
5770 (device_id->lun == lun))) {
5771 *found_lun = device_id->lun;
5772 memcpy(found_vport_wwpn,
5773 &device_id->vport_wwpn,
5774 sizeof(struct lpfc_name));
5775 memcpy(found_target_wwpn,
5776 &device_id->target_wwpn,
5777 sizeof(struct lpfc_name));
5778 if (lun_info->available)
5779 *found_lun_status =
5780 OAS_LUN_STATUS_EXISTS;
5781 else
5782 *found_lun_status = 0;
5783 if (phba->cfg_oas_flags & OAS_FIND_ANY_VPORT)
5784 memset(vport_wwpn, 0x0,
5785 sizeof(struct lpfc_name));
5786 if (phba->cfg_oas_flags & OAS_FIND_ANY_TARGET)
5787 memset(target_wwpn, 0x0,
5788 sizeof(struct lpfc_name));
5789 found = true;
5790 } else if (found) {
5791 *starting_lun = device_id->lun;
5792 memcpy(vport_wwpn, &device_id->vport_wwpn,
5793 sizeof(struct lpfc_name));
5794 memcpy(target_wwpn, &device_id->target_wwpn,
5795 sizeof(struct lpfc_name));
5796 break;
5797 }
5798 }
5799 }
5800 spin_unlock_irqrestore(&phba->devicelock, flags);
5801 return found;
5802}
5803
5804/**
5805 * lpfc_enable_oas_lun - enables a lun for OAS operations
5806 * @pha: Pointer to host bus adapter structure.
5807 * @vport_wwpn: Pointer to vport's wwpn information
5808 * @target_wwpn: Pointer to target's wwpn information
5809 * @lun: Lun
5810 *
5811 * This routine enables a lun for oas operations. The routines does so by
5812 * doing the following :
5813 *
5814 * 1) Checks to see if the device data for the lun has been created.
5815 * 2) If found, sets the OAS enabled flag if not set and returns.
5816 * 3) Otherwise, creates a device data structure.
5817 * 4) If successfully created, indicates the device data is for an OAS lun,
5818 * indicates the lun is not available and add to the list of luns.
5819 *
5820 * Return codes:
5821 * false - Error
5822 * true - Success
5823 **/
5824bool
5825lpfc_enable_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
5826 struct lpfc_name *target_wwpn, uint64_t lun)
5827{
5828
5829 struct lpfc_device_data *lun_info;
5830 unsigned long flags;
5831
5832 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
James Smartf38fa0b2014-04-04 13:52:21 -04005833 !phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005834 return false;
5835
5836 spin_lock_irqsave(&phba->devicelock, flags);
5837
5838 /* Check to see if the device data for the lun has been created */
5839 lun_info = __lpfc_get_device_data(phba, &phba->luns, vport_wwpn,
5840 target_wwpn, lun);
5841 if (lun_info) {
5842 if (!lun_info->oas_enabled)
5843 lun_info->oas_enabled = true;
5844 spin_unlock_irqrestore(&phba->devicelock, flags);
5845 return true;
5846 }
5847
5848 /* Create an lun info structure and add to list of luns */
5849 lun_info = lpfc_create_device_data(phba, vport_wwpn, target_wwpn, lun,
5850 false);
5851 if (lun_info) {
5852 lun_info->oas_enabled = true;
5853 lun_info->available = false;
5854 list_add_tail(&lun_info->listentry, &phba->luns);
5855 spin_unlock_irqrestore(&phba->devicelock, flags);
5856 return true;
5857 }
5858 spin_unlock_irqrestore(&phba->devicelock, flags);
5859 return false;
5860}
5861
5862/**
5863 * lpfc_disable_oas_lun - disables a lun for OAS operations
5864 * @pha: Pointer to host bus adapter structure.
5865 * @vport_wwpn: Pointer to vport's wwpn information
5866 * @target_wwpn: Pointer to target's wwpn information
5867 * @lun: Lun
5868 *
5869 * This routine disables a lun for oas operations. The routines does so by
5870 * doing the following :
5871 *
5872 * 1) Checks to see if the device data for the lun is created.
5873 * 2) If present, clears the flag indicating this lun is for OAS.
5874 * 3) If the lun is not available by the system, the device data is
5875 * freed.
5876 *
5877 * Return codes:
5878 * false - Error
5879 * true - Success
5880 **/
5881bool
5882lpfc_disable_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
5883 struct lpfc_name *target_wwpn, uint64_t lun)
5884{
5885
5886 struct lpfc_device_data *lun_info;
5887 unsigned long flags;
5888
5889 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
James Smartf38fa0b2014-04-04 13:52:21 -04005890 !phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005891 return false;
5892
5893 spin_lock_irqsave(&phba->devicelock, flags);
5894
5895 /* Check to see if the lun is available. */
5896 lun_info = __lpfc_get_device_data(phba,
5897 &phba->luns, vport_wwpn,
5898 target_wwpn, lun);
5899 if (lun_info) {
5900 lun_info->oas_enabled = false;
5901 if (!lun_info->available)
5902 lpfc_delete_device_data(phba, lun_info);
5903 spin_unlock_irqrestore(&phba->devicelock, flags);
5904 return true;
5905 }
5906
5907 spin_unlock_irqrestore(&phba->devicelock, flags);
5908 return false;
5909}
James Smart92d7f7b2007-06-17 19:56:38 -05005910
James Smartea4142f2015-04-07 15:07:13 -04005911struct scsi_host_template lpfc_template_s3 = {
5912 .module = THIS_MODULE,
5913 .name = LPFC_DRIVER_NAME,
5914 .info = lpfc_info,
5915 .queuecommand = lpfc_queuecommand,
5916 .eh_abort_handler = lpfc_abort_handler,
5917 .eh_device_reset_handler = lpfc_device_reset_handler,
5918 .eh_target_reset_handler = lpfc_target_reset_handler,
5919 .eh_bus_reset_handler = lpfc_bus_reset_handler,
5920 .slave_alloc = lpfc_slave_alloc,
5921 .slave_configure = lpfc_slave_configure,
5922 .slave_destroy = lpfc_slave_destroy,
5923 .scan_finished = lpfc_scan_finished,
5924 .this_id = -1,
5925 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
5926 .cmd_per_lun = LPFC_CMD_PER_LUN,
5927 .use_clustering = ENABLE_CLUSTERING,
5928 .shost_attrs = lpfc_hba_attrs,
5929 .max_sectors = 0xFFFF,
5930 .vendor_id = LPFC_NL_VENDOR_ID,
5931 .change_queue_depth = scsi_change_queue_depth,
James Smartea4142f2015-04-07 15:07:13 -04005932 .track_queue_depth = 1,
5933};
5934
dea31012005-04-17 16:05:31 -05005935struct scsi_host_template lpfc_template = {
5936 .module = THIS_MODULE,
5937 .name = LPFC_DRIVER_NAME,
5938 .info = lpfc_info,
5939 .queuecommand = lpfc_queuecommand,
5940 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04005941 .eh_device_reset_handler = lpfc_device_reset_handler,
5942 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart7054a602007-04-25 09:52:34 -04005943 .eh_bus_reset_handler = lpfc_bus_reset_handler,
James Smart27b01b82012-05-09 21:19:44 -04005944 .eh_host_reset_handler = lpfc_host_reset_handler,
dea31012005-04-17 16:05:31 -05005945 .slave_alloc = lpfc_slave_alloc,
5946 .slave_configure = lpfc_slave_configure,
5947 .slave_destroy = lpfc_slave_destroy,
James Smart47a86172007-04-25 09:53:22 -04005948 .scan_finished = lpfc_scan_finished,
dea31012005-04-17 16:05:31 -05005949 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05005950 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
dea31012005-04-17 16:05:31 -05005951 .cmd_per_lun = LPFC_CMD_PER_LUN,
5952 .use_clustering = ENABLE_CLUSTERING,
James Smart2e0fef82007-06-17 19:56:36 -05005953 .shost_attrs = lpfc_hba_attrs,
James.Smart@Emulex.Com564b2962005-06-25 10:34:17 -04005954 .max_sectors = 0xFFFF,
James Smartf1c3b0f2009-07-19 10:01:32 -04005955 .vendor_id = LPFC_NL_VENDOR_ID,
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01005956 .change_queue_depth = scsi_change_queue_depth,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01005957 .track_queue_depth = 1,
dea31012005-04-17 16:05:31 -05005958};
James Smart3de2a652007-08-02 11:09:59 -04005959
5960struct scsi_host_template lpfc_vport_template = {
5961 .module = THIS_MODULE,
5962 .name = LPFC_DRIVER_NAME,
5963 .info = lpfc_info,
5964 .queuecommand = lpfc_queuecommand,
5965 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04005966 .eh_device_reset_handler = lpfc_device_reset_handler,
5967 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart3de2a652007-08-02 11:09:59 -04005968 .eh_bus_reset_handler = lpfc_bus_reset_handler,
5969 .slave_alloc = lpfc_slave_alloc,
5970 .slave_configure = lpfc_slave_configure,
5971 .slave_destroy = lpfc_slave_destroy,
5972 .scan_finished = lpfc_scan_finished,
5973 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05005974 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
James Smart3de2a652007-08-02 11:09:59 -04005975 .cmd_per_lun = LPFC_CMD_PER_LUN,
5976 .use_clustering = ENABLE_CLUSTERING,
5977 .shost_attrs = lpfc_vport_attrs,
5978 .max_sectors = 0xFFFF,
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01005979 .change_queue_depth = scsi_change_queue_depth,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01005980 .track_queue_depth = 1,
James Smart3de2a652007-08-02 11:09:59 -04005981};