blob: f1af3f9dead41f574c71bed3866f6e2cb3ae7628 [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 Smart792581d2011-03-11 16:06:44 -05004 * Copyright (C) 2004-2011 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
1283/*
1284 * Given a scsi cmnd, determine the BlockGuard tags to be used with it
1285 * @sc: The SCSI command to examine
1286 * @reftag: (out) BlockGuard reference tag for transmitted data
1287 * @apptag: (out) BlockGuard application tag for transmitted data
1288 * @new_guard (in) Value to replace CRC with if needed
1289 *
1290 * Returns (1) if error injection was performed, (0) otherwise
1291 */
1292static int
1293lpfc_bg_err_inject(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1294 uint32_t *reftag, uint16_t *apptag, uint32_t new_guard)
1295{
1296 struct scatterlist *sgpe; /* s/g prot entry */
1297 struct scatterlist *sgde; /* s/g data entry */
1298 struct scsi_dif_tuple *src;
1299 uint32_t op = scsi_get_prot_op(sc);
1300 uint32_t blksize;
1301 uint32_t numblks;
1302 sector_t lba;
1303 int rc = 0;
1304
1305 if (op == SCSI_PROT_NORMAL)
1306 return 0;
1307
1308 lba = scsi_get_lba(sc);
1309 if (phba->lpfc_injerr_lba != LPFC_INJERR_LBA_OFF) {
1310 blksize = lpfc_cmd_blksize(sc);
1311 numblks = (scsi_bufflen(sc) + blksize - 1) / blksize;
1312
1313 /* Make sure we have the right LBA if one is specified */
1314 if ((phba->lpfc_injerr_lba < lba) ||
1315 (phba->lpfc_injerr_lba >= (lba + numblks)))
1316 return 0;
1317 }
1318
1319 sgpe = scsi_prot_sglist(sc);
1320 sgde = scsi_sglist(sc);
1321
1322 /* Should we change the Reference Tag */
1323 if (reftag) {
1324 /*
1325 * If we are SCSI_PROT_WRITE_STRIP, the protection data is
1326 * being stripped from the wire, thus it doesn't matter.
1327 */
1328 if ((op == SCSI_PROT_WRITE_PASS) ||
1329 (op == SCSI_PROT_WRITE_INSERT)) {
1330 if (phba->lpfc_injerr_wref_cnt) {
1331
1332 /* DEADBEEF will be the reftag on the wire */
1333 *reftag = 0xDEADBEEF;
1334 phba->lpfc_injerr_wref_cnt--;
1335 phba->lpfc_injerr_lba = LPFC_INJERR_LBA_OFF;
1336 rc = 1;
1337
1338 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1339 "9081 BLKGRD: Injecting reftag error: "
1340 "write lba x%lx\n", (unsigned long)lba);
1341 }
1342 } else {
1343 if (phba->lpfc_injerr_rref_cnt) {
1344 *reftag = 0xDEADBEEF;
1345 phba->lpfc_injerr_rref_cnt--;
1346 phba->lpfc_injerr_lba = LPFC_INJERR_LBA_OFF;
1347 rc = 1;
1348
1349 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1350 "9076 BLKGRD: Injecting reftag error: "
1351 "read lba x%lx\n", (unsigned long)lba);
1352 }
1353 }
1354 }
1355
1356 /* Should we change the Application Tag */
1357 if (apptag) {
1358 /*
1359 * If we are SCSI_PROT_WRITE_STRIP, the protection data is
1360 * being stripped from the wire, thus it doesn't matter.
1361 */
1362 if ((op == SCSI_PROT_WRITE_PASS) ||
1363 (op == SCSI_PROT_WRITE_INSERT)) {
1364 if (phba->lpfc_injerr_wapp_cnt) {
1365
1366 /* DEAD will be the apptag on the wire */
1367 *apptag = 0xDEAD;
1368 phba->lpfc_injerr_wapp_cnt--;
1369 phba->lpfc_injerr_lba = LPFC_INJERR_LBA_OFF;
1370 rc = 1;
1371
1372 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1373 "9077 BLKGRD: Injecting apptag error: "
1374 "write lba x%lx\n", (unsigned long)lba);
1375 }
1376 } else {
1377 if (phba->lpfc_injerr_rapp_cnt) {
1378 *apptag = 0xDEAD;
1379 phba->lpfc_injerr_rapp_cnt--;
1380 phba->lpfc_injerr_lba = LPFC_INJERR_LBA_OFF;
1381 rc = 1;
1382
1383 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1384 "9078 BLKGRD: Injecting apptag error: "
1385 "read lba x%lx\n", (unsigned long)lba);
1386 }
1387 }
1388 }
1389
1390 /* Should we change the Guard Tag */
1391
1392 /*
1393 * If we are SCSI_PROT_WRITE_INSERT, the protection data is
1394 * being on the wire is being fully generated on the HBA.
1395 * The host cannot change it or force an error.
1396 */
1397 if (((op == SCSI_PROT_WRITE_STRIP) ||
1398 (op == SCSI_PROT_WRITE_PASS)) &&
1399 phba->lpfc_injerr_wgrd_cnt) {
1400 if (sgpe) {
1401 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
1402 /*
1403 * Just inject an error in the first
1404 * prot block.
1405 */
1406 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1407 "9079 BLKGRD: Injecting guard error: "
1408 "write lba x%lx oldGuard x%x refTag x%x\n",
1409 (unsigned long)lba, src->guard_tag,
1410 src->ref_tag);
1411
1412 src->guard_tag = (uint16_t)new_guard;
1413 phba->lpfc_injerr_wgrd_cnt--;
1414 phba->lpfc_injerr_lba = LPFC_INJERR_LBA_OFF;
1415 rc = 1;
1416
1417 } else {
1418 blksize = lpfc_cmd_blksize(sc);
1419 /*
1420 * Jump past the first data block
1421 * and inject an error in the
1422 * prot data. The prot data is already
1423 * embedded after the regular data.
1424 */
1425 src = (struct scsi_dif_tuple *)
1426 (sg_virt(sgde) + blksize);
1427
1428 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1429 "9080 BLKGRD: Injecting guard error: "
1430 "write lba x%lx oldGuard x%x refTag x%x\n",
1431 (unsigned long)lba, src->guard_tag,
1432 src->ref_tag);
1433
1434 src->guard_tag = (uint16_t)new_guard;
1435 phba->lpfc_injerr_wgrd_cnt--;
1436 phba->lpfc_injerr_lba = LPFC_INJERR_LBA_OFF;
1437 rc = 1;
1438 }
1439 }
1440 return rc;
1441}
1442#endif
1443
James Smarte2a0a9d2008-12-04 22:40:02 -05001444/*
James Smart6c8eea52010-04-06 14:49:53 -04001445 * Given a scsi cmnd, determine the BlockGuard opcodes to be used with it
1446 * @sc: The SCSI command to examine
1447 * @txopt: (out) BlockGuard operation for transmitted data
1448 * @rxopt: (out) BlockGuard operation for received data
1449 *
1450 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1451 *
James Smarte2a0a9d2008-12-04 22:40:02 -05001452 */
1453static int
James Smart6c8eea52010-04-06 14:49:53 -04001454lpfc_sc_to_bg_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1455 uint8_t *txop, uint8_t *rxop)
James Smarte2a0a9d2008-12-04 22:40:02 -05001456{
1457 uint8_t guard_type = scsi_host_get_guard(sc->device->host);
James Smart6c8eea52010-04-06 14:49:53 -04001458 uint8_t ret = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05001459
1460 if (guard_type == SHOST_DIX_GUARD_IP) {
1461 switch (scsi_get_prot_op(sc)) {
1462 case SCSI_PROT_READ_INSERT:
1463 case SCSI_PROT_WRITE_STRIP:
James Smart6c8eea52010-04-06 14:49:53 -04001464 *txop = BG_OP_IN_CSUM_OUT_NODIF;
1465 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smarte2a0a9d2008-12-04 22:40:02 -05001466 break;
1467
1468 case SCSI_PROT_READ_STRIP:
1469 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001470 *txop = BG_OP_IN_NODIF_OUT_CRC;
1471 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smarte2a0a9d2008-12-04 22:40:02 -05001472 break;
1473
Martin K. Petersenc6af4042009-09-18 17:32:59 -04001474 case SCSI_PROT_READ_PASS:
1475 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001476 *txop = BG_OP_IN_CSUM_OUT_CRC;
1477 *rxop = BG_OP_IN_CRC_OUT_CSUM;
James Smarte2a0a9d2008-12-04 22:40:02 -05001478 break;
1479
James Smarte2a0a9d2008-12-04 22:40:02 -05001480 case SCSI_PROT_NORMAL:
1481 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001482 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001483 "9063 BLKGRD: Bad op/guard:%d/IP combination\n",
1484 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001485 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001486 break;
1487
1488 }
James Smart7c56b9f2011-07-22 18:36:25 -04001489 } else {
James Smarte2a0a9d2008-12-04 22:40:02 -05001490 switch (scsi_get_prot_op(sc)) {
1491 case SCSI_PROT_READ_STRIP:
1492 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001493 *txop = BG_OP_IN_NODIF_OUT_CRC;
1494 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smarte2a0a9d2008-12-04 22:40:02 -05001495 break;
1496
1497 case SCSI_PROT_READ_PASS:
1498 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001499 *txop = BG_OP_IN_CRC_OUT_CRC;
1500 *rxop = BG_OP_IN_CRC_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001501 break;
1502
James Smarte2a0a9d2008-12-04 22:40:02 -05001503 case SCSI_PROT_READ_INSERT:
1504 case SCSI_PROT_WRITE_STRIP:
James Smart7c56b9f2011-07-22 18:36:25 -04001505 *txop = BG_OP_IN_CRC_OUT_NODIF;
1506 *rxop = BG_OP_IN_NODIF_OUT_CRC;
1507 break;
1508
James Smarte2a0a9d2008-12-04 22:40:02 -05001509 case SCSI_PROT_NORMAL:
1510 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001511 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001512 "9075 BLKGRD: Bad op/guard:%d/CRC combination\n",
1513 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001514 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001515 break;
1516 }
James Smarte2a0a9d2008-12-04 22:40:02 -05001517 }
1518
James Smart6c8eea52010-04-06 14:49:53 -04001519 return ret;
James Smarte2a0a9d2008-12-04 22:40:02 -05001520}
1521
James Smarte2a0a9d2008-12-04 22:40:02 -05001522/*
1523 * This function sets up buffer list for protection groups of
1524 * type LPFC_PG_TYPE_NO_DIF
1525 *
1526 * This is usually used when the HBA is instructed to generate
1527 * DIFs and insert them into data stream (or strip DIF from
1528 * incoming data stream)
1529 *
1530 * The buffer list consists of just one protection group described
1531 * below:
1532 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001533 * start of prot group --> | PDE_5 |
1534 * +-------------------------+
1535 * | PDE_6 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001536 * +-------------------------+
1537 * | Data BDE |
1538 * +-------------------------+
1539 * |more Data BDE's ... (opt)|
1540 * +-------------------------+
1541 *
1542 * @sc: pointer to scsi command we're working on
1543 * @bpl: pointer to buffer list for protection groups
1544 * @datacnt: number of segments of data that have been dma mapped
1545 *
1546 * Note: Data s/g buffers have been dma mapped
1547 */
1548static int
1549lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1550 struct ulp_bde64 *bpl, int datasegcnt)
1551{
1552 struct scatterlist *sgde = NULL; /* s/g data entry */
James Smart6c8eea52010-04-06 14:49:53 -04001553 struct lpfc_pde5 *pde5 = NULL;
1554 struct lpfc_pde6 *pde6 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05001555 dma_addr_t physaddr;
James Smart6c8eea52010-04-06 14:49:53 -04001556 int i = 0, num_bde = 0, status;
James Smarte2a0a9d2008-12-04 22:40:02 -05001557 int datadir = sc->sc_data_direction;
James Smarte2a0a9d2008-12-04 22:40:02 -05001558 uint32_t reftag;
James Smart7c56b9f2011-07-22 18:36:25 -04001559 unsigned blksize;
James Smart6c8eea52010-04-06 14:49:53 -04001560 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05001561
James Smart6c8eea52010-04-06 14:49:53 -04001562 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1563 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05001564 goto out;
1565
James Smart6c8eea52010-04-06 14:49:53 -04001566 /* extract some info from the scsi command for pde*/
James Smarte2a0a9d2008-12-04 22:40:02 -05001567 blksize = lpfc_cmd_blksize(sc);
James Smart7c56b9f2011-07-22 18:36:25 -04001568 reftag = scsi_get_lba(sc) & 0xffffffff;
James Smarte2a0a9d2008-12-04 22:40:02 -05001569
James Smartf9bb2da2011-10-10 21:34:11 -04001570#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1571 /* reftag is the only error we can inject here */
1572 lpfc_bg_err_inject(phba, sc, &reftag, 0, 0);
1573#endif
1574
James Smart6c8eea52010-04-06 14:49:53 -04001575 /* setup PDE5 with what we have */
1576 pde5 = (struct lpfc_pde5 *) bpl;
1577 memset(pde5, 0, sizeof(struct lpfc_pde5));
1578 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05001579
James Smartbc739052010-08-04 16:11:18 -04001580 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04001581 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04001582 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04001583
James Smart6c8eea52010-04-06 14:49:53 -04001584 /* advance bpl and increment bde count */
1585 num_bde++;
1586 bpl++;
1587 pde6 = (struct lpfc_pde6 *) bpl;
1588
1589 /* setup PDE6 with the rest of the info */
1590 memset(pde6, 0, sizeof(struct lpfc_pde6));
1591 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
1592 bf_set(pde6_optx, pde6, txop);
1593 bf_set(pde6_oprx, pde6, rxop);
1594 if (datadir == DMA_FROM_DEVICE) {
1595 bf_set(pde6_ce, pde6, 1);
1596 bf_set(pde6_re, pde6, 1);
James Smart6c8eea52010-04-06 14:49:53 -04001597 }
1598 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04001599 bf_set(pde6_ae, pde6, 0);
1600 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04001601
James Smartbc739052010-08-04 16:11:18 -04001602 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04001603 pde6->word0 = cpu_to_le32(pde6->word0);
1604 pde6->word1 = cpu_to_le32(pde6->word1);
1605 pde6->word2 = cpu_to_le32(pde6->word2);
1606
James Smart6c8eea52010-04-06 14:49:53 -04001607 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05001608 num_bde++;
1609 bpl++;
1610
1611 /* assumption: caller has already run dma_map_sg on command data */
1612 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
1613 physaddr = sg_dma_address(sgde);
1614 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
1615 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1616 bpl->tus.f.bdeSize = sg_dma_len(sgde);
1617 if (datadir == DMA_TO_DEVICE)
1618 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1619 else
1620 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1621 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1622 bpl++;
1623 num_bde++;
1624 }
1625
1626out:
1627 return num_bde;
1628}
1629
1630/*
1631 * This function sets up buffer list for protection groups of
1632 * type LPFC_PG_TYPE_DIF_BUF
1633 *
1634 * This is usually used when DIFs are in their own buffers,
1635 * separate from the data. The HBA can then by instructed
1636 * to place the DIFs in the outgoing stream. For read operations,
1637 * The HBA could extract the DIFs and place it in DIF buffers.
1638 *
1639 * The buffer list for this type consists of one or more of the
1640 * protection groups described below:
1641 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001642 * start of first prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001643 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001644 * | PDE_6 |
1645 * +-------------------------+
1646 * | PDE_7 (Prot BDE) |
James Smarte2a0a9d2008-12-04 22:40:02 -05001647 * +-------------------------+
1648 * | Data BDE |
1649 * +-------------------------+
1650 * |more Data BDE's ... (opt)|
1651 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001652 * start of new prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001653 * +-------------------------+
1654 * | ... |
1655 * +-------------------------+
1656 *
1657 * @sc: pointer to scsi command we're working on
1658 * @bpl: pointer to buffer list for protection groups
1659 * @datacnt: number of segments of data that have been dma mapped
1660 * @protcnt: number of segment of protection data that have been dma mapped
1661 *
1662 * Note: It is assumed that both data and protection s/g buffers have been
1663 * mapped for DMA
1664 */
1665static int
1666lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1667 struct ulp_bde64 *bpl, int datacnt, int protcnt)
1668{
1669 struct scatterlist *sgde = NULL; /* s/g data entry */
1670 struct scatterlist *sgpe = NULL; /* s/g prot entry */
James Smart6c8eea52010-04-06 14:49:53 -04001671 struct lpfc_pde5 *pde5 = NULL;
1672 struct lpfc_pde6 *pde6 = NULL;
James Smart7f860592011-03-11 16:05:52 -05001673 struct lpfc_pde7 *pde7 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05001674 dma_addr_t dataphysaddr, protphysaddr;
1675 unsigned short curr_data = 0, curr_prot = 0;
James Smart7f860592011-03-11 16:05:52 -05001676 unsigned int split_offset;
1677 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
James Smarte2a0a9d2008-12-04 22:40:02 -05001678 unsigned int protgrp_blks, protgrp_bytes;
1679 unsigned int remainder, subtotal;
James Smart6c8eea52010-04-06 14:49:53 -04001680 int status;
James Smarte2a0a9d2008-12-04 22:40:02 -05001681 int datadir = sc->sc_data_direction;
1682 unsigned char pgdone = 0, alldone = 0;
1683 unsigned blksize;
1684 uint32_t reftag;
James Smart6c8eea52010-04-06 14:49:53 -04001685 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05001686 int num_bde = 0;
1687
1688 sgpe = scsi_prot_sglist(sc);
1689 sgde = scsi_sglist(sc);
1690
1691 if (!sgpe || !sgde) {
1692 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1693 "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
1694 sgpe, sgde);
1695 return 0;
1696 }
1697
James Smart6c8eea52010-04-06 14:49:53 -04001698 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1699 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05001700 goto out;
1701
James Smart6c8eea52010-04-06 14:49:53 -04001702 /* extract some info from the scsi command */
James Smarte2a0a9d2008-12-04 22:40:02 -05001703 blksize = lpfc_cmd_blksize(sc);
James Smart7c56b9f2011-07-22 18:36:25 -04001704 reftag = scsi_get_lba(sc) & 0xffffffff;
James Smarte2a0a9d2008-12-04 22:40:02 -05001705
James Smartf9bb2da2011-10-10 21:34:11 -04001706#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1707 /* reftag / guard tag are the only errors we can inject here */
1708 lpfc_bg_err_inject(phba, sc, &reftag, 0, 0xDEAD);
1709#endif
1710
James Smarte2a0a9d2008-12-04 22:40:02 -05001711 split_offset = 0;
1712 do {
James Smart6c8eea52010-04-06 14:49:53 -04001713 /* setup PDE5 with what we have */
1714 pde5 = (struct lpfc_pde5 *) bpl;
1715 memset(pde5, 0, sizeof(struct lpfc_pde5));
1716 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05001717
James Smartbc739052010-08-04 16:11:18 -04001718 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04001719 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04001720 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04001721
James Smart6c8eea52010-04-06 14:49:53 -04001722 /* advance bpl and increment bde count */
1723 num_bde++;
1724 bpl++;
1725 pde6 = (struct lpfc_pde6 *) bpl;
James Smarte2a0a9d2008-12-04 22:40:02 -05001726
James Smart6c8eea52010-04-06 14:49:53 -04001727 /* setup PDE6 with the rest of the info */
1728 memset(pde6, 0, sizeof(struct lpfc_pde6));
1729 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
1730 bf_set(pde6_optx, pde6, txop);
1731 bf_set(pde6_oprx, pde6, rxop);
1732 bf_set(pde6_ce, pde6, 1);
1733 bf_set(pde6_re, pde6, 1);
James Smart6c8eea52010-04-06 14:49:53 -04001734 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04001735 bf_set(pde6_ae, pde6, 0);
1736 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04001737
James Smartbc739052010-08-04 16:11:18 -04001738 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04001739 pde6->word0 = cpu_to_le32(pde6->word0);
1740 pde6->word1 = cpu_to_le32(pde6->word1);
1741 pde6->word2 = cpu_to_le32(pde6->word2);
1742
James Smart6c8eea52010-04-06 14:49:53 -04001743 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05001744 num_bde++;
1745 bpl++;
1746
1747 /* setup the first BDE that points to protection buffer */
James Smart7f860592011-03-11 16:05:52 -05001748 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
1749 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
James Smarte2a0a9d2008-12-04 22:40:02 -05001750
James Smarte2a0a9d2008-12-04 22:40:02 -05001751 /* must be integer multiple of the DIF block length */
1752 BUG_ON(protgroup_len % 8);
1753
James Smart7f860592011-03-11 16:05:52 -05001754 pde7 = (struct lpfc_pde7 *) bpl;
1755 memset(pde7, 0, sizeof(struct lpfc_pde7));
1756 bf_set(pde7_type, pde7, LPFC_PDE7_DESCRIPTOR);
1757
James Smart7c56b9f2011-07-22 18:36:25 -04001758 pde7->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
1759 pde7->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
James Smart7f860592011-03-11 16:05:52 -05001760
James Smarte2a0a9d2008-12-04 22:40:02 -05001761 protgrp_blks = protgroup_len / 8;
1762 protgrp_bytes = protgrp_blks * blksize;
1763
James Smart7f860592011-03-11 16:05:52 -05001764 /* check if this pde is crossing the 4K boundary; if so split */
1765 if ((pde7->addrLow & 0xfff) + protgroup_len > 0x1000) {
1766 protgroup_remainder = 0x1000 - (pde7->addrLow & 0xfff);
1767 protgroup_offset += protgroup_remainder;
1768 protgrp_blks = protgroup_remainder / 8;
James Smart7c56b9f2011-07-22 18:36:25 -04001769 protgrp_bytes = protgrp_blks * blksize;
James Smart7f860592011-03-11 16:05:52 -05001770 } else {
1771 protgroup_offset = 0;
1772 curr_prot++;
1773 }
James Smarte2a0a9d2008-12-04 22:40:02 -05001774
James Smarte2a0a9d2008-12-04 22:40:02 -05001775 num_bde++;
1776
1777 /* setup BDE's for data blocks associated with DIF data */
1778 pgdone = 0;
1779 subtotal = 0; /* total bytes processed for current prot grp */
1780 while (!pgdone) {
1781 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -04001782 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1783 "9065 BLKGRD:%s Invalid data segment\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001784 __func__);
1785 return 0;
1786 }
1787 bpl++;
1788 dataphysaddr = sg_dma_address(sgde) + split_offset;
1789 bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
1790 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
1791
1792 remainder = sg_dma_len(sgde) - split_offset;
1793
1794 if ((subtotal + remainder) <= protgrp_bytes) {
1795 /* we can use this whole buffer */
1796 bpl->tus.f.bdeSize = remainder;
1797 split_offset = 0;
1798
1799 if ((subtotal + remainder) == protgrp_bytes)
1800 pgdone = 1;
1801 } else {
1802 /* must split this buffer with next prot grp */
1803 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
1804 split_offset += bpl->tus.f.bdeSize;
1805 }
1806
1807 subtotal += bpl->tus.f.bdeSize;
1808
1809 if (datadir == DMA_TO_DEVICE)
1810 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1811 else
1812 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1813 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1814
1815 num_bde++;
1816 curr_data++;
1817
1818 if (split_offset)
1819 break;
1820
1821 /* Move to the next s/g segment if possible */
1822 sgde = sg_next(sgde);
James Smart6c8eea52010-04-06 14:49:53 -04001823
James Smarte2a0a9d2008-12-04 22:40:02 -05001824 }
1825
James Smart7f860592011-03-11 16:05:52 -05001826 if (protgroup_offset) {
1827 /* update the reference tag */
1828 reftag += protgrp_blks;
1829 bpl++;
1830 continue;
1831 }
1832
James Smarte2a0a9d2008-12-04 22:40:02 -05001833 /* are we done ? */
1834 if (curr_prot == protcnt) {
1835 alldone = 1;
1836 } else if (curr_prot < protcnt) {
1837 /* advance to next prot buffer */
1838 sgpe = sg_next(sgpe);
1839 bpl++;
1840
1841 /* update the reference tag */
1842 reftag += protgrp_blks;
1843 } else {
1844 /* if we're here, we have a bug */
James Smart6a9c52c2009-10-02 15:16:51 -04001845 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1846 "9054 BLKGRD: bug in %s\n", __func__);
James Smarte2a0a9d2008-12-04 22:40:02 -05001847 }
1848
1849 } while (!alldone);
James Smarte2a0a9d2008-12-04 22:40:02 -05001850out:
1851
James Smarte2a0a9d2008-12-04 22:40:02 -05001852 return num_bde;
1853}
James Smart7f860592011-03-11 16:05:52 -05001854
James Smarte2a0a9d2008-12-04 22:40:02 -05001855/*
1856 * Given a SCSI command that supports DIF, determine composition of protection
1857 * groups involved in setting up buffer lists
1858 *
1859 * Returns:
1860 * for DIF (for both read and write)
1861 * */
1862static int
1863lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
1864{
1865 int ret = LPFC_PG_TYPE_INVALID;
1866 unsigned char op = scsi_get_prot_op(sc);
1867
1868 switch (op) {
1869 case SCSI_PROT_READ_STRIP:
1870 case SCSI_PROT_WRITE_INSERT:
1871 ret = LPFC_PG_TYPE_NO_DIF;
1872 break;
1873 case SCSI_PROT_READ_INSERT:
1874 case SCSI_PROT_WRITE_STRIP:
1875 case SCSI_PROT_READ_PASS:
1876 case SCSI_PROT_WRITE_PASS:
James Smarte2a0a9d2008-12-04 22:40:02 -05001877 ret = LPFC_PG_TYPE_DIF_BUF;
1878 break;
1879 default:
1880 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1881 "9021 Unsupported protection op:%d\n", op);
1882 break;
1883 }
1884
1885 return ret;
1886}
1887
1888/*
1889 * This is the protection/DIF aware version of
1890 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
1891 * two functions eventually, but for now, it's here
1892 */
1893static int
1894lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba,
1895 struct lpfc_scsi_buf *lpfc_cmd)
1896{
1897 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1898 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1899 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
1900 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
1901 uint32_t num_bde = 0;
1902 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
1903 int prot_group_type = 0;
1904 int diflen, fcpdl;
1905 unsigned blksize;
1906
1907 /*
1908 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
1909 * fcp_rsp regions to the first data bde entry
1910 */
1911 bpl += 2;
1912 if (scsi_sg_count(scsi_cmnd)) {
1913 /*
1914 * The driver stores the segment count returned from pci_map_sg
1915 * because this a count of dma-mappings used to map the use_sg
1916 * pages. They are not guaranteed to be the same for those
1917 * architectures that implement an IOMMU.
1918 */
1919 datasegcnt = dma_map_sg(&phba->pcidev->dev,
1920 scsi_sglist(scsi_cmnd),
1921 scsi_sg_count(scsi_cmnd), datadir);
1922 if (unlikely(!datasegcnt))
1923 return 1;
1924
1925 lpfc_cmd->seg_cnt = datasegcnt;
1926 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001927 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1928 "9067 BLKGRD: %s: Too many sg segments"
1929 " from dma_map_sg. Config %d, seg_cnt"
1930 " %d\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001931 __func__, phba->cfg_sg_seg_cnt,
1932 lpfc_cmd->seg_cnt);
1933 scsi_dma_unmap(scsi_cmnd);
1934 return 1;
1935 }
1936
1937 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
1938
1939 switch (prot_group_type) {
1940 case LPFC_PG_TYPE_NO_DIF:
1941 num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
1942 datasegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05001943 /* we should have 2 or more entries in buffer list */
James Smarte2a0a9d2008-12-04 22:40:02 -05001944 if (num_bde < 2)
1945 goto err;
1946 break;
1947 case LPFC_PG_TYPE_DIF_BUF:{
1948 /*
1949 * This type indicates that protection buffers are
1950 * passed to the driver, so that needs to be prepared
1951 * for DMA
1952 */
1953 protsegcnt = dma_map_sg(&phba->pcidev->dev,
1954 scsi_prot_sglist(scsi_cmnd),
1955 scsi_prot_sg_count(scsi_cmnd), datadir);
1956 if (unlikely(!protsegcnt)) {
1957 scsi_dma_unmap(scsi_cmnd);
1958 return 1;
1959 }
1960
1961 lpfc_cmd->prot_seg_cnt = protsegcnt;
1962 if (lpfc_cmd->prot_seg_cnt
1963 > phba->cfg_prot_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001964 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1965 "9068 BLKGRD: %s: Too many prot sg "
1966 "segments from dma_map_sg. Config %d,"
James Smarte2a0a9d2008-12-04 22:40:02 -05001967 "prot_seg_cnt %d\n", __func__,
1968 phba->cfg_prot_sg_seg_cnt,
1969 lpfc_cmd->prot_seg_cnt);
1970 dma_unmap_sg(&phba->pcidev->dev,
1971 scsi_prot_sglist(scsi_cmnd),
1972 scsi_prot_sg_count(scsi_cmnd),
1973 datadir);
1974 scsi_dma_unmap(scsi_cmnd);
1975 return 1;
1976 }
1977
1978 num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
1979 datasegcnt, protsegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05001980 /* we should have 3 or more entries in buffer list */
James Smarte2a0a9d2008-12-04 22:40:02 -05001981 if (num_bde < 3)
1982 goto err;
1983 break;
1984 }
1985 case LPFC_PG_TYPE_INVALID:
1986 default:
1987 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1988 "9022 Unexpected protection group %i\n",
1989 prot_group_type);
1990 return 1;
1991 }
1992 }
1993
1994 /*
1995 * Finish initializing those IOCB fields that are dependent on the
1996 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
1997 * reinitialized since all iocb memory resources are used many times
1998 * for transmit, receive, and continuation bpl's.
1999 */
2000 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
2001 iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
2002 iocb_cmd->ulpBdeCount = 1;
2003 iocb_cmd->ulpLe = 1;
2004
2005 fcpdl = scsi_bufflen(scsi_cmnd);
2006
2007 if (scsi_get_prot_type(scsi_cmnd) == SCSI_PROT_DIF_TYPE1) {
2008 /*
2009 * We are in DIF Type 1 mode
2010 * Every data block has a 8 byte DIF (trailer)
2011 * attached to it. Must ajust FCP data length
2012 */
2013 blksize = lpfc_cmd_blksize(scsi_cmnd);
2014 diflen = (fcpdl / blksize) * 8;
2015 fcpdl += diflen;
2016 }
2017 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
2018
2019 /*
2020 * Due to difference in data length between DIF/non-DIF paths,
2021 * we need to set word 4 of IOCB here
2022 */
2023 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
2024
2025 return 0;
2026err:
2027 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2028 "9023 Could not setup all needed BDE's"
2029 "prot_group_type=%d, num_bde=%d\n",
2030 prot_group_type, num_bde);
2031 return 1;
2032}
2033
2034/*
2035 * This function checks for BlockGuard errors detected by
2036 * the HBA. In case of errors, the ASC/ASCQ fields in the
2037 * sense buffer will be set accordingly, paired with
2038 * ILLEGAL_REQUEST to signal to the kernel that the HBA
2039 * detected corruption.
2040 *
2041 * Returns:
2042 * 0 - No error found
2043 * 1 - BlockGuard error found
2044 * -1 - Internal error (bad profile, ...etc)
2045 */
2046static int
2047lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
2048 struct lpfc_iocbq *pIocbOut)
2049{
2050 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
2051 struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
2052 int ret = 0;
2053 uint32_t bghm = bgf->bghm;
2054 uint32_t bgstat = bgf->bgstat;
2055 uint64_t failing_sector = 0;
2056
James Smart6a9c52c2009-10-02 15:16:51 -04002057 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9069 BLKGRD: BG ERROR in cmd"
2058 " 0x%x lba 0x%llx blk cnt 0x%x "
James Smarte2a0a9d2008-12-04 22:40:02 -05002059 "bgstat=0x%x bghm=0x%x\n",
James Smart87b5c322008-12-16 10:34:09 -05002060 cmd->cmnd[0], (unsigned long long)scsi_get_lba(cmd),
Tejun Heo83096eb2009-05-07 22:24:39 +09002061 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05002062
2063 spin_lock(&_dump_buf_lock);
2064 if (!_dump_buf_done) {
James Smart6a9c52c2009-10-02 15:16:51 -04002065 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9070 BLKGRD: Saving"
2066 " Data for %u blocks to debugfs\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002067 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
James Smart6a9c52c2009-10-02 15:16:51 -04002068 lpfc_debug_save_data(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002069
2070 /* If we have a prot sgl, save the DIF buffer */
2071 if (lpfc_prot_group_type(phba, cmd) ==
2072 LPFC_PG_TYPE_DIF_BUF) {
James Smart6a9c52c2009-10-02 15:16:51 -04002073 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9071 BLKGRD: "
2074 "Saving DIF for %u blocks to debugfs\n",
2075 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
2076 lpfc_debug_save_dif(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002077 }
2078
2079 _dump_buf_done = 1;
2080 }
2081 spin_unlock(&_dump_buf_lock);
2082
2083 if (lpfc_bgs_get_invalid_prof(bgstat)) {
2084 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart6a9c52c2009-10-02 15:16:51 -04002085 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9072 BLKGRD: Invalid"
2086 " BlockGuard profile. bgstat:0x%x\n",
2087 bgstat);
James Smarte2a0a9d2008-12-04 22:40:02 -05002088 ret = (-1);
2089 goto out;
2090 }
2091
2092 if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
2093 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart6a9c52c2009-10-02 15:16:51 -04002094 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9073 BLKGRD: "
2095 "Invalid BlockGuard DIF Block. bgstat:0x%x\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002096 bgstat);
2097 ret = (-1);
2098 goto out;
2099 }
2100
2101 if (lpfc_bgs_get_guard_err(bgstat)) {
2102 ret = 1;
2103
2104 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2105 0x10, 0x1);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05002106 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05002107 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
2108 phba->bg_guard_err_cnt++;
James Smart6a9c52c2009-10-02 15:16:51 -04002109 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2110 "9055 BLKGRD: guard_tag error\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05002111 }
2112
2113 if (lpfc_bgs_get_reftag_err(bgstat)) {
2114 ret = 1;
2115
2116 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2117 0x10, 0x3);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05002118 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05002119 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
2120
2121 phba->bg_reftag_err_cnt++;
James Smart6a9c52c2009-10-02 15:16:51 -04002122 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2123 "9056 BLKGRD: ref_tag error\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05002124 }
2125
2126 if (lpfc_bgs_get_apptag_err(bgstat)) {
2127 ret = 1;
2128
2129 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2130 0x10, 0x2);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05002131 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05002132 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
2133
2134 phba->bg_apptag_err_cnt++;
James Smart6a9c52c2009-10-02 15:16:51 -04002135 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2136 "9061 BLKGRD: app_tag error\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05002137 }
2138
2139 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
2140 /*
2141 * setup sense data descriptor 0 per SPC-4 as an information
James Smart7c56b9f2011-07-22 18:36:25 -04002142 * field, and put the failing LBA in it.
2143 * This code assumes there was also a guard/app/ref tag error
2144 * indication.
James Smarte2a0a9d2008-12-04 22:40:02 -05002145 */
James Smart7c56b9f2011-07-22 18:36:25 -04002146 cmd->sense_buffer[7] = 0xc; /* Additional sense length */
2147 cmd->sense_buffer[8] = 0; /* Information descriptor type */
2148 cmd->sense_buffer[9] = 0xa; /* Additional descriptor length */
2149 cmd->sense_buffer[10] = 0x80; /* Validity bit */
David Howells2344b5b2009-04-14 17:08:34 +01002150 bghm /= cmd->device->sector_size;
James Smarte2a0a9d2008-12-04 22:40:02 -05002151
2152 failing_sector = scsi_get_lba(cmd);
2153 failing_sector += bghm;
2154
James Smart7c56b9f2011-07-22 18:36:25 -04002155 /* Descriptor Information */
2156 put_unaligned_be64(failing_sector, &cmd->sense_buffer[12]);
James Smarte2a0a9d2008-12-04 22:40:02 -05002157 }
2158
2159 if (!ret) {
2160 /* No error was reported - problem in FW? */
2161 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart6a9c52c2009-10-02 15:16:51 -04002162 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2163 "9057 BLKGRD: no errors reported!\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05002164 }
2165
2166out:
2167 return ret;
2168}
2169
James Smartea2151b2008-09-07 11:52:10 -04002170/**
James Smartda0436e2009-05-22 14:51:39 -04002171 * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
2172 * @phba: The Hba for which this call is being executed.
2173 * @lpfc_cmd: The scsi buffer which is going to be mapped.
2174 *
2175 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
2176 * field of @lpfc_cmd for device with SLI-4 interface spec.
2177 *
2178 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04002179 * 1 - Error
2180 * 0 - Success
James Smartda0436e2009-05-22 14:51:39 -04002181 **/
2182static int
2183lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
2184{
2185 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2186 struct scatterlist *sgel = NULL;
2187 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2188 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
James Smartfedd3b72011-02-16 12:39:24 -05002189 struct sli4_sge *first_data_sgl;
James Smartda0436e2009-05-22 14:51:39 -04002190 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2191 dma_addr_t physaddr;
2192 uint32_t num_bde = 0;
2193 uint32_t dma_len;
2194 uint32_t dma_offset = 0;
2195 int nseg;
James Smartfedd3b72011-02-16 12:39:24 -05002196 struct ulp_bde64 *bde;
James Smartda0436e2009-05-22 14:51:39 -04002197
2198 /*
2199 * There are three possibilities here - use scatter-gather segment, use
2200 * the single mapping, or neither. Start the lpfc command prep by
2201 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
2202 * data bde entry.
2203 */
2204 if (scsi_sg_count(scsi_cmnd)) {
2205 /*
2206 * The driver stores the segment count returned from pci_map_sg
2207 * because this a count of dma-mappings used to map the use_sg
2208 * pages. They are not guaranteed to be the same for those
2209 * architectures that implement an IOMMU.
2210 */
2211
2212 nseg = scsi_dma_map(scsi_cmnd);
2213 if (unlikely(!nseg))
2214 return 1;
2215 sgl += 1;
2216 /* clear the last flag in the fcp_rsp map entry */
2217 sgl->word2 = le32_to_cpu(sgl->word2);
2218 bf_set(lpfc_sli4_sge_last, sgl, 0);
2219 sgl->word2 = cpu_to_le32(sgl->word2);
2220 sgl += 1;
James Smartfedd3b72011-02-16 12:39:24 -05002221 first_data_sgl = sgl;
James Smartda0436e2009-05-22 14:51:39 -04002222 lpfc_cmd->seg_cnt = nseg;
2223 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04002224 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
2225 " %s: Too many sg segments from "
2226 "dma_map_sg. Config %d, seg_cnt %d\n",
2227 __func__, phba->cfg_sg_seg_cnt,
James Smartda0436e2009-05-22 14:51:39 -04002228 lpfc_cmd->seg_cnt);
2229 scsi_dma_unmap(scsi_cmnd);
2230 return 1;
2231 }
2232
2233 /*
2234 * The driver established a maximum scatter-gather segment count
2235 * during probe that limits the number of sg elements in any
2236 * single scsi command. Just run through the seg_cnt and format
2237 * the sge's.
2238 * When using SLI-3 the driver will try to fit all the BDEs into
2239 * the IOCB. If it can't then the BDEs get added to a BPL as it
2240 * does for SLI-2 mode.
2241 */
2242 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
2243 physaddr = sg_dma_address(sgel);
2244 dma_len = sg_dma_len(sgel);
James Smartda0436e2009-05-22 14:51:39 -04002245 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
2246 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
James Smart05580562011-05-24 11:40:48 -04002247 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -04002248 if ((num_bde + 1) == nseg)
2249 bf_set(lpfc_sli4_sge_last, sgl, 1);
2250 else
2251 bf_set(lpfc_sli4_sge_last, sgl, 0);
2252 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
James Smartf9bb2da2011-10-10 21:34:11 -04002253 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
James Smartda0436e2009-05-22 14:51:39 -04002254 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05002255 sgl->sge_len = cpu_to_le32(dma_len);
James Smartda0436e2009-05-22 14:51:39 -04002256 dma_offset += dma_len;
2257 sgl++;
2258 }
James Smartfedd3b72011-02-16 12:39:24 -05002259 /* setup the performance hint (first data BDE) if enabled */
2260 if (phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) {
2261 bde = (struct ulp_bde64 *)
2262 &(iocb_cmd->unsli3.sli3Words[5]);
2263 bde->addrLow = first_data_sgl->addr_lo;
2264 bde->addrHigh = first_data_sgl->addr_hi;
2265 bde->tus.f.bdeSize =
2266 le32_to_cpu(first_data_sgl->sge_len);
2267 bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2268 bde->tus.w = cpu_to_le32(bde->tus.w);
2269 }
James Smartda0436e2009-05-22 14:51:39 -04002270 } else {
2271 sgl += 1;
2272 /* clear the last flag in the fcp_rsp map entry */
2273 sgl->word2 = le32_to_cpu(sgl->word2);
2274 bf_set(lpfc_sli4_sge_last, sgl, 1);
2275 sgl->word2 = cpu_to_le32(sgl->word2);
2276 }
2277
2278 /*
2279 * Finish initializing those IOCB fields that are dependent on the
2280 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
2281 * explicitly reinitialized.
2282 * all iocb memory resources are reused.
2283 */
2284 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
2285
2286 /*
2287 * Due to difference in data length between DIF/non-DIF paths,
2288 * we need to set word 4 of IOCB here
2289 */
2290 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
2291 return 0;
2292}
2293
2294/**
James Smart3772a992009-05-22 14:50:54 -04002295 * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
2296 * @phba: The Hba for which this call is being executed.
2297 * @lpfc_cmd: The scsi buffer which is going to be mapped.
2298 *
2299 * This routine wraps the actual DMA mapping function pointer from the
2300 * lpfc_hba struct.
2301 *
2302 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04002303 * 1 - Error
2304 * 0 - Success
James Smart3772a992009-05-22 14:50:54 -04002305 **/
2306static inline int
2307lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
2308{
2309 return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
2310}
2311
2312/**
James Smart3621a712009-04-06 18:47:14 -04002313 * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
James Smartea2151b2008-09-07 11:52:10 -04002314 * @phba: Pointer to hba context object.
2315 * @vport: Pointer to vport object.
2316 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
2317 * @rsp_iocb: Pointer to response iocb object which reported error.
2318 *
2319 * This function posts an event when there is a SCSI command reporting
2320 * error from the scsi device.
2321 **/
2322static void
2323lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
2324 struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
2325 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
2326 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
2327 uint32_t resp_info = fcprsp->rspStatus2;
2328 uint32_t scsi_status = fcprsp->rspStatus3;
2329 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
2330 struct lpfc_fast_path_event *fast_path_evt = NULL;
2331 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
2332 unsigned long flags;
2333
James Smart5989b8d2010-10-22 11:06:56 -04002334 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
2335 return;
2336
James Smartea2151b2008-09-07 11:52:10 -04002337 /* If there is queuefull or busy condition send a scsi event */
2338 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
2339 (cmnd->result == SAM_STAT_BUSY)) {
2340 fast_path_evt = lpfc_alloc_fast_evt(phba);
2341 if (!fast_path_evt)
2342 return;
2343 fast_path_evt->un.scsi_evt.event_type =
2344 FC_REG_SCSI_EVENT;
2345 fast_path_evt->un.scsi_evt.subcategory =
2346 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
2347 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
2348 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
2349 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
2350 &pnode->nlp_portname, sizeof(struct lpfc_name));
2351 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
2352 &pnode->nlp_nodename, sizeof(struct lpfc_name));
2353 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
2354 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
2355 fast_path_evt = lpfc_alloc_fast_evt(phba);
2356 if (!fast_path_evt)
2357 return;
2358 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
2359 FC_REG_SCSI_EVENT;
2360 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
2361 LPFC_EVENT_CHECK_COND;
2362 fast_path_evt->un.check_cond_evt.scsi_event.lun =
2363 cmnd->device->lun;
2364 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
2365 &pnode->nlp_portname, sizeof(struct lpfc_name));
2366 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
2367 &pnode->nlp_nodename, sizeof(struct lpfc_name));
2368 fast_path_evt->un.check_cond_evt.sense_key =
2369 cmnd->sense_buffer[2] & 0xf;
2370 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
2371 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
2372 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
2373 fcpi_parm &&
2374 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
2375 ((scsi_status == SAM_STAT_GOOD) &&
2376 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
2377 /*
2378 * If status is good or resid does not match with fcp_param and
2379 * there is valid fcpi_parm, then there is a read_check error
2380 */
2381 fast_path_evt = lpfc_alloc_fast_evt(phba);
2382 if (!fast_path_evt)
2383 return;
2384 fast_path_evt->un.read_check_error.header.event_type =
2385 FC_REG_FABRIC_EVENT;
2386 fast_path_evt->un.read_check_error.header.subcategory =
2387 LPFC_EVENT_FCPRDCHKERR;
2388 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
2389 &pnode->nlp_portname, sizeof(struct lpfc_name));
2390 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
2391 &pnode->nlp_nodename, sizeof(struct lpfc_name));
2392 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
2393 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
2394 fast_path_evt->un.read_check_error.fcpiparam =
2395 fcpi_parm;
2396 } else
2397 return;
2398
2399 fast_path_evt->vport = vport;
2400 spin_lock_irqsave(&phba->hbalock, flags);
2401 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
2402 spin_unlock_irqrestore(&phba->hbalock, flags);
2403 lpfc_worker_wake_up(phba);
2404 return;
2405}
James Smart9bad7672008-12-04 22:39:02 -05002406
2407/**
James Smartf1126682009-06-10 17:22:44 -04002408 * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
James Smart3772a992009-05-22 14:50:54 -04002409 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -05002410 * @psb: The scsi buffer which is going to be un-mapped.
2411 *
2412 * This routine does DMA un-mapping of scatter gather list of scsi command
James Smart3772a992009-05-22 14:50:54 -04002413 * field of @lpfc_cmd for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05002414 **/
dea31012005-04-17 16:05:31 -05002415static void
James Smartf1126682009-06-10 17:22:44 -04002416lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James Smartbcf4dbf2006-07-06 15:50:08 -04002417{
2418 /*
2419 * There are only two special cases to consider. (1) the scsi command
2420 * requested scatter-gather usage or (2) the scsi command allocated
2421 * a request buffer, but did not request use_sg. There is a third
2422 * case, but it does not require resource deallocation.
2423 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002424 if (psb->seg_cnt > 0)
2425 scsi_dma_unmap(psb->pCmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002426 if (psb->prot_seg_cnt > 0)
2427 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
2428 scsi_prot_sg_count(psb->pCmd),
2429 psb->pCmd->sc_data_direction);
James Smartbcf4dbf2006-07-06 15:50:08 -04002430}
2431
James Smart9bad7672008-12-04 22:39:02 -05002432/**
James Smart3621a712009-04-06 18:47:14 -04002433 * lpfc_handler_fcp_err - FCP response handler
James Smart9bad7672008-12-04 22:39:02 -05002434 * @vport: The virtual port for which this call is being executed.
2435 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
2436 * @rsp_iocb: The response IOCB which contains FCP error.
2437 *
2438 * This routine is called to process response IOCB with status field
2439 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
2440 * based upon SCSI and FCP error.
2441 **/
James Smartbcf4dbf2006-07-06 15:50:08 -04002442static void
James Smart2e0fef82007-06-17 19:56:36 -05002443lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
2444 struct lpfc_iocbq *rsp_iocb)
dea31012005-04-17 16:05:31 -05002445{
2446 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
2447 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
2448 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
James Smart7054a602007-04-25 09:52:34 -04002449 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
dea31012005-04-17 16:05:31 -05002450 uint32_t resp_info = fcprsp->rspStatus2;
2451 uint32_t scsi_status = fcprsp->rspStatus3;
James Smartc7743952006-12-02 13:34:42 -05002452 uint32_t *lp;
dea31012005-04-17 16:05:31 -05002453 uint32_t host_status = DID_OK;
2454 uint32_t rsplen = 0;
James Smartc7743952006-12-02 13:34:42 -05002455 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea31012005-04-17 16:05:31 -05002456
James Smartea2151b2008-09-07 11:52:10 -04002457
dea31012005-04-17 16:05:31 -05002458 /*
2459 * If this is a task management command, there is no
2460 * scsi packet associated with this lpfc_cmd. The driver
2461 * consumes it.
2462 */
2463 if (fcpcmd->fcpCntl2) {
2464 scsi_status = 0;
2465 goto out;
2466 }
2467
James Smart6a9c52c2009-10-02 15:16:51 -04002468 if (resp_info & RSP_LEN_VALID) {
2469 rsplen = be32_to_cpu(fcprsp->rspRspLen);
James Smarte40a02c2010-02-26 14:13:54 -05002470 if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
James Smart6a9c52c2009-10-02 15:16:51 -04002471 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
2472 "2719 Invalid response length: "
2473 "tgt x%x lun x%x cmnd x%x rsplen x%x\n",
2474 cmnd->device->id,
2475 cmnd->device->lun, cmnd->cmnd[0],
2476 rsplen);
2477 host_status = DID_ERROR;
2478 goto out;
2479 }
James Smarte40a02c2010-02-26 14:13:54 -05002480 if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
2481 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
2482 "2757 Protocol failure detected during "
2483 "processing of FCP I/O op: "
2484 "tgt x%x lun x%x cmnd x%x rspInfo3 x%x\n",
2485 cmnd->device->id,
2486 cmnd->device->lun, cmnd->cmnd[0],
2487 fcprsp->rspInfo3);
2488 host_status = DID_ERROR;
2489 goto out;
2490 }
James Smart6a9c52c2009-10-02 15:16:51 -04002491 }
2492
James Smartc7743952006-12-02 13:34:42 -05002493 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
2494 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
2495 if (snslen > SCSI_SENSE_BUFFERSIZE)
2496 snslen = SCSI_SENSE_BUFFERSIZE;
2497
2498 if (resp_info & RSP_LEN_VALID)
2499 rsplen = be32_to_cpu(fcprsp->rspRspLen);
2500 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
2501 }
2502 lp = (uint32_t *)cmnd->sense_buffer;
2503
James Smart73d91e52011-10-10 21:32:10 -04002504 if (!scsi_status && (resp_info & RESID_UNDER) &&
2505 vport->cfg_log_verbose & LOG_FCP_UNDER)
2506 logit = LOG_FCP_UNDER;
James Smartc7743952006-12-02 13:34:42 -05002507
James Smarte8b62012007-08-02 11:10:09 -04002508 lpfc_printf_vlog(vport, KERN_WARNING, logit,
James Smarte2a0a9d2008-12-04 22:40:02 -05002509 "9024 FCP command x%x failed: x%x SNS x%x x%x "
James Smarte8b62012007-08-02 11:10:09 -04002510 "Data: x%x x%x x%x x%x x%x\n",
2511 cmnd->cmnd[0], scsi_status,
2512 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
2513 be32_to_cpu(fcprsp->rspResId),
2514 be32_to_cpu(fcprsp->rspSnsLen),
2515 be32_to_cpu(fcprsp->rspRspLen),
2516 fcprsp->rspInfo3);
dea31012005-04-17 16:05:31 -05002517
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002518 scsi_set_resid(cmnd, 0);
dea31012005-04-17 16:05:31 -05002519 if (resp_info & RESID_UNDER) {
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002520 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea31012005-04-17 16:05:31 -05002521
James Smart73d91e52011-10-10 21:32:10 -04002522 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_UNDER,
James Smarte2a0a9d2008-12-04 22:40:02 -05002523 "9025 FCP Read Underrun, expected %d, "
James Smarte8b62012007-08-02 11:10:09 -04002524 "residual %d Data: x%x x%x x%x\n",
2525 be32_to_cpu(fcpcmd->fcpDl),
2526 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
2527 cmnd->underflow);
dea31012005-04-17 16:05:31 -05002528
2529 /*
James Smart7054a602007-04-25 09:52:34 -04002530 * If there is an under run check if under run reported by
2531 * storage array is same as the under run reported by HBA.
2532 * If this is not same, there is a dropped frame.
2533 */
2534 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
2535 fcpi_parm &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002536 (scsi_get_resid(cmnd) != fcpi_parm)) {
James Smarte8b62012007-08-02 11:10:09 -04002537 lpfc_printf_vlog(vport, KERN_WARNING,
2538 LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05002539 "9026 FCP Read Check Error "
James Smarte8b62012007-08-02 11:10:09 -04002540 "and Underrun Data: x%x x%x x%x x%x\n",
2541 be32_to_cpu(fcpcmd->fcpDl),
2542 scsi_get_resid(cmnd), fcpi_parm,
2543 cmnd->cmnd[0]);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002544 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
James Smart7054a602007-04-25 09:52:34 -04002545 host_status = DID_ERROR;
2546 }
2547 /*
dea31012005-04-17 16:05:31 -05002548 * The cmnd->underflow is the minimum number of bytes that must
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002549 * be transferred for this command. Provided a sense condition
dea31012005-04-17 16:05:31 -05002550 * is not present, make sure the actual amount transferred is at
2551 * least the underflow value or fail.
2552 */
2553 if (!(resp_info & SNS_LEN_VALID) &&
2554 (scsi_status == SAM_STAT_GOOD) &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002555 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
2556 < cmnd->underflow)) {
James Smarte8b62012007-08-02 11:10:09 -04002557 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05002558 "9027 FCP command x%x residual "
James Smarte8b62012007-08-02 11:10:09 -04002559 "underrun converted to error "
2560 "Data: x%x x%x x%x\n",
James Smart66dbfbe2007-08-05 06:08:38 -04002561 cmnd->cmnd[0], scsi_bufflen(cmnd),
James Smarte8b62012007-08-02 11:10:09 -04002562 scsi_get_resid(cmnd), cmnd->underflow);
dea31012005-04-17 16:05:31 -05002563 host_status = DID_ERROR;
2564 }
2565 } else if (resp_info & RESID_OVER) {
James Smarte8b62012007-08-02 11:10:09 -04002566 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05002567 "9028 FCP command x%x residual overrun error. "
James Smarte4e74272009-07-19 10:01:38 -04002568 "Data: x%x x%x\n", cmnd->cmnd[0],
James Smarte8b62012007-08-02 11:10:09 -04002569 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea31012005-04-17 16:05:31 -05002570 host_status = DID_ERROR;
2571
2572 /*
2573 * Check SLI validation that all the transfer was actually done
2574 * (fcpi_parm should be zero). Apply check only to reads.
2575 */
James Smarteee88772010-09-29 11:19:08 -04002576 } else if (fcpi_parm && (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
James Smarte8b62012007-08-02 11:10:09 -04002577 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05002578 "9029 FCP Read Check Error Data: "
James Smarteee88772010-09-29 11:19:08 -04002579 "x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002580 be32_to_cpu(fcpcmd->fcpDl),
2581 be32_to_cpu(fcprsp->rspResId),
James Smarteee88772010-09-29 11:19:08 -04002582 fcpi_parm, cmnd->cmnd[0], scsi_status);
2583 switch (scsi_status) {
2584 case SAM_STAT_GOOD:
2585 case SAM_STAT_CHECK_CONDITION:
2586 /* Fabric dropped a data frame. Fail any successful
2587 * command in which we detected dropped frames.
2588 * A status of good or some check conditions could
2589 * be considered a successful command.
2590 */
2591 host_status = DID_ERROR;
2592 break;
2593 }
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002594 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea31012005-04-17 16:05:31 -05002595 }
2596
2597 out:
2598 cmnd->result = ScsiResult(host_status, scsi_status);
James Smartea2151b2008-09-07 11:52:10 -04002599 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
dea31012005-04-17 16:05:31 -05002600}
2601
James Smart9bad7672008-12-04 22:39:02 -05002602/**
James Smart3621a712009-04-06 18:47:14 -04002603 * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
James Smart9bad7672008-12-04 22:39:02 -05002604 * @phba: The Hba for which this call is being executed.
2605 * @pIocbIn: The command IOCBQ for the scsi cmnd.
James Smart3772a992009-05-22 14:50:54 -04002606 * @pIocbOut: The response IOCBQ for the scsi cmnd.
James Smart9bad7672008-12-04 22:39:02 -05002607 *
2608 * This routine assigns scsi command result by looking into response IOCB
2609 * status field appropriately. This routine handles QUEUE FULL condition as
2610 * well by ramping down device queue depth.
2611 **/
dea31012005-04-17 16:05:31 -05002612static void
2613lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
2614 struct lpfc_iocbq *pIocbOut)
2615{
2616 struct lpfc_scsi_buf *lpfc_cmd =
2617 (struct lpfc_scsi_buf *) pIocbIn->context1;
James Smart2e0fef82007-06-17 19:56:36 -05002618 struct lpfc_vport *vport = pIocbIn->vport;
dea31012005-04-17 16:05:31 -05002619 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
2620 struct lpfc_nodelist *pnode = rdata->pnode;
James Smart75baf692010-06-08 18:31:21 -04002621 struct scsi_cmnd *cmd;
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002622 int result;
James Smarta257bf92009-04-06 18:48:10 -04002623 struct scsi_device *tmp_sdev;
James Smart5ffc2662009-11-18 15:39:44 -05002624 int depth;
James Smartfa61a542008-01-11 01:52:42 -05002625 unsigned long flags;
James Smartea2151b2008-09-07 11:52:10 -04002626 struct lpfc_fast_path_event *fast_path_evt;
James Smart75baf692010-06-08 18:31:21 -04002627 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04002628 uint32_t queue_depth, scsi_id;
James Smart73d91e52011-10-10 21:32:10 -04002629 uint32_t logit = LOG_FCP;
dea31012005-04-17 16:05:31 -05002630
James Smart75baf692010-06-08 18:31:21 -04002631 /* Sanity check on return of outstanding command */
2632 if (!(lpfc_cmd->pCmd))
2633 return;
2634 cmd = lpfc_cmd->pCmd;
2635 shost = cmd->device->host;
2636
dea31012005-04-17 16:05:31 -05002637 lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
2638 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
James Smart341af102010-01-26 23:07:37 -05002639 /* pick up SLI4 exhange busy status from HBA */
2640 lpfc_cmd->exch_busy = pIocbOut->iocb_flag & LPFC_EXCHANGE_BUSY;
2641
James Smart109f6ed2008-12-04 22:39:08 -05002642 if (pnode && NLP_CHK_NODE_ACT(pnode))
2643 atomic_dec(&pnode->cmd_pending);
dea31012005-04-17 16:05:31 -05002644
2645 if (lpfc_cmd->status) {
2646 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
2647 (lpfc_cmd->result & IOERR_DRVR_MASK))
2648 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
2649 else if (lpfc_cmd->status >= IOSTAT_CNT)
2650 lpfc_cmd->status = IOSTAT_DEFAULT;
James Smart73d91e52011-10-10 21:32:10 -04002651 if (lpfc_cmd->status == IOSTAT_FCP_RSP_ERROR
2652 && !lpfc_cmd->fcp_rsp->rspStatus3
2653 && (lpfc_cmd->fcp_rsp->rspStatus2 & RESID_UNDER)
2654 && !(phba->cfg_log_verbose & LOG_FCP_UNDER))
2655 logit = 0;
2656 else
2657 logit = LOG_FCP | LOG_FCP_UNDER;
2658 lpfc_printf_vlog(vport, KERN_WARNING, logit,
2659 "9030 FCP cmd x%x failed <%d/%d> "
2660 "status: x%x result: x%x Data: x%x x%x\n",
2661 cmd->cmnd[0],
2662 cmd->device ? cmd->device->id : 0xffff,
2663 cmd->device ? cmd->device->lun : 0xffff,
2664 lpfc_cmd->status, lpfc_cmd->result,
2665 pIocbOut->iocb.ulpContext,
2666 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
dea31012005-04-17 16:05:31 -05002667
2668 switch (lpfc_cmd->status) {
2669 case IOSTAT_FCP_RSP_ERROR:
2670 /* Call FCP RSP handler to determine result */
James Smart2e0fef82007-06-17 19:56:36 -05002671 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea31012005-04-17 16:05:31 -05002672 break;
2673 case IOSTAT_NPORT_BSY:
2674 case IOSTAT_FABRIC_BSY:
James Smart0f1f53a2008-08-24 21:50:18 -04002675 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
James Smartea2151b2008-09-07 11:52:10 -04002676 fast_path_evt = lpfc_alloc_fast_evt(phba);
2677 if (!fast_path_evt)
2678 break;
2679 fast_path_evt->un.fabric_evt.event_type =
2680 FC_REG_FABRIC_EVENT;
2681 fast_path_evt->un.fabric_evt.subcategory =
2682 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
2683 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
2684 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
2685 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
2686 &pnode->nlp_portname,
2687 sizeof(struct lpfc_name));
2688 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
2689 &pnode->nlp_nodename,
2690 sizeof(struct lpfc_name));
2691 }
2692 fast_path_evt->vport = vport;
2693 fast_path_evt->work_evt.evt =
2694 LPFC_EVT_FASTPATH_MGMT_EVT;
2695 spin_lock_irqsave(&phba->hbalock, flags);
2696 list_add_tail(&fast_path_evt->work_evt.evt_listp,
2697 &phba->work_list);
2698 spin_unlock_irqrestore(&phba->hbalock, flags);
2699 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05002700 break;
James Smart92d7f7b2007-06-17 19:56:38 -05002701 case IOSTAT_LOCAL_REJECT:
James Smart1151e3e2011-02-16 12:39:35 -05002702 case IOSTAT_REMOTE_STOP:
James Smartab56dc22011-02-16 12:39:57 -05002703 if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
2704 lpfc_cmd->result ==
2705 IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
2706 lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
2707 lpfc_cmd->result ==
2708 IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
2709 cmd->result = ScsiResult(DID_NO_CONNECT, 0);
2710 break;
2711 }
James Smartd7c255b2008-08-24 21:50:00 -04002712 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
James Smart92d7f7b2007-06-17 19:56:38 -05002713 lpfc_cmd->result == IOERR_NO_RESOURCES ||
James Smartb92938b2010-06-07 15:24:12 -04002714 lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
2715 lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
James Smart92d7f7b2007-06-17 19:56:38 -05002716 cmd->result = ScsiResult(DID_REQUEUE, 0);
James Smart58da1ff2008-04-07 10:15:56 -04002717 break;
James Smarte2a0a9d2008-12-04 22:40:02 -05002718 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002719 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
2720 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
2721 pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
2722 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
2723 /*
2724 * This is a response for a BG enabled
2725 * cmd. Parse BG error
2726 */
2727 lpfc_parse_bg_err(phba, lpfc_cmd,
2728 pIocbOut);
2729 break;
2730 } else {
2731 lpfc_printf_vlog(vport, KERN_WARNING,
2732 LOG_BG,
2733 "9031 non-zero BGSTAT "
James Smart6a9c52c2009-10-02 15:16:51 -04002734 "on unprotected cmd\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05002735 }
2736 }
James Smart1151e3e2011-02-16 12:39:35 -05002737 if ((lpfc_cmd->status == IOSTAT_REMOTE_STOP)
2738 && (phba->sli_rev == LPFC_SLI_REV4)
2739 && (pnode && NLP_CHK_NODE_ACT(pnode))) {
2740 /* This IO was aborted by the target, we don't
2741 * know the rxid and because we did not send the
2742 * ABTS we cannot generate and RRQ.
2743 */
2744 lpfc_set_rrq_active(phba, pnode,
2745 lpfc_cmd->cur_iocbq.sli4_xritag,
2746 0, 0);
2747 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002748 /* else: fall through */
dea31012005-04-17 16:05:31 -05002749 default:
2750 cmd->result = ScsiResult(DID_ERROR, 0);
2751 break;
2752 }
2753
James Smart58da1ff2008-04-07 10:15:56 -04002754 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002755 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
James Smart0f1f53a2008-08-24 21:50:18 -04002756 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
2757 SAM_STAT_BUSY);
James Smartab56dc22011-02-16 12:39:57 -05002758 } else
dea31012005-04-17 16:05:31 -05002759 cmd->result = ScsiResult(DID_OK, 0);
dea31012005-04-17 16:05:31 -05002760
2761 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
2762 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
2763
James Smarte8b62012007-08-02 11:10:09 -04002764 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
2765 "0710 Iodone <%d/%d> cmd %p, error "
2766 "x%x SNS x%x x%x Data: x%x x%x\n",
2767 cmd->device->id, cmd->device->lun, cmd,
2768 cmd->result, *lp, *(lp + 3), cmd->retries,
2769 scsi_get_resid(cmd));
dea31012005-04-17 16:05:31 -05002770 }
2771
James Smartea2151b2008-09-07 11:52:10 -04002772 lpfc_update_stats(phba, lpfc_cmd);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002773 result = cmd->result;
James Smart977b5a02008-09-07 11:52:04 -04002774 if (vport->cfg_max_scsicmpl_time &&
2775 time_after(jiffies, lpfc_cmd->start_time +
2776 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
James Smarta257bf92009-04-06 18:48:10 -04002777 spin_lock_irqsave(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05002778 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
2779 if (pnode->cmd_qdepth >
2780 atomic_read(&pnode->cmd_pending) &&
2781 (atomic_read(&pnode->cmd_pending) >
2782 LPFC_MIN_TGT_QDEPTH) &&
2783 ((cmd->cmnd[0] == READ_10) ||
2784 (cmd->cmnd[0] == WRITE_10)))
2785 pnode->cmd_qdepth =
2786 atomic_read(&pnode->cmd_pending);
James Smart977b5a02008-09-07 11:52:04 -04002787
James Smart109f6ed2008-12-04 22:39:08 -05002788 pnode->last_change_time = jiffies;
2789 }
James Smarta257bf92009-04-06 18:48:10 -04002790 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05002791 } else if (pnode && NLP_CHK_NODE_ACT(pnode)) {
James Smart7dc517d2010-07-14 15:32:10 -04002792 if ((pnode->cmd_qdepth < vport->cfg_tgt_queue_depth) &&
James Smart977b5a02008-09-07 11:52:04 -04002793 time_after(jiffies, pnode->last_change_time +
James Smart109f6ed2008-12-04 22:39:08 -05002794 msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
James Smarta257bf92009-04-06 18:48:10 -04002795 spin_lock_irqsave(shost->host_lock, flags);
James Smart7dc517d2010-07-14 15:32:10 -04002796 depth = pnode->cmd_qdepth * LPFC_TGTQ_RAMPUP_PCENT
2797 / 100;
2798 depth = depth ? depth : 1;
2799 pnode->cmd_qdepth += depth;
2800 if (pnode->cmd_qdepth > vport->cfg_tgt_queue_depth)
2801 pnode->cmd_qdepth = vport->cfg_tgt_queue_depth;
James Smart109f6ed2008-12-04 22:39:08 -05002802 pnode->last_change_time = jiffies;
James Smarta257bf92009-04-06 18:48:10 -04002803 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05002804 }
James Smart977b5a02008-09-07 11:52:04 -04002805 }
2806
James Smart1dcb58e2007-04-25 09:51:30 -04002807 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James Smarta257bf92009-04-06 18:48:10 -04002808
2809 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
2810 queue_depth = cmd->device->queue_depth;
2811 scsi_id = cmd->device->id;
dea31012005-04-17 16:05:31 -05002812 cmd->scsi_done(cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002813
Jamie Wellnitzb8086082006-02-28 22:33:12 -05002814 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smartfa61a542008-01-11 01:52:42 -05002815 /*
2816 * If there is a thread waiting for command completion
2817 * wake up the thread.
2818 */
James Smarta257bf92009-04-06 18:48:10 -04002819 spin_lock_irqsave(shost->host_lock, flags);
James Smart495a7142008-06-14 22:52:59 -04002820 lpfc_cmd->pCmd = NULL;
James Smartfa61a542008-01-11 01:52:42 -05002821 if (lpfc_cmd->waitq)
2822 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04002823 spin_unlock_irqrestore(shost->host_lock, flags);
Jamie Wellnitzb8086082006-02-28 22:33:12 -05002824 lpfc_release_scsi_buf(phba, lpfc_cmd);
2825 return;
2826 }
2827
James Smart92d7f7b2007-06-17 19:56:38 -05002828 if (!result)
James Smarta257bf92009-04-06 18:48:10 -04002829 lpfc_rampup_queue_depth(vport, queue_depth);
James Smart92d7f7b2007-06-17 19:56:38 -05002830
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002831 /*
2832 * Check for queue full. If the lun is reporting queue full, then
2833 * back off the lun queue depth to prevent target overloads.
2834 */
James Smart58da1ff2008-04-07 10:15:56 -04002835 if (result == SAM_STAT_TASK_SET_FULL && pnode &&
2836 NLP_CHK_NODE_ACT(pnode)) {
James Smarta257bf92009-04-06 18:48:10 -04002837 shost_for_each_device(tmp_sdev, shost) {
2838 if (tmp_sdev->id != scsi_id)
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002839 continue;
2840 depth = scsi_track_queue_full(tmp_sdev,
James Smart5ffc2662009-11-18 15:39:44 -05002841 tmp_sdev->queue_depth-1);
2842 if (depth <= 0)
2843 continue;
James Smarte8b62012007-08-02 11:10:09 -04002844 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
2845 "0711 detected queue full - lun queue "
2846 "depth adjusted to %d.\n", depth);
James Smartea2151b2008-09-07 11:52:10 -04002847 lpfc_send_sdev_queuedepth_change_event(phba, vport,
James Smart5ffc2662009-11-18 15:39:44 -05002848 pnode,
2849 tmp_sdev->lun,
2850 depth+1, depth);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002851 }
2852 }
2853
James Smartfa61a542008-01-11 01:52:42 -05002854 /*
2855 * If there is a thread waiting for command completion
2856 * wake up the thread.
2857 */
James Smarta257bf92009-04-06 18:48:10 -04002858 spin_lock_irqsave(shost->host_lock, flags);
James Smart495a7142008-06-14 22:52:59 -04002859 lpfc_cmd->pCmd = NULL;
James Smartfa61a542008-01-11 01:52:42 -05002860 if (lpfc_cmd->waitq)
2861 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04002862 spin_unlock_irqrestore(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05002863
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002864 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05002865}
2866
James Smart34b02dc2008-08-24 21:49:55 -04002867/**
James Smart3621a712009-04-06 18:47:14 -04002868 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
James Smart34b02dc2008-08-24 21:49:55 -04002869 * @data: A pointer to the immediate command data portion of the IOCB.
2870 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
2871 *
2872 * The routine copies the entire FCP command from @fcp_cmnd to @data while
2873 * byte swapping the data to big endian format for transmission on the wire.
2874 **/
2875static void
2876lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
2877{
2878 int i, j;
2879 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
2880 i += sizeof(uint32_t), j++) {
2881 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
2882 }
2883}
2884
James Smart9bad7672008-12-04 22:39:02 -05002885/**
James Smartf1126682009-06-10 17:22:44 -04002886 * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05002887 * @vport: The virtual port for which this call is being executed.
2888 * @lpfc_cmd: The scsi command which needs to send.
2889 * @pnode: Pointer to lpfc_nodelist.
2890 *
2891 * This routine initializes fcp_cmnd and iocb data structure from scsi command
James Smart3772a992009-05-22 14:50:54 -04002892 * to transfer for device with SLI3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05002893 **/
dea31012005-04-17 16:05:31 -05002894static void
James Smartf1126682009-06-10 17:22:44 -04002895lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
James Smart2e0fef82007-06-17 19:56:36 -05002896 struct lpfc_nodelist *pnode)
dea31012005-04-17 16:05:31 -05002897{
James Smart2e0fef82007-06-17 19:56:36 -05002898 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002899 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2900 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2901 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2902 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
2903 int datadir = scsi_cmnd->sc_data_direction;
James Smart7e2b19f2007-10-29 11:00:39 -04002904 char tag[2];
dea31012005-04-17 16:05:31 -05002905
James Smart58da1ff2008-04-07 10:15:56 -04002906 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
2907 return;
2908
dea31012005-04-17 16:05:31 -05002909 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
James.Smart@Emulex.Com69859dc2005-08-10 15:02:37 -04002910 /* clear task management bits */
2911 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea31012005-04-17 16:05:31 -05002912
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -04002913 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
2914 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05002915
2916 memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, 16);
2917
James Smart7e2b19f2007-10-29 11:00:39 -04002918 if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
2919 switch (tag[0]) {
dea31012005-04-17 16:05:31 -05002920 case HEAD_OF_QUEUE_TAG:
2921 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
2922 break;
2923 case ORDERED_QUEUE_TAG:
2924 fcp_cmnd->fcpCntl1 = ORDERED_Q;
2925 break;
2926 default:
2927 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
2928 break;
2929 }
2930 } else
2931 fcp_cmnd->fcpCntl1 = 0;
2932
2933 /*
2934 * There are three possibilities here - use scatter-gather segment, use
2935 * the single mapping, or neither. Start the lpfc command prep by
2936 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
2937 * data bde entry.
2938 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002939 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05002940 if (datadir == DMA_TO_DEVICE) {
2941 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
James Smart3772a992009-05-22 14:50:54 -04002942 if (phba->sli_rev < LPFC_SLI_REV4) {
2943 iocb_cmd->un.fcpi.fcpi_parm = 0;
2944 iocb_cmd->ulpPU = 0;
2945 } else
2946 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05002947 fcp_cmnd->fcpCntl3 = WRITE_DATA;
2948 phba->fc4OutputRequests++;
2949 } else {
2950 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
2951 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05002952 fcp_cmnd->fcpCntl3 = READ_DATA;
2953 phba->fc4InputRequests++;
2954 }
2955 } else {
2956 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
2957 iocb_cmd->un.fcpi.fcpi_parm = 0;
2958 iocb_cmd->ulpPU = 0;
2959 fcp_cmnd->fcpCntl3 = 0;
2960 phba->fc4ControlRequests++;
2961 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002962 if (phba->sli_rev == 3 &&
2963 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04002964 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05002965 /*
2966 * Finish initializing those IOCB fields that are independent
2967 * of the scsi_cmnd request_buffer
2968 */
2969 piocbq->iocb.ulpContext = pnode->nlp_rpi;
James Smart6d368e52011-05-24 11:44:12 -04002970 if (phba->sli_rev == LPFC_SLI_REV4)
2971 piocbq->iocb.ulpContext =
2972 phba->sli4_hba.rpi_ids[pnode->nlp_rpi];
dea31012005-04-17 16:05:31 -05002973 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
2974 piocbq->iocb.ulpFCP2Rcvy = 1;
James Smart09372822008-01-11 01:52:54 -05002975 else
2976 piocbq->iocb.ulpFCP2Rcvy = 0;
dea31012005-04-17 16:05:31 -05002977
2978 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
2979 piocbq->context1 = lpfc_cmd;
2980 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
2981 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
James Smart2e0fef82007-06-17 19:56:36 -05002982 piocbq->vport = vport;
dea31012005-04-17 16:05:31 -05002983}
2984
James Smart9bad7672008-12-04 22:39:02 -05002985/**
James Smart6d368e52011-05-24 11:44:12 -04002986 * lpfc_scsi_prep_task_mgmt_cmd - Convert SLI3 scsi TM cmd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05002987 * @vport: The virtual port for which this call is being executed.
2988 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
2989 * @lun: Logical unit number.
2990 * @task_mgmt_cmd: SCSI task management command.
2991 *
James Smart3772a992009-05-22 14:50:54 -04002992 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
2993 * for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05002994 *
2995 * Return codes:
2996 * 0 - Error
2997 * 1 - Success
2998 **/
dea31012005-04-17 16:05:31 -05002999static int
James Smartf1126682009-06-10 17:22:44 -04003000lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
dea31012005-04-17 16:05:31 -05003001 struct lpfc_scsi_buf *lpfc_cmd,
James Smart420b630d2006-07-06 15:50:16 -04003002 unsigned int lun,
dea31012005-04-17 16:05:31 -05003003 uint8_t task_mgmt_cmd)
3004{
dea31012005-04-17 16:05:31 -05003005 struct lpfc_iocbq *piocbq;
3006 IOCB_t *piocb;
3007 struct fcp_cmnd *fcp_cmnd;
James Smart0b18ac42006-05-01 21:50:40 -04003008 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea31012005-04-17 16:05:31 -05003009 struct lpfc_nodelist *ndlp = rdata->pnode;
3010
James Smart58da1ff2008-04-07 10:15:56 -04003011 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
3012 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05003013 return 0;
dea31012005-04-17 16:05:31 -05003014
dea31012005-04-17 16:05:31 -05003015 piocbq = &(lpfc_cmd->cur_iocbq);
James Smart2e0fef82007-06-17 19:56:36 -05003016 piocbq->vport = vport;
3017
dea31012005-04-17 16:05:31 -05003018 piocb = &piocbq->iocb;
3019
3020 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart34b02dc2008-08-24 21:49:55 -04003021 /* Clear out any old data in the FCP command area */
3022 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
3023 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05003024 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
James Smarte2a0a9d2008-12-04 22:40:02 -05003025 if (vport->phba->sli_rev == 3 &&
3026 !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04003027 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05003028 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
dea31012005-04-17 16:05:31 -05003029 piocb->ulpContext = ndlp->nlp_rpi;
James Smart6d368e52011-05-24 11:44:12 -04003030 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
3031 piocb->ulpContext =
3032 vport->phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
3033 }
dea31012005-04-17 16:05:31 -05003034 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
3035 piocb->ulpFCP2Rcvy = 1;
3036 }
3037 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
3038
3039 /* ulpTimeout is only one byte */
3040 if (lpfc_cmd->timeout > 0xff) {
3041 /*
3042 * Do not timeout the command at the firmware level.
3043 * The driver will provide the timeout mechanism.
3044 */
3045 piocb->ulpTimeout = 0;
James Smartf1126682009-06-10 17:22:44 -04003046 } else
dea31012005-04-17 16:05:31 -05003047 piocb->ulpTimeout = lpfc_cmd->timeout;
James Smartf1126682009-06-10 17:22:44 -04003048
3049 if (vport->phba->sli_rev == LPFC_SLI_REV4)
3050 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05003051
James Smart2e0fef82007-06-17 19:56:36 -05003052 return 1;
dea31012005-04-17 16:05:31 -05003053}
3054
James Smart9bad7672008-12-04 22:39:02 -05003055/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003056 * lpfc_scsi_api_table_setup - Set up scsi api function jump table
James Smart3772a992009-05-22 14:50:54 -04003057 * @phba: The hba struct for which this call is being executed.
3058 * @dev_grp: The HBA PCI-Device group number.
3059 *
3060 * This routine sets up the SCSI interface API function jump table in @phba
3061 * struct.
3062 * Returns: 0 - success, -ENODEV - failure.
3063 **/
3064int
3065lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
3066{
3067
James Smartf1126682009-06-10 17:22:44 -04003068 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
3069 phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
James Smartf1126682009-06-10 17:22:44 -04003070
James Smart3772a992009-05-22 14:50:54 -04003071 switch (dev_grp) {
3072 case LPFC_PCI_DEV_LP:
3073 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s3;
3074 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04003075 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
James Smart19ca7602010-11-20 23:11:55 -05003076 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04003077 break;
James Smartda0436e2009-05-22 14:51:39 -04003078 case LPFC_PCI_DEV_OC:
3079 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s4;
3080 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04003081 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
James Smart19ca7602010-11-20 23:11:55 -05003082 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04003083 break;
James Smart3772a992009-05-22 14:50:54 -04003084 default:
3085 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3086 "1418 Invalid HBA PCI-device group: 0x%x\n",
3087 dev_grp);
3088 return -ENODEV;
3089 break;
3090 }
James Smart3772a992009-05-22 14:50:54 -04003091 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
James Smart84d1b002010-02-12 14:42:33 -05003092 phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
James Smart3772a992009-05-22 14:50:54 -04003093 return 0;
3094}
3095
3096/**
James Smart3621a712009-04-06 18:47:14 -04003097 * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
James Smart9bad7672008-12-04 22:39:02 -05003098 * @phba: The Hba for which this call is being executed.
3099 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
3100 * @rspiocbq: Pointer to lpfc_iocbq data structure.
3101 *
3102 * This routine is IOCB completion routine for device reset and target reset
3103 * routine. This routine release scsi buffer associated with lpfc_cmd.
3104 **/
James Smart7054a602007-04-25 09:52:34 -04003105static void
3106lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
3107 struct lpfc_iocbq *cmdiocbq,
3108 struct lpfc_iocbq *rspiocbq)
3109{
3110 struct lpfc_scsi_buf *lpfc_cmd =
3111 (struct lpfc_scsi_buf *) cmdiocbq->context1;
3112 if (lpfc_cmd)
3113 lpfc_release_scsi_buf(phba, lpfc_cmd);
3114 return;
3115}
3116
James Smart9bad7672008-12-04 22:39:02 -05003117/**
James Smart3621a712009-04-06 18:47:14 -04003118 * lpfc_info - Info entry point of scsi_host_template data structure
James Smart9bad7672008-12-04 22:39:02 -05003119 * @host: The scsi host for which this call is being executed.
3120 *
3121 * This routine provides module information about hba.
3122 *
3123 * Reutrn code:
3124 * Pointer to char - Success.
3125 **/
dea31012005-04-17 16:05:31 -05003126const char *
3127lpfc_info(struct Scsi_Host *host)
3128{
James Smart2e0fef82007-06-17 19:56:36 -05003129 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
3130 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003131 int len;
3132 static char lpfcinfobuf[384];
3133
3134 memset(lpfcinfobuf,0,384);
3135 if (phba && phba->pcidev){
3136 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
3137 len = strlen(lpfcinfobuf);
3138 snprintf(lpfcinfobuf + len,
3139 384-len,
3140 " on PCI bus %02x device %02x irq %d",
3141 phba->pcidev->bus->number,
3142 phba->pcidev->devfn,
3143 phba->pcidev->irq);
3144 len = strlen(lpfcinfobuf);
3145 if (phba->Port[0]) {
3146 snprintf(lpfcinfobuf + len,
3147 384-len,
3148 " port %s",
3149 phba->Port);
3150 }
James Smart65467b62010-01-26 23:08:29 -05003151 len = strlen(lpfcinfobuf);
3152 if (phba->sli4_hba.link_state.logical_speed) {
3153 snprintf(lpfcinfobuf + len,
3154 384-len,
3155 " Logical Link Speed: %d Mbps",
3156 phba->sli4_hba.link_state.logical_speed * 10);
3157 }
dea31012005-04-17 16:05:31 -05003158 }
3159 return lpfcinfobuf;
3160}
3161
James Smart9bad7672008-12-04 22:39:02 -05003162/**
James Smart3621a712009-04-06 18:47:14 -04003163 * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
James Smart9bad7672008-12-04 22:39:02 -05003164 * @phba: The Hba for which this call is being executed.
3165 *
3166 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
3167 * The default value of cfg_poll_tmo is 10 milliseconds.
3168 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003169static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
3170{
3171 unsigned long poll_tmo_expires =
3172 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
3173
3174 if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
3175 mod_timer(&phba->fcp_poll_timer,
3176 poll_tmo_expires);
3177}
3178
James Smart9bad7672008-12-04 22:39:02 -05003179/**
James Smart3621a712009-04-06 18:47:14 -04003180 * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
James Smart9bad7672008-12-04 22:39:02 -05003181 * @phba: The Hba for which this call is being executed.
3182 *
3183 * This routine starts the fcp_poll_timer of @phba.
3184 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003185void lpfc_poll_start_timer(struct lpfc_hba * phba)
3186{
3187 lpfc_poll_rearm_timer(phba);
3188}
3189
James Smart9bad7672008-12-04 22:39:02 -05003190/**
James Smart3621a712009-04-06 18:47:14 -04003191 * lpfc_poll_timeout - Restart polling timer
James Smart9bad7672008-12-04 22:39:02 -05003192 * @ptr: Map to lpfc_hba data structure pointer.
3193 *
3194 * This routine restarts fcp_poll timer, when FCP ring polling is enable
3195 * and FCP Ring interrupt is disable.
3196 **/
3197
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003198void lpfc_poll_timeout(unsigned long ptr)
3199{
James Smart2e0fef82007-06-17 19:56:36 -05003200 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003201
3202 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04003203 lpfc_sli_handle_fast_ring_event(phba,
3204 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
3205
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003206 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
3207 lpfc_poll_rearm_timer(phba);
3208 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003209}
3210
James Smart9bad7672008-12-04 22:39:02 -05003211/**
James Smart3621a712009-04-06 18:47:14 -04003212 * lpfc_queuecommand - scsi_host_template queuecommand entry point
James Smart9bad7672008-12-04 22:39:02 -05003213 * @cmnd: Pointer to scsi_cmnd data structure.
3214 * @done: Pointer to done routine.
3215 *
3216 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
3217 * This routine prepares an IOCB from scsi command and provides to firmware.
3218 * The @done callback is invoked after driver finished processing the command.
3219 *
3220 * Return value :
3221 * 0 - Success
3222 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
3223 **/
dea31012005-04-17 16:05:31 -05003224static int
Jeff Garzikf2812332010-11-16 02:10:29 -05003225lpfc_queuecommand_lck(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
dea31012005-04-17 16:05:31 -05003226{
James Smart2e0fef82007-06-17 19:56:36 -05003227 struct Scsi_Host *shost = cmnd->device->host;
3228 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3229 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003230 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05003231 struct lpfc_nodelist *ndlp;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003232 struct lpfc_scsi_buf *lpfc_cmd;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003233 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003234 int err;
dea31012005-04-17 16:05:31 -05003235
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003236 err = fc_remote_port_chkready(rport);
3237 if (err) {
3238 cmnd->result = err;
dea31012005-04-17 16:05:31 -05003239 goto out_fail_command;
3240 }
James Smart1c6f4ef52009-11-18 15:40:49 -05003241 ndlp = rdata->pnode;
dea31012005-04-17 16:05:31 -05003242
James Smartbf086112011-08-21 21:48:13 -04003243 if ((scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) &&
3244 (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED) ||
3245 (phba->sli_rev == LPFC_SLI_REV4))) {
James Smarte2a0a9d2008-12-04 22:40:02 -05003246
James Smart6a9c52c2009-10-02 15:16:51 -04003247 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
3248 "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
3249 " op:%02x str=%s without registering for"
3250 " BlockGuard - Rejecting command\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05003251 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
3252 dif_op_str[scsi_get_prot_op(cmnd)]);
3253 goto out_fail_command;
3254 }
3255
dea31012005-04-17 16:05:31 -05003256 /*
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003257 * Catch race where our node has transitioned, but the
3258 * transport is still transitioning.
dea31012005-04-17 16:05:31 -05003259 */
James Smartb522d7d2008-09-07 11:51:56 -04003260 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
Mike Christief55ca84d2011-02-25 14:04:27 -06003261 cmnd->result = ScsiResult(DID_IMM_RETRY, 0);
James Smartb522d7d2008-09-07 11:51:56 -04003262 goto out_fail_command;
3263 }
James Smart7dc517d2010-07-14 15:32:10 -04003264 if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth)
Mike Christie34963432011-02-25 14:04:28 -06003265 goto out_tgt_busy;
Mike Christiea93ce022008-08-17 15:24:41 -05003266
James Smart19ca7602010-11-20 23:11:55 -05003267 lpfc_cmd = lpfc_get_scsi_buf(phba, ndlp);
dea31012005-04-17 16:05:31 -05003268 if (lpfc_cmd == NULL) {
James Smarteaf15d52008-12-04 22:39:29 -05003269 lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003270
James Smarte8b62012007-08-02 11:10:09 -04003271 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
3272 "0707 driver's buffer pool is empty, "
3273 "IO busied\n");
dea31012005-04-17 16:05:31 -05003274 goto out_host_busy;
3275 }
3276
3277 /*
3278 * Store the midlayer's command structure for the completion phase
3279 * and complete the command initialization.
3280 */
3281 lpfc_cmd->pCmd = cmnd;
3282 lpfc_cmd->rdata = rdata;
3283 lpfc_cmd->timeout = 0;
James Smart977b5a02008-09-07 11:52:04 -04003284 lpfc_cmd->start_time = jiffies;
dea31012005-04-17 16:05:31 -05003285 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
3286 cmnd->scsi_done = done;
3287
James Smarte2a0a9d2008-12-04 22:40:02 -05003288 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
James Smart6a9c52c2009-10-02 15:16:51 -04003289 if (vport->phba->cfg_enable_bg) {
3290 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05003291 "9033 BLKGRD: rcvd protected cmd:%02x op:%02x "
3292 "str=%s\n",
3293 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
3294 dif_op_str[scsi_get_prot_op(cmnd)]);
James Smart6a9c52c2009-10-02 15:16:51 -04003295 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05003296 "9034 BLKGRD: CDB: %02x %02x %02x %02x %02x "
James Smarte4e74272009-07-19 10:01:38 -04003297 "%02x %02x %02x %02x %02x\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05003298 cmnd->cmnd[0], cmnd->cmnd[1], cmnd->cmnd[2],
3299 cmnd->cmnd[3], cmnd->cmnd[4], cmnd->cmnd[5],
3300 cmnd->cmnd[6], cmnd->cmnd[7], cmnd->cmnd[8],
3301 cmnd->cmnd[9]);
James Smart6a9c52c2009-10-02 15:16:51 -04003302 if (cmnd->cmnd[0] == READ_10)
3303 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05003304 "9035 BLKGRD: READ @ sector %llu, "
Tejun Heo83096eb2009-05-07 22:24:39 +09003305 "count %u\n",
3306 (unsigned long long)scsi_get_lba(cmnd),
3307 blk_rq_sectors(cmnd->request));
James Smart6a9c52c2009-10-02 15:16:51 -04003308 else if (cmnd->cmnd[0] == WRITE_10)
3309 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05003310 "9036 BLKGRD: WRITE @ sector %llu, "
Tejun Heo83096eb2009-05-07 22:24:39 +09003311 "count %u cmd=%p\n",
James Smart87b5c322008-12-16 10:34:09 -05003312 (unsigned long long)scsi_get_lba(cmnd),
Tejun Heo83096eb2009-05-07 22:24:39 +09003313 blk_rq_sectors(cmnd->request),
James Smarte2a0a9d2008-12-04 22:40:02 -05003314 cmnd);
James Smart6a9c52c2009-10-02 15:16:51 -04003315 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003316
3317 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
3318 } else {
James Smart6a9c52c2009-10-02 15:16:51 -04003319 if (vport->phba->cfg_enable_bg) {
James Smarte2a0a9d2008-12-04 22:40:02 -05003320 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smart6a9c52c2009-10-02 15:16:51 -04003321 "9038 BLKGRD: rcvd unprotected cmd:"
3322 "%02x op:%02x str=%s\n",
3323 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
3324 dif_op_str[scsi_get_prot_op(cmnd)]);
3325 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
3326 "9039 BLKGRD: CDB: %02x %02x %02x "
3327 "%02x %02x %02x %02x %02x %02x %02x\n",
3328 cmnd->cmnd[0], cmnd->cmnd[1],
3329 cmnd->cmnd[2], cmnd->cmnd[3],
3330 cmnd->cmnd[4], cmnd->cmnd[5],
3331 cmnd->cmnd[6], cmnd->cmnd[7],
3332 cmnd->cmnd[8], cmnd->cmnd[9]);
3333 if (cmnd->cmnd[0] == READ_10)
3334 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
3335 "9040 dbg: READ @ sector %llu, "
3336 "count %u\n",
3337 (unsigned long long)scsi_get_lba(cmnd),
Tejun Heo83096eb2009-05-07 22:24:39 +09003338 blk_rq_sectors(cmnd->request));
James Smart6a9c52c2009-10-02 15:16:51 -04003339 else if (cmnd->cmnd[0] == WRITE_10)
3340 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05003341 "9041 dbg: WRITE @ sector %llu, "
Tejun Heo83096eb2009-05-07 22:24:39 +09003342 "count %u cmd=%p\n",
James Smart87b5c322008-12-16 10:34:09 -05003343 (unsigned long long)scsi_get_lba(cmnd),
Tejun Heo83096eb2009-05-07 22:24:39 +09003344 blk_rq_sectors(cmnd->request), cmnd);
James Smart6a9c52c2009-10-02 15:16:51 -04003345 else
3346 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05003347 "9042 dbg: parser not implemented\n");
James Smart6a9c52c2009-10-02 15:16:51 -04003348 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003349 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
3350 }
3351
dea31012005-04-17 16:05:31 -05003352 if (err)
3353 goto out_host_busy_free_buf;
3354
James Smart2e0fef82007-06-17 19:56:36 -05003355 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea31012005-04-17 16:05:31 -05003356
James Smart977b5a02008-09-07 11:52:04 -04003357 atomic_inc(&ndlp->cmd_pending);
James Smart3772a992009-05-22 14:50:54 -04003358 err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
James Smart92d7f7b2007-06-17 19:56:38 -05003359 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
James Smarteaf15d52008-12-04 22:39:29 -05003360 if (err) {
3361 atomic_dec(&ndlp->cmd_pending);
dea31012005-04-17 16:05:31 -05003362 goto out_host_busy_free_buf;
James Smarteaf15d52008-12-04 22:39:29 -05003363 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003364 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04003365 spin_unlock(shost->host_lock);
3366 lpfc_sli_handle_fast_ring_event(phba,
3367 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
3368
3369 spin_lock(shost->host_lock);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003370 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
3371 lpfc_poll_rearm_timer(phba);
3372 }
3373
dea31012005-04-17 16:05:31 -05003374 return 0;
3375
3376 out_host_busy_free_buf:
James Smartbcf4dbf2006-07-06 15:50:08 -04003377 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003378 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05003379 out_host_busy:
3380 return SCSI_MLQUEUE_HOST_BUSY;
3381
Mike Christie34963432011-02-25 14:04:28 -06003382 out_tgt_busy:
3383 return SCSI_MLQUEUE_TARGET_BUSY;
3384
dea31012005-04-17 16:05:31 -05003385 out_fail_command:
3386 done(cmnd);
3387 return 0;
3388}
3389
Jeff Garzikf2812332010-11-16 02:10:29 -05003390static DEF_SCSI_QCMD(lpfc_queuecommand)
3391
James Smart9bad7672008-12-04 22:39:02 -05003392/**
James Smart3621a712009-04-06 18:47:14 -04003393 * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05003394 * @cmnd: Pointer to scsi_cmnd data structure.
3395 *
3396 * This routine aborts @cmnd pending in base driver.
3397 *
3398 * Return code :
3399 * 0x2003 - Error
3400 * 0x2002 - Success
3401 **/
dea31012005-04-17 16:05:31 -05003402static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05003403lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05003404{
James Smart2e0fef82007-06-17 19:56:36 -05003405 struct Scsi_Host *shost = cmnd->device->host;
3406 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3407 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003408 struct lpfc_iocbq *iocb;
3409 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05003410 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05003411 IOCB_t *cmd, *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003412 int ret = SUCCESS;
James Smartfa61a542008-01-11 01:52:42 -05003413 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003414
James Smart589a52d2010-07-14 15:30:54 -04003415 ret = fc_block_scsi_eh(cmnd);
3416 if (ret)
3417 return ret;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003418 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
James Smarteee88772010-09-29 11:19:08 -04003419 if (!lpfc_cmd) {
3420 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3421 "2873 SCSI Layer I/O Abort Request IO CMPL Status "
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04003422 "x%x ID %d LUN %d\n",
3423 ret, cmnd->device->id, cmnd->device->lun);
James Smarteee88772010-09-29 11:19:08 -04003424 return SUCCESS;
3425 }
dea31012005-04-17 16:05:31 -05003426
3427 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003428 * If pCmd field of the corresponding lpfc_scsi_buf structure
3429 * points to a different SCSI command, then the driver has
3430 * already completed this command, but the midlayer did not
3431 * see the completion before the eh fired. Just return
3432 * SUCCESS.
dea31012005-04-17 16:05:31 -05003433 */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003434 iocb = &lpfc_cmd->cur_iocbq;
3435 if (lpfc_cmd->pCmd != cmnd)
3436 goto out;
dea31012005-04-17 16:05:31 -05003437
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003438 BUG_ON(iocb->context1 != lpfc_cmd);
dea31012005-04-17 16:05:31 -05003439
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003440 abtsiocb = lpfc_sli_get_iocbq(phba);
3441 if (abtsiocb == NULL) {
3442 ret = FAILED;
dea31012005-04-17 16:05:31 -05003443 goto out;
3444 }
3445
dea31012005-04-17 16:05:31 -05003446 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003447 * The scsi command can not be in txq and it is in flight because the
3448 * pCmd is still pointig at the SCSI command we have to abort. There
3449 * is no need to search the txcmplq. Just send an abort to the FW.
dea31012005-04-17 16:05:31 -05003450 */
dea31012005-04-17 16:05:31 -05003451
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003452 cmd = &iocb->iocb;
3453 icmd = &abtsiocb->iocb;
3454 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
3455 icmd->un.acxri.abortContextTag = cmd->ulpContext;
James Smart3772a992009-05-22 14:50:54 -04003456 if (phba->sli_rev == LPFC_SLI_REV4)
3457 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
3458 else
3459 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05003460
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003461 icmd->ulpLe = 1;
3462 icmd->ulpClass = cmd->ulpClass;
James Smart5ffc2662009-11-18 15:39:44 -05003463
3464 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
3465 abtsiocb->fcp_wqidx = iocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05003466 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05003467
James Smart2e0fef82007-06-17 19:56:36 -05003468 if (lpfc_is_link_up(phba))
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003469 icmd->ulpCommand = CMD_ABORT_XRI_CN;
3470 else
3471 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea31012005-04-17 16:05:31 -05003472
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003473 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smart2e0fef82007-06-17 19:56:36 -05003474 abtsiocb->vport = vport;
James Smart3772a992009-05-22 14:50:54 -04003475 if (lpfc_sli_issue_iocb(phba, LPFC_FCP_RING, abtsiocb, 0) ==
3476 IOCB_ERROR) {
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003477 lpfc_sli_release_iocbq(phba, abtsiocb);
3478 ret = FAILED;
3479 goto out;
3480 }
3481
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003482 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
James Smart45ed1192009-10-02 15:17:02 -04003483 lpfc_sli_handle_fast_ring_event(phba,
3484 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003485
James Smartfa61a542008-01-11 01:52:42 -05003486 lpfc_cmd->waitq = &waitq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003487 /* Wait for abort to complete */
James Smartfa61a542008-01-11 01:52:42 -05003488 wait_event_timeout(waitq,
3489 (lpfc_cmd->pCmd != cmnd),
3490 (2*vport->cfg_devloss_tmo*HZ));
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003491
James Smartfa61a542008-01-11 01:52:42 -05003492 spin_lock_irq(shost->host_lock);
3493 lpfc_cmd->waitq = NULL;
3494 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003495
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003496 if (lpfc_cmd->pCmd == cmnd) {
3497 ret = FAILED;
James Smarte8b62012007-08-02 11:10:09 -04003498 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3499 "0748 abort handler timed out waiting "
3500 "for abort to complete: ret %#x, ID %d, "
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04003501 "LUN %d\n",
3502 ret, cmnd->device->id, cmnd->device->lun);
dea31012005-04-17 16:05:31 -05003503 }
3504
3505 out:
James Smarte8b62012007-08-02 11:10:09 -04003506 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3507 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04003508 "LUN %d\n", ret, cmnd->device->id,
3509 cmnd->device->lun);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003510 return ret;
dea31012005-04-17 16:05:31 -05003511}
3512
James Smartbbb9d182009-06-10 17:23:16 -04003513static char *
3514lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
3515{
3516 switch (task_mgmt_cmd) {
3517 case FCP_ABORT_TASK_SET:
3518 return "ABORT_TASK_SET";
3519 case FCP_CLEAR_TASK_SET:
3520 return "FCP_CLEAR_TASK_SET";
3521 case FCP_BUS_RESET:
3522 return "FCP_BUS_RESET";
3523 case FCP_LUN_RESET:
3524 return "FCP_LUN_RESET";
3525 case FCP_TARGET_RESET:
3526 return "FCP_TARGET_RESET";
3527 case FCP_CLEAR_ACA:
3528 return "FCP_CLEAR_ACA";
3529 case FCP_TERMINATE_TASK:
3530 return "FCP_TERMINATE_TASK";
3531 default:
3532 return "unknown";
3533 }
3534}
3535
3536/**
3537 * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
3538 * @vport: The virtual port for which this call is being executed.
3539 * @rdata: Pointer to remote port local data
3540 * @tgt_id: Target ID of remote device.
3541 * @lun_id: Lun number for the TMF
3542 * @task_mgmt_cmd: type of TMF to send
3543 *
3544 * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
3545 * a remote port.
3546 *
3547 * Return Code:
3548 * 0x2003 - Error
3549 * 0x2002 - Success.
3550 **/
3551static int
3552lpfc_send_taskmgmt(struct lpfc_vport *vport, struct lpfc_rport_data *rdata,
3553 unsigned tgt_id, unsigned int lun_id,
3554 uint8_t task_mgmt_cmd)
3555{
3556 struct lpfc_hba *phba = vport->phba;
3557 struct lpfc_scsi_buf *lpfc_cmd;
3558 struct lpfc_iocbq *iocbq;
3559 struct lpfc_iocbq *iocbqrsp;
James Smart5989b8d2010-10-22 11:06:56 -04003560 struct lpfc_nodelist *pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04003561 int ret;
3562 int status;
3563
James Smart5989b8d2010-10-22 11:06:56 -04003564 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
James Smartbbb9d182009-06-10 17:23:16 -04003565 return FAILED;
3566
James Smart19ca7602010-11-20 23:11:55 -05003567 lpfc_cmd = lpfc_get_scsi_buf(phba, rdata->pnode);
James Smartbbb9d182009-06-10 17:23:16 -04003568 if (lpfc_cmd == NULL)
3569 return FAILED;
3570 lpfc_cmd->timeout = 60;
3571 lpfc_cmd->rdata = rdata;
3572
3573 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
3574 task_mgmt_cmd);
3575 if (!status) {
3576 lpfc_release_scsi_buf(phba, lpfc_cmd);
3577 return FAILED;
3578 }
3579
3580 iocbq = &lpfc_cmd->cur_iocbq;
3581 iocbqrsp = lpfc_sli_get_iocbq(phba);
3582 if (iocbqrsp == NULL) {
3583 lpfc_release_scsi_buf(phba, lpfc_cmd);
3584 return FAILED;
3585 }
3586
3587 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
3588 "0702 Issue %s to TGT %d LUN %d "
James Smart6d368e52011-05-24 11:44:12 -04003589 "rpi x%x nlp_flag x%x Data: x%x x%x\n",
James Smartbbb9d182009-06-10 17:23:16 -04003590 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
James Smart6d368e52011-05-24 11:44:12 -04003591 pnode->nlp_rpi, pnode->nlp_flag, iocbq->sli4_xritag,
3592 iocbq->iocb_flag);
James Smartbbb9d182009-06-10 17:23:16 -04003593
3594 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
3595 iocbq, iocbqrsp, lpfc_cmd->timeout);
3596 if (status != IOCB_SUCCESS) {
3597 if (status == IOCB_TIMEDOUT) {
3598 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
3599 ret = TIMEOUT_ERROR;
3600 } else
3601 ret = FAILED;
3602 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
3603 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smart6d368e52011-05-24 11:44:12 -04003604 "0727 TMF %s to TGT %d LUN %d failed (%d, %d) "
3605 "iocb_flag x%x\n",
James Smartbbb9d182009-06-10 17:23:16 -04003606 lpfc_taskmgmt_name(task_mgmt_cmd),
3607 tgt_id, lun_id, iocbqrsp->iocb.ulpStatus,
James Smart6d368e52011-05-24 11:44:12 -04003608 iocbqrsp->iocb.un.ulpWord[4],
3609 iocbq->iocb_flag);
James Smart2a9bf3d2010-06-07 15:24:45 -04003610 } else if (status == IOCB_BUSY)
3611 ret = FAILED;
3612 else
James Smartbbb9d182009-06-10 17:23:16 -04003613 ret = SUCCESS;
3614
3615 lpfc_sli_release_iocbq(phba, iocbqrsp);
3616
3617 if (ret != TIMEOUT_ERROR)
3618 lpfc_release_scsi_buf(phba, lpfc_cmd);
3619
3620 return ret;
3621}
3622
3623/**
3624 * lpfc_chk_tgt_mapped -
3625 * @vport: The virtual port to check on
3626 * @cmnd: Pointer to scsi_cmnd data structure.
3627 *
3628 * This routine delays until the scsi target (aka rport) for the
3629 * command exists (is present and logged in) or we declare it non-existent.
3630 *
3631 * Return code :
3632 * 0x2003 - Error
3633 * 0x2002 - Success
3634 **/
3635static int
3636lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
3637{
3638 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05003639 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04003640 unsigned long later;
3641
James Smart1c6f4ef52009-11-18 15:40:49 -05003642 if (!rdata) {
3643 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
3644 "0797 Tgt Map rport failure: rdata x%p\n", rdata);
3645 return FAILED;
3646 }
3647 pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04003648 /*
3649 * If target is not in a MAPPED state, delay until
3650 * target is rediscovered or devloss timeout expires.
3651 */
3652 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
3653 while (time_after(later, jiffies)) {
3654 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3655 return FAILED;
3656 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
3657 return SUCCESS;
3658 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
3659 rdata = cmnd->device->hostdata;
3660 if (!rdata)
3661 return FAILED;
3662 pnode = rdata->pnode;
3663 }
3664 if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
3665 (pnode->nlp_state != NLP_STE_MAPPED_NODE))
3666 return FAILED;
3667 return SUCCESS;
3668}
3669
3670/**
3671 * lpfc_reset_flush_io_context -
3672 * @vport: The virtual port (scsi_host) for the flush context
3673 * @tgt_id: If aborting by Target contect - specifies the target id
3674 * @lun_id: If aborting by Lun context - specifies the lun id
3675 * @context: specifies the context level to flush at.
3676 *
3677 * After a reset condition via TMF, we need to flush orphaned i/o
3678 * contexts from the adapter. This routine aborts any contexts
3679 * outstanding, then waits for their completions. The wait is
3680 * bounded by devloss_tmo though.
3681 *
3682 * Return code :
3683 * 0x2003 - Error
3684 * 0x2002 - Success
3685 **/
3686static int
3687lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
3688 uint64_t lun_id, lpfc_ctx_cmd context)
3689{
3690 struct lpfc_hba *phba = vport->phba;
3691 unsigned long later;
3692 int cnt;
3693
3694 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
3695 if (cnt)
3696 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
3697 tgt_id, lun_id, context);
3698 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
3699 while (time_after(later, jiffies) && cnt) {
3700 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
3701 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
3702 }
3703 if (cnt) {
3704 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3705 "0724 I/O flush failure for context %s : cnt x%x\n",
3706 ((context == LPFC_CTX_LUN) ? "LUN" :
3707 ((context == LPFC_CTX_TGT) ? "TGT" :
3708 ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
3709 cnt);
3710 return FAILED;
3711 }
3712 return SUCCESS;
3713}
3714
James Smart9bad7672008-12-04 22:39:02 -05003715/**
James Smart3621a712009-04-06 18:47:14 -04003716 * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
James Smart9bad7672008-12-04 22:39:02 -05003717 * @cmnd: Pointer to scsi_cmnd data structure.
3718 *
James Smartbbb9d182009-06-10 17:23:16 -04003719 * This routine does a device reset by sending a LUN_RESET task management
James Smart9bad7672008-12-04 22:39:02 -05003720 * command.
3721 *
3722 * Return code :
3723 * 0x2003 - Error
James Smart3621a712009-04-06 18:47:14 -04003724 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05003725 **/
dea31012005-04-17 16:05:31 -05003726static int
James Smart7054a602007-04-25 09:52:34 -04003727lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05003728{
James Smart2e0fef82007-06-17 19:56:36 -05003729 struct Scsi_Host *shost = cmnd->device->host;
3730 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05003731 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05003732 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04003733 unsigned tgt_id = cmnd->device->id;
3734 unsigned int lun_id = cmnd->device->lun;
James Smartea2151b2008-09-07 11:52:10 -04003735 struct lpfc_scsi_event_header scsi_event;
James Smartbbb9d182009-06-10 17:23:16 -04003736 int status;
dea31012005-04-17 16:05:31 -05003737
James Smart1c6f4ef52009-11-18 15:40:49 -05003738 if (!rdata) {
3739 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3740 "0798 Device Reset rport failure: rdata x%p\n", rdata);
3741 return FAILED;
3742 }
3743 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04003744 status = fc_block_scsi_eh(cmnd);
3745 if (status)
3746 return status;
James Smartbbb9d182009-06-10 17:23:16 -04003747
3748 status = lpfc_chk_tgt_mapped(vport, cmnd);
3749 if (status == FAILED) {
3750 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3751 "0721 Device Reset rport failure: rdata x%p\n", rdata);
3752 return FAILED;
3753 }
3754
3755 scsi_event.event_type = FC_REG_SCSI_EVENT;
3756 scsi_event.subcategory = LPFC_EVENT_LUNRESET;
3757 scsi_event.lun = lun_id;
3758 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
3759 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
3760
3761 fc_host_post_vendor_event(shost, fc_get_event_number(),
3762 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
3763
3764 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
3765 FCP_LUN_RESET);
3766
3767 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3768 "0713 SCSI layer issued Device Reset (%d, %d) "
3769 "return x%x\n", tgt_id, lun_id, status);
3770
dea31012005-04-17 16:05:31 -05003771 /*
James Smartbbb9d182009-06-10 17:23:16 -04003772 * We have to clean up i/o as : they may be orphaned by the TMF;
3773 * or if the TMF failed, they may be in an indeterminate state.
3774 * So, continue on.
3775 * We will report success if all the i/o aborts successfully.
dea31012005-04-17 16:05:31 -05003776 */
James Smartbbb9d182009-06-10 17:23:16 -04003777 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
3778 LPFC_CTX_LUN);
3779 return status;
3780}
3781
3782/**
3783 * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
3784 * @cmnd: Pointer to scsi_cmnd data structure.
3785 *
3786 * This routine does a target reset by sending a TARGET_RESET task management
3787 * command.
3788 *
3789 * Return code :
3790 * 0x2003 - Error
3791 * 0x2002 - Success
3792 **/
3793static int
3794lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
3795{
3796 struct Scsi_Host *shost = cmnd->device->host;
3797 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3798 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05003799 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04003800 unsigned tgt_id = cmnd->device->id;
3801 unsigned int lun_id = cmnd->device->lun;
3802 struct lpfc_scsi_event_header scsi_event;
3803 int status;
3804
James Smart1c6f4ef52009-11-18 15:40:49 -05003805 if (!rdata) {
3806 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3807 "0799 Target Reset rport failure: rdata x%p\n", rdata);
3808 return FAILED;
3809 }
3810 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04003811 status = fc_block_scsi_eh(cmnd);
3812 if (status)
3813 return status;
James Smartbbb9d182009-06-10 17:23:16 -04003814
3815 status = lpfc_chk_tgt_mapped(vport, cmnd);
3816 if (status == FAILED) {
3817 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3818 "0722 Target Reset rport failure: rdata x%p\n", rdata);
3819 return FAILED;
dea31012005-04-17 16:05:31 -05003820 }
James Smartea2151b2008-09-07 11:52:10 -04003821
3822 scsi_event.event_type = FC_REG_SCSI_EVENT;
3823 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
3824 scsi_event.lun = 0;
3825 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
3826 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
3827
James Smartbbb9d182009-06-10 17:23:16 -04003828 fc_host_post_vendor_event(shost, fc_get_event_number(),
3829 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -04003830
James Smartbbb9d182009-06-10 17:23:16 -04003831 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
3832 FCP_TARGET_RESET);
dea31012005-04-17 16:05:31 -05003833
James Smarte8b62012007-08-02 11:10:09 -04003834 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smartbbb9d182009-06-10 17:23:16 -04003835 "0723 SCSI layer issued Target Reset (%d, %d) "
3836 "return x%x\n", tgt_id, lun_id, status);
3837
3838 /*
3839 * We have to clean up i/o as : they may be orphaned by the TMF;
3840 * or if the TMF failed, they may be in an indeterminate state.
3841 * So, continue on.
3842 * We will report success if all the i/o aborts successfully.
3843 */
3844 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
3845 LPFC_CTX_TGT);
3846 return status;
dea31012005-04-17 16:05:31 -05003847}
3848
James Smart9bad7672008-12-04 22:39:02 -05003849/**
James Smart3621a712009-04-06 18:47:14 -04003850 * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05003851 * @cmnd: Pointer to scsi_cmnd data structure.
3852 *
James Smartbbb9d182009-06-10 17:23:16 -04003853 * This routine does target reset to all targets on @cmnd->device->host.
3854 * This emulates Parallel SCSI Bus Reset Semantics.
James Smart9bad7672008-12-04 22:39:02 -05003855 *
James Smartbbb9d182009-06-10 17:23:16 -04003856 * Return code :
3857 * 0x2003 - Error
3858 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05003859 **/
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04003860static int
James Smart7054a602007-04-25 09:52:34 -04003861lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05003862{
James Smart2e0fef82007-06-17 19:56:36 -05003863 struct Scsi_Host *shost = cmnd->device->host;
3864 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05003865 struct lpfc_nodelist *ndlp = NULL;
James Smartea2151b2008-09-07 11:52:10 -04003866 struct lpfc_scsi_event_header scsi_event;
James Smartbbb9d182009-06-10 17:23:16 -04003867 int match;
3868 int ret = SUCCESS, status, i;
James Smartea2151b2008-09-07 11:52:10 -04003869
3870 scsi_event.event_type = FC_REG_SCSI_EVENT;
3871 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
3872 scsi_event.lun = 0;
3873 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
3874 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
3875
James Smartbbb9d182009-06-10 17:23:16 -04003876 fc_host_post_vendor_event(shost, fc_get_event_number(),
3877 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
dea31012005-04-17 16:05:31 -05003878
James Smartbf086112011-08-21 21:48:13 -04003879 status = fc_block_scsi_eh(cmnd);
3880 if (status)
3881 return status;
James Smartbbb9d182009-06-10 17:23:16 -04003882
dea31012005-04-17 16:05:31 -05003883 /*
3884 * Since the driver manages a single bus device, reset all
3885 * targets known to the driver. Should any target reset
3886 * fail, this routine returns failure to the midlayer.
3887 */
James Smarte17da182006-07-06 15:49:25 -04003888 for (i = 0; i < LPFC_MAX_TARGET; i++) {
James Smart685f0bf2007-04-25 09:53:08 -04003889 /* Search for mapped node by target ID */
dea31012005-04-17 16:05:31 -05003890 match = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003891 spin_lock_irq(shost->host_lock);
3892 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05003893 if (!NLP_CHK_NODE_ACT(ndlp))
3894 continue;
James Smart685f0bf2007-04-25 09:53:08 -04003895 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
James Smart915caaa2008-06-14 22:52:38 -04003896 ndlp->nlp_sid == i &&
James Smart685f0bf2007-04-25 09:53:08 -04003897 ndlp->rport) {
dea31012005-04-17 16:05:31 -05003898 match = 1;
3899 break;
3900 }
3901 }
James Smart2e0fef82007-06-17 19:56:36 -05003902 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003903 if (!match)
3904 continue;
James Smartbbb9d182009-06-10 17:23:16 -04003905
3906 status = lpfc_send_taskmgmt(vport, ndlp->rport->dd_data,
3907 i, 0, FCP_TARGET_RESET);
3908
3909 if (status != SUCCESS) {
James Smarte8b62012007-08-02 11:10:09 -04003910 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3911 "0700 Bus Reset on target %d failed\n",
3912 i);
James Smart915caaa2008-06-14 22:52:38 -04003913 ret = FAILED;
dea31012005-04-17 16:05:31 -05003914 }
3915 }
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05003916 /*
James Smartbbb9d182009-06-10 17:23:16 -04003917 * We have to clean up i/o as : they may be orphaned by the TMFs
3918 * above; or if any of the TMFs failed, they may be in an
3919 * indeterminate state.
3920 * We will report success if all the i/o aborts successfully.
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05003921 */
James Smartbbb9d182009-06-10 17:23:16 -04003922
3923 status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
3924 if (status != SUCCESS)
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05003925 ret = FAILED;
James Smartbbb9d182009-06-10 17:23:16 -04003926
James Smarte8b62012007-08-02 11:10:09 -04003927 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3928 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea31012005-04-17 16:05:31 -05003929 return ret;
3930}
3931
James Smart9bad7672008-12-04 22:39:02 -05003932/**
James Smart3621a712009-04-06 18:47:14 -04003933 * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
James Smart9bad7672008-12-04 22:39:02 -05003934 * @sdev: Pointer to scsi_device.
3935 *
3936 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
3937 * globally available list of scsi buffers. This routine also makes sure scsi
3938 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
3939 * of scsi buffer exists for the lifetime of the driver.
3940 *
3941 * Return codes:
3942 * non-0 - Error
3943 * 0 - Success
3944 **/
dea31012005-04-17 16:05:31 -05003945static int
dea31012005-04-17 16:05:31 -05003946lpfc_slave_alloc(struct scsi_device *sdev)
3947{
James Smart2e0fef82007-06-17 19:56:36 -05003948 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
3949 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003950 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
James Smart3772a992009-05-22 14:50:54 -04003951 uint32_t total = 0;
dea31012005-04-17 16:05:31 -05003952 uint32_t num_to_alloc = 0;
James Smart3772a992009-05-22 14:50:54 -04003953 int num_allocated = 0;
James Smartd7c47992010-06-08 18:31:54 -04003954 uint32_t sdev_cnt;
dea31012005-04-17 16:05:31 -05003955
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003956 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05003957 return -ENXIO;
3958
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003959 sdev->hostdata = rport->dd_data;
James Smartd7c47992010-06-08 18:31:54 -04003960 sdev_cnt = atomic_inc_return(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05003961
3962 /*
3963 * Populate the cmds_per_lun count scsi_bufs into this host's globally
3964 * available list of scsi buffers. Don't allocate more than the
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04003965 * HBA limit conveyed to the midlayer via the host structure. The
3966 * formula accounts for the lun_queue_depth + error handlers + 1
3967 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea31012005-04-17 16:05:31 -05003968 */
3969 total = phba->total_scsi_bufs;
James Smart3de2a652007-08-02 11:09:59 -04003970 num_to_alloc = vport->cfg_lun_queue_depth + 2;
James Smart92d7f7b2007-06-17 19:56:38 -05003971
James Smartd7c47992010-06-08 18:31:54 -04003972 /* If allocated buffers are enough do nothing */
3973 if ((sdev_cnt * (vport->cfg_lun_queue_depth + 2)) < total)
3974 return 0;
3975
James Smart92d7f7b2007-06-17 19:56:38 -05003976 /* Allow some exchanges to be available always to complete discovery */
3977 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04003978 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3979 "0704 At limitation of %d preallocated "
3980 "command buffers\n", total);
dea31012005-04-17 16:05:31 -05003981 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05003982 /* Allow some exchanges to be available always to complete discovery */
3983 } else if (total + num_to_alloc >
3984 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04003985 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3986 "0705 Allocation request of %d "
3987 "command buffers will exceed max of %d. "
3988 "Reducing allocation request to %d.\n",
3989 num_to_alloc, phba->cfg_hba_queue_depth,
3990 (phba->cfg_hba_queue_depth - total));
dea31012005-04-17 16:05:31 -05003991 num_to_alloc = phba->cfg_hba_queue_depth - total;
3992 }
James Smart3772a992009-05-22 14:50:54 -04003993 num_allocated = lpfc_new_scsi_buf(vport, num_to_alloc);
3994 if (num_to_alloc != num_allocated) {
3995 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3996 "0708 Allocation request of %d "
3997 "command buffers did not succeed. "
3998 "Allocated %d buffers.\n",
3999 num_to_alloc, num_allocated);
dea31012005-04-17 16:05:31 -05004000 }
James Smart1c6f4ef52009-11-18 15:40:49 -05004001 if (num_allocated > 0)
4002 phba->total_scsi_bufs += num_allocated;
dea31012005-04-17 16:05:31 -05004003 return 0;
4004}
4005
James Smart9bad7672008-12-04 22:39:02 -05004006/**
James Smart3621a712009-04-06 18:47:14 -04004007 * lpfc_slave_configure - scsi_host_template slave_configure entry point
James Smart9bad7672008-12-04 22:39:02 -05004008 * @sdev: Pointer to scsi_device.
4009 *
4010 * This routine configures following items
4011 * - Tag command queuing support for @sdev if supported.
James Smart9bad7672008-12-04 22:39:02 -05004012 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
4013 *
4014 * Return codes:
4015 * 0 - Success
4016 **/
dea31012005-04-17 16:05:31 -05004017static int
4018lpfc_slave_configure(struct scsi_device *sdev)
4019{
James Smart2e0fef82007-06-17 19:56:36 -05004020 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
4021 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004022
4023 if (sdev->tagged_supported)
James Smart3de2a652007-08-02 11:09:59 -04004024 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05004025 else
James Smart3de2a652007-08-02 11:09:59 -04004026 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05004027
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004028 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004029 lpfc_sli_handle_fast_ring_event(phba,
4030 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004031 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4032 lpfc_poll_rearm_timer(phba);
4033 }
4034
dea31012005-04-17 16:05:31 -05004035 return 0;
4036}
4037
James Smart9bad7672008-12-04 22:39:02 -05004038/**
James Smart3621a712009-04-06 18:47:14 -04004039 * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
James Smart9bad7672008-12-04 22:39:02 -05004040 * @sdev: Pointer to scsi_device.
4041 *
4042 * This routine sets @sdev hostatdata filed to null.
4043 **/
dea31012005-04-17 16:05:31 -05004044static void
4045lpfc_slave_destroy(struct scsi_device *sdev)
4046{
James Smartd7c47992010-06-08 18:31:54 -04004047 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
4048 struct lpfc_hba *phba = vport->phba;
4049 atomic_dec(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05004050 sdev->hostdata = NULL;
4051 return;
4052}
4053
James Smart92d7f7b2007-06-17 19:56:38 -05004054
dea31012005-04-17 16:05:31 -05004055struct scsi_host_template lpfc_template = {
4056 .module = THIS_MODULE,
4057 .name = LPFC_DRIVER_NAME,
4058 .info = lpfc_info,
4059 .queuecommand = lpfc_queuecommand,
4060 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04004061 .eh_device_reset_handler = lpfc_device_reset_handler,
4062 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart7054a602007-04-25 09:52:34 -04004063 .eh_bus_reset_handler = lpfc_bus_reset_handler,
dea31012005-04-17 16:05:31 -05004064 .slave_alloc = lpfc_slave_alloc,
4065 .slave_configure = lpfc_slave_configure,
4066 .slave_destroy = lpfc_slave_destroy,
James Smart47a86172007-04-25 09:53:22 -04004067 .scan_finished = lpfc_scan_finished,
dea31012005-04-17 16:05:31 -05004068 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05004069 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
dea31012005-04-17 16:05:31 -05004070 .cmd_per_lun = LPFC_CMD_PER_LUN,
4071 .use_clustering = ENABLE_CLUSTERING,
James Smart2e0fef82007-06-17 19:56:36 -05004072 .shost_attrs = lpfc_hba_attrs,
James.Smart@Emulex.Com564b2962005-06-25 10:34:17 -04004073 .max_sectors = 0xFFFF,
James Smartf1c3b0f2009-07-19 10:01:32 -04004074 .vendor_id = LPFC_NL_VENDOR_ID,
James Smart5ffc2662009-11-18 15:39:44 -05004075 .change_queue_depth = lpfc_change_queue_depth,
dea31012005-04-17 16:05:31 -05004076};
James Smart3de2a652007-08-02 11:09:59 -04004077
4078struct scsi_host_template lpfc_vport_template = {
4079 .module = THIS_MODULE,
4080 .name = LPFC_DRIVER_NAME,
4081 .info = lpfc_info,
4082 .queuecommand = lpfc_queuecommand,
4083 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04004084 .eh_device_reset_handler = lpfc_device_reset_handler,
4085 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart3de2a652007-08-02 11:09:59 -04004086 .eh_bus_reset_handler = lpfc_bus_reset_handler,
4087 .slave_alloc = lpfc_slave_alloc,
4088 .slave_configure = lpfc_slave_configure,
4089 .slave_destroy = lpfc_slave_destroy,
4090 .scan_finished = lpfc_scan_finished,
4091 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05004092 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
James Smart3de2a652007-08-02 11:09:59 -04004093 .cmd_per_lun = LPFC_CMD_PER_LUN,
4094 .use_clustering = ENABLE_CLUSTERING,
4095 .shost_attrs = lpfc_vport_attrs,
4096 .max_sectors = 0xFFFF,
James Smart5ffc2662009-11-18 15:39:44 -05004097 .change_queue_depth = lpfc_change_queue_depth,
James Smart3de2a652007-08-02 11:09:59 -04004098};