blob: 5d63dadbe0e1d026438e78828a3bee9c0c2883ca [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 Smart6a485eb2013-04-17 20:19:00 -04001218 psb->cur_iocbq.iocb_flag = LPFC_IO_FCP;
James Smarta40fc5f2013-04-17 20:17:40 -04001219 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list_put);
1220 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
dea31012005-04-17 16:05:31 -05001221}
1222
James Smart9bad7672008-12-04 22:39:02 -05001223/**
James Smartda0436e2009-05-22 14:51:39 -04001224 * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
1225 * @phba: The Hba for which this call is being executed.
1226 * @psb: The scsi buffer which is being released.
1227 *
1228 * This routine releases @psb scsi buffer by adding it to tail of @phba
1229 * lpfc_scsi_buf_list list. For SLI4 XRI's are tied to the scsi buffer
1230 * and cannot be reused for at least RA_TOV amount of time if it was
1231 * aborted.
1232 **/
1233static void
1234lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1235{
1236 unsigned long iflag = 0;
1237
James Smarta40fc5f2013-04-17 20:17:40 -04001238 psb->seg_cnt = 0;
1239 psb->nonsg_phys = 0;
1240 psb->prot_seg_cnt = 0;
1241
James Smart341af102010-01-26 23:07:37 -05001242 if (psb->exch_busy) {
James Smartda0436e2009-05-22 14:51:39 -04001243 spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock,
1244 iflag);
1245 psb->pCmd = NULL;
1246 list_add_tail(&psb->list,
1247 &phba->sli4_hba.lpfc_abts_scsi_buf_list);
1248 spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
1249 iflag);
1250 } else {
James Smartda0436e2009-05-22 14:51:39 -04001251 psb->pCmd = NULL;
James Smart6a485eb2013-04-17 20:19:00 -04001252 psb->cur_iocbq.iocb_flag = LPFC_IO_FCP;
James Smarta40fc5f2013-04-17 20:17:40 -04001253 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
1254 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list_put);
1255 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
James Smartda0436e2009-05-22 14:51:39 -04001256 }
1257}
1258
1259/**
James Smart3772a992009-05-22 14:50:54 -04001260 * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
1261 * @phba: The Hba for which this call is being executed.
1262 * @psb: The scsi buffer which is being released.
1263 *
1264 * This routine releases @psb scsi buffer by adding it to tail of @phba
1265 * lpfc_scsi_buf_list list.
1266 **/
1267static void
1268lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1269{
1270
1271 phba->lpfc_release_scsi_buf(phba, psb);
1272}
1273
1274/**
1275 * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -05001276 * @phba: The Hba for which this call is being executed.
1277 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1278 *
1279 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
James Smart3772a992009-05-22 14:50:54 -04001280 * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
1281 * through sg elements and format the bdea. This routine also initializes all
1282 * IOCB fields which are dependent on scsi command request buffer.
James Smart9bad7672008-12-04 22:39:02 -05001283 *
1284 * Return codes:
1285 * 1 - Error
1286 * 0 - Success
1287 **/
dea31012005-04-17 16:05:31 -05001288static int
James Smart3772a992009-05-22 14:50:54 -04001289lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
dea31012005-04-17 16:05:31 -05001290{
1291 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1292 struct scatterlist *sgel = NULL;
1293 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1294 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
James Smart0f65ff62010-02-26 14:14:23 -05001295 struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
dea31012005-04-17 16:05:31 -05001296 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart34b02dc2008-08-24 21:49:55 -04001297 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
dea31012005-04-17 16:05:31 -05001298 dma_addr_t physaddr;
James Smart34b02dc2008-08-24 21:49:55 -04001299 uint32_t num_bde = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001300 int nseg, datadir = scsi_cmnd->sc_data_direction;
dea31012005-04-17 16:05:31 -05001301
1302 /*
1303 * There are three possibilities here - use scatter-gather segment, use
1304 * the single mapping, or neither. Start the lpfc command prep by
1305 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1306 * data bde entry.
1307 */
1308 bpl += 2;
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001309 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05001310 /*
1311 * The driver stores the segment count returned from pci_map_sg
1312 * because this a count of dma-mappings used to map the use_sg
1313 * pages. They are not guaranteed to be the same for those
1314 * architectures that implement an IOMMU.
1315 */
dea31012005-04-17 16:05:31 -05001316
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001317 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
1318 scsi_sg_count(scsi_cmnd), datadir);
1319 if (unlikely(!nseg))
1320 return 1;
1321
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001322 lpfc_cmd->seg_cnt = nseg;
dea31012005-04-17 16:05:31 -05001323 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001324 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1325 "9064 BLKGRD: %s: Too many sg segments from "
James Smarte2a0a9d2008-12-04 22:40:02 -05001326 "dma_map_sg. Config %d, seg_cnt %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001327 __func__, phba->cfg_sg_seg_cnt,
dea31012005-04-17 16:05:31 -05001328 lpfc_cmd->seg_cnt);
James Smart96f70772013-04-17 20:16:15 -04001329 lpfc_cmd->seg_cnt = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001330 scsi_dma_unmap(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001331 return 1;
1332 }
1333
1334 /*
1335 * The driver established a maximum scatter-gather segment count
1336 * during probe that limits the number of sg elements in any
1337 * single scsi command. Just run through the seg_cnt and format
1338 * the bde's.
James Smart34b02dc2008-08-24 21:49:55 -04001339 * When using SLI-3 the driver will try to fit all the BDEs into
1340 * the IOCB. If it can't then the BDEs get added to a BPL as it
1341 * does for SLI-2 mode.
dea31012005-04-17 16:05:31 -05001342 */
James Smart34b02dc2008-08-24 21:49:55 -04001343 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
dea31012005-04-17 16:05:31 -05001344 physaddr = sg_dma_address(sgel);
James Smart34b02dc2008-08-24 21:49:55 -04001345 if (phba->sli_rev == 3 &&
James Smarte2a0a9d2008-12-04 22:40:02 -05001346 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
James Smart0f65ff62010-02-26 14:14:23 -05001347 !(iocbq->iocb_flag & DSS_SECURITY_OP) &&
James Smart34b02dc2008-08-24 21:49:55 -04001348 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
1349 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1350 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
1351 data_bde->addrLow = putPaddrLow(physaddr);
1352 data_bde->addrHigh = putPaddrHigh(physaddr);
1353 data_bde++;
1354 } else {
1355 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1356 bpl->tus.f.bdeSize = sg_dma_len(sgel);
1357 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1358 bpl->addrLow =
1359 le32_to_cpu(putPaddrLow(physaddr));
1360 bpl->addrHigh =
1361 le32_to_cpu(putPaddrHigh(physaddr));
1362 bpl++;
1363 }
dea31012005-04-17 16:05:31 -05001364 }
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001365 }
dea31012005-04-17 16:05:31 -05001366
1367 /*
1368 * Finish initializing those IOCB fields that are dependent on the
James Smart34b02dc2008-08-24 21:49:55 -04001369 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
1370 * explicitly reinitialized and for SLI-3 the extended bde count is
1371 * explicitly reinitialized since all iocb memory resources are reused.
dea31012005-04-17 16:05:31 -05001372 */
James Smarte2a0a9d2008-12-04 22:40:02 -05001373 if (phba->sli_rev == 3 &&
James Smart0f65ff62010-02-26 14:14:23 -05001374 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
1375 !(iocbq->iocb_flag & DSS_SECURITY_OP)) {
James Smart34b02dc2008-08-24 21:49:55 -04001376 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
1377 /*
1378 * The extended IOCB format can only fit 3 BDE or a BPL.
1379 * This I/O has more than 3 BDE so the 1st data bde will
1380 * be a BPL that is filled in here.
1381 */
1382 physaddr = lpfc_cmd->dma_handle;
1383 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
1384 data_bde->tus.f.bdeSize = (num_bde *
1385 sizeof(struct ulp_bde64));
1386 physaddr += (sizeof(struct fcp_cmnd) +
1387 sizeof(struct fcp_rsp) +
1388 (2 * sizeof(struct ulp_bde64)));
1389 data_bde->addrHigh = putPaddrHigh(physaddr);
1390 data_bde->addrLow = putPaddrLow(physaddr);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001391 /* ebde count includes the response bde and data bpl */
James Smart34b02dc2008-08-24 21:49:55 -04001392 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
1393 } else {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001394 /* ebde count includes the response bde and data bdes */
James Smart34b02dc2008-08-24 21:49:55 -04001395 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1396 }
1397 } else {
1398 iocb_cmd->un.fcpi64.bdl.bdeSize =
1399 ((num_bde + 2) * sizeof(struct ulp_bde64));
James Smart0f65ff62010-02-26 14:14:23 -05001400 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
James Smart34b02dc2008-08-24 21:49:55 -04001401 }
James Smart09372822008-01-11 01:52:54 -05001402 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
James Smarte2a0a9d2008-12-04 22:40:02 -05001403
1404 /*
1405 * Due to difference in data length between DIF/non-DIF paths,
1406 * we need to set word 4 of IOCB here
1407 */
James Smarta257bf92009-04-06 18:48:10 -04001408 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001409 return 0;
1410}
1411
James Smartf9bb2da2011-10-10 21:34:11 -04001412static inline unsigned
1413lpfc_cmd_blksize(struct scsi_cmnd *sc)
1414{
1415 return sc->device->sector_size;
1416}
1417
1418#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001419
James Smart9a6b09c2012-03-01 22:37:42 -05001420/* Return if if error injection is detected by Initiator */
1421#define BG_ERR_INIT 0x1
1422/* Return if if error injection is detected by Target */
1423#define BG_ERR_TGT 0x2
1424/* Return if if swapping CSUM<-->CRC is required for error injection */
1425#define BG_ERR_SWAP 0x10
1426/* Return if disabling Guard/Ref/App checking is required for error injection */
1427#define BG_ERR_CHECK 0x20
James Smartacd68592012-01-18 16:25:09 -05001428
1429/**
1430 * lpfc_bg_err_inject - Determine if we should inject an error
1431 * @phba: The Hba for which this call is being executed.
James Smartf9bb2da2011-10-10 21:34:11 -04001432 * @sc: The SCSI command to examine
1433 * @reftag: (out) BlockGuard reference tag for transmitted data
1434 * @apptag: (out) BlockGuard application tag for transmitted data
1435 * @new_guard (in) Value to replace CRC with if needed
1436 *
James Smart9a6b09c2012-03-01 22:37:42 -05001437 * Returns BG_ERR_* bit mask or 0 if request ignored
James Smartacd68592012-01-18 16:25:09 -05001438 **/
James Smartf9bb2da2011-10-10 21:34:11 -04001439static int
1440lpfc_bg_err_inject(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1441 uint32_t *reftag, uint16_t *apptag, uint32_t new_guard)
1442{
1443 struct scatterlist *sgpe; /* s/g prot entry */
1444 struct scatterlist *sgde; /* s/g data entry */
James Smart9a6b09c2012-03-01 22:37:42 -05001445 struct lpfc_scsi_buf *lpfc_cmd = NULL;
James Smartacd68592012-01-18 16:25:09 -05001446 struct scsi_dif_tuple *src = NULL;
James Smart4ac9b222012-03-01 22:38:29 -05001447 struct lpfc_nodelist *ndlp;
1448 struct lpfc_rport_data *rdata;
James Smartf9bb2da2011-10-10 21:34:11 -04001449 uint32_t op = scsi_get_prot_op(sc);
1450 uint32_t blksize;
1451 uint32_t numblks;
1452 sector_t lba;
1453 int rc = 0;
James Smartacd68592012-01-18 16:25:09 -05001454 int blockoff = 0;
James Smartf9bb2da2011-10-10 21:34:11 -04001455
1456 if (op == SCSI_PROT_NORMAL)
1457 return 0;
1458
James Smartacd68592012-01-18 16:25:09 -05001459 sgpe = scsi_prot_sglist(sc);
1460 sgde = scsi_sglist(sc);
James Smartf9bb2da2011-10-10 21:34:11 -04001461 lba = scsi_get_lba(sc);
James Smart4ac9b222012-03-01 22:38:29 -05001462
1463 /* First check if we need to match the LBA */
James Smartf9bb2da2011-10-10 21:34:11 -04001464 if (phba->lpfc_injerr_lba != LPFC_INJERR_LBA_OFF) {
1465 blksize = lpfc_cmd_blksize(sc);
1466 numblks = (scsi_bufflen(sc) + blksize - 1) / blksize;
1467
1468 /* Make sure we have the right LBA if one is specified */
1469 if ((phba->lpfc_injerr_lba < lba) ||
1470 (phba->lpfc_injerr_lba >= (lba + numblks)))
1471 return 0;
James Smartacd68592012-01-18 16:25:09 -05001472 if (sgpe) {
1473 blockoff = phba->lpfc_injerr_lba - lba;
1474 numblks = sg_dma_len(sgpe) /
1475 sizeof(struct scsi_dif_tuple);
1476 if (numblks < blockoff)
1477 blockoff = numblks;
James Smartacd68592012-01-18 16:25:09 -05001478 }
James Smartf9bb2da2011-10-10 21:34:11 -04001479 }
1480
James Smart4ac9b222012-03-01 22:38:29 -05001481 /* Next check if we need to match the remote NPortID or WWPN */
1482 rdata = sc->device->hostdata;
1483 if (rdata && rdata->pnode) {
1484 ndlp = rdata->pnode;
1485
1486 /* Make sure we have the right NPortID if one is specified */
1487 if (phba->lpfc_injerr_nportid &&
1488 (phba->lpfc_injerr_nportid != ndlp->nlp_DID))
1489 return 0;
1490
1491 /*
1492 * Make sure we have the right WWPN if one is specified.
1493 * wwn[0] should be a non-zero NAA in a good WWPN.
1494 */
1495 if (phba->lpfc_injerr_wwpn.u.wwn[0] &&
1496 (memcmp(&ndlp->nlp_portname, &phba->lpfc_injerr_wwpn,
1497 sizeof(struct lpfc_name)) != 0))
1498 return 0;
1499 }
1500
1501 /* Setup a ptr to the protection data if the SCSI host provides it */
1502 if (sgpe) {
1503 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
1504 src += blockoff;
1505 lpfc_cmd = (struct lpfc_scsi_buf *)sc->host_scribble;
1506 }
1507
James Smartf9bb2da2011-10-10 21:34:11 -04001508 /* Should we change the Reference Tag */
1509 if (reftag) {
James Smartacd68592012-01-18 16:25:09 -05001510 if (phba->lpfc_injerr_wref_cnt) {
1511 switch (op) {
1512 case SCSI_PROT_WRITE_PASS:
James Smart9a6b09c2012-03-01 22:37:42 -05001513 if (src) {
1514 /*
1515 * For WRITE_PASS, force the error
1516 * to be sent on the wire. It should
1517 * be detected by the Target.
1518 * If blockoff != 0 error will be
1519 * inserted in middle of the IO.
1520 */
James Smartf9bb2da2011-10-10 21:34:11 -04001521
James Smartacd68592012-01-18 16:25:09 -05001522 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1523 "9076 BLKGRD: Injecting reftag error: "
1524 "write lba x%lx + x%x oldrefTag x%x\n",
1525 (unsigned long)lba, blockoff,
James Smart9a6b09c2012-03-01 22:37:42 -05001526 be32_to_cpu(src->ref_tag));
James Smartacd68592012-01-18 16:25:09 -05001527
1528 /*
James Smart9a6b09c2012-03-01 22:37:42 -05001529 * Save the old ref_tag so we can
1530 * restore it on completion.
James Smartacd68592012-01-18 16:25:09 -05001531 */
James Smart9a6b09c2012-03-01 22:37:42 -05001532 if (lpfc_cmd) {
1533 lpfc_cmd->prot_data_type =
1534 LPFC_INJERR_REFTAG;
1535 lpfc_cmd->prot_data_segment =
1536 src;
1537 lpfc_cmd->prot_data =
1538 src->ref_tag;
1539 }
1540 src->ref_tag = cpu_to_be32(0xDEADBEEF);
James Smartacd68592012-01-18 16:25:09 -05001541 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001542 if (phba->lpfc_injerr_wref_cnt == 0) {
1543 phba->lpfc_injerr_nportid = 0;
1544 phba->lpfc_injerr_lba =
1545 LPFC_INJERR_LBA_OFF;
1546 memset(&phba->lpfc_injerr_wwpn,
1547 0, sizeof(struct lpfc_name));
1548 }
James Smart9a6b09c2012-03-01 22:37:42 -05001549 rc = BG_ERR_TGT | BG_ERR_CHECK;
1550
James Smartacd68592012-01-18 16:25:09 -05001551 break;
1552 }
1553 /* Drop thru */
James Smart9a6b09c2012-03-01 22:37:42 -05001554 case SCSI_PROT_WRITE_INSERT:
1555 /*
1556 * For WRITE_INSERT, force the error
1557 * to be sent on the wire. It should be
1558 * detected by the Target.
1559 */
1560 /* DEADBEEF will be the reftag on the wire */
1561 *reftag = 0xDEADBEEF;
1562 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001563 if (phba->lpfc_injerr_wref_cnt == 0) {
1564 phba->lpfc_injerr_nportid = 0;
1565 phba->lpfc_injerr_lba =
1566 LPFC_INJERR_LBA_OFF;
1567 memset(&phba->lpfc_injerr_wwpn,
1568 0, sizeof(struct lpfc_name));
1569 }
James Smart9a6b09c2012-03-01 22:37:42 -05001570 rc = BG_ERR_TGT | BG_ERR_CHECK;
1571
1572 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1573 "9078 BLKGRD: Injecting reftag error: "
1574 "write lba x%lx\n", (unsigned long)lba);
1575 break;
James Smartacd68592012-01-18 16:25:09 -05001576 case SCSI_PROT_WRITE_STRIP:
1577 /*
1578 * For WRITE_STRIP and WRITE_PASS,
1579 * force the error on data
1580 * being copied from SLI-Host to SLI-Port.
1581 */
1582 *reftag = 0xDEADBEEF;
1583 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001584 if (phba->lpfc_injerr_wref_cnt == 0) {
1585 phba->lpfc_injerr_nportid = 0;
1586 phba->lpfc_injerr_lba =
1587 LPFC_INJERR_LBA_OFF;
1588 memset(&phba->lpfc_injerr_wwpn,
1589 0, sizeof(struct lpfc_name));
1590 }
James Smartacd68592012-01-18 16:25:09 -05001591 rc = BG_ERR_INIT;
1592
1593 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1594 "9077 BLKGRD: Injecting reftag error: "
1595 "write lba x%lx\n", (unsigned long)lba);
1596 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001597 }
James Smartacd68592012-01-18 16:25:09 -05001598 }
1599 if (phba->lpfc_injerr_rref_cnt) {
1600 switch (op) {
1601 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001602 case SCSI_PROT_READ_STRIP:
1603 case SCSI_PROT_READ_PASS:
1604 /*
1605 * For READ_STRIP and READ_PASS, force the
1606 * error on data being read off the wire. It
1607 * should force an IO error to the driver.
1608 */
James Smartf9bb2da2011-10-10 21:34:11 -04001609 *reftag = 0xDEADBEEF;
1610 phba->lpfc_injerr_rref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001611 if (phba->lpfc_injerr_rref_cnt == 0) {
1612 phba->lpfc_injerr_nportid = 0;
1613 phba->lpfc_injerr_lba =
1614 LPFC_INJERR_LBA_OFF;
1615 memset(&phba->lpfc_injerr_wwpn,
1616 0, sizeof(struct lpfc_name));
1617 }
James Smartacd68592012-01-18 16:25:09 -05001618 rc = BG_ERR_INIT;
James Smartf9bb2da2011-10-10 21:34:11 -04001619
1620 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smartacd68592012-01-18 16:25:09 -05001621 "9079 BLKGRD: Injecting reftag error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001622 "read lba x%lx\n", (unsigned long)lba);
James Smartacd68592012-01-18 16:25:09 -05001623 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001624 }
1625 }
1626 }
1627
1628 /* Should we change the Application Tag */
1629 if (apptag) {
James Smartacd68592012-01-18 16:25:09 -05001630 if (phba->lpfc_injerr_wapp_cnt) {
1631 switch (op) {
1632 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001633 if (src) {
James Smart9a6b09c2012-03-01 22:37:42 -05001634 /*
1635 * For WRITE_PASS, force the error
1636 * to be sent on the wire. It should
1637 * be detected by the Target.
1638 * If blockoff != 0 error will be
1639 * inserted in middle of the IO.
1640 */
1641
James Smartacd68592012-01-18 16:25:09 -05001642 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1643 "9080 BLKGRD: Injecting apptag error: "
1644 "write lba x%lx + x%x oldappTag x%x\n",
1645 (unsigned long)lba, blockoff,
James Smart9a6b09c2012-03-01 22:37:42 -05001646 be16_to_cpu(src->app_tag));
James Smartacd68592012-01-18 16:25:09 -05001647
1648 /*
James Smart9a6b09c2012-03-01 22:37:42 -05001649 * Save the old app_tag so we can
1650 * restore it on completion.
James Smartacd68592012-01-18 16:25:09 -05001651 */
James Smart9a6b09c2012-03-01 22:37:42 -05001652 if (lpfc_cmd) {
1653 lpfc_cmd->prot_data_type =
1654 LPFC_INJERR_APPTAG;
1655 lpfc_cmd->prot_data_segment =
1656 src;
1657 lpfc_cmd->prot_data =
1658 src->app_tag;
1659 }
1660 src->app_tag = cpu_to_be16(0xDEAD);
James Smartacd68592012-01-18 16:25:09 -05001661 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001662 if (phba->lpfc_injerr_wapp_cnt == 0) {
1663 phba->lpfc_injerr_nportid = 0;
1664 phba->lpfc_injerr_lba =
1665 LPFC_INJERR_LBA_OFF;
1666 memset(&phba->lpfc_injerr_wwpn,
1667 0, sizeof(struct lpfc_name));
1668 }
James Smart9a6b09c2012-03-01 22:37:42 -05001669 rc = BG_ERR_TGT | BG_ERR_CHECK;
James Smartacd68592012-01-18 16:25:09 -05001670 break;
1671 }
1672 /* Drop thru */
James Smart9a6b09c2012-03-01 22:37:42 -05001673 case SCSI_PROT_WRITE_INSERT:
1674 /*
1675 * For WRITE_INSERT, force the
1676 * error to be sent on the wire. It should be
1677 * detected by the Target.
1678 */
1679 /* DEAD will be the apptag on the wire */
1680 *apptag = 0xDEAD;
1681 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001682 if (phba->lpfc_injerr_wapp_cnt == 0) {
1683 phba->lpfc_injerr_nportid = 0;
1684 phba->lpfc_injerr_lba =
1685 LPFC_INJERR_LBA_OFF;
1686 memset(&phba->lpfc_injerr_wwpn,
1687 0, sizeof(struct lpfc_name));
1688 }
James Smart9a6b09c2012-03-01 22:37:42 -05001689 rc = BG_ERR_TGT | BG_ERR_CHECK;
1690
1691 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1692 "0813 BLKGRD: Injecting apptag error: "
1693 "write lba x%lx\n", (unsigned long)lba);
1694 break;
James Smartacd68592012-01-18 16:25:09 -05001695 case SCSI_PROT_WRITE_STRIP:
1696 /*
1697 * For WRITE_STRIP and WRITE_PASS,
1698 * force the error on data
1699 * being copied from SLI-Host to SLI-Port.
1700 */
1701 *apptag = 0xDEAD;
1702 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001703 if (phba->lpfc_injerr_wapp_cnt == 0) {
1704 phba->lpfc_injerr_nportid = 0;
1705 phba->lpfc_injerr_lba =
1706 LPFC_INJERR_LBA_OFF;
1707 memset(&phba->lpfc_injerr_wwpn,
1708 0, sizeof(struct lpfc_name));
1709 }
James Smartacd68592012-01-18 16:25:09 -05001710 rc = BG_ERR_INIT;
1711
1712 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1713 "0812 BLKGRD: Injecting apptag error: "
1714 "write lba x%lx\n", (unsigned long)lba);
1715 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001716 }
James Smartacd68592012-01-18 16:25:09 -05001717 }
1718 if (phba->lpfc_injerr_rapp_cnt) {
1719 switch (op) {
1720 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001721 case SCSI_PROT_READ_STRIP:
1722 case SCSI_PROT_READ_PASS:
1723 /*
1724 * For READ_STRIP and READ_PASS, force the
1725 * error on data being read off the wire. It
1726 * should force an IO error to the driver.
1727 */
James Smartf9bb2da2011-10-10 21:34:11 -04001728 *apptag = 0xDEAD;
1729 phba->lpfc_injerr_rapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001730 if (phba->lpfc_injerr_rapp_cnt == 0) {
1731 phba->lpfc_injerr_nportid = 0;
1732 phba->lpfc_injerr_lba =
1733 LPFC_INJERR_LBA_OFF;
1734 memset(&phba->lpfc_injerr_wwpn,
1735 0, sizeof(struct lpfc_name));
1736 }
James Smartacd68592012-01-18 16:25:09 -05001737 rc = BG_ERR_INIT;
James Smartf9bb2da2011-10-10 21:34:11 -04001738
1739 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smartacd68592012-01-18 16:25:09 -05001740 "0814 BLKGRD: Injecting apptag error: "
1741 "read lba x%lx\n", (unsigned long)lba);
1742 break;
1743 }
1744 }
1745 }
1746
1747
1748 /* Should we change the Guard Tag */
1749 if (new_guard) {
1750 if (phba->lpfc_injerr_wgrd_cnt) {
1751 switch (op) {
1752 case SCSI_PROT_WRITE_PASS:
James Smart9a6b09c2012-03-01 22:37:42 -05001753 rc = BG_ERR_CHECK;
James Smartacd68592012-01-18 16:25:09 -05001754 /* Drop thru */
James Smartacd68592012-01-18 16:25:09 -05001755
James Smartacd68592012-01-18 16:25:09 -05001756 case SCSI_PROT_WRITE_INSERT:
1757 /*
1758 * For WRITE_INSERT, force the
1759 * error to be sent on the wire. It should be
1760 * detected by the Target.
1761 */
1762 phba->lpfc_injerr_wgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001763 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1764 phba->lpfc_injerr_nportid = 0;
1765 phba->lpfc_injerr_lba =
1766 LPFC_INJERR_LBA_OFF;
1767 memset(&phba->lpfc_injerr_wwpn,
1768 0, sizeof(struct lpfc_name));
1769 }
James Smartacd68592012-01-18 16:25:09 -05001770
James Smart9a6b09c2012-03-01 22:37:42 -05001771 rc |= BG_ERR_TGT | BG_ERR_SWAP;
James Smartacd68592012-01-18 16:25:09 -05001772 /* Signals the caller to swap CRC->CSUM */
1773
1774 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1775 "0817 BLKGRD: Injecting guard error: "
1776 "write lba x%lx\n", (unsigned long)lba);
1777 break;
James Smart9a6b09c2012-03-01 22:37:42 -05001778 case SCSI_PROT_WRITE_STRIP:
1779 /*
1780 * For WRITE_STRIP and WRITE_PASS,
1781 * force the error on data
1782 * being copied from SLI-Host to SLI-Port.
1783 */
1784 phba->lpfc_injerr_wgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001785 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1786 phba->lpfc_injerr_nportid = 0;
1787 phba->lpfc_injerr_lba =
1788 LPFC_INJERR_LBA_OFF;
1789 memset(&phba->lpfc_injerr_wwpn,
1790 0, sizeof(struct lpfc_name));
1791 }
James Smart9a6b09c2012-03-01 22:37:42 -05001792
1793 rc = BG_ERR_INIT | BG_ERR_SWAP;
1794 /* Signals the caller to swap CRC->CSUM */
1795
1796 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1797 "0816 BLKGRD: Injecting guard error: "
1798 "write lba x%lx\n", (unsigned long)lba);
1799 break;
James Smartacd68592012-01-18 16:25:09 -05001800 }
1801 }
1802 if (phba->lpfc_injerr_rgrd_cnt) {
1803 switch (op) {
1804 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001805 case SCSI_PROT_READ_STRIP:
1806 case SCSI_PROT_READ_PASS:
1807 /*
1808 * For READ_STRIP and READ_PASS, force the
1809 * error on data being read off the wire. It
1810 * should force an IO error to the driver.
1811 */
James Smartacd68592012-01-18 16:25:09 -05001812 phba->lpfc_injerr_rgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001813 if (phba->lpfc_injerr_rgrd_cnt == 0) {
1814 phba->lpfc_injerr_nportid = 0;
1815 phba->lpfc_injerr_lba =
1816 LPFC_INJERR_LBA_OFF;
1817 memset(&phba->lpfc_injerr_wwpn,
1818 0, sizeof(struct lpfc_name));
1819 }
James Smartacd68592012-01-18 16:25:09 -05001820
James Smart9a6b09c2012-03-01 22:37:42 -05001821 rc = BG_ERR_INIT | BG_ERR_SWAP;
James Smartacd68592012-01-18 16:25:09 -05001822 /* Signals the caller to swap CRC->CSUM */
1823
1824 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1825 "0818 BLKGRD: Injecting guard error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001826 "read lba x%lx\n", (unsigned long)lba);
1827 }
1828 }
1829 }
1830
James Smartf9bb2da2011-10-10 21:34:11 -04001831 return rc;
1832}
1833#endif
1834
James Smartacd68592012-01-18 16:25:09 -05001835/**
1836 * lpfc_sc_to_bg_opcodes - Determine the BlockGuard opcodes to be used with
1837 * the specified SCSI command.
1838 * @phba: The Hba for which this call is being executed.
James Smart6c8eea52010-04-06 14:49:53 -04001839 * @sc: The SCSI command to examine
1840 * @txopt: (out) BlockGuard operation for transmitted data
1841 * @rxopt: (out) BlockGuard operation for received data
1842 *
1843 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1844 *
James Smartacd68592012-01-18 16:25:09 -05001845 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001846static int
James Smart6c8eea52010-04-06 14:49:53 -04001847lpfc_sc_to_bg_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1848 uint8_t *txop, uint8_t *rxop)
James Smarte2a0a9d2008-12-04 22:40:02 -05001849{
1850 uint8_t guard_type = scsi_host_get_guard(sc->device->host);
James Smart6c8eea52010-04-06 14:49:53 -04001851 uint8_t ret = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05001852
1853 if (guard_type == SHOST_DIX_GUARD_IP) {
1854 switch (scsi_get_prot_op(sc)) {
1855 case SCSI_PROT_READ_INSERT:
1856 case SCSI_PROT_WRITE_STRIP:
James Smart6c8eea52010-04-06 14:49:53 -04001857 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001858 *txop = BG_OP_IN_CSUM_OUT_NODIF;
James Smarte2a0a9d2008-12-04 22:40:02 -05001859 break;
1860
1861 case SCSI_PROT_READ_STRIP:
1862 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001863 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001864 *txop = BG_OP_IN_NODIF_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001865 break;
1866
Martin K. Petersenc6af4042009-09-18 17:32:59 -04001867 case SCSI_PROT_READ_PASS:
1868 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001869 *rxop = BG_OP_IN_CRC_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001870 *txop = BG_OP_IN_CSUM_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001871 break;
1872
James Smarte2a0a9d2008-12-04 22:40:02 -05001873 case SCSI_PROT_NORMAL:
1874 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001875 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001876 "9063 BLKGRD: Bad op/guard:%d/IP combination\n",
1877 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001878 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001879 break;
1880
1881 }
James Smart7c56b9f2011-07-22 18:36:25 -04001882 } else {
James Smarte2a0a9d2008-12-04 22:40:02 -05001883 switch (scsi_get_prot_op(sc)) {
1884 case SCSI_PROT_READ_STRIP:
1885 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001886 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001887 *txop = BG_OP_IN_NODIF_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001888 break;
1889
1890 case SCSI_PROT_READ_PASS:
1891 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001892 *rxop = BG_OP_IN_CRC_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001893 *txop = BG_OP_IN_CRC_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001894 break;
1895
James Smarte2a0a9d2008-12-04 22:40:02 -05001896 case SCSI_PROT_READ_INSERT:
1897 case SCSI_PROT_WRITE_STRIP:
James Smart7c56b9f2011-07-22 18:36:25 -04001898 *rxop = BG_OP_IN_NODIF_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001899 *txop = BG_OP_IN_CRC_OUT_NODIF;
James Smart7c56b9f2011-07-22 18:36:25 -04001900 break;
1901
James Smarte2a0a9d2008-12-04 22:40:02 -05001902 case SCSI_PROT_NORMAL:
1903 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001904 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001905 "9075 BLKGRD: Bad op/guard:%d/CRC combination\n",
1906 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001907 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001908 break;
1909 }
James Smarte2a0a9d2008-12-04 22:40:02 -05001910 }
1911
James Smart6c8eea52010-04-06 14:49:53 -04001912 return ret;
James Smarte2a0a9d2008-12-04 22:40:02 -05001913}
1914
James Smartacd68592012-01-18 16:25:09 -05001915#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1916/**
1917 * lpfc_bg_err_opcodes - reDetermine the BlockGuard opcodes to be used with
1918 * the specified SCSI command in order to force a guard tag error.
1919 * @phba: The Hba for which this call is being executed.
1920 * @sc: The SCSI command to examine
1921 * @txopt: (out) BlockGuard operation for transmitted data
1922 * @rxopt: (out) BlockGuard operation for received data
1923 *
1924 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1925 *
1926 **/
1927static int
1928lpfc_bg_err_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1929 uint8_t *txop, uint8_t *rxop)
1930{
1931 uint8_t guard_type = scsi_host_get_guard(sc->device->host);
1932 uint8_t ret = 0;
1933
1934 if (guard_type == SHOST_DIX_GUARD_IP) {
1935 switch (scsi_get_prot_op(sc)) {
1936 case SCSI_PROT_READ_INSERT:
1937 case SCSI_PROT_WRITE_STRIP:
James Smartacd68592012-01-18 16:25:09 -05001938 *rxop = BG_OP_IN_NODIF_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001939 *txop = BG_OP_IN_CRC_OUT_NODIF;
James Smartacd68592012-01-18 16:25:09 -05001940 break;
1941
1942 case SCSI_PROT_READ_STRIP:
1943 case SCSI_PROT_WRITE_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001944 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001945 *txop = BG_OP_IN_NODIF_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001946 break;
1947
1948 case SCSI_PROT_READ_PASS:
1949 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001950 *rxop = BG_OP_IN_CSUM_OUT_CRC;
James Smart9a6b09c2012-03-01 22:37:42 -05001951 *txop = BG_OP_IN_CRC_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001952 break;
1953
1954 case SCSI_PROT_NORMAL:
1955 default:
1956 break;
1957
1958 }
1959 } else {
1960 switch (scsi_get_prot_op(sc)) {
1961 case SCSI_PROT_READ_STRIP:
1962 case SCSI_PROT_WRITE_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001963 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001964 *txop = BG_OP_IN_NODIF_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001965 break;
1966
1967 case SCSI_PROT_READ_PASS:
1968 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001969 *rxop = BG_OP_IN_CSUM_OUT_CSUM;
James Smart9a6b09c2012-03-01 22:37:42 -05001970 *txop = BG_OP_IN_CSUM_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001971 break;
1972
1973 case SCSI_PROT_READ_INSERT:
1974 case SCSI_PROT_WRITE_STRIP:
James Smartacd68592012-01-18 16:25:09 -05001975 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001976 *txop = BG_OP_IN_CSUM_OUT_NODIF;
James Smartacd68592012-01-18 16:25:09 -05001977 break;
1978
1979 case SCSI_PROT_NORMAL:
1980 default:
1981 break;
1982 }
1983 }
1984
1985 return ret;
1986}
1987#endif
1988
1989/**
1990 * lpfc_bg_setup_bpl - Setup BlockGuard BPL with no protection data
1991 * @phba: The Hba for which this call is being executed.
1992 * @sc: pointer to scsi command we're working on
1993 * @bpl: pointer to buffer list for protection groups
1994 * @datacnt: number of segments of data that have been dma mapped
1995 *
1996 * This function sets up BPL buffer list for protection groups of
James Smarte2a0a9d2008-12-04 22:40:02 -05001997 * type LPFC_PG_TYPE_NO_DIF
1998 *
1999 * This is usually used when the HBA is instructed to generate
2000 * DIFs and insert them into data stream (or strip DIF from
2001 * incoming data stream)
2002 *
2003 * The buffer list consists of just one protection group described
2004 * below:
2005 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002006 * start of prot group --> | PDE_5 |
2007 * +-------------------------+
2008 * | PDE_6 |
James Smarte2a0a9d2008-12-04 22:40:02 -05002009 * +-------------------------+
2010 * | Data BDE |
2011 * +-------------------------+
2012 * |more Data BDE's ... (opt)|
2013 * +-------------------------+
2014 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002015 *
2016 * Note: Data s/g buffers have been dma mapped
James Smartacd68592012-01-18 16:25:09 -05002017 *
2018 * Returns the number of BDEs added to the BPL.
2019 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002020static int
2021lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2022 struct ulp_bde64 *bpl, int datasegcnt)
2023{
2024 struct scatterlist *sgde = NULL; /* s/g data entry */
James Smart6c8eea52010-04-06 14:49:53 -04002025 struct lpfc_pde5 *pde5 = NULL;
2026 struct lpfc_pde6 *pde6 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05002027 dma_addr_t physaddr;
James Smart6c8eea52010-04-06 14:49:53 -04002028 int i = 0, num_bde = 0, status;
James Smarte2a0a9d2008-12-04 22:40:02 -05002029 int datadir = sc->sc_data_direction;
James Smart0829a192012-05-09 21:18:12 -04002030#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002031 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002032#endif
James Smartacd68592012-01-18 16:25:09 -05002033 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05002034 uint32_t reftag;
James Smart7c56b9f2011-07-22 18:36:25 -04002035 unsigned blksize;
James Smart6c8eea52010-04-06 14:49:53 -04002036 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05002037
James Smart6c8eea52010-04-06 14:49:53 -04002038 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2039 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05002040 goto out;
2041
James Smart6c8eea52010-04-06 14:49:53 -04002042 /* extract some info from the scsi command for pde*/
James Smarte2a0a9d2008-12-04 22:40:02 -05002043 blksize = lpfc_cmd_blksize(sc);
James Smartacd68592012-01-18 16:25:09 -05002044 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
James Smarte2a0a9d2008-12-04 22:40:02 -05002045
James Smartf9bb2da2011-10-10 21:34:11 -04002046#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002047 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002048 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002049 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002050 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002051 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002052 checking = 0;
2053 }
James Smartf9bb2da2011-10-10 21:34:11 -04002054#endif
2055
James Smart6c8eea52010-04-06 14:49:53 -04002056 /* setup PDE5 with what we have */
2057 pde5 = (struct lpfc_pde5 *) bpl;
2058 memset(pde5, 0, sizeof(struct lpfc_pde5));
2059 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05002060
James Smartbc739052010-08-04 16:11:18 -04002061 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04002062 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04002063 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04002064
James Smart6c8eea52010-04-06 14:49:53 -04002065 /* advance bpl and increment bde count */
2066 num_bde++;
2067 bpl++;
2068 pde6 = (struct lpfc_pde6 *) bpl;
2069
2070 /* setup PDE6 with the rest of the info */
2071 memset(pde6, 0, sizeof(struct lpfc_pde6));
2072 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
2073 bf_set(pde6_optx, pde6, txop);
2074 bf_set(pde6_oprx, pde6, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002075
2076 /*
2077 * We only need to check the data on READs, for WRITEs
2078 * protection data is automatically generated, not checked.
2079 */
James Smart6c8eea52010-04-06 14:49:53 -04002080 if (datadir == DMA_FROM_DEVICE) {
James Smarta6887e22013-04-17 20:18:07 -04002081 if (scsi_prot_flagged(sc, SCSI_PROT_GUARD_CHECK))
2082 bf_set(pde6_ce, pde6, checking);
2083 else
2084 bf_set(pde6_ce, pde6, 0);
2085
2086 if (scsi_prot_flagged(sc, SCSI_PROT_REF_CHECK))
2087 bf_set(pde6_re, pde6, checking);
2088 else
2089 bf_set(pde6_re, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04002090 }
2091 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04002092 bf_set(pde6_ae, pde6, 0);
2093 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04002094
James Smartbc739052010-08-04 16:11:18 -04002095 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04002096 pde6->word0 = cpu_to_le32(pde6->word0);
2097 pde6->word1 = cpu_to_le32(pde6->word1);
2098 pde6->word2 = cpu_to_le32(pde6->word2);
2099
James Smart6c8eea52010-04-06 14:49:53 -04002100 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05002101 num_bde++;
2102 bpl++;
2103
2104 /* assumption: caller has already run dma_map_sg on command data */
2105 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
2106 physaddr = sg_dma_address(sgde);
2107 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
2108 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
2109 bpl->tus.f.bdeSize = sg_dma_len(sgde);
2110 if (datadir == DMA_TO_DEVICE)
2111 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2112 else
2113 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2114 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2115 bpl++;
2116 num_bde++;
2117 }
2118
2119out:
2120 return num_bde;
2121}
2122
James Smartacd68592012-01-18 16:25:09 -05002123/**
2124 * lpfc_bg_setup_bpl_prot - Setup BlockGuard BPL with protection data
2125 * @phba: The Hba for which this call is being executed.
2126 * @sc: pointer to scsi command we're working on
2127 * @bpl: pointer to buffer list for protection groups
2128 * @datacnt: number of segments of data that have been dma mapped
2129 * @protcnt: number of segment of protection data that have been dma mapped
2130 *
2131 * This function sets up BPL buffer list for protection groups of
2132 * type LPFC_PG_TYPE_DIF
James Smarte2a0a9d2008-12-04 22:40:02 -05002133 *
2134 * This is usually used when DIFs are in their own buffers,
2135 * separate from the data. The HBA can then by instructed
2136 * to place the DIFs in the outgoing stream. For read operations,
2137 * The HBA could extract the DIFs and place it in DIF buffers.
2138 *
2139 * The buffer list for this type consists of one or more of the
2140 * protection groups described below:
2141 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002142 * start of first prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05002143 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002144 * | PDE_6 |
2145 * +-------------------------+
2146 * | PDE_7 (Prot BDE) |
James Smarte2a0a9d2008-12-04 22:40:02 -05002147 * +-------------------------+
2148 * | Data BDE |
2149 * +-------------------------+
2150 * |more Data BDE's ... (opt)|
2151 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002152 * start of new prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05002153 * +-------------------------+
2154 * | ... |
2155 * +-------------------------+
2156 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002157 * Note: It is assumed that both data and protection s/g buffers have been
2158 * mapped for DMA
James Smartacd68592012-01-18 16:25:09 -05002159 *
2160 * Returns the number of BDEs added to the BPL.
2161 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002162static int
2163lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2164 struct ulp_bde64 *bpl, int datacnt, int protcnt)
2165{
2166 struct scatterlist *sgde = NULL; /* s/g data entry */
2167 struct scatterlist *sgpe = NULL; /* s/g prot entry */
James Smart6c8eea52010-04-06 14:49:53 -04002168 struct lpfc_pde5 *pde5 = NULL;
2169 struct lpfc_pde6 *pde6 = NULL;
James Smart7f860592011-03-11 16:05:52 -05002170 struct lpfc_pde7 *pde7 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05002171 dma_addr_t dataphysaddr, protphysaddr;
2172 unsigned short curr_data = 0, curr_prot = 0;
James Smart7f860592011-03-11 16:05:52 -05002173 unsigned int split_offset;
2174 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
James Smarte2a0a9d2008-12-04 22:40:02 -05002175 unsigned int protgrp_blks, protgrp_bytes;
2176 unsigned int remainder, subtotal;
James Smart6c8eea52010-04-06 14:49:53 -04002177 int status;
James Smarte2a0a9d2008-12-04 22:40:02 -05002178 int datadir = sc->sc_data_direction;
2179 unsigned char pgdone = 0, alldone = 0;
2180 unsigned blksize;
James Smart0829a192012-05-09 21:18:12 -04002181#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002182 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002183#endif
James Smartacd68592012-01-18 16:25:09 -05002184 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05002185 uint32_t reftag;
James Smart6c8eea52010-04-06 14:49:53 -04002186 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05002187 int num_bde = 0;
2188
2189 sgpe = scsi_prot_sglist(sc);
2190 sgde = scsi_sglist(sc);
2191
2192 if (!sgpe || !sgde) {
2193 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2194 "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
2195 sgpe, sgde);
2196 return 0;
2197 }
2198
James Smart6c8eea52010-04-06 14:49:53 -04002199 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2200 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05002201 goto out;
2202
James Smart6c8eea52010-04-06 14:49:53 -04002203 /* extract some info from the scsi command */
James Smarte2a0a9d2008-12-04 22:40:02 -05002204 blksize = lpfc_cmd_blksize(sc);
James Smartacd68592012-01-18 16:25:09 -05002205 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
James Smarte2a0a9d2008-12-04 22:40:02 -05002206
James Smartf9bb2da2011-10-10 21:34:11 -04002207#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002208 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002209 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002210 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002211 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002212 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002213 checking = 0;
2214 }
James Smartf9bb2da2011-10-10 21:34:11 -04002215#endif
2216
James Smarte2a0a9d2008-12-04 22:40:02 -05002217 split_offset = 0;
2218 do {
James Smart96f70772013-04-17 20:16:15 -04002219 /* Check to see if we ran out of space */
2220 if (num_bde >= (phba->cfg_total_seg_cnt - 2))
2221 return num_bde + 3;
2222
James Smart6c8eea52010-04-06 14:49:53 -04002223 /* setup PDE5 with what we have */
2224 pde5 = (struct lpfc_pde5 *) bpl;
2225 memset(pde5, 0, sizeof(struct lpfc_pde5));
2226 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05002227
James Smartbc739052010-08-04 16:11:18 -04002228 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04002229 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04002230 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04002231
James Smart6c8eea52010-04-06 14:49:53 -04002232 /* advance bpl and increment bde count */
2233 num_bde++;
2234 bpl++;
2235 pde6 = (struct lpfc_pde6 *) bpl;
James Smarte2a0a9d2008-12-04 22:40:02 -05002236
James Smart6c8eea52010-04-06 14:49:53 -04002237 /* setup PDE6 with the rest of the info */
2238 memset(pde6, 0, sizeof(struct lpfc_pde6));
2239 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
2240 bf_set(pde6_optx, pde6, txop);
2241 bf_set(pde6_oprx, pde6, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002242
2243 if (scsi_prot_flagged(sc, SCSI_PROT_GUARD_CHECK))
2244 bf_set(pde6_ce, pde6, checking);
2245 else
2246 bf_set(pde6_ce, pde6, 0);
2247
2248 if (scsi_prot_flagged(sc, SCSI_PROT_REF_CHECK))
2249 bf_set(pde6_re, pde6, checking);
2250 else
2251 bf_set(pde6_re, pde6, 0);
2252
James Smart6c8eea52010-04-06 14:49:53 -04002253 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04002254 bf_set(pde6_ae, pde6, 0);
2255 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04002256
James Smartbc739052010-08-04 16:11:18 -04002257 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04002258 pde6->word0 = cpu_to_le32(pde6->word0);
2259 pde6->word1 = cpu_to_le32(pde6->word1);
2260 pde6->word2 = cpu_to_le32(pde6->word2);
2261
James Smart6c8eea52010-04-06 14:49:53 -04002262 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05002263 num_bde++;
2264 bpl++;
2265
2266 /* setup the first BDE that points to protection buffer */
James Smart7f860592011-03-11 16:05:52 -05002267 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2268 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
James Smarte2a0a9d2008-12-04 22:40:02 -05002269
James Smarte2a0a9d2008-12-04 22:40:02 -05002270 /* must be integer multiple of the DIF block length */
2271 BUG_ON(protgroup_len % 8);
2272
James Smart7f860592011-03-11 16:05:52 -05002273 pde7 = (struct lpfc_pde7 *) bpl;
2274 memset(pde7, 0, sizeof(struct lpfc_pde7));
2275 bf_set(pde7_type, pde7, LPFC_PDE7_DESCRIPTOR);
2276
James Smart7c56b9f2011-07-22 18:36:25 -04002277 pde7->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
2278 pde7->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
James Smart7f860592011-03-11 16:05:52 -05002279
James Smarte2a0a9d2008-12-04 22:40:02 -05002280 protgrp_blks = protgroup_len / 8;
2281 protgrp_bytes = protgrp_blks * blksize;
2282
James Smart7f860592011-03-11 16:05:52 -05002283 /* check if this pde is crossing the 4K boundary; if so split */
2284 if ((pde7->addrLow & 0xfff) + protgroup_len > 0x1000) {
2285 protgroup_remainder = 0x1000 - (pde7->addrLow & 0xfff);
2286 protgroup_offset += protgroup_remainder;
2287 protgrp_blks = protgroup_remainder / 8;
James Smart7c56b9f2011-07-22 18:36:25 -04002288 protgrp_bytes = protgrp_blks * blksize;
James Smart7f860592011-03-11 16:05:52 -05002289 } else {
2290 protgroup_offset = 0;
2291 curr_prot++;
2292 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002293
James Smarte2a0a9d2008-12-04 22:40:02 -05002294 num_bde++;
2295
2296 /* setup BDE's for data blocks associated with DIF data */
2297 pgdone = 0;
2298 subtotal = 0; /* total bytes processed for current prot grp */
2299 while (!pgdone) {
James Smart96f70772013-04-17 20:16:15 -04002300 /* Check to see if we ran out of space */
2301 if (num_bde >= phba->cfg_total_seg_cnt)
2302 return num_bde + 1;
2303
James Smarte2a0a9d2008-12-04 22:40:02 -05002304 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -04002305 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2306 "9065 BLKGRD:%s Invalid data segment\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002307 __func__);
2308 return 0;
2309 }
2310 bpl++;
2311 dataphysaddr = sg_dma_address(sgde) + split_offset;
2312 bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
2313 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
2314
2315 remainder = sg_dma_len(sgde) - split_offset;
2316
2317 if ((subtotal + remainder) <= protgrp_bytes) {
2318 /* we can use this whole buffer */
2319 bpl->tus.f.bdeSize = remainder;
2320 split_offset = 0;
2321
2322 if ((subtotal + remainder) == protgrp_bytes)
2323 pgdone = 1;
2324 } else {
2325 /* must split this buffer with next prot grp */
2326 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
2327 split_offset += bpl->tus.f.bdeSize;
2328 }
2329
2330 subtotal += bpl->tus.f.bdeSize;
2331
2332 if (datadir == DMA_TO_DEVICE)
2333 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2334 else
2335 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2336 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2337
2338 num_bde++;
2339 curr_data++;
2340
2341 if (split_offset)
2342 break;
2343
2344 /* Move to the next s/g segment if possible */
2345 sgde = sg_next(sgde);
James Smart6c8eea52010-04-06 14:49:53 -04002346
James Smarte2a0a9d2008-12-04 22:40:02 -05002347 }
2348
James Smart7f860592011-03-11 16:05:52 -05002349 if (protgroup_offset) {
2350 /* update the reference tag */
2351 reftag += protgrp_blks;
2352 bpl++;
2353 continue;
2354 }
2355
James Smarte2a0a9d2008-12-04 22:40:02 -05002356 /* are we done ? */
2357 if (curr_prot == protcnt) {
2358 alldone = 1;
2359 } else if (curr_prot < protcnt) {
2360 /* advance to next prot buffer */
2361 sgpe = sg_next(sgpe);
2362 bpl++;
2363
2364 /* update the reference tag */
2365 reftag += protgrp_blks;
2366 } else {
2367 /* if we're here, we have a bug */
James Smart6a9c52c2009-10-02 15:16:51 -04002368 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2369 "9054 BLKGRD: bug in %s\n", __func__);
James Smarte2a0a9d2008-12-04 22:40:02 -05002370 }
2371
2372 } while (!alldone);
James Smarte2a0a9d2008-12-04 22:40:02 -05002373out:
2374
James Smarte2a0a9d2008-12-04 22:40:02 -05002375 return num_bde;
2376}
James Smart7f860592011-03-11 16:05:52 -05002377
James Smartacd68592012-01-18 16:25:09 -05002378/**
2379 * lpfc_bg_setup_sgl - Setup BlockGuard SGL with no protection data
2380 * @phba: The Hba for which this call is being executed.
2381 * @sc: pointer to scsi command we're working on
2382 * @sgl: pointer to buffer list for protection groups
2383 * @datacnt: number of segments of data that have been dma mapped
2384 *
2385 * This function sets up SGL buffer list for protection groups of
2386 * type LPFC_PG_TYPE_NO_DIF
2387 *
2388 * This is usually used when the HBA is instructed to generate
2389 * DIFs and insert them into data stream (or strip DIF from
2390 * incoming data stream)
2391 *
2392 * The buffer list consists of just one protection group described
2393 * below:
2394 * +-------------------------+
2395 * start of prot group --> | DI_SEED |
2396 * +-------------------------+
2397 * | Data SGE |
2398 * +-------------------------+
2399 * |more Data SGE's ... (opt)|
2400 * +-------------------------+
2401 *
2402 *
2403 * Note: Data s/g buffers have been dma mapped
2404 *
2405 * Returns the number of SGEs added to the SGL.
2406 **/
2407static int
2408lpfc_bg_setup_sgl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2409 struct sli4_sge *sgl, int datasegcnt)
2410{
2411 struct scatterlist *sgde = NULL; /* s/g data entry */
2412 struct sli4_sge_diseed *diseed = NULL;
2413 dma_addr_t physaddr;
2414 int i = 0, num_sge = 0, status;
James Smartacd68592012-01-18 16:25:09 -05002415 uint32_t reftag;
2416 unsigned blksize;
2417 uint8_t txop, rxop;
James Smart0829a192012-05-09 21:18:12 -04002418#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002419 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002420#endif
James Smartacd68592012-01-18 16:25:09 -05002421 uint32_t checking = 1;
2422 uint32_t dma_len;
2423 uint32_t dma_offset = 0;
2424
2425 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2426 if (status)
2427 goto out;
2428
2429 /* extract some info from the scsi command for pde*/
2430 blksize = lpfc_cmd_blksize(sc);
2431 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2432
2433#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002434 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002435 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002436 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002437 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002438 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002439 checking = 0;
2440 }
2441#endif
2442
2443 /* setup DISEED with what we have */
2444 diseed = (struct sli4_sge_diseed *) sgl;
2445 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2446 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2447
2448 /* Endianness conversion if necessary */
2449 diseed->ref_tag = cpu_to_le32(reftag);
2450 diseed->ref_tag_tran = diseed->ref_tag;
2451
James Smarta6887e22013-04-17 20:18:07 -04002452 /*
2453 * We only need to check the data on READs, for WRITEs
2454 * protection data is automatically generated, not checked.
2455 */
2456 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
2457 if (scsi_prot_flagged(sc, SCSI_PROT_GUARD_CHECK))
2458 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2459 else
2460 bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2461
2462 if (scsi_prot_flagged(sc, SCSI_PROT_REF_CHECK))
2463 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2464 else
2465 bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2466 }
2467
James Smartacd68592012-01-18 16:25:09 -05002468 /* setup DISEED with the rest of the info */
2469 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2470 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002471
James Smartacd68592012-01-18 16:25:09 -05002472 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2473 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2474
2475 /* Endianness conversion if necessary for DISEED */
2476 diseed->word2 = cpu_to_le32(diseed->word2);
2477 diseed->word3 = cpu_to_le32(diseed->word3);
2478
2479 /* advance bpl and increment sge count */
2480 num_sge++;
2481 sgl++;
2482
2483 /* assumption: caller has already run dma_map_sg on command data */
2484 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
2485 physaddr = sg_dma_address(sgde);
2486 dma_len = sg_dma_len(sgde);
2487 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
2488 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
2489 if ((i + 1) == datasegcnt)
2490 bf_set(lpfc_sli4_sge_last, sgl, 1);
2491 else
2492 bf_set(lpfc_sli4_sge_last, sgl, 0);
2493 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2494 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2495
2496 sgl->sge_len = cpu_to_le32(dma_len);
2497 dma_offset += dma_len;
2498
2499 sgl++;
2500 num_sge++;
2501 }
2502
2503out:
2504 return num_sge;
2505}
2506
2507/**
2508 * lpfc_bg_setup_sgl_prot - Setup BlockGuard SGL with protection data
2509 * @phba: The Hba for which this call is being executed.
2510 * @sc: pointer to scsi command we're working on
2511 * @sgl: pointer to buffer list for protection groups
2512 * @datacnt: number of segments of data that have been dma mapped
2513 * @protcnt: number of segment of protection data that have been dma mapped
2514 *
2515 * This function sets up SGL buffer list for protection groups of
2516 * type LPFC_PG_TYPE_DIF
2517 *
2518 * This is usually used when DIFs are in their own buffers,
2519 * separate from the data. The HBA can then by instructed
2520 * to place the DIFs in the outgoing stream. For read operations,
2521 * The HBA could extract the DIFs and place it in DIF buffers.
2522 *
2523 * The buffer list for this type consists of one or more of the
2524 * protection groups described below:
2525 * +-------------------------+
2526 * start of first prot group --> | DISEED |
2527 * +-------------------------+
2528 * | DIF (Prot SGE) |
2529 * +-------------------------+
2530 * | Data SGE |
2531 * +-------------------------+
2532 * |more Data SGE's ... (opt)|
2533 * +-------------------------+
2534 * start of new prot group --> | DISEED |
2535 * +-------------------------+
2536 * | ... |
2537 * +-------------------------+
2538 *
2539 * Note: It is assumed that both data and protection s/g buffers have been
2540 * mapped for DMA
2541 *
2542 * Returns the number of SGEs added to the SGL.
2543 **/
2544static int
2545lpfc_bg_setup_sgl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2546 struct sli4_sge *sgl, int datacnt, int protcnt)
2547{
2548 struct scatterlist *sgde = NULL; /* s/g data entry */
2549 struct scatterlist *sgpe = NULL; /* s/g prot entry */
2550 struct sli4_sge_diseed *diseed = NULL;
2551 dma_addr_t dataphysaddr, protphysaddr;
2552 unsigned short curr_data = 0, curr_prot = 0;
2553 unsigned int split_offset;
2554 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
2555 unsigned int protgrp_blks, protgrp_bytes;
2556 unsigned int remainder, subtotal;
2557 int status;
2558 unsigned char pgdone = 0, alldone = 0;
2559 unsigned blksize;
2560 uint32_t reftag;
2561 uint8_t txop, rxop;
2562 uint32_t dma_len;
James Smart0829a192012-05-09 21:18:12 -04002563#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002564 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002565#endif
James Smartacd68592012-01-18 16:25:09 -05002566 uint32_t checking = 1;
2567 uint32_t dma_offset = 0;
2568 int num_sge = 0;
2569
2570 sgpe = scsi_prot_sglist(sc);
2571 sgde = scsi_sglist(sc);
2572
2573 if (!sgpe || !sgde) {
2574 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2575 "9082 Invalid s/g entry: data=0x%p prot=0x%p\n",
2576 sgpe, sgde);
2577 return 0;
2578 }
2579
2580 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2581 if (status)
2582 goto out;
2583
2584 /* extract some info from the scsi command */
2585 blksize = lpfc_cmd_blksize(sc);
2586 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2587
2588#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002589 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002590 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002591 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002592 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002593 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002594 checking = 0;
2595 }
2596#endif
2597
2598 split_offset = 0;
2599 do {
James Smart96f70772013-04-17 20:16:15 -04002600 /* Check to see if we ran out of space */
2601 if (num_sge >= (phba->cfg_total_seg_cnt - 2))
2602 return num_sge + 3;
2603
James Smartacd68592012-01-18 16:25:09 -05002604 /* setup DISEED with what we have */
2605 diseed = (struct sli4_sge_diseed *) sgl;
2606 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2607 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2608
2609 /* Endianness conversion if necessary */
2610 diseed->ref_tag = cpu_to_le32(reftag);
2611 diseed->ref_tag_tran = diseed->ref_tag;
2612
James Smarta6887e22013-04-17 20:18:07 -04002613 if (scsi_prot_flagged(sc, SCSI_PROT_GUARD_CHECK)) {
2614 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2615
2616 } else {
2617 bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2618 /*
2619 * When in this mode, the hardware will replace
2620 * the guard tag from the host with a
2621 * newly generated good CRC for the wire.
2622 * Switch to raw mode here to avoid this
2623 * behavior. What the host sends gets put on the wire.
2624 */
2625 if (txop == BG_OP_IN_CRC_OUT_CRC) {
2626 txop = BG_OP_RAW_MODE;
2627 rxop = BG_OP_RAW_MODE;
2628 }
2629 }
2630
2631
2632 if (scsi_prot_flagged(sc, SCSI_PROT_REF_CHECK))
2633 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2634 else
2635 bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2636
James Smartacd68592012-01-18 16:25:09 -05002637 /* setup DISEED with the rest of the info */
2638 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2639 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002640
James Smartacd68592012-01-18 16:25:09 -05002641 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2642 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2643
2644 /* Endianness conversion if necessary for DISEED */
2645 diseed->word2 = cpu_to_le32(diseed->word2);
2646 diseed->word3 = cpu_to_le32(diseed->word3);
2647
2648 /* advance sgl and increment bde count */
2649 num_sge++;
2650 sgl++;
2651
2652 /* setup the first BDE that points to protection buffer */
2653 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2654 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
2655
2656 /* must be integer multiple of the DIF block length */
2657 BUG_ON(protgroup_len % 8);
2658
2659 /* Now setup DIF SGE */
2660 sgl->word2 = 0;
2661 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DIF);
2662 sgl->addr_hi = le32_to_cpu(putPaddrHigh(protphysaddr));
2663 sgl->addr_lo = le32_to_cpu(putPaddrLow(protphysaddr));
2664 sgl->word2 = cpu_to_le32(sgl->word2);
2665
2666 protgrp_blks = protgroup_len / 8;
2667 protgrp_bytes = protgrp_blks * blksize;
2668
2669 /* check if DIF SGE is crossing the 4K boundary; if so split */
2670 if ((sgl->addr_lo & 0xfff) + protgroup_len > 0x1000) {
2671 protgroup_remainder = 0x1000 - (sgl->addr_lo & 0xfff);
2672 protgroup_offset += protgroup_remainder;
2673 protgrp_blks = protgroup_remainder / 8;
2674 protgrp_bytes = protgrp_blks * blksize;
2675 } else {
2676 protgroup_offset = 0;
2677 curr_prot++;
2678 }
2679
2680 num_sge++;
2681
2682 /* setup SGE's for data blocks associated with DIF data */
2683 pgdone = 0;
2684 subtotal = 0; /* total bytes processed for current prot grp */
2685 while (!pgdone) {
James Smart96f70772013-04-17 20:16:15 -04002686 /* Check to see if we ran out of space */
2687 if (num_sge >= phba->cfg_total_seg_cnt)
2688 return num_sge + 1;
2689
James Smartacd68592012-01-18 16:25:09 -05002690 if (!sgde) {
2691 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2692 "9086 BLKGRD:%s Invalid data segment\n",
2693 __func__);
2694 return 0;
2695 }
2696 sgl++;
2697 dataphysaddr = sg_dma_address(sgde) + split_offset;
2698
2699 remainder = sg_dma_len(sgde) - split_offset;
2700
2701 if ((subtotal + remainder) <= protgrp_bytes) {
2702 /* we can use this whole buffer */
2703 dma_len = remainder;
2704 split_offset = 0;
2705
2706 if ((subtotal + remainder) == protgrp_bytes)
2707 pgdone = 1;
2708 } else {
2709 /* must split this buffer with next prot grp */
2710 dma_len = protgrp_bytes - subtotal;
2711 split_offset += dma_len;
2712 }
2713
2714 subtotal += dma_len;
2715
2716 sgl->addr_lo = cpu_to_le32(putPaddrLow(dataphysaddr));
2717 sgl->addr_hi = cpu_to_le32(putPaddrHigh(dataphysaddr));
2718 bf_set(lpfc_sli4_sge_last, sgl, 0);
2719 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2720 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2721
2722 sgl->sge_len = cpu_to_le32(dma_len);
2723 dma_offset += dma_len;
2724
2725 num_sge++;
2726 curr_data++;
2727
2728 if (split_offset)
2729 break;
2730
2731 /* Move to the next s/g segment if possible */
2732 sgde = sg_next(sgde);
2733 }
2734
2735 if (protgroup_offset) {
2736 /* update the reference tag */
2737 reftag += protgrp_blks;
2738 sgl++;
2739 continue;
2740 }
2741
2742 /* are we done ? */
2743 if (curr_prot == protcnt) {
2744 bf_set(lpfc_sli4_sge_last, sgl, 1);
2745 alldone = 1;
2746 } else if (curr_prot < protcnt) {
2747 /* advance to next prot buffer */
2748 sgpe = sg_next(sgpe);
2749 sgl++;
2750
2751 /* update the reference tag */
2752 reftag += protgrp_blks;
2753 } else {
2754 /* if we're here, we have a bug */
2755 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2756 "9085 BLKGRD: bug in %s\n", __func__);
2757 }
2758
2759 } while (!alldone);
2760
2761out:
2762
2763 return num_sge;
2764}
2765
2766/**
2767 * lpfc_prot_group_type - Get prtotection group type of SCSI command
2768 * @phba: The Hba for which this call is being executed.
2769 * @sc: pointer to scsi command we're working on
2770 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002771 * Given a SCSI command that supports DIF, determine composition of protection
2772 * groups involved in setting up buffer lists
2773 *
James Smartacd68592012-01-18 16:25:09 -05002774 * Returns: Protection group type (with or without DIF)
2775 *
2776 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002777static int
2778lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
2779{
2780 int ret = LPFC_PG_TYPE_INVALID;
2781 unsigned char op = scsi_get_prot_op(sc);
2782
2783 switch (op) {
2784 case SCSI_PROT_READ_STRIP:
2785 case SCSI_PROT_WRITE_INSERT:
2786 ret = LPFC_PG_TYPE_NO_DIF;
2787 break;
2788 case SCSI_PROT_READ_INSERT:
2789 case SCSI_PROT_WRITE_STRIP:
2790 case SCSI_PROT_READ_PASS:
2791 case SCSI_PROT_WRITE_PASS:
James Smarte2a0a9d2008-12-04 22:40:02 -05002792 ret = LPFC_PG_TYPE_DIF_BUF;
2793 break;
2794 default:
2795 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2796 "9021 Unsupported protection op:%d\n", op);
2797 break;
2798 }
2799
2800 return ret;
2801}
2802
James Smartacd68592012-01-18 16:25:09 -05002803/**
James Smarta6887e22013-04-17 20:18:07 -04002804 * lpfc_bg_scsi_adjust_dl - Adjust SCSI data length for BlockGuard
2805 * @phba: The Hba for which this call is being executed.
2806 * @lpfc_cmd: The scsi buffer which is going to be adjusted.
2807 *
2808 * Adjust the data length to account for how much data
2809 * is actually on the wire.
2810 *
2811 * returns the adjusted data length
2812 **/
2813static int
2814lpfc_bg_scsi_adjust_dl(struct lpfc_hba *phba,
2815 struct lpfc_scsi_buf *lpfc_cmd)
2816{
2817 struct scsi_cmnd *sc = lpfc_cmd->pCmd;
2818 int fcpdl;
2819
2820 fcpdl = scsi_bufflen(sc);
2821
2822 /* Check if there is protection data on the wire */
2823 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
2824 /* Read */
2825 if (scsi_get_prot_op(sc) == SCSI_PROT_READ_INSERT)
2826 return fcpdl;
2827
2828 } else {
2829 /* Write */
2830 if (scsi_get_prot_op(sc) == SCSI_PROT_WRITE_STRIP)
2831 return fcpdl;
2832 }
2833
2834 /*
2835 * If we are in DIF Type 1 mode every data block has a 8 byte
2836 * DIF (trailer) attached to it. Must ajust FCP data length.
2837 */
2838 if (scsi_prot_flagged(sc, SCSI_PROT_TRANSFER_PI))
2839 fcpdl += (fcpdl / lpfc_cmd_blksize(sc)) * 8;
2840
2841 return fcpdl;
2842}
2843
2844/**
James Smartacd68592012-01-18 16:25:09 -05002845 * lpfc_bg_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
2846 * @phba: The Hba for which this call is being executed.
2847 * @lpfc_cmd: The scsi buffer which is going to be prep'ed.
2848 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002849 * This is the protection/DIF aware version of
2850 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
2851 * two functions eventually, but for now, it's here
James Smartacd68592012-01-18 16:25:09 -05002852 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002853static int
James Smartacd68592012-01-18 16:25:09 -05002854lpfc_bg_scsi_prep_dma_buf_s3(struct lpfc_hba *phba,
James Smarte2a0a9d2008-12-04 22:40:02 -05002855 struct lpfc_scsi_buf *lpfc_cmd)
2856{
2857 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2858 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2859 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
2860 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2861 uint32_t num_bde = 0;
2862 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
2863 int prot_group_type = 0;
James Smarta6887e22013-04-17 20:18:07 -04002864 int fcpdl;
James Smarte2a0a9d2008-12-04 22:40:02 -05002865
2866 /*
2867 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
2868 * fcp_rsp regions to the first data bde entry
2869 */
2870 bpl += 2;
2871 if (scsi_sg_count(scsi_cmnd)) {
2872 /*
2873 * The driver stores the segment count returned from pci_map_sg
2874 * because this a count of dma-mappings used to map the use_sg
2875 * pages. They are not guaranteed to be the same for those
2876 * architectures that implement an IOMMU.
2877 */
2878 datasegcnt = dma_map_sg(&phba->pcidev->dev,
2879 scsi_sglist(scsi_cmnd),
2880 scsi_sg_count(scsi_cmnd), datadir);
2881 if (unlikely(!datasegcnt))
2882 return 1;
2883
2884 lpfc_cmd->seg_cnt = datasegcnt;
James Smart96f70772013-04-17 20:16:15 -04002885
2886 /* First check if data segment count from SCSI Layer is good */
2887 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt)
2888 goto err;
James Smarte2a0a9d2008-12-04 22:40:02 -05002889
2890 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
2891
2892 switch (prot_group_type) {
2893 case LPFC_PG_TYPE_NO_DIF:
James Smart96f70772013-04-17 20:16:15 -04002894
2895 /* Here we need to add a PDE5 and PDE6 to the count */
2896 if ((lpfc_cmd->seg_cnt + 2) > phba->cfg_total_seg_cnt)
2897 goto err;
2898
James Smarte2a0a9d2008-12-04 22:40:02 -05002899 num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
2900 datasegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002901 /* we should have 2 or more entries in buffer list */
James Smarte2a0a9d2008-12-04 22:40:02 -05002902 if (num_bde < 2)
2903 goto err;
2904 break;
James Smart96f70772013-04-17 20:16:15 -04002905
2906 case LPFC_PG_TYPE_DIF_BUF:
James Smarte2a0a9d2008-12-04 22:40:02 -05002907 /*
2908 * This type indicates that protection buffers are
2909 * passed to the driver, so that needs to be prepared
2910 * for DMA
2911 */
2912 protsegcnt = dma_map_sg(&phba->pcidev->dev,
2913 scsi_prot_sglist(scsi_cmnd),
2914 scsi_prot_sg_count(scsi_cmnd), datadir);
2915 if (unlikely(!protsegcnt)) {
2916 scsi_dma_unmap(scsi_cmnd);
2917 return 1;
2918 }
2919
2920 lpfc_cmd->prot_seg_cnt = protsegcnt;
James Smart96f70772013-04-17 20:16:15 -04002921
2922 /*
2923 * There is a minimun of 4 BPLs used for every
2924 * protection data segment.
2925 */
2926 if ((lpfc_cmd->prot_seg_cnt * 4) >
2927 (phba->cfg_total_seg_cnt - 2))
2928 goto err;
James Smarte2a0a9d2008-12-04 22:40:02 -05002929
2930 num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
2931 datasegcnt, protsegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002932 /* we should have 3 or more entries in buffer list */
James Smart96f70772013-04-17 20:16:15 -04002933 if ((num_bde < 3) ||
2934 (num_bde > phba->cfg_total_seg_cnt))
James Smarte2a0a9d2008-12-04 22:40:02 -05002935 goto err;
2936 break;
James Smart96f70772013-04-17 20:16:15 -04002937
James Smarte2a0a9d2008-12-04 22:40:02 -05002938 case LPFC_PG_TYPE_INVALID:
2939 default:
James Smart96f70772013-04-17 20:16:15 -04002940 scsi_dma_unmap(scsi_cmnd);
2941 lpfc_cmd->seg_cnt = 0;
2942
James Smarte2a0a9d2008-12-04 22:40:02 -05002943 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2944 "9022 Unexpected protection group %i\n",
2945 prot_group_type);
2946 return 1;
2947 }
2948 }
2949
2950 /*
2951 * Finish initializing those IOCB fields that are dependent on the
2952 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
2953 * reinitialized since all iocb memory resources are used many times
2954 * for transmit, receive, and continuation bpl's.
2955 */
2956 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
2957 iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
2958 iocb_cmd->ulpBdeCount = 1;
2959 iocb_cmd->ulpLe = 1;
2960
James Smarta6887e22013-04-17 20:18:07 -04002961 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002962 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
2963
2964 /*
2965 * Due to difference in data length between DIF/non-DIF paths,
2966 * we need to set word 4 of IOCB here
2967 */
2968 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
2969
2970 return 0;
2971err:
James Smart96f70772013-04-17 20:16:15 -04002972 if (lpfc_cmd->seg_cnt)
2973 scsi_dma_unmap(scsi_cmnd);
2974 if (lpfc_cmd->prot_seg_cnt)
2975 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
2976 scsi_prot_sg_count(scsi_cmnd),
2977 scsi_cmnd->sc_data_direction);
2978
James Smarte2a0a9d2008-12-04 22:40:02 -05002979 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart96f70772013-04-17 20:16:15 -04002980 "9023 Cannot setup S/G List for HBA"
2981 "IO segs %d/%d BPL %d SCSI %d: %d %d\n",
2982 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
2983 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
James Smarte2a0a9d2008-12-04 22:40:02 -05002984 prot_group_type, num_bde);
James Smart96f70772013-04-17 20:16:15 -04002985
2986 lpfc_cmd->seg_cnt = 0;
2987 lpfc_cmd->prot_seg_cnt = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05002988 return 1;
2989}
2990
2991/*
James Smart737d4242013-04-17 20:14:49 -04002992 * This function calcuates the T10 DIF guard tag
2993 * on the specified data using a CRC algorithmn
2994 * using crc_t10dif.
2995 */
2996uint16_t
2997lpfc_bg_crc(uint8_t *data, int count)
2998{
2999 uint16_t crc = 0;
3000 uint16_t x;
3001
3002 crc = crc_t10dif(data, count);
3003 x = cpu_to_be16(crc);
3004 return x;
3005}
3006
3007/*
3008 * This function calcuates the T10 DIF guard tag
3009 * on the specified data using a CSUM algorithmn
3010 * using ip_compute_csum.
3011 */
3012uint16_t
3013lpfc_bg_csum(uint8_t *data, int count)
3014{
3015 uint16_t ret;
3016
3017 ret = ip_compute_csum(data, count);
3018 return ret;
3019}
3020
3021/*
3022 * This function examines the protection data to try to determine
3023 * what type of T10-DIF error occurred.
3024 */
3025void
3026lpfc_calc_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3027{
3028 struct scatterlist *sgpe; /* s/g prot entry */
3029 struct scatterlist *sgde; /* s/g data entry */
3030 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
3031 struct scsi_dif_tuple *src = NULL;
3032 uint8_t *data_src = NULL;
3033 uint16_t guard_tag, guard_type;
3034 uint16_t start_app_tag, app_tag;
3035 uint32_t start_ref_tag, ref_tag;
3036 int prot, protsegcnt;
3037 int err_type, len, data_len;
3038 int chk_ref, chk_app, chk_guard;
3039 uint16_t sum;
3040 unsigned blksize;
3041
3042 err_type = BGS_GUARD_ERR_MASK;
3043 sum = 0;
3044 guard_tag = 0;
3045
3046 /* First check to see if there is protection data to examine */
3047 prot = scsi_get_prot_op(cmd);
3048 if ((prot == SCSI_PROT_READ_STRIP) ||
3049 (prot == SCSI_PROT_WRITE_INSERT) ||
3050 (prot == SCSI_PROT_NORMAL))
3051 goto out;
3052
3053 /* Currently the driver just supports ref_tag and guard_tag checking */
3054 chk_ref = 1;
3055 chk_app = 0;
3056 chk_guard = 0;
3057
3058 /* Setup a ptr to the protection data provided by the SCSI host */
3059 sgpe = scsi_prot_sglist(cmd);
3060 protsegcnt = lpfc_cmd->prot_seg_cnt;
3061
3062 if (sgpe && protsegcnt) {
3063
3064 /*
3065 * We will only try to verify guard tag if the segment
3066 * data length is a multiple of the blksize.
3067 */
3068 sgde = scsi_sglist(cmd);
3069 blksize = lpfc_cmd_blksize(cmd);
3070 data_src = (uint8_t *)sg_virt(sgde);
3071 data_len = sgde->length;
3072 if ((data_len & (blksize - 1)) == 0)
3073 chk_guard = 1;
3074 guard_type = scsi_host_get_guard(cmd->device->host);
3075
James Smarta6887e22013-04-17 20:18:07 -04003076 start_ref_tag = (uint32_t)scsi_get_lba(cmd); /* Truncate LBA */
James Smart737d4242013-04-17 20:14:49 -04003077 start_app_tag = src->app_tag;
3078 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
3079 len = sgpe->length;
3080 while (src && protsegcnt) {
3081 while (len) {
3082
3083 /*
3084 * First check to see if a protection data
3085 * check is valid
3086 */
3087 if ((src->ref_tag == 0xffffffff) ||
3088 (src->app_tag == 0xffff)) {
3089 start_ref_tag++;
3090 goto skipit;
3091 }
3092
3093 /* App Tag checking */
3094 app_tag = src->app_tag;
3095 if (chk_app && (app_tag != start_app_tag)) {
3096 err_type = BGS_APPTAG_ERR_MASK;
3097 goto out;
3098 }
3099
3100 /* Reference Tag checking */
3101 ref_tag = be32_to_cpu(src->ref_tag);
3102 if (chk_ref && (ref_tag != start_ref_tag)) {
3103 err_type = BGS_REFTAG_ERR_MASK;
3104 goto out;
3105 }
3106 start_ref_tag++;
3107
3108 /* Guard Tag checking */
3109 if (chk_guard) {
3110 guard_tag = src->guard_tag;
3111 if (guard_type == SHOST_DIX_GUARD_IP)
3112 sum = lpfc_bg_csum(data_src,
3113 blksize);
3114 else
3115 sum = lpfc_bg_crc(data_src,
3116 blksize);
3117 if ((guard_tag != sum)) {
3118 err_type = BGS_GUARD_ERR_MASK;
3119 goto out;
3120 }
3121 }
3122skipit:
3123 len -= sizeof(struct scsi_dif_tuple);
3124 if (len < 0)
3125 len = 0;
3126 src++;
3127
3128 data_src += blksize;
3129 data_len -= blksize;
3130
3131 /*
3132 * Are we at the end of the Data segment?
3133 * The data segment is only used for Guard
3134 * tag checking.
3135 */
3136 if (chk_guard && (data_len == 0)) {
3137 chk_guard = 0;
3138 sgde = sg_next(sgde);
3139 if (!sgde)
3140 goto out;
3141
3142 data_src = (uint8_t *)sg_virt(sgde);
3143 data_len = sgde->length;
3144 if ((data_len & (blksize - 1)) == 0)
3145 chk_guard = 1;
3146 }
3147 }
3148
3149 /* Goto the next Protection data segment */
3150 sgpe = sg_next(sgpe);
3151 if (sgpe) {
3152 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
3153 len = sgpe->length;
3154 } else {
3155 src = NULL;
3156 }
3157 protsegcnt--;
3158 }
3159 }
3160out:
3161 if (err_type == BGS_GUARD_ERR_MASK) {
3162 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3163 0x10, 0x1);
3164 cmd->result = DRIVER_SENSE << 24
3165 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3166 phba->bg_guard_err_cnt++;
3167 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3168 "9069 BLKGRD: LBA %lx grd_tag error %x != %x\n",
3169 (unsigned long)scsi_get_lba(cmd),
3170 sum, guard_tag);
3171
3172 } else if (err_type == BGS_REFTAG_ERR_MASK) {
3173 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3174 0x10, 0x3);
3175 cmd->result = DRIVER_SENSE << 24
3176 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3177
3178 phba->bg_reftag_err_cnt++;
3179 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3180 "9066 BLKGRD: LBA %lx ref_tag error %x != %x\n",
3181 (unsigned long)scsi_get_lba(cmd),
3182 ref_tag, start_ref_tag);
3183
3184 } else if (err_type == BGS_APPTAG_ERR_MASK) {
3185 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3186 0x10, 0x2);
3187 cmd->result = DRIVER_SENSE << 24
3188 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3189
3190 phba->bg_apptag_err_cnt++;
3191 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3192 "9041 BLKGRD: LBA %lx app_tag error %x != %x\n",
3193 (unsigned long)scsi_get_lba(cmd),
3194 app_tag, start_app_tag);
3195 }
3196}
3197
3198
3199/*
James Smarte2a0a9d2008-12-04 22:40:02 -05003200 * This function checks for BlockGuard errors detected by
3201 * the HBA. In case of errors, the ASC/ASCQ fields in the
3202 * sense buffer will be set accordingly, paired with
3203 * ILLEGAL_REQUEST to signal to the kernel that the HBA
3204 * detected corruption.
3205 *
3206 * Returns:
3207 * 0 - No error found
3208 * 1 - BlockGuard error found
3209 * -1 - Internal error (bad profile, ...etc)
3210 */
3211static int
3212lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
3213 struct lpfc_iocbq *pIocbOut)
3214{
3215 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
3216 struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
3217 int ret = 0;
3218 uint32_t bghm = bgf->bghm;
3219 uint32_t bgstat = bgf->bgstat;
3220 uint64_t failing_sector = 0;
3221
James Smarte2a0a9d2008-12-04 22:40:02 -05003222 spin_lock(&_dump_buf_lock);
3223 if (!_dump_buf_done) {
James Smart6a9c52c2009-10-02 15:16:51 -04003224 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9070 BLKGRD: Saving"
3225 " Data for %u blocks to debugfs\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05003226 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
James Smart6a9c52c2009-10-02 15:16:51 -04003227 lpfc_debug_save_data(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003228
3229 /* If we have a prot sgl, save the DIF buffer */
3230 if (lpfc_prot_group_type(phba, cmd) ==
3231 LPFC_PG_TYPE_DIF_BUF) {
James Smart6a9c52c2009-10-02 15:16:51 -04003232 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9071 BLKGRD: "
3233 "Saving DIF for %u blocks to debugfs\n",
3234 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
3235 lpfc_debug_save_dif(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003236 }
3237
3238 _dump_buf_done = 1;
3239 }
3240 spin_unlock(&_dump_buf_lock);
3241
3242 if (lpfc_bgs_get_invalid_prof(bgstat)) {
3243 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart737d4242013-04-17 20:14:49 -04003244 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3245 "9072 BLKGRD: Invalid BG Profile in cmd"
3246 " 0x%x lba 0x%llx blk cnt 0x%x "
3247 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3248 (unsigned long long)scsi_get_lba(cmd),
3249 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003250 ret = (-1);
3251 goto out;
3252 }
3253
3254 if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
3255 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart737d4242013-04-17 20:14:49 -04003256 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3257 "9073 BLKGRD: Invalid BG PDIF Block in cmd"
3258 " 0x%x lba 0x%llx blk cnt 0x%x "
3259 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3260 (unsigned long long)scsi_get_lba(cmd),
3261 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003262 ret = (-1);
3263 goto out;
3264 }
3265
3266 if (lpfc_bgs_get_guard_err(bgstat)) {
3267 ret = 1;
3268
3269 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3270 0x10, 0x1);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05003271 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05003272 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3273 phba->bg_guard_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04003274 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3275 "9055 BLKGRD: Guard Tag error in cmd"
3276 " 0x%x lba 0x%llx blk cnt 0x%x "
3277 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3278 (unsigned long long)scsi_get_lba(cmd),
3279 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003280 }
3281
3282 if (lpfc_bgs_get_reftag_err(bgstat)) {
3283 ret = 1;
3284
3285 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3286 0x10, 0x3);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05003287 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05003288 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3289
3290 phba->bg_reftag_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04003291 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3292 "9056 BLKGRD: Ref Tag error in cmd"
3293 " 0x%x lba 0x%llx blk cnt 0x%x "
3294 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3295 (unsigned long long)scsi_get_lba(cmd),
3296 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003297 }
3298
3299 if (lpfc_bgs_get_apptag_err(bgstat)) {
3300 ret = 1;
3301
3302 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3303 0x10, 0x2);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05003304 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05003305 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3306
3307 phba->bg_apptag_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04003308 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3309 "9061 BLKGRD: App Tag error in cmd"
3310 " 0x%x lba 0x%llx blk cnt 0x%x "
3311 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3312 (unsigned long long)scsi_get_lba(cmd),
3313 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003314 }
3315
3316 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
3317 /*
3318 * setup sense data descriptor 0 per SPC-4 as an information
James Smart7c56b9f2011-07-22 18:36:25 -04003319 * field, and put the failing LBA in it.
3320 * This code assumes there was also a guard/app/ref tag error
3321 * indication.
James Smarte2a0a9d2008-12-04 22:40:02 -05003322 */
James Smart7c56b9f2011-07-22 18:36:25 -04003323 cmd->sense_buffer[7] = 0xc; /* Additional sense length */
3324 cmd->sense_buffer[8] = 0; /* Information descriptor type */
3325 cmd->sense_buffer[9] = 0xa; /* Additional descriptor length */
3326 cmd->sense_buffer[10] = 0x80; /* Validity bit */
James Smartacd68592012-01-18 16:25:09 -05003327
3328 /* bghm is a "on the wire" FC frame based count */
3329 switch (scsi_get_prot_op(cmd)) {
3330 case SCSI_PROT_READ_INSERT:
3331 case SCSI_PROT_WRITE_STRIP:
3332 bghm /= cmd->device->sector_size;
3333 break;
3334 case SCSI_PROT_READ_STRIP:
3335 case SCSI_PROT_WRITE_INSERT:
3336 case SCSI_PROT_READ_PASS:
3337 case SCSI_PROT_WRITE_PASS:
3338 bghm /= (cmd->device->sector_size +
3339 sizeof(struct scsi_dif_tuple));
3340 break;
3341 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003342
3343 failing_sector = scsi_get_lba(cmd);
3344 failing_sector += bghm;
3345
James Smart7c56b9f2011-07-22 18:36:25 -04003346 /* Descriptor Information */
3347 put_unaligned_be64(failing_sector, &cmd->sense_buffer[12]);
James Smarte2a0a9d2008-12-04 22:40:02 -05003348 }
3349
3350 if (!ret) {
3351 /* No error was reported - problem in FW? */
James Smart737d4242013-04-17 20:14:49 -04003352 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3353 "9057 BLKGRD: Unknown error in cmd"
3354 " 0x%x lba 0x%llx blk cnt 0x%x "
3355 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3356 (unsigned long long)scsi_get_lba(cmd),
3357 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003358
James Smart737d4242013-04-17 20:14:49 -04003359 /* Calcuate what type of error it was */
3360 lpfc_calc_bg_err(phba, lpfc_cmd);
3361 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003362out:
3363 return ret;
3364}
3365
James Smartea2151b2008-09-07 11:52:10 -04003366/**
James Smartda0436e2009-05-22 14:51:39 -04003367 * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3368 * @phba: The Hba for which this call is being executed.
3369 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3370 *
3371 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
3372 * field of @lpfc_cmd for device with SLI-4 interface spec.
3373 *
3374 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04003375 * 1 - Error
3376 * 0 - Success
James Smartda0436e2009-05-22 14:51:39 -04003377 **/
3378static int
3379lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3380{
3381 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3382 struct scatterlist *sgel = NULL;
3383 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3384 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
James Smartfedd3b72011-02-16 12:39:24 -05003385 struct sli4_sge *first_data_sgl;
James Smartda0436e2009-05-22 14:51:39 -04003386 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
3387 dma_addr_t physaddr;
3388 uint32_t num_bde = 0;
3389 uint32_t dma_len;
3390 uint32_t dma_offset = 0;
3391 int nseg;
James Smartfedd3b72011-02-16 12:39:24 -05003392 struct ulp_bde64 *bde;
James Smartda0436e2009-05-22 14:51:39 -04003393
3394 /*
3395 * There are three possibilities here - use scatter-gather segment, use
3396 * the single mapping, or neither. Start the lpfc command prep by
3397 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
3398 * data bde entry.
3399 */
3400 if (scsi_sg_count(scsi_cmnd)) {
3401 /*
3402 * The driver stores the segment count returned from pci_map_sg
3403 * because this a count of dma-mappings used to map the use_sg
3404 * pages. They are not guaranteed to be the same for those
3405 * architectures that implement an IOMMU.
3406 */
3407
3408 nseg = scsi_dma_map(scsi_cmnd);
3409 if (unlikely(!nseg))
3410 return 1;
3411 sgl += 1;
3412 /* clear the last flag in the fcp_rsp map entry */
3413 sgl->word2 = le32_to_cpu(sgl->word2);
3414 bf_set(lpfc_sli4_sge_last, sgl, 0);
3415 sgl->word2 = cpu_to_le32(sgl->word2);
3416 sgl += 1;
James Smartfedd3b72011-02-16 12:39:24 -05003417 first_data_sgl = sgl;
James Smartda0436e2009-05-22 14:51:39 -04003418 lpfc_cmd->seg_cnt = nseg;
3419 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04003420 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
3421 " %s: Too many sg segments from "
3422 "dma_map_sg. Config %d, seg_cnt %d\n",
3423 __func__, phba->cfg_sg_seg_cnt,
James Smartda0436e2009-05-22 14:51:39 -04003424 lpfc_cmd->seg_cnt);
James Smart96f70772013-04-17 20:16:15 -04003425 lpfc_cmd->seg_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04003426 scsi_dma_unmap(scsi_cmnd);
3427 return 1;
3428 }
3429
3430 /*
3431 * The driver established a maximum scatter-gather segment count
3432 * during probe that limits the number of sg elements in any
3433 * single scsi command. Just run through the seg_cnt and format
3434 * the sge's.
3435 * When using SLI-3 the driver will try to fit all the BDEs into
3436 * the IOCB. If it can't then the BDEs get added to a BPL as it
3437 * does for SLI-2 mode.
3438 */
3439 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
3440 physaddr = sg_dma_address(sgel);
3441 dma_len = sg_dma_len(sgel);
James Smartda0436e2009-05-22 14:51:39 -04003442 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
3443 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
James Smart05580562011-05-24 11:40:48 -04003444 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -04003445 if ((num_bde + 1) == nseg)
3446 bf_set(lpfc_sli4_sge_last, sgl, 1);
3447 else
3448 bf_set(lpfc_sli4_sge_last, sgl, 0);
3449 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
James Smartf9bb2da2011-10-10 21:34:11 -04003450 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
James Smartda0436e2009-05-22 14:51:39 -04003451 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05003452 sgl->sge_len = cpu_to_le32(dma_len);
James Smartda0436e2009-05-22 14:51:39 -04003453 dma_offset += dma_len;
3454 sgl++;
3455 }
James Smartfedd3b72011-02-16 12:39:24 -05003456 /* setup the performance hint (first data BDE) if enabled */
3457 if (phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) {
3458 bde = (struct ulp_bde64 *)
3459 &(iocb_cmd->unsli3.sli3Words[5]);
3460 bde->addrLow = first_data_sgl->addr_lo;
3461 bde->addrHigh = first_data_sgl->addr_hi;
3462 bde->tus.f.bdeSize =
3463 le32_to_cpu(first_data_sgl->sge_len);
3464 bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
3465 bde->tus.w = cpu_to_le32(bde->tus.w);
3466 }
James Smartda0436e2009-05-22 14:51:39 -04003467 } else {
3468 sgl += 1;
3469 /* clear the last flag in the fcp_rsp map entry */
3470 sgl->word2 = le32_to_cpu(sgl->word2);
3471 bf_set(lpfc_sli4_sge_last, sgl, 1);
3472 sgl->word2 = cpu_to_le32(sgl->word2);
3473 }
3474
3475 /*
3476 * Finish initializing those IOCB fields that are dependent on the
3477 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
3478 * explicitly reinitialized.
3479 * all iocb memory resources are reused.
3480 */
3481 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
3482
3483 /*
3484 * Due to difference in data length between DIF/non-DIF paths,
3485 * we need to set word 4 of IOCB here
3486 */
3487 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
3488 return 0;
3489}
3490
3491/**
James Smartacd68592012-01-18 16:25:09 -05003492 * lpfc_bg_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3493 * @phba: The Hba for which this call is being executed.
3494 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3495 *
3496 * This is the protection/DIF aware version of
3497 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
3498 * two functions eventually, but for now, it's here
3499 **/
3500static int
3501lpfc_bg_scsi_prep_dma_buf_s4(struct lpfc_hba *phba,
3502 struct lpfc_scsi_buf *lpfc_cmd)
3503{
3504 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3505 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3506 struct sli4_sge *sgl = (struct sli4_sge *)(lpfc_cmd->fcp_bpl);
3507 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart96f70772013-04-17 20:16:15 -04003508 uint32_t num_sge = 0;
James Smartacd68592012-01-18 16:25:09 -05003509 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
3510 int prot_group_type = 0;
3511 int fcpdl;
3512
3513 /*
3514 * Start the lpfc command prep by bumping the sgl beyond fcp_cmnd
James Smart96f70772013-04-17 20:16:15 -04003515 * fcp_rsp regions to the first data sge entry
James Smartacd68592012-01-18 16:25:09 -05003516 */
3517 if (scsi_sg_count(scsi_cmnd)) {
3518 /*
3519 * The driver stores the segment count returned from pci_map_sg
3520 * because this a count of dma-mappings used to map the use_sg
3521 * pages. They are not guaranteed to be the same for those
3522 * architectures that implement an IOMMU.
3523 */
3524 datasegcnt = dma_map_sg(&phba->pcidev->dev,
3525 scsi_sglist(scsi_cmnd),
3526 scsi_sg_count(scsi_cmnd), datadir);
3527 if (unlikely(!datasegcnt))
3528 return 1;
3529
3530 sgl += 1;
3531 /* clear the last flag in the fcp_rsp map entry */
3532 sgl->word2 = le32_to_cpu(sgl->word2);
3533 bf_set(lpfc_sli4_sge_last, sgl, 0);
3534 sgl->word2 = cpu_to_le32(sgl->word2);
3535
3536 sgl += 1;
3537 lpfc_cmd->seg_cnt = datasegcnt;
James Smart96f70772013-04-17 20:16:15 -04003538
3539 /* First check if data segment count from SCSI Layer is good */
3540 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt)
3541 goto err;
James Smartacd68592012-01-18 16:25:09 -05003542
3543 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
3544
3545 switch (prot_group_type) {
3546 case LPFC_PG_TYPE_NO_DIF:
James Smart96f70772013-04-17 20:16:15 -04003547 /* Here we need to add a DISEED to the count */
3548 if ((lpfc_cmd->seg_cnt + 1) > phba->cfg_total_seg_cnt)
3549 goto err;
3550
3551 num_sge = lpfc_bg_setup_sgl(phba, scsi_cmnd, sgl,
James Smartacd68592012-01-18 16:25:09 -05003552 datasegcnt);
James Smart96f70772013-04-17 20:16:15 -04003553
James Smartacd68592012-01-18 16:25:09 -05003554 /* we should have 2 or more entries in buffer list */
James Smart96f70772013-04-17 20:16:15 -04003555 if (num_sge < 2)
James Smartacd68592012-01-18 16:25:09 -05003556 goto err;
3557 break;
James Smart96f70772013-04-17 20:16:15 -04003558
3559 case LPFC_PG_TYPE_DIF_BUF:
James Smartacd68592012-01-18 16:25:09 -05003560 /*
3561 * This type indicates that protection buffers are
3562 * passed to the driver, so that needs to be prepared
3563 * for DMA
3564 */
3565 protsegcnt = dma_map_sg(&phba->pcidev->dev,
3566 scsi_prot_sglist(scsi_cmnd),
3567 scsi_prot_sg_count(scsi_cmnd), datadir);
3568 if (unlikely(!protsegcnt)) {
3569 scsi_dma_unmap(scsi_cmnd);
3570 return 1;
3571 }
3572
3573 lpfc_cmd->prot_seg_cnt = protsegcnt;
James Smart96f70772013-04-17 20:16:15 -04003574 /*
3575 * There is a minimun of 3 SGEs used for every
3576 * protection data segment.
3577 */
3578 if ((lpfc_cmd->prot_seg_cnt * 3) >
3579 (phba->cfg_total_seg_cnt - 2))
3580 goto err;
James Smartacd68592012-01-18 16:25:09 -05003581
James Smart96f70772013-04-17 20:16:15 -04003582 num_sge = lpfc_bg_setup_sgl_prot(phba, scsi_cmnd, sgl,
James Smartacd68592012-01-18 16:25:09 -05003583 datasegcnt, protsegcnt);
James Smart96f70772013-04-17 20:16:15 -04003584
James Smartacd68592012-01-18 16:25:09 -05003585 /* we should have 3 or more entries in buffer list */
James Smart96f70772013-04-17 20:16:15 -04003586 if ((num_sge < 3) ||
3587 (num_sge > phba->cfg_total_seg_cnt))
James Smartacd68592012-01-18 16:25:09 -05003588 goto err;
3589 break;
James Smart96f70772013-04-17 20:16:15 -04003590
James Smartacd68592012-01-18 16:25:09 -05003591 case LPFC_PG_TYPE_INVALID:
3592 default:
James Smart96f70772013-04-17 20:16:15 -04003593 scsi_dma_unmap(scsi_cmnd);
3594 lpfc_cmd->seg_cnt = 0;
3595
James Smartacd68592012-01-18 16:25:09 -05003596 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
3597 "9083 Unexpected protection group %i\n",
3598 prot_group_type);
3599 return 1;
3600 }
3601 }
3602
James Smart8012cc32012-10-31 14:44:49 -04003603 switch (scsi_get_prot_op(scsi_cmnd)) {
3604 case SCSI_PROT_WRITE_STRIP:
3605 case SCSI_PROT_READ_STRIP:
3606 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_STRIP;
3607 break;
3608 case SCSI_PROT_WRITE_INSERT:
3609 case SCSI_PROT_READ_INSERT:
3610 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_INSERT;
3611 break;
3612 case SCSI_PROT_WRITE_PASS:
3613 case SCSI_PROT_READ_PASS:
3614 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_PASS;
3615 break;
3616 }
3617
James Smartacd68592012-01-18 16:25:09 -05003618 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
James Smartacd68592012-01-18 16:25:09 -05003619 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
3620
3621 /*
3622 * Due to difference in data length between DIF/non-DIF paths,
3623 * we need to set word 4 of IOCB here
3624 */
3625 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
James Smartacd68592012-01-18 16:25:09 -05003626
3627 return 0;
3628err:
James Smart96f70772013-04-17 20:16:15 -04003629 if (lpfc_cmd->seg_cnt)
3630 scsi_dma_unmap(scsi_cmnd);
3631 if (lpfc_cmd->prot_seg_cnt)
3632 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
3633 scsi_prot_sg_count(scsi_cmnd),
3634 scsi_cmnd->sc_data_direction);
3635
James Smartacd68592012-01-18 16:25:09 -05003636 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart96f70772013-04-17 20:16:15 -04003637 "9084 Cannot setup S/G List for HBA"
3638 "IO segs %d/%d SGL %d SCSI %d: %d %d\n",
3639 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
3640 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
3641 prot_group_type, num_sge);
3642
3643 lpfc_cmd->seg_cnt = 0;
3644 lpfc_cmd->prot_seg_cnt = 0;
James Smartacd68592012-01-18 16:25:09 -05003645 return 1;
3646}
3647
3648/**
James Smart3772a992009-05-22 14:50:54 -04003649 * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3650 * @phba: The Hba for which this call is being executed.
3651 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3652 *
3653 * This routine wraps the actual DMA mapping function pointer from the
3654 * lpfc_hba struct.
3655 *
3656 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04003657 * 1 - Error
3658 * 0 - Success
James Smart3772a992009-05-22 14:50:54 -04003659 **/
3660static inline int
3661lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3662{
3663 return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
3664}
3665
3666/**
James Smartacd68592012-01-18 16:25:09 -05003667 * lpfc_bg_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3668 * using BlockGuard.
3669 * @phba: The Hba for which this call is being executed.
3670 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3671 *
3672 * This routine wraps the actual DMA mapping function pointer from the
3673 * lpfc_hba struct.
3674 *
3675 * Return codes:
3676 * 1 - Error
3677 * 0 - Success
3678 **/
3679static inline int
3680lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3681{
3682 return phba->lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
3683}
3684
3685/**
James Smart3621a712009-04-06 18:47:14 -04003686 * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
James Smartea2151b2008-09-07 11:52:10 -04003687 * @phba: Pointer to hba context object.
3688 * @vport: Pointer to vport object.
3689 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
3690 * @rsp_iocb: Pointer to response iocb object which reported error.
3691 *
3692 * This function posts an event when there is a SCSI command reporting
3693 * error from the scsi device.
3694 **/
3695static void
3696lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
3697 struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
3698 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3699 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
3700 uint32_t resp_info = fcprsp->rspStatus2;
3701 uint32_t scsi_status = fcprsp->rspStatus3;
3702 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
3703 struct lpfc_fast_path_event *fast_path_evt = NULL;
3704 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
3705 unsigned long flags;
3706
James Smart5989b8d2010-10-22 11:06:56 -04003707 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3708 return;
3709
James Smartea2151b2008-09-07 11:52:10 -04003710 /* If there is queuefull or busy condition send a scsi event */
3711 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
3712 (cmnd->result == SAM_STAT_BUSY)) {
3713 fast_path_evt = lpfc_alloc_fast_evt(phba);
3714 if (!fast_path_evt)
3715 return;
3716 fast_path_evt->un.scsi_evt.event_type =
3717 FC_REG_SCSI_EVENT;
3718 fast_path_evt->un.scsi_evt.subcategory =
3719 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
3720 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
3721 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
3722 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
3723 &pnode->nlp_portname, sizeof(struct lpfc_name));
3724 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
3725 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3726 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
3727 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
3728 fast_path_evt = lpfc_alloc_fast_evt(phba);
3729 if (!fast_path_evt)
3730 return;
3731 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
3732 FC_REG_SCSI_EVENT;
3733 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
3734 LPFC_EVENT_CHECK_COND;
3735 fast_path_evt->un.check_cond_evt.scsi_event.lun =
3736 cmnd->device->lun;
3737 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
3738 &pnode->nlp_portname, sizeof(struct lpfc_name));
3739 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
3740 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3741 fast_path_evt->un.check_cond_evt.sense_key =
3742 cmnd->sense_buffer[2] & 0xf;
3743 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
3744 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
3745 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3746 fcpi_parm &&
3747 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
3748 ((scsi_status == SAM_STAT_GOOD) &&
3749 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
3750 /*
3751 * If status is good or resid does not match with fcp_param and
3752 * there is valid fcpi_parm, then there is a read_check error
3753 */
3754 fast_path_evt = lpfc_alloc_fast_evt(phba);
3755 if (!fast_path_evt)
3756 return;
3757 fast_path_evt->un.read_check_error.header.event_type =
3758 FC_REG_FABRIC_EVENT;
3759 fast_path_evt->un.read_check_error.header.subcategory =
3760 LPFC_EVENT_FCPRDCHKERR;
3761 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
3762 &pnode->nlp_portname, sizeof(struct lpfc_name));
3763 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
3764 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3765 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
3766 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
3767 fast_path_evt->un.read_check_error.fcpiparam =
3768 fcpi_parm;
3769 } else
3770 return;
3771
3772 fast_path_evt->vport = vport;
3773 spin_lock_irqsave(&phba->hbalock, flags);
3774 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
3775 spin_unlock_irqrestore(&phba->hbalock, flags);
3776 lpfc_worker_wake_up(phba);
3777 return;
3778}
James Smart9bad7672008-12-04 22:39:02 -05003779
3780/**
James Smartf1126682009-06-10 17:22:44 -04003781 * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
James Smart3772a992009-05-22 14:50:54 -04003782 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -05003783 * @psb: The scsi buffer which is going to be un-mapped.
3784 *
3785 * This routine does DMA un-mapping of scatter gather list of scsi command
James Smart3772a992009-05-22 14:50:54 -04003786 * field of @lpfc_cmd for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05003787 **/
dea31012005-04-17 16:05:31 -05003788static void
James Smartf1126682009-06-10 17:22:44 -04003789lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James Smartbcf4dbf2006-07-06 15:50:08 -04003790{
3791 /*
3792 * There are only two special cases to consider. (1) the scsi command
3793 * requested scatter-gather usage or (2) the scsi command allocated
3794 * a request buffer, but did not request use_sg. There is a third
3795 * case, but it does not require resource deallocation.
3796 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003797 if (psb->seg_cnt > 0)
3798 scsi_dma_unmap(psb->pCmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003799 if (psb->prot_seg_cnt > 0)
3800 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
3801 scsi_prot_sg_count(psb->pCmd),
3802 psb->pCmd->sc_data_direction);
James Smartbcf4dbf2006-07-06 15:50:08 -04003803}
3804
James Smart9bad7672008-12-04 22:39:02 -05003805/**
James Smart3621a712009-04-06 18:47:14 -04003806 * lpfc_handler_fcp_err - FCP response handler
James Smart9bad7672008-12-04 22:39:02 -05003807 * @vport: The virtual port for which this call is being executed.
3808 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
3809 * @rsp_iocb: The response IOCB which contains FCP error.
3810 *
3811 * This routine is called to process response IOCB with status field
3812 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
3813 * based upon SCSI and FCP error.
3814 **/
James Smartbcf4dbf2006-07-06 15:50:08 -04003815static void
James Smart2e0fef82007-06-17 19:56:36 -05003816lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
3817 struct lpfc_iocbq *rsp_iocb)
dea31012005-04-17 16:05:31 -05003818{
3819 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3820 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
3821 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
James Smart7054a602007-04-25 09:52:34 -04003822 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
dea31012005-04-17 16:05:31 -05003823 uint32_t resp_info = fcprsp->rspStatus2;
3824 uint32_t scsi_status = fcprsp->rspStatus3;
James Smartc7743952006-12-02 13:34:42 -05003825 uint32_t *lp;
dea31012005-04-17 16:05:31 -05003826 uint32_t host_status = DID_OK;
3827 uint32_t rsplen = 0;
James Smartc7743952006-12-02 13:34:42 -05003828 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea31012005-04-17 16:05:31 -05003829
James Smartea2151b2008-09-07 11:52:10 -04003830
dea31012005-04-17 16:05:31 -05003831 /*
3832 * If this is a task management command, there is no
3833 * scsi packet associated with this lpfc_cmd. The driver
3834 * consumes it.
3835 */
3836 if (fcpcmd->fcpCntl2) {
3837 scsi_status = 0;
3838 goto out;
3839 }
3840
James Smart6a9c52c2009-10-02 15:16:51 -04003841 if (resp_info & RSP_LEN_VALID) {
3842 rsplen = be32_to_cpu(fcprsp->rspRspLen);
James Smarte40a02c2010-02-26 14:13:54 -05003843 if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
James Smart6a9c52c2009-10-02 15:16:51 -04003844 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3845 "2719 Invalid response length: "
3846 "tgt x%x lun x%x cmnd x%x rsplen x%x\n",
3847 cmnd->device->id,
3848 cmnd->device->lun, cmnd->cmnd[0],
3849 rsplen);
3850 host_status = DID_ERROR;
3851 goto out;
3852 }
James Smarte40a02c2010-02-26 14:13:54 -05003853 if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
3854 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3855 "2757 Protocol failure detected during "
3856 "processing of FCP I/O op: "
3857 "tgt x%x lun x%x cmnd x%x rspInfo3 x%x\n",
3858 cmnd->device->id,
3859 cmnd->device->lun, cmnd->cmnd[0],
3860 fcprsp->rspInfo3);
3861 host_status = DID_ERROR;
3862 goto out;
3863 }
James Smart6a9c52c2009-10-02 15:16:51 -04003864 }
3865
James Smartc7743952006-12-02 13:34:42 -05003866 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
3867 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
3868 if (snslen > SCSI_SENSE_BUFFERSIZE)
3869 snslen = SCSI_SENSE_BUFFERSIZE;
3870
3871 if (resp_info & RSP_LEN_VALID)
3872 rsplen = be32_to_cpu(fcprsp->rspRspLen);
3873 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
3874 }
3875 lp = (uint32_t *)cmnd->sense_buffer;
3876
James Smartaa1c7ee2012-08-14 14:26:06 -04003877 /* special handling for under run conditions */
3878 if (!scsi_status && (resp_info & RESID_UNDER)) {
3879 /* don't log under runs if fcp set... */
3880 if (vport->cfg_log_verbose & LOG_FCP)
3881 logit = LOG_FCP_ERROR;
3882 /* unless operator says so */
3883 if (vport->cfg_log_verbose & LOG_FCP_UNDER)
3884 logit = LOG_FCP_UNDER;
3885 }
James Smartc7743952006-12-02 13:34:42 -05003886
James Smarte8b62012007-08-02 11:10:09 -04003887 lpfc_printf_vlog(vport, KERN_WARNING, logit,
James Smarte2a0a9d2008-12-04 22:40:02 -05003888 "9024 FCP command x%x failed: x%x SNS x%x x%x "
James Smarte8b62012007-08-02 11:10:09 -04003889 "Data: x%x x%x x%x x%x x%x\n",
3890 cmnd->cmnd[0], scsi_status,
3891 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
3892 be32_to_cpu(fcprsp->rspResId),
3893 be32_to_cpu(fcprsp->rspSnsLen),
3894 be32_to_cpu(fcprsp->rspRspLen),
3895 fcprsp->rspInfo3);
dea31012005-04-17 16:05:31 -05003896
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003897 scsi_set_resid(cmnd, 0);
dea31012005-04-17 16:05:31 -05003898 if (resp_info & RESID_UNDER) {
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003899 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea31012005-04-17 16:05:31 -05003900
James Smart73d91e52011-10-10 21:32:10 -04003901 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_UNDER,
James Smarte2a0a9d2008-12-04 22:40:02 -05003902 "9025 FCP Read Underrun, expected %d, "
James Smarte8b62012007-08-02 11:10:09 -04003903 "residual %d Data: x%x x%x x%x\n",
3904 be32_to_cpu(fcpcmd->fcpDl),
3905 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
3906 cmnd->underflow);
dea31012005-04-17 16:05:31 -05003907
3908 /*
James Smart7054a602007-04-25 09:52:34 -04003909 * If there is an under run check if under run reported by
3910 * storage array is same as the under run reported by HBA.
3911 * If this is not same, there is a dropped frame.
3912 */
3913 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3914 fcpi_parm &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003915 (scsi_get_resid(cmnd) != fcpi_parm)) {
James Smarte8b62012007-08-02 11:10:09 -04003916 lpfc_printf_vlog(vport, KERN_WARNING,
3917 LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05003918 "9026 FCP Read Check Error "
James Smarte8b62012007-08-02 11:10:09 -04003919 "and Underrun Data: x%x x%x x%x x%x\n",
3920 be32_to_cpu(fcpcmd->fcpDl),
3921 scsi_get_resid(cmnd), fcpi_parm,
3922 cmnd->cmnd[0]);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003923 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
James Smart7054a602007-04-25 09:52:34 -04003924 host_status = DID_ERROR;
3925 }
3926 /*
dea31012005-04-17 16:05:31 -05003927 * The cmnd->underflow is the minimum number of bytes that must
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003928 * be transferred for this command. Provided a sense condition
dea31012005-04-17 16:05:31 -05003929 * is not present, make sure the actual amount transferred is at
3930 * least the underflow value or fail.
3931 */
3932 if (!(resp_info & SNS_LEN_VALID) &&
3933 (scsi_status == SAM_STAT_GOOD) &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003934 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
3935 < cmnd->underflow)) {
James Smarte8b62012007-08-02 11:10:09 -04003936 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05003937 "9027 FCP command x%x residual "
James Smarte8b62012007-08-02 11:10:09 -04003938 "underrun converted to error "
3939 "Data: x%x x%x x%x\n",
James Smart66dbfbe2007-08-05 06:08:38 -04003940 cmnd->cmnd[0], scsi_bufflen(cmnd),
James Smarte8b62012007-08-02 11:10:09 -04003941 scsi_get_resid(cmnd), cmnd->underflow);
dea31012005-04-17 16:05:31 -05003942 host_status = DID_ERROR;
3943 }
3944 } else if (resp_info & RESID_OVER) {
James Smarte8b62012007-08-02 11:10:09 -04003945 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05003946 "9028 FCP command x%x residual overrun error. "
James Smarte4e74272009-07-19 10:01:38 -04003947 "Data: x%x x%x\n", cmnd->cmnd[0],
James Smarte8b62012007-08-02 11:10:09 -04003948 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea31012005-04-17 16:05:31 -05003949 host_status = DID_ERROR;
3950
3951 /*
3952 * Check SLI validation that all the transfer was actually done
James Smart582dd792012-08-03 12:34:44 -04003953 * (fcpi_parm should be zero).
dea31012005-04-17 16:05:31 -05003954 */
James Smart582dd792012-08-03 12:34:44 -04003955 } else if (fcpi_parm) {
James Smarte8b62012007-08-02 11:10:09 -04003956 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
James Smart582dd792012-08-03 12:34:44 -04003957 "9029 FCP Data Transfer Check Error: "
James Smarteee88772010-09-29 11:19:08 -04003958 "x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003959 be32_to_cpu(fcpcmd->fcpDl),
3960 be32_to_cpu(fcprsp->rspResId),
James Smarteee88772010-09-29 11:19:08 -04003961 fcpi_parm, cmnd->cmnd[0], scsi_status);
3962 switch (scsi_status) {
3963 case SAM_STAT_GOOD:
3964 case SAM_STAT_CHECK_CONDITION:
3965 /* Fabric dropped a data frame. Fail any successful
3966 * command in which we detected dropped frames.
3967 * A status of good or some check conditions could
3968 * be considered a successful command.
3969 */
3970 host_status = DID_ERROR;
3971 break;
3972 }
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003973 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea31012005-04-17 16:05:31 -05003974 }
3975
3976 out:
3977 cmnd->result = ScsiResult(host_status, scsi_status);
James Smartea2151b2008-09-07 11:52:10 -04003978 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
dea31012005-04-17 16:05:31 -05003979}
3980
James Smart9bad7672008-12-04 22:39:02 -05003981/**
James Smart3621a712009-04-06 18:47:14 -04003982 * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
James Smart9bad7672008-12-04 22:39:02 -05003983 * @phba: The Hba for which this call is being executed.
3984 * @pIocbIn: The command IOCBQ for the scsi cmnd.
James Smart3772a992009-05-22 14:50:54 -04003985 * @pIocbOut: The response IOCBQ for the scsi cmnd.
James Smart9bad7672008-12-04 22:39:02 -05003986 *
3987 * This routine assigns scsi command result by looking into response IOCB
3988 * status field appropriately. This routine handles QUEUE FULL condition as
3989 * well by ramping down device queue depth.
3990 **/
dea31012005-04-17 16:05:31 -05003991static void
3992lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
3993 struct lpfc_iocbq *pIocbOut)
3994{
3995 struct lpfc_scsi_buf *lpfc_cmd =
3996 (struct lpfc_scsi_buf *) pIocbIn->context1;
James Smart2e0fef82007-06-17 19:56:36 -05003997 struct lpfc_vport *vport = pIocbIn->vport;
dea31012005-04-17 16:05:31 -05003998 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
3999 struct lpfc_nodelist *pnode = rdata->pnode;
James Smart75baf692010-06-08 18:31:21 -04004000 struct scsi_cmnd *cmd;
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05004001 int result;
James Smarta257bf92009-04-06 18:48:10 -04004002 struct scsi_device *tmp_sdev;
James Smart5ffc2662009-11-18 15:39:44 -05004003 int depth;
James Smartfa61a542008-01-11 01:52:42 -05004004 unsigned long flags;
James Smartea2151b2008-09-07 11:52:10 -04004005 struct lpfc_fast_path_event *fast_path_evt;
James Smart75baf692010-06-08 18:31:21 -04004006 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04004007 uint32_t queue_depth, scsi_id;
James Smart73d91e52011-10-10 21:32:10 -04004008 uint32_t logit = LOG_FCP;
dea31012005-04-17 16:05:31 -05004009
James Smart75baf692010-06-08 18:31:21 -04004010 /* Sanity check on return of outstanding command */
4011 if (!(lpfc_cmd->pCmd))
4012 return;
4013 cmd = lpfc_cmd->pCmd;
4014 shost = cmd->device->host;
4015
James Smarte3d2b802012-08-14 14:25:43 -04004016 lpfc_cmd->result = (pIocbOut->iocb.un.ulpWord[4] & IOERR_PARAM_MASK);
dea31012005-04-17 16:05:31 -05004017 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
James Smart341af102010-01-26 23:07:37 -05004018 /* pick up SLI4 exhange busy status from HBA */
4019 lpfc_cmd->exch_busy = pIocbOut->iocb_flag & LPFC_EXCHANGE_BUSY;
4020
James Smart9a6b09c2012-03-01 22:37:42 -05004021#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4022 if (lpfc_cmd->prot_data_type) {
4023 struct scsi_dif_tuple *src = NULL;
4024
4025 src = (struct scsi_dif_tuple *)lpfc_cmd->prot_data_segment;
4026 /*
4027 * Used to restore any changes to protection
4028 * data for error injection.
4029 */
4030 switch (lpfc_cmd->prot_data_type) {
4031 case LPFC_INJERR_REFTAG:
4032 src->ref_tag =
4033 lpfc_cmd->prot_data;
4034 break;
4035 case LPFC_INJERR_APPTAG:
4036 src->app_tag =
4037 (uint16_t)lpfc_cmd->prot_data;
4038 break;
4039 case LPFC_INJERR_GUARD:
4040 src->guard_tag =
4041 (uint16_t)lpfc_cmd->prot_data;
4042 break;
4043 default:
4044 break;
4045 }
4046
4047 lpfc_cmd->prot_data = 0;
4048 lpfc_cmd->prot_data_type = 0;
4049 lpfc_cmd->prot_data_segment = NULL;
4050 }
4051#endif
James Smart109f6ed2008-12-04 22:39:08 -05004052 if (pnode && NLP_CHK_NODE_ACT(pnode))
4053 atomic_dec(&pnode->cmd_pending);
dea31012005-04-17 16:05:31 -05004054
4055 if (lpfc_cmd->status) {
4056 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
4057 (lpfc_cmd->result & IOERR_DRVR_MASK))
4058 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
4059 else if (lpfc_cmd->status >= IOSTAT_CNT)
4060 lpfc_cmd->status = IOSTAT_DEFAULT;
James Smartaa1c7ee2012-08-14 14:26:06 -04004061 if (lpfc_cmd->status == IOSTAT_FCP_RSP_ERROR &&
4062 !lpfc_cmd->fcp_rsp->rspStatus3 &&
4063 (lpfc_cmd->fcp_rsp->rspStatus2 & RESID_UNDER) &&
4064 !(vport->cfg_log_verbose & LOG_FCP_UNDER))
James Smart73d91e52011-10-10 21:32:10 -04004065 logit = 0;
4066 else
4067 logit = LOG_FCP | LOG_FCP_UNDER;
4068 lpfc_printf_vlog(vport, KERN_WARNING, logit,
4069 "9030 FCP cmd x%x failed <%d/%d> "
James Smart5a0d80f2012-05-09 21:18:20 -04004070 "status: x%x result: x%x "
4071 "sid: x%x did: x%x oxid: x%x "
4072 "Data: x%x x%x\n",
James Smart73d91e52011-10-10 21:32:10 -04004073 cmd->cmnd[0],
4074 cmd->device ? cmd->device->id : 0xffff,
4075 cmd->device ? cmd->device->lun : 0xffff,
4076 lpfc_cmd->status, lpfc_cmd->result,
James Smart3bf41ba2013-05-31 17:03:18 -04004077 vport->fc_myDID,
4078 (pnode) ? pnode->nlp_DID : 0,
James Smart5a0d80f2012-05-09 21:18:20 -04004079 phba->sli_rev == LPFC_SLI_REV4 ?
4080 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
James Smart73d91e52011-10-10 21:32:10 -04004081 pIocbOut->iocb.ulpContext,
4082 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
dea31012005-04-17 16:05:31 -05004083
4084 switch (lpfc_cmd->status) {
4085 case IOSTAT_FCP_RSP_ERROR:
4086 /* Call FCP RSP handler to determine result */
James Smart2e0fef82007-06-17 19:56:36 -05004087 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea31012005-04-17 16:05:31 -05004088 break;
4089 case IOSTAT_NPORT_BSY:
4090 case IOSTAT_FABRIC_BSY:
James Smart0f1f53a2008-08-24 21:50:18 -04004091 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
James Smartea2151b2008-09-07 11:52:10 -04004092 fast_path_evt = lpfc_alloc_fast_evt(phba);
4093 if (!fast_path_evt)
4094 break;
4095 fast_path_evt->un.fabric_evt.event_type =
4096 FC_REG_FABRIC_EVENT;
4097 fast_path_evt->un.fabric_evt.subcategory =
4098 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
4099 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
4100 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
4101 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
4102 &pnode->nlp_portname,
4103 sizeof(struct lpfc_name));
4104 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
4105 &pnode->nlp_nodename,
4106 sizeof(struct lpfc_name));
4107 }
4108 fast_path_evt->vport = vport;
4109 fast_path_evt->work_evt.evt =
4110 LPFC_EVT_FASTPATH_MGMT_EVT;
4111 spin_lock_irqsave(&phba->hbalock, flags);
4112 list_add_tail(&fast_path_evt->work_evt.evt_listp,
4113 &phba->work_list);
4114 spin_unlock_irqrestore(&phba->hbalock, flags);
4115 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05004116 break;
James Smart92d7f7b2007-06-17 19:56:38 -05004117 case IOSTAT_LOCAL_REJECT:
James Smart1151e3e2011-02-16 12:39:35 -05004118 case IOSTAT_REMOTE_STOP:
James Smartab56dc22011-02-16 12:39:57 -05004119 if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
4120 lpfc_cmd->result ==
4121 IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
4122 lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
4123 lpfc_cmd->result ==
4124 IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
4125 cmd->result = ScsiResult(DID_NO_CONNECT, 0);
4126 break;
4127 }
James Smartd7c255b2008-08-24 21:50:00 -04004128 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
James Smart92d7f7b2007-06-17 19:56:38 -05004129 lpfc_cmd->result == IOERR_NO_RESOURCES ||
James Smartb92938b2010-06-07 15:24:12 -04004130 lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
4131 lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
James Smart92d7f7b2007-06-17 19:56:38 -05004132 cmd->result = ScsiResult(DID_REQUEUE, 0);
James Smart58da1ff2008-04-07 10:15:56 -04004133 break;
James Smarte2a0a9d2008-12-04 22:40:02 -05004134 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004135 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
4136 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
4137 pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
4138 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
4139 /*
4140 * This is a response for a BG enabled
4141 * cmd. Parse BG error
4142 */
4143 lpfc_parse_bg_err(phba, lpfc_cmd,
4144 pIocbOut);
4145 break;
4146 } else {
4147 lpfc_printf_vlog(vport, KERN_WARNING,
4148 LOG_BG,
4149 "9031 non-zero BGSTAT "
James Smart6a9c52c2009-10-02 15:16:51 -04004150 "on unprotected cmd\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05004151 }
4152 }
James Smart1151e3e2011-02-16 12:39:35 -05004153 if ((lpfc_cmd->status == IOSTAT_REMOTE_STOP)
4154 && (phba->sli_rev == LPFC_SLI_REV4)
4155 && (pnode && NLP_CHK_NODE_ACT(pnode))) {
4156 /* This IO was aborted by the target, we don't
4157 * know the rxid and because we did not send the
4158 * ABTS we cannot generate and RRQ.
4159 */
4160 lpfc_set_rrq_active(phba, pnode,
James Smartee0f4fe2012-05-09 21:19:14 -04004161 lpfc_cmd->cur_iocbq.sli4_lxritag,
4162 0, 0);
James Smart1151e3e2011-02-16 12:39:35 -05004163 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004164 /* else: fall through */
dea31012005-04-17 16:05:31 -05004165 default:
4166 cmd->result = ScsiResult(DID_ERROR, 0);
4167 break;
4168 }
4169
James Smart58da1ff2008-04-07 10:15:56 -04004170 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004171 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
James Smart0f1f53a2008-08-24 21:50:18 -04004172 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
4173 SAM_STAT_BUSY);
James Smartab56dc22011-02-16 12:39:57 -05004174 } else
dea31012005-04-17 16:05:31 -05004175 cmd->result = ScsiResult(DID_OK, 0);
dea31012005-04-17 16:05:31 -05004176
4177 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
4178 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
4179
James Smarte8b62012007-08-02 11:10:09 -04004180 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4181 "0710 Iodone <%d/%d> cmd %p, error "
4182 "x%x SNS x%x x%x Data: x%x x%x\n",
4183 cmd->device->id, cmd->device->lun, cmd,
4184 cmd->result, *lp, *(lp + 3), cmd->retries,
4185 scsi_get_resid(cmd));
dea31012005-04-17 16:05:31 -05004186 }
4187
James Smartea2151b2008-09-07 11:52:10 -04004188 lpfc_update_stats(phba, lpfc_cmd);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05004189 result = cmd->result;
James Smart977b5a02008-09-07 11:52:04 -04004190 if (vport->cfg_max_scsicmpl_time &&
4191 time_after(jiffies, lpfc_cmd->start_time +
4192 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
James Smarta257bf92009-04-06 18:48:10 -04004193 spin_lock_irqsave(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05004194 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
4195 if (pnode->cmd_qdepth >
4196 atomic_read(&pnode->cmd_pending) &&
4197 (atomic_read(&pnode->cmd_pending) >
4198 LPFC_MIN_TGT_QDEPTH) &&
4199 ((cmd->cmnd[0] == READ_10) ||
4200 (cmd->cmnd[0] == WRITE_10)))
4201 pnode->cmd_qdepth =
4202 atomic_read(&pnode->cmd_pending);
James Smart977b5a02008-09-07 11:52:04 -04004203
James Smart109f6ed2008-12-04 22:39:08 -05004204 pnode->last_change_time = jiffies;
4205 }
James Smarta257bf92009-04-06 18:48:10 -04004206 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05004207 } else if (pnode && NLP_CHK_NODE_ACT(pnode)) {
James Smart7dc517d2010-07-14 15:32:10 -04004208 if ((pnode->cmd_qdepth < vport->cfg_tgt_queue_depth) &&
James Smart977b5a02008-09-07 11:52:04 -04004209 time_after(jiffies, pnode->last_change_time +
James Smart109f6ed2008-12-04 22:39:08 -05004210 msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
James Smarta257bf92009-04-06 18:48:10 -04004211 spin_lock_irqsave(shost->host_lock, flags);
James Smart7dc517d2010-07-14 15:32:10 -04004212 depth = pnode->cmd_qdepth * LPFC_TGTQ_RAMPUP_PCENT
4213 / 100;
4214 depth = depth ? depth : 1;
4215 pnode->cmd_qdepth += depth;
4216 if (pnode->cmd_qdepth > vport->cfg_tgt_queue_depth)
4217 pnode->cmd_qdepth = vport->cfg_tgt_queue_depth;
James Smart109f6ed2008-12-04 22:39:08 -05004218 pnode->last_change_time = jiffies;
James Smarta257bf92009-04-06 18:48:10 -04004219 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05004220 }
James Smart977b5a02008-09-07 11:52:04 -04004221 }
4222
James Smart1dcb58e2007-04-25 09:51:30 -04004223 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James Smarta257bf92009-04-06 18:48:10 -04004224
4225 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
4226 queue_depth = cmd->device->queue_depth;
4227 scsi_id = cmd->device->id;
dea31012005-04-17 16:05:31 -05004228 cmd->scsi_done(cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004229
Jamie Wellnitzb8086082006-02-28 22:33:12 -05004230 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart876dd7d2012-09-29 11:31:28 -04004231 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92e3af62012-08-14 14:26:28 -04004232 lpfc_cmd->pCmd = NULL;
James Smart876dd7d2012-09-29 11:31:28 -04004233 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart92e3af62012-08-14 14:26:28 -04004234
James Smartfa61a542008-01-11 01:52:42 -05004235 /*
4236 * If there is a thread waiting for command completion
4237 * wake up the thread.
4238 */
James Smarta257bf92009-04-06 18:48:10 -04004239 spin_lock_irqsave(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05004240 if (lpfc_cmd->waitq)
4241 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04004242 spin_unlock_irqrestore(shost->host_lock, flags);
Jamie Wellnitzb8086082006-02-28 22:33:12 -05004243 lpfc_release_scsi_buf(phba, lpfc_cmd);
4244 return;
4245 }
4246
James Smart92d7f7b2007-06-17 19:56:38 -05004247 if (!result)
James Smarta257bf92009-04-06 18:48:10 -04004248 lpfc_rampup_queue_depth(vport, queue_depth);
James Smart92d7f7b2007-06-17 19:56:38 -05004249
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05004250 /*
4251 * Check for queue full. If the lun is reporting queue full, then
4252 * back off the lun queue depth to prevent target overloads.
4253 */
James Smart58da1ff2008-04-07 10:15:56 -04004254 if (result == SAM_STAT_TASK_SET_FULL && pnode &&
4255 NLP_CHK_NODE_ACT(pnode)) {
James Smarta257bf92009-04-06 18:48:10 -04004256 shost_for_each_device(tmp_sdev, shost) {
4257 if (tmp_sdev->id != scsi_id)
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05004258 continue;
4259 depth = scsi_track_queue_full(tmp_sdev,
James Smart5ffc2662009-11-18 15:39:44 -05004260 tmp_sdev->queue_depth-1);
4261 if (depth <= 0)
4262 continue;
James Smarte8b62012007-08-02 11:10:09 -04004263 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4264 "0711 detected queue full - lun queue "
4265 "depth adjusted to %d.\n", depth);
James Smartea2151b2008-09-07 11:52:10 -04004266 lpfc_send_sdev_queuedepth_change_event(phba, vport,
James Smart5ffc2662009-11-18 15:39:44 -05004267 pnode,
4268 tmp_sdev->lun,
4269 depth+1, depth);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05004270 }
4271 }
4272
James Smart876dd7d2012-09-29 11:31:28 -04004273 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92e3af62012-08-14 14:26:28 -04004274 lpfc_cmd->pCmd = NULL;
James Smart876dd7d2012-09-29 11:31:28 -04004275 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart92e3af62012-08-14 14:26:28 -04004276
James Smartfa61a542008-01-11 01:52:42 -05004277 /*
4278 * If there is a thread waiting for command completion
4279 * wake up the thread.
4280 */
James Smarta257bf92009-04-06 18:48:10 -04004281 spin_lock_irqsave(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05004282 if (lpfc_cmd->waitq)
4283 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04004284 spin_unlock_irqrestore(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05004285
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004286 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004287}
4288
James Smart34b02dc2008-08-24 21:49:55 -04004289/**
James Smart3621a712009-04-06 18:47:14 -04004290 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
James Smart34b02dc2008-08-24 21:49:55 -04004291 * @data: A pointer to the immediate command data portion of the IOCB.
4292 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
4293 *
4294 * The routine copies the entire FCP command from @fcp_cmnd to @data while
4295 * byte swapping the data to big endian format for transmission on the wire.
4296 **/
4297static void
4298lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
4299{
4300 int i, j;
4301 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
4302 i += sizeof(uint32_t), j++) {
4303 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
4304 }
4305}
4306
James Smart9bad7672008-12-04 22:39:02 -05004307/**
James Smartf1126682009-06-10 17:22:44 -04004308 * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05004309 * @vport: The virtual port for which this call is being executed.
4310 * @lpfc_cmd: The scsi command which needs to send.
4311 * @pnode: Pointer to lpfc_nodelist.
4312 *
4313 * This routine initializes fcp_cmnd and iocb data structure from scsi command
James Smart3772a992009-05-22 14:50:54 -04004314 * to transfer for device with SLI3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05004315 **/
dea31012005-04-17 16:05:31 -05004316static void
James Smartf1126682009-06-10 17:22:44 -04004317lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
James Smart2e0fef82007-06-17 19:56:36 -05004318 struct lpfc_nodelist *pnode)
dea31012005-04-17 16:05:31 -05004319{
James Smart2e0fef82007-06-17 19:56:36 -05004320 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004321 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
4322 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
4323 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
4324 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
4325 int datadir = scsi_cmnd->sc_data_direction;
James Smart7e2b19f2007-10-29 11:00:39 -04004326 char tag[2];
James Smart027140e2012-08-03 12:35:44 -04004327 uint8_t *ptr;
4328 bool sli4;
dea31012005-04-17 16:05:31 -05004329
James Smart58da1ff2008-04-07 10:15:56 -04004330 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
4331 return;
4332
dea31012005-04-17 16:05:31 -05004333 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
James.Smart@Emulex.Com69859dc2005-08-10 15:02:37 -04004334 /* clear task management bits */
4335 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea31012005-04-17 16:05:31 -05004336
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -04004337 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
4338 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05004339
James Smart027140e2012-08-03 12:35:44 -04004340 ptr = &fcp_cmnd->fcpCdb[0];
4341 memcpy(ptr, scsi_cmnd->cmnd, scsi_cmnd->cmd_len);
4342 if (scsi_cmnd->cmd_len < LPFC_FCP_CDB_LEN) {
4343 ptr += scsi_cmnd->cmd_len;
4344 memset(ptr, 0, (LPFC_FCP_CDB_LEN - scsi_cmnd->cmd_len));
4345 }
4346
James Smart7e2b19f2007-10-29 11:00:39 -04004347 if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
4348 switch (tag[0]) {
dea31012005-04-17 16:05:31 -05004349 case HEAD_OF_QUEUE_TAG:
4350 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
4351 break;
4352 case ORDERED_QUEUE_TAG:
4353 fcp_cmnd->fcpCntl1 = ORDERED_Q;
4354 break;
4355 default:
4356 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
4357 break;
4358 }
4359 } else
James Smartfe8f7f92013-01-03 15:43:03 -05004360 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
dea31012005-04-17 16:05:31 -05004361
James Smart027140e2012-08-03 12:35:44 -04004362 sli4 = (phba->sli_rev == LPFC_SLI_REV4);
4363
dea31012005-04-17 16:05:31 -05004364 /*
4365 * There are three possibilities here - use scatter-gather segment, use
4366 * the single mapping, or neither. Start the lpfc command prep by
4367 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
4368 * data bde entry.
4369 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05004370 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05004371 if (datadir == DMA_TO_DEVICE) {
4372 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
James Smart027140e2012-08-03 12:35:44 -04004373 if (sli4)
4374 iocb_cmd->ulpPU = PARM_READ_CHECK;
4375 else {
James Smart3772a992009-05-22 14:50:54 -04004376 iocb_cmd->un.fcpi.fcpi_parm = 0;
4377 iocb_cmd->ulpPU = 0;
James Smart027140e2012-08-03 12:35:44 -04004378 }
dea31012005-04-17 16:05:31 -05004379 fcp_cmnd->fcpCntl3 = WRITE_DATA;
4380 phba->fc4OutputRequests++;
4381 } else {
4382 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
4383 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05004384 fcp_cmnd->fcpCntl3 = READ_DATA;
4385 phba->fc4InputRequests++;
4386 }
4387 } else {
4388 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
4389 iocb_cmd->un.fcpi.fcpi_parm = 0;
4390 iocb_cmd->ulpPU = 0;
4391 fcp_cmnd->fcpCntl3 = 0;
4392 phba->fc4ControlRequests++;
4393 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004394 if (phba->sli_rev == 3 &&
4395 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04004396 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05004397 /*
4398 * Finish initializing those IOCB fields that are independent
4399 * of the scsi_cmnd request_buffer
4400 */
4401 piocbq->iocb.ulpContext = pnode->nlp_rpi;
James Smart027140e2012-08-03 12:35:44 -04004402 if (sli4)
James Smart6d368e52011-05-24 11:44:12 -04004403 piocbq->iocb.ulpContext =
4404 phba->sli4_hba.rpi_ids[pnode->nlp_rpi];
dea31012005-04-17 16:05:31 -05004405 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
4406 piocbq->iocb.ulpFCP2Rcvy = 1;
James Smart09372822008-01-11 01:52:54 -05004407 else
4408 piocbq->iocb.ulpFCP2Rcvy = 0;
dea31012005-04-17 16:05:31 -05004409
4410 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
4411 piocbq->context1 = lpfc_cmd;
4412 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
4413 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
James Smart2e0fef82007-06-17 19:56:36 -05004414 piocbq->vport = vport;
dea31012005-04-17 16:05:31 -05004415}
4416
James Smart9bad7672008-12-04 22:39:02 -05004417/**
James Smart6d368e52011-05-24 11:44:12 -04004418 * lpfc_scsi_prep_task_mgmt_cmd - Convert SLI3 scsi TM cmd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05004419 * @vport: The virtual port for which this call is being executed.
4420 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
4421 * @lun: Logical unit number.
4422 * @task_mgmt_cmd: SCSI task management command.
4423 *
James Smart3772a992009-05-22 14:50:54 -04004424 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
4425 * for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05004426 *
4427 * Return codes:
4428 * 0 - Error
4429 * 1 - Success
4430 **/
dea31012005-04-17 16:05:31 -05004431static int
James Smartf1126682009-06-10 17:22:44 -04004432lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
dea31012005-04-17 16:05:31 -05004433 struct lpfc_scsi_buf *lpfc_cmd,
James Smart420b630d2006-07-06 15:50:16 -04004434 unsigned int lun,
dea31012005-04-17 16:05:31 -05004435 uint8_t task_mgmt_cmd)
4436{
dea31012005-04-17 16:05:31 -05004437 struct lpfc_iocbq *piocbq;
4438 IOCB_t *piocb;
4439 struct fcp_cmnd *fcp_cmnd;
James Smart0b18ac42006-05-01 21:50:40 -04004440 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea31012005-04-17 16:05:31 -05004441 struct lpfc_nodelist *ndlp = rdata->pnode;
4442
James Smart58da1ff2008-04-07 10:15:56 -04004443 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
4444 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05004445 return 0;
dea31012005-04-17 16:05:31 -05004446
dea31012005-04-17 16:05:31 -05004447 piocbq = &(lpfc_cmd->cur_iocbq);
James Smart2e0fef82007-06-17 19:56:36 -05004448 piocbq->vport = vport;
4449
dea31012005-04-17 16:05:31 -05004450 piocb = &piocbq->iocb;
4451
4452 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart34b02dc2008-08-24 21:49:55 -04004453 /* Clear out any old data in the FCP command area */
4454 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
4455 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05004456 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
James Smarte2a0a9d2008-12-04 22:40:02 -05004457 if (vport->phba->sli_rev == 3 &&
4458 !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04004459 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05004460 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
dea31012005-04-17 16:05:31 -05004461 piocb->ulpContext = ndlp->nlp_rpi;
James Smart6d368e52011-05-24 11:44:12 -04004462 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
4463 piocb->ulpContext =
4464 vport->phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
4465 }
dea31012005-04-17 16:05:31 -05004466 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
4467 piocb->ulpFCP2Rcvy = 1;
4468 }
4469 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
4470
4471 /* ulpTimeout is only one byte */
4472 if (lpfc_cmd->timeout > 0xff) {
4473 /*
4474 * Do not timeout the command at the firmware level.
4475 * The driver will provide the timeout mechanism.
4476 */
4477 piocb->ulpTimeout = 0;
James Smartf1126682009-06-10 17:22:44 -04004478 } else
dea31012005-04-17 16:05:31 -05004479 piocb->ulpTimeout = lpfc_cmd->timeout;
James Smartf1126682009-06-10 17:22:44 -04004480
4481 if (vport->phba->sli_rev == LPFC_SLI_REV4)
4482 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004483
James Smart2e0fef82007-06-17 19:56:36 -05004484 return 1;
dea31012005-04-17 16:05:31 -05004485}
4486
James Smart9bad7672008-12-04 22:39:02 -05004487/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004488 * lpfc_scsi_api_table_setup - Set up scsi api function jump table
James Smart3772a992009-05-22 14:50:54 -04004489 * @phba: The hba struct for which this call is being executed.
4490 * @dev_grp: The HBA PCI-Device group number.
4491 *
4492 * This routine sets up the SCSI interface API function jump table in @phba
4493 * struct.
4494 * Returns: 0 - success, -ENODEV - failure.
4495 **/
4496int
4497lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4498{
4499
James Smartf1126682009-06-10 17:22:44 -04004500 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
4501 phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
James Smartf1126682009-06-10 17:22:44 -04004502
James Smart3772a992009-05-22 14:50:54 -04004503 switch (dev_grp) {
4504 case LPFC_PCI_DEV_LP:
4505 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s3;
4506 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
James Smartacd68592012-01-18 16:25:09 -05004507 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04004508 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
James Smart19ca7602010-11-20 23:11:55 -05004509 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04004510 break;
James Smartda0436e2009-05-22 14:51:39 -04004511 case LPFC_PCI_DEV_OC:
4512 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s4;
4513 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
James Smartacd68592012-01-18 16:25:09 -05004514 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04004515 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
James Smart19ca7602010-11-20 23:11:55 -05004516 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04004517 break;
James Smart3772a992009-05-22 14:50:54 -04004518 default:
4519 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4520 "1418 Invalid HBA PCI-device group: 0x%x\n",
4521 dev_grp);
4522 return -ENODEV;
4523 break;
4524 }
James Smart3772a992009-05-22 14:50:54 -04004525 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
James Smart84d1b002010-02-12 14:42:33 -05004526 phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
James Smart3772a992009-05-22 14:50:54 -04004527 return 0;
4528}
4529
4530/**
James Smart3621a712009-04-06 18:47:14 -04004531 * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
James Smart9bad7672008-12-04 22:39:02 -05004532 * @phba: The Hba for which this call is being executed.
4533 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
4534 * @rspiocbq: Pointer to lpfc_iocbq data structure.
4535 *
4536 * This routine is IOCB completion routine for device reset and target reset
4537 * routine. This routine release scsi buffer associated with lpfc_cmd.
4538 **/
James Smart7054a602007-04-25 09:52:34 -04004539static void
4540lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
4541 struct lpfc_iocbq *cmdiocbq,
4542 struct lpfc_iocbq *rspiocbq)
4543{
4544 struct lpfc_scsi_buf *lpfc_cmd =
4545 (struct lpfc_scsi_buf *) cmdiocbq->context1;
4546 if (lpfc_cmd)
4547 lpfc_release_scsi_buf(phba, lpfc_cmd);
4548 return;
4549}
4550
James Smart9bad7672008-12-04 22:39:02 -05004551/**
James Smart3621a712009-04-06 18:47:14 -04004552 * lpfc_info - Info entry point of scsi_host_template data structure
James Smart9bad7672008-12-04 22:39:02 -05004553 * @host: The scsi host for which this call is being executed.
4554 *
4555 * This routine provides module information about hba.
4556 *
4557 * Reutrn code:
4558 * Pointer to char - Success.
4559 **/
dea31012005-04-17 16:05:31 -05004560const char *
4561lpfc_info(struct Scsi_Host *host)
4562{
James Smart2e0fef82007-06-17 19:56:36 -05004563 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
4564 struct lpfc_hba *phba = vport->phba;
James Smart8b68cd52012-09-29 11:32:37 -04004565 int len, link_speed = 0;
dea31012005-04-17 16:05:31 -05004566 static char lpfcinfobuf[384];
4567
4568 memset(lpfcinfobuf,0,384);
4569 if (phba && phba->pcidev){
4570 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
4571 len = strlen(lpfcinfobuf);
4572 snprintf(lpfcinfobuf + len,
4573 384-len,
4574 " on PCI bus %02x device %02x irq %d",
4575 phba->pcidev->bus->number,
4576 phba->pcidev->devfn,
4577 phba->pcidev->irq);
4578 len = strlen(lpfcinfobuf);
4579 if (phba->Port[0]) {
4580 snprintf(lpfcinfobuf + len,
4581 384-len,
4582 " port %s",
4583 phba->Port);
4584 }
James Smart65467b62010-01-26 23:08:29 -05004585 len = strlen(lpfcinfobuf);
James Smart8b68cd52012-09-29 11:32:37 -04004586 if (phba->sli_rev <= LPFC_SLI_REV3) {
4587 link_speed = lpfc_sli_port_speed_get(phba);
4588 } else {
4589 if (phba->sli4_hba.link_state.logical_speed)
4590 link_speed =
4591 phba->sli4_hba.link_state.logical_speed;
4592 else
4593 link_speed = phba->sli4_hba.link_state.speed;
James Smart65467b62010-01-26 23:08:29 -05004594 }
James Smart8b68cd52012-09-29 11:32:37 -04004595 if (link_speed != 0)
4596 snprintf(lpfcinfobuf + len, 384-len,
4597 " Logical Link Speed: %d Mbps", link_speed);
dea31012005-04-17 16:05:31 -05004598 }
4599 return lpfcinfobuf;
4600}
4601
James Smart9bad7672008-12-04 22:39:02 -05004602/**
James Smart3621a712009-04-06 18:47:14 -04004603 * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
James Smart9bad7672008-12-04 22:39:02 -05004604 * @phba: The Hba for which this call is being executed.
4605 *
4606 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
4607 * The default value of cfg_poll_tmo is 10 milliseconds.
4608 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004609static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
4610{
4611 unsigned long poll_tmo_expires =
4612 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
4613
James Smart0e9bb8d2013-03-01 16:35:12 -05004614 if (!list_empty(&phba->sli.ring[LPFC_FCP_RING].txcmplq))
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004615 mod_timer(&phba->fcp_poll_timer,
4616 poll_tmo_expires);
4617}
4618
James Smart9bad7672008-12-04 22:39:02 -05004619/**
James Smart3621a712009-04-06 18:47:14 -04004620 * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
James Smart9bad7672008-12-04 22:39:02 -05004621 * @phba: The Hba for which this call is being executed.
4622 *
4623 * This routine starts the fcp_poll_timer of @phba.
4624 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004625void lpfc_poll_start_timer(struct lpfc_hba * phba)
4626{
4627 lpfc_poll_rearm_timer(phba);
4628}
4629
James Smart9bad7672008-12-04 22:39:02 -05004630/**
James Smart3621a712009-04-06 18:47:14 -04004631 * lpfc_poll_timeout - Restart polling timer
James Smart9bad7672008-12-04 22:39:02 -05004632 * @ptr: Map to lpfc_hba data structure pointer.
4633 *
4634 * This routine restarts fcp_poll timer, when FCP ring polling is enable
4635 * and FCP Ring interrupt is disable.
4636 **/
4637
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004638void lpfc_poll_timeout(unsigned long ptr)
4639{
James Smart2e0fef82007-06-17 19:56:36 -05004640 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004641
4642 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004643 lpfc_sli_handle_fast_ring_event(phba,
4644 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4645
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004646 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4647 lpfc_poll_rearm_timer(phba);
4648 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004649}
4650
James Smart9bad7672008-12-04 22:39:02 -05004651/**
James Smart3621a712009-04-06 18:47:14 -04004652 * lpfc_queuecommand - scsi_host_template queuecommand entry point
James Smart9bad7672008-12-04 22:39:02 -05004653 * @cmnd: Pointer to scsi_cmnd data structure.
4654 * @done: Pointer to done routine.
4655 *
4656 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
4657 * This routine prepares an IOCB from scsi command and provides to firmware.
4658 * The @done callback is invoked after driver finished processing the command.
4659 *
4660 * Return value :
4661 * 0 - Success
4662 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
4663 **/
dea31012005-04-17 16:05:31 -05004664static int
James Smartb9a7c632012-08-03 12:35:24 -04004665lpfc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004666{
James Smart2e0fef82007-06-17 19:56:36 -05004667 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4668 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004669 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05004670 struct lpfc_nodelist *ndlp;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004671 struct lpfc_scsi_buf *lpfc_cmd;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004672 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004673 int err;
dea31012005-04-17 16:05:31 -05004674
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004675 err = fc_remote_port_chkready(rport);
4676 if (err) {
4677 cmnd->result = err;
dea31012005-04-17 16:05:31 -05004678 goto out_fail_command;
4679 }
James Smart1c6f4ef52009-11-18 15:40:49 -05004680 ndlp = rdata->pnode;
dea31012005-04-17 16:05:31 -05004681
James Smartbf086112011-08-21 21:48:13 -04004682 if ((scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) &&
James Smartacd68592012-01-18 16:25:09 -05004683 (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED))) {
James Smarte2a0a9d2008-12-04 22:40:02 -05004684
James Smart6a9c52c2009-10-02 15:16:51 -04004685 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4686 "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
4687 " op:%02x str=%s without registering for"
4688 " BlockGuard - Rejecting command\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05004689 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
4690 dif_op_str[scsi_get_prot_op(cmnd)]);
4691 goto out_fail_command;
4692 }
4693
dea31012005-04-17 16:05:31 -05004694 /*
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004695 * Catch race where our node has transitioned, but the
4696 * transport is still transitioning.
dea31012005-04-17 16:05:31 -05004697 */
James Smart6b415f52012-06-12 13:54:59 -04004698 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
4699 goto out_tgt_busy;
James Smart7dc517d2010-07-14 15:32:10 -04004700 if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth)
Mike Christie34963432011-02-25 14:04:28 -06004701 goto out_tgt_busy;
Mike Christiea93ce022008-08-17 15:24:41 -05004702
James Smart19ca7602010-11-20 23:11:55 -05004703 lpfc_cmd = lpfc_get_scsi_buf(phba, ndlp);
dea31012005-04-17 16:05:31 -05004704 if (lpfc_cmd == NULL) {
James Smarteaf15d52008-12-04 22:39:29 -05004705 lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05004706
James Smarte8b62012007-08-02 11:10:09 -04004707 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4708 "0707 driver's buffer pool is empty, "
4709 "IO busied\n");
dea31012005-04-17 16:05:31 -05004710 goto out_host_busy;
4711 }
4712
4713 /*
4714 * Store the midlayer's command structure for the completion phase
4715 * and complete the command initialization.
4716 */
4717 lpfc_cmd->pCmd = cmnd;
4718 lpfc_cmd->rdata = rdata;
4719 lpfc_cmd->timeout = 0;
James Smart977b5a02008-09-07 11:52:04 -04004720 lpfc_cmd->start_time = jiffies;
dea31012005-04-17 16:05:31 -05004721 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
dea31012005-04-17 16:05:31 -05004722
James Smarte2a0a9d2008-12-04 22:40:02 -05004723 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
James Smart6a9c52c2009-10-02 15:16:51 -04004724 if (vport->phba->cfg_enable_bg) {
James Smart737d4242013-04-17 20:14:49 -04004725 lpfc_printf_vlog(vport,
4726 KERN_INFO, LOG_SCSI_CMD,
James Smart26134702012-08-14 14:25:50 -04004727 "9033 BLKGRD: rcvd %s cmd:x%x "
4728 "sector x%llx cnt %u pt %x\n",
4729 dif_op_str[scsi_get_prot_op(cmnd)],
4730 cmnd->cmnd[0],
4731 (unsigned long long)scsi_get_lba(cmnd),
4732 blk_rq_sectors(cmnd->request),
4733 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004734 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004735 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
4736 } else {
James Smart6a9c52c2009-10-02 15:16:51 -04004737 if (vport->phba->cfg_enable_bg) {
James Smart737d4242013-04-17 20:14:49 -04004738 lpfc_printf_vlog(vport,
4739 KERN_INFO, LOG_SCSI_CMD,
James Smart26134702012-08-14 14:25:50 -04004740 "9038 BLKGRD: rcvd PROT_NORMAL cmd: "
4741 "x%x sector x%llx cnt %u pt %x\n",
4742 cmnd->cmnd[0],
4743 (unsigned long long)scsi_get_lba(cmnd),
James Smart9a6b09c2012-03-01 22:37:42 -05004744 blk_rq_sectors(cmnd->request),
James Smart26134702012-08-14 14:25:50 -04004745 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004746 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004747 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
4748 }
4749
dea31012005-04-17 16:05:31 -05004750 if (err)
4751 goto out_host_busy_free_buf;
4752
James Smart2e0fef82007-06-17 19:56:36 -05004753 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea31012005-04-17 16:05:31 -05004754
James Smart977b5a02008-09-07 11:52:04 -04004755 atomic_inc(&ndlp->cmd_pending);
James Smart3772a992009-05-22 14:50:54 -04004756 err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
James Smart92d7f7b2007-06-17 19:56:38 -05004757 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
James Smarteaf15d52008-12-04 22:39:29 -05004758 if (err) {
4759 atomic_dec(&ndlp->cmd_pending);
dea31012005-04-17 16:05:31 -05004760 goto out_host_busy_free_buf;
James Smarteaf15d52008-12-04 22:39:29 -05004761 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004762 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004763 lpfc_sli_handle_fast_ring_event(phba,
4764 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4765
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004766 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4767 lpfc_poll_rearm_timer(phba);
4768 }
4769
dea31012005-04-17 16:05:31 -05004770 return 0;
4771
4772 out_host_busy_free_buf:
James Smartbcf4dbf2006-07-06 15:50:08 -04004773 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004774 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004775 out_host_busy:
4776 return SCSI_MLQUEUE_HOST_BUSY;
4777
Mike Christie34963432011-02-25 14:04:28 -06004778 out_tgt_busy:
4779 return SCSI_MLQUEUE_TARGET_BUSY;
4780
dea31012005-04-17 16:05:31 -05004781 out_fail_command:
James Smartb9a7c632012-08-03 12:35:24 -04004782 cmnd->scsi_done(cmnd);
dea31012005-04-17 16:05:31 -05004783 return 0;
4784}
4785
Jeff Garzikf2812332010-11-16 02:10:29 -05004786
James Smart9bad7672008-12-04 22:39:02 -05004787/**
James Smart3621a712009-04-06 18:47:14 -04004788 * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05004789 * @cmnd: Pointer to scsi_cmnd data structure.
4790 *
4791 * This routine aborts @cmnd pending in base driver.
4792 *
4793 * Return code :
4794 * 0x2003 - Error
4795 * 0x2002 - Success
4796 **/
dea31012005-04-17 16:05:31 -05004797static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05004798lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004799{
James Smart2e0fef82007-06-17 19:56:36 -05004800 struct Scsi_Host *shost = cmnd->device->host;
4801 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4802 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004803 struct lpfc_iocbq *iocb;
4804 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05004805 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05004806 IOCB_t *cmd, *icmd;
James Smart3a707302012-06-12 13:54:42 -04004807 int ret = SUCCESS, status = 0;
James Smart876dd7d2012-09-29 11:31:28 -04004808 unsigned long flags;
James Smartfa61a542008-01-11 01:52:42 -05004809 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004810
James Smart3a707302012-06-12 13:54:42 -04004811 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04004812 if (status != 0 && status != SUCCESS)
James Smart3a707302012-06-12 13:54:42 -04004813 return status;
James Smart4f2e66c2012-05-09 21:17:07 -04004814
James Smart876dd7d2012-09-29 11:31:28 -04004815 spin_lock_irqsave(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004816 /* driver queued commands are in process of being flushed */
4817 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
James Smart876dd7d2012-09-29 11:31:28 -04004818 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004819 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4820 "3168 SCSI Layer abort requested I/O has been "
4821 "flushed by LLD.\n");
4822 return FAILED;
4823 }
4824
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004825 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
James Smart92e3af62012-08-14 14:26:28 -04004826 if (!lpfc_cmd || !lpfc_cmd->pCmd) {
James Smart876dd7d2012-09-29 11:31:28 -04004827 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarteee88772010-09-29 11:19:08 -04004828 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4829 "2873 SCSI Layer I/O Abort Request IO CMPL Status "
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04004830 "x%x ID %d LUN %d\n",
James Smart3a707302012-06-12 13:54:42 -04004831 SUCCESS, cmnd->device->id, cmnd->device->lun);
James Smarteee88772010-09-29 11:19:08 -04004832 return SUCCESS;
4833 }
dea31012005-04-17 16:05:31 -05004834
James Smart4f2e66c2012-05-09 21:17:07 -04004835 iocb = &lpfc_cmd->cur_iocbq;
4836 /* the command is in process of being cancelled */
4837 if (!(iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
James Smart876dd7d2012-09-29 11:31:28 -04004838 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004839 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4840 "3169 SCSI Layer abort requested I/O has been "
4841 "cancelled by LLD.\n");
4842 return FAILED;
4843 }
dea31012005-04-17 16:05:31 -05004844 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004845 * If pCmd field of the corresponding lpfc_scsi_buf structure
4846 * points to a different SCSI command, then the driver has
4847 * already completed this command, but the midlayer did not
James Smart4f2e66c2012-05-09 21:17:07 -04004848 * see the completion before the eh fired. Just return SUCCESS.
dea31012005-04-17 16:05:31 -05004849 */
James Smart4f2e66c2012-05-09 21:17:07 -04004850 if (lpfc_cmd->pCmd != cmnd) {
4851 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4852 "3170 SCSI Layer abort requested I/O has been "
4853 "completed by LLD.\n");
4854 goto out_unlock;
4855 }
dea31012005-04-17 16:05:31 -05004856
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004857 BUG_ON(iocb->context1 != lpfc_cmd);
dea31012005-04-17 16:05:31 -05004858
James Smart4f2e66c2012-05-09 21:17:07 -04004859 abtsiocb = __lpfc_sli_get_iocbq(phba);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004860 if (abtsiocb == NULL) {
4861 ret = FAILED;
James Smart4f2e66c2012-05-09 21:17:07 -04004862 goto out_unlock;
dea31012005-04-17 16:05:31 -05004863 }
4864
dea31012005-04-17 16:05:31 -05004865 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004866 * The scsi command can not be in txq and it is in flight because the
4867 * pCmd is still pointig at the SCSI command we have to abort. There
4868 * is no need to search the txcmplq. Just send an abort to the FW.
dea31012005-04-17 16:05:31 -05004869 */
dea31012005-04-17 16:05:31 -05004870
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004871 cmd = &iocb->iocb;
4872 icmd = &abtsiocb->iocb;
4873 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
4874 icmd->un.acxri.abortContextTag = cmd->ulpContext;
James Smart3772a992009-05-22 14:50:54 -04004875 if (phba->sli_rev == LPFC_SLI_REV4)
4876 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
4877 else
4878 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05004879
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004880 icmd->ulpLe = 1;
4881 icmd->ulpClass = cmd->ulpClass;
James Smart5ffc2662009-11-18 15:39:44 -05004882
4883 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
4884 abtsiocb->fcp_wqidx = iocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05004885 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05004886
James Smart2e0fef82007-06-17 19:56:36 -05004887 if (lpfc_is_link_up(phba))
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004888 icmd->ulpCommand = CMD_ABORT_XRI_CN;
4889 else
4890 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea31012005-04-17 16:05:31 -05004891
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004892 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smart2e0fef82007-06-17 19:56:36 -05004893 abtsiocb->vport = vport;
James Smart4f2e66c2012-05-09 21:17:07 -04004894 /* no longer need the lock after this point */
James Smart876dd7d2012-09-29 11:31:28 -04004895 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004896
James Smart3772a992009-05-22 14:50:54 -04004897 if (lpfc_sli_issue_iocb(phba, LPFC_FCP_RING, abtsiocb, 0) ==
4898 IOCB_ERROR) {
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004899 lpfc_sli_release_iocbq(phba, abtsiocb);
4900 ret = FAILED;
4901 goto out;
4902 }
4903
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004904 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
James Smart45ed1192009-10-02 15:17:02 -04004905 lpfc_sli_handle_fast_ring_event(phba,
4906 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004907
James Smartfa61a542008-01-11 01:52:42 -05004908 lpfc_cmd->waitq = &waitq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004909 /* Wait for abort to complete */
James Smartfa61a542008-01-11 01:52:42 -05004910 wait_event_timeout(waitq,
4911 (lpfc_cmd->pCmd != cmnd),
James Smart256ec0d2013-04-17 20:14:58 -04004912 msecs_to_jiffies(2*vport->cfg_devloss_tmo*1000));
James Smartfa61a542008-01-11 01:52:42 -05004913 lpfc_cmd->waitq = NULL;
dea31012005-04-17 16:05:31 -05004914
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004915 if (lpfc_cmd->pCmd == cmnd) {
4916 ret = FAILED;
James Smarte8b62012007-08-02 11:10:09 -04004917 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4918 "0748 abort handler timed out waiting "
James Smart247ca942012-08-14 14:26:13 -04004919 "for abortng I/O (xri:x%x) to complete: "
4920 "ret %#x, ID %d, LUN %d\n",
4921 iocb->sli4_xritag, ret,
4922 cmnd->device->id, cmnd->device->lun);
dea31012005-04-17 16:05:31 -05004923 }
James Smart4f2e66c2012-05-09 21:17:07 -04004924 goto out;
dea31012005-04-17 16:05:31 -05004925
James Smart4f2e66c2012-05-09 21:17:07 -04004926out_unlock:
James Smart876dd7d2012-09-29 11:31:28 -04004927 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004928out:
James Smarte8b62012007-08-02 11:10:09 -04004929 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4930 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04004931 "LUN %d\n", ret, cmnd->device->id,
4932 cmnd->device->lun);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004933 return ret;
dea31012005-04-17 16:05:31 -05004934}
4935
James Smartbbb9d182009-06-10 17:23:16 -04004936static char *
4937lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
4938{
4939 switch (task_mgmt_cmd) {
4940 case FCP_ABORT_TASK_SET:
4941 return "ABORT_TASK_SET";
4942 case FCP_CLEAR_TASK_SET:
4943 return "FCP_CLEAR_TASK_SET";
4944 case FCP_BUS_RESET:
4945 return "FCP_BUS_RESET";
4946 case FCP_LUN_RESET:
4947 return "FCP_LUN_RESET";
4948 case FCP_TARGET_RESET:
4949 return "FCP_TARGET_RESET";
4950 case FCP_CLEAR_ACA:
4951 return "FCP_CLEAR_ACA";
4952 case FCP_TERMINATE_TASK:
4953 return "FCP_TERMINATE_TASK";
4954 default:
4955 return "unknown";
4956 }
4957}
4958
4959/**
4960 * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
4961 * @vport: The virtual port for which this call is being executed.
4962 * @rdata: Pointer to remote port local data
4963 * @tgt_id: Target ID of remote device.
4964 * @lun_id: Lun number for the TMF
4965 * @task_mgmt_cmd: type of TMF to send
4966 *
4967 * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
4968 * a remote port.
4969 *
4970 * Return Code:
4971 * 0x2003 - Error
4972 * 0x2002 - Success.
4973 **/
4974static int
4975lpfc_send_taskmgmt(struct lpfc_vport *vport, struct lpfc_rport_data *rdata,
4976 unsigned tgt_id, unsigned int lun_id,
4977 uint8_t task_mgmt_cmd)
4978{
4979 struct lpfc_hba *phba = vport->phba;
4980 struct lpfc_scsi_buf *lpfc_cmd;
4981 struct lpfc_iocbq *iocbq;
4982 struct lpfc_iocbq *iocbqrsp;
James Smart5989b8d2010-10-22 11:06:56 -04004983 struct lpfc_nodelist *pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004984 int ret;
4985 int status;
4986
James Smart5989b8d2010-10-22 11:06:56 -04004987 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
James Smartbbb9d182009-06-10 17:23:16 -04004988 return FAILED;
4989
James Smart19ca7602010-11-20 23:11:55 -05004990 lpfc_cmd = lpfc_get_scsi_buf(phba, rdata->pnode);
James Smartbbb9d182009-06-10 17:23:16 -04004991 if (lpfc_cmd == NULL)
4992 return FAILED;
4993 lpfc_cmd->timeout = 60;
4994 lpfc_cmd->rdata = rdata;
4995
4996 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
4997 task_mgmt_cmd);
4998 if (!status) {
4999 lpfc_release_scsi_buf(phba, lpfc_cmd);
5000 return FAILED;
5001 }
5002
5003 iocbq = &lpfc_cmd->cur_iocbq;
5004 iocbqrsp = lpfc_sli_get_iocbq(phba);
5005 if (iocbqrsp == NULL) {
5006 lpfc_release_scsi_buf(phba, lpfc_cmd);
5007 return FAILED;
5008 }
5009
5010 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5011 "0702 Issue %s to TGT %d LUN %d "
James Smart6d368e52011-05-24 11:44:12 -04005012 "rpi x%x nlp_flag x%x Data: x%x x%x\n",
James Smartbbb9d182009-06-10 17:23:16 -04005013 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
James Smart6d368e52011-05-24 11:44:12 -04005014 pnode->nlp_rpi, pnode->nlp_flag, iocbq->sli4_xritag,
5015 iocbq->iocb_flag);
James Smartbbb9d182009-06-10 17:23:16 -04005016
5017 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
5018 iocbq, iocbqrsp, lpfc_cmd->timeout);
5019 if (status != IOCB_SUCCESS) {
5020 if (status == IOCB_TIMEDOUT) {
5021 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
5022 ret = TIMEOUT_ERROR;
5023 } else
5024 ret = FAILED;
5025 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
5026 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smart6d368e52011-05-24 11:44:12 -04005027 "0727 TMF %s to TGT %d LUN %d failed (%d, %d) "
5028 "iocb_flag x%x\n",
James Smartbbb9d182009-06-10 17:23:16 -04005029 lpfc_taskmgmt_name(task_mgmt_cmd),
5030 tgt_id, lun_id, iocbqrsp->iocb.ulpStatus,
James Smart6d368e52011-05-24 11:44:12 -04005031 iocbqrsp->iocb.un.ulpWord[4],
5032 iocbq->iocb_flag);
James Smart2a9bf3d2010-06-07 15:24:45 -04005033 } else if (status == IOCB_BUSY)
5034 ret = FAILED;
5035 else
James Smartbbb9d182009-06-10 17:23:16 -04005036 ret = SUCCESS;
5037
5038 lpfc_sli_release_iocbq(phba, iocbqrsp);
5039
5040 if (ret != TIMEOUT_ERROR)
5041 lpfc_release_scsi_buf(phba, lpfc_cmd);
5042
5043 return ret;
5044}
5045
5046/**
5047 * lpfc_chk_tgt_mapped -
5048 * @vport: The virtual port to check on
5049 * @cmnd: Pointer to scsi_cmnd data structure.
5050 *
5051 * This routine delays until the scsi target (aka rport) for the
5052 * command exists (is present and logged in) or we declare it non-existent.
5053 *
5054 * Return code :
5055 * 0x2003 - Error
5056 * 0x2002 - Success
5057 **/
5058static int
5059lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
5060{
5061 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005062 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005063 unsigned long later;
5064
James Smart1c6f4ef52009-11-18 15:40:49 -05005065 if (!rdata) {
5066 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5067 "0797 Tgt Map rport failure: rdata x%p\n", rdata);
5068 return FAILED;
5069 }
5070 pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005071 /*
5072 * If target is not in a MAPPED state, delay until
5073 * target is rediscovered or devloss timeout expires.
5074 */
5075 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
5076 while (time_after(later, jiffies)) {
5077 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
5078 return FAILED;
5079 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
5080 return SUCCESS;
5081 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
5082 rdata = cmnd->device->hostdata;
5083 if (!rdata)
5084 return FAILED;
5085 pnode = rdata->pnode;
5086 }
5087 if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
5088 (pnode->nlp_state != NLP_STE_MAPPED_NODE))
5089 return FAILED;
5090 return SUCCESS;
5091}
5092
5093/**
5094 * lpfc_reset_flush_io_context -
5095 * @vport: The virtual port (scsi_host) for the flush context
5096 * @tgt_id: If aborting by Target contect - specifies the target id
5097 * @lun_id: If aborting by Lun context - specifies the lun id
5098 * @context: specifies the context level to flush at.
5099 *
5100 * After a reset condition via TMF, we need to flush orphaned i/o
5101 * contexts from the adapter. This routine aborts any contexts
5102 * outstanding, then waits for their completions. The wait is
5103 * bounded by devloss_tmo though.
5104 *
5105 * Return code :
5106 * 0x2003 - Error
5107 * 0x2002 - Success
5108 **/
5109static int
5110lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
5111 uint64_t lun_id, lpfc_ctx_cmd context)
5112{
5113 struct lpfc_hba *phba = vport->phba;
5114 unsigned long later;
5115 int cnt;
5116
5117 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
5118 if (cnt)
5119 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
5120 tgt_id, lun_id, context);
5121 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
5122 while (time_after(later, jiffies) && cnt) {
5123 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
5124 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
5125 }
5126 if (cnt) {
5127 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5128 "0724 I/O flush failure for context %s : cnt x%x\n",
5129 ((context == LPFC_CTX_LUN) ? "LUN" :
5130 ((context == LPFC_CTX_TGT) ? "TGT" :
5131 ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
5132 cnt);
5133 return FAILED;
5134 }
5135 return SUCCESS;
5136}
5137
James Smart9bad7672008-12-04 22:39:02 -05005138/**
James Smart3621a712009-04-06 18:47:14 -04005139 * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
James Smart9bad7672008-12-04 22:39:02 -05005140 * @cmnd: Pointer to scsi_cmnd data structure.
5141 *
James Smartbbb9d182009-06-10 17:23:16 -04005142 * This routine does a device reset by sending a LUN_RESET task management
James Smart9bad7672008-12-04 22:39:02 -05005143 * command.
5144 *
5145 * Return code :
5146 * 0x2003 - Error
James Smart3621a712009-04-06 18:47:14 -04005147 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05005148 **/
dea31012005-04-17 16:05:31 -05005149static int
James Smart7054a602007-04-25 09:52:34 -04005150lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05005151{
James Smart2e0fef82007-06-17 19:56:36 -05005152 struct Scsi_Host *shost = cmnd->device->host;
5153 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05005154 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005155 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005156 unsigned tgt_id = cmnd->device->id;
5157 unsigned int lun_id = cmnd->device->lun;
James Smartea2151b2008-09-07 11:52:10 -04005158 struct lpfc_scsi_event_header scsi_event;
James Smart3a707302012-06-12 13:54:42 -04005159 int status, ret = SUCCESS;
dea31012005-04-17 16:05:31 -05005160
James Smart1c6f4ef52009-11-18 15:40:49 -05005161 if (!rdata) {
5162 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5163 "0798 Device Reset rport failure: rdata x%p\n", rdata);
5164 return FAILED;
5165 }
5166 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04005167 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005168 if (status != 0 && status != SUCCESS)
James Smart589a52d2010-07-14 15:30:54 -04005169 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005170
5171 status = lpfc_chk_tgt_mapped(vport, cmnd);
5172 if (status == FAILED) {
5173 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5174 "0721 Device Reset rport failure: rdata x%p\n", rdata);
5175 return FAILED;
5176 }
5177
5178 scsi_event.event_type = FC_REG_SCSI_EVENT;
5179 scsi_event.subcategory = LPFC_EVENT_LUNRESET;
5180 scsi_event.lun = lun_id;
5181 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5182 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5183
5184 fc_host_post_vendor_event(shost, fc_get_event_number(),
5185 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
5186
5187 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
5188 FCP_LUN_RESET);
5189
5190 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5191 "0713 SCSI layer issued Device Reset (%d, %d) "
5192 "return x%x\n", tgt_id, lun_id, status);
5193
dea31012005-04-17 16:05:31 -05005194 /*
James Smartbbb9d182009-06-10 17:23:16 -04005195 * We have to clean up i/o as : they may be orphaned by the TMF;
5196 * or if the TMF failed, they may be in an indeterminate state.
5197 * So, continue on.
5198 * We will report success if all the i/o aborts successfully.
dea31012005-04-17 16:05:31 -05005199 */
James Smart3a707302012-06-12 13:54:42 -04005200 ret = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
James Smartbbb9d182009-06-10 17:23:16 -04005201 LPFC_CTX_LUN);
James Smart3a707302012-06-12 13:54:42 -04005202 return ret;
James Smartbbb9d182009-06-10 17:23:16 -04005203}
5204
5205/**
5206 * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
5207 * @cmnd: Pointer to scsi_cmnd data structure.
5208 *
5209 * This routine does a target reset by sending a TARGET_RESET task management
5210 * command.
5211 *
5212 * Return code :
5213 * 0x2003 - Error
5214 * 0x2002 - Success
5215 **/
5216static int
5217lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
5218{
5219 struct Scsi_Host *shost = cmnd->device->host;
5220 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5221 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005222 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005223 unsigned tgt_id = cmnd->device->id;
5224 unsigned int lun_id = cmnd->device->lun;
5225 struct lpfc_scsi_event_header scsi_event;
James Smart3a707302012-06-12 13:54:42 -04005226 int status, ret = SUCCESS;
James Smartbbb9d182009-06-10 17:23:16 -04005227
James Smart1c6f4ef52009-11-18 15:40:49 -05005228 if (!rdata) {
5229 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5230 "0799 Target Reset rport failure: rdata x%p\n", rdata);
5231 return FAILED;
5232 }
5233 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04005234 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005235 if (status != 0 && status != SUCCESS)
James Smart589a52d2010-07-14 15:30:54 -04005236 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005237
5238 status = lpfc_chk_tgt_mapped(vport, cmnd);
5239 if (status == FAILED) {
5240 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5241 "0722 Target Reset rport failure: rdata x%p\n", rdata);
5242 return FAILED;
dea31012005-04-17 16:05:31 -05005243 }
James Smartea2151b2008-09-07 11:52:10 -04005244
5245 scsi_event.event_type = FC_REG_SCSI_EVENT;
5246 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
5247 scsi_event.lun = 0;
5248 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5249 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5250
James Smartbbb9d182009-06-10 17:23:16 -04005251 fc_host_post_vendor_event(shost, fc_get_event_number(),
5252 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -04005253
James Smartbbb9d182009-06-10 17:23:16 -04005254 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
5255 FCP_TARGET_RESET);
dea31012005-04-17 16:05:31 -05005256
James Smarte8b62012007-08-02 11:10:09 -04005257 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smartbbb9d182009-06-10 17:23:16 -04005258 "0723 SCSI layer issued Target Reset (%d, %d) "
5259 "return x%x\n", tgt_id, lun_id, status);
5260
5261 /*
5262 * We have to clean up i/o as : they may be orphaned by the TMF;
5263 * or if the TMF failed, they may be in an indeterminate state.
5264 * So, continue on.
5265 * We will report success if all the i/o aborts successfully.
5266 */
James Smart3a707302012-06-12 13:54:42 -04005267 ret = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
5268 LPFC_CTX_TGT);
5269 return ret;
dea31012005-04-17 16:05:31 -05005270}
5271
James Smart9bad7672008-12-04 22:39:02 -05005272/**
James Smart3621a712009-04-06 18:47:14 -04005273 * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05005274 * @cmnd: Pointer to scsi_cmnd data structure.
5275 *
James Smartbbb9d182009-06-10 17:23:16 -04005276 * This routine does target reset to all targets on @cmnd->device->host.
5277 * This emulates Parallel SCSI Bus Reset Semantics.
James Smart9bad7672008-12-04 22:39:02 -05005278 *
James Smartbbb9d182009-06-10 17:23:16 -04005279 * Return code :
5280 * 0x2003 - Error
5281 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05005282 **/
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04005283static int
James Smart7054a602007-04-25 09:52:34 -04005284lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05005285{
James Smart2e0fef82007-06-17 19:56:36 -05005286 struct Scsi_Host *shost = cmnd->device->host;
5287 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05005288 struct lpfc_nodelist *ndlp = NULL;
James Smartea2151b2008-09-07 11:52:10 -04005289 struct lpfc_scsi_event_header scsi_event;
James Smartbbb9d182009-06-10 17:23:16 -04005290 int match;
5291 int ret = SUCCESS, status, i;
James Smartea2151b2008-09-07 11:52:10 -04005292
5293 scsi_event.event_type = FC_REG_SCSI_EVENT;
5294 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
5295 scsi_event.lun = 0;
5296 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
5297 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
5298
James Smartbbb9d182009-06-10 17:23:16 -04005299 fc_host_post_vendor_event(shost, fc_get_event_number(),
5300 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
dea31012005-04-17 16:05:31 -05005301
James Smartbf086112011-08-21 21:48:13 -04005302 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005303 if (status != 0 && status != SUCCESS)
James Smartbf086112011-08-21 21:48:13 -04005304 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005305
dea31012005-04-17 16:05:31 -05005306 /*
5307 * Since the driver manages a single bus device, reset all
5308 * targets known to the driver. Should any target reset
5309 * fail, this routine returns failure to the midlayer.
5310 */
James Smarte17da182006-07-06 15:49:25 -04005311 for (i = 0; i < LPFC_MAX_TARGET; i++) {
James Smart685f0bf2007-04-25 09:53:08 -04005312 /* Search for mapped node by target ID */
dea31012005-04-17 16:05:31 -05005313 match = 0;
James Smart2e0fef82007-06-17 19:56:36 -05005314 spin_lock_irq(shost->host_lock);
5315 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05005316 if (!NLP_CHK_NODE_ACT(ndlp))
5317 continue;
James Smarta6571c62012-10-31 14:44:42 -04005318 if (vport->phba->cfg_fcp2_no_tgt_reset &&
5319 (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE))
5320 continue;
James Smart685f0bf2007-04-25 09:53:08 -04005321 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
James Smart915caaa2008-06-14 22:52:38 -04005322 ndlp->nlp_sid == i &&
James Smart685f0bf2007-04-25 09:53:08 -04005323 ndlp->rport) {
dea31012005-04-17 16:05:31 -05005324 match = 1;
5325 break;
5326 }
5327 }
James Smart2e0fef82007-06-17 19:56:36 -05005328 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005329 if (!match)
5330 continue;
James Smartbbb9d182009-06-10 17:23:16 -04005331
5332 status = lpfc_send_taskmgmt(vport, ndlp->rport->dd_data,
5333 i, 0, FCP_TARGET_RESET);
5334
5335 if (status != SUCCESS) {
James Smarte8b62012007-08-02 11:10:09 -04005336 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5337 "0700 Bus Reset on target %d failed\n",
5338 i);
James Smart915caaa2008-06-14 22:52:38 -04005339 ret = FAILED;
dea31012005-04-17 16:05:31 -05005340 }
5341 }
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005342 /*
James Smartbbb9d182009-06-10 17:23:16 -04005343 * We have to clean up i/o as : they may be orphaned by the TMFs
5344 * above; or if any of the TMFs failed, they may be in an
5345 * indeterminate state.
5346 * We will report success if all the i/o aborts successfully.
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005347 */
James Smartbbb9d182009-06-10 17:23:16 -04005348
5349 status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
5350 if (status != SUCCESS)
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005351 ret = FAILED;
James Smartbbb9d182009-06-10 17:23:16 -04005352
James Smarte8b62012007-08-02 11:10:09 -04005353 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5354 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea31012005-04-17 16:05:31 -05005355 return ret;
5356}
5357
James Smart9bad7672008-12-04 22:39:02 -05005358/**
James Smart27b01b82012-05-09 21:19:44 -04005359 * lpfc_host_reset_handler - scsi_host_template eh_host_reset_handler entry pt
5360 * @cmnd: Pointer to scsi_cmnd data structure.
5361 *
5362 * This routine does host reset to the adaptor port. It brings the HBA
5363 * offline, performs a board restart, and then brings the board back online.
5364 * The lpfc_offline calls lpfc_sli_hba_down which will abort and local
5365 * reject all outstanding SCSI commands to the host and error returned
5366 * back to SCSI mid-level. As this will be SCSI mid-level's last resort
5367 * of error handling, it will only return error if resetting of the adapter
5368 * is not successful; in all other cases, will return success.
5369 *
5370 * Return code :
5371 * 0x2003 - Error
5372 * 0x2002 - Success
5373 **/
5374static int
5375lpfc_host_reset_handler(struct scsi_cmnd *cmnd)
5376{
5377 struct Scsi_Host *shost = cmnd->device->host;
5378 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5379 struct lpfc_hba *phba = vport->phba;
5380 int rc, ret = SUCCESS;
5381
James Smarta88dbb62013-04-17 20:18:39 -04005382 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5383 "3172 SCSI layer issued Host Reset Data:\n");
5384
James Smart618a5232012-06-12 13:54:36 -04005385 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart27b01b82012-05-09 21:19:44 -04005386 lpfc_offline(phba);
5387 rc = lpfc_sli_brdrestart(phba);
5388 if (rc)
5389 ret = FAILED;
James Smarta88dbb62013-04-17 20:18:39 -04005390 rc = lpfc_online(phba);
5391 if (rc)
5392 ret = FAILED;
James Smart27b01b82012-05-09 21:19:44 -04005393 lpfc_unblock_mgmt_io(phba);
5394
James Smarta88dbb62013-04-17 20:18:39 -04005395 if (ret == FAILED) {
5396 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5397 "3323 Failed host reset, bring it offline\n");
5398 lpfc_sli4_offline_eratt(phba);
5399 }
James Smart27b01b82012-05-09 21:19:44 -04005400 return ret;
5401}
5402
5403/**
James Smart3621a712009-04-06 18:47:14 -04005404 * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
James Smart9bad7672008-12-04 22:39:02 -05005405 * @sdev: Pointer to scsi_device.
5406 *
5407 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
5408 * globally available list of scsi buffers. This routine also makes sure scsi
5409 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
5410 * of scsi buffer exists for the lifetime of the driver.
5411 *
5412 * Return codes:
5413 * non-0 - Error
5414 * 0 - Success
5415 **/
dea31012005-04-17 16:05:31 -05005416static int
dea31012005-04-17 16:05:31 -05005417lpfc_slave_alloc(struct scsi_device *sdev)
5418{
James Smart2e0fef82007-06-17 19:56:36 -05005419 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5420 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005421 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
James Smart3772a992009-05-22 14:50:54 -04005422 uint32_t total = 0;
dea31012005-04-17 16:05:31 -05005423 uint32_t num_to_alloc = 0;
James Smart3772a992009-05-22 14:50:54 -04005424 int num_allocated = 0;
James Smartd7c47992010-06-08 18:31:54 -04005425 uint32_t sdev_cnt;
dea31012005-04-17 16:05:31 -05005426
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005427 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05005428 return -ENXIO;
5429
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005430 sdev->hostdata = rport->dd_data;
James Smartd7c47992010-06-08 18:31:54 -04005431 sdev_cnt = atomic_inc_return(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05005432
5433 /*
5434 * Populate the cmds_per_lun count scsi_bufs into this host's globally
5435 * available list of scsi buffers. Don't allocate more than the
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04005436 * HBA limit conveyed to the midlayer via the host structure. The
5437 * formula accounts for the lun_queue_depth + error handlers + 1
5438 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea31012005-04-17 16:05:31 -05005439 */
5440 total = phba->total_scsi_bufs;
James Smart3de2a652007-08-02 11:09:59 -04005441 num_to_alloc = vport->cfg_lun_queue_depth + 2;
James Smart92d7f7b2007-06-17 19:56:38 -05005442
James Smartd7c47992010-06-08 18:31:54 -04005443 /* If allocated buffers are enough do nothing */
5444 if ((sdev_cnt * (vport->cfg_lun_queue_depth + 2)) < total)
5445 return 0;
5446
James Smart92d7f7b2007-06-17 19:56:38 -05005447 /* Allow some exchanges to be available always to complete discovery */
5448 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04005449 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5450 "0704 At limitation of %d preallocated "
5451 "command buffers\n", total);
dea31012005-04-17 16:05:31 -05005452 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05005453 /* Allow some exchanges to be available always to complete discovery */
5454 } else if (total + num_to_alloc >
5455 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04005456 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5457 "0705 Allocation request of %d "
5458 "command buffers will exceed max of %d. "
5459 "Reducing allocation request to %d.\n",
5460 num_to_alloc, phba->cfg_hba_queue_depth,
5461 (phba->cfg_hba_queue_depth - total));
dea31012005-04-17 16:05:31 -05005462 num_to_alloc = phba->cfg_hba_queue_depth - total;
5463 }
James Smart3772a992009-05-22 14:50:54 -04005464 num_allocated = lpfc_new_scsi_buf(vport, num_to_alloc);
5465 if (num_to_alloc != num_allocated) {
James Smart96f70772013-04-17 20:16:15 -04005466 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5467 "0708 Allocation request of %d "
5468 "command buffers did not succeed. "
5469 "Allocated %d buffers.\n",
5470 num_to_alloc, num_allocated);
dea31012005-04-17 16:05:31 -05005471 }
James Smart1c6f4ef52009-11-18 15:40:49 -05005472 if (num_allocated > 0)
5473 phba->total_scsi_bufs += num_allocated;
dea31012005-04-17 16:05:31 -05005474 return 0;
5475}
5476
James Smart9bad7672008-12-04 22:39:02 -05005477/**
James Smart3621a712009-04-06 18:47:14 -04005478 * lpfc_slave_configure - scsi_host_template slave_configure entry point
James Smart9bad7672008-12-04 22:39:02 -05005479 * @sdev: Pointer to scsi_device.
5480 *
5481 * This routine configures following items
5482 * - Tag command queuing support for @sdev if supported.
James Smart9bad7672008-12-04 22:39:02 -05005483 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
5484 *
5485 * Return codes:
5486 * 0 - Success
5487 **/
dea31012005-04-17 16:05:31 -05005488static int
5489lpfc_slave_configure(struct scsi_device *sdev)
5490{
James Smart2e0fef82007-06-17 19:56:36 -05005491 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5492 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05005493
5494 if (sdev->tagged_supported)
James Smart3de2a652007-08-02 11:09:59 -04005495 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05005496 else
James Smart3de2a652007-08-02 11:09:59 -04005497 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05005498
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005499 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04005500 lpfc_sli_handle_fast_ring_event(phba,
5501 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005502 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
5503 lpfc_poll_rearm_timer(phba);
5504 }
5505
dea31012005-04-17 16:05:31 -05005506 return 0;
5507}
5508
James Smart9bad7672008-12-04 22:39:02 -05005509/**
James Smart3621a712009-04-06 18:47:14 -04005510 * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
James Smart9bad7672008-12-04 22:39:02 -05005511 * @sdev: Pointer to scsi_device.
5512 *
5513 * This routine sets @sdev hostatdata filed to null.
5514 **/
dea31012005-04-17 16:05:31 -05005515static void
5516lpfc_slave_destroy(struct scsi_device *sdev)
5517{
James Smartd7c47992010-06-08 18:31:54 -04005518 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5519 struct lpfc_hba *phba = vport->phba;
5520 atomic_dec(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05005521 sdev->hostdata = NULL;
5522 return;
5523}
5524
James Smart92d7f7b2007-06-17 19:56:38 -05005525
dea31012005-04-17 16:05:31 -05005526struct scsi_host_template lpfc_template = {
5527 .module = THIS_MODULE,
5528 .name = LPFC_DRIVER_NAME,
5529 .info = lpfc_info,
5530 .queuecommand = lpfc_queuecommand,
5531 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04005532 .eh_device_reset_handler = lpfc_device_reset_handler,
5533 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart7054a602007-04-25 09:52:34 -04005534 .eh_bus_reset_handler = lpfc_bus_reset_handler,
James Smart27b01b82012-05-09 21:19:44 -04005535 .eh_host_reset_handler = lpfc_host_reset_handler,
dea31012005-04-17 16:05:31 -05005536 .slave_alloc = lpfc_slave_alloc,
5537 .slave_configure = lpfc_slave_configure,
5538 .slave_destroy = lpfc_slave_destroy,
James Smart47a86172007-04-25 09:53:22 -04005539 .scan_finished = lpfc_scan_finished,
dea31012005-04-17 16:05:31 -05005540 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05005541 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
dea31012005-04-17 16:05:31 -05005542 .cmd_per_lun = LPFC_CMD_PER_LUN,
5543 .use_clustering = ENABLE_CLUSTERING,
James Smart2e0fef82007-06-17 19:56:36 -05005544 .shost_attrs = lpfc_hba_attrs,
James.Smart@Emulex.Com564b2962005-06-25 10:34:17 -04005545 .max_sectors = 0xFFFF,
James Smartf1c3b0f2009-07-19 10:01:32 -04005546 .vendor_id = LPFC_NL_VENDOR_ID,
James Smart5ffc2662009-11-18 15:39:44 -05005547 .change_queue_depth = lpfc_change_queue_depth,
James Smartfe8f7f92013-01-03 15:43:03 -05005548 .change_queue_type = lpfc_change_queue_type,
dea31012005-04-17 16:05:31 -05005549};
James Smart3de2a652007-08-02 11:09:59 -04005550
5551struct scsi_host_template lpfc_vport_template = {
5552 .module = THIS_MODULE,
5553 .name = LPFC_DRIVER_NAME,
5554 .info = lpfc_info,
5555 .queuecommand = lpfc_queuecommand,
5556 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04005557 .eh_device_reset_handler = lpfc_device_reset_handler,
5558 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart3de2a652007-08-02 11:09:59 -04005559 .eh_bus_reset_handler = lpfc_bus_reset_handler,
5560 .slave_alloc = lpfc_slave_alloc,
5561 .slave_configure = lpfc_slave_configure,
5562 .slave_destroy = lpfc_slave_destroy,
5563 .scan_finished = lpfc_scan_finished,
5564 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05005565 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
James Smart3de2a652007-08-02 11:09:59 -04005566 .cmd_per_lun = LPFC_CMD_PER_LUN,
5567 .use_clustering = ENABLE_CLUSTERING,
5568 .shost_attrs = lpfc_vport_attrs,
5569 .max_sectors = 0xFFFF,
James Smart5ffc2662009-11-18 15:39:44 -05005570 .change_queue_depth = lpfc_change_queue_depth,
James Smartfe8f7f92013-01-03 15:43:03 -05005571 .change_queue_type = lpfc_change_queue_type,
James Smart3de2a652007-08-02 11:09:59 -04005572};