blob: 44995de74a62af8af25360f42b32ed3bb00a2050 [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 Smartacd68592012-01-18 16:25:09 -05004 * Copyright (C) 2004-2012 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 Smarta6887e22013-04-17 20:18:07 -040071#if !defined(SCSI_PROT_GUARD_CHECK) || !defined(SCSI_PROT_REF_CHECK)
72#define scsi_prot_flagged(sc, flg) sc
73#endif
74
James Smartda0436e2009-05-22 14:51:39 -040075static void
76lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
James Smart1c6f4ef52009-11-18 15:40:49 -050077static void
78lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
James Smarte2a0a9d2008-12-04 22:40:02 -050079
80static void
James Smart6a9c52c2009-10-02 15:16:51 -040081lpfc_debug_save_data(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
James Smarte2a0a9d2008-12-04 22:40:02 -050082{
83 void *src, *dst;
84 struct scatterlist *sgde = scsi_sglist(cmnd);
85
86 if (!_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -040087 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
88 "9050 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
James Smarte2a0a9d2008-12-04 22:40:02 -050089 __func__);
90 return;
91 }
92
93
94 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -040095 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
96 "9051 BLKGRD: ERROR: data scatterlist is null\n");
James Smarte2a0a9d2008-12-04 22:40:02 -050097 return;
98 }
99
100 dst = (void *) _dump_buf_data;
101 while (sgde) {
102 src = sg_virt(sgde);
103 memcpy(dst, src, sgde->length);
104 dst += sgde->length;
105 sgde = sg_next(sgde);
106 }
107}
108
109static void
James Smart6a9c52c2009-10-02 15:16:51 -0400110lpfc_debug_save_dif(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
James Smarte2a0a9d2008-12-04 22:40:02 -0500111{
112 void *src, *dst;
113 struct scatterlist *sgde = scsi_prot_sglist(cmnd);
114
115 if (!_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -0400116 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
117 "9052 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
James Smarte2a0a9d2008-12-04 22:40:02 -0500118 __func__);
119 return;
120 }
121
122 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -0400123 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
124 "9053 BLKGRD: ERROR: prot scatterlist is null\n");
James Smarte2a0a9d2008-12-04 22:40:02 -0500125 return;
126 }
127
128 dst = _dump_buf_dif;
129 while (sgde) {
130 src = sg_virt(sgde);
131 memcpy(dst, src, sgde->length);
132 dst += sgde->length;
133 sgde = sg_next(sgde);
134 }
135}
136
James Smartea2151b2008-09-07 11:52:10 -0400137/**
James Smartf1126682009-06-10 17:22:44 -0400138 * lpfc_sli4_set_rsp_sgl_last - Set the last bit in the response sge.
139 * @phba: Pointer to HBA object.
140 * @lpfc_cmd: lpfc scsi command object pointer.
141 *
142 * This function is called from the lpfc_prep_task_mgmt_cmd function to
143 * set the last bit in the response sge entry.
144 **/
145static void
146lpfc_sli4_set_rsp_sgl_last(struct lpfc_hba *phba,
147 struct lpfc_scsi_buf *lpfc_cmd)
148{
149 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
150 if (sgl) {
151 sgl += 1;
152 sgl->word2 = le32_to_cpu(sgl->word2);
153 bf_set(lpfc_sli4_sge_last, sgl, 1);
154 sgl->word2 = cpu_to_le32(sgl->word2);
155 }
156}
157
158/**
James Smart3621a712009-04-06 18:47:14 -0400159 * lpfc_update_stats - Update statistical data for the command completion
James Smartea2151b2008-09-07 11:52:10 -0400160 * @phba: Pointer to HBA object.
161 * @lpfc_cmd: lpfc scsi command object pointer.
162 *
163 * This function is called when there is a command completion and this
164 * function updates the statistical data for the command completion.
165 **/
166static void
167lpfc_update_stats(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
168{
169 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
170 struct lpfc_nodelist *pnode = rdata->pnode;
171 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
172 unsigned long flags;
173 struct Scsi_Host *shost = cmd->device->host;
174 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
175 unsigned long latency;
176 int i;
177
178 if (cmd->result)
179 return;
180
James Smart9f1e1b52008-12-04 22:39:40 -0500181 latency = jiffies_to_msecs((long)jiffies - (long)lpfc_cmd->start_time);
182
James Smartea2151b2008-09-07 11:52:10 -0400183 spin_lock_irqsave(shost->host_lock, flags);
184 if (!vport->stat_data_enabled ||
185 vport->stat_data_blocked ||
James Smart5989b8d2010-10-22 11:06:56 -0400186 !pnode ||
James Smartea2151b2008-09-07 11:52:10 -0400187 !pnode->lat_data ||
188 (phba->bucket_type == LPFC_NO_BUCKET)) {
189 spin_unlock_irqrestore(shost->host_lock, flags);
190 return;
191 }
James Smartea2151b2008-09-07 11:52:10 -0400192
193 if (phba->bucket_type == LPFC_LINEAR_BUCKET) {
194 i = (latency + phba->bucket_step - 1 - phba->bucket_base)/
195 phba->bucket_step;
James Smart9f1e1b52008-12-04 22:39:40 -0500196 /* check array subscript bounds */
197 if (i < 0)
198 i = 0;
199 else if (i >= LPFC_MAX_BUCKET_COUNT)
200 i = LPFC_MAX_BUCKET_COUNT - 1;
James Smartea2151b2008-09-07 11:52:10 -0400201 } else {
202 for (i = 0; i < LPFC_MAX_BUCKET_COUNT-1; i++)
203 if (latency <= (phba->bucket_base +
204 ((1<<i)*phba->bucket_step)))
205 break;
206 }
207
208 pnode->lat_data[i].cmd_count++;
209 spin_unlock_irqrestore(shost->host_lock, flags);
210}
211
James Smartea2151b2008-09-07 11:52:10 -0400212/**
James Smart3621a712009-04-06 18:47:14 -0400213 * lpfc_send_sdev_queuedepth_change_event - Posts a queuedepth change event
James Smartea2151b2008-09-07 11:52:10 -0400214 * @phba: Pointer to HBA context object.
215 * @vport: Pointer to vport object.
216 * @ndlp: Pointer to FC node associated with the target.
217 * @lun: Lun number of the scsi device.
218 * @old_val: Old value of the queue depth.
219 * @new_val: New value of the queue depth.
220 *
221 * This function sends an event to the mgmt application indicating
222 * there is a change in the scsi device queue depth.
223 **/
224static void
225lpfc_send_sdev_queuedepth_change_event(struct lpfc_hba *phba,
226 struct lpfc_vport *vport,
227 struct lpfc_nodelist *ndlp,
228 uint32_t lun,
229 uint32_t old_val,
230 uint32_t new_val)
231{
232 struct lpfc_fast_path_event *fast_path_evt;
233 unsigned long flags;
234
235 fast_path_evt = lpfc_alloc_fast_evt(phba);
236 if (!fast_path_evt)
237 return;
238
239 fast_path_evt->un.queue_depth_evt.scsi_event.event_type =
240 FC_REG_SCSI_EVENT;
241 fast_path_evt->un.queue_depth_evt.scsi_event.subcategory =
242 LPFC_EVENT_VARQUEDEPTH;
243
244 /* Report all luns with change in queue depth */
245 fast_path_evt->un.queue_depth_evt.scsi_event.lun = lun;
246 if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
247 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwpn,
248 &ndlp->nlp_portname, sizeof(struct lpfc_name));
249 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwnn,
250 &ndlp->nlp_nodename, sizeof(struct lpfc_name));
251 }
252
253 fast_path_evt->un.queue_depth_evt.oldval = old_val;
254 fast_path_evt->un.queue_depth_evt.newval = new_val;
255 fast_path_evt->vport = vport;
256
257 fast_path_evt->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
258 spin_lock_irqsave(&phba->hbalock, flags);
259 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
260 spin_unlock_irqrestore(&phba->hbalock, flags);
261 lpfc_worker_wake_up(phba);
262
263 return;
264}
265
James Smart9bad7672008-12-04 22:39:02 -0500266/**
James Smart5ffc2662009-11-18 15:39:44 -0500267 * lpfc_change_queue_depth - Alter scsi device queue depth
268 * @sdev: Pointer the scsi device on which to change the queue depth.
269 * @qdepth: New queue depth to set the sdev to.
270 * @reason: The reason for the queue depth change.
271 *
272 * This function is called by the midlayer and the LLD to alter the queue
273 * depth for a scsi device. This function sets the queue depth to the new
274 * value and sends an event out to log the queue depth change.
275 **/
276int
277lpfc_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
278{
279 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
280 struct lpfc_hba *phba = vport->phba;
281 struct lpfc_rport_data *rdata;
282 unsigned long new_queue_depth, old_queue_depth;
283
284 old_queue_depth = sdev->queue_depth;
285 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
286 new_queue_depth = sdev->queue_depth;
287 rdata = sdev->hostdata;
288 if (rdata)
289 lpfc_send_sdev_queuedepth_change_event(phba, vport,
290 rdata->pnode, sdev->lun,
291 old_queue_depth,
292 new_queue_depth);
293 return sdev->queue_depth;
294}
295
296/**
James Smartfe8f7f92013-01-03 15:43:03 -0500297 * lpfc_change_queue_type() - Change a device's scsi tag queuing type
298 * @sdev: Pointer the scsi device whose queue depth is to change
299 * @tag_type: Identifier for queue tag type
300 */
301static int
302lpfc_change_queue_type(struct scsi_device *sdev, int tag_type)
303{
304 if (sdev->tagged_supported) {
305 scsi_set_tag_type(sdev, tag_type);
306 if (tag_type)
307 scsi_activate_tcq(sdev, sdev->queue_depth);
308 else
309 scsi_deactivate_tcq(sdev, sdev->queue_depth);
310 } else
311 tag_type = 0;
312
313 return tag_type;
314}
315
316/**
James Smart3621a712009-04-06 18:47:14 -0400317 * lpfc_rampdown_queue_depth - Post RAMP_DOWN_QUEUE event to worker thread
James Smart9bad7672008-12-04 22:39:02 -0500318 * @phba: The Hba for which this call is being executed.
319 *
320 * This routine is called when there is resource error in driver or firmware.
321 * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
322 * posts at most 1 event each second. This routine wakes up worker thread of
323 * @phba to process WORKER_RAM_DOWN_EVENT event.
324 *
325 * This routine should be called with no lock held.
326 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500327void
James Smarteaf15d52008-12-04 22:39:29 -0500328lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
James Smart92d7f7b2007-06-17 19:56:38 -0500329{
330 unsigned long flags;
James Smart5e9d9b82008-06-14 22:52:53 -0400331 uint32_t evt_posted;
James Smart92d7f7b2007-06-17 19:56:38 -0500332
333 spin_lock_irqsave(&phba->hbalock, flags);
334 atomic_inc(&phba->num_rsrc_err);
335 phba->last_rsrc_error_time = jiffies;
336
337 if ((phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL) > jiffies) {
338 spin_unlock_irqrestore(&phba->hbalock, flags);
339 return;
340 }
341
342 phba->last_ramp_down_time = jiffies;
343
344 spin_unlock_irqrestore(&phba->hbalock, flags);
345
346 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400347 evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
348 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500349 phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500350 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
351
James Smart5e9d9b82008-06-14 22:52:53 -0400352 if (!evt_posted)
353 lpfc_worker_wake_up(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500354 return;
355}
356
James Smart9bad7672008-12-04 22:39:02 -0500357/**
James Smart3621a712009-04-06 18:47:14 -0400358 * lpfc_rampup_queue_depth - Post RAMP_UP_QUEUE event for worker thread
James Smart9bad7672008-12-04 22:39:02 -0500359 * @phba: The Hba for which this call is being executed.
360 *
361 * This routine post WORKER_RAMP_UP_QUEUE event for @phba vport. This routine
362 * post at most 1 event every 5 minute after last_ramp_up_time or
363 * last_rsrc_error_time. This routine wakes up worker thread of @phba
364 * to process WORKER_RAM_DOWN_EVENT event.
365 *
366 * This routine should be called with no lock held.
367 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500368static inline void
James Smart3de2a652007-08-02 11:09:59 -0400369lpfc_rampup_queue_depth(struct lpfc_vport *vport,
James Smarta257bf92009-04-06 18:48:10 -0400370 uint32_t queue_depth)
James Smart92d7f7b2007-06-17 19:56:38 -0500371{
372 unsigned long flags;
James Smart3de2a652007-08-02 11:09:59 -0400373 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -0400374 uint32_t evt_posted;
James Smart92d7f7b2007-06-17 19:56:38 -0500375 atomic_inc(&phba->num_cmd_success);
376
James Smarta257bf92009-04-06 18:48:10 -0400377 if (vport->cfg_lun_queue_depth <= queue_depth)
James Smart92d7f7b2007-06-17 19:56:38 -0500378 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500379 spin_lock_irqsave(&phba->hbalock, flags);
James Smart5ffc2662009-11-18 15:39:44 -0500380 if (time_before(jiffies,
381 phba->last_ramp_up_time + QUEUE_RAMP_UP_INTERVAL) ||
382 time_before(jiffies,
383 phba->last_rsrc_error_time + QUEUE_RAMP_UP_INTERVAL)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500384 spin_unlock_irqrestore(&phba->hbalock, flags);
385 return;
386 }
James Smart92d7f7b2007-06-17 19:56:38 -0500387 phba->last_ramp_up_time = jiffies;
388 spin_unlock_irqrestore(&phba->hbalock, flags);
389
390 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400391 evt_posted = phba->pport->work_port_events & WORKER_RAMP_UP_QUEUE;
392 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500393 phba->pport->work_port_events |= WORKER_RAMP_UP_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500394 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
395
James Smart5e9d9b82008-06-14 22:52:53 -0400396 if (!evt_posted)
397 lpfc_worker_wake_up(phba);
398 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500399}
400
James Smart9bad7672008-12-04 22:39:02 -0500401/**
James Smart3621a712009-04-06 18:47:14 -0400402 * lpfc_ramp_down_queue_handler - WORKER_RAMP_DOWN_QUEUE event handler
James Smart9bad7672008-12-04 22:39:02 -0500403 * @phba: The Hba for which this call is being executed.
404 *
405 * This routine is called to process WORKER_RAMP_DOWN_QUEUE event for worker
406 * thread.This routine reduces queue depth for all scsi device on each vport
407 * associated with @phba.
408 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500409void
410lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
411{
James Smart549e55c2007-08-02 11:09:51 -0400412 struct lpfc_vport **vports;
413 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500414 struct scsi_device *sdev;
James Smart5ffc2662009-11-18 15:39:44 -0500415 unsigned long new_queue_depth;
James Smart92d7f7b2007-06-17 19:56:38 -0500416 unsigned long num_rsrc_err, num_cmd_success;
James Smart549e55c2007-08-02 11:09:51 -0400417 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500418
419 num_rsrc_err = atomic_read(&phba->num_rsrc_err);
420 num_cmd_success = atomic_read(&phba->num_cmd_success);
421
James Smart75ad83a2012-05-09 21:18:40 -0400422 /*
423 * The error and success command counters are global per
424 * driver instance. If another handler has already
425 * operated on this error event, just exit.
426 */
427 if (num_rsrc_err == 0)
428 return;
429
James Smart549e55c2007-08-02 11:09:51 -0400430 vports = lpfc_create_vport_work_array(phba);
431 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400432 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400433 shost = lpfc_shost_from_vport(vports[i]);
434 shost_for_each_device(sdev, shost) {
James Smart92d7f7b2007-06-17 19:56:38 -0500435 new_queue_depth =
James Smart549e55c2007-08-02 11:09:51 -0400436 sdev->queue_depth * num_rsrc_err /
437 (num_rsrc_err + num_cmd_success);
438 if (!new_queue_depth)
439 new_queue_depth = sdev->queue_depth - 1;
440 else
441 new_queue_depth = sdev->queue_depth -
442 new_queue_depth;
James Smart5ffc2662009-11-18 15:39:44 -0500443 lpfc_change_queue_depth(sdev, new_queue_depth,
444 SCSI_QDEPTH_DEFAULT);
James Smart549e55c2007-08-02 11:09:51 -0400445 }
James Smart92d7f7b2007-06-17 19:56:38 -0500446 }
James Smart09372822008-01-11 01:52:54 -0500447 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500448 atomic_set(&phba->num_rsrc_err, 0);
449 atomic_set(&phba->num_cmd_success, 0);
450}
451
James Smart9bad7672008-12-04 22:39:02 -0500452/**
James Smart3621a712009-04-06 18:47:14 -0400453 * lpfc_ramp_up_queue_handler - WORKER_RAMP_UP_QUEUE event handler
James Smart9bad7672008-12-04 22:39:02 -0500454 * @phba: The Hba for which this call is being executed.
455 *
456 * This routine is called to process WORKER_RAMP_UP_QUEUE event for worker
457 * thread.This routine increases queue depth for all scsi device on each vport
458 * associated with @phba by 1. This routine also sets @phba num_rsrc_err and
459 * num_cmd_success to zero.
460 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500461void
462lpfc_ramp_up_queue_handler(struct lpfc_hba *phba)
463{
James Smart549e55c2007-08-02 11:09:51 -0400464 struct lpfc_vport **vports;
465 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500466 struct scsi_device *sdev;
James Smart549e55c2007-08-02 11:09:51 -0400467 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500468
James Smart549e55c2007-08-02 11:09:51 -0400469 vports = lpfc_create_vport_work_array(phba);
470 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400471 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400472 shost = lpfc_shost_from_vport(vports[i]);
473 shost_for_each_device(sdev, shost) {
James Smart97eab632008-04-07 10:16:05 -0400474 if (vports[i]->cfg_lun_queue_depth <=
475 sdev->queue_depth)
476 continue;
James Smart5ffc2662009-11-18 15:39:44 -0500477 lpfc_change_queue_depth(sdev,
478 sdev->queue_depth+1,
479 SCSI_QDEPTH_RAMP_UP);
James Smart549e55c2007-08-02 11:09:51 -0400480 }
James Smart92d7f7b2007-06-17 19:56:38 -0500481 }
James Smart09372822008-01-11 01:52:54 -0500482 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500483 atomic_set(&phba->num_rsrc_err, 0);
484 atomic_set(&phba->num_cmd_success, 0);
485}
486
James Smarta8e497d2008-08-24 21:50:11 -0400487/**
James Smart3621a712009-04-06 18:47:14 -0400488 * lpfc_scsi_dev_block - set all scsi hosts to block state
James Smarta8e497d2008-08-24 21:50:11 -0400489 * @phba: Pointer to HBA context object.
490 *
491 * This function walks vport list and set each SCSI host to block state
492 * by invoking fc_remote_port_delete() routine. This function is invoked
493 * with EEH when device's PCI slot has been permanently disabled.
494 **/
495void
496lpfc_scsi_dev_block(struct lpfc_hba *phba)
497{
498 struct lpfc_vport **vports;
499 struct Scsi_Host *shost;
500 struct scsi_device *sdev;
501 struct fc_rport *rport;
502 int i;
503
504 vports = lpfc_create_vport_work_array(phba);
505 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400506 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8e497d2008-08-24 21:50:11 -0400507 shost = lpfc_shost_from_vport(vports[i]);
508 shost_for_each_device(sdev, shost) {
509 rport = starget_to_rport(scsi_target(sdev));
510 fc_remote_port_delete(rport);
511 }
512 }
513 lpfc_destroy_vport_work_array(phba, vports);
514}
515
James Smart9bad7672008-12-04 22:39:02 -0500516/**
James Smart3772a992009-05-22 14:50:54 -0400517 * lpfc_new_scsi_buf_s3 - Scsi buffer allocator for HBA with SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -0500518 * @vport: The virtual port for which this call being executed.
James Smart3772a992009-05-22 14:50:54 -0400519 * @num_to_allocate: The requested number of buffers to allocate.
James Smart9bad7672008-12-04 22:39:02 -0500520 *
James Smart3772a992009-05-22 14:50:54 -0400521 * This routine allocates a scsi buffer for device with SLI-3 interface spec,
522 * the scsi buffer contains all the necessary information needed to initiate
523 * a SCSI I/O. The non-DMAable buffer region contains information to build
524 * the IOCB. The DMAable region contains memory for the FCP CMND, FCP RSP,
525 * and the initial BPL. In addition to allocating memory, the FCP CMND and
526 * FCP RSP BDEs are setup in the BPL and the BPL BDE is setup in the IOCB.
James Smart9bad7672008-12-04 22:39:02 -0500527 *
528 * Return codes:
James Smart3772a992009-05-22 14:50:54 -0400529 * int - number of scsi buffers that were allocated.
530 * 0 = failure, less than num_to_alloc is a partial failure.
James Smart9bad7672008-12-04 22:39:02 -0500531 **/
James Smart3772a992009-05-22 14:50:54 -0400532static int
533lpfc_new_scsi_buf_s3(struct lpfc_vport *vport, int num_to_alloc)
dea31012005-04-17 16:05:31 -0500534{
James Smart2e0fef82007-06-17 19:56:36 -0500535 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500536 struct lpfc_scsi_buf *psb;
537 struct ulp_bde64 *bpl;
538 IOCB_t *iocb;
James Smart34b02dc2008-08-24 21:49:55 -0400539 dma_addr_t pdma_phys_fcp_cmd;
540 dma_addr_t pdma_phys_fcp_rsp;
541 dma_addr_t pdma_phys_bpl;
James Bottomley604a3e32005-10-29 10:28:33 -0500542 uint16_t iotag;
James Smart96f70772013-04-17 20:16:15 -0400543 int bcnt, bpl_size;
544
545 bpl_size = phba->cfg_sg_dma_buf_size -
546 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
547
548 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
549 "9067 ALLOC %d scsi_bufs: %d (%d + %d + %d)\n",
550 num_to_alloc, phba->cfg_sg_dma_buf_size,
551 (int)sizeof(struct fcp_cmnd),
552 (int)sizeof(struct fcp_rsp), bpl_size);
dea31012005-04-17 16:05:31 -0500553
James Smart3772a992009-05-22 14:50:54 -0400554 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
555 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
556 if (!psb)
557 break;
dea31012005-04-17 16:05:31 -0500558
James Smart3772a992009-05-22 14:50:54 -0400559 /*
560 * Get memory from the pci pool to map the virt space to pci
561 * bus space for an I/O. The DMA buffer includes space for the
562 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
563 * necessary to support the sg_tablesize.
564 */
565 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
566 GFP_KERNEL, &psb->dma_handle);
567 if (!psb->data) {
568 kfree(psb);
569 break;
570 }
dea31012005-04-17 16:05:31 -0500571
James Smart3772a992009-05-22 14:50:54 -0400572 /* Initialize virtual ptrs to dma_buf region. */
573 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
dea31012005-04-17 16:05:31 -0500574
James Smart3772a992009-05-22 14:50:54 -0400575 /* Allocate iotag for psb->cur_iocbq. */
576 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
577 if (iotag == 0) {
578 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
579 psb->data, psb->dma_handle);
580 kfree(psb);
581 break;
582 }
583 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Bottomley604a3e32005-10-29 10:28:33 -0500584
James Smart3772a992009-05-22 14:50:54 -0400585 psb->fcp_cmnd = psb->data;
586 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
587 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
James Smart34b02dc2008-08-24 21:49:55 -0400588 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500589
James Smart3772a992009-05-22 14:50:54 -0400590 /* Initialize local short-hand pointers. */
591 bpl = psb->fcp_bpl;
592 pdma_phys_fcp_cmd = psb->dma_handle;
593 pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
594 pdma_phys_bpl = psb->dma_handle + sizeof(struct fcp_cmnd) +
595 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500596
James Smart3772a992009-05-22 14:50:54 -0400597 /*
598 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
599 * are sg list bdes. Initialize the first two and leave the
600 * rest for queuecommand.
601 */
602 bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
603 bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
604 bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
605 bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
606 bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
dea31012005-04-17 16:05:31 -0500607
James Smart3772a992009-05-22 14:50:54 -0400608 /* Setup the physical region for the FCP RSP */
609 bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
610 bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
611 bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
612 bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
613 bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
614
615 /*
616 * Since the IOCB for the FCP I/O is built into this
617 * lpfc_scsi_buf, initialize it with all known data now.
618 */
619 iocb = &psb->cur_iocbq.iocb;
620 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
621 if ((phba->sli_rev == 3) &&
622 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
623 /* fill in immediate fcp command BDE */
624 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
625 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
626 iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
627 unsli3.fcp_ext.icd);
628 iocb->un.fcpi64.bdl.addrHigh = 0;
629 iocb->ulpBdeCount = 0;
630 iocb->ulpLe = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300631 /* fill in response BDE */
James Smart3772a992009-05-22 14:50:54 -0400632 iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags =
633 BUFF_TYPE_BDE_64;
634 iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
635 sizeof(struct fcp_rsp);
636 iocb->unsli3.fcp_ext.rbde.addrLow =
637 putPaddrLow(pdma_phys_fcp_rsp);
638 iocb->unsli3.fcp_ext.rbde.addrHigh =
639 putPaddrHigh(pdma_phys_fcp_rsp);
640 } else {
641 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
642 iocb->un.fcpi64.bdl.bdeSize =
643 (2 * sizeof(struct ulp_bde64));
644 iocb->un.fcpi64.bdl.addrLow =
645 putPaddrLow(pdma_phys_bpl);
646 iocb->un.fcpi64.bdl.addrHigh =
647 putPaddrHigh(pdma_phys_bpl);
648 iocb->ulpBdeCount = 1;
649 iocb->ulpLe = 1;
650 }
651 iocb->ulpClass = CLASS3;
652 psb->status = IOSTAT_SUCCESS;
James Smartda0436e2009-05-22 14:51:39 -0400653 /* Put it back into the SCSI buffer list */
James Smarteee88772010-09-29 11:19:08 -0400654 psb->cur_iocbq.context1 = psb;
James Smart1c6f4ef52009-11-18 15:40:49 -0500655 lpfc_release_scsi_buf_s3(phba, psb);
James Smart3772a992009-05-22 14:50:54 -0400656
James Smart34b02dc2008-08-24 21:49:55 -0400657 }
dea31012005-04-17 16:05:31 -0500658
James Smart3772a992009-05-22 14:50:54 -0400659 return bcnt;
dea31012005-04-17 16:05:31 -0500660}
661
James Smart9bad7672008-12-04 22:39:02 -0500662/**
James Smart1151e3e2011-02-16 12:39:35 -0500663 * lpfc_sli4_vport_delete_fcp_xri_aborted -Remove all ndlp references for vport
664 * @vport: pointer to lpfc vport data structure.
665 *
666 * This routine is invoked by the vport cleanup for deletions and the cleanup
667 * for an ndlp on removal.
668 **/
669void
670lpfc_sli4_vport_delete_fcp_xri_aborted(struct lpfc_vport *vport)
671{
672 struct lpfc_hba *phba = vport->phba;
673 struct lpfc_scsi_buf *psb, *next_psb;
674 unsigned long iflag = 0;
675
676 spin_lock_irqsave(&phba->hbalock, iflag);
677 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
678 list_for_each_entry_safe(psb, next_psb,
679 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
680 if (psb->rdata && psb->rdata->pnode
681 && psb->rdata->pnode->vport == vport)
682 psb->rdata = NULL;
683 }
684 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
685 spin_unlock_irqrestore(&phba->hbalock, iflag);
686}
687
688/**
James Smartda0436e2009-05-22 14:51:39 -0400689 * lpfc_sli4_fcp_xri_aborted - Fast-path process of fcp xri abort
690 * @phba: pointer to lpfc hba data structure.
691 * @axri: pointer to the fcp xri abort wcqe structure.
692 *
693 * This routine is invoked by the worker thread to process a SLI4 fast-path
694 * FCP aborted xri.
695 **/
696void
697lpfc_sli4_fcp_xri_aborted(struct lpfc_hba *phba,
698 struct sli4_wcqe_xri_aborted *axri)
699{
700 uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
James Smart19ca7602010-11-20 23:11:55 -0500701 uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
James Smartda0436e2009-05-22 14:51:39 -0400702 struct lpfc_scsi_buf *psb, *next_psb;
703 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -0500704 struct lpfc_iocbq *iocbq;
705 int i;
James Smart19ca7602010-11-20 23:11:55 -0500706 struct lpfc_nodelist *ndlp;
707 int rrq_empty = 0;
James Smart589a52d2010-07-14 15:30:54 -0400708 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smartda0436e2009-05-22 14:51:39 -0400709
James Smart0f65ff62010-02-26 14:14:23 -0500710 spin_lock_irqsave(&phba->hbalock, iflag);
711 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
James Smartda0436e2009-05-22 14:51:39 -0400712 list_for_each_entry_safe(psb, next_psb,
713 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
714 if (psb->cur_iocbq.sli4_xritag == xri) {
715 list_del(&psb->list);
James Smart341af102010-01-26 23:07:37 -0500716 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400717 psb->status = IOSTAT_SUCCESS;
James Smart0f65ff62010-02-26 14:14:23 -0500718 spin_unlock(
719 &phba->sli4_hba.abts_scsi_buf_list_lock);
James Smart1151e3e2011-02-16 12:39:35 -0500720 if (psb->rdata && psb->rdata->pnode)
721 ndlp = psb->rdata->pnode;
722 else
723 ndlp = NULL;
724
James Smart19ca7602010-11-20 23:11:55 -0500725 rrq_empty = list_empty(&phba->active_rrq_list);
James Smart0f65ff62010-02-26 14:14:23 -0500726 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartcb69f7d2011-12-13 13:21:57 -0500727 if (ndlp) {
James Smartee0f4fe2012-05-09 21:19:14 -0400728 lpfc_set_rrq_active(phba, ndlp,
729 psb->cur_iocbq.sli4_lxritag, rxid, 1);
James Smartcb69f7d2011-12-13 13:21:57 -0500730 lpfc_sli4_abts_err_handler(phba, ndlp, axri);
731 }
James Smartda0436e2009-05-22 14:51:39 -0400732 lpfc_release_scsi_buf_s4(phba, psb);
James Smart19ca7602010-11-20 23:11:55 -0500733 if (rrq_empty)
734 lpfc_worker_wake_up(phba);
James Smartda0436e2009-05-22 14:51:39 -0400735 return;
736 }
737 }
James Smart0f65ff62010-02-26 14:14:23 -0500738 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
739 for (i = 1; i <= phba->sli.last_iotag; i++) {
740 iocbq = phba->sli.iocbq_lookup[i];
741
742 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
743 (iocbq->iocb_flag & LPFC_IO_LIBDFC))
744 continue;
745 if (iocbq->sli4_xritag != xri)
746 continue;
747 psb = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
748 psb->exch_busy = 0;
749 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart0e9bb8d2013-03-01 16:35:12 -0500750 if (!list_empty(&pring->txq))
James Smart589a52d2010-07-14 15:30:54 -0400751 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -0500752 return;
753
754 }
755 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartda0436e2009-05-22 14:51:39 -0400756}
757
758/**
James Smart8a9d2e82012-05-09 21:16:12 -0400759 * lpfc_sli4_post_scsi_sgl_list - Psot blocks of scsi buffer sgls from a list
760 * @phba: pointer to lpfc hba data structure.
761 * @post_sblist: pointer to the scsi buffer list.
762 *
763 * This routine walks a list of scsi buffers that was passed in. It attempts
764 * to construct blocks of scsi buffer sgls which contains contiguous xris and
765 * uses the non-embedded SGL block post mailbox commands to post to the port.
766 * For single SCSI buffer sgl with non-contiguous xri, if any, it shall use
767 * embedded SGL post mailbox command for posting. The @post_sblist passed in
768 * must be local list, thus no lock is needed when manipulate the list.
769 *
770 * Returns: 0 = failure, non-zero number of successfully posted buffers.
771 **/
772int
773lpfc_sli4_post_scsi_sgl_list(struct lpfc_hba *phba,
774 struct list_head *post_sblist, int sb_count)
775{
776 struct lpfc_scsi_buf *psb, *psb_next;
James Smart96f70772013-04-17 20:16:15 -0400777 int status, sgl_size;
James Smart8a9d2e82012-05-09 21:16:12 -0400778 int post_cnt = 0, block_cnt = 0, num_posting = 0, num_posted = 0;
779 dma_addr_t pdma_phys_bpl1;
780 int last_xritag = NO_XRI;
781 LIST_HEAD(prep_sblist);
782 LIST_HEAD(blck_sblist);
783 LIST_HEAD(scsi_sblist);
784
785 /* sanity check */
786 if (sb_count <= 0)
787 return -EINVAL;
788
James Smart96f70772013-04-17 20:16:15 -0400789 sgl_size = phba->cfg_sg_dma_buf_size -
790 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
791
James Smart8a9d2e82012-05-09 21:16:12 -0400792 list_for_each_entry_safe(psb, psb_next, post_sblist, list) {
793 list_del_init(&psb->list);
794 block_cnt++;
795 if ((last_xritag != NO_XRI) &&
796 (psb->cur_iocbq.sli4_xritag != last_xritag + 1)) {
797 /* a hole in xri block, form a sgl posting block */
798 list_splice_init(&prep_sblist, &blck_sblist);
799 post_cnt = block_cnt - 1;
800 /* prepare list for next posting block */
801 list_add_tail(&psb->list, &prep_sblist);
802 block_cnt = 1;
803 } else {
804 /* prepare list for next posting block */
805 list_add_tail(&psb->list, &prep_sblist);
806 /* enough sgls for non-embed sgl mbox command */
807 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
808 list_splice_init(&prep_sblist, &blck_sblist);
809 post_cnt = block_cnt;
810 block_cnt = 0;
811 }
812 }
813 num_posting++;
814 last_xritag = psb->cur_iocbq.sli4_xritag;
815
816 /* end of repost sgl list condition for SCSI buffers */
817 if (num_posting == sb_count) {
818 if (post_cnt == 0) {
819 /* last sgl posting block */
820 list_splice_init(&prep_sblist, &blck_sblist);
821 post_cnt = block_cnt;
822 } else if (block_cnt == 1) {
823 /* last single sgl with non-contiguous xri */
James Smart96f70772013-04-17 20:16:15 -0400824 if (sgl_size > SGL_PAGE_SIZE)
James Smart8a9d2e82012-05-09 21:16:12 -0400825 pdma_phys_bpl1 = psb->dma_phys_bpl +
826 SGL_PAGE_SIZE;
827 else
828 pdma_phys_bpl1 = 0;
829 status = lpfc_sli4_post_sgl(phba,
830 psb->dma_phys_bpl,
831 pdma_phys_bpl1,
832 psb->cur_iocbq.sli4_xritag);
833 if (status) {
834 /* failure, put on abort scsi list */
835 psb->exch_busy = 1;
836 } else {
837 /* success, put on SCSI buffer list */
838 psb->exch_busy = 0;
839 psb->status = IOSTAT_SUCCESS;
840 num_posted++;
841 }
842 /* success, put on SCSI buffer sgl list */
843 list_add_tail(&psb->list, &scsi_sblist);
844 }
845 }
846
847 /* continue until a nembed page worth of sgls */
848 if (post_cnt == 0)
849 continue;
850
851 /* post block of SCSI buffer list sgls */
852 status = lpfc_sli4_post_scsi_sgl_block(phba, &blck_sblist,
853 post_cnt);
854
855 /* don't reset xirtag due to hole in xri block */
856 if (block_cnt == 0)
857 last_xritag = NO_XRI;
858
859 /* reset SCSI buffer post count for next round of posting */
860 post_cnt = 0;
861
862 /* put posted SCSI buffer-sgl posted on SCSI buffer sgl list */
863 while (!list_empty(&blck_sblist)) {
864 list_remove_head(&blck_sblist, psb,
865 struct lpfc_scsi_buf, list);
866 if (status) {
867 /* failure, put on abort scsi list */
868 psb->exch_busy = 1;
869 } else {
870 /* success, put on SCSI buffer list */
871 psb->exch_busy = 0;
872 psb->status = IOSTAT_SUCCESS;
873 num_posted++;
874 }
875 list_add_tail(&psb->list, &scsi_sblist);
876 }
877 }
878 /* Push SCSI buffers with sgl posted to the availble list */
879 while (!list_empty(&scsi_sblist)) {
880 list_remove_head(&scsi_sblist, psb,
881 struct lpfc_scsi_buf, list);
882 lpfc_release_scsi_buf_s4(phba, psb);
883 }
884 return num_posted;
885}
886
887/**
888 * lpfc_sli4_repost_scsi_sgl_list - Repsot all the allocated scsi buffer sgls
James Smartda0436e2009-05-22 14:51:39 -0400889 * @phba: pointer to lpfc hba data structure.
890 *
891 * This routine walks the list of scsi buffers that have been allocated and
James Smart8a9d2e82012-05-09 21:16:12 -0400892 * repost them to the port by using SGL block post. This is needed after a
James Smartda0436e2009-05-22 14:51:39 -0400893 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
894 * is responsible for moving all scsi buffers on the lpfc_abts_scsi_sgl_list
895 * to the lpfc_scsi_buf_list. If the repost fails, reject all scsi buffers.
896 *
897 * Returns: 0 = success, non-zero failure.
898 **/
899int
900lpfc_sli4_repost_scsi_sgl_list(struct lpfc_hba *phba)
901{
James Smart8a9d2e82012-05-09 21:16:12 -0400902 LIST_HEAD(post_sblist);
903 int num_posted, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -0400904
James Smart8a9d2e82012-05-09 21:16:12 -0400905 /* get all SCSI buffers need to repost to a local list */
James Smarta40fc5f2013-04-17 20:17:40 -0400906 spin_lock_irq(&phba->scsi_buf_list_get_lock);
907 spin_lock_irq(&phba->scsi_buf_list_put_lock);
908 list_splice_init(&phba->lpfc_scsi_buf_list_get, &post_sblist);
909 list_splice(&phba->lpfc_scsi_buf_list_put, &post_sblist);
910 spin_unlock_irq(&phba->scsi_buf_list_put_lock);
911 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smartda0436e2009-05-22 14:51:39 -0400912
James Smart8a9d2e82012-05-09 21:16:12 -0400913 /* post the list of scsi buffer sgls to port if available */
914 if (!list_empty(&post_sblist)) {
915 num_posted = lpfc_sli4_post_scsi_sgl_list(phba, &post_sblist,
916 phba->sli4_hba.scsi_xri_cnt);
917 /* failed to post any scsi buffer, return error */
918 if (num_posted == 0)
919 rc = -EIO;
James Smartda0436e2009-05-22 14:51:39 -0400920 }
921 return rc;
922}
923
924/**
925 * lpfc_new_scsi_buf_s4 - Scsi buffer allocator for HBA with SLI4 IF spec
926 * @vport: The virtual port for which this call being executed.
927 * @num_to_allocate: The requested number of buffers to allocate.
928 *
James Smart8a9d2e82012-05-09 21:16:12 -0400929 * This routine allocates scsi buffers for device with SLI-4 interface spec,
James Smartda0436e2009-05-22 14:51:39 -0400930 * the scsi buffer contains all the necessary information needed to initiate
James Smart8a9d2e82012-05-09 21:16:12 -0400931 * a SCSI I/O. After allocating up to @num_to_allocate SCSI buffers and put
932 * them on a list, it post them to the port by using SGL block post.
James Smartda0436e2009-05-22 14:51:39 -0400933 *
934 * Return codes:
James Smart8a9d2e82012-05-09 21:16:12 -0400935 * int - number of scsi buffers that were allocated and posted.
James Smartda0436e2009-05-22 14:51:39 -0400936 * 0 = failure, less than num_to_alloc is a partial failure.
937 **/
938static int
939lpfc_new_scsi_buf_s4(struct lpfc_vport *vport, int num_to_alloc)
940{
941 struct lpfc_hba *phba = vport->phba;
942 struct lpfc_scsi_buf *psb;
943 struct sli4_sge *sgl;
944 IOCB_t *iocb;
945 dma_addr_t pdma_phys_fcp_cmd;
946 dma_addr_t pdma_phys_fcp_rsp;
James Smart96f70772013-04-17 20:16:15 -0400947 dma_addr_t pdma_phys_bpl;
James Smart8a9d2e82012-05-09 21:16:12 -0400948 uint16_t iotag, lxri = 0;
James Smart96f70772013-04-17 20:16:15 -0400949 int bcnt, num_posted, sgl_size;
James Smart8a9d2e82012-05-09 21:16:12 -0400950 LIST_HEAD(prep_sblist);
951 LIST_HEAD(post_sblist);
952 LIST_HEAD(scsi_sblist);
James Smartda0436e2009-05-22 14:51:39 -0400953
James Smart96f70772013-04-17 20:16:15 -0400954 sgl_size = phba->cfg_sg_dma_buf_size -
955 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
956
957 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
958 "9068 ALLOC %d scsi_bufs: %d (%d + %d + %d)\n",
959 num_to_alloc, phba->cfg_sg_dma_buf_size, sgl_size,
960 (int)sizeof(struct fcp_cmnd),
961 (int)sizeof(struct fcp_rsp));
962
James Smartda0436e2009-05-22 14:51:39 -0400963 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
964 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
965 if (!psb)
966 break;
James Smartda0436e2009-05-22 14:51:39 -0400967 /*
James Smart8a9d2e82012-05-09 21:16:12 -0400968 * Get memory from the pci pool to map the virt space to
969 * pci bus space for an I/O. The DMA buffer includes space
970 * for the struct fcp_cmnd, struct fcp_rsp and the number
971 * of bde's necessary to support the sg_tablesize.
James Smartda0436e2009-05-22 14:51:39 -0400972 */
973 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
974 GFP_KERNEL, &psb->dma_handle);
975 if (!psb->data) {
976 kfree(psb);
977 break;
978 }
James Smartda0436e2009-05-22 14:51:39 -0400979 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
980
James Smart96f70772013-04-17 20:16:15 -0400981 /* Page alignment is CRITICAL, double check to be sure */
982 if (((unsigned long)(psb->data) &
983 (unsigned long)(SLI4_PAGE_SIZE - 1)) != 0) {
984 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
985 psb->data, psb->dma_handle);
986 kfree(psb);
987 break;
988 }
989
James Smartda0436e2009-05-22 14:51:39 -0400990 /* Allocate iotag for psb->cur_iocbq. */
991 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
992 if (iotag == 0) {
James Smartb92938b2010-06-07 15:24:12 -0400993 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
994 psb->data, psb->dma_handle);
James Smartda0436e2009-05-22 14:51:39 -0400995 kfree(psb);
996 break;
997 }
998
James Smart6d368e52011-05-24 11:44:12 -0400999 lxri = lpfc_sli4_next_xritag(phba);
1000 if (lxri == NO_XRI) {
James Smartda0436e2009-05-22 14:51:39 -04001001 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
1002 psb->data, psb->dma_handle);
1003 kfree(psb);
1004 break;
1005 }
James Smart6d368e52011-05-24 11:44:12 -04001006 psb->cur_iocbq.sli4_lxritag = lxri;
1007 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
James Smartda0436e2009-05-22 14:51:39 -04001008 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Smartda0436e2009-05-22 14:51:39 -04001009 psb->fcp_bpl = psb->data;
James Smart96f70772013-04-17 20:16:15 -04001010 psb->fcp_cmnd = (psb->data + sgl_size);
James Smartda0436e2009-05-22 14:51:39 -04001011 psb->fcp_rsp = (struct fcp_rsp *)((uint8_t *)psb->fcp_cmnd +
1012 sizeof(struct fcp_cmnd));
1013
1014 /* Initialize local short-hand pointers. */
1015 sgl = (struct sli4_sge *)psb->fcp_bpl;
1016 pdma_phys_bpl = psb->dma_handle;
James Smart96f70772013-04-17 20:16:15 -04001017 pdma_phys_fcp_cmd = (psb->dma_handle + sgl_size);
James Smartda0436e2009-05-22 14:51:39 -04001018 pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
1019
1020 /*
James Smart8a9d2e82012-05-09 21:16:12 -04001021 * The first two bdes are the FCP_CMD and FCP_RSP.
1022 * The balance are sg list bdes. Initialize the
1023 * first two and leave the rest for queuecommand.
James Smartda0436e2009-05-22 14:51:39 -04001024 */
1025 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
1026 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
James Smart05580562011-05-24 11:40:48 -04001027 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -04001028 bf_set(lpfc_sli4_sge_last, sgl, 0);
1029 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05001030 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_cmnd));
James Smartda0436e2009-05-22 14:51:39 -04001031 sgl++;
1032
1033 /* Setup the physical region for the FCP RSP */
1034 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
1035 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
James Smart05580562011-05-24 11:40:48 -04001036 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -04001037 bf_set(lpfc_sli4_sge_last, sgl, 1);
1038 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05001039 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
James Smartda0436e2009-05-22 14:51:39 -04001040
1041 /*
1042 * Since the IOCB for the FCP I/O is built into this
1043 * lpfc_scsi_buf, initialize it with all known data now.
1044 */
1045 iocb = &psb->cur_iocbq.iocb;
1046 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
1047 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
1048 /* setting the BLP size to 2 * sizeof BDE may not be correct.
1049 * We are setting the bpl to point to out sgl. An sgl's
1050 * entries are 16 bytes, a bpl entries are 12 bytes.
1051 */
1052 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
1053 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
1054 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
1055 iocb->ulpBdeCount = 1;
1056 iocb->ulpLe = 1;
1057 iocb->ulpClass = CLASS3;
James Smart8a9d2e82012-05-09 21:16:12 -04001058 psb->cur_iocbq.context1 = psb;
James Smartda0436e2009-05-22 14:51:39 -04001059 psb->dma_phys_bpl = pdma_phys_bpl;
James Smart6d368e52011-05-24 11:44:12 -04001060
James Smart8a9d2e82012-05-09 21:16:12 -04001061 /* add the scsi buffer to a post list */
1062 list_add_tail(&psb->list, &post_sblist);
James Smarta40fc5f2013-04-17 20:17:40 -04001063 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04001064 phba->sli4_hba.scsi_xri_cnt++;
James Smarta40fc5f2013-04-17 20:17:40 -04001065 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smartda0436e2009-05-22 14:51:39 -04001066 }
James Smart8a9d2e82012-05-09 21:16:12 -04001067 lpfc_printf_log(phba, KERN_INFO, LOG_BG,
1068 "3021 Allocate %d out of %d requested new SCSI "
1069 "buffers\n", bcnt, num_to_alloc);
James Smartda0436e2009-05-22 14:51:39 -04001070
James Smart8a9d2e82012-05-09 21:16:12 -04001071 /* post the list of scsi buffer sgls to port if available */
1072 if (!list_empty(&post_sblist))
1073 num_posted = lpfc_sli4_post_scsi_sgl_list(phba,
1074 &post_sblist, bcnt);
1075 else
1076 num_posted = 0;
1077
1078 return num_posted;
James Smartda0436e2009-05-22 14:51:39 -04001079}
1080
1081/**
James Smart3772a992009-05-22 14:50:54 -04001082 * lpfc_new_scsi_buf - Wrapper funciton for scsi buffer allocator
1083 * @vport: The virtual port for which this call being executed.
1084 * @num_to_allocate: The requested number of buffers to allocate.
1085 *
1086 * This routine wraps the actual SCSI buffer allocator function pointer from
1087 * the lpfc_hba struct.
1088 *
1089 * Return codes:
1090 * int - number of scsi buffers that were allocated.
1091 * 0 = failure, less than num_to_alloc is a partial failure.
1092 **/
1093static inline int
1094lpfc_new_scsi_buf(struct lpfc_vport *vport, int num_to_alloc)
1095{
1096 return vport->phba->lpfc_new_scsi_buf(vport, num_to_alloc);
1097}
1098
1099/**
James Smart19ca7602010-11-20 23:11:55 -05001100 * lpfc_get_scsi_buf_s3 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
James Smart3772a992009-05-22 14:50:54 -04001101 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -05001102 *
1103 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1104 * and returns to caller.
1105 *
1106 * Return codes:
1107 * NULL - Error
1108 * Pointer to lpfc_scsi_buf - Success
1109 **/
Adrian Bunk455c53e2006-01-06 20:21:28 +01001110static struct lpfc_scsi_buf*
James Smart19ca7602010-11-20 23:11:55 -05001111lpfc_get_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001112{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001113 struct lpfc_scsi_buf * lpfc_cmd = NULL;
James Smarta40fc5f2013-04-17 20:17:40 -04001114 struct list_head *scsi_buf_list_get = &phba->lpfc_scsi_buf_list_get;
1115 unsigned long gflag = 0;
1116 unsigned long pflag = 0;
dea31012005-04-17 16:05:31 -05001117
James Smarta40fc5f2013-04-17 20:17:40 -04001118 spin_lock_irqsave(&phba->scsi_buf_list_get_lock, gflag);
1119 list_remove_head(scsi_buf_list_get, lpfc_cmd, struct lpfc_scsi_buf,
1120 list);
1121 if (!lpfc_cmd) {
1122 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, pflag);
1123 list_splice(&phba->lpfc_scsi_buf_list_put,
1124 &phba->lpfc_scsi_buf_list_get);
1125 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
1126 list_remove_head(scsi_buf_list_get, lpfc_cmd,
1127 struct lpfc_scsi_buf, list);
1128 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, pflag);
James Smart1dcb58e2007-04-25 09:51:30 -04001129 }
James Smarta40fc5f2013-04-17 20:17:40 -04001130 spin_unlock_irqrestore(&phba->scsi_buf_list_get_lock, gflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001131 return lpfc_cmd;
1132}
James Smart19ca7602010-11-20 23:11:55 -05001133/**
1134 * lpfc_get_scsi_buf_s4 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1135 * @phba: The HBA for which this call is being executed.
1136 *
1137 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1138 * and returns to caller.
1139 *
1140 * Return codes:
1141 * NULL - Error
1142 * Pointer to lpfc_scsi_buf - Success
1143 **/
1144static struct lpfc_scsi_buf*
1145lpfc_get_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1146{
James Smart1151e3e2011-02-16 12:39:35 -05001147 struct lpfc_scsi_buf *lpfc_cmd ;
James Smarta40fc5f2013-04-17 20:17:40 -04001148 unsigned long gflag = 0;
1149 unsigned long pflag = 0;
James Smart19ca7602010-11-20 23:11:55 -05001150 int found = 0;
1151
James Smarta40fc5f2013-04-17 20:17:40 -04001152 spin_lock_irqsave(&phba->scsi_buf_list_get_lock, gflag);
1153 list_for_each_entry(lpfc_cmd, &phba->lpfc_scsi_buf_list_get, list) {
James Smart19ca7602010-11-20 23:11:55 -05001154 if (lpfc_test_rrq_active(phba, ndlp,
James Smartee0f4fe2012-05-09 21:19:14 -04001155 lpfc_cmd->cur_iocbq.sli4_lxritag))
James Smart1151e3e2011-02-16 12:39:35 -05001156 continue;
1157 list_del(&lpfc_cmd->list);
James Smart19ca7602010-11-20 23:11:55 -05001158 found = 1;
James Smart1151e3e2011-02-16 12:39:35 -05001159 break;
James Smart19ca7602010-11-20 23:11:55 -05001160 }
James Smarta40fc5f2013-04-17 20:17:40 -04001161 if (!found) {
1162 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, pflag);
1163 list_splice(&phba->lpfc_scsi_buf_list_put,
1164 &phba->lpfc_scsi_buf_list_get);
1165 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
1166 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, pflag);
1167 list_for_each_entry(lpfc_cmd, &phba->lpfc_scsi_buf_list_get,
1168 list) {
1169 if (lpfc_test_rrq_active(
1170 phba, ndlp, lpfc_cmd->cur_iocbq.sli4_lxritag))
1171 continue;
1172 list_del(&lpfc_cmd->list);
1173 found = 1;
1174 break;
1175 }
1176 }
1177 spin_unlock_irqrestore(&phba->scsi_buf_list_get_lock, gflag);
James Smart1151e3e2011-02-16 12:39:35 -05001178 if (!found)
1179 return NULL;
James Smarta40fc5f2013-04-17 20:17:40 -04001180 return lpfc_cmd;
James Smart19ca7602010-11-20 23:11:55 -05001181}
1182/**
1183 * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1184 * @phba: The HBA for which this call is being executed.
1185 *
1186 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1187 * and returns to caller.
1188 *
1189 * Return codes:
1190 * NULL - Error
1191 * Pointer to lpfc_scsi_buf - Success
1192 **/
1193static struct lpfc_scsi_buf*
1194lpfc_get_scsi_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1195{
1196 return phba->lpfc_get_scsi_buf(phba, ndlp);
1197}
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001198
James Smart9bad7672008-12-04 22:39:02 -05001199/**
James Smart3772a992009-05-22 14:50:54 -04001200 * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
James Smart9bad7672008-12-04 22:39:02 -05001201 * @phba: The Hba for which this call is being executed.
1202 * @psb: The scsi buffer which is being released.
1203 *
1204 * This routine releases @psb scsi buffer by adding it to tail of @phba
1205 * lpfc_scsi_buf_list list.
1206 **/
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001207static void
James Smart3772a992009-05-22 14:50:54 -04001208lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001209{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001210 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -05001211
James Smarta40fc5f2013-04-17 20:17:40 -04001212 psb->seg_cnt = 0;
1213 psb->nonsg_phys = 0;
1214 psb->prot_seg_cnt = 0;
1215
1216 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001217 psb->pCmd = NULL;
James Smarta40fc5f2013-04-17 20:17:40 -04001218 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list_put);
1219 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
dea31012005-04-17 16:05:31 -05001220}
1221
James Smart9bad7672008-12-04 22:39:02 -05001222/**
James Smartda0436e2009-05-22 14:51:39 -04001223 * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
1224 * @phba: The Hba for which this call is being executed.
1225 * @psb: The scsi buffer which is being released.
1226 *
1227 * This routine releases @psb scsi buffer by adding it to tail of @phba
1228 * lpfc_scsi_buf_list list. For SLI4 XRI's are tied to the scsi buffer
1229 * and cannot be reused for at least RA_TOV amount of time if it was
1230 * aborted.
1231 **/
1232static void
1233lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1234{
1235 unsigned long iflag = 0;
1236
James Smarta40fc5f2013-04-17 20:17:40 -04001237 psb->seg_cnt = 0;
1238 psb->nonsg_phys = 0;
1239 psb->prot_seg_cnt = 0;
1240
James Smart341af102010-01-26 23:07:37 -05001241 if (psb->exch_busy) {
James Smartda0436e2009-05-22 14:51:39 -04001242 spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock,
1243 iflag);
1244 psb->pCmd = NULL;
1245 list_add_tail(&psb->list,
1246 &phba->sli4_hba.lpfc_abts_scsi_buf_list);
1247 spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
1248 iflag);
1249 } else {
James Smartda0436e2009-05-22 14:51:39 -04001250 psb->pCmd = NULL;
James Smarta40fc5f2013-04-17 20:17:40 -04001251 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
1252 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list_put);
1253 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
James Smartda0436e2009-05-22 14:51:39 -04001254 }
1255}
1256
1257/**
James Smart3772a992009-05-22 14:50:54 -04001258 * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
1259 * @phba: The Hba for which this call is being executed.
1260 * @psb: The scsi buffer which is being released.
1261 *
1262 * This routine releases @psb scsi buffer by adding it to tail of @phba
1263 * lpfc_scsi_buf_list list.
1264 **/
1265static void
1266lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1267{
1268
1269 phba->lpfc_release_scsi_buf(phba, psb);
1270}
1271
1272/**
1273 * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -05001274 * @phba: The Hba for which this call is being executed.
1275 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1276 *
1277 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
James Smart3772a992009-05-22 14:50:54 -04001278 * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
1279 * through sg elements and format the bdea. This routine also initializes all
1280 * IOCB fields which are dependent on scsi command request buffer.
James Smart9bad7672008-12-04 22:39:02 -05001281 *
1282 * Return codes:
1283 * 1 - Error
1284 * 0 - Success
1285 **/
dea31012005-04-17 16:05:31 -05001286static int
James Smart3772a992009-05-22 14:50:54 -04001287lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
dea31012005-04-17 16:05:31 -05001288{
1289 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1290 struct scatterlist *sgel = NULL;
1291 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1292 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
James Smart0f65ff62010-02-26 14:14:23 -05001293 struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
dea31012005-04-17 16:05:31 -05001294 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart34b02dc2008-08-24 21:49:55 -04001295 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
dea31012005-04-17 16:05:31 -05001296 dma_addr_t physaddr;
James Smart34b02dc2008-08-24 21:49:55 -04001297 uint32_t num_bde = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001298 int nseg, datadir = scsi_cmnd->sc_data_direction;
dea31012005-04-17 16:05:31 -05001299
1300 /*
1301 * There are three possibilities here - use scatter-gather segment, use
1302 * the single mapping, or neither. Start the lpfc command prep by
1303 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1304 * data bde entry.
1305 */
1306 bpl += 2;
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001307 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05001308 /*
1309 * The driver stores the segment count returned from pci_map_sg
1310 * because this a count of dma-mappings used to map the use_sg
1311 * pages. They are not guaranteed to be the same for those
1312 * architectures that implement an IOMMU.
1313 */
dea31012005-04-17 16:05:31 -05001314
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001315 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
1316 scsi_sg_count(scsi_cmnd), datadir);
1317 if (unlikely(!nseg))
1318 return 1;
1319
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001320 lpfc_cmd->seg_cnt = nseg;
dea31012005-04-17 16:05:31 -05001321 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001322 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1323 "9064 BLKGRD: %s: Too many sg segments from "
James Smarte2a0a9d2008-12-04 22:40:02 -05001324 "dma_map_sg. Config %d, seg_cnt %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001325 __func__, phba->cfg_sg_seg_cnt,
dea31012005-04-17 16:05:31 -05001326 lpfc_cmd->seg_cnt);
James Smart96f70772013-04-17 20:16:15 -04001327 lpfc_cmd->seg_cnt = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001328 scsi_dma_unmap(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001329 return 1;
1330 }
1331
1332 /*
1333 * The driver established a maximum scatter-gather segment count
1334 * during probe that limits the number of sg elements in any
1335 * single scsi command. Just run through the seg_cnt and format
1336 * the bde's.
James Smart34b02dc2008-08-24 21:49:55 -04001337 * When using SLI-3 the driver will try to fit all the BDEs into
1338 * the IOCB. If it can't then the BDEs get added to a BPL as it
1339 * does for SLI-2 mode.
dea31012005-04-17 16:05:31 -05001340 */
James Smart34b02dc2008-08-24 21:49:55 -04001341 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
dea31012005-04-17 16:05:31 -05001342 physaddr = sg_dma_address(sgel);
James Smart34b02dc2008-08-24 21:49:55 -04001343 if (phba->sli_rev == 3 &&
James Smarte2a0a9d2008-12-04 22:40:02 -05001344 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
James Smart0f65ff62010-02-26 14:14:23 -05001345 !(iocbq->iocb_flag & DSS_SECURITY_OP) &&
James Smart34b02dc2008-08-24 21:49:55 -04001346 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
1347 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1348 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
1349 data_bde->addrLow = putPaddrLow(physaddr);
1350 data_bde->addrHigh = putPaddrHigh(physaddr);
1351 data_bde++;
1352 } else {
1353 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1354 bpl->tus.f.bdeSize = sg_dma_len(sgel);
1355 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1356 bpl->addrLow =
1357 le32_to_cpu(putPaddrLow(physaddr));
1358 bpl->addrHigh =
1359 le32_to_cpu(putPaddrHigh(physaddr));
1360 bpl++;
1361 }
dea31012005-04-17 16:05:31 -05001362 }
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001363 }
dea31012005-04-17 16:05:31 -05001364
1365 /*
1366 * Finish initializing those IOCB fields that are dependent on the
James Smart34b02dc2008-08-24 21:49:55 -04001367 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
1368 * explicitly reinitialized and for SLI-3 the extended bde count is
1369 * explicitly reinitialized since all iocb memory resources are reused.
dea31012005-04-17 16:05:31 -05001370 */
James Smarte2a0a9d2008-12-04 22:40:02 -05001371 if (phba->sli_rev == 3 &&
James Smart0f65ff62010-02-26 14:14:23 -05001372 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
1373 !(iocbq->iocb_flag & DSS_SECURITY_OP)) {
James Smart34b02dc2008-08-24 21:49:55 -04001374 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
1375 /*
1376 * The extended IOCB format can only fit 3 BDE or a BPL.
1377 * This I/O has more than 3 BDE so the 1st data bde will
1378 * be a BPL that is filled in here.
1379 */
1380 physaddr = lpfc_cmd->dma_handle;
1381 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
1382 data_bde->tus.f.bdeSize = (num_bde *
1383 sizeof(struct ulp_bde64));
1384 physaddr += (sizeof(struct fcp_cmnd) +
1385 sizeof(struct fcp_rsp) +
1386 (2 * sizeof(struct ulp_bde64)));
1387 data_bde->addrHigh = putPaddrHigh(physaddr);
1388 data_bde->addrLow = putPaddrLow(physaddr);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001389 /* ebde count includes the response bde and data bpl */
James Smart34b02dc2008-08-24 21:49:55 -04001390 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
1391 } else {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001392 /* ebde count includes the response bde and data bdes */
James Smart34b02dc2008-08-24 21:49:55 -04001393 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1394 }
1395 } else {
1396 iocb_cmd->un.fcpi64.bdl.bdeSize =
1397 ((num_bde + 2) * sizeof(struct ulp_bde64));
James Smart0f65ff62010-02-26 14:14:23 -05001398 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
James Smart34b02dc2008-08-24 21:49:55 -04001399 }
James Smart09372822008-01-11 01:52:54 -05001400 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
James Smarte2a0a9d2008-12-04 22:40:02 -05001401
1402 /*
1403 * Due to difference in data length between DIF/non-DIF paths,
1404 * we need to set word 4 of IOCB here
1405 */
James Smarta257bf92009-04-06 18:48:10 -04001406 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001407 return 0;
1408}
1409
James Smartf9bb2da2011-10-10 21:34:11 -04001410static inline unsigned
1411lpfc_cmd_blksize(struct scsi_cmnd *sc)
1412{
1413 return sc->device->sector_size;
1414}
1415
1416#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001417
James Smart9a6b09c2012-03-01 22:37:42 -05001418/* Return if if error injection is detected by Initiator */
1419#define BG_ERR_INIT 0x1
1420/* Return if if error injection is detected by Target */
1421#define BG_ERR_TGT 0x2
1422/* Return if if swapping CSUM<-->CRC is required for error injection */
1423#define BG_ERR_SWAP 0x10
1424/* Return if disabling Guard/Ref/App checking is required for error injection */
1425#define BG_ERR_CHECK 0x20
James Smartacd68592012-01-18 16:25:09 -05001426
1427/**
1428 * lpfc_bg_err_inject - Determine if we should inject an error
1429 * @phba: The Hba for which this call is being executed.
James Smartf9bb2da2011-10-10 21:34:11 -04001430 * @sc: The SCSI command to examine
1431 * @reftag: (out) BlockGuard reference tag for transmitted data
1432 * @apptag: (out) BlockGuard application tag for transmitted data
1433 * @new_guard (in) Value to replace CRC with if needed
1434 *
James Smart9a6b09c2012-03-01 22:37:42 -05001435 * Returns BG_ERR_* bit mask or 0 if request ignored
James Smartacd68592012-01-18 16:25:09 -05001436 **/
James Smartf9bb2da2011-10-10 21:34:11 -04001437static int
1438lpfc_bg_err_inject(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1439 uint32_t *reftag, uint16_t *apptag, uint32_t new_guard)
1440{
1441 struct scatterlist *sgpe; /* s/g prot entry */
1442 struct scatterlist *sgde; /* s/g data entry */
James Smart9a6b09c2012-03-01 22:37:42 -05001443 struct lpfc_scsi_buf *lpfc_cmd = NULL;
James Smartacd68592012-01-18 16:25:09 -05001444 struct scsi_dif_tuple *src = NULL;
James Smart4ac9b222012-03-01 22:38:29 -05001445 struct lpfc_nodelist *ndlp;
1446 struct lpfc_rport_data *rdata;
James Smartf9bb2da2011-10-10 21:34:11 -04001447 uint32_t op = scsi_get_prot_op(sc);
1448 uint32_t blksize;
1449 uint32_t numblks;
1450 sector_t lba;
1451 int rc = 0;
James Smartacd68592012-01-18 16:25:09 -05001452 int blockoff = 0;
James Smartf9bb2da2011-10-10 21:34:11 -04001453
1454 if (op == SCSI_PROT_NORMAL)
1455 return 0;
1456
James Smartacd68592012-01-18 16:25:09 -05001457 sgpe = scsi_prot_sglist(sc);
1458 sgde = scsi_sglist(sc);
James Smartf9bb2da2011-10-10 21:34:11 -04001459 lba = scsi_get_lba(sc);
James Smart4ac9b222012-03-01 22:38:29 -05001460
1461 /* First check if we need to match the LBA */
James Smartf9bb2da2011-10-10 21:34:11 -04001462 if (phba->lpfc_injerr_lba != LPFC_INJERR_LBA_OFF) {
1463 blksize = lpfc_cmd_blksize(sc);
1464 numblks = (scsi_bufflen(sc) + blksize - 1) / blksize;
1465
1466 /* Make sure we have the right LBA if one is specified */
1467 if ((phba->lpfc_injerr_lba < lba) ||
1468 (phba->lpfc_injerr_lba >= (lba + numblks)))
1469 return 0;
James Smartacd68592012-01-18 16:25:09 -05001470 if (sgpe) {
1471 blockoff = phba->lpfc_injerr_lba - lba;
1472 numblks = sg_dma_len(sgpe) /
1473 sizeof(struct scsi_dif_tuple);
1474 if (numblks < blockoff)
1475 blockoff = numblks;
James Smartacd68592012-01-18 16:25:09 -05001476 }
James Smartf9bb2da2011-10-10 21:34:11 -04001477 }
1478
James Smart4ac9b222012-03-01 22:38:29 -05001479 /* Next check if we need to match the remote NPortID or WWPN */
1480 rdata = sc->device->hostdata;
1481 if (rdata && rdata->pnode) {
1482 ndlp = rdata->pnode;
1483
1484 /* Make sure we have the right NPortID if one is specified */
1485 if (phba->lpfc_injerr_nportid &&
1486 (phba->lpfc_injerr_nportid != ndlp->nlp_DID))
1487 return 0;
1488
1489 /*
1490 * Make sure we have the right WWPN if one is specified.
1491 * wwn[0] should be a non-zero NAA in a good WWPN.
1492 */
1493 if (phba->lpfc_injerr_wwpn.u.wwn[0] &&
1494 (memcmp(&ndlp->nlp_portname, &phba->lpfc_injerr_wwpn,
1495 sizeof(struct lpfc_name)) != 0))
1496 return 0;
1497 }
1498
1499 /* Setup a ptr to the protection data if the SCSI host provides it */
1500 if (sgpe) {
1501 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
1502 src += blockoff;
1503 lpfc_cmd = (struct lpfc_scsi_buf *)sc->host_scribble;
1504 }
1505
James Smartf9bb2da2011-10-10 21:34:11 -04001506 /* Should we change the Reference Tag */
1507 if (reftag) {
James Smartacd68592012-01-18 16:25:09 -05001508 if (phba->lpfc_injerr_wref_cnt) {
1509 switch (op) {
1510 case SCSI_PROT_WRITE_PASS:
James Smart9a6b09c2012-03-01 22:37:42 -05001511 if (src) {
1512 /*
1513 * For WRITE_PASS, force the error
1514 * to be sent on the wire. It should
1515 * be detected by the Target.
1516 * If blockoff != 0 error will be
1517 * inserted in middle of the IO.
1518 */
James Smartf9bb2da2011-10-10 21:34:11 -04001519
James Smartacd68592012-01-18 16:25:09 -05001520 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1521 "9076 BLKGRD: Injecting reftag error: "
1522 "write lba x%lx + x%x oldrefTag x%x\n",
1523 (unsigned long)lba, blockoff,
James Smart9a6b09c2012-03-01 22:37:42 -05001524 be32_to_cpu(src->ref_tag));
James Smartacd68592012-01-18 16:25:09 -05001525
1526 /*
James Smart9a6b09c2012-03-01 22:37:42 -05001527 * Save the old ref_tag so we can
1528 * restore it on completion.
James Smartacd68592012-01-18 16:25:09 -05001529 */
James Smart9a6b09c2012-03-01 22:37:42 -05001530 if (lpfc_cmd) {
1531 lpfc_cmd->prot_data_type =
1532 LPFC_INJERR_REFTAG;
1533 lpfc_cmd->prot_data_segment =
1534 src;
1535 lpfc_cmd->prot_data =
1536 src->ref_tag;
1537 }
1538 src->ref_tag = cpu_to_be32(0xDEADBEEF);
James Smartacd68592012-01-18 16:25:09 -05001539 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001540 if (phba->lpfc_injerr_wref_cnt == 0) {
1541 phba->lpfc_injerr_nportid = 0;
1542 phba->lpfc_injerr_lba =
1543 LPFC_INJERR_LBA_OFF;
1544 memset(&phba->lpfc_injerr_wwpn,
1545 0, sizeof(struct lpfc_name));
1546 }
James Smart9a6b09c2012-03-01 22:37:42 -05001547 rc = BG_ERR_TGT | BG_ERR_CHECK;
1548
James Smartacd68592012-01-18 16:25:09 -05001549 break;
1550 }
1551 /* Drop thru */
James Smart9a6b09c2012-03-01 22:37:42 -05001552 case SCSI_PROT_WRITE_INSERT:
1553 /*
1554 * For WRITE_INSERT, force the error
1555 * to be sent on the wire. It should be
1556 * detected by the Target.
1557 */
1558 /* DEADBEEF will be the reftag on the wire */
1559 *reftag = 0xDEADBEEF;
1560 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001561 if (phba->lpfc_injerr_wref_cnt == 0) {
1562 phba->lpfc_injerr_nportid = 0;
1563 phba->lpfc_injerr_lba =
1564 LPFC_INJERR_LBA_OFF;
1565 memset(&phba->lpfc_injerr_wwpn,
1566 0, sizeof(struct lpfc_name));
1567 }
James Smart9a6b09c2012-03-01 22:37:42 -05001568 rc = BG_ERR_TGT | BG_ERR_CHECK;
1569
1570 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1571 "9078 BLKGRD: Injecting reftag error: "
1572 "write lba x%lx\n", (unsigned long)lba);
1573 break;
James Smartacd68592012-01-18 16:25:09 -05001574 case SCSI_PROT_WRITE_STRIP:
1575 /*
1576 * For WRITE_STRIP and WRITE_PASS,
1577 * force the error on data
1578 * being copied from SLI-Host to SLI-Port.
1579 */
1580 *reftag = 0xDEADBEEF;
1581 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001582 if (phba->lpfc_injerr_wref_cnt == 0) {
1583 phba->lpfc_injerr_nportid = 0;
1584 phba->lpfc_injerr_lba =
1585 LPFC_INJERR_LBA_OFF;
1586 memset(&phba->lpfc_injerr_wwpn,
1587 0, sizeof(struct lpfc_name));
1588 }
James Smartacd68592012-01-18 16:25:09 -05001589 rc = BG_ERR_INIT;
1590
1591 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1592 "9077 BLKGRD: Injecting reftag error: "
1593 "write lba x%lx\n", (unsigned long)lba);
1594 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001595 }
James Smartacd68592012-01-18 16:25:09 -05001596 }
1597 if (phba->lpfc_injerr_rref_cnt) {
1598 switch (op) {
1599 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001600 case SCSI_PROT_READ_STRIP:
1601 case SCSI_PROT_READ_PASS:
1602 /*
1603 * For READ_STRIP and READ_PASS, force the
1604 * error on data being read off the wire. It
1605 * should force an IO error to the driver.
1606 */
James Smartf9bb2da2011-10-10 21:34:11 -04001607 *reftag = 0xDEADBEEF;
1608 phba->lpfc_injerr_rref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001609 if (phba->lpfc_injerr_rref_cnt == 0) {
1610 phba->lpfc_injerr_nportid = 0;
1611 phba->lpfc_injerr_lba =
1612 LPFC_INJERR_LBA_OFF;
1613 memset(&phba->lpfc_injerr_wwpn,
1614 0, sizeof(struct lpfc_name));
1615 }
James Smartacd68592012-01-18 16:25:09 -05001616 rc = BG_ERR_INIT;
James Smartf9bb2da2011-10-10 21:34:11 -04001617
1618 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smartacd68592012-01-18 16:25:09 -05001619 "9079 BLKGRD: Injecting reftag error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001620 "read lba x%lx\n", (unsigned long)lba);
James Smartacd68592012-01-18 16:25:09 -05001621 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001622 }
1623 }
1624 }
1625
1626 /* Should we change the Application Tag */
1627 if (apptag) {
James Smartacd68592012-01-18 16:25:09 -05001628 if (phba->lpfc_injerr_wapp_cnt) {
1629 switch (op) {
1630 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001631 if (src) {
James Smart9a6b09c2012-03-01 22:37:42 -05001632 /*
1633 * For WRITE_PASS, force the error
1634 * to be sent on the wire. It should
1635 * be detected by the Target.
1636 * If blockoff != 0 error will be
1637 * inserted in middle of the IO.
1638 */
1639
James Smartacd68592012-01-18 16:25:09 -05001640 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1641 "9080 BLKGRD: Injecting apptag error: "
1642 "write lba x%lx + x%x oldappTag x%x\n",
1643 (unsigned long)lba, blockoff,
James Smart9a6b09c2012-03-01 22:37:42 -05001644 be16_to_cpu(src->app_tag));
James Smartacd68592012-01-18 16:25:09 -05001645
1646 /*
James Smart9a6b09c2012-03-01 22:37:42 -05001647 * Save the old app_tag so we can
1648 * restore it on completion.
James Smartacd68592012-01-18 16:25:09 -05001649 */
James Smart9a6b09c2012-03-01 22:37:42 -05001650 if (lpfc_cmd) {
1651 lpfc_cmd->prot_data_type =
1652 LPFC_INJERR_APPTAG;
1653 lpfc_cmd->prot_data_segment =
1654 src;
1655 lpfc_cmd->prot_data =
1656 src->app_tag;
1657 }
1658 src->app_tag = cpu_to_be16(0xDEAD);
James Smartacd68592012-01-18 16:25:09 -05001659 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001660 if (phba->lpfc_injerr_wapp_cnt == 0) {
1661 phba->lpfc_injerr_nportid = 0;
1662 phba->lpfc_injerr_lba =
1663 LPFC_INJERR_LBA_OFF;
1664 memset(&phba->lpfc_injerr_wwpn,
1665 0, sizeof(struct lpfc_name));
1666 }
James Smart9a6b09c2012-03-01 22:37:42 -05001667 rc = BG_ERR_TGT | BG_ERR_CHECK;
James Smartacd68592012-01-18 16:25:09 -05001668 break;
1669 }
1670 /* Drop thru */
James Smart9a6b09c2012-03-01 22:37:42 -05001671 case SCSI_PROT_WRITE_INSERT:
1672 /*
1673 * For WRITE_INSERT, force the
1674 * error to be sent on the wire. It should be
1675 * detected by the Target.
1676 */
1677 /* DEAD will be the apptag on the wire */
1678 *apptag = 0xDEAD;
1679 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001680 if (phba->lpfc_injerr_wapp_cnt == 0) {
1681 phba->lpfc_injerr_nportid = 0;
1682 phba->lpfc_injerr_lba =
1683 LPFC_INJERR_LBA_OFF;
1684 memset(&phba->lpfc_injerr_wwpn,
1685 0, sizeof(struct lpfc_name));
1686 }
James Smart9a6b09c2012-03-01 22:37:42 -05001687 rc = BG_ERR_TGT | BG_ERR_CHECK;
1688
1689 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1690 "0813 BLKGRD: Injecting apptag error: "
1691 "write lba x%lx\n", (unsigned long)lba);
1692 break;
James Smartacd68592012-01-18 16:25:09 -05001693 case SCSI_PROT_WRITE_STRIP:
1694 /*
1695 * For WRITE_STRIP and WRITE_PASS,
1696 * force the error on data
1697 * being copied from SLI-Host to SLI-Port.
1698 */
1699 *apptag = 0xDEAD;
1700 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001701 if (phba->lpfc_injerr_wapp_cnt == 0) {
1702 phba->lpfc_injerr_nportid = 0;
1703 phba->lpfc_injerr_lba =
1704 LPFC_INJERR_LBA_OFF;
1705 memset(&phba->lpfc_injerr_wwpn,
1706 0, sizeof(struct lpfc_name));
1707 }
James Smartacd68592012-01-18 16:25:09 -05001708 rc = BG_ERR_INIT;
1709
1710 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1711 "0812 BLKGRD: Injecting apptag error: "
1712 "write lba x%lx\n", (unsigned long)lba);
1713 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001714 }
James Smartacd68592012-01-18 16:25:09 -05001715 }
1716 if (phba->lpfc_injerr_rapp_cnt) {
1717 switch (op) {
1718 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001719 case SCSI_PROT_READ_STRIP:
1720 case SCSI_PROT_READ_PASS:
1721 /*
1722 * For READ_STRIP and READ_PASS, force the
1723 * error on data being read off the wire. It
1724 * should force an IO error to the driver.
1725 */
James Smartf9bb2da2011-10-10 21:34:11 -04001726 *apptag = 0xDEAD;
1727 phba->lpfc_injerr_rapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001728 if (phba->lpfc_injerr_rapp_cnt == 0) {
1729 phba->lpfc_injerr_nportid = 0;
1730 phba->lpfc_injerr_lba =
1731 LPFC_INJERR_LBA_OFF;
1732 memset(&phba->lpfc_injerr_wwpn,
1733 0, sizeof(struct lpfc_name));
1734 }
James Smartacd68592012-01-18 16:25:09 -05001735 rc = BG_ERR_INIT;
James Smartf9bb2da2011-10-10 21:34:11 -04001736
1737 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smartacd68592012-01-18 16:25:09 -05001738 "0814 BLKGRD: Injecting apptag error: "
1739 "read lba x%lx\n", (unsigned long)lba);
1740 break;
1741 }
1742 }
1743 }
1744
1745
1746 /* Should we change the Guard Tag */
1747 if (new_guard) {
1748 if (phba->lpfc_injerr_wgrd_cnt) {
1749 switch (op) {
1750 case SCSI_PROT_WRITE_PASS:
James Smart9a6b09c2012-03-01 22:37:42 -05001751 rc = BG_ERR_CHECK;
James Smartacd68592012-01-18 16:25:09 -05001752 /* Drop thru */
James Smartacd68592012-01-18 16:25:09 -05001753
James Smartacd68592012-01-18 16:25:09 -05001754 case SCSI_PROT_WRITE_INSERT:
1755 /*
1756 * For WRITE_INSERT, force the
1757 * error to be sent on the wire. It should be
1758 * detected by the Target.
1759 */
1760 phba->lpfc_injerr_wgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001761 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1762 phba->lpfc_injerr_nportid = 0;
1763 phba->lpfc_injerr_lba =
1764 LPFC_INJERR_LBA_OFF;
1765 memset(&phba->lpfc_injerr_wwpn,
1766 0, sizeof(struct lpfc_name));
1767 }
James Smartacd68592012-01-18 16:25:09 -05001768
James Smart9a6b09c2012-03-01 22:37:42 -05001769 rc |= BG_ERR_TGT | BG_ERR_SWAP;
James Smartacd68592012-01-18 16:25:09 -05001770 /* Signals the caller to swap CRC->CSUM */
1771
1772 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1773 "0817 BLKGRD: Injecting guard error: "
1774 "write lba x%lx\n", (unsigned long)lba);
1775 break;
James Smart9a6b09c2012-03-01 22:37:42 -05001776 case SCSI_PROT_WRITE_STRIP:
1777 /*
1778 * For WRITE_STRIP and WRITE_PASS,
1779 * force the error on data
1780 * being copied from SLI-Host to SLI-Port.
1781 */
1782 phba->lpfc_injerr_wgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001783 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1784 phba->lpfc_injerr_nportid = 0;
1785 phba->lpfc_injerr_lba =
1786 LPFC_INJERR_LBA_OFF;
1787 memset(&phba->lpfc_injerr_wwpn,
1788 0, sizeof(struct lpfc_name));
1789 }
James Smart9a6b09c2012-03-01 22:37:42 -05001790
1791 rc = BG_ERR_INIT | BG_ERR_SWAP;
1792 /* Signals the caller to swap CRC->CSUM */
1793
1794 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1795 "0816 BLKGRD: Injecting guard error: "
1796 "write lba x%lx\n", (unsigned long)lba);
1797 break;
James Smartacd68592012-01-18 16:25:09 -05001798 }
1799 }
1800 if (phba->lpfc_injerr_rgrd_cnt) {
1801 switch (op) {
1802 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001803 case SCSI_PROT_READ_STRIP:
1804 case SCSI_PROT_READ_PASS:
1805 /*
1806 * For READ_STRIP and READ_PASS, force the
1807 * error on data being read off the wire. It
1808 * should force an IO error to the driver.
1809 */
James Smartacd68592012-01-18 16:25:09 -05001810 phba->lpfc_injerr_rgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001811 if (phba->lpfc_injerr_rgrd_cnt == 0) {
1812 phba->lpfc_injerr_nportid = 0;
1813 phba->lpfc_injerr_lba =
1814 LPFC_INJERR_LBA_OFF;
1815 memset(&phba->lpfc_injerr_wwpn,
1816 0, sizeof(struct lpfc_name));
1817 }
James Smartacd68592012-01-18 16:25:09 -05001818
James Smart9a6b09c2012-03-01 22:37:42 -05001819 rc = BG_ERR_INIT | BG_ERR_SWAP;
James Smartacd68592012-01-18 16:25:09 -05001820 /* Signals the caller to swap CRC->CSUM */
1821
1822 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1823 "0818 BLKGRD: Injecting guard error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001824 "read lba x%lx\n", (unsigned long)lba);
1825 }
1826 }
1827 }
1828
James Smartf9bb2da2011-10-10 21:34:11 -04001829 return rc;
1830}
1831#endif
1832
James Smartacd68592012-01-18 16:25:09 -05001833/**
1834 * lpfc_sc_to_bg_opcodes - Determine the BlockGuard opcodes to be used with
1835 * the specified SCSI command.
1836 * @phba: The Hba for which this call is being executed.
James Smart6c8eea52010-04-06 14:49:53 -04001837 * @sc: The SCSI command to examine
1838 * @txopt: (out) BlockGuard operation for transmitted data
1839 * @rxopt: (out) BlockGuard operation for received data
1840 *
1841 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1842 *
James Smartacd68592012-01-18 16:25:09 -05001843 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001844static int
James Smart6c8eea52010-04-06 14:49:53 -04001845lpfc_sc_to_bg_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1846 uint8_t *txop, uint8_t *rxop)
James Smarte2a0a9d2008-12-04 22:40:02 -05001847{
1848 uint8_t guard_type = scsi_host_get_guard(sc->device->host);
James Smart6c8eea52010-04-06 14:49:53 -04001849 uint8_t ret = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05001850
1851 if (guard_type == SHOST_DIX_GUARD_IP) {
1852 switch (scsi_get_prot_op(sc)) {
1853 case SCSI_PROT_READ_INSERT:
1854 case SCSI_PROT_WRITE_STRIP:
James Smart6c8eea52010-04-06 14:49:53 -04001855 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001856 *txop = BG_OP_IN_CSUM_OUT_NODIF;
James Smarte2a0a9d2008-12-04 22:40:02 -05001857 break;
1858
1859 case SCSI_PROT_READ_STRIP:
1860 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001861 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001862 *txop = BG_OP_IN_NODIF_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001863 break;
1864
Martin K. Petersenc6af4042009-09-18 17:32:59 -04001865 case SCSI_PROT_READ_PASS:
1866 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001867 *rxop = BG_OP_IN_CRC_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001868 *txop = BG_OP_IN_CSUM_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001869 break;
1870
James Smarte2a0a9d2008-12-04 22:40:02 -05001871 case SCSI_PROT_NORMAL:
1872 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001873 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001874 "9063 BLKGRD: Bad op/guard:%d/IP combination\n",
1875 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001876 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001877 break;
1878
1879 }
James Smart7c56b9f2011-07-22 18:36:25 -04001880 } else {
James Smarte2a0a9d2008-12-04 22:40:02 -05001881 switch (scsi_get_prot_op(sc)) {
1882 case SCSI_PROT_READ_STRIP:
1883 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001884 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001885 *txop = BG_OP_IN_NODIF_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001886 break;
1887
1888 case SCSI_PROT_READ_PASS:
1889 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001890 *rxop = BG_OP_IN_CRC_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001891 *txop = BG_OP_IN_CRC_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001892 break;
1893
James Smarte2a0a9d2008-12-04 22:40:02 -05001894 case SCSI_PROT_READ_INSERT:
1895 case SCSI_PROT_WRITE_STRIP:
James Smart7c56b9f2011-07-22 18:36:25 -04001896 *rxop = BG_OP_IN_NODIF_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001897 *txop = BG_OP_IN_CRC_OUT_NODIF;
James Smart7c56b9f2011-07-22 18:36:25 -04001898 break;
1899
James Smarte2a0a9d2008-12-04 22:40:02 -05001900 case SCSI_PROT_NORMAL:
1901 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001902 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001903 "9075 BLKGRD: Bad op/guard:%d/CRC combination\n",
1904 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001905 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001906 break;
1907 }
James Smarte2a0a9d2008-12-04 22:40:02 -05001908 }
1909
James Smart6c8eea52010-04-06 14:49:53 -04001910 return ret;
James Smarte2a0a9d2008-12-04 22:40:02 -05001911}
1912
James Smartacd68592012-01-18 16:25:09 -05001913#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1914/**
1915 * lpfc_bg_err_opcodes - reDetermine the BlockGuard opcodes to be used with
1916 * the specified SCSI command in order to force a guard tag error.
1917 * @phba: The Hba for which this call is being executed.
1918 * @sc: The SCSI command to examine
1919 * @txopt: (out) BlockGuard operation for transmitted data
1920 * @rxopt: (out) BlockGuard operation for received data
1921 *
1922 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1923 *
1924 **/
1925static int
1926lpfc_bg_err_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1927 uint8_t *txop, uint8_t *rxop)
1928{
1929 uint8_t guard_type = scsi_host_get_guard(sc->device->host);
1930 uint8_t ret = 0;
1931
1932 if (guard_type == SHOST_DIX_GUARD_IP) {
1933 switch (scsi_get_prot_op(sc)) {
1934 case SCSI_PROT_READ_INSERT:
1935 case SCSI_PROT_WRITE_STRIP:
James Smartacd68592012-01-18 16:25:09 -05001936 *rxop = BG_OP_IN_NODIF_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001937 *txop = BG_OP_IN_CRC_OUT_NODIF;
James Smartacd68592012-01-18 16:25:09 -05001938 break;
1939
1940 case SCSI_PROT_READ_STRIP:
1941 case SCSI_PROT_WRITE_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001942 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001943 *txop = BG_OP_IN_NODIF_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001944 break;
1945
1946 case SCSI_PROT_READ_PASS:
1947 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001948 *rxop = BG_OP_IN_CSUM_OUT_CRC;
James Smart9a6b09c2012-03-01 22:37:42 -05001949 *txop = BG_OP_IN_CRC_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001950 break;
1951
1952 case SCSI_PROT_NORMAL:
1953 default:
1954 break;
1955
1956 }
1957 } else {
1958 switch (scsi_get_prot_op(sc)) {
1959 case SCSI_PROT_READ_STRIP:
1960 case SCSI_PROT_WRITE_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001961 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001962 *txop = BG_OP_IN_NODIF_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001963 break;
1964
1965 case SCSI_PROT_READ_PASS:
1966 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001967 *rxop = BG_OP_IN_CSUM_OUT_CSUM;
James Smart9a6b09c2012-03-01 22:37:42 -05001968 *txop = BG_OP_IN_CSUM_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001969 break;
1970
1971 case SCSI_PROT_READ_INSERT:
1972 case SCSI_PROT_WRITE_STRIP:
James Smartacd68592012-01-18 16:25:09 -05001973 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001974 *txop = BG_OP_IN_CSUM_OUT_NODIF;
James Smartacd68592012-01-18 16:25:09 -05001975 break;
1976
1977 case SCSI_PROT_NORMAL:
1978 default:
1979 break;
1980 }
1981 }
1982
1983 return ret;
1984}
1985#endif
1986
1987/**
1988 * lpfc_bg_setup_bpl - Setup BlockGuard BPL with no protection data
1989 * @phba: The Hba for which this call is being executed.
1990 * @sc: pointer to scsi command we're working on
1991 * @bpl: pointer to buffer list for protection groups
1992 * @datacnt: number of segments of data that have been dma mapped
1993 *
1994 * This function sets up BPL buffer list for protection groups of
James Smarte2a0a9d2008-12-04 22:40:02 -05001995 * type LPFC_PG_TYPE_NO_DIF
1996 *
1997 * This is usually used when the HBA is instructed to generate
1998 * DIFs and insert them into data stream (or strip DIF from
1999 * incoming data stream)
2000 *
2001 * The buffer list consists of just one protection group described
2002 * below:
2003 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002004 * start of prot group --> | PDE_5 |
2005 * +-------------------------+
2006 * | PDE_6 |
James Smarte2a0a9d2008-12-04 22:40:02 -05002007 * +-------------------------+
2008 * | Data BDE |
2009 * +-------------------------+
2010 * |more Data BDE's ... (opt)|
2011 * +-------------------------+
2012 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002013 *
2014 * Note: Data s/g buffers have been dma mapped
James Smartacd68592012-01-18 16:25:09 -05002015 *
2016 * Returns the number of BDEs added to the BPL.
2017 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002018static int
2019lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2020 struct ulp_bde64 *bpl, int datasegcnt)
2021{
2022 struct scatterlist *sgde = NULL; /* s/g data entry */
James Smart6c8eea52010-04-06 14:49:53 -04002023 struct lpfc_pde5 *pde5 = NULL;
2024 struct lpfc_pde6 *pde6 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05002025 dma_addr_t physaddr;
James Smart6c8eea52010-04-06 14:49:53 -04002026 int i = 0, num_bde = 0, status;
James Smarte2a0a9d2008-12-04 22:40:02 -05002027 int datadir = sc->sc_data_direction;
James Smart0829a192012-05-09 21:18:12 -04002028#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002029 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002030#endif
James Smartacd68592012-01-18 16:25:09 -05002031 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05002032 uint32_t reftag;
James Smart7c56b9f2011-07-22 18:36:25 -04002033 unsigned blksize;
James Smart6c8eea52010-04-06 14:49:53 -04002034 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05002035
James Smart6c8eea52010-04-06 14:49:53 -04002036 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2037 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05002038 goto out;
2039
James Smart6c8eea52010-04-06 14:49:53 -04002040 /* extract some info from the scsi command for pde*/
James Smarte2a0a9d2008-12-04 22:40:02 -05002041 blksize = lpfc_cmd_blksize(sc);
James Smartacd68592012-01-18 16:25:09 -05002042 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
James Smarte2a0a9d2008-12-04 22:40:02 -05002043
James Smartf9bb2da2011-10-10 21:34:11 -04002044#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002045 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002046 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002047 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002048 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002049 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002050 checking = 0;
2051 }
James Smartf9bb2da2011-10-10 21:34:11 -04002052#endif
2053
James Smart6c8eea52010-04-06 14:49:53 -04002054 /* setup PDE5 with what we have */
2055 pde5 = (struct lpfc_pde5 *) bpl;
2056 memset(pde5, 0, sizeof(struct lpfc_pde5));
2057 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05002058
James Smartbc739052010-08-04 16:11:18 -04002059 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04002060 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04002061 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04002062
James Smart6c8eea52010-04-06 14:49:53 -04002063 /* advance bpl and increment bde count */
2064 num_bde++;
2065 bpl++;
2066 pde6 = (struct lpfc_pde6 *) bpl;
2067
2068 /* setup PDE6 with the rest of the info */
2069 memset(pde6, 0, sizeof(struct lpfc_pde6));
2070 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
2071 bf_set(pde6_optx, pde6, txop);
2072 bf_set(pde6_oprx, pde6, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002073
2074 /*
2075 * We only need to check the data on READs, for WRITEs
2076 * protection data is automatically generated, not checked.
2077 */
James Smart6c8eea52010-04-06 14:49:53 -04002078 if (datadir == DMA_FROM_DEVICE) {
James Smarta6887e22013-04-17 20:18:07 -04002079 if (scsi_prot_flagged(sc, SCSI_PROT_GUARD_CHECK))
2080 bf_set(pde6_ce, pde6, checking);
2081 else
2082 bf_set(pde6_ce, pde6, 0);
2083
2084 if (scsi_prot_flagged(sc, SCSI_PROT_REF_CHECK))
2085 bf_set(pde6_re, pde6, checking);
2086 else
2087 bf_set(pde6_re, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04002088 }
2089 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04002090 bf_set(pde6_ae, pde6, 0);
2091 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04002092
James Smartbc739052010-08-04 16:11:18 -04002093 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04002094 pde6->word0 = cpu_to_le32(pde6->word0);
2095 pde6->word1 = cpu_to_le32(pde6->word1);
2096 pde6->word2 = cpu_to_le32(pde6->word2);
2097
James Smart6c8eea52010-04-06 14:49:53 -04002098 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05002099 num_bde++;
2100 bpl++;
2101
2102 /* assumption: caller has already run dma_map_sg on command data */
2103 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
2104 physaddr = sg_dma_address(sgde);
2105 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
2106 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
2107 bpl->tus.f.bdeSize = sg_dma_len(sgde);
2108 if (datadir == DMA_TO_DEVICE)
2109 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2110 else
2111 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2112 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2113 bpl++;
2114 num_bde++;
2115 }
2116
2117out:
2118 return num_bde;
2119}
2120
James Smartacd68592012-01-18 16:25:09 -05002121/**
2122 * lpfc_bg_setup_bpl_prot - Setup BlockGuard BPL with protection data
2123 * @phba: The Hba for which this call is being executed.
2124 * @sc: pointer to scsi command we're working on
2125 * @bpl: pointer to buffer list for protection groups
2126 * @datacnt: number of segments of data that have been dma mapped
2127 * @protcnt: number of segment of protection data that have been dma mapped
2128 *
2129 * This function sets up BPL buffer list for protection groups of
2130 * type LPFC_PG_TYPE_DIF
James Smarte2a0a9d2008-12-04 22:40:02 -05002131 *
2132 * This is usually used when DIFs are in their own buffers,
2133 * separate from the data. The HBA can then by instructed
2134 * to place the DIFs in the outgoing stream. For read operations,
2135 * The HBA could extract the DIFs and place it in DIF buffers.
2136 *
2137 * The buffer list for this type consists of one or more of the
2138 * protection groups described below:
2139 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002140 * start of first prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05002141 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002142 * | PDE_6 |
2143 * +-------------------------+
2144 * | PDE_7 (Prot BDE) |
James Smarte2a0a9d2008-12-04 22:40:02 -05002145 * +-------------------------+
2146 * | Data BDE |
2147 * +-------------------------+
2148 * |more Data BDE's ... (opt)|
2149 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002150 * start of new prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05002151 * +-------------------------+
2152 * | ... |
2153 * +-------------------------+
2154 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002155 * Note: It is assumed that both data and protection s/g buffers have been
2156 * mapped for DMA
James Smartacd68592012-01-18 16:25:09 -05002157 *
2158 * Returns the number of BDEs added to the BPL.
2159 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002160static int
2161lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2162 struct ulp_bde64 *bpl, int datacnt, int protcnt)
2163{
2164 struct scatterlist *sgde = NULL; /* s/g data entry */
2165 struct scatterlist *sgpe = NULL; /* s/g prot entry */
James Smart6c8eea52010-04-06 14:49:53 -04002166 struct lpfc_pde5 *pde5 = NULL;
2167 struct lpfc_pde6 *pde6 = NULL;
James Smart7f860592011-03-11 16:05:52 -05002168 struct lpfc_pde7 *pde7 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05002169 dma_addr_t dataphysaddr, protphysaddr;
2170 unsigned short curr_data = 0, curr_prot = 0;
James Smart7f860592011-03-11 16:05:52 -05002171 unsigned int split_offset;
2172 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
James Smarte2a0a9d2008-12-04 22:40:02 -05002173 unsigned int protgrp_blks, protgrp_bytes;
2174 unsigned int remainder, subtotal;
James Smart6c8eea52010-04-06 14:49:53 -04002175 int status;
James Smarte2a0a9d2008-12-04 22:40:02 -05002176 int datadir = sc->sc_data_direction;
2177 unsigned char pgdone = 0, alldone = 0;
2178 unsigned blksize;
James Smart0829a192012-05-09 21:18:12 -04002179#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002180 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002181#endif
James Smartacd68592012-01-18 16:25:09 -05002182 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05002183 uint32_t reftag;
James Smart6c8eea52010-04-06 14:49:53 -04002184 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05002185 int num_bde = 0;
2186
2187 sgpe = scsi_prot_sglist(sc);
2188 sgde = scsi_sglist(sc);
2189
2190 if (!sgpe || !sgde) {
2191 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2192 "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
2193 sgpe, sgde);
2194 return 0;
2195 }
2196
James Smart6c8eea52010-04-06 14:49:53 -04002197 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2198 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05002199 goto out;
2200
James Smart6c8eea52010-04-06 14:49:53 -04002201 /* extract some info from the scsi command */
James Smarte2a0a9d2008-12-04 22:40:02 -05002202 blksize = lpfc_cmd_blksize(sc);
James Smartacd68592012-01-18 16:25:09 -05002203 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
James Smarte2a0a9d2008-12-04 22:40:02 -05002204
James Smartf9bb2da2011-10-10 21:34:11 -04002205#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002206 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002207 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002208 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002209 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002210 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002211 checking = 0;
2212 }
James Smartf9bb2da2011-10-10 21:34:11 -04002213#endif
2214
James Smarte2a0a9d2008-12-04 22:40:02 -05002215 split_offset = 0;
2216 do {
James Smart96f70772013-04-17 20:16:15 -04002217 /* Check to see if we ran out of space */
2218 if (num_bde >= (phba->cfg_total_seg_cnt - 2))
2219 return num_bde + 3;
2220
James Smart6c8eea52010-04-06 14:49:53 -04002221 /* setup PDE5 with what we have */
2222 pde5 = (struct lpfc_pde5 *) bpl;
2223 memset(pde5, 0, sizeof(struct lpfc_pde5));
2224 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05002225
James Smartbc739052010-08-04 16:11:18 -04002226 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04002227 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04002228 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04002229
James Smart6c8eea52010-04-06 14:49:53 -04002230 /* advance bpl and increment bde count */
2231 num_bde++;
2232 bpl++;
2233 pde6 = (struct lpfc_pde6 *) bpl;
James Smarte2a0a9d2008-12-04 22:40:02 -05002234
James Smart6c8eea52010-04-06 14:49:53 -04002235 /* setup PDE6 with the rest of the info */
2236 memset(pde6, 0, sizeof(struct lpfc_pde6));
2237 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
2238 bf_set(pde6_optx, pde6, txop);
2239 bf_set(pde6_oprx, pde6, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002240
2241 if (scsi_prot_flagged(sc, SCSI_PROT_GUARD_CHECK))
2242 bf_set(pde6_ce, pde6, checking);
2243 else
2244 bf_set(pde6_ce, pde6, 0);
2245
2246 if (scsi_prot_flagged(sc, SCSI_PROT_REF_CHECK))
2247 bf_set(pde6_re, pde6, checking);
2248 else
2249 bf_set(pde6_re, pde6, 0);
2250
James Smart6c8eea52010-04-06 14:49:53 -04002251 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04002252 bf_set(pde6_ae, pde6, 0);
2253 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04002254
James Smartbc739052010-08-04 16:11:18 -04002255 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04002256 pde6->word0 = cpu_to_le32(pde6->word0);
2257 pde6->word1 = cpu_to_le32(pde6->word1);
2258 pde6->word2 = cpu_to_le32(pde6->word2);
2259
James Smart6c8eea52010-04-06 14:49:53 -04002260 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05002261 num_bde++;
2262 bpl++;
2263
2264 /* setup the first BDE that points to protection buffer */
James Smart7f860592011-03-11 16:05:52 -05002265 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2266 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
James Smarte2a0a9d2008-12-04 22:40:02 -05002267
James Smarte2a0a9d2008-12-04 22:40:02 -05002268 /* must be integer multiple of the DIF block length */
2269 BUG_ON(protgroup_len % 8);
2270
James Smart7f860592011-03-11 16:05:52 -05002271 pde7 = (struct lpfc_pde7 *) bpl;
2272 memset(pde7, 0, sizeof(struct lpfc_pde7));
2273 bf_set(pde7_type, pde7, LPFC_PDE7_DESCRIPTOR);
2274
James Smart7c56b9f2011-07-22 18:36:25 -04002275 pde7->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
2276 pde7->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
James Smart7f860592011-03-11 16:05:52 -05002277
James Smarte2a0a9d2008-12-04 22:40:02 -05002278 protgrp_blks = protgroup_len / 8;
2279 protgrp_bytes = protgrp_blks * blksize;
2280
James Smart7f860592011-03-11 16:05:52 -05002281 /* check if this pde is crossing the 4K boundary; if so split */
2282 if ((pde7->addrLow & 0xfff) + protgroup_len > 0x1000) {
2283 protgroup_remainder = 0x1000 - (pde7->addrLow & 0xfff);
2284 protgroup_offset += protgroup_remainder;
2285 protgrp_blks = protgroup_remainder / 8;
James Smart7c56b9f2011-07-22 18:36:25 -04002286 protgrp_bytes = protgrp_blks * blksize;
James Smart7f860592011-03-11 16:05:52 -05002287 } else {
2288 protgroup_offset = 0;
2289 curr_prot++;
2290 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002291
James Smarte2a0a9d2008-12-04 22:40:02 -05002292 num_bde++;
2293
2294 /* setup BDE's for data blocks associated with DIF data */
2295 pgdone = 0;
2296 subtotal = 0; /* total bytes processed for current prot grp */
2297 while (!pgdone) {
James Smart96f70772013-04-17 20:16:15 -04002298 /* Check to see if we ran out of space */
2299 if (num_bde >= phba->cfg_total_seg_cnt)
2300 return num_bde + 1;
2301
James Smarte2a0a9d2008-12-04 22:40:02 -05002302 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -04002303 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2304 "9065 BLKGRD:%s Invalid data segment\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002305 __func__);
2306 return 0;
2307 }
2308 bpl++;
2309 dataphysaddr = sg_dma_address(sgde) + split_offset;
2310 bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
2311 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
2312
2313 remainder = sg_dma_len(sgde) - split_offset;
2314
2315 if ((subtotal + remainder) <= protgrp_bytes) {
2316 /* we can use this whole buffer */
2317 bpl->tus.f.bdeSize = remainder;
2318 split_offset = 0;
2319
2320 if ((subtotal + remainder) == protgrp_bytes)
2321 pgdone = 1;
2322 } else {
2323 /* must split this buffer with next prot grp */
2324 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
2325 split_offset += bpl->tus.f.bdeSize;
2326 }
2327
2328 subtotal += bpl->tus.f.bdeSize;
2329
2330 if (datadir == DMA_TO_DEVICE)
2331 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2332 else
2333 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2334 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2335
2336 num_bde++;
2337 curr_data++;
2338
2339 if (split_offset)
2340 break;
2341
2342 /* Move to the next s/g segment if possible */
2343 sgde = sg_next(sgde);
James Smart6c8eea52010-04-06 14:49:53 -04002344
James Smarte2a0a9d2008-12-04 22:40:02 -05002345 }
2346
James Smart7f860592011-03-11 16:05:52 -05002347 if (protgroup_offset) {
2348 /* update the reference tag */
2349 reftag += protgrp_blks;
2350 bpl++;
2351 continue;
2352 }
2353
James Smarte2a0a9d2008-12-04 22:40:02 -05002354 /* are we done ? */
2355 if (curr_prot == protcnt) {
2356 alldone = 1;
2357 } else if (curr_prot < protcnt) {
2358 /* advance to next prot buffer */
2359 sgpe = sg_next(sgpe);
2360 bpl++;
2361
2362 /* update the reference tag */
2363 reftag += protgrp_blks;
2364 } else {
2365 /* if we're here, we have a bug */
James Smart6a9c52c2009-10-02 15:16:51 -04002366 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2367 "9054 BLKGRD: bug in %s\n", __func__);
James Smarte2a0a9d2008-12-04 22:40:02 -05002368 }
2369
2370 } while (!alldone);
James Smarte2a0a9d2008-12-04 22:40:02 -05002371out:
2372
James Smarte2a0a9d2008-12-04 22:40:02 -05002373 return num_bde;
2374}
James Smart7f860592011-03-11 16:05:52 -05002375
James Smartacd68592012-01-18 16:25:09 -05002376/**
2377 * lpfc_bg_setup_sgl - Setup BlockGuard SGL with no protection data
2378 * @phba: The Hba for which this call is being executed.
2379 * @sc: pointer to scsi command we're working on
2380 * @sgl: pointer to buffer list for protection groups
2381 * @datacnt: number of segments of data that have been dma mapped
2382 *
2383 * This function sets up SGL buffer list for protection groups of
2384 * type LPFC_PG_TYPE_NO_DIF
2385 *
2386 * This is usually used when the HBA is instructed to generate
2387 * DIFs and insert them into data stream (or strip DIF from
2388 * incoming data stream)
2389 *
2390 * The buffer list consists of just one protection group described
2391 * below:
2392 * +-------------------------+
2393 * start of prot group --> | DI_SEED |
2394 * +-------------------------+
2395 * | Data SGE |
2396 * +-------------------------+
2397 * |more Data SGE's ... (opt)|
2398 * +-------------------------+
2399 *
2400 *
2401 * Note: Data s/g buffers have been dma mapped
2402 *
2403 * Returns the number of SGEs added to the SGL.
2404 **/
2405static int
2406lpfc_bg_setup_sgl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2407 struct sli4_sge *sgl, int datasegcnt)
2408{
2409 struct scatterlist *sgde = NULL; /* s/g data entry */
2410 struct sli4_sge_diseed *diseed = NULL;
2411 dma_addr_t physaddr;
2412 int i = 0, num_sge = 0, status;
James Smartacd68592012-01-18 16:25:09 -05002413 uint32_t reftag;
2414 unsigned blksize;
2415 uint8_t txop, rxop;
James Smart0829a192012-05-09 21:18:12 -04002416#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002417 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002418#endif
James Smartacd68592012-01-18 16:25:09 -05002419 uint32_t checking = 1;
2420 uint32_t dma_len;
2421 uint32_t dma_offset = 0;
2422
2423 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2424 if (status)
2425 goto out;
2426
2427 /* extract some info from the scsi command for pde*/
2428 blksize = lpfc_cmd_blksize(sc);
2429 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2430
2431#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002432 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002433 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002434 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002435 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002436 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002437 checking = 0;
2438 }
2439#endif
2440
2441 /* setup DISEED with what we have */
2442 diseed = (struct sli4_sge_diseed *) sgl;
2443 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2444 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2445
2446 /* Endianness conversion if necessary */
2447 diseed->ref_tag = cpu_to_le32(reftag);
2448 diseed->ref_tag_tran = diseed->ref_tag;
2449
James Smarta6887e22013-04-17 20:18:07 -04002450 /*
2451 * We only need to check the data on READs, for WRITEs
2452 * protection data is automatically generated, not checked.
2453 */
2454 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
2455 if (scsi_prot_flagged(sc, SCSI_PROT_GUARD_CHECK))
2456 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2457 else
2458 bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2459
2460 if (scsi_prot_flagged(sc, SCSI_PROT_REF_CHECK))
2461 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2462 else
2463 bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2464 }
2465
James Smartacd68592012-01-18 16:25:09 -05002466 /* setup DISEED with the rest of the info */
2467 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2468 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002469
James Smartacd68592012-01-18 16:25:09 -05002470 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2471 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2472
2473 /* Endianness conversion if necessary for DISEED */
2474 diseed->word2 = cpu_to_le32(diseed->word2);
2475 diseed->word3 = cpu_to_le32(diseed->word3);
2476
2477 /* advance bpl and increment sge count */
2478 num_sge++;
2479 sgl++;
2480
2481 /* assumption: caller has already run dma_map_sg on command data */
2482 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
2483 physaddr = sg_dma_address(sgde);
2484 dma_len = sg_dma_len(sgde);
2485 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
2486 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
2487 if ((i + 1) == datasegcnt)
2488 bf_set(lpfc_sli4_sge_last, sgl, 1);
2489 else
2490 bf_set(lpfc_sli4_sge_last, sgl, 0);
2491 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2492 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2493
2494 sgl->sge_len = cpu_to_le32(dma_len);
2495 dma_offset += dma_len;
2496
2497 sgl++;
2498 num_sge++;
2499 }
2500
2501out:
2502 return num_sge;
2503}
2504
2505/**
2506 * lpfc_bg_setup_sgl_prot - Setup BlockGuard SGL with protection data
2507 * @phba: The Hba for which this call is being executed.
2508 * @sc: pointer to scsi command we're working on
2509 * @sgl: pointer to buffer list for protection groups
2510 * @datacnt: number of segments of data that have been dma mapped
2511 * @protcnt: number of segment of protection data that have been dma mapped
2512 *
2513 * This function sets up SGL buffer list for protection groups of
2514 * type LPFC_PG_TYPE_DIF
2515 *
2516 * This is usually used when DIFs are in their own buffers,
2517 * separate from the data. The HBA can then by instructed
2518 * to place the DIFs in the outgoing stream. For read operations,
2519 * The HBA could extract the DIFs and place it in DIF buffers.
2520 *
2521 * The buffer list for this type consists of one or more of the
2522 * protection groups described below:
2523 * +-------------------------+
2524 * start of first prot group --> | DISEED |
2525 * +-------------------------+
2526 * | DIF (Prot SGE) |
2527 * +-------------------------+
2528 * | Data SGE |
2529 * +-------------------------+
2530 * |more Data SGE's ... (opt)|
2531 * +-------------------------+
2532 * start of new prot group --> | DISEED |
2533 * +-------------------------+
2534 * | ... |
2535 * +-------------------------+
2536 *
2537 * Note: It is assumed that both data and protection s/g buffers have been
2538 * mapped for DMA
2539 *
2540 * Returns the number of SGEs added to the SGL.
2541 **/
2542static int
2543lpfc_bg_setup_sgl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2544 struct sli4_sge *sgl, int datacnt, int protcnt)
2545{
2546 struct scatterlist *sgde = NULL; /* s/g data entry */
2547 struct scatterlist *sgpe = NULL; /* s/g prot entry */
2548 struct sli4_sge_diseed *diseed = NULL;
2549 dma_addr_t dataphysaddr, protphysaddr;
2550 unsigned short curr_data = 0, curr_prot = 0;
2551 unsigned int split_offset;
2552 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
2553 unsigned int protgrp_blks, protgrp_bytes;
2554 unsigned int remainder, subtotal;
2555 int status;
2556 unsigned char pgdone = 0, alldone = 0;
2557 unsigned blksize;
2558 uint32_t reftag;
2559 uint8_t txop, rxop;
2560 uint32_t dma_len;
James Smart0829a192012-05-09 21:18:12 -04002561#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002562 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002563#endif
James Smartacd68592012-01-18 16:25:09 -05002564 uint32_t checking = 1;
2565 uint32_t dma_offset = 0;
2566 int num_sge = 0;
2567
2568 sgpe = scsi_prot_sglist(sc);
2569 sgde = scsi_sglist(sc);
2570
2571 if (!sgpe || !sgde) {
2572 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2573 "9082 Invalid s/g entry: data=0x%p prot=0x%p\n",
2574 sgpe, sgde);
2575 return 0;
2576 }
2577
2578 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2579 if (status)
2580 goto out;
2581
2582 /* extract some info from the scsi command */
2583 blksize = lpfc_cmd_blksize(sc);
2584 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2585
2586#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002587 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002588 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002589 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002590 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002591 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002592 checking = 0;
2593 }
2594#endif
2595
2596 split_offset = 0;
2597 do {
James Smart96f70772013-04-17 20:16:15 -04002598 /* Check to see if we ran out of space */
2599 if (num_sge >= (phba->cfg_total_seg_cnt - 2))
2600 return num_sge + 3;
2601
James Smartacd68592012-01-18 16:25:09 -05002602 /* setup DISEED with what we have */
2603 diseed = (struct sli4_sge_diseed *) sgl;
2604 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2605 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2606
2607 /* Endianness conversion if necessary */
2608 diseed->ref_tag = cpu_to_le32(reftag);
2609 diseed->ref_tag_tran = diseed->ref_tag;
2610
James Smarta6887e22013-04-17 20:18:07 -04002611 if (scsi_prot_flagged(sc, SCSI_PROT_GUARD_CHECK)) {
2612 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2613
2614 } else {
2615 bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2616 /*
2617 * When in this mode, the hardware will replace
2618 * the guard tag from the host with a
2619 * newly generated good CRC for the wire.
2620 * Switch to raw mode here to avoid this
2621 * behavior. What the host sends gets put on the wire.
2622 */
2623 if (txop == BG_OP_IN_CRC_OUT_CRC) {
2624 txop = BG_OP_RAW_MODE;
2625 rxop = BG_OP_RAW_MODE;
2626 }
2627 }
2628
2629
2630 if (scsi_prot_flagged(sc, SCSI_PROT_REF_CHECK))
2631 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2632 else
2633 bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2634
James Smartacd68592012-01-18 16:25:09 -05002635 /* setup DISEED with the rest of the info */
2636 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2637 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002638
James Smartacd68592012-01-18 16:25:09 -05002639 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2640 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2641
2642 /* Endianness conversion if necessary for DISEED */
2643 diseed->word2 = cpu_to_le32(diseed->word2);
2644 diseed->word3 = cpu_to_le32(diseed->word3);
2645
2646 /* advance sgl and increment bde count */
2647 num_sge++;
2648 sgl++;
2649
2650 /* setup the first BDE that points to protection buffer */
2651 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2652 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
2653
2654 /* must be integer multiple of the DIF block length */
2655 BUG_ON(protgroup_len % 8);
2656
2657 /* Now setup DIF SGE */
2658 sgl->word2 = 0;
2659 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DIF);
2660 sgl->addr_hi = le32_to_cpu(putPaddrHigh(protphysaddr));
2661 sgl->addr_lo = le32_to_cpu(putPaddrLow(protphysaddr));
2662 sgl->word2 = cpu_to_le32(sgl->word2);
2663
2664 protgrp_blks = protgroup_len / 8;
2665 protgrp_bytes = protgrp_blks * blksize;
2666
2667 /* check if DIF SGE is crossing the 4K boundary; if so split */
2668 if ((sgl->addr_lo & 0xfff) + protgroup_len > 0x1000) {
2669 protgroup_remainder = 0x1000 - (sgl->addr_lo & 0xfff);
2670 protgroup_offset += protgroup_remainder;
2671 protgrp_blks = protgroup_remainder / 8;
2672 protgrp_bytes = protgrp_blks * blksize;
2673 } else {
2674 protgroup_offset = 0;
2675 curr_prot++;
2676 }
2677
2678 num_sge++;
2679
2680 /* setup SGE's for data blocks associated with DIF data */
2681 pgdone = 0;
2682 subtotal = 0; /* total bytes processed for current prot grp */
2683 while (!pgdone) {
James Smart96f70772013-04-17 20:16:15 -04002684 /* Check to see if we ran out of space */
2685 if (num_sge >= phba->cfg_total_seg_cnt)
2686 return num_sge + 1;
2687
James Smartacd68592012-01-18 16:25:09 -05002688 if (!sgde) {
2689 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2690 "9086 BLKGRD:%s Invalid data segment\n",
2691 __func__);
2692 return 0;
2693 }
2694 sgl++;
2695 dataphysaddr = sg_dma_address(sgde) + split_offset;
2696
2697 remainder = sg_dma_len(sgde) - split_offset;
2698
2699 if ((subtotal + remainder) <= protgrp_bytes) {
2700 /* we can use this whole buffer */
2701 dma_len = remainder;
2702 split_offset = 0;
2703
2704 if ((subtotal + remainder) == protgrp_bytes)
2705 pgdone = 1;
2706 } else {
2707 /* must split this buffer with next prot grp */
2708 dma_len = protgrp_bytes - subtotal;
2709 split_offset += dma_len;
2710 }
2711
2712 subtotal += dma_len;
2713
2714 sgl->addr_lo = cpu_to_le32(putPaddrLow(dataphysaddr));
2715 sgl->addr_hi = cpu_to_le32(putPaddrHigh(dataphysaddr));
2716 bf_set(lpfc_sli4_sge_last, sgl, 0);
2717 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2718 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2719
2720 sgl->sge_len = cpu_to_le32(dma_len);
2721 dma_offset += dma_len;
2722
2723 num_sge++;
2724 curr_data++;
2725
2726 if (split_offset)
2727 break;
2728
2729 /* Move to the next s/g segment if possible */
2730 sgde = sg_next(sgde);
2731 }
2732
2733 if (protgroup_offset) {
2734 /* update the reference tag */
2735 reftag += protgrp_blks;
2736 sgl++;
2737 continue;
2738 }
2739
2740 /* are we done ? */
2741 if (curr_prot == protcnt) {
2742 bf_set(lpfc_sli4_sge_last, sgl, 1);
2743 alldone = 1;
2744 } else if (curr_prot < protcnt) {
2745 /* advance to next prot buffer */
2746 sgpe = sg_next(sgpe);
2747 sgl++;
2748
2749 /* update the reference tag */
2750 reftag += protgrp_blks;
2751 } else {
2752 /* if we're here, we have a bug */
2753 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2754 "9085 BLKGRD: bug in %s\n", __func__);
2755 }
2756
2757 } while (!alldone);
2758
2759out:
2760
2761 return num_sge;
2762}
2763
2764/**
2765 * lpfc_prot_group_type - Get prtotection group type of SCSI command
2766 * @phba: The Hba for which this call is being executed.
2767 * @sc: pointer to scsi command we're working on
2768 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002769 * Given a SCSI command that supports DIF, determine composition of protection
2770 * groups involved in setting up buffer lists
2771 *
James Smartacd68592012-01-18 16:25:09 -05002772 * Returns: Protection group type (with or without DIF)
2773 *
2774 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002775static int
2776lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
2777{
2778 int ret = LPFC_PG_TYPE_INVALID;
2779 unsigned char op = scsi_get_prot_op(sc);
2780
2781 switch (op) {
2782 case SCSI_PROT_READ_STRIP:
2783 case SCSI_PROT_WRITE_INSERT:
2784 ret = LPFC_PG_TYPE_NO_DIF;
2785 break;
2786 case SCSI_PROT_READ_INSERT:
2787 case SCSI_PROT_WRITE_STRIP:
2788 case SCSI_PROT_READ_PASS:
2789 case SCSI_PROT_WRITE_PASS:
James Smarte2a0a9d2008-12-04 22:40:02 -05002790 ret = LPFC_PG_TYPE_DIF_BUF;
2791 break;
2792 default:
2793 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2794 "9021 Unsupported protection op:%d\n", op);
2795 break;
2796 }
2797
2798 return ret;
2799}
2800
James Smartacd68592012-01-18 16:25:09 -05002801/**
James Smarta6887e22013-04-17 20:18:07 -04002802 * lpfc_bg_scsi_adjust_dl - Adjust SCSI data length for BlockGuard
2803 * @phba: The Hba for which this call is being executed.
2804 * @lpfc_cmd: The scsi buffer which is going to be adjusted.
2805 *
2806 * Adjust the data length to account for how much data
2807 * is actually on the wire.
2808 *
2809 * returns the adjusted data length
2810 **/
2811static int
2812lpfc_bg_scsi_adjust_dl(struct lpfc_hba *phba,
2813 struct lpfc_scsi_buf *lpfc_cmd)
2814{
2815 struct scsi_cmnd *sc = lpfc_cmd->pCmd;
2816 int fcpdl;
2817
2818 fcpdl = scsi_bufflen(sc);
2819
2820 /* Check if there is protection data on the wire */
2821 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
2822 /* Read */
2823 if (scsi_get_prot_op(sc) == SCSI_PROT_READ_INSERT)
2824 return fcpdl;
2825
2826 } else {
2827 /* Write */
2828 if (scsi_get_prot_op(sc) == SCSI_PROT_WRITE_STRIP)
2829 return fcpdl;
2830 }
2831
2832 /*
2833 * If we are in DIF Type 1 mode every data block has a 8 byte
2834 * DIF (trailer) attached to it. Must ajust FCP data length.
2835 */
2836 if (scsi_prot_flagged(sc, SCSI_PROT_TRANSFER_PI))
2837 fcpdl += (fcpdl / lpfc_cmd_blksize(sc)) * 8;
2838
2839 return fcpdl;
2840}
2841
2842/**
James Smartacd68592012-01-18 16:25:09 -05002843 * lpfc_bg_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
2844 * @phba: The Hba for which this call is being executed.
2845 * @lpfc_cmd: The scsi buffer which is going to be prep'ed.
2846 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002847 * This is the protection/DIF aware version of
2848 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
2849 * two functions eventually, but for now, it's here
James Smartacd68592012-01-18 16:25:09 -05002850 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002851static int
James Smartacd68592012-01-18 16:25:09 -05002852lpfc_bg_scsi_prep_dma_buf_s3(struct lpfc_hba *phba,
James Smarte2a0a9d2008-12-04 22:40:02 -05002853 struct lpfc_scsi_buf *lpfc_cmd)
2854{
2855 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2856 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2857 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
2858 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2859 uint32_t num_bde = 0;
2860 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
2861 int prot_group_type = 0;
James Smarta6887e22013-04-17 20:18:07 -04002862 int fcpdl;
James Smarte2a0a9d2008-12-04 22:40:02 -05002863
2864 /*
2865 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
2866 * fcp_rsp regions to the first data bde entry
2867 */
2868 bpl += 2;
2869 if (scsi_sg_count(scsi_cmnd)) {
2870 /*
2871 * The driver stores the segment count returned from pci_map_sg
2872 * because this a count of dma-mappings used to map the use_sg
2873 * pages. They are not guaranteed to be the same for those
2874 * architectures that implement an IOMMU.
2875 */
2876 datasegcnt = dma_map_sg(&phba->pcidev->dev,
2877 scsi_sglist(scsi_cmnd),
2878 scsi_sg_count(scsi_cmnd), datadir);
2879 if (unlikely(!datasegcnt))
2880 return 1;
2881
2882 lpfc_cmd->seg_cnt = datasegcnt;
James Smart96f70772013-04-17 20:16:15 -04002883
2884 /* First check if data segment count from SCSI Layer is good */
2885 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt)
2886 goto err;
James Smarte2a0a9d2008-12-04 22:40:02 -05002887
2888 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
2889
2890 switch (prot_group_type) {
2891 case LPFC_PG_TYPE_NO_DIF:
James Smart96f70772013-04-17 20:16:15 -04002892
2893 /* Here we need to add a PDE5 and PDE6 to the count */
2894 if ((lpfc_cmd->seg_cnt + 2) > phba->cfg_total_seg_cnt)
2895 goto err;
2896
James Smarte2a0a9d2008-12-04 22:40:02 -05002897 num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
2898 datasegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002899 /* we should have 2 or more entries in buffer list */
James Smarte2a0a9d2008-12-04 22:40:02 -05002900 if (num_bde < 2)
2901 goto err;
2902 break;
James Smart96f70772013-04-17 20:16:15 -04002903
2904 case LPFC_PG_TYPE_DIF_BUF:
James Smarte2a0a9d2008-12-04 22:40:02 -05002905 /*
2906 * This type indicates that protection buffers are
2907 * passed to the driver, so that needs to be prepared
2908 * for DMA
2909 */
2910 protsegcnt = dma_map_sg(&phba->pcidev->dev,
2911 scsi_prot_sglist(scsi_cmnd),
2912 scsi_prot_sg_count(scsi_cmnd), datadir);
2913 if (unlikely(!protsegcnt)) {
2914 scsi_dma_unmap(scsi_cmnd);
2915 return 1;
2916 }
2917
2918 lpfc_cmd->prot_seg_cnt = protsegcnt;
James Smart96f70772013-04-17 20:16:15 -04002919
2920 /*
2921 * There is a minimun of 4 BPLs used for every
2922 * protection data segment.
2923 */
2924 if ((lpfc_cmd->prot_seg_cnt * 4) >
2925 (phba->cfg_total_seg_cnt - 2))
2926 goto err;
James Smarte2a0a9d2008-12-04 22:40:02 -05002927
2928 num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
2929 datasegcnt, protsegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002930 /* we should have 3 or more entries in buffer list */
James Smart96f70772013-04-17 20:16:15 -04002931 if ((num_bde < 3) ||
2932 (num_bde > phba->cfg_total_seg_cnt))
James Smarte2a0a9d2008-12-04 22:40:02 -05002933 goto err;
2934 break;
James Smart96f70772013-04-17 20:16:15 -04002935
James Smarte2a0a9d2008-12-04 22:40:02 -05002936 case LPFC_PG_TYPE_INVALID:
2937 default:
James Smart96f70772013-04-17 20:16:15 -04002938 scsi_dma_unmap(scsi_cmnd);
2939 lpfc_cmd->seg_cnt = 0;
2940
James Smarte2a0a9d2008-12-04 22:40:02 -05002941 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2942 "9022 Unexpected protection group %i\n",
2943 prot_group_type);
2944 return 1;
2945 }
2946 }
2947
2948 /*
2949 * Finish initializing those IOCB fields that are dependent on the
2950 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
2951 * reinitialized since all iocb memory resources are used many times
2952 * for transmit, receive, and continuation bpl's.
2953 */
2954 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
2955 iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
2956 iocb_cmd->ulpBdeCount = 1;
2957 iocb_cmd->ulpLe = 1;
2958
James Smarta6887e22013-04-17 20:18:07 -04002959 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002960 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
2961
2962 /*
2963 * Due to difference in data length between DIF/non-DIF paths,
2964 * we need to set word 4 of IOCB here
2965 */
2966 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
2967
2968 return 0;
2969err:
James Smart96f70772013-04-17 20:16:15 -04002970 if (lpfc_cmd->seg_cnt)
2971 scsi_dma_unmap(scsi_cmnd);
2972 if (lpfc_cmd->prot_seg_cnt)
2973 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
2974 scsi_prot_sg_count(scsi_cmnd),
2975 scsi_cmnd->sc_data_direction);
2976
James Smarte2a0a9d2008-12-04 22:40:02 -05002977 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart96f70772013-04-17 20:16:15 -04002978 "9023 Cannot setup S/G List for HBA"
2979 "IO segs %d/%d BPL %d SCSI %d: %d %d\n",
2980 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
2981 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
James Smarte2a0a9d2008-12-04 22:40:02 -05002982 prot_group_type, num_bde);
James Smart96f70772013-04-17 20:16:15 -04002983
2984 lpfc_cmd->seg_cnt = 0;
2985 lpfc_cmd->prot_seg_cnt = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05002986 return 1;
2987}
2988
2989/*
James Smart737d4242013-04-17 20:14:49 -04002990 * This function calcuates the T10 DIF guard tag
2991 * on the specified data using a CRC algorithmn
2992 * using crc_t10dif.
2993 */
2994uint16_t
2995lpfc_bg_crc(uint8_t *data, int count)
2996{
2997 uint16_t crc = 0;
2998 uint16_t x;
2999
3000 crc = crc_t10dif(data, count);
3001 x = cpu_to_be16(crc);
3002 return x;
3003}
3004
3005/*
3006 * This function calcuates the T10 DIF guard tag
3007 * on the specified data using a CSUM algorithmn
3008 * using ip_compute_csum.
3009 */
3010uint16_t
3011lpfc_bg_csum(uint8_t *data, int count)
3012{
3013 uint16_t ret;
3014
3015 ret = ip_compute_csum(data, count);
3016 return ret;
3017}
3018
3019/*
3020 * This function examines the protection data to try to determine
3021 * what type of T10-DIF error occurred.
3022 */
3023void
3024lpfc_calc_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3025{
3026 struct scatterlist *sgpe; /* s/g prot entry */
3027 struct scatterlist *sgde; /* s/g data entry */
3028 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
3029 struct scsi_dif_tuple *src = NULL;
3030 uint8_t *data_src = NULL;
3031 uint16_t guard_tag, guard_type;
3032 uint16_t start_app_tag, app_tag;
3033 uint32_t start_ref_tag, ref_tag;
3034 int prot, protsegcnt;
3035 int err_type, len, data_len;
3036 int chk_ref, chk_app, chk_guard;
3037 uint16_t sum;
3038 unsigned blksize;
3039
3040 err_type = BGS_GUARD_ERR_MASK;
3041 sum = 0;
3042 guard_tag = 0;
3043
3044 /* First check to see if there is protection data to examine */
3045 prot = scsi_get_prot_op(cmd);
3046 if ((prot == SCSI_PROT_READ_STRIP) ||
3047 (prot == SCSI_PROT_WRITE_INSERT) ||
3048 (prot == SCSI_PROT_NORMAL))
3049 goto out;
3050
3051 /* Currently the driver just supports ref_tag and guard_tag checking */
3052 chk_ref = 1;
3053 chk_app = 0;
3054 chk_guard = 0;
3055
3056 /* Setup a ptr to the protection data provided by the SCSI host */
3057 sgpe = scsi_prot_sglist(cmd);
3058 protsegcnt = lpfc_cmd->prot_seg_cnt;
3059
3060 if (sgpe && protsegcnt) {
3061
3062 /*
3063 * We will only try to verify guard tag if the segment
3064 * data length is a multiple of the blksize.
3065 */
3066 sgde = scsi_sglist(cmd);
3067 blksize = lpfc_cmd_blksize(cmd);
3068 data_src = (uint8_t *)sg_virt(sgde);
3069 data_len = sgde->length;
3070 if ((data_len & (blksize - 1)) == 0)
3071 chk_guard = 1;
3072 guard_type = scsi_host_get_guard(cmd->device->host);
3073
James Smarta6887e22013-04-17 20:18:07 -04003074 start_ref_tag = (uint32_t)scsi_get_lba(cmd); /* Truncate LBA */
James Smart737d4242013-04-17 20:14:49 -04003075 start_app_tag = src->app_tag;
3076 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
3077 len = sgpe->length;
3078 while (src && protsegcnt) {
3079 while (len) {
3080
3081 /*
3082 * First check to see if a protection data
3083 * check is valid
3084 */
3085 if ((src->ref_tag == 0xffffffff) ||
3086 (src->app_tag == 0xffff)) {
3087 start_ref_tag++;
3088 goto skipit;
3089 }
3090
3091 /* App Tag checking */
3092 app_tag = src->app_tag;
3093 if (chk_app && (app_tag != start_app_tag)) {
3094 err_type = BGS_APPTAG_ERR_MASK;
3095 goto out;
3096 }
3097
3098 /* Reference Tag checking */
3099 ref_tag = be32_to_cpu(src->ref_tag);
3100 if (chk_ref && (ref_tag != start_ref_tag)) {
3101 err_type = BGS_REFTAG_ERR_MASK;
3102 goto out;
3103 }
3104 start_ref_tag++;
3105
3106 /* Guard Tag checking */
3107 if (chk_guard) {
3108 guard_tag = src->guard_tag;
3109 if (guard_type == SHOST_DIX_GUARD_IP)
3110 sum = lpfc_bg_csum(data_src,
3111 blksize);
3112 else
3113 sum = lpfc_bg_crc(data_src,
3114 blksize);
3115 if ((guard_tag != sum)) {
3116 err_type = BGS_GUARD_ERR_MASK;
3117 goto out;
3118 }
3119 }
3120skipit:
3121 len -= sizeof(struct scsi_dif_tuple);
3122 if (len < 0)
3123 len = 0;
3124 src++;
3125
3126 data_src += blksize;
3127 data_len -= blksize;
3128
3129 /*
3130 * Are we at the end of the Data segment?
3131 * The data segment is only used for Guard
3132 * tag checking.
3133 */
3134 if (chk_guard && (data_len == 0)) {
3135 chk_guard = 0;
3136 sgde = sg_next(sgde);
3137 if (!sgde)
3138 goto out;
3139
3140 data_src = (uint8_t *)sg_virt(sgde);
3141 data_len = sgde->length;
3142 if ((data_len & (blksize - 1)) == 0)
3143 chk_guard = 1;
3144 }
3145 }
3146
3147 /* Goto the next Protection data segment */
3148 sgpe = sg_next(sgpe);
3149 if (sgpe) {
3150 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
3151 len = sgpe->length;
3152 } else {
3153 src = NULL;
3154 }
3155 protsegcnt--;
3156 }
3157 }
3158out:
3159 if (err_type == BGS_GUARD_ERR_MASK) {
3160 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3161 0x10, 0x1);
3162 cmd->result = DRIVER_SENSE << 24
3163 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3164 phba->bg_guard_err_cnt++;
3165 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3166 "9069 BLKGRD: LBA %lx grd_tag error %x != %x\n",
3167 (unsigned long)scsi_get_lba(cmd),
3168 sum, guard_tag);
3169
3170 } else if (err_type == BGS_REFTAG_ERR_MASK) {
3171 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3172 0x10, 0x3);
3173 cmd->result = DRIVER_SENSE << 24
3174 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3175
3176 phba->bg_reftag_err_cnt++;
3177 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3178 "9066 BLKGRD: LBA %lx ref_tag error %x != %x\n",
3179 (unsigned long)scsi_get_lba(cmd),
3180 ref_tag, start_ref_tag);
3181
3182 } else if (err_type == BGS_APPTAG_ERR_MASK) {
3183 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3184 0x10, 0x2);
3185 cmd->result = DRIVER_SENSE << 24
3186 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3187
3188 phba->bg_apptag_err_cnt++;
3189 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3190 "9041 BLKGRD: LBA %lx app_tag error %x != %x\n",
3191 (unsigned long)scsi_get_lba(cmd),
3192 app_tag, start_app_tag);
3193 }
3194}
3195
3196
3197/*
James Smarte2a0a9d2008-12-04 22:40:02 -05003198 * This function checks for BlockGuard errors detected by
3199 * the HBA. In case of errors, the ASC/ASCQ fields in the
3200 * sense buffer will be set accordingly, paired with
3201 * ILLEGAL_REQUEST to signal to the kernel that the HBA
3202 * detected corruption.
3203 *
3204 * Returns:
3205 * 0 - No error found
3206 * 1 - BlockGuard error found
3207 * -1 - Internal error (bad profile, ...etc)
3208 */
3209static int
3210lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
3211 struct lpfc_iocbq *pIocbOut)
3212{
3213 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
3214 struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
3215 int ret = 0;
3216 uint32_t bghm = bgf->bghm;
3217 uint32_t bgstat = bgf->bgstat;
3218 uint64_t failing_sector = 0;
3219
James Smarte2a0a9d2008-12-04 22:40:02 -05003220 spin_lock(&_dump_buf_lock);
3221 if (!_dump_buf_done) {
James Smart6a9c52c2009-10-02 15:16:51 -04003222 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9070 BLKGRD: Saving"
3223 " Data for %u blocks to debugfs\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05003224 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
James Smart6a9c52c2009-10-02 15:16:51 -04003225 lpfc_debug_save_data(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003226
3227 /* If we have a prot sgl, save the DIF buffer */
3228 if (lpfc_prot_group_type(phba, cmd) ==
3229 LPFC_PG_TYPE_DIF_BUF) {
James Smart6a9c52c2009-10-02 15:16:51 -04003230 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9071 BLKGRD: "
3231 "Saving DIF for %u blocks to debugfs\n",
3232 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
3233 lpfc_debug_save_dif(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003234 }
3235
3236 _dump_buf_done = 1;
3237 }
3238 spin_unlock(&_dump_buf_lock);
3239
3240 if (lpfc_bgs_get_invalid_prof(bgstat)) {
3241 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart737d4242013-04-17 20:14:49 -04003242 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3243 "9072 BLKGRD: Invalid BG Profile in cmd"
3244 " 0x%x lba 0x%llx blk cnt 0x%x "
3245 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3246 (unsigned long long)scsi_get_lba(cmd),
3247 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003248 ret = (-1);
3249 goto out;
3250 }
3251
3252 if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
3253 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart737d4242013-04-17 20:14:49 -04003254 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3255 "9073 BLKGRD: Invalid BG PDIF Block in cmd"
3256 " 0x%x lba 0x%llx blk cnt 0x%x "
3257 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3258 (unsigned long long)scsi_get_lba(cmd),
3259 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003260 ret = (-1);
3261 goto out;
3262 }
3263
3264 if (lpfc_bgs_get_guard_err(bgstat)) {
3265 ret = 1;
3266
3267 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3268 0x10, 0x1);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05003269 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05003270 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3271 phba->bg_guard_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04003272 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3273 "9055 BLKGRD: Guard Tag error in cmd"
3274 " 0x%x lba 0x%llx blk cnt 0x%x "
3275 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3276 (unsigned long long)scsi_get_lba(cmd),
3277 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003278 }
3279
3280 if (lpfc_bgs_get_reftag_err(bgstat)) {
3281 ret = 1;
3282
3283 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3284 0x10, 0x3);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05003285 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05003286 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3287
3288 phba->bg_reftag_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04003289 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3290 "9056 BLKGRD: Ref Tag error in cmd"
3291 " 0x%x lba 0x%llx blk cnt 0x%x "
3292 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3293 (unsigned long long)scsi_get_lba(cmd),
3294 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003295 }
3296
3297 if (lpfc_bgs_get_apptag_err(bgstat)) {
3298 ret = 1;
3299
3300 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3301 0x10, 0x2);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05003302 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05003303 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3304
3305 phba->bg_apptag_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04003306 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3307 "9061 BLKGRD: App Tag error in cmd"
3308 " 0x%x lba 0x%llx blk cnt 0x%x "
3309 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3310 (unsigned long long)scsi_get_lba(cmd),
3311 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003312 }
3313
3314 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
3315 /*
3316 * setup sense data descriptor 0 per SPC-4 as an information
James Smart7c56b9f2011-07-22 18:36:25 -04003317 * field, and put the failing LBA in it.
3318 * This code assumes there was also a guard/app/ref tag error
3319 * indication.
James Smarte2a0a9d2008-12-04 22:40:02 -05003320 */
James Smart7c56b9f2011-07-22 18:36:25 -04003321 cmd->sense_buffer[7] = 0xc; /* Additional sense length */
3322 cmd->sense_buffer[8] = 0; /* Information descriptor type */
3323 cmd->sense_buffer[9] = 0xa; /* Additional descriptor length */
3324 cmd->sense_buffer[10] = 0x80; /* Validity bit */
James Smartacd68592012-01-18 16:25:09 -05003325
3326 /* bghm is a "on the wire" FC frame based count */
3327 switch (scsi_get_prot_op(cmd)) {
3328 case SCSI_PROT_READ_INSERT:
3329 case SCSI_PROT_WRITE_STRIP:
3330 bghm /= cmd->device->sector_size;
3331 break;
3332 case SCSI_PROT_READ_STRIP:
3333 case SCSI_PROT_WRITE_INSERT:
3334 case SCSI_PROT_READ_PASS:
3335 case SCSI_PROT_WRITE_PASS:
3336 bghm /= (cmd->device->sector_size +
3337 sizeof(struct scsi_dif_tuple));
3338 break;
3339 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003340
3341 failing_sector = scsi_get_lba(cmd);
3342 failing_sector += bghm;
3343
James Smart7c56b9f2011-07-22 18:36:25 -04003344 /* Descriptor Information */
3345 put_unaligned_be64(failing_sector, &cmd->sense_buffer[12]);
James Smarte2a0a9d2008-12-04 22:40:02 -05003346 }
3347
3348 if (!ret) {
3349 /* No error was reported - problem in FW? */
James Smart737d4242013-04-17 20:14:49 -04003350 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3351 "9057 BLKGRD: Unknown error in cmd"
3352 " 0x%x lba 0x%llx blk cnt 0x%x "
3353 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3354 (unsigned long long)scsi_get_lba(cmd),
3355 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003356
James Smart737d4242013-04-17 20:14:49 -04003357 /* Calcuate what type of error it was */
3358 lpfc_calc_bg_err(phba, lpfc_cmd);
3359 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003360out:
3361 return ret;
3362}
3363
James Smartea2151b2008-09-07 11:52:10 -04003364/**
James Smartda0436e2009-05-22 14:51:39 -04003365 * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3366 * @phba: The Hba for which this call is being executed.
3367 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3368 *
3369 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
3370 * field of @lpfc_cmd for device with SLI-4 interface spec.
3371 *
3372 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04003373 * 1 - Error
3374 * 0 - Success
James Smartda0436e2009-05-22 14:51:39 -04003375 **/
3376static int
3377lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3378{
3379 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3380 struct scatterlist *sgel = NULL;
3381 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3382 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
James Smartfedd3b72011-02-16 12:39:24 -05003383 struct sli4_sge *first_data_sgl;
James Smartda0436e2009-05-22 14:51:39 -04003384 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
3385 dma_addr_t physaddr;
3386 uint32_t num_bde = 0;
3387 uint32_t dma_len;
3388 uint32_t dma_offset = 0;
3389 int nseg;
James Smartfedd3b72011-02-16 12:39:24 -05003390 struct ulp_bde64 *bde;
James Smartda0436e2009-05-22 14:51:39 -04003391
3392 /*
3393 * There are three possibilities here - use scatter-gather segment, use
3394 * the single mapping, or neither. Start the lpfc command prep by
3395 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
3396 * data bde entry.
3397 */
3398 if (scsi_sg_count(scsi_cmnd)) {
3399 /*
3400 * The driver stores the segment count returned from pci_map_sg
3401 * because this a count of dma-mappings used to map the use_sg
3402 * pages. They are not guaranteed to be the same for those
3403 * architectures that implement an IOMMU.
3404 */
3405
3406 nseg = scsi_dma_map(scsi_cmnd);
3407 if (unlikely(!nseg))
3408 return 1;
3409 sgl += 1;
3410 /* clear the last flag in the fcp_rsp map entry */
3411 sgl->word2 = le32_to_cpu(sgl->word2);
3412 bf_set(lpfc_sli4_sge_last, sgl, 0);
3413 sgl->word2 = cpu_to_le32(sgl->word2);
3414 sgl += 1;
James Smartfedd3b72011-02-16 12:39:24 -05003415 first_data_sgl = sgl;
James Smartda0436e2009-05-22 14:51:39 -04003416 lpfc_cmd->seg_cnt = nseg;
3417 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04003418 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
3419 " %s: Too many sg segments from "
3420 "dma_map_sg. Config %d, seg_cnt %d\n",
3421 __func__, phba->cfg_sg_seg_cnt,
James Smartda0436e2009-05-22 14:51:39 -04003422 lpfc_cmd->seg_cnt);
James Smart96f70772013-04-17 20:16:15 -04003423 lpfc_cmd->seg_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04003424 scsi_dma_unmap(scsi_cmnd);
3425 return 1;
3426 }
3427
3428 /*
3429 * The driver established a maximum scatter-gather segment count
3430 * during probe that limits the number of sg elements in any
3431 * single scsi command. Just run through the seg_cnt and format
3432 * the sge's.
3433 * When using SLI-3 the driver will try to fit all the BDEs into
3434 * the IOCB. If it can't then the BDEs get added to a BPL as it
3435 * does for SLI-2 mode.
3436 */
3437 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
3438 physaddr = sg_dma_address(sgel);
3439 dma_len = sg_dma_len(sgel);
James Smartda0436e2009-05-22 14:51:39 -04003440 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
3441 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
James Smart05580562011-05-24 11:40:48 -04003442 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -04003443 if ((num_bde + 1) == nseg)
3444 bf_set(lpfc_sli4_sge_last, sgl, 1);
3445 else
3446 bf_set(lpfc_sli4_sge_last, sgl, 0);
3447 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
James Smartf9bb2da2011-10-10 21:34:11 -04003448 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
James Smartda0436e2009-05-22 14:51:39 -04003449 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05003450 sgl->sge_len = cpu_to_le32(dma_len);
James Smartda0436e2009-05-22 14:51:39 -04003451 dma_offset += dma_len;
3452 sgl++;
3453 }
James Smartfedd3b72011-02-16 12:39:24 -05003454 /* setup the performance hint (first data BDE) if enabled */
3455 if (phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) {
3456 bde = (struct ulp_bde64 *)
3457 &(iocb_cmd->unsli3.sli3Words[5]);
3458 bde->addrLow = first_data_sgl->addr_lo;
3459 bde->addrHigh = first_data_sgl->addr_hi;
3460 bde->tus.f.bdeSize =
3461 le32_to_cpu(first_data_sgl->sge_len);
3462 bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
3463 bde->tus.w = cpu_to_le32(bde->tus.w);
3464 }
James Smartda0436e2009-05-22 14:51:39 -04003465 } else {
3466 sgl += 1;
3467 /* clear the last flag in the fcp_rsp map entry */
3468 sgl->word2 = le32_to_cpu(sgl->word2);
3469 bf_set(lpfc_sli4_sge_last, sgl, 1);
3470 sgl->word2 = cpu_to_le32(sgl->word2);
3471 }
3472
3473 /*
3474 * Finish initializing those IOCB fields that are dependent on the
3475 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
3476 * explicitly reinitialized.
3477 * all iocb memory resources are reused.
3478 */
3479 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
3480
3481 /*
3482 * Due to difference in data length between DIF/non-DIF paths,
3483 * we need to set word 4 of IOCB here
3484 */
3485 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
3486 return 0;
3487}
3488
3489/**
James Smartacd68592012-01-18 16:25:09 -05003490 * lpfc_bg_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3491 * @phba: The Hba for which this call is being executed.
3492 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3493 *
3494 * This is the protection/DIF aware version of
3495 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
3496 * two functions eventually, but for now, it's here
3497 **/
3498static int
3499lpfc_bg_scsi_prep_dma_buf_s4(struct lpfc_hba *phba,
3500 struct lpfc_scsi_buf *lpfc_cmd)
3501{
3502 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3503 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3504 struct sli4_sge *sgl = (struct sli4_sge *)(lpfc_cmd->fcp_bpl);
3505 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart96f70772013-04-17 20:16:15 -04003506 uint32_t num_sge = 0;
James Smartacd68592012-01-18 16:25:09 -05003507 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
3508 int prot_group_type = 0;
3509 int fcpdl;
3510
3511 /*
3512 * Start the lpfc command prep by bumping the sgl beyond fcp_cmnd
James Smart96f70772013-04-17 20:16:15 -04003513 * fcp_rsp regions to the first data sge entry
James Smartacd68592012-01-18 16:25:09 -05003514 */
3515 if (scsi_sg_count(scsi_cmnd)) {
3516 /*
3517 * The driver stores the segment count returned from pci_map_sg
3518 * because this a count of dma-mappings used to map the use_sg
3519 * pages. They are not guaranteed to be the same for those
3520 * architectures that implement an IOMMU.
3521 */
3522 datasegcnt = dma_map_sg(&phba->pcidev->dev,
3523 scsi_sglist(scsi_cmnd),
3524 scsi_sg_count(scsi_cmnd), datadir);
3525 if (unlikely(!datasegcnt))
3526 return 1;
3527
3528 sgl += 1;
3529 /* clear the last flag in the fcp_rsp map entry */
3530 sgl->word2 = le32_to_cpu(sgl->word2);
3531 bf_set(lpfc_sli4_sge_last, sgl, 0);
3532 sgl->word2 = cpu_to_le32(sgl->word2);
3533
3534 sgl += 1;
3535 lpfc_cmd->seg_cnt = datasegcnt;
James Smart96f70772013-04-17 20:16:15 -04003536
3537 /* First check if data segment count from SCSI Layer is good */
3538 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt)
3539 goto err;
James Smartacd68592012-01-18 16:25:09 -05003540
3541 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
3542
3543 switch (prot_group_type) {
3544 case LPFC_PG_TYPE_NO_DIF:
James Smart96f70772013-04-17 20:16:15 -04003545 /* Here we need to add a DISEED to the count */
3546 if ((lpfc_cmd->seg_cnt + 1) > phba->cfg_total_seg_cnt)
3547 goto err;
3548
3549 num_sge = lpfc_bg_setup_sgl(phba, scsi_cmnd, sgl,
James Smartacd68592012-01-18 16:25:09 -05003550 datasegcnt);
James Smart96f70772013-04-17 20:16:15 -04003551
James Smartacd68592012-01-18 16:25:09 -05003552 /* we should have 2 or more entries in buffer list */
James Smart96f70772013-04-17 20:16:15 -04003553 if (num_sge < 2)
James Smartacd68592012-01-18 16:25:09 -05003554 goto err;
3555 break;
James Smart96f70772013-04-17 20:16:15 -04003556
3557 case LPFC_PG_TYPE_DIF_BUF:
James Smartacd68592012-01-18 16:25:09 -05003558 /*
3559 * This type indicates that protection buffers are
3560 * passed to the driver, so that needs to be prepared
3561 * for DMA
3562 */
3563 protsegcnt = dma_map_sg(&phba->pcidev->dev,
3564 scsi_prot_sglist(scsi_cmnd),
3565 scsi_prot_sg_count(scsi_cmnd), datadir);
3566 if (unlikely(!protsegcnt)) {
3567 scsi_dma_unmap(scsi_cmnd);
3568 return 1;
3569 }
3570
3571 lpfc_cmd->prot_seg_cnt = protsegcnt;
James Smart96f70772013-04-17 20:16:15 -04003572 /*
3573 * There is a minimun of 3 SGEs used for every
3574 * protection data segment.
3575 */
3576 if ((lpfc_cmd->prot_seg_cnt * 3) >
3577 (phba->cfg_total_seg_cnt - 2))
3578 goto err;
James Smartacd68592012-01-18 16:25:09 -05003579
James Smart96f70772013-04-17 20:16:15 -04003580 num_sge = lpfc_bg_setup_sgl_prot(phba, scsi_cmnd, sgl,
James Smartacd68592012-01-18 16:25:09 -05003581 datasegcnt, protsegcnt);
James Smart96f70772013-04-17 20:16:15 -04003582
James Smartacd68592012-01-18 16:25:09 -05003583 /* we should have 3 or more entries in buffer list */
James Smart96f70772013-04-17 20:16:15 -04003584 if ((num_sge < 3) ||
3585 (num_sge > phba->cfg_total_seg_cnt))
James Smartacd68592012-01-18 16:25:09 -05003586 goto err;
3587 break;
James Smart96f70772013-04-17 20:16:15 -04003588
James Smartacd68592012-01-18 16:25:09 -05003589 case LPFC_PG_TYPE_INVALID:
3590 default:
James Smart96f70772013-04-17 20:16:15 -04003591 scsi_dma_unmap(scsi_cmnd);
3592 lpfc_cmd->seg_cnt = 0;
3593
James Smartacd68592012-01-18 16:25:09 -05003594 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
3595 "9083 Unexpected protection group %i\n",
3596 prot_group_type);
3597 return 1;
3598 }
3599 }
3600
James Smart8012cc32012-10-31 14:44:49 -04003601 switch (scsi_get_prot_op(scsi_cmnd)) {
3602 case SCSI_PROT_WRITE_STRIP:
3603 case SCSI_PROT_READ_STRIP:
3604 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_STRIP;
3605 break;
3606 case SCSI_PROT_WRITE_INSERT:
3607 case SCSI_PROT_READ_INSERT:
3608 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_INSERT;
3609 break;
3610 case SCSI_PROT_WRITE_PASS:
3611 case SCSI_PROT_READ_PASS:
3612 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_PASS;
3613 break;
3614 }
3615
James Smartacd68592012-01-18 16:25:09 -05003616 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
James Smartacd68592012-01-18 16:25:09 -05003617 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
3618
3619 /*
3620 * Due to difference in data length between DIF/non-DIF paths,
3621 * we need to set word 4 of IOCB here
3622 */
3623 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
James Smartacd68592012-01-18 16:25:09 -05003624
3625 return 0;
3626err:
James Smart96f70772013-04-17 20:16:15 -04003627 if (lpfc_cmd->seg_cnt)
3628 scsi_dma_unmap(scsi_cmnd);
3629 if (lpfc_cmd->prot_seg_cnt)
3630 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
3631 scsi_prot_sg_count(scsi_cmnd),
3632 scsi_cmnd->sc_data_direction);
3633
James Smartacd68592012-01-18 16:25:09 -05003634 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart96f70772013-04-17 20:16:15 -04003635 "9084 Cannot setup S/G List for HBA"
3636 "IO segs %d/%d SGL %d SCSI %d: %d %d\n",
3637 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
3638 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
3639 prot_group_type, num_sge);
3640
3641 lpfc_cmd->seg_cnt = 0;
3642 lpfc_cmd->prot_seg_cnt = 0;
James Smartacd68592012-01-18 16:25:09 -05003643 return 1;
3644}
3645
3646/**
James Smart3772a992009-05-22 14:50:54 -04003647 * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3648 * @phba: The Hba for which this call is being executed.
3649 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3650 *
3651 * This routine wraps the actual DMA mapping function pointer from the
3652 * lpfc_hba struct.
3653 *
3654 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04003655 * 1 - Error
3656 * 0 - Success
James Smart3772a992009-05-22 14:50:54 -04003657 **/
3658static inline int
3659lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3660{
3661 return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
3662}
3663
3664/**
James Smartacd68592012-01-18 16:25:09 -05003665 * lpfc_bg_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3666 * using BlockGuard.
3667 * @phba: The Hba for which this call is being executed.
3668 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3669 *
3670 * This routine wraps the actual DMA mapping function pointer from the
3671 * lpfc_hba struct.
3672 *
3673 * Return codes:
3674 * 1 - Error
3675 * 0 - Success
3676 **/
3677static inline int
3678lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3679{
3680 return phba->lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
3681}
3682
3683/**
James Smart3621a712009-04-06 18:47:14 -04003684 * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
James Smartea2151b2008-09-07 11:52:10 -04003685 * @phba: Pointer to hba context object.
3686 * @vport: Pointer to vport object.
3687 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
3688 * @rsp_iocb: Pointer to response iocb object which reported error.
3689 *
3690 * This function posts an event when there is a SCSI command reporting
3691 * error from the scsi device.
3692 **/
3693static void
3694lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
3695 struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
3696 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3697 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
3698 uint32_t resp_info = fcprsp->rspStatus2;
3699 uint32_t scsi_status = fcprsp->rspStatus3;
3700 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
3701 struct lpfc_fast_path_event *fast_path_evt = NULL;
3702 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
3703 unsigned long flags;
3704
James Smart5989b8d2010-10-22 11:06:56 -04003705 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3706 return;
3707
James Smartea2151b2008-09-07 11:52:10 -04003708 /* If there is queuefull or busy condition send a scsi event */
3709 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
3710 (cmnd->result == SAM_STAT_BUSY)) {
3711 fast_path_evt = lpfc_alloc_fast_evt(phba);
3712 if (!fast_path_evt)
3713 return;
3714 fast_path_evt->un.scsi_evt.event_type =
3715 FC_REG_SCSI_EVENT;
3716 fast_path_evt->un.scsi_evt.subcategory =
3717 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
3718 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
3719 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
3720 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
3721 &pnode->nlp_portname, sizeof(struct lpfc_name));
3722 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
3723 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3724 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
3725 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
3726 fast_path_evt = lpfc_alloc_fast_evt(phba);
3727 if (!fast_path_evt)
3728 return;
3729 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
3730 FC_REG_SCSI_EVENT;
3731 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
3732 LPFC_EVENT_CHECK_COND;
3733 fast_path_evt->un.check_cond_evt.scsi_event.lun =
3734 cmnd->device->lun;
3735 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
3736 &pnode->nlp_portname, sizeof(struct lpfc_name));
3737 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
3738 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3739 fast_path_evt->un.check_cond_evt.sense_key =
3740 cmnd->sense_buffer[2] & 0xf;
3741 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
3742 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
3743 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3744 fcpi_parm &&
3745 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
3746 ((scsi_status == SAM_STAT_GOOD) &&
3747 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
3748 /*
3749 * If status is good or resid does not match with fcp_param and
3750 * there is valid fcpi_parm, then there is a read_check error
3751 */
3752 fast_path_evt = lpfc_alloc_fast_evt(phba);
3753 if (!fast_path_evt)
3754 return;
3755 fast_path_evt->un.read_check_error.header.event_type =
3756 FC_REG_FABRIC_EVENT;
3757 fast_path_evt->un.read_check_error.header.subcategory =
3758 LPFC_EVENT_FCPRDCHKERR;
3759 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
3760 &pnode->nlp_portname, sizeof(struct lpfc_name));
3761 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
3762 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3763 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
3764 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
3765 fast_path_evt->un.read_check_error.fcpiparam =
3766 fcpi_parm;
3767 } else
3768 return;
3769
3770 fast_path_evt->vport = vport;
3771 spin_lock_irqsave(&phba->hbalock, flags);
3772 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
3773 spin_unlock_irqrestore(&phba->hbalock, flags);
3774 lpfc_worker_wake_up(phba);
3775 return;
3776}
James Smart9bad7672008-12-04 22:39:02 -05003777
3778/**
James Smartf1126682009-06-10 17:22:44 -04003779 * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
James Smart3772a992009-05-22 14:50:54 -04003780 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -05003781 * @psb: The scsi buffer which is going to be un-mapped.
3782 *
3783 * This routine does DMA un-mapping of scatter gather list of scsi command
James Smart3772a992009-05-22 14:50:54 -04003784 * field of @lpfc_cmd for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05003785 **/
dea31012005-04-17 16:05:31 -05003786static void
James Smartf1126682009-06-10 17:22:44 -04003787lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James Smartbcf4dbf2006-07-06 15:50:08 -04003788{
3789 /*
3790 * There are only two special cases to consider. (1) the scsi command
3791 * requested scatter-gather usage or (2) the scsi command allocated
3792 * a request buffer, but did not request use_sg. There is a third
3793 * case, but it does not require resource deallocation.
3794 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003795 if (psb->seg_cnt > 0)
3796 scsi_dma_unmap(psb->pCmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003797 if (psb->prot_seg_cnt > 0)
3798 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
3799 scsi_prot_sg_count(psb->pCmd),
3800 psb->pCmd->sc_data_direction);
James Smartbcf4dbf2006-07-06 15:50:08 -04003801}
3802
James Smart9bad7672008-12-04 22:39:02 -05003803/**
James Smart3621a712009-04-06 18:47:14 -04003804 * lpfc_handler_fcp_err - FCP response handler
James Smart9bad7672008-12-04 22:39:02 -05003805 * @vport: The virtual port for which this call is being executed.
3806 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
3807 * @rsp_iocb: The response IOCB which contains FCP error.
3808 *
3809 * This routine is called to process response IOCB with status field
3810 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
3811 * based upon SCSI and FCP error.
3812 **/
James Smartbcf4dbf2006-07-06 15:50:08 -04003813static void
James Smart2e0fef82007-06-17 19:56:36 -05003814lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
3815 struct lpfc_iocbq *rsp_iocb)
dea31012005-04-17 16:05:31 -05003816{
3817 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3818 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
3819 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
James Smart7054a602007-04-25 09:52:34 -04003820 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
dea31012005-04-17 16:05:31 -05003821 uint32_t resp_info = fcprsp->rspStatus2;
3822 uint32_t scsi_status = fcprsp->rspStatus3;
James Smartc7743952006-12-02 13:34:42 -05003823 uint32_t *lp;
dea31012005-04-17 16:05:31 -05003824 uint32_t host_status = DID_OK;
3825 uint32_t rsplen = 0;
James Smartc7743952006-12-02 13:34:42 -05003826 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea31012005-04-17 16:05:31 -05003827
James Smartea2151b2008-09-07 11:52:10 -04003828
dea31012005-04-17 16:05:31 -05003829 /*
3830 * If this is a task management command, there is no
3831 * scsi packet associated with this lpfc_cmd. The driver
3832 * consumes it.
3833 */
3834 if (fcpcmd->fcpCntl2) {
3835 scsi_status = 0;
3836 goto out;
3837 }
3838
James Smart6a9c52c2009-10-02 15:16:51 -04003839 if (resp_info & RSP_LEN_VALID) {
3840 rsplen = be32_to_cpu(fcprsp->rspRspLen);
James Smarte40a02c2010-02-26 14:13:54 -05003841 if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
James Smart6a9c52c2009-10-02 15:16:51 -04003842 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3843 "2719 Invalid response length: "
3844 "tgt x%x lun x%x cmnd x%x rsplen x%x\n",
3845 cmnd->device->id,
3846 cmnd->device->lun, cmnd->cmnd[0],
3847 rsplen);
3848 host_status = DID_ERROR;
3849 goto out;
3850 }
James Smarte40a02c2010-02-26 14:13:54 -05003851 if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
3852 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3853 "2757 Protocol failure detected during "
3854 "processing of FCP I/O op: "
3855 "tgt x%x lun x%x cmnd x%x rspInfo3 x%x\n",
3856 cmnd->device->id,
3857 cmnd->device->lun, cmnd->cmnd[0],
3858 fcprsp->rspInfo3);
3859 host_status = DID_ERROR;
3860 goto out;
3861 }
James Smart6a9c52c2009-10-02 15:16:51 -04003862 }
3863
James Smartc7743952006-12-02 13:34:42 -05003864 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
3865 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
3866 if (snslen > SCSI_SENSE_BUFFERSIZE)
3867 snslen = SCSI_SENSE_BUFFERSIZE;
3868
3869 if (resp_info & RSP_LEN_VALID)
3870 rsplen = be32_to_cpu(fcprsp->rspRspLen);
3871 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
3872 }
3873 lp = (uint32_t *)cmnd->sense_buffer;
3874
James Smartaa1c7ee2012-08-14 14:26:06 -04003875 /* special handling for under run conditions */
3876 if (!scsi_status && (resp_info & RESID_UNDER)) {
3877 /* don't log under runs if fcp set... */
3878 if (vport->cfg_log_verbose & LOG_FCP)
3879 logit = LOG_FCP_ERROR;
3880 /* unless operator says so */
3881 if (vport->cfg_log_verbose & LOG_FCP_UNDER)
3882 logit = LOG_FCP_UNDER;
3883 }
James Smartc7743952006-12-02 13:34:42 -05003884
James Smarte8b62012007-08-02 11:10:09 -04003885 lpfc_printf_vlog(vport, KERN_WARNING, logit,
James Smarte2a0a9d2008-12-04 22:40:02 -05003886 "9024 FCP command x%x failed: x%x SNS x%x x%x "
James Smarte8b62012007-08-02 11:10:09 -04003887 "Data: x%x x%x x%x x%x x%x\n",
3888 cmnd->cmnd[0], scsi_status,
3889 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
3890 be32_to_cpu(fcprsp->rspResId),
3891 be32_to_cpu(fcprsp->rspSnsLen),
3892 be32_to_cpu(fcprsp->rspRspLen),
3893 fcprsp->rspInfo3);
dea31012005-04-17 16:05:31 -05003894
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003895 scsi_set_resid(cmnd, 0);
dea31012005-04-17 16:05:31 -05003896 if (resp_info & RESID_UNDER) {
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003897 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea31012005-04-17 16:05:31 -05003898
James Smart73d91e52011-10-10 21:32:10 -04003899 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_UNDER,
James Smarte2a0a9d2008-12-04 22:40:02 -05003900 "9025 FCP Read Underrun, expected %d, "
James Smarte8b62012007-08-02 11:10:09 -04003901 "residual %d Data: x%x x%x x%x\n",
3902 be32_to_cpu(fcpcmd->fcpDl),
3903 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
3904 cmnd->underflow);
dea31012005-04-17 16:05:31 -05003905
3906 /*
James Smart7054a602007-04-25 09:52:34 -04003907 * If there is an under run check if under run reported by
3908 * storage array is same as the under run reported by HBA.
3909 * If this is not same, there is a dropped frame.
3910 */
3911 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3912 fcpi_parm &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003913 (scsi_get_resid(cmnd) != fcpi_parm)) {
James Smarte8b62012007-08-02 11:10:09 -04003914 lpfc_printf_vlog(vport, KERN_WARNING,
3915 LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05003916 "9026 FCP Read Check Error "
James Smarte8b62012007-08-02 11:10:09 -04003917 "and Underrun Data: x%x x%x x%x x%x\n",
3918 be32_to_cpu(fcpcmd->fcpDl),
3919 scsi_get_resid(cmnd), fcpi_parm,
3920 cmnd->cmnd[0]);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003921 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
James Smart7054a602007-04-25 09:52:34 -04003922 host_status = DID_ERROR;
3923 }
3924 /*
dea31012005-04-17 16:05:31 -05003925 * The cmnd->underflow is the minimum number of bytes that must
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003926 * be transferred for this command. Provided a sense condition
dea31012005-04-17 16:05:31 -05003927 * is not present, make sure the actual amount transferred is at
3928 * least the underflow value or fail.
3929 */
3930 if (!(resp_info & SNS_LEN_VALID) &&
3931 (scsi_status == SAM_STAT_GOOD) &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003932 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
3933 < cmnd->underflow)) {
James Smarte8b62012007-08-02 11:10:09 -04003934 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05003935 "9027 FCP command x%x residual "
James Smarte8b62012007-08-02 11:10:09 -04003936 "underrun converted to error "
3937 "Data: x%x x%x x%x\n",
James Smart66dbfbe2007-08-05 06:08:38 -04003938 cmnd->cmnd[0], scsi_bufflen(cmnd),
James Smarte8b62012007-08-02 11:10:09 -04003939 scsi_get_resid(cmnd), cmnd->underflow);
dea31012005-04-17 16:05:31 -05003940 host_status = DID_ERROR;
3941 }
3942 } else if (resp_info & RESID_OVER) {
James Smarte8b62012007-08-02 11:10:09 -04003943 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05003944 "9028 FCP command x%x residual overrun error. "
James Smarte4e74272009-07-19 10:01:38 -04003945 "Data: x%x x%x\n", cmnd->cmnd[0],
James Smarte8b62012007-08-02 11:10:09 -04003946 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea31012005-04-17 16:05:31 -05003947 host_status = DID_ERROR;
3948
3949 /*
3950 * Check SLI validation that all the transfer was actually done
James Smart582dd792012-08-03 12:34:44 -04003951 * (fcpi_parm should be zero).
dea31012005-04-17 16:05:31 -05003952 */
James Smart582dd792012-08-03 12:34:44 -04003953 } else if (fcpi_parm) {
James Smarte8b62012007-08-02 11:10:09 -04003954 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
James Smart582dd792012-08-03 12:34:44 -04003955 "9029 FCP Data Transfer Check Error: "
James Smarteee88772010-09-29 11:19:08 -04003956 "x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003957 be32_to_cpu(fcpcmd->fcpDl),
3958 be32_to_cpu(fcprsp->rspResId),
James Smarteee88772010-09-29 11:19:08 -04003959 fcpi_parm, cmnd->cmnd[0], scsi_status);
3960 switch (scsi_status) {
3961 case SAM_STAT_GOOD:
3962 case SAM_STAT_CHECK_CONDITION:
3963 /* Fabric dropped a data frame. Fail any successful
3964 * command in which we detected dropped frames.
3965 * A status of good or some check conditions could
3966 * be considered a successful command.
3967 */
3968 host_status = DID_ERROR;
3969 break;
3970 }
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003971 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea31012005-04-17 16:05:31 -05003972 }
3973
3974 out:
3975 cmnd->result = ScsiResult(host_status, scsi_status);
James Smartea2151b2008-09-07 11:52:10 -04003976 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
dea31012005-04-17 16:05:31 -05003977}
3978
James Smart9bad7672008-12-04 22:39:02 -05003979/**
James Smart3621a712009-04-06 18:47:14 -04003980 * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
James Smart9bad7672008-12-04 22:39:02 -05003981 * @phba: The Hba for which this call is being executed.
3982 * @pIocbIn: The command IOCBQ for the scsi cmnd.
James Smart3772a992009-05-22 14:50:54 -04003983 * @pIocbOut: The response IOCBQ for the scsi cmnd.
James Smart9bad7672008-12-04 22:39:02 -05003984 *
3985 * This routine assigns scsi command result by looking into response IOCB
3986 * status field appropriately. This routine handles QUEUE FULL condition as
3987 * well by ramping down device queue depth.
3988 **/
dea31012005-04-17 16:05:31 -05003989static void
3990lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
3991 struct lpfc_iocbq *pIocbOut)
3992{
3993 struct lpfc_scsi_buf *lpfc_cmd =
3994 (struct lpfc_scsi_buf *) pIocbIn->context1;
James Smart2e0fef82007-06-17 19:56:36 -05003995 struct lpfc_vport *vport = pIocbIn->vport;
dea31012005-04-17 16:05:31 -05003996 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
3997 struct lpfc_nodelist *pnode = rdata->pnode;
James Smart75baf692010-06-08 18:31:21 -04003998 struct scsi_cmnd *cmd;
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05003999 int result;
James Smarta257bf92009-04-06 18:48:10 -04004000 struct scsi_device *tmp_sdev;
James Smart5ffc2662009-11-18 15:39:44 -05004001 int depth;
James Smartfa61a542008-01-11 01:52:42 -05004002 unsigned long flags;
James Smartea2151b2008-09-07 11:52:10 -04004003 struct lpfc_fast_path_event *fast_path_evt;
James Smart75baf692010-06-08 18:31:21 -04004004 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04004005 uint32_t queue_depth, scsi_id;
James Smart73d91e52011-10-10 21:32:10 -04004006 uint32_t logit = LOG_FCP;
dea31012005-04-17 16:05:31 -05004007
James Smart75baf692010-06-08 18:31:21 -04004008 /* Sanity check on return of outstanding command */
4009 if (!(lpfc_cmd->pCmd))
4010 return;
4011 cmd = lpfc_cmd->pCmd;
4012 shost = cmd->device->host;
4013
James Smarte3d2b802012-08-14 14:25:43 -04004014 lpfc_cmd->result = (pIocbOut->iocb.un.ulpWord[4] & IOERR_PARAM_MASK);
dea31012005-04-17 16:05:31 -05004015 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
James Smart341af102010-01-26 23:07:37 -05004016 /* pick up SLI4 exhange busy status from HBA */
4017 lpfc_cmd->exch_busy = pIocbOut->iocb_flag & LPFC_EXCHANGE_BUSY;
4018
James Smart9a6b09c2012-03-01 22:37:42 -05004019#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4020 if (lpfc_cmd->prot_data_type) {
4021 struct scsi_dif_tuple *src = NULL;
4022
4023 src = (struct scsi_dif_tuple *)lpfc_cmd->prot_data_segment;
4024 /*
4025 * Used to restore any changes to protection
4026 * data for error injection.
4027 */
4028 switch (lpfc_cmd->prot_data_type) {
4029 case LPFC_INJERR_REFTAG:
4030 src->ref_tag =
4031 lpfc_cmd->prot_data;
4032 break;
4033 case LPFC_INJERR_APPTAG:
4034 src->app_tag =
4035 (uint16_t)lpfc_cmd->prot_data;
4036 break;
4037 case LPFC_INJERR_GUARD:
4038 src->guard_tag =
4039 (uint16_t)lpfc_cmd->prot_data;
4040 break;
4041 default:
4042 break;
4043 }
4044
4045 lpfc_cmd->prot_data = 0;
4046 lpfc_cmd->prot_data_type = 0;
4047 lpfc_cmd->prot_data_segment = NULL;
4048 }
4049#endif
James Smart109f6ed2008-12-04 22:39:08 -05004050 if (pnode && NLP_CHK_NODE_ACT(pnode))
4051 atomic_dec(&pnode->cmd_pending);
dea31012005-04-17 16:05:31 -05004052
4053 if (lpfc_cmd->status) {
4054 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
4055 (lpfc_cmd->result & IOERR_DRVR_MASK))
4056 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
4057 else if (lpfc_cmd->status >= IOSTAT_CNT)
4058 lpfc_cmd->status = IOSTAT_DEFAULT;
James Smartaa1c7ee2012-08-14 14:26:06 -04004059 if (lpfc_cmd->status == IOSTAT_FCP_RSP_ERROR &&
4060 !lpfc_cmd->fcp_rsp->rspStatus3 &&
4061 (lpfc_cmd->fcp_rsp->rspStatus2 & RESID_UNDER) &&
4062 !(vport->cfg_log_verbose & LOG_FCP_UNDER))
James Smart73d91e52011-10-10 21:32:10 -04004063 logit = 0;
4064 else
4065 logit = LOG_FCP | LOG_FCP_UNDER;
4066 lpfc_printf_vlog(vport, KERN_WARNING, logit,
4067 "9030 FCP cmd x%x failed <%d/%d> "
James Smart5a0d80f2012-05-09 21:18:20 -04004068 "status: x%x result: x%x "
4069 "sid: x%x did: x%x oxid: x%x "
4070 "Data: x%x x%x\n",
James Smart73d91e52011-10-10 21:32:10 -04004071 cmd->cmnd[0],
4072 cmd->device ? cmd->device->id : 0xffff,
4073 cmd->device ? cmd->device->lun : 0xffff,
4074 lpfc_cmd->status, lpfc_cmd->result,
James Smart5a0d80f2012-05-09 21:18:20 -04004075 vport->fc_myDID, pnode->nlp_DID,
4076 phba->sli_rev == LPFC_SLI_REV4 ?
4077 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
James Smart73d91e52011-10-10 21:32:10 -04004078 pIocbOut->iocb.ulpContext,
4079 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
dea31012005-04-17 16:05:31 -05004080
4081 switch (lpfc_cmd->status) {
4082 case IOSTAT_FCP_RSP_ERROR:
4083 /* Call FCP RSP handler to determine result */
James Smart2e0fef82007-06-17 19:56:36 -05004084 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea31012005-04-17 16:05:31 -05004085 break;
4086 case IOSTAT_NPORT_BSY:
4087 case IOSTAT_FABRIC_BSY:
James Smart0f1f53a2008-08-24 21:50:18 -04004088 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
James Smartea2151b2008-09-07 11:52:10 -04004089 fast_path_evt = lpfc_alloc_fast_evt(phba);
4090 if (!fast_path_evt)
4091 break;
4092 fast_path_evt->un.fabric_evt.event_type =
4093 FC_REG_FABRIC_EVENT;
4094 fast_path_evt->un.fabric_evt.subcategory =
4095 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
4096 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
4097 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
4098 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
4099 &pnode->nlp_portname,
4100 sizeof(struct lpfc_name));
4101 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
4102 &pnode->nlp_nodename,
4103 sizeof(struct lpfc_name));
4104 }
4105 fast_path_evt->vport = vport;
4106 fast_path_evt->work_evt.evt =
4107 LPFC_EVT_FASTPATH_MGMT_EVT;
4108 spin_lock_irqsave(&phba->hbalock, flags);
4109 list_add_tail(&fast_path_evt->work_evt.evt_listp,
4110 &phba->work_list);
4111 spin_unlock_irqrestore(&phba->hbalock, flags);
4112 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05004113 break;
James Smart92d7f7b2007-06-17 19:56:38 -05004114 case IOSTAT_LOCAL_REJECT:
James Smart1151e3e2011-02-16 12:39:35 -05004115 case IOSTAT_REMOTE_STOP:
James Smartab56dc22011-02-16 12:39:57 -05004116 if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
4117 lpfc_cmd->result ==
4118 IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
4119 lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
4120 lpfc_cmd->result ==
4121 IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
4122 cmd->result = ScsiResult(DID_NO_CONNECT, 0);
4123 break;
4124 }
James Smartd7c255b2008-08-24 21:50:00 -04004125 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
James Smart92d7f7b2007-06-17 19:56:38 -05004126 lpfc_cmd->result == IOERR_NO_RESOURCES ||
James Smartb92938b2010-06-07 15:24:12 -04004127 lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
4128 lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
James Smart92d7f7b2007-06-17 19:56:38 -05004129 cmd->result = ScsiResult(DID_REQUEUE, 0);
James Smart58da1ff2008-04-07 10:15:56 -04004130 break;
James Smarte2a0a9d2008-12-04 22:40:02 -05004131 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004132 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
4133 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
4134 pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
4135 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
4136 /*
4137 * This is a response for a BG enabled
4138 * cmd. Parse BG error
4139 */
4140 lpfc_parse_bg_err(phba, lpfc_cmd,
4141 pIocbOut);
4142 break;
4143 } else {
4144 lpfc_printf_vlog(vport, KERN_WARNING,
4145 LOG_BG,
4146 "9031 non-zero BGSTAT "
James Smart6a9c52c2009-10-02 15:16:51 -04004147 "on unprotected cmd\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05004148 }
4149 }
James Smart1151e3e2011-02-16 12:39:35 -05004150 if ((lpfc_cmd->status == IOSTAT_REMOTE_STOP)
4151 && (phba->sli_rev == LPFC_SLI_REV4)
4152 && (pnode && NLP_CHK_NODE_ACT(pnode))) {
4153 /* This IO was aborted by the target, we don't
4154 * know the rxid and because we did not send the
4155 * ABTS we cannot generate and RRQ.
4156 */
4157 lpfc_set_rrq_active(phba, pnode,
James Smartee0f4fe2012-05-09 21:19:14 -04004158 lpfc_cmd->cur_iocbq.sli4_lxritag,
4159 0, 0);
James Smart1151e3e2011-02-16 12:39:35 -05004160 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004161 /* else: fall through */
dea31012005-04-17 16:05:31 -05004162 default:
4163 cmd->result = ScsiResult(DID_ERROR, 0);
4164 break;
4165 }
4166
James Smart58da1ff2008-04-07 10:15:56 -04004167 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004168 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
James Smart0f1f53a2008-08-24 21:50:18 -04004169 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
4170 SAM_STAT_BUSY);
James Smartab56dc22011-02-16 12:39:57 -05004171 } else
dea31012005-04-17 16:05:31 -05004172 cmd->result = ScsiResult(DID_OK, 0);
dea31012005-04-17 16:05:31 -05004173
4174 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
4175 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
4176
James Smarte8b62012007-08-02 11:10:09 -04004177 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4178 "0710 Iodone <%d/%d> cmd %p, error "
4179 "x%x SNS x%x x%x Data: x%x x%x\n",
4180 cmd->device->id, cmd->device->lun, cmd,
4181 cmd->result, *lp, *(lp + 3), cmd->retries,
4182 scsi_get_resid(cmd));
dea31012005-04-17 16:05:31 -05004183 }
4184
James Smartea2151b2008-09-07 11:52:10 -04004185 lpfc_update_stats(phba, lpfc_cmd);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05004186 result = cmd->result;
James Smart977b5a02008-09-07 11:52:04 -04004187 if (vport->cfg_max_scsicmpl_time &&
4188 time_after(jiffies, lpfc_cmd->start_time +
4189 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
James Smarta257bf92009-04-06 18:48:10 -04004190 spin_lock_irqsave(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05004191 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
4192 if (pnode->cmd_qdepth >
4193 atomic_read(&pnode->cmd_pending) &&
4194 (atomic_read(&pnode->cmd_pending) >
4195 LPFC_MIN_TGT_QDEPTH) &&
4196 ((cmd->cmnd[0] == READ_10) ||
4197 (cmd->cmnd[0] == WRITE_10)))
4198 pnode->cmd_qdepth =
4199 atomic_read(&pnode->cmd_pending);
James Smart977b5a02008-09-07 11:52:04 -04004200
James Smart109f6ed2008-12-04 22:39:08 -05004201 pnode->last_change_time = jiffies;
4202 }
James Smarta257bf92009-04-06 18:48:10 -04004203 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05004204 } else if (pnode && NLP_CHK_NODE_ACT(pnode)) {
James Smart7dc517d2010-07-14 15:32:10 -04004205 if ((pnode->cmd_qdepth < vport->cfg_tgt_queue_depth) &&
James Smart977b5a02008-09-07 11:52:04 -04004206 time_after(jiffies, pnode->last_change_time +
James Smart109f6ed2008-12-04 22:39:08 -05004207 msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
James Smarta257bf92009-04-06 18:48:10 -04004208 spin_lock_irqsave(shost->host_lock, flags);
James Smart7dc517d2010-07-14 15:32:10 -04004209 depth = pnode->cmd_qdepth * LPFC_TGTQ_RAMPUP_PCENT
4210 / 100;
4211 depth = depth ? depth : 1;
4212 pnode->cmd_qdepth += depth;
4213 if (pnode->cmd_qdepth > vport->cfg_tgt_queue_depth)
4214 pnode->cmd_qdepth = vport->cfg_tgt_queue_depth;
James Smart109f6ed2008-12-04 22:39:08 -05004215 pnode->last_change_time = jiffies;
James Smarta257bf92009-04-06 18:48:10 -04004216 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05004217 }
James Smart977b5a02008-09-07 11:52:04 -04004218 }
4219
James Smart1dcb58e2007-04-25 09:51:30 -04004220 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James Smarta257bf92009-04-06 18:48:10 -04004221
4222 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
4223 queue_depth = cmd->device->queue_depth;
4224 scsi_id = cmd->device->id;
dea31012005-04-17 16:05:31 -05004225 cmd->scsi_done(cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004226
Jamie Wellnitzb8086082006-02-28 22:33:12 -05004227 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart876dd7d2012-09-29 11:31:28 -04004228 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92e3af62012-08-14 14:26:28 -04004229 lpfc_cmd->pCmd = NULL;
James Smart876dd7d2012-09-29 11:31:28 -04004230 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart92e3af62012-08-14 14:26:28 -04004231
James Smartfa61a542008-01-11 01:52:42 -05004232 /*
4233 * If there is a thread waiting for command completion
4234 * wake up the thread.
4235 */
James Smarta257bf92009-04-06 18:48:10 -04004236 spin_lock_irqsave(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05004237 if (lpfc_cmd->waitq)
4238 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04004239 spin_unlock_irqrestore(shost->host_lock, flags);
Jamie Wellnitzb8086082006-02-28 22:33:12 -05004240 lpfc_release_scsi_buf(phba, lpfc_cmd);
4241 return;
4242 }
4243
James Smart92d7f7b2007-06-17 19:56:38 -05004244 if (!result)
James Smarta257bf92009-04-06 18:48:10 -04004245 lpfc_rampup_queue_depth(vport, queue_depth);
James Smart92d7f7b2007-06-17 19:56:38 -05004246
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05004247 /*
4248 * Check for queue full. If the lun is reporting queue full, then
4249 * back off the lun queue depth to prevent target overloads.
4250 */
James Smart58da1ff2008-04-07 10:15:56 -04004251 if (result == SAM_STAT_TASK_SET_FULL && pnode &&
4252 NLP_CHK_NODE_ACT(pnode)) {
James Smarta257bf92009-04-06 18:48:10 -04004253 shost_for_each_device(tmp_sdev, shost) {
4254 if (tmp_sdev->id != scsi_id)
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05004255 continue;
4256 depth = scsi_track_queue_full(tmp_sdev,
James Smart5ffc2662009-11-18 15:39:44 -05004257 tmp_sdev->queue_depth-1);
4258 if (depth <= 0)
4259 continue;
James Smarte8b62012007-08-02 11:10:09 -04004260 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4261 "0711 detected queue full - lun queue "
4262 "depth adjusted to %d.\n", depth);
James Smartea2151b2008-09-07 11:52:10 -04004263 lpfc_send_sdev_queuedepth_change_event(phba, vport,
James Smart5ffc2662009-11-18 15:39:44 -05004264 pnode,
4265 tmp_sdev->lun,
4266 depth+1, depth);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05004267 }
4268 }
4269
James Smart876dd7d2012-09-29 11:31:28 -04004270 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92e3af62012-08-14 14:26:28 -04004271 lpfc_cmd->pCmd = NULL;
James Smart876dd7d2012-09-29 11:31:28 -04004272 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart92e3af62012-08-14 14:26:28 -04004273
James Smartfa61a542008-01-11 01:52:42 -05004274 /*
4275 * If there is a thread waiting for command completion
4276 * wake up the thread.
4277 */
James Smarta257bf92009-04-06 18:48:10 -04004278 spin_lock_irqsave(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05004279 if (lpfc_cmd->waitq)
4280 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04004281 spin_unlock_irqrestore(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05004282
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004283 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004284}
4285
James Smart34b02dc2008-08-24 21:49:55 -04004286/**
James Smart3621a712009-04-06 18:47:14 -04004287 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
James Smart34b02dc2008-08-24 21:49:55 -04004288 * @data: A pointer to the immediate command data portion of the IOCB.
4289 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
4290 *
4291 * The routine copies the entire FCP command from @fcp_cmnd to @data while
4292 * byte swapping the data to big endian format for transmission on the wire.
4293 **/
4294static void
4295lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
4296{
4297 int i, j;
4298 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
4299 i += sizeof(uint32_t), j++) {
4300 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
4301 }
4302}
4303
James Smart9bad7672008-12-04 22:39:02 -05004304/**
James Smartf1126682009-06-10 17:22:44 -04004305 * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05004306 * @vport: The virtual port for which this call is being executed.
4307 * @lpfc_cmd: The scsi command which needs to send.
4308 * @pnode: Pointer to lpfc_nodelist.
4309 *
4310 * This routine initializes fcp_cmnd and iocb data structure from scsi command
James Smart3772a992009-05-22 14:50:54 -04004311 * to transfer for device with SLI3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05004312 **/
dea31012005-04-17 16:05:31 -05004313static void
James Smartf1126682009-06-10 17:22:44 -04004314lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
James Smart2e0fef82007-06-17 19:56:36 -05004315 struct lpfc_nodelist *pnode)
dea31012005-04-17 16:05:31 -05004316{
James Smart2e0fef82007-06-17 19:56:36 -05004317 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004318 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
4319 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
4320 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
4321 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
4322 int datadir = scsi_cmnd->sc_data_direction;
James Smart7e2b19f2007-10-29 11:00:39 -04004323 char tag[2];
James Smart027140e2012-08-03 12:35:44 -04004324 uint8_t *ptr;
4325 bool sli4;
dea31012005-04-17 16:05:31 -05004326
James Smart58da1ff2008-04-07 10:15:56 -04004327 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
4328 return;
4329
dea31012005-04-17 16:05:31 -05004330 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
James.Smart@Emulex.Com69859dc2005-08-10 15:02:37 -04004331 /* clear task management bits */
4332 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea31012005-04-17 16:05:31 -05004333
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -04004334 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
4335 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05004336
James Smart027140e2012-08-03 12:35:44 -04004337 ptr = &fcp_cmnd->fcpCdb[0];
4338 memcpy(ptr, scsi_cmnd->cmnd, scsi_cmnd->cmd_len);
4339 if (scsi_cmnd->cmd_len < LPFC_FCP_CDB_LEN) {
4340 ptr += scsi_cmnd->cmd_len;
4341 memset(ptr, 0, (LPFC_FCP_CDB_LEN - scsi_cmnd->cmd_len));
4342 }
4343
James Smart7e2b19f2007-10-29 11:00:39 -04004344 if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
4345 switch (tag[0]) {
dea31012005-04-17 16:05:31 -05004346 case HEAD_OF_QUEUE_TAG:
4347 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
4348 break;
4349 case ORDERED_QUEUE_TAG:
4350 fcp_cmnd->fcpCntl1 = ORDERED_Q;
4351 break;
4352 default:
4353 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
4354 break;
4355 }
4356 } else
James Smartfe8f7f92013-01-03 15:43:03 -05004357 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
dea31012005-04-17 16:05:31 -05004358
James Smart027140e2012-08-03 12:35:44 -04004359 sli4 = (phba->sli_rev == LPFC_SLI_REV4);
4360
dea31012005-04-17 16:05:31 -05004361 /*
4362 * There are three possibilities here - use scatter-gather segment, use
4363 * the single mapping, or neither. Start the lpfc command prep by
4364 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
4365 * data bde entry.
4366 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05004367 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05004368 if (datadir == DMA_TO_DEVICE) {
4369 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
James Smart027140e2012-08-03 12:35:44 -04004370 if (sli4)
4371 iocb_cmd->ulpPU = PARM_READ_CHECK;
4372 else {
James Smart3772a992009-05-22 14:50:54 -04004373 iocb_cmd->un.fcpi.fcpi_parm = 0;
4374 iocb_cmd->ulpPU = 0;
James Smart027140e2012-08-03 12:35:44 -04004375 }
dea31012005-04-17 16:05:31 -05004376 fcp_cmnd->fcpCntl3 = WRITE_DATA;
4377 phba->fc4OutputRequests++;
4378 } else {
4379 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
4380 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05004381 fcp_cmnd->fcpCntl3 = READ_DATA;
4382 phba->fc4InputRequests++;
4383 }
4384 } else {
4385 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
4386 iocb_cmd->un.fcpi.fcpi_parm = 0;
4387 iocb_cmd->ulpPU = 0;
4388 fcp_cmnd->fcpCntl3 = 0;
4389 phba->fc4ControlRequests++;
4390 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004391 if (phba->sli_rev == 3 &&
4392 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04004393 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05004394 /*
4395 * Finish initializing those IOCB fields that are independent
4396 * of the scsi_cmnd request_buffer
4397 */
4398 piocbq->iocb.ulpContext = pnode->nlp_rpi;
James Smart027140e2012-08-03 12:35:44 -04004399 if (sli4)
James Smart6d368e52011-05-24 11:44:12 -04004400 piocbq->iocb.ulpContext =
4401 phba->sli4_hba.rpi_ids[pnode->nlp_rpi];
dea31012005-04-17 16:05:31 -05004402 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
4403 piocbq->iocb.ulpFCP2Rcvy = 1;
James Smart09372822008-01-11 01:52:54 -05004404 else
4405 piocbq->iocb.ulpFCP2Rcvy = 0;
dea31012005-04-17 16:05:31 -05004406
4407 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
4408 piocbq->context1 = lpfc_cmd;
4409 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
4410 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
James Smart2e0fef82007-06-17 19:56:36 -05004411 piocbq->vport = vport;
dea31012005-04-17 16:05:31 -05004412}
4413
James Smart9bad7672008-12-04 22:39:02 -05004414/**
James Smart6d368e52011-05-24 11:44:12 -04004415 * lpfc_scsi_prep_task_mgmt_cmd - Convert SLI3 scsi TM cmd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05004416 * @vport: The virtual port for which this call is being executed.
4417 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
4418 * @lun: Logical unit number.
4419 * @task_mgmt_cmd: SCSI task management command.
4420 *
James Smart3772a992009-05-22 14:50:54 -04004421 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
4422 * for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05004423 *
4424 * Return codes:
4425 * 0 - Error
4426 * 1 - Success
4427 **/
dea31012005-04-17 16:05:31 -05004428static int
James Smartf1126682009-06-10 17:22:44 -04004429lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
dea31012005-04-17 16:05:31 -05004430 struct lpfc_scsi_buf *lpfc_cmd,
James Smart420b630d2006-07-06 15:50:16 -04004431 unsigned int lun,
dea31012005-04-17 16:05:31 -05004432 uint8_t task_mgmt_cmd)
4433{
dea31012005-04-17 16:05:31 -05004434 struct lpfc_iocbq *piocbq;
4435 IOCB_t *piocb;
4436 struct fcp_cmnd *fcp_cmnd;
James Smart0b18ac42006-05-01 21:50:40 -04004437 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea31012005-04-17 16:05:31 -05004438 struct lpfc_nodelist *ndlp = rdata->pnode;
4439
James Smart58da1ff2008-04-07 10:15:56 -04004440 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
4441 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05004442 return 0;
dea31012005-04-17 16:05:31 -05004443
dea31012005-04-17 16:05:31 -05004444 piocbq = &(lpfc_cmd->cur_iocbq);
James Smart2e0fef82007-06-17 19:56:36 -05004445 piocbq->vport = vport;
4446
dea31012005-04-17 16:05:31 -05004447 piocb = &piocbq->iocb;
4448
4449 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart34b02dc2008-08-24 21:49:55 -04004450 /* Clear out any old data in the FCP command area */
4451 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
4452 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05004453 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
James Smarte2a0a9d2008-12-04 22:40:02 -05004454 if (vport->phba->sli_rev == 3 &&
4455 !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04004456 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05004457 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
dea31012005-04-17 16:05:31 -05004458 piocb->ulpContext = ndlp->nlp_rpi;
James Smart6d368e52011-05-24 11:44:12 -04004459 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
4460 piocb->ulpContext =
4461 vport->phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
4462 }
dea31012005-04-17 16:05:31 -05004463 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
4464 piocb->ulpFCP2Rcvy = 1;
4465 }
4466 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
4467
4468 /* ulpTimeout is only one byte */
4469 if (lpfc_cmd->timeout > 0xff) {
4470 /*
4471 * Do not timeout the command at the firmware level.
4472 * The driver will provide the timeout mechanism.
4473 */
4474 piocb->ulpTimeout = 0;
James Smartf1126682009-06-10 17:22:44 -04004475 } else
dea31012005-04-17 16:05:31 -05004476 piocb->ulpTimeout = lpfc_cmd->timeout;
James Smartf1126682009-06-10 17:22:44 -04004477
4478 if (vport->phba->sli_rev == LPFC_SLI_REV4)
4479 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004480
James Smart2e0fef82007-06-17 19:56:36 -05004481 return 1;
dea31012005-04-17 16:05:31 -05004482}
4483
James Smart9bad7672008-12-04 22:39:02 -05004484/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004485 * lpfc_scsi_api_table_setup - Set up scsi api function jump table
James Smart3772a992009-05-22 14:50:54 -04004486 * @phba: The hba struct for which this call is being executed.
4487 * @dev_grp: The HBA PCI-Device group number.
4488 *
4489 * This routine sets up the SCSI interface API function jump table in @phba
4490 * struct.
4491 * Returns: 0 - success, -ENODEV - failure.
4492 **/
4493int
4494lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4495{
4496
James Smartf1126682009-06-10 17:22:44 -04004497 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
4498 phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
James Smartf1126682009-06-10 17:22:44 -04004499
James Smart3772a992009-05-22 14:50:54 -04004500 switch (dev_grp) {
4501 case LPFC_PCI_DEV_LP:
4502 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s3;
4503 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
James Smartacd68592012-01-18 16:25:09 -05004504 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04004505 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
James Smart19ca7602010-11-20 23:11:55 -05004506 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04004507 break;
James Smartda0436e2009-05-22 14:51:39 -04004508 case LPFC_PCI_DEV_OC:
4509 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s4;
4510 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
James Smartacd68592012-01-18 16:25:09 -05004511 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04004512 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
James Smart19ca7602010-11-20 23:11:55 -05004513 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04004514 break;
James Smart3772a992009-05-22 14:50:54 -04004515 default:
4516 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4517 "1418 Invalid HBA PCI-device group: 0x%x\n",
4518 dev_grp);
4519 return -ENODEV;
4520 break;
4521 }
James Smart3772a992009-05-22 14:50:54 -04004522 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
James Smart84d1b002010-02-12 14:42:33 -05004523 phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
James Smart3772a992009-05-22 14:50:54 -04004524 return 0;
4525}
4526
4527/**
James Smart3621a712009-04-06 18:47:14 -04004528 * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
James Smart9bad7672008-12-04 22:39:02 -05004529 * @phba: The Hba for which this call is being executed.
4530 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
4531 * @rspiocbq: Pointer to lpfc_iocbq data structure.
4532 *
4533 * This routine is IOCB completion routine for device reset and target reset
4534 * routine. This routine release scsi buffer associated with lpfc_cmd.
4535 **/
James Smart7054a602007-04-25 09:52:34 -04004536static void
4537lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
4538 struct lpfc_iocbq *cmdiocbq,
4539 struct lpfc_iocbq *rspiocbq)
4540{
4541 struct lpfc_scsi_buf *lpfc_cmd =
4542 (struct lpfc_scsi_buf *) cmdiocbq->context1;
4543 if (lpfc_cmd)
4544 lpfc_release_scsi_buf(phba, lpfc_cmd);
4545 return;
4546}
4547
James Smart9bad7672008-12-04 22:39:02 -05004548/**
James Smart3621a712009-04-06 18:47:14 -04004549 * lpfc_info - Info entry point of scsi_host_template data structure
James Smart9bad7672008-12-04 22:39:02 -05004550 * @host: The scsi host for which this call is being executed.
4551 *
4552 * This routine provides module information about hba.
4553 *
4554 * Reutrn code:
4555 * Pointer to char - Success.
4556 **/
dea31012005-04-17 16:05:31 -05004557const char *
4558lpfc_info(struct Scsi_Host *host)
4559{
James Smart2e0fef82007-06-17 19:56:36 -05004560 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
4561 struct lpfc_hba *phba = vport->phba;
James Smart8b68cd52012-09-29 11:32:37 -04004562 int len, link_speed = 0;
dea31012005-04-17 16:05:31 -05004563 static char lpfcinfobuf[384];
4564
4565 memset(lpfcinfobuf,0,384);
4566 if (phba && phba->pcidev){
4567 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
4568 len = strlen(lpfcinfobuf);
4569 snprintf(lpfcinfobuf + len,
4570 384-len,
4571 " on PCI bus %02x device %02x irq %d",
4572 phba->pcidev->bus->number,
4573 phba->pcidev->devfn,
4574 phba->pcidev->irq);
4575 len = strlen(lpfcinfobuf);
4576 if (phba->Port[0]) {
4577 snprintf(lpfcinfobuf + len,
4578 384-len,
4579 " port %s",
4580 phba->Port);
4581 }
James Smart65467b62010-01-26 23:08:29 -05004582 len = strlen(lpfcinfobuf);
James Smart8b68cd52012-09-29 11:32:37 -04004583 if (phba->sli_rev <= LPFC_SLI_REV3) {
4584 link_speed = lpfc_sli_port_speed_get(phba);
4585 } else {
4586 if (phba->sli4_hba.link_state.logical_speed)
4587 link_speed =
4588 phba->sli4_hba.link_state.logical_speed;
4589 else
4590 link_speed = phba->sli4_hba.link_state.speed;
James Smart65467b62010-01-26 23:08:29 -05004591 }
James Smart8b68cd52012-09-29 11:32:37 -04004592 if (link_speed != 0)
4593 snprintf(lpfcinfobuf + len, 384-len,
4594 " Logical Link Speed: %d Mbps", link_speed);
dea31012005-04-17 16:05:31 -05004595 }
4596 return lpfcinfobuf;
4597}
4598
James Smart9bad7672008-12-04 22:39:02 -05004599/**
James Smart3621a712009-04-06 18:47:14 -04004600 * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
James Smart9bad7672008-12-04 22:39:02 -05004601 * @phba: The Hba for which this call is being executed.
4602 *
4603 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
4604 * The default value of cfg_poll_tmo is 10 milliseconds.
4605 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004606static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
4607{
4608 unsigned long poll_tmo_expires =
4609 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
4610
James Smart0e9bb8d2013-03-01 16:35:12 -05004611 if (!list_empty(&phba->sli.ring[LPFC_FCP_RING].txcmplq))
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004612 mod_timer(&phba->fcp_poll_timer,
4613 poll_tmo_expires);
4614}
4615
James Smart9bad7672008-12-04 22:39:02 -05004616/**
James Smart3621a712009-04-06 18:47:14 -04004617 * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
James Smart9bad7672008-12-04 22:39:02 -05004618 * @phba: The Hba for which this call is being executed.
4619 *
4620 * This routine starts the fcp_poll_timer of @phba.
4621 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004622void lpfc_poll_start_timer(struct lpfc_hba * phba)
4623{
4624 lpfc_poll_rearm_timer(phba);
4625}
4626
James Smart9bad7672008-12-04 22:39:02 -05004627/**
James Smart3621a712009-04-06 18:47:14 -04004628 * lpfc_poll_timeout - Restart polling timer
James Smart9bad7672008-12-04 22:39:02 -05004629 * @ptr: Map to lpfc_hba data structure pointer.
4630 *
4631 * This routine restarts fcp_poll timer, when FCP ring polling is enable
4632 * and FCP Ring interrupt is disable.
4633 **/
4634
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004635void lpfc_poll_timeout(unsigned long ptr)
4636{
James Smart2e0fef82007-06-17 19:56:36 -05004637 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004638
4639 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004640 lpfc_sli_handle_fast_ring_event(phba,
4641 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4642
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004643 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4644 lpfc_poll_rearm_timer(phba);
4645 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004646}
4647
James Smart9bad7672008-12-04 22:39:02 -05004648/**
James Smart3621a712009-04-06 18:47:14 -04004649 * lpfc_queuecommand - scsi_host_template queuecommand entry point
James Smart9bad7672008-12-04 22:39:02 -05004650 * @cmnd: Pointer to scsi_cmnd data structure.
4651 * @done: Pointer to done routine.
4652 *
4653 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
4654 * This routine prepares an IOCB from scsi command and provides to firmware.
4655 * The @done callback is invoked after driver finished processing the command.
4656 *
4657 * Return value :
4658 * 0 - Success
4659 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
4660 **/
dea31012005-04-17 16:05:31 -05004661static int
James Smartb9a7c632012-08-03 12:35:24 -04004662lpfc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004663{
James Smart2e0fef82007-06-17 19:56:36 -05004664 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4665 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004666 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05004667 struct lpfc_nodelist *ndlp;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004668 struct lpfc_scsi_buf *lpfc_cmd;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004669 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004670 int err;
dea31012005-04-17 16:05:31 -05004671
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004672 err = fc_remote_port_chkready(rport);
4673 if (err) {
4674 cmnd->result = err;
dea31012005-04-17 16:05:31 -05004675 goto out_fail_command;
4676 }
James Smart1c6f4ef52009-11-18 15:40:49 -05004677 ndlp = rdata->pnode;
dea31012005-04-17 16:05:31 -05004678
James Smartbf086112011-08-21 21:48:13 -04004679 if ((scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) &&
James Smartacd68592012-01-18 16:25:09 -05004680 (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED))) {
James Smarte2a0a9d2008-12-04 22:40:02 -05004681
James Smart6a9c52c2009-10-02 15:16:51 -04004682 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4683 "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
4684 " op:%02x str=%s without registering for"
4685 " BlockGuard - Rejecting command\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05004686 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
4687 dif_op_str[scsi_get_prot_op(cmnd)]);
4688 goto out_fail_command;
4689 }
4690
dea31012005-04-17 16:05:31 -05004691 /*
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004692 * Catch race where our node has transitioned, but the
4693 * transport is still transitioning.
dea31012005-04-17 16:05:31 -05004694 */
James Smart6b415f52012-06-12 13:54:59 -04004695 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
4696 goto out_tgt_busy;
James Smart7dc517d2010-07-14 15:32:10 -04004697 if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth)
Mike Christie34963432011-02-25 14:04:28 -06004698 goto out_tgt_busy;
Mike Christiea93ce022008-08-17 15:24:41 -05004699
James Smart19ca7602010-11-20 23:11:55 -05004700 lpfc_cmd = lpfc_get_scsi_buf(phba, ndlp);
dea31012005-04-17 16:05:31 -05004701 if (lpfc_cmd == NULL) {
James Smarteaf15d52008-12-04 22:39:29 -05004702 lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05004703
James Smarte8b62012007-08-02 11:10:09 -04004704 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4705 "0707 driver's buffer pool is empty, "
4706 "IO busied\n");
dea31012005-04-17 16:05:31 -05004707 goto out_host_busy;
4708 }
4709
4710 /*
4711 * Store the midlayer's command structure for the completion phase
4712 * and complete the command initialization.
4713 */
4714 lpfc_cmd->pCmd = cmnd;
4715 lpfc_cmd->rdata = rdata;
4716 lpfc_cmd->timeout = 0;
James Smart977b5a02008-09-07 11:52:04 -04004717 lpfc_cmd->start_time = jiffies;
dea31012005-04-17 16:05:31 -05004718 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
dea31012005-04-17 16:05:31 -05004719
James Smarte2a0a9d2008-12-04 22:40:02 -05004720 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
James Smart6a9c52c2009-10-02 15:16:51 -04004721 if (vport->phba->cfg_enable_bg) {
James Smart737d4242013-04-17 20:14:49 -04004722 lpfc_printf_vlog(vport,
4723 KERN_INFO, LOG_SCSI_CMD,
James Smart26134702012-08-14 14:25:50 -04004724 "9033 BLKGRD: rcvd %s cmd:x%x "
4725 "sector x%llx cnt %u pt %x\n",
4726 dif_op_str[scsi_get_prot_op(cmnd)],
4727 cmnd->cmnd[0],
4728 (unsigned long long)scsi_get_lba(cmnd),
4729 blk_rq_sectors(cmnd->request),
4730 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004731 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004732 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
4733 } else {
James Smart6a9c52c2009-10-02 15:16:51 -04004734 if (vport->phba->cfg_enable_bg) {
James Smart737d4242013-04-17 20:14:49 -04004735 lpfc_printf_vlog(vport,
4736 KERN_INFO, LOG_SCSI_CMD,
James Smart26134702012-08-14 14:25:50 -04004737 "9038 BLKGRD: rcvd PROT_NORMAL cmd: "
4738 "x%x sector x%llx cnt %u pt %x\n",
4739 cmnd->cmnd[0],
4740 (unsigned long long)scsi_get_lba(cmnd),
James Smart9a6b09c2012-03-01 22:37:42 -05004741 blk_rq_sectors(cmnd->request),
James Smart26134702012-08-14 14:25:50 -04004742 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004743 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004744 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
4745 }
4746
dea31012005-04-17 16:05:31 -05004747 if (err)
4748 goto out_host_busy_free_buf;
4749
James Smart2e0fef82007-06-17 19:56:36 -05004750 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea31012005-04-17 16:05:31 -05004751
James Smart977b5a02008-09-07 11:52:04 -04004752 atomic_inc(&ndlp->cmd_pending);
James Smart3772a992009-05-22 14:50:54 -04004753 err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
James Smart92d7f7b2007-06-17 19:56:38 -05004754 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
James Smarteaf15d52008-12-04 22:39:29 -05004755 if (err) {
4756 atomic_dec(&ndlp->cmd_pending);
dea31012005-04-17 16:05:31 -05004757 goto out_host_busy_free_buf;
James Smarteaf15d52008-12-04 22:39:29 -05004758 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004759 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004760 lpfc_sli_handle_fast_ring_event(phba,
4761 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4762
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004763 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4764 lpfc_poll_rearm_timer(phba);
4765 }
4766
dea31012005-04-17 16:05:31 -05004767 return 0;
4768
4769 out_host_busy_free_buf:
James Smartbcf4dbf2006-07-06 15:50:08 -04004770 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004771 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004772 out_host_busy:
4773 return SCSI_MLQUEUE_HOST_BUSY;
4774
Mike Christie34963432011-02-25 14:04:28 -06004775 out_tgt_busy:
4776 return SCSI_MLQUEUE_TARGET_BUSY;
4777
dea31012005-04-17 16:05:31 -05004778 out_fail_command:
James Smartb9a7c632012-08-03 12:35:24 -04004779 cmnd->scsi_done(cmnd);
dea31012005-04-17 16:05:31 -05004780 return 0;
4781}
4782
Jeff Garzikf2812332010-11-16 02:10:29 -05004783
James Smart9bad7672008-12-04 22:39:02 -05004784/**
James Smart3621a712009-04-06 18:47:14 -04004785 * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05004786 * @cmnd: Pointer to scsi_cmnd data structure.
4787 *
4788 * This routine aborts @cmnd pending in base driver.
4789 *
4790 * Return code :
4791 * 0x2003 - Error
4792 * 0x2002 - Success
4793 **/
dea31012005-04-17 16:05:31 -05004794static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05004795lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004796{
James Smart2e0fef82007-06-17 19:56:36 -05004797 struct Scsi_Host *shost = cmnd->device->host;
4798 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4799 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004800 struct lpfc_iocbq *iocb;
4801 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05004802 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05004803 IOCB_t *cmd, *icmd;
James Smart3a707302012-06-12 13:54:42 -04004804 int ret = SUCCESS, status = 0;
James Smart876dd7d2012-09-29 11:31:28 -04004805 unsigned long flags;
James Smartfa61a542008-01-11 01:52:42 -05004806 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004807
James Smart3a707302012-06-12 13:54:42 -04004808 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04004809 if (status != 0 && status != SUCCESS)
James Smart3a707302012-06-12 13:54:42 -04004810 return status;
James Smart4f2e66c2012-05-09 21:17:07 -04004811
James Smart876dd7d2012-09-29 11:31:28 -04004812 spin_lock_irqsave(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004813 /* driver queued commands are in process of being flushed */
4814 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
James Smart876dd7d2012-09-29 11:31:28 -04004815 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004816 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4817 "3168 SCSI Layer abort requested I/O has been "
4818 "flushed by LLD.\n");
4819 return FAILED;
4820 }
4821
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004822 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
James Smart92e3af62012-08-14 14:26:28 -04004823 if (!lpfc_cmd || !lpfc_cmd->pCmd) {
James Smart876dd7d2012-09-29 11:31:28 -04004824 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarteee88772010-09-29 11:19:08 -04004825 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4826 "2873 SCSI Layer I/O Abort Request IO CMPL Status "
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04004827 "x%x ID %d LUN %d\n",
James Smart3a707302012-06-12 13:54:42 -04004828 SUCCESS, cmnd->device->id, cmnd->device->lun);
James Smarteee88772010-09-29 11:19:08 -04004829 return SUCCESS;
4830 }
dea31012005-04-17 16:05:31 -05004831
James Smart4f2e66c2012-05-09 21:17:07 -04004832 iocb = &lpfc_cmd->cur_iocbq;
4833 /* the command is in process of being cancelled */
4834 if (!(iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
James Smart876dd7d2012-09-29 11:31:28 -04004835 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004836 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4837 "3169 SCSI Layer abort requested I/O has been "
4838 "cancelled by LLD.\n");
4839 return FAILED;
4840 }
dea31012005-04-17 16:05:31 -05004841 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004842 * If pCmd field of the corresponding lpfc_scsi_buf structure
4843 * points to a different SCSI command, then the driver has
4844 * already completed this command, but the midlayer did not
James Smart4f2e66c2012-05-09 21:17:07 -04004845 * see the completion before the eh fired. Just return SUCCESS.
dea31012005-04-17 16:05:31 -05004846 */
James Smart4f2e66c2012-05-09 21:17:07 -04004847 if (lpfc_cmd->pCmd != cmnd) {
4848 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4849 "3170 SCSI Layer abort requested I/O has been "
4850 "completed by LLD.\n");
4851 goto out_unlock;
4852 }
dea31012005-04-17 16:05:31 -05004853
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004854 BUG_ON(iocb->context1 != lpfc_cmd);
dea31012005-04-17 16:05:31 -05004855
James Smart4f2e66c2012-05-09 21:17:07 -04004856 abtsiocb = __lpfc_sli_get_iocbq(phba);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004857 if (abtsiocb == NULL) {
4858 ret = FAILED;
James Smart4f2e66c2012-05-09 21:17:07 -04004859 goto out_unlock;
dea31012005-04-17 16:05:31 -05004860 }
4861
dea31012005-04-17 16:05:31 -05004862 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004863 * The scsi command can not be in txq and it is in flight because the
4864 * pCmd is still pointig at the SCSI command we have to abort. There
4865 * is no need to search the txcmplq. Just send an abort to the FW.
dea31012005-04-17 16:05:31 -05004866 */
dea31012005-04-17 16:05:31 -05004867
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004868 cmd = &iocb->iocb;
4869 icmd = &abtsiocb->iocb;
4870 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
4871 icmd->un.acxri.abortContextTag = cmd->ulpContext;
James Smart3772a992009-05-22 14:50:54 -04004872 if (phba->sli_rev == LPFC_SLI_REV4)
4873 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
4874 else
4875 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05004876
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004877 icmd->ulpLe = 1;
4878 icmd->ulpClass = cmd->ulpClass;
James Smart5ffc2662009-11-18 15:39:44 -05004879
4880 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
4881 abtsiocb->fcp_wqidx = iocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05004882 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05004883
James Smart2e0fef82007-06-17 19:56:36 -05004884 if (lpfc_is_link_up(phba))
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004885 icmd->ulpCommand = CMD_ABORT_XRI_CN;
4886 else
4887 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea31012005-04-17 16:05:31 -05004888
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004889 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smart2e0fef82007-06-17 19:56:36 -05004890 abtsiocb->vport = vport;
James Smart4f2e66c2012-05-09 21:17:07 -04004891 /* no longer need the lock after this point */
James Smart876dd7d2012-09-29 11:31:28 -04004892 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004893
James Smart3772a992009-05-22 14:50:54 -04004894 if (lpfc_sli_issue_iocb(phba, LPFC_FCP_RING, abtsiocb, 0) ==
4895 IOCB_ERROR) {
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004896 lpfc_sli_release_iocbq(phba, abtsiocb);
4897 ret = FAILED;
4898 goto out;
4899 }
4900
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004901 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
James Smart45ed1192009-10-02 15:17:02 -04004902 lpfc_sli_handle_fast_ring_event(phba,
4903 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004904
James Smartfa61a542008-01-11 01:52:42 -05004905 lpfc_cmd->waitq = &waitq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004906 /* Wait for abort to complete */
James Smartfa61a542008-01-11 01:52:42 -05004907 wait_event_timeout(waitq,
4908 (lpfc_cmd->pCmd != cmnd),
James Smart256ec0d2013-04-17 20:14:58 -04004909 msecs_to_jiffies(2*vport->cfg_devloss_tmo*1000));
James Smartfa61a542008-01-11 01:52:42 -05004910 lpfc_cmd->waitq = NULL;
dea31012005-04-17 16:05:31 -05004911
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004912 if (lpfc_cmd->pCmd == cmnd) {
4913 ret = FAILED;
James Smarte8b62012007-08-02 11:10:09 -04004914 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4915 "0748 abort handler timed out waiting "
James Smart247ca942012-08-14 14:26:13 -04004916 "for abortng I/O (xri:x%x) to complete: "
4917 "ret %#x, ID %d, LUN %d\n",
4918 iocb->sli4_xritag, ret,
4919 cmnd->device->id, cmnd->device->lun);
dea31012005-04-17 16:05:31 -05004920 }
James Smart4f2e66c2012-05-09 21:17:07 -04004921 goto out;
dea31012005-04-17 16:05:31 -05004922
James Smart4f2e66c2012-05-09 21:17:07 -04004923out_unlock:
James Smart876dd7d2012-09-29 11:31:28 -04004924 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004925out:
James Smarte8b62012007-08-02 11:10:09 -04004926 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4927 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04004928 "LUN %d\n", ret, cmnd->device->id,
4929 cmnd->device->lun);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004930 return ret;
dea31012005-04-17 16:05:31 -05004931}
4932
James Smartbbb9d182009-06-10 17:23:16 -04004933static char *
4934lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
4935{
4936 switch (task_mgmt_cmd) {
4937 case FCP_ABORT_TASK_SET:
4938 return "ABORT_TASK_SET";
4939 case FCP_CLEAR_TASK_SET:
4940 return "FCP_CLEAR_TASK_SET";
4941 case FCP_BUS_RESET:
4942 return "FCP_BUS_RESET";
4943 case FCP_LUN_RESET:
4944 return "FCP_LUN_RESET";
4945 case FCP_TARGET_RESET:
4946 return "FCP_TARGET_RESET";
4947 case FCP_CLEAR_ACA:
4948 return "FCP_CLEAR_ACA";
4949 case FCP_TERMINATE_TASK:
4950 return "FCP_TERMINATE_TASK";
4951 default:
4952 return "unknown";
4953 }
4954}
4955
4956/**
4957 * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
4958 * @vport: The virtual port for which this call is being executed.
4959 * @rdata: Pointer to remote port local data
4960 * @tgt_id: Target ID of remote device.
4961 * @lun_id: Lun number for the TMF
4962 * @task_mgmt_cmd: type of TMF to send
4963 *
4964 * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
4965 * a remote port.
4966 *
4967 * Return Code:
4968 * 0x2003 - Error
4969 * 0x2002 - Success.
4970 **/
4971static int
4972lpfc_send_taskmgmt(struct lpfc_vport *vport, struct lpfc_rport_data *rdata,
4973 unsigned tgt_id, unsigned int lun_id,
4974 uint8_t task_mgmt_cmd)
4975{
4976 struct lpfc_hba *phba = vport->phba;
4977 struct lpfc_scsi_buf *lpfc_cmd;
4978 struct lpfc_iocbq *iocbq;
4979 struct lpfc_iocbq *iocbqrsp;
James Smart5989b8d2010-10-22 11:06:56 -04004980 struct lpfc_nodelist *pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004981 int ret;
4982 int status;
4983
James Smart5989b8d2010-10-22 11:06:56 -04004984 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
James Smartbbb9d182009-06-10 17:23:16 -04004985 return FAILED;
4986
James Smart19ca7602010-11-20 23:11:55 -05004987 lpfc_cmd = lpfc_get_scsi_buf(phba, rdata->pnode);
James Smartbbb9d182009-06-10 17:23:16 -04004988 if (lpfc_cmd == NULL)
4989 return FAILED;
4990 lpfc_cmd->timeout = 60;
4991 lpfc_cmd->rdata = rdata;
4992
4993 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
4994 task_mgmt_cmd);
4995 if (!status) {
4996 lpfc_release_scsi_buf(phba, lpfc_cmd);
4997 return FAILED;
4998 }
4999
5000 iocbq = &lpfc_cmd->cur_iocbq;
5001 iocbqrsp = lpfc_sli_get_iocbq(phba);
5002 if (iocbqrsp == NULL) {
5003 lpfc_release_scsi_buf(phba, lpfc_cmd);
5004 return FAILED;
5005 }
5006
5007 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5008 "0702 Issue %s to TGT %d LUN %d "
James Smart6d368e52011-05-24 11:44:12 -04005009 "rpi x%x nlp_flag x%x Data: x%x x%x\n",
James Smartbbb9d182009-06-10 17:23:16 -04005010 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
James Smart6d368e52011-05-24 11:44:12 -04005011 pnode->nlp_rpi, pnode->nlp_flag, iocbq->sli4_xritag,
5012 iocbq->iocb_flag);
James Smartbbb9d182009-06-10 17:23:16 -04005013
5014 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
5015 iocbq, iocbqrsp, lpfc_cmd->timeout);
5016 if (status != IOCB_SUCCESS) {
5017 if (status == IOCB_TIMEDOUT) {
5018 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
5019 ret = TIMEOUT_ERROR;
5020 } else
5021 ret = FAILED;
5022 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
5023 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smart6d368e52011-05-24 11:44:12 -04005024 "0727 TMF %s to TGT %d LUN %d failed (%d, %d) "
5025 "iocb_flag x%x\n",
James Smartbbb9d182009-06-10 17:23:16 -04005026 lpfc_taskmgmt_name(task_mgmt_cmd),
5027 tgt_id, lun_id, iocbqrsp->iocb.ulpStatus,
James Smart6d368e52011-05-24 11:44:12 -04005028 iocbqrsp->iocb.un.ulpWord[4],
5029 iocbq->iocb_flag);
James Smart2a9bf3d2010-06-07 15:24:45 -04005030 } else if (status == IOCB_BUSY)
5031 ret = FAILED;
5032 else
James Smartbbb9d182009-06-10 17:23:16 -04005033 ret = SUCCESS;
5034
5035 lpfc_sli_release_iocbq(phba, iocbqrsp);
5036
5037 if (ret != TIMEOUT_ERROR)
5038 lpfc_release_scsi_buf(phba, lpfc_cmd);
5039
5040 return ret;
5041}
5042
5043/**
5044 * lpfc_chk_tgt_mapped -
5045 * @vport: The virtual port to check on
5046 * @cmnd: Pointer to scsi_cmnd data structure.
5047 *
5048 * This routine delays until the scsi target (aka rport) for the
5049 * command exists (is present and logged in) or we declare it non-existent.
5050 *
5051 * Return code :
5052 * 0x2003 - Error
5053 * 0x2002 - Success
5054 **/
5055static int
5056lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
5057{
5058 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005059 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005060 unsigned long later;
5061
James Smart1c6f4ef52009-11-18 15:40:49 -05005062 if (!rdata) {
5063 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5064 "0797 Tgt Map rport failure: rdata x%p\n", rdata);
5065 return FAILED;
5066 }
5067 pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005068 /*
5069 * If target is not in a MAPPED state, delay until
5070 * target is rediscovered or devloss timeout expires.
5071 */
5072 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
5073 while (time_after(later, jiffies)) {
5074 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
5075 return FAILED;
5076 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
5077 return SUCCESS;
5078 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
5079 rdata = cmnd->device->hostdata;
5080 if (!rdata)
5081 return FAILED;
5082 pnode = rdata->pnode;
5083 }
5084 if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
5085 (pnode->nlp_state != NLP_STE_MAPPED_NODE))
5086 return FAILED;
5087 return SUCCESS;
5088}
5089
5090/**
5091 * lpfc_reset_flush_io_context -
5092 * @vport: The virtual port (scsi_host) for the flush context
5093 * @tgt_id: If aborting by Target contect - specifies the target id
5094 * @lun_id: If aborting by Lun context - specifies the lun id
5095 * @context: specifies the context level to flush at.
5096 *
5097 * After a reset condition via TMF, we need to flush orphaned i/o
5098 * contexts from the adapter. This routine aborts any contexts
5099 * outstanding, then waits for their completions. The wait is
5100 * bounded by devloss_tmo though.
5101 *
5102 * Return code :
5103 * 0x2003 - Error
5104 * 0x2002 - Success
5105 **/
5106static int
5107lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
5108 uint64_t lun_id, lpfc_ctx_cmd context)
5109{
5110 struct lpfc_hba *phba = vport->phba;
5111 unsigned long later;
5112 int cnt;
5113
5114 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
5115 if (cnt)
5116 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
5117 tgt_id, lun_id, context);
5118 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
5119 while (time_after(later, jiffies) && cnt) {
5120 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
5121 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
5122 }
5123 if (cnt) {
5124 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5125 "0724 I/O flush failure for context %s : cnt x%x\n",
5126 ((context == LPFC_CTX_LUN) ? "LUN" :
5127 ((context == LPFC_CTX_TGT) ? "TGT" :
5128 ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
5129 cnt);
5130 return FAILED;
5131 }
5132 return SUCCESS;
5133}
5134
James Smart9bad7672008-12-04 22:39:02 -05005135/**
James Smart3621a712009-04-06 18:47:14 -04005136 * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
James Smart9bad7672008-12-04 22:39:02 -05005137 * @cmnd: Pointer to scsi_cmnd data structure.
5138 *
James Smartbbb9d182009-06-10 17:23:16 -04005139 * This routine does a device reset by sending a LUN_RESET task management
James Smart9bad7672008-12-04 22:39:02 -05005140 * command.
5141 *
5142 * Return code :
5143 * 0x2003 - Error
James Smart3621a712009-04-06 18:47:14 -04005144 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05005145 **/
dea31012005-04-17 16:05:31 -05005146static int
James Smart7054a602007-04-25 09:52:34 -04005147lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05005148{
James Smart2e0fef82007-06-17 19:56:36 -05005149 struct Scsi_Host *shost = cmnd->device->host;
5150 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05005151 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005152 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005153 unsigned tgt_id = cmnd->device->id;
5154 unsigned int lun_id = cmnd->device->lun;
James Smartea2151b2008-09-07 11:52:10 -04005155 struct lpfc_scsi_event_header scsi_event;
James Smart3a707302012-06-12 13:54:42 -04005156 int status, ret = SUCCESS;
dea31012005-04-17 16:05:31 -05005157
James Smart1c6f4ef52009-11-18 15:40:49 -05005158 if (!rdata) {
5159 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5160 "0798 Device Reset rport failure: rdata x%p\n", rdata);
5161 return FAILED;
5162 }
5163 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04005164 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005165 if (status != 0 && status != SUCCESS)
James Smart589a52d2010-07-14 15:30:54 -04005166 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005167
5168 status = lpfc_chk_tgt_mapped(vport, cmnd);
5169 if (status == FAILED) {
5170 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5171 "0721 Device Reset rport failure: rdata x%p\n", rdata);
5172 return FAILED;
5173 }
5174
5175 scsi_event.event_type = FC_REG_SCSI_EVENT;
5176 scsi_event.subcategory = LPFC_EVENT_LUNRESET;
5177 scsi_event.lun = lun_id;
5178 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5179 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5180
5181 fc_host_post_vendor_event(shost, fc_get_event_number(),
5182 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
5183
5184 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
5185 FCP_LUN_RESET);
5186
5187 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5188 "0713 SCSI layer issued Device Reset (%d, %d) "
5189 "return x%x\n", tgt_id, lun_id, status);
5190
dea31012005-04-17 16:05:31 -05005191 /*
James Smartbbb9d182009-06-10 17:23:16 -04005192 * We have to clean up i/o as : they may be orphaned by the TMF;
5193 * or if the TMF failed, they may be in an indeterminate state.
5194 * So, continue on.
5195 * We will report success if all the i/o aborts successfully.
dea31012005-04-17 16:05:31 -05005196 */
James Smart3a707302012-06-12 13:54:42 -04005197 ret = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
James Smartbbb9d182009-06-10 17:23:16 -04005198 LPFC_CTX_LUN);
James Smart3a707302012-06-12 13:54:42 -04005199 return ret;
James Smartbbb9d182009-06-10 17:23:16 -04005200}
5201
5202/**
5203 * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
5204 * @cmnd: Pointer to scsi_cmnd data structure.
5205 *
5206 * This routine does a target reset by sending a TARGET_RESET task management
5207 * command.
5208 *
5209 * Return code :
5210 * 0x2003 - Error
5211 * 0x2002 - Success
5212 **/
5213static int
5214lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
5215{
5216 struct Scsi_Host *shost = cmnd->device->host;
5217 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5218 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005219 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005220 unsigned tgt_id = cmnd->device->id;
5221 unsigned int lun_id = cmnd->device->lun;
5222 struct lpfc_scsi_event_header scsi_event;
James Smart3a707302012-06-12 13:54:42 -04005223 int status, ret = SUCCESS;
James Smartbbb9d182009-06-10 17:23:16 -04005224
James Smart1c6f4ef52009-11-18 15:40:49 -05005225 if (!rdata) {
5226 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5227 "0799 Target Reset rport failure: rdata x%p\n", rdata);
5228 return FAILED;
5229 }
5230 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04005231 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005232 if (status != 0 && status != SUCCESS)
James Smart589a52d2010-07-14 15:30:54 -04005233 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005234
5235 status = lpfc_chk_tgt_mapped(vport, cmnd);
5236 if (status == FAILED) {
5237 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5238 "0722 Target Reset rport failure: rdata x%p\n", rdata);
5239 return FAILED;
dea31012005-04-17 16:05:31 -05005240 }
James Smartea2151b2008-09-07 11:52:10 -04005241
5242 scsi_event.event_type = FC_REG_SCSI_EVENT;
5243 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
5244 scsi_event.lun = 0;
5245 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5246 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5247
James Smartbbb9d182009-06-10 17:23:16 -04005248 fc_host_post_vendor_event(shost, fc_get_event_number(),
5249 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -04005250
James Smartbbb9d182009-06-10 17:23:16 -04005251 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
5252 FCP_TARGET_RESET);
dea31012005-04-17 16:05:31 -05005253
James Smarte8b62012007-08-02 11:10:09 -04005254 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smartbbb9d182009-06-10 17:23:16 -04005255 "0723 SCSI layer issued Target Reset (%d, %d) "
5256 "return x%x\n", tgt_id, lun_id, status);
5257
5258 /*
5259 * We have to clean up i/o as : they may be orphaned by the TMF;
5260 * or if the TMF failed, they may be in an indeterminate state.
5261 * So, continue on.
5262 * We will report success if all the i/o aborts successfully.
5263 */
James Smart3a707302012-06-12 13:54:42 -04005264 ret = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
5265 LPFC_CTX_TGT);
5266 return ret;
dea31012005-04-17 16:05:31 -05005267}
5268
James Smart9bad7672008-12-04 22:39:02 -05005269/**
James Smart3621a712009-04-06 18:47:14 -04005270 * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05005271 * @cmnd: Pointer to scsi_cmnd data structure.
5272 *
James Smartbbb9d182009-06-10 17:23:16 -04005273 * This routine does target reset to all targets on @cmnd->device->host.
5274 * This emulates Parallel SCSI Bus Reset Semantics.
James Smart9bad7672008-12-04 22:39:02 -05005275 *
James Smartbbb9d182009-06-10 17:23:16 -04005276 * Return code :
5277 * 0x2003 - Error
5278 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05005279 **/
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04005280static int
James Smart7054a602007-04-25 09:52:34 -04005281lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05005282{
James Smart2e0fef82007-06-17 19:56:36 -05005283 struct Scsi_Host *shost = cmnd->device->host;
5284 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05005285 struct lpfc_nodelist *ndlp = NULL;
James Smartea2151b2008-09-07 11:52:10 -04005286 struct lpfc_scsi_event_header scsi_event;
James Smartbbb9d182009-06-10 17:23:16 -04005287 int match;
5288 int ret = SUCCESS, status, i;
James Smartea2151b2008-09-07 11:52:10 -04005289
5290 scsi_event.event_type = FC_REG_SCSI_EVENT;
5291 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
5292 scsi_event.lun = 0;
5293 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
5294 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
5295
James Smartbbb9d182009-06-10 17:23:16 -04005296 fc_host_post_vendor_event(shost, fc_get_event_number(),
5297 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
dea31012005-04-17 16:05:31 -05005298
James Smartbf086112011-08-21 21:48:13 -04005299 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005300 if (status != 0 && status != SUCCESS)
James Smartbf086112011-08-21 21:48:13 -04005301 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005302
dea31012005-04-17 16:05:31 -05005303 /*
5304 * Since the driver manages a single bus device, reset all
5305 * targets known to the driver. Should any target reset
5306 * fail, this routine returns failure to the midlayer.
5307 */
James Smarte17da182006-07-06 15:49:25 -04005308 for (i = 0; i < LPFC_MAX_TARGET; i++) {
James Smart685f0bf2007-04-25 09:53:08 -04005309 /* Search for mapped node by target ID */
dea31012005-04-17 16:05:31 -05005310 match = 0;
James Smart2e0fef82007-06-17 19:56:36 -05005311 spin_lock_irq(shost->host_lock);
5312 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05005313 if (!NLP_CHK_NODE_ACT(ndlp))
5314 continue;
James Smarta6571c62012-10-31 14:44:42 -04005315 if (vport->phba->cfg_fcp2_no_tgt_reset &&
5316 (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE))
5317 continue;
James Smart685f0bf2007-04-25 09:53:08 -04005318 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
James Smart915caaa2008-06-14 22:52:38 -04005319 ndlp->nlp_sid == i &&
James Smart685f0bf2007-04-25 09:53:08 -04005320 ndlp->rport) {
dea31012005-04-17 16:05:31 -05005321 match = 1;
5322 break;
5323 }
5324 }
James Smart2e0fef82007-06-17 19:56:36 -05005325 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005326 if (!match)
5327 continue;
James Smartbbb9d182009-06-10 17:23:16 -04005328
5329 status = lpfc_send_taskmgmt(vport, ndlp->rport->dd_data,
5330 i, 0, FCP_TARGET_RESET);
5331
5332 if (status != SUCCESS) {
James Smarte8b62012007-08-02 11:10:09 -04005333 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5334 "0700 Bus Reset on target %d failed\n",
5335 i);
James Smart915caaa2008-06-14 22:52:38 -04005336 ret = FAILED;
dea31012005-04-17 16:05:31 -05005337 }
5338 }
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005339 /*
James Smartbbb9d182009-06-10 17:23:16 -04005340 * We have to clean up i/o as : they may be orphaned by the TMFs
5341 * above; or if any of the TMFs failed, they may be in an
5342 * indeterminate state.
5343 * We will report success if all the i/o aborts successfully.
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005344 */
James Smartbbb9d182009-06-10 17:23:16 -04005345
5346 status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
5347 if (status != SUCCESS)
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005348 ret = FAILED;
James Smartbbb9d182009-06-10 17:23:16 -04005349
James Smarte8b62012007-08-02 11:10:09 -04005350 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5351 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea31012005-04-17 16:05:31 -05005352 return ret;
5353}
5354
James Smart9bad7672008-12-04 22:39:02 -05005355/**
James Smart27b01b82012-05-09 21:19:44 -04005356 * lpfc_host_reset_handler - scsi_host_template eh_host_reset_handler entry pt
5357 * @cmnd: Pointer to scsi_cmnd data structure.
5358 *
5359 * This routine does host reset to the adaptor port. It brings the HBA
5360 * offline, performs a board restart, and then brings the board back online.
5361 * The lpfc_offline calls lpfc_sli_hba_down which will abort and local
5362 * reject all outstanding SCSI commands to the host and error returned
5363 * back to SCSI mid-level. As this will be SCSI mid-level's last resort
5364 * of error handling, it will only return error if resetting of the adapter
5365 * is not successful; in all other cases, will return success.
5366 *
5367 * Return code :
5368 * 0x2003 - Error
5369 * 0x2002 - Success
5370 **/
5371static int
5372lpfc_host_reset_handler(struct scsi_cmnd *cmnd)
5373{
5374 struct Scsi_Host *shost = cmnd->device->host;
5375 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5376 struct lpfc_hba *phba = vport->phba;
5377 int rc, ret = SUCCESS;
5378
James Smart618a5232012-06-12 13:54:36 -04005379 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart27b01b82012-05-09 21:19:44 -04005380 lpfc_offline(phba);
5381 rc = lpfc_sli_brdrestart(phba);
5382 if (rc)
5383 ret = FAILED;
5384 lpfc_online(phba);
5385 lpfc_unblock_mgmt_io(phba);
5386
5387 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
5388 "3172 SCSI layer issued Host Reset Data: x%x\n", ret);
5389 return ret;
5390}
5391
5392/**
James Smart3621a712009-04-06 18:47:14 -04005393 * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
James Smart9bad7672008-12-04 22:39:02 -05005394 * @sdev: Pointer to scsi_device.
5395 *
5396 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
5397 * globally available list of scsi buffers. This routine also makes sure scsi
5398 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
5399 * of scsi buffer exists for the lifetime of the driver.
5400 *
5401 * Return codes:
5402 * non-0 - Error
5403 * 0 - Success
5404 **/
dea31012005-04-17 16:05:31 -05005405static int
dea31012005-04-17 16:05:31 -05005406lpfc_slave_alloc(struct scsi_device *sdev)
5407{
James Smart2e0fef82007-06-17 19:56:36 -05005408 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5409 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005410 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
James Smart3772a992009-05-22 14:50:54 -04005411 uint32_t total = 0;
dea31012005-04-17 16:05:31 -05005412 uint32_t num_to_alloc = 0;
James Smart3772a992009-05-22 14:50:54 -04005413 int num_allocated = 0;
James Smartd7c47992010-06-08 18:31:54 -04005414 uint32_t sdev_cnt;
dea31012005-04-17 16:05:31 -05005415
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005416 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05005417 return -ENXIO;
5418
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005419 sdev->hostdata = rport->dd_data;
James Smartd7c47992010-06-08 18:31:54 -04005420 sdev_cnt = atomic_inc_return(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05005421
5422 /*
5423 * Populate the cmds_per_lun count scsi_bufs into this host's globally
5424 * available list of scsi buffers. Don't allocate more than the
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04005425 * HBA limit conveyed to the midlayer via the host structure. The
5426 * formula accounts for the lun_queue_depth + error handlers + 1
5427 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea31012005-04-17 16:05:31 -05005428 */
5429 total = phba->total_scsi_bufs;
James Smart3de2a652007-08-02 11:09:59 -04005430 num_to_alloc = vport->cfg_lun_queue_depth + 2;
James Smart92d7f7b2007-06-17 19:56:38 -05005431
James Smartd7c47992010-06-08 18:31:54 -04005432 /* If allocated buffers are enough do nothing */
5433 if ((sdev_cnt * (vport->cfg_lun_queue_depth + 2)) < total)
5434 return 0;
5435
James Smart92d7f7b2007-06-17 19:56:38 -05005436 /* Allow some exchanges to be available always to complete discovery */
5437 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04005438 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5439 "0704 At limitation of %d preallocated "
5440 "command buffers\n", total);
dea31012005-04-17 16:05:31 -05005441 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05005442 /* Allow some exchanges to be available always to complete discovery */
5443 } else if (total + num_to_alloc >
5444 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04005445 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5446 "0705 Allocation request of %d "
5447 "command buffers will exceed max of %d. "
5448 "Reducing allocation request to %d.\n",
5449 num_to_alloc, phba->cfg_hba_queue_depth,
5450 (phba->cfg_hba_queue_depth - total));
dea31012005-04-17 16:05:31 -05005451 num_to_alloc = phba->cfg_hba_queue_depth - total;
5452 }
James Smart3772a992009-05-22 14:50:54 -04005453 num_allocated = lpfc_new_scsi_buf(vport, num_to_alloc);
5454 if (num_to_alloc != num_allocated) {
James Smart96f70772013-04-17 20:16:15 -04005455 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5456 "0708 Allocation request of %d "
5457 "command buffers did not succeed. "
5458 "Allocated %d buffers.\n",
5459 num_to_alloc, num_allocated);
dea31012005-04-17 16:05:31 -05005460 }
James Smart1c6f4ef52009-11-18 15:40:49 -05005461 if (num_allocated > 0)
5462 phba->total_scsi_bufs += num_allocated;
dea31012005-04-17 16:05:31 -05005463 return 0;
5464}
5465
James Smart9bad7672008-12-04 22:39:02 -05005466/**
James Smart3621a712009-04-06 18:47:14 -04005467 * lpfc_slave_configure - scsi_host_template slave_configure entry point
James Smart9bad7672008-12-04 22:39:02 -05005468 * @sdev: Pointer to scsi_device.
5469 *
5470 * This routine configures following items
5471 * - Tag command queuing support for @sdev if supported.
James Smart9bad7672008-12-04 22:39:02 -05005472 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
5473 *
5474 * Return codes:
5475 * 0 - Success
5476 **/
dea31012005-04-17 16:05:31 -05005477static int
5478lpfc_slave_configure(struct scsi_device *sdev)
5479{
James Smart2e0fef82007-06-17 19:56:36 -05005480 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5481 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05005482
5483 if (sdev->tagged_supported)
James Smart3de2a652007-08-02 11:09:59 -04005484 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05005485 else
James Smart3de2a652007-08-02 11:09:59 -04005486 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05005487
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005488 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04005489 lpfc_sli_handle_fast_ring_event(phba,
5490 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005491 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
5492 lpfc_poll_rearm_timer(phba);
5493 }
5494
dea31012005-04-17 16:05:31 -05005495 return 0;
5496}
5497
James Smart9bad7672008-12-04 22:39:02 -05005498/**
James Smart3621a712009-04-06 18:47:14 -04005499 * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
James Smart9bad7672008-12-04 22:39:02 -05005500 * @sdev: Pointer to scsi_device.
5501 *
5502 * This routine sets @sdev hostatdata filed to null.
5503 **/
dea31012005-04-17 16:05:31 -05005504static void
5505lpfc_slave_destroy(struct scsi_device *sdev)
5506{
James Smartd7c47992010-06-08 18:31:54 -04005507 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5508 struct lpfc_hba *phba = vport->phba;
5509 atomic_dec(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05005510 sdev->hostdata = NULL;
5511 return;
5512}
5513
James Smart92d7f7b2007-06-17 19:56:38 -05005514
dea31012005-04-17 16:05:31 -05005515struct scsi_host_template lpfc_template = {
5516 .module = THIS_MODULE,
5517 .name = LPFC_DRIVER_NAME,
5518 .info = lpfc_info,
5519 .queuecommand = lpfc_queuecommand,
5520 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04005521 .eh_device_reset_handler = lpfc_device_reset_handler,
5522 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart7054a602007-04-25 09:52:34 -04005523 .eh_bus_reset_handler = lpfc_bus_reset_handler,
James Smart27b01b82012-05-09 21:19:44 -04005524 .eh_host_reset_handler = lpfc_host_reset_handler,
dea31012005-04-17 16:05:31 -05005525 .slave_alloc = lpfc_slave_alloc,
5526 .slave_configure = lpfc_slave_configure,
5527 .slave_destroy = lpfc_slave_destroy,
James Smart47a86172007-04-25 09:53:22 -04005528 .scan_finished = lpfc_scan_finished,
dea31012005-04-17 16:05:31 -05005529 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05005530 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
dea31012005-04-17 16:05:31 -05005531 .cmd_per_lun = LPFC_CMD_PER_LUN,
5532 .use_clustering = ENABLE_CLUSTERING,
James Smart2e0fef82007-06-17 19:56:36 -05005533 .shost_attrs = lpfc_hba_attrs,
James.Smart@Emulex.Com564b2962005-06-25 10:34:17 -04005534 .max_sectors = 0xFFFF,
James Smartf1c3b0f2009-07-19 10:01:32 -04005535 .vendor_id = LPFC_NL_VENDOR_ID,
James Smart5ffc2662009-11-18 15:39:44 -05005536 .change_queue_depth = lpfc_change_queue_depth,
James Smartfe8f7f92013-01-03 15:43:03 -05005537 .change_queue_type = lpfc_change_queue_type,
dea31012005-04-17 16:05:31 -05005538};
James Smart3de2a652007-08-02 11:09:59 -04005539
5540struct scsi_host_template lpfc_vport_template = {
5541 .module = THIS_MODULE,
5542 .name = LPFC_DRIVER_NAME,
5543 .info = lpfc_info,
5544 .queuecommand = lpfc_queuecommand,
5545 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04005546 .eh_device_reset_handler = lpfc_device_reset_handler,
5547 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart3de2a652007-08-02 11:09:59 -04005548 .eh_bus_reset_handler = lpfc_bus_reset_handler,
5549 .slave_alloc = lpfc_slave_alloc,
5550 .slave_configure = lpfc_slave_configure,
5551 .slave_destroy = lpfc_slave_destroy,
5552 .scan_finished = lpfc_scan_finished,
5553 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05005554 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
James Smart3de2a652007-08-02 11:09:59 -04005555 .cmd_per_lun = LPFC_CMD_PER_LUN,
5556 .use_clustering = ENABLE_CLUSTERING,
5557 .shost_attrs = lpfc_vport_attrs,
5558 .max_sectors = 0xFFFF,
James Smart5ffc2662009-11-18 15:39:44 -05005559 .change_queue_depth = lpfc_change_queue_depth,
James Smartfe8f7f92013-01-03 15:43:03 -05005560 .change_queue_type = lpfc_change_queue_type,
James Smart3de2a652007-08-02 11:09:59 -04005561};