blob: 889a7b9ec92bce84e715dae9f56c7ee79ded4a1a [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;
623
624 spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock, iflag);
625 list_for_each_entry_safe(psb, next_psb,
626 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
627 if (psb->cur_iocbq.sli4_xritag == xri) {
628 list_del(&psb->list);
James Smart341af102010-01-26 23:07:37 -0500629 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400630 psb->status = IOSTAT_SUCCESS;
631 spin_unlock_irqrestore(
632 &phba->sli4_hba.abts_scsi_buf_list_lock,
633 iflag);
634 lpfc_release_scsi_buf_s4(phba, psb);
635 return;
636 }
637 }
638 spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
639 iflag);
640}
641
642/**
643 * lpfc_sli4_repost_scsi_sgl_list - Repsot the Scsi buffers sgl pages as block
644 * @phba: pointer to lpfc hba data structure.
645 *
646 * This routine walks the list of scsi buffers that have been allocated and
647 * repost them to the HBA by using SGL block post. This is needed after a
648 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
649 * is responsible for moving all scsi buffers on the lpfc_abts_scsi_sgl_list
650 * to the lpfc_scsi_buf_list. If the repost fails, reject all scsi buffers.
651 *
652 * Returns: 0 = success, non-zero failure.
653 **/
654int
655lpfc_sli4_repost_scsi_sgl_list(struct lpfc_hba *phba)
656{
657 struct lpfc_scsi_buf *psb;
658 int index, status, bcnt = 0, rcnt = 0, rc = 0;
659 LIST_HEAD(sblist);
660
661 for (index = 0; index < phba->sli4_hba.scsi_xri_cnt; index++) {
662 psb = phba->sli4_hba.lpfc_scsi_psb_array[index];
663 if (psb) {
664 /* Remove from SCSI buffer list */
665 list_del(&psb->list);
666 /* Add it to a local SCSI buffer list */
667 list_add_tail(&psb->list, &sblist);
668 if (++rcnt == LPFC_NEMBED_MBOX_SGL_CNT) {
669 bcnt = rcnt;
670 rcnt = 0;
671 }
672 } else
673 /* A hole present in the XRI array, need to skip */
674 bcnt = rcnt;
675
676 if (index == phba->sli4_hba.scsi_xri_cnt - 1)
677 /* End of XRI array for SCSI buffer, complete */
678 bcnt = rcnt;
679
680 /* Continue until collect up to a nembed page worth of sgls */
681 if (bcnt == 0)
682 continue;
683 /* Now, post the SCSI buffer list sgls as a block */
684 status = lpfc_sli4_post_scsi_sgl_block(phba, &sblist, bcnt);
685 /* Reset SCSI buffer count for next round of posting */
686 bcnt = 0;
687 while (!list_empty(&sblist)) {
688 list_remove_head(&sblist, psb, struct lpfc_scsi_buf,
689 list);
690 if (status) {
691 /* Put this back on the abort scsi list */
James Smart341af102010-01-26 23:07:37 -0500692 psb->exch_busy = 1;
James Smartda0436e2009-05-22 14:51:39 -0400693 rc++;
James Smart341af102010-01-26 23:07:37 -0500694 } else {
695 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400696 psb->status = IOSTAT_SUCCESS;
James Smart341af102010-01-26 23:07:37 -0500697 }
James Smartda0436e2009-05-22 14:51:39 -0400698 /* Put it back into the SCSI buffer list */
699 lpfc_release_scsi_buf_s4(phba, psb);
700 }
701 }
702 return rc;
703}
704
705/**
706 * lpfc_new_scsi_buf_s4 - Scsi buffer allocator for HBA with SLI4 IF spec
707 * @vport: The virtual port for which this call being executed.
708 * @num_to_allocate: The requested number of buffers to allocate.
709 *
710 * This routine allocates a scsi buffer for device with SLI-4 interface spec,
711 * the scsi buffer contains all the necessary information needed to initiate
712 * a SCSI I/O.
713 *
714 * Return codes:
715 * int - number of scsi buffers that were allocated.
716 * 0 = failure, less than num_to_alloc is a partial failure.
717 **/
718static int
719lpfc_new_scsi_buf_s4(struct lpfc_vport *vport, int num_to_alloc)
720{
721 struct lpfc_hba *phba = vport->phba;
722 struct lpfc_scsi_buf *psb;
723 struct sli4_sge *sgl;
724 IOCB_t *iocb;
725 dma_addr_t pdma_phys_fcp_cmd;
726 dma_addr_t pdma_phys_fcp_rsp;
727 dma_addr_t pdma_phys_bpl, pdma_phys_bpl1;
728 uint16_t iotag, last_xritag = NO_XRI;
729 int status = 0, index;
730 int bcnt;
731 int non_sequential_xri = 0;
732 int rc = 0;
733 LIST_HEAD(sblist);
734
735 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
736 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
737 if (!psb)
738 break;
739
740 /*
741 * Get memory from the pci pool to map the virt space to pci bus
742 * space for an I/O. The DMA buffer includes space for the
743 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
744 * necessary to support the sg_tablesize.
745 */
746 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
747 GFP_KERNEL, &psb->dma_handle);
748 if (!psb->data) {
749 kfree(psb);
750 break;
751 }
752
753 /* Initialize virtual ptrs to dma_buf region. */
754 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
755
756 /* Allocate iotag for psb->cur_iocbq. */
757 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
758 if (iotag == 0) {
759 kfree(psb);
760 break;
761 }
762
763 psb->cur_iocbq.sli4_xritag = lpfc_sli4_next_xritag(phba);
764 if (psb->cur_iocbq.sli4_xritag == NO_XRI) {
765 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
766 psb->data, psb->dma_handle);
767 kfree(psb);
768 break;
769 }
770 if (last_xritag != NO_XRI
771 && psb->cur_iocbq.sli4_xritag != (last_xritag+1)) {
772 non_sequential_xri = 1;
773 } else
774 list_add_tail(&psb->list, &sblist);
775 last_xritag = psb->cur_iocbq.sli4_xritag;
776
777 index = phba->sli4_hba.scsi_xri_cnt++;
778 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
779
780 psb->fcp_bpl = psb->data;
781 psb->fcp_cmnd = (psb->data + phba->cfg_sg_dma_buf_size)
782 - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
783 psb->fcp_rsp = (struct fcp_rsp *)((uint8_t *)psb->fcp_cmnd +
784 sizeof(struct fcp_cmnd));
785
786 /* Initialize local short-hand pointers. */
787 sgl = (struct sli4_sge *)psb->fcp_bpl;
788 pdma_phys_bpl = psb->dma_handle;
789 pdma_phys_fcp_cmd =
790 (psb->dma_handle + phba->cfg_sg_dma_buf_size)
791 - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
792 pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
793
794 /*
795 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
796 * are sg list bdes. Initialize the first two and leave the
797 * rest for queuecommand.
798 */
799 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
800 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
James Smartda0436e2009-05-22 14:51:39 -0400801 bf_set(lpfc_sli4_sge_last, sgl, 0);
802 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -0500803 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_cmnd));
James Smartda0436e2009-05-22 14:51:39 -0400804 sgl++;
805
806 /* Setup the physical region for the FCP RSP */
807 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
808 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
James Smartda0436e2009-05-22 14:51:39 -0400809 bf_set(lpfc_sli4_sge_last, sgl, 1);
810 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -0500811 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
James Smartda0436e2009-05-22 14:51:39 -0400812
813 /*
814 * Since the IOCB for the FCP I/O is built into this
815 * lpfc_scsi_buf, initialize it with all known data now.
816 */
817 iocb = &psb->cur_iocbq.iocb;
818 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
819 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
820 /* setting the BLP size to 2 * sizeof BDE may not be correct.
821 * We are setting the bpl to point to out sgl. An sgl's
822 * entries are 16 bytes, a bpl entries are 12 bytes.
823 */
824 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
825 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
826 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
827 iocb->ulpBdeCount = 1;
828 iocb->ulpLe = 1;
829 iocb->ulpClass = CLASS3;
830 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
831 pdma_phys_bpl1 = pdma_phys_bpl + SGL_PAGE_SIZE;
832 else
833 pdma_phys_bpl1 = 0;
834 psb->dma_phys_bpl = pdma_phys_bpl;
835 phba->sli4_hba.lpfc_scsi_psb_array[index] = psb;
836 if (non_sequential_xri) {
837 status = lpfc_sli4_post_sgl(phba, pdma_phys_bpl,
838 pdma_phys_bpl1,
839 psb->cur_iocbq.sli4_xritag);
840 if (status) {
841 /* Put this back on the abort scsi list */
James Smart341af102010-01-26 23:07:37 -0500842 psb->exch_busy = 1;
James Smartda0436e2009-05-22 14:51:39 -0400843 rc++;
James Smart341af102010-01-26 23:07:37 -0500844 } else {
845 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400846 psb->status = IOSTAT_SUCCESS;
James Smart341af102010-01-26 23:07:37 -0500847 }
James Smartda0436e2009-05-22 14:51:39 -0400848 /* Put it back into the SCSI buffer list */
849 lpfc_release_scsi_buf_s4(phba, psb);
850 break;
851 }
852 }
853 if (bcnt) {
854 status = lpfc_sli4_post_scsi_sgl_block(phba, &sblist, bcnt);
855 /* Reset SCSI buffer count for next round of posting */
856 while (!list_empty(&sblist)) {
857 list_remove_head(&sblist, psb, struct lpfc_scsi_buf,
858 list);
859 if (status) {
860 /* Put this back on the abort scsi list */
James Smart341af102010-01-26 23:07:37 -0500861 psb->exch_busy = 1;
James Smartda0436e2009-05-22 14:51:39 -0400862 rc++;
James Smart341af102010-01-26 23:07:37 -0500863 } else {
864 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400865 psb->status = IOSTAT_SUCCESS;
James Smart341af102010-01-26 23:07:37 -0500866 }
James Smartda0436e2009-05-22 14:51:39 -0400867 /* Put it back into the SCSI buffer list */
868 lpfc_release_scsi_buf_s4(phba, psb);
869 }
870 }
871
872 return bcnt + non_sequential_xri - rc;
873}
874
875/**
James Smart3772a992009-05-22 14:50:54 -0400876 * lpfc_new_scsi_buf - Wrapper funciton for scsi buffer allocator
877 * @vport: The virtual port for which this call being executed.
878 * @num_to_allocate: The requested number of buffers to allocate.
879 *
880 * This routine wraps the actual SCSI buffer allocator function pointer from
881 * the lpfc_hba struct.
882 *
883 * Return codes:
884 * int - number of scsi buffers that were allocated.
885 * 0 = failure, less than num_to_alloc is a partial failure.
886 **/
887static inline int
888lpfc_new_scsi_buf(struct lpfc_vport *vport, int num_to_alloc)
889{
890 return vport->phba->lpfc_new_scsi_buf(vport, num_to_alloc);
891}
892
893/**
894 * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
895 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -0500896 *
897 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
898 * and returns to caller.
899 *
900 * Return codes:
901 * NULL - Error
902 * Pointer to lpfc_scsi_buf - Success
903 **/
Adrian Bunk455c53e2006-01-06 20:21:28 +0100904static struct lpfc_scsi_buf*
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500905lpfc_get_scsi_buf(struct lpfc_hba * phba)
dea31012005-04-17 16:05:31 -0500906{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400907 struct lpfc_scsi_buf * lpfc_cmd = NULL;
908 struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500909 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500910
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500911 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400912 list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
James Smart1dcb58e2007-04-25 09:51:30 -0400913 if (lpfc_cmd) {
914 lpfc_cmd->seg_cnt = 0;
915 lpfc_cmd->nonsg_phys = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -0500916 lpfc_cmd->prot_seg_cnt = 0;
James Smart1dcb58e2007-04-25 09:51:30 -0400917 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500918 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400919 return lpfc_cmd;
920}
921
James Smart9bad7672008-12-04 22:39:02 -0500922/**
James Smart3772a992009-05-22 14:50:54 -0400923 * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
James Smart9bad7672008-12-04 22:39:02 -0500924 * @phba: The Hba for which this call is being executed.
925 * @psb: The scsi buffer which is being released.
926 *
927 * This routine releases @psb scsi buffer by adding it to tail of @phba
928 * lpfc_scsi_buf_list list.
929 **/
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400930static void
James Smart3772a992009-05-22 14:50:54 -0400931lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400932{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500933 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500934
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500935 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400936 psb->pCmd = NULL;
dea31012005-04-17 16:05:31 -0500937 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500938 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
dea31012005-04-17 16:05:31 -0500939}
940
James Smart9bad7672008-12-04 22:39:02 -0500941/**
James Smartda0436e2009-05-22 14:51:39 -0400942 * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
943 * @phba: The Hba for which this call is being executed.
944 * @psb: The scsi buffer which is being released.
945 *
946 * This routine releases @psb scsi buffer by adding it to tail of @phba
947 * lpfc_scsi_buf_list list. For SLI4 XRI's are tied to the scsi buffer
948 * and cannot be reused for at least RA_TOV amount of time if it was
949 * aborted.
950 **/
951static void
952lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
953{
954 unsigned long iflag = 0;
955
James Smart341af102010-01-26 23:07:37 -0500956 if (psb->exch_busy) {
James Smartda0436e2009-05-22 14:51:39 -0400957 spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock,
958 iflag);
959 psb->pCmd = NULL;
960 list_add_tail(&psb->list,
961 &phba->sli4_hba.lpfc_abts_scsi_buf_list);
962 spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
963 iflag);
964 } else {
965
966 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
967 psb->pCmd = NULL;
968 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
969 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
970 }
971}
972
973/**
James Smart3772a992009-05-22 14:50:54 -0400974 * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
975 * @phba: The Hba for which this call is being executed.
976 * @psb: The scsi buffer which is being released.
977 *
978 * This routine releases @psb scsi buffer by adding it to tail of @phba
979 * lpfc_scsi_buf_list list.
980 **/
981static void
982lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
983{
984
985 phba->lpfc_release_scsi_buf(phba, psb);
986}
987
988/**
989 * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -0500990 * @phba: The Hba for which this call is being executed.
991 * @lpfc_cmd: The scsi buffer which is going to be mapped.
992 *
993 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
James Smart3772a992009-05-22 14:50:54 -0400994 * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
995 * through sg elements and format the bdea. This routine also initializes all
996 * IOCB fields which are dependent on scsi command request buffer.
James Smart9bad7672008-12-04 22:39:02 -0500997 *
998 * Return codes:
999 * 1 - Error
1000 * 0 - Success
1001 **/
dea31012005-04-17 16:05:31 -05001002static int
James Smart3772a992009-05-22 14:50:54 -04001003lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
dea31012005-04-17 16:05:31 -05001004{
1005 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1006 struct scatterlist *sgel = NULL;
1007 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1008 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
1009 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart34b02dc2008-08-24 21:49:55 -04001010 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
dea31012005-04-17 16:05:31 -05001011 dma_addr_t physaddr;
James Smart34b02dc2008-08-24 21:49:55 -04001012 uint32_t num_bde = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001013 int nseg, datadir = scsi_cmnd->sc_data_direction;
dea31012005-04-17 16:05:31 -05001014
1015 /*
1016 * There are three possibilities here - use scatter-gather segment, use
1017 * the single mapping, or neither. Start the lpfc command prep by
1018 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1019 * data bde entry.
1020 */
1021 bpl += 2;
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001022 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05001023 /*
1024 * The driver stores the segment count returned from pci_map_sg
1025 * because this a count of dma-mappings used to map the use_sg
1026 * pages. They are not guaranteed to be the same for those
1027 * architectures that implement an IOMMU.
1028 */
dea31012005-04-17 16:05:31 -05001029
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001030 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
1031 scsi_sg_count(scsi_cmnd), datadir);
1032 if (unlikely(!nseg))
1033 return 1;
1034
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001035 lpfc_cmd->seg_cnt = nseg;
dea31012005-04-17 16:05:31 -05001036 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001037 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1038 "9064 BLKGRD: %s: Too many sg segments from "
James Smarte2a0a9d2008-12-04 22:40:02 -05001039 "dma_map_sg. Config %d, seg_cnt %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001040 __func__, phba->cfg_sg_seg_cnt,
dea31012005-04-17 16:05:31 -05001041 lpfc_cmd->seg_cnt);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001042 scsi_dma_unmap(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001043 return 1;
1044 }
1045
1046 /*
1047 * The driver established a maximum scatter-gather segment count
1048 * during probe that limits the number of sg elements in any
1049 * single scsi command. Just run through the seg_cnt and format
1050 * the bde's.
James Smart34b02dc2008-08-24 21:49:55 -04001051 * When using SLI-3 the driver will try to fit all the BDEs into
1052 * the IOCB. If it can't then the BDEs get added to a BPL as it
1053 * does for SLI-2 mode.
dea31012005-04-17 16:05:31 -05001054 */
James Smart34b02dc2008-08-24 21:49:55 -04001055 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
dea31012005-04-17 16:05:31 -05001056 physaddr = sg_dma_address(sgel);
James Smart34b02dc2008-08-24 21:49:55 -04001057 if (phba->sli_rev == 3 &&
James Smarte2a0a9d2008-12-04 22:40:02 -05001058 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
James Smart34b02dc2008-08-24 21:49:55 -04001059 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
1060 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1061 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
1062 data_bde->addrLow = putPaddrLow(physaddr);
1063 data_bde->addrHigh = putPaddrHigh(physaddr);
1064 data_bde++;
1065 } else {
1066 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1067 bpl->tus.f.bdeSize = sg_dma_len(sgel);
1068 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1069 bpl->addrLow =
1070 le32_to_cpu(putPaddrLow(physaddr));
1071 bpl->addrHigh =
1072 le32_to_cpu(putPaddrHigh(physaddr));
1073 bpl++;
1074 }
dea31012005-04-17 16:05:31 -05001075 }
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001076 }
dea31012005-04-17 16:05:31 -05001077
1078 /*
1079 * Finish initializing those IOCB fields that are dependent on the
James Smart34b02dc2008-08-24 21:49:55 -04001080 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
1081 * explicitly reinitialized and for SLI-3 the extended bde count is
1082 * explicitly reinitialized since all iocb memory resources are reused.
dea31012005-04-17 16:05:31 -05001083 */
James Smarte2a0a9d2008-12-04 22:40:02 -05001084 if (phba->sli_rev == 3 &&
1085 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
James Smart34b02dc2008-08-24 21:49:55 -04001086 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
1087 /*
1088 * The extended IOCB format can only fit 3 BDE or a BPL.
1089 * This I/O has more than 3 BDE so the 1st data bde will
1090 * be a BPL that is filled in here.
1091 */
1092 physaddr = lpfc_cmd->dma_handle;
1093 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
1094 data_bde->tus.f.bdeSize = (num_bde *
1095 sizeof(struct ulp_bde64));
1096 physaddr += (sizeof(struct fcp_cmnd) +
1097 sizeof(struct fcp_rsp) +
1098 (2 * sizeof(struct ulp_bde64)));
1099 data_bde->addrHigh = putPaddrHigh(physaddr);
1100 data_bde->addrLow = putPaddrLow(physaddr);
1101 /* ebde count includes the responce bde and data bpl */
1102 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
1103 } else {
1104 /* ebde count includes the responce bde and data bdes */
1105 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1106 }
1107 } else {
1108 iocb_cmd->un.fcpi64.bdl.bdeSize =
1109 ((num_bde + 2) * sizeof(struct ulp_bde64));
1110 }
James Smart09372822008-01-11 01:52:54 -05001111 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
James Smarte2a0a9d2008-12-04 22:40:02 -05001112
1113 /*
1114 * Due to difference in data length between DIF/non-DIF paths,
1115 * we need to set word 4 of IOCB here
1116 */
James Smarta257bf92009-04-06 18:48:10 -04001117 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001118 return 0;
1119}
1120
James Smarte2a0a9d2008-12-04 22:40:02 -05001121/*
1122 * Given a scsi cmnd, determine the BlockGuard profile to be used
1123 * with the cmd
1124 */
1125static int
James Smart6a9c52c2009-10-02 15:16:51 -04001126lpfc_sc_to_sli_prof(struct lpfc_hba *phba, struct scsi_cmnd *sc)
James Smarte2a0a9d2008-12-04 22:40:02 -05001127{
1128 uint8_t guard_type = scsi_host_get_guard(sc->device->host);
1129 uint8_t ret_prof = LPFC_PROF_INVALID;
1130
1131 if (guard_type == SHOST_DIX_GUARD_IP) {
1132 switch (scsi_get_prot_op(sc)) {
1133 case SCSI_PROT_READ_INSERT:
1134 case SCSI_PROT_WRITE_STRIP:
1135 ret_prof = LPFC_PROF_AST2;
1136 break;
1137
1138 case SCSI_PROT_READ_STRIP:
1139 case SCSI_PROT_WRITE_INSERT:
1140 ret_prof = LPFC_PROF_A1;
1141 break;
1142
Martin K. Petersenc6af4042009-09-18 17:32:59 -04001143 case SCSI_PROT_READ_PASS:
1144 case SCSI_PROT_WRITE_PASS:
James Smarte2a0a9d2008-12-04 22:40:02 -05001145 ret_prof = LPFC_PROF_AST1;
1146 break;
1147
James Smarte2a0a9d2008-12-04 22:40:02 -05001148 case SCSI_PROT_NORMAL:
1149 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001150 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1151 "9063 BLKGRD:Bad op/guard:%d/%d combination\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001152 scsi_get_prot_op(sc), guard_type);
1153 break;
1154
1155 }
1156 } else if (guard_type == SHOST_DIX_GUARD_CRC) {
1157 switch (scsi_get_prot_op(sc)) {
1158 case SCSI_PROT_READ_STRIP:
1159 case SCSI_PROT_WRITE_INSERT:
1160 ret_prof = LPFC_PROF_A1;
1161 break;
1162
1163 case SCSI_PROT_READ_PASS:
1164 case SCSI_PROT_WRITE_PASS:
1165 ret_prof = LPFC_PROF_C1;
1166 break;
1167
James Smarte2a0a9d2008-12-04 22:40:02 -05001168 case SCSI_PROT_READ_INSERT:
1169 case SCSI_PROT_WRITE_STRIP:
1170 case SCSI_PROT_NORMAL:
1171 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001172 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1173 "9075 BLKGRD: Bad op/guard:%d/%d combination\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001174 scsi_get_prot_op(sc), guard_type);
1175 break;
1176 }
1177 } else {
1178 /* unsupported format */
1179 BUG();
1180 }
1181
1182 return ret_prof;
1183}
1184
1185struct scsi_dif_tuple {
1186 __be16 guard_tag; /* Checksum */
1187 __be16 app_tag; /* Opaque storage */
1188 __be32 ref_tag; /* Target LBA or indirect LBA */
1189};
1190
1191static inline unsigned
1192lpfc_cmd_blksize(struct scsi_cmnd *sc)
1193{
1194 return sc->device->sector_size;
1195}
1196
1197/**
1198 * lpfc_get_cmd_dif_parms - Extract DIF parameters from SCSI command
1199 * @sc: in: SCSI command
James Smart3621a712009-04-06 18:47:14 -04001200 * @apptagmask: out: app tag mask
1201 * @apptagval: out: app tag value
1202 * @reftag: out: ref tag (reference tag)
James Smarte2a0a9d2008-12-04 22:40:02 -05001203 *
1204 * Description:
Martin Olsson98a17082009-04-22 18:21:29 +02001205 * Extract DIF parameters from the command if possible. Otherwise,
1206 * use default parameters.
James Smarte2a0a9d2008-12-04 22:40:02 -05001207 *
1208 **/
1209static inline void
1210lpfc_get_cmd_dif_parms(struct scsi_cmnd *sc, uint16_t *apptagmask,
1211 uint16_t *apptagval, uint32_t *reftag)
1212{
1213 struct scsi_dif_tuple *spt;
1214 unsigned char op = scsi_get_prot_op(sc);
1215 unsigned int protcnt = scsi_prot_sg_count(sc);
1216 static int cnt;
1217
1218 if (protcnt && (op == SCSI_PROT_WRITE_STRIP ||
Martin K. Petersenc6af4042009-09-18 17:32:59 -04001219 op == SCSI_PROT_WRITE_PASS)) {
James Smarte2a0a9d2008-12-04 22:40:02 -05001220
1221 cnt++;
1222 spt = page_address(sg_page(scsi_prot_sglist(sc))) +
1223 scsi_prot_sglist(sc)[0].offset;
1224 *apptagmask = 0;
1225 *apptagval = 0;
1226 *reftag = cpu_to_be32(spt->ref_tag);
1227
1228 } else {
1229 /* SBC defines ref tag to be lower 32bits of LBA */
1230 *reftag = (uint32_t) (0xffffffff & scsi_get_lba(sc));
1231 *apptagmask = 0;
1232 *apptagval = 0;
1233 }
1234}
1235
1236/*
1237 * This function sets up buffer list for protection groups of
1238 * type LPFC_PG_TYPE_NO_DIF
1239 *
1240 * This is usually used when the HBA is instructed to generate
1241 * DIFs and insert them into data stream (or strip DIF from
1242 * incoming data stream)
1243 *
1244 * The buffer list consists of just one protection group described
1245 * below:
1246 * +-------------------------+
1247 * start of prot group --> | PDE_1 |
1248 * +-------------------------+
1249 * | Data BDE |
1250 * +-------------------------+
1251 * |more Data BDE's ... (opt)|
1252 * +-------------------------+
1253 *
1254 * @sc: pointer to scsi command we're working on
1255 * @bpl: pointer to buffer list for protection groups
1256 * @datacnt: number of segments of data that have been dma mapped
1257 *
1258 * Note: Data s/g buffers have been dma mapped
1259 */
1260static int
1261lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1262 struct ulp_bde64 *bpl, int datasegcnt)
1263{
1264 struct scatterlist *sgde = NULL; /* s/g data entry */
1265 struct lpfc_pde *pde1 = NULL;
1266 dma_addr_t physaddr;
1267 int i = 0, num_bde = 0;
1268 int datadir = sc->sc_data_direction;
1269 int prof = LPFC_PROF_INVALID;
1270 unsigned blksize;
1271 uint32_t reftag;
1272 uint16_t apptagmask, apptagval;
1273
1274 pde1 = (struct lpfc_pde *) bpl;
James Smart6a9c52c2009-10-02 15:16:51 -04001275 prof = lpfc_sc_to_sli_prof(phba, sc);
James Smarte2a0a9d2008-12-04 22:40:02 -05001276
1277 if (prof == LPFC_PROF_INVALID)
1278 goto out;
1279
1280 /* extract some info from the scsi command for PDE1*/
1281 blksize = lpfc_cmd_blksize(sc);
1282 lpfc_get_cmd_dif_parms(sc, &apptagmask, &apptagval, &reftag);
1283
1284 /* setup PDE1 with what we have */
1285 lpfc_pde_set_bg_parms(pde1, LPFC_PDE1_DESCRIPTOR, prof, blksize,
1286 BG_EC_STOP_ERR);
1287 lpfc_pde_set_dif_parms(pde1, apptagmask, apptagval, reftag);
1288
1289 num_bde++;
1290 bpl++;
1291
1292 /* assumption: caller has already run dma_map_sg on command data */
1293 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
1294 physaddr = sg_dma_address(sgde);
1295 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
1296 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1297 bpl->tus.f.bdeSize = sg_dma_len(sgde);
1298 if (datadir == DMA_TO_DEVICE)
1299 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1300 else
1301 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1302 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1303 bpl++;
1304 num_bde++;
1305 }
1306
1307out:
1308 return num_bde;
1309}
1310
1311/*
1312 * This function sets up buffer list for protection groups of
1313 * type LPFC_PG_TYPE_DIF_BUF
1314 *
1315 * This is usually used when DIFs are in their own buffers,
1316 * separate from the data. The HBA can then by instructed
1317 * to place the DIFs in the outgoing stream. For read operations,
1318 * The HBA could extract the DIFs and place it in DIF buffers.
1319 *
1320 * The buffer list for this type consists of one or more of the
1321 * protection groups described below:
1322 * +-------------------------+
1323 * start of first prot group --> | PDE_1 |
1324 * +-------------------------+
1325 * | PDE_3 (Prot BDE) |
1326 * +-------------------------+
1327 * | Data BDE |
1328 * +-------------------------+
1329 * |more Data BDE's ... (opt)|
1330 * +-------------------------+
1331 * start of new prot group --> | PDE_1 |
1332 * +-------------------------+
1333 * | ... |
1334 * +-------------------------+
1335 *
1336 * @sc: pointer to scsi command we're working on
1337 * @bpl: pointer to buffer list for protection groups
1338 * @datacnt: number of segments of data that have been dma mapped
1339 * @protcnt: number of segment of protection data that have been dma mapped
1340 *
1341 * Note: It is assumed that both data and protection s/g buffers have been
1342 * mapped for DMA
1343 */
1344static int
1345lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1346 struct ulp_bde64 *bpl, int datacnt, int protcnt)
1347{
1348 struct scatterlist *sgde = NULL; /* s/g data entry */
1349 struct scatterlist *sgpe = NULL; /* s/g prot entry */
1350 struct lpfc_pde *pde1 = NULL;
1351 struct ulp_bde64 *prot_bde = NULL;
1352 dma_addr_t dataphysaddr, protphysaddr;
1353 unsigned short curr_data = 0, curr_prot = 0;
1354 unsigned int split_offset, protgroup_len;
1355 unsigned int protgrp_blks, protgrp_bytes;
1356 unsigned int remainder, subtotal;
1357 int prof = LPFC_PROF_INVALID;
1358 int datadir = sc->sc_data_direction;
1359 unsigned char pgdone = 0, alldone = 0;
1360 unsigned blksize;
1361 uint32_t reftag;
1362 uint16_t apptagmask, apptagval;
1363 int num_bde = 0;
1364
1365 sgpe = scsi_prot_sglist(sc);
1366 sgde = scsi_sglist(sc);
1367
1368 if (!sgpe || !sgde) {
1369 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1370 "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
1371 sgpe, sgde);
1372 return 0;
1373 }
1374
James Smart6a9c52c2009-10-02 15:16:51 -04001375 prof = lpfc_sc_to_sli_prof(phba, sc);
James Smarte2a0a9d2008-12-04 22:40:02 -05001376 if (prof == LPFC_PROF_INVALID)
1377 goto out;
1378
1379 /* extract some info from the scsi command for PDE1*/
1380 blksize = lpfc_cmd_blksize(sc);
1381 lpfc_get_cmd_dif_parms(sc, &apptagmask, &apptagval, &reftag);
1382
1383 split_offset = 0;
1384 do {
1385 /* setup the first PDE_1 */
1386 pde1 = (struct lpfc_pde *) bpl;
1387
1388 lpfc_pde_set_bg_parms(pde1, LPFC_PDE1_DESCRIPTOR, prof, blksize,
1389 BG_EC_STOP_ERR);
1390 lpfc_pde_set_dif_parms(pde1, apptagmask, apptagval, reftag);
1391
1392 num_bde++;
1393 bpl++;
1394
1395 /* setup the first BDE that points to protection buffer */
1396 prot_bde = (struct ulp_bde64 *) bpl;
1397 protphysaddr = sg_dma_address(sgpe);
1398 prot_bde->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
1399 prot_bde->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
1400 protgroup_len = sg_dma_len(sgpe);
1401
1402
1403 /* must be integer multiple of the DIF block length */
1404 BUG_ON(protgroup_len % 8);
1405
1406 protgrp_blks = protgroup_len / 8;
1407 protgrp_bytes = protgrp_blks * blksize;
1408
1409 prot_bde->tus.f.bdeSize = protgroup_len;
1410 if (datadir == DMA_TO_DEVICE)
1411 prot_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1412 else
1413 prot_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1414 prot_bde->tus.w = le32_to_cpu(bpl->tus.w);
1415
1416 curr_prot++;
1417 num_bde++;
1418
1419 /* setup BDE's for data blocks associated with DIF data */
1420 pgdone = 0;
1421 subtotal = 0; /* total bytes processed for current prot grp */
1422 while (!pgdone) {
1423 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -04001424 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1425 "9065 BLKGRD:%s Invalid data segment\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001426 __func__);
1427 return 0;
1428 }
1429 bpl++;
1430 dataphysaddr = sg_dma_address(sgde) + split_offset;
1431 bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
1432 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
1433
1434 remainder = sg_dma_len(sgde) - split_offset;
1435
1436 if ((subtotal + remainder) <= protgrp_bytes) {
1437 /* we can use this whole buffer */
1438 bpl->tus.f.bdeSize = remainder;
1439 split_offset = 0;
1440
1441 if ((subtotal + remainder) == protgrp_bytes)
1442 pgdone = 1;
1443 } else {
1444 /* must split this buffer with next prot grp */
1445 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
1446 split_offset += bpl->tus.f.bdeSize;
1447 }
1448
1449 subtotal += bpl->tus.f.bdeSize;
1450
1451 if (datadir == DMA_TO_DEVICE)
1452 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1453 else
1454 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1455 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1456
1457 num_bde++;
1458 curr_data++;
1459
1460 if (split_offset)
1461 break;
1462
1463 /* Move to the next s/g segment if possible */
1464 sgde = sg_next(sgde);
1465 }
1466
1467 /* are we done ? */
1468 if (curr_prot == protcnt) {
1469 alldone = 1;
1470 } else if (curr_prot < protcnt) {
1471 /* advance to next prot buffer */
1472 sgpe = sg_next(sgpe);
1473 bpl++;
1474
1475 /* update the reference tag */
1476 reftag += protgrp_blks;
1477 } else {
1478 /* if we're here, we have a bug */
James Smart6a9c52c2009-10-02 15:16:51 -04001479 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1480 "9054 BLKGRD: bug in %s\n", __func__);
James Smarte2a0a9d2008-12-04 22:40:02 -05001481 }
1482
1483 } while (!alldone);
1484
1485out:
1486
1487
1488 return num_bde;
1489}
1490/*
1491 * Given a SCSI command that supports DIF, determine composition of protection
1492 * groups involved in setting up buffer lists
1493 *
1494 * Returns:
1495 * for DIF (for both read and write)
1496 * */
1497static int
1498lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
1499{
1500 int ret = LPFC_PG_TYPE_INVALID;
1501 unsigned char op = scsi_get_prot_op(sc);
1502
1503 switch (op) {
1504 case SCSI_PROT_READ_STRIP:
1505 case SCSI_PROT_WRITE_INSERT:
1506 ret = LPFC_PG_TYPE_NO_DIF;
1507 break;
1508 case SCSI_PROT_READ_INSERT:
1509 case SCSI_PROT_WRITE_STRIP:
1510 case SCSI_PROT_READ_PASS:
1511 case SCSI_PROT_WRITE_PASS:
James Smarte2a0a9d2008-12-04 22:40:02 -05001512 ret = LPFC_PG_TYPE_DIF_BUF;
1513 break;
1514 default:
1515 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1516 "9021 Unsupported protection op:%d\n", op);
1517 break;
1518 }
1519
1520 return ret;
1521}
1522
1523/*
1524 * This is the protection/DIF aware version of
1525 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
1526 * two functions eventually, but for now, it's here
1527 */
1528static int
1529lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba,
1530 struct lpfc_scsi_buf *lpfc_cmd)
1531{
1532 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1533 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1534 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
1535 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
1536 uint32_t num_bde = 0;
1537 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
1538 int prot_group_type = 0;
1539 int diflen, fcpdl;
1540 unsigned blksize;
1541
1542 /*
1543 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
1544 * fcp_rsp regions to the first data bde entry
1545 */
1546 bpl += 2;
1547 if (scsi_sg_count(scsi_cmnd)) {
1548 /*
1549 * The driver stores the segment count returned from pci_map_sg
1550 * because this a count of dma-mappings used to map the use_sg
1551 * pages. They are not guaranteed to be the same for those
1552 * architectures that implement an IOMMU.
1553 */
1554 datasegcnt = dma_map_sg(&phba->pcidev->dev,
1555 scsi_sglist(scsi_cmnd),
1556 scsi_sg_count(scsi_cmnd), datadir);
1557 if (unlikely(!datasegcnt))
1558 return 1;
1559
1560 lpfc_cmd->seg_cnt = datasegcnt;
1561 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001562 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1563 "9067 BLKGRD: %s: Too many sg segments"
1564 " from dma_map_sg. Config %d, seg_cnt"
1565 " %d\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001566 __func__, phba->cfg_sg_seg_cnt,
1567 lpfc_cmd->seg_cnt);
1568 scsi_dma_unmap(scsi_cmnd);
1569 return 1;
1570 }
1571
1572 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
1573
1574 switch (prot_group_type) {
1575 case LPFC_PG_TYPE_NO_DIF:
1576 num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
1577 datasegcnt);
1578 /* we shoud have 2 or more entries in buffer list */
1579 if (num_bde < 2)
1580 goto err;
1581 break;
1582 case LPFC_PG_TYPE_DIF_BUF:{
1583 /*
1584 * This type indicates that protection buffers are
1585 * passed to the driver, so that needs to be prepared
1586 * for DMA
1587 */
1588 protsegcnt = dma_map_sg(&phba->pcidev->dev,
1589 scsi_prot_sglist(scsi_cmnd),
1590 scsi_prot_sg_count(scsi_cmnd), datadir);
1591 if (unlikely(!protsegcnt)) {
1592 scsi_dma_unmap(scsi_cmnd);
1593 return 1;
1594 }
1595
1596 lpfc_cmd->prot_seg_cnt = protsegcnt;
1597 if (lpfc_cmd->prot_seg_cnt
1598 > phba->cfg_prot_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001599 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1600 "9068 BLKGRD: %s: Too many prot sg "
1601 "segments from dma_map_sg. Config %d,"
James Smarte2a0a9d2008-12-04 22:40:02 -05001602 "prot_seg_cnt %d\n", __func__,
1603 phba->cfg_prot_sg_seg_cnt,
1604 lpfc_cmd->prot_seg_cnt);
1605 dma_unmap_sg(&phba->pcidev->dev,
1606 scsi_prot_sglist(scsi_cmnd),
1607 scsi_prot_sg_count(scsi_cmnd),
1608 datadir);
1609 scsi_dma_unmap(scsi_cmnd);
1610 return 1;
1611 }
1612
1613 num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
1614 datasegcnt, protsegcnt);
1615 /* we shoud have 3 or more entries in buffer list */
1616 if (num_bde < 3)
1617 goto err;
1618 break;
1619 }
1620 case LPFC_PG_TYPE_INVALID:
1621 default:
1622 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1623 "9022 Unexpected protection group %i\n",
1624 prot_group_type);
1625 return 1;
1626 }
1627 }
1628
1629 /*
1630 * Finish initializing those IOCB fields that are dependent on the
1631 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
1632 * reinitialized since all iocb memory resources are used many times
1633 * for transmit, receive, and continuation bpl's.
1634 */
1635 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
1636 iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
1637 iocb_cmd->ulpBdeCount = 1;
1638 iocb_cmd->ulpLe = 1;
1639
1640 fcpdl = scsi_bufflen(scsi_cmnd);
1641
1642 if (scsi_get_prot_type(scsi_cmnd) == SCSI_PROT_DIF_TYPE1) {
1643 /*
1644 * We are in DIF Type 1 mode
1645 * Every data block has a 8 byte DIF (trailer)
1646 * attached to it. Must ajust FCP data length
1647 */
1648 blksize = lpfc_cmd_blksize(scsi_cmnd);
1649 diflen = (fcpdl / blksize) * 8;
1650 fcpdl += diflen;
1651 }
1652 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
1653
1654 /*
1655 * Due to difference in data length between DIF/non-DIF paths,
1656 * we need to set word 4 of IOCB here
1657 */
1658 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
1659
1660 return 0;
1661err:
1662 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1663 "9023 Could not setup all needed BDE's"
1664 "prot_group_type=%d, num_bde=%d\n",
1665 prot_group_type, num_bde);
1666 return 1;
1667}
1668
1669/*
1670 * This function checks for BlockGuard errors detected by
1671 * the HBA. In case of errors, the ASC/ASCQ fields in the
1672 * sense buffer will be set accordingly, paired with
1673 * ILLEGAL_REQUEST to signal to the kernel that the HBA
1674 * detected corruption.
1675 *
1676 * Returns:
1677 * 0 - No error found
1678 * 1 - BlockGuard error found
1679 * -1 - Internal error (bad profile, ...etc)
1680 */
1681static int
1682lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
1683 struct lpfc_iocbq *pIocbOut)
1684{
1685 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
1686 struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
1687 int ret = 0;
1688 uint32_t bghm = bgf->bghm;
1689 uint32_t bgstat = bgf->bgstat;
1690 uint64_t failing_sector = 0;
1691
James Smart6a9c52c2009-10-02 15:16:51 -04001692 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9069 BLKGRD: BG ERROR in cmd"
1693 " 0x%x lba 0x%llx blk cnt 0x%x "
James Smarte2a0a9d2008-12-04 22:40:02 -05001694 "bgstat=0x%x bghm=0x%x\n",
James Smart87b5c322008-12-16 10:34:09 -05001695 cmd->cmnd[0], (unsigned long long)scsi_get_lba(cmd),
Tejun Heo83096eb2009-05-07 22:24:39 +09001696 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05001697
1698 spin_lock(&_dump_buf_lock);
1699 if (!_dump_buf_done) {
James Smart6a9c52c2009-10-02 15:16:51 -04001700 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9070 BLKGRD: Saving"
1701 " Data for %u blocks to debugfs\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001702 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
James Smart6a9c52c2009-10-02 15:16:51 -04001703 lpfc_debug_save_data(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05001704
1705 /* If we have a prot sgl, save the DIF buffer */
1706 if (lpfc_prot_group_type(phba, cmd) ==
1707 LPFC_PG_TYPE_DIF_BUF) {
James Smart6a9c52c2009-10-02 15:16:51 -04001708 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9071 BLKGRD: "
1709 "Saving DIF for %u blocks to debugfs\n",
1710 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
1711 lpfc_debug_save_dif(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05001712 }
1713
1714 _dump_buf_done = 1;
1715 }
1716 spin_unlock(&_dump_buf_lock);
1717
1718 if (lpfc_bgs_get_invalid_prof(bgstat)) {
1719 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart6a9c52c2009-10-02 15:16:51 -04001720 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9072 BLKGRD: Invalid"
1721 " BlockGuard profile. bgstat:0x%x\n",
1722 bgstat);
James Smarte2a0a9d2008-12-04 22:40:02 -05001723 ret = (-1);
1724 goto out;
1725 }
1726
1727 if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
1728 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart6a9c52c2009-10-02 15:16:51 -04001729 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9073 BLKGRD: "
1730 "Invalid BlockGuard DIF Block. bgstat:0x%x\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001731 bgstat);
1732 ret = (-1);
1733 goto out;
1734 }
1735
1736 if (lpfc_bgs_get_guard_err(bgstat)) {
1737 ret = 1;
1738
1739 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
1740 0x10, 0x1);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05001741 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05001742 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
1743 phba->bg_guard_err_cnt++;
James Smart6a9c52c2009-10-02 15:16:51 -04001744 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1745 "9055 BLKGRD: guard_tag error\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05001746 }
1747
1748 if (lpfc_bgs_get_reftag_err(bgstat)) {
1749 ret = 1;
1750
1751 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
1752 0x10, 0x3);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05001753 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05001754 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
1755
1756 phba->bg_reftag_err_cnt++;
James Smart6a9c52c2009-10-02 15:16:51 -04001757 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1758 "9056 BLKGRD: ref_tag error\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05001759 }
1760
1761 if (lpfc_bgs_get_apptag_err(bgstat)) {
1762 ret = 1;
1763
1764 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
1765 0x10, 0x2);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05001766 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05001767 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
1768
1769 phba->bg_apptag_err_cnt++;
James Smart6a9c52c2009-10-02 15:16:51 -04001770 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1771 "9061 BLKGRD: app_tag error\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05001772 }
1773
1774 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
1775 /*
1776 * setup sense data descriptor 0 per SPC-4 as an information
1777 * field, and put the failing LBA in it
1778 */
1779 cmd->sense_buffer[8] = 0; /* Information */
1780 cmd->sense_buffer[9] = 0xa; /* Add. length */
David Howells2344b5b2009-04-14 17:08:34 +01001781 bghm /= cmd->device->sector_size;
James Smarte2a0a9d2008-12-04 22:40:02 -05001782
1783 failing_sector = scsi_get_lba(cmd);
1784 failing_sector += bghm;
1785
1786 put_unaligned_be64(failing_sector, &cmd->sense_buffer[10]);
1787 }
1788
1789 if (!ret) {
1790 /* No error was reported - problem in FW? */
1791 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart6a9c52c2009-10-02 15:16:51 -04001792 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1793 "9057 BLKGRD: no errors reported!\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05001794 }
1795
1796out:
1797 return ret;
1798}
1799
James Smartea2151b2008-09-07 11:52:10 -04001800/**
James Smartda0436e2009-05-22 14:51:39 -04001801 * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
1802 * @phba: The Hba for which this call is being executed.
1803 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1804 *
1805 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
1806 * field of @lpfc_cmd for device with SLI-4 interface spec.
1807 *
1808 * Return codes:
1809 * 1 - Error
1810 * 0 - Success
1811 **/
1812static int
1813lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
1814{
1815 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1816 struct scatterlist *sgel = NULL;
1817 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1818 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
1819 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
1820 dma_addr_t physaddr;
1821 uint32_t num_bde = 0;
1822 uint32_t dma_len;
1823 uint32_t dma_offset = 0;
1824 int nseg;
1825
1826 /*
1827 * There are three possibilities here - use scatter-gather segment, use
1828 * the single mapping, or neither. Start the lpfc command prep by
1829 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1830 * data bde entry.
1831 */
1832 if (scsi_sg_count(scsi_cmnd)) {
1833 /*
1834 * The driver stores the segment count returned from pci_map_sg
1835 * because this a count of dma-mappings used to map the use_sg
1836 * pages. They are not guaranteed to be the same for those
1837 * architectures that implement an IOMMU.
1838 */
1839
1840 nseg = scsi_dma_map(scsi_cmnd);
1841 if (unlikely(!nseg))
1842 return 1;
1843 sgl += 1;
1844 /* clear the last flag in the fcp_rsp map entry */
1845 sgl->word2 = le32_to_cpu(sgl->word2);
1846 bf_set(lpfc_sli4_sge_last, sgl, 0);
1847 sgl->word2 = cpu_to_le32(sgl->word2);
1848 sgl += 1;
1849
1850 lpfc_cmd->seg_cnt = nseg;
1851 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001852 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
1853 " %s: Too many sg segments from "
1854 "dma_map_sg. Config %d, seg_cnt %d\n",
1855 __func__, phba->cfg_sg_seg_cnt,
James Smartda0436e2009-05-22 14:51:39 -04001856 lpfc_cmd->seg_cnt);
1857 scsi_dma_unmap(scsi_cmnd);
1858 return 1;
1859 }
1860
1861 /*
1862 * The driver established a maximum scatter-gather segment count
1863 * during probe that limits the number of sg elements in any
1864 * single scsi command. Just run through the seg_cnt and format
1865 * the sge's.
1866 * When using SLI-3 the driver will try to fit all the BDEs into
1867 * the IOCB. If it can't then the BDEs get added to a BPL as it
1868 * does for SLI-2 mode.
1869 */
1870 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
1871 physaddr = sg_dma_address(sgel);
1872 dma_len = sg_dma_len(sgel);
James Smartda0436e2009-05-22 14:51:39 -04001873 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
1874 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
1875 if ((num_bde + 1) == nseg)
1876 bf_set(lpfc_sli4_sge_last, sgl, 1);
1877 else
1878 bf_set(lpfc_sli4_sge_last, sgl, 0);
1879 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
1880 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05001881 sgl->sge_len = cpu_to_le32(dma_len);
James Smartda0436e2009-05-22 14:51:39 -04001882 dma_offset += dma_len;
1883 sgl++;
1884 }
1885 } else {
1886 sgl += 1;
1887 /* clear the last flag in the fcp_rsp map entry */
1888 sgl->word2 = le32_to_cpu(sgl->word2);
1889 bf_set(lpfc_sli4_sge_last, sgl, 1);
1890 sgl->word2 = cpu_to_le32(sgl->word2);
1891 }
1892
1893 /*
1894 * Finish initializing those IOCB fields that are dependent on the
1895 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
1896 * explicitly reinitialized.
1897 * all iocb memory resources are reused.
1898 */
1899 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
1900
1901 /*
1902 * Due to difference in data length between DIF/non-DIF paths,
1903 * we need to set word 4 of IOCB here
1904 */
1905 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
1906 return 0;
1907}
1908
1909/**
James Smart3772a992009-05-22 14:50:54 -04001910 * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
1911 * @phba: The Hba for which this call is being executed.
1912 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1913 *
1914 * This routine wraps the actual DMA mapping function pointer from the
1915 * lpfc_hba struct.
1916 *
1917 * Return codes:
1918 * 1 - Error
1919 * 0 - Success
1920 **/
1921static inline int
1922lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
1923{
1924 return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
1925}
1926
1927/**
James Smart3621a712009-04-06 18:47:14 -04001928 * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
James Smartea2151b2008-09-07 11:52:10 -04001929 * @phba: Pointer to hba context object.
1930 * @vport: Pointer to vport object.
1931 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
1932 * @rsp_iocb: Pointer to response iocb object which reported error.
1933 *
1934 * This function posts an event when there is a SCSI command reporting
1935 * error from the scsi device.
1936 **/
1937static void
1938lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
1939 struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
1940 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
1941 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
1942 uint32_t resp_info = fcprsp->rspStatus2;
1943 uint32_t scsi_status = fcprsp->rspStatus3;
1944 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
1945 struct lpfc_fast_path_event *fast_path_evt = NULL;
1946 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
1947 unsigned long flags;
1948
1949 /* If there is queuefull or busy condition send a scsi event */
1950 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
1951 (cmnd->result == SAM_STAT_BUSY)) {
1952 fast_path_evt = lpfc_alloc_fast_evt(phba);
1953 if (!fast_path_evt)
1954 return;
1955 fast_path_evt->un.scsi_evt.event_type =
1956 FC_REG_SCSI_EVENT;
1957 fast_path_evt->un.scsi_evt.subcategory =
1958 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
1959 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
1960 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
1961 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
1962 &pnode->nlp_portname, sizeof(struct lpfc_name));
1963 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
1964 &pnode->nlp_nodename, sizeof(struct lpfc_name));
1965 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
1966 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
1967 fast_path_evt = lpfc_alloc_fast_evt(phba);
1968 if (!fast_path_evt)
1969 return;
1970 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
1971 FC_REG_SCSI_EVENT;
1972 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
1973 LPFC_EVENT_CHECK_COND;
1974 fast_path_evt->un.check_cond_evt.scsi_event.lun =
1975 cmnd->device->lun;
1976 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
1977 &pnode->nlp_portname, sizeof(struct lpfc_name));
1978 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
1979 &pnode->nlp_nodename, sizeof(struct lpfc_name));
1980 fast_path_evt->un.check_cond_evt.sense_key =
1981 cmnd->sense_buffer[2] & 0xf;
1982 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
1983 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
1984 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
1985 fcpi_parm &&
1986 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
1987 ((scsi_status == SAM_STAT_GOOD) &&
1988 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
1989 /*
1990 * If status is good or resid does not match with fcp_param and
1991 * there is valid fcpi_parm, then there is a read_check error
1992 */
1993 fast_path_evt = lpfc_alloc_fast_evt(phba);
1994 if (!fast_path_evt)
1995 return;
1996 fast_path_evt->un.read_check_error.header.event_type =
1997 FC_REG_FABRIC_EVENT;
1998 fast_path_evt->un.read_check_error.header.subcategory =
1999 LPFC_EVENT_FCPRDCHKERR;
2000 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
2001 &pnode->nlp_portname, sizeof(struct lpfc_name));
2002 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
2003 &pnode->nlp_nodename, sizeof(struct lpfc_name));
2004 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
2005 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
2006 fast_path_evt->un.read_check_error.fcpiparam =
2007 fcpi_parm;
2008 } else
2009 return;
2010
2011 fast_path_evt->vport = vport;
2012 spin_lock_irqsave(&phba->hbalock, flags);
2013 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
2014 spin_unlock_irqrestore(&phba->hbalock, flags);
2015 lpfc_worker_wake_up(phba);
2016 return;
2017}
James Smart9bad7672008-12-04 22:39:02 -05002018
2019/**
James Smartf1126682009-06-10 17:22:44 -04002020 * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
James Smart3772a992009-05-22 14:50:54 -04002021 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -05002022 * @psb: The scsi buffer which is going to be un-mapped.
2023 *
2024 * This routine does DMA un-mapping of scatter gather list of scsi command
James Smart3772a992009-05-22 14:50:54 -04002025 * field of @lpfc_cmd for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05002026 **/
dea31012005-04-17 16:05:31 -05002027static void
James Smartf1126682009-06-10 17:22:44 -04002028lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James Smartbcf4dbf2006-07-06 15:50:08 -04002029{
2030 /*
2031 * There are only two special cases to consider. (1) the scsi command
2032 * requested scatter-gather usage or (2) the scsi command allocated
2033 * a request buffer, but did not request use_sg. There is a third
2034 * case, but it does not require resource deallocation.
2035 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002036 if (psb->seg_cnt > 0)
2037 scsi_dma_unmap(psb->pCmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002038 if (psb->prot_seg_cnt > 0)
2039 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
2040 scsi_prot_sg_count(psb->pCmd),
2041 psb->pCmd->sc_data_direction);
James Smartbcf4dbf2006-07-06 15:50:08 -04002042}
2043
James Smart9bad7672008-12-04 22:39:02 -05002044/**
James Smart3621a712009-04-06 18:47:14 -04002045 * lpfc_handler_fcp_err - FCP response handler
James Smart9bad7672008-12-04 22:39:02 -05002046 * @vport: The virtual port for which this call is being executed.
2047 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
2048 * @rsp_iocb: The response IOCB which contains FCP error.
2049 *
2050 * This routine is called to process response IOCB with status field
2051 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
2052 * based upon SCSI and FCP error.
2053 **/
James Smartbcf4dbf2006-07-06 15:50:08 -04002054static void
James Smart2e0fef82007-06-17 19:56:36 -05002055lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
2056 struct lpfc_iocbq *rsp_iocb)
dea31012005-04-17 16:05:31 -05002057{
2058 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
2059 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
2060 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
James Smart7054a602007-04-25 09:52:34 -04002061 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
dea31012005-04-17 16:05:31 -05002062 uint32_t resp_info = fcprsp->rspStatus2;
2063 uint32_t scsi_status = fcprsp->rspStatus3;
James Smartc7743952006-12-02 13:34:42 -05002064 uint32_t *lp;
dea31012005-04-17 16:05:31 -05002065 uint32_t host_status = DID_OK;
2066 uint32_t rsplen = 0;
James Smartc7743952006-12-02 13:34:42 -05002067 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea31012005-04-17 16:05:31 -05002068
James Smartea2151b2008-09-07 11:52:10 -04002069
dea31012005-04-17 16:05:31 -05002070 /*
2071 * If this is a task management command, there is no
2072 * scsi packet associated with this lpfc_cmd. The driver
2073 * consumes it.
2074 */
2075 if (fcpcmd->fcpCntl2) {
2076 scsi_status = 0;
2077 goto out;
2078 }
2079
James Smart6a9c52c2009-10-02 15:16:51 -04002080 if (resp_info & RSP_LEN_VALID) {
2081 rsplen = be32_to_cpu(fcprsp->rspRspLen);
James Smarte40a02c2010-02-26 14:13:54 -05002082 if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
James Smart6a9c52c2009-10-02 15:16:51 -04002083 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
2084 "2719 Invalid response length: "
2085 "tgt x%x lun x%x cmnd x%x rsplen x%x\n",
2086 cmnd->device->id,
2087 cmnd->device->lun, cmnd->cmnd[0],
2088 rsplen);
2089 host_status = DID_ERROR;
2090 goto out;
2091 }
James Smarte40a02c2010-02-26 14:13:54 -05002092 if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
2093 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
2094 "2757 Protocol failure detected during "
2095 "processing of FCP I/O op: "
2096 "tgt x%x lun x%x cmnd x%x rspInfo3 x%x\n",
2097 cmnd->device->id,
2098 cmnd->device->lun, cmnd->cmnd[0],
2099 fcprsp->rspInfo3);
2100 host_status = DID_ERROR;
2101 goto out;
2102 }
James Smart6a9c52c2009-10-02 15:16:51 -04002103 }
2104
James Smartc7743952006-12-02 13:34:42 -05002105 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
2106 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
2107 if (snslen > SCSI_SENSE_BUFFERSIZE)
2108 snslen = SCSI_SENSE_BUFFERSIZE;
2109
2110 if (resp_info & RSP_LEN_VALID)
2111 rsplen = be32_to_cpu(fcprsp->rspRspLen);
2112 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
2113 }
2114 lp = (uint32_t *)cmnd->sense_buffer;
2115
2116 if (!scsi_status && (resp_info & RESID_UNDER))
2117 logit = LOG_FCP;
2118
James Smarte8b62012007-08-02 11:10:09 -04002119 lpfc_printf_vlog(vport, KERN_WARNING, logit,
James Smarte2a0a9d2008-12-04 22:40:02 -05002120 "9024 FCP command x%x failed: x%x SNS x%x x%x "
James Smarte8b62012007-08-02 11:10:09 -04002121 "Data: x%x x%x x%x x%x x%x\n",
2122 cmnd->cmnd[0], scsi_status,
2123 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
2124 be32_to_cpu(fcprsp->rspResId),
2125 be32_to_cpu(fcprsp->rspSnsLen),
2126 be32_to_cpu(fcprsp->rspRspLen),
2127 fcprsp->rspInfo3);
dea31012005-04-17 16:05:31 -05002128
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002129 scsi_set_resid(cmnd, 0);
dea31012005-04-17 16:05:31 -05002130 if (resp_info & RESID_UNDER) {
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002131 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea31012005-04-17 16:05:31 -05002132
James Smarte8b62012007-08-02 11:10:09 -04002133 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05002134 "9025 FCP Read Underrun, expected %d, "
James Smarte8b62012007-08-02 11:10:09 -04002135 "residual %d Data: x%x x%x x%x\n",
2136 be32_to_cpu(fcpcmd->fcpDl),
2137 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
2138 cmnd->underflow);
dea31012005-04-17 16:05:31 -05002139
2140 /*
James Smart7054a602007-04-25 09:52:34 -04002141 * If there is an under run check if under run reported by
2142 * storage array is same as the under run reported by HBA.
2143 * If this is not same, there is a dropped frame.
2144 */
2145 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
2146 fcpi_parm &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002147 (scsi_get_resid(cmnd) != fcpi_parm)) {
James Smarte8b62012007-08-02 11:10:09 -04002148 lpfc_printf_vlog(vport, KERN_WARNING,
2149 LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05002150 "9026 FCP Read Check Error "
James Smarte8b62012007-08-02 11:10:09 -04002151 "and Underrun Data: x%x x%x x%x x%x\n",
2152 be32_to_cpu(fcpcmd->fcpDl),
2153 scsi_get_resid(cmnd), fcpi_parm,
2154 cmnd->cmnd[0]);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002155 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
James Smart7054a602007-04-25 09:52:34 -04002156 host_status = DID_ERROR;
2157 }
2158 /*
dea31012005-04-17 16:05:31 -05002159 * The cmnd->underflow is the minimum number of bytes that must
2160 * be transfered for this command. Provided a sense condition
2161 * is not present, make sure the actual amount transferred is at
2162 * least the underflow value or fail.
2163 */
2164 if (!(resp_info & SNS_LEN_VALID) &&
2165 (scsi_status == SAM_STAT_GOOD) &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002166 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
2167 < cmnd->underflow)) {
James Smarte8b62012007-08-02 11:10:09 -04002168 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05002169 "9027 FCP command x%x residual "
James Smarte8b62012007-08-02 11:10:09 -04002170 "underrun converted to error "
2171 "Data: x%x x%x x%x\n",
James Smart66dbfbe2007-08-05 06:08:38 -04002172 cmnd->cmnd[0], scsi_bufflen(cmnd),
James Smarte8b62012007-08-02 11:10:09 -04002173 scsi_get_resid(cmnd), cmnd->underflow);
dea31012005-04-17 16:05:31 -05002174 host_status = DID_ERROR;
2175 }
2176 } else if (resp_info & RESID_OVER) {
James Smarte8b62012007-08-02 11:10:09 -04002177 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05002178 "9028 FCP command x%x residual overrun error. "
James Smarte4e74272009-07-19 10:01:38 -04002179 "Data: x%x x%x\n", cmnd->cmnd[0],
James Smarte8b62012007-08-02 11:10:09 -04002180 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea31012005-04-17 16:05:31 -05002181 host_status = DID_ERROR;
2182
2183 /*
2184 * Check SLI validation that all the transfer was actually done
2185 * (fcpi_parm should be zero). Apply check only to reads.
2186 */
2187 } else if ((scsi_status == SAM_STAT_GOOD) && fcpi_parm &&
2188 (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
James Smarte8b62012007-08-02 11:10:09 -04002189 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05002190 "9029 FCP Read Check Error Data: "
James Smarte8b62012007-08-02 11:10:09 -04002191 "x%x x%x x%x x%x\n",
2192 be32_to_cpu(fcpcmd->fcpDl),
2193 be32_to_cpu(fcprsp->rspResId),
2194 fcpi_parm, cmnd->cmnd[0]);
dea31012005-04-17 16:05:31 -05002195 host_status = DID_ERROR;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002196 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea31012005-04-17 16:05:31 -05002197 }
2198
2199 out:
2200 cmnd->result = ScsiResult(host_status, scsi_status);
James Smartea2151b2008-09-07 11:52:10 -04002201 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
dea31012005-04-17 16:05:31 -05002202}
2203
James Smart9bad7672008-12-04 22:39:02 -05002204/**
James Smart3621a712009-04-06 18:47:14 -04002205 * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
James Smart9bad7672008-12-04 22:39:02 -05002206 * @phba: The Hba for which this call is being executed.
2207 * @pIocbIn: The command IOCBQ for the scsi cmnd.
James Smart3772a992009-05-22 14:50:54 -04002208 * @pIocbOut: The response IOCBQ for the scsi cmnd.
James Smart9bad7672008-12-04 22:39:02 -05002209 *
2210 * This routine assigns scsi command result by looking into response IOCB
2211 * status field appropriately. This routine handles QUEUE FULL condition as
2212 * well by ramping down device queue depth.
2213 **/
dea31012005-04-17 16:05:31 -05002214static void
2215lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
2216 struct lpfc_iocbq *pIocbOut)
2217{
2218 struct lpfc_scsi_buf *lpfc_cmd =
2219 (struct lpfc_scsi_buf *) pIocbIn->context1;
James Smart2e0fef82007-06-17 19:56:36 -05002220 struct lpfc_vport *vport = pIocbIn->vport;
dea31012005-04-17 16:05:31 -05002221 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
2222 struct lpfc_nodelist *pnode = rdata->pnode;
2223 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002224 int result;
James Smarta257bf92009-04-06 18:48:10 -04002225 struct scsi_device *tmp_sdev;
James Smart5ffc2662009-11-18 15:39:44 -05002226 int depth;
James Smartfa61a542008-01-11 01:52:42 -05002227 unsigned long flags;
James Smartea2151b2008-09-07 11:52:10 -04002228 struct lpfc_fast_path_event *fast_path_evt;
James Smarta257bf92009-04-06 18:48:10 -04002229 struct Scsi_Host *shost = cmd->device->host;
2230 uint32_t queue_depth, scsi_id;
dea31012005-04-17 16:05:31 -05002231
2232 lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
2233 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
James Smart341af102010-01-26 23:07:37 -05002234 /* pick up SLI4 exhange busy status from HBA */
2235 lpfc_cmd->exch_busy = pIocbOut->iocb_flag & LPFC_EXCHANGE_BUSY;
2236
James Smart109f6ed2008-12-04 22:39:08 -05002237 if (pnode && NLP_CHK_NODE_ACT(pnode))
2238 atomic_dec(&pnode->cmd_pending);
dea31012005-04-17 16:05:31 -05002239
2240 if (lpfc_cmd->status) {
2241 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
2242 (lpfc_cmd->result & IOERR_DRVR_MASK))
2243 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
2244 else if (lpfc_cmd->status >= IOSTAT_CNT)
2245 lpfc_cmd->status = IOSTAT_DEFAULT;
2246
James Smarte8b62012007-08-02 11:10:09 -04002247 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05002248 "9030 FCP cmd x%x failed <%d/%d> "
James Smarte8b62012007-08-02 11:10:09 -04002249 "status: x%x result: x%x Data: x%x x%x\n",
2250 cmd->cmnd[0],
2251 cmd->device ? cmd->device->id : 0xffff,
2252 cmd->device ? cmd->device->lun : 0xffff,
2253 lpfc_cmd->status, lpfc_cmd->result,
2254 pIocbOut->iocb.ulpContext,
2255 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
dea31012005-04-17 16:05:31 -05002256
2257 switch (lpfc_cmd->status) {
2258 case IOSTAT_FCP_RSP_ERROR:
2259 /* Call FCP RSP handler to determine result */
James Smart2e0fef82007-06-17 19:56:36 -05002260 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea31012005-04-17 16:05:31 -05002261 break;
2262 case IOSTAT_NPORT_BSY:
2263 case IOSTAT_FABRIC_BSY:
James Smart0f1f53a2008-08-24 21:50:18 -04002264 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
James Smartea2151b2008-09-07 11:52:10 -04002265 fast_path_evt = lpfc_alloc_fast_evt(phba);
2266 if (!fast_path_evt)
2267 break;
2268 fast_path_evt->un.fabric_evt.event_type =
2269 FC_REG_FABRIC_EVENT;
2270 fast_path_evt->un.fabric_evt.subcategory =
2271 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
2272 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
2273 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
2274 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
2275 &pnode->nlp_portname,
2276 sizeof(struct lpfc_name));
2277 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
2278 &pnode->nlp_nodename,
2279 sizeof(struct lpfc_name));
2280 }
2281 fast_path_evt->vport = vport;
2282 fast_path_evt->work_evt.evt =
2283 LPFC_EVT_FASTPATH_MGMT_EVT;
2284 spin_lock_irqsave(&phba->hbalock, flags);
2285 list_add_tail(&fast_path_evt->work_evt.evt_listp,
2286 &phba->work_list);
2287 spin_unlock_irqrestore(&phba->hbalock, flags);
2288 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05002289 break;
James Smart92d7f7b2007-06-17 19:56:38 -05002290 case IOSTAT_LOCAL_REJECT:
James Smartd7c255b2008-08-24 21:50:00 -04002291 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
James Smart92d7f7b2007-06-17 19:56:38 -05002292 lpfc_cmd->result == IOERR_NO_RESOURCES ||
James Smartd7c255b2008-08-24 21:50:00 -04002293 lpfc_cmd->result == IOERR_ABORT_REQUESTED) {
James Smart92d7f7b2007-06-17 19:56:38 -05002294 cmd->result = ScsiResult(DID_REQUEUE, 0);
James Smart58da1ff2008-04-07 10:15:56 -04002295 break;
James Smarte2a0a9d2008-12-04 22:40:02 -05002296 }
2297
2298 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
2299 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
2300 pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
2301 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
2302 /*
2303 * This is a response for a BG enabled
2304 * cmd. Parse BG error
2305 */
2306 lpfc_parse_bg_err(phba, lpfc_cmd,
2307 pIocbOut);
2308 break;
2309 } else {
2310 lpfc_printf_vlog(vport, KERN_WARNING,
2311 LOG_BG,
2312 "9031 non-zero BGSTAT "
James Smart6a9c52c2009-10-02 15:16:51 -04002313 "on unprotected cmd\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05002314 }
2315 }
2316
2317 /* else: fall through */
dea31012005-04-17 16:05:31 -05002318 default:
2319 cmd->result = ScsiResult(DID_ERROR, 0);
2320 break;
2321 }
2322
James Smart58da1ff2008-04-07 10:15:56 -04002323 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002324 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
James Smart0f1f53a2008-08-24 21:50:18 -04002325 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
2326 SAM_STAT_BUSY);
dea31012005-04-17 16:05:31 -05002327 } else {
2328 cmd->result = ScsiResult(DID_OK, 0);
2329 }
2330
2331 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
2332 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
2333
James Smarte8b62012007-08-02 11:10:09 -04002334 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
2335 "0710 Iodone <%d/%d> cmd %p, error "
2336 "x%x SNS x%x x%x Data: x%x x%x\n",
2337 cmd->device->id, cmd->device->lun, cmd,
2338 cmd->result, *lp, *(lp + 3), cmd->retries,
2339 scsi_get_resid(cmd));
dea31012005-04-17 16:05:31 -05002340 }
2341
James Smartea2151b2008-09-07 11:52:10 -04002342 lpfc_update_stats(phba, lpfc_cmd);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002343 result = cmd->result;
James Smart977b5a02008-09-07 11:52:04 -04002344 if (vport->cfg_max_scsicmpl_time &&
2345 time_after(jiffies, lpfc_cmd->start_time +
2346 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
James Smarta257bf92009-04-06 18:48:10 -04002347 spin_lock_irqsave(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05002348 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
2349 if (pnode->cmd_qdepth >
2350 atomic_read(&pnode->cmd_pending) &&
2351 (atomic_read(&pnode->cmd_pending) >
2352 LPFC_MIN_TGT_QDEPTH) &&
2353 ((cmd->cmnd[0] == READ_10) ||
2354 (cmd->cmnd[0] == WRITE_10)))
2355 pnode->cmd_qdepth =
2356 atomic_read(&pnode->cmd_pending);
James Smart977b5a02008-09-07 11:52:04 -04002357
James Smart109f6ed2008-12-04 22:39:08 -05002358 pnode->last_change_time = jiffies;
2359 }
James Smarta257bf92009-04-06 18:48:10 -04002360 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05002361 } else if (pnode && NLP_CHK_NODE_ACT(pnode)) {
2362 if ((pnode->cmd_qdepth < LPFC_MAX_TGT_QDEPTH) &&
James Smart977b5a02008-09-07 11:52:04 -04002363 time_after(jiffies, pnode->last_change_time +
James Smart109f6ed2008-12-04 22:39:08 -05002364 msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
James Smarta257bf92009-04-06 18:48:10 -04002365 spin_lock_irqsave(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05002366 pnode->cmd_qdepth += pnode->cmd_qdepth *
2367 LPFC_TGTQ_RAMPUP_PCENT / 100;
2368 if (pnode->cmd_qdepth > LPFC_MAX_TGT_QDEPTH)
2369 pnode->cmd_qdepth = LPFC_MAX_TGT_QDEPTH;
2370 pnode->last_change_time = jiffies;
James Smarta257bf92009-04-06 18:48:10 -04002371 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05002372 }
James Smart977b5a02008-09-07 11:52:04 -04002373 }
2374
James Smart1dcb58e2007-04-25 09:51:30 -04002375 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James Smarta257bf92009-04-06 18:48:10 -04002376
2377 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
2378 queue_depth = cmd->device->queue_depth;
2379 scsi_id = cmd->device->id;
dea31012005-04-17 16:05:31 -05002380 cmd->scsi_done(cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002381
Jamie Wellnitzb8086082006-02-28 22:33:12 -05002382 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smartfa61a542008-01-11 01:52:42 -05002383 /*
2384 * If there is a thread waiting for command completion
2385 * wake up the thread.
2386 */
James Smarta257bf92009-04-06 18:48:10 -04002387 spin_lock_irqsave(shost->host_lock, flags);
James Smart495a7142008-06-14 22:52:59 -04002388 lpfc_cmd->pCmd = NULL;
James Smartfa61a542008-01-11 01:52:42 -05002389 if (lpfc_cmd->waitq)
2390 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04002391 spin_unlock_irqrestore(shost->host_lock, flags);
Jamie Wellnitzb8086082006-02-28 22:33:12 -05002392 lpfc_release_scsi_buf(phba, lpfc_cmd);
2393 return;
2394 }
2395
James Smart92d7f7b2007-06-17 19:56:38 -05002396 if (!result)
James Smarta257bf92009-04-06 18:48:10 -04002397 lpfc_rampup_queue_depth(vport, queue_depth);
James Smart92d7f7b2007-06-17 19:56:38 -05002398
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002399 /*
2400 * Check for queue full. If the lun is reporting queue full, then
2401 * back off the lun queue depth to prevent target overloads.
2402 */
James Smart58da1ff2008-04-07 10:15:56 -04002403 if (result == SAM_STAT_TASK_SET_FULL && pnode &&
2404 NLP_CHK_NODE_ACT(pnode)) {
James Smarta257bf92009-04-06 18:48:10 -04002405 shost_for_each_device(tmp_sdev, shost) {
2406 if (tmp_sdev->id != scsi_id)
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002407 continue;
2408 depth = scsi_track_queue_full(tmp_sdev,
James Smart5ffc2662009-11-18 15:39:44 -05002409 tmp_sdev->queue_depth-1);
2410 if (depth <= 0)
2411 continue;
James Smarte8b62012007-08-02 11:10:09 -04002412 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
2413 "0711 detected queue full - lun queue "
2414 "depth adjusted to %d.\n", depth);
James Smartea2151b2008-09-07 11:52:10 -04002415 lpfc_send_sdev_queuedepth_change_event(phba, vport,
James Smart5ffc2662009-11-18 15:39:44 -05002416 pnode,
2417 tmp_sdev->lun,
2418 depth+1, depth);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002419 }
2420 }
2421
James Smartfa61a542008-01-11 01:52:42 -05002422 /*
2423 * If there is a thread waiting for command completion
2424 * wake up the thread.
2425 */
James Smarta257bf92009-04-06 18:48:10 -04002426 spin_lock_irqsave(shost->host_lock, flags);
James Smart495a7142008-06-14 22:52:59 -04002427 lpfc_cmd->pCmd = NULL;
James Smartfa61a542008-01-11 01:52:42 -05002428 if (lpfc_cmd->waitq)
2429 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04002430 spin_unlock_irqrestore(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05002431
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002432 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05002433}
2434
James Smart34b02dc2008-08-24 21:49:55 -04002435/**
James Smart3621a712009-04-06 18:47:14 -04002436 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
James Smart34b02dc2008-08-24 21:49:55 -04002437 * @data: A pointer to the immediate command data portion of the IOCB.
2438 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
2439 *
2440 * The routine copies the entire FCP command from @fcp_cmnd to @data while
2441 * byte swapping the data to big endian format for transmission on the wire.
2442 **/
2443static void
2444lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
2445{
2446 int i, j;
2447 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
2448 i += sizeof(uint32_t), j++) {
2449 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
2450 }
2451}
2452
James Smart9bad7672008-12-04 22:39:02 -05002453/**
James Smartf1126682009-06-10 17:22:44 -04002454 * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05002455 * @vport: The virtual port for which this call is being executed.
2456 * @lpfc_cmd: The scsi command which needs to send.
2457 * @pnode: Pointer to lpfc_nodelist.
2458 *
2459 * This routine initializes fcp_cmnd and iocb data structure from scsi command
James Smart3772a992009-05-22 14:50:54 -04002460 * to transfer for device with SLI3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05002461 **/
dea31012005-04-17 16:05:31 -05002462static void
James Smartf1126682009-06-10 17:22:44 -04002463lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
James Smart2e0fef82007-06-17 19:56:36 -05002464 struct lpfc_nodelist *pnode)
dea31012005-04-17 16:05:31 -05002465{
James Smart2e0fef82007-06-17 19:56:36 -05002466 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002467 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2468 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2469 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2470 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
2471 int datadir = scsi_cmnd->sc_data_direction;
James Smart7e2b19f2007-10-29 11:00:39 -04002472 char tag[2];
dea31012005-04-17 16:05:31 -05002473
James Smart58da1ff2008-04-07 10:15:56 -04002474 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
2475 return;
2476
dea31012005-04-17 16:05:31 -05002477 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
James.Smart@Emulex.Com69859dc2005-08-10 15:02:37 -04002478 /* clear task management bits */
2479 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea31012005-04-17 16:05:31 -05002480
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -04002481 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
2482 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05002483
2484 memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, 16);
2485
James Smart7e2b19f2007-10-29 11:00:39 -04002486 if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
2487 switch (tag[0]) {
dea31012005-04-17 16:05:31 -05002488 case HEAD_OF_QUEUE_TAG:
2489 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
2490 break;
2491 case ORDERED_QUEUE_TAG:
2492 fcp_cmnd->fcpCntl1 = ORDERED_Q;
2493 break;
2494 default:
2495 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
2496 break;
2497 }
2498 } else
2499 fcp_cmnd->fcpCntl1 = 0;
2500
2501 /*
2502 * There are three possibilities here - use scatter-gather segment, use
2503 * the single mapping, or neither. Start the lpfc command prep by
2504 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
2505 * data bde entry.
2506 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002507 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05002508 if (datadir == DMA_TO_DEVICE) {
2509 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
James Smart3772a992009-05-22 14:50:54 -04002510 if (phba->sli_rev < LPFC_SLI_REV4) {
2511 iocb_cmd->un.fcpi.fcpi_parm = 0;
2512 iocb_cmd->ulpPU = 0;
2513 } else
2514 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05002515 fcp_cmnd->fcpCntl3 = WRITE_DATA;
2516 phba->fc4OutputRequests++;
2517 } else {
2518 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
2519 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05002520 fcp_cmnd->fcpCntl3 = READ_DATA;
2521 phba->fc4InputRequests++;
2522 }
2523 } else {
2524 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
2525 iocb_cmd->un.fcpi.fcpi_parm = 0;
2526 iocb_cmd->ulpPU = 0;
2527 fcp_cmnd->fcpCntl3 = 0;
2528 phba->fc4ControlRequests++;
2529 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002530 if (phba->sli_rev == 3 &&
2531 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04002532 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05002533 /*
2534 * Finish initializing those IOCB fields that are independent
2535 * of the scsi_cmnd request_buffer
2536 */
2537 piocbq->iocb.ulpContext = pnode->nlp_rpi;
2538 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
2539 piocbq->iocb.ulpFCP2Rcvy = 1;
James Smart09372822008-01-11 01:52:54 -05002540 else
2541 piocbq->iocb.ulpFCP2Rcvy = 0;
dea31012005-04-17 16:05:31 -05002542
2543 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
2544 piocbq->context1 = lpfc_cmd;
2545 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
2546 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
James Smart2e0fef82007-06-17 19:56:36 -05002547 piocbq->vport = vport;
dea31012005-04-17 16:05:31 -05002548}
2549
James Smart9bad7672008-12-04 22:39:02 -05002550/**
James Smartf1126682009-06-10 17:22:44 -04002551 * lpfc_scsi_prep_task_mgmt_cmnd - Convert SLI3 scsi TM cmd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05002552 * @vport: The virtual port for which this call is being executed.
2553 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
2554 * @lun: Logical unit number.
2555 * @task_mgmt_cmd: SCSI task management command.
2556 *
James Smart3772a992009-05-22 14:50:54 -04002557 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
2558 * for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05002559 *
2560 * Return codes:
2561 * 0 - Error
2562 * 1 - Success
2563 **/
dea31012005-04-17 16:05:31 -05002564static int
James Smartf1126682009-06-10 17:22:44 -04002565lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
dea31012005-04-17 16:05:31 -05002566 struct lpfc_scsi_buf *lpfc_cmd,
James Smart420b630d2006-07-06 15:50:16 -04002567 unsigned int lun,
dea31012005-04-17 16:05:31 -05002568 uint8_t task_mgmt_cmd)
2569{
dea31012005-04-17 16:05:31 -05002570 struct lpfc_iocbq *piocbq;
2571 IOCB_t *piocb;
2572 struct fcp_cmnd *fcp_cmnd;
James Smart0b18ac42006-05-01 21:50:40 -04002573 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea31012005-04-17 16:05:31 -05002574 struct lpfc_nodelist *ndlp = rdata->pnode;
2575
James Smart58da1ff2008-04-07 10:15:56 -04002576 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
2577 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05002578 return 0;
dea31012005-04-17 16:05:31 -05002579
dea31012005-04-17 16:05:31 -05002580 piocbq = &(lpfc_cmd->cur_iocbq);
James Smart2e0fef82007-06-17 19:56:36 -05002581 piocbq->vport = vport;
2582
dea31012005-04-17 16:05:31 -05002583 piocb = &piocbq->iocb;
2584
2585 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart34b02dc2008-08-24 21:49:55 -04002586 /* Clear out any old data in the FCP command area */
2587 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
2588 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05002589 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
James Smarte2a0a9d2008-12-04 22:40:02 -05002590 if (vport->phba->sli_rev == 3 &&
2591 !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04002592 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05002593 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
dea31012005-04-17 16:05:31 -05002594 piocb->ulpContext = ndlp->nlp_rpi;
2595 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
2596 piocb->ulpFCP2Rcvy = 1;
2597 }
2598 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
2599
2600 /* ulpTimeout is only one byte */
2601 if (lpfc_cmd->timeout > 0xff) {
2602 /*
2603 * Do not timeout the command at the firmware level.
2604 * The driver will provide the timeout mechanism.
2605 */
2606 piocb->ulpTimeout = 0;
James Smartf1126682009-06-10 17:22:44 -04002607 } else
dea31012005-04-17 16:05:31 -05002608 piocb->ulpTimeout = lpfc_cmd->timeout;
James Smartf1126682009-06-10 17:22:44 -04002609
2610 if (vport->phba->sli_rev == LPFC_SLI_REV4)
2611 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05002612
James Smart2e0fef82007-06-17 19:56:36 -05002613 return 1;
dea31012005-04-17 16:05:31 -05002614}
2615
James Smart9bad7672008-12-04 22:39:02 -05002616/**
James Smart3772a992009-05-22 14:50:54 -04002617 * lpfc_scsi_api_table_setup - Set up scsi api fucntion jump table
2618 * @phba: The hba struct for which this call is being executed.
2619 * @dev_grp: The HBA PCI-Device group number.
2620 *
2621 * This routine sets up the SCSI interface API function jump table in @phba
2622 * struct.
2623 * Returns: 0 - success, -ENODEV - failure.
2624 **/
2625int
2626lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
2627{
2628
James Smartf1126682009-06-10 17:22:44 -04002629 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
2630 phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
2631 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf;
2632
James Smart3772a992009-05-22 14:50:54 -04002633 switch (dev_grp) {
2634 case LPFC_PCI_DEV_LP:
2635 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s3;
2636 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04002637 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
2638 break;
James Smartda0436e2009-05-22 14:51:39 -04002639 case LPFC_PCI_DEV_OC:
2640 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s4;
2641 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04002642 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
2643 break;
James Smart3772a992009-05-22 14:50:54 -04002644 default:
2645 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2646 "1418 Invalid HBA PCI-device group: 0x%x\n",
2647 dev_grp);
2648 return -ENODEV;
2649 break;
2650 }
2651 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf;
2652 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
James Smart84d1b002010-02-12 14:42:33 -05002653 phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
James Smart3772a992009-05-22 14:50:54 -04002654 return 0;
2655}
2656
2657/**
James Smart3621a712009-04-06 18:47:14 -04002658 * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
James Smart9bad7672008-12-04 22:39:02 -05002659 * @phba: The Hba for which this call is being executed.
2660 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
2661 * @rspiocbq: Pointer to lpfc_iocbq data structure.
2662 *
2663 * This routine is IOCB completion routine for device reset and target reset
2664 * routine. This routine release scsi buffer associated with lpfc_cmd.
2665 **/
James Smart7054a602007-04-25 09:52:34 -04002666static void
2667lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
2668 struct lpfc_iocbq *cmdiocbq,
2669 struct lpfc_iocbq *rspiocbq)
2670{
2671 struct lpfc_scsi_buf *lpfc_cmd =
2672 (struct lpfc_scsi_buf *) cmdiocbq->context1;
2673 if (lpfc_cmd)
2674 lpfc_release_scsi_buf(phba, lpfc_cmd);
2675 return;
2676}
2677
James Smart9bad7672008-12-04 22:39:02 -05002678/**
James Smart3621a712009-04-06 18:47:14 -04002679 * lpfc_info - Info entry point of scsi_host_template data structure
James Smart9bad7672008-12-04 22:39:02 -05002680 * @host: The scsi host for which this call is being executed.
2681 *
2682 * This routine provides module information about hba.
2683 *
2684 * Reutrn code:
2685 * Pointer to char - Success.
2686 **/
dea31012005-04-17 16:05:31 -05002687const char *
2688lpfc_info(struct Scsi_Host *host)
2689{
James Smart2e0fef82007-06-17 19:56:36 -05002690 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
2691 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002692 int len;
2693 static char lpfcinfobuf[384];
2694
2695 memset(lpfcinfobuf,0,384);
2696 if (phba && phba->pcidev){
2697 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
2698 len = strlen(lpfcinfobuf);
2699 snprintf(lpfcinfobuf + len,
2700 384-len,
2701 " on PCI bus %02x device %02x irq %d",
2702 phba->pcidev->bus->number,
2703 phba->pcidev->devfn,
2704 phba->pcidev->irq);
2705 len = strlen(lpfcinfobuf);
2706 if (phba->Port[0]) {
2707 snprintf(lpfcinfobuf + len,
2708 384-len,
2709 " port %s",
2710 phba->Port);
2711 }
James Smart65467b62010-01-26 23:08:29 -05002712 len = strlen(lpfcinfobuf);
2713 if (phba->sli4_hba.link_state.logical_speed) {
2714 snprintf(lpfcinfobuf + len,
2715 384-len,
2716 " Logical Link Speed: %d Mbps",
2717 phba->sli4_hba.link_state.logical_speed * 10);
2718 }
dea31012005-04-17 16:05:31 -05002719 }
2720 return lpfcinfobuf;
2721}
2722
James Smart9bad7672008-12-04 22:39:02 -05002723/**
James Smart3621a712009-04-06 18:47:14 -04002724 * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
James Smart9bad7672008-12-04 22:39:02 -05002725 * @phba: The Hba for which this call is being executed.
2726 *
2727 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
2728 * The default value of cfg_poll_tmo is 10 milliseconds.
2729 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002730static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
2731{
2732 unsigned long poll_tmo_expires =
2733 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
2734
2735 if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
2736 mod_timer(&phba->fcp_poll_timer,
2737 poll_tmo_expires);
2738}
2739
James Smart9bad7672008-12-04 22:39:02 -05002740/**
James Smart3621a712009-04-06 18:47:14 -04002741 * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
James Smart9bad7672008-12-04 22:39:02 -05002742 * @phba: The Hba for which this call is being executed.
2743 *
2744 * This routine starts the fcp_poll_timer of @phba.
2745 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002746void lpfc_poll_start_timer(struct lpfc_hba * phba)
2747{
2748 lpfc_poll_rearm_timer(phba);
2749}
2750
James Smart9bad7672008-12-04 22:39:02 -05002751/**
James Smart3621a712009-04-06 18:47:14 -04002752 * lpfc_poll_timeout - Restart polling timer
James Smart9bad7672008-12-04 22:39:02 -05002753 * @ptr: Map to lpfc_hba data structure pointer.
2754 *
2755 * This routine restarts fcp_poll timer, when FCP ring polling is enable
2756 * and FCP Ring interrupt is disable.
2757 **/
2758
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002759void lpfc_poll_timeout(unsigned long ptr)
2760{
James Smart2e0fef82007-06-17 19:56:36 -05002761 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002762
2763 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04002764 lpfc_sli_handle_fast_ring_event(phba,
2765 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
2766
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002767 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
2768 lpfc_poll_rearm_timer(phba);
2769 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002770}
2771
James Smart9bad7672008-12-04 22:39:02 -05002772/**
James Smart3621a712009-04-06 18:47:14 -04002773 * lpfc_queuecommand - scsi_host_template queuecommand entry point
James Smart9bad7672008-12-04 22:39:02 -05002774 * @cmnd: Pointer to scsi_cmnd data structure.
2775 * @done: Pointer to done routine.
2776 *
2777 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
2778 * This routine prepares an IOCB from scsi command and provides to firmware.
2779 * The @done callback is invoked after driver finished processing the command.
2780 *
2781 * Return value :
2782 * 0 - Success
2783 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
2784 **/
dea31012005-04-17 16:05:31 -05002785static int
2786lpfc_queuecommand(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
2787{
James Smart2e0fef82007-06-17 19:56:36 -05002788 struct Scsi_Host *shost = cmnd->device->host;
2789 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2790 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002791 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05002792 struct lpfc_nodelist *ndlp;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002793 struct lpfc_scsi_buf *lpfc_cmd;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002794 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002795 int err;
dea31012005-04-17 16:05:31 -05002796
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002797 err = fc_remote_port_chkready(rport);
2798 if (err) {
2799 cmnd->result = err;
dea31012005-04-17 16:05:31 -05002800 goto out_fail_command;
2801 }
James Smart1c6f4ef52009-11-18 15:40:49 -05002802 ndlp = rdata->pnode;
dea31012005-04-17 16:05:31 -05002803
James Smarte2a0a9d2008-12-04 22:40:02 -05002804 if (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
2805 scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
2806
James Smart6a9c52c2009-10-02 15:16:51 -04002807 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2808 "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
2809 " op:%02x str=%s without registering for"
2810 " BlockGuard - Rejecting command\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002811 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
2812 dif_op_str[scsi_get_prot_op(cmnd)]);
2813 goto out_fail_command;
2814 }
2815
dea31012005-04-17 16:05:31 -05002816 /*
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002817 * Catch race where our node has transitioned, but the
2818 * transport is still transitioning.
dea31012005-04-17 16:05:31 -05002819 */
James Smartb522d7d2008-09-07 11:51:56 -04002820 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
2821 cmnd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
2822 goto out_fail_command;
2823 }
James Smart109f6ed2008-12-04 22:39:08 -05002824 if (vport->cfg_max_scsicmpl_time &&
2825 (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth))
James Smart977b5a02008-09-07 11:52:04 -04002826 goto out_host_busy;
Mike Christiea93ce022008-08-17 15:24:41 -05002827
James Smarted957682007-06-17 19:56:37 -05002828 lpfc_cmd = lpfc_get_scsi_buf(phba);
dea31012005-04-17 16:05:31 -05002829 if (lpfc_cmd == NULL) {
James Smarteaf15d52008-12-04 22:39:29 -05002830 lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002831
James Smarte8b62012007-08-02 11:10:09 -04002832 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
2833 "0707 driver's buffer pool is empty, "
2834 "IO busied\n");
dea31012005-04-17 16:05:31 -05002835 goto out_host_busy;
2836 }
2837
2838 /*
2839 * Store the midlayer's command structure for the completion phase
2840 * and complete the command initialization.
2841 */
2842 lpfc_cmd->pCmd = cmnd;
2843 lpfc_cmd->rdata = rdata;
2844 lpfc_cmd->timeout = 0;
James Smart977b5a02008-09-07 11:52:04 -04002845 lpfc_cmd->start_time = jiffies;
dea31012005-04-17 16:05:31 -05002846 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
2847 cmnd->scsi_done = done;
2848
James Smarte2a0a9d2008-12-04 22:40:02 -05002849 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
James Smart6a9c52c2009-10-02 15:16:51 -04002850 if (vport->phba->cfg_enable_bg) {
2851 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05002852 "9033 BLKGRD: rcvd protected cmd:%02x op:%02x "
2853 "str=%s\n",
2854 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
2855 dif_op_str[scsi_get_prot_op(cmnd)]);
James Smart6a9c52c2009-10-02 15:16:51 -04002856 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05002857 "9034 BLKGRD: CDB: %02x %02x %02x %02x %02x "
James Smarte4e74272009-07-19 10:01:38 -04002858 "%02x %02x %02x %02x %02x\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002859 cmnd->cmnd[0], cmnd->cmnd[1], cmnd->cmnd[2],
2860 cmnd->cmnd[3], cmnd->cmnd[4], cmnd->cmnd[5],
2861 cmnd->cmnd[6], cmnd->cmnd[7], cmnd->cmnd[8],
2862 cmnd->cmnd[9]);
James Smart6a9c52c2009-10-02 15:16:51 -04002863 if (cmnd->cmnd[0] == READ_10)
2864 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05002865 "9035 BLKGRD: READ @ sector %llu, "
Tejun Heo83096eb2009-05-07 22:24:39 +09002866 "count %u\n",
2867 (unsigned long long)scsi_get_lba(cmnd),
2868 blk_rq_sectors(cmnd->request));
James Smart6a9c52c2009-10-02 15:16:51 -04002869 else if (cmnd->cmnd[0] == WRITE_10)
2870 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05002871 "9036 BLKGRD: WRITE @ sector %llu, "
Tejun Heo83096eb2009-05-07 22:24:39 +09002872 "count %u cmd=%p\n",
James Smart87b5c322008-12-16 10:34:09 -05002873 (unsigned long long)scsi_get_lba(cmnd),
Tejun Heo83096eb2009-05-07 22:24:39 +09002874 blk_rq_sectors(cmnd->request),
James Smarte2a0a9d2008-12-04 22:40:02 -05002875 cmnd);
James Smart6a9c52c2009-10-02 15:16:51 -04002876 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002877
2878 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
2879 } else {
James Smart6a9c52c2009-10-02 15:16:51 -04002880 if (vport->phba->cfg_enable_bg) {
James Smarte2a0a9d2008-12-04 22:40:02 -05002881 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smart6a9c52c2009-10-02 15:16:51 -04002882 "9038 BLKGRD: rcvd unprotected cmd:"
2883 "%02x op:%02x str=%s\n",
2884 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
2885 dif_op_str[scsi_get_prot_op(cmnd)]);
2886 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
2887 "9039 BLKGRD: CDB: %02x %02x %02x "
2888 "%02x %02x %02x %02x %02x %02x %02x\n",
2889 cmnd->cmnd[0], cmnd->cmnd[1],
2890 cmnd->cmnd[2], cmnd->cmnd[3],
2891 cmnd->cmnd[4], cmnd->cmnd[5],
2892 cmnd->cmnd[6], cmnd->cmnd[7],
2893 cmnd->cmnd[8], cmnd->cmnd[9]);
2894 if (cmnd->cmnd[0] == READ_10)
2895 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
2896 "9040 dbg: READ @ sector %llu, "
2897 "count %u\n",
2898 (unsigned long long)scsi_get_lba(cmnd),
Tejun Heo83096eb2009-05-07 22:24:39 +09002899 blk_rq_sectors(cmnd->request));
James Smart6a9c52c2009-10-02 15:16:51 -04002900 else if (cmnd->cmnd[0] == WRITE_10)
2901 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05002902 "9041 dbg: WRITE @ sector %llu, "
Tejun Heo83096eb2009-05-07 22:24:39 +09002903 "count %u cmd=%p\n",
James Smart87b5c322008-12-16 10:34:09 -05002904 (unsigned long long)scsi_get_lba(cmnd),
Tejun Heo83096eb2009-05-07 22:24:39 +09002905 blk_rq_sectors(cmnd->request), cmnd);
James Smart6a9c52c2009-10-02 15:16:51 -04002906 else
2907 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05002908 "9042 dbg: parser not implemented\n");
James Smart6a9c52c2009-10-02 15:16:51 -04002909 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002910 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
2911 }
2912
dea31012005-04-17 16:05:31 -05002913 if (err)
2914 goto out_host_busy_free_buf;
2915
James Smart2e0fef82007-06-17 19:56:36 -05002916 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea31012005-04-17 16:05:31 -05002917
James Smart977b5a02008-09-07 11:52:04 -04002918 atomic_inc(&ndlp->cmd_pending);
James Smart3772a992009-05-22 14:50:54 -04002919 err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
James Smart92d7f7b2007-06-17 19:56:38 -05002920 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
James Smarteaf15d52008-12-04 22:39:29 -05002921 if (err) {
2922 atomic_dec(&ndlp->cmd_pending);
dea31012005-04-17 16:05:31 -05002923 goto out_host_busy_free_buf;
James Smarteaf15d52008-12-04 22:39:29 -05002924 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002925 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04002926 spin_unlock(shost->host_lock);
2927 lpfc_sli_handle_fast_ring_event(phba,
2928 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
2929
2930 spin_lock(shost->host_lock);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002931 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
2932 lpfc_poll_rearm_timer(phba);
2933 }
2934
dea31012005-04-17 16:05:31 -05002935 return 0;
2936
2937 out_host_busy_free_buf:
James Smartbcf4dbf2006-07-06 15:50:08 -04002938 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002939 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05002940 out_host_busy:
2941 return SCSI_MLQUEUE_HOST_BUSY;
2942
2943 out_fail_command:
2944 done(cmnd);
2945 return 0;
2946}
2947
James Smart9bad7672008-12-04 22:39:02 -05002948/**
James Smart3621a712009-04-06 18:47:14 -04002949 * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05002950 * @cmnd: Pointer to scsi_cmnd data structure.
2951 *
2952 * This routine aborts @cmnd pending in base driver.
2953 *
2954 * Return code :
2955 * 0x2003 - Error
2956 * 0x2002 - Success
2957 **/
dea31012005-04-17 16:05:31 -05002958static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05002959lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05002960{
James Smart2e0fef82007-06-17 19:56:36 -05002961 struct Scsi_Host *shost = cmnd->device->host;
2962 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2963 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002964 struct lpfc_iocbq *iocb;
2965 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05002966 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05002967 IOCB_t *cmd, *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002968 int ret = SUCCESS;
James Smartfa61a542008-01-11 01:52:42 -05002969 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002970
Christof Schmitt65d430f2009-10-30 17:59:29 +01002971 fc_block_scsi_eh(cmnd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002972 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
2973 BUG_ON(!lpfc_cmd);
dea31012005-04-17 16:05:31 -05002974
2975 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002976 * If pCmd field of the corresponding lpfc_scsi_buf structure
2977 * points to a different SCSI command, then the driver has
2978 * already completed this command, but the midlayer did not
2979 * see the completion before the eh fired. Just return
2980 * SUCCESS.
dea31012005-04-17 16:05:31 -05002981 */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002982 iocb = &lpfc_cmd->cur_iocbq;
2983 if (lpfc_cmd->pCmd != cmnd)
2984 goto out;
dea31012005-04-17 16:05:31 -05002985
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002986 BUG_ON(iocb->context1 != lpfc_cmd);
dea31012005-04-17 16:05:31 -05002987
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002988 abtsiocb = lpfc_sli_get_iocbq(phba);
2989 if (abtsiocb == NULL) {
2990 ret = FAILED;
dea31012005-04-17 16:05:31 -05002991 goto out;
2992 }
2993
dea31012005-04-17 16:05:31 -05002994 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002995 * The scsi command can not be in txq and it is in flight because the
2996 * pCmd is still pointig at the SCSI command we have to abort. There
2997 * is no need to search the txcmplq. Just send an abort to the FW.
dea31012005-04-17 16:05:31 -05002998 */
dea31012005-04-17 16:05:31 -05002999
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003000 cmd = &iocb->iocb;
3001 icmd = &abtsiocb->iocb;
3002 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
3003 icmd->un.acxri.abortContextTag = cmd->ulpContext;
James Smart3772a992009-05-22 14:50:54 -04003004 if (phba->sli_rev == LPFC_SLI_REV4)
3005 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
3006 else
3007 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05003008
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003009 icmd->ulpLe = 1;
3010 icmd->ulpClass = cmd->ulpClass;
James Smart5ffc2662009-11-18 15:39:44 -05003011
3012 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
3013 abtsiocb->fcp_wqidx = iocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05003014 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05003015
James Smart2e0fef82007-06-17 19:56:36 -05003016 if (lpfc_is_link_up(phba))
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003017 icmd->ulpCommand = CMD_ABORT_XRI_CN;
3018 else
3019 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea31012005-04-17 16:05:31 -05003020
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003021 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smart2e0fef82007-06-17 19:56:36 -05003022 abtsiocb->vport = vport;
James Smart3772a992009-05-22 14:50:54 -04003023 if (lpfc_sli_issue_iocb(phba, LPFC_FCP_RING, abtsiocb, 0) ==
3024 IOCB_ERROR) {
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003025 lpfc_sli_release_iocbq(phba, abtsiocb);
3026 ret = FAILED;
3027 goto out;
3028 }
3029
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003030 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
James Smart45ed1192009-10-02 15:17:02 -04003031 lpfc_sli_handle_fast_ring_event(phba,
3032 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003033
James Smartfa61a542008-01-11 01:52:42 -05003034 lpfc_cmd->waitq = &waitq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003035 /* Wait for abort to complete */
James Smartfa61a542008-01-11 01:52:42 -05003036 wait_event_timeout(waitq,
3037 (lpfc_cmd->pCmd != cmnd),
3038 (2*vport->cfg_devloss_tmo*HZ));
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003039
James Smartfa61a542008-01-11 01:52:42 -05003040 spin_lock_irq(shost->host_lock);
3041 lpfc_cmd->waitq = NULL;
3042 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003043
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003044 if (lpfc_cmd->pCmd == cmnd) {
3045 ret = FAILED;
James Smarte8b62012007-08-02 11:10:09 -04003046 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3047 "0748 abort handler timed out waiting "
3048 "for abort to complete: ret %#x, ID %d, "
3049 "LUN %d, snum %#lx\n",
3050 ret, cmnd->device->id, cmnd->device->lun,
3051 cmnd->serial_number);
dea31012005-04-17 16:05:31 -05003052 }
3053
3054 out:
James Smarte8b62012007-08-02 11:10:09 -04003055 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3056 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
3057 "LUN %d snum %#lx\n", ret, cmnd->device->id,
3058 cmnd->device->lun, cmnd->serial_number);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003059 return ret;
dea31012005-04-17 16:05:31 -05003060}
3061
James Smartbbb9d182009-06-10 17:23:16 -04003062static char *
3063lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
3064{
3065 switch (task_mgmt_cmd) {
3066 case FCP_ABORT_TASK_SET:
3067 return "ABORT_TASK_SET";
3068 case FCP_CLEAR_TASK_SET:
3069 return "FCP_CLEAR_TASK_SET";
3070 case FCP_BUS_RESET:
3071 return "FCP_BUS_RESET";
3072 case FCP_LUN_RESET:
3073 return "FCP_LUN_RESET";
3074 case FCP_TARGET_RESET:
3075 return "FCP_TARGET_RESET";
3076 case FCP_CLEAR_ACA:
3077 return "FCP_CLEAR_ACA";
3078 case FCP_TERMINATE_TASK:
3079 return "FCP_TERMINATE_TASK";
3080 default:
3081 return "unknown";
3082 }
3083}
3084
3085/**
3086 * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
3087 * @vport: The virtual port for which this call is being executed.
3088 * @rdata: Pointer to remote port local data
3089 * @tgt_id: Target ID of remote device.
3090 * @lun_id: Lun number for the TMF
3091 * @task_mgmt_cmd: type of TMF to send
3092 *
3093 * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
3094 * a remote port.
3095 *
3096 * Return Code:
3097 * 0x2003 - Error
3098 * 0x2002 - Success.
3099 **/
3100static int
3101lpfc_send_taskmgmt(struct lpfc_vport *vport, struct lpfc_rport_data *rdata,
3102 unsigned tgt_id, unsigned int lun_id,
3103 uint8_t task_mgmt_cmd)
3104{
3105 struct lpfc_hba *phba = vport->phba;
3106 struct lpfc_scsi_buf *lpfc_cmd;
3107 struct lpfc_iocbq *iocbq;
3108 struct lpfc_iocbq *iocbqrsp;
3109 int ret;
3110 int status;
3111
3112 if (!rdata->pnode || !NLP_CHK_NODE_ACT(rdata->pnode))
3113 return FAILED;
3114
3115 lpfc_cmd = lpfc_get_scsi_buf(phba);
3116 if (lpfc_cmd == NULL)
3117 return FAILED;
3118 lpfc_cmd->timeout = 60;
3119 lpfc_cmd->rdata = rdata;
3120
3121 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
3122 task_mgmt_cmd);
3123 if (!status) {
3124 lpfc_release_scsi_buf(phba, lpfc_cmd);
3125 return FAILED;
3126 }
3127
3128 iocbq = &lpfc_cmd->cur_iocbq;
3129 iocbqrsp = lpfc_sli_get_iocbq(phba);
3130 if (iocbqrsp == NULL) {
3131 lpfc_release_scsi_buf(phba, lpfc_cmd);
3132 return FAILED;
3133 }
3134
3135 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
3136 "0702 Issue %s to TGT %d LUN %d "
3137 "rpi x%x nlp_flag x%x\n",
3138 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
3139 rdata->pnode->nlp_rpi, rdata->pnode->nlp_flag);
3140
3141 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
3142 iocbq, iocbqrsp, lpfc_cmd->timeout);
3143 if (status != IOCB_SUCCESS) {
3144 if (status == IOCB_TIMEDOUT) {
3145 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
3146 ret = TIMEOUT_ERROR;
3147 } else
3148 ret = FAILED;
3149 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
3150 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3151 "0727 TMF %s to TGT %d LUN %d failed (%d, %d)\n",
3152 lpfc_taskmgmt_name(task_mgmt_cmd),
3153 tgt_id, lun_id, iocbqrsp->iocb.ulpStatus,
3154 iocbqrsp->iocb.un.ulpWord[4]);
3155 } else
3156 ret = SUCCESS;
3157
3158 lpfc_sli_release_iocbq(phba, iocbqrsp);
3159
3160 if (ret != TIMEOUT_ERROR)
3161 lpfc_release_scsi_buf(phba, lpfc_cmd);
3162
3163 return ret;
3164}
3165
3166/**
3167 * lpfc_chk_tgt_mapped -
3168 * @vport: The virtual port to check on
3169 * @cmnd: Pointer to scsi_cmnd data structure.
3170 *
3171 * This routine delays until the scsi target (aka rport) for the
3172 * command exists (is present and logged in) or we declare it non-existent.
3173 *
3174 * Return code :
3175 * 0x2003 - Error
3176 * 0x2002 - Success
3177 **/
3178static int
3179lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
3180{
3181 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05003182 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04003183 unsigned long later;
3184
James Smart1c6f4ef52009-11-18 15:40:49 -05003185 if (!rdata) {
3186 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
3187 "0797 Tgt Map rport failure: rdata x%p\n", rdata);
3188 return FAILED;
3189 }
3190 pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04003191 /*
3192 * If target is not in a MAPPED state, delay until
3193 * target is rediscovered or devloss timeout expires.
3194 */
3195 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
3196 while (time_after(later, jiffies)) {
3197 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3198 return FAILED;
3199 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
3200 return SUCCESS;
3201 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
3202 rdata = cmnd->device->hostdata;
3203 if (!rdata)
3204 return FAILED;
3205 pnode = rdata->pnode;
3206 }
3207 if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
3208 (pnode->nlp_state != NLP_STE_MAPPED_NODE))
3209 return FAILED;
3210 return SUCCESS;
3211}
3212
3213/**
3214 * lpfc_reset_flush_io_context -
3215 * @vport: The virtual port (scsi_host) for the flush context
3216 * @tgt_id: If aborting by Target contect - specifies the target id
3217 * @lun_id: If aborting by Lun context - specifies the lun id
3218 * @context: specifies the context level to flush at.
3219 *
3220 * After a reset condition via TMF, we need to flush orphaned i/o
3221 * contexts from the adapter. This routine aborts any contexts
3222 * outstanding, then waits for their completions. The wait is
3223 * bounded by devloss_tmo though.
3224 *
3225 * Return code :
3226 * 0x2003 - Error
3227 * 0x2002 - Success
3228 **/
3229static int
3230lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
3231 uint64_t lun_id, lpfc_ctx_cmd context)
3232{
3233 struct lpfc_hba *phba = vport->phba;
3234 unsigned long later;
3235 int cnt;
3236
3237 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
3238 if (cnt)
3239 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
3240 tgt_id, lun_id, context);
3241 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
3242 while (time_after(later, jiffies) && cnt) {
3243 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
3244 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
3245 }
3246 if (cnt) {
3247 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3248 "0724 I/O flush failure for context %s : cnt x%x\n",
3249 ((context == LPFC_CTX_LUN) ? "LUN" :
3250 ((context == LPFC_CTX_TGT) ? "TGT" :
3251 ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
3252 cnt);
3253 return FAILED;
3254 }
3255 return SUCCESS;
3256}
3257
James Smart9bad7672008-12-04 22:39:02 -05003258/**
James Smart3621a712009-04-06 18:47:14 -04003259 * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
James Smart9bad7672008-12-04 22:39:02 -05003260 * @cmnd: Pointer to scsi_cmnd data structure.
3261 *
James Smartbbb9d182009-06-10 17:23:16 -04003262 * This routine does a device reset by sending a LUN_RESET task management
James Smart9bad7672008-12-04 22:39:02 -05003263 * command.
3264 *
3265 * Return code :
3266 * 0x2003 - Error
James Smart3621a712009-04-06 18:47:14 -04003267 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05003268 **/
dea31012005-04-17 16:05:31 -05003269static int
James Smart7054a602007-04-25 09:52:34 -04003270lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05003271{
James Smart2e0fef82007-06-17 19:56:36 -05003272 struct Scsi_Host *shost = cmnd->device->host;
3273 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05003274 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05003275 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04003276 unsigned tgt_id = cmnd->device->id;
3277 unsigned int lun_id = cmnd->device->lun;
James Smartea2151b2008-09-07 11:52:10 -04003278 struct lpfc_scsi_event_header scsi_event;
James Smartbbb9d182009-06-10 17:23:16 -04003279 int status;
dea31012005-04-17 16:05:31 -05003280
James Smart1c6f4ef52009-11-18 15:40:49 -05003281 if (!rdata) {
3282 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3283 "0798 Device Reset rport failure: rdata x%p\n", rdata);
3284 return FAILED;
3285 }
3286 pnode = rdata->pnode;
Christof Schmitt65d430f2009-10-30 17:59:29 +01003287 fc_block_scsi_eh(cmnd);
James Smartbbb9d182009-06-10 17:23:16 -04003288
3289 status = lpfc_chk_tgt_mapped(vport, cmnd);
3290 if (status == FAILED) {
3291 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3292 "0721 Device Reset rport failure: rdata x%p\n", rdata);
3293 return FAILED;
3294 }
3295
3296 scsi_event.event_type = FC_REG_SCSI_EVENT;
3297 scsi_event.subcategory = LPFC_EVENT_LUNRESET;
3298 scsi_event.lun = lun_id;
3299 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
3300 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
3301
3302 fc_host_post_vendor_event(shost, fc_get_event_number(),
3303 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
3304
3305 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
3306 FCP_LUN_RESET);
3307
3308 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3309 "0713 SCSI layer issued Device Reset (%d, %d) "
3310 "return x%x\n", tgt_id, lun_id, status);
3311
dea31012005-04-17 16:05:31 -05003312 /*
James Smartbbb9d182009-06-10 17:23:16 -04003313 * We have to clean up i/o as : they may be orphaned by the TMF;
3314 * or if the TMF failed, they may be in an indeterminate state.
3315 * So, continue on.
3316 * We will report success if all the i/o aborts successfully.
dea31012005-04-17 16:05:31 -05003317 */
James Smartbbb9d182009-06-10 17:23:16 -04003318 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
3319 LPFC_CTX_LUN);
3320 return status;
3321}
3322
3323/**
3324 * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
3325 * @cmnd: Pointer to scsi_cmnd data structure.
3326 *
3327 * This routine does a target reset by sending a TARGET_RESET task management
3328 * command.
3329 *
3330 * Return code :
3331 * 0x2003 - Error
3332 * 0x2002 - Success
3333 **/
3334static int
3335lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
3336{
3337 struct Scsi_Host *shost = cmnd->device->host;
3338 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3339 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05003340 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04003341 unsigned tgt_id = cmnd->device->id;
3342 unsigned int lun_id = cmnd->device->lun;
3343 struct lpfc_scsi_event_header scsi_event;
3344 int status;
3345
James Smart1c6f4ef52009-11-18 15:40:49 -05003346 if (!rdata) {
3347 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3348 "0799 Target Reset rport failure: rdata x%p\n", rdata);
3349 return FAILED;
3350 }
3351 pnode = rdata->pnode;
Christof Schmitt65d430f2009-10-30 17:59:29 +01003352 fc_block_scsi_eh(cmnd);
James Smartbbb9d182009-06-10 17:23:16 -04003353
3354 status = lpfc_chk_tgt_mapped(vport, cmnd);
3355 if (status == FAILED) {
3356 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3357 "0722 Target Reset rport failure: rdata x%p\n", rdata);
3358 return FAILED;
dea31012005-04-17 16:05:31 -05003359 }
James Smartea2151b2008-09-07 11:52:10 -04003360
3361 scsi_event.event_type = FC_REG_SCSI_EVENT;
3362 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
3363 scsi_event.lun = 0;
3364 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
3365 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
3366
James Smartbbb9d182009-06-10 17:23:16 -04003367 fc_host_post_vendor_event(shost, fc_get_event_number(),
3368 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -04003369
James Smartbbb9d182009-06-10 17:23:16 -04003370 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
3371 FCP_TARGET_RESET);
dea31012005-04-17 16:05:31 -05003372
James Smarte8b62012007-08-02 11:10:09 -04003373 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smartbbb9d182009-06-10 17:23:16 -04003374 "0723 SCSI layer issued Target Reset (%d, %d) "
3375 "return x%x\n", tgt_id, lun_id, status);
3376
3377 /*
3378 * We have to clean up i/o as : they may be orphaned by the TMF;
3379 * or if the TMF failed, they may be in an indeterminate state.
3380 * So, continue on.
3381 * We will report success if all the i/o aborts successfully.
3382 */
3383 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
3384 LPFC_CTX_TGT);
3385 return status;
dea31012005-04-17 16:05:31 -05003386}
3387
James Smart9bad7672008-12-04 22:39:02 -05003388/**
James Smart3621a712009-04-06 18:47:14 -04003389 * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05003390 * @cmnd: Pointer to scsi_cmnd data structure.
3391 *
James Smartbbb9d182009-06-10 17:23:16 -04003392 * This routine does target reset to all targets on @cmnd->device->host.
3393 * This emulates Parallel SCSI Bus Reset Semantics.
James Smart9bad7672008-12-04 22:39:02 -05003394 *
James Smartbbb9d182009-06-10 17:23:16 -04003395 * Return code :
3396 * 0x2003 - Error
3397 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05003398 **/
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04003399static int
James Smart7054a602007-04-25 09:52:34 -04003400lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05003401{
James Smart2e0fef82007-06-17 19:56:36 -05003402 struct Scsi_Host *shost = cmnd->device->host;
3403 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05003404 struct lpfc_nodelist *ndlp = NULL;
James Smartea2151b2008-09-07 11:52:10 -04003405 struct lpfc_scsi_event_header scsi_event;
James Smartbbb9d182009-06-10 17:23:16 -04003406 int match;
3407 int ret = SUCCESS, status, i;
James Smartea2151b2008-09-07 11:52:10 -04003408
3409 scsi_event.event_type = FC_REG_SCSI_EVENT;
3410 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
3411 scsi_event.lun = 0;
3412 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
3413 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
3414
James Smartbbb9d182009-06-10 17:23:16 -04003415 fc_host_post_vendor_event(shost, fc_get_event_number(),
3416 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
dea31012005-04-17 16:05:31 -05003417
Christof Schmitt65d430f2009-10-30 17:59:29 +01003418 fc_block_scsi_eh(cmnd);
James Smartbbb9d182009-06-10 17:23:16 -04003419
dea31012005-04-17 16:05:31 -05003420 /*
3421 * Since the driver manages a single bus device, reset all
3422 * targets known to the driver. Should any target reset
3423 * fail, this routine returns failure to the midlayer.
3424 */
James Smarte17da182006-07-06 15:49:25 -04003425 for (i = 0; i < LPFC_MAX_TARGET; i++) {
James Smart685f0bf2007-04-25 09:53:08 -04003426 /* Search for mapped node by target ID */
dea31012005-04-17 16:05:31 -05003427 match = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003428 spin_lock_irq(shost->host_lock);
3429 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05003430 if (!NLP_CHK_NODE_ACT(ndlp))
3431 continue;
James Smart685f0bf2007-04-25 09:53:08 -04003432 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
James Smart915caaa2008-06-14 22:52:38 -04003433 ndlp->nlp_sid == i &&
James Smart685f0bf2007-04-25 09:53:08 -04003434 ndlp->rport) {
dea31012005-04-17 16:05:31 -05003435 match = 1;
3436 break;
3437 }
3438 }
James Smart2e0fef82007-06-17 19:56:36 -05003439 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003440 if (!match)
3441 continue;
James Smartbbb9d182009-06-10 17:23:16 -04003442
3443 status = lpfc_send_taskmgmt(vport, ndlp->rport->dd_data,
3444 i, 0, FCP_TARGET_RESET);
3445
3446 if (status != SUCCESS) {
James Smarte8b62012007-08-02 11:10:09 -04003447 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3448 "0700 Bus Reset on target %d failed\n",
3449 i);
James Smart915caaa2008-06-14 22:52:38 -04003450 ret = FAILED;
dea31012005-04-17 16:05:31 -05003451 }
3452 }
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05003453 /*
James Smartbbb9d182009-06-10 17:23:16 -04003454 * We have to clean up i/o as : they may be orphaned by the TMFs
3455 * above; or if any of the TMFs failed, they may be in an
3456 * indeterminate state.
3457 * We will report success if all the i/o aborts successfully.
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05003458 */
James Smartbbb9d182009-06-10 17:23:16 -04003459
3460 status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
3461 if (status != SUCCESS)
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05003462 ret = FAILED;
James Smartbbb9d182009-06-10 17:23:16 -04003463
James Smarte8b62012007-08-02 11:10:09 -04003464 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3465 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea31012005-04-17 16:05:31 -05003466 return ret;
3467}
3468
James Smart9bad7672008-12-04 22:39:02 -05003469/**
James Smart3621a712009-04-06 18:47:14 -04003470 * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
James Smart9bad7672008-12-04 22:39:02 -05003471 * @sdev: Pointer to scsi_device.
3472 *
3473 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
3474 * globally available list of scsi buffers. This routine also makes sure scsi
3475 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
3476 * of scsi buffer exists for the lifetime of the driver.
3477 *
3478 * Return codes:
3479 * non-0 - Error
3480 * 0 - Success
3481 **/
dea31012005-04-17 16:05:31 -05003482static int
dea31012005-04-17 16:05:31 -05003483lpfc_slave_alloc(struct scsi_device *sdev)
3484{
James Smart2e0fef82007-06-17 19:56:36 -05003485 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
3486 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003487 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
James Smart3772a992009-05-22 14:50:54 -04003488 uint32_t total = 0;
dea31012005-04-17 16:05:31 -05003489 uint32_t num_to_alloc = 0;
James Smart3772a992009-05-22 14:50:54 -04003490 int num_allocated = 0;
dea31012005-04-17 16:05:31 -05003491
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003492 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05003493 return -ENXIO;
3494
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003495 sdev->hostdata = rport->dd_data;
dea31012005-04-17 16:05:31 -05003496
3497 /*
3498 * Populate the cmds_per_lun count scsi_bufs into this host's globally
3499 * available list of scsi buffers. Don't allocate more than the
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04003500 * HBA limit conveyed to the midlayer via the host structure. The
3501 * formula accounts for the lun_queue_depth + error handlers + 1
3502 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea31012005-04-17 16:05:31 -05003503 */
3504 total = phba->total_scsi_bufs;
James Smart3de2a652007-08-02 11:09:59 -04003505 num_to_alloc = vport->cfg_lun_queue_depth + 2;
James Smart92d7f7b2007-06-17 19:56:38 -05003506
3507 /* Allow some exchanges to be available always to complete discovery */
3508 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04003509 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3510 "0704 At limitation of %d preallocated "
3511 "command buffers\n", total);
dea31012005-04-17 16:05:31 -05003512 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05003513 /* Allow some exchanges to be available always to complete discovery */
3514 } else if (total + num_to_alloc >
3515 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04003516 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3517 "0705 Allocation request of %d "
3518 "command buffers will exceed max of %d. "
3519 "Reducing allocation request to %d.\n",
3520 num_to_alloc, phba->cfg_hba_queue_depth,
3521 (phba->cfg_hba_queue_depth - total));
dea31012005-04-17 16:05:31 -05003522 num_to_alloc = phba->cfg_hba_queue_depth - total;
3523 }
James Smart3772a992009-05-22 14:50:54 -04003524 num_allocated = lpfc_new_scsi_buf(vport, num_to_alloc);
3525 if (num_to_alloc != num_allocated) {
3526 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3527 "0708 Allocation request of %d "
3528 "command buffers did not succeed. "
3529 "Allocated %d buffers.\n",
3530 num_to_alloc, num_allocated);
dea31012005-04-17 16:05:31 -05003531 }
James Smart1c6f4ef52009-11-18 15:40:49 -05003532 if (num_allocated > 0)
3533 phba->total_scsi_bufs += num_allocated;
dea31012005-04-17 16:05:31 -05003534 return 0;
3535}
3536
James Smart9bad7672008-12-04 22:39:02 -05003537/**
James Smart3621a712009-04-06 18:47:14 -04003538 * lpfc_slave_configure - scsi_host_template slave_configure entry point
James Smart9bad7672008-12-04 22:39:02 -05003539 * @sdev: Pointer to scsi_device.
3540 *
3541 * This routine configures following items
3542 * - Tag command queuing support for @sdev if supported.
3543 * - Dev loss time out value of fc_rport.
3544 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
3545 *
3546 * Return codes:
3547 * 0 - Success
3548 **/
dea31012005-04-17 16:05:31 -05003549static int
3550lpfc_slave_configure(struct scsi_device *sdev)
3551{
James Smart2e0fef82007-06-17 19:56:36 -05003552 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
3553 struct lpfc_hba *phba = vport->phba;
3554 struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
dea31012005-04-17 16:05:31 -05003555
3556 if (sdev->tagged_supported)
James Smart3de2a652007-08-02 11:09:59 -04003557 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05003558 else
James Smart3de2a652007-08-02 11:09:59 -04003559 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05003560
3561 /*
3562 * Initialize the fc transport attributes for the target
3563 * containing this scsi device. Also note that the driver's
3564 * target pointer is stored in the starget_data for the
3565 * driver's sysfs entry point functions.
3566 */
James Smart3de2a652007-08-02 11:09:59 -04003567 rport->dev_loss_tmo = vport->cfg_devloss_tmo;
dea31012005-04-17 16:05:31 -05003568
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003569 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04003570 lpfc_sli_handle_fast_ring_event(phba,
3571 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003572 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
3573 lpfc_poll_rearm_timer(phba);
3574 }
3575
dea31012005-04-17 16:05:31 -05003576 return 0;
3577}
3578
James Smart9bad7672008-12-04 22:39:02 -05003579/**
James Smart3621a712009-04-06 18:47:14 -04003580 * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
James Smart9bad7672008-12-04 22:39:02 -05003581 * @sdev: Pointer to scsi_device.
3582 *
3583 * This routine sets @sdev hostatdata filed to null.
3584 **/
dea31012005-04-17 16:05:31 -05003585static void
3586lpfc_slave_destroy(struct scsi_device *sdev)
3587{
3588 sdev->hostdata = NULL;
3589 return;
3590}
3591
James Smart92d7f7b2007-06-17 19:56:38 -05003592
dea31012005-04-17 16:05:31 -05003593struct scsi_host_template lpfc_template = {
3594 .module = THIS_MODULE,
3595 .name = LPFC_DRIVER_NAME,
3596 .info = lpfc_info,
3597 .queuecommand = lpfc_queuecommand,
3598 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04003599 .eh_device_reset_handler = lpfc_device_reset_handler,
3600 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart7054a602007-04-25 09:52:34 -04003601 .eh_bus_reset_handler = lpfc_bus_reset_handler,
dea31012005-04-17 16:05:31 -05003602 .slave_alloc = lpfc_slave_alloc,
3603 .slave_configure = lpfc_slave_configure,
3604 .slave_destroy = lpfc_slave_destroy,
James Smart47a86172007-04-25 09:53:22 -04003605 .scan_finished = lpfc_scan_finished,
dea31012005-04-17 16:05:31 -05003606 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05003607 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
dea31012005-04-17 16:05:31 -05003608 .cmd_per_lun = LPFC_CMD_PER_LUN,
3609 .use_clustering = ENABLE_CLUSTERING,
James Smart2e0fef82007-06-17 19:56:36 -05003610 .shost_attrs = lpfc_hba_attrs,
James.Smart@Emulex.Com564b2962005-06-25 10:34:17 -04003611 .max_sectors = 0xFFFF,
James Smartf1c3b0f2009-07-19 10:01:32 -04003612 .vendor_id = LPFC_NL_VENDOR_ID,
James Smart5ffc2662009-11-18 15:39:44 -05003613 .change_queue_depth = lpfc_change_queue_depth,
dea31012005-04-17 16:05:31 -05003614};
James Smart3de2a652007-08-02 11:09:59 -04003615
3616struct scsi_host_template lpfc_vport_template = {
3617 .module = THIS_MODULE,
3618 .name = LPFC_DRIVER_NAME,
3619 .info = lpfc_info,
3620 .queuecommand = lpfc_queuecommand,
3621 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04003622 .eh_device_reset_handler = lpfc_device_reset_handler,
3623 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart3de2a652007-08-02 11:09:59 -04003624 .eh_bus_reset_handler = lpfc_bus_reset_handler,
3625 .slave_alloc = lpfc_slave_alloc,
3626 .slave_configure = lpfc_slave_configure,
3627 .slave_destroy = lpfc_slave_destroy,
3628 .scan_finished = lpfc_scan_finished,
3629 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05003630 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
James Smart3de2a652007-08-02 11:09:59 -04003631 .cmd_per_lun = LPFC_CMD_PER_LUN,
3632 .use_clustering = ENABLE_CLUSTERING,
3633 .shost_attrs = lpfc_vport_attrs,
3634 .max_sectors = 0xFFFF,
James Smart5ffc2662009-11-18 15:39:44 -05003635 .change_queue_depth = lpfc_change_queue_depth,
James Smart3de2a652007-08-02 11:09:59 -04003636};