blob: a4881f26ab1bc0cbedcc5da0d439f94069dded43 [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 Smart1c6f4ef52009-11-18 15:40:49 -050062static void
63lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
James Smarte2a0a9d2008-12-04 22:40:02 -050064
65static void
James Smart6a9c52c2009-10-02 15:16:51 -040066lpfc_debug_save_data(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
James Smarte2a0a9d2008-12-04 22:40:02 -050067{
68 void *src, *dst;
69 struct scatterlist *sgde = scsi_sglist(cmnd);
70
71 if (!_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -040072 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
73 "9050 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
James Smarte2a0a9d2008-12-04 22:40:02 -050074 __func__);
75 return;
76 }
77
78
79 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -040080 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
81 "9051 BLKGRD: ERROR: data scatterlist is null\n");
James Smarte2a0a9d2008-12-04 22:40:02 -050082 return;
83 }
84
85 dst = (void *) _dump_buf_data;
86 while (sgde) {
87 src = sg_virt(sgde);
88 memcpy(dst, src, sgde->length);
89 dst += sgde->length;
90 sgde = sg_next(sgde);
91 }
92}
93
94static void
James Smart6a9c52c2009-10-02 15:16:51 -040095lpfc_debug_save_dif(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
James Smarte2a0a9d2008-12-04 22:40:02 -050096{
97 void *src, *dst;
98 struct scatterlist *sgde = scsi_prot_sglist(cmnd);
99
100 if (!_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -0400101 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
102 "9052 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
James Smarte2a0a9d2008-12-04 22:40:02 -0500103 __func__);
104 return;
105 }
106
107 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -0400108 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
109 "9053 BLKGRD: ERROR: prot scatterlist is null\n");
James Smarte2a0a9d2008-12-04 22:40:02 -0500110 return;
111 }
112
113 dst = _dump_buf_dif;
114 while (sgde) {
115 src = sg_virt(sgde);
116 memcpy(dst, src, sgde->length);
117 dst += sgde->length;
118 sgde = sg_next(sgde);
119 }
120}
121
James Smartea2151b2008-09-07 11:52:10 -0400122/**
James Smartf1126682009-06-10 17:22:44 -0400123 * lpfc_sli4_set_rsp_sgl_last - Set the last bit in the response sge.
124 * @phba: Pointer to HBA object.
125 * @lpfc_cmd: lpfc scsi command object pointer.
126 *
127 * This function is called from the lpfc_prep_task_mgmt_cmd function to
128 * set the last bit in the response sge entry.
129 **/
130static void
131lpfc_sli4_set_rsp_sgl_last(struct lpfc_hba *phba,
132 struct lpfc_scsi_buf *lpfc_cmd)
133{
134 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
135 if (sgl) {
136 sgl += 1;
137 sgl->word2 = le32_to_cpu(sgl->word2);
138 bf_set(lpfc_sli4_sge_last, sgl, 1);
139 sgl->word2 = cpu_to_le32(sgl->word2);
140 }
141}
142
143/**
James Smart3621a712009-04-06 18:47:14 -0400144 * lpfc_update_stats - Update statistical data for the command completion
James Smartea2151b2008-09-07 11:52:10 -0400145 * @phba: Pointer to HBA object.
146 * @lpfc_cmd: lpfc scsi command object pointer.
147 *
148 * This function is called when there is a command completion and this
149 * function updates the statistical data for the command completion.
150 **/
151static void
152lpfc_update_stats(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
153{
154 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
155 struct lpfc_nodelist *pnode = rdata->pnode;
156 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
157 unsigned long flags;
158 struct Scsi_Host *shost = cmd->device->host;
159 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
160 unsigned long latency;
161 int i;
162
163 if (cmd->result)
164 return;
165
James Smart9f1e1b52008-12-04 22:39:40 -0500166 latency = jiffies_to_msecs((long)jiffies - (long)lpfc_cmd->start_time);
167
James Smartea2151b2008-09-07 11:52:10 -0400168 spin_lock_irqsave(shost->host_lock, flags);
169 if (!vport->stat_data_enabled ||
170 vport->stat_data_blocked ||
171 !pnode->lat_data ||
172 (phba->bucket_type == LPFC_NO_BUCKET)) {
173 spin_unlock_irqrestore(shost->host_lock, flags);
174 return;
175 }
James Smartea2151b2008-09-07 11:52:10 -0400176
177 if (phba->bucket_type == LPFC_LINEAR_BUCKET) {
178 i = (latency + phba->bucket_step - 1 - phba->bucket_base)/
179 phba->bucket_step;
James Smart9f1e1b52008-12-04 22:39:40 -0500180 /* check array subscript bounds */
181 if (i < 0)
182 i = 0;
183 else if (i >= LPFC_MAX_BUCKET_COUNT)
184 i = LPFC_MAX_BUCKET_COUNT - 1;
James Smartea2151b2008-09-07 11:52:10 -0400185 } else {
186 for (i = 0; i < LPFC_MAX_BUCKET_COUNT-1; i++)
187 if (latency <= (phba->bucket_base +
188 ((1<<i)*phba->bucket_step)))
189 break;
190 }
191
192 pnode->lat_data[i].cmd_count++;
193 spin_unlock_irqrestore(shost->host_lock, flags);
194}
195
James Smartea2151b2008-09-07 11:52:10 -0400196/**
James Smart3621a712009-04-06 18:47:14 -0400197 * lpfc_send_sdev_queuedepth_change_event - Posts a queuedepth change event
James Smartea2151b2008-09-07 11:52:10 -0400198 * @phba: Pointer to HBA context object.
199 * @vport: Pointer to vport object.
200 * @ndlp: Pointer to FC node associated with the target.
201 * @lun: Lun number of the scsi device.
202 * @old_val: Old value of the queue depth.
203 * @new_val: New value of the queue depth.
204 *
205 * This function sends an event to the mgmt application indicating
206 * there is a change in the scsi device queue depth.
207 **/
208static void
209lpfc_send_sdev_queuedepth_change_event(struct lpfc_hba *phba,
210 struct lpfc_vport *vport,
211 struct lpfc_nodelist *ndlp,
212 uint32_t lun,
213 uint32_t old_val,
214 uint32_t new_val)
215{
216 struct lpfc_fast_path_event *fast_path_evt;
217 unsigned long flags;
218
219 fast_path_evt = lpfc_alloc_fast_evt(phba);
220 if (!fast_path_evt)
221 return;
222
223 fast_path_evt->un.queue_depth_evt.scsi_event.event_type =
224 FC_REG_SCSI_EVENT;
225 fast_path_evt->un.queue_depth_evt.scsi_event.subcategory =
226 LPFC_EVENT_VARQUEDEPTH;
227
228 /* Report all luns with change in queue depth */
229 fast_path_evt->un.queue_depth_evt.scsi_event.lun = lun;
230 if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
231 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwpn,
232 &ndlp->nlp_portname, sizeof(struct lpfc_name));
233 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwnn,
234 &ndlp->nlp_nodename, sizeof(struct lpfc_name));
235 }
236
237 fast_path_evt->un.queue_depth_evt.oldval = old_val;
238 fast_path_evt->un.queue_depth_evt.newval = new_val;
239 fast_path_evt->vport = vport;
240
241 fast_path_evt->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
242 spin_lock_irqsave(&phba->hbalock, flags);
243 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
244 spin_unlock_irqrestore(&phba->hbalock, flags);
245 lpfc_worker_wake_up(phba);
246
247 return;
248}
249
James Smart9bad7672008-12-04 22:39:02 -0500250/**
James Smart5ffc2662009-11-18 15:39:44 -0500251 * lpfc_change_queue_depth - Alter scsi device queue depth
252 * @sdev: Pointer the scsi device on which to change the queue depth.
253 * @qdepth: New queue depth to set the sdev to.
254 * @reason: The reason for the queue depth change.
255 *
256 * This function is called by the midlayer and the LLD to alter the queue
257 * depth for a scsi device. This function sets the queue depth to the new
258 * value and sends an event out to log the queue depth change.
259 **/
260int
261lpfc_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
262{
263 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
264 struct lpfc_hba *phba = vport->phba;
265 struct lpfc_rport_data *rdata;
266 unsigned long new_queue_depth, old_queue_depth;
267
268 old_queue_depth = sdev->queue_depth;
269 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
270 new_queue_depth = sdev->queue_depth;
271 rdata = sdev->hostdata;
272 if (rdata)
273 lpfc_send_sdev_queuedepth_change_event(phba, vport,
274 rdata->pnode, sdev->lun,
275 old_queue_depth,
276 new_queue_depth);
277 return sdev->queue_depth;
278}
279
280/**
James Smart3621a712009-04-06 18:47:14 -0400281 * lpfc_rampdown_queue_depth - Post RAMP_DOWN_QUEUE event to worker thread
James Smart9bad7672008-12-04 22:39:02 -0500282 * @phba: The Hba for which this call is being executed.
283 *
284 * This routine is called when there is resource error in driver or firmware.
285 * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
286 * posts at most 1 event each second. This routine wakes up worker thread of
287 * @phba to process WORKER_RAM_DOWN_EVENT event.
288 *
289 * This routine should be called with no lock held.
290 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500291void
James Smarteaf15d52008-12-04 22:39:29 -0500292lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
James Smart92d7f7b2007-06-17 19:56:38 -0500293{
294 unsigned long flags;
James Smart5e9d9b82008-06-14 22:52:53 -0400295 uint32_t evt_posted;
James Smart92d7f7b2007-06-17 19:56:38 -0500296
297 spin_lock_irqsave(&phba->hbalock, flags);
298 atomic_inc(&phba->num_rsrc_err);
299 phba->last_rsrc_error_time = jiffies;
300
301 if ((phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL) > jiffies) {
302 spin_unlock_irqrestore(&phba->hbalock, flags);
303 return;
304 }
305
306 phba->last_ramp_down_time = jiffies;
307
308 spin_unlock_irqrestore(&phba->hbalock, flags);
309
310 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400311 evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
312 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500313 phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500314 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
315
James Smart5e9d9b82008-06-14 22:52:53 -0400316 if (!evt_posted)
317 lpfc_worker_wake_up(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500318 return;
319}
320
James Smart9bad7672008-12-04 22:39:02 -0500321/**
James Smart3621a712009-04-06 18:47:14 -0400322 * lpfc_rampup_queue_depth - Post RAMP_UP_QUEUE event for worker thread
James Smart9bad7672008-12-04 22:39:02 -0500323 * @phba: The Hba for which this call is being executed.
324 *
325 * This routine post WORKER_RAMP_UP_QUEUE event for @phba vport. This routine
326 * post at most 1 event every 5 minute after last_ramp_up_time or
327 * last_rsrc_error_time. This routine wakes up worker thread of @phba
328 * to process WORKER_RAM_DOWN_EVENT event.
329 *
330 * This routine should be called with no lock held.
331 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500332static inline void
James Smart3de2a652007-08-02 11:09:59 -0400333lpfc_rampup_queue_depth(struct lpfc_vport *vport,
James Smarta257bf92009-04-06 18:48:10 -0400334 uint32_t queue_depth)
James Smart92d7f7b2007-06-17 19:56:38 -0500335{
336 unsigned long flags;
James Smart3de2a652007-08-02 11:09:59 -0400337 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -0400338 uint32_t evt_posted;
James Smart92d7f7b2007-06-17 19:56:38 -0500339 atomic_inc(&phba->num_cmd_success);
340
James Smarta257bf92009-04-06 18:48:10 -0400341 if (vport->cfg_lun_queue_depth <= queue_depth)
James Smart92d7f7b2007-06-17 19:56:38 -0500342 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500343 spin_lock_irqsave(&phba->hbalock, flags);
James Smart5ffc2662009-11-18 15:39:44 -0500344 if (time_before(jiffies,
345 phba->last_ramp_up_time + QUEUE_RAMP_UP_INTERVAL) ||
346 time_before(jiffies,
347 phba->last_rsrc_error_time + QUEUE_RAMP_UP_INTERVAL)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500348 spin_unlock_irqrestore(&phba->hbalock, flags);
349 return;
350 }
James Smart92d7f7b2007-06-17 19:56:38 -0500351 phba->last_ramp_up_time = jiffies;
352 spin_unlock_irqrestore(&phba->hbalock, flags);
353
354 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400355 evt_posted = phba->pport->work_port_events & WORKER_RAMP_UP_QUEUE;
356 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500357 phba->pport->work_port_events |= WORKER_RAMP_UP_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500358 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
359
James Smart5e9d9b82008-06-14 22:52:53 -0400360 if (!evt_posted)
361 lpfc_worker_wake_up(phba);
362 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500363}
364
James Smart9bad7672008-12-04 22:39:02 -0500365/**
James Smart3621a712009-04-06 18:47:14 -0400366 * lpfc_ramp_down_queue_handler - WORKER_RAMP_DOWN_QUEUE event handler
James Smart9bad7672008-12-04 22:39:02 -0500367 * @phba: The Hba for which this call is being executed.
368 *
369 * This routine is called to process WORKER_RAMP_DOWN_QUEUE event for worker
370 * thread.This routine reduces queue depth for all scsi device on each vport
371 * associated with @phba.
372 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500373void
374lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
375{
James Smart549e55c2007-08-02 11:09:51 -0400376 struct lpfc_vport **vports;
377 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500378 struct scsi_device *sdev;
James Smart5ffc2662009-11-18 15:39:44 -0500379 unsigned long new_queue_depth;
James Smart92d7f7b2007-06-17 19:56:38 -0500380 unsigned long num_rsrc_err, num_cmd_success;
James Smart549e55c2007-08-02 11:09:51 -0400381 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500382
383 num_rsrc_err = atomic_read(&phba->num_rsrc_err);
384 num_cmd_success = atomic_read(&phba->num_cmd_success);
385
James Smart549e55c2007-08-02 11:09:51 -0400386 vports = lpfc_create_vport_work_array(phba);
387 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400388 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400389 shost = lpfc_shost_from_vport(vports[i]);
390 shost_for_each_device(sdev, shost) {
James Smart92d7f7b2007-06-17 19:56:38 -0500391 new_queue_depth =
James Smart549e55c2007-08-02 11:09:51 -0400392 sdev->queue_depth * num_rsrc_err /
393 (num_rsrc_err + num_cmd_success);
394 if (!new_queue_depth)
395 new_queue_depth = sdev->queue_depth - 1;
396 else
397 new_queue_depth = sdev->queue_depth -
398 new_queue_depth;
James Smart5ffc2662009-11-18 15:39:44 -0500399 lpfc_change_queue_depth(sdev, new_queue_depth,
400 SCSI_QDEPTH_DEFAULT);
James Smart549e55c2007-08-02 11:09:51 -0400401 }
James Smart92d7f7b2007-06-17 19:56:38 -0500402 }
James Smart09372822008-01-11 01:52:54 -0500403 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500404 atomic_set(&phba->num_rsrc_err, 0);
405 atomic_set(&phba->num_cmd_success, 0);
406}
407
James Smart9bad7672008-12-04 22:39:02 -0500408/**
James Smart3621a712009-04-06 18:47:14 -0400409 * lpfc_ramp_up_queue_handler - WORKER_RAMP_UP_QUEUE event handler
James Smart9bad7672008-12-04 22:39:02 -0500410 * @phba: The Hba for which this call is being executed.
411 *
412 * This routine is called to process WORKER_RAMP_UP_QUEUE event for worker
413 * thread.This routine increases queue depth for all scsi device on each vport
414 * associated with @phba by 1. This routine also sets @phba num_rsrc_err and
415 * num_cmd_success to zero.
416 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500417void
418lpfc_ramp_up_queue_handler(struct lpfc_hba *phba)
419{
James Smart549e55c2007-08-02 11:09:51 -0400420 struct lpfc_vport **vports;
421 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500422 struct scsi_device *sdev;
James Smart549e55c2007-08-02 11:09:51 -0400423 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500424
James Smart549e55c2007-08-02 11:09:51 -0400425 vports = lpfc_create_vport_work_array(phba);
426 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400427 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400428 shost = lpfc_shost_from_vport(vports[i]);
429 shost_for_each_device(sdev, shost) {
James Smart97eab632008-04-07 10:16:05 -0400430 if (vports[i]->cfg_lun_queue_depth <=
431 sdev->queue_depth)
432 continue;
James Smart5ffc2662009-11-18 15:39:44 -0500433 lpfc_change_queue_depth(sdev,
434 sdev->queue_depth+1,
435 SCSI_QDEPTH_RAMP_UP);
James Smart549e55c2007-08-02 11:09:51 -0400436 }
James Smart92d7f7b2007-06-17 19:56:38 -0500437 }
James Smart09372822008-01-11 01:52:54 -0500438 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500439 atomic_set(&phba->num_rsrc_err, 0);
440 atomic_set(&phba->num_cmd_success, 0);
441}
442
James Smarta8e497d2008-08-24 21:50:11 -0400443/**
James Smart3621a712009-04-06 18:47:14 -0400444 * lpfc_scsi_dev_block - set all scsi hosts to block state
James Smarta8e497d2008-08-24 21:50:11 -0400445 * @phba: Pointer to HBA context object.
446 *
447 * This function walks vport list and set each SCSI host to block state
448 * by invoking fc_remote_port_delete() routine. This function is invoked
449 * with EEH when device's PCI slot has been permanently disabled.
450 **/
451void
452lpfc_scsi_dev_block(struct lpfc_hba *phba)
453{
454 struct lpfc_vport **vports;
455 struct Scsi_Host *shost;
456 struct scsi_device *sdev;
457 struct fc_rport *rport;
458 int i;
459
460 vports = lpfc_create_vport_work_array(phba);
461 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400462 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8e497d2008-08-24 21:50:11 -0400463 shost = lpfc_shost_from_vport(vports[i]);
464 shost_for_each_device(sdev, shost) {
465 rport = starget_to_rport(scsi_target(sdev));
466 fc_remote_port_delete(rport);
467 }
468 }
469 lpfc_destroy_vport_work_array(phba, vports);
470}
471
James Smart9bad7672008-12-04 22:39:02 -0500472/**
James Smart3772a992009-05-22 14:50:54 -0400473 * lpfc_new_scsi_buf_s3 - Scsi buffer allocator for HBA with SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -0500474 * @vport: The virtual port for which this call being executed.
James Smart3772a992009-05-22 14:50:54 -0400475 * @num_to_allocate: The requested number of buffers to allocate.
James Smart9bad7672008-12-04 22:39:02 -0500476 *
James Smart3772a992009-05-22 14:50:54 -0400477 * This routine allocates a scsi buffer for device with SLI-3 interface spec,
478 * the scsi buffer contains all the necessary information needed to initiate
479 * a SCSI I/O. The non-DMAable buffer region contains information to build
480 * the IOCB. The DMAable region contains memory for the FCP CMND, FCP RSP,
481 * and the initial BPL. In addition to allocating memory, the FCP CMND and
482 * FCP RSP BDEs are setup in the BPL and the BPL BDE is setup in the IOCB.
James Smart9bad7672008-12-04 22:39:02 -0500483 *
484 * Return codes:
James Smart3772a992009-05-22 14:50:54 -0400485 * int - number of scsi buffers that were allocated.
486 * 0 = failure, less than num_to_alloc is a partial failure.
James Smart9bad7672008-12-04 22:39:02 -0500487 **/
James Smart3772a992009-05-22 14:50:54 -0400488static int
489lpfc_new_scsi_buf_s3(struct lpfc_vport *vport, int num_to_alloc)
dea31012005-04-17 16:05:31 -0500490{
James Smart2e0fef82007-06-17 19:56:36 -0500491 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500492 struct lpfc_scsi_buf *psb;
493 struct ulp_bde64 *bpl;
494 IOCB_t *iocb;
James Smart34b02dc2008-08-24 21:49:55 -0400495 dma_addr_t pdma_phys_fcp_cmd;
496 dma_addr_t pdma_phys_fcp_rsp;
497 dma_addr_t pdma_phys_bpl;
James Bottomley604a3e32005-10-29 10:28:33 -0500498 uint16_t iotag;
James Smart3772a992009-05-22 14:50:54 -0400499 int bcnt;
dea31012005-04-17 16:05:31 -0500500
James Smart3772a992009-05-22 14:50:54 -0400501 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
502 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
503 if (!psb)
504 break;
dea31012005-04-17 16:05:31 -0500505
James Smart3772a992009-05-22 14:50:54 -0400506 /*
507 * Get memory from the pci pool to map the virt space to pci
508 * bus space for an I/O. The DMA buffer includes space for the
509 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
510 * necessary to support the sg_tablesize.
511 */
512 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
513 GFP_KERNEL, &psb->dma_handle);
514 if (!psb->data) {
515 kfree(psb);
516 break;
517 }
dea31012005-04-17 16:05:31 -0500518
James Smart3772a992009-05-22 14:50:54 -0400519 /* Initialize virtual ptrs to dma_buf region. */
520 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
dea31012005-04-17 16:05:31 -0500521
James Smart3772a992009-05-22 14:50:54 -0400522 /* Allocate iotag for psb->cur_iocbq. */
523 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
524 if (iotag == 0) {
525 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
526 psb->data, psb->dma_handle);
527 kfree(psb);
528 break;
529 }
530 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Bottomley604a3e32005-10-29 10:28:33 -0500531
James Smart3772a992009-05-22 14:50:54 -0400532 psb->fcp_cmnd = psb->data;
533 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
534 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
James Smart34b02dc2008-08-24 21:49:55 -0400535 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500536
James Smart3772a992009-05-22 14:50:54 -0400537 /* Initialize local short-hand pointers. */
538 bpl = psb->fcp_bpl;
539 pdma_phys_fcp_cmd = psb->dma_handle;
540 pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
541 pdma_phys_bpl = psb->dma_handle + sizeof(struct fcp_cmnd) +
542 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500543
James Smart3772a992009-05-22 14:50:54 -0400544 /*
545 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
546 * are sg list bdes. Initialize the first two and leave the
547 * rest for queuecommand.
548 */
549 bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
550 bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
551 bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
552 bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
553 bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
dea31012005-04-17 16:05:31 -0500554
James Smart3772a992009-05-22 14:50:54 -0400555 /* Setup the physical region for the FCP RSP */
556 bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
557 bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
558 bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
559 bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
560 bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
561
562 /*
563 * Since the IOCB for the FCP I/O is built into this
564 * lpfc_scsi_buf, initialize it with all known data now.
565 */
566 iocb = &psb->cur_iocbq.iocb;
567 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
568 if ((phba->sli_rev == 3) &&
569 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
570 /* fill in immediate fcp command BDE */
571 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
572 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
573 iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
574 unsli3.fcp_ext.icd);
575 iocb->un.fcpi64.bdl.addrHigh = 0;
576 iocb->ulpBdeCount = 0;
577 iocb->ulpLe = 0;
578 /* fill in responce BDE */
579 iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags =
580 BUFF_TYPE_BDE_64;
581 iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
582 sizeof(struct fcp_rsp);
583 iocb->unsli3.fcp_ext.rbde.addrLow =
584 putPaddrLow(pdma_phys_fcp_rsp);
585 iocb->unsli3.fcp_ext.rbde.addrHigh =
586 putPaddrHigh(pdma_phys_fcp_rsp);
587 } else {
588 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
589 iocb->un.fcpi64.bdl.bdeSize =
590 (2 * sizeof(struct ulp_bde64));
591 iocb->un.fcpi64.bdl.addrLow =
592 putPaddrLow(pdma_phys_bpl);
593 iocb->un.fcpi64.bdl.addrHigh =
594 putPaddrHigh(pdma_phys_bpl);
595 iocb->ulpBdeCount = 1;
596 iocb->ulpLe = 1;
597 }
598 iocb->ulpClass = CLASS3;
599 psb->status = IOSTAT_SUCCESS;
James Smartda0436e2009-05-22 14:51:39 -0400600 /* Put it back into the SCSI buffer list */
James Smart1c6f4ef52009-11-18 15:40:49 -0500601 lpfc_release_scsi_buf_s3(phba, psb);
James Smart3772a992009-05-22 14:50:54 -0400602
James Smart34b02dc2008-08-24 21:49:55 -0400603 }
dea31012005-04-17 16:05:31 -0500604
James Smart3772a992009-05-22 14:50:54 -0400605 return bcnt;
dea31012005-04-17 16:05:31 -0500606}
607
James Smart9bad7672008-12-04 22:39:02 -0500608/**
James Smartda0436e2009-05-22 14:51:39 -0400609 * lpfc_sli4_fcp_xri_aborted - Fast-path process of fcp xri abort
610 * @phba: pointer to lpfc hba data structure.
611 * @axri: pointer to the fcp xri abort wcqe structure.
612 *
613 * This routine is invoked by the worker thread to process a SLI4 fast-path
614 * FCP aborted xri.
615 **/
616void
617lpfc_sli4_fcp_xri_aborted(struct lpfc_hba *phba,
618 struct sli4_wcqe_xri_aborted *axri)
619{
620 uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
621 struct lpfc_scsi_buf *psb, *next_psb;
622 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -0500623 struct lpfc_iocbq *iocbq;
624 int i;
James Smartda0436e2009-05-22 14:51:39 -0400625
James Smart0f65ff62010-02-26 14:14:23 -0500626 spin_lock_irqsave(&phba->hbalock, iflag);
627 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
James Smartda0436e2009-05-22 14:51:39 -0400628 list_for_each_entry_safe(psb, next_psb,
629 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
630 if (psb->cur_iocbq.sli4_xritag == xri) {
631 list_del(&psb->list);
James Smart341af102010-01-26 23:07:37 -0500632 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400633 psb->status = IOSTAT_SUCCESS;
James Smart0f65ff62010-02-26 14:14:23 -0500634 spin_unlock(
635 &phba->sli4_hba.abts_scsi_buf_list_lock);
636 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartda0436e2009-05-22 14:51:39 -0400637 lpfc_release_scsi_buf_s4(phba, psb);
638 return;
639 }
640 }
James Smart0f65ff62010-02-26 14:14:23 -0500641 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
642 for (i = 1; i <= phba->sli.last_iotag; i++) {
643 iocbq = phba->sli.iocbq_lookup[i];
644
645 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
646 (iocbq->iocb_flag & LPFC_IO_LIBDFC))
647 continue;
648 if (iocbq->sli4_xritag != xri)
649 continue;
650 psb = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
651 psb->exch_busy = 0;
652 spin_unlock_irqrestore(&phba->hbalock, iflag);
653 return;
654
655 }
656 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartda0436e2009-05-22 14:51:39 -0400657}
658
659/**
660 * lpfc_sli4_repost_scsi_sgl_list - Repsot the Scsi buffers sgl pages as block
661 * @phba: pointer to lpfc hba data structure.
662 *
663 * This routine walks the list of scsi buffers that have been allocated and
664 * repost them to the HBA by using SGL block post. This is needed after a
665 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
666 * is responsible for moving all scsi buffers on the lpfc_abts_scsi_sgl_list
667 * to the lpfc_scsi_buf_list. If the repost fails, reject all scsi buffers.
668 *
669 * Returns: 0 = success, non-zero failure.
670 **/
671int
672lpfc_sli4_repost_scsi_sgl_list(struct lpfc_hba *phba)
673{
674 struct lpfc_scsi_buf *psb;
675 int index, status, bcnt = 0, rcnt = 0, rc = 0;
676 LIST_HEAD(sblist);
677
678 for (index = 0; index < phba->sli4_hba.scsi_xri_cnt; index++) {
679 psb = phba->sli4_hba.lpfc_scsi_psb_array[index];
680 if (psb) {
681 /* Remove from SCSI buffer list */
682 list_del(&psb->list);
683 /* Add it to a local SCSI buffer list */
684 list_add_tail(&psb->list, &sblist);
685 if (++rcnt == LPFC_NEMBED_MBOX_SGL_CNT) {
686 bcnt = rcnt;
687 rcnt = 0;
688 }
689 } else
690 /* A hole present in the XRI array, need to skip */
691 bcnt = rcnt;
692
693 if (index == phba->sli4_hba.scsi_xri_cnt - 1)
694 /* End of XRI array for SCSI buffer, complete */
695 bcnt = rcnt;
696
697 /* Continue until collect up to a nembed page worth of sgls */
698 if (bcnt == 0)
699 continue;
700 /* Now, post the SCSI buffer list sgls as a block */
701 status = lpfc_sli4_post_scsi_sgl_block(phba, &sblist, bcnt);
702 /* Reset SCSI buffer count for next round of posting */
703 bcnt = 0;
704 while (!list_empty(&sblist)) {
705 list_remove_head(&sblist, psb, struct lpfc_scsi_buf,
706 list);
707 if (status) {
708 /* Put this back on the abort scsi list */
James Smart341af102010-01-26 23:07:37 -0500709 psb->exch_busy = 1;
James Smartda0436e2009-05-22 14:51:39 -0400710 rc++;
James Smart341af102010-01-26 23:07:37 -0500711 } else {
712 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400713 psb->status = IOSTAT_SUCCESS;
James Smart341af102010-01-26 23:07:37 -0500714 }
James Smartda0436e2009-05-22 14:51:39 -0400715 /* Put it back into the SCSI buffer list */
716 lpfc_release_scsi_buf_s4(phba, psb);
717 }
718 }
719 return rc;
720}
721
722/**
723 * lpfc_new_scsi_buf_s4 - Scsi buffer allocator for HBA with SLI4 IF spec
724 * @vport: The virtual port for which this call being executed.
725 * @num_to_allocate: The requested number of buffers to allocate.
726 *
727 * This routine allocates a scsi buffer for device with SLI-4 interface spec,
728 * the scsi buffer contains all the necessary information needed to initiate
729 * a SCSI I/O.
730 *
731 * Return codes:
732 * int - number of scsi buffers that were allocated.
733 * 0 = failure, less than num_to_alloc is a partial failure.
734 **/
735static int
736lpfc_new_scsi_buf_s4(struct lpfc_vport *vport, int num_to_alloc)
737{
738 struct lpfc_hba *phba = vport->phba;
739 struct lpfc_scsi_buf *psb;
740 struct sli4_sge *sgl;
741 IOCB_t *iocb;
742 dma_addr_t pdma_phys_fcp_cmd;
743 dma_addr_t pdma_phys_fcp_rsp;
744 dma_addr_t pdma_phys_bpl, pdma_phys_bpl1;
745 uint16_t iotag, last_xritag = NO_XRI;
746 int status = 0, index;
747 int bcnt;
748 int non_sequential_xri = 0;
749 int rc = 0;
750 LIST_HEAD(sblist);
751
752 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
753 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
754 if (!psb)
755 break;
756
757 /*
758 * Get memory from the pci pool to map the virt space to pci bus
759 * space for an I/O. The DMA buffer includes space for the
760 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
761 * necessary to support the sg_tablesize.
762 */
763 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
764 GFP_KERNEL, &psb->dma_handle);
765 if (!psb->data) {
766 kfree(psb);
767 break;
768 }
769
770 /* Initialize virtual ptrs to dma_buf region. */
771 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
772
773 /* Allocate iotag for psb->cur_iocbq. */
774 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
775 if (iotag == 0) {
776 kfree(psb);
777 break;
778 }
779
780 psb->cur_iocbq.sli4_xritag = lpfc_sli4_next_xritag(phba);
781 if (psb->cur_iocbq.sli4_xritag == NO_XRI) {
782 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
783 psb->data, psb->dma_handle);
784 kfree(psb);
785 break;
786 }
787 if (last_xritag != NO_XRI
788 && psb->cur_iocbq.sli4_xritag != (last_xritag+1)) {
789 non_sequential_xri = 1;
790 } else
791 list_add_tail(&psb->list, &sblist);
792 last_xritag = psb->cur_iocbq.sli4_xritag;
793
794 index = phba->sli4_hba.scsi_xri_cnt++;
795 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
796
797 psb->fcp_bpl = psb->data;
798 psb->fcp_cmnd = (psb->data + phba->cfg_sg_dma_buf_size)
799 - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
800 psb->fcp_rsp = (struct fcp_rsp *)((uint8_t *)psb->fcp_cmnd +
801 sizeof(struct fcp_cmnd));
802
803 /* Initialize local short-hand pointers. */
804 sgl = (struct sli4_sge *)psb->fcp_bpl;
805 pdma_phys_bpl = psb->dma_handle;
806 pdma_phys_fcp_cmd =
807 (psb->dma_handle + phba->cfg_sg_dma_buf_size)
808 - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
809 pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
810
811 /*
812 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
813 * are sg list bdes. Initialize the first two and leave the
814 * rest for queuecommand.
815 */
816 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
817 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
James Smartda0436e2009-05-22 14:51:39 -0400818 bf_set(lpfc_sli4_sge_last, sgl, 0);
819 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -0500820 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_cmnd));
James Smartda0436e2009-05-22 14:51:39 -0400821 sgl++;
822
823 /* Setup the physical region for the FCP RSP */
824 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
825 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
James Smartda0436e2009-05-22 14:51:39 -0400826 bf_set(lpfc_sli4_sge_last, sgl, 1);
827 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -0500828 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
James Smartda0436e2009-05-22 14:51:39 -0400829
830 /*
831 * Since the IOCB for the FCP I/O is built into this
832 * lpfc_scsi_buf, initialize it with all known data now.
833 */
834 iocb = &psb->cur_iocbq.iocb;
835 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
836 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
837 /* setting the BLP size to 2 * sizeof BDE may not be correct.
838 * We are setting the bpl to point to out sgl. An sgl's
839 * entries are 16 bytes, a bpl entries are 12 bytes.
840 */
841 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
842 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
843 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
844 iocb->ulpBdeCount = 1;
845 iocb->ulpLe = 1;
846 iocb->ulpClass = CLASS3;
847 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
848 pdma_phys_bpl1 = pdma_phys_bpl + SGL_PAGE_SIZE;
849 else
850 pdma_phys_bpl1 = 0;
851 psb->dma_phys_bpl = pdma_phys_bpl;
852 phba->sli4_hba.lpfc_scsi_psb_array[index] = psb;
853 if (non_sequential_xri) {
854 status = lpfc_sli4_post_sgl(phba, pdma_phys_bpl,
855 pdma_phys_bpl1,
856 psb->cur_iocbq.sli4_xritag);
857 if (status) {
858 /* Put this back on the abort scsi list */
James Smart341af102010-01-26 23:07:37 -0500859 psb->exch_busy = 1;
James Smartda0436e2009-05-22 14:51:39 -0400860 rc++;
James Smart341af102010-01-26 23:07:37 -0500861 } else {
862 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400863 psb->status = IOSTAT_SUCCESS;
James Smart341af102010-01-26 23:07:37 -0500864 }
James Smartda0436e2009-05-22 14:51:39 -0400865 /* Put it back into the SCSI buffer list */
866 lpfc_release_scsi_buf_s4(phba, psb);
867 break;
868 }
869 }
870 if (bcnt) {
871 status = lpfc_sli4_post_scsi_sgl_block(phba, &sblist, bcnt);
872 /* Reset SCSI buffer count for next round of posting */
873 while (!list_empty(&sblist)) {
874 list_remove_head(&sblist, psb, struct lpfc_scsi_buf,
875 list);
876 if (status) {
877 /* Put this back on the abort scsi list */
James Smart341af102010-01-26 23:07:37 -0500878 psb->exch_busy = 1;
James Smartda0436e2009-05-22 14:51:39 -0400879 rc++;
James Smart341af102010-01-26 23:07:37 -0500880 } else {
881 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400882 psb->status = IOSTAT_SUCCESS;
James Smart341af102010-01-26 23:07:37 -0500883 }
James Smartda0436e2009-05-22 14:51:39 -0400884 /* Put it back into the SCSI buffer list */
885 lpfc_release_scsi_buf_s4(phba, psb);
886 }
887 }
888
889 return bcnt + non_sequential_xri - rc;
890}
891
892/**
James Smart3772a992009-05-22 14:50:54 -0400893 * lpfc_new_scsi_buf - Wrapper funciton for scsi buffer allocator
894 * @vport: The virtual port for which this call being executed.
895 * @num_to_allocate: The requested number of buffers to allocate.
896 *
897 * This routine wraps the actual SCSI buffer allocator function pointer from
898 * the lpfc_hba struct.
899 *
900 * Return codes:
901 * int - number of scsi buffers that were allocated.
902 * 0 = failure, less than num_to_alloc is a partial failure.
903 **/
904static inline int
905lpfc_new_scsi_buf(struct lpfc_vport *vport, int num_to_alloc)
906{
907 return vport->phba->lpfc_new_scsi_buf(vport, num_to_alloc);
908}
909
910/**
911 * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
912 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -0500913 *
914 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
915 * and returns to caller.
916 *
917 * Return codes:
918 * NULL - Error
919 * Pointer to lpfc_scsi_buf - Success
920 **/
Adrian Bunk455c53e2006-01-06 20:21:28 +0100921static struct lpfc_scsi_buf*
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500922lpfc_get_scsi_buf(struct lpfc_hba * phba)
dea31012005-04-17 16:05:31 -0500923{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400924 struct lpfc_scsi_buf * lpfc_cmd = NULL;
925 struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
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 list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
James Smart1dcb58e2007-04-25 09:51:30 -0400930 if (lpfc_cmd) {
931 lpfc_cmd->seg_cnt = 0;
932 lpfc_cmd->nonsg_phys = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -0500933 lpfc_cmd->prot_seg_cnt = 0;
James Smart1dcb58e2007-04-25 09:51:30 -0400934 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500935 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400936 return lpfc_cmd;
937}
938
James Smart9bad7672008-12-04 22:39:02 -0500939/**
James Smart3772a992009-05-22 14:50:54 -0400940 * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
James Smart9bad7672008-12-04 22:39:02 -0500941 * @phba: The Hba for which this call is being executed.
942 * @psb: The scsi buffer which is being released.
943 *
944 * This routine releases @psb scsi buffer by adding it to tail of @phba
945 * lpfc_scsi_buf_list list.
946 **/
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400947static void
James Smart3772a992009-05-22 14:50:54 -0400948lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400949{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500950 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500951
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500952 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400953 psb->pCmd = NULL;
dea31012005-04-17 16:05:31 -0500954 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500955 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
dea31012005-04-17 16:05:31 -0500956}
957
James Smart9bad7672008-12-04 22:39:02 -0500958/**
James Smartda0436e2009-05-22 14:51:39 -0400959 * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
960 * @phba: The Hba for which this call is being executed.
961 * @psb: The scsi buffer which is being released.
962 *
963 * This routine releases @psb scsi buffer by adding it to tail of @phba
964 * lpfc_scsi_buf_list list. For SLI4 XRI's are tied to the scsi buffer
965 * and cannot be reused for at least RA_TOV amount of time if it was
966 * aborted.
967 **/
968static void
969lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
970{
971 unsigned long iflag = 0;
972
James Smart341af102010-01-26 23:07:37 -0500973 if (psb->exch_busy) {
James Smartda0436e2009-05-22 14:51:39 -0400974 spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock,
975 iflag);
976 psb->pCmd = NULL;
977 list_add_tail(&psb->list,
978 &phba->sli4_hba.lpfc_abts_scsi_buf_list);
979 spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
980 iflag);
981 } else {
982
983 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
984 psb->pCmd = NULL;
985 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
986 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
987 }
988}
989
990/**
James Smart3772a992009-05-22 14:50:54 -0400991 * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
992 * @phba: The Hba for which this call is being executed.
993 * @psb: The scsi buffer which is being released.
994 *
995 * This routine releases @psb scsi buffer by adding it to tail of @phba
996 * lpfc_scsi_buf_list list.
997 **/
998static void
999lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1000{
1001
1002 phba->lpfc_release_scsi_buf(phba, psb);
1003}
1004
1005/**
1006 * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -05001007 * @phba: The Hba for which this call is being executed.
1008 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1009 *
1010 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
James Smart3772a992009-05-22 14:50:54 -04001011 * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
1012 * through sg elements and format the bdea. This routine also initializes all
1013 * IOCB fields which are dependent on scsi command request buffer.
James Smart9bad7672008-12-04 22:39:02 -05001014 *
1015 * Return codes:
1016 * 1 - Error
1017 * 0 - Success
1018 **/
dea31012005-04-17 16:05:31 -05001019static int
James Smart3772a992009-05-22 14:50:54 -04001020lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
dea31012005-04-17 16:05:31 -05001021{
1022 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1023 struct scatterlist *sgel = NULL;
1024 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1025 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
James Smart0f65ff62010-02-26 14:14:23 -05001026 struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
dea31012005-04-17 16:05:31 -05001027 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart34b02dc2008-08-24 21:49:55 -04001028 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
dea31012005-04-17 16:05:31 -05001029 dma_addr_t physaddr;
James Smart34b02dc2008-08-24 21:49:55 -04001030 uint32_t num_bde = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001031 int nseg, datadir = scsi_cmnd->sc_data_direction;
dea31012005-04-17 16:05:31 -05001032
1033 /*
1034 * There are three possibilities here - use scatter-gather segment, use
1035 * the single mapping, or neither. Start the lpfc command prep by
1036 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1037 * data bde entry.
1038 */
1039 bpl += 2;
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001040 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05001041 /*
1042 * The driver stores the segment count returned from pci_map_sg
1043 * because this a count of dma-mappings used to map the use_sg
1044 * pages. They are not guaranteed to be the same for those
1045 * architectures that implement an IOMMU.
1046 */
dea31012005-04-17 16:05:31 -05001047
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001048 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
1049 scsi_sg_count(scsi_cmnd), datadir);
1050 if (unlikely(!nseg))
1051 return 1;
1052
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001053 lpfc_cmd->seg_cnt = nseg;
dea31012005-04-17 16:05:31 -05001054 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001055 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1056 "9064 BLKGRD: %s: Too many sg segments from "
James Smarte2a0a9d2008-12-04 22:40:02 -05001057 "dma_map_sg. Config %d, seg_cnt %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001058 __func__, phba->cfg_sg_seg_cnt,
dea31012005-04-17 16:05:31 -05001059 lpfc_cmd->seg_cnt);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001060 scsi_dma_unmap(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001061 return 1;
1062 }
1063
1064 /*
1065 * The driver established a maximum scatter-gather segment count
1066 * during probe that limits the number of sg elements in any
1067 * single scsi command. Just run through the seg_cnt and format
1068 * the bde's.
James Smart34b02dc2008-08-24 21:49:55 -04001069 * When using SLI-3 the driver will try to fit all the BDEs into
1070 * the IOCB. If it can't then the BDEs get added to a BPL as it
1071 * does for SLI-2 mode.
dea31012005-04-17 16:05:31 -05001072 */
James Smart34b02dc2008-08-24 21:49:55 -04001073 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
dea31012005-04-17 16:05:31 -05001074 physaddr = sg_dma_address(sgel);
James Smart34b02dc2008-08-24 21:49:55 -04001075 if (phba->sli_rev == 3 &&
James Smarte2a0a9d2008-12-04 22:40:02 -05001076 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
James Smart0f65ff62010-02-26 14:14:23 -05001077 !(iocbq->iocb_flag & DSS_SECURITY_OP) &&
James Smart34b02dc2008-08-24 21:49:55 -04001078 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
1079 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1080 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
1081 data_bde->addrLow = putPaddrLow(physaddr);
1082 data_bde->addrHigh = putPaddrHigh(physaddr);
1083 data_bde++;
1084 } else {
1085 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1086 bpl->tus.f.bdeSize = sg_dma_len(sgel);
1087 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1088 bpl->addrLow =
1089 le32_to_cpu(putPaddrLow(physaddr));
1090 bpl->addrHigh =
1091 le32_to_cpu(putPaddrHigh(physaddr));
1092 bpl++;
1093 }
dea31012005-04-17 16:05:31 -05001094 }
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001095 }
dea31012005-04-17 16:05:31 -05001096
1097 /*
1098 * Finish initializing those IOCB fields that are dependent on the
James Smart34b02dc2008-08-24 21:49:55 -04001099 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
1100 * explicitly reinitialized and for SLI-3 the extended bde count is
1101 * explicitly reinitialized since all iocb memory resources are reused.
dea31012005-04-17 16:05:31 -05001102 */
James Smarte2a0a9d2008-12-04 22:40:02 -05001103 if (phba->sli_rev == 3 &&
James Smart0f65ff62010-02-26 14:14:23 -05001104 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
1105 !(iocbq->iocb_flag & DSS_SECURITY_OP)) {
James Smart34b02dc2008-08-24 21:49:55 -04001106 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
1107 /*
1108 * The extended IOCB format can only fit 3 BDE or a BPL.
1109 * This I/O has more than 3 BDE so the 1st data bde will
1110 * be a BPL that is filled in here.
1111 */
1112 physaddr = lpfc_cmd->dma_handle;
1113 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
1114 data_bde->tus.f.bdeSize = (num_bde *
1115 sizeof(struct ulp_bde64));
1116 physaddr += (sizeof(struct fcp_cmnd) +
1117 sizeof(struct fcp_rsp) +
1118 (2 * sizeof(struct ulp_bde64)));
1119 data_bde->addrHigh = putPaddrHigh(physaddr);
1120 data_bde->addrLow = putPaddrLow(physaddr);
1121 /* ebde count includes the responce bde and data bpl */
1122 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
1123 } else {
1124 /* ebde count includes the responce bde and data bdes */
1125 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1126 }
1127 } else {
1128 iocb_cmd->un.fcpi64.bdl.bdeSize =
1129 ((num_bde + 2) * sizeof(struct ulp_bde64));
James Smart0f65ff62010-02-26 14:14:23 -05001130 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
James Smart34b02dc2008-08-24 21:49:55 -04001131 }
James Smart09372822008-01-11 01:52:54 -05001132 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
James Smarte2a0a9d2008-12-04 22:40:02 -05001133
1134 /*
1135 * Due to difference in data length between DIF/non-DIF paths,
1136 * we need to set word 4 of IOCB here
1137 */
James Smarta257bf92009-04-06 18:48:10 -04001138 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001139 return 0;
1140}
1141
James Smarte2a0a9d2008-12-04 22:40:02 -05001142/*
1143 * Given a scsi cmnd, determine the BlockGuard profile to be used
1144 * with the cmd
1145 */
1146static int
James Smart6a9c52c2009-10-02 15:16:51 -04001147lpfc_sc_to_sli_prof(struct lpfc_hba *phba, struct scsi_cmnd *sc)
James Smarte2a0a9d2008-12-04 22:40:02 -05001148{
1149 uint8_t guard_type = scsi_host_get_guard(sc->device->host);
1150 uint8_t ret_prof = LPFC_PROF_INVALID;
1151
1152 if (guard_type == SHOST_DIX_GUARD_IP) {
1153 switch (scsi_get_prot_op(sc)) {
1154 case SCSI_PROT_READ_INSERT:
1155 case SCSI_PROT_WRITE_STRIP:
1156 ret_prof = LPFC_PROF_AST2;
1157 break;
1158
1159 case SCSI_PROT_READ_STRIP:
1160 case SCSI_PROT_WRITE_INSERT:
1161 ret_prof = LPFC_PROF_A1;
1162 break;
1163
Martin K. Petersenc6af4042009-09-18 17:32:59 -04001164 case SCSI_PROT_READ_PASS:
1165 case SCSI_PROT_WRITE_PASS:
James Smarte2a0a9d2008-12-04 22:40:02 -05001166 ret_prof = LPFC_PROF_AST1;
1167 break;
1168
James Smarte2a0a9d2008-12-04 22:40:02 -05001169 case SCSI_PROT_NORMAL:
1170 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001171 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1172 "9063 BLKGRD:Bad op/guard:%d/%d combination\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001173 scsi_get_prot_op(sc), guard_type);
1174 break;
1175
1176 }
1177 } else if (guard_type == SHOST_DIX_GUARD_CRC) {
1178 switch (scsi_get_prot_op(sc)) {
1179 case SCSI_PROT_READ_STRIP:
1180 case SCSI_PROT_WRITE_INSERT:
1181 ret_prof = LPFC_PROF_A1;
1182 break;
1183
1184 case SCSI_PROT_READ_PASS:
1185 case SCSI_PROT_WRITE_PASS:
1186 ret_prof = LPFC_PROF_C1;
1187 break;
1188
James Smarte2a0a9d2008-12-04 22:40:02 -05001189 case SCSI_PROT_READ_INSERT:
1190 case SCSI_PROT_WRITE_STRIP:
1191 case SCSI_PROT_NORMAL:
1192 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001193 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1194 "9075 BLKGRD: Bad op/guard:%d/%d combination\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001195 scsi_get_prot_op(sc), guard_type);
1196 break;
1197 }
1198 } else {
1199 /* unsupported format */
1200 BUG();
1201 }
1202
1203 return ret_prof;
1204}
1205
1206struct scsi_dif_tuple {
1207 __be16 guard_tag; /* Checksum */
1208 __be16 app_tag; /* Opaque storage */
1209 __be32 ref_tag; /* Target LBA or indirect LBA */
1210};
1211
1212static inline unsigned
1213lpfc_cmd_blksize(struct scsi_cmnd *sc)
1214{
1215 return sc->device->sector_size;
1216}
1217
1218/**
1219 * lpfc_get_cmd_dif_parms - Extract DIF parameters from SCSI command
1220 * @sc: in: SCSI command
James Smart3621a712009-04-06 18:47:14 -04001221 * @apptagmask: out: app tag mask
1222 * @apptagval: out: app tag value
1223 * @reftag: out: ref tag (reference tag)
James Smarte2a0a9d2008-12-04 22:40:02 -05001224 *
1225 * Description:
Martin Olsson98a17082009-04-22 18:21:29 +02001226 * Extract DIF parameters from the command if possible. Otherwise,
1227 * use default parameters.
James Smarte2a0a9d2008-12-04 22:40:02 -05001228 *
1229 **/
1230static inline void
1231lpfc_get_cmd_dif_parms(struct scsi_cmnd *sc, uint16_t *apptagmask,
1232 uint16_t *apptagval, uint32_t *reftag)
1233{
1234 struct scsi_dif_tuple *spt;
1235 unsigned char op = scsi_get_prot_op(sc);
1236 unsigned int protcnt = scsi_prot_sg_count(sc);
1237 static int cnt;
1238
1239 if (protcnt && (op == SCSI_PROT_WRITE_STRIP ||
Martin K. Petersenc6af4042009-09-18 17:32:59 -04001240 op == SCSI_PROT_WRITE_PASS)) {
James Smarte2a0a9d2008-12-04 22:40:02 -05001241
1242 cnt++;
1243 spt = page_address(sg_page(scsi_prot_sglist(sc))) +
1244 scsi_prot_sglist(sc)[0].offset;
1245 *apptagmask = 0;
1246 *apptagval = 0;
1247 *reftag = cpu_to_be32(spt->ref_tag);
1248
1249 } else {
1250 /* SBC defines ref tag to be lower 32bits of LBA */
1251 *reftag = (uint32_t) (0xffffffff & scsi_get_lba(sc));
1252 *apptagmask = 0;
1253 *apptagval = 0;
1254 }
1255}
1256
1257/*
1258 * This function sets up buffer list for protection groups of
1259 * type LPFC_PG_TYPE_NO_DIF
1260 *
1261 * This is usually used when the HBA is instructed to generate
1262 * DIFs and insert them into data stream (or strip DIF from
1263 * incoming data stream)
1264 *
1265 * The buffer list consists of just one protection group described
1266 * below:
1267 * +-------------------------+
1268 * start of prot group --> | PDE_1 |
1269 * +-------------------------+
1270 * | Data BDE |
1271 * +-------------------------+
1272 * |more Data BDE's ... (opt)|
1273 * +-------------------------+
1274 *
1275 * @sc: pointer to scsi command we're working on
1276 * @bpl: pointer to buffer list for protection groups
1277 * @datacnt: number of segments of data that have been dma mapped
1278 *
1279 * Note: Data s/g buffers have been dma mapped
1280 */
1281static int
1282lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1283 struct ulp_bde64 *bpl, int datasegcnt)
1284{
1285 struct scatterlist *sgde = NULL; /* s/g data entry */
1286 struct lpfc_pde *pde1 = NULL;
1287 dma_addr_t physaddr;
1288 int i = 0, num_bde = 0;
1289 int datadir = sc->sc_data_direction;
1290 int prof = LPFC_PROF_INVALID;
1291 unsigned blksize;
1292 uint32_t reftag;
1293 uint16_t apptagmask, apptagval;
1294
1295 pde1 = (struct lpfc_pde *) bpl;
James Smart6a9c52c2009-10-02 15:16:51 -04001296 prof = lpfc_sc_to_sli_prof(phba, sc);
James Smarte2a0a9d2008-12-04 22:40:02 -05001297
1298 if (prof == LPFC_PROF_INVALID)
1299 goto out;
1300
1301 /* extract some info from the scsi command for PDE1*/
1302 blksize = lpfc_cmd_blksize(sc);
1303 lpfc_get_cmd_dif_parms(sc, &apptagmask, &apptagval, &reftag);
1304
1305 /* setup PDE1 with what we have */
1306 lpfc_pde_set_bg_parms(pde1, LPFC_PDE1_DESCRIPTOR, prof, blksize,
1307 BG_EC_STOP_ERR);
1308 lpfc_pde_set_dif_parms(pde1, apptagmask, apptagval, reftag);
1309
1310 num_bde++;
1311 bpl++;
1312
1313 /* assumption: caller has already run dma_map_sg on command data */
1314 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
1315 physaddr = sg_dma_address(sgde);
1316 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
1317 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1318 bpl->tus.f.bdeSize = sg_dma_len(sgde);
1319 if (datadir == DMA_TO_DEVICE)
1320 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1321 else
1322 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1323 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1324 bpl++;
1325 num_bde++;
1326 }
1327
1328out:
1329 return num_bde;
1330}
1331
1332/*
1333 * This function sets up buffer list for protection groups of
1334 * type LPFC_PG_TYPE_DIF_BUF
1335 *
1336 * This is usually used when DIFs are in their own buffers,
1337 * separate from the data. The HBA can then by instructed
1338 * to place the DIFs in the outgoing stream. For read operations,
1339 * The HBA could extract the DIFs and place it in DIF buffers.
1340 *
1341 * The buffer list for this type consists of one or more of the
1342 * protection groups described below:
1343 * +-------------------------+
1344 * start of first prot group --> | PDE_1 |
1345 * +-------------------------+
1346 * | PDE_3 (Prot BDE) |
1347 * +-------------------------+
1348 * | Data BDE |
1349 * +-------------------------+
1350 * |more Data BDE's ... (opt)|
1351 * +-------------------------+
1352 * start of new prot group --> | PDE_1 |
1353 * +-------------------------+
1354 * | ... |
1355 * +-------------------------+
1356 *
1357 * @sc: pointer to scsi command we're working on
1358 * @bpl: pointer to buffer list for protection groups
1359 * @datacnt: number of segments of data that have been dma mapped
1360 * @protcnt: number of segment of protection data that have been dma mapped
1361 *
1362 * Note: It is assumed that both data and protection s/g buffers have been
1363 * mapped for DMA
1364 */
1365static int
1366lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1367 struct ulp_bde64 *bpl, int datacnt, int protcnt)
1368{
1369 struct scatterlist *sgde = NULL; /* s/g data entry */
1370 struct scatterlist *sgpe = NULL; /* s/g prot entry */
1371 struct lpfc_pde *pde1 = NULL;
1372 struct ulp_bde64 *prot_bde = NULL;
1373 dma_addr_t dataphysaddr, protphysaddr;
1374 unsigned short curr_data = 0, curr_prot = 0;
1375 unsigned int split_offset, protgroup_len;
1376 unsigned int protgrp_blks, protgrp_bytes;
1377 unsigned int remainder, subtotal;
1378 int prof = LPFC_PROF_INVALID;
1379 int datadir = sc->sc_data_direction;
1380 unsigned char pgdone = 0, alldone = 0;
1381 unsigned blksize;
1382 uint32_t reftag;
1383 uint16_t apptagmask, apptagval;
1384 int num_bde = 0;
1385
1386 sgpe = scsi_prot_sglist(sc);
1387 sgde = scsi_sglist(sc);
1388
1389 if (!sgpe || !sgde) {
1390 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1391 "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
1392 sgpe, sgde);
1393 return 0;
1394 }
1395
James Smart6a9c52c2009-10-02 15:16:51 -04001396 prof = lpfc_sc_to_sli_prof(phba, sc);
James Smarte2a0a9d2008-12-04 22:40:02 -05001397 if (prof == LPFC_PROF_INVALID)
1398 goto out;
1399
1400 /* extract some info from the scsi command for PDE1*/
1401 blksize = lpfc_cmd_blksize(sc);
1402 lpfc_get_cmd_dif_parms(sc, &apptagmask, &apptagval, &reftag);
1403
1404 split_offset = 0;
1405 do {
1406 /* setup the first PDE_1 */
1407 pde1 = (struct lpfc_pde *) bpl;
1408
1409 lpfc_pde_set_bg_parms(pde1, LPFC_PDE1_DESCRIPTOR, prof, blksize,
1410 BG_EC_STOP_ERR);
1411 lpfc_pde_set_dif_parms(pde1, apptagmask, apptagval, reftag);
1412
1413 num_bde++;
1414 bpl++;
1415
1416 /* setup the first BDE that points to protection buffer */
1417 prot_bde = (struct ulp_bde64 *) bpl;
1418 protphysaddr = sg_dma_address(sgpe);
1419 prot_bde->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
1420 prot_bde->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
1421 protgroup_len = sg_dma_len(sgpe);
1422
1423
1424 /* must be integer multiple of the DIF block length */
1425 BUG_ON(protgroup_len % 8);
1426
1427 protgrp_blks = protgroup_len / 8;
1428 protgrp_bytes = protgrp_blks * blksize;
1429
1430 prot_bde->tus.f.bdeSize = protgroup_len;
1431 if (datadir == DMA_TO_DEVICE)
1432 prot_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1433 else
1434 prot_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1435 prot_bde->tus.w = le32_to_cpu(bpl->tus.w);
1436
1437 curr_prot++;
1438 num_bde++;
1439
1440 /* setup BDE's for data blocks associated with DIF data */
1441 pgdone = 0;
1442 subtotal = 0; /* total bytes processed for current prot grp */
1443 while (!pgdone) {
1444 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -04001445 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1446 "9065 BLKGRD:%s Invalid data segment\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001447 __func__);
1448 return 0;
1449 }
1450 bpl++;
1451 dataphysaddr = sg_dma_address(sgde) + split_offset;
1452 bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
1453 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
1454
1455 remainder = sg_dma_len(sgde) - split_offset;
1456
1457 if ((subtotal + remainder) <= protgrp_bytes) {
1458 /* we can use this whole buffer */
1459 bpl->tus.f.bdeSize = remainder;
1460 split_offset = 0;
1461
1462 if ((subtotal + remainder) == protgrp_bytes)
1463 pgdone = 1;
1464 } else {
1465 /* must split this buffer with next prot grp */
1466 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
1467 split_offset += bpl->tus.f.bdeSize;
1468 }
1469
1470 subtotal += bpl->tus.f.bdeSize;
1471
1472 if (datadir == DMA_TO_DEVICE)
1473 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1474 else
1475 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1476 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1477
1478 num_bde++;
1479 curr_data++;
1480
1481 if (split_offset)
1482 break;
1483
1484 /* Move to the next s/g segment if possible */
1485 sgde = sg_next(sgde);
1486 }
1487
1488 /* are we done ? */
1489 if (curr_prot == protcnt) {
1490 alldone = 1;
1491 } else if (curr_prot < protcnt) {
1492 /* advance to next prot buffer */
1493 sgpe = sg_next(sgpe);
1494 bpl++;
1495
1496 /* update the reference tag */
1497 reftag += protgrp_blks;
1498 } else {
1499 /* if we're here, we have a bug */
James Smart6a9c52c2009-10-02 15:16:51 -04001500 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1501 "9054 BLKGRD: bug in %s\n", __func__);
James Smarte2a0a9d2008-12-04 22:40:02 -05001502 }
1503
1504 } while (!alldone);
1505
1506out:
1507
1508
1509 return num_bde;
1510}
1511/*
1512 * Given a SCSI command that supports DIF, determine composition of protection
1513 * groups involved in setting up buffer lists
1514 *
1515 * Returns:
1516 * for DIF (for both read and write)
1517 * */
1518static int
1519lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
1520{
1521 int ret = LPFC_PG_TYPE_INVALID;
1522 unsigned char op = scsi_get_prot_op(sc);
1523
1524 switch (op) {
1525 case SCSI_PROT_READ_STRIP:
1526 case SCSI_PROT_WRITE_INSERT:
1527 ret = LPFC_PG_TYPE_NO_DIF;
1528 break;
1529 case SCSI_PROT_READ_INSERT:
1530 case SCSI_PROT_WRITE_STRIP:
1531 case SCSI_PROT_READ_PASS:
1532 case SCSI_PROT_WRITE_PASS:
James Smarte2a0a9d2008-12-04 22:40:02 -05001533 ret = LPFC_PG_TYPE_DIF_BUF;
1534 break;
1535 default:
1536 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1537 "9021 Unsupported protection op:%d\n", op);
1538 break;
1539 }
1540
1541 return ret;
1542}
1543
1544/*
1545 * This is the protection/DIF aware version of
1546 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
1547 * two functions eventually, but for now, it's here
1548 */
1549static int
1550lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba,
1551 struct lpfc_scsi_buf *lpfc_cmd)
1552{
1553 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1554 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1555 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
1556 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
1557 uint32_t num_bde = 0;
1558 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
1559 int prot_group_type = 0;
1560 int diflen, fcpdl;
1561 unsigned blksize;
1562
1563 /*
1564 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
1565 * fcp_rsp regions to the first data bde entry
1566 */
1567 bpl += 2;
1568 if (scsi_sg_count(scsi_cmnd)) {
1569 /*
1570 * The driver stores the segment count returned from pci_map_sg
1571 * because this a count of dma-mappings used to map the use_sg
1572 * pages. They are not guaranteed to be the same for those
1573 * architectures that implement an IOMMU.
1574 */
1575 datasegcnt = dma_map_sg(&phba->pcidev->dev,
1576 scsi_sglist(scsi_cmnd),
1577 scsi_sg_count(scsi_cmnd), datadir);
1578 if (unlikely(!datasegcnt))
1579 return 1;
1580
1581 lpfc_cmd->seg_cnt = datasegcnt;
1582 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001583 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1584 "9067 BLKGRD: %s: Too many sg segments"
1585 " from dma_map_sg. Config %d, seg_cnt"
1586 " %d\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001587 __func__, phba->cfg_sg_seg_cnt,
1588 lpfc_cmd->seg_cnt);
1589 scsi_dma_unmap(scsi_cmnd);
1590 return 1;
1591 }
1592
1593 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
1594
1595 switch (prot_group_type) {
1596 case LPFC_PG_TYPE_NO_DIF:
1597 num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
1598 datasegcnt);
1599 /* we shoud have 2 or more entries in buffer list */
1600 if (num_bde < 2)
1601 goto err;
1602 break;
1603 case LPFC_PG_TYPE_DIF_BUF:{
1604 /*
1605 * This type indicates that protection buffers are
1606 * passed to the driver, so that needs to be prepared
1607 * for DMA
1608 */
1609 protsegcnt = dma_map_sg(&phba->pcidev->dev,
1610 scsi_prot_sglist(scsi_cmnd),
1611 scsi_prot_sg_count(scsi_cmnd), datadir);
1612 if (unlikely(!protsegcnt)) {
1613 scsi_dma_unmap(scsi_cmnd);
1614 return 1;
1615 }
1616
1617 lpfc_cmd->prot_seg_cnt = protsegcnt;
1618 if (lpfc_cmd->prot_seg_cnt
1619 > phba->cfg_prot_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001620 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1621 "9068 BLKGRD: %s: Too many prot sg "
1622 "segments from dma_map_sg. Config %d,"
James Smarte2a0a9d2008-12-04 22:40:02 -05001623 "prot_seg_cnt %d\n", __func__,
1624 phba->cfg_prot_sg_seg_cnt,
1625 lpfc_cmd->prot_seg_cnt);
1626 dma_unmap_sg(&phba->pcidev->dev,
1627 scsi_prot_sglist(scsi_cmnd),
1628 scsi_prot_sg_count(scsi_cmnd),
1629 datadir);
1630 scsi_dma_unmap(scsi_cmnd);
1631 return 1;
1632 }
1633
1634 num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
1635 datasegcnt, protsegcnt);
1636 /* we shoud have 3 or more entries in buffer list */
1637 if (num_bde < 3)
1638 goto err;
1639 break;
1640 }
1641 case LPFC_PG_TYPE_INVALID:
1642 default:
1643 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1644 "9022 Unexpected protection group %i\n",
1645 prot_group_type);
1646 return 1;
1647 }
1648 }
1649
1650 /*
1651 * Finish initializing those IOCB fields that are dependent on the
1652 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
1653 * reinitialized since all iocb memory resources are used many times
1654 * for transmit, receive, and continuation bpl's.
1655 */
1656 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
1657 iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
1658 iocb_cmd->ulpBdeCount = 1;
1659 iocb_cmd->ulpLe = 1;
1660
1661 fcpdl = scsi_bufflen(scsi_cmnd);
1662
1663 if (scsi_get_prot_type(scsi_cmnd) == SCSI_PROT_DIF_TYPE1) {
1664 /*
1665 * We are in DIF Type 1 mode
1666 * Every data block has a 8 byte DIF (trailer)
1667 * attached to it. Must ajust FCP data length
1668 */
1669 blksize = lpfc_cmd_blksize(scsi_cmnd);
1670 diflen = (fcpdl / blksize) * 8;
1671 fcpdl += diflen;
1672 }
1673 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
1674
1675 /*
1676 * Due to difference in data length between DIF/non-DIF paths,
1677 * we need to set word 4 of IOCB here
1678 */
1679 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
1680
1681 return 0;
1682err:
1683 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1684 "9023 Could not setup all needed BDE's"
1685 "prot_group_type=%d, num_bde=%d\n",
1686 prot_group_type, num_bde);
1687 return 1;
1688}
1689
1690/*
1691 * This function checks for BlockGuard errors detected by
1692 * the HBA. In case of errors, the ASC/ASCQ fields in the
1693 * sense buffer will be set accordingly, paired with
1694 * ILLEGAL_REQUEST to signal to the kernel that the HBA
1695 * detected corruption.
1696 *
1697 * Returns:
1698 * 0 - No error found
1699 * 1 - BlockGuard error found
1700 * -1 - Internal error (bad profile, ...etc)
1701 */
1702static int
1703lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
1704 struct lpfc_iocbq *pIocbOut)
1705{
1706 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
1707 struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
1708 int ret = 0;
1709 uint32_t bghm = bgf->bghm;
1710 uint32_t bgstat = bgf->bgstat;
1711 uint64_t failing_sector = 0;
1712
James Smart6a9c52c2009-10-02 15:16:51 -04001713 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9069 BLKGRD: BG ERROR in cmd"
1714 " 0x%x lba 0x%llx blk cnt 0x%x "
James Smarte2a0a9d2008-12-04 22:40:02 -05001715 "bgstat=0x%x bghm=0x%x\n",
James Smart87b5c322008-12-16 10:34:09 -05001716 cmd->cmnd[0], (unsigned long long)scsi_get_lba(cmd),
Tejun Heo83096eb2009-05-07 22:24:39 +09001717 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05001718
1719 spin_lock(&_dump_buf_lock);
1720 if (!_dump_buf_done) {
James Smart6a9c52c2009-10-02 15:16:51 -04001721 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9070 BLKGRD: Saving"
1722 " Data for %u blocks to debugfs\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001723 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
James Smart6a9c52c2009-10-02 15:16:51 -04001724 lpfc_debug_save_data(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05001725
1726 /* If we have a prot sgl, save the DIF buffer */
1727 if (lpfc_prot_group_type(phba, cmd) ==
1728 LPFC_PG_TYPE_DIF_BUF) {
James Smart6a9c52c2009-10-02 15:16:51 -04001729 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9071 BLKGRD: "
1730 "Saving DIF for %u blocks to debugfs\n",
1731 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
1732 lpfc_debug_save_dif(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05001733 }
1734
1735 _dump_buf_done = 1;
1736 }
1737 spin_unlock(&_dump_buf_lock);
1738
1739 if (lpfc_bgs_get_invalid_prof(bgstat)) {
1740 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart6a9c52c2009-10-02 15:16:51 -04001741 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9072 BLKGRD: Invalid"
1742 " BlockGuard profile. bgstat:0x%x\n",
1743 bgstat);
James Smarte2a0a9d2008-12-04 22:40:02 -05001744 ret = (-1);
1745 goto out;
1746 }
1747
1748 if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
1749 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart6a9c52c2009-10-02 15:16:51 -04001750 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9073 BLKGRD: "
1751 "Invalid BlockGuard DIF Block. bgstat:0x%x\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001752 bgstat);
1753 ret = (-1);
1754 goto out;
1755 }
1756
1757 if (lpfc_bgs_get_guard_err(bgstat)) {
1758 ret = 1;
1759
1760 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
1761 0x10, 0x1);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05001762 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05001763 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
1764 phba->bg_guard_err_cnt++;
James Smart6a9c52c2009-10-02 15:16:51 -04001765 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1766 "9055 BLKGRD: guard_tag error\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05001767 }
1768
1769 if (lpfc_bgs_get_reftag_err(bgstat)) {
1770 ret = 1;
1771
1772 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
1773 0x10, 0x3);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05001774 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05001775 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
1776
1777 phba->bg_reftag_err_cnt++;
James Smart6a9c52c2009-10-02 15:16:51 -04001778 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1779 "9056 BLKGRD: ref_tag error\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05001780 }
1781
1782 if (lpfc_bgs_get_apptag_err(bgstat)) {
1783 ret = 1;
1784
1785 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
1786 0x10, 0x2);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05001787 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05001788 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
1789
1790 phba->bg_apptag_err_cnt++;
James Smart6a9c52c2009-10-02 15:16:51 -04001791 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1792 "9061 BLKGRD: app_tag error\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05001793 }
1794
1795 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
1796 /*
1797 * setup sense data descriptor 0 per SPC-4 as an information
1798 * field, and put the failing LBA in it
1799 */
1800 cmd->sense_buffer[8] = 0; /* Information */
1801 cmd->sense_buffer[9] = 0xa; /* Add. length */
David Howells2344b5b2009-04-14 17:08:34 +01001802 bghm /= cmd->device->sector_size;
James Smarte2a0a9d2008-12-04 22:40:02 -05001803
1804 failing_sector = scsi_get_lba(cmd);
1805 failing_sector += bghm;
1806
1807 put_unaligned_be64(failing_sector, &cmd->sense_buffer[10]);
1808 }
1809
1810 if (!ret) {
1811 /* No error was reported - problem in FW? */
1812 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart6a9c52c2009-10-02 15:16:51 -04001813 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1814 "9057 BLKGRD: no errors reported!\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05001815 }
1816
1817out:
1818 return ret;
1819}
1820
James Smartea2151b2008-09-07 11:52:10 -04001821/**
James Smartda0436e2009-05-22 14:51:39 -04001822 * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
1823 * @phba: The Hba for which this call is being executed.
1824 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1825 *
1826 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
1827 * field of @lpfc_cmd for device with SLI-4 interface spec.
1828 *
1829 * Return codes:
1830 * 1 - Error
1831 * 0 - Success
1832 **/
1833static int
1834lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
1835{
1836 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1837 struct scatterlist *sgel = NULL;
1838 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1839 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
1840 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
1841 dma_addr_t physaddr;
1842 uint32_t num_bde = 0;
1843 uint32_t dma_len;
1844 uint32_t dma_offset = 0;
1845 int nseg;
1846
1847 /*
1848 * There are three possibilities here - use scatter-gather segment, use
1849 * the single mapping, or neither. Start the lpfc command prep by
1850 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1851 * data bde entry.
1852 */
1853 if (scsi_sg_count(scsi_cmnd)) {
1854 /*
1855 * The driver stores the segment count returned from pci_map_sg
1856 * because this a count of dma-mappings used to map the use_sg
1857 * pages. They are not guaranteed to be the same for those
1858 * architectures that implement an IOMMU.
1859 */
1860
1861 nseg = scsi_dma_map(scsi_cmnd);
1862 if (unlikely(!nseg))
1863 return 1;
1864 sgl += 1;
1865 /* clear the last flag in the fcp_rsp map entry */
1866 sgl->word2 = le32_to_cpu(sgl->word2);
1867 bf_set(lpfc_sli4_sge_last, sgl, 0);
1868 sgl->word2 = cpu_to_le32(sgl->word2);
1869 sgl += 1;
1870
1871 lpfc_cmd->seg_cnt = nseg;
1872 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001873 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
1874 " %s: Too many sg segments from "
1875 "dma_map_sg. Config %d, seg_cnt %d\n",
1876 __func__, phba->cfg_sg_seg_cnt,
James Smartda0436e2009-05-22 14:51:39 -04001877 lpfc_cmd->seg_cnt);
1878 scsi_dma_unmap(scsi_cmnd);
1879 return 1;
1880 }
1881
1882 /*
1883 * The driver established a maximum scatter-gather segment count
1884 * during probe that limits the number of sg elements in any
1885 * single scsi command. Just run through the seg_cnt and format
1886 * the sge's.
1887 * When using SLI-3 the driver will try to fit all the BDEs into
1888 * the IOCB. If it can't then the BDEs get added to a BPL as it
1889 * does for SLI-2 mode.
1890 */
1891 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
1892 physaddr = sg_dma_address(sgel);
1893 dma_len = sg_dma_len(sgel);
James Smartda0436e2009-05-22 14:51:39 -04001894 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
1895 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
1896 if ((num_bde + 1) == nseg)
1897 bf_set(lpfc_sli4_sge_last, sgl, 1);
1898 else
1899 bf_set(lpfc_sli4_sge_last, sgl, 0);
1900 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
1901 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05001902 sgl->sge_len = cpu_to_le32(dma_len);
James Smartda0436e2009-05-22 14:51:39 -04001903 dma_offset += dma_len;
1904 sgl++;
1905 }
1906 } else {
1907 sgl += 1;
1908 /* clear the last flag in the fcp_rsp map entry */
1909 sgl->word2 = le32_to_cpu(sgl->word2);
1910 bf_set(lpfc_sli4_sge_last, sgl, 1);
1911 sgl->word2 = cpu_to_le32(sgl->word2);
1912 }
1913
1914 /*
1915 * Finish initializing those IOCB fields that are dependent on the
1916 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
1917 * explicitly reinitialized.
1918 * all iocb memory resources are reused.
1919 */
1920 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
1921
1922 /*
1923 * Due to difference in data length between DIF/non-DIF paths,
1924 * we need to set word 4 of IOCB here
1925 */
1926 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
1927 return 0;
1928}
1929
1930/**
James Smart3772a992009-05-22 14:50:54 -04001931 * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
1932 * @phba: The Hba for which this call is being executed.
1933 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1934 *
1935 * This routine wraps the actual DMA mapping function pointer from the
1936 * lpfc_hba struct.
1937 *
1938 * Return codes:
1939 * 1 - Error
1940 * 0 - Success
1941 **/
1942static inline int
1943lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
1944{
1945 return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
1946}
1947
1948/**
James Smart3621a712009-04-06 18:47:14 -04001949 * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
James Smartea2151b2008-09-07 11:52:10 -04001950 * @phba: Pointer to hba context object.
1951 * @vport: Pointer to vport object.
1952 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
1953 * @rsp_iocb: Pointer to response iocb object which reported error.
1954 *
1955 * This function posts an event when there is a SCSI command reporting
1956 * error from the scsi device.
1957 **/
1958static void
1959lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
1960 struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
1961 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
1962 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
1963 uint32_t resp_info = fcprsp->rspStatus2;
1964 uint32_t scsi_status = fcprsp->rspStatus3;
1965 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
1966 struct lpfc_fast_path_event *fast_path_evt = NULL;
1967 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
1968 unsigned long flags;
1969
1970 /* If there is queuefull or busy condition send a scsi event */
1971 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
1972 (cmnd->result == SAM_STAT_BUSY)) {
1973 fast_path_evt = lpfc_alloc_fast_evt(phba);
1974 if (!fast_path_evt)
1975 return;
1976 fast_path_evt->un.scsi_evt.event_type =
1977 FC_REG_SCSI_EVENT;
1978 fast_path_evt->un.scsi_evt.subcategory =
1979 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
1980 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
1981 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
1982 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
1983 &pnode->nlp_portname, sizeof(struct lpfc_name));
1984 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
1985 &pnode->nlp_nodename, sizeof(struct lpfc_name));
1986 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
1987 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
1988 fast_path_evt = lpfc_alloc_fast_evt(phba);
1989 if (!fast_path_evt)
1990 return;
1991 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
1992 FC_REG_SCSI_EVENT;
1993 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
1994 LPFC_EVENT_CHECK_COND;
1995 fast_path_evt->un.check_cond_evt.scsi_event.lun =
1996 cmnd->device->lun;
1997 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
1998 &pnode->nlp_portname, sizeof(struct lpfc_name));
1999 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
2000 &pnode->nlp_nodename, sizeof(struct lpfc_name));
2001 fast_path_evt->un.check_cond_evt.sense_key =
2002 cmnd->sense_buffer[2] & 0xf;
2003 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
2004 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
2005 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
2006 fcpi_parm &&
2007 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
2008 ((scsi_status == SAM_STAT_GOOD) &&
2009 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
2010 /*
2011 * If status is good or resid does not match with fcp_param and
2012 * there is valid fcpi_parm, then there is a read_check error
2013 */
2014 fast_path_evt = lpfc_alloc_fast_evt(phba);
2015 if (!fast_path_evt)
2016 return;
2017 fast_path_evt->un.read_check_error.header.event_type =
2018 FC_REG_FABRIC_EVENT;
2019 fast_path_evt->un.read_check_error.header.subcategory =
2020 LPFC_EVENT_FCPRDCHKERR;
2021 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
2022 &pnode->nlp_portname, sizeof(struct lpfc_name));
2023 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
2024 &pnode->nlp_nodename, sizeof(struct lpfc_name));
2025 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
2026 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
2027 fast_path_evt->un.read_check_error.fcpiparam =
2028 fcpi_parm;
2029 } else
2030 return;
2031
2032 fast_path_evt->vport = vport;
2033 spin_lock_irqsave(&phba->hbalock, flags);
2034 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
2035 spin_unlock_irqrestore(&phba->hbalock, flags);
2036 lpfc_worker_wake_up(phba);
2037 return;
2038}
James Smart9bad7672008-12-04 22:39:02 -05002039
2040/**
James Smartf1126682009-06-10 17:22:44 -04002041 * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
James Smart3772a992009-05-22 14:50:54 -04002042 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -05002043 * @psb: The scsi buffer which is going to be un-mapped.
2044 *
2045 * This routine does DMA un-mapping of scatter gather list of scsi command
James Smart3772a992009-05-22 14:50:54 -04002046 * field of @lpfc_cmd for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05002047 **/
dea31012005-04-17 16:05:31 -05002048static void
James Smartf1126682009-06-10 17:22:44 -04002049lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James Smartbcf4dbf2006-07-06 15:50:08 -04002050{
2051 /*
2052 * There are only two special cases to consider. (1) the scsi command
2053 * requested scatter-gather usage or (2) the scsi command allocated
2054 * a request buffer, but did not request use_sg. There is a third
2055 * case, but it does not require resource deallocation.
2056 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002057 if (psb->seg_cnt > 0)
2058 scsi_dma_unmap(psb->pCmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002059 if (psb->prot_seg_cnt > 0)
2060 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
2061 scsi_prot_sg_count(psb->pCmd),
2062 psb->pCmd->sc_data_direction);
James Smartbcf4dbf2006-07-06 15:50:08 -04002063}
2064
James Smart9bad7672008-12-04 22:39:02 -05002065/**
James Smart3621a712009-04-06 18:47:14 -04002066 * lpfc_handler_fcp_err - FCP response handler
James Smart9bad7672008-12-04 22:39:02 -05002067 * @vport: The virtual port for which this call is being executed.
2068 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
2069 * @rsp_iocb: The response IOCB which contains FCP error.
2070 *
2071 * This routine is called to process response IOCB with status field
2072 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
2073 * based upon SCSI and FCP error.
2074 **/
James Smartbcf4dbf2006-07-06 15:50:08 -04002075static void
James Smart2e0fef82007-06-17 19:56:36 -05002076lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
2077 struct lpfc_iocbq *rsp_iocb)
dea31012005-04-17 16:05:31 -05002078{
2079 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
2080 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
2081 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
James Smart7054a602007-04-25 09:52:34 -04002082 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
dea31012005-04-17 16:05:31 -05002083 uint32_t resp_info = fcprsp->rspStatus2;
2084 uint32_t scsi_status = fcprsp->rspStatus3;
James Smartc7743952006-12-02 13:34:42 -05002085 uint32_t *lp;
dea31012005-04-17 16:05:31 -05002086 uint32_t host_status = DID_OK;
2087 uint32_t rsplen = 0;
James Smartc7743952006-12-02 13:34:42 -05002088 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea31012005-04-17 16:05:31 -05002089
James Smartea2151b2008-09-07 11:52:10 -04002090
dea31012005-04-17 16:05:31 -05002091 /*
2092 * If this is a task management command, there is no
2093 * scsi packet associated with this lpfc_cmd. The driver
2094 * consumes it.
2095 */
2096 if (fcpcmd->fcpCntl2) {
2097 scsi_status = 0;
2098 goto out;
2099 }
2100
James Smart6a9c52c2009-10-02 15:16:51 -04002101 if (resp_info & RSP_LEN_VALID) {
2102 rsplen = be32_to_cpu(fcprsp->rspRspLen);
James Smarte40a02c2010-02-26 14:13:54 -05002103 if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
James Smart6a9c52c2009-10-02 15:16:51 -04002104 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
2105 "2719 Invalid response length: "
2106 "tgt x%x lun x%x cmnd x%x rsplen x%x\n",
2107 cmnd->device->id,
2108 cmnd->device->lun, cmnd->cmnd[0],
2109 rsplen);
2110 host_status = DID_ERROR;
2111 goto out;
2112 }
James Smarte40a02c2010-02-26 14:13:54 -05002113 if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
2114 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
2115 "2757 Protocol failure detected during "
2116 "processing of FCP I/O op: "
2117 "tgt x%x lun x%x cmnd x%x rspInfo3 x%x\n",
2118 cmnd->device->id,
2119 cmnd->device->lun, cmnd->cmnd[0],
2120 fcprsp->rspInfo3);
2121 host_status = DID_ERROR;
2122 goto out;
2123 }
James Smart6a9c52c2009-10-02 15:16:51 -04002124 }
2125
James Smartc7743952006-12-02 13:34:42 -05002126 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
2127 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
2128 if (snslen > SCSI_SENSE_BUFFERSIZE)
2129 snslen = SCSI_SENSE_BUFFERSIZE;
2130
2131 if (resp_info & RSP_LEN_VALID)
2132 rsplen = be32_to_cpu(fcprsp->rspRspLen);
2133 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
2134 }
2135 lp = (uint32_t *)cmnd->sense_buffer;
2136
2137 if (!scsi_status && (resp_info & RESID_UNDER))
2138 logit = LOG_FCP;
2139
James Smarte8b62012007-08-02 11:10:09 -04002140 lpfc_printf_vlog(vport, KERN_WARNING, logit,
James Smarte2a0a9d2008-12-04 22:40:02 -05002141 "9024 FCP command x%x failed: x%x SNS x%x x%x "
James Smarte8b62012007-08-02 11:10:09 -04002142 "Data: x%x x%x x%x x%x x%x\n",
2143 cmnd->cmnd[0], scsi_status,
2144 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
2145 be32_to_cpu(fcprsp->rspResId),
2146 be32_to_cpu(fcprsp->rspSnsLen),
2147 be32_to_cpu(fcprsp->rspRspLen),
2148 fcprsp->rspInfo3);
dea31012005-04-17 16:05:31 -05002149
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002150 scsi_set_resid(cmnd, 0);
dea31012005-04-17 16:05:31 -05002151 if (resp_info & RESID_UNDER) {
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002152 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea31012005-04-17 16:05:31 -05002153
James Smarte8b62012007-08-02 11:10:09 -04002154 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05002155 "9025 FCP Read Underrun, expected %d, "
James Smarte8b62012007-08-02 11:10:09 -04002156 "residual %d Data: x%x x%x x%x\n",
2157 be32_to_cpu(fcpcmd->fcpDl),
2158 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
2159 cmnd->underflow);
dea31012005-04-17 16:05:31 -05002160
2161 /*
James Smart7054a602007-04-25 09:52:34 -04002162 * If there is an under run check if under run reported by
2163 * storage array is same as the under run reported by HBA.
2164 * If this is not same, there is a dropped frame.
2165 */
2166 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
2167 fcpi_parm &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002168 (scsi_get_resid(cmnd) != fcpi_parm)) {
James Smarte8b62012007-08-02 11:10:09 -04002169 lpfc_printf_vlog(vport, KERN_WARNING,
2170 LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05002171 "9026 FCP Read Check Error "
James Smarte8b62012007-08-02 11:10:09 -04002172 "and Underrun Data: x%x x%x x%x x%x\n",
2173 be32_to_cpu(fcpcmd->fcpDl),
2174 scsi_get_resid(cmnd), fcpi_parm,
2175 cmnd->cmnd[0]);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002176 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
James Smart7054a602007-04-25 09:52:34 -04002177 host_status = DID_ERROR;
2178 }
2179 /*
dea31012005-04-17 16:05:31 -05002180 * The cmnd->underflow is the minimum number of bytes that must
2181 * be transfered for this command. Provided a sense condition
2182 * is not present, make sure the actual amount transferred is at
2183 * least the underflow value or fail.
2184 */
2185 if (!(resp_info & SNS_LEN_VALID) &&
2186 (scsi_status == SAM_STAT_GOOD) &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002187 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
2188 < cmnd->underflow)) {
James Smarte8b62012007-08-02 11:10:09 -04002189 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05002190 "9027 FCP command x%x residual "
James Smarte8b62012007-08-02 11:10:09 -04002191 "underrun converted to error "
2192 "Data: x%x x%x x%x\n",
James Smart66dbfbe2007-08-05 06:08:38 -04002193 cmnd->cmnd[0], scsi_bufflen(cmnd),
James Smarte8b62012007-08-02 11:10:09 -04002194 scsi_get_resid(cmnd), cmnd->underflow);
dea31012005-04-17 16:05:31 -05002195 host_status = DID_ERROR;
2196 }
2197 } else if (resp_info & RESID_OVER) {
James Smarte8b62012007-08-02 11:10:09 -04002198 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05002199 "9028 FCP command x%x residual overrun error. "
James Smarte4e74272009-07-19 10:01:38 -04002200 "Data: x%x x%x\n", cmnd->cmnd[0],
James Smarte8b62012007-08-02 11:10:09 -04002201 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea31012005-04-17 16:05:31 -05002202 host_status = DID_ERROR;
2203
2204 /*
2205 * Check SLI validation that all the transfer was actually done
2206 * (fcpi_parm should be zero). Apply check only to reads.
2207 */
2208 } else if ((scsi_status == SAM_STAT_GOOD) && fcpi_parm &&
2209 (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
James Smarte8b62012007-08-02 11:10:09 -04002210 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05002211 "9029 FCP Read Check Error Data: "
James Smarte8b62012007-08-02 11:10:09 -04002212 "x%x x%x x%x x%x\n",
2213 be32_to_cpu(fcpcmd->fcpDl),
2214 be32_to_cpu(fcprsp->rspResId),
2215 fcpi_parm, cmnd->cmnd[0]);
dea31012005-04-17 16:05:31 -05002216 host_status = DID_ERROR;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002217 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea31012005-04-17 16:05:31 -05002218 }
2219
2220 out:
2221 cmnd->result = ScsiResult(host_status, scsi_status);
James Smartea2151b2008-09-07 11:52:10 -04002222 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
dea31012005-04-17 16:05:31 -05002223}
2224
James Smart9bad7672008-12-04 22:39:02 -05002225/**
James Smart3621a712009-04-06 18:47:14 -04002226 * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
James Smart9bad7672008-12-04 22:39:02 -05002227 * @phba: The Hba for which this call is being executed.
2228 * @pIocbIn: The command IOCBQ for the scsi cmnd.
James Smart3772a992009-05-22 14:50:54 -04002229 * @pIocbOut: The response IOCBQ for the scsi cmnd.
James Smart9bad7672008-12-04 22:39:02 -05002230 *
2231 * This routine assigns scsi command result by looking into response IOCB
2232 * status field appropriately. This routine handles QUEUE FULL condition as
2233 * well by ramping down device queue depth.
2234 **/
dea31012005-04-17 16:05:31 -05002235static void
2236lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
2237 struct lpfc_iocbq *pIocbOut)
2238{
2239 struct lpfc_scsi_buf *lpfc_cmd =
2240 (struct lpfc_scsi_buf *) pIocbIn->context1;
James Smart2e0fef82007-06-17 19:56:36 -05002241 struct lpfc_vport *vport = pIocbIn->vport;
dea31012005-04-17 16:05:31 -05002242 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
2243 struct lpfc_nodelist *pnode = rdata->pnode;
2244 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002245 int result;
James Smarta257bf92009-04-06 18:48:10 -04002246 struct scsi_device *tmp_sdev;
James Smart5ffc2662009-11-18 15:39:44 -05002247 int depth;
James Smartfa61a542008-01-11 01:52:42 -05002248 unsigned long flags;
James Smartea2151b2008-09-07 11:52:10 -04002249 struct lpfc_fast_path_event *fast_path_evt;
James Smarta257bf92009-04-06 18:48:10 -04002250 struct Scsi_Host *shost = cmd->device->host;
2251 uint32_t queue_depth, scsi_id;
dea31012005-04-17 16:05:31 -05002252
2253 lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
2254 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
James Smart341af102010-01-26 23:07:37 -05002255 /* pick up SLI4 exhange busy status from HBA */
2256 lpfc_cmd->exch_busy = pIocbOut->iocb_flag & LPFC_EXCHANGE_BUSY;
2257
James Smart109f6ed2008-12-04 22:39:08 -05002258 if (pnode && NLP_CHK_NODE_ACT(pnode))
2259 atomic_dec(&pnode->cmd_pending);
dea31012005-04-17 16:05:31 -05002260
2261 if (lpfc_cmd->status) {
2262 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
2263 (lpfc_cmd->result & IOERR_DRVR_MASK))
2264 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
2265 else if (lpfc_cmd->status >= IOSTAT_CNT)
2266 lpfc_cmd->status = IOSTAT_DEFAULT;
2267
James Smarte8b62012007-08-02 11:10:09 -04002268 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05002269 "9030 FCP cmd x%x failed <%d/%d> "
James Smarte8b62012007-08-02 11:10:09 -04002270 "status: x%x result: x%x Data: x%x x%x\n",
2271 cmd->cmnd[0],
2272 cmd->device ? cmd->device->id : 0xffff,
2273 cmd->device ? cmd->device->lun : 0xffff,
2274 lpfc_cmd->status, lpfc_cmd->result,
2275 pIocbOut->iocb.ulpContext,
2276 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
dea31012005-04-17 16:05:31 -05002277
2278 switch (lpfc_cmd->status) {
2279 case IOSTAT_FCP_RSP_ERROR:
2280 /* Call FCP RSP handler to determine result */
James Smart2e0fef82007-06-17 19:56:36 -05002281 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea31012005-04-17 16:05:31 -05002282 break;
2283 case IOSTAT_NPORT_BSY:
2284 case IOSTAT_FABRIC_BSY:
James Smart0f1f53a2008-08-24 21:50:18 -04002285 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
James Smartea2151b2008-09-07 11:52:10 -04002286 fast_path_evt = lpfc_alloc_fast_evt(phba);
2287 if (!fast_path_evt)
2288 break;
2289 fast_path_evt->un.fabric_evt.event_type =
2290 FC_REG_FABRIC_EVENT;
2291 fast_path_evt->un.fabric_evt.subcategory =
2292 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
2293 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
2294 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
2295 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
2296 &pnode->nlp_portname,
2297 sizeof(struct lpfc_name));
2298 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
2299 &pnode->nlp_nodename,
2300 sizeof(struct lpfc_name));
2301 }
2302 fast_path_evt->vport = vport;
2303 fast_path_evt->work_evt.evt =
2304 LPFC_EVT_FASTPATH_MGMT_EVT;
2305 spin_lock_irqsave(&phba->hbalock, flags);
2306 list_add_tail(&fast_path_evt->work_evt.evt_listp,
2307 &phba->work_list);
2308 spin_unlock_irqrestore(&phba->hbalock, flags);
2309 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05002310 break;
James Smart92d7f7b2007-06-17 19:56:38 -05002311 case IOSTAT_LOCAL_REJECT:
James Smartd7c255b2008-08-24 21:50:00 -04002312 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
James Smart92d7f7b2007-06-17 19:56:38 -05002313 lpfc_cmd->result == IOERR_NO_RESOURCES ||
James Smartd7c255b2008-08-24 21:50:00 -04002314 lpfc_cmd->result == IOERR_ABORT_REQUESTED) {
James Smart92d7f7b2007-06-17 19:56:38 -05002315 cmd->result = ScsiResult(DID_REQUEUE, 0);
James Smart58da1ff2008-04-07 10:15:56 -04002316 break;
James Smarte2a0a9d2008-12-04 22:40:02 -05002317 }
2318
2319 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
2320 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
2321 pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
2322 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
2323 /*
2324 * This is a response for a BG enabled
2325 * cmd. Parse BG error
2326 */
2327 lpfc_parse_bg_err(phba, lpfc_cmd,
2328 pIocbOut);
2329 break;
2330 } else {
2331 lpfc_printf_vlog(vport, KERN_WARNING,
2332 LOG_BG,
2333 "9031 non-zero BGSTAT "
James Smart6a9c52c2009-10-02 15:16:51 -04002334 "on unprotected cmd\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05002335 }
2336 }
2337
2338 /* else: fall through */
dea31012005-04-17 16:05:31 -05002339 default:
2340 cmd->result = ScsiResult(DID_ERROR, 0);
2341 break;
2342 }
2343
James Smart58da1ff2008-04-07 10:15:56 -04002344 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002345 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
James Smart0f1f53a2008-08-24 21:50:18 -04002346 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
2347 SAM_STAT_BUSY);
dea31012005-04-17 16:05:31 -05002348 } else {
2349 cmd->result = ScsiResult(DID_OK, 0);
2350 }
2351
2352 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
2353 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
2354
James Smarte8b62012007-08-02 11:10:09 -04002355 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
2356 "0710 Iodone <%d/%d> cmd %p, error "
2357 "x%x SNS x%x x%x Data: x%x x%x\n",
2358 cmd->device->id, cmd->device->lun, cmd,
2359 cmd->result, *lp, *(lp + 3), cmd->retries,
2360 scsi_get_resid(cmd));
dea31012005-04-17 16:05:31 -05002361 }
2362
James Smartea2151b2008-09-07 11:52:10 -04002363 lpfc_update_stats(phba, lpfc_cmd);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002364 result = cmd->result;
James Smart977b5a02008-09-07 11:52:04 -04002365 if (vport->cfg_max_scsicmpl_time &&
2366 time_after(jiffies, lpfc_cmd->start_time +
2367 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
James Smarta257bf92009-04-06 18:48:10 -04002368 spin_lock_irqsave(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05002369 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
2370 if (pnode->cmd_qdepth >
2371 atomic_read(&pnode->cmd_pending) &&
2372 (atomic_read(&pnode->cmd_pending) >
2373 LPFC_MIN_TGT_QDEPTH) &&
2374 ((cmd->cmnd[0] == READ_10) ||
2375 (cmd->cmnd[0] == WRITE_10)))
2376 pnode->cmd_qdepth =
2377 atomic_read(&pnode->cmd_pending);
James Smart977b5a02008-09-07 11:52:04 -04002378
James Smart109f6ed2008-12-04 22:39:08 -05002379 pnode->last_change_time = jiffies;
2380 }
James Smarta257bf92009-04-06 18:48:10 -04002381 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05002382 } else if (pnode && NLP_CHK_NODE_ACT(pnode)) {
2383 if ((pnode->cmd_qdepth < LPFC_MAX_TGT_QDEPTH) &&
James Smart977b5a02008-09-07 11:52:04 -04002384 time_after(jiffies, pnode->last_change_time +
James Smart109f6ed2008-12-04 22:39:08 -05002385 msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
James Smarta257bf92009-04-06 18:48:10 -04002386 spin_lock_irqsave(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05002387 pnode->cmd_qdepth += pnode->cmd_qdepth *
2388 LPFC_TGTQ_RAMPUP_PCENT / 100;
2389 if (pnode->cmd_qdepth > LPFC_MAX_TGT_QDEPTH)
2390 pnode->cmd_qdepth = LPFC_MAX_TGT_QDEPTH;
2391 pnode->last_change_time = jiffies;
James Smarta257bf92009-04-06 18:48:10 -04002392 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05002393 }
James Smart977b5a02008-09-07 11:52:04 -04002394 }
2395
James Smart1dcb58e2007-04-25 09:51:30 -04002396 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James Smarta257bf92009-04-06 18:48:10 -04002397
2398 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
2399 queue_depth = cmd->device->queue_depth;
2400 scsi_id = cmd->device->id;
dea31012005-04-17 16:05:31 -05002401 cmd->scsi_done(cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002402
Jamie Wellnitzb8086082006-02-28 22:33:12 -05002403 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smartfa61a542008-01-11 01:52:42 -05002404 /*
2405 * If there is a thread waiting for command completion
2406 * wake up the thread.
2407 */
James Smarta257bf92009-04-06 18:48:10 -04002408 spin_lock_irqsave(shost->host_lock, flags);
James Smart495a7142008-06-14 22:52:59 -04002409 lpfc_cmd->pCmd = NULL;
James Smartfa61a542008-01-11 01:52:42 -05002410 if (lpfc_cmd->waitq)
2411 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04002412 spin_unlock_irqrestore(shost->host_lock, flags);
Jamie Wellnitzb8086082006-02-28 22:33:12 -05002413 lpfc_release_scsi_buf(phba, lpfc_cmd);
2414 return;
2415 }
2416
James Smart92d7f7b2007-06-17 19:56:38 -05002417 if (!result)
James Smarta257bf92009-04-06 18:48:10 -04002418 lpfc_rampup_queue_depth(vport, queue_depth);
James Smart92d7f7b2007-06-17 19:56:38 -05002419
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002420 /*
2421 * Check for queue full. If the lun is reporting queue full, then
2422 * back off the lun queue depth to prevent target overloads.
2423 */
James Smart58da1ff2008-04-07 10:15:56 -04002424 if (result == SAM_STAT_TASK_SET_FULL && pnode &&
2425 NLP_CHK_NODE_ACT(pnode)) {
James Smarta257bf92009-04-06 18:48:10 -04002426 shost_for_each_device(tmp_sdev, shost) {
2427 if (tmp_sdev->id != scsi_id)
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002428 continue;
2429 depth = scsi_track_queue_full(tmp_sdev,
James Smart5ffc2662009-11-18 15:39:44 -05002430 tmp_sdev->queue_depth-1);
2431 if (depth <= 0)
2432 continue;
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,
James Smart5ffc2662009-11-18 15:39:44 -05002437 pnode,
2438 tmp_sdev->lun,
2439 depth+1, depth);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002440 }
2441 }
2442
James Smartfa61a542008-01-11 01:52:42 -05002443 /*
2444 * If there is a thread waiting for command completion
2445 * wake up the thread.
2446 */
James Smarta257bf92009-04-06 18:48:10 -04002447 spin_lock_irqsave(shost->host_lock, flags);
James Smart495a7142008-06-14 22:52:59 -04002448 lpfc_cmd->pCmd = NULL;
James Smartfa61a542008-01-11 01:52:42 -05002449 if (lpfc_cmd->waitq)
2450 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04002451 spin_unlock_irqrestore(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05002452
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002453 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05002454}
2455
James Smart34b02dc2008-08-24 21:49:55 -04002456/**
James Smart3621a712009-04-06 18:47:14 -04002457 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
James Smart34b02dc2008-08-24 21:49:55 -04002458 * @data: A pointer to the immediate command data portion of the IOCB.
2459 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
2460 *
2461 * The routine copies the entire FCP command from @fcp_cmnd to @data while
2462 * byte swapping the data to big endian format for transmission on the wire.
2463 **/
2464static void
2465lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
2466{
2467 int i, j;
2468 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
2469 i += sizeof(uint32_t), j++) {
2470 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
2471 }
2472}
2473
James Smart9bad7672008-12-04 22:39:02 -05002474/**
James Smartf1126682009-06-10 17:22:44 -04002475 * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05002476 * @vport: The virtual port for which this call is being executed.
2477 * @lpfc_cmd: The scsi command which needs to send.
2478 * @pnode: Pointer to lpfc_nodelist.
2479 *
2480 * This routine initializes fcp_cmnd and iocb data structure from scsi command
James Smart3772a992009-05-22 14:50:54 -04002481 * to transfer for device with SLI3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05002482 **/
dea31012005-04-17 16:05:31 -05002483static void
James Smartf1126682009-06-10 17:22:44 -04002484lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
James Smart2e0fef82007-06-17 19:56:36 -05002485 struct lpfc_nodelist *pnode)
dea31012005-04-17 16:05:31 -05002486{
James Smart2e0fef82007-06-17 19:56:36 -05002487 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002488 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2489 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2490 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2491 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
2492 int datadir = scsi_cmnd->sc_data_direction;
James Smart7e2b19f2007-10-29 11:00:39 -04002493 char tag[2];
dea31012005-04-17 16:05:31 -05002494
James Smart58da1ff2008-04-07 10:15:56 -04002495 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
2496 return;
2497
dea31012005-04-17 16:05:31 -05002498 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
James.Smart@Emulex.Com69859dc2005-08-10 15:02:37 -04002499 /* clear task management bits */
2500 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea31012005-04-17 16:05:31 -05002501
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -04002502 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
2503 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05002504
2505 memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, 16);
2506
James Smart7e2b19f2007-10-29 11:00:39 -04002507 if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
2508 switch (tag[0]) {
dea31012005-04-17 16:05:31 -05002509 case HEAD_OF_QUEUE_TAG:
2510 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
2511 break;
2512 case ORDERED_QUEUE_TAG:
2513 fcp_cmnd->fcpCntl1 = ORDERED_Q;
2514 break;
2515 default:
2516 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
2517 break;
2518 }
2519 } else
2520 fcp_cmnd->fcpCntl1 = 0;
2521
2522 /*
2523 * There are three possibilities here - use scatter-gather segment, use
2524 * the single mapping, or neither. Start the lpfc command prep by
2525 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
2526 * data bde entry.
2527 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002528 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05002529 if (datadir == DMA_TO_DEVICE) {
2530 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
James Smart3772a992009-05-22 14:50:54 -04002531 if (phba->sli_rev < LPFC_SLI_REV4) {
2532 iocb_cmd->un.fcpi.fcpi_parm = 0;
2533 iocb_cmd->ulpPU = 0;
2534 } else
2535 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05002536 fcp_cmnd->fcpCntl3 = WRITE_DATA;
2537 phba->fc4OutputRequests++;
2538 } else {
2539 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
2540 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05002541 fcp_cmnd->fcpCntl3 = READ_DATA;
2542 phba->fc4InputRequests++;
2543 }
2544 } else {
2545 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
2546 iocb_cmd->un.fcpi.fcpi_parm = 0;
2547 iocb_cmd->ulpPU = 0;
2548 fcp_cmnd->fcpCntl3 = 0;
2549 phba->fc4ControlRequests++;
2550 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002551 if (phba->sli_rev == 3 &&
2552 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04002553 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05002554 /*
2555 * Finish initializing those IOCB fields that are independent
2556 * of the scsi_cmnd request_buffer
2557 */
2558 piocbq->iocb.ulpContext = pnode->nlp_rpi;
2559 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
2560 piocbq->iocb.ulpFCP2Rcvy = 1;
James Smart09372822008-01-11 01:52:54 -05002561 else
2562 piocbq->iocb.ulpFCP2Rcvy = 0;
dea31012005-04-17 16:05:31 -05002563
2564 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
2565 piocbq->context1 = lpfc_cmd;
2566 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
2567 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
James Smart2e0fef82007-06-17 19:56:36 -05002568 piocbq->vport = vport;
dea31012005-04-17 16:05:31 -05002569}
2570
James Smart9bad7672008-12-04 22:39:02 -05002571/**
James Smartf1126682009-06-10 17:22:44 -04002572 * lpfc_scsi_prep_task_mgmt_cmnd - Convert SLI3 scsi TM cmd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05002573 * @vport: The virtual port for which this call is being executed.
2574 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
2575 * @lun: Logical unit number.
2576 * @task_mgmt_cmd: SCSI task management command.
2577 *
James Smart3772a992009-05-22 14:50:54 -04002578 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
2579 * for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05002580 *
2581 * Return codes:
2582 * 0 - Error
2583 * 1 - Success
2584 **/
dea31012005-04-17 16:05:31 -05002585static int
James Smartf1126682009-06-10 17:22:44 -04002586lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
dea31012005-04-17 16:05:31 -05002587 struct lpfc_scsi_buf *lpfc_cmd,
James Smart420b630d2006-07-06 15:50:16 -04002588 unsigned int lun,
dea31012005-04-17 16:05:31 -05002589 uint8_t task_mgmt_cmd)
2590{
dea31012005-04-17 16:05:31 -05002591 struct lpfc_iocbq *piocbq;
2592 IOCB_t *piocb;
2593 struct fcp_cmnd *fcp_cmnd;
James Smart0b18ac42006-05-01 21:50:40 -04002594 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea31012005-04-17 16:05:31 -05002595 struct lpfc_nodelist *ndlp = rdata->pnode;
2596
James Smart58da1ff2008-04-07 10:15:56 -04002597 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
2598 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05002599 return 0;
dea31012005-04-17 16:05:31 -05002600
dea31012005-04-17 16:05:31 -05002601 piocbq = &(lpfc_cmd->cur_iocbq);
James Smart2e0fef82007-06-17 19:56:36 -05002602 piocbq->vport = vport;
2603
dea31012005-04-17 16:05:31 -05002604 piocb = &piocbq->iocb;
2605
2606 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart34b02dc2008-08-24 21:49:55 -04002607 /* Clear out any old data in the FCP command area */
2608 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
2609 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05002610 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
James Smarte2a0a9d2008-12-04 22:40:02 -05002611 if (vport->phba->sli_rev == 3 &&
2612 !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04002613 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05002614 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
dea31012005-04-17 16:05:31 -05002615 piocb->ulpContext = ndlp->nlp_rpi;
2616 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
2617 piocb->ulpFCP2Rcvy = 1;
2618 }
2619 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
2620
2621 /* ulpTimeout is only one byte */
2622 if (lpfc_cmd->timeout > 0xff) {
2623 /*
2624 * Do not timeout the command at the firmware level.
2625 * The driver will provide the timeout mechanism.
2626 */
2627 piocb->ulpTimeout = 0;
James Smartf1126682009-06-10 17:22:44 -04002628 } else
dea31012005-04-17 16:05:31 -05002629 piocb->ulpTimeout = lpfc_cmd->timeout;
James Smartf1126682009-06-10 17:22:44 -04002630
2631 if (vport->phba->sli_rev == LPFC_SLI_REV4)
2632 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05002633
James Smart2e0fef82007-06-17 19:56:36 -05002634 return 1;
dea31012005-04-17 16:05:31 -05002635}
2636
James Smart9bad7672008-12-04 22:39:02 -05002637/**
James Smart3772a992009-05-22 14:50:54 -04002638 * lpfc_scsi_api_table_setup - Set up scsi api fucntion jump table
2639 * @phba: The hba struct for which this call is being executed.
2640 * @dev_grp: The HBA PCI-Device group number.
2641 *
2642 * This routine sets up the SCSI interface API function jump table in @phba
2643 * struct.
2644 * Returns: 0 - success, -ENODEV - failure.
2645 **/
2646int
2647lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
2648{
2649
James Smartf1126682009-06-10 17:22:44 -04002650 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
2651 phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
2652 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf;
2653
James Smart3772a992009-05-22 14:50:54 -04002654 switch (dev_grp) {
2655 case LPFC_PCI_DEV_LP:
2656 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s3;
2657 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04002658 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
2659 break;
James Smartda0436e2009-05-22 14:51:39 -04002660 case LPFC_PCI_DEV_OC:
2661 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s4;
2662 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04002663 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
2664 break;
James Smart3772a992009-05-22 14:50:54 -04002665 default:
2666 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2667 "1418 Invalid HBA PCI-device group: 0x%x\n",
2668 dev_grp);
2669 return -ENODEV;
2670 break;
2671 }
2672 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf;
2673 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
James Smart84d1b002010-02-12 14:42:33 -05002674 phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
James Smart3772a992009-05-22 14:50:54 -04002675 return 0;
2676}
2677
2678/**
James Smart3621a712009-04-06 18:47:14 -04002679 * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
James Smart9bad7672008-12-04 22:39:02 -05002680 * @phba: The Hba for which this call is being executed.
2681 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
2682 * @rspiocbq: Pointer to lpfc_iocbq data structure.
2683 *
2684 * This routine is IOCB completion routine for device reset and target reset
2685 * routine. This routine release scsi buffer associated with lpfc_cmd.
2686 **/
James Smart7054a602007-04-25 09:52:34 -04002687static void
2688lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
2689 struct lpfc_iocbq *cmdiocbq,
2690 struct lpfc_iocbq *rspiocbq)
2691{
2692 struct lpfc_scsi_buf *lpfc_cmd =
2693 (struct lpfc_scsi_buf *) cmdiocbq->context1;
2694 if (lpfc_cmd)
2695 lpfc_release_scsi_buf(phba, lpfc_cmd);
2696 return;
2697}
2698
James Smart9bad7672008-12-04 22:39:02 -05002699/**
James Smart3621a712009-04-06 18:47:14 -04002700 * lpfc_info - Info entry point of scsi_host_template data structure
James Smart9bad7672008-12-04 22:39:02 -05002701 * @host: The scsi host for which this call is being executed.
2702 *
2703 * This routine provides module information about hba.
2704 *
2705 * Reutrn code:
2706 * Pointer to char - Success.
2707 **/
dea31012005-04-17 16:05:31 -05002708const char *
2709lpfc_info(struct Scsi_Host *host)
2710{
James Smart2e0fef82007-06-17 19:56:36 -05002711 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
2712 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002713 int len;
2714 static char lpfcinfobuf[384];
2715
2716 memset(lpfcinfobuf,0,384);
2717 if (phba && phba->pcidev){
2718 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
2719 len = strlen(lpfcinfobuf);
2720 snprintf(lpfcinfobuf + len,
2721 384-len,
2722 " on PCI bus %02x device %02x irq %d",
2723 phba->pcidev->bus->number,
2724 phba->pcidev->devfn,
2725 phba->pcidev->irq);
2726 len = strlen(lpfcinfobuf);
2727 if (phba->Port[0]) {
2728 snprintf(lpfcinfobuf + len,
2729 384-len,
2730 " port %s",
2731 phba->Port);
2732 }
James Smart65467b62010-01-26 23:08:29 -05002733 len = strlen(lpfcinfobuf);
2734 if (phba->sli4_hba.link_state.logical_speed) {
2735 snprintf(lpfcinfobuf + len,
2736 384-len,
2737 " Logical Link Speed: %d Mbps",
2738 phba->sli4_hba.link_state.logical_speed * 10);
2739 }
dea31012005-04-17 16:05:31 -05002740 }
2741 return lpfcinfobuf;
2742}
2743
James Smart9bad7672008-12-04 22:39:02 -05002744/**
James Smart3621a712009-04-06 18:47:14 -04002745 * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
James Smart9bad7672008-12-04 22:39:02 -05002746 * @phba: The Hba for which this call is being executed.
2747 *
2748 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
2749 * The default value of cfg_poll_tmo is 10 milliseconds.
2750 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002751static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
2752{
2753 unsigned long poll_tmo_expires =
2754 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
2755
2756 if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
2757 mod_timer(&phba->fcp_poll_timer,
2758 poll_tmo_expires);
2759}
2760
James Smart9bad7672008-12-04 22:39:02 -05002761/**
James Smart3621a712009-04-06 18:47:14 -04002762 * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
James Smart9bad7672008-12-04 22:39:02 -05002763 * @phba: The Hba for which this call is being executed.
2764 *
2765 * This routine starts the fcp_poll_timer of @phba.
2766 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002767void lpfc_poll_start_timer(struct lpfc_hba * phba)
2768{
2769 lpfc_poll_rearm_timer(phba);
2770}
2771
James Smart9bad7672008-12-04 22:39:02 -05002772/**
James Smart3621a712009-04-06 18:47:14 -04002773 * lpfc_poll_timeout - Restart polling timer
James Smart9bad7672008-12-04 22:39:02 -05002774 * @ptr: Map to lpfc_hba data structure pointer.
2775 *
2776 * This routine restarts fcp_poll timer, when FCP ring polling is enable
2777 * and FCP Ring interrupt is disable.
2778 **/
2779
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002780void lpfc_poll_timeout(unsigned long ptr)
2781{
James Smart2e0fef82007-06-17 19:56:36 -05002782 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002783
2784 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04002785 lpfc_sli_handle_fast_ring_event(phba,
2786 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
2787
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002788 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
2789 lpfc_poll_rearm_timer(phba);
2790 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002791}
2792
James Smart9bad7672008-12-04 22:39:02 -05002793/**
James Smart3621a712009-04-06 18:47:14 -04002794 * lpfc_queuecommand - scsi_host_template queuecommand entry point
James Smart9bad7672008-12-04 22:39:02 -05002795 * @cmnd: Pointer to scsi_cmnd data structure.
2796 * @done: Pointer to done routine.
2797 *
2798 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
2799 * This routine prepares an IOCB from scsi command and provides to firmware.
2800 * The @done callback is invoked after driver finished processing the command.
2801 *
2802 * Return value :
2803 * 0 - Success
2804 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
2805 **/
dea31012005-04-17 16:05:31 -05002806static int
2807lpfc_queuecommand(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
2808{
James Smart2e0fef82007-06-17 19:56:36 -05002809 struct Scsi_Host *shost = cmnd->device->host;
2810 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2811 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002812 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05002813 struct lpfc_nodelist *ndlp;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002814 struct lpfc_scsi_buf *lpfc_cmd;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002815 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002816 int err;
dea31012005-04-17 16:05:31 -05002817
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002818 err = fc_remote_port_chkready(rport);
2819 if (err) {
2820 cmnd->result = err;
dea31012005-04-17 16:05:31 -05002821 goto out_fail_command;
2822 }
James Smart1c6f4ef52009-11-18 15:40:49 -05002823 ndlp = rdata->pnode;
dea31012005-04-17 16:05:31 -05002824
James Smarte2a0a9d2008-12-04 22:40:02 -05002825 if (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
2826 scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
2827
James Smart6a9c52c2009-10-02 15:16:51 -04002828 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2829 "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
2830 " op:%02x str=%s without registering for"
2831 " BlockGuard - Rejecting command\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002832 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
2833 dif_op_str[scsi_get_prot_op(cmnd)]);
2834 goto out_fail_command;
2835 }
2836
dea31012005-04-17 16:05:31 -05002837 /*
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002838 * Catch race where our node has transitioned, but the
2839 * transport is still transitioning.
dea31012005-04-17 16:05:31 -05002840 */
James Smartb522d7d2008-09-07 11:51:56 -04002841 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
2842 cmnd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
2843 goto out_fail_command;
2844 }
James Smart109f6ed2008-12-04 22:39:08 -05002845 if (vport->cfg_max_scsicmpl_time &&
2846 (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth))
James Smart977b5a02008-09-07 11:52:04 -04002847 goto out_host_busy;
Mike Christiea93ce022008-08-17 15:24:41 -05002848
James Smarted957682007-06-17 19:56:37 -05002849 lpfc_cmd = lpfc_get_scsi_buf(phba);
dea31012005-04-17 16:05:31 -05002850 if (lpfc_cmd == NULL) {
James Smarteaf15d52008-12-04 22:39:29 -05002851 lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002852
James Smarte8b62012007-08-02 11:10:09 -04002853 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
2854 "0707 driver's buffer pool is empty, "
2855 "IO busied\n");
dea31012005-04-17 16:05:31 -05002856 goto out_host_busy;
2857 }
2858
2859 /*
2860 * Store the midlayer's command structure for the completion phase
2861 * and complete the command initialization.
2862 */
2863 lpfc_cmd->pCmd = cmnd;
2864 lpfc_cmd->rdata = rdata;
2865 lpfc_cmd->timeout = 0;
James Smart977b5a02008-09-07 11:52:04 -04002866 lpfc_cmd->start_time = jiffies;
dea31012005-04-17 16:05:31 -05002867 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
2868 cmnd->scsi_done = done;
2869
James Smarte2a0a9d2008-12-04 22:40:02 -05002870 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
James Smart6a9c52c2009-10-02 15:16:51 -04002871 if (vport->phba->cfg_enable_bg) {
2872 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05002873 "9033 BLKGRD: rcvd protected cmd:%02x op:%02x "
2874 "str=%s\n",
2875 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
2876 dif_op_str[scsi_get_prot_op(cmnd)]);
James Smart6a9c52c2009-10-02 15:16:51 -04002877 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05002878 "9034 BLKGRD: CDB: %02x %02x %02x %02x %02x "
James Smarte4e74272009-07-19 10:01:38 -04002879 "%02x %02x %02x %02x %02x\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002880 cmnd->cmnd[0], cmnd->cmnd[1], cmnd->cmnd[2],
2881 cmnd->cmnd[3], cmnd->cmnd[4], cmnd->cmnd[5],
2882 cmnd->cmnd[6], cmnd->cmnd[7], cmnd->cmnd[8],
2883 cmnd->cmnd[9]);
James Smart6a9c52c2009-10-02 15:16:51 -04002884 if (cmnd->cmnd[0] == READ_10)
2885 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05002886 "9035 BLKGRD: READ @ sector %llu, "
Tejun Heo83096eb2009-05-07 22:24:39 +09002887 "count %u\n",
2888 (unsigned long long)scsi_get_lba(cmnd),
2889 blk_rq_sectors(cmnd->request));
James Smart6a9c52c2009-10-02 15:16:51 -04002890 else if (cmnd->cmnd[0] == WRITE_10)
2891 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05002892 "9036 BLKGRD: WRITE @ sector %llu, "
Tejun Heo83096eb2009-05-07 22:24:39 +09002893 "count %u cmd=%p\n",
James Smart87b5c322008-12-16 10:34:09 -05002894 (unsigned long long)scsi_get_lba(cmnd),
Tejun Heo83096eb2009-05-07 22:24:39 +09002895 blk_rq_sectors(cmnd->request),
James Smarte2a0a9d2008-12-04 22:40:02 -05002896 cmnd);
James Smart6a9c52c2009-10-02 15:16:51 -04002897 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002898
2899 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
2900 } else {
James Smart6a9c52c2009-10-02 15:16:51 -04002901 if (vport->phba->cfg_enable_bg) {
James Smarte2a0a9d2008-12-04 22:40:02 -05002902 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smart6a9c52c2009-10-02 15:16:51 -04002903 "9038 BLKGRD: rcvd unprotected cmd:"
2904 "%02x op:%02x str=%s\n",
2905 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
2906 dif_op_str[scsi_get_prot_op(cmnd)]);
2907 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
2908 "9039 BLKGRD: CDB: %02x %02x %02x "
2909 "%02x %02x %02x %02x %02x %02x %02x\n",
2910 cmnd->cmnd[0], cmnd->cmnd[1],
2911 cmnd->cmnd[2], cmnd->cmnd[3],
2912 cmnd->cmnd[4], cmnd->cmnd[5],
2913 cmnd->cmnd[6], cmnd->cmnd[7],
2914 cmnd->cmnd[8], cmnd->cmnd[9]);
2915 if (cmnd->cmnd[0] == READ_10)
2916 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
2917 "9040 dbg: READ @ sector %llu, "
2918 "count %u\n",
2919 (unsigned long long)scsi_get_lba(cmnd),
Tejun Heo83096eb2009-05-07 22:24:39 +09002920 blk_rq_sectors(cmnd->request));
James Smart6a9c52c2009-10-02 15:16:51 -04002921 else if (cmnd->cmnd[0] == WRITE_10)
2922 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05002923 "9041 dbg: WRITE @ sector %llu, "
Tejun Heo83096eb2009-05-07 22:24:39 +09002924 "count %u cmd=%p\n",
James Smart87b5c322008-12-16 10:34:09 -05002925 (unsigned long long)scsi_get_lba(cmnd),
Tejun Heo83096eb2009-05-07 22:24:39 +09002926 blk_rq_sectors(cmnd->request), cmnd);
James Smart6a9c52c2009-10-02 15:16:51 -04002927 else
2928 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05002929 "9042 dbg: parser not implemented\n");
James Smart6a9c52c2009-10-02 15:16:51 -04002930 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002931 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
2932 }
2933
dea31012005-04-17 16:05:31 -05002934 if (err)
2935 goto out_host_busy_free_buf;
2936
James Smart2e0fef82007-06-17 19:56:36 -05002937 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea31012005-04-17 16:05:31 -05002938
James Smart977b5a02008-09-07 11:52:04 -04002939 atomic_inc(&ndlp->cmd_pending);
James Smart3772a992009-05-22 14:50:54 -04002940 err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
James Smart92d7f7b2007-06-17 19:56:38 -05002941 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
James Smarteaf15d52008-12-04 22:39:29 -05002942 if (err) {
2943 atomic_dec(&ndlp->cmd_pending);
dea31012005-04-17 16:05:31 -05002944 goto out_host_busy_free_buf;
James Smarteaf15d52008-12-04 22:39:29 -05002945 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002946 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04002947 spin_unlock(shost->host_lock);
2948 lpfc_sli_handle_fast_ring_event(phba,
2949 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
2950
2951 spin_lock(shost->host_lock);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002952 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
2953 lpfc_poll_rearm_timer(phba);
2954 }
2955
dea31012005-04-17 16:05:31 -05002956 return 0;
2957
2958 out_host_busy_free_buf:
James Smartbcf4dbf2006-07-06 15:50:08 -04002959 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002960 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05002961 out_host_busy:
2962 return SCSI_MLQUEUE_HOST_BUSY;
2963
2964 out_fail_command:
2965 done(cmnd);
2966 return 0;
2967}
2968
James Smart9bad7672008-12-04 22:39:02 -05002969/**
James Smart3621a712009-04-06 18:47:14 -04002970 * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05002971 * @cmnd: Pointer to scsi_cmnd data structure.
2972 *
2973 * This routine aborts @cmnd pending in base driver.
2974 *
2975 * Return code :
2976 * 0x2003 - Error
2977 * 0x2002 - Success
2978 **/
dea31012005-04-17 16:05:31 -05002979static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05002980lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05002981{
James Smart2e0fef82007-06-17 19:56:36 -05002982 struct Scsi_Host *shost = cmnd->device->host;
2983 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2984 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002985 struct lpfc_iocbq *iocb;
2986 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05002987 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05002988 IOCB_t *cmd, *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002989 int ret = SUCCESS;
James Smartfa61a542008-01-11 01:52:42 -05002990 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002991
Christof Schmitt65d430f2009-10-30 17:59:29 +01002992 fc_block_scsi_eh(cmnd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002993 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
2994 BUG_ON(!lpfc_cmd);
dea31012005-04-17 16:05:31 -05002995
2996 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002997 * If pCmd field of the corresponding lpfc_scsi_buf structure
2998 * points to a different SCSI command, then the driver has
2999 * already completed this command, but the midlayer did not
3000 * see the completion before the eh fired. Just return
3001 * SUCCESS.
dea31012005-04-17 16:05:31 -05003002 */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003003 iocb = &lpfc_cmd->cur_iocbq;
3004 if (lpfc_cmd->pCmd != cmnd)
3005 goto out;
dea31012005-04-17 16:05:31 -05003006
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003007 BUG_ON(iocb->context1 != lpfc_cmd);
dea31012005-04-17 16:05:31 -05003008
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003009 abtsiocb = lpfc_sli_get_iocbq(phba);
3010 if (abtsiocb == NULL) {
3011 ret = FAILED;
dea31012005-04-17 16:05:31 -05003012 goto out;
3013 }
3014
dea31012005-04-17 16:05:31 -05003015 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003016 * The scsi command can not be in txq and it is in flight because the
3017 * pCmd is still pointig at the SCSI command we have to abort. There
3018 * is no need to search the txcmplq. Just send an abort to the FW.
dea31012005-04-17 16:05:31 -05003019 */
dea31012005-04-17 16:05:31 -05003020
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003021 cmd = &iocb->iocb;
3022 icmd = &abtsiocb->iocb;
3023 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
3024 icmd->un.acxri.abortContextTag = cmd->ulpContext;
James Smart3772a992009-05-22 14:50:54 -04003025 if (phba->sli_rev == LPFC_SLI_REV4)
3026 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
3027 else
3028 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05003029
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003030 icmd->ulpLe = 1;
3031 icmd->ulpClass = cmd->ulpClass;
James Smart5ffc2662009-11-18 15:39:44 -05003032
3033 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
3034 abtsiocb->fcp_wqidx = iocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05003035 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05003036
James Smart2e0fef82007-06-17 19:56:36 -05003037 if (lpfc_is_link_up(phba))
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003038 icmd->ulpCommand = CMD_ABORT_XRI_CN;
3039 else
3040 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea31012005-04-17 16:05:31 -05003041
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003042 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smart2e0fef82007-06-17 19:56:36 -05003043 abtsiocb->vport = vport;
James Smart3772a992009-05-22 14:50:54 -04003044 if (lpfc_sli_issue_iocb(phba, LPFC_FCP_RING, abtsiocb, 0) ==
3045 IOCB_ERROR) {
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003046 lpfc_sli_release_iocbq(phba, abtsiocb);
3047 ret = FAILED;
3048 goto out;
3049 }
3050
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003051 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
James Smart45ed1192009-10-02 15:17:02 -04003052 lpfc_sli_handle_fast_ring_event(phba,
3053 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003054
James Smartfa61a542008-01-11 01:52:42 -05003055 lpfc_cmd->waitq = &waitq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003056 /* Wait for abort to complete */
James Smartfa61a542008-01-11 01:52:42 -05003057 wait_event_timeout(waitq,
3058 (lpfc_cmd->pCmd != cmnd),
3059 (2*vport->cfg_devloss_tmo*HZ));
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003060
James Smartfa61a542008-01-11 01:52:42 -05003061 spin_lock_irq(shost->host_lock);
3062 lpfc_cmd->waitq = NULL;
3063 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003064
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003065 if (lpfc_cmd->pCmd == cmnd) {
3066 ret = FAILED;
James Smarte8b62012007-08-02 11:10:09 -04003067 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3068 "0748 abort handler timed out waiting "
3069 "for abort to complete: ret %#x, ID %d, "
3070 "LUN %d, snum %#lx\n",
3071 ret, cmnd->device->id, cmnd->device->lun,
3072 cmnd->serial_number);
dea31012005-04-17 16:05:31 -05003073 }
3074
3075 out:
James Smarte8b62012007-08-02 11:10:09 -04003076 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3077 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
3078 "LUN %d snum %#lx\n", ret, cmnd->device->id,
3079 cmnd->device->lun, cmnd->serial_number);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003080 return ret;
dea31012005-04-17 16:05:31 -05003081}
3082
James Smartbbb9d182009-06-10 17:23:16 -04003083static char *
3084lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
3085{
3086 switch (task_mgmt_cmd) {
3087 case FCP_ABORT_TASK_SET:
3088 return "ABORT_TASK_SET";
3089 case FCP_CLEAR_TASK_SET:
3090 return "FCP_CLEAR_TASK_SET";
3091 case FCP_BUS_RESET:
3092 return "FCP_BUS_RESET";
3093 case FCP_LUN_RESET:
3094 return "FCP_LUN_RESET";
3095 case FCP_TARGET_RESET:
3096 return "FCP_TARGET_RESET";
3097 case FCP_CLEAR_ACA:
3098 return "FCP_CLEAR_ACA";
3099 case FCP_TERMINATE_TASK:
3100 return "FCP_TERMINATE_TASK";
3101 default:
3102 return "unknown";
3103 }
3104}
3105
3106/**
3107 * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
3108 * @vport: The virtual port for which this call is being executed.
3109 * @rdata: Pointer to remote port local data
3110 * @tgt_id: Target ID of remote device.
3111 * @lun_id: Lun number for the TMF
3112 * @task_mgmt_cmd: type of TMF to send
3113 *
3114 * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
3115 * a remote port.
3116 *
3117 * Return Code:
3118 * 0x2003 - Error
3119 * 0x2002 - Success.
3120 **/
3121static int
3122lpfc_send_taskmgmt(struct lpfc_vport *vport, struct lpfc_rport_data *rdata,
3123 unsigned tgt_id, unsigned int lun_id,
3124 uint8_t task_mgmt_cmd)
3125{
3126 struct lpfc_hba *phba = vport->phba;
3127 struct lpfc_scsi_buf *lpfc_cmd;
3128 struct lpfc_iocbq *iocbq;
3129 struct lpfc_iocbq *iocbqrsp;
3130 int ret;
3131 int status;
3132
3133 if (!rdata->pnode || !NLP_CHK_NODE_ACT(rdata->pnode))
3134 return FAILED;
3135
3136 lpfc_cmd = lpfc_get_scsi_buf(phba);
3137 if (lpfc_cmd == NULL)
3138 return FAILED;
3139 lpfc_cmd->timeout = 60;
3140 lpfc_cmd->rdata = rdata;
3141
3142 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
3143 task_mgmt_cmd);
3144 if (!status) {
3145 lpfc_release_scsi_buf(phba, lpfc_cmd);
3146 return FAILED;
3147 }
3148
3149 iocbq = &lpfc_cmd->cur_iocbq;
3150 iocbqrsp = lpfc_sli_get_iocbq(phba);
3151 if (iocbqrsp == NULL) {
3152 lpfc_release_scsi_buf(phba, lpfc_cmd);
3153 return FAILED;
3154 }
3155
3156 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
3157 "0702 Issue %s to TGT %d LUN %d "
3158 "rpi x%x nlp_flag x%x\n",
3159 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
3160 rdata->pnode->nlp_rpi, rdata->pnode->nlp_flag);
3161
3162 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
3163 iocbq, iocbqrsp, lpfc_cmd->timeout);
3164 if (status != IOCB_SUCCESS) {
3165 if (status == IOCB_TIMEDOUT) {
3166 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
3167 ret = TIMEOUT_ERROR;
3168 } else
3169 ret = FAILED;
3170 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
3171 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3172 "0727 TMF %s to TGT %d LUN %d failed (%d, %d)\n",
3173 lpfc_taskmgmt_name(task_mgmt_cmd),
3174 tgt_id, lun_id, iocbqrsp->iocb.ulpStatus,
3175 iocbqrsp->iocb.un.ulpWord[4]);
3176 } else
3177 ret = SUCCESS;
3178
3179 lpfc_sli_release_iocbq(phba, iocbqrsp);
3180
3181 if (ret != TIMEOUT_ERROR)
3182 lpfc_release_scsi_buf(phba, lpfc_cmd);
3183
3184 return ret;
3185}
3186
3187/**
3188 * lpfc_chk_tgt_mapped -
3189 * @vport: The virtual port to check on
3190 * @cmnd: Pointer to scsi_cmnd data structure.
3191 *
3192 * This routine delays until the scsi target (aka rport) for the
3193 * command exists (is present and logged in) or we declare it non-existent.
3194 *
3195 * Return code :
3196 * 0x2003 - Error
3197 * 0x2002 - Success
3198 **/
3199static int
3200lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
3201{
3202 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05003203 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04003204 unsigned long later;
3205
James Smart1c6f4ef52009-11-18 15:40:49 -05003206 if (!rdata) {
3207 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
3208 "0797 Tgt Map rport failure: rdata x%p\n", rdata);
3209 return FAILED;
3210 }
3211 pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04003212 /*
3213 * If target is not in a MAPPED state, delay until
3214 * target is rediscovered or devloss timeout expires.
3215 */
3216 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
3217 while (time_after(later, jiffies)) {
3218 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3219 return FAILED;
3220 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
3221 return SUCCESS;
3222 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
3223 rdata = cmnd->device->hostdata;
3224 if (!rdata)
3225 return FAILED;
3226 pnode = rdata->pnode;
3227 }
3228 if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
3229 (pnode->nlp_state != NLP_STE_MAPPED_NODE))
3230 return FAILED;
3231 return SUCCESS;
3232}
3233
3234/**
3235 * lpfc_reset_flush_io_context -
3236 * @vport: The virtual port (scsi_host) for the flush context
3237 * @tgt_id: If aborting by Target contect - specifies the target id
3238 * @lun_id: If aborting by Lun context - specifies the lun id
3239 * @context: specifies the context level to flush at.
3240 *
3241 * After a reset condition via TMF, we need to flush orphaned i/o
3242 * contexts from the adapter. This routine aborts any contexts
3243 * outstanding, then waits for their completions. The wait is
3244 * bounded by devloss_tmo though.
3245 *
3246 * Return code :
3247 * 0x2003 - Error
3248 * 0x2002 - Success
3249 **/
3250static int
3251lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
3252 uint64_t lun_id, lpfc_ctx_cmd context)
3253{
3254 struct lpfc_hba *phba = vport->phba;
3255 unsigned long later;
3256 int cnt;
3257
3258 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
3259 if (cnt)
3260 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
3261 tgt_id, lun_id, context);
3262 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
3263 while (time_after(later, jiffies) && cnt) {
3264 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
3265 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
3266 }
3267 if (cnt) {
3268 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3269 "0724 I/O flush failure for context %s : cnt x%x\n",
3270 ((context == LPFC_CTX_LUN) ? "LUN" :
3271 ((context == LPFC_CTX_TGT) ? "TGT" :
3272 ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
3273 cnt);
3274 return FAILED;
3275 }
3276 return SUCCESS;
3277}
3278
James Smart9bad7672008-12-04 22:39:02 -05003279/**
James Smart3621a712009-04-06 18:47:14 -04003280 * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
James Smart9bad7672008-12-04 22:39:02 -05003281 * @cmnd: Pointer to scsi_cmnd data structure.
3282 *
James Smartbbb9d182009-06-10 17:23:16 -04003283 * This routine does a device reset by sending a LUN_RESET task management
James Smart9bad7672008-12-04 22:39:02 -05003284 * command.
3285 *
3286 * Return code :
3287 * 0x2003 - Error
James Smart3621a712009-04-06 18:47:14 -04003288 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05003289 **/
dea31012005-04-17 16:05:31 -05003290static int
James Smart7054a602007-04-25 09:52:34 -04003291lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05003292{
James Smart2e0fef82007-06-17 19:56:36 -05003293 struct Scsi_Host *shost = cmnd->device->host;
3294 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05003295 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05003296 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04003297 unsigned tgt_id = cmnd->device->id;
3298 unsigned int lun_id = cmnd->device->lun;
James Smartea2151b2008-09-07 11:52:10 -04003299 struct lpfc_scsi_event_header scsi_event;
James Smartbbb9d182009-06-10 17:23:16 -04003300 int status;
dea31012005-04-17 16:05:31 -05003301
James Smart1c6f4ef52009-11-18 15:40:49 -05003302 if (!rdata) {
3303 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3304 "0798 Device Reset rport failure: rdata x%p\n", rdata);
3305 return FAILED;
3306 }
3307 pnode = rdata->pnode;
Christof Schmitt65d430f2009-10-30 17:59:29 +01003308 fc_block_scsi_eh(cmnd);
James Smartbbb9d182009-06-10 17:23:16 -04003309
3310 status = lpfc_chk_tgt_mapped(vport, cmnd);
3311 if (status == FAILED) {
3312 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3313 "0721 Device Reset rport failure: rdata x%p\n", rdata);
3314 return FAILED;
3315 }
3316
3317 scsi_event.event_type = FC_REG_SCSI_EVENT;
3318 scsi_event.subcategory = LPFC_EVENT_LUNRESET;
3319 scsi_event.lun = lun_id;
3320 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
3321 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
3322
3323 fc_host_post_vendor_event(shost, fc_get_event_number(),
3324 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
3325
3326 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
3327 FCP_LUN_RESET);
3328
3329 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3330 "0713 SCSI layer issued Device Reset (%d, %d) "
3331 "return x%x\n", tgt_id, lun_id, status);
3332
dea31012005-04-17 16:05:31 -05003333 /*
James Smartbbb9d182009-06-10 17:23:16 -04003334 * We have to clean up i/o as : they may be orphaned by the TMF;
3335 * or if the TMF failed, they may be in an indeterminate state.
3336 * So, continue on.
3337 * We will report success if all the i/o aborts successfully.
dea31012005-04-17 16:05:31 -05003338 */
James Smartbbb9d182009-06-10 17:23:16 -04003339 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
3340 LPFC_CTX_LUN);
3341 return status;
3342}
3343
3344/**
3345 * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
3346 * @cmnd: Pointer to scsi_cmnd data structure.
3347 *
3348 * This routine does a target reset by sending a TARGET_RESET task management
3349 * command.
3350 *
3351 * Return code :
3352 * 0x2003 - Error
3353 * 0x2002 - Success
3354 **/
3355static int
3356lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
3357{
3358 struct Scsi_Host *shost = cmnd->device->host;
3359 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3360 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05003361 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04003362 unsigned tgt_id = cmnd->device->id;
3363 unsigned int lun_id = cmnd->device->lun;
3364 struct lpfc_scsi_event_header scsi_event;
3365 int status;
3366
James Smart1c6f4ef52009-11-18 15:40:49 -05003367 if (!rdata) {
3368 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3369 "0799 Target Reset rport failure: rdata x%p\n", rdata);
3370 return FAILED;
3371 }
3372 pnode = rdata->pnode;
Christof Schmitt65d430f2009-10-30 17:59:29 +01003373 fc_block_scsi_eh(cmnd);
James Smartbbb9d182009-06-10 17:23:16 -04003374
3375 status = lpfc_chk_tgt_mapped(vport, cmnd);
3376 if (status == FAILED) {
3377 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3378 "0722 Target Reset rport failure: rdata x%p\n", rdata);
3379 return FAILED;
dea31012005-04-17 16:05:31 -05003380 }
James Smartea2151b2008-09-07 11:52:10 -04003381
3382 scsi_event.event_type = FC_REG_SCSI_EVENT;
3383 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
3384 scsi_event.lun = 0;
3385 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
3386 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
3387
James Smartbbb9d182009-06-10 17:23:16 -04003388 fc_host_post_vendor_event(shost, fc_get_event_number(),
3389 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -04003390
James Smartbbb9d182009-06-10 17:23:16 -04003391 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
3392 FCP_TARGET_RESET);
dea31012005-04-17 16:05:31 -05003393
James Smarte8b62012007-08-02 11:10:09 -04003394 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smartbbb9d182009-06-10 17:23:16 -04003395 "0723 SCSI layer issued Target Reset (%d, %d) "
3396 "return x%x\n", tgt_id, lun_id, status);
3397
3398 /*
3399 * We have to clean up i/o as : they may be orphaned by the TMF;
3400 * or if the TMF failed, they may be in an indeterminate state.
3401 * So, continue on.
3402 * We will report success if all the i/o aborts successfully.
3403 */
3404 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
3405 LPFC_CTX_TGT);
3406 return status;
dea31012005-04-17 16:05:31 -05003407}
3408
James Smart9bad7672008-12-04 22:39:02 -05003409/**
James Smart3621a712009-04-06 18:47:14 -04003410 * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05003411 * @cmnd: Pointer to scsi_cmnd data structure.
3412 *
James Smartbbb9d182009-06-10 17:23:16 -04003413 * This routine does target reset to all targets on @cmnd->device->host.
3414 * This emulates Parallel SCSI Bus Reset Semantics.
James Smart9bad7672008-12-04 22:39:02 -05003415 *
James Smartbbb9d182009-06-10 17:23:16 -04003416 * Return code :
3417 * 0x2003 - Error
3418 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05003419 **/
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04003420static int
James Smart7054a602007-04-25 09:52:34 -04003421lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05003422{
James Smart2e0fef82007-06-17 19:56:36 -05003423 struct Scsi_Host *shost = cmnd->device->host;
3424 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05003425 struct lpfc_nodelist *ndlp = NULL;
James Smartea2151b2008-09-07 11:52:10 -04003426 struct lpfc_scsi_event_header scsi_event;
James Smartbbb9d182009-06-10 17:23:16 -04003427 int match;
3428 int ret = SUCCESS, status, i;
James Smartea2151b2008-09-07 11:52:10 -04003429
3430 scsi_event.event_type = FC_REG_SCSI_EVENT;
3431 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
3432 scsi_event.lun = 0;
3433 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
3434 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
3435
James Smartbbb9d182009-06-10 17:23:16 -04003436 fc_host_post_vendor_event(shost, fc_get_event_number(),
3437 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
dea31012005-04-17 16:05:31 -05003438
Christof Schmitt65d430f2009-10-30 17:59:29 +01003439 fc_block_scsi_eh(cmnd);
James Smartbbb9d182009-06-10 17:23:16 -04003440
dea31012005-04-17 16:05:31 -05003441 /*
3442 * Since the driver manages a single bus device, reset all
3443 * targets known to the driver. Should any target reset
3444 * fail, this routine returns failure to the midlayer.
3445 */
James Smarte17da182006-07-06 15:49:25 -04003446 for (i = 0; i < LPFC_MAX_TARGET; i++) {
James Smart685f0bf2007-04-25 09:53:08 -04003447 /* Search for mapped node by target ID */
dea31012005-04-17 16:05:31 -05003448 match = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003449 spin_lock_irq(shost->host_lock);
3450 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05003451 if (!NLP_CHK_NODE_ACT(ndlp))
3452 continue;
James Smart685f0bf2007-04-25 09:53:08 -04003453 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
James Smart915caaa2008-06-14 22:52:38 -04003454 ndlp->nlp_sid == i &&
James Smart685f0bf2007-04-25 09:53:08 -04003455 ndlp->rport) {
dea31012005-04-17 16:05:31 -05003456 match = 1;
3457 break;
3458 }
3459 }
James Smart2e0fef82007-06-17 19:56:36 -05003460 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003461 if (!match)
3462 continue;
James Smartbbb9d182009-06-10 17:23:16 -04003463
3464 status = lpfc_send_taskmgmt(vport, ndlp->rport->dd_data,
3465 i, 0, FCP_TARGET_RESET);
3466
3467 if (status != SUCCESS) {
James Smarte8b62012007-08-02 11:10:09 -04003468 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3469 "0700 Bus Reset on target %d failed\n",
3470 i);
James Smart915caaa2008-06-14 22:52:38 -04003471 ret = FAILED;
dea31012005-04-17 16:05:31 -05003472 }
3473 }
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05003474 /*
James Smartbbb9d182009-06-10 17:23:16 -04003475 * We have to clean up i/o as : they may be orphaned by the TMFs
3476 * above; or if any of the TMFs failed, they may be in an
3477 * indeterminate state.
3478 * We will report success if all the i/o aborts successfully.
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05003479 */
James Smartbbb9d182009-06-10 17:23:16 -04003480
3481 status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
3482 if (status != SUCCESS)
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05003483 ret = FAILED;
James Smartbbb9d182009-06-10 17:23:16 -04003484
James Smarte8b62012007-08-02 11:10:09 -04003485 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3486 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea31012005-04-17 16:05:31 -05003487 return ret;
3488}
3489
James Smart9bad7672008-12-04 22:39:02 -05003490/**
James Smart3621a712009-04-06 18:47:14 -04003491 * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
James Smart9bad7672008-12-04 22:39:02 -05003492 * @sdev: Pointer to scsi_device.
3493 *
3494 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
3495 * globally available list of scsi buffers. This routine also makes sure scsi
3496 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
3497 * of scsi buffer exists for the lifetime of the driver.
3498 *
3499 * Return codes:
3500 * non-0 - Error
3501 * 0 - Success
3502 **/
dea31012005-04-17 16:05:31 -05003503static int
dea31012005-04-17 16:05:31 -05003504lpfc_slave_alloc(struct scsi_device *sdev)
3505{
James Smart2e0fef82007-06-17 19:56:36 -05003506 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
3507 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003508 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
James Smart3772a992009-05-22 14:50:54 -04003509 uint32_t total = 0;
dea31012005-04-17 16:05:31 -05003510 uint32_t num_to_alloc = 0;
James Smart3772a992009-05-22 14:50:54 -04003511 int num_allocated = 0;
dea31012005-04-17 16:05:31 -05003512
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003513 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05003514 return -ENXIO;
3515
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003516 sdev->hostdata = rport->dd_data;
dea31012005-04-17 16:05:31 -05003517
3518 /*
3519 * Populate the cmds_per_lun count scsi_bufs into this host's globally
3520 * available list of scsi buffers. Don't allocate more than the
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04003521 * HBA limit conveyed to the midlayer via the host structure. The
3522 * formula accounts for the lun_queue_depth + error handlers + 1
3523 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea31012005-04-17 16:05:31 -05003524 */
3525 total = phba->total_scsi_bufs;
James Smart3de2a652007-08-02 11:09:59 -04003526 num_to_alloc = vport->cfg_lun_queue_depth + 2;
James Smart92d7f7b2007-06-17 19:56:38 -05003527
3528 /* Allow some exchanges to be available always to complete discovery */
3529 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04003530 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3531 "0704 At limitation of %d preallocated "
3532 "command buffers\n", total);
dea31012005-04-17 16:05:31 -05003533 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05003534 /* Allow some exchanges to be available always to complete discovery */
3535 } else if (total + num_to_alloc >
3536 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04003537 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3538 "0705 Allocation request of %d "
3539 "command buffers will exceed max of %d. "
3540 "Reducing allocation request to %d.\n",
3541 num_to_alloc, phba->cfg_hba_queue_depth,
3542 (phba->cfg_hba_queue_depth - total));
dea31012005-04-17 16:05:31 -05003543 num_to_alloc = phba->cfg_hba_queue_depth - total;
3544 }
James Smart3772a992009-05-22 14:50:54 -04003545 num_allocated = lpfc_new_scsi_buf(vport, num_to_alloc);
3546 if (num_to_alloc != num_allocated) {
3547 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3548 "0708 Allocation request of %d "
3549 "command buffers did not succeed. "
3550 "Allocated %d buffers.\n",
3551 num_to_alloc, num_allocated);
dea31012005-04-17 16:05:31 -05003552 }
James Smart1c6f4ef52009-11-18 15:40:49 -05003553 if (num_allocated > 0)
3554 phba->total_scsi_bufs += num_allocated;
dea31012005-04-17 16:05:31 -05003555 return 0;
3556}
3557
James Smart9bad7672008-12-04 22:39:02 -05003558/**
James Smart3621a712009-04-06 18:47:14 -04003559 * lpfc_slave_configure - scsi_host_template slave_configure entry point
James Smart9bad7672008-12-04 22:39:02 -05003560 * @sdev: Pointer to scsi_device.
3561 *
3562 * This routine configures following items
3563 * - Tag command queuing support for @sdev if supported.
3564 * - Dev loss time out value of fc_rport.
3565 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
3566 *
3567 * Return codes:
3568 * 0 - Success
3569 **/
dea31012005-04-17 16:05:31 -05003570static int
3571lpfc_slave_configure(struct scsi_device *sdev)
3572{
James Smart2e0fef82007-06-17 19:56:36 -05003573 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
3574 struct lpfc_hba *phba = vport->phba;
3575 struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
dea31012005-04-17 16:05:31 -05003576
3577 if (sdev->tagged_supported)
James Smart3de2a652007-08-02 11:09:59 -04003578 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05003579 else
James Smart3de2a652007-08-02 11:09:59 -04003580 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05003581
3582 /*
3583 * Initialize the fc transport attributes for the target
3584 * containing this scsi device. Also note that the driver's
3585 * target pointer is stored in the starget_data for the
3586 * driver's sysfs entry point functions.
3587 */
James Smart3de2a652007-08-02 11:09:59 -04003588 rport->dev_loss_tmo = vport->cfg_devloss_tmo;
dea31012005-04-17 16:05:31 -05003589
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003590 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04003591 lpfc_sli_handle_fast_ring_event(phba,
3592 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003593 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
3594 lpfc_poll_rearm_timer(phba);
3595 }
3596
dea31012005-04-17 16:05:31 -05003597 return 0;
3598}
3599
James Smart9bad7672008-12-04 22:39:02 -05003600/**
James Smart3621a712009-04-06 18:47:14 -04003601 * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
James Smart9bad7672008-12-04 22:39:02 -05003602 * @sdev: Pointer to scsi_device.
3603 *
3604 * This routine sets @sdev hostatdata filed to null.
3605 **/
dea31012005-04-17 16:05:31 -05003606static void
3607lpfc_slave_destroy(struct scsi_device *sdev)
3608{
3609 sdev->hostdata = NULL;
3610 return;
3611}
3612
James Smart92d7f7b2007-06-17 19:56:38 -05003613
dea31012005-04-17 16:05:31 -05003614struct scsi_host_template lpfc_template = {
3615 .module = THIS_MODULE,
3616 .name = LPFC_DRIVER_NAME,
3617 .info = lpfc_info,
3618 .queuecommand = lpfc_queuecommand,
3619 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04003620 .eh_device_reset_handler = lpfc_device_reset_handler,
3621 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart7054a602007-04-25 09:52:34 -04003622 .eh_bus_reset_handler = lpfc_bus_reset_handler,
dea31012005-04-17 16:05:31 -05003623 .slave_alloc = lpfc_slave_alloc,
3624 .slave_configure = lpfc_slave_configure,
3625 .slave_destroy = lpfc_slave_destroy,
James Smart47a86172007-04-25 09:53:22 -04003626 .scan_finished = lpfc_scan_finished,
dea31012005-04-17 16:05:31 -05003627 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05003628 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
dea31012005-04-17 16:05:31 -05003629 .cmd_per_lun = LPFC_CMD_PER_LUN,
3630 .use_clustering = ENABLE_CLUSTERING,
James Smart2e0fef82007-06-17 19:56:36 -05003631 .shost_attrs = lpfc_hba_attrs,
James.Smart@Emulex.Com564b2962005-06-25 10:34:17 -04003632 .max_sectors = 0xFFFF,
James Smartf1c3b0f2009-07-19 10:01:32 -04003633 .vendor_id = LPFC_NL_VENDOR_ID,
James Smart5ffc2662009-11-18 15:39:44 -05003634 .change_queue_depth = lpfc_change_queue_depth,
dea31012005-04-17 16:05:31 -05003635};
James Smart3de2a652007-08-02 11:09:59 -04003636
3637struct scsi_host_template lpfc_vport_template = {
3638 .module = THIS_MODULE,
3639 .name = LPFC_DRIVER_NAME,
3640 .info = lpfc_info,
3641 .queuecommand = lpfc_queuecommand,
3642 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04003643 .eh_device_reset_handler = lpfc_device_reset_handler,
3644 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart3de2a652007-08-02 11:09:59 -04003645 .eh_bus_reset_handler = lpfc_bus_reset_handler,
3646 .slave_alloc = lpfc_slave_alloc,
3647 .slave_configure = lpfc_slave_configure,
3648 .slave_destroy = lpfc_slave_destroy,
3649 .scan_finished = lpfc_scan_finished,
3650 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05003651 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
James Smart3de2a652007-08-02 11:09:59 -04003652 .cmd_per_lun = LPFC_CMD_PER_LUN,
3653 .use_clustering = ENABLE_CLUSTERING,
3654 .shost_attrs = lpfc_vport_attrs,
3655 .max_sectors = 0xFFFF,
James Smart5ffc2662009-11-18 15:39:44 -05003656 .change_queue_depth = lpfc_change_queue_depth,
James Smart3de2a652007-08-02 11:09:59 -04003657};