blob: dacde58b602cd6156ef15b9987a7fe25bd47b914 [file] [log] [blame]
James Smartd85296c2012-03-01 22:38:13 -05001/*******************************************************************
dea31012005-04-17 16:05:31 -05002 * 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 Smartacd68592012-01-18 16:25:09 -05004 * Copyright (C) 2004-2012 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
dea31012005-04-17 16:05:31 -050021#include <linux/pci.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
dea31012005-04-17 16:05:31 -050023#include <linux/interrupt.h>
Paul Gortmaker09703662011-05-27 09:37:25 -040024#include <linux/export.h>
James Smarta90f5682006-08-17 11:58:04 -040025#include <linux/delay.h>
James Smarte2a0a9d2008-12-04 22:40:02 -050026#include <asm/unaligned.h>
James Smart737d4242013-04-17 20:14:49 -040027#include <linux/crc-t10dif.h>
28#include <net/checksum.h>
dea31012005-04-17 16:05:31 -050029
30#include <scsi/scsi.h>
31#include <scsi/scsi_device.h>
James Smarte2a0a9d2008-12-04 22:40:02 -050032#include <scsi/scsi_eh.h>
dea31012005-04-17 16:05:31 -050033#include <scsi/scsi_host.h>
34#include <scsi/scsi_tcq.h>
35#include <scsi/scsi_transport_fc.h>
36
37#include "lpfc_version.h"
James Smartda0436e2009-05-22 14:51:39 -040038#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050039#include "lpfc_hw.h"
40#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040041#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040042#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050043#include "lpfc_disc.h"
dea31012005-04-17 16:05:31 -050044#include "lpfc.h"
James Smart9a6b09c2012-03-01 22:37:42 -050045#include "lpfc_scsi.h"
dea31012005-04-17 16:05:31 -050046#include "lpfc_logmsg.h"
47#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050048#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050049
50#define LPFC_RESET_WAIT 2
51#define LPFC_ABORT_WAIT 2
52
James Smart737d4242013-04-17 20:14:49 -040053int _dump_buf_done = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -050054
55static char *dif_op_str[] = {
James Smart9a6b09c2012-03-01 22:37:42 -050056 "PROT_NORMAL",
57 "PROT_READ_INSERT",
58 "PROT_WRITE_STRIP",
59 "PROT_READ_STRIP",
60 "PROT_WRITE_INSERT",
61 "PROT_READ_PASS",
62 "PROT_WRITE_PASS",
63};
64
James Smartf9bb2da2011-10-10 21:34:11 -040065struct scsi_dif_tuple {
66 __be16 guard_tag; /* Checksum */
67 __be16 app_tag; /* Opaque storage */
68 __be32 ref_tag; /* Target LBA or indirect LBA */
69};
70
James Smartda0436e2009-05-22 14:51:39 -040071static void
72lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
James Smart1c6f4ef52009-11-18 15:40:49 -050073static void
74lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
James Smarte2a0a9d2008-12-04 22:40:02 -050075
76static void
James Smart6a9c52c2009-10-02 15:16:51 -040077lpfc_debug_save_data(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
James Smarte2a0a9d2008-12-04 22:40:02 -050078{
79 void *src, *dst;
80 struct scatterlist *sgde = scsi_sglist(cmnd);
81
82 if (!_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -040083 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
84 "9050 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
James Smarte2a0a9d2008-12-04 22:40:02 -050085 __func__);
86 return;
87 }
88
89
90 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -040091 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
92 "9051 BLKGRD: ERROR: data scatterlist is null\n");
James Smarte2a0a9d2008-12-04 22:40:02 -050093 return;
94 }
95
96 dst = (void *) _dump_buf_data;
97 while (sgde) {
98 src = sg_virt(sgde);
99 memcpy(dst, src, sgde->length);
100 dst += sgde->length;
101 sgde = sg_next(sgde);
102 }
103}
104
105static void
James Smart6a9c52c2009-10-02 15:16:51 -0400106lpfc_debug_save_dif(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
James Smarte2a0a9d2008-12-04 22:40:02 -0500107{
108 void *src, *dst;
109 struct scatterlist *sgde = scsi_prot_sglist(cmnd);
110
111 if (!_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -0400112 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
113 "9052 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
James Smarte2a0a9d2008-12-04 22:40:02 -0500114 __func__);
115 return;
116 }
117
118 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -0400119 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
120 "9053 BLKGRD: ERROR: prot scatterlist is null\n");
James Smarte2a0a9d2008-12-04 22:40:02 -0500121 return;
122 }
123
124 dst = _dump_buf_dif;
125 while (sgde) {
126 src = sg_virt(sgde);
127 memcpy(dst, src, sgde->length);
128 dst += sgde->length;
129 sgde = sg_next(sgde);
130 }
131}
132
James Smartea2151b2008-09-07 11:52:10 -0400133/**
James Smartf1126682009-06-10 17:22:44 -0400134 * lpfc_sli4_set_rsp_sgl_last - Set the last bit in the response sge.
135 * @phba: Pointer to HBA object.
136 * @lpfc_cmd: lpfc scsi command object pointer.
137 *
138 * This function is called from the lpfc_prep_task_mgmt_cmd function to
139 * set the last bit in the response sge entry.
140 **/
141static void
142lpfc_sli4_set_rsp_sgl_last(struct lpfc_hba *phba,
143 struct lpfc_scsi_buf *lpfc_cmd)
144{
145 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
146 if (sgl) {
147 sgl += 1;
148 sgl->word2 = le32_to_cpu(sgl->word2);
149 bf_set(lpfc_sli4_sge_last, sgl, 1);
150 sgl->word2 = cpu_to_le32(sgl->word2);
151 }
152}
153
154/**
James Smart3621a712009-04-06 18:47:14 -0400155 * lpfc_update_stats - Update statistical data for the command completion
James Smartea2151b2008-09-07 11:52:10 -0400156 * @phba: Pointer to HBA object.
157 * @lpfc_cmd: lpfc scsi command object pointer.
158 *
159 * This function is called when there is a command completion and this
160 * function updates the statistical data for the command completion.
161 **/
162static void
163lpfc_update_stats(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
164{
165 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
166 struct lpfc_nodelist *pnode = rdata->pnode;
167 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
168 unsigned long flags;
169 struct Scsi_Host *shost = cmd->device->host;
170 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
171 unsigned long latency;
172 int i;
173
174 if (cmd->result)
175 return;
176
James Smart9f1e1b52008-12-04 22:39:40 -0500177 latency = jiffies_to_msecs((long)jiffies - (long)lpfc_cmd->start_time);
178
James Smartea2151b2008-09-07 11:52:10 -0400179 spin_lock_irqsave(shost->host_lock, flags);
180 if (!vport->stat_data_enabled ||
181 vport->stat_data_blocked ||
James Smart5989b8d2010-10-22 11:06:56 -0400182 !pnode ||
James Smartea2151b2008-09-07 11:52:10 -0400183 !pnode->lat_data ||
184 (phba->bucket_type == LPFC_NO_BUCKET)) {
185 spin_unlock_irqrestore(shost->host_lock, flags);
186 return;
187 }
James Smartea2151b2008-09-07 11:52:10 -0400188
189 if (phba->bucket_type == LPFC_LINEAR_BUCKET) {
190 i = (latency + phba->bucket_step - 1 - phba->bucket_base)/
191 phba->bucket_step;
James Smart9f1e1b52008-12-04 22:39:40 -0500192 /* check array subscript bounds */
193 if (i < 0)
194 i = 0;
195 else if (i >= LPFC_MAX_BUCKET_COUNT)
196 i = LPFC_MAX_BUCKET_COUNT - 1;
James Smartea2151b2008-09-07 11:52:10 -0400197 } else {
198 for (i = 0; i < LPFC_MAX_BUCKET_COUNT-1; i++)
199 if (latency <= (phba->bucket_base +
200 ((1<<i)*phba->bucket_step)))
201 break;
202 }
203
204 pnode->lat_data[i].cmd_count++;
205 spin_unlock_irqrestore(shost->host_lock, flags);
206}
207
James Smartea2151b2008-09-07 11:52:10 -0400208/**
James Smart3621a712009-04-06 18:47:14 -0400209 * lpfc_send_sdev_queuedepth_change_event - Posts a queuedepth change event
James Smartea2151b2008-09-07 11:52:10 -0400210 * @phba: Pointer to HBA context object.
211 * @vport: Pointer to vport object.
212 * @ndlp: Pointer to FC node associated with the target.
213 * @lun: Lun number of the scsi device.
214 * @old_val: Old value of the queue depth.
215 * @new_val: New value of the queue depth.
216 *
217 * This function sends an event to the mgmt application indicating
218 * there is a change in the scsi device queue depth.
219 **/
220static void
221lpfc_send_sdev_queuedepth_change_event(struct lpfc_hba *phba,
222 struct lpfc_vport *vport,
223 struct lpfc_nodelist *ndlp,
224 uint32_t lun,
225 uint32_t old_val,
226 uint32_t new_val)
227{
228 struct lpfc_fast_path_event *fast_path_evt;
229 unsigned long flags;
230
231 fast_path_evt = lpfc_alloc_fast_evt(phba);
232 if (!fast_path_evt)
233 return;
234
235 fast_path_evt->un.queue_depth_evt.scsi_event.event_type =
236 FC_REG_SCSI_EVENT;
237 fast_path_evt->un.queue_depth_evt.scsi_event.subcategory =
238 LPFC_EVENT_VARQUEDEPTH;
239
240 /* Report all luns with change in queue depth */
241 fast_path_evt->un.queue_depth_evt.scsi_event.lun = lun;
242 if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
243 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwpn,
244 &ndlp->nlp_portname, sizeof(struct lpfc_name));
245 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwnn,
246 &ndlp->nlp_nodename, sizeof(struct lpfc_name));
247 }
248
249 fast_path_evt->un.queue_depth_evt.oldval = old_val;
250 fast_path_evt->un.queue_depth_evt.newval = new_val;
251 fast_path_evt->vport = vport;
252
253 fast_path_evt->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
254 spin_lock_irqsave(&phba->hbalock, flags);
255 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
256 spin_unlock_irqrestore(&phba->hbalock, flags);
257 lpfc_worker_wake_up(phba);
258
259 return;
260}
261
James Smart9bad7672008-12-04 22:39:02 -0500262/**
James Smart5ffc2662009-11-18 15:39:44 -0500263 * lpfc_change_queue_depth - Alter scsi device queue depth
264 * @sdev: Pointer the scsi device on which to change the queue depth.
265 * @qdepth: New queue depth to set the sdev to.
266 * @reason: The reason for the queue depth change.
267 *
268 * This function is called by the midlayer and the LLD to alter the queue
269 * depth for a scsi device. This function sets the queue depth to the new
270 * value and sends an event out to log the queue depth change.
271 **/
272int
273lpfc_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
274{
275 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
276 struct lpfc_hba *phba = vport->phba;
277 struct lpfc_rport_data *rdata;
278 unsigned long new_queue_depth, old_queue_depth;
279
280 old_queue_depth = sdev->queue_depth;
281 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
282 new_queue_depth = sdev->queue_depth;
283 rdata = sdev->hostdata;
284 if (rdata)
285 lpfc_send_sdev_queuedepth_change_event(phba, vport,
286 rdata->pnode, sdev->lun,
287 old_queue_depth,
288 new_queue_depth);
289 return sdev->queue_depth;
290}
291
292/**
James Smartfe8f7f92013-01-03 15:43:03 -0500293 * lpfc_change_queue_type() - Change a device's scsi tag queuing type
294 * @sdev: Pointer the scsi device whose queue depth is to change
295 * @tag_type: Identifier for queue tag type
296 */
297static int
298lpfc_change_queue_type(struct scsi_device *sdev, int tag_type)
299{
300 if (sdev->tagged_supported) {
301 scsi_set_tag_type(sdev, tag_type);
302 if (tag_type)
303 scsi_activate_tcq(sdev, sdev->queue_depth);
304 else
305 scsi_deactivate_tcq(sdev, sdev->queue_depth);
306 } else
307 tag_type = 0;
308
309 return tag_type;
310}
311
312/**
James Smart3621a712009-04-06 18:47:14 -0400313 * lpfc_rampdown_queue_depth - Post RAMP_DOWN_QUEUE event to worker thread
James Smart9bad7672008-12-04 22:39:02 -0500314 * @phba: The Hba for which this call is being executed.
315 *
316 * This routine is called when there is resource error in driver or firmware.
317 * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
318 * posts at most 1 event each second. This routine wakes up worker thread of
319 * @phba to process WORKER_RAM_DOWN_EVENT event.
320 *
321 * This routine should be called with no lock held.
322 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500323void
James Smarteaf15d52008-12-04 22:39:29 -0500324lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
James Smart92d7f7b2007-06-17 19:56:38 -0500325{
326 unsigned long flags;
James Smart5e9d9b82008-06-14 22:52:53 -0400327 uint32_t evt_posted;
James Smart92d7f7b2007-06-17 19:56:38 -0500328
329 spin_lock_irqsave(&phba->hbalock, flags);
330 atomic_inc(&phba->num_rsrc_err);
331 phba->last_rsrc_error_time = jiffies;
332
333 if ((phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL) > jiffies) {
334 spin_unlock_irqrestore(&phba->hbalock, flags);
335 return;
336 }
337
338 phba->last_ramp_down_time = jiffies;
339
340 spin_unlock_irqrestore(&phba->hbalock, flags);
341
342 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400343 evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
344 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500345 phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500346 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
347
James Smart5e9d9b82008-06-14 22:52:53 -0400348 if (!evt_posted)
349 lpfc_worker_wake_up(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500350 return;
351}
352
James Smart9bad7672008-12-04 22:39:02 -0500353/**
James Smart3621a712009-04-06 18:47:14 -0400354 * lpfc_rampup_queue_depth - Post RAMP_UP_QUEUE event for worker thread
James Smart9bad7672008-12-04 22:39:02 -0500355 * @phba: The Hba for which this call is being executed.
356 *
357 * This routine post WORKER_RAMP_UP_QUEUE event for @phba vport. This routine
358 * post at most 1 event every 5 minute after last_ramp_up_time or
359 * last_rsrc_error_time. This routine wakes up worker thread of @phba
360 * to process WORKER_RAM_DOWN_EVENT event.
361 *
362 * This routine should be called with no lock held.
363 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500364static inline void
James Smart3de2a652007-08-02 11:09:59 -0400365lpfc_rampup_queue_depth(struct lpfc_vport *vport,
James Smarta257bf92009-04-06 18:48:10 -0400366 uint32_t queue_depth)
James Smart92d7f7b2007-06-17 19:56:38 -0500367{
368 unsigned long flags;
James Smart3de2a652007-08-02 11:09:59 -0400369 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -0400370 uint32_t evt_posted;
James Smart92d7f7b2007-06-17 19:56:38 -0500371 atomic_inc(&phba->num_cmd_success);
372
James Smarta257bf92009-04-06 18:48:10 -0400373 if (vport->cfg_lun_queue_depth <= queue_depth)
James Smart92d7f7b2007-06-17 19:56:38 -0500374 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500375 spin_lock_irqsave(&phba->hbalock, flags);
James Smart5ffc2662009-11-18 15:39:44 -0500376 if (time_before(jiffies,
377 phba->last_ramp_up_time + QUEUE_RAMP_UP_INTERVAL) ||
378 time_before(jiffies,
379 phba->last_rsrc_error_time + QUEUE_RAMP_UP_INTERVAL)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500380 spin_unlock_irqrestore(&phba->hbalock, flags);
381 return;
382 }
James Smart92d7f7b2007-06-17 19:56:38 -0500383 phba->last_ramp_up_time = jiffies;
384 spin_unlock_irqrestore(&phba->hbalock, flags);
385
386 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400387 evt_posted = phba->pport->work_port_events & WORKER_RAMP_UP_QUEUE;
388 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500389 phba->pport->work_port_events |= WORKER_RAMP_UP_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500390 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
391
James Smart5e9d9b82008-06-14 22:52:53 -0400392 if (!evt_posted)
393 lpfc_worker_wake_up(phba);
394 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500395}
396
James Smart9bad7672008-12-04 22:39:02 -0500397/**
James Smart3621a712009-04-06 18:47:14 -0400398 * lpfc_ramp_down_queue_handler - WORKER_RAMP_DOWN_QUEUE event handler
James Smart9bad7672008-12-04 22:39:02 -0500399 * @phba: The Hba for which this call is being executed.
400 *
401 * This routine is called to process WORKER_RAMP_DOWN_QUEUE event for worker
402 * thread.This routine reduces queue depth for all scsi device on each vport
403 * associated with @phba.
404 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500405void
406lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
407{
James Smart549e55c2007-08-02 11:09:51 -0400408 struct lpfc_vport **vports;
409 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500410 struct scsi_device *sdev;
James Smart5ffc2662009-11-18 15:39:44 -0500411 unsigned long new_queue_depth;
James Smart92d7f7b2007-06-17 19:56:38 -0500412 unsigned long num_rsrc_err, num_cmd_success;
James Smart549e55c2007-08-02 11:09:51 -0400413 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500414
415 num_rsrc_err = atomic_read(&phba->num_rsrc_err);
416 num_cmd_success = atomic_read(&phba->num_cmd_success);
417
James Smart75ad83a2012-05-09 21:18:40 -0400418 /*
419 * The error and success command counters are global per
420 * driver instance. If another handler has already
421 * operated on this error event, just exit.
422 */
423 if (num_rsrc_err == 0)
424 return;
425
James Smart549e55c2007-08-02 11:09:51 -0400426 vports = lpfc_create_vport_work_array(phba);
427 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400428 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400429 shost = lpfc_shost_from_vport(vports[i]);
430 shost_for_each_device(sdev, shost) {
James Smart92d7f7b2007-06-17 19:56:38 -0500431 new_queue_depth =
James Smart549e55c2007-08-02 11:09:51 -0400432 sdev->queue_depth * num_rsrc_err /
433 (num_rsrc_err + num_cmd_success);
434 if (!new_queue_depth)
435 new_queue_depth = sdev->queue_depth - 1;
436 else
437 new_queue_depth = sdev->queue_depth -
438 new_queue_depth;
James Smart5ffc2662009-11-18 15:39:44 -0500439 lpfc_change_queue_depth(sdev, new_queue_depth,
440 SCSI_QDEPTH_DEFAULT);
James Smart549e55c2007-08-02 11:09:51 -0400441 }
James Smart92d7f7b2007-06-17 19:56:38 -0500442 }
James Smart09372822008-01-11 01:52:54 -0500443 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500444 atomic_set(&phba->num_rsrc_err, 0);
445 atomic_set(&phba->num_cmd_success, 0);
446}
447
James Smart9bad7672008-12-04 22:39:02 -0500448/**
James Smart3621a712009-04-06 18:47:14 -0400449 * lpfc_ramp_up_queue_handler - WORKER_RAMP_UP_QUEUE event handler
James Smart9bad7672008-12-04 22:39:02 -0500450 * @phba: The Hba for which this call is being executed.
451 *
452 * This routine is called to process WORKER_RAMP_UP_QUEUE event for worker
453 * thread.This routine increases queue depth for all scsi device on each vport
454 * associated with @phba by 1. This routine also sets @phba num_rsrc_err and
455 * num_cmd_success to zero.
456 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500457void
458lpfc_ramp_up_queue_handler(struct lpfc_hba *phba)
459{
James Smart549e55c2007-08-02 11:09:51 -0400460 struct lpfc_vport **vports;
461 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500462 struct scsi_device *sdev;
James Smart549e55c2007-08-02 11:09:51 -0400463 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500464
James Smart549e55c2007-08-02 11:09:51 -0400465 vports = lpfc_create_vport_work_array(phba);
466 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400467 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400468 shost = lpfc_shost_from_vport(vports[i]);
469 shost_for_each_device(sdev, shost) {
James Smart97eab632008-04-07 10:16:05 -0400470 if (vports[i]->cfg_lun_queue_depth <=
471 sdev->queue_depth)
472 continue;
James Smart5ffc2662009-11-18 15:39:44 -0500473 lpfc_change_queue_depth(sdev,
474 sdev->queue_depth+1,
475 SCSI_QDEPTH_RAMP_UP);
James Smart549e55c2007-08-02 11:09:51 -0400476 }
James Smart92d7f7b2007-06-17 19:56:38 -0500477 }
James Smart09372822008-01-11 01:52:54 -0500478 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500479 atomic_set(&phba->num_rsrc_err, 0);
480 atomic_set(&phba->num_cmd_success, 0);
481}
482
James Smarta8e497d2008-08-24 21:50:11 -0400483/**
James Smart3621a712009-04-06 18:47:14 -0400484 * lpfc_scsi_dev_block - set all scsi hosts to block state
James Smarta8e497d2008-08-24 21:50:11 -0400485 * @phba: Pointer to HBA context object.
486 *
487 * This function walks vport list and set each SCSI host to block state
488 * by invoking fc_remote_port_delete() routine. This function is invoked
489 * with EEH when device's PCI slot has been permanently disabled.
490 **/
491void
492lpfc_scsi_dev_block(struct lpfc_hba *phba)
493{
494 struct lpfc_vport **vports;
495 struct Scsi_Host *shost;
496 struct scsi_device *sdev;
497 struct fc_rport *rport;
498 int i;
499
500 vports = lpfc_create_vport_work_array(phba);
501 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400502 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8e497d2008-08-24 21:50:11 -0400503 shost = lpfc_shost_from_vport(vports[i]);
504 shost_for_each_device(sdev, shost) {
505 rport = starget_to_rport(scsi_target(sdev));
506 fc_remote_port_delete(rport);
507 }
508 }
509 lpfc_destroy_vport_work_array(phba, vports);
510}
511
James Smart9bad7672008-12-04 22:39:02 -0500512/**
James Smart3772a992009-05-22 14:50:54 -0400513 * lpfc_new_scsi_buf_s3 - Scsi buffer allocator for HBA with SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -0500514 * @vport: The virtual port for which this call being executed.
James Smart3772a992009-05-22 14:50:54 -0400515 * @num_to_allocate: The requested number of buffers to allocate.
James Smart9bad7672008-12-04 22:39:02 -0500516 *
James Smart3772a992009-05-22 14:50:54 -0400517 * This routine allocates a scsi buffer for device with SLI-3 interface spec,
518 * the scsi buffer contains all the necessary information needed to initiate
519 * a SCSI I/O. The non-DMAable buffer region contains information to build
520 * the IOCB. The DMAable region contains memory for the FCP CMND, FCP RSP,
521 * and the initial BPL. In addition to allocating memory, the FCP CMND and
522 * FCP RSP BDEs are setup in the BPL and the BPL BDE is setup in the IOCB.
James Smart9bad7672008-12-04 22:39:02 -0500523 *
524 * Return codes:
James Smart3772a992009-05-22 14:50:54 -0400525 * int - number of scsi buffers that were allocated.
526 * 0 = failure, less than num_to_alloc is a partial failure.
James Smart9bad7672008-12-04 22:39:02 -0500527 **/
James Smart3772a992009-05-22 14:50:54 -0400528static int
529lpfc_new_scsi_buf_s3(struct lpfc_vport *vport, int num_to_alloc)
dea31012005-04-17 16:05:31 -0500530{
James Smart2e0fef82007-06-17 19:56:36 -0500531 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500532 struct lpfc_scsi_buf *psb;
533 struct ulp_bde64 *bpl;
534 IOCB_t *iocb;
James Smart34b02dc2008-08-24 21:49:55 -0400535 dma_addr_t pdma_phys_fcp_cmd;
536 dma_addr_t pdma_phys_fcp_rsp;
537 dma_addr_t pdma_phys_bpl;
James Bottomley604a3e32005-10-29 10:28:33 -0500538 uint16_t iotag;
James Smart3772a992009-05-22 14:50:54 -0400539 int bcnt;
dea31012005-04-17 16:05:31 -0500540
James Smart3772a992009-05-22 14:50:54 -0400541 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
542 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
543 if (!psb)
544 break;
dea31012005-04-17 16:05:31 -0500545
James Smart3772a992009-05-22 14:50:54 -0400546 /*
547 * Get memory from the pci pool to map the virt space to pci
548 * bus space for an I/O. The DMA buffer includes space for the
549 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
550 * necessary to support the sg_tablesize.
551 */
552 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
553 GFP_KERNEL, &psb->dma_handle);
554 if (!psb->data) {
555 kfree(psb);
556 break;
557 }
dea31012005-04-17 16:05:31 -0500558
James Smart3772a992009-05-22 14:50:54 -0400559 /* Initialize virtual ptrs to dma_buf region. */
560 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
dea31012005-04-17 16:05:31 -0500561
James Smart3772a992009-05-22 14:50:54 -0400562 /* Allocate iotag for psb->cur_iocbq. */
563 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
564 if (iotag == 0) {
565 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
566 psb->data, psb->dma_handle);
567 kfree(psb);
568 break;
569 }
570 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Bottomley604a3e32005-10-29 10:28:33 -0500571
James Smart3772a992009-05-22 14:50:54 -0400572 psb->fcp_cmnd = psb->data;
573 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
574 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
James Smart34b02dc2008-08-24 21:49:55 -0400575 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500576
James Smart3772a992009-05-22 14:50:54 -0400577 /* Initialize local short-hand pointers. */
578 bpl = psb->fcp_bpl;
579 pdma_phys_fcp_cmd = psb->dma_handle;
580 pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
581 pdma_phys_bpl = psb->dma_handle + sizeof(struct fcp_cmnd) +
582 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500583
James Smart3772a992009-05-22 14:50:54 -0400584 /*
585 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
586 * are sg list bdes. Initialize the first two and leave the
587 * rest for queuecommand.
588 */
589 bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
590 bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
591 bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
592 bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
593 bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
dea31012005-04-17 16:05:31 -0500594
James Smart3772a992009-05-22 14:50:54 -0400595 /* Setup the physical region for the FCP RSP */
596 bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
597 bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
598 bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
599 bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
600 bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
601
602 /*
603 * Since the IOCB for the FCP I/O is built into this
604 * lpfc_scsi_buf, initialize it with all known data now.
605 */
606 iocb = &psb->cur_iocbq.iocb;
607 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
608 if ((phba->sli_rev == 3) &&
609 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
610 /* fill in immediate fcp command BDE */
611 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
612 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
613 iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
614 unsli3.fcp_ext.icd);
615 iocb->un.fcpi64.bdl.addrHigh = 0;
616 iocb->ulpBdeCount = 0;
617 iocb->ulpLe = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300618 /* fill in response BDE */
James Smart3772a992009-05-22 14:50:54 -0400619 iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags =
620 BUFF_TYPE_BDE_64;
621 iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
622 sizeof(struct fcp_rsp);
623 iocb->unsli3.fcp_ext.rbde.addrLow =
624 putPaddrLow(pdma_phys_fcp_rsp);
625 iocb->unsli3.fcp_ext.rbde.addrHigh =
626 putPaddrHigh(pdma_phys_fcp_rsp);
627 } else {
628 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
629 iocb->un.fcpi64.bdl.bdeSize =
630 (2 * sizeof(struct ulp_bde64));
631 iocb->un.fcpi64.bdl.addrLow =
632 putPaddrLow(pdma_phys_bpl);
633 iocb->un.fcpi64.bdl.addrHigh =
634 putPaddrHigh(pdma_phys_bpl);
635 iocb->ulpBdeCount = 1;
636 iocb->ulpLe = 1;
637 }
638 iocb->ulpClass = CLASS3;
639 psb->status = IOSTAT_SUCCESS;
James Smartda0436e2009-05-22 14:51:39 -0400640 /* Put it back into the SCSI buffer list */
James Smarteee88772010-09-29 11:19:08 -0400641 psb->cur_iocbq.context1 = psb;
James Smart1c6f4ef52009-11-18 15:40:49 -0500642 lpfc_release_scsi_buf_s3(phba, psb);
James Smart3772a992009-05-22 14:50:54 -0400643
James Smart34b02dc2008-08-24 21:49:55 -0400644 }
dea31012005-04-17 16:05:31 -0500645
James Smart3772a992009-05-22 14:50:54 -0400646 return bcnt;
dea31012005-04-17 16:05:31 -0500647}
648
James Smart9bad7672008-12-04 22:39:02 -0500649/**
James Smart1151e3e2011-02-16 12:39:35 -0500650 * lpfc_sli4_vport_delete_fcp_xri_aborted -Remove all ndlp references for vport
651 * @vport: pointer to lpfc vport data structure.
652 *
653 * This routine is invoked by the vport cleanup for deletions and the cleanup
654 * for an ndlp on removal.
655 **/
656void
657lpfc_sli4_vport_delete_fcp_xri_aborted(struct lpfc_vport *vport)
658{
659 struct lpfc_hba *phba = vport->phba;
660 struct lpfc_scsi_buf *psb, *next_psb;
661 unsigned long iflag = 0;
662
663 spin_lock_irqsave(&phba->hbalock, iflag);
664 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
665 list_for_each_entry_safe(psb, next_psb,
666 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
667 if (psb->rdata && psb->rdata->pnode
668 && psb->rdata->pnode->vport == vport)
669 psb->rdata = NULL;
670 }
671 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
672 spin_unlock_irqrestore(&phba->hbalock, iflag);
673}
674
675/**
James Smartda0436e2009-05-22 14:51:39 -0400676 * lpfc_sli4_fcp_xri_aborted - Fast-path process of fcp xri abort
677 * @phba: pointer to lpfc hba data structure.
678 * @axri: pointer to the fcp xri abort wcqe structure.
679 *
680 * This routine is invoked by the worker thread to process a SLI4 fast-path
681 * FCP aborted xri.
682 **/
683void
684lpfc_sli4_fcp_xri_aborted(struct lpfc_hba *phba,
685 struct sli4_wcqe_xri_aborted *axri)
686{
687 uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
James Smart19ca7602010-11-20 23:11:55 -0500688 uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
James Smartda0436e2009-05-22 14:51:39 -0400689 struct lpfc_scsi_buf *psb, *next_psb;
690 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -0500691 struct lpfc_iocbq *iocbq;
692 int i;
James Smart19ca7602010-11-20 23:11:55 -0500693 struct lpfc_nodelist *ndlp;
694 int rrq_empty = 0;
James Smart589a52d2010-07-14 15:30:54 -0400695 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smartda0436e2009-05-22 14:51:39 -0400696
James Smart0f65ff62010-02-26 14:14:23 -0500697 spin_lock_irqsave(&phba->hbalock, iflag);
698 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
James Smartda0436e2009-05-22 14:51:39 -0400699 list_for_each_entry_safe(psb, next_psb,
700 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
701 if (psb->cur_iocbq.sli4_xritag == xri) {
702 list_del(&psb->list);
James Smart341af102010-01-26 23:07:37 -0500703 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400704 psb->status = IOSTAT_SUCCESS;
James Smart0f65ff62010-02-26 14:14:23 -0500705 spin_unlock(
706 &phba->sli4_hba.abts_scsi_buf_list_lock);
James Smart1151e3e2011-02-16 12:39:35 -0500707 if (psb->rdata && psb->rdata->pnode)
708 ndlp = psb->rdata->pnode;
709 else
710 ndlp = NULL;
711
James Smart19ca7602010-11-20 23:11:55 -0500712 rrq_empty = list_empty(&phba->active_rrq_list);
James Smart0f65ff62010-02-26 14:14:23 -0500713 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartcb69f7d2011-12-13 13:21:57 -0500714 if (ndlp) {
James Smartee0f4fe2012-05-09 21:19:14 -0400715 lpfc_set_rrq_active(phba, ndlp,
716 psb->cur_iocbq.sli4_lxritag, rxid, 1);
James Smartcb69f7d2011-12-13 13:21:57 -0500717 lpfc_sli4_abts_err_handler(phba, ndlp, axri);
718 }
James Smartda0436e2009-05-22 14:51:39 -0400719 lpfc_release_scsi_buf_s4(phba, psb);
James Smart19ca7602010-11-20 23:11:55 -0500720 if (rrq_empty)
721 lpfc_worker_wake_up(phba);
James Smartda0436e2009-05-22 14:51:39 -0400722 return;
723 }
724 }
James Smart0f65ff62010-02-26 14:14:23 -0500725 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
726 for (i = 1; i <= phba->sli.last_iotag; i++) {
727 iocbq = phba->sli.iocbq_lookup[i];
728
729 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
730 (iocbq->iocb_flag & LPFC_IO_LIBDFC))
731 continue;
732 if (iocbq->sli4_xritag != xri)
733 continue;
734 psb = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
735 psb->exch_busy = 0;
736 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart0e9bb8d2013-03-01 16:35:12 -0500737 if (!list_empty(&pring->txq))
James Smart589a52d2010-07-14 15:30:54 -0400738 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -0500739 return;
740
741 }
742 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartda0436e2009-05-22 14:51:39 -0400743}
744
745/**
James Smart8a9d2e82012-05-09 21:16:12 -0400746 * lpfc_sli4_post_scsi_sgl_list - Psot blocks of scsi buffer sgls from a list
747 * @phba: pointer to lpfc hba data structure.
748 * @post_sblist: pointer to the scsi buffer list.
749 *
750 * This routine walks a list of scsi buffers that was passed in. It attempts
751 * to construct blocks of scsi buffer sgls which contains contiguous xris and
752 * uses the non-embedded SGL block post mailbox commands to post to the port.
753 * For single SCSI buffer sgl with non-contiguous xri, if any, it shall use
754 * embedded SGL post mailbox command for posting. The @post_sblist passed in
755 * must be local list, thus no lock is needed when manipulate the list.
756 *
757 * Returns: 0 = failure, non-zero number of successfully posted buffers.
758 **/
759int
760lpfc_sli4_post_scsi_sgl_list(struct lpfc_hba *phba,
761 struct list_head *post_sblist, int sb_count)
762{
763 struct lpfc_scsi_buf *psb, *psb_next;
764 int status;
765 int post_cnt = 0, block_cnt = 0, num_posting = 0, num_posted = 0;
766 dma_addr_t pdma_phys_bpl1;
767 int last_xritag = NO_XRI;
768 LIST_HEAD(prep_sblist);
769 LIST_HEAD(blck_sblist);
770 LIST_HEAD(scsi_sblist);
771
772 /* sanity check */
773 if (sb_count <= 0)
774 return -EINVAL;
775
776 list_for_each_entry_safe(psb, psb_next, post_sblist, list) {
777 list_del_init(&psb->list);
778 block_cnt++;
779 if ((last_xritag != NO_XRI) &&
780 (psb->cur_iocbq.sli4_xritag != last_xritag + 1)) {
781 /* a hole in xri block, form a sgl posting block */
782 list_splice_init(&prep_sblist, &blck_sblist);
783 post_cnt = block_cnt - 1;
784 /* prepare list for next posting block */
785 list_add_tail(&psb->list, &prep_sblist);
786 block_cnt = 1;
787 } else {
788 /* prepare list for next posting block */
789 list_add_tail(&psb->list, &prep_sblist);
790 /* enough sgls for non-embed sgl mbox command */
791 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
792 list_splice_init(&prep_sblist, &blck_sblist);
793 post_cnt = block_cnt;
794 block_cnt = 0;
795 }
796 }
797 num_posting++;
798 last_xritag = psb->cur_iocbq.sli4_xritag;
799
800 /* end of repost sgl list condition for SCSI buffers */
801 if (num_posting == sb_count) {
802 if (post_cnt == 0) {
803 /* last sgl posting block */
804 list_splice_init(&prep_sblist, &blck_sblist);
805 post_cnt = block_cnt;
806 } else if (block_cnt == 1) {
807 /* last single sgl with non-contiguous xri */
808 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
809 pdma_phys_bpl1 = psb->dma_phys_bpl +
810 SGL_PAGE_SIZE;
811 else
812 pdma_phys_bpl1 = 0;
813 status = lpfc_sli4_post_sgl(phba,
814 psb->dma_phys_bpl,
815 pdma_phys_bpl1,
816 psb->cur_iocbq.sli4_xritag);
817 if (status) {
818 /* failure, put on abort scsi list */
819 psb->exch_busy = 1;
820 } else {
821 /* success, put on SCSI buffer list */
822 psb->exch_busy = 0;
823 psb->status = IOSTAT_SUCCESS;
824 num_posted++;
825 }
826 /* success, put on SCSI buffer sgl list */
827 list_add_tail(&psb->list, &scsi_sblist);
828 }
829 }
830
831 /* continue until a nembed page worth of sgls */
832 if (post_cnt == 0)
833 continue;
834
835 /* post block of SCSI buffer list sgls */
836 status = lpfc_sli4_post_scsi_sgl_block(phba, &blck_sblist,
837 post_cnt);
838
839 /* don't reset xirtag due to hole in xri block */
840 if (block_cnt == 0)
841 last_xritag = NO_XRI;
842
843 /* reset SCSI buffer post count for next round of posting */
844 post_cnt = 0;
845
846 /* put posted SCSI buffer-sgl posted on SCSI buffer sgl list */
847 while (!list_empty(&blck_sblist)) {
848 list_remove_head(&blck_sblist, psb,
849 struct lpfc_scsi_buf, list);
850 if (status) {
851 /* failure, put on abort scsi list */
852 psb->exch_busy = 1;
853 } else {
854 /* success, put on SCSI buffer list */
855 psb->exch_busy = 0;
856 psb->status = IOSTAT_SUCCESS;
857 num_posted++;
858 }
859 list_add_tail(&psb->list, &scsi_sblist);
860 }
861 }
862 /* Push SCSI buffers with sgl posted to the availble list */
863 while (!list_empty(&scsi_sblist)) {
864 list_remove_head(&scsi_sblist, psb,
865 struct lpfc_scsi_buf, list);
866 lpfc_release_scsi_buf_s4(phba, psb);
867 }
868 return num_posted;
869}
870
871/**
872 * lpfc_sli4_repost_scsi_sgl_list - Repsot all the allocated scsi buffer sgls
James Smartda0436e2009-05-22 14:51:39 -0400873 * @phba: pointer to lpfc hba data structure.
874 *
875 * This routine walks the list of scsi buffers that have been allocated and
James Smart8a9d2e82012-05-09 21:16:12 -0400876 * repost them to the port by using SGL block post. This is needed after a
James Smartda0436e2009-05-22 14:51:39 -0400877 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
878 * is responsible for moving all scsi buffers on the lpfc_abts_scsi_sgl_list
879 * to the lpfc_scsi_buf_list. If the repost fails, reject all scsi buffers.
880 *
881 * Returns: 0 = success, non-zero failure.
882 **/
883int
884lpfc_sli4_repost_scsi_sgl_list(struct lpfc_hba *phba)
885{
James Smart8a9d2e82012-05-09 21:16:12 -0400886 LIST_HEAD(post_sblist);
887 int num_posted, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -0400888
James Smart8a9d2e82012-05-09 21:16:12 -0400889 /* get all SCSI buffers need to repost to a local list */
James Smart38c20672013-03-01 16:37:44 -0500890 spin_lock_irq(&phba->scsi_buf_list_lock);
James Smart8a9d2e82012-05-09 21:16:12 -0400891 list_splice_init(&phba->lpfc_scsi_buf_list, &post_sblist);
James Smart38c20672013-03-01 16:37:44 -0500892 spin_unlock_irq(&phba->scsi_buf_list_lock);
James Smartda0436e2009-05-22 14:51:39 -0400893
James Smart8a9d2e82012-05-09 21:16:12 -0400894 /* post the list of scsi buffer sgls to port if available */
895 if (!list_empty(&post_sblist)) {
896 num_posted = lpfc_sli4_post_scsi_sgl_list(phba, &post_sblist,
897 phba->sli4_hba.scsi_xri_cnt);
898 /* failed to post any scsi buffer, return error */
899 if (num_posted == 0)
900 rc = -EIO;
James Smartda0436e2009-05-22 14:51:39 -0400901 }
902 return rc;
903}
904
905/**
906 * lpfc_new_scsi_buf_s4 - Scsi buffer allocator for HBA with SLI4 IF spec
907 * @vport: The virtual port for which this call being executed.
908 * @num_to_allocate: The requested number of buffers to allocate.
909 *
James Smart8a9d2e82012-05-09 21:16:12 -0400910 * This routine allocates scsi buffers for device with SLI-4 interface spec,
James Smartda0436e2009-05-22 14:51:39 -0400911 * the scsi buffer contains all the necessary information needed to initiate
James Smart8a9d2e82012-05-09 21:16:12 -0400912 * a SCSI I/O. After allocating up to @num_to_allocate SCSI buffers and put
913 * them on a list, it post them to the port by using SGL block post.
James Smartda0436e2009-05-22 14:51:39 -0400914 *
915 * Return codes:
James Smart8a9d2e82012-05-09 21:16:12 -0400916 * int - number of scsi buffers that were allocated and posted.
James Smartda0436e2009-05-22 14:51:39 -0400917 * 0 = failure, less than num_to_alloc is a partial failure.
918 **/
919static int
920lpfc_new_scsi_buf_s4(struct lpfc_vport *vport, int num_to_alloc)
921{
922 struct lpfc_hba *phba = vport->phba;
923 struct lpfc_scsi_buf *psb;
924 struct sli4_sge *sgl;
925 IOCB_t *iocb;
926 dma_addr_t pdma_phys_fcp_cmd;
927 dma_addr_t pdma_phys_fcp_rsp;
928 dma_addr_t pdma_phys_bpl, pdma_phys_bpl1;
James Smart8a9d2e82012-05-09 21:16:12 -0400929 uint16_t iotag, lxri = 0;
930 int bcnt, num_posted;
931 LIST_HEAD(prep_sblist);
932 LIST_HEAD(post_sblist);
933 LIST_HEAD(scsi_sblist);
James Smartda0436e2009-05-22 14:51:39 -0400934
935 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
936 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
937 if (!psb)
938 break;
James Smartda0436e2009-05-22 14:51:39 -0400939 /*
James Smart8a9d2e82012-05-09 21:16:12 -0400940 * Get memory from the pci pool to map the virt space to
941 * pci bus space for an I/O. The DMA buffer includes space
942 * for the struct fcp_cmnd, struct fcp_rsp and the number
943 * of bde's necessary to support the sg_tablesize.
James Smartda0436e2009-05-22 14:51:39 -0400944 */
945 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
946 GFP_KERNEL, &psb->dma_handle);
947 if (!psb->data) {
948 kfree(psb);
949 break;
950 }
James Smartda0436e2009-05-22 14:51:39 -0400951 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
952
953 /* Allocate iotag for psb->cur_iocbq. */
954 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
955 if (iotag == 0) {
James Smartb92938b2010-06-07 15:24:12 -0400956 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
957 psb->data, psb->dma_handle);
James Smartda0436e2009-05-22 14:51:39 -0400958 kfree(psb);
959 break;
960 }
961
James Smart6d368e52011-05-24 11:44:12 -0400962 lxri = lpfc_sli4_next_xritag(phba);
963 if (lxri == NO_XRI) {
James Smartda0436e2009-05-22 14:51:39 -0400964 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
965 psb->data, psb->dma_handle);
966 kfree(psb);
967 break;
968 }
James Smart6d368e52011-05-24 11:44:12 -0400969 psb->cur_iocbq.sli4_lxritag = lxri;
970 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
James Smartda0436e2009-05-22 14:51:39 -0400971 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Smartda0436e2009-05-22 14:51:39 -0400972 psb->fcp_bpl = psb->data;
973 psb->fcp_cmnd = (psb->data + phba->cfg_sg_dma_buf_size)
974 - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
975 psb->fcp_rsp = (struct fcp_rsp *)((uint8_t *)psb->fcp_cmnd +
976 sizeof(struct fcp_cmnd));
977
978 /* Initialize local short-hand pointers. */
979 sgl = (struct sli4_sge *)psb->fcp_bpl;
980 pdma_phys_bpl = psb->dma_handle;
981 pdma_phys_fcp_cmd =
982 (psb->dma_handle + phba->cfg_sg_dma_buf_size)
983 - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
984 pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
985
986 /*
James Smart8a9d2e82012-05-09 21:16:12 -0400987 * The first two bdes are the FCP_CMD and FCP_RSP.
988 * The balance are sg list bdes. Initialize the
989 * first two and leave the rest for queuecommand.
James Smartda0436e2009-05-22 14:51:39 -0400990 */
991 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
992 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
James Smart05580562011-05-24 11:40:48 -0400993 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -0400994 bf_set(lpfc_sli4_sge_last, sgl, 0);
995 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -0500996 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_cmnd));
James Smartda0436e2009-05-22 14:51:39 -0400997 sgl++;
998
999 /* Setup the physical region for the FCP RSP */
1000 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
1001 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
James Smart05580562011-05-24 11:40:48 -04001002 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -04001003 bf_set(lpfc_sli4_sge_last, sgl, 1);
1004 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05001005 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
James Smartda0436e2009-05-22 14:51:39 -04001006
1007 /*
1008 * Since the IOCB for the FCP I/O is built into this
1009 * lpfc_scsi_buf, initialize it with all known data now.
1010 */
1011 iocb = &psb->cur_iocbq.iocb;
1012 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
1013 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
1014 /* setting the BLP size to 2 * sizeof BDE may not be correct.
1015 * We are setting the bpl to point to out sgl. An sgl's
1016 * entries are 16 bytes, a bpl entries are 12 bytes.
1017 */
1018 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
1019 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
1020 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
1021 iocb->ulpBdeCount = 1;
1022 iocb->ulpLe = 1;
1023 iocb->ulpClass = CLASS3;
James Smart8a9d2e82012-05-09 21:16:12 -04001024 psb->cur_iocbq.context1 = psb;
James Smartda0436e2009-05-22 14:51:39 -04001025 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
1026 pdma_phys_bpl1 = pdma_phys_bpl + SGL_PAGE_SIZE;
1027 else
1028 pdma_phys_bpl1 = 0;
1029 psb->dma_phys_bpl = pdma_phys_bpl;
James Smart6d368e52011-05-24 11:44:12 -04001030
James Smart8a9d2e82012-05-09 21:16:12 -04001031 /* add the scsi buffer to a post list */
1032 list_add_tail(&psb->list, &post_sblist);
1033 spin_lock_irq(&phba->scsi_buf_list_lock);
1034 phba->sli4_hba.scsi_xri_cnt++;
1035 spin_unlock_irq(&phba->scsi_buf_list_lock);
James Smartda0436e2009-05-22 14:51:39 -04001036 }
James Smart8a9d2e82012-05-09 21:16:12 -04001037 lpfc_printf_log(phba, KERN_INFO, LOG_BG,
1038 "3021 Allocate %d out of %d requested new SCSI "
1039 "buffers\n", bcnt, num_to_alloc);
James Smartda0436e2009-05-22 14:51:39 -04001040
James Smart8a9d2e82012-05-09 21:16:12 -04001041 /* post the list of scsi buffer sgls to port if available */
1042 if (!list_empty(&post_sblist))
1043 num_posted = lpfc_sli4_post_scsi_sgl_list(phba,
1044 &post_sblist, bcnt);
1045 else
1046 num_posted = 0;
1047
1048 return num_posted;
James Smartda0436e2009-05-22 14:51:39 -04001049}
1050
1051/**
James Smart3772a992009-05-22 14:50:54 -04001052 * lpfc_new_scsi_buf - Wrapper funciton for scsi buffer allocator
1053 * @vport: The virtual port for which this call being executed.
1054 * @num_to_allocate: The requested number of buffers to allocate.
1055 *
1056 * This routine wraps the actual SCSI buffer allocator function pointer from
1057 * the lpfc_hba struct.
1058 *
1059 * Return codes:
1060 * int - number of scsi buffers that were allocated.
1061 * 0 = failure, less than num_to_alloc is a partial failure.
1062 **/
1063static inline int
1064lpfc_new_scsi_buf(struct lpfc_vport *vport, int num_to_alloc)
1065{
1066 return vport->phba->lpfc_new_scsi_buf(vport, num_to_alloc);
1067}
1068
1069/**
James Smart19ca7602010-11-20 23:11:55 -05001070 * lpfc_get_scsi_buf_s3 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
James Smart3772a992009-05-22 14:50:54 -04001071 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -05001072 *
1073 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1074 * and returns to caller.
1075 *
1076 * Return codes:
1077 * NULL - Error
1078 * Pointer to lpfc_scsi_buf - Success
1079 **/
Adrian Bunk455c53e2006-01-06 20:21:28 +01001080static struct lpfc_scsi_buf*
James Smart19ca7602010-11-20 23:11:55 -05001081lpfc_get_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001082{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001083 struct lpfc_scsi_buf * lpfc_cmd = NULL;
1084 struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001085 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -05001086
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001087 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001088 list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
James Smart1dcb58e2007-04-25 09:51:30 -04001089 if (lpfc_cmd) {
1090 lpfc_cmd->seg_cnt = 0;
1091 lpfc_cmd->nonsg_phys = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05001092 lpfc_cmd->prot_seg_cnt = 0;
James Smart1dcb58e2007-04-25 09:51:30 -04001093 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001094 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001095 return lpfc_cmd;
1096}
James Smart19ca7602010-11-20 23:11:55 -05001097/**
1098 * lpfc_get_scsi_buf_s4 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1099 * @phba: The HBA for which this call is being executed.
1100 *
1101 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1102 * and returns to caller.
1103 *
1104 * Return codes:
1105 * NULL - Error
1106 * Pointer to lpfc_scsi_buf - Success
1107 **/
1108static struct lpfc_scsi_buf*
1109lpfc_get_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1110{
James Smart1151e3e2011-02-16 12:39:35 -05001111 struct lpfc_scsi_buf *lpfc_cmd ;
James Smart19ca7602010-11-20 23:11:55 -05001112 unsigned long iflag = 0;
1113 int found = 0;
1114
1115 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James Smart1151e3e2011-02-16 12:39:35 -05001116 list_for_each_entry(lpfc_cmd, &phba->lpfc_scsi_buf_list,
1117 list) {
James Smart19ca7602010-11-20 23:11:55 -05001118 if (lpfc_test_rrq_active(phba, ndlp,
James Smartee0f4fe2012-05-09 21:19:14 -04001119 lpfc_cmd->cur_iocbq.sli4_lxritag))
James Smart1151e3e2011-02-16 12:39:35 -05001120 continue;
1121 list_del(&lpfc_cmd->list);
James Smart19ca7602010-11-20 23:11:55 -05001122 found = 1;
1123 lpfc_cmd->seg_cnt = 0;
1124 lpfc_cmd->nonsg_phys = 0;
1125 lpfc_cmd->prot_seg_cnt = 0;
James Smart1151e3e2011-02-16 12:39:35 -05001126 break;
James Smart19ca7602010-11-20 23:11:55 -05001127 }
James Smart1151e3e2011-02-16 12:39:35 -05001128 spin_unlock_irqrestore(&phba->scsi_buf_list_lock,
1129 iflag);
1130 if (!found)
1131 return NULL;
1132 else
1133 return lpfc_cmd;
James Smart19ca7602010-11-20 23:11:55 -05001134}
1135/**
1136 * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1137 * @phba: The HBA for which this call is being executed.
1138 *
1139 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1140 * and returns to caller.
1141 *
1142 * Return codes:
1143 * NULL - Error
1144 * Pointer to lpfc_scsi_buf - Success
1145 **/
1146static struct lpfc_scsi_buf*
1147lpfc_get_scsi_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1148{
1149 return phba->lpfc_get_scsi_buf(phba, ndlp);
1150}
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001151
James Smart9bad7672008-12-04 22:39:02 -05001152/**
James Smart3772a992009-05-22 14:50:54 -04001153 * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
James Smart9bad7672008-12-04 22:39:02 -05001154 * @phba: The Hba for which this call is being executed.
1155 * @psb: The scsi buffer which is being released.
1156 *
1157 * This routine releases @psb scsi buffer by adding it to tail of @phba
1158 * lpfc_scsi_buf_list list.
1159 **/
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001160static void
James Smart3772a992009-05-22 14:50:54 -04001161lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001162{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001163 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -05001164
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001165 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001166 psb->pCmd = NULL;
dea31012005-04-17 16:05:31 -05001167 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001168 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
dea31012005-04-17 16:05:31 -05001169}
1170
James Smart9bad7672008-12-04 22:39:02 -05001171/**
James Smartda0436e2009-05-22 14:51:39 -04001172 * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
1173 * @phba: The Hba for which this call is being executed.
1174 * @psb: The scsi buffer which is being released.
1175 *
1176 * This routine releases @psb scsi buffer by adding it to tail of @phba
1177 * lpfc_scsi_buf_list list. For SLI4 XRI's are tied to the scsi buffer
1178 * and cannot be reused for at least RA_TOV amount of time if it was
1179 * aborted.
1180 **/
1181static void
1182lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1183{
1184 unsigned long iflag = 0;
1185
James Smart341af102010-01-26 23:07:37 -05001186 if (psb->exch_busy) {
James Smartda0436e2009-05-22 14:51:39 -04001187 spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock,
1188 iflag);
1189 psb->pCmd = NULL;
1190 list_add_tail(&psb->list,
1191 &phba->sli4_hba.lpfc_abts_scsi_buf_list);
1192 spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
1193 iflag);
1194 } else {
1195
1196 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
1197 psb->pCmd = NULL;
1198 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
1199 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
1200 }
1201}
1202
1203/**
James Smart3772a992009-05-22 14:50:54 -04001204 * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
1205 * @phba: The Hba for which this call is being executed.
1206 * @psb: The scsi buffer which is being released.
1207 *
1208 * This routine releases @psb scsi buffer by adding it to tail of @phba
1209 * lpfc_scsi_buf_list list.
1210 **/
1211static void
1212lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1213{
1214
1215 phba->lpfc_release_scsi_buf(phba, psb);
1216}
1217
1218/**
1219 * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -05001220 * @phba: The Hba for which this call is being executed.
1221 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1222 *
1223 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
James Smart3772a992009-05-22 14:50:54 -04001224 * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
1225 * through sg elements and format the bdea. This routine also initializes all
1226 * IOCB fields which are dependent on scsi command request buffer.
James Smart9bad7672008-12-04 22:39:02 -05001227 *
1228 * Return codes:
1229 * 1 - Error
1230 * 0 - Success
1231 **/
dea31012005-04-17 16:05:31 -05001232static int
James Smart3772a992009-05-22 14:50:54 -04001233lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
dea31012005-04-17 16:05:31 -05001234{
1235 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1236 struct scatterlist *sgel = NULL;
1237 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1238 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
James Smart0f65ff62010-02-26 14:14:23 -05001239 struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
dea31012005-04-17 16:05:31 -05001240 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart34b02dc2008-08-24 21:49:55 -04001241 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
dea31012005-04-17 16:05:31 -05001242 dma_addr_t physaddr;
James Smart34b02dc2008-08-24 21:49:55 -04001243 uint32_t num_bde = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001244 int nseg, datadir = scsi_cmnd->sc_data_direction;
dea31012005-04-17 16:05:31 -05001245
1246 /*
1247 * There are three possibilities here - use scatter-gather segment, use
1248 * the single mapping, or neither. Start the lpfc command prep by
1249 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1250 * data bde entry.
1251 */
1252 bpl += 2;
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001253 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05001254 /*
1255 * The driver stores the segment count returned from pci_map_sg
1256 * because this a count of dma-mappings used to map the use_sg
1257 * pages. They are not guaranteed to be the same for those
1258 * architectures that implement an IOMMU.
1259 */
dea31012005-04-17 16:05:31 -05001260
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001261 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
1262 scsi_sg_count(scsi_cmnd), datadir);
1263 if (unlikely(!nseg))
1264 return 1;
1265
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001266 lpfc_cmd->seg_cnt = nseg;
dea31012005-04-17 16:05:31 -05001267 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001268 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1269 "9064 BLKGRD: %s: Too many sg segments from "
James Smarte2a0a9d2008-12-04 22:40:02 -05001270 "dma_map_sg. Config %d, seg_cnt %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001271 __func__, phba->cfg_sg_seg_cnt,
dea31012005-04-17 16:05:31 -05001272 lpfc_cmd->seg_cnt);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001273 scsi_dma_unmap(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001274 return 1;
1275 }
1276
1277 /*
1278 * The driver established a maximum scatter-gather segment count
1279 * during probe that limits the number of sg elements in any
1280 * single scsi command. Just run through the seg_cnt and format
1281 * the bde's.
James Smart34b02dc2008-08-24 21:49:55 -04001282 * When using SLI-3 the driver will try to fit all the BDEs into
1283 * the IOCB. If it can't then the BDEs get added to a BPL as it
1284 * does for SLI-2 mode.
dea31012005-04-17 16:05:31 -05001285 */
James Smart34b02dc2008-08-24 21:49:55 -04001286 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
dea31012005-04-17 16:05:31 -05001287 physaddr = sg_dma_address(sgel);
James Smart34b02dc2008-08-24 21:49:55 -04001288 if (phba->sli_rev == 3 &&
James Smarte2a0a9d2008-12-04 22:40:02 -05001289 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
James Smart0f65ff62010-02-26 14:14:23 -05001290 !(iocbq->iocb_flag & DSS_SECURITY_OP) &&
James Smart34b02dc2008-08-24 21:49:55 -04001291 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
1292 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1293 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
1294 data_bde->addrLow = putPaddrLow(physaddr);
1295 data_bde->addrHigh = putPaddrHigh(physaddr);
1296 data_bde++;
1297 } else {
1298 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1299 bpl->tus.f.bdeSize = sg_dma_len(sgel);
1300 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1301 bpl->addrLow =
1302 le32_to_cpu(putPaddrLow(physaddr));
1303 bpl->addrHigh =
1304 le32_to_cpu(putPaddrHigh(physaddr));
1305 bpl++;
1306 }
dea31012005-04-17 16:05:31 -05001307 }
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001308 }
dea31012005-04-17 16:05:31 -05001309
1310 /*
1311 * Finish initializing those IOCB fields that are dependent on the
James Smart34b02dc2008-08-24 21:49:55 -04001312 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
1313 * explicitly reinitialized and for SLI-3 the extended bde count is
1314 * explicitly reinitialized since all iocb memory resources are reused.
dea31012005-04-17 16:05:31 -05001315 */
James Smarte2a0a9d2008-12-04 22:40:02 -05001316 if (phba->sli_rev == 3 &&
James Smart0f65ff62010-02-26 14:14:23 -05001317 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
1318 !(iocbq->iocb_flag & DSS_SECURITY_OP)) {
James Smart34b02dc2008-08-24 21:49:55 -04001319 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
1320 /*
1321 * The extended IOCB format can only fit 3 BDE or a BPL.
1322 * This I/O has more than 3 BDE so the 1st data bde will
1323 * be a BPL that is filled in here.
1324 */
1325 physaddr = lpfc_cmd->dma_handle;
1326 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
1327 data_bde->tus.f.bdeSize = (num_bde *
1328 sizeof(struct ulp_bde64));
1329 physaddr += (sizeof(struct fcp_cmnd) +
1330 sizeof(struct fcp_rsp) +
1331 (2 * sizeof(struct ulp_bde64)));
1332 data_bde->addrHigh = putPaddrHigh(physaddr);
1333 data_bde->addrLow = putPaddrLow(physaddr);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001334 /* ebde count includes the response bde and data bpl */
James Smart34b02dc2008-08-24 21:49:55 -04001335 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
1336 } else {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001337 /* ebde count includes the response bde and data bdes */
James Smart34b02dc2008-08-24 21:49:55 -04001338 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1339 }
1340 } else {
1341 iocb_cmd->un.fcpi64.bdl.bdeSize =
1342 ((num_bde + 2) * sizeof(struct ulp_bde64));
James Smart0f65ff62010-02-26 14:14:23 -05001343 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
James Smart34b02dc2008-08-24 21:49:55 -04001344 }
James Smart09372822008-01-11 01:52:54 -05001345 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
James Smarte2a0a9d2008-12-04 22:40:02 -05001346
1347 /*
1348 * Due to difference in data length between DIF/non-DIF paths,
1349 * we need to set word 4 of IOCB here
1350 */
James Smarta257bf92009-04-06 18:48:10 -04001351 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001352 return 0;
1353}
1354
James Smartf9bb2da2011-10-10 21:34:11 -04001355static inline unsigned
1356lpfc_cmd_blksize(struct scsi_cmnd *sc)
1357{
1358 return sc->device->sector_size;
1359}
1360
1361#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001362
James Smart9a6b09c2012-03-01 22:37:42 -05001363/* Return if if error injection is detected by Initiator */
1364#define BG_ERR_INIT 0x1
1365/* Return if if error injection is detected by Target */
1366#define BG_ERR_TGT 0x2
1367/* Return if if swapping CSUM<-->CRC is required for error injection */
1368#define BG_ERR_SWAP 0x10
1369/* Return if disabling Guard/Ref/App checking is required for error injection */
1370#define BG_ERR_CHECK 0x20
James Smartacd68592012-01-18 16:25:09 -05001371
1372/**
1373 * lpfc_bg_err_inject - Determine if we should inject an error
1374 * @phba: The Hba for which this call is being executed.
James Smartf9bb2da2011-10-10 21:34:11 -04001375 * @sc: The SCSI command to examine
1376 * @reftag: (out) BlockGuard reference tag for transmitted data
1377 * @apptag: (out) BlockGuard application tag for transmitted data
1378 * @new_guard (in) Value to replace CRC with if needed
1379 *
James Smart9a6b09c2012-03-01 22:37:42 -05001380 * Returns BG_ERR_* bit mask or 0 if request ignored
James Smartacd68592012-01-18 16:25:09 -05001381 **/
James Smartf9bb2da2011-10-10 21:34:11 -04001382static int
1383lpfc_bg_err_inject(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1384 uint32_t *reftag, uint16_t *apptag, uint32_t new_guard)
1385{
1386 struct scatterlist *sgpe; /* s/g prot entry */
1387 struct scatterlist *sgde; /* s/g data entry */
James Smart9a6b09c2012-03-01 22:37:42 -05001388 struct lpfc_scsi_buf *lpfc_cmd = NULL;
James Smartacd68592012-01-18 16:25:09 -05001389 struct scsi_dif_tuple *src = NULL;
James Smart4ac9b222012-03-01 22:38:29 -05001390 struct lpfc_nodelist *ndlp;
1391 struct lpfc_rport_data *rdata;
James Smartf9bb2da2011-10-10 21:34:11 -04001392 uint32_t op = scsi_get_prot_op(sc);
1393 uint32_t blksize;
1394 uint32_t numblks;
1395 sector_t lba;
1396 int rc = 0;
James Smartacd68592012-01-18 16:25:09 -05001397 int blockoff = 0;
James Smartf9bb2da2011-10-10 21:34:11 -04001398
1399 if (op == SCSI_PROT_NORMAL)
1400 return 0;
1401
James Smartacd68592012-01-18 16:25:09 -05001402 sgpe = scsi_prot_sglist(sc);
1403 sgde = scsi_sglist(sc);
James Smartf9bb2da2011-10-10 21:34:11 -04001404 lba = scsi_get_lba(sc);
James Smart4ac9b222012-03-01 22:38:29 -05001405
1406 /* First check if we need to match the LBA */
James Smartf9bb2da2011-10-10 21:34:11 -04001407 if (phba->lpfc_injerr_lba != LPFC_INJERR_LBA_OFF) {
1408 blksize = lpfc_cmd_blksize(sc);
1409 numblks = (scsi_bufflen(sc) + blksize - 1) / blksize;
1410
1411 /* Make sure we have the right LBA if one is specified */
1412 if ((phba->lpfc_injerr_lba < lba) ||
1413 (phba->lpfc_injerr_lba >= (lba + numblks)))
1414 return 0;
James Smartacd68592012-01-18 16:25:09 -05001415 if (sgpe) {
1416 blockoff = phba->lpfc_injerr_lba - lba;
1417 numblks = sg_dma_len(sgpe) /
1418 sizeof(struct scsi_dif_tuple);
1419 if (numblks < blockoff)
1420 blockoff = numblks;
James Smartacd68592012-01-18 16:25:09 -05001421 }
James Smartf9bb2da2011-10-10 21:34:11 -04001422 }
1423
James Smart4ac9b222012-03-01 22:38:29 -05001424 /* Next check if we need to match the remote NPortID or WWPN */
1425 rdata = sc->device->hostdata;
1426 if (rdata && rdata->pnode) {
1427 ndlp = rdata->pnode;
1428
1429 /* Make sure we have the right NPortID if one is specified */
1430 if (phba->lpfc_injerr_nportid &&
1431 (phba->lpfc_injerr_nportid != ndlp->nlp_DID))
1432 return 0;
1433
1434 /*
1435 * Make sure we have the right WWPN if one is specified.
1436 * wwn[0] should be a non-zero NAA in a good WWPN.
1437 */
1438 if (phba->lpfc_injerr_wwpn.u.wwn[0] &&
1439 (memcmp(&ndlp->nlp_portname, &phba->lpfc_injerr_wwpn,
1440 sizeof(struct lpfc_name)) != 0))
1441 return 0;
1442 }
1443
1444 /* Setup a ptr to the protection data if the SCSI host provides it */
1445 if (sgpe) {
1446 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
1447 src += blockoff;
1448 lpfc_cmd = (struct lpfc_scsi_buf *)sc->host_scribble;
1449 }
1450
James Smartf9bb2da2011-10-10 21:34:11 -04001451 /* Should we change the Reference Tag */
1452 if (reftag) {
James Smartacd68592012-01-18 16:25:09 -05001453 if (phba->lpfc_injerr_wref_cnt) {
1454 switch (op) {
1455 case SCSI_PROT_WRITE_PASS:
James Smart9a6b09c2012-03-01 22:37:42 -05001456 if (src) {
1457 /*
1458 * For WRITE_PASS, force the error
1459 * to be sent on the wire. It should
1460 * be detected by the Target.
1461 * If blockoff != 0 error will be
1462 * inserted in middle of the IO.
1463 */
James Smartf9bb2da2011-10-10 21:34:11 -04001464
James Smartacd68592012-01-18 16:25:09 -05001465 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1466 "9076 BLKGRD: Injecting reftag error: "
1467 "write lba x%lx + x%x oldrefTag x%x\n",
1468 (unsigned long)lba, blockoff,
James Smart9a6b09c2012-03-01 22:37:42 -05001469 be32_to_cpu(src->ref_tag));
James Smartacd68592012-01-18 16:25:09 -05001470
1471 /*
James Smart9a6b09c2012-03-01 22:37:42 -05001472 * Save the old ref_tag so we can
1473 * restore it on completion.
James Smartacd68592012-01-18 16:25:09 -05001474 */
James Smart9a6b09c2012-03-01 22:37:42 -05001475 if (lpfc_cmd) {
1476 lpfc_cmd->prot_data_type =
1477 LPFC_INJERR_REFTAG;
1478 lpfc_cmd->prot_data_segment =
1479 src;
1480 lpfc_cmd->prot_data =
1481 src->ref_tag;
1482 }
1483 src->ref_tag = cpu_to_be32(0xDEADBEEF);
James Smartacd68592012-01-18 16:25:09 -05001484 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001485 if (phba->lpfc_injerr_wref_cnt == 0) {
1486 phba->lpfc_injerr_nportid = 0;
1487 phba->lpfc_injerr_lba =
1488 LPFC_INJERR_LBA_OFF;
1489 memset(&phba->lpfc_injerr_wwpn,
1490 0, sizeof(struct lpfc_name));
1491 }
James Smart9a6b09c2012-03-01 22:37:42 -05001492 rc = BG_ERR_TGT | BG_ERR_CHECK;
1493
James Smartacd68592012-01-18 16:25:09 -05001494 break;
1495 }
1496 /* Drop thru */
James Smart9a6b09c2012-03-01 22:37:42 -05001497 case SCSI_PROT_WRITE_INSERT:
1498 /*
1499 * For WRITE_INSERT, force the error
1500 * to be sent on the wire. It should be
1501 * detected by the Target.
1502 */
1503 /* DEADBEEF will be the reftag on the wire */
1504 *reftag = 0xDEADBEEF;
1505 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001506 if (phba->lpfc_injerr_wref_cnt == 0) {
1507 phba->lpfc_injerr_nportid = 0;
1508 phba->lpfc_injerr_lba =
1509 LPFC_INJERR_LBA_OFF;
1510 memset(&phba->lpfc_injerr_wwpn,
1511 0, sizeof(struct lpfc_name));
1512 }
James Smart9a6b09c2012-03-01 22:37:42 -05001513 rc = BG_ERR_TGT | BG_ERR_CHECK;
1514
1515 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1516 "9078 BLKGRD: Injecting reftag error: "
1517 "write lba x%lx\n", (unsigned long)lba);
1518 break;
James Smartacd68592012-01-18 16:25:09 -05001519 case SCSI_PROT_WRITE_STRIP:
1520 /*
1521 * For WRITE_STRIP and WRITE_PASS,
1522 * force the error on data
1523 * being copied from SLI-Host to SLI-Port.
1524 */
1525 *reftag = 0xDEADBEEF;
1526 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001527 if (phba->lpfc_injerr_wref_cnt == 0) {
1528 phba->lpfc_injerr_nportid = 0;
1529 phba->lpfc_injerr_lba =
1530 LPFC_INJERR_LBA_OFF;
1531 memset(&phba->lpfc_injerr_wwpn,
1532 0, sizeof(struct lpfc_name));
1533 }
James Smartacd68592012-01-18 16:25:09 -05001534 rc = BG_ERR_INIT;
1535
1536 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1537 "9077 BLKGRD: Injecting reftag error: "
1538 "write lba x%lx\n", (unsigned long)lba);
1539 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001540 }
James Smartacd68592012-01-18 16:25:09 -05001541 }
1542 if (phba->lpfc_injerr_rref_cnt) {
1543 switch (op) {
1544 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001545 case SCSI_PROT_READ_STRIP:
1546 case SCSI_PROT_READ_PASS:
1547 /*
1548 * For READ_STRIP and READ_PASS, force the
1549 * error on data being read off the wire. It
1550 * should force an IO error to the driver.
1551 */
James Smartf9bb2da2011-10-10 21:34:11 -04001552 *reftag = 0xDEADBEEF;
1553 phba->lpfc_injerr_rref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001554 if (phba->lpfc_injerr_rref_cnt == 0) {
1555 phba->lpfc_injerr_nportid = 0;
1556 phba->lpfc_injerr_lba =
1557 LPFC_INJERR_LBA_OFF;
1558 memset(&phba->lpfc_injerr_wwpn,
1559 0, sizeof(struct lpfc_name));
1560 }
James Smartacd68592012-01-18 16:25:09 -05001561 rc = BG_ERR_INIT;
James Smartf9bb2da2011-10-10 21:34:11 -04001562
1563 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smartacd68592012-01-18 16:25:09 -05001564 "9079 BLKGRD: Injecting reftag error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001565 "read lba x%lx\n", (unsigned long)lba);
James Smartacd68592012-01-18 16:25:09 -05001566 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001567 }
1568 }
1569 }
1570
1571 /* Should we change the Application Tag */
1572 if (apptag) {
James Smartacd68592012-01-18 16:25:09 -05001573 if (phba->lpfc_injerr_wapp_cnt) {
1574 switch (op) {
1575 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001576 if (src) {
James Smart9a6b09c2012-03-01 22:37:42 -05001577 /*
1578 * For WRITE_PASS, force the error
1579 * to be sent on the wire. It should
1580 * be detected by the Target.
1581 * If blockoff != 0 error will be
1582 * inserted in middle of the IO.
1583 */
1584
James Smartacd68592012-01-18 16:25:09 -05001585 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1586 "9080 BLKGRD: Injecting apptag error: "
1587 "write lba x%lx + x%x oldappTag x%x\n",
1588 (unsigned long)lba, blockoff,
James Smart9a6b09c2012-03-01 22:37:42 -05001589 be16_to_cpu(src->app_tag));
James Smartacd68592012-01-18 16:25:09 -05001590
1591 /*
James Smart9a6b09c2012-03-01 22:37:42 -05001592 * Save the old app_tag so we can
1593 * restore it on completion.
James Smartacd68592012-01-18 16:25:09 -05001594 */
James Smart9a6b09c2012-03-01 22:37:42 -05001595 if (lpfc_cmd) {
1596 lpfc_cmd->prot_data_type =
1597 LPFC_INJERR_APPTAG;
1598 lpfc_cmd->prot_data_segment =
1599 src;
1600 lpfc_cmd->prot_data =
1601 src->app_tag;
1602 }
1603 src->app_tag = cpu_to_be16(0xDEAD);
James Smartacd68592012-01-18 16:25:09 -05001604 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001605 if (phba->lpfc_injerr_wapp_cnt == 0) {
1606 phba->lpfc_injerr_nportid = 0;
1607 phba->lpfc_injerr_lba =
1608 LPFC_INJERR_LBA_OFF;
1609 memset(&phba->lpfc_injerr_wwpn,
1610 0, sizeof(struct lpfc_name));
1611 }
James Smart9a6b09c2012-03-01 22:37:42 -05001612 rc = BG_ERR_TGT | BG_ERR_CHECK;
James Smartacd68592012-01-18 16:25:09 -05001613 break;
1614 }
1615 /* Drop thru */
James Smart9a6b09c2012-03-01 22:37:42 -05001616 case SCSI_PROT_WRITE_INSERT:
1617 /*
1618 * For WRITE_INSERT, force the
1619 * error to be sent on the wire. It should be
1620 * detected by the Target.
1621 */
1622 /* DEAD will be the apptag on the wire */
1623 *apptag = 0xDEAD;
1624 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001625 if (phba->lpfc_injerr_wapp_cnt == 0) {
1626 phba->lpfc_injerr_nportid = 0;
1627 phba->lpfc_injerr_lba =
1628 LPFC_INJERR_LBA_OFF;
1629 memset(&phba->lpfc_injerr_wwpn,
1630 0, sizeof(struct lpfc_name));
1631 }
James Smart9a6b09c2012-03-01 22:37:42 -05001632 rc = BG_ERR_TGT | BG_ERR_CHECK;
1633
1634 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1635 "0813 BLKGRD: Injecting apptag error: "
1636 "write lba x%lx\n", (unsigned long)lba);
1637 break;
James Smartacd68592012-01-18 16:25:09 -05001638 case SCSI_PROT_WRITE_STRIP:
1639 /*
1640 * For WRITE_STRIP and WRITE_PASS,
1641 * force the error on data
1642 * being copied from SLI-Host to SLI-Port.
1643 */
1644 *apptag = 0xDEAD;
1645 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001646 if (phba->lpfc_injerr_wapp_cnt == 0) {
1647 phba->lpfc_injerr_nportid = 0;
1648 phba->lpfc_injerr_lba =
1649 LPFC_INJERR_LBA_OFF;
1650 memset(&phba->lpfc_injerr_wwpn,
1651 0, sizeof(struct lpfc_name));
1652 }
James Smartacd68592012-01-18 16:25:09 -05001653 rc = BG_ERR_INIT;
1654
1655 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1656 "0812 BLKGRD: Injecting apptag error: "
1657 "write lba x%lx\n", (unsigned long)lba);
1658 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001659 }
James Smartacd68592012-01-18 16:25:09 -05001660 }
1661 if (phba->lpfc_injerr_rapp_cnt) {
1662 switch (op) {
1663 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001664 case SCSI_PROT_READ_STRIP:
1665 case SCSI_PROT_READ_PASS:
1666 /*
1667 * For READ_STRIP and READ_PASS, force the
1668 * error on data being read off the wire. It
1669 * should force an IO error to the driver.
1670 */
James Smartf9bb2da2011-10-10 21:34:11 -04001671 *apptag = 0xDEAD;
1672 phba->lpfc_injerr_rapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001673 if (phba->lpfc_injerr_rapp_cnt == 0) {
1674 phba->lpfc_injerr_nportid = 0;
1675 phba->lpfc_injerr_lba =
1676 LPFC_INJERR_LBA_OFF;
1677 memset(&phba->lpfc_injerr_wwpn,
1678 0, sizeof(struct lpfc_name));
1679 }
James Smartacd68592012-01-18 16:25:09 -05001680 rc = BG_ERR_INIT;
James Smartf9bb2da2011-10-10 21:34:11 -04001681
1682 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smartacd68592012-01-18 16:25:09 -05001683 "0814 BLKGRD: Injecting apptag error: "
1684 "read lba x%lx\n", (unsigned long)lba);
1685 break;
1686 }
1687 }
1688 }
1689
1690
1691 /* Should we change the Guard Tag */
1692 if (new_guard) {
1693 if (phba->lpfc_injerr_wgrd_cnt) {
1694 switch (op) {
1695 case SCSI_PROT_WRITE_PASS:
James Smart9a6b09c2012-03-01 22:37:42 -05001696 rc = BG_ERR_CHECK;
James Smartacd68592012-01-18 16:25:09 -05001697 /* Drop thru */
James Smartacd68592012-01-18 16:25:09 -05001698
James Smartacd68592012-01-18 16:25:09 -05001699 case SCSI_PROT_WRITE_INSERT:
1700 /*
1701 * For WRITE_INSERT, force the
1702 * error to be sent on the wire. It should be
1703 * detected by the Target.
1704 */
1705 phba->lpfc_injerr_wgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001706 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1707 phba->lpfc_injerr_nportid = 0;
1708 phba->lpfc_injerr_lba =
1709 LPFC_INJERR_LBA_OFF;
1710 memset(&phba->lpfc_injerr_wwpn,
1711 0, sizeof(struct lpfc_name));
1712 }
James Smartacd68592012-01-18 16:25:09 -05001713
James Smart9a6b09c2012-03-01 22:37:42 -05001714 rc |= BG_ERR_TGT | BG_ERR_SWAP;
James Smartacd68592012-01-18 16:25:09 -05001715 /* Signals the caller to swap CRC->CSUM */
1716
1717 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1718 "0817 BLKGRD: Injecting guard error: "
1719 "write lba x%lx\n", (unsigned long)lba);
1720 break;
James Smart9a6b09c2012-03-01 22:37:42 -05001721 case SCSI_PROT_WRITE_STRIP:
1722 /*
1723 * For WRITE_STRIP and WRITE_PASS,
1724 * force the error on data
1725 * being copied from SLI-Host to SLI-Port.
1726 */
1727 phba->lpfc_injerr_wgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001728 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1729 phba->lpfc_injerr_nportid = 0;
1730 phba->lpfc_injerr_lba =
1731 LPFC_INJERR_LBA_OFF;
1732 memset(&phba->lpfc_injerr_wwpn,
1733 0, sizeof(struct lpfc_name));
1734 }
James Smart9a6b09c2012-03-01 22:37:42 -05001735
1736 rc = BG_ERR_INIT | BG_ERR_SWAP;
1737 /* Signals the caller to swap CRC->CSUM */
1738
1739 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1740 "0816 BLKGRD: Injecting guard error: "
1741 "write lba x%lx\n", (unsigned long)lba);
1742 break;
James Smartacd68592012-01-18 16:25:09 -05001743 }
1744 }
1745 if (phba->lpfc_injerr_rgrd_cnt) {
1746 switch (op) {
1747 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001748 case SCSI_PROT_READ_STRIP:
1749 case SCSI_PROT_READ_PASS:
1750 /*
1751 * For READ_STRIP and READ_PASS, force the
1752 * error on data being read off the wire. It
1753 * should force an IO error to the driver.
1754 */
James Smartacd68592012-01-18 16:25:09 -05001755 phba->lpfc_injerr_rgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001756 if (phba->lpfc_injerr_rgrd_cnt == 0) {
1757 phba->lpfc_injerr_nportid = 0;
1758 phba->lpfc_injerr_lba =
1759 LPFC_INJERR_LBA_OFF;
1760 memset(&phba->lpfc_injerr_wwpn,
1761 0, sizeof(struct lpfc_name));
1762 }
James Smartacd68592012-01-18 16:25:09 -05001763
James Smart9a6b09c2012-03-01 22:37:42 -05001764 rc = BG_ERR_INIT | BG_ERR_SWAP;
James Smartacd68592012-01-18 16:25:09 -05001765 /* Signals the caller to swap CRC->CSUM */
1766
1767 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1768 "0818 BLKGRD: Injecting guard error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001769 "read lba x%lx\n", (unsigned long)lba);
1770 }
1771 }
1772 }
1773
James Smartf9bb2da2011-10-10 21:34:11 -04001774 return rc;
1775}
1776#endif
1777
James Smartacd68592012-01-18 16:25:09 -05001778/**
1779 * lpfc_sc_to_bg_opcodes - Determine the BlockGuard opcodes to be used with
1780 * the specified SCSI command.
1781 * @phba: The Hba for which this call is being executed.
James Smart6c8eea52010-04-06 14:49:53 -04001782 * @sc: The SCSI command to examine
1783 * @txopt: (out) BlockGuard operation for transmitted data
1784 * @rxopt: (out) BlockGuard operation for received data
1785 *
1786 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1787 *
James Smartacd68592012-01-18 16:25:09 -05001788 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001789static int
James Smart6c8eea52010-04-06 14:49:53 -04001790lpfc_sc_to_bg_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1791 uint8_t *txop, uint8_t *rxop)
James Smarte2a0a9d2008-12-04 22:40:02 -05001792{
1793 uint8_t guard_type = scsi_host_get_guard(sc->device->host);
James Smart6c8eea52010-04-06 14:49:53 -04001794 uint8_t ret = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05001795
1796 if (guard_type == SHOST_DIX_GUARD_IP) {
1797 switch (scsi_get_prot_op(sc)) {
1798 case SCSI_PROT_READ_INSERT:
1799 case SCSI_PROT_WRITE_STRIP:
James Smart6c8eea52010-04-06 14:49:53 -04001800 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001801 *txop = BG_OP_IN_CSUM_OUT_NODIF;
James Smarte2a0a9d2008-12-04 22:40:02 -05001802 break;
1803
1804 case SCSI_PROT_READ_STRIP:
1805 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001806 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001807 *txop = BG_OP_IN_NODIF_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001808 break;
1809
Martin K. Petersenc6af4042009-09-18 17:32:59 -04001810 case SCSI_PROT_READ_PASS:
1811 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001812 *rxop = BG_OP_IN_CRC_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001813 *txop = BG_OP_IN_CSUM_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001814 break;
1815
James Smarte2a0a9d2008-12-04 22:40:02 -05001816 case SCSI_PROT_NORMAL:
1817 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001818 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001819 "9063 BLKGRD: Bad op/guard:%d/IP combination\n",
1820 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001821 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001822 break;
1823
1824 }
James Smart7c56b9f2011-07-22 18:36:25 -04001825 } else {
James Smarte2a0a9d2008-12-04 22:40:02 -05001826 switch (scsi_get_prot_op(sc)) {
1827 case SCSI_PROT_READ_STRIP:
1828 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001829 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001830 *txop = BG_OP_IN_NODIF_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001831 break;
1832
1833 case SCSI_PROT_READ_PASS:
1834 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001835 *rxop = BG_OP_IN_CRC_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001836 *txop = BG_OP_IN_CRC_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001837 break;
1838
James Smarte2a0a9d2008-12-04 22:40:02 -05001839 case SCSI_PROT_READ_INSERT:
1840 case SCSI_PROT_WRITE_STRIP:
James Smart7c56b9f2011-07-22 18:36:25 -04001841 *rxop = BG_OP_IN_NODIF_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001842 *txop = BG_OP_IN_CRC_OUT_NODIF;
James Smart7c56b9f2011-07-22 18:36:25 -04001843 break;
1844
James Smarte2a0a9d2008-12-04 22:40:02 -05001845 case SCSI_PROT_NORMAL:
1846 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001847 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001848 "9075 BLKGRD: Bad op/guard:%d/CRC combination\n",
1849 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001850 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001851 break;
1852 }
James Smarte2a0a9d2008-12-04 22:40:02 -05001853 }
1854
James Smart6c8eea52010-04-06 14:49:53 -04001855 return ret;
James Smarte2a0a9d2008-12-04 22:40:02 -05001856}
1857
James Smartacd68592012-01-18 16:25:09 -05001858#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1859/**
1860 * lpfc_bg_err_opcodes - reDetermine the BlockGuard opcodes to be used with
1861 * the specified SCSI command in order to force a guard tag error.
1862 * @phba: The Hba for which this call is being executed.
1863 * @sc: The SCSI command to examine
1864 * @txopt: (out) BlockGuard operation for transmitted data
1865 * @rxopt: (out) BlockGuard operation for received data
1866 *
1867 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1868 *
1869 **/
1870static int
1871lpfc_bg_err_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1872 uint8_t *txop, uint8_t *rxop)
1873{
1874 uint8_t guard_type = scsi_host_get_guard(sc->device->host);
1875 uint8_t ret = 0;
1876
1877 if (guard_type == SHOST_DIX_GUARD_IP) {
1878 switch (scsi_get_prot_op(sc)) {
1879 case SCSI_PROT_READ_INSERT:
1880 case SCSI_PROT_WRITE_STRIP:
James Smartacd68592012-01-18 16:25:09 -05001881 *rxop = BG_OP_IN_NODIF_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001882 *txop = BG_OP_IN_CRC_OUT_NODIF;
James Smartacd68592012-01-18 16:25:09 -05001883 break;
1884
1885 case SCSI_PROT_READ_STRIP:
1886 case SCSI_PROT_WRITE_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001887 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001888 *txop = BG_OP_IN_NODIF_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001889 break;
1890
1891 case SCSI_PROT_READ_PASS:
1892 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001893 *rxop = BG_OP_IN_CSUM_OUT_CRC;
James Smart9a6b09c2012-03-01 22:37:42 -05001894 *txop = BG_OP_IN_CRC_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001895 break;
1896
1897 case SCSI_PROT_NORMAL:
1898 default:
1899 break;
1900
1901 }
1902 } else {
1903 switch (scsi_get_prot_op(sc)) {
1904 case SCSI_PROT_READ_STRIP:
1905 case SCSI_PROT_WRITE_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001906 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001907 *txop = BG_OP_IN_NODIF_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001908 break;
1909
1910 case SCSI_PROT_READ_PASS:
1911 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001912 *rxop = BG_OP_IN_CSUM_OUT_CSUM;
James Smart9a6b09c2012-03-01 22:37:42 -05001913 *txop = BG_OP_IN_CSUM_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001914 break;
1915
1916 case SCSI_PROT_READ_INSERT:
1917 case SCSI_PROT_WRITE_STRIP:
James Smartacd68592012-01-18 16:25:09 -05001918 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001919 *txop = BG_OP_IN_CSUM_OUT_NODIF;
James Smartacd68592012-01-18 16:25:09 -05001920 break;
1921
1922 case SCSI_PROT_NORMAL:
1923 default:
1924 break;
1925 }
1926 }
1927
1928 return ret;
1929}
1930#endif
1931
1932/**
1933 * lpfc_bg_setup_bpl - Setup BlockGuard BPL with no protection data
1934 * @phba: The Hba for which this call is being executed.
1935 * @sc: pointer to scsi command we're working on
1936 * @bpl: pointer to buffer list for protection groups
1937 * @datacnt: number of segments of data that have been dma mapped
1938 *
1939 * This function sets up BPL buffer list for protection groups of
James Smarte2a0a9d2008-12-04 22:40:02 -05001940 * type LPFC_PG_TYPE_NO_DIF
1941 *
1942 * This is usually used when the HBA is instructed to generate
1943 * DIFs and insert them into data stream (or strip DIF from
1944 * incoming data stream)
1945 *
1946 * The buffer list consists of just one protection group described
1947 * below:
1948 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001949 * start of prot group --> | PDE_5 |
1950 * +-------------------------+
1951 * | PDE_6 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001952 * +-------------------------+
1953 * | Data BDE |
1954 * +-------------------------+
1955 * |more Data BDE's ... (opt)|
1956 * +-------------------------+
1957 *
James Smarte2a0a9d2008-12-04 22:40:02 -05001958 *
1959 * Note: Data s/g buffers have been dma mapped
James Smartacd68592012-01-18 16:25:09 -05001960 *
1961 * Returns the number of BDEs added to the BPL.
1962 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001963static int
1964lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1965 struct ulp_bde64 *bpl, int datasegcnt)
1966{
1967 struct scatterlist *sgde = NULL; /* s/g data entry */
James Smart6c8eea52010-04-06 14:49:53 -04001968 struct lpfc_pde5 *pde5 = NULL;
1969 struct lpfc_pde6 *pde6 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05001970 dma_addr_t physaddr;
James Smart6c8eea52010-04-06 14:49:53 -04001971 int i = 0, num_bde = 0, status;
James Smarte2a0a9d2008-12-04 22:40:02 -05001972 int datadir = sc->sc_data_direction;
James Smart0829a192012-05-09 21:18:12 -04001973#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001974 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04001975#endif
James Smartacd68592012-01-18 16:25:09 -05001976 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001977 uint32_t reftag;
James Smart7c56b9f2011-07-22 18:36:25 -04001978 unsigned blksize;
James Smart6c8eea52010-04-06 14:49:53 -04001979 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05001980
James Smart6c8eea52010-04-06 14:49:53 -04001981 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1982 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05001983 goto out;
1984
James Smart6c8eea52010-04-06 14:49:53 -04001985 /* extract some info from the scsi command for pde*/
James Smarte2a0a9d2008-12-04 22:40:02 -05001986 blksize = lpfc_cmd_blksize(sc);
James Smartacd68592012-01-18 16:25:09 -05001987 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
James Smarte2a0a9d2008-12-04 22:40:02 -05001988
James Smartf9bb2da2011-10-10 21:34:11 -04001989#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05001990 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05001991 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05001992 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05001993 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05001994 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05001995 checking = 0;
1996 }
James Smartf9bb2da2011-10-10 21:34:11 -04001997#endif
1998
James Smart6c8eea52010-04-06 14:49:53 -04001999 /* setup PDE5 with what we have */
2000 pde5 = (struct lpfc_pde5 *) bpl;
2001 memset(pde5, 0, sizeof(struct lpfc_pde5));
2002 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05002003
James Smartbc739052010-08-04 16:11:18 -04002004 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04002005 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04002006 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04002007
James Smart6c8eea52010-04-06 14:49:53 -04002008 /* advance bpl and increment bde count */
2009 num_bde++;
2010 bpl++;
2011 pde6 = (struct lpfc_pde6 *) bpl;
2012
2013 /* setup PDE6 with the rest of the info */
2014 memset(pde6, 0, sizeof(struct lpfc_pde6));
2015 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
2016 bf_set(pde6_optx, pde6, txop);
2017 bf_set(pde6_oprx, pde6, rxop);
2018 if (datadir == DMA_FROM_DEVICE) {
James Smartacd68592012-01-18 16:25:09 -05002019 bf_set(pde6_ce, pde6, checking);
2020 bf_set(pde6_re, pde6, checking);
James Smart6c8eea52010-04-06 14:49:53 -04002021 }
2022 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04002023 bf_set(pde6_ae, pde6, 0);
2024 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04002025
James Smartbc739052010-08-04 16:11:18 -04002026 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04002027 pde6->word0 = cpu_to_le32(pde6->word0);
2028 pde6->word1 = cpu_to_le32(pde6->word1);
2029 pde6->word2 = cpu_to_le32(pde6->word2);
2030
James Smart6c8eea52010-04-06 14:49:53 -04002031 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05002032 num_bde++;
2033 bpl++;
2034
2035 /* assumption: caller has already run dma_map_sg on command data */
2036 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
2037 physaddr = sg_dma_address(sgde);
2038 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
2039 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
2040 bpl->tus.f.bdeSize = sg_dma_len(sgde);
2041 if (datadir == DMA_TO_DEVICE)
2042 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2043 else
2044 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2045 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2046 bpl++;
2047 num_bde++;
2048 }
2049
2050out:
2051 return num_bde;
2052}
2053
James Smartacd68592012-01-18 16:25:09 -05002054/**
2055 * lpfc_bg_setup_bpl_prot - Setup BlockGuard BPL with protection data
2056 * @phba: The Hba for which this call is being executed.
2057 * @sc: pointer to scsi command we're working on
2058 * @bpl: pointer to buffer list for protection groups
2059 * @datacnt: number of segments of data that have been dma mapped
2060 * @protcnt: number of segment of protection data that have been dma mapped
2061 *
2062 * This function sets up BPL buffer list for protection groups of
2063 * type LPFC_PG_TYPE_DIF
James Smarte2a0a9d2008-12-04 22:40:02 -05002064 *
2065 * This is usually used when DIFs are in their own buffers,
2066 * separate from the data. The HBA can then by instructed
2067 * to place the DIFs in the outgoing stream. For read operations,
2068 * The HBA could extract the DIFs and place it in DIF buffers.
2069 *
2070 * The buffer list for this type consists of one or more of the
2071 * protection groups described below:
2072 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002073 * start of first prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05002074 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002075 * | PDE_6 |
2076 * +-------------------------+
2077 * | PDE_7 (Prot BDE) |
James Smarte2a0a9d2008-12-04 22:40:02 -05002078 * +-------------------------+
2079 * | Data BDE |
2080 * +-------------------------+
2081 * |more Data BDE's ... (opt)|
2082 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002083 * start of new prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05002084 * +-------------------------+
2085 * | ... |
2086 * +-------------------------+
2087 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002088 * Note: It is assumed that both data and protection s/g buffers have been
2089 * mapped for DMA
James Smartacd68592012-01-18 16:25:09 -05002090 *
2091 * Returns the number of BDEs added to the BPL.
2092 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002093static int
2094lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2095 struct ulp_bde64 *bpl, int datacnt, int protcnt)
2096{
2097 struct scatterlist *sgde = NULL; /* s/g data entry */
2098 struct scatterlist *sgpe = NULL; /* s/g prot entry */
James Smart6c8eea52010-04-06 14:49:53 -04002099 struct lpfc_pde5 *pde5 = NULL;
2100 struct lpfc_pde6 *pde6 = NULL;
James Smart7f860592011-03-11 16:05:52 -05002101 struct lpfc_pde7 *pde7 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05002102 dma_addr_t dataphysaddr, protphysaddr;
2103 unsigned short curr_data = 0, curr_prot = 0;
James Smart7f860592011-03-11 16:05:52 -05002104 unsigned int split_offset;
2105 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
James Smarte2a0a9d2008-12-04 22:40:02 -05002106 unsigned int protgrp_blks, protgrp_bytes;
2107 unsigned int remainder, subtotal;
James Smart6c8eea52010-04-06 14:49:53 -04002108 int status;
James Smarte2a0a9d2008-12-04 22:40:02 -05002109 int datadir = sc->sc_data_direction;
2110 unsigned char pgdone = 0, alldone = 0;
2111 unsigned blksize;
James Smart0829a192012-05-09 21:18:12 -04002112#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002113 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002114#endif
James Smartacd68592012-01-18 16:25:09 -05002115 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05002116 uint32_t reftag;
James Smart6c8eea52010-04-06 14:49:53 -04002117 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05002118 int num_bde = 0;
2119
2120 sgpe = scsi_prot_sglist(sc);
2121 sgde = scsi_sglist(sc);
2122
2123 if (!sgpe || !sgde) {
2124 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2125 "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
2126 sgpe, sgde);
2127 return 0;
2128 }
2129
James Smart6c8eea52010-04-06 14:49:53 -04002130 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2131 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05002132 goto out;
2133
James Smart6c8eea52010-04-06 14:49:53 -04002134 /* extract some info from the scsi command */
James Smarte2a0a9d2008-12-04 22:40:02 -05002135 blksize = lpfc_cmd_blksize(sc);
James Smartacd68592012-01-18 16:25:09 -05002136 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
James Smarte2a0a9d2008-12-04 22:40:02 -05002137
James Smartf9bb2da2011-10-10 21:34:11 -04002138#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002139 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002140 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002141 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002142 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002143 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002144 checking = 0;
2145 }
James Smartf9bb2da2011-10-10 21:34:11 -04002146#endif
2147
James Smarte2a0a9d2008-12-04 22:40:02 -05002148 split_offset = 0;
2149 do {
James Smart6c8eea52010-04-06 14:49:53 -04002150 /* setup PDE5 with what we have */
2151 pde5 = (struct lpfc_pde5 *) bpl;
2152 memset(pde5, 0, sizeof(struct lpfc_pde5));
2153 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05002154
James Smartbc739052010-08-04 16:11:18 -04002155 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04002156 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04002157 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04002158
James Smart6c8eea52010-04-06 14:49:53 -04002159 /* advance bpl and increment bde count */
2160 num_bde++;
2161 bpl++;
2162 pde6 = (struct lpfc_pde6 *) bpl;
James Smarte2a0a9d2008-12-04 22:40:02 -05002163
James Smart6c8eea52010-04-06 14:49:53 -04002164 /* setup PDE6 with the rest of the info */
2165 memset(pde6, 0, sizeof(struct lpfc_pde6));
2166 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
2167 bf_set(pde6_optx, pde6, txop);
2168 bf_set(pde6_oprx, pde6, rxop);
James Smartacd68592012-01-18 16:25:09 -05002169 bf_set(pde6_ce, pde6, checking);
2170 bf_set(pde6_re, pde6, checking);
James Smart6c8eea52010-04-06 14:49:53 -04002171 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04002172 bf_set(pde6_ae, pde6, 0);
2173 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04002174
James Smartbc739052010-08-04 16:11:18 -04002175 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04002176 pde6->word0 = cpu_to_le32(pde6->word0);
2177 pde6->word1 = cpu_to_le32(pde6->word1);
2178 pde6->word2 = cpu_to_le32(pde6->word2);
2179
James Smart6c8eea52010-04-06 14:49:53 -04002180 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05002181 num_bde++;
2182 bpl++;
2183
2184 /* setup the first BDE that points to protection buffer */
James Smart7f860592011-03-11 16:05:52 -05002185 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2186 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
James Smarte2a0a9d2008-12-04 22:40:02 -05002187
James Smarte2a0a9d2008-12-04 22:40:02 -05002188 /* must be integer multiple of the DIF block length */
2189 BUG_ON(protgroup_len % 8);
2190
James Smart7f860592011-03-11 16:05:52 -05002191 pde7 = (struct lpfc_pde7 *) bpl;
2192 memset(pde7, 0, sizeof(struct lpfc_pde7));
2193 bf_set(pde7_type, pde7, LPFC_PDE7_DESCRIPTOR);
2194
James Smart7c56b9f2011-07-22 18:36:25 -04002195 pde7->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
2196 pde7->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
James Smart7f860592011-03-11 16:05:52 -05002197
James Smarte2a0a9d2008-12-04 22:40:02 -05002198 protgrp_blks = protgroup_len / 8;
2199 protgrp_bytes = protgrp_blks * blksize;
2200
James Smart7f860592011-03-11 16:05:52 -05002201 /* check if this pde is crossing the 4K boundary; if so split */
2202 if ((pde7->addrLow & 0xfff) + protgroup_len > 0x1000) {
2203 protgroup_remainder = 0x1000 - (pde7->addrLow & 0xfff);
2204 protgroup_offset += protgroup_remainder;
2205 protgrp_blks = protgroup_remainder / 8;
James Smart7c56b9f2011-07-22 18:36:25 -04002206 protgrp_bytes = protgrp_blks * blksize;
James Smart7f860592011-03-11 16:05:52 -05002207 } else {
2208 protgroup_offset = 0;
2209 curr_prot++;
2210 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002211
James Smarte2a0a9d2008-12-04 22:40:02 -05002212 num_bde++;
2213
2214 /* setup BDE's for data blocks associated with DIF data */
2215 pgdone = 0;
2216 subtotal = 0; /* total bytes processed for current prot grp */
2217 while (!pgdone) {
2218 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -04002219 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2220 "9065 BLKGRD:%s Invalid data segment\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002221 __func__);
2222 return 0;
2223 }
2224 bpl++;
2225 dataphysaddr = sg_dma_address(sgde) + split_offset;
2226 bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
2227 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
2228
2229 remainder = sg_dma_len(sgde) - split_offset;
2230
2231 if ((subtotal + remainder) <= protgrp_bytes) {
2232 /* we can use this whole buffer */
2233 bpl->tus.f.bdeSize = remainder;
2234 split_offset = 0;
2235
2236 if ((subtotal + remainder) == protgrp_bytes)
2237 pgdone = 1;
2238 } else {
2239 /* must split this buffer with next prot grp */
2240 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
2241 split_offset += bpl->tus.f.bdeSize;
2242 }
2243
2244 subtotal += bpl->tus.f.bdeSize;
2245
2246 if (datadir == DMA_TO_DEVICE)
2247 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2248 else
2249 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2250 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2251
2252 num_bde++;
2253 curr_data++;
2254
2255 if (split_offset)
2256 break;
2257
2258 /* Move to the next s/g segment if possible */
2259 sgde = sg_next(sgde);
James Smart6c8eea52010-04-06 14:49:53 -04002260
James Smarte2a0a9d2008-12-04 22:40:02 -05002261 }
2262
James Smart7f860592011-03-11 16:05:52 -05002263 if (protgroup_offset) {
2264 /* update the reference tag */
2265 reftag += protgrp_blks;
2266 bpl++;
2267 continue;
2268 }
2269
James Smarte2a0a9d2008-12-04 22:40:02 -05002270 /* are we done ? */
2271 if (curr_prot == protcnt) {
2272 alldone = 1;
2273 } else if (curr_prot < protcnt) {
2274 /* advance to next prot buffer */
2275 sgpe = sg_next(sgpe);
2276 bpl++;
2277
2278 /* update the reference tag */
2279 reftag += protgrp_blks;
2280 } else {
2281 /* if we're here, we have a bug */
James Smart6a9c52c2009-10-02 15:16:51 -04002282 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2283 "9054 BLKGRD: bug in %s\n", __func__);
James Smarte2a0a9d2008-12-04 22:40:02 -05002284 }
2285
2286 } while (!alldone);
James Smarte2a0a9d2008-12-04 22:40:02 -05002287out:
2288
James Smarte2a0a9d2008-12-04 22:40:02 -05002289 return num_bde;
2290}
James Smart7f860592011-03-11 16:05:52 -05002291
James Smartacd68592012-01-18 16:25:09 -05002292/**
2293 * lpfc_bg_setup_sgl - Setup BlockGuard SGL with no protection data
2294 * @phba: The Hba for which this call is being executed.
2295 * @sc: pointer to scsi command we're working on
2296 * @sgl: pointer to buffer list for protection groups
2297 * @datacnt: number of segments of data that have been dma mapped
2298 *
2299 * This function sets up SGL buffer list for protection groups of
2300 * type LPFC_PG_TYPE_NO_DIF
2301 *
2302 * This is usually used when the HBA is instructed to generate
2303 * DIFs and insert them into data stream (or strip DIF from
2304 * incoming data stream)
2305 *
2306 * The buffer list consists of just one protection group described
2307 * below:
2308 * +-------------------------+
2309 * start of prot group --> | DI_SEED |
2310 * +-------------------------+
2311 * | Data SGE |
2312 * +-------------------------+
2313 * |more Data SGE's ... (opt)|
2314 * +-------------------------+
2315 *
2316 *
2317 * Note: Data s/g buffers have been dma mapped
2318 *
2319 * Returns the number of SGEs added to the SGL.
2320 **/
2321static int
2322lpfc_bg_setup_sgl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2323 struct sli4_sge *sgl, int datasegcnt)
2324{
2325 struct scatterlist *sgde = NULL; /* s/g data entry */
2326 struct sli4_sge_diseed *diseed = NULL;
2327 dma_addr_t physaddr;
2328 int i = 0, num_sge = 0, status;
2329 int datadir = sc->sc_data_direction;
2330 uint32_t reftag;
2331 unsigned blksize;
2332 uint8_t txop, rxop;
James Smart0829a192012-05-09 21:18:12 -04002333#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002334 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002335#endif
James Smartacd68592012-01-18 16:25:09 -05002336 uint32_t checking = 1;
2337 uint32_t dma_len;
2338 uint32_t dma_offset = 0;
2339
2340 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2341 if (status)
2342 goto out;
2343
2344 /* extract some info from the scsi command for pde*/
2345 blksize = lpfc_cmd_blksize(sc);
2346 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2347
2348#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002349 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002350 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002351 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002352 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002353 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002354 checking = 0;
2355 }
2356#endif
2357
2358 /* setup DISEED with what we have */
2359 diseed = (struct sli4_sge_diseed *) sgl;
2360 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2361 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2362
2363 /* Endianness conversion if necessary */
2364 diseed->ref_tag = cpu_to_le32(reftag);
2365 diseed->ref_tag_tran = diseed->ref_tag;
2366
2367 /* setup DISEED with the rest of the info */
2368 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2369 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
2370 if (datadir == DMA_FROM_DEVICE) {
2371 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2372 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2373 }
2374 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2375 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2376
2377 /* Endianness conversion if necessary for DISEED */
2378 diseed->word2 = cpu_to_le32(diseed->word2);
2379 diseed->word3 = cpu_to_le32(diseed->word3);
2380
2381 /* advance bpl and increment sge count */
2382 num_sge++;
2383 sgl++;
2384
2385 /* assumption: caller has already run dma_map_sg on command data */
2386 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
2387 physaddr = sg_dma_address(sgde);
2388 dma_len = sg_dma_len(sgde);
2389 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
2390 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
2391 if ((i + 1) == datasegcnt)
2392 bf_set(lpfc_sli4_sge_last, sgl, 1);
2393 else
2394 bf_set(lpfc_sli4_sge_last, sgl, 0);
2395 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2396 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2397
2398 sgl->sge_len = cpu_to_le32(dma_len);
2399 dma_offset += dma_len;
2400
2401 sgl++;
2402 num_sge++;
2403 }
2404
2405out:
2406 return num_sge;
2407}
2408
2409/**
2410 * lpfc_bg_setup_sgl_prot - Setup BlockGuard SGL with protection data
2411 * @phba: The Hba for which this call is being executed.
2412 * @sc: pointer to scsi command we're working on
2413 * @sgl: pointer to buffer list for protection groups
2414 * @datacnt: number of segments of data that have been dma mapped
2415 * @protcnt: number of segment of protection data that have been dma mapped
2416 *
2417 * This function sets up SGL buffer list for protection groups of
2418 * type LPFC_PG_TYPE_DIF
2419 *
2420 * This is usually used when DIFs are in their own buffers,
2421 * separate from the data. The HBA can then by instructed
2422 * to place the DIFs in the outgoing stream. For read operations,
2423 * The HBA could extract the DIFs and place it in DIF buffers.
2424 *
2425 * The buffer list for this type consists of one or more of the
2426 * protection groups described below:
2427 * +-------------------------+
2428 * start of first prot group --> | DISEED |
2429 * +-------------------------+
2430 * | DIF (Prot SGE) |
2431 * +-------------------------+
2432 * | Data SGE |
2433 * +-------------------------+
2434 * |more Data SGE's ... (opt)|
2435 * +-------------------------+
2436 * start of new prot group --> | DISEED |
2437 * +-------------------------+
2438 * | ... |
2439 * +-------------------------+
2440 *
2441 * Note: It is assumed that both data and protection s/g buffers have been
2442 * mapped for DMA
2443 *
2444 * Returns the number of SGEs added to the SGL.
2445 **/
2446static int
2447lpfc_bg_setup_sgl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2448 struct sli4_sge *sgl, int datacnt, int protcnt)
2449{
2450 struct scatterlist *sgde = NULL; /* s/g data entry */
2451 struct scatterlist *sgpe = NULL; /* s/g prot entry */
2452 struct sli4_sge_diseed *diseed = NULL;
2453 dma_addr_t dataphysaddr, protphysaddr;
2454 unsigned short curr_data = 0, curr_prot = 0;
2455 unsigned int split_offset;
2456 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
2457 unsigned int protgrp_blks, protgrp_bytes;
2458 unsigned int remainder, subtotal;
2459 int status;
2460 unsigned char pgdone = 0, alldone = 0;
2461 unsigned blksize;
2462 uint32_t reftag;
2463 uint8_t txop, rxop;
2464 uint32_t dma_len;
James Smart0829a192012-05-09 21:18:12 -04002465#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002466 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002467#endif
James Smartacd68592012-01-18 16:25:09 -05002468 uint32_t checking = 1;
2469 uint32_t dma_offset = 0;
2470 int num_sge = 0;
2471
2472 sgpe = scsi_prot_sglist(sc);
2473 sgde = scsi_sglist(sc);
2474
2475 if (!sgpe || !sgde) {
2476 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2477 "9082 Invalid s/g entry: data=0x%p prot=0x%p\n",
2478 sgpe, sgde);
2479 return 0;
2480 }
2481
2482 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2483 if (status)
2484 goto out;
2485
2486 /* extract some info from the scsi command */
2487 blksize = lpfc_cmd_blksize(sc);
2488 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2489
2490#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002491 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002492 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002493 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002494 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002495 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002496 checking = 0;
2497 }
2498#endif
2499
2500 split_offset = 0;
2501 do {
2502 /* setup DISEED with what we have */
2503 diseed = (struct sli4_sge_diseed *) sgl;
2504 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2505 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2506
2507 /* Endianness conversion if necessary */
2508 diseed->ref_tag = cpu_to_le32(reftag);
2509 diseed->ref_tag_tran = diseed->ref_tag;
2510
2511 /* setup DISEED with the rest of the info */
2512 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2513 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
2514 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2515 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2516 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2517 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2518
2519 /* Endianness conversion if necessary for DISEED */
2520 diseed->word2 = cpu_to_le32(diseed->word2);
2521 diseed->word3 = cpu_to_le32(diseed->word3);
2522
2523 /* advance sgl and increment bde count */
2524 num_sge++;
2525 sgl++;
2526
2527 /* setup the first BDE that points to protection buffer */
2528 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2529 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
2530
2531 /* must be integer multiple of the DIF block length */
2532 BUG_ON(protgroup_len % 8);
2533
2534 /* Now setup DIF SGE */
2535 sgl->word2 = 0;
2536 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DIF);
2537 sgl->addr_hi = le32_to_cpu(putPaddrHigh(protphysaddr));
2538 sgl->addr_lo = le32_to_cpu(putPaddrLow(protphysaddr));
2539 sgl->word2 = cpu_to_le32(sgl->word2);
2540
2541 protgrp_blks = protgroup_len / 8;
2542 protgrp_bytes = protgrp_blks * blksize;
2543
2544 /* check if DIF SGE is crossing the 4K boundary; if so split */
2545 if ((sgl->addr_lo & 0xfff) + protgroup_len > 0x1000) {
2546 protgroup_remainder = 0x1000 - (sgl->addr_lo & 0xfff);
2547 protgroup_offset += protgroup_remainder;
2548 protgrp_blks = protgroup_remainder / 8;
2549 protgrp_bytes = protgrp_blks * blksize;
2550 } else {
2551 protgroup_offset = 0;
2552 curr_prot++;
2553 }
2554
2555 num_sge++;
2556
2557 /* setup SGE's for data blocks associated with DIF data */
2558 pgdone = 0;
2559 subtotal = 0; /* total bytes processed for current prot grp */
2560 while (!pgdone) {
2561 if (!sgde) {
2562 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2563 "9086 BLKGRD:%s Invalid data segment\n",
2564 __func__);
2565 return 0;
2566 }
2567 sgl++;
2568 dataphysaddr = sg_dma_address(sgde) + split_offset;
2569
2570 remainder = sg_dma_len(sgde) - split_offset;
2571
2572 if ((subtotal + remainder) <= protgrp_bytes) {
2573 /* we can use this whole buffer */
2574 dma_len = remainder;
2575 split_offset = 0;
2576
2577 if ((subtotal + remainder) == protgrp_bytes)
2578 pgdone = 1;
2579 } else {
2580 /* must split this buffer with next prot grp */
2581 dma_len = protgrp_bytes - subtotal;
2582 split_offset += dma_len;
2583 }
2584
2585 subtotal += dma_len;
2586
2587 sgl->addr_lo = cpu_to_le32(putPaddrLow(dataphysaddr));
2588 sgl->addr_hi = cpu_to_le32(putPaddrHigh(dataphysaddr));
2589 bf_set(lpfc_sli4_sge_last, sgl, 0);
2590 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2591 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2592
2593 sgl->sge_len = cpu_to_le32(dma_len);
2594 dma_offset += dma_len;
2595
2596 num_sge++;
2597 curr_data++;
2598
2599 if (split_offset)
2600 break;
2601
2602 /* Move to the next s/g segment if possible */
2603 sgde = sg_next(sgde);
2604 }
2605
2606 if (protgroup_offset) {
2607 /* update the reference tag */
2608 reftag += protgrp_blks;
2609 sgl++;
2610 continue;
2611 }
2612
2613 /* are we done ? */
2614 if (curr_prot == protcnt) {
2615 bf_set(lpfc_sli4_sge_last, sgl, 1);
2616 alldone = 1;
2617 } else if (curr_prot < protcnt) {
2618 /* advance to next prot buffer */
2619 sgpe = sg_next(sgpe);
2620 sgl++;
2621
2622 /* update the reference tag */
2623 reftag += protgrp_blks;
2624 } else {
2625 /* if we're here, we have a bug */
2626 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2627 "9085 BLKGRD: bug in %s\n", __func__);
2628 }
2629
2630 } while (!alldone);
2631
2632out:
2633
2634 return num_sge;
2635}
2636
2637/**
2638 * lpfc_prot_group_type - Get prtotection group type of SCSI command
2639 * @phba: The Hba for which this call is being executed.
2640 * @sc: pointer to scsi command we're working on
2641 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002642 * Given a SCSI command that supports DIF, determine composition of protection
2643 * groups involved in setting up buffer lists
2644 *
James Smartacd68592012-01-18 16:25:09 -05002645 * Returns: Protection group type (with or without DIF)
2646 *
2647 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002648static int
2649lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
2650{
2651 int ret = LPFC_PG_TYPE_INVALID;
2652 unsigned char op = scsi_get_prot_op(sc);
2653
2654 switch (op) {
2655 case SCSI_PROT_READ_STRIP:
2656 case SCSI_PROT_WRITE_INSERT:
2657 ret = LPFC_PG_TYPE_NO_DIF;
2658 break;
2659 case SCSI_PROT_READ_INSERT:
2660 case SCSI_PROT_WRITE_STRIP:
2661 case SCSI_PROT_READ_PASS:
2662 case SCSI_PROT_WRITE_PASS:
James Smarte2a0a9d2008-12-04 22:40:02 -05002663 ret = LPFC_PG_TYPE_DIF_BUF;
2664 break;
2665 default:
2666 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2667 "9021 Unsupported protection op:%d\n", op);
2668 break;
2669 }
2670
2671 return ret;
2672}
2673
James Smartacd68592012-01-18 16:25:09 -05002674/**
2675 * lpfc_bg_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
2676 * @phba: The Hba for which this call is being executed.
2677 * @lpfc_cmd: The scsi buffer which is going to be prep'ed.
2678 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002679 * This is the protection/DIF aware version of
2680 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
2681 * two functions eventually, but for now, it's here
James Smartacd68592012-01-18 16:25:09 -05002682 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002683static int
James Smartacd68592012-01-18 16:25:09 -05002684lpfc_bg_scsi_prep_dma_buf_s3(struct lpfc_hba *phba,
James Smarte2a0a9d2008-12-04 22:40:02 -05002685 struct lpfc_scsi_buf *lpfc_cmd)
2686{
2687 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2688 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2689 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
2690 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2691 uint32_t num_bde = 0;
2692 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
2693 int prot_group_type = 0;
2694 int diflen, fcpdl;
2695 unsigned blksize;
2696
2697 /*
2698 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
2699 * fcp_rsp regions to the first data bde entry
2700 */
2701 bpl += 2;
2702 if (scsi_sg_count(scsi_cmnd)) {
2703 /*
2704 * The driver stores the segment count returned from pci_map_sg
2705 * because this a count of dma-mappings used to map the use_sg
2706 * pages. They are not guaranteed to be the same for those
2707 * architectures that implement an IOMMU.
2708 */
2709 datasegcnt = dma_map_sg(&phba->pcidev->dev,
2710 scsi_sglist(scsi_cmnd),
2711 scsi_sg_count(scsi_cmnd), datadir);
2712 if (unlikely(!datasegcnt))
2713 return 1;
2714
2715 lpfc_cmd->seg_cnt = datasegcnt;
2716 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04002717 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2718 "9067 BLKGRD: %s: Too many sg segments"
2719 " from dma_map_sg. Config %d, seg_cnt"
2720 " %d\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002721 __func__, phba->cfg_sg_seg_cnt,
2722 lpfc_cmd->seg_cnt);
2723 scsi_dma_unmap(scsi_cmnd);
2724 return 1;
2725 }
2726
2727 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
2728
2729 switch (prot_group_type) {
2730 case LPFC_PG_TYPE_NO_DIF:
2731 num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
2732 datasegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002733 /* we should have 2 or more entries in buffer list */
James Smarte2a0a9d2008-12-04 22:40:02 -05002734 if (num_bde < 2)
2735 goto err;
2736 break;
2737 case LPFC_PG_TYPE_DIF_BUF:{
2738 /*
2739 * This type indicates that protection buffers are
2740 * passed to the driver, so that needs to be prepared
2741 * for DMA
2742 */
2743 protsegcnt = dma_map_sg(&phba->pcidev->dev,
2744 scsi_prot_sglist(scsi_cmnd),
2745 scsi_prot_sg_count(scsi_cmnd), datadir);
2746 if (unlikely(!protsegcnt)) {
2747 scsi_dma_unmap(scsi_cmnd);
2748 return 1;
2749 }
2750
2751 lpfc_cmd->prot_seg_cnt = protsegcnt;
2752 if (lpfc_cmd->prot_seg_cnt
2753 > phba->cfg_prot_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04002754 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2755 "9068 BLKGRD: %s: Too many prot sg "
2756 "segments from dma_map_sg. Config %d,"
James Smarte2a0a9d2008-12-04 22:40:02 -05002757 "prot_seg_cnt %d\n", __func__,
2758 phba->cfg_prot_sg_seg_cnt,
2759 lpfc_cmd->prot_seg_cnt);
2760 dma_unmap_sg(&phba->pcidev->dev,
2761 scsi_prot_sglist(scsi_cmnd),
2762 scsi_prot_sg_count(scsi_cmnd),
2763 datadir);
2764 scsi_dma_unmap(scsi_cmnd);
2765 return 1;
2766 }
2767
2768 num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
2769 datasegcnt, protsegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002770 /* we should have 3 or more entries in buffer list */
James Smarte2a0a9d2008-12-04 22:40:02 -05002771 if (num_bde < 3)
2772 goto err;
2773 break;
2774 }
2775 case LPFC_PG_TYPE_INVALID:
2776 default:
2777 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2778 "9022 Unexpected protection group %i\n",
2779 prot_group_type);
2780 return 1;
2781 }
2782 }
2783
2784 /*
2785 * Finish initializing those IOCB fields that are dependent on the
2786 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
2787 * reinitialized since all iocb memory resources are used many times
2788 * for transmit, receive, and continuation bpl's.
2789 */
2790 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
2791 iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
2792 iocb_cmd->ulpBdeCount = 1;
2793 iocb_cmd->ulpLe = 1;
2794
2795 fcpdl = scsi_bufflen(scsi_cmnd);
2796
2797 if (scsi_get_prot_type(scsi_cmnd) == SCSI_PROT_DIF_TYPE1) {
2798 /*
2799 * We are in DIF Type 1 mode
2800 * Every data block has a 8 byte DIF (trailer)
2801 * attached to it. Must ajust FCP data length
2802 */
2803 blksize = lpfc_cmd_blksize(scsi_cmnd);
2804 diflen = (fcpdl / blksize) * 8;
2805 fcpdl += diflen;
2806 }
2807 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
2808
2809 /*
2810 * Due to difference in data length between DIF/non-DIF paths,
2811 * we need to set word 4 of IOCB here
2812 */
2813 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
2814
2815 return 0;
2816err:
2817 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2818 "9023 Could not setup all needed BDE's"
2819 "prot_group_type=%d, num_bde=%d\n",
2820 prot_group_type, num_bde);
2821 return 1;
2822}
2823
2824/*
James Smart737d4242013-04-17 20:14:49 -04002825 * This function calcuates the T10 DIF guard tag
2826 * on the specified data using a CRC algorithmn
2827 * using crc_t10dif.
2828 */
2829uint16_t
2830lpfc_bg_crc(uint8_t *data, int count)
2831{
2832 uint16_t crc = 0;
2833 uint16_t x;
2834
2835 crc = crc_t10dif(data, count);
2836 x = cpu_to_be16(crc);
2837 return x;
2838}
2839
2840/*
2841 * This function calcuates the T10 DIF guard tag
2842 * on the specified data using a CSUM algorithmn
2843 * using ip_compute_csum.
2844 */
2845uint16_t
2846lpfc_bg_csum(uint8_t *data, int count)
2847{
2848 uint16_t ret;
2849
2850 ret = ip_compute_csum(data, count);
2851 return ret;
2852}
2853
2854/*
2855 * This function examines the protection data to try to determine
2856 * what type of T10-DIF error occurred.
2857 */
2858void
2859lpfc_calc_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
2860{
2861 struct scatterlist *sgpe; /* s/g prot entry */
2862 struct scatterlist *sgde; /* s/g data entry */
2863 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
2864 struct scsi_dif_tuple *src = NULL;
2865 uint8_t *data_src = NULL;
2866 uint16_t guard_tag, guard_type;
2867 uint16_t start_app_tag, app_tag;
2868 uint32_t start_ref_tag, ref_tag;
2869 int prot, protsegcnt;
2870 int err_type, len, data_len;
2871 int chk_ref, chk_app, chk_guard;
2872 uint16_t sum;
2873 unsigned blksize;
2874
2875 err_type = BGS_GUARD_ERR_MASK;
2876 sum = 0;
2877 guard_tag = 0;
2878
2879 /* First check to see if there is protection data to examine */
2880 prot = scsi_get_prot_op(cmd);
2881 if ((prot == SCSI_PROT_READ_STRIP) ||
2882 (prot == SCSI_PROT_WRITE_INSERT) ||
2883 (prot == SCSI_PROT_NORMAL))
2884 goto out;
2885
2886 /* Currently the driver just supports ref_tag and guard_tag checking */
2887 chk_ref = 1;
2888 chk_app = 0;
2889 chk_guard = 0;
2890
2891 /* Setup a ptr to the protection data provided by the SCSI host */
2892 sgpe = scsi_prot_sglist(cmd);
2893 protsegcnt = lpfc_cmd->prot_seg_cnt;
2894
2895 if (sgpe && protsegcnt) {
2896
2897 /*
2898 * We will only try to verify guard tag if the segment
2899 * data length is a multiple of the blksize.
2900 */
2901 sgde = scsi_sglist(cmd);
2902 blksize = lpfc_cmd_blksize(cmd);
2903 data_src = (uint8_t *)sg_virt(sgde);
2904 data_len = sgde->length;
2905 if ((data_len & (blksize - 1)) == 0)
2906 chk_guard = 1;
2907 guard_type = scsi_host_get_guard(cmd->device->host);
2908
2909 start_ref_tag = scsi_get_lba(cmd);
2910 start_app_tag = src->app_tag;
2911 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
2912 len = sgpe->length;
2913 while (src && protsegcnt) {
2914 while (len) {
2915
2916 /*
2917 * First check to see if a protection data
2918 * check is valid
2919 */
2920 if ((src->ref_tag == 0xffffffff) ||
2921 (src->app_tag == 0xffff)) {
2922 start_ref_tag++;
2923 goto skipit;
2924 }
2925
2926 /* App Tag checking */
2927 app_tag = src->app_tag;
2928 if (chk_app && (app_tag != start_app_tag)) {
2929 err_type = BGS_APPTAG_ERR_MASK;
2930 goto out;
2931 }
2932
2933 /* Reference Tag checking */
2934 ref_tag = be32_to_cpu(src->ref_tag);
2935 if (chk_ref && (ref_tag != start_ref_tag)) {
2936 err_type = BGS_REFTAG_ERR_MASK;
2937 goto out;
2938 }
2939 start_ref_tag++;
2940
2941 /* Guard Tag checking */
2942 if (chk_guard) {
2943 guard_tag = src->guard_tag;
2944 if (guard_type == SHOST_DIX_GUARD_IP)
2945 sum = lpfc_bg_csum(data_src,
2946 blksize);
2947 else
2948 sum = lpfc_bg_crc(data_src,
2949 blksize);
2950 if ((guard_tag != sum)) {
2951 err_type = BGS_GUARD_ERR_MASK;
2952 goto out;
2953 }
2954 }
2955skipit:
2956 len -= sizeof(struct scsi_dif_tuple);
2957 if (len < 0)
2958 len = 0;
2959 src++;
2960
2961 data_src += blksize;
2962 data_len -= blksize;
2963
2964 /*
2965 * Are we at the end of the Data segment?
2966 * The data segment is only used for Guard
2967 * tag checking.
2968 */
2969 if (chk_guard && (data_len == 0)) {
2970 chk_guard = 0;
2971 sgde = sg_next(sgde);
2972 if (!sgde)
2973 goto out;
2974
2975 data_src = (uint8_t *)sg_virt(sgde);
2976 data_len = sgde->length;
2977 if ((data_len & (blksize - 1)) == 0)
2978 chk_guard = 1;
2979 }
2980 }
2981
2982 /* Goto the next Protection data segment */
2983 sgpe = sg_next(sgpe);
2984 if (sgpe) {
2985 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
2986 len = sgpe->length;
2987 } else {
2988 src = NULL;
2989 }
2990 protsegcnt--;
2991 }
2992 }
2993out:
2994 if (err_type == BGS_GUARD_ERR_MASK) {
2995 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2996 0x10, 0x1);
2997 cmd->result = DRIVER_SENSE << 24
2998 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
2999 phba->bg_guard_err_cnt++;
3000 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3001 "9069 BLKGRD: LBA %lx grd_tag error %x != %x\n",
3002 (unsigned long)scsi_get_lba(cmd),
3003 sum, guard_tag);
3004
3005 } else if (err_type == BGS_REFTAG_ERR_MASK) {
3006 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3007 0x10, 0x3);
3008 cmd->result = DRIVER_SENSE << 24
3009 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3010
3011 phba->bg_reftag_err_cnt++;
3012 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3013 "9066 BLKGRD: LBA %lx ref_tag error %x != %x\n",
3014 (unsigned long)scsi_get_lba(cmd),
3015 ref_tag, start_ref_tag);
3016
3017 } else if (err_type == BGS_APPTAG_ERR_MASK) {
3018 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3019 0x10, 0x2);
3020 cmd->result = DRIVER_SENSE << 24
3021 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3022
3023 phba->bg_apptag_err_cnt++;
3024 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3025 "9041 BLKGRD: LBA %lx app_tag error %x != %x\n",
3026 (unsigned long)scsi_get_lba(cmd),
3027 app_tag, start_app_tag);
3028 }
3029}
3030
3031
3032/*
James Smarte2a0a9d2008-12-04 22:40:02 -05003033 * This function checks for BlockGuard errors detected by
3034 * the HBA. In case of errors, the ASC/ASCQ fields in the
3035 * sense buffer will be set accordingly, paired with
3036 * ILLEGAL_REQUEST to signal to the kernel that the HBA
3037 * detected corruption.
3038 *
3039 * Returns:
3040 * 0 - No error found
3041 * 1 - BlockGuard error found
3042 * -1 - Internal error (bad profile, ...etc)
3043 */
3044static int
3045lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
3046 struct lpfc_iocbq *pIocbOut)
3047{
3048 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
3049 struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
3050 int ret = 0;
3051 uint32_t bghm = bgf->bghm;
3052 uint32_t bgstat = bgf->bgstat;
3053 uint64_t failing_sector = 0;
3054
James Smarte2a0a9d2008-12-04 22:40:02 -05003055 spin_lock(&_dump_buf_lock);
3056 if (!_dump_buf_done) {
James Smart6a9c52c2009-10-02 15:16:51 -04003057 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9070 BLKGRD: Saving"
3058 " Data for %u blocks to debugfs\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05003059 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
James Smart6a9c52c2009-10-02 15:16:51 -04003060 lpfc_debug_save_data(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003061
3062 /* If we have a prot sgl, save the DIF buffer */
3063 if (lpfc_prot_group_type(phba, cmd) ==
3064 LPFC_PG_TYPE_DIF_BUF) {
James Smart6a9c52c2009-10-02 15:16:51 -04003065 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9071 BLKGRD: "
3066 "Saving DIF for %u blocks to debugfs\n",
3067 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
3068 lpfc_debug_save_dif(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003069 }
3070
3071 _dump_buf_done = 1;
3072 }
3073 spin_unlock(&_dump_buf_lock);
3074
3075 if (lpfc_bgs_get_invalid_prof(bgstat)) {
3076 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart737d4242013-04-17 20:14:49 -04003077 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3078 "9072 BLKGRD: Invalid BG Profile in cmd"
3079 " 0x%x lba 0x%llx blk cnt 0x%x "
3080 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3081 (unsigned long long)scsi_get_lba(cmd),
3082 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003083 ret = (-1);
3084 goto out;
3085 }
3086
3087 if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
3088 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart737d4242013-04-17 20:14:49 -04003089 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3090 "9073 BLKGRD: Invalid BG PDIF Block in cmd"
3091 " 0x%x lba 0x%llx blk cnt 0x%x "
3092 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3093 (unsigned long long)scsi_get_lba(cmd),
3094 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003095 ret = (-1);
3096 goto out;
3097 }
3098
3099 if (lpfc_bgs_get_guard_err(bgstat)) {
3100 ret = 1;
3101
3102 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3103 0x10, 0x1);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05003104 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05003105 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3106 phba->bg_guard_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04003107 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3108 "9055 BLKGRD: Guard Tag error in cmd"
3109 " 0x%x lba 0x%llx blk cnt 0x%x "
3110 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3111 (unsigned long long)scsi_get_lba(cmd),
3112 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003113 }
3114
3115 if (lpfc_bgs_get_reftag_err(bgstat)) {
3116 ret = 1;
3117
3118 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3119 0x10, 0x3);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05003120 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05003121 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3122
3123 phba->bg_reftag_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04003124 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3125 "9056 BLKGRD: Ref Tag error in cmd"
3126 " 0x%x lba 0x%llx blk cnt 0x%x "
3127 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3128 (unsigned long long)scsi_get_lba(cmd),
3129 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003130 }
3131
3132 if (lpfc_bgs_get_apptag_err(bgstat)) {
3133 ret = 1;
3134
3135 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3136 0x10, 0x2);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05003137 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05003138 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3139
3140 phba->bg_apptag_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04003141 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3142 "9061 BLKGRD: App Tag error in cmd"
3143 " 0x%x lba 0x%llx blk cnt 0x%x "
3144 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3145 (unsigned long long)scsi_get_lba(cmd),
3146 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003147 }
3148
3149 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
3150 /*
3151 * setup sense data descriptor 0 per SPC-4 as an information
James Smart7c56b9f2011-07-22 18:36:25 -04003152 * field, and put the failing LBA in it.
3153 * This code assumes there was also a guard/app/ref tag error
3154 * indication.
James Smarte2a0a9d2008-12-04 22:40:02 -05003155 */
James Smart7c56b9f2011-07-22 18:36:25 -04003156 cmd->sense_buffer[7] = 0xc; /* Additional sense length */
3157 cmd->sense_buffer[8] = 0; /* Information descriptor type */
3158 cmd->sense_buffer[9] = 0xa; /* Additional descriptor length */
3159 cmd->sense_buffer[10] = 0x80; /* Validity bit */
James Smartacd68592012-01-18 16:25:09 -05003160
3161 /* bghm is a "on the wire" FC frame based count */
3162 switch (scsi_get_prot_op(cmd)) {
3163 case SCSI_PROT_READ_INSERT:
3164 case SCSI_PROT_WRITE_STRIP:
3165 bghm /= cmd->device->sector_size;
3166 break;
3167 case SCSI_PROT_READ_STRIP:
3168 case SCSI_PROT_WRITE_INSERT:
3169 case SCSI_PROT_READ_PASS:
3170 case SCSI_PROT_WRITE_PASS:
3171 bghm /= (cmd->device->sector_size +
3172 sizeof(struct scsi_dif_tuple));
3173 break;
3174 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003175
3176 failing_sector = scsi_get_lba(cmd);
3177 failing_sector += bghm;
3178
James Smart7c56b9f2011-07-22 18:36:25 -04003179 /* Descriptor Information */
3180 put_unaligned_be64(failing_sector, &cmd->sense_buffer[12]);
James Smarte2a0a9d2008-12-04 22:40:02 -05003181 }
3182
3183 if (!ret) {
3184 /* No error was reported - problem in FW? */
James Smart737d4242013-04-17 20:14:49 -04003185 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3186 "9057 BLKGRD: Unknown error in cmd"
3187 " 0x%x lba 0x%llx blk cnt 0x%x "
3188 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3189 (unsigned long long)scsi_get_lba(cmd),
3190 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003191
James Smart737d4242013-04-17 20:14:49 -04003192 /* Calcuate what type of error it was */
3193 lpfc_calc_bg_err(phba, lpfc_cmd);
3194 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003195out:
3196 return ret;
3197}
3198
James Smartea2151b2008-09-07 11:52:10 -04003199/**
James Smartda0436e2009-05-22 14:51:39 -04003200 * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3201 * @phba: The Hba for which this call is being executed.
3202 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3203 *
3204 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
3205 * field of @lpfc_cmd for device with SLI-4 interface spec.
3206 *
3207 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04003208 * 1 - Error
3209 * 0 - Success
James Smartda0436e2009-05-22 14:51:39 -04003210 **/
3211static int
3212lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3213{
3214 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3215 struct scatterlist *sgel = NULL;
3216 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3217 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
James Smartfedd3b72011-02-16 12:39:24 -05003218 struct sli4_sge *first_data_sgl;
James Smartda0436e2009-05-22 14:51:39 -04003219 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
3220 dma_addr_t physaddr;
3221 uint32_t num_bde = 0;
3222 uint32_t dma_len;
3223 uint32_t dma_offset = 0;
3224 int nseg;
James Smartfedd3b72011-02-16 12:39:24 -05003225 struct ulp_bde64 *bde;
James Smartda0436e2009-05-22 14:51:39 -04003226
3227 /*
3228 * There are three possibilities here - use scatter-gather segment, use
3229 * the single mapping, or neither. Start the lpfc command prep by
3230 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
3231 * data bde entry.
3232 */
3233 if (scsi_sg_count(scsi_cmnd)) {
3234 /*
3235 * The driver stores the segment count returned from pci_map_sg
3236 * because this a count of dma-mappings used to map the use_sg
3237 * pages. They are not guaranteed to be the same for those
3238 * architectures that implement an IOMMU.
3239 */
3240
3241 nseg = scsi_dma_map(scsi_cmnd);
3242 if (unlikely(!nseg))
3243 return 1;
3244 sgl += 1;
3245 /* clear the last flag in the fcp_rsp map entry */
3246 sgl->word2 = le32_to_cpu(sgl->word2);
3247 bf_set(lpfc_sli4_sge_last, sgl, 0);
3248 sgl->word2 = cpu_to_le32(sgl->word2);
3249 sgl += 1;
James Smartfedd3b72011-02-16 12:39:24 -05003250 first_data_sgl = sgl;
James Smartda0436e2009-05-22 14:51:39 -04003251 lpfc_cmd->seg_cnt = nseg;
3252 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04003253 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
3254 " %s: Too many sg segments from "
3255 "dma_map_sg. Config %d, seg_cnt %d\n",
3256 __func__, phba->cfg_sg_seg_cnt,
James Smartda0436e2009-05-22 14:51:39 -04003257 lpfc_cmd->seg_cnt);
3258 scsi_dma_unmap(scsi_cmnd);
3259 return 1;
3260 }
3261
3262 /*
3263 * The driver established a maximum scatter-gather segment count
3264 * during probe that limits the number of sg elements in any
3265 * single scsi command. Just run through the seg_cnt and format
3266 * the sge's.
3267 * When using SLI-3 the driver will try to fit all the BDEs into
3268 * the IOCB. If it can't then the BDEs get added to a BPL as it
3269 * does for SLI-2 mode.
3270 */
3271 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
3272 physaddr = sg_dma_address(sgel);
3273 dma_len = sg_dma_len(sgel);
James Smartda0436e2009-05-22 14:51:39 -04003274 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
3275 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
James Smart05580562011-05-24 11:40:48 -04003276 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -04003277 if ((num_bde + 1) == nseg)
3278 bf_set(lpfc_sli4_sge_last, sgl, 1);
3279 else
3280 bf_set(lpfc_sli4_sge_last, sgl, 0);
3281 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
James Smartf9bb2da2011-10-10 21:34:11 -04003282 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
James Smartda0436e2009-05-22 14:51:39 -04003283 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05003284 sgl->sge_len = cpu_to_le32(dma_len);
James Smartda0436e2009-05-22 14:51:39 -04003285 dma_offset += dma_len;
3286 sgl++;
3287 }
James Smartfedd3b72011-02-16 12:39:24 -05003288 /* setup the performance hint (first data BDE) if enabled */
3289 if (phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) {
3290 bde = (struct ulp_bde64 *)
3291 &(iocb_cmd->unsli3.sli3Words[5]);
3292 bde->addrLow = first_data_sgl->addr_lo;
3293 bde->addrHigh = first_data_sgl->addr_hi;
3294 bde->tus.f.bdeSize =
3295 le32_to_cpu(first_data_sgl->sge_len);
3296 bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
3297 bde->tus.w = cpu_to_le32(bde->tus.w);
3298 }
James Smartda0436e2009-05-22 14:51:39 -04003299 } else {
3300 sgl += 1;
3301 /* clear the last flag in the fcp_rsp map entry */
3302 sgl->word2 = le32_to_cpu(sgl->word2);
3303 bf_set(lpfc_sli4_sge_last, sgl, 1);
3304 sgl->word2 = cpu_to_le32(sgl->word2);
3305 }
3306
3307 /*
3308 * Finish initializing those IOCB fields that are dependent on the
3309 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
3310 * explicitly reinitialized.
3311 * all iocb memory resources are reused.
3312 */
3313 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
3314
3315 /*
3316 * Due to difference in data length between DIF/non-DIF paths,
3317 * we need to set word 4 of IOCB here
3318 */
3319 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
3320 return 0;
3321}
3322
3323/**
James Smartacd68592012-01-18 16:25:09 -05003324 * lpfc_bg_scsi_adjust_dl - Adjust SCSI data length for BlockGuard
3325 * @phba: The Hba for which this call is being executed.
3326 * @lpfc_cmd: The scsi buffer which is going to be adjusted.
3327 *
3328 * Adjust the data length to account for how much data
3329 * is actually on the wire.
3330 *
3331 * returns the adjusted data length
3332 **/
3333static int
3334lpfc_bg_scsi_adjust_dl(struct lpfc_hba *phba,
3335 struct lpfc_scsi_buf *lpfc_cmd)
3336{
3337 struct scsi_cmnd *sc = lpfc_cmd->pCmd;
3338 int diflen, fcpdl;
3339 unsigned blksize;
3340
3341 fcpdl = scsi_bufflen(sc);
3342
3343 /* Check if there is protection data on the wire */
3344 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
3345 /* Read */
3346 if (scsi_get_prot_op(sc) == SCSI_PROT_READ_INSERT)
3347 return fcpdl;
3348
3349 } else {
3350 /* Write */
3351 if (scsi_get_prot_op(sc) == SCSI_PROT_WRITE_STRIP)
3352 return fcpdl;
3353 }
3354
3355 /* If protection data on the wire, adjust the count accordingly */
3356 blksize = lpfc_cmd_blksize(sc);
3357 diflen = (fcpdl / blksize) * 8;
3358 fcpdl += diflen;
3359 return fcpdl;
3360}
3361
3362/**
3363 * lpfc_bg_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3364 * @phba: The Hba for which this call is being executed.
3365 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3366 *
3367 * This is the protection/DIF aware version of
3368 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
3369 * two functions eventually, but for now, it's here
3370 **/
3371static int
3372lpfc_bg_scsi_prep_dma_buf_s4(struct lpfc_hba *phba,
3373 struct lpfc_scsi_buf *lpfc_cmd)
3374{
3375 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3376 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3377 struct sli4_sge *sgl = (struct sli4_sge *)(lpfc_cmd->fcp_bpl);
3378 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
3379 uint32_t num_bde = 0;
3380 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
3381 int prot_group_type = 0;
3382 int fcpdl;
3383
3384 /*
3385 * Start the lpfc command prep by bumping the sgl beyond fcp_cmnd
3386 * fcp_rsp regions to the first data bde entry
3387 */
3388 if (scsi_sg_count(scsi_cmnd)) {
3389 /*
3390 * The driver stores the segment count returned from pci_map_sg
3391 * because this a count of dma-mappings used to map the use_sg
3392 * pages. They are not guaranteed to be the same for those
3393 * architectures that implement an IOMMU.
3394 */
3395 datasegcnt = dma_map_sg(&phba->pcidev->dev,
3396 scsi_sglist(scsi_cmnd),
3397 scsi_sg_count(scsi_cmnd), datadir);
3398 if (unlikely(!datasegcnt))
3399 return 1;
3400
3401 sgl += 1;
3402 /* clear the last flag in the fcp_rsp map entry */
3403 sgl->word2 = le32_to_cpu(sgl->word2);
3404 bf_set(lpfc_sli4_sge_last, sgl, 0);
3405 sgl->word2 = cpu_to_le32(sgl->word2);
3406
3407 sgl += 1;
3408 lpfc_cmd->seg_cnt = datasegcnt;
3409 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
3410 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
3411 "9087 BLKGRD: %s: Too many sg segments"
3412 " from dma_map_sg. Config %d, seg_cnt"
3413 " %d\n",
3414 __func__, phba->cfg_sg_seg_cnt,
3415 lpfc_cmd->seg_cnt);
3416 scsi_dma_unmap(scsi_cmnd);
3417 return 1;
3418 }
3419
3420 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
3421
3422 switch (prot_group_type) {
3423 case LPFC_PG_TYPE_NO_DIF:
3424 num_bde = lpfc_bg_setup_sgl(phba, scsi_cmnd, sgl,
3425 datasegcnt);
3426 /* we should have 2 or more entries in buffer list */
3427 if (num_bde < 2)
3428 goto err;
3429 break;
3430 case LPFC_PG_TYPE_DIF_BUF:{
3431 /*
3432 * This type indicates that protection buffers are
3433 * passed to the driver, so that needs to be prepared
3434 * for DMA
3435 */
3436 protsegcnt = dma_map_sg(&phba->pcidev->dev,
3437 scsi_prot_sglist(scsi_cmnd),
3438 scsi_prot_sg_count(scsi_cmnd), datadir);
3439 if (unlikely(!protsegcnt)) {
3440 scsi_dma_unmap(scsi_cmnd);
3441 return 1;
3442 }
3443
3444 lpfc_cmd->prot_seg_cnt = protsegcnt;
3445 if (lpfc_cmd->prot_seg_cnt
3446 > phba->cfg_prot_sg_seg_cnt) {
3447 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
3448 "9088 BLKGRD: %s: Too many prot sg "
3449 "segments from dma_map_sg. Config %d,"
3450 "prot_seg_cnt %d\n", __func__,
3451 phba->cfg_prot_sg_seg_cnt,
3452 lpfc_cmd->prot_seg_cnt);
3453 dma_unmap_sg(&phba->pcidev->dev,
3454 scsi_prot_sglist(scsi_cmnd),
3455 scsi_prot_sg_count(scsi_cmnd),
3456 datadir);
3457 scsi_dma_unmap(scsi_cmnd);
3458 return 1;
3459 }
3460
3461 num_bde = lpfc_bg_setup_sgl_prot(phba, scsi_cmnd, sgl,
3462 datasegcnt, protsegcnt);
3463 /* we should have 3 or more entries in buffer list */
3464 if (num_bde < 3)
3465 goto err;
3466 break;
3467 }
3468 case LPFC_PG_TYPE_INVALID:
3469 default:
3470 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
3471 "9083 Unexpected protection group %i\n",
3472 prot_group_type);
3473 return 1;
3474 }
3475 }
3476
James Smart8012cc32012-10-31 14:44:49 -04003477 switch (scsi_get_prot_op(scsi_cmnd)) {
3478 case SCSI_PROT_WRITE_STRIP:
3479 case SCSI_PROT_READ_STRIP:
3480 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_STRIP;
3481 break;
3482 case SCSI_PROT_WRITE_INSERT:
3483 case SCSI_PROT_READ_INSERT:
3484 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_INSERT;
3485 break;
3486 case SCSI_PROT_WRITE_PASS:
3487 case SCSI_PROT_READ_PASS:
3488 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_PASS;
3489 break;
3490 }
3491
James Smartacd68592012-01-18 16:25:09 -05003492 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
3493
3494 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
3495
3496 /*
3497 * Due to difference in data length between DIF/non-DIF paths,
3498 * we need to set word 4 of IOCB here
3499 */
3500 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
James Smartacd68592012-01-18 16:25:09 -05003501
3502 return 0;
3503err:
3504 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
3505 "9084 Could not setup all needed BDE's"
3506 "prot_group_type=%d, num_bde=%d\n",
3507 prot_group_type, num_bde);
3508 return 1;
3509}
3510
3511/**
James Smart3772a992009-05-22 14:50:54 -04003512 * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3513 * @phba: The Hba for which this call is being executed.
3514 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3515 *
3516 * This routine wraps the actual DMA mapping function pointer from the
3517 * lpfc_hba struct.
3518 *
3519 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04003520 * 1 - Error
3521 * 0 - Success
James Smart3772a992009-05-22 14:50:54 -04003522 **/
3523static inline int
3524lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3525{
3526 return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
3527}
3528
3529/**
James Smartacd68592012-01-18 16:25:09 -05003530 * lpfc_bg_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3531 * using BlockGuard.
3532 * @phba: The Hba for which this call is being executed.
3533 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3534 *
3535 * This routine wraps the actual DMA mapping function pointer from the
3536 * lpfc_hba struct.
3537 *
3538 * Return codes:
3539 * 1 - Error
3540 * 0 - Success
3541 **/
3542static inline int
3543lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3544{
3545 return phba->lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
3546}
3547
3548/**
James Smart3621a712009-04-06 18:47:14 -04003549 * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
James Smartea2151b2008-09-07 11:52:10 -04003550 * @phba: Pointer to hba context object.
3551 * @vport: Pointer to vport object.
3552 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
3553 * @rsp_iocb: Pointer to response iocb object which reported error.
3554 *
3555 * This function posts an event when there is a SCSI command reporting
3556 * error from the scsi device.
3557 **/
3558static void
3559lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
3560 struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
3561 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3562 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
3563 uint32_t resp_info = fcprsp->rspStatus2;
3564 uint32_t scsi_status = fcprsp->rspStatus3;
3565 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
3566 struct lpfc_fast_path_event *fast_path_evt = NULL;
3567 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
3568 unsigned long flags;
3569
James Smart5989b8d2010-10-22 11:06:56 -04003570 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3571 return;
3572
James Smartea2151b2008-09-07 11:52:10 -04003573 /* If there is queuefull or busy condition send a scsi event */
3574 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
3575 (cmnd->result == SAM_STAT_BUSY)) {
3576 fast_path_evt = lpfc_alloc_fast_evt(phba);
3577 if (!fast_path_evt)
3578 return;
3579 fast_path_evt->un.scsi_evt.event_type =
3580 FC_REG_SCSI_EVENT;
3581 fast_path_evt->un.scsi_evt.subcategory =
3582 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
3583 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
3584 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
3585 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
3586 &pnode->nlp_portname, sizeof(struct lpfc_name));
3587 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
3588 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3589 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
3590 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
3591 fast_path_evt = lpfc_alloc_fast_evt(phba);
3592 if (!fast_path_evt)
3593 return;
3594 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
3595 FC_REG_SCSI_EVENT;
3596 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
3597 LPFC_EVENT_CHECK_COND;
3598 fast_path_evt->un.check_cond_evt.scsi_event.lun =
3599 cmnd->device->lun;
3600 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
3601 &pnode->nlp_portname, sizeof(struct lpfc_name));
3602 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
3603 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3604 fast_path_evt->un.check_cond_evt.sense_key =
3605 cmnd->sense_buffer[2] & 0xf;
3606 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
3607 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
3608 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3609 fcpi_parm &&
3610 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
3611 ((scsi_status == SAM_STAT_GOOD) &&
3612 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
3613 /*
3614 * If status is good or resid does not match with fcp_param and
3615 * there is valid fcpi_parm, then there is a read_check error
3616 */
3617 fast_path_evt = lpfc_alloc_fast_evt(phba);
3618 if (!fast_path_evt)
3619 return;
3620 fast_path_evt->un.read_check_error.header.event_type =
3621 FC_REG_FABRIC_EVENT;
3622 fast_path_evt->un.read_check_error.header.subcategory =
3623 LPFC_EVENT_FCPRDCHKERR;
3624 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
3625 &pnode->nlp_portname, sizeof(struct lpfc_name));
3626 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
3627 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3628 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
3629 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
3630 fast_path_evt->un.read_check_error.fcpiparam =
3631 fcpi_parm;
3632 } else
3633 return;
3634
3635 fast_path_evt->vport = vport;
3636 spin_lock_irqsave(&phba->hbalock, flags);
3637 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
3638 spin_unlock_irqrestore(&phba->hbalock, flags);
3639 lpfc_worker_wake_up(phba);
3640 return;
3641}
James Smart9bad7672008-12-04 22:39:02 -05003642
3643/**
James Smartf1126682009-06-10 17:22:44 -04003644 * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
James Smart3772a992009-05-22 14:50:54 -04003645 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -05003646 * @psb: The scsi buffer which is going to be un-mapped.
3647 *
3648 * This routine does DMA un-mapping of scatter gather list of scsi command
James Smart3772a992009-05-22 14:50:54 -04003649 * field of @lpfc_cmd for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05003650 **/
dea31012005-04-17 16:05:31 -05003651static void
James Smartf1126682009-06-10 17:22:44 -04003652lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James Smartbcf4dbf2006-07-06 15:50:08 -04003653{
3654 /*
3655 * There are only two special cases to consider. (1) the scsi command
3656 * requested scatter-gather usage or (2) the scsi command allocated
3657 * a request buffer, but did not request use_sg. There is a third
3658 * case, but it does not require resource deallocation.
3659 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003660 if (psb->seg_cnt > 0)
3661 scsi_dma_unmap(psb->pCmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003662 if (psb->prot_seg_cnt > 0)
3663 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
3664 scsi_prot_sg_count(psb->pCmd),
3665 psb->pCmd->sc_data_direction);
James Smartbcf4dbf2006-07-06 15:50:08 -04003666}
3667
James Smart9bad7672008-12-04 22:39:02 -05003668/**
James Smart3621a712009-04-06 18:47:14 -04003669 * lpfc_handler_fcp_err - FCP response handler
James Smart9bad7672008-12-04 22:39:02 -05003670 * @vport: The virtual port for which this call is being executed.
3671 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
3672 * @rsp_iocb: The response IOCB which contains FCP error.
3673 *
3674 * This routine is called to process response IOCB with status field
3675 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
3676 * based upon SCSI and FCP error.
3677 **/
James Smartbcf4dbf2006-07-06 15:50:08 -04003678static void
James Smart2e0fef82007-06-17 19:56:36 -05003679lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
3680 struct lpfc_iocbq *rsp_iocb)
dea31012005-04-17 16:05:31 -05003681{
3682 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3683 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
3684 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
James Smart7054a602007-04-25 09:52:34 -04003685 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
dea31012005-04-17 16:05:31 -05003686 uint32_t resp_info = fcprsp->rspStatus2;
3687 uint32_t scsi_status = fcprsp->rspStatus3;
James Smartc7743952006-12-02 13:34:42 -05003688 uint32_t *lp;
dea31012005-04-17 16:05:31 -05003689 uint32_t host_status = DID_OK;
3690 uint32_t rsplen = 0;
James Smartc7743952006-12-02 13:34:42 -05003691 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea31012005-04-17 16:05:31 -05003692
James Smartea2151b2008-09-07 11:52:10 -04003693
dea31012005-04-17 16:05:31 -05003694 /*
3695 * If this is a task management command, there is no
3696 * scsi packet associated with this lpfc_cmd. The driver
3697 * consumes it.
3698 */
3699 if (fcpcmd->fcpCntl2) {
3700 scsi_status = 0;
3701 goto out;
3702 }
3703
James Smart6a9c52c2009-10-02 15:16:51 -04003704 if (resp_info & RSP_LEN_VALID) {
3705 rsplen = be32_to_cpu(fcprsp->rspRspLen);
James Smarte40a02c2010-02-26 14:13:54 -05003706 if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
James Smart6a9c52c2009-10-02 15:16:51 -04003707 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3708 "2719 Invalid response length: "
3709 "tgt x%x lun x%x cmnd x%x rsplen x%x\n",
3710 cmnd->device->id,
3711 cmnd->device->lun, cmnd->cmnd[0],
3712 rsplen);
3713 host_status = DID_ERROR;
3714 goto out;
3715 }
James Smarte40a02c2010-02-26 14:13:54 -05003716 if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
3717 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3718 "2757 Protocol failure detected during "
3719 "processing of FCP I/O op: "
3720 "tgt x%x lun x%x cmnd x%x rspInfo3 x%x\n",
3721 cmnd->device->id,
3722 cmnd->device->lun, cmnd->cmnd[0],
3723 fcprsp->rspInfo3);
3724 host_status = DID_ERROR;
3725 goto out;
3726 }
James Smart6a9c52c2009-10-02 15:16:51 -04003727 }
3728
James Smartc7743952006-12-02 13:34:42 -05003729 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
3730 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
3731 if (snslen > SCSI_SENSE_BUFFERSIZE)
3732 snslen = SCSI_SENSE_BUFFERSIZE;
3733
3734 if (resp_info & RSP_LEN_VALID)
3735 rsplen = be32_to_cpu(fcprsp->rspRspLen);
3736 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
3737 }
3738 lp = (uint32_t *)cmnd->sense_buffer;
3739
James Smartaa1c7ee2012-08-14 14:26:06 -04003740 /* special handling for under run conditions */
3741 if (!scsi_status && (resp_info & RESID_UNDER)) {
3742 /* don't log under runs if fcp set... */
3743 if (vport->cfg_log_verbose & LOG_FCP)
3744 logit = LOG_FCP_ERROR;
3745 /* unless operator says so */
3746 if (vport->cfg_log_verbose & LOG_FCP_UNDER)
3747 logit = LOG_FCP_UNDER;
3748 }
James Smartc7743952006-12-02 13:34:42 -05003749
James Smarte8b62012007-08-02 11:10:09 -04003750 lpfc_printf_vlog(vport, KERN_WARNING, logit,
James Smarte2a0a9d2008-12-04 22:40:02 -05003751 "9024 FCP command x%x failed: x%x SNS x%x x%x "
James Smarte8b62012007-08-02 11:10:09 -04003752 "Data: x%x x%x x%x x%x x%x\n",
3753 cmnd->cmnd[0], scsi_status,
3754 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
3755 be32_to_cpu(fcprsp->rspResId),
3756 be32_to_cpu(fcprsp->rspSnsLen),
3757 be32_to_cpu(fcprsp->rspRspLen),
3758 fcprsp->rspInfo3);
dea31012005-04-17 16:05:31 -05003759
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003760 scsi_set_resid(cmnd, 0);
dea31012005-04-17 16:05:31 -05003761 if (resp_info & RESID_UNDER) {
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003762 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea31012005-04-17 16:05:31 -05003763
James Smart73d91e52011-10-10 21:32:10 -04003764 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_UNDER,
James Smarte2a0a9d2008-12-04 22:40:02 -05003765 "9025 FCP Read Underrun, expected %d, "
James Smarte8b62012007-08-02 11:10:09 -04003766 "residual %d Data: x%x x%x x%x\n",
3767 be32_to_cpu(fcpcmd->fcpDl),
3768 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
3769 cmnd->underflow);
dea31012005-04-17 16:05:31 -05003770
3771 /*
James Smart7054a602007-04-25 09:52:34 -04003772 * If there is an under run check if under run reported by
3773 * storage array is same as the under run reported by HBA.
3774 * If this is not same, there is a dropped frame.
3775 */
3776 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3777 fcpi_parm &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003778 (scsi_get_resid(cmnd) != fcpi_parm)) {
James Smarte8b62012007-08-02 11:10:09 -04003779 lpfc_printf_vlog(vport, KERN_WARNING,
3780 LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05003781 "9026 FCP Read Check Error "
James Smarte8b62012007-08-02 11:10:09 -04003782 "and Underrun Data: x%x x%x x%x x%x\n",
3783 be32_to_cpu(fcpcmd->fcpDl),
3784 scsi_get_resid(cmnd), fcpi_parm,
3785 cmnd->cmnd[0]);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003786 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
James Smart7054a602007-04-25 09:52:34 -04003787 host_status = DID_ERROR;
3788 }
3789 /*
dea31012005-04-17 16:05:31 -05003790 * The cmnd->underflow is the minimum number of bytes that must
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003791 * be transferred for this command. Provided a sense condition
dea31012005-04-17 16:05:31 -05003792 * is not present, make sure the actual amount transferred is at
3793 * least the underflow value or fail.
3794 */
3795 if (!(resp_info & SNS_LEN_VALID) &&
3796 (scsi_status == SAM_STAT_GOOD) &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003797 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
3798 < cmnd->underflow)) {
James Smarte8b62012007-08-02 11:10:09 -04003799 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05003800 "9027 FCP command x%x residual "
James Smarte8b62012007-08-02 11:10:09 -04003801 "underrun converted to error "
3802 "Data: x%x x%x x%x\n",
James Smart66dbfbe2007-08-05 06:08:38 -04003803 cmnd->cmnd[0], scsi_bufflen(cmnd),
James Smarte8b62012007-08-02 11:10:09 -04003804 scsi_get_resid(cmnd), cmnd->underflow);
dea31012005-04-17 16:05:31 -05003805 host_status = DID_ERROR;
3806 }
3807 } else if (resp_info & RESID_OVER) {
James Smarte8b62012007-08-02 11:10:09 -04003808 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05003809 "9028 FCP command x%x residual overrun error. "
James Smarte4e74272009-07-19 10:01:38 -04003810 "Data: x%x x%x\n", cmnd->cmnd[0],
James Smarte8b62012007-08-02 11:10:09 -04003811 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea31012005-04-17 16:05:31 -05003812 host_status = DID_ERROR;
3813
3814 /*
3815 * Check SLI validation that all the transfer was actually done
James Smart582dd792012-08-03 12:34:44 -04003816 * (fcpi_parm should be zero).
dea31012005-04-17 16:05:31 -05003817 */
James Smart582dd792012-08-03 12:34:44 -04003818 } else if (fcpi_parm) {
James Smarte8b62012007-08-02 11:10:09 -04003819 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
James Smart582dd792012-08-03 12:34:44 -04003820 "9029 FCP Data Transfer Check Error: "
James Smarteee88772010-09-29 11:19:08 -04003821 "x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003822 be32_to_cpu(fcpcmd->fcpDl),
3823 be32_to_cpu(fcprsp->rspResId),
James Smarteee88772010-09-29 11:19:08 -04003824 fcpi_parm, cmnd->cmnd[0], scsi_status);
3825 switch (scsi_status) {
3826 case SAM_STAT_GOOD:
3827 case SAM_STAT_CHECK_CONDITION:
3828 /* Fabric dropped a data frame. Fail any successful
3829 * command in which we detected dropped frames.
3830 * A status of good or some check conditions could
3831 * be considered a successful command.
3832 */
3833 host_status = DID_ERROR;
3834 break;
3835 }
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003836 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea31012005-04-17 16:05:31 -05003837 }
3838
3839 out:
3840 cmnd->result = ScsiResult(host_status, scsi_status);
James Smartea2151b2008-09-07 11:52:10 -04003841 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
dea31012005-04-17 16:05:31 -05003842}
3843
James Smart9bad7672008-12-04 22:39:02 -05003844/**
James Smart3621a712009-04-06 18:47:14 -04003845 * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
James Smart9bad7672008-12-04 22:39:02 -05003846 * @phba: The Hba for which this call is being executed.
3847 * @pIocbIn: The command IOCBQ for the scsi cmnd.
James Smart3772a992009-05-22 14:50:54 -04003848 * @pIocbOut: The response IOCBQ for the scsi cmnd.
James Smart9bad7672008-12-04 22:39:02 -05003849 *
3850 * This routine assigns scsi command result by looking into response IOCB
3851 * status field appropriately. This routine handles QUEUE FULL condition as
3852 * well by ramping down device queue depth.
3853 **/
dea31012005-04-17 16:05:31 -05003854static void
3855lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
3856 struct lpfc_iocbq *pIocbOut)
3857{
3858 struct lpfc_scsi_buf *lpfc_cmd =
3859 (struct lpfc_scsi_buf *) pIocbIn->context1;
James Smart2e0fef82007-06-17 19:56:36 -05003860 struct lpfc_vport *vport = pIocbIn->vport;
dea31012005-04-17 16:05:31 -05003861 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
3862 struct lpfc_nodelist *pnode = rdata->pnode;
James Smart75baf692010-06-08 18:31:21 -04003863 struct scsi_cmnd *cmd;
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05003864 int result;
James Smarta257bf92009-04-06 18:48:10 -04003865 struct scsi_device *tmp_sdev;
James Smart5ffc2662009-11-18 15:39:44 -05003866 int depth;
James Smartfa61a542008-01-11 01:52:42 -05003867 unsigned long flags;
James Smartea2151b2008-09-07 11:52:10 -04003868 struct lpfc_fast_path_event *fast_path_evt;
James Smart75baf692010-06-08 18:31:21 -04003869 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04003870 uint32_t queue_depth, scsi_id;
James Smart73d91e52011-10-10 21:32:10 -04003871 uint32_t logit = LOG_FCP;
dea31012005-04-17 16:05:31 -05003872
James Smart75baf692010-06-08 18:31:21 -04003873 /* Sanity check on return of outstanding command */
3874 if (!(lpfc_cmd->pCmd))
3875 return;
3876 cmd = lpfc_cmd->pCmd;
3877 shost = cmd->device->host;
3878
James Smarte3d2b802012-08-14 14:25:43 -04003879 lpfc_cmd->result = (pIocbOut->iocb.un.ulpWord[4] & IOERR_PARAM_MASK);
dea31012005-04-17 16:05:31 -05003880 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
James Smart341af102010-01-26 23:07:37 -05003881 /* pick up SLI4 exhange busy status from HBA */
3882 lpfc_cmd->exch_busy = pIocbOut->iocb_flag & LPFC_EXCHANGE_BUSY;
3883
James Smart9a6b09c2012-03-01 22:37:42 -05003884#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
3885 if (lpfc_cmd->prot_data_type) {
3886 struct scsi_dif_tuple *src = NULL;
3887
3888 src = (struct scsi_dif_tuple *)lpfc_cmd->prot_data_segment;
3889 /*
3890 * Used to restore any changes to protection
3891 * data for error injection.
3892 */
3893 switch (lpfc_cmd->prot_data_type) {
3894 case LPFC_INJERR_REFTAG:
3895 src->ref_tag =
3896 lpfc_cmd->prot_data;
3897 break;
3898 case LPFC_INJERR_APPTAG:
3899 src->app_tag =
3900 (uint16_t)lpfc_cmd->prot_data;
3901 break;
3902 case LPFC_INJERR_GUARD:
3903 src->guard_tag =
3904 (uint16_t)lpfc_cmd->prot_data;
3905 break;
3906 default:
3907 break;
3908 }
3909
3910 lpfc_cmd->prot_data = 0;
3911 lpfc_cmd->prot_data_type = 0;
3912 lpfc_cmd->prot_data_segment = NULL;
3913 }
3914#endif
James Smart109f6ed2008-12-04 22:39:08 -05003915 if (pnode && NLP_CHK_NODE_ACT(pnode))
3916 atomic_dec(&pnode->cmd_pending);
dea31012005-04-17 16:05:31 -05003917
3918 if (lpfc_cmd->status) {
3919 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
3920 (lpfc_cmd->result & IOERR_DRVR_MASK))
3921 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
3922 else if (lpfc_cmd->status >= IOSTAT_CNT)
3923 lpfc_cmd->status = IOSTAT_DEFAULT;
James Smartaa1c7ee2012-08-14 14:26:06 -04003924 if (lpfc_cmd->status == IOSTAT_FCP_RSP_ERROR &&
3925 !lpfc_cmd->fcp_rsp->rspStatus3 &&
3926 (lpfc_cmd->fcp_rsp->rspStatus2 & RESID_UNDER) &&
3927 !(vport->cfg_log_verbose & LOG_FCP_UNDER))
James Smart73d91e52011-10-10 21:32:10 -04003928 logit = 0;
3929 else
3930 logit = LOG_FCP | LOG_FCP_UNDER;
3931 lpfc_printf_vlog(vport, KERN_WARNING, logit,
3932 "9030 FCP cmd x%x failed <%d/%d> "
James Smart5a0d80f2012-05-09 21:18:20 -04003933 "status: x%x result: x%x "
3934 "sid: x%x did: x%x oxid: x%x "
3935 "Data: x%x x%x\n",
James Smart73d91e52011-10-10 21:32:10 -04003936 cmd->cmnd[0],
3937 cmd->device ? cmd->device->id : 0xffff,
3938 cmd->device ? cmd->device->lun : 0xffff,
3939 lpfc_cmd->status, lpfc_cmd->result,
James Smart5a0d80f2012-05-09 21:18:20 -04003940 vport->fc_myDID, pnode->nlp_DID,
3941 phba->sli_rev == LPFC_SLI_REV4 ?
3942 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
James Smart73d91e52011-10-10 21:32:10 -04003943 pIocbOut->iocb.ulpContext,
3944 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
dea31012005-04-17 16:05:31 -05003945
3946 switch (lpfc_cmd->status) {
3947 case IOSTAT_FCP_RSP_ERROR:
3948 /* Call FCP RSP handler to determine result */
James Smart2e0fef82007-06-17 19:56:36 -05003949 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea31012005-04-17 16:05:31 -05003950 break;
3951 case IOSTAT_NPORT_BSY:
3952 case IOSTAT_FABRIC_BSY:
James Smart0f1f53a2008-08-24 21:50:18 -04003953 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
James Smartea2151b2008-09-07 11:52:10 -04003954 fast_path_evt = lpfc_alloc_fast_evt(phba);
3955 if (!fast_path_evt)
3956 break;
3957 fast_path_evt->un.fabric_evt.event_type =
3958 FC_REG_FABRIC_EVENT;
3959 fast_path_evt->un.fabric_evt.subcategory =
3960 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
3961 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
3962 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
3963 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
3964 &pnode->nlp_portname,
3965 sizeof(struct lpfc_name));
3966 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
3967 &pnode->nlp_nodename,
3968 sizeof(struct lpfc_name));
3969 }
3970 fast_path_evt->vport = vport;
3971 fast_path_evt->work_evt.evt =
3972 LPFC_EVT_FASTPATH_MGMT_EVT;
3973 spin_lock_irqsave(&phba->hbalock, flags);
3974 list_add_tail(&fast_path_evt->work_evt.evt_listp,
3975 &phba->work_list);
3976 spin_unlock_irqrestore(&phba->hbalock, flags);
3977 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05003978 break;
James Smart92d7f7b2007-06-17 19:56:38 -05003979 case IOSTAT_LOCAL_REJECT:
James Smart1151e3e2011-02-16 12:39:35 -05003980 case IOSTAT_REMOTE_STOP:
James Smartab56dc22011-02-16 12:39:57 -05003981 if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
3982 lpfc_cmd->result ==
3983 IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
3984 lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
3985 lpfc_cmd->result ==
3986 IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
3987 cmd->result = ScsiResult(DID_NO_CONNECT, 0);
3988 break;
3989 }
James Smartd7c255b2008-08-24 21:50:00 -04003990 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
James Smart92d7f7b2007-06-17 19:56:38 -05003991 lpfc_cmd->result == IOERR_NO_RESOURCES ||
James Smartb92938b2010-06-07 15:24:12 -04003992 lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
3993 lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
James Smart92d7f7b2007-06-17 19:56:38 -05003994 cmd->result = ScsiResult(DID_REQUEUE, 0);
James Smart58da1ff2008-04-07 10:15:56 -04003995 break;
James Smarte2a0a9d2008-12-04 22:40:02 -05003996 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003997 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
3998 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
3999 pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
4000 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
4001 /*
4002 * This is a response for a BG enabled
4003 * cmd. Parse BG error
4004 */
4005 lpfc_parse_bg_err(phba, lpfc_cmd,
4006 pIocbOut);
4007 break;
4008 } else {
4009 lpfc_printf_vlog(vport, KERN_WARNING,
4010 LOG_BG,
4011 "9031 non-zero BGSTAT "
James Smart6a9c52c2009-10-02 15:16:51 -04004012 "on unprotected cmd\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05004013 }
4014 }
James Smart1151e3e2011-02-16 12:39:35 -05004015 if ((lpfc_cmd->status == IOSTAT_REMOTE_STOP)
4016 && (phba->sli_rev == LPFC_SLI_REV4)
4017 && (pnode && NLP_CHK_NODE_ACT(pnode))) {
4018 /* This IO was aborted by the target, we don't
4019 * know the rxid and because we did not send the
4020 * ABTS we cannot generate and RRQ.
4021 */
4022 lpfc_set_rrq_active(phba, pnode,
James Smartee0f4fe2012-05-09 21:19:14 -04004023 lpfc_cmd->cur_iocbq.sli4_lxritag,
4024 0, 0);
James Smart1151e3e2011-02-16 12:39:35 -05004025 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004026 /* else: fall through */
dea31012005-04-17 16:05:31 -05004027 default:
4028 cmd->result = ScsiResult(DID_ERROR, 0);
4029 break;
4030 }
4031
James Smart58da1ff2008-04-07 10:15:56 -04004032 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004033 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
James Smart0f1f53a2008-08-24 21:50:18 -04004034 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
4035 SAM_STAT_BUSY);
James Smartab56dc22011-02-16 12:39:57 -05004036 } else
dea31012005-04-17 16:05:31 -05004037 cmd->result = ScsiResult(DID_OK, 0);
dea31012005-04-17 16:05:31 -05004038
4039 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
4040 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
4041
James Smarte8b62012007-08-02 11:10:09 -04004042 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4043 "0710 Iodone <%d/%d> cmd %p, error "
4044 "x%x SNS x%x x%x Data: x%x x%x\n",
4045 cmd->device->id, cmd->device->lun, cmd,
4046 cmd->result, *lp, *(lp + 3), cmd->retries,
4047 scsi_get_resid(cmd));
dea31012005-04-17 16:05:31 -05004048 }
4049
James Smartea2151b2008-09-07 11:52:10 -04004050 lpfc_update_stats(phba, lpfc_cmd);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05004051 result = cmd->result;
James Smart977b5a02008-09-07 11:52:04 -04004052 if (vport->cfg_max_scsicmpl_time &&
4053 time_after(jiffies, lpfc_cmd->start_time +
4054 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
James Smarta257bf92009-04-06 18:48:10 -04004055 spin_lock_irqsave(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05004056 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
4057 if (pnode->cmd_qdepth >
4058 atomic_read(&pnode->cmd_pending) &&
4059 (atomic_read(&pnode->cmd_pending) >
4060 LPFC_MIN_TGT_QDEPTH) &&
4061 ((cmd->cmnd[0] == READ_10) ||
4062 (cmd->cmnd[0] == WRITE_10)))
4063 pnode->cmd_qdepth =
4064 atomic_read(&pnode->cmd_pending);
James Smart977b5a02008-09-07 11:52:04 -04004065
James Smart109f6ed2008-12-04 22:39:08 -05004066 pnode->last_change_time = jiffies;
4067 }
James Smarta257bf92009-04-06 18:48:10 -04004068 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05004069 } else if (pnode && NLP_CHK_NODE_ACT(pnode)) {
James Smart7dc517d2010-07-14 15:32:10 -04004070 if ((pnode->cmd_qdepth < vport->cfg_tgt_queue_depth) &&
James Smart977b5a02008-09-07 11:52:04 -04004071 time_after(jiffies, pnode->last_change_time +
James Smart109f6ed2008-12-04 22:39:08 -05004072 msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
James Smarta257bf92009-04-06 18:48:10 -04004073 spin_lock_irqsave(shost->host_lock, flags);
James Smart7dc517d2010-07-14 15:32:10 -04004074 depth = pnode->cmd_qdepth * LPFC_TGTQ_RAMPUP_PCENT
4075 / 100;
4076 depth = depth ? depth : 1;
4077 pnode->cmd_qdepth += depth;
4078 if (pnode->cmd_qdepth > vport->cfg_tgt_queue_depth)
4079 pnode->cmd_qdepth = vport->cfg_tgt_queue_depth;
James Smart109f6ed2008-12-04 22:39:08 -05004080 pnode->last_change_time = jiffies;
James Smarta257bf92009-04-06 18:48:10 -04004081 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05004082 }
James Smart977b5a02008-09-07 11:52:04 -04004083 }
4084
James Smart1dcb58e2007-04-25 09:51:30 -04004085 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James Smarta257bf92009-04-06 18:48:10 -04004086
4087 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
4088 queue_depth = cmd->device->queue_depth;
4089 scsi_id = cmd->device->id;
dea31012005-04-17 16:05:31 -05004090 cmd->scsi_done(cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004091
Jamie Wellnitzb8086082006-02-28 22:33:12 -05004092 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart876dd7d2012-09-29 11:31:28 -04004093 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92e3af62012-08-14 14:26:28 -04004094 lpfc_cmd->pCmd = NULL;
James Smart876dd7d2012-09-29 11:31:28 -04004095 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart92e3af62012-08-14 14:26:28 -04004096
James Smartfa61a542008-01-11 01:52:42 -05004097 /*
4098 * If there is a thread waiting for command completion
4099 * wake up the thread.
4100 */
James Smarta257bf92009-04-06 18:48:10 -04004101 spin_lock_irqsave(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05004102 if (lpfc_cmd->waitq)
4103 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04004104 spin_unlock_irqrestore(shost->host_lock, flags);
Jamie Wellnitzb8086082006-02-28 22:33:12 -05004105 lpfc_release_scsi_buf(phba, lpfc_cmd);
4106 return;
4107 }
4108
James Smart92d7f7b2007-06-17 19:56:38 -05004109 if (!result)
James Smarta257bf92009-04-06 18:48:10 -04004110 lpfc_rampup_queue_depth(vport, queue_depth);
James Smart92d7f7b2007-06-17 19:56:38 -05004111
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05004112 /*
4113 * Check for queue full. If the lun is reporting queue full, then
4114 * back off the lun queue depth to prevent target overloads.
4115 */
James Smart58da1ff2008-04-07 10:15:56 -04004116 if (result == SAM_STAT_TASK_SET_FULL && pnode &&
4117 NLP_CHK_NODE_ACT(pnode)) {
James Smarta257bf92009-04-06 18:48:10 -04004118 shost_for_each_device(tmp_sdev, shost) {
4119 if (tmp_sdev->id != scsi_id)
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05004120 continue;
4121 depth = scsi_track_queue_full(tmp_sdev,
James Smart5ffc2662009-11-18 15:39:44 -05004122 tmp_sdev->queue_depth-1);
4123 if (depth <= 0)
4124 continue;
James Smarte8b62012007-08-02 11:10:09 -04004125 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4126 "0711 detected queue full - lun queue "
4127 "depth adjusted to %d.\n", depth);
James Smartea2151b2008-09-07 11:52:10 -04004128 lpfc_send_sdev_queuedepth_change_event(phba, vport,
James Smart5ffc2662009-11-18 15:39:44 -05004129 pnode,
4130 tmp_sdev->lun,
4131 depth+1, depth);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05004132 }
4133 }
4134
James Smart876dd7d2012-09-29 11:31:28 -04004135 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92e3af62012-08-14 14:26:28 -04004136 lpfc_cmd->pCmd = NULL;
James Smart876dd7d2012-09-29 11:31:28 -04004137 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart92e3af62012-08-14 14:26:28 -04004138
James Smartfa61a542008-01-11 01:52:42 -05004139 /*
4140 * If there is a thread waiting for command completion
4141 * wake up the thread.
4142 */
James Smarta257bf92009-04-06 18:48:10 -04004143 spin_lock_irqsave(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05004144 if (lpfc_cmd->waitq)
4145 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04004146 spin_unlock_irqrestore(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05004147
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004148 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004149}
4150
James Smart34b02dc2008-08-24 21:49:55 -04004151/**
James Smart3621a712009-04-06 18:47:14 -04004152 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
James Smart34b02dc2008-08-24 21:49:55 -04004153 * @data: A pointer to the immediate command data portion of the IOCB.
4154 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
4155 *
4156 * The routine copies the entire FCP command from @fcp_cmnd to @data while
4157 * byte swapping the data to big endian format for transmission on the wire.
4158 **/
4159static void
4160lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
4161{
4162 int i, j;
4163 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
4164 i += sizeof(uint32_t), j++) {
4165 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
4166 }
4167}
4168
James Smart9bad7672008-12-04 22:39:02 -05004169/**
James Smartf1126682009-06-10 17:22:44 -04004170 * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05004171 * @vport: The virtual port for which this call is being executed.
4172 * @lpfc_cmd: The scsi command which needs to send.
4173 * @pnode: Pointer to lpfc_nodelist.
4174 *
4175 * This routine initializes fcp_cmnd and iocb data structure from scsi command
James Smart3772a992009-05-22 14:50:54 -04004176 * to transfer for device with SLI3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05004177 **/
dea31012005-04-17 16:05:31 -05004178static void
James Smartf1126682009-06-10 17:22:44 -04004179lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
James Smart2e0fef82007-06-17 19:56:36 -05004180 struct lpfc_nodelist *pnode)
dea31012005-04-17 16:05:31 -05004181{
James Smart2e0fef82007-06-17 19:56:36 -05004182 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004183 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
4184 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
4185 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
4186 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
4187 int datadir = scsi_cmnd->sc_data_direction;
James Smart7e2b19f2007-10-29 11:00:39 -04004188 char tag[2];
James Smart027140e2012-08-03 12:35:44 -04004189 uint8_t *ptr;
4190 bool sli4;
dea31012005-04-17 16:05:31 -05004191
James Smart58da1ff2008-04-07 10:15:56 -04004192 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
4193 return;
4194
dea31012005-04-17 16:05:31 -05004195 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
James.Smart@Emulex.Com69859dc2005-08-10 15:02:37 -04004196 /* clear task management bits */
4197 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea31012005-04-17 16:05:31 -05004198
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -04004199 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
4200 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05004201
James Smart027140e2012-08-03 12:35:44 -04004202 ptr = &fcp_cmnd->fcpCdb[0];
4203 memcpy(ptr, scsi_cmnd->cmnd, scsi_cmnd->cmd_len);
4204 if (scsi_cmnd->cmd_len < LPFC_FCP_CDB_LEN) {
4205 ptr += scsi_cmnd->cmd_len;
4206 memset(ptr, 0, (LPFC_FCP_CDB_LEN - scsi_cmnd->cmd_len));
4207 }
4208
James Smart7e2b19f2007-10-29 11:00:39 -04004209 if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
4210 switch (tag[0]) {
dea31012005-04-17 16:05:31 -05004211 case HEAD_OF_QUEUE_TAG:
4212 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
4213 break;
4214 case ORDERED_QUEUE_TAG:
4215 fcp_cmnd->fcpCntl1 = ORDERED_Q;
4216 break;
4217 default:
4218 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
4219 break;
4220 }
4221 } else
James Smartfe8f7f92013-01-03 15:43:03 -05004222 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
dea31012005-04-17 16:05:31 -05004223
James Smart027140e2012-08-03 12:35:44 -04004224 sli4 = (phba->sli_rev == LPFC_SLI_REV4);
4225
dea31012005-04-17 16:05:31 -05004226 /*
4227 * There are three possibilities here - use scatter-gather segment, use
4228 * the single mapping, or neither. Start the lpfc command prep by
4229 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
4230 * data bde entry.
4231 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05004232 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05004233 if (datadir == DMA_TO_DEVICE) {
4234 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
James Smart027140e2012-08-03 12:35:44 -04004235 if (sli4)
4236 iocb_cmd->ulpPU = PARM_READ_CHECK;
4237 else {
James Smart3772a992009-05-22 14:50:54 -04004238 iocb_cmd->un.fcpi.fcpi_parm = 0;
4239 iocb_cmd->ulpPU = 0;
James Smart027140e2012-08-03 12:35:44 -04004240 }
dea31012005-04-17 16:05:31 -05004241 fcp_cmnd->fcpCntl3 = WRITE_DATA;
4242 phba->fc4OutputRequests++;
4243 } else {
4244 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
4245 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05004246 fcp_cmnd->fcpCntl3 = READ_DATA;
4247 phba->fc4InputRequests++;
4248 }
4249 } else {
4250 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
4251 iocb_cmd->un.fcpi.fcpi_parm = 0;
4252 iocb_cmd->ulpPU = 0;
4253 fcp_cmnd->fcpCntl3 = 0;
4254 phba->fc4ControlRequests++;
4255 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004256 if (phba->sli_rev == 3 &&
4257 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04004258 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05004259 /*
4260 * Finish initializing those IOCB fields that are independent
4261 * of the scsi_cmnd request_buffer
4262 */
4263 piocbq->iocb.ulpContext = pnode->nlp_rpi;
James Smart027140e2012-08-03 12:35:44 -04004264 if (sli4)
James Smart6d368e52011-05-24 11:44:12 -04004265 piocbq->iocb.ulpContext =
4266 phba->sli4_hba.rpi_ids[pnode->nlp_rpi];
dea31012005-04-17 16:05:31 -05004267 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
4268 piocbq->iocb.ulpFCP2Rcvy = 1;
James Smart09372822008-01-11 01:52:54 -05004269 else
4270 piocbq->iocb.ulpFCP2Rcvy = 0;
dea31012005-04-17 16:05:31 -05004271
4272 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
4273 piocbq->context1 = lpfc_cmd;
4274 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
4275 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
James Smart2e0fef82007-06-17 19:56:36 -05004276 piocbq->vport = vport;
dea31012005-04-17 16:05:31 -05004277}
4278
James Smart9bad7672008-12-04 22:39:02 -05004279/**
James Smart6d368e52011-05-24 11:44:12 -04004280 * lpfc_scsi_prep_task_mgmt_cmd - Convert SLI3 scsi TM cmd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05004281 * @vport: The virtual port for which this call is being executed.
4282 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
4283 * @lun: Logical unit number.
4284 * @task_mgmt_cmd: SCSI task management command.
4285 *
James Smart3772a992009-05-22 14:50:54 -04004286 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
4287 * for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05004288 *
4289 * Return codes:
4290 * 0 - Error
4291 * 1 - Success
4292 **/
dea31012005-04-17 16:05:31 -05004293static int
James Smartf1126682009-06-10 17:22:44 -04004294lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
dea31012005-04-17 16:05:31 -05004295 struct lpfc_scsi_buf *lpfc_cmd,
James Smart420b630d2006-07-06 15:50:16 -04004296 unsigned int lun,
dea31012005-04-17 16:05:31 -05004297 uint8_t task_mgmt_cmd)
4298{
dea31012005-04-17 16:05:31 -05004299 struct lpfc_iocbq *piocbq;
4300 IOCB_t *piocb;
4301 struct fcp_cmnd *fcp_cmnd;
James Smart0b18ac42006-05-01 21:50:40 -04004302 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea31012005-04-17 16:05:31 -05004303 struct lpfc_nodelist *ndlp = rdata->pnode;
4304
James Smart58da1ff2008-04-07 10:15:56 -04004305 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
4306 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05004307 return 0;
dea31012005-04-17 16:05:31 -05004308
dea31012005-04-17 16:05:31 -05004309 piocbq = &(lpfc_cmd->cur_iocbq);
James Smart2e0fef82007-06-17 19:56:36 -05004310 piocbq->vport = vport;
4311
dea31012005-04-17 16:05:31 -05004312 piocb = &piocbq->iocb;
4313
4314 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart34b02dc2008-08-24 21:49:55 -04004315 /* Clear out any old data in the FCP command area */
4316 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
4317 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05004318 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
James Smarte2a0a9d2008-12-04 22:40:02 -05004319 if (vport->phba->sli_rev == 3 &&
4320 !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04004321 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05004322 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
dea31012005-04-17 16:05:31 -05004323 piocb->ulpContext = ndlp->nlp_rpi;
James Smart6d368e52011-05-24 11:44:12 -04004324 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
4325 piocb->ulpContext =
4326 vport->phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
4327 }
dea31012005-04-17 16:05:31 -05004328 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
4329 piocb->ulpFCP2Rcvy = 1;
4330 }
4331 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
4332
4333 /* ulpTimeout is only one byte */
4334 if (lpfc_cmd->timeout > 0xff) {
4335 /*
4336 * Do not timeout the command at the firmware level.
4337 * The driver will provide the timeout mechanism.
4338 */
4339 piocb->ulpTimeout = 0;
James Smartf1126682009-06-10 17:22:44 -04004340 } else
dea31012005-04-17 16:05:31 -05004341 piocb->ulpTimeout = lpfc_cmd->timeout;
James Smartf1126682009-06-10 17:22:44 -04004342
4343 if (vport->phba->sli_rev == LPFC_SLI_REV4)
4344 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004345
James Smart2e0fef82007-06-17 19:56:36 -05004346 return 1;
dea31012005-04-17 16:05:31 -05004347}
4348
James Smart9bad7672008-12-04 22:39:02 -05004349/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004350 * lpfc_scsi_api_table_setup - Set up scsi api function jump table
James Smart3772a992009-05-22 14:50:54 -04004351 * @phba: The hba struct for which this call is being executed.
4352 * @dev_grp: The HBA PCI-Device group number.
4353 *
4354 * This routine sets up the SCSI interface API function jump table in @phba
4355 * struct.
4356 * Returns: 0 - success, -ENODEV - failure.
4357 **/
4358int
4359lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4360{
4361
James Smartf1126682009-06-10 17:22:44 -04004362 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
4363 phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
James Smartf1126682009-06-10 17:22:44 -04004364
James Smart3772a992009-05-22 14:50:54 -04004365 switch (dev_grp) {
4366 case LPFC_PCI_DEV_LP:
4367 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s3;
4368 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
James Smartacd68592012-01-18 16:25:09 -05004369 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04004370 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
James Smart19ca7602010-11-20 23:11:55 -05004371 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04004372 break;
James Smartda0436e2009-05-22 14:51:39 -04004373 case LPFC_PCI_DEV_OC:
4374 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s4;
4375 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
James Smartacd68592012-01-18 16:25:09 -05004376 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04004377 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
James Smart19ca7602010-11-20 23:11:55 -05004378 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04004379 break;
James Smart3772a992009-05-22 14:50:54 -04004380 default:
4381 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4382 "1418 Invalid HBA PCI-device group: 0x%x\n",
4383 dev_grp);
4384 return -ENODEV;
4385 break;
4386 }
James Smart3772a992009-05-22 14:50:54 -04004387 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
James Smart84d1b002010-02-12 14:42:33 -05004388 phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
James Smart3772a992009-05-22 14:50:54 -04004389 return 0;
4390}
4391
4392/**
James Smart3621a712009-04-06 18:47:14 -04004393 * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
James Smart9bad7672008-12-04 22:39:02 -05004394 * @phba: The Hba for which this call is being executed.
4395 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
4396 * @rspiocbq: Pointer to lpfc_iocbq data structure.
4397 *
4398 * This routine is IOCB completion routine for device reset and target reset
4399 * routine. This routine release scsi buffer associated with lpfc_cmd.
4400 **/
James Smart7054a602007-04-25 09:52:34 -04004401static void
4402lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
4403 struct lpfc_iocbq *cmdiocbq,
4404 struct lpfc_iocbq *rspiocbq)
4405{
4406 struct lpfc_scsi_buf *lpfc_cmd =
4407 (struct lpfc_scsi_buf *) cmdiocbq->context1;
4408 if (lpfc_cmd)
4409 lpfc_release_scsi_buf(phba, lpfc_cmd);
4410 return;
4411}
4412
James Smart9bad7672008-12-04 22:39:02 -05004413/**
James Smart3621a712009-04-06 18:47:14 -04004414 * lpfc_info - Info entry point of scsi_host_template data structure
James Smart9bad7672008-12-04 22:39:02 -05004415 * @host: The scsi host for which this call is being executed.
4416 *
4417 * This routine provides module information about hba.
4418 *
4419 * Reutrn code:
4420 * Pointer to char - Success.
4421 **/
dea31012005-04-17 16:05:31 -05004422const char *
4423lpfc_info(struct Scsi_Host *host)
4424{
James Smart2e0fef82007-06-17 19:56:36 -05004425 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
4426 struct lpfc_hba *phba = vport->phba;
James Smart8b68cd52012-09-29 11:32:37 -04004427 int len, link_speed = 0;
dea31012005-04-17 16:05:31 -05004428 static char lpfcinfobuf[384];
4429
4430 memset(lpfcinfobuf,0,384);
4431 if (phba && phba->pcidev){
4432 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
4433 len = strlen(lpfcinfobuf);
4434 snprintf(lpfcinfobuf + len,
4435 384-len,
4436 " on PCI bus %02x device %02x irq %d",
4437 phba->pcidev->bus->number,
4438 phba->pcidev->devfn,
4439 phba->pcidev->irq);
4440 len = strlen(lpfcinfobuf);
4441 if (phba->Port[0]) {
4442 snprintf(lpfcinfobuf + len,
4443 384-len,
4444 " port %s",
4445 phba->Port);
4446 }
James Smart65467b62010-01-26 23:08:29 -05004447 len = strlen(lpfcinfobuf);
James Smart8b68cd52012-09-29 11:32:37 -04004448 if (phba->sli_rev <= LPFC_SLI_REV3) {
4449 link_speed = lpfc_sli_port_speed_get(phba);
4450 } else {
4451 if (phba->sli4_hba.link_state.logical_speed)
4452 link_speed =
4453 phba->sli4_hba.link_state.logical_speed;
4454 else
4455 link_speed = phba->sli4_hba.link_state.speed;
James Smart65467b62010-01-26 23:08:29 -05004456 }
James Smart8b68cd52012-09-29 11:32:37 -04004457 if (link_speed != 0)
4458 snprintf(lpfcinfobuf + len, 384-len,
4459 " Logical Link Speed: %d Mbps", link_speed);
dea31012005-04-17 16:05:31 -05004460 }
4461 return lpfcinfobuf;
4462}
4463
James Smart9bad7672008-12-04 22:39:02 -05004464/**
James Smart3621a712009-04-06 18:47:14 -04004465 * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
James Smart9bad7672008-12-04 22:39:02 -05004466 * @phba: The Hba for which this call is being executed.
4467 *
4468 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
4469 * The default value of cfg_poll_tmo is 10 milliseconds.
4470 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004471static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
4472{
4473 unsigned long poll_tmo_expires =
4474 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
4475
James Smart0e9bb8d2013-03-01 16:35:12 -05004476 if (!list_empty(&phba->sli.ring[LPFC_FCP_RING].txcmplq))
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004477 mod_timer(&phba->fcp_poll_timer,
4478 poll_tmo_expires);
4479}
4480
James Smart9bad7672008-12-04 22:39:02 -05004481/**
James Smart3621a712009-04-06 18:47:14 -04004482 * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
James Smart9bad7672008-12-04 22:39:02 -05004483 * @phba: The Hba for which this call is being executed.
4484 *
4485 * This routine starts the fcp_poll_timer of @phba.
4486 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004487void lpfc_poll_start_timer(struct lpfc_hba * phba)
4488{
4489 lpfc_poll_rearm_timer(phba);
4490}
4491
James Smart9bad7672008-12-04 22:39:02 -05004492/**
James Smart3621a712009-04-06 18:47:14 -04004493 * lpfc_poll_timeout - Restart polling timer
James Smart9bad7672008-12-04 22:39:02 -05004494 * @ptr: Map to lpfc_hba data structure pointer.
4495 *
4496 * This routine restarts fcp_poll timer, when FCP ring polling is enable
4497 * and FCP Ring interrupt is disable.
4498 **/
4499
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004500void lpfc_poll_timeout(unsigned long ptr)
4501{
James Smart2e0fef82007-06-17 19:56:36 -05004502 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004503
4504 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004505 lpfc_sli_handle_fast_ring_event(phba,
4506 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4507
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004508 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4509 lpfc_poll_rearm_timer(phba);
4510 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004511}
4512
James Smart9bad7672008-12-04 22:39:02 -05004513/**
James Smart3621a712009-04-06 18:47:14 -04004514 * lpfc_queuecommand - scsi_host_template queuecommand entry point
James Smart9bad7672008-12-04 22:39:02 -05004515 * @cmnd: Pointer to scsi_cmnd data structure.
4516 * @done: Pointer to done routine.
4517 *
4518 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
4519 * This routine prepares an IOCB from scsi command and provides to firmware.
4520 * The @done callback is invoked after driver finished processing the command.
4521 *
4522 * Return value :
4523 * 0 - Success
4524 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
4525 **/
dea31012005-04-17 16:05:31 -05004526static int
James Smartb9a7c632012-08-03 12:35:24 -04004527lpfc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004528{
James Smart2e0fef82007-06-17 19:56:36 -05004529 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4530 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004531 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05004532 struct lpfc_nodelist *ndlp;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004533 struct lpfc_scsi_buf *lpfc_cmd;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004534 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004535 int err;
dea31012005-04-17 16:05:31 -05004536
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004537 err = fc_remote_port_chkready(rport);
4538 if (err) {
4539 cmnd->result = err;
dea31012005-04-17 16:05:31 -05004540 goto out_fail_command;
4541 }
James Smart1c6f4ef52009-11-18 15:40:49 -05004542 ndlp = rdata->pnode;
dea31012005-04-17 16:05:31 -05004543
James Smartbf086112011-08-21 21:48:13 -04004544 if ((scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) &&
James Smartacd68592012-01-18 16:25:09 -05004545 (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED))) {
James Smarte2a0a9d2008-12-04 22:40:02 -05004546
James Smart6a9c52c2009-10-02 15:16:51 -04004547 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4548 "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
4549 " op:%02x str=%s without registering for"
4550 " BlockGuard - Rejecting command\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05004551 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
4552 dif_op_str[scsi_get_prot_op(cmnd)]);
4553 goto out_fail_command;
4554 }
4555
dea31012005-04-17 16:05:31 -05004556 /*
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004557 * Catch race where our node has transitioned, but the
4558 * transport is still transitioning.
dea31012005-04-17 16:05:31 -05004559 */
James Smart6b415f52012-06-12 13:54:59 -04004560 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
4561 goto out_tgt_busy;
James Smart7dc517d2010-07-14 15:32:10 -04004562 if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth)
Mike Christie34963432011-02-25 14:04:28 -06004563 goto out_tgt_busy;
Mike Christiea93ce022008-08-17 15:24:41 -05004564
James Smart19ca7602010-11-20 23:11:55 -05004565 lpfc_cmd = lpfc_get_scsi_buf(phba, ndlp);
dea31012005-04-17 16:05:31 -05004566 if (lpfc_cmd == NULL) {
James Smarteaf15d52008-12-04 22:39:29 -05004567 lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05004568
James Smarte8b62012007-08-02 11:10:09 -04004569 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4570 "0707 driver's buffer pool is empty, "
4571 "IO busied\n");
dea31012005-04-17 16:05:31 -05004572 goto out_host_busy;
4573 }
4574
4575 /*
4576 * Store the midlayer's command structure for the completion phase
4577 * and complete the command initialization.
4578 */
4579 lpfc_cmd->pCmd = cmnd;
4580 lpfc_cmd->rdata = rdata;
4581 lpfc_cmd->timeout = 0;
James Smart977b5a02008-09-07 11:52:04 -04004582 lpfc_cmd->start_time = jiffies;
dea31012005-04-17 16:05:31 -05004583 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
dea31012005-04-17 16:05:31 -05004584
James Smarte2a0a9d2008-12-04 22:40:02 -05004585 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
James Smart6a9c52c2009-10-02 15:16:51 -04004586 if (vport->phba->cfg_enable_bg) {
James Smart737d4242013-04-17 20:14:49 -04004587 lpfc_printf_vlog(vport,
4588 KERN_INFO, LOG_SCSI_CMD,
James Smart26134702012-08-14 14:25:50 -04004589 "9033 BLKGRD: rcvd %s cmd:x%x "
4590 "sector x%llx cnt %u pt %x\n",
4591 dif_op_str[scsi_get_prot_op(cmnd)],
4592 cmnd->cmnd[0],
4593 (unsigned long long)scsi_get_lba(cmnd),
4594 blk_rq_sectors(cmnd->request),
4595 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004596 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004597 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
4598 } else {
James Smart6a9c52c2009-10-02 15:16:51 -04004599 if (vport->phba->cfg_enable_bg) {
James Smart737d4242013-04-17 20:14:49 -04004600 lpfc_printf_vlog(vport,
4601 KERN_INFO, LOG_SCSI_CMD,
James Smart26134702012-08-14 14:25:50 -04004602 "9038 BLKGRD: rcvd PROT_NORMAL cmd: "
4603 "x%x sector x%llx cnt %u pt %x\n",
4604 cmnd->cmnd[0],
4605 (unsigned long long)scsi_get_lba(cmnd),
James Smart9a6b09c2012-03-01 22:37:42 -05004606 blk_rq_sectors(cmnd->request),
James Smart26134702012-08-14 14:25:50 -04004607 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004608 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004609 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
4610 }
4611
dea31012005-04-17 16:05:31 -05004612 if (err)
4613 goto out_host_busy_free_buf;
4614
James Smart2e0fef82007-06-17 19:56:36 -05004615 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea31012005-04-17 16:05:31 -05004616
James Smart977b5a02008-09-07 11:52:04 -04004617 atomic_inc(&ndlp->cmd_pending);
James Smart3772a992009-05-22 14:50:54 -04004618 err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
James Smart92d7f7b2007-06-17 19:56:38 -05004619 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
James Smarteaf15d52008-12-04 22:39:29 -05004620 if (err) {
4621 atomic_dec(&ndlp->cmd_pending);
dea31012005-04-17 16:05:31 -05004622 goto out_host_busy_free_buf;
James Smarteaf15d52008-12-04 22:39:29 -05004623 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004624 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004625 lpfc_sli_handle_fast_ring_event(phba,
4626 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4627
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004628 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4629 lpfc_poll_rearm_timer(phba);
4630 }
4631
dea31012005-04-17 16:05:31 -05004632 return 0;
4633
4634 out_host_busy_free_buf:
James Smartbcf4dbf2006-07-06 15:50:08 -04004635 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004636 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004637 out_host_busy:
4638 return SCSI_MLQUEUE_HOST_BUSY;
4639
Mike Christie34963432011-02-25 14:04:28 -06004640 out_tgt_busy:
4641 return SCSI_MLQUEUE_TARGET_BUSY;
4642
dea31012005-04-17 16:05:31 -05004643 out_fail_command:
James Smartb9a7c632012-08-03 12:35:24 -04004644 cmnd->scsi_done(cmnd);
dea31012005-04-17 16:05:31 -05004645 return 0;
4646}
4647
Jeff Garzikf2812332010-11-16 02:10:29 -05004648
James Smart9bad7672008-12-04 22:39:02 -05004649/**
James Smart3621a712009-04-06 18:47:14 -04004650 * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05004651 * @cmnd: Pointer to scsi_cmnd data structure.
4652 *
4653 * This routine aborts @cmnd pending in base driver.
4654 *
4655 * Return code :
4656 * 0x2003 - Error
4657 * 0x2002 - Success
4658 **/
dea31012005-04-17 16:05:31 -05004659static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05004660lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004661{
James Smart2e0fef82007-06-17 19:56:36 -05004662 struct Scsi_Host *shost = cmnd->device->host;
4663 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4664 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004665 struct lpfc_iocbq *iocb;
4666 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05004667 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05004668 IOCB_t *cmd, *icmd;
James Smart3a707302012-06-12 13:54:42 -04004669 int ret = SUCCESS, status = 0;
James Smart876dd7d2012-09-29 11:31:28 -04004670 unsigned long flags;
James Smartfa61a542008-01-11 01:52:42 -05004671 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004672
James Smart3a707302012-06-12 13:54:42 -04004673 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04004674 if (status != 0 && status != SUCCESS)
James Smart3a707302012-06-12 13:54:42 -04004675 return status;
James Smart4f2e66c2012-05-09 21:17:07 -04004676
James Smart876dd7d2012-09-29 11:31:28 -04004677 spin_lock_irqsave(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004678 /* driver queued commands are in process of being flushed */
4679 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
James Smart876dd7d2012-09-29 11:31:28 -04004680 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004681 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4682 "3168 SCSI Layer abort requested I/O has been "
4683 "flushed by LLD.\n");
4684 return FAILED;
4685 }
4686
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004687 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
James Smart92e3af62012-08-14 14:26:28 -04004688 if (!lpfc_cmd || !lpfc_cmd->pCmd) {
James Smart876dd7d2012-09-29 11:31:28 -04004689 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarteee88772010-09-29 11:19:08 -04004690 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4691 "2873 SCSI Layer I/O Abort Request IO CMPL Status "
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04004692 "x%x ID %d LUN %d\n",
James Smart3a707302012-06-12 13:54:42 -04004693 SUCCESS, cmnd->device->id, cmnd->device->lun);
James Smarteee88772010-09-29 11:19:08 -04004694 return SUCCESS;
4695 }
dea31012005-04-17 16:05:31 -05004696
James Smart4f2e66c2012-05-09 21:17:07 -04004697 iocb = &lpfc_cmd->cur_iocbq;
4698 /* the command is in process of being cancelled */
4699 if (!(iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
James Smart876dd7d2012-09-29 11:31:28 -04004700 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004701 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4702 "3169 SCSI Layer abort requested I/O has been "
4703 "cancelled by LLD.\n");
4704 return FAILED;
4705 }
dea31012005-04-17 16:05:31 -05004706 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004707 * If pCmd field of the corresponding lpfc_scsi_buf structure
4708 * points to a different SCSI command, then the driver has
4709 * already completed this command, but the midlayer did not
James Smart4f2e66c2012-05-09 21:17:07 -04004710 * see the completion before the eh fired. Just return SUCCESS.
dea31012005-04-17 16:05:31 -05004711 */
James Smart4f2e66c2012-05-09 21:17:07 -04004712 if (lpfc_cmd->pCmd != cmnd) {
4713 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4714 "3170 SCSI Layer abort requested I/O has been "
4715 "completed by LLD.\n");
4716 goto out_unlock;
4717 }
dea31012005-04-17 16:05:31 -05004718
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004719 BUG_ON(iocb->context1 != lpfc_cmd);
dea31012005-04-17 16:05:31 -05004720
James Smart4f2e66c2012-05-09 21:17:07 -04004721 abtsiocb = __lpfc_sli_get_iocbq(phba);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004722 if (abtsiocb == NULL) {
4723 ret = FAILED;
James Smart4f2e66c2012-05-09 21:17:07 -04004724 goto out_unlock;
dea31012005-04-17 16:05:31 -05004725 }
4726
dea31012005-04-17 16:05:31 -05004727 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004728 * The scsi command can not be in txq and it is in flight because the
4729 * pCmd is still pointig at the SCSI command we have to abort. There
4730 * is no need to search the txcmplq. Just send an abort to the FW.
dea31012005-04-17 16:05:31 -05004731 */
dea31012005-04-17 16:05:31 -05004732
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004733 cmd = &iocb->iocb;
4734 icmd = &abtsiocb->iocb;
4735 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
4736 icmd->un.acxri.abortContextTag = cmd->ulpContext;
James Smart3772a992009-05-22 14:50:54 -04004737 if (phba->sli_rev == LPFC_SLI_REV4)
4738 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
4739 else
4740 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05004741
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004742 icmd->ulpLe = 1;
4743 icmd->ulpClass = cmd->ulpClass;
James Smart5ffc2662009-11-18 15:39:44 -05004744
4745 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
4746 abtsiocb->fcp_wqidx = iocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05004747 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05004748
James Smart2e0fef82007-06-17 19:56:36 -05004749 if (lpfc_is_link_up(phba))
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004750 icmd->ulpCommand = CMD_ABORT_XRI_CN;
4751 else
4752 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea31012005-04-17 16:05:31 -05004753
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004754 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smart2e0fef82007-06-17 19:56:36 -05004755 abtsiocb->vport = vport;
James Smart4f2e66c2012-05-09 21:17:07 -04004756 /* no longer need the lock after this point */
James Smart876dd7d2012-09-29 11:31:28 -04004757 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004758
James Smart3772a992009-05-22 14:50:54 -04004759 if (lpfc_sli_issue_iocb(phba, LPFC_FCP_RING, abtsiocb, 0) ==
4760 IOCB_ERROR) {
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004761 lpfc_sli_release_iocbq(phba, abtsiocb);
4762 ret = FAILED;
4763 goto out;
4764 }
4765
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004766 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
James Smart45ed1192009-10-02 15:17:02 -04004767 lpfc_sli_handle_fast_ring_event(phba,
4768 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004769
James Smartfa61a542008-01-11 01:52:42 -05004770 lpfc_cmd->waitq = &waitq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004771 /* Wait for abort to complete */
James Smartfa61a542008-01-11 01:52:42 -05004772 wait_event_timeout(waitq,
4773 (lpfc_cmd->pCmd != cmnd),
4774 (2*vport->cfg_devloss_tmo*HZ));
James Smartfa61a542008-01-11 01:52:42 -05004775 lpfc_cmd->waitq = NULL;
dea31012005-04-17 16:05:31 -05004776
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004777 if (lpfc_cmd->pCmd == cmnd) {
4778 ret = FAILED;
James Smarte8b62012007-08-02 11:10:09 -04004779 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4780 "0748 abort handler timed out waiting "
James Smart247ca942012-08-14 14:26:13 -04004781 "for abortng I/O (xri:x%x) to complete: "
4782 "ret %#x, ID %d, LUN %d\n",
4783 iocb->sli4_xritag, ret,
4784 cmnd->device->id, cmnd->device->lun);
dea31012005-04-17 16:05:31 -05004785 }
James Smart4f2e66c2012-05-09 21:17:07 -04004786 goto out;
dea31012005-04-17 16:05:31 -05004787
James Smart4f2e66c2012-05-09 21:17:07 -04004788out_unlock:
James Smart876dd7d2012-09-29 11:31:28 -04004789 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004790out:
James Smarte8b62012007-08-02 11:10:09 -04004791 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4792 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04004793 "LUN %d\n", ret, cmnd->device->id,
4794 cmnd->device->lun);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004795 return ret;
dea31012005-04-17 16:05:31 -05004796}
4797
James Smartbbb9d182009-06-10 17:23:16 -04004798static char *
4799lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
4800{
4801 switch (task_mgmt_cmd) {
4802 case FCP_ABORT_TASK_SET:
4803 return "ABORT_TASK_SET";
4804 case FCP_CLEAR_TASK_SET:
4805 return "FCP_CLEAR_TASK_SET";
4806 case FCP_BUS_RESET:
4807 return "FCP_BUS_RESET";
4808 case FCP_LUN_RESET:
4809 return "FCP_LUN_RESET";
4810 case FCP_TARGET_RESET:
4811 return "FCP_TARGET_RESET";
4812 case FCP_CLEAR_ACA:
4813 return "FCP_CLEAR_ACA";
4814 case FCP_TERMINATE_TASK:
4815 return "FCP_TERMINATE_TASK";
4816 default:
4817 return "unknown";
4818 }
4819}
4820
4821/**
4822 * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
4823 * @vport: The virtual port for which this call is being executed.
4824 * @rdata: Pointer to remote port local data
4825 * @tgt_id: Target ID of remote device.
4826 * @lun_id: Lun number for the TMF
4827 * @task_mgmt_cmd: type of TMF to send
4828 *
4829 * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
4830 * a remote port.
4831 *
4832 * Return Code:
4833 * 0x2003 - Error
4834 * 0x2002 - Success.
4835 **/
4836static int
4837lpfc_send_taskmgmt(struct lpfc_vport *vport, struct lpfc_rport_data *rdata,
4838 unsigned tgt_id, unsigned int lun_id,
4839 uint8_t task_mgmt_cmd)
4840{
4841 struct lpfc_hba *phba = vport->phba;
4842 struct lpfc_scsi_buf *lpfc_cmd;
4843 struct lpfc_iocbq *iocbq;
4844 struct lpfc_iocbq *iocbqrsp;
James Smart5989b8d2010-10-22 11:06:56 -04004845 struct lpfc_nodelist *pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004846 int ret;
4847 int status;
4848
James Smart5989b8d2010-10-22 11:06:56 -04004849 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
James Smartbbb9d182009-06-10 17:23:16 -04004850 return FAILED;
4851
James Smart19ca7602010-11-20 23:11:55 -05004852 lpfc_cmd = lpfc_get_scsi_buf(phba, rdata->pnode);
James Smartbbb9d182009-06-10 17:23:16 -04004853 if (lpfc_cmd == NULL)
4854 return FAILED;
4855 lpfc_cmd->timeout = 60;
4856 lpfc_cmd->rdata = rdata;
4857
4858 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
4859 task_mgmt_cmd);
4860 if (!status) {
4861 lpfc_release_scsi_buf(phba, lpfc_cmd);
4862 return FAILED;
4863 }
4864
4865 iocbq = &lpfc_cmd->cur_iocbq;
4866 iocbqrsp = lpfc_sli_get_iocbq(phba);
4867 if (iocbqrsp == NULL) {
4868 lpfc_release_scsi_buf(phba, lpfc_cmd);
4869 return FAILED;
4870 }
4871
4872 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4873 "0702 Issue %s to TGT %d LUN %d "
James Smart6d368e52011-05-24 11:44:12 -04004874 "rpi x%x nlp_flag x%x Data: x%x x%x\n",
James Smartbbb9d182009-06-10 17:23:16 -04004875 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
James Smart6d368e52011-05-24 11:44:12 -04004876 pnode->nlp_rpi, pnode->nlp_flag, iocbq->sli4_xritag,
4877 iocbq->iocb_flag);
James Smartbbb9d182009-06-10 17:23:16 -04004878
4879 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
4880 iocbq, iocbqrsp, lpfc_cmd->timeout);
4881 if (status != IOCB_SUCCESS) {
4882 if (status == IOCB_TIMEDOUT) {
4883 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
4884 ret = TIMEOUT_ERROR;
4885 } else
4886 ret = FAILED;
4887 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
4888 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smart6d368e52011-05-24 11:44:12 -04004889 "0727 TMF %s to TGT %d LUN %d failed (%d, %d) "
4890 "iocb_flag x%x\n",
James Smartbbb9d182009-06-10 17:23:16 -04004891 lpfc_taskmgmt_name(task_mgmt_cmd),
4892 tgt_id, lun_id, iocbqrsp->iocb.ulpStatus,
James Smart6d368e52011-05-24 11:44:12 -04004893 iocbqrsp->iocb.un.ulpWord[4],
4894 iocbq->iocb_flag);
James Smart2a9bf3d2010-06-07 15:24:45 -04004895 } else if (status == IOCB_BUSY)
4896 ret = FAILED;
4897 else
James Smartbbb9d182009-06-10 17:23:16 -04004898 ret = SUCCESS;
4899
4900 lpfc_sli_release_iocbq(phba, iocbqrsp);
4901
4902 if (ret != TIMEOUT_ERROR)
4903 lpfc_release_scsi_buf(phba, lpfc_cmd);
4904
4905 return ret;
4906}
4907
4908/**
4909 * lpfc_chk_tgt_mapped -
4910 * @vport: The virtual port to check on
4911 * @cmnd: Pointer to scsi_cmnd data structure.
4912 *
4913 * This routine delays until the scsi target (aka rport) for the
4914 * command exists (is present and logged in) or we declare it non-existent.
4915 *
4916 * Return code :
4917 * 0x2003 - Error
4918 * 0x2002 - Success
4919 **/
4920static int
4921lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
4922{
4923 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05004924 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004925 unsigned long later;
4926
James Smart1c6f4ef52009-11-18 15:40:49 -05004927 if (!rdata) {
4928 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4929 "0797 Tgt Map rport failure: rdata x%p\n", rdata);
4930 return FAILED;
4931 }
4932 pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004933 /*
4934 * If target is not in a MAPPED state, delay until
4935 * target is rediscovered or devloss timeout expires.
4936 */
4937 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
4938 while (time_after(later, jiffies)) {
4939 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
4940 return FAILED;
4941 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
4942 return SUCCESS;
4943 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
4944 rdata = cmnd->device->hostdata;
4945 if (!rdata)
4946 return FAILED;
4947 pnode = rdata->pnode;
4948 }
4949 if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
4950 (pnode->nlp_state != NLP_STE_MAPPED_NODE))
4951 return FAILED;
4952 return SUCCESS;
4953}
4954
4955/**
4956 * lpfc_reset_flush_io_context -
4957 * @vport: The virtual port (scsi_host) for the flush context
4958 * @tgt_id: If aborting by Target contect - specifies the target id
4959 * @lun_id: If aborting by Lun context - specifies the lun id
4960 * @context: specifies the context level to flush at.
4961 *
4962 * After a reset condition via TMF, we need to flush orphaned i/o
4963 * contexts from the adapter. This routine aborts any contexts
4964 * outstanding, then waits for their completions. The wait is
4965 * bounded by devloss_tmo though.
4966 *
4967 * Return code :
4968 * 0x2003 - Error
4969 * 0x2002 - Success
4970 **/
4971static int
4972lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
4973 uint64_t lun_id, lpfc_ctx_cmd context)
4974{
4975 struct lpfc_hba *phba = vport->phba;
4976 unsigned long later;
4977 int cnt;
4978
4979 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
4980 if (cnt)
4981 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
4982 tgt_id, lun_id, context);
4983 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
4984 while (time_after(later, jiffies) && cnt) {
4985 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
4986 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
4987 }
4988 if (cnt) {
4989 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4990 "0724 I/O flush failure for context %s : cnt x%x\n",
4991 ((context == LPFC_CTX_LUN) ? "LUN" :
4992 ((context == LPFC_CTX_TGT) ? "TGT" :
4993 ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
4994 cnt);
4995 return FAILED;
4996 }
4997 return SUCCESS;
4998}
4999
James Smart9bad7672008-12-04 22:39:02 -05005000/**
James Smart3621a712009-04-06 18:47:14 -04005001 * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
James Smart9bad7672008-12-04 22:39:02 -05005002 * @cmnd: Pointer to scsi_cmnd data structure.
5003 *
James Smartbbb9d182009-06-10 17:23:16 -04005004 * This routine does a device reset by sending a LUN_RESET task management
James Smart9bad7672008-12-04 22:39:02 -05005005 * command.
5006 *
5007 * Return code :
5008 * 0x2003 - Error
James Smart3621a712009-04-06 18:47:14 -04005009 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05005010 **/
dea31012005-04-17 16:05:31 -05005011static int
James Smart7054a602007-04-25 09:52:34 -04005012lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05005013{
James Smart2e0fef82007-06-17 19:56:36 -05005014 struct Scsi_Host *shost = cmnd->device->host;
5015 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05005016 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005017 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005018 unsigned tgt_id = cmnd->device->id;
5019 unsigned int lun_id = cmnd->device->lun;
James Smartea2151b2008-09-07 11:52:10 -04005020 struct lpfc_scsi_event_header scsi_event;
James Smart3a707302012-06-12 13:54:42 -04005021 int status, ret = SUCCESS;
dea31012005-04-17 16:05:31 -05005022
James Smart1c6f4ef52009-11-18 15:40:49 -05005023 if (!rdata) {
5024 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5025 "0798 Device Reset rport failure: rdata x%p\n", rdata);
5026 return FAILED;
5027 }
5028 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04005029 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005030 if (status != 0 && status != SUCCESS)
James Smart589a52d2010-07-14 15:30:54 -04005031 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005032
5033 status = lpfc_chk_tgt_mapped(vport, cmnd);
5034 if (status == FAILED) {
5035 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5036 "0721 Device Reset rport failure: rdata x%p\n", rdata);
5037 return FAILED;
5038 }
5039
5040 scsi_event.event_type = FC_REG_SCSI_EVENT;
5041 scsi_event.subcategory = LPFC_EVENT_LUNRESET;
5042 scsi_event.lun = lun_id;
5043 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5044 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5045
5046 fc_host_post_vendor_event(shost, fc_get_event_number(),
5047 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
5048
5049 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
5050 FCP_LUN_RESET);
5051
5052 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5053 "0713 SCSI layer issued Device Reset (%d, %d) "
5054 "return x%x\n", tgt_id, lun_id, status);
5055
dea31012005-04-17 16:05:31 -05005056 /*
James Smartbbb9d182009-06-10 17:23:16 -04005057 * We have to clean up i/o as : they may be orphaned by the TMF;
5058 * or if the TMF failed, they may be in an indeterminate state.
5059 * So, continue on.
5060 * We will report success if all the i/o aborts successfully.
dea31012005-04-17 16:05:31 -05005061 */
James Smart3a707302012-06-12 13:54:42 -04005062 ret = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
James Smartbbb9d182009-06-10 17:23:16 -04005063 LPFC_CTX_LUN);
James Smart3a707302012-06-12 13:54:42 -04005064 return ret;
James Smartbbb9d182009-06-10 17:23:16 -04005065}
5066
5067/**
5068 * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
5069 * @cmnd: Pointer to scsi_cmnd data structure.
5070 *
5071 * This routine does a target reset by sending a TARGET_RESET task management
5072 * command.
5073 *
5074 * Return code :
5075 * 0x2003 - Error
5076 * 0x2002 - Success
5077 **/
5078static int
5079lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
5080{
5081 struct Scsi_Host *shost = cmnd->device->host;
5082 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5083 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005084 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005085 unsigned tgt_id = cmnd->device->id;
5086 unsigned int lun_id = cmnd->device->lun;
5087 struct lpfc_scsi_event_header scsi_event;
James Smart3a707302012-06-12 13:54:42 -04005088 int status, ret = SUCCESS;
James Smartbbb9d182009-06-10 17:23:16 -04005089
James Smart1c6f4ef52009-11-18 15:40:49 -05005090 if (!rdata) {
5091 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5092 "0799 Target Reset rport failure: rdata x%p\n", rdata);
5093 return FAILED;
5094 }
5095 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04005096 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005097 if (status != 0 && status != SUCCESS)
James Smart589a52d2010-07-14 15:30:54 -04005098 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005099
5100 status = lpfc_chk_tgt_mapped(vport, cmnd);
5101 if (status == FAILED) {
5102 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5103 "0722 Target Reset rport failure: rdata x%p\n", rdata);
5104 return FAILED;
dea31012005-04-17 16:05:31 -05005105 }
James Smartea2151b2008-09-07 11:52:10 -04005106
5107 scsi_event.event_type = FC_REG_SCSI_EVENT;
5108 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
5109 scsi_event.lun = 0;
5110 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5111 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5112
James Smartbbb9d182009-06-10 17:23:16 -04005113 fc_host_post_vendor_event(shost, fc_get_event_number(),
5114 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -04005115
James Smartbbb9d182009-06-10 17:23:16 -04005116 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
5117 FCP_TARGET_RESET);
dea31012005-04-17 16:05:31 -05005118
James Smarte8b62012007-08-02 11:10:09 -04005119 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smartbbb9d182009-06-10 17:23:16 -04005120 "0723 SCSI layer issued Target Reset (%d, %d) "
5121 "return x%x\n", tgt_id, lun_id, status);
5122
5123 /*
5124 * We have to clean up i/o as : they may be orphaned by the TMF;
5125 * or if the TMF failed, they may be in an indeterminate state.
5126 * So, continue on.
5127 * We will report success if all the i/o aborts successfully.
5128 */
James Smart3a707302012-06-12 13:54:42 -04005129 ret = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
5130 LPFC_CTX_TGT);
5131 return ret;
dea31012005-04-17 16:05:31 -05005132}
5133
James Smart9bad7672008-12-04 22:39:02 -05005134/**
James Smart3621a712009-04-06 18:47:14 -04005135 * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05005136 * @cmnd: Pointer to scsi_cmnd data structure.
5137 *
James Smartbbb9d182009-06-10 17:23:16 -04005138 * This routine does target reset to all targets on @cmnd->device->host.
5139 * This emulates Parallel SCSI Bus Reset Semantics.
James Smart9bad7672008-12-04 22:39:02 -05005140 *
James Smartbbb9d182009-06-10 17:23:16 -04005141 * Return code :
5142 * 0x2003 - Error
5143 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05005144 **/
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04005145static int
James Smart7054a602007-04-25 09:52:34 -04005146lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05005147{
James Smart2e0fef82007-06-17 19:56:36 -05005148 struct Scsi_Host *shost = cmnd->device->host;
5149 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05005150 struct lpfc_nodelist *ndlp = NULL;
James Smartea2151b2008-09-07 11:52:10 -04005151 struct lpfc_scsi_event_header scsi_event;
James Smartbbb9d182009-06-10 17:23:16 -04005152 int match;
5153 int ret = SUCCESS, status, i;
James Smartea2151b2008-09-07 11:52:10 -04005154
5155 scsi_event.event_type = FC_REG_SCSI_EVENT;
5156 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
5157 scsi_event.lun = 0;
5158 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
5159 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
5160
James Smartbbb9d182009-06-10 17:23:16 -04005161 fc_host_post_vendor_event(shost, fc_get_event_number(),
5162 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
dea31012005-04-17 16:05:31 -05005163
James Smartbf086112011-08-21 21:48:13 -04005164 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005165 if (status != 0 && status != SUCCESS)
James Smartbf086112011-08-21 21:48:13 -04005166 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005167
dea31012005-04-17 16:05:31 -05005168 /*
5169 * Since the driver manages a single bus device, reset all
5170 * targets known to the driver. Should any target reset
5171 * fail, this routine returns failure to the midlayer.
5172 */
James Smarte17da182006-07-06 15:49:25 -04005173 for (i = 0; i < LPFC_MAX_TARGET; i++) {
James Smart685f0bf2007-04-25 09:53:08 -04005174 /* Search for mapped node by target ID */
dea31012005-04-17 16:05:31 -05005175 match = 0;
James Smart2e0fef82007-06-17 19:56:36 -05005176 spin_lock_irq(shost->host_lock);
5177 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05005178 if (!NLP_CHK_NODE_ACT(ndlp))
5179 continue;
James Smarta6571c62012-10-31 14:44:42 -04005180 if (vport->phba->cfg_fcp2_no_tgt_reset &&
5181 (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE))
5182 continue;
James Smart685f0bf2007-04-25 09:53:08 -04005183 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
James Smart915caaa2008-06-14 22:52:38 -04005184 ndlp->nlp_sid == i &&
James Smart685f0bf2007-04-25 09:53:08 -04005185 ndlp->rport) {
dea31012005-04-17 16:05:31 -05005186 match = 1;
5187 break;
5188 }
5189 }
James Smart2e0fef82007-06-17 19:56:36 -05005190 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005191 if (!match)
5192 continue;
James Smartbbb9d182009-06-10 17:23:16 -04005193
5194 status = lpfc_send_taskmgmt(vport, ndlp->rport->dd_data,
5195 i, 0, FCP_TARGET_RESET);
5196
5197 if (status != SUCCESS) {
James Smarte8b62012007-08-02 11:10:09 -04005198 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5199 "0700 Bus Reset on target %d failed\n",
5200 i);
James Smart915caaa2008-06-14 22:52:38 -04005201 ret = FAILED;
dea31012005-04-17 16:05:31 -05005202 }
5203 }
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005204 /*
James Smartbbb9d182009-06-10 17:23:16 -04005205 * We have to clean up i/o as : they may be orphaned by the TMFs
5206 * above; or if any of the TMFs failed, they may be in an
5207 * indeterminate state.
5208 * We will report success if all the i/o aborts successfully.
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005209 */
James Smartbbb9d182009-06-10 17:23:16 -04005210
5211 status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
5212 if (status != SUCCESS)
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005213 ret = FAILED;
James Smartbbb9d182009-06-10 17:23:16 -04005214
James Smarte8b62012007-08-02 11:10:09 -04005215 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5216 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea31012005-04-17 16:05:31 -05005217 return ret;
5218}
5219
James Smart9bad7672008-12-04 22:39:02 -05005220/**
James Smart27b01b82012-05-09 21:19:44 -04005221 * lpfc_host_reset_handler - scsi_host_template eh_host_reset_handler entry pt
5222 * @cmnd: Pointer to scsi_cmnd data structure.
5223 *
5224 * This routine does host reset to the adaptor port. It brings the HBA
5225 * offline, performs a board restart, and then brings the board back online.
5226 * The lpfc_offline calls lpfc_sli_hba_down which will abort and local
5227 * reject all outstanding SCSI commands to the host and error returned
5228 * back to SCSI mid-level. As this will be SCSI mid-level's last resort
5229 * of error handling, it will only return error if resetting of the adapter
5230 * is not successful; in all other cases, will return success.
5231 *
5232 * Return code :
5233 * 0x2003 - Error
5234 * 0x2002 - Success
5235 **/
5236static int
5237lpfc_host_reset_handler(struct scsi_cmnd *cmnd)
5238{
5239 struct Scsi_Host *shost = cmnd->device->host;
5240 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5241 struct lpfc_hba *phba = vport->phba;
5242 int rc, ret = SUCCESS;
5243
James Smart618a5232012-06-12 13:54:36 -04005244 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart27b01b82012-05-09 21:19:44 -04005245 lpfc_offline(phba);
5246 rc = lpfc_sli_brdrestart(phba);
5247 if (rc)
5248 ret = FAILED;
5249 lpfc_online(phba);
5250 lpfc_unblock_mgmt_io(phba);
5251
5252 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
5253 "3172 SCSI layer issued Host Reset Data: x%x\n", ret);
5254 return ret;
5255}
5256
5257/**
James Smart3621a712009-04-06 18:47:14 -04005258 * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
James Smart9bad7672008-12-04 22:39:02 -05005259 * @sdev: Pointer to scsi_device.
5260 *
5261 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
5262 * globally available list of scsi buffers. This routine also makes sure scsi
5263 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
5264 * of scsi buffer exists for the lifetime of the driver.
5265 *
5266 * Return codes:
5267 * non-0 - Error
5268 * 0 - Success
5269 **/
dea31012005-04-17 16:05:31 -05005270static int
dea31012005-04-17 16:05:31 -05005271lpfc_slave_alloc(struct scsi_device *sdev)
5272{
James Smart2e0fef82007-06-17 19:56:36 -05005273 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5274 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005275 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
James Smart3772a992009-05-22 14:50:54 -04005276 uint32_t total = 0;
dea31012005-04-17 16:05:31 -05005277 uint32_t num_to_alloc = 0;
James Smart3772a992009-05-22 14:50:54 -04005278 int num_allocated = 0;
James Smartd7c47992010-06-08 18:31:54 -04005279 uint32_t sdev_cnt;
dea31012005-04-17 16:05:31 -05005280
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005281 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05005282 return -ENXIO;
5283
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005284 sdev->hostdata = rport->dd_data;
James Smartd7c47992010-06-08 18:31:54 -04005285 sdev_cnt = atomic_inc_return(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05005286
5287 /*
5288 * Populate the cmds_per_lun count scsi_bufs into this host's globally
5289 * available list of scsi buffers. Don't allocate more than the
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04005290 * HBA limit conveyed to the midlayer via the host structure. The
5291 * formula accounts for the lun_queue_depth + error handlers + 1
5292 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea31012005-04-17 16:05:31 -05005293 */
5294 total = phba->total_scsi_bufs;
James Smart3de2a652007-08-02 11:09:59 -04005295 num_to_alloc = vport->cfg_lun_queue_depth + 2;
James Smart92d7f7b2007-06-17 19:56:38 -05005296
James Smartd7c47992010-06-08 18:31:54 -04005297 /* If allocated buffers are enough do nothing */
5298 if ((sdev_cnt * (vport->cfg_lun_queue_depth + 2)) < total)
5299 return 0;
5300
James Smart92d7f7b2007-06-17 19:56:38 -05005301 /* Allow some exchanges to be available always to complete discovery */
5302 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04005303 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5304 "0704 At limitation of %d preallocated "
5305 "command buffers\n", total);
dea31012005-04-17 16:05:31 -05005306 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05005307 /* Allow some exchanges to be available always to complete discovery */
5308 } else if (total + num_to_alloc >
5309 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04005310 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5311 "0705 Allocation request of %d "
5312 "command buffers will exceed max of %d. "
5313 "Reducing allocation request to %d.\n",
5314 num_to_alloc, phba->cfg_hba_queue_depth,
5315 (phba->cfg_hba_queue_depth - total));
dea31012005-04-17 16:05:31 -05005316 num_to_alloc = phba->cfg_hba_queue_depth - total;
5317 }
James Smart3772a992009-05-22 14:50:54 -04005318 num_allocated = lpfc_new_scsi_buf(vport, num_to_alloc);
5319 if (num_to_alloc != num_allocated) {
5320 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5321 "0708 Allocation request of %d "
5322 "command buffers did not succeed. "
5323 "Allocated %d buffers.\n",
5324 num_to_alloc, num_allocated);
dea31012005-04-17 16:05:31 -05005325 }
James Smart1c6f4ef52009-11-18 15:40:49 -05005326 if (num_allocated > 0)
5327 phba->total_scsi_bufs += num_allocated;
dea31012005-04-17 16:05:31 -05005328 return 0;
5329}
5330
James Smart9bad7672008-12-04 22:39:02 -05005331/**
James Smart3621a712009-04-06 18:47:14 -04005332 * lpfc_slave_configure - scsi_host_template slave_configure entry point
James Smart9bad7672008-12-04 22:39:02 -05005333 * @sdev: Pointer to scsi_device.
5334 *
5335 * This routine configures following items
5336 * - Tag command queuing support for @sdev if supported.
James Smart9bad7672008-12-04 22:39:02 -05005337 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
5338 *
5339 * Return codes:
5340 * 0 - Success
5341 **/
dea31012005-04-17 16:05:31 -05005342static int
5343lpfc_slave_configure(struct scsi_device *sdev)
5344{
James Smart2e0fef82007-06-17 19:56:36 -05005345 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5346 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05005347
5348 if (sdev->tagged_supported)
James Smart3de2a652007-08-02 11:09:59 -04005349 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05005350 else
James Smart3de2a652007-08-02 11:09:59 -04005351 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05005352
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005353 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04005354 lpfc_sli_handle_fast_ring_event(phba,
5355 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005356 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
5357 lpfc_poll_rearm_timer(phba);
5358 }
5359
dea31012005-04-17 16:05:31 -05005360 return 0;
5361}
5362
James Smart9bad7672008-12-04 22:39:02 -05005363/**
James Smart3621a712009-04-06 18:47:14 -04005364 * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
James Smart9bad7672008-12-04 22:39:02 -05005365 * @sdev: Pointer to scsi_device.
5366 *
5367 * This routine sets @sdev hostatdata filed to null.
5368 **/
dea31012005-04-17 16:05:31 -05005369static void
5370lpfc_slave_destroy(struct scsi_device *sdev)
5371{
James Smartd7c47992010-06-08 18:31:54 -04005372 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5373 struct lpfc_hba *phba = vport->phba;
5374 atomic_dec(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05005375 sdev->hostdata = NULL;
5376 return;
5377}
5378
James Smart92d7f7b2007-06-17 19:56:38 -05005379
dea31012005-04-17 16:05:31 -05005380struct scsi_host_template lpfc_template = {
5381 .module = THIS_MODULE,
5382 .name = LPFC_DRIVER_NAME,
5383 .info = lpfc_info,
5384 .queuecommand = lpfc_queuecommand,
5385 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04005386 .eh_device_reset_handler = lpfc_device_reset_handler,
5387 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart7054a602007-04-25 09:52:34 -04005388 .eh_bus_reset_handler = lpfc_bus_reset_handler,
James Smart27b01b82012-05-09 21:19:44 -04005389 .eh_host_reset_handler = lpfc_host_reset_handler,
dea31012005-04-17 16:05:31 -05005390 .slave_alloc = lpfc_slave_alloc,
5391 .slave_configure = lpfc_slave_configure,
5392 .slave_destroy = lpfc_slave_destroy,
James Smart47a86172007-04-25 09:53:22 -04005393 .scan_finished = lpfc_scan_finished,
dea31012005-04-17 16:05:31 -05005394 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05005395 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
dea31012005-04-17 16:05:31 -05005396 .cmd_per_lun = LPFC_CMD_PER_LUN,
5397 .use_clustering = ENABLE_CLUSTERING,
James Smart2e0fef82007-06-17 19:56:36 -05005398 .shost_attrs = lpfc_hba_attrs,
James.Smart@Emulex.Com564b2962005-06-25 10:34:17 -04005399 .max_sectors = 0xFFFF,
James Smartf1c3b0f2009-07-19 10:01:32 -04005400 .vendor_id = LPFC_NL_VENDOR_ID,
James Smart5ffc2662009-11-18 15:39:44 -05005401 .change_queue_depth = lpfc_change_queue_depth,
James Smartfe8f7f92013-01-03 15:43:03 -05005402 .change_queue_type = lpfc_change_queue_type,
dea31012005-04-17 16:05:31 -05005403};
James Smart3de2a652007-08-02 11:09:59 -04005404
5405struct scsi_host_template lpfc_vport_template = {
5406 .module = THIS_MODULE,
5407 .name = LPFC_DRIVER_NAME,
5408 .info = lpfc_info,
5409 .queuecommand = lpfc_queuecommand,
5410 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04005411 .eh_device_reset_handler = lpfc_device_reset_handler,
5412 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart3de2a652007-08-02 11:09:59 -04005413 .eh_bus_reset_handler = lpfc_bus_reset_handler,
5414 .slave_alloc = lpfc_slave_alloc,
5415 .slave_configure = lpfc_slave_configure,
5416 .slave_destroy = lpfc_slave_destroy,
5417 .scan_finished = lpfc_scan_finished,
5418 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05005419 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
James Smart3de2a652007-08-02 11:09:59 -04005420 .cmd_per_lun = LPFC_CMD_PER_LUN,
5421 .use_clustering = ENABLE_CLUSTERING,
5422 .shost_attrs = lpfc_vport_attrs,
5423 .max_sectors = 0xFFFF,
James Smart5ffc2662009-11-18 15:39:44 -05005424 .change_queue_depth = lpfc_change_queue_depth,
James Smartfe8f7f92013-01-03 15:43:03 -05005425 .change_queue_type = lpfc_change_queue_type,
James Smart3de2a652007-08-02 11:09:59 -04005426};