blob: efc055b6bac4fe6fffb8432c4004ea2cbc9ac5aa [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * 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"
42#include "lpfc_scsi.h"
43#include "lpfc.h"
44#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[] = {
54 "SCSI_PROT_NORMAL",
55 "SCSI_PROT_READ_INSERT",
56 "SCSI_PROT_WRITE_STRIP",
57 "SCSI_PROT_READ_STRIP",
58 "SCSI_PROT_WRITE_INSERT",
59 "SCSI_PROT_READ_PASS",
60 "SCSI_PROT_WRITE_PASS",
James Smarte2a0a9d2008-12-04 22:40:02 -050061};
James Smartf9bb2da2011-10-10 21:34:11 -040062
63struct 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 Smart3621a712009-04-06 18:47:14 -0400291 * lpfc_rampdown_queue_depth - Post RAMP_DOWN_QUEUE event to worker thread
James Smart9bad7672008-12-04 22:39:02 -0500292 * @phba: The Hba for which this call is being executed.
293 *
294 * This routine is called when there is resource error in driver or firmware.
295 * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
296 * posts at most 1 event each second. This routine wakes up worker thread of
297 * @phba to process WORKER_RAM_DOWN_EVENT event.
298 *
299 * This routine should be called with no lock held.
300 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500301void
James Smarteaf15d52008-12-04 22:39:29 -0500302lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
James Smart92d7f7b2007-06-17 19:56:38 -0500303{
304 unsigned long flags;
James Smart5e9d9b82008-06-14 22:52:53 -0400305 uint32_t evt_posted;
James Smart92d7f7b2007-06-17 19:56:38 -0500306
307 spin_lock_irqsave(&phba->hbalock, flags);
308 atomic_inc(&phba->num_rsrc_err);
309 phba->last_rsrc_error_time = jiffies;
310
311 if ((phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL) > jiffies) {
312 spin_unlock_irqrestore(&phba->hbalock, flags);
313 return;
314 }
315
316 phba->last_ramp_down_time = jiffies;
317
318 spin_unlock_irqrestore(&phba->hbalock, flags);
319
320 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400321 evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
322 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500323 phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500324 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
325
James Smart5e9d9b82008-06-14 22:52:53 -0400326 if (!evt_posted)
327 lpfc_worker_wake_up(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500328 return;
329}
330
James Smart9bad7672008-12-04 22:39:02 -0500331/**
James Smart3621a712009-04-06 18:47:14 -0400332 * lpfc_rampup_queue_depth - Post RAMP_UP_QUEUE event for worker thread
James Smart9bad7672008-12-04 22:39:02 -0500333 * @phba: The Hba for which this call is being executed.
334 *
335 * This routine post WORKER_RAMP_UP_QUEUE event for @phba vport. This routine
336 * post at most 1 event every 5 minute after last_ramp_up_time or
337 * last_rsrc_error_time. This routine wakes up worker thread of @phba
338 * to process WORKER_RAM_DOWN_EVENT event.
339 *
340 * This routine should be called with no lock held.
341 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500342static inline void
James Smart3de2a652007-08-02 11:09:59 -0400343lpfc_rampup_queue_depth(struct lpfc_vport *vport,
James Smarta257bf92009-04-06 18:48:10 -0400344 uint32_t queue_depth)
James Smart92d7f7b2007-06-17 19:56:38 -0500345{
346 unsigned long flags;
James Smart3de2a652007-08-02 11:09:59 -0400347 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -0400348 uint32_t evt_posted;
James Smart92d7f7b2007-06-17 19:56:38 -0500349 atomic_inc(&phba->num_cmd_success);
350
James Smarta257bf92009-04-06 18:48:10 -0400351 if (vport->cfg_lun_queue_depth <= queue_depth)
James Smart92d7f7b2007-06-17 19:56:38 -0500352 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500353 spin_lock_irqsave(&phba->hbalock, flags);
James Smart5ffc2662009-11-18 15:39:44 -0500354 if (time_before(jiffies,
355 phba->last_ramp_up_time + QUEUE_RAMP_UP_INTERVAL) ||
356 time_before(jiffies,
357 phba->last_rsrc_error_time + QUEUE_RAMP_UP_INTERVAL)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500358 spin_unlock_irqrestore(&phba->hbalock, flags);
359 return;
360 }
James Smart92d7f7b2007-06-17 19:56:38 -0500361 phba->last_ramp_up_time = jiffies;
362 spin_unlock_irqrestore(&phba->hbalock, flags);
363
364 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400365 evt_posted = phba->pport->work_port_events & WORKER_RAMP_UP_QUEUE;
366 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500367 phba->pport->work_port_events |= WORKER_RAMP_UP_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500368 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
369
James Smart5e9d9b82008-06-14 22:52:53 -0400370 if (!evt_posted)
371 lpfc_worker_wake_up(phba);
372 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500373}
374
James Smart9bad7672008-12-04 22:39:02 -0500375/**
James Smart3621a712009-04-06 18:47:14 -0400376 * lpfc_ramp_down_queue_handler - WORKER_RAMP_DOWN_QUEUE event handler
James Smart9bad7672008-12-04 22:39:02 -0500377 * @phba: The Hba for which this call is being executed.
378 *
379 * This routine is called to process WORKER_RAMP_DOWN_QUEUE event for worker
380 * thread.This routine reduces queue depth for all scsi device on each vport
381 * associated with @phba.
382 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500383void
384lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
385{
James Smart549e55c2007-08-02 11:09:51 -0400386 struct lpfc_vport **vports;
387 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500388 struct scsi_device *sdev;
James Smart5ffc2662009-11-18 15:39:44 -0500389 unsigned long new_queue_depth;
James Smart92d7f7b2007-06-17 19:56:38 -0500390 unsigned long num_rsrc_err, num_cmd_success;
James Smart549e55c2007-08-02 11:09:51 -0400391 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500392
393 num_rsrc_err = atomic_read(&phba->num_rsrc_err);
394 num_cmd_success = atomic_read(&phba->num_cmd_success);
395
James Smart549e55c2007-08-02 11:09:51 -0400396 vports = lpfc_create_vport_work_array(phba);
397 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400398 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400399 shost = lpfc_shost_from_vport(vports[i]);
400 shost_for_each_device(sdev, shost) {
James Smart92d7f7b2007-06-17 19:56:38 -0500401 new_queue_depth =
James Smart549e55c2007-08-02 11:09:51 -0400402 sdev->queue_depth * num_rsrc_err /
403 (num_rsrc_err + num_cmd_success);
404 if (!new_queue_depth)
405 new_queue_depth = sdev->queue_depth - 1;
406 else
407 new_queue_depth = sdev->queue_depth -
408 new_queue_depth;
James Smart5ffc2662009-11-18 15:39:44 -0500409 lpfc_change_queue_depth(sdev, new_queue_depth,
410 SCSI_QDEPTH_DEFAULT);
James Smart549e55c2007-08-02 11:09:51 -0400411 }
James Smart92d7f7b2007-06-17 19:56:38 -0500412 }
James Smart09372822008-01-11 01:52:54 -0500413 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500414 atomic_set(&phba->num_rsrc_err, 0);
415 atomic_set(&phba->num_cmd_success, 0);
416}
417
James Smart9bad7672008-12-04 22:39:02 -0500418/**
James Smart3621a712009-04-06 18:47:14 -0400419 * lpfc_ramp_up_queue_handler - WORKER_RAMP_UP_QUEUE event handler
James Smart9bad7672008-12-04 22:39:02 -0500420 * @phba: The Hba for which this call is being executed.
421 *
422 * This routine is called to process WORKER_RAMP_UP_QUEUE event for worker
423 * thread.This routine increases queue depth for all scsi device on each vport
424 * associated with @phba by 1. This routine also sets @phba num_rsrc_err and
425 * num_cmd_success to zero.
426 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500427void
428lpfc_ramp_up_queue_handler(struct lpfc_hba *phba)
429{
James Smart549e55c2007-08-02 11:09:51 -0400430 struct lpfc_vport **vports;
431 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500432 struct scsi_device *sdev;
James Smart549e55c2007-08-02 11:09:51 -0400433 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500434
James Smart549e55c2007-08-02 11:09:51 -0400435 vports = lpfc_create_vport_work_array(phba);
436 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400437 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400438 shost = lpfc_shost_from_vport(vports[i]);
439 shost_for_each_device(sdev, shost) {
James Smart97eab632008-04-07 10:16:05 -0400440 if (vports[i]->cfg_lun_queue_depth <=
441 sdev->queue_depth)
442 continue;
James Smart5ffc2662009-11-18 15:39:44 -0500443 lpfc_change_queue_depth(sdev,
444 sdev->queue_depth+1,
445 SCSI_QDEPTH_RAMP_UP);
James Smart549e55c2007-08-02 11:09:51 -0400446 }
James Smart92d7f7b2007-06-17 19:56:38 -0500447 }
James Smart09372822008-01-11 01:52:54 -0500448 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500449 atomic_set(&phba->num_rsrc_err, 0);
450 atomic_set(&phba->num_cmd_success, 0);
451}
452
James Smarta8e497d2008-08-24 21:50:11 -0400453/**
James Smart3621a712009-04-06 18:47:14 -0400454 * lpfc_scsi_dev_block - set all scsi hosts to block state
James Smarta8e497d2008-08-24 21:50:11 -0400455 * @phba: Pointer to HBA context object.
456 *
457 * This function walks vport list and set each SCSI host to block state
458 * by invoking fc_remote_port_delete() routine. This function is invoked
459 * with EEH when device's PCI slot has been permanently disabled.
460 **/
461void
462lpfc_scsi_dev_block(struct lpfc_hba *phba)
463{
464 struct lpfc_vport **vports;
465 struct Scsi_Host *shost;
466 struct scsi_device *sdev;
467 struct fc_rport *rport;
468 int i;
469
470 vports = lpfc_create_vport_work_array(phba);
471 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400472 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8e497d2008-08-24 21:50:11 -0400473 shost = lpfc_shost_from_vport(vports[i]);
474 shost_for_each_device(sdev, shost) {
475 rport = starget_to_rport(scsi_target(sdev));
476 fc_remote_port_delete(rport);
477 }
478 }
479 lpfc_destroy_vport_work_array(phba, vports);
480}
481
James Smart9bad7672008-12-04 22:39:02 -0500482/**
James Smart3772a992009-05-22 14:50:54 -0400483 * lpfc_new_scsi_buf_s3 - Scsi buffer allocator for HBA with SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -0500484 * @vport: The virtual port for which this call being executed.
James Smart3772a992009-05-22 14:50:54 -0400485 * @num_to_allocate: The requested number of buffers to allocate.
James Smart9bad7672008-12-04 22:39:02 -0500486 *
James Smart3772a992009-05-22 14:50:54 -0400487 * This routine allocates a scsi buffer for device with SLI-3 interface spec,
488 * the scsi buffer contains all the necessary information needed to initiate
489 * a SCSI I/O. The non-DMAable buffer region contains information to build
490 * the IOCB. The DMAable region contains memory for the FCP CMND, FCP RSP,
491 * and the initial BPL. In addition to allocating memory, the FCP CMND and
492 * FCP RSP BDEs are setup in the BPL and the BPL BDE is setup in the IOCB.
James Smart9bad7672008-12-04 22:39:02 -0500493 *
494 * Return codes:
James Smart3772a992009-05-22 14:50:54 -0400495 * int - number of scsi buffers that were allocated.
496 * 0 = failure, less than num_to_alloc is a partial failure.
James Smart9bad7672008-12-04 22:39:02 -0500497 **/
James Smart3772a992009-05-22 14:50:54 -0400498static int
499lpfc_new_scsi_buf_s3(struct lpfc_vport *vport, int num_to_alloc)
dea31012005-04-17 16:05:31 -0500500{
James Smart2e0fef82007-06-17 19:56:36 -0500501 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500502 struct lpfc_scsi_buf *psb;
503 struct ulp_bde64 *bpl;
504 IOCB_t *iocb;
James Smart34b02dc2008-08-24 21:49:55 -0400505 dma_addr_t pdma_phys_fcp_cmd;
506 dma_addr_t pdma_phys_fcp_rsp;
507 dma_addr_t pdma_phys_bpl;
James Bottomley604a3e32005-10-29 10:28:33 -0500508 uint16_t iotag;
James Smart3772a992009-05-22 14:50:54 -0400509 int bcnt;
dea31012005-04-17 16:05:31 -0500510
James Smart3772a992009-05-22 14:50:54 -0400511 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
512 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
513 if (!psb)
514 break;
dea31012005-04-17 16:05:31 -0500515
James Smart3772a992009-05-22 14:50:54 -0400516 /*
517 * Get memory from the pci pool to map the virt space to pci
518 * bus space for an I/O. The DMA buffer includes space for the
519 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
520 * necessary to support the sg_tablesize.
521 */
522 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
523 GFP_KERNEL, &psb->dma_handle);
524 if (!psb->data) {
525 kfree(psb);
526 break;
527 }
dea31012005-04-17 16:05:31 -0500528
James Smart3772a992009-05-22 14:50:54 -0400529 /* Initialize virtual ptrs to dma_buf region. */
530 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
dea31012005-04-17 16:05:31 -0500531
James Smart3772a992009-05-22 14:50:54 -0400532 /* Allocate iotag for psb->cur_iocbq. */
533 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
534 if (iotag == 0) {
535 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
536 psb->data, psb->dma_handle);
537 kfree(psb);
538 break;
539 }
540 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Bottomley604a3e32005-10-29 10:28:33 -0500541
James Smart3772a992009-05-22 14:50:54 -0400542 psb->fcp_cmnd = psb->data;
543 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
544 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
James Smart34b02dc2008-08-24 21:49:55 -0400545 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500546
James Smart3772a992009-05-22 14:50:54 -0400547 /* Initialize local short-hand pointers. */
548 bpl = psb->fcp_bpl;
549 pdma_phys_fcp_cmd = psb->dma_handle;
550 pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
551 pdma_phys_bpl = psb->dma_handle + sizeof(struct fcp_cmnd) +
552 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500553
James Smart3772a992009-05-22 14:50:54 -0400554 /*
555 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
556 * are sg list bdes. Initialize the first two and leave the
557 * rest for queuecommand.
558 */
559 bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
560 bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
561 bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
562 bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
563 bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
dea31012005-04-17 16:05:31 -0500564
James Smart3772a992009-05-22 14:50:54 -0400565 /* Setup the physical region for the FCP RSP */
566 bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
567 bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
568 bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
569 bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
570 bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
571
572 /*
573 * Since the IOCB for the FCP I/O is built into this
574 * lpfc_scsi_buf, initialize it with all known data now.
575 */
576 iocb = &psb->cur_iocbq.iocb;
577 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
578 if ((phba->sli_rev == 3) &&
579 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
580 /* fill in immediate fcp command BDE */
581 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
582 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
583 iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
584 unsli3.fcp_ext.icd);
585 iocb->un.fcpi64.bdl.addrHigh = 0;
586 iocb->ulpBdeCount = 0;
587 iocb->ulpLe = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300588 /* fill in response BDE */
James Smart3772a992009-05-22 14:50:54 -0400589 iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags =
590 BUFF_TYPE_BDE_64;
591 iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
592 sizeof(struct fcp_rsp);
593 iocb->unsli3.fcp_ext.rbde.addrLow =
594 putPaddrLow(pdma_phys_fcp_rsp);
595 iocb->unsli3.fcp_ext.rbde.addrHigh =
596 putPaddrHigh(pdma_phys_fcp_rsp);
597 } else {
598 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
599 iocb->un.fcpi64.bdl.bdeSize =
600 (2 * sizeof(struct ulp_bde64));
601 iocb->un.fcpi64.bdl.addrLow =
602 putPaddrLow(pdma_phys_bpl);
603 iocb->un.fcpi64.bdl.addrHigh =
604 putPaddrHigh(pdma_phys_bpl);
605 iocb->ulpBdeCount = 1;
606 iocb->ulpLe = 1;
607 }
608 iocb->ulpClass = CLASS3;
609 psb->status = IOSTAT_SUCCESS;
James Smartda0436e2009-05-22 14:51:39 -0400610 /* Put it back into the SCSI buffer list */
James Smarteee88772010-09-29 11:19:08 -0400611 psb->cur_iocbq.context1 = psb;
James Smart1c6f4ef52009-11-18 15:40:49 -0500612 lpfc_release_scsi_buf_s3(phba, psb);
James Smart3772a992009-05-22 14:50:54 -0400613
James Smart34b02dc2008-08-24 21:49:55 -0400614 }
dea31012005-04-17 16:05:31 -0500615
James Smart3772a992009-05-22 14:50:54 -0400616 return bcnt;
dea31012005-04-17 16:05:31 -0500617}
618
James Smart9bad7672008-12-04 22:39:02 -0500619/**
James Smart1151e3e2011-02-16 12:39:35 -0500620 * lpfc_sli4_vport_delete_fcp_xri_aborted -Remove all ndlp references for vport
621 * @vport: pointer to lpfc vport data structure.
622 *
623 * This routine is invoked by the vport cleanup for deletions and the cleanup
624 * for an ndlp on removal.
625 **/
626void
627lpfc_sli4_vport_delete_fcp_xri_aborted(struct lpfc_vport *vport)
628{
629 struct lpfc_hba *phba = vport->phba;
630 struct lpfc_scsi_buf *psb, *next_psb;
631 unsigned long iflag = 0;
632
633 spin_lock_irqsave(&phba->hbalock, iflag);
634 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
635 list_for_each_entry_safe(psb, next_psb,
636 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
637 if (psb->rdata && psb->rdata->pnode
638 && psb->rdata->pnode->vport == vport)
639 psb->rdata = NULL;
640 }
641 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
642 spin_unlock_irqrestore(&phba->hbalock, iflag);
643}
644
645/**
James Smartda0436e2009-05-22 14:51:39 -0400646 * lpfc_sli4_fcp_xri_aborted - Fast-path process of fcp xri abort
647 * @phba: pointer to lpfc hba data structure.
648 * @axri: pointer to the fcp xri abort wcqe structure.
649 *
650 * This routine is invoked by the worker thread to process a SLI4 fast-path
651 * FCP aborted xri.
652 **/
653void
654lpfc_sli4_fcp_xri_aborted(struct lpfc_hba *phba,
655 struct sli4_wcqe_xri_aborted *axri)
656{
657 uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
James Smart19ca7602010-11-20 23:11:55 -0500658 uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
James Smartda0436e2009-05-22 14:51:39 -0400659 struct lpfc_scsi_buf *psb, *next_psb;
660 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -0500661 struct lpfc_iocbq *iocbq;
662 int i;
James Smart19ca7602010-11-20 23:11:55 -0500663 struct lpfc_nodelist *ndlp;
664 int rrq_empty = 0;
James Smart589a52d2010-07-14 15:30:54 -0400665 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smartda0436e2009-05-22 14:51:39 -0400666
James Smart0f65ff62010-02-26 14:14:23 -0500667 spin_lock_irqsave(&phba->hbalock, iflag);
668 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
James Smartda0436e2009-05-22 14:51:39 -0400669 list_for_each_entry_safe(psb, next_psb,
670 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
671 if (psb->cur_iocbq.sli4_xritag == xri) {
672 list_del(&psb->list);
James Smart341af102010-01-26 23:07:37 -0500673 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400674 psb->status = IOSTAT_SUCCESS;
James Smart0f65ff62010-02-26 14:14:23 -0500675 spin_unlock(
676 &phba->sli4_hba.abts_scsi_buf_list_lock);
James Smart1151e3e2011-02-16 12:39:35 -0500677 if (psb->rdata && psb->rdata->pnode)
678 ndlp = psb->rdata->pnode;
679 else
680 ndlp = NULL;
681
James Smart19ca7602010-11-20 23:11:55 -0500682 rrq_empty = list_empty(&phba->active_rrq_list);
James Smart0f65ff62010-02-26 14:14:23 -0500683 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartcb69f7d2011-12-13 13:21:57 -0500684 if (ndlp) {
James Smart19ca7602010-11-20 23:11:55 -0500685 lpfc_set_rrq_active(phba, ndlp, xri, rxid, 1);
James Smartcb69f7d2011-12-13 13:21:57 -0500686 lpfc_sli4_abts_err_handler(phba, ndlp, axri);
687 }
James Smartda0436e2009-05-22 14:51:39 -0400688 lpfc_release_scsi_buf_s4(phba, psb);
James Smart19ca7602010-11-20 23:11:55 -0500689 if (rrq_empty)
690 lpfc_worker_wake_up(phba);
James Smartda0436e2009-05-22 14:51:39 -0400691 return;
692 }
693 }
James Smart0f65ff62010-02-26 14:14:23 -0500694 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
695 for (i = 1; i <= phba->sli.last_iotag; i++) {
696 iocbq = phba->sli.iocbq_lookup[i];
697
698 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
699 (iocbq->iocb_flag & LPFC_IO_LIBDFC))
700 continue;
701 if (iocbq->sli4_xritag != xri)
702 continue;
703 psb = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
704 psb->exch_busy = 0;
705 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart589a52d2010-07-14 15:30:54 -0400706 if (pring->txq_cnt)
707 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -0500708 return;
709
710 }
711 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartda0436e2009-05-22 14:51:39 -0400712}
713
714/**
715 * lpfc_sli4_repost_scsi_sgl_list - Repsot the Scsi buffers sgl pages as block
716 * @phba: pointer to lpfc hba data structure.
717 *
718 * This routine walks the list of scsi buffers that have been allocated and
719 * repost them to the HBA by using SGL block post. This is needed after a
720 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
721 * is responsible for moving all scsi buffers on the lpfc_abts_scsi_sgl_list
722 * to the lpfc_scsi_buf_list. If the repost fails, reject all scsi buffers.
723 *
724 * Returns: 0 = success, non-zero failure.
725 **/
726int
727lpfc_sli4_repost_scsi_sgl_list(struct lpfc_hba *phba)
728{
729 struct lpfc_scsi_buf *psb;
730 int index, status, bcnt = 0, rcnt = 0, rc = 0;
731 LIST_HEAD(sblist);
732
733 for (index = 0; index < phba->sli4_hba.scsi_xri_cnt; index++) {
734 psb = phba->sli4_hba.lpfc_scsi_psb_array[index];
735 if (psb) {
736 /* Remove from SCSI buffer list */
737 list_del(&psb->list);
738 /* Add it to a local SCSI buffer list */
739 list_add_tail(&psb->list, &sblist);
740 if (++rcnt == LPFC_NEMBED_MBOX_SGL_CNT) {
741 bcnt = rcnt;
742 rcnt = 0;
743 }
744 } else
745 /* A hole present in the XRI array, need to skip */
746 bcnt = rcnt;
747
748 if (index == phba->sli4_hba.scsi_xri_cnt - 1)
749 /* End of XRI array for SCSI buffer, complete */
750 bcnt = rcnt;
751
752 /* Continue until collect up to a nembed page worth of sgls */
753 if (bcnt == 0)
754 continue;
755 /* Now, post the SCSI buffer list sgls as a block */
James Smart6d368e52011-05-24 11:44:12 -0400756 if (!phba->sli4_hba.extents_in_use)
757 status = lpfc_sli4_post_scsi_sgl_block(phba,
758 &sblist,
759 bcnt);
760 else
761 status = lpfc_sli4_post_scsi_sgl_blk_ext(phba,
762 &sblist,
763 bcnt);
James Smartda0436e2009-05-22 14:51:39 -0400764 /* Reset SCSI buffer count for next round of posting */
765 bcnt = 0;
766 while (!list_empty(&sblist)) {
767 list_remove_head(&sblist, psb, struct lpfc_scsi_buf,
768 list);
769 if (status) {
770 /* Put this back on the abort scsi list */
James Smart341af102010-01-26 23:07:37 -0500771 psb->exch_busy = 1;
James Smartda0436e2009-05-22 14:51:39 -0400772 rc++;
James Smart341af102010-01-26 23:07:37 -0500773 } else {
774 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400775 psb->status = IOSTAT_SUCCESS;
James Smart341af102010-01-26 23:07:37 -0500776 }
James Smartda0436e2009-05-22 14:51:39 -0400777 /* Put it back into the SCSI buffer list */
778 lpfc_release_scsi_buf_s4(phba, psb);
779 }
780 }
781 return rc;
782}
783
784/**
785 * lpfc_new_scsi_buf_s4 - Scsi buffer allocator for HBA with SLI4 IF spec
786 * @vport: The virtual port for which this call being executed.
787 * @num_to_allocate: The requested number of buffers to allocate.
788 *
789 * This routine allocates a scsi buffer for device with SLI-4 interface spec,
790 * the scsi buffer contains all the necessary information needed to initiate
791 * a SCSI I/O.
792 *
793 * Return codes:
794 * int - number of scsi buffers that were allocated.
795 * 0 = failure, less than num_to_alloc is a partial failure.
796 **/
797static int
798lpfc_new_scsi_buf_s4(struct lpfc_vport *vport, int num_to_alloc)
799{
800 struct lpfc_hba *phba = vport->phba;
801 struct lpfc_scsi_buf *psb;
802 struct sli4_sge *sgl;
803 IOCB_t *iocb;
804 dma_addr_t pdma_phys_fcp_cmd;
805 dma_addr_t pdma_phys_fcp_rsp;
806 dma_addr_t pdma_phys_bpl, pdma_phys_bpl1;
James Smart6d368e52011-05-24 11:44:12 -0400807 uint16_t iotag, last_xritag = NO_XRI, lxri = 0;
James Smartda0436e2009-05-22 14:51:39 -0400808 int status = 0, index;
809 int bcnt;
810 int non_sequential_xri = 0;
James Smartda0436e2009-05-22 14:51:39 -0400811 LIST_HEAD(sblist);
812
813 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
814 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
815 if (!psb)
816 break;
817
818 /*
819 * Get memory from the pci pool to map the virt space to pci bus
820 * space for an I/O. The DMA buffer includes space for the
821 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
822 * necessary to support the sg_tablesize.
823 */
824 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
825 GFP_KERNEL, &psb->dma_handle);
826 if (!psb->data) {
827 kfree(psb);
828 break;
829 }
830
831 /* Initialize virtual ptrs to dma_buf region. */
832 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
833
834 /* Allocate iotag for psb->cur_iocbq. */
835 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
836 if (iotag == 0) {
James Smartb92938b2010-06-07 15:24:12 -0400837 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
838 psb->data, psb->dma_handle);
James Smartda0436e2009-05-22 14:51:39 -0400839 kfree(psb);
840 break;
841 }
842
James Smart6d368e52011-05-24 11:44:12 -0400843 lxri = lpfc_sli4_next_xritag(phba);
844 if (lxri == NO_XRI) {
James Smartda0436e2009-05-22 14:51:39 -0400845 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
846 psb->data, psb->dma_handle);
847 kfree(psb);
848 break;
849 }
James Smart6d368e52011-05-24 11:44:12 -0400850 psb->cur_iocbq.sli4_lxritag = lxri;
851 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
James Smartda0436e2009-05-22 14:51:39 -0400852 if (last_xritag != NO_XRI
853 && psb->cur_iocbq.sli4_xritag != (last_xritag+1)) {
854 non_sequential_xri = 1;
855 } else
856 list_add_tail(&psb->list, &sblist);
857 last_xritag = psb->cur_iocbq.sli4_xritag;
858
859 index = phba->sli4_hba.scsi_xri_cnt++;
860 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
861
862 psb->fcp_bpl = psb->data;
863 psb->fcp_cmnd = (psb->data + phba->cfg_sg_dma_buf_size)
864 - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
865 psb->fcp_rsp = (struct fcp_rsp *)((uint8_t *)psb->fcp_cmnd +
866 sizeof(struct fcp_cmnd));
867
868 /* Initialize local short-hand pointers. */
869 sgl = (struct sli4_sge *)psb->fcp_bpl;
870 pdma_phys_bpl = psb->dma_handle;
871 pdma_phys_fcp_cmd =
872 (psb->dma_handle + phba->cfg_sg_dma_buf_size)
873 - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
874 pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
875
876 /*
877 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
878 * are sg list bdes. Initialize the first two and leave the
879 * rest for queuecommand.
880 */
881 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
882 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
James Smart05580562011-05-24 11:40:48 -0400883 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -0400884 bf_set(lpfc_sli4_sge_last, sgl, 0);
885 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -0500886 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_cmnd));
James Smartda0436e2009-05-22 14:51:39 -0400887 sgl++;
888
889 /* Setup the physical region for the FCP RSP */
890 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
891 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
James Smart05580562011-05-24 11:40:48 -0400892 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -0400893 bf_set(lpfc_sli4_sge_last, sgl, 1);
894 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -0500895 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
James Smartda0436e2009-05-22 14:51:39 -0400896
897 /*
898 * Since the IOCB for the FCP I/O is built into this
899 * lpfc_scsi_buf, initialize it with all known data now.
900 */
901 iocb = &psb->cur_iocbq.iocb;
902 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
903 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
904 /* setting the BLP size to 2 * sizeof BDE may not be correct.
905 * We are setting the bpl to point to out sgl. An sgl's
906 * entries are 16 bytes, a bpl entries are 12 bytes.
907 */
908 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
909 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
910 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
911 iocb->ulpBdeCount = 1;
912 iocb->ulpLe = 1;
913 iocb->ulpClass = CLASS3;
James Smarteee88772010-09-29 11:19:08 -0400914 psb->cur_iocbq.context1 = psb;
James Smartda0436e2009-05-22 14:51:39 -0400915 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
916 pdma_phys_bpl1 = pdma_phys_bpl + SGL_PAGE_SIZE;
917 else
918 pdma_phys_bpl1 = 0;
919 psb->dma_phys_bpl = pdma_phys_bpl;
920 phba->sli4_hba.lpfc_scsi_psb_array[index] = psb;
921 if (non_sequential_xri) {
922 status = lpfc_sli4_post_sgl(phba, pdma_phys_bpl,
923 pdma_phys_bpl1,
924 psb->cur_iocbq.sli4_xritag);
925 if (status) {
926 /* Put this back on the abort scsi list */
James Smart341af102010-01-26 23:07:37 -0500927 psb->exch_busy = 1;
James Smart341af102010-01-26 23:07:37 -0500928 } else {
929 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400930 psb->status = IOSTAT_SUCCESS;
James Smart341af102010-01-26 23:07:37 -0500931 }
James Smartda0436e2009-05-22 14:51:39 -0400932 /* Put it back into the SCSI buffer list */
933 lpfc_release_scsi_buf_s4(phba, psb);
934 break;
935 }
936 }
937 if (bcnt) {
James Smart6d368e52011-05-24 11:44:12 -0400938 if (!phba->sli4_hba.extents_in_use)
939 status = lpfc_sli4_post_scsi_sgl_block(phba,
940 &sblist,
941 bcnt);
942 else
943 status = lpfc_sli4_post_scsi_sgl_blk_ext(phba,
944 &sblist,
945 bcnt);
946
947 if (status) {
948 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
949 "3021 SCSI SGL post error %d\n",
950 status);
951 bcnt = 0;
952 }
James Smartda0436e2009-05-22 14:51:39 -0400953 /* Reset SCSI buffer count for next round of posting */
954 while (!list_empty(&sblist)) {
955 list_remove_head(&sblist, psb, struct lpfc_scsi_buf,
956 list);
957 if (status) {
958 /* Put this back on the abort scsi list */
James Smart341af102010-01-26 23:07:37 -0500959 psb->exch_busy = 1;
James Smart341af102010-01-26 23:07:37 -0500960 } else {
961 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400962 psb->status = IOSTAT_SUCCESS;
James Smart341af102010-01-26 23:07:37 -0500963 }
James Smartda0436e2009-05-22 14:51:39 -0400964 /* Put it back into the SCSI buffer list */
965 lpfc_release_scsi_buf_s4(phba, psb);
966 }
967 }
968
James Smartd7c47992010-06-08 18:31:54 -0400969 return bcnt + non_sequential_xri;
James Smartda0436e2009-05-22 14:51:39 -0400970}
971
972/**
James Smart3772a992009-05-22 14:50:54 -0400973 * lpfc_new_scsi_buf - Wrapper funciton for scsi buffer allocator
974 * @vport: The virtual port for which this call being executed.
975 * @num_to_allocate: The requested number of buffers to allocate.
976 *
977 * This routine wraps the actual SCSI buffer allocator function pointer from
978 * the lpfc_hba struct.
979 *
980 * Return codes:
981 * int - number of scsi buffers that were allocated.
982 * 0 = failure, less than num_to_alloc is a partial failure.
983 **/
984static inline int
985lpfc_new_scsi_buf(struct lpfc_vport *vport, int num_to_alloc)
986{
987 return vport->phba->lpfc_new_scsi_buf(vport, num_to_alloc);
988}
989
990/**
James Smart19ca7602010-11-20 23:11:55 -0500991 * lpfc_get_scsi_buf_s3 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
James Smart3772a992009-05-22 14:50:54 -0400992 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -0500993 *
994 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
995 * and returns to caller.
996 *
997 * Return codes:
998 * NULL - Error
999 * Pointer to lpfc_scsi_buf - Success
1000 **/
Adrian Bunk455c53e2006-01-06 20:21:28 +01001001static struct lpfc_scsi_buf*
James Smart19ca7602010-11-20 23:11:55 -05001002lpfc_get_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001003{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001004 struct lpfc_scsi_buf * lpfc_cmd = NULL;
1005 struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001006 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -05001007
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001008 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001009 list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
James Smart1dcb58e2007-04-25 09:51:30 -04001010 if (lpfc_cmd) {
1011 lpfc_cmd->seg_cnt = 0;
1012 lpfc_cmd->nonsg_phys = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05001013 lpfc_cmd->prot_seg_cnt = 0;
James Smart1dcb58e2007-04-25 09:51:30 -04001014 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001015 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001016 return lpfc_cmd;
1017}
James Smart19ca7602010-11-20 23:11:55 -05001018/**
1019 * lpfc_get_scsi_buf_s4 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1020 * @phba: The HBA for which this call is being executed.
1021 *
1022 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1023 * and returns to caller.
1024 *
1025 * Return codes:
1026 * NULL - Error
1027 * Pointer to lpfc_scsi_buf - Success
1028 **/
1029static struct lpfc_scsi_buf*
1030lpfc_get_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1031{
James Smart1151e3e2011-02-16 12:39:35 -05001032 struct lpfc_scsi_buf *lpfc_cmd ;
James Smart19ca7602010-11-20 23:11:55 -05001033 unsigned long iflag = 0;
1034 int found = 0;
1035
1036 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James Smart1151e3e2011-02-16 12:39:35 -05001037 list_for_each_entry(lpfc_cmd, &phba->lpfc_scsi_buf_list,
1038 list) {
James Smart19ca7602010-11-20 23:11:55 -05001039 if (lpfc_test_rrq_active(phba, ndlp,
James Smart1151e3e2011-02-16 12:39:35 -05001040 lpfc_cmd->cur_iocbq.sli4_xritag))
1041 continue;
1042 list_del(&lpfc_cmd->list);
James Smart19ca7602010-11-20 23:11:55 -05001043 found = 1;
1044 lpfc_cmd->seg_cnt = 0;
1045 lpfc_cmd->nonsg_phys = 0;
1046 lpfc_cmd->prot_seg_cnt = 0;
James Smart1151e3e2011-02-16 12:39:35 -05001047 break;
James Smart19ca7602010-11-20 23:11:55 -05001048 }
James Smart1151e3e2011-02-16 12:39:35 -05001049 spin_unlock_irqrestore(&phba->scsi_buf_list_lock,
1050 iflag);
1051 if (!found)
1052 return NULL;
1053 else
1054 return lpfc_cmd;
James Smart19ca7602010-11-20 23:11:55 -05001055}
1056/**
1057 * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1058 * @phba: The HBA for which this call is being executed.
1059 *
1060 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1061 * and returns to caller.
1062 *
1063 * Return codes:
1064 * NULL - Error
1065 * Pointer to lpfc_scsi_buf - Success
1066 **/
1067static struct lpfc_scsi_buf*
1068lpfc_get_scsi_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1069{
1070 return phba->lpfc_get_scsi_buf(phba, ndlp);
1071}
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001072
James Smart9bad7672008-12-04 22:39:02 -05001073/**
James Smart3772a992009-05-22 14:50:54 -04001074 * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
James Smart9bad7672008-12-04 22:39:02 -05001075 * @phba: The Hba for which this call is being executed.
1076 * @psb: The scsi buffer which is being released.
1077 *
1078 * This routine releases @psb scsi buffer by adding it to tail of @phba
1079 * lpfc_scsi_buf_list list.
1080 **/
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001081static void
James Smart3772a992009-05-22 14:50:54 -04001082lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001083{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001084 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -05001085
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001086 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001087 psb->pCmd = NULL;
dea31012005-04-17 16:05:31 -05001088 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001089 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
dea31012005-04-17 16:05:31 -05001090}
1091
James Smart9bad7672008-12-04 22:39:02 -05001092/**
James Smartda0436e2009-05-22 14:51:39 -04001093 * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
1094 * @phba: The Hba for which this call is being executed.
1095 * @psb: The scsi buffer which is being released.
1096 *
1097 * This routine releases @psb scsi buffer by adding it to tail of @phba
1098 * lpfc_scsi_buf_list list. For SLI4 XRI's are tied to the scsi buffer
1099 * and cannot be reused for at least RA_TOV amount of time if it was
1100 * aborted.
1101 **/
1102static void
1103lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1104{
1105 unsigned long iflag = 0;
1106
James Smart341af102010-01-26 23:07:37 -05001107 if (psb->exch_busy) {
James Smartda0436e2009-05-22 14:51:39 -04001108 spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock,
1109 iflag);
1110 psb->pCmd = NULL;
1111 list_add_tail(&psb->list,
1112 &phba->sli4_hba.lpfc_abts_scsi_buf_list);
1113 spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
1114 iflag);
1115 } else {
1116
1117 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
1118 psb->pCmd = NULL;
1119 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
1120 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
1121 }
1122}
1123
1124/**
James Smart3772a992009-05-22 14:50:54 -04001125 * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
1126 * @phba: The Hba for which this call is being executed.
1127 * @psb: The scsi buffer which is being released.
1128 *
1129 * This routine releases @psb scsi buffer by adding it to tail of @phba
1130 * lpfc_scsi_buf_list list.
1131 **/
1132static void
1133lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1134{
1135
1136 phba->lpfc_release_scsi_buf(phba, psb);
1137}
1138
1139/**
1140 * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -05001141 * @phba: The Hba for which this call is being executed.
1142 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1143 *
1144 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
James Smart3772a992009-05-22 14:50:54 -04001145 * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
1146 * through sg elements and format the bdea. This routine also initializes all
1147 * IOCB fields which are dependent on scsi command request buffer.
James Smart9bad7672008-12-04 22:39:02 -05001148 *
1149 * Return codes:
1150 * 1 - Error
1151 * 0 - Success
1152 **/
dea31012005-04-17 16:05:31 -05001153static int
James Smart3772a992009-05-22 14:50:54 -04001154lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
dea31012005-04-17 16:05:31 -05001155{
1156 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1157 struct scatterlist *sgel = NULL;
1158 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1159 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
James Smart0f65ff62010-02-26 14:14:23 -05001160 struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
dea31012005-04-17 16:05:31 -05001161 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart34b02dc2008-08-24 21:49:55 -04001162 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
dea31012005-04-17 16:05:31 -05001163 dma_addr_t physaddr;
James Smart34b02dc2008-08-24 21:49:55 -04001164 uint32_t num_bde = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001165 int nseg, datadir = scsi_cmnd->sc_data_direction;
dea31012005-04-17 16:05:31 -05001166
1167 /*
1168 * There are three possibilities here - use scatter-gather segment, use
1169 * the single mapping, or neither. Start the lpfc command prep by
1170 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1171 * data bde entry.
1172 */
1173 bpl += 2;
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001174 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05001175 /*
1176 * The driver stores the segment count returned from pci_map_sg
1177 * because this a count of dma-mappings used to map the use_sg
1178 * pages. They are not guaranteed to be the same for those
1179 * architectures that implement an IOMMU.
1180 */
dea31012005-04-17 16:05:31 -05001181
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001182 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
1183 scsi_sg_count(scsi_cmnd), datadir);
1184 if (unlikely(!nseg))
1185 return 1;
1186
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001187 lpfc_cmd->seg_cnt = nseg;
dea31012005-04-17 16:05:31 -05001188 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001189 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1190 "9064 BLKGRD: %s: Too many sg segments from "
James Smarte2a0a9d2008-12-04 22:40:02 -05001191 "dma_map_sg. Config %d, seg_cnt %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001192 __func__, phba->cfg_sg_seg_cnt,
dea31012005-04-17 16:05:31 -05001193 lpfc_cmd->seg_cnt);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001194 scsi_dma_unmap(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001195 return 1;
1196 }
1197
1198 /*
1199 * The driver established a maximum scatter-gather segment count
1200 * during probe that limits the number of sg elements in any
1201 * single scsi command. Just run through the seg_cnt and format
1202 * the bde's.
James Smart34b02dc2008-08-24 21:49:55 -04001203 * When using SLI-3 the driver will try to fit all the BDEs into
1204 * the IOCB. If it can't then the BDEs get added to a BPL as it
1205 * does for SLI-2 mode.
dea31012005-04-17 16:05:31 -05001206 */
James Smart34b02dc2008-08-24 21:49:55 -04001207 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
dea31012005-04-17 16:05:31 -05001208 physaddr = sg_dma_address(sgel);
James Smart34b02dc2008-08-24 21:49:55 -04001209 if (phba->sli_rev == 3 &&
James Smarte2a0a9d2008-12-04 22:40:02 -05001210 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
James Smart0f65ff62010-02-26 14:14:23 -05001211 !(iocbq->iocb_flag & DSS_SECURITY_OP) &&
James Smart34b02dc2008-08-24 21:49:55 -04001212 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
1213 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1214 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
1215 data_bde->addrLow = putPaddrLow(physaddr);
1216 data_bde->addrHigh = putPaddrHigh(physaddr);
1217 data_bde++;
1218 } else {
1219 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1220 bpl->tus.f.bdeSize = sg_dma_len(sgel);
1221 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1222 bpl->addrLow =
1223 le32_to_cpu(putPaddrLow(physaddr));
1224 bpl->addrHigh =
1225 le32_to_cpu(putPaddrHigh(physaddr));
1226 bpl++;
1227 }
dea31012005-04-17 16:05:31 -05001228 }
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001229 }
dea31012005-04-17 16:05:31 -05001230
1231 /*
1232 * Finish initializing those IOCB fields that are dependent on the
James Smart34b02dc2008-08-24 21:49:55 -04001233 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
1234 * explicitly reinitialized and for SLI-3 the extended bde count is
1235 * explicitly reinitialized since all iocb memory resources are reused.
dea31012005-04-17 16:05:31 -05001236 */
James Smarte2a0a9d2008-12-04 22:40:02 -05001237 if (phba->sli_rev == 3 &&
James Smart0f65ff62010-02-26 14:14:23 -05001238 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
1239 !(iocbq->iocb_flag & DSS_SECURITY_OP)) {
James Smart34b02dc2008-08-24 21:49:55 -04001240 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
1241 /*
1242 * The extended IOCB format can only fit 3 BDE or a BPL.
1243 * This I/O has more than 3 BDE so the 1st data bde will
1244 * be a BPL that is filled in here.
1245 */
1246 physaddr = lpfc_cmd->dma_handle;
1247 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
1248 data_bde->tus.f.bdeSize = (num_bde *
1249 sizeof(struct ulp_bde64));
1250 physaddr += (sizeof(struct fcp_cmnd) +
1251 sizeof(struct fcp_rsp) +
1252 (2 * sizeof(struct ulp_bde64)));
1253 data_bde->addrHigh = putPaddrHigh(physaddr);
1254 data_bde->addrLow = putPaddrLow(physaddr);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001255 /* ebde count includes the response bde and data bpl */
James Smart34b02dc2008-08-24 21:49:55 -04001256 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
1257 } else {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001258 /* ebde count includes the response bde and data bdes */
James Smart34b02dc2008-08-24 21:49:55 -04001259 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1260 }
1261 } else {
1262 iocb_cmd->un.fcpi64.bdl.bdeSize =
1263 ((num_bde + 2) * sizeof(struct ulp_bde64));
James Smart0f65ff62010-02-26 14:14:23 -05001264 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
James Smart34b02dc2008-08-24 21:49:55 -04001265 }
James Smart09372822008-01-11 01:52:54 -05001266 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
James Smarte2a0a9d2008-12-04 22:40:02 -05001267
1268 /*
1269 * Due to difference in data length between DIF/non-DIF paths,
1270 * we need to set word 4 of IOCB here
1271 */
James Smarta257bf92009-04-06 18:48:10 -04001272 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001273 return 0;
1274}
1275
James Smartf9bb2da2011-10-10 21:34:11 -04001276static inline unsigned
1277lpfc_cmd_blksize(struct scsi_cmnd *sc)
1278{
1279 return sc->device->sector_size;
1280}
1281
1282#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001283
1284#define BG_ERR_INIT 1
1285#define BG_ERR_TGT 2
1286#define BG_ERR_SWAP 3
1287#define BG_ERR_CHECK 4
1288
1289/**
1290 * lpfc_bg_err_inject - Determine if we should inject an error
1291 * @phba: The Hba for which this call is being executed.
James Smartf9bb2da2011-10-10 21:34:11 -04001292 * @sc: The SCSI command to examine
1293 * @reftag: (out) BlockGuard reference tag for transmitted data
1294 * @apptag: (out) BlockGuard application tag for transmitted data
1295 * @new_guard (in) Value to replace CRC with if needed
1296 *
James Smartacd68592012-01-18 16:25:09 -05001297 * Returns (1) if error injection is detected by Initiator
1298 * Returns (2) if error injection is detected by Target
1299 * Returns (3) if swapping CSUM->CRC is required for error injection
1300 * Returns (4) disabling Guard/Ref/App checking is required for error injection
1301 **/
James Smartf9bb2da2011-10-10 21:34:11 -04001302static int
1303lpfc_bg_err_inject(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1304 uint32_t *reftag, uint16_t *apptag, uint32_t new_guard)
1305{
1306 struct scatterlist *sgpe; /* s/g prot entry */
1307 struct scatterlist *sgde; /* s/g data entry */
James Smartacd68592012-01-18 16:25:09 -05001308 struct scsi_dif_tuple *src = NULL;
James Smartf9bb2da2011-10-10 21:34:11 -04001309 uint32_t op = scsi_get_prot_op(sc);
1310 uint32_t blksize;
1311 uint32_t numblks;
1312 sector_t lba;
1313 int rc = 0;
James Smartacd68592012-01-18 16:25:09 -05001314 int blockoff = 0;
James Smartf9bb2da2011-10-10 21:34:11 -04001315
1316 if (op == SCSI_PROT_NORMAL)
1317 return 0;
1318
James Smartacd68592012-01-18 16:25:09 -05001319 sgpe = scsi_prot_sglist(sc);
1320 sgde = scsi_sglist(sc);
1321
James Smartf9bb2da2011-10-10 21:34:11 -04001322 lba = scsi_get_lba(sc);
1323 if (phba->lpfc_injerr_lba != LPFC_INJERR_LBA_OFF) {
1324 blksize = lpfc_cmd_blksize(sc);
1325 numblks = (scsi_bufflen(sc) + blksize - 1) / blksize;
1326
1327 /* Make sure we have the right LBA if one is specified */
1328 if ((phba->lpfc_injerr_lba < lba) ||
1329 (phba->lpfc_injerr_lba >= (lba + numblks)))
1330 return 0;
James Smartacd68592012-01-18 16:25:09 -05001331 if (sgpe) {
1332 blockoff = phba->lpfc_injerr_lba - lba;
1333 numblks = sg_dma_len(sgpe) /
1334 sizeof(struct scsi_dif_tuple);
1335 if (numblks < blockoff)
1336 blockoff = numblks;
1337 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
1338 src += blockoff;
1339 }
James Smartf9bb2da2011-10-10 21:34:11 -04001340 }
1341
James Smartf9bb2da2011-10-10 21:34:11 -04001342 /* Should we change the Reference Tag */
1343 if (reftag) {
James Smartacd68592012-01-18 16:25:09 -05001344 if (phba->lpfc_injerr_wref_cnt) {
1345 switch (op) {
1346 case SCSI_PROT_WRITE_PASS:
1347 if (blockoff && src) {
1348 /* Insert error in middle of the IO */
James Smartf9bb2da2011-10-10 21:34:11 -04001349
James Smartacd68592012-01-18 16:25:09 -05001350 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1351 "9076 BLKGRD: Injecting reftag error: "
1352 "write lba x%lx + x%x oldrefTag x%x\n",
1353 (unsigned long)lba, blockoff,
1354 src->ref_tag);
1355
1356 /*
1357 * NOTE, this will change ref tag in
1358 * the memory location forever!
1359 */
1360 src->ref_tag = 0xDEADBEEF;
1361 phba->lpfc_injerr_wref_cnt--;
1362 phba->lpfc_injerr_lba =
1363 LPFC_INJERR_LBA_OFF;
1364 rc = BG_ERR_CHECK;
1365 break;
1366 }
1367 /* Drop thru */
1368 case SCSI_PROT_WRITE_STRIP:
1369 /*
1370 * For WRITE_STRIP and WRITE_PASS,
1371 * force the error on data
1372 * being copied from SLI-Host to SLI-Port.
1373 */
1374 *reftag = 0xDEADBEEF;
1375 phba->lpfc_injerr_wref_cnt--;
1376 phba->lpfc_injerr_lba = LPFC_INJERR_LBA_OFF;
1377 rc = BG_ERR_INIT;
1378
1379 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1380 "9077 BLKGRD: Injecting reftag error: "
1381 "write lba x%lx\n", (unsigned long)lba);
1382 break;
1383 case SCSI_PROT_WRITE_INSERT:
1384 /*
1385 * For WRITE_INSERT, force the
1386 * error to be sent on the wire. It should be
1387 * detected by the Target.
1388 */
James Smartf9bb2da2011-10-10 21:34:11 -04001389 /* DEADBEEF will be the reftag on the wire */
1390 *reftag = 0xDEADBEEF;
1391 phba->lpfc_injerr_wref_cnt--;
1392 phba->lpfc_injerr_lba = LPFC_INJERR_LBA_OFF;
James Smartacd68592012-01-18 16:25:09 -05001393 rc = BG_ERR_TGT;
James Smartf9bb2da2011-10-10 21:34:11 -04001394
1395 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smartacd68592012-01-18 16:25:09 -05001396 "9078 BLKGRD: Injecting reftag error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001397 "write lba x%lx\n", (unsigned long)lba);
James Smartacd68592012-01-18 16:25:09 -05001398 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001399 }
James Smartacd68592012-01-18 16:25:09 -05001400 }
1401 if (phba->lpfc_injerr_rref_cnt) {
1402 switch (op) {
1403 case SCSI_PROT_READ_INSERT:
1404 /*
1405 * For READ_INSERT, it doesn't make sense
1406 * to change the reftag.
1407 */
1408 break;
1409 case SCSI_PROT_READ_STRIP:
1410 case SCSI_PROT_READ_PASS:
1411 /*
1412 * For READ_STRIP and READ_PASS, force the
1413 * error on data being read off the wire. It
1414 * should force an IO error to the driver.
1415 */
James Smartf9bb2da2011-10-10 21:34:11 -04001416 *reftag = 0xDEADBEEF;
1417 phba->lpfc_injerr_rref_cnt--;
1418 phba->lpfc_injerr_lba = LPFC_INJERR_LBA_OFF;
James Smartacd68592012-01-18 16:25:09 -05001419 rc = BG_ERR_INIT;
James Smartf9bb2da2011-10-10 21:34:11 -04001420
1421 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smartacd68592012-01-18 16:25:09 -05001422 "9079 BLKGRD: Injecting reftag error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001423 "read lba x%lx\n", (unsigned long)lba);
James Smartacd68592012-01-18 16:25:09 -05001424 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001425 }
1426 }
1427 }
1428
1429 /* Should we change the Application Tag */
1430 if (apptag) {
James Smartacd68592012-01-18 16:25:09 -05001431 if (phba->lpfc_injerr_wapp_cnt) {
1432 switch (op) {
1433 case SCSI_PROT_WRITE_PASS:
1434 if (blockoff && src) {
1435 /* Insert error in middle of the IO */
James Smartf9bb2da2011-10-10 21:34:11 -04001436
James Smartacd68592012-01-18 16:25:09 -05001437 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1438 "9080 BLKGRD: Injecting apptag error: "
1439 "write lba x%lx + x%x oldappTag x%x\n",
1440 (unsigned long)lba, blockoff,
1441 src->app_tag);
1442
1443 /*
1444 * NOTE, this will change app tag in
1445 * the memory location forever!
1446 */
1447 src->app_tag = 0xDEAD;
1448 phba->lpfc_injerr_wapp_cnt--;
1449 phba->lpfc_injerr_lba =
1450 LPFC_INJERR_LBA_OFF;
1451 rc = BG_ERR_CHECK;
1452 break;
1453 }
1454 /* Drop thru */
1455 case SCSI_PROT_WRITE_STRIP:
1456 /*
1457 * For WRITE_STRIP and WRITE_PASS,
1458 * force the error on data
1459 * being copied from SLI-Host to SLI-Port.
1460 */
1461 *apptag = 0xDEAD;
1462 phba->lpfc_injerr_wapp_cnt--;
1463 phba->lpfc_injerr_lba = LPFC_INJERR_LBA_OFF;
1464 rc = BG_ERR_INIT;
1465
1466 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1467 "0812 BLKGRD: Injecting apptag error: "
1468 "write lba x%lx\n", (unsigned long)lba);
1469 break;
1470 case SCSI_PROT_WRITE_INSERT:
1471 /*
1472 * For WRITE_INSERT, force the
1473 * error to be sent on the wire. It should be
1474 * detected by the Target.
1475 */
James Smartf9bb2da2011-10-10 21:34:11 -04001476 /* DEAD will be the apptag on the wire */
1477 *apptag = 0xDEAD;
1478 phba->lpfc_injerr_wapp_cnt--;
1479 phba->lpfc_injerr_lba = LPFC_INJERR_LBA_OFF;
James Smartacd68592012-01-18 16:25:09 -05001480 rc = BG_ERR_TGT;
James Smartf9bb2da2011-10-10 21:34:11 -04001481
1482 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smartacd68592012-01-18 16:25:09 -05001483 "0813 BLKGRD: Injecting apptag error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001484 "write lba x%lx\n", (unsigned long)lba);
James Smartacd68592012-01-18 16:25:09 -05001485 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001486 }
James Smartacd68592012-01-18 16:25:09 -05001487 }
1488 if (phba->lpfc_injerr_rapp_cnt) {
1489 switch (op) {
1490 case SCSI_PROT_READ_INSERT:
1491 /*
1492 * For READ_INSERT, it doesn't make sense
1493 * to change the apptag.
1494 */
1495 break;
1496 case SCSI_PROT_READ_STRIP:
1497 case SCSI_PROT_READ_PASS:
1498 /*
1499 * For READ_STRIP and READ_PASS, force the
1500 * error on data being read off the wire. It
1501 * should force an IO error to the driver.
1502 */
James Smartf9bb2da2011-10-10 21:34:11 -04001503 *apptag = 0xDEAD;
1504 phba->lpfc_injerr_rapp_cnt--;
1505 phba->lpfc_injerr_lba = LPFC_INJERR_LBA_OFF;
James Smartacd68592012-01-18 16:25:09 -05001506 rc = BG_ERR_INIT;
James Smartf9bb2da2011-10-10 21:34:11 -04001507
1508 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smartacd68592012-01-18 16:25:09 -05001509 "0814 BLKGRD: Injecting apptag error: "
1510 "read lba x%lx\n", (unsigned long)lba);
1511 break;
1512 }
1513 }
1514 }
1515
1516
1517 /* Should we change the Guard Tag */
1518 if (new_guard) {
1519 if (phba->lpfc_injerr_wgrd_cnt) {
1520 switch (op) {
1521 case SCSI_PROT_WRITE_PASS:
1522 if (blockoff && src) {
1523 /* Insert error in middle of the IO */
1524
1525 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1526 "0815 BLKGRD: Injecting guard error: "
1527 "write lba x%lx + x%x oldgrdTag x%x\n",
1528 (unsigned long)lba, blockoff,
1529 src->guard_tag);
1530
1531 /*
1532 * NOTE, this will change guard tag in
1533 * the memory location forever!
1534 */
1535 src->guard_tag = 0xDEAD;
1536 phba->lpfc_injerr_wgrd_cnt--;
1537 phba->lpfc_injerr_lba =
1538 LPFC_INJERR_LBA_OFF;
1539 rc = BG_ERR_CHECK;
1540 break;
1541 }
1542 /* Drop thru */
1543 case SCSI_PROT_WRITE_STRIP:
1544 /*
1545 * For WRITE_STRIP and WRITE_PASS,
1546 * force the error on data
1547 * being copied from SLI-Host to SLI-Port.
1548 */
1549 phba->lpfc_injerr_wgrd_cnt--;
1550 phba->lpfc_injerr_lba = LPFC_INJERR_LBA_OFF;
1551
1552 rc = BG_ERR_SWAP;
1553 /* Signals the caller to swap CRC->CSUM */
1554
1555 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1556 "0816 BLKGRD: Injecting guard error: "
1557 "write lba x%lx\n", (unsigned long)lba);
1558 break;
1559 case SCSI_PROT_WRITE_INSERT:
1560 /*
1561 * For WRITE_INSERT, force the
1562 * error to be sent on the wire. It should be
1563 * detected by the Target.
1564 */
1565 phba->lpfc_injerr_wgrd_cnt--;
1566 phba->lpfc_injerr_lba = LPFC_INJERR_LBA_OFF;
1567
1568 rc = BG_ERR_SWAP;
1569 /* Signals the caller to swap CRC->CSUM */
1570
1571 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1572 "0817 BLKGRD: Injecting guard error: "
1573 "write lba x%lx\n", (unsigned long)lba);
1574 break;
1575 }
1576 }
1577 if (phba->lpfc_injerr_rgrd_cnt) {
1578 switch (op) {
1579 case SCSI_PROT_READ_INSERT:
1580 /*
1581 * For READ_INSERT, it doesn't make sense
1582 * to change the guard tag.
1583 */
1584 break;
1585 case SCSI_PROT_READ_STRIP:
1586 case SCSI_PROT_READ_PASS:
1587 /*
1588 * For READ_STRIP and READ_PASS, force the
1589 * error on data being read off the wire. It
1590 * should force an IO error to the driver.
1591 */
1592 *apptag = 0xDEAD;
1593 phba->lpfc_injerr_rgrd_cnt--;
1594 phba->lpfc_injerr_lba = LPFC_INJERR_LBA_OFF;
1595
1596 rc = BG_ERR_SWAP;
1597 /* Signals the caller to swap CRC->CSUM */
1598
1599 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1600 "0818 BLKGRD: Injecting guard error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001601 "read lba x%lx\n", (unsigned long)lba);
1602 }
1603 }
1604 }
1605
James Smartf9bb2da2011-10-10 21:34:11 -04001606 return rc;
1607}
1608#endif
1609
James Smartacd68592012-01-18 16:25:09 -05001610/**
1611 * lpfc_sc_to_bg_opcodes - Determine the BlockGuard opcodes to be used with
1612 * the specified SCSI command.
1613 * @phba: The Hba for which this call is being executed.
James Smart6c8eea52010-04-06 14:49:53 -04001614 * @sc: The SCSI command to examine
1615 * @txopt: (out) BlockGuard operation for transmitted data
1616 * @rxopt: (out) BlockGuard operation for received data
1617 *
1618 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1619 *
James Smartacd68592012-01-18 16:25:09 -05001620 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001621static int
James Smart6c8eea52010-04-06 14:49:53 -04001622lpfc_sc_to_bg_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1623 uint8_t *txop, uint8_t *rxop)
James Smarte2a0a9d2008-12-04 22:40:02 -05001624{
1625 uint8_t guard_type = scsi_host_get_guard(sc->device->host);
James Smart6c8eea52010-04-06 14:49:53 -04001626 uint8_t ret = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05001627
1628 if (guard_type == SHOST_DIX_GUARD_IP) {
1629 switch (scsi_get_prot_op(sc)) {
1630 case SCSI_PROT_READ_INSERT:
1631 case SCSI_PROT_WRITE_STRIP:
James Smart6c8eea52010-04-06 14:49:53 -04001632 *txop = BG_OP_IN_CSUM_OUT_NODIF;
1633 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smarte2a0a9d2008-12-04 22:40:02 -05001634 break;
1635
1636 case SCSI_PROT_READ_STRIP:
1637 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001638 *txop = BG_OP_IN_NODIF_OUT_CRC;
1639 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smarte2a0a9d2008-12-04 22:40:02 -05001640 break;
1641
Martin K. Petersenc6af4042009-09-18 17:32:59 -04001642 case SCSI_PROT_READ_PASS:
1643 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001644 *txop = BG_OP_IN_CSUM_OUT_CRC;
1645 *rxop = BG_OP_IN_CRC_OUT_CSUM;
James Smarte2a0a9d2008-12-04 22:40:02 -05001646 break;
1647
James Smarte2a0a9d2008-12-04 22:40:02 -05001648 case SCSI_PROT_NORMAL:
1649 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001650 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001651 "9063 BLKGRD: Bad op/guard:%d/IP combination\n",
1652 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001653 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001654 break;
1655
1656 }
James Smart7c56b9f2011-07-22 18:36:25 -04001657 } else {
James Smarte2a0a9d2008-12-04 22:40:02 -05001658 switch (scsi_get_prot_op(sc)) {
1659 case SCSI_PROT_READ_STRIP:
1660 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001661 *txop = BG_OP_IN_NODIF_OUT_CRC;
1662 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smarte2a0a9d2008-12-04 22:40:02 -05001663 break;
1664
1665 case SCSI_PROT_READ_PASS:
1666 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001667 *txop = BG_OP_IN_CRC_OUT_CRC;
1668 *rxop = BG_OP_IN_CRC_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001669 break;
1670
James Smarte2a0a9d2008-12-04 22:40:02 -05001671 case SCSI_PROT_READ_INSERT:
1672 case SCSI_PROT_WRITE_STRIP:
James Smart7c56b9f2011-07-22 18:36:25 -04001673 *txop = BG_OP_IN_CRC_OUT_NODIF;
1674 *rxop = BG_OP_IN_NODIF_OUT_CRC;
1675 break;
1676
James Smarte2a0a9d2008-12-04 22:40:02 -05001677 case SCSI_PROT_NORMAL:
1678 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001679 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001680 "9075 BLKGRD: Bad op/guard:%d/CRC combination\n",
1681 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001682 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001683 break;
1684 }
James Smarte2a0a9d2008-12-04 22:40:02 -05001685 }
1686
James Smart6c8eea52010-04-06 14:49:53 -04001687 return ret;
James Smarte2a0a9d2008-12-04 22:40:02 -05001688}
1689
James Smartacd68592012-01-18 16:25:09 -05001690#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1691/**
1692 * lpfc_bg_err_opcodes - reDetermine the BlockGuard opcodes to be used with
1693 * the specified SCSI command in order to force a guard tag error.
1694 * @phba: The Hba for which this call is being executed.
1695 * @sc: The SCSI command to examine
1696 * @txopt: (out) BlockGuard operation for transmitted data
1697 * @rxopt: (out) BlockGuard operation for received data
1698 *
1699 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1700 *
1701 **/
1702static int
1703lpfc_bg_err_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1704 uint8_t *txop, uint8_t *rxop)
1705{
1706 uint8_t guard_type = scsi_host_get_guard(sc->device->host);
1707 uint8_t ret = 0;
1708
1709 if (guard_type == SHOST_DIX_GUARD_IP) {
1710 switch (scsi_get_prot_op(sc)) {
1711 case SCSI_PROT_READ_INSERT:
1712 case SCSI_PROT_WRITE_STRIP:
1713 *txop = BG_OP_IN_CRC_OUT_NODIF;
1714 *rxop = BG_OP_IN_NODIF_OUT_CRC;
1715 break;
1716
1717 case SCSI_PROT_READ_STRIP:
1718 case SCSI_PROT_WRITE_INSERT:
1719 *txop = BG_OP_IN_NODIF_OUT_CSUM;
1720 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
1721 break;
1722
1723 case SCSI_PROT_READ_PASS:
1724 case SCSI_PROT_WRITE_PASS:
1725 *txop = BG_OP_IN_CRC_OUT_CRC;
1726 *rxop = BG_OP_IN_CRC_OUT_CRC;
1727 break;
1728
1729 case SCSI_PROT_NORMAL:
1730 default:
1731 break;
1732
1733 }
1734 } else {
1735 switch (scsi_get_prot_op(sc)) {
1736 case SCSI_PROT_READ_STRIP:
1737 case SCSI_PROT_WRITE_INSERT:
1738 *txop = BG_OP_IN_NODIF_OUT_CSUM;
1739 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
1740 break;
1741
1742 case SCSI_PROT_READ_PASS:
1743 case SCSI_PROT_WRITE_PASS:
1744 *txop = BG_OP_IN_CSUM_OUT_CRC;
1745 *rxop = BG_OP_IN_CRC_OUT_CSUM;
1746 break;
1747
1748 case SCSI_PROT_READ_INSERT:
1749 case SCSI_PROT_WRITE_STRIP:
1750 *txop = BG_OP_IN_CSUM_OUT_NODIF;
1751 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
1752 break;
1753
1754 case SCSI_PROT_NORMAL:
1755 default:
1756 break;
1757 }
1758 }
1759
1760 return ret;
1761}
1762#endif
1763
1764/**
1765 * lpfc_bg_setup_bpl - Setup BlockGuard BPL with no protection data
1766 * @phba: The Hba for which this call is being executed.
1767 * @sc: pointer to scsi command we're working on
1768 * @bpl: pointer to buffer list for protection groups
1769 * @datacnt: number of segments of data that have been dma mapped
1770 *
1771 * This function sets up BPL buffer list for protection groups of
James Smarte2a0a9d2008-12-04 22:40:02 -05001772 * type LPFC_PG_TYPE_NO_DIF
1773 *
1774 * This is usually used when the HBA is instructed to generate
1775 * DIFs and insert them into data stream (or strip DIF from
1776 * incoming data stream)
1777 *
1778 * The buffer list consists of just one protection group described
1779 * below:
1780 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001781 * start of prot group --> | PDE_5 |
1782 * +-------------------------+
1783 * | PDE_6 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001784 * +-------------------------+
1785 * | Data BDE |
1786 * +-------------------------+
1787 * |more Data BDE's ... (opt)|
1788 * +-------------------------+
1789 *
James Smarte2a0a9d2008-12-04 22:40:02 -05001790 *
1791 * Note: Data s/g buffers have been dma mapped
James Smartacd68592012-01-18 16:25:09 -05001792 *
1793 * Returns the number of BDEs added to the BPL.
1794 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001795static int
1796lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1797 struct ulp_bde64 *bpl, int datasegcnt)
1798{
1799 struct scatterlist *sgde = NULL; /* s/g data entry */
James Smart6c8eea52010-04-06 14:49:53 -04001800 struct lpfc_pde5 *pde5 = NULL;
1801 struct lpfc_pde6 *pde6 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05001802 dma_addr_t physaddr;
James Smart6c8eea52010-04-06 14:49:53 -04001803 int i = 0, num_bde = 0, status;
James Smarte2a0a9d2008-12-04 22:40:02 -05001804 int datadir = sc->sc_data_direction;
James Smartacd68592012-01-18 16:25:09 -05001805 uint32_t rc;
1806 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001807 uint32_t reftag;
James Smart7c56b9f2011-07-22 18:36:25 -04001808 unsigned blksize;
James Smart6c8eea52010-04-06 14:49:53 -04001809 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05001810
James Smart6c8eea52010-04-06 14:49:53 -04001811 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1812 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05001813 goto out;
1814
James Smart6c8eea52010-04-06 14:49:53 -04001815 /* extract some info from the scsi command for pde*/
James Smarte2a0a9d2008-12-04 22:40:02 -05001816 blksize = lpfc_cmd_blksize(sc);
James Smartacd68592012-01-18 16:25:09 -05001817 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
James Smarte2a0a9d2008-12-04 22:40:02 -05001818
James Smartf9bb2da2011-10-10 21:34:11 -04001819#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001820 rc = lpfc_bg_err_inject(phba, sc, &reftag, 0, 1);
1821 if (rc) {
1822 if (rc == BG_ERR_SWAP)
1823 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
1824 if (rc == BG_ERR_CHECK)
1825 checking = 0;
1826 }
James Smartf9bb2da2011-10-10 21:34:11 -04001827#endif
1828
James Smart6c8eea52010-04-06 14:49:53 -04001829 /* setup PDE5 with what we have */
1830 pde5 = (struct lpfc_pde5 *) bpl;
1831 memset(pde5, 0, sizeof(struct lpfc_pde5));
1832 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05001833
James Smartbc739052010-08-04 16:11:18 -04001834 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04001835 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04001836 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04001837
James Smart6c8eea52010-04-06 14:49:53 -04001838 /* advance bpl and increment bde count */
1839 num_bde++;
1840 bpl++;
1841 pde6 = (struct lpfc_pde6 *) bpl;
1842
1843 /* setup PDE6 with the rest of the info */
1844 memset(pde6, 0, sizeof(struct lpfc_pde6));
1845 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
1846 bf_set(pde6_optx, pde6, txop);
1847 bf_set(pde6_oprx, pde6, rxop);
1848 if (datadir == DMA_FROM_DEVICE) {
James Smartacd68592012-01-18 16:25:09 -05001849 bf_set(pde6_ce, pde6, checking);
1850 bf_set(pde6_re, pde6, checking);
James Smart6c8eea52010-04-06 14:49:53 -04001851 }
1852 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04001853 bf_set(pde6_ae, pde6, 0);
1854 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04001855
James Smartbc739052010-08-04 16:11:18 -04001856 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04001857 pde6->word0 = cpu_to_le32(pde6->word0);
1858 pde6->word1 = cpu_to_le32(pde6->word1);
1859 pde6->word2 = cpu_to_le32(pde6->word2);
1860
James Smart6c8eea52010-04-06 14:49:53 -04001861 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05001862 num_bde++;
1863 bpl++;
1864
1865 /* assumption: caller has already run dma_map_sg on command data */
1866 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
1867 physaddr = sg_dma_address(sgde);
1868 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
1869 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1870 bpl->tus.f.bdeSize = sg_dma_len(sgde);
1871 if (datadir == DMA_TO_DEVICE)
1872 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1873 else
1874 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1875 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1876 bpl++;
1877 num_bde++;
1878 }
1879
1880out:
1881 return num_bde;
1882}
1883
James Smartacd68592012-01-18 16:25:09 -05001884/**
1885 * lpfc_bg_setup_bpl_prot - Setup BlockGuard BPL with protection data
1886 * @phba: The Hba for which this call is being executed.
1887 * @sc: pointer to scsi command we're working on
1888 * @bpl: pointer to buffer list for protection groups
1889 * @datacnt: number of segments of data that have been dma mapped
1890 * @protcnt: number of segment of protection data that have been dma mapped
1891 *
1892 * This function sets up BPL buffer list for protection groups of
1893 * type LPFC_PG_TYPE_DIF
James Smarte2a0a9d2008-12-04 22:40:02 -05001894 *
1895 * This is usually used when DIFs are in their own buffers,
1896 * separate from the data. The HBA can then by instructed
1897 * to place the DIFs in the outgoing stream. For read operations,
1898 * The HBA could extract the DIFs and place it in DIF buffers.
1899 *
1900 * The buffer list for this type consists of one or more of the
1901 * protection groups described below:
1902 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001903 * start of first prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001904 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001905 * | PDE_6 |
1906 * +-------------------------+
1907 * | PDE_7 (Prot BDE) |
James Smarte2a0a9d2008-12-04 22:40:02 -05001908 * +-------------------------+
1909 * | Data BDE |
1910 * +-------------------------+
1911 * |more Data BDE's ... (opt)|
1912 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001913 * start of new prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001914 * +-------------------------+
1915 * | ... |
1916 * +-------------------------+
1917 *
James Smarte2a0a9d2008-12-04 22:40:02 -05001918 * Note: It is assumed that both data and protection s/g buffers have been
1919 * mapped for DMA
James Smartacd68592012-01-18 16:25:09 -05001920 *
1921 * Returns the number of BDEs added to the BPL.
1922 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001923static int
1924lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1925 struct ulp_bde64 *bpl, int datacnt, int protcnt)
1926{
1927 struct scatterlist *sgde = NULL; /* s/g data entry */
1928 struct scatterlist *sgpe = NULL; /* s/g prot entry */
James Smart6c8eea52010-04-06 14:49:53 -04001929 struct lpfc_pde5 *pde5 = NULL;
1930 struct lpfc_pde6 *pde6 = NULL;
James Smart7f860592011-03-11 16:05:52 -05001931 struct lpfc_pde7 *pde7 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05001932 dma_addr_t dataphysaddr, protphysaddr;
1933 unsigned short curr_data = 0, curr_prot = 0;
James Smart7f860592011-03-11 16:05:52 -05001934 unsigned int split_offset;
1935 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
James Smarte2a0a9d2008-12-04 22:40:02 -05001936 unsigned int protgrp_blks, protgrp_bytes;
1937 unsigned int remainder, subtotal;
James Smart6c8eea52010-04-06 14:49:53 -04001938 int status;
James Smarte2a0a9d2008-12-04 22:40:02 -05001939 int datadir = sc->sc_data_direction;
1940 unsigned char pgdone = 0, alldone = 0;
1941 unsigned blksize;
James Smartacd68592012-01-18 16:25:09 -05001942 uint32_t rc;
1943 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001944 uint32_t reftag;
James Smart6c8eea52010-04-06 14:49:53 -04001945 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05001946 int num_bde = 0;
1947
1948 sgpe = scsi_prot_sglist(sc);
1949 sgde = scsi_sglist(sc);
1950
1951 if (!sgpe || !sgde) {
1952 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1953 "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
1954 sgpe, sgde);
1955 return 0;
1956 }
1957
James Smart6c8eea52010-04-06 14:49:53 -04001958 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1959 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05001960 goto out;
1961
James Smart6c8eea52010-04-06 14:49:53 -04001962 /* extract some info from the scsi command */
James Smarte2a0a9d2008-12-04 22:40:02 -05001963 blksize = lpfc_cmd_blksize(sc);
James Smartacd68592012-01-18 16:25:09 -05001964 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
James Smarte2a0a9d2008-12-04 22:40:02 -05001965
James Smartf9bb2da2011-10-10 21:34:11 -04001966#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001967 rc = lpfc_bg_err_inject(phba, sc, &reftag, 0, 1);
1968 if (rc) {
1969 if (rc == BG_ERR_SWAP)
1970 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
1971 if (rc == BG_ERR_CHECK)
1972 checking = 0;
1973 }
James Smartf9bb2da2011-10-10 21:34:11 -04001974#endif
1975
James Smarte2a0a9d2008-12-04 22:40:02 -05001976 split_offset = 0;
1977 do {
James Smart6c8eea52010-04-06 14:49:53 -04001978 /* setup PDE5 with what we have */
1979 pde5 = (struct lpfc_pde5 *) bpl;
1980 memset(pde5, 0, sizeof(struct lpfc_pde5));
1981 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05001982
James Smartbc739052010-08-04 16:11:18 -04001983 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04001984 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04001985 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04001986
James Smart6c8eea52010-04-06 14:49:53 -04001987 /* advance bpl and increment bde count */
1988 num_bde++;
1989 bpl++;
1990 pde6 = (struct lpfc_pde6 *) bpl;
James Smarte2a0a9d2008-12-04 22:40:02 -05001991
James Smart6c8eea52010-04-06 14:49:53 -04001992 /* setup PDE6 with the rest of the info */
1993 memset(pde6, 0, sizeof(struct lpfc_pde6));
1994 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
1995 bf_set(pde6_optx, pde6, txop);
1996 bf_set(pde6_oprx, pde6, rxop);
James Smartacd68592012-01-18 16:25:09 -05001997 bf_set(pde6_ce, pde6, checking);
1998 bf_set(pde6_re, pde6, checking);
James Smart6c8eea52010-04-06 14:49:53 -04001999 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04002000 bf_set(pde6_ae, pde6, 0);
2001 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04002002
James Smartbc739052010-08-04 16:11:18 -04002003 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04002004 pde6->word0 = cpu_to_le32(pde6->word0);
2005 pde6->word1 = cpu_to_le32(pde6->word1);
2006 pde6->word2 = cpu_to_le32(pde6->word2);
2007
James Smart6c8eea52010-04-06 14:49:53 -04002008 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05002009 num_bde++;
2010 bpl++;
2011
2012 /* setup the first BDE that points to protection buffer */
James Smart7f860592011-03-11 16:05:52 -05002013 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2014 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
James Smarte2a0a9d2008-12-04 22:40:02 -05002015
James Smarte2a0a9d2008-12-04 22:40:02 -05002016 /* must be integer multiple of the DIF block length */
2017 BUG_ON(protgroup_len % 8);
2018
James Smart7f860592011-03-11 16:05:52 -05002019 pde7 = (struct lpfc_pde7 *) bpl;
2020 memset(pde7, 0, sizeof(struct lpfc_pde7));
2021 bf_set(pde7_type, pde7, LPFC_PDE7_DESCRIPTOR);
2022
James Smart7c56b9f2011-07-22 18:36:25 -04002023 pde7->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
2024 pde7->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
James Smart7f860592011-03-11 16:05:52 -05002025
James Smarte2a0a9d2008-12-04 22:40:02 -05002026 protgrp_blks = protgroup_len / 8;
2027 protgrp_bytes = protgrp_blks * blksize;
2028
James Smart7f860592011-03-11 16:05:52 -05002029 /* check if this pde is crossing the 4K boundary; if so split */
2030 if ((pde7->addrLow & 0xfff) + protgroup_len > 0x1000) {
2031 protgroup_remainder = 0x1000 - (pde7->addrLow & 0xfff);
2032 protgroup_offset += protgroup_remainder;
2033 protgrp_blks = protgroup_remainder / 8;
James Smart7c56b9f2011-07-22 18:36:25 -04002034 protgrp_bytes = protgrp_blks * blksize;
James Smart7f860592011-03-11 16:05:52 -05002035 } else {
2036 protgroup_offset = 0;
2037 curr_prot++;
2038 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002039
James Smarte2a0a9d2008-12-04 22:40:02 -05002040 num_bde++;
2041
2042 /* setup BDE's for data blocks associated with DIF data */
2043 pgdone = 0;
2044 subtotal = 0; /* total bytes processed for current prot grp */
2045 while (!pgdone) {
2046 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -04002047 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2048 "9065 BLKGRD:%s Invalid data segment\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002049 __func__);
2050 return 0;
2051 }
2052 bpl++;
2053 dataphysaddr = sg_dma_address(sgde) + split_offset;
2054 bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
2055 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
2056
2057 remainder = sg_dma_len(sgde) - split_offset;
2058
2059 if ((subtotal + remainder) <= protgrp_bytes) {
2060 /* we can use this whole buffer */
2061 bpl->tus.f.bdeSize = remainder;
2062 split_offset = 0;
2063
2064 if ((subtotal + remainder) == protgrp_bytes)
2065 pgdone = 1;
2066 } else {
2067 /* must split this buffer with next prot grp */
2068 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
2069 split_offset += bpl->tus.f.bdeSize;
2070 }
2071
2072 subtotal += bpl->tus.f.bdeSize;
2073
2074 if (datadir == DMA_TO_DEVICE)
2075 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2076 else
2077 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2078 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2079
2080 num_bde++;
2081 curr_data++;
2082
2083 if (split_offset)
2084 break;
2085
2086 /* Move to the next s/g segment if possible */
2087 sgde = sg_next(sgde);
James Smart6c8eea52010-04-06 14:49:53 -04002088
James Smarte2a0a9d2008-12-04 22:40:02 -05002089 }
2090
James Smart7f860592011-03-11 16:05:52 -05002091 if (protgroup_offset) {
2092 /* update the reference tag */
2093 reftag += protgrp_blks;
2094 bpl++;
2095 continue;
2096 }
2097
James Smarte2a0a9d2008-12-04 22:40:02 -05002098 /* are we done ? */
2099 if (curr_prot == protcnt) {
2100 alldone = 1;
2101 } else if (curr_prot < protcnt) {
2102 /* advance to next prot buffer */
2103 sgpe = sg_next(sgpe);
2104 bpl++;
2105
2106 /* update the reference tag */
2107 reftag += protgrp_blks;
2108 } else {
2109 /* if we're here, we have a bug */
James Smart6a9c52c2009-10-02 15:16:51 -04002110 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2111 "9054 BLKGRD: bug in %s\n", __func__);
James Smarte2a0a9d2008-12-04 22:40:02 -05002112 }
2113
2114 } while (!alldone);
James Smarte2a0a9d2008-12-04 22:40:02 -05002115out:
2116
James Smarte2a0a9d2008-12-04 22:40:02 -05002117 return num_bde;
2118}
James Smart7f860592011-03-11 16:05:52 -05002119
James Smartacd68592012-01-18 16:25:09 -05002120/**
2121 * lpfc_bg_setup_sgl - Setup BlockGuard SGL with no protection data
2122 * @phba: The Hba for which this call is being executed.
2123 * @sc: pointer to scsi command we're working on
2124 * @sgl: pointer to buffer list for protection groups
2125 * @datacnt: number of segments of data that have been dma mapped
2126 *
2127 * This function sets up SGL buffer list for protection groups of
2128 * type LPFC_PG_TYPE_NO_DIF
2129 *
2130 * This is usually used when the HBA is instructed to generate
2131 * DIFs and insert them into data stream (or strip DIF from
2132 * incoming data stream)
2133 *
2134 * The buffer list consists of just one protection group described
2135 * below:
2136 * +-------------------------+
2137 * start of prot group --> | DI_SEED |
2138 * +-------------------------+
2139 * | Data SGE |
2140 * +-------------------------+
2141 * |more Data SGE's ... (opt)|
2142 * +-------------------------+
2143 *
2144 *
2145 * Note: Data s/g buffers have been dma mapped
2146 *
2147 * Returns the number of SGEs added to the SGL.
2148 **/
2149static int
2150lpfc_bg_setup_sgl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2151 struct sli4_sge *sgl, int datasegcnt)
2152{
2153 struct scatterlist *sgde = NULL; /* s/g data entry */
2154 struct sli4_sge_diseed *diseed = NULL;
2155 dma_addr_t physaddr;
2156 int i = 0, num_sge = 0, status;
2157 int datadir = sc->sc_data_direction;
2158 uint32_t reftag;
2159 unsigned blksize;
2160 uint8_t txop, rxop;
2161 uint32_t rc;
2162 uint32_t checking = 1;
2163 uint32_t dma_len;
2164 uint32_t dma_offset = 0;
2165
2166 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2167 if (status)
2168 goto out;
2169
2170 /* extract some info from the scsi command for pde*/
2171 blksize = lpfc_cmd_blksize(sc);
2172 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2173
2174#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2175 rc = lpfc_bg_err_inject(phba, sc, &reftag, 0, 1);
2176 if (rc) {
2177 if (rc == BG_ERR_SWAP)
2178 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
2179 if (rc == BG_ERR_CHECK)
2180 checking = 0;
2181 }
2182#endif
2183
2184 /* setup DISEED with what we have */
2185 diseed = (struct sli4_sge_diseed *) sgl;
2186 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2187 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2188
2189 /* Endianness conversion if necessary */
2190 diseed->ref_tag = cpu_to_le32(reftag);
2191 diseed->ref_tag_tran = diseed->ref_tag;
2192
2193 /* setup DISEED with the rest of the info */
2194 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2195 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
2196 if (datadir == DMA_FROM_DEVICE) {
2197 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2198 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2199 }
2200 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2201 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2202
2203 /* Endianness conversion if necessary for DISEED */
2204 diseed->word2 = cpu_to_le32(diseed->word2);
2205 diseed->word3 = cpu_to_le32(diseed->word3);
2206
2207 /* advance bpl and increment sge count */
2208 num_sge++;
2209 sgl++;
2210
2211 /* assumption: caller has already run dma_map_sg on command data */
2212 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
2213 physaddr = sg_dma_address(sgde);
2214 dma_len = sg_dma_len(sgde);
2215 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
2216 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
2217 if ((i + 1) == datasegcnt)
2218 bf_set(lpfc_sli4_sge_last, sgl, 1);
2219 else
2220 bf_set(lpfc_sli4_sge_last, sgl, 0);
2221 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2222 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2223
2224 sgl->sge_len = cpu_to_le32(dma_len);
2225 dma_offset += dma_len;
2226
2227 sgl++;
2228 num_sge++;
2229 }
2230
2231out:
2232 return num_sge;
2233}
2234
2235/**
2236 * lpfc_bg_setup_sgl_prot - Setup BlockGuard SGL with protection data
2237 * @phba: The Hba for which this call is being executed.
2238 * @sc: pointer to scsi command we're working on
2239 * @sgl: pointer to buffer list for protection groups
2240 * @datacnt: number of segments of data that have been dma mapped
2241 * @protcnt: number of segment of protection data that have been dma mapped
2242 *
2243 * This function sets up SGL buffer list for protection groups of
2244 * type LPFC_PG_TYPE_DIF
2245 *
2246 * This is usually used when DIFs are in their own buffers,
2247 * separate from the data. The HBA can then by instructed
2248 * to place the DIFs in the outgoing stream. For read operations,
2249 * The HBA could extract the DIFs and place it in DIF buffers.
2250 *
2251 * The buffer list for this type consists of one or more of the
2252 * protection groups described below:
2253 * +-------------------------+
2254 * start of first prot group --> | DISEED |
2255 * +-------------------------+
2256 * | DIF (Prot SGE) |
2257 * +-------------------------+
2258 * | Data SGE |
2259 * +-------------------------+
2260 * |more Data SGE's ... (opt)|
2261 * +-------------------------+
2262 * start of new prot group --> | DISEED |
2263 * +-------------------------+
2264 * | ... |
2265 * +-------------------------+
2266 *
2267 * Note: It is assumed that both data and protection s/g buffers have been
2268 * mapped for DMA
2269 *
2270 * Returns the number of SGEs added to the SGL.
2271 **/
2272static int
2273lpfc_bg_setup_sgl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2274 struct sli4_sge *sgl, int datacnt, int protcnt)
2275{
2276 struct scatterlist *sgde = NULL; /* s/g data entry */
2277 struct scatterlist *sgpe = NULL; /* s/g prot entry */
2278 struct sli4_sge_diseed *diseed = NULL;
2279 dma_addr_t dataphysaddr, protphysaddr;
2280 unsigned short curr_data = 0, curr_prot = 0;
2281 unsigned int split_offset;
2282 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
2283 unsigned int protgrp_blks, protgrp_bytes;
2284 unsigned int remainder, subtotal;
2285 int status;
2286 unsigned char pgdone = 0, alldone = 0;
2287 unsigned blksize;
2288 uint32_t reftag;
2289 uint8_t txop, rxop;
2290 uint32_t dma_len;
2291 uint32_t rc;
2292 uint32_t checking = 1;
2293 uint32_t dma_offset = 0;
2294 int num_sge = 0;
2295
2296 sgpe = scsi_prot_sglist(sc);
2297 sgde = scsi_sglist(sc);
2298
2299 if (!sgpe || !sgde) {
2300 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2301 "9082 Invalid s/g entry: data=0x%p prot=0x%p\n",
2302 sgpe, sgde);
2303 return 0;
2304 }
2305
2306 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2307 if (status)
2308 goto out;
2309
2310 /* extract some info from the scsi command */
2311 blksize = lpfc_cmd_blksize(sc);
2312 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2313
2314#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2315 rc = lpfc_bg_err_inject(phba, sc, &reftag, 0, 1);
2316 if (rc) {
2317 if (rc == BG_ERR_SWAP)
2318 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
2319 if (rc == BG_ERR_CHECK)
2320 checking = 0;
2321 }
2322#endif
2323
2324 split_offset = 0;
2325 do {
2326 /* setup DISEED with what we have */
2327 diseed = (struct sli4_sge_diseed *) sgl;
2328 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2329 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2330
2331 /* Endianness conversion if necessary */
2332 diseed->ref_tag = cpu_to_le32(reftag);
2333 diseed->ref_tag_tran = diseed->ref_tag;
2334
2335 /* setup DISEED with the rest of the info */
2336 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2337 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
2338 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2339 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2340 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2341 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2342
2343 /* Endianness conversion if necessary for DISEED */
2344 diseed->word2 = cpu_to_le32(diseed->word2);
2345 diseed->word3 = cpu_to_le32(diseed->word3);
2346
2347 /* advance sgl and increment bde count */
2348 num_sge++;
2349 sgl++;
2350
2351 /* setup the first BDE that points to protection buffer */
2352 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2353 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
2354
2355 /* must be integer multiple of the DIF block length */
2356 BUG_ON(protgroup_len % 8);
2357
2358 /* Now setup DIF SGE */
2359 sgl->word2 = 0;
2360 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DIF);
2361 sgl->addr_hi = le32_to_cpu(putPaddrHigh(protphysaddr));
2362 sgl->addr_lo = le32_to_cpu(putPaddrLow(protphysaddr));
2363 sgl->word2 = cpu_to_le32(sgl->word2);
2364
2365 protgrp_blks = protgroup_len / 8;
2366 protgrp_bytes = protgrp_blks * blksize;
2367
2368 /* check if DIF SGE is crossing the 4K boundary; if so split */
2369 if ((sgl->addr_lo & 0xfff) + protgroup_len > 0x1000) {
2370 protgroup_remainder = 0x1000 - (sgl->addr_lo & 0xfff);
2371 protgroup_offset += protgroup_remainder;
2372 protgrp_blks = protgroup_remainder / 8;
2373 protgrp_bytes = protgrp_blks * blksize;
2374 } else {
2375 protgroup_offset = 0;
2376 curr_prot++;
2377 }
2378
2379 num_sge++;
2380
2381 /* setup SGE's for data blocks associated with DIF data */
2382 pgdone = 0;
2383 subtotal = 0; /* total bytes processed for current prot grp */
2384 while (!pgdone) {
2385 if (!sgde) {
2386 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2387 "9086 BLKGRD:%s Invalid data segment\n",
2388 __func__);
2389 return 0;
2390 }
2391 sgl++;
2392 dataphysaddr = sg_dma_address(sgde) + split_offset;
2393
2394 remainder = sg_dma_len(sgde) - split_offset;
2395
2396 if ((subtotal + remainder) <= protgrp_bytes) {
2397 /* we can use this whole buffer */
2398 dma_len = remainder;
2399 split_offset = 0;
2400
2401 if ((subtotal + remainder) == protgrp_bytes)
2402 pgdone = 1;
2403 } else {
2404 /* must split this buffer with next prot grp */
2405 dma_len = protgrp_bytes - subtotal;
2406 split_offset += dma_len;
2407 }
2408
2409 subtotal += dma_len;
2410
2411 sgl->addr_lo = cpu_to_le32(putPaddrLow(dataphysaddr));
2412 sgl->addr_hi = cpu_to_le32(putPaddrHigh(dataphysaddr));
2413 bf_set(lpfc_sli4_sge_last, sgl, 0);
2414 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2415 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2416
2417 sgl->sge_len = cpu_to_le32(dma_len);
2418 dma_offset += dma_len;
2419
2420 num_sge++;
2421 curr_data++;
2422
2423 if (split_offset)
2424 break;
2425
2426 /* Move to the next s/g segment if possible */
2427 sgde = sg_next(sgde);
2428 }
2429
2430 if (protgroup_offset) {
2431 /* update the reference tag */
2432 reftag += protgrp_blks;
2433 sgl++;
2434 continue;
2435 }
2436
2437 /* are we done ? */
2438 if (curr_prot == protcnt) {
2439 bf_set(lpfc_sli4_sge_last, sgl, 1);
2440 alldone = 1;
2441 } else if (curr_prot < protcnt) {
2442 /* advance to next prot buffer */
2443 sgpe = sg_next(sgpe);
2444 sgl++;
2445
2446 /* update the reference tag */
2447 reftag += protgrp_blks;
2448 } else {
2449 /* if we're here, we have a bug */
2450 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2451 "9085 BLKGRD: bug in %s\n", __func__);
2452 }
2453
2454 } while (!alldone);
2455
2456out:
2457
2458 return num_sge;
2459}
2460
2461/**
2462 * lpfc_prot_group_type - Get prtotection group type of SCSI command
2463 * @phba: The Hba for which this call is being executed.
2464 * @sc: pointer to scsi command we're working on
2465 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002466 * Given a SCSI command that supports DIF, determine composition of protection
2467 * groups involved in setting up buffer lists
2468 *
James Smartacd68592012-01-18 16:25:09 -05002469 * Returns: Protection group type (with or without DIF)
2470 *
2471 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002472static int
2473lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
2474{
2475 int ret = LPFC_PG_TYPE_INVALID;
2476 unsigned char op = scsi_get_prot_op(sc);
2477
2478 switch (op) {
2479 case SCSI_PROT_READ_STRIP:
2480 case SCSI_PROT_WRITE_INSERT:
2481 ret = LPFC_PG_TYPE_NO_DIF;
2482 break;
2483 case SCSI_PROT_READ_INSERT:
2484 case SCSI_PROT_WRITE_STRIP:
2485 case SCSI_PROT_READ_PASS:
2486 case SCSI_PROT_WRITE_PASS:
James Smarte2a0a9d2008-12-04 22:40:02 -05002487 ret = LPFC_PG_TYPE_DIF_BUF;
2488 break;
2489 default:
2490 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2491 "9021 Unsupported protection op:%d\n", op);
2492 break;
2493 }
2494
2495 return ret;
2496}
2497
James Smartacd68592012-01-18 16:25:09 -05002498/**
2499 * lpfc_bg_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
2500 * @phba: The Hba for which this call is being executed.
2501 * @lpfc_cmd: The scsi buffer which is going to be prep'ed.
2502 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002503 * This is the protection/DIF aware version of
2504 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
2505 * two functions eventually, but for now, it's here
James Smartacd68592012-01-18 16:25:09 -05002506 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002507static int
James Smartacd68592012-01-18 16:25:09 -05002508lpfc_bg_scsi_prep_dma_buf_s3(struct lpfc_hba *phba,
James Smarte2a0a9d2008-12-04 22:40:02 -05002509 struct lpfc_scsi_buf *lpfc_cmd)
2510{
2511 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2512 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2513 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
2514 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2515 uint32_t num_bde = 0;
2516 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
2517 int prot_group_type = 0;
2518 int diflen, fcpdl;
2519 unsigned blksize;
2520
2521 /*
2522 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
2523 * fcp_rsp regions to the first data bde entry
2524 */
2525 bpl += 2;
2526 if (scsi_sg_count(scsi_cmnd)) {
2527 /*
2528 * The driver stores the segment count returned from pci_map_sg
2529 * because this a count of dma-mappings used to map the use_sg
2530 * pages. They are not guaranteed to be the same for those
2531 * architectures that implement an IOMMU.
2532 */
2533 datasegcnt = dma_map_sg(&phba->pcidev->dev,
2534 scsi_sglist(scsi_cmnd),
2535 scsi_sg_count(scsi_cmnd), datadir);
2536 if (unlikely(!datasegcnt))
2537 return 1;
2538
2539 lpfc_cmd->seg_cnt = datasegcnt;
2540 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04002541 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2542 "9067 BLKGRD: %s: Too many sg segments"
2543 " from dma_map_sg. Config %d, seg_cnt"
2544 " %d\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002545 __func__, phba->cfg_sg_seg_cnt,
2546 lpfc_cmd->seg_cnt);
2547 scsi_dma_unmap(scsi_cmnd);
2548 return 1;
2549 }
2550
2551 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
2552
2553 switch (prot_group_type) {
2554 case LPFC_PG_TYPE_NO_DIF:
2555 num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
2556 datasegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002557 /* we should have 2 or more entries in buffer list */
James Smarte2a0a9d2008-12-04 22:40:02 -05002558 if (num_bde < 2)
2559 goto err;
2560 break;
2561 case LPFC_PG_TYPE_DIF_BUF:{
2562 /*
2563 * This type indicates that protection buffers are
2564 * passed to the driver, so that needs to be prepared
2565 * for DMA
2566 */
2567 protsegcnt = dma_map_sg(&phba->pcidev->dev,
2568 scsi_prot_sglist(scsi_cmnd),
2569 scsi_prot_sg_count(scsi_cmnd), datadir);
2570 if (unlikely(!protsegcnt)) {
2571 scsi_dma_unmap(scsi_cmnd);
2572 return 1;
2573 }
2574
2575 lpfc_cmd->prot_seg_cnt = protsegcnt;
2576 if (lpfc_cmd->prot_seg_cnt
2577 > phba->cfg_prot_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04002578 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2579 "9068 BLKGRD: %s: Too many prot sg "
2580 "segments from dma_map_sg. Config %d,"
James Smarte2a0a9d2008-12-04 22:40:02 -05002581 "prot_seg_cnt %d\n", __func__,
2582 phba->cfg_prot_sg_seg_cnt,
2583 lpfc_cmd->prot_seg_cnt);
2584 dma_unmap_sg(&phba->pcidev->dev,
2585 scsi_prot_sglist(scsi_cmnd),
2586 scsi_prot_sg_count(scsi_cmnd),
2587 datadir);
2588 scsi_dma_unmap(scsi_cmnd);
2589 return 1;
2590 }
2591
2592 num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
2593 datasegcnt, protsegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002594 /* we should have 3 or more entries in buffer list */
James Smarte2a0a9d2008-12-04 22:40:02 -05002595 if (num_bde < 3)
2596 goto err;
2597 break;
2598 }
2599 case LPFC_PG_TYPE_INVALID:
2600 default:
2601 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2602 "9022 Unexpected protection group %i\n",
2603 prot_group_type);
2604 return 1;
2605 }
2606 }
2607
2608 /*
2609 * Finish initializing those IOCB fields that are dependent on the
2610 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
2611 * reinitialized since all iocb memory resources are used many times
2612 * for transmit, receive, and continuation bpl's.
2613 */
2614 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
2615 iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
2616 iocb_cmd->ulpBdeCount = 1;
2617 iocb_cmd->ulpLe = 1;
2618
2619 fcpdl = scsi_bufflen(scsi_cmnd);
2620
2621 if (scsi_get_prot_type(scsi_cmnd) == SCSI_PROT_DIF_TYPE1) {
2622 /*
2623 * We are in DIF Type 1 mode
2624 * Every data block has a 8 byte DIF (trailer)
2625 * attached to it. Must ajust FCP data length
2626 */
2627 blksize = lpfc_cmd_blksize(scsi_cmnd);
2628 diflen = (fcpdl / blksize) * 8;
2629 fcpdl += diflen;
2630 }
2631 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
2632
2633 /*
2634 * Due to difference in data length between DIF/non-DIF paths,
2635 * we need to set word 4 of IOCB here
2636 */
2637 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
2638
2639 return 0;
2640err:
2641 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2642 "9023 Could not setup all needed BDE's"
2643 "prot_group_type=%d, num_bde=%d\n",
2644 prot_group_type, num_bde);
2645 return 1;
2646}
2647
2648/*
2649 * This function checks for BlockGuard errors detected by
2650 * the HBA. In case of errors, the ASC/ASCQ fields in the
2651 * sense buffer will be set accordingly, paired with
2652 * ILLEGAL_REQUEST to signal to the kernel that the HBA
2653 * detected corruption.
2654 *
2655 * Returns:
2656 * 0 - No error found
2657 * 1 - BlockGuard error found
2658 * -1 - Internal error (bad profile, ...etc)
2659 */
2660static int
2661lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
2662 struct lpfc_iocbq *pIocbOut)
2663{
2664 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
2665 struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
2666 int ret = 0;
2667 uint32_t bghm = bgf->bghm;
2668 uint32_t bgstat = bgf->bgstat;
2669 uint64_t failing_sector = 0;
2670
James Smart6a9c52c2009-10-02 15:16:51 -04002671 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9069 BLKGRD: BG ERROR in cmd"
2672 " 0x%x lba 0x%llx blk cnt 0x%x "
James Smarte2a0a9d2008-12-04 22:40:02 -05002673 "bgstat=0x%x bghm=0x%x\n",
James Smart87b5c322008-12-16 10:34:09 -05002674 cmd->cmnd[0], (unsigned long long)scsi_get_lba(cmd),
Tejun Heo83096eb2009-05-07 22:24:39 +09002675 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05002676
2677 spin_lock(&_dump_buf_lock);
2678 if (!_dump_buf_done) {
James Smart6a9c52c2009-10-02 15:16:51 -04002679 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9070 BLKGRD: Saving"
2680 " Data for %u blocks to debugfs\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002681 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
James Smart6a9c52c2009-10-02 15:16:51 -04002682 lpfc_debug_save_data(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002683
2684 /* If we have a prot sgl, save the DIF buffer */
2685 if (lpfc_prot_group_type(phba, cmd) ==
2686 LPFC_PG_TYPE_DIF_BUF) {
James Smart6a9c52c2009-10-02 15:16:51 -04002687 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9071 BLKGRD: "
2688 "Saving DIF for %u blocks to debugfs\n",
2689 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
2690 lpfc_debug_save_dif(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002691 }
2692
2693 _dump_buf_done = 1;
2694 }
2695 spin_unlock(&_dump_buf_lock);
2696
2697 if (lpfc_bgs_get_invalid_prof(bgstat)) {
2698 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart6a9c52c2009-10-02 15:16:51 -04002699 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9072 BLKGRD: Invalid"
2700 " BlockGuard profile. bgstat:0x%x\n",
2701 bgstat);
James Smarte2a0a9d2008-12-04 22:40:02 -05002702 ret = (-1);
2703 goto out;
2704 }
2705
2706 if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
2707 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart6a9c52c2009-10-02 15:16:51 -04002708 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9073 BLKGRD: "
2709 "Invalid BlockGuard DIF Block. bgstat:0x%x\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002710 bgstat);
2711 ret = (-1);
2712 goto out;
2713 }
2714
2715 if (lpfc_bgs_get_guard_err(bgstat)) {
2716 ret = 1;
2717
2718 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2719 0x10, 0x1);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05002720 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05002721 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
2722 phba->bg_guard_err_cnt++;
James Smart6a9c52c2009-10-02 15:16:51 -04002723 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2724 "9055 BLKGRD: guard_tag error\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05002725 }
2726
2727 if (lpfc_bgs_get_reftag_err(bgstat)) {
2728 ret = 1;
2729
2730 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2731 0x10, 0x3);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05002732 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05002733 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
2734
2735 phba->bg_reftag_err_cnt++;
James Smart6a9c52c2009-10-02 15:16:51 -04002736 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2737 "9056 BLKGRD: ref_tag error\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05002738 }
2739
2740 if (lpfc_bgs_get_apptag_err(bgstat)) {
2741 ret = 1;
2742
2743 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2744 0x10, 0x2);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05002745 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05002746 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
2747
2748 phba->bg_apptag_err_cnt++;
James Smart6a9c52c2009-10-02 15:16:51 -04002749 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2750 "9061 BLKGRD: app_tag error\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05002751 }
2752
2753 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
2754 /*
2755 * setup sense data descriptor 0 per SPC-4 as an information
James Smart7c56b9f2011-07-22 18:36:25 -04002756 * field, and put the failing LBA in it.
2757 * This code assumes there was also a guard/app/ref tag error
2758 * indication.
James Smarte2a0a9d2008-12-04 22:40:02 -05002759 */
James Smart7c56b9f2011-07-22 18:36:25 -04002760 cmd->sense_buffer[7] = 0xc; /* Additional sense length */
2761 cmd->sense_buffer[8] = 0; /* Information descriptor type */
2762 cmd->sense_buffer[9] = 0xa; /* Additional descriptor length */
2763 cmd->sense_buffer[10] = 0x80; /* Validity bit */
James Smartacd68592012-01-18 16:25:09 -05002764
2765 /* bghm is a "on the wire" FC frame based count */
2766 switch (scsi_get_prot_op(cmd)) {
2767 case SCSI_PROT_READ_INSERT:
2768 case SCSI_PROT_WRITE_STRIP:
2769 bghm /= cmd->device->sector_size;
2770 break;
2771 case SCSI_PROT_READ_STRIP:
2772 case SCSI_PROT_WRITE_INSERT:
2773 case SCSI_PROT_READ_PASS:
2774 case SCSI_PROT_WRITE_PASS:
2775 bghm /= (cmd->device->sector_size +
2776 sizeof(struct scsi_dif_tuple));
2777 break;
2778 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002779
2780 failing_sector = scsi_get_lba(cmd);
2781 failing_sector += bghm;
2782
James Smart7c56b9f2011-07-22 18:36:25 -04002783 /* Descriptor Information */
2784 put_unaligned_be64(failing_sector, &cmd->sense_buffer[12]);
James Smarte2a0a9d2008-12-04 22:40:02 -05002785 }
2786
2787 if (!ret) {
2788 /* No error was reported - problem in FW? */
2789 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart6a9c52c2009-10-02 15:16:51 -04002790 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2791 "9057 BLKGRD: no errors reported!\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05002792 }
2793
2794out:
2795 return ret;
2796}
2797
James Smartea2151b2008-09-07 11:52:10 -04002798/**
James Smartda0436e2009-05-22 14:51:39 -04002799 * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
2800 * @phba: The Hba for which this call is being executed.
2801 * @lpfc_cmd: The scsi buffer which is going to be mapped.
2802 *
2803 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
2804 * field of @lpfc_cmd for device with SLI-4 interface spec.
2805 *
2806 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04002807 * 1 - Error
2808 * 0 - Success
James Smartda0436e2009-05-22 14:51:39 -04002809 **/
2810static int
2811lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
2812{
2813 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2814 struct scatterlist *sgel = NULL;
2815 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2816 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
James Smartfedd3b72011-02-16 12:39:24 -05002817 struct sli4_sge *first_data_sgl;
James Smartda0436e2009-05-22 14:51:39 -04002818 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2819 dma_addr_t physaddr;
2820 uint32_t num_bde = 0;
2821 uint32_t dma_len;
2822 uint32_t dma_offset = 0;
2823 int nseg;
James Smartfedd3b72011-02-16 12:39:24 -05002824 struct ulp_bde64 *bde;
James Smartda0436e2009-05-22 14:51:39 -04002825
2826 /*
2827 * There are three possibilities here - use scatter-gather segment, use
2828 * the single mapping, or neither. Start the lpfc command prep by
2829 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
2830 * data bde entry.
2831 */
2832 if (scsi_sg_count(scsi_cmnd)) {
2833 /*
2834 * The driver stores the segment count returned from pci_map_sg
2835 * because this a count of dma-mappings used to map the use_sg
2836 * pages. They are not guaranteed to be the same for those
2837 * architectures that implement an IOMMU.
2838 */
2839
2840 nseg = scsi_dma_map(scsi_cmnd);
2841 if (unlikely(!nseg))
2842 return 1;
2843 sgl += 1;
2844 /* clear the last flag in the fcp_rsp map entry */
2845 sgl->word2 = le32_to_cpu(sgl->word2);
2846 bf_set(lpfc_sli4_sge_last, sgl, 0);
2847 sgl->word2 = cpu_to_le32(sgl->word2);
2848 sgl += 1;
James Smartfedd3b72011-02-16 12:39:24 -05002849 first_data_sgl = sgl;
James Smartda0436e2009-05-22 14:51:39 -04002850 lpfc_cmd->seg_cnt = nseg;
2851 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04002852 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
2853 " %s: Too many sg segments from "
2854 "dma_map_sg. Config %d, seg_cnt %d\n",
2855 __func__, phba->cfg_sg_seg_cnt,
James Smartda0436e2009-05-22 14:51:39 -04002856 lpfc_cmd->seg_cnt);
2857 scsi_dma_unmap(scsi_cmnd);
2858 return 1;
2859 }
2860
2861 /*
2862 * The driver established a maximum scatter-gather segment count
2863 * during probe that limits the number of sg elements in any
2864 * single scsi command. Just run through the seg_cnt and format
2865 * the sge's.
2866 * When using SLI-3 the driver will try to fit all the BDEs into
2867 * the IOCB. If it can't then the BDEs get added to a BPL as it
2868 * does for SLI-2 mode.
2869 */
2870 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
2871 physaddr = sg_dma_address(sgel);
2872 dma_len = sg_dma_len(sgel);
James Smartda0436e2009-05-22 14:51:39 -04002873 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
2874 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
James Smart05580562011-05-24 11:40:48 -04002875 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -04002876 if ((num_bde + 1) == nseg)
2877 bf_set(lpfc_sli4_sge_last, sgl, 1);
2878 else
2879 bf_set(lpfc_sli4_sge_last, sgl, 0);
2880 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
James Smartf9bb2da2011-10-10 21:34:11 -04002881 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
James Smartda0436e2009-05-22 14:51:39 -04002882 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05002883 sgl->sge_len = cpu_to_le32(dma_len);
James Smartda0436e2009-05-22 14:51:39 -04002884 dma_offset += dma_len;
2885 sgl++;
2886 }
James Smartfedd3b72011-02-16 12:39:24 -05002887 /* setup the performance hint (first data BDE) if enabled */
2888 if (phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) {
2889 bde = (struct ulp_bde64 *)
2890 &(iocb_cmd->unsli3.sli3Words[5]);
2891 bde->addrLow = first_data_sgl->addr_lo;
2892 bde->addrHigh = first_data_sgl->addr_hi;
2893 bde->tus.f.bdeSize =
2894 le32_to_cpu(first_data_sgl->sge_len);
2895 bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2896 bde->tus.w = cpu_to_le32(bde->tus.w);
2897 }
James Smartda0436e2009-05-22 14:51:39 -04002898 } else {
2899 sgl += 1;
2900 /* clear the last flag in the fcp_rsp map entry */
2901 sgl->word2 = le32_to_cpu(sgl->word2);
2902 bf_set(lpfc_sli4_sge_last, sgl, 1);
2903 sgl->word2 = cpu_to_le32(sgl->word2);
2904 }
2905
2906 /*
2907 * Finish initializing those IOCB fields that are dependent on the
2908 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
2909 * explicitly reinitialized.
2910 * all iocb memory resources are reused.
2911 */
2912 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
2913
2914 /*
2915 * Due to difference in data length between DIF/non-DIF paths,
2916 * we need to set word 4 of IOCB here
2917 */
2918 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
2919 return 0;
2920}
2921
2922/**
James Smartacd68592012-01-18 16:25:09 -05002923 * lpfc_bg_scsi_adjust_dl - Adjust SCSI data length for BlockGuard
2924 * @phba: The Hba for which this call is being executed.
2925 * @lpfc_cmd: The scsi buffer which is going to be adjusted.
2926 *
2927 * Adjust the data length to account for how much data
2928 * is actually on the wire.
2929 *
2930 * returns the adjusted data length
2931 **/
2932static int
2933lpfc_bg_scsi_adjust_dl(struct lpfc_hba *phba,
2934 struct lpfc_scsi_buf *lpfc_cmd)
2935{
2936 struct scsi_cmnd *sc = lpfc_cmd->pCmd;
2937 int diflen, fcpdl;
2938 unsigned blksize;
2939
2940 fcpdl = scsi_bufflen(sc);
2941
2942 /* Check if there is protection data on the wire */
2943 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
2944 /* Read */
2945 if (scsi_get_prot_op(sc) == SCSI_PROT_READ_INSERT)
2946 return fcpdl;
2947
2948 } else {
2949 /* Write */
2950 if (scsi_get_prot_op(sc) == SCSI_PROT_WRITE_STRIP)
2951 return fcpdl;
2952 }
2953
2954 /* If protection data on the wire, adjust the count accordingly */
2955 blksize = lpfc_cmd_blksize(sc);
2956 diflen = (fcpdl / blksize) * 8;
2957 fcpdl += diflen;
2958 return fcpdl;
2959}
2960
2961/**
2962 * lpfc_bg_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
2963 * @phba: The Hba for which this call is being executed.
2964 * @lpfc_cmd: The scsi buffer which is going to be mapped.
2965 *
2966 * This is the protection/DIF aware version of
2967 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
2968 * two functions eventually, but for now, it's here
2969 **/
2970static int
2971lpfc_bg_scsi_prep_dma_buf_s4(struct lpfc_hba *phba,
2972 struct lpfc_scsi_buf *lpfc_cmd)
2973{
2974 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2975 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2976 struct sli4_sge *sgl = (struct sli4_sge *)(lpfc_cmd->fcp_bpl);
2977 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2978 uint32_t num_bde = 0;
2979 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
2980 int prot_group_type = 0;
2981 int fcpdl;
2982
2983 /*
2984 * Start the lpfc command prep by bumping the sgl beyond fcp_cmnd
2985 * fcp_rsp regions to the first data bde entry
2986 */
2987 if (scsi_sg_count(scsi_cmnd)) {
2988 /*
2989 * The driver stores the segment count returned from pci_map_sg
2990 * because this a count of dma-mappings used to map the use_sg
2991 * pages. They are not guaranteed to be the same for those
2992 * architectures that implement an IOMMU.
2993 */
2994 datasegcnt = dma_map_sg(&phba->pcidev->dev,
2995 scsi_sglist(scsi_cmnd),
2996 scsi_sg_count(scsi_cmnd), datadir);
2997 if (unlikely(!datasegcnt))
2998 return 1;
2999
3000 sgl += 1;
3001 /* clear the last flag in the fcp_rsp map entry */
3002 sgl->word2 = le32_to_cpu(sgl->word2);
3003 bf_set(lpfc_sli4_sge_last, sgl, 0);
3004 sgl->word2 = cpu_to_le32(sgl->word2);
3005
3006 sgl += 1;
3007 lpfc_cmd->seg_cnt = datasegcnt;
3008 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
3009 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
3010 "9087 BLKGRD: %s: Too many sg segments"
3011 " from dma_map_sg. Config %d, seg_cnt"
3012 " %d\n",
3013 __func__, phba->cfg_sg_seg_cnt,
3014 lpfc_cmd->seg_cnt);
3015 scsi_dma_unmap(scsi_cmnd);
3016 return 1;
3017 }
3018
3019 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
3020
3021 switch (prot_group_type) {
3022 case LPFC_PG_TYPE_NO_DIF:
3023 num_bde = lpfc_bg_setup_sgl(phba, scsi_cmnd, sgl,
3024 datasegcnt);
3025 /* we should have 2 or more entries in buffer list */
3026 if (num_bde < 2)
3027 goto err;
3028 break;
3029 case LPFC_PG_TYPE_DIF_BUF:{
3030 /*
3031 * This type indicates that protection buffers are
3032 * passed to the driver, so that needs to be prepared
3033 * for DMA
3034 */
3035 protsegcnt = dma_map_sg(&phba->pcidev->dev,
3036 scsi_prot_sglist(scsi_cmnd),
3037 scsi_prot_sg_count(scsi_cmnd), datadir);
3038 if (unlikely(!protsegcnt)) {
3039 scsi_dma_unmap(scsi_cmnd);
3040 return 1;
3041 }
3042
3043 lpfc_cmd->prot_seg_cnt = protsegcnt;
3044 if (lpfc_cmd->prot_seg_cnt
3045 > phba->cfg_prot_sg_seg_cnt) {
3046 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
3047 "9088 BLKGRD: %s: Too many prot sg "
3048 "segments from dma_map_sg. Config %d,"
3049 "prot_seg_cnt %d\n", __func__,
3050 phba->cfg_prot_sg_seg_cnt,
3051 lpfc_cmd->prot_seg_cnt);
3052 dma_unmap_sg(&phba->pcidev->dev,
3053 scsi_prot_sglist(scsi_cmnd),
3054 scsi_prot_sg_count(scsi_cmnd),
3055 datadir);
3056 scsi_dma_unmap(scsi_cmnd);
3057 return 1;
3058 }
3059
3060 num_bde = lpfc_bg_setup_sgl_prot(phba, scsi_cmnd, sgl,
3061 datasegcnt, protsegcnt);
3062 /* we should have 3 or more entries in buffer list */
3063 if (num_bde < 3)
3064 goto err;
3065 break;
3066 }
3067 case LPFC_PG_TYPE_INVALID:
3068 default:
3069 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
3070 "9083 Unexpected protection group %i\n",
3071 prot_group_type);
3072 return 1;
3073 }
3074 }
3075
3076 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
3077
3078 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
3079
3080 /*
3081 * Due to difference in data length between DIF/non-DIF paths,
3082 * we need to set word 4 of IOCB here
3083 */
3084 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
3085 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF;
3086
3087 return 0;
3088err:
3089 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
3090 "9084 Could not setup all needed BDE's"
3091 "prot_group_type=%d, num_bde=%d\n",
3092 prot_group_type, num_bde);
3093 return 1;
3094}
3095
3096/**
James Smart3772a992009-05-22 14:50:54 -04003097 * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3098 * @phba: The Hba for which this call is being executed.
3099 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3100 *
3101 * This routine wraps the actual DMA mapping function pointer from the
3102 * lpfc_hba struct.
3103 *
3104 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04003105 * 1 - Error
3106 * 0 - Success
James Smart3772a992009-05-22 14:50:54 -04003107 **/
3108static inline int
3109lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3110{
3111 return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
3112}
3113
3114/**
James Smartacd68592012-01-18 16:25:09 -05003115 * lpfc_bg_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3116 * using BlockGuard.
3117 * @phba: The Hba for which this call is being executed.
3118 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3119 *
3120 * This routine wraps the actual DMA mapping function pointer from the
3121 * lpfc_hba struct.
3122 *
3123 * Return codes:
3124 * 1 - Error
3125 * 0 - Success
3126 **/
3127static inline int
3128lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3129{
3130 return phba->lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
3131}
3132
3133/**
James Smart3621a712009-04-06 18:47:14 -04003134 * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
James Smartea2151b2008-09-07 11:52:10 -04003135 * @phba: Pointer to hba context object.
3136 * @vport: Pointer to vport object.
3137 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
3138 * @rsp_iocb: Pointer to response iocb object which reported error.
3139 *
3140 * This function posts an event when there is a SCSI command reporting
3141 * error from the scsi device.
3142 **/
3143static void
3144lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
3145 struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
3146 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3147 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
3148 uint32_t resp_info = fcprsp->rspStatus2;
3149 uint32_t scsi_status = fcprsp->rspStatus3;
3150 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
3151 struct lpfc_fast_path_event *fast_path_evt = NULL;
3152 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
3153 unsigned long flags;
3154
James Smart5989b8d2010-10-22 11:06:56 -04003155 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3156 return;
3157
James Smartea2151b2008-09-07 11:52:10 -04003158 /* If there is queuefull or busy condition send a scsi event */
3159 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
3160 (cmnd->result == SAM_STAT_BUSY)) {
3161 fast_path_evt = lpfc_alloc_fast_evt(phba);
3162 if (!fast_path_evt)
3163 return;
3164 fast_path_evt->un.scsi_evt.event_type =
3165 FC_REG_SCSI_EVENT;
3166 fast_path_evt->un.scsi_evt.subcategory =
3167 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
3168 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
3169 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
3170 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
3171 &pnode->nlp_portname, sizeof(struct lpfc_name));
3172 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
3173 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3174 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
3175 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
3176 fast_path_evt = lpfc_alloc_fast_evt(phba);
3177 if (!fast_path_evt)
3178 return;
3179 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
3180 FC_REG_SCSI_EVENT;
3181 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
3182 LPFC_EVENT_CHECK_COND;
3183 fast_path_evt->un.check_cond_evt.scsi_event.lun =
3184 cmnd->device->lun;
3185 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
3186 &pnode->nlp_portname, sizeof(struct lpfc_name));
3187 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
3188 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3189 fast_path_evt->un.check_cond_evt.sense_key =
3190 cmnd->sense_buffer[2] & 0xf;
3191 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
3192 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
3193 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3194 fcpi_parm &&
3195 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
3196 ((scsi_status == SAM_STAT_GOOD) &&
3197 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
3198 /*
3199 * If status is good or resid does not match with fcp_param and
3200 * there is valid fcpi_parm, then there is a read_check error
3201 */
3202 fast_path_evt = lpfc_alloc_fast_evt(phba);
3203 if (!fast_path_evt)
3204 return;
3205 fast_path_evt->un.read_check_error.header.event_type =
3206 FC_REG_FABRIC_EVENT;
3207 fast_path_evt->un.read_check_error.header.subcategory =
3208 LPFC_EVENT_FCPRDCHKERR;
3209 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
3210 &pnode->nlp_portname, sizeof(struct lpfc_name));
3211 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
3212 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3213 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
3214 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
3215 fast_path_evt->un.read_check_error.fcpiparam =
3216 fcpi_parm;
3217 } else
3218 return;
3219
3220 fast_path_evt->vport = vport;
3221 spin_lock_irqsave(&phba->hbalock, flags);
3222 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
3223 spin_unlock_irqrestore(&phba->hbalock, flags);
3224 lpfc_worker_wake_up(phba);
3225 return;
3226}
James Smart9bad7672008-12-04 22:39:02 -05003227
3228/**
James Smartf1126682009-06-10 17:22:44 -04003229 * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
James Smart3772a992009-05-22 14:50:54 -04003230 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -05003231 * @psb: The scsi buffer which is going to be un-mapped.
3232 *
3233 * This routine does DMA un-mapping of scatter gather list of scsi command
James Smart3772a992009-05-22 14:50:54 -04003234 * field of @lpfc_cmd for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05003235 **/
dea31012005-04-17 16:05:31 -05003236static void
James Smartf1126682009-06-10 17:22:44 -04003237lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James Smartbcf4dbf2006-07-06 15:50:08 -04003238{
3239 /*
3240 * There are only two special cases to consider. (1) the scsi command
3241 * requested scatter-gather usage or (2) the scsi command allocated
3242 * a request buffer, but did not request use_sg. There is a third
3243 * case, but it does not require resource deallocation.
3244 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003245 if (psb->seg_cnt > 0)
3246 scsi_dma_unmap(psb->pCmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003247 if (psb->prot_seg_cnt > 0)
3248 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
3249 scsi_prot_sg_count(psb->pCmd),
3250 psb->pCmd->sc_data_direction);
James Smartbcf4dbf2006-07-06 15:50:08 -04003251}
3252
James Smart9bad7672008-12-04 22:39:02 -05003253/**
James Smart3621a712009-04-06 18:47:14 -04003254 * lpfc_handler_fcp_err - FCP response handler
James Smart9bad7672008-12-04 22:39:02 -05003255 * @vport: The virtual port for which this call is being executed.
3256 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
3257 * @rsp_iocb: The response IOCB which contains FCP error.
3258 *
3259 * This routine is called to process response IOCB with status field
3260 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
3261 * based upon SCSI and FCP error.
3262 **/
James Smartbcf4dbf2006-07-06 15:50:08 -04003263static void
James Smart2e0fef82007-06-17 19:56:36 -05003264lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
3265 struct lpfc_iocbq *rsp_iocb)
dea31012005-04-17 16:05:31 -05003266{
3267 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3268 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
3269 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
James Smart7054a602007-04-25 09:52:34 -04003270 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
dea31012005-04-17 16:05:31 -05003271 uint32_t resp_info = fcprsp->rspStatus2;
3272 uint32_t scsi_status = fcprsp->rspStatus3;
James Smartc7743952006-12-02 13:34:42 -05003273 uint32_t *lp;
dea31012005-04-17 16:05:31 -05003274 uint32_t host_status = DID_OK;
3275 uint32_t rsplen = 0;
James Smartc7743952006-12-02 13:34:42 -05003276 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea31012005-04-17 16:05:31 -05003277
James Smartea2151b2008-09-07 11:52:10 -04003278
dea31012005-04-17 16:05:31 -05003279 /*
3280 * If this is a task management command, there is no
3281 * scsi packet associated with this lpfc_cmd. The driver
3282 * consumes it.
3283 */
3284 if (fcpcmd->fcpCntl2) {
3285 scsi_status = 0;
3286 goto out;
3287 }
3288
James Smart6a9c52c2009-10-02 15:16:51 -04003289 if (resp_info & RSP_LEN_VALID) {
3290 rsplen = be32_to_cpu(fcprsp->rspRspLen);
James Smarte40a02c2010-02-26 14:13:54 -05003291 if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
James Smart6a9c52c2009-10-02 15:16:51 -04003292 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3293 "2719 Invalid response length: "
3294 "tgt x%x lun x%x cmnd x%x rsplen x%x\n",
3295 cmnd->device->id,
3296 cmnd->device->lun, cmnd->cmnd[0],
3297 rsplen);
3298 host_status = DID_ERROR;
3299 goto out;
3300 }
James Smarte40a02c2010-02-26 14:13:54 -05003301 if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
3302 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3303 "2757 Protocol failure detected during "
3304 "processing of FCP I/O op: "
3305 "tgt x%x lun x%x cmnd x%x rspInfo3 x%x\n",
3306 cmnd->device->id,
3307 cmnd->device->lun, cmnd->cmnd[0],
3308 fcprsp->rspInfo3);
3309 host_status = DID_ERROR;
3310 goto out;
3311 }
James Smart6a9c52c2009-10-02 15:16:51 -04003312 }
3313
James Smartc7743952006-12-02 13:34:42 -05003314 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
3315 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
3316 if (snslen > SCSI_SENSE_BUFFERSIZE)
3317 snslen = SCSI_SENSE_BUFFERSIZE;
3318
3319 if (resp_info & RSP_LEN_VALID)
3320 rsplen = be32_to_cpu(fcprsp->rspRspLen);
3321 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
3322 }
3323 lp = (uint32_t *)cmnd->sense_buffer;
3324
James Smart73d91e52011-10-10 21:32:10 -04003325 if (!scsi_status && (resp_info & RESID_UNDER) &&
3326 vport->cfg_log_verbose & LOG_FCP_UNDER)
3327 logit = LOG_FCP_UNDER;
James Smartc7743952006-12-02 13:34:42 -05003328
James Smarte8b62012007-08-02 11:10:09 -04003329 lpfc_printf_vlog(vport, KERN_WARNING, logit,
James Smarte2a0a9d2008-12-04 22:40:02 -05003330 "9024 FCP command x%x failed: x%x SNS x%x x%x "
James Smarte8b62012007-08-02 11:10:09 -04003331 "Data: x%x x%x x%x x%x x%x\n",
3332 cmnd->cmnd[0], scsi_status,
3333 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
3334 be32_to_cpu(fcprsp->rspResId),
3335 be32_to_cpu(fcprsp->rspSnsLen),
3336 be32_to_cpu(fcprsp->rspRspLen),
3337 fcprsp->rspInfo3);
dea31012005-04-17 16:05:31 -05003338
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003339 scsi_set_resid(cmnd, 0);
dea31012005-04-17 16:05:31 -05003340 if (resp_info & RESID_UNDER) {
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003341 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea31012005-04-17 16:05:31 -05003342
James Smart73d91e52011-10-10 21:32:10 -04003343 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_UNDER,
James Smarte2a0a9d2008-12-04 22:40:02 -05003344 "9025 FCP Read Underrun, expected %d, "
James Smarte8b62012007-08-02 11:10:09 -04003345 "residual %d Data: x%x x%x x%x\n",
3346 be32_to_cpu(fcpcmd->fcpDl),
3347 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
3348 cmnd->underflow);
dea31012005-04-17 16:05:31 -05003349
3350 /*
James Smart7054a602007-04-25 09:52:34 -04003351 * If there is an under run check if under run reported by
3352 * storage array is same as the under run reported by HBA.
3353 * If this is not same, there is a dropped frame.
3354 */
3355 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3356 fcpi_parm &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003357 (scsi_get_resid(cmnd) != fcpi_parm)) {
James Smarte8b62012007-08-02 11:10:09 -04003358 lpfc_printf_vlog(vport, KERN_WARNING,
3359 LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05003360 "9026 FCP Read Check Error "
James Smarte8b62012007-08-02 11:10:09 -04003361 "and Underrun Data: x%x x%x x%x x%x\n",
3362 be32_to_cpu(fcpcmd->fcpDl),
3363 scsi_get_resid(cmnd), fcpi_parm,
3364 cmnd->cmnd[0]);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003365 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
James Smart7054a602007-04-25 09:52:34 -04003366 host_status = DID_ERROR;
3367 }
3368 /*
dea31012005-04-17 16:05:31 -05003369 * The cmnd->underflow is the minimum number of bytes that must
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003370 * be transferred for this command. Provided a sense condition
dea31012005-04-17 16:05:31 -05003371 * is not present, make sure the actual amount transferred is at
3372 * least the underflow value or fail.
3373 */
3374 if (!(resp_info & SNS_LEN_VALID) &&
3375 (scsi_status == SAM_STAT_GOOD) &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003376 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
3377 < cmnd->underflow)) {
James Smarte8b62012007-08-02 11:10:09 -04003378 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05003379 "9027 FCP command x%x residual "
James Smarte8b62012007-08-02 11:10:09 -04003380 "underrun converted to error "
3381 "Data: x%x x%x x%x\n",
James Smart66dbfbe2007-08-05 06:08:38 -04003382 cmnd->cmnd[0], scsi_bufflen(cmnd),
James Smarte8b62012007-08-02 11:10:09 -04003383 scsi_get_resid(cmnd), cmnd->underflow);
dea31012005-04-17 16:05:31 -05003384 host_status = DID_ERROR;
3385 }
3386 } else if (resp_info & RESID_OVER) {
James Smarte8b62012007-08-02 11:10:09 -04003387 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05003388 "9028 FCP command x%x residual overrun error. "
James Smarte4e74272009-07-19 10:01:38 -04003389 "Data: x%x x%x\n", cmnd->cmnd[0],
James Smarte8b62012007-08-02 11:10:09 -04003390 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea31012005-04-17 16:05:31 -05003391 host_status = DID_ERROR;
3392
3393 /*
3394 * Check SLI validation that all the transfer was actually done
3395 * (fcpi_parm should be zero). Apply check only to reads.
3396 */
James Smarteee88772010-09-29 11:19:08 -04003397 } else if (fcpi_parm && (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
James Smarte8b62012007-08-02 11:10:09 -04003398 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05003399 "9029 FCP Read Check Error Data: "
James Smarteee88772010-09-29 11:19:08 -04003400 "x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003401 be32_to_cpu(fcpcmd->fcpDl),
3402 be32_to_cpu(fcprsp->rspResId),
James Smarteee88772010-09-29 11:19:08 -04003403 fcpi_parm, cmnd->cmnd[0], scsi_status);
3404 switch (scsi_status) {
3405 case SAM_STAT_GOOD:
3406 case SAM_STAT_CHECK_CONDITION:
3407 /* Fabric dropped a data frame. Fail any successful
3408 * command in which we detected dropped frames.
3409 * A status of good or some check conditions could
3410 * be considered a successful command.
3411 */
3412 host_status = DID_ERROR;
3413 break;
3414 }
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003415 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea31012005-04-17 16:05:31 -05003416 }
3417
3418 out:
3419 cmnd->result = ScsiResult(host_status, scsi_status);
James Smartea2151b2008-09-07 11:52:10 -04003420 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
dea31012005-04-17 16:05:31 -05003421}
3422
James Smart9bad7672008-12-04 22:39:02 -05003423/**
James Smart3621a712009-04-06 18:47:14 -04003424 * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
James Smart9bad7672008-12-04 22:39:02 -05003425 * @phba: The Hba for which this call is being executed.
3426 * @pIocbIn: The command IOCBQ for the scsi cmnd.
James Smart3772a992009-05-22 14:50:54 -04003427 * @pIocbOut: The response IOCBQ for the scsi cmnd.
James Smart9bad7672008-12-04 22:39:02 -05003428 *
3429 * This routine assigns scsi command result by looking into response IOCB
3430 * status field appropriately. This routine handles QUEUE FULL condition as
3431 * well by ramping down device queue depth.
3432 **/
dea31012005-04-17 16:05:31 -05003433static void
3434lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
3435 struct lpfc_iocbq *pIocbOut)
3436{
3437 struct lpfc_scsi_buf *lpfc_cmd =
3438 (struct lpfc_scsi_buf *) pIocbIn->context1;
James Smart2e0fef82007-06-17 19:56:36 -05003439 struct lpfc_vport *vport = pIocbIn->vport;
dea31012005-04-17 16:05:31 -05003440 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
3441 struct lpfc_nodelist *pnode = rdata->pnode;
James Smart75baf692010-06-08 18:31:21 -04003442 struct scsi_cmnd *cmd;
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05003443 int result;
James Smarta257bf92009-04-06 18:48:10 -04003444 struct scsi_device *tmp_sdev;
James Smart5ffc2662009-11-18 15:39:44 -05003445 int depth;
James Smartfa61a542008-01-11 01:52:42 -05003446 unsigned long flags;
James Smartea2151b2008-09-07 11:52:10 -04003447 struct lpfc_fast_path_event *fast_path_evt;
James Smart75baf692010-06-08 18:31:21 -04003448 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04003449 uint32_t queue_depth, scsi_id;
James Smart73d91e52011-10-10 21:32:10 -04003450 uint32_t logit = LOG_FCP;
dea31012005-04-17 16:05:31 -05003451
James Smart75baf692010-06-08 18:31:21 -04003452 /* Sanity check on return of outstanding command */
3453 if (!(lpfc_cmd->pCmd))
3454 return;
3455 cmd = lpfc_cmd->pCmd;
3456 shost = cmd->device->host;
3457
dea31012005-04-17 16:05:31 -05003458 lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
3459 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
James Smart341af102010-01-26 23:07:37 -05003460 /* pick up SLI4 exhange busy status from HBA */
3461 lpfc_cmd->exch_busy = pIocbOut->iocb_flag & LPFC_EXCHANGE_BUSY;
3462
James Smart109f6ed2008-12-04 22:39:08 -05003463 if (pnode && NLP_CHK_NODE_ACT(pnode))
3464 atomic_dec(&pnode->cmd_pending);
dea31012005-04-17 16:05:31 -05003465
3466 if (lpfc_cmd->status) {
3467 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
3468 (lpfc_cmd->result & IOERR_DRVR_MASK))
3469 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
3470 else if (lpfc_cmd->status >= IOSTAT_CNT)
3471 lpfc_cmd->status = IOSTAT_DEFAULT;
James Smart73d91e52011-10-10 21:32:10 -04003472 if (lpfc_cmd->status == IOSTAT_FCP_RSP_ERROR
3473 && !lpfc_cmd->fcp_rsp->rspStatus3
3474 && (lpfc_cmd->fcp_rsp->rspStatus2 & RESID_UNDER)
3475 && !(phba->cfg_log_verbose & LOG_FCP_UNDER))
3476 logit = 0;
3477 else
3478 logit = LOG_FCP | LOG_FCP_UNDER;
3479 lpfc_printf_vlog(vport, KERN_WARNING, logit,
3480 "9030 FCP cmd x%x failed <%d/%d> "
3481 "status: x%x result: x%x Data: x%x x%x\n",
3482 cmd->cmnd[0],
3483 cmd->device ? cmd->device->id : 0xffff,
3484 cmd->device ? cmd->device->lun : 0xffff,
3485 lpfc_cmd->status, lpfc_cmd->result,
3486 pIocbOut->iocb.ulpContext,
3487 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
dea31012005-04-17 16:05:31 -05003488
3489 switch (lpfc_cmd->status) {
3490 case IOSTAT_FCP_RSP_ERROR:
3491 /* Call FCP RSP handler to determine result */
James Smart2e0fef82007-06-17 19:56:36 -05003492 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea31012005-04-17 16:05:31 -05003493 break;
3494 case IOSTAT_NPORT_BSY:
3495 case IOSTAT_FABRIC_BSY:
James Smart0f1f53a2008-08-24 21:50:18 -04003496 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
James Smartea2151b2008-09-07 11:52:10 -04003497 fast_path_evt = lpfc_alloc_fast_evt(phba);
3498 if (!fast_path_evt)
3499 break;
3500 fast_path_evt->un.fabric_evt.event_type =
3501 FC_REG_FABRIC_EVENT;
3502 fast_path_evt->un.fabric_evt.subcategory =
3503 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
3504 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
3505 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
3506 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
3507 &pnode->nlp_portname,
3508 sizeof(struct lpfc_name));
3509 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
3510 &pnode->nlp_nodename,
3511 sizeof(struct lpfc_name));
3512 }
3513 fast_path_evt->vport = vport;
3514 fast_path_evt->work_evt.evt =
3515 LPFC_EVT_FASTPATH_MGMT_EVT;
3516 spin_lock_irqsave(&phba->hbalock, flags);
3517 list_add_tail(&fast_path_evt->work_evt.evt_listp,
3518 &phba->work_list);
3519 spin_unlock_irqrestore(&phba->hbalock, flags);
3520 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05003521 break;
James Smart92d7f7b2007-06-17 19:56:38 -05003522 case IOSTAT_LOCAL_REJECT:
James Smart1151e3e2011-02-16 12:39:35 -05003523 case IOSTAT_REMOTE_STOP:
James Smartab56dc22011-02-16 12:39:57 -05003524 if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
3525 lpfc_cmd->result ==
3526 IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
3527 lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
3528 lpfc_cmd->result ==
3529 IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
3530 cmd->result = ScsiResult(DID_NO_CONNECT, 0);
3531 break;
3532 }
James Smartd7c255b2008-08-24 21:50:00 -04003533 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
James Smart92d7f7b2007-06-17 19:56:38 -05003534 lpfc_cmd->result == IOERR_NO_RESOURCES ||
James Smartb92938b2010-06-07 15:24:12 -04003535 lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
3536 lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
James Smart92d7f7b2007-06-17 19:56:38 -05003537 cmd->result = ScsiResult(DID_REQUEUE, 0);
James Smart58da1ff2008-04-07 10:15:56 -04003538 break;
James Smarte2a0a9d2008-12-04 22:40:02 -05003539 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003540 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
3541 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
3542 pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
3543 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
3544 /*
3545 * This is a response for a BG enabled
3546 * cmd. Parse BG error
3547 */
3548 lpfc_parse_bg_err(phba, lpfc_cmd,
3549 pIocbOut);
3550 break;
3551 } else {
3552 lpfc_printf_vlog(vport, KERN_WARNING,
3553 LOG_BG,
3554 "9031 non-zero BGSTAT "
James Smart6a9c52c2009-10-02 15:16:51 -04003555 "on unprotected cmd\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05003556 }
3557 }
James Smart1151e3e2011-02-16 12:39:35 -05003558 if ((lpfc_cmd->status == IOSTAT_REMOTE_STOP)
3559 && (phba->sli_rev == LPFC_SLI_REV4)
3560 && (pnode && NLP_CHK_NODE_ACT(pnode))) {
3561 /* This IO was aborted by the target, we don't
3562 * know the rxid and because we did not send the
3563 * ABTS we cannot generate and RRQ.
3564 */
3565 lpfc_set_rrq_active(phba, pnode,
3566 lpfc_cmd->cur_iocbq.sli4_xritag,
3567 0, 0);
3568 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003569 /* else: fall through */
dea31012005-04-17 16:05:31 -05003570 default:
3571 cmd->result = ScsiResult(DID_ERROR, 0);
3572 break;
3573 }
3574
James Smart58da1ff2008-04-07 10:15:56 -04003575 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003576 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
James Smart0f1f53a2008-08-24 21:50:18 -04003577 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
3578 SAM_STAT_BUSY);
James Smartab56dc22011-02-16 12:39:57 -05003579 } else
dea31012005-04-17 16:05:31 -05003580 cmd->result = ScsiResult(DID_OK, 0);
dea31012005-04-17 16:05:31 -05003581
3582 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
3583 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
3584
James Smarte8b62012007-08-02 11:10:09 -04003585 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
3586 "0710 Iodone <%d/%d> cmd %p, error "
3587 "x%x SNS x%x x%x Data: x%x x%x\n",
3588 cmd->device->id, cmd->device->lun, cmd,
3589 cmd->result, *lp, *(lp + 3), cmd->retries,
3590 scsi_get_resid(cmd));
dea31012005-04-17 16:05:31 -05003591 }
3592
James Smartea2151b2008-09-07 11:52:10 -04003593 lpfc_update_stats(phba, lpfc_cmd);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05003594 result = cmd->result;
James Smart977b5a02008-09-07 11:52:04 -04003595 if (vport->cfg_max_scsicmpl_time &&
3596 time_after(jiffies, lpfc_cmd->start_time +
3597 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
James Smarta257bf92009-04-06 18:48:10 -04003598 spin_lock_irqsave(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05003599 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
3600 if (pnode->cmd_qdepth >
3601 atomic_read(&pnode->cmd_pending) &&
3602 (atomic_read(&pnode->cmd_pending) >
3603 LPFC_MIN_TGT_QDEPTH) &&
3604 ((cmd->cmnd[0] == READ_10) ||
3605 (cmd->cmnd[0] == WRITE_10)))
3606 pnode->cmd_qdepth =
3607 atomic_read(&pnode->cmd_pending);
James Smart977b5a02008-09-07 11:52:04 -04003608
James Smart109f6ed2008-12-04 22:39:08 -05003609 pnode->last_change_time = jiffies;
3610 }
James Smarta257bf92009-04-06 18:48:10 -04003611 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05003612 } else if (pnode && NLP_CHK_NODE_ACT(pnode)) {
James Smart7dc517d2010-07-14 15:32:10 -04003613 if ((pnode->cmd_qdepth < vport->cfg_tgt_queue_depth) &&
James Smart977b5a02008-09-07 11:52:04 -04003614 time_after(jiffies, pnode->last_change_time +
James Smart109f6ed2008-12-04 22:39:08 -05003615 msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
James Smarta257bf92009-04-06 18:48:10 -04003616 spin_lock_irqsave(shost->host_lock, flags);
James Smart7dc517d2010-07-14 15:32:10 -04003617 depth = pnode->cmd_qdepth * LPFC_TGTQ_RAMPUP_PCENT
3618 / 100;
3619 depth = depth ? depth : 1;
3620 pnode->cmd_qdepth += depth;
3621 if (pnode->cmd_qdepth > vport->cfg_tgt_queue_depth)
3622 pnode->cmd_qdepth = vport->cfg_tgt_queue_depth;
James Smart109f6ed2008-12-04 22:39:08 -05003623 pnode->last_change_time = jiffies;
James Smarta257bf92009-04-06 18:48:10 -04003624 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05003625 }
James Smart977b5a02008-09-07 11:52:04 -04003626 }
3627
James Smart1dcb58e2007-04-25 09:51:30 -04003628 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James Smarta257bf92009-04-06 18:48:10 -04003629
3630 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
3631 queue_depth = cmd->device->queue_depth;
3632 scsi_id = cmd->device->id;
dea31012005-04-17 16:05:31 -05003633 cmd->scsi_done(cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003634
Jamie Wellnitzb8086082006-02-28 22:33:12 -05003635 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smartfa61a542008-01-11 01:52:42 -05003636 /*
3637 * If there is a thread waiting for command completion
3638 * wake up the thread.
3639 */
James Smarta257bf92009-04-06 18:48:10 -04003640 spin_lock_irqsave(shost->host_lock, flags);
James Smart495a7142008-06-14 22:52:59 -04003641 lpfc_cmd->pCmd = NULL;
James Smartfa61a542008-01-11 01:52:42 -05003642 if (lpfc_cmd->waitq)
3643 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04003644 spin_unlock_irqrestore(shost->host_lock, flags);
Jamie Wellnitzb8086082006-02-28 22:33:12 -05003645 lpfc_release_scsi_buf(phba, lpfc_cmd);
3646 return;
3647 }
3648
James Smart92d7f7b2007-06-17 19:56:38 -05003649 if (!result)
James Smarta257bf92009-04-06 18:48:10 -04003650 lpfc_rampup_queue_depth(vport, queue_depth);
James Smart92d7f7b2007-06-17 19:56:38 -05003651
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05003652 /*
3653 * Check for queue full. If the lun is reporting queue full, then
3654 * back off the lun queue depth to prevent target overloads.
3655 */
James Smart58da1ff2008-04-07 10:15:56 -04003656 if (result == SAM_STAT_TASK_SET_FULL && pnode &&
3657 NLP_CHK_NODE_ACT(pnode)) {
James Smarta257bf92009-04-06 18:48:10 -04003658 shost_for_each_device(tmp_sdev, shost) {
3659 if (tmp_sdev->id != scsi_id)
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05003660 continue;
3661 depth = scsi_track_queue_full(tmp_sdev,
James Smart5ffc2662009-11-18 15:39:44 -05003662 tmp_sdev->queue_depth-1);
3663 if (depth <= 0)
3664 continue;
James Smarte8b62012007-08-02 11:10:09 -04003665 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3666 "0711 detected queue full - lun queue "
3667 "depth adjusted to %d.\n", depth);
James Smartea2151b2008-09-07 11:52:10 -04003668 lpfc_send_sdev_queuedepth_change_event(phba, vport,
James Smart5ffc2662009-11-18 15:39:44 -05003669 pnode,
3670 tmp_sdev->lun,
3671 depth+1, depth);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05003672 }
3673 }
3674
James Smartfa61a542008-01-11 01:52:42 -05003675 /*
3676 * If there is a thread waiting for command completion
3677 * wake up the thread.
3678 */
James Smarta257bf92009-04-06 18:48:10 -04003679 spin_lock_irqsave(shost->host_lock, flags);
James Smart495a7142008-06-14 22:52:59 -04003680 lpfc_cmd->pCmd = NULL;
James Smartfa61a542008-01-11 01:52:42 -05003681 if (lpfc_cmd->waitq)
3682 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04003683 spin_unlock_irqrestore(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05003684
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003685 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05003686}
3687
James Smart34b02dc2008-08-24 21:49:55 -04003688/**
James Smart3621a712009-04-06 18:47:14 -04003689 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
James Smart34b02dc2008-08-24 21:49:55 -04003690 * @data: A pointer to the immediate command data portion of the IOCB.
3691 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
3692 *
3693 * The routine copies the entire FCP command from @fcp_cmnd to @data while
3694 * byte swapping the data to big endian format for transmission on the wire.
3695 **/
3696static void
3697lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
3698{
3699 int i, j;
3700 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
3701 i += sizeof(uint32_t), j++) {
3702 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
3703 }
3704}
3705
James Smart9bad7672008-12-04 22:39:02 -05003706/**
James Smartf1126682009-06-10 17:22:44 -04003707 * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05003708 * @vport: The virtual port for which this call is being executed.
3709 * @lpfc_cmd: The scsi command which needs to send.
3710 * @pnode: Pointer to lpfc_nodelist.
3711 *
3712 * This routine initializes fcp_cmnd and iocb data structure from scsi command
James Smart3772a992009-05-22 14:50:54 -04003713 * to transfer for device with SLI3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05003714 **/
dea31012005-04-17 16:05:31 -05003715static void
James Smartf1126682009-06-10 17:22:44 -04003716lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
James Smart2e0fef82007-06-17 19:56:36 -05003717 struct lpfc_nodelist *pnode)
dea31012005-04-17 16:05:31 -05003718{
James Smart2e0fef82007-06-17 19:56:36 -05003719 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003720 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3721 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3722 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
3723 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
3724 int datadir = scsi_cmnd->sc_data_direction;
James Smart7e2b19f2007-10-29 11:00:39 -04003725 char tag[2];
dea31012005-04-17 16:05:31 -05003726
James Smart58da1ff2008-04-07 10:15:56 -04003727 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3728 return;
3729
dea31012005-04-17 16:05:31 -05003730 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
James.Smart@Emulex.Com69859dc2005-08-10 15:02:37 -04003731 /* clear task management bits */
3732 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea31012005-04-17 16:05:31 -05003733
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -04003734 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
3735 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05003736
James Smartdf9e1b52011-12-13 13:22:17 -05003737 memset(&fcp_cmnd->fcpCdb[0], 0, LPFC_FCP_CDB_LEN);
3738 memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, scsi_cmnd->cmd_len);
James Smart7e2b19f2007-10-29 11:00:39 -04003739 if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
3740 switch (tag[0]) {
dea31012005-04-17 16:05:31 -05003741 case HEAD_OF_QUEUE_TAG:
3742 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
3743 break;
3744 case ORDERED_QUEUE_TAG:
3745 fcp_cmnd->fcpCntl1 = ORDERED_Q;
3746 break;
3747 default:
3748 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
3749 break;
3750 }
3751 } else
3752 fcp_cmnd->fcpCntl1 = 0;
3753
3754 /*
3755 * There are three possibilities here - use scatter-gather segment, use
3756 * the single mapping, or neither. Start the lpfc command prep by
3757 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
3758 * data bde entry.
3759 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003760 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05003761 if (datadir == DMA_TO_DEVICE) {
3762 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
James Smart3772a992009-05-22 14:50:54 -04003763 if (phba->sli_rev < LPFC_SLI_REV4) {
3764 iocb_cmd->un.fcpi.fcpi_parm = 0;
3765 iocb_cmd->ulpPU = 0;
3766 } else
3767 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05003768 fcp_cmnd->fcpCntl3 = WRITE_DATA;
3769 phba->fc4OutputRequests++;
3770 } else {
3771 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
3772 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05003773 fcp_cmnd->fcpCntl3 = READ_DATA;
3774 phba->fc4InputRequests++;
3775 }
3776 } else {
3777 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
3778 iocb_cmd->un.fcpi.fcpi_parm = 0;
3779 iocb_cmd->ulpPU = 0;
3780 fcp_cmnd->fcpCntl3 = 0;
3781 phba->fc4ControlRequests++;
3782 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003783 if (phba->sli_rev == 3 &&
3784 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04003785 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05003786 /*
3787 * Finish initializing those IOCB fields that are independent
3788 * of the scsi_cmnd request_buffer
3789 */
3790 piocbq->iocb.ulpContext = pnode->nlp_rpi;
James Smart6d368e52011-05-24 11:44:12 -04003791 if (phba->sli_rev == LPFC_SLI_REV4)
3792 piocbq->iocb.ulpContext =
3793 phba->sli4_hba.rpi_ids[pnode->nlp_rpi];
dea31012005-04-17 16:05:31 -05003794 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
3795 piocbq->iocb.ulpFCP2Rcvy = 1;
James Smart09372822008-01-11 01:52:54 -05003796 else
3797 piocbq->iocb.ulpFCP2Rcvy = 0;
dea31012005-04-17 16:05:31 -05003798
3799 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
3800 piocbq->context1 = lpfc_cmd;
3801 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
3802 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
James Smart2e0fef82007-06-17 19:56:36 -05003803 piocbq->vport = vport;
dea31012005-04-17 16:05:31 -05003804}
3805
James Smart9bad7672008-12-04 22:39:02 -05003806/**
James Smart6d368e52011-05-24 11:44:12 -04003807 * lpfc_scsi_prep_task_mgmt_cmd - Convert SLI3 scsi TM cmd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05003808 * @vport: The virtual port for which this call is being executed.
3809 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
3810 * @lun: Logical unit number.
3811 * @task_mgmt_cmd: SCSI task management command.
3812 *
James Smart3772a992009-05-22 14:50:54 -04003813 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
3814 * for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05003815 *
3816 * Return codes:
3817 * 0 - Error
3818 * 1 - Success
3819 **/
dea31012005-04-17 16:05:31 -05003820static int
James Smartf1126682009-06-10 17:22:44 -04003821lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
dea31012005-04-17 16:05:31 -05003822 struct lpfc_scsi_buf *lpfc_cmd,
James Smart420b630d2006-07-06 15:50:16 -04003823 unsigned int lun,
dea31012005-04-17 16:05:31 -05003824 uint8_t task_mgmt_cmd)
3825{
dea31012005-04-17 16:05:31 -05003826 struct lpfc_iocbq *piocbq;
3827 IOCB_t *piocb;
3828 struct fcp_cmnd *fcp_cmnd;
James Smart0b18ac42006-05-01 21:50:40 -04003829 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea31012005-04-17 16:05:31 -05003830 struct lpfc_nodelist *ndlp = rdata->pnode;
3831
James Smart58da1ff2008-04-07 10:15:56 -04003832 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
3833 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05003834 return 0;
dea31012005-04-17 16:05:31 -05003835
dea31012005-04-17 16:05:31 -05003836 piocbq = &(lpfc_cmd->cur_iocbq);
James Smart2e0fef82007-06-17 19:56:36 -05003837 piocbq->vport = vport;
3838
dea31012005-04-17 16:05:31 -05003839 piocb = &piocbq->iocb;
3840
3841 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart34b02dc2008-08-24 21:49:55 -04003842 /* Clear out any old data in the FCP command area */
3843 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
3844 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05003845 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
James Smarte2a0a9d2008-12-04 22:40:02 -05003846 if (vport->phba->sli_rev == 3 &&
3847 !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04003848 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05003849 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
dea31012005-04-17 16:05:31 -05003850 piocb->ulpContext = ndlp->nlp_rpi;
James Smart6d368e52011-05-24 11:44:12 -04003851 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
3852 piocb->ulpContext =
3853 vport->phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
3854 }
dea31012005-04-17 16:05:31 -05003855 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
3856 piocb->ulpFCP2Rcvy = 1;
3857 }
3858 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
3859
3860 /* ulpTimeout is only one byte */
3861 if (lpfc_cmd->timeout > 0xff) {
3862 /*
3863 * Do not timeout the command at the firmware level.
3864 * The driver will provide the timeout mechanism.
3865 */
3866 piocb->ulpTimeout = 0;
James Smartf1126682009-06-10 17:22:44 -04003867 } else
dea31012005-04-17 16:05:31 -05003868 piocb->ulpTimeout = lpfc_cmd->timeout;
James Smartf1126682009-06-10 17:22:44 -04003869
3870 if (vport->phba->sli_rev == LPFC_SLI_REV4)
3871 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05003872
James Smart2e0fef82007-06-17 19:56:36 -05003873 return 1;
dea31012005-04-17 16:05:31 -05003874}
3875
James Smart9bad7672008-12-04 22:39:02 -05003876/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003877 * lpfc_scsi_api_table_setup - Set up scsi api function jump table
James Smart3772a992009-05-22 14:50:54 -04003878 * @phba: The hba struct for which this call is being executed.
3879 * @dev_grp: The HBA PCI-Device group number.
3880 *
3881 * This routine sets up the SCSI interface API function jump table in @phba
3882 * struct.
3883 * Returns: 0 - success, -ENODEV - failure.
3884 **/
3885int
3886lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
3887{
3888
James Smartf1126682009-06-10 17:22:44 -04003889 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
3890 phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
James Smartf1126682009-06-10 17:22:44 -04003891
James Smart3772a992009-05-22 14:50:54 -04003892 switch (dev_grp) {
3893 case LPFC_PCI_DEV_LP:
3894 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s3;
3895 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
James Smartacd68592012-01-18 16:25:09 -05003896 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04003897 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
James Smart19ca7602010-11-20 23:11:55 -05003898 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04003899 break;
James Smartda0436e2009-05-22 14:51:39 -04003900 case LPFC_PCI_DEV_OC:
3901 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s4;
3902 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
James Smartacd68592012-01-18 16:25:09 -05003903 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04003904 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
James Smart19ca7602010-11-20 23:11:55 -05003905 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04003906 break;
James Smart3772a992009-05-22 14:50:54 -04003907 default:
3908 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3909 "1418 Invalid HBA PCI-device group: 0x%x\n",
3910 dev_grp);
3911 return -ENODEV;
3912 break;
3913 }
James Smart3772a992009-05-22 14:50:54 -04003914 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
James Smart84d1b002010-02-12 14:42:33 -05003915 phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
James Smart3772a992009-05-22 14:50:54 -04003916 return 0;
3917}
3918
3919/**
James Smart3621a712009-04-06 18:47:14 -04003920 * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
James Smart9bad7672008-12-04 22:39:02 -05003921 * @phba: The Hba for which this call is being executed.
3922 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
3923 * @rspiocbq: Pointer to lpfc_iocbq data structure.
3924 *
3925 * This routine is IOCB completion routine for device reset and target reset
3926 * routine. This routine release scsi buffer associated with lpfc_cmd.
3927 **/
James Smart7054a602007-04-25 09:52:34 -04003928static void
3929lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
3930 struct lpfc_iocbq *cmdiocbq,
3931 struct lpfc_iocbq *rspiocbq)
3932{
3933 struct lpfc_scsi_buf *lpfc_cmd =
3934 (struct lpfc_scsi_buf *) cmdiocbq->context1;
3935 if (lpfc_cmd)
3936 lpfc_release_scsi_buf(phba, lpfc_cmd);
3937 return;
3938}
3939
James Smart9bad7672008-12-04 22:39:02 -05003940/**
James Smart3621a712009-04-06 18:47:14 -04003941 * lpfc_info - Info entry point of scsi_host_template data structure
James Smart9bad7672008-12-04 22:39:02 -05003942 * @host: The scsi host for which this call is being executed.
3943 *
3944 * This routine provides module information about hba.
3945 *
3946 * Reutrn code:
3947 * Pointer to char - Success.
3948 **/
dea31012005-04-17 16:05:31 -05003949const char *
3950lpfc_info(struct Scsi_Host *host)
3951{
James Smart2e0fef82007-06-17 19:56:36 -05003952 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
3953 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003954 int len;
3955 static char lpfcinfobuf[384];
3956
3957 memset(lpfcinfobuf,0,384);
3958 if (phba && phba->pcidev){
3959 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
3960 len = strlen(lpfcinfobuf);
3961 snprintf(lpfcinfobuf + len,
3962 384-len,
3963 " on PCI bus %02x device %02x irq %d",
3964 phba->pcidev->bus->number,
3965 phba->pcidev->devfn,
3966 phba->pcidev->irq);
3967 len = strlen(lpfcinfobuf);
3968 if (phba->Port[0]) {
3969 snprintf(lpfcinfobuf + len,
3970 384-len,
3971 " port %s",
3972 phba->Port);
3973 }
James Smart65467b62010-01-26 23:08:29 -05003974 len = strlen(lpfcinfobuf);
3975 if (phba->sli4_hba.link_state.logical_speed) {
3976 snprintf(lpfcinfobuf + len,
3977 384-len,
3978 " Logical Link Speed: %d Mbps",
3979 phba->sli4_hba.link_state.logical_speed * 10);
3980 }
dea31012005-04-17 16:05:31 -05003981 }
3982 return lpfcinfobuf;
3983}
3984
James Smart9bad7672008-12-04 22:39:02 -05003985/**
James Smart3621a712009-04-06 18:47:14 -04003986 * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
James Smart9bad7672008-12-04 22:39:02 -05003987 * @phba: The Hba for which this call is being executed.
3988 *
3989 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
3990 * The default value of cfg_poll_tmo is 10 milliseconds.
3991 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003992static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
3993{
3994 unsigned long poll_tmo_expires =
3995 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
3996
3997 if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
3998 mod_timer(&phba->fcp_poll_timer,
3999 poll_tmo_expires);
4000}
4001
James Smart9bad7672008-12-04 22:39:02 -05004002/**
James Smart3621a712009-04-06 18:47:14 -04004003 * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
James Smart9bad7672008-12-04 22:39:02 -05004004 * @phba: The Hba for which this call is being executed.
4005 *
4006 * This routine starts the fcp_poll_timer of @phba.
4007 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004008void lpfc_poll_start_timer(struct lpfc_hba * phba)
4009{
4010 lpfc_poll_rearm_timer(phba);
4011}
4012
James Smart9bad7672008-12-04 22:39:02 -05004013/**
James Smart3621a712009-04-06 18:47:14 -04004014 * lpfc_poll_timeout - Restart polling timer
James Smart9bad7672008-12-04 22:39:02 -05004015 * @ptr: Map to lpfc_hba data structure pointer.
4016 *
4017 * This routine restarts fcp_poll timer, when FCP ring polling is enable
4018 * and FCP Ring interrupt is disable.
4019 **/
4020
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004021void lpfc_poll_timeout(unsigned long ptr)
4022{
James Smart2e0fef82007-06-17 19:56:36 -05004023 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004024
4025 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004026 lpfc_sli_handle_fast_ring_event(phba,
4027 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4028
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004029 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4030 lpfc_poll_rearm_timer(phba);
4031 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004032}
4033
James Smart9bad7672008-12-04 22:39:02 -05004034/**
James Smart3621a712009-04-06 18:47:14 -04004035 * lpfc_queuecommand - scsi_host_template queuecommand entry point
James Smart9bad7672008-12-04 22:39:02 -05004036 * @cmnd: Pointer to scsi_cmnd data structure.
4037 * @done: Pointer to done routine.
4038 *
4039 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
4040 * This routine prepares an IOCB from scsi command and provides to firmware.
4041 * The @done callback is invoked after driver finished processing the command.
4042 *
4043 * Return value :
4044 * 0 - Success
4045 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
4046 **/
dea31012005-04-17 16:05:31 -05004047static int
Jeff Garzikf2812332010-11-16 02:10:29 -05004048lpfc_queuecommand_lck(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
dea31012005-04-17 16:05:31 -05004049{
James Smart2e0fef82007-06-17 19:56:36 -05004050 struct Scsi_Host *shost = cmnd->device->host;
4051 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4052 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004053 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05004054 struct lpfc_nodelist *ndlp;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004055 struct lpfc_scsi_buf *lpfc_cmd;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004056 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004057 int err;
dea31012005-04-17 16:05:31 -05004058
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004059 err = fc_remote_port_chkready(rport);
4060 if (err) {
4061 cmnd->result = err;
dea31012005-04-17 16:05:31 -05004062 goto out_fail_command;
4063 }
James Smartdf9e1b52011-12-13 13:22:17 -05004064 /*
4065 * Do not let the mid-layer retry I/O too fast. If an I/O is retried
4066 * without waiting a bit then indicate that the device is busy.
4067 */
4068 if (cmnd->retries &&
4069 time_before(jiffies, (cmnd->jiffies_at_alloc +
4070 msecs_to_jiffies(LPFC_RETRY_PAUSE *
4071 cmnd->retries))))
4072 return SCSI_MLQUEUE_DEVICE_BUSY;
James Smart1c6f4ef52009-11-18 15:40:49 -05004073 ndlp = rdata->pnode;
dea31012005-04-17 16:05:31 -05004074
James Smartbf086112011-08-21 21:48:13 -04004075 if ((scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) &&
James Smartacd68592012-01-18 16:25:09 -05004076 (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED))) {
James Smarte2a0a9d2008-12-04 22:40:02 -05004077
James Smart6a9c52c2009-10-02 15:16:51 -04004078 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4079 "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
4080 " op:%02x str=%s without registering for"
4081 " BlockGuard - Rejecting command\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05004082 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
4083 dif_op_str[scsi_get_prot_op(cmnd)]);
4084 goto out_fail_command;
4085 }
4086
dea31012005-04-17 16:05:31 -05004087 /*
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004088 * Catch race where our node has transitioned, but the
4089 * transport is still transitioning.
dea31012005-04-17 16:05:31 -05004090 */
James Smartb522d7d2008-09-07 11:51:56 -04004091 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
Mike Christief55ca84d2011-02-25 14:04:27 -06004092 cmnd->result = ScsiResult(DID_IMM_RETRY, 0);
James Smartb522d7d2008-09-07 11:51:56 -04004093 goto out_fail_command;
4094 }
James Smart7dc517d2010-07-14 15:32:10 -04004095 if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth)
Mike Christie34963432011-02-25 14:04:28 -06004096 goto out_tgt_busy;
Mike Christiea93ce022008-08-17 15:24:41 -05004097
James Smart19ca7602010-11-20 23:11:55 -05004098 lpfc_cmd = lpfc_get_scsi_buf(phba, ndlp);
dea31012005-04-17 16:05:31 -05004099 if (lpfc_cmd == NULL) {
James Smarteaf15d52008-12-04 22:39:29 -05004100 lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05004101
James Smarte8b62012007-08-02 11:10:09 -04004102 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4103 "0707 driver's buffer pool is empty, "
4104 "IO busied\n");
dea31012005-04-17 16:05:31 -05004105 goto out_host_busy;
4106 }
4107
4108 /*
4109 * Store the midlayer's command structure for the completion phase
4110 * and complete the command initialization.
4111 */
4112 lpfc_cmd->pCmd = cmnd;
4113 lpfc_cmd->rdata = rdata;
4114 lpfc_cmd->timeout = 0;
James Smart977b5a02008-09-07 11:52:04 -04004115 lpfc_cmd->start_time = jiffies;
dea31012005-04-17 16:05:31 -05004116 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
4117 cmnd->scsi_done = done;
4118
James Smarte2a0a9d2008-12-04 22:40:02 -05004119 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
James Smart6a9c52c2009-10-02 15:16:51 -04004120 if (vport->phba->cfg_enable_bg) {
4121 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05004122 "9033 BLKGRD: rcvd protected cmd:%02x op:%02x "
4123 "str=%s\n",
4124 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
4125 dif_op_str[scsi_get_prot_op(cmnd)]);
James Smart6a9c52c2009-10-02 15:16:51 -04004126 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05004127 "9034 BLKGRD: CDB: %02x %02x %02x %02x %02x "
James Smarte4e74272009-07-19 10:01:38 -04004128 "%02x %02x %02x %02x %02x\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05004129 cmnd->cmnd[0], cmnd->cmnd[1], cmnd->cmnd[2],
4130 cmnd->cmnd[3], cmnd->cmnd[4], cmnd->cmnd[5],
4131 cmnd->cmnd[6], cmnd->cmnd[7], cmnd->cmnd[8],
4132 cmnd->cmnd[9]);
James Smart6a9c52c2009-10-02 15:16:51 -04004133 if (cmnd->cmnd[0] == READ_10)
4134 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05004135 "9035 BLKGRD: READ @ sector %llu, "
Tejun Heo83096eb2009-05-07 22:24:39 +09004136 "count %u\n",
4137 (unsigned long long)scsi_get_lba(cmnd),
4138 blk_rq_sectors(cmnd->request));
James Smart6a9c52c2009-10-02 15:16:51 -04004139 else if (cmnd->cmnd[0] == WRITE_10)
4140 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05004141 "9036 BLKGRD: WRITE @ sector %llu, "
Tejun Heo83096eb2009-05-07 22:24:39 +09004142 "count %u cmd=%p\n",
James Smart87b5c322008-12-16 10:34:09 -05004143 (unsigned long long)scsi_get_lba(cmnd),
Tejun Heo83096eb2009-05-07 22:24:39 +09004144 blk_rq_sectors(cmnd->request),
James Smarte2a0a9d2008-12-04 22:40:02 -05004145 cmnd);
James Smart6a9c52c2009-10-02 15:16:51 -04004146 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004147
4148 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
4149 } else {
James Smart6a9c52c2009-10-02 15:16:51 -04004150 if (vport->phba->cfg_enable_bg) {
James Smarte2a0a9d2008-12-04 22:40:02 -05004151 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smart6a9c52c2009-10-02 15:16:51 -04004152 "9038 BLKGRD: rcvd unprotected cmd:"
4153 "%02x op:%02x str=%s\n",
4154 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
4155 dif_op_str[scsi_get_prot_op(cmnd)]);
4156 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
4157 "9039 BLKGRD: CDB: %02x %02x %02x "
4158 "%02x %02x %02x %02x %02x %02x %02x\n",
4159 cmnd->cmnd[0], cmnd->cmnd[1],
4160 cmnd->cmnd[2], cmnd->cmnd[3],
4161 cmnd->cmnd[4], cmnd->cmnd[5],
4162 cmnd->cmnd[6], cmnd->cmnd[7],
4163 cmnd->cmnd[8], cmnd->cmnd[9]);
4164 if (cmnd->cmnd[0] == READ_10)
4165 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
4166 "9040 dbg: READ @ sector %llu, "
4167 "count %u\n",
4168 (unsigned long long)scsi_get_lba(cmnd),
Tejun Heo83096eb2009-05-07 22:24:39 +09004169 blk_rq_sectors(cmnd->request));
James Smart6a9c52c2009-10-02 15:16:51 -04004170 else if (cmnd->cmnd[0] == WRITE_10)
4171 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05004172 "9041 dbg: WRITE @ sector %llu, "
Tejun Heo83096eb2009-05-07 22:24:39 +09004173 "count %u cmd=%p\n",
James Smart87b5c322008-12-16 10:34:09 -05004174 (unsigned long long)scsi_get_lba(cmnd),
Tejun Heo83096eb2009-05-07 22:24:39 +09004175 blk_rq_sectors(cmnd->request), cmnd);
James Smart6a9c52c2009-10-02 15:16:51 -04004176 else
4177 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05004178 "9042 dbg: parser not implemented\n");
James Smart6a9c52c2009-10-02 15:16:51 -04004179 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004180 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
4181 }
4182
dea31012005-04-17 16:05:31 -05004183 if (err)
4184 goto out_host_busy_free_buf;
4185
James Smart2e0fef82007-06-17 19:56:36 -05004186 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea31012005-04-17 16:05:31 -05004187
James Smart977b5a02008-09-07 11:52:04 -04004188 atomic_inc(&ndlp->cmd_pending);
James Smart3772a992009-05-22 14:50:54 -04004189 err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
James Smart92d7f7b2007-06-17 19:56:38 -05004190 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
James Smarteaf15d52008-12-04 22:39:29 -05004191 if (err) {
4192 atomic_dec(&ndlp->cmd_pending);
dea31012005-04-17 16:05:31 -05004193 goto out_host_busy_free_buf;
James Smarteaf15d52008-12-04 22:39:29 -05004194 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004195 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004196 spin_unlock(shost->host_lock);
4197 lpfc_sli_handle_fast_ring_event(phba,
4198 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4199
4200 spin_lock(shost->host_lock);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004201 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4202 lpfc_poll_rearm_timer(phba);
4203 }
4204
dea31012005-04-17 16:05:31 -05004205 return 0;
4206
4207 out_host_busy_free_buf:
James Smartbcf4dbf2006-07-06 15:50:08 -04004208 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004209 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004210 out_host_busy:
4211 return SCSI_MLQUEUE_HOST_BUSY;
4212
Mike Christie34963432011-02-25 14:04:28 -06004213 out_tgt_busy:
4214 return SCSI_MLQUEUE_TARGET_BUSY;
4215
dea31012005-04-17 16:05:31 -05004216 out_fail_command:
4217 done(cmnd);
4218 return 0;
4219}
4220
Jeff Garzikf2812332010-11-16 02:10:29 -05004221static DEF_SCSI_QCMD(lpfc_queuecommand)
4222
James Smart9bad7672008-12-04 22:39:02 -05004223/**
James Smart3621a712009-04-06 18:47:14 -04004224 * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05004225 * @cmnd: Pointer to scsi_cmnd data structure.
4226 *
4227 * This routine aborts @cmnd pending in base driver.
4228 *
4229 * Return code :
4230 * 0x2003 - Error
4231 * 0x2002 - Success
4232 **/
dea31012005-04-17 16:05:31 -05004233static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05004234lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004235{
James Smart2e0fef82007-06-17 19:56:36 -05004236 struct Scsi_Host *shost = cmnd->device->host;
4237 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4238 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004239 struct lpfc_iocbq *iocb;
4240 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05004241 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05004242 IOCB_t *cmd, *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004243 int ret = SUCCESS;
James Smartfa61a542008-01-11 01:52:42 -05004244 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004245
James Smart589a52d2010-07-14 15:30:54 -04004246 ret = fc_block_scsi_eh(cmnd);
4247 if (ret)
4248 return ret;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004249 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
James Smarteee88772010-09-29 11:19:08 -04004250 if (!lpfc_cmd) {
4251 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4252 "2873 SCSI Layer I/O Abort Request IO CMPL Status "
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04004253 "x%x ID %d LUN %d\n",
4254 ret, cmnd->device->id, cmnd->device->lun);
James Smarteee88772010-09-29 11:19:08 -04004255 return SUCCESS;
4256 }
dea31012005-04-17 16:05:31 -05004257
4258 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004259 * If pCmd field of the corresponding lpfc_scsi_buf structure
4260 * points to a different SCSI command, then the driver has
4261 * already completed this command, but the midlayer did not
4262 * see the completion before the eh fired. Just return
4263 * SUCCESS.
dea31012005-04-17 16:05:31 -05004264 */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004265 iocb = &lpfc_cmd->cur_iocbq;
4266 if (lpfc_cmd->pCmd != cmnd)
4267 goto out;
dea31012005-04-17 16:05:31 -05004268
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004269 BUG_ON(iocb->context1 != lpfc_cmd);
dea31012005-04-17 16:05:31 -05004270
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004271 abtsiocb = lpfc_sli_get_iocbq(phba);
4272 if (abtsiocb == NULL) {
4273 ret = FAILED;
dea31012005-04-17 16:05:31 -05004274 goto out;
4275 }
4276
dea31012005-04-17 16:05:31 -05004277 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004278 * The scsi command can not be in txq and it is in flight because the
4279 * pCmd is still pointig at the SCSI command we have to abort. There
4280 * is no need to search the txcmplq. Just send an abort to the FW.
dea31012005-04-17 16:05:31 -05004281 */
dea31012005-04-17 16:05:31 -05004282
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004283 cmd = &iocb->iocb;
4284 icmd = &abtsiocb->iocb;
4285 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
4286 icmd->un.acxri.abortContextTag = cmd->ulpContext;
James Smart3772a992009-05-22 14:50:54 -04004287 if (phba->sli_rev == LPFC_SLI_REV4)
4288 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
4289 else
4290 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05004291
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004292 icmd->ulpLe = 1;
4293 icmd->ulpClass = cmd->ulpClass;
James Smart5ffc2662009-11-18 15:39:44 -05004294
4295 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
4296 abtsiocb->fcp_wqidx = iocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05004297 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05004298
James Smart2e0fef82007-06-17 19:56:36 -05004299 if (lpfc_is_link_up(phba))
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004300 icmd->ulpCommand = CMD_ABORT_XRI_CN;
4301 else
4302 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea31012005-04-17 16:05:31 -05004303
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004304 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smart2e0fef82007-06-17 19:56:36 -05004305 abtsiocb->vport = vport;
James Smart3772a992009-05-22 14:50:54 -04004306 if (lpfc_sli_issue_iocb(phba, LPFC_FCP_RING, abtsiocb, 0) ==
4307 IOCB_ERROR) {
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004308 lpfc_sli_release_iocbq(phba, abtsiocb);
4309 ret = FAILED;
4310 goto out;
4311 }
4312
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004313 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
James Smart45ed1192009-10-02 15:17:02 -04004314 lpfc_sli_handle_fast_ring_event(phba,
4315 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004316
James Smartfa61a542008-01-11 01:52:42 -05004317 lpfc_cmd->waitq = &waitq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004318 /* Wait for abort to complete */
James Smartfa61a542008-01-11 01:52:42 -05004319 wait_event_timeout(waitq,
4320 (lpfc_cmd->pCmd != cmnd),
4321 (2*vport->cfg_devloss_tmo*HZ));
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004322
James Smartfa61a542008-01-11 01:52:42 -05004323 spin_lock_irq(shost->host_lock);
4324 lpfc_cmd->waitq = NULL;
4325 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004326
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004327 if (lpfc_cmd->pCmd == cmnd) {
4328 ret = FAILED;
James Smarte8b62012007-08-02 11:10:09 -04004329 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4330 "0748 abort handler timed out waiting "
4331 "for abort to complete: ret %#x, ID %d, "
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04004332 "LUN %d\n",
4333 ret, cmnd->device->id, cmnd->device->lun);
dea31012005-04-17 16:05:31 -05004334 }
4335
4336 out:
James Smarte8b62012007-08-02 11:10:09 -04004337 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4338 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04004339 "LUN %d\n", ret, cmnd->device->id,
4340 cmnd->device->lun);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004341 return ret;
dea31012005-04-17 16:05:31 -05004342}
4343
James Smartbbb9d182009-06-10 17:23:16 -04004344static char *
4345lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
4346{
4347 switch (task_mgmt_cmd) {
4348 case FCP_ABORT_TASK_SET:
4349 return "ABORT_TASK_SET";
4350 case FCP_CLEAR_TASK_SET:
4351 return "FCP_CLEAR_TASK_SET";
4352 case FCP_BUS_RESET:
4353 return "FCP_BUS_RESET";
4354 case FCP_LUN_RESET:
4355 return "FCP_LUN_RESET";
4356 case FCP_TARGET_RESET:
4357 return "FCP_TARGET_RESET";
4358 case FCP_CLEAR_ACA:
4359 return "FCP_CLEAR_ACA";
4360 case FCP_TERMINATE_TASK:
4361 return "FCP_TERMINATE_TASK";
4362 default:
4363 return "unknown";
4364 }
4365}
4366
4367/**
4368 * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
4369 * @vport: The virtual port for which this call is being executed.
4370 * @rdata: Pointer to remote port local data
4371 * @tgt_id: Target ID of remote device.
4372 * @lun_id: Lun number for the TMF
4373 * @task_mgmt_cmd: type of TMF to send
4374 *
4375 * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
4376 * a remote port.
4377 *
4378 * Return Code:
4379 * 0x2003 - Error
4380 * 0x2002 - Success.
4381 **/
4382static int
4383lpfc_send_taskmgmt(struct lpfc_vport *vport, struct lpfc_rport_data *rdata,
4384 unsigned tgt_id, unsigned int lun_id,
4385 uint8_t task_mgmt_cmd)
4386{
4387 struct lpfc_hba *phba = vport->phba;
4388 struct lpfc_scsi_buf *lpfc_cmd;
4389 struct lpfc_iocbq *iocbq;
4390 struct lpfc_iocbq *iocbqrsp;
James Smart5989b8d2010-10-22 11:06:56 -04004391 struct lpfc_nodelist *pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004392 int ret;
4393 int status;
4394
James Smart5989b8d2010-10-22 11:06:56 -04004395 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
James Smartbbb9d182009-06-10 17:23:16 -04004396 return FAILED;
4397
James Smart19ca7602010-11-20 23:11:55 -05004398 lpfc_cmd = lpfc_get_scsi_buf(phba, rdata->pnode);
James Smartbbb9d182009-06-10 17:23:16 -04004399 if (lpfc_cmd == NULL)
4400 return FAILED;
4401 lpfc_cmd->timeout = 60;
4402 lpfc_cmd->rdata = rdata;
4403
4404 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
4405 task_mgmt_cmd);
4406 if (!status) {
4407 lpfc_release_scsi_buf(phba, lpfc_cmd);
4408 return FAILED;
4409 }
4410
4411 iocbq = &lpfc_cmd->cur_iocbq;
4412 iocbqrsp = lpfc_sli_get_iocbq(phba);
4413 if (iocbqrsp == NULL) {
4414 lpfc_release_scsi_buf(phba, lpfc_cmd);
4415 return FAILED;
4416 }
4417
4418 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4419 "0702 Issue %s to TGT %d LUN %d "
James Smart6d368e52011-05-24 11:44:12 -04004420 "rpi x%x nlp_flag x%x Data: x%x x%x\n",
James Smartbbb9d182009-06-10 17:23:16 -04004421 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
James Smart6d368e52011-05-24 11:44:12 -04004422 pnode->nlp_rpi, pnode->nlp_flag, iocbq->sli4_xritag,
4423 iocbq->iocb_flag);
James Smartbbb9d182009-06-10 17:23:16 -04004424
4425 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
4426 iocbq, iocbqrsp, lpfc_cmd->timeout);
4427 if (status != IOCB_SUCCESS) {
4428 if (status == IOCB_TIMEDOUT) {
4429 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
4430 ret = TIMEOUT_ERROR;
4431 } else
4432 ret = FAILED;
4433 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
4434 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smart6d368e52011-05-24 11:44:12 -04004435 "0727 TMF %s to TGT %d LUN %d failed (%d, %d) "
4436 "iocb_flag x%x\n",
James Smartbbb9d182009-06-10 17:23:16 -04004437 lpfc_taskmgmt_name(task_mgmt_cmd),
4438 tgt_id, lun_id, iocbqrsp->iocb.ulpStatus,
James Smart6d368e52011-05-24 11:44:12 -04004439 iocbqrsp->iocb.un.ulpWord[4],
4440 iocbq->iocb_flag);
James Smart2a9bf3d2010-06-07 15:24:45 -04004441 } else if (status == IOCB_BUSY)
4442 ret = FAILED;
4443 else
James Smartbbb9d182009-06-10 17:23:16 -04004444 ret = SUCCESS;
4445
4446 lpfc_sli_release_iocbq(phba, iocbqrsp);
4447
4448 if (ret != TIMEOUT_ERROR)
4449 lpfc_release_scsi_buf(phba, lpfc_cmd);
4450
4451 return ret;
4452}
4453
4454/**
4455 * lpfc_chk_tgt_mapped -
4456 * @vport: The virtual port to check on
4457 * @cmnd: Pointer to scsi_cmnd data structure.
4458 *
4459 * This routine delays until the scsi target (aka rport) for the
4460 * command exists (is present and logged in) or we declare it non-existent.
4461 *
4462 * Return code :
4463 * 0x2003 - Error
4464 * 0x2002 - Success
4465 **/
4466static int
4467lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
4468{
4469 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05004470 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004471 unsigned long later;
4472
James Smart1c6f4ef52009-11-18 15:40:49 -05004473 if (!rdata) {
4474 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4475 "0797 Tgt Map rport failure: rdata x%p\n", rdata);
4476 return FAILED;
4477 }
4478 pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004479 /*
4480 * If target is not in a MAPPED state, delay until
4481 * target is rediscovered or devloss timeout expires.
4482 */
4483 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
4484 while (time_after(later, jiffies)) {
4485 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
4486 return FAILED;
4487 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
4488 return SUCCESS;
4489 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
4490 rdata = cmnd->device->hostdata;
4491 if (!rdata)
4492 return FAILED;
4493 pnode = rdata->pnode;
4494 }
4495 if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
4496 (pnode->nlp_state != NLP_STE_MAPPED_NODE))
4497 return FAILED;
4498 return SUCCESS;
4499}
4500
4501/**
4502 * lpfc_reset_flush_io_context -
4503 * @vport: The virtual port (scsi_host) for the flush context
4504 * @tgt_id: If aborting by Target contect - specifies the target id
4505 * @lun_id: If aborting by Lun context - specifies the lun id
4506 * @context: specifies the context level to flush at.
4507 *
4508 * After a reset condition via TMF, we need to flush orphaned i/o
4509 * contexts from the adapter. This routine aborts any contexts
4510 * outstanding, then waits for their completions. The wait is
4511 * bounded by devloss_tmo though.
4512 *
4513 * Return code :
4514 * 0x2003 - Error
4515 * 0x2002 - Success
4516 **/
4517static int
4518lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
4519 uint64_t lun_id, lpfc_ctx_cmd context)
4520{
4521 struct lpfc_hba *phba = vport->phba;
4522 unsigned long later;
4523 int cnt;
4524
4525 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
4526 if (cnt)
4527 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
4528 tgt_id, lun_id, context);
4529 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
4530 while (time_after(later, jiffies) && cnt) {
4531 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
4532 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
4533 }
4534 if (cnt) {
4535 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4536 "0724 I/O flush failure for context %s : cnt x%x\n",
4537 ((context == LPFC_CTX_LUN) ? "LUN" :
4538 ((context == LPFC_CTX_TGT) ? "TGT" :
4539 ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
4540 cnt);
4541 return FAILED;
4542 }
4543 return SUCCESS;
4544}
4545
James Smart9bad7672008-12-04 22:39:02 -05004546/**
James Smart3621a712009-04-06 18:47:14 -04004547 * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
James Smart9bad7672008-12-04 22:39:02 -05004548 * @cmnd: Pointer to scsi_cmnd data structure.
4549 *
James Smartbbb9d182009-06-10 17:23:16 -04004550 * This routine does a device reset by sending a LUN_RESET task management
James Smart9bad7672008-12-04 22:39:02 -05004551 * command.
4552 *
4553 * Return code :
4554 * 0x2003 - Error
James Smart3621a712009-04-06 18:47:14 -04004555 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05004556 **/
dea31012005-04-17 16:05:31 -05004557static int
James Smart7054a602007-04-25 09:52:34 -04004558lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004559{
James Smart2e0fef82007-06-17 19:56:36 -05004560 struct Scsi_Host *shost = cmnd->device->host;
4561 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05004562 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05004563 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004564 unsigned tgt_id = cmnd->device->id;
4565 unsigned int lun_id = cmnd->device->lun;
James Smartea2151b2008-09-07 11:52:10 -04004566 struct lpfc_scsi_event_header scsi_event;
James Smartbbb9d182009-06-10 17:23:16 -04004567 int status;
dea31012005-04-17 16:05:31 -05004568
James Smart1c6f4ef52009-11-18 15:40:49 -05004569 if (!rdata) {
4570 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4571 "0798 Device Reset rport failure: rdata x%p\n", rdata);
4572 return FAILED;
4573 }
4574 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04004575 status = fc_block_scsi_eh(cmnd);
4576 if (status)
4577 return status;
James Smartbbb9d182009-06-10 17:23:16 -04004578
4579 status = lpfc_chk_tgt_mapped(vport, cmnd);
4580 if (status == FAILED) {
4581 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4582 "0721 Device Reset rport failure: rdata x%p\n", rdata);
4583 return FAILED;
4584 }
4585
4586 scsi_event.event_type = FC_REG_SCSI_EVENT;
4587 scsi_event.subcategory = LPFC_EVENT_LUNRESET;
4588 scsi_event.lun = lun_id;
4589 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
4590 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
4591
4592 fc_host_post_vendor_event(shost, fc_get_event_number(),
4593 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
4594
4595 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
4596 FCP_LUN_RESET);
4597
4598 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4599 "0713 SCSI layer issued Device Reset (%d, %d) "
4600 "return x%x\n", tgt_id, lun_id, status);
4601
dea31012005-04-17 16:05:31 -05004602 /*
James Smartbbb9d182009-06-10 17:23:16 -04004603 * We have to clean up i/o as : they may be orphaned by the TMF;
4604 * or if the TMF failed, they may be in an indeterminate state.
4605 * So, continue on.
4606 * We will report success if all the i/o aborts successfully.
dea31012005-04-17 16:05:31 -05004607 */
James Smartbbb9d182009-06-10 17:23:16 -04004608 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
4609 LPFC_CTX_LUN);
4610 return status;
4611}
4612
4613/**
4614 * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
4615 * @cmnd: Pointer to scsi_cmnd data structure.
4616 *
4617 * This routine does a target reset by sending a TARGET_RESET task management
4618 * command.
4619 *
4620 * Return code :
4621 * 0x2003 - Error
4622 * 0x2002 - Success
4623 **/
4624static int
4625lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
4626{
4627 struct Scsi_Host *shost = cmnd->device->host;
4628 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4629 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05004630 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004631 unsigned tgt_id = cmnd->device->id;
4632 unsigned int lun_id = cmnd->device->lun;
4633 struct lpfc_scsi_event_header scsi_event;
4634 int status;
4635
James Smart1c6f4ef52009-11-18 15:40:49 -05004636 if (!rdata) {
4637 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4638 "0799 Target Reset rport failure: rdata x%p\n", rdata);
4639 return FAILED;
4640 }
4641 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04004642 status = fc_block_scsi_eh(cmnd);
4643 if (status)
4644 return status;
James Smartbbb9d182009-06-10 17:23:16 -04004645
4646 status = lpfc_chk_tgt_mapped(vport, cmnd);
4647 if (status == FAILED) {
4648 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4649 "0722 Target Reset rport failure: rdata x%p\n", rdata);
4650 return FAILED;
dea31012005-04-17 16:05:31 -05004651 }
James Smartea2151b2008-09-07 11:52:10 -04004652
4653 scsi_event.event_type = FC_REG_SCSI_EVENT;
4654 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
4655 scsi_event.lun = 0;
4656 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
4657 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
4658
James Smartbbb9d182009-06-10 17:23:16 -04004659 fc_host_post_vendor_event(shost, fc_get_event_number(),
4660 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -04004661
James Smartbbb9d182009-06-10 17:23:16 -04004662 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
4663 FCP_TARGET_RESET);
dea31012005-04-17 16:05:31 -05004664
James Smarte8b62012007-08-02 11:10:09 -04004665 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smartbbb9d182009-06-10 17:23:16 -04004666 "0723 SCSI layer issued Target Reset (%d, %d) "
4667 "return x%x\n", tgt_id, lun_id, status);
4668
4669 /*
4670 * We have to clean up i/o as : they may be orphaned by the TMF;
4671 * or if the TMF failed, they may be in an indeterminate state.
4672 * So, continue on.
4673 * We will report success if all the i/o aborts successfully.
4674 */
4675 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
4676 LPFC_CTX_TGT);
4677 return status;
dea31012005-04-17 16:05:31 -05004678}
4679
James Smart9bad7672008-12-04 22:39:02 -05004680/**
James Smart3621a712009-04-06 18:47:14 -04004681 * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05004682 * @cmnd: Pointer to scsi_cmnd data structure.
4683 *
James Smartbbb9d182009-06-10 17:23:16 -04004684 * This routine does target reset to all targets on @cmnd->device->host.
4685 * This emulates Parallel SCSI Bus Reset Semantics.
James Smart9bad7672008-12-04 22:39:02 -05004686 *
James Smartbbb9d182009-06-10 17:23:16 -04004687 * Return code :
4688 * 0x2003 - Error
4689 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05004690 **/
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04004691static int
James Smart7054a602007-04-25 09:52:34 -04004692lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004693{
James Smart2e0fef82007-06-17 19:56:36 -05004694 struct Scsi_Host *shost = cmnd->device->host;
4695 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05004696 struct lpfc_nodelist *ndlp = NULL;
James Smartea2151b2008-09-07 11:52:10 -04004697 struct lpfc_scsi_event_header scsi_event;
James Smartbbb9d182009-06-10 17:23:16 -04004698 int match;
4699 int ret = SUCCESS, status, i;
James Smartea2151b2008-09-07 11:52:10 -04004700
4701 scsi_event.event_type = FC_REG_SCSI_EVENT;
4702 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
4703 scsi_event.lun = 0;
4704 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
4705 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
4706
James Smartbbb9d182009-06-10 17:23:16 -04004707 fc_host_post_vendor_event(shost, fc_get_event_number(),
4708 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
dea31012005-04-17 16:05:31 -05004709
James Smartbf086112011-08-21 21:48:13 -04004710 status = fc_block_scsi_eh(cmnd);
4711 if (status)
4712 return status;
James Smartbbb9d182009-06-10 17:23:16 -04004713
dea31012005-04-17 16:05:31 -05004714 /*
4715 * Since the driver manages a single bus device, reset all
4716 * targets known to the driver. Should any target reset
4717 * fail, this routine returns failure to the midlayer.
4718 */
James Smarte17da182006-07-06 15:49:25 -04004719 for (i = 0; i < LPFC_MAX_TARGET; i++) {
James Smart685f0bf2007-04-25 09:53:08 -04004720 /* Search for mapped node by target ID */
dea31012005-04-17 16:05:31 -05004721 match = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004722 spin_lock_irq(shost->host_lock);
4723 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05004724 if (!NLP_CHK_NODE_ACT(ndlp))
4725 continue;
James Smart685f0bf2007-04-25 09:53:08 -04004726 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
James Smart915caaa2008-06-14 22:52:38 -04004727 ndlp->nlp_sid == i &&
James Smart685f0bf2007-04-25 09:53:08 -04004728 ndlp->rport) {
dea31012005-04-17 16:05:31 -05004729 match = 1;
4730 break;
4731 }
4732 }
James Smart2e0fef82007-06-17 19:56:36 -05004733 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004734 if (!match)
4735 continue;
James Smartbbb9d182009-06-10 17:23:16 -04004736
4737 status = lpfc_send_taskmgmt(vport, ndlp->rport->dd_data,
4738 i, 0, FCP_TARGET_RESET);
4739
4740 if (status != SUCCESS) {
James Smarte8b62012007-08-02 11:10:09 -04004741 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4742 "0700 Bus Reset on target %d failed\n",
4743 i);
James Smart915caaa2008-06-14 22:52:38 -04004744 ret = FAILED;
dea31012005-04-17 16:05:31 -05004745 }
4746 }
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05004747 /*
James Smartbbb9d182009-06-10 17:23:16 -04004748 * We have to clean up i/o as : they may be orphaned by the TMFs
4749 * above; or if any of the TMFs failed, they may be in an
4750 * indeterminate state.
4751 * We will report success if all the i/o aborts successfully.
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05004752 */
James Smartbbb9d182009-06-10 17:23:16 -04004753
4754 status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
4755 if (status != SUCCESS)
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05004756 ret = FAILED;
James Smartbbb9d182009-06-10 17:23:16 -04004757
James Smarte8b62012007-08-02 11:10:09 -04004758 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4759 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea31012005-04-17 16:05:31 -05004760 return ret;
4761}
4762
James Smart9bad7672008-12-04 22:39:02 -05004763/**
James Smart3621a712009-04-06 18:47:14 -04004764 * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
James Smart9bad7672008-12-04 22:39:02 -05004765 * @sdev: Pointer to scsi_device.
4766 *
4767 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
4768 * globally available list of scsi buffers. This routine also makes sure scsi
4769 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
4770 * of scsi buffer exists for the lifetime of the driver.
4771 *
4772 * Return codes:
4773 * non-0 - Error
4774 * 0 - Success
4775 **/
dea31012005-04-17 16:05:31 -05004776static int
dea31012005-04-17 16:05:31 -05004777lpfc_slave_alloc(struct scsi_device *sdev)
4778{
James Smart2e0fef82007-06-17 19:56:36 -05004779 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
4780 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004781 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
James Smart3772a992009-05-22 14:50:54 -04004782 uint32_t total = 0;
dea31012005-04-17 16:05:31 -05004783 uint32_t num_to_alloc = 0;
James Smart3772a992009-05-22 14:50:54 -04004784 int num_allocated = 0;
James Smartd7c47992010-06-08 18:31:54 -04004785 uint32_t sdev_cnt;
dea31012005-04-17 16:05:31 -05004786
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004787 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05004788 return -ENXIO;
4789
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004790 sdev->hostdata = rport->dd_data;
James Smartd7c47992010-06-08 18:31:54 -04004791 sdev_cnt = atomic_inc_return(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05004792
4793 /*
4794 * Populate the cmds_per_lun count scsi_bufs into this host's globally
4795 * available list of scsi buffers. Don't allocate more than the
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04004796 * HBA limit conveyed to the midlayer via the host structure. The
4797 * formula accounts for the lun_queue_depth + error handlers + 1
4798 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea31012005-04-17 16:05:31 -05004799 */
4800 total = phba->total_scsi_bufs;
James Smart3de2a652007-08-02 11:09:59 -04004801 num_to_alloc = vport->cfg_lun_queue_depth + 2;
James Smart92d7f7b2007-06-17 19:56:38 -05004802
James Smartd7c47992010-06-08 18:31:54 -04004803 /* If allocated buffers are enough do nothing */
4804 if ((sdev_cnt * (vport->cfg_lun_queue_depth + 2)) < total)
4805 return 0;
4806
James Smart92d7f7b2007-06-17 19:56:38 -05004807 /* Allow some exchanges to be available always to complete discovery */
4808 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04004809 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4810 "0704 At limitation of %d preallocated "
4811 "command buffers\n", total);
dea31012005-04-17 16:05:31 -05004812 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05004813 /* Allow some exchanges to be available always to complete discovery */
4814 } else if (total + num_to_alloc >
4815 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04004816 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4817 "0705 Allocation request of %d "
4818 "command buffers will exceed max of %d. "
4819 "Reducing allocation request to %d.\n",
4820 num_to_alloc, phba->cfg_hba_queue_depth,
4821 (phba->cfg_hba_queue_depth - total));
dea31012005-04-17 16:05:31 -05004822 num_to_alloc = phba->cfg_hba_queue_depth - total;
4823 }
James Smart3772a992009-05-22 14:50:54 -04004824 num_allocated = lpfc_new_scsi_buf(vport, num_to_alloc);
4825 if (num_to_alloc != num_allocated) {
4826 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4827 "0708 Allocation request of %d "
4828 "command buffers did not succeed. "
4829 "Allocated %d buffers.\n",
4830 num_to_alloc, num_allocated);
dea31012005-04-17 16:05:31 -05004831 }
James Smart1c6f4ef52009-11-18 15:40:49 -05004832 if (num_allocated > 0)
4833 phba->total_scsi_bufs += num_allocated;
dea31012005-04-17 16:05:31 -05004834 return 0;
4835}
4836
James Smart9bad7672008-12-04 22:39:02 -05004837/**
James Smart3621a712009-04-06 18:47:14 -04004838 * lpfc_slave_configure - scsi_host_template slave_configure entry point
James Smart9bad7672008-12-04 22:39:02 -05004839 * @sdev: Pointer to scsi_device.
4840 *
4841 * This routine configures following items
4842 * - Tag command queuing support for @sdev if supported.
James Smart9bad7672008-12-04 22:39:02 -05004843 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
4844 *
4845 * Return codes:
4846 * 0 - Success
4847 **/
dea31012005-04-17 16:05:31 -05004848static int
4849lpfc_slave_configure(struct scsi_device *sdev)
4850{
James Smart2e0fef82007-06-17 19:56:36 -05004851 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
4852 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004853
4854 if (sdev->tagged_supported)
James Smart3de2a652007-08-02 11:09:59 -04004855 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05004856 else
James Smart3de2a652007-08-02 11:09:59 -04004857 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05004858
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004859 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004860 lpfc_sli_handle_fast_ring_event(phba,
4861 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004862 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4863 lpfc_poll_rearm_timer(phba);
4864 }
4865
dea31012005-04-17 16:05:31 -05004866 return 0;
4867}
4868
James Smart9bad7672008-12-04 22:39:02 -05004869/**
James Smart3621a712009-04-06 18:47:14 -04004870 * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
James Smart9bad7672008-12-04 22:39:02 -05004871 * @sdev: Pointer to scsi_device.
4872 *
4873 * This routine sets @sdev hostatdata filed to null.
4874 **/
dea31012005-04-17 16:05:31 -05004875static void
4876lpfc_slave_destroy(struct scsi_device *sdev)
4877{
James Smartd7c47992010-06-08 18:31:54 -04004878 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
4879 struct lpfc_hba *phba = vport->phba;
4880 atomic_dec(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05004881 sdev->hostdata = NULL;
4882 return;
4883}
4884
James Smart92d7f7b2007-06-17 19:56:38 -05004885
dea31012005-04-17 16:05:31 -05004886struct scsi_host_template lpfc_template = {
4887 .module = THIS_MODULE,
4888 .name = LPFC_DRIVER_NAME,
4889 .info = lpfc_info,
4890 .queuecommand = lpfc_queuecommand,
4891 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04004892 .eh_device_reset_handler = lpfc_device_reset_handler,
4893 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart7054a602007-04-25 09:52:34 -04004894 .eh_bus_reset_handler = lpfc_bus_reset_handler,
dea31012005-04-17 16:05:31 -05004895 .slave_alloc = lpfc_slave_alloc,
4896 .slave_configure = lpfc_slave_configure,
4897 .slave_destroy = lpfc_slave_destroy,
James Smart47a86172007-04-25 09:53:22 -04004898 .scan_finished = lpfc_scan_finished,
dea31012005-04-17 16:05:31 -05004899 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05004900 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
dea31012005-04-17 16:05:31 -05004901 .cmd_per_lun = LPFC_CMD_PER_LUN,
4902 .use_clustering = ENABLE_CLUSTERING,
James Smart2e0fef82007-06-17 19:56:36 -05004903 .shost_attrs = lpfc_hba_attrs,
James.Smart@Emulex.Com564b2962005-06-25 10:34:17 -04004904 .max_sectors = 0xFFFF,
James Smartf1c3b0f2009-07-19 10:01:32 -04004905 .vendor_id = LPFC_NL_VENDOR_ID,
James Smart5ffc2662009-11-18 15:39:44 -05004906 .change_queue_depth = lpfc_change_queue_depth,
dea31012005-04-17 16:05:31 -05004907};
James Smart3de2a652007-08-02 11:09:59 -04004908
4909struct scsi_host_template lpfc_vport_template = {
4910 .module = THIS_MODULE,
4911 .name = LPFC_DRIVER_NAME,
4912 .info = lpfc_info,
4913 .queuecommand = lpfc_queuecommand,
4914 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04004915 .eh_device_reset_handler = lpfc_device_reset_handler,
4916 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart3de2a652007-08-02 11:09:59 -04004917 .eh_bus_reset_handler = lpfc_bus_reset_handler,
4918 .slave_alloc = lpfc_slave_alloc,
4919 .slave_configure = lpfc_slave_configure,
4920 .slave_destroy = lpfc_slave_destroy,
4921 .scan_finished = lpfc_scan_finished,
4922 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05004923 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
James Smart3de2a652007-08-02 11:09:59 -04004924 .cmd_per_lun = LPFC_CMD_PER_LUN,
4925 .use_clustering = ENABLE_CLUSTERING,
4926 .shost_attrs = lpfc_vport_attrs,
4927 .max_sectors = 0xFFFF,
James Smart5ffc2662009-11-18 15:39:44 -05004928 .change_queue_depth = lpfc_change_queue_depth,
James Smart3de2a652007-08-02 11:09:59 -04004929};