blob: a226c053c0f4a210906f288198a308608012f0a0 [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 Smarte47c9092008-02-08 18:49:26 -05004 * Copyright (C) 2004-2008 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>
22#include <linux/interrupt.h>
James Smarta90f5682006-08-17 11:58:04 -040023#include <linux/delay.h>
James Smarte2a0a9d2008-12-04 22:40:02 -050024#include <asm/unaligned.h>
dea31012005-04-17 16:05:31 -050025
26#include <scsi/scsi.h>
27#include <scsi/scsi_device.h>
James Smarte2a0a9d2008-12-04 22:40:02 -050028#include <scsi/scsi_eh.h>
dea31012005-04-17 16:05:31 -050029#include <scsi/scsi_host.h>
30#include <scsi/scsi_tcq.h>
31#include <scsi/scsi_transport_fc.h>
32
33#include "lpfc_version.h"
34#include "lpfc_hw.h"
35#include "lpfc_sli.h"
James Smartea2151b2008-09-07 11:52:10 -040036#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050037#include "lpfc_disc.h"
38#include "lpfc_scsi.h"
39#include "lpfc.h"
40#include "lpfc_logmsg.h"
41#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050042#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050043
44#define LPFC_RESET_WAIT 2
45#define LPFC_ABORT_WAIT 2
46
James Smarte2a0a9d2008-12-04 22:40:02 -050047int _dump_buf_done;
48
49static char *dif_op_str[] = {
50 "SCSI_PROT_NORMAL",
51 "SCSI_PROT_READ_INSERT",
52 "SCSI_PROT_WRITE_STRIP",
53 "SCSI_PROT_READ_STRIP",
54 "SCSI_PROT_WRITE_INSERT",
55 "SCSI_PROT_READ_PASS",
56 "SCSI_PROT_WRITE_PASS",
57 "SCSI_PROT_READ_CONVERT",
58 "SCSI_PROT_WRITE_CONVERT"
59};
60
61static void
62lpfc_debug_save_data(struct scsi_cmnd *cmnd)
63{
64 void *src, *dst;
65 struct scatterlist *sgde = scsi_sglist(cmnd);
66
67 if (!_dump_buf_data) {
68 printk(KERN_ERR "BLKGRD ERROR %s _dump_buf_data is NULL\n",
69 __func__);
70 return;
71 }
72
73
74 if (!sgde) {
75 printk(KERN_ERR "BLKGRD ERROR: data scatterlist is null\n");
76 return;
77 }
78
79 dst = (void *) _dump_buf_data;
80 while (sgde) {
81 src = sg_virt(sgde);
82 memcpy(dst, src, sgde->length);
83 dst += sgde->length;
84 sgde = sg_next(sgde);
85 }
86}
87
88static void
89lpfc_debug_save_dif(struct scsi_cmnd *cmnd)
90{
91 void *src, *dst;
92 struct scatterlist *sgde = scsi_prot_sglist(cmnd);
93
94 if (!_dump_buf_dif) {
95 printk(KERN_ERR "BLKGRD ERROR %s _dump_buf_data is NULL\n",
96 __func__);
97 return;
98 }
99
100 if (!sgde) {
101 printk(KERN_ERR "BLKGRD ERROR: prot scatterlist is null\n");
102 return;
103 }
104
105 dst = _dump_buf_dif;
106 while (sgde) {
107 src = sg_virt(sgde);
108 memcpy(dst, src, sgde->length);
109 dst += sgde->length;
110 sgde = sg_next(sgde);
111 }
112}
113
James Smartea2151b2008-09-07 11:52:10 -0400114/**
James Smart3621a712009-04-06 18:47:14 -0400115 * lpfc_update_stats - Update statistical data for the command completion
James Smartea2151b2008-09-07 11:52:10 -0400116 * @phba: Pointer to HBA object.
117 * @lpfc_cmd: lpfc scsi command object pointer.
118 *
119 * This function is called when there is a command completion and this
120 * function updates the statistical data for the command completion.
121 **/
122static void
123lpfc_update_stats(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
124{
125 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
126 struct lpfc_nodelist *pnode = rdata->pnode;
127 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
128 unsigned long flags;
129 struct Scsi_Host *shost = cmd->device->host;
130 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
131 unsigned long latency;
132 int i;
133
134 if (cmd->result)
135 return;
136
James Smart9f1e1b52008-12-04 22:39:40 -0500137 latency = jiffies_to_msecs((long)jiffies - (long)lpfc_cmd->start_time);
138
James Smartea2151b2008-09-07 11:52:10 -0400139 spin_lock_irqsave(shost->host_lock, flags);
140 if (!vport->stat_data_enabled ||
141 vport->stat_data_blocked ||
142 !pnode->lat_data ||
143 (phba->bucket_type == LPFC_NO_BUCKET)) {
144 spin_unlock_irqrestore(shost->host_lock, flags);
145 return;
146 }
James Smartea2151b2008-09-07 11:52:10 -0400147
148 if (phba->bucket_type == LPFC_LINEAR_BUCKET) {
149 i = (latency + phba->bucket_step - 1 - phba->bucket_base)/
150 phba->bucket_step;
James Smart9f1e1b52008-12-04 22:39:40 -0500151 /* check array subscript bounds */
152 if (i < 0)
153 i = 0;
154 else if (i >= LPFC_MAX_BUCKET_COUNT)
155 i = LPFC_MAX_BUCKET_COUNT - 1;
James Smartea2151b2008-09-07 11:52:10 -0400156 } else {
157 for (i = 0; i < LPFC_MAX_BUCKET_COUNT-1; i++)
158 if (latency <= (phba->bucket_base +
159 ((1<<i)*phba->bucket_step)))
160 break;
161 }
162
163 pnode->lat_data[i].cmd_count++;
164 spin_unlock_irqrestore(shost->host_lock, flags);
165}
166
James Smartea2151b2008-09-07 11:52:10 -0400167/**
James Smart3621a712009-04-06 18:47:14 -0400168 * lpfc_send_sdev_queuedepth_change_event - Posts a queuedepth change event
James Smartea2151b2008-09-07 11:52:10 -0400169 * @phba: Pointer to HBA context object.
170 * @vport: Pointer to vport object.
171 * @ndlp: Pointer to FC node associated with the target.
172 * @lun: Lun number of the scsi device.
173 * @old_val: Old value of the queue depth.
174 * @new_val: New value of the queue depth.
175 *
176 * This function sends an event to the mgmt application indicating
177 * there is a change in the scsi device queue depth.
178 **/
179static void
180lpfc_send_sdev_queuedepth_change_event(struct lpfc_hba *phba,
181 struct lpfc_vport *vport,
182 struct lpfc_nodelist *ndlp,
183 uint32_t lun,
184 uint32_t old_val,
185 uint32_t new_val)
186{
187 struct lpfc_fast_path_event *fast_path_evt;
188 unsigned long flags;
189
190 fast_path_evt = lpfc_alloc_fast_evt(phba);
191 if (!fast_path_evt)
192 return;
193
194 fast_path_evt->un.queue_depth_evt.scsi_event.event_type =
195 FC_REG_SCSI_EVENT;
196 fast_path_evt->un.queue_depth_evt.scsi_event.subcategory =
197 LPFC_EVENT_VARQUEDEPTH;
198
199 /* Report all luns with change in queue depth */
200 fast_path_evt->un.queue_depth_evt.scsi_event.lun = lun;
201 if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
202 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwpn,
203 &ndlp->nlp_portname, sizeof(struct lpfc_name));
204 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwnn,
205 &ndlp->nlp_nodename, sizeof(struct lpfc_name));
206 }
207
208 fast_path_evt->un.queue_depth_evt.oldval = old_val;
209 fast_path_evt->un.queue_depth_evt.newval = new_val;
210 fast_path_evt->vport = vport;
211
212 fast_path_evt->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
213 spin_lock_irqsave(&phba->hbalock, flags);
214 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
215 spin_unlock_irqrestore(&phba->hbalock, flags);
216 lpfc_worker_wake_up(phba);
217
218 return;
219}
220
James Smart9bad7672008-12-04 22:39:02 -0500221/**
James Smart3621a712009-04-06 18:47:14 -0400222 * lpfc_rampdown_queue_depth - Post RAMP_DOWN_QUEUE event to worker thread
James Smart9bad7672008-12-04 22:39:02 -0500223 * @phba: The Hba for which this call is being executed.
224 *
225 * This routine is called when there is resource error in driver or firmware.
226 * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
227 * posts at most 1 event each second. This routine wakes up worker thread of
228 * @phba to process WORKER_RAM_DOWN_EVENT event.
229 *
230 * This routine should be called with no lock held.
231 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500232void
James Smarteaf15d52008-12-04 22:39:29 -0500233lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
James Smart92d7f7b2007-06-17 19:56:38 -0500234{
235 unsigned long flags;
James Smart5e9d9b82008-06-14 22:52:53 -0400236 uint32_t evt_posted;
James Smart92d7f7b2007-06-17 19:56:38 -0500237
238 spin_lock_irqsave(&phba->hbalock, flags);
239 atomic_inc(&phba->num_rsrc_err);
240 phba->last_rsrc_error_time = jiffies;
241
242 if ((phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL) > jiffies) {
243 spin_unlock_irqrestore(&phba->hbalock, flags);
244 return;
245 }
246
247 phba->last_ramp_down_time = jiffies;
248
249 spin_unlock_irqrestore(&phba->hbalock, flags);
250
251 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400252 evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
253 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500254 phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500255 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
256
James Smart5e9d9b82008-06-14 22:52:53 -0400257 if (!evt_posted)
258 lpfc_worker_wake_up(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500259 return;
260}
261
James Smart9bad7672008-12-04 22:39:02 -0500262/**
James Smart3621a712009-04-06 18:47:14 -0400263 * lpfc_rampup_queue_depth - Post RAMP_UP_QUEUE event for worker thread
James Smart9bad7672008-12-04 22:39:02 -0500264 * @phba: The Hba for which this call is being executed.
265 *
266 * This routine post WORKER_RAMP_UP_QUEUE event for @phba vport. This routine
267 * post at most 1 event every 5 minute after last_ramp_up_time or
268 * last_rsrc_error_time. This routine wakes up worker thread of @phba
269 * to process WORKER_RAM_DOWN_EVENT event.
270 *
271 * This routine should be called with no lock held.
272 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500273static inline void
James Smart3de2a652007-08-02 11:09:59 -0400274lpfc_rampup_queue_depth(struct lpfc_vport *vport,
James Smarta257bf92009-04-06 18:48:10 -0400275 uint32_t queue_depth)
James Smart92d7f7b2007-06-17 19:56:38 -0500276{
277 unsigned long flags;
James Smart3de2a652007-08-02 11:09:59 -0400278 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -0400279 uint32_t evt_posted;
James Smart92d7f7b2007-06-17 19:56:38 -0500280 atomic_inc(&phba->num_cmd_success);
281
James Smarta257bf92009-04-06 18:48:10 -0400282 if (vport->cfg_lun_queue_depth <= queue_depth)
James Smart92d7f7b2007-06-17 19:56:38 -0500283 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500284 spin_lock_irqsave(&phba->hbalock, flags);
285 if (((phba->last_ramp_up_time + QUEUE_RAMP_UP_INTERVAL) > jiffies) ||
286 ((phba->last_rsrc_error_time + QUEUE_RAMP_UP_INTERVAL ) > jiffies)) {
287 spin_unlock_irqrestore(&phba->hbalock, flags);
288 return;
289 }
James Smart92d7f7b2007-06-17 19:56:38 -0500290 phba->last_ramp_up_time = jiffies;
291 spin_unlock_irqrestore(&phba->hbalock, flags);
292
293 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400294 evt_posted = phba->pport->work_port_events & WORKER_RAMP_UP_QUEUE;
295 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500296 phba->pport->work_port_events |= WORKER_RAMP_UP_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500297 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
298
James Smart5e9d9b82008-06-14 22:52:53 -0400299 if (!evt_posted)
300 lpfc_worker_wake_up(phba);
301 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500302}
303
James Smart9bad7672008-12-04 22:39:02 -0500304/**
James Smart3621a712009-04-06 18:47:14 -0400305 * lpfc_ramp_down_queue_handler - WORKER_RAMP_DOWN_QUEUE event handler
James Smart9bad7672008-12-04 22:39:02 -0500306 * @phba: The Hba for which this call is being executed.
307 *
308 * This routine is called to process WORKER_RAMP_DOWN_QUEUE event for worker
309 * thread.This routine reduces queue depth for all scsi device on each vport
310 * associated with @phba.
311 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500312void
313lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
314{
James Smart549e55c2007-08-02 11:09:51 -0400315 struct lpfc_vport **vports;
316 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500317 struct scsi_device *sdev;
James Smartea2151b2008-09-07 11:52:10 -0400318 unsigned long new_queue_depth, old_queue_depth;
James Smart92d7f7b2007-06-17 19:56:38 -0500319 unsigned long num_rsrc_err, num_cmd_success;
James Smart549e55c2007-08-02 11:09:51 -0400320 int i;
James Smartea2151b2008-09-07 11:52:10 -0400321 struct lpfc_rport_data *rdata;
James Smart92d7f7b2007-06-17 19:56:38 -0500322
323 num_rsrc_err = atomic_read(&phba->num_rsrc_err);
324 num_cmd_success = atomic_read(&phba->num_cmd_success);
325
James Smart549e55c2007-08-02 11:09:51 -0400326 vports = lpfc_create_vport_work_array(phba);
327 if (vports != NULL)
James Smart09372822008-01-11 01:52:54 -0500328 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400329 shost = lpfc_shost_from_vport(vports[i]);
330 shost_for_each_device(sdev, shost) {
James Smart92d7f7b2007-06-17 19:56:38 -0500331 new_queue_depth =
James Smart549e55c2007-08-02 11:09:51 -0400332 sdev->queue_depth * num_rsrc_err /
333 (num_rsrc_err + num_cmd_success);
334 if (!new_queue_depth)
335 new_queue_depth = sdev->queue_depth - 1;
336 else
337 new_queue_depth = sdev->queue_depth -
338 new_queue_depth;
James Smartea2151b2008-09-07 11:52:10 -0400339 old_queue_depth = sdev->queue_depth;
James Smart549e55c2007-08-02 11:09:51 -0400340 if (sdev->ordered_tags)
341 scsi_adjust_queue_depth(sdev,
342 MSG_ORDERED_TAG,
343 new_queue_depth);
344 else
345 scsi_adjust_queue_depth(sdev,
346 MSG_SIMPLE_TAG,
347 new_queue_depth);
James Smartea2151b2008-09-07 11:52:10 -0400348 rdata = sdev->hostdata;
349 if (rdata)
350 lpfc_send_sdev_queuedepth_change_event(
351 phba, vports[i],
352 rdata->pnode,
353 sdev->lun, old_queue_depth,
354 new_queue_depth);
James Smart549e55c2007-08-02 11:09:51 -0400355 }
James Smart92d7f7b2007-06-17 19:56:38 -0500356 }
James Smart09372822008-01-11 01:52:54 -0500357 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500358 atomic_set(&phba->num_rsrc_err, 0);
359 atomic_set(&phba->num_cmd_success, 0);
360}
361
James Smart9bad7672008-12-04 22:39:02 -0500362/**
James Smart3621a712009-04-06 18:47:14 -0400363 * lpfc_ramp_up_queue_handler - WORKER_RAMP_UP_QUEUE event handler
James Smart9bad7672008-12-04 22:39:02 -0500364 * @phba: The Hba for which this call is being executed.
365 *
366 * This routine is called to process WORKER_RAMP_UP_QUEUE event for worker
367 * thread.This routine increases queue depth for all scsi device on each vport
368 * associated with @phba by 1. This routine also sets @phba num_rsrc_err and
369 * num_cmd_success to zero.
370 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500371void
372lpfc_ramp_up_queue_handler(struct lpfc_hba *phba)
373{
James Smart549e55c2007-08-02 11:09:51 -0400374 struct lpfc_vport **vports;
375 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500376 struct scsi_device *sdev;
James Smart549e55c2007-08-02 11:09:51 -0400377 int i;
James Smartea2151b2008-09-07 11:52:10 -0400378 struct lpfc_rport_data *rdata;
James Smart92d7f7b2007-06-17 19:56:38 -0500379
James Smart549e55c2007-08-02 11:09:51 -0400380 vports = lpfc_create_vport_work_array(phba);
381 if (vports != NULL)
James Smart09372822008-01-11 01:52:54 -0500382 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400383 shost = lpfc_shost_from_vport(vports[i]);
384 shost_for_each_device(sdev, shost) {
James Smart97eab632008-04-07 10:16:05 -0400385 if (vports[i]->cfg_lun_queue_depth <=
386 sdev->queue_depth)
387 continue;
James Smart549e55c2007-08-02 11:09:51 -0400388 if (sdev->ordered_tags)
389 scsi_adjust_queue_depth(sdev,
390 MSG_ORDERED_TAG,
391 sdev->queue_depth+1);
392 else
393 scsi_adjust_queue_depth(sdev,
394 MSG_SIMPLE_TAG,
395 sdev->queue_depth+1);
James Smartea2151b2008-09-07 11:52:10 -0400396 rdata = sdev->hostdata;
397 if (rdata)
398 lpfc_send_sdev_queuedepth_change_event(
399 phba, vports[i],
400 rdata->pnode,
401 sdev->lun,
402 sdev->queue_depth - 1,
403 sdev->queue_depth);
James Smart549e55c2007-08-02 11:09:51 -0400404 }
James Smart92d7f7b2007-06-17 19:56:38 -0500405 }
James Smart09372822008-01-11 01:52:54 -0500406 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500407 atomic_set(&phba->num_rsrc_err, 0);
408 atomic_set(&phba->num_cmd_success, 0);
409}
410
James Smarta8e497d2008-08-24 21:50:11 -0400411/**
James Smart3621a712009-04-06 18:47:14 -0400412 * lpfc_scsi_dev_block - set all scsi hosts to block state
James Smarta8e497d2008-08-24 21:50:11 -0400413 * @phba: Pointer to HBA context object.
414 *
415 * This function walks vport list and set each SCSI host to block state
416 * by invoking fc_remote_port_delete() routine. This function is invoked
417 * with EEH when device's PCI slot has been permanently disabled.
418 **/
419void
420lpfc_scsi_dev_block(struct lpfc_hba *phba)
421{
422 struct lpfc_vport **vports;
423 struct Scsi_Host *shost;
424 struct scsi_device *sdev;
425 struct fc_rport *rport;
426 int i;
427
428 vports = lpfc_create_vport_work_array(phba);
429 if (vports != NULL)
430 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
431 shost = lpfc_shost_from_vport(vports[i]);
432 shost_for_each_device(sdev, shost) {
433 rport = starget_to_rport(scsi_target(sdev));
434 fc_remote_port_delete(rport);
435 }
436 }
437 lpfc_destroy_vport_work_array(phba, vports);
438}
439
James Smart9bad7672008-12-04 22:39:02 -0500440/**
James Smart3772a992009-05-22 14:50:54 -0400441 * lpfc_new_scsi_buf_s3 - Scsi buffer allocator for HBA with SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -0500442 * @vport: The virtual port for which this call being executed.
James Smart3772a992009-05-22 14:50:54 -0400443 * @num_to_allocate: The requested number of buffers to allocate.
James Smart9bad7672008-12-04 22:39:02 -0500444 *
James Smart3772a992009-05-22 14:50:54 -0400445 * This routine allocates a scsi buffer for device with SLI-3 interface spec,
446 * the scsi buffer contains all the necessary information needed to initiate
447 * a SCSI I/O. The non-DMAable buffer region contains information to build
448 * the IOCB. The DMAable region contains memory for the FCP CMND, FCP RSP,
449 * and the initial BPL. In addition to allocating memory, the FCP CMND and
450 * FCP RSP BDEs are setup in the BPL and the BPL BDE is setup in the IOCB.
James Smart9bad7672008-12-04 22:39:02 -0500451 *
452 * Return codes:
James Smart3772a992009-05-22 14:50:54 -0400453 * int - number of scsi buffers that were allocated.
454 * 0 = failure, less than num_to_alloc is a partial failure.
James Smart9bad7672008-12-04 22:39:02 -0500455 **/
James Smart3772a992009-05-22 14:50:54 -0400456static int
457lpfc_new_scsi_buf_s3(struct lpfc_vport *vport, int num_to_alloc)
dea31012005-04-17 16:05:31 -0500458{
James Smart2e0fef82007-06-17 19:56:36 -0500459 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500460 struct lpfc_scsi_buf *psb;
461 struct ulp_bde64 *bpl;
462 IOCB_t *iocb;
James Smart34b02dc2008-08-24 21:49:55 -0400463 dma_addr_t pdma_phys_fcp_cmd;
464 dma_addr_t pdma_phys_fcp_rsp;
465 dma_addr_t pdma_phys_bpl;
James Bottomley604a3e32005-10-29 10:28:33 -0500466 uint16_t iotag;
James Smart3772a992009-05-22 14:50:54 -0400467 int bcnt;
dea31012005-04-17 16:05:31 -0500468
James Smart3772a992009-05-22 14:50:54 -0400469 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
470 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
471 if (!psb)
472 break;
dea31012005-04-17 16:05:31 -0500473
James Smart3772a992009-05-22 14:50:54 -0400474 /*
475 * Get memory from the pci pool to map the virt space to pci
476 * bus space for an I/O. The DMA buffer includes space for the
477 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
478 * necessary to support the sg_tablesize.
479 */
480 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
481 GFP_KERNEL, &psb->dma_handle);
482 if (!psb->data) {
483 kfree(psb);
484 break;
485 }
dea31012005-04-17 16:05:31 -0500486
James Smart3772a992009-05-22 14:50:54 -0400487 /* Initialize virtual ptrs to dma_buf region. */
488 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
dea31012005-04-17 16:05:31 -0500489
James Smart3772a992009-05-22 14:50:54 -0400490 /* Allocate iotag for psb->cur_iocbq. */
491 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
492 if (iotag == 0) {
493 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
494 psb->data, psb->dma_handle);
495 kfree(psb);
496 break;
497 }
498 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Bottomley604a3e32005-10-29 10:28:33 -0500499
James Smart3772a992009-05-22 14:50:54 -0400500 psb->fcp_cmnd = psb->data;
501 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
502 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
James Smart34b02dc2008-08-24 21:49:55 -0400503 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500504
James Smart3772a992009-05-22 14:50:54 -0400505 /* Initialize local short-hand pointers. */
506 bpl = psb->fcp_bpl;
507 pdma_phys_fcp_cmd = psb->dma_handle;
508 pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
509 pdma_phys_bpl = psb->dma_handle + sizeof(struct fcp_cmnd) +
510 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500511
James Smart3772a992009-05-22 14:50:54 -0400512 /*
513 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
514 * are sg list bdes. Initialize the first two and leave the
515 * rest for queuecommand.
516 */
517 bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
518 bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
519 bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
520 bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
521 bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
dea31012005-04-17 16:05:31 -0500522
James Smart3772a992009-05-22 14:50:54 -0400523 /* Setup the physical region for the FCP RSP */
524 bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
525 bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
526 bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
527 bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
528 bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
529
530 /*
531 * Since the IOCB for the FCP I/O is built into this
532 * lpfc_scsi_buf, initialize it with all known data now.
533 */
534 iocb = &psb->cur_iocbq.iocb;
535 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
536 if ((phba->sli_rev == 3) &&
537 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
538 /* fill in immediate fcp command BDE */
539 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
540 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
541 iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
542 unsli3.fcp_ext.icd);
543 iocb->un.fcpi64.bdl.addrHigh = 0;
544 iocb->ulpBdeCount = 0;
545 iocb->ulpLe = 0;
546 /* fill in responce BDE */
547 iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags =
548 BUFF_TYPE_BDE_64;
549 iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
550 sizeof(struct fcp_rsp);
551 iocb->unsli3.fcp_ext.rbde.addrLow =
552 putPaddrLow(pdma_phys_fcp_rsp);
553 iocb->unsli3.fcp_ext.rbde.addrHigh =
554 putPaddrHigh(pdma_phys_fcp_rsp);
555 } else {
556 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
557 iocb->un.fcpi64.bdl.bdeSize =
558 (2 * sizeof(struct ulp_bde64));
559 iocb->un.fcpi64.bdl.addrLow =
560 putPaddrLow(pdma_phys_bpl);
561 iocb->un.fcpi64.bdl.addrHigh =
562 putPaddrHigh(pdma_phys_bpl);
563 iocb->ulpBdeCount = 1;
564 iocb->ulpLe = 1;
565 }
566 iocb->ulpClass = CLASS3;
567 psb->status = IOSTAT_SUCCESS;
568
James Smart34b02dc2008-08-24 21:49:55 -0400569 }
dea31012005-04-17 16:05:31 -0500570
James Smart3772a992009-05-22 14:50:54 -0400571 return bcnt;
dea31012005-04-17 16:05:31 -0500572}
573
James Smart9bad7672008-12-04 22:39:02 -0500574/**
James Smart3772a992009-05-22 14:50:54 -0400575 * lpfc_new_scsi_buf - Wrapper funciton for scsi buffer allocator
576 * @vport: The virtual port for which this call being executed.
577 * @num_to_allocate: The requested number of buffers to allocate.
578 *
579 * This routine wraps the actual SCSI buffer allocator function pointer from
580 * the lpfc_hba struct.
581 *
582 * Return codes:
583 * int - number of scsi buffers that were allocated.
584 * 0 = failure, less than num_to_alloc is a partial failure.
585 **/
586static inline int
587lpfc_new_scsi_buf(struct lpfc_vport *vport, int num_to_alloc)
588{
589 return vport->phba->lpfc_new_scsi_buf(vport, num_to_alloc);
590}
591
592/**
593 * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
594 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -0500595 *
596 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
597 * and returns to caller.
598 *
599 * Return codes:
600 * NULL - Error
601 * Pointer to lpfc_scsi_buf - Success
602 **/
Adrian Bunk455c53e2006-01-06 20:21:28 +0100603static struct lpfc_scsi_buf*
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500604lpfc_get_scsi_buf(struct lpfc_hba * phba)
dea31012005-04-17 16:05:31 -0500605{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400606 struct lpfc_scsi_buf * lpfc_cmd = NULL;
607 struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500608 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500609
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500610 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400611 list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
James Smart1dcb58e2007-04-25 09:51:30 -0400612 if (lpfc_cmd) {
613 lpfc_cmd->seg_cnt = 0;
614 lpfc_cmd->nonsg_phys = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -0500615 lpfc_cmd->prot_seg_cnt = 0;
James Smart1dcb58e2007-04-25 09:51:30 -0400616 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500617 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400618 return lpfc_cmd;
619}
620
James Smart9bad7672008-12-04 22:39:02 -0500621/**
James Smart3772a992009-05-22 14:50:54 -0400622 * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
James Smart9bad7672008-12-04 22:39:02 -0500623 * @phba: The Hba for which this call is being executed.
624 * @psb: The scsi buffer which is being released.
625 *
626 * This routine releases @psb scsi buffer by adding it to tail of @phba
627 * lpfc_scsi_buf_list list.
628 **/
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400629static void
James Smart3772a992009-05-22 14:50:54 -0400630lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400631{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500632 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500633
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500634 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400635 psb->pCmd = NULL;
dea31012005-04-17 16:05:31 -0500636 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500637 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
dea31012005-04-17 16:05:31 -0500638}
639
James Smart9bad7672008-12-04 22:39:02 -0500640/**
James Smart3772a992009-05-22 14:50:54 -0400641 * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
642 * @phba: The Hba for which this call is being executed.
643 * @psb: The scsi buffer which is being released.
644 *
645 * This routine releases @psb scsi buffer by adding it to tail of @phba
646 * lpfc_scsi_buf_list list.
647 **/
648static void
649lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
650{
651
652 phba->lpfc_release_scsi_buf(phba, psb);
653}
654
655/**
656 * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -0500657 * @phba: The Hba for which this call is being executed.
658 * @lpfc_cmd: The scsi buffer which is going to be mapped.
659 *
660 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
James Smart3772a992009-05-22 14:50:54 -0400661 * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
662 * through sg elements and format the bdea. This routine also initializes all
663 * IOCB fields which are dependent on scsi command request buffer.
James Smart9bad7672008-12-04 22:39:02 -0500664 *
665 * Return codes:
666 * 1 - Error
667 * 0 - Success
668 **/
dea31012005-04-17 16:05:31 -0500669static int
James Smart3772a992009-05-22 14:50:54 -0400670lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
dea31012005-04-17 16:05:31 -0500671{
672 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
673 struct scatterlist *sgel = NULL;
674 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
675 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
676 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart34b02dc2008-08-24 21:49:55 -0400677 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
dea31012005-04-17 16:05:31 -0500678 dma_addr_t physaddr;
James Smart34b02dc2008-08-24 21:49:55 -0400679 uint32_t num_bde = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500680 int nseg, datadir = scsi_cmnd->sc_data_direction;
dea31012005-04-17 16:05:31 -0500681
682 /*
683 * There are three possibilities here - use scatter-gather segment, use
684 * the single mapping, or neither. Start the lpfc command prep by
685 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
686 * data bde entry.
687 */
688 bpl += 2;
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -0700689 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -0500690 /*
691 * The driver stores the segment count returned from pci_map_sg
692 * because this a count of dma-mappings used to map the use_sg
693 * pages. They are not guaranteed to be the same for those
694 * architectures that implement an IOMMU.
695 */
dea31012005-04-17 16:05:31 -0500696
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -0700697 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
698 scsi_sg_count(scsi_cmnd), datadir);
699 if (unlikely(!nseg))
700 return 1;
701
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500702 lpfc_cmd->seg_cnt = nseg;
dea31012005-04-17 16:05:31 -0500703 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
704 printk(KERN_ERR "%s: Too many sg segments from "
James Smarte2a0a9d2008-12-04 22:40:02 -0500705 "dma_map_sg. Config %d, seg_cnt %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700706 __func__, phba->cfg_sg_seg_cnt,
dea31012005-04-17 16:05:31 -0500707 lpfc_cmd->seg_cnt);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500708 scsi_dma_unmap(scsi_cmnd);
dea31012005-04-17 16:05:31 -0500709 return 1;
710 }
711
712 /*
713 * The driver established a maximum scatter-gather segment count
714 * during probe that limits the number of sg elements in any
715 * single scsi command. Just run through the seg_cnt and format
716 * the bde's.
James Smart34b02dc2008-08-24 21:49:55 -0400717 * When using SLI-3 the driver will try to fit all the BDEs into
718 * the IOCB. If it can't then the BDEs get added to a BPL as it
719 * does for SLI-2 mode.
dea31012005-04-17 16:05:31 -0500720 */
James Smart34b02dc2008-08-24 21:49:55 -0400721 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
dea31012005-04-17 16:05:31 -0500722 physaddr = sg_dma_address(sgel);
James Smart34b02dc2008-08-24 21:49:55 -0400723 if (phba->sli_rev == 3 &&
James Smarte2a0a9d2008-12-04 22:40:02 -0500724 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
James Smart34b02dc2008-08-24 21:49:55 -0400725 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
726 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
727 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
728 data_bde->addrLow = putPaddrLow(physaddr);
729 data_bde->addrHigh = putPaddrHigh(physaddr);
730 data_bde++;
731 } else {
732 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
733 bpl->tus.f.bdeSize = sg_dma_len(sgel);
734 bpl->tus.w = le32_to_cpu(bpl->tus.w);
735 bpl->addrLow =
736 le32_to_cpu(putPaddrLow(physaddr));
737 bpl->addrHigh =
738 le32_to_cpu(putPaddrHigh(physaddr));
739 bpl++;
740 }
dea31012005-04-17 16:05:31 -0500741 }
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -0700742 }
dea31012005-04-17 16:05:31 -0500743
744 /*
745 * Finish initializing those IOCB fields that are dependent on the
James Smart34b02dc2008-08-24 21:49:55 -0400746 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
747 * explicitly reinitialized and for SLI-3 the extended bde count is
748 * explicitly reinitialized since all iocb memory resources are reused.
dea31012005-04-17 16:05:31 -0500749 */
James Smarte2a0a9d2008-12-04 22:40:02 -0500750 if (phba->sli_rev == 3 &&
751 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
James Smart34b02dc2008-08-24 21:49:55 -0400752 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
753 /*
754 * The extended IOCB format can only fit 3 BDE or a BPL.
755 * This I/O has more than 3 BDE so the 1st data bde will
756 * be a BPL that is filled in here.
757 */
758 physaddr = lpfc_cmd->dma_handle;
759 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
760 data_bde->tus.f.bdeSize = (num_bde *
761 sizeof(struct ulp_bde64));
762 physaddr += (sizeof(struct fcp_cmnd) +
763 sizeof(struct fcp_rsp) +
764 (2 * sizeof(struct ulp_bde64)));
765 data_bde->addrHigh = putPaddrHigh(physaddr);
766 data_bde->addrLow = putPaddrLow(physaddr);
767 /* ebde count includes the responce bde and data bpl */
768 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
769 } else {
770 /* ebde count includes the responce bde and data bdes */
771 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
772 }
773 } else {
774 iocb_cmd->un.fcpi64.bdl.bdeSize =
775 ((num_bde + 2) * sizeof(struct ulp_bde64));
776 }
James Smart09372822008-01-11 01:52:54 -0500777 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
James Smarte2a0a9d2008-12-04 22:40:02 -0500778
779 /*
780 * Due to difference in data length between DIF/non-DIF paths,
781 * we need to set word 4 of IOCB here
782 */
James Smarta257bf92009-04-06 18:48:10 -0400783 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
dea31012005-04-17 16:05:31 -0500784 return 0;
785}
786
James Smarte2a0a9d2008-12-04 22:40:02 -0500787/*
788 * Given a scsi cmnd, determine the BlockGuard profile to be used
789 * with the cmd
790 */
791static int
792lpfc_sc_to_sli_prof(struct scsi_cmnd *sc)
793{
794 uint8_t guard_type = scsi_host_get_guard(sc->device->host);
795 uint8_t ret_prof = LPFC_PROF_INVALID;
796
797 if (guard_type == SHOST_DIX_GUARD_IP) {
798 switch (scsi_get_prot_op(sc)) {
799 case SCSI_PROT_READ_INSERT:
800 case SCSI_PROT_WRITE_STRIP:
801 ret_prof = LPFC_PROF_AST2;
802 break;
803
804 case SCSI_PROT_READ_STRIP:
805 case SCSI_PROT_WRITE_INSERT:
806 ret_prof = LPFC_PROF_A1;
807 break;
808
809 case SCSI_PROT_READ_CONVERT:
810 case SCSI_PROT_WRITE_CONVERT:
811 ret_prof = LPFC_PROF_AST1;
812 break;
813
814 case SCSI_PROT_READ_PASS:
815 case SCSI_PROT_WRITE_PASS:
816 case SCSI_PROT_NORMAL:
817 default:
818 printk(KERN_ERR "Bad op/guard:%d/%d combination\n",
819 scsi_get_prot_op(sc), guard_type);
820 break;
821
822 }
823 } else if (guard_type == SHOST_DIX_GUARD_CRC) {
824 switch (scsi_get_prot_op(sc)) {
825 case SCSI_PROT_READ_STRIP:
826 case SCSI_PROT_WRITE_INSERT:
827 ret_prof = LPFC_PROF_A1;
828 break;
829
830 case SCSI_PROT_READ_PASS:
831 case SCSI_PROT_WRITE_PASS:
832 ret_prof = LPFC_PROF_C1;
833 break;
834
835 case SCSI_PROT_READ_CONVERT:
836 case SCSI_PROT_WRITE_CONVERT:
837 case SCSI_PROT_READ_INSERT:
838 case SCSI_PROT_WRITE_STRIP:
839 case SCSI_PROT_NORMAL:
840 default:
841 printk(KERN_ERR "Bad op/guard:%d/%d combination\n",
842 scsi_get_prot_op(sc), guard_type);
843 break;
844 }
845 } else {
846 /* unsupported format */
847 BUG();
848 }
849
850 return ret_prof;
851}
852
853struct scsi_dif_tuple {
854 __be16 guard_tag; /* Checksum */
855 __be16 app_tag; /* Opaque storage */
856 __be32 ref_tag; /* Target LBA or indirect LBA */
857};
858
859static inline unsigned
860lpfc_cmd_blksize(struct scsi_cmnd *sc)
861{
862 return sc->device->sector_size;
863}
864
865/**
866 * lpfc_get_cmd_dif_parms - Extract DIF parameters from SCSI command
867 * @sc: in: SCSI command
James Smart3621a712009-04-06 18:47:14 -0400868 * @apptagmask: out: app tag mask
869 * @apptagval: out: app tag value
870 * @reftag: out: ref tag (reference tag)
James Smarte2a0a9d2008-12-04 22:40:02 -0500871 *
872 * Description:
873 * Extract DIF paramters from the command if possible. Otherwise,
874 * use default paratmers.
875 *
876 **/
877static inline void
878lpfc_get_cmd_dif_parms(struct scsi_cmnd *sc, uint16_t *apptagmask,
879 uint16_t *apptagval, uint32_t *reftag)
880{
881 struct scsi_dif_tuple *spt;
882 unsigned char op = scsi_get_prot_op(sc);
883 unsigned int protcnt = scsi_prot_sg_count(sc);
884 static int cnt;
885
886 if (protcnt && (op == SCSI_PROT_WRITE_STRIP ||
887 op == SCSI_PROT_WRITE_PASS ||
888 op == SCSI_PROT_WRITE_CONVERT)) {
889
890 cnt++;
891 spt = page_address(sg_page(scsi_prot_sglist(sc))) +
892 scsi_prot_sglist(sc)[0].offset;
893 *apptagmask = 0;
894 *apptagval = 0;
895 *reftag = cpu_to_be32(spt->ref_tag);
896
897 } else {
898 /* SBC defines ref tag to be lower 32bits of LBA */
899 *reftag = (uint32_t) (0xffffffff & scsi_get_lba(sc));
900 *apptagmask = 0;
901 *apptagval = 0;
902 }
903}
904
905/*
906 * This function sets up buffer list for protection groups of
907 * type LPFC_PG_TYPE_NO_DIF
908 *
909 * This is usually used when the HBA is instructed to generate
910 * DIFs and insert them into data stream (or strip DIF from
911 * incoming data stream)
912 *
913 * The buffer list consists of just one protection group described
914 * below:
915 * +-------------------------+
916 * start of prot group --> | PDE_1 |
917 * +-------------------------+
918 * | Data BDE |
919 * +-------------------------+
920 * |more Data BDE's ... (opt)|
921 * +-------------------------+
922 *
923 * @sc: pointer to scsi command we're working on
924 * @bpl: pointer to buffer list for protection groups
925 * @datacnt: number of segments of data that have been dma mapped
926 *
927 * Note: Data s/g buffers have been dma mapped
928 */
929static int
930lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
931 struct ulp_bde64 *bpl, int datasegcnt)
932{
933 struct scatterlist *sgde = NULL; /* s/g data entry */
934 struct lpfc_pde *pde1 = NULL;
935 dma_addr_t physaddr;
936 int i = 0, num_bde = 0;
937 int datadir = sc->sc_data_direction;
938 int prof = LPFC_PROF_INVALID;
939 unsigned blksize;
940 uint32_t reftag;
941 uint16_t apptagmask, apptagval;
942
943 pde1 = (struct lpfc_pde *) bpl;
944 prof = lpfc_sc_to_sli_prof(sc);
945
946 if (prof == LPFC_PROF_INVALID)
947 goto out;
948
949 /* extract some info from the scsi command for PDE1*/
950 blksize = lpfc_cmd_blksize(sc);
951 lpfc_get_cmd_dif_parms(sc, &apptagmask, &apptagval, &reftag);
952
953 /* setup PDE1 with what we have */
954 lpfc_pde_set_bg_parms(pde1, LPFC_PDE1_DESCRIPTOR, prof, blksize,
955 BG_EC_STOP_ERR);
956 lpfc_pde_set_dif_parms(pde1, apptagmask, apptagval, reftag);
957
958 num_bde++;
959 bpl++;
960
961 /* assumption: caller has already run dma_map_sg on command data */
962 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
963 physaddr = sg_dma_address(sgde);
964 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
965 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
966 bpl->tus.f.bdeSize = sg_dma_len(sgde);
967 if (datadir == DMA_TO_DEVICE)
968 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
969 else
970 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
971 bpl->tus.w = le32_to_cpu(bpl->tus.w);
972 bpl++;
973 num_bde++;
974 }
975
976out:
977 return num_bde;
978}
979
980/*
981 * This function sets up buffer list for protection groups of
982 * type LPFC_PG_TYPE_DIF_BUF
983 *
984 * This is usually used when DIFs are in their own buffers,
985 * separate from the data. The HBA can then by instructed
986 * to place the DIFs in the outgoing stream. For read operations,
987 * The HBA could extract the DIFs and place it in DIF buffers.
988 *
989 * The buffer list for this type consists of one or more of the
990 * protection groups described below:
991 * +-------------------------+
992 * start of first prot group --> | PDE_1 |
993 * +-------------------------+
994 * | PDE_3 (Prot BDE) |
995 * +-------------------------+
996 * | Data BDE |
997 * +-------------------------+
998 * |more Data BDE's ... (opt)|
999 * +-------------------------+
1000 * start of new prot group --> | PDE_1 |
1001 * +-------------------------+
1002 * | ... |
1003 * +-------------------------+
1004 *
1005 * @sc: pointer to scsi command we're working on
1006 * @bpl: pointer to buffer list for protection groups
1007 * @datacnt: number of segments of data that have been dma mapped
1008 * @protcnt: number of segment of protection data that have been dma mapped
1009 *
1010 * Note: It is assumed that both data and protection s/g buffers have been
1011 * mapped for DMA
1012 */
1013static int
1014lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1015 struct ulp_bde64 *bpl, int datacnt, int protcnt)
1016{
1017 struct scatterlist *sgde = NULL; /* s/g data entry */
1018 struct scatterlist *sgpe = NULL; /* s/g prot entry */
1019 struct lpfc_pde *pde1 = NULL;
1020 struct ulp_bde64 *prot_bde = NULL;
1021 dma_addr_t dataphysaddr, protphysaddr;
1022 unsigned short curr_data = 0, curr_prot = 0;
1023 unsigned int split_offset, protgroup_len;
1024 unsigned int protgrp_blks, protgrp_bytes;
1025 unsigned int remainder, subtotal;
1026 int prof = LPFC_PROF_INVALID;
1027 int datadir = sc->sc_data_direction;
1028 unsigned char pgdone = 0, alldone = 0;
1029 unsigned blksize;
1030 uint32_t reftag;
1031 uint16_t apptagmask, apptagval;
1032 int num_bde = 0;
1033
1034 sgpe = scsi_prot_sglist(sc);
1035 sgde = scsi_sglist(sc);
1036
1037 if (!sgpe || !sgde) {
1038 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1039 "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
1040 sgpe, sgde);
1041 return 0;
1042 }
1043
1044 prof = lpfc_sc_to_sli_prof(sc);
1045 if (prof == LPFC_PROF_INVALID)
1046 goto out;
1047
1048 /* extract some info from the scsi command for PDE1*/
1049 blksize = lpfc_cmd_blksize(sc);
1050 lpfc_get_cmd_dif_parms(sc, &apptagmask, &apptagval, &reftag);
1051
1052 split_offset = 0;
1053 do {
1054 /* setup the first PDE_1 */
1055 pde1 = (struct lpfc_pde *) bpl;
1056
1057 lpfc_pde_set_bg_parms(pde1, LPFC_PDE1_DESCRIPTOR, prof, blksize,
1058 BG_EC_STOP_ERR);
1059 lpfc_pde_set_dif_parms(pde1, apptagmask, apptagval, reftag);
1060
1061 num_bde++;
1062 bpl++;
1063
1064 /* setup the first BDE that points to protection buffer */
1065 prot_bde = (struct ulp_bde64 *) bpl;
1066 protphysaddr = sg_dma_address(sgpe);
1067 prot_bde->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
1068 prot_bde->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
1069 protgroup_len = sg_dma_len(sgpe);
1070
1071
1072 /* must be integer multiple of the DIF block length */
1073 BUG_ON(protgroup_len % 8);
1074
1075 protgrp_blks = protgroup_len / 8;
1076 protgrp_bytes = protgrp_blks * blksize;
1077
1078 prot_bde->tus.f.bdeSize = protgroup_len;
1079 if (datadir == DMA_TO_DEVICE)
1080 prot_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1081 else
1082 prot_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1083 prot_bde->tus.w = le32_to_cpu(bpl->tus.w);
1084
1085 curr_prot++;
1086 num_bde++;
1087
1088 /* setup BDE's for data blocks associated with DIF data */
1089 pgdone = 0;
1090 subtotal = 0; /* total bytes processed for current prot grp */
1091 while (!pgdone) {
1092 if (!sgde) {
1093 printk(KERN_ERR "%s Invalid data segment\n",
1094 __func__);
1095 return 0;
1096 }
1097 bpl++;
1098 dataphysaddr = sg_dma_address(sgde) + split_offset;
1099 bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
1100 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
1101
1102 remainder = sg_dma_len(sgde) - split_offset;
1103
1104 if ((subtotal + remainder) <= protgrp_bytes) {
1105 /* we can use this whole buffer */
1106 bpl->tus.f.bdeSize = remainder;
1107 split_offset = 0;
1108
1109 if ((subtotal + remainder) == protgrp_bytes)
1110 pgdone = 1;
1111 } else {
1112 /* must split this buffer with next prot grp */
1113 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
1114 split_offset += bpl->tus.f.bdeSize;
1115 }
1116
1117 subtotal += bpl->tus.f.bdeSize;
1118
1119 if (datadir == DMA_TO_DEVICE)
1120 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1121 else
1122 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1123 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1124
1125 num_bde++;
1126 curr_data++;
1127
1128 if (split_offset)
1129 break;
1130
1131 /* Move to the next s/g segment if possible */
1132 sgde = sg_next(sgde);
1133 }
1134
1135 /* are we done ? */
1136 if (curr_prot == protcnt) {
1137 alldone = 1;
1138 } else if (curr_prot < protcnt) {
1139 /* advance to next prot buffer */
1140 sgpe = sg_next(sgpe);
1141 bpl++;
1142
1143 /* update the reference tag */
1144 reftag += protgrp_blks;
1145 } else {
1146 /* if we're here, we have a bug */
1147 printk(KERN_ERR "BLKGRD: bug in %s\n", __func__);
1148 }
1149
1150 } while (!alldone);
1151
1152out:
1153
1154
1155 return num_bde;
1156}
1157/*
1158 * Given a SCSI command that supports DIF, determine composition of protection
1159 * groups involved in setting up buffer lists
1160 *
1161 * Returns:
1162 * for DIF (for both read and write)
1163 * */
1164static int
1165lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
1166{
1167 int ret = LPFC_PG_TYPE_INVALID;
1168 unsigned char op = scsi_get_prot_op(sc);
1169
1170 switch (op) {
1171 case SCSI_PROT_READ_STRIP:
1172 case SCSI_PROT_WRITE_INSERT:
1173 ret = LPFC_PG_TYPE_NO_DIF;
1174 break;
1175 case SCSI_PROT_READ_INSERT:
1176 case SCSI_PROT_WRITE_STRIP:
1177 case SCSI_PROT_READ_PASS:
1178 case SCSI_PROT_WRITE_PASS:
1179 case SCSI_PROT_WRITE_CONVERT:
1180 case SCSI_PROT_READ_CONVERT:
1181 ret = LPFC_PG_TYPE_DIF_BUF;
1182 break;
1183 default:
1184 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1185 "9021 Unsupported protection op:%d\n", op);
1186 break;
1187 }
1188
1189 return ret;
1190}
1191
1192/*
1193 * This is the protection/DIF aware version of
1194 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
1195 * two functions eventually, but for now, it's here
1196 */
1197static int
1198lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba,
1199 struct lpfc_scsi_buf *lpfc_cmd)
1200{
1201 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1202 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1203 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
1204 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
1205 uint32_t num_bde = 0;
1206 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
1207 int prot_group_type = 0;
1208 int diflen, fcpdl;
1209 unsigned blksize;
1210
1211 /*
1212 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
1213 * fcp_rsp regions to the first data bde entry
1214 */
1215 bpl += 2;
1216 if (scsi_sg_count(scsi_cmnd)) {
1217 /*
1218 * The driver stores the segment count returned from pci_map_sg
1219 * because this a count of dma-mappings used to map the use_sg
1220 * pages. They are not guaranteed to be the same for those
1221 * architectures that implement an IOMMU.
1222 */
1223 datasegcnt = dma_map_sg(&phba->pcidev->dev,
1224 scsi_sglist(scsi_cmnd),
1225 scsi_sg_count(scsi_cmnd), datadir);
1226 if (unlikely(!datasegcnt))
1227 return 1;
1228
1229 lpfc_cmd->seg_cnt = datasegcnt;
1230 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
1231 printk(KERN_ERR "%s: Too many sg segments from "
1232 "dma_map_sg. Config %d, seg_cnt %d\n",
1233 __func__, phba->cfg_sg_seg_cnt,
1234 lpfc_cmd->seg_cnt);
1235 scsi_dma_unmap(scsi_cmnd);
1236 return 1;
1237 }
1238
1239 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
1240
1241 switch (prot_group_type) {
1242 case LPFC_PG_TYPE_NO_DIF:
1243 num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
1244 datasegcnt);
1245 /* we shoud have 2 or more entries in buffer list */
1246 if (num_bde < 2)
1247 goto err;
1248 break;
1249 case LPFC_PG_TYPE_DIF_BUF:{
1250 /*
1251 * This type indicates that protection buffers are
1252 * passed to the driver, so that needs to be prepared
1253 * for DMA
1254 */
1255 protsegcnt = dma_map_sg(&phba->pcidev->dev,
1256 scsi_prot_sglist(scsi_cmnd),
1257 scsi_prot_sg_count(scsi_cmnd), datadir);
1258 if (unlikely(!protsegcnt)) {
1259 scsi_dma_unmap(scsi_cmnd);
1260 return 1;
1261 }
1262
1263 lpfc_cmd->prot_seg_cnt = protsegcnt;
1264 if (lpfc_cmd->prot_seg_cnt
1265 > phba->cfg_prot_sg_seg_cnt) {
1266 printk(KERN_ERR "%s: Too many prot sg segments "
1267 "from dma_map_sg. Config %d,"
1268 "prot_seg_cnt %d\n", __func__,
1269 phba->cfg_prot_sg_seg_cnt,
1270 lpfc_cmd->prot_seg_cnt);
1271 dma_unmap_sg(&phba->pcidev->dev,
1272 scsi_prot_sglist(scsi_cmnd),
1273 scsi_prot_sg_count(scsi_cmnd),
1274 datadir);
1275 scsi_dma_unmap(scsi_cmnd);
1276 return 1;
1277 }
1278
1279 num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
1280 datasegcnt, protsegcnt);
1281 /* we shoud have 3 or more entries in buffer list */
1282 if (num_bde < 3)
1283 goto err;
1284 break;
1285 }
1286 case LPFC_PG_TYPE_INVALID:
1287 default:
1288 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1289 "9022 Unexpected protection group %i\n",
1290 prot_group_type);
1291 return 1;
1292 }
1293 }
1294
1295 /*
1296 * Finish initializing those IOCB fields that are dependent on the
1297 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
1298 * reinitialized since all iocb memory resources are used many times
1299 * for transmit, receive, and continuation bpl's.
1300 */
1301 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
1302 iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
1303 iocb_cmd->ulpBdeCount = 1;
1304 iocb_cmd->ulpLe = 1;
1305
1306 fcpdl = scsi_bufflen(scsi_cmnd);
1307
1308 if (scsi_get_prot_type(scsi_cmnd) == SCSI_PROT_DIF_TYPE1) {
1309 /*
1310 * We are in DIF Type 1 mode
1311 * Every data block has a 8 byte DIF (trailer)
1312 * attached to it. Must ajust FCP data length
1313 */
1314 blksize = lpfc_cmd_blksize(scsi_cmnd);
1315 diflen = (fcpdl / blksize) * 8;
1316 fcpdl += diflen;
1317 }
1318 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
1319
1320 /*
1321 * Due to difference in data length between DIF/non-DIF paths,
1322 * we need to set word 4 of IOCB here
1323 */
1324 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
1325
1326 return 0;
1327err:
1328 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1329 "9023 Could not setup all needed BDE's"
1330 "prot_group_type=%d, num_bde=%d\n",
1331 prot_group_type, num_bde);
1332 return 1;
1333}
1334
1335/*
1336 * This function checks for BlockGuard errors detected by
1337 * the HBA. In case of errors, the ASC/ASCQ fields in the
1338 * sense buffer will be set accordingly, paired with
1339 * ILLEGAL_REQUEST to signal to the kernel that the HBA
1340 * detected corruption.
1341 *
1342 * Returns:
1343 * 0 - No error found
1344 * 1 - BlockGuard error found
1345 * -1 - Internal error (bad profile, ...etc)
1346 */
1347static int
1348lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
1349 struct lpfc_iocbq *pIocbOut)
1350{
1351 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
1352 struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
1353 int ret = 0;
1354 uint32_t bghm = bgf->bghm;
1355 uint32_t bgstat = bgf->bgstat;
1356 uint64_t failing_sector = 0;
1357
1358 printk(KERN_ERR "BG ERROR in cmd 0x%x lba 0x%llx blk cnt 0x%lx "
1359 "bgstat=0x%x bghm=0x%x\n",
James Smart87b5c322008-12-16 10:34:09 -05001360 cmd->cmnd[0], (unsigned long long)scsi_get_lba(cmd),
James Smarte2a0a9d2008-12-04 22:40:02 -05001361 cmd->request->nr_sectors, bgstat, bghm);
1362
1363 spin_lock(&_dump_buf_lock);
1364 if (!_dump_buf_done) {
1365 printk(KERN_ERR "Saving Data for %u blocks to debugfs\n",
1366 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
1367 lpfc_debug_save_data(cmd);
1368
1369 /* If we have a prot sgl, save the DIF buffer */
1370 if (lpfc_prot_group_type(phba, cmd) ==
1371 LPFC_PG_TYPE_DIF_BUF) {
1372 printk(KERN_ERR "Saving DIF for %u blocks to debugfs\n",
1373 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
1374 lpfc_debug_save_dif(cmd);
1375 }
1376
1377 _dump_buf_done = 1;
1378 }
1379 spin_unlock(&_dump_buf_lock);
1380
1381 if (lpfc_bgs_get_invalid_prof(bgstat)) {
1382 cmd->result = ScsiResult(DID_ERROR, 0);
1383 printk(KERN_ERR "Invalid BlockGuard profile. bgstat:0x%x\n",
1384 bgstat);
1385 ret = (-1);
1386 goto out;
1387 }
1388
1389 if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
1390 cmd->result = ScsiResult(DID_ERROR, 0);
1391 printk(KERN_ERR "Invalid BlockGuard DIF Block. bgstat:0x%x\n",
1392 bgstat);
1393 ret = (-1);
1394 goto out;
1395 }
1396
1397 if (lpfc_bgs_get_guard_err(bgstat)) {
1398 ret = 1;
1399
1400 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
1401 0x10, 0x1);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05001402 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05001403 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
1404 phba->bg_guard_err_cnt++;
1405 printk(KERN_ERR "BLKGRD: guard_tag error\n");
1406 }
1407
1408 if (lpfc_bgs_get_reftag_err(bgstat)) {
1409 ret = 1;
1410
1411 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
1412 0x10, 0x3);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05001413 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05001414 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
1415
1416 phba->bg_reftag_err_cnt++;
1417 printk(KERN_ERR "BLKGRD: ref_tag error\n");
1418 }
1419
1420 if (lpfc_bgs_get_apptag_err(bgstat)) {
1421 ret = 1;
1422
1423 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
1424 0x10, 0x2);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05001425 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05001426 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
1427
1428 phba->bg_apptag_err_cnt++;
1429 printk(KERN_ERR "BLKGRD: app_tag error\n");
1430 }
1431
1432 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
1433 /*
1434 * setup sense data descriptor 0 per SPC-4 as an information
1435 * field, and put the failing LBA in it
1436 */
1437 cmd->sense_buffer[8] = 0; /* Information */
1438 cmd->sense_buffer[9] = 0xa; /* Add. length */
David Howells2344b5b2009-04-14 17:08:34 +01001439 bghm /= cmd->device->sector_size;
James Smarte2a0a9d2008-12-04 22:40:02 -05001440
1441 failing_sector = scsi_get_lba(cmd);
1442 failing_sector += bghm;
1443
1444 put_unaligned_be64(failing_sector, &cmd->sense_buffer[10]);
1445 }
1446
1447 if (!ret) {
1448 /* No error was reported - problem in FW? */
1449 cmd->result = ScsiResult(DID_ERROR, 0);
1450 printk(KERN_ERR "BLKGRD: no errors reported!\n");
1451 }
1452
1453out:
1454 return ret;
1455}
1456
James Smartea2151b2008-09-07 11:52:10 -04001457/**
James Smart3772a992009-05-22 14:50:54 -04001458 * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
1459 * @phba: The Hba for which this call is being executed.
1460 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1461 *
1462 * This routine wraps the actual DMA mapping function pointer from the
1463 * lpfc_hba struct.
1464 *
1465 * Return codes:
1466 * 1 - Error
1467 * 0 - Success
1468 **/
1469static inline int
1470lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
1471{
1472 return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
1473}
1474
1475/**
James Smart3621a712009-04-06 18:47:14 -04001476 * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
James Smartea2151b2008-09-07 11:52:10 -04001477 * @phba: Pointer to hba context object.
1478 * @vport: Pointer to vport object.
1479 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
1480 * @rsp_iocb: Pointer to response iocb object which reported error.
1481 *
1482 * This function posts an event when there is a SCSI command reporting
1483 * error from the scsi device.
1484 **/
1485static void
1486lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
1487 struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
1488 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
1489 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
1490 uint32_t resp_info = fcprsp->rspStatus2;
1491 uint32_t scsi_status = fcprsp->rspStatus3;
1492 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
1493 struct lpfc_fast_path_event *fast_path_evt = NULL;
1494 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
1495 unsigned long flags;
1496
1497 /* If there is queuefull or busy condition send a scsi event */
1498 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
1499 (cmnd->result == SAM_STAT_BUSY)) {
1500 fast_path_evt = lpfc_alloc_fast_evt(phba);
1501 if (!fast_path_evt)
1502 return;
1503 fast_path_evt->un.scsi_evt.event_type =
1504 FC_REG_SCSI_EVENT;
1505 fast_path_evt->un.scsi_evt.subcategory =
1506 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
1507 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
1508 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
1509 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
1510 &pnode->nlp_portname, sizeof(struct lpfc_name));
1511 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
1512 &pnode->nlp_nodename, sizeof(struct lpfc_name));
1513 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
1514 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
1515 fast_path_evt = lpfc_alloc_fast_evt(phba);
1516 if (!fast_path_evt)
1517 return;
1518 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
1519 FC_REG_SCSI_EVENT;
1520 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
1521 LPFC_EVENT_CHECK_COND;
1522 fast_path_evt->un.check_cond_evt.scsi_event.lun =
1523 cmnd->device->lun;
1524 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
1525 &pnode->nlp_portname, sizeof(struct lpfc_name));
1526 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
1527 &pnode->nlp_nodename, sizeof(struct lpfc_name));
1528 fast_path_evt->un.check_cond_evt.sense_key =
1529 cmnd->sense_buffer[2] & 0xf;
1530 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
1531 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
1532 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
1533 fcpi_parm &&
1534 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
1535 ((scsi_status == SAM_STAT_GOOD) &&
1536 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
1537 /*
1538 * If status is good or resid does not match with fcp_param and
1539 * there is valid fcpi_parm, then there is a read_check error
1540 */
1541 fast_path_evt = lpfc_alloc_fast_evt(phba);
1542 if (!fast_path_evt)
1543 return;
1544 fast_path_evt->un.read_check_error.header.event_type =
1545 FC_REG_FABRIC_EVENT;
1546 fast_path_evt->un.read_check_error.header.subcategory =
1547 LPFC_EVENT_FCPRDCHKERR;
1548 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
1549 &pnode->nlp_portname, sizeof(struct lpfc_name));
1550 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
1551 &pnode->nlp_nodename, sizeof(struct lpfc_name));
1552 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
1553 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
1554 fast_path_evt->un.read_check_error.fcpiparam =
1555 fcpi_parm;
1556 } else
1557 return;
1558
1559 fast_path_evt->vport = vport;
1560 spin_lock_irqsave(&phba->hbalock, flags);
1561 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
1562 spin_unlock_irqrestore(&phba->hbalock, flags);
1563 lpfc_worker_wake_up(phba);
1564 return;
1565}
James Smart9bad7672008-12-04 22:39:02 -05001566
1567/**
James Smart3772a992009-05-22 14:50:54 -04001568 * lpfc_scsi_unprep_dma_buf_s3 - Un-map DMA mapping of SG-list for SLI3 dev
1569 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -05001570 * @psb: The scsi buffer which is going to be un-mapped.
1571 *
1572 * This routine does DMA un-mapping of scatter gather list of scsi command
James Smart3772a992009-05-22 14:50:54 -04001573 * field of @lpfc_cmd for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05001574 **/
dea31012005-04-17 16:05:31 -05001575static void
James Smart3772a992009-05-22 14:50:54 -04001576lpfc_scsi_unprep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James Smartbcf4dbf2006-07-06 15:50:08 -04001577{
1578 /*
1579 * There are only two special cases to consider. (1) the scsi command
1580 * requested scatter-gather usage or (2) the scsi command allocated
1581 * a request buffer, but did not request use_sg. There is a third
1582 * case, but it does not require resource deallocation.
1583 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001584 if (psb->seg_cnt > 0)
1585 scsi_dma_unmap(psb->pCmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05001586 if (psb->prot_seg_cnt > 0)
1587 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
1588 scsi_prot_sg_count(psb->pCmd),
1589 psb->pCmd->sc_data_direction);
James Smartbcf4dbf2006-07-06 15:50:08 -04001590}
1591
James Smart9bad7672008-12-04 22:39:02 -05001592/**
James Smart3772a992009-05-22 14:50:54 -04001593 * lpfc_scsi_unprep_dma_buf - Wrapper function for unmap DMA mapping of SG-list
1594 * @phba: The Hba for which this call is being executed.
1595 * @psb: The scsi buffer which is going to be un-mapped.
1596 *
1597 * This routine does DMA un-mapping of scatter gather list of scsi command
1598 * field of @lpfc_cmd for device with SLI-4 interface spec.
1599 **/
1600static void
1601lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1602{
1603 phba->lpfc_scsi_unprep_dma_buf(phba, psb);
1604}
1605
1606/**
James Smart3621a712009-04-06 18:47:14 -04001607 * lpfc_handler_fcp_err - FCP response handler
James Smart9bad7672008-12-04 22:39:02 -05001608 * @vport: The virtual port for which this call is being executed.
1609 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
1610 * @rsp_iocb: The response IOCB which contains FCP error.
1611 *
1612 * This routine is called to process response IOCB with status field
1613 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
1614 * based upon SCSI and FCP error.
1615 **/
James Smartbcf4dbf2006-07-06 15:50:08 -04001616static void
James Smart2e0fef82007-06-17 19:56:36 -05001617lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
1618 struct lpfc_iocbq *rsp_iocb)
dea31012005-04-17 16:05:31 -05001619{
1620 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
1621 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
1622 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
James Smart7054a602007-04-25 09:52:34 -04001623 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
dea31012005-04-17 16:05:31 -05001624 uint32_t resp_info = fcprsp->rspStatus2;
1625 uint32_t scsi_status = fcprsp->rspStatus3;
James Smartc7743952006-12-02 13:34:42 -05001626 uint32_t *lp;
dea31012005-04-17 16:05:31 -05001627 uint32_t host_status = DID_OK;
1628 uint32_t rsplen = 0;
James Smartc7743952006-12-02 13:34:42 -05001629 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea31012005-04-17 16:05:31 -05001630
James Smartea2151b2008-09-07 11:52:10 -04001631
dea31012005-04-17 16:05:31 -05001632 /*
1633 * If this is a task management command, there is no
1634 * scsi packet associated with this lpfc_cmd. The driver
1635 * consumes it.
1636 */
1637 if (fcpcmd->fcpCntl2) {
1638 scsi_status = 0;
1639 goto out;
1640 }
1641
James Smartc7743952006-12-02 13:34:42 -05001642 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
1643 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
1644 if (snslen > SCSI_SENSE_BUFFERSIZE)
1645 snslen = SCSI_SENSE_BUFFERSIZE;
1646
1647 if (resp_info & RSP_LEN_VALID)
1648 rsplen = be32_to_cpu(fcprsp->rspRspLen);
1649 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
1650 }
1651 lp = (uint32_t *)cmnd->sense_buffer;
1652
1653 if (!scsi_status && (resp_info & RESID_UNDER))
1654 logit = LOG_FCP;
1655
James Smarte8b62012007-08-02 11:10:09 -04001656 lpfc_printf_vlog(vport, KERN_WARNING, logit,
James Smarte2a0a9d2008-12-04 22:40:02 -05001657 "9024 FCP command x%x failed: x%x SNS x%x x%x "
James Smarte8b62012007-08-02 11:10:09 -04001658 "Data: x%x x%x x%x x%x x%x\n",
1659 cmnd->cmnd[0], scsi_status,
1660 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
1661 be32_to_cpu(fcprsp->rspResId),
1662 be32_to_cpu(fcprsp->rspSnsLen),
1663 be32_to_cpu(fcprsp->rspRspLen),
1664 fcprsp->rspInfo3);
dea31012005-04-17 16:05:31 -05001665
1666 if (resp_info & RSP_LEN_VALID) {
1667 rsplen = be32_to_cpu(fcprsp->rspRspLen);
1668 if ((rsplen != 0 && rsplen != 4 && rsplen != 8) ||
1669 (fcprsp->rspInfo3 != RSP_NO_FAILURE)) {
1670 host_status = DID_ERROR;
1671 goto out;
1672 }
1673 }
1674
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001675 scsi_set_resid(cmnd, 0);
dea31012005-04-17 16:05:31 -05001676 if (resp_info & RESID_UNDER) {
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001677 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea31012005-04-17 16:05:31 -05001678
James Smarte8b62012007-08-02 11:10:09 -04001679 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05001680 "9025 FCP Read Underrun, expected %d, "
James Smarte8b62012007-08-02 11:10:09 -04001681 "residual %d Data: x%x x%x x%x\n",
1682 be32_to_cpu(fcpcmd->fcpDl),
1683 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
1684 cmnd->underflow);
dea31012005-04-17 16:05:31 -05001685
1686 /*
James Smart7054a602007-04-25 09:52:34 -04001687 * If there is an under run check if under run reported by
1688 * storage array is same as the under run reported by HBA.
1689 * If this is not same, there is a dropped frame.
1690 */
1691 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
1692 fcpi_parm &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001693 (scsi_get_resid(cmnd) != fcpi_parm)) {
James Smarte8b62012007-08-02 11:10:09 -04001694 lpfc_printf_vlog(vport, KERN_WARNING,
1695 LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05001696 "9026 FCP Read Check Error "
James Smarte8b62012007-08-02 11:10:09 -04001697 "and Underrun Data: x%x x%x x%x x%x\n",
1698 be32_to_cpu(fcpcmd->fcpDl),
1699 scsi_get_resid(cmnd), fcpi_parm,
1700 cmnd->cmnd[0]);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001701 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
James Smart7054a602007-04-25 09:52:34 -04001702 host_status = DID_ERROR;
1703 }
1704 /*
dea31012005-04-17 16:05:31 -05001705 * The cmnd->underflow is the minimum number of bytes that must
1706 * be transfered for this command. Provided a sense condition
1707 * is not present, make sure the actual amount transferred is at
1708 * least the underflow value or fail.
1709 */
1710 if (!(resp_info & SNS_LEN_VALID) &&
1711 (scsi_status == SAM_STAT_GOOD) &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001712 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
1713 < cmnd->underflow)) {
James Smarte8b62012007-08-02 11:10:09 -04001714 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05001715 "9027 FCP command x%x residual "
James Smarte8b62012007-08-02 11:10:09 -04001716 "underrun converted to error "
1717 "Data: x%x x%x x%x\n",
James Smart66dbfbe2007-08-05 06:08:38 -04001718 cmnd->cmnd[0], scsi_bufflen(cmnd),
James Smarte8b62012007-08-02 11:10:09 -04001719 scsi_get_resid(cmnd), cmnd->underflow);
dea31012005-04-17 16:05:31 -05001720 host_status = DID_ERROR;
1721 }
1722 } else if (resp_info & RESID_OVER) {
James Smarte8b62012007-08-02 11:10:09 -04001723 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05001724 "9028 FCP command x%x residual overrun error. "
James Smarte8b62012007-08-02 11:10:09 -04001725 "Data: x%x x%x \n", cmnd->cmnd[0],
1726 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea31012005-04-17 16:05:31 -05001727 host_status = DID_ERROR;
1728
1729 /*
1730 * Check SLI validation that all the transfer was actually done
1731 * (fcpi_parm should be zero). Apply check only to reads.
1732 */
1733 } else if ((scsi_status == SAM_STAT_GOOD) && fcpi_parm &&
1734 (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
James Smarte8b62012007-08-02 11:10:09 -04001735 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05001736 "9029 FCP Read Check Error Data: "
James Smarte8b62012007-08-02 11:10:09 -04001737 "x%x x%x x%x x%x\n",
1738 be32_to_cpu(fcpcmd->fcpDl),
1739 be32_to_cpu(fcprsp->rspResId),
1740 fcpi_parm, cmnd->cmnd[0]);
dea31012005-04-17 16:05:31 -05001741 host_status = DID_ERROR;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001742 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea31012005-04-17 16:05:31 -05001743 }
1744
1745 out:
1746 cmnd->result = ScsiResult(host_status, scsi_status);
James Smartea2151b2008-09-07 11:52:10 -04001747 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
dea31012005-04-17 16:05:31 -05001748}
1749
James Smart9bad7672008-12-04 22:39:02 -05001750/**
James Smart3621a712009-04-06 18:47:14 -04001751 * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
James Smart9bad7672008-12-04 22:39:02 -05001752 * @phba: The Hba for which this call is being executed.
1753 * @pIocbIn: The command IOCBQ for the scsi cmnd.
James Smart3772a992009-05-22 14:50:54 -04001754 * @pIocbOut: The response IOCBQ for the scsi cmnd.
James Smart9bad7672008-12-04 22:39:02 -05001755 *
1756 * This routine assigns scsi command result by looking into response IOCB
1757 * status field appropriately. This routine handles QUEUE FULL condition as
1758 * well by ramping down device queue depth.
1759 **/
dea31012005-04-17 16:05:31 -05001760static void
1761lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
1762 struct lpfc_iocbq *pIocbOut)
1763{
1764 struct lpfc_scsi_buf *lpfc_cmd =
1765 (struct lpfc_scsi_buf *) pIocbIn->context1;
James Smart2e0fef82007-06-17 19:56:36 -05001766 struct lpfc_vport *vport = pIocbIn->vport;
dea31012005-04-17 16:05:31 -05001767 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
1768 struct lpfc_nodelist *pnode = rdata->pnode;
1769 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05001770 int result;
James Smarta257bf92009-04-06 18:48:10 -04001771 struct scsi_device *tmp_sdev;
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05001772 int depth = 0;
James Smartfa61a542008-01-11 01:52:42 -05001773 unsigned long flags;
James Smartea2151b2008-09-07 11:52:10 -04001774 struct lpfc_fast_path_event *fast_path_evt;
James Smarta257bf92009-04-06 18:48:10 -04001775 struct Scsi_Host *shost = cmd->device->host;
1776 uint32_t queue_depth, scsi_id;
dea31012005-04-17 16:05:31 -05001777
1778 lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
1779 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
James Smart109f6ed2008-12-04 22:39:08 -05001780 if (pnode && NLP_CHK_NODE_ACT(pnode))
1781 atomic_dec(&pnode->cmd_pending);
dea31012005-04-17 16:05:31 -05001782
1783 if (lpfc_cmd->status) {
1784 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
1785 (lpfc_cmd->result & IOERR_DRVR_MASK))
1786 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
1787 else if (lpfc_cmd->status >= IOSTAT_CNT)
1788 lpfc_cmd->status = IOSTAT_DEFAULT;
1789
James Smarte8b62012007-08-02 11:10:09 -04001790 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05001791 "9030 FCP cmd x%x failed <%d/%d> "
James Smarte8b62012007-08-02 11:10:09 -04001792 "status: x%x result: x%x Data: x%x x%x\n",
1793 cmd->cmnd[0],
1794 cmd->device ? cmd->device->id : 0xffff,
1795 cmd->device ? cmd->device->lun : 0xffff,
1796 lpfc_cmd->status, lpfc_cmd->result,
1797 pIocbOut->iocb.ulpContext,
1798 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
dea31012005-04-17 16:05:31 -05001799
1800 switch (lpfc_cmd->status) {
1801 case IOSTAT_FCP_RSP_ERROR:
1802 /* Call FCP RSP handler to determine result */
James Smart2e0fef82007-06-17 19:56:36 -05001803 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea31012005-04-17 16:05:31 -05001804 break;
1805 case IOSTAT_NPORT_BSY:
1806 case IOSTAT_FABRIC_BSY:
James Smart0f1f53a2008-08-24 21:50:18 -04001807 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
James Smartea2151b2008-09-07 11:52:10 -04001808 fast_path_evt = lpfc_alloc_fast_evt(phba);
1809 if (!fast_path_evt)
1810 break;
1811 fast_path_evt->un.fabric_evt.event_type =
1812 FC_REG_FABRIC_EVENT;
1813 fast_path_evt->un.fabric_evt.subcategory =
1814 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
1815 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
1816 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
1817 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
1818 &pnode->nlp_portname,
1819 sizeof(struct lpfc_name));
1820 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
1821 &pnode->nlp_nodename,
1822 sizeof(struct lpfc_name));
1823 }
1824 fast_path_evt->vport = vport;
1825 fast_path_evt->work_evt.evt =
1826 LPFC_EVT_FASTPATH_MGMT_EVT;
1827 spin_lock_irqsave(&phba->hbalock, flags);
1828 list_add_tail(&fast_path_evt->work_evt.evt_listp,
1829 &phba->work_list);
1830 spin_unlock_irqrestore(&phba->hbalock, flags);
1831 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05001832 break;
James Smart92d7f7b2007-06-17 19:56:38 -05001833 case IOSTAT_LOCAL_REJECT:
James Smartd7c255b2008-08-24 21:50:00 -04001834 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
James Smart92d7f7b2007-06-17 19:56:38 -05001835 lpfc_cmd->result == IOERR_NO_RESOURCES ||
James Smartd7c255b2008-08-24 21:50:00 -04001836 lpfc_cmd->result == IOERR_ABORT_REQUESTED) {
James Smart92d7f7b2007-06-17 19:56:38 -05001837 cmd->result = ScsiResult(DID_REQUEUE, 0);
James Smart58da1ff2008-04-07 10:15:56 -04001838 break;
James Smarte2a0a9d2008-12-04 22:40:02 -05001839 }
1840
1841 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
1842 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
1843 pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
1844 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
1845 /*
1846 * This is a response for a BG enabled
1847 * cmd. Parse BG error
1848 */
1849 lpfc_parse_bg_err(phba, lpfc_cmd,
1850 pIocbOut);
1851 break;
1852 } else {
1853 lpfc_printf_vlog(vport, KERN_WARNING,
1854 LOG_BG,
1855 "9031 non-zero BGSTAT "
1856 "on unprotected cmd");
1857 }
1858 }
1859
1860 /* else: fall through */
dea31012005-04-17 16:05:31 -05001861 default:
1862 cmd->result = ScsiResult(DID_ERROR, 0);
1863 break;
1864 }
1865
James Smart58da1ff2008-04-07 10:15:56 -04001866 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001867 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
James Smart0f1f53a2008-08-24 21:50:18 -04001868 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
1869 SAM_STAT_BUSY);
dea31012005-04-17 16:05:31 -05001870 } else {
1871 cmd->result = ScsiResult(DID_OK, 0);
1872 }
1873
1874 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
1875 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
1876
James Smarte8b62012007-08-02 11:10:09 -04001877 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
1878 "0710 Iodone <%d/%d> cmd %p, error "
1879 "x%x SNS x%x x%x Data: x%x x%x\n",
1880 cmd->device->id, cmd->device->lun, cmd,
1881 cmd->result, *lp, *(lp + 3), cmd->retries,
1882 scsi_get_resid(cmd));
dea31012005-04-17 16:05:31 -05001883 }
1884
James Smartea2151b2008-09-07 11:52:10 -04001885 lpfc_update_stats(phba, lpfc_cmd);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05001886 result = cmd->result;
James Smart977b5a02008-09-07 11:52:04 -04001887 if (vport->cfg_max_scsicmpl_time &&
1888 time_after(jiffies, lpfc_cmd->start_time +
1889 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
James Smarta257bf92009-04-06 18:48:10 -04001890 spin_lock_irqsave(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05001891 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
1892 if (pnode->cmd_qdepth >
1893 atomic_read(&pnode->cmd_pending) &&
1894 (atomic_read(&pnode->cmd_pending) >
1895 LPFC_MIN_TGT_QDEPTH) &&
1896 ((cmd->cmnd[0] == READ_10) ||
1897 (cmd->cmnd[0] == WRITE_10)))
1898 pnode->cmd_qdepth =
1899 atomic_read(&pnode->cmd_pending);
James Smart977b5a02008-09-07 11:52:04 -04001900
James Smart109f6ed2008-12-04 22:39:08 -05001901 pnode->last_change_time = jiffies;
1902 }
James Smarta257bf92009-04-06 18:48:10 -04001903 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05001904 } else if (pnode && NLP_CHK_NODE_ACT(pnode)) {
1905 if ((pnode->cmd_qdepth < LPFC_MAX_TGT_QDEPTH) &&
James Smart977b5a02008-09-07 11:52:04 -04001906 time_after(jiffies, pnode->last_change_time +
James Smart109f6ed2008-12-04 22:39:08 -05001907 msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
James Smarta257bf92009-04-06 18:48:10 -04001908 spin_lock_irqsave(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05001909 pnode->cmd_qdepth += pnode->cmd_qdepth *
1910 LPFC_TGTQ_RAMPUP_PCENT / 100;
1911 if (pnode->cmd_qdepth > LPFC_MAX_TGT_QDEPTH)
1912 pnode->cmd_qdepth = LPFC_MAX_TGT_QDEPTH;
1913 pnode->last_change_time = jiffies;
James Smarta257bf92009-04-06 18:48:10 -04001914 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05001915 }
James Smart977b5a02008-09-07 11:52:04 -04001916 }
1917
James Smart1dcb58e2007-04-25 09:51:30 -04001918 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James Smarta257bf92009-04-06 18:48:10 -04001919
1920 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
1921 queue_depth = cmd->device->queue_depth;
1922 scsi_id = cmd->device->id;
dea31012005-04-17 16:05:31 -05001923 cmd->scsi_done(cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001924
Jamie Wellnitzb8086082006-02-28 22:33:12 -05001925 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smartfa61a542008-01-11 01:52:42 -05001926 /*
1927 * If there is a thread waiting for command completion
1928 * wake up the thread.
1929 */
James Smarta257bf92009-04-06 18:48:10 -04001930 spin_lock_irqsave(shost->host_lock, flags);
James Smart495a7142008-06-14 22:52:59 -04001931 lpfc_cmd->pCmd = NULL;
James Smartfa61a542008-01-11 01:52:42 -05001932 if (lpfc_cmd->waitq)
1933 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04001934 spin_unlock_irqrestore(shost->host_lock, flags);
Jamie Wellnitzb8086082006-02-28 22:33:12 -05001935 lpfc_release_scsi_buf(phba, lpfc_cmd);
1936 return;
1937 }
1938
James Smart92d7f7b2007-06-17 19:56:38 -05001939
1940 if (!result)
James Smarta257bf92009-04-06 18:48:10 -04001941 lpfc_rampup_queue_depth(vport, queue_depth);
James Smart92d7f7b2007-06-17 19:56:38 -05001942
James Smart58da1ff2008-04-07 10:15:56 -04001943 if (!result && pnode && NLP_CHK_NODE_ACT(pnode) &&
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05001944 ((jiffies - pnode->last_ramp_up_time) >
1945 LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
1946 ((jiffies - pnode->last_q_full_time) >
1947 LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
James Smarta257bf92009-04-06 18:48:10 -04001948 (vport->cfg_lun_queue_depth > queue_depth)) {
1949 shost_for_each_device(tmp_sdev, shost) {
James Smart3de2a652007-08-02 11:09:59 -04001950 if (vport->cfg_lun_queue_depth > tmp_sdev->queue_depth){
James Smarta257bf92009-04-06 18:48:10 -04001951 if (tmp_sdev->id != scsi_id)
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05001952 continue;
1953 if (tmp_sdev->ordered_tags)
1954 scsi_adjust_queue_depth(tmp_sdev,
1955 MSG_ORDERED_TAG,
1956 tmp_sdev->queue_depth+1);
1957 else
1958 scsi_adjust_queue_depth(tmp_sdev,
1959 MSG_SIMPLE_TAG,
1960 tmp_sdev->queue_depth+1);
1961
1962 pnode->last_ramp_up_time = jiffies;
1963 }
1964 }
James Smartea2151b2008-09-07 11:52:10 -04001965 lpfc_send_sdev_queuedepth_change_event(phba, vport, pnode,
1966 0xFFFFFFFF,
James Smarta257bf92009-04-06 18:48:10 -04001967 queue_depth , queue_depth + 1);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05001968 }
1969
1970 /*
1971 * Check for queue full. If the lun is reporting queue full, then
1972 * back off the lun queue depth to prevent target overloads.
1973 */
James Smart58da1ff2008-04-07 10:15:56 -04001974 if (result == SAM_STAT_TASK_SET_FULL && pnode &&
1975 NLP_CHK_NODE_ACT(pnode)) {
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05001976 pnode->last_q_full_time = jiffies;
1977
James Smarta257bf92009-04-06 18:48:10 -04001978 shost_for_each_device(tmp_sdev, shost) {
1979 if (tmp_sdev->id != scsi_id)
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05001980 continue;
1981 depth = scsi_track_queue_full(tmp_sdev,
1982 tmp_sdev->queue_depth - 1);
1983 }
1984 /*
James Smart2e0fef82007-06-17 19:56:36 -05001985 * The queue depth cannot be lowered any more.
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05001986 * Modify the returned error code to store
1987 * the final depth value set by
1988 * scsi_track_queue_full.
1989 */
1990 if (depth == -1)
James Smarta257bf92009-04-06 18:48:10 -04001991 depth = shost->cmd_per_lun;
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05001992
1993 if (depth) {
James Smarte8b62012007-08-02 11:10:09 -04001994 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
1995 "0711 detected queue full - lun queue "
1996 "depth adjusted to %d.\n", depth);
James Smartea2151b2008-09-07 11:52:10 -04001997 lpfc_send_sdev_queuedepth_change_event(phba, vport,
1998 pnode, 0xFFFFFFFF,
1999 depth+1, depth);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002000 }
2001 }
2002
James Smartfa61a542008-01-11 01:52:42 -05002003 /*
2004 * If there is a thread waiting for command completion
2005 * wake up the thread.
2006 */
James Smarta257bf92009-04-06 18:48:10 -04002007 spin_lock_irqsave(shost->host_lock, flags);
James Smart495a7142008-06-14 22:52:59 -04002008 lpfc_cmd->pCmd = NULL;
James Smartfa61a542008-01-11 01:52:42 -05002009 if (lpfc_cmd->waitq)
2010 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04002011 spin_unlock_irqrestore(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05002012
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002013 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05002014}
2015
James Smart34b02dc2008-08-24 21:49:55 -04002016/**
James Smart3621a712009-04-06 18:47:14 -04002017 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
James Smart34b02dc2008-08-24 21:49:55 -04002018 * @data: A pointer to the immediate command data portion of the IOCB.
2019 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
2020 *
2021 * The routine copies the entire FCP command from @fcp_cmnd to @data while
2022 * byte swapping the data to big endian format for transmission on the wire.
2023 **/
2024static void
2025lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
2026{
2027 int i, j;
2028 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
2029 i += sizeof(uint32_t), j++) {
2030 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
2031 }
2032}
2033
James Smart9bad7672008-12-04 22:39:02 -05002034/**
James Smart3772a992009-05-22 14:50:54 -04002035 * lpfc_scsi_prep_cmnd_s3 - Convert scsi cmnd to FCP infor unit for SLI3 dev
James Smart9bad7672008-12-04 22:39:02 -05002036 * @vport: The virtual port for which this call is being executed.
2037 * @lpfc_cmd: The scsi command which needs to send.
2038 * @pnode: Pointer to lpfc_nodelist.
2039 *
2040 * This routine initializes fcp_cmnd and iocb data structure from scsi command
James Smart3772a992009-05-22 14:50:54 -04002041 * to transfer for device with SLI3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05002042 **/
dea31012005-04-17 16:05:31 -05002043static void
James Smart3772a992009-05-22 14:50:54 -04002044lpfc_scsi_prep_cmnd_s3(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
James Smart2e0fef82007-06-17 19:56:36 -05002045 struct lpfc_nodelist *pnode)
dea31012005-04-17 16:05:31 -05002046{
James Smart2e0fef82007-06-17 19:56:36 -05002047 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002048 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2049 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2050 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2051 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
2052 int datadir = scsi_cmnd->sc_data_direction;
James Smart7e2b19f2007-10-29 11:00:39 -04002053 char tag[2];
dea31012005-04-17 16:05:31 -05002054
James Smart58da1ff2008-04-07 10:15:56 -04002055 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
2056 return;
2057
dea31012005-04-17 16:05:31 -05002058 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
James.Smart@Emulex.Com69859dc2005-08-10 15:02:37 -04002059 /* clear task management bits */
2060 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea31012005-04-17 16:05:31 -05002061
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -04002062 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
2063 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05002064
2065 memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, 16);
2066
James Smart7e2b19f2007-10-29 11:00:39 -04002067 if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
2068 switch (tag[0]) {
dea31012005-04-17 16:05:31 -05002069 case HEAD_OF_QUEUE_TAG:
2070 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
2071 break;
2072 case ORDERED_QUEUE_TAG:
2073 fcp_cmnd->fcpCntl1 = ORDERED_Q;
2074 break;
2075 default:
2076 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
2077 break;
2078 }
2079 } else
2080 fcp_cmnd->fcpCntl1 = 0;
2081
2082 /*
2083 * There are three possibilities here - use scatter-gather segment, use
2084 * the single mapping, or neither. Start the lpfc command prep by
2085 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
2086 * data bde entry.
2087 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002088 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05002089 if (datadir == DMA_TO_DEVICE) {
2090 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
James Smart3772a992009-05-22 14:50:54 -04002091 if (phba->sli_rev < LPFC_SLI_REV4) {
2092 iocb_cmd->un.fcpi.fcpi_parm = 0;
2093 iocb_cmd->ulpPU = 0;
2094 } else
2095 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05002096 fcp_cmnd->fcpCntl3 = WRITE_DATA;
2097 phba->fc4OutputRequests++;
2098 } else {
2099 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
2100 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05002101 fcp_cmnd->fcpCntl3 = READ_DATA;
2102 phba->fc4InputRequests++;
2103 }
2104 } else {
2105 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
2106 iocb_cmd->un.fcpi.fcpi_parm = 0;
2107 iocb_cmd->ulpPU = 0;
2108 fcp_cmnd->fcpCntl3 = 0;
2109 phba->fc4ControlRequests++;
2110 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002111 if (phba->sli_rev == 3 &&
2112 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04002113 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05002114 /*
2115 * Finish initializing those IOCB fields that are independent
2116 * of the scsi_cmnd request_buffer
2117 */
2118 piocbq->iocb.ulpContext = pnode->nlp_rpi;
2119 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
2120 piocbq->iocb.ulpFCP2Rcvy = 1;
James Smart09372822008-01-11 01:52:54 -05002121 else
2122 piocbq->iocb.ulpFCP2Rcvy = 0;
dea31012005-04-17 16:05:31 -05002123
2124 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
2125 piocbq->context1 = lpfc_cmd;
2126 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
2127 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
James Smart2e0fef82007-06-17 19:56:36 -05002128 piocbq->vport = vport;
dea31012005-04-17 16:05:31 -05002129}
2130
James Smart9bad7672008-12-04 22:39:02 -05002131/**
James Smart3772a992009-05-22 14:50:54 -04002132 * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
2133 * @vport: The virtual port for which this call is being executed.
2134 * @lpfc_cmd: The scsi command which needs to send.
2135 * @pnode: Pointer to lpfc_nodelist.
2136 *
2137 * This routine wraps the actual convert SCSI cmnd function pointer from
2138 * the lpfc_hba struct.
2139 **/
2140static inline void
2141lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
2142 struct lpfc_nodelist *pnode)
2143{
2144 vport->phba->lpfc_scsi_prep_cmnd(vport, lpfc_cmd, pnode);
2145}
2146
2147/**
2148 * lpfc_scsi_prep_task_mgmt_cmnd_s3 - Convert SLI3 scsi TM cmd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05002149 * @vport: The virtual port for which this call is being executed.
2150 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
2151 * @lun: Logical unit number.
2152 * @task_mgmt_cmd: SCSI task management command.
2153 *
James Smart3772a992009-05-22 14:50:54 -04002154 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
2155 * for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05002156 *
2157 * Return codes:
2158 * 0 - Error
2159 * 1 - Success
2160 **/
dea31012005-04-17 16:05:31 -05002161static int
James Smart3772a992009-05-22 14:50:54 -04002162lpfc_scsi_prep_task_mgmt_cmd_s3(struct lpfc_vport *vport,
dea31012005-04-17 16:05:31 -05002163 struct lpfc_scsi_buf *lpfc_cmd,
James Smart420b630d2006-07-06 15:50:16 -04002164 unsigned int lun,
dea31012005-04-17 16:05:31 -05002165 uint8_t task_mgmt_cmd)
2166{
dea31012005-04-17 16:05:31 -05002167 struct lpfc_iocbq *piocbq;
2168 IOCB_t *piocb;
2169 struct fcp_cmnd *fcp_cmnd;
James Smart0b18ac42006-05-01 21:50:40 -04002170 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea31012005-04-17 16:05:31 -05002171 struct lpfc_nodelist *ndlp = rdata->pnode;
2172
James Smart58da1ff2008-04-07 10:15:56 -04002173 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
2174 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05002175 return 0;
dea31012005-04-17 16:05:31 -05002176
dea31012005-04-17 16:05:31 -05002177 piocbq = &(lpfc_cmd->cur_iocbq);
James Smart2e0fef82007-06-17 19:56:36 -05002178 piocbq->vport = vport;
2179
dea31012005-04-17 16:05:31 -05002180 piocb = &piocbq->iocb;
2181
2182 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart34b02dc2008-08-24 21:49:55 -04002183 /* Clear out any old data in the FCP command area */
2184 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
2185 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05002186 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
James Smarte2a0a9d2008-12-04 22:40:02 -05002187 if (vport->phba->sli_rev == 3 &&
2188 !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04002189 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05002190 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
dea31012005-04-17 16:05:31 -05002191 piocb->ulpContext = ndlp->nlp_rpi;
2192 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
2193 piocb->ulpFCP2Rcvy = 1;
2194 }
2195 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
2196
2197 /* ulpTimeout is only one byte */
2198 if (lpfc_cmd->timeout > 0xff) {
2199 /*
2200 * Do not timeout the command at the firmware level.
2201 * The driver will provide the timeout mechanism.
2202 */
2203 piocb->ulpTimeout = 0;
2204 } else {
2205 piocb->ulpTimeout = lpfc_cmd->timeout;
2206 }
2207
James Smart2e0fef82007-06-17 19:56:36 -05002208 return 1;
dea31012005-04-17 16:05:31 -05002209}
2210
James Smart9bad7672008-12-04 22:39:02 -05002211/**
James Smart3772a992009-05-22 14:50:54 -04002212 * lpfc_scsi_prep_task_mgmt_cmnd - Wrapper func convert scsi TM cmd to FCP info
2213 * @vport: The virtual port for which this call is being executed.
2214 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
2215 * @lun: Logical unit number.
2216 * @task_mgmt_cmd: SCSI task management command.
2217 *
2218 * This routine wraps the actual convert SCSI TM to FCP information unit
2219 * function pointer from the lpfc_hba struct.
2220 *
2221 * Return codes:
2222 * 0 - Error
2223 * 1 - Success
2224 **/
2225static inline int
2226lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
2227 struct lpfc_scsi_buf *lpfc_cmd,
2228 unsigned int lun,
2229 uint8_t task_mgmt_cmd)
2230{
2231 struct lpfc_hba *phba = vport->phba;
2232
2233 return phba->lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun,
2234 task_mgmt_cmd);
2235}
2236
2237/**
2238 * lpfc_scsi_api_table_setup - Set up scsi api fucntion jump table
2239 * @phba: The hba struct for which this call is being executed.
2240 * @dev_grp: The HBA PCI-Device group number.
2241 *
2242 * This routine sets up the SCSI interface API function jump table in @phba
2243 * struct.
2244 * Returns: 0 - success, -ENODEV - failure.
2245 **/
2246int
2247lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
2248{
2249
2250 switch (dev_grp) {
2251 case LPFC_PCI_DEV_LP:
2252 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s3;
2253 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
2254 phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd_s3;
2255 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf_s3;
2256 phba->lpfc_scsi_prep_task_mgmt_cmd =
2257 lpfc_scsi_prep_task_mgmt_cmd_s3;
2258 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
2259 break;
2260 default:
2261 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2262 "1418 Invalid HBA PCI-device group: 0x%x\n",
2263 dev_grp);
2264 return -ENODEV;
2265 break;
2266 }
2267 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf;
2268 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
2269 return 0;
2270}
2271
2272/**
James Smart3621a712009-04-06 18:47:14 -04002273 * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
James Smart9bad7672008-12-04 22:39:02 -05002274 * @phba: The Hba for which this call is being executed.
2275 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
2276 * @rspiocbq: Pointer to lpfc_iocbq data structure.
2277 *
2278 * This routine is IOCB completion routine for device reset and target reset
2279 * routine. This routine release scsi buffer associated with lpfc_cmd.
2280 **/
James Smart7054a602007-04-25 09:52:34 -04002281static void
2282lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
2283 struct lpfc_iocbq *cmdiocbq,
2284 struct lpfc_iocbq *rspiocbq)
2285{
2286 struct lpfc_scsi_buf *lpfc_cmd =
2287 (struct lpfc_scsi_buf *) cmdiocbq->context1;
2288 if (lpfc_cmd)
2289 lpfc_release_scsi_buf(phba, lpfc_cmd);
2290 return;
2291}
2292
James Smart9bad7672008-12-04 22:39:02 -05002293/**
James Smart3621a712009-04-06 18:47:14 -04002294 * lpfc_scsi_tgt_reset - Target reset handler
James Smart9bad7672008-12-04 22:39:02 -05002295 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure
2296 * @vport: The virtual port for which this call is being executed.
2297 * @tgt_id: Target ID.
2298 * @lun: Lun number.
2299 * @rdata: Pointer to lpfc_rport_data.
2300 *
2301 * This routine issues a TARGET RESET iocb to reset a target with @tgt_id ID.
2302 *
2303 * Return Code:
2304 * 0x2003 - Error
2305 * 0x2002 - Success.
2306 **/
dea31012005-04-17 16:05:31 -05002307static int
James Smart2e0fef82007-06-17 19:56:36 -05002308lpfc_scsi_tgt_reset(struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_vport *vport,
James Smart420b630d2006-07-06 15:50:16 -04002309 unsigned tgt_id, unsigned int lun,
2310 struct lpfc_rport_data *rdata)
dea31012005-04-17 16:05:31 -05002311{
James Smart2e0fef82007-06-17 19:56:36 -05002312 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002313 struct lpfc_iocbq *iocbq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002314 struct lpfc_iocbq *iocbqrsp;
dea31012005-04-17 16:05:31 -05002315 int ret;
James Smart915caaa2008-06-14 22:52:38 -04002316 int status;
dea31012005-04-17 16:05:31 -05002317
James Smart58da1ff2008-04-07 10:15:56 -04002318 if (!rdata->pnode || !NLP_CHK_NODE_ACT(rdata->pnode))
James Smartf5603512006-12-02 13:35:43 -05002319 return FAILED;
2320
James Smart0b18ac42006-05-01 21:50:40 -04002321 lpfc_cmd->rdata = rdata;
James Smart915caaa2008-06-14 22:52:38 -04002322 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun,
James Smart420b630d2006-07-06 15:50:16 -04002323 FCP_TARGET_RESET);
James Smart915caaa2008-06-14 22:52:38 -04002324 if (!status)
dea31012005-04-17 16:05:31 -05002325 return FAILED;
2326
dea31012005-04-17 16:05:31 -05002327 iocbq = &lpfc_cmd->cur_iocbq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002328 iocbqrsp = lpfc_sli_get_iocbq(phba);
2329
dea31012005-04-17 16:05:31 -05002330 if (!iocbqrsp)
2331 return FAILED;
dea31012005-04-17 16:05:31 -05002332
James Smart0b18ac42006-05-01 21:50:40 -04002333 /* Issue Target Reset to TGT <num> */
James Smarte8b62012007-08-02 11:10:09 -04002334 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
2335 "0702 Issue Target Reset to TGT %d Data: x%x x%x\n",
2336 tgt_id, rdata->pnode->nlp_rpi, rdata->pnode->nlp_flag);
James Smart3772a992009-05-22 14:50:54 -04002337 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
2338 iocbq, iocbqrsp, lpfc_cmd->timeout);
James Smart915caaa2008-06-14 22:52:38 -04002339 if (status != IOCB_SUCCESS) {
2340 if (status == IOCB_TIMEDOUT) {
James Smart7054a602007-04-25 09:52:34 -04002341 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
James Smart915caaa2008-06-14 22:52:38 -04002342 ret = TIMEOUT_ERROR;
2343 } else
2344 ret = FAILED;
dea31012005-04-17 16:05:31 -05002345 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
dea31012005-04-17 16:05:31 -05002346 } else {
2347 ret = SUCCESS;
2348 lpfc_cmd->result = iocbqrsp->iocb.un.ulpWord[4];
2349 lpfc_cmd->status = iocbqrsp->iocb.ulpStatus;
2350 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
2351 (lpfc_cmd->result & IOERR_DRVR_MASK))
2352 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
2353 }
2354
James Bottomley604a3e32005-10-29 10:28:33 -05002355 lpfc_sli_release_iocbq(phba, iocbqrsp);
dea31012005-04-17 16:05:31 -05002356 return ret;
2357}
2358
James Smart9bad7672008-12-04 22:39:02 -05002359/**
James Smart3621a712009-04-06 18:47:14 -04002360 * lpfc_info - Info entry point of scsi_host_template data structure
James Smart9bad7672008-12-04 22:39:02 -05002361 * @host: The scsi host for which this call is being executed.
2362 *
2363 * This routine provides module information about hba.
2364 *
2365 * Reutrn code:
2366 * Pointer to char - Success.
2367 **/
dea31012005-04-17 16:05:31 -05002368const char *
2369lpfc_info(struct Scsi_Host *host)
2370{
James Smart2e0fef82007-06-17 19:56:36 -05002371 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
2372 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002373 int len;
2374 static char lpfcinfobuf[384];
2375
2376 memset(lpfcinfobuf,0,384);
2377 if (phba && phba->pcidev){
2378 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
2379 len = strlen(lpfcinfobuf);
2380 snprintf(lpfcinfobuf + len,
2381 384-len,
2382 " on PCI bus %02x device %02x irq %d",
2383 phba->pcidev->bus->number,
2384 phba->pcidev->devfn,
2385 phba->pcidev->irq);
2386 len = strlen(lpfcinfobuf);
2387 if (phba->Port[0]) {
2388 snprintf(lpfcinfobuf + len,
2389 384-len,
2390 " port %s",
2391 phba->Port);
2392 }
2393 }
2394 return lpfcinfobuf;
2395}
2396
James Smart9bad7672008-12-04 22:39:02 -05002397/**
James Smart3621a712009-04-06 18:47:14 -04002398 * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
James Smart9bad7672008-12-04 22:39:02 -05002399 * @phba: The Hba for which this call is being executed.
2400 *
2401 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
2402 * The default value of cfg_poll_tmo is 10 milliseconds.
2403 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002404static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
2405{
2406 unsigned long poll_tmo_expires =
2407 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
2408
2409 if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
2410 mod_timer(&phba->fcp_poll_timer,
2411 poll_tmo_expires);
2412}
2413
James Smart9bad7672008-12-04 22:39:02 -05002414/**
James Smart3621a712009-04-06 18:47:14 -04002415 * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
James Smart9bad7672008-12-04 22:39:02 -05002416 * @phba: The Hba for which this call is being executed.
2417 *
2418 * This routine starts the fcp_poll_timer of @phba.
2419 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002420void lpfc_poll_start_timer(struct lpfc_hba * phba)
2421{
2422 lpfc_poll_rearm_timer(phba);
2423}
2424
James Smart9bad7672008-12-04 22:39:02 -05002425/**
James Smart3621a712009-04-06 18:47:14 -04002426 * lpfc_poll_timeout - Restart polling timer
James Smart9bad7672008-12-04 22:39:02 -05002427 * @ptr: Map to lpfc_hba data structure pointer.
2428 *
2429 * This routine restarts fcp_poll timer, when FCP ring polling is enable
2430 * and FCP Ring interrupt is disable.
2431 **/
2432
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002433void lpfc_poll_timeout(unsigned long ptr)
2434{
James Smart2e0fef82007-06-17 19:56:36 -05002435 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002436
2437 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
2438 lpfc_sli_poll_fcp_ring (phba);
2439 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
2440 lpfc_poll_rearm_timer(phba);
2441 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002442}
2443
James Smart9bad7672008-12-04 22:39:02 -05002444/**
James Smart3621a712009-04-06 18:47:14 -04002445 * lpfc_queuecommand - scsi_host_template queuecommand entry point
James Smart9bad7672008-12-04 22:39:02 -05002446 * @cmnd: Pointer to scsi_cmnd data structure.
2447 * @done: Pointer to done routine.
2448 *
2449 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
2450 * This routine prepares an IOCB from scsi command and provides to firmware.
2451 * The @done callback is invoked after driver finished processing the command.
2452 *
2453 * Return value :
2454 * 0 - Success
2455 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
2456 **/
dea31012005-04-17 16:05:31 -05002457static int
2458lpfc_queuecommand(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
2459{
James Smart2e0fef82007-06-17 19:56:36 -05002460 struct Scsi_Host *shost = cmnd->device->host;
2461 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2462 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002463 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
2464 struct lpfc_nodelist *ndlp = rdata->pnode;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002465 struct lpfc_scsi_buf *lpfc_cmd;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002466 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002467 int err;
dea31012005-04-17 16:05:31 -05002468
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002469 err = fc_remote_port_chkready(rport);
2470 if (err) {
2471 cmnd->result = err;
dea31012005-04-17 16:05:31 -05002472 goto out_fail_command;
2473 }
2474
James Smarte2a0a9d2008-12-04 22:40:02 -05002475 if (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
2476 scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
2477
2478 printk(KERN_ERR "BLKGRD ERROR: rcvd protected cmd:%02x op:%02x "
2479 "str=%s without registering for BlockGuard - "
2480 "Rejecting command\n",
2481 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
2482 dif_op_str[scsi_get_prot_op(cmnd)]);
2483 goto out_fail_command;
2484 }
2485
dea31012005-04-17 16:05:31 -05002486 /*
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002487 * Catch race where our node has transitioned, but the
2488 * transport is still transitioning.
dea31012005-04-17 16:05:31 -05002489 */
James Smartb522d7d2008-09-07 11:51:56 -04002490 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
2491 cmnd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
2492 goto out_fail_command;
2493 }
James Smart109f6ed2008-12-04 22:39:08 -05002494 if (vport->cfg_max_scsicmpl_time &&
2495 (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth))
James Smart977b5a02008-09-07 11:52:04 -04002496 goto out_host_busy;
Mike Christiea93ce022008-08-17 15:24:41 -05002497
James Smarted957682007-06-17 19:56:37 -05002498 lpfc_cmd = lpfc_get_scsi_buf(phba);
dea31012005-04-17 16:05:31 -05002499 if (lpfc_cmd == NULL) {
James Smarteaf15d52008-12-04 22:39:29 -05002500 lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002501
James Smarte8b62012007-08-02 11:10:09 -04002502 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
2503 "0707 driver's buffer pool is empty, "
2504 "IO busied\n");
dea31012005-04-17 16:05:31 -05002505 goto out_host_busy;
2506 }
2507
2508 /*
2509 * Store the midlayer's command structure for the completion phase
2510 * and complete the command initialization.
2511 */
2512 lpfc_cmd->pCmd = cmnd;
2513 lpfc_cmd->rdata = rdata;
2514 lpfc_cmd->timeout = 0;
James Smart977b5a02008-09-07 11:52:04 -04002515 lpfc_cmd->start_time = jiffies;
dea31012005-04-17 16:05:31 -05002516 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
2517 cmnd->scsi_done = done;
2518
James Smarte2a0a9d2008-12-04 22:40:02 -05002519 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
2520 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
2521 "9033 BLKGRD: rcvd protected cmd:%02x op:%02x "
2522 "str=%s\n",
2523 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
2524 dif_op_str[scsi_get_prot_op(cmnd)]);
2525 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
2526 "9034 BLKGRD: CDB: %02x %02x %02x %02x %02x "
2527 "%02x %02x %02x %02x %02x \n",
2528 cmnd->cmnd[0], cmnd->cmnd[1], cmnd->cmnd[2],
2529 cmnd->cmnd[3], cmnd->cmnd[4], cmnd->cmnd[5],
2530 cmnd->cmnd[6], cmnd->cmnd[7], cmnd->cmnd[8],
2531 cmnd->cmnd[9]);
2532 if (cmnd->cmnd[0] == READ_10)
2533 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
2534 "9035 BLKGRD: READ @ sector %llu, "
2535 "count %lu\n",
James Smart87b5c322008-12-16 10:34:09 -05002536 (unsigned long long)scsi_get_lba(cmnd),
James Smarte2a0a9d2008-12-04 22:40:02 -05002537 cmnd->request->nr_sectors);
2538 else if (cmnd->cmnd[0] == WRITE_10)
2539 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
2540 "9036 BLKGRD: WRITE @ sector %llu, "
2541 "count %lu cmd=%p\n",
James Smart87b5c322008-12-16 10:34:09 -05002542 (unsigned long long)scsi_get_lba(cmnd),
James Smarte2a0a9d2008-12-04 22:40:02 -05002543 cmnd->request->nr_sectors,
2544 cmnd);
2545
2546 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
2547 } else {
2548 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
2549 "9038 BLKGRD: rcvd unprotected cmd:%02x op:%02x"
2550 " str=%s\n",
2551 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
2552 dif_op_str[scsi_get_prot_op(cmnd)]);
2553 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
2554 "9039 BLKGRD: CDB: %02x %02x %02x %02x %02x "
2555 "%02x %02x %02x %02x %02x \n",
2556 cmnd->cmnd[0], cmnd->cmnd[1], cmnd->cmnd[2],
2557 cmnd->cmnd[3], cmnd->cmnd[4], cmnd->cmnd[5],
2558 cmnd->cmnd[6], cmnd->cmnd[7], cmnd->cmnd[8],
2559 cmnd->cmnd[9]);
2560 if (cmnd->cmnd[0] == READ_10)
2561 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
2562 "9040 dbg: READ @ sector %llu, "
James Smart87b5c322008-12-16 10:34:09 -05002563 "count %lu\n",
2564 (unsigned long long)scsi_get_lba(cmnd),
James Smarte2a0a9d2008-12-04 22:40:02 -05002565 cmnd->request->nr_sectors);
2566 else if (cmnd->cmnd[0] == WRITE_10)
2567 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
2568 "9041 dbg: WRITE @ sector %llu, "
2569 "count %lu cmd=%p\n",
James Smart87b5c322008-12-16 10:34:09 -05002570 (unsigned long long)scsi_get_lba(cmnd),
James Smarte2a0a9d2008-12-04 22:40:02 -05002571 cmnd->request->nr_sectors, cmnd);
2572 else
2573 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
2574 "9042 dbg: parser not implemented\n");
2575 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
2576 }
2577
dea31012005-04-17 16:05:31 -05002578 if (err)
2579 goto out_host_busy_free_buf;
2580
James Smart2e0fef82007-06-17 19:56:36 -05002581 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea31012005-04-17 16:05:31 -05002582
James Smart977b5a02008-09-07 11:52:04 -04002583 atomic_inc(&ndlp->cmd_pending);
James Smart3772a992009-05-22 14:50:54 -04002584 err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
James Smart92d7f7b2007-06-17 19:56:38 -05002585 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
James Smarteaf15d52008-12-04 22:39:29 -05002586 if (err) {
2587 atomic_dec(&ndlp->cmd_pending);
dea31012005-04-17 16:05:31 -05002588 goto out_host_busy_free_buf;
James Smarteaf15d52008-12-04 22:39:29 -05002589 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002590 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
2591 lpfc_sli_poll_fcp_ring(phba);
2592 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
2593 lpfc_poll_rearm_timer(phba);
2594 }
2595
dea31012005-04-17 16:05:31 -05002596 return 0;
2597
2598 out_host_busy_free_buf:
James Smartbcf4dbf2006-07-06 15:50:08 -04002599 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002600 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05002601 out_host_busy:
2602 return SCSI_MLQUEUE_HOST_BUSY;
2603
2604 out_fail_command:
2605 done(cmnd);
2606 return 0;
2607}
2608
James Smart9bad7672008-12-04 22:39:02 -05002609/**
James Smart3621a712009-04-06 18:47:14 -04002610 * lpfc_block_error_handler - Routine to block error handler
James Smart9bad7672008-12-04 22:39:02 -05002611 * @cmnd: Pointer to scsi_cmnd data structure.
2612 *
2613 * This routine blocks execution till fc_rport state is not FC_PORSTAT_BLCOEKD.
2614 **/
James Smarta90f5682006-08-17 11:58:04 -04002615static void
2616lpfc_block_error_handler(struct scsi_cmnd *cmnd)
2617{
2618 struct Scsi_Host *shost = cmnd->device->host;
2619 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
2620
2621 spin_lock_irq(shost->host_lock);
2622 while (rport->port_state == FC_PORTSTATE_BLOCKED) {
2623 spin_unlock_irq(shost->host_lock);
2624 msleep(1000);
2625 spin_lock_irq(shost->host_lock);
2626 }
2627 spin_unlock_irq(shost->host_lock);
2628 return;
2629}
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05002630
James Smart9bad7672008-12-04 22:39:02 -05002631/**
James Smart3621a712009-04-06 18:47:14 -04002632 * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05002633 * @cmnd: Pointer to scsi_cmnd data structure.
2634 *
2635 * This routine aborts @cmnd pending in base driver.
2636 *
2637 * Return code :
2638 * 0x2003 - Error
2639 * 0x2002 - Success
2640 **/
dea31012005-04-17 16:05:31 -05002641static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05002642lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05002643{
James Smart2e0fef82007-06-17 19:56:36 -05002644 struct Scsi_Host *shost = cmnd->device->host;
2645 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2646 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002647 struct lpfc_iocbq *iocb;
2648 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05002649 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05002650 IOCB_t *cmd, *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002651 int ret = SUCCESS;
James Smartfa61a542008-01-11 01:52:42 -05002652 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002653
James Smarta90f5682006-08-17 11:58:04 -04002654 lpfc_block_error_handler(cmnd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002655 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
2656 BUG_ON(!lpfc_cmd);
dea31012005-04-17 16:05:31 -05002657
2658 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002659 * If pCmd field of the corresponding lpfc_scsi_buf structure
2660 * points to a different SCSI command, then the driver has
2661 * already completed this command, but the midlayer did not
2662 * see the completion before the eh fired. Just return
2663 * SUCCESS.
dea31012005-04-17 16:05:31 -05002664 */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002665 iocb = &lpfc_cmd->cur_iocbq;
2666 if (lpfc_cmd->pCmd != cmnd)
2667 goto out;
dea31012005-04-17 16:05:31 -05002668
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002669 BUG_ON(iocb->context1 != lpfc_cmd);
dea31012005-04-17 16:05:31 -05002670
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002671 abtsiocb = lpfc_sli_get_iocbq(phba);
2672 if (abtsiocb == NULL) {
2673 ret = FAILED;
dea31012005-04-17 16:05:31 -05002674 goto out;
2675 }
2676
dea31012005-04-17 16:05:31 -05002677 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002678 * The scsi command can not be in txq and it is in flight because the
2679 * pCmd is still pointig at the SCSI command we have to abort. There
2680 * is no need to search the txcmplq. Just send an abort to the FW.
dea31012005-04-17 16:05:31 -05002681 */
dea31012005-04-17 16:05:31 -05002682
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002683 cmd = &iocb->iocb;
2684 icmd = &abtsiocb->iocb;
2685 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
2686 icmd->un.acxri.abortContextTag = cmd->ulpContext;
James Smart3772a992009-05-22 14:50:54 -04002687 if (phba->sli_rev == LPFC_SLI_REV4)
2688 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
2689 else
2690 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05002691
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002692 icmd->ulpLe = 1;
2693 icmd->ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05002694 if (lpfc_is_link_up(phba))
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002695 icmd->ulpCommand = CMD_ABORT_XRI_CN;
2696 else
2697 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea31012005-04-17 16:05:31 -05002698
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002699 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smart2e0fef82007-06-17 19:56:36 -05002700 abtsiocb->vport = vport;
James Smart3772a992009-05-22 14:50:54 -04002701 if (lpfc_sli_issue_iocb(phba, LPFC_FCP_RING, abtsiocb, 0) ==
2702 IOCB_ERROR) {
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002703 lpfc_sli_release_iocbq(phba, abtsiocb);
2704 ret = FAILED;
2705 goto out;
2706 }
2707
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002708 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
2709 lpfc_sli_poll_fcp_ring (phba);
2710
James Smartfa61a542008-01-11 01:52:42 -05002711 lpfc_cmd->waitq = &waitq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002712 /* Wait for abort to complete */
James Smartfa61a542008-01-11 01:52:42 -05002713 wait_event_timeout(waitq,
2714 (lpfc_cmd->pCmd != cmnd),
2715 (2*vport->cfg_devloss_tmo*HZ));
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002716
James Smartfa61a542008-01-11 01:52:42 -05002717 spin_lock_irq(shost->host_lock);
2718 lpfc_cmd->waitq = NULL;
2719 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002720
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002721 if (lpfc_cmd->pCmd == cmnd) {
2722 ret = FAILED;
James Smarte8b62012007-08-02 11:10:09 -04002723 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
2724 "0748 abort handler timed out waiting "
2725 "for abort to complete: ret %#x, ID %d, "
2726 "LUN %d, snum %#lx\n",
2727 ret, cmnd->device->id, cmnd->device->lun,
2728 cmnd->serial_number);
dea31012005-04-17 16:05:31 -05002729 }
2730
2731 out:
James Smarte8b62012007-08-02 11:10:09 -04002732 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
2733 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
2734 "LUN %d snum %#lx\n", ret, cmnd->device->id,
2735 cmnd->device->lun, cmnd->serial_number);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002736 return ret;
dea31012005-04-17 16:05:31 -05002737}
2738
James Smart9bad7672008-12-04 22:39:02 -05002739/**
James Smart3621a712009-04-06 18:47:14 -04002740 * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
James Smart9bad7672008-12-04 22:39:02 -05002741 * @cmnd: Pointer to scsi_cmnd data structure.
2742 *
2743 * This routine does a device reset by sending a TARGET_RESET task management
2744 * command.
2745 *
2746 * Return code :
2747 * 0x2003 - Error
James Smart3621a712009-04-06 18:47:14 -04002748 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05002749 **/
dea31012005-04-17 16:05:31 -05002750static int
James Smart7054a602007-04-25 09:52:34 -04002751lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05002752{
James Smart2e0fef82007-06-17 19:56:36 -05002753 struct Scsi_Host *shost = cmnd->device->host;
2754 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2755 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002756 struct lpfc_scsi_buf *lpfc_cmd;
2757 struct lpfc_iocbq *iocbq, *iocbqrsp;
dea31012005-04-17 16:05:31 -05002758 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
2759 struct lpfc_nodelist *pnode = rdata->pnode;
James Smart915caaa2008-06-14 22:52:38 -04002760 unsigned long later;
2761 int ret = SUCCESS;
2762 int status;
2763 int cnt;
James Smartea2151b2008-09-07 11:52:10 -04002764 struct lpfc_scsi_event_header scsi_event;
dea31012005-04-17 16:05:31 -05002765
James Smarta90f5682006-08-17 11:58:04 -04002766 lpfc_block_error_handler(cmnd);
dea31012005-04-17 16:05:31 -05002767 /*
2768 * If target is not in a MAPPED state, delay the reset until
James Smartc01f3202006-08-18 17:47:08 -04002769 * target is rediscovered or devloss timeout expires.
dea31012005-04-17 16:05:31 -05002770 */
James Smart915caaa2008-06-14 22:52:38 -04002771 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
2772 while (time_after(later, jiffies)) {
James Smart58da1ff2008-04-07 10:15:56 -04002773 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
James Smart915caaa2008-06-14 22:52:38 -04002774 return FAILED;
James Smartf5603512006-12-02 13:35:43 -05002775 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05002776 break;
James Smart915caaa2008-06-14 22:52:38 -04002777 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
2778 rdata = cmnd->device->hostdata;
2779 if (!rdata)
2780 break;
2781 pnode = rdata->pnode;
dea31012005-04-17 16:05:31 -05002782 }
James Smartea2151b2008-09-07 11:52:10 -04002783
2784 scsi_event.event_type = FC_REG_SCSI_EVENT;
2785 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
2786 scsi_event.lun = 0;
2787 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
2788 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
2789
2790 fc_host_post_vendor_event(shost,
2791 fc_get_event_number(),
2792 sizeof(scsi_event),
2793 (char *)&scsi_event,
James Smartddcc50f2008-12-04 22:38:46 -05002794 LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -04002795
James Smart915caaa2008-06-14 22:52:38 -04002796 if (!rdata || pnode->nlp_state != NLP_STE_MAPPED_NODE) {
2797 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
2798 "0721 LUN Reset rport "
2799 "failure: msec x%x rdata x%p\n",
2800 jiffies_to_msecs(jiffies - later), rdata);
2801 return FAILED;
2802 }
James Smart2e0fef82007-06-17 19:56:36 -05002803 lpfc_cmd = lpfc_get_scsi_buf(phba);
dea31012005-04-17 16:05:31 -05002804 if (lpfc_cmd == NULL)
James Smart915caaa2008-06-14 22:52:38 -04002805 return FAILED;
dea31012005-04-17 16:05:31 -05002806 lpfc_cmd->timeout = 60;
James Smart0b18ac42006-05-01 21:50:40 -04002807 lpfc_cmd->rdata = rdata;
dea31012005-04-17 16:05:31 -05002808
James Smart915caaa2008-06-14 22:52:38 -04002809 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd,
2810 cmnd->device->lun,
2811 FCP_TARGET_RESET);
2812 if (!status) {
2813 lpfc_release_scsi_buf(phba, lpfc_cmd);
2814 return FAILED;
2815 }
dea31012005-04-17 16:05:31 -05002816 iocbq = &lpfc_cmd->cur_iocbq;
2817
2818 /* get a buffer for this IOCB command response */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002819 iocbqrsp = lpfc_sli_get_iocbq(phba);
James Smart915caaa2008-06-14 22:52:38 -04002820 if (iocbqrsp == NULL) {
2821 lpfc_release_scsi_buf(phba, lpfc_cmd);
2822 return FAILED;
2823 }
James Smarte8b62012007-08-02 11:10:09 -04002824 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
2825 "0703 Issue target reset to TGT %d LUN %d "
2826 "rpi x%x nlp_flag x%x\n", cmnd->device->id,
2827 cmnd->device->lun, pnode->nlp_rpi, pnode->nlp_flag);
James Smart3772a992009-05-22 14:50:54 -04002828 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
James Smart915caaa2008-06-14 22:52:38 -04002829 iocbq, iocbqrsp, lpfc_cmd->timeout);
2830 if (status == IOCB_TIMEDOUT) {
James Smart7054a602007-04-25 09:52:34 -04002831 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
James Smart915caaa2008-06-14 22:52:38 -04002832 ret = TIMEOUT_ERROR;
2833 } else {
2834 if (status != IOCB_SUCCESS)
2835 ret = FAILED;
James Smart7054a602007-04-25 09:52:34 -04002836 lpfc_release_scsi_buf(phba, lpfc_cmd);
2837 }
James Smarte8b62012007-08-02 11:10:09 -04002838 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
2839 "0713 SCSI layer issued device reset (%d, %d) "
2840 "return x%x status x%x result x%x\n",
2841 cmnd->device->id, cmnd->device->lun, ret,
James Smart915caaa2008-06-14 22:52:38 -04002842 iocbqrsp->iocb.ulpStatus,
2843 iocbqrsp->iocb.un.ulpWord[4]);
2844 lpfc_sli_release_iocbq(phba, iocbqrsp);
2845 cnt = lpfc_sli_sum_iocb(vport, cmnd->device->id, cmnd->device->lun,
2846 LPFC_CTX_TGT);
2847 if (cnt)
2848 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
2849 cmnd->device->id, cmnd->device->lun,
2850 LPFC_CTX_TGT);
2851 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
2852 while (time_after(later, jiffies) && cnt) {
2853 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
2854 cnt = lpfc_sli_sum_iocb(vport, cmnd->device->id,
2855 cmnd->device->lun, LPFC_CTX_TGT);
2856 }
2857 if (cnt) {
2858 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
2859 "0719 device reset I/O flush failure: "
2860 "cnt x%x\n", cnt);
2861 ret = FAILED;
2862 }
dea31012005-04-17 16:05:31 -05002863 return ret;
2864}
2865
James Smart9bad7672008-12-04 22:39:02 -05002866/**
James Smart3621a712009-04-06 18:47:14 -04002867 * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05002868 * @cmnd: Pointer to scsi_cmnd data structure.
2869 *
2870 * This routine does target reset to all target on @cmnd->device->host.
2871 *
2872 * Return Code:
2873 * 0x2003 - Error
2874 * 0x2002 - Success
2875 **/
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04002876static int
James Smart7054a602007-04-25 09:52:34 -04002877lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05002878{
James Smart2e0fef82007-06-17 19:56:36 -05002879 struct Scsi_Host *shost = cmnd->device->host;
2880 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2881 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002882 struct lpfc_nodelist *ndlp = NULL;
2883 int match;
James Smartd7c255b2008-08-24 21:50:00 -04002884 int ret = SUCCESS, status = SUCCESS, i;
James Smart915caaa2008-06-14 22:52:38 -04002885 int cnt;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002886 struct lpfc_scsi_buf * lpfc_cmd;
James Smart915caaa2008-06-14 22:52:38 -04002887 unsigned long later;
James Smartea2151b2008-09-07 11:52:10 -04002888 struct lpfc_scsi_event_header scsi_event;
2889
2890 scsi_event.event_type = FC_REG_SCSI_EVENT;
2891 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
2892 scsi_event.lun = 0;
2893 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
2894 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
2895
2896 fc_host_post_vendor_event(shost,
2897 fc_get_event_number(),
2898 sizeof(scsi_event),
2899 (char *)&scsi_event,
James Smartddcc50f2008-12-04 22:38:46 -05002900 LPFC_NL_VENDOR_ID);
dea31012005-04-17 16:05:31 -05002901
James Smarta90f5682006-08-17 11:58:04 -04002902 lpfc_block_error_handler(cmnd);
dea31012005-04-17 16:05:31 -05002903 /*
2904 * Since the driver manages a single bus device, reset all
2905 * targets known to the driver. Should any target reset
2906 * fail, this routine returns failure to the midlayer.
2907 */
James Smarte17da182006-07-06 15:49:25 -04002908 for (i = 0; i < LPFC_MAX_TARGET; i++) {
James Smart685f0bf2007-04-25 09:53:08 -04002909 /* Search for mapped node by target ID */
dea31012005-04-17 16:05:31 -05002910 match = 0;
James Smart2e0fef82007-06-17 19:56:36 -05002911 spin_lock_irq(shost->host_lock);
2912 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002913 if (!NLP_CHK_NODE_ACT(ndlp))
2914 continue;
James Smart685f0bf2007-04-25 09:53:08 -04002915 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
James Smart915caaa2008-06-14 22:52:38 -04002916 ndlp->nlp_sid == i &&
James Smart685f0bf2007-04-25 09:53:08 -04002917 ndlp->rport) {
dea31012005-04-17 16:05:31 -05002918 match = 1;
2919 break;
2920 }
2921 }
James Smart2e0fef82007-06-17 19:56:36 -05002922 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002923 if (!match)
2924 continue;
James Smart915caaa2008-06-14 22:52:38 -04002925 lpfc_cmd = lpfc_get_scsi_buf(phba);
2926 if (lpfc_cmd) {
2927 lpfc_cmd->timeout = 60;
2928 status = lpfc_scsi_tgt_reset(lpfc_cmd, vport, i,
2929 cmnd->device->lun,
2930 ndlp->rport->dd_data);
2931 if (status != TIMEOUT_ERROR)
2932 lpfc_release_scsi_buf(phba, lpfc_cmd);
2933 }
2934 if (!lpfc_cmd || status != SUCCESS) {
James Smarte8b62012007-08-02 11:10:09 -04002935 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
2936 "0700 Bus Reset on target %d failed\n",
2937 i);
James Smart915caaa2008-06-14 22:52:38 -04002938 ret = FAILED;
dea31012005-04-17 16:05:31 -05002939 }
2940 }
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05002941 /*
2942 * All outstanding txcmplq I/Os should have been aborted by
2943 * the targets. Unfortunately, some targets do not abide by
2944 * this forcing the driver to double check.
2945 */
James Smart51ef4c22007-08-02 11:10:31 -04002946 cnt = lpfc_sli_sum_iocb(vport, 0, 0, LPFC_CTX_HOST);
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05002947 if (cnt)
James Smart51ef4c22007-08-02 11:10:31 -04002948 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
2949 0, 0, LPFC_CTX_HOST);
James Smart915caaa2008-06-14 22:52:38 -04002950 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
2951 while (time_after(later, jiffies) && cnt) {
2952 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
James Smart51ef4c22007-08-02 11:10:31 -04002953 cnt = lpfc_sli_sum_iocb(vport, 0, 0, LPFC_CTX_HOST);
dea31012005-04-17 16:05:31 -05002954 }
dea31012005-04-17 16:05:31 -05002955 if (cnt) {
James Smarte8b62012007-08-02 11:10:09 -04002956 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
2957 "0715 Bus Reset I/O flush failure: "
2958 "cnt x%x left x%x\n", cnt, i);
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05002959 ret = FAILED;
dea31012005-04-17 16:05:31 -05002960 }
James Smarte8b62012007-08-02 11:10:09 -04002961 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
2962 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea31012005-04-17 16:05:31 -05002963 return ret;
2964}
2965
James Smart9bad7672008-12-04 22:39:02 -05002966/**
James Smart3621a712009-04-06 18:47:14 -04002967 * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
James Smart9bad7672008-12-04 22:39:02 -05002968 * @sdev: Pointer to scsi_device.
2969 *
2970 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
2971 * globally available list of scsi buffers. This routine also makes sure scsi
2972 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
2973 * of scsi buffer exists for the lifetime of the driver.
2974 *
2975 * Return codes:
2976 * non-0 - Error
2977 * 0 - Success
2978 **/
dea31012005-04-17 16:05:31 -05002979static int
dea31012005-04-17 16:05:31 -05002980lpfc_slave_alloc(struct scsi_device *sdev)
2981{
James Smart2e0fef82007-06-17 19:56:36 -05002982 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
2983 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002984 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
James Smart3772a992009-05-22 14:50:54 -04002985 uint32_t total = 0;
dea31012005-04-17 16:05:31 -05002986 uint32_t num_to_alloc = 0;
James Smart3772a992009-05-22 14:50:54 -04002987 int num_allocated = 0;
dea31012005-04-17 16:05:31 -05002988
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002989 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05002990 return -ENXIO;
2991
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002992 sdev->hostdata = rport->dd_data;
dea31012005-04-17 16:05:31 -05002993
2994 /*
2995 * Populate the cmds_per_lun count scsi_bufs into this host's globally
2996 * available list of scsi buffers. Don't allocate more than the
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04002997 * HBA limit conveyed to the midlayer via the host structure. The
2998 * formula accounts for the lun_queue_depth + error handlers + 1
2999 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea31012005-04-17 16:05:31 -05003000 */
3001 total = phba->total_scsi_bufs;
James Smart3de2a652007-08-02 11:09:59 -04003002 num_to_alloc = vport->cfg_lun_queue_depth + 2;
James Smart92d7f7b2007-06-17 19:56:38 -05003003
3004 /* Allow some exchanges to be available always to complete discovery */
3005 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04003006 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3007 "0704 At limitation of %d preallocated "
3008 "command buffers\n", total);
dea31012005-04-17 16:05:31 -05003009 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05003010 /* Allow some exchanges to be available always to complete discovery */
3011 } else if (total + num_to_alloc >
3012 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04003013 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3014 "0705 Allocation request of %d "
3015 "command buffers will exceed max of %d. "
3016 "Reducing allocation request to %d.\n",
3017 num_to_alloc, phba->cfg_hba_queue_depth,
3018 (phba->cfg_hba_queue_depth - total));
dea31012005-04-17 16:05:31 -05003019 num_to_alloc = phba->cfg_hba_queue_depth - total;
3020 }
James Smart3772a992009-05-22 14:50:54 -04003021 num_allocated = lpfc_new_scsi_buf(vport, num_to_alloc);
3022 if (num_to_alloc != num_allocated) {
3023 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3024 "0708 Allocation request of %d "
3025 "command buffers did not succeed. "
3026 "Allocated %d buffers.\n",
3027 num_to_alloc, num_allocated);
dea31012005-04-17 16:05:31 -05003028 }
3029 return 0;
3030}
3031
James Smart9bad7672008-12-04 22:39:02 -05003032/**
James Smart3621a712009-04-06 18:47:14 -04003033 * lpfc_slave_configure - scsi_host_template slave_configure entry point
James Smart9bad7672008-12-04 22:39:02 -05003034 * @sdev: Pointer to scsi_device.
3035 *
3036 * This routine configures following items
3037 * - Tag command queuing support for @sdev if supported.
3038 * - Dev loss time out value of fc_rport.
3039 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
3040 *
3041 * Return codes:
3042 * 0 - Success
3043 **/
dea31012005-04-17 16:05:31 -05003044static int
3045lpfc_slave_configure(struct scsi_device *sdev)
3046{
James Smart2e0fef82007-06-17 19:56:36 -05003047 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
3048 struct lpfc_hba *phba = vport->phba;
3049 struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
dea31012005-04-17 16:05:31 -05003050
3051 if (sdev->tagged_supported)
James Smart3de2a652007-08-02 11:09:59 -04003052 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05003053 else
James Smart3de2a652007-08-02 11:09:59 -04003054 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05003055
3056 /*
3057 * Initialize the fc transport attributes for the target
3058 * containing this scsi device. Also note that the driver's
3059 * target pointer is stored in the starget_data for the
3060 * driver's sysfs entry point functions.
3061 */
James Smart3de2a652007-08-02 11:09:59 -04003062 rport->dev_loss_tmo = vport->cfg_devloss_tmo;
dea31012005-04-17 16:05:31 -05003063
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003064 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
3065 lpfc_sli_poll_fcp_ring(phba);
3066 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
3067 lpfc_poll_rearm_timer(phba);
3068 }
3069
dea31012005-04-17 16:05:31 -05003070 return 0;
3071}
3072
James Smart9bad7672008-12-04 22:39:02 -05003073/**
James Smart3621a712009-04-06 18:47:14 -04003074 * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
James Smart9bad7672008-12-04 22:39:02 -05003075 * @sdev: Pointer to scsi_device.
3076 *
3077 * This routine sets @sdev hostatdata filed to null.
3078 **/
dea31012005-04-17 16:05:31 -05003079static void
3080lpfc_slave_destroy(struct scsi_device *sdev)
3081{
3082 sdev->hostdata = NULL;
3083 return;
3084}
3085
James Smart92d7f7b2007-06-17 19:56:38 -05003086
dea31012005-04-17 16:05:31 -05003087struct scsi_host_template lpfc_template = {
3088 .module = THIS_MODULE,
3089 .name = LPFC_DRIVER_NAME,
3090 .info = lpfc_info,
3091 .queuecommand = lpfc_queuecommand,
3092 .eh_abort_handler = lpfc_abort_handler,
James Smart7054a602007-04-25 09:52:34 -04003093 .eh_device_reset_handler= lpfc_device_reset_handler,
3094 .eh_bus_reset_handler = lpfc_bus_reset_handler,
dea31012005-04-17 16:05:31 -05003095 .slave_alloc = lpfc_slave_alloc,
3096 .slave_configure = lpfc_slave_configure,
3097 .slave_destroy = lpfc_slave_destroy,
James Smart47a86172007-04-25 09:53:22 -04003098 .scan_finished = lpfc_scan_finished,
dea31012005-04-17 16:05:31 -05003099 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05003100 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
dea31012005-04-17 16:05:31 -05003101 .cmd_per_lun = LPFC_CMD_PER_LUN,
3102 .use_clustering = ENABLE_CLUSTERING,
James Smart2e0fef82007-06-17 19:56:36 -05003103 .shost_attrs = lpfc_hba_attrs,
James.Smart@Emulex.Com564b2962005-06-25 10:34:17 -04003104 .max_sectors = 0xFFFF,
dea31012005-04-17 16:05:31 -05003105};
James Smart3de2a652007-08-02 11:09:59 -04003106
3107struct scsi_host_template lpfc_vport_template = {
3108 .module = THIS_MODULE,
3109 .name = LPFC_DRIVER_NAME,
3110 .info = lpfc_info,
3111 .queuecommand = lpfc_queuecommand,
3112 .eh_abort_handler = lpfc_abort_handler,
3113 .eh_device_reset_handler= lpfc_device_reset_handler,
3114 .eh_bus_reset_handler = lpfc_bus_reset_handler,
3115 .slave_alloc = lpfc_slave_alloc,
3116 .slave_configure = lpfc_slave_configure,
3117 .slave_destroy = lpfc_slave_destroy,
3118 .scan_finished = lpfc_scan_finished,
3119 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05003120 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
James Smart3de2a652007-08-02 11:09:59 -04003121 .cmd_per_lun = LPFC_CMD_PER_LUN,
3122 .use_clustering = ENABLE_CLUSTERING,
3123 .shost_attrs = lpfc_vport_attrs,
3124 .max_sectors = 0xFFFF,
3125};