blob: 9480c55963ea715ba44a33a40878ca29b56453f4 [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 Smart92c13f22013-05-31 17:05:45 -04004 * Copyright (C) 2004-2013 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 Smart9c6aa9d2013-05-31 17:03:39 -040075static int
76lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc);
James Smarte2a0a9d2008-12-04 22:40:02 -050077
78static void
James Smart6a9c52c2009-10-02 15:16:51 -040079lpfc_debug_save_data(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
James Smarte2a0a9d2008-12-04 22:40:02 -050080{
81 void *src, *dst;
82 struct scatterlist *sgde = scsi_sglist(cmnd);
83
84 if (!_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -040085 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
86 "9050 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
James Smarte2a0a9d2008-12-04 22:40:02 -050087 __func__);
88 return;
89 }
90
91
92 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -040093 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
94 "9051 BLKGRD: ERROR: data scatterlist is null\n");
James Smarte2a0a9d2008-12-04 22:40:02 -050095 return;
96 }
97
98 dst = (void *) _dump_buf_data;
99 while (sgde) {
100 src = sg_virt(sgde);
101 memcpy(dst, src, sgde->length);
102 dst += sgde->length;
103 sgde = sg_next(sgde);
104 }
105}
106
107static void
James Smart6a9c52c2009-10-02 15:16:51 -0400108lpfc_debug_save_dif(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
James Smarte2a0a9d2008-12-04 22:40:02 -0500109{
110 void *src, *dst;
111 struct scatterlist *sgde = scsi_prot_sglist(cmnd);
112
113 if (!_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -0400114 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
115 "9052 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
James Smarte2a0a9d2008-12-04 22:40:02 -0500116 __func__);
117 return;
118 }
119
120 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -0400121 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
122 "9053 BLKGRD: ERROR: prot scatterlist is null\n");
James Smarte2a0a9d2008-12-04 22:40:02 -0500123 return;
124 }
125
126 dst = _dump_buf_dif;
127 while (sgde) {
128 src = sg_virt(sgde);
129 memcpy(dst, src, sgde->length);
130 dst += sgde->length;
131 sgde = sg_next(sgde);
132 }
133}
134
James Smart9c6aa9d2013-05-31 17:03:39 -0400135static inline unsigned
136lpfc_cmd_blksize(struct scsi_cmnd *sc)
137{
138 return sc->device->sector_size;
139}
140
141#define LPFC_CHECK_PROTECT_GUARD 1
142#define LPFC_CHECK_PROTECT_REF 2
143static inline unsigned
144lpfc_cmd_protect(struct scsi_cmnd *sc, int flag)
145{
146 return 1;
147}
148
149static inline unsigned
150lpfc_cmd_guard_csum(struct scsi_cmnd *sc)
151{
152 if (lpfc_prot_group_type(NULL, sc) == LPFC_PG_TYPE_NO_DIF)
153 return 0;
154 if (scsi_host_get_guard(sc->device->host) == SHOST_DIX_GUARD_IP)
155 return 1;
156 return 0;
157}
158
James Smartea2151b2008-09-07 11:52:10 -0400159/**
James Smartf1126682009-06-10 17:22:44 -0400160 * lpfc_sli4_set_rsp_sgl_last - Set the last bit in the response sge.
161 * @phba: Pointer to HBA object.
162 * @lpfc_cmd: lpfc scsi command object pointer.
163 *
164 * This function is called from the lpfc_prep_task_mgmt_cmd function to
165 * set the last bit in the response sge entry.
166 **/
167static void
168lpfc_sli4_set_rsp_sgl_last(struct lpfc_hba *phba,
169 struct lpfc_scsi_buf *lpfc_cmd)
170{
171 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
172 if (sgl) {
173 sgl += 1;
174 sgl->word2 = le32_to_cpu(sgl->word2);
175 bf_set(lpfc_sli4_sge_last, sgl, 1);
176 sgl->word2 = cpu_to_le32(sgl->word2);
177 }
178}
179
180/**
James Smart3621a712009-04-06 18:47:14 -0400181 * lpfc_update_stats - Update statistical data for the command completion
James Smartea2151b2008-09-07 11:52:10 -0400182 * @phba: Pointer to HBA object.
183 * @lpfc_cmd: lpfc scsi command object pointer.
184 *
185 * This function is called when there is a command completion and this
186 * function updates the statistical data for the command completion.
187 **/
188static void
189lpfc_update_stats(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
190{
191 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
192 struct lpfc_nodelist *pnode = rdata->pnode;
193 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
194 unsigned long flags;
195 struct Scsi_Host *shost = cmd->device->host;
196 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
197 unsigned long latency;
198 int i;
199
200 if (cmd->result)
201 return;
202
James Smart9f1e1b52008-12-04 22:39:40 -0500203 latency = jiffies_to_msecs((long)jiffies - (long)lpfc_cmd->start_time);
204
James Smartea2151b2008-09-07 11:52:10 -0400205 spin_lock_irqsave(shost->host_lock, flags);
206 if (!vport->stat_data_enabled ||
207 vport->stat_data_blocked ||
James Smart5989b8d2010-10-22 11:06:56 -0400208 !pnode ||
James Smartea2151b2008-09-07 11:52:10 -0400209 !pnode->lat_data ||
210 (phba->bucket_type == LPFC_NO_BUCKET)) {
211 spin_unlock_irqrestore(shost->host_lock, flags);
212 return;
213 }
James Smartea2151b2008-09-07 11:52:10 -0400214
215 if (phba->bucket_type == LPFC_LINEAR_BUCKET) {
216 i = (latency + phba->bucket_step - 1 - phba->bucket_base)/
217 phba->bucket_step;
James Smart9f1e1b52008-12-04 22:39:40 -0500218 /* check array subscript bounds */
219 if (i < 0)
220 i = 0;
221 else if (i >= LPFC_MAX_BUCKET_COUNT)
222 i = LPFC_MAX_BUCKET_COUNT - 1;
James Smartea2151b2008-09-07 11:52:10 -0400223 } else {
224 for (i = 0; i < LPFC_MAX_BUCKET_COUNT-1; i++)
225 if (latency <= (phba->bucket_base +
226 ((1<<i)*phba->bucket_step)))
227 break;
228 }
229
230 pnode->lat_data[i].cmd_count++;
231 spin_unlock_irqrestore(shost->host_lock, flags);
232}
233
James Smartea2151b2008-09-07 11:52:10 -0400234/**
James Smart3621a712009-04-06 18:47:14 -0400235 * lpfc_send_sdev_queuedepth_change_event - Posts a queuedepth change event
James Smartea2151b2008-09-07 11:52:10 -0400236 * @phba: Pointer to HBA context object.
237 * @vport: Pointer to vport object.
238 * @ndlp: Pointer to FC node associated with the target.
239 * @lun: Lun number of the scsi device.
240 * @old_val: Old value of the queue depth.
241 * @new_val: New value of the queue depth.
242 *
243 * This function sends an event to the mgmt application indicating
244 * there is a change in the scsi device queue depth.
245 **/
246static void
247lpfc_send_sdev_queuedepth_change_event(struct lpfc_hba *phba,
248 struct lpfc_vport *vport,
249 struct lpfc_nodelist *ndlp,
250 uint32_t lun,
251 uint32_t old_val,
252 uint32_t new_val)
253{
254 struct lpfc_fast_path_event *fast_path_evt;
255 unsigned long flags;
256
257 fast_path_evt = lpfc_alloc_fast_evt(phba);
258 if (!fast_path_evt)
259 return;
260
261 fast_path_evt->un.queue_depth_evt.scsi_event.event_type =
262 FC_REG_SCSI_EVENT;
263 fast_path_evt->un.queue_depth_evt.scsi_event.subcategory =
264 LPFC_EVENT_VARQUEDEPTH;
265
266 /* Report all luns with change in queue depth */
267 fast_path_evt->un.queue_depth_evt.scsi_event.lun = lun;
268 if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
269 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwpn,
270 &ndlp->nlp_portname, sizeof(struct lpfc_name));
271 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwnn,
272 &ndlp->nlp_nodename, sizeof(struct lpfc_name));
273 }
274
275 fast_path_evt->un.queue_depth_evt.oldval = old_val;
276 fast_path_evt->un.queue_depth_evt.newval = new_val;
277 fast_path_evt->vport = vport;
278
279 fast_path_evt->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
280 spin_lock_irqsave(&phba->hbalock, flags);
281 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
282 spin_unlock_irqrestore(&phba->hbalock, flags);
283 lpfc_worker_wake_up(phba);
284
285 return;
286}
287
James Smart9bad7672008-12-04 22:39:02 -0500288/**
James Smart5ffc2662009-11-18 15:39:44 -0500289 * lpfc_change_queue_depth - Alter scsi device queue depth
290 * @sdev: Pointer the scsi device on which to change the queue depth.
291 * @qdepth: New queue depth to set the sdev to.
292 * @reason: The reason for the queue depth change.
293 *
294 * This function is called by the midlayer and the LLD to alter the queue
295 * depth for a scsi device. This function sets the queue depth to the new
296 * value and sends an event out to log the queue depth change.
297 **/
298int
299lpfc_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
300{
301 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
302 struct lpfc_hba *phba = vport->phba;
303 struct lpfc_rport_data *rdata;
304 unsigned long new_queue_depth, old_queue_depth;
305
306 old_queue_depth = sdev->queue_depth;
307 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
308 new_queue_depth = sdev->queue_depth;
309 rdata = sdev->hostdata;
310 if (rdata)
311 lpfc_send_sdev_queuedepth_change_event(phba, vport,
312 rdata->pnode, sdev->lun,
313 old_queue_depth,
314 new_queue_depth);
315 return sdev->queue_depth;
316}
317
318/**
James Smartfe8f7f92013-01-03 15:43:03 -0500319 * lpfc_change_queue_type() - Change a device's scsi tag queuing type
320 * @sdev: Pointer the scsi device whose queue depth is to change
321 * @tag_type: Identifier for queue tag type
322 */
323static int
324lpfc_change_queue_type(struct scsi_device *sdev, int tag_type)
325{
326 if (sdev->tagged_supported) {
327 scsi_set_tag_type(sdev, tag_type);
328 if (tag_type)
329 scsi_activate_tcq(sdev, sdev->queue_depth);
330 else
331 scsi_deactivate_tcq(sdev, sdev->queue_depth);
332 } else
333 tag_type = 0;
334
335 return tag_type;
336}
337
338/**
James Smart3621a712009-04-06 18:47:14 -0400339 * lpfc_rampdown_queue_depth - Post RAMP_DOWN_QUEUE event to worker thread
James Smart9bad7672008-12-04 22:39:02 -0500340 * @phba: The Hba for which this call is being executed.
341 *
342 * This routine is called when there is resource error in driver or firmware.
343 * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
344 * posts at most 1 event each second. This routine wakes up worker thread of
345 * @phba to process WORKER_RAM_DOWN_EVENT event.
346 *
347 * This routine should be called with no lock held.
348 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500349void
James Smarteaf15d52008-12-04 22:39:29 -0500350lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
James Smart92d7f7b2007-06-17 19:56:38 -0500351{
352 unsigned long flags;
James Smart5e9d9b82008-06-14 22:52:53 -0400353 uint32_t evt_posted;
James Smart92d7f7b2007-06-17 19:56:38 -0500354
355 spin_lock_irqsave(&phba->hbalock, flags);
356 atomic_inc(&phba->num_rsrc_err);
357 phba->last_rsrc_error_time = jiffies;
358
359 if ((phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL) > jiffies) {
360 spin_unlock_irqrestore(&phba->hbalock, flags);
361 return;
362 }
363
364 phba->last_ramp_down_time = jiffies;
365
366 spin_unlock_irqrestore(&phba->hbalock, flags);
367
368 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400369 evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
370 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500371 phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500372 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
373
James Smart5e9d9b82008-06-14 22:52:53 -0400374 if (!evt_posted)
375 lpfc_worker_wake_up(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500376 return;
377}
378
James Smart9bad7672008-12-04 22:39:02 -0500379/**
James Smart3621a712009-04-06 18:47:14 -0400380 * lpfc_rampup_queue_depth - Post RAMP_UP_QUEUE event for worker thread
James Smart9bad7672008-12-04 22:39:02 -0500381 * @phba: The Hba for which this call is being executed.
382 *
383 * This routine post WORKER_RAMP_UP_QUEUE event for @phba vport. This routine
384 * post at most 1 event every 5 minute after last_ramp_up_time or
385 * last_rsrc_error_time. This routine wakes up worker thread of @phba
386 * to process WORKER_RAM_DOWN_EVENT event.
387 *
388 * This routine should be called with no lock held.
389 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500390static inline void
James Smart3de2a652007-08-02 11:09:59 -0400391lpfc_rampup_queue_depth(struct lpfc_vport *vport,
James Smarta257bf92009-04-06 18:48:10 -0400392 uint32_t queue_depth)
James Smart92d7f7b2007-06-17 19:56:38 -0500393{
394 unsigned long flags;
James Smart3de2a652007-08-02 11:09:59 -0400395 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -0400396 uint32_t evt_posted;
James Smart92d7f7b2007-06-17 19:56:38 -0500397 atomic_inc(&phba->num_cmd_success);
398
James Smarta257bf92009-04-06 18:48:10 -0400399 if (vport->cfg_lun_queue_depth <= queue_depth)
James Smart92d7f7b2007-06-17 19:56:38 -0500400 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500401 spin_lock_irqsave(&phba->hbalock, flags);
James Smart5ffc2662009-11-18 15:39:44 -0500402 if (time_before(jiffies,
403 phba->last_ramp_up_time + QUEUE_RAMP_UP_INTERVAL) ||
404 time_before(jiffies,
405 phba->last_rsrc_error_time + QUEUE_RAMP_UP_INTERVAL)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500406 spin_unlock_irqrestore(&phba->hbalock, flags);
407 return;
408 }
James Smart92d7f7b2007-06-17 19:56:38 -0500409 phba->last_ramp_up_time = jiffies;
410 spin_unlock_irqrestore(&phba->hbalock, flags);
411
412 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400413 evt_posted = phba->pport->work_port_events & WORKER_RAMP_UP_QUEUE;
414 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500415 phba->pport->work_port_events |= WORKER_RAMP_UP_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500416 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
417
James Smart5e9d9b82008-06-14 22:52:53 -0400418 if (!evt_posted)
419 lpfc_worker_wake_up(phba);
420 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500421}
422
James Smart9bad7672008-12-04 22:39:02 -0500423/**
James Smart3621a712009-04-06 18:47:14 -0400424 * lpfc_ramp_down_queue_handler - WORKER_RAMP_DOWN_QUEUE event handler
James Smart9bad7672008-12-04 22:39:02 -0500425 * @phba: The Hba for which this call is being executed.
426 *
427 * This routine is called to process WORKER_RAMP_DOWN_QUEUE event for worker
428 * thread.This routine reduces queue depth for all scsi device on each vport
429 * associated with @phba.
430 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500431void
432lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
433{
James Smart549e55c2007-08-02 11:09:51 -0400434 struct lpfc_vport **vports;
435 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500436 struct scsi_device *sdev;
James Smart5ffc2662009-11-18 15:39:44 -0500437 unsigned long new_queue_depth;
James Smart92d7f7b2007-06-17 19:56:38 -0500438 unsigned long num_rsrc_err, num_cmd_success;
James Smart549e55c2007-08-02 11:09:51 -0400439 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500440
441 num_rsrc_err = atomic_read(&phba->num_rsrc_err);
442 num_cmd_success = atomic_read(&phba->num_cmd_success);
443
James Smart75ad83a2012-05-09 21:18:40 -0400444 /*
445 * The error and success command counters are global per
446 * driver instance. If another handler has already
447 * operated on this error event, just exit.
448 */
449 if (num_rsrc_err == 0)
450 return;
451
James Smart549e55c2007-08-02 11:09:51 -0400452 vports = lpfc_create_vport_work_array(phba);
453 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400454 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400455 shost = lpfc_shost_from_vport(vports[i]);
456 shost_for_each_device(sdev, shost) {
James Smart92d7f7b2007-06-17 19:56:38 -0500457 new_queue_depth =
James Smart549e55c2007-08-02 11:09:51 -0400458 sdev->queue_depth * num_rsrc_err /
459 (num_rsrc_err + num_cmd_success);
460 if (!new_queue_depth)
461 new_queue_depth = sdev->queue_depth - 1;
462 else
463 new_queue_depth = sdev->queue_depth -
464 new_queue_depth;
James Smart5ffc2662009-11-18 15:39:44 -0500465 lpfc_change_queue_depth(sdev, new_queue_depth,
466 SCSI_QDEPTH_DEFAULT);
James Smart549e55c2007-08-02 11:09:51 -0400467 }
James Smart92d7f7b2007-06-17 19:56:38 -0500468 }
James Smart09372822008-01-11 01:52:54 -0500469 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500470 atomic_set(&phba->num_rsrc_err, 0);
471 atomic_set(&phba->num_cmd_success, 0);
472}
473
James Smart9bad7672008-12-04 22:39:02 -0500474/**
James Smart3621a712009-04-06 18:47:14 -0400475 * lpfc_ramp_up_queue_handler - WORKER_RAMP_UP_QUEUE event handler
James Smart9bad7672008-12-04 22:39:02 -0500476 * @phba: The Hba for which this call is being executed.
477 *
478 * This routine is called to process WORKER_RAMP_UP_QUEUE event for worker
479 * thread.This routine increases queue depth for all scsi device on each vport
480 * associated with @phba by 1. This routine also sets @phba num_rsrc_err and
481 * num_cmd_success to zero.
482 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500483void
484lpfc_ramp_up_queue_handler(struct lpfc_hba *phba)
485{
James Smart549e55c2007-08-02 11:09:51 -0400486 struct lpfc_vport **vports;
487 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500488 struct scsi_device *sdev;
James Smart549e55c2007-08-02 11:09:51 -0400489 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500490
James Smart549e55c2007-08-02 11:09:51 -0400491 vports = lpfc_create_vport_work_array(phba);
492 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400493 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400494 shost = lpfc_shost_from_vport(vports[i]);
495 shost_for_each_device(sdev, shost) {
James Smart97eab632008-04-07 10:16:05 -0400496 if (vports[i]->cfg_lun_queue_depth <=
497 sdev->queue_depth)
498 continue;
James Smart5ffc2662009-11-18 15:39:44 -0500499 lpfc_change_queue_depth(sdev,
500 sdev->queue_depth+1,
501 SCSI_QDEPTH_RAMP_UP);
James Smart549e55c2007-08-02 11:09:51 -0400502 }
James Smart92d7f7b2007-06-17 19:56:38 -0500503 }
James Smart09372822008-01-11 01:52:54 -0500504 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500505 atomic_set(&phba->num_rsrc_err, 0);
506 atomic_set(&phba->num_cmd_success, 0);
507}
508
James Smarta8e497d2008-08-24 21:50:11 -0400509/**
James Smart3621a712009-04-06 18:47:14 -0400510 * lpfc_scsi_dev_block - set all scsi hosts to block state
James Smarta8e497d2008-08-24 21:50:11 -0400511 * @phba: Pointer to HBA context object.
512 *
513 * This function walks vport list and set each SCSI host to block state
514 * by invoking fc_remote_port_delete() routine. This function is invoked
515 * with EEH when device's PCI slot has been permanently disabled.
516 **/
517void
518lpfc_scsi_dev_block(struct lpfc_hba *phba)
519{
520 struct lpfc_vport **vports;
521 struct Scsi_Host *shost;
522 struct scsi_device *sdev;
523 struct fc_rport *rport;
524 int i;
525
526 vports = lpfc_create_vport_work_array(phba);
527 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400528 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8e497d2008-08-24 21:50:11 -0400529 shost = lpfc_shost_from_vport(vports[i]);
530 shost_for_each_device(sdev, shost) {
531 rport = starget_to_rport(scsi_target(sdev));
532 fc_remote_port_delete(rport);
533 }
534 }
535 lpfc_destroy_vport_work_array(phba, vports);
536}
537
James Smart9bad7672008-12-04 22:39:02 -0500538/**
James Smart3772a992009-05-22 14:50:54 -0400539 * lpfc_new_scsi_buf_s3 - Scsi buffer allocator for HBA with SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -0500540 * @vport: The virtual port for which this call being executed.
James Smart3772a992009-05-22 14:50:54 -0400541 * @num_to_allocate: The requested number of buffers to allocate.
James Smart9bad7672008-12-04 22:39:02 -0500542 *
James Smart3772a992009-05-22 14:50:54 -0400543 * This routine allocates a scsi buffer for device with SLI-3 interface spec,
544 * the scsi buffer contains all the necessary information needed to initiate
545 * a SCSI I/O. The non-DMAable buffer region contains information to build
546 * the IOCB. The DMAable region contains memory for the FCP CMND, FCP RSP,
547 * and the initial BPL. In addition to allocating memory, the FCP CMND and
548 * FCP RSP BDEs are setup in the BPL and the BPL BDE is setup in the IOCB.
James Smart9bad7672008-12-04 22:39:02 -0500549 *
550 * Return codes:
James Smart3772a992009-05-22 14:50:54 -0400551 * int - number of scsi buffers that were allocated.
552 * 0 = failure, less than num_to_alloc is a partial failure.
James Smart9bad7672008-12-04 22:39:02 -0500553 **/
James Smart3772a992009-05-22 14:50:54 -0400554static int
555lpfc_new_scsi_buf_s3(struct lpfc_vport *vport, int num_to_alloc)
dea31012005-04-17 16:05:31 -0500556{
James Smart2e0fef82007-06-17 19:56:36 -0500557 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500558 struct lpfc_scsi_buf *psb;
559 struct ulp_bde64 *bpl;
560 IOCB_t *iocb;
James Smart34b02dc2008-08-24 21:49:55 -0400561 dma_addr_t pdma_phys_fcp_cmd;
562 dma_addr_t pdma_phys_fcp_rsp;
563 dma_addr_t pdma_phys_bpl;
James Bottomley604a3e32005-10-29 10:28:33 -0500564 uint16_t iotag;
James Smart96f70772013-04-17 20:16:15 -0400565 int bcnt, bpl_size;
566
567 bpl_size = phba->cfg_sg_dma_buf_size -
568 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
569
570 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
571 "9067 ALLOC %d scsi_bufs: %d (%d + %d + %d)\n",
572 num_to_alloc, phba->cfg_sg_dma_buf_size,
573 (int)sizeof(struct fcp_cmnd),
574 (int)sizeof(struct fcp_rsp), bpl_size);
dea31012005-04-17 16:05:31 -0500575
James Smart3772a992009-05-22 14:50:54 -0400576 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
577 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
578 if (!psb)
579 break;
dea31012005-04-17 16:05:31 -0500580
James Smart3772a992009-05-22 14:50:54 -0400581 /*
582 * Get memory from the pci pool to map the virt space to pci
583 * bus space for an I/O. The DMA buffer includes space for the
584 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
585 * necessary to support the sg_tablesize.
586 */
587 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
588 GFP_KERNEL, &psb->dma_handle);
589 if (!psb->data) {
590 kfree(psb);
591 break;
592 }
dea31012005-04-17 16:05:31 -0500593
James Smart3772a992009-05-22 14:50:54 -0400594 /* Initialize virtual ptrs to dma_buf region. */
595 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
dea31012005-04-17 16:05:31 -0500596
James Smart3772a992009-05-22 14:50:54 -0400597 /* Allocate iotag for psb->cur_iocbq. */
598 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
599 if (iotag == 0) {
600 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
601 psb->data, psb->dma_handle);
602 kfree(psb);
603 break;
604 }
605 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Bottomley604a3e32005-10-29 10:28:33 -0500606
James Smart3772a992009-05-22 14:50:54 -0400607 psb->fcp_cmnd = psb->data;
608 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
609 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
James Smart34b02dc2008-08-24 21:49:55 -0400610 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500611
James Smart3772a992009-05-22 14:50:54 -0400612 /* Initialize local short-hand pointers. */
613 bpl = psb->fcp_bpl;
614 pdma_phys_fcp_cmd = psb->dma_handle;
615 pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
616 pdma_phys_bpl = psb->dma_handle + sizeof(struct fcp_cmnd) +
617 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500618
James Smart3772a992009-05-22 14:50:54 -0400619 /*
620 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
621 * are sg list bdes. Initialize the first two and leave the
622 * rest for queuecommand.
623 */
624 bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
625 bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
626 bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
627 bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
628 bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
dea31012005-04-17 16:05:31 -0500629
James Smart3772a992009-05-22 14:50:54 -0400630 /* Setup the physical region for the FCP RSP */
631 bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
632 bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
633 bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
634 bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
635 bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
636
637 /*
638 * Since the IOCB for the FCP I/O is built into this
639 * lpfc_scsi_buf, initialize it with all known data now.
640 */
641 iocb = &psb->cur_iocbq.iocb;
642 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
643 if ((phba->sli_rev == 3) &&
644 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
645 /* fill in immediate fcp command BDE */
646 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
647 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
648 iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
649 unsli3.fcp_ext.icd);
650 iocb->un.fcpi64.bdl.addrHigh = 0;
651 iocb->ulpBdeCount = 0;
652 iocb->ulpLe = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300653 /* fill in response BDE */
James Smart3772a992009-05-22 14:50:54 -0400654 iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags =
655 BUFF_TYPE_BDE_64;
656 iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
657 sizeof(struct fcp_rsp);
658 iocb->unsli3.fcp_ext.rbde.addrLow =
659 putPaddrLow(pdma_phys_fcp_rsp);
660 iocb->unsli3.fcp_ext.rbde.addrHigh =
661 putPaddrHigh(pdma_phys_fcp_rsp);
662 } else {
663 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
664 iocb->un.fcpi64.bdl.bdeSize =
665 (2 * sizeof(struct ulp_bde64));
666 iocb->un.fcpi64.bdl.addrLow =
667 putPaddrLow(pdma_phys_bpl);
668 iocb->un.fcpi64.bdl.addrHigh =
669 putPaddrHigh(pdma_phys_bpl);
670 iocb->ulpBdeCount = 1;
671 iocb->ulpLe = 1;
672 }
673 iocb->ulpClass = CLASS3;
674 psb->status = IOSTAT_SUCCESS;
James Smartda0436e2009-05-22 14:51:39 -0400675 /* Put it back into the SCSI buffer list */
James Smarteee88772010-09-29 11:19:08 -0400676 psb->cur_iocbq.context1 = psb;
James Smart1c6f4ef52009-11-18 15:40:49 -0500677 lpfc_release_scsi_buf_s3(phba, psb);
James Smart3772a992009-05-22 14:50:54 -0400678
James Smart34b02dc2008-08-24 21:49:55 -0400679 }
dea31012005-04-17 16:05:31 -0500680
James Smart3772a992009-05-22 14:50:54 -0400681 return bcnt;
dea31012005-04-17 16:05:31 -0500682}
683
James Smart9bad7672008-12-04 22:39:02 -0500684/**
James Smart1151e3e2011-02-16 12:39:35 -0500685 * lpfc_sli4_vport_delete_fcp_xri_aborted -Remove all ndlp references for vport
686 * @vport: pointer to lpfc vport data structure.
687 *
688 * This routine is invoked by the vport cleanup for deletions and the cleanup
689 * for an ndlp on removal.
690 **/
691void
692lpfc_sli4_vport_delete_fcp_xri_aborted(struct lpfc_vport *vport)
693{
694 struct lpfc_hba *phba = vport->phba;
695 struct lpfc_scsi_buf *psb, *next_psb;
696 unsigned long iflag = 0;
697
698 spin_lock_irqsave(&phba->hbalock, iflag);
699 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
700 list_for_each_entry_safe(psb, next_psb,
701 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
702 if (psb->rdata && psb->rdata->pnode
703 && psb->rdata->pnode->vport == vport)
704 psb->rdata = NULL;
705 }
706 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
707 spin_unlock_irqrestore(&phba->hbalock, iflag);
708}
709
710/**
James Smartda0436e2009-05-22 14:51:39 -0400711 * lpfc_sli4_fcp_xri_aborted - Fast-path process of fcp xri abort
712 * @phba: pointer to lpfc hba data structure.
713 * @axri: pointer to the fcp xri abort wcqe structure.
714 *
715 * This routine is invoked by the worker thread to process a SLI4 fast-path
716 * FCP aborted xri.
717 **/
718void
719lpfc_sli4_fcp_xri_aborted(struct lpfc_hba *phba,
720 struct sli4_wcqe_xri_aborted *axri)
721{
722 uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
James Smart19ca7602010-11-20 23:11:55 -0500723 uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
James Smartda0436e2009-05-22 14:51:39 -0400724 struct lpfc_scsi_buf *psb, *next_psb;
725 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -0500726 struct lpfc_iocbq *iocbq;
727 int i;
James Smart19ca7602010-11-20 23:11:55 -0500728 struct lpfc_nodelist *ndlp;
729 int rrq_empty = 0;
James Smart589a52d2010-07-14 15:30:54 -0400730 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smartda0436e2009-05-22 14:51:39 -0400731
James Smart0f65ff62010-02-26 14:14:23 -0500732 spin_lock_irqsave(&phba->hbalock, iflag);
733 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
James Smartda0436e2009-05-22 14:51:39 -0400734 list_for_each_entry_safe(psb, next_psb,
735 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
736 if (psb->cur_iocbq.sli4_xritag == xri) {
737 list_del(&psb->list);
James Smart341af102010-01-26 23:07:37 -0500738 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400739 psb->status = IOSTAT_SUCCESS;
James Smart0f65ff62010-02-26 14:14:23 -0500740 spin_unlock(
741 &phba->sli4_hba.abts_scsi_buf_list_lock);
James Smart1151e3e2011-02-16 12:39:35 -0500742 if (psb->rdata && psb->rdata->pnode)
743 ndlp = psb->rdata->pnode;
744 else
745 ndlp = NULL;
746
James Smart19ca7602010-11-20 23:11:55 -0500747 rrq_empty = list_empty(&phba->active_rrq_list);
James Smart0f65ff62010-02-26 14:14:23 -0500748 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartcb69f7d2011-12-13 13:21:57 -0500749 if (ndlp) {
James Smartee0f4fe2012-05-09 21:19:14 -0400750 lpfc_set_rrq_active(phba, ndlp,
751 psb->cur_iocbq.sli4_lxritag, rxid, 1);
James Smartcb69f7d2011-12-13 13:21:57 -0500752 lpfc_sli4_abts_err_handler(phba, ndlp, axri);
753 }
James Smartda0436e2009-05-22 14:51:39 -0400754 lpfc_release_scsi_buf_s4(phba, psb);
James Smart19ca7602010-11-20 23:11:55 -0500755 if (rrq_empty)
756 lpfc_worker_wake_up(phba);
James Smartda0436e2009-05-22 14:51:39 -0400757 return;
758 }
759 }
James Smart0f65ff62010-02-26 14:14:23 -0500760 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
761 for (i = 1; i <= phba->sli.last_iotag; i++) {
762 iocbq = phba->sli.iocbq_lookup[i];
763
764 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
765 (iocbq->iocb_flag & LPFC_IO_LIBDFC))
766 continue;
767 if (iocbq->sli4_xritag != xri)
768 continue;
769 psb = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
770 psb->exch_busy = 0;
771 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart0e9bb8d2013-03-01 16:35:12 -0500772 if (!list_empty(&pring->txq))
James Smart589a52d2010-07-14 15:30:54 -0400773 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -0500774 return;
775
776 }
777 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartda0436e2009-05-22 14:51:39 -0400778}
779
780/**
James Smart8a9d2e82012-05-09 21:16:12 -0400781 * lpfc_sli4_post_scsi_sgl_list - Psot blocks of scsi buffer sgls from a list
782 * @phba: pointer to lpfc hba data structure.
783 * @post_sblist: pointer to the scsi buffer list.
784 *
785 * This routine walks a list of scsi buffers that was passed in. It attempts
786 * to construct blocks of scsi buffer sgls which contains contiguous xris and
787 * uses the non-embedded SGL block post mailbox commands to post to the port.
788 * For single SCSI buffer sgl with non-contiguous xri, if any, it shall use
789 * embedded SGL post mailbox command for posting. The @post_sblist passed in
790 * must be local list, thus no lock is needed when manipulate the list.
791 *
792 * Returns: 0 = failure, non-zero number of successfully posted buffers.
793 **/
794int
795lpfc_sli4_post_scsi_sgl_list(struct lpfc_hba *phba,
796 struct list_head *post_sblist, int sb_count)
797{
798 struct lpfc_scsi_buf *psb, *psb_next;
James Smart96f70772013-04-17 20:16:15 -0400799 int status, sgl_size;
James Smart8a9d2e82012-05-09 21:16:12 -0400800 int post_cnt = 0, block_cnt = 0, num_posting = 0, num_posted = 0;
801 dma_addr_t pdma_phys_bpl1;
802 int last_xritag = NO_XRI;
803 LIST_HEAD(prep_sblist);
804 LIST_HEAD(blck_sblist);
805 LIST_HEAD(scsi_sblist);
806
807 /* sanity check */
808 if (sb_count <= 0)
809 return -EINVAL;
810
James Smart96f70772013-04-17 20:16:15 -0400811 sgl_size = phba->cfg_sg_dma_buf_size -
812 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
813
James Smart8a9d2e82012-05-09 21:16:12 -0400814 list_for_each_entry_safe(psb, psb_next, post_sblist, list) {
815 list_del_init(&psb->list);
816 block_cnt++;
817 if ((last_xritag != NO_XRI) &&
818 (psb->cur_iocbq.sli4_xritag != last_xritag + 1)) {
819 /* a hole in xri block, form a sgl posting block */
820 list_splice_init(&prep_sblist, &blck_sblist);
821 post_cnt = block_cnt - 1;
822 /* prepare list for next posting block */
823 list_add_tail(&psb->list, &prep_sblist);
824 block_cnt = 1;
825 } else {
826 /* prepare list for next posting block */
827 list_add_tail(&psb->list, &prep_sblist);
828 /* enough sgls for non-embed sgl mbox command */
829 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
830 list_splice_init(&prep_sblist, &blck_sblist);
831 post_cnt = block_cnt;
832 block_cnt = 0;
833 }
834 }
835 num_posting++;
836 last_xritag = psb->cur_iocbq.sli4_xritag;
837
838 /* end of repost sgl list condition for SCSI buffers */
839 if (num_posting == sb_count) {
840 if (post_cnt == 0) {
841 /* last sgl posting block */
842 list_splice_init(&prep_sblist, &blck_sblist);
843 post_cnt = block_cnt;
844 } else if (block_cnt == 1) {
845 /* last single sgl with non-contiguous xri */
James Smart96f70772013-04-17 20:16:15 -0400846 if (sgl_size > SGL_PAGE_SIZE)
James Smart8a9d2e82012-05-09 21:16:12 -0400847 pdma_phys_bpl1 = psb->dma_phys_bpl +
848 SGL_PAGE_SIZE;
849 else
850 pdma_phys_bpl1 = 0;
851 status = lpfc_sli4_post_sgl(phba,
852 psb->dma_phys_bpl,
853 pdma_phys_bpl1,
854 psb->cur_iocbq.sli4_xritag);
855 if (status) {
856 /* failure, put on abort scsi list */
857 psb->exch_busy = 1;
858 } else {
859 /* success, put on SCSI buffer list */
860 psb->exch_busy = 0;
861 psb->status = IOSTAT_SUCCESS;
862 num_posted++;
863 }
864 /* success, put on SCSI buffer sgl list */
865 list_add_tail(&psb->list, &scsi_sblist);
866 }
867 }
868
869 /* continue until a nembed page worth of sgls */
870 if (post_cnt == 0)
871 continue;
872
873 /* post block of SCSI buffer list sgls */
874 status = lpfc_sli4_post_scsi_sgl_block(phba, &blck_sblist,
875 post_cnt);
876
877 /* don't reset xirtag due to hole in xri block */
878 if (block_cnt == 0)
879 last_xritag = NO_XRI;
880
881 /* reset SCSI buffer post count for next round of posting */
882 post_cnt = 0;
883
884 /* put posted SCSI buffer-sgl posted on SCSI buffer sgl list */
885 while (!list_empty(&blck_sblist)) {
886 list_remove_head(&blck_sblist, psb,
887 struct lpfc_scsi_buf, list);
888 if (status) {
889 /* failure, put on abort scsi list */
890 psb->exch_busy = 1;
891 } else {
892 /* success, put on SCSI buffer list */
893 psb->exch_busy = 0;
894 psb->status = IOSTAT_SUCCESS;
895 num_posted++;
896 }
897 list_add_tail(&psb->list, &scsi_sblist);
898 }
899 }
900 /* Push SCSI buffers with sgl posted to the availble list */
901 while (!list_empty(&scsi_sblist)) {
902 list_remove_head(&scsi_sblist, psb,
903 struct lpfc_scsi_buf, list);
904 lpfc_release_scsi_buf_s4(phba, psb);
905 }
906 return num_posted;
907}
908
909/**
910 * lpfc_sli4_repost_scsi_sgl_list - Repsot all the allocated scsi buffer sgls
James Smartda0436e2009-05-22 14:51:39 -0400911 * @phba: pointer to lpfc hba data structure.
912 *
913 * This routine walks the list of scsi buffers that have been allocated and
James Smart8a9d2e82012-05-09 21:16:12 -0400914 * repost them to the port by using SGL block post. This is needed after a
James Smartda0436e2009-05-22 14:51:39 -0400915 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
916 * is responsible for moving all scsi buffers on the lpfc_abts_scsi_sgl_list
917 * to the lpfc_scsi_buf_list. If the repost fails, reject all scsi buffers.
918 *
919 * Returns: 0 = success, non-zero failure.
920 **/
921int
922lpfc_sli4_repost_scsi_sgl_list(struct lpfc_hba *phba)
923{
James Smart8a9d2e82012-05-09 21:16:12 -0400924 LIST_HEAD(post_sblist);
925 int num_posted, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -0400926
James Smart8a9d2e82012-05-09 21:16:12 -0400927 /* get all SCSI buffers need to repost to a local list */
James Smarta40fc5f2013-04-17 20:17:40 -0400928 spin_lock_irq(&phba->scsi_buf_list_get_lock);
929 spin_lock_irq(&phba->scsi_buf_list_put_lock);
930 list_splice_init(&phba->lpfc_scsi_buf_list_get, &post_sblist);
931 list_splice(&phba->lpfc_scsi_buf_list_put, &post_sblist);
932 spin_unlock_irq(&phba->scsi_buf_list_put_lock);
933 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smartda0436e2009-05-22 14:51:39 -0400934
James Smart8a9d2e82012-05-09 21:16:12 -0400935 /* post the list of scsi buffer sgls to port if available */
936 if (!list_empty(&post_sblist)) {
937 num_posted = lpfc_sli4_post_scsi_sgl_list(phba, &post_sblist,
938 phba->sli4_hba.scsi_xri_cnt);
939 /* failed to post any scsi buffer, return error */
940 if (num_posted == 0)
941 rc = -EIO;
James Smartda0436e2009-05-22 14:51:39 -0400942 }
943 return rc;
944}
945
946/**
947 * lpfc_new_scsi_buf_s4 - Scsi buffer allocator for HBA with SLI4 IF spec
948 * @vport: The virtual port for which this call being executed.
949 * @num_to_allocate: The requested number of buffers to allocate.
950 *
James Smart8a9d2e82012-05-09 21:16:12 -0400951 * This routine allocates scsi buffers for device with SLI-4 interface spec,
James Smartda0436e2009-05-22 14:51:39 -0400952 * the scsi buffer contains all the necessary information needed to initiate
James Smart8a9d2e82012-05-09 21:16:12 -0400953 * a SCSI I/O. After allocating up to @num_to_allocate SCSI buffers and put
954 * them on a list, it post them to the port by using SGL block post.
James Smartda0436e2009-05-22 14:51:39 -0400955 *
956 * Return codes:
James Smart8a9d2e82012-05-09 21:16:12 -0400957 * int - number of scsi buffers that were allocated and posted.
James Smartda0436e2009-05-22 14:51:39 -0400958 * 0 = failure, less than num_to_alloc is a partial failure.
959 **/
960static int
961lpfc_new_scsi_buf_s4(struct lpfc_vport *vport, int num_to_alloc)
962{
963 struct lpfc_hba *phba = vport->phba;
964 struct lpfc_scsi_buf *psb;
965 struct sli4_sge *sgl;
966 IOCB_t *iocb;
967 dma_addr_t pdma_phys_fcp_cmd;
968 dma_addr_t pdma_phys_fcp_rsp;
James Smart96f70772013-04-17 20:16:15 -0400969 dma_addr_t pdma_phys_bpl;
James Smart8a9d2e82012-05-09 21:16:12 -0400970 uint16_t iotag, lxri = 0;
James Smart96f70772013-04-17 20:16:15 -0400971 int bcnt, num_posted, sgl_size;
James Smart8a9d2e82012-05-09 21:16:12 -0400972 LIST_HEAD(prep_sblist);
973 LIST_HEAD(post_sblist);
974 LIST_HEAD(scsi_sblist);
James Smartda0436e2009-05-22 14:51:39 -0400975
James Smart96f70772013-04-17 20:16:15 -0400976 sgl_size = phba->cfg_sg_dma_buf_size -
977 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
978
979 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
980 "9068 ALLOC %d scsi_bufs: %d (%d + %d + %d)\n",
981 num_to_alloc, phba->cfg_sg_dma_buf_size, sgl_size,
982 (int)sizeof(struct fcp_cmnd),
983 (int)sizeof(struct fcp_rsp));
984
James Smartda0436e2009-05-22 14:51:39 -0400985 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
986 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
987 if (!psb)
988 break;
James Smartda0436e2009-05-22 14:51:39 -0400989 /*
James Smart8a9d2e82012-05-09 21:16:12 -0400990 * Get memory from the pci pool to map the virt space to
991 * pci bus space for an I/O. The DMA buffer includes space
992 * for the struct fcp_cmnd, struct fcp_rsp and the number
993 * of bde's necessary to support the sg_tablesize.
James Smartda0436e2009-05-22 14:51:39 -0400994 */
995 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
996 GFP_KERNEL, &psb->dma_handle);
997 if (!psb->data) {
998 kfree(psb);
999 break;
1000 }
James Smartda0436e2009-05-22 14:51:39 -04001001 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
1002
James Smart092cb032013-09-06 12:21:50 -04001003 /*
1004 * 4K Page alignment is CRITICAL to BlockGuard, double check
1005 * to be sure.
1006 */
1007 if (phba->cfg_enable_bg && (((unsigned long)(psb->data) &
1008 (unsigned long)(SLI4_PAGE_SIZE - 1)) != 0)) {
James Smart96f70772013-04-17 20:16:15 -04001009 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
1010 psb->data, psb->dma_handle);
1011 kfree(psb);
1012 break;
1013 }
1014
James Smartda0436e2009-05-22 14:51:39 -04001015 /* Allocate iotag for psb->cur_iocbq. */
1016 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
1017 if (iotag == 0) {
James Smartb92938b2010-06-07 15:24:12 -04001018 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
1019 psb->data, psb->dma_handle);
James Smartda0436e2009-05-22 14:51:39 -04001020 kfree(psb);
1021 break;
1022 }
1023
James Smart6d368e52011-05-24 11:44:12 -04001024 lxri = lpfc_sli4_next_xritag(phba);
1025 if (lxri == NO_XRI) {
James Smartda0436e2009-05-22 14:51:39 -04001026 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
1027 psb->data, psb->dma_handle);
1028 kfree(psb);
1029 break;
1030 }
James Smart6d368e52011-05-24 11:44:12 -04001031 psb->cur_iocbq.sli4_lxritag = lxri;
1032 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
James Smartda0436e2009-05-22 14:51:39 -04001033 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Smartda0436e2009-05-22 14:51:39 -04001034 psb->fcp_bpl = psb->data;
James Smart96f70772013-04-17 20:16:15 -04001035 psb->fcp_cmnd = (psb->data + sgl_size);
James Smartda0436e2009-05-22 14:51:39 -04001036 psb->fcp_rsp = (struct fcp_rsp *)((uint8_t *)psb->fcp_cmnd +
1037 sizeof(struct fcp_cmnd));
1038
1039 /* Initialize local short-hand pointers. */
1040 sgl = (struct sli4_sge *)psb->fcp_bpl;
1041 pdma_phys_bpl = psb->dma_handle;
James Smart96f70772013-04-17 20:16:15 -04001042 pdma_phys_fcp_cmd = (psb->dma_handle + sgl_size);
James Smartda0436e2009-05-22 14:51:39 -04001043 pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
1044
1045 /*
James Smart8a9d2e82012-05-09 21:16:12 -04001046 * The first two bdes are the FCP_CMD and FCP_RSP.
1047 * The balance are sg list bdes. Initialize the
1048 * first two and leave the rest for queuecommand.
James Smartda0436e2009-05-22 14:51:39 -04001049 */
1050 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
1051 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
James Smart05580562011-05-24 11:40:48 -04001052 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -04001053 bf_set(lpfc_sli4_sge_last, sgl, 0);
1054 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05001055 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_cmnd));
James Smartda0436e2009-05-22 14:51:39 -04001056 sgl++;
1057
1058 /* Setup the physical region for the FCP RSP */
1059 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
1060 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
James Smart05580562011-05-24 11:40:48 -04001061 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -04001062 bf_set(lpfc_sli4_sge_last, sgl, 1);
1063 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05001064 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
James Smartda0436e2009-05-22 14:51:39 -04001065
1066 /*
1067 * Since the IOCB for the FCP I/O is built into this
1068 * lpfc_scsi_buf, initialize it with all known data now.
1069 */
1070 iocb = &psb->cur_iocbq.iocb;
1071 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
1072 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
1073 /* setting the BLP size to 2 * sizeof BDE may not be correct.
1074 * We are setting the bpl to point to out sgl. An sgl's
1075 * entries are 16 bytes, a bpl entries are 12 bytes.
1076 */
1077 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
1078 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
1079 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
1080 iocb->ulpBdeCount = 1;
1081 iocb->ulpLe = 1;
1082 iocb->ulpClass = CLASS3;
James Smart8a9d2e82012-05-09 21:16:12 -04001083 psb->cur_iocbq.context1 = psb;
James Smartda0436e2009-05-22 14:51:39 -04001084 psb->dma_phys_bpl = pdma_phys_bpl;
James Smart6d368e52011-05-24 11:44:12 -04001085
James Smart8a9d2e82012-05-09 21:16:12 -04001086 /* add the scsi buffer to a post list */
1087 list_add_tail(&psb->list, &post_sblist);
James Smarta40fc5f2013-04-17 20:17:40 -04001088 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04001089 phba->sli4_hba.scsi_xri_cnt++;
James Smarta40fc5f2013-04-17 20:17:40 -04001090 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smartda0436e2009-05-22 14:51:39 -04001091 }
James Smart8a9d2e82012-05-09 21:16:12 -04001092 lpfc_printf_log(phba, KERN_INFO, LOG_BG,
1093 "3021 Allocate %d out of %d requested new SCSI "
1094 "buffers\n", bcnt, num_to_alloc);
James Smartda0436e2009-05-22 14:51:39 -04001095
James Smart8a9d2e82012-05-09 21:16:12 -04001096 /* post the list of scsi buffer sgls to port if available */
1097 if (!list_empty(&post_sblist))
1098 num_posted = lpfc_sli4_post_scsi_sgl_list(phba,
1099 &post_sblist, bcnt);
1100 else
1101 num_posted = 0;
1102
1103 return num_posted;
James Smartda0436e2009-05-22 14:51:39 -04001104}
1105
1106/**
James Smart3772a992009-05-22 14:50:54 -04001107 * lpfc_new_scsi_buf - Wrapper funciton for scsi buffer allocator
1108 * @vport: The virtual port for which this call being executed.
1109 * @num_to_allocate: The requested number of buffers to allocate.
1110 *
1111 * This routine wraps the actual SCSI buffer allocator function pointer from
1112 * the lpfc_hba struct.
1113 *
1114 * Return codes:
1115 * int - number of scsi buffers that were allocated.
1116 * 0 = failure, less than num_to_alloc is a partial failure.
1117 **/
1118static inline int
1119lpfc_new_scsi_buf(struct lpfc_vport *vport, int num_to_alloc)
1120{
1121 return vport->phba->lpfc_new_scsi_buf(vport, num_to_alloc);
1122}
1123
1124/**
James Smart19ca7602010-11-20 23:11:55 -05001125 * lpfc_get_scsi_buf_s3 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
James Smart3772a992009-05-22 14:50:54 -04001126 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -05001127 *
1128 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1129 * and returns to caller.
1130 *
1131 * Return codes:
1132 * NULL - Error
1133 * Pointer to lpfc_scsi_buf - Success
1134 **/
Adrian Bunk455c53e2006-01-06 20:21:28 +01001135static struct lpfc_scsi_buf*
James Smart19ca7602010-11-20 23:11:55 -05001136lpfc_get_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001137{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001138 struct lpfc_scsi_buf * lpfc_cmd = NULL;
James Smarta40fc5f2013-04-17 20:17:40 -04001139 struct list_head *scsi_buf_list_get = &phba->lpfc_scsi_buf_list_get;
1140 unsigned long gflag = 0;
1141 unsigned long pflag = 0;
dea31012005-04-17 16:05:31 -05001142
James Smarta40fc5f2013-04-17 20:17:40 -04001143 spin_lock_irqsave(&phba->scsi_buf_list_get_lock, gflag);
1144 list_remove_head(scsi_buf_list_get, lpfc_cmd, struct lpfc_scsi_buf,
1145 list);
1146 if (!lpfc_cmd) {
1147 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, pflag);
1148 list_splice(&phba->lpfc_scsi_buf_list_put,
1149 &phba->lpfc_scsi_buf_list_get);
1150 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
1151 list_remove_head(scsi_buf_list_get, lpfc_cmd,
1152 struct lpfc_scsi_buf, list);
1153 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, pflag);
James Smart1dcb58e2007-04-25 09:51:30 -04001154 }
James Smarta40fc5f2013-04-17 20:17:40 -04001155 spin_unlock_irqrestore(&phba->scsi_buf_list_get_lock, gflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001156 return lpfc_cmd;
1157}
James Smart19ca7602010-11-20 23:11:55 -05001158/**
1159 * lpfc_get_scsi_buf_s4 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1160 * @phba: The HBA for which this call is being executed.
1161 *
1162 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1163 * and returns to caller.
1164 *
1165 * Return codes:
1166 * NULL - Error
1167 * Pointer to lpfc_scsi_buf - Success
1168 **/
1169static struct lpfc_scsi_buf*
1170lpfc_get_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1171{
James Smart3be30e02013-05-31 17:05:17 -04001172 struct lpfc_scsi_buf *lpfc_cmd, *lpfc_cmd_next;
James Smarta40fc5f2013-04-17 20:17:40 -04001173 unsigned long gflag = 0;
1174 unsigned long pflag = 0;
James Smart19ca7602010-11-20 23:11:55 -05001175 int found = 0;
1176
James Smarta40fc5f2013-04-17 20:17:40 -04001177 spin_lock_irqsave(&phba->scsi_buf_list_get_lock, gflag);
James Smart3be30e02013-05-31 17:05:17 -04001178 list_for_each_entry_safe(lpfc_cmd, lpfc_cmd_next,
1179 &phba->lpfc_scsi_buf_list_get, list) {
James Smart19ca7602010-11-20 23:11:55 -05001180 if (lpfc_test_rrq_active(phba, ndlp,
James Smartee0f4fe2012-05-09 21:19:14 -04001181 lpfc_cmd->cur_iocbq.sli4_lxritag))
James Smart1151e3e2011-02-16 12:39:35 -05001182 continue;
1183 list_del(&lpfc_cmd->list);
James Smart19ca7602010-11-20 23:11:55 -05001184 found = 1;
James Smart1151e3e2011-02-16 12:39:35 -05001185 break;
James Smart19ca7602010-11-20 23:11:55 -05001186 }
James Smarta40fc5f2013-04-17 20:17:40 -04001187 if (!found) {
1188 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, pflag);
1189 list_splice(&phba->lpfc_scsi_buf_list_put,
1190 &phba->lpfc_scsi_buf_list_get);
1191 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
1192 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, pflag);
James Smart3be30e02013-05-31 17:05:17 -04001193 list_for_each_entry_safe(lpfc_cmd, lpfc_cmd_next,
1194 &phba->lpfc_scsi_buf_list_get, list) {
James Smarta40fc5f2013-04-17 20:17:40 -04001195 if (lpfc_test_rrq_active(
1196 phba, ndlp, lpfc_cmd->cur_iocbq.sli4_lxritag))
1197 continue;
1198 list_del(&lpfc_cmd->list);
1199 found = 1;
1200 break;
1201 }
1202 }
1203 spin_unlock_irqrestore(&phba->scsi_buf_list_get_lock, gflag);
James Smart1151e3e2011-02-16 12:39:35 -05001204 if (!found)
1205 return NULL;
James Smarta40fc5f2013-04-17 20:17:40 -04001206 return lpfc_cmd;
James Smart19ca7602010-11-20 23:11:55 -05001207}
1208/**
1209 * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1210 * @phba: The HBA for which this call is being executed.
1211 *
1212 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1213 * and returns to caller.
1214 *
1215 * Return codes:
1216 * NULL - Error
1217 * Pointer to lpfc_scsi_buf - Success
1218 **/
1219static struct lpfc_scsi_buf*
1220lpfc_get_scsi_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1221{
1222 return phba->lpfc_get_scsi_buf(phba, ndlp);
1223}
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001224
James Smart9bad7672008-12-04 22:39:02 -05001225/**
James Smart3772a992009-05-22 14:50:54 -04001226 * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
James Smart9bad7672008-12-04 22:39:02 -05001227 * @phba: The Hba for which this call is being executed.
1228 * @psb: The scsi buffer which is being released.
1229 *
1230 * This routine releases @psb scsi buffer by adding it to tail of @phba
1231 * lpfc_scsi_buf_list list.
1232 **/
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001233static void
James Smart3772a992009-05-22 14:50:54 -04001234lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001235{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001236 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -05001237
James Smarta40fc5f2013-04-17 20:17:40 -04001238 psb->seg_cnt = 0;
1239 psb->nonsg_phys = 0;
1240 psb->prot_seg_cnt = 0;
1241
1242 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001243 psb->pCmd = NULL;
James Smart6a485eb2013-04-17 20:19:00 -04001244 psb->cur_iocbq.iocb_flag = LPFC_IO_FCP;
James Smarta40fc5f2013-04-17 20:17:40 -04001245 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list_put);
1246 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
dea31012005-04-17 16:05:31 -05001247}
1248
James Smart9bad7672008-12-04 22:39:02 -05001249/**
James Smartda0436e2009-05-22 14:51:39 -04001250 * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
1251 * @phba: The Hba for which this call is being executed.
1252 * @psb: The scsi buffer which is being released.
1253 *
1254 * This routine releases @psb scsi buffer by adding it to tail of @phba
1255 * lpfc_scsi_buf_list list. For SLI4 XRI's are tied to the scsi buffer
1256 * and cannot be reused for at least RA_TOV amount of time if it was
1257 * aborted.
1258 **/
1259static void
1260lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1261{
1262 unsigned long iflag = 0;
1263
James Smarta40fc5f2013-04-17 20:17:40 -04001264 psb->seg_cnt = 0;
1265 psb->nonsg_phys = 0;
1266 psb->prot_seg_cnt = 0;
1267
James Smart341af102010-01-26 23:07:37 -05001268 if (psb->exch_busy) {
James Smartda0436e2009-05-22 14:51:39 -04001269 spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock,
1270 iflag);
1271 psb->pCmd = NULL;
1272 list_add_tail(&psb->list,
1273 &phba->sli4_hba.lpfc_abts_scsi_buf_list);
1274 spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
1275 iflag);
1276 } else {
James Smartda0436e2009-05-22 14:51:39 -04001277 psb->pCmd = NULL;
James Smart6a485eb2013-04-17 20:19:00 -04001278 psb->cur_iocbq.iocb_flag = LPFC_IO_FCP;
James Smarta40fc5f2013-04-17 20:17:40 -04001279 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
1280 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list_put);
1281 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
James Smartda0436e2009-05-22 14:51:39 -04001282 }
1283}
1284
1285/**
James Smart3772a992009-05-22 14:50:54 -04001286 * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
1287 * @phba: The Hba for which this call is being executed.
1288 * @psb: The scsi buffer which is being released.
1289 *
1290 * This routine releases @psb scsi buffer by adding it to tail of @phba
1291 * lpfc_scsi_buf_list list.
1292 **/
1293static void
1294lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1295{
1296
1297 phba->lpfc_release_scsi_buf(phba, psb);
1298}
1299
1300/**
1301 * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -05001302 * @phba: The Hba for which this call is being executed.
1303 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1304 *
1305 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
James Smart3772a992009-05-22 14:50:54 -04001306 * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
1307 * through sg elements and format the bdea. This routine also initializes all
1308 * IOCB fields which are dependent on scsi command request buffer.
James Smart9bad7672008-12-04 22:39:02 -05001309 *
1310 * Return codes:
1311 * 1 - Error
1312 * 0 - Success
1313 **/
dea31012005-04-17 16:05:31 -05001314static int
James Smart3772a992009-05-22 14:50:54 -04001315lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
dea31012005-04-17 16:05:31 -05001316{
1317 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1318 struct scatterlist *sgel = NULL;
1319 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1320 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
James Smart0f65ff62010-02-26 14:14:23 -05001321 struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
dea31012005-04-17 16:05:31 -05001322 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart34b02dc2008-08-24 21:49:55 -04001323 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
dea31012005-04-17 16:05:31 -05001324 dma_addr_t physaddr;
James Smart34b02dc2008-08-24 21:49:55 -04001325 uint32_t num_bde = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001326 int nseg, datadir = scsi_cmnd->sc_data_direction;
dea31012005-04-17 16:05:31 -05001327
1328 /*
1329 * There are three possibilities here - use scatter-gather segment, use
1330 * the single mapping, or neither. Start the lpfc command prep by
1331 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1332 * data bde entry.
1333 */
1334 bpl += 2;
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001335 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05001336 /*
1337 * The driver stores the segment count returned from pci_map_sg
1338 * because this a count of dma-mappings used to map the use_sg
1339 * pages. They are not guaranteed to be the same for those
1340 * architectures that implement an IOMMU.
1341 */
dea31012005-04-17 16:05:31 -05001342
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001343 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
1344 scsi_sg_count(scsi_cmnd), datadir);
1345 if (unlikely(!nseg))
1346 return 1;
1347
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001348 lpfc_cmd->seg_cnt = nseg;
dea31012005-04-17 16:05:31 -05001349 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001350 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1351 "9064 BLKGRD: %s: Too many sg segments from "
James Smarte2a0a9d2008-12-04 22:40:02 -05001352 "dma_map_sg. Config %d, seg_cnt %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001353 __func__, phba->cfg_sg_seg_cnt,
dea31012005-04-17 16:05:31 -05001354 lpfc_cmd->seg_cnt);
James Smart96f70772013-04-17 20:16:15 -04001355 lpfc_cmd->seg_cnt = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001356 scsi_dma_unmap(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001357 return 1;
1358 }
1359
1360 /*
1361 * The driver established a maximum scatter-gather segment count
1362 * during probe that limits the number of sg elements in any
1363 * single scsi command. Just run through the seg_cnt and format
1364 * the bde's.
James Smart34b02dc2008-08-24 21:49:55 -04001365 * When using SLI-3 the driver will try to fit all the BDEs into
1366 * the IOCB. If it can't then the BDEs get added to a BPL as it
1367 * does for SLI-2 mode.
dea31012005-04-17 16:05:31 -05001368 */
James Smart34b02dc2008-08-24 21:49:55 -04001369 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
dea31012005-04-17 16:05:31 -05001370 physaddr = sg_dma_address(sgel);
James Smart34b02dc2008-08-24 21:49:55 -04001371 if (phba->sli_rev == 3 &&
James Smarte2a0a9d2008-12-04 22:40:02 -05001372 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
James Smart0f65ff62010-02-26 14:14:23 -05001373 !(iocbq->iocb_flag & DSS_SECURITY_OP) &&
James Smart34b02dc2008-08-24 21:49:55 -04001374 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
1375 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1376 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
1377 data_bde->addrLow = putPaddrLow(physaddr);
1378 data_bde->addrHigh = putPaddrHigh(physaddr);
1379 data_bde++;
1380 } else {
1381 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1382 bpl->tus.f.bdeSize = sg_dma_len(sgel);
1383 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1384 bpl->addrLow =
1385 le32_to_cpu(putPaddrLow(physaddr));
1386 bpl->addrHigh =
1387 le32_to_cpu(putPaddrHigh(physaddr));
1388 bpl++;
1389 }
dea31012005-04-17 16:05:31 -05001390 }
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001391 }
dea31012005-04-17 16:05:31 -05001392
1393 /*
1394 * Finish initializing those IOCB fields that are dependent on the
James Smart34b02dc2008-08-24 21:49:55 -04001395 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
1396 * explicitly reinitialized and for SLI-3 the extended bde count is
1397 * explicitly reinitialized since all iocb memory resources are reused.
dea31012005-04-17 16:05:31 -05001398 */
James Smarte2a0a9d2008-12-04 22:40:02 -05001399 if (phba->sli_rev == 3 &&
James Smart0f65ff62010-02-26 14:14:23 -05001400 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
1401 !(iocbq->iocb_flag & DSS_SECURITY_OP)) {
James Smart34b02dc2008-08-24 21:49:55 -04001402 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
1403 /*
1404 * The extended IOCB format can only fit 3 BDE or a BPL.
1405 * This I/O has more than 3 BDE so the 1st data bde will
1406 * be a BPL that is filled in here.
1407 */
1408 physaddr = lpfc_cmd->dma_handle;
1409 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
1410 data_bde->tus.f.bdeSize = (num_bde *
1411 sizeof(struct ulp_bde64));
1412 physaddr += (sizeof(struct fcp_cmnd) +
1413 sizeof(struct fcp_rsp) +
1414 (2 * sizeof(struct ulp_bde64)));
1415 data_bde->addrHigh = putPaddrHigh(physaddr);
1416 data_bde->addrLow = putPaddrLow(physaddr);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001417 /* ebde count includes the response bde and data bpl */
James Smart34b02dc2008-08-24 21:49:55 -04001418 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
1419 } else {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001420 /* ebde count includes the response bde and data bdes */
James Smart34b02dc2008-08-24 21:49:55 -04001421 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1422 }
1423 } else {
1424 iocb_cmd->un.fcpi64.bdl.bdeSize =
1425 ((num_bde + 2) * sizeof(struct ulp_bde64));
James Smart0f65ff62010-02-26 14:14:23 -05001426 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
James Smart34b02dc2008-08-24 21:49:55 -04001427 }
James Smart09372822008-01-11 01:52:54 -05001428 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
James Smarte2a0a9d2008-12-04 22:40:02 -05001429
1430 /*
1431 * Due to difference in data length between DIF/non-DIF paths,
1432 * we need to set word 4 of IOCB here
1433 */
James Smarta257bf92009-04-06 18:48:10 -04001434 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001435 return 0;
1436}
1437
James Smartf9bb2da2011-10-10 21:34:11 -04001438#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001439
James Smart9a6b09c2012-03-01 22:37:42 -05001440/* Return if if error injection is detected by Initiator */
1441#define BG_ERR_INIT 0x1
1442/* Return if if error injection is detected by Target */
1443#define BG_ERR_TGT 0x2
1444/* Return if if swapping CSUM<-->CRC is required for error injection */
1445#define BG_ERR_SWAP 0x10
1446/* Return if disabling Guard/Ref/App checking is required for error injection */
1447#define BG_ERR_CHECK 0x20
James Smartacd68592012-01-18 16:25:09 -05001448
1449/**
1450 * lpfc_bg_err_inject - Determine if we should inject an error
1451 * @phba: The Hba for which this call is being executed.
James Smartf9bb2da2011-10-10 21:34:11 -04001452 * @sc: The SCSI command to examine
1453 * @reftag: (out) BlockGuard reference tag for transmitted data
1454 * @apptag: (out) BlockGuard application tag for transmitted data
1455 * @new_guard (in) Value to replace CRC with if needed
1456 *
James Smart9a6b09c2012-03-01 22:37:42 -05001457 * Returns BG_ERR_* bit mask or 0 if request ignored
James Smartacd68592012-01-18 16:25:09 -05001458 **/
James Smartf9bb2da2011-10-10 21:34:11 -04001459static int
1460lpfc_bg_err_inject(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1461 uint32_t *reftag, uint16_t *apptag, uint32_t new_guard)
1462{
1463 struct scatterlist *sgpe; /* s/g prot entry */
1464 struct scatterlist *sgde; /* s/g data entry */
James Smart9a6b09c2012-03-01 22:37:42 -05001465 struct lpfc_scsi_buf *lpfc_cmd = NULL;
James Smartacd68592012-01-18 16:25:09 -05001466 struct scsi_dif_tuple *src = NULL;
James Smart4ac9b222012-03-01 22:38:29 -05001467 struct lpfc_nodelist *ndlp;
1468 struct lpfc_rport_data *rdata;
James Smartf9bb2da2011-10-10 21:34:11 -04001469 uint32_t op = scsi_get_prot_op(sc);
1470 uint32_t blksize;
1471 uint32_t numblks;
1472 sector_t lba;
1473 int rc = 0;
James Smartacd68592012-01-18 16:25:09 -05001474 int blockoff = 0;
James Smartf9bb2da2011-10-10 21:34:11 -04001475
1476 if (op == SCSI_PROT_NORMAL)
1477 return 0;
1478
James Smartacd68592012-01-18 16:25:09 -05001479 sgpe = scsi_prot_sglist(sc);
1480 sgde = scsi_sglist(sc);
James Smartf9bb2da2011-10-10 21:34:11 -04001481 lba = scsi_get_lba(sc);
James Smart4ac9b222012-03-01 22:38:29 -05001482
1483 /* First check if we need to match the LBA */
James Smartf9bb2da2011-10-10 21:34:11 -04001484 if (phba->lpfc_injerr_lba != LPFC_INJERR_LBA_OFF) {
1485 blksize = lpfc_cmd_blksize(sc);
1486 numblks = (scsi_bufflen(sc) + blksize - 1) / blksize;
1487
1488 /* Make sure we have the right LBA if one is specified */
1489 if ((phba->lpfc_injerr_lba < lba) ||
1490 (phba->lpfc_injerr_lba >= (lba + numblks)))
1491 return 0;
James Smartacd68592012-01-18 16:25:09 -05001492 if (sgpe) {
1493 blockoff = phba->lpfc_injerr_lba - lba;
1494 numblks = sg_dma_len(sgpe) /
1495 sizeof(struct scsi_dif_tuple);
1496 if (numblks < blockoff)
1497 blockoff = numblks;
James Smartacd68592012-01-18 16:25:09 -05001498 }
James Smartf9bb2da2011-10-10 21:34:11 -04001499 }
1500
James Smart4ac9b222012-03-01 22:38:29 -05001501 /* Next check if we need to match the remote NPortID or WWPN */
1502 rdata = sc->device->hostdata;
1503 if (rdata && rdata->pnode) {
1504 ndlp = rdata->pnode;
1505
1506 /* Make sure we have the right NPortID if one is specified */
1507 if (phba->lpfc_injerr_nportid &&
1508 (phba->lpfc_injerr_nportid != ndlp->nlp_DID))
1509 return 0;
1510
1511 /*
1512 * Make sure we have the right WWPN if one is specified.
1513 * wwn[0] should be a non-zero NAA in a good WWPN.
1514 */
1515 if (phba->lpfc_injerr_wwpn.u.wwn[0] &&
1516 (memcmp(&ndlp->nlp_portname, &phba->lpfc_injerr_wwpn,
1517 sizeof(struct lpfc_name)) != 0))
1518 return 0;
1519 }
1520
1521 /* Setup a ptr to the protection data if the SCSI host provides it */
1522 if (sgpe) {
1523 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
1524 src += blockoff;
1525 lpfc_cmd = (struct lpfc_scsi_buf *)sc->host_scribble;
1526 }
1527
James Smartf9bb2da2011-10-10 21:34:11 -04001528 /* Should we change the Reference Tag */
1529 if (reftag) {
James Smartacd68592012-01-18 16:25:09 -05001530 if (phba->lpfc_injerr_wref_cnt) {
1531 switch (op) {
1532 case SCSI_PROT_WRITE_PASS:
James Smart9a6b09c2012-03-01 22:37:42 -05001533 if (src) {
1534 /*
1535 * For WRITE_PASS, force the error
1536 * to be sent on the wire. It should
1537 * be detected by the Target.
1538 * If blockoff != 0 error will be
1539 * inserted in middle of the IO.
1540 */
James Smartf9bb2da2011-10-10 21:34:11 -04001541
James Smartacd68592012-01-18 16:25:09 -05001542 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1543 "9076 BLKGRD: Injecting reftag error: "
1544 "write lba x%lx + x%x oldrefTag x%x\n",
1545 (unsigned long)lba, blockoff,
James Smart9a6b09c2012-03-01 22:37:42 -05001546 be32_to_cpu(src->ref_tag));
James Smartacd68592012-01-18 16:25:09 -05001547
1548 /*
James Smart9a6b09c2012-03-01 22:37:42 -05001549 * Save the old ref_tag so we can
1550 * restore it on completion.
James Smartacd68592012-01-18 16:25:09 -05001551 */
James Smart9a6b09c2012-03-01 22:37:42 -05001552 if (lpfc_cmd) {
1553 lpfc_cmd->prot_data_type =
1554 LPFC_INJERR_REFTAG;
1555 lpfc_cmd->prot_data_segment =
1556 src;
1557 lpfc_cmd->prot_data =
1558 src->ref_tag;
1559 }
1560 src->ref_tag = cpu_to_be32(0xDEADBEEF);
James Smartacd68592012-01-18 16:25:09 -05001561 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001562 if (phba->lpfc_injerr_wref_cnt == 0) {
1563 phba->lpfc_injerr_nportid = 0;
1564 phba->lpfc_injerr_lba =
1565 LPFC_INJERR_LBA_OFF;
1566 memset(&phba->lpfc_injerr_wwpn,
1567 0, sizeof(struct lpfc_name));
1568 }
James Smart9a6b09c2012-03-01 22:37:42 -05001569 rc = BG_ERR_TGT | BG_ERR_CHECK;
1570
James Smartacd68592012-01-18 16:25:09 -05001571 break;
1572 }
1573 /* Drop thru */
James Smart9a6b09c2012-03-01 22:37:42 -05001574 case SCSI_PROT_WRITE_INSERT:
1575 /*
1576 * For WRITE_INSERT, force the error
1577 * to be sent on the wire. It should be
1578 * detected by the Target.
1579 */
1580 /* DEADBEEF will be the reftag on the wire */
1581 *reftag = 0xDEADBEEF;
1582 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001583 if (phba->lpfc_injerr_wref_cnt == 0) {
1584 phba->lpfc_injerr_nportid = 0;
1585 phba->lpfc_injerr_lba =
1586 LPFC_INJERR_LBA_OFF;
1587 memset(&phba->lpfc_injerr_wwpn,
1588 0, sizeof(struct lpfc_name));
1589 }
James Smart9a6b09c2012-03-01 22:37:42 -05001590 rc = BG_ERR_TGT | BG_ERR_CHECK;
1591
1592 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1593 "9078 BLKGRD: Injecting reftag error: "
1594 "write lba x%lx\n", (unsigned long)lba);
1595 break;
James Smartacd68592012-01-18 16:25:09 -05001596 case SCSI_PROT_WRITE_STRIP:
1597 /*
1598 * For WRITE_STRIP and WRITE_PASS,
1599 * force the error on data
1600 * being copied from SLI-Host to SLI-Port.
1601 */
1602 *reftag = 0xDEADBEEF;
1603 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001604 if (phba->lpfc_injerr_wref_cnt == 0) {
1605 phba->lpfc_injerr_nportid = 0;
1606 phba->lpfc_injerr_lba =
1607 LPFC_INJERR_LBA_OFF;
1608 memset(&phba->lpfc_injerr_wwpn,
1609 0, sizeof(struct lpfc_name));
1610 }
James Smartacd68592012-01-18 16:25:09 -05001611 rc = BG_ERR_INIT;
1612
1613 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1614 "9077 BLKGRD: Injecting reftag error: "
1615 "write lba x%lx\n", (unsigned long)lba);
1616 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001617 }
James Smartacd68592012-01-18 16:25:09 -05001618 }
1619 if (phba->lpfc_injerr_rref_cnt) {
1620 switch (op) {
1621 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001622 case SCSI_PROT_READ_STRIP:
1623 case SCSI_PROT_READ_PASS:
1624 /*
1625 * For READ_STRIP and READ_PASS, force the
1626 * error on data being read off the wire. It
1627 * should force an IO error to the driver.
1628 */
James Smartf9bb2da2011-10-10 21:34:11 -04001629 *reftag = 0xDEADBEEF;
1630 phba->lpfc_injerr_rref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001631 if (phba->lpfc_injerr_rref_cnt == 0) {
1632 phba->lpfc_injerr_nportid = 0;
1633 phba->lpfc_injerr_lba =
1634 LPFC_INJERR_LBA_OFF;
1635 memset(&phba->lpfc_injerr_wwpn,
1636 0, sizeof(struct lpfc_name));
1637 }
James Smartacd68592012-01-18 16:25:09 -05001638 rc = BG_ERR_INIT;
James Smartf9bb2da2011-10-10 21:34:11 -04001639
1640 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smartacd68592012-01-18 16:25:09 -05001641 "9079 BLKGRD: Injecting reftag error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001642 "read lba x%lx\n", (unsigned long)lba);
James Smartacd68592012-01-18 16:25:09 -05001643 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001644 }
1645 }
1646 }
1647
1648 /* Should we change the Application Tag */
1649 if (apptag) {
James Smartacd68592012-01-18 16:25:09 -05001650 if (phba->lpfc_injerr_wapp_cnt) {
1651 switch (op) {
1652 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001653 if (src) {
James Smart9a6b09c2012-03-01 22:37:42 -05001654 /*
1655 * For WRITE_PASS, force the error
1656 * to be sent on the wire. It should
1657 * be detected by the Target.
1658 * If blockoff != 0 error will be
1659 * inserted in middle of the IO.
1660 */
1661
James Smartacd68592012-01-18 16:25:09 -05001662 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1663 "9080 BLKGRD: Injecting apptag error: "
1664 "write lba x%lx + x%x oldappTag x%x\n",
1665 (unsigned long)lba, blockoff,
James Smart9a6b09c2012-03-01 22:37:42 -05001666 be16_to_cpu(src->app_tag));
James Smartacd68592012-01-18 16:25:09 -05001667
1668 /*
James Smart9a6b09c2012-03-01 22:37:42 -05001669 * Save the old app_tag so we can
1670 * restore it on completion.
James Smartacd68592012-01-18 16:25:09 -05001671 */
James Smart9a6b09c2012-03-01 22:37:42 -05001672 if (lpfc_cmd) {
1673 lpfc_cmd->prot_data_type =
1674 LPFC_INJERR_APPTAG;
1675 lpfc_cmd->prot_data_segment =
1676 src;
1677 lpfc_cmd->prot_data =
1678 src->app_tag;
1679 }
1680 src->app_tag = cpu_to_be16(0xDEAD);
James Smartacd68592012-01-18 16:25:09 -05001681 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001682 if (phba->lpfc_injerr_wapp_cnt == 0) {
1683 phba->lpfc_injerr_nportid = 0;
1684 phba->lpfc_injerr_lba =
1685 LPFC_INJERR_LBA_OFF;
1686 memset(&phba->lpfc_injerr_wwpn,
1687 0, sizeof(struct lpfc_name));
1688 }
James Smart9a6b09c2012-03-01 22:37:42 -05001689 rc = BG_ERR_TGT | BG_ERR_CHECK;
James Smartacd68592012-01-18 16:25:09 -05001690 break;
1691 }
1692 /* Drop thru */
James Smart9a6b09c2012-03-01 22:37:42 -05001693 case SCSI_PROT_WRITE_INSERT:
1694 /*
1695 * For WRITE_INSERT, force the
1696 * error to be sent on the wire. It should be
1697 * detected by the Target.
1698 */
1699 /* DEAD will be the apptag on the wire */
1700 *apptag = 0xDEAD;
1701 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001702 if (phba->lpfc_injerr_wapp_cnt == 0) {
1703 phba->lpfc_injerr_nportid = 0;
1704 phba->lpfc_injerr_lba =
1705 LPFC_INJERR_LBA_OFF;
1706 memset(&phba->lpfc_injerr_wwpn,
1707 0, sizeof(struct lpfc_name));
1708 }
James Smart9a6b09c2012-03-01 22:37:42 -05001709 rc = BG_ERR_TGT | BG_ERR_CHECK;
1710
1711 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1712 "0813 BLKGRD: Injecting apptag error: "
1713 "write lba x%lx\n", (unsigned long)lba);
1714 break;
James Smartacd68592012-01-18 16:25:09 -05001715 case SCSI_PROT_WRITE_STRIP:
1716 /*
1717 * For WRITE_STRIP and WRITE_PASS,
1718 * force the error on data
1719 * being copied from SLI-Host to SLI-Port.
1720 */
1721 *apptag = 0xDEAD;
1722 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001723 if (phba->lpfc_injerr_wapp_cnt == 0) {
1724 phba->lpfc_injerr_nportid = 0;
1725 phba->lpfc_injerr_lba =
1726 LPFC_INJERR_LBA_OFF;
1727 memset(&phba->lpfc_injerr_wwpn,
1728 0, sizeof(struct lpfc_name));
1729 }
James Smartacd68592012-01-18 16:25:09 -05001730 rc = BG_ERR_INIT;
1731
1732 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1733 "0812 BLKGRD: Injecting apptag error: "
1734 "write lba x%lx\n", (unsigned long)lba);
1735 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001736 }
James Smartacd68592012-01-18 16:25:09 -05001737 }
1738 if (phba->lpfc_injerr_rapp_cnt) {
1739 switch (op) {
1740 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001741 case SCSI_PROT_READ_STRIP:
1742 case SCSI_PROT_READ_PASS:
1743 /*
1744 * For READ_STRIP and READ_PASS, force the
1745 * error on data being read off the wire. It
1746 * should force an IO error to the driver.
1747 */
James Smartf9bb2da2011-10-10 21:34:11 -04001748 *apptag = 0xDEAD;
1749 phba->lpfc_injerr_rapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001750 if (phba->lpfc_injerr_rapp_cnt == 0) {
1751 phba->lpfc_injerr_nportid = 0;
1752 phba->lpfc_injerr_lba =
1753 LPFC_INJERR_LBA_OFF;
1754 memset(&phba->lpfc_injerr_wwpn,
1755 0, sizeof(struct lpfc_name));
1756 }
James Smartacd68592012-01-18 16:25:09 -05001757 rc = BG_ERR_INIT;
James Smartf9bb2da2011-10-10 21:34:11 -04001758
1759 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smartacd68592012-01-18 16:25:09 -05001760 "0814 BLKGRD: Injecting apptag error: "
1761 "read lba x%lx\n", (unsigned long)lba);
1762 break;
1763 }
1764 }
1765 }
1766
1767
1768 /* Should we change the Guard Tag */
1769 if (new_guard) {
1770 if (phba->lpfc_injerr_wgrd_cnt) {
1771 switch (op) {
1772 case SCSI_PROT_WRITE_PASS:
James Smart9a6b09c2012-03-01 22:37:42 -05001773 rc = BG_ERR_CHECK;
James Smartacd68592012-01-18 16:25:09 -05001774 /* Drop thru */
James Smartacd68592012-01-18 16:25:09 -05001775
James Smartacd68592012-01-18 16:25:09 -05001776 case SCSI_PROT_WRITE_INSERT:
1777 /*
1778 * For WRITE_INSERT, force the
1779 * error to be sent on the wire. It should be
1780 * detected by the Target.
1781 */
1782 phba->lpfc_injerr_wgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001783 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1784 phba->lpfc_injerr_nportid = 0;
1785 phba->lpfc_injerr_lba =
1786 LPFC_INJERR_LBA_OFF;
1787 memset(&phba->lpfc_injerr_wwpn,
1788 0, sizeof(struct lpfc_name));
1789 }
James Smartacd68592012-01-18 16:25:09 -05001790
James Smart9a6b09c2012-03-01 22:37:42 -05001791 rc |= BG_ERR_TGT | BG_ERR_SWAP;
James Smartacd68592012-01-18 16:25:09 -05001792 /* Signals the caller to swap CRC->CSUM */
1793
1794 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1795 "0817 BLKGRD: Injecting guard error: "
1796 "write lba x%lx\n", (unsigned long)lba);
1797 break;
James Smart9a6b09c2012-03-01 22:37:42 -05001798 case SCSI_PROT_WRITE_STRIP:
1799 /*
1800 * For WRITE_STRIP and WRITE_PASS,
1801 * force the error on data
1802 * being copied from SLI-Host to SLI-Port.
1803 */
1804 phba->lpfc_injerr_wgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001805 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1806 phba->lpfc_injerr_nportid = 0;
1807 phba->lpfc_injerr_lba =
1808 LPFC_INJERR_LBA_OFF;
1809 memset(&phba->lpfc_injerr_wwpn,
1810 0, sizeof(struct lpfc_name));
1811 }
James Smart9a6b09c2012-03-01 22:37:42 -05001812
1813 rc = BG_ERR_INIT | BG_ERR_SWAP;
1814 /* Signals the caller to swap CRC->CSUM */
1815
1816 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1817 "0816 BLKGRD: Injecting guard error: "
1818 "write lba x%lx\n", (unsigned long)lba);
1819 break;
James Smartacd68592012-01-18 16:25:09 -05001820 }
1821 }
1822 if (phba->lpfc_injerr_rgrd_cnt) {
1823 switch (op) {
1824 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001825 case SCSI_PROT_READ_STRIP:
1826 case SCSI_PROT_READ_PASS:
1827 /*
1828 * For READ_STRIP and READ_PASS, force the
1829 * error on data being read off the wire. It
1830 * should force an IO error to the driver.
1831 */
James Smartacd68592012-01-18 16:25:09 -05001832 phba->lpfc_injerr_rgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001833 if (phba->lpfc_injerr_rgrd_cnt == 0) {
1834 phba->lpfc_injerr_nportid = 0;
1835 phba->lpfc_injerr_lba =
1836 LPFC_INJERR_LBA_OFF;
1837 memset(&phba->lpfc_injerr_wwpn,
1838 0, sizeof(struct lpfc_name));
1839 }
James Smartacd68592012-01-18 16:25:09 -05001840
James Smart9a6b09c2012-03-01 22:37:42 -05001841 rc = BG_ERR_INIT | BG_ERR_SWAP;
James Smartacd68592012-01-18 16:25:09 -05001842 /* Signals the caller to swap CRC->CSUM */
1843
1844 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1845 "0818 BLKGRD: Injecting guard error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001846 "read lba x%lx\n", (unsigned long)lba);
1847 }
1848 }
1849 }
1850
James Smartf9bb2da2011-10-10 21:34:11 -04001851 return rc;
1852}
1853#endif
1854
James Smartacd68592012-01-18 16:25:09 -05001855/**
1856 * lpfc_sc_to_bg_opcodes - Determine the BlockGuard opcodes to be used with
1857 * the specified SCSI command.
1858 * @phba: The Hba for which this call is being executed.
James Smart6c8eea52010-04-06 14:49:53 -04001859 * @sc: The SCSI command to examine
1860 * @txopt: (out) BlockGuard operation for transmitted data
1861 * @rxopt: (out) BlockGuard operation for received data
1862 *
1863 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1864 *
James Smartacd68592012-01-18 16:25:09 -05001865 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001866static int
James Smart6c8eea52010-04-06 14:49:53 -04001867lpfc_sc_to_bg_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1868 uint8_t *txop, uint8_t *rxop)
James Smarte2a0a9d2008-12-04 22:40:02 -05001869{
James Smart6c8eea52010-04-06 14:49:53 -04001870 uint8_t ret = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05001871
James Smart9c6aa9d2013-05-31 17:03:39 -04001872 if (lpfc_cmd_guard_csum(sc)) {
James Smarte2a0a9d2008-12-04 22:40:02 -05001873 switch (scsi_get_prot_op(sc)) {
1874 case SCSI_PROT_READ_INSERT:
1875 case SCSI_PROT_WRITE_STRIP:
James Smart6c8eea52010-04-06 14:49:53 -04001876 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001877 *txop = BG_OP_IN_CSUM_OUT_NODIF;
James Smarte2a0a9d2008-12-04 22:40:02 -05001878 break;
1879
1880 case SCSI_PROT_READ_STRIP:
1881 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001882 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001883 *txop = BG_OP_IN_NODIF_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001884 break;
1885
Martin K. Petersenc6af4042009-09-18 17:32:59 -04001886 case SCSI_PROT_READ_PASS:
1887 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001888 *rxop = BG_OP_IN_CRC_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001889 *txop = BG_OP_IN_CSUM_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001890 break;
1891
James Smarte2a0a9d2008-12-04 22:40:02 -05001892 case SCSI_PROT_NORMAL:
1893 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001894 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001895 "9063 BLKGRD: Bad op/guard:%d/IP combination\n",
1896 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001897 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001898 break;
1899
1900 }
James Smart7c56b9f2011-07-22 18:36:25 -04001901 } else {
James Smarte2a0a9d2008-12-04 22:40:02 -05001902 switch (scsi_get_prot_op(sc)) {
1903 case SCSI_PROT_READ_STRIP:
1904 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001905 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001906 *txop = BG_OP_IN_NODIF_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001907 break;
1908
1909 case SCSI_PROT_READ_PASS:
1910 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001911 *rxop = BG_OP_IN_CRC_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001912 *txop = BG_OP_IN_CRC_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001913 break;
1914
James Smarte2a0a9d2008-12-04 22:40:02 -05001915 case SCSI_PROT_READ_INSERT:
1916 case SCSI_PROT_WRITE_STRIP:
James Smart7c56b9f2011-07-22 18:36:25 -04001917 *rxop = BG_OP_IN_NODIF_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001918 *txop = BG_OP_IN_CRC_OUT_NODIF;
James Smart7c56b9f2011-07-22 18:36:25 -04001919 break;
1920
James Smarte2a0a9d2008-12-04 22:40:02 -05001921 case SCSI_PROT_NORMAL:
1922 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001923 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001924 "9075 BLKGRD: Bad op/guard:%d/CRC combination\n",
1925 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001926 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001927 break;
1928 }
James Smarte2a0a9d2008-12-04 22:40:02 -05001929 }
1930
James Smart6c8eea52010-04-06 14:49:53 -04001931 return ret;
James Smarte2a0a9d2008-12-04 22:40:02 -05001932}
1933
James Smartacd68592012-01-18 16:25:09 -05001934#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1935/**
1936 * lpfc_bg_err_opcodes - reDetermine the BlockGuard opcodes to be used with
1937 * the specified SCSI command in order to force a guard tag error.
1938 * @phba: The Hba for which this call is being executed.
1939 * @sc: The SCSI command to examine
1940 * @txopt: (out) BlockGuard operation for transmitted data
1941 * @rxopt: (out) BlockGuard operation for received data
1942 *
1943 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1944 *
1945 **/
1946static int
1947lpfc_bg_err_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1948 uint8_t *txop, uint8_t *rxop)
1949{
James Smartacd68592012-01-18 16:25:09 -05001950 uint8_t ret = 0;
1951
James Smart9c6aa9d2013-05-31 17:03:39 -04001952 if (lpfc_cmd_guard_csum(sc)) {
James Smartacd68592012-01-18 16:25:09 -05001953 switch (scsi_get_prot_op(sc)) {
1954 case SCSI_PROT_READ_INSERT:
1955 case SCSI_PROT_WRITE_STRIP:
James Smartacd68592012-01-18 16:25:09 -05001956 *rxop = BG_OP_IN_NODIF_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001957 *txop = BG_OP_IN_CRC_OUT_NODIF;
James Smartacd68592012-01-18 16:25:09 -05001958 break;
1959
1960 case SCSI_PROT_READ_STRIP:
1961 case SCSI_PROT_WRITE_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001962 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001963 *txop = BG_OP_IN_NODIF_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001964 break;
1965
1966 case SCSI_PROT_READ_PASS:
1967 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001968 *rxop = BG_OP_IN_CSUM_OUT_CRC;
James Smart9a6b09c2012-03-01 22:37:42 -05001969 *txop = BG_OP_IN_CRC_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001970 break;
1971
1972 case SCSI_PROT_NORMAL:
1973 default:
1974 break;
1975
1976 }
1977 } else {
1978 switch (scsi_get_prot_op(sc)) {
1979 case SCSI_PROT_READ_STRIP:
1980 case SCSI_PROT_WRITE_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001981 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001982 *txop = BG_OP_IN_NODIF_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001983 break;
1984
1985 case SCSI_PROT_READ_PASS:
1986 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001987 *rxop = BG_OP_IN_CSUM_OUT_CSUM;
James Smart9a6b09c2012-03-01 22:37:42 -05001988 *txop = BG_OP_IN_CSUM_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001989 break;
1990
1991 case SCSI_PROT_READ_INSERT:
1992 case SCSI_PROT_WRITE_STRIP:
James Smartacd68592012-01-18 16:25:09 -05001993 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001994 *txop = BG_OP_IN_CSUM_OUT_NODIF;
James Smartacd68592012-01-18 16:25:09 -05001995 break;
1996
1997 case SCSI_PROT_NORMAL:
1998 default:
1999 break;
2000 }
2001 }
2002
2003 return ret;
2004}
2005#endif
2006
2007/**
2008 * lpfc_bg_setup_bpl - Setup BlockGuard BPL with no protection data
2009 * @phba: The Hba for which this call is being executed.
2010 * @sc: pointer to scsi command we're working on
2011 * @bpl: pointer to buffer list for protection groups
2012 * @datacnt: number of segments of data that have been dma mapped
2013 *
2014 * This function sets up BPL buffer list for protection groups of
James Smarte2a0a9d2008-12-04 22:40:02 -05002015 * type LPFC_PG_TYPE_NO_DIF
2016 *
2017 * This is usually used when the HBA is instructed to generate
2018 * DIFs and insert them into data stream (or strip DIF from
2019 * incoming data stream)
2020 *
2021 * The buffer list consists of just one protection group described
2022 * below:
2023 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002024 * start of prot group --> | PDE_5 |
2025 * +-------------------------+
2026 * | PDE_6 |
James Smarte2a0a9d2008-12-04 22:40:02 -05002027 * +-------------------------+
2028 * | Data BDE |
2029 * +-------------------------+
2030 * |more Data BDE's ... (opt)|
2031 * +-------------------------+
2032 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002033 *
2034 * Note: Data s/g buffers have been dma mapped
James Smartacd68592012-01-18 16:25:09 -05002035 *
2036 * Returns the number of BDEs added to the BPL.
2037 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002038static int
2039lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2040 struct ulp_bde64 *bpl, int datasegcnt)
2041{
2042 struct scatterlist *sgde = NULL; /* s/g data entry */
James Smart6c8eea52010-04-06 14:49:53 -04002043 struct lpfc_pde5 *pde5 = NULL;
2044 struct lpfc_pde6 *pde6 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05002045 dma_addr_t physaddr;
James Smart6c8eea52010-04-06 14:49:53 -04002046 int i = 0, num_bde = 0, status;
James Smarte2a0a9d2008-12-04 22:40:02 -05002047 int datadir = sc->sc_data_direction;
James Smart0829a192012-05-09 21:18:12 -04002048#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002049 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002050#endif
James Smartacd68592012-01-18 16:25:09 -05002051 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05002052 uint32_t reftag;
James Smart7c56b9f2011-07-22 18:36:25 -04002053 unsigned blksize;
James Smart6c8eea52010-04-06 14:49:53 -04002054 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05002055
James Smart6c8eea52010-04-06 14:49:53 -04002056 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2057 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05002058 goto out;
2059
James Smart6c8eea52010-04-06 14:49:53 -04002060 /* extract some info from the scsi command for pde*/
James Smarte2a0a9d2008-12-04 22:40:02 -05002061 blksize = lpfc_cmd_blksize(sc);
James Smartacd68592012-01-18 16:25:09 -05002062 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
James Smarte2a0a9d2008-12-04 22:40:02 -05002063
James Smartf9bb2da2011-10-10 21:34:11 -04002064#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002065 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002066 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002067 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002068 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002069 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002070 checking = 0;
2071 }
James Smartf9bb2da2011-10-10 21:34:11 -04002072#endif
2073
James Smart6c8eea52010-04-06 14:49:53 -04002074 /* setup PDE5 with what we have */
2075 pde5 = (struct lpfc_pde5 *) bpl;
2076 memset(pde5, 0, sizeof(struct lpfc_pde5));
2077 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05002078
James Smartbc739052010-08-04 16:11:18 -04002079 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04002080 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04002081 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04002082
James Smart6c8eea52010-04-06 14:49:53 -04002083 /* advance bpl and increment bde count */
2084 num_bde++;
2085 bpl++;
2086 pde6 = (struct lpfc_pde6 *) bpl;
2087
2088 /* setup PDE6 with the rest of the info */
2089 memset(pde6, 0, sizeof(struct lpfc_pde6));
2090 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
2091 bf_set(pde6_optx, pde6, txop);
2092 bf_set(pde6_oprx, pde6, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002093
2094 /*
2095 * We only need to check the data on READs, for WRITEs
2096 * protection data is automatically generated, not checked.
2097 */
James Smart6c8eea52010-04-06 14:49:53 -04002098 if (datadir == DMA_FROM_DEVICE) {
James Smart9c6aa9d2013-05-31 17:03:39 -04002099 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
James Smarta6887e22013-04-17 20:18:07 -04002100 bf_set(pde6_ce, pde6, checking);
2101 else
2102 bf_set(pde6_ce, pde6, 0);
2103
James Smart9c6aa9d2013-05-31 17:03:39 -04002104 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04002105 bf_set(pde6_re, pde6, checking);
2106 else
2107 bf_set(pde6_re, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04002108 }
2109 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04002110 bf_set(pde6_ae, pde6, 0);
2111 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04002112
James Smartbc739052010-08-04 16:11:18 -04002113 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04002114 pde6->word0 = cpu_to_le32(pde6->word0);
2115 pde6->word1 = cpu_to_le32(pde6->word1);
2116 pde6->word2 = cpu_to_le32(pde6->word2);
2117
James Smart6c8eea52010-04-06 14:49:53 -04002118 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05002119 num_bde++;
2120 bpl++;
2121
2122 /* assumption: caller has already run dma_map_sg on command data */
2123 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
2124 physaddr = sg_dma_address(sgde);
2125 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
2126 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
2127 bpl->tus.f.bdeSize = sg_dma_len(sgde);
2128 if (datadir == DMA_TO_DEVICE)
2129 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2130 else
2131 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2132 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2133 bpl++;
2134 num_bde++;
2135 }
2136
2137out:
2138 return num_bde;
2139}
2140
James Smartacd68592012-01-18 16:25:09 -05002141/**
2142 * lpfc_bg_setup_bpl_prot - Setup BlockGuard BPL with protection data
2143 * @phba: The Hba for which this call is being executed.
2144 * @sc: pointer to scsi command we're working on
2145 * @bpl: pointer to buffer list for protection groups
2146 * @datacnt: number of segments of data that have been dma mapped
2147 * @protcnt: number of segment of protection data that have been dma mapped
2148 *
2149 * This function sets up BPL buffer list for protection groups of
2150 * type LPFC_PG_TYPE_DIF
James Smarte2a0a9d2008-12-04 22:40:02 -05002151 *
2152 * This is usually used when DIFs are in their own buffers,
2153 * separate from the data. The HBA can then by instructed
2154 * to place the DIFs in the outgoing stream. For read operations,
2155 * The HBA could extract the DIFs and place it in DIF buffers.
2156 *
2157 * The buffer list for this type consists of one or more of the
2158 * protection groups described below:
2159 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002160 * start of first prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05002161 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002162 * | PDE_6 |
2163 * +-------------------------+
2164 * | PDE_7 (Prot BDE) |
James Smarte2a0a9d2008-12-04 22:40:02 -05002165 * +-------------------------+
2166 * | Data BDE |
2167 * +-------------------------+
2168 * |more Data BDE's ... (opt)|
2169 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002170 * start of new prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05002171 * +-------------------------+
2172 * | ... |
2173 * +-------------------------+
2174 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002175 * Note: It is assumed that both data and protection s/g buffers have been
2176 * mapped for DMA
James Smartacd68592012-01-18 16:25:09 -05002177 *
2178 * Returns the number of BDEs added to the BPL.
2179 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002180static int
2181lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2182 struct ulp_bde64 *bpl, int datacnt, int protcnt)
2183{
2184 struct scatterlist *sgde = NULL; /* s/g data entry */
2185 struct scatterlist *sgpe = NULL; /* s/g prot entry */
James Smart6c8eea52010-04-06 14:49:53 -04002186 struct lpfc_pde5 *pde5 = NULL;
2187 struct lpfc_pde6 *pde6 = NULL;
James Smart7f860592011-03-11 16:05:52 -05002188 struct lpfc_pde7 *pde7 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05002189 dma_addr_t dataphysaddr, protphysaddr;
2190 unsigned short curr_data = 0, curr_prot = 0;
James Smart7f860592011-03-11 16:05:52 -05002191 unsigned int split_offset;
2192 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
James Smarte2a0a9d2008-12-04 22:40:02 -05002193 unsigned int protgrp_blks, protgrp_bytes;
2194 unsigned int remainder, subtotal;
James Smart6c8eea52010-04-06 14:49:53 -04002195 int status;
James Smarte2a0a9d2008-12-04 22:40:02 -05002196 int datadir = sc->sc_data_direction;
2197 unsigned char pgdone = 0, alldone = 0;
2198 unsigned blksize;
James Smart0829a192012-05-09 21:18:12 -04002199#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002200 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002201#endif
James Smartacd68592012-01-18 16:25:09 -05002202 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05002203 uint32_t reftag;
James Smart6c8eea52010-04-06 14:49:53 -04002204 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05002205 int num_bde = 0;
2206
2207 sgpe = scsi_prot_sglist(sc);
2208 sgde = scsi_sglist(sc);
2209
2210 if (!sgpe || !sgde) {
2211 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2212 "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
2213 sgpe, sgde);
2214 return 0;
2215 }
2216
James Smart6c8eea52010-04-06 14:49:53 -04002217 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2218 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05002219 goto out;
2220
James Smart6c8eea52010-04-06 14:49:53 -04002221 /* extract some info from the scsi command */
James Smarte2a0a9d2008-12-04 22:40:02 -05002222 blksize = lpfc_cmd_blksize(sc);
James Smartacd68592012-01-18 16:25:09 -05002223 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
James Smarte2a0a9d2008-12-04 22:40:02 -05002224
James Smartf9bb2da2011-10-10 21:34:11 -04002225#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002226 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002227 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002228 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002229 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002230 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002231 checking = 0;
2232 }
James Smartf9bb2da2011-10-10 21:34:11 -04002233#endif
2234
James Smarte2a0a9d2008-12-04 22:40:02 -05002235 split_offset = 0;
2236 do {
James Smart96f70772013-04-17 20:16:15 -04002237 /* Check to see if we ran out of space */
2238 if (num_bde >= (phba->cfg_total_seg_cnt - 2))
2239 return num_bde + 3;
2240
James Smart6c8eea52010-04-06 14:49:53 -04002241 /* setup PDE5 with what we have */
2242 pde5 = (struct lpfc_pde5 *) bpl;
2243 memset(pde5, 0, sizeof(struct lpfc_pde5));
2244 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05002245
James Smartbc739052010-08-04 16:11:18 -04002246 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04002247 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04002248 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04002249
James Smart6c8eea52010-04-06 14:49:53 -04002250 /* advance bpl and increment bde count */
2251 num_bde++;
2252 bpl++;
2253 pde6 = (struct lpfc_pde6 *) bpl;
James Smarte2a0a9d2008-12-04 22:40:02 -05002254
James Smart6c8eea52010-04-06 14:49:53 -04002255 /* setup PDE6 with the rest of the info */
2256 memset(pde6, 0, sizeof(struct lpfc_pde6));
2257 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
2258 bf_set(pde6_optx, pde6, txop);
2259 bf_set(pde6_oprx, pde6, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002260
James Smart9c6aa9d2013-05-31 17:03:39 -04002261 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
James Smarta6887e22013-04-17 20:18:07 -04002262 bf_set(pde6_ce, pde6, checking);
2263 else
2264 bf_set(pde6_ce, pde6, 0);
2265
James Smart9c6aa9d2013-05-31 17:03:39 -04002266 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04002267 bf_set(pde6_re, pde6, checking);
2268 else
2269 bf_set(pde6_re, pde6, 0);
2270
James Smart6c8eea52010-04-06 14:49:53 -04002271 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04002272 bf_set(pde6_ae, pde6, 0);
2273 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04002274
James Smartbc739052010-08-04 16:11:18 -04002275 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04002276 pde6->word0 = cpu_to_le32(pde6->word0);
2277 pde6->word1 = cpu_to_le32(pde6->word1);
2278 pde6->word2 = cpu_to_le32(pde6->word2);
2279
James Smart6c8eea52010-04-06 14:49:53 -04002280 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05002281 num_bde++;
2282 bpl++;
2283
2284 /* setup the first BDE that points to protection buffer */
James Smart7f860592011-03-11 16:05:52 -05002285 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2286 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
James Smarte2a0a9d2008-12-04 22:40:02 -05002287
James Smarte2a0a9d2008-12-04 22:40:02 -05002288 /* must be integer multiple of the DIF block length */
2289 BUG_ON(protgroup_len % 8);
2290
James Smart7f860592011-03-11 16:05:52 -05002291 pde7 = (struct lpfc_pde7 *) bpl;
2292 memset(pde7, 0, sizeof(struct lpfc_pde7));
2293 bf_set(pde7_type, pde7, LPFC_PDE7_DESCRIPTOR);
2294
James Smart7c56b9f2011-07-22 18:36:25 -04002295 pde7->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
2296 pde7->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
James Smart7f860592011-03-11 16:05:52 -05002297
James Smarte2a0a9d2008-12-04 22:40:02 -05002298 protgrp_blks = protgroup_len / 8;
2299 protgrp_bytes = protgrp_blks * blksize;
2300
James Smart7f860592011-03-11 16:05:52 -05002301 /* check if this pde is crossing the 4K boundary; if so split */
2302 if ((pde7->addrLow & 0xfff) + protgroup_len > 0x1000) {
2303 protgroup_remainder = 0x1000 - (pde7->addrLow & 0xfff);
2304 protgroup_offset += protgroup_remainder;
2305 protgrp_blks = protgroup_remainder / 8;
James Smart7c56b9f2011-07-22 18:36:25 -04002306 protgrp_bytes = protgrp_blks * blksize;
James Smart7f860592011-03-11 16:05:52 -05002307 } else {
2308 protgroup_offset = 0;
2309 curr_prot++;
2310 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002311
James Smarte2a0a9d2008-12-04 22:40:02 -05002312 num_bde++;
2313
2314 /* setup BDE's for data blocks associated with DIF data */
2315 pgdone = 0;
2316 subtotal = 0; /* total bytes processed for current prot grp */
2317 while (!pgdone) {
James Smart96f70772013-04-17 20:16:15 -04002318 /* Check to see if we ran out of space */
2319 if (num_bde >= phba->cfg_total_seg_cnt)
2320 return num_bde + 1;
2321
James Smarte2a0a9d2008-12-04 22:40:02 -05002322 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -04002323 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2324 "9065 BLKGRD:%s Invalid data segment\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002325 __func__);
2326 return 0;
2327 }
2328 bpl++;
2329 dataphysaddr = sg_dma_address(sgde) + split_offset;
2330 bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
2331 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
2332
2333 remainder = sg_dma_len(sgde) - split_offset;
2334
2335 if ((subtotal + remainder) <= protgrp_bytes) {
2336 /* we can use this whole buffer */
2337 bpl->tus.f.bdeSize = remainder;
2338 split_offset = 0;
2339
2340 if ((subtotal + remainder) == protgrp_bytes)
2341 pgdone = 1;
2342 } else {
2343 /* must split this buffer with next prot grp */
2344 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
2345 split_offset += bpl->tus.f.bdeSize;
2346 }
2347
2348 subtotal += bpl->tus.f.bdeSize;
2349
2350 if (datadir == DMA_TO_DEVICE)
2351 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2352 else
2353 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2354 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2355
2356 num_bde++;
2357 curr_data++;
2358
2359 if (split_offset)
2360 break;
2361
2362 /* Move to the next s/g segment if possible */
2363 sgde = sg_next(sgde);
James Smart6c8eea52010-04-06 14:49:53 -04002364
James Smarte2a0a9d2008-12-04 22:40:02 -05002365 }
2366
James Smart7f860592011-03-11 16:05:52 -05002367 if (protgroup_offset) {
2368 /* update the reference tag */
2369 reftag += protgrp_blks;
2370 bpl++;
2371 continue;
2372 }
2373
James Smarte2a0a9d2008-12-04 22:40:02 -05002374 /* are we done ? */
2375 if (curr_prot == protcnt) {
2376 alldone = 1;
2377 } else if (curr_prot < protcnt) {
2378 /* advance to next prot buffer */
2379 sgpe = sg_next(sgpe);
2380 bpl++;
2381
2382 /* update the reference tag */
2383 reftag += protgrp_blks;
2384 } else {
2385 /* if we're here, we have a bug */
James Smart6a9c52c2009-10-02 15:16:51 -04002386 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2387 "9054 BLKGRD: bug in %s\n", __func__);
James Smarte2a0a9d2008-12-04 22:40:02 -05002388 }
2389
2390 } while (!alldone);
James Smarte2a0a9d2008-12-04 22:40:02 -05002391out:
2392
James Smarte2a0a9d2008-12-04 22:40:02 -05002393 return num_bde;
2394}
James Smart7f860592011-03-11 16:05:52 -05002395
James Smartacd68592012-01-18 16:25:09 -05002396/**
2397 * lpfc_bg_setup_sgl - Setup BlockGuard SGL with no protection data
2398 * @phba: The Hba for which this call is being executed.
2399 * @sc: pointer to scsi command we're working on
2400 * @sgl: pointer to buffer list for protection groups
2401 * @datacnt: number of segments of data that have been dma mapped
2402 *
2403 * This function sets up SGL buffer list for protection groups of
2404 * type LPFC_PG_TYPE_NO_DIF
2405 *
2406 * This is usually used when the HBA is instructed to generate
2407 * DIFs and insert them into data stream (or strip DIF from
2408 * incoming data stream)
2409 *
2410 * The buffer list consists of just one protection group described
2411 * below:
2412 * +-------------------------+
2413 * start of prot group --> | DI_SEED |
2414 * +-------------------------+
2415 * | Data SGE |
2416 * +-------------------------+
2417 * |more Data SGE's ... (opt)|
2418 * +-------------------------+
2419 *
2420 *
2421 * Note: Data s/g buffers have been dma mapped
2422 *
2423 * Returns the number of SGEs added to the SGL.
2424 **/
2425static int
2426lpfc_bg_setup_sgl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2427 struct sli4_sge *sgl, int datasegcnt)
2428{
2429 struct scatterlist *sgde = NULL; /* s/g data entry */
2430 struct sli4_sge_diseed *diseed = NULL;
2431 dma_addr_t physaddr;
2432 int i = 0, num_sge = 0, status;
James Smartacd68592012-01-18 16:25:09 -05002433 uint32_t reftag;
2434 unsigned blksize;
2435 uint8_t txop, rxop;
James Smart0829a192012-05-09 21:18:12 -04002436#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002437 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002438#endif
James Smartacd68592012-01-18 16:25:09 -05002439 uint32_t checking = 1;
2440 uint32_t dma_len;
2441 uint32_t dma_offset = 0;
2442
2443 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2444 if (status)
2445 goto out;
2446
2447 /* extract some info from the scsi command for pde*/
2448 blksize = lpfc_cmd_blksize(sc);
2449 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2450
2451#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002452 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002453 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002454 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002455 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002456 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002457 checking = 0;
2458 }
2459#endif
2460
2461 /* setup DISEED with what we have */
2462 diseed = (struct sli4_sge_diseed *) sgl;
2463 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2464 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2465
2466 /* Endianness conversion if necessary */
2467 diseed->ref_tag = cpu_to_le32(reftag);
2468 diseed->ref_tag_tran = diseed->ref_tag;
2469
James Smarta6887e22013-04-17 20:18:07 -04002470 /*
2471 * We only need to check the data on READs, for WRITEs
2472 * protection data is automatically generated, not checked.
2473 */
2474 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
James Smart9c6aa9d2013-05-31 17:03:39 -04002475 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
James Smarta6887e22013-04-17 20:18:07 -04002476 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2477 else
2478 bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2479
James Smart9c6aa9d2013-05-31 17:03:39 -04002480 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04002481 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2482 else
2483 bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2484 }
2485
James Smartacd68592012-01-18 16:25:09 -05002486 /* setup DISEED with the rest of the info */
2487 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2488 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002489
James Smartacd68592012-01-18 16:25:09 -05002490 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2491 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2492
2493 /* Endianness conversion if necessary for DISEED */
2494 diseed->word2 = cpu_to_le32(diseed->word2);
2495 diseed->word3 = cpu_to_le32(diseed->word3);
2496
2497 /* advance bpl and increment sge count */
2498 num_sge++;
2499 sgl++;
2500
2501 /* assumption: caller has already run dma_map_sg on command data */
2502 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
2503 physaddr = sg_dma_address(sgde);
2504 dma_len = sg_dma_len(sgde);
2505 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
2506 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
2507 if ((i + 1) == datasegcnt)
2508 bf_set(lpfc_sli4_sge_last, sgl, 1);
2509 else
2510 bf_set(lpfc_sli4_sge_last, sgl, 0);
2511 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2512 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2513
2514 sgl->sge_len = cpu_to_le32(dma_len);
2515 dma_offset += dma_len;
2516
2517 sgl++;
2518 num_sge++;
2519 }
2520
2521out:
2522 return num_sge;
2523}
2524
2525/**
2526 * lpfc_bg_setup_sgl_prot - Setup BlockGuard SGL with protection data
2527 * @phba: The Hba for which this call is being executed.
2528 * @sc: pointer to scsi command we're working on
2529 * @sgl: pointer to buffer list for protection groups
2530 * @datacnt: number of segments of data that have been dma mapped
2531 * @protcnt: number of segment of protection data that have been dma mapped
2532 *
2533 * This function sets up SGL buffer list for protection groups of
2534 * type LPFC_PG_TYPE_DIF
2535 *
2536 * This is usually used when DIFs are in their own buffers,
2537 * separate from the data. The HBA can then by instructed
2538 * to place the DIFs in the outgoing stream. For read operations,
2539 * The HBA could extract the DIFs and place it in DIF buffers.
2540 *
2541 * The buffer list for this type consists of one or more of the
2542 * protection groups described below:
2543 * +-------------------------+
2544 * start of first prot group --> | DISEED |
2545 * +-------------------------+
2546 * | DIF (Prot SGE) |
2547 * +-------------------------+
2548 * | Data SGE |
2549 * +-------------------------+
2550 * |more Data SGE's ... (opt)|
2551 * +-------------------------+
2552 * start of new prot group --> | DISEED |
2553 * +-------------------------+
2554 * | ... |
2555 * +-------------------------+
2556 *
2557 * Note: It is assumed that both data and protection s/g buffers have been
2558 * mapped for DMA
2559 *
2560 * Returns the number of SGEs added to the SGL.
2561 **/
2562static int
2563lpfc_bg_setup_sgl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2564 struct sli4_sge *sgl, int datacnt, int protcnt)
2565{
2566 struct scatterlist *sgde = NULL; /* s/g data entry */
2567 struct scatterlist *sgpe = NULL; /* s/g prot entry */
2568 struct sli4_sge_diseed *diseed = NULL;
2569 dma_addr_t dataphysaddr, protphysaddr;
2570 unsigned short curr_data = 0, curr_prot = 0;
2571 unsigned int split_offset;
2572 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
2573 unsigned int protgrp_blks, protgrp_bytes;
2574 unsigned int remainder, subtotal;
2575 int status;
2576 unsigned char pgdone = 0, alldone = 0;
2577 unsigned blksize;
2578 uint32_t reftag;
2579 uint8_t txop, rxop;
2580 uint32_t dma_len;
James Smart0829a192012-05-09 21:18:12 -04002581#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002582 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002583#endif
James Smartacd68592012-01-18 16:25:09 -05002584 uint32_t checking = 1;
2585 uint32_t dma_offset = 0;
2586 int num_sge = 0;
2587
2588 sgpe = scsi_prot_sglist(sc);
2589 sgde = scsi_sglist(sc);
2590
2591 if (!sgpe || !sgde) {
2592 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2593 "9082 Invalid s/g entry: data=0x%p prot=0x%p\n",
2594 sgpe, sgde);
2595 return 0;
2596 }
2597
2598 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2599 if (status)
2600 goto out;
2601
2602 /* extract some info from the scsi command */
2603 blksize = lpfc_cmd_blksize(sc);
2604 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2605
2606#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002607 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002608 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002609 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002610 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002611 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002612 checking = 0;
2613 }
2614#endif
2615
2616 split_offset = 0;
2617 do {
James Smart96f70772013-04-17 20:16:15 -04002618 /* Check to see if we ran out of space */
2619 if (num_sge >= (phba->cfg_total_seg_cnt - 2))
2620 return num_sge + 3;
2621
James Smartacd68592012-01-18 16:25:09 -05002622 /* setup DISEED with what we have */
2623 diseed = (struct sli4_sge_diseed *) sgl;
2624 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2625 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2626
2627 /* Endianness conversion if necessary */
2628 diseed->ref_tag = cpu_to_le32(reftag);
2629 diseed->ref_tag_tran = diseed->ref_tag;
2630
James Smart9c6aa9d2013-05-31 17:03:39 -04002631 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD)) {
James Smarta6887e22013-04-17 20:18:07 -04002632 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2633
2634 } else {
2635 bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2636 /*
2637 * When in this mode, the hardware will replace
2638 * the guard tag from the host with a
2639 * newly generated good CRC for the wire.
2640 * Switch to raw mode here to avoid this
2641 * behavior. What the host sends gets put on the wire.
2642 */
2643 if (txop == BG_OP_IN_CRC_OUT_CRC) {
2644 txop = BG_OP_RAW_MODE;
2645 rxop = BG_OP_RAW_MODE;
2646 }
2647 }
2648
2649
James Smart9c6aa9d2013-05-31 17:03:39 -04002650 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04002651 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2652 else
2653 bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2654
James Smartacd68592012-01-18 16:25:09 -05002655 /* setup DISEED with the rest of the info */
2656 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2657 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002658
James Smartacd68592012-01-18 16:25:09 -05002659 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2660 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2661
2662 /* Endianness conversion if necessary for DISEED */
2663 diseed->word2 = cpu_to_le32(diseed->word2);
2664 diseed->word3 = cpu_to_le32(diseed->word3);
2665
2666 /* advance sgl and increment bde count */
2667 num_sge++;
2668 sgl++;
2669
2670 /* setup the first BDE that points to protection buffer */
2671 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2672 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
2673
2674 /* must be integer multiple of the DIF block length */
2675 BUG_ON(protgroup_len % 8);
2676
2677 /* Now setup DIF SGE */
2678 sgl->word2 = 0;
2679 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DIF);
2680 sgl->addr_hi = le32_to_cpu(putPaddrHigh(protphysaddr));
2681 sgl->addr_lo = le32_to_cpu(putPaddrLow(protphysaddr));
2682 sgl->word2 = cpu_to_le32(sgl->word2);
2683
2684 protgrp_blks = protgroup_len / 8;
2685 protgrp_bytes = protgrp_blks * blksize;
2686
2687 /* check if DIF SGE is crossing the 4K boundary; if so split */
2688 if ((sgl->addr_lo & 0xfff) + protgroup_len > 0x1000) {
2689 protgroup_remainder = 0x1000 - (sgl->addr_lo & 0xfff);
2690 protgroup_offset += protgroup_remainder;
2691 protgrp_blks = protgroup_remainder / 8;
2692 protgrp_bytes = protgrp_blks * blksize;
2693 } else {
2694 protgroup_offset = 0;
2695 curr_prot++;
2696 }
2697
2698 num_sge++;
2699
2700 /* setup SGE's for data blocks associated with DIF data */
2701 pgdone = 0;
2702 subtotal = 0; /* total bytes processed for current prot grp */
2703 while (!pgdone) {
James Smart96f70772013-04-17 20:16:15 -04002704 /* Check to see if we ran out of space */
2705 if (num_sge >= phba->cfg_total_seg_cnt)
2706 return num_sge + 1;
2707
James Smartacd68592012-01-18 16:25:09 -05002708 if (!sgde) {
2709 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2710 "9086 BLKGRD:%s Invalid data segment\n",
2711 __func__);
2712 return 0;
2713 }
2714 sgl++;
2715 dataphysaddr = sg_dma_address(sgde) + split_offset;
2716
2717 remainder = sg_dma_len(sgde) - split_offset;
2718
2719 if ((subtotal + remainder) <= protgrp_bytes) {
2720 /* we can use this whole buffer */
2721 dma_len = remainder;
2722 split_offset = 0;
2723
2724 if ((subtotal + remainder) == protgrp_bytes)
2725 pgdone = 1;
2726 } else {
2727 /* must split this buffer with next prot grp */
2728 dma_len = protgrp_bytes - subtotal;
2729 split_offset += dma_len;
2730 }
2731
2732 subtotal += dma_len;
2733
2734 sgl->addr_lo = cpu_to_le32(putPaddrLow(dataphysaddr));
2735 sgl->addr_hi = cpu_to_le32(putPaddrHigh(dataphysaddr));
2736 bf_set(lpfc_sli4_sge_last, sgl, 0);
2737 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2738 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2739
2740 sgl->sge_len = cpu_to_le32(dma_len);
2741 dma_offset += dma_len;
2742
2743 num_sge++;
2744 curr_data++;
2745
2746 if (split_offset)
2747 break;
2748
2749 /* Move to the next s/g segment if possible */
2750 sgde = sg_next(sgde);
2751 }
2752
2753 if (protgroup_offset) {
2754 /* update the reference tag */
2755 reftag += protgrp_blks;
2756 sgl++;
2757 continue;
2758 }
2759
2760 /* are we done ? */
2761 if (curr_prot == protcnt) {
2762 bf_set(lpfc_sli4_sge_last, sgl, 1);
2763 alldone = 1;
2764 } else if (curr_prot < protcnt) {
2765 /* advance to next prot buffer */
2766 sgpe = sg_next(sgpe);
2767 sgl++;
2768
2769 /* update the reference tag */
2770 reftag += protgrp_blks;
2771 } else {
2772 /* if we're here, we have a bug */
2773 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2774 "9085 BLKGRD: bug in %s\n", __func__);
2775 }
2776
2777 } while (!alldone);
2778
2779out:
2780
2781 return num_sge;
2782}
2783
2784/**
2785 * lpfc_prot_group_type - Get prtotection group type of SCSI command
2786 * @phba: The Hba for which this call is being executed.
2787 * @sc: pointer to scsi command we're working on
2788 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002789 * Given a SCSI command that supports DIF, determine composition of protection
2790 * groups involved in setting up buffer lists
2791 *
James Smartacd68592012-01-18 16:25:09 -05002792 * Returns: Protection group type (with or without DIF)
2793 *
2794 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002795static int
2796lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
2797{
2798 int ret = LPFC_PG_TYPE_INVALID;
2799 unsigned char op = scsi_get_prot_op(sc);
2800
2801 switch (op) {
2802 case SCSI_PROT_READ_STRIP:
2803 case SCSI_PROT_WRITE_INSERT:
2804 ret = LPFC_PG_TYPE_NO_DIF;
2805 break;
2806 case SCSI_PROT_READ_INSERT:
2807 case SCSI_PROT_WRITE_STRIP:
2808 case SCSI_PROT_READ_PASS:
2809 case SCSI_PROT_WRITE_PASS:
James Smarte2a0a9d2008-12-04 22:40:02 -05002810 ret = LPFC_PG_TYPE_DIF_BUF;
2811 break;
2812 default:
James Smart9c6aa9d2013-05-31 17:03:39 -04002813 if (phba)
2814 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2815 "9021 Unsupported protection op:%d\n",
2816 op);
James Smarte2a0a9d2008-12-04 22:40:02 -05002817 break;
2818 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002819 return ret;
2820}
2821
James Smartacd68592012-01-18 16:25:09 -05002822/**
James Smarta6887e22013-04-17 20:18:07 -04002823 * lpfc_bg_scsi_adjust_dl - Adjust SCSI data length for BlockGuard
2824 * @phba: The Hba for which this call is being executed.
2825 * @lpfc_cmd: The scsi buffer which is going to be adjusted.
2826 *
2827 * Adjust the data length to account for how much data
2828 * is actually on the wire.
2829 *
2830 * returns the adjusted data length
2831 **/
2832static int
2833lpfc_bg_scsi_adjust_dl(struct lpfc_hba *phba,
2834 struct lpfc_scsi_buf *lpfc_cmd)
2835{
2836 struct scsi_cmnd *sc = lpfc_cmd->pCmd;
2837 int fcpdl;
2838
2839 fcpdl = scsi_bufflen(sc);
2840
2841 /* Check if there is protection data on the wire */
2842 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
James Smart9c6aa9d2013-05-31 17:03:39 -04002843 /* Read check for protection data */
James Smarta6887e22013-04-17 20:18:07 -04002844 if (scsi_get_prot_op(sc) == SCSI_PROT_READ_INSERT)
2845 return fcpdl;
2846
2847 } else {
James Smart9c6aa9d2013-05-31 17:03:39 -04002848 /* Write check for protection data */
James Smarta6887e22013-04-17 20:18:07 -04002849 if (scsi_get_prot_op(sc) == SCSI_PROT_WRITE_STRIP)
2850 return fcpdl;
2851 }
2852
2853 /*
2854 * If we are in DIF Type 1 mode every data block has a 8 byte
James Smart9c6aa9d2013-05-31 17:03:39 -04002855 * DIF (trailer) attached to it. Must ajust FCP data length
2856 * to account for the protection data.
James Smarta6887e22013-04-17 20:18:07 -04002857 */
James Smart9c6aa9d2013-05-31 17:03:39 -04002858 fcpdl += (fcpdl / lpfc_cmd_blksize(sc)) * 8;
James Smarta6887e22013-04-17 20:18:07 -04002859
2860 return fcpdl;
2861}
2862
2863/**
James Smartacd68592012-01-18 16:25:09 -05002864 * lpfc_bg_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
2865 * @phba: The Hba for which this call is being executed.
2866 * @lpfc_cmd: The scsi buffer which is going to be prep'ed.
2867 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002868 * This is the protection/DIF aware version of
2869 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
2870 * two functions eventually, but for now, it's here
James Smartacd68592012-01-18 16:25:09 -05002871 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002872static int
James Smartacd68592012-01-18 16:25:09 -05002873lpfc_bg_scsi_prep_dma_buf_s3(struct lpfc_hba *phba,
James Smarte2a0a9d2008-12-04 22:40:02 -05002874 struct lpfc_scsi_buf *lpfc_cmd)
2875{
2876 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2877 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2878 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
2879 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2880 uint32_t num_bde = 0;
2881 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
2882 int prot_group_type = 0;
James Smarta6887e22013-04-17 20:18:07 -04002883 int fcpdl;
James Smarte2a0a9d2008-12-04 22:40:02 -05002884
2885 /*
2886 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
2887 * fcp_rsp regions to the first data bde entry
2888 */
2889 bpl += 2;
2890 if (scsi_sg_count(scsi_cmnd)) {
2891 /*
2892 * The driver stores the segment count returned from pci_map_sg
2893 * because this a count of dma-mappings used to map the use_sg
2894 * pages. They are not guaranteed to be the same for those
2895 * architectures that implement an IOMMU.
2896 */
2897 datasegcnt = dma_map_sg(&phba->pcidev->dev,
2898 scsi_sglist(scsi_cmnd),
2899 scsi_sg_count(scsi_cmnd), datadir);
2900 if (unlikely(!datasegcnt))
2901 return 1;
2902
2903 lpfc_cmd->seg_cnt = datasegcnt;
James Smart96f70772013-04-17 20:16:15 -04002904
2905 /* First check if data segment count from SCSI Layer is good */
2906 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt)
2907 goto err;
James Smarte2a0a9d2008-12-04 22:40:02 -05002908
2909 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
2910
2911 switch (prot_group_type) {
2912 case LPFC_PG_TYPE_NO_DIF:
James Smart96f70772013-04-17 20:16:15 -04002913
2914 /* Here we need to add a PDE5 and PDE6 to the count */
2915 if ((lpfc_cmd->seg_cnt + 2) > phba->cfg_total_seg_cnt)
2916 goto err;
2917
James Smarte2a0a9d2008-12-04 22:40:02 -05002918 num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
2919 datasegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002920 /* we should have 2 or more entries in buffer list */
James Smarte2a0a9d2008-12-04 22:40:02 -05002921 if (num_bde < 2)
2922 goto err;
2923 break;
James Smart96f70772013-04-17 20:16:15 -04002924
2925 case LPFC_PG_TYPE_DIF_BUF:
James Smarte2a0a9d2008-12-04 22:40:02 -05002926 /*
2927 * This type indicates that protection buffers are
2928 * passed to the driver, so that needs to be prepared
2929 * for DMA
2930 */
2931 protsegcnt = dma_map_sg(&phba->pcidev->dev,
2932 scsi_prot_sglist(scsi_cmnd),
2933 scsi_prot_sg_count(scsi_cmnd), datadir);
2934 if (unlikely(!protsegcnt)) {
2935 scsi_dma_unmap(scsi_cmnd);
2936 return 1;
2937 }
2938
2939 lpfc_cmd->prot_seg_cnt = protsegcnt;
James Smart96f70772013-04-17 20:16:15 -04002940
2941 /*
2942 * There is a minimun of 4 BPLs used for every
2943 * protection data segment.
2944 */
2945 if ((lpfc_cmd->prot_seg_cnt * 4) >
2946 (phba->cfg_total_seg_cnt - 2))
2947 goto err;
James Smarte2a0a9d2008-12-04 22:40:02 -05002948
2949 num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
2950 datasegcnt, protsegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002951 /* we should have 3 or more entries in buffer list */
James Smart96f70772013-04-17 20:16:15 -04002952 if ((num_bde < 3) ||
2953 (num_bde > phba->cfg_total_seg_cnt))
James Smarte2a0a9d2008-12-04 22:40:02 -05002954 goto err;
2955 break;
James Smart96f70772013-04-17 20:16:15 -04002956
James Smarte2a0a9d2008-12-04 22:40:02 -05002957 case LPFC_PG_TYPE_INVALID:
2958 default:
James Smart96f70772013-04-17 20:16:15 -04002959 scsi_dma_unmap(scsi_cmnd);
2960 lpfc_cmd->seg_cnt = 0;
2961
James Smarte2a0a9d2008-12-04 22:40:02 -05002962 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2963 "9022 Unexpected protection group %i\n",
2964 prot_group_type);
2965 return 1;
2966 }
2967 }
2968
2969 /*
2970 * Finish initializing those IOCB fields that are dependent on the
2971 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
2972 * reinitialized since all iocb memory resources are used many times
2973 * for transmit, receive, and continuation bpl's.
2974 */
2975 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
2976 iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
2977 iocb_cmd->ulpBdeCount = 1;
2978 iocb_cmd->ulpLe = 1;
2979
James Smarta6887e22013-04-17 20:18:07 -04002980 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002981 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
2982
2983 /*
2984 * Due to difference in data length between DIF/non-DIF paths,
2985 * we need to set word 4 of IOCB here
2986 */
2987 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
2988
2989 return 0;
2990err:
James Smart96f70772013-04-17 20:16:15 -04002991 if (lpfc_cmd->seg_cnt)
2992 scsi_dma_unmap(scsi_cmnd);
2993 if (lpfc_cmd->prot_seg_cnt)
2994 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
2995 scsi_prot_sg_count(scsi_cmnd),
2996 scsi_cmnd->sc_data_direction);
2997
James Smarte2a0a9d2008-12-04 22:40:02 -05002998 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart96f70772013-04-17 20:16:15 -04002999 "9023 Cannot setup S/G List for HBA"
3000 "IO segs %d/%d BPL %d SCSI %d: %d %d\n",
3001 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
3002 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
James Smarte2a0a9d2008-12-04 22:40:02 -05003003 prot_group_type, num_bde);
James Smart96f70772013-04-17 20:16:15 -04003004
3005 lpfc_cmd->seg_cnt = 0;
3006 lpfc_cmd->prot_seg_cnt = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05003007 return 1;
3008}
3009
3010/*
James Smart737d4242013-04-17 20:14:49 -04003011 * This function calcuates the T10 DIF guard tag
3012 * on the specified data using a CRC algorithmn
3013 * using crc_t10dif.
3014 */
3015uint16_t
3016lpfc_bg_crc(uint8_t *data, int count)
3017{
3018 uint16_t crc = 0;
3019 uint16_t x;
3020
3021 crc = crc_t10dif(data, count);
3022 x = cpu_to_be16(crc);
3023 return x;
3024}
3025
3026/*
3027 * This function calcuates the T10 DIF guard tag
3028 * on the specified data using a CSUM algorithmn
3029 * using ip_compute_csum.
3030 */
3031uint16_t
3032lpfc_bg_csum(uint8_t *data, int count)
3033{
3034 uint16_t ret;
3035
3036 ret = ip_compute_csum(data, count);
3037 return ret;
3038}
3039
3040/*
3041 * This function examines the protection data to try to determine
3042 * what type of T10-DIF error occurred.
3043 */
3044void
3045lpfc_calc_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3046{
3047 struct scatterlist *sgpe; /* s/g prot entry */
3048 struct scatterlist *sgde; /* s/g data entry */
3049 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
3050 struct scsi_dif_tuple *src = NULL;
3051 uint8_t *data_src = NULL;
3052 uint16_t guard_tag, guard_type;
3053 uint16_t start_app_tag, app_tag;
3054 uint32_t start_ref_tag, ref_tag;
3055 int prot, protsegcnt;
3056 int err_type, len, data_len;
3057 int chk_ref, chk_app, chk_guard;
3058 uint16_t sum;
3059 unsigned blksize;
3060
3061 err_type = BGS_GUARD_ERR_MASK;
3062 sum = 0;
3063 guard_tag = 0;
3064
3065 /* First check to see if there is protection data to examine */
3066 prot = scsi_get_prot_op(cmd);
3067 if ((prot == SCSI_PROT_READ_STRIP) ||
3068 (prot == SCSI_PROT_WRITE_INSERT) ||
3069 (prot == SCSI_PROT_NORMAL))
3070 goto out;
3071
3072 /* Currently the driver just supports ref_tag and guard_tag checking */
3073 chk_ref = 1;
3074 chk_app = 0;
3075 chk_guard = 0;
3076
3077 /* Setup a ptr to the protection data provided by the SCSI host */
3078 sgpe = scsi_prot_sglist(cmd);
3079 protsegcnt = lpfc_cmd->prot_seg_cnt;
3080
3081 if (sgpe && protsegcnt) {
3082
3083 /*
3084 * We will only try to verify guard tag if the segment
3085 * data length is a multiple of the blksize.
3086 */
3087 sgde = scsi_sglist(cmd);
3088 blksize = lpfc_cmd_blksize(cmd);
3089 data_src = (uint8_t *)sg_virt(sgde);
3090 data_len = sgde->length;
3091 if ((data_len & (blksize - 1)) == 0)
3092 chk_guard = 1;
3093 guard_type = scsi_host_get_guard(cmd->device->host);
3094
James Smarte85d8f92013-05-31 17:04:10 -04003095 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
James Smarta6887e22013-04-17 20:18:07 -04003096 start_ref_tag = (uint32_t)scsi_get_lba(cmd); /* Truncate LBA */
James Smart737d4242013-04-17 20:14:49 -04003097 start_app_tag = src->app_tag;
James Smart737d4242013-04-17 20:14:49 -04003098 len = sgpe->length;
3099 while (src && protsegcnt) {
3100 while (len) {
3101
3102 /*
3103 * First check to see if a protection data
3104 * check is valid
3105 */
3106 if ((src->ref_tag == 0xffffffff) ||
3107 (src->app_tag == 0xffff)) {
3108 start_ref_tag++;
3109 goto skipit;
3110 }
3111
James Smart9c6aa9d2013-05-31 17:03:39 -04003112 /* First Guard Tag checking */
3113 if (chk_guard) {
3114 guard_tag = src->guard_tag;
3115 if (lpfc_cmd_guard_csum(cmd))
3116 sum = lpfc_bg_csum(data_src,
3117 blksize);
3118 else
3119 sum = lpfc_bg_crc(data_src,
3120 blksize);
3121 if ((guard_tag != sum)) {
3122 err_type = BGS_GUARD_ERR_MASK;
3123 goto out;
3124 }
James Smart737d4242013-04-17 20:14:49 -04003125 }
3126
3127 /* Reference Tag checking */
3128 ref_tag = be32_to_cpu(src->ref_tag);
3129 if (chk_ref && (ref_tag != start_ref_tag)) {
3130 err_type = BGS_REFTAG_ERR_MASK;
3131 goto out;
3132 }
3133 start_ref_tag++;
3134
James Smart9c6aa9d2013-05-31 17:03:39 -04003135 /* App Tag checking */
3136 app_tag = src->app_tag;
3137 if (chk_app && (app_tag != start_app_tag)) {
3138 err_type = BGS_APPTAG_ERR_MASK;
3139 goto out;
James Smart737d4242013-04-17 20:14:49 -04003140 }
3141skipit:
3142 len -= sizeof(struct scsi_dif_tuple);
3143 if (len < 0)
3144 len = 0;
3145 src++;
3146
3147 data_src += blksize;
3148 data_len -= blksize;
3149
3150 /*
3151 * Are we at the end of the Data segment?
3152 * The data segment is only used for Guard
3153 * tag checking.
3154 */
3155 if (chk_guard && (data_len == 0)) {
3156 chk_guard = 0;
3157 sgde = sg_next(sgde);
3158 if (!sgde)
3159 goto out;
3160
3161 data_src = (uint8_t *)sg_virt(sgde);
3162 data_len = sgde->length;
3163 if ((data_len & (blksize - 1)) == 0)
3164 chk_guard = 1;
3165 }
3166 }
3167
3168 /* Goto the next Protection data segment */
3169 sgpe = sg_next(sgpe);
3170 if (sgpe) {
3171 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
3172 len = sgpe->length;
3173 } else {
3174 src = NULL;
3175 }
3176 protsegcnt--;
3177 }
3178 }
3179out:
3180 if (err_type == BGS_GUARD_ERR_MASK) {
3181 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3182 0x10, 0x1);
3183 cmd->result = DRIVER_SENSE << 24
3184 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3185 phba->bg_guard_err_cnt++;
3186 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3187 "9069 BLKGRD: LBA %lx grd_tag error %x != %x\n",
3188 (unsigned long)scsi_get_lba(cmd),
3189 sum, guard_tag);
3190
3191 } else if (err_type == BGS_REFTAG_ERR_MASK) {
3192 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3193 0x10, 0x3);
3194 cmd->result = DRIVER_SENSE << 24
3195 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3196
3197 phba->bg_reftag_err_cnt++;
3198 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3199 "9066 BLKGRD: LBA %lx ref_tag error %x != %x\n",
3200 (unsigned long)scsi_get_lba(cmd),
3201 ref_tag, start_ref_tag);
3202
3203 } else if (err_type == BGS_APPTAG_ERR_MASK) {
3204 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3205 0x10, 0x2);
3206 cmd->result = DRIVER_SENSE << 24
3207 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3208
3209 phba->bg_apptag_err_cnt++;
3210 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3211 "9041 BLKGRD: LBA %lx app_tag error %x != %x\n",
3212 (unsigned long)scsi_get_lba(cmd),
3213 app_tag, start_app_tag);
3214 }
3215}
3216
3217
3218/*
James Smarte2a0a9d2008-12-04 22:40:02 -05003219 * This function checks for BlockGuard errors detected by
3220 * the HBA. In case of errors, the ASC/ASCQ fields in the
3221 * sense buffer will be set accordingly, paired with
3222 * ILLEGAL_REQUEST to signal to the kernel that the HBA
3223 * detected corruption.
3224 *
3225 * Returns:
3226 * 0 - No error found
3227 * 1 - BlockGuard error found
3228 * -1 - Internal error (bad profile, ...etc)
3229 */
3230static int
3231lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
3232 struct lpfc_iocbq *pIocbOut)
3233{
3234 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
3235 struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
3236 int ret = 0;
3237 uint32_t bghm = bgf->bghm;
3238 uint32_t bgstat = bgf->bgstat;
3239 uint64_t failing_sector = 0;
3240
James Smarte2a0a9d2008-12-04 22:40:02 -05003241 spin_lock(&_dump_buf_lock);
3242 if (!_dump_buf_done) {
James Smart6a9c52c2009-10-02 15:16:51 -04003243 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9070 BLKGRD: Saving"
3244 " Data for %u blocks to debugfs\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05003245 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
James Smart6a9c52c2009-10-02 15:16:51 -04003246 lpfc_debug_save_data(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003247
3248 /* If we have a prot sgl, save the DIF buffer */
3249 if (lpfc_prot_group_type(phba, cmd) ==
3250 LPFC_PG_TYPE_DIF_BUF) {
James Smart6a9c52c2009-10-02 15:16:51 -04003251 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9071 BLKGRD: "
3252 "Saving DIF for %u blocks to debugfs\n",
3253 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
3254 lpfc_debug_save_dif(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003255 }
3256
3257 _dump_buf_done = 1;
3258 }
3259 spin_unlock(&_dump_buf_lock);
3260
3261 if (lpfc_bgs_get_invalid_prof(bgstat)) {
3262 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart737d4242013-04-17 20:14:49 -04003263 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3264 "9072 BLKGRD: Invalid BG Profile in cmd"
3265 " 0x%x lba 0x%llx blk cnt 0x%x "
3266 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3267 (unsigned long long)scsi_get_lba(cmd),
3268 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003269 ret = (-1);
3270 goto out;
3271 }
3272
3273 if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
3274 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart737d4242013-04-17 20:14:49 -04003275 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3276 "9073 BLKGRD: Invalid BG PDIF Block in cmd"
3277 " 0x%x lba 0x%llx blk cnt 0x%x "
3278 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3279 (unsigned long long)scsi_get_lba(cmd),
3280 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003281 ret = (-1);
3282 goto out;
3283 }
3284
3285 if (lpfc_bgs_get_guard_err(bgstat)) {
3286 ret = 1;
3287
3288 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3289 0x10, 0x1);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05003290 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05003291 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3292 phba->bg_guard_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04003293 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3294 "9055 BLKGRD: Guard Tag error in cmd"
3295 " 0x%x lba 0x%llx blk cnt 0x%x "
3296 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3297 (unsigned long long)scsi_get_lba(cmd),
3298 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003299 }
3300
3301 if (lpfc_bgs_get_reftag_err(bgstat)) {
3302 ret = 1;
3303
3304 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3305 0x10, 0x3);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05003306 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05003307 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3308
3309 phba->bg_reftag_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04003310 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3311 "9056 BLKGRD: Ref Tag error in cmd"
3312 " 0x%x lba 0x%llx blk cnt 0x%x "
3313 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3314 (unsigned long long)scsi_get_lba(cmd),
3315 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003316 }
3317
3318 if (lpfc_bgs_get_apptag_err(bgstat)) {
3319 ret = 1;
3320
3321 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3322 0x10, 0x2);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05003323 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05003324 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3325
3326 phba->bg_apptag_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04003327 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3328 "9061 BLKGRD: App Tag error in cmd"
3329 " 0x%x lba 0x%llx blk cnt 0x%x "
3330 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3331 (unsigned long long)scsi_get_lba(cmd),
3332 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003333 }
3334
3335 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
3336 /*
3337 * setup sense data descriptor 0 per SPC-4 as an information
James Smart7c56b9f2011-07-22 18:36:25 -04003338 * field, and put the failing LBA in it.
3339 * This code assumes there was also a guard/app/ref tag error
3340 * indication.
James Smarte2a0a9d2008-12-04 22:40:02 -05003341 */
James Smart7c56b9f2011-07-22 18:36:25 -04003342 cmd->sense_buffer[7] = 0xc; /* Additional sense length */
3343 cmd->sense_buffer[8] = 0; /* Information descriptor type */
3344 cmd->sense_buffer[9] = 0xa; /* Additional descriptor length */
3345 cmd->sense_buffer[10] = 0x80; /* Validity bit */
James Smartacd68592012-01-18 16:25:09 -05003346
3347 /* bghm is a "on the wire" FC frame based count */
3348 switch (scsi_get_prot_op(cmd)) {
3349 case SCSI_PROT_READ_INSERT:
3350 case SCSI_PROT_WRITE_STRIP:
3351 bghm /= cmd->device->sector_size;
3352 break;
3353 case SCSI_PROT_READ_STRIP:
3354 case SCSI_PROT_WRITE_INSERT:
3355 case SCSI_PROT_READ_PASS:
3356 case SCSI_PROT_WRITE_PASS:
3357 bghm /= (cmd->device->sector_size +
3358 sizeof(struct scsi_dif_tuple));
3359 break;
3360 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003361
3362 failing_sector = scsi_get_lba(cmd);
3363 failing_sector += bghm;
3364
James Smart7c56b9f2011-07-22 18:36:25 -04003365 /* Descriptor Information */
3366 put_unaligned_be64(failing_sector, &cmd->sense_buffer[12]);
James Smarte2a0a9d2008-12-04 22:40:02 -05003367 }
3368
3369 if (!ret) {
3370 /* No error was reported - problem in FW? */
James Smart737d4242013-04-17 20:14:49 -04003371 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3372 "9057 BLKGRD: Unknown error in cmd"
3373 " 0x%x lba 0x%llx blk cnt 0x%x "
3374 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3375 (unsigned long long)scsi_get_lba(cmd),
3376 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003377
James Smart737d4242013-04-17 20:14:49 -04003378 /* Calcuate what type of error it was */
3379 lpfc_calc_bg_err(phba, lpfc_cmd);
3380 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003381out:
3382 return ret;
3383}
3384
James Smartea2151b2008-09-07 11:52:10 -04003385/**
James Smartda0436e2009-05-22 14:51:39 -04003386 * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3387 * @phba: The Hba for which this call is being executed.
3388 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3389 *
3390 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
3391 * field of @lpfc_cmd for device with SLI-4 interface spec.
3392 *
3393 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04003394 * 1 - Error
3395 * 0 - Success
James Smartda0436e2009-05-22 14:51:39 -04003396 **/
3397static int
3398lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3399{
3400 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3401 struct scatterlist *sgel = NULL;
3402 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3403 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
James Smartfedd3b72011-02-16 12:39:24 -05003404 struct sli4_sge *first_data_sgl;
James Smartda0436e2009-05-22 14:51:39 -04003405 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
3406 dma_addr_t physaddr;
3407 uint32_t num_bde = 0;
3408 uint32_t dma_len;
3409 uint32_t dma_offset = 0;
3410 int nseg;
James Smartfedd3b72011-02-16 12:39:24 -05003411 struct ulp_bde64 *bde;
James Smartda0436e2009-05-22 14:51:39 -04003412
3413 /*
3414 * There are three possibilities here - use scatter-gather segment, use
3415 * the single mapping, or neither. Start the lpfc command prep by
3416 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
3417 * data bde entry.
3418 */
3419 if (scsi_sg_count(scsi_cmnd)) {
3420 /*
3421 * The driver stores the segment count returned from pci_map_sg
3422 * because this a count of dma-mappings used to map the use_sg
3423 * pages. They are not guaranteed to be the same for those
3424 * architectures that implement an IOMMU.
3425 */
3426
3427 nseg = scsi_dma_map(scsi_cmnd);
3428 if (unlikely(!nseg))
3429 return 1;
3430 sgl += 1;
3431 /* clear the last flag in the fcp_rsp map entry */
3432 sgl->word2 = le32_to_cpu(sgl->word2);
3433 bf_set(lpfc_sli4_sge_last, sgl, 0);
3434 sgl->word2 = cpu_to_le32(sgl->word2);
3435 sgl += 1;
James Smartfedd3b72011-02-16 12:39:24 -05003436 first_data_sgl = sgl;
James Smartda0436e2009-05-22 14:51:39 -04003437 lpfc_cmd->seg_cnt = nseg;
3438 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04003439 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
3440 " %s: Too many sg segments from "
3441 "dma_map_sg. Config %d, seg_cnt %d\n",
3442 __func__, phba->cfg_sg_seg_cnt,
James Smartda0436e2009-05-22 14:51:39 -04003443 lpfc_cmd->seg_cnt);
James Smart96f70772013-04-17 20:16:15 -04003444 lpfc_cmd->seg_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04003445 scsi_dma_unmap(scsi_cmnd);
3446 return 1;
3447 }
3448
3449 /*
3450 * The driver established a maximum scatter-gather segment count
3451 * during probe that limits the number of sg elements in any
3452 * single scsi command. Just run through the seg_cnt and format
3453 * the sge's.
3454 * When using SLI-3 the driver will try to fit all the BDEs into
3455 * the IOCB. If it can't then the BDEs get added to a BPL as it
3456 * does for SLI-2 mode.
3457 */
3458 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
3459 physaddr = sg_dma_address(sgel);
3460 dma_len = sg_dma_len(sgel);
James Smartda0436e2009-05-22 14:51:39 -04003461 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
3462 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
James Smart05580562011-05-24 11:40:48 -04003463 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -04003464 if ((num_bde + 1) == nseg)
3465 bf_set(lpfc_sli4_sge_last, sgl, 1);
3466 else
3467 bf_set(lpfc_sli4_sge_last, sgl, 0);
3468 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
James Smartf9bb2da2011-10-10 21:34:11 -04003469 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
James Smartda0436e2009-05-22 14:51:39 -04003470 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05003471 sgl->sge_len = cpu_to_le32(dma_len);
James Smartda0436e2009-05-22 14:51:39 -04003472 dma_offset += dma_len;
3473 sgl++;
3474 }
James Smartfedd3b72011-02-16 12:39:24 -05003475 /* setup the performance hint (first data BDE) if enabled */
3476 if (phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) {
3477 bde = (struct ulp_bde64 *)
3478 &(iocb_cmd->unsli3.sli3Words[5]);
3479 bde->addrLow = first_data_sgl->addr_lo;
3480 bde->addrHigh = first_data_sgl->addr_hi;
3481 bde->tus.f.bdeSize =
3482 le32_to_cpu(first_data_sgl->sge_len);
3483 bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
3484 bde->tus.w = cpu_to_le32(bde->tus.w);
3485 }
James Smartda0436e2009-05-22 14:51:39 -04003486 } else {
3487 sgl += 1;
3488 /* clear the last flag in the fcp_rsp map entry */
3489 sgl->word2 = le32_to_cpu(sgl->word2);
3490 bf_set(lpfc_sli4_sge_last, sgl, 1);
3491 sgl->word2 = cpu_to_le32(sgl->word2);
3492 }
3493
3494 /*
3495 * Finish initializing those IOCB fields that are dependent on the
3496 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
3497 * explicitly reinitialized.
3498 * all iocb memory resources are reused.
3499 */
3500 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
3501
3502 /*
3503 * Due to difference in data length between DIF/non-DIF paths,
3504 * we need to set word 4 of IOCB here
3505 */
3506 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
3507 return 0;
3508}
3509
3510/**
James Smartacd68592012-01-18 16:25:09 -05003511 * lpfc_bg_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3512 * @phba: The Hba for which this call is being executed.
3513 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3514 *
3515 * This is the protection/DIF aware version of
3516 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
3517 * two functions eventually, but for now, it's here
3518 **/
3519static int
3520lpfc_bg_scsi_prep_dma_buf_s4(struct lpfc_hba *phba,
3521 struct lpfc_scsi_buf *lpfc_cmd)
3522{
3523 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3524 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3525 struct sli4_sge *sgl = (struct sli4_sge *)(lpfc_cmd->fcp_bpl);
3526 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart96f70772013-04-17 20:16:15 -04003527 uint32_t num_sge = 0;
James Smartacd68592012-01-18 16:25:09 -05003528 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
3529 int prot_group_type = 0;
3530 int fcpdl;
3531
3532 /*
3533 * Start the lpfc command prep by bumping the sgl beyond fcp_cmnd
James Smart96f70772013-04-17 20:16:15 -04003534 * fcp_rsp regions to the first data sge entry
James Smartacd68592012-01-18 16:25:09 -05003535 */
3536 if (scsi_sg_count(scsi_cmnd)) {
3537 /*
3538 * The driver stores the segment count returned from pci_map_sg
3539 * because this a count of dma-mappings used to map the use_sg
3540 * pages. They are not guaranteed to be the same for those
3541 * architectures that implement an IOMMU.
3542 */
3543 datasegcnt = dma_map_sg(&phba->pcidev->dev,
3544 scsi_sglist(scsi_cmnd),
3545 scsi_sg_count(scsi_cmnd), datadir);
3546 if (unlikely(!datasegcnt))
3547 return 1;
3548
3549 sgl += 1;
3550 /* clear the last flag in the fcp_rsp map entry */
3551 sgl->word2 = le32_to_cpu(sgl->word2);
3552 bf_set(lpfc_sli4_sge_last, sgl, 0);
3553 sgl->word2 = cpu_to_le32(sgl->word2);
3554
3555 sgl += 1;
3556 lpfc_cmd->seg_cnt = datasegcnt;
James Smart96f70772013-04-17 20:16:15 -04003557
3558 /* First check if data segment count from SCSI Layer is good */
3559 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt)
3560 goto err;
James Smartacd68592012-01-18 16:25:09 -05003561
3562 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
3563
3564 switch (prot_group_type) {
3565 case LPFC_PG_TYPE_NO_DIF:
James Smart96f70772013-04-17 20:16:15 -04003566 /* Here we need to add a DISEED to the count */
3567 if ((lpfc_cmd->seg_cnt + 1) > phba->cfg_total_seg_cnt)
3568 goto err;
3569
3570 num_sge = lpfc_bg_setup_sgl(phba, scsi_cmnd, sgl,
James Smartacd68592012-01-18 16:25:09 -05003571 datasegcnt);
James Smart96f70772013-04-17 20:16:15 -04003572
James Smartacd68592012-01-18 16:25:09 -05003573 /* we should have 2 or more entries in buffer list */
James Smart96f70772013-04-17 20:16:15 -04003574 if (num_sge < 2)
James Smartacd68592012-01-18 16:25:09 -05003575 goto err;
3576 break;
James Smart96f70772013-04-17 20:16:15 -04003577
3578 case LPFC_PG_TYPE_DIF_BUF:
James Smartacd68592012-01-18 16:25:09 -05003579 /*
3580 * This type indicates that protection buffers are
3581 * passed to the driver, so that needs to be prepared
3582 * for DMA
3583 */
3584 protsegcnt = dma_map_sg(&phba->pcidev->dev,
3585 scsi_prot_sglist(scsi_cmnd),
3586 scsi_prot_sg_count(scsi_cmnd), datadir);
3587 if (unlikely(!protsegcnt)) {
3588 scsi_dma_unmap(scsi_cmnd);
3589 return 1;
3590 }
3591
3592 lpfc_cmd->prot_seg_cnt = protsegcnt;
James Smart96f70772013-04-17 20:16:15 -04003593 /*
3594 * There is a minimun of 3 SGEs used for every
3595 * protection data segment.
3596 */
3597 if ((lpfc_cmd->prot_seg_cnt * 3) >
3598 (phba->cfg_total_seg_cnt - 2))
3599 goto err;
James Smartacd68592012-01-18 16:25:09 -05003600
James Smart96f70772013-04-17 20:16:15 -04003601 num_sge = lpfc_bg_setup_sgl_prot(phba, scsi_cmnd, sgl,
James Smartacd68592012-01-18 16:25:09 -05003602 datasegcnt, protsegcnt);
James Smart96f70772013-04-17 20:16:15 -04003603
James Smartacd68592012-01-18 16:25:09 -05003604 /* we should have 3 or more entries in buffer list */
James Smart96f70772013-04-17 20:16:15 -04003605 if ((num_sge < 3) ||
3606 (num_sge > phba->cfg_total_seg_cnt))
James Smartacd68592012-01-18 16:25:09 -05003607 goto err;
3608 break;
James Smart96f70772013-04-17 20:16:15 -04003609
James Smartacd68592012-01-18 16:25:09 -05003610 case LPFC_PG_TYPE_INVALID:
3611 default:
James Smart96f70772013-04-17 20:16:15 -04003612 scsi_dma_unmap(scsi_cmnd);
3613 lpfc_cmd->seg_cnt = 0;
3614
James Smartacd68592012-01-18 16:25:09 -05003615 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
3616 "9083 Unexpected protection group %i\n",
3617 prot_group_type);
3618 return 1;
3619 }
3620 }
3621
James Smart8012cc32012-10-31 14:44:49 -04003622 switch (scsi_get_prot_op(scsi_cmnd)) {
3623 case SCSI_PROT_WRITE_STRIP:
3624 case SCSI_PROT_READ_STRIP:
3625 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_STRIP;
3626 break;
3627 case SCSI_PROT_WRITE_INSERT:
3628 case SCSI_PROT_READ_INSERT:
3629 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_INSERT;
3630 break;
3631 case SCSI_PROT_WRITE_PASS:
3632 case SCSI_PROT_READ_PASS:
3633 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_PASS;
3634 break;
3635 }
3636
James Smartacd68592012-01-18 16:25:09 -05003637 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
James Smartacd68592012-01-18 16:25:09 -05003638 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
3639
3640 /*
3641 * Due to difference in data length between DIF/non-DIF paths,
3642 * we need to set word 4 of IOCB here
3643 */
3644 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
James Smartacd68592012-01-18 16:25:09 -05003645
3646 return 0;
3647err:
James Smart96f70772013-04-17 20:16:15 -04003648 if (lpfc_cmd->seg_cnt)
3649 scsi_dma_unmap(scsi_cmnd);
3650 if (lpfc_cmd->prot_seg_cnt)
3651 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
3652 scsi_prot_sg_count(scsi_cmnd),
3653 scsi_cmnd->sc_data_direction);
3654
James Smartacd68592012-01-18 16:25:09 -05003655 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart96f70772013-04-17 20:16:15 -04003656 "9084 Cannot setup S/G List for HBA"
3657 "IO segs %d/%d SGL %d SCSI %d: %d %d\n",
3658 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
3659 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
3660 prot_group_type, num_sge);
3661
3662 lpfc_cmd->seg_cnt = 0;
3663 lpfc_cmd->prot_seg_cnt = 0;
James Smartacd68592012-01-18 16:25:09 -05003664 return 1;
3665}
3666
3667/**
James Smart3772a992009-05-22 14:50:54 -04003668 * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3669 * @phba: The Hba for which this call is being executed.
3670 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3671 *
3672 * This routine wraps the actual DMA mapping function pointer from the
3673 * lpfc_hba struct.
3674 *
3675 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04003676 * 1 - Error
3677 * 0 - Success
James Smart3772a992009-05-22 14:50:54 -04003678 **/
3679static inline int
3680lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3681{
3682 return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
3683}
3684
3685/**
James Smartacd68592012-01-18 16:25:09 -05003686 * lpfc_bg_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3687 * using BlockGuard.
3688 * @phba: The Hba for which this call is being executed.
3689 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3690 *
3691 * This routine wraps the actual DMA mapping function pointer from the
3692 * lpfc_hba struct.
3693 *
3694 * Return codes:
3695 * 1 - Error
3696 * 0 - Success
3697 **/
3698static inline int
3699lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3700{
3701 return phba->lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
3702}
3703
3704/**
James Smart3621a712009-04-06 18:47:14 -04003705 * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
James Smartea2151b2008-09-07 11:52:10 -04003706 * @phba: Pointer to hba context object.
3707 * @vport: Pointer to vport object.
3708 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
3709 * @rsp_iocb: Pointer to response iocb object which reported error.
3710 *
3711 * This function posts an event when there is a SCSI command reporting
3712 * error from the scsi device.
3713 **/
3714static void
3715lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
3716 struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
3717 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3718 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
3719 uint32_t resp_info = fcprsp->rspStatus2;
3720 uint32_t scsi_status = fcprsp->rspStatus3;
3721 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
3722 struct lpfc_fast_path_event *fast_path_evt = NULL;
3723 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
3724 unsigned long flags;
3725
James Smart5989b8d2010-10-22 11:06:56 -04003726 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3727 return;
3728
James Smartea2151b2008-09-07 11:52:10 -04003729 /* If there is queuefull or busy condition send a scsi event */
3730 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
3731 (cmnd->result == SAM_STAT_BUSY)) {
3732 fast_path_evt = lpfc_alloc_fast_evt(phba);
3733 if (!fast_path_evt)
3734 return;
3735 fast_path_evt->un.scsi_evt.event_type =
3736 FC_REG_SCSI_EVENT;
3737 fast_path_evt->un.scsi_evt.subcategory =
3738 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
3739 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
3740 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
3741 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
3742 &pnode->nlp_portname, sizeof(struct lpfc_name));
3743 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
3744 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3745 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
3746 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
3747 fast_path_evt = lpfc_alloc_fast_evt(phba);
3748 if (!fast_path_evt)
3749 return;
3750 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
3751 FC_REG_SCSI_EVENT;
3752 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
3753 LPFC_EVENT_CHECK_COND;
3754 fast_path_evt->un.check_cond_evt.scsi_event.lun =
3755 cmnd->device->lun;
3756 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
3757 &pnode->nlp_portname, sizeof(struct lpfc_name));
3758 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
3759 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3760 fast_path_evt->un.check_cond_evt.sense_key =
3761 cmnd->sense_buffer[2] & 0xf;
3762 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
3763 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
3764 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3765 fcpi_parm &&
3766 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
3767 ((scsi_status == SAM_STAT_GOOD) &&
3768 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
3769 /*
3770 * If status is good or resid does not match with fcp_param and
3771 * there is valid fcpi_parm, then there is a read_check error
3772 */
3773 fast_path_evt = lpfc_alloc_fast_evt(phba);
3774 if (!fast_path_evt)
3775 return;
3776 fast_path_evt->un.read_check_error.header.event_type =
3777 FC_REG_FABRIC_EVENT;
3778 fast_path_evt->un.read_check_error.header.subcategory =
3779 LPFC_EVENT_FCPRDCHKERR;
3780 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
3781 &pnode->nlp_portname, sizeof(struct lpfc_name));
3782 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
3783 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3784 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
3785 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
3786 fast_path_evt->un.read_check_error.fcpiparam =
3787 fcpi_parm;
3788 } else
3789 return;
3790
3791 fast_path_evt->vport = vport;
3792 spin_lock_irqsave(&phba->hbalock, flags);
3793 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
3794 spin_unlock_irqrestore(&phba->hbalock, flags);
3795 lpfc_worker_wake_up(phba);
3796 return;
3797}
James Smart9bad7672008-12-04 22:39:02 -05003798
3799/**
James Smartf1126682009-06-10 17:22:44 -04003800 * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
James Smart3772a992009-05-22 14:50:54 -04003801 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -05003802 * @psb: The scsi buffer which is going to be un-mapped.
3803 *
3804 * This routine does DMA un-mapping of scatter gather list of scsi command
James Smart3772a992009-05-22 14:50:54 -04003805 * field of @lpfc_cmd for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05003806 **/
dea31012005-04-17 16:05:31 -05003807static void
James Smartf1126682009-06-10 17:22:44 -04003808lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James Smartbcf4dbf2006-07-06 15:50:08 -04003809{
3810 /*
3811 * There are only two special cases to consider. (1) the scsi command
3812 * requested scatter-gather usage or (2) the scsi command allocated
3813 * a request buffer, but did not request use_sg. There is a third
3814 * case, but it does not require resource deallocation.
3815 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003816 if (psb->seg_cnt > 0)
3817 scsi_dma_unmap(psb->pCmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003818 if (psb->prot_seg_cnt > 0)
3819 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
3820 scsi_prot_sg_count(psb->pCmd),
3821 psb->pCmd->sc_data_direction);
James Smartbcf4dbf2006-07-06 15:50:08 -04003822}
3823
James Smart9bad7672008-12-04 22:39:02 -05003824/**
James Smart3621a712009-04-06 18:47:14 -04003825 * lpfc_handler_fcp_err - FCP response handler
James Smart9bad7672008-12-04 22:39:02 -05003826 * @vport: The virtual port for which this call is being executed.
3827 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
3828 * @rsp_iocb: The response IOCB which contains FCP error.
3829 *
3830 * This routine is called to process response IOCB with status field
3831 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
3832 * based upon SCSI and FCP error.
3833 **/
James Smartbcf4dbf2006-07-06 15:50:08 -04003834static void
James Smart2e0fef82007-06-17 19:56:36 -05003835lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
3836 struct lpfc_iocbq *rsp_iocb)
dea31012005-04-17 16:05:31 -05003837{
3838 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3839 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
3840 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
James Smart7054a602007-04-25 09:52:34 -04003841 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
dea31012005-04-17 16:05:31 -05003842 uint32_t resp_info = fcprsp->rspStatus2;
3843 uint32_t scsi_status = fcprsp->rspStatus3;
James Smartc7743952006-12-02 13:34:42 -05003844 uint32_t *lp;
dea31012005-04-17 16:05:31 -05003845 uint32_t host_status = DID_OK;
3846 uint32_t rsplen = 0;
James Smartc7743952006-12-02 13:34:42 -05003847 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea31012005-04-17 16:05:31 -05003848
James Smartea2151b2008-09-07 11:52:10 -04003849
dea31012005-04-17 16:05:31 -05003850 /*
3851 * If this is a task management command, there is no
3852 * scsi packet associated with this lpfc_cmd. The driver
3853 * consumes it.
3854 */
3855 if (fcpcmd->fcpCntl2) {
3856 scsi_status = 0;
3857 goto out;
3858 }
3859
James Smart6a9c52c2009-10-02 15:16:51 -04003860 if (resp_info & RSP_LEN_VALID) {
3861 rsplen = be32_to_cpu(fcprsp->rspRspLen);
James Smarte40a02c2010-02-26 14:13:54 -05003862 if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
James Smart6a9c52c2009-10-02 15:16:51 -04003863 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3864 "2719 Invalid response length: "
3865 "tgt x%x lun x%x cmnd x%x rsplen x%x\n",
3866 cmnd->device->id,
3867 cmnd->device->lun, cmnd->cmnd[0],
3868 rsplen);
3869 host_status = DID_ERROR;
3870 goto out;
3871 }
James Smarte40a02c2010-02-26 14:13:54 -05003872 if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
3873 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3874 "2757 Protocol failure detected during "
3875 "processing of FCP I/O op: "
3876 "tgt x%x lun x%x cmnd x%x rspInfo3 x%x\n",
3877 cmnd->device->id,
3878 cmnd->device->lun, cmnd->cmnd[0],
3879 fcprsp->rspInfo3);
3880 host_status = DID_ERROR;
3881 goto out;
3882 }
James Smart6a9c52c2009-10-02 15:16:51 -04003883 }
3884
James Smartc7743952006-12-02 13:34:42 -05003885 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
3886 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
3887 if (snslen > SCSI_SENSE_BUFFERSIZE)
3888 snslen = SCSI_SENSE_BUFFERSIZE;
3889
3890 if (resp_info & RSP_LEN_VALID)
3891 rsplen = be32_to_cpu(fcprsp->rspRspLen);
3892 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
3893 }
3894 lp = (uint32_t *)cmnd->sense_buffer;
3895
James Smartaa1c7ee2012-08-14 14:26:06 -04003896 /* special handling for under run conditions */
3897 if (!scsi_status && (resp_info & RESID_UNDER)) {
3898 /* don't log under runs if fcp set... */
3899 if (vport->cfg_log_verbose & LOG_FCP)
3900 logit = LOG_FCP_ERROR;
3901 /* unless operator says so */
3902 if (vport->cfg_log_verbose & LOG_FCP_UNDER)
3903 logit = LOG_FCP_UNDER;
3904 }
James Smartc7743952006-12-02 13:34:42 -05003905
James Smarte8b62012007-08-02 11:10:09 -04003906 lpfc_printf_vlog(vport, KERN_WARNING, logit,
James Smarte2a0a9d2008-12-04 22:40:02 -05003907 "9024 FCP command x%x failed: x%x SNS x%x x%x "
James Smarte8b62012007-08-02 11:10:09 -04003908 "Data: x%x x%x x%x x%x x%x\n",
3909 cmnd->cmnd[0], scsi_status,
3910 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
3911 be32_to_cpu(fcprsp->rspResId),
3912 be32_to_cpu(fcprsp->rspSnsLen),
3913 be32_to_cpu(fcprsp->rspRspLen),
3914 fcprsp->rspInfo3);
dea31012005-04-17 16:05:31 -05003915
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003916 scsi_set_resid(cmnd, 0);
dea31012005-04-17 16:05:31 -05003917 if (resp_info & RESID_UNDER) {
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003918 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea31012005-04-17 16:05:31 -05003919
James Smart73d91e52011-10-10 21:32:10 -04003920 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_UNDER,
James Smarte2a0a9d2008-12-04 22:40:02 -05003921 "9025 FCP Read Underrun, expected %d, "
James Smarte8b62012007-08-02 11:10:09 -04003922 "residual %d Data: x%x x%x x%x\n",
3923 be32_to_cpu(fcpcmd->fcpDl),
3924 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
3925 cmnd->underflow);
dea31012005-04-17 16:05:31 -05003926
3927 /*
James Smart7054a602007-04-25 09:52:34 -04003928 * If there is an under run check if under run reported by
3929 * storage array is same as the under run reported by HBA.
3930 * If this is not same, there is a dropped frame.
3931 */
3932 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3933 fcpi_parm &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003934 (scsi_get_resid(cmnd) != fcpi_parm)) {
James Smarte8b62012007-08-02 11:10:09 -04003935 lpfc_printf_vlog(vport, KERN_WARNING,
3936 LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05003937 "9026 FCP Read Check Error "
James Smarte8b62012007-08-02 11:10:09 -04003938 "and Underrun Data: x%x x%x x%x x%x\n",
3939 be32_to_cpu(fcpcmd->fcpDl),
3940 scsi_get_resid(cmnd), fcpi_parm,
3941 cmnd->cmnd[0]);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003942 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
James Smart7054a602007-04-25 09:52:34 -04003943 host_status = DID_ERROR;
3944 }
3945 /*
dea31012005-04-17 16:05:31 -05003946 * The cmnd->underflow is the minimum number of bytes that must
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003947 * be transferred for this command. Provided a sense condition
dea31012005-04-17 16:05:31 -05003948 * is not present, make sure the actual amount transferred is at
3949 * least the underflow value or fail.
3950 */
3951 if (!(resp_info & SNS_LEN_VALID) &&
3952 (scsi_status == SAM_STAT_GOOD) &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003953 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
3954 < cmnd->underflow)) {
James Smarte8b62012007-08-02 11:10:09 -04003955 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05003956 "9027 FCP command x%x residual "
James Smarte8b62012007-08-02 11:10:09 -04003957 "underrun converted to error "
3958 "Data: x%x x%x x%x\n",
James Smart66dbfbe2007-08-05 06:08:38 -04003959 cmnd->cmnd[0], scsi_bufflen(cmnd),
James Smarte8b62012007-08-02 11:10:09 -04003960 scsi_get_resid(cmnd), cmnd->underflow);
dea31012005-04-17 16:05:31 -05003961 host_status = DID_ERROR;
3962 }
3963 } else if (resp_info & RESID_OVER) {
James Smarte8b62012007-08-02 11:10:09 -04003964 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05003965 "9028 FCP command x%x residual overrun error. "
James Smarte4e74272009-07-19 10:01:38 -04003966 "Data: x%x x%x\n", cmnd->cmnd[0],
James Smarte8b62012007-08-02 11:10:09 -04003967 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea31012005-04-17 16:05:31 -05003968 host_status = DID_ERROR;
3969
3970 /*
3971 * Check SLI validation that all the transfer was actually done
James Smart26373d22013-09-06 12:19:17 -04003972 * (fcpi_parm should be zero). Apply check only to reads.
dea31012005-04-17 16:05:31 -05003973 */
James Smart26373d22013-09-06 12:19:17 -04003974 } else if (fcpi_parm && (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
James Smarte8b62012007-08-02 11:10:09 -04003975 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
James Smart26373d22013-09-06 12:19:17 -04003976 "9029 FCP Read Check Error Data: "
James Smarteee88772010-09-29 11:19:08 -04003977 "x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003978 be32_to_cpu(fcpcmd->fcpDl),
3979 be32_to_cpu(fcprsp->rspResId),
James Smarteee88772010-09-29 11:19:08 -04003980 fcpi_parm, cmnd->cmnd[0], scsi_status);
3981 switch (scsi_status) {
3982 case SAM_STAT_GOOD:
3983 case SAM_STAT_CHECK_CONDITION:
3984 /* Fabric dropped a data frame. Fail any successful
3985 * command in which we detected dropped frames.
3986 * A status of good or some check conditions could
3987 * be considered a successful command.
3988 */
3989 host_status = DID_ERROR;
3990 break;
3991 }
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003992 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea31012005-04-17 16:05:31 -05003993 }
3994
3995 out:
3996 cmnd->result = ScsiResult(host_status, scsi_status);
James Smartea2151b2008-09-07 11:52:10 -04003997 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
dea31012005-04-17 16:05:31 -05003998}
3999
James Smart9bad7672008-12-04 22:39:02 -05004000/**
James Smart3621a712009-04-06 18:47:14 -04004001 * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
James Smart9bad7672008-12-04 22:39:02 -05004002 * @phba: The Hba for which this call is being executed.
4003 * @pIocbIn: The command IOCBQ for the scsi cmnd.
James Smart3772a992009-05-22 14:50:54 -04004004 * @pIocbOut: The response IOCBQ for the scsi cmnd.
James Smart9bad7672008-12-04 22:39:02 -05004005 *
4006 * This routine assigns scsi command result by looking into response IOCB
4007 * status field appropriately. This routine handles QUEUE FULL condition as
4008 * well by ramping down device queue depth.
4009 **/
dea31012005-04-17 16:05:31 -05004010static void
4011lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
4012 struct lpfc_iocbq *pIocbOut)
4013{
4014 struct lpfc_scsi_buf *lpfc_cmd =
4015 (struct lpfc_scsi_buf *) pIocbIn->context1;
James Smart2e0fef82007-06-17 19:56:36 -05004016 struct lpfc_vport *vport = pIocbIn->vport;
dea31012005-04-17 16:05:31 -05004017 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
4018 struct lpfc_nodelist *pnode = rdata->pnode;
James Smart75baf692010-06-08 18:31:21 -04004019 struct scsi_cmnd *cmd;
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05004020 int result;
James Smarta257bf92009-04-06 18:48:10 -04004021 struct scsi_device *tmp_sdev;
James Smart5ffc2662009-11-18 15:39:44 -05004022 int depth;
James Smartfa61a542008-01-11 01:52:42 -05004023 unsigned long flags;
James Smartea2151b2008-09-07 11:52:10 -04004024 struct lpfc_fast_path_event *fast_path_evt;
James Smart75baf692010-06-08 18:31:21 -04004025 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04004026 uint32_t queue_depth, scsi_id;
James Smart73d91e52011-10-10 21:32:10 -04004027 uint32_t logit = LOG_FCP;
dea31012005-04-17 16:05:31 -05004028
James Smart75baf692010-06-08 18:31:21 -04004029 /* Sanity check on return of outstanding command */
4030 if (!(lpfc_cmd->pCmd))
4031 return;
4032 cmd = lpfc_cmd->pCmd;
4033 shost = cmd->device->host;
4034
James Smarte3d2b802012-08-14 14:25:43 -04004035 lpfc_cmd->result = (pIocbOut->iocb.un.ulpWord[4] & IOERR_PARAM_MASK);
dea31012005-04-17 16:05:31 -05004036 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
James Smart341af102010-01-26 23:07:37 -05004037 /* pick up SLI4 exhange busy status from HBA */
4038 lpfc_cmd->exch_busy = pIocbOut->iocb_flag & LPFC_EXCHANGE_BUSY;
4039
James Smart9a6b09c2012-03-01 22:37:42 -05004040#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4041 if (lpfc_cmd->prot_data_type) {
4042 struct scsi_dif_tuple *src = NULL;
4043
4044 src = (struct scsi_dif_tuple *)lpfc_cmd->prot_data_segment;
4045 /*
4046 * Used to restore any changes to protection
4047 * data for error injection.
4048 */
4049 switch (lpfc_cmd->prot_data_type) {
4050 case LPFC_INJERR_REFTAG:
4051 src->ref_tag =
4052 lpfc_cmd->prot_data;
4053 break;
4054 case LPFC_INJERR_APPTAG:
4055 src->app_tag =
4056 (uint16_t)lpfc_cmd->prot_data;
4057 break;
4058 case LPFC_INJERR_GUARD:
4059 src->guard_tag =
4060 (uint16_t)lpfc_cmd->prot_data;
4061 break;
4062 default:
4063 break;
4064 }
4065
4066 lpfc_cmd->prot_data = 0;
4067 lpfc_cmd->prot_data_type = 0;
4068 lpfc_cmd->prot_data_segment = NULL;
4069 }
4070#endif
James Smart109f6ed2008-12-04 22:39:08 -05004071 if (pnode && NLP_CHK_NODE_ACT(pnode))
4072 atomic_dec(&pnode->cmd_pending);
dea31012005-04-17 16:05:31 -05004073
4074 if (lpfc_cmd->status) {
4075 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
4076 (lpfc_cmd->result & IOERR_DRVR_MASK))
4077 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
4078 else if (lpfc_cmd->status >= IOSTAT_CNT)
4079 lpfc_cmd->status = IOSTAT_DEFAULT;
James Smartaa1c7ee2012-08-14 14:26:06 -04004080 if (lpfc_cmd->status == IOSTAT_FCP_RSP_ERROR &&
4081 !lpfc_cmd->fcp_rsp->rspStatus3 &&
4082 (lpfc_cmd->fcp_rsp->rspStatus2 & RESID_UNDER) &&
4083 !(vport->cfg_log_verbose & LOG_FCP_UNDER))
James Smart73d91e52011-10-10 21:32:10 -04004084 logit = 0;
4085 else
4086 logit = LOG_FCP | LOG_FCP_UNDER;
4087 lpfc_printf_vlog(vport, KERN_WARNING, logit,
4088 "9030 FCP cmd x%x failed <%d/%d> "
James Smart5a0d80f2012-05-09 21:18:20 -04004089 "status: x%x result: x%x "
4090 "sid: x%x did: x%x oxid: x%x "
4091 "Data: x%x x%x\n",
James Smart73d91e52011-10-10 21:32:10 -04004092 cmd->cmnd[0],
4093 cmd->device ? cmd->device->id : 0xffff,
4094 cmd->device ? cmd->device->lun : 0xffff,
4095 lpfc_cmd->status, lpfc_cmd->result,
James Smart3bf41ba2013-05-31 17:03:18 -04004096 vport->fc_myDID,
4097 (pnode) ? pnode->nlp_DID : 0,
James Smart5a0d80f2012-05-09 21:18:20 -04004098 phba->sli_rev == LPFC_SLI_REV4 ?
4099 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
James Smart73d91e52011-10-10 21:32:10 -04004100 pIocbOut->iocb.ulpContext,
4101 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
dea31012005-04-17 16:05:31 -05004102
4103 switch (lpfc_cmd->status) {
4104 case IOSTAT_FCP_RSP_ERROR:
4105 /* Call FCP RSP handler to determine result */
James Smart2e0fef82007-06-17 19:56:36 -05004106 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea31012005-04-17 16:05:31 -05004107 break;
4108 case IOSTAT_NPORT_BSY:
4109 case IOSTAT_FABRIC_BSY:
James Smart0f1f53a2008-08-24 21:50:18 -04004110 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
James Smartea2151b2008-09-07 11:52:10 -04004111 fast_path_evt = lpfc_alloc_fast_evt(phba);
4112 if (!fast_path_evt)
4113 break;
4114 fast_path_evt->un.fabric_evt.event_type =
4115 FC_REG_FABRIC_EVENT;
4116 fast_path_evt->un.fabric_evt.subcategory =
4117 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
4118 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
4119 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
4120 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
4121 &pnode->nlp_portname,
4122 sizeof(struct lpfc_name));
4123 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
4124 &pnode->nlp_nodename,
4125 sizeof(struct lpfc_name));
4126 }
4127 fast_path_evt->vport = vport;
4128 fast_path_evt->work_evt.evt =
4129 LPFC_EVT_FASTPATH_MGMT_EVT;
4130 spin_lock_irqsave(&phba->hbalock, flags);
4131 list_add_tail(&fast_path_evt->work_evt.evt_listp,
4132 &phba->work_list);
4133 spin_unlock_irqrestore(&phba->hbalock, flags);
4134 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05004135 break;
James Smart92d7f7b2007-06-17 19:56:38 -05004136 case IOSTAT_LOCAL_REJECT:
James Smart1151e3e2011-02-16 12:39:35 -05004137 case IOSTAT_REMOTE_STOP:
James Smartab56dc22011-02-16 12:39:57 -05004138 if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
4139 lpfc_cmd->result ==
4140 IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
4141 lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
4142 lpfc_cmd->result ==
4143 IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
4144 cmd->result = ScsiResult(DID_NO_CONNECT, 0);
4145 break;
4146 }
James Smartd7c255b2008-08-24 21:50:00 -04004147 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
James Smart92d7f7b2007-06-17 19:56:38 -05004148 lpfc_cmd->result == IOERR_NO_RESOURCES ||
James Smartb92938b2010-06-07 15:24:12 -04004149 lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
4150 lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
James Smart92d7f7b2007-06-17 19:56:38 -05004151 cmd->result = ScsiResult(DID_REQUEUE, 0);
James Smart58da1ff2008-04-07 10:15:56 -04004152 break;
James Smarte2a0a9d2008-12-04 22:40:02 -05004153 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004154 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
4155 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
4156 pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
4157 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
4158 /*
4159 * This is a response for a BG enabled
4160 * cmd. Parse BG error
4161 */
4162 lpfc_parse_bg_err(phba, lpfc_cmd,
4163 pIocbOut);
4164 break;
4165 } else {
4166 lpfc_printf_vlog(vport, KERN_WARNING,
4167 LOG_BG,
4168 "9031 non-zero BGSTAT "
James Smart6a9c52c2009-10-02 15:16:51 -04004169 "on unprotected cmd\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05004170 }
4171 }
James Smart1151e3e2011-02-16 12:39:35 -05004172 if ((lpfc_cmd->status == IOSTAT_REMOTE_STOP)
4173 && (phba->sli_rev == LPFC_SLI_REV4)
4174 && (pnode && NLP_CHK_NODE_ACT(pnode))) {
4175 /* This IO was aborted by the target, we don't
4176 * know the rxid and because we did not send the
4177 * ABTS we cannot generate and RRQ.
4178 */
4179 lpfc_set_rrq_active(phba, pnode,
James Smartee0f4fe2012-05-09 21:19:14 -04004180 lpfc_cmd->cur_iocbq.sli4_lxritag,
4181 0, 0);
James Smart1151e3e2011-02-16 12:39:35 -05004182 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004183 /* else: fall through */
dea31012005-04-17 16:05:31 -05004184 default:
4185 cmd->result = ScsiResult(DID_ERROR, 0);
4186 break;
4187 }
4188
James Smart58da1ff2008-04-07 10:15:56 -04004189 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004190 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
James Smart0f1f53a2008-08-24 21:50:18 -04004191 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
4192 SAM_STAT_BUSY);
James Smartab56dc22011-02-16 12:39:57 -05004193 } else
dea31012005-04-17 16:05:31 -05004194 cmd->result = ScsiResult(DID_OK, 0);
dea31012005-04-17 16:05:31 -05004195
4196 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
4197 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
4198
James Smarte8b62012007-08-02 11:10:09 -04004199 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4200 "0710 Iodone <%d/%d> cmd %p, error "
4201 "x%x SNS x%x x%x Data: x%x x%x\n",
4202 cmd->device->id, cmd->device->lun, cmd,
4203 cmd->result, *lp, *(lp + 3), cmd->retries,
4204 scsi_get_resid(cmd));
dea31012005-04-17 16:05:31 -05004205 }
4206
James Smartea2151b2008-09-07 11:52:10 -04004207 lpfc_update_stats(phba, lpfc_cmd);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05004208 result = cmd->result;
James Smart977b5a02008-09-07 11:52:04 -04004209 if (vport->cfg_max_scsicmpl_time &&
4210 time_after(jiffies, lpfc_cmd->start_time +
4211 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
James Smarta257bf92009-04-06 18:48:10 -04004212 spin_lock_irqsave(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05004213 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
4214 if (pnode->cmd_qdepth >
4215 atomic_read(&pnode->cmd_pending) &&
4216 (atomic_read(&pnode->cmd_pending) >
4217 LPFC_MIN_TGT_QDEPTH) &&
4218 ((cmd->cmnd[0] == READ_10) ||
4219 (cmd->cmnd[0] == WRITE_10)))
4220 pnode->cmd_qdepth =
4221 atomic_read(&pnode->cmd_pending);
James Smart977b5a02008-09-07 11:52:04 -04004222
James Smart109f6ed2008-12-04 22:39:08 -05004223 pnode->last_change_time = jiffies;
4224 }
James Smarta257bf92009-04-06 18:48:10 -04004225 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05004226 } else if (pnode && NLP_CHK_NODE_ACT(pnode)) {
James Smart7dc517d2010-07-14 15:32:10 -04004227 if ((pnode->cmd_qdepth < vport->cfg_tgt_queue_depth) &&
James Smart977b5a02008-09-07 11:52:04 -04004228 time_after(jiffies, pnode->last_change_time +
James Smart109f6ed2008-12-04 22:39:08 -05004229 msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
James Smarta257bf92009-04-06 18:48:10 -04004230 spin_lock_irqsave(shost->host_lock, flags);
James Smart7dc517d2010-07-14 15:32:10 -04004231 depth = pnode->cmd_qdepth * LPFC_TGTQ_RAMPUP_PCENT
4232 / 100;
4233 depth = depth ? depth : 1;
4234 pnode->cmd_qdepth += depth;
4235 if (pnode->cmd_qdepth > vport->cfg_tgt_queue_depth)
4236 pnode->cmd_qdepth = vport->cfg_tgt_queue_depth;
James Smart109f6ed2008-12-04 22:39:08 -05004237 pnode->last_change_time = jiffies;
James Smarta257bf92009-04-06 18:48:10 -04004238 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05004239 }
James Smart977b5a02008-09-07 11:52:04 -04004240 }
4241
James Smart1dcb58e2007-04-25 09:51:30 -04004242 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James Smarta257bf92009-04-06 18:48:10 -04004243
4244 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
4245 queue_depth = cmd->device->queue_depth;
4246 scsi_id = cmd->device->id;
dea31012005-04-17 16:05:31 -05004247 cmd->scsi_done(cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004248
Jamie Wellnitzb8086082006-02-28 22:33:12 -05004249 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart876dd7d2012-09-29 11:31:28 -04004250 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92e3af62012-08-14 14:26:28 -04004251 lpfc_cmd->pCmd = NULL;
James Smart876dd7d2012-09-29 11:31:28 -04004252 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart92e3af62012-08-14 14:26:28 -04004253
James Smartfa61a542008-01-11 01:52:42 -05004254 /*
4255 * If there is a thread waiting for command completion
4256 * wake up the thread.
4257 */
James Smarta257bf92009-04-06 18:48:10 -04004258 spin_lock_irqsave(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05004259 if (lpfc_cmd->waitq)
4260 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04004261 spin_unlock_irqrestore(shost->host_lock, flags);
Jamie Wellnitzb8086082006-02-28 22:33:12 -05004262 lpfc_release_scsi_buf(phba, lpfc_cmd);
4263 return;
4264 }
4265
James Smart92d7f7b2007-06-17 19:56:38 -05004266 if (!result)
James Smarta257bf92009-04-06 18:48:10 -04004267 lpfc_rampup_queue_depth(vport, queue_depth);
James Smart92d7f7b2007-06-17 19:56:38 -05004268
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05004269 /*
4270 * Check for queue full. If the lun is reporting queue full, then
4271 * back off the lun queue depth to prevent target overloads.
4272 */
James Smart58da1ff2008-04-07 10:15:56 -04004273 if (result == SAM_STAT_TASK_SET_FULL && pnode &&
4274 NLP_CHK_NODE_ACT(pnode)) {
James Smarta257bf92009-04-06 18:48:10 -04004275 shost_for_each_device(tmp_sdev, shost) {
4276 if (tmp_sdev->id != scsi_id)
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05004277 continue;
4278 depth = scsi_track_queue_full(tmp_sdev,
James Smart5ffc2662009-11-18 15:39:44 -05004279 tmp_sdev->queue_depth-1);
4280 if (depth <= 0)
4281 continue;
James Smarte8b62012007-08-02 11:10:09 -04004282 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4283 "0711 detected queue full - lun queue "
4284 "depth adjusted to %d.\n", depth);
James Smartea2151b2008-09-07 11:52:10 -04004285 lpfc_send_sdev_queuedepth_change_event(phba, vport,
James Smart5ffc2662009-11-18 15:39:44 -05004286 pnode,
4287 tmp_sdev->lun,
4288 depth+1, depth);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05004289 }
4290 }
4291
James Smart876dd7d2012-09-29 11:31:28 -04004292 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92e3af62012-08-14 14:26:28 -04004293 lpfc_cmd->pCmd = NULL;
James Smart876dd7d2012-09-29 11:31:28 -04004294 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart92e3af62012-08-14 14:26:28 -04004295
James Smartfa61a542008-01-11 01:52:42 -05004296 /*
4297 * If there is a thread waiting for command completion
4298 * wake up the thread.
4299 */
James Smarta257bf92009-04-06 18:48:10 -04004300 spin_lock_irqsave(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05004301 if (lpfc_cmd->waitq)
4302 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04004303 spin_unlock_irqrestore(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05004304
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004305 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004306}
4307
James Smart34b02dc2008-08-24 21:49:55 -04004308/**
James Smart3621a712009-04-06 18:47:14 -04004309 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
James Smart34b02dc2008-08-24 21:49:55 -04004310 * @data: A pointer to the immediate command data portion of the IOCB.
4311 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
4312 *
4313 * The routine copies the entire FCP command from @fcp_cmnd to @data while
4314 * byte swapping the data to big endian format for transmission on the wire.
4315 **/
4316static void
4317lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
4318{
4319 int i, j;
4320 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
4321 i += sizeof(uint32_t), j++) {
4322 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
4323 }
4324}
4325
James Smart9bad7672008-12-04 22:39:02 -05004326/**
James Smartf1126682009-06-10 17:22:44 -04004327 * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05004328 * @vport: The virtual port for which this call is being executed.
4329 * @lpfc_cmd: The scsi command which needs to send.
4330 * @pnode: Pointer to lpfc_nodelist.
4331 *
4332 * This routine initializes fcp_cmnd and iocb data structure from scsi command
James Smart3772a992009-05-22 14:50:54 -04004333 * to transfer for device with SLI3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05004334 **/
dea31012005-04-17 16:05:31 -05004335static void
James Smartf1126682009-06-10 17:22:44 -04004336lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
James Smart2e0fef82007-06-17 19:56:36 -05004337 struct lpfc_nodelist *pnode)
dea31012005-04-17 16:05:31 -05004338{
James Smart2e0fef82007-06-17 19:56:36 -05004339 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004340 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
4341 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
4342 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
4343 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
4344 int datadir = scsi_cmnd->sc_data_direction;
James Smart7e2b19f2007-10-29 11:00:39 -04004345 char tag[2];
James Smart027140e2012-08-03 12:35:44 -04004346 uint8_t *ptr;
4347 bool sli4;
James Smart98bbf5f2013-09-06 12:18:45 -04004348 uint32_t fcpdl;
dea31012005-04-17 16:05:31 -05004349
James Smart58da1ff2008-04-07 10:15:56 -04004350 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
4351 return;
4352
dea31012005-04-17 16:05:31 -05004353 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
James.Smart@Emulex.Com69859dc2005-08-10 15:02:37 -04004354 /* clear task management bits */
4355 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea31012005-04-17 16:05:31 -05004356
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -04004357 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
4358 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05004359
James Smart027140e2012-08-03 12:35:44 -04004360 ptr = &fcp_cmnd->fcpCdb[0];
4361 memcpy(ptr, scsi_cmnd->cmnd, scsi_cmnd->cmd_len);
4362 if (scsi_cmnd->cmd_len < LPFC_FCP_CDB_LEN) {
4363 ptr += scsi_cmnd->cmd_len;
4364 memset(ptr, 0, (LPFC_FCP_CDB_LEN - scsi_cmnd->cmd_len));
4365 }
4366
James Smart7e2b19f2007-10-29 11:00:39 -04004367 if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
4368 switch (tag[0]) {
dea31012005-04-17 16:05:31 -05004369 case HEAD_OF_QUEUE_TAG:
4370 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
4371 break;
4372 case ORDERED_QUEUE_TAG:
4373 fcp_cmnd->fcpCntl1 = ORDERED_Q;
4374 break;
4375 default:
4376 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
4377 break;
4378 }
4379 } else
James Smartfe8f7f92013-01-03 15:43:03 -05004380 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
dea31012005-04-17 16:05:31 -05004381
James Smart027140e2012-08-03 12:35:44 -04004382 sli4 = (phba->sli_rev == LPFC_SLI_REV4);
4383
dea31012005-04-17 16:05:31 -05004384 /*
4385 * There are three possibilities here - use scatter-gather segment, use
4386 * the single mapping, or neither. Start the lpfc command prep by
4387 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
4388 * data bde entry.
4389 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05004390 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05004391 if (datadir == DMA_TO_DEVICE) {
4392 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
James Smart182ba752013-07-15 18:34:05 -04004393 iocb_cmd->ulpPU = PARM_READ_CHECK;
James Smart3cb01c52013-07-15 18:35:04 -04004394 if (vport->cfg_first_burst_size &&
4395 (pnode->nlp_flag & NLP_FIRSTBURST)) {
James Smart98bbf5f2013-09-06 12:18:45 -04004396 fcpdl = scsi_bufflen(scsi_cmnd);
4397 if (fcpdl < vport->cfg_first_burst_size)
4398 piocbq->iocb.un.fcpi.fcpi_XRdy = fcpdl;
4399 else
4400 piocbq->iocb.un.fcpi.fcpi_XRdy =
4401 vport->cfg_first_burst_size;
James Smart3cb01c52013-07-15 18:35:04 -04004402 }
dea31012005-04-17 16:05:31 -05004403 fcp_cmnd->fcpCntl3 = WRITE_DATA;
4404 phba->fc4OutputRequests++;
4405 } else {
4406 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
4407 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05004408 fcp_cmnd->fcpCntl3 = READ_DATA;
4409 phba->fc4InputRequests++;
4410 }
4411 } else {
4412 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
4413 iocb_cmd->un.fcpi.fcpi_parm = 0;
4414 iocb_cmd->ulpPU = 0;
4415 fcp_cmnd->fcpCntl3 = 0;
4416 phba->fc4ControlRequests++;
4417 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004418 if (phba->sli_rev == 3 &&
4419 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04004420 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05004421 /*
4422 * Finish initializing those IOCB fields that are independent
4423 * of the scsi_cmnd request_buffer
4424 */
4425 piocbq->iocb.ulpContext = pnode->nlp_rpi;
James Smart027140e2012-08-03 12:35:44 -04004426 if (sli4)
James Smart6d368e52011-05-24 11:44:12 -04004427 piocbq->iocb.ulpContext =
4428 phba->sli4_hba.rpi_ids[pnode->nlp_rpi];
dea31012005-04-17 16:05:31 -05004429 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
4430 piocbq->iocb.ulpFCP2Rcvy = 1;
James Smart09372822008-01-11 01:52:54 -05004431 else
4432 piocbq->iocb.ulpFCP2Rcvy = 0;
dea31012005-04-17 16:05:31 -05004433
4434 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
4435 piocbq->context1 = lpfc_cmd;
4436 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
4437 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
James Smart2e0fef82007-06-17 19:56:36 -05004438 piocbq->vport = vport;
dea31012005-04-17 16:05:31 -05004439}
4440
James Smart9bad7672008-12-04 22:39:02 -05004441/**
James Smart6d368e52011-05-24 11:44:12 -04004442 * lpfc_scsi_prep_task_mgmt_cmd - Convert SLI3 scsi TM cmd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05004443 * @vport: The virtual port for which this call is being executed.
4444 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
4445 * @lun: Logical unit number.
4446 * @task_mgmt_cmd: SCSI task management command.
4447 *
James Smart3772a992009-05-22 14:50:54 -04004448 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
4449 * for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05004450 *
4451 * Return codes:
4452 * 0 - Error
4453 * 1 - Success
4454 **/
dea31012005-04-17 16:05:31 -05004455static int
James Smartf1126682009-06-10 17:22:44 -04004456lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
dea31012005-04-17 16:05:31 -05004457 struct lpfc_scsi_buf *lpfc_cmd,
James Smart420b630d2006-07-06 15:50:16 -04004458 unsigned int lun,
dea31012005-04-17 16:05:31 -05004459 uint8_t task_mgmt_cmd)
4460{
dea31012005-04-17 16:05:31 -05004461 struct lpfc_iocbq *piocbq;
4462 IOCB_t *piocb;
4463 struct fcp_cmnd *fcp_cmnd;
James Smart0b18ac42006-05-01 21:50:40 -04004464 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea31012005-04-17 16:05:31 -05004465 struct lpfc_nodelist *ndlp = rdata->pnode;
4466
James Smart58da1ff2008-04-07 10:15:56 -04004467 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
4468 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05004469 return 0;
dea31012005-04-17 16:05:31 -05004470
dea31012005-04-17 16:05:31 -05004471 piocbq = &(lpfc_cmd->cur_iocbq);
James Smart2e0fef82007-06-17 19:56:36 -05004472 piocbq->vport = vport;
4473
dea31012005-04-17 16:05:31 -05004474 piocb = &piocbq->iocb;
4475
4476 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart34b02dc2008-08-24 21:49:55 -04004477 /* Clear out any old data in the FCP command area */
4478 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
4479 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05004480 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
James Smarte2a0a9d2008-12-04 22:40:02 -05004481 if (vport->phba->sli_rev == 3 &&
4482 !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04004483 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05004484 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
dea31012005-04-17 16:05:31 -05004485 piocb->ulpContext = ndlp->nlp_rpi;
James Smart6d368e52011-05-24 11:44:12 -04004486 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
4487 piocb->ulpContext =
4488 vport->phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
4489 }
dea31012005-04-17 16:05:31 -05004490 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
4491 piocb->ulpFCP2Rcvy = 1;
4492 }
4493 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
4494
4495 /* ulpTimeout is only one byte */
4496 if (lpfc_cmd->timeout > 0xff) {
4497 /*
4498 * Do not timeout the command at the firmware level.
4499 * The driver will provide the timeout mechanism.
4500 */
4501 piocb->ulpTimeout = 0;
James Smartf1126682009-06-10 17:22:44 -04004502 } else
dea31012005-04-17 16:05:31 -05004503 piocb->ulpTimeout = lpfc_cmd->timeout;
James Smartf1126682009-06-10 17:22:44 -04004504
4505 if (vport->phba->sli_rev == LPFC_SLI_REV4)
4506 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004507
James Smart2e0fef82007-06-17 19:56:36 -05004508 return 1;
dea31012005-04-17 16:05:31 -05004509}
4510
James Smart9bad7672008-12-04 22:39:02 -05004511/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004512 * lpfc_scsi_api_table_setup - Set up scsi api function jump table
James Smart3772a992009-05-22 14:50:54 -04004513 * @phba: The hba struct for which this call is being executed.
4514 * @dev_grp: The HBA PCI-Device group number.
4515 *
4516 * This routine sets up the SCSI interface API function jump table in @phba
4517 * struct.
4518 * Returns: 0 - success, -ENODEV - failure.
4519 **/
4520int
4521lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4522{
4523
James Smartf1126682009-06-10 17:22:44 -04004524 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
4525 phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
James Smartf1126682009-06-10 17:22:44 -04004526
James Smart3772a992009-05-22 14:50:54 -04004527 switch (dev_grp) {
4528 case LPFC_PCI_DEV_LP:
4529 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s3;
4530 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
James Smartacd68592012-01-18 16:25:09 -05004531 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04004532 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
James Smart19ca7602010-11-20 23:11:55 -05004533 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04004534 break;
James Smartda0436e2009-05-22 14:51:39 -04004535 case LPFC_PCI_DEV_OC:
4536 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s4;
4537 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
James Smartacd68592012-01-18 16:25:09 -05004538 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04004539 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
James Smart19ca7602010-11-20 23:11:55 -05004540 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04004541 break;
James Smart3772a992009-05-22 14:50:54 -04004542 default:
4543 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4544 "1418 Invalid HBA PCI-device group: 0x%x\n",
4545 dev_grp);
4546 return -ENODEV;
4547 break;
4548 }
James Smart3772a992009-05-22 14:50:54 -04004549 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
James Smart84d1b002010-02-12 14:42:33 -05004550 phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
James Smart3772a992009-05-22 14:50:54 -04004551 return 0;
4552}
4553
4554/**
James Smart3621a712009-04-06 18:47:14 -04004555 * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
James Smart9bad7672008-12-04 22:39:02 -05004556 * @phba: The Hba for which this call is being executed.
4557 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
4558 * @rspiocbq: Pointer to lpfc_iocbq data structure.
4559 *
4560 * This routine is IOCB completion routine for device reset and target reset
4561 * routine. This routine release scsi buffer associated with lpfc_cmd.
4562 **/
James Smart7054a602007-04-25 09:52:34 -04004563static void
4564lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
4565 struct lpfc_iocbq *cmdiocbq,
4566 struct lpfc_iocbq *rspiocbq)
4567{
4568 struct lpfc_scsi_buf *lpfc_cmd =
4569 (struct lpfc_scsi_buf *) cmdiocbq->context1;
4570 if (lpfc_cmd)
4571 lpfc_release_scsi_buf(phba, lpfc_cmd);
4572 return;
4573}
4574
James Smart9bad7672008-12-04 22:39:02 -05004575/**
James Smart3621a712009-04-06 18:47:14 -04004576 * lpfc_info - Info entry point of scsi_host_template data structure
James Smart9bad7672008-12-04 22:39:02 -05004577 * @host: The scsi host for which this call is being executed.
4578 *
4579 * This routine provides module information about hba.
4580 *
4581 * Reutrn code:
4582 * Pointer to char - Success.
4583 **/
dea31012005-04-17 16:05:31 -05004584const char *
4585lpfc_info(struct Scsi_Host *host)
4586{
James Smart2e0fef82007-06-17 19:56:36 -05004587 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
4588 struct lpfc_hba *phba = vport->phba;
James Smart8b68cd52012-09-29 11:32:37 -04004589 int len, link_speed = 0;
dea31012005-04-17 16:05:31 -05004590 static char lpfcinfobuf[384];
4591
4592 memset(lpfcinfobuf,0,384);
4593 if (phba && phba->pcidev){
4594 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
4595 len = strlen(lpfcinfobuf);
4596 snprintf(lpfcinfobuf + len,
4597 384-len,
4598 " on PCI bus %02x device %02x irq %d",
4599 phba->pcidev->bus->number,
4600 phba->pcidev->devfn,
4601 phba->pcidev->irq);
4602 len = strlen(lpfcinfobuf);
4603 if (phba->Port[0]) {
4604 snprintf(lpfcinfobuf + len,
4605 384-len,
4606 " port %s",
4607 phba->Port);
4608 }
James Smart65467b62010-01-26 23:08:29 -05004609 len = strlen(lpfcinfobuf);
James Smart8b68cd52012-09-29 11:32:37 -04004610 if (phba->sli_rev <= LPFC_SLI_REV3) {
4611 link_speed = lpfc_sli_port_speed_get(phba);
4612 } else {
4613 if (phba->sli4_hba.link_state.logical_speed)
4614 link_speed =
4615 phba->sli4_hba.link_state.logical_speed;
4616 else
4617 link_speed = phba->sli4_hba.link_state.speed;
James Smart65467b62010-01-26 23:08:29 -05004618 }
James Smart8b68cd52012-09-29 11:32:37 -04004619 if (link_speed != 0)
4620 snprintf(lpfcinfobuf + len, 384-len,
4621 " Logical Link Speed: %d Mbps", link_speed);
dea31012005-04-17 16:05:31 -05004622 }
4623 return lpfcinfobuf;
4624}
4625
James Smart9bad7672008-12-04 22:39:02 -05004626/**
James Smart3621a712009-04-06 18:47:14 -04004627 * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
James Smart9bad7672008-12-04 22:39:02 -05004628 * @phba: The Hba for which this call is being executed.
4629 *
4630 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
4631 * The default value of cfg_poll_tmo is 10 milliseconds.
4632 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004633static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
4634{
4635 unsigned long poll_tmo_expires =
4636 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
4637
James Smart0e9bb8d2013-03-01 16:35:12 -05004638 if (!list_empty(&phba->sli.ring[LPFC_FCP_RING].txcmplq))
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004639 mod_timer(&phba->fcp_poll_timer,
4640 poll_tmo_expires);
4641}
4642
James Smart9bad7672008-12-04 22:39:02 -05004643/**
James Smart3621a712009-04-06 18:47:14 -04004644 * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
James Smart9bad7672008-12-04 22:39:02 -05004645 * @phba: The Hba for which this call is being executed.
4646 *
4647 * This routine starts the fcp_poll_timer of @phba.
4648 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004649void lpfc_poll_start_timer(struct lpfc_hba * phba)
4650{
4651 lpfc_poll_rearm_timer(phba);
4652}
4653
James Smart9bad7672008-12-04 22:39:02 -05004654/**
James Smart3621a712009-04-06 18:47:14 -04004655 * lpfc_poll_timeout - Restart polling timer
James Smart9bad7672008-12-04 22:39:02 -05004656 * @ptr: Map to lpfc_hba data structure pointer.
4657 *
4658 * This routine restarts fcp_poll timer, when FCP ring polling is enable
4659 * and FCP Ring interrupt is disable.
4660 **/
4661
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004662void lpfc_poll_timeout(unsigned long ptr)
4663{
James Smart2e0fef82007-06-17 19:56:36 -05004664 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004665
4666 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004667 lpfc_sli_handle_fast_ring_event(phba,
4668 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4669
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004670 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4671 lpfc_poll_rearm_timer(phba);
4672 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004673}
4674
James Smart9bad7672008-12-04 22:39:02 -05004675/**
James Smart3621a712009-04-06 18:47:14 -04004676 * lpfc_queuecommand - scsi_host_template queuecommand entry point
James Smart9bad7672008-12-04 22:39:02 -05004677 * @cmnd: Pointer to scsi_cmnd data structure.
4678 * @done: Pointer to done routine.
4679 *
4680 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
4681 * This routine prepares an IOCB from scsi command and provides to firmware.
4682 * The @done callback is invoked after driver finished processing the command.
4683 *
4684 * Return value :
4685 * 0 - Success
4686 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
4687 **/
dea31012005-04-17 16:05:31 -05004688static int
James Smartb9a7c632012-08-03 12:35:24 -04004689lpfc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004690{
James Smart2e0fef82007-06-17 19:56:36 -05004691 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4692 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004693 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05004694 struct lpfc_nodelist *ndlp;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004695 struct lpfc_scsi_buf *lpfc_cmd;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004696 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004697 int err;
dea31012005-04-17 16:05:31 -05004698
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004699 err = fc_remote_port_chkready(rport);
4700 if (err) {
4701 cmnd->result = err;
dea31012005-04-17 16:05:31 -05004702 goto out_fail_command;
4703 }
James Smart1c6f4ef52009-11-18 15:40:49 -05004704 ndlp = rdata->pnode;
dea31012005-04-17 16:05:31 -05004705
James Smartbf086112011-08-21 21:48:13 -04004706 if ((scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) &&
James Smartacd68592012-01-18 16:25:09 -05004707 (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED))) {
James Smarte2a0a9d2008-12-04 22:40:02 -05004708
James Smart6a9c52c2009-10-02 15:16:51 -04004709 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4710 "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
4711 " op:%02x str=%s without registering for"
4712 " BlockGuard - Rejecting command\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05004713 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
4714 dif_op_str[scsi_get_prot_op(cmnd)]);
4715 goto out_fail_command;
4716 }
4717
dea31012005-04-17 16:05:31 -05004718 /*
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004719 * Catch race where our node has transitioned, but the
4720 * transport is still transitioning.
dea31012005-04-17 16:05:31 -05004721 */
James Smart6b415f52012-06-12 13:54:59 -04004722 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
4723 goto out_tgt_busy;
James Smart7dc517d2010-07-14 15:32:10 -04004724 if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth)
Mike Christie34963432011-02-25 14:04:28 -06004725 goto out_tgt_busy;
Mike Christiea93ce022008-08-17 15:24:41 -05004726
James Smart19ca7602010-11-20 23:11:55 -05004727 lpfc_cmd = lpfc_get_scsi_buf(phba, ndlp);
dea31012005-04-17 16:05:31 -05004728 if (lpfc_cmd == NULL) {
James Smarteaf15d52008-12-04 22:39:29 -05004729 lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05004730
James Smarte8b62012007-08-02 11:10:09 -04004731 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4732 "0707 driver's buffer pool is empty, "
4733 "IO busied\n");
dea31012005-04-17 16:05:31 -05004734 goto out_host_busy;
4735 }
4736
4737 /*
4738 * Store the midlayer's command structure for the completion phase
4739 * and complete the command initialization.
4740 */
4741 lpfc_cmd->pCmd = cmnd;
4742 lpfc_cmd->rdata = rdata;
4743 lpfc_cmd->timeout = 0;
James Smart977b5a02008-09-07 11:52:04 -04004744 lpfc_cmd->start_time = jiffies;
dea31012005-04-17 16:05:31 -05004745 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
dea31012005-04-17 16:05:31 -05004746
James Smarte2a0a9d2008-12-04 22:40:02 -05004747 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
James Smart6a9c52c2009-10-02 15:16:51 -04004748 if (vport->phba->cfg_enable_bg) {
James Smart737d4242013-04-17 20:14:49 -04004749 lpfc_printf_vlog(vport,
4750 KERN_INFO, LOG_SCSI_CMD,
James Smart26134702012-08-14 14:25:50 -04004751 "9033 BLKGRD: rcvd %s cmd:x%x "
4752 "sector x%llx cnt %u pt %x\n",
4753 dif_op_str[scsi_get_prot_op(cmnd)],
4754 cmnd->cmnd[0],
4755 (unsigned long long)scsi_get_lba(cmnd),
4756 blk_rq_sectors(cmnd->request),
4757 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004758 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004759 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
4760 } else {
James Smart6a9c52c2009-10-02 15:16:51 -04004761 if (vport->phba->cfg_enable_bg) {
James Smart737d4242013-04-17 20:14:49 -04004762 lpfc_printf_vlog(vport,
4763 KERN_INFO, LOG_SCSI_CMD,
James Smart26134702012-08-14 14:25:50 -04004764 "9038 BLKGRD: rcvd PROT_NORMAL cmd: "
4765 "x%x sector x%llx cnt %u pt %x\n",
4766 cmnd->cmnd[0],
4767 (unsigned long long)scsi_get_lba(cmnd),
James Smart9a6b09c2012-03-01 22:37:42 -05004768 blk_rq_sectors(cmnd->request),
James Smart26134702012-08-14 14:25:50 -04004769 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004770 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004771 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
4772 }
4773
dea31012005-04-17 16:05:31 -05004774 if (err)
4775 goto out_host_busy_free_buf;
4776
James Smart2e0fef82007-06-17 19:56:36 -05004777 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea31012005-04-17 16:05:31 -05004778
James Smart977b5a02008-09-07 11:52:04 -04004779 atomic_inc(&ndlp->cmd_pending);
James Smart3772a992009-05-22 14:50:54 -04004780 err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
James Smart92d7f7b2007-06-17 19:56:38 -05004781 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
James Smarteaf15d52008-12-04 22:39:29 -05004782 if (err) {
4783 atomic_dec(&ndlp->cmd_pending);
dea31012005-04-17 16:05:31 -05004784 goto out_host_busy_free_buf;
James Smarteaf15d52008-12-04 22:39:29 -05004785 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004786 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004787 lpfc_sli_handle_fast_ring_event(phba,
4788 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4789
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004790 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4791 lpfc_poll_rearm_timer(phba);
4792 }
4793
dea31012005-04-17 16:05:31 -05004794 return 0;
4795
4796 out_host_busy_free_buf:
James Smartbcf4dbf2006-07-06 15:50:08 -04004797 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004798 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004799 out_host_busy:
4800 return SCSI_MLQUEUE_HOST_BUSY;
4801
Mike Christie34963432011-02-25 14:04:28 -06004802 out_tgt_busy:
4803 return SCSI_MLQUEUE_TARGET_BUSY;
4804
dea31012005-04-17 16:05:31 -05004805 out_fail_command:
James Smartb9a7c632012-08-03 12:35:24 -04004806 cmnd->scsi_done(cmnd);
dea31012005-04-17 16:05:31 -05004807 return 0;
4808}
4809
Jeff Garzikf2812332010-11-16 02:10:29 -05004810
James Smart9bad7672008-12-04 22:39:02 -05004811/**
James Smart3621a712009-04-06 18:47:14 -04004812 * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05004813 * @cmnd: Pointer to scsi_cmnd data structure.
4814 *
4815 * This routine aborts @cmnd pending in base driver.
4816 *
4817 * Return code :
4818 * 0x2003 - Error
4819 * 0x2002 - Success
4820 **/
dea31012005-04-17 16:05:31 -05004821static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05004822lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004823{
James Smart2e0fef82007-06-17 19:56:36 -05004824 struct Scsi_Host *shost = cmnd->device->host;
4825 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4826 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004827 struct lpfc_iocbq *iocb;
4828 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05004829 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05004830 IOCB_t *cmd, *icmd;
James Smart3a707302012-06-12 13:54:42 -04004831 int ret = SUCCESS, status = 0;
James Smart876dd7d2012-09-29 11:31:28 -04004832 unsigned long flags;
James Smartfa61a542008-01-11 01:52:42 -05004833 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004834
James Smart3a707302012-06-12 13:54:42 -04004835 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04004836 if (status != 0 && status != SUCCESS)
James Smart3a707302012-06-12 13:54:42 -04004837 return status;
James Smart4f2e66c2012-05-09 21:17:07 -04004838
James Smart876dd7d2012-09-29 11:31:28 -04004839 spin_lock_irqsave(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004840 /* driver queued commands are in process of being flushed */
4841 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
James Smart876dd7d2012-09-29 11:31:28 -04004842 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004843 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4844 "3168 SCSI Layer abort requested I/O has been "
4845 "flushed by LLD.\n");
4846 return FAILED;
4847 }
4848
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004849 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
James Smart92e3af62012-08-14 14:26:28 -04004850 if (!lpfc_cmd || !lpfc_cmd->pCmd) {
James Smart876dd7d2012-09-29 11:31:28 -04004851 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarteee88772010-09-29 11:19:08 -04004852 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4853 "2873 SCSI Layer I/O Abort Request IO CMPL Status "
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04004854 "x%x ID %d LUN %d\n",
James Smart3a707302012-06-12 13:54:42 -04004855 SUCCESS, cmnd->device->id, cmnd->device->lun);
James Smarteee88772010-09-29 11:19:08 -04004856 return SUCCESS;
4857 }
dea31012005-04-17 16:05:31 -05004858
James Smart4f2e66c2012-05-09 21:17:07 -04004859 iocb = &lpfc_cmd->cur_iocbq;
4860 /* the command is in process of being cancelled */
4861 if (!(iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
James Smart876dd7d2012-09-29 11:31:28 -04004862 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004863 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4864 "3169 SCSI Layer abort requested I/O has been "
4865 "cancelled by LLD.\n");
4866 return FAILED;
4867 }
dea31012005-04-17 16:05:31 -05004868 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004869 * If pCmd field of the corresponding lpfc_scsi_buf structure
4870 * points to a different SCSI command, then the driver has
4871 * already completed this command, but the midlayer did not
James Smart4f2e66c2012-05-09 21:17:07 -04004872 * see the completion before the eh fired. Just return SUCCESS.
dea31012005-04-17 16:05:31 -05004873 */
James Smart4f2e66c2012-05-09 21:17:07 -04004874 if (lpfc_cmd->pCmd != cmnd) {
4875 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4876 "3170 SCSI Layer abort requested I/O has been "
4877 "completed by LLD.\n");
4878 goto out_unlock;
4879 }
dea31012005-04-17 16:05:31 -05004880
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004881 BUG_ON(iocb->context1 != lpfc_cmd);
dea31012005-04-17 16:05:31 -05004882
James Smart4f2e66c2012-05-09 21:17:07 -04004883 abtsiocb = __lpfc_sli_get_iocbq(phba);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004884 if (abtsiocb == NULL) {
4885 ret = FAILED;
James Smart4f2e66c2012-05-09 21:17:07 -04004886 goto out_unlock;
dea31012005-04-17 16:05:31 -05004887 }
4888
dea31012005-04-17 16:05:31 -05004889 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004890 * The scsi command can not be in txq and it is in flight because the
4891 * pCmd is still pointig at the SCSI command we have to abort. There
4892 * is no need to search the txcmplq. Just send an abort to the FW.
dea31012005-04-17 16:05:31 -05004893 */
dea31012005-04-17 16:05:31 -05004894
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004895 cmd = &iocb->iocb;
4896 icmd = &abtsiocb->iocb;
4897 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
4898 icmd->un.acxri.abortContextTag = cmd->ulpContext;
James Smart3772a992009-05-22 14:50:54 -04004899 if (phba->sli_rev == LPFC_SLI_REV4)
4900 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
4901 else
4902 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05004903
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004904 icmd->ulpLe = 1;
4905 icmd->ulpClass = cmd->ulpClass;
James Smart5ffc2662009-11-18 15:39:44 -05004906
4907 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
4908 abtsiocb->fcp_wqidx = iocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05004909 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05004910
James Smart2e0fef82007-06-17 19:56:36 -05004911 if (lpfc_is_link_up(phba))
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004912 icmd->ulpCommand = CMD_ABORT_XRI_CN;
4913 else
4914 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea31012005-04-17 16:05:31 -05004915
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004916 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smart2e0fef82007-06-17 19:56:36 -05004917 abtsiocb->vport = vport;
James Smart4f2e66c2012-05-09 21:17:07 -04004918 /* no longer need the lock after this point */
James Smart876dd7d2012-09-29 11:31:28 -04004919 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004920
James Smart3772a992009-05-22 14:50:54 -04004921 if (lpfc_sli_issue_iocb(phba, LPFC_FCP_RING, abtsiocb, 0) ==
4922 IOCB_ERROR) {
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004923 lpfc_sli_release_iocbq(phba, abtsiocb);
4924 ret = FAILED;
4925 goto out;
4926 }
4927
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004928 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
James Smart45ed1192009-10-02 15:17:02 -04004929 lpfc_sli_handle_fast_ring_event(phba,
4930 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004931
James Smartfa61a542008-01-11 01:52:42 -05004932 lpfc_cmd->waitq = &waitq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004933 /* Wait for abort to complete */
James Smartfa61a542008-01-11 01:52:42 -05004934 wait_event_timeout(waitq,
4935 (lpfc_cmd->pCmd != cmnd),
James Smart256ec0d2013-04-17 20:14:58 -04004936 msecs_to_jiffies(2*vport->cfg_devloss_tmo*1000));
James Smartfa61a542008-01-11 01:52:42 -05004937 lpfc_cmd->waitq = NULL;
dea31012005-04-17 16:05:31 -05004938
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004939 if (lpfc_cmd->pCmd == cmnd) {
4940 ret = FAILED;
James Smarte8b62012007-08-02 11:10:09 -04004941 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4942 "0748 abort handler timed out waiting "
James Smart247ca942012-08-14 14:26:13 -04004943 "for abortng I/O (xri:x%x) to complete: "
4944 "ret %#x, ID %d, LUN %d\n",
4945 iocb->sli4_xritag, ret,
4946 cmnd->device->id, cmnd->device->lun);
dea31012005-04-17 16:05:31 -05004947 }
James Smart4f2e66c2012-05-09 21:17:07 -04004948 goto out;
dea31012005-04-17 16:05:31 -05004949
James Smart4f2e66c2012-05-09 21:17:07 -04004950out_unlock:
James Smart876dd7d2012-09-29 11:31:28 -04004951 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004952out:
James Smarte8b62012007-08-02 11:10:09 -04004953 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4954 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04004955 "LUN %d\n", ret, cmnd->device->id,
4956 cmnd->device->lun);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004957 return ret;
dea31012005-04-17 16:05:31 -05004958}
4959
James Smartbbb9d182009-06-10 17:23:16 -04004960static char *
4961lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
4962{
4963 switch (task_mgmt_cmd) {
4964 case FCP_ABORT_TASK_SET:
4965 return "ABORT_TASK_SET";
4966 case FCP_CLEAR_TASK_SET:
4967 return "FCP_CLEAR_TASK_SET";
4968 case FCP_BUS_RESET:
4969 return "FCP_BUS_RESET";
4970 case FCP_LUN_RESET:
4971 return "FCP_LUN_RESET";
4972 case FCP_TARGET_RESET:
4973 return "FCP_TARGET_RESET";
4974 case FCP_CLEAR_ACA:
4975 return "FCP_CLEAR_ACA";
4976 case FCP_TERMINATE_TASK:
4977 return "FCP_TERMINATE_TASK";
4978 default:
4979 return "unknown";
4980 }
4981}
4982
4983/**
4984 * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
4985 * @vport: The virtual port for which this call is being executed.
4986 * @rdata: Pointer to remote port local data
4987 * @tgt_id: Target ID of remote device.
4988 * @lun_id: Lun number for the TMF
4989 * @task_mgmt_cmd: type of TMF to send
4990 *
4991 * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
4992 * a remote port.
4993 *
4994 * Return Code:
4995 * 0x2003 - Error
4996 * 0x2002 - Success.
4997 **/
4998static int
4999lpfc_send_taskmgmt(struct lpfc_vport *vport, struct lpfc_rport_data *rdata,
5000 unsigned tgt_id, unsigned int lun_id,
5001 uint8_t task_mgmt_cmd)
5002{
5003 struct lpfc_hba *phba = vport->phba;
5004 struct lpfc_scsi_buf *lpfc_cmd;
5005 struct lpfc_iocbq *iocbq;
5006 struct lpfc_iocbq *iocbqrsp;
James Smart5989b8d2010-10-22 11:06:56 -04005007 struct lpfc_nodelist *pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005008 int ret;
5009 int status;
5010
James Smart5989b8d2010-10-22 11:06:56 -04005011 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
James Smartbbb9d182009-06-10 17:23:16 -04005012 return FAILED;
5013
James Smart19ca7602010-11-20 23:11:55 -05005014 lpfc_cmd = lpfc_get_scsi_buf(phba, rdata->pnode);
James Smartbbb9d182009-06-10 17:23:16 -04005015 if (lpfc_cmd == NULL)
5016 return FAILED;
5017 lpfc_cmd->timeout = 60;
5018 lpfc_cmd->rdata = rdata;
5019
5020 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
5021 task_mgmt_cmd);
5022 if (!status) {
5023 lpfc_release_scsi_buf(phba, lpfc_cmd);
5024 return FAILED;
5025 }
5026
5027 iocbq = &lpfc_cmd->cur_iocbq;
5028 iocbqrsp = lpfc_sli_get_iocbq(phba);
5029 if (iocbqrsp == NULL) {
5030 lpfc_release_scsi_buf(phba, lpfc_cmd);
5031 return FAILED;
5032 }
James Smart5a0916b2013-07-15 18:31:42 -04005033 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
James Smartbbb9d182009-06-10 17:23:16 -04005034
5035 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5036 "0702 Issue %s to TGT %d LUN %d "
James Smart6d368e52011-05-24 11:44:12 -04005037 "rpi x%x nlp_flag x%x Data: x%x x%x\n",
James Smartbbb9d182009-06-10 17:23:16 -04005038 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
James Smart6d368e52011-05-24 11:44:12 -04005039 pnode->nlp_rpi, pnode->nlp_flag, iocbq->sli4_xritag,
5040 iocbq->iocb_flag);
James Smartbbb9d182009-06-10 17:23:16 -04005041
5042 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
5043 iocbq, iocbqrsp, lpfc_cmd->timeout);
5044 if (status != IOCB_SUCCESS) {
5045 if (status == IOCB_TIMEDOUT) {
James Smartbbb9d182009-06-10 17:23:16 -04005046 ret = TIMEOUT_ERROR;
5047 } else
5048 ret = FAILED;
5049 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
5050 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smart6d368e52011-05-24 11:44:12 -04005051 "0727 TMF %s to TGT %d LUN %d failed (%d, %d) "
5052 "iocb_flag x%x\n",
James Smartbbb9d182009-06-10 17:23:16 -04005053 lpfc_taskmgmt_name(task_mgmt_cmd),
5054 tgt_id, lun_id, iocbqrsp->iocb.ulpStatus,
James Smart6d368e52011-05-24 11:44:12 -04005055 iocbqrsp->iocb.un.ulpWord[4],
5056 iocbq->iocb_flag);
James Smart2a9bf3d2010-06-07 15:24:45 -04005057 } else if (status == IOCB_BUSY)
5058 ret = FAILED;
5059 else
James Smartbbb9d182009-06-10 17:23:16 -04005060 ret = SUCCESS;
5061
5062 lpfc_sli_release_iocbq(phba, iocbqrsp);
5063
5064 if (ret != TIMEOUT_ERROR)
5065 lpfc_release_scsi_buf(phba, lpfc_cmd);
5066
5067 return ret;
5068}
5069
5070/**
5071 * lpfc_chk_tgt_mapped -
5072 * @vport: The virtual port to check on
5073 * @cmnd: Pointer to scsi_cmnd data structure.
5074 *
5075 * This routine delays until the scsi target (aka rport) for the
5076 * command exists (is present and logged in) or we declare it non-existent.
5077 *
5078 * Return code :
5079 * 0x2003 - Error
5080 * 0x2002 - Success
5081 **/
5082static int
5083lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
5084{
5085 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005086 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005087 unsigned long later;
5088
James Smart1c6f4ef52009-11-18 15:40:49 -05005089 if (!rdata) {
5090 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5091 "0797 Tgt Map rport failure: rdata x%p\n", rdata);
5092 return FAILED;
5093 }
5094 pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005095 /*
5096 * If target is not in a MAPPED state, delay until
5097 * target is rediscovered or devloss timeout expires.
5098 */
5099 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
5100 while (time_after(later, jiffies)) {
5101 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
5102 return FAILED;
5103 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
5104 return SUCCESS;
5105 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
5106 rdata = cmnd->device->hostdata;
5107 if (!rdata)
5108 return FAILED;
5109 pnode = rdata->pnode;
5110 }
5111 if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
5112 (pnode->nlp_state != NLP_STE_MAPPED_NODE))
5113 return FAILED;
5114 return SUCCESS;
5115}
5116
5117/**
5118 * lpfc_reset_flush_io_context -
5119 * @vport: The virtual port (scsi_host) for the flush context
5120 * @tgt_id: If aborting by Target contect - specifies the target id
5121 * @lun_id: If aborting by Lun context - specifies the lun id
5122 * @context: specifies the context level to flush at.
5123 *
5124 * After a reset condition via TMF, we need to flush orphaned i/o
5125 * contexts from the adapter. This routine aborts any contexts
5126 * outstanding, then waits for their completions. The wait is
5127 * bounded by devloss_tmo though.
5128 *
5129 * Return code :
5130 * 0x2003 - Error
5131 * 0x2002 - Success
5132 **/
5133static int
5134lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
5135 uint64_t lun_id, lpfc_ctx_cmd context)
5136{
5137 struct lpfc_hba *phba = vport->phba;
5138 unsigned long later;
5139 int cnt;
5140
5141 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
5142 if (cnt)
5143 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
5144 tgt_id, lun_id, context);
5145 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
5146 while (time_after(later, jiffies) && cnt) {
5147 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
5148 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
5149 }
5150 if (cnt) {
5151 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5152 "0724 I/O flush failure for context %s : cnt x%x\n",
5153 ((context == LPFC_CTX_LUN) ? "LUN" :
5154 ((context == LPFC_CTX_TGT) ? "TGT" :
5155 ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
5156 cnt);
5157 return FAILED;
5158 }
5159 return SUCCESS;
5160}
5161
James Smart9bad7672008-12-04 22:39:02 -05005162/**
James Smart3621a712009-04-06 18:47:14 -04005163 * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
James Smart9bad7672008-12-04 22:39:02 -05005164 * @cmnd: Pointer to scsi_cmnd data structure.
5165 *
James Smartbbb9d182009-06-10 17:23:16 -04005166 * This routine does a device reset by sending a LUN_RESET task management
James Smart9bad7672008-12-04 22:39:02 -05005167 * command.
5168 *
5169 * Return code :
5170 * 0x2003 - Error
James Smart3621a712009-04-06 18:47:14 -04005171 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05005172 **/
dea31012005-04-17 16:05:31 -05005173static int
James Smart7054a602007-04-25 09:52:34 -04005174lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05005175{
James Smart2e0fef82007-06-17 19:56:36 -05005176 struct Scsi_Host *shost = cmnd->device->host;
5177 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05005178 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005179 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005180 unsigned tgt_id = cmnd->device->id;
5181 unsigned int lun_id = cmnd->device->lun;
James Smartea2151b2008-09-07 11:52:10 -04005182 struct lpfc_scsi_event_header scsi_event;
James Smart3a707302012-06-12 13:54:42 -04005183 int status, ret = SUCCESS;
dea31012005-04-17 16:05:31 -05005184
James Smart1c6f4ef52009-11-18 15:40:49 -05005185 if (!rdata) {
5186 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5187 "0798 Device Reset rport failure: rdata x%p\n", rdata);
5188 return FAILED;
5189 }
5190 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04005191 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005192 if (status != 0 && status != SUCCESS)
James Smart589a52d2010-07-14 15:30:54 -04005193 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005194
5195 status = lpfc_chk_tgt_mapped(vport, cmnd);
5196 if (status == FAILED) {
5197 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5198 "0721 Device Reset rport failure: rdata x%p\n", rdata);
5199 return FAILED;
5200 }
5201
5202 scsi_event.event_type = FC_REG_SCSI_EVENT;
5203 scsi_event.subcategory = LPFC_EVENT_LUNRESET;
5204 scsi_event.lun = lun_id;
5205 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5206 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5207
5208 fc_host_post_vendor_event(shost, fc_get_event_number(),
5209 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
5210
5211 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
5212 FCP_LUN_RESET);
5213
5214 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5215 "0713 SCSI layer issued Device Reset (%d, %d) "
5216 "return x%x\n", tgt_id, lun_id, status);
5217
dea31012005-04-17 16:05:31 -05005218 /*
James Smartbbb9d182009-06-10 17:23:16 -04005219 * We have to clean up i/o as : they may be orphaned by the TMF;
5220 * or if the TMF failed, they may be in an indeterminate state.
5221 * So, continue on.
5222 * We will report success if all the i/o aborts successfully.
dea31012005-04-17 16:05:31 -05005223 */
James Smart3a707302012-06-12 13:54:42 -04005224 ret = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
James Smartbbb9d182009-06-10 17:23:16 -04005225 LPFC_CTX_LUN);
James Smart3a707302012-06-12 13:54:42 -04005226 return ret;
James Smartbbb9d182009-06-10 17:23:16 -04005227}
5228
5229/**
5230 * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
5231 * @cmnd: Pointer to scsi_cmnd data structure.
5232 *
5233 * This routine does a target reset by sending a TARGET_RESET task management
5234 * command.
5235 *
5236 * Return code :
5237 * 0x2003 - Error
5238 * 0x2002 - Success
5239 **/
5240static int
5241lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
5242{
5243 struct Scsi_Host *shost = cmnd->device->host;
5244 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5245 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005246 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005247 unsigned tgt_id = cmnd->device->id;
5248 unsigned int lun_id = cmnd->device->lun;
5249 struct lpfc_scsi_event_header scsi_event;
James Smart3a707302012-06-12 13:54:42 -04005250 int status, ret = SUCCESS;
James Smartbbb9d182009-06-10 17:23:16 -04005251
James Smart1c6f4ef52009-11-18 15:40:49 -05005252 if (!rdata) {
5253 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5254 "0799 Target Reset rport failure: rdata x%p\n", rdata);
5255 return FAILED;
5256 }
5257 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04005258 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005259 if (status != 0 && status != SUCCESS)
James Smart589a52d2010-07-14 15:30:54 -04005260 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005261
5262 status = lpfc_chk_tgt_mapped(vport, cmnd);
5263 if (status == FAILED) {
5264 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5265 "0722 Target Reset rport failure: rdata x%p\n", rdata);
5266 return FAILED;
dea31012005-04-17 16:05:31 -05005267 }
James Smartea2151b2008-09-07 11:52:10 -04005268
5269 scsi_event.event_type = FC_REG_SCSI_EVENT;
5270 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
5271 scsi_event.lun = 0;
5272 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5273 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5274
James Smartbbb9d182009-06-10 17:23:16 -04005275 fc_host_post_vendor_event(shost, fc_get_event_number(),
5276 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -04005277
James Smartbbb9d182009-06-10 17:23:16 -04005278 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
5279 FCP_TARGET_RESET);
dea31012005-04-17 16:05:31 -05005280
James Smarte8b62012007-08-02 11:10:09 -04005281 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smartbbb9d182009-06-10 17:23:16 -04005282 "0723 SCSI layer issued Target Reset (%d, %d) "
5283 "return x%x\n", tgt_id, lun_id, status);
5284
5285 /*
5286 * We have to clean up i/o as : they may be orphaned by the TMF;
5287 * or if the TMF failed, they may be in an indeterminate state.
5288 * So, continue on.
5289 * We will report success if all the i/o aborts successfully.
5290 */
James Smart3a707302012-06-12 13:54:42 -04005291 ret = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
5292 LPFC_CTX_TGT);
5293 return ret;
dea31012005-04-17 16:05:31 -05005294}
5295
James Smart9bad7672008-12-04 22:39:02 -05005296/**
James Smart3621a712009-04-06 18:47:14 -04005297 * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05005298 * @cmnd: Pointer to scsi_cmnd data structure.
5299 *
James Smartbbb9d182009-06-10 17:23:16 -04005300 * This routine does target reset to all targets on @cmnd->device->host.
5301 * This emulates Parallel SCSI Bus Reset Semantics.
James Smart9bad7672008-12-04 22:39:02 -05005302 *
James Smartbbb9d182009-06-10 17:23:16 -04005303 * Return code :
5304 * 0x2003 - Error
5305 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05005306 **/
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04005307static int
James Smart7054a602007-04-25 09:52:34 -04005308lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05005309{
James Smart2e0fef82007-06-17 19:56:36 -05005310 struct Scsi_Host *shost = cmnd->device->host;
5311 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05005312 struct lpfc_nodelist *ndlp = NULL;
James Smartea2151b2008-09-07 11:52:10 -04005313 struct lpfc_scsi_event_header scsi_event;
James Smartbbb9d182009-06-10 17:23:16 -04005314 int match;
5315 int ret = SUCCESS, status, i;
James Smartea2151b2008-09-07 11:52:10 -04005316
5317 scsi_event.event_type = FC_REG_SCSI_EVENT;
5318 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
5319 scsi_event.lun = 0;
5320 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
5321 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
5322
James Smartbbb9d182009-06-10 17:23:16 -04005323 fc_host_post_vendor_event(shost, fc_get_event_number(),
5324 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
dea31012005-04-17 16:05:31 -05005325
James Smartbf086112011-08-21 21:48:13 -04005326 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005327 if (status != 0 && status != SUCCESS)
James Smartbf086112011-08-21 21:48:13 -04005328 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005329
dea31012005-04-17 16:05:31 -05005330 /*
5331 * Since the driver manages a single bus device, reset all
5332 * targets known to the driver. Should any target reset
5333 * fail, this routine returns failure to the midlayer.
5334 */
James Smarte17da182006-07-06 15:49:25 -04005335 for (i = 0; i < LPFC_MAX_TARGET; i++) {
James Smart685f0bf2007-04-25 09:53:08 -04005336 /* Search for mapped node by target ID */
dea31012005-04-17 16:05:31 -05005337 match = 0;
James Smart2e0fef82007-06-17 19:56:36 -05005338 spin_lock_irq(shost->host_lock);
5339 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05005340 if (!NLP_CHK_NODE_ACT(ndlp))
5341 continue;
James Smarta6571c62012-10-31 14:44:42 -04005342 if (vport->phba->cfg_fcp2_no_tgt_reset &&
5343 (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE))
5344 continue;
James Smart685f0bf2007-04-25 09:53:08 -04005345 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
James Smart915caaa2008-06-14 22:52:38 -04005346 ndlp->nlp_sid == i &&
James Smart685f0bf2007-04-25 09:53:08 -04005347 ndlp->rport) {
dea31012005-04-17 16:05:31 -05005348 match = 1;
5349 break;
5350 }
5351 }
James Smart2e0fef82007-06-17 19:56:36 -05005352 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005353 if (!match)
5354 continue;
James Smartbbb9d182009-06-10 17:23:16 -04005355
5356 status = lpfc_send_taskmgmt(vport, ndlp->rport->dd_data,
5357 i, 0, FCP_TARGET_RESET);
5358
5359 if (status != SUCCESS) {
James Smarte8b62012007-08-02 11:10:09 -04005360 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5361 "0700 Bus Reset on target %d failed\n",
5362 i);
James Smart915caaa2008-06-14 22:52:38 -04005363 ret = FAILED;
dea31012005-04-17 16:05:31 -05005364 }
5365 }
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005366 /*
James Smartbbb9d182009-06-10 17:23:16 -04005367 * We have to clean up i/o as : they may be orphaned by the TMFs
5368 * above; or if any of the TMFs failed, they may be in an
5369 * indeterminate state.
5370 * We will report success if all the i/o aborts successfully.
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005371 */
James Smartbbb9d182009-06-10 17:23:16 -04005372
5373 status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
5374 if (status != SUCCESS)
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005375 ret = FAILED;
James Smartbbb9d182009-06-10 17:23:16 -04005376
James Smarte8b62012007-08-02 11:10:09 -04005377 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5378 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea31012005-04-17 16:05:31 -05005379 return ret;
5380}
5381
James Smart9bad7672008-12-04 22:39:02 -05005382/**
James Smart27b01b82012-05-09 21:19:44 -04005383 * lpfc_host_reset_handler - scsi_host_template eh_host_reset_handler entry pt
5384 * @cmnd: Pointer to scsi_cmnd data structure.
5385 *
5386 * This routine does host reset to the adaptor port. It brings the HBA
5387 * offline, performs a board restart, and then brings the board back online.
5388 * The lpfc_offline calls lpfc_sli_hba_down which will abort and local
5389 * reject all outstanding SCSI commands to the host and error returned
5390 * back to SCSI mid-level. As this will be SCSI mid-level's last resort
5391 * of error handling, it will only return error if resetting of the adapter
5392 * is not successful; in all other cases, will return success.
5393 *
5394 * Return code :
5395 * 0x2003 - Error
5396 * 0x2002 - Success
5397 **/
5398static int
5399lpfc_host_reset_handler(struct scsi_cmnd *cmnd)
5400{
5401 struct Scsi_Host *shost = cmnd->device->host;
5402 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5403 struct lpfc_hba *phba = vport->phba;
5404 int rc, ret = SUCCESS;
5405
James Smarta88dbb62013-04-17 20:18:39 -04005406 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5407 "3172 SCSI layer issued Host Reset Data:\n");
5408
James Smart618a5232012-06-12 13:54:36 -04005409 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart27b01b82012-05-09 21:19:44 -04005410 lpfc_offline(phba);
5411 rc = lpfc_sli_brdrestart(phba);
5412 if (rc)
5413 ret = FAILED;
James Smarta88dbb62013-04-17 20:18:39 -04005414 rc = lpfc_online(phba);
5415 if (rc)
5416 ret = FAILED;
James Smart27b01b82012-05-09 21:19:44 -04005417 lpfc_unblock_mgmt_io(phba);
5418
James Smarta88dbb62013-04-17 20:18:39 -04005419 if (ret == FAILED) {
5420 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5421 "3323 Failed host reset, bring it offline\n");
5422 lpfc_sli4_offline_eratt(phba);
5423 }
James Smart27b01b82012-05-09 21:19:44 -04005424 return ret;
5425}
5426
5427/**
James Smart3621a712009-04-06 18:47:14 -04005428 * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
James Smart9bad7672008-12-04 22:39:02 -05005429 * @sdev: Pointer to scsi_device.
5430 *
5431 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
5432 * globally available list of scsi buffers. This routine also makes sure scsi
5433 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
5434 * of scsi buffer exists for the lifetime of the driver.
5435 *
5436 * Return codes:
5437 * non-0 - Error
5438 * 0 - Success
5439 **/
dea31012005-04-17 16:05:31 -05005440static int
dea31012005-04-17 16:05:31 -05005441lpfc_slave_alloc(struct scsi_device *sdev)
5442{
James Smart2e0fef82007-06-17 19:56:36 -05005443 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5444 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005445 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
James Smart3772a992009-05-22 14:50:54 -04005446 uint32_t total = 0;
dea31012005-04-17 16:05:31 -05005447 uint32_t num_to_alloc = 0;
James Smart3772a992009-05-22 14:50:54 -04005448 int num_allocated = 0;
James Smartd7c47992010-06-08 18:31:54 -04005449 uint32_t sdev_cnt;
dea31012005-04-17 16:05:31 -05005450
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005451 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05005452 return -ENXIO;
5453
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005454 sdev->hostdata = rport->dd_data;
James Smartd7c47992010-06-08 18:31:54 -04005455 sdev_cnt = atomic_inc_return(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05005456
5457 /*
5458 * Populate the cmds_per_lun count scsi_bufs into this host's globally
5459 * available list of scsi buffers. Don't allocate more than the
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04005460 * HBA limit conveyed to the midlayer via the host structure. The
5461 * formula accounts for the lun_queue_depth + error handlers + 1
5462 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea31012005-04-17 16:05:31 -05005463 */
5464 total = phba->total_scsi_bufs;
James Smart3de2a652007-08-02 11:09:59 -04005465 num_to_alloc = vport->cfg_lun_queue_depth + 2;
James Smart92d7f7b2007-06-17 19:56:38 -05005466
James Smartd7c47992010-06-08 18:31:54 -04005467 /* If allocated buffers are enough do nothing */
5468 if ((sdev_cnt * (vport->cfg_lun_queue_depth + 2)) < total)
5469 return 0;
5470
James Smart92d7f7b2007-06-17 19:56:38 -05005471 /* Allow some exchanges to be available always to complete discovery */
5472 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04005473 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5474 "0704 At limitation of %d preallocated "
5475 "command buffers\n", total);
dea31012005-04-17 16:05:31 -05005476 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05005477 /* Allow some exchanges to be available always to complete discovery */
5478 } else if (total + num_to_alloc >
5479 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04005480 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5481 "0705 Allocation request of %d "
5482 "command buffers will exceed max of %d. "
5483 "Reducing allocation request to %d.\n",
5484 num_to_alloc, phba->cfg_hba_queue_depth,
5485 (phba->cfg_hba_queue_depth - total));
dea31012005-04-17 16:05:31 -05005486 num_to_alloc = phba->cfg_hba_queue_depth - total;
5487 }
James Smart3772a992009-05-22 14:50:54 -04005488 num_allocated = lpfc_new_scsi_buf(vport, num_to_alloc);
5489 if (num_to_alloc != num_allocated) {
James Smart96f70772013-04-17 20:16:15 -04005490 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5491 "0708 Allocation request of %d "
5492 "command buffers did not succeed. "
5493 "Allocated %d buffers.\n",
5494 num_to_alloc, num_allocated);
dea31012005-04-17 16:05:31 -05005495 }
James Smart1c6f4ef52009-11-18 15:40:49 -05005496 if (num_allocated > 0)
5497 phba->total_scsi_bufs += num_allocated;
dea31012005-04-17 16:05:31 -05005498 return 0;
5499}
5500
James Smart9bad7672008-12-04 22:39:02 -05005501/**
James Smart3621a712009-04-06 18:47:14 -04005502 * lpfc_slave_configure - scsi_host_template slave_configure entry point
James Smart9bad7672008-12-04 22:39:02 -05005503 * @sdev: Pointer to scsi_device.
5504 *
5505 * This routine configures following items
5506 * - Tag command queuing support for @sdev if supported.
James Smart9bad7672008-12-04 22:39:02 -05005507 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
5508 *
5509 * Return codes:
5510 * 0 - Success
5511 **/
dea31012005-04-17 16:05:31 -05005512static int
5513lpfc_slave_configure(struct scsi_device *sdev)
5514{
James Smart2e0fef82007-06-17 19:56:36 -05005515 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5516 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05005517
5518 if (sdev->tagged_supported)
James Smart3de2a652007-08-02 11:09:59 -04005519 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05005520 else
James Smart3de2a652007-08-02 11:09:59 -04005521 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05005522
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005523 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04005524 lpfc_sli_handle_fast_ring_event(phba,
5525 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005526 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
5527 lpfc_poll_rearm_timer(phba);
5528 }
5529
dea31012005-04-17 16:05:31 -05005530 return 0;
5531}
5532
James Smart9bad7672008-12-04 22:39:02 -05005533/**
James Smart3621a712009-04-06 18:47:14 -04005534 * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
James Smart9bad7672008-12-04 22:39:02 -05005535 * @sdev: Pointer to scsi_device.
5536 *
5537 * This routine sets @sdev hostatdata filed to null.
5538 **/
dea31012005-04-17 16:05:31 -05005539static void
5540lpfc_slave_destroy(struct scsi_device *sdev)
5541{
James Smartd7c47992010-06-08 18:31:54 -04005542 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5543 struct lpfc_hba *phba = vport->phba;
5544 atomic_dec(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05005545 sdev->hostdata = NULL;
5546 return;
5547}
5548
James Smart92d7f7b2007-06-17 19:56:38 -05005549
dea31012005-04-17 16:05:31 -05005550struct scsi_host_template lpfc_template = {
5551 .module = THIS_MODULE,
5552 .name = LPFC_DRIVER_NAME,
5553 .info = lpfc_info,
5554 .queuecommand = lpfc_queuecommand,
5555 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04005556 .eh_device_reset_handler = lpfc_device_reset_handler,
5557 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart7054a602007-04-25 09:52:34 -04005558 .eh_bus_reset_handler = lpfc_bus_reset_handler,
James Smart27b01b82012-05-09 21:19:44 -04005559 .eh_host_reset_handler = lpfc_host_reset_handler,
dea31012005-04-17 16:05:31 -05005560 .slave_alloc = lpfc_slave_alloc,
5561 .slave_configure = lpfc_slave_configure,
5562 .slave_destroy = lpfc_slave_destroy,
James Smart47a86172007-04-25 09:53:22 -04005563 .scan_finished = lpfc_scan_finished,
dea31012005-04-17 16:05:31 -05005564 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05005565 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
dea31012005-04-17 16:05:31 -05005566 .cmd_per_lun = LPFC_CMD_PER_LUN,
5567 .use_clustering = ENABLE_CLUSTERING,
James Smart2e0fef82007-06-17 19:56:36 -05005568 .shost_attrs = lpfc_hba_attrs,
James.Smart@Emulex.Com564b2962005-06-25 10:34:17 -04005569 .max_sectors = 0xFFFF,
James Smartf1c3b0f2009-07-19 10:01:32 -04005570 .vendor_id = LPFC_NL_VENDOR_ID,
James Smart5ffc2662009-11-18 15:39:44 -05005571 .change_queue_depth = lpfc_change_queue_depth,
James Smartfe8f7f92013-01-03 15:43:03 -05005572 .change_queue_type = lpfc_change_queue_type,
dea31012005-04-17 16:05:31 -05005573};
James Smart3de2a652007-08-02 11:09:59 -04005574
5575struct scsi_host_template lpfc_vport_template = {
5576 .module = THIS_MODULE,
5577 .name = LPFC_DRIVER_NAME,
5578 .info = lpfc_info,
5579 .queuecommand = lpfc_queuecommand,
5580 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04005581 .eh_device_reset_handler = lpfc_device_reset_handler,
5582 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart3de2a652007-08-02 11:09:59 -04005583 .eh_bus_reset_handler = lpfc_bus_reset_handler,
5584 .slave_alloc = lpfc_slave_alloc,
5585 .slave_configure = lpfc_slave_configure,
5586 .slave_destroy = lpfc_slave_destroy,
5587 .scan_finished = lpfc_scan_finished,
5588 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05005589 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
James Smart3de2a652007-08-02 11:09:59 -04005590 .cmd_per_lun = LPFC_CMD_PER_LUN,
5591 .use_clustering = ENABLE_CLUSTERING,
5592 .shost_attrs = lpfc_vport_attrs,
5593 .max_sectors = 0xFFFF,
James Smart5ffc2662009-11-18 15:39:44 -05005594 .change_queue_depth = lpfc_change_queue_depth,
James Smartfe8f7f92013-01-03 15:43:03 -05005595 .change_queue_type = lpfc_change_queue_type,
James Smart3de2a652007-08-02 11:09:59 -04005596};