blob: bcddb6c1a1488ebe6415bf322a93dda091af2ba1 [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 Smartd8e93df2009-05-22 14:53:05 -04004 * Copyright (C) 2004-2009 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"
James Smartda0436e2009-05-22 14:51:39 -040034#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050035#include "lpfc_hw.h"
36#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040037#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040038#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050039#include "lpfc_disc.h"
40#include "lpfc_scsi.h"
41#include "lpfc.h"
42#include "lpfc_logmsg.h"
43#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050044#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050045
46#define LPFC_RESET_WAIT 2
47#define LPFC_ABORT_WAIT 2
48
James Smarte2a0a9d2008-12-04 22:40:02 -050049int _dump_buf_done;
50
51static char *dif_op_str[] = {
52 "SCSI_PROT_NORMAL",
53 "SCSI_PROT_READ_INSERT",
54 "SCSI_PROT_WRITE_STRIP",
55 "SCSI_PROT_READ_STRIP",
56 "SCSI_PROT_WRITE_INSERT",
57 "SCSI_PROT_READ_PASS",
58 "SCSI_PROT_WRITE_PASS",
James Smarte2a0a9d2008-12-04 22:40:02 -050059};
James Smartda0436e2009-05-22 14:51:39 -040060static void
61lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
James Smarte2a0a9d2008-12-04 22:40:02 -050062
63static void
James Smart6a9c52c2009-10-02 15:16:51 -040064lpfc_debug_save_data(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
James Smarte2a0a9d2008-12-04 22:40:02 -050065{
66 void *src, *dst;
67 struct scatterlist *sgde = scsi_sglist(cmnd);
68
69 if (!_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -040070 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
71 "9050 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
James Smarte2a0a9d2008-12-04 22:40:02 -050072 __func__);
73 return;
74 }
75
76
77 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -040078 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
79 "9051 BLKGRD: ERROR: data scatterlist is null\n");
James Smarte2a0a9d2008-12-04 22:40:02 -050080 return;
81 }
82
83 dst = (void *) _dump_buf_data;
84 while (sgde) {
85 src = sg_virt(sgde);
86 memcpy(dst, src, sgde->length);
87 dst += sgde->length;
88 sgde = sg_next(sgde);
89 }
90}
91
92static void
James Smart6a9c52c2009-10-02 15:16:51 -040093lpfc_debug_save_dif(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
James Smarte2a0a9d2008-12-04 22:40:02 -050094{
95 void *src, *dst;
96 struct scatterlist *sgde = scsi_prot_sglist(cmnd);
97
98 if (!_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -040099 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
100 "9052 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
James Smarte2a0a9d2008-12-04 22:40:02 -0500101 __func__);
102 return;
103 }
104
105 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -0400106 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
107 "9053 BLKGRD: ERROR: prot scatterlist is null\n");
James Smarte2a0a9d2008-12-04 22:40:02 -0500108 return;
109 }
110
111 dst = _dump_buf_dif;
112 while (sgde) {
113 src = sg_virt(sgde);
114 memcpy(dst, src, sgde->length);
115 dst += sgde->length;
116 sgde = sg_next(sgde);
117 }
118}
119
James Smartea2151b2008-09-07 11:52:10 -0400120/**
James Smartf1126682009-06-10 17:22:44 -0400121 * lpfc_sli4_set_rsp_sgl_last - Set the last bit in the response sge.
122 * @phba: Pointer to HBA object.
123 * @lpfc_cmd: lpfc scsi command object pointer.
124 *
125 * This function is called from the lpfc_prep_task_mgmt_cmd function to
126 * set the last bit in the response sge entry.
127 **/
128static void
129lpfc_sli4_set_rsp_sgl_last(struct lpfc_hba *phba,
130 struct lpfc_scsi_buf *lpfc_cmd)
131{
132 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
133 if (sgl) {
134 sgl += 1;
135 sgl->word2 = le32_to_cpu(sgl->word2);
136 bf_set(lpfc_sli4_sge_last, sgl, 1);
137 sgl->word2 = cpu_to_le32(sgl->word2);
138 }
139}
140
141/**
James Smart3621a712009-04-06 18:47:14 -0400142 * lpfc_update_stats - Update statistical data for the command completion
James Smartea2151b2008-09-07 11:52:10 -0400143 * @phba: Pointer to HBA object.
144 * @lpfc_cmd: lpfc scsi command object pointer.
145 *
146 * This function is called when there is a command completion and this
147 * function updates the statistical data for the command completion.
148 **/
149static void
150lpfc_update_stats(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
151{
152 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
153 struct lpfc_nodelist *pnode = rdata->pnode;
154 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
155 unsigned long flags;
156 struct Scsi_Host *shost = cmd->device->host;
157 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
158 unsigned long latency;
159 int i;
160
161 if (cmd->result)
162 return;
163
James Smart9f1e1b52008-12-04 22:39:40 -0500164 latency = jiffies_to_msecs((long)jiffies - (long)lpfc_cmd->start_time);
165
James Smartea2151b2008-09-07 11:52:10 -0400166 spin_lock_irqsave(shost->host_lock, flags);
167 if (!vport->stat_data_enabled ||
168 vport->stat_data_blocked ||
169 !pnode->lat_data ||
170 (phba->bucket_type == LPFC_NO_BUCKET)) {
171 spin_unlock_irqrestore(shost->host_lock, flags);
172 return;
173 }
James Smartea2151b2008-09-07 11:52:10 -0400174
175 if (phba->bucket_type == LPFC_LINEAR_BUCKET) {
176 i = (latency + phba->bucket_step - 1 - phba->bucket_base)/
177 phba->bucket_step;
James Smart9f1e1b52008-12-04 22:39:40 -0500178 /* check array subscript bounds */
179 if (i < 0)
180 i = 0;
181 else if (i >= LPFC_MAX_BUCKET_COUNT)
182 i = LPFC_MAX_BUCKET_COUNT - 1;
James Smartea2151b2008-09-07 11:52:10 -0400183 } else {
184 for (i = 0; i < LPFC_MAX_BUCKET_COUNT-1; i++)
185 if (latency <= (phba->bucket_base +
186 ((1<<i)*phba->bucket_step)))
187 break;
188 }
189
190 pnode->lat_data[i].cmd_count++;
191 spin_unlock_irqrestore(shost->host_lock, flags);
192}
193
James Smartea2151b2008-09-07 11:52:10 -0400194/**
James Smart3621a712009-04-06 18:47:14 -0400195 * lpfc_send_sdev_queuedepth_change_event - Posts a queuedepth change event
James Smartea2151b2008-09-07 11:52:10 -0400196 * @phba: Pointer to HBA context object.
197 * @vport: Pointer to vport object.
198 * @ndlp: Pointer to FC node associated with the target.
199 * @lun: Lun number of the scsi device.
200 * @old_val: Old value of the queue depth.
201 * @new_val: New value of the queue depth.
202 *
203 * This function sends an event to the mgmt application indicating
204 * there is a change in the scsi device queue depth.
205 **/
206static void
207lpfc_send_sdev_queuedepth_change_event(struct lpfc_hba *phba,
208 struct lpfc_vport *vport,
209 struct lpfc_nodelist *ndlp,
210 uint32_t lun,
211 uint32_t old_val,
212 uint32_t new_val)
213{
214 struct lpfc_fast_path_event *fast_path_evt;
215 unsigned long flags;
216
217 fast_path_evt = lpfc_alloc_fast_evt(phba);
218 if (!fast_path_evt)
219 return;
220
221 fast_path_evt->un.queue_depth_evt.scsi_event.event_type =
222 FC_REG_SCSI_EVENT;
223 fast_path_evt->un.queue_depth_evt.scsi_event.subcategory =
224 LPFC_EVENT_VARQUEDEPTH;
225
226 /* Report all luns with change in queue depth */
227 fast_path_evt->un.queue_depth_evt.scsi_event.lun = lun;
228 if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
229 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwpn,
230 &ndlp->nlp_portname, sizeof(struct lpfc_name));
231 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwnn,
232 &ndlp->nlp_nodename, sizeof(struct lpfc_name));
233 }
234
235 fast_path_evt->un.queue_depth_evt.oldval = old_val;
236 fast_path_evt->un.queue_depth_evt.newval = new_val;
237 fast_path_evt->vport = vport;
238
239 fast_path_evt->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
240 spin_lock_irqsave(&phba->hbalock, flags);
241 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
242 spin_unlock_irqrestore(&phba->hbalock, flags);
243 lpfc_worker_wake_up(phba);
244
245 return;
246}
247
James Smart9bad7672008-12-04 22:39:02 -0500248/**
James Smart3621a712009-04-06 18:47:14 -0400249 * lpfc_rampdown_queue_depth - Post RAMP_DOWN_QUEUE event to worker thread
James Smart9bad7672008-12-04 22:39:02 -0500250 * @phba: The Hba for which this call is being executed.
251 *
252 * This routine is called when there is resource error in driver or firmware.
253 * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
254 * posts at most 1 event each second. This routine wakes up worker thread of
255 * @phba to process WORKER_RAM_DOWN_EVENT event.
256 *
257 * This routine should be called with no lock held.
258 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500259void
James Smarteaf15d52008-12-04 22:39:29 -0500260lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
James Smart92d7f7b2007-06-17 19:56:38 -0500261{
262 unsigned long flags;
James Smart5e9d9b82008-06-14 22:52:53 -0400263 uint32_t evt_posted;
James Smart92d7f7b2007-06-17 19:56:38 -0500264
265 spin_lock_irqsave(&phba->hbalock, flags);
266 atomic_inc(&phba->num_rsrc_err);
267 phba->last_rsrc_error_time = jiffies;
268
269 if ((phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL) > jiffies) {
270 spin_unlock_irqrestore(&phba->hbalock, flags);
271 return;
272 }
273
274 phba->last_ramp_down_time = jiffies;
275
276 spin_unlock_irqrestore(&phba->hbalock, flags);
277
278 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400279 evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
280 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500281 phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500282 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
283
James Smart5e9d9b82008-06-14 22:52:53 -0400284 if (!evt_posted)
285 lpfc_worker_wake_up(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500286 return;
287}
288
James Smart9bad7672008-12-04 22:39:02 -0500289/**
James Smart3621a712009-04-06 18:47:14 -0400290 * lpfc_rampup_queue_depth - Post RAMP_UP_QUEUE event for worker thread
James Smart9bad7672008-12-04 22:39:02 -0500291 * @phba: The Hba for which this call is being executed.
292 *
293 * This routine post WORKER_RAMP_UP_QUEUE event for @phba vport. This routine
294 * post at most 1 event every 5 minute after last_ramp_up_time or
295 * last_rsrc_error_time. This routine wakes up worker thread of @phba
296 * to process WORKER_RAM_DOWN_EVENT event.
297 *
298 * This routine should be called with no lock held.
299 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500300static inline void
James Smart3de2a652007-08-02 11:09:59 -0400301lpfc_rampup_queue_depth(struct lpfc_vport *vport,
James Smarta257bf92009-04-06 18:48:10 -0400302 uint32_t queue_depth)
James Smart92d7f7b2007-06-17 19:56:38 -0500303{
304 unsigned long flags;
James Smart3de2a652007-08-02 11:09:59 -0400305 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -0400306 uint32_t evt_posted;
James Smart92d7f7b2007-06-17 19:56:38 -0500307 atomic_inc(&phba->num_cmd_success);
308
James Smarta257bf92009-04-06 18:48:10 -0400309 if (vport->cfg_lun_queue_depth <= queue_depth)
James Smart92d7f7b2007-06-17 19:56:38 -0500310 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500311 spin_lock_irqsave(&phba->hbalock, flags);
312 if (((phba->last_ramp_up_time + QUEUE_RAMP_UP_INTERVAL) > jiffies) ||
313 ((phba->last_rsrc_error_time + QUEUE_RAMP_UP_INTERVAL ) > jiffies)) {
314 spin_unlock_irqrestore(&phba->hbalock, flags);
315 return;
316 }
James Smart92d7f7b2007-06-17 19:56:38 -0500317 phba->last_ramp_up_time = jiffies;
318 spin_unlock_irqrestore(&phba->hbalock, flags);
319
320 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400321 evt_posted = phba->pport->work_port_events & WORKER_RAMP_UP_QUEUE;
322 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500323 phba->pport->work_port_events |= WORKER_RAMP_UP_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500324 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
325
James Smart5e9d9b82008-06-14 22:52:53 -0400326 if (!evt_posted)
327 lpfc_worker_wake_up(phba);
328 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500329}
330
James Smart9bad7672008-12-04 22:39:02 -0500331/**
James Smart3621a712009-04-06 18:47:14 -0400332 * lpfc_ramp_down_queue_handler - WORKER_RAMP_DOWN_QUEUE event handler
James Smart9bad7672008-12-04 22:39:02 -0500333 * @phba: The Hba for which this call is being executed.
334 *
335 * This routine is called to process WORKER_RAMP_DOWN_QUEUE event for worker
336 * thread.This routine reduces queue depth for all scsi device on each vport
337 * associated with @phba.
338 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500339void
340lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
341{
James Smart549e55c2007-08-02 11:09:51 -0400342 struct lpfc_vport **vports;
343 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500344 struct scsi_device *sdev;
James Smartea2151b2008-09-07 11:52:10 -0400345 unsigned long new_queue_depth, old_queue_depth;
James Smart92d7f7b2007-06-17 19:56:38 -0500346 unsigned long num_rsrc_err, num_cmd_success;
James Smart549e55c2007-08-02 11:09:51 -0400347 int i;
James Smartea2151b2008-09-07 11:52:10 -0400348 struct lpfc_rport_data *rdata;
James Smart92d7f7b2007-06-17 19:56:38 -0500349
350 num_rsrc_err = atomic_read(&phba->num_rsrc_err);
351 num_cmd_success = atomic_read(&phba->num_cmd_success);
352
James Smart549e55c2007-08-02 11:09:51 -0400353 vports = lpfc_create_vport_work_array(phba);
354 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400355 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400356 shost = lpfc_shost_from_vport(vports[i]);
357 shost_for_each_device(sdev, shost) {
James Smart92d7f7b2007-06-17 19:56:38 -0500358 new_queue_depth =
James Smart549e55c2007-08-02 11:09:51 -0400359 sdev->queue_depth * num_rsrc_err /
360 (num_rsrc_err + num_cmd_success);
361 if (!new_queue_depth)
362 new_queue_depth = sdev->queue_depth - 1;
363 else
364 new_queue_depth = sdev->queue_depth -
365 new_queue_depth;
James Smartea2151b2008-09-07 11:52:10 -0400366 old_queue_depth = sdev->queue_depth;
James Smart549e55c2007-08-02 11:09:51 -0400367 if (sdev->ordered_tags)
368 scsi_adjust_queue_depth(sdev,
369 MSG_ORDERED_TAG,
370 new_queue_depth);
371 else
372 scsi_adjust_queue_depth(sdev,
373 MSG_SIMPLE_TAG,
374 new_queue_depth);
James Smartea2151b2008-09-07 11:52:10 -0400375 rdata = sdev->hostdata;
376 if (rdata)
377 lpfc_send_sdev_queuedepth_change_event(
378 phba, vports[i],
379 rdata->pnode,
380 sdev->lun, old_queue_depth,
381 new_queue_depth);
James Smart549e55c2007-08-02 11:09:51 -0400382 }
James Smart92d7f7b2007-06-17 19:56:38 -0500383 }
James Smart09372822008-01-11 01:52:54 -0500384 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500385 atomic_set(&phba->num_rsrc_err, 0);
386 atomic_set(&phba->num_cmd_success, 0);
387}
388
James Smart9bad7672008-12-04 22:39:02 -0500389/**
James Smart3621a712009-04-06 18:47:14 -0400390 * lpfc_ramp_up_queue_handler - WORKER_RAMP_UP_QUEUE event handler
James Smart9bad7672008-12-04 22:39:02 -0500391 * @phba: The Hba for which this call is being executed.
392 *
393 * This routine is called to process WORKER_RAMP_UP_QUEUE event for worker
394 * thread.This routine increases queue depth for all scsi device on each vport
395 * associated with @phba by 1. This routine also sets @phba num_rsrc_err and
396 * num_cmd_success to zero.
397 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500398void
399lpfc_ramp_up_queue_handler(struct lpfc_hba *phba)
400{
James Smart549e55c2007-08-02 11:09:51 -0400401 struct lpfc_vport **vports;
402 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500403 struct scsi_device *sdev;
James Smart549e55c2007-08-02 11:09:51 -0400404 int i;
James Smartea2151b2008-09-07 11:52:10 -0400405 struct lpfc_rport_data *rdata;
James Smart92d7f7b2007-06-17 19:56:38 -0500406
James Smart549e55c2007-08-02 11:09:51 -0400407 vports = lpfc_create_vport_work_array(phba);
408 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400409 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400410 shost = lpfc_shost_from_vport(vports[i]);
411 shost_for_each_device(sdev, shost) {
James Smart97eab632008-04-07 10:16:05 -0400412 if (vports[i]->cfg_lun_queue_depth <=
413 sdev->queue_depth)
414 continue;
James Smart549e55c2007-08-02 11:09:51 -0400415 if (sdev->ordered_tags)
416 scsi_adjust_queue_depth(sdev,
417 MSG_ORDERED_TAG,
418 sdev->queue_depth+1);
419 else
420 scsi_adjust_queue_depth(sdev,
421 MSG_SIMPLE_TAG,
422 sdev->queue_depth+1);
James Smartea2151b2008-09-07 11:52:10 -0400423 rdata = sdev->hostdata;
424 if (rdata)
425 lpfc_send_sdev_queuedepth_change_event(
426 phba, vports[i],
427 rdata->pnode,
428 sdev->lun,
429 sdev->queue_depth - 1,
430 sdev->queue_depth);
James Smart549e55c2007-08-02 11:09:51 -0400431 }
James Smart92d7f7b2007-06-17 19:56:38 -0500432 }
James Smart09372822008-01-11 01:52:54 -0500433 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500434 atomic_set(&phba->num_rsrc_err, 0);
435 atomic_set(&phba->num_cmd_success, 0);
436}
437
James Smarta8e497d2008-08-24 21:50:11 -0400438/**
James Smart3621a712009-04-06 18:47:14 -0400439 * lpfc_scsi_dev_block - set all scsi hosts to block state
James Smarta8e497d2008-08-24 21:50:11 -0400440 * @phba: Pointer to HBA context object.
441 *
442 * This function walks vport list and set each SCSI host to block state
443 * by invoking fc_remote_port_delete() routine. This function is invoked
444 * with EEH when device's PCI slot has been permanently disabled.
445 **/
446void
447lpfc_scsi_dev_block(struct lpfc_hba *phba)
448{
449 struct lpfc_vport **vports;
450 struct Scsi_Host *shost;
451 struct scsi_device *sdev;
452 struct fc_rport *rport;
453 int i;
454
455 vports = lpfc_create_vport_work_array(phba);
456 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400457 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8e497d2008-08-24 21:50:11 -0400458 shost = lpfc_shost_from_vport(vports[i]);
459 shost_for_each_device(sdev, shost) {
460 rport = starget_to_rport(scsi_target(sdev));
461 fc_remote_port_delete(rport);
462 }
463 }
464 lpfc_destroy_vport_work_array(phba, vports);
465}
466
James Smart9bad7672008-12-04 22:39:02 -0500467/**
James Smart3772a992009-05-22 14:50:54 -0400468 * lpfc_new_scsi_buf_s3 - Scsi buffer allocator for HBA with SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -0500469 * @vport: The virtual port for which this call being executed.
James Smart3772a992009-05-22 14:50:54 -0400470 * @num_to_allocate: The requested number of buffers to allocate.
James Smart9bad7672008-12-04 22:39:02 -0500471 *
James Smart3772a992009-05-22 14:50:54 -0400472 * This routine allocates a scsi buffer for device with SLI-3 interface spec,
473 * the scsi buffer contains all the necessary information needed to initiate
474 * a SCSI I/O. The non-DMAable buffer region contains information to build
475 * the IOCB. The DMAable region contains memory for the FCP CMND, FCP RSP,
476 * and the initial BPL. In addition to allocating memory, the FCP CMND and
477 * FCP RSP BDEs are setup in the BPL and the BPL BDE is setup in the IOCB.
James Smart9bad7672008-12-04 22:39:02 -0500478 *
479 * Return codes:
James Smart3772a992009-05-22 14:50:54 -0400480 * int - number of scsi buffers that were allocated.
481 * 0 = failure, less than num_to_alloc is a partial failure.
James Smart9bad7672008-12-04 22:39:02 -0500482 **/
James Smart3772a992009-05-22 14:50:54 -0400483static int
484lpfc_new_scsi_buf_s3(struct lpfc_vport *vport, int num_to_alloc)
dea31012005-04-17 16:05:31 -0500485{
James Smart2e0fef82007-06-17 19:56:36 -0500486 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500487 struct lpfc_scsi_buf *psb;
488 struct ulp_bde64 *bpl;
489 IOCB_t *iocb;
James Smart34b02dc2008-08-24 21:49:55 -0400490 dma_addr_t pdma_phys_fcp_cmd;
491 dma_addr_t pdma_phys_fcp_rsp;
492 dma_addr_t pdma_phys_bpl;
James Bottomley604a3e32005-10-29 10:28:33 -0500493 uint16_t iotag;
James Smart3772a992009-05-22 14:50:54 -0400494 int bcnt;
dea31012005-04-17 16:05:31 -0500495
James Smart3772a992009-05-22 14:50:54 -0400496 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
497 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
498 if (!psb)
499 break;
dea31012005-04-17 16:05:31 -0500500
James Smart3772a992009-05-22 14:50:54 -0400501 /*
502 * Get memory from the pci pool to map the virt space to pci
503 * bus space for an I/O. The DMA buffer includes space for the
504 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
505 * necessary to support the sg_tablesize.
506 */
507 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
508 GFP_KERNEL, &psb->dma_handle);
509 if (!psb->data) {
510 kfree(psb);
511 break;
512 }
dea31012005-04-17 16:05:31 -0500513
James Smart3772a992009-05-22 14:50:54 -0400514 /* Initialize virtual ptrs to dma_buf region. */
515 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
dea31012005-04-17 16:05:31 -0500516
James Smart3772a992009-05-22 14:50:54 -0400517 /* Allocate iotag for psb->cur_iocbq. */
518 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
519 if (iotag == 0) {
520 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
521 psb->data, psb->dma_handle);
522 kfree(psb);
523 break;
524 }
525 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Bottomley604a3e32005-10-29 10:28:33 -0500526
James Smart3772a992009-05-22 14:50:54 -0400527 psb->fcp_cmnd = psb->data;
528 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
529 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
James Smart34b02dc2008-08-24 21:49:55 -0400530 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500531
James Smart3772a992009-05-22 14:50:54 -0400532 /* Initialize local short-hand pointers. */
533 bpl = psb->fcp_bpl;
534 pdma_phys_fcp_cmd = psb->dma_handle;
535 pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
536 pdma_phys_bpl = psb->dma_handle + sizeof(struct fcp_cmnd) +
537 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500538
James Smart3772a992009-05-22 14:50:54 -0400539 /*
540 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
541 * are sg list bdes. Initialize the first two and leave the
542 * rest for queuecommand.
543 */
544 bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
545 bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
546 bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
547 bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
548 bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
dea31012005-04-17 16:05:31 -0500549
James Smart3772a992009-05-22 14:50:54 -0400550 /* Setup the physical region for the FCP RSP */
551 bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
552 bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
553 bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
554 bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
555 bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
556
557 /*
558 * Since the IOCB for the FCP I/O is built into this
559 * lpfc_scsi_buf, initialize it with all known data now.
560 */
561 iocb = &psb->cur_iocbq.iocb;
562 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
563 if ((phba->sli_rev == 3) &&
564 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
565 /* fill in immediate fcp command BDE */
566 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
567 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
568 iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
569 unsli3.fcp_ext.icd);
570 iocb->un.fcpi64.bdl.addrHigh = 0;
571 iocb->ulpBdeCount = 0;
572 iocb->ulpLe = 0;
573 /* fill in responce BDE */
574 iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags =
575 BUFF_TYPE_BDE_64;
576 iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
577 sizeof(struct fcp_rsp);
578 iocb->unsli3.fcp_ext.rbde.addrLow =
579 putPaddrLow(pdma_phys_fcp_rsp);
580 iocb->unsli3.fcp_ext.rbde.addrHigh =
581 putPaddrHigh(pdma_phys_fcp_rsp);
582 } else {
583 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
584 iocb->un.fcpi64.bdl.bdeSize =
585 (2 * sizeof(struct ulp_bde64));
586 iocb->un.fcpi64.bdl.addrLow =
587 putPaddrLow(pdma_phys_bpl);
588 iocb->un.fcpi64.bdl.addrHigh =
589 putPaddrHigh(pdma_phys_bpl);
590 iocb->ulpBdeCount = 1;
591 iocb->ulpLe = 1;
592 }
593 iocb->ulpClass = CLASS3;
594 psb->status = IOSTAT_SUCCESS;
James Smartda0436e2009-05-22 14:51:39 -0400595 /* Put it back into the SCSI buffer list */
596 lpfc_release_scsi_buf_s4(phba, psb);
James Smart3772a992009-05-22 14:50:54 -0400597
James Smart34b02dc2008-08-24 21:49:55 -0400598 }
dea31012005-04-17 16:05:31 -0500599
James Smart3772a992009-05-22 14:50:54 -0400600 return bcnt;
dea31012005-04-17 16:05:31 -0500601}
602
James Smart9bad7672008-12-04 22:39:02 -0500603/**
James Smartda0436e2009-05-22 14:51:39 -0400604 * lpfc_sli4_fcp_xri_aborted - Fast-path process of fcp xri abort
605 * @phba: pointer to lpfc hba data structure.
606 * @axri: pointer to the fcp xri abort wcqe structure.
607 *
608 * This routine is invoked by the worker thread to process a SLI4 fast-path
609 * FCP aborted xri.
610 **/
611void
612lpfc_sli4_fcp_xri_aborted(struct lpfc_hba *phba,
613 struct sli4_wcqe_xri_aborted *axri)
614{
615 uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
616 struct lpfc_scsi_buf *psb, *next_psb;
617 unsigned long iflag = 0;
618
619 spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock, iflag);
620 list_for_each_entry_safe(psb, next_psb,
621 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
622 if (psb->cur_iocbq.sli4_xritag == xri) {
623 list_del(&psb->list);
624 psb->status = IOSTAT_SUCCESS;
625 spin_unlock_irqrestore(
626 &phba->sli4_hba.abts_scsi_buf_list_lock,
627 iflag);
628 lpfc_release_scsi_buf_s4(phba, psb);
629 return;
630 }
631 }
632 spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
633 iflag);
634}
635
636/**
637 * lpfc_sli4_repost_scsi_sgl_list - Repsot the Scsi buffers sgl pages as block
638 * @phba: pointer to lpfc hba data structure.
639 *
640 * This routine walks the list of scsi buffers that have been allocated and
641 * repost them to the HBA by using SGL block post. This is needed after a
642 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
643 * is responsible for moving all scsi buffers on the lpfc_abts_scsi_sgl_list
644 * to the lpfc_scsi_buf_list. If the repost fails, reject all scsi buffers.
645 *
646 * Returns: 0 = success, non-zero failure.
647 **/
648int
649lpfc_sli4_repost_scsi_sgl_list(struct lpfc_hba *phba)
650{
651 struct lpfc_scsi_buf *psb;
652 int index, status, bcnt = 0, rcnt = 0, rc = 0;
653 LIST_HEAD(sblist);
654
655 for (index = 0; index < phba->sli4_hba.scsi_xri_cnt; index++) {
656 psb = phba->sli4_hba.lpfc_scsi_psb_array[index];
657 if (psb) {
658 /* Remove from SCSI buffer list */
659 list_del(&psb->list);
660 /* Add it to a local SCSI buffer list */
661 list_add_tail(&psb->list, &sblist);
662 if (++rcnt == LPFC_NEMBED_MBOX_SGL_CNT) {
663 bcnt = rcnt;
664 rcnt = 0;
665 }
666 } else
667 /* A hole present in the XRI array, need to skip */
668 bcnt = rcnt;
669
670 if (index == phba->sli4_hba.scsi_xri_cnt - 1)
671 /* End of XRI array for SCSI buffer, complete */
672 bcnt = rcnt;
673
674 /* Continue until collect up to a nembed page worth of sgls */
675 if (bcnt == 0)
676 continue;
677 /* Now, post the SCSI buffer list sgls as a block */
678 status = lpfc_sli4_post_scsi_sgl_block(phba, &sblist, bcnt);
679 /* Reset SCSI buffer count for next round of posting */
680 bcnt = 0;
681 while (!list_empty(&sblist)) {
682 list_remove_head(&sblist, psb, struct lpfc_scsi_buf,
683 list);
684 if (status) {
685 /* Put this back on the abort scsi list */
686 psb->status = IOSTAT_LOCAL_REJECT;
687 psb->result = IOERR_ABORT_REQUESTED;
688 rc++;
689 } else
690 psb->status = IOSTAT_SUCCESS;
691 /* Put it back into the SCSI buffer list */
692 lpfc_release_scsi_buf_s4(phba, psb);
693 }
694 }
695 return rc;
696}
697
698/**
699 * lpfc_new_scsi_buf_s4 - Scsi buffer allocator for HBA with SLI4 IF spec
700 * @vport: The virtual port for which this call being executed.
701 * @num_to_allocate: The requested number of buffers to allocate.
702 *
703 * This routine allocates a scsi buffer for device with SLI-4 interface spec,
704 * the scsi buffer contains all the necessary information needed to initiate
705 * a SCSI I/O.
706 *
707 * Return codes:
708 * int - number of scsi buffers that were allocated.
709 * 0 = failure, less than num_to_alloc is a partial failure.
710 **/
711static int
712lpfc_new_scsi_buf_s4(struct lpfc_vport *vport, int num_to_alloc)
713{
714 struct lpfc_hba *phba = vport->phba;
715 struct lpfc_scsi_buf *psb;
716 struct sli4_sge *sgl;
717 IOCB_t *iocb;
718 dma_addr_t pdma_phys_fcp_cmd;
719 dma_addr_t pdma_phys_fcp_rsp;
720 dma_addr_t pdma_phys_bpl, pdma_phys_bpl1;
721 uint16_t iotag, last_xritag = NO_XRI;
722 int status = 0, index;
723 int bcnt;
724 int non_sequential_xri = 0;
725 int rc = 0;
726 LIST_HEAD(sblist);
727
728 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
729 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
730 if (!psb)
731 break;
732
733 /*
734 * Get memory from the pci pool to map the virt space to pci bus
735 * space for an I/O. The DMA buffer includes space for the
736 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
737 * necessary to support the sg_tablesize.
738 */
739 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
740 GFP_KERNEL, &psb->dma_handle);
741 if (!psb->data) {
742 kfree(psb);
743 break;
744 }
745
746 /* Initialize virtual ptrs to dma_buf region. */
747 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
748
749 /* Allocate iotag for psb->cur_iocbq. */
750 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
751 if (iotag == 0) {
752 kfree(psb);
753 break;
754 }
755
756 psb->cur_iocbq.sli4_xritag = lpfc_sli4_next_xritag(phba);
757 if (psb->cur_iocbq.sli4_xritag == NO_XRI) {
758 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
759 psb->data, psb->dma_handle);
760 kfree(psb);
761 break;
762 }
763 if (last_xritag != NO_XRI
764 && psb->cur_iocbq.sli4_xritag != (last_xritag+1)) {
765 non_sequential_xri = 1;
766 } else
767 list_add_tail(&psb->list, &sblist);
768 last_xritag = psb->cur_iocbq.sli4_xritag;
769
770 index = phba->sli4_hba.scsi_xri_cnt++;
771 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
772
773 psb->fcp_bpl = psb->data;
774 psb->fcp_cmnd = (psb->data + phba->cfg_sg_dma_buf_size)
775 - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
776 psb->fcp_rsp = (struct fcp_rsp *)((uint8_t *)psb->fcp_cmnd +
777 sizeof(struct fcp_cmnd));
778
779 /* Initialize local short-hand pointers. */
780 sgl = (struct sli4_sge *)psb->fcp_bpl;
781 pdma_phys_bpl = psb->dma_handle;
782 pdma_phys_fcp_cmd =
783 (psb->dma_handle + phba->cfg_sg_dma_buf_size)
784 - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
785 pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
786
787 /*
788 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
789 * are sg list bdes. Initialize the first two and leave the
790 * rest for queuecommand.
791 */
792 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
793 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
794 bf_set(lpfc_sli4_sge_len, sgl, sizeof(struct fcp_cmnd));
795 bf_set(lpfc_sli4_sge_last, sgl, 0);
796 sgl->word2 = cpu_to_le32(sgl->word2);
797 sgl->word3 = cpu_to_le32(sgl->word3);
798 sgl++;
799
800 /* Setup the physical region for the FCP RSP */
801 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
802 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
803 bf_set(lpfc_sli4_sge_len, sgl, sizeof(struct fcp_rsp));
804 bf_set(lpfc_sli4_sge_last, sgl, 1);
805 sgl->word2 = cpu_to_le32(sgl->word2);
806 sgl->word3 = cpu_to_le32(sgl->word3);
807
808 /*
809 * Since the IOCB for the FCP I/O is built into this
810 * lpfc_scsi_buf, initialize it with all known data now.
811 */
812 iocb = &psb->cur_iocbq.iocb;
813 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
814 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
815 /* setting the BLP size to 2 * sizeof BDE may not be correct.
816 * We are setting the bpl to point to out sgl. An sgl's
817 * entries are 16 bytes, a bpl entries are 12 bytes.
818 */
819 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
820 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
821 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
822 iocb->ulpBdeCount = 1;
823 iocb->ulpLe = 1;
824 iocb->ulpClass = CLASS3;
825 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
826 pdma_phys_bpl1 = pdma_phys_bpl + SGL_PAGE_SIZE;
827 else
828 pdma_phys_bpl1 = 0;
829 psb->dma_phys_bpl = pdma_phys_bpl;
830 phba->sli4_hba.lpfc_scsi_psb_array[index] = psb;
831 if (non_sequential_xri) {
832 status = lpfc_sli4_post_sgl(phba, pdma_phys_bpl,
833 pdma_phys_bpl1,
834 psb->cur_iocbq.sli4_xritag);
835 if (status) {
836 /* Put this back on the abort scsi list */
837 psb->status = IOSTAT_LOCAL_REJECT;
838 psb->result = IOERR_ABORT_REQUESTED;
839 rc++;
840 } else
841 psb->status = IOSTAT_SUCCESS;
842 /* Put it back into the SCSI buffer list */
843 lpfc_release_scsi_buf_s4(phba, psb);
844 break;
845 }
846 }
847 if (bcnt) {
848 status = lpfc_sli4_post_scsi_sgl_block(phba, &sblist, bcnt);
849 /* Reset SCSI buffer count for next round of posting */
850 while (!list_empty(&sblist)) {
851 list_remove_head(&sblist, psb, struct lpfc_scsi_buf,
852 list);
853 if (status) {
854 /* Put this back on the abort scsi list */
855 psb->status = IOSTAT_LOCAL_REJECT;
856 psb->result = IOERR_ABORT_REQUESTED;
857 rc++;
858 } else
859 psb->status = IOSTAT_SUCCESS;
860 /* Put it back into the SCSI buffer list */
861 lpfc_release_scsi_buf_s4(phba, psb);
862 }
863 }
864
865 return bcnt + non_sequential_xri - rc;
866}
867
868/**
James Smart3772a992009-05-22 14:50:54 -0400869 * lpfc_new_scsi_buf - Wrapper funciton for scsi buffer allocator
870 * @vport: The virtual port for which this call being executed.
871 * @num_to_allocate: The requested number of buffers to allocate.
872 *
873 * This routine wraps the actual SCSI buffer allocator function pointer from
874 * the lpfc_hba struct.
875 *
876 * Return codes:
877 * int - number of scsi buffers that were allocated.
878 * 0 = failure, less than num_to_alloc is a partial failure.
879 **/
880static inline int
881lpfc_new_scsi_buf(struct lpfc_vport *vport, int num_to_alloc)
882{
883 return vport->phba->lpfc_new_scsi_buf(vport, num_to_alloc);
884}
885
886/**
887 * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
888 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -0500889 *
890 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
891 * and returns to caller.
892 *
893 * Return codes:
894 * NULL - Error
895 * Pointer to lpfc_scsi_buf - Success
896 **/
Adrian Bunk455c53e2006-01-06 20:21:28 +0100897static struct lpfc_scsi_buf*
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500898lpfc_get_scsi_buf(struct lpfc_hba * phba)
dea31012005-04-17 16:05:31 -0500899{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400900 struct lpfc_scsi_buf * lpfc_cmd = NULL;
901 struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500902 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500903
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500904 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400905 list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
James Smart1dcb58e2007-04-25 09:51:30 -0400906 if (lpfc_cmd) {
907 lpfc_cmd->seg_cnt = 0;
908 lpfc_cmd->nonsg_phys = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -0500909 lpfc_cmd->prot_seg_cnt = 0;
James Smart1dcb58e2007-04-25 09:51:30 -0400910 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500911 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400912 return lpfc_cmd;
913}
914
James Smart9bad7672008-12-04 22:39:02 -0500915/**
James Smart3772a992009-05-22 14:50:54 -0400916 * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
James Smart9bad7672008-12-04 22:39:02 -0500917 * @phba: The Hba for which this call is being executed.
918 * @psb: The scsi buffer which is being released.
919 *
920 * This routine releases @psb scsi buffer by adding it to tail of @phba
921 * lpfc_scsi_buf_list list.
922 **/
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400923static void
James Smart3772a992009-05-22 14:50:54 -0400924lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400925{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500926 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500927
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500928 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400929 psb->pCmd = NULL;
dea31012005-04-17 16:05:31 -0500930 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500931 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
dea31012005-04-17 16:05:31 -0500932}
933
James Smart9bad7672008-12-04 22:39:02 -0500934/**
James Smartda0436e2009-05-22 14:51:39 -0400935 * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
936 * @phba: The Hba for which this call is being executed.
937 * @psb: The scsi buffer which is being released.
938 *
939 * This routine releases @psb scsi buffer by adding it to tail of @phba
940 * lpfc_scsi_buf_list list. For SLI4 XRI's are tied to the scsi buffer
941 * and cannot be reused for at least RA_TOV amount of time if it was
942 * aborted.
943 **/
944static void
945lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
946{
947 unsigned long iflag = 0;
948
949 if (psb->status == IOSTAT_LOCAL_REJECT
950 && psb->result == IOERR_ABORT_REQUESTED) {
951 spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock,
952 iflag);
953 psb->pCmd = NULL;
954 list_add_tail(&psb->list,
955 &phba->sli4_hba.lpfc_abts_scsi_buf_list);
956 spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
957 iflag);
958 } else {
959
960 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
961 psb->pCmd = NULL;
962 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
963 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
964 }
965}
966
967/**
James Smart3772a992009-05-22 14:50:54 -0400968 * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
969 * @phba: The Hba for which this call is being executed.
970 * @psb: The scsi buffer which is being released.
971 *
972 * This routine releases @psb scsi buffer by adding it to tail of @phba
973 * lpfc_scsi_buf_list list.
974 **/
975static void
976lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
977{
978
979 phba->lpfc_release_scsi_buf(phba, psb);
980}
981
982/**
983 * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -0500984 * @phba: The Hba for which this call is being executed.
985 * @lpfc_cmd: The scsi buffer which is going to be mapped.
986 *
987 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
James Smart3772a992009-05-22 14:50:54 -0400988 * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
989 * through sg elements and format the bdea. This routine also initializes all
990 * IOCB fields which are dependent on scsi command request buffer.
James Smart9bad7672008-12-04 22:39:02 -0500991 *
992 * Return codes:
993 * 1 - Error
994 * 0 - Success
995 **/
dea31012005-04-17 16:05:31 -0500996static int
James Smart3772a992009-05-22 14:50:54 -0400997lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
dea31012005-04-17 16:05:31 -0500998{
999 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1000 struct scatterlist *sgel = NULL;
1001 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1002 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
1003 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart34b02dc2008-08-24 21:49:55 -04001004 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
dea31012005-04-17 16:05:31 -05001005 dma_addr_t physaddr;
James Smart34b02dc2008-08-24 21:49:55 -04001006 uint32_t num_bde = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001007 int nseg, datadir = scsi_cmnd->sc_data_direction;
dea31012005-04-17 16:05:31 -05001008
1009 /*
1010 * There are three possibilities here - use scatter-gather segment, use
1011 * the single mapping, or neither. Start the lpfc command prep by
1012 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1013 * data bde entry.
1014 */
1015 bpl += 2;
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001016 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05001017 /*
1018 * The driver stores the segment count returned from pci_map_sg
1019 * because this a count of dma-mappings used to map the use_sg
1020 * pages. They are not guaranteed to be the same for those
1021 * architectures that implement an IOMMU.
1022 */
dea31012005-04-17 16:05:31 -05001023
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001024 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
1025 scsi_sg_count(scsi_cmnd), datadir);
1026 if (unlikely(!nseg))
1027 return 1;
1028
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001029 lpfc_cmd->seg_cnt = nseg;
dea31012005-04-17 16:05:31 -05001030 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001031 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1032 "9064 BLKGRD: %s: Too many sg segments from "
James Smarte2a0a9d2008-12-04 22:40:02 -05001033 "dma_map_sg. Config %d, seg_cnt %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001034 __func__, phba->cfg_sg_seg_cnt,
dea31012005-04-17 16:05:31 -05001035 lpfc_cmd->seg_cnt);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001036 scsi_dma_unmap(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001037 return 1;
1038 }
1039
1040 /*
1041 * The driver established a maximum scatter-gather segment count
1042 * during probe that limits the number of sg elements in any
1043 * single scsi command. Just run through the seg_cnt and format
1044 * the bde's.
James Smart34b02dc2008-08-24 21:49:55 -04001045 * When using SLI-3 the driver will try to fit all the BDEs into
1046 * the IOCB. If it can't then the BDEs get added to a BPL as it
1047 * does for SLI-2 mode.
dea31012005-04-17 16:05:31 -05001048 */
James Smart34b02dc2008-08-24 21:49:55 -04001049 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
dea31012005-04-17 16:05:31 -05001050 physaddr = sg_dma_address(sgel);
James Smart34b02dc2008-08-24 21:49:55 -04001051 if (phba->sli_rev == 3 &&
James Smarte2a0a9d2008-12-04 22:40:02 -05001052 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
James Smart34b02dc2008-08-24 21:49:55 -04001053 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
1054 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1055 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
1056 data_bde->addrLow = putPaddrLow(physaddr);
1057 data_bde->addrHigh = putPaddrHigh(physaddr);
1058 data_bde++;
1059 } else {
1060 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1061 bpl->tus.f.bdeSize = sg_dma_len(sgel);
1062 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1063 bpl->addrLow =
1064 le32_to_cpu(putPaddrLow(physaddr));
1065 bpl->addrHigh =
1066 le32_to_cpu(putPaddrHigh(physaddr));
1067 bpl++;
1068 }
dea31012005-04-17 16:05:31 -05001069 }
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001070 }
dea31012005-04-17 16:05:31 -05001071
1072 /*
1073 * Finish initializing those IOCB fields that are dependent on the
James Smart34b02dc2008-08-24 21:49:55 -04001074 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
1075 * explicitly reinitialized and for SLI-3 the extended bde count is
1076 * explicitly reinitialized since all iocb memory resources are reused.
dea31012005-04-17 16:05:31 -05001077 */
James Smarte2a0a9d2008-12-04 22:40:02 -05001078 if (phba->sli_rev == 3 &&
1079 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
James Smart34b02dc2008-08-24 21:49:55 -04001080 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
1081 /*
1082 * The extended IOCB format can only fit 3 BDE or a BPL.
1083 * This I/O has more than 3 BDE so the 1st data bde will
1084 * be a BPL that is filled in here.
1085 */
1086 physaddr = lpfc_cmd->dma_handle;
1087 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
1088 data_bde->tus.f.bdeSize = (num_bde *
1089 sizeof(struct ulp_bde64));
1090 physaddr += (sizeof(struct fcp_cmnd) +
1091 sizeof(struct fcp_rsp) +
1092 (2 * sizeof(struct ulp_bde64)));
1093 data_bde->addrHigh = putPaddrHigh(physaddr);
1094 data_bde->addrLow = putPaddrLow(physaddr);
1095 /* ebde count includes the responce bde and data bpl */
1096 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
1097 } else {
1098 /* ebde count includes the responce bde and data bdes */
1099 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1100 }
1101 } else {
1102 iocb_cmd->un.fcpi64.bdl.bdeSize =
1103 ((num_bde + 2) * sizeof(struct ulp_bde64));
1104 }
James Smart09372822008-01-11 01:52:54 -05001105 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
James Smarte2a0a9d2008-12-04 22:40:02 -05001106
1107 /*
1108 * Due to difference in data length between DIF/non-DIF paths,
1109 * we need to set word 4 of IOCB here
1110 */
James Smarta257bf92009-04-06 18:48:10 -04001111 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001112 return 0;
1113}
1114
James Smarte2a0a9d2008-12-04 22:40:02 -05001115/*
1116 * Given a scsi cmnd, determine the BlockGuard profile to be used
1117 * with the cmd
1118 */
1119static int
James Smart6a9c52c2009-10-02 15:16:51 -04001120lpfc_sc_to_sli_prof(struct lpfc_hba *phba, struct scsi_cmnd *sc)
James Smarte2a0a9d2008-12-04 22:40:02 -05001121{
1122 uint8_t guard_type = scsi_host_get_guard(sc->device->host);
1123 uint8_t ret_prof = LPFC_PROF_INVALID;
1124
1125 if (guard_type == SHOST_DIX_GUARD_IP) {
1126 switch (scsi_get_prot_op(sc)) {
1127 case SCSI_PROT_READ_INSERT:
1128 case SCSI_PROT_WRITE_STRIP:
1129 ret_prof = LPFC_PROF_AST2;
1130 break;
1131
1132 case SCSI_PROT_READ_STRIP:
1133 case SCSI_PROT_WRITE_INSERT:
1134 ret_prof = LPFC_PROF_A1;
1135 break;
1136
Martin K. Petersenc6af4042009-09-18 17:32:59 -04001137 case SCSI_PROT_READ_PASS:
1138 case SCSI_PROT_WRITE_PASS:
James Smarte2a0a9d2008-12-04 22:40:02 -05001139 ret_prof = LPFC_PROF_AST1;
1140 break;
1141
James Smarte2a0a9d2008-12-04 22:40:02 -05001142 case SCSI_PROT_NORMAL:
1143 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001144 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1145 "9063 BLKGRD:Bad op/guard:%d/%d combination\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001146 scsi_get_prot_op(sc), guard_type);
1147 break;
1148
1149 }
1150 } else if (guard_type == SHOST_DIX_GUARD_CRC) {
1151 switch (scsi_get_prot_op(sc)) {
1152 case SCSI_PROT_READ_STRIP:
1153 case SCSI_PROT_WRITE_INSERT:
1154 ret_prof = LPFC_PROF_A1;
1155 break;
1156
1157 case SCSI_PROT_READ_PASS:
1158 case SCSI_PROT_WRITE_PASS:
1159 ret_prof = LPFC_PROF_C1;
1160 break;
1161
James Smarte2a0a9d2008-12-04 22:40:02 -05001162 case SCSI_PROT_READ_INSERT:
1163 case SCSI_PROT_WRITE_STRIP:
1164 case SCSI_PROT_NORMAL:
1165 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001166 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1167 "9075 BLKGRD: Bad op/guard:%d/%d combination\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001168 scsi_get_prot_op(sc), guard_type);
1169 break;
1170 }
1171 } else {
1172 /* unsupported format */
1173 BUG();
1174 }
1175
1176 return ret_prof;
1177}
1178
1179struct scsi_dif_tuple {
1180 __be16 guard_tag; /* Checksum */
1181 __be16 app_tag; /* Opaque storage */
1182 __be32 ref_tag; /* Target LBA or indirect LBA */
1183};
1184
1185static inline unsigned
1186lpfc_cmd_blksize(struct scsi_cmnd *sc)
1187{
1188 return sc->device->sector_size;
1189}
1190
1191/**
1192 * lpfc_get_cmd_dif_parms - Extract DIF parameters from SCSI command
1193 * @sc: in: SCSI command
James Smart3621a712009-04-06 18:47:14 -04001194 * @apptagmask: out: app tag mask
1195 * @apptagval: out: app tag value
1196 * @reftag: out: ref tag (reference tag)
James Smarte2a0a9d2008-12-04 22:40:02 -05001197 *
1198 * Description:
Martin Olsson98a17082009-04-22 18:21:29 +02001199 * Extract DIF parameters from the command if possible. Otherwise,
1200 * use default parameters.
James Smarte2a0a9d2008-12-04 22:40:02 -05001201 *
1202 **/
1203static inline void
1204lpfc_get_cmd_dif_parms(struct scsi_cmnd *sc, uint16_t *apptagmask,
1205 uint16_t *apptagval, uint32_t *reftag)
1206{
1207 struct scsi_dif_tuple *spt;
1208 unsigned char op = scsi_get_prot_op(sc);
1209 unsigned int protcnt = scsi_prot_sg_count(sc);
1210 static int cnt;
1211
1212 if (protcnt && (op == SCSI_PROT_WRITE_STRIP ||
Martin K. Petersenc6af4042009-09-18 17:32:59 -04001213 op == SCSI_PROT_WRITE_PASS)) {
James Smarte2a0a9d2008-12-04 22:40:02 -05001214
1215 cnt++;
1216 spt = page_address(sg_page(scsi_prot_sglist(sc))) +
1217 scsi_prot_sglist(sc)[0].offset;
1218 *apptagmask = 0;
1219 *apptagval = 0;
1220 *reftag = cpu_to_be32(spt->ref_tag);
1221
1222 } else {
1223 /* SBC defines ref tag to be lower 32bits of LBA */
1224 *reftag = (uint32_t) (0xffffffff & scsi_get_lba(sc));
1225 *apptagmask = 0;
1226 *apptagval = 0;
1227 }
1228}
1229
1230/*
1231 * This function sets up buffer list for protection groups of
1232 * type LPFC_PG_TYPE_NO_DIF
1233 *
1234 * This is usually used when the HBA is instructed to generate
1235 * DIFs and insert them into data stream (or strip DIF from
1236 * incoming data stream)
1237 *
1238 * The buffer list consists of just one protection group described
1239 * below:
1240 * +-------------------------+
1241 * start of prot group --> | PDE_1 |
1242 * +-------------------------+
1243 * | Data BDE |
1244 * +-------------------------+
1245 * |more Data BDE's ... (opt)|
1246 * +-------------------------+
1247 *
1248 * @sc: pointer to scsi command we're working on
1249 * @bpl: pointer to buffer list for protection groups
1250 * @datacnt: number of segments of data that have been dma mapped
1251 *
1252 * Note: Data s/g buffers have been dma mapped
1253 */
1254static int
1255lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1256 struct ulp_bde64 *bpl, int datasegcnt)
1257{
1258 struct scatterlist *sgde = NULL; /* s/g data entry */
1259 struct lpfc_pde *pde1 = NULL;
1260 dma_addr_t physaddr;
1261 int i = 0, num_bde = 0;
1262 int datadir = sc->sc_data_direction;
1263 int prof = LPFC_PROF_INVALID;
1264 unsigned blksize;
1265 uint32_t reftag;
1266 uint16_t apptagmask, apptagval;
1267
1268 pde1 = (struct lpfc_pde *) bpl;
James Smart6a9c52c2009-10-02 15:16:51 -04001269 prof = lpfc_sc_to_sli_prof(phba, sc);
James Smarte2a0a9d2008-12-04 22:40:02 -05001270
1271 if (prof == LPFC_PROF_INVALID)
1272 goto out;
1273
1274 /* extract some info from the scsi command for PDE1*/
1275 blksize = lpfc_cmd_blksize(sc);
1276 lpfc_get_cmd_dif_parms(sc, &apptagmask, &apptagval, &reftag);
1277
1278 /* setup PDE1 with what we have */
1279 lpfc_pde_set_bg_parms(pde1, LPFC_PDE1_DESCRIPTOR, prof, blksize,
1280 BG_EC_STOP_ERR);
1281 lpfc_pde_set_dif_parms(pde1, apptagmask, apptagval, reftag);
1282
1283 num_bde++;
1284 bpl++;
1285
1286 /* assumption: caller has already run dma_map_sg on command data */
1287 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
1288 physaddr = sg_dma_address(sgde);
1289 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
1290 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1291 bpl->tus.f.bdeSize = sg_dma_len(sgde);
1292 if (datadir == DMA_TO_DEVICE)
1293 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1294 else
1295 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1296 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1297 bpl++;
1298 num_bde++;
1299 }
1300
1301out:
1302 return num_bde;
1303}
1304
1305/*
1306 * This function sets up buffer list for protection groups of
1307 * type LPFC_PG_TYPE_DIF_BUF
1308 *
1309 * This is usually used when DIFs are in their own buffers,
1310 * separate from the data. The HBA can then by instructed
1311 * to place the DIFs in the outgoing stream. For read operations,
1312 * The HBA could extract the DIFs and place it in DIF buffers.
1313 *
1314 * The buffer list for this type consists of one or more of the
1315 * protection groups described below:
1316 * +-------------------------+
1317 * start of first prot group --> | PDE_1 |
1318 * +-------------------------+
1319 * | PDE_3 (Prot BDE) |
1320 * +-------------------------+
1321 * | Data BDE |
1322 * +-------------------------+
1323 * |more Data BDE's ... (opt)|
1324 * +-------------------------+
1325 * start of new prot group --> | PDE_1 |
1326 * +-------------------------+
1327 * | ... |
1328 * +-------------------------+
1329 *
1330 * @sc: pointer to scsi command we're working on
1331 * @bpl: pointer to buffer list for protection groups
1332 * @datacnt: number of segments of data that have been dma mapped
1333 * @protcnt: number of segment of protection data that have been dma mapped
1334 *
1335 * Note: It is assumed that both data and protection s/g buffers have been
1336 * mapped for DMA
1337 */
1338static int
1339lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1340 struct ulp_bde64 *bpl, int datacnt, int protcnt)
1341{
1342 struct scatterlist *sgde = NULL; /* s/g data entry */
1343 struct scatterlist *sgpe = NULL; /* s/g prot entry */
1344 struct lpfc_pde *pde1 = NULL;
1345 struct ulp_bde64 *prot_bde = NULL;
1346 dma_addr_t dataphysaddr, protphysaddr;
1347 unsigned short curr_data = 0, curr_prot = 0;
1348 unsigned int split_offset, protgroup_len;
1349 unsigned int protgrp_blks, protgrp_bytes;
1350 unsigned int remainder, subtotal;
1351 int prof = LPFC_PROF_INVALID;
1352 int datadir = sc->sc_data_direction;
1353 unsigned char pgdone = 0, alldone = 0;
1354 unsigned blksize;
1355 uint32_t reftag;
1356 uint16_t apptagmask, apptagval;
1357 int num_bde = 0;
1358
1359 sgpe = scsi_prot_sglist(sc);
1360 sgde = scsi_sglist(sc);
1361
1362 if (!sgpe || !sgde) {
1363 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1364 "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
1365 sgpe, sgde);
1366 return 0;
1367 }
1368
James Smart6a9c52c2009-10-02 15:16:51 -04001369 prof = lpfc_sc_to_sli_prof(phba, sc);
James Smarte2a0a9d2008-12-04 22:40:02 -05001370 if (prof == LPFC_PROF_INVALID)
1371 goto out;
1372
1373 /* extract some info from the scsi command for PDE1*/
1374 blksize = lpfc_cmd_blksize(sc);
1375 lpfc_get_cmd_dif_parms(sc, &apptagmask, &apptagval, &reftag);
1376
1377 split_offset = 0;
1378 do {
1379 /* setup the first PDE_1 */
1380 pde1 = (struct lpfc_pde *) bpl;
1381
1382 lpfc_pde_set_bg_parms(pde1, LPFC_PDE1_DESCRIPTOR, prof, blksize,
1383 BG_EC_STOP_ERR);
1384 lpfc_pde_set_dif_parms(pde1, apptagmask, apptagval, reftag);
1385
1386 num_bde++;
1387 bpl++;
1388
1389 /* setup the first BDE that points to protection buffer */
1390 prot_bde = (struct ulp_bde64 *) bpl;
1391 protphysaddr = sg_dma_address(sgpe);
1392 prot_bde->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
1393 prot_bde->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
1394 protgroup_len = sg_dma_len(sgpe);
1395
1396
1397 /* must be integer multiple of the DIF block length */
1398 BUG_ON(protgroup_len % 8);
1399
1400 protgrp_blks = protgroup_len / 8;
1401 protgrp_bytes = protgrp_blks * blksize;
1402
1403 prot_bde->tus.f.bdeSize = protgroup_len;
1404 if (datadir == DMA_TO_DEVICE)
1405 prot_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1406 else
1407 prot_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1408 prot_bde->tus.w = le32_to_cpu(bpl->tus.w);
1409
1410 curr_prot++;
1411 num_bde++;
1412
1413 /* setup BDE's for data blocks associated with DIF data */
1414 pgdone = 0;
1415 subtotal = 0; /* total bytes processed for current prot grp */
1416 while (!pgdone) {
1417 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -04001418 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1419 "9065 BLKGRD:%s Invalid data segment\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001420 __func__);
1421 return 0;
1422 }
1423 bpl++;
1424 dataphysaddr = sg_dma_address(sgde) + split_offset;
1425 bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
1426 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
1427
1428 remainder = sg_dma_len(sgde) - split_offset;
1429
1430 if ((subtotal + remainder) <= protgrp_bytes) {
1431 /* we can use this whole buffer */
1432 bpl->tus.f.bdeSize = remainder;
1433 split_offset = 0;
1434
1435 if ((subtotal + remainder) == protgrp_bytes)
1436 pgdone = 1;
1437 } else {
1438 /* must split this buffer with next prot grp */
1439 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
1440 split_offset += bpl->tus.f.bdeSize;
1441 }
1442
1443 subtotal += bpl->tus.f.bdeSize;
1444
1445 if (datadir == DMA_TO_DEVICE)
1446 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1447 else
1448 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1449 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1450
1451 num_bde++;
1452 curr_data++;
1453
1454 if (split_offset)
1455 break;
1456
1457 /* Move to the next s/g segment if possible */
1458 sgde = sg_next(sgde);
1459 }
1460
1461 /* are we done ? */
1462 if (curr_prot == protcnt) {
1463 alldone = 1;
1464 } else if (curr_prot < protcnt) {
1465 /* advance to next prot buffer */
1466 sgpe = sg_next(sgpe);
1467 bpl++;
1468
1469 /* update the reference tag */
1470 reftag += protgrp_blks;
1471 } else {
1472 /* if we're here, we have a bug */
James Smart6a9c52c2009-10-02 15:16:51 -04001473 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1474 "9054 BLKGRD: bug in %s\n", __func__);
James Smarte2a0a9d2008-12-04 22:40:02 -05001475 }
1476
1477 } while (!alldone);
1478
1479out:
1480
1481
1482 return num_bde;
1483}
1484/*
1485 * Given a SCSI command that supports DIF, determine composition of protection
1486 * groups involved in setting up buffer lists
1487 *
1488 * Returns:
1489 * for DIF (for both read and write)
1490 * */
1491static int
1492lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
1493{
1494 int ret = LPFC_PG_TYPE_INVALID;
1495 unsigned char op = scsi_get_prot_op(sc);
1496
1497 switch (op) {
1498 case SCSI_PROT_READ_STRIP:
1499 case SCSI_PROT_WRITE_INSERT:
1500 ret = LPFC_PG_TYPE_NO_DIF;
1501 break;
1502 case SCSI_PROT_READ_INSERT:
1503 case SCSI_PROT_WRITE_STRIP:
1504 case SCSI_PROT_READ_PASS:
1505 case SCSI_PROT_WRITE_PASS:
James Smarte2a0a9d2008-12-04 22:40:02 -05001506 ret = LPFC_PG_TYPE_DIF_BUF;
1507 break;
1508 default:
1509 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1510 "9021 Unsupported protection op:%d\n", op);
1511 break;
1512 }
1513
1514 return ret;
1515}
1516
1517/*
1518 * This is the protection/DIF aware version of
1519 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
1520 * two functions eventually, but for now, it's here
1521 */
1522static int
1523lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba,
1524 struct lpfc_scsi_buf *lpfc_cmd)
1525{
1526 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1527 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1528 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
1529 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
1530 uint32_t num_bde = 0;
1531 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
1532 int prot_group_type = 0;
1533 int diflen, fcpdl;
1534 unsigned blksize;
1535
1536 /*
1537 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
1538 * fcp_rsp regions to the first data bde entry
1539 */
1540 bpl += 2;
1541 if (scsi_sg_count(scsi_cmnd)) {
1542 /*
1543 * The driver stores the segment count returned from pci_map_sg
1544 * because this a count of dma-mappings used to map the use_sg
1545 * pages. They are not guaranteed to be the same for those
1546 * architectures that implement an IOMMU.
1547 */
1548 datasegcnt = dma_map_sg(&phba->pcidev->dev,
1549 scsi_sglist(scsi_cmnd),
1550 scsi_sg_count(scsi_cmnd), datadir);
1551 if (unlikely(!datasegcnt))
1552 return 1;
1553
1554 lpfc_cmd->seg_cnt = datasegcnt;
1555 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001556 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1557 "9067 BLKGRD: %s: Too many sg segments"
1558 " from dma_map_sg. Config %d, seg_cnt"
1559 " %d\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001560 __func__, phba->cfg_sg_seg_cnt,
1561 lpfc_cmd->seg_cnt);
1562 scsi_dma_unmap(scsi_cmnd);
1563 return 1;
1564 }
1565
1566 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
1567
1568 switch (prot_group_type) {
1569 case LPFC_PG_TYPE_NO_DIF:
1570 num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
1571 datasegcnt);
1572 /* we shoud have 2 or more entries in buffer list */
1573 if (num_bde < 2)
1574 goto err;
1575 break;
1576 case LPFC_PG_TYPE_DIF_BUF:{
1577 /*
1578 * This type indicates that protection buffers are
1579 * passed to the driver, so that needs to be prepared
1580 * for DMA
1581 */
1582 protsegcnt = dma_map_sg(&phba->pcidev->dev,
1583 scsi_prot_sglist(scsi_cmnd),
1584 scsi_prot_sg_count(scsi_cmnd), datadir);
1585 if (unlikely(!protsegcnt)) {
1586 scsi_dma_unmap(scsi_cmnd);
1587 return 1;
1588 }
1589
1590 lpfc_cmd->prot_seg_cnt = protsegcnt;
1591 if (lpfc_cmd->prot_seg_cnt
1592 > phba->cfg_prot_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001593 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1594 "9068 BLKGRD: %s: Too many prot sg "
1595 "segments from dma_map_sg. Config %d,"
James Smarte2a0a9d2008-12-04 22:40:02 -05001596 "prot_seg_cnt %d\n", __func__,
1597 phba->cfg_prot_sg_seg_cnt,
1598 lpfc_cmd->prot_seg_cnt);
1599 dma_unmap_sg(&phba->pcidev->dev,
1600 scsi_prot_sglist(scsi_cmnd),
1601 scsi_prot_sg_count(scsi_cmnd),
1602 datadir);
1603 scsi_dma_unmap(scsi_cmnd);
1604 return 1;
1605 }
1606
1607 num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
1608 datasegcnt, protsegcnt);
1609 /* we shoud have 3 or more entries in buffer list */
1610 if (num_bde < 3)
1611 goto err;
1612 break;
1613 }
1614 case LPFC_PG_TYPE_INVALID:
1615 default:
1616 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1617 "9022 Unexpected protection group %i\n",
1618 prot_group_type);
1619 return 1;
1620 }
1621 }
1622
1623 /*
1624 * Finish initializing those IOCB fields that are dependent on the
1625 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
1626 * reinitialized since all iocb memory resources are used many times
1627 * for transmit, receive, and continuation bpl's.
1628 */
1629 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
1630 iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
1631 iocb_cmd->ulpBdeCount = 1;
1632 iocb_cmd->ulpLe = 1;
1633
1634 fcpdl = scsi_bufflen(scsi_cmnd);
1635
1636 if (scsi_get_prot_type(scsi_cmnd) == SCSI_PROT_DIF_TYPE1) {
1637 /*
1638 * We are in DIF Type 1 mode
1639 * Every data block has a 8 byte DIF (trailer)
1640 * attached to it. Must ajust FCP data length
1641 */
1642 blksize = lpfc_cmd_blksize(scsi_cmnd);
1643 diflen = (fcpdl / blksize) * 8;
1644 fcpdl += diflen;
1645 }
1646 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
1647
1648 /*
1649 * Due to difference in data length between DIF/non-DIF paths,
1650 * we need to set word 4 of IOCB here
1651 */
1652 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
1653
1654 return 0;
1655err:
1656 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1657 "9023 Could not setup all needed BDE's"
1658 "prot_group_type=%d, num_bde=%d\n",
1659 prot_group_type, num_bde);
1660 return 1;
1661}
1662
1663/*
1664 * This function checks for BlockGuard errors detected by
1665 * the HBA. In case of errors, the ASC/ASCQ fields in the
1666 * sense buffer will be set accordingly, paired with
1667 * ILLEGAL_REQUEST to signal to the kernel that the HBA
1668 * detected corruption.
1669 *
1670 * Returns:
1671 * 0 - No error found
1672 * 1 - BlockGuard error found
1673 * -1 - Internal error (bad profile, ...etc)
1674 */
1675static int
1676lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
1677 struct lpfc_iocbq *pIocbOut)
1678{
1679 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
1680 struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
1681 int ret = 0;
1682 uint32_t bghm = bgf->bghm;
1683 uint32_t bgstat = bgf->bgstat;
1684 uint64_t failing_sector = 0;
1685
James Smart6a9c52c2009-10-02 15:16:51 -04001686 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9069 BLKGRD: BG ERROR in cmd"
1687 " 0x%x lba 0x%llx blk cnt 0x%x "
James Smarte2a0a9d2008-12-04 22:40:02 -05001688 "bgstat=0x%x bghm=0x%x\n",
James Smart87b5c322008-12-16 10:34:09 -05001689 cmd->cmnd[0], (unsigned long long)scsi_get_lba(cmd),
Tejun Heo83096eb2009-05-07 22:24:39 +09001690 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05001691
1692 spin_lock(&_dump_buf_lock);
1693 if (!_dump_buf_done) {
James Smart6a9c52c2009-10-02 15:16:51 -04001694 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9070 BLKGRD: Saving"
1695 " Data for %u blocks to debugfs\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001696 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
James Smart6a9c52c2009-10-02 15:16:51 -04001697 lpfc_debug_save_data(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05001698
1699 /* If we have a prot sgl, save the DIF buffer */
1700 if (lpfc_prot_group_type(phba, cmd) ==
1701 LPFC_PG_TYPE_DIF_BUF) {
James Smart6a9c52c2009-10-02 15:16:51 -04001702 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9071 BLKGRD: "
1703 "Saving DIF for %u blocks to debugfs\n",
1704 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
1705 lpfc_debug_save_dif(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05001706 }
1707
1708 _dump_buf_done = 1;
1709 }
1710 spin_unlock(&_dump_buf_lock);
1711
1712 if (lpfc_bgs_get_invalid_prof(bgstat)) {
1713 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart6a9c52c2009-10-02 15:16:51 -04001714 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9072 BLKGRD: Invalid"
1715 " BlockGuard profile. bgstat:0x%x\n",
1716 bgstat);
James Smarte2a0a9d2008-12-04 22:40:02 -05001717 ret = (-1);
1718 goto out;
1719 }
1720
1721 if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
1722 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart6a9c52c2009-10-02 15:16:51 -04001723 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9073 BLKGRD: "
1724 "Invalid BlockGuard DIF Block. bgstat:0x%x\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001725 bgstat);
1726 ret = (-1);
1727 goto out;
1728 }
1729
1730 if (lpfc_bgs_get_guard_err(bgstat)) {
1731 ret = 1;
1732
1733 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
1734 0x10, 0x1);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05001735 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05001736 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
1737 phba->bg_guard_err_cnt++;
James Smart6a9c52c2009-10-02 15:16:51 -04001738 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1739 "9055 BLKGRD: guard_tag error\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05001740 }
1741
1742 if (lpfc_bgs_get_reftag_err(bgstat)) {
1743 ret = 1;
1744
1745 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
1746 0x10, 0x3);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05001747 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05001748 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
1749
1750 phba->bg_reftag_err_cnt++;
James Smart6a9c52c2009-10-02 15:16:51 -04001751 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1752 "9056 BLKGRD: ref_tag error\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05001753 }
1754
1755 if (lpfc_bgs_get_apptag_err(bgstat)) {
1756 ret = 1;
1757
1758 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
1759 0x10, 0x2);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05001760 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05001761 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
1762
1763 phba->bg_apptag_err_cnt++;
James Smart6a9c52c2009-10-02 15:16:51 -04001764 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1765 "9061 BLKGRD: app_tag error\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05001766 }
1767
1768 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
1769 /*
1770 * setup sense data descriptor 0 per SPC-4 as an information
1771 * field, and put the failing LBA in it
1772 */
1773 cmd->sense_buffer[8] = 0; /* Information */
1774 cmd->sense_buffer[9] = 0xa; /* Add. length */
David Howells2344b5b2009-04-14 17:08:34 +01001775 bghm /= cmd->device->sector_size;
James Smarte2a0a9d2008-12-04 22:40:02 -05001776
1777 failing_sector = scsi_get_lba(cmd);
1778 failing_sector += bghm;
1779
1780 put_unaligned_be64(failing_sector, &cmd->sense_buffer[10]);
1781 }
1782
1783 if (!ret) {
1784 /* No error was reported - problem in FW? */
1785 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart6a9c52c2009-10-02 15:16:51 -04001786 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1787 "9057 BLKGRD: no errors reported!\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05001788 }
1789
1790out:
1791 return ret;
1792}
1793
James Smartea2151b2008-09-07 11:52:10 -04001794/**
James Smartda0436e2009-05-22 14:51:39 -04001795 * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
1796 * @phba: The Hba for which this call is being executed.
1797 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1798 *
1799 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
1800 * field of @lpfc_cmd for device with SLI-4 interface spec.
1801 *
1802 * Return codes:
1803 * 1 - Error
1804 * 0 - Success
1805 **/
1806static int
1807lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
1808{
1809 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1810 struct scatterlist *sgel = NULL;
1811 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1812 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
1813 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
1814 dma_addr_t physaddr;
1815 uint32_t num_bde = 0;
1816 uint32_t dma_len;
1817 uint32_t dma_offset = 0;
1818 int nseg;
1819
1820 /*
1821 * There are three possibilities here - use scatter-gather segment, use
1822 * the single mapping, or neither. Start the lpfc command prep by
1823 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1824 * data bde entry.
1825 */
1826 if (scsi_sg_count(scsi_cmnd)) {
1827 /*
1828 * The driver stores the segment count returned from pci_map_sg
1829 * because this a count of dma-mappings used to map the use_sg
1830 * pages. They are not guaranteed to be the same for those
1831 * architectures that implement an IOMMU.
1832 */
1833
1834 nseg = scsi_dma_map(scsi_cmnd);
1835 if (unlikely(!nseg))
1836 return 1;
1837 sgl += 1;
1838 /* clear the last flag in the fcp_rsp map entry */
1839 sgl->word2 = le32_to_cpu(sgl->word2);
1840 bf_set(lpfc_sli4_sge_last, sgl, 0);
1841 sgl->word2 = cpu_to_le32(sgl->word2);
1842 sgl += 1;
1843
1844 lpfc_cmd->seg_cnt = nseg;
1845 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001846 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
1847 " %s: Too many sg segments from "
1848 "dma_map_sg. Config %d, seg_cnt %d\n",
1849 __func__, phba->cfg_sg_seg_cnt,
James Smartda0436e2009-05-22 14:51:39 -04001850 lpfc_cmd->seg_cnt);
1851 scsi_dma_unmap(scsi_cmnd);
1852 return 1;
1853 }
1854
1855 /*
1856 * The driver established a maximum scatter-gather segment count
1857 * during probe that limits the number of sg elements in any
1858 * single scsi command. Just run through the seg_cnt and format
1859 * the sge's.
1860 * When using SLI-3 the driver will try to fit all the BDEs into
1861 * the IOCB. If it can't then the BDEs get added to a BPL as it
1862 * does for SLI-2 mode.
1863 */
1864 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
1865 physaddr = sg_dma_address(sgel);
1866 dma_len = sg_dma_len(sgel);
1867 bf_set(lpfc_sli4_sge_len, sgl, sg_dma_len(sgel));
1868 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
1869 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
1870 if ((num_bde + 1) == nseg)
1871 bf_set(lpfc_sli4_sge_last, sgl, 1);
1872 else
1873 bf_set(lpfc_sli4_sge_last, sgl, 0);
1874 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
1875 sgl->word2 = cpu_to_le32(sgl->word2);
1876 sgl->word3 = cpu_to_le32(sgl->word3);
1877 dma_offset += dma_len;
1878 sgl++;
1879 }
1880 } else {
1881 sgl += 1;
1882 /* clear the last flag in the fcp_rsp map entry */
1883 sgl->word2 = le32_to_cpu(sgl->word2);
1884 bf_set(lpfc_sli4_sge_last, sgl, 1);
1885 sgl->word2 = cpu_to_le32(sgl->word2);
1886 }
1887
1888 /*
1889 * Finish initializing those IOCB fields that are dependent on the
1890 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
1891 * explicitly reinitialized.
1892 * all iocb memory resources are reused.
1893 */
1894 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
1895
1896 /*
1897 * Due to difference in data length between DIF/non-DIF paths,
1898 * we need to set word 4 of IOCB here
1899 */
1900 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
1901 return 0;
1902}
1903
1904/**
James Smart3772a992009-05-22 14:50:54 -04001905 * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
1906 * @phba: The Hba for which this call is being executed.
1907 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1908 *
1909 * This routine wraps the actual DMA mapping function pointer from the
1910 * lpfc_hba struct.
1911 *
1912 * Return codes:
1913 * 1 - Error
1914 * 0 - Success
1915 **/
1916static inline int
1917lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
1918{
1919 return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
1920}
1921
1922/**
James Smart3621a712009-04-06 18:47:14 -04001923 * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
James Smartea2151b2008-09-07 11:52:10 -04001924 * @phba: Pointer to hba context object.
1925 * @vport: Pointer to vport object.
1926 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
1927 * @rsp_iocb: Pointer to response iocb object which reported error.
1928 *
1929 * This function posts an event when there is a SCSI command reporting
1930 * error from the scsi device.
1931 **/
1932static void
1933lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
1934 struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
1935 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
1936 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
1937 uint32_t resp_info = fcprsp->rspStatus2;
1938 uint32_t scsi_status = fcprsp->rspStatus3;
1939 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
1940 struct lpfc_fast_path_event *fast_path_evt = NULL;
1941 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
1942 unsigned long flags;
1943
1944 /* If there is queuefull or busy condition send a scsi event */
1945 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
1946 (cmnd->result == SAM_STAT_BUSY)) {
1947 fast_path_evt = lpfc_alloc_fast_evt(phba);
1948 if (!fast_path_evt)
1949 return;
1950 fast_path_evt->un.scsi_evt.event_type =
1951 FC_REG_SCSI_EVENT;
1952 fast_path_evt->un.scsi_evt.subcategory =
1953 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
1954 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
1955 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
1956 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
1957 &pnode->nlp_portname, sizeof(struct lpfc_name));
1958 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
1959 &pnode->nlp_nodename, sizeof(struct lpfc_name));
1960 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
1961 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
1962 fast_path_evt = lpfc_alloc_fast_evt(phba);
1963 if (!fast_path_evt)
1964 return;
1965 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
1966 FC_REG_SCSI_EVENT;
1967 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
1968 LPFC_EVENT_CHECK_COND;
1969 fast_path_evt->un.check_cond_evt.scsi_event.lun =
1970 cmnd->device->lun;
1971 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
1972 &pnode->nlp_portname, sizeof(struct lpfc_name));
1973 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
1974 &pnode->nlp_nodename, sizeof(struct lpfc_name));
1975 fast_path_evt->un.check_cond_evt.sense_key =
1976 cmnd->sense_buffer[2] & 0xf;
1977 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
1978 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
1979 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
1980 fcpi_parm &&
1981 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
1982 ((scsi_status == SAM_STAT_GOOD) &&
1983 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
1984 /*
1985 * If status is good or resid does not match with fcp_param and
1986 * there is valid fcpi_parm, then there is a read_check error
1987 */
1988 fast_path_evt = lpfc_alloc_fast_evt(phba);
1989 if (!fast_path_evt)
1990 return;
1991 fast_path_evt->un.read_check_error.header.event_type =
1992 FC_REG_FABRIC_EVENT;
1993 fast_path_evt->un.read_check_error.header.subcategory =
1994 LPFC_EVENT_FCPRDCHKERR;
1995 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
1996 &pnode->nlp_portname, sizeof(struct lpfc_name));
1997 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
1998 &pnode->nlp_nodename, sizeof(struct lpfc_name));
1999 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
2000 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
2001 fast_path_evt->un.read_check_error.fcpiparam =
2002 fcpi_parm;
2003 } else
2004 return;
2005
2006 fast_path_evt->vport = vport;
2007 spin_lock_irqsave(&phba->hbalock, flags);
2008 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
2009 spin_unlock_irqrestore(&phba->hbalock, flags);
2010 lpfc_worker_wake_up(phba);
2011 return;
2012}
James Smart9bad7672008-12-04 22:39:02 -05002013
2014/**
James Smartf1126682009-06-10 17:22:44 -04002015 * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
James Smart3772a992009-05-22 14:50:54 -04002016 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -05002017 * @psb: The scsi buffer which is going to be un-mapped.
2018 *
2019 * This routine does DMA un-mapping of scatter gather list of scsi command
James Smart3772a992009-05-22 14:50:54 -04002020 * field of @lpfc_cmd for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05002021 **/
dea31012005-04-17 16:05:31 -05002022static void
James Smartf1126682009-06-10 17:22:44 -04002023lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James Smartbcf4dbf2006-07-06 15:50:08 -04002024{
2025 /*
2026 * There are only two special cases to consider. (1) the scsi command
2027 * requested scatter-gather usage or (2) the scsi command allocated
2028 * a request buffer, but did not request use_sg. There is a third
2029 * case, but it does not require resource deallocation.
2030 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002031 if (psb->seg_cnt > 0)
2032 scsi_dma_unmap(psb->pCmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002033 if (psb->prot_seg_cnt > 0)
2034 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
2035 scsi_prot_sg_count(psb->pCmd),
2036 psb->pCmd->sc_data_direction);
James Smartbcf4dbf2006-07-06 15:50:08 -04002037}
2038
James Smart9bad7672008-12-04 22:39:02 -05002039/**
James Smart3621a712009-04-06 18:47:14 -04002040 * lpfc_handler_fcp_err - FCP response handler
James Smart9bad7672008-12-04 22:39:02 -05002041 * @vport: The virtual port for which this call is being executed.
2042 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
2043 * @rsp_iocb: The response IOCB which contains FCP error.
2044 *
2045 * This routine is called to process response IOCB with status field
2046 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
2047 * based upon SCSI and FCP error.
2048 **/
James Smartbcf4dbf2006-07-06 15:50:08 -04002049static void
James Smart2e0fef82007-06-17 19:56:36 -05002050lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
2051 struct lpfc_iocbq *rsp_iocb)
dea31012005-04-17 16:05:31 -05002052{
2053 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
2054 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
2055 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
James Smart7054a602007-04-25 09:52:34 -04002056 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
dea31012005-04-17 16:05:31 -05002057 uint32_t resp_info = fcprsp->rspStatus2;
2058 uint32_t scsi_status = fcprsp->rspStatus3;
James Smartc7743952006-12-02 13:34:42 -05002059 uint32_t *lp;
dea31012005-04-17 16:05:31 -05002060 uint32_t host_status = DID_OK;
2061 uint32_t rsplen = 0;
James Smartc7743952006-12-02 13:34:42 -05002062 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea31012005-04-17 16:05:31 -05002063
James Smartea2151b2008-09-07 11:52:10 -04002064
dea31012005-04-17 16:05:31 -05002065 /*
2066 * If this is a task management command, there is no
2067 * scsi packet associated with this lpfc_cmd. The driver
2068 * consumes it.
2069 */
2070 if (fcpcmd->fcpCntl2) {
2071 scsi_status = 0;
2072 goto out;
2073 }
2074
James Smart6a9c52c2009-10-02 15:16:51 -04002075 if (resp_info & RSP_LEN_VALID) {
2076 rsplen = be32_to_cpu(fcprsp->rspRspLen);
2077 if ((rsplen != 0 && rsplen != 4 && rsplen != 8) ||
2078 (fcprsp->rspInfo3 != RSP_NO_FAILURE)) {
2079 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
2080 "2719 Invalid response length: "
2081 "tgt x%x lun x%x cmnd x%x rsplen x%x\n",
2082 cmnd->device->id,
2083 cmnd->device->lun, cmnd->cmnd[0],
2084 rsplen);
2085 host_status = DID_ERROR;
2086 goto out;
2087 }
2088 }
2089
James Smartc7743952006-12-02 13:34:42 -05002090 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
2091 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
2092 if (snslen > SCSI_SENSE_BUFFERSIZE)
2093 snslen = SCSI_SENSE_BUFFERSIZE;
2094
2095 if (resp_info & RSP_LEN_VALID)
2096 rsplen = be32_to_cpu(fcprsp->rspRspLen);
2097 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
2098 }
2099 lp = (uint32_t *)cmnd->sense_buffer;
2100
2101 if (!scsi_status && (resp_info & RESID_UNDER))
2102 logit = LOG_FCP;
2103
James Smarte8b62012007-08-02 11:10:09 -04002104 lpfc_printf_vlog(vport, KERN_WARNING, logit,
James Smarte2a0a9d2008-12-04 22:40:02 -05002105 "9024 FCP command x%x failed: x%x SNS x%x x%x "
James Smarte8b62012007-08-02 11:10:09 -04002106 "Data: x%x x%x x%x x%x x%x\n",
2107 cmnd->cmnd[0], scsi_status,
2108 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
2109 be32_to_cpu(fcprsp->rspResId),
2110 be32_to_cpu(fcprsp->rspSnsLen),
2111 be32_to_cpu(fcprsp->rspRspLen),
2112 fcprsp->rspInfo3);
dea31012005-04-17 16:05:31 -05002113
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002114 scsi_set_resid(cmnd, 0);
dea31012005-04-17 16:05:31 -05002115 if (resp_info & RESID_UNDER) {
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002116 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea31012005-04-17 16:05:31 -05002117
James Smarte8b62012007-08-02 11:10:09 -04002118 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05002119 "9025 FCP Read Underrun, expected %d, "
James Smarte8b62012007-08-02 11:10:09 -04002120 "residual %d Data: x%x x%x x%x\n",
2121 be32_to_cpu(fcpcmd->fcpDl),
2122 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
2123 cmnd->underflow);
dea31012005-04-17 16:05:31 -05002124
2125 /*
James Smart7054a602007-04-25 09:52:34 -04002126 * If there is an under run check if under run reported by
2127 * storage array is same as the under run reported by HBA.
2128 * If this is not same, there is a dropped frame.
2129 */
2130 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
2131 fcpi_parm &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002132 (scsi_get_resid(cmnd) != fcpi_parm)) {
James Smarte8b62012007-08-02 11:10:09 -04002133 lpfc_printf_vlog(vport, KERN_WARNING,
2134 LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05002135 "9026 FCP Read Check Error "
James Smarte8b62012007-08-02 11:10:09 -04002136 "and Underrun Data: x%x x%x x%x x%x\n",
2137 be32_to_cpu(fcpcmd->fcpDl),
2138 scsi_get_resid(cmnd), fcpi_parm,
2139 cmnd->cmnd[0]);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002140 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
James Smart7054a602007-04-25 09:52:34 -04002141 host_status = DID_ERROR;
2142 }
2143 /*
dea31012005-04-17 16:05:31 -05002144 * The cmnd->underflow is the minimum number of bytes that must
2145 * be transfered for this command. Provided a sense condition
2146 * is not present, make sure the actual amount transferred is at
2147 * least the underflow value or fail.
2148 */
2149 if (!(resp_info & SNS_LEN_VALID) &&
2150 (scsi_status == SAM_STAT_GOOD) &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002151 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
2152 < cmnd->underflow)) {
James Smarte8b62012007-08-02 11:10:09 -04002153 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05002154 "9027 FCP command x%x residual "
James Smarte8b62012007-08-02 11:10:09 -04002155 "underrun converted to error "
2156 "Data: x%x x%x x%x\n",
James Smart66dbfbe2007-08-05 06:08:38 -04002157 cmnd->cmnd[0], scsi_bufflen(cmnd),
James Smarte8b62012007-08-02 11:10:09 -04002158 scsi_get_resid(cmnd), cmnd->underflow);
dea31012005-04-17 16:05:31 -05002159 host_status = DID_ERROR;
2160 }
2161 } else if (resp_info & RESID_OVER) {
James Smarte8b62012007-08-02 11:10:09 -04002162 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05002163 "9028 FCP command x%x residual overrun error. "
James Smarte4e74272009-07-19 10:01:38 -04002164 "Data: x%x x%x\n", cmnd->cmnd[0],
James Smarte8b62012007-08-02 11:10:09 -04002165 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea31012005-04-17 16:05:31 -05002166 host_status = DID_ERROR;
2167
2168 /*
2169 * Check SLI validation that all the transfer was actually done
2170 * (fcpi_parm should be zero). Apply check only to reads.
2171 */
2172 } else if ((scsi_status == SAM_STAT_GOOD) && fcpi_parm &&
2173 (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
James Smarte8b62012007-08-02 11:10:09 -04002174 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05002175 "9029 FCP Read Check Error Data: "
James Smarte8b62012007-08-02 11:10:09 -04002176 "x%x x%x x%x x%x\n",
2177 be32_to_cpu(fcpcmd->fcpDl),
2178 be32_to_cpu(fcprsp->rspResId),
2179 fcpi_parm, cmnd->cmnd[0]);
dea31012005-04-17 16:05:31 -05002180 host_status = DID_ERROR;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002181 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea31012005-04-17 16:05:31 -05002182 }
2183
2184 out:
2185 cmnd->result = ScsiResult(host_status, scsi_status);
James Smartea2151b2008-09-07 11:52:10 -04002186 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
dea31012005-04-17 16:05:31 -05002187}
2188
James Smart9bad7672008-12-04 22:39:02 -05002189/**
James Smart3621a712009-04-06 18:47:14 -04002190 * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
James Smart9bad7672008-12-04 22:39:02 -05002191 * @phba: The Hba for which this call is being executed.
2192 * @pIocbIn: The command IOCBQ for the scsi cmnd.
James Smart3772a992009-05-22 14:50:54 -04002193 * @pIocbOut: The response IOCBQ for the scsi cmnd.
James Smart9bad7672008-12-04 22:39:02 -05002194 *
2195 * This routine assigns scsi command result by looking into response IOCB
2196 * status field appropriately. This routine handles QUEUE FULL condition as
2197 * well by ramping down device queue depth.
2198 **/
dea31012005-04-17 16:05:31 -05002199static void
2200lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
2201 struct lpfc_iocbq *pIocbOut)
2202{
2203 struct lpfc_scsi_buf *lpfc_cmd =
2204 (struct lpfc_scsi_buf *) pIocbIn->context1;
James Smart2e0fef82007-06-17 19:56:36 -05002205 struct lpfc_vport *vport = pIocbIn->vport;
dea31012005-04-17 16:05:31 -05002206 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
2207 struct lpfc_nodelist *pnode = rdata->pnode;
2208 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002209 int result;
James Smarta257bf92009-04-06 18:48:10 -04002210 struct scsi_device *tmp_sdev;
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002211 int depth = 0;
James Smartfa61a542008-01-11 01:52:42 -05002212 unsigned long flags;
James Smartea2151b2008-09-07 11:52:10 -04002213 struct lpfc_fast_path_event *fast_path_evt;
James Smarta257bf92009-04-06 18:48:10 -04002214 struct Scsi_Host *shost = cmd->device->host;
2215 uint32_t queue_depth, scsi_id;
dea31012005-04-17 16:05:31 -05002216
2217 lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
2218 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
James Smart109f6ed2008-12-04 22:39:08 -05002219 if (pnode && NLP_CHK_NODE_ACT(pnode))
2220 atomic_dec(&pnode->cmd_pending);
dea31012005-04-17 16:05:31 -05002221
2222 if (lpfc_cmd->status) {
2223 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
2224 (lpfc_cmd->result & IOERR_DRVR_MASK))
2225 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
2226 else if (lpfc_cmd->status >= IOSTAT_CNT)
2227 lpfc_cmd->status = IOSTAT_DEFAULT;
2228
James Smarte8b62012007-08-02 11:10:09 -04002229 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05002230 "9030 FCP cmd x%x failed <%d/%d> "
James Smarte8b62012007-08-02 11:10:09 -04002231 "status: x%x result: x%x Data: x%x x%x\n",
2232 cmd->cmnd[0],
2233 cmd->device ? cmd->device->id : 0xffff,
2234 cmd->device ? cmd->device->lun : 0xffff,
2235 lpfc_cmd->status, lpfc_cmd->result,
2236 pIocbOut->iocb.ulpContext,
2237 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
dea31012005-04-17 16:05:31 -05002238
2239 switch (lpfc_cmd->status) {
2240 case IOSTAT_FCP_RSP_ERROR:
2241 /* Call FCP RSP handler to determine result */
James Smart2e0fef82007-06-17 19:56:36 -05002242 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea31012005-04-17 16:05:31 -05002243 break;
2244 case IOSTAT_NPORT_BSY:
2245 case IOSTAT_FABRIC_BSY:
James Smart0f1f53a2008-08-24 21:50:18 -04002246 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
James Smartea2151b2008-09-07 11:52:10 -04002247 fast_path_evt = lpfc_alloc_fast_evt(phba);
2248 if (!fast_path_evt)
2249 break;
2250 fast_path_evt->un.fabric_evt.event_type =
2251 FC_REG_FABRIC_EVENT;
2252 fast_path_evt->un.fabric_evt.subcategory =
2253 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
2254 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
2255 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
2256 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
2257 &pnode->nlp_portname,
2258 sizeof(struct lpfc_name));
2259 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
2260 &pnode->nlp_nodename,
2261 sizeof(struct lpfc_name));
2262 }
2263 fast_path_evt->vport = vport;
2264 fast_path_evt->work_evt.evt =
2265 LPFC_EVT_FASTPATH_MGMT_EVT;
2266 spin_lock_irqsave(&phba->hbalock, flags);
2267 list_add_tail(&fast_path_evt->work_evt.evt_listp,
2268 &phba->work_list);
2269 spin_unlock_irqrestore(&phba->hbalock, flags);
2270 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05002271 break;
James Smart92d7f7b2007-06-17 19:56:38 -05002272 case IOSTAT_LOCAL_REJECT:
James Smartd7c255b2008-08-24 21:50:00 -04002273 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
James Smart92d7f7b2007-06-17 19:56:38 -05002274 lpfc_cmd->result == IOERR_NO_RESOURCES ||
James Smartd7c255b2008-08-24 21:50:00 -04002275 lpfc_cmd->result == IOERR_ABORT_REQUESTED) {
James Smart92d7f7b2007-06-17 19:56:38 -05002276 cmd->result = ScsiResult(DID_REQUEUE, 0);
James Smart58da1ff2008-04-07 10:15:56 -04002277 break;
James Smarte2a0a9d2008-12-04 22:40:02 -05002278 }
2279
2280 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
2281 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
2282 pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
2283 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
2284 /*
2285 * This is a response for a BG enabled
2286 * cmd. Parse BG error
2287 */
2288 lpfc_parse_bg_err(phba, lpfc_cmd,
2289 pIocbOut);
2290 break;
2291 } else {
2292 lpfc_printf_vlog(vport, KERN_WARNING,
2293 LOG_BG,
2294 "9031 non-zero BGSTAT "
James Smart6a9c52c2009-10-02 15:16:51 -04002295 "on unprotected cmd\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05002296 }
2297 }
2298
2299 /* else: fall through */
dea31012005-04-17 16:05:31 -05002300 default:
2301 cmd->result = ScsiResult(DID_ERROR, 0);
2302 break;
2303 }
2304
James Smart58da1ff2008-04-07 10:15:56 -04002305 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002306 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
James Smart0f1f53a2008-08-24 21:50:18 -04002307 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
2308 SAM_STAT_BUSY);
dea31012005-04-17 16:05:31 -05002309 } else {
2310 cmd->result = ScsiResult(DID_OK, 0);
2311 }
2312
2313 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
2314 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
2315
James Smarte8b62012007-08-02 11:10:09 -04002316 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
2317 "0710 Iodone <%d/%d> cmd %p, error "
2318 "x%x SNS x%x x%x Data: x%x x%x\n",
2319 cmd->device->id, cmd->device->lun, cmd,
2320 cmd->result, *lp, *(lp + 3), cmd->retries,
2321 scsi_get_resid(cmd));
dea31012005-04-17 16:05:31 -05002322 }
2323
James Smartea2151b2008-09-07 11:52:10 -04002324 lpfc_update_stats(phba, lpfc_cmd);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002325 result = cmd->result;
James Smart977b5a02008-09-07 11:52:04 -04002326 if (vport->cfg_max_scsicmpl_time &&
2327 time_after(jiffies, lpfc_cmd->start_time +
2328 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
James Smarta257bf92009-04-06 18:48:10 -04002329 spin_lock_irqsave(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05002330 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
2331 if (pnode->cmd_qdepth >
2332 atomic_read(&pnode->cmd_pending) &&
2333 (atomic_read(&pnode->cmd_pending) >
2334 LPFC_MIN_TGT_QDEPTH) &&
2335 ((cmd->cmnd[0] == READ_10) ||
2336 (cmd->cmnd[0] == WRITE_10)))
2337 pnode->cmd_qdepth =
2338 atomic_read(&pnode->cmd_pending);
James Smart977b5a02008-09-07 11:52:04 -04002339
James Smart109f6ed2008-12-04 22:39:08 -05002340 pnode->last_change_time = jiffies;
2341 }
James Smarta257bf92009-04-06 18:48:10 -04002342 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05002343 } else if (pnode && NLP_CHK_NODE_ACT(pnode)) {
2344 if ((pnode->cmd_qdepth < LPFC_MAX_TGT_QDEPTH) &&
James Smart977b5a02008-09-07 11:52:04 -04002345 time_after(jiffies, pnode->last_change_time +
James Smart109f6ed2008-12-04 22:39:08 -05002346 msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
James Smarta257bf92009-04-06 18:48:10 -04002347 spin_lock_irqsave(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05002348 pnode->cmd_qdepth += pnode->cmd_qdepth *
2349 LPFC_TGTQ_RAMPUP_PCENT / 100;
2350 if (pnode->cmd_qdepth > LPFC_MAX_TGT_QDEPTH)
2351 pnode->cmd_qdepth = LPFC_MAX_TGT_QDEPTH;
2352 pnode->last_change_time = jiffies;
James Smarta257bf92009-04-06 18:48:10 -04002353 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05002354 }
James Smart977b5a02008-09-07 11:52:04 -04002355 }
2356
James Smart1dcb58e2007-04-25 09:51:30 -04002357 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James Smarta257bf92009-04-06 18:48:10 -04002358
2359 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
2360 queue_depth = cmd->device->queue_depth;
2361 scsi_id = cmd->device->id;
dea31012005-04-17 16:05:31 -05002362 cmd->scsi_done(cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002363
Jamie Wellnitzb8086082006-02-28 22:33:12 -05002364 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smartfa61a542008-01-11 01:52:42 -05002365 /*
2366 * If there is a thread waiting for command completion
2367 * wake up the thread.
2368 */
James Smarta257bf92009-04-06 18:48:10 -04002369 spin_lock_irqsave(shost->host_lock, flags);
James Smart495a7142008-06-14 22:52:59 -04002370 lpfc_cmd->pCmd = NULL;
James Smartfa61a542008-01-11 01:52:42 -05002371 if (lpfc_cmd->waitq)
2372 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04002373 spin_unlock_irqrestore(shost->host_lock, flags);
Jamie Wellnitzb8086082006-02-28 22:33:12 -05002374 lpfc_release_scsi_buf(phba, lpfc_cmd);
2375 return;
2376 }
2377
James Smart92d7f7b2007-06-17 19:56:38 -05002378
2379 if (!result)
James Smarta257bf92009-04-06 18:48:10 -04002380 lpfc_rampup_queue_depth(vport, queue_depth);
James Smart92d7f7b2007-06-17 19:56:38 -05002381
James Smart58da1ff2008-04-07 10:15:56 -04002382 if (!result && pnode && NLP_CHK_NODE_ACT(pnode) &&
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002383 ((jiffies - pnode->last_ramp_up_time) >
2384 LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
2385 ((jiffies - pnode->last_q_full_time) >
2386 LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
James Smarta257bf92009-04-06 18:48:10 -04002387 (vport->cfg_lun_queue_depth > queue_depth)) {
2388 shost_for_each_device(tmp_sdev, shost) {
James Smart3de2a652007-08-02 11:09:59 -04002389 if (vport->cfg_lun_queue_depth > tmp_sdev->queue_depth){
James Smarta257bf92009-04-06 18:48:10 -04002390 if (tmp_sdev->id != scsi_id)
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002391 continue;
2392 if (tmp_sdev->ordered_tags)
2393 scsi_adjust_queue_depth(tmp_sdev,
2394 MSG_ORDERED_TAG,
2395 tmp_sdev->queue_depth+1);
2396 else
2397 scsi_adjust_queue_depth(tmp_sdev,
2398 MSG_SIMPLE_TAG,
2399 tmp_sdev->queue_depth+1);
2400
2401 pnode->last_ramp_up_time = jiffies;
2402 }
2403 }
James Smartea2151b2008-09-07 11:52:10 -04002404 lpfc_send_sdev_queuedepth_change_event(phba, vport, pnode,
2405 0xFFFFFFFF,
James Smarta257bf92009-04-06 18:48:10 -04002406 queue_depth , queue_depth + 1);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002407 }
2408
2409 /*
2410 * Check for queue full. If the lun is reporting queue full, then
2411 * back off the lun queue depth to prevent target overloads.
2412 */
James Smart58da1ff2008-04-07 10:15:56 -04002413 if (result == SAM_STAT_TASK_SET_FULL && pnode &&
2414 NLP_CHK_NODE_ACT(pnode)) {
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002415 pnode->last_q_full_time = jiffies;
2416
James Smarta257bf92009-04-06 18:48:10 -04002417 shost_for_each_device(tmp_sdev, shost) {
2418 if (tmp_sdev->id != scsi_id)
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002419 continue;
2420 depth = scsi_track_queue_full(tmp_sdev,
2421 tmp_sdev->queue_depth - 1);
2422 }
2423 /*
James Smart2e0fef82007-06-17 19:56:36 -05002424 * The queue depth cannot be lowered any more.
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002425 * Modify the returned error code to store
2426 * the final depth value set by
2427 * scsi_track_queue_full.
2428 */
2429 if (depth == -1)
James Smarta257bf92009-04-06 18:48:10 -04002430 depth = shost->cmd_per_lun;
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002431
2432 if (depth) {
James Smarte8b62012007-08-02 11:10:09 -04002433 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
2434 "0711 detected queue full - lun queue "
2435 "depth adjusted to %d.\n", depth);
James Smartea2151b2008-09-07 11:52:10 -04002436 lpfc_send_sdev_queuedepth_change_event(phba, vport,
2437 pnode, 0xFFFFFFFF,
2438 depth+1, depth);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002439 }
2440 }
2441
James Smartfa61a542008-01-11 01:52:42 -05002442 /*
2443 * If there is a thread waiting for command completion
2444 * wake up the thread.
2445 */
James Smarta257bf92009-04-06 18:48:10 -04002446 spin_lock_irqsave(shost->host_lock, flags);
James Smart495a7142008-06-14 22:52:59 -04002447 lpfc_cmd->pCmd = NULL;
James Smartfa61a542008-01-11 01:52:42 -05002448 if (lpfc_cmd->waitq)
2449 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04002450 spin_unlock_irqrestore(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05002451
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002452 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05002453}
2454
James Smart34b02dc2008-08-24 21:49:55 -04002455/**
James Smart3621a712009-04-06 18:47:14 -04002456 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
James Smart34b02dc2008-08-24 21:49:55 -04002457 * @data: A pointer to the immediate command data portion of the IOCB.
2458 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
2459 *
2460 * The routine copies the entire FCP command from @fcp_cmnd to @data while
2461 * byte swapping the data to big endian format for transmission on the wire.
2462 **/
2463static void
2464lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
2465{
2466 int i, j;
2467 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
2468 i += sizeof(uint32_t), j++) {
2469 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
2470 }
2471}
2472
James Smart9bad7672008-12-04 22:39:02 -05002473/**
James Smartf1126682009-06-10 17:22:44 -04002474 * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05002475 * @vport: The virtual port for which this call is being executed.
2476 * @lpfc_cmd: The scsi command which needs to send.
2477 * @pnode: Pointer to lpfc_nodelist.
2478 *
2479 * This routine initializes fcp_cmnd and iocb data structure from scsi command
James Smart3772a992009-05-22 14:50:54 -04002480 * to transfer for device with SLI3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05002481 **/
dea31012005-04-17 16:05:31 -05002482static void
James Smartf1126682009-06-10 17:22:44 -04002483lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
James Smart2e0fef82007-06-17 19:56:36 -05002484 struct lpfc_nodelist *pnode)
dea31012005-04-17 16:05:31 -05002485{
James Smart2e0fef82007-06-17 19:56:36 -05002486 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002487 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2488 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2489 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2490 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
2491 int datadir = scsi_cmnd->sc_data_direction;
James Smart7e2b19f2007-10-29 11:00:39 -04002492 char tag[2];
dea31012005-04-17 16:05:31 -05002493
James Smart58da1ff2008-04-07 10:15:56 -04002494 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
2495 return;
2496
dea31012005-04-17 16:05:31 -05002497 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
James.Smart@Emulex.Com69859dc2005-08-10 15:02:37 -04002498 /* clear task management bits */
2499 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea31012005-04-17 16:05:31 -05002500
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -04002501 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
2502 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05002503
2504 memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, 16);
2505
James Smart7e2b19f2007-10-29 11:00:39 -04002506 if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
2507 switch (tag[0]) {
dea31012005-04-17 16:05:31 -05002508 case HEAD_OF_QUEUE_TAG:
2509 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
2510 break;
2511 case ORDERED_QUEUE_TAG:
2512 fcp_cmnd->fcpCntl1 = ORDERED_Q;
2513 break;
2514 default:
2515 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
2516 break;
2517 }
2518 } else
2519 fcp_cmnd->fcpCntl1 = 0;
2520
2521 /*
2522 * There are three possibilities here - use scatter-gather segment, use
2523 * the single mapping, or neither. Start the lpfc command prep by
2524 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
2525 * data bde entry.
2526 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002527 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05002528 if (datadir == DMA_TO_DEVICE) {
2529 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
James Smart3772a992009-05-22 14:50:54 -04002530 if (phba->sli_rev < LPFC_SLI_REV4) {
2531 iocb_cmd->un.fcpi.fcpi_parm = 0;
2532 iocb_cmd->ulpPU = 0;
2533 } else
2534 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05002535 fcp_cmnd->fcpCntl3 = WRITE_DATA;
2536 phba->fc4OutputRequests++;
2537 } else {
2538 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
2539 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05002540 fcp_cmnd->fcpCntl3 = READ_DATA;
2541 phba->fc4InputRequests++;
2542 }
2543 } else {
2544 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
2545 iocb_cmd->un.fcpi.fcpi_parm = 0;
2546 iocb_cmd->ulpPU = 0;
2547 fcp_cmnd->fcpCntl3 = 0;
2548 phba->fc4ControlRequests++;
2549 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002550 if (phba->sli_rev == 3 &&
2551 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04002552 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05002553 /*
2554 * Finish initializing those IOCB fields that are independent
2555 * of the scsi_cmnd request_buffer
2556 */
2557 piocbq->iocb.ulpContext = pnode->nlp_rpi;
2558 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
2559 piocbq->iocb.ulpFCP2Rcvy = 1;
James Smart09372822008-01-11 01:52:54 -05002560 else
2561 piocbq->iocb.ulpFCP2Rcvy = 0;
dea31012005-04-17 16:05:31 -05002562
2563 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
2564 piocbq->context1 = lpfc_cmd;
2565 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
2566 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
James Smart2e0fef82007-06-17 19:56:36 -05002567 piocbq->vport = vport;
dea31012005-04-17 16:05:31 -05002568}
2569
James Smart9bad7672008-12-04 22:39:02 -05002570/**
James Smartf1126682009-06-10 17:22:44 -04002571 * lpfc_scsi_prep_task_mgmt_cmnd - Convert SLI3 scsi TM cmd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05002572 * @vport: The virtual port for which this call is being executed.
2573 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
2574 * @lun: Logical unit number.
2575 * @task_mgmt_cmd: SCSI task management command.
2576 *
James Smart3772a992009-05-22 14:50:54 -04002577 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
2578 * for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05002579 *
2580 * Return codes:
2581 * 0 - Error
2582 * 1 - Success
2583 **/
dea31012005-04-17 16:05:31 -05002584static int
James Smartf1126682009-06-10 17:22:44 -04002585lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
dea31012005-04-17 16:05:31 -05002586 struct lpfc_scsi_buf *lpfc_cmd,
James Smart420b630d2006-07-06 15:50:16 -04002587 unsigned int lun,
dea31012005-04-17 16:05:31 -05002588 uint8_t task_mgmt_cmd)
2589{
dea31012005-04-17 16:05:31 -05002590 struct lpfc_iocbq *piocbq;
2591 IOCB_t *piocb;
2592 struct fcp_cmnd *fcp_cmnd;
James Smart0b18ac42006-05-01 21:50:40 -04002593 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea31012005-04-17 16:05:31 -05002594 struct lpfc_nodelist *ndlp = rdata->pnode;
2595
James Smart58da1ff2008-04-07 10:15:56 -04002596 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
2597 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05002598 return 0;
dea31012005-04-17 16:05:31 -05002599
dea31012005-04-17 16:05:31 -05002600 piocbq = &(lpfc_cmd->cur_iocbq);
James Smart2e0fef82007-06-17 19:56:36 -05002601 piocbq->vport = vport;
2602
dea31012005-04-17 16:05:31 -05002603 piocb = &piocbq->iocb;
2604
2605 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart34b02dc2008-08-24 21:49:55 -04002606 /* Clear out any old data in the FCP command area */
2607 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
2608 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05002609 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
James Smarte2a0a9d2008-12-04 22:40:02 -05002610 if (vport->phba->sli_rev == 3 &&
2611 !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04002612 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05002613 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
dea31012005-04-17 16:05:31 -05002614 piocb->ulpContext = ndlp->nlp_rpi;
2615 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
2616 piocb->ulpFCP2Rcvy = 1;
2617 }
2618 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
2619
2620 /* ulpTimeout is only one byte */
2621 if (lpfc_cmd->timeout > 0xff) {
2622 /*
2623 * Do not timeout the command at the firmware level.
2624 * The driver will provide the timeout mechanism.
2625 */
2626 piocb->ulpTimeout = 0;
James Smartf1126682009-06-10 17:22:44 -04002627 } else
dea31012005-04-17 16:05:31 -05002628 piocb->ulpTimeout = lpfc_cmd->timeout;
James Smartf1126682009-06-10 17:22:44 -04002629
2630 if (vport->phba->sli_rev == LPFC_SLI_REV4)
2631 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05002632
James Smart2e0fef82007-06-17 19:56:36 -05002633 return 1;
dea31012005-04-17 16:05:31 -05002634}
2635
James Smart9bad7672008-12-04 22:39:02 -05002636/**
James Smart3772a992009-05-22 14:50:54 -04002637 * lpfc_scsi_api_table_setup - Set up scsi api fucntion jump table
2638 * @phba: The hba struct for which this call is being executed.
2639 * @dev_grp: The HBA PCI-Device group number.
2640 *
2641 * This routine sets up the SCSI interface API function jump table in @phba
2642 * struct.
2643 * Returns: 0 - success, -ENODEV - failure.
2644 **/
2645int
2646lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
2647{
2648
James Smartf1126682009-06-10 17:22:44 -04002649 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
2650 phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
2651 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf;
2652
James Smart3772a992009-05-22 14:50:54 -04002653 switch (dev_grp) {
2654 case LPFC_PCI_DEV_LP:
2655 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s3;
2656 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04002657 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
2658 break;
James Smartda0436e2009-05-22 14:51:39 -04002659 case LPFC_PCI_DEV_OC:
2660 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s4;
2661 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04002662 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
2663 break;
James Smart3772a992009-05-22 14:50:54 -04002664 default:
2665 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2666 "1418 Invalid HBA PCI-device group: 0x%x\n",
2667 dev_grp);
2668 return -ENODEV;
2669 break;
2670 }
2671 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf;
2672 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
2673 return 0;
2674}
2675
2676/**
James Smart3621a712009-04-06 18:47:14 -04002677 * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
James Smart9bad7672008-12-04 22:39:02 -05002678 * @phba: The Hba for which this call is being executed.
2679 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
2680 * @rspiocbq: Pointer to lpfc_iocbq data structure.
2681 *
2682 * This routine is IOCB completion routine for device reset and target reset
2683 * routine. This routine release scsi buffer associated with lpfc_cmd.
2684 **/
James Smart7054a602007-04-25 09:52:34 -04002685static void
2686lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
2687 struct lpfc_iocbq *cmdiocbq,
2688 struct lpfc_iocbq *rspiocbq)
2689{
2690 struct lpfc_scsi_buf *lpfc_cmd =
2691 (struct lpfc_scsi_buf *) cmdiocbq->context1;
2692 if (lpfc_cmd)
2693 lpfc_release_scsi_buf(phba, lpfc_cmd);
2694 return;
2695}
2696
James Smart9bad7672008-12-04 22:39:02 -05002697/**
James Smart3621a712009-04-06 18:47:14 -04002698 * lpfc_info - Info entry point of scsi_host_template data structure
James Smart9bad7672008-12-04 22:39:02 -05002699 * @host: The scsi host for which this call is being executed.
2700 *
2701 * This routine provides module information about hba.
2702 *
2703 * Reutrn code:
2704 * Pointer to char - Success.
2705 **/
dea31012005-04-17 16:05:31 -05002706const char *
2707lpfc_info(struct Scsi_Host *host)
2708{
James Smart2e0fef82007-06-17 19:56:36 -05002709 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
2710 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002711 int len;
2712 static char lpfcinfobuf[384];
2713
2714 memset(lpfcinfobuf,0,384);
2715 if (phba && phba->pcidev){
2716 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
2717 len = strlen(lpfcinfobuf);
2718 snprintf(lpfcinfobuf + len,
2719 384-len,
2720 " on PCI bus %02x device %02x irq %d",
2721 phba->pcidev->bus->number,
2722 phba->pcidev->devfn,
2723 phba->pcidev->irq);
2724 len = strlen(lpfcinfobuf);
2725 if (phba->Port[0]) {
2726 snprintf(lpfcinfobuf + len,
2727 384-len,
2728 " port %s",
2729 phba->Port);
2730 }
2731 }
2732 return lpfcinfobuf;
2733}
2734
James Smart9bad7672008-12-04 22:39:02 -05002735/**
James Smart3621a712009-04-06 18:47:14 -04002736 * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
James Smart9bad7672008-12-04 22:39:02 -05002737 * @phba: The Hba for which this call is being executed.
2738 *
2739 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
2740 * The default value of cfg_poll_tmo is 10 milliseconds.
2741 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002742static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
2743{
2744 unsigned long poll_tmo_expires =
2745 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
2746
2747 if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
2748 mod_timer(&phba->fcp_poll_timer,
2749 poll_tmo_expires);
2750}
2751
James Smart9bad7672008-12-04 22:39:02 -05002752/**
James Smart3621a712009-04-06 18:47:14 -04002753 * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
James Smart9bad7672008-12-04 22:39:02 -05002754 * @phba: The Hba for which this call is being executed.
2755 *
2756 * This routine starts the fcp_poll_timer of @phba.
2757 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002758void lpfc_poll_start_timer(struct lpfc_hba * phba)
2759{
2760 lpfc_poll_rearm_timer(phba);
2761}
2762
James Smart9bad7672008-12-04 22:39:02 -05002763/**
James Smart3621a712009-04-06 18:47:14 -04002764 * lpfc_poll_timeout - Restart polling timer
James Smart9bad7672008-12-04 22:39:02 -05002765 * @ptr: Map to lpfc_hba data structure pointer.
2766 *
2767 * This routine restarts fcp_poll timer, when FCP ring polling is enable
2768 * and FCP Ring interrupt is disable.
2769 **/
2770
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002771void lpfc_poll_timeout(unsigned long ptr)
2772{
James Smart2e0fef82007-06-17 19:56:36 -05002773 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002774
2775 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
2776 lpfc_sli_poll_fcp_ring (phba);
2777 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
2778 lpfc_poll_rearm_timer(phba);
2779 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002780}
2781
James Smart9bad7672008-12-04 22:39:02 -05002782/**
James Smart3621a712009-04-06 18:47:14 -04002783 * lpfc_queuecommand - scsi_host_template queuecommand entry point
James Smart9bad7672008-12-04 22:39:02 -05002784 * @cmnd: Pointer to scsi_cmnd data structure.
2785 * @done: Pointer to done routine.
2786 *
2787 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
2788 * This routine prepares an IOCB from scsi command and provides to firmware.
2789 * The @done callback is invoked after driver finished processing the command.
2790 *
2791 * Return value :
2792 * 0 - Success
2793 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
2794 **/
dea31012005-04-17 16:05:31 -05002795static int
2796lpfc_queuecommand(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
2797{
James Smart2e0fef82007-06-17 19:56:36 -05002798 struct Scsi_Host *shost = cmnd->device->host;
2799 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2800 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002801 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
2802 struct lpfc_nodelist *ndlp = rdata->pnode;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002803 struct lpfc_scsi_buf *lpfc_cmd;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002804 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002805 int err;
dea31012005-04-17 16:05:31 -05002806
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002807 err = fc_remote_port_chkready(rport);
2808 if (err) {
2809 cmnd->result = err;
dea31012005-04-17 16:05:31 -05002810 goto out_fail_command;
2811 }
2812
James Smarte2a0a9d2008-12-04 22:40:02 -05002813 if (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
2814 scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
2815
James Smart6a9c52c2009-10-02 15:16:51 -04002816 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2817 "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
2818 " op:%02x str=%s without registering for"
2819 " BlockGuard - Rejecting command\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002820 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
2821 dif_op_str[scsi_get_prot_op(cmnd)]);
2822 goto out_fail_command;
2823 }
2824
dea31012005-04-17 16:05:31 -05002825 /*
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002826 * Catch race where our node has transitioned, but the
2827 * transport is still transitioning.
dea31012005-04-17 16:05:31 -05002828 */
James Smartb522d7d2008-09-07 11:51:56 -04002829 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
2830 cmnd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
2831 goto out_fail_command;
2832 }
James Smart109f6ed2008-12-04 22:39:08 -05002833 if (vport->cfg_max_scsicmpl_time &&
2834 (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth))
James Smart977b5a02008-09-07 11:52:04 -04002835 goto out_host_busy;
Mike Christiea93ce022008-08-17 15:24:41 -05002836
James Smarted957682007-06-17 19:56:37 -05002837 lpfc_cmd = lpfc_get_scsi_buf(phba);
dea31012005-04-17 16:05:31 -05002838 if (lpfc_cmd == NULL) {
James Smarteaf15d52008-12-04 22:39:29 -05002839 lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002840
James Smarte8b62012007-08-02 11:10:09 -04002841 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
2842 "0707 driver's buffer pool is empty, "
2843 "IO busied\n");
dea31012005-04-17 16:05:31 -05002844 goto out_host_busy;
2845 }
2846
2847 /*
2848 * Store the midlayer's command structure for the completion phase
2849 * and complete the command initialization.
2850 */
2851 lpfc_cmd->pCmd = cmnd;
2852 lpfc_cmd->rdata = rdata;
2853 lpfc_cmd->timeout = 0;
James Smart977b5a02008-09-07 11:52:04 -04002854 lpfc_cmd->start_time = jiffies;
dea31012005-04-17 16:05:31 -05002855 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
2856 cmnd->scsi_done = done;
2857
James Smarte2a0a9d2008-12-04 22:40:02 -05002858 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
James Smart6a9c52c2009-10-02 15:16:51 -04002859 if (vport->phba->cfg_enable_bg) {
2860 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05002861 "9033 BLKGRD: rcvd protected cmd:%02x op:%02x "
2862 "str=%s\n",
2863 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
2864 dif_op_str[scsi_get_prot_op(cmnd)]);
James Smart6a9c52c2009-10-02 15:16:51 -04002865 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05002866 "9034 BLKGRD: CDB: %02x %02x %02x %02x %02x "
James Smarte4e74272009-07-19 10:01:38 -04002867 "%02x %02x %02x %02x %02x\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002868 cmnd->cmnd[0], cmnd->cmnd[1], cmnd->cmnd[2],
2869 cmnd->cmnd[3], cmnd->cmnd[4], cmnd->cmnd[5],
2870 cmnd->cmnd[6], cmnd->cmnd[7], cmnd->cmnd[8],
2871 cmnd->cmnd[9]);
James Smart6a9c52c2009-10-02 15:16:51 -04002872 if (cmnd->cmnd[0] == READ_10)
2873 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05002874 "9035 BLKGRD: READ @ sector %llu, "
Tejun Heo83096eb2009-05-07 22:24:39 +09002875 "count %u\n",
2876 (unsigned long long)scsi_get_lba(cmnd),
2877 blk_rq_sectors(cmnd->request));
James Smart6a9c52c2009-10-02 15:16:51 -04002878 else if (cmnd->cmnd[0] == WRITE_10)
2879 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05002880 "9036 BLKGRD: WRITE @ sector %llu, "
Tejun Heo83096eb2009-05-07 22:24:39 +09002881 "count %u cmd=%p\n",
James Smart87b5c322008-12-16 10:34:09 -05002882 (unsigned long long)scsi_get_lba(cmnd),
Tejun Heo83096eb2009-05-07 22:24:39 +09002883 blk_rq_sectors(cmnd->request),
James Smarte2a0a9d2008-12-04 22:40:02 -05002884 cmnd);
James Smart6a9c52c2009-10-02 15:16:51 -04002885 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002886
2887 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
2888 } else {
James Smart6a9c52c2009-10-02 15:16:51 -04002889 if (vport->phba->cfg_enable_bg) {
James Smarte2a0a9d2008-12-04 22:40:02 -05002890 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smart6a9c52c2009-10-02 15:16:51 -04002891 "9038 BLKGRD: rcvd unprotected cmd:"
2892 "%02x op:%02x str=%s\n",
2893 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
2894 dif_op_str[scsi_get_prot_op(cmnd)]);
2895 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
2896 "9039 BLKGRD: CDB: %02x %02x %02x "
2897 "%02x %02x %02x %02x %02x %02x %02x\n",
2898 cmnd->cmnd[0], cmnd->cmnd[1],
2899 cmnd->cmnd[2], cmnd->cmnd[3],
2900 cmnd->cmnd[4], cmnd->cmnd[5],
2901 cmnd->cmnd[6], cmnd->cmnd[7],
2902 cmnd->cmnd[8], cmnd->cmnd[9]);
2903 if (cmnd->cmnd[0] == READ_10)
2904 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
2905 "9040 dbg: READ @ sector %llu, "
2906 "count %u\n",
2907 (unsigned long long)scsi_get_lba(cmnd),
Tejun Heo83096eb2009-05-07 22:24:39 +09002908 blk_rq_sectors(cmnd->request));
James Smart6a9c52c2009-10-02 15:16:51 -04002909 else if (cmnd->cmnd[0] == WRITE_10)
2910 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05002911 "9041 dbg: WRITE @ sector %llu, "
Tejun Heo83096eb2009-05-07 22:24:39 +09002912 "count %u cmd=%p\n",
James Smart87b5c322008-12-16 10:34:09 -05002913 (unsigned long long)scsi_get_lba(cmnd),
Tejun Heo83096eb2009-05-07 22:24:39 +09002914 blk_rq_sectors(cmnd->request), cmnd);
James Smart6a9c52c2009-10-02 15:16:51 -04002915 else
2916 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05002917 "9042 dbg: parser not implemented\n");
James Smart6a9c52c2009-10-02 15:16:51 -04002918 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002919 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
2920 }
2921
dea31012005-04-17 16:05:31 -05002922 if (err)
2923 goto out_host_busy_free_buf;
2924
James Smart2e0fef82007-06-17 19:56:36 -05002925 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea31012005-04-17 16:05:31 -05002926
James Smart977b5a02008-09-07 11:52:04 -04002927 atomic_inc(&ndlp->cmd_pending);
James Smart3772a992009-05-22 14:50:54 -04002928 err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
James Smart92d7f7b2007-06-17 19:56:38 -05002929 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
James Smarteaf15d52008-12-04 22:39:29 -05002930 if (err) {
2931 atomic_dec(&ndlp->cmd_pending);
dea31012005-04-17 16:05:31 -05002932 goto out_host_busy_free_buf;
James Smarteaf15d52008-12-04 22:39:29 -05002933 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002934 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
2935 lpfc_sli_poll_fcp_ring(phba);
2936 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
2937 lpfc_poll_rearm_timer(phba);
2938 }
2939
dea31012005-04-17 16:05:31 -05002940 return 0;
2941
2942 out_host_busy_free_buf:
James Smartbcf4dbf2006-07-06 15:50:08 -04002943 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002944 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05002945 out_host_busy:
2946 return SCSI_MLQUEUE_HOST_BUSY;
2947
2948 out_fail_command:
2949 done(cmnd);
2950 return 0;
2951}
2952
James Smart9bad7672008-12-04 22:39:02 -05002953/**
James Smart3621a712009-04-06 18:47:14 -04002954 * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05002955 * @cmnd: Pointer to scsi_cmnd data structure.
2956 *
2957 * This routine aborts @cmnd pending in base driver.
2958 *
2959 * Return code :
2960 * 0x2003 - Error
2961 * 0x2002 - Success
2962 **/
dea31012005-04-17 16:05:31 -05002963static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05002964lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05002965{
James Smart2e0fef82007-06-17 19:56:36 -05002966 struct Scsi_Host *shost = cmnd->device->host;
2967 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2968 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002969 struct lpfc_iocbq *iocb;
2970 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05002971 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05002972 IOCB_t *cmd, *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002973 int ret = SUCCESS;
James Smartfa61a542008-01-11 01:52:42 -05002974 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002975
Christof Schmitt65d430f2009-10-30 17:59:29 +01002976 fc_block_scsi_eh(cmnd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002977 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
2978 BUG_ON(!lpfc_cmd);
dea31012005-04-17 16:05:31 -05002979
2980 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002981 * If pCmd field of the corresponding lpfc_scsi_buf structure
2982 * points to a different SCSI command, then the driver has
2983 * already completed this command, but the midlayer did not
2984 * see the completion before the eh fired. Just return
2985 * SUCCESS.
dea31012005-04-17 16:05:31 -05002986 */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002987 iocb = &lpfc_cmd->cur_iocbq;
2988 if (lpfc_cmd->pCmd != cmnd)
2989 goto out;
dea31012005-04-17 16:05:31 -05002990
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002991 BUG_ON(iocb->context1 != lpfc_cmd);
dea31012005-04-17 16:05:31 -05002992
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002993 abtsiocb = lpfc_sli_get_iocbq(phba);
2994 if (abtsiocb == NULL) {
2995 ret = FAILED;
dea31012005-04-17 16:05:31 -05002996 goto out;
2997 }
2998
dea31012005-04-17 16:05:31 -05002999 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003000 * The scsi command can not be in txq and it is in flight because the
3001 * pCmd is still pointig at the SCSI command we have to abort. There
3002 * is no need to search the txcmplq. Just send an abort to the FW.
dea31012005-04-17 16:05:31 -05003003 */
dea31012005-04-17 16:05:31 -05003004
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003005 cmd = &iocb->iocb;
3006 icmd = &abtsiocb->iocb;
3007 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
3008 icmd->un.acxri.abortContextTag = cmd->ulpContext;
James Smart3772a992009-05-22 14:50:54 -04003009 if (phba->sli_rev == LPFC_SLI_REV4)
3010 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
3011 else
3012 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05003013
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003014 icmd->ulpLe = 1;
3015 icmd->ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05003016 if (lpfc_is_link_up(phba))
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003017 icmd->ulpCommand = CMD_ABORT_XRI_CN;
3018 else
3019 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea31012005-04-17 16:05:31 -05003020
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003021 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smart2e0fef82007-06-17 19:56:36 -05003022 abtsiocb->vport = vport;
James Smart3772a992009-05-22 14:50:54 -04003023 if (lpfc_sli_issue_iocb(phba, LPFC_FCP_RING, abtsiocb, 0) ==
3024 IOCB_ERROR) {
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003025 lpfc_sli_release_iocbq(phba, abtsiocb);
3026 ret = FAILED;
3027 goto out;
3028 }
3029
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003030 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
3031 lpfc_sli_poll_fcp_ring (phba);
3032
James Smartfa61a542008-01-11 01:52:42 -05003033 lpfc_cmd->waitq = &waitq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003034 /* Wait for abort to complete */
James Smartfa61a542008-01-11 01:52:42 -05003035 wait_event_timeout(waitq,
3036 (lpfc_cmd->pCmd != cmnd),
3037 (2*vport->cfg_devloss_tmo*HZ));
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003038
James Smartfa61a542008-01-11 01:52:42 -05003039 spin_lock_irq(shost->host_lock);
3040 lpfc_cmd->waitq = NULL;
3041 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003042
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003043 if (lpfc_cmd->pCmd == cmnd) {
3044 ret = FAILED;
James Smarte8b62012007-08-02 11:10:09 -04003045 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3046 "0748 abort handler timed out waiting "
3047 "for abort to complete: ret %#x, ID %d, "
3048 "LUN %d, snum %#lx\n",
3049 ret, cmnd->device->id, cmnd->device->lun,
3050 cmnd->serial_number);
dea31012005-04-17 16:05:31 -05003051 }
3052
3053 out:
James Smarte8b62012007-08-02 11:10:09 -04003054 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3055 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
3056 "LUN %d snum %#lx\n", ret, cmnd->device->id,
3057 cmnd->device->lun, cmnd->serial_number);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003058 return ret;
dea31012005-04-17 16:05:31 -05003059}
3060
James Smartbbb9d182009-06-10 17:23:16 -04003061static char *
3062lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
3063{
3064 switch (task_mgmt_cmd) {
3065 case FCP_ABORT_TASK_SET:
3066 return "ABORT_TASK_SET";
3067 case FCP_CLEAR_TASK_SET:
3068 return "FCP_CLEAR_TASK_SET";
3069 case FCP_BUS_RESET:
3070 return "FCP_BUS_RESET";
3071 case FCP_LUN_RESET:
3072 return "FCP_LUN_RESET";
3073 case FCP_TARGET_RESET:
3074 return "FCP_TARGET_RESET";
3075 case FCP_CLEAR_ACA:
3076 return "FCP_CLEAR_ACA";
3077 case FCP_TERMINATE_TASK:
3078 return "FCP_TERMINATE_TASK";
3079 default:
3080 return "unknown";
3081 }
3082}
3083
3084/**
3085 * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
3086 * @vport: The virtual port for which this call is being executed.
3087 * @rdata: Pointer to remote port local data
3088 * @tgt_id: Target ID of remote device.
3089 * @lun_id: Lun number for the TMF
3090 * @task_mgmt_cmd: type of TMF to send
3091 *
3092 * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
3093 * a remote port.
3094 *
3095 * Return Code:
3096 * 0x2003 - Error
3097 * 0x2002 - Success.
3098 **/
3099static int
3100lpfc_send_taskmgmt(struct lpfc_vport *vport, struct lpfc_rport_data *rdata,
3101 unsigned tgt_id, unsigned int lun_id,
3102 uint8_t task_mgmt_cmd)
3103{
3104 struct lpfc_hba *phba = vport->phba;
3105 struct lpfc_scsi_buf *lpfc_cmd;
3106 struct lpfc_iocbq *iocbq;
3107 struct lpfc_iocbq *iocbqrsp;
3108 int ret;
3109 int status;
3110
3111 if (!rdata->pnode || !NLP_CHK_NODE_ACT(rdata->pnode))
3112 return FAILED;
3113
3114 lpfc_cmd = lpfc_get_scsi_buf(phba);
3115 if (lpfc_cmd == NULL)
3116 return FAILED;
3117 lpfc_cmd->timeout = 60;
3118 lpfc_cmd->rdata = rdata;
3119
3120 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
3121 task_mgmt_cmd);
3122 if (!status) {
3123 lpfc_release_scsi_buf(phba, lpfc_cmd);
3124 return FAILED;
3125 }
3126
3127 iocbq = &lpfc_cmd->cur_iocbq;
3128 iocbqrsp = lpfc_sli_get_iocbq(phba);
3129 if (iocbqrsp == NULL) {
3130 lpfc_release_scsi_buf(phba, lpfc_cmd);
3131 return FAILED;
3132 }
3133
3134 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
3135 "0702 Issue %s to TGT %d LUN %d "
3136 "rpi x%x nlp_flag x%x\n",
3137 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
3138 rdata->pnode->nlp_rpi, rdata->pnode->nlp_flag);
3139
3140 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
3141 iocbq, iocbqrsp, lpfc_cmd->timeout);
3142 if (status != IOCB_SUCCESS) {
3143 if (status == IOCB_TIMEDOUT) {
3144 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
3145 ret = TIMEOUT_ERROR;
3146 } else
3147 ret = FAILED;
3148 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
3149 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3150 "0727 TMF %s to TGT %d LUN %d failed (%d, %d)\n",
3151 lpfc_taskmgmt_name(task_mgmt_cmd),
3152 tgt_id, lun_id, iocbqrsp->iocb.ulpStatus,
3153 iocbqrsp->iocb.un.ulpWord[4]);
3154 } else
3155 ret = SUCCESS;
3156
3157 lpfc_sli_release_iocbq(phba, iocbqrsp);
3158
3159 if (ret != TIMEOUT_ERROR)
3160 lpfc_release_scsi_buf(phba, lpfc_cmd);
3161
3162 return ret;
3163}
3164
3165/**
3166 * lpfc_chk_tgt_mapped -
3167 * @vport: The virtual port to check on
3168 * @cmnd: Pointer to scsi_cmnd data structure.
3169 *
3170 * This routine delays until the scsi target (aka rport) for the
3171 * command exists (is present and logged in) or we declare it non-existent.
3172 *
3173 * Return code :
3174 * 0x2003 - Error
3175 * 0x2002 - Success
3176 **/
3177static int
3178lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
3179{
3180 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
3181 struct lpfc_nodelist *pnode = rdata->pnode;
3182 unsigned long later;
3183
3184 /*
3185 * If target is not in a MAPPED state, delay until
3186 * target is rediscovered or devloss timeout expires.
3187 */
3188 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
3189 while (time_after(later, jiffies)) {
3190 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3191 return FAILED;
3192 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
3193 return SUCCESS;
3194 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
3195 rdata = cmnd->device->hostdata;
3196 if (!rdata)
3197 return FAILED;
3198 pnode = rdata->pnode;
3199 }
3200 if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
3201 (pnode->nlp_state != NLP_STE_MAPPED_NODE))
3202 return FAILED;
3203 return SUCCESS;
3204}
3205
3206/**
3207 * lpfc_reset_flush_io_context -
3208 * @vport: The virtual port (scsi_host) for the flush context
3209 * @tgt_id: If aborting by Target contect - specifies the target id
3210 * @lun_id: If aborting by Lun context - specifies the lun id
3211 * @context: specifies the context level to flush at.
3212 *
3213 * After a reset condition via TMF, we need to flush orphaned i/o
3214 * contexts from the adapter. This routine aborts any contexts
3215 * outstanding, then waits for their completions. The wait is
3216 * bounded by devloss_tmo though.
3217 *
3218 * Return code :
3219 * 0x2003 - Error
3220 * 0x2002 - Success
3221 **/
3222static int
3223lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
3224 uint64_t lun_id, lpfc_ctx_cmd context)
3225{
3226 struct lpfc_hba *phba = vport->phba;
3227 unsigned long later;
3228 int cnt;
3229
3230 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
3231 if (cnt)
3232 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
3233 tgt_id, lun_id, context);
3234 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
3235 while (time_after(later, jiffies) && cnt) {
3236 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
3237 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
3238 }
3239 if (cnt) {
3240 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3241 "0724 I/O flush failure for context %s : cnt x%x\n",
3242 ((context == LPFC_CTX_LUN) ? "LUN" :
3243 ((context == LPFC_CTX_TGT) ? "TGT" :
3244 ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
3245 cnt);
3246 return FAILED;
3247 }
3248 return SUCCESS;
3249}
3250
James Smart9bad7672008-12-04 22:39:02 -05003251/**
James Smart3621a712009-04-06 18:47:14 -04003252 * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
James Smart9bad7672008-12-04 22:39:02 -05003253 * @cmnd: Pointer to scsi_cmnd data structure.
3254 *
James Smartbbb9d182009-06-10 17:23:16 -04003255 * This routine does a device reset by sending a LUN_RESET task management
James Smart9bad7672008-12-04 22:39:02 -05003256 * command.
3257 *
3258 * Return code :
3259 * 0x2003 - Error
James Smart3621a712009-04-06 18:47:14 -04003260 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05003261 **/
dea31012005-04-17 16:05:31 -05003262static int
James Smart7054a602007-04-25 09:52:34 -04003263lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05003264{
James Smart2e0fef82007-06-17 19:56:36 -05003265 struct Scsi_Host *shost = cmnd->device->host;
3266 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05003267 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
3268 struct lpfc_nodelist *pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04003269 unsigned tgt_id = cmnd->device->id;
3270 unsigned int lun_id = cmnd->device->lun;
James Smartea2151b2008-09-07 11:52:10 -04003271 struct lpfc_scsi_event_header scsi_event;
James Smartbbb9d182009-06-10 17:23:16 -04003272 int status;
dea31012005-04-17 16:05:31 -05003273
Christof Schmitt65d430f2009-10-30 17:59:29 +01003274 fc_block_scsi_eh(cmnd);
James Smartbbb9d182009-06-10 17:23:16 -04003275
3276 status = lpfc_chk_tgt_mapped(vport, cmnd);
3277 if (status == FAILED) {
3278 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3279 "0721 Device Reset rport failure: rdata x%p\n", rdata);
3280 return FAILED;
3281 }
3282
3283 scsi_event.event_type = FC_REG_SCSI_EVENT;
3284 scsi_event.subcategory = LPFC_EVENT_LUNRESET;
3285 scsi_event.lun = lun_id;
3286 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
3287 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
3288
3289 fc_host_post_vendor_event(shost, fc_get_event_number(),
3290 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
3291
3292 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
3293 FCP_LUN_RESET);
3294
3295 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3296 "0713 SCSI layer issued Device Reset (%d, %d) "
3297 "return x%x\n", tgt_id, lun_id, status);
3298
dea31012005-04-17 16:05:31 -05003299 /*
James Smartbbb9d182009-06-10 17:23:16 -04003300 * We have to clean up i/o as : they may be orphaned by the TMF;
3301 * or if the TMF failed, they may be in an indeterminate state.
3302 * So, continue on.
3303 * We will report success if all the i/o aborts successfully.
dea31012005-04-17 16:05:31 -05003304 */
James Smartbbb9d182009-06-10 17:23:16 -04003305 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
3306 LPFC_CTX_LUN);
3307 return status;
3308}
3309
3310/**
3311 * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
3312 * @cmnd: Pointer to scsi_cmnd data structure.
3313 *
3314 * This routine does a target reset by sending a TARGET_RESET task management
3315 * command.
3316 *
3317 * Return code :
3318 * 0x2003 - Error
3319 * 0x2002 - Success
3320 **/
3321static int
3322lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
3323{
3324 struct Scsi_Host *shost = cmnd->device->host;
3325 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3326 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
3327 struct lpfc_nodelist *pnode = rdata->pnode;
3328 unsigned tgt_id = cmnd->device->id;
3329 unsigned int lun_id = cmnd->device->lun;
3330 struct lpfc_scsi_event_header scsi_event;
3331 int status;
3332
Christof Schmitt65d430f2009-10-30 17:59:29 +01003333 fc_block_scsi_eh(cmnd);
James Smartbbb9d182009-06-10 17:23:16 -04003334
3335 status = lpfc_chk_tgt_mapped(vport, cmnd);
3336 if (status == FAILED) {
3337 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3338 "0722 Target Reset rport failure: rdata x%p\n", rdata);
3339 return FAILED;
dea31012005-04-17 16:05:31 -05003340 }
James Smartea2151b2008-09-07 11:52:10 -04003341
3342 scsi_event.event_type = FC_REG_SCSI_EVENT;
3343 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
3344 scsi_event.lun = 0;
3345 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
3346 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
3347
James Smartbbb9d182009-06-10 17:23:16 -04003348 fc_host_post_vendor_event(shost, fc_get_event_number(),
3349 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -04003350
James Smartbbb9d182009-06-10 17:23:16 -04003351 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
3352 FCP_TARGET_RESET);
dea31012005-04-17 16:05:31 -05003353
James Smarte8b62012007-08-02 11:10:09 -04003354 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smartbbb9d182009-06-10 17:23:16 -04003355 "0723 SCSI layer issued Target Reset (%d, %d) "
3356 "return x%x\n", tgt_id, lun_id, status);
3357
3358 /*
3359 * We have to clean up i/o as : they may be orphaned by the TMF;
3360 * or if the TMF failed, they may be in an indeterminate state.
3361 * So, continue on.
3362 * We will report success if all the i/o aborts successfully.
3363 */
3364 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
3365 LPFC_CTX_TGT);
3366 return status;
dea31012005-04-17 16:05:31 -05003367}
3368
James Smart9bad7672008-12-04 22:39:02 -05003369/**
James Smart3621a712009-04-06 18:47:14 -04003370 * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05003371 * @cmnd: Pointer to scsi_cmnd data structure.
3372 *
James Smartbbb9d182009-06-10 17:23:16 -04003373 * This routine does target reset to all targets on @cmnd->device->host.
3374 * This emulates Parallel SCSI Bus Reset Semantics.
James Smart9bad7672008-12-04 22:39:02 -05003375 *
James Smartbbb9d182009-06-10 17:23:16 -04003376 * Return code :
3377 * 0x2003 - Error
3378 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05003379 **/
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04003380static int
James Smart7054a602007-04-25 09:52:34 -04003381lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05003382{
James Smart2e0fef82007-06-17 19:56:36 -05003383 struct Scsi_Host *shost = cmnd->device->host;
3384 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05003385 struct lpfc_nodelist *ndlp = NULL;
James Smartea2151b2008-09-07 11:52:10 -04003386 struct lpfc_scsi_event_header scsi_event;
James Smartbbb9d182009-06-10 17:23:16 -04003387 int match;
3388 int ret = SUCCESS, status, i;
James Smartea2151b2008-09-07 11:52:10 -04003389
3390 scsi_event.event_type = FC_REG_SCSI_EVENT;
3391 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
3392 scsi_event.lun = 0;
3393 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
3394 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
3395
James Smartbbb9d182009-06-10 17:23:16 -04003396 fc_host_post_vendor_event(shost, fc_get_event_number(),
3397 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
dea31012005-04-17 16:05:31 -05003398
Christof Schmitt65d430f2009-10-30 17:59:29 +01003399 fc_block_scsi_eh(cmnd);
James Smartbbb9d182009-06-10 17:23:16 -04003400
dea31012005-04-17 16:05:31 -05003401 /*
3402 * Since the driver manages a single bus device, reset all
3403 * targets known to the driver. Should any target reset
3404 * fail, this routine returns failure to the midlayer.
3405 */
James Smarte17da182006-07-06 15:49:25 -04003406 for (i = 0; i < LPFC_MAX_TARGET; i++) {
James Smart685f0bf2007-04-25 09:53:08 -04003407 /* Search for mapped node by target ID */
dea31012005-04-17 16:05:31 -05003408 match = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003409 spin_lock_irq(shost->host_lock);
3410 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05003411 if (!NLP_CHK_NODE_ACT(ndlp))
3412 continue;
James Smart685f0bf2007-04-25 09:53:08 -04003413 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
James Smart915caaa2008-06-14 22:52:38 -04003414 ndlp->nlp_sid == i &&
James Smart685f0bf2007-04-25 09:53:08 -04003415 ndlp->rport) {
dea31012005-04-17 16:05:31 -05003416 match = 1;
3417 break;
3418 }
3419 }
James Smart2e0fef82007-06-17 19:56:36 -05003420 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003421 if (!match)
3422 continue;
James Smartbbb9d182009-06-10 17:23:16 -04003423
3424 status = lpfc_send_taskmgmt(vport, ndlp->rport->dd_data,
3425 i, 0, FCP_TARGET_RESET);
3426
3427 if (status != SUCCESS) {
James Smarte8b62012007-08-02 11:10:09 -04003428 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3429 "0700 Bus Reset on target %d failed\n",
3430 i);
James Smart915caaa2008-06-14 22:52:38 -04003431 ret = FAILED;
dea31012005-04-17 16:05:31 -05003432 }
3433 }
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05003434 /*
James Smartbbb9d182009-06-10 17:23:16 -04003435 * We have to clean up i/o as : they may be orphaned by the TMFs
3436 * above; or if any of the TMFs failed, they may be in an
3437 * indeterminate state.
3438 * We will report success if all the i/o aborts successfully.
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05003439 */
James Smartbbb9d182009-06-10 17:23:16 -04003440
3441 status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
3442 if (status != SUCCESS)
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05003443 ret = FAILED;
James Smartbbb9d182009-06-10 17:23:16 -04003444
James Smarte8b62012007-08-02 11:10:09 -04003445 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3446 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea31012005-04-17 16:05:31 -05003447 return ret;
3448}
3449
James Smart9bad7672008-12-04 22:39:02 -05003450/**
James Smart3621a712009-04-06 18:47:14 -04003451 * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
James Smart9bad7672008-12-04 22:39:02 -05003452 * @sdev: Pointer to scsi_device.
3453 *
3454 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
3455 * globally available list of scsi buffers. This routine also makes sure scsi
3456 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
3457 * of scsi buffer exists for the lifetime of the driver.
3458 *
3459 * Return codes:
3460 * non-0 - Error
3461 * 0 - Success
3462 **/
dea31012005-04-17 16:05:31 -05003463static int
dea31012005-04-17 16:05:31 -05003464lpfc_slave_alloc(struct scsi_device *sdev)
3465{
James Smart2e0fef82007-06-17 19:56:36 -05003466 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
3467 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003468 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
James Smart3772a992009-05-22 14:50:54 -04003469 uint32_t total = 0;
dea31012005-04-17 16:05:31 -05003470 uint32_t num_to_alloc = 0;
James Smart3772a992009-05-22 14:50:54 -04003471 int num_allocated = 0;
dea31012005-04-17 16:05:31 -05003472
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003473 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05003474 return -ENXIO;
3475
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003476 sdev->hostdata = rport->dd_data;
dea31012005-04-17 16:05:31 -05003477
3478 /*
3479 * Populate the cmds_per_lun count scsi_bufs into this host's globally
3480 * available list of scsi buffers. Don't allocate more than the
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04003481 * HBA limit conveyed to the midlayer via the host structure. The
3482 * formula accounts for the lun_queue_depth + error handlers + 1
3483 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea31012005-04-17 16:05:31 -05003484 */
3485 total = phba->total_scsi_bufs;
James Smart3de2a652007-08-02 11:09:59 -04003486 num_to_alloc = vport->cfg_lun_queue_depth + 2;
James Smart92d7f7b2007-06-17 19:56:38 -05003487
3488 /* Allow some exchanges to be available always to complete discovery */
3489 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04003490 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3491 "0704 At limitation of %d preallocated "
3492 "command buffers\n", total);
dea31012005-04-17 16:05:31 -05003493 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05003494 /* Allow some exchanges to be available always to complete discovery */
3495 } else if (total + num_to_alloc >
3496 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04003497 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3498 "0705 Allocation request of %d "
3499 "command buffers will exceed max of %d. "
3500 "Reducing allocation request to %d.\n",
3501 num_to_alloc, phba->cfg_hba_queue_depth,
3502 (phba->cfg_hba_queue_depth - total));
dea31012005-04-17 16:05:31 -05003503 num_to_alloc = phba->cfg_hba_queue_depth - total;
3504 }
James Smart3772a992009-05-22 14:50:54 -04003505 num_allocated = lpfc_new_scsi_buf(vport, num_to_alloc);
3506 if (num_to_alloc != num_allocated) {
3507 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3508 "0708 Allocation request of %d "
3509 "command buffers did not succeed. "
3510 "Allocated %d buffers.\n",
3511 num_to_alloc, num_allocated);
dea31012005-04-17 16:05:31 -05003512 }
3513 return 0;
3514}
3515
James Smart9bad7672008-12-04 22:39:02 -05003516/**
James Smart3621a712009-04-06 18:47:14 -04003517 * lpfc_slave_configure - scsi_host_template slave_configure entry point
James Smart9bad7672008-12-04 22:39:02 -05003518 * @sdev: Pointer to scsi_device.
3519 *
3520 * This routine configures following items
3521 * - Tag command queuing support for @sdev if supported.
3522 * - Dev loss time out value of fc_rport.
3523 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
3524 *
3525 * Return codes:
3526 * 0 - Success
3527 **/
dea31012005-04-17 16:05:31 -05003528static int
3529lpfc_slave_configure(struct scsi_device *sdev)
3530{
James Smart2e0fef82007-06-17 19:56:36 -05003531 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
3532 struct lpfc_hba *phba = vport->phba;
3533 struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
dea31012005-04-17 16:05:31 -05003534
3535 if (sdev->tagged_supported)
James Smart3de2a652007-08-02 11:09:59 -04003536 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05003537 else
James Smart3de2a652007-08-02 11:09:59 -04003538 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05003539
3540 /*
3541 * Initialize the fc transport attributes for the target
3542 * containing this scsi device. Also note that the driver's
3543 * target pointer is stored in the starget_data for the
3544 * driver's sysfs entry point functions.
3545 */
James Smart3de2a652007-08-02 11:09:59 -04003546 rport->dev_loss_tmo = vport->cfg_devloss_tmo;
dea31012005-04-17 16:05:31 -05003547
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003548 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
3549 lpfc_sli_poll_fcp_ring(phba);
3550 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
3551 lpfc_poll_rearm_timer(phba);
3552 }
3553
dea31012005-04-17 16:05:31 -05003554 return 0;
3555}
3556
James Smart9bad7672008-12-04 22:39:02 -05003557/**
James Smart3621a712009-04-06 18:47:14 -04003558 * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
James Smart9bad7672008-12-04 22:39:02 -05003559 * @sdev: Pointer to scsi_device.
3560 *
3561 * This routine sets @sdev hostatdata filed to null.
3562 **/
dea31012005-04-17 16:05:31 -05003563static void
3564lpfc_slave_destroy(struct scsi_device *sdev)
3565{
3566 sdev->hostdata = NULL;
3567 return;
3568}
3569
James Smart92d7f7b2007-06-17 19:56:38 -05003570
dea31012005-04-17 16:05:31 -05003571struct scsi_host_template lpfc_template = {
3572 .module = THIS_MODULE,
3573 .name = LPFC_DRIVER_NAME,
3574 .info = lpfc_info,
3575 .queuecommand = lpfc_queuecommand,
3576 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04003577 .eh_device_reset_handler = lpfc_device_reset_handler,
3578 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart7054a602007-04-25 09:52:34 -04003579 .eh_bus_reset_handler = lpfc_bus_reset_handler,
dea31012005-04-17 16:05:31 -05003580 .slave_alloc = lpfc_slave_alloc,
3581 .slave_configure = lpfc_slave_configure,
3582 .slave_destroy = lpfc_slave_destroy,
James Smart47a86172007-04-25 09:53:22 -04003583 .scan_finished = lpfc_scan_finished,
dea31012005-04-17 16:05:31 -05003584 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05003585 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
dea31012005-04-17 16:05:31 -05003586 .cmd_per_lun = LPFC_CMD_PER_LUN,
3587 .use_clustering = ENABLE_CLUSTERING,
James Smart2e0fef82007-06-17 19:56:36 -05003588 .shost_attrs = lpfc_hba_attrs,
James.Smart@Emulex.Com564b2962005-06-25 10:34:17 -04003589 .max_sectors = 0xFFFF,
James Smartf1c3b0f2009-07-19 10:01:32 -04003590 .vendor_id = LPFC_NL_VENDOR_ID,
dea31012005-04-17 16:05:31 -05003591};
James Smart3de2a652007-08-02 11:09:59 -04003592
3593struct scsi_host_template lpfc_vport_template = {
3594 .module = THIS_MODULE,
3595 .name = LPFC_DRIVER_NAME,
3596 .info = lpfc_info,
3597 .queuecommand = lpfc_queuecommand,
3598 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04003599 .eh_device_reset_handler = lpfc_device_reset_handler,
3600 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart3de2a652007-08-02 11:09:59 -04003601 .eh_bus_reset_handler = lpfc_bus_reset_handler,
3602 .slave_alloc = lpfc_slave_alloc,
3603 .slave_configure = lpfc_slave_configure,
3604 .slave_destroy = lpfc_slave_destroy,
3605 .scan_finished = lpfc_scan_finished,
3606 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05003607 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
James Smart3de2a652007-08-02 11:09:59 -04003608 .cmd_per_lun = LPFC_CMD_PER_LUN,
3609 .use_clustering = ENABLE_CLUSTERING,
3610 .shost_attrs = lpfc_vport_attrs,
3611 .max_sectors = 0xFFFF,
3612};