blob: e75210d67970568ef1ed194c5d0d8f1fe98d3b90 [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
James Smartf9bb2da2011-10-10 21:34:11 -040063struct scsi_dif_tuple {
64 __be16 guard_tag; /* Checksum */
65 __be16 app_tag; /* Opaque storage */
66 __be32 ref_tag; /* Target LBA or indirect LBA */
67};
68
James Smartda0436e2009-05-22 14:51:39 -040069static void
70lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
James Smart1c6f4ef52009-11-18 15:40:49 -050071static void
72lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
James Smarte2a0a9d2008-12-04 22:40:02 -050073
74static void
James Smart6a9c52c2009-10-02 15:16:51 -040075lpfc_debug_save_data(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
James Smarte2a0a9d2008-12-04 22:40:02 -050076{
77 void *src, *dst;
78 struct scatterlist *sgde = scsi_sglist(cmnd);
79
80 if (!_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -040081 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
82 "9050 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
James Smarte2a0a9d2008-12-04 22:40:02 -050083 __func__);
84 return;
85 }
86
87
88 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -040089 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
90 "9051 BLKGRD: ERROR: data scatterlist is null\n");
James Smarte2a0a9d2008-12-04 22:40:02 -050091 return;
92 }
93
94 dst = (void *) _dump_buf_data;
95 while (sgde) {
96 src = sg_virt(sgde);
97 memcpy(dst, src, sgde->length);
98 dst += sgde->length;
99 sgde = sg_next(sgde);
100 }
101}
102
103static void
James Smart6a9c52c2009-10-02 15:16:51 -0400104lpfc_debug_save_dif(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
James Smarte2a0a9d2008-12-04 22:40:02 -0500105{
106 void *src, *dst;
107 struct scatterlist *sgde = scsi_prot_sglist(cmnd);
108
109 if (!_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -0400110 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
111 "9052 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
James Smarte2a0a9d2008-12-04 22:40:02 -0500112 __func__);
113 return;
114 }
115
116 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -0400117 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
118 "9053 BLKGRD: ERROR: prot scatterlist is null\n");
James Smarte2a0a9d2008-12-04 22:40:02 -0500119 return;
120 }
121
122 dst = _dump_buf_dif;
123 while (sgde) {
124 src = sg_virt(sgde);
125 memcpy(dst, src, sgde->length);
126 dst += sgde->length;
127 sgde = sg_next(sgde);
128 }
129}
130
James Smartea2151b2008-09-07 11:52:10 -0400131/**
James Smartf1126682009-06-10 17:22:44 -0400132 * lpfc_sli4_set_rsp_sgl_last - Set the last bit in the response sge.
133 * @phba: Pointer to HBA object.
134 * @lpfc_cmd: lpfc scsi command object pointer.
135 *
136 * This function is called from the lpfc_prep_task_mgmt_cmd function to
137 * set the last bit in the response sge entry.
138 **/
139static void
140lpfc_sli4_set_rsp_sgl_last(struct lpfc_hba *phba,
141 struct lpfc_scsi_buf *lpfc_cmd)
142{
143 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
144 if (sgl) {
145 sgl += 1;
146 sgl->word2 = le32_to_cpu(sgl->word2);
147 bf_set(lpfc_sli4_sge_last, sgl, 1);
148 sgl->word2 = cpu_to_le32(sgl->word2);
149 }
150}
151
152/**
James Smart3621a712009-04-06 18:47:14 -0400153 * lpfc_update_stats - Update statistical data for the command completion
James Smartea2151b2008-09-07 11:52:10 -0400154 * @phba: Pointer to HBA object.
155 * @lpfc_cmd: lpfc scsi command object pointer.
156 *
157 * This function is called when there is a command completion and this
158 * function updates the statistical data for the command completion.
159 **/
160static void
161lpfc_update_stats(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
162{
163 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
164 struct lpfc_nodelist *pnode = rdata->pnode;
165 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
166 unsigned long flags;
167 struct Scsi_Host *shost = cmd->device->host;
168 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
169 unsigned long latency;
170 int i;
171
172 if (cmd->result)
173 return;
174
James Smart9f1e1b52008-12-04 22:39:40 -0500175 latency = jiffies_to_msecs((long)jiffies - (long)lpfc_cmd->start_time);
176
James Smartea2151b2008-09-07 11:52:10 -0400177 spin_lock_irqsave(shost->host_lock, flags);
178 if (!vport->stat_data_enabled ||
179 vport->stat_data_blocked ||
James Smart5989b8d2010-10-22 11:06:56 -0400180 !pnode ||
James Smartea2151b2008-09-07 11:52:10 -0400181 !pnode->lat_data ||
182 (phba->bucket_type == LPFC_NO_BUCKET)) {
183 spin_unlock_irqrestore(shost->host_lock, flags);
184 return;
185 }
James Smartea2151b2008-09-07 11:52:10 -0400186
187 if (phba->bucket_type == LPFC_LINEAR_BUCKET) {
188 i = (latency + phba->bucket_step - 1 - phba->bucket_base)/
189 phba->bucket_step;
James Smart9f1e1b52008-12-04 22:39:40 -0500190 /* check array subscript bounds */
191 if (i < 0)
192 i = 0;
193 else if (i >= LPFC_MAX_BUCKET_COUNT)
194 i = LPFC_MAX_BUCKET_COUNT - 1;
James Smartea2151b2008-09-07 11:52:10 -0400195 } else {
196 for (i = 0; i < LPFC_MAX_BUCKET_COUNT-1; i++)
197 if (latency <= (phba->bucket_base +
198 ((1<<i)*phba->bucket_step)))
199 break;
200 }
201
202 pnode->lat_data[i].cmd_count++;
203 spin_unlock_irqrestore(shost->host_lock, flags);
204}
205
James Smartea2151b2008-09-07 11:52:10 -0400206/**
James Smart3621a712009-04-06 18:47:14 -0400207 * lpfc_send_sdev_queuedepth_change_event - Posts a queuedepth change event
James Smartea2151b2008-09-07 11:52:10 -0400208 * @phba: Pointer to HBA context object.
209 * @vport: Pointer to vport object.
210 * @ndlp: Pointer to FC node associated with the target.
211 * @lun: Lun number of the scsi device.
212 * @old_val: Old value of the queue depth.
213 * @new_val: New value of the queue depth.
214 *
215 * This function sends an event to the mgmt application indicating
216 * there is a change in the scsi device queue depth.
217 **/
218static void
219lpfc_send_sdev_queuedepth_change_event(struct lpfc_hba *phba,
220 struct lpfc_vport *vport,
221 struct lpfc_nodelist *ndlp,
222 uint32_t lun,
223 uint32_t old_val,
224 uint32_t new_val)
225{
226 struct lpfc_fast_path_event *fast_path_evt;
227 unsigned long flags;
228
229 fast_path_evt = lpfc_alloc_fast_evt(phba);
230 if (!fast_path_evt)
231 return;
232
233 fast_path_evt->un.queue_depth_evt.scsi_event.event_type =
234 FC_REG_SCSI_EVENT;
235 fast_path_evt->un.queue_depth_evt.scsi_event.subcategory =
236 LPFC_EVENT_VARQUEDEPTH;
237
238 /* Report all luns with change in queue depth */
239 fast_path_evt->un.queue_depth_evt.scsi_event.lun = lun;
240 if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
241 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwpn,
242 &ndlp->nlp_portname, sizeof(struct lpfc_name));
243 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwnn,
244 &ndlp->nlp_nodename, sizeof(struct lpfc_name));
245 }
246
247 fast_path_evt->un.queue_depth_evt.oldval = old_val;
248 fast_path_evt->un.queue_depth_evt.newval = new_val;
249 fast_path_evt->vport = vport;
250
251 fast_path_evt->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
252 spin_lock_irqsave(&phba->hbalock, flags);
253 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
254 spin_unlock_irqrestore(&phba->hbalock, flags);
255 lpfc_worker_wake_up(phba);
256
257 return;
258}
259
James Smart9bad7672008-12-04 22:39:02 -0500260/**
James Smart5ffc2662009-11-18 15:39:44 -0500261 * lpfc_change_queue_depth - Alter scsi device queue depth
262 * @sdev: Pointer the scsi device on which to change the queue depth.
263 * @qdepth: New queue depth to set the sdev to.
264 * @reason: The reason for the queue depth change.
265 *
266 * This function is called by the midlayer and the LLD to alter the queue
267 * depth for a scsi device. This function sets the queue depth to the new
268 * value and sends an event out to log the queue depth change.
269 **/
270int
271lpfc_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
272{
273 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
274 struct lpfc_hba *phba = vport->phba;
275 struct lpfc_rport_data *rdata;
276 unsigned long new_queue_depth, old_queue_depth;
277
278 old_queue_depth = sdev->queue_depth;
279 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
280 new_queue_depth = sdev->queue_depth;
281 rdata = sdev->hostdata;
282 if (rdata)
283 lpfc_send_sdev_queuedepth_change_event(phba, vport,
284 rdata->pnode, sdev->lun,
285 old_queue_depth,
286 new_queue_depth);
287 return sdev->queue_depth;
288}
289
290/**
James Smartfe8f7f92013-01-03 15:43:03 -0500291 * lpfc_change_queue_type() - Change a device's scsi tag queuing type
292 * @sdev: Pointer the scsi device whose queue depth is to change
293 * @tag_type: Identifier for queue tag type
294 */
295static int
296lpfc_change_queue_type(struct scsi_device *sdev, int tag_type)
297{
298 if (sdev->tagged_supported) {
299 scsi_set_tag_type(sdev, tag_type);
300 if (tag_type)
301 scsi_activate_tcq(sdev, sdev->queue_depth);
302 else
303 scsi_deactivate_tcq(sdev, sdev->queue_depth);
304 } else
305 tag_type = 0;
306
307 return tag_type;
308}
309
310/**
James Smart3621a712009-04-06 18:47:14 -0400311 * lpfc_rampdown_queue_depth - Post RAMP_DOWN_QUEUE event to worker thread
James Smart9bad7672008-12-04 22:39:02 -0500312 * @phba: The Hba for which this call is being executed.
313 *
314 * This routine is called when there is resource error in driver or firmware.
315 * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
316 * posts at most 1 event each second. This routine wakes up worker thread of
317 * @phba to process WORKER_RAM_DOWN_EVENT event.
318 *
319 * This routine should be called with no lock held.
320 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500321void
James Smarteaf15d52008-12-04 22:39:29 -0500322lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
James Smart92d7f7b2007-06-17 19:56:38 -0500323{
324 unsigned long flags;
James Smart5e9d9b82008-06-14 22:52:53 -0400325 uint32_t evt_posted;
James Smart92d7f7b2007-06-17 19:56:38 -0500326
327 spin_lock_irqsave(&phba->hbalock, flags);
328 atomic_inc(&phba->num_rsrc_err);
329 phba->last_rsrc_error_time = jiffies;
330
331 if ((phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL) > jiffies) {
332 spin_unlock_irqrestore(&phba->hbalock, flags);
333 return;
334 }
335
336 phba->last_ramp_down_time = jiffies;
337
338 spin_unlock_irqrestore(&phba->hbalock, flags);
339
340 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400341 evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
342 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500343 phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500344 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
345
James Smart5e9d9b82008-06-14 22:52:53 -0400346 if (!evt_posted)
347 lpfc_worker_wake_up(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500348 return;
349}
350
James Smart9bad7672008-12-04 22:39:02 -0500351/**
James Smart3621a712009-04-06 18:47:14 -0400352 * lpfc_rampup_queue_depth - Post RAMP_UP_QUEUE event for worker thread
James Smart9bad7672008-12-04 22:39:02 -0500353 * @phba: The Hba for which this call is being executed.
354 *
355 * This routine post WORKER_RAMP_UP_QUEUE event for @phba vport. This routine
356 * post at most 1 event every 5 minute after last_ramp_up_time or
357 * last_rsrc_error_time. This routine wakes up worker thread of @phba
358 * to process WORKER_RAM_DOWN_EVENT event.
359 *
360 * This routine should be called with no lock held.
361 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500362static inline void
James Smart3de2a652007-08-02 11:09:59 -0400363lpfc_rampup_queue_depth(struct lpfc_vport *vport,
James Smarta257bf92009-04-06 18:48:10 -0400364 uint32_t queue_depth)
James Smart92d7f7b2007-06-17 19:56:38 -0500365{
366 unsigned long flags;
James Smart3de2a652007-08-02 11:09:59 -0400367 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -0400368 uint32_t evt_posted;
James Smart92d7f7b2007-06-17 19:56:38 -0500369 atomic_inc(&phba->num_cmd_success);
370
James Smarta257bf92009-04-06 18:48:10 -0400371 if (vport->cfg_lun_queue_depth <= queue_depth)
James Smart92d7f7b2007-06-17 19:56:38 -0500372 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500373 spin_lock_irqsave(&phba->hbalock, flags);
James Smart5ffc2662009-11-18 15:39:44 -0500374 if (time_before(jiffies,
375 phba->last_ramp_up_time + QUEUE_RAMP_UP_INTERVAL) ||
376 time_before(jiffies,
377 phba->last_rsrc_error_time + QUEUE_RAMP_UP_INTERVAL)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500378 spin_unlock_irqrestore(&phba->hbalock, flags);
379 return;
380 }
James Smart92d7f7b2007-06-17 19:56:38 -0500381 phba->last_ramp_up_time = jiffies;
382 spin_unlock_irqrestore(&phba->hbalock, flags);
383
384 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400385 evt_posted = phba->pport->work_port_events & WORKER_RAMP_UP_QUEUE;
386 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500387 phba->pport->work_port_events |= WORKER_RAMP_UP_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500388 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
389
James Smart5e9d9b82008-06-14 22:52:53 -0400390 if (!evt_posted)
391 lpfc_worker_wake_up(phba);
392 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500393}
394
James Smart9bad7672008-12-04 22:39:02 -0500395/**
James Smart3621a712009-04-06 18:47:14 -0400396 * lpfc_ramp_down_queue_handler - WORKER_RAMP_DOWN_QUEUE event handler
James Smart9bad7672008-12-04 22:39:02 -0500397 * @phba: The Hba for which this call is being executed.
398 *
399 * This routine is called to process WORKER_RAMP_DOWN_QUEUE event for worker
400 * thread.This routine reduces queue depth for all scsi device on each vport
401 * associated with @phba.
402 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500403void
404lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
405{
James Smart549e55c2007-08-02 11:09:51 -0400406 struct lpfc_vport **vports;
407 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500408 struct scsi_device *sdev;
James Smart5ffc2662009-11-18 15:39:44 -0500409 unsigned long new_queue_depth;
James Smart92d7f7b2007-06-17 19:56:38 -0500410 unsigned long num_rsrc_err, num_cmd_success;
James Smart549e55c2007-08-02 11:09:51 -0400411 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500412
413 num_rsrc_err = atomic_read(&phba->num_rsrc_err);
414 num_cmd_success = atomic_read(&phba->num_cmd_success);
415
James Smart75ad83a2012-05-09 21:18:40 -0400416 /*
417 * The error and success command counters are global per
418 * driver instance. If another handler has already
419 * operated on this error event, just exit.
420 */
421 if (num_rsrc_err == 0)
422 return;
423
James Smart549e55c2007-08-02 11:09:51 -0400424 vports = lpfc_create_vport_work_array(phba);
425 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400426 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400427 shost = lpfc_shost_from_vport(vports[i]);
428 shost_for_each_device(sdev, shost) {
James Smart92d7f7b2007-06-17 19:56:38 -0500429 new_queue_depth =
James Smart549e55c2007-08-02 11:09:51 -0400430 sdev->queue_depth * num_rsrc_err /
431 (num_rsrc_err + num_cmd_success);
432 if (!new_queue_depth)
433 new_queue_depth = sdev->queue_depth - 1;
434 else
435 new_queue_depth = sdev->queue_depth -
436 new_queue_depth;
James Smart5ffc2662009-11-18 15:39:44 -0500437 lpfc_change_queue_depth(sdev, new_queue_depth,
438 SCSI_QDEPTH_DEFAULT);
James Smart549e55c2007-08-02 11:09:51 -0400439 }
James Smart92d7f7b2007-06-17 19:56:38 -0500440 }
James Smart09372822008-01-11 01:52:54 -0500441 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500442 atomic_set(&phba->num_rsrc_err, 0);
443 atomic_set(&phba->num_cmd_success, 0);
444}
445
James Smart9bad7672008-12-04 22:39:02 -0500446/**
James Smart3621a712009-04-06 18:47:14 -0400447 * lpfc_ramp_up_queue_handler - WORKER_RAMP_UP_QUEUE event handler
James Smart9bad7672008-12-04 22:39:02 -0500448 * @phba: The Hba for which this call is being executed.
449 *
450 * This routine is called to process WORKER_RAMP_UP_QUEUE event for worker
451 * thread.This routine increases queue depth for all scsi device on each vport
452 * associated with @phba by 1. This routine also sets @phba num_rsrc_err and
453 * num_cmd_success to zero.
454 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500455void
456lpfc_ramp_up_queue_handler(struct lpfc_hba *phba)
457{
James Smart549e55c2007-08-02 11:09:51 -0400458 struct lpfc_vport **vports;
459 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500460 struct scsi_device *sdev;
James Smart549e55c2007-08-02 11:09:51 -0400461 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500462
James Smart549e55c2007-08-02 11:09:51 -0400463 vports = lpfc_create_vport_work_array(phba);
464 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400465 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400466 shost = lpfc_shost_from_vport(vports[i]);
467 shost_for_each_device(sdev, shost) {
James Smart97eab632008-04-07 10:16:05 -0400468 if (vports[i]->cfg_lun_queue_depth <=
469 sdev->queue_depth)
470 continue;
James Smart5ffc2662009-11-18 15:39:44 -0500471 lpfc_change_queue_depth(sdev,
472 sdev->queue_depth+1,
473 SCSI_QDEPTH_RAMP_UP);
James Smart549e55c2007-08-02 11:09:51 -0400474 }
James Smart92d7f7b2007-06-17 19:56:38 -0500475 }
James Smart09372822008-01-11 01:52:54 -0500476 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500477 atomic_set(&phba->num_rsrc_err, 0);
478 atomic_set(&phba->num_cmd_success, 0);
479}
480
James Smarta8e497d2008-08-24 21:50:11 -0400481/**
James Smart3621a712009-04-06 18:47:14 -0400482 * lpfc_scsi_dev_block - set all scsi hosts to block state
James Smarta8e497d2008-08-24 21:50:11 -0400483 * @phba: Pointer to HBA context object.
484 *
485 * This function walks vport list and set each SCSI host to block state
486 * by invoking fc_remote_port_delete() routine. This function is invoked
487 * with EEH when device's PCI slot has been permanently disabled.
488 **/
489void
490lpfc_scsi_dev_block(struct lpfc_hba *phba)
491{
492 struct lpfc_vport **vports;
493 struct Scsi_Host *shost;
494 struct scsi_device *sdev;
495 struct fc_rport *rport;
496 int i;
497
498 vports = lpfc_create_vport_work_array(phba);
499 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400500 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8e497d2008-08-24 21:50:11 -0400501 shost = lpfc_shost_from_vport(vports[i]);
502 shost_for_each_device(sdev, shost) {
503 rport = starget_to_rport(scsi_target(sdev));
504 fc_remote_port_delete(rport);
505 }
506 }
507 lpfc_destroy_vport_work_array(phba, vports);
508}
509
James Smart9bad7672008-12-04 22:39:02 -0500510/**
James Smart3772a992009-05-22 14:50:54 -0400511 * lpfc_new_scsi_buf_s3 - Scsi buffer allocator for HBA with SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -0500512 * @vport: The virtual port for which this call being executed.
James Smart3772a992009-05-22 14:50:54 -0400513 * @num_to_allocate: The requested number of buffers to allocate.
James Smart9bad7672008-12-04 22:39:02 -0500514 *
James Smart3772a992009-05-22 14:50:54 -0400515 * This routine allocates a scsi buffer for device with SLI-3 interface spec,
516 * the scsi buffer contains all the necessary information needed to initiate
517 * a SCSI I/O. The non-DMAable buffer region contains information to build
518 * the IOCB. The DMAable region contains memory for the FCP CMND, FCP RSP,
519 * and the initial BPL. In addition to allocating memory, the FCP CMND and
520 * FCP RSP BDEs are setup in the BPL and the BPL BDE is setup in the IOCB.
James Smart9bad7672008-12-04 22:39:02 -0500521 *
522 * Return codes:
James Smart3772a992009-05-22 14:50:54 -0400523 * int - number of scsi buffers that were allocated.
524 * 0 = failure, less than num_to_alloc is a partial failure.
James Smart9bad7672008-12-04 22:39:02 -0500525 **/
James Smart3772a992009-05-22 14:50:54 -0400526static int
527lpfc_new_scsi_buf_s3(struct lpfc_vport *vport, int num_to_alloc)
dea31012005-04-17 16:05:31 -0500528{
James Smart2e0fef82007-06-17 19:56:36 -0500529 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500530 struct lpfc_scsi_buf *psb;
531 struct ulp_bde64 *bpl;
532 IOCB_t *iocb;
James Smart34b02dc2008-08-24 21:49:55 -0400533 dma_addr_t pdma_phys_fcp_cmd;
534 dma_addr_t pdma_phys_fcp_rsp;
535 dma_addr_t pdma_phys_bpl;
James Bottomley604a3e32005-10-29 10:28:33 -0500536 uint16_t iotag;
James Smart3772a992009-05-22 14:50:54 -0400537 int bcnt;
dea31012005-04-17 16:05:31 -0500538
James Smart3772a992009-05-22 14:50:54 -0400539 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
540 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
541 if (!psb)
542 break;
dea31012005-04-17 16:05:31 -0500543
James Smart3772a992009-05-22 14:50:54 -0400544 /*
545 * Get memory from the pci pool to map the virt space to pci
546 * bus space for an I/O. The DMA buffer includes space for the
547 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
548 * necessary to support the sg_tablesize.
549 */
550 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
551 GFP_KERNEL, &psb->dma_handle);
552 if (!psb->data) {
553 kfree(psb);
554 break;
555 }
dea31012005-04-17 16:05:31 -0500556
James Smart3772a992009-05-22 14:50:54 -0400557 /* Initialize virtual ptrs to dma_buf region. */
558 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
dea31012005-04-17 16:05:31 -0500559
James Smart3772a992009-05-22 14:50:54 -0400560 /* Allocate iotag for psb->cur_iocbq. */
561 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
562 if (iotag == 0) {
563 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
564 psb->data, psb->dma_handle);
565 kfree(psb);
566 break;
567 }
568 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Bottomley604a3e32005-10-29 10:28:33 -0500569
James Smart3772a992009-05-22 14:50:54 -0400570 psb->fcp_cmnd = psb->data;
571 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
572 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
James Smart34b02dc2008-08-24 21:49:55 -0400573 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500574
James Smart3772a992009-05-22 14:50:54 -0400575 /* Initialize local short-hand pointers. */
576 bpl = psb->fcp_bpl;
577 pdma_phys_fcp_cmd = psb->dma_handle;
578 pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
579 pdma_phys_bpl = psb->dma_handle + sizeof(struct fcp_cmnd) +
580 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500581
James Smart3772a992009-05-22 14:50:54 -0400582 /*
583 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
584 * are sg list bdes. Initialize the first two and leave the
585 * rest for queuecommand.
586 */
587 bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
588 bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
589 bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
590 bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
591 bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
dea31012005-04-17 16:05:31 -0500592
James Smart3772a992009-05-22 14:50:54 -0400593 /* Setup the physical region for the FCP RSP */
594 bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
595 bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
596 bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
597 bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
598 bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
599
600 /*
601 * Since the IOCB for the FCP I/O is built into this
602 * lpfc_scsi_buf, initialize it with all known data now.
603 */
604 iocb = &psb->cur_iocbq.iocb;
605 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
606 if ((phba->sli_rev == 3) &&
607 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
608 /* fill in immediate fcp command BDE */
609 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
610 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
611 iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
612 unsli3.fcp_ext.icd);
613 iocb->un.fcpi64.bdl.addrHigh = 0;
614 iocb->ulpBdeCount = 0;
615 iocb->ulpLe = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300616 /* fill in response BDE */
James Smart3772a992009-05-22 14:50:54 -0400617 iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags =
618 BUFF_TYPE_BDE_64;
619 iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
620 sizeof(struct fcp_rsp);
621 iocb->unsli3.fcp_ext.rbde.addrLow =
622 putPaddrLow(pdma_phys_fcp_rsp);
623 iocb->unsli3.fcp_ext.rbde.addrHigh =
624 putPaddrHigh(pdma_phys_fcp_rsp);
625 } else {
626 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
627 iocb->un.fcpi64.bdl.bdeSize =
628 (2 * sizeof(struct ulp_bde64));
629 iocb->un.fcpi64.bdl.addrLow =
630 putPaddrLow(pdma_phys_bpl);
631 iocb->un.fcpi64.bdl.addrHigh =
632 putPaddrHigh(pdma_phys_bpl);
633 iocb->ulpBdeCount = 1;
634 iocb->ulpLe = 1;
635 }
636 iocb->ulpClass = CLASS3;
637 psb->status = IOSTAT_SUCCESS;
James Smartda0436e2009-05-22 14:51:39 -0400638 /* Put it back into the SCSI buffer list */
James Smarteee88772010-09-29 11:19:08 -0400639 psb->cur_iocbq.context1 = psb;
James Smart1c6f4ef52009-11-18 15:40:49 -0500640 lpfc_release_scsi_buf_s3(phba, psb);
James Smart3772a992009-05-22 14:50:54 -0400641
James Smart34b02dc2008-08-24 21:49:55 -0400642 }
dea31012005-04-17 16:05:31 -0500643
James Smart3772a992009-05-22 14:50:54 -0400644 return bcnt;
dea31012005-04-17 16:05:31 -0500645}
646
James Smart9bad7672008-12-04 22:39:02 -0500647/**
James Smart1151e3e2011-02-16 12:39:35 -0500648 * lpfc_sli4_vport_delete_fcp_xri_aborted -Remove all ndlp references for vport
649 * @vport: pointer to lpfc vport data structure.
650 *
651 * This routine is invoked by the vport cleanup for deletions and the cleanup
652 * for an ndlp on removal.
653 **/
654void
655lpfc_sli4_vport_delete_fcp_xri_aborted(struct lpfc_vport *vport)
656{
657 struct lpfc_hba *phba = vport->phba;
658 struct lpfc_scsi_buf *psb, *next_psb;
659 unsigned long iflag = 0;
660
661 spin_lock_irqsave(&phba->hbalock, iflag);
662 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
663 list_for_each_entry_safe(psb, next_psb,
664 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
665 if (psb->rdata && psb->rdata->pnode
666 && psb->rdata->pnode->vport == vport)
667 psb->rdata = NULL;
668 }
669 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
670 spin_unlock_irqrestore(&phba->hbalock, iflag);
671}
672
673/**
James Smartda0436e2009-05-22 14:51:39 -0400674 * lpfc_sli4_fcp_xri_aborted - Fast-path process of fcp xri abort
675 * @phba: pointer to lpfc hba data structure.
676 * @axri: pointer to the fcp xri abort wcqe structure.
677 *
678 * This routine is invoked by the worker thread to process a SLI4 fast-path
679 * FCP aborted xri.
680 **/
681void
682lpfc_sli4_fcp_xri_aborted(struct lpfc_hba *phba,
683 struct sli4_wcqe_xri_aborted *axri)
684{
685 uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
James Smart19ca7602010-11-20 23:11:55 -0500686 uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
James Smartda0436e2009-05-22 14:51:39 -0400687 struct lpfc_scsi_buf *psb, *next_psb;
688 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -0500689 struct lpfc_iocbq *iocbq;
690 int i;
James Smart19ca7602010-11-20 23:11:55 -0500691 struct lpfc_nodelist *ndlp;
692 int rrq_empty = 0;
James Smart589a52d2010-07-14 15:30:54 -0400693 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smartda0436e2009-05-22 14:51:39 -0400694
James Smart0f65ff62010-02-26 14:14:23 -0500695 spin_lock_irqsave(&phba->hbalock, iflag);
696 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
James Smartda0436e2009-05-22 14:51:39 -0400697 list_for_each_entry_safe(psb, next_psb,
698 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
699 if (psb->cur_iocbq.sli4_xritag == xri) {
700 list_del(&psb->list);
James Smart341af102010-01-26 23:07:37 -0500701 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400702 psb->status = IOSTAT_SUCCESS;
James Smart0f65ff62010-02-26 14:14:23 -0500703 spin_unlock(
704 &phba->sli4_hba.abts_scsi_buf_list_lock);
James Smart1151e3e2011-02-16 12:39:35 -0500705 if (psb->rdata && psb->rdata->pnode)
706 ndlp = psb->rdata->pnode;
707 else
708 ndlp = NULL;
709
James Smart19ca7602010-11-20 23:11:55 -0500710 rrq_empty = list_empty(&phba->active_rrq_list);
James Smart0f65ff62010-02-26 14:14:23 -0500711 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartcb69f7d2011-12-13 13:21:57 -0500712 if (ndlp) {
James Smartee0f4fe2012-05-09 21:19:14 -0400713 lpfc_set_rrq_active(phba, ndlp,
714 psb->cur_iocbq.sli4_lxritag, rxid, 1);
James Smartcb69f7d2011-12-13 13:21:57 -0500715 lpfc_sli4_abts_err_handler(phba, ndlp, axri);
716 }
James Smartda0436e2009-05-22 14:51:39 -0400717 lpfc_release_scsi_buf_s4(phba, psb);
James Smart19ca7602010-11-20 23:11:55 -0500718 if (rrq_empty)
719 lpfc_worker_wake_up(phba);
James Smartda0436e2009-05-22 14:51:39 -0400720 return;
721 }
722 }
James Smart0f65ff62010-02-26 14:14:23 -0500723 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
724 for (i = 1; i <= phba->sli.last_iotag; i++) {
725 iocbq = phba->sli.iocbq_lookup[i];
726
727 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
728 (iocbq->iocb_flag & LPFC_IO_LIBDFC))
729 continue;
730 if (iocbq->sli4_xritag != xri)
731 continue;
732 psb = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
733 psb->exch_busy = 0;
734 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart0e9bb8d2013-03-01 16:35:12 -0500735 if (!list_empty(&pring->txq))
James Smart589a52d2010-07-14 15:30:54 -0400736 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -0500737 return;
738
739 }
740 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartda0436e2009-05-22 14:51:39 -0400741}
742
743/**
James Smart8a9d2e82012-05-09 21:16:12 -0400744 * lpfc_sli4_post_scsi_sgl_list - Psot blocks of scsi buffer sgls from a list
745 * @phba: pointer to lpfc hba data structure.
746 * @post_sblist: pointer to the scsi buffer list.
747 *
748 * This routine walks a list of scsi buffers that was passed in. It attempts
749 * to construct blocks of scsi buffer sgls which contains contiguous xris and
750 * uses the non-embedded SGL block post mailbox commands to post to the port.
751 * For single SCSI buffer sgl with non-contiguous xri, if any, it shall use
752 * embedded SGL post mailbox command for posting. The @post_sblist passed in
753 * must be local list, thus no lock is needed when manipulate the list.
754 *
755 * Returns: 0 = failure, non-zero number of successfully posted buffers.
756 **/
757int
758lpfc_sli4_post_scsi_sgl_list(struct lpfc_hba *phba,
759 struct list_head *post_sblist, int sb_count)
760{
761 struct lpfc_scsi_buf *psb, *psb_next;
762 int status;
763 int post_cnt = 0, block_cnt = 0, num_posting = 0, num_posted = 0;
764 dma_addr_t pdma_phys_bpl1;
765 int last_xritag = NO_XRI;
766 LIST_HEAD(prep_sblist);
767 LIST_HEAD(blck_sblist);
768 LIST_HEAD(scsi_sblist);
769
770 /* sanity check */
771 if (sb_count <= 0)
772 return -EINVAL;
773
774 list_for_each_entry_safe(psb, psb_next, post_sblist, list) {
775 list_del_init(&psb->list);
776 block_cnt++;
777 if ((last_xritag != NO_XRI) &&
778 (psb->cur_iocbq.sli4_xritag != last_xritag + 1)) {
779 /* a hole in xri block, form a sgl posting block */
780 list_splice_init(&prep_sblist, &blck_sblist);
781 post_cnt = block_cnt - 1;
782 /* prepare list for next posting block */
783 list_add_tail(&psb->list, &prep_sblist);
784 block_cnt = 1;
785 } else {
786 /* prepare list for next posting block */
787 list_add_tail(&psb->list, &prep_sblist);
788 /* enough sgls for non-embed sgl mbox command */
789 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
790 list_splice_init(&prep_sblist, &blck_sblist);
791 post_cnt = block_cnt;
792 block_cnt = 0;
793 }
794 }
795 num_posting++;
796 last_xritag = psb->cur_iocbq.sli4_xritag;
797
798 /* end of repost sgl list condition for SCSI buffers */
799 if (num_posting == sb_count) {
800 if (post_cnt == 0) {
801 /* last sgl posting block */
802 list_splice_init(&prep_sblist, &blck_sblist);
803 post_cnt = block_cnt;
804 } else if (block_cnt == 1) {
805 /* last single sgl with non-contiguous xri */
806 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
807 pdma_phys_bpl1 = psb->dma_phys_bpl +
808 SGL_PAGE_SIZE;
809 else
810 pdma_phys_bpl1 = 0;
811 status = lpfc_sli4_post_sgl(phba,
812 psb->dma_phys_bpl,
813 pdma_phys_bpl1,
814 psb->cur_iocbq.sli4_xritag);
815 if (status) {
816 /* failure, put on abort scsi list */
817 psb->exch_busy = 1;
818 } else {
819 /* success, put on SCSI buffer list */
820 psb->exch_busy = 0;
821 psb->status = IOSTAT_SUCCESS;
822 num_posted++;
823 }
824 /* success, put on SCSI buffer sgl list */
825 list_add_tail(&psb->list, &scsi_sblist);
826 }
827 }
828
829 /* continue until a nembed page worth of sgls */
830 if (post_cnt == 0)
831 continue;
832
833 /* post block of SCSI buffer list sgls */
834 status = lpfc_sli4_post_scsi_sgl_block(phba, &blck_sblist,
835 post_cnt);
836
837 /* don't reset xirtag due to hole in xri block */
838 if (block_cnt == 0)
839 last_xritag = NO_XRI;
840
841 /* reset SCSI buffer post count for next round of posting */
842 post_cnt = 0;
843
844 /* put posted SCSI buffer-sgl posted on SCSI buffer sgl list */
845 while (!list_empty(&blck_sblist)) {
846 list_remove_head(&blck_sblist, psb,
847 struct lpfc_scsi_buf, list);
848 if (status) {
849 /* failure, put on abort scsi list */
850 psb->exch_busy = 1;
851 } else {
852 /* success, put on SCSI buffer list */
853 psb->exch_busy = 0;
854 psb->status = IOSTAT_SUCCESS;
855 num_posted++;
856 }
857 list_add_tail(&psb->list, &scsi_sblist);
858 }
859 }
860 /* Push SCSI buffers with sgl posted to the availble list */
861 while (!list_empty(&scsi_sblist)) {
862 list_remove_head(&scsi_sblist, psb,
863 struct lpfc_scsi_buf, list);
864 lpfc_release_scsi_buf_s4(phba, psb);
865 }
866 return num_posted;
867}
868
869/**
870 * lpfc_sli4_repost_scsi_sgl_list - Repsot all the allocated scsi buffer sgls
James Smartda0436e2009-05-22 14:51:39 -0400871 * @phba: pointer to lpfc hba data structure.
872 *
873 * This routine walks the list of scsi buffers that have been allocated and
James Smart8a9d2e82012-05-09 21:16:12 -0400874 * repost them to the port by using SGL block post. This is needed after a
James Smartda0436e2009-05-22 14:51:39 -0400875 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
876 * is responsible for moving all scsi buffers on the lpfc_abts_scsi_sgl_list
877 * to the lpfc_scsi_buf_list. If the repost fails, reject all scsi buffers.
878 *
879 * Returns: 0 = success, non-zero failure.
880 **/
881int
882lpfc_sli4_repost_scsi_sgl_list(struct lpfc_hba *phba)
883{
James Smart8a9d2e82012-05-09 21:16:12 -0400884 LIST_HEAD(post_sblist);
885 int num_posted, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -0400886
James Smart8a9d2e82012-05-09 21:16:12 -0400887 /* get all SCSI buffers need to repost to a local list */
888 spin_lock(&phba->scsi_buf_list_lock);
889 list_splice_init(&phba->lpfc_scsi_buf_list, &post_sblist);
890 spin_unlock(&phba->scsi_buf_list_lock);
James Smartda0436e2009-05-22 14:51:39 -0400891
James Smart8a9d2e82012-05-09 21:16:12 -0400892 /* post the list of scsi buffer sgls to port if available */
893 if (!list_empty(&post_sblist)) {
894 num_posted = lpfc_sli4_post_scsi_sgl_list(phba, &post_sblist,
895 phba->sli4_hba.scsi_xri_cnt);
896 /* failed to post any scsi buffer, return error */
897 if (num_posted == 0)
898 rc = -EIO;
James Smartda0436e2009-05-22 14:51:39 -0400899 }
900 return rc;
901}
902
903/**
904 * lpfc_new_scsi_buf_s4 - Scsi buffer allocator for HBA with SLI4 IF spec
905 * @vport: The virtual port for which this call being executed.
906 * @num_to_allocate: The requested number of buffers to allocate.
907 *
James Smart8a9d2e82012-05-09 21:16:12 -0400908 * This routine allocates scsi buffers for device with SLI-4 interface spec,
James Smartda0436e2009-05-22 14:51:39 -0400909 * the scsi buffer contains all the necessary information needed to initiate
James Smart8a9d2e82012-05-09 21:16:12 -0400910 * a SCSI I/O. After allocating up to @num_to_allocate SCSI buffers and put
911 * them on a list, it post them to the port by using SGL block post.
James Smartda0436e2009-05-22 14:51:39 -0400912 *
913 * Return codes:
James Smart8a9d2e82012-05-09 21:16:12 -0400914 * int - number of scsi buffers that were allocated and posted.
James Smartda0436e2009-05-22 14:51:39 -0400915 * 0 = failure, less than num_to_alloc is a partial failure.
916 **/
917static int
918lpfc_new_scsi_buf_s4(struct lpfc_vport *vport, int num_to_alloc)
919{
920 struct lpfc_hba *phba = vport->phba;
921 struct lpfc_scsi_buf *psb;
922 struct sli4_sge *sgl;
923 IOCB_t *iocb;
924 dma_addr_t pdma_phys_fcp_cmd;
925 dma_addr_t pdma_phys_fcp_rsp;
926 dma_addr_t pdma_phys_bpl, pdma_phys_bpl1;
James Smart8a9d2e82012-05-09 21:16:12 -0400927 uint16_t iotag, lxri = 0;
928 int bcnt, num_posted;
929 LIST_HEAD(prep_sblist);
930 LIST_HEAD(post_sblist);
931 LIST_HEAD(scsi_sblist);
James Smartda0436e2009-05-22 14:51:39 -0400932
933 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
934 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
935 if (!psb)
936 break;
James Smartda0436e2009-05-22 14:51:39 -0400937 /*
James Smart8a9d2e82012-05-09 21:16:12 -0400938 * Get memory from the pci pool to map the virt space to
939 * pci bus space for an I/O. The DMA buffer includes space
940 * for the struct fcp_cmnd, struct fcp_rsp and the number
941 * of bde's necessary to support the sg_tablesize.
James Smartda0436e2009-05-22 14:51:39 -0400942 */
943 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
944 GFP_KERNEL, &psb->dma_handle);
945 if (!psb->data) {
946 kfree(psb);
947 break;
948 }
James Smartda0436e2009-05-22 14:51:39 -0400949 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
950
951 /* Allocate iotag for psb->cur_iocbq. */
952 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
953 if (iotag == 0) {
James Smartb92938b2010-06-07 15:24:12 -0400954 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
955 psb->data, psb->dma_handle);
James Smartda0436e2009-05-22 14:51:39 -0400956 kfree(psb);
957 break;
958 }
959
James Smart6d368e52011-05-24 11:44:12 -0400960 lxri = lpfc_sli4_next_xritag(phba);
961 if (lxri == NO_XRI) {
James Smartda0436e2009-05-22 14:51:39 -0400962 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
963 psb->data, psb->dma_handle);
964 kfree(psb);
965 break;
966 }
James Smart6d368e52011-05-24 11:44:12 -0400967 psb->cur_iocbq.sli4_lxritag = lxri;
968 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
James Smartda0436e2009-05-22 14:51:39 -0400969 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Smartda0436e2009-05-22 14:51:39 -0400970 psb->fcp_bpl = psb->data;
971 psb->fcp_cmnd = (psb->data + phba->cfg_sg_dma_buf_size)
972 - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
973 psb->fcp_rsp = (struct fcp_rsp *)((uint8_t *)psb->fcp_cmnd +
974 sizeof(struct fcp_cmnd));
975
976 /* Initialize local short-hand pointers. */
977 sgl = (struct sli4_sge *)psb->fcp_bpl;
978 pdma_phys_bpl = psb->dma_handle;
979 pdma_phys_fcp_cmd =
980 (psb->dma_handle + phba->cfg_sg_dma_buf_size)
981 - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
982 pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
983
984 /*
James Smart8a9d2e82012-05-09 21:16:12 -0400985 * The first two bdes are the FCP_CMD and FCP_RSP.
986 * The balance are sg list bdes. Initialize the
987 * first two and leave the rest for queuecommand.
James Smartda0436e2009-05-22 14:51:39 -0400988 */
989 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
990 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
James Smart05580562011-05-24 11:40:48 -0400991 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -0400992 bf_set(lpfc_sli4_sge_last, sgl, 0);
993 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -0500994 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_cmnd));
James Smartda0436e2009-05-22 14:51:39 -0400995 sgl++;
996
997 /* Setup the physical region for the FCP RSP */
998 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
999 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
James Smart05580562011-05-24 11:40:48 -04001000 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -04001001 bf_set(lpfc_sli4_sge_last, sgl, 1);
1002 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05001003 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
James Smartda0436e2009-05-22 14:51:39 -04001004
1005 /*
1006 * Since the IOCB for the FCP I/O is built into this
1007 * lpfc_scsi_buf, initialize it with all known data now.
1008 */
1009 iocb = &psb->cur_iocbq.iocb;
1010 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
1011 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
1012 /* setting the BLP size to 2 * sizeof BDE may not be correct.
1013 * We are setting the bpl to point to out sgl. An sgl's
1014 * entries are 16 bytes, a bpl entries are 12 bytes.
1015 */
1016 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
1017 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
1018 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
1019 iocb->ulpBdeCount = 1;
1020 iocb->ulpLe = 1;
1021 iocb->ulpClass = CLASS3;
James Smart8a9d2e82012-05-09 21:16:12 -04001022 psb->cur_iocbq.context1 = psb;
James Smartda0436e2009-05-22 14:51:39 -04001023 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
1024 pdma_phys_bpl1 = pdma_phys_bpl + SGL_PAGE_SIZE;
1025 else
1026 pdma_phys_bpl1 = 0;
1027 psb->dma_phys_bpl = pdma_phys_bpl;
James Smart6d368e52011-05-24 11:44:12 -04001028
James Smart8a9d2e82012-05-09 21:16:12 -04001029 /* add the scsi buffer to a post list */
1030 list_add_tail(&psb->list, &post_sblist);
1031 spin_lock_irq(&phba->scsi_buf_list_lock);
1032 phba->sli4_hba.scsi_xri_cnt++;
1033 spin_unlock_irq(&phba->scsi_buf_list_lock);
James Smartda0436e2009-05-22 14:51:39 -04001034 }
James Smart8a9d2e82012-05-09 21:16:12 -04001035 lpfc_printf_log(phba, KERN_INFO, LOG_BG,
1036 "3021 Allocate %d out of %d requested new SCSI "
1037 "buffers\n", bcnt, num_to_alloc);
James Smartda0436e2009-05-22 14:51:39 -04001038
James Smart8a9d2e82012-05-09 21:16:12 -04001039 /* post the list of scsi buffer sgls to port if available */
1040 if (!list_empty(&post_sblist))
1041 num_posted = lpfc_sli4_post_scsi_sgl_list(phba,
1042 &post_sblist, bcnt);
1043 else
1044 num_posted = 0;
1045
1046 return num_posted;
James Smartda0436e2009-05-22 14:51:39 -04001047}
1048
1049/**
James Smart3772a992009-05-22 14:50:54 -04001050 * lpfc_new_scsi_buf - Wrapper funciton for scsi buffer allocator
1051 * @vport: The virtual port for which this call being executed.
1052 * @num_to_allocate: The requested number of buffers to allocate.
1053 *
1054 * This routine wraps the actual SCSI buffer allocator function pointer from
1055 * the lpfc_hba struct.
1056 *
1057 * Return codes:
1058 * int - number of scsi buffers that were allocated.
1059 * 0 = failure, less than num_to_alloc is a partial failure.
1060 **/
1061static inline int
1062lpfc_new_scsi_buf(struct lpfc_vport *vport, int num_to_alloc)
1063{
1064 return vport->phba->lpfc_new_scsi_buf(vport, num_to_alloc);
1065}
1066
1067/**
James Smart19ca7602010-11-20 23:11:55 -05001068 * lpfc_get_scsi_buf_s3 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
James Smart3772a992009-05-22 14:50:54 -04001069 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -05001070 *
1071 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1072 * and returns to caller.
1073 *
1074 * Return codes:
1075 * NULL - Error
1076 * Pointer to lpfc_scsi_buf - Success
1077 **/
Adrian Bunk455c53e2006-01-06 20:21:28 +01001078static struct lpfc_scsi_buf*
James Smart19ca7602010-11-20 23:11:55 -05001079lpfc_get_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001080{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001081 struct lpfc_scsi_buf * lpfc_cmd = NULL;
1082 struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001083 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -05001084
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001085 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001086 list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
James Smart1dcb58e2007-04-25 09:51:30 -04001087 if (lpfc_cmd) {
1088 lpfc_cmd->seg_cnt = 0;
1089 lpfc_cmd->nonsg_phys = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05001090 lpfc_cmd->prot_seg_cnt = 0;
James Smart1dcb58e2007-04-25 09:51:30 -04001091 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001092 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001093 return lpfc_cmd;
1094}
James Smart19ca7602010-11-20 23:11:55 -05001095/**
1096 * lpfc_get_scsi_buf_s4 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1097 * @phba: The HBA for which this call is being executed.
1098 *
1099 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1100 * and returns to caller.
1101 *
1102 * Return codes:
1103 * NULL - Error
1104 * Pointer to lpfc_scsi_buf - Success
1105 **/
1106static struct lpfc_scsi_buf*
1107lpfc_get_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1108{
James Smart1151e3e2011-02-16 12:39:35 -05001109 struct lpfc_scsi_buf *lpfc_cmd ;
James Smart19ca7602010-11-20 23:11:55 -05001110 unsigned long iflag = 0;
1111 int found = 0;
1112
1113 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James Smart1151e3e2011-02-16 12:39:35 -05001114 list_for_each_entry(lpfc_cmd, &phba->lpfc_scsi_buf_list,
1115 list) {
James Smart19ca7602010-11-20 23:11:55 -05001116 if (lpfc_test_rrq_active(phba, ndlp,
James Smartee0f4fe2012-05-09 21:19:14 -04001117 lpfc_cmd->cur_iocbq.sli4_lxritag))
James Smart1151e3e2011-02-16 12:39:35 -05001118 continue;
1119 list_del(&lpfc_cmd->list);
James Smart19ca7602010-11-20 23:11:55 -05001120 found = 1;
1121 lpfc_cmd->seg_cnt = 0;
1122 lpfc_cmd->nonsg_phys = 0;
1123 lpfc_cmd->prot_seg_cnt = 0;
James Smart1151e3e2011-02-16 12:39:35 -05001124 break;
James Smart19ca7602010-11-20 23:11:55 -05001125 }
James Smart1151e3e2011-02-16 12:39:35 -05001126 spin_unlock_irqrestore(&phba->scsi_buf_list_lock,
1127 iflag);
1128 if (!found)
1129 return NULL;
1130 else
1131 return lpfc_cmd;
James Smart19ca7602010-11-20 23:11:55 -05001132}
1133/**
1134 * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1135 * @phba: The HBA for which this call is being executed.
1136 *
1137 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1138 * and returns to caller.
1139 *
1140 * Return codes:
1141 * NULL - Error
1142 * Pointer to lpfc_scsi_buf - Success
1143 **/
1144static struct lpfc_scsi_buf*
1145lpfc_get_scsi_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1146{
1147 return phba->lpfc_get_scsi_buf(phba, ndlp);
1148}
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001149
James Smart9bad7672008-12-04 22:39:02 -05001150/**
James Smart3772a992009-05-22 14:50:54 -04001151 * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
James Smart9bad7672008-12-04 22:39:02 -05001152 * @phba: The Hba for which this call is being executed.
1153 * @psb: The scsi buffer which is being released.
1154 *
1155 * This routine releases @psb scsi buffer by adding it to tail of @phba
1156 * lpfc_scsi_buf_list list.
1157 **/
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001158static void
James Smart3772a992009-05-22 14:50:54 -04001159lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001160{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001161 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -05001162
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001163 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001164 psb->pCmd = NULL;
dea31012005-04-17 16:05:31 -05001165 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001166 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
dea31012005-04-17 16:05:31 -05001167}
1168
James Smart9bad7672008-12-04 22:39:02 -05001169/**
James Smartda0436e2009-05-22 14:51:39 -04001170 * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
1171 * @phba: The Hba for which this call is being executed.
1172 * @psb: The scsi buffer which is being released.
1173 *
1174 * This routine releases @psb scsi buffer by adding it to tail of @phba
1175 * lpfc_scsi_buf_list list. For SLI4 XRI's are tied to the scsi buffer
1176 * and cannot be reused for at least RA_TOV amount of time if it was
1177 * aborted.
1178 **/
1179static void
1180lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1181{
1182 unsigned long iflag = 0;
1183
James Smart341af102010-01-26 23:07:37 -05001184 if (psb->exch_busy) {
James Smartda0436e2009-05-22 14:51:39 -04001185 spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock,
1186 iflag);
1187 psb->pCmd = NULL;
1188 list_add_tail(&psb->list,
1189 &phba->sli4_hba.lpfc_abts_scsi_buf_list);
1190 spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
1191 iflag);
1192 } else {
1193
1194 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
1195 psb->pCmd = NULL;
1196 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
1197 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
1198 }
1199}
1200
1201/**
James Smart3772a992009-05-22 14:50:54 -04001202 * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
1203 * @phba: The Hba for which this call is being executed.
1204 * @psb: The scsi buffer which is being released.
1205 *
1206 * This routine releases @psb scsi buffer by adding it to tail of @phba
1207 * lpfc_scsi_buf_list list.
1208 **/
1209static void
1210lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1211{
1212
1213 phba->lpfc_release_scsi_buf(phba, psb);
1214}
1215
1216/**
1217 * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -05001218 * @phba: The Hba for which this call is being executed.
1219 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1220 *
1221 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
James Smart3772a992009-05-22 14:50:54 -04001222 * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
1223 * through sg elements and format the bdea. This routine also initializes all
1224 * IOCB fields which are dependent on scsi command request buffer.
James Smart9bad7672008-12-04 22:39:02 -05001225 *
1226 * Return codes:
1227 * 1 - Error
1228 * 0 - Success
1229 **/
dea31012005-04-17 16:05:31 -05001230static int
James Smart3772a992009-05-22 14:50:54 -04001231lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
dea31012005-04-17 16:05:31 -05001232{
1233 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1234 struct scatterlist *sgel = NULL;
1235 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1236 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
James Smart0f65ff62010-02-26 14:14:23 -05001237 struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
dea31012005-04-17 16:05:31 -05001238 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart34b02dc2008-08-24 21:49:55 -04001239 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
dea31012005-04-17 16:05:31 -05001240 dma_addr_t physaddr;
James Smart34b02dc2008-08-24 21:49:55 -04001241 uint32_t num_bde = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001242 int nseg, datadir = scsi_cmnd->sc_data_direction;
dea31012005-04-17 16:05:31 -05001243
1244 /*
1245 * There are three possibilities here - use scatter-gather segment, use
1246 * the single mapping, or neither. Start the lpfc command prep by
1247 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1248 * data bde entry.
1249 */
1250 bpl += 2;
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001251 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05001252 /*
1253 * The driver stores the segment count returned from pci_map_sg
1254 * because this a count of dma-mappings used to map the use_sg
1255 * pages. They are not guaranteed to be the same for those
1256 * architectures that implement an IOMMU.
1257 */
dea31012005-04-17 16:05:31 -05001258
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001259 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
1260 scsi_sg_count(scsi_cmnd), datadir);
1261 if (unlikely(!nseg))
1262 return 1;
1263
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001264 lpfc_cmd->seg_cnt = nseg;
dea31012005-04-17 16:05:31 -05001265 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001266 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1267 "9064 BLKGRD: %s: Too many sg segments from "
James Smarte2a0a9d2008-12-04 22:40:02 -05001268 "dma_map_sg. Config %d, seg_cnt %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001269 __func__, phba->cfg_sg_seg_cnt,
dea31012005-04-17 16:05:31 -05001270 lpfc_cmd->seg_cnt);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001271 scsi_dma_unmap(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001272 return 1;
1273 }
1274
1275 /*
1276 * The driver established a maximum scatter-gather segment count
1277 * during probe that limits the number of sg elements in any
1278 * single scsi command. Just run through the seg_cnt and format
1279 * the bde's.
James Smart34b02dc2008-08-24 21:49:55 -04001280 * When using SLI-3 the driver will try to fit all the BDEs into
1281 * the IOCB. If it can't then the BDEs get added to a BPL as it
1282 * does for SLI-2 mode.
dea31012005-04-17 16:05:31 -05001283 */
James Smart34b02dc2008-08-24 21:49:55 -04001284 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
dea31012005-04-17 16:05:31 -05001285 physaddr = sg_dma_address(sgel);
James Smart34b02dc2008-08-24 21:49:55 -04001286 if (phba->sli_rev == 3 &&
James Smarte2a0a9d2008-12-04 22:40:02 -05001287 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
James Smart0f65ff62010-02-26 14:14:23 -05001288 !(iocbq->iocb_flag & DSS_SECURITY_OP) &&
James Smart34b02dc2008-08-24 21:49:55 -04001289 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
1290 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1291 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
1292 data_bde->addrLow = putPaddrLow(physaddr);
1293 data_bde->addrHigh = putPaddrHigh(physaddr);
1294 data_bde++;
1295 } else {
1296 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1297 bpl->tus.f.bdeSize = sg_dma_len(sgel);
1298 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1299 bpl->addrLow =
1300 le32_to_cpu(putPaddrLow(physaddr));
1301 bpl->addrHigh =
1302 le32_to_cpu(putPaddrHigh(physaddr));
1303 bpl++;
1304 }
dea31012005-04-17 16:05:31 -05001305 }
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001306 }
dea31012005-04-17 16:05:31 -05001307
1308 /*
1309 * Finish initializing those IOCB fields that are dependent on the
James Smart34b02dc2008-08-24 21:49:55 -04001310 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
1311 * explicitly reinitialized and for SLI-3 the extended bde count is
1312 * explicitly reinitialized since all iocb memory resources are reused.
dea31012005-04-17 16:05:31 -05001313 */
James Smarte2a0a9d2008-12-04 22:40:02 -05001314 if (phba->sli_rev == 3 &&
James Smart0f65ff62010-02-26 14:14:23 -05001315 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
1316 !(iocbq->iocb_flag & DSS_SECURITY_OP)) {
James Smart34b02dc2008-08-24 21:49:55 -04001317 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
1318 /*
1319 * The extended IOCB format can only fit 3 BDE or a BPL.
1320 * This I/O has more than 3 BDE so the 1st data bde will
1321 * be a BPL that is filled in here.
1322 */
1323 physaddr = lpfc_cmd->dma_handle;
1324 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
1325 data_bde->tus.f.bdeSize = (num_bde *
1326 sizeof(struct ulp_bde64));
1327 physaddr += (sizeof(struct fcp_cmnd) +
1328 sizeof(struct fcp_rsp) +
1329 (2 * sizeof(struct ulp_bde64)));
1330 data_bde->addrHigh = putPaddrHigh(physaddr);
1331 data_bde->addrLow = putPaddrLow(physaddr);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001332 /* ebde count includes the response bde and data bpl */
James Smart34b02dc2008-08-24 21:49:55 -04001333 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
1334 } else {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001335 /* ebde count includes the response bde and data bdes */
James Smart34b02dc2008-08-24 21:49:55 -04001336 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1337 }
1338 } else {
1339 iocb_cmd->un.fcpi64.bdl.bdeSize =
1340 ((num_bde + 2) * sizeof(struct ulp_bde64));
James Smart0f65ff62010-02-26 14:14:23 -05001341 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
James Smart34b02dc2008-08-24 21:49:55 -04001342 }
James Smart09372822008-01-11 01:52:54 -05001343 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
James Smarte2a0a9d2008-12-04 22:40:02 -05001344
1345 /*
1346 * Due to difference in data length between DIF/non-DIF paths,
1347 * we need to set word 4 of IOCB here
1348 */
James Smarta257bf92009-04-06 18:48:10 -04001349 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001350 return 0;
1351}
1352
James Smartf9bb2da2011-10-10 21:34:11 -04001353static inline unsigned
1354lpfc_cmd_blksize(struct scsi_cmnd *sc)
1355{
1356 return sc->device->sector_size;
1357}
1358
1359#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001360
James Smart9a6b09c2012-03-01 22:37:42 -05001361/* Return if if error injection is detected by Initiator */
1362#define BG_ERR_INIT 0x1
1363/* Return if if error injection is detected by Target */
1364#define BG_ERR_TGT 0x2
1365/* Return if if swapping CSUM<-->CRC is required for error injection */
1366#define BG_ERR_SWAP 0x10
1367/* Return if disabling Guard/Ref/App checking is required for error injection */
1368#define BG_ERR_CHECK 0x20
James Smartacd68592012-01-18 16:25:09 -05001369
1370/**
1371 * lpfc_bg_err_inject - Determine if we should inject an error
1372 * @phba: The Hba for which this call is being executed.
James Smartf9bb2da2011-10-10 21:34:11 -04001373 * @sc: The SCSI command to examine
1374 * @reftag: (out) BlockGuard reference tag for transmitted data
1375 * @apptag: (out) BlockGuard application tag for transmitted data
1376 * @new_guard (in) Value to replace CRC with if needed
1377 *
James Smart9a6b09c2012-03-01 22:37:42 -05001378 * Returns BG_ERR_* bit mask or 0 if request ignored
James Smartacd68592012-01-18 16:25:09 -05001379 **/
James Smartf9bb2da2011-10-10 21:34:11 -04001380static int
1381lpfc_bg_err_inject(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1382 uint32_t *reftag, uint16_t *apptag, uint32_t new_guard)
1383{
1384 struct scatterlist *sgpe; /* s/g prot entry */
1385 struct scatterlist *sgde; /* s/g data entry */
James Smart9a6b09c2012-03-01 22:37:42 -05001386 struct lpfc_scsi_buf *lpfc_cmd = NULL;
James Smartacd68592012-01-18 16:25:09 -05001387 struct scsi_dif_tuple *src = NULL;
James Smart4ac9b222012-03-01 22:38:29 -05001388 struct lpfc_nodelist *ndlp;
1389 struct lpfc_rport_data *rdata;
James Smartf9bb2da2011-10-10 21:34:11 -04001390 uint32_t op = scsi_get_prot_op(sc);
1391 uint32_t blksize;
1392 uint32_t numblks;
1393 sector_t lba;
1394 int rc = 0;
James Smartacd68592012-01-18 16:25:09 -05001395 int blockoff = 0;
James Smartf9bb2da2011-10-10 21:34:11 -04001396
1397 if (op == SCSI_PROT_NORMAL)
1398 return 0;
1399
James Smartacd68592012-01-18 16:25:09 -05001400 sgpe = scsi_prot_sglist(sc);
1401 sgde = scsi_sglist(sc);
James Smartf9bb2da2011-10-10 21:34:11 -04001402 lba = scsi_get_lba(sc);
James Smart4ac9b222012-03-01 22:38:29 -05001403
1404 /* First check if we need to match the LBA */
James Smartf9bb2da2011-10-10 21:34:11 -04001405 if (phba->lpfc_injerr_lba != LPFC_INJERR_LBA_OFF) {
1406 blksize = lpfc_cmd_blksize(sc);
1407 numblks = (scsi_bufflen(sc) + blksize - 1) / blksize;
1408
1409 /* Make sure we have the right LBA if one is specified */
1410 if ((phba->lpfc_injerr_lba < lba) ||
1411 (phba->lpfc_injerr_lba >= (lba + numblks)))
1412 return 0;
James Smartacd68592012-01-18 16:25:09 -05001413 if (sgpe) {
1414 blockoff = phba->lpfc_injerr_lba - lba;
1415 numblks = sg_dma_len(sgpe) /
1416 sizeof(struct scsi_dif_tuple);
1417 if (numblks < blockoff)
1418 blockoff = numblks;
James Smartacd68592012-01-18 16:25:09 -05001419 }
James Smartf9bb2da2011-10-10 21:34:11 -04001420 }
1421
James Smart4ac9b222012-03-01 22:38:29 -05001422 /* Next check if we need to match the remote NPortID or WWPN */
1423 rdata = sc->device->hostdata;
1424 if (rdata && rdata->pnode) {
1425 ndlp = rdata->pnode;
1426
1427 /* Make sure we have the right NPortID if one is specified */
1428 if (phba->lpfc_injerr_nportid &&
1429 (phba->lpfc_injerr_nportid != ndlp->nlp_DID))
1430 return 0;
1431
1432 /*
1433 * Make sure we have the right WWPN if one is specified.
1434 * wwn[0] should be a non-zero NAA in a good WWPN.
1435 */
1436 if (phba->lpfc_injerr_wwpn.u.wwn[0] &&
1437 (memcmp(&ndlp->nlp_portname, &phba->lpfc_injerr_wwpn,
1438 sizeof(struct lpfc_name)) != 0))
1439 return 0;
1440 }
1441
1442 /* Setup a ptr to the protection data if the SCSI host provides it */
1443 if (sgpe) {
1444 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
1445 src += blockoff;
1446 lpfc_cmd = (struct lpfc_scsi_buf *)sc->host_scribble;
1447 }
1448
James Smartf9bb2da2011-10-10 21:34:11 -04001449 /* Should we change the Reference Tag */
1450 if (reftag) {
James Smartacd68592012-01-18 16:25:09 -05001451 if (phba->lpfc_injerr_wref_cnt) {
1452 switch (op) {
1453 case SCSI_PROT_WRITE_PASS:
James Smart9a6b09c2012-03-01 22:37:42 -05001454 if (src) {
1455 /*
1456 * For WRITE_PASS, force the error
1457 * to be sent on the wire. It should
1458 * be detected by the Target.
1459 * If blockoff != 0 error will be
1460 * inserted in middle of the IO.
1461 */
James Smartf9bb2da2011-10-10 21:34:11 -04001462
James Smartacd68592012-01-18 16:25:09 -05001463 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1464 "9076 BLKGRD: Injecting reftag error: "
1465 "write lba x%lx + x%x oldrefTag x%x\n",
1466 (unsigned long)lba, blockoff,
James Smart9a6b09c2012-03-01 22:37:42 -05001467 be32_to_cpu(src->ref_tag));
James Smartacd68592012-01-18 16:25:09 -05001468
1469 /*
James Smart9a6b09c2012-03-01 22:37:42 -05001470 * Save the old ref_tag so we can
1471 * restore it on completion.
James Smartacd68592012-01-18 16:25:09 -05001472 */
James Smart9a6b09c2012-03-01 22:37:42 -05001473 if (lpfc_cmd) {
1474 lpfc_cmd->prot_data_type =
1475 LPFC_INJERR_REFTAG;
1476 lpfc_cmd->prot_data_segment =
1477 src;
1478 lpfc_cmd->prot_data =
1479 src->ref_tag;
1480 }
1481 src->ref_tag = cpu_to_be32(0xDEADBEEF);
James Smartacd68592012-01-18 16:25:09 -05001482 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001483 if (phba->lpfc_injerr_wref_cnt == 0) {
1484 phba->lpfc_injerr_nportid = 0;
1485 phba->lpfc_injerr_lba =
1486 LPFC_INJERR_LBA_OFF;
1487 memset(&phba->lpfc_injerr_wwpn,
1488 0, sizeof(struct lpfc_name));
1489 }
James Smart9a6b09c2012-03-01 22:37:42 -05001490 rc = BG_ERR_TGT | BG_ERR_CHECK;
1491
James Smartacd68592012-01-18 16:25:09 -05001492 break;
1493 }
1494 /* Drop thru */
James Smart9a6b09c2012-03-01 22:37:42 -05001495 case SCSI_PROT_WRITE_INSERT:
1496 /*
1497 * For WRITE_INSERT, force the error
1498 * to be sent on the wire. It should be
1499 * detected by the Target.
1500 */
1501 /* DEADBEEF will be the reftag on the wire */
1502 *reftag = 0xDEADBEEF;
1503 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001504 if (phba->lpfc_injerr_wref_cnt == 0) {
1505 phba->lpfc_injerr_nportid = 0;
1506 phba->lpfc_injerr_lba =
1507 LPFC_INJERR_LBA_OFF;
1508 memset(&phba->lpfc_injerr_wwpn,
1509 0, sizeof(struct lpfc_name));
1510 }
James Smart9a6b09c2012-03-01 22:37:42 -05001511 rc = BG_ERR_TGT | BG_ERR_CHECK;
1512
1513 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1514 "9078 BLKGRD: Injecting reftag error: "
1515 "write lba x%lx\n", (unsigned long)lba);
1516 break;
James Smartacd68592012-01-18 16:25:09 -05001517 case SCSI_PROT_WRITE_STRIP:
1518 /*
1519 * For WRITE_STRIP and WRITE_PASS,
1520 * force the error on data
1521 * being copied from SLI-Host to SLI-Port.
1522 */
1523 *reftag = 0xDEADBEEF;
1524 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001525 if (phba->lpfc_injerr_wref_cnt == 0) {
1526 phba->lpfc_injerr_nportid = 0;
1527 phba->lpfc_injerr_lba =
1528 LPFC_INJERR_LBA_OFF;
1529 memset(&phba->lpfc_injerr_wwpn,
1530 0, sizeof(struct lpfc_name));
1531 }
James Smartacd68592012-01-18 16:25:09 -05001532 rc = BG_ERR_INIT;
1533
1534 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1535 "9077 BLKGRD: Injecting reftag error: "
1536 "write lba x%lx\n", (unsigned long)lba);
1537 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001538 }
James Smartacd68592012-01-18 16:25:09 -05001539 }
1540 if (phba->lpfc_injerr_rref_cnt) {
1541 switch (op) {
1542 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001543 case SCSI_PROT_READ_STRIP:
1544 case SCSI_PROT_READ_PASS:
1545 /*
1546 * For READ_STRIP and READ_PASS, force the
1547 * error on data being read off the wire. It
1548 * should force an IO error to the driver.
1549 */
James Smartf9bb2da2011-10-10 21:34:11 -04001550 *reftag = 0xDEADBEEF;
1551 phba->lpfc_injerr_rref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001552 if (phba->lpfc_injerr_rref_cnt == 0) {
1553 phba->lpfc_injerr_nportid = 0;
1554 phba->lpfc_injerr_lba =
1555 LPFC_INJERR_LBA_OFF;
1556 memset(&phba->lpfc_injerr_wwpn,
1557 0, sizeof(struct lpfc_name));
1558 }
James Smartacd68592012-01-18 16:25:09 -05001559 rc = BG_ERR_INIT;
James Smartf9bb2da2011-10-10 21:34:11 -04001560
1561 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smartacd68592012-01-18 16:25:09 -05001562 "9079 BLKGRD: Injecting reftag error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001563 "read lba x%lx\n", (unsigned long)lba);
James Smartacd68592012-01-18 16:25:09 -05001564 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001565 }
1566 }
1567 }
1568
1569 /* Should we change the Application Tag */
1570 if (apptag) {
James Smartacd68592012-01-18 16:25:09 -05001571 if (phba->lpfc_injerr_wapp_cnt) {
1572 switch (op) {
1573 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001574 if (src) {
James Smart9a6b09c2012-03-01 22:37:42 -05001575 /*
1576 * For WRITE_PASS, force the error
1577 * to be sent on the wire. It should
1578 * be detected by the Target.
1579 * If blockoff != 0 error will be
1580 * inserted in middle of the IO.
1581 */
1582
James Smartacd68592012-01-18 16:25:09 -05001583 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1584 "9080 BLKGRD: Injecting apptag error: "
1585 "write lba x%lx + x%x oldappTag x%x\n",
1586 (unsigned long)lba, blockoff,
James Smart9a6b09c2012-03-01 22:37:42 -05001587 be16_to_cpu(src->app_tag));
James Smartacd68592012-01-18 16:25:09 -05001588
1589 /*
James Smart9a6b09c2012-03-01 22:37:42 -05001590 * Save the old app_tag so we can
1591 * restore it on completion.
James Smartacd68592012-01-18 16:25:09 -05001592 */
James Smart9a6b09c2012-03-01 22:37:42 -05001593 if (lpfc_cmd) {
1594 lpfc_cmd->prot_data_type =
1595 LPFC_INJERR_APPTAG;
1596 lpfc_cmd->prot_data_segment =
1597 src;
1598 lpfc_cmd->prot_data =
1599 src->app_tag;
1600 }
1601 src->app_tag = cpu_to_be16(0xDEAD);
James Smartacd68592012-01-18 16:25:09 -05001602 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001603 if (phba->lpfc_injerr_wapp_cnt == 0) {
1604 phba->lpfc_injerr_nportid = 0;
1605 phba->lpfc_injerr_lba =
1606 LPFC_INJERR_LBA_OFF;
1607 memset(&phba->lpfc_injerr_wwpn,
1608 0, sizeof(struct lpfc_name));
1609 }
James Smart9a6b09c2012-03-01 22:37:42 -05001610 rc = BG_ERR_TGT | BG_ERR_CHECK;
James Smartacd68592012-01-18 16:25:09 -05001611 break;
1612 }
1613 /* Drop thru */
James Smart9a6b09c2012-03-01 22:37:42 -05001614 case SCSI_PROT_WRITE_INSERT:
1615 /*
1616 * For WRITE_INSERT, force the
1617 * error to be sent on the wire. It should be
1618 * detected by the Target.
1619 */
1620 /* DEAD will be the apptag on the wire */
1621 *apptag = 0xDEAD;
1622 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001623 if (phba->lpfc_injerr_wapp_cnt == 0) {
1624 phba->lpfc_injerr_nportid = 0;
1625 phba->lpfc_injerr_lba =
1626 LPFC_INJERR_LBA_OFF;
1627 memset(&phba->lpfc_injerr_wwpn,
1628 0, sizeof(struct lpfc_name));
1629 }
James Smart9a6b09c2012-03-01 22:37:42 -05001630 rc = BG_ERR_TGT | BG_ERR_CHECK;
1631
1632 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1633 "0813 BLKGRD: Injecting apptag error: "
1634 "write lba x%lx\n", (unsigned long)lba);
1635 break;
James Smartacd68592012-01-18 16:25:09 -05001636 case SCSI_PROT_WRITE_STRIP:
1637 /*
1638 * For WRITE_STRIP and WRITE_PASS,
1639 * force the error on data
1640 * being copied from SLI-Host to SLI-Port.
1641 */
1642 *apptag = 0xDEAD;
1643 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001644 if (phba->lpfc_injerr_wapp_cnt == 0) {
1645 phba->lpfc_injerr_nportid = 0;
1646 phba->lpfc_injerr_lba =
1647 LPFC_INJERR_LBA_OFF;
1648 memset(&phba->lpfc_injerr_wwpn,
1649 0, sizeof(struct lpfc_name));
1650 }
James Smartacd68592012-01-18 16:25:09 -05001651 rc = BG_ERR_INIT;
1652
1653 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1654 "0812 BLKGRD: Injecting apptag error: "
1655 "write lba x%lx\n", (unsigned long)lba);
1656 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001657 }
James Smartacd68592012-01-18 16:25:09 -05001658 }
1659 if (phba->lpfc_injerr_rapp_cnt) {
1660 switch (op) {
1661 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001662 case SCSI_PROT_READ_STRIP:
1663 case SCSI_PROT_READ_PASS:
1664 /*
1665 * For READ_STRIP and READ_PASS, force the
1666 * error on data being read off the wire. It
1667 * should force an IO error to the driver.
1668 */
James Smartf9bb2da2011-10-10 21:34:11 -04001669 *apptag = 0xDEAD;
1670 phba->lpfc_injerr_rapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001671 if (phba->lpfc_injerr_rapp_cnt == 0) {
1672 phba->lpfc_injerr_nportid = 0;
1673 phba->lpfc_injerr_lba =
1674 LPFC_INJERR_LBA_OFF;
1675 memset(&phba->lpfc_injerr_wwpn,
1676 0, sizeof(struct lpfc_name));
1677 }
James Smartacd68592012-01-18 16:25:09 -05001678 rc = BG_ERR_INIT;
James Smartf9bb2da2011-10-10 21:34:11 -04001679
1680 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smartacd68592012-01-18 16:25:09 -05001681 "0814 BLKGRD: Injecting apptag error: "
1682 "read lba x%lx\n", (unsigned long)lba);
1683 break;
1684 }
1685 }
1686 }
1687
1688
1689 /* Should we change the Guard Tag */
1690 if (new_guard) {
1691 if (phba->lpfc_injerr_wgrd_cnt) {
1692 switch (op) {
1693 case SCSI_PROT_WRITE_PASS:
James Smart9a6b09c2012-03-01 22:37:42 -05001694 rc = BG_ERR_CHECK;
James Smartacd68592012-01-18 16:25:09 -05001695 /* Drop thru */
James Smartacd68592012-01-18 16:25:09 -05001696
James Smartacd68592012-01-18 16:25:09 -05001697 case SCSI_PROT_WRITE_INSERT:
1698 /*
1699 * For WRITE_INSERT, force the
1700 * error to be sent on the wire. It should be
1701 * detected by the Target.
1702 */
1703 phba->lpfc_injerr_wgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001704 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1705 phba->lpfc_injerr_nportid = 0;
1706 phba->lpfc_injerr_lba =
1707 LPFC_INJERR_LBA_OFF;
1708 memset(&phba->lpfc_injerr_wwpn,
1709 0, sizeof(struct lpfc_name));
1710 }
James Smartacd68592012-01-18 16:25:09 -05001711
James Smart9a6b09c2012-03-01 22:37:42 -05001712 rc |= BG_ERR_TGT | BG_ERR_SWAP;
James Smartacd68592012-01-18 16:25:09 -05001713 /* Signals the caller to swap CRC->CSUM */
1714
1715 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1716 "0817 BLKGRD: Injecting guard error: "
1717 "write lba x%lx\n", (unsigned long)lba);
1718 break;
James Smart9a6b09c2012-03-01 22:37:42 -05001719 case SCSI_PROT_WRITE_STRIP:
1720 /*
1721 * For WRITE_STRIP and WRITE_PASS,
1722 * force the error on data
1723 * being copied from SLI-Host to SLI-Port.
1724 */
1725 phba->lpfc_injerr_wgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001726 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1727 phba->lpfc_injerr_nportid = 0;
1728 phba->lpfc_injerr_lba =
1729 LPFC_INJERR_LBA_OFF;
1730 memset(&phba->lpfc_injerr_wwpn,
1731 0, sizeof(struct lpfc_name));
1732 }
James Smart9a6b09c2012-03-01 22:37:42 -05001733
1734 rc = BG_ERR_INIT | BG_ERR_SWAP;
1735 /* Signals the caller to swap CRC->CSUM */
1736
1737 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1738 "0816 BLKGRD: Injecting guard error: "
1739 "write lba x%lx\n", (unsigned long)lba);
1740 break;
James Smartacd68592012-01-18 16:25:09 -05001741 }
1742 }
1743 if (phba->lpfc_injerr_rgrd_cnt) {
1744 switch (op) {
1745 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001746 case SCSI_PROT_READ_STRIP:
1747 case SCSI_PROT_READ_PASS:
1748 /*
1749 * For READ_STRIP and READ_PASS, force the
1750 * error on data being read off the wire. It
1751 * should force an IO error to the driver.
1752 */
James Smartacd68592012-01-18 16:25:09 -05001753 phba->lpfc_injerr_rgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001754 if (phba->lpfc_injerr_rgrd_cnt == 0) {
1755 phba->lpfc_injerr_nportid = 0;
1756 phba->lpfc_injerr_lba =
1757 LPFC_INJERR_LBA_OFF;
1758 memset(&phba->lpfc_injerr_wwpn,
1759 0, sizeof(struct lpfc_name));
1760 }
James Smartacd68592012-01-18 16:25:09 -05001761
James Smart9a6b09c2012-03-01 22:37:42 -05001762 rc = BG_ERR_INIT | BG_ERR_SWAP;
James Smartacd68592012-01-18 16:25:09 -05001763 /* Signals the caller to swap CRC->CSUM */
1764
1765 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1766 "0818 BLKGRD: Injecting guard error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001767 "read lba x%lx\n", (unsigned long)lba);
1768 }
1769 }
1770 }
1771
James Smartf9bb2da2011-10-10 21:34:11 -04001772 return rc;
1773}
1774#endif
1775
James Smartacd68592012-01-18 16:25:09 -05001776/**
1777 * lpfc_sc_to_bg_opcodes - Determine the BlockGuard opcodes to be used with
1778 * the specified SCSI command.
1779 * @phba: The Hba for which this call is being executed.
James Smart6c8eea52010-04-06 14:49:53 -04001780 * @sc: The SCSI command to examine
1781 * @txopt: (out) BlockGuard operation for transmitted data
1782 * @rxopt: (out) BlockGuard operation for received data
1783 *
1784 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1785 *
James Smartacd68592012-01-18 16:25:09 -05001786 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001787static int
James Smart6c8eea52010-04-06 14:49:53 -04001788lpfc_sc_to_bg_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1789 uint8_t *txop, uint8_t *rxop)
James Smarte2a0a9d2008-12-04 22:40:02 -05001790{
1791 uint8_t guard_type = scsi_host_get_guard(sc->device->host);
James Smart6c8eea52010-04-06 14:49:53 -04001792 uint8_t ret = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05001793
1794 if (guard_type == SHOST_DIX_GUARD_IP) {
1795 switch (scsi_get_prot_op(sc)) {
1796 case SCSI_PROT_READ_INSERT:
1797 case SCSI_PROT_WRITE_STRIP:
James Smart6c8eea52010-04-06 14:49:53 -04001798 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001799 *txop = BG_OP_IN_CSUM_OUT_NODIF;
James Smarte2a0a9d2008-12-04 22:40:02 -05001800 break;
1801
1802 case SCSI_PROT_READ_STRIP:
1803 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001804 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001805 *txop = BG_OP_IN_NODIF_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001806 break;
1807
Martin K. Petersenc6af4042009-09-18 17:32:59 -04001808 case SCSI_PROT_READ_PASS:
1809 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001810 *rxop = BG_OP_IN_CRC_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001811 *txop = BG_OP_IN_CSUM_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001812 break;
1813
James Smarte2a0a9d2008-12-04 22:40:02 -05001814 case SCSI_PROT_NORMAL:
1815 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001816 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001817 "9063 BLKGRD: Bad op/guard:%d/IP combination\n",
1818 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001819 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001820 break;
1821
1822 }
James Smart7c56b9f2011-07-22 18:36:25 -04001823 } else {
James Smarte2a0a9d2008-12-04 22:40:02 -05001824 switch (scsi_get_prot_op(sc)) {
1825 case SCSI_PROT_READ_STRIP:
1826 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001827 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001828 *txop = BG_OP_IN_NODIF_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001829 break;
1830
1831 case SCSI_PROT_READ_PASS:
1832 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001833 *rxop = BG_OP_IN_CRC_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001834 *txop = BG_OP_IN_CRC_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001835 break;
1836
James Smarte2a0a9d2008-12-04 22:40:02 -05001837 case SCSI_PROT_READ_INSERT:
1838 case SCSI_PROT_WRITE_STRIP:
James Smart7c56b9f2011-07-22 18:36:25 -04001839 *rxop = BG_OP_IN_NODIF_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001840 *txop = BG_OP_IN_CRC_OUT_NODIF;
James Smart7c56b9f2011-07-22 18:36:25 -04001841 break;
1842
James Smarte2a0a9d2008-12-04 22:40:02 -05001843 case SCSI_PROT_NORMAL:
1844 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001845 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001846 "9075 BLKGRD: Bad op/guard:%d/CRC combination\n",
1847 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001848 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001849 break;
1850 }
James Smarte2a0a9d2008-12-04 22:40:02 -05001851 }
1852
James Smart6c8eea52010-04-06 14:49:53 -04001853 return ret;
James Smarte2a0a9d2008-12-04 22:40:02 -05001854}
1855
James Smartacd68592012-01-18 16:25:09 -05001856#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1857/**
1858 * lpfc_bg_err_opcodes - reDetermine the BlockGuard opcodes to be used with
1859 * the specified SCSI command in order to force a guard tag error.
1860 * @phba: The Hba for which this call is being executed.
1861 * @sc: The SCSI command to examine
1862 * @txopt: (out) BlockGuard operation for transmitted data
1863 * @rxopt: (out) BlockGuard operation for received data
1864 *
1865 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1866 *
1867 **/
1868static int
1869lpfc_bg_err_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1870 uint8_t *txop, uint8_t *rxop)
1871{
1872 uint8_t guard_type = scsi_host_get_guard(sc->device->host);
1873 uint8_t ret = 0;
1874
1875 if (guard_type == SHOST_DIX_GUARD_IP) {
1876 switch (scsi_get_prot_op(sc)) {
1877 case SCSI_PROT_READ_INSERT:
1878 case SCSI_PROT_WRITE_STRIP:
James Smartacd68592012-01-18 16:25:09 -05001879 *rxop = BG_OP_IN_NODIF_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001880 *txop = BG_OP_IN_CRC_OUT_NODIF;
James Smartacd68592012-01-18 16:25:09 -05001881 break;
1882
1883 case SCSI_PROT_READ_STRIP:
1884 case SCSI_PROT_WRITE_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001885 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001886 *txop = BG_OP_IN_NODIF_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001887 break;
1888
1889 case SCSI_PROT_READ_PASS:
1890 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001891 *rxop = BG_OP_IN_CSUM_OUT_CRC;
James Smart9a6b09c2012-03-01 22:37:42 -05001892 *txop = BG_OP_IN_CRC_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001893 break;
1894
1895 case SCSI_PROT_NORMAL:
1896 default:
1897 break;
1898
1899 }
1900 } else {
1901 switch (scsi_get_prot_op(sc)) {
1902 case SCSI_PROT_READ_STRIP:
1903 case SCSI_PROT_WRITE_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001904 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001905 *txop = BG_OP_IN_NODIF_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001906 break;
1907
1908 case SCSI_PROT_READ_PASS:
1909 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001910 *rxop = BG_OP_IN_CSUM_OUT_CSUM;
James Smart9a6b09c2012-03-01 22:37:42 -05001911 *txop = BG_OP_IN_CSUM_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001912 break;
1913
1914 case SCSI_PROT_READ_INSERT:
1915 case SCSI_PROT_WRITE_STRIP:
James Smartacd68592012-01-18 16:25:09 -05001916 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001917 *txop = BG_OP_IN_CSUM_OUT_NODIF;
James Smartacd68592012-01-18 16:25:09 -05001918 break;
1919
1920 case SCSI_PROT_NORMAL:
1921 default:
1922 break;
1923 }
1924 }
1925
1926 return ret;
1927}
1928#endif
1929
1930/**
1931 * lpfc_bg_setup_bpl - Setup BlockGuard BPL with no protection data
1932 * @phba: The Hba for which this call is being executed.
1933 * @sc: pointer to scsi command we're working on
1934 * @bpl: pointer to buffer list for protection groups
1935 * @datacnt: number of segments of data that have been dma mapped
1936 *
1937 * This function sets up BPL buffer list for protection groups of
James Smarte2a0a9d2008-12-04 22:40:02 -05001938 * type LPFC_PG_TYPE_NO_DIF
1939 *
1940 * This is usually used when the HBA is instructed to generate
1941 * DIFs and insert them into data stream (or strip DIF from
1942 * incoming data stream)
1943 *
1944 * The buffer list consists of just one protection group described
1945 * below:
1946 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001947 * start of prot group --> | PDE_5 |
1948 * +-------------------------+
1949 * | PDE_6 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001950 * +-------------------------+
1951 * | Data BDE |
1952 * +-------------------------+
1953 * |more Data BDE's ... (opt)|
1954 * +-------------------------+
1955 *
James Smarte2a0a9d2008-12-04 22:40:02 -05001956 *
1957 * Note: Data s/g buffers have been dma mapped
James Smartacd68592012-01-18 16:25:09 -05001958 *
1959 * Returns the number of BDEs added to the BPL.
1960 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001961static int
1962lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1963 struct ulp_bde64 *bpl, int datasegcnt)
1964{
1965 struct scatterlist *sgde = NULL; /* s/g data entry */
James Smart6c8eea52010-04-06 14:49:53 -04001966 struct lpfc_pde5 *pde5 = NULL;
1967 struct lpfc_pde6 *pde6 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05001968 dma_addr_t physaddr;
James Smart6c8eea52010-04-06 14:49:53 -04001969 int i = 0, num_bde = 0, status;
James Smarte2a0a9d2008-12-04 22:40:02 -05001970 int datadir = sc->sc_data_direction;
James Smart0829a192012-05-09 21:18:12 -04001971#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001972 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04001973#endif
James Smartacd68592012-01-18 16:25:09 -05001974 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001975 uint32_t reftag;
James Smart7c56b9f2011-07-22 18:36:25 -04001976 unsigned blksize;
James Smart6c8eea52010-04-06 14:49:53 -04001977 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05001978
James Smart6c8eea52010-04-06 14:49:53 -04001979 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1980 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05001981 goto out;
1982
James Smart6c8eea52010-04-06 14:49:53 -04001983 /* extract some info from the scsi command for pde*/
James Smarte2a0a9d2008-12-04 22:40:02 -05001984 blksize = lpfc_cmd_blksize(sc);
James Smartacd68592012-01-18 16:25:09 -05001985 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
James Smarte2a0a9d2008-12-04 22:40:02 -05001986
James Smartf9bb2da2011-10-10 21:34:11 -04001987#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05001988 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05001989 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05001990 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05001991 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05001992 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05001993 checking = 0;
1994 }
James Smartf9bb2da2011-10-10 21:34:11 -04001995#endif
1996
James Smart6c8eea52010-04-06 14:49:53 -04001997 /* setup PDE5 with what we have */
1998 pde5 = (struct lpfc_pde5 *) bpl;
1999 memset(pde5, 0, sizeof(struct lpfc_pde5));
2000 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05002001
James Smartbc739052010-08-04 16:11:18 -04002002 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04002003 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04002004 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04002005
James Smart6c8eea52010-04-06 14:49:53 -04002006 /* advance bpl and increment bde count */
2007 num_bde++;
2008 bpl++;
2009 pde6 = (struct lpfc_pde6 *) bpl;
2010
2011 /* setup PDE6 with the rest of the info */
2012 memset(pde6, 0, sizeof(struct lpfc_pde6));
2013 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
2014 bf_set(pde6_optx, pde6, txop);
2015 bf_set(pde6_oprx, pde6, rxop);
2016 if (datadir == DMA_FROM_DEVICE) {
James Smartacd68592012-01-18 16:25:09 -05002017 bf_set(pde6_ce, pde6, checking);
2018 bf_set(pde6_re, pde6, checking);
James Smart6c8eea52010-04-06 14:49:53 -04002019 }
2020 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04002021 bf_set(pde6_ae, pde6, 0);
2022 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04002023
James Smartbc739052010-08-04 16:11:18 -04002024 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04002025 pde6->word0 = cpu_to_le32(pde6->word0);
2026 pde6->word1 = cpu_to_le32(pde6->word1);
2027 pde6->word2 = cpu_to_le32(pde6->word2);
2028
James Smart6c8eea52010-04-06 14:49:53 -04002029 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05002030 num_bde++;
2031 bpl++;
2032
2033 /* assumption: caller has already run dma_map_sg on command data */
2034 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
2035 physaddr = sg_dma_address(sgde);
2036 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
2037 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
2038 bpl->tus.f.bdeSize = sg_dma_len(sgde);
2039 if (datadir == DMA_TO_DEVICE)
2040 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2041 else
2042 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2043 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2044 bpl++;
2045 num_bde++;
2046 }
2047
2048out:
2049 return num_bde;
2050}
2051
James Smartacd68592012-01-18 16:25:09 -05002052/**
2053 * lpfc_bg_setup_bpl_prot - Setup BlockGuard BPL with protection data
2054 * @phba: The Hba for which this call is being executed.
2055 * @sc: pointer to scsi command we're working on
2056 * @bpl: pointer to buffer list for protection groups
2057 * @datacnt: number of segments of data that have been dma mapped
2058 * @protcnt: number of segment of protection data that have been dma mapped
2059 *
2060 * This function sets up BPL buffer list for protection groups of
2061 * type LPFC_PG_TYPE_DIF
James Smarte2a0a9d2008-12-04 22:40:02 -05002062 *
2063 * This is usually used when DIFs are in their own buffers,
2064 * separate from the data. The HBA can then by instructed
2065 * to place the DIFs in the outgoing stream. For read operations,
2066 * The HBA could extract the DIFs and place it in DIF buffers.
2067 *
2068 * The buffer list for this type consists of one or more of the
2069 * protection groups described below:
2070 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002071 * start of first prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05002072 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002073 * | PDE_6 |
2074 * +-------------------------+
2075 * | PDE_7 (Prot BDE) |
James Smarte2a0a9d2008-12-04 22:40:02 -05002076 * +-------------------------+
2077 * | Data BDE |
2078 * +-------------------------+
2079 * |more Data BDE's ... (opt)|
2080 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002081 * start of new prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05002082 * +-------------------------+
2083 * | ... |
2084 * +-------------------------+
2085 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002086 * Note: It is assumed that both data and protection s/g buffers have been
2087 * mapped for DMA
James Smartacd68592012-01-18 16:25:09 -05002088 *
2089 * Returns the number of BDEs added to the BPL.
2090 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002091static int
2092lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2093 struct ulp_bde64 *bpl, int datacnt, int protcnt)
2094{
2095 struct scatterlist *sgde = NULL; /* s/g data entry */
2096 struct scatterlist *sgpe = NULL; /* s/g prot entry */
James Smart6c8eea52010-04-06 14:49:53 -04002097 struct lpfc_pde5 *pde5 = NULL;
2098 struct lpfc_pde6 *pde6 = NULL;
James Smart7f860592011-03-11 16:05:52 -05002099 struct lpfc_pde7 *pde7 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05002100 dma_addr_t dataphysaddr, protphysaddr;
2101 unsigned short curr_data = 0, curr_prot = 0;
James Smart7f860592011-03-11 16:05:52 -05002102 unsigned int split_offset;
2103 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
James Smarte2a0a9d2008-12-04 22:40:02 -05002104 unsigned int protgrp_blks, protgrp_bytes;
2105 unsigned int remainder, subtotal;
James Smart6c8eea52010-04-06 14:49:53 -04002106 int status;
James Smarte2a0a9d2008-12-04 22:40:02 -05002107 int datadir = sc->sc_data_direction;
2108 unsigned char pgdone = 0, alldone = 0;
2109 unsigned blksize;
James Smart0829a192012-05-09 21:18:12 -04002110#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002111 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002112#endif
James Smartacd68592012-01-18 16:25:09 -05002113 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05002114 uint32_t reftag;
James Smart6c8eea52010-04-06 14:49:53 -04002115 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05002116 int num_bde = 0;
2117
2118 sgpe = scsi_prot_sglist(sc);
2119 sgde = scsi_sglist(sc);
2120
2121 if (!sgpe || !sgde) {
2122 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2123 "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
2124 sgpe, sgde);
2125 return 0;
2126 }
2127
James Smart6c8eea52010-04-06 14:49:53 -04002128 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2129 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05002130 goto out;
2131
James Smart6c8eea52010-04-06 14:49:53 -04002132 /* extract some info from the scsi command */
James Smarte2a0a9d2008-12-04 22:40:02 -05002133 blksize = lpfc_cmd_blksize(sc);
James Smartacd68592012-01-18 16:25:09 -05002134 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
James Smarte2a0a9d2008-12-04 22:40:02 -05002135
James Smartf9bb2da2011-10-10 21:34:11 -04002136#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002137 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002138 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002139 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002140 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002141 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002142 checking = 0;
2143 }
James Smartf9bb2da2011-10-10 21:34:11 -04002144#endif
2145
James Smarte2a0a9d2008-12-04 22:40:02 -05002146 split_offset = 0;
2147 do {
James Smart6c8eea52010-04-06 14:49:53 -04002148 /* setup PDE5 with what we have */
2149 pde5 = (struct lpfc_pde5 *) bpl;
2150 memset(pde5, 0, sizeof(struct lpfc_pde5));
2151 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05002152
James Smartbc739052010-08-04 16:11:18 -04002153 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04002154 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04002155 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04002156
James Smart6c8eea52010-04-06 14:49:53 -04002157 /* advance bpl and increment bde count */
2158 num_bde++;
2159 bpl++;
2160 pde6 = (struct lpfc_pde6 *) bpl;
James Smarte2a0a9d2008-12-04 22:40:02 -05002161
James Smart6c8eea52010-04-06 14:49:53 -04002162 /* setup PDE6 with the rest of the info */
2163 memset(pde6, 0, sizeof(struct lpfc_pde6));
2164 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
2165 bf_set(pde6_optx, pde6, txop);
2166 bf_set(pde6_oprx, pde6, rxop);
James Smartacd68592012-01-18 16:25:09 -05002167 bf_set(pde6_ce, pde6, checking);
2168 bf_set(pde6_re, pde6, checking);
James Smart6c8eea52010-04-06 14:49:53 -04002169 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04002170 bf_set(pde6_ae, pde6, 0);
2171 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04002172
James Smartbc739052010-08-04 16:11:18 -04002173 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04002174 pde6->word0 = cpu_to_le32(pde6->word0);
2175 pde6->word1 = cpu_to_le32(pde6->word1);
2176 pde6->word2 = cpu_to_le32(pde6->word2);
2177
James Smart6c8eea52010-04-06 14:49:53 -04002178 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05002179 num_bde++;
2180 bpl++;
2181
2182 /* setup the first BDE that points to protection buffer */
James Smart7f860592011-03-11 16:05:52 -05002183 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2184 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
James Smarte2a0a9d2008-12-04 22:40:02 -05002185
James Smarte2a0a9d2008-12-04 22:40:02 -05002186 /* must be integer multiple of the DIF block length */
2187 BUG_ON(protgroup_len % 8);
2188
James Smart7f860592011-03-11 16:05:52 -05002189 pde7 = (struct lpfc_pde7 *) bpl;
2190 memset(pde7, 0, sizeof(struct lpfc_pde7));
2191 bf_set(pde7_type, pde7, LPFC_PDE7_DESCRIPTOR);
2192
James Smart7c56b9f2011-07-22 18:36:25 -04002193 pde7->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
2194 pde7->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
James Smart7f860592011-03-11 16:05:52 -05002195
James Smarte2a0a9d2008-12-04 22:40:02 -05002196 protgrp_blks = protgroup_len / 8;
2197 protgrp_bytes = protgrp_blks * blksize;
2198
James Smart7f860592011-03-11 16:05:52 -05002199 /* check if this pde is crossing the 4K boundary; if so split */
2200 if ((pde7->addrLow & 0xfff) + protgroup_len > 0x1000) {
2201 protgroup_remainder = 0x1000 - (pde7->addrLow & 0xfff);
2202 protgroup_offset += protgroup_remainder;
2203 protgrp_blks = protgroup_remainder / 8;
James Smart7c56b9f2011-07-22 18:36:25 -04002204 protgrp_bytes = protgrp_blks * blksize;
James Smart7f860592011-03-11 16:05:52 -05002205 } else {
2206 protgroup_offset = 0;
2207 curr_prot++;
2208 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002209
James Smarte2a0a9d2008-12-04 22:40:02 -05002210 num_bde++;
2211
2212 /* setup BDE's for data blocks associated with DIF data */
2213 pgdone = 0;
2214 subtotal = 0; /* total bytes processed for current prot grp */
2215 while (!pgdone) {
2216 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -04002217 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2218 "9065 BLKGRD:%s Invalid data segment\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002219 __func__);
2220 return 0;
2221 }
2222 bpl++;
2223 dataphysaddr = sg_dma_address(sgde) + split_offset;
2224 bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
2225 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
2226
2227 remainder = sg_dma_len(sgde) - split_offset;
2228
2229 if ((subtotal + remainder) <= protgrp_bytes) {
2230 /* we can use this whole buffer */
2231 bpl->tus.f.bdeSize = remainder;
2232 split_offset = 0;
2233
2234 if ((subtotal + remainder) == protgrp_bytes)
2235 pgdone = 1;
2236 } else {
2237 /* must split this buffer with next prot grp */
2238 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
2239 split_offset += bpl->tus.f.bdeSize;
2240 }
2241
2242 subtotal += bpl->tus.f.bdeSize;
2243
2244 if (datadir == DMA_TO_DEVICE)
2245 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2246 else
2247 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2248 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2249
2250 num_bde++;
2251 curr_data++;
2252
2253 if (split_offset)
2254 break;
2255
2256 /* Move to the next s/g segment if possible */
2257 sgde = sg_next(sgde);
James Smart6c8eea52010-04-06 14:49:53 -04002258
James Smarte2a0a9d2008-12-04 22:40:02 -05002259 }
2260
James Smart7f860592011-03-11 16:05:52 -05002261 if (protgroup_offset) {
2262 /* update the reference tag */
2263 reftag += protgrp_blks;
2264 bpl++;
2265 continue;
2266 }
2267
James Smarte2a0a9d2008-12-04 22:40:02 -05002268 /* are we done ? */
2269 if (curr_prot == protcnt) {
2270 alldone = 1;
2271 } else if (curr_prot < protcnt) {
2272 /* advance to next prot buffer */
2273 sgpe = sg_next(sgpe);
2274 bpl++;
2275
2276 /* update the reference tag */
2277 reftag += protgrp_blks;
2278 } else {
2279 /* if we're here, we have a bug */
James Smart6a9c52c2009-10-02 15:16:51 -04002280 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2281 "9054 BLKGRD: bug in %s\n", __func__);
James Smarte2a0a9d2008-12-04 22:40:02 -05002282 }
2283
2284 } while (!alldone);
James Smarte2a0a9d2008-12-04 22:40:02 -05002285out:
2286
James Smarte2a0a9d2008-12-04 22:40:02 -05002287 return num_bde;
2288}
James Smart7f860592011-03-11 16:05:52 -05002289
James Smartacd68592012-01-18 16:25:09 -05002290/**
2291 * lpfc_bg_setup_sgl - Setup BlockGuard SGL with no protection data
2292 * @phba: The Hba for which this call is being executed.
2293 * @sc: pointer to scsi command we're working on
2294 * @sgl: pointer to buffer list for protection groups
2295 * @datacnt: number of segments of data that have been dma mapped
2296 *
2297 * This function sets up SGL buffer list for protection groups of
2298 * type LPFC_PG_TYPE_NO_DIF
2299 *
2300 * This is usually used when the HBA is instructed to generate
2301 * DIFs and insert them into data stream (or strip DIF from
2302 * incoming data stream)
2303 *
2304 * The buffer list consists of just one protection group described
2305 * below:
2306 * +-------------------------+
2307 * start of prot group --> | DI_SEED |
2308 * +-------------------------+
2309 * | Data SGE |
2310 * +-------------------------+
2311 * |more Data SGE's ... (opt)|
2312 * +-------------------------+
2313 *
2314 *
2315 * Note: Data s/g buffers have been dma mapped
2316 *
2317 * Returns the number of SGEs added to the SGL.
2318 **/
2319static int
2320lpfc_bg_setup_sgl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2321 struct sli4_sge *sgl, int datasegcnt)
2322{
2323 struct scatterlist *sgde = NULL; /* s/g data entry */
2324 struct sli4_sge_diseed *diseed = NULL;
2325 dma_addr_t physaddr;
2326 int i = 0, num_sge = 0, status;
2327 int datadir = sc->sc_data_direction;
2328 uint32_t reftag;
2329 unsigned blksize;
2330 uint8_t txop, rxop;
James Smart0829a192012-05-09 21:18:12 -04002331#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002332 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002333#endif
James Smartacd68592012-01-18 16:25:09 -05002334 uint32_t checking = 1;
2335 uint32_t dma_len;
2336 uint32_t dma_offset = 0;
2337
2338 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2339 if (status)
2340 goto out;
2341
2342 /* extract some info from the scsi command for pde*/
2343 blksize = lpfc_cmd_blksize(sc);
2344 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2345
2346#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002347 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002348 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002349 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002350 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002351 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002352 checking = 0;
2353 }
2354#endif
2355
2356 /* setup DISEED with what we have */
2357 diseed = (struct sli4_sge_diseed *) sgl;
2358 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2359 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2360
2361 /* Endianness conversion if necessary */
2362 diseed->ref_tag = cpu_to_le32(reftag);
2363 diseed->ref_tag_tran = diseed->ref_tag;
2364
2365 /* setup DISEED with the rest of the info */
2366 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2367 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
2368 if (datadir == DMA_FROM_DEVICE) {
2369 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2370 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2371 }
2372 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2373 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2374
2375 /* Endianness conversion if necessary for DISEED */
2376 diseed->word2 = cpu_to_le32(diseed->word2);
2377 diseed->word3 = cpu_to_le32(diseed->word3);
2378
2379 /* advance bpl and increment sge count */
2380 num_sge++;
2381 sgl++;
2382
2383 /* assumption: caller has already run dma_map_sg on command data */
2384 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
2385 physaddr = sg_dma_address(sgde);
2386 dma_len = sg_dma_len(sgde);
2387 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
2388 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
2389 if ((i + 1) == datasegcnt)
2390 bf_set(lpfc_sli4_sge_last, sgl, 1);
2391 else
2392 bf_set(lpfc_sli4_sge_last, sgl, 0);
2393 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2394 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2395
2396 sgl->sge_len = cpu_to_le32(dma_len);
2397 dma_offset += dma_len;
2398
2399 sgl++;
2400 num_sge++;
2401 }
2402
2403out:
2404 return num_sge;
2405}
2406
2407/**
2408 * lpfc_bg_setup_sgl_prot - Setup BlockGuard SGL with protection data
2409 * @phba: The Hba for which this call is being executed.
2410 * @sc: pointer to scsi command we're working on
2411 * @sgl: pointer to buffer list for protection groups
2412 * @datacnt: number of segments of data that have been dma mapped
2413 * @protcnt: number of segment of protection data that have been dma mapped
2414 *
2415 * This function sets up SGL buffer list for protection groups of
2416 * type LPFC_PG_TYPE_DIF
2417 *
2418 * This is usually used when DIFs are in their own buffers,
2419 * separate from the data. The HBA can then by instructed
2420 * to place the DIFs in the outgoing stream. For read operations,
2421 * The HBA could extract the DIFs and place it in DIF buffers.
2422 *
2423 * The buffer list for this type consists of one or more of the
2424 * protection groups described below:
2425 * +-------------------------+
2426 * start of first prot group --> | DISEED |
2427 * +-------------------------+
2428 * | DIF (Prot SGE) |
2429 * +-------------------------+
2430 * | Data SGE |
2431 * +-------------------------+
2432 * |more Data SGE's ... (opt)|
2433 * +-------------------------+
2434 * start of new prot group --> | DISEED |
2435 * +-------------------------+
2436 * | ... |
2437 * +-------------------------+
2438 *
2439 * Note: It is assumed that both data and protection s/g buffers have been
2440 * mapped for DMA
2441 *
2442 * Returns the number of SGEs added to the SGL.
2443 **/
2444static int
2445lpfc_bg_setup_sgl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2446 struct sli4_sge *sgl, int datacnt, int protcnt)
2447{
2448 struct scatterlist *sgde = NULL; /* s/g data entry */
2449 struct scatterlist *sgpe = NULL; /* s/g prot entry */
2450 struct sli4_sge_diseed *diseed = NULL;
2451 dma_addr_t dataphysaddr, protphysaddr;
2452 unsigned short curr_data = 0, curr_prot = 0;
2453 unsigned int split_offset;
2454 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
2455 unsigned int protgrp_blks, protgrp_bytes;
2456 unsigned int remainder, subtotal;
2457 int status;
2458 unsigned char pgdone = 0, alldone = 0;
2459 unsigned blksize;
2460 uint32_t reftag;
2461 uint8_t txop, rxop;
2462 uint32_t dma_len;
James Smart0829a192012-05-09 21:18:12 -04002463#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002464 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002465#endif
James Smartacd68592012-01-18 16:25:09 -05002466 uint32_t checking = 1;
2467 uint32_t dma_offset = 0;
2468 int num_sge = 0;
2469
2470 sgpe = scsi_prot_sglist(sc);
2471 sgde = scsi_sglist(sc);
2472
2473 if (!sgpe || !sgde) {
2474 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2475 "9082 Invalid s/g entry: data=0x%p prot=0x%p\n",
2476 sgpe, sgde);
2477 return 0;
2478 }
2479
2480 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2481 if (status)
2482 goto out;
2483
2484 /* extract some info from the scsi command */
2485 blksize = lpfc_cmd_blksize(sc);
2486 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2487
2488#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002489 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002490 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002491 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002492 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002493 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002494 checking = 0;
2495 }
2496#endif
2497
2498 split_offset = 0;
2499 do {
2500 /* setup DISEED with what we have */
2501 diseed = (struct sli4_sge_diseed *) sgl;
2502 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2503 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2504
2505 /* Endianness conversion if necessary */
2506 diseed->ref_tag = cpu_to_le32(reftag);
2507 diseed->ref_tag_tran = diseed->ref_tag;
2508
2509 /* setup DISEED with the rest of the info */
2510 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2511 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
2512 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2513 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2514 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2515 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2516
2517 /* Endianness conversion if necessary for DISEED */
2518 diseed->word2 = cpu_to_le32(diseed->word2);
2519 diseed->word3 = cpu_to_le32(diseed->word3);
2520
2521 /* advance sgl and increment bde count */
2522 num_sge++;
2523 sgl++;
2524
2525 /* setup the first BDE that points to protection buffer */
2526 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2527 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
2528
2529 /* must be integer multiple of the DIF block length */
2530 BUG_ON(protgroup_len % 8);
2531
2532 /* Now setup DIF SGE */
2533 sgl->word2 = 0;
2534 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DIF);
2535 sgl->addr_hi = le32_to_cpu(putPaddrHigh(protphysaddr));
2536 sgl->addr_lo = le32_to_cpu(putPaddrLow(protphysaddr));
2537 sgl->word2 = cpu_to_le32(sgl->word2);
2538
2539 protgrp_blks = protgroup_len / 8;
2540 protgrp_bytes = protgrp_blks * blksize;
2541
2542 /* check if DIF SGE is crossing the 4K boundary; if so split */
2543 if ((sgl->addr_lo & 0xfff) + protgroup_len > 0x1000) {
2544 protgroup_remainder = 0x1000 - (sgl->addr_lo & 0xfff);
2545 protgroup_offset += protgroup_remainder;
2546 protgrp_blks = protgroup_remainder / 8;
2547 protgrp_bytes = protgrp_blks * blksize;
2548 } else {
2549 protgroup_offset = 0;
2550 curr_prot++;
2551 }
2552
2553 num_sge++;
2554
2555 /* setup SGE's for data blocks associated with DIF data */
2556 pgdone = 0;
2557 subtotal = 0; /* total bytes processed for current prot grp */
2558 while (!pgdone) {
2559 if (!sgde) {
2560 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2561 "9086 BLKGRD:%s Invalid data segment\n",
2562 __func__);
2563 return 0;
2564 }
2565 sgl++;
2566 dataphysaddr = sg_dma_address(sgde) + split_offset;
2567
2568 remainder = sg_dma_len(sgde) - split_offset;
2569
2570 if ((subtotal + remainder) <= protgrp_bytes) {
2571 /* we can use this whole buffer */
2572 dma_len = remainder;
2573 split_offset = 0;
2574
2575 if ((subtotal + remainder) == protgrp_bytes)
2576 pgdone = 1;
2577 } else {
2578 /* must split this buffer with next prot grp */
2579 dma_len = protgrp_bytes - subtotal;
2580 split_offset += dma_len;
2581 }
2582
2583 subtotal += dma_len;
2584
2585 sgl->addr_lo = cpu_to_le32(putPaddrLow(dataphysaddr));
2586 sgl->addr_hi = cpu_to_le32(putPaddrHigh(dataphysaddr));
2587 bf_set(lpfc_sli4_sge_last, sgl, 0);
2588 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2589 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2590
2591 sgl->sge_len = cpu_to_le32(dma_len);
2592 dma_offset += dma_len;
2593
2594 num_sge++;
2595 curr_data++;
2596
2597 if (split_offset)
2598 break;
2599
2600 /* Move to the next s/g segment if possible */
2601 sgde = sg_next(sgde);
2602 }
2603
2604 if (protgroup_offset) {
2605 /* update the reference tag */
2606 reftag += protgrp_blks;
2607 sgl++;
2608 continue;
2609 }
2610
2611 /* are we done ? */
2612 if (curr_prot == protcnt) {
2613 bf_set(lpfc_sli4_sge_last, sgl, 1);
2614 alldone = 1;
2615 } else if (curr_prot < protcnt) {
2616 /* advance to next prot buffer */
2617 sgpe = sg_next(sgpe);
2618 sgl++;
2619
2620 /* update the reference tag */
2621 reftag += protgrp_blks;
2622 } else {
2623 /* if we're here, we have a bug */
2624 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2625 "9085 BLKGRD: bug in %s\n", __func__);
2626 }
2627
2628 } while (!alldone);
2629
2630out:
2631
2632 return num_sge;
2633}
2634
2635/**
2636 * lpfc_prot_group_type - Get prtotection group type of SCSI command
2637 * @phba: The Hba for which this call is being executed.
2638 * @sc: pointer to scsi command we're working on
2639 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002640 * Given a SCSI command that supports DIF, determine composition of protection
2641 * groups involved in setting up buffer lists
2642 *
James Smartacd68592012-01-18 16:25:09 -05002643 * Returns: Protection group type (with or without DIF)
2644 *
2645 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002646static int
2647lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
2648{
2649 int ret = LPFC_PG_TYPE_INVALID;
2650 unsigned char op = scsi_get_prot_op(sc);
2651
2652 switch (op) {
2653 case SCSI_PROT_READ_STRIP:
2654 case SCSI_PROT_WRITE_INSERT:
2655 ret = LPFC_PG_TYPE_NO_DIF;
2656 break;
2657 case SCSI_PROT_READ_INSERT:
2658 case SCSI_PROT_WRITE_STRIP:
2659 case SCSI_PROT_READ_PASS:
2660 case SCSI_PROT_WRITE_PASS:
James Smarte2a0a9d2008-12-04 22:40:02 -05002661 ret = LPFC_PG_TYPE_DIF_BUF;
2662 break;
2663 default:
2664 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2665 "9021 Unsupported protection op:%d\n", op);
2666 break;
2667 }
2668
2669 return ret;
2670}
2671
James Smartacd68592012-01-18 16:25:09 -05002672/**
2673 * lpfc_bg_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
2674 * @phba: The Hba for which this call is being executed.
2675 * @lpfc_cmd: The scsi buffer which is going to be prep'ed.
2676 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002677 * This is the protection/DIF aware version of
2678 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
2679 * two functions eventually, but for now, it's here
James Smartacd68592012-01-18 16:25:09 -05002680 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002681static int
James Smartacd68592012-01-18 16:25:09 -05002682lpfc_bg_scsi_prep_dma_buf_s3(struct lpfc_hba *phba,
James Smarte2a0a9d2008-12-04 22:40:02 -05002683 struct lpfc_scsi_buf *lpfc_cmd)
2684{
2685 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2686 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2687 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
2688 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2689 uint32_t num_bde = 0;
2690 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
2691 int prot_group_type = 0;
2692 int diflen, fcpdl;
2693 unsigned blksize;
2694
2695 /*
2696 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
2697 * fcp_rsp regions to the first data bde entry
2698 */
2699 bpl += 2;
2700 if (scsi_sg_count(scsi_cmnd)) {
2701 /*
2702 * The driver stores the segment count returned from pci_map_sg
2703 * because this a count of dma-mappings used to map the use_sg
2704 * pages. They are not guaranteed to be the same for those
2705 * architectures that implement an IOMMU.
2706 */
2707 datasegcnt = dma_map_sg(&phba->pcidev->dev,
2708 scsi_sglist(scsi_cmnd),
2709 scsi_sg_count(scsi_cmnd), datadir);
2710 if (unlikely(!datasegcnt))
2711 return 1;
2712
2713 lpfc_cmd->seg_cnt = datasegcnt;
2714 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04002715 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2716 "9067 BLKGRD: %s: Too many sg segments"
2717 " from dma_map_sg. Config %d, seg_cnt"
2718 " %d\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002719 __func__, phba->cfg_sg_seg_cnt,
2720 lpfc_cmd->seg_cnt);
2721 scsi_dma_unmap(scsi_cmnd);
2722 return 1;
2723 }
2724
2725 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
2726
2727 switch (prot_group_type) {
2728 case LPFC_PG_TYPE_NO_DIF:
2729 num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
2730 datasegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002731 /* we should have 2 or more entries in buffer list */
James Smarte2a0a9d2008-12-04 22:40:02 -05002732 if (num_bde < 2)
2733 goto err;
2734 break;
2735 case LPFC_PG_TYPE_DIF_BUF:{
2736 /*
2737 * This type indicates that protection buffers are
2738 * passed to the driver, so that needs to be prepared
2739 * for DMA
2740 */
2741 protsegcnt = dma_map_sg(&phba->pcidev->dev,
2742 scsi_prot_sglist(scsi_cmnd),
2743 scsi_prot_sg_count(scsi_cmnd), datadir);
2744 if (unlikely(!protsegcnt)) {
2745 scsi_dma_unmap(scsi_cmnd);
2746 return 1;
2747 }
2748
2749 lpfc_cmd->prot_seg_cnt = protsegcnt;
2750 if (lpfc_cmd->prot_seg_cnt
2751 > phba->cfg_prot_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04002752 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2753 "9068 BLKGRD: %s: Too many prot sg "
2754 "segments from dma_map_sg. Config %d,"
James Smarte2a0a9d2008-12-04 22:40:02 -05002755 "prot_seg_cnt %d\n", __func__,
2756 phba->cfg_prot_sg_seg_cnt,
2757 lpfc_cmd->prot_seg_cnt);
2758 dma_unmap_sg(&phba->pcidev->dev,
2759 scsi_prot_sglist(scsi_cmnd),
2760 scsi_prot_sg_count(scsi_cmnd),
2761 datadir);
2762 scsi_dma_unmap(scsi_cmnd);
2763 return 1;
2764 }
2765
2766 num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
2767 datasegcnt, protsegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002768 /* we should have 3 or more entries in buffer list */
James Smarte2a0a9d2008-12-04 22:40:02 -05002769 if (num_bde < 3)
2770 goto err;
2771 break;
2772 }
2773 case LPFC_PG_TYPE_INVALID:
2774 default:
2775 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2776 "9022 Unexpected protection group %i\n",
2777 prot_group_type);
2778 return 1;
2779 }
2780 }
2781
2782 /*
2783 * Finish initializing those IOCB fields that are dependent on the
2784 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
2785 * reinitialized since all iocb memory resources are used many times
2786 * for transmit, receive, and continuation bpl's.
2787 */
2788 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
2789 iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
2790 iocb_cmd->ulpBdeCount = 1;
2791 iocb_cmd->ulpLe = 1;
2792
2793 fcpdl = scsi_bufflen(scsi_cmnd);
2794
2795 if (scsi_get_prot_type(scsi_cmnd) == SCSI_PROT_DIF_TYPE1) {
2796 /*
2797 * We are in DIF Type 1 mode
2798 * Every data block has a 8 byte DIF (trailer)
2799 * attached to it. Must ajust FCP data length
2800 */
2801 blksize = lpfc_cmd_blksize(scsi_cmnd);
2802 diflen = (fcpdl / blksize) * 8;
2803 fcpdl += diflen;
2804 }
2805 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
2806
2807 /*
2808 * Due to difference in data length between DIF/non-DIF paths,
2809 * we need to set word 4 of IOCB here
2810 */
2811 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
2812
2813 return 0;
2814err:
2815 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2816 "9023 Could not setup all needed BDE's"
2817 "prot_group_type=%d, num_bde=%d\n",
2818 prot_group_type, num_bde);
2819 return 1;
2820}
2821
2822/*
2823 * This function checks for BlockGuard errors detected by
2824 * the HBA. In case of errors, the ASC/ASCQ fields in the
2825 * sense buffer will be set accordingly, paired with
2826 * ILLEGAL_REQUEST to signal to the kernel that the HBA
2827 * detected corruption.
2828 *
2829 * Returns:
2830 * 0 - No error found
2831 * 1 - BlockGuard error found
2832 * -1 - Internal error (bad profile, ...etc)
2833 */
2834static int
2835lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
2836 struct lpfc_iocbq *pIocbOut)
2837{
2838 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
2839 struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
2840 int ret = 0;
2841 uint32_t bghm = bgf->bghm;
2842 uint32_t bgstat = bgf->bgstat;
2843 uint64_t failing_sector = 0;
2844
James Smart6a9c52c2009-10-02 15:16:51 -04002845 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9069 BLKGRD: BG ERROR in cmd"
2846 " 0x%x lba 0x%llx blk cnt 0x%x "
James Smarte2a0a9d2008-12-04 22:40:02 -05002847 "bgstat=0x%x bghm=0x%x\n",
James Smart87b5c322008-12-16 10:34:09 -05002848 cmd->cmnd[0], (unsigned long long)scsi_get_lba(cmd),
Tejun Heo83096eb2009-05-07 22:24:39 +09002849 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05002850
2851 spin_lock(&_dump_buf_lock);
2852 if (!_dump_buf_done) {
James Smart6a9c52c2009-10-02 15:16:51 -04002853 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9070 BLKGRD: Saving"
2854 " Data for %u blocks to debugfs\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002855 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
James Smart6a9c52c2009-10-02 15:16:51 -04002856 lpfc_debug_save_data(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002857
2858 /* If we have a prot sgl, save the DIF buffer */
2859 if (lpfc_prot_group_type(phba, cmd) ==
2860 LPFC_PG_TYPE_DIF_BUF) {
James Smart6a9c52c2009-10-02 15:16:51 -04002861 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9071 BLKGRD: "
2862 "Saving DIF for %u blocks to debugfs\n",
2863 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
2864 lpfc_debug_save_dif(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002865 }
2866
2867 _dump_buf_done = 1;
2868 }
2869 spin_unlock(&_dump_buf_lock);
2870
2871 if (lpfc_bgs_get_invalid_prof(bgstat)) {
2872 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart6a9c52c2009-10-02 15:16:51 -04002873 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9072 BLKGRD: Invalid"
2874 " BlockGuard profile. bgstat:0x%x\n",
2875 bgstat);
James Smarte2a0a9d2008-12-04 22:40:02 -05002876 ret = (-1);
2877 goto out;
2878 }
2879
2880 if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
2881 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart6a9c52c2009-10-02 15:16:51 -04002882 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9073 BLKGRD: "
2883 "Invalid BlockGuard DIF Block. bgstat:0x%x\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002884 bgstat);
2885 ret = (-1);
2886 goto out;
2887 }
2888
2889 if (lpfc_bgs_get_guard_err(bgstat)) {
2890 ret = 1;
2891
2892 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2893 0x10, 0x1);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05002894 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05002895 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
2896 phba->bg_guard_err_cnt++;
James Smart6a9c52c2009-10-02 15:16:51 -04002897 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2898 "9055 BLKGRD: guard_tag error\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05002899 }
2900
2901 if (lpfc_bgs_get_reftag_err(bgstat)) {
2902 ret = 1;
2903
2904 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2905 0x10, 0x3);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05002906 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05002907 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
2908
2909 phba->bg_reftag_err_cnt++;
James Smart6a9c52c2009-10-02 15:16:51 -04002910 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2911 "9056 BLKGRD: ref_tag error\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05002912 }
2913
2914 if (lpfc_bgs_get_apptag_err(bgstat)) {
2915 ret = 1;
2916
2917 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2918 0x10, 0x2);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05002919 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05002920 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
2921
2922 phba->bg_apptag_err_cnt++;
James Smart6a9c52c2009-10-02 15:16:51 -04002923 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2924 "9061 BLKGRD: app_tag error\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05002925 }
2926
2927 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
2928 /*
2929 * setup sense data descriptor 0 per SPC-4 as an information
James Smart7c56b9f2011-07-22 18:36:25 -04002930 * field, and put the failing LBA in it.
2931 * This code assumes there was also a guard/app/ref tag error
2932 * indication.
James Smarte2a0a9d2008-12-04 22:40:02 -05002933 */
James Smart7c56b9f2011-07-22 18:36:25 -04002934 cmd->sense_buffer[7] = 0xc; /* Additional sense length */
2935 cmd->sense_buffer[8] = 0; /* Information descriptor type */
2936 cmd->sense_buffer[9] = 0xa; /* Additional descriptor length */
2937 cmd->sense_buffer[10] = 0x80; /* Validity bit */
James Smartacd68592012-01-18 16:25:09 -05002938
2939 /* bghm is a "on the wire" FC frame based count */
2940 switch (scsi_get_prot_op(cmd)) {
2941 case SCSI_PROT_READ_INSERT:
2942 case SCSI_PROT_WRITE_STRIP:
2943 bghm /= cmd->device->sector_size;
2944 break;
2945 case SCSI_PROT_READ_STRIP:
2946 case SCSI_PROT_WRITE_INSERT:
2947 case SCSI_PROT_READ_PASS:
2948 case SCSI_PROT_WRITE_PASS:
2949 bghm /= (cmd->device->sector_size +
2950 sizeof(struct scsi_dif_tuple));
2951 break;
2952 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002953
2954 failing_sector = scsi_get_lba(cmd);
2955 failing_sector += bghm;
2956
James Smart7c56b9f2011-07-22 18:36:25 -04002957 /* Descriptor Information */
2958 put_unaligned_be64(failing_sector, &cmd->sense_buffer[12]);
James Smarte2a0a9d2008-12-04 22:40:02 -05002959 }
2960
2961 if (!ret) {
2962 /* No error was reported - problem in FW? */
2963 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart6a9c52c2009-10-02 15:16:51 -04002964 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart4ac9b222012-03-01 22:38:29 -05002965 "9057 BLKGRD: Unknown error reported!\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05002966 }
2967
2968out:
2969 return ret;
2970}
2971
James Smartea2151b2008-09-07 11:52:10 -04002972/**
James Smartda0436e2009-05-22 14:51:39 -04002973 * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
2974 * @phba: The Hba for which this call is being executed.
2975 * @lpfc_cmd: The scsi buffer which is going to be mapped.
2976 *
2977 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
2978 * field of @lpfc_cmd for device with SLI-4 interface spec.
2979 *
2980 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04002981 * 1 - Error
2982 * 0 - Success
James Smartda0436e2009-05-22 14:51:39 -04002983 **/
2984static int
2985lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
2986{
2987 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2988 struct scatterlist *sgel = NULL;
2989 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2990 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
James Smartfedd3b72011-02-16 12:39:24 -05002991 struct sli4_sge *first_data_sgl;
James Smartda0436e2009-05-22 14:51:39 -04002992 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2993 dma_addr_t physaddr;
2994 uint32_t num_bde = 0;
2995 uint32_t dma_len;
2996 uint32_t dma_offset = 0;
2997 int nseg;
James Smartfedd3b72011-02-16 12:39:24 -05002998 struct ulp_bde64 *bde;
James Smartda0436e2009-05-22 14:51:39 -04002999
3000 /*
3001 * There are three possibilities here - use scatter-gather segment, use
3002 * the single mapping, or neither. Start the lpfc command prep by
3003 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
3004 * data bde entry.
3005 */
3006 if (scsi_sg_count(scsi_cmnd)) {
3007 /*
3008 * The driver stores the segment count returned from pci_map_sg
3009 * because this a count of dma-mappings used to map the use_sg
3010 * pages. They are not guaranteed to be the same for those
3011 * architectures that implement an IOMMU.
3012 */
3013
3014 nseg = scsi_dma_map(scsi_cmnd);
3015 if (unlikely(!nseg))
3016 return 1;
3017 sgl += 1;
3018 /* clear the last flag in the fcp_rsp map entry */
3019 sgl->word2 = le32_to_cpu(sgl->word2);
3020 bf_set(lpfc_sli4_sge_last, sgl, 0);
3021 sgl->word2 = cpu_to_le32(sgl->word2);
3022 sgl += 1;
James Smartfedd3b72011-02-16 12:39:24 -05003023 first_data_sgl = sgl;
James Smartda0436e2009-05-22 14:51:39 -04003024 lpfc_cmd->seg_cnt = nseg;
3025 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04003026 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
3027 " %s: Too many sg segments from "
3028 "dma_map_sg. Config %d, seg_cnt %d\n",
3029 __func__, phba->cfg_sg_seg_cnt,
James Smartda0436e2009-05-22 14:51:39 -04003030 lpfc_cmd->seg_cnt);
3031 scsi_dma_unmap(scsi_cmnd);
3032 return 1;
3033 }
3034
3035 /*
3036 * The driver established a maximum scatter-gather segment count
3037 * during probe that limits the number of sg elements in any
3038 * single scsi command. Just run through the seg_cnt and format
3039 * the sge's.
3040 * When using SLI-3 the driver will try to fit all the BDEs into
3041 * the IOCB. If it can't then the BDEs get added to a BPL as it
3042 * does for SLI-2 mode.
3043 */
3044 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
3045 physaddr = sg_dma_address(sgel);
3046 dma_len = sg_dma_len(sgel);
James Smartda0436e2009-05-22 14:51:39 -04003047 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
3048 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
James Smart05580562011-05-24 11:40:48 -04003049 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -04003050 if ((num_bde + 1) == nseg)
3051 bf_set(lpfc_sli4_sge_last, sgl, 1);
3052 else
3053 bf_set(lpfc_sli4_sge_last, sgl, 0);
3054 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
James Smartf9bb2da2011-10-10 21:34:11 -04003055 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
James Smartda0436e2009-05-22 14:51:39 -04003056 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05003057 sgl->sge_len = cpu_to_le32(dma_len);
James Smartda0436e2009-05-22 14:51:39 -04003058 dma_offset += dma_len;
3059 sgl++;
3060 }
James Smartfedd3b72011-02-16 12:39:24 -05003061 /* setup the performance hint (first data BDE) if enabled */
3062 if (phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) {
3063 bde = (struct ulp_bde64 *)
3064 &(iocb_cmd->unsli3.sli3Words[5]);
3065 bde->addrLow = first_data_sgl->addr_lo;
3066 bde->addrHigh = first_data_sgl->addr_hi;
3067 bde->tus.f.bdeSize =
3068 le32_to_cpu(first_data_sgl->sge_len);
3069 bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
3070 bde->tus.w = cpu_to_le32(bde->tus.w);
3071 }
James Smartda0436e2009-05-22 14:51:39 -04003072 } else {
3073 sgl += 1;
3074 /* clear the last flag in the fcp_rsp map entry */
3075 sgl->word2 = le32_to_cpu(sgl->word2);
3076 bf_set(lpfc_sli4_sge_last, sgl, 1);
3077 sgl->word2 = cpu_to_le32(sgl->word2);
3078 }
3079
3080 /*
3081 * Finish initializing those IOCB fields that are dependent on the
3082 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
3083 * explicitly reinitialized.
3084 * all iocb memory resources are reused.
3085 */
3086 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
3087
3088 /*
3089 * Due to difference in data length between DIF/non-DIF paths,
3090 * we need to set word 4 of IOCB here
3091 */
3092 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
3093 return 0;
3094}
3095
3096/**
James Smartacd68592012-01-18 16:25:09 -05003097 * lpfc_bg_scsi_adjust_dl - Adjust SCSI data length for BlockGuard
3098 * @phba: The Hba for which this call is being executed.
3099 * @lpfc_cmd: The scsi buffer which is going to be adjusted.
3100 *
3101 * Adjust the data length to account for how much data
3102 * is actually on the wire.
3103 *
3104 * returns the adjusted data length
3105 **/
3106static int
3107lpfc_bg_scsi_adjust_dl(struct lpfc_hba *phba,
3108 struct lpfc_scsi_buf *lpfc_cmd)
3109{
3110 struct scsi_cmnd *sc = lpfc_cmd->pCmd;
3111 int diflen, fcpdl;
3112 unsigned blksize;
3113
3114 fcpdl = scsi_bufflen(sc);
3115
3116 /* Check if there is protection data on the wire */
3117 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
3118 /* Read */
3119 if (scsi_get_prot_op(sc) == SCSI_PROT_READ_INSERT)
3120 return fcpdl;
3121
3122 } else {
3123 /* Write */
3124 if (scsi_get_prot_op(sc) == SCSI_PROT_WRITE_STRIP)
3125 return fcpdl;
3126 }
3127
3128 /* If protection data on the wire, adjust the count accordingly */
3129 blksize = lpfc_cmd_blksize(sc);
3130 diflen = (fcpdl / blksize) * 8;
3131 fcpdl += diflen;
3132 return fcpdl;
3133}
3134
3135/**
3136 * lpfc_bg_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3137 * @phba: The Hba for which this call is being executed.
3138 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3139 *
3140 * This is the protection/DIF aware version of
3141 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
3142 * two functions eventually, but for now, it's here
3143 **/
3144static int
3145lpfc_bg_scsi_prep_dma_buf_s4(struct lpfc_hba *phba,
3146 struct lpfc_scsi_buf *lpfc_cmd)
3147{
3148 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3149 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3150 struct sli4_sge *sgl = (struct sli4_sge *)(lpfc_cmd->fcp_bpl);
3151 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
3152 uint32_t num_bde = 0;
3153 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
3154 int prot_group_type = 0;
3155 int fcpdl;
3156
3157 /*
3158 * Start the lpfc command prep by bumping the sgl beyond fcp_cmnd
3159 * fcp_rsp regions to the first data bde entry
3160 */
3161 if (scsi_sg_count(scsi_cmnd)) {
3162 /*
3163 * The driver stores the segment count returned from pci_map_sg
3164 * because this a count of dma-mappings used to map the use_sg
3165 * pages. They are not guaranteed to be the same for those
3166 * architectures that implement an IOMMU.
3167 */
3168 datasegcnt = dma_map_sg(&phba->pcidev->dev,
3169 scsi_sglist(scsi_cmnd),
3170 scsi_sg_count(scsi_cmnd), datadir);
3171 if (unlikely(!datasegcnt))
3172 return 1;
3173
3174 sgl += 1;
3175 /* clear the last flag in the fcp_rsp map entry */
3176 sgl->word2 = le32_to_cpu(sgl->word2);
3177 bf_set(lpfc_sli4_sge_last, sgl, 0);
3178 sgl->word2 = cpu_to_le32(sgl->word2);
3179
3180 sgl += 1;
3181 lpfc_cmd->seg_cnt = datasegcnt;
3182 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
3183 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
3184 "9087 BLKGRD: %s: Too many sg segments"
3185 " from dma_map_sg. Config %d, seg_cnt"
3186 " %d\n",
3187 __func__, phba->cfg_sg_seg_cnt,
3188 lpfc_cmd->seg_cnt);
3189 scsi_dma_unmap(scsi_cmnd);
3190 return 1;
3191 }
3192
3193 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
3194
3195 switch (prot_group_type) {
3196 case LPFC_PG_TYPE_NO_DIF:
3197 num_bde = lpfc_bg_setup_sgl(phba, scsi_cmnd, sgl,
3198 datasegcnt);
3199 /* we should have 2 or more entries in buffer list */
3200 if (num_bde < 2)
3201 goto err;
3202 break;
3203 case LPFC_PG_TYPE_DIF_BUF:{
3204 /*
3205 * This type indicates that protection buffers are
3206 * passed to the driver, so that needs to be prepared
3207 * for DMA
3208 */
3209 protsegcnt = dma_map_sg(&phba->pcidev->dev,
3210 scsi_prot_sglist(scsi_cmnd),
3211 scsi_prot_sg_count(scsi_cmnd), datadir);
3212 if (unlikely(!protsegcnt)) {
3213 scsi_dma_unmap(scsi_cmnd);
3214 return 1;
3215 }
3216
3217 lpfc_cmd->prot_seg_cnt = protsegcnt;
3218 if (lpfc_cmd->prot_seg_cnt
3219 > phba->cfg_prot_sg_seg_cnt) {
3220 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
3221 "9088 BLKGRD: %s: Too many prot sg "
3222 "segments from dma_map_sg. Config %d,"
3223 "prot_seg_cnt %d\n", __func__,
3224 phba->cfg_prot_sg_seg_cnt,
3225 lpfc_cmd->prot_seg_cnt);
3226 dma_unmap_sg(&phba->pcidev->dev,
3227 scsi_prot_sglist(scsi_cmnd),
3228 scsi_prot_sg_count(scsi_cmnd),
3229 datadir);
3230 scsi_dma_unmap(scsi_cmnd);
3231 return 1;
3232 }
3233
3234 num_bde = lpfc_bg_setup_sgl_prot(phba, scsi_cmnd, sgl,
3235 datasegcnt, protsegcnt);
3236 /* we should have 3 or more entries in buffer list */
3237 if (num_bde < 3)
3238 goto err;
3239 break;
3240 }
3241 case LPFC_PG_TYPE_INVALID:
3242 default:
3243 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
3244 "9083 Unexpected protection group %i\n",
3245 prot_group_type);
3246 return 1;
3247 }
3248 }
3249
James Smart8012cc32012-10-31 14:44:49 -04003250 switch (scsi_get_prot_op(scsi_cmnd)) {
3251 case SCSI_PROT_WRITE_STRIP:
3252 case SCSI_PROT_READ_STRIP:
3253 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_STRIP;
3254 break;
3255 case SCSI_PROT_WRITE_INSERT:
3256 case SCSI_PROT_READ_INSERT:
3257 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_INSERT;
3258 break;
3259 case SCSI_PROT_WRITE_PASS:
3260 case SCSI_PROT_READ_PASS:
3261 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_PASS;
3262 break;
3263 }
3264
James Smartacd68592012-01-18 16:25:09 -05003265 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
3266
3267 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
3268
3269 /*
3270 * Due to difference in data length between DIF/non-DIF paths,
3271 * we need to set word 4 of IOCB here
3272 */
3273 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
James Smartacd68592012-01-18 16:25:09 -05003274
3275 return 0;
3276err:
3277 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
3278 "9084 Could not setup all needed BDE's"
3279 "prot_group_type=%d, num_bde=%d\n",
3280 prot_group_type, num_bde);
3281 return 1;
3282}
3283
3284/**
James Smart3772a992009-05-22 14:50:54 -04003285 * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3286 * @phba: The Hba for which this call is being executed.
3287 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3288 *
3289 * This routine wraps the actual DMA mapping function pointer from the
3290 * lpfc_hba struct.
3291 *
3292 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04003293 * 1 - Error
3294 * 0 - Success
James Smart3772a992009-05-22 14:50:54 -04003295 **/
3296static inline int
3297lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3298{
3299 return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
3300}
3301
3302/**
James Smartacd68592012-01-18 16:25:09 -05003303 * lpfc_bg_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3304 * using BlockGuard.
3305 * @phba: The Hba for which this call is being executed.
3306 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3307 *
3308 * This routine wraps the actual DMA mapping function pointer from the
3309 * lpfc_hba struct.
3310 *
3311 * Return codes:
3312 * 1 - Error
3313 * 0 - Success
3314 **/
3315static inline int
3316lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3317{
3318 return phba->lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
3319}
3320
3321/**
James Smart3621a712009-04-06 18:47:14 -04003322 * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
James Smartea2151b2008-09-07 11:52:10 -04003323 * @phba: Pointer to hba context object.
3324 * @vport: Pointer to vport object.
3325 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
3326 * @rsp_iocb: Pointer to response iocb object which reported error.
3327 *
3328 * This function posts an event when there is a SCSI command reporting
3329 * error from the scsi device.
3330 **/
3331static void
3332lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
3333 struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
3334 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3335 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
3336 uint32_t resp_info = fcprsp->rspStatus2;
3337 uint32_t scsi_status = fcprsp->rspStatus3;
3338 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
3339 struct lpfc_fast_path_event *fast_path_evt = NULL;
3340 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
3341 unsigned long flags;
3342
James Smart5989b8d2010-10-22 11:06:56 -04003343 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3344 return;
3345
James Smartea2151b2008-09-07 11:52:10 -04003346 /* If there is queuefull or busy condition send a scsi event */
3347 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
3348 (cmnd->result == SAM_STAT_BUSY)) {
3349 fast_path_evt = lpfc_alloc_fast_evt(phba);
3350 if (!fast_path_evt)
3351 return;
3352 fast_path_evt->un.scsi_evt.event_type =
3353 FC_REG_SCSI_EVENT;
3354 fast_path_evt->un.scsi_evt.subcategory =
3355 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
3356 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
3357 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
3358 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
3359 &pnode->nlp_portname, sizeof(struct lpfc_name));
3360 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
3361 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3362 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
3363 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
3364 fast_path_evt = lpfc_alloc_fast_evt(phba);
3365 if (!fast_path_evt)
3366 return;
3367 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
3368 FC_REG_SCSI_EVENT;
3369 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
3370 LPFC_EVENT_CHECK_COND;
3371 fast_path_evt->un.check_cond_evt.scsi_event.lun =
3372 cmnd->device->lun;
3373 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
3374 &pnode->nlp_portname, sizeof(struct lpfc_name));
3375 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
3376 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3377 fast_path_evt->un.check_cond_evt.sense_key =
3378 cmnd->sense_buffer[2] & 0xf;
3379 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
3380 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
3381 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3382 fcpi_parm &&
3383 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
3384 ((scsi_status == SAM_STAT_GOOD) &&
3385 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
3386 /*
3387 * If status is good or resid does not match with fcp_param and
3388 * there is valid fcpi_parm, then there is a read_check error
3389 */
3390 fast_path_evt = lpfc_alloc_fast_evt(phba);
3391 if (!fast_path_evt)
3392 return;
3393 fast_path_evt->un.read_check_error.header.event_type =
3394 FC_REG_FABRIC_EVENT;
3395 fast_path_evt->un.read_check_error.header.subcategory =
3396 LPFC_EVENT_FCPRDCHKERR;
3397 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
3398 &pnode->nlp_portname, sizeof(struct lpfc_name));
3399 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
3400 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3401 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
3402 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
3403 fast_path_evt->un.read_check_error.fcpiparam =
3404 fcpi_parm;
3405 } else
3406 return;
3407
3408 fast_path_evt->vport = vport;
3409 spin_lock_irqsave(&phba->hbalock, flags);
3410 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
3411 spin_unlock_irqrestore(&phba->hbalock, flags);
3412 lpfc_worker_wake_up(phba);
3413 return;
3414}
James Smart9bad7672008-12-04 22:39:02 -05003415
3416/**
James Smartf1126682009-06-10 17:22:44 -04003417 * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
James Smart3772a992009-05-22 14:50:54 -04003418 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -05003419 * @psb: The scsi buffer which is going to be un-mapped.
3420 *
3421 * This routine does DMA un-mapping of scatter gather list of scsi command
James Smart3772a992009-05-22 14:50:54 -04003422 * field of @lpfc_cmd for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05003423 **/
dea31012005-04-17 16:05:31 -05003424static void
James Smartf1126682009-06-10 17:22:44 -04003425lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James Smartbcf4dbf2006-07-06 15:50:08 -04003426{
3427 /*
3428 * There are only two special cases to consider. (1) the scsi command
3429 * requested scatter-gather usage or (2) the scsi command allocated
3430 * a request buffer, but did not request use_sg. There is a third
3431 * case, but it does not require resource deallocation.
3432 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003433 if (psb->seg_cnt > 0)
3434 scsi_dma_unmap(psb->pCmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003435 if (psb->prot_seg_cnt > 0)
3436 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
3437 scsi_prot_sg_count(psb->pCmd),
3438 psb->pCmd->sc_data_direction);
James Smartbcf4dbf2006-07-06 15:50:08 -04003439}
3440
James Smart9bad7672008-12-04 22:39:02 -05003441/**
James Smart3621a712009-04-06 18:47:14 -04003442 * lpfc_handler_fcp_err - FCP response handler
James Smart9bad7672008-12-04 22:39:02 -05003443 * @vport: The virtual port for which this call is being executed.
3444 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
3445 * @rsp_iocb: The response IOCB which contains FCP error.
3446 *
3447 * This routine is called to process response IOCB with status field
3448 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
3449 * based upon SCSI and FCP error.
3450 **/
James Smartbcf4dbf2006-07-06 15:50:08 -04003451static void
James Smart2e0fef82007-06-17 19:56:36 -05003452lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
3453 struct lpfc_iocbq *rsp_iocb)
dea31012005-04-17 16:05:31 -05003454{
3455 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3456 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
3457 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
James Smart7054a602007-04-25 09:52:34 -04003458 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
dea31012005-04-17 16:05:31 -05003459 uint32_t resp_info = fcprsp->rspStatus2;
3460 uint32_t scsi_status = fcprsp->rspStatus3;
James Smartc7743952006-12-02 13:34:42 -05003461 uint32_t *lp;
dea31012005-04-17 16:05:31 -05003462 uint32_t host_status = DID_OK;
3463 uint32_t rsplen = 0;
James Smartc7743952006-12-02 13:34:42 -05003464 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea31012005-04-17 16:05:31 -05003465
James Smartea2151b2008-09-07 11:52:10 -04003466
dea31012005-04-17 16:05:31 -05003467 /*
3468 * If this is a task management command, there is no
3469 * scsi packet associated with this lpfc_cmd. The driver
3470 * consumes it.
3471 */
3472 if (fcpcmd->fcpCntl2) {
3473 scsi_status = 0;
3474 goto out;
3475 }
3476
James Smart6a9c52c2009-10-02 15:16:51 -04003477 if (resp_info & RSP_LEN_VALID) {
3478 rsplen = be32_to_cpu(fcprsp->rspRspLen);
James Smarte40a02c2010-02-26 14:13:54 -05003479 if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
James Smart6a9c52c2009-10-02 15:16:51 -04003480 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3481 "2719 Invalid response length: "
3482 "tgt x%x lun x%x cmnd x%x rsplen x%x\n",
3483 cmnd->device->id,
3484 cmnd->device->lun, cmnd->cmnd[0],
3485 rsplen);
3486 host_status = DID_ERROR;
3487 goto out;
3488 }
James Smarte40a02c2010-02-26 14:13:54 -05003489 if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
3490 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3491 "2757 Protocol failure detected during "
3492 "processing of FCP I/O op: "
3493 "tgt x%x lun x%x cmnd x%x rspInfo3 x%x\n",
3494 cmnd->device->id,
3495 cmnd->device->lun, cmnd->cmnd[0],
3496 fcprsp->rspInfo3);
3497 host_status = DID_ERROR;
3498 goto out;
3499 }
James Smart6a9c52c2009-10-02 15:16:51 -04003500 }
3501
James Smartc7743952006-12-02 13:34:42 -05003502 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
3503 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
3504 if (snslen > SCSI_SENSE_BUFFERSIZE)
3505 snslen = SCSI_SENSE_BUFFERSIZE;
3506
3507 if (resp_info & RSP_LEN_VALID)
3508 rsplen = be32_to_cpu(fcprsp->rspRspLen);
3509 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
3510 }
3511 lp = (uint32_t *)cmnd->sense_buffer;
3512
James Smartaa1c7ee2012-08-14 14:26:06 -04003513 /* special handling for under run conditions */
3514 if (!scsi_status && (resp_info & RESID_UNDER)) {
3515 /* don't log under runs if fcp set... */
3516 if (vport->cfg_log_verbose & LOG_FCP)
3517 logit = LOG_FCP_ERROR;
3518 /* unless operator says so */
3519 if (vport->cfg_log_verbose & LOG_FCP_UNDER)
3520 logit = LOG_FCP_UNDER;
3521 }
James Smartc7743952006-12-02 13:34:42 -05003522
James Smarte8b62012007-08-02 11:10:09 -04003523 lpfc_printf_vlog(vport, KERN_WARNING, logit,
James Smarte2a0a9d2008-12-04 22:40:02 -05003524 "9024 FCP command x%x failed: x%x SNS x%x x%x "
James Smarte8b62012007-08-02 11:10:09 -04003525 "Data: x%x x%x x%x x%x x%x\n",
3526 cmnd->cmnd[0], scsi_status,
3527 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
3528 be32_to_cpu(fcprsp->rspResId),
3529 be32_to_cpu(fcprsp->rspSnsLen),
3530 be32_to_cpu(fcprsp->rspRspLen),
3531 fcprsp->rspInfo3);
dea31012005-04-17 16:05:31 -05003532
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003533 scsi_set_resid(cmnd, 0);
dea31012005-04-17 16:05:31 -05003534 if (resp_info & RESID_UNDER) {
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003535 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea31012005-04-17 16:05:31 -05003536
James Smart73d91e52011-10-10 21:32:10 -04003537 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_UNDER,
James Smarte2a0a9d2008-12-04 22:40:02 -05003538 "9025 FCP Read Underrun, expected %d, "
James Smarte8b62012007-08-02 11:10:09 -04003539 "residual %d Data: x%x x%x x%x\n",
3540 be32_to_cpu(fcpcmd->fcpDl),
3541 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
3542 cmnd->underflow);
dea31012005-04-17 16:05:31 -05003543
3544 /*
James Smart7054a602007-04-25 09:52:34 -04003545 * If there is an under run check if under run reported by
3546 * storage array is same as the under run reported by HBA.
3547 * If this is not same, there is a dropped frame.
3548 */
3549 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3550 fcpi_parm &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003551 (scsi_get_resid(cmnd) != fcpi_parm)) {
James Smarte8b62012007-08-02 11:10:09 -04003552 lpfc_printf_vlog(vport, KERN_WARNING,
3553 LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05003554 "9026 FCP Read Check Error "
James Smarte8b62012007-08-02 11:10:09 -04003555 "and Underrun Data: x%x x%x x%x x%x\n",
3556 be32_to_cpu(fcpcmd->fcpDl),
3557 scsi_get_resid(cmnd), fcpi_parm,
3558 cmnd->cmnd[0]);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003559 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
James Smart7054a602007-04-25 09:52:34 -04003560 host_status = DID_ERROR;
3561 }
3562 /*
dea31012005-04-17 16:05:31 -05003563 * The cmnd->underflow is the minimum number of bytes that must
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003564 * be transferred for this command. Provided a sense condition
dea31012005-04-17 16:05:31 -05003565 * is not present, make sure the actual amount transferred is at
3566 * least the underflow value or fail.
3567 */
3568 if (!(resp_info & SNS_LEN_VALID) &&
3569 (scsi_status == SAM_STAT_GOOD) &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003570 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
3571 < cmnd->underflow)) {
James Smarte8b62012007-08-02 11:10:09 -04003572 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05003573 "9027 FCP command x%x residual "
James Smarte8b62012007-08-02 11:10:09 -04003574 "underrun converted to error "
3575 "Data: x%x x%x x%x\n",
James Smart66dbfbe2007-08-05 06:08:38 -04003576 cmnd->cmnd[0], scsi_bufflen(cmnd),
James Smarte8b62012007-08-02 11:10:09 -04003577 scsi_get_resid(cmnd), cmnd->underflow);
dea31012005-04-17 16:05:31 -05003578 host_status = DID_ERROR;
3579 }
3580 } else if (resp_info & RESID_OVER) {
James Smarte8b62012007-08-02 11:10:09 -04003581 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05003582 "9028 FCP command x%x residual overrun error. "
James Smarte4e74272009-07-19 10:01:38 -04003583 "Data: x%x x%x\n", cmnd->cmnd[0],
James Smarte8b62012007-08-02 11:10:09 -04003584 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea31012005-04-17 16:05:31 -05003585 host_status = DID_ERROR;
3586
3587 /*
3588 * Check SLI validation that all the transfer was actually done
James Smart582dd792012-08-03 12:34:44 -04003589 * (fcpi_parm should be zero).
dea31012005-04-17 16:05:31 -05003590 */
James Smart582dd792012-08-03 12:34:44 -04003591 } else if (fcpi_parm) {
James Smarte8b62012007-08-02 11:10:09 -04003592 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
James Smart582dd792012-08-03 12:34:44 -04003593 "9029 FCP Data Transfer Check Error: "
James Smarteee88772010-09-29 11:19:08 -04003594 "x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003595 be32_to_cpu(fcpcmd->fcpDl),
3596 be32_to_cpu(fcprsp->rspResId),
James Smarteee88772010-09-29 11:19:08 -04003597 fcpi_parm, cmnd->cmnd[0], scsi_status);
3598 switch (scsi_status) {
3599 case SAM_STAT_GOOD:
3600 case SAM_STAT_CHECK_CONDITION:
3601 /* Fabric dropped a data frame. Fail any successful
3602 * command in which we detected dropped frames.
3603 * A status of good or some check conditions could
3604 * be considered a successful command.
3605 */
3606 host_status = DID_ERROR;
3607 break;
3608 }
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003609 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea31012005-04-17 16:05:31 -05003610 }
3611
3612 out:
3613 cmnd->result = ScsiResult(host_status, scsi_status);
James Smartea2151b2008-09-07 11:52:10 -04003614 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
dea31012005-04-17 16:05:31 -05003615}
3616
James Smart9bad7672008-12-04 22:39:02 -05003617/**
James Smart3621a712009-04-06 18:47:14 -04003618 * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
James Smart9bad7672008-12-04 22:39:02 -05003619 * @phba: The Hba for which this call is being executed.
3620 * @pIocbIn: The command IOCBQ for the scsi cmnd.
James Smart3772a992009-05-22 14:50:54 -04003621 * @pIocbOut: The response IOCBQ for the scsi cmnd.
James Smart9bad7672008-12-04 22:39:02 -05003622 *
3623 * This routine assigns scsi command result by looking into response IOCB
3624 * status field appropriately. This routine handles QUEUE FULL condition as
3625 * well by ramping down device queue depth.
3626 **/
dea31012005-04-17 16:05:31 -05003627static void
3628lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
3629 struct lpfc_iocbq *pIocbOut)
3630{
3631 struct lpfc_scsi_buf *lpfc_cmd =
3632 (struct lpfc_scsi_buf *) pIocbIn->context1;
James Smart2e0fef82007-06-17 19:56:36 -05003633 struct lpfc_vport *vport = pIocbIn->vport;
dea31012005-04-17 16:05:31 -05003634 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
3635 struct lpfc_nodelist *pnode = rdata->pnode;
James Smart75baf692010-06-08 18:31:21 -04003636 struct scsi_cmnd *cmd;
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05003637 int result;
James Smarta257bf92009-04-06 18:48:10 -04003638 struct scsi_device *tmp_sdev;
James Smart5ffc2662009-11-18 15:39:44 -05003639 int depth;
James Smartfa61a542008-01-11 01:52:42 -05003640 unsigned long flags;
James Smartea2151b2008-09-07 11:52:10 -04003641 struct lpfc_fast_path_event *fast_path_evt;
James Smart75baf692010-06-08 18:31:21 -04003642 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04003643 uint32_t queue_depth, scsi_id;
James Smart73d91e52011-10-10 21:32:10 -04003644 uint32_t logit = LOG_FCP;
dea31012005-04-17 16:05:31 -05003645
James Smart75baf692010-06-08 18:31:21 -04003646 /* Sanity check on return of outstanding command */
3647 if (!(lpfc_cmd->pCmd))
3648 return;
3649 cmd = lpfc_cmd->pCmd;
3650 shost = cmd->device->host;
3651
James Smarte3d2b802012-08-14 14:25:43 -04003652 lpfc_cmd->result = (pIocbOut->iocb.un.ulpWord[4] & IOERR_PARAM_MASK);
dea31012005-04-17 16:05:31 -05003653 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
James Smart341af102010-01-26 23:07:37 -05003654 /* pick up SLI4 exhange busy status from HBA */
3655 lpfc_cmd->exch_busy = pIocbOut->iocb_flag & LPFC_EXCHANGE_BUSY;
3656
James Smart9a6b09c2012-03-01 22:37:42 -05003657#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
3658 if (lpfc_cmd->prot_data_type) {
3659 struct scsi_dif_tuple *src = NULL;
3660
3661 src = (struct scsi_dif_tuple *)lpfc_cmd->prot_data_segment;
3662 /*
3663 * Used to restore any changes to protection
3664 * data for error injection.
3665 */
3666 switch (lpfc_cmd->prot_data_type) {
3667 case LPFC_INJERR_REFTAG:
3668 src->ref_tag =
3669 lpfc_cmd->prot_data;
3670 break;
3671 case LPFC_INJERR_APPTAG:
3672 src->app_tag =
3673 (uint16_t)lpfc_cmd->prot_data;
3674 break;
3675 case LPFC_INJERR_GUARD:
3676 src->guard_tag =
3677 (uint16_t)lpfc_cmd->prot_data;
3678 break;
3679 default:
3680 break;
3681 }
3682
3683 lpfc_cmd->prot_data = 0;
3684 lpfc_cmd->prot_data_type = 0;
3685 lpfc_cmd->prot_data_segment = NULL;
3686 }
3687#endif
James Smart109f6ed2008-12-04 22:39:08 -05003688 if (pnode && NLP_CHK_NODE_ACT(pnode))
3689 atomic_dec(&pnode->cmd_pending);
dea31012005-04-17 16:05:31 -05003690
3691 if (lpfc_cmd->status) {
3692 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
3693 (lpfc_cmd->result & IOERR_DRVR_MASK))
3694 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
3695 else if (lpfc_cmd->status >= IOSTAT_CNT)
3696 lpfc_cmd->status = IOSTAT_DEFAULT;
James Smartaa1c7ee2012-08-14 14:26:06 -04003697 if (lpfc_cmd->status == IOSTAT_FCP_RSP_ERROR &&
3698 !lpfc_cmd->fcp_rsp->rspStatus3 &&
3699 (lpfc_cmd->fcp_rsp->rspStatus2 & RESID_UNDER) &&
3700 !(vport->cfg_log_verbose & LOG_FCP_UNDER))
James Smart73d91e52011-10-10 21:32:10 -04003701 logit = 0;
3702 else
3703 logit = LOG_FCP | LOG_FCP_UNDER;
3704 lpfc_printf_vlog(vport, KERN_WARNING, logit,
3705 "9030 FCP cmd x%x failed <%d/%d> "
James Smart5a0d80f2012-05-09 21:18:20 -04003706 "status: x%x result: x%x "
3707 "sid: x%x did: x%x oxid: x%x "
3708 "Data: x%x x%x\n",
James Smart73d91e52011-10-10 21:32:10 -04003709 cmd->cmnd[0],
3710 cmd->device ? cmd->device->id : 0xffff,
3711 cmd->device ? cmd->device->lun : 0xffff,
3712 lpfc_cmd->status, lpfc_cmd->result,
James Smart5a0d80f2012-05-09 21:18:20 -04003713 vport->fc_myDID, pnode->nlp_DID,
3714 phba->sli_rev == LPFC_SLI_REV4 ?
3715 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
James Smart73d91e52011-10-10 21:32:10 -04003716 pIocbOut->iocb.ulpContext,
3717 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
dea31012005-04-17 16:05:31 -05003718
3719 switch (lpfc_cmd->status) {
3720 case IOSTAT_FCP_RSP_ERROR:
3721 /* Call FCP RSP handler to determine result */
James Smart2e0fef82007-06-17 19:56:36 -05003722 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea31012005-04-17 16:05:31 -05003723 break;
3724 case IOSTAT_NPORT_BSY:
3725 case IOSTAT_FABRIC_BSY:
James Smart0f1f53a2008-08-24 21:50:18 -04003726 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
James Smartea2151b2008-09-07 11:52:10 -04003727 fast_path_evt = lpfc_alloc_fast_evt(phba);
3728 if (!fast_path_evt)
3729 break;
3730 fast_path_evt->un.fabric_evt.event_type =
3731 FC_REG_FABRIC_EVENT;
3732 fast_path_evt->un.fabric_evt.subcategory =
3733 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
3734 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
3735 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
3736 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
3737 &pnode->nlp_portname,
3738 sizeof(struct lpfc_name));
3739 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
3740 &pnode->nlp_nodename,
3741 sizeof(struct lpfc_name));
3742 }
3743 fast_path_evt->vport = vport;
3744 fast_path_evt->work_evt.evt =
3745 LPFC_EVT_FASTPATH_MGMT_EVT;
3746 spin_lock_irqsave(&phba->hbalock, flags);
3747 list_add_tail(&fast_path_evt->work_evt.evt_listp,
3748 &phba->work_list);
3749 spin_unlock_irqrestore(&phba->hbalock, flags);
3750 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05003751 break;
James Smart92d7f7b2007-06-17 19:56:38 -05003752 case IOSTAT_LOCAL_REJECT:
James Smart1151e3e2011-02-16 12:39:35 -05003753 case IOSTAT_REMOTE_STOP:
James Smartab56dc22011-02-16 12:39:57 -05003754 if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
3755 lpfc_cmd->result ==
3756 IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
3757 lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
3758 lpfc_cmd->result ==
3759 IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
3760 cmd->result = ScsiResult(DID_NO_CONNECT, 0);
3761 break;
3762 }
James Smartd7c255b2008-08-24 21:50:00 -04003763 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
James Smart92d7f7b2007-06-17 19:56:38 -05003764 lpfc_cmd->result == IOERR_NO_RESOURCES ||
James Smartb92938b2010-06-07 15:24:12 -04003765 lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
3766 lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
James Smart92d7f7b2007-06-17 19:56:38 -05003767 cmd->result = ScsiResult(DID_REQUEUE, 0);
James Smart58da1ff2008-04-07 10:15:56 -04003768 break;
James Smarte2a0a9d2008-12-04 22:40:02 -05003769 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003770 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
3771 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
3772 pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
3773 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
3774 /*
3775 * This is a response for a BG enabled
3776 * cmd. Parse BG error
3777 */
3778 lpfc_parse_bg_err(phba, lpfc_cmd,
3779 pIocbOut);
3780 break;
3781 } else {
3782 lpfc_printf_vlog(vport, KERN_WARNING,
3783 LOG_BG,
3784 "9031 non-zero BGSTAT "
James Smart6a9c52c2009-10-02 15:16:51 -04003785 "on unprotected cmd\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05003786 }
3787 }
James Smart1151e3e2011-02-16 12:39:35 -05003788 if ((lpfc_cmd->status == IOSTAT_REMOTE_STOP)
3789 && (phba->sli_rev == LPFC_SLI_REV4)
3790 && (pnode && NLP_CHK_NODE_ACT(pnode))) {
3791 /* This IO was aborted by the target, we don't
3792 * know the rxid and because we did not send the
3793 * ABTS we cannot generate and RRQ.
3794 */
3795 lpfc_set_rrq_active(phba, pnode,
James Smartee0f4fe2012-05-09 21:19:14 -04003796 lpfc_cmd->cur_iocbq.sli4_lxritag,
3797 0, 0);
James Smart1151e3e2011-02-16 12:39:35 -05003798 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003799 /* else: fall through */
dea31012005-04-17 16:05:31 -05003800 default:
3801 cmd->result = ScsiResult(DID_ERROR, 0);
3802 break;
3803 }
3804
James Smart58da1ff2008-04-07 10:15:56 -04003805 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003806 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
James Smart0f1f53a2008-08-24 21:50:18 -04003807 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
3808 SAM_STAT_BUSY);
James Smartab56dc22011-02-16 12:39:57 -05003809 } else
dea31012005-04-17 16:05:31 -05003810 cmd->result = ScsiResult(DID_OK, 0);
dea31012005-04-17 16:05:31 -05003811
3812 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
3813 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
3814
James Smarte8b62012007-08-02 11:10:09 -04003815 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
3816 "0710 Iodone <%d/%d> cmd %p, error "
3817 "x%x SNS x%x x%x Data: x%x x%x\n",
3818 cmd->device->id, cmd->device->lun, cmd,
3819 cmd->result, *lp, *(lp + 3), cmd->retries,
3820 scsi_get_resid(cmd));
dea31012005-04-17 16:05:31 -05003821 }
3822
James Smartea2151b2008-09-07 11:52:10 -04003823 lpfc_update_stats(phba, lpfc_cmd);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05003824 result = cmd->result;
James Smart977b5a02008-09-07 11:52:04 -04003825 if (vport->cfg_max_scsicmpl_time &&
3826 time_after(jiffies, lpfc_cmd->start_time +
3827 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
James Smarta257bf92009-04-06 18:48:10 -04003828 spin_lock_irqsave(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05003829 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
3830 if (pnode->cmd_qdepth >
3831 atomic_read(&pnode->cmd_pending) &&
3832 (atomic_read(&pnode->cmd_pending) >
3833 LPFC_MIN_TGT_QDEPTH) &&
3834 ((cmd->cmnd[0] == READ_10) ||
3835 (cmd->cmnd[0] == WRITE_10)))
3836 pnode->cmd_qdepth =
3837 atomic_read(&pnode->cmd_pending);
James Smart977b5a02008-09-07 11:52:04 -04003838
James Smart109f6ed2008-12-04 22:39:08 -05003839 pnode->last_change_time = jiffies;
3840 }
James Smarta257bf92009-04-06 18:48:10 -04003841 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05003842 } else if (pnode && NLP_CHK_NODE_ACT(pnode)) {
James Smart7dc517d2010-07-14 15:32:10 -04003843 if ((pnode->cmd_qdepth < vport->cfg_tgt_queue_depth) &&
James Smart977b5a02008-09-07 11:52:04 -04003844 time_after(jiffies, pnode->last_change_time +
James Smart109f6ed2008-12-04 22:39:08 -05003845 msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
James Smarta257bf92009-04-06 18:48:10 -04003846 spin_lock_irqsave(shost->host_lock, flags);
James Smart7dc517d2010-07-14 15:32:10 -04003847 depth = pnode->cmd_qdepth * LPFC_TGTQ_RAMPUP_PCENT
3848 / 100;
3849 depth = depth ? depth : 1;
3850 pnode->cmd_qdepth += depth;
3851 if (pnode->cmd_qdepth > vport->cfg_tgt_queue_depth)
3852 pnode->cmd_qdepth = vport->cfg_tgt_queue_depth;
James Smart109f6ed2008-12-04 22:39:08 -05003853 pnode->last_change_time = jiffies;
James Smarta257bf92009-04-06 18:48:10 -04003854 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05003855 }
James Smart977b5a02008-09-07 11:52:04 -04003856 }
3857
James Smart1dcb58e2007-04-25 09:51:30 -04003858 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James Smarta257bf92009-04-06 18:48:10 -04003859
3860 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
3861 queue_depth = cmd->device->queue_depth;
3862 scsi_id = cmd->device->id;
dea31012005-04-17 16:05:31 -05003863 cmd->scsi_done(cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003864
Jamie Wellnitzb8086082006-02-28 22:33:12 -05003865 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart876dd7d2012-09-29 11:31:28 -04003866 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92e3af62012-08-14 14:26:28 -04003867 lpfc_cmd->pCmd = NULL;
James Smart876dd7d2012-09-29 11:31:28 -04003868 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart92e3af62012-08-14 14:26:28 -04003869
James Smartfa61a542008-01-11 01:52:42 -05003870 /*
3871 * If there is a thread waiting for command completion
3872 * wake up the thread.
3873 */
James Smarta257bf92009-04-06 18:48:10 -04003874 spin_lock_irqsave(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05003875 if (lpfc_cmd->waitq)
3876 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04003877 spin_unlock_irqrestore(shost->host_lock, flags);
Jamie Wellnitzb8086082006-02-28 22:33:12 -05003878 lpfc_release_scsi_buf(phba, lpfc_cmd);
3879 return;
3880 }
3881
James Smart92d7f7b2007-06-17 19:56:38 -05003882 if (!result)
James Smarta257bf92009-04-06 18:48:10 -04003883 lpfc_rampup_queue_depth(vport, queue_depth);
James Smart92d7f7b2007-06-17 19:56:38 -05003884
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05003885 /*
3886 * Check for queue full. If the lun is reporting queue full, then
3887 * back off the lun queue depth to prevent target overloads.
3888 */
James Smart58da1ff2008-04-07 10:15:56 -04003889 if (result == SAM_STAT_TASK_SET_FULL && pnode &&
3890 NLP_CHK_NODE_ACT(pnode)) {
James Smarta257bf92009-04-06 18:48:10 -04003891 shost_for_each_device(tmp_sdev, shost) {
3892 if (tmp_sdev->id != scsi_id)
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05003893 continue;
3894 depth = scsi_track_queue_full(tmp_sdev,
James Smart5ffc2662009-11-18 15:39:44 -05003895 tmp_sdev->queue_depth-1);
3896 if (depth <= 0)
3897 continue;
James Smarte8b62012007-08-02 11:10:09 -04003898 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3899 "0711 detected queue full - lun queue "
3900 "depth adjusted to %d.\n", depth);
James Smartea2151b2008-09-07 11:52:10 -04003901 lpfc_send_sdev_queuedepth_change_event(phba, vport,
James Smart5ffc2662009-11-18 15:39:44 -05003902 pnode,
3903 tmp_sdev->lun,
3904 depth+1, depth);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05003905 }
3906 }
3907
James Smart876dd7d2012-09-29 11:31:28 -04003908 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92e3af62012-08-14 14:26:28 -04003909 lpfc_cmd->pCmd = NULL;
James Smart876dd7d2012-09-29 11:31:28 -04003910 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart92e3af62012-08-14 14:26:28 -04003911
James Smartfa61a542008-01-11 01:52:42 -05003912 /*
3913 * If there is a thread waiting for command completion
3914 * wake up the thread.
3915 */
James Smarta257bf92009-04-06 18:48:10 -04003916 spin_lock_irqsave(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05003917 if (lpfc_cmd->waitq)
3918 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04003919 spin_unlock_irqrestore(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05003920
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003921 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05003922}
3923
James Smart34b02dc2008-08-24 21:49:55 -04003924/**
James Smart3621a712009-04-06 18:47:14 -04003925 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
James Smart34b02dc2008-08-24 21:49:55 -04003926 * @data: A pointer to the immediate command data portion of the IOCB.
3927 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
3928 *
3929 * The routine copies the entire FCP command from @fcp_cmnd to @data while
3930 * byte swapping the data to big endian format for transmission on the wire.
3931 **/
3932static void
3933lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
3934{
3935 int i, j;
3936 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
3937 i += sizeof(uint32_t), j++) {
3938 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
3939 }
3940}
3941
James Smart9bad7672008-12-04 22:39:02 -05003942/**
James Smartf1126682009-06-10 17:22:44 -04003943 * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05003944 * @vport: The virtual port for which this call is being executed.
3945 * @lpfc_cmd: The scsi command which needs to send.
3946 * @pnode: Pointer to lpfc_nodelist.
3947 *
3948 * This routine initializes fcp_cmnd and iocb data structure from scsi command
James Smart3772a992009-05-22 14:50:54 -04003949 * to transfer for device with SLI3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05003950 **/
dea31012005-04-17 16:05:31 -05003951static void
James Smartf1126682009-06-10 17:22:44 -04003952lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
James Smart2e0fef82007-06-17 19:56:36 -05003953 struct lpfc_nodelist *pnode)
dea31012005-04-17 16:05:31 -05003954{
James Smart2e0fef82007-06-17 19:56:36 -05003955 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003956 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3957 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3958 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
3959 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
3960 int datadir = scsi_cmnd->sc_data_direction;
James Smart7e2b19f2007-10-29 11:00:39 -04003961 char tag[2];
James Smart027140e2012-08-03 12:35:44 -04003962 uint8_t *ptr;
3963 bool sli4;
dea31012005-04-17 16:05:31 -05003964
James Smart58da1ff2008-04-07 10:15:56 -04003965 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3966 return;
3967
dea31012005-04-17 16:05:31 -05003968 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
James.Smart@Emulex.Com69859dc2005-08-10 15:02:37 -04003969 /* clear task management bits */
3970 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea31012005-04-17 16:05:31 -05003971
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -04003972 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
3973 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05003974
James Smart027140e2012-08-03 12:35:44 -04003975 ptr = &fcp_cmnd->fcpCdb[0];
3976 memcpy(ptr, scsi_cmnd->cmnd, scsi_cmnd->cmd_len);
3977 if (scsi_cmnd->cmd_len < LPFC_FCP_CDB_LEN) {
3978 ptr += scsi_cmnd->cmd_len;
3979 memset(ptr, 0, (LPFC_FCP_CDB_LEN - scsi_cmnd->cmd_len));
3980 }
3981
James Smart7e2b19f2007-10-29 11:00:39 -04003982 if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
3983 switch (tag[0]) {
dea31012005-04-17 16:05:31 -05003984 case HEAD_OF_QUEUE_TAG:
3985 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
3986 break;
3987 case ORDERED_QUEUE_TAG:
3988 fcp_cmnd->fcpCntl1 = ORDERED_Q;
3989 break;
3990 default:
3991 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
3992 break;
3993 }
3994 } else
James Smartfe8f7f92013-01-03 15:43:03 -05003995 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
dea31012005-04-17 16:05:31 -05003996
James Smart027140e2012-08-03 12:35:44 -04003997 sli4 = (phba->sli_rev == LPFC_SLI_REV4);
3998
dea31012005-04-17 16:05:31 -05003999 /*
4000 * There are three possibilities here - use scatter-gather segment, use
4001 * the single mapping, or neither. Start the lpfc command prep by
4002 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
4003 * data bde entry.
4004 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05004005 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05004006 if (datadir == DMA_TO_DEVICE) {
4007 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
James Smart027140e2012-08-03 12:35:44 -04004008 if (sli4)
4009 iocb_cmd->ulpPU = PARM_READ_CHECK;
4010 else {
James Smart3772a992009-05-22 14:50:54 -04004011 iocb_cmd->un.fcpi.fcpi_parm = 0;
4012 iocb_cmd->ulpPU = 0;
James Smart027140e2012-08-03 12:35:44 -04004013 }
dea31012005-04-17 16:05:31 -05004014 fcp_cmnd->fcpCntl3 = WRITE_DATA;
4015 phba->fc4OutputRequests++;
4016 } else {
4017 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
4018 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05004019 fcp_cmnd->fcpCntl3 = READ_DATA;
4020 phba->fc4InputRequests++;
4021 }
4022 } else {
4023 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
4024 iocb_cmd->un.fcpi.fcpi_parm = 0;
4025 iocb_cmd->ulpPU = 0;
4026 fcp_cmnd->fcpCntl3 = 0;
4027 phba->fc4ControlRequests++;
4028 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004029 if (phba->sli_rev == 3 &&
4030 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04004031 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05004032 /*
4033 * Finish initializing those IOCB fields that are independent
4034 * of the scsi_cmnd request_buffer
4035 */
4036 piocbq->iocb.ulpContext = pnode->nlp_rpi;
James Smart027140e2012-08-03 12:35:44 -04004037 if (sli4)
James Smart6d368e52011-05-24 11:44:12 -04004038 piocbq->iocb.ulpContext =
4039 phba->sli4_hba.rpi_ids[pnode->nlp_rpi];
dea31012005-04-17 16:05:31 -05004040 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
4041 piocbq->iocb.ulpFCP2Rcvy = 1;
James Smart09372822008-01-11 01:52:54 -05004042 else
4043 piocbq->iocb.ulpFCP2Rcvy = 0;
dea31012005-04-17 16:05:31 -05004044
4045 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
4046 piocbq->context1 = lpfc_cmd;
4047 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
4048 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
James Smart2e0fef82007-06-17 19:56:36 -05004049 piocbq->vport = vport;
dea31012005-04-17 16:05:31 -05004050}
4051
James Smart9bad7672008-12-04 22:39:02 -05004052/**
James Smart6d368e52011-05-24 11:44:12 -04004053 * lpfc_scsi_prep_task_mgmt_cmd - Convert SLI3 scsi TM cmd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05004054 * @vport: The virtual port for which this call is being executed.
4055 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
4056 * @lun: Logical unit number.
4057 * @task_mgmt_cmd: SCSI task management command.
4058 *
James Smart3772a992009-05-22 14:50:54 -04004059 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
4060 * for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05004061 *
4062 * Return codes:
4063 * 0 - Error
4064 * 1 - Success
4065 **/
dea31012005-04-17 16:05:31 -05004066static int
James Smartf1126682009-06-10 17:22:44 -04004067lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
dea31012005-04-17 16:05:31 -05004068 struct lpfc_scsi_buf *lpfc_cmd,
James Smart420b630d2006-07-06 15:50:16 -04004069 unsigned int lun,
dea31012005-04-17 16:05:31 -05004070 uint8_t task_mgmt_cmd)
4071{
dea31012005-04-17 16:05:31 -05004072 struct lpfc_iocbq *piocbq;
4073 IOCB_t *piocb;
4074 struct fcp_cmnd *fcp_cmnd;
James Smart0b18ac42006-05-01 21:50:40 -04004075 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea31012005-04-17 16:05:31 -05004076 struct lpfc_nodelist *ndlp = rdata->pnode;
4077
James Smart58da1ff2008-04-07 10:15:56 -04004078 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
4079 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05004080 return 0;
dea31012005-04-17 16:05:31 -05004081
dea31012005-04-17 16:05:31 -05004082 piocbq = &(lpfc_cmd->cur_iocbq);
James Smart2e0fef82007-06-17 19:56:36 -05004083 piocbq->vport = vport;
4084
dea31012005-04-17 16:05:31 -05004085 piocb = &piocbq->iocb;
4086
4087 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart34b02dc2008-08-24 21:49:55 -04004088 /* Clear out any old data in the FCP command area */
4089 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
4090 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05004091 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
James Smarte2a0a9d2008-12-04 22:40:02 -05004092 if (vport->phba->sli_rev == 3 &&
4093 !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04004094 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05004095 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
dea31012005-04-17 16:05:31 -05004096 piocb->ulpContext = ndlp->nlp_rpi;
James Smart6d368e52011-05-24 11:44:12 -04004097 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
4098 piocb->ulpContext =
4099 vport->phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
4100 }
dea31012005-04-17 16:05:31 -05004101 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
4102 piocb->ulpFCP2Rcvy = 1;
4103 }
4104 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
4105
4106 /* ulpTimeout is only one byte */
4107 if (lpfc_cmd->timeout > 0xff) {
4108 /*
4109 * Do not timeout the command at the firmware level.
4110 * The driver will provide the timeout mechanism.
4111 */
4112 piocb->ulpTimeout = 0;
James Smartf1126682009-06-10 17:22:44 -04004113 } else
dea31012005-04-17 16:05:31 -05004114 piocb->ulpTimeout = lpfc_cmd->timeout;
James Smartf1126682009-06-10 17:22:44 -04004115
4116 if (vport->phba->sli_rev == LPFC_SLI_REV4)
4117 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004118
James Smart2e0fef82007-06-17 19:56:36 -05004119 return 1;
dea31012005-04-17 16:05:31 -05004120}
4121
James Smart9bad7672008-12-04 22:39:02 -05004122/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004123 * lpfc_scsi_api_table_setup - Set up scsi api function jump table
James Smart3772a992009-05-22 14:50:54 -04004124 * @phba: The hba struct for which this call is being executed.
4125 * @dev_grp: The HBA PCI-Device group number.
4126 *
4127 * This routine sets up the SCSI interface API function jump table in @phba
4128 * struct.
4129 * Returns: 0 - success, -ENODEV - failure.
4130 **/
4131int
4132lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4133{
4134
James Smartf1126682009-06-10 17:22:44 -04004135 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
4136 phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
James Smartf1126682009-06-10 17:22:44 -04004137
James Smart3772a992009-05-22 14:50:54 -04004138 switch (dev_grp) {
4139 case LPFC_PCI_DEV_LP:
4140 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s3;
4141 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
James Smartacd68592012-01-18 16:25:09 -05004142 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04004143 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
James Smart19ca7602010-11-20 23:11:55 -05004144 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04004145 break;
James Smartda0436e2009-05-22 14:51:39 -04004146 case LPFC_PCI_DEV_OC:
4147 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s4;
4148 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
James Smartacd68592012-01-18 16:25:09 -05004149 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04004150 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
James Smart19ca7602010-11-20 23:11:55 -05004151 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04004152 break;
James Smart3772a992009-05-22 14:50:54 -04004153 default:
4154 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4155 "1418 Invalid HBA PCI-device group: 0x%x\n",
4156 dev_grp);
4157 return -ENODEV;
4158 break;
4159 }
James Smart3772a992009-05-22 14:50:54 -04004160 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
James Smart84d1b002010-02-12 14:42:33 -05004161 phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
James Smart3772a992009-05-22 14:50:54 -04004162 return 0;
4163}
4164
4165/**
James Smart3621a712009-04-06 18:47:14 -04004166 * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
James Smart9bad7672008-12-04 22:39:02 -05004167 * @phba: The Hba for which this call is being executed.
4168 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
4169 * @rspiocbq: Pointer to lpfc_iocbq data structure.
4170 *
4171 * This routine is IOCB completion routine for device reset and target reset
4172 * routine. This routine release scsi buffer associated with lpfc_cmd.
4173 **/
James Smart7054a602007-04-25 09:52:34 -04004174static void
4175lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
4176 struct lpfc_iocbq *cmdiocbq,
4177 struct lpfc_iocbq *rspiocbq)
4178{
4179 struct lpfc_scsi_buf *lpfc_cmd =
4180 (struct lpfc_scsi_buf *) cmdiocbq->context1;
4181 if (lpfc_cmd)
4182 lpfc_release_scsi_buf(phba, lpfc_cmd);
4183 return;
4184}
4185
James Smart9bad7672008-12-04 22:39:02 -05004186/**
James Smart3621a712009-04-06 18:47:14 -04004187 * lpfc_info - Info entry point of scsi_host_template data structure
James Smart9bad7672008-12-04 22:39:02 -05004188 * @host: The scsi host for which this call is being executed.
4189 *
4190 * This routine provides module information about hba.
4191 *
4192 * Reutrn code:
4193 * Pointer to char - Success.
4194 **/
dea31012005-04-17 16:05:31 -05004195const char *
4196lpfc_info(struct Scsi_Host *host)
4197{
James Smart2e0fef82007-06-17 19:56:36 -05004198 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
4199 struct lpfc_hba *phba = vport->phba;
James Smart8b68cd52012-09-29 11:32:37 -04004200 int len, link_speed = 0;
dea31012005-04-17 16:05:31 -05004201 static char lpfcinfobuf[384];
4202
4203 memset(lpfcinfobuf,0,384);
4204 if (phba && phba->pcidev){
4205 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
4206 len = strlen(lpfcinfobuf);
4207 snprintf(lpfcinfobuf + len,
4208 384-len,
4209 " on PCI bus %02x device %02x irq %d",
4210 phba->pcidev->bus->number,
4211 phba->pcidev->devfn,
4212 phba->pcidev->irq);
4213 len = strlen(lpfcinfobuf);
4214 if (phba->Port[0]) {
4215 snprintf(lpfcinfobuf + len,
4216 384-len,
4217 " port %s",
4218 phba->Port);
4219 }
James Smart65467b62010-01-26 23:08:29 -05004220 len = strlen(lpfcinfobuf);
James Smart8b68cd52012-09-29 11:32:37 -04004221 if (phba->sli_rev <= LPFC_SLI_REV3) {
4222 link_speed = lpfc_sli_port_speed_get(phba);
4223 } else {
4224 if (phba->sli4_hba.link_state.logical_speed)
4225 link_speed =
4226 phba->sli4_hba.link_state.logical_speed;
4227 else
4228 link_speed = phba->sli4_hba.link_state.speed;
James Smart65467b62010-01-26 23:08:29 -05004229 }
James Smart8b68cd52012-09-29 11:32:37 -04004230 if (link_speed != 0)
4231 snprintf(lpfcinfobuf + len, 384-len,
4232 " Logical Link Speed: %d Mbps", link_speed);
dea31012005-04-17 16:05:31 -05004233 }
4234 return lpfcinfobuf;
4235}
4236
James Smart9bad7672008-12-04 22:39:02 -05004237/**
James Smart3621a712009-04-06 18:47:14 -04004238 * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
James Smart9bad7672008-12-04 22:39:02 -05004239 * @phba: The Hba for which this call is being executed.
4240 *
4241 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
4242 * The default value of cfg_poll_tmo is 10 milliseconds.
4243 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004244static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
4245{
4246 unsigned long poll_tmo_expires =
4247 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
4248
James Smart0e9bb8d2013-03-01 16:35:12 -05004249 if (!list_empty(&phba->sli.ring[LPFC_FCP_RING].txcmplq))
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004250 mod_timer(&phba->fcp_poll_timer,
4251 poll_tmo_expires);
4252}
4253
James Smart9bad7672008-12-04 22:39:02 -05004254/**
James Smart3621a712009-04-06 18:47:14 -04004255 * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
James Smart9bad7672008-12-04 22:39:02 -05004256 * @phba: The Hba for which this call is being executed.
4257 *
4258 * This routine starts the fcp_poll_timer of @phba.
4259 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004260void lpfc_poll_start_timer(struct lpfc_hba * phba)
4261{
4262 lpfc_poll_rearm_timer(phba);
4263}
4264
James Smart9bad7672008-12-04 22:39:02 -05004265/**
James Smart3621a712009-04-06 18:47:14 -04004266 * lpfc_poll_timeout - Restart polling timer
James Smart9bad7672008-12-04 22:39:02 -05004267 * @ptr: Map to lpfc_hba data structure pointer.
4268 *
4269 * This routine restarts fcp_poll timer, when FCP ring polling is enable
4270 * and FCP Ring interrupt is disable.
4271 **/
4272
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004273void lpfc_poll_timeout(unsigned long ptr)
4274{
James Smart2e0fef82007-06-17 19:56:36 -05004275 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004276
4277 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004278 lpfc_sli_handle_fast_ring_event(phba,
4279 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4280
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004281 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4282 lpfc_poll_rearm_timer(phba);
4283 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004284}
4285
James Smart9bad7672008-12-04 22:39:02 -05004286/**
James Smart3621a712009-04-06 18:47:14 -04004287 * lpfc_queuecommand - scsi_host_template queuecommand entry point
James Smart9bad7672008-12-04 22:39:02 -05004288 * @cmnd: Pointer to scsi_cmnd data structure.
4289 * @done: Pointer to done routine.
4290 *
4291 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
4292 * This routine prepares an IOCB from scsi command and provides to firmware.
4293 * The @done callback is invoked after driver finished processing the command.
4294 *
4295 * Return value :
4296 * 0 - Success
4297 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
4298 **/
dea31012005-04-17 16:05:31 -05004299static int
James Smartb9a7c632012-08-03 12:35:24 -04004300lpfc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004301{
James Smart2e0fef82007-06-17 19:56:36 -05004302 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4303 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004304 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05004305 struct lpfc_nodelist *ndlp;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004306 struct lpfc_scsi_buf *lpfc_cmd;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004307 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004308 int err;
dea31012005-04-17 16:05:31 -05004309
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004310 err = fc_remote_port_chkready(rport);
4311 if (err) {
4312 cmnd->result = err;
dea31012005-04-17 16:05:31 -05004313 goto out_fail_command;
4314 }
James Smart1c6f4ef52009-11-18 15:40:49 -05004315 ndlp = rdata->pnode;
dea31012005-04-17 16:05:31 -05004316
James Smartbf086112011-08-21 21:48:13 -04004317 if ((scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) &&
James Smartacd68592012-01-18 16:25:09 -05004318 (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED))) {
James Smarte2a0a9d2008-12-04 22:40:02 -05004319
James Smart6a9c52c2009-10-02 15:16:51 -04004320 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4321 "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
4322 " op:%02x str=%s without registering for"
4323 " BlockGuard - Rejecting command\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05004324 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
4325 dif_op_str[scsi_get_prot_op(cmnd)]);
4326 goto out_fail_command;
4327 }
4328
dea31012005-04-17 16:05:31 -05004329 /*
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004330 * Catch race where our node has transitioned, but the
4331 * transport is still transitioning.
dea31012005-04-17 16:05:31 -05004332 */
James Smart6b415f52012-06-12 13:54:59 -04004333 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
4334 goto out_tgt_busy;
James Smart7dc517d2010-07-14 15:32:10 -04004335 if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth)
Mike Christie34963432011-02-25 14:04:28 -06004336 goto out_tgt_busy;
Mike Christiea93ce022008-08-17 15:24:41 -05004337
James Smart19ca7602010-11-20 23:11:55 -05004338 lpfc_cmd = lpfc_get_scsi_buf(phba, ndlp);
dea31012005-04-17 16:05:31 -05004339 if (lpfc_cmd == NULL) {
James Smarteaf15d52008-12-04 22:39:29 -05004340 lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05004341
James Smarte8b62012007-08-02 11:10:09 -04004342 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4343 "0707 driver's buffer pool is empty, "
4344 "IO busied\n");
dea31012005-04-17 16:05:31 -05004345 goto out_host_busy;
4346 }
4347
4348 /*
4349 * Store the midlayer's command structure for the completion phase
4350 * and complete the command initialization.
4351 */
4352 lpfc_cmd->pCmd = cmnd;
4353 lpfc_cmd->rdata = rdata;
4354 lpfc_cmd->timeout = 0;
James Smart977b5a02008-09-07 11:52:04 -04004355 lpfc_cmd->start_time = jiffies;
dea31012005-04-17 16:05:31 -05004356 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
dea31012005-04-17 16:05:31 -05004357
James Smarte2a0a9d2008-12-04 22:40:02 -05004358 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
James Smart6a9c52c2009-10-02 15:16:51 -04004359 if (vport->phba->cfg_enable_bg) {
James Smart26134702012-08-14 14:25:50 -04004360 lpfc_printf_vlog(vport, KERN_INFO, LOG_BG,
4361 "9033 BLKGRD: rcvd %s cmd:x%x "
4362 "sector x%llx cnt %u pt %x\n",
4363 dif_op_str[scsi_get_prot_op(cmnd)],
4364 cmnd->cmnd[0],
4365 (unsigned long long)scsi_get_lba(cmnd),
4366 blk_rq_sectors(cmnd->request),
4367 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004368 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004369 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
4370 } else {
James Smart6a9c52c2009-10-02 15:16:51 -04004371 if (vport->phba->cfg_enable_bg) {
James Smart26134702012-08-14 14:25:50 -04004372 lpfc_printf_vlog(vport, KERN_INFO, LOG_BG,
4373 "9038 BLKGRD: rcvd PROT_NORMAL cmd: "
4374 "x%x sector x%llx cnt %u pt %x\n",
4375 cmnd->cmnd[0],
4376 (unsigned long long)scsi_get_lba(cmnd),
James Smart9a6b09c2012-03-01 22:37:42 -05004377 blk_rq_sectors(cmnd->request),
James Smart26134702012-08-14 14:25:50 -04004378 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004379 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004380 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
4381 }
4382
dea31012005-04-17 16:05:31 -05004383 if (err)
4384 goto out_host_busy_free_buf;
4385
James Smart2e0fef82007-06-17 19:56:36 -05004386 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea31012005-04-17 16:05:31 -05004387
James Smart977b5a02008-09-07 11:52:04 -04004388 atomic_inc(&ndlp->cmd_pending);
James Smart3772a992009-05-22 14:50:54 -04004389 err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
James Smart92d7f7b2007-06-17 19:56:38 -05004390 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
James Smarteaf15d52008-12-04 22:39:29 -05004391 if (err) {
4392 atomic_dec(&ndlp->cmd_pending);
dea31012005-04-17 16:05:31 -05004393 goto out_host_busy_free_buf;
James Smarteaf15d52008-12-04 22:39:29 -05004394 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004395 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004396 lpfc_sli_handle_fast_ring_event(phba,
4397 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4398
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004399 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4400 lpfc_poll_rearm_timer(phba);
4401 }
4402
dea31012005-04-17 16:05:31 -05004403 return 0;
4404
4405 out_host_busy_free_buf:
James Smartbcf4dbf2006-07-06 15:50:08 -04004406 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004407 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004408 out_host_busy:
4409 return SCSI_MLQUEUE_HOST_BUSY;
4410
Mike Christie34963432011-02-25 14:04:28 -06004411 out_tgt_busy:
4412 return SCSI_MLQUEUE_TARGET_BUSY;
4413
dea31012005-04-17 16:05:31 -05004414 out_fail_command:
James Smartb9a7c632012-08-03 12:35:24 -04004415 cmnd->scsi_done(cmnd);
dea31012005-04-17 16:05:31 -05004416 return 0;
4417}
4418
Jeff Garzikf2812332010-11-16 02:10:29 -05004419
James Smart9bad7672008-12-04 22:39:02 -05004420/**
James Smart3621a712009-04-06 18:47:14 -04004421 * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05004422 * @cmnd: Pointer to scsi_cmnd data structure.
4423 *
4424 * This routine aborts @cmnd pending in base driver.
4425 *
4426 * Return code :
4427 * 0x2003 - Error
4428 * 0x2002 - Success
4429 **/
dea31012005-04-17 16:05:31 -05004430static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05004431lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004432{
James Smart2e0fef82007-06-17 19:56:36 -05004433 struct Scsi_Host *shost = cmnd->device->host;
4434 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4435 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004436 struct lpfc_iocbq *iocb;
4437 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05004438 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05004439 IOCB_t *cmd, *icmd;
James Smart3a707302012-06-12 13:54:42 -04004440 int ret = SUCCESS, status = 0;
James Smart876dd7d2012-09-29 11:31:28 -04004441 unsigned long flags;
James Smartfa61a542008-01-11 01:52:42 -05004442 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004443
James Smart3a707302012-06-12 13:54:42 -04004444 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04004445 if (status != 0 && status != SUCCESS)
James Smart3a707302012-06-12 13:54:42 -04004446 return status;
James Smart4f2e66c2012-05-09 21:17:07 -04004447
James Smart876dd7d2012-09-29 11:31:28 -04004448 spin_lock_irqsave(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004449 /* driver queued commands are in process of being flushed */
4450 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
James Smart876dd7d2012-09-29 11:31:28 -04004451 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004452 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4453 "3168 SCSI Layer abort requested I/O has been "
4454 "flushed by LLD.\n");
4455 return FAILED;
4456 }
4457
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004458 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
James Smart92e3af62012-08-14 14:26:28 -04004459 if (!lpfc_cmd || !lpfc_cmd->pCmd) {
James Smart876dd7d2012-09-29 11:31:28 -04004460 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarteee88772010-09-29 11:19:08 -04004461 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4462 "2873 SCSI Layer I/O Abort Request IO CMPL Status "
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04004463 "x%x ID %d LUN %d\n",
James Smart3a707302012-06-12 13:54:42 -04004464 SUCCESS, cmnd->device->id, cmnd->device->lun);
James Smarteee88772010-09-29 11:19:08 -04004465 return SUCCESS;
4466 }
dea31012005-04-17 16:05:31 -05004467
James Smart4f2e66c2012-05-09 21:17:07 -04004468 iocb = &lpfc_cmd->cur_iocbq;
4469 /* the command is in process of being cancelled */
4470 if (!(iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
James Smart876dd7d2012-09-29 11:31:28 -04004471 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004472 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4473 "3169 SCSI Layer abort requested I/O has been "
4474 "cancelled by LLD.\n");
4475 return FAILED;
4476 }
dea31012005-04-17 16:05:31 -05004477 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004478 * If pCmd field of the corresponding lpfc_scsi_buf structure
4479 * points to a different SCSI command, then the driver has
4480 * already completed this command, but the midlayer did not
James Smart4f2e66c2012-05-09 21:17:07 -04004481 * see the completion before the eh fired. Just return SUCCESS.
dea31012005-04-17 16:05:31 -05004482 */
James Smart4f2e66c2012-05-09 21:17:07 -04004483 if (lpfc_cmd->pCmd != cmnd) {
4484 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4485 "3170 SCSI Layer abort requested I/O has been "
4486 "completed by LLD.\n");
4487 goto out_unlock;
4488 }
dea31012005-04-17 16:05:31 -05004489
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004490 BUG_ON(iocb->context1 != lpfc_cmd);
dea31012005-04-17 16:05:31 -05004491
James Smart4f2e66c2012-05-09 21:17:07 -04004492 abtsiocb = __lpfc_sli_get_iocbq(phba);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004493 if (abtsiocb == NULL) {
4494 ret = FAILED;
James Smart4f2e66c2012-05-09 21:17:07 -04004495 goto out_unlock;
dea31012005-04-17 16:05:31 -05004496 }
4497
dea31012005-04-17 16:05:31 -05004498 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004499 * The scsi command can not be in txq and it is in flight because the
4500 * pCmd is still pointig at the SCSI command we have to abort. There
4501 * is no need to search the txcmplq. Just send an abort to the FW.
dea31012005-04-17 16:05:31 -05004502 */
dea31012005-04-17 16:05:31 -05004503
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004504 cmd = &iocb->iocb;
4505 icmd = &abtsiocb->iocb;
4506 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
4507 icmd->un.acxri.abortContextTag = cmd->ulpContext;
James Smart3772a992009-05-22 14:50:54 -04004508 if (phba->sli_rev == LPFC_SLI_REV4)
4509 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
4510 else
4511 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05004512
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004513 icmd->ulpLe = 1;
4514 icmd->ulpClass = cmd->ulpClass;
James Smart5ffc2662009-11-18 15:39:44 -05004515
4516 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
4517 abtsiocb->fcp_wqidx = iocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05004518 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05004519
James Smart2e0fef82007-06-17 19:56:36 -05004520 if (lpfc_is_link_up(phba))
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004521 icmd->ulpCommand = CMD_ABORT_XRI_CN;
4522 else
4523 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea31012005-04-17 16:05:31 -05004524
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004525 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smart2e0fef82007-06-17 19:56:36 -05004526 abtsiocb->vport = vport;
James Smart4f2e66c2012-05-09 21:17:07 -04004527 /* no longer need the lock after this point */
James Smart876dd7d2012-09-29 11:31:28 -04004528 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004529
James Smart3772a992009-05-22 14:50:54 -04004530 if (lpfc_sli_issue_iocb(phba, LPFC_FCP_RING, abtsiocb, 0) ==
4531 IOCB_ERROR) {
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004532 lpfc_sli_release_iocbq(phba, abtsiocb);
4533 ret = FAILED;
4534 goto out;
4535 }
4536
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004537 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
James Smart45ed1192009-10-02 15:17:02 -04004538 lpfc_sli_handle_fast_ring_event(phba,
4539 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004540
James Smartfa61a542008-01-11 01:52:42 -05004541 lpfc_cmd->waitq = &waitq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004542 /* Wait for abort to complete */
James Smartfa61a542008-01-11 01:52:42 -05004543 wait_event_timeout(waitq,
4544 (lpfc_cmd->pCmd != cmnd),
4545 (2*vport->cfg_devloss_tmo*HZ));
James Smartfa61a542008-01-11 01:52:42 -05004546 lpfc_cmd->waitq = NULL;
dea31012005-04-17 16:05:31 -05004547
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004548 if (lpfc_cmd->pCmd == cmnd) {
4549 ret = FAILED;
James Smarte8b62012007-08-02 11:10:09 -04004550 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4551 "0748 abort handler timed out waiting "
James Smart247ca942012-08-14 14:26:13 -04004552 "for abortng I/O (xri:x%x) to complete: "
4553 "ret %#x, ID %d, LUN %d\n",
4554 iocb->sli4_xritag, ret,
4555 cmnd->device->id, cmnd->device->lun);
dea31012005-04-17 16:05:31 -05004556 }
James Smart4f2e66c2012-05-09 21:17:07 -04004557 goto out;
dea31012005-04-17 16:05:31 -05004558
James Smart4f2e66c2012-05-09 21:17:07 -04004559out_unlock:
James Smart876dd7d2012-09-29 11:31:28 -04004560 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004561out:
James Smarte8b62012007-08-02 11:10:09 -04004562 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4563 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04004564 "LUN %d\n", ret, cmnd->device->id,
4565 cmnd->device->lun);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004566 return ret;
dea31012005-04-17 16:05:31 -05004567}
4568
James Smartbbb9d182009-06-10 17:23:16 -04004569static char *
4570lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
4571{
4572 switch (task_mgmt_cmd) {
4573 case FCP_ABORT_TASK_SET:
4574 return "ABORT_TASK_SET";
4575 case FCP_CLEAR_TASK_SET:
4576 return "FCP_CLEAR_TASK_SET";
4577 case FCP_BUS_RESET:
4578 return "FCP_BUS_RESET";
4579 case FCP_LUN_RESET:
4580 return "FCP_LUN_RESET";
4581 case FCP_TARGET_RESET:
4582 return "FCP_TARGET_RESET";
4583 case FCP_CLEAR_ACA:
4584 return "FCP_CLEAR_ACA";
4585 case FCP_TERMINATE_TASK:
4586 return "FCP_TERMINATE_TASK";
4587 default:
4588 return "unknown";
4589 }
4590}
4591
4592/**
4593 * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
4594 * @vport: The virtual port for which this call is being executed.
4595 * @rdata: Pointer to remote port local data
4596 * @tgt_id: Target ID of remote device.
4597 * @lun_id: Lun number for the TMF
4598 * @task_mgmt_cmd: type of TMF to send
4599 *
4600 * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
4601 * a remote port.
4602 *
4603 * Return Code:
4604 * 0x2003 - Error
4605 * 0x2002 - Success.
4606 **/
4607static int
4608lpfc_send_taskmgmt(struct lpfc_vport *vport, struct lpfc_rport_data *rdata,
4609 unsigned tgt_id, unsigned int lun_id,
4610 uint8_t task_mgmt_cmd)
4611{
4612 struct lpfc_hba *phba = vport->phba;
4613 struct lpfc_scsi_buf *lpfc_cmd;
4614 struct lpfc_iocbq *iocbq;
4615 struct lpfc_iocbq *iocbqrsp;
James Smart5989b8d2010-10-22 11:06:56 -04004616 struct lpfc_nodelist *pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004617 int ret;
4618 int status;
4619
James Smart5989b8d2010-10-22 11:06:56 -04004620 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
James Smartbbb9d182009-06-10 17:23:16 -04004621 return FAILED;
4622
James Smart19ca7602010-11-20 23:11:55 -05004623 lpfc_cmd = lpfc_get_scsi_buf(phba, rdata->pnode);
James Smartbbb9d182009-06-10 17:23:16 -04004624 if (lpfc_cmd == NULL)
4625 return FAILED;
4626 lpfc_cmd->timeout = 60;
4627 lpfc_cmd->rdata = rdata;
4628
4629 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
4630 task_mgmt_cmd);
4631 if (!status) {
4632 lpfc_release_scsi_buf(phba, lpfc_cmd);
4633 return FAILED;
4634 }
4635
4636 iocbq = &lpfc_cmd->cur_iocbq;
4637 iocbqrsp = lpfc_sli_get_iocbq(phba);
4638 if (iocbqrsp == NULL) {
4639 lpfc_release_scsi_buf(phba, lpfc_cmd);
4640 return FAILED;
4641 }
4642
4643 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4644 "0702 Issue %s to TGT %d LUN %d "
James Smart6d368e52011-05-24 11:44:12 -04004645 "rpi x%x nlp_flag x%x Data: x%x x%x\n",
James Smartbbb9d182009-06-10 17:23:16 -04004646 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
James Smart6d368e52011-05-24 11:44:12 -04004647 pnode->nlp_rpi, pnode->nlp_flag, iocbq->sli4_xritag,
4648 iocbq->iocb_flag);
James Smartbbb9d182009-06-10 17:23:16 -04004649
4650 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
4651 iocbq, iocbqrsp, lpfc_cmd->timeout);
4652 if (status != IOCB_SUCCESS) {
4653 if (status == IOCB_TIMEDOUT) {
4654 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
4655 ret = TIMEOUT_ERROR;
4656 } else
4657 ret = FAILED;
4658 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
4659 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smart6d368e52011-05-24 11:44:12 -04004660 "0727 TMF %s to TGT %d LUN %d failed (%d, %d) "
4661 "iocb_flag x%x\n",
James Smartbbb9d182009-06-10 17:23:16 -04004662 lpfc_taskmgmt_name(task_mgmt_cmd),
4663 tgt_id, lun_id, iocbqrsp->iocb.ulpStatus,
James Smart6d368e52011-05-24 11:44:12 -04004664 iocbqrsp->iocb.un.ulpWord[4],
4665 iocbq->iocb_flag);
James Smart2a9bf3d2010-06-07 15:24:45 -04004666 } else if (status == IOCB_BUSY)
4667 ret = FAILED;
4668 else
James Smartbbb9d182009-06-10 17:23:16 -04004669 ret = SUCCESS;
4670
4671 lpfc_sli_release_iocbq(phba, iocbqrsp);
4672
4673 if (ret != TIMEOUT_ERROR)
4674 lpfc_release_scsi_buf(phba, lpfc_cmd);
4675
4676 return ret;
4677}
4678
4679/**
4680 * lpfc_chk_tgt_mapped -
4681 * @vport: The virtual port to check on
4682 * @cmnd: Pointer to scsi_cmnd data structure.
4683 *
4684 * This routine delays until the scsi target (aka rport) for the
4685 * command exists (is present and logged in) or we declare it non-existent.
4686 *
4687 * Return code :
4688 * 0x2003 - Error
4689 * 0x2002 - Success
4690 **/
4691static int
4692lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
4693{
4694 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05004695 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004696 unsigned long later;
4697
James Smart1c6f4ef52009-11-18 15:40:49 -05004698 if (!rdata) {
4699 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4700 "0797 Tgt Map rport failure: rdata x%p\n", rdata);
4701 return FAILED;
4702 }
4703 pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004704 /*
4705 * If target is not in a MAPPED state, delay until
4706 * target is rediscovered or devloss timeout expires.
4707 */
4708 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
4709 while (time_after(later, jiffies)) {
4710 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
4711 return FAILED;
4712 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
4713 return SUCCESS;
4714 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
4715 rdata = cmnd->device->hostdata;
4716 if (!rdata)
4717 return FAILED;
4718 pnode = rdata->pnode;
4719 }
4720 if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
4721 (pnode->nlp_state != NLP_STE_MAPPED_NODE))
4722 return FAILED;
4723 return SUCCESS;
4724}
4725
4726/**
4727 * lpfc_reset_flush_io_context -
4728 * @vport: The virtual port (scsi_host) for the flush context
4729 * @tgt_id: If aborting by Target contect - specifies the target id
4730 * @lun_id: If aborting by Lun context - specifies the lun id
4731 * @context: specifies the context level to flush at.
4732 *
4733 * After a reset condition via TMF, we need to flush orphaned i/o
4734 * contexts from the adapter. This routine aborts any contexts
4735 * outstanding, then waits for their completions. The wait is
4736 * bounded by devloss_tmo though.
4737 *
4738 * Return code :
4739 * 0x2003 - Error
4740 * 0x2002 - Success
4741 **/
4742static int
4743lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
4744 uint64_t lun_id, lpfc_ctx_cmd context)
4745{
4746 struct lpfc_hba *phba = vport->phba;
4747 unsigned long later;
4748 int cnt;
4749
4750 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
4751 if (cnt)
4752 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
4753 tgt_id, lun_id, context);
4754 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
4755 while (time_after(later, jiffies) && cnt) {
4756 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
4757 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
4758 }
4759 if (cnt) {
4760 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4761 "0724 I/O flush failure for context %s : cnt x%x\n",
4762 ((context == LPFC_CTX_LUN) ? "LUN" :
4763 ((context == LPFC_CTX_TGT) ? "TGT" :
4764 ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
4765 cnt);
4766 return FAILED;
4767 }
4768 return SUCCESS;
4769}
4770
James Smart9bad7672008-12-04 22:39:02 -05004771/**
James Smart3621a712009-04-06 18:47:14 -04004772 * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
James Smart9bad7672008-12-04 22:39:02 -05004773 * @cmnd: Pointer to scsi_cmnd data structure.
4774 *
James Smartbbb9d182009-06-10 17:23:16 -04004775 * This routine does a device reset by sending a LUN_RESET task management
James Smart9bad7672008-12-04 22:39:02 -05004776 * command.
4777 *
4778 * Return code :
4779 * 0x2003 - Error
James Smart3621a712009-04-06 18:47:14 -04004780 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05004781 **/
dea31012005-04-17 16:05:31 -05004782static int
James Smart7054a602007-04-25 09:52:34 -04004783lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004784{
James Smart2e0fef82007-06-17 19:56:36 -05004785 struct Scsi_Host *shost = cmnd->device->host;
4786 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05004787 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05004788 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004789 unsigned tgt_id = cmnd->device->id;
4790 unsigned int lun_id = cmnd->device->lun;
James Smartea2151b2008-09-07 11:52:10 -04004791 struct lpfc_scsi_event_header scsi_event;
James Smart3a707302012-06-12 13:54:42 -04004792 int status, ret = SUCCESS;
dea31012005-04-17 16:05:31 -05004793
James Smart1c6f4ef52009-11-18 15:40:49 -05004794 if (!rdata) {
4795 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4796 "0798 Device Reset rport failure: rdata x%p\n", rdata);
4797 return FAILED;
4798 }
4799 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04004800 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04004801 if (status != 0 && status != SUCCESS)
James Smart589a52d2010-07-14 15:30:54 -04004802 return status;
James Smartbbb9d182009-06-10 17:23:16 -04004803
4804 status = lpfc_chk_tgt_mapped(vport, cmnd);
4805 if (status == FAILED) {
4806 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4807 "0721 Device Reset rport failure: rdata x%p\n", rdata);
4808 return FAILED;
4809 }
4810
4811 scsi_event.event_type = FC_REG_SCSI_EVENT;
4812 scsi_event.subcategory = LPFC_EVENT_LUNRESET;
4813 scsi_event.lun = lun_id;
4814 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
4815 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
4816
4817 fc_host_post_vendor_event(shost, fc_get_event_number(),
4818 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
4819
4820 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
4821 FCP_LUN_RESET);
4822
4823 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4824 "0713 SCSI layer issued Device Reset (%d, %d) "
4825 "return x%x\n", tgt_id, lun_id, status);
4826
dea31012005-04-17 16:05:31 -05004827 /*
James Smartbbb9d182009-06-10 17:23:16 -04004828 * We have to clean up i/o as : they may be orphaned by the TMF;
4829 * or if the TMF failed, they may be in an indeterminate state.
4830 * So, continue on.
4831 * We will report success if all the i/o aborts successfully.
dea31012005-04-17 16:05:31 -05004832 */
James Smart3a707302012-06-12 13:54:42 -04004833 ret = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
James Smartbbb9d182009-06-10 17:23:16 -04004834 LPFC_CTX_LUN);
James Smart3a707302012-06-12 13:54:42 -04004835 return ret;
James Smartbbb9d182009-06-10 17:23:16 -04004836}
4837
4838/**
4839 * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
4840 * @cmnd: Pointer to scsi_cmnd data structure.
4841 *
4842 * This routine does a target reset by sending a TARGET_RESET task management
4843 * command.
4844 *
4845 * Return code :
4846 * 0x2003 - Error
4847 * 0x2002 - Success
4848 **/
4849static int
4850lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
4851{
4852 struct Scsi_Host *shost = cmnd->device->host;
4853 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4854 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05004855 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004856 unsigned tgt_id = cmnd->device->id;
4857 unsigned int lun_id = cmnd->device->lun;
4858 struct lpfc_scsi_event_header scsi_event;
James Smart3a707302012-06-12 13:54:42 -04004859 int status, ret = SUCCESS;
James Smartbbb9d182009-06-10 17:23:16 -04004860
James Smart1c6f4ef52009-11-18 15:40:49 -05004861 if (!rdata) {
4862 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4863 "0799 Target Reset rport failure: rdata x%p\n", rdata);
4864 return FAILED;
4865 }
4866 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04004867 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04004868 if (status != 0 && status != SUCCESS)
James Smart589a52d2010-07-14 15:30:54 -04004869 return status;
James Smartbbb9d182009-06-10 17:23:16 -04004870
4871 status = lpfc_chk_tgt_mapped(vport, cmnd);
4872 if (status == FAILED) {
4873 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4874 "0722 Target Reset rport failure: rdata x%p\n", rdata);
4875 return FAILED;
dea31012005-04-17 16:05:31 -05004876 }
James Smartea2151b2008-09-07 11:52:10 -04004877
4878 scsi_event.event_type = FC_REG_SCSI_EVENT;
4879 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
4880 scsi_event.lun = 0;
4881 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
4882 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
4883
James Smartbbb9d182009-06-10 17:23:16 -04004884 fc_host_post_vendor_event(shost, fc_get_event_number(),
4885 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -04004886
James Smartbbb9d182009-06-10 17:23:16 -04004887 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
4888 FCP_TARGET_RESET);
dea31012005-04-17 16:05:31 -05004889
James Smarte8b62012007-08-02 11:10:09 -04004890 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smartbbb9d182009-06-10 17:23:16 -04004891 "0723 SCSI layer issued Target Reset (%d, %d) "
4892 "return x%x\n", tgt_id, lun_id, status);
4893
4894 /*
4895 * We have to clean up i/o as : they may be orphaned by the TMF;
4896 * or if the TMF failed, they may be in an indeterminate state.
4897 * So, continue on.
4898 * We will report success if all the i/o aborts successfully.
4899 */
James Smart3a707302012-06-12 13:54:42 -04004900 ret = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
4901 LPFC_CTX_TGT);
4902 return ret;
dea31012005-04-17 16:05:31 -05004903}
4904
James Smart9bad7672008-12-04 22:39:02 -05004905/**
James Smart3621a712009-04-06 18:47:14 -04004906 * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05004907 * @cmnd: Pointer to scsi_cmnd data structure.
4908 *
James Smartbbb9d182009-06-10 17:23:16 -04004909 * This routine does target reset to all targets on @cmnd->device->host.
4910 * This emulates Parallel SCSI Bus Reset Semantics.
James Smart9bad7672008-12-04 22:39:02 -05004911 *
James Smartbbb9d182009-06-10 17:23:16 -04004912 * Return code :
4913 * 0x2003 - Error
4914 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05004915 **/
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04004916static int
James Smart7054a602007-04-25 09:52:34 -04004917lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004918{
James Smart2e0fef82007-06-17 19:56:36 -05004919 struct Scsi_Host *shost = cmnd->device->host;
4920 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05004921 struct lpfc_nodelist *ndlp = NULL;
James Smartea2151b2008-09-07 11:52:10 -04004922 struct lpfc_scsi_event_header scsi_event;
James Smartbbb9d182009-06-10 17:23:16 -04004923 int match;
4924 int ret = SUCCESS, status, i;
James Smartea2151b2008-09-07 11:52:10 -04004925
4926 scsi_event.event_type = FC_REG_SCSI_EVENT;
4927 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
4928 scsi_event.lun = 0;
4929 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
4930 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
4931
James Smartbbb9d182009-06-10 17:23:16 -04004932 fc_host_post_vendor_event(shost, fc_get_event_number(),
4933 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
dea31012005-04-17 16:05:31 -05004934
James Smartbf086112011-08-21 21:48:13 -04004935 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04004936 if (status != 0 && status != SUCCESS)
James Smartbf086112011-08-21 21:48:13 -04004937 return status;
James Smartbbb9d182009-06-10 17:23:16 -04004938
dea31012005-04-17 16:05:31 -05004939 /*
4940 * Since the driver manages a single bus device, reset all
4941 * targets known to the driver. Should any target reset
4942 * fail, this routine returns failure to the midlayer.
4943 */
James Smarte17da182006-07-06 15:49:25 -04004944 for (i = 0; i < LPFC_MAX_TARGET; i++) {
James Smart685f0bf2007-04-25 09:53:08 -04004945 /* Search for mapped node by target ID */
dea31012005-04-17 16:05:31 -05004946 match = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004947 spin_lock_irq(shost->host_lock);
4948 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05004949 if (!NLP_CHK_NODE_ACT(ndlp))
4950 continue;
James Smarta6571c62012-10-31 14:44:42 -04004951 if (vport->phba->cfg_fcp2_no_tgt_reset &&
4952 (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE))
4953 continue;
James Smart685f0bf2007-04-25 09:53:08 -04004954 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
James Smart915caaa2008-06-14 22:52:38 -04004955 ndlp->nlp_sid == i &&
James Smart685f0bf2007-04-25 09:53:08 -04004956 ndlp->rport) {
dea31012005-04-17 16:05:31 -05004957 match = 1;
4958 break;
4959 }
4960 }
James Smart2e0fef82007-06-17 19:56:36 -05004961 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004962 if (!match)
4963 continue;
James Smartbbb9d182009-06-10 17:23:16 -04004964
4965 status = lpfc_send_taskmgmt(vport, ndlp->rport->dd_data,
4966 i, 0, FCP_TARGET_RESET);
4967
4968 if (status != SUCCESS) {
James Smarte8b62012007-08-02 11:10:09 -04004969 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4970 "0700 Bus Reset on target %d failed\n",
4971 i);
James Smart915caaa2008-06-14 22:52:38 -04004972 ret = FAILED;
dea31012005-04-17 16:05:31 -05004973 }
4974 }
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05004975 /*
James Smartbbb9d182009-06-10 17:23:16 -04004976 * We have to clean up i/o as : they may be orphaned by the TMFs
4977 * above; or if any of the TMFs failed, they may be in an
4978 * indeterminate state.
4979 * We will report success if all the i/o aborts successfully.
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05004980 */
James Smartbbb9d182009-06-10 17:23:16 -04004981
4982 status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
4983 if (status != SUCCESS)
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05004984 ret = FAILED;
James Smartbbb9d182009-06-10 17:23:16 -04004985
James Smarte8b62012007-08-02 11:10:09 -04004986 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4987 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea31012005-04-17 16:05:31 -05004988 return ret;
4989}
4990
James Smart9bad7672008-12-04 22:39:02 -05004991/**
James Smart27b01b82012-05-09 21:19:44 -04004992 * lpfc_host_reset_handler - scsi_host_template eh_host_reset_handler entry pt
4993 * @cmnd: Pointer to scsi_cmnd data structure.
4994 *
4995 * This routine does host reset to the adaptor port. It brings the HBA
4996 * offline, performs a board restart, and then brings the board back online.
4997 * The lpfc_offline calls lpfc_sli_hba_down which will abort and local
4998 * reject all outstanding SCSI commands to the host and error returned
4999 * back to SCSI mid-level. As this will be SCSI mid-level's last resort
5000 * of error handling, it will only return error if resetting of the adapter
5001 * is not successful; in all other cases, will return success.
5002 *
5003 * Return code :
5004 * 0x2003 - Error
5005 * 0x2002 - Success
5006 **/
5007static int
5008lpfc_host_reset_handler(struct scsi_cmnd *cmnd)
5009{
5010 struct Scsi_Host *shost = cmnd->device->host;
5011 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5012 struct lpfc_hba *phba = vport->phba;
5013 int rc, ret = SUCCESS;
5014
James Smart618a5232012-06-12 13:54:36 -04005015 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart27b01b82012-05-09 21:19:44 -04005016 lpfc_offline(phba);
5017 rc = lpfc_sli_brdrestart(phba);
5018 if (rc)
5019 ret = FAILED;
5020 lpfc_online(phba);
5021 lpfc_unblock_mgmt_io(phba);
5022
5023 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
5024 "3172 SCSI layer issued Host Reset Data: x%x\n", ret);
5025 return ret;
5026}
5027
5028/**
James Smart3621a712009-04-06 18:47:14 -04005029 * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
James Smart9bad7672008-12-04 22:39:02 -05005030 * @sdev: Pointer to scsi_device.
5031 *
5032 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
5033 * globally available list of scsi buffers. This routine also makes sure scsi
5034 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
5035 * of scsi buffer exists for the lifetime of the driver.
5036 *
5037 * Return codes:
5038 * non-0 - Error
5039 * 0 - Success
5040 **/
dea31012005-04-17 16:05:31 -05005041static int
dea31012005-04-17 16:05:31 -05005042lpfc_slave_alloc(struct scsi_device *sdev)
5043{
James Smart2e0fef82007-06-17 19:56:36 -05005044 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5045 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005046 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
James Smart3772a992009-05-22 14:50:54 -04005047 uint32_t total = 0;
dea31012005-04-17 16:05:31 -05005048 uint32_t num_to_alloc = 0;
James Smart3772a992009-05-22 14:50:54 -04005049 int num_allocated = 0;
James Smartd7c47992010-06-08 18:31:54 -04005050 uint32_t sdev_cnt;
dea31012005-04-17 16:05:31 -05005051
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005052 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05005053 return -ENXIO;
5054
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005055 sdev->hostdata = rport->dd_data;
James Smartd7c47992010-06-08 18:31:54 -04005056 sdev_cnt = atomic_inc_return(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05005057
5058 /*
5059 * Populate the cmds_per_lun count scsi_bufs into this host's globally
5060 * available list of scsi buffers. Don't allocate more than the
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04005061 * HBA limit conveyed to the midlayer via the host structure. The
5062 * formula accounts for the lun_queue_depth + error handlers + 1
5063 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea31012005-04-17 16:05:31 -05005064 */
5065 total = phba->total_scsi_bufs;
James Smart3de2a652007-08-02 11:09:59 -04005066 num_to_alloc = vport->cfg_lun_queue_depth + 2;
James Smart92d7f7b2007-06-17 19:56:38 -05005067
James Smartd7c47992010-06-08 18:31:54 -04005068 /* If allocated buffers are enough do nothing */
5069 if ((sdev_cnt * (vport->cfg_lun_queue_depth + 2)) < total)
5070 return 0;
5071
James Smart92d7f7b2007-06-17 19:56:38 -05005072 /* Allow some exchanges to be available always to complete discovery */
5073 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04005074 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5075 "0704 At limitation of %d preallocated "
5076 "command buffers\n", total);
dea31012005-04-17 16:05:31 -05005077 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05005078 /* Allow some exchanges to be available always to complete discovery */
5079 } else if (total + num_to_alloc >
5080 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04005081 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5082 "0705 Allocation request of %d "
5083 "command buffers will exceed max of %d. "
5084 "Reducing allocation request to %d.\n",
5085 num_to_alloc, phba->cfg_hba_queue_depth,
5086 (phba->cfg_hba_queue_depth - total));
dea31012005-04-17 16:05:31 -05005087 num_to_alloc = phba->cfg_hba_queue_depth - total;
5088 }
James Smart3772a992009-05-22 14:50:54 -04005089 num_allocated = lpfc_new_scsi_buf(vport, num_to_alloc);
5090 if (num_to_alloc != num_allocated) {
5091 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5092 "0708 Allocation request of %d "
5093 "command buffers did not succeed. "
5094 "Allocated %d buffers.\n",
5095 num_to_alloc, num_allocated);
dea31012005-04-17 16:05:31 -05005096 }
James Smart1c6f4ef52009-11-18 15:40:49 -05005097 if (num_allocated > 0)
5098 phba->total_scsi_bufs += num_allocated;
dea31012005-04-17 16:05:31 -05005099 return 0;
5100}
5101
James Smart9bad7672008-12-04 22:39:02 -05005102/**
James Smart3621a712009-04-06 18:47:14 -04005103 * lpfc_slave_configure - scsi_host_template slave_configure entry point
James Smart9bad7672008-12-04 22:39:02 -05005104 * @sdev: Pointer to scsi_device.
5105 *
5106 * This routine configures following items
5107 * - Tag command queuing support for @sdev if supported.
James Smart9bad7672008-12-04 22:39:02 -05005108 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
5109 *
5110 * Return codes:
5111 * 0 - Success
5112 **/
dea31012005-04-17 16:05:31 -05005113static int
5114lpfc_slave_configure(struct scsi_device *sdev)
5115{
James Smart2e0fef82007-06-17 19:56:36 -05005116 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5117 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05005118
5119 if (sdev->tagged_supported)
James Smart3de2a652007-08-02 11:09:59 -04005120 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05005121 else
James Smart3de2a652007-08-02 11:09:59 -04005122 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05005123
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005124 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04005125 lpfc_sli_handle_fast_ring_event(phba,
5126 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005127 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
5128 lpfc_poll_rearm_timer(phba);
5129 }
5130
dea31012005-04-17 16:05:31 -05005131 return 0;
5132}
5133
James Smart9bad7672008-12-04 22:39:02 -05005134/**
James Smart3621a712009-04-06 18:47:14 -04005135 * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
James Smart9bad7672008-12-04 22:39:02 -05005136 * @sdev: Pointer to scsi_device.
5137 *
5138 * This routine sets @sdev hostatdata filed to null.
5139 **/
dea31012005-04-17 16:05:31 -05005140static void
5141lpfc_slave_destroy(struct scsi_device *sdev)
5142{
James Smartd7c47992010-06-08 18:31:54 -04005143 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5144 struct lpfc_hba *phba = vport->phba;
5145 atomic_dec(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05005146 sdev->hostdata = NULL;
5147 return;
5148}
5149
James Smart92d7f7b2007-06-17 19:56:38 -05005150
dea31012005-04-17 16:05:31 -05005151struct scsi_host_template lpfc_template = {
5152 .module = THIS_MODULE,
5153 .name = LPFC_DRIVER_NAME,
5154 .info = lpfc_info,
5155 .queuecommand = lpfc_queuecommand,
5156 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04005157 .eh_device_reset_handler = lpfc_device_reset_handler,
5158 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart7054a602007-04-25 09:52:34 -04005159 .eh_bus_reset_handler = lpfc_bus_reset_handler,
James Smart27b01b82012-05-09 21:19:44 -04005160 .eh_host_reset_handler = lpfc_host_reset_handler,
dea31012005-04-17 16:05:31 -05005161 .slave_alloc = lpfc_slave_alloc,
5162 .slave_configure = lpfc_slave_configure,
5163 .slave_destroy = lpfc_slave_destroy,
James Smart47a86172007-04-25 09:53:22 -04005164 .scan_finished = lpfc_scan_finished,
dea31012005-04-17 16:05:31 -05005165 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05005166 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
dea31012005-04-17 16:05:31 -05005167 .cmd_per_lun = LPFC_CMD_PER_LUN,
5168 .use_clustering = ENABLE_CLUSTERING,
James Smart2e0fef82007-06-17 19:56:36 -05005169 .shost_attrs = lpfc_hba_attrs,
James.Smart@Emulex.Com564b2962005-06-25 10:34:17 -04005170 .max_sectors = 0xFFFF,
James Smartf1c3b0f2009-07-19 10:01:32 -04005171 .vendor_id = LPFC_NL_VENDOR_ID,
James Smart5ffc2662009-11-18 15:39:44 -05005172 .change_queue_depth = lpfc_change_queue_depth,
James Smartfe8f7f92013-01-03 15:43:03 -05005173 .change_queue_type = lpfc_change_queue_type,
dea31012005-04-17 16:05:31 -05005174};
James Smart3de2a652007-08-02 11:09:59 -04005175
5176struct scsi_host_template lpfc_vport_template = {
5177 .module = THIS_MODULE,
5178 .name = LPFC_DRIVER_NAME,
5179 .info = lpfc_info,
5180 .queuecommand = lpfc_queuecommand,
5181 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04005182 .eh_device_reset_handler = lpfc_device_reset_handler,
5183 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart3de2a652007-08-02 11:09:59 -04005184 .eh_bus_reset_handler = lpfc_bus_reset_handler,
5185 .slave_alloc = lpfc_slave_alloc,
5186 .slave_configure = lpfc_slave_configure,
5187 .slave_destroy = lpfc_slave_destroy,
5188 .scan_finished = lpfc_scan_finished,
5189 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05005190 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
James Smart3de2a652007-08-02 11:09:59 -04005191 .cmd_per_lun = LPFC_CMD_PER_LUN,
5192 .use_clustering = ENABLE_CLUSTERING,
5193 .shost_attrs = lpfc_vport_attrs,
5194 .max_sectors = 0xFFFF,
James Smart5ffc2662009-11-18 15:39:44 -05005195 .change_queue_depth = lpfc_change_queue_depth,
James Smartfe8f7f92013-01-03 15:43:03 -05005196 .change_queue_type = lpfc_change_queue_type,
James Smart3de2a652007-08-02 11:09:59 -04005197};