blob: bb9a2249b75d3df9a8c075f0fdaabb4348292864 [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>
dea31012005-04-17 16:05:31 -050027
28#include <scsi/scsi.h>
29#include <scsi/scsi_device.h>
James Smarte2a0a9d2008-12-04 22:40:02 -050030#include <scsi/scsi_eh.h>
dea31012005-04-17 16:05:31 -050031#include <scsi/scsi_host.h>
32#include <scsi/scsi_tcq.h>
33#include <scsi/scsi_transport_fc.h>
34
35#include "lpfc_version.h"
James Smartda0436e2009-05-22 14:51:39 -040036#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050037#include "lpfc_hw.h"
38#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040039#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040040#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050041#include "lpfc_disc.h"
dea31012005-04-17 16:05:31 -050042#include "lpfc.h"
James Smart9a6b09c2012-03-01 22:37:42 -050043#include "lpfc_scsi.h"
dea31012005-04-17 16:05:31 -050044#include "lpfc_logmsg.h"
45#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050046#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050047
48#define LPFC_RESET_WAIT 2
49#define LPFC_ABORT_WAIT 2
50
James Smarte2a0a9d2008-12-04 22:40:02 -050051int _dump_buf_done;
52
53static char *dif_op_str[] = {
James Smart9a6b09c2012-03-01 22:37:42 -050054 "PROT_NORMAL",
55 "PROT_READ_INSERT",
56 "PROT_WRITE_STRIP",
57 "PROT_READ_STRIP",
58 "PROT_WRITE_INSERT",
59 "PROT_READ_PASS",
60 "PROT_WRITE_PASS",
61};
62
63static char *dif_grd_str[] = {
64 "NO_GUARD",
65 "DIF_CRC",
66 "DIX_IP",
James Smarte2a0a9d2008-12-04 22:40:02 -050067};
James Smartf9bb2da2011-10-10 21:34:11 -040068
69struct scsi_dif_tuple {
70 __be16 guard_tag; /* Checksum */
71 __be16 app_tag; /* Opaque storage */
72 __be32 ref_tag; /* Target LBA or indirect LBA */
73};
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 Smart3621a712009-04-06 18:47:14 -0400297 * lpfc_rampdown_queue_depth - Post RAMP_DOWN_QUEUE event to worker thread
James Smart9bad7672008-12-04 22:39:02 -0500298 * @phba: The Hba for which this call is being executed.
299 *
300 * This routine is called when there is resource error in driver or firmware.
301 * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
302 * posts at most 1 event each second. This routine wakes up worker thread of
303 * @phba to process WORKER_RAM_DOWN_EVENT event.
304 *
305 * This routine should be called with no lock held.
306 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500307void
James Smarteaf15d52008-12-04 22:39:29 -0500308lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
James Smart92d7f7b2007-06-17 19:56:38 -0500309{
310 unsigned long flags;
James Smart5e9d9b82008-06-14 22:52:53 -0400311 uint32_t evt_posted;
James Smart92d7f7b2007-06-17 19:56:38 -0500312
313 spin_lock_irqsave(&phba->hbalock, flags);
314 atomic_inc(&phba->num_rsrc_err);
315 phba->last_rsrc_error_time = jiffies;
316
317 if ((phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL) > jiffies) {
318 spin_unlock_irqrestore(&phba->hbalock, flags);
319 return;
320 }
321
322 phba->last_ramp_down_time = jiffies;
323
324 spin_unlock_irqrestore(&phba->hbalock, flags);
325
326 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400327 evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
328 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500329 phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500330 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
331
James Smart5e9d9b82008-06-14 22:52:53 -0400332 if (!evt_posted)
333 lpfc_worker_wake_up(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500334 return;
335}
336
James Smart9bad7672008-12-04 22:39:02 -0500337/**
James Smart3621a712009-04-06 18:47:14 -0400338 * lpfc_rampup_queue_depth - Post RAMP_UP_QUEUE event for worker thread
James Smart9bad7672008-12-04 22:39:02 -0500339 * @phba: The Hba for which this call is being executed.
340 *
341 * This routine post WORKER_RAMP_UP_QUEUE event for @phba vport. This routine
342 * post at most 1 event every 5 minute after last_ramp_up_time or
343 * last_rsrc_error_time. This routine wakes up worker thread of @phba
344 * to process WORKER_RAM_DOWN_EVENT event.
345 *
346 * This routine should be called with no lock held.
347 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500348static inline void
James Smart3de2a652007-08-02 11:09:59 -0400349lpfc_rampup_queue_depth(struct lpfc_vport *vport,
James Smarta257bf92009-04-06 18:48:10 -0400350 uint32_t queue_depth)
James Smart92d7f7b2007-06-17 19:56:38 -0500351{
352 unsigned long flags;
James Smart3de2a652007-08-02 11:09:59 -0400353 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -0400354 uint32_t evt_posted;
James Smart92d7f7b2007-06-17 19:56:38 -0500355 atomic_inc(&phba->num_cmd_success);
356
James Smarta257bf92009-04-06 18:48:10 -0400357 if (vport->cfg_lun_queue_depth <= queue_depth)
James Smart92d7f7b2007-06-17 19:56:38 -0500358 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500359 spin_lock_irqsave(&phba->hbalock, flags);
James Smart5ffc2662009-11-18 15:39:44 -0500360 if (time_before(jiffies,
361 phba->last_ramp_up_time + QUEUE_RAMP_UP_INTERVAL) ||
362 time_before(jiffies,
363 phba->last_rsrc_error_time + QUEUE_RAMP_UP_INTERVAL)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500364 spin_unlock_irqrestore(&phba->hbalock, flags);
365 return;
366 }
James Smart92d7f7b2007-06-17 19:56:38 -0500367 phba->last_ramp_up_time = jiffies;
368 spin_unlock_irqrestore(&phba->hbalock, flags);
369
370 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400371 evt_posted = phba->pport->work_port_events & WORKER_RAMP_UP_QUEUE;
372 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500373 phba->pport->work_port_events |= WORKER_RAMP_UP_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500374 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
375
James Smart5e9d9b82008-06-14 22:52:53 -0400376 if (!evt_posted)
377 lpfc_worker_wake_up(phba);
378 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500379}
380
James Smart9bad7672008-12-04 22:39:02 -0500381/**
James Smart3621a712009-04-06 18:47:14 -0400382 * lpfc_ramp_down_queue_handler - WORKER_RAMP_DOWN_QUEUE event handler
James Smart9bad7672008-12-04 22:39:02 -0500383 * @phba: The Hba for which this call is being executed.
384 *
385 * This routine is called to process WORKER_RAMP_DOWN_QUEUE event for worker
386 * thread.This routine reduces queue depth for all scsi device on each vport
387 * associated with @phba.
388 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500389void
390lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
391{
James Smart549e55c2007-08-02 11:09:51 -0400392 struct lpfc_vport **vports;
393 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500394 struct scsi_device *sdev;
James Smart5ffc2662009-11-18 15:39:44 -0500395 unsigned long new_queue_depth;
James Smart92d7f7b2007-06-17 19:56:38 -0500396 unsigned long num_rsrc_err, num_cmd_success;
James Smart549e55c2007-08-02 11:09:51 -0400397 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500398
399 num_rsrc_err = atomic_read(&phba->num_rsrc_err);
400 num_cmd_success = atomic_read(&phba->num_cmd_success);
401
James Smart75ad83a2012-05-09 21:18:40 -0400402 /*
403 * The error and success command counters are global per
404 * driver instance. If another handler has already
405 * operated on this error event, just exit.
406 */
407 if (num_rsrc_err == 0)
408 return;
409
James Smart549e55c2007-08-02 11:09:51 -0400410 vports = lpfc_create_vport_work_array(phba);
411 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400412 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400413 shost = lpfc_shost_from_vport(vports[i]);
414 shost_for_each_device(sdev, shost) {
James Smart92d7f7b2007-06-17 19:56:38 -0500415 new_queue_depth =
James Smart549e55c2007-08-02 11:09:51 -0400416 sdev->queue_depth * num_rsrc_err /
417 (num_rsrc_err + num_cmd_success);
418 if (!new_queue_depth)
419 new_queue_depth = sdev->queue_depth - 1;
420 else
421 new_queue_depth = sdev->queue_depth -
422 new_queue_depth;
James Smart5ffc2662009-11-18 15:39:44 -0500423 lpfc_change_queue_depth(sdev, new_queue_depth,
424 SCSI_QDEPTH_DEFAULT);
James Smart549e55c2007-08-02 11:09:51 -0400425 }
James Smart92d7f7b2007-06-17 19:56:38 -0500426 }
James Smart09372822008-01-11 01:52:54 -0500427 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500428 atomic_set(&phba->num_rsrc_err, 0);
429 atomic_set(&phba->num_cmd_success, 0);
430}
431
James Smart9bad7672008-12-04 22:39:02 -0500432/**
James Smart3621a712009-04-06 18:47:14 -0400433 * lpfc_ramp_up_queue_handler - WORKER_RAMP_UP_QUEUE event handler
James Smart9bad7672008-12-04 22:39:02 -0500434 * @phba: The Hba for which this call is being executed.
435 *
436 * This routine is called to process WORKER_RAMP_UP_QUEUE event for worker
437 * thread.This routine increases queue depth for all scsi device on each vport
438 * associated with @phba by 1. This routine also sets @phba num_rsrc_err and
439 * num_cmd_success to zero.
440 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500441void
442lpfc_ramp_up_queue_handler(struct lpfc_hba *phba)
443{
James Smart549e55c2007-08-02 11:09:51 -0400444 struct lpfc_vport **vports;
445 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500446 struct scsi_device *sdev;
James Smart549e55c2007-08-02 11:09:51 -0400447 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500448
James Smart549e55c2007-08-02 11:09:51 -0400449 vports = lpfc_create_vport_work_array(phba);
450 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400451 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400452 shost = lpfc_shost_from_vport(vports[i]);
453 shost_for_each_device(sdev, shost) {
James Smart97eab632008-04-07 10:16:05 -0400454 if (vports[i]->cfg_lun_queue_depth <=
455 sdev->queue_depth)
456 continue;
James Smart5ffc2662009-11-18 15:39:44 -0500457 lpfc_change_queue_depth(sdev,
458 sdev->queue_depth+1,
459 SCSI_QDEPTH_RAMP_UP);
James Smart549e55c2007-08-02 11:09:51 -0400460 }
James Smart92d7f7b2007-06-17 19:56:38 -0500461 }
James Smart09372822008-01-11 01:52:54 -0500462 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500463 atomic_set(&phba->num_rsrc_err, 0);
464 atomic_set(&phba->num_cmd_success, 0);
465}
466
James Smarta8e497d2008-08-24 21:50:11 -0400467/**
James Smart3621a712009-04-06 18:47:14 -0400468 * lpfc_scsi_dev_block - set all scsi hosts to block state
James Smarta8e497d2008-08-24 21:50:11 -0400469 * @phba: Pointer to HBA context object.
470 *
471 * This function walks vport list and set each SCSI host to block state
472 * by invoking fc_remote_port_delete() routine. This function is invoked
473 * with EEH when device's PCI slot has been permanently disabled.
474 **/
475void
476lpfc_scsi_dev_block(struct lpfc_hba *phba)
477{
478 struct lpfc_vport **vports;
479 struct Scsi_Host *shost;
480 struct scsi_device *sdev;
481 struct fc_rport *rport;
482 int i;
483
484 vports = lpfc_create_vport_work_array(phba);
485 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400486 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8e497d2008-08-24 21:50:11 -0400487 shost = lpfc_shost_from_vport(vports[i]);
488 shost_for_each_device(sdev, shost) {
489 rport = starget_to_rport(scsi_target(sdev));
490 fc_remote_port_delete(rport);
491 }
492 }
493 lpfc_destroy_vport_work_array(phba, vports);
494}
495
James Smart9bad7672008-12-04 22:39:02 -0500496/**
James Smart3772a992009-05-22 14:50:54 -0400497 * lpfc_new_scsi_buf_s3 - Scsi buffer allocator for HBA with SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -0500498 * @vport: The virtual port for which this call being executed.
James Smart3772a992009-05-22 14:50:54 -0400499 * @num_to_allocate: The requested number of buffers to allocate.
James Smart9bad7672008-12-04 22:39:02 -0500500 *
James Smart3772a992009-05-22 14:50:54 -0400501 * This routine allocates a scsi buffer for device with SLI-3 interface spec,
502 * the scsi buffer contains all the necessary information needed to initiate
503 * a SCSI I/O. The non-DMAable buffer region contains information to build
504 * the IOCB. The DMAable region contains memory for the FCP CMND, FCP RSP,
505 * and the initial BPL. In addition to allocating memory, the FCP CMND and
506 * FCP RSP BDEs are setup in the BPL and the BPL BDE is setup in the IOCB.
James Smart9bad7672008-12-04 22:39:02 -0500507 *
508 * Return codes:
James Smart3772a992009-05-22 14:50:54 -0400509 * int - number of scsi buffers that were allocated.
510 * 0 = failure, less than num_to_alloc is a partial failure.
James Smart9bad7672008-12-04 22:39:02 -0500511 **/
James Smart3772a992009-05-22 14:50:54 -0400512static int
513lpfc_new_scsi_buf_s3(struct lpfc_vport *vport, int num_to_alloc)
dea31012005-04-17 16:05:31 -0500514{
James Smart2e0fef82007-06-17 19:56:36 -0500515 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500516 struct lpfc_scsi_buf *psb;
517 struct ulp_bde64 *bpl;
518 IOCB_t *iocb;
James Smart34b02dc2008-08-24 21:49:55 -0400519 dma_addr_t pdma_phys_fcp_cmd;
520 dma_addr_t pdma_phys_fcp_rsp;
521 dma_addr_t pdma_phys_bpl;
James Bottomley604a3e32005-10-29 10:28:33 -0500522 uint16_t iotag;
James Smart3772a992009-05-22 14:50:54 -0400523 int bcnt;
dea31012005-04-17 16:05:31 -0500524
James Smart3772a992009-05-22 14:50:54 -0400525 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
526 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
527 if (!psb)
528 break;
dea31012005-04-17 16:05:31 -0500529
James Smart3772a992009-05-22 14:50:54 -0400530 /*
531 * Get memory from the pci pool to map the virt space to pci
532 * bus space for an I/O. The DMA buffer includes space for the
533 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
534 * necessary to support the sg_tablesize.
535 */
536 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
537 GFP_KERNEL, &psb->dma_handle);
538 if (!psb->data) {
539 kfree(psb);
540 break;
541 }
dea31012005-04-17 16:05:31 -0500542
James Smart3772a992009-05-22 14:50:54 -0400543 /* Initialize virtual ptrs to dma_buf region. */
544 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
dea31012005-04-17 16:05:31 -0500545
James Smart3772a992009-05-22 14:50:54 -0400546 /* Allocate iotag for psb->cur_iocbq. */
547 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
548 if (iotag == 0) {
549 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
550 psb->data, psb->dma_handle);
551 kfree(psb);
552 break;
553 }
554 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Bottomley604a3e32005-10-29 10:28:33 -0500555
James Smart3772a992009-05-22 14:50:54 -0400556 psb->fcp_cmnd = psb->data;
557 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
558 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
James Smart34b02dc2008-08-24 21:49:55 -0400559 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500560
James Smart3772a992009-05-22 14:50:54 -0400561 /* Initialize local short-hand pointers. */
562 bpl = psb->fcp_bpl;
563 pdma_phys_fcp_cmd = psb->dma_handle;
564 pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
565 pdma_phys_bpl = psb->dma_handle + sizeof(struct fcp_cmnd) +
566 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500567
James Smart3772a992009-05-22 14:50:54 -0400568 /*
569 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
570 * are sg list bdes. Initialize the first two and leave the
571 * rest for queuecommand.
572 */
573 bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
574 bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
575 bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
576 bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
577 bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
dea31012005-04-17 16:05:31 -0500578
James Smart3772a992009-05-22 14:50:54 -0400579 /* Setup the physical region for the FCP RSP */
580 bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
581 bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
582 bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
583 bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
584 bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
585
586 /*
587 * Since the IOCB for the FCP I/O is built into this
588 * lpfc_scsi_buf, initialize it with all known data now.
589 */
590 iocb = &psb->cur_iocbq.iocb;
591 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
592 if ((phba->sli_rev == 3) &&
593 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
594 /* fill in immediate fcp command BDE */
595 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
596 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
597 iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
598 unsli3.fcp_ext.icd);
599 iocb->un.fcpi64.bdl.addrHigh = 0;
600 iocb->ulpBdeCount = 0;
601 iocb->ulpLe = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300602 /* fill in response BDE */
James Smart3772a992009-05-22 14:50:54 -0400603 iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags =
604 BUFF_TYPE_BDE_64;
605 iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
606 sizeof(struct fcp_rsp);
607 iocb->unsli3.fcp_ext.rbde.addrLow =
608 putPaddrLow(pdma_phys_fcp_rsp);
609 iocb->unsli3.fcp_ext.rbde.addrHigh =
610 putPaddrHigh(pdma_phys_fcp_rsp);
611 } else {
612 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
613 iocb->un.fcpi64.bdl.bdeSize =
614 (2 * sizeof(struct ulp_bde64));
615 iocb->un.fcpi64.bdl.addrLow =
616 putPaddrLow(pdma_phys_bpl);
617 iocb->un.fcpi64.bdl.addrHigh =
618 putPaddrHigh(pdma_phys_bpl);
619 iocb->ulpBdeCount = 1;
620 iocb->ulpLe = 1;
621 }
622 iocb->ulpClass = CLASS3;
623 psb->status = IOSTAT_SUCCESS;
James Smartda0436e2009-05-22 14:51:39 -0400624 /* Put it back into the SCSI buffer list */
James Smarteee88772010-09-29 11:19:08 -0400625 psb->cur_iocbq.context1 = psb;
James Smart1c6f4ef52009-11-18 15:40:49 -0500626 lpfc_release_scsi_buf_s3(phba, psb);
James Smart3772a992009-05-22 14:50:54 -0400627
James Smart34b02dc2008-08-24 21:49:55 -0400628 }
dea31012005-04-17 16:05:31 -0500629
James Smart3772a992009-05-22 14:50:54 -0400630 return bcnt;
dea31012005-04-17 16:05:31 -0500631}
632
James Smart9bad7672008-12-04 22:39:02 -0500633/**
James Smart1151e3e2011-02-16 12:39:35 -0500634 * lpfc_sli4_vport_delete_fcp_xri_aborted -Remove all ndlp references for vport
635 * @vport: pointer to lpfc vport data structure.
636 *
637 * This routine is invoked by the vport cleanup for deletions and the cleanup
638 * for an ndlp on removal.
639 **/
640void
641lpfc_sli4_vport_delete_fcp_xri_aborted(struct lpfc_vport *vport)
642{
643 struct lpfc_hba *phba = vport->phba;
644 struct lpfc_scsi_buf *psb, *next_psb;
645 unsigned long iflag = 0;
646
647 spin_lock_irqsave(&phba->hbalock, iflag);
648 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
649 list_for_each_entry_safe(psb, next_psb,
650 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
651 if (psb->rdata && psb->rdata->pnode
652 && psb->rdata->pnode->vport == vport)
653 psb->rdata = NULL;
654 }
655 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
656 spin_unlock_irqrestore(&phba->hbalock, iflag);
657}
658
659/**
James Smartda0436e2009-05-22 14:51:39 -0400660 * lpfc_sli4_fcp_xri_aborted - Fast-path process of fcp xri abort
661 * @phba: pointer to lpfc hba data structure.
662 * @axri: pointer to the fcp xri abort wcqe structure.
663 *
664 * This routine is invoked by the worker thread to process a SLI4 fast-path
665 * FCP aborted xri.
666 **/
667void
668lpfc_sli4_fcp_xri_aborted(struct lpfc_hba *phba,
669 struct sli4_wcqe_xri_aborted *axri)
670{
671 uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
James Smart19ca7602010-11-20 23:11:55 -0500672 uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
James Smartda0436e2009-05-22 14:51:39 -0400673 struct lpfc_scsi_buf *psb, *next_psb;
674 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -0500675 struct lpfc_iocbq *iocbq;
676 int i;
James Smart19ca7602010-11-20 23:11:55 -0500677 struct lpfc_nodelist *ndlp;
678 int rrq_empty = 0;
James Smart589a52d2010-07-14 15:30:54 -0400679 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smartda0436e2009-05-22 14:51:39 -0400680
James Smart0f65ff62010-02-26 14:14:23 -0500681 spin_lock_irqsave(&phba->hbalock, iflag);
682 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
James Smartda0436e2009-05-22 14:51:39 -0400683 list_for_each_entry_safe(psb, next_psb,
684 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
685 if (psb->cur_iocbq.sli4_xritag == xri) {
686 list_del(&psb->list);
James Smart341af102010-01-26 23:07:37 -0500687 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400688 psb->status = IOSTAT_SUCCESS;
James Smart0f65ff62010-02-26 14:14:23 -0500689 spin_unlock(
690 &phba->sli4_hba.abts_scsi_buf_list_lock);
James Smart1151e3e2011-02-16 12:39:35 -0500691 if (psb->rdata && psb->rdata->pnode)
692 ndlp = psb->rdata->pnode;
693 else
694 ndlp = NULL;
695
James Smart19ca7602010-11-20 23:11:55 -0500696 rrq_empty = list_empty(&phba->active_rrq_list);
James Smart0f65ff62010-02-26 14:14:23 -0500697 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartcb69f7d2011-12-13 13:21:57 -0500698 if (ndlp) {
James Smartee0f4fe2012-05-09 21:19:14 -0400699 lpfc_set_rrq_active(phba, ndlp,
700 psb->cur_iocbq.sli4_lxritag, rxid, 1);
James Smartcb69f7d2011-12-13 13:21:57 -0500701 lpfc_sli4_abts_err_handler(phba, ndlp, axri);
702 }
James Smartda0436e2009-05-22 14:51:39 -0400703 lpfc_release_scsi_buf_s4(phba, psb);
James Smart19ca7602010-11-20 23:11:55 -0500704 if (rrq_empty)
705 lpfc_worker_wake_up(phba);
James Smartda0436e2009-05-22 14:51:39 -0400706 return;
707 }
708 }
James Smart0f65ff62010-02-26 14:14:23 -0500709 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
710 for (i = 1; i <= phba->sli.last_iotag; i++) {
711 iocbq = phba->sli.iocbq_lookup[i];
712
713 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
714 (iocbq->iocb_flag & LPFC_IO_LIBDFC))
715 continue;
716 if (iocbq->sli4_xritag != xri)
717 continue;
718 psb = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
719 psb->exch_busy = 0;
720 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart589a52d2010-07-14 15:30:54 -0400721 if (pring->txq_cnt)
722 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -0500723 return;
724
725 }
726 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartda0436e2009-05-22 14:51:39 -0400727}
728
729/**
James Smart8a9d2e82012-05-09 21:16:12 -0400730 * lpfc_sli4_post_scsi_sgl_list - Psot blocks of scsi buffer sgls from a list
731 * @phba: pointer to lpfc hba data structure.
732 * @post_sblist: pointer to the scsi buffer list.
733 *
734 * This routine walks a list of scsi buffers that was passed in. It attempts
735 * to construct blocks of scsi buffer sgls which contains contiguous xris and
736 * uses the non-embedded SGL block post mailbox commands to post to the port.
737 * For single SCSI buffer sgl with non-contiguous xri, if any, it shall use
738 * embedded SGL post mailbox command for posting. The @post_sblist passed in
739 * must be local list, thus no lock is needed when manipulate the list.
740 *
741 * Returns: 0 = failure, non-zero number of successfully posted buffers.
742 **/
743int
744lpfc_sli4_post_scsi_sgl_list(struct lpfc_hba *phba,
745 struct list_head *post_sblist, int sb_count)
746{
747 struct lpfc_scsi_buf *psb, *psb_next;
748 int status;
749 int post_cnt = 0, block_cnt = 0, num_posting = 0, num_posted = 0;
750 dma_addr_t pdma_phys_bpl1;
751 int last_xritag = NO_XRI;
752 LIST_HEAD(prep_sblist);
753 LIST_HEAD(blck_sblist);
754 LIST_HEAD(scsi_sblist);
755
756 /* sanity check */
757 if (sb_count <= 0)
758 return -EINVAL;
759
760 list_for_each_entry_safe(psb, psb_next, post_sblist, list) {
761 list_del_init(&psb->list);
762 block_cnt++;
763 if ((last_xritag != NO_XRI) &&
764 (psb->cur_iocbq.sli4_xritag != last_xritag + 1)) {
765 /* a hole in xri block, form a sgl posting block */
766 list_splice_init(&prep_sblist, &blck_sblist);
767 post_cnt = block_cnt - 1;
768 /* prepare list for next posting block */
769 list_add_tail(&psb->list, &prep_sblist);
770 block_cnt = 1;
771 } else {
772 /* prepare list for next posting block */
773 list_add_tail(&psb->list, &prep_sblist);
774 /* enough sgls for non-embed sgl mbox command */
775 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
776 list_splice_init(&prep_sblist, &blck_sblist);
777 post_cnt = block_cnt;
778 block_cnt = 0;
779 }
780 }
781 num_posting++;
782 last_xritag = psb->cur_iocbq.sli4_xritag;
783
784 /* end of repost sgl list condition for SCSI buffers */
785 if (num_posting == sb_count) {
786 if (post_cnt == 0) {
787 /* last sgl posting block */
788 list_splice_init(&prep_sblist, &blck_sblist);
789 post_cnt = block_cnt;
790 } else if (block_cnt == 1) {
791 /* last single sgl with non-contiguous xri */
792 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
793 pdma_phys_bpl1 = psb->dma_phys_bpl +
794 SGL_PAGE_SIZE;
795 else
796 pdma_phys_bpl1 = 0;
797 status = lpfc_sli4_post_sgl(phba,
798 psb->dma_phys_bpl,
799 pdma_phys_bpl1,
800 psb->cur_iocbq.sli4_xritag);
801 if (status) {
802 /* failure, put on abort scsi list */
803 psb->exch_busy = 1;
804 } else {
805 /* success, put on SCSI buffer list */
806 psb->exch_busy = 0;
807 psb->status = IOSTAT_SUCCESS;
808 num_posted++;
809 }
810 /* success, put on SCSI buffer sgl list */
811 list_add_tail(&psb->list, &scsi_sblist);
812 }
813 }
814
815 /* continue until a nembed page worth of sgls */
816 if (post_cnt == 0)
817 continue;
818
819 /* post block of SCSI buffer list sgls */
820 status = lpfc_sli4_post_scsi_sgl_block(phba, &blck_sblist,
821 post_cnt);
822
823 /* don't reset xirtag due to hole in xri block */
824 if (block_cnt == 0)
825 last_xritag = NO_XRI;
826
827 /* reset SCSI buffer post count for next round of posting */
828 post_cnt = 0;
829
830 /* put posted SCSI buffer-sgl posted on SCSI buffer sgl list */
831 while (!list_empty(&blck_sblist)) {
832 list_remove_head(&blck_sblist, psb,
833 struct lpfc_scsi_buf, list);
834 if (status) {
835 /* failure, put on abort scsi list */
836 psb->exch_busy = 1;
837 } else {
838 /* success, put on SCSI buffer list */
839 psb->exch_busy = 0;
840 psb->status = IOSTAT_SUCCESS;
841 num_posted++;
842 }
843 list_add_tail(&psb->list, &scsi_sblist);
844 }
845 }
846 /* Push SCSI buffers with sgl posted to the availble list */
847 while (!list_empty(&scsi_sblist)) {
848 list_remove_head(&scsi_sblist, psb,
849 struct lpfc_scsi_buf, list);
850 lpfc_release_scsi_buf_s4(phba, psb);
851 }
852 return num_posted;
853}
854
855/**
856 * lpfc_sli4_repost_scsi_sgl_list - Repsot all the allocated scsi buffer sgls
James Smartda0436e2009-05-22 14:51:39 -0400857 * @phba: pointer to lpfc hba data structure.
858 *
859 * This routine walks the list of scsi buffers that have been allocated and
James Smart8a9d2e82012-05-09 21:16:12 -0400860 * repost them to the port by using SGL block post. This is needed after a
James Smartda0436e2009-05-22 14:51:39 -0400861 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
862 * is responsible for moving all scsi buffers on the lpfc_abts_scsi_sgl_list
863 * to the lpfc_scsi_buf_list. If the repost fails, reject all scsi buffers.
864 *
865 * Returns: 0 = success, non-zero failure.
866 **/
867int
868lpfc_sli4_repost_scsi_sgl_list(struct lpfc_hba *phba)
869{
James Smart8a9d2e82012-05-09 21:16:12 -0400870 LIST_HEAD(post_sblist);
871 int num_posted, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -0400872
James Smart8a9d2e82012-05-09 21:16:12 -0400873 /* get all SCSI buffers need to repost to a local list */
874 spin_lock(&phba->scsi_buf_list_lock);
875 list_splice_init(&phba->lpfc_scsi_buf_list, &post_sblist);
876 spin_unlock(&phba->scsi_buf_list_lock);
James Smartda0436e2009-05-22 14:51:39 -0400877
James Smart8a9d2e82012-05-09 21:16:12 -0400878 /* post the list of scsi buffer sgls to port if available */
879 if (!list_empty(&post_sblist)) {
880 num_posted = lpfc_sli4_post_scsi_sgl_list(phba, &post_sblist,
881 phba->sli4_hba.scsi_xri_cnt);
882 /* failed to post any scsi buffer, return error */
883 if (num_posted == 0)
884 rc = -EIO;
James Smartda0436e2009-05-22 14:51:39 -0400885 }
886 return rc;
887}
888
889/**
890 * lpfc_new_scsi_buf_s4 - Scsi buffer allocator for HBA with SLI4 IF spec
891 * @vport: The virtual port for which this call being executed.
892 * @num_to_allocate: The requested number of buffers to allocate.
893 *
James Smart8a9d2e82012-05-09 21:16:12 -0400894 * This routine allocates scsi buffers for device with SLI-4 interface spec,
James Smartda0436e2009-05-22 14:51:39 -0400895 * the scsi buffer contains all the necessary information needed to initiate
James Smart8a9d2e82012-05-09 21:16:12 -0400896 * a SCSI I/O. After allocating up to @num_to_allocate SCSI buffers and put
897 * them on a list, it post them to the port by using SGL block post.
James Smartda0436e2009-05-22 14:51:39 -0400898 *
899 * Return codes:
James Smart8a9d2e82012-05-09 21:16:12 -0400900 * int - number of scsi buffers that were allocated and posted.
James Smartda0436e2009-05-22 14:51:39 -0400901 * 0 = failure, less than num_to_alloc is a partial failure.
902 **/
903static int
904lpfc_new_scsi_buf_s4(struct lpfc_vport *vport, int num_to_alloc)
905{
906 struct lpfc_hba *phba = vport->phba;
907 struct lpfc_scsi_buf *psb;
908 struct sli4_sge *sgl;
909 IOCB_t *iocb;
910 dma_addr_t pdma_phys_fcp_cmd;
911 dma_addr_t pdma_phys_fcp_rsp;
912 dma_addr_t pdma_phys_bpl, pdma_phys_bpl1;
James Smart8a9d2e82012-05-09 21:16:12 -0400913 uint16_t iotag, lxri = 0;
914 int bcnt, num_posted;
915 LIST_HEAD(prep_sblist);
916 LIST_HEAD(post_sblist);
917 LIST_HEAD(scsi_sblist);
James Smartda0436e2009-05-22 14:51:39 -0400918
919 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
920 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
921 if (!psb)
922 break;
James Smartda0436e2009-05-22 14:51:39 -0400923 /*
James Smart8a9d2e82012-05-09 21:16:12 -0400924 * Get memory from the pci pool to map the virt space to
925 * pci bus space for an I/O. The DMA buffer includes space
926 * for the struct fcp_cmnd, struct fcp_rsp and the number
927 * of bde's necessary to support the sg_tablesize.
James Smartda0436e2009-05-22 14:51:39 -0400928 */
929 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
930 GFP_KERNEL, &psb->dma_handle);
931 if (!psb->data) {
932 kfree(psb);
933 break;
934 }
James Smartda0436e2009-05-22 14:51:39 -0400935 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
936
937 /* Allocate iotag for psb->cur_iocbq. */
938 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
939 if (iotag == 0) {
James Smartb92938b2010-06-07 15:24:12 -0400940 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
941 psb->data, psb->dma_handle);
James Smartda0436e2009-05-22 14:51:39 -0400942 kfree(psb);
943 break;
944 }
945
James Smart6d368e52011-05-24 11:44:12 -0400946 lxri = lpfc_sli4_next_xritag(phba);
947 if (lxri == NO_XRI) {
James Smartda0436e2009-05-22 14:51:39 -0400948 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
949 psb->data, psb->dma_handle);
950 kfree(psb);
951 break;
952 }
James Smart6d368e52011-05-24 11:44:12 -0400953 psb->cur_iocbq.sli4_lxritag = lxri;
954 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
James Smartda0436e2009-05-22 14:51:39 -0400955 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Smartda0436e2009-05-22 14:51:39 -0400956 psb->fcp_bpl = psb->data;
957 psb->fcp_cmnd = (psb->data + phba->cfg_sg_dma_buf_size)
958 - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
959 psb->fcp_rsp = (struct fcp_rsp *)((uint8_t *)psb->fcp_cmnd +
960 sizeof(struct fcp_cmnd));
961
962 /* Initialize local short-hand pointers. */
963 sgl = (struct sli4_sge *)psb->fcp_bpl;
964 pdma_phys_bpl = psb->dma_handle;
965 pdma_phys_fcp_cmd =
966 (psb->dma_handle + phba->cfg_sg_dma_buf_size)
967 - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
968 pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
969
970 /*
James Smart8a9d2e82012-05-09 21:16:12 -0400971 * The first two bdes are the FCP_CMD and FCP_RSP.
972 * The balance are sg list bdes. Initialize the
973 * first two and leave the rest for queuecommand.
James Smartda0436e2009-05-22 14:51:39 -0400974 */
975 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
976 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
James Smart05580562011-05-24 11:40:48 -0400977 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -0400978 bf_set(lpfc_sli4_sge_last, sgl, 0);
979 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -0500980 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_cmnd));
James Smartda0436e2009-05-22 14:51:39 -0400981 sgl++;
982
983 /* Setup the physical region for the FCP RSP */
984 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
985 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
James Smart05580562011-05-24 11:40:48 -0400986 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -0400987 bf_set(lpfc_sli4_sge_last, sgl, 1);
988 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -0500989 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
James Smartda0436e2009-05-22 14:51:39 -0400990
991 /*
992 * Since the IOCB for the FCP I/O is built into this
993 * lpfc_scsi_buf, initialize it with all known data now.
994 */
995 iocb = &psb->cur_iocbq.iocb;
996 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
997 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
998 /* setting the BLP size to 2 * sizeof BDE may not be correct.
999 * We are setting the bpl to point to out sgl. An sgl's
1000 * entries are 16 bytes, a bpl entries are 12 bytes.
1001 */
1002 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
1003 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
1004 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
1005 iocb->ulpBdeCount = 1;
1006 iocb->ulpLe = 1;
1007 iocb->ulpClass = CLASS3;
James Smart8a9d2e82012-05-09 21:16:12 -04001008 psb->cur_iocbq.context1 = psb;
James Smartda0436e2009-05-22 14:51:39 -04001009 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
1010 pdma_phys_bpl1 = pdma_phys_bpl + SGL_PAGE_SIZE;
1011 else
1012 pdma_phys_bpl1 = 0;
1013 psb->dma_phys_bpl = pdma_phys_bpl;
James Smart6d368e52011-05-24 11:44:12 -04001014
James Smart8a9d2e82012-05-09 21:16:12 -04001015 /* add the scsi buffer to a post list */
1016 list_add_tail(&psb->list, &post_sblist);
1017 spin_lock_irq(&phba->scsi_buf_list_lock);
1018 phba->sli4_hba.scsi_xri_cnt++;
1019 spin_unlock_irq(&phba->scsi_buf_list_lock);
James Smartda0436e2009-05-22 14:51:39 -04001020 }
James Smart8a9d2e82012-05-09 21:16:12 -04001021 lpfc_printf_log(phba, KERN_INFO, LOG_BG,
1022 "3021 Allocate %d out of %d requested new SCSI "
1023 "buffers\n", bcnt, num_to_alloc);
James Smartda0436e2009-05-22 14:51:39 -04001024
James Smart8a9d2e82012-05-09 21:16:12 -04001025 /* post the list of scsi buffer sgls to port if available */
1026 if (!list_empty(&post_sblist))
1027 num_posted = lpfc_sli4_post_scsi_sgl_list(phba,
1028 &post_sblist, bcnt);
1029 else
1030 num_posted = 0;
1031
1032 return num_posted;
James Smartda0436e2009-05-22 14:51:39 -04001033}
1034
1035/**
James Smart3772a992009-05-22 14:50:54 -04001036 * lpfc_new_scsi_buf - Wrapper funciton for scsi buffer allocator
1037 * @vport: The virtual port for which this call being executed.
1038 * @num_to_allocate: The requested number of buffers to allocate.
1039 *
1040 * This routine wraps the actual SCSI buffer allocator function pointer from
1041 * the lpfc_hba struct.
1042 *
1043 * Return codes:
1044 * int - number of scsi buffers that were allocated.
1045 * 0 = failure, less than num_to_alloc is a partial failure.
1046 **/
1047static inline int
1048lpfc_new_scsi_buf(struct lpfc_vport *vport, int num_to_alloc)
1049{
1050 return vport->phba->lpfc_new_scsi_buf(vport, num_to_alloc);
1051}
1052
1053/**
James Smart19ca7602010-11-20 23:11:55 -05001054 * lpfc_get_scsi_buf_s3 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
James Smart3772a992009-05-22 14:50:54 -04001055 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -05001056 *
1057 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1058 * and returns to caller.
1059 *
1060 * Return codes:
1061 * NULL - Error
1062 * Pointer to lpfc_scsi_buf - Success
1063 **/
Adrian Bunk455c53e2006-01-06 20:21:28 +01001064static struct lpfc_scsi_buf*
James Smart19ca7602010-11-20 23:11:55 -05001065lpfc_get_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001066{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001067 struct lpfc_scsi_buf * lpfc_cmd = NULL;
1068 struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001069 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -05001070
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001071 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001072 list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
James Smart1dcb58e2007-04-25 09:51:30 -04001073 if (lpfc_cmd) {
1074 lpfc_cmd->seg_cnt = 0;
1075 lpfc_cmd->nonsg_phys = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05001076 lpfc_cmd->prot_seg_cnt = 0;
James Smart1dcb58e2007-04-25 09:51:30 -04001077 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001078 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001079 return lpfc_cmd;
1080}
James Smart19ca7602010-11-20 23:11:55 -05001081/**
1082 * lpfc_get_scsi_buf_s4 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1083 * @phba: The HBA for which this call is being executed.
1084 *
1085 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1086 * and returns to caller.
1087 *
1088 * Return codes:
1089 * NULL - Error
1090 * Pointer to lpfc_scsi_buf - Success
1091 **/
1092static struct lpfc_scsi_buf*
1093lpfc_get_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1094{
James Smart1151e3e2011-02-16 12:39:35 -05001095 struct lpfc_scsi_buf *lpfc_cmd ;
James Smart19ca7602010-11-20 23:11:55 -05001096 unsigned long iflag = 0;
1097 int found = 0;
1098
1099 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James Smart1151e3e2011-02-16 12:39:35 -05001100 list_for_each_entry(lpfc_cmd, &phba->lpfc_scsi_buf_list,
1101 list) {
James Smart19ca7602010-11-20 23:11:55 -05001102 if (lpfc_test_rrq_active(phba, ndlp,
James Smartee0f4fe2012-05-09 21:19:14 -04001103 lpfc_cmd->cur_iocbq.sli4_lxritag))
James Smart1151e3e2011-02-16 12:39:35 -05001104 continue;
1105 list_del(&lpfc_cmd->list);
James Smart19ca7602010-11-20 23:11:55 -05001106 found = 1;
1107 lpfc_cmd->seg_cnt = 0;
1108 lpfc_cmd->nonsg_phys = 0;
1109 lpfc_cmd->prot_seg_cnt = 0;
James Smart1151e3e2011-02-16 12:39:35 -05001110 break;
James Smart19ca7602010-11-20 23:11:55 -05001111 }
James Smart1151e3e2011-02-16 12:39:35 -05001112 spin_unlock_irqrestore(&phba->scsi_buf_list_lock,
1113 iflag);
1114 if (!found)
1115 return NULL;
1116 else
1117 return lpfc_cmd;
James Smart19ca7602010-11-20 23:11:55 -05001118}
1119/**
1120 * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1121 * @phba: The HBA for which this call is being executed.
1122 *
1123 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1124 * and returns to caller.
1125 *
1126 * Return codes:
1127 * NULL - Error
1128 * Pointer to lpfc_scsi_buf - Success
1129 **/
1130static struct lpfc_scsi_buf*
1131lpfc_get_scsi_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1132{
1133 return phba->lpfc_get_scsi_buf(phba, ndlp);
1134}
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001135
James Smart9bad7672008-12-04 22:39:02 -05001136/**
James Smart3772a992009-05-22 14:50:54 -04001137 * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
James Smart9bad7672008-12-04 22:39:02 -05001138 * @phba: The Hba for which this call is being executed.
1139 * @psb: The scsi buffer which is being released.
1140 *
1141 * This routine releases @psb scsi buffer by adding it to tail of @phba
1142 * lpfc_scsi_buf_list list.
1143 **/
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001144static void
James Smart3772a992009-05-22 14:50:54 -04001145lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001146{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001147 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -05001148
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001149 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001150 psb->pCmd = NULL;
dea31012005-04-17 16:05:31 -05001151 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001152 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
dea31012005-04-17 16:05:31 -05001153}
1154
James Smart9bad7672008-12-04 22:39:02 -05001155/**
James Smartda0436e2009-05-22 14:51:39 -04001156 * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
1157 * @phba: The Hba for which this call is being executed.
1158 * @psb: The scsi buffer which is being released.
1159 *
1160 * This routine releases @psb scsi buffer by adding it to tail of @phba
1161 * lpfc_scsi_buf_list list. For SLI4 XRI's are tied to the scsi buffer
1162 * and cannot be reused for at least RA_TOV amount of time if it was
1163 * aborted.
1164 **/
1165static void
1166lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1167{
1168 unsigned long iflag = 0;
1169
James Smart341af102010-01-26 23:07:37 -05001170 if (psb->exch_busy) {
James Smartda0436e2009-05-22 14:51:39 -04001171 spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock,
1172 iflag);
1173 psb->pCmd = NULL;
1174 list_add_tail(&psb->list,
1175 &phba->sli4_hba.lpfc_abts_scsi_buf_list);
1176 spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
1177 iflag);
1178 } else {
1179
1180 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
1181 psb->pCmd = NULL;
1182 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
1183 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
1184 }
1185}
1186
1187/**
James Smart3772a992009-05-22 14:50:54 -04001188 * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
1189 * @phba: The Hba for which this call is being executed.
1190 * @psb: The scsi buffer which is being released.
1191 *
1192 * This routine releases @psb scsi buffer by adding it to tail of @phba
1193 * lpfc_scsi_buf_list list.
1194 **/
1195static void
1196lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1197{
1198
1199 phba->lpfc_release_scsi_buf(phba, psb);
1200}
1201
1202/**
1203 * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -05001204 * @phba: The Hba for which this call is being executed.
1205 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1206 *
1207 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
James Smart3772a992009-05-22 14:50:54 -04001208 * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
1209 * through sg elements and format the bdea. This routine also initializes all
1210 * IOCB fields which are dependent on scsi command request buffer.
James Smart9bad7672008-12-04 22:39:02 -05001211 *
1212 * Return codes:
1213 * 1 - Error
1214 * 0 - Success
1215 **/
dea31012005-04-17 16:05:31 -05001216static int
James Smart3772a992009-05-22 14:50:54 -04001217lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
dea31012005-04-17 16:05:31 -05001218{
1219 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1220 struct scatterlist *sgel = NULL;
1221 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1222 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
James Smart0f65ff62010-02-26 14:14:23 -05001223 struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
dea31012005-04-17 16:05:31 -05001224 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart34b02dc2008-08-24 21:49:55 -04001225 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
dea31012005-04-17 16:05:31 -05001226 dma_addr_t physaddr;
James Smart34b02dc2008-08-24 21:49:55 -04001227 uint32_t num_bde = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001228 int nseg, datadir = scsi_cmnd->sc_data_direction;
dea31012005-04-17 16:05:31 -05001229
1230 /*
1231 * There are three possibilities here - use scatter-gather segment, use
1232 * the single mapping, or neither. Start the lpfc command prep by
1233 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1234 * data bde entry.
1235 */
1236 bpl += 2;
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001237 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05001238 /*
1239 * The driver stores the segment count returned from pci_map_sg
1240 * because this a count of dma-mappings used to map the use_sg
1241 * pages. They are not guaranteed to be the same for those
1242 * architectures that implement an IOMMU.
1243 */
dea31012005-04-17 16:05:31 -05001244
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001245 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
1246 scsi_sg_count(scsi_cmnd), datadir);
1247 if (unlikely(!nseg))
1248 return 1;
1249
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001250 lpfc_cmd->seg_cnt = nseg;
dea31012005-04-17 16:05:31 -05001251 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001252 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1253 "9064 BLKGRD: %s: Too many sg segments from "
James Smarte2a0a9d2008-12-04 22:40:02 -05001254 "dma_map_sg. Config %d, seg_cnt %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001255 __func__, phba->cfg_sg_seg_cnt,
dea31012005-04-17 16:05:31 -05001256 lpfc_cmd->seg_cnt);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001257 scsi_dma_unmap(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001258 return 1;
1259 }
1260
1261 /*
1262 * The driver established a maximum scatter-gather segment count
1263 * during probe that limits the number of sg elements in any
1264 * single scsi command. Just run through the seg_cnt and format
1265 * the bde's.
James Smart34b02dc2008-08-24 21:49:55 -04001266 * When using SLI-3 the driver will try to fit all the BDEs into
1267 * the IOCB. If it can't then the BDEs get added to a BPL as it
1268 * does for SLI-2 mode.
dea31012005-04-17 16:05:31 -05001269 */
James Smart34b02dc2008-08-24 21:49:55 -04001270 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
dea31012005-04-17 16:05:31 -05001271 physaddr = sg_dma_address(sgel);
James Smart34b02dc2008-08-24 21:49:55 -04001272 if (phba->sli_rev == 3 &&
James Smarte2a0a9d2008-12-04 22:40:02 -05001273 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
James Smart0f65ff62010-02-26 14:14:23 -05001274 !(iocbq->iocb_flag & DSS_SECURITY_OP) &&
James Smart34b02dc2008-08-24 21:49:55 -04001275 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
1276 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1277 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
1278 data_bde->addrLow = putPaddrLow(physaddr);
1279 data_bde->addrHigh = putPaddrHigh(physaddr);
1280 data_bde++;
1281 } else {
1282 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1283 bpl->tus.f.bdeSize = sg_dma_len(sgel);
1284 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1285 bpl->addrLow =
1286 le32_to_cpu(putPaddrLow(physaddr));
1287 bpl->addrHigh =
1288 le32_to_cpu(putPaddrHigh(physaddr));
1289 bpl++;
1290 }
dea31012005-04-17 16:05:31 -05001291 }
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001292 }
dea31012005-04-17 16:05:31 -05001293
1294 /*
1295 * Finish initializing those IOCB fields that are dependent on the
James Smart34b02dc2008-08-24 21:49:55 -04001296 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
1297 * explicitly reinitialized and for SLI-3 the extended bde count is
1298 * explicitly reinitialized since all iocb memory resources are reused.
dea31012005-04-17 16:05:31 -05001299 */
James Smarte2a0a9d2008-12-04 22:40:02 -05001300 if (phba->sli_rev == 3 &&
James Smart0f65ff62010-02-26 14:14:23 -05001301 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
1302 !(iocbq->iocb_flag & DSS_SECURITY_OP)) {
James Smart34b02dc2008-08-24 21:49:55 -04001303 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
1304 /*
1305 * The extended IOCB format can only fit 3 BDE or a BPL.
1306 * This I/O has more than 3 BDE so the 1st data bde will
1307 * be a BPL that is filled in here.
1308 */
1309 physaddr = lpfc_cmd->dma_handle;
1310 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
1311 data_bde->tus.f.bdeSize = (num_bde *
1312 sizeof(struct ulp_bde64));
1313 physaddr += (sizeof(struct fcp_cmnd) +
1314 sizeof(struct fcp_rsp) +
1315 (2 * sizeof(struct ulp_bde64)));
1316 data_bde->addrHigh = putPaddrHigh(physaddr);
1317 data_bde->addrLow = putPaddrLow(physaddr);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001318 /* ebde count includes the response bde and data bpl */
James Smart34b02dc2008-08-24 21:49:55 -04001319 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
1320 } else {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001321 /* ebde count includes the response bde and data bdes */
James Smart34b02dc2008-08-24 21:49:55 -04001322 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1323 }
1324 } else {
1325 iocb_cmd->un.fcpi64.bdl.bdeSize =
1326 ((num_bde + 2) * sizeof(struct ulp_bde64));
James Smart0f65ff62010-02-26 14:14:23 -05001327 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
James Smart34b02dc2008-08-24 21:49:55 -04001328 }
James Smart09372822008-01-11 01:52:54 -05001329 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
James Smarte2a0a9d2008-12-04 22:40:02 -05001330
1331 /*
1332 * Due to difference in data length between DIF/non-DIF paths,
1333 * we need to set word 4 of IOCB here
1334 */
James Smarta257bf92009-04-06 18:48:10 -04001335 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001336 return 0;
1337}
1338
James Smartf9bb2da2011-10-10 21:34:11 -04001339static inline unsigned
1340lpfc_cmd_blksize(struct scsi_cmnd *sc)
1341{
1342 return sc->device->sector_size;
1343}
1344
1345#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001346
James Smart9a6b09c2012-03-01 22:37:42 -05001347/* Return if if error injection is detected by Initiator */
1348#define BG_ERR_INIT 0x1
1349/* Return if if error injection is detected by Target */
1350#define BG_ERR_TGT 0x2
1351/* Return if if swapping CSUM<-->CRC is required for error injection */
1352#define BG_ERR_SWAP 0x10
1353/* Return if disabling Guard/Ref/App checking is required for error injection */
1354#define BG_ERR_CHECK 0x20
James Smartacd68592012-01-18 16:25:09 -05001355
1356/**
1357 * lpfc_bg_err_inject - Determine if we should inject an error
1358 * @phba: The Hba for which this call is being executed.
James Smartf9bb2da2011-10-10 21:34:11 -04001359 * @sc: The SCSI command to examine
1360 * @reftag: (out) BlockGuard reference tag for transmitted data
1361 * @apptag: (out) BlockGuard application tag for transmitted data
1362 * @new_guard (in) Value to replace CRC with if needed
1363 *
James Smart9a6b09c2012-03-01 22:37:42 -05001364 * Returns BG_ERR_* bit mask or 0 if request ignored
James Smartacd68592012-01-18 16:25:09 -05001365 **/
James Smartf9bb2da2011-10-10 21:34:11 -04001366static int
1367lpfc_bg_err_inject(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1368 uint32_t *reftag, uint16_t *apptag, uint32_t new_guard)
1369{
1370 struct scatterlist *sgpe; /* s/g prot entry */
1371 struct scatterlist *sgde; /* s/g data entry */
James Smart9a6b09c2012-03-01 22:37:42 -05001372 struct lpfc_scsi_buf *lpfc_cmd = NULL;
James Smartacd68592012-01-18 16:25:09 -05001373 struct scsi_dif_tuple *src = NULL;
James Smart4ac9b222012-03-01 22:38:29 -05001374 struct lpfc_nodelist *ndlp;
1375 struct lpfc_rport_data *rdata;
James Smartf9bb2da2011-10-10 21:34:11 -04001376 uint32_t op = scsi_get_prot_op(sc);
1377 uint32_t blksize;
1378 uint32_t numblks;
1379 sector_t lba;
1380 int rc = 0;
James Smartacd68592012-01-18 16:25:09 -05001381 int blockoff = 0;
James Smartf9bb2da2011-10-10 21:34:11 -04001382
1383 if (op == SCSI_PROT_NORMAL)
1384 return 0;
1385
James Smartacd68592012-01-18 16:25:09 -05001386 sgpe = scsi_prot_sglist(sc);
1387 sgde = scsi_sglist(sc);
James Smartf9bb2da2011-10-10 21:34:11 -04001388 lba = scsi_get_lba(sc);
James Smart4ac9b222012-03-01 22:38:29 -05001389
1390 /* First check if we need to match the LBA */
James Smartf9bb2da2011-10-10 21:34:11 -04001391 if (phba->lpfc_injerr_lba != LPFC_INJERR_LBA_OFF) {
1392 blksize = lpfc_cmd_blksize(sc);
1393 numblks = (scsi_bufflen(sc) + blksize - 1) / blksize;
1394
1395 /* Make sure we have the right LBA if one is specified */
1396 if ((phba->lpfc_injerr_lba < lba) ||
1397 (phba->lpfc_injerr_lba >= (lba + numblks)))
1398 return 0;
James Smartacd68592012-01-18 16:25:09 -05001399 if (sgpe) {
1400 blockoff = phba->lpfc_injerr_lba - lba;
1401 numblks = sg_dma_len(sgpe) /
1402 sizeof(struct scsi_dif_tuple);
1403 if (numblks < blockoff)
1404 blockoff = numblks;
James Smartacd68592012-01-18 16:25:09 -05001405 }
James Smartf9bb2da2011-10-10 21:34:11 -04001406 }
1407
James Smart4ac9b222012-03-01 22:38:29 -05001408 /* Next check if we need to match the remote NPortID or WWPN */
1409 rdata = sc->device->hostdata;
1410 if (rdata && rdata->pnode) {
1411 ndlp = rdata->pnode;
1412
1413 /* Make sure we have the right NPortID if one is specified */
1414 if (phba->lpfc_injerr_nportid &&
1415 (phba->lpfc_injerr_nportid != ndlp->nlp_DID))
1416 return 0;
1417
1418 /*
1419 * Make sure we have the right WWPN if one is specified.
1420 * wwn[0] should be a non-zero NAA in a good WWPN.
1421 */
1422 if (phba->lpfc_injerr_wwpn.u.wwn[0] &&
1423 (memcmp(&ndlp->nlp_portname, &phba->lpfc_injerr_wwpn,
1424 sizeof(struct lpfc_name)) != 0))
1425 return 0;
1426 }
1427
1428 /* Setup a ptr to the protection data if the SCSI host provides it */
1429 if (sgpe) {
1430 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
1431 src += blockoff;
1432 lpfc_cmd = (struct lpfc_scsi_buf *)sc->host_scribble;
1433 }
1434
James Smartf9bb2da2011-10-10 21:34:11 -04001435 /* Should we change the Reference Tag */
1436 if (reftag) {
James Smartacd68592012-01-18 16:25:09 -05001437 if (phba->lpfc_injerr_wref_cnt) {
1438 switch (op) {
1439 case SCSI_PROT_WRITE_PASS:
James Smart9a6b09c2012-03-01 22:37:42 -05001440 if (src) {
1441 /*
1442 * For WRITE_PASS, force the error
1443 * to be sent on the wire. It should
1444 * be detected by the Target.
1445 * If blockoff != 0 error will be
1446 * inserted in middle of the IO.
1447 */
James Smartf9bb2da2011-10-10 21:34:11 -04001448
James Smartacd68592012-01-18 16:25:09 -05001449 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1450 "9076 BLKGRD: Injecting reftag error: "
1451 "write lba x%lx + x%x oldrefTag x%x\n",
1452 (unsigned long)lba, blockoff,
James Smart9a6b09c2012-03-01 22:37:42 -05001453 be32_to_cpu(src->ref_tag));
James Smartacd68592012-01-18 16:25:09 -05001454
1455 /*
James Smart9a6b09c2012-03-01 22:37:42 -05001456 * Save the old ref_tag so we can
1457 * restore it on completion.
James Smartacd68592012-01-18 16:25:09 -05001458 */
James Smart9a6b09c2012-03-01 22:37:42 -05001459 if (lpfc_cmd) {
1460 lpfc_cmd->prot_data_type =
1461 LPFC_INJERR_REFTAG;
1462 lpfc_cmd->prot_data_segment =
1463 src;
1464 lpfc_cmd->prot_data =
1465 src->ref_tag;
1466 }
1467 src->ref_tag = cpu_to_be32(0xDEADBEEF);
James Smartacd68592012-01-18 16:25:09 -05001468 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001469 if (phba->lpfc_injerr_wref_cnt == 0) {
1470 phba->lpfc_injerr_nportid = 0;
1471 phba->lpfc_injerr_lba =
1472 LPFC_INJERR_LBA_OFF;
1473 memset(&phba->lpfc_injerr_wwpn,
1474 0, sizeof(struct lpfc_name));
1475 }
James Smart9a6b09c2012-03-01 22:37:42 -05001476 rc = BG_ERR_TGT | BG_ERR_CHECK;
1477
James Smartacd68592012-01-18 16:25:09 -05001478 break;
1479 }
1480 /* Drop thru */
James Smart9a6b09c2012-03-01 22:37:42 -05001481 case SCSI_PROT_WRITE_INSERT:
1482 /*
1483 * For WRITE_INSERT, force the error
1484 * to be sent on the wire. It should be
1485 * detected by the Target.
1486 */
1487 /* DEADBEEF will be the reftag on the wire */
1488 *reftag = 0xDEADBEEF;
1489 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001490 if (phba->lpfc_injerr_wref_cnt == 0) {
1491 phba->lpfc_injerr_nportid = 0;
1492 phba->lpfc_injerr_lba =
1493 LPFC_INJERR_LBA_OFF;
1494 memset(&phba->lpfc_injerr_wwpn,
1495 0, sizeof(struct lpfc_name));
1496 }
James Smart9a6b09c2012-03-01 22:37:42 -05001497 rc = BG_ERR_TGT | BG_ERR_CHECK;
1498
1499 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1500 "9078 BLKGRD: Injecting reftag error: "
1501 "write lba x%lx\n", (unsigned long)lba);
1502 break;
James Smartacd68592012-01-18 16:25:09 -05001503 case SCSI_PROT_WRITE_STRIP:
1504 /*
1505 * For WRITE_STRIP and WRITE_PASS,
1506 * force the error on data
1507 * being copied from SLI-Host to SLI-Port.
1508 */
1509 *reftag = 0xDEADBEEF;
1510 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001511 if (phba->lpfc_injerr_wref_cnt == 0) {
1512 phba->lpfc_injerr_nportid = 0;
1513 phba->lpfc_injerr_lba =
1514 LPFC_INJERR_LBA_OFF;
1515 memset(&phba->lpfc_injerr_wwpn,
1516 0, sizeof(struct lpfc_name));
1517 }
James Smartacd68592012-01-18 16:25:09 -05001518 rc = BG_ERR_INIT;
1519
1520 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1521 "9077 BLKGRD: Injecting reftag error: "
1522 "write lba x%lx\n", (unsigned long)lba);
1523 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001524 }
James Smartacd68592012-01-18 16:25:09 -05001525 }
1526 if (phba->lpfc_injerr_rref_cnt) {
1527 switch (op) {
1528 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001529 case SCSI_PROT_READ_STRIP:
1530 case SCSI_PROT_READ_PASS:
1531 /*
1532 * For READ_STRIP and READ_PASS, force the
1533 * error on data being read off the wire. It
1534 * should force an IO error to the driver.
1535 */
James Smartf9bb2da2011-10-10 21:34:11 -04001536 *reftag = 0xDEADBEEF;
1537 phba->lpfc_injerr_rref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001538 if (phba->lpfc_injerr_rref_cnt == 0) {
1539 phba->lpfc_injerr_nportid = 0;
1540 phba->lpfc_injerr_lba =
1541 LPFC_INJERR_LBA_OFF;
1542 memset(&phba->lpfc_injerr_wwpn,
1543 0, sizeof(struct lpfc_name));
1544 }
James Smartacd68592012-01-18 16:25:09 -05001545 rc = BG_ERR_INIT;
James Smartf9bb2da2011-10-10 21:34:11 -04001546
1547 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smartacd68592012-01-18 16:25:09 -05001548 "9079 BLKGRD: Injecting reftag error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001549 "read lba x%lx\n", (unsigned long)lba);
James Smartacd68592012-01-18 16:25:09 -05001550 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001551 }
1552 }
1553 }
1554
1555 /* Should we change the Application Tag */
1556 if (apptag) {
James Smartacd68592012-01-18 16:25:09 -05001557 if (phba->lpfc_injerr_wapp_cnt) {
1558 switch (op) {
1559 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001560 if (src) {
James Smart9a6b09c2012-03-01 22:37:42 -05001561 /*
1562 * For WRITE_PASS, force the error
1563 * to be sent on the wire. It should
1564 * be detected by the Target.
1565 * If blockoff != 0 error will be
1566 * inserted in middle of the IO.
1567 */
1568
James Smartacd68592012-01-18 16:25:09 -05001569 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1570 "9080 BLKGRD: Injecting apptag error: "
1571 "write lba x%lx + x%x oldappTag x%x\n",
1572 (unsigned long)lba, blockoff,
James Smart9a6b09c2012-03-01 22:37:42 -05001573 be16_to_cpu(src->app_tag));
James Smartacd68592012-01-18 16:25:09 -05001574
1575 /*
James Smart9a6b09c2012-03-01 22:37:42 -05001576 * Save the old app_tag so we can
1577 * restore it on completion.
James Smartacd68592012-01-18 16:25:09 -05001578 */
James Smart9a6b09c2012-03-01 22:37:42 -05001579 if (lpfc_cmd) {
1580 lpfc_cmd->prot_data_type =
1581 LPFC_INJERR_APPTAG;
1582 lpfc_cmd->prot_data_segment =
1583 src;
1584 lpfc_cmd->prot_data =
1585 src->app_tag;
1586 }
1587 src->app_tag = cpu_to_be16(0xDEAD);
James Smartacd68592012-01-18 16:25:09 -05001588 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001589 if (phba->lpfc_injerr_wapp_cnt == 0) {
1590 phba->lpfc_injerr_nportid = 0;
1591 phba->lpfc_injerr_lba =
1592 LPFC_INJERR_LBA_OFF;
1593 memset(&phba->lpfc_injerr_wwpn,
1594 0, sizeof(struct lpfc_name));
1595 }
James Smart9a6b09c2012-03-01 22:37:42 -05001596 rc = BG_ERR_TGT | BG_ERR_CHECK;
James Smartacd68592012-01-18 16:25:09 -05001597 break;
1598 }
1599 /* Drop thru */
James Smart9a6b09c2012-03-01 22:37:42 -05001600 case SCSI_PROT_WRITE_INSERT:
1601 /*
1602 * For WRITE_INSERT, force the
1603 * error to be sent on the wire. It should be
1604 * detected by the Target.
1605 */
1606 /* DEAD will be the apptag on the wire */
1607 *apptag = 0xDEAD;
1608 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001609 if (phba->lpfc_injerr_wapp_cnt == 0) {
1610 phba->lpfc_injerr_nportid = 0;
1611 phba->lpfc_injerr_lba =
1612 LPFC_INJERR_LBA_OFF;
1613 memset(&phba->lpfc_injerr_wwpn,
1614 0, sizeof(struct lpfc_name));
1615 }
James Smart9a6b09c2012-03-01 22:37:42 -05001616 rc = BG_ERR_TGT | BG_ERR_CHECK;
1617
1618 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1619 "0813 BLKGRD: Injecting apptag error: "
1620 "write lba x%lx\n", (unsigned long)lba);
1621 break;
James Smartacd68592012-01-18 16:25:09 -05001622 case SCSI_PROT_WRITE_STRIP:
1623 /*
1624 * For WRITE_STRIP and WRITE_PASS,
1625 * force the error on data
1626 * being copied from SLI-Host to SLI-Port.
1627 */
1628 *apptag = 0xDEAD;
1629 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001630 if (phba->lpfc_injerr_wapp_cnt == 0) {
1631 phba->lpfc_injerr_nportid = 0;
1632 phba->lpfc_injerr_lba =
1633 LPFC_INJERR_LBA_OFF;
1634 memset(&phba->lpfc_injerr_wwpn,
1635 0, sizeof(struct lpfc_name));
1636 }
James Smartacd68592012-01-18 16:25:09 -05001637 rc = BG_ERR_INIT;
1638
1639 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1640 "0812 BLKGRD: Injecting apptag error: "
1641 "write lba x%lx\n", (unsigned long)lba);
1642 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001643 }
James Smartacd68592012-01-18 16:25:09 -05001644 }
1645 if (phba->lpfc_injerr_rapp_cnt) {
1646 switch (op) {
1647 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001648 case SCSI_PROT_READ_STRIP:
1649 case SCSI_PROT_READ_PASS:
1650 /*
1651 * For READ_STRIP and READ_PASS, force the
1652 * error on data being read off the wire. It
1653 * should force an IO error to the driver.
1654 */
James Smartf9bb2da2011-10-10 21:34:11 -04001655 *apptag = 0xDEAD;
1656 phba->lpfc_injerr_rapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001657 if (phba->lpfc_injerr_rapp_cnt == 0) {
1658 phba->lpfc_injerr_nportid = 0;
1659 phba->lpfc_injerr_lba =
1660 LPFC_INJERR_LBA_OFF;
1661 memset(&phba->lpfc_injerr_wwpn,
1662 0, sizeof(struct lpfc_name));
1663 }
James Smartacd68592012-01-18 16:25:09 -05001664 rc = BG_ERR_INIT;
James Smartf9bb2da2011-10-10 21:34:11 -04001665
1666 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smartacd68592012-01-18 16:25:09 -05001667 "0814 BLKGRD: Injecting apptag error: "
1668 "read lba x%lx\n", (unsigned long)lba);
1669 break;
1670 }
1671 }
1672 }
1673
1674
1675 /* Should we change the Guard Tag */
1676 if (new_guard) {
1677 if (phba->lpfc_injerr_wgrd_cnt) {
1678 switch (op) {
1679 case SCSI_PROT_WRITE_PASS:
James Smart9a6b09c2012-03-01 22:37:42 -05001680 rc = BG_ERR_CHECK;
James Smartacd68592012-01-18 16:25:09 -05001681 /* Drop thru */
James Smartacd68592012-01-18 16:25:09 -05001682
James Smartacd68592012-01-18 16:25:09 -05001683 case SCSI_PROT_WRITE_INSERT:
1684 /*
1685 * For WRITE_INSERT, force the
1686 * error to be sent on the wire. It should be
1687 * detected by the Target.
1688 */
1689 phba->lpfc_injerr_wgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001690 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1691 phba->lpfc_injerr_nportid = 0;
1692 phba->lpfc_injerr_lba =
1693 LPFC_INJERR_LBA_OFF;
1694 memset(&phba->lpfc_injerr_wwpn,
1695 0, sizeof(struct lpfc_name));
1696 }
James Smartacd68592012-01-18 16:25:09 -05001697
James Smart9a6b09c2012-03-01 22:37:42 -05001698 rc |= BG_ERR_TGT | BG_ERR_SWAP;
James Smartacd68592012-01-18 16:25:09 -05001699 /* Signals the caller to swap CRC->CSUM */
1700
1701 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1702 "0817 BLKGRD: Injecting guard error: "
1703 "write lba x%lx\n", (unsigned long)lba);
1704 break;
James Smart9a6b09c2012-03-01 22:37:42 -05001705 case SCSI_PROT_WRITE_STRIP:
1706 /*
1707 * For WRITE_STRIP and WRITE_PASS,
1708 * force the error on data
1709 * being copied from SLI-Host to SLI-Port.
1710 */
1711 phba->lpfc_injerr_wgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001712 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1713 phba->lpfc_injerr_nportid = 0;
1714 phba->lpfc_injerr_lba =
1715 LPFC_INJERR_LBA_OFF;
1716 memset(&phba->lpfc_injerr_wwpn,
1717 0, sizeof(struct lpfc_name));
1718 }
James Smart9a6b09c2012-03-01 22:37:42 -05001719
1720 rc = BG_ERR_INIT | BG_ERR_SWAP;
1721 /* Signals the caller to swap CRC->CSUM */
1722
1723 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1724 "0816 BLKGRD: Injecting guard error: "
1725 "write lba x%lx\n", (unsigned long)lba);
1726 break;
James Smartacd68592012-01-18 16:25:09 -05001727 }
1728 }
1729 if (phba->lpfc_injerr_rgrd_cnt) {
1730 switch (op) {
1731 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001732 case SCSI_PROT_READ_STRIP:
1733 case SCSI_PROT_READ_PASS:
1734 /*
1735 * For READ_STRIP and READ_PASS, force the
1736 * error on data being read off the wire. It
1737 * should force an IO error to the driver.
1738 */
James Smartacd68592012-01-18 16:25:09 -05001739 phba->lpfc_injerr_rgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001740 if (phba->lpfc_injerr_rgrd_cnt == 0) {
1741 phba->lpfc_injerr_nportid = 0;
1742 phba->lpfc_injerr_lba =
1743 LPFC_INJERR_LBA_OFF;
1744 memset(&phba->lpfc_injerr_wwpn,
1745 0, sizeof(struct lpfc_name));
1746 }
James Smartacd68592012-01-18 16:25:09 -05001747
James Smart9a6b09c2012-03-01 22:37:42 -05001748 rc = BG_ERR_INIT | BG_ERR_SWAP;
James Smartacd68592012-01-18 16:25:09 -05001749 /* Signals the caller to swap CRC->CSUM */
1750
1751 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1752 "0818 BLKGRD: Injecting guard error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001753 "read lba x%lx\n", (unsigned long)lba);
1754 }
1755 }
1756 }
1757
James Smartf9bb2da2011-10-10 21:34:11 -04001758 return rc;
1759}
1760#endif
1761
James Smartacd68592012-01-18 16:25:09 -05001762/**
1763 * lpfc_sc_to_bg_opcodes - Determine the BlockGuard opcodes to be used with
1764 * the specified SCSI command.
1765 * @phba: The Hba for which this call is being executed.
James Smart6c8eea52010-04-06 14:49:53 -04001766 * @sc: The SCSI command to examine
1767 * @txopt: (out) BlockGuard operation for transmitted data
1768 * @rxopt: (out) BlockGuard operation for received data
1769 *
1770 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1771 *
James Smartacd68592012-01-18 16:25:09 -05001772 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001773static int
James Smart6c8eea52010-04-06 14:49:53 -04001774lpfc_sc_to_bg_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1775 uint8_t *txop, uint8_t *rxop)
James Smarte2a0a9d2008-12-04 22:40:02 -05001776{
1777 uint8_t guard_type = scsi_host_get_guard(sc->device->host);
James Smart6c8eea52010-04-06 14:49:53 -04001778 uint8_t ret = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05001779
1780 if (guard_type == SHOST_DIX_GUARD_IP) {
1781 switch (scsi_get_prot_op(sc)) {
1782 case SCSI_PROT_READ_INSERT:
1783 case SCSI_PROT_WRITE_STRIP:
James Smart6c8eea52010-04-06 14:49:53 -04001784 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001785 *txop = BG_OP_IN_CSUM_OUT_NODIF;
James Smarte2a0a9d2008-12-04 22:40:02 -05001786 break;
1787
1788 case SCSI_PROT_READ_STRIP:
1789 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001790 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001791 *txop = BG_OP_IN_NODIF_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001792 break;
1793
Martin K. Petersenc6af4042009-09-18 17:32:59 -04001794 case SCSI_PROT_READ_PASS:
1795 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001796 *rxop = BG_OP_IN_CRC_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001797 *txop = BG_OP_IN_CSUM_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001798 break;
1799
James Smarte2a0a9d2008-12-04 22:40:02 -05001800 case SCSI_PROT_NORMAL:
1801 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001802 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001803 "9063 BLKGRD: Bad op/guard:%d/IP combination\n",
1804 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001805 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001806 break;
1807
1808 }
James Smart7c56b9f2011-07-22 18:36:25 -04001809 } else {
James Smarte2a0a9d2008-12-04 22:40:02 -05001810 switch (scsi_get_prot_op(sc)) {
1811 case SCSI_PROT_READ_STRIP:
1812 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001813 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001814 *txop = BG_OP_IN_NODIF_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001815 break;
1816
1817 case SCSI_PROT_READ_PASS:
1818 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001819 *rxop = BG_OP_IN_CRC_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001820 *txop = BG_OP_IN_CRC_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001821 break;
1822
James Smarte2a0a9d2008-12-04 22:40:02 -05001823 case SCSI_PROT_READ_INSERT:
1824 case SCSI_PROT_WRITE_STRIP:
James Smart7c56b9f2011-07-22 18:36:25 -04001825 *rxop = BG_OP_IN_NODIF_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001826 *txop = BG_OP_IN_CRC_OUT_NODIF;
James Smart7c56b9f2011-07-22 18:36:25 -04001827 break;
1828
James Smarte2a0a9d2008-12-04 22:40:02 -05001829 case SCSI_PROT_NORMAL:
1830 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001831 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001832 "9075 BLKGRD: Bad op/guard:%d/CRC combination\n",
1833 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001834 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001835 break;
1836 }
James Smarte2a0a9d2008-12-04 22:40:02 -05001837 }
1838
James Smart6c8eea52010-04-06 14:49:53 -04001839 return ret;
James Smarte2a0a9d2008-12-04 22:40:02 -05001840}
1841
James Smartacd68592012-01-18 16:25:09 -05001842#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1843/**
1844 * lpfc_bg_err_opcodes - reDetermine the BlockGuard opcodes to be used with
1845 * the specified SCSI command in order to force a guard tag error.
1846 * @phba: The Hba for which this call is being executed.
1847 * @sc: The SCSI command to examine
1848 * @txopt: (out) BlockGuard operation for transmitted data
1849 * @rxopt: (out) BlockGuard operation for received data
1850 *
1851 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1852 *
1853 **/
1854static int
1855lpfc_bg_err_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1856 uint8_t *txop, uint8_t *rxop)
1857{
1858 uint8_t guard_type = scsi_host_get_guard(sc->device->host);
1859 uint8_t ret = 0;
1860
1861 if (guard_type == SHOST_DIX_GUARD_IP) {
1862 switch (scsi_get_prot_op(sc)) {
1863 case SCSI_PROT_READ_INSERT:
1864 case SCSI_PROT_WRITE_STRIP:
James Smartacd68592012-01-18 16:25:09 -05001865 *rxop = BG_OP_IN_NODIF_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001866 *txop = BG_OP_IN_CRC_OUT_NODIF;
James Smartacd68592012-01-18 16:25:09 -05001867 break;
1868
1869 case SCSI_PROT_READ_STRIP:
1870 case SCSI_PROT_WRITE_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001871 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001872 *txop = BG_OP_IN_NODIF_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001873 break;
1874
1875 case SCSI_PROT_READ_PASS:
1876 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001877 *rxop = BG_OP_IN_CSUM_OUT_CRC;
James Smart9a6b09c2012-03-01 22:37:42 -05001878 *txop = BG_OP_IN_CRC_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001879 break;
1880
1881 case SCSI_PROT_NORMAL:
1882 default:
1883 break;
1884
1885 }
1886 } else {
1887 switch (scsi_get_prot_op(sc)) {
1888 case SCSI_PROT_READ_STRIP:
1889 case SCSI_PROT_WRITE_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001890 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001891 *txop = BG_OP_IN_NODIF_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001892 break;
1893
1894 case SCSI_PROT_READ_PASS:
1895 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001896 *rxop = BG_OP_IN_CSUM_OUT_CSUM;
James Smart9a6b09c2012-03-01 22:37:42 -05001897 *txop = BG_OP_IN_CSUM_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001898 break;
1899
1900 case SCSI_PROT_READ_INSERT:
1901 case SCSI_PROT_WRITE_STRIP:
James Smartacd68592012-01-18 16:25:09 -05001902 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001903 *txop = BG_OP_IN_CSUM_OUT_NODIF;
James Smartacd68592012-01-18 16:25:09 -05001904 break;
1905
1906 case SCSI_PROT_NORMAL:
1907 default:
1908 break;
1909 }
1910 }
1911
1912 return ret;
1913}
1914#endif
1915
1916/**
1917 * lpfc_bg_setup_bpl - Setup BlockGuard BPL with no protection data
1918 * @phba: The Hba for which this call is being executed.
1919 * @sc: pointer to scsi command we're working on
1920 * @bpl: pointer to buffer list for protection groups
1921 * @datacnt: number of segments of data that have been dma mapped
1922 *
1923 * This function sets up BPL buffer list for protection groups of
James Smarte2a0a9d2008-12-04 22:40:02 -05001924 * type LPFC_PG_TYPE_NO_DIF
1925 *
1926 * This is usually used when the HBA is instructed to generate
1927 * DIFs and insert them into data stream (or strip DIF from
1928 * incoming data stream)
1929 *
1930 * The buffer list consists of just one protection group described
1931 * below:
1932 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001933 * start of prot group --> | PDE_5 |
1934 * +-------------------------+
1935 * | PDE_6 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001936 * +-------------------------+
1937 * | Data BDE |
1938 * +-------------------------+
1939 * |more Data BDE's ... (opt)|
1940 * +-------------------------+
1941 *
James Smarte2a0a9d2008-12-04 22:40:02 -05001942 *
1943 * Note: Data s/g buffers have been dma mapped
James Smartacd68592012-01-18 16:25:09 -05001944 *
1945 * Returns the number of BDEs added to the BPL.
1946 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001947static int
1948lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1949 struct ulp_bde64 *bpl, int datasegcnt)
1950{
1951 struct scatterlist *sgde = NULL; /* s/g data entry */
James Smart6c8eea52010-04-06 14:49:53 -04001952 struct lpfc_pde5 *pde5 = NULL;
1953 struct lpfc_pde6 *pde6 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05001954 dma_addr_t physaddr;
James Smart6c8eea52010-04-06 14:49:53 -04001955 int i = 0, num_bde = 0, status;
James Smarte2a0a9d2008-12-04 22:40:02 -05001956 int datadir = sc->sc_data_direction;
James Smart0829a192012-05-09 21:18:12 -04001957#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001958 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04001959#endif
James Smartacd68592012-01-18 16:25:09 -05001960 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001961 uint32_t reftag;
James Smart7c56b9f2011-07-22 18:36:25 -04001962 unsigned blksize;
James Smart6c8eea52010-04-06 14:49:53 -04001963 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05001964
James Smart6c8eea52010-04-06 14:49:53 -04001965 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1966 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05001967 goto out;
1968
James Smart6c8eea52010-04-06 14:49:53 -04001969 /* extract some info from the scsi command for pde*/
James Smarte2a0a9d2008-12-04 22:40:02 -05001970 blksize = lpfc_cmd_blksize(sc);
James Smartacd68592012-01-18 16:25:09 -05001971 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
James Smarte2a0a9d2008-12-04 22:40:02 -05001972
James Smartf9bb2da2011-10-10 21:34:11 -04001973#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05001974 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05001975 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05001976 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05001977 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05001978 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05001979 checking = 0;
1980 }
James Smartf9bb2da2011-10-10 21:34:11 -04001981#endif
1982
James Smart6c8eea52010-04-06 14:49:53 -04001983 /* setup PDE5 with what we have */
1984 pde5 = (struct lpfc_pde5 *) bpl;
1985 memset(pde5, 0, sizeof(struct lpfc_pde5));
1986 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05001987
James Smartbc739052010-08-04 16:11:18 -04001988 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04001989 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04001990 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04001991
James Smart6c8eea52010-04-06 14:49:53 -04001992 /* advance bpl and increment bde count */
1993 num_bde++;
1994 bpl++;
1995 pde6 = (struct lpfc_pde6 *) bpl;
1996
1997 /* setup PDE6 with the rest of the info */
1998 memset(pde6, 0, sizeof(struct lpfc_pde6));
1999 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
2000 bf_set(pde6_optx, pde6, txop);
2001 bf_set(pde6_oprx, pde6, rxop);
2002 if (datadir == DMA_FROM_DEVICE) {
James Smartacd68592012-01-18 16:25:09 -05002003 bf_set(pde6_ce, pde6, checking);
2004 bf_set(pde6_re, pde6, checking);
James Smart6c8eea52010-04-06 14:49:53 -04002005 }
2006 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04002007 bf_set(pde6_ae, pde6, 0);
2008 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04002009
James Smartbc739052010-08-04 16:11:18 -04002010 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04002011 pde6->word0 = cpu_to_le32(pde6->word0);
2012 pde6->word1 = cpu_to_le32(pde6->word1);
2013 pde6->word2 = cpu_to_le32(pde6->word2);
2014
James Smart6c8eea52010-04-06 14:49:53 -04002015 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05002016 num_bde++;
2017 bpl++;
2018
2019 /* assumption: caller has already run dma_map_sg on command data */
2020 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
2021 physaddr = sg_dma_address(sgde);
2022 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
2023 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
2024 bpl->tus.f.bdeSize = sg_dma_len(sgde);
2025 if (datadir == DMA_TO_DEVICE)
2026 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2027 else
2028 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2029 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2030 bpl++;
2031 num_bde++;
2032 }
2033
2034out:
2035 return num_bde;
2036}
2037
James Smartacd68592012-01-18 16:25:09 -05002038/**
2039 * lpfc_bg_setup_bpl_prot - Setup BlockGuard BPL with protection data
2040 * @phba: The Hba for which this call is being executed.
2041 * @sc: pointer to scsi command we're working on
2042 * @bpl: pointer to buffer list for protection groups
2043 * @datacnt: number of segments of data that have been dma mapped
2044 * @protcnt: number of segment of protection data that have been dma mapped
2045 *
2046 * This function sets up BPL buffer list for protection groups of
2047 * type LPFC_PG_TYPE_DIF
James Smarte2a0a9d2008-12-04 22:40:02 -05002048 *
2049 * This is usually used when DIFs are in their own buffers,
2050 * separate from the data. The HBA can then by instructed
2051 * to place the DIFs in the outgoing stream. For read operations,
2052 * The HBA could extract the DIFs and place it in DIF buffers.
2053 *
2054 * The buffer list for this type consists of one or more of the
2055 * protection groups described below:
2056 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002057 * start of first prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05002058 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002059 * | PDE_6 |
2060 * +-------------------------+
2061 * | PDE_7 (Prot BDE) |
James Smarte2a0a9d2008-12-04 22:40:02 -05002062 * +-------------------------+
2063 * | Data BDE |
2064 * +-------------------------+
2065 * |more Data BDE's ... (opt)|
2066 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002067 * start of new prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05002068 * +-------------------------+
2069 * | ... |
2070 * +-------------------------+
2071 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002072 * Note: It is assumed that both data and protection s/g buffers have been
2073 * mapped for DMA
James Smartacd68592012-01-18 16:25:09 -05002074 *
2075 * Returns the number of BDEs added to the BPL.
2076 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002077static int
2078lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2079 struct ulp_bde64 *bpl, int datacnt, int protcnt)
2080{
2081 struct scatterlist *sgde = NULL; /* s/g data entry */
2082 struct scatterlist *sgpe = NULL; /* s/g prot entry */
James Smart6c8eea52010-04-06 14:49:53 -04002083 struct lpfc_pde5 *pde5 = NULL;
2084 struct lpfc_pde6 *pde6 = NULL;
James Smart7f860592011-03-11 16:05:52 -05002085 struct lpfc_pde7 *pde7 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05002086 dma_addr_t dataphysaddr, protphysaddr;
2087 unsigned short curr_data = 0, curr_prot = 0;
James Smart7f860592011-03-11 16:05:52 -05002088 unsigned int split_offset;
2089 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
James Smarte2a0a9d2008-12-04 22:40:02 -05002090 unsigned int protgrp_blks, protgrp_bytes;
2091 unsigned int remainder, subtotal;
James Smart6c8eea52010-04-06 14:49:53 -04002092 int status;
James Smarte2a0a9d2008-12-04 22:40:02 -05002093 int datadir = sc->sc_data_direction;
2094 unsigned char pgdone = 0, alldone = 0;
2095 unsigned blksize;
James Smart0829a192012-05-09 21:18:12 -04002096#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002097 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002098#endif
James Smartacd68592012-01-18 16:25:09 -05002099 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05002100 uint32_t reftag;
James Smart6c8eea52010-04-06 14:49:53 -04002101 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05002102 int num_bde = 0;
2103
2104 sgpe = scsi_prot_sglist(sc);
2105 sgde = scsi_sglist(sc);
2106
2107 if (!sgpe || !sgde) {
2108 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2109 "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
2110 sgpe, sgde);
2111 return 0;
2112 }
2113
James Smart6c8eea52010-04-06 14:49:53 -04002114 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2115 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05002116 goto out;
2117
James Smart6c8eea52010-04-06 14:49:53 -04002118 /* extract some info from the scsi command */
James Smarte2a0a9d2008-12-04 22:40:02 -05002119 blksize = lpfc_cmd_blksize(sc);
James Smartacd68592012-01-18 16:25:09 -05002120 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
James Smarte2a0a9d2008-12-04 22:40:02 -05002121
James Smartf9bb2da2011-10-10 21:34:11 -04002122#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002123 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002124 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002125 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002126 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002127 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002128 checking = 0;
2129 }
James Smartf9bb2da2011-10-10 21:34:11 -04002130#endif
2131
James Smarte2a0a9d2008-12-04 22:40:02 -05002132 split_offset = 0;
2133 do {
James Smart6c8eea52010-04-06 14:49:53 -04002134 /* setup PDE5 with what we have */
2135 pde5 = (struct lpfc_pde5 *) bpl;
2136 memset(pde5, 0, sizeof(struct lpfc_pde5));
2137 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05002138
James Smartbc739052010-08-04 16:11:18 -04002139 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04002140 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04002141 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04002142
James Smart6c8eea52010-04-06 14:49:53 -04002143 /* advance bpl and increment bde count */
2144 num_bde++;
2145 bpl++;
2146 pde6 = (struct lpfc_pde6 *) bpl;
James Smarte2a0a9d2008-12-04 22:40:02 -05002147
James Smart6c8eea52010-04-06 14:49:53 -04002148 /* setup PDE6 with the rest of the info */
2149 memset(pde6, 0, sizeof(struct lpfc_pde6));
2150 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
2151 bf_set(pde6_optx, pde6, txop);
2152 bf_set(pde6_oprx, pde6, rxop);
James Smartacd68592012-01-18 16:25:09 -05002153 bf_set(pde6_ce, pde6, checking);
2154 bf_set(pde6_re, pde6, checking);
James Smart6c8eea52010-04-06 14:49:53 -04002155 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04002156 bf_set(pde6_ae, pde6, 0);
2157 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04002158
James Smartbc739052010-08-04 16:11:18 -04002159 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04002160 pde6->word0 = cpu_to_le32(pde6->word0);
2161 pde6->word1 = cpu_to_le32(pde6->word1);
2162 pde6->word2 = cpu_to_le32(pde6->word2);
2163
James Smart6c8eea52010-04-06 14:49:53 -04002164 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05002165 num_bde++;
2166 bpl++;
2167
2168 /* setup the first BDE that points to protection buffer */
James Smart7f860592011-03-11 16:05:52 -05002169 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2170 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
James Smarte2a0a9d2008-12-04 22:40:02 -05002171
James Smarte2a0a9d2008-12-04 22:40:02 -05002172 /* must be integer multiple of the DIF block length */
2173 BUG_ON(protgroup_len % 8);
2174
James Smart7f860592011-03-11 16:05:52 -05002175 pde7 = (struct lpfc_pde7 *) bpl;
2176 memset(pde7, 0, sizeof(struct lpfc_pde7));
2177 bf_set(pde7_type, pde7, LPFC_PDE7_DESCRIPTOR);
2178
James Smart7c56b9f2011-07-22 18:36:25 -04002179 pde7->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
2180 pde7->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
James Smart7f860592011-03-11 16:05:52 -05002181
James Smarte2a0a9d2008-12-04 22:40:02 -05002182 protgrp_blks = protgroup_len / 8;
2183 protgrp_bytes = protgrp_blks * blksize;
2184
James Smart7f860592011-03-11 16:05:52 -05002185 /* check if this pde is crossing the 4K boundary; if so split */
2186 if ((pde7->addrLow & 0xfff) + protgroup_len > 0x1000) {
2187 protgroup_remainder = 0x1000 - (pde7->addrLow & 0xfff);
2188 protgroup_offset += protgroup_remainder;
2189 protgrp_blks = protgroup_remainder / 8;
James Smart7c56b9f2011-07-22 18:36:25 -04002190 protgrp_bytes = protgrp_blks * blksize;
James Smart7f860592011-03-11 16:05:52 -05002191 } else {
2192 protgroup_offset = 0;
2193 curr_prot++;
2194 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002195
James Smarte2a0a9d2008-12-04 22:40:02 -05002196 num_bde++;
2197
2198 /* setup BDE's for data blocks associated with DIF data */
2199 pgdone = 0;
2200 subtotal = 0; /* total bytes processed for current prot grp */
2201 while (!pgdone) {
2202 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -04002203 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2204 "9065 BLKGRD:%s Invalid data segment\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002205 __func__);
2206 return 0;
2207 }
2208 bpl++;
2209 dataphysaddr = sg_dma_address(sgde) + split_offset;
2210 bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
2211 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
2212
2213 remainder = sg_dma_len(sgde) - split_offset;
2214
2215 if ((subtotal + remainder) <= protgrp_bytes) {
2216 /* we can use this whole buffer */
2217 bpl->tus.f.bdeSize = remainder;
2218 split_offset = 0;
2219
2220 if ((subtotal + remainder) == protgrp_bytes)
2221 pgdone = 1;
2222 } else {
2223 /* must split this buffer with next prot grp */
2224 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
2225 split_offset += bpl->tus.f.bdeSize;
2226 }
2227
2228 subtotal += bpl->tus.f.bdeSize;
2229
2230 if (datadir == DMA_TO_DEVICE)
2231 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2232 else
2233 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2234 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2235
2236 num_bde++;
2237 curr_data++;
2238
2239 if (split_offset)
2240 break;
2241
2242 /* Move to the next s/g segment if possible */
2243 sgde = sg_next(sgde);
James Smart6c8eea52010-04-06 14:49:53 -04002244
James Smarte2a0a9d2008-12-04 22:40:02 -05002245 }
2246
James Smart7f860592011-03-11 16:05:52 -05002247 if (protgroup_offset) {
2248 /* update the reference tag */
2249 reftag += protgrp_blks;
2250 bpl++;
2251 continue;
2252 }
2253
James Smarte2a0a9d2008-12-04 22:40:02 -05002254 /* are we done ? */
2255 if (curr_prot == protcnt) {
2256 alldone = 1;
2257 } else if (curr_prot < protcnt) {
2258 /* advance to next prot buffer */
2259 sgpe = sg_next(sgpe);
2260 bpl++;
2261
2262 /* update the reference tag */
2263 reftag += protgrp_blks;
2264 } else {
2265 /* if we're here, we have a bug */
James Smart6a9c52c2009-10-02 15:16:51 -04002266 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2267 "9054 BLKGRD: bug in %s\n", __func__);
James Smarte2a0a9d2008-12-04 22:40:02 -05002268 }
2269
2270 } while (!alldone);
James Smarte2a0a9d2008-12-04 22:40:02 -05002271out:
2272
James Smarte2a0a9d2008-12-04 22:40:02 -05002273 return num_bde;
2274}
James Smart7f860592011-03-11 16:05:52 -05002275
James Smartacd68592012-01-18 16:25:09 -05002276/**
2277 * lpfc_bg_setup_sgl - Setup BlockGuard SGL with no protection data
2278 * @phba: The Hba for which this call is being executed.
2279 * @sc: pointer to scsi command we're working on
2280 * @sgl: pointer to buffer list for protection groups
2281 * @datacnt: number of segments of data that have been dma mapped
2282 *
2283 * This function sets up SGL buffer list for protection groups of
2284 * type LPFC_PG_TYPE_NO_DIF
2285 *
2286 * This is usually used when the HBA is instructed to generate
2287 * DIFs and insert them into data stream (or strip DIF from
2288 * incoming data stream)
2289 *
2290 * The buffer list consists of just one protection group described
2291 * below:
2292 * +-------------------------+
2293 * start of prot group --> | DI_SEED |
2294 * +-------------------------+
2295 * | Data SGE |
2296 * +-------------------------+
2297 * |more Data SGE's ... (opt)|
2298 * +-------------------------+
2299 *
2300 *
2301 * Note: Data s/g buffers have been dma mapped
2302 *
2303 * Returns the number of SGEs added to the SGL.
2304 **/
2305static int
2306lpfc_bg_setup_sgl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2307 struct sli4_sge *sgl, int datasegcnt)
2308{
2309 struct scatterlist *sgde = NULL; /* s/g data entry */
2310 struct sli4_sge_diseed *diseed = NULL;
2311 dma_addr_t physaddr;
2312 int i = 0, num_sge = 0, status;
2313 int datadir = sc->sc_data_direction;
2314 uint32_t reftag;
2315 unsigned blksize;
2316 uint8_t txop, rxop;
James Smart0829a192012-05-09 21:18:12 -04002317#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002318 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002319#endif
James Smartacd68592012-01-18 16:25:09 -05002320 uint32_t checking = 1;
2321 uint32_t dma_len;
2322 uint32_t dma_offset = 0;
2323
2324 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2325 if (status)
2326 goto out;
2327
2328 /* extract some info from the scsi command for pde*/
2329 blksize = lpfc_cmd_blksize(sc);
2330 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2331
2332#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002333 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002334 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002335 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002336 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002337 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002338 checking = 0;
2339 }
2340#endif
2341
2342 /* setup DISEED with what we have */
2343 diseed = (struct sli4_sge_diseed *) sgl;
2344 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2345 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2346
2347 /* Endianness conversion if necessary */
2348 diseed->ref_tag = cpu_to_le32(reftag);
2349 diseed->ref_tag_tran = diseed->ref_tag;
2350
2351 /* setup DISEED with the rest of the info */
2352 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2353 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
2354 if (datadir == DMA_FROM_DEVICE) {
2355 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2356 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2357 }
2358 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2359 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2360
2361 /* Endianness conversion if necessary for DISEED */
2362 diseed->word2 = cpu_to_le32(diseed->word2);
2363 diseed->word3 = cpu_to_le32(diseed->word3);
2364
2365 /* advance bpl and increment sge count */
2366 num_sge++;
2367 sgl++;
2368
2369 /* assumption: caller has already run dma_map_sg on command data */
2370 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
2371 physaddr = sg_dma_address(sgde);
2372 dma_len = sg_dma_len(sgde);
2373 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
2374 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
2375 if ((i + 1) == datasegcnt)
2376 bf_set(lpfc_sli4_sge_last, sgl, 1);
2377 else
2378 bf_set(lpfc_sli4_sge_last, sgl, 0);
2379 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2380 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2381
2382 sgl->sge_len = cpu_to_le32(dma_len);
2383 dma_offset += dma_len;
2384
2385 sgl++;
2386 num_sge++;
2387 }
2388
2389out:
2390 return num_sge;
2391}
2392
2393/**
2394 * lpfc_bg_setup_sgl_prot - Setup BlockGuard SGL with protection data
2395 * @phba: The Hba for which this call is being executed.
2396 * @sc: pointer to scsi command we're working on
2397 * @sgl: pointer to buffer list for protection groups
2398 * @datacnt: number of segments of data that have been dma mapped
2399 * @protcnt: number of segment of protection data that have been dma mapped
2400 *
2401 * This function sets up SGL buffer list for protection groups of
2402 * type LPFC_PG_TYPE_DIF
2403 *
2404 * This is usually used when DIFs are in their own buffers,
2405 * separate from the data. The HBA can then by instructed
2406 * to place the DIFs in the outgoing stream. For read operations,
2407 * The HBA could extract the DIFs and place it in DIF buffers.
2408 *
2409 * The buffer list for this type consists of one or more of the
2410 * protection groups described below:
2411 * +-------------------------+
2412 * start of first prot group --> | DISEED |
2413 * +-------------------------+
2414 * | DIF (Prot SGE) |
2415 * +-------------------------+
2416 * | Data SGE |
2417 * +-------------------------+
2418 * |more Data SGE's ... (opt)|
2419 * +-------------------------+
2420 * start of new prot group --> | DISEED |
2421 * +-------------------------+
2422 * | ... |
2423 * +-------------------------+
2424 *
2425 * Note: It is assumed that both data and protection s/g buffers have been
2426 * mapped for DMA
2427 *
2428 * Returns the number of SGEs added to the SGL.
2429 **/
2430static int
2431lpfc_bg_setup_sgl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2432 struct sli4_sge *sgl, int datacnt, int protcnt)
2433{
2434 struct scatterlist *sgde = NULL; /* s/g data entry */
2435 struct scatterlist *sgpe = NULL; /* s/g prot entry */
2436 struct sli4_sge_diseed *diseed = NULL;
2437 dma_addr_t dataphysaddr, protphysaddr;
2438 unsigned short curr_data = 0, curr_prot = 0;
2439 unsigned int split_offset;
2440 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
2441 unsigned int protgrp_blks, protgrp_bytes;
2442 unsigned int remainder, subtotal;
2443 int status;
2444 unsigned char pgdone = 0, alldone = 0;
2445 unsigned blksize;
2446 uint32_t reftag;
2447 uint8_t txop, rxop;
2448 uint32_t dma_len;
James Smart0829a192012-05-09 21:18:12 -04002449#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002450 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002451#endif
James Smartacd68592012-01-18 16:25:09 -05002452 uint32_t checking = 1;
2453 uint32_t dma_offset = 0;
2454 int num_sge = 0;
2455
2456 sgpe = scsi_prot_sglist(sc);
2457 sgde = scsi_sglist(sc);
2458
2459 if (!sgpe || !sgde) {
2460 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2461 "9082 Invalid s/g entry: data=0x%p prot=0x%p\n",
2462 sgpe, sgde);
2463 return 0;
2464 }
2465
2466 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2467 if (status)
2468 goto out;
2469
2470 /* extract some info from the scsi command */
2471 blksize = lpfc_cmd_blksize(sc);
2472 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2473
2474#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002475 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002476 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002477 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002478 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002479 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002480 checking = 0;
2481 }
2482#endif
2483
2484 split_offset = 0;
2485 do {
2486 /* setup DISEED with what we have */
2487 diseed = (struct sli4_sge_diseed *) sgl;
2488 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2489 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2490
2491 /* Endianness conversion if necessary */
2492 diseed->ref_tag = cpu_to_le32(reftag);
2493 diseed->ref_tag_tran = diseed->ref_tag;
2494
2495 /* setup DISEED with the rest of the info */
2496 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2497 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
2498 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2499 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2500 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2501 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2502
2503 /* Endianness conversion if necessary for DISEED */
2504 diseed->word2 = cpu_to_le32(diseed->word2);
2505 diseed->word3 = cpu_to_le32(diseed->word3);
2506
2507 /* advance sgl and increment bde count */
2508 num_sge++;
2509 sgl++;
2510
2511 /* setup the first BDE that points to protection buffer */
2512 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2513 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
2514
2515 /* must be integer multiple of the DIF block length */
2516 BUG_ON(protgroup_len % 8);
2517
2518 /* Now setup DIF SGE */
2519 sgl->word2 = 0;
2520 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DIF);
2521 sgl->addr_hi = le32_to_cpu(putPaddrHigh(protphysaddr));
2522 sgl->addr_lo = le32_to_cpu(putPaddrLow(protphysaddr));
2523 sgl->word2 = cpu_to_le32(sgl->word2);
2524
2525 protgrp_blks = protgroup_len / 8;
2526 protgrp_bytes = protgrp_blks * blksize;
2527
2528 /* check if DIF SGE is crossing the 4K boundary; if so split */
2529 if ((sgl->addr_lo & 0xfff) + protgroup_len > 0x1000) {
2530 protgroup_remainder = 0x1000 - (sgl->addr_lo & 0xfff);
2531 protgroup_offset += protgroup_remainder;
2532 protgrp_blks = protgroup_remainder / 8;
2533 protgrp_bytes = protgrp_blks * blksize;
2534 } else {
2535 protgroup_offset = 0;
2536 curr_prot++;
2537 }
2538
2539 num_sge++;
2540
2541 /* setup SGE's for data blocks associated with DIF data */
2542 pgdone = 0;
2543 subtotal = 0; /* total bytes processed for current prot grp */
2544 while (!pgdone) {
2545 if (!sgde) {
2546 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2547 "9086 BLKGRD:%s Invalid data segment\n",
2548 __func__);
2549 return 0;
2550 }
2551 sgl++;
2552 dataphysaddr = sg_dma_address(sgde) + split_offset;
2553
2554 remainder = sg_dma_len(sgde) - split_offset;
2555
2556 if ((subtotal + remainder) <= protgrp_bytes) {
2557 /* we can use this whole buffer */
2558 dma_len = remainder;
2559 split_offset = 0;
2560
2561 if ((subtotal + remainder) == protgrp_bytes)
2562 pgdone = 1;
2563 } else {
2564 /* must split this buffer with next prot grp */
2565 dma_len = protgrp_bytes - subtotal;
2566 split_offset += dma_len;
2567 }
2568
2569 subtotal += dma_len;
2570
2571 sgl->addr_lo = cpu_to_le32(putPaddrLow(dataphysaddr));
2572 sgl->addr_hi = cpu_to_le32(putPaddrHigh(dataphysaddr));
2573 bf_set(lpfc_sli4_sge_last, sgl, 0);
2574 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2575 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2576
2577 sgl->sge_len = cpu_to_le32(dma_len);
2578 dma_offset += dma_len;
2579
2580 num_sge++;
2581 curr_data++;
2582
2583 if (split_offset)
2584 break;
2585
2586 /* Move to the next s/g segment if possible */
2587 sgde = sg_next(sgde);
2588 }
2589
2590 if (protgroup_offset) {
2591 /* update the reference tag */
2592 reftag += protgrp_blks;
2593 sgl++;
2594 continue;
2595 }
2596
2597 /* are we done ? */
2598 if (curr_prot == protcnt) {
2599 bf_set(lpfc_sli4_sge_last, sgl, 1);
2600 alldone = 1;
2601 } else if (curr_prot < protcnt) {
2602 /* advance to next prot buffer */
2603 sgpe = sg_next(sgpe);
2604 sgl++;
2605
2606 /* update the reference tag */
2607 reftag += protgrp_blks;
2608 } else {
2609 /* if we're here, we have a bug */
2610 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2611 "9085 BLKGRD: bug in %s\n", __func__);
2612 }
2613
2614 } while (!alldone);
2615
2616out:
2617
2618 return num_sge;
2619}
2620
2621/**
2622 * lpfc_prot_group_type - Get prtotection group type of SCSI command
2623 * @phba: The Hba for which this call is being executed.
2624 * @sc: pointer to scsi command we're working on
2625 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002626 * Given a SCSI command that supports DIF, determine composition of protection
2627 * groups involved in setting up buffer lists
2628 *
James Smartacd68592012-01-18 16:25:09 -05002629 * Returns: Protection group type (with or without DIF)
2630 *
2631 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002632static int
2633lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
2634{
2635 int ret = LPFC_PG_TYPE_INVALID;
2636 unsigned char op = scsi_get_prot_op(sc);
2637
2638 switch (op) {
2639 case SCSI_PROT_READ_STRIP:
2640 case SCSI_PROT_WRITE_INSERT:
2641 ret = LPFC_PG_TYPE_NO_DIF;
2642 break;
2643 case SCSI_PROT_READ_INSERT:
2644 case SCSI_PROT_WRITE_STRIP:
2645 case SCSI_PROT_READ_PASS:
2646 case SCSI_PROT_WRITE_PASS:
James Smarte2a0a9d2008-12-04 22:40:02 -05002647 ret = LPFC_PG_TYPE_DIF_BUF;
2648 break;
2649 default:
2650 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2651 "9021 Unsupported protection op:%d\n", op);
2652 break;
2653 }
2654
2655 return ret;
2656}
2657
James Smartacd68592012-01-18 16:25:09 -05002658/**
2659 * lpfc_bg_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
2660 * @phba: The Hba for which this call is being executed.
2661 * @lpfc_cmd: The scsi buffer which is going to be prep'ed.
2662 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002663 * This is the protection/DIF aware version of
2664 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
2665 * two functions eventually, but for now, it's here
James Smartacd68592012-01-18 16:25:09 -05002666 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002667static int
James Smartacd68592012-01-18 16:25:09 -05002668lpfc_bg_scsi_prep_dma_buf_s3(struct lpfc_hba *phba,
James Smarte2a0a9d2008-12-04 22:40:02 -05002669 struct lpfc_scsi_buf *lpfc_cmd)
2670{
2671 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2672 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2673 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
2674 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2675 uint32_t num_bde = 0;
2676 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
2677 int prot_group_type = 0;
2678 int diflen, fcpdl;
2679 unsigned blksize;
2680
2681 /*
2682 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
2683 * fcp_rsp regions to the first data bde entry
2684 */
2685 bpl += 2;
2686 if (scsi_sg_count(scsi_cmnd)) {
2687 /*
2688 * The driver stores the segment count returned from pci_map_sg
2689 * because this a count of dma-mappings used to map the use_sg
2690 * pages. They are not guaranteed to be the same for those
2691 * architectures that implement an IOMMU.
2692 */
2693 datasegcnt = dma_map_sg(&phba->pcidev->dev,
2694 scsi_sglist(scsi_cmnd),
2695 scsi_sg_count(scsi_cmnd), datadir);
2696 if (unlikely(!datasegcnt))
2697 return 1;
2698
2699 lpfc_cmd->seg_cnt = datasegcnt;
2700 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04002701 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2702 "9067 BLKGRD: %s: Too many sg segments"
2703 " from dma_map_sg. Config %d, seg_cnt"
2704 " %d\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002705 __func__, phba->cfg_sg_seg_cnt,
2706 lpfc_cmd->seg_cnt);
2707 scsi_dma_unmap(scsi_cmnd);
2708 return 1;
2709 }
2710
2711 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
2712
2713 switch (prot_group_type) {
2714 case LPFC_PG_TYPE_NO_DIF:
2715 num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
2716 datasegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002717 /* we should have 2 or more entries in buffer list */
James Smarte2a0a9d2008-12-04 22:40:02 -05002718 if (num_bde < 2)
2719 goto err;
2720 break;
2721 case LPFC_PG_TYPE_DIF_BUF:{
2722 /*
2723 * This type indicates that protection buffers are
2724 * passed to the driver, so that needs to be prepared
2725 * for DMA
2726 */
2727 protsegcnt = dma_map_sg(&phba->pcidev->dev,
2728 scsi_prot_sglist(scsi_cmnd),
2729 scsi_prot_sg_count(scsi_cmnd), datadir);
2730 if (unlikely(!protsegcnt)) {
2731 scsi_dma_unmap(scsi_cmnd);
2732 return 1;
2733 }
2734
2735 lpfc_cmd->prot_seg_cnt = protsegcnt;
2736 if (lpfc_cmd->prot_seg_cnt
2737 > phba->cfg_prot_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04002738 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2739 "9068 BLKGRD: %s: Too many prot sg "
2740 "segments from dma_map_sg. Config %d,"
James Smarte2a0a9d2008-12-04 22:40:02 -05002741 "prot_seg_cnt %d\n", __func__,
2742 phba->cfg_prot_sg_seg_cnt,
2743 lpfc_cmd->prot_seg_cnt);
2744 dma_unmap_sg(&phba->pcidev->dev,
2745 scsi_prot_sglist(scsi_cmnd),
2746 scsi_prot_sg_count(scsi_cmnd),
2747 datadir);
2748 scsi_dma_unmap(scsi_cmnd);
2749 return 1;
2750 }
2751
2752 num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
2753 datasegcnt, protsegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002754 /* we should have 3 or more entries in buffer list */
James Smarte2a0a9d2008-12-04 22:40:02 -05002755 if (num_bde < 3)
2756 goto err;
2757 break;
2758 }
2759 case LPFC_PG_TYPE_INVALID:
2760 default:
2761 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2762 "9022 Unexpected protection group %i\n",
2763 prot_group_type);
2764 return 1;
2765 }
2766 }
2767
2768 /*
2769 * Finish initializing those IOCB fields that are dependent on the
2770 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
2771 * reinitialized since all iocb memory resources are used many times
2772 * for transmit, receive, and continuation bpl's.
2773 */
2774 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
2775 iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
2776 iocb_cmd->ulpBdeCount = 1;
2777 iocb_cmd->ulpLe = 1;
2778
2779 fcpdl = scsi_bufflen(scsi_cmnd);
2780
2781 if (scsi_get_prot_type(scsi_cmnd) == SCSI_PROT_DIF_TYPE1) {
2782 /*
2783 * We are in DIF Type 1 mode
2784 * Every data block has a 8 byte DIF (trailer)
2785 * attached to it. Must ajust FCP data length
2786 */
2787 blksize = lpfc_cmd_blksize(scsi_cmnd);
2788 diflen = (fcpdl / blksize) * 8;
2789 fcpdl += diflen;
2790 }
2791 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
2792
2793 /*
2794 * Due to difference in data length between DIF/non-DIF paths,
2795 * we need to set word 4 of IOCB here
2796 */
2797 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
2798
2799 return 0;
2800err:
2801 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2802 "9023 Could not setup all needed BDE's"
2803 "prot_group_type=%d, num_bde=%d\n",
2804 prot_group_type, num_bde);
2805 return 1;
2806}
2807
2808/*
2809 * This function checks for BlockGuard errors detected by
2810 * the HBA. In case of errors, the ASC/ASCQ fields in the
2811 * sense buffer will be set accordingly, paired with
2812 * ILLEGAL_REQUEST to signal to the kernel that the HBA
2813 * detected corruption.
2814 *
2815 * Returns:
2816 * 0 - No error found
2817 * 1 - BlockGuard error found
2818 * -1 - Internal error (bad profile, ...etc)
2819 */
2820static int
2821lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
2822 struct lpfc_iocbq *pIocbOut)
2823{
2824 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
2825 struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
2826 int ret = 0;
2827 uint32_t bghm = bgf->bghm;
2828 uint32_t bgstat = bgf->bgstat;
2829 uint64_t failing_sector = 0;
2830
James Smart6a9c52c2009-10-02 15:16:51 -04002831 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9069 BLKGRD: BG ERROR in cmd"
2832 " 0x%x lba 0x%llx blk cnt 0x%x "
James Smarte2a0a9d2008-12-04 22:40:02 -05002833 "bgstat=0x%x bghm=0x%x\n",
James Smart87b5c322008-12-16 10:34:09 -05002834 cmd->cmnd[0], (unsigned long long)scsi_get_lba(cmd),
Tejun Heo83096eb2009-05-07 22:24:39 +09002835 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05002836
2837 spin_lock(&_dump_buf_lock);
2838 if (!_dump_buf_done) {
James Smart6a9c52c2009-10-02 15:16:51 -04002839 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9070 BLKGRD: Saving"
2840 " Data for %u blocks to debugfs\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002841 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
James Smart6a9c52c2009-10-02 15:16:51 -04002842 lpfc_debug_save_data(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002843
2844 /* If we have a prot sgl, save the DIF buffer */
2845 if (lpfc_prot_group_type(phba, cmd) ==
2846 LPFC_PG_TYPE_DIF_BUF) {
James Smart6a9c52c2009-10-02 15:16:51 -04002847 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9071 BLKGRD: "
2848 "Saving DIF for %u blocks to debugfs\n",
2849 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
2850 lpfc_debug_save_dif(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002851 }
2852
2853 _dump_buf_done = 1;
2854 }
2855 spin_unlock(&_dump_buf_lock);
2856
2857 if (lpfc_bgs_get_invalid_prof(bgstat)) {
2858 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart6a9c52c2009-10-02 15:16:51 -04002859 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9072 BLKGRD: Invalid"
2860 " BlockGuard profile. bgstat:0x%x\n",
2861 bgstat);
James Smarte2a0a9d2008-12-04 22:40:02 -05002862 ret = (-1);
2863 goto out;
2864 }
2865
2866 if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
2867 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart6a9c52c2009-10-02 15:16:51 -04002868 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9073 BLKGRD: "
2869 "Invalid BlockGuard DIF Block. bgstat:0x%x\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002870 bgstat);
2871 ret = (-1);
2872 goto out;
2873 }
2874
2875 if (lpfc_bgs_get_guard_err(bgstat)) {
2876 ret = 1;
2877
2878 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2879 0x10, 0x1);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05002880 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05002881 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
2882 phba->bg_guard_err_cnt++;
James Smart6a9c52c2009-10-02 15:16:51 -04002883 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2884 "9055 BLKGRD: guard_tag error\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05002885 }
2886
2887 if (lpfc_bgs_get_reftag_err(bgstat)) {
2888 ret = 1;
2889
2890 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2891 0x10, 0x3);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05002892 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05002893 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
2894
2895 phba->bg_reftag_err_cnt++;
James Smart6a9c52c2009-10-02 15:16:51 -04002896 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2897 "9056 BLKGRD: ref_tag error\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05002898 }
2899
2900 if (lpfc_bgs_get_apptag_err(bgstat)) {
2901 ret = 1;
2902
2903 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2904 0x10, 0x2);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05002905 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05002906 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
2907
2908 phba->bg_apptag_err_cnt++;
James Smart6a9c52c2009-10-02 15:16:51 -04002909 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2910 "9061 BLKGRD: app_tag error\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05002911 }
2912
2913 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
2914 /*
2915 * setup sense data descriptor 0 per SPC-4 as an information
James Smart7c56b9f2011-07-22 18:36:25 -04002916 * field, and put the failing LBA in it.
2917 * This code assumes there was also a guard/app/ref tag error
2918 * indication.
James Smarte2a0a9d2008-12-04 22:40:02 -05002919 */
James Smart7c56b9f2011-07-22 18:36:25 -04002920 cmd->sense_buffer[7] = 0xc; /* Additional sense length */
2921 cmd->sense_buffer[8] = 0; /* Information descriptor type */
2922 cmd->sense_buffer[9] = 0xa; /* Additional descriptor length */
2923 cmd->sense_buffer[10] = 0x80; /* Validity bit */
James Smartacd68592012-01-18 16:25:09 -05002924
2925 /* bghm is a "on the wire" FC frame based count */
2926 switch (scsi_get_prot_op(cmd)) {
2927 case SCSI_PROT_READ_INSERT:
2928 case SCSI_PROT_WRITE_STRIP:
2929 bghm /= cmd->device->sector_size;
2930 break;
2931 case SCSI_PROT_READ_STRIP:
2932 case SCSI_PROT_WRITE_INSERT:
2933 case SCSI_PROT_READ_PASS:
2934 case SCSI_PROT_WRITE_PASS:
2935 bghm /= (cmd->device->sector_size +
2936 sizeof(struct scsi_dif_tuple));
2937 break;
2938 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002939
2940 failing_sector = scsi_get_lba(cmd);
2941 failing_sector += bghm;
2942
James Smart7c56b9f2011-07-22 18:36:25 -04002943 /* Descriptor Information */
2944 put_unaligned_be64(failing_sector, &cmd->sense_buffer[12]);
James Smarte2a0a9d2008-12-04 22:40:02 -05002945 }
2946
2947 if (!ret) {
2948 /* No error was reported - problem in FW? */
2949 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart6a9c52c2009-10-02 15:16:51 -04002950 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart4ac9b222012-03-01 22:38:29 -05002951 "9057 BLKGRD: Unknown error reported!\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05002952 }
2953
2954out:
2955 return ret;
2956}
2957
James Smartea2151b2008-09-07 11:52:10 -04002958/**
James Smartda0436e2009-05-22 14:51:39 -04002959 * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
2960 * @phba: The Hba for which this call is being executed.
2961 * @lpfc_cmd: The scsi buffer which is going to be mapped.
2962 *
2963 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
2964 * field of @lpfc_cmd for device with SLI-4 interface spec.
2965 *
2966 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04002967 * 1 - Error
2968 * 0 - Success
James Smartda0436e2009-05-22 14:51:39 -04002969 **/
2970static int
2971lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
2972{
2973 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2974 struct scatterlist *sgel = NULL;
2975 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2976 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
James Smartfedd3b72011-02-16 12:39:24 -05002977 struct sli4_sge *first_data_sgl;
James Smartda0436e2009-05-22 14:51:39 -04002978 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2979 dma_addr_t physaddr;
2980 uint32_t num_bde = 0;
2981 uint32_t dma_len;
2982 uint32_t dma_offset = 0;
2983 int nseg;
James Smartfedd3b72011-02-16 12:39:24 -05002984 struct ulp_bde64 *bde;
James Smartda0436e2009-05-22 14:51:39 -04002985
2986 /*
2987 * There are three possibilities here - use scatter-gather segment, use
2988 * the single mapping, or neither. Start the lpfc command prep by
2989 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
2990 * data bde entry.
2991 */
2992 if (scsi_sg_count(scsi_cmnd)) {
2993 /*
2994 * The driver stores the segment count returned from pci_map_sg
2995 * because this a count of dma-mappings used to map the use_sg
2996 * pages. They are not guaranteed to be the same for those
2997 * architectures that implement an IOMMU.
2998 */
2999
3000 nseg = scsi_dma_map(scsi_cmnd);
3001 if (unlikely(!nseg))
3002 return 1;
3003 sgl += 1;
3004 /* clear the last flag in the fcp_rsp map entry */
3005 sgl->word2 = le32_to_cpu(sgl->word2);
3006 bf_set(lpfc_sli4_sge_last, sgl, 0);
3007 sgl->word2 = cpu_to_le32(sgl->word2);
3008 sgl += 1;
James Smartfedd3b72011-02-16 12:39:24 -05003009 first_data_sgl = sgl;
James Smartda0436e2009-05-22 14:51:39 -04003010 lpfc_cmd->seg_cnt = nseg;
3011 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04003012 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
3013 " %s: Too many sg segments from "
3014 "dma_map_sg. Config %d, seg_cnt %d\n",
3015 __func__, phba->cfg_sg_seg_cnt,
James Smartda0436e2009-05-22 14:51:39 -04003016 lpfc_cmd->seg_cnt);
3017 scsi_dma_unmap(scsi_cmnd);
3018 return 1;
3019 }
3020
3021 /*
3022 * The driver established a maximum scatter-gather segment count
3023 * during probe that limits the number of sg elements in any
3024 * single scsi command. Just run through the seg_cnt and format
3025 * the sge's.
3026 * When using SLI-3 the driver will try to fit all the BDEs into
3027 * the IOCB. If it can't then the BDEs get added to a BPL as it
3028 * does for SLI-2 mode.
3029 */
3030 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
3031 physaddr = sg_dma_address(sgel);
3032 dma_len = sg_dma_len(sgel);
James Smartda0436e2009-05-22 14:51:39 -04003033 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
3034 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
James Smart05580562011-05-24 11:40:48 -04003035 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -04003036 if ((num_bde + 1) == nseg)
3037 bf_set(lpfc_sli4_sge_last, sgl, 1);
3038 else
3039 bf_set(lpfc_sli4_sge_last, sgl, 0);
3040 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
James Smartf9bb2da2011-10-10 21:34:11 -04003041 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
James Smartda0436e2009-05-22 14:51:39 -04003042 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05003043 sgl->sge_len = cpu_to_le32(dma_len);
James Smartda0436e2009-05-22 14:51:39 -04003044 dma_offset += dma_len;
3045 sgl++;
3046 }
James Smartfedd3b72011-02-16 12:39:24 -05003047 /* setup the performance hint (first data BDE) if enabled */
3048 if (phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) {
3049 bde = (struct ulp_bde64 *)
3050 &(iocb_cmd->unsli3.sli3Words[5]);
3051 bde->addrLow = first_data_sgl->addr_lo;
3052 bde->addrHigh = first_data_sgl->addr_hi;
3053 bde->tus.f.bdeSize =
3054 le32_to_cpu(first_data_sgl->sge_len);
3055 bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
3056 bde->tus.w = cpu_to_le32(bde->tus.w);
3057 }
James Smartda0436e2009-05-22 14:51:39 -04003058 } else {
3059 sgl += 1;
3060 /* clear the last flag in the fcp_rsp map entry */
3061 sgl->word2 = le32_to_cpu(sgl->word2);
3062 bf_set(lpfc_sli4_sge_last, sgl, 1);
3063 sgl->word2 = cpu_to_le32(sgl->word2);
3064 }
3065
3066 /*
3067 * Finish initializing those IOCB fields that are dependent on the
3068 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
3069 * explicitly reinitialized.
3070 * all iocb memory resources are reused.
3071 */
3072 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
3073
3074 /*
3075 * Due to difference in data length between DIF/non-DIF paths,
3076 * we need to set word 4 of IOCB here
3077 */
3078 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
3079 return 0;
3080}
3081
3082/**
James Smartacd68592012-01-18 16:25:09 -05003083 * lpfc_bg_scsi_adjust_dl - Adjust SCSI data length for BlockGuard
3084 * @phba: The Hba for which this call is being executed.
3085 * @lpfc_cmd: The scsi buffer which is going to be adjusted.
3086 *
3087 * Adjust the data length to account for how much data
3088 * is actually on the wire.
3089 *
3090 * returns the adjusted data length
3091 **/
3092static int
3093lpfc_bg_scsi_adjust_dl(struct lpfc_hba *phba,
3094 struct lpfc_scsi_buf *lpfc_cmd)
3095{
3096 struct scsi_cmnd *sc = lpfc_cmd->pCmd;
3097 int diflen, fcpdl;
3098 unsigned blksize;
3099
3100 fcpdl = scsi_bufflen(sc);
3101
3102 /* Check if there is protection data on the wire */
3103 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
3104 /* Read */
3105 if (scsi_get_prot_op(sc) == SCSI_PROT_READ_INSERT)
3106 return fcpdl;
3107
3108 } else {
3109 /* Write */
3110 if (scsi_get_prot_op(sc) == SCSI_PROT_WRITE_STRIP)
3111 return fcpdl;
3112 }
3113
3114 /* If protection data on the wire, adjust the count accordingly */
3115 blksize = lpfc_cmd_blksize(sc);
3116 diflen = (fcpdl / blksize) * 8;
3117 fcpdl += diflen;
3118 return fcpdl;
3119}
3120
3121/**
3122 * lpfc_bg_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3123 * @phba: The Hba for which this call is being executed.
3124 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3125 *
3126 * This is the protection/DIF aware version of
3127 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
3128 * two functions eventually, but for now, it's here
3129 **/
3130static int
3131lpfc_bg_scsi_prep_dma_buf_s4(struct lpfc_hba *phba,
3132 struct lpfc_scsi_buf *lpfc_cmd)
3133{
3134 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3135 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3136 struct sli4_sge *sgl = (struct sli4_sge *)(lpfc_cmd->fcp_bpl);
3137 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
3138 uint32_t num_bde = 0;
3139 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
3140 int prot_group_type = 0;
3141 int fcpdl;
3142
3143 /*
3144 * Start the lpfc command prep by bumping the sgl beyond fcp_cmnd
3145 * fcp_rsp regions to the first data bde entry
3146 */
3147 if (scsi_sg_count(scsi_cmnd)) {
3148 /*
3149 * The driver stores the segment count returned from pci_map_sg
3150 * because this a count of dma-mappings used to map the use_sg
3151 * pages. They are not guaranteed to be the same for those
3152 * architectures that implement an IOMMU.
3153 */
3154 datasegcnt = dma_map_sg(&phba->pcidev->dev,
3155 scsi_sglist(scsi_cmnd),
3156 scsi_sg_count(scsi_cmnd), datadir);
3157 if (unlikely(!datasegcnt))
3158 return 1;
3159
3160 sgl += 1;
3161 /* clear the last flag in the fcp_rsp map entry */
3162 sgl->word2 = le32_to_cpu(sgl->word2);
3163 bf_set(lpfc_sli4_sge_last, sgl, 0);
3164 sgl->word2 = cpu_to_le32(sgl->word2);
3165
3166 sgl += 1;
3167 lpfc_cmd->seg_cnt = datasegcnt;
3168 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
3169 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
3170 "9087 BLKGRD: %s: Too many sg segments"
3171 " from dma_map_sg. Config %d, seg_cnt"
3172 " %d\n",
3173 __func__, phba->cfg_sg_seg_cnt,
3174 lpfc_cmd->seg_cnt);
3175 scsi_dma_unmap(scsi_cmnd);
3176 return 1;
3177 }
3178
3179 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
3180
3181 switch (prot_group_type) {
3182 case LPFC_PG_TYPE_NO_DIF:
3183 num_bde = lpfc_bg_setup_sgl(phba, scsi_cmnd, sgl,
3184 datasegcnt);
3185 /* we should have 2 or more entries in buffer list */
3186 if (num_bde < 2)
3187 goto err;
3188 break;
3189 case LPFC_PG_TYPE_DIF_BUF:{
3190 /*
3191 * This type indicates that protection buffers are
3192 * passed to the driver, so that needs to be prepared
3193 * for DMA
3194 */
3195 protsegcnt = dma_map_sg(&phba->pcidev->dev,
3196 scsi_prot_sglist(scsi_cmnd),
3197 scsi_prot_sg_count(scsi_cmnd), datadir);
3198 if (unlikely(!protsegcnt)) {
3199 scsi_dma_unmap(scsi_cmnd);
3200 return 1;
3201 }
3202
3203 lpfc_cmd->prot_seg_cnt = protsegcnt;
3204 if (lpfc_cmd->prot_seg_cnt
3205 > phba->cfg_prot_sg_seg_cnt) {
3206 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
3207 "9088 BLKGRD: %s: Too many prot sg "
3208 "segments from dma_map_sg. Config %d,"
3209 "prot_seg_cnt %d\n", __func__,
3210 phba->cfg_prot_sg_seg_cnt,
3211 lpfc_cmd->prot_seg_cnt);
3212 dma_unmap_sg(&phba->pcidev->dev,
3213 scsi_prot_sglist(scsi_cmnd),
3214 scsi_prot_sg_count(scsi_cmnd),
3215 datadir);
3216 scsi_dma_unmap(scsi_cmnd);
3217 return 1;
3218 }
3219
3220 num_bde = lpfc_bg_setup_sgl_prot(phba, scsi_cmnd, sgl,
3221 datasegcnt, protsegcnt);
3222 /* we should have 3 or more entries in buffer list */
3223 if (num_bde < 3)
3224 goto err;
3225 break;
3226 }
3227 case LPFC_PG_TYPE_INVALID:
3228 default:
3229 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
3230 "9083 Unexpected protection group %i\n",
3231 prot_group_type);
3232 return 1;
3233 }
3234 }
3235
3236 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
3237
3238 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
3239
3240 /*
3241 * Due to difference in data length between DIF/non-DIF paths,
3242 * we need to set word 4 of IOCB here
3243 */
3244 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
3245 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF;
3246
3247 return 0;
3248err:
3249 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
3250 "9084 Could not setup all needed BDE's"
3251 "prot_group_type=%d, num_bde=%d\n",
3252 prot_group_type, num_bde);
3253 return 1;
3254}
3255
3256/**
James Smart3772a992009-05-22 14:50:54 -04003257 * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3258 * @phba: The Hba for which this call is being executed.
3259 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3260 *
3261 * This routine wraps the actual DMA mapping function pointer from the
3262 * lpfc_hba struct.
3263 *
3264 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04003265 * 1 - Error
3266 * 0 - Success
James Smart3772a992009-05-22 14:50:54 -04003267 **/
3268static inline int
3269lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3270{
3271 return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
3272}
3273
3274/**
James Smartacd68592012-01-18 16:25:09 -05003275 * lpfc_bg_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3276 * using BlockGuard.
3277 * @phba: The Hba for which this call is being executed.
3278 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3279 *
3280 * This routine wraps the actual DMA mapping function pointer from the
3281 * lpfc_hba struct.
3282 *
3283 * Return codes:
3284 * 1 - Error
3285 * 0 - Success
3286 **/
3287static inline int
3288lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3289{
3290 return phba->lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
3291}
3292
3293/**
James Smart3621a712009-04-06 18:47:14 -04003294 * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
James Smartea2151b2008-09-07 11:52:10 -04003295 * @phba: Pointer to hba context object.
3296 * @vport: Pointer to vport object.
3297 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
3298 * @rsp_iocb: Pointer to response iocb object which reported error.
3299 *
3300 * This function posts an event when there is a SCSI command reporting
3301 * error from the scsi device.
3302 **/
3303static void
3304lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
3305 struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
3306 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3307 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
3308 uint32_t resp_info = fcprsp->rspStatus2;
3309 uint32_t scsi_status = fcprsp->rspStatus3;
3310 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
3311 struct lpfc_fast_path_event *fast_path_evt = NULL;
3312 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
3313 unsigned long flags;
3314
James Smart5989b8d2010-10-22 11:06:56 -04003315 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3316 return;
3317
James Smartea2151b2008-09-07 11:52:10 -04003318 /* If there is queuefull or busy condition send a scsi event */
3319 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
3320 (cmnd->result == SAM_STAT_BUSY)) {
3321 fast_path_evt = lpfc_alloc_fast_evt(phba);
3322 if (!fast_path_evt)
3323 return;
3324 fast_path_evt->un.scsi_evt.event_type =
3325 FC_REG_SCSI_EVENT;
3326 fast_path_evt->un.scsi_evt.subcategory =
3327 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
3328 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
3329 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
3330 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
3331 &pnode->nlp_portname, sizeof(struct lpfc_name));
3332 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
3333 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3334 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
3335 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
3336 fast_path_evt = lpfc_alloc_fast_evt(phba);
3337 if (!fast_path_evt)
3338 return;
3339 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
3340 FC_REG_SCSI_EVENT;
3341 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
3342 LPFC_EVENT_CHECK_COND;
3343 fast_path_evt->un.check_cond_evt.scsi_event.lun =
3344 cmnd->device->lun;
3345 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
3346 &pnode->nlp_portname, sizeof(struct lpfc_name));
3347 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
3348 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3349 fast_path_evt->un.check_cond_evt.sense_key =
3350 cmnd->sense_buffer[2] & 0xf;
3351 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
3352 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
3353 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3354 fcpi_parm &&
3355 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
3356 ((scsi_status == SAM_STAT_GOOD) &&
3357 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
3358 /*
3359 * If status is good or resid does not match with fcp_param and
3360 * there is valid fcpi_parm, then there is a read_check error
3361 */
3362 fast_path_evt = lpfc_alloc_fast_evt(phba);
3363 if (!fast_path_evt)
3364 return;
3365 fast_path_evt->un.read_check_error.header.event_type =
3366 FC_REG_FABRIC_EVENT;
3367 fast_path_evt->un.read_check_error.header.subcategory =
3368 LPFC_EVENT_FCPRDCHKERR;
3369 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
3370 &pnode->nlp_portname, sizeof(struct lpfc_name));
3371 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
3372 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3373 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
3374 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
3375 fast_path_evt->un.read_check_error.fcpiparam =
3376 fcpi_parm;
3377 } else
3378 return;
3379
3380 fast_path_evt->vport = vport;
3381 spin_lock_irqsave(&phba->hbalock, flags);
3382 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
3383 spin_unlock_irqrestore(&phba->hbalock, flags);
3384 lpfc_worker_wake_up(phba);
3385 return;
3386}
James Smart9bad7672008-12-04 22:39:02 -05003387
3388/**
James Smartf1126682009-06-10 17:22:44 -04003389 * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
James Smart3772a992009-05-22 14:50:54 -04003390 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -05003391 * @psb: The scsi buffer which is going to be un-mapped.
3392 *
3393 * This routine does DMA un-mapping of scatter gather list of scsi command
James Smart3772a992009-05-22 14:50:54 -04003394 * field of @lpfc_cmd for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05003395 **/
dea31012005-04-17 16:05:31 -05003396static void
James Smartf1126682009-06-10 17:22:44 -04003397lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James Smartbcf4dbf2006-07-06 15:50:08 -04003398{
3399 /*
3400 * There are only two special cases to consider. (1) the scsi command
3401 * requested scatter-gather usage or (2) the scsi command allocated
3402 * a request buffer, but did not request use_sg. There is a third
3403 * case, but it does not require resource deallocation.
3404 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003405 if (psb->seg_cnt > 0)
3406 scsi_dma_unmap(psb->pCmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003407 if (psb->prot_seg_cnt > 0)
3408 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
3409 scsi_prot_sg_count(psb->pCmd),
3410 psb->pCmd->sc_data_direction);
James Smartbcf4dbf2006-07-06 15:50:08 -04003411}
3412
James Smart9bad7672008-12-04 22:39:02 -05003413/**
James Smart3621a712009-04-06 18:47:14 -04003414 * lpfc_handler_fcp_err - FCP response handler
James Smart9bad7672008-12-04 22:39:02 -05003415 * @vport: The virtual port for which this call is being executed.
3416 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
3417 * @rsp_iocb: The response IOCB which contains FCP error.
3418 *
3419 * This routine is called to process response IOCB with status field
3420 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
3421 * based upon SCSI and FCP error.
3422 **/
James Smartbcf4dbf2006-07-06 15:50:08 -04003423static void
James Smart2e0fef82007-06-17 19:56:36 -05003424lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
3425 struct lpfc_iocbq *rsp_iocb)
dea31012005-04-17 16:05:31 -05003426{
3427 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3428 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
3429 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
James Smart7054a602007-04-25 09:52:34 -04003430 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
dea31012005-04-17 16:05:31 -05003431 uint32_t resp_info = fcprsp->rspStatus2;
3432 uint32_t scsi_status = fcprsp->rspStatus3;
James Smartc7743952006-12-02 13:34:42 -05003433 uint32_t *lp;
dea31012005-04-17 16:05:31 -05003434 uint32_t host_status = DID_OK;
3435 uint32_t rsplen = 0;
James Smartc7743952006-12-02 13:34:42 -05003436 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea31012005-04-17 16:05:31 -05003437
James Smartea2151b2008-09-07 11:52:10 -04003438
dea31012005-04-17 16:05:31 -05003439 /*
3440 * If this is a task management command, there is no
3441 * scsi packet associated with this lpfc_cmd. The driver
3442 * consumes it.
3443 */
3444 if (fcpcmd->fcpCntl2) {
3445 scsi_status = 0;
3446 goto out;
3447 }
3448
James Smart6a9c52c2009-10-02 15:16:51 -04003449 if (resp_info & RSP_LEN_VALID) {
3450 rsplen = be32_to_cpu(fcprsp->rspRspLen);
James Smarte40a02c2010-02-26 14:13:54 -05003451 if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
James Smart6a9c52c2009-10-02 15:16:51 -04003452 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3453 "2719 Invalid response length: "
3454 "tgt x%x lun x%x cmnd x%x rsplen x%x\n",
3455 cmnd->device->id,
3456 cmnd->device->lun, cmnd->cmnd[0],
3457 rsplen);
3458 host_status = DID_ERROR;
3459 goto out;
3460 }
James Smarte40a02c2010-02-26 14:13:54 -05003461 if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
3462 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3463 "2757 Protocol failure detected during "
3464 "processing of FCP I/O op: "
3465 "tgt x%x lun x%x cmnd x%x rspInfo3 x%x\n",
3466 cmnd->device->id,
3467 cmnd->device->lun, cmnd->cmnd[0],
3468 fcprsp->rspInfo3);
3469 host_status = DID_ERROR;
3470 goto out;
3471 }
James Smart6a9c52c2009-10-02 15:16:51 -04003472 }
3473
James Smartc7743952006-12-02 13:34:42 -05003474 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
3475 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
3476 if (snslen > SCSI_SENSE_BUFFERSIZE)
3477 snslen = SCSI_SENSE_BUFFERSIZE;
3478
3479 if (resp_info & RSP_LEN_VALID)
3480 rsplen = be32_to_cpu(fcprsp->rspRspLen);
3481 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
3482 }
3483 lp = (uint32_t *)cmnd->sense_buffer;
3484
James Smart73d91e52011-10-10 21:32:10 -04003485 if (!scsi_status && (resp_info & RESID_UNDER) &&
3486 vport->cfg_log_verbose & LOG_FCP_UNDER)
3487 logit = LOG_FCP_UNDER;
James Smartc7743952006-12-02 13:34:42 -05003488
James Smarte8b62012007-08-02 11:10:09 -04003489 lpfc_printf_vlog(vport, KERN_WARNING, logit,
James Smarte2a0a9d2008-12-04 22:40:02 -05003490 "9024 FCP command x%x failed: x%x SNS x%x x%x "
James Smarte8b62012007-08-02 11:10:09 -04003491 "Data: x%x x%x x%x x%x x%x\n",
3492 cmnd->cmnd[0], scsi_status,
3493 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
3494 be32_to_cpu(fcprsp->rspResId),
3495 be32_to_cpu(fcprsp->rspSnsLen),
3496 be32_to_cpu(fcprsp->rspRspLen),
3497 fcprsp->rspInfo3);
dea31012005-04-17 16:05:31 -05003498
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003499 scsi_set_resid(cmnd, 0);
dea31012005-04-17 16:05:31 -05003500 if (resp_info & RESID_UNDER) {
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003501 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea31012005-04-17 16:05:31 -05003502
James Smart73d91e52011-10-10 21:32:10 -04003503 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_UNDER,
James Smarte2a0a9d2008-12-04 22:40:02 -05003504 "9025 FCP Read Underrun, expected %d, "
James Smarte8b62012007-08-02 11:10:09 -04003505 "residual %d Data: x%x x%x x%x\n",
3506 be32_to_cpu(fcpcmd->fcpDl),
3507 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
3508 cmnd->underflow);
dea31012005-04-17 16:05:31 -05003509
3510 /*
James Smart7054a602007-04-25 09:52:34 -04003511 * If there is an under run check if under run reported by
3512 * storage array is same as the under run reported by HBA.
3513 * If this is not same, there is a dropped frame.
3514 */
3515 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3516 fcpi_parm &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003517 (scsi_get_resid(cmnd) != fcpi_parm)) {
James Smarte8b62012007-08-02 11:10:09 -04003518 lpfc_printf_vlog(vport, KERN_WARNING,
3519 LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05003520 "9026 FCP Read Check Error "
James Smarte8b62012007-08-02 11:10:09 -04003521 "and Underrun Data: x%x x%x x%x x%x\n",
3522 be32_to_cpu(fcpcmd->fcpDl),
3523 scsi_get_resid(cmnd), fcpi_parm,
3524 cmnd->cmnd[0]);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003525 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
James Smart7054a602007-04-25 09:52:34 -04003526 host_status = DID_ERROR;
3527 }
3528 /*
dea31012005-04-17 16:05:31 -05003529 * The cmnd->underflow is the minimum number of bytes that must
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003530 * be transferred for this command. Provided a sense condition
dea31012005-04-17 16:05:31 -05003531 * is not present, make sure the actual amount transferred is at
3532 * least the underflow value or fail.
3533 */
3534 if (!(resp_info & SNS_LEN_VALID) &&
3535 (scsi_status == SAM_STAT_GOOD) &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003536 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
3537 < cmnd->underflow)) {
James Smarte8b62012007-08-02 11:10:09 -04003538 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05003539 "9027 FCP command x%x residual "
James Smarte8b62012007-08-02 11:10:09 -04003540 "underrun converted to error "
3541 "Data: x%x x%x x%x\n",
James Smart66dbfbe2007-08-05 06:08:38 -04003542 cmnd->cmnd[0], scsi_bufflen(cmnd),
James Smarte8b62012007-08-02 11:10:09 -04003543 scsi_get_resid(cmnd), cmnd->underflow);
dea31012005-04-17 16:05:31 -05003544 host_status = DID_ERROR;
3545 }
3546 } else if (resp_info & RESID_OVER) {
James Smarte8b62012007-08-02 11:10:09 -04003547 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05003548 "9028 FCP command x%x residual overrun error. "
James Smarte4e74272009-07-19 10:01:38 -04003549 "Data: x%x x%x\n", cmnd->cmnd[0],
James Smarte8b62012007-08-02 11:10:09 -04003550 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea31012005-04-17 16:05:31 -05003551 host_status = DID_ERROR;
3552
3553 /*
3554 * Check SLI validation that all the transfer was actually done
James Smart582dd792012-08-03 12:34:44 -04003555 * (fcpi_parm should be zero).
dea31012005-04-17 16:05:31 -05003556 */
James Smart582dd792012-08-03 12:34:44 -04003557 } else if (fcpi_parm) {
James Smarte8b62012007-08-02 11:10:09 -04003558 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
James Smart582dd792012-08-03 12:34:44 -04003559 "9029 FCP Data Transfer Check Error: "
James Smarteee88772010-09-29 11:19:08 -04003560 "x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003561 be32_to_cpu(fcpcmd->fcpDl),
3562 be32_to_cpu(fcprsp->rspResId),
James Smarteee88772010-09-29 11:19:08 -04003563 fcpi_parm, cmnd->cmnd[0], scsi_status);
3564 switch (scsi_status) {
3565 case SAM_STAT_GOOD:
3566 case SAM_STAT_CHECK_CONDITION:
3567 /* Fabric dropped a data frame. Fail any successful
3568 * command in which we detected dropped frames.
3569 * A status of good or some check conditions could
3570 * be considered a successful command.
3571 */
3572 host_status = DID_ERROR;
3573 break;
3574 }
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003575 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea31012005-04-17 16:05:31 -05003576 }
3577
3578 out:
3579 cmnd->result = ScsiResult(host_status, scsi_status);
James Smartea2151b2008-09-07 11:52:10 -04003580 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
dea31012005-04-17 16:05:31 -05003581}
3582
James Smart9bad7672008-12-04 22:39:02 -05003583/**
James Smart3621a712009-04-06 18:47:14 -04003584 * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
James Smart9bad7672008-12-04 22:39:02 -05003585 * @phba: The Hba for which this call is being executed.
3586 * @pIocbIn: The command IOCBQ for the scsi cmnd.
James Smart3772a992009-05-22 14:50:54 -04003587 * @pIocbOut: The response IOCBQ for the scsi cmnd.
James Smart9bad7672008-12-04 22:39:02 -05003588 *
3589 * This routine assigns scsi command result by looking into response IOCB
3590 * status field appropriately. This routine handles QUEUE FULL condition as
3591 * well by ramping down device queue depth.
3592 **/
dea31012005-04-17 16:05:31 -05003593static void
3594lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
3595 struct lpfc_iocbq *pIocbOut)
3596{
3597 struct lpfc_scsi_buf *lpfc_cmd =
3598 (struct lpfc_scsi_buf *) pIocbIn->context1;
James Smart2e0fef82007-06-17 19:56:36 -05003599 struct lpfc_vport *vport = pIocbIn->vport;
dea31012005-04-17 16:05:31 -05003600 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
3601 struct lpfc_nodelist *pnode = rdata->pnode;
James Smart75baf692010-06-08 18:31:21 -04003602 struct scsi_cmnd *cmd;
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05003603 int result;
James Smarta257bf92009-04-06 18:48:10 -04003604 struct scsi_device *tmp_sdev;
James Smart5ffc2662009-11-18 15:39:44 -05003605 int depth;
James Smartfa61a542008-01-11 01:52:42 -05003606 unsigned long flags;
James Smartea2151b2008-09-07 11:52:10 -04003607 struct lpfc_fast_path_event *fast_path_evt;
James Smart75baf692010-06-08 18:31:21 -04003608 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04003609 uint32_t queue_depth, scsi_id;
James Smart73d91e52011-10-10 21:32:10 -04003610 uint32_t logit = LOG_FCP;
dea31012005-04-17 16:05:31 -05003611
James Smart75baf692010-06-08 18:31:21 -04003612 /* Sanity check on return of outstanding command */
3613 if (!(lpfc_cmd->pCmd))
3614 return;
3615 cmd = lpfc_cmd->pCmd;
3616 shost = cmd->device->host;
3617
dea31012005-04-17 16:05:31 -05003618 lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
3619 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
James Smart341af102010-01-26 23:07:37 -05003620 /* pick up SLI4 exhange busy status from HBA */
3621 lpfc_cmd->exch_busy = pIocbOut->iocb_flag & LPFC_EXCHANGE_BUSY;
3622
James Smart9a6b09c2012-03-01 22:37:42 -05003623#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
3624 if (lpfc_cmd->prot_data_type) {
3625 struct scsi_dif_tuple *src = NULL;
3626
3627 src = (struct scsi_dif_tuple *)lpfc_cmd->prot_data_segment;
3628 /*
3629 * Used to restore any changes to protection
3630 * data for error injection.
3631 */
3632 switch (lpfc_cmd->prot_data_type) {
3633 case LPFC_INJERR_REFTAG:
3634 src->ref_tag =
3635 lpfc_cmd->prot_data;
3636 break;
3637 case LPFC_INJERR_APPTAG:
3638 src->app_tag =
3639 (uint16_t)lpfc_cmd->prot_data;
3640 break;
3641 case LPFC_INJERR_GUARD:
3642 src->guard_tag =
3643 (uint16_t)lpfc_cmd->prot_data;
3644 break;
3645 default:
3646 break;
3647 }
3648
3649 lpfc_cmd->prot_data = 0;
3650 lpfc_cmd->prot_data_type = 0;
3651 lpfc_cmd->prot_data_segment = NULL;
3652 }
3653#endif
James Smart109f6ed2008-12-04 22:39:08 -05003654 if (pnode && NLP_CHK_NODE_ACT(pnode))
3655 atomic_dec(&pnode->cmd_pending);
dea31012005-04-17 16:05:31 -05003656
3657 if (lpfc_cmd->status) {
3658 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
3659 (lpfc_cmd->result & IOERR_DRVR_MASK))
3660 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
3661 else if (lpfc_cmd->status >= IOSTAT_CNT)
3662 lpfc_cmd->status = IOSTAT_DEFAULT;
James Smart73d91e52011-10-10 21:32:10 -04003663 if (lpfc_cmd->status == IOSTAT_FCP_RSP_ERROR
3664 && !lpfc_cmd->fcp_rsp->rspStatus3
3665 && (lpfc_cmd->fcp_rsp->rspStatus2 & RESID_UNDER)
3666 && !(phba->cfg_log_verbose & LOG_FCP_UNDER))
3667 logit = 0;
3668 else
3669 logit = LOG_FCP | LOG_FCP_UNDER;
3670 lpfc_printf_vlog(vport, KERN_WARNING, logit,
3671 "9030 FCP cmd x%x failed <%d/%d> "
James Smart5a0d80f2012-05-09 21:18:20 -04003672 "status: x%x result: x%x "
3673 "sid: x%x did: x%x oxid: x%x "
3674 "Data: x%x x%x\n",
James Smart73d91e52011-10-10 21:32:10 -04003675 cmd->cmnd[0],
3676 cmd->device ? cmd->device->id : 0xffff,
3677 cmd->device ? cmd->device->lun : 0xffff,
3678 lpfc_cmd->status, lpfc_cmd->result,
James Smart5a0d80f2012-05-09 21:18:20 -04003679 vport->fc_myDID, pnode->nlp_DID,
3680 phba->sli_rev == LPFC_SLI_REV4 ?
3681 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
James Smart73d91e52011-10-10 21:32:10 -04003682 pIocbOut->iocb.ulpContext,
3683 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
dea31012005-04-17 16:05:31 -05003684
3685 switch (lpfc_cmd->status) {
3686 case IOSTAT_FCP_RSP_ERROR:
3687 /* Call FCP RSP handler to determine result */
James Smart2e0fef82007-06-17 19:56:36 -05003688 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea31012005-04-17 16:05:31 -05003689 break;
3690 case IOSTAT_NPORT_BSY:
3691 case IOSTAT_FABRIC_BSY:
James Smart0f1f53a2008-08-24 21:50:18 -04003692 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
James Smartea2151b2008-09-07 11:52:10 -04003693 fast_path_evt = lpfc_alloc_fast_evt(phba);
3694 if (!fast_path_evt)
3695 break;
3696 fast_path_evt->un.fabric_evt.event_type =
3697 FC_REG_FABRIC_EVENT;
3698 fast_path_evt->un.fabric_evt.subcategory =
3699 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
3700 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
3701 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
3702 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
3703 &pnode->nlp_portname,
3704 sizeof(struct lpfc_name));
3705 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
3706 &pnode->nlp_nodename,
3707 sizeof(struct lpfc_name));
3708 }
3709 fast_path_evt->vport = vport;
3710 fast_path_evt->work_evt.evt =
3711 LPFC_EVT_FASTPATH_MGMT_EVT;
3712 spin_lock_irqsave(&phba->hbalock, flags);
3713 list_add_tail(&fast_path_evt->work_evt.evt_listp,
3714 &phba->work_list);
3715 spin_unlock_irqrestore(&phba->hbalock, flags);
3716 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05003717 break;
James Smart92d7f7b2007-06-17 19:56:38 -05003718 case IOSTAT_LOCAL_REJECT:
James Smart1151e3e2011-02-16 12:39:35 -05003719 case IOSTAT_REMOTE_STOP:
James Smartab56dc22011-02-16 12:39:57 -05003720 if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
3721 lpfc_cmd->result ==
3722 IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
3723 lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
3724 lpfc_cmd->result ==
3725 IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
3726 cmd->result = ScsiResult(DID_NO_CONNECT, 0);
3727 break;
3728 }
James Smartd7c255b2008-08-24 21:50:00 -04003729 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
James Smart92d7f7b2007-06-17 19:56:38 -05003730 lpfc_cmd->result == IOERR_NO_RESOURCES ||
James Smartb92938b2010-06-07 15:24:12 -04003731 lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
3732 lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
James Smart92d7f7b2007-06-17 19:56:38 -05003733 cmd->result = ScsiResult(DID_REQUEUE, 0);
James Smart58da1ff2008-04-07 10:15:56 -04003734 break;
James Smarte2a0a9d2008-12-04 22:40:02 -05003735 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003736 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
3737 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
3738 pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
3739 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
3740 /*
3741 * This is a response for a BG enabled
3742 * cmd. Parse BG error
3743 */
3744 lpfc_parse_bg_err(phba, lpfc_cmd,
3745 pIocbOut);
3746 break;
3747 } else {
3748 lpfc_printf_vlog(vport, KERN_WARNING,
3749 LOG_BG,
3750 "9031 non-zero BGSTAT "
James Smart6a9c52c2009-10-02 15:16:51 -04003751 "on unprotected cmd\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05003752 }
3753 }
James Smart1151e3e2011-02-16 12:39:35 -05003754 if ((lpfc_cmd->status == IOSTAT_REMOTE_STOP)
3755 && (phba->sli_rev == LPFC_SLI_REV4)
3756 && (pnode && NLP_CHK_NODE_ACT(pnode))) {
3757 /* This IO was aborted by the target, we don't
3758 * know the rxid and because we did not send the
3759 * ABTS we cannot generate and RRQ.
3760 */
3761 lpfc_set_rrq_active(phba, pnode,
James Smartee0f4fe2012-05-09 21:19:14 -04003762 lpfc_cmd->cur_iocbq.sli4_lxritag,
3763 0, 0);
James Smart1151e3e2011-02-16 12:39:35 -05003764 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003765 /* else: fall through */
dea31012005-04-17 16:05:31 -05003766 default:
3767 cmd->result = ScsiResult(DID_ERROR, 0);
3768 break;
3769 }
3770
James Smart58da1ff2008-04-07 10:15:56 -04003771 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003772 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
James Smart0f1f53a2008-08-24 21:50:18 -04003773 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
3774 SAM_STAT_BUSY);
James Smartab56dc22011-02-16 12:39:57 -05003775 } else
dea31012005-04-17 16:05:31 -05003776 cmd->result = ScsiResult(DID_OK, 0);
dea31012005-04-17 16:05:31 -05003777
3778 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
3779 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
3780
James Smarte8b62012007-08-02 11:10:09 -04003781 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
3782 "0710 Iodone <%d/%d> cmd %p, error "
3783 "x%x SNS x%x x%x Data: x%x x%x\n",
3784 cmd->device->id, cmd->device->lun, cmd,
3785 cmd->result, *lp, *(lp + 3), cmd->retries,
3786 scsi_get_resid(cmd));
dea31012005-04-17 16:05:31 -05003787 }
3788
James Smartea2151b2008-09-07 11:52:10 -04003789 lpfc_update_stats(phba, lpfc_cmd);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05003790 result = cmd->result;
James Smart977b5a02008-09-07 11:52:04 -04003791 if (vport->cfg_max_scsicmpl_time &&
3792 time_after(jiffies, lpfc_cmd->start_time +
3793 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
James Smarta257bf92009-04-06 18:48:10 -04003794 spin_lock_irqsave(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05003795 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
3796 if (pnode->cmd_qdepth >
3797 atomic_read(&pnode->cmd_pending) &&
3798 (atomic_read(&pnode->cmd_pending) >
3799 LPFC_MIN_TGT_QDEPTH) &&
3800 ((cmd->cmnd[0] == READ_10) ||
3801 (cmd->cmnd[0] == WRITE_10)))
3802 pnode->cmd_qdepth =
3803 atomic_read(&pnode->cmd_pending);
James Smart977b5a02008-09-07 11:52:04 -04003804
James Smart109f6ed2008-12-04 22:39:08 -05003805 pnode->last_change_time = jiffies;
3806 }
James Smarta257bf92009-04-06 18:48:10 -04003807 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05003808 } else if (pnode && NLP_CHK_NODE_ACT(pnode)) {
James Smart7dc517d2010-07-14 15:32:10 -04003809 if ((pnode->cmd_qdepth < vport->cfg_tgt_queue_depth) &&
James Smart977b5a02008-09-07 11:52:04 -04003810 time_after(jiffies, pnode->last_change_time +
James Smart109f6ed2008-12-04 22:39:08 -05003811 msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
James Smarta257bf92009-04-06 18:48:10 -04003812 spin_lock_irqsave(shost->host_lock, flags);
James Smart7dc517d2010-07-14 15:32:10 -04003813 depth = pnode->cmd_qdepth * LPFC_TGTQ_RAMPUP_PCENT
3814 / 100;
3815 depth = depth ? depth : 1;
3816 pnode->cmd_qdepth += depth;
3817 if (pnode->cmd_qdepth > vport->cfg_tgt_queue_depth)
3818 pnode->cmd_qdepth = vport->cfg_tgt_queue_depth;
James Smart109f6ed2008-12-04 22:39:08 -05003819 pnode->last_change_time = jiffies;
James Smarta257bf92009-04-06 18:48:10 -04003820 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05003821 }
James Smart977b5a02008-09-07 11:52:04 -04003822 }
3823
James Smart1dcb58e2007-04-25 09:51:30 -04003824 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James Smarta257bf92009-04-06 18:48:10 -04003825
3826 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
3827 queue_depth = cmd->device->queue_depth;
3828 scsi_id = cmd->device->id;
dea31012005-04-17 16:05:31 -05003829 cmd->scsi_done(cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003830
Jamie Wellnitzb8086082006-02-28 22:33:12 -05003831 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smartfa61a542008-01-11 01:52:42 -05003832 /*
3833 * If there is a thread waiting for command completion
3834 * wake up the thread.
3835 */
James Smarta257bf92009-04-06 18:48:10 -04003836 spin_lock_irqsave(shost->host_lock, flags);
James Smart495a7142008-06-14 22:52:59 -04003837 lpfc_cmd->pCmd = NULL;
James Smartfa61a542008-01-11 01:52:42 -05003838 if (lpfc_cmd->waitq)
3839 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04003840 spin_unlock_irqrestore(shost->host_lock, flags);
Jamie Wellnitzb8086082006-02-28 22:33:12 -05003841 lpfc_release_scsi_buf(phba, lpfc_cmd);
3842 return;
3843 }
3844
James Smart92d7f7b2007-06-17 19:56:38 -05003845 if (!result)
James Smarta257bf92009-04-06 18:48:10 -04003846 lpfc_rampup_queue_depth(vport, queue_depth);
James Smart92d7f7b2007-06-17 19:56:38 -05003847
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05003848 /*
3849 * Check for queue full. If the lun is reporting queue full, then
3850 * back off the lun queue depth to prevent target overloads.
3851 */
James Smart58da1ff2008-04-07 10:15:56 -04003852 if (result == SAM_STAT_TASK_SET_FULL && pnode &&
3853 NLP_CHK_NODE_ACT(pnode)) {
James Smarta257bf92009-04-06 18:48:10 -04003854 shost_for_each_device(tmp_sdev, shost) {
3855 if (tmp_sdev->id != scsi_id)
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05003856 continue;
3857 depth = scsi_track_queue_full(tmp_sdev,
James Smart5ffc2662009-11-18 15:39:44 -05003858 tmp_sdev->queue_depth-1);
3859 if (depth <= 0)
3860 continue;
James Smarte8b62012007-08-02 11:10:09 -04003861 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3862 "0711 detected queue full - lun queue "
3863 "depth adjusted to %d.\n", depth);
James Smartea2151b2008-09-07 11:52:10 -04003864 lpfc_send_sdev_queuedepth_change_event(phba, vport,
James Smart5ffc2662009-11-18 15:39:44 -05003865 pnode,
3866 tmp_sdev->lun,
3867 depth+1, depth);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05003868 }
3869 }
3870
James Smartfa61a542008-01-11 01:52:42 -05003871 /*
3872 * If there is a thread waiting for command completion
3873 * wake up the thread.
3874 */
James Smarta257bf92009-04-06 18:48:10 -04003875 spin_lock_irqsave(shost->host_lock, flags);
James Smart495a7142008-06-14 22:52:59 -04003876 lpfc_cmd->pCmd = NULL;
James Smartfa61a542008-01-11 01:52:42 -05003877 if (lpfc_cmd->waitq)
3878 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04003879 spin_unlock_irqrestore(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05003880
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003881 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05003882}
3883
James Smart34b02dc2008-08-24 21:49:55 -04003884/**
James Smart3621a712009-04-06 18:47:14 -04003885 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
James Smart34b02dc2008-08-24 21:49:55 -04003886 * @data: A pointer to the immediate command data portion of the IOCB.
3887 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
3888 *
3889 * The routine copies the entire FCP command from @fcp_cmnd to @data while
3890 * byte swapping the data to big endian format for transmission on the wire.
3891 **/
3892static void
3893lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
3894{
3895 int i, j;
3896 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
3897 i += sizeof(uint32_t), j++) {
3898 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
3899 }
3900}
3901
James Smart9bad7672008-12-04 22:39:02 -05003902/**
James Smartf1126682009-06-10 17:22:44 -04003903 * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05003904 * @vport: The virtual port for which this call is being executed.
3905 * @lpfc_cmd: The scsi command which needs to send.
3906 * @pnode: Pointer to lpfc_nodelist.
3907 *
3908 * This routine initializes fcp_cmnd and iocb data structure from scsi command
James Smart3772a992009-05-22 14:50:54 -04003909 * to transfer for device with SLI3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05003910 **/
dea31012005-04-17 16:05:31 -05003911static void
James Smartf1126682009-06-10 17:22:44 -04003912lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
James Smart2e0fef82007-06-17 19:56:36 -05003913 struct lpfc_nodelist *pnode)
dea31012005-04-17 16:05:31 -05003914{
James Smart2e0fef82007-06-17 19:56:36 -05003915 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003916 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3917 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3918 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
3919 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
3920 int datadir = scsi_cmnd->sc_data_direction;
James Smart7e2b19f2007-10-29 11:00:39 -04003921 char tag[2];
dea31012005-04-17 16:05:31 -05003922
James Smart58da1ff2008-04-07 10:15:56 -04003923 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3924 return;
3925
dea31012005-04-17 16:05:31 -05003926 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
James.Smart@Emulex.Com69859dc2005-08-10 15:02:37 -04003927 /* clear task management bits */
3928 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea31012005-04-17 16:05:31 -05003929
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -04003930 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
3931 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05003932
James Smartdf9e1b52011-12-13 13:22:17 -05003933 memset(&fcp_cmnd->fcpCdb[0], 0, LPFC_FCP_CDB_LEN);
3934 memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, scsi_cmnd->cmd_len);
James Smart7e2b19f2007-10-29 11:00:39 -04003935 if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
3936 switch (tag[0]) {
dea31012005-04-17 16:05:31 -05003937 case HEAD_OF_QUEUE_TAG:
3938 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
3939 break;
3940 case ORDERED_QUEUE_TAG:
3941 fcp_cmnd->fcpCntl1 = ORDERED_Q;
3942 break;
3943 default:
3944 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
3945 break;
3946 }
3947 } else
3948 fcp_cmnd->fcpCntl1 = 0;
3949
3950 /*
3951 * There are three possibilities here - use scatter-gather segment, use
3952 * the single mapping, or neither. Start the lpfc command prep by
3953 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
3954 * data bde entry.
3955 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003956 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05003957 if (datadir == DMA_TO_DEVICE) {
3958 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
James Smart3772a992009-05-22 14:50:54 -04003959 if (phba->sli_rev < LPFC_SLI_REV4) {
3960 iocb_cmd->un.fcpi.fcpi_parm = 0;
3961 iocb_cmd->ulpPU = 0;
3962 } else
3963 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05003964 fcp_cmnd->fcpCntl3 = WRITE_DATA;
3965 phba->fc4OutputRequests++;
3966 } else {
3967 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
3968 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05003969 fcp_cmnd->fcpCntl3 = READ_DATA;
3970 phba->fc4InputRequests++;
3971 }
3972 } else {
3973 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
3974 iocb_cmd->un.fcpi.fcpi_parm = 0;
3975 iocb_cmd->ulpPU = 0;
3976 fcp_cmnd->fcpCntl3 = 0;
3977 phba->fc4ControlRequests++;
3978 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003979 if (phba->sli_rev == 3 &&
3980 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04003981 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05003982 /*
3983 * Finish initializing those IOCB fields that are independent
3984 * of the scsi_cmnd request_buffer
3985 */
3986 piocbq->iocb.ulpContext = pnode->nlp_rpi;
James Smart6d368e52011-05-24 11:44:12 -04003987 if (phba->sli_rev == LPFC_SLI_REV4)
3988 piocbq->iocb.ulpContext =
3989 phba->sli4_hba.rpi_ids[pnode->nlp_rpi];
dea31012005-04-17 16:05:31 -05003990 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
3991 piocbq->iocb.ulpFCP2Rcvy = 1;
James Smart09372822008-01-11 01:52:54 -05003992 else
3993 piocbq->iocb.ulpFCP2Rcvy = 0;
dea31012005-04-17 16:05:31 -05003994
3995 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
3996 piocbq->context1 = lpfc_cmd;
3997 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
3998 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
James Smart2e0fef82007-06-17 19:56:36 -05003999 piocbq->vport = vport;
dea31012005-04-17 16:05:31 -05004000}
4001
James Smart9bad7672008-12-04 22:39:02 -05004002/**
James Smart6d368e52011-05-24 11:44:12 -04004003 * lpfc_scsi_prep_task_mgmt_cmd - Convert SLI3 scsi TM cmd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05004004 * @vport: The virtual port for which this call is being executed.
4005 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
4006 * @lun: Logical unit number.
4007 * @task_mgmt_cmd: SCSI task management command.
4008 *
James Smart3772a992009-05-22 14:50:54 -04004009 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
4010 * for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05004011 *
4012 * Return codes:
4013 * 0 - Error
4014 * 1 - Success
4015 **/
dea31012005-04-17 16:05:31 -05004016static int
James Smartf1126682009-06-10 17:22:44 -04004017lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
dea31012005-04-17 16:05:31 -05004018 struct lpfc_scsi_buf *lpfc_cmd,
James Smart420b630d2006-07-06 15:50:16 -04004019 unsigned int lun,
dea31012005-04-17 16:05:31 -05004020 uint8_t task_mgmt_cmd)
4021{
dea31012005-04-17 16:05:31 -05004022 struct lpfc_iocbq *piocbq;
4023 IOCB_t *piocb;
4024 struct fcp_cmnd *fcp_cmnd;
James Smart0b18ac42006-05-01 21:50:40 -04004025 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea31012005-04-17 16:05:31 -05004026 struct lpfc_nodelist *ndlp = rdata->pnode;
4027
James Smart58da1ff2008-04-07 10:15:56 -04004028 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
4029 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05004030 return 0;
dea31012005-04-17 16:05:31 -05004031
dea31012005-04-17 16:05:31 -05004032 piocbq = &(lpfc_cmd->cur_iocbq);
James Smart2e0fef82007-06-17 19:56:36 -05004033 piocbq->vport = vport;
4034
dea31012005-04-17 16:05:31 -05004035 piocb = &piocbq->iocb;
4036
4037 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart34b02dc2008-08-24 21:49:55 -04004038 /* Clear out any old data in the FCP command area */
4039 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
4040 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05004041 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
James Smarte2a0a9d2008-12-04 22:40:02 -05004042 if (vport->phba->sli_rev == 3 &&
4043 !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04004044 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05004045 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
dea31012005-04-17 16:05:31 -05004046 piocb->ulpContext = ndlp->nlp_rpi;
James Smart6d368e52011-05-24 11:44:12 -04004047 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
4048 piocb->ulpContext =
4049 vport->phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
4050 }
dea31012005-04-17 16:05:31 -05004051 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
4052 piocb->ulpFCP2Rcvy = 1;
4053 }
4054 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
4055
4056 /* ulpTimeout is only one byte */
4057 if (lpfc_cmd->timeout > 0xff) {
4058 /*
4059 * Do not timeout the command at the firmware level.
4060 * The driver will provide the timeout mechanism.
4061 */
4062 piocb->ulpTimeout = 0;
James Smartf1126682009-06-10 17:22:44 -04004063 } else
dea31012005-04-17 16:05:31 -05004064 piocb->ulpTimeout = lpfc_cmd->timeout;
James Smartf1126682009-06-10 17:22:44 -04004065
4066 if (vport->phba->sli_rev == LPFC_SLI_REV4)
4067 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004068
James Smart2e0fef82007-06-17 19:56:36 -05004069 return 1;
dea31012005-04-17 16:05:31 -05004070}
4071
James Smart9bad7672008-12-04 22:39:02 -05004072/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004073 * lpfc_scsi_api_table_setup - Set up scsi api function jump table
James Smart3772a992009-05-22 14:50:54 -04004074 * @phba: The hba struct for which this call is being executed.
4075 * @dev_grp: The HBA PCI-Device group number.
4076 *
4077 * This routine sets up the SCSI interface API function jump table in @phba
4078 * struct.
4079 * Returns: 0 - success, -ENODEV - failure.
4080 **/
4081int
4082lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4083{
4084
James Smartf1126682009-06-10 17:22:44 -04004085 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
4086 phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
James Smartf1126682009-06-10 17:22:44 -04004087
James Smart3772a992009-05-22 14:50:54 -04004088 switch (dev_grp) {
4089 case LPFC_PCI_DEV_LP:
4090 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s3;
4091 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
James Smartacd68592012-01-18 16:25:09 -05004092 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04004093 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
James Smart19ca7602010-11-20 23:11:55 -05004094 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04004095 break;
James Smartda0436e2009-05-22 14:51:39 -04004096 case LPFC_PCI_DEV_OC:
4097 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s4;
4098 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
James Smartacd68592012-01-18 16:25:09 -05004099 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04004100 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
James Smart19ca7602010-11-20 23:11:55 -05004101 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04004102 break;
James Smart3772a992009-05-22 14:50:54 -04004103 default:
4104 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4105 "1418 Invalid HBA PCI-device group: 0x%x\n",
4106 dev_grp);
4107 return -ENODEV;
4108 break;
4109 }
James Smart3772a992009-05-22 14:50:54 -04004110 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
James Smart84d1b002010-02-12 14:42:33 -05004111 phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
James Smart3772a992009-05-22 14:50:54 -04004112 return 0;
4113}
4114
4115/**
James Smart3621a712009-04-06 18:47:14 -04004116 * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
James Smart9bad7672008-12-04 22:39:02 -05004117 * @phba: The Hba for which this call is being executed.
4118 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
4119 * @rspiocbq: Pointer to lpfc_iocbq data structure.
4120 *
4121 * This routine is IOCB completion routine for device reset and target reset
4122 * routine. This routine release scsi buffer associated with lpfc_cmd.
4123 **/
James Smart7054a602007-04-25 09:52:34 -04004124static void
4125lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
4126 struct lpfc_iocbq *cmdiocbq,
4127 struct lpfc_iocbq *rspiocbq)
4128{
4129 struct lpfc_scsi_buf *lpfc_cmd =
4130 (struct lpfc_scsi_buf *) cmdiocbq->context1;
4131 if (lpfc_cmd)
4132 lpfc_release_scsi_buf(phba, lpfc_cmd);
4133 return;
4134}
4135
James Smart9bad7672008-12-04 22:39:02 -05004136/**
James Smart3621a712009-04-06 18:47:14 -04004137 * lpfc_info - Info entry point of scsi_host_template data structure
James Smart9bad7672008-12-04 22:39:02 -05004138 * @host: The scsi host for which this call is being executed.
4139 *
4140 * This routine provides module information about hba.
4141 *
4142 * Reutrn code:
4143 * Pointer to char - Success.
4144 **/
dea31012005-04-17 16:05:31 -05004145const char *
4146lpfc_info(struct Scsi_Host *host)
4147{
James Smart2e0fef82007-06-17 19:56:36 -05004148 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
4149 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004150 int len;
4151 static char lpfcinfobuf[384];
4152
4153 memset(lpfcinfobuf,0,384);
4154 if (phba && phba->pcidev){
4155 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
4156 len = strlen(lpfcinfobuf);
4157 snprintf(lpfcinfobuf + len,
4158 384-len,
4159 " on PCI bus %02x device %02x irq %d",
4160 phba->pcidev->bus->number,
4161 phba->pcidev->devfn,
4162 phba->pcidev->irq);
4163 len = strlen(lpfcinfobuf);
4164 if (phba->Port[0]) {
4165 snprintf(lpfcinfobuf + len,
4166 384-len,
4167 " port %s",
4168 phba->Port);
4169 }
James Smart65467b62010-01-26 23:08:29 -05004170 len = strlen(lpfcinfobuf);
4171 if (phba->sli4_hba.link_state.logical_speed) {
4172 snprintf(lpfcinfobuf + len,
4173 384-len,
4174 " Logical Link Speed: %d Mbps",
4175 phba->sli4_hba.link_state.logical_speed * 10);
4176 }
dea31012005-04-17 16:05:31 -05004177 }
4178 return lpfcinfobuf;
4179}
4180
James Smart9bad7672008-12-04 22:39:02 -05004181/**
James Smart3621a712009-04-06 18:47:14 -04004182 * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
James Smart9bad7672008-12-04 22:39:02 -05004183 * @phba: The Hba for which this call is being executed.
4184 *
4185 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
4186 * The default value of cfg_poll_tmo is 10 milliseconds.
4187 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004188static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
4189{
4190 unsigned long poll_tmo_expires =
4191 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
4192
4193 if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
4194 mod_timer(&phba->fcp_poll_timer,
4195 poll_tmo_expires);
4196}
4197
James Smart9bad7672008-12-04 22:39:02 -05004198/**
James Smart3621a712009-04-06 18:47:14 -04004199 * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
James Smart9bad7672008-12-04 22:39:02 -05004200 * @phba: The Hba for which this call is being executed.
4201 *
4202 * This routine starts the fcp_poll_timer of @phba.
4203 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004204void lpfc_poll_start_timer(struct lpfc_hba * phba)
4205{
4206 lpfc_poll_rearm_timer(phba);
4207}
4208
James Smart9bad7672008-12-04 22:39:02 -05004209/**
James Smart3621a712009-04-06 18:47:14 -04004210 * lpfc_poll_timeout - Restart polling timer
James Smart9bad7672008-12-04 22:39:02 -05004211 * @ptr: Map to lpfc_hba data structure pointer.
4212 *
4213 * This routine restarts fcp_poll timer, when FCP ring polling is enable
4214 * and FCP Ring interrupt is disable.
4215 **/
4216
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004217void lpfc_poll_timeout(unsigned long ptr)
4218{
James Smart2e0fef82007-06-17 19:56:36 -05004219 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004220
4221 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004222 lpfc_sli_handle_fast_ring_event(phba,
4223 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4224
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004225 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4226 lpfc_poll_rearm_timer(phba);
4227 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004228}
4229
James Smart9bad7672008-12-04 22:39:02 -05004230/**
James Smart3621a712009-04-06 18:47:14 -04004231 * lpfc_queuecommand - scsi_host_template queuecommand entry point
James Smart9bad7672008-12-04 22:39:02 -05004232 * @cmnd: Pointer to scsi_cmnd data structure.
4233 * @done: Pointer to done routine.
4234 *
4235 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
4236 * This routine prepares an IOCB from scsi command and provides to firmware.
4237 * The @done callback is invoked after driver finished processing the command.
4238 *
4239 * Return value :
4240 * 0 - Success
4241 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
4242 **/
dea31012005-04-17 16:05:31 -05004243static int
James Smartb9a7c632012-08-03 12:35:24 -04004244lpfc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004245{
James Smart2e0fef82007-06-17 19:56:36 -05004246 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4247 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004248 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05004249 struct lpfc_nodelist *ndlp;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004250 struct lpfc_scsi_buf *lpfc_cmd;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004251 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004252 int err;
dea31012005-04-17 16:05:31 -05004253
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004254 err = fc_remote_port_chkready(rport);
4255 if (err) {
4256 cmnd->result = err;
dea31012005-04-17 16:05:31 -05004257 goto out_fail_command;
4258 }
James Smart1c6f4ef52009-11-18 15:40:49 -05004259 ndlp = rdata->pnode;
dea31012005-04-17 16:05:31 -05004260
James Smartbf086112011-08-21 21:48:13 -04004261 if ((scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) &&
James Smartacd68592012-01-18 16:25:09 -05004262 (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED))) {
James Smarte2a0a9d2008-12-04 22:40:02 -05004263
James Smart6a9c52c2009-10-02 15:16:51 -04004264 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4265 "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
4266 " op:%02x str=%s without registering for"
4267 " BlockGuard - Rejecting command\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05004268 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
4269 dif_op_str[scsi_get_prot_op(cmnd)]);
4270 goto out_fail_command;
4271 }
4272
dea31012005-04-17 16:05:31 -05004273 /*
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004274 * Catch race where our node has transitioned, but the
4275 * transport is still transitioning.
dea31012005-04-17 16:05:31 -05004276 */
James Smart6b415f52012-06-12 13:54:59 -04004277 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
4278 goto out_tgt_busy;
James Smart7dc517d2010-07-14 15:32:10 -04004279 if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth)
Mike Christie34963432011-02-25 14:04:28 -06004280 goto out_tgt_busy;
Mike Christiea93ce022008-08-17 15:24:41 -05004281
James Smart19ca7602010-11-20 23:11:55 -05004282 lpfc_cmd = lpfc_get_scsi_buf(phba, ndlp);
dea31012005-04-17 16:05:31 -05004283 if (lpfc_cmd == NULL) {
James Smarteaf15d52008-12-04 22:39:29 -05004284 lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05004285
James Smarte8b62012007-08-02 11:10:09 -04004286 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4287 "0707 driver's buffer pool is empty, "
4288 "IO busied\n");
dea31012005-04-17 16:05:31 -05004289 goto out_host_busy;
4290 }
4291
4292 /*
4293 * Store the midlayer's command structure for the completion phase
4294 * and complete the command initialization.
4295 */
4296 lpfc_cmd->pCmd = cmnd;
4297 lpfc_cmd->rdata = rdata;
4298 lpfc_cmd->timeout = 0;
James Smart977b5a02008-09-07 11:52:04 -04004299 lpfc_cmd->start_time = jiffies;
dea31012005-04-17 16:05:31 -05004300 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
dea31012005-04-17 16:05:31 -05004301
James Smarte2a0a9d2008-12-04 22:40:02 -05004302 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
James Smart6a9c52c2009-10-02 15:16:51 -04004303 if (vport->phba->cfg_enable_bg) {
4304 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smart9a6b09c2012-03-01 22:37:42 -05004305 "9033 BLKGRD: rcvd protected cmd:%02x op=%s "
4306 "guard=%s\n", cmnd->cmnd[0],
4307 dif_op_str[scsi_get_prot_op(cmnd)],
4308 dif_grd_str[scsi_host_get_guard(shost)]);
James Smart6a9c52c2009-10-02 15:16:51 -04004309 if (cmnd->cmnd[0] == READ_10)
4310 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05004311 "9035 BLKGRD: READ @ sector %llu, "
James Smart9a6b09c2012-03-01 22:37:42 -05004312 "cnt %u, rpt %d\n",
Tejun Heo83096eb2009-05-07 22:24:39 +09004313 (unsigned long long)scsi_get_lba(cmnd),
James Smart9a6b09c2012-03-01 22:37:42 -05004314 blk_rq_sectors(cmnd->request),
4315 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004316 else if (cmnd->cmnd[0] == WRITE_10)
4317 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05004318 "9036 BLKGRD: WRITE @ sector %llu, "
James Smart9a6b09c2012-03-01 22:37:42 -05004319 "cnt %u, wpt %d\n",
James Smart87b5c322008-12-16 10:34:09 -05004320 (unsigned long long)scsi_get_lba(cmnd),
Tejun Heo83096eb2009-05-07 22:24:39 +09004321 blk_rq_sectors(cmnd->request),
James Smart9a6b09c2012-03-01 22:37:42 -05004322 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004323 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004324
4325 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
4326 } else {
James Smart6a9c52c2009-10-02 15:16:51 -04004327 if (vport->phba->cfg_enable_bg) {
James Smarte2a0a9d2008-12-04 22:40:02 -05004328 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smart9a6b09c2012-03-01 22:37:42 -05004329 "9038 BLKGRD: rcvd unprotected cmd:"
4330 "%02x op=%s guard=%s\n", cmnd->cmnd[0],
4331 dif_op_str[scsi_get_prot_op(cmnd)],
4332 dif_grd_str[scsi_host_get_guard(shost)]);
James Smart6a9c52c2009-10-02 15:16:51 -04004333 if (cmnd->cmnd[0] == READ_10)
4334 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
4335 "9040 dbg: READ @ sector %llu, "
James Smart9a6b09c2012-03-01 22:37:42 -05004336 "cnt %u, rpt %d\n",
James Smart6a9c52c2009-10-02 15:16:51 -04004337 (unsigned long long)scsi_get_lba(cmnd),
James Smart9a6b09c2012-03-01 22:37:42 -05004338 blk_rq_sectors(cmnd->request),
4339 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004340 else if (cmnd->cmnd[0] == WRITE_10)
4341 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smart9a6b09c2012-03-01 22:37:42 -05004342 "9041 dbg: WRITE @ sector %llu, "
4343 "cnt %u, wpt %d\n",
4344 (unsigned long long)scsi_get_lba(cmnd),
4345 blk_rq_sectors(cmnd->request),
4346 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004347 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004348 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
4349 }
4350
dea31012005-04-17 16:05:31 -05004351 if (err)
4352 goto out_host_busy_free_buf;
4353
James Smart2e0fef82007-06-17 19:56:36 -05004354 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea31012005-04-17 16:05:31 -05004355
James Smart977b5a02008-09-07 11:52:04 -04004356 atomic_inc(&ndlp->cmd_pending);
James Smart3772a992009-05-22 14:50:54 -04004357 err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
James Smart92d7f7b2007-06-17 19:56:38 -05004358 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
James Smarteaf15d52008-12-04 22:39:29 -05004359 if (err) {
4360 atomic_dec(&ndlp->cmd_pending);
dea31012005-04-17 16:05:31 -05004361 goto out_host_busy_free_buf;
James Smarteaf15d52008-12-04 22:39:29 -05004362 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004363 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004364 lpfc_sli_handle_fast_ring_event(phba,
4365 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4366
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004367 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4368 lpfc_poll_rearm_timer(phba);
4369 }
4370
dea31012005-04-17 16:05:31 -05004371 return 0;
4372
4373 out_host_busy_free_buf:
James Smartbcf4dbf2006-07-06 15:50:08 -04004374 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004375 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004376 out_host_busy:
4377 return SCSI_MLQUEUE_HOST_BUSY;
4378
Mike Christie34963432011-02-25 14:04:28 -06004379 out_tgt_busy:
4380 return SCSI_MLQUEUE_TARGET_BUSY;
4381
dea31012005-04-17 16:05:31 -05004382 out_fail_command:
James Smartb9a7c632012-08-03 12:35:24 -04004383 cmnd->scsi_done(cmnd);
dea31012005-04-17 16:05:31 -05004384 return 0;
4385}
4386
Jeff Garzikf2812332010-11-16 02:10:29 -05004387
James Smart9bad7672008-12-04 22:39:02 -05004388/**
James Smart3621a712009-04-06 18:47:14 -04004389 * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05004390 * @cmnd: Pointer to scsi_cmnd data structure.
4391 *
4392 * This routine aborts @cmnd pending in base driver.
4393 *
4394 * Return code :
4395 * 0x2003 - Error
4396 * 0x2002 - Success
4397 **/
dea31012005-04-17 16:05:31 -05004398static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05004399lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004400{
James Smart2e0fef82007-06-17 19:56:36 -05004401 struct Scsi_Host *shost = cmnd->device->host;
4402 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4403 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004404 struct lpfc_iocbq *iocb;
4405 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05004406 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05004407 IOCB_t *cmd, *icmd;
James Smart3a707302012-06-12 13:54:42 -04004408 int ret = SUCCESS, status = 0;
James Smartfa61a542008-01-11 01:52:42 -05004409 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004410
James Smart3a707302012-06-12 13:54:42 -04004411 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04004412 if (status != 0 && status != SUCCESS)
James Smart3a707302012-06-12 13:54:42 -04004413 return status;
James Smart4f2e66c2012-05-09 21:17:07 -04004414
4415 spin_lock_irq(&phba->hbalock);
4416 /* driver queued commands are in process of being flushed */
4417 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
4418 spin_unlock_irq(&phba->hbalock);
4419 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4420 "3168 SCSI Layer abort requested I/O has been "
4421 "flushed by LLD.\n");
4422 return FAILED;
4423 }
4424
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004425 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
James Smarteee88772010-09-29 11:19:08 -04004426 if (!lpfc_cmd) {
James Smart4f2e66c2012-05-09 21:17:07 -04004427 spin_unlock_irq(&phba->hbalock);
James Smarteee88772010-09-29 11:19:08 -04004428 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4429 "2873 SCSI Layer I/O Abort Request IO CMPL Status "
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04004430 "x%x ID %d LUN %d\n",
James Smart3a707302012-06-12 13:54:42 -04004431 SUCCESS, cmnd->device->id, cmnd->device->lun);
James Smarteee88772010-09-29 11:19:08 -04004432 return SUCCESS;
4433 }
dea31012005-04-17 16:05:31 -05004434
James Smart4f2e66c2012-05-09 21:17:07 -04004435 iocb = &lpfc_cmd->cur_iocbq;
4436 /* the command is in process of being cancelled */
4437 if (!(iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
4438 spin_unlock_irq(&phba->hbalock);
4439 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4440 "3169 SCSI Layer abort requested I/O has been "
4441 "cancelled by LLD.\n");
4442 return FAILED;
4443 }
dea31012005-04-17 16:05:31 -05004444 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004445 * If pCmd field of the corresponding lpfc_scsi_buf structure
4446 * points to a different SCSI command, then the driver has
4447 * already completed this command, but the midlayer did not
James Smart4f2e66c2012-05-09 21:17:07 -04004448 * see the completion before the eh fired. Just return SUCCESS.
dea31012005-04-17 16:05:31 -05004449 */
James Smart4f2e66c2012-05-09 21:17:07 -04004450 if (lpfc_cmd->pCmd != cmnd) {
4451 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4452 "3170 SCSI Layer abort requested I/O has been "
4453 "completed by LLD.\n");
4454 goto out_unlock;
4455 }
dea31012005-04-17 16:05:31 -05004456
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004457 BUG_ON(iocb->context1 != lpfc_cmd);
dea31012005-04-17 16:05:31 -05004458
James Smart4f2e66c2012-05-09 21:17:07 -04004459 abtsiocb = __lpfc_sli_get_iocbq(phba);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004460 if (abtsiocb == NULL) {
4461 ret = FAILED;
James Smart4f2e66c2012-05-09 21:17:07 -04004462 goto out_unlock;
dea31012005-04-17 16:05:31 -05004463 }
4464
dea31012005-04-17 16:05:31 -05004465 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004466 * The scsi command can not be in txq and it is in flight because the
4467 * pCmd is still pointig at the SCSI command we have to abort. There
4468 * is no need to search the txcmplq. Just send an abort to the FW.
dea31012005-04-17 16:05:31 -05004469 */
dea31012005-04-17 16:05:31 -05004470
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004471 cmd = &iocb->iocb;
4472 icmd = &abtsiocb->iocb;
4473 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
4474 icmd->un.acxri.abortContextTag = cmd->ulpContext;
James Smart3772a992009-05-22 14:50:54 -04004475 if (phba->sli_rev == LPFC_SLI_REV4)
4476 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
4477 else
4478 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05004479
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004480 icmd->ulpLe = 1;
4481 icmd->ulpClass = cmd->ulpClass;
James Smart5ffc2662009-11-18 15:39:44 -05004482
4483 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
4484 abtsiocb->fcp_wqidx = iocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05004485 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05004486
James Smart2e0fef82007-06-17 19:56:36 -05004487 if (lpfc_is_link_up(phba))
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004488 icmd->ulpCommand = CMD_ABORT_XRI_CN;
4489 else
4490 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea31012005-04-17 16:05:31 -05004491
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004492 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smart2e0fef82007-06-17 19:56:36 -05004493 abtsiocb->vport = vport;
James Smart4f2e66c2012-05-09 21:17:07 -04004494 /* no longer need the lock after this point */
4495 spin_unlock_irq(&phba->hbalock);
4496
James Smart3772a992009-05-22 14:50:54 -04004497 if (lpfc_sli_issue_iocb(phba, LPFC_FCP_RING, abtsiocb, 0) ==
4498 IOCB_ERROR) {
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004499 lpfc_sli_release_iocbq(phba, abtsiocb);
4500 ret = FAILED;
4501 goto out;
4502 }
4503
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004504 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
James Smart45ed1192009-10-02 15:17:02 -04004505 lpfc_sli_handle_fast_ring_event(phba,
4506 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004507
James Smartfa61a542008-01-11 01:52:42 -05004508 lpfc_cmd->waitq = &waitq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004509 /* Wait for abort to complete */
James Smartfa61a542008-01-11 01:52:42 -05004510 wait_event_timeout(waitq,
4511 (lpfc_cmd->pCmd != cmnd),
4512 (2*vport->cfg_devloss_tmo*HZ));
James Smartfa61a542008-01-11 01:52:42 -05004513 lpfc_cmd->waitq = NULL;
dea31012005-04-17 16:05:31 -05004514
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004515 if (lpfc_cmd->pCmd == cmnd) {
4516 ret = FAILED;
James Smarte8b62012007-08-02 11:10:09 -04004517 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4518 "0748 abort handler timed out waiting "
4519 "for abort to complete: ret %#x, ID %d, "
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04004520 "LUN %d\n",
4521 ret, cmnd->device->id, cmnd->device->lun);
dea31012005-04-17 16:05:31 -05004522 }
James Smart4f2e66c2012-05-09 21:17:07 -04004523 goto out;
dea31012005-04-17 16:05:31 -05004524
James Smart4f2e66c2012-05-09 21:17:07 -04004525out_unlock:
4526 spin_unlock_irq(&phba->hbalock);
4527out:
James Smarte8b62012007-08-02 11:10:09 -04004528 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4529 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04004530 "LUN %d\n", ret, cmnd->device->id,
4531 cmnd->device->lun);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004532 return ret;
dea31012005-04-17 16:05:31 -05004533}
4534
James Smartbbb9d182009-06-10 17:23:16 -04004535static char *
4536lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
4537{
4538 switch (task_mgmt_cmd) {
4539 case FCP_ABORT_TASK_SET:
4540 return "ABORT_TASK_SET";
4541 case FCP_CLEAR_TASK_SET:
4542 return "FCP_CLEAR_TASK_SET";
4543 case FCP_BUS_RESET:
4544 return "FCP_BUS_RESET";
4545 case FCP_LUN_RESET:
4546 return "FCP_LUN_RESET";
4547 case FCP_TARGET_RESET:
4548 return "FCP_TARGET_RESET";
4549 case FCP_CLEAR_ACA:
4550 return "FCP_CLEAR_ACA";
4551 case FCP_TERMINATE_TASK:
4552 return "FCP_TERMINATE_TASK";
4553 default:
4554 return "unknown";
4555 }
4556}
4557
4558/**
4559 * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
4560 * @vport: The virtual port for which this call is being executed.
4561 * @rdata: Pointer to remote port local data
4562 * @tgt_id: Target ID of remote device.
4563 * @lun_id: Lun number for the TMF
4564 * @task_mgmt_cmd: type of TMF to send
4565 *
4566 * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
4567 * a remote port.
4568 *
4569 * Return Code:
4570 * 0x2003 - Error
4571 * 0x2002 - Success.
4572 **/
4573static int
4574lpfc_send_taskmgmt(struct lpfc_vport *vport, struct lpfc_rport_data *rdata,
4575 unsigned tgt_id, unsigned int lun_id,
4576 uint8_t task_mgmt_cmd)
4577{
4578 struct lpfc_hba *phba = vport->phba;
4579 struct lpfc_scsi_buf *lpfc_cmd;
4580 struct lpfc_iocbq *iocbq;
4581 struct lpfc_iocbq *iocbqrsp;
James Smart5989b8d2010-10-22 11:06:56 -04004582 struct lpfc_nodelist *pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004583 int ret;
4584 int status;
4585
James Smart5989b8d2010-10-22 11:06:56 -04004586 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
James Smartbbb9d182009-06-10 17:23:16 -04004587 return FAILED;
4588
James Smart19ca7602010-11-20 23:11:55 -05004589 lpfc_cmd = lpfc_get_scsi_buf(phba, rdata->pnode);
James Smartbbb9d182009-06-10 17:23:16 -04004590 if (lpfc_cmd == NULL)
4591 return FAILED;
4592 lpfc_cmd->timeout = 60;
4593 lpfc_cmd->rdata = rdata;
4594
4595 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
4596 task_mgmt_cmd);
4597 if (!status) {
4598 lpfc_release_scsi_buf(phba, lpfc_cmd);
4599 return FAILED;
4600 }
4601
4602 iocbq = &lpfc_cmd->cur_iocbq;
4603 iocbqrsp = lpfc_sli_get_iocbq(phba);
4604 if (iocbqrsp == NULL) {
4605 lpfc_release_scsi_buf(phba, lpfc_cmd);
4606 return FAILED;
4607 }
4608
4609 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4610 "0702 Issue %s to TGT %d LUN %d "
James Smart6d368e52011-05-24 11:44:12 -04004611 "rpi x%x nlp_flag x%x Data: x%x x%x\n",
James Smartbbb9d182009-06-10 17:23:16 -04004612 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
James Smart6d368e52011-05-24 11:44:12 -04004613 pnode->nlp_rpi, pnode->nlp_flag, iocbq->sli4_xritag,
4614 iocbq->iocb_flag);
James Smartbbb9d182009-06-10 17:23:16 -04004615
4616 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
4617 iocbq, iocbqrsp, lpfc_cmd->timeout);
4618 if (status != IOCB_SUCCESS) {
4619 if (status == IOCB_TIMEDOUT) {
4620 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
4621 ret = TIMEOUT_ERROR;
4622 } else
4623 ret = FAILED;
4624 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
4625 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smart6d368e52011-05-24 11:44:12 -04004626 "0727 TMF %s to TGT %d LUN %d failed (%d, %d) "
4627 "iocb_flag x%x\n",
James Smartbbb9d182009-06-10 17:23:16 -04004628 lpfc_taskmgmt_name(task_mgmt_cmd),
4629 tgt_id, lun_id, iocbqrsp->iocb.ulpStatus,
James Smart6d368e52011-05-24 11:44:12 -04004630 iocbqrsp->iocb.un.ulpWord[4],
4631 iocbq->iocb_flag);
James Smart2a9bf3d2010-06-07 15:24:45 -04004632 } else if (status == IOCB_BUSY)
4633 ret = FAILED;
4634 else
James Smartbbb9d182009-06-10 17:23:16 -04004635 ret = SUCCESS;
4636
4637 lpfc_sli_release_iocbq(phba, iocbqrsp);
4638
4639 if (ret != TIMEOUT_ERROR)
4640 lpfc_release_scsi_buf(phba, lpfc_cmd);
4641
4642 return ret;
4643}
4644
4645/**
4646 * lpfc_chk_tgt_mapped -
4647 * @vport: The virtual port to check on
4648 * @cmnd: Pointer to scsi_cmnd data structure.
4649 *
4650 * This routine delays until the scsi target (aka rport) for the
4651 * command exists (is present and logged in) or we declare it non-existent.
4652 *
4653 * Return code :
4654 * 0x2003 - Error
4655 * 0x2002 - Success
4656 **/
4657static int
4658lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
4659{
4660 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05004661 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004662 unsigned long later;
4663
James Smart1c6f4ef52009-11-18 15:40:49 -05004664 if (!rdata) {
4665 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4666 "0797 Tgt Map rport failure: rdata x%p\n", rdata);
4667 return FAILED;
4668 }
4669 pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004670 /*
4671 * If target is not in a MAPPED state, delay until
4672 * target is rediscovered or devloss timeout expires.
4673 */
4674 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
4675 while (time_after(later, jiffies)) {
4676 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
4677 return FAILED;
4678 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
4679 return SUCCESS;
4680 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
4681 rdata = cmnd->device->hostdata;
4682 if (!rdata)
4683 return FAILED;
4684 pnode = rdata->pnode;
4685 }
4686 if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
4687 (pnode->nlp_state != NLP_STE_MAPPED_NODE))
4688 return FAILED;
4689 return SUCCESS;
4690}
4691
4692/**
4693 * lpfc_reset_flush_io_context -
4694 * @vport: The virtual port (scsi_host) for the flush context
4695 * @tgt_id: If aborting by Target contect - specifies the target id
4696 * @lun_id: If aborting by Lun context - specifies the lun id
4697 * @context: specifies the context level to flush at.
4698 *
4699 * After a reset condition via TMF, we need to flush orphaned i/o
4700 * contexts from the adapter. This routine aborts any contexts
4701 * outstanding, then waits for their completions. The wait is
4702 * bounded by devloss_tmo though.
4703 *
4704 * Return code :
4705 * 0x2003 - Error
4706 * 0x2002 - Success
4707 **/
4708static int
4709lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
4710 uint64_t lun_id, lpfc_ctx_cmd context)
4711{
4712 struct lpfc_hba *phba = vport->phba;
4713 unsigned long later;
4714 int cnt;
4715
4716 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
4717 if (cnt)
4718 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
4719 tgt_id, lun_id, context);
4720 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
4721 while (time_after(later, jiffies) && cnt) {
4722 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
4723 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
4724 }
4725 if (cnt) {
4726 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4727 "0724 I/O flush failure for context %s : cnt x%x\n",
4728 ((context == LPFC_CTX_LUN) ? "LUN" :
4729 ((context == LPFC_CTX_TGT) ? "TGT" :
4730 ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
4731 cnt);
4732 return FAILED;
4733 }
4734 return SUCCESS;
4735}
4736
James Smart9bad7672008-12-04 22:39:02 -05004737/**
James Smart3621a712009-04-06 18:47:14 -04004738 * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
James Smart9bad7672008-12-04 22:39:02 -05004739 * @cmnd: Pointer to scsi_cmnd data structure.
4740 *
James Smartbbb9d182009-06-10 17:23:16 -04004741 * This routine does a device reset by sending a LUN_RESET task management
James Smart9bad7672008-12-04 22:39:02 -05004742 * command.
4743 *
4744 * Return code :
4745 * 0x2003 - Error
James Smart3621a712009-04-06 18:47:14 -04004746 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05004747 **/
dea31012005-04-17 16:05:31 -05004748static int
James Smart7054a602007-04-25 09:52:34 -04004749lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004750{
James Smart2e0fef82007-06-17 19:56:36 -05004751 struct Scsi_Host *shost = cmnd->device->host;
4752 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05004753 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05004754 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004755 unsigned tgt_id = cmnd->device->id;
4756 unsigned int lun_id = cmnd->device->lun;
James Smartea2151b2008-09-07 11:52:10 -04004757 struct lpfc_scsi_event_header scsi_event;
James Smart3a707302012-06-12 13:54:42 -04004758 int status, ret = SUCCESS;
dea31012005-04-17 16:05:31 -05004759
James Smart1c6f4ef52009-11-18 15:40:49 -05004760 if (!rdata) {
4761 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4762 "0798 Device Reset rport failure: rdata x%p\n", rdata);
4763 return FAILED;
4764 }
4765 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04004766 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04004767 if (status != 0 && status != SUCCESS)
James Smart589a52d2010-07-14 15:30:54 -04004768 return status;
James Smartbbb9d182009-06-10 17:23:16 -04004769
4770 status = lpfc_chk_tgt_mapped(vport, cmnd);
4771 if (status == FAILED) {
4772 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4773 "0721 Device Reset rport failure: rdata x%p\n", rdata);
4774 return FAILED;
4775 }
4776
4777 scsi_event.event_type = FC_REG_SCSI_EVENT;
4778 scsi_event.subcategory = LPFC_EVENT_LUNRESET;
4779 scsi_event.lun = lun_id;
4780 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
4781 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
4782
4783 fc_host_post_vendor_event(shost, fc_get_event_number(),
4784 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
4785
4786 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
4787 FCP_LUN_RESET);
4788
4789 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4790 "0713 SCSI layer issued Device Reset (%d, %d) "
4791 "return x%x\n", tgt_id, lun_id, status);
4792
dea31012005-04-17 16:05:31 -05004793 /*
James Smartbbb9d182009-06-10 17:23:16 -04004794 * We have to clean up i/o as : they may be orphaned by the TMF;
4795 * or if the TMF failed, they may be in an indeterminate state.
4796 * So, continue on.
4797 * We will report success if all the i/o aborts successfully.
dea31012005-04-17 16:05:31 -05004798 */
James Smart3a707302012-06-12 13:54:42 -04004799 ret = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
James Smartbbb9d182009-06-10 17:23:16 -04004800 LPFC_CTX_LUN);
James Smart3a707302012-06-12 13:54:42 -04004801 return ret;
James Smartbbb9d182009-06-10 17:23:16 -04004802}
4803
4804/**
4805 * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
4806 * @cmnd: Pointer to scsi_cmnd data structure.
4807 *
4808 * This routine does a target reset by sending a TARGET_RESET task management
4809 * command.
4810 *
4811 * Return code :
4812 * 0x2003 - Error
4813 * 0x2002 - Success
4814 **/
4815static int
4816lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
4817{
4818 struct Scsi_Host *shost = cmnd->device->host;
4819 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4820 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05004821 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004822 unsigned tgt_id = cmnd->device->id;
4823 unsigned int lun_id = cmnd->device->lun;
4824 struct lpfc_scsi_event_header scsi_event;
James Smart3a707302012-06-12 13:54:42 -04004825 int status, ret = SUCCESS;
James Smartbbb9d182009-06-10 17:23:16 -04004826
James Smart1c6f4ef52009-11-18 15:40:49 -05004827 if (!rdata) {
4828 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4829 "0799 Target Reset rport failure: rdata x%p\n", rdata);
4830 return FAILED;
4831 }
4832 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04004833 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04004834 if (status != 0 && status != SUCCESS)
James Smart589a52d2010-07-14 15:30:54 -04004835 return status;
James Smartbbb9d182009-06-10 17:23:16 -04004836
4837 status = lpfc_chk_tgt_mapped(vport, cmnd);
4838 if (status == FAILED) {
4839 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4840 "0722 Target Reset rport failure: rdata x%p\n", rdata);
4841 return FAILED;
dea31012005-04-17 16:05:31 -05004842 }
James Smartea2151b2008-09-07 11:52:10 -04004843
4844 scsi_event.event_type = FC_REG_SCSI_EVENT;
4845 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
4846 scsi_event.lun = 0;
4847 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
4848 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
4849
James Smartbbb9d182009-06-10 17:23:16 -04004850 fc_host_post_vendor_event(shost, fc_get_event_number(),
4851 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -04004852
James Smartbbb9d182009-06-10 17:23:16 -04004853 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
4854 FCP_TARGET_RESET);
dea31012005-04-17 16:05:31 -05004855
James Smarte8b62012007-08-02 11:10:09 -04004856 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smartbbb9d182009-06-10 17:23:16 -04004857 "0723 SCSI layer issued Target Reset (%d, %d) "
4858 "return x%x\n", tgt_id, lun_id, status);
4859
4860 /*
4861 * We have to clean up i/o as : they may be orphaned by the TMF;
4862 * or if the TMF failed, they may be in an indeterminate state.
4863 * So, continue on.
4864 * We will report success if all the i/o aborts successfully.
4865 */
James Smart3a707302012-06-12 13:54:42 -04004866 ret = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
4867 LPFC_CTX_TGT);
4868 return ret;
dea31012005-04-17 16:05:31 -05004869}
4870
James Smart9bad7672008-12-04 22:39:02 -05004871/**
James Smart3621a712009-04-06 18:47:14 -04004872 * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05004873 * @cmnd: Pointer to scsi_cmnd data structure.
4874 *
James Smartbbb9d182009-06-10 17:23:16 -04004875 * This routine does target reset to all targets on @cmnd->device->host.
4876 * This emulates Parallel SCSI Bus Reset Semantics.
James Smart9bad7672008-12-04 22:39:02 -05004877 *
James Smartbbb9d182009-06-10 17:23:16 -04004878 * Return code :
4879 * 0x2003 - Error
4880 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05004881 **/
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04004882static int
James Smart7054a602007-04-25 09:52:34 -04004883lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004884{
James Smart2e0fef82007-06-17 19:56:36 -05004885 struct Scsi_Host *shost = cmnd->device->host;
4886 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05004887 struct lpfc_nodelist *ndlp = NULL;
James Smartea2151b2008-09-07 11:52:10 -04004888 struct lpfc_scsi_event_header scsi_event;
James Smartbbb9d182009-06-10 17:23:16 -04004889 int match;
4890 int ret = SUCCESS, status, i;
James Smartea2151b2008-09-07 11:52:10 -04004891
4892 scsi_event.event_type = FC_REG_SCSI_EVENT;
4893 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
4894 scsi_event.lun = 0;
4895 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
4896 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
4897
James Smartbbb9d182009-06-10 17:23:16 -04004898 fc_host_post_vendor_event(shost, fc_get_event_number(),
4899 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
dea31012005-04-17 16:05:31 -05004900
James Smartbf086112011-08-21 21:48:13 -04004901 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04004902 if (status != 0 && status != SUCCESS)
James Smartbf086112011-08-21 21:48:13 -04004903 return status;
James Smartbbb9d182009-06-10 17:23:16 -04004904
dea31012005-04-17 16:05:31 -05004905 /*
4906 * Since the driver manages a single bus device, reset all
4907 * targets known to the driver. Should any target reset
4908 * fail, this routine returns failure to the midlayer.
4909 */
James Smarte17da182006-07-06 15:49:25 -04004910 for (i = 0; i < LPFC_MAX_TARGET; i++) {
James Smart685f0bf2007-04-25 09:53:08 -04004911 /* Search for mapped node by target ID */
dea31012005-04-17 16:05:31 -05004912 match = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004913 spin_lock_irq(shost->host_lock);
4914 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05004915 if (!NLP_CHK_NODE_ACT(ndlp))
4916 continue;
James Smart685f0bf2007-04-25 09:53:08 -04004917 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
James Smart915caaa2008-06-14 22:52:38 -04004918 ndlp->nlp_sid == i &&
James Smart685f0bf2007-04-25 09:53:08 -04004919 ndlp->rport) {
dea31012005-04-17 16:05:31 -05004920 match = 1;
4921 break;
4922 }
4923 }
James Smart2e0fef82007-06-17 19:56:36 -05004924 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004925 if (!match)
4926 continue;
James Smartbbb9d182009-06-10 17:23:16 -04004927
4928 status = lpfc_send_taskmgmt(vport, ndlp->rport->dd_data,
4929 i, 0, FCP_TARGET_RESET);
4930
4931 if (status != SUCCESS) {
James Smarte8b62012007-08-02 11:10:09 -04004932 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4933 "0700 Bus Reset on target %d failed\n",
4934 i);
James Smart915caaa2008-06-14 22:52:38 -04004935 ret = FAILED;
dea31012005-04-17 16:05:31 -05004936 }
4937 }
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05004938 /*
James Smartbbb9d182009-06-10 17:23:16 -04004939 * We have to clean up i/o as : they may be orphaned by the TMFs
4940 * above; or if any of the TMFs failed, they may be in an
4941 * indeterminate state.
4942 * We will report success if all the i/o aborts successfully.
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05004943 */
James Smartbbb9d182009-06-10 17:23:16 -04004944
4945 status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
4946 if (status != SUCCESS)
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05004947 ret = FAILED;
James Smartbbb9d182009-06-10 17:23:16 -04004948
James Smarte8b62012007-08-02 11:10:09 -04004949 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4950 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea31012005-04-17 16:05:31 -05004951 return ret;
4952}
4953
James Smart9bad7672008-12-04 22:39:02 -05004954/**
James Smart27b01b82012-05-09 21:19:44 -04004955 * lpfc_host_reset_handler - scsi_host_template eh_host_reset_handler entry pt
4956 * @cmnd: Pointer to scsi_cmnd data structure.
4957 *
4958 * This routine does host reset to the adaptor port. It brings the HBA
4959 * offline, performs a board restart, and then brings the board back online.
4960 * The lpfc_offline calls lpfc_sli_hba_down which will abort and local
4961 * reject all outstanding SCSI commands to the host and error returned
4962 * back to SCSI mid-level. As this will be SCSI mid-level's last resort
4963 * of error handling, it will only return error if resetting of the adapter
4964 * is not successful; in all other cases, will return success.
4965 *
4966 * Return code :
4967 * 0x2003 - Error
4968 * 0x2002 - Success
4969 **/
4970static int
4971lpfc_host_reset_handler(struct scsi_cmnd *cmnd)
4972{
4973 struct Scsi_Host *shost = cmnd->device->host;
4974 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4975 struct lpfc_hba *phba = vport->phba;
4976 int rc, ret = SUCCESS;
4977
James Smart618a5232012-06-12 13:54:36 -04004978 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart27b01b82012-05-09 21:19:44 -04004979 lpfc_offline(phba);
4980 rc = lpfc_sli_brdrestart(phba);
4981 if (rc)
4982 ret = FAILED;
4983 lpfc_online(phba);
4984 lpfc_unblock_mgmt_io(phba);
4985
4986 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
4987 "3172 SCSI layer issued Host Reset Data: x%x\n", ret);
4988 return ret;
4989}
4990
4991/**
James Smart3621a712009-04-06 18:47:14 -04004992 * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
James Smart9bad7672008-12-04 22:39:02 -05004993 * @sdev: Pointer to scsi_device.
4994 *
4995 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
4996 * globally available list of scsi buffers. This routine also makes sure scsi
4997 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
4998 * of scsi buffer exists for the lifetime of the driver.
4999 *
5000 * Return codes:
5001 * non-0 - Error
5002 * 0 - Success
5003 **/
dea31012005-04-17 16:05:31 -05005004static int
dea31012005-04-17 16:05:31 -05005005lpfc_slave_alloc(struct scsi_device *sdev)
5006{
James Smart2e0fef82007-06-17 19:56:36 -05005007 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5008 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005009 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
James Smart3772a992009-05-22 14:50:54 -04005010 uint32_t total = 0;
dea31012005-04-17 16:05:31 -05005011 uint32_t num_to_alloc = 0;
James Smart3772a992009-05-22 14:50:54 -04005012 int num_allocated = 0;
James Smartd7c47992010-06-08 18:31:54 -04005013 uint32_t sdev_cnt;
dea31012005-04-17 16:05:31 -05005014
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005015 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05005016 return -ENXIO;
5017
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005018 sdev->hostdata = rport->dd_data;
James Smartd7c47992010-06-08 18:31:54 -04005019 sdev_cnt = atomic_inc_return(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05005020
5021 /*
5022 * Populate the cmds_per_lun count scsi_bufs into this host's globally
5023 * available list of scsi buffers. Don't allocate more than the
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04005024 * HBA limit conveyed to the midlayer via the host structure. The
5025 * formula accounts for the lun_queue_depth + error handlers + 1
5026 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea31012005-04-17 16:05:31 -05005027 */
5028 total = phba->total_scsi_bufs;
James Smart3de2a652007-08-02 11:09:59 -04005029 num_to_alloc = vport->cfg_lun_queue_depth + 2;
James Smart92d7f7b2007-06-17 19:56:38 -05005030
James Smartd7c47992010-06-08 18:31:54 -04005031 /* If allocated buffers are enough do nothing */
5032 if ((sdev_cnt * (vport->cfg_lun_queue_depth + 2)) < total)
5033 return 0;
5034
James Smart92d7f7b2007-06-17 19:56:38 -05005035 /* Allow some exchanges to be available always to complete discovery */
5036 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04005037 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5038 "0704 At limitation of %d preallocated "
5039 "command buffers\n", total);
dea31012005-04-17 16:05:31 -05005040 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05005041 /* Allow some exchanges to be available always to complete discovery */
5042 } else if (total + num_to_alloc >
5043 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04005044 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5045 "0705 Allocation request of %d "
5046 "command buffers will exceed max of %d. "
5047 "Reducing allocation request to %d.\n",
5048 num_to_alloc, phba->cfg_hba_queue_depth,
5049 (phba->cfg_hba_queue_depth - total));
dea31012005-04-17 16:05:31 -05005050 num_to_alloc = phba->cfg_hba_queue_depth - total;
5051 }
James Smart3772a992009-05-22 14:50:54 -04005052 num_allocated = lpfc_new_scsi_buf(vport, num_to_alloc);
5053 if (num_to_alloc != num_allocated) {
5054 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5055 "0708 Allocation request of %d "
5056 "command buffers did not succeed. "
5057 "Allocated %d buffers.\n",
5058 num_to_alloc, num_allocated);
dea31012005-04-17 16:05:31 -05005059 }
James Smart1c6f4ef52009-11-18 15:40:49 -05005060 if (num_allocated > 0)
5061 phba->total_scsi_bufs += num_allocated;
dea31012005-04-17 16:05:31 -05005062 return 0;
5063}
5064
James Smart9bad7672008-12-04 22:39:02 -05005065/**
James Smart3621a712009-04-06 18:47:14 -04005066 * lpfc_slave_configure - scsi_host_template slave_configure entry point
James Smart9bad7672008-12-04 22:39:02 -05005067 * @sdev: Pointer to scsi_device.
5068 *
5069 * This routine configures following items
5070 * - Tag command queuing support for @sdev if supported.
James Smart9bad7672008-12-04 22:39:02 -05005071 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
5072 *
5073 * Return codes:
5074 * 0 - Success
5075 **/
dea31012005-04-17 16:05:31 -05005076static int
5077lpfc_slave_configure(struct scsi_device *sdev)
5078{
James Smart2e0fef82007-06-17 19:56:36 -05005079 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5080 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05005081
5082 if (sdev->tagged_supported)
James Smart3de2a652007-08-02 11:09:59 -04005083 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05005084 else
James Smart3de2a652007-08-02 11:09:59 -04005085 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05005086
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005087 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04005088 lpfc_sli_handle_fast_ring_event(phba,
5089 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005090 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
5091 lpfc_poll_rearm_timer(phba);
5092 }
5093
dea31012005-04-17 16:05:31 -05005094 return 0;
5095}
5096
James Smart9bad7672008-12-04 22:39:02 -05005097/**
James Smart3621a712009-04-06 18:47:14 -04005098 * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
James Smart9bad7672008-12-04 22:39:02 -05005099 * @sdev: Pointer to scsi_device.
5100 *
5101 * This routine sets @sdev hostatdata filed to null.
5102 **/
dea31012005-04-17 16:05:31 -05005103static void
5104lpfc_slave_destroy(struct scsi_device *sdev)
5105{
James Smartd7c47992010-06-08 18:31:54 -04005106 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5107 struct lpfc_hba *phba = vport->phba;
5108 atomic_dec(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05005109 sdev->hostdata = NULL;
5110 return;
5111}
5112
James Smart92d7f7b2007-06-17 19:56:38 -05005113
dea31012005-04-17 16:05:31 -05005114struct scsi_host_template lpfc_template = {
5115 .module = THIS_MODULE,
5116 .name = LPFC_DRIVER_NAME,
5117 .info = lpfc_info,
5118 .queuecommand = lpfc_queuecommand,
5119 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04005120 .eh_device_reset_handler = lpfc_device_reset_handler,
5121 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart7054a602007-04-25 09:52:34 -04005122 .eh_bus_reset_handler = lpfc_bus_reset_handler,
James Smart27b01b82012-05-09 21:19:44 -04005123 .eh_host_reset_handler = lpfc_host_reset_handler,
dea31012005-04-17 16:05:31 -05005124 .slave_alloc = lpfc_slave_alloc,
5125 .slave_configure = lpfc_slave_configure,
5126 .slave_destroy = lpfc_slave_destroy,
James Smart47a86172007-04-25 09:53:22 -04005127 .scan_finished = lpfc_scan_finished,
dea31012005-04-17 16:05:31 -05005128 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05005129 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
dea31012005-04-17 16:05:31 -05005130 .cmd_per_lun = LPFC_CMD_PER_LUN,
5131 .use_clustering = ENABLE_CLUSTERING,
James Smart2e0fef82007-06-17 19:56:36 -05005132 .shost_attrs = lpfc_hba_attrs,
James.Smart@Emulex.Com564b2962005-06-25 10:34:17 -04005133 .max_sectors = 0xFFFF,
James Smartf1c3b0f2009-07-19 10:01:32 -04005134 .vendor_id = LPFC_NL_VENDOR_ID,
James Smart5ffc2662009-11-18 15:39:44 -05005135 .change_queue_depth = lpfc_change_queue_depth,
dea31012005-04-17 16:05:31 -05005136};
James Smart3de2a652007-08-02 11:09:59 -04005137
5138struct scsi_host_template lpfc_vport_template = {
5139 .module = THIS_MODULE,
5140 .name = LPFC_DRIVER_NAME,
5141 .info = lpfc_info,
5142 .queuecommand = lpfc_queuecommand,
5143 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04005144 .eh_device_reset_handler = lpfc_device_reset_handler,
5145 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart3de2a652007-08-02 11:09:59 -04005146 .eh_bus_reset_handler = lpfc_bus_reset_handler,
5147 .slave_alloc = lpfc_slave_alloc,
5148 .slave_configure = lpfc_slave_configure,
5149 .slave_destroy = lpfc_slave_destroy,
5150 .scan_finished = lpfc_scan_finished,
5151 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05005152 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
James Smart3de2a652007-08-02 11:09:59 -04005153 .cmd_per_lun = LPFC_CMD_PER_LUN,
5154 .use_clustering = ENABLE_CLUSTERING,
5155 .shost_attrs = lpfc_vport_attrs,
5156 .max_sectors = 0xFFFF,
James Smart5ffc2662009-11-18 15:39:44 -05005157 .change_queue_depth = lpfc_change_queue_depth,
James Smart3de2a652007-08-02 11:09:59 -04005158};